From: Guenter Roeck <linux@roeck-us.net>
To: Naresh Solanki <naresh.solanki@9elements.com>
Cc: Jean Delvare <jdelvare@suse.com>,
linux-hwmon@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: Re: [PATCH] hwmon: (max6639) : Allow setting target RPM
Date: Wed, 26 Mar 2025 09:49:04 -0700 [thread overview]
Message-ID: <be099cf4-338b-45c8-b0d3-24b2cefe386e@roeck-us.net> (raw)
In-Reply-To: <CABqG17h8cpnFkdD-nnqyr+UnwADU9XWK6TGBxj_FCH37Y3Q1Lw@mail.gmail.com>
On 3/26/25 09:36, Naresh Solanki wrote:
> Hi Guenter,
>
> On Tue, 25 Mar 2025 at 05:00, Guenter Roeck <linux@roeck-us.net> wrote:
>>
>> On 3/24/25 11:57, Your Name wrote:
>>> From: Naresh Solanki <naresh.solanki@9elements.com>
>>>
>>> Currently, during startup, the fan is set to its maximum RPM by default,
>>> which may not be suitable for all use cases.
>>> This patch introduces support for specifying a target RPM via the Device
>>> Tree property "target-rpm".
>>>
>>> Changes:
>>> - Added `target_rpm` field to `max6639_data` structure to store the
>>> target RPM for each fan channel.
>>> - Modified `max6639_probe_child_from_dt()` to read the `"target-rpm"`
>>> property from the Device Tree and set `target_rpm` accordingly.
>>> - Updated `max6639_init_client()` to use `target_rpm` to compute the
>>> initial PWM duty cycle instead of defaulting to full speed (120/120).
>>>
>>> Behavior:
>>> - If `"target-rpm"` is specified, the fan speed is set accordingly.
>>> - If `"target-rpm"` is not specified, the previous behavior (full speed
>>> at startup) is retained.
>>>
>>
>> Unless I am missing something, that is not really correct. See below.
>>
>>> This allows better control over fan speed during system initialization.
>>>
>>> Signed-off-by: Naresh Solanki <naresh.solanki@9elements.com>
>>> ---
>>> drivers/hwmon/max6639.c | 15 ++++++++++++---
>>> 1 file changed, 12 insertions(+), 3 deletions(-)
>>>
>>> diff --git a/drivers/hwmon/max6639.c b/drivers/hwmon/max6639.c
>>> index 32b4d54b2076..ca8a8f58d133 100644
>>> --- a/drivers/hwmon/max6639.c
>>> +++ b/drivers/hwmon/max6639.c
>>> @@ -80,6 +80,7 @@ struct max6639_data {
>>> /* Register values initialized only once */
>>> u8 ppr[MAX6639_NUM_CHANNELS]; /* Pulses per rotation 0..3 for 1..4 ppr */
>>> u8 rpm_range[MAX6639_NUM_CHANNELS]; /* Index in above rpm_ranges table */
>>> + u32 target_rpm[MAX6639_NUM_CHANNELS];
>>>
>>> /* Optional regulator for FAN supply */
>>> struct regulator *reg;
>>> @@ -560,8 +561,14 @@ static int max6639_probe_child_from_dt(struct i2c_client *client,
>>> }
>>>
>>
>> target_rpm[] is 0 here.
>>
>>> err = of_property_read_u32(child, "max-rpm", &val);
>>> - if (!err)
>>> + if (!err) {
>>> data->rpm_range[i] = rpm_range_to_reg(val);
>>> + data->target_rpm[i] = val;
>>> + }
>>
>> If there is no max-rpm property, or if there is no devicetree support,
>> target_rpm[i] is still 0.
>>
>>> +
>>> + err = of_property_read_u32(child, "target-rpm", &val);
>>> + if (!err)
>>> + data->target_rpm[i] = val;
>>
>> If there is neither max-rpm nor target-rpm, target_rpm[i] is still 0.
>>
>>>
>>> return 0;
>>> }
>>> @@ -573,6 +580,7 @@ static int max6639_init_client(struct i2c_client *client,
>>> const struct device_node *np = dev->of_node;
>>> struct device_node *child;
>>> int i, err;
>>> + u8 target_duty;
>>>
>>> /* Reset chip to default values, see below for GCONFIG setup */
>>> err = regmap_write(data->regmap, MAX6639_REG_GCONFIG, MAX6639_GCONFIG_POR);
>>> @@ -639,8 +647,9 @@ static int max6639_init_client(struct i2c_client *client,
>>> if (err)
>>> return err;
>>>
>>> - /* PWM 120/120 (i.e. 100%) */
>>> - err = regmap_write(data->regmap, MAX6639_REG_TARGTDUTY(i), 120);
>>> + /* Set PWM based on target RPM if specified */
>>> + target_duty = 120 * data->target_rpm[i] / rpm_ranges[data->rpm_range[i]];
>>
>> If there is no devicetree support, or if neither max-rpm nor target-rpm are
>> provided, target_duty will be 0, and the fans will stop.
>>
>> Maybe my interpretation is wrong, but I think both target_rpm[] and rpm_range[]
>> will need to be initialized. Also, it seems to me that there will need to be an
>> upper bound for target_rpm[]; without it, it is possible that target_duty > 120,
>> which would probably not be a good idea.
> Yes you're right. I missed it in my analysis.
>
> Here is the logic that would address:
> target_rpm = 120;
> /* Set PWM based on target RPM if specified */
> if (data->target_rpm[i] != 0 &&
> data->target_rpm[i] <= rpm_ranges[data->rpm_range[i]]) {
>
> target_duty = 120 * data->target_rpm[i] /
> rpm_ranges[data->rpm_range[i]];
> }
>
> Please let me know your thoughts & suggestions.
>
I would prefer if target_rpm[] and rpm_range[] were pre-initialized with default
values in the probe function. That would avoid runtime checks.
Thanks,
Guenter
next prev parent reply other threads:[~2025-03-26 16:49 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-03-24 18:57 [PATCH] hwmon: (max6639) : Allow setting target RPM Your Name
2025-03-24 23:30 ` Guenter Roeck
2025-03-26 16:36 ` Naresh Solanki
2025-03-26 16:49 ` Guenter Roeck [this message]
2025-03-26 16:59 ` Naresh Solanki
2025-03-26 17:42 ` Guenter Roeck
2025-03-26 18:07 ` Naresh Solanki
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=be099cf4-338b-45c8-b0d3-24b2cefe386e@roeck-us.net \
--to=linux@roeck-us.net \
--cc=jdelvare@suse.com \
--cc=linux-hwmon@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=naresh.solanki@9elements.com \
/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