Re: [PATCH v2 5/6] hwmon: (adt7470) Register as a PWM provider

Uwe Kleine-König <[email protected]> Sun, 19 Jul 2026 19:04:24 +0200
Newsgroups org.kernel.vger.linux-pwm,org.kernel.vger.linux-devicetree,org.kernel.vger.linux-hwmon,org.kernel.vger.linux-kernel
Message-ID <al0CHPmBmvCd5DW0@monoceros>
Hello,

On Fri, Jul 17, 2026 at 05:59:18PM -0300, Luiz Angelo Daros de Luca wrote:
> @@ -864,6 +865,57 @@ static int adt7470_pwm_write(struct device *dev, u32 attr, int channel, long val
>  	return err;
>  }
>  
> +static int adt7470_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm,
> +			     const struct pwm_state *state)
> +{
> +	struct adt7470_data *data = pwmchip_get_drvdata(chip);
> +	unsigned int pwm_auto_reg_mask;
> +	int err;
> +	u8 val;
> +
> +	if (pwm->hwpwm % 2)
> +		pwm_auto_reg_mask = ADT7470_PWM2_AUTO_MASK;
> +	else
> +		pwm_auto_reg_mask = ADT7470_PWM1_AUTO_MASK;
> +
> +	if (state->enabled && state->period > 0)
> +		val = DIV_ROUND_CLOSEST_ULL(state->duty_cycle * 255, state->period);

rounding a division using closest is wrong in the context of PWM.

Also state->duty_cycle * 255 might overflow and you have to use the
actual period in the calculation of val instead of the requested value.

> +	else
> +		val = 0;
> +
> +	mutex_lock(&data->lock);

If you use guard(), the control flow simplifies and using goto becomes
unnecessary.

> +	if (data->pwm[pwm->hwpwm] == val &&
> +	    data->pwm_automatic[pwm->hwpwm] == 0) {
> +		mutex_unlock(&data->lock);
> +		return 0;
> +	}
> +
> +	/* Put the PWM channel in manual mode before updating it. */
> +	err = regmap_update_bits(data->regmap,
> +				 ADT7470_REG_PWM_CFG(pwm->hwpwm),
> +				 pwm_auto_reg_mask, 0);
> +	if (err < 0)
> +		goto out;
> +
> +	data->pwm_automatic[pwm->hwpwm] = 0;
> +
> +	err = regmap_write(data->regmap,
> +			   ADT7470_REG_PWM(pwm->hwpwm), val);
> +	if (err < 0)
> +		goto out;
> +
> +	data->pwm[pwm->hwpwm] = val;
> +out:
> +	mutex_unlock(&data->lock);
> +
> +	return err;
> +}
> +
> +static const struct pwm_ops adt7470_pwm_ops = {
> +	.apply = adt7470_pwm_apply,

For new drivers please implement .round_waveform_tohw(),
.round_waveform_fromhw(), .write_waveform() and ideally so
.read_waveform().

> +};

Best regards
Uwe
signature.asc (application/pgp-signature, 488 B)
-----BEGIN PGP SIGNATURE-----

iQEzBAABCgAdFiEEP4GsaTp6HlmJrf7Tj4D7WH0S/k4FAmpdA5UACgkQj4D7WH0S
/k430Af+IAbgKHwB3wHiNuH68F/7F3iV8A3do+Oslwv7fyhg3Q2Lghx1h+cZafMr
N/kmHskCaZrYPOgBG43e/4mBG2ffDQF8m2vQXFnfVHoYBX0SOgqtyqm9xj5Kz6T5
DcUc+s5Jok+tgjvfbjRbiTY3qRqKCDYGIzF9cOJU3yHAsIPF33wPXmyUMqKFQ0nx
dWLbbvS/RN0qPfsFBBp1OOPUDisEo+Ix7RavTFLj1dfqeBEYP6jjdJtX42SkMI+L
ohOUO9t7r2vgGP3bv8flvlG7GTSDaE7A/NEzOHvsZbZlUcozX70l5zwt38vJjIg1
GMDeGjayOEkAfiRyf0/PlFtl7WPGwg==
=x6bT
-----END PGP SIGNATURE-----