aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/pwm
diff options
context:
space:
mode:
authorDavid Rivshin <drivshin@allworx.com>2016-01-29 23:26:51 -0500
committerThierry Reding <thierry.reding@gmail.com>2016-03-23 12:11:45 -0400
commitf8caa792261c0edded20eba2b8fcc899a1b91819 (patch)
tree7920bbf9a010fe99d53ac154f7fc20253127f3f3 /drivers/pwm
parentc5857e3f94ab2719dfac649a146cb5dd6f21fcf3 (diff)
pwm: omap-dmtimer: Fix inaccurate period and duty cycle calculations
Fix the calculation of load_value and match_value. Currently they are slightly too low, which produces a noticeably wrong PWM rate with sufficiently short periods (i.e. when 1/period approaches clk_rate/2). Example: clk_rate=32768Hz, period=122070ns, duty_cycle=61035ns (8192Hz/50% PWM) Correct values: load = 0xfffffffc, match = 0xfffffffd Current values: load = 0xfffffffa, match = 0xfffffffc effective PWM: period=183105ns, duty_cycle=91553ns (5461Hz/50% PWM) Fixes: 6604c6556db9 ("pwm: Add PWM driver for OMAP using dual-mode timers") Signed-off-by: David Rivshin <drivshin@allworx.com> Acked-by: Neil Armstrong <narmstrong@baylibre.com> Tested-by: Adam Ford <aford173@gmail.com> Signed-off-by: Thierry Reding <thierry.reding@gmail.com>
Diffstat (limited to 'drivers/pwm')
-rw-r--r--drivers/pwm/pwm-omap-dmtimer.c27
1 files changed, 20 insertions, 7 deletions
diff --git a/drivers/pwm/pwm-omap-dmtimer.c b/drivers/pwm/pwm-omap-dmtimer.c
index 826634ec0d5c..0083e75d950f 100644
--- a/drivers/pwm/pwm-omap-dmtimer.c
+++ b/drivers/pwm/pwm-omap-dmtimer.c
@@ -31,6 +31,7 @@
31#include <linux/time.h> 31#include <linux/time.h>
32 32
33#define DM_TIMER_LOAD_MIN 0xfffffffe 33#define DM_TIMER_LOAD_MIN 0xfffffffe
34#define DM_TIMER_MAX 0xffffffff
34 35
35struct pwm_omap_dmtimer_chip { 36struct pwm_omap_dmtimer_chip {
36 struct pwm_chip chip; 37 struct pwm_chip chip;
@@ -46,13 +47,13 @@ to_pwm_omap_dmtimer_chip(struct pwm_chip *chip)
46 return container_of(chip, struct pwm_omap_dmtimer_chip, chip); 47 return container_of(chip, struct pwm_omap_dmtimer_chip, chip);
47} 48}
48 49
49static int pwm_omap_dmtimer_calc_value(unsigned long clk_rate, int ns) 50static u32 pwm_omap_dmtimer_get_clock_cycles(unsigned long clk_rate, int ns)
50{ 51{
51 u64 c = (u64)clk_rate * ns; 52 u64 c = (u64)clk_rate * ns;
52 53
53 do_div(c, NSEC_PER_SEC); 54 do_div(c, NSEC_PER_SEC);
54 55
55 return DM_TIMER_LOAD_MIN - c; 56 return c;
56} 57}
57 58
58static void pwm_omap_dmtimer_start(struct pwm_omap_dmtimer_chip *omap) 59static void pwm_omap_dmtimer_start(struct pwm_omap_dmtimer_chip *omap)
@@ -99,7 +100,8 @@ static int pwm_omap_dmtimer_config(struct pwm_chip *chip,
99 int duty_ns, int period_ns) 100 int duty_ns, int period_ns)
100{ 101{
101 struct pwm_omap_dmtimer_chip *omap = to_pwm_omap_dmtimer_chip(chip); 102 struct pwm_omap_dmtimer_chip *omap = to_pwm_omap_dmtimer_chip(chip);
102 int load_value, match_value; 103 u32 period_cycles, duty_cycles;
104 u32 load_value, match_value;
103 struct clk *fclk; 105 struct clk *fclk;
104 unsigned long clk_rate; 106 unsigned long clk_rate;
105 bool timer_active; 107 bool timer_active;
@@ -133,11 +135,22 @@ static int pwm_omap_dmtimer_config(struct pwm_chip *chip,
133 /* 135 /*
134 * Calculate the appropriate load and match values based on the 136 * Calculate the appropriate load and match values based on the
135 * specified period and duty cycle. The load value determines the 137 * specified period and duty cycle. The load value determines the
136 * cycle time and the match value determines the duty cycle. 138 * period time and the match value determines the duty time.
139 *
140 * The period lasts for (DM_TIMER_MAX-load_value+1) clock cycles.
141 * Similarly, the active time lasts (match_value-load_value+1) cycles.
142 * The non-active time is the remainder: (DM_TIMER_MAX-match_value)
143 * clock cycles.
144 *
145 * References:
146 * OMAP4430/60/70 TRM sections 22.2.4.10 and 22.2.4.11
147 * AM335x Sitara TRM sections 20.1.3.5 and 20.1.3.6
137 */ 148 */
138 load_value = pwm_omap_dmtimer_calc_value(clk_rate, period_ns); 149 period_cycles = pwm_omap_dmtimer_get_clock_cycles(clk_rate, period_ns);
139 match_value = pwm_omap_dmtimer_calc_value(clk_rate, 150 duty_cycles = pwm_omap_dmtimer_get_clock_cycles(clk_rate, duty_ns);
140 period_ns - duty_ns); 151
152 load_value = (DM_TIMER_MAX - period_cycles) + 1;
153 match_value = load_value + duty_cycles - 1;
141 154
142 /* 155 /*
143 * We MUST stop the associated dual-mode timer before attempting to 156 * We MUST stop the associated dual-mode timer before attempting to