From patchwork Mon Jan 9 23:35:30 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jon Cormier X-Patchwork-Id: 640740 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id C1C53C54EBD for ; Mon, 9 Jan 2023 23:36:02 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237729AbjAIXgB (ORCPT ); Mon, 9 Jan 2023 18:36:01 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58376 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237682AbjAIXf6 (ORCPT ); Mon, 9 Jan 2023 18:35:58 -0500 Received: from mail-qv1-xf2b.google.com (mail-qv1-xf2b.google.com [IPv6:2607:f8b0:4864:20::f2b]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 39CD26448 for ; Mon, 9 Jan 2023 15:35:57 -0800 (PST) Received: by mail-qv1-xf2b.google.com with SMTP id d13so7437093qvj.8 for ; Mon, 09 Jan 2023 15:35:57 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=criticallink.com; s=google; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=gWfOKCoKa6fVAUu6wQ1ieIdGSmrl8W8A2HgAxf0hwpE=; b=bvtIZBszxda3SApy+Vt245BDingr6VZTIE0uCE8ugMrSsv+pvqEUruYgswQfXSdXNC VAmuqsJCdrrXukhir+jfqy8l19M5PrfFk69mRqg9raoPIPug6x1o6cRVtZeAcf/lOLoW 7Cgg+TicuwJrkCt5Sqy5P5530IM+waDzfGZ1yMxP1Bhq+TjdXyNkEBDiPUXqqJtq5za/ 5ax6j0+xghsb2p/NVTdAbJPQrBnzzSRRzhpfdWhVDR20+0j0LFf7xuI7fjcJsyYZBgAd stdkfOLeBElQnrxjBCk5Qbkot5Uojaw2N8f1rxRdMveNL9PTyrPQK8hy1aaRoLP9lGgp EuWw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=gWfOKCoKa6fVAUu6wQ1ieIdGSmrl8W8A2HgAxf0hwpE=; b=ggzd3x9Z8G9d9c6rcGj5FPB8A5l+c7lTK78vIpdVLjNBz5raLZRs7Bdb3ZiiF4+D/+ uljSmfJrIzcPPWXszfjhHIbdPAKrVbKaGwuDos7DcS8OgVCrNZf3ifhRGEavFfRbjyWE XmdgwwgYySHjfUdugqrUvlEnN3SbW4yMi13AsMdVfXxsIKs8be3VWGOogUo6lgWLCOYe t5z3dBt5ViuPfLMRkQaTnDFPxkEf+CA9fRXAYqXd4iR0JJgHEeuVumNIkYY2BszzktiE J+W4TFTe1FneZwe8wDloORAEJwKq3mlVpq3jAuMAjVBNKl26DDs0PZ66ksttxZdS7WJe rLrA== X-Gm-Message-State: AFqh2krVCwvfg3Ihja6L4eM6+bweyl6yLBjBfQqjYj/iUfcwZipCHBXk xq1ZflBgPPZlHceVq5VIT6lwMg== X-Google-Smtp-Source: AMrXdXs/rF2/7d4tbX4eiwWCEqNoi1JKKTU7hd5nGyVo6/fHYoOLbHXFadfBW4oZivtIGTPI+Nn6wQ== X-Received: by 2002:a05:6214:c6c:b0:50e:48cc:a95a with SMTP id t12-20020a0562140c6c00b0050e48cca95amr123111751qvj.43.1673307356280; Mon, 09 Jan 2023 15:35:56 -0800 (PST) Received: from jcormier-MS-7A93.syr.criticallink.com (static-72-90-70-109.syrcny.fios.verizon.net. [72.90.70.109]) by smtp.gmail.com with ESMTPSA id t5-20020a05620a450500b006fa8299b4d5sm6294846qkp.100.2023.01.09.15.35.55 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 09 Jan 2023 15:35:55 -0800 (PST) From: Jonathan Cormier To: linux-hwmon@vger.kernel.org Cc: Jonathan Cormier , Jean Delvare , Guenter Roeck , linux-kernel@vger.kernel.org, devicetree@vger.kernel.org, Rob Herring , Krzysztof Kozlowski , Bob Duke , John Pruitt Subject: [PATCH v3 1/5] dt-bindings: hwmon: adi,ltc2945: Add binding Date: Mon, 9 Jan 2023 18:35:30 -0500 Message-Id: <20230109233534.1932370-2-jcormier@criticallink.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230109233534.1932370-1-jcormier@criticallink.com> References: <20221214220727.1350784-3-jcormier@criticallink.com> <20230109233534.1932370-1-jcormier@criticallink.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: devicetree@vger.kernel.org Create initial binding for the LTC2945 I2C power monitor. Add shunt-resistor-micro-ohms parameter Signed-off-by: Jonathan Cormier --- .../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..5cb66e97e816 --- /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: Analog Devices LTC2945 wide range i2c power monitor + +maintainers: + - Guenter Roeck + +description: | + Analog Devices LTC2945 wide range i2c power monitor over I2C. + + https://www.analog.com/media/en/technical-documentation/data-sheets/LTC2945.pdf + +properties: + compatible: + enum: + - adi,ltc2945 + + reg: + maxItems: 1 + + shunt-resistor-micro-ohms: + description: + Shunt resistor value in micro-Ohms + default: 1000 + +required: + - compatible + - reg + +additionalProperties: false + +examples: + - | + i2c { + #address-cells = <1>; + #size-cells = <0>; + + power-monitor@6e { + compatible = "adi,ltc2945"; + reg = <0x6e>; + /* 10 milli-Ohm shunt resistor */ + shunt-resistor-micro-ohms = <10000>; + }; + }; +... From patchwork Wed Dec 14 22:07:27 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jon Cormier X-Patchwork-Id: 633986 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id AC96DC4332F for ; Wed, 14 Dec 2022 22:07:54 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229803AbiLNWHx (ORCPT ); Wed, 14 Dec 2022 17:07:53 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46992 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229737AbiLNWHu (ORCPT ); Wed, 14 Dec 2022 17:07:50 -0500 Received: from mail-qt1-x833.google.com (mail-qt1-x833.google.com [IPv6:2607:f8b0:4864:20::833]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9ED11442F5 for ; Wed, 14 Dec 2022 14:07:43 -0800 (PST) Received: by mail-qt1-x833.google.com with SMTP id s9so3703269qtx.6 for ; Wed, 14 Dec 2022 14:07:43 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=criticallink.com; s=google; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=KkruMqduxIbjtx8gXwhOn81yywW3pdpreSNZQFqbSOk=; b=TcJVe3cOaqvIGQaH6BmLwuzQ18Pqn6CwMr9RJM6kFs2gxALvRVq7dTcBOngYh5vES7 iDZJzdeftZnWyRsIFKX06imAc3Vc0CBCvOHLR83C4DV/LFwMz1mRf+XZew9nrCcCtBu3 z/Cj09zA8ZnL8sOrLiKhUg1v9NlhoT5KXtdnCjASLj1UGkXEl72qQPqQ4L9SK1FzC8mv jec6AxiFPSofa9b2+mRw6zE8MG7roXNobJxG6JD+fdAJV++GHdoKzQ4jGzE39SEvU2rF NxAXm5kqD4o3MSvy2YNIzuaEGQHgLgK6iShzWiHU3ZItTY/LBo20HAZzfC2pHk1yw5Fw zhFQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=KkruMqduxIbjtx8gXwhOn81yywW3pdpreSNZQFqbSOk=; b=bolGFqdp8HEFkTuy7dlfrIqsSK0HSOAlxoTu1pbUgYkk8WVB80gYIoMt6z+4GRMqMC G3/D2unvxziPtlhFdoIG/HtPJaz3lAny6XJujU+0J6aMHFBTfdaM3eibWYxYbkeEDZig LiyZOKANy29OVaeU76qy0K4a03myq+8iUV/IFdsugJFVkTBkoaqsuToCIoQ1A7MqUQwU zeBxLn6M2MMmDUkkx0UFhpRUc7awSz1dbSmzBSSDbYIBsZlF6JPYjuodc3I6e92ID6IX Nf+r5cYdcm6oDSKE0shUbywttU/naArpqyxgDBYZvw2fvWTMuB5apN1SvUj/FsUif2pd CGnA== X-Gm-Message-State: ANoB5pnCq+pkvIbmGtF+PfDXdB96V8w2xhwiOn/CuiKrKm1pPMh8fDEi T3qhUboasvCskSFhY+rtAhq3xw== X-Google-Smtp-Source: AA0mqf61hmftnE2cie9po67xol3T56V060QxdLlDKUi4YXYmHzfqUmHNiaujOiiymdbHijyRbv4FEw== X-Received: by 2002:ac8:689a:0:b0:3a7:ec54:cfa2 with SMTP id m26-20020ac8689a000000b003a7ec54cfa2mr38298744qtq.56.1671055662630; Wed, 14 Dec 2022 14:07:42 -0800 (PST) Received: from jcormier-MS-7A93.syr.criticallink.com (static-72-90-70-109.syrcny.fios.verizon.net. [72.90.70.109]) by smtp.gmail.com with ESMTPSA id l24-20020ae9f018000000b006fc2b672950sm10623939qkg.37.2022.12.14.14.07.41 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 14 Dec 2022 14:07:42 -0800 (PST) From: "Cormier, Jonathan" To: linux-hwmon@vger.kernel.org Cc: John Pruitt , Jean Delvare , Guenter Roeck , linux-kernel@vger.kernel.org, devicetree@vger.kernel.org, Rob Herring , Krzysztof Kozlowski , Bob Duke , "Cormier, Jonathan" Subject: [PATCH 2/2] hwmon: ltc2945: Allow setting shunt resistor Date: Wed, 14 Dec 2022 17:07:27 -0500 Message-Id: <20221214220727.1350784-3-jcormier@criticallink.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20221214220727.1350784-1-jcormier@criticallink.com> References: <20221214220727.1350784-1-jcormier@criticallink.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: devicetree@vger.kernel.org From: John Pruitt Added the ability to specify the value of the shunt resistor in the device tree instead of assuming it is 1 milliOhm. The value in the device tree has the name shunt-resistor-micro-ohms and the default value is 1000 micro-ohms in order to preserve the current behavior. Signed-off-by: "Cormier, Jonathan" Signed-off-by: John Pruitt --- drivers/hwmon/ltc2945.c | 86 +++++++++++++++++++++++++++++++---------- 1 file changed, 65 insertions(+), 21 deletions(-) diff --git a/drivers/hwmon/ltc2945.c b/drivers/hwmon/ltc2945.c index 9adebb59f604..4e8b92e53133 100644 --- a/drivers/hwmon/ltc2945.c +++ b/drivers/hwmon/ltc2945.c @@ -58,6 +58,11 @@ #define CONTROL_MULT_SELECT (1 << 0) #define CONTROL_TEST_MODE (1 << 4) +struct ltc2945_data { + struct regmap *regmap; + u32 shunt_resistor; // number of microohms +}; + static inline bool is_power_reg(u8 reg) { return reg < LTC2945_SENSE_H; @@ -66,7 +71,9 @@ 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_data *data = dev_get_drvdata(dev); + struct regmap *regmap = data->regmap; + u32 shunt_resistor = data->shunt_resistor; unsigned int control; u8 buf[3]; long long val; @@ -93,8 +100,11 @@ static long long ltc2945_reg_to_val(struct device *dev, u8 reg) case LTC2945_MIN_POWER_THRES_H: /* * Convert to uW by assuming current is measured with - * an 1mOhm sense resistor, similar to current - * measurements. + * a 1000 microOhm sense resistor (aka shunt resistor) + * or what is specified in DT, similar to current + * measurements. The shunt_resistor value is in + * microOhms. + * * Control register bit 0 selects if voltage at SENSE+/VDD * or voltage at ADIN is used to measure power. */ @@ -108,6 +118,8 @@ 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 *= 1000; + val = DIV_ROUND_CLOSEST_ULL(val, shunt_resistor); break; case LTC2945_VIN_H: case LTC2945_MAX_VIN_H: @@ -132,12 +144,15 @@ static long long ltc2945_reg_to_val(struct device *dev, u8 reg) 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. + * an 1000 microOhm sense (or shunt) resistor, in mA. + * If a different shunt resistor is installed, calculate + * the actual current by dividing the reported current + * by the shunt resistor value in microOhms. The actual + * The actual shunt resistor value can be specified + * in the device tree. */ - val *= 25; + val *= 25 * 1000; + val = DIV_ROUND_CLOSEST_ULL(val, shunt_resistor); break; default: return -EINVAL; @@ -148,7 +163,9 @@ 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_data *data = dev_get_drvdata(dev); + struct regmap *regmap = data->regmap; + u32 shunt_resistor = data->shunt_resistor; unsigned int control; int ret; @@ -160,8 +177,10 @@ static int ltc2945_val_to_reg(struct device *dev, u8 reg, case LTC2945_MIN_POWER_THRES_H: /* * Convert to register value by assuming current is measured - * with an 1mOhm sense resistor, similar to current + * with a 1000 microOhm sense resistor, (aka shunt resistor) + * or what is specified in DT, 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. @@ -171,14 +190,16 @@ static int ltc2945_val_to_reg(struct device *dev, u8 reg, return ret; if (control & CONTROL_MULT_SELECT) { /* 25 mV * 25 uV = 0.625 uV resolution. */ - val = DIV_ROUND_CLOSEST(val, 625); + val *= shunt_resistor; + val = DIV_ROUND_CLOSEST(val, 625 * 1000); } else { /* * 0.5 mV * 25 uV = 0.0125 uV resolution. * Divide first to avoid overflow; * accept loss of accuracy. */ - val = DIV_ROUND_CLOSEST(val, 25) * 2; + val *= shunt_resistor; + val = DIV_ROUND_CLOSEST(val, 25 * 1000) * 2; } break; case LTC2945_VIN_H: @@ -204,12 +225,14 @@ static int ltc2945_val_to_reg(struct device *dev, u8 reg, 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. + * a 1000 microOhm sense (or shunt) resistor, in mA. If + * a different shunt resistor is installed, calculate + * the actual current by dividing the reported current + * by the sense resistor value in microOhms. The actual + * shunt resistor value can be specified in the device tree. */ - val = DIV_ROUND_CLOSEST(val, 25); + val *= shunt_resistor; + val = DIV_ROUND_CLOSEST(val, 25 * 1000); break; default: return -EINVAL; @@ -234,7 +257,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_data *data = dev_get_drvdata(dev); + struct regmap *regmap = data->regmap; u8 reg = attr->index; unsigned long val; u8 regbuf[3]; @@ -269,7 +293,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_data *data = dev_get_drvdata(dev); + struct regmap *regmap = data->regmap; u8 reg = attr->index; int num_regs = is_power_reg(reg) ? 3 : 2; u8 buf_min[3] = { 0xff, 0xff, 0xff }; @@ -321,7 +346,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_data *data = dev_get_drvdata(dev); + struct regmap *regmap = data->regmap; unsigned int fault; int ret; @@ -450,6 +476,14 @@ static int ltc2945_probe(struct i2c_client *client) struct device *dev = &client->dev; struct device *hwmon_dev; struct regmap *regmap; + u32 shunt_resistor; + + struct ltc2945_data *data; + + data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL); + if (!data) + return -ENOMEM; + dev_set_drvdata(dev, data); regmap = devm_regmap_init_i2c(client, <c2945_regmap_config); if (IS_ERR(regmap)) { @@ -457,11 +491,21 @@ static int ltc2945_probe(struct i2c_client *client) return PTR_ERR(regmap); } + data->regmap = regmap; + if (of_property_read_u32(client->dev.of_node, + "shunt-resistor-micro-ohms", &shunt_resistor)) + shunt_resistor = 1000; /* 1000 micro-Ohm if not set via DT */ + + if (shunt_resistor == 0) + return -EINVAL; + + data->shunt_resistor = shunt_resistor; + /* Clear faults */ regmap_write(regmap, LTC2945_FAULT, 0x00); hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name, - regmap, + data, ltc2945_groups); return PTR_ERR_OR_ZERO(hwmon_dev); } From patchwork Mon Jan 9 23:35:32 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jon Cormier X-Patchwork-Id: 640739 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 010EFC54EBD for ; Mon, 9 Jan 2023 23:36:06 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233794AbjAIXgF (ORCPT ); Mon, 9 Jan 2023 18:36:05 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58490 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237835AbjAIXgC (ORCPT ); Mon, 9 Jan 2023 18:36:02 -0500 Received: from mail-qt1-x836.google.com (mail-qt1-x836.google.com [IPv6:2607:f8b0:4864:20::836]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 76B66E083 for ; Mon, 9 Jan 2023 15:36:00 -0800 (PST) Received: by mail-qt1-x836.google.com with SMTP id h21so9433578qta.12 for ; Mon, 09 Jan 2023 15:36:00 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=criticallink.com; s=google; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=Omc6RJb75WehF9fTbabhEnE5DcQvC42CI90e1s9PLrk=; b=siN7GujK+FUeCr1O9CoGS68J8NCUuh891jd428rzwVirnNPB7oC2DlSjmTSm8qIabm 4Kr2rS3D0QyFDcK+DybptVP8qF4AHpoXkE9+xX6TGlOz2M3kfVOJIYjkiWOycG1WhD7d 3++yAlvgrHbibGcQWIOQex/6Avqplc+Gi3n9Z88jNWozj45nCH9Wk0hIxyknVDmJu8WW Qg1oCYk/7P/Z6uo5vbhiiZ0XV0L4pIguflN4qPbqMjo+/kmDFOemsJVQSbnlnXgyk/Rb eIY26LrBkunXiEPKVeky1t+vm87yTROF5QClgnYHZOtGnzXkgkxfXblMnAu/7KCEJQWO JNNg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=Omc6RJb75WehF9fTbabhEnE5DcQvC42CI90e1s9PLrk=; b=DLMLDsuFSwqfZIk+p5CDDhLySWK8oIa8a7nrlnV1O951eVQhKnhojSM9IMN3tEvHUm lJiKaW9zhIgjdKF+0Uz8qPKMAX3d5zeXcNBMkSA9e/5x7bp/gtya4CU4CQ+KBpHTJ57l o1V4wfVJRN2gISGu/reRiPbQ580np0UbZIQn1gnEPH7CHPnoYkPk0lIgjFiFXu03npv/ lqudY3wDAA4Vn2uGsZCcnYDAe5dVVEUMt894hWQabaHA+BdQf07QEiXyw++CajGIea75 RzUbQvpo/+/3nklX771XA4zzZGOHtUL8d06IBvXYUVyswKcDNxVQPZROUKz1fJolOPh5 mqPQ== X-Gm-Message-State: AFqh2krsqfwUfbvJf8kqOs/4ALgwls7R47pieNuUEtL2kaDRKtXLlb2d ImdTGEYeiJEZ10fdVsL4WIJqNQ== X-Google-Smtp-Source: AMrXdXs7LyHcYWze0fIdxaO9udBB/U/ezdUsN+AtYBx515eKR7aZSpGNVVO2mex3UDJQ/WXNOeiouw== X-Received: by 2002:ac8:7551:0:b0:3a7:f46b:7a82 with SMTP id b17-20020ac87551000000b003a7f46b7a82mr85112283qtr.21.1673307359514; Mon, 09 Jan 2023 15:35:59 -0800 (PST) Received: from jcormier-MS-7A93.syr.criticallink.com (static-72-90-70-109.syrcny.fios.verizon.net. [72.90.70.109]) by smtp.gmail.com with ESMTPSA id t5-20020a05620a450500b006fa8299b4d5sm6294846qkp.100.2023.01.09.15.35.58 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 09 Jan 2023 15:35:59 -0800 (PST) From: Jonathan Cormier To: linux-hwmon@vger.kernel.org Cc: Jonathan Cormier , Jean Delvare , Guenter Roeck , linux-kernel@vger.kernel.org, devicetree@vger.kernel.org, Rob Herring , Krzysztof Kozlowski , Bob Duke , John Pruitt Subject: [PATCH v3 3/5] hwmon: ltc2945: Handle error case in ltc2945_value_store Date: Mon, 9 Jan 2023 18:35:32 -0500 Message-Id: <20230109233534.1932370-4-jcormier@criticallink.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230109233534.1932370-1-jcormier@criticallink.com> References: <20221214220727.1350784-3-jcormier@criticallink.com> <20230109233534.1932370-1-jcormier@criticallink.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: devicetree@vger.kernel.org ltc2945_val_to_reg errors were not being handled which would have resulted in register being set to 0 (clamped) instead of being left alone. Change reg_to_val and val_to_reg to return values via parameters to make it more obvious when an error case isn't handled. Also to allow the regval type to be the correct sign in prep for next commits. Fixes: 6700ce035f83 ("hwmon: Driver for Linear Technologies LTC2945") Signed-off-by: Jonathan Cormier --- drivers/hwmon/ltc2945.c | 30 ++++++++++++++++++------------ 1 file changed, 18 insertions(+), 12 deletions(-) diff --git a/drivers/hwmon/ltc2945.c b/drivers/hwmon/ltc2945.c index 9af3e3821152..c66acf8d2124 100644 --- a/drivers/hwmon/ltc2945.c +++ b/drivers/hwmon/ltc2945.c @@ -70,12 +70,12 @@ 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) +static int ltc2945_reg_to_val(struct device *dev, u8 reg, u64 *regval) { struct regmap *regmap = dev_get_drvdata(dev); unsigned int control; u8 buf[3]; - long long val; + u64 val; int ret; ret = regmap_bulk_read(regmap, reg, buf, @@ -148,11 +148,12 @@ static long long ltc2945_reg_to_val(struct device *dev, u8 reg) default: return -EINVAL; } - return val; + *regval = val; + return 0; } static int ltc2945_val_to_reg(struct device *dev, u8 reg, - unsigned long val) + unsigned long val, unsigned long *regval) { struct regmap *regmap = dev_get_drvdata(dev); unsigned int control; @@ -220,19 +221,21 @@ static int ltc2945_val_to_reg(struct device *dev, u8 reg, default: return -EINVAL; } - return val; + *regval = val; + return 0; } static ssize_t ltc2945_value_show(struct device *dev, struct device_attribute *da, char *buf) { struct sensor_device_attribute *attr = to_sensor_dev_attr(da); - long long value; + int ret; + u64 value; - value = ltc2945_reg_to_val(dev, attr->index); - if (value < 0) - return value; - return sysfs_emit(buf, "%lld\n", value); + ret = ltc2945_reg_to_val(dev, attr->index, &value); + if (ret < 0) + return ret; + return sysfs_emit(buf, "%llu\n", value); } static ssize_t ltc2945_value_store(struct device *dev, @@ -245,7 +248,7 @@ static ssize_t ltc2945_value_store(struct device *dev, unsigned long val; u8 regbuf[3]; int num_regs; - int regval; + unsigned long regval; int ret; ret = kstrtoul(buf, 10, &val); @@ -253,7 +256,10 @@ static ssize_t ltc2945_value_store(struct device *dev, return ret; /* convert to register value, then clamp and write result */ - regval = ltc2945_val_to_reg(dev, reg, val); + ret = ltc2945_val_to_reg(dev, reg, val, ®val); + if (ret < 0) + return ret; + if (is_power_reg(reg)) { regval = clamp_val(regval, 0, 0xffffff); regbuf[0] = regval >> 16; From patchwork Mon Jan 9 23:35:34 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jon Cormier X-Patchwork-Id: 640738 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 74B80C54EBD for ; Mon, 9 Jan 2023 23:36:39 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237739AbjAIXge (ORCPT ); Mon, 9 Jan 2023 18:36:34 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58670 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237856AbjAIXgJ (ORCPT ); Mon, 9 Jan 2023 18:36:09 -0500 Received: from mail-qt1-x832.google.com (mail-qt1-x832.google.com [IPv6:2607:f8b0:4864:20::832]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 798AB13D03 for ; Mon, 9 Jan 2023 15:36:03 -0800 (PST) Received: by mail-qt1-x832.google.com with SMTP id v14so9561617qtq.3 for ; Mon, 09 Jan 2023 15:36:03 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=criticallink.com; s=google; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=tHdylmQcW9wYYsCa9wwYr0VU2Cu3mjkXydVxqDNSd7s=; b=IgpF0zOJNGRyYHPgr14oe84dRF4bZ4IkqF/xVJTwejgGZZo5CdElnmDII9psWkdYdV VQgAkixF2ZeGL6DjydEyuZtNHfBmLn5rpdRFaqqVL6z6PTXvZGBtF2fg3LEBWOnKmrnD GRdkAhof4yATzA6aIyB0m9QnqDPh8pJZnSPX5VoFJfhoAK6iXoQFA/3N8coGLwXEfWIt H1fpOo3VaGy9D6L276k0b/V0maEo+T8b3eyXLn7gmkjhPLokinE0gtFt89rvOcRu/IiB JOulRj1Gu0ycfFA24YeJLNK2FoAMGqHZlXEZ5ywgdniFUq3JKfUlvxPa/A1U2rYYCjDJ SIMA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=tHdylmQcW9wYYsCa9wwYr0VU2Cu3mjkXydVxqDNSd7s=; b=0noqk5uzlyNOjwNzzbXsgyp9IOZ+HJWMJTTDbANaAhiglffLx6LW3Jje/i1ncrmd+Q lhM4fZ8ap3sndjcPbzW6VvCLk7uCaQSRxJr53LMr8GbNoeM/PxNiHmomt7FEKwMDcefX 4ptqObq87nq5j3UmYuUqrG6Z8+4j2I8L5Lt5NLEDntcG0JJpfICWpKWtTPt+PyGciUq3 WIexLkB6qQNuoPawqVTX8JGbRQ58VBQuQNoMAkZqLmYAcFR7RaKa1SGsaCXlixs4GDiT jXwSjL2WJK6HK3ORu8HBxjfCD0mXLMBSsAVFXzl2X/+PGu90Lr+aNlpC3bbS7kAmikMp zAbA== X-Gm-Message-State: AFqh2kog1iy+3a9/F3o4hYN3yKJ+cyNLUGiEzrFiNx5QNL4I/nptBAtq 9VtuHvoN1VPhHceQRu5tdcIRHQ== X-Google-Smtp-Source: AMrXdXulseYYzs7cQNeCTGbJ59lnFcneJ7Iqd1kaKyqxoO1OhcE8XyTsdvklFinEON7uDw2wkbYhkQ== X-Received: by 2002:a05:622a:1646:b0:3a7:f6cf:a597 with SMTP id y6-20020a05622a164600b003a7f6cfa597mr120230588qtj.33.1673307362569; Mon, 09 Jan 2023 15:36:02 -0800 (PST) Received: from jcormier-MS-7A93.syr.criticallink.com (static-72-90-70-109.syrcny.fios.verizon.net. [72.90.70.109]) by smtp.gmail.com with ESMTPSA id t5-20020a05620a450500b006fa8299b4d5sm6294846qkp.100.2023.01.09.15.36.01 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 09 Jan 2023 15:36:02 -0800 (PST) From: Jonathan Cormier To: linux-hwmon@vger.kernel.org Cc: Jonathan Cormier , Jean Delvare , Guenter Roeck , linux-kernel@vger.kernel.org, devicetree@vger.kernel.org, Rob Herring , Krzysztof Kozlowski , Bob Duke , John Pruitt Subject: [PATCH v3 5/5] hwmon: ltc2945: Convert division to DIV_ROUND_CLOSEST_ULL Date: Mon, 9 Jan 2023 18:35:34 -0500 Message-Id: <20230109233534.1932370-6-jcormier@criticallink.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230109233534.1932370-1-jcormier@criticallink.com> References: <20221214220727.1350784-3-jcormier@criticallink.com> <20230109233534.1932370-1-jcormier@criticallink.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: devicetree@vger.kernel.org Convert division to DIV_ROUND_CLOSEST_ULL to match code in same function Signed-off-by: Jonathan Cormier --- drivers/hwmon/ltc2945.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/drivers/hwmon/ltc2945.c b/drivers/hwmon/ltc2945.c index 0d743d75459c..5456c1b60b8b 100644 --- a/drivers/hwmon/ltc2945.c +++ b/drivers/hwmon/ltc2945.c @@ -203,7 +203,7 @@ static int ltc2945_val_to_reg(struct device *dev, u8 reg, case LTC2945_MAX_VIN_THRES_H: case LTC2945_MIN_VIN_THRES_H: /* 25 mV resolution. */ - val /= 25; + val = DIV_ROUND_CLOSEST_ULL(val, 25); break; case LTC2945_ADIN_H: case LTC2945_MAX_ADIN_H: