From patchwork Wed Sep 26 06:42:44 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nicolin Chen X-Patchwork-Id: 10615325 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 4A02815A6 for ; Wed, 26 Sep 2018 06:43:11 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 3BB8428684 for ; Wed, 26 Sep 2018 06:43:11 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 2E5B228710; Wed, 26 Sep 2018 06:43:11 +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 801EE28684 for ; Wed, 26 Sep 2018 06:43:10 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727038AbeIZMy0 (ORCPT ); Wed, 26 Sep 2018 08:54:26 -0400 Received: from mail-pg1-f193.google.com ([209.85.215.193]:47099 "EHLO mail-pg1-f193.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727157AbeIZMyZ (ORCPT ); Wed, 26 Sep 2018 08:54:25 -0400 Received: by mail-pg1-f193.google.com with SMTP id b129-v6so13382645pga.13; Tue, 25 Sep 2018 23:43:02 -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=o7s/I/J2d8DTZzq/9p3rNAbym0cArQaKgYfnOK1mWxU=; b=mqlbfqTFA95S/qt/wtmsdsNjrAzknlA2IRm6blVfKFcV2NPkw7FwTCBYdf/bk7plfP cuJJCJk+kDbxJZiVg68LINRB7y2+VJMeCKFc4f6pyLhZUIulXel+Ig9VMfLeuWEOs0Wu N9pJ8bVjucnphERLr2dx9jx2a7YGEphOj8QFoz1j4ma3QMAS+L4iFu6uHfdSPVfSF0LS K0ZYVi8TQRj+FJpixOqM0JTo3Yi6xmODqq97Q3kOG26Zrhv4JKOz8HKAla18MG6lS8ri lezzdcF9KNGZzD+nk6r8PqXO0G3kLFwU9HGCgTwPmtLzuXgbs4m+bgEPBNPdqsSvoAD5 BDIA== 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=o7s/I/J2d8DTZzq/9p3rNAbym0cArQaKgYfnOK1mWxU=; b=YpzpgjERn3iLVkwEMRAALyzE5U4NQhFzc3rZuOKFqqeMB4Ow/kby1WhyBVUMu70QDt wQczsiaHdiGiMJs2BaqibuA+8bgkQPoU2WyP1XJTv8vUHcLudS3i6jJozNjUkmsm5p+R 5zJ4ISWzjtYXYNwLvLaBr4ZQYEoEx9T2It+OD40PuZUCVokHJpG68SsxocQBqwfLUJVq 4tgCB8p53KfwjDpd8moE5Ln11m4ag7rTVbm56tDfpxdDQ+JWGuDDdrnTv1BmvlkfVmRP s4qDIMtjhwpNixfoQDuKmApAgemtzqTxYqtaVYxB8uGCzmRKS3wK0l6hp+qMR74NfGCz MPyA== X-Gm-Message-State: ABuFfojWA/A6VYIaU7KVFgehQvAfmf4q7iQKZHEtpuxY1FCuQxPMnYfm L87Rj02Jm17U4McSq2WTqgI= X-Google-Smtp-Source: ACcGV63poHSZWs95//t6Ex2HLHTMcCesbZmHzV4P1/bLmsqKGJMxQtcdJe3gQS4O38KoE7dAfsmMag== X-Received: by 2002:a62:d286:: with SMTP id c128-v6mr3344146pfg.14.1537944181720; Tue, 25 Sep 2018 23:43:01 -0700 (PDT) Received: from Asurada-Nvidia.nvidia.com (thunderhill.nvidia.com. [216.228.112.22]) by smtp.gmail.com with ESMTPSA id 71-v6sm5574861pfx.182.2018.09.25.23.43.01 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 25 Sep 2018 23:43:01 -0700 (PDT) From: Nicolin Chen To: jdelvare@suse.com, linux@roeck-us.net, corbet@lwn.net Cc: afd@ti.com, linux-hwmon@vger.kernel.org, linux-kernel@vger.kernel.org, linux-doc@vger.kernel.org Subject: [PATCH 1/2] hwmon: ina3221: Add power sysfs nodes Date: Tue, 25 Sep 2018 23:42:44 -0700 Message-Id: <20180926064245.4091-2-nicoleotsuka@gmail.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20180926064245.4091-1-nicoleotsuka@gmail.com> References: <20180926064245.4091-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 hwmon sysfs ABI supports powerX_input and powerX_crit. This can ease user space programs who care more about power in total than voltage or current individually. So this patch adds these two sysfs nodes for INA3221 driver. Signed-off-by: Nicolin Chen --- Documentation/hwmon/ina3221 | 4 + drivers/hwmon/ina3221.c | 145 ++++++++++++++++++++++++++++++++---- 2 files changed, 133 insertions(+), 16 deletions(-) diff --git a/Documentation/hwmon/ina3221 b/Documentation/hwmon/ina3221 index 2726038be5bd..6be64b553cd0 100644 --- a/Documentation/hwmon/ina3221 +++ b/Documentation/hwmon/ina3221 @@ -34,3 +34,7 @@ curr[123]_max Warning alert current(mA) setting, activates the average is above this value. curr[123]_max_alarm Warning alert current limit exceeded in[456]_input Shunt voltage(uV) for channels 1, 2, and 3 respectively +power[123]_input Power(uW) measurement channels +power[123]_crit Critical alert power(uW) setting, activates the + corresponding alarm when the respective power + is above this value diff --git a/drivers/hwmon/ina3221.c b/drivers/hwmon/ina3221.c index ce4d1f55e9cd..5890a2da3bfe 100644 --- a/drivers/hwmon/ina3221.c +++ b/drivers/hwmon/ina3221.c @@ -38,6 +38,8 @@ #define INA3221_WARN3 0x0c #define INA3221_MASK_ENABLE 0x0f +#define INA3221_BUS(x) (INA3221_BUS1 + ((x) << 1)) + #define INA3221_CONFIG_MODE_SHUNT BIT(1) #define INA3221_CONFIG_MODE_BUS BIT(2) #define INA3221_CONFIG_MODE_CONTINUOUS BIT(3) @@ -172,43 +174,50 @@ static ssize_t ina3221_show_shunt_voltage(struct device *dev, return snprintf(buf, PAGE_SIZE, "%d\n", voltage_uv); } -static ssize_t ina3221_show_current(struct device *dev, - struct device_attribute *attr, char *buf) +static int __ina3221_show_current(struct ina3221_data *ina, + unsigned int reg, int *current_ma) { - struct sensor_device_attribute *sd_attr = to_sensor_dev_attr(attr); - struct ina3221_data *ina = dev_get_drvdata(dev); - unsigned int reg = sd_attr->index; unsigned int channel = register_channel[reg]; struct ina3221_input *input = &ina->inputs[channel]; int resistance_uo = input->shunt_resistor; - int val, current_ma, voltage_nv, ret; + int val, voltage_nv, ret; + /* Read bus shunt voltage */ ret = ina3221_read_value(ina, reg, &val); if (ret) return ret; + + /* LSB (4th bit) is 40 uV (40000 nV) */ voltage_nv = val * 40000; - current_ma = DIV_ROUND_CLOSEST(voltage_nv, resistance_uo); + *current_ma = DIV_ROUND_CLOSEST(voltage_nv, resistance_uo); - return snprintf(buf, PAGE_SIZE, "%d\n", current_ma); + return 0; } -static ssize_t ina3221_set_current(struct device *dev, - struct device_attribute *attr, - const char *buf, size_t count) +static ssize_t ina3221_show_current(struct device *dev, + struct device_attribute *attr, char *buf) { struct sensor_device_attribute *sd_attr = to_sensor_dev_attr(attr); struct ina3221_data *ina = dev_get_drvdata(dev); unsigned int reg = sd_attr->index; - unsigned int channel = register_channel[reg]; - struct ina3221_input *input = &ina->inputs[channel]; - int resistance_uo = input->shunt_resistor; - int val, current_ma, voltage_uv, ret; + int current_ma, ret; - ret = kstrtoint(buf, 0, ¤t_ma); + ret = __ina3221_show_current(ina, reg, ¤t_ma); if (ret) return ret; + return snprintf(buf, PAGE_SIZE, "%d\n", current_ma); +} + +static int __ina3221_set_current(struct ina3221_data *ina, + unsigned int reg, int current_ma) +{ + unsigned int channel = register_channel[reg]; + struct ina3221_input *input = &ina->inputs[channel]; + int resistance_uo = input->shunt_resistor; + int val, voltage_uv, ret; + /* clamp current */ current_ma = clamp_val(current_ma, INT_MIN / resistance_uo, @@ -226,6 +235,26 @@ static ssize_t ina3221_set_current(struct device *dev, if (ret) return ret; + return 0; +} + +static ssize_t ina3221_set_current(struct device *dev, + struct device_attribute *attr, + const char *buf, size_t count) +{ + struct sensor_device_attribute *sd_attr = to_sensor_dev_attr(attr); + struct ina3221_data *ina = dev_get_drvdata(dev); + unsigned int reg = sd_attr->index; + int current_ma, ret; + + ret = kstrtoint(buf, 0, ¤t_ma); + if (ret) + return ret; + + ret = __ina3221_set_current(ina, reg, current_ma); + if (ret) + return ret; + return count; } @@ -278,6 +307,68 @@ static ssize_t ina3221_show_alert(struct device *dev, return snprintf(buf, PAGE_SIZE, "%d\n", regval); } +static ssize_t ina3221_show_power(struct device *dev, + struct device_attribute *attr, char *buf) +{ + struct sensor_device_attribute *sd_attr = to_sensor_dev_attr(attr); + struct ina3221_data *ina = dev_get_drvdata(dev); + unsigned int reg = sd_attr->index; + unsigned int channel = register_channel[reg]; + unsigned int reg_bus = INA3221_BUS(channel); + int val, current_ma, voltage_mv, ret; + s64 power_uw; + + /* Read bus voltage */ + ret = ina3221_read_value(ina, reg_bus, &val); + if (ret) + return ret; + + /* LSB (4th bit) is 8 mV */ + voltage_mv = val * 8; + + /* Read calculated current */ + ret = __ina3221_show_current(ina, reg, ¤t_ma); + if (ret) + return ret; + + power_uw = (s64)voltage_mv * current_ma; + + return snprintf(buf, PAGE_SIZE, "%lld\n", power_uw); +} + +static ssize_t ina3221_set_power(struct device *dev, + struct device_attribute *attr, + const char *buf, size_t count) +{ + struct sensor_device_attribute *sd_attr = to_sensor_dev_attr(attr); + struct ina3221_data *ina = dev_get_drvdata(dev); + unsigned int reg = sd_attr->index; + unsigned int channel = register_channel[reg]; + unsigned int reg_bus = INA3221_BUS(channel); + int val, current_ma, voltage_mv, ret; + s64 power_uw; + + ret = kstrtos64(buf, 0, &power_uw); + if (ret) + return ret; + + /* Read bus voltage */ + ret = ina3221_read_value(ina, reg_bus, &val); + if (ret) + return ret; + + /* LSB (4th bit) is 8 mV */ + voltage_mv = val * 8; + + current_ma = DIV_ROUND_CLOSEST(power_uw, voltage_mv); + + ret = __ina3221_set_current(ina, reg, current_ma); + if (ret) + return ret; + + return count; +} + /* input channel label */ static SENSOR_DEVICE_ATTR(in1_label, 0444, ina3221_show_label, NULL, INA3221_CHANNEL1); @@ -350,6 +441,22 @@ static SENSOR_DEVICE_ATTR(in5_input, S_IRUGO, static SENSOR_DEVICE_ATTR(in6_input, S_IRUGO, ina3221_show_shunt_voltage, NULL, INA3221_SHUNT3); +/* calculated power */ +static SENSOR_DEVICE_ATTR(power1_input, 0444, + ina3221_show_power, NULL, INA3221_SHUNT1); +static SENSOR_DEVICE_ATTR(power2_input, 0444, + ina3221_show_power, NULL, INA3221_SHUNT2); +static SENSOR_DEVICE_ATTR(power3_input, 0444, + ina3221_show_power, NULL, INA3221_SHUNT3); + +/* critical power */ +static SENSOR_DEVICE_ATTR(power1_crit, 0644, + ina3221_show_power, ina3221_set_power, INA3221_CRIT1); +static SENSOR_DEVICE_ATTR(power2_crit, 0644, + ina3221_show_power, ina3221_set_power, INA3221_CRIT2); +static SENSOR_DEVICE_ATTR(power3_crit, 0644, + ina3221_show_power, ina3221_set_power, INA3221_CRIT3); + static struct attribute *ina3221_attrs[] = { /* channel 1 -- make sure label at first */ &sensor_dev_attr_in1_label.dev_attr.attr, @@ -361,6 +468,8 @@ static struct attribute *ina3221_attrs[] = { &sensor_dev_attr_curr1_max.dev_attr.attr, &sensor_dev_attr_curr1_max_alarm.dev_attr.attr, &sensor_dev_attr_in4_input.dev_attr.attr, + &sensor_dev_attr_power1_input.dev_attr.attr, + &sensor_dev_attr_power1_crit.dev_attr.attr, /* channel 2 -- make sure label at first */ &sensor_dev_attr_in2_label.dev_attr.attr, @@ -372,6 +481,8 @@ static struct attribute *ina3221_attrs[] = { &sensor_dev_attr_curr2_max.dev_attr.attr, &sensor_dev_attr_curr2_max_alarm.dev_attr.attr, &sensor_dev_attr_in5_input.dev_attr.attr, + &sensor_dev_attr_power2_input.dev_attr.attr, + &sensor_dev_attr_power2_crit.dev_attr.attr, /* channel 3 -- make sure label at first */ &sensor_dev_attr_in3_label.dev_attr.attr, @@ -383,6 +494,8 @@ static struct attribute *ina3221_attrs[] = { &sensor_dev_attr_curr3_max.dev_attr.attr, &sensor_dev_attr_curr3_max_alarm.dev_attr.attr, &sensor_dev_attr_in6_input.dev_attr.attr, + &sensor_dev_attr_power3_input.dev_attr.attr, + &sensor_dev_attr_power3_crit.dev_attr.attr, NULL, }; From patchwork Wed Sep 26 06:42:45 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nicolin Chen X-Patchwork-Id: 10615327 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 9CEC813A4 for ; Wed, 26 Sep 2018 06:43:16 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 8A64C28684 for ; Wed, 26 Sep 2018 06:43:16 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 7C71B286EC; Wed, 26 Sep 2018 06:43:16 +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 D9E1228684 for ; Wed, 26 Sep 2018 06:43:15 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727270AbeIZMyc (ORCPT ); Wed, 26 Sep 2018 08:54:32 -0400 Received: from mail-pf1-f195.google.com ([209.85.210.195]:40026 "EHLO mail-pf1-f195.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726987AbeIZMy0 (ORCPT ); Wed, 26 Sep 2018 08:54:26 -0400 Received: by mail-pf1-f195.google.com with SMTP id s5-v6so5174341pfj.7; Tue, 25 Sep 2018 23:43:03 -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=42ZH26G1GulK3kR63BbBk0+Kh0/u+HjdaTLKuRf+Mnc=; b=AZGJROmt00K4siKgZFnEiEsI/F5jsOTDsUduXGODDe6LVWOIDUWC6ZPRP041lujzQS zzg3ids9XF3vU6z+lyQFvid9McEgvtb4/zqBcoLvtnRYUl3pn+Kqyp50c9OkdEi1RWo4 XHsrHxivP1GG4JxhHhpe2g7qZXK1szNGjEKVzN1KvXbqJNchOjVQrZ1gbK1XE5oDEgxY +dOtCTWATHb3sOrPvw4+Kjui+NzBPJJlHkOiY0phrbsp49OTLqwqGkbLyfh0RjZwY6xV BYR8o5TWLnPwZPkrAGAhBFLMZbk35K+XRWAgL0bowFXOaOzet0THrUR6tauSnGcbi6bc juUw== 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=42ZH26G1GulK3kR63BbBk0+Kh0/u+HjdaTLKuRf+Mnc=; b=pkFXwuaEGsNtGWcPO0sAFvWkdFQ1yfmscTRlGPsIcV2etZVTAqmqmlxXvlvS57q3Xn fYIojoBQV3lH4cy/nEwsh05WnmGiXNeDX8gdTEaF64p2OJ57eCMbQiVYj2Xau7cMD8B2 TGW3dKfUX4y6c+s5Fj2FjJPXR/w9I8J5BAwovC3POl2mu8OXSHZFF3XWA80gnIA2CjNX 0VqM6Wj+dYXrnBJjDtlkhenbIxPieabm56byqAWXtO3Wvuuwb0NMqjNuChAUg04pL7ij NLSmChOZZubaNUgOZO10QwAhpK0eQyWt9JGV2b5iGJOPUlFrCIJH9QuxOu79485rbhKo IIwQ== X-Gm-Message-State: ABuFfogcyTEeZ/1voDuMkrmc0JEpwxTe3VKEFOqawnxfUsMf02FNYwtN RaAECzr42xByQq0QrSGg9rk= X-Google-Smtp-Source: ACcGV60CerIXF2b+kpAxzK/JgVhny9LbrJsp8nL+CM69L24nCkUcpcI406Gi+gfSTsnn1Yq/CdOmOw== X-Received: by 2002:a62:cc0e:: with SMTP id a14-v6mr4621354pfg.131.1537944182457; Tue, 25 Sep 2018 23:43:02 -0700 (PDT) Received: from Asurada-Nvidia.nvidia.com (thunderhill.nvidia.com. [216.228.112.22]) by smtp.gmail.com with ESMTPSA id 71-v6sm5574861pfx.182.2018.09.25.23.43.01 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 25 Sep 2018 23:43:02 -0700 (PDT) From: Nicolin Chen To: jdelvare@suse.com, linux@roeck-us.net, corbet@lwn.net Cc: afd@ti.com, linux-hwmon@vger.kernel.org, linux-kernel@vger.kernel.org, linux-doc@vger.kernel.org Subject: [PATCH 2/2] hwmon: ina3221: Add enable sysfs nodes Date: Tue, 25 Sep 2018 23:42:45 -0700 Message-Id: <20180926064245.4091-3-nicoleotsuka@gmail.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20180926064245.4091-1-nicoleotsuka@gmail.com> References: <20180926064245.4091-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 inX_enable interface allows user space to enable or disable the corresponding channel. Meanwhile, according to hwmon ABI, a disabled channel/sensor should return -ENODATA as a read result. However, there're configurable nodes sharing the same __show() functions. So this change also adds to check if the attribute is read-only to make sure it's not reading a configuration but the sensor data. Signed-off-by: Nicolin Chen --- Documentation/hwmon/ina3221 | 1 + drivers/hwmon/ina3221.c | 85 +++++++++++++++++++++++++++++++++++++ 2 files changed, 86 insertions(+) diff --git a/Documentation/hwmon/ina3221 b/Documentation/hwmon/ina3221 index 6be64b553cd0..1aa32366d8aa 100644 --- a/Documentation/hwmon/ina3221 +++ b/Documentation/hwmon/ina3221 @@ -23,6 +23,7 @@ Sysfs entries in[123]_input Bus voltage(mV) channels in[123]_label Voltage channel labels +in[123]_enable Voltage channel enable controls curr[123]_input Current(mA) measurement channels shunt[123]_resistor Shunt resistance(uOhm) channels curr[123]_crit Critical alert current(mA) setting, activates the diff --git a/drivers/hwmon/ina3221.c b/drivers/hwmon/ina3221.c index 5890a2da3bfe..1be2d062a19e 100644 --- a/drivers/hwmon/ina3221.c +++ b/drivers/hwmon/ina3221.c @@ -78,6 +78,9 @@ enum ina3221_channels { }; static const unsigned int register_channel[] = { + [INA3221_BUS1] = INA3221_CHANNEL1, + [INA3221_BUS2] = INA3221_CHANNEL2, + [INA3221_BUS3] = INA3221_CHANNEL3, [INA3221_SHUNT1] = INA3221_CHANNEL1, [INA3221_SHUNT2] = INA3221_CHANNEL2, [INA3221_SHUNT3] = INA3221_CHANNEL3, @@ -113,6 +116,19 @@ struct ina3221_data { struct ina3221_input inputs[INA3221_NUM_CHANNELS]; }; +static inline bool ina3221_is_enable(struct ina3221_data *ina, int channel) +{ + unsigned int config; + int ret; + + /* Return false to reject further read */ + ret = regmap_read(ina->regmap, INA3221_CONFIG, &config); + if (ret) + return false; + + return (config & INA3221_CONFIG_CHx_EN(channel)) > 0; +} + static ssize_t ina3221_show_label(struct device *dev, struct device_attribute *attr, char *buf) { @@ -124,6 +140,45 @@ static ssize_t ina3221_show_label(struct device *dev, return snprintf(buf, PAGE_SIZE, "%s\n", input->label); } +static ssize_t ina3221_show_enable(struct device *dev, + struct device_attribute *attr, char *buf) +{ + struct sensor_device_attribute *sd_attr = to_sensor_dev_attr(attr); + struct ina3221_data *ina = dev_get_drvdata(dev); + unsigned int channel = sd_attr->index; + + return snprintf(buf, PAGE_SIZE, "%d\n", + ina3221_is_enable(ina, channel)); +} + +static ssize_t ina3221_set_enable(struct device *dev, + struct device_attribute *attr, + const char *buf, size_t count) +{ + struct sensor_device_attribute *sd_attr = to_sensor_dev_attr(attr); + struct ina3221_data *ina = dev_get_drvdata(dev); + unsigned int channel = sd_attr->index; + unsigned int mask = INA3221_CONFIG_CHx_EN(channel); + unsigned int config; + int val, ret; + + ret = kstrtoint(buf, 0, &val); + if (ret) + return ret; + + /* inX_enable only accepts 1 for enabling or 0 for disabling */ + if (val != 0 && val != 1) + return -EINVAL; + + config = val ? mask : 0; + + ret = regmap_update_bits(ina->regmap, INA3221_CONFIG, mask, config); + if (ret) + return ret; + + return count; +} + static int ina3221_read_value(struct ina3221_data *ina, unsigned int reg, int *val) { @@ -146,8 +201,13 @@ static ssize_t ina3221_show_bus_voltage(struct device *dev, struct sensor_device_attribute *sd_attr = to_sensor_dev_attr(attr); struct ina3221_data *ina = dev_get_drvdata(dev); unsigned int reg = sd_attr->index; + unsigned int channel = register_channel[reg]; int val, voltage_mv, ret; + /* No data for read-only attribute if channel is disabled */ + if (!attr->store && !ina3221_is_enable(ina, channel)) + return -ENODATA; + ret = ina3221_read_value(ina, reg, &val); if (ret) return ret; @@ -164,8 +224,13 @@ static ssize_t ina3221_show_shunt_voltage(struct device *dev, struct sensor_device_attribute *sd_attr = to_sensor_dev_attr(attr); struct ina3221_data *ina = dev_get_drvdata(dev); unsigned int reg = sd_attr->index; + unsigned int channel = register_channel[reg]; int val, voltage_uv, ret; + /* No data for read-only attribute if channel is disabled */ + if (!attr->store && !ina3221_is_enable(ina, channel)) + return -ENODATA; + ret = ina3221_read_value(ina, reg, &val); if (ret) return ret; @@ -201,8 +266,13 @@ static ssize_t ina3221_show_current(struct device *dev, struct sensor_device_attribute *sd_attr = to_sensor_dev_attr(attr); struct ina3221_data *ina = dev_get_drvdata(dev); unsigned int reg = sd_attr->index; + unsigned int channel = register_channel[reg]; int current_ma, ret; + /* No data for read-only attribute if channel is disabled */ + if (!attr->store && !ina3221_is_enable(ina, channel)) + return -ENODATA; + ret = __ina3221_show_current(ina, reg, ¤t_ma); if (ret) return ret; @@ -318,6 +388,10 @@ static ssize_t ina3221_show_power(struct device *dev, int val, current_ma, voltage_mv, ret; s64 power_uw; + /* No data for read-only attribute if channel is disabled */ + if (!attr->store && !ina3221_is_enable(ina, channel)) + return -ENODATA; + /* Read bus voltage */ ret = ina3221_read_value(ina, reg_bus, &val); if (ret) @@ -377,6 +451,14 @@ static SENSOR_DEVICE_ATTR(in2_label, 0444, static SENSOR_DEVICE_ATTR(in3_label, 0444, ina3221_show_label, NULL, INA3221_CHANNEL3); +/* voltage channel enable */ +static SENSOR_DEVICE_ATTR(in1_enable, 0644, + ina3221_show_enable, ina3221_set_enable, INA3221_CHANNEL1); +static SENSOR_DEVICE_ATTR(in2_enable, 0644, + ina3221_show_enable, ina3221_set_enable, INA3221_CHANNEL2); +static SENSOR_DEVICE_ATTR(in3_enable, 0644, + ina3221_show_enable, ina3221_set_enable, INA3221_CHANNEL3); + /* bus voltage */ static SENSOR_DEVICE_ATTR(in1_input, S_IRUGO, ina3221_show_bus_voltage, NULL, INA3221_BUS1); @@ -460,6 +542,7 @@ static SENSOR_DEVICE_ATTR(power3_crit, 0644, static struct attribute *ina3221_attrs[] = { /* channel 1 -- make sure label at first */ &sensor_dev_attr_in1_label.dev_attr.attr, + &sensor_dev_attr_in1_enable.dev_attr.attr, &sensor_dev_attr_in1_input.dev_attr.attr, &sensor_dev_attr_curr1_input.dev_attr.attr, &sensor_dev_attr_shunt1_resistor.dev_attr.attr, @@ -473,6 +556,7 @@ static struct attribute *ina3221_attrs[] = { /* channel 2 -- make sure label at first */ &sensor_dev_attr_in2_label.dev_attr.attr, + &sensor_dev_attr_in2_enable.dev_attr.attr, &sensor_dev_attr_in2_input.dev_attr.attr, &sensor_dev_attr_curr2_input.dev_attr.attr, &sensor_dev_attr_shunt2_resistor.dev_attr.attr, @@ -486,6 +570,7 @@ static struct attribute *ina3221_attrs[] = { /* channel 3 -- make sure label at first */ &sensor_dev_attr_in3_label.dev_attr.attr, + &sensor_dev_attr_in3_enable.dev_attr.attr, &sensor_dev_attr_in3_input.dev_attr.attr, &sensor_dev_attr_curr3_input.dev_attr.attr, &sensor_dev_attr_shunt3_resistor.dev_attr.attr,