Skip to content

Commit

Permalink
pwm: stm32: Fix enable count for clk in .probe()
Browse files Browse the repository at this point in the history
Make the driver take over hardware state without disabling in .probe()
and enable the clock for each enabled channel.

Signed-off-by: Philipp Zabel <p.zabel@pengutronix.de>
[ukleinek: split off from a patch that also implemented .get_state()]
Signed-off-by: Uwe Kleine-König <u.kleine-koenig@pengutronix.de>
Fixes: 7edf736 ("pwm: Add driver for STM32 plaftorm")
Reviewed-by: Fabrice Gasnier <fabrice.gasnier@foss.st.com>
Signed-off-by: Thierry Reding <thierry.reding@gmail.com>
  • Loading branch information
Philipp Zabel authored and Thierry Reding committed Dec 20, 2023
1 parent e56ec7b commit 19f1016
Showing 1 changed file with 14 additions and 4 deletions.
18 changes: 14 additions & 4 deletions drivers/pwm/pwm-stm32.c
Original file line number Diff line number Diff line change
Expand Up @@ -605,17 +605,21 @@ static void stm32_pwm_detect_complementary(struct stm32_pwm *priv)
priv->have_complementary_output = (ccer != 0);
}

static unsigned int stm32_pwm_detect_channels(struct stm32_pwm *priv)
static unsigned int stm32_pwm_detect_channels(struct stm32_pwm *priv,
unsigned int *num_enabled)
{
u32 ccer;
u32 ccer, ccer_backup;

/*
* If channels enable bits don't exist writing 1 will have no
* effect so we can detect and count them.
*/
regmap_read(priv->regmap, TIM_CCER, &ccer_backup);
regmap_set_bits(priv->regmap, TIM_CCER, TIM_CCER_CCXE);
regmap_read(priv->regmap, TIM_CCER, &ccer);
regmap_clear_bits(priv->regmap, TIM_CCER, TIM_CCER_CCXE);
regmap_write(priv->regmap, TIM_CCER, ccer_backup);

*num_enabled = hweight32(ccer_backup & TIM_CCER_CCXE);

return hweight32(ccer & TIM_CCER_CCXE);
}
Expand All @@ -626,6 +630,8 @@ static int stm32_pwm_probe(struct platform_device *pdev)
struct device_node *np = dev->of_node;
struct stm32_timers *ddata = dev_get_drvdata(pdev->dev.parent);
struct stm32_pwm *priv;
unsigned int num_enabled;
unsigned int i;
int ret;

priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
Expand All @@ -648,7 +654,11 @@ static int stm32_pwm_probe(struct platform_device *pdev)

priv->chip.dev = dev;
priv->chip.ops = &stm32pwm_ops;
priv->chip.npwm = stm32_pwm_detect_channels(priv);
priv->chip.npwm = stm32_pwm_detect_channels(priv, &num_enabled);

/* Initialize clock refcount to number of enabled PWM channels. */
for (i = 0; i < num_enabled; i++)
clk_enable(priv->clk);

ret = devm_pwmchip_add(dev, &priv->chip);
if (ret < 0)
Expand Down

0 comments on commit 19f1016

Please sign in to comment.