[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <20230609142155.h5fvn4fxdcleoznw@pengutronix.de>
Date: Fri, 9 Jun 2023 16:21:55 +0200
From: Uwe Kleine-König <u.kleine-koenig@...gutronix.de>
To: Fabrice Gasnier <fabrice.gasnier@...s.st.com>
Cc: Philipp Zabel <p.zabel@...gutronix.de>,
Thierry Reding <thierry.reding@...il.com>,
Maxime Coquelin <mcoquelin.stm32@...il.com>,
Alexandre Torgue <alexandre.torgue@...s.st.com>,
linux-pwm@...r.kernel.org,
linux-stm32@...md-mailman.stormreply.com,
linux-arm-kernel@...ts.infradead.org, linux-kernel@...r.kernel.org
Subject: Re: [PATCH] pwm: stm32: Implement .get_state()
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/ |
Download attachment "signature.asc" of type "application/pgp-signature" (489 bytes)
Powered by blists - more mailing lists