Skip to content

Commit 0f02f49

Browse files
Uwe Kleine-Königthierryreding
authored andcommitted
pwm: sifive: Reduce time the controller lock is held
The lock is only to serialize access and update to user_count and approx_period between different PWMs served by the same pwm_chip. So the lock needs only to be taken during the check if the (chip global) period can and/or needs to be changed. Signed-off-by: Uwe Kleine-König <[email protected]> Tested-by: Emil Renner Berthing <[email protected]> Signed-off-by: Thierry Reding <[email protected]>
1 parent 61180f6 commit 0f02f49

File tree

1 file changed

+5
-3
lines changed

1 file changed

+5
-3
lines changed

drivers/pwm/pwm-sifive.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141

4242
struct pwm_sifive_ddata {
4343
struct pwm_chip chip;
44-
struct mutex lock; /* lock to protect user_count */
44+
struct mutex lock; /* lock to protect user_count and approx_period */
4545
struct notifier_block notifier;
4646
struct clk *clk;
4747
void __iomem *regs;
@@ -76,6 +76,7 @@ static void pwm_sifive_free(struct pwm_chip *chip, struct pwm_device *pwm)
7676
mutex_unlock(&ddata->lock);
7777
}
7878

79+
/* Called holding ddata->lock */
7980
static void pwm_sifive_update_clock(struct pwm_sifive_ddata *ddata,
8081
unsigned long rate)
8182
{
@@ -144,7 +145,6 @@ static int pwm_sifive_apply(struct pwm_chip *chip, struct pwm_device *pwm,
144145
return ret;
145146
}
146147

147-
mutex_lock(&ddata->lock);
148148
cur_state = pwm->state;
149149
enabled = cur_state.enabled;
150150

@@ -163,14 +163,17 @@ static int pwm_sifive_apply(struct pwm_chip *chip, struct pwm_device *pwm,
163163
/* The hardware cannot generate a 100% duty cycle */
164164
frac = min(frac, (1U << PWM_SIFIVE_CMPWIDTH) - 1);
165165

166+
mutex_lock(&ddata->lock);
166167
if (state->period != ddata->approx_period) {
167168
if (ddata->user_count != 1) {
169+
mutex_unlock(&ddata->lock);
168170
ret = -EBUSY;
169171
goto exit;
170172
}
171173
ddata->approx_period = state->period;
172174
pwm_sifive_update_clock(ddata, clk_get_rate(ddata->clk));
173175
}
176+
mutex_unlock(&ddata->lock);
174177

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

@@ -185,7 +188,6 @@ static int pwm_sifive_apply(struct pwm_chip *chip, struct pwm_device *pwm,
185188

186189
exit:
187190
clk_disable(ddata->clk);
188-
mutex_unlock(&ddata->lock);
189191
return ret;
190192
}
191193

0 commit comments

Comments
 (0)