Merge branch 'master' of git://git.infradead.org/users/eparis/selinux into for-linus
[platform/adaptation/renesas_rcar/renesas_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/map.h>
24
25 #include <plat/regs-timer.h>
26
27 struct pwm_device {
28         struct list_head         list;
29         struct platform_device  *pdev;
30
31         struct clk              *clk_div;
32         struct clk              *clk;
33         const char              *label;
34
35         unsigned int             period_ns;
36         unsigned int             duty_ns;
37
38         unsigned char            tcon_base;
39         unsigned char            running;
40         unsigned char            use_count;
41         unsigned char            pwm_id;
42 };
43
44 #define pwm_dbg(_pwm, msg...) dev_dbg(&(_pwm)->pdev->dev, msg)
45
46 static struct clk *clk_scaler[2];
47
48 static inline int pwm_is_tdiv(struct pwm_device *pwm)
49 {
50         return clk_get_parent(pwm->clk) == pwm->clk_div;
51 }
52
53 static DEFINE_MUTEX(pwm_lock);
54 static LIST_HEAD(pwm_list);
55
56 struct pwm_device *pwm_request(int pwm_id, const char *label)
57 {
58         struct pwm_device *pwm;
59         int found = 0;
60
61         mutex_lock(&pwm_lock);
62
63         list_for_each_entry(pwm, &pwm_list, list) {
64                 if (pwm->pwm_id == pwm_id) {
65                         found = 1;
66                         break;
67                 }
68         }
69
70         if (found) {
71                 if (pwm->use_count == 0) {
72                         pwm->use_count = 1;
73                         pwm->label = label;
74                 } else
75                         pwm = ERR_PTR(-EBUSY);
76         } else
77                 pwm = ERR_PTR(-ENOENT);
78
79         mutex_unlock(&pwm_lock);
80         return pwm;
81 }
82
83 EXPORT_SYMBOL(pwm_request);
84
85
86 void pwm_free(struct pwm_device *pwm)
87 {
88         mutex_lock(&pwm_lock);
89
90         if (pwm->use_count) {
91                 pwm->use_count--;
92                 pwm->label = NULL;
93         } else
94                 printk(KERN_ERR "PWM%d device already freed\n", pwm->pwm_id);
95
96         mutex_unlock(&pwm_lock);
97 }
98
99 EXPORT_SYMBOL(pwm_free);
100
101 #define pwm_tcon_start(pwm) (1 << (pwm->tcon_base + 0))
102 #define pwm_tcon_invert(pwm) (1 << (pwm->tcon_base + 2))
103 #define pwm_tcon_autoreload(pwm) (1 << (pwm->tcon_base + 3))
104 #define pwm_tcon_manulupdate(pwm) (1 << (pwm->tcon_base + 1))
105
106 int pwm_enable(struct pwm_device *pwm)
107 {
108         unsigned long flags;
109         unsigned long tcon;
110
111         local_irq_save(flags);
112
113         tcon = __raw_readl(S3C2410_TCON);
114         tcon |= pwm_tcon_start(pwm);
115         __raw_writel(tcon, S3C2410_TCON);
116
117         local_irq_restore(flags);
118
119         pwm->running = 1;
120         return 0;
121 }
122
123 EXPORT_SYMBOL(pwm_enable);
124
125 void pwm_disable(struct pwm_device *pwm)
126 {
127         unsigned long flags;
128         unsigned long tcon;
129
130         local_irq_save(flags);
131
132         tcon = __raw_readl(S3C2410_TCON);
133         tcon &= ~pwm_tcon_start(pwm);
134         __raw_writel(tcon, S3C2410_TCON);
135
136         local_irq_restore(flags);
137
138         pwm->running = 0;
139 }
140
141 EXPORT_SYMBOL(pwm_disable);
142
143 static unsigned long pwm_calc_tin(struct pwm_device *pwm, unsigned long freq)
144 {
145         unsigned long tin_parent_rate;
146         unsigned int div;
147
148         tin_parent_rate = clk_get_rate(clk_get_parent(pwm->clk_div));
149         pwm_dbg(pwm, "tin parent at %lu\n", tin_parent_rate);
150
151         for (div = 2; div <= 16; div *= 2) {
152                 if ((tin_parent_rate / (div << 16)) < freq)
153                         return tin_parent_rate / div;
154         }
155
156         return tin_parent_rate / 16;
157 }
158
159 #define NS_IN_HZ (1000000000UL)
160
161 int pwm_config(struct pwm_device *pwm, int duty_ns, int period_ns)
162 {
163         unsigned long tin_rate;
164         unsigned long tin_ns;
165         unsigned long period;
166         unsigned long flags;
167         unsigned long tcon;
168         unsigned long tcnt;
169         long tcmp;
170
171         /* We currently avoid using 64bit arithmetic by using the
172          * fact that anything faster than 1Hz is easily representable
173          * by 32bits. */
174
175         if (period_ns > NS_IN_HZ || duty_ns > NS_IN_HZ)
176                 return -ERANGE;
177
178         if (duty_ns > period_ns)
179                 return -EINVAL;
180
181         if (period_ns == pwm->period_ns &&
182             duty_ns == pwm->duty_ns)
183                 return 0;
184
185         /* The TCMP and TCNT can be read without a lock, they're not
186          * shared between the timers. */
187
188         tcmp = __raw_readl(S3C2410_TCMPB(pwm->pwm_id));
189         tcnt = __raw_readl(S3C2410_TCNTB(pwm->pwm_id));
190
191         period = NS_IN_HZ / period_ns;
192
193         pwm_dbg(pwm, "duty_ns=%d, period_ns=%d (%lu)\n",
194                 duty_ns, period_ns, period);
195
196         /* Check to see if we are changing the clock rate of the PWM */
197
198         if (pwm->period_ns != period_ns) {
199                 if (pwm_is_tdiv(pwm)) {
200                         tin_rate = pwm_calc_tin(pwm, period);
201                         clk_set_rate(pwm->clk_div, tin_rate);
202                 } else
203                         tin_rate = clk_get_rate(pwm->clk);
204
205                 pwm->period_ns = period_ns;
206
207                 pwm_dbg(pwm, "tin_rate=%lu\n", tin_rate);
208
209                 tin_ns = NS_IN_HZ / tin_rate;
210                 tcnt = period_ns / tin_ns;
211         } else
212                 tin_ns = NS_IN_HZ / clk_get_rate(pwm->clk);
213
214         /* Note, counters count down */
215
216         tcmp = duty_ns / tin_ns;
217         tcmp = tcnt - tcmp;
218         /* the pwm hw only checks the compare register after a decrement,
219            so the pin never toggles if tcmp = tcnt */
220         if (tcmp == tcnt)
221                 tcmp--;
222
223         pwm_dbg(pwm, "tin_ns=%lu, tcmp=%ld/%lu\n", tin_ns, tcmp, tcnt);
224
225         if (tcmp < 0)
226                 tcmp = 0;
227
228         /* Update the PWM register block. */
229
230         local_irq_save(flags);
231
232         __raw_writel(tcmp, S3C2410_TCMPB(pwm->pwm_id));
233         __raw_writel(tcnt, S3C2410_TCNTB(pwm->pwm_id));
234
235         tcon = __raw_readl(S3C2410_TCON);
236         tcon |= pwm_tcon_manulupdate(pwm);
237         tcon |= pwm_tcon_autoreload(pwm);
238         __raw_writel(tcon, S3C2410_TCON);
239
240         tcon &= ~pwm_tcon_manulupdate(pwm);
241         __raw_writel(tcon, S3C2410_TCON);
242
243         local_irq_restore(flags);
244
245         return 0;
246 }
247
248 EXPORT_SYMBOL(pwm_config);
249
250 static int pwm_register(struct pwm_device *pwm)
251 {
252         pwm->duty_ns = -1;
253         pwm->period_ns = -1;
254
255         mutex_lock(&pwm_lock);
256         list_add_tail(&pwm->list, &pwm_list);
257         mutex_unlock(&pwm_lock);
258
259         return 0;
260 }
261
262 static int s3c_pwm_probe(struct platform_device *pdev)
263 {
264         struct device *dev = &pdev->dev;
265         struct pwm_device *pwm;
266         unsigned long flags;
267         unsigned long tcon;
268         unsigned int id = pdev->id;
269         int ret;
270
271         if (id == 4) {
272                 dev_err(dev, "TIMER4 is currently not supported\n");
273                 return -ENXIO;
274         }
275
276         pwm = kzalloc(sizeof(struct pwm_device), GFP_KERNEL);
277         if (pwm == NULL) {
278                 dev_err(dev, "failed to allocate pwm_device\n");
279                 return -ENOMEM;
280         }
281
282         pwm->pdev = pdev;
283         pwm->pwm_id = id;
284
285         /* calculate base of control bits in TCON */
286         pwm->tcon_base = id == 0 ? 0 : (id * 4) + 4;
287
288         pwm->clk = clk_get(dev, "pwm-tin");
289         if (IS_ERR(pwm->clk)) {
290                 dev_err(dev, "failed to get pwm tin clk\n");
291                 ret = PTR_ERR(pwm->clk);
292                 goto err_alloc;
293         }
294
295         pwm->clk_div = clk_get(dev, "pwm-tdiv");
296         if (IS_ERR(pwm->clk_div)) {
297                 dev_err(dev, "failed to get pwm tdiv clk\n");
298                 ret = PTR_ERR(pwm->clk_div);
299                 goto err_clk_tin;
300         }
301
302         local_irq_save(flags);
303
304         tcon = __raw_readl(S3C2410_TCON);
305         tcon |= pwm_tcon_invert(pwm);
306         __raw_writel(tcon, S3C2410_TCON);
307
308         local_irq_restore(flags);
309
310
311         ret = pwm_register(pwm);
312         if (ret) {
313                 dev_err(dev, "failed to register pwm\n");
314                 goto err_clk_tdiv;
315         }
316
317         pwm_dbg(pwm, "config bits %02x\n",
318                 (__raw_readl(S3C2410_TCON) >> pwm->tcon_base) & 0x0f);
319
320         dev_info(dev, "tin at %lu, tdiv at %lu, tin=%sclk, base %d\n",
321                  clk_get_rate(pwm->clk),
322                  clk_get_rate(pwm->clk_div),
323                  pwm_is_tdiv(pwm) ? "div" : "ext", pwm->tcon_base);
324
325         platform_set_drvdata(pdev, pwm);
326         return 0;
327
328  err_clk_tdiv:
329         clk_put(pwm->clk_div);
330
331  err_clk_tin:
332         clk_put(pwm->clk);
333
334  err_alloc:
335         kfree(pwm);
336         return ret;
337 }
338
339 static int __devexit s3c_pwm_remove(struct platform_device *pdev)
340 {
341         struct pwm_device *pwm = platform_get_drvdata(pdev);
342
343         clk_put(pwm->clk_div);
344         clk_put(pwm->clk);
345         kfree(pwm);
346
347         return 0;
348 }
349
350 #ifdef CONFIG_PM
351 static int s3c_pwm_suspend(struct platform_device *pdev, pm_message_t state)
352 {
353         struct pwm_device *pwm = platform_get_drvdata(pdev);
354
355         /* No one preserve these values during suspend so reset them
356          * Otherwise driver leaves PWM unconfigured if same values
357          * passed to pwm_config
358          */
359         pwm->period_ns = 0;
360         pwm->duty_ns = 0;
361
362         return 0;
363 }
364
365 static int s3c_pwm_resume(struct platform_device *pdev)
366 {
367         struct pwm_device *pwm = platform_get_drvdata(pdev);
368         unsigned long tcon;
369
370         /* Restore invertion */
371         tcon = __raw_readl(S3C2410_TCON);
372         tcon |= pwm_tcon_invert(pwm);
373         __raw_writel(tcon, S3C2410_TCON);
374
375         return 0;
376 }
377
378 #else
379 #define s3c_pwm_suspend NULL
380 #define s3c_pwm_resume NULL
381 #endif
382
383 static struct platform_driver s3c_pwm_driver = {
384         .driver         = {
385                 .name   = "s3c24xx-pwm",
386                 .owner  = THIS_MODULE,
387         },
388         .probe          = s3c_pwm_probe,
389         .remove         = __devexit_p(s3c_pwm_remove),
390         .suspend        = s3c_pwm_suspend,
391         .resume         = s3c_pwm_resume,
392 };
393
394 static int __init pwm_init(void)
395 {
396         int ret;
397
398         clk_scaler[0] = clk_get(NULL, "pwm-scaler0");
399         clk_scaler[1] = clk_get(NULL, "pwm-scaler1");
400
401         if (IS_ERR(clk_scaler[0]) || IS_ERR(clk_scaler[1])) {
402                 printk(KERN_ERR "%s: failed to get scaler clocks\n", __func__);
403                 return -EINVAL;
404         }
405
406         ret = platform_driver_register(&s3c_pwm_driver);
407         if (ret)
408                 printk(KERN_ERR "%s: failed to add pwm driver\n", __func__);
409
410         return ret;
411 }
412
413 arch_initcall(pwm_init);