From: Guenter Roeck <linux@roeck-us.net>
To: Chris Packham <chris.packham@alliedtelesis.co.nz>,
jdelvare@suse.com, robh@kernel.org, krzk+dt@kernel.org,
conor+dt@kernel.org, ukleinek@kernel.org
Cc: linux-hwmon@vger.kernel.org, devicetree@vger.kernel.org,
linux-kernel@vger.kernel.org, linux-pwm@vger.kernel.org
Subject: Re: [PATCH v5 3/3] hwmon: (adt7475) Add support for configuring initial PWM state
Date: Thu, 11 Jul 2024 21:37:29 -0700 [thread overview]
Message-ID: <bd2b256b-5149-491a-a482-6e4488467fa5@roeck-us.net> (raw)
In-Reply-To: <20240711234614.3104839-4-chris.packham@alliedtelesis.co.nz>
On 7/11/24 16:46, Chris Packham wrote:
> By default the PWM duty cycle in hardware is 100%. On some systems this
> can cause unwanted fan noise. Add the ability to specify the fan
> connections and initial state of the PWMs via device properties.
>
> Signed-off-by: Chris Packham <chris.packham@alliedtelesis.co.nz>
> ---
...
> +static int adt7475_pwm_properties_parse_reference_args(struct fwnode_handle *fwnode,
> + struct adt7475_pwm_config *cfg)
> +{
> + int ret;
> + struct fwnode_reference_args args = {};
> + int freq_hz;
> + int duty;
> +
> + ret = fwnode_property_get_reference_args(fwnode, "pwms", "#pwm-cells", 0, 0, &args);
> + if (ret)
> + return ret;
> +
> + if (args.nargs != 4) {
> + fwnode_handle_put(args.fwnode);
> + return -EINVAL;
> + }
> +
> + freq_hz = 1000000000UL / args.args[1];
> + duty = 255 / (args.args[1] / args.args[3]);
> +
You'll need to validate args.args[1] and args.args[3] to ensure that there are no
divide by 0 errors.
On a side note,
a = b / (c / d) == b / c * d (at least for d != 0)
Since the result is defined for d == 0, you'd only have to make sure
that args.args[1] > 0 and that the result for the duty cycle is <= 255.
> + cfg->index = args.args[0];
> + cfg->freq = find_closest(freq_hz, pwmfreq_table, ARRAY_SIZE(pwmfreq_table));
> + cfg->flags = args.args[2];
> + cfg->duty = clamp_val(duty, 0, 0xFF);
> +
> + fwnode_handle_put(args.fwnode);
> +
> + return 0;
> +}
> +
> +static int adt7475_pwm_properties_parse_args(struct fwnode_handle *fwnode,
> + struct adt7475_pwm_config *cfg)
> +{
> + int ret;
> + u32 args[4] = {};
> + int freq_hz;
> + int duty;
> +
> + ret = fwnode_property_read_u32_array(fwnode, "pwms", args, ARRAY_SIZE(args));
> + if (ret)
> + return ret;
> +
> + freq_hz = 1000000000UL / args[1];
> + duty = 255 / (args[1] / args[3]);
> +
> + cfg->index = args[0];
> + cfg->freq = find_closest(freq_hz, pwmfreq_table, ARRAY_SIZE(pwmfreq_table));
> + cfg->flags = args[2];
> + cfg->duty = clamp_val(duty, 0, 0xFF);
> +
The code above is duplicate; please combine into a single function
(I don't mind if it has four parameters).
Thanks,
Guenter
next prev parent reply other threads:[~2024-07-12 4:37 UTC|newest]
Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-07-11 23:46 [PATCH v5 0/3] hwmon: (adt7475) duty cycle configuration Chris Packham
2024-07-11 23:46 ` [PATCH v5 1/3] dt-bindings: hwmon: Add adt7475 fan/pwm properties Chris Packham
2024-07-15 12:17 ` Rob Herring (Arm)
2024-07-11 23:46 ` [PATCH v5 2/3] dt-bindings: hwmon: adt7475: Deprecate adi,pwm-active-state Chris Packham
2024-07-11 23:46 ` [PATCH v5 3/3] hwmon: (adt7475) Add support for configuring initial PWM state Chris Packham
2024-07-12 4:37 ` Guenter Roeck [this message]
2024-07-12 5:24 ` Uwe Kleine-König
2024-07-12 12:45 ` kernel test robot
2024-07-12 16:34 ` Uwe Kleine-König
2024-07-12 17:29 ` kernel test robot
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=bd2b256b-5149-491a-a482-6e4488467fa5@roeck-us.net \
--to=linux@roeck-us.net \
--cc=chris.packham@alliedtelesis.co.nz \
--cc=conor+dt@kernel.org \
--cc=devicetree@vger.kernel.org \
--cc=jdelvare@suse.com \
--cc=krzk+dt@kernel.org \
--cc=linux-hwmon@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-pwm@vger.kernel.org \
--cc=robh@kernel.org \
--cc=ukleinek@kernel.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