GUI: Fix Tomato RAF theme for all builds. Compilation typo.
[tomato.git] / release / src-rt-6.x.4708 / linux / linux-2.6.36 / arch / arm / plat-samsung / pwm.c
blob2eeb49fa056d6413c1963abc6113fa0eff76caf8
1 /* arch/arm/plat-s3c/pwm.c
3 * Copyright (c) 2007 Ben Dooks
4 * Copyright (c) 2008 Simtec Electronics
5 * Ben Dooks <ben@simtec.co.uk>, <ben-linux@fluff.org>
7 * S3C series PWM device core
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.
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>
23 #include <mach/irqs.h>
24 #include <mach/map.h>
26 #include <plat/devs.h>
27 #include <plat/regs-timer.h>
29 struct pwm_device {
30 struct list_head list;
31 struct platform_device *pdev;
33 struct clk *clk_div;
34 struct clk *clk;
35 const char *label;
37 unsigned int period_ns;
38 unsigned int duty_ns;
40 unsigned char tcon_base;
41 unsigned char running;
42 unsigned char use_count;
43 unsigned char pwm_id;
46 #define pwm_dbg(_pwm, msg...) dev_dbg(&(_pwm)->pdev->dev, msg)
48 static struct clk *clk_scaler[2];
50 /* Standard setup for a timer block. */
52 #define TIMER_RESOURCE_SIZE (1)
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 } \
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), \
69 /* since we already have an static mapping for the timer, we do not
70 * bother setting any IO resource for the base.
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) },
81 static inline int pwm_is_tdiv(struct pwm_device *pwm)
83 return clk_get_parent(pwm->clk) == pwm->clk_div;
86 static DEFINE_MUTEX(pwm_lock);
87 static LIST_HEAD(pwm_list);
89 struct pwm_device *pwm_request(int pwm_id, const char *label)
91 struct pwm_device *pwm;
92 int found = 0;
94 mutex_lock(&pwm_lock);
96 list_for_each_entry(pwm, &pwm_list, list) {
97 if (pwm->pwm_id == pwm_id) {
98 found = 1;
99 break;
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);
112 mutex_unlock(&pwm_lock);
113 return pwm;
116 EXPORT_SYMBOL(pwm_request);
119 void pwm_free(struct pwm_device *pwm)
121 mutex_lock(&pwm_lock);
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);
129 mutex_unlock(&pwm_lock);
132 EXPORT_SYMBOL(pwm_free);
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))
139 int pwm_enable(struct pwm_device *pwm)
141 unsigned long flags;
142 unsigned long tcon;
144 local_irq_save(flags);
146 tcon = __raw_readl(S3C2410_TCON);
147 tcon |= pwm_tcon_start(pwm);
148 __raw_writel(tcon, S3C2410_TCON);
150 local_irq_restore(flags);
152 pwm->running = 1;
153 return 0;
156 EXPORT_SYMBOL(pwm_enable);
158 void pwm_disable(struct pwm_device *pwm)
160 unsigned long flags;
161 unsigned long tcon;
163 local_irq_save(flags);
165 tcon = __raw_readl(S3C2410_TCON);
166 tcon &= ~pwm_tcon_start(pwm);
167 __raw_writel(tcon, S3C2410_TCON);
169 local_irq_restore(flags);
171 pwm->running = 0;
174 EXPORT_SYMBOL(pwm_disable);
176 static unsigned long pwm_calc_tin(struct pwm_device *pwm, unsigned long freq)
178 unsigned long tin_parent_rate;
179 unsigned int div;
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);
184 for (div = 2; div <= 16; div *= 2) {
185 if ((tin_parent_rate / (div << 16)) < freq)
186 return tin_parent_rate / div;
189 return tin_parent_rate / 16;
192 #define NS_IN_HZ (1000000000UL)
194 int pwm_config(struct pwm_device *pwm, int duty_ns, int period_ns)
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;
204 /* We currently avoid using 64bit arithmetic by using the
205 * fact that anything faster than 1Hz is easily representable
206 * by 32bits. */
208 if (period_ns > NS_IN_HZ || duty_ns > NS_IN_HZ)
209 return -ERANGE;
211 if (duty_ns > period_ns)
212 return -EINVAL;
214 if (period_ns == pwm->period_ns &&
215 duty_ns == pwm->duty_ns)
216 return 0;
218 /* The TCMP and TCNT can be read without a lock, they're not
219 * shared between the timers. */
221 tcmp = __raw_readl(S3C2410_TCMPB(pwm->pwm_id));
222 tcnt = __raw_readl(S3C2410_TCNTB(pwm->pwm_id));
224 period = NS_IN_HZ / period_ns;
226 pwm_dbg(pwm, "duty_ns=%d, period_ns=%d (%lu)\n",
227 duty_ns, period_ns, period);
229 /* Check to see if we are changing the clock rate of the PWM */
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);
238 pwm->period_ns = period_ns;
240 pwm_dbg(pwm, "tin_rate=%lu\n", tin_rate);
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);
247 /* Note, counters count down */
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--;
256 pwm_dbg(pwm, "tin_ns=%lu, tcmp=%ld/%lu\n", tin_ns, tcmp, tcnt);
258 if (tcmp < 0)
259 tcmp = 0;
261 /* Update the PWM register block. */
263 local_irq_save(flags);
265 __raw_writel(tcmp, S3C2410_TCMPB(pwm->pwm_id));
266 __raw_writel(tcnt, S3C2410_TCNTB(pwm->pwm_id));
268 tcon = __raw_readl(S3C2410_TCON);
269 tcon |= pwm_tcon_manulupdate(pwm);
270 tcon |= pwm_tcon_autoreload(pwm);
271 __raw_writel(tcon, S3C2410_TCON);
273 tcon &= ~pwm_tcon_manulupdate(pwm);
274 __raw_writel(tcon, S3C2410_TCON);
276 local_irq_restore(flags);
278 return 0;
281 EXPORT_SYMBOL(pwm_config);
283 static int pwm_register(struct pwm_device *pwm)
285 pwm->duty_ns = -1;
286 pwm->period_ns = -1;
288 mutex_lock(&pwm_lock);
289 list_add_tail(&pwm->list, &pwm_list);
290 mutex_unlock(&pwm_lock);
292 return 0;
295 static int s3c_pwm_probe(struct platform_device *pdev)
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;
304 if (id == 4) {
305 dev_err(dev, "TIMER4 is currently not supported\n");
306 return -ENXIO;
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;
315 pwm->pdev = pdev;
316 pwm->pwm_id = id;
318 /* calculate base of control bits in TCON */
319 pwm->tcon_base = id == 0 ? 0 : (id * 4) + 4;
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;
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;
335 local_irq_save(flags);
337 tcon = __raw_readl(S3C2410_TCON);
338 tcon |= pwm_tcon_invert(pwm);
339 __raw_writel(tcon, S3C2410_TCON);
341 local_irq_restore(flags);
344 ret = pwm_register(pwm);
345 if (ret) {
346 dev_err(dev, "failed to register pwm\n");
347 goto err_clk_tdiv;
350 pwm_dbg(pwm, "config bits %02x\n",
351 (__raw_readl(S3C2410_TCON) >> pwm->tcon_base) & 0x0f);
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);
358 platform_set_drvdata(pdev, pwm);
359 return 0;
361 err_clk_tdiv:
362 clk_put(pwm->clk_div);
364 err_clk_tin:
365 clk_put(pwm->clk);
367 err_alloc:
368 kfree(pwm);
369 return ret;
372 static int __devexit s3c_pwm_remove(struct platform_device *pdev)
374 struct pwm_device *pwm = platform_get_drvdata(pdev);
376 clk_put(pwm->clk_div);
377 clk_put(pwm->clk);
378 kfree(pwm);
380 return 0;
383 #ifdef CONFIG_PM
384 static int s3c_pwm_suspend(struct platform_device *pdev, pm_message_t state)
386 struct pwm_device *pwm = platform_get_drvdata(pdev);
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
392 pwm->period_ns = 0;
393 pwm->duty_ns = 0;
395 return 0;
398 static int s3c_pwm_resume(struct platform_device *pdev)
400 struct pwm_device *pwm = platform_get_drvdata(pdev);
401 unsigned long tcon;
403 /* Restore invertion */
404 tcon = __raw_readl(S3C2410_TCON);
405 tcon |= pwm_tcon_invert(pwm);
406 __raw_writel(tcon, S3C2410_TCON);
408 return 0;
411 #else
412 #define s3c_pwm_suspend NULL
413 #define s3c_pwm_resume NULL
414 #endif
416 static struct platform_driver s3c_pwm_driver = {
417 .driver = {
418 .name = "s3c24xx-pwm",
419 .owner = THIS_MODULE,
421 .probe = s3c_pwm_probe,
422 .remove = __devexit_p(s3c_pwm_remove),
423 .suspend = s3c_pwm_suspend,
424 .resume = s3c_pwm_resume,
427 static int __init pwm_init(void)
429 int ret;
431 clk_scaler[0] = clk_get(NULL, "pwm-scaler0");
432 clk_scaler[1] = clk_get(NULL, "pwm-scaler1");
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;
439 ret = platform_driver_register(&s3c_pwm_driver);
440 if (ret)
441 printk(KERN_ERR "%s: failed to add pwm driver\n", __func__);
443 return ret;
446 arch_initcall(pwm_init);