Skip to content

Commit e56ec7b

Browse files
pH5thierryreding
authored andcommitted
pwm: stm32: Implement .get_state()
Implement the &pwm_ops->get_state callback so drivers can inherit PWM state set by the bootloader. Signed-off-by: Philipp Zabel <[email protected]> [ukl: split off from a patch that also fixes clk enable count in .probe()] Signed-off-by: Uwe Kleine-König <[email protected]> Reviewed-by: Fabrice Gasnier <[email protected]> Signed-off-by: Thierry Reding <[email protected]>
1 parent 41fa8f5 commit e56ec7b

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

drivers/pwm/pwm-stm32.c

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -471,8 +471,50 @@ static int stm32_pwm_apply_locked(struct pwm_chip *chip, struct pwm_device *pwm,
471471
return ret;
472472
}
473473

474+
static int stm32_pwm_get_state(struct pwm_chip *chip,
475+
struct pwm_device *pwm, struct pwm_state *state)
476+
{
477+
struct stm32_pwm *priv = to_stm32_pwm_dev(chip);
478+
int ch = pwm->hwpwm;
479+
unsigned long rate;
480+
u32 ccer, psc, arr, ccr;
481+
u64 dty, prd;
482+
int ret;
483+
484+
mutex_lock(&priv->lock);
485+
486+
ret = regmap_read(priv->regmap, TIM_CCER, &ccer);
487+
if (ret)
488+
goto out;
489+
490+
state->enabled = ccer & (TIM_CCER_CC1E << (ch * 4));
491+
state->polarity = (ccer & (TIM_CCER_CC1P << (ch * 4))) ?
492+
PWM_POLARITY_INVERSED : PWM_POLARITY_NORMAL;
493+
ret = regmap_read(priv->regmap, TIM_PSC, &psc);
494+
if (ret)
495+
goto out;
496+
ret = regmap_read(priv->regmap, TIM_ARR, &arr);
497+
if (ret)
498+
goto out;
499+
ret = regmap_read(priv->regmap, TIM_CCR1 + 4 * ch, &ccr);
500+
if (ret)
501+
goto out;
502+
503+
rate = clk_get_rate(priv->clk);
504+
505+
prd = (u64)NSEC_PER_SEC * (psc + 1) * (arr + 1);
506+
state->period = DIV_ROUND_UP_ULL(prd, rate);
507+
dty = (u64)NSEC_PER_SEC * (psc + 1) * ccr;
508+
state->duty_cycle = DIV_ROUND_UP_ULL(dty, rate);
509+
510+
out:
511+
mutex_unlock(&priv->lock);
512+
return ret;
513+
}
514+
474515
static const struct pwm_ops stm32pwm_ops = {
475516
.apply = stm32_pwm_apply_locked,
517+
.get_state = stm32_pwm_get_state,
476518
.capture = IS_ENABLED(CONFIG_DMA_ENGINE) ? stm32_pwm_capture : NULL,
477519
};
478520

0 commit comments

Comments
 (0)