blob: 2eeb49fa056d6413c1963abc6113fa0eff76caf8 [file] [log] [blame]
Peter Korsgaardb5ead1c2009-07-01 17:47:07 +02001/* arch/arm/plat-s3c/pwm.c
Ben Dooks6fc601e2008-07-01 13:17:24 +01002 *
3 * Copyright (c) 2007 Ben Dooks
4 * Copyright (c) 2008 Simtec Electronics
5 * Ben Dooks <ben@simtec.co.uk>, <ben-linux@fluff.org>
6 *
Peter Korsgaardb5ead1c2009-07-01 17:47:07 +02007 * S3C series PWM device core
Ben Dooks6fc601e2008-07-01 13:17:24 +01008 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License.
12*/
13
14#include <linux/module.h>
15#include <linux/kernel.h>
16#include <linux/platform_device.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090017#include <linux/slab.h>
Ben Dooks6fc601e2008-07-01 13:17:24 +010018#include <linux/err.h>
19#include <linux/clk.h>
20#include <linux/io.h>
21#include <linux/pwm.h>
22
Russell King80b02c12009-01-08 10:01:47 +000023#include <mach/irqs.h>
Peter Korsgaardb5ead1c2009-07-01 17:47:07 +020024#include <mach/map.h>
Russell King80b02c12009-01-08 10:01:47 +000025
Ben Dooksa2b7ba92008-10-07 22:26:09 +010026#include <plat/devs.h>
27#include <plat/regs-timer.h>
Ben Dooks6fc601e2008-07-01 13:17:24 +010028
29struct pwm_device {
30 struct list_head list;
31 struct platform_device *pdev;
32
33 struct clk *clk_div;
34 struct clk *clk;
35 const char *label;
36
37 unsigned int period_ns;
38 unsigned int duty_ns;
39
40 unsigned char tcon_base;
41 unsigned char running;
42 unsigned char use_count;
43 unsigned char pwm_id;
44};
45
Ben Dooks66592ee2008-08-26 22:54:06 +010046#define pwm_dbg(_pwm, msg...) dev_dbg(&(_pwm)->pdev->dev, msg)
Ben Dooks6fc601e2008-07-01 13:17:24 +010047
48static struct clk *clk_scaler[2];
49
50/* Standard setup for a timer block. */
51
52#define TIMER_RESOURCE_SIZE (1)
53
54#define TIMER_RESOURCE(_tmr, _irq) \
55 (struct resource [TIMER_RESOURCE_SIZE]) { \
56 [0] = { \
57 .start = _irq, \
58 .end = _irq, \
59 .flags = IORESOURCE_IRQ \
60 } \
61 }
62
Nelson Castillo55b404f2008-10-16 16:46:10 +010063#define DEFINE_S3C_TIMER(_tmr_no, _irq) \
Ben Dooks6fc601e2008-07-01 13:17:24 +010064 .name = "s3c24xx-pwm", \
65 .id = _tmr_no, \
66 .num_resources = TIMER_RESOURCE_SIZE, \
67 .resource = TIMER_RESOURCE(_tmr_no, _irq), \
68
69/* since we already have an static mapping for the timer, we do not
70 * bother setting any IO resource for the base.
71 */
72
73struct platform_device s3c_device_timer[] = {
Nelson Castillo55b404f2008-10-16 16:46:10 +010074 [0] = { DEFINE_S3C_TIMER(0, IRQ_TIMER0) },
75 [1] = { DEFINE_S3C_TIMER(1, IRQ_TIMER1) },
76 [2] = { DEFINE_S3C_TIMER(2, IRQ_TIMER2) },
77 [3] = { DEFINE_S3C_TIMER(3, IRQ_TIMER3) },
78 [4] = { DEFINE_S3C_TIMER(4, IRQ_TIMER4) },
Ben Dooks6fc601e2008-07-01 13:17:24 +010079};
80
81static inline int pwm_is_tdiv(struct pwm_device *pwm)
82{
83 return clk_get_parent(pwm->clk) == pwm->clk_div;
84}
85
86static DEFINE_MUTEX(pwm_lock);
87static LIST_HEAD(pwm_list);
88
89struct pwm_device *pwm_request(int pwm_id, const char *label)
90{
91 struct pwm_device *pwm;
92 int found = 0;
93
94 mutex_lock(&pwm_lock);
95
96 list_for_each_entry(pwm, &pwm_list, list) {
97 if (pwm->pwm_id == pwm_id) {
98 found = 1;
99 break;
100 }
101 }
102
103 if (found) {
104 if (pwm->use_count == 0) {
105 pwm->use_count = 1;
106 pwm->label = label;
107 } else
108 pwm = ERR_PTR(-EBUSY);
109 } else
110 pwm = ERR_PTR(-ENOENT);
111
112 mutex_unlock(&pwm_lock);
113 return pwm;
114}
115
116EXPORT_SYMBOL(pwm_request);
117
118
119void pwm_free(struct pwm_device *pwm)
120{
121 mutex_lock(&pwm_lock);
122
123 if (pwm->use_count) {
124 pwm->use_count--;
125 pwm->label = NULL;
126 } else
127 printk(KERN_ERR "PWM%d device already freed\n", pwm->pwm_id);
128
129 mutex_unlock(&pwm_lock);
130}
131
132EXPORT_SYMBOL(pwm_free);
133
134#define pwm_tcon_start(pwm) (1 << (pwm->tcon_base + 0))
135#define pwm_tcon_invert(pwm) (1 << (pwm->tcon_base + 2))
136#define pwm_tcon_autoreload(pwm) (1 << (pwm->tcon_base + 3))
137#define pwm_tcon_manulupdate(pwm) (1 << (pwm->tcon_base + 1))
138
139int pwm_enable(struct pwm_device *pwm)
140{
141 unsigned long flags;
142 unsigned long tcon;
143
144 local_irq_save(flags);
145
146 tcon = __raw_readl(S3C2410_TCON);
147 tcon |= pwm_tcon_start(pwm);
148 __raw_writel(tcon, S3C2410_TCON);
149
150 local_irq_restore(flags);
151
152 pwm->running = 1;
153 return 0;
154}
155
156EXPORT_SYMBOL(pwm_enable);
157
158void pwm_disable(struct pwm_device *pwm)
159{
160 unsigned long flags;
161 unsigned long tcon;
162
163 local_irq_save(flags);
164
165 tcon = __raw_readl(S3C2410_TCON);
166 tcon &= ~pwm_tcon_start(pwm);
167 __raw_writel(tcon, S3C2410_TCON);
168
169 local_irq_restore(flags);
170
171 pwm->running = 0;
172}
173
174EXPORT_SYMBOL(pwm_disable);
175
Ben Dooks10389592008-08-26 22:54:05 +0100176static unsigned long pwm_calc_tin(struct pwm_device *pwm, unsigned long freq)
Ben Dooks6fc601e2008-07-01 13:17:24 +0100177{
178 unsigned long tin_parent_rate;
179 unsigned int div;
180
181 tin_parent_rate = clk_get_rate(clk_get_parent(pwm->clk_div));
182 pwm_dbg(pwm, "tin parent at %lu\n", tin_parent_rate);
183
184 for (div = 2; div <= 16; div *= 2) {
185 if ((tin_parent_rate / (div << 16)) < freq)
186 return tin_parent_rate / div;
187 }
188
189 return tin_parent_rate / 16;
190}
191
192#define NS_IN_HZ (1000000000UL)
193
194int pwm_config(struct pwm_device *pwm, int duty_ns, int period_ns)
195{
196 unsigned long tin_rate;
197 unsigned long tin_ns;
198 unsigned long period;
199 unsigned long flags;
200 unsigned long tcon;
201 unsigned long tcnt;
202 long tcmp;
203
204 /* We currently avoid using 64bit arithmetic by using the
205 * fact that anything faster than 1Hz is easily representable
206 * by 32bits. */
207
208 if (period_ns > NS_IN_HZ || duty_ns > NS_IN_HZ)
209 return -ERANGE;
210
211 if (duty_ns > period_ns)
212 return -EINVAL;
213
214 if (period_ns == pwm->period_ns &&
215 duty_ns == pwm->duty_ns)
216 return 0;
217
218 /* The TCMP and TCNT can be read without a lock, they're not
219 * shared between the timers. */
220
221 tcmp = __raw_readl(S3C2410_TCMPB(pwm->pwm_id));
222 tcnt = __raw_readl(S3C2410_TCNTB(pwm->pwm_id));
223
224 period = NS_IN_HZ / period_ns;
225
226 pwm_dbg(pwm, "duty_ns=%d, period_ns=%d (%lu)\n",
227 duty_ns, period_ns, period);
228
229 /* Check to see if we are changing the clock rate of the PWM */
230
231 if (pwm->period_ns != period_ns) {
232 if (pwm_is_tdiv(pwm)) {
233 tin_rate = pwm_calc_tin(pwm, period);
234 clk_set_rate(pwm->clk_div, tin_rate);
235 } else
236 tin_rate = clk_get_rate(pwm->clk);
237
238 pwm->period_ns = period_ns;
239
240 pwm_dbg(pwm, "tin_rate=%lu\n", tin_rate);
241
242 tin_ns = NS_IN_HZ / tin_rate;
243 tcnt = period_ns / tin_ns;
244 } else
245 tin_ns = NS_IN_HZ / clk_get_rate(pwm->clk);
246
247 /* Note, counters count down */
248
249 tcmp = duty_ns / tin_ns;
250 tcmp = tcnt - tcmp;
Peter Korsgaard165f5f62009-07-01 17:47:08 +0200251 /* the pwm hw only checks the compare register after a decrement,
252 so the pin never toggles if tcmp = tcnt */
253 if (tcmp == tcnt)
254 tcmp--;
Ben Dooks6fc601e2008-07-01 13:17:24 +0100255
256 pwm_dbg(pwm, "tin_ns=%lu, tcmp=%ld/%lu\n", tin_ns, tcmp, tcnt);
257
258 if (tcmp < 0)
259 tcmp = 0;
260
261 /* Update the PWM register block. */
262
263 local_irq_save(flags);
264
265 __raw_writel(tcmp, S3C2410_TCMPB(pwm->pwm_id));
266 __raw_writel(tcnt, S3C2410_TCNTB(pwm->pwm_id));
267
268 tcon = __raw_readl(S3C2410_TCON);
269 tcon |= pwm_tcon_manulupdate(pwm);
270 tcon |= pwm_tcon_autoreload(pwm);
271 __raw_writel(tcon, S3C2410_TCON);
272
273 tcon &= ~pwm_tcon_manulupdate(pwm);
274 __raw_writel(tcon, S3C2410_TCON);
275
276 local_irq_restore(flags);
277
278 return 0;
279}
280
281EXPORT_SYMBOL(pwm_config);
282
283static int pwm_register(struct pwm_device *pwm)
284{
285 pwm->duty_ns = -1;
286 pwm->period_ns = -1;
287
288 mutex_lock(&pwm_lock);
289 list_add_tail(&pwm->list, &pwm_list);
290 mutex_unlock(&pwm_lock);
291
292 return 0;
293}
294
295static int s3c_pwm_probe(struct platform_device *pdev)
296{
297 struct device *dev = &pdev->dev;
298 struct pwm_device *pwm;
299 unsigned long flags;
300 unsigned long tcon;
301 unsigned int id = pdev->id;
302 int ret;
303
304 if (id == 4) {
305 dev_err(dev, "TIMER4 is currently not supported\n");
306 return -ENXIO;
307 }
308
309 pwm = kzalloc(sizeof(struct pwm_device), GFP_KERNEL);
310 if (pwm == NULL) {
311 dev_err(dev, "failed to allocate pwm_device\n");
312 return -ENOMEM;
313 }
314
315 pwm->pdev = pdev;
316 pwm->pwm_id = id;
317
318 /* calculate base of control bits in TCON */
319 pwm->tcon_base = id == 0 ? 0 : (id * 4) + 4;
320
321 pwm->clk = clk_get(dev, "pwm-tin");
322 if (IS_ERR(pwm->clk)) {
323 dev_err(dev, "failed to get pwm tin clk\n");
324 ret = PTR_ERR(pwm->clk);
325 goto err_alloc;
326 }
327
328 pwm->clk_div = clk_get(dev, "pwm-tdiv");
329 if (IS_ERR(pwm->clk_div)) {
330 dev_err(dev, "failed to get pwm tdiv clk\n");
331 ret = PTR_ERR(pwm->clk_div);
332 goto err_clk_tin;
333 }
334
335 local_irq_save(flags);
336
337 tcon = __raw_readl(S3C2410_TCON);
338 tcon |= pwm_tcon_invert(pwm);
339 __raw_writel(tcon, S3C2410_TCON);
340
341 local_irq_restore(flags);
342
343
344 ret = pwm_register(pwm);
345 if (ret) {
346 dev_err(dev, "failed to register pwm\n");
347 goto err_clk_tdiv;
348 }
349
350 pwm_dbg(pwm, "config bits %02x\n",
351 (__raw_readl(S3C2410_TCON) >> pwm->tcon_base) & 0x0f);
352
353 dev_info(dev, "tin at %lu, tdiv at %lu, tin=%sclk, base %d\n",
354 clk_get_rate(pwm->clk),
355 clk_get_rate(pwm->clk_div),
356 pwm_is_tdiv(pwm) ? "div" : "ext", pwm->tcon_base);
357
358 platform_set_drvdata(pdev, pwm);
359 return 0;
360
361 err_clk_tdiv:
362 clk_put(pwm->clk_div);
363
364 err_clk_tin:
365 clk_put(pwm->clk);
366
367 err_alloc:
368 kfree(pwm);
369 return ret;
370}
371
Uwe Kleine-König93b0d8c2009-11-24 22:07:11 +0100372static int __devexit s3c_pwm_remove(struct platform_device *pdev)
Ben Dooks6fc601e2008-07-01 13:17:24 +0100373{
374 struct pwm_device *pwm = platform_get_drvdata(pdev);
375
376 clk_put(pwm->clk_div);
377 clk_put(pwm->clk);
378 kfree(pwm);
379
380 return 0;
381}
382
Vasily Khoruzhicke79032a2010-03-15 16:27:08 +0200383#ifdef CONFIG_PM
384static int s3c_pwm_suspend(struct platform_device *pdev, pm_message_t state)
385{
386 struct pwm_device *pwm = platform_get_drvdata(pdev);
387
388 /* No one preserve these values during suspend so reset them
389 * Otherwise driver leaves PWM unconfigured if same values
390 * passed to pwm_config
391 */
392 pwm->period_ns = 0;
393 pwm->duty_ns = 0;
394
395 return 0;
396}
397
398static int s3c_pwm_resume(struct platform_device *pdev)
399{
400 struct pwm_device *pwm = platform_get_drvdata(pdev);
401 unsigned long tcon;
402
403 /* Restore invertion */
404 tcon = __raw_readl(S3C2410_TCON);
405 tcon |= pwm_tcon_invert(pwm);
406 __raw_writel(tcon, S3C2410_TCON);
407
408 return 0;
409}
410
411#else
412#define s3c_pwm_suspend NULL
413#define s3c_pwm_resume NULL
414#endif
415
Ben Dooks6fc601e2008-07-01 13:17:24 +0100416static struct platform_driver s3c_pwm_driver = {
417 .driver = {
418 .name = "s3c24xx-pwm",
419 .owner = THIS_MODULE,
420 },
421 .probe = s3c_pwm_probe,
422 .remove = __devexit_p(s3c_pwm_remove),
Vasily Khoruzhicke79032a2010-03-15 16:27:08 +0200423 .suspend = s3c_pwm_suspend,
424 .resume = s3c_pwm_resume,
Ben Dooks6fc601e2008-07-01 13:17:24 +0100425};
426
427static int __init pwm_init(void)
428{
429 int ret;
430
431 clk_scaler[0] = clk_get(NULL, "pwm-scaler0");
432 clk_scaler[1] = clk_get(NULL, "pwm-scaler1");
433
434 if (IS_ERR(clk_scaler[0]) || IS_ERR(clk_scaler[1])) {
435 printk(KERN_ERR "%s: failed to get scaler clocks\n", __func__);
436 return -EINVAL;
437 }
438
439 ret = platform_driver_register(&s3c_pwm_driver);
440 if (ret)
441 printk(KERN_ERR "%s: failed to add pwm driver\n", __func__);
442
443 return ret;
444}
445
446arch_initcall(pwm_init);