aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/pwm
diff options
context:
space:
mode:
authorThomas Hebb <tommyhebb@gmail.com>2018-06-06 13:42:10 -0400
committerThierry Reding <thierry.reding@gmail.com>2018-07-09 12:57:03 -0400
commit4de445cb431eb4b473936396436d819a0724735c (patch)
tree9a3838763cdb9ba785a1b1e0af8da57d47be73fc /drivers/pwm
parentce397d215ccd07b8ae3f71db689aedb85d56ab40 (diff)
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 <tommyhebb@gmail.com> Signed-off-by: Thierry Reding <thierry.reding@gmail.com>
Diffstat (limited to 'drivers/pwm')
-rw-r--r--drivers/pwm/pwm-berlin.c45
1 files changed, 25 insertions, 20 deletions
diff --git a/drivers/pwm/pwm-berlin.c b/drivers/pwm/pwm-berlin.c
index 771859aca4be..7c8d6a168ceb 100644
--- a/drivers/pwm/pwm-berlin.c
+++ b/drivers/pwm/pwm-berlin.c
@@ -21,8 +21,18 @@
21#define BERLIN_PWM_EN 0x0 21#define BERLIN_PWM_EN 0x0
22#define BERLIN_PWM_ENABLE BIT(0) 22#define BERLIN_PWM_ENABLE BIT(0)
23#define BERLIN_PWM_CONTROL 0x4 23#define BERLIN_PWM_CONTROL 0x4
24#define BERLIN_PWM_PRESCALE_MASK 0x7 24/*
25#define BERLIN_PWM_PRESCALE_MAX 4096 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
26#define BERLIN_PWM_INVERT_POLARITY BIT(3) 36#define BERLIN_PWM_INVERT_POLARITY BIT(3)
27#define BERLIN_PWM_DUTY 0x8 37#define BERLIN_PWM_DUTY 0x8
28#define BERLIN_PWM_TCNT 0xc 38#define BERLIN_PWM_TCNT 0xc
@@ -46,10 +56,6 @@ static inline struct berlin_pwm_chip *to_berlin_pwm_chip(struct pwm_chip *chip)
46 return container_of(chip, struct berlin_pwm_chip, chip); 56 return container_of(chip, struct berlin_pwm_chip, chip);
47} 57}
48 58
49static const u32 prescaler_table[] = {
50 1, 4, 8, 16, 64, 256, 1024, 4096
51};
52
53static inline u32 berlin_pwm_readl(struct berlin_pwm_chip *chip, 59static inline u32 berlin_pwm_readl(struct berlin_pwm_chip *chip,
54 unsigned int channel, unsigned long offset) 60 unsigned int channel, unsigned long offset)
55{ 61{
@@ -86,33 +92,32 @@ static int berlin_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm_dev,
86 int duty_ns, int period_ns) 92 int duty_ns, int period_ns)
87{ 93{
88 struct berlin_pwm_chip *pwm = to_berlin_pwm_chip(chip); 94 struct berlin_pwm_chip *pwm = to_berlin_pwm_chip(chip);
89 unsigned int prescale; 95 bool prescale_4096 = false;
90 u32 value, duty, period; 96 u32 value, duty, period;
91 u64 cycles, tmp; 97 u64 cycles;
92 98
93 cycles = clk_get_rate(pwm->clk); 99 cycles = clk_get_rate(pwm->clk);
94 cycles *= period_ns; 100 cycles *= period_ns;
95 do_div(cycles, NSEC_PER_SEC); 101 do_div(cycles, NSEC_PER_SEC);
96 102
97 for (prescale = 0; prescale < ARRAY_SIZE(prescaler_table); prescale++) { 103 if (cycles > BERLIN_PWM_MAX_TCNT) {
98 tmp = cycles; 104 prescale_4096 = true;
99 do_div(tmp, prescaler_table[prescale]); 105 cycles >>= 12; // Prescaled by 4096
100 106
101 if (tmp <= BERLIN_PWM_MAX_TCNT) 107 if (cycles > BERLIN_PWM_MAX_TCNT)
102 break; 108 return -ERANGE;
103 } 109 }
104 110
105 if (tmp > BERLIN_PWM_MAX_TCNT) 111 period = cycles;
106 return -ERANGE; 112 cycles *= duty_ns;
107
108 period = tmp;
109 cycles = tmp * duty_ns;
110 do_div(cycles, period_ns); 113 do_div(cycles, period_ns);
111 duty = cycles; 114 duty = cycles;
112 115
113 value = berlin_pwm_readl(pwm, pwm_dev->hwpwm, BERLIN_PWM_CONTROL); 116 value = berlin_pwm_readl(pwm, pwm_dev->hwpwm, BERLIN_PWM_CONTROL);
114 value &= ~BERLIN_PWM_PRESCALE_MASK; 117 if (prescale_4096)
115 value |= prescale; 118 value |= BERLIN_PWM_PRESCALE_4096;
119 else
120 value &= ~BERLIN_PWM_PRESCALE_4096;
116 berlin_pwm_writel(pwm, pwm_dev->hwpwm, value, BERLIN_PWM_CONTROL); 121 berlin_pwm_writel(pwm, pwm_dev->hwpwm, value, BERLIN_PWM_CONTROL);
117 122
118 berlin_pwm_writel(pwm, pwm_dev->hwpwm, duty, BERLIN_PWM_DUTY); 123 berlin_pwm_writel(pwm, pwm_dev->hwpwm, duty, BERLIN_PWM_DUTY);