Input: ad7877 - keep dma rx buffers in seperate cache lines
[pandora-kernel.git] / arch / arm / plat-samsung / pwm.c
1 /* arch/arm/plat-s3c/pwm.c
2  *
3  * Copyright (c) 2007 Ben Dooks
4  * Copyright (c) 2008 Simtec Electronics
5  *      Ben Dooks <ben@simtec.co.uk>, <ben-linux@fluff.org>
6  *
7  * S3C series PWM device core
8  *
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>
17 #include <linux/slab.h>
18 #include <linux/err.h>
19 #include <linux/clk.h>
20 #include <linux/io.h>
21 #include <linux/pwm.h>
22
23 #include <mach/irqs.h>
24 #include <mach/map.h>
25
26 #include <plat/devs.h>
27 #include <plat/regs-timer.h>
28
29 struct 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
46 #define pwm_dbg(_pwm, msg...) dev_dbg(&(_pwm)->pdev->dev, msg)
47
48 static 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
63 #define DEFINE_S3C_TIMER(_tmr_no, _irq)                 \
64         .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
73 struct platform_device s3c_device_timer[] = {
74         [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) },
79 };
80
81 static inline int pwm_is_tdiv(struct pwm_device *pwm)
82 {
83         return clk_get_parent(pwm->clk) == pwm->clk_div;
84 }
85
86 static DEFINE_MUTEX(pwm_lock);
87 static LIST_HEAD(pwm_list);
88
89 struct 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
116 EXPORT_SYMBOL(pwm_request);
117
118
119 void 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
132 EXPORT_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
139 int 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
156 EXPORT_SYMBOL(pwm_enable);
157
158 void 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
174 EXPORT_SYMBOL(pwm_disable);
175
176 static unsigned long pwm_calc_tin(struct pwm_device *pwm, unsigned long freq)
177 {
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
194 int 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;
251         /* 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--;
255
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
281 EXPORT_SYMBOL(pwm_config);
282
283 static 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
295 static 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
372 static int __devexit s3c_pwm_remove(struct platform_device *pdev)
373 {
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
383 #ifdef CONFIG_PM
384 static 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
398 static 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
416 static 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),
423         .suspend        = s3c_pwm_suspend,
424         .resume         = s3c_pwm_resume,
425 };
426
427 static 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
446 arch_initcall(pwm_init);