Skip to content

Commit f4a8e31

Browse files
Uwe Kleine-Königthierryreding
authored andcommitted
pwm: ep93xx: Ensure configuring period and duty_cycle isn't wrongly skipped
As the last call to ep93xx_pwm_apply() might have exited early if state->enabled was false, the values for period and duty_cycle stored in pwm->state might not have been written to hardware and it must be ensured that they are configured before enabling the PWM. Fixes: 6d45374 ("pwm: ep93xx: Implement .apply callback") Signed-off-by: Uwe Kleine-König <[email protected]> Signed-off-by: Thierry Reding <[email protected]>
1 parent 7d6d4aa commit f4a8e31

File tree

1 file changed

+40
-45
lines changed

1 file changed

+40
-45
lines changed

drivers/pwm/pwm-ep93xx.c

Lines changed: 40 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,11 @@ static int ep93xx_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm,
6464
int ret;
6565
struct ep93xx_pwm *ep93xx_pwm = to_ep93xx_pwm(chip);
6666
bool enabled = state->enabled;
67+
void __iomem *base = ep93xx_pwm->base;
68+
unsigned long long c;
69+
unsigned long period_cycles;
70+
unsigned long duty_cycles;
71+
unsigned long term;
6772

6873
if (state->polarity != pwm->state.polarity) {
6974
if (enabled) {
@@ -97,57 +102,47 @@ static int ep93xx_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm,
97102
return 0;
98103
}
99104

100-
if (state->period != pwm->state.period ||
101-
state->duty_cycle != pwm->state.duty_cycle) {
102-
struct ep93xx_pwm *ep93xx_pwm = to_ep93xx_pwm(chip);
103-
void __iomem *base = ep93xx_pwm->base;
104-
unsigned long long c;
105-
unsigned long period_cycles;
106-
unsigned long duty_cycles;
107-
unsigned long term;
105+
/*
106+
* The clock needs to be enabled to access the PWM registers.
107+
* Configuration can be changed at any time.
108+
*/
109+
if (!pwm_is_enabled(pwm)) {
110+
ret = clk_prepare_enable(ep93xx_pwm->clk);
111+
if (ret)
112+
return ret;
113+
}
108114

109-
/*
110-
* The clock needs to be enabled to access the PWM registers.
111-
* Configuration can be changed at any time.
112-
*/
113-
if (!pwm_is_enabled(pwm)) {
114-
ret = clk_prepare_enable(ep93xx_pwm->clk);
115-
if (ret)
116-
return ret;
117-
}
115+
c = clk_get_rate(ep93xx_pwm->clk);
116+
c *= state->period;
117+
do_div(c, 1000000000);
118+
period_cycles = c;
119+
120+
c = period_cycles;
121+
c *= state->duty_cycle;
122+
do_div(c, state->period);
123+
duty_cycles = c;
118124

119-
c = clk_get_rate(ep93xx_pwm->clk);
120-
c *= state->period;
121-
do_div(c, 1000000000);
122-
period_cycles = c;
123-
124-
c = period_cycles;
125-
c *= state->duty_cycle;
126-
do_div(c, state->period);
127-
duty_cycles = c;
128-
129-
if (period_cycles < 0x10000 && duty_cycles < 0x10000) {
130-
term = readw(base + EP93XX_PWMx_TERM_COUNT);
131-
132-
/* Order is important if PWM is running */
133-
if (period_cycles > term) {
134-
writew(period_cycles, base + EP93XX_PWMx_TERM_COUNT);
135-
writew(duty_cycles, base + EP93XX_PWMx_DUTY_CYCLE);
136-
} else {
137-
writew(duty_cycles, base + EP93XX_PWMx_DUTY_CYCLE);
138-
writew(period_cycles, base + EP93XX_PWMx_TERM_COUNT);
139-
}
140-
ret = 0;
125+
if (period_cycles < 0x10000 && duty_cycles < 0x10000) {
126+
term = readw(base + EP93XX_PWMx_TERM_COUNT);
127+
128+
/* Order is important if PWM is running */
129+
if (period_cycles > term) {
130+
writew(period_cycles, base + EP93XX_PWMx_TERM_COUNT);
131+
writew(duty_cycles, base + EP93XX_PWMx_DUTY_CYCLE);
141132
} else {
142-
ret = -EINVAL;
133+
writew(duty_cycles, base + EP93XX_PWMx_DUTY_CYCLE);
134+
writew(period_cycles, base + EP93XX_PWMx_TERM_COUNT);
143135
}
136+
ret = 0;
137+
} else {
138+
ret = -EINVAL;
139+
}
144140

145-
if (!pwm_is_enabled(pwm))
146-
clk_disable_unprepare(ep93xx_pwm->clk);
141+
if (!pwm_is_enabled(pwm))
142+
clk_disable_unprepare(ep93xx_pwm->clk);
147143

148-
if (ret)
149-
return ret;
150-
}
144+
if (ret)
145+
return ret;
151146

152147
if (!enabled) {
153148
ret = clk_prepare_enable(ep93xx_pwm->clk);

0 commit comments

Comments
 (0)