Linux Hardware Monitor development
 help / color / mirror / Atom feed
From: Guenter Roeck <linux@roeck-us.net>
To: Igor Reznichenko <igor@reznichenko.net>
Cc: conor+dt@kernel.org, corbet@lwn.net,
	david.hunter.linux@gmail.com, devicetree@vger.kernel.org,
	krzk+dt@kernel.org, linux-doc@vger.kernel.org,
	linux-hwmon@vger.kernel.org, linux-kernel@vger.kernel.org,
	robh@kernel.org, skhan@linuxfoundation.org
Subject: Re: [PATCH v2 2/2] hwmon: Add TSC1641 I2C power monitor driver
Date: Mon, 27 Oct 2025 09:52:53 -0700	[thread overview]
Message-ID: <afe3f2e6-703b-4d9f-ae88-99da1321d1fc@roeck-us.net> (raw)
In-Reply-To: <20251027064127.648712-1-igor@reznichenko.net>

On 10/26/25 23:41, Igor Reznichenko wrote:
>> In some way this is inconsistent: It accepts a shunt resistor value of, say, 105
>> even though the chip can only accept multiples of 10 uOhm. In situations like this
>> I suggest to expect devicetree values to be accurate and to clamp values entered
>> through sysfs. More on that below.
>>
>>> +	return 0;
>>> +}
>>> +
>>> +static int tsc1641_set_shunt(struct tsc1641_data *data, u32 val)
>>> +{
>>> +	struct regmap *regmap = data->regmap;
>>> +	long rshunt_reg;
>>> +
>>> +	if (tsc1641_validate_shunt(val) < 0)
>>> +		return -EINVAL;
>>> +
>>> +	data->rshunt_uohm = val;
>>> +	data->current_lsb_ua = DIV_ROUND_CLOSEST(TSC1641_VSHUNT_LSB_NVOLT * 1000,
>>> +						 data->rshunt_uohm);
>>> +	/* RSHUNT register LSB is 10uOhm so need to divide further*/
>>> +	rshunt_reg = DIV_ROUND_CLOSEST(data->rshunt_uohm, TSC1641_RSHUNT_LSB_UOHM);
>>
>> This means that all calculations do not use the actual shunt resistor values used
>> by the chip, but an approximation. I would suggest to store and use the actual shunt
>> resistor value instead, not the one entered by the user.
> 
> By "actual shunt" you mean defined in devicetree? Then does it mean disabling
> writing value by user via sysfs and making "shunt_resistor" read-only or leaving it
> writable and clamping to devicetree value, thus discarding the user provided value?
> 

I said "used by the chip", and referred to the value written into TSC1641_RSHUNT_LSB_UOHM.

>> See below - clamping is insufficient for negative values, and it is not clear to me if
>> the limit register is signed or unsigned.
> 
>> Also, the datasheet doesn't say that the limit value would be signed. Did you verify
>> that negative temperature limit values are actually treated as negative values ?
> 
> SUL, SOL, TOL are signed, I verified. The negative limits for current and temperature
> work well based on my testing.
> 

Please add a respective comment into the code.

>> This doesn't work as intended for negative values. regmap doesn't expect to see
>> negative register values and returns an error if trying to write one, so clamping
>> against SHRT_MIN and SHRT_MAX is insufficient. You also need to mask the result
>> against 0xffff.
> 
> I was under impression regmap would handle this masking correctly when defining
> .val_bits = 16. E.g. in regmap.c:973 it selects formatting function for 16bit values.
> I can mask explicitly if it's required.
> It certainly doesn't throw error since negative alerts work as mentioned.
> 

My unit test code bails out on negative values, returning an error from regmap when
trying to write negative values. I had seen that before. Masking the value passed
to regmap with 0xffff solved the problem.

>> Why did you choose lcrit/crit attributes instead of min/max ? If there is only
>> one alert limit, that usually means the first level of alert, not a critical level.
>> Raising an alert does not mean it is a critical alert. Please reconsider.
> 
> I used hwmon/ina2xx.c as a reference. It covers many similar power monitors which
> have single threshold alerts and defines only lcrit/crit. If this is a wrong approach
> I'll change to min/max.

Isn't that great ? You can always find an example for everything in the Linux kernel
if you are looking for it.

Guenter


  reply	other threads:[~2025-10-27 16:52 UTC|newest]

Thread overview: 37+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-10-22  4:47 [PATCH 0/5] hwmon: Add TSC1641 I2C power monitor driver Igor Reznichenko
2025-10-22  4:47 ` [PATCH 1/5] drivers/hwmon: " Igor Reznichenko
2025-10-22 14:51   ` Guenter Roeck
2025-10-23  7:50     ` Igor Reznichenko
2025-10-23 12:55       ` Guenter Roeck
2025-10-22  4:47 ` [PATCH 2/5] drivers/hwmon: Add Kconfig entry for TSC1641 Igor Reznichenko
2025-10-22  6:49   ` Krzysztof Kozlowski
2025-10-22  4:47 ` [PATCH 3/5] drivers/hwmon: Add TSC1641 module to Makefile Igor Reznichenko
2025-10-22  6:49   ` Krzysztof Kozlowski
2025-10-22  4:47 ` [PATCH 4/5] Documentation/hwmon: Add TSC1641 driver documentation Igor Reznichenko
2025-10-22  4:47 ` [PATCH 5/5] Documentation/devicetree/bindings/hwmon: Add TSC1641 binding Igor Reznichenko
2025-10-22  6:29   ` Rob Herring (Arm)
2025-10-22  6:48   ` Krzysztof Kozlowski
2025-10-22 14:07 ` [PATCH 0/5] hwmon: Add TSC1641 I2C power monitor driver Guenter Roeck
2025-10-26  6:50 ` [PATCH v2 0/2] " Igor Reznichenko
2025-10-26  6:50   ` [PATCH v2 1/2] dt-bindings: hwmon: Add support for ST TSC1641 power monitor Igor Reznichenko
2025-10-26 16:32     ` Krzysztof Kozlowski
2025-10-26 17:22       ` Guenter Roeck
2025-10-26 19:15         ` Krzysztof Kozlowski
2025-10-26 18:46       ` Igor Reznichenko
2025-10-26 19:41         ` Krzysztof Kozlowski
2025-10-26 19:58           ` Guenter Roeck
2025-10-27  8:40             ` Krzysztof Kozlowski
2025-10-27 16:53               ` Guenter Roeck
2025-10-27 18:01                 ` Krzysztof Kozlowski
2025-10-27 19:14                   ` Rob Herring
2025-10-28 15:17                     ` Igor Reznichenko
2025-10-28 15:33                       ` Guenter Roeck
2025-10-31  4:40                         ` Igor Reznichenko
2025-10-31  7:57                           ` Krzysztof Kozlowski
2025-10-31 17:30                             ` Igor Reznichenko
2025-10-31 18:37                               ` Guenter Roeck
2025-10-26  6:50   ` [PATCH v2 2/2] hwmon: Add TSC1641 I2C power monitor driver Igor Reznichenko
2025-10-26 17:08     ` Guenter Roeck
2025-10-27  6:41       ` Igor Reznichenko
2025-10-27 16:52         ` Guenter Roeck [this message]
2025-10-26 16:28   ` [PATCH v2 0/2] " Krzysztof Kozlowski

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=afe3f2e6-703b-4d9f-ae88-99da1321d1fc@roeck-us.net \
    --to=linux@roeck-us.net \
    --cc=conor+dt@kernel.org \
    --cc=corbet@lwn.net \
    --cc=david.hunter.linux@gmail.com \
    --cc=devicetree@vger.kernel.org \
    --cc=igor@reznichenko.net \
    --cc=krzk+dt@kernel.org \
    --cc=linux-doc@vger.kernel.org \
    --cc=linux-hwmon@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=robh@kernel.org \
    --cc=skhan@linuxfoundation.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox