Received: by 2002:a05:7412:d8a:b0:e2:908c:2ebd with SMTP id b10csp3218530rdg; Tue, 17 Oct 2023 08:01:48 -0700 (PDT) X-Google-Smtp-Source: AGHT+IExDRKyVYM64ke/ryF2qJ0NXe6Yrg8ZixTjfVZhe7qtysex+NsM0NpgxmxkDPfT03Zmy3kO X-Received: by 2002:a05:6a00:23ca:b0:6b1:bf32:4fb5 with SMTP id g10-20020a056a0023ca00b006b1bf324fb5mr2620246pfc.28.1697554907813; Tue, 17 Oct 2023 08:01:47 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1697554907; cv=none; d=google.com; s=arc-20160816; b=OnoMb7qr65DQzi5KMKoTjoI1oUg0U8bBmSgZSB4WK+oIDWHc4VDdyjnb1mfF4sd9tG VjuMPlJ5GvcnYnDbjJDJsKDapAHT2MvGT7hhJeGxnlPmEQB4seQfBK/mPVvylGOsKnGZ uY6koX1j/q+bPuuTFLPGvkUa6ZX1WDEywuoveAqjfLXFleCgQEiu5+ftMevi4uJddrVZ JX+6ZOov/762ntQpV5eE4KpxiotDdSCnPIDGV5O1OPTdkHufWufv48o1PpgFfx3Slz0z FV2hemzH0fOb0amqRNeF4tg9ODtRA4/alBC+QkZjXUX7JaInKImQl/F4TJN6GUBp/dbC HI6w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:subject:message-id:date:from:in-reply-to :references:mime-version:dkim-signature; bh=Uq+1vYvsKUhR+01sCVILJR2at0sMDRh6S+TkivbpdsI=; fh=YjKuYIxCneAPZ7l0PUmssBd+pKDQCp5z73uw6KJPsSk=; b=lxbTXLR7wIyFHhEFGaCka7BHoU5KvsDPm974VHATsx+9yc4xojxPvHNGBpUk2WmkY4 2p+h42C/GZlHzFuRkaDMawMyGfJtk+dynvbKDoVi3SHuxfMJM9JDdGU3vNlzNSY+rZrZ h+4hV3tNe0fKrcHj8EwSr5Oc03dZqNmtqJHZvueHP3v0UTtzW3wJ1mfb4RcWEMUMrMSS q//wskGifCA6Qv8pHe8/8XP7yL0JD2WWzJjd6oxGkdSTrvZSl94/OHW3DMdl8okUeR5V Pyve4OFf+DOsF9tnZACnS+6E0F/jmN58/RfmsdTzeS1AvVoxdCj6LaQi64NCD8CpeX8z 4Q9Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@9elements.com header.s=google header.b=dxLh6l71; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:8 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=9elements.com Return-Path: Received: from fry.vger.email (fry.vger.email. [2620:137:e000::3:8]) by mx.google.com with ESMTPS id b62-20020a62cf41000000b006b18ecd03dasi1719704pfg.264.2023.10.17.08.01.37 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 17 Oct 2023 08:01:47 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:8 as permitted sender) client-ip=2620:137:e000::3:8; Authentication-Results: mx.google.com; dkim=pass header.i=@9elements.com header.s=google header.b=dxLh6l71; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:8 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=9elements.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by fry.vger.email (Postfix) with ESMTP id 221FF806141E; Tue, 17 Oct 2023 08:01:35 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at fry.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1344098AbjJQPBT (ORCPT + 99 others); Tue, 17 Oct 2023 11:01:19 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53960 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1344080AbjJQPBS (ORCPT ); Tue, 17 Oct 2023 11:01:18 -0400 Received: from mail-pj1-x102d.google.com (mail-pj1-x102d.google.com [IPv6:2607:f8b0:4864:20::102d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5534710F for ; Tue, 17 Oct 2023 08:01:15 -0700 (PDT) Received: by mail-pj1-x102d.google.com with SMTP id 98e67ed59e1d1-27d4372322aso2452506a91.3 for ; Tue, 17 Oct 2023 08:01:15 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=9elements.com; s=google; t=1697554875; x=1698159675; darn=vger.kernel.org; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=Uq+1vYvsKUhR+01sCVILJR2at0sMDRh6S+TkivbpdsI=; b=dxLh6l711+sNW2qDv26FbbOQHlZp88JFydocY+9vB/zHc7axS9VqF3aaJlqpiUtM/y /5CEESAdYKuVH3QO1t8yEm8OnUhwi9KXBtvKsaDCuYmTAEnLo7nkx+tBOlq9fmre+HyL SBJkUFw5oUB2PEEdvAgfW4seoEPGmzrHuEzg4RkGtOw8fLoYGZtq9HHVfcSG//zgm5/l DDsr3z/vES/YP31SvSNyFGzcu5+swsXbURU8u0T1x5w32DCY8kb8pAXWVJV2jBxUsjHS S7XF7XhThEi+hKswyH76Op103tVD2F8lifT1IYDeSB3lb5tfMIPiP0mGtP9m/uQt1cdd RmDA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1697554875; x=1698159675; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=Uq+1vYvsKUhR+01sCVILJR2at0sMDRh6S+TkivbpdsI=; b=QVx0vqvZ5Qdrj4ioHNw3+a7cGjmB3Eh6Clrg1H4g5W5DzscTPIOexz2iZWvAb6SFq1 9xPAqt+d0MFAb6Sve1Fl7gPq7E0902DJfKwA5REaw/cRadj1TKCItGak5G1vHjOndQTk eni/2Kt8o6sz2pSgFqN11Iss+rYuf3T+gIctbguM5TRuX0bsHP+OptROK5EDCeVIQDXM wecDVopdLYALZEDyseDQgTqViCUJ7J/QpyRWuPzAw/4A5dxolLXTFxGoOWTdm9OVpYkS h9l+Q67h66q81zdU6+8Z/W9shtxQSeg7ZzCvZNCJ6+XGSuSlx7kdTiI8BdvtFKPKN16l d4YQ== X-Gm-Message-State: AOJu0YxXNZPoQMyussIVsTTfq437Ca4fNQgQjcBUs4Grs4/yGL1fDmpi a1uGYccjATa6ap6AK4VgdvqV0e0zZycR5F7M5fS//g== X-Received: by 2002:a17:90a:ac0a:b0:27d:3968:8366 with SMTP id o10-20020a17090aac0a00b0027d39688366mr2439909pjq.8.1697554874479; Tue, 17 Oct 2023 08:01:14 -0700 (PDT) MIME-Version: 1.0 References: <20231005075508.1656071-1-naresh.solanki@9elements.com> In-Reply-To: From: Naresh Solanki Date: Tue, 17 Oct 2023 20:31:05 +0530 Message-ID: Subject: Re: [PATCH v5] regulator (max5970): Add hwmon support To: Guenter Roeck Cc: broonie@kernel.org, zev@bewilderbeest.net, Liam Girdwood , Jean Delvare , linux-kernel@vger.kernel.org, linux-hwmon@vger.kernel.org Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-0.8 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI, SPF_HELO_NONE,SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on fry.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (fry.vger.email [0.0.0.0]); Tue, 17 Oct 2023 08:01:35 -0700 (PDT) Hi, On Wed, 11 Oct 2023 at 19:50, Guenter Roeck wrote: > > On Thu, Oct 05, 2023 at 09:55:07AM +0200, Naresh Solanki wrote: > > Utilize the integrated 10-bit ADC in Max5970/Max5978 to enable voltage > > and current monitoring. This feature is seamlessly integrated through > > the hwmon subsystem. > > > > Signed-off-by: Naresh Solanki > > --- > > Changes in V5: > > - Simplify memory allocation for rdevs > > Changes in V4: > > - Use IS_REACHABLE > > - Use rdevs array for hwmon ops. > > - Remove duplicate i2c_set_clientdata > > I just noticed this one. Not my decision how to handle this, > but the change is really unrelated to this patch. Sure. Will make separate change for it. > > > Changes in V3: > > - Update signed-off > > - Add break > > - Update hwmon dev register name to max5970 > > - Remove changes in Kconfig. > > Changes in V2: > > - default case added for switch statement > > - Add dependency on HWMON > > --- > > drivers/regulator/max5970-regulator.c | 141 +++++++++++++++++++++++++- > > 1 file changed, 139 insertions(+), 2 deletions(-) > > > > diff --git a/drivers/regulator/max5970-regulator.c b/drivers/regulator/max5970-regulator.c > > index b56a174cde3d..71b4a543339a 100644 > > --- a/drivers/regulator/max5970-regulator.c > > +++ b/drivers/regulator/max5970-regulator.c > > @@ -10,6 +10,7 @@ > > #include > > #include > > #include > > +#include > > #include > > #include > > #include > > @@ -32,6 +33,128 @@ enum max597x_regulator_id { > > MAX597X_SW1, > > }; > > > > +static int max5970_read_adc(struct regmap *regmap, int reg, long *val) > > +{ > > + u8 reg_data[2]; > > + int ret; > > + > > + ret = regmap_bulk_read(regmap, reg, ®_data[0], 2); > > + if (ret < 0) > > + return ret; > > + > > + *val = (reg_data[0] << 2) | (reg_data[1] & 3); > > + > > + return 0; > > +} > > + > > +static int max5970_read(struct device *dev, enum hwmon_sensor_types type, > > + u32 attr, int channel, long *val) > > +{ > > + struct regulator_dev **rdevs = dev_get_drvdata(dev); > > + struct max5970_regulator *ddata = rdev_get_drvdata(rdevs[channel]); > > + struct regmap *regmap = ddata->regmap; > > + int ret; > > + > > + switch (type) { > > + case hwmon_curr: > > + switch (attr) { > > + case hwmon_curr_input: > > + ret = max5970_read_adc(regmap, MAX5970_REG_CURRENT_H(channel), val); > > if (ret < 0) > return ret; Ack > > > + /* > > + * Calculate current from ADC value, IRNG range & shunt resistor value. > > + * ddata->irng holds the voltage corresponding to the maximum value the > > + * 10-bit ADC can measure. > > + * To obtain the output, multiply the ADC value by the IRNG range (in > > + * millivolts) and then divide it by the maximum value of the 10-bit ADC. > > + */ > > + *val = (*val * ddata->irng) >> 10; > > + /* Convert the voltage meansurement across shunt resistor to current */ > > + *val = (*val * 1000) / ddata->shunt_micro_ohms; > > + return ret; > > return 0; Ack > > > + default: > > + return -EOPNOTSUPP; > > + } > > + > > + case hwmon_in: > > + switch (attr) { > > + case hwmon_in_input: > > + ret = max5970_read_adc(regmap, MAX5970_REG_VOLTAGE_H(channel), val); > if (ret < 0) > return ret; > Ack > > + /* > > + * Calculate voltage from ADC value and MON range. > > + * ddata->mon_rng holds the voltage corresponding to the maximum value the > > + * 10-bit ADC can measure. > > + * To obtain the output, multiply the ADC value by the MON range (in > > + * microvolts) and then divide it by the maximum value of the 10-bit ADC. > > + */ > > + *val = mul_u64_u32_shr(*val, ddata->mon_rng, 10); > > + /* uV to mV */ > > + *val = *val / 1000; > > + return ret; > > return 0; > Ack > > + default: > > + return -EOPNOTSUPP; > > + } > > + default: > > + return -EOPNOTSUPP; > > + } > > +} > > + > > +static umode_t max5970_is_visible(const void *data, > > + enum hwmon_sensor_types type, > > + u32 attr, int channel) > > +{ > > + struct regulator_dev **rdevs = (struct regulator_dev **)data; > > + struct max5970_regulator *ddata; > > + > > + if (channel >= MAX5970_NUM_SWITCHES || !rdevs[channel]) > > + return 0; > > + > > + ddata = rdev_get_drvdata(rdevs[channel]); > > + > > + if (!ddata || channel >= ddata->num_switches) > > How would ddata ever be NULL ? I don't see that check > elsewhere in the driver. > Will remove the NULL check. > > + return 0; > > + > > + switch (type) { > > + case hwmon_in: > > + switch (attr) { > > + case hwmon_in_input: > > + return 0444; > > + default: > > + break; > > + } > > + break; > > + case hwmon_curr: > > + switch (attr) { > > + case hwmon_curr_input: > > + /* Current measurement requires knowledge of the shunt resistor value. */ > > + if (ddata->shunt_micro_ohms) > > + return 0444; > > The common approach in hwmon drivers is to define a default > (typically 1 mOhm) if the value is not set, to let the user > configure it from userspace with the sensors configuration file. > > Checking ... Never mind. I guess the problem here is that while the > shunt-resistor-micro-ohms property is mandatory, the dt author can > explicitly configure a shunt resistor value of 0 and that is accepted > by the rest of the code. I am not sure if that makes sense, but ... > With this in mind, keep as is, but please add a note explaining that > this can happen. Ack Regards, Naresh > > > + break; > > + default: > > + break; > > + } > > + break; > > + default: > > + break; > > + } > > + return 0; > > +} > > + > > +static const struct hwmon_ops max5970_hwmon_ops = { > > + .is_visible = max5970_is_visible, > > + .read = max5970_read, > > +}; > > + > > +static const struct hwmon_channel_info *max5970_info[] = { > > + HWMON_CHANNEL_INFO(in, HWMON_I_INPUT, HWMON_I_INPUT), > > + HWMON_CHANNEL_INFO(curr, HWMON_C_INPUT, HWMON_C_INPUT), > > + NULL > > +}; > > + > > +static const struct hwmon_chip_info max5970_chip_info = { > > + .ops = &max5970_hwmon_ops, > > + .info = max5970_info, > > +}; > > + > > static int max597x_uvp_ovp_check_mode(struct regulator_dev *rdev, int severity) > > { > > int ret, reg; > > @@ -431,7 +554,8 @@ static int max597x_regulator_probe(struct platform_device *pdev) > > struct i2c_client *i2c = to_i2c_client(pdev->dev.parent); > > struct regulator_config config = { }; > > struct regulator_dev *rdev; > > - struct regulator_dev *rdevs[MAX5970_NUM_SWITCHES]; > > + struct regulator_dev **rdevs = NULL; > > + struct device *hwmon_dev; > > int num_switches; > > int ret, i; > > > > @@ -442,6 +566,11 @@ static int max597x_regulator_probe(struct platform_device *pdev) > > if (!max597x) > > return -ENOMEM; > > > > + rdevs = devm_kcalloc(&i2c->dev, MAX5970_NUM_SWITCHES, sizeof(struct regulator_dev *), > > + GFP_KERNEL); > > + if (!rdevs) > > + return -ENOMEM; > > + > > i2c_set_clientdata(i2c, max597x); > > > > if (of_device_is_compatible(i2c->dev.of_node, "maxim,max5978")) > > @@ -451,7 +580,6 @@ static int max597x_regulator_probe(struct platform_device *pdev) > > else > > return -ENODEV; > > > > - i2c_set_clientdata(i2c, max597x); > > num_switches = max597x->num_switches; > > > > for (i = 0; i < num_switches; i++) { > > @@ -485,6 +613,15 @@ static int max597x_regulator_probe(struct platform_device *pdev) > > max597x->shunt_micro_ohms[i] = data->shunt_micro_ohms; > > } > > > > + if (IS_REACHABLE(CONFIG_HWMON)) { > > + hwmon_dev = devm_hwmon_device_register_with_info(&i2c->dev, "max5970", rdevs, > > + &max5970_chip_info, NULL); > > + if (IS_ERR(hwmon_dev)) { > > + return dev_err_probe(&i2c->dev, PTR_ERR(hwmon_dev), > > + "Unable to register hwmon device\n"); > > + } > > + } > > + > > if (i2c->irq) { > > ret = > > max597x_setup_irq(&i2c->dev, i2c->irq, rdevs, num_switches, > > > > base-commit: f9a1d31874c383f58bb4f89bfe79b764682cd026 > > -- > > 2.41.0 > >