From patchwork Thu Jan 7 10:34:14 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexandru Ardelean X-Patchwork-Id: 12003497 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-16.8 required=3.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id C3C8EC433E6 for ; Thu, 7 Jan 2021 10:32:03 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 86A8F23341 for ; Thu, 7 Jan 2021 10:32:03 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727327AbhAGKbw (ORCPT ); Thu, 7 Jan 2021 05:31:52 -0500 Received: from mx0a-00128a01.pphosted.com ([148.163.135.77]:55910 "EHLO mx0a-00128a01.pphosted.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726110AbhAGKbw (ORCPT ); Thu, 7 Jan 2021 05:31:52 -0500 Received: from pps.filterd (m0167089.ppops.net [127.0.0.1]) by mx0a-00128a01.pphosted.com (8.16.0.43/8.16.0.43) with SMTP id 107ATgZf007256; Thu, 7 Jan 2021 05:30:52 -0500 Received: from nwd2mta4.analog.com ([137.71.173.58]) by mx0a-00128a01.pphosted.com with ESMTP id 35wnkjhdta-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Thu, 07 Jan 2021 05:30:52 -0500 Received: from SCSQMBX10.ad.analog.com (SCSQMBX10.ad.analog.com [10.77.17.5]) by nwd2mta4.analog.com (8.14.7/8.14.7) with ESMTP id 107AUo2r023976 (version=TLSv1/SSLv3 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=FAIL); Thu, 7 Jan 2021 05:30:51 -0500 Received: from SCSQCASHYB6.ad.analog.com (10.77.17.132) by SCSQMBX10.ad.analog.com (10.77.17.5) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.721.2; Thu, 7 Jan 2021 02:30:49 -0800 Received: from SCSQMBX10.ad.analog.com (10.77.17.5) by SCSQCASHYB6.ad.analog.com (10.77.17.132) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.721.2; Thu, 7 Jan 2021 02:30:49 -0800 Received: from zeus.spd.analog.com (10.66.68.11) by scsqmbx10.ad.analog.com (10.77.17.5) with Microsoft SMTP Server id 15.2.721.2 via Frontend Transport; Thu, 7 Jan 2021 02:30:48 -0800 Received: from localhost.localdomain ([10.48.65.12]) by zeus.spd.analog.com (8.15.1/8.15.1) with ESMTP id 107AUhsi013109; Thu, 7 Jan 2021 05:30:46 -0500 From: Alexandru Ardelean To: , , CC: , , , , , Alexandru Ardelean Subject: [PATCH v3 1/4] hwmon: (ltc2945): wrap regmap into an ltc2945_state struct Date: Thu, 7 Jan 2021 12:34:14 +0200 Message-ID: <20210107103417.16010-2-alexandru.ardelean@analog.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20210107103417.16010-1-alexandru.ardelean@analog.com> References: <20210107103417.16010-1-alexandru.ardelean@analog.com> MIME-Version: 1.0 X-ADIRuleOP-NewSCL: Rule Triggered X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10434:6.0.343,18.0.737 definitions=2021-01-07_05:2021-01-07,2021-01-07 signatures=0 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 spamscore=0 impostorscore=0 suspectscore=0 clxscore=1015 phishscore=0 mlxscore=0 adultscore=0 mlxlogscore=999 priorityscore=1501 lowpriorityscore=0 bulkscore=0 malwarescore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2009150000 definitions=main-2101070063 Precedence: bulk List-ID: X-Mailing-List: linux-hwmon@vger.kernel.org The intent is to add pass the value of the sense resistor in the driver. This change wraps a 'struct ltc2945_state', and moves the regmap reference on that object. Then we can add the value of the sense resistor, or other information that would be useful for the driver. Signed-off-by: Alexandru Ardelean --- drivers/hwmon/ltc2945.c | 30 +++++++++++++++++++++++++----- 1 file changed, 25 insertions(+), 5 deletions(-) diff --git a/drivers/hwmon/ltc2945.c b/drivers/hwmon/ltc2945.c index ba9c868a8641..42a59170da78 100644 --- a/drivers/hwmon/ltc2945.c +++ b/drivers/hwmon/ltc2945.c @@ -58,6 +58,14 @@ #define CONTROL_MULT_SELECT (1 << 0) #define CONTROL_TEST_MODE (1 << 4) +/** + * struct ltc2945_state - driver instance specific data + * @regmap: regmap object to access device registers + */ +struct ltc2945_state { + struct regmap *regmap; +}; + static inline bool is_power_reg(u8 reg) { return reg < LTC2945_SENSE_H; @@ -66,7 +74,8 @@ static inline bool is_power_reg(u8 reg) /* Return the value from the given register in uW, mV, or mA */ static long long ltc2945_reg_to_val(struct device *dev, u8 reg) { - struct regmap *regmap = dev_get_drvdata(dev); + struct ltc2945_state *st = dev_get_drvdata(dev); + struct regmap *regmap = st->regmap; unsigned int control; u8 buf[3]; long long val; @@ -148,7 +157,8 @@ static long long ltc2945_reg_to_val(struct device *dev, u8 reg) static int ltc2945_val_to_reg(struct device *dev, u8 reg, unsigned long val) { - struct regmap *regmap = dev_get_drvdata(dev); + struct ltc2945_state *st = dev_get_drvdata(dev); + struct regmap *regmap = st->regmap; unsigned int control; int ret; @@ -234,7 +244,8 @@ static ssize_t ltc2945_value_store(struct device *dev, const char *buf, size_t count) { struct sensor_device_attribute *attr = to_sensor_dev_attr(da); - struct regmap *regmap = dev_get_drvdata(dev); + struct ltc2945_state *st = dev_get_drvdata(dev); + struct regmap *regmap = st->regmap; u8 reg = attr->index; unsigned long val; u8 regbuf[3]; @@ -269,7 +280,8 @@ static ssize_t ltc2945_history_store(struct device *dev, const char *buf, size_t count) { struct sensor_device_attribute *attr = to_sensor_dev_attr(da); - struct regmap *regmap = dev_get_drvdata(dev); + struct ltc2945_state *st = dev_get_drvdata(dev); + struct regmap *regmap = st->regmap; u8 reg = attr->index; int num_regs = is_power_reg(reg) ? 3 : 2; u8 buf_min[3] = { 0xff, 0xff, 0xff }; @@ -321,7 +333,8 @@ static ssize_t ltc2945_bool_show(struct device *dev, struct device_attribute *da, char *buf) { struct sensor_device_attribute *attr = to_sensor_dev_attr(da); - struct regmap *regmap = dev_get_drvdata(dev); + struct ltc2945_state *st = dev_get_drvdata(dev); + struct regmap *regmap = st->regmap; unsigned int fault; int ret; @@ -448,15 +461,22 @@ static const struct regmap_config ltc2945_regmap_config = { static int ltc2945_probe(struct i2c_client *client) { struct device *dev = &client->dev; + struct ltc2945_state *st; struct device *hwmon_dev; struct regmap *regmap; + st = devm_kzalloc(dev, sizeof(*st), GFP_KERNEL); + if (!st) + return -ENOMEM; + regmap = devm_regmap_init_i2c(client, <c2945_regmap_config); if (IS_ERR(regmap)) { dev_err(dev, "failed to allocate register map\n"); return PTR_ERR(regmap); } + st->regmap = regmap; + /* Clear faults */ regmap_write(regmap, LTC2945_FAULT, 0x00); From patchwork Thu Jan 7 10:34:15 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexandru Ardelean X-Patchwork-Id: 12003503 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-16.8 required=3.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 22F67C43217 for ; Thu, 7 Jan 2021 10:32:05 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id DA3D82336F for ; Thu, 7 Jan 2021 10:32:04 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727590AbhAGKbt (ORCPT ); Thu, 7 Jan 2021 05:31:49 -0500 Received: from mx0a-00128a01.pphosted.com ([148.163.135.77]:52108 "EHLO mx0a-00128a01.pphosted.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726110AbhAGKbs (ORCPT ); Thu, 7 Jan 2021 05:31:48 -0500 Received: from pps.filterd (m0167088.ppops.net [127.0.0.1]) by mx0a-00128a01.pphosted.com (8.16.0.43/8.16.0.43) with SMTP id 107AUhLq013617; Thu, 7 Jan 2021 05:30:52 -0500 Received: from nwd2mta4.analog.com ([137.71.173.58]) by mx0a-00128a01.pphosted.com with ESMTP id 35wsv6gwm0-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Thu, 07 Jan 2021 05:30:51 -0500 Received: from ASHBMBX9.ad.analog.com (ASHBMBX9.ad.analog.com [10.64.17.10]) by nwd2mta4.analog.com (8.14.7/8.14.7) with ESMTP id 107AUouP023977 (version=TLSv1/SSLv3 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128 verify=FAIL); Thu, 7 Jan 2021 05:30:50 -0500 Received: from ASHBMBX8.ad.analog.com (10.64.17.5) by ASHBMBX9.ad.analog.com (10.64.17.10) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.1779.2; Thu, 7 Jan 2021 05:30:49 -0500 Received: from zeus.spd.analog.com (10.66.68.11) by ashbmbx8.ad.analog.com (10.64.17.5) with Microsoft SMTP Server id 15.2.721.2 via Frontend Transport; Thu, 7 Jan 2021 05:30:49 -0500 Received: from localhost.localdomain ([10.48.65.12]) by zeus.spd.analog.com (8.15.1/8.15.1) with ESMTP id 107AUhsj013109; Thu, 7 Jan 2021 05:30:47 -0500 From: Alexandru Ardelean To: , , CC: , , , , , Alexandru Ardelean Subject: [PATCH v3 2/4] hwmon: (ltc2945): clamp values before converting Date: Thu, 7 Jan 2021 12:34:15 +0200 Message-ID: <20210107103417.16010-3-alexandru.ardelean@analog.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20210107103417.16010-1-alexandru.ardelean@analog.com> References: <20210107103417.16010-1-alexandru.ardelean@analog.com> MIME-Version: 1.0 X-ADIRuleOP-NewSCL: Rule Triggered X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10434:6.0.343,18.0.737 definitions=2021-01-07_05:2021-01-07,2021-01-07 signatures=0 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 clxscore=1015 phishscore=0 impostorscore=0 malwarescore=0 mlxlogscore=999 mlxscore=0 adultscore=0 suspectscore=0 bulkscore=0 spamscore=0 priorityscore=1501 lowpriorityscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2009150000 definitions=main-2101070063 Precedence: bulk List-ID: X-Mailing-List: linux-hwmon@vger.kernel.org We can compute the full-scale values for power, voltages and current, and apply them before conversion. This way we sanitize the input from userspace a bit. We can't however clamp the value for power, since that is represented in micro-Watts. The full-scale power that can be represented is around 10480 Watts, which in micro-Watts ends up being around 10 billion micro-Watts. Current and voltage is represented in millis, so these can be clamped. Signed-off-by: Alexandru Ardelean --- drivers/hwmon/ltc2945.c | 48 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/drivers/hwmon/ltc2945.c b/drivers/hwmon/ltc2945.c index 42a59170da78..41df2c8b7673 100644 --- a/drivers/hwmon/ltc2945.c +++ b/drivers/hwmon/ltc2945.c @@ -58,6 +58,15 @@ #define CONTROL_MULT_SELECT (1 << 0) #define CONTROL_TEST_MODE (1 << 4) +/* Full scale ranges (page 4 of the datasheet) */ + +#define LTC2945_VIN_FULL_SCALE_MV 102375 +#define LTC2945_ADIN_FULL_SCALE_MV 2048 + +/* Power and current computed assuming a 1mOhm sense resistor */ +#define LTC2945_POWER_FULL_SCALE_UW 10485759375ULL +#define LTC2945_SENSE_FULL_SCALE_MA 102375 + /** * struct ltc2945_state - driver instance specific data * @regmap: regmap object to access device registers @@ -154,6 +163,43 @@ static long long ltc2945_reg_to_val(struct device *dev, u8 reg) return val; } +static unsigned long ltc2945_val_clamp(u8 reg, unsigned long val) +{ + switch (reg) { + case LTC2945_POWER_H: + case LTC2945_MAX_POWER_H: + case LTC2945_MIN_POWER_H: + case LTC2945_MAX_POWER_THRES_H: + case LTC2945_MIN_POWER_THRES_H: + /* No sense in clamping now, LTC2945_POWER_FULL_SCALE_UW is larger than UINT32_MAX */ + return val; + case LTC2945_VIN_H: + case LTC2945_MAX_VIN_H: + case LTC2945_MIN_VIN_H: + case LTC2945_MAX_VIN_THRES_H: + case LTC2945_MIN_VIN_THRES_H: + return clamp_val(val, 0, LTC2945_VIN_FULL_SCALE_MV); + case LTC2945_ADIN_H: + case LTC2945_MAX_ADIN_H: + case LTC2945_MIN_ADIN_THRES_H: + case LTC2945_MAX_ADIN_THRES_H: + case LTC2945_MIN_ADIN_H: + return clamp_val(val, 0, LTC2945_ADIN_FULL_SCALE_MV); + case LTC2945_SENSE_H: + case LTC2945_MAX_SENSE_H: + case LTC2945_MIN_SENSE_H: + case LTC2945_MAX_SENSE_THRES_H: + case LTC2945_MIN_SENSE_THRES_H: + return clamp_val(val, 0, LTC2945_SENSE_FULL_SCALE_MA); + default: + /* + * This is unlikely to happen, and if it does, it should + * error out on the next call, we can't return negative here + */ + return 0; + } +} + static int ltc2945_val_to_reg(struct device *dev, u8 reg, unsigned long val) { @@ -257,6 +303,8 @@ static ssize_t ltc2945_value_store(struct device *dev, if (ret) return ret; + val = ltc2945_val_clamp(reg, val); + /* convert to register value, then clamp and write result */ regval = ltc2945_val_to_reg(dev, reg, val); if (is_power_reg(reg)) { From patchwork Thu Jan 7 10:34:16 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexandru Ardelean X-Patchwork-Id: 12003501 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-16.8 required=3.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED,USER_AGENT_GIT autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id D8654C43331 for ; Thu, 7 Jan 2021 10:32:04 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 9C1CC23343 for ; Thu, 7 Jan 2021 10:32:04 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727771AbhAGKb5 (ORCPT ); Thu, 7 Jan 2021 05:31:57 -0500 Received: from mx0a-00128a01.pphosted.com ([148.163.135.77]:57434 "EHLO mx0a-00128a01.pphosted.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727716AbhAGKbx (ORCPT ); Thu, 7 Jan 2021 05:31:53 -0500 Received: from pps.filterd (m0167089.ppops.net [127.0.0.1]) by mx0a-00128a01.pphosted.com (8.16.0.43/8.16.0.43) with SMTP id 107ATiCK007408; Thu, 7 Jan 2021 05:30:55 -0500 Received: from nwd2mta3.analog.com ([137.71.173.56]) by mx0a-00128a01.pphosted.com with ESMTP id 35wnkjhdte-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Thu, 07 Jan 2021 05:30:55 -0500 Received: from SCSQMBX11.ad.analog.com (SCSQMBX11.ad.analog.com [10.77.17.10]) by nwd2mta3.analog.com (8.14.7/8.14.7) with ESMTP id 107AUrdH041849 (version=TLSv1/SSLv3 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128 verify=FAIL); Thu, 7 Jan 2021 05:30:53 -0500 Received: from SCSQMBX10.ad.analog.com (10.77.17.5) by SCSQMBX11.ad.analog.com (10.77.17.10) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.1779.2; Thu, 7 Jan 2021 02:30:52 -0800 Received: from zeus.spd.analog.com (10.66.68.11) by scsqmbx10.ad.analog.com (10.77.17.5) with Microsoft SMTP Server id 15.2.721.2 via Frontend Transport; Thu, 7 Jan 2021 02:30:52 -0800 Received: from localhost.localdomain ([10.48.65.12]) by zeus.spd.analog.com (8.15.1/8.15.1) with ESMTP id 107AUhsk013109; Thu, 7 Jan 2021 05:30:49 -0500 From: Alexandru Ardelean To: , , CC: , , , , , Alexandru Ardelean Subject: [PATCH v3 3/4] hwmon: (ltc2945): add support for sense resistor Date: Thu, 7 Jan 2021 12:34:16 +0200 Message-ID: <20210107103417.16010-4-alexandru.ardelean@analog.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20210107103417.16010-1-alexandru.ardelean@analog.com> References: <20210107103417.16010-1-alexandru.ardelean@analog.com> MIME-Version: 1.0 X-ADIRuleOP-NewSCL: Rule Triggered X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10434:6.0.343,18.0.737 definitions=2021-01-07_05:2021-01-07,2021-01-07 signatures=0 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 spamscore=0 impostorscore=0 suspectscore=0 clxscore=1015 phishscore=0 mlxscore=0 adultscore=0 mlxlogscore=999 priorityscore=1501 lowpriorityscore=0 bulkscore=0 malwarescore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2009150000 definitions=main-2101070063 Precedence: bulk List-ID: X-Mailing-List: linux-hwmon@vger.kernel.org The sense resistor is a parameter of the board. It should be configured in the driver via a device-tree / ACPI property, so that the proper current measurements can be done in the driver. It shouldn't be necessary that userspace need to know about the value of the resistor. It makes things a bit harder to make the application code portable from one board to another. This change implements support for the sense resistor to be configured from DT/ACPI and used in current calculations. Also, the maximum power and current that can be represented by the driver are scaled with the value of the sense resistor. Signed-off-by: Alexandru Ardelean Reported-by: kernel test robot Reported-by: kernel test robot --- drivers/hwmon/ltc2945.c | 60 ++++++++++++++++++++++++++--------------- 1 file changed, 39 insertions(+), 21 deletions(-) diff --git a/drivers/hwmon/ltc2945.c b/drivers/hwmon/ltc2945.c index 41df2c8b7673..e60b15832b0e 100644 --- a/drivers/hwmon/ltc2945.c +++ b/drivers/hwmon/ltc2945.c @@ -70,9 +70,15 @@ /** * struct ltc2945_state - driver instance specific data * @regmap: regmap object to access device registers + * @max_power_uw: maximum power that can be represented based on sense resistor + * @max_current_ma: maximum current that can be represented based on sense resistor + * @r_sense_mohm: current sense resistor value */ struct ltc2945_state { struct regmap *regmap; + u32 max_power_uw; + u32 max_current_ma; + u32 r_sense_mohm; }; static inline bool is_power_reg(u8 reg) @@ -110,9 +116,8 @@ static long long ltc2945_reg_to_val(struct device *dev, u8 reg) case LTC2945_MAX_POWER_THRES_H: case LTC2945_MIN_POWER_THRES_H: /* - * Convert to uW by assuming current is measured with - * an 1mOhm sense resistor, similar to current - * measurements. + * Convert to uW by and scale it with the configured + * sense resistor, similar to current measurements. * Control register bit 0 selects if voltage at SENSE+/VDD * or voltage at ADIN is used to measure power. */ @@ -126,6 +131,7 @@ static long long ltc2945_reg_to_val(struct device *dev, u8 reg) /* 0.5 mV * 25 uV = 0.0125 uV resolution. */ val = (val * 25LL) >> 1; } + val /= st->r_sense_mohm; break; case LTC2945_VIN_H: case LTC2945_MAX_VIN_H: @@ -149,13 +155,11 @@ static long long ltc2945_reg_to_val(struct device *dev, u8 reg) case LTC2945_MAX_SENSE_THRES_H: case LTC2945_MIN_SENSE_THRES_H: /* - * 25 uV resolution. Convert to current as measured with - * an 1 mOhm sense resistor, in mA. If a different sense - * resistor is installed, calculate the actual current by - * dividing the reported current by the sense resistor value - * in mOhm. + * 25 uV resolution. Convert to current and scale it + * with the value of the sense resistor. */ val *= 25; + val /= st->r_sense_mohm; break; default: return -EINVAL; @@ -163,7 +167,8 @@ static long long ltc2945_reg_to_val(struct device *dev, u8 reg) return val; } -static unsigned long ltc2945_val_clamp(u8 reg, unsigned long val) +static unsigned long ltc2945_val_clamp(struct ltc2945_state *st, u8 reg, + unsigned long val) { switch (reg) { case LTC2945_POWER_H: @@ -171,8 +176,7 @@ static unsigned long ltc2945_val_clamp(u8 reg, unsigned long val) case LTC2945_MIN_POWER_H: case LTC2945_MAX_POWER_THRES_H: case LTC2945_MIN_POWER_THRES_H: - /* No sense in clamping now, LTC2945_POWER_FULL_SCALE_UW is larger than UINT32_MAX */ - return val; + return clamp_val(val, 0, st->max_power_uw); case LTC2945_VIN_H: case LTC2945_MAX_VIN_H: case LTC2945_MIN_VIN_H: @@ -190,7 +194,7 @@ static unsigned long ltc2945_val_clamp(u8 reg, unsigned long val) case LTC2945_MIN_SENSE_H: case LTC2945_MAX_SENSE_THRES_H: case LTC2945_MIN_SENSE_THRES_H: - return clamp_val(val, 0, LTC2945_SENSE_FULL_SCALE_MA); + return clamp_val(val, 0, st->max_current_ma); default: /* * This is unlikely to happen, and if it does, it should @@ -215,9 +219,8 @@ static int ltc2945_val_to_reg(struct device *dev, u8 reg, case LTC2945_MAX_POWER_THRES_H: case LTC2945_MIN_POWER_THRES_H: /* - * Convert to register value by assuming current is measured - * with an 1mOhm sense resistor, similar to current - * measurements. + * Convert to register value, scale it with the configured sense + * resistor value, similar to current measurements. * Control register bit 0 selects if voltage at SENSE+/VDD * or voltage at ADIN is used to measure power, which in turn * determines register calculations. @@ -236,6 +239,7 @@ static int ltc2945_val_to_reg(struct device *dev, u8 reg, */ val = DIV_ROUND_CLOSEST(val, 25) * 2; } + val *= st->r_sense_mohm; break; case LTC2945_VIN_H: case LTC2945_MAX_VIN_H: @@ -259,12 +263,10 @@ static int ltc2945_val_to_reg(struct device *dev, u8 reg, case LTC2945_MAX_SENSE_THRES_H: case LTC2945_MIN_SENSE_THRES_H: /* - * 25 uV resolution. Convert to current as measured with - * an 1 mOhm sense resistor, in mA. If a different sense - * resistor is installed, calculate the actual current by - * dividing the reported current by the sense resistor value - * in mOhm. + * 25 uV resolution. Convert to current and scale it + * with the value of the sense resistor, in mA. */ + val *= st->r_sense_mohm; val = DIV_ROUND_CLOSEST(val, 25); break; default: @@ -303,7 +305,7 @@ static ssize_t ltc2945_value_store(struct device *dev, if (ret) return ret; - val = ltc2945_val_clamp(reg, val); + val = ltc2945_val_clamp(st, reg, val); /* convert to register value, then clamp and write result */ regval = ltc2945_val_to_reg(dev, reg, val); @@ -512,6 +514,7 @@ static int ltc2945_probe(struct i2c_client *client) struct ltc2945_state *st; struct device *hwmon_dev; struct regmap *regmap; + u64 val64; st = devm_kzalloc(dev, sizeof(*st), GFP_KERNEL); if (!st) @@ -523,7 +526,22 @@ static int ltc2945_probe(struct i2c_client *client) return PTR_ERR(regmap); } + if (device_property_read_u32(dev, "shunt-resistor-micro-ohms", + &st->r_sense_mohm)) + st->r_sense_mohm = 1000; + + if (st->r_sense_mohm < 1000) { + dev_err(dev, "Value too small for sense resistor, minimum 1000\n"); + return -EINVAL; + } + st->r_sense_mohm /= 1000; + st->regmap = regmap; + val64 = LTC2945_POWER_FULL_SCALE_UW / st->r_sense_mohm; + /* clamp power to ULONG_MAX, since we represent it on 32 bits */ + st->max_power_uw = clamp_val(val64, 0, ULONG_MAX); + + st->max_current_ma = LTC2945_SENSE_FULL_SCALE_MA / st->r_sense_mohm; /* Clear faults */ regmap_write(regmap, LTC2945_FAULT, 0x00); From patchwork Thu Jan 7 10:34:17 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexandru Ardelean X-Patchwork-Id: 12003499 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-16.8 required=3.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED,USER_AGENT_GIT autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 74F1DC4332B for ; Thu, 7 Jan 2021 10:32:04 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 381622333F for ; Thu, 7 Jan 2021 10:32:04 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727730AbhAGKbx (ORCPT ); Thu, 7 Jan 2021 05:31:53 -0500 Received: from mx0a-00128a01.pphosted.com ([148.163.135.77]:57360 "EHLO mx0a-00128a01.pphosted.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727714AbhAGKbx (ORCPT ); Thu, 7 Jan 2021 05:31:53 -0500 Received: from pps.filterd (m0167088.ppops.net [127.0.0.1]) by mx0a-00128a01.pphosted.com (8.16.0.43/8.16.0.43) with SMTP id 107AUXEe013148; Thu, 7 Jan 2021 05:30:57 -0500 Received: from nwd2mta3.analog.com ([137.71.173.56]) by mx0a-00128a01.pphosted.com with ESMTP id 35wsv6gwmb-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Thu, 07 Jan 2021 05:30:57 -0500 Received: from SCSQMBX11.ad.analog.com (SCSQMBX11.ad.analog.com [10.77.17.10]) by nwd2mta3.analog.com (8.14.7/8.14.7) with ESMTP id 107AUtBu041856 (version=TLSv1/SSLv3 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128 verify=FAIL); Thu, 7 Jan 2021 05:30:55 -0500 Received: from SCSQCASHYB6.ad.analog.com (10.77.17.132) by SCSQMBX11.ad.analog.com (10.77.17.10) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.1779.2; Thu, 7 Jan 2021 02:30:54 -0800 Received: from SCSQMBX10.ad.analog.com (10.77.17.5) by SCSQCASHYB6.ad.analog.com (10.77.17.132) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.721.2; Thu, 7 Jan 2021 02:30:54 -0800 Received: from zeus.spd.analog.com (10.66.68.11) by scsqmbx10.ad.analog.com (10.77.17.5) with Microsoft SMTP Server id 15.2.721.2 via Frontend Transport; Thu, 7 Jan 2021 02:30:53 -0800 Received: from localhost.localdomain ([10.48.65.12]) by zeus.spd.analog.com (8.15.1/8.15.1) with ESMTP id 107AUhsl013109; Thu, 7 Jan 2021 05:30:51 -0500 From: Alexandru Ardelean To: , , CC: , , , , , Alexandru Ardelean Subject: [PATCH v3 4/4] dt-bindings: hwmon: ltc2945: add device tree doc for ltc2945 Date: Thu, 7 Jan 2021 12:34:17 +0200 Message-ID: <20210107103417.16010-5-alexandru.ardelean@analog.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20210107103417.16010-1-alexandru.ardelean@analog.com> References: <20210107103417.16010-1-alexandru.ardelean@analog.com> MIME-Version: 1.0 X-ADIRuleOP-NewSCL: Rule Triggered X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10434:6.0.343,18.0.737 definitions=2021-01-07_05:2021-01-07,2021-01-07 signatures=0 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 clxscore=1015 phishscore=0 impostorscore=0 malwarescore=0 mlxlogscore=999 mlxscore=0 adultscore=0 suspectscore=0 bulkscore=0 spamscore=0 priorityscore=1501 lowpriorityscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2009150000 definitions=main-2101070063 Precedence: bulk List-ID: X-Mailing-List: linux-hwmon@vger.kernel.org This change adds a device-tree binding documentation for the Linear Technology (now Analog Devices) LTC2945 Wide Range I2C Power Monitor. Reviewed-by: Rob Herring Signed-off-by: Alexandru Ardelean --- .../bindings/hwmon/adi,ltc2945.yaml | 49 +++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 Documentation/devicetree/bindings/hwmon/adi,ltc2945.yaml diff --git a/Documentation/devicetree/bindings/hwmon/adi,ltc2945.yaml b/Documentation/devicetree/bindings/hwmon/adi,ltc2945.yaml new file mode 100644 index 000000000000..e49d7da09f74 --- /dev/null +++ b/Documentation/devicetree/bindings/hwmon/adi,ltc2945.yaml @@ -0,0 +1,49 @@ +# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) +%YAML 1.2 +--- +$id: http://devicetree.org/schemas/hwmon/adi,ltc2945.yaml# +$schema: http://devicetree.org/meta-schemas/core.yaml# + +title: Linear Technology LTC2945 Wide Range I2C Power Monitor + +maintainers: + - Guenter Roeck + +description: | + The LTC2945 is a rail-to-rail system monitor that measures current, voltage, + and power consumption. + +properties: + compatible: + enum: + - adi,ltc2945 + + reg: + maxItems: 1 + + shunt-resistor-micro-ohms: + description: + The value of curent sense resistor in microohms. If not provided, + a default value of 1000 microohms is used. + default: 1000 + +required: + - compatible + - reg + +additionalProperties: false + +examples: + - | + i2c { + #address-cells = <1>; + #size-cells = <0>; + + ltc2945_i2c: ltc2945@6f { + compatible = "adi,ltc2945"; + reg = <0x6f>; + + shunt-resistor-micro-ohms = <20000>; + }; + }; +...