Re: [PATCH] pwm: stm32: Implement .get_state()

From: Uwe Kleine-König
Date: Fri Jun 09 2023 - 10:22:10 EST


Hello Fabrice,

On Fri, Jun 09, 2023 at 03:06:47PM +0200, Fabrice Gasnier wrote:
> On 6/8/23 16:06, Philipp Zabel wrote:
> > +static int stm32_pwm_get_state(struct pwm_chip *chip,
> > + struct pwm_device *pwm, struct pwm_state *state)
> > +{
> > + struct stm32_pwm *priv = to_stm32_pwm_dev(chip);
> > + int ch = pwm->hwpwm;
> > + unsigned long rate;
> > + u32 ccer, psc, arr, ccr;
> > + u64 dty, prd;
> > + int ret;
> > +
> > + ret = regmap_read(priv->regmap, TIM_CCER, &ccer);
> > + if (ret)
> > + return ret;
> > +
> > + state->enabled = ccer & (TIM_CCER_CC1E << (ch * 4));
> > + state->polarity = (ccer & (TIM_CCER_CC1P << (ch * 4))) ?
> > + PWM_POLARITY_INVERSED : PWM_POLARITY_NORMAL;
> > + regmap_read(priv->regmap, TIM_PSC, &psc);
> > + regmap_read(priv->regmap, TIM_ARR, &arr);
> > + read_ccrx(priv, ch, &ccr);
> > + rate = clk_get_rate(priv->clk);
> > +
> > + prd = (u64)NSEC_PER_SEC * (psc + 1) * (arr + 1);
> > + state->period = DIV_ROUND_UP_ULL(prd, rate);
> > + dty = (u64)NSEC_PER_SEC * (psc + 1) * ccr;
> > + state->duty_cycle = DIV_ROUND_UP_ULL(dty, rate);
>
> Just a question/thought, could it be worth to use DIV_ROUND_CLOSEST_ULL() ?

No, round up is the right choice. The reason for that is that .apply()
rounds down in its divisions. If you use ROUND_CLOSEST here, reapplying
the result from .get_state() might not be idempotent.

> > +
> > + return ret;
> > +}

Best regards
Uwe

--
Pengutronix e.K. | Uwe Kleine-König |
Industrial Linux Solutions | https://www.pengutronix.de/ |

Attachment: signature.asc
Description: PGP signature