From patchwork Wed Oct 17 01:24:22 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nicolin Chen X-Patchwork-Id: 10644493 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 3E53715E2 for ; Wed, 17 Oct 2018 01:25:02 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 2F6242A419 for ; Wed, 17 Oct 2018 01:25:02 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 220242A426; Wed, 17 Oct 2018 01:25:02 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-8.0 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FROM,MAILING_LIST_MULTI,RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id C38922A419 for ; Wed, 17 Oct 2018 01:25:01 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726691AbeJQJRo (ORCPT ); Wed, 17 Oct 2018 05:17:44 -0400 Received: from mail-pf1-f194.google.com ([209.85.210.194]:46049 "EHLO mail-pf1-f194.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727020AbeJQJRo (ORCPT ); Wed, 17 Oct 2018 05:17:44 -0400 Received: by mail-pf1-f194.google.com with SMTP id u12-v6so12306999pfn.12; Tue, 16 Oct 2018 18:24:34 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=jpZN/UlmIedOp/RKX94XiVdcF9ZfG+QGI91FHkbRWhk=; b=uQVuRfKtHlivOgY0ZxSOL9P+Hxz4gRsS2sD/9p0zKKEsIc6ZmC5IuhVYWLRVmUsbc8 68JSzV2PfmYzj/saIZcAjhnpJ4yKxEkGComQyCOnWDGkBXF8iwEs3axTdI94YRFkPDRR v55r5B7VJ1drDS3topuadQJPFtlctvYrCQmfwUzJuNy/4OB/RQyvpdExIZuCCn3Tb6Ia 1ZpekuXBaaV83/lD0icqDODzwTdHIqQbeBQgyW4rDVIxBuv0HNlWRIf4d7X+DsjOkbIy Bb6Yr+wLGPj6RgTqHMDTdkdyCVHWPQcch4RTnu4WUeeNfdQ7C2GnoYOBOHfNHHOoAs4I sjbw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=jpZN/UlmIedOp/RKX94XiVdcF9ZfG+QGI91FHkbRWhk=; b=Yabwtaiz8woRV13HMt4KqWlM83WOhCmeg6TapxlNYVBaepm9BnkJdYf8ePlbaXUJwf WYr46AMURpR4Ik43xCTtODeLOYyWugT6mY6bTADSkLpCBmtUlCJnA36tNSyPL4ioLTkX h0VeSMtVW1/QWqcH7/qLszqno4x2i/S4AjJmgIoRJvGe51RmgojE3iYkKzL5zpPA3rYb MqmP6DfKRseeVUU6gpX+LI/bmoXMmAGCgkFujQxioMyWxhFPgwZuzRPNS6mv/Y469YI4 xq0ixNFZA7xqbtBhlthY/La+kR1LNes1CvNNB1D0LNLtbI42Kt56uydy8CPmxPEwjt1H ThDg== X-Gm-Message-State: ABuFfoiTM1oDo4zVQMlQK8zmPcaMpfLsGrgPasN9a06Kx3+SReL2SMWQ g21w0OGjmgwwdZgXZ7NzOCE= X-Google-Smtp-Source: ACcGV62dkkPktNSuueU1ZXn0foP9qAQS7q6lJUax7eTR8b1CYQLGgSa6Gt0TlpFE97ivKlG7DrUPXQ== X-Received: by 2002:a62:15ce:: with SMTP id 197-v6mr15679172pfv.222.1539739473915; Tue, 16 Oct 2018 18:24:33 -0700 (PDT) Received: from Asurada-Nvidia.nvidia.com (thunderhill.nvidia.com. [216.228.112.22]) by smtp.gmail.com with ESMTPSA id n64-v6sm22841851pfi.185.2018.10.16.18.24.33 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 16 Oct 2018 18:24:33 -0700 (PDT) From: Nicolin Chen To: jdelvare@suse.com, linux@roeck-us.net Cc: linux-hwmon@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH 1/5] hwmon: (core) Inherit power properties to hdev Date: Tue, 16 Oct 2018 18:24:22 -0700 Message-Id: <20181017012426.26958-2-nicoleotsuka@gmail.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20181017012426.26958-1-nicoleotsuka@gmail.com> References: <20181017012426.26958-1-nicoleotsuka@gmail.com> Sender: linux-hwmon-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-hwmon@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP The new hdev is a child device related to the original parent hwmon driver and its device. However, it doesn't support the power features, typically being defined in the parent driver. So this patch inherits three necessary power properties from the parent dev to hdev: power, pm_domain and driver pointers. Note that the dev->driver pointer is the place that contains a dev_pm_ops pointer defined in the parent device driver and the pm runtime core also checks this pointer: if (!cb && dev->driver && dev->driver->pm) Signed-off-by: Nicolin Chen --- drivers/hwmon/hwmon.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/drivers/hwmon/hwmon.c b/drivers/hwmon/hwmon.c index 975c95169884..7c064e1218ba 100644 --- a/drivers/hwmon/hwmon.c +++ b/drivers/hwmon/hwmon.c @@ -625,6 +625,9 @@ __hwmon_device_register(struct device *dev, const char *name, void *drvdata, hwdev->name = name; hdev->class = &hwmon_class; hdev->parent = dev; + hdev->driver = dev->driver; + hdev->power = dev->power; + hdev->pm_domain = dev->pm_domain; hdev->of_node = dev ? dev->of_node : NULL; hwdev->chip = chip; dev_set_drvdata(hdev, drvdata); From patchwork Wed Oct 17 01:24:23 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nicolin Chen X-Patchwork-Id: 10644491 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 2751213AD for ; Wed, 17 Oct 2018 01:24:58 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 16E3E2A419 for ; Wed, 17 Oct 2018 01:24:58 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 0891C2A426; Wed, 17 Oct 2018 01:24:58 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-8.0 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FROM,MAILING_LIST_MULTI,RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id B2D0F2A419 for ; Wed, 17 Oct 2018 01:24:57 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727158AbeJQJRq (ORCPT ); Wed, 17 Oct 2018 05:17:46 -0400 Received: from mail-pf1-f195.google.com ([209.85.210.195]:43076 "EHLO mail-pf1-f195.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727108AbeJQJRp (ORCPT ); Wed, 17 Oct 2018 05:17:45 -0400 Received: by mail-pf1-f195.google.com with SMTP id p24-v6so12319680pff.10; Tue, 16 Oct 2018 18:24:35 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=cZrnXJgpaLW8wt9ct/fjPUh76p1CqfOkjQzMlSnZDKI=; b=rrEc41ih1H/0RdIOtR2MK0KuQuFiGiL6x97XOrhcVGKupcpN9gLBdNB2BmCL4egfs5 8SRYfsMnOgB+VA2z5Y1xSKvaelYo9zNum83MeUjaLPaI9OwyBYIaP5R33EoOr/Tcy4U7 Oc/ejs9vI4pEWE25D0yv/EvDWLVW19fVHCFZNJwMg0t2mVeL86ZRzhw75qi2gnWdzo+Q zkwGNdTky7dkJfxIaSee5MFzJKadDh9eQpqgRSLxWByYdV/g6bno8648eiu5/YL4I2ah IfvM/IT3KOUUfVeJKNhB4+rpP0ixRF39+pvysdECIq/GYLpAr9Og3KjOahUoIto0Ddoe UMyQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=cZrnXJgpaLW8wt9ct/fjPUh76p1CqfOkjQzMlSnZDKI=; b=a6tZAQc0iws5+oJSie78hMZbwFNHkSmDWPSJZh9yLYYdb5kiTiMff97uAEtq7ArbE+ uV45gNNmDEp4G5nkgCzEFEsR8epiG8wpx7roEtfrreS4PWkcDR0eJtyA4zwWBE876ED/ ntuGp//1PG8dJbwB2PkeIWvPFPqCR+mJK0JzNMua9mJx+2ARhpAkJTjUDVKHhKYHPGlq d+0p/0KNp1KISU+QdxLMXblPW2u0G5rzDyr4gxFUS/RJJJkgQ8CumJ8a06nXcPNtPjfC SwDYBA7rvladgMjU9EOQhLanKs13JlIDArew60CKKTAaqPfRhEJHOy7CsE56esl117WT F61w== X-Gm-Message-State: ABuFfoiAB+HMCvsth59OBldOtS9UYGPCsvB4VxTxD6Pqk3fuaICoMInz qhPw785ykVm0LR+d3q4bhq/3nr46 X-Google-Smtp-Source: ACcGV63HxtBWJckFAFod1epEucyG0sr5Hlv8m1PtRMdrwEpmI5lXf56Mf8ZwwOoaGsqCAz4c87ZPrg== X-Received: by 2002:a62:583:: with SMTP id 125-v6mr12152154pff.186.1539739474592; Tue, 16 Oct 2018 18:24:34 -0700 (PDT) Received: from Asurada-Nvidia.nvidia.com (thunderhill.nvidia.com. [216.228.112.22]) by smtp.gmail.com with ESMTPSA id n64-v6sm22841851pfi.185.2018.10.16.18.24.33 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 16 Oct 2018 18:24:34 -0700 (PDT) From: Nicolin Chen To: jdelvare@suse.com, linux@roeck-us.net Cc: linux-hwmon@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH 2/5] hwmon: (ina3221) Return -ENODATA for two alarms attributes Date: Tue, 16 Oct 2018 18:24:23 -0700 Message-Id: <20181017012426.26958-3-nicoleotsuka@gmail.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20181017012426.26958-1-nicoleotsuka@gmail.com> References: <20181017012426.26958-1-nicoleotsuka@gmail.com> Sender: linux-hwmon-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-hwmon@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP There is nothing critically wrong to read these two attributes without having a is_enabled() check at this point. But reading the MASK_ENABLE register would clear the CVRF bit according to the datasheet. So it'd be safer to fence for disabled channels in order to add pm runtime feature. Signed-off-by: Nicolin Chen --- drivers/hwmon/ina3221.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/drivers/hwmon/ina3221.c b/drivers/hwmon/ina3221.c index d61688f04594..3e98b59108ee 100644 --- a/drivers/hwmon/ina3221.c +++ b/drivers/hwmon/ina3221.c @@ -200,6 +200,8 @@ static int ina3221_read_curr(struct device *dev, u32 attr, return 0; case hwmon_curr_crit_alarm: case hwmon_curr_max_alarm: + if (!ina3221_is_enabled(ina, channel)) + return -ENODATA; ret = regmap_field_read(ina->fields[reg], ®val); if (ret) return ret; From patchwork Wed Oct 17 01:24:24 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nicolin Chen X-Patchwork-Id: 10644485 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 2928113AD for ; Wed, 17 Oct 2018 01:24:39 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 185F02A424 for ; Wed, 17 Oct 2018 01:24:39 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 099982A432; Wed, 17 Oct 2018 01:24:39 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-8.0 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FROM,MAILING_LIST_MULTI,RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 91B1D2A424 for ; Wed, 17 Oct 2018 01:24:38 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727186AbeJQJRq (ORCPT ); Wed, 17 Oct 2018 05:17:46 -0400 Received: from mail-pf1-f193.google.com ([209.85.210.193]:38708 "EHLO mail-pf1-f193.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727020AbeJQJRq (ORCPT ); Wed, 17 Oct 2018 05:17:46 -0400 Received: by mail-pf1-f193.google.com with SMTP id f29-v6so12337162pff.5; Tue, 16 Oct 2018 18:24:35 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=14cljtA7/SkZpoUxuHPLkxQuZ59w2eqeIYUVwxGaV2A=; b=mOne4JNmLqR3Q1QpKwinW+vCvs060tKFMuE9NSWurLCJb0+vtbm+MEqQWvxYDfJp0d LYSC8k4bDpT/sY0GczK7sAH14RH38KVOLkaK11frhPTD4PnHRetcN3cLph2oP1xMVOgj CNl609ALEWT11BOhl3U3ZRKgb94dGAkhA3O1crrtwOu5frSTixu9G8dPNEyaK31xhiJf vrd6+7vJghja4NF0osYhVks1sNg09cFSb8IjXL+WCBhgi34fy8mxSIJQlyGrbz2hKg2d XHo44+iYAxpjKP6fYrfSsETneCgTIKCZnGwNVP01frkfPN6Cg/k5fvEyQ9BzQLok9/4w MIFw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=14cljtA7/SkZpoUxuHPLkxQuZ59w2eqeIYUVwxGaV2A=; b=s4aAIYpJbx3OipgOEUwX96tm8qVosE0FCV+8yssnZtWZlU55lO2KVp7OzziLvlj5fE unjf97Vm889QlPojUSHnbMnuB2Fic/UJ8cE5gyoxmt5mwcXiaoIMH2pSw+CRmjNy1CtR B2r//Uh2zq+T/Iq9HcKKKaaaJny/WgXsJVwJa0JAcmtkEsj8Ey6X/gNkuJNFkDr0for5 FceOAImqU3pPqS90BMvMv41Ef3+IUdoNTtGCLB7cCR+pYm+c/mEcy1E8dYq+LXfs9IUN N8FERIcjUbycE83vC1LiUO/H+zI3zytZiHCyfXc7YXErHQSvM71BLbPEwcYtV4O7zuCr 5l0Q== X-Gm-Message-State: ABuFfogy3DVRqRIq/GHwv7gtCLRIMmRmqhpYoiNacGLg1fWUaqjfP+VK GzyHTykXqbSwcwbUhmcJlqduC4yM X-Google-Smtp-Source: ACcGV619FRzv6yV6EeebPmpEIJu4p5w+6pS8TlR3TawRs1PvPJKFw633twXFsSXrB1V0Gp0oGQOGfw== X-Received: by 2002:a65:65c6:: with SMTP id y6-v6mr21926236pgv.233.1539739475311; Tue, 16 Oct 2018 18:24:35 -0700 (PDT) Received: from Asurada-Nvidia.nvidia.com (thunderhill.nvidia.com. [216.228.112.22]) by smtp.gmail.com with ESMTPSA id n64-v6sm22841851pfi.185.2018.10.16.18.24.34 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 16 Oct 2018 18:24:34 -0700 (PDT) From: Nicolin Chen To: jdelvare@suse.com, linux@roeck-us.net Cc: linux-hwmon@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH 3/5] hwmon: (ina3221) Serialize sysfs ABI accesses Date: Tue, 16 Oct 2018 18:24:24 -0700 Message-Id: <20181017012426.26958-4-nicoleotsuka@gmail.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20181017012426.26958-1-nicoleotsuka@gmail.com> References: <20181017012426.26958-1-nicoleotsuka@gmail.com> Sender: linux-hwmon-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-hwmon@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP This change adds a mutex to serialize accesses of sysfs attributes. This is required when polling CVRF bit of the MASK/ENABLE register because this bit is cleared on a read of this MASK/ENABLE register or a write to CONFIG register, which means that this bit might be accidentally cleared by reading other fields like alert flags. So this patch adds a mutex lock to protect the write() and read() callbacks. The read_string() callback won't need the lock since it just returns the label without touching any hardware register. Signed-off-by: Nicolin Chen --- drivers/hwmon/ina3221.c | 51 ++++++++++++++++++++++++++++++++++++----- 1 file changed, 45 insertions(+), 6 deletions(-) diff --git a/drivers/hwmon/ina3221.c b/drivers/hwmon/ina3221.c index 3e98b59108ee..5fc375bf6635 100644 --- a/drivers/hwmon/ina3221.c +++ b/drivers/hwmon/ina3221.c @@ -18,6 +18,7 @@ #include #include #include +#include #include #include @@ -94,12 +95,14 @@ struct ina3221_input { * @regmap: Register map of the device * @fields: Register fields of the device * @inputs: Array of channel input source specific structures + * @lock: mutex lock to serialize sysfs attribute accesses * @reg_config: Register value of INA3221_CONFIG */ struct ina3221_data { struct regmap *regmap; struct regmap_field *fields[F_MAX_FIELDS]; struct ina3221_input inputs[INA3221_NUM_CHANNELS]; + struct mutex lock; u32 reg_config; }; @@ -261,29 +264,53 @@ static int ina3221_write_enable(struct device *dev, int channel, bool enable) static int ina3221_read(struct device *dev, enum hwmon_sensor_types type, u32 attr, int channel, long *val) { + struct ina3221_data *ina = dev_get_drvdata(dev); + int ret; + + mutex_lock(&ina->lock); + switch (type) { case hwmon_in: /* 0-align channel ID */ - return ina3221_read_in(dev, attr, channel - 1, val); + ret = ina3221_read_in(dev, attr, channel - 1, val); + break; case hwmon_curr: - return ina3221_read_curr(dev, attr, channel, val); + ret = ina3221_read_curr(dev, attr, channel, val); + break; default: - return -EOPNOTSUPP; + ret = -EOPNOTSUPP; + break; } + + mutex_unlock(&ina->lock); + + return ret; } static int ina3221_write(struct device *dev, enum hwmon_sensor_types type, u32 attr, int channel, long val) { + struct ina3221_data *ina = dev_get_drvdata(dev); + int ret; + + mutex_lock(&ina->lock); + switch (type) { case hwmon_in: /* 0-align channel ID */ - return ina3221_write_enable(dev, channel - 1, val); + ret = ina3221_write_enable(dev, channel - 1, val); + break; case hwmon_curr: - return ina3221_write_curr(dev, attr, channel, val); + ret = ina3221_write_curr(dev, attr, channel, val); + break; default: - return -EOPNOTSUPP; + ret = -EOPNOTSUPP; + break; } + + mutex_unlock(&ina->lock); + + return ret; } static int ina3221_read_string(struct device *dev, enum hwmon_sensor_types type, @@ -578,6 +605,7 @@ static int ina3221_probe(struct i2c_client *client, if (ret) return ret; + mutex_init(&ina->lock); dev_set_drvdata(dev, ina); hwmon_dev = devm_hwmon_device_register_with_info(dev, client->name, ina, @@ -585,12 +613,22 @@ static int ina3221_probe(struct i2c_client *client, ina3221_groups); if (IS_ERR(hwmon_dev)) { dev_err(dev, "Unable to register hwmon device\n"); + mutex_destroy(&ina->lock); return PTR_ERR(hwmon_dev); } return 0; } +static int ina3221_remove(struct i2c_client *client) +{ + struct ina3221_data *ina = dev_get_drvdata(&client->dev); + + mutex_destroy(&ina->lock); + + return 0; +} + static int __maybe_unused ina3221_suspend(struct device *dev) { struct ina3221_data *ina = dev_get_drvdata(dev); @@ -659,6 +697,7 @@ MODULE_DEVICE_TABLE(i2c, ina3221_ids); static struct i2c_driver ina3221_i2c_driver = { .probe = ina3221_probe, + .remove = ina3221_remove, .driver = { .name = INA3221_DRIVER_NAME, .of_match_table = ina3221_of_match_table, From patchwork Wed Oct 17 01:24:25 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nicolin Chen X-Patchwork-Id: 10644489 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id E2AF913AD for ; Wed, 17 Oct 2018 01:24:49 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id D33062A419 for ; Wed, 17 Oct 2018 01:24:49 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id C41A42A426; Wed, 17 Oct 2018 01:24:49 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-8.0 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FROM,MAILING_LIST_MULTI,RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 5A89E2A419 for ; Wed, 17 Oct 2018 01:24:49 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727218AbeJQJRr (ORCPT ); Wed, 17 Oct 2018 05:17:47 -0400 Received: from mail-pf1-f194.google.com ([209.85.210.194]:38710 "EHLO mail-pf1-f194.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727108AbeJQJRq (ORCPT ); Wed, 17 Oct 2018 05:17:46 -0400 Received: by mail-pf1-f194.google.com with SMTP id f29-v6so12337177pff.5; Tue, 16 Oct 2018 18:24:36 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=vFfDD2gWNfwOEnszdMRio65CHH3Wl6oxXiNH3dNN12o=; b=nP4EKuPDeVqog11yorrYjuFKGgHU7wbO92gSQ7NGyEHUxPha3ll9A8k9eLoFf+snU4 j0HpNQaBvvrPd5+4Ut2UE7OSWRWCRQBWvT4llYsMNpea2rbVbr1zxbyHHqaGIXbEedJI rC1qoYV07v4ze/nB3ZJwy/EX12EeX6Banbf02fphY3firix0fO5imkff5MhV7pKuQjZS mtUnib9vyHCVH6X4NeK476tONzqXb9iKsPuHK7iftn0awSvObYZY5xpOEJ4LOa8Kxzcn jyJXE2Tu1iNDAd5R4Em8qoKFqwSF/CNMn+VRf4y+5Q94sjah7bkgDLiSKfo84Kx5U71M SC5g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=vFfDD2gWNfwOEnszdMRio65CHH3Wl6oxXiNH3dNN12o=; b=IW5kPqIWsGbTQmHLyz8ie3dnhs/+YCgbrLAvwjEwiaWUF02CQZ+IGcrLoXe6KDnrG2 bIF/FFTeYupRn+O8nDjwG97qSxjqQDie/YGvbgtOAyBNZ6l2sy9N9Buk8HPjaCUND5p7 zJ0QxL45gzpPyUztR5++9HFHy/SY0UYZjv/nlRbNElXxwn8Hmi3Cpzh1jYNEfy54eGdb efEipfvp99+KtDiIlb+LCvzemlHLzrtzFKh5Q0zwCdr3gpL/ZxWpHRBfbmlegmruDuXb +LcTjmT78Rew3RZLc/vzqs1nkzsJRVgsvvvJv7zZrqy0XfQa9mQBzMirln8/uqN0rFaX 5RVA== X-Gm-Message-State: ABuFfohpNaZ9ufgLRoiPDgU40oeHUdC43HmBZmvPoHKc7cGteIbjkI1V kO7K/f+VYQoc48gTyN0cVR8= X-Google-Smtp-Source: ACcGV613fZR3oQfqCsO7fgrCzWQhizzLnMIqix6UiPwKrdiJF7A9c5esIvSAOhThPUSvL/wfJTZEXg== X-Received: by 2002:a62:5ac6:: with SMTP id o189-v6mr24936315pfb.40.1539739476075; Tue, 16 Oct 2018 18:24:36 -0700 (PDT) Received: from Asurada-Nvidia.nvidia.com (thunderhill.nvidia.com. [216.228.112.22]) by smtp.gmail.com with ESMTPSA id n64-v6sm22841851pfi.185.2018.10.16.18.24.35 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 16 Oct 2018 18:24:35 -0700 (PDT) From: Nicolin Chen To: jdelvare@suse.com, linux@roeck-us.net Cc: linux-hwmon@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH 4/5] hwmon: (ina3221) Make sure data is ready after channel enabling Date: Tue, 16 Oct 2018 18:24:25 -0700 Message-Id: <20181017012426.26958-5-nicoleotsuka@gmail.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20181017012426.26958-1-nicoleotsuka@gmail.com> References: <20181017012426.26958-1-nicoleotsuka@gmail.com> Sender: linux-hwmon-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-hwmon@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP The data might need some time to get ready after channel enabling, although the data register is readable without CVRF being set. So this patch adds a CVRF polling to make sure that data register is updated with the new data. Signed-off-by: Nicolin Chen --- drivers/hwmon/ina3221.c | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/drivers/hwmon/ina3221.c b/drivers/hwmon/ina3221.c index 5fc375bf6635..160ddc404d73 100644 --- a/drivers/hwmon/ina3221.c +++ b/drivers/hwmon/ina3221.c @@ -45,6 +45,7 @@ #define INA3221_CONFIG_MODE_BUS BIT(1) #define INA3221_CONFIG_MODE_CONTINUOUS BIT(2) #define INA3221_CONFIG_CHx_EN(x) BIT(14 - (x)) +#define INA3221_CONFIG_CHs_EN_MASK GENMASK(14, 12) #define INA3221_RSHUNT_DEFAULT 10000 @@ -52,6 +53,9 @@ enum ina3221_fields { /* Configuration */ F_RST, + /* Status Flags */ + F_CVRF, + /* Alert Flags */ F_WF3, F_WF2, F_WF1, F_CF3, F_CF2, F_CF1, @@ -63,6 +67,7 @@ enum ina3221_fields { static const struct reg_field ina3221_reg_fields[] = { [F_RST] = REG_FIELD(INA3221_CONFIG, 15, 15), + [F_CVRF] = REG_FIELD(INA3221_MASK_ENABLE, 0, 0), [F_WF3] = REG_FIELD(INA3221_MASK_ENABLE, 3, 3), [F_WF2] = REG_FIELD(INA3221_MASK_ENABLE, 4, 4), [F_WF1] = REG_FIELD(INA3221_MASK_ENABLE, 5, 5), @@ -111,6 +116,19 @@ static inline bool ina3221_is_enabled(struct ina3221_data *ina, int channel) return ina->reg_config & INA3221_CONFIG_CHx_EN(channel); } +static inline int ina3221_wait_for_data_if_active(struct ina3221_data *ina) +{ + u32 cvrf; + + /* No need to wait if all channels are disabled */ + if ((ina->reg_config & INA3221_CONFIG_CHs_EN_MASK) == 0) + return 0; + + /* Polling the CVRF bit to make sure read data is ready */ + return regmap_field_read_poll_timeout(ina->fields[F_CVRF], + cvrf, cvrf, 100, 100000); +} + static int ina3221_read_value(struct ina3221_data *ina, unsigned int reg, int *val) { @@ -258,6 +276,13 @@ static int ina3221_write_enable(struct device *dev, int channel, bool enable) if (ret) return ret; + /* Make sure data conversion is finished */ + ret = ina3221_wait_for_data_if_active(ina); + if (ret) { + dev_err(dev, "Timed out at waiting for CVRF bit\n"); + return ret; + } + return 0; } @@ -676,6 +701,13 @@ static int __maybe_unused ina3221_resume(struct device *dev) if (ret) return ret; + /* Make sure data conversion is finished */ + ret = ina3221_wait_for_data_if_active(ina); + if (ret) { + dev_err(dev, "Timed out at waiting for CVRF bit\n"); + return ret; + } + return 0; } From patchwork Wed Oct 17 01:24:26 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nicolin Chen X-Patchwork-Id: 10644487 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 655D513AD for ; Wed, 17 Oct 2018 01:24:46 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 56D5B2A419 for ; Wed, 17 Oct 2018 01:24:46 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 4B03A2A426; Wed, 17 Oct 2018 01:24:46 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-8.0 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FROM,MAILING_LIST_MULTI,RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id A654A2A419 for ; Wed, 17 Oct 2018 01:24:45 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727254AbeJQJRt (ORCPT ); Wed, 17 Oct 2018 05:17:49 -0400 Received: from mail-pf1-f193.google.com ([209.85.210.193]:40821 "EHLO mail-pf1-f193.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727217AbeJQJRs (ORCPT ); Wed, 17 Oct 2018 05:17:48 -0400 Received: by mail-pf1-f193.google.com with SMTP id g21-v6so5328273pfi.7; Tue, 16 Oct 2018 18:24:37 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=gWfmCcpf7HgzojI1AcY47Q6G9hhldA69dpOLXL491Lo=; b=HQujuozW3vDZEBbemfDyuNW+ctdf9/tGDmrVWoSYVLHeb/7nHKRQccU11BF/MGMDvx y9VYY0nZlqJNPQJjrhjui3iIUkU3c7davttLHIhq4f68UgY/F3d8b8NZlqfM+WO+48PG vSTEDtwESiBpDJLEzLogsxh6dA6Lxhu6xi1qwwPaIY1kFvI/133Hdt8msyvIeGGiIRNW WkvEFjnAcuOLVUlFwcRVrqD3NPHRe/tAivusRl3y9TmusvCNqW+PRcLWraPUtgf7rTaT sAImsmANZZa8+y+JCo1NjYDoFX0ay7vZOgqohYq9l+WvckO1VWeWsK769VmKYuGWfhmj 5xHg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=gWfmCcpf7HgzojI1AcY47Q6G9hhldA69dpOLXL491Lo=; b=p5+Rs6K47QoD3FEFC9rjW2kWY9ZRTjuzcwQz4XQf3pl5OYXpyBk6heJ1Crm2biZIXG cyU2wc/NWMNwKw+4vj8o3O44n/hMhg6A30L+jww6hcsYPw2A0GkI4SNkWcSWoxHQ4B71 atisqPhlhulPP8+2zUP8oIlrJm/M6/X74Sh5eVuxqtdjMoqzb68vQOTAXHvwXPWdgPJ4 kemuIcOlfDeL/O9MBxfd2+l0/2Elhd67AXLhxDoWEHnR+TU/w3s9XyofSivFXihCtjY/ oY+vRviXXWS0J0+l2iN2hq856CcrWZtHNRWQFji7p8LNKak5O6vzvbJ0R5qIRoZn5FD+ qq3g== X-Gm-Message-State: ABuFfogyKMvZzatX8J8u4h72/1qmklcDEQditYuU7XcQ8k+4Sm3jDpIT kUOm5MA/wVu9VutqOM4PcBo= X-Google-Smtp-Source: ACcGV61vg9bKhdnnZ8nsIIuc+ZR8ZvjL3YN9QS18n2MgPUpMzNpbGhaqNhty5iQ9MIm3rNqcjy4Gkw== X-Received: by 2002:a62:c186:: with SMTP id i128-v6mr24542472pfg.248.1539739476829; Tue, 16 Oct 2018 18:24:36 -0700 (PDT) Received: from Asurada-Nvidia.nvidia.com (thunderhill.nvidia.com. [216.228.112.22]) by smtp.gmail.com with ESMTPSA id n64-v6sm22841851pfi.185.2018.10.16.18.24.36 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 16 Oct 2018 18:24:36 -0700 (PDT) From: Nicolin Chen To: jdelvare@suse.com, linux@roeck-us.net Cc: linux-hwmon@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH 5/5] hwmon: (ina3221) Add PM runtime support Date: Tue, 16 Oct 2018 18:24:26 -0700 Message-Id: <20181017012426.26958-6-nicoleotsuka@gmail.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20181017012426.26958-1-nicoleotsuka@gmail.com> References: <20181017012426.26958-1-nicoleotsuka@gmail.com> Sender: linux-hwmon-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-hwmon@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP If all three channels are disabled via in[123]_enable ABI, the driver could suspend the chip for power saving purpose. So this patch addsd the PM runtime support in order to gain more power control than system suspend and resume use case. For PM runtime, there are a few related changes happening: 1) Added a new explicit hdev device pointer for all the pm runtime callbacks. This is because hwmon core registers a child device for each hwmon driver. So there might be a mismatch between two device pointers in the driver if mixing using them. 2) Added a check in ina3221_is_enabled() to make sure that the chip is resumed. 3) Bypassed the unchanged status in ina3221_write_enable() in order to keep the pm runtime refcount being matched. 4) Removed the reset routine in the probe() by calling the resume() via pm_runtime_get_sync(), as they're similar. It's also necessary to do so to match initial refcount with the number of enabled channels. Signed-off-by: Nicolin Chen --- drivers/hwmon/ina3221.c | 104 +++++++++++++++++++++++++++++++--------- 1 file changed, 82 insertions(+), 22 deletions(-) diff --git a/drivers/hwmon/ina3221.c b/drivers/hwmon/ina3221.c index 160ddc404d73..5ad3ab22b07a 100644 --- a/drivers/hwmon/ina3221.c +++ b/drivers/hwmon/ina3221.c @@ -21,6 +21,7 @@ #include #include #include +#include #define INA3221_DRIVER_NAME "ina3221" @@ -47,6 +48,7 @@ #define INA3221_CONFIG_CHx_EN(x) BIT(14 - (x)) #define INA3221_CONFIG_CHs_EN_MASK GENMASK(14, 12) +#define INA3221_CONFIG_DEFAULT 0x7127 #define INA3221_RSHUNT_DEFAULT 10000 enum ina3221_fields { @@ -97,6 +99,7 @@ struct ina3221_input { /** * struct ina3221_data - device specific information + * @hdev: Device pointer of hwmon child device, used for pm runtime * @regmap: Register map of the device * @fields: Register fields of the device * @inputs: Array of channel input source specific structures @@ -104,6 +107,7 @@ struct ina3221_input { * @reg_config: Register value of INA3221_CONFIG */ struct ina3221_data { + struct device *hdev; struct regmap *regmap; struct regmap_field *fields[F_MAX_FIELDS]; struct ina3221_input inputs[INA3221_NUM_CHANNELS]; @@ -113,7 +117,8 @@ struct ina3221_data { static inline bool ina3221_is_enabled(struct ina3221_data *ina, int channel) { - return ina->reg_config & INA3221_CONFIG_CHx_EN(channel); + return pm_runtime_active(ina->hdev) && + (ina->reg_config & INA3221_CONFIG_CHx_EN(channel)); } static inline int ina3221_wait_for_data_if_active(struct ina3221_data *ina) @@ -262,19 +267,33 @@ static int ina3221_write_enable(struct device *dev, int channel, bool enable) { struct ina3221_data *ina = dev_get_drvdata(dev); u16 config, mask = INA3221_CONFIG_CHx_EN(channel); + u16 config_old = ina->reg_config & mask; int ret; config = enable ? mask : 0; + /* Bypass if enable status is not being changed */ + if (config_old == config) + return 0; + + /* For enabling routine, increase refcount and resume() at first */ + if (enable) { + ret = pm_runtime_get_sync(ina->hdev); + if (ret < 0) { + dev_err(dev, "Failed to get PM runtime"); + return ret; + } + } + /* Enable or disable the channel */ ret = regmap_update_bits(ina->regmap, INA3221_CONFIG, mask, config); if (ret) - return ret; + goto fail; /* Cache the latest config register value */ ret = regmap_read(ina->regmap, INA3221_CONFIG, &ina->reg_config); if (ret) - return ret; + goto fail; /* Make sure data conversion is finished */ ret = ina3221_wait_for_data_if_active(ina); @@ -283,7 +302,20 @@ static int ina3221_write_enable(struct device *dev, int channel, bool enable) return ret; } + /* For disabling routine, decrease refcount or suspend() at last */ + if (!enable) + pm_runtime_put_sync(ina->hdev); + return 0; + +fail: + if (enable) { + dev_err(dev, "Reverting channel%d enabling: %d\n", + channel, ret); + pm_runtime_put_sync(ina->hdev); + } + + return ret; } static int ina3221_read(struct device *dev, enum hwmon_sensor_types type, @@ -578,7 +610,6 @@ static int ina3221_probe(struct i2c_client *client, { struct device *dev = &client->dev; struct ina3221_data *ina; - struct device *hwmon_dev; int i, ret; ina = devm_kzalloc(dev, sizeof(*ina), GFP_KERNEL); @@ -610,44 +641,71 @@ static int ina3221_probe(struct i2c_client *client, return ret; } - ret = regmap_field_write(ina->fields[F_RST], true); - if (ret) { - dev_err(dev, "Unable to reset device\n"); - return ret; - } - - /* Sync config register after reset */ - ret = regmap_read(ina->regmap, INA3221_CONFIG, &ina->reg_config); - if (ret) - return ret; + /* The driver will be reset, so use reset value */ + ina->reg_config = INA3221_CONFIG_DEFAULT; /* Disable channels if their inputs are disconnected */ for (i = 0; i < INA3221_NUM_CHANNELS; i++) { if (ina->inputs[i].disconnected) ina->reg_config &= ~INA3221_CONFIG_CHx_EN(i); } - ret = regmap_write(ina->regmap, INA3221_CONFIG, ina->reg_config); - if (ret) - return ret; mutex_init(&ina->lock); dev_set_drvdata(dev, ina); - hwmon_dev = devm_hwmon_device_register_with_info(dev, client->name, ina, + /* Fence sysfs nodes till pm_runtime is resumed */ + mutex_lock(&ina->lock); + + /* Use the returned hdev for pm_runtime */ + ina->hdev = devm_hwmon_device_register_with_info(dev, client->name, ina, &ina3221_chip_info, ina3221_groups); - if (IS_ERR(hwmon_dev)) { + if (IS_ERR(ina->hdev)) { dev_err(dev, "Unable to register hwmon device\n"); - mutex_destroy(&ina->lock); - return PTR_ERR(hwmon_dev); + ret = PTR_ERR(ina->hdev); + goto fail_lock; } + /* Enable PM runtime -- status is suspended by default */ + pm_runtime_enable(ina->hdev); + + /* Initialize (resume) the device */ + for (i = 0; i < INA3221_NUM_CHANNELS; i++) { + if (ina->inputs[i].disconnected) + continue; + /* Match the refcount with number of enabled channels */ + ret = pm_runtime_get_sync(ina->hdev); + if (ret < 0) + goto fail_pm; + } + + mutex_unlock(&ina->lock); + return 0; + +fail_pm: + pm_runtime_disable(ina->hdev); + pm_runtime_set_suspended(ina->hdev); + for (i = 0; i < INA3221_NUM_CHANNELS; i++) + pm_runtime_put_noidle(ina->hdev); +fail_lock: + mutex_unlock(&ina->lock); + mutex_destroy(&ina->lock); + + return ret; } static int ina3221_remove(struct i2c_client *client) { struct ina3221_data *ina = dev_get_drvdata(&client->dev); + int i; + + pm_runtime_disable(ina->hdev); + pm_runtime_set_suspended(ina->hdev); + + /* Decrease the PM refcount */ + for (i = 0; i < INA3221_NUM_CHANNELS; i++) + pm_runtime_put_noidle(ina->hdev); mutex_destroy(&ina->lock); @@ -712,7 +770,9 @@ static int __maybe_unused ina3221_resume(struct device *dev) } static const struct dev_pm_ops ina3221_pm = { - SET_SYSTEM_SLEEP_PM_OPS(ina3221_suspend, ina3221_resume) + SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend, + pm_runtime_force_resume) + SET_RUNTIME_PM_OPS(ina3221_suspend, ina3221_resume, NULL) }; static const struct of_device_id ina3221_of_match_table[] = {