[PATCH] [RFC] pwm: sun4i: Move pwm_calculate out of spin_lock

Clément Péron peron.clem at gmail.com
Thu Jan 9 15:31:06 PST 2020

pwm_calculate calls clk_get_rate while holding a spin_lock.

This create an issue as clk_get_rate() may sleep.

Move pwm_calculate out of this spin_lock.

Fixes: c32c5c50d4fe ("pwm: sun4i: Switch to atomic PWM")
Reported-by: Alex Mobigo <alex.mobigo at gmail.com>
Suggested-by: Vasily Khoruzhick <anarsoul at gmail.com>
Signed-off-by: Clément Péron <peron.clem at gmail.com>


this issue has been reported on linux-sunxi Google groups.

I don't have a board with PWM to confirm it.

Please wait a tested-by.


 drivers/pwm/pwm-sun4i.c | 7 +++----
 1 file changed, 3 insertions(+), 4 deletions(-)

diff --git a/drivers/pwm/pwm-sun4i.c b/drivers/pwm/pwm-sun4i.c
index 1afd41ebd3fd..6b230029dc49 100644
--- a/drivers/pwm/pwm-sun4i.c
+++ b/drivers/pwm/pwm-sun4i.c
@@ -248,19 +248,18 @@ static int sun4i_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm,
-	spin_lock(&sun4i_pwm->ctrl_lock);
-	ctrl = sun4i_pwm_readl(sun4i_pwm, PWM_CTRL_REG);
 	ret = sun4i_pwm_calculate(sun4i_pwm, state, &duty, &period, &prescaler,
 	if (ret) {
 		dev_err(chip->dev, "period exceeds the maximum value\n");
-		spin_unlock(&sun4i_pwm->ctrl_lock);
 		if (!cstate.enabled)
 		return ret;
+	spin_lock(&sun4i_pwm->ctrl_lock);
+	ctrl = sun4i_pwm_readl(sun4i_pwm, PWM_CTRL_REG);
 	if (sun4i_pwm->data->has_direct_mod_clk_output) {
 		if (bypass) {
 			ctrl |= BIT_CH(PWM_BYPASS, pwm->hwpwm);

More information about the linux-arm-kernel mailing list