pwm: omap-dmtimer: Add sanity checking for load and match values
[deliverable/linux.git] / drivers / pwm / pwm-omap-dmtimer.c
CommitLineData
6604c655
NA
1/*
2 * Copyright (c) 2015 Neil Armstrong <narmstrong@baylibre.com>
3 * Copyright (c) 2014 Joachim Eastwood <manabian@gmail.com>
4 * Copyright (c) 2012 NeilBrown <neilb@suse.de>
5 * Heavily based on earlier code which is:
6 * Copyright (c) 2010 Grant Erickson <marathon96@gmail.com>
7 *
8 * Also based on pwm-samsung.c
9 *
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License
12 * version 2 as published by the Free Software Foundation.
13 *
14 * Description:
15 * This file is the core OMAP support for the generic, Linux
16 * PWM driver / controller, using the OMAP's dual-mode timers.
17 */
18
19#include <linux/clk.h>
20#include <linux/err.h>
21#include <linux/kernel.h>
22#include <linux/module.h>
23#include <linux/mutex.h>
24#include <linux/of.h>
25#include <linux/of_platform.h>
26#include <linux/platform_data/pwm_omap_dmtimer.h>
27#include <linux/platform_device.h>
28#include <linux/pm_runtime.h>
29#include <linux/pwm.h>
30#include <linux/slab.h>
31#include <linux/time.h>
32
33#define DM_TIMER_LOAD_MIN 0xfffffffe
f8caa792 34#define DM_TIMER_MAX 0xffffffff
6604c655
NA
35
36struct pwm_omap_dmtimer_chip {
37 struct pwm_chip chip;
38 struct mutex mutex;
39 pwm_omap_dmtimer *dm_timer;
40 struct pwm_omap_dmtimer_pdata *pdata;
41 struct platform_device *dm_timer_pdev;
42};
43
44static inline struct pwm_omap_dmtimer_chip *
45to_pwm_omap_dmtimer_chip(struct pwm_chip *chip)
46{
47 return container_of(chip, struct pwm_omap_dmtimer_chip, chip);
48}
49
f8caa792 50static u32 pwm_omap_dmtimer_get_clock_cycles(unsigned long clk_rate, int ns)
6604c655
NA
51{
52 u64 c = (u64)clk_rate * ns;
53
54 do_div(c, NSEC_PER_SEC);
55
f8caa792 56 return c;
6604c655
NA
57}
58
59static void pwm_omap_dmtimer_start(struct pwm_omap_dmtimer_chip *omap)
60{
61 /*
62 * According to OMAP 4 TRM section 22.2.4.10 the counter should be
63 * started at 0xFFFFFFFE when overflow and match is used to ensure
64 * that the PWM line is toggled on the first event.
65 *
66 * Note that omap_dm_timer_enable/disable is for register access and
67 * not the timer counter itself.
68 */
69 omap->pdata->enable(omap->dm_timer);
70 omap->pdata->write_counter(omap->dm_timer, DM_TIMER_LOAD_MIN);
71 omap->pdata->disable(omap->dm_timer);
72
73 omap->pdata->start(omap->dm_timer);
74}
75
76static int pwm_omap_dmtimer_enable(struct pwm_chip *chip,
77 struct pwm_device *pwm)
78{
79 struct pwm_omap_dmtimer_chip *omap = to_pwm_omap_dmtimer_chip(chip);
80
81 mutex_lock(&omap->mutex);
82 pwm_omap_dmtimer_start(omap);
83 mutex_unlock(&omap->mutex);
84
85 return 0;
86}
87
88static void pwm_omap_dmtimer_disable(struct pwm_chip *chip,
89 struct pwm_device *pwm)
90{
91 struct pwm_omap_dmtimer_chip *omap = to_pwm_omap_dmtimer_chip(chip);
92
93 mutex_lock(&omap->mutex);
94 omap->pdata->stop(omap->dm_timer);
95 mutex_unlock(&omap->mutex);
96}
97
98static int pwm_omap_dmtimer_config(struct pwm_chip *chip,
99 struct pwm_device *pwm,
100 int duty_ns, int period_ns)
101{
102 struct pwm_omap_dmtimer_chip *omap = to_pwm_omap_dmtimer_chip(chip);
f8caa792
DR
103 u32 period_cycles, duty_cycles;
104 u32 load_value, match_value;
6604c655
NA
105 struct clk *fclk;
106 unsigned long clk_rate;
107 bool timer_active;
108
109 dev_dbg(chip->dev, "duty cycle: %d, period %d\n", duty_ns, period_ns);
110
111 mutex_lock(&omap->mutex);
112 if (duty_ns == pwm_get_duty_cycle(pwm) &&
113 period_ns == pwm_get_period(pwm)) {
114 /* No change - don't cause any transients. */
115 mutex_unlock(&omap->mutex);
116 return 0;
117 }
118
119 fclk = omap->pdata->get_fclk(omap->dm_timer);
120 if (!fclk) {
121 dev_err(chip->dev, "invalid pmtimer fclk\n");
cd378881 122 goto err_einval;
6604c655
NA
123 }
124
125 clk_rate = clk_get_rate(fclk);
126 if (!clk_rate) {
127 dev_err(chip->dev, "invalid pmtimer fclk rate\n");
cd378881 128 goto err_einval;
6604c655
NA
129 }
130
131 dev_dbg(chip->dev, "clk rate: %luHz\n", clk_rate);
132
133 /*
134 * Calculate the appropriate load and match values based on the
135 * specified period and duty cycle. The load value determines the
f8caa792
DR
136 * period time and the match value determines the duty time.
137 *
138 * The period lasts for (DM_TIMER_MAX-load_value+1) clock cycles.
139 * Similarly, the active time lasts (match_value-load_value+1) cycles.
140 * The non-active time is the remainder: (DM_TIMER_MAX-match_value)
141 * clock cycles.
142 *
cd378881
DR
143 * NOTE: It is required that: load_value <= match_value < DM_TIMER_MAX
144 *
f8caa792
DR
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
6604c655 148 */
f8caa792
DR
149 period_cycles = pwm_omap_dmtimer_get_clock_cycles(clk_rate, period_ns);
150 duty_cycles = pwm_omap_dmtimer_get_clock_cycles(clk_rate, duty_ns);
151
cd378881
DR
152 if (period_cycles < 2) {
153 dev_info(chip->dev,
154 "period %d ns too short for clock rate %lu Hz\n",
155 period_ns, clk_rate);
156 goto err_einval;
157 }
158
159 if (duty_cycles < 1) {
160 dev_dbg(chip->dev,
161 "duty cycle %d ns is too short for clock rate %lu Hz\n",
162 duty_ns, clk_rate);
163 dev_dbg(chip->dev, "using minimum of 1 clock cycle\n");
164 duty_cycles = 1;
165 } else if (duty_cycles >= period_cycles) {
166 dev_dbg(chip->dev,
167 "duty cycle %d ns is too long for period %d ns at clock rate %lu Hz\n",
168 duty_ns, period_ns, clk_rate);
169 dev_dbg(chip->dev, "using maximum of 1 clock cycle less than period\n");
170 duty_cycles = period_cycles - 1;
171 }
172
f8caa792
DR
173 load_value = (DM_TIMER_MAX - period_cycles) + 1;
174 match_value = load_value + duty_cycles - 1;
6604c655
NA
175
176 /*
177 * We MUST stop the associated dual-mode timer before attempting to
178 * write its registers, but calls to omap_dm_timer_start/stop must
179 * be balanced so check if timer is active before calling timer_stop.
180 */
181 timer_active = pm_runtime_active(&omap->dm_timer_pdev->dev);
182 if (timer_active)
183 omap->pdata->stop(omap->dm_timer);
184
185 omap->pdata->set_load(omap->dm_timer, true, load_value);
186 omap->pdata->set_match(omap->dm_timer, true, match_value);
187
188 dev_dbg(chip->dev, "load value: %#08x (%d), match value: %#08x (%d)\n",
189 load_value, load_value, match_value, match_value);
190
191 omap->pdata->set_pwm(omap->dm_timer,
192 pwm->polarity == PWM_POLARITY_INVERSED,
193 true,
194 PWM_OMAP_DMTIMER_TRIGGER_OVERFLOW_AND_COMPARE);
195
196 /* If config was called while timer was running it must be reenabled. */
197 if (timer_active)
198 pwm_omap_dmtimer_start(omap);
199
200 mutex_unlock(&omap->mutex);
201
202 return 0;
cd378881
DR
203
204err_einval:
205 mutex_unlock(&omap->mutex);
206
207 return -EINVAL;
6604c655
NA
208}
209
210static int pwm_omap_dmtimer_set_polarity(struct pwm_chip *chip,
211 struct pwm_device *pwm,
212 enum pwm_polarity polarity)
213{
214 struct pwm_omap_dmtimer_chip *omap = to_pwm_omap_dmtimer_chip(chip);
215
216 /*
217 * PWM core will not call set_polarity while PWM is enabled so it's
218 * safe to reconfigure the timer here without stopping it first.
219 */
220 mutex_lock(&omap->mutex);
221 omap->pdata->set_pwm(omap->dm_timer,
222 polarity == PWM_POLARITY_INVERSED,
223 true,
224 PWM_OMAP_DMTIMER_TRIGGER_OVERFLOW_AND_COMPARE);
225 mutex_unlock(&omap->mutex);
226
227 return 0;
228}
229
230static const struct pwm_ops pwm_omap_dmtimer_ops = {
231 .enable = pwm_omap_dmtimer_enable,
232 .disable = pwm_omap_dmtimer_disable,
233 .config = pwm_omap_dmtimer_config,
234 .set_polarity = pwm_omap_dmtimer_set_polarity,
235 .owner = THIS_MODULE,
236};
237
238static int pwm_omap_dmtimer_probe(struct platform_device *pdev)
239{
240 struct device_node *np = pdev->dev.of_node;
241 struct device_node *timer;
242 struct pwm_omap_dmtimer_chip *omap;
243 struct pwm_omap_dmtimer_pdata *pdata;
244 pwm_omap_dmtimer *dm_timer;
245 u32 prescaler;
246 int status;
247
248 pdata = dev_get_platdata(&pdev->dev);
249 if (!pdata) {
250 dev_err(&pdev->dev, "Missing dmtimer platform data\n");
251 return -EINVAL;
252 }
253
254 if (!pdata->request_by_node ||
255 !pdata->free ||
256 !pdata->enable ||
257 !pdata->disable ||
258 !pdata->get_fclk ||
259 !pdata->start ||
260 !pdata->stop ||
261 !pdata->set_load ||
262 !pdata->set_match ||
263 !pdata->set_pwm ||
264 !pdata->set_prescaler ||
265 !pdata->write_counter) {
266 dev_err(&pdev->dev, "Incomplete dmtimer pdata structure\n");
267 return -EINVAL;
268 }
269
270 timer = of_parse_phandle(np, "ti,timers", 0);
271 if (!timer)
272 return -ENODEV;
273
274 if (!of_get_property(timer, "ti,timer-pwm", NULL)) {
275 dev_err(&pdev->dev, "Missing ti,timer-pwm capability\n");
276 return -ENODEV;
277 }
278
279 dm_timer = pdata->request_by_node(timer);
280 if (!dm_timer)
281 return -EPROBE_DEFER;
282
283 omap = devm_kzalloc(&pdev->dev, sizeof(*omap), GFP_KERNEL);
284 if (!omap) {
07472640 285 pdata->free(dm_timer);
6604c655
NA
286 return -ENOMEM;
287 }
288
289 omap->pdata = pdata;
290 omap->dm_timer = dm_timer;
291
292 omap->dm_timer_pdev = of_find_device_by_node(timer);
293 if (!omap->dm_timer_pdev) {
294 dev_err(&pdev->dev, "Unable to find timer pdev\n");
295 omap->pdata->free(dm_timer);
296 return -EINVAL;
297 }
298
299 /*
300 * Ensure that the timer is stopped before we allow PWM core to call
301 * pwm_enable.
302 */
303 if (pm_runtime_active(&omap->dm_timer_pdev->dev))
304 omap->pdata->stop(omap->dm_timer);
305
306 /* setup dmtimer prescaler */
307 if (!of_property_read_u32(pdev->dev.of_node, "ti,prescaler",
308 &prescaler))
309 omap->pdata->set_prescaler(omap->dm_timer, prescaler);
310
311 omap->chip.dev = &pdev->dev;
312 omap->chip.ops = &pwm_omap_dmtimer_ops;
313 omap->chip.base = -1;
314 omap->chip.npwm = 1;
315 omap->chip.of_xlate = of_pwm_xlate_with_flags;
316 omap->chip.of_pwm_n_cells = 3;
317
318 mutex_init(&omap->mutex);
319
320 status = pwmchip_add(&omap->chip);
321 if (status < 0) {
322 dev_err(&pdev->dev, "failed to register PWM\n");
323 omap->pdata->free(omap->dm_timer);
324 return status;
325 }
326
327 platform_set_drvdata(pdev, omap);
328
329 return 0;
330}
331
332static int pwm_omap_dmtimer_remove(struct platform_device *pdev)
333{
334 struct pwm_omap_dmtimer_chip *omap = platform_get_drvdata(pdev);
335
336 if (pm_runtime_active(&omap->dm_timer_pdev->dev))
337 omap->pdata->stop(omap->dm_timer);
338
339 omap->pdata->free(omap->dm_timer);
340
341 mutex_destroy(&omap->mutex);
342
343 return pwmchip_remove(&omap->chip);
344}
345
346static const struct of_device_id pwm_omap_dmtimer_of_match[] = {
347 {.compatible = "ti,omap-dmtimer-pwm"},
348 {}
349};
350MODULE_DEVICE_TABLE(of, pwm_omap_dmtimer_of_match);
351
352static struct platform_driver pwm_omap_dmtimer_driver = {
353 .driver = {
354 .name = "omap-dmtimer-pwm",
355 .of_match_table = of_match_ptr(pwm_omap_dmtimer_of_match),
356 },
357 .probe = pwm_omap_dmtimer_probe,
358 .remove = pwm_omap_dmtimer_remove,
359};
360module_platform_driver(pwm_omap_dmtimer_driver);
361
362MODULE_AUTHOR("Grant Erickson <marathon96@gmail.com>");
363MODULE_AUTHOR("NeilBrown <neilb@suse.de>");
364MODULE_AUTHOR("Neil Armstrong <narmstrong@baylibre.com>");
365MODULE_LICENSE("GPL v2");
366MODULE_DESCRIPTION("OMAP PWM Driver using Dual-mode Timers");
This page took 0.040926 seconds and 5 git commands to generate.