Skip to content

Commit

Permalink
pwm: sifive: Enable clk only after period check in .apply()
Browse files Browse the repository at this point in the history
For the period check and the initial calculations of register values there
is no hardware access needed. So delay enabling the clk a bit to simplify
the code flow a bit.

Signed-off-by: Uwe Kleine-König <u.kleine-koenig@pengutronix.de>
Tested-by: Emil Renner Berthing <emil.renner.berthing@canonical.com>
Signed-off-by: Thierry Reding <thierry.reding@gmail.com>
  • Loading branch information
Uwe Kleine-König authored and Thierry Reding committed Jul 29, 2022
1 parent 0f02f49 commit 3586b02
Showing 1 changed file with 8 additions and 10 deletions.
18 changes: 8 additions & 10 deletions drivers/pwm/pwm-sifive.c
Original file line number Diff line number Diff line change
Expand Up @@ -139,12 +139,6 @@ static int pwm_sifive_apply(struct pwm_chip *chip, struct pwm_device *pwm,
if (state->polarity != PWM_POLARITY_INVERSED)
return -EINVAL;

ret = clk_enable(ddata->clk);
if (ret) {
dev_err(ddata->chip.dev, "Enable clk failed\n");
return ret;
}

cur_state = pwm->state;
enabled = cur_state.enabled;

Expand All @@ -167,14 +161,19 @@ static int pwm_sifive_apply(struct pwm_chip *chip, struct pwm_device *pwm,
if (state->period != ddata->approx_period) {
if (ddata->user_count != 1) {
mutex_unlock(&ddata->lock);
ret = -EBUSY;
goto exit;
return -EBUSY;
}
ddata->approx_period = state->period;
pwm_sifive_update_clock(ddata, clk_get_rate(ddata->clk));
}
mutex_unlock(&ddata->lock);

ret = clk_enable(ddata->clk);
if (ret) {
dev_err(ddata->chip.dev, "Enable clk failed\n");
return ret;
}

writel(frac, ddata->regs + PWM_SIFIVE_PWMCMP(pwm->hwpwm));

if (state->enabled != enabled) {
Expand All @@ -186,9 +185,8 @@ static int pwm_sifive_apply(struct pwm_chip *chip, struct pwm_device *pwm,
}
}

exit:
clk_disable(ddata->clk);
return ret;
return 0;
}

static const struct pwm_ops pwm_sifive_ops = {
Expand Down

0 comments on commit 3586b02

Please sign in to comment.