Skip to content

Commit fcc7607

Browse files
seanyoungthierryreding
authored andcommitted
pwm: bcm2835: Allow PWM driver to be used in atomic context
clk_get_rate() may do a mutex lock. Fetch the clock rate once, and prevent rate changes using clk_rate_exclusive_get(). Signed-off-by: Sean Young <[email protected]> Reviewed-by: Florian Fainelli <[email protected]> Signed-off-by: Thierry Reding <[email protected]>
1 parent 7170d3b commit fcc7607

File tree

1 file changed

+29
-9
lines changed

1 file changed

+29
-9
lines changed

drivers/pwm/pwm-bcm2835.c

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ struct bcm2835_pwm {
2828
struct device *dev;
2929
void __iomem *base;
3030
struct clk *clk;
31+
unsigned long rate;
3132
};
3233

3334
static inline struct bcm2835_pwm *to_bcm2835_pwm(struct pwm_chip *chip)
@@ -63,17 +64,11 @@ static int bcm2835_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm,
6364
{
6465

6566
struct bcm2835_pwm *pc = to_bcm2835_pwm(chip);
66-
unsigned long rate = clk_get_rate(pc->clk);
6767
unsigned long long period_cycles;
6868
u64 max_period;
6969

7070
u32 val;
7171

72-
if (!rate) {
73-
dev_err(pc->dev, "failed to get clock rate\n");
74-
return -EINVAL;
75-
}
76-
7772
/*
7873
* period_cycles must be a 32 bit value, so period * rate / NSEC_PER_SEC
7974
* must be <= U32_MAX. As U32_MAX * NSEC_PER_SEC < U64_MAX the
@@ -88,13 +83,13 @@ static int bcm2835_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm,
8883
* <=> period < ((U32_MAX * NSEC_PER_SEC + NSEC_PER_SEC/2) / rate
8984
* <=> period <= ceil((U32_MAX * NSEC_PER_SEC + NSEC_PER_SEC/2) / rate) - 1
9085
*/
91-
max_period = DIV_ROUND_UP_ULL((u64)U32_MAX * NSEC_PER_SEC + NSEC_PER_SEC / 2, rate) - 1;
86+
max_period = DIV_ROUND_UP_ULL((u64)U32_MAX * NSEC_PER_SEC + NSEC_PER_SEC / 2, pc->rate) - 1;
9287

9388
if (state->period > max_period)
9489
return -EINVAL;
9590

9691
/* set period */
97-
period_cycles = DIV_ROUND_CLOSEST_ULL(state->period * rate, NSEC_PER_SEC);
92+
period_cycles = DIV_ROUND_CLOSEST_ULL(state->period * pc->rate, NSEC_PER_SEC);
9893

9994
/* don't accept a period that is too small */
10095
if (period_cycles < PERIOD_MIN)
@@ -103,7 +98,7 @@ static int bcm2835_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm,
10398
writel(period_cycles, pc->base + PERIOD(pwm->hwpwm));
10499

105100
/* set duty cycle */
106-
val = DIV_ROUND_CLOSEST_ULL(state->duty_cycle * rate, NSEC_PER_SEC);
101+
val = DIV_ROUND_CLOSEST_ULL(state->duty_cycle * pc->rate, NSEC_PER_SEC);
107102
writel(val, pc->base + DUTY(pwm->hwpwm));
108103

109104
/* set polarity */
@@ -131,6 +126,13 @@ static const struct pwm_ops bcm2835_pwm_ops = {
131126
.apply = bcm2835_pwm_apply,
132127
};
133128

129+
static void devm_clk_rate_exclusive_put(void *data)
130+
{
131+
struct clk *clk = data;
132+
133+
clk_rate_exclusive_put(clk);
134+
}
135+
134136
static int bcm2835_pwm_probe(struct platform_device *pdev)
135137
{
136138
struct bcm2835_pwm *pc;
@@ -151,8 +153,26 @@ static int bcm2835_pwm_probe(struct platform_device *pdev)
151153
return dev_err_probe(&pdev->dev, PTR_ERR(pc->clk),
152154
"clock not found\n");
153155

156+
ret = clk_rate_exclusive_get(pc->clk);
157+
if (ret)
158+
return dev_err_probe(&pdev->dev, ret,
159+
"fail to get exclusive rate\n");
160+
161+
ret = devm_add_action_or_reset(&pdev->dev, devm_clk_rate_exclusive_put,
162+
pc->clk);
163+
if (ret) {
164+
clk_rate_exclusive_put(pc->clk);
165+
return ret;
166+
}
167+
168+
pc->rate = clk_get_rate(pc->clk);
169+
if (!pc->rate)
170+
return dev_err_probe(&pdev->dev, -EINVAL,
171+
"failed to get clock rate\n");
172+
154173
pc->chip.dev = &pdev->dev;
155174
pc->chip.ops = &bcm2835_pwm_ops;
175+
pc->chip.atomic = true;
156176
pc->chip.npwm = 2;
157177

158178
platform_set_drvdata(pdev, pc);

0 commit comments

Comments
 (0)