Skip to content

Commit 4de445c

Browse files
tchebbthierryreding
authored andcommitted
pwm: berlin: Don't use broken prescaler values
The Berlin PWM driver is currently broken on at least BG2CD. The symptoms manifest as a very non-linear and erratic mapping from the duty cycle configured in software to the duty cycle produced by hardware. The cause of the bug is software's configuration of the prescaler, and in particular its usage of the six prescaler values between the minimum value of 1 and the maximum value of 4096. As it turns out, these six values do not actually slow down the PWM clock; rather, they emulate slowing down the clock by internally multiplying the value of TCNT. This would be a fine trick, if not for the fact that the internal, scaled TCNT value has no extra bits beyond the 16 already exposed to software in the register. What this means is that, for a prescaler of 4, the software must ensure that the top two bits of TCNT are not set, because hardware will chop them off; for a prescaler of 8, the top three bits must not be set, and so forth. Software does not currently ensure this, resulting in a TCNT several orders of magnitude lower than intended any time one of those six prescalers are selected. Because hardware chops off the high bits in its internal shift, the middle six prescalers don't actually allow *anything* that the first doesn't. In fact, they are strictly worse than the first, since the internal shift of TCNT prevents software from setting the low bits, decreasing the resolution, without providing any extra high bits. By skipping the useless prescalers entirely, this patch both fixes the driver's behavior and increases its performance (since, when the 4096 prescaler is selected, it now does only a single shift rather than the seven successive divisions it did before). Tested on BG2CD. Signed-off-by: Thomas Hebb <[email protected]> Signed-off-by: Thierry Reding <[email protected]>
1 parent ce397d2 commit 4de445c

File tree

1 file changed

+25
-20
lines changed

1 file changed

+25
-20
lines changed

drivers/pwm/pwm-berlin.c

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,18 @@
2121
#define BERLIN_PWM_EN 0x0
2222
#define BERLIN_PWM_ENABLE BIT(0)
2323
#define BERLIN_PWM_CONTROL 0x4
24-
#define BERLIN_PWM_PRESCALE_MASK 0x7
25-
#define BERLIN_PWM_PRESCALE_MAX 4096
24+
/*
25+
* The prescaler claims to support 8 different moduli, configured using the
26+
* low three bits of PWM_CONTROL. (Sequentially, they are 1, 4, 8, 16, 64,
27+
* 256, 1024, and 4096.) However, the moduli from 4 to 1024 appear to be
28+
* implemented by internally shifting TCNT left without adding additional
29+
* bits. So, the max TCNT that actually works for a modulus of 4 is 0x3fff;
30+
* for 8, 0x1fff; and so on. This means that those moduli are entirely
31+
* useless, as we could just do the shift ourselves. The 4096 modulus is
32+
* implemented with a real prescaler, so we do use that, but we treat it
33+
* as a flag instead of pretending the modulus is actually configurable.
34+
*/
35+
#define BERLIN_PWM_PRESCALE_4096 0x7
2636
#define BERLIN_PWM_INVERT_POLARITY BIT(3)
2737
#define BERLIN_PWM_DUTY 0x8
2838
#define BERLIN_PWM_TCNT 0xc
@@ -46,10 +56,6 @@ static inline struct berlin_pwm_chip *to_berlin_pwm_chip(struct pwm_chip *chip)
4656
return container_of(chip, struct berlin_pwm_chip, chip);
4757
}
4858

49-
static const u32 prescaler_table[] = {
50-
1, 4, 8, 16, 64, 256, 1024, 4096
51-
};
52-
5359
static inline u32 berlin_pwm_readl(struct berlin_pwm_chip *chip,
5460
unsigned int channel, unsigned long offset)
5561
{
@@ -86,33 +92,32 @@ static int berlin_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm_dev,
8692
int duty_ns, int period_ns)
8793
{
8894
struct berlin_pwm_chip *pwm = to_berlin_pwm_chip(chip);
89-
unsigned int prescale;
95+
bool prescale_4096 = false;
9096
u32 value, duty, period;
91-
u64 cycles, tmp;
97+
u64 cycles;
9298

9399
cycles = clk_get_rate(pwm->clk);
94100
cycles *= period_ns;
95101
do_div(cycles, NSEC_PER_SEC);
96102

97-
for (prescale = 0; prescale < ARRAY_SIZE(prescaler_table); prescale++) {
98-
tmp = cycles;
99-
do_div(tmp, prescaler_table[prescale]);
103+
if (cycles > BERLIN_PWM_MAX_TCNT) {
104+
prescale_4096 = true;
105+
cycles >>= 12; // Prescaled by 4096
100106

101-
if (tmp <= BERLIN_PWM_MAX_TCNT)
102-
break;
107+
if (cycles > BERLIN_PWM_MAX_TCNT)
108+
return -ERANGE;
103109
}
104110

105-
if (tmp > BERLIN_PWM_MAX_TCNT)
106-
return -ERANGE;
107-
108-
period = tmp;
109-
cycles = tmp * duty_ns;
111+
period = cycles;
112+
cycles *= duty_ns;
110113
do_div(cycles, period_ns);
111114
duty = cycles;
112115

113116
value = berlin_pwm_readl(pwm, pwm_dev->hwpwm, BERLIN_PWM_CONTROL);
114-
value &= ~BERLIN_PWM_PRESCALE_MASK;
115-
value |= prescale;
117+
if (prescale_4096)
118+
value |= BERLIN_PWM_PRESCALE_4096;
119+
else
120+
value &= ~BERLIN_PWM_PRESCALE_4096;
116121
berlin_pwm_writel(pwm, pwm_dev->hwpwm, value, BERLIN_PWM_CONTROL);
117122

118123
berlin_pwm_writel(pwm, pwm_dev->hwpwm, duty, BERLIN_PWM_DUTY);

0 commit comments

Comments
 (0)