OMAP3 SRF: Generic shared resource f/w
[linux-ginger.git] / arch / arm / plat-s3c / pwm.c
blob4fdc5b307fd2f95cb3a0ae1152030b22588bc079
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/err.h>
18 #include <linux/clk.h>
19 #include <linux/io.h>
20 #include <linux/pwm.h>
22 #include <mach/irqs.h>
23 #include <mach/map.h>
25 #include <plat/devs.h>
26 #include <plat/regs-timer.h>
28 struct pwm_device {
29 struct list_head list;
30 struct platform_device *pdev;
32 struct clk *clk_div;
33 struct clk *clk;
34 const char *label;
36 unsigned int period_ns;
37 unsigned int duty_ns;
39 unsigned char tcon_base;
40 unsigned char running;
41 unsigned char use_count;
42 unsigned char pwm_id;
45 #define pwm_dbg(_pwm, msg...) dev_dbg(&(_pwm)->pdev->dev, msg)
47 static struct clk *clk_scaler[2];
49 /* Standard setup for a timer block. */
51 #define TIMER_RESOURCE_SIZE (1)
53 #define TIMER_RESOURCE(_tmr, _irq) \
54 (struct resource [TIMER_RESOURCE_SIZE]) { \
55 [0] = { \
56 .start = _irq, \
57 .end = _irq, \
58 .flags = IORESOURCE_IRQ \
59 } \
62 #define DEFINE_S3C_TIMER(_tmr_no, _irq) \
63 .name = "s3c24xx-pwm", \
64 .id = _tmr_no, \
65 .num_resources = TIMER_RESOURCE_SIZE, \
66 .resource = TIMER_RESOURCE(_tmr_no, _irq), \
68 /* since we already have an static mapping for the timer, we do not
69 * bother setting any IO resource for the base.
72 struct platform_device s3c_device_timer[] = {
73 [0] = { DEFINE_S3C_TIMER(0, IRQ_TIMER0) },
74 [1] = { DEFINE_S3C_TIMER(1, IRQ_TIMER1) },
75 [2] = { DEFINE_S3C_TIMER(2, IRQ_TIMER2) },
76 [3] = { DEFINE_S3C_TIMER(3, IRQ_TIMER3) },
77 [4] = { DEFINE_S3C_TIMER(4, IRQ_TIMER4) },
80 static inline int pwm_is_tdiv(struct pwm_device *pwm)
82 return clk_get_parent(pwm->clk) == pwm->clk_div;
85 static DEFINE_MUTEX(pwm_lock);
86 static LIST_HEAD(pwm_list);
88 struct pwm_device *pwm_request(int pwm_id, const char *label)
90 struct pwm_device *pwm;
91 int found = 0;
93 mutex_lock(&pwm_lock);
95 list_for_each_entry(pwm, &pwm_list, list) {
96 if (pwm->pwm_id == pwm_id) {
97 found = 1;
98 break;
102 if (found) {
103 if (pwm->use_count == 0) {
104 pwm->use_count = 1;
105 pwm->label = label;
106 } else
107 pwm = ERR_PTR(-EBUSY);
108 } else
109 pwm = ERR_PTR(-ENOENT);
111 mutex_unlock(&pwm_lock);
112 return pwm;
115 EXPORT_SYMBOL(pwm_request);
118 void pwm_free(struct pwm_device *pwm)
120 mutex_lock(&pwm_lock);
122 if (pwm->use_count) {
123 pwm->use_count--;
124 pwm->label = NULL;
125 } else
126 printk(KERN_ERR "PWM%d device already freed\n", pwm->pwm_id);
128 mutex_unlock(&pwm_lock);
131 EXPORT_SYMBOL(pwm_free);
133 #define pwm_tcon_start(pwm) (1 << (pwm->tcon_base + 0))
134 #define pwm_tcon_invert(pwm) (1 << (pwm->tcon_base + 2))
135 #define pwm_tcon_autoreload(pwm) (1 << (pwm->tcon_base + 3))
136 #define pwm_tcon_manulupdate(pwm) (1 << (pwm->tcon_base + 1))
138 int pwm_enable(struct pwm_device *pwm)
140 unsigned long flags;
141 unsigned long tcon;
143 local_irq_save(flags);
145 tcon = __raw_readl(S3C2410_TCON);
146 tcon |= pwm_tcon_start(pwm);
147 __raw_writel(tcon, S3C2410_TCON);
149 local_irq_restore(flags);
151 pwm->running = 1;
152 return 0;
155 EXPORT_SYMBOL(pwm_enable);
157 void pwm_disable(struct pwm_device *pwm)
159 unsigned long flags;
160 unsigned long tcon;
162 local_irq_save(flags);
164 tcon = __raw_readl(S3C2410_TCON);
165 tcon &= ~pwm_tcon_start(pwm);
166 __raw_writel(tcon, S3C2410_TCON);
168 local_irq_restore(flags);
170 pwm->running = 0;
173 EXPORT_SYMBOL(pwm_disable);
175 static unsigned long pwm_calc_tin(struct pwm_device *pwm, unsigned long freq)
177 unsigned long tin_parent_rate;
178 unsigned int div;
180 tin_parent_rate = clk_get_rate(clk_get_parent(pwm->clk_div));
181 pwm_dbg(pwm, "tin parent at %lu\n", tin_parent_rate);
183 for (div = 2; div <= 16; div *= 2) {
184 if ((tin_parent_rate / (div << 16)) < freq)
185 return tin_parent_rate / div;
188 return tin_parent_rate / 16;
191 #define NS_IN_HZ (1000000000UL)
193 int pwm_config(struct pwm_device *pwm, int duty_ns, int period_ns)
195 unsigned long tin_rate;
196 unsigned long tin_ns;
197 unsigned long period;
198 unsigned long flags;
199 unsigned long tcon;
200 unsigned long tcnt;
201 long tcmp;
203 /* We currently avoid using 64bit arithmetic by using the
204 * fact that anything faster than 1Hz is easily representable
205 * by 32bits. */
207 if (period_ns > NS_IN_HZ || duty_ns > NS_IN_HZ)
208 return -ERANGE;
210 if (duty_ns > period_ns)
211 return -EINVAL;
213 if (period_ns == pwm->period_ns &&
214 duty_ns == pwm->duty_ns)
215 return 0;
217 /* The TCMP and TCNT can be read without a lock, they're not
218 * shared between the timers. */
220 tcmp = __raw_readl(S3C2410_TCMPB(pwm->pwm_id));
221 tcnt = __raw_readl(S3C2410_TCNTB(pwm->pwm_id));
223 period = NS_IN_HZ / period_ns;
225 pwm_dbg(pwm, "duty_ns=%d, period_ns=%d (%lu)\n",
226 duty_ns, period_ns, period);
228 /* Check to see if we are changing the clock rate of the PWM */
230 if (pwm->period_ns != period_ns) {
231 if (pwm_is_tdiv(pwm)) {
232 tin_rate = pwm_calc_tin(pwm, period);
233 clk_set_rate(pwm->clk_div, tin_rate);
234 } else
235 tin_rate = clk_get_rate(pwm->clk);
237 pwm->period_ns = period_ns;
239 pwm_dbg(pwm, "tin_rate=%lu\n", tin_rate);
241 tin_ns = NS_IN_HZ / tin_rate;
242 tcnt = period_ns / tin_ns;
243 } else
244 tin_ns = NS_IN_HZ / clk_get_rate(pwm->clk);
246 /* Note, counters count down */
248 tcmp = duty_ns / tin_ns;
249 tcmp = tcnt - tcmp;
250 /* the pwm hw only checks the compare register after a decrement,
251 so the pin never toggles if tcmp = tcnt */
252 if (tcmp == tcnt)
253 tcmp--;
255 pwm_dbg(pwm, "tin_ns=%lu, tcmp=%ld/%lu\n", tin_ns, tcmp, tcnt);
257 if (tcmp < 0)
258 tcmp = 0;
260 /* Update the PWM register block. */
262 local_irq_save(flags);
264 __raw_writel(tcmp, S3C2410_TCMPB(pwm->pwm_id));
265 __raw_writel(tcnt, S3C2410_TCNTB(pwm->pwm_id));
267 tcon = __raw_readl(S3C2410_TCON);
268 tcon |= pwm_tcon_manulupdate(pwm);
269 tcon |= pwm_tcon_autoreload(pwm);
270 __raw_writel(tcon, S3C2410_TCON);
272 tcon &= ~pwm_tcon_manulupdate(pwm);
273 __raw_writel(tcon, S3C2410_TCON);
275 local_irq_restore(flags);
277 return 0;
280 EXPORT_SYMBOL(pwm_config);
282 static int pwm_register(struct pwm_device *pwm)
284 pwm->duty_ns = -1;
285 pwm->period_ns = -1;
287 mutex_lock(&pwm_lock);
288 list_add_tail(&pwm->list, &pwm_list);
289 mutex_unlock(&pwm_lock);
291 return 0;
294 static int s3c_pwm_probe(struct platform_device *pdev)
296 struct device *dev = &pdev->dev;
297 struct pwm_device *pwm;
298 unsigned long flags;
299 unsigned long tcon;
300 unsigned int id = pdev->id;
301 int ret;
303 if (id == 4) {
304 dev_err(dev, "TIMER4 is currently not supported\n");
305 return -ENXIO;
308 pwm = kzalloc(sizeof(struct pwm_device), GFP_KERNEL);
309 if (pwm == NULL) {
310 dev_err(dev, "failed to allocate pwm_device\n");
311 return -ENOMEM;
314 pwm->pdev = pdev;
315 pwm->pwm_id = id;
317 /* calculate base of control bits in TCON */
318 pwm->tcon_base = id == 0 ? 0 : (id * 4) + 4;
320 pwm->clk = clk_get(dev, "pwm-tin");
321 if (IS_ERR(pwm->clk)) {
322 dev_err(dev, "failed to get pwm tin clk\n");
323 ret = PTR_ERR(pwm->clk);
324 goto err_alloc;
327 pwm->clk_div = clk_get(dev, "pwm-tdiv");
328 if (IS_ERR(pwm->clk_div)) {
329 dev_err(dev, "failed to get pwm tdiv clk\n");
330 ret = PTR_ERR(pwm->clk_div);
331 goto err_clk_tin;
334 local_irq_save(flags);
336 tcon = __raw_readl(S3C2410_TCON);
337 tcon |= pwm_tcon_invert(pwm);
338 __raw_writel(tcon, S3C2410_TCON);
340 local_irq_restore(flags);
343 ret = pwm_register(pwm);
344 if (ret) {
345 dev_err(dev, "failed to register pwm\n");
346 goto err_clk_tdiv;
349 pwm_dbg(pwm, "config bits %02x\n",
350 (__raw_readl(S3C2410_TCON) >> pwm->tcon_base) & 0x0f);
352 dev_info(dev, "tin at %lu, tdiv at %lu, tin=%sclk, base %d\n",
353 clk_get_rate(pwm->clk),
354 clk_get_rate(pwm->clk_div),
355 pwm_is_tdiv(pwm) ? "div" : "ext", pwm->tcon_base);
357 platform_set_drvdata(pdev, pwm);
358 return 0;
360 err_clk_tdiv:
361 clk_put(pwm->clk_div);
363 err_clk_tin:
364 clk_put(pwm->clk);
366 err_alloc:
367 kfree(pwm);
368 return ret;
371 static int s3c_pwm_remove(struct platform_device *pdev)
373 struct pwm_device *pwm = platform_get_drvdata(pdev);
375 clk_put(pwm->clk_div);
376 clk_put(pwm->clk);
377 kfree(pwm);
379 return 0;
382 static struct platform_driver s3c_pwm_driver = {
383 .driver = {
384 .name = "s3c24xx-pwm",
385 .owner = THIS_MODULE,
387 .probe = s3c_pwm_probe,
388 .remove = __devexit_p(s3c_pwm_remove),
391 static int __init pwm_init(void)
393 int ret;
395 clk_scaler[0] = clk_get(NULL, "pwm-scaler0");
396 clk_scaler[1] = clk_get(NULL, "pwm-scaler1");
398 if (IS_ERR(clk_scaler[0]) || IS_ERR(clk_scaler[1])) {
399 printk(KERN_ERR "%s: failed to get scaler clocks\n", __func__);
400 return -EINVAL;
403 ret = platform_driver_register(&s3c_pwm_driver);
404 if (ret)
405 printk(KERN_ERR "%s: failed to add pwm driver\n", __func__);
407 return ret;
410 arch_initcall(pwm_init);