1 /* arch/arm/plat-s3c24xx/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 * S3C24XX 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>
20 #include <linux/pwm.h>
22 #include <asm/plat-s3c24xx/devs.h>
23 #include <asm/plat-s3c/regs-timer.h>
26 struct list_head list;
27 struct platform_device *pdev;
33 unsigned int period_ns;
36 unsigned char tcon_base;
37 unsigned char running;
38 unsigned char use_count;
42 #define pwm_dbg(_pwm, msg...) dev_dbg(&(_pwm)->pdev->dev, msg)
44 static struct clk *clk_scaler[2];
46 /* Standard setup for a timer block. */
48 #define TIMER_RESOURCE_SIZE (1)
50 #define TIMER_RESOURCE(_tmr, _irq) \
51 (struct resource [TIMER_RESOURCE_SIZE]) { \
55 .flags = IORESOURCE_IRQ \
59 #define DEFINE_TIMER(_tmr_no, _irq) \
60 .name = "s3c24xx-pwm", \
62 .num_resources = TIMER_RESOURCE_SIZE, \
63 .resource = TIMER_RESOURCE(_tmr_no, _irq), \
65 /* since we already have an static mapping for the timer, we do not
66 * bother setting any IO resource for the base.
69 struct platform_device s3c_device_timer[] = {
70 [0] = { DEFINE_TIMER(0, IRQ_TIMER0) },
71 [1] = { DEFINE_TIMER(1, IRQ_TIMER1) },
72 [2] = { DEFINE_TIMER(2, IRQ_TIMER2) },
73 [3] = { DEFINE_TIMER(3, IRQ_TIMER3) },
74 [4] = { DEFINE_TIMER(4, IRQ_TIMER4) },
77 static inline int pwm_is_tdiv(struct pwm_device *pwm)
79 return clk_get_parent(pwm->clk) == pwm->clk_div;
82 static DEFINE_MUTEX(pwm_lock);
83 static LIST_HEAD(pwm_list);
85 struct pwm_device *pwm_request(int pwm_id, const char *label)
87 struct pwm_device *pwm;
90 mutex_lock(&pwm_lock);
92 list_for_each_entry(pwm, &pwm_list, list) {
93 if (pwm->pwm_id == pwm_id) {
100 if (pwm->use_count == 0) {
104 pwm = ERR_PTR(-EBUSY);
106 pwm = ERR_PTR(-ENOENT);
108 mutex_unlock(&pwm_lock);
112 EXPORT_SYMBOL(pwm_request);
115 void pwm_free(struct pwm_device *pwm)
117 mutex_lock(&pwm_lock);
119 if (pwm->use_count) {
123 printk(KERN_ERR "PWM%d device already freed\n", pwm->pwm_id);
125 mutex_unlock(&pwm_lock);
128 EXPORT_SYMBOL(pwm_free);
130 #define pwm_tcon_start(pwm) (1 << (pwm->tcon_base + 0))
131 #define pwm_tcon_invert(pwm) (1 << (pwm->tcon_base + 2))
132 #define pwm_tcon_autoreload(pwm) (1 << (pwm->tcon_base + 3))
133 #define pwm_tcon_manulupdate(pwm) (1 << (pwm->tcon_base + 1))
135 int pwm_enable(struct pwm_device *pwm)
140 local_irq_save(flags);
142 tcon = __raw_readl(S3C2410_TCON);
143 tcon |= pwm_tcon_start(pwm);
144 __raw_writel(tcon, S3C2410_TCON);
146 local_irq_restore(flags);
152 EXPORT_SYMBOL(pwm_enable);
154 void pwm_disable(struct pwm_device *pwm)
159 local_irq_save(flags);
161 tcon = __raw_readl(S3C2410_TCON);
162 tcon &= ~pwm_tcon_start(pwm);
163 __raw_writel(tcon, S3C2410_TCON);
165 local_irq_restore(flags);
170 EXPORT_SYMBOL(pwm_disable);
172 static unsigned long pwm_calc_tin(struct pwm_device *pwm, unsigned long freq)
174 unsigned long tin_parent_rate;
177 tin_parent_rate = clk_get_rate(clk_get_parent(pwm->clk_div));
178 pwm_dbg(pwm, "tin parent at %lu\n", tin_parent_rate);
180 for (div = 2; div <= 16; div *= 2) {
181 if ((tin_parent_rate / (div << 16)) < freq)
182 return tin_parent_rate / div;
185 return tin_parent_rate / 16;
188 #define NS_IN_HZ (1000000000UL)
190 int pwm_config(struct pwm_device *pwm, int duty_ns, int period_ns)
192 unsigned long tin_rate;
193 unsigned long tin_ns;
194 unsigned long period;
200 /* We currently avoid using 64bit arithmetic by using the
201 * fact that anything faster than 1Hz is easily representable
204 if (period_ns > NS_IN_HZ || duty_ns > NS_IN_HZ)
207 if (duty_ns > period_ns)
210 if (period_ns == pwm->period_ns &&
211 duty_ns == pwm->duty_ns)
214 /* The TCMP and TCNT can be read without a lock, they're not
215 * shared between the timers. */
217 tcmp = __raw_readl(S3C2410_TCMPB(pwm->pwm_id));
218 tcnt = __raw_readl(S3C2410_TCNTB(pwm->pwm_id));
220 period = NS_IN_HZ / period_ns;
222 pwm_dbg(pwm, "duty_ns=%d, period_ns=%d (%lu)\n",
223 duty_ns, period_ns, period);
225 /* Check to see if we are changing the clock rate of the PWM */
227 if (pwm->period_ns != period_ns) {
228 if (pwm_is_tdiv(pwm)) {
229 tin_rate = pwm_calc_tin(pwm, period);
230 clk_set_rate(pwm->clk_div, tin_rate);
232 tin_rate = clk_get_rate(pwm->clk);
234 pwm->period_ns = period_ns;
236 pwm_dbg(pwm, "tin_rate=%lu\n", tin_rate);
238 tin_ns = NS_IN_HZ / tin_rate;
239 tcnt = period_ns / tin_ns;
241 tin_ns = NS_IN_HZ / clk_get_rate(pwm->clk);
243 /* Note, counters count down */
245 tcmp = duty_ns / tin_ns;
248 pwm_dbg(pwm, "tin_ns=%lu, tcmp=%ld/%lu\n", tin_ns, tcmp, tcnt);
253 /* Update the PWM register block. */
255 local_irq_save(flags);
257 __raw_writel(tcmp, S3C2410_TCMPB(pwm->pwm_id));
258 __raw_writel(tcnt, S3C2410_TCNTB(pwm->pwm_id));
260 tcon = __raw_readl(S3C2410_TCON);
261 tcon |= pwm_tcon_manulupdate(pwm);
262 tcon |= pwm_tcon_autoreload(pwm);
263 __raw_writel(tcon, S3C2410_TCON);
265 tcon &= ~pwm_tcon_manulupdate(pwm);
266 __raw_writel(tcon, S3C2410_TCON);
268 local_irq_restore(flags);
273 EXPORT_SYMBOL(pwm_config);
275 static int pwm_register(struct pwm_device *pwm)
280 mutex_lock(&pwm_lock);
281 list_add_tail(&pwm->list, &pwm_list);
282 mutex_unlock(&pwm_lock);
287 static int s3c_pwm_probe(struct platform_device *pdev)
289 struct device *dev = &pdev->dev;
290 struct pwm_device *pwm;
293 unsigned int id = pdev->id;
297 dev_err(dev, "TIMER4 is currently not supported\n");
301 pwm = kzalloc(sizeof(struct pwm_device), GFP_KERNEL);
303 dev_err(dev, "failed to allocate pwm_device\n");
310 /* calculate base of control bits in TCON */
311 pwm->tcon_base = id == 0 ? 0 : (id * 4) + 4;
313 pwm->clk = clk_get(dev, "pwm-tin");
314 if (IS_ERR(pwm->clk)) {
315 dev_err(dev, "failed to get pwm tin clk\n");
316 ret = PTR_ERR(pwm->clk);
320 pwm->clk_div = clk_get(dev, "pwm-tdiv");
321 if (IS_ERR(pwm->clk_div)) {
322 dev_err(dev, "failed to get pwm tdiv clk\n");
323 ret = PTR_ERR(pwm->clk_div);
327 local_irq_save(flags);
329 tcon = __raw_readl(S3C2410_TCON);
330 tcon |= pwm_tcon_invert(pwm);
331 __raw_writel(tcon, S3C2410_TCON);
333 local_irq_restore(flags);
336 ret = pwm_register(pwm);
338 dev_err(dev, "failed to register pwm\n");
342 pwm_dbg(pwm, "config bits %02x\n",
343 (__raw_readl(S3C2410_TCON) >> pwm->tcon_base) & 0x0f);
345 dev_info(dev, "tin at %lu, tdiv at %lu, tin=%sclk, base %d\n",
346 clk_get_rate(pwm->clk),
347 clk_get_rate(pwm->clk_div),
348 pwm_is_tdiv(pwm) ? "div" : "ext", pwm->tcon_base);
350 platform_set_drvdata(pdev, pwm);
354 clk_put(pwm->clk_div);
364 static int s3c_pwm_remove(struct platform_device *pdev)
366 struct pwm_device *pwm = platform_get_drvdata(pdev);
368 clk_put(pwm->clk_div);
375 static struct platform_driver s3c_pwm_driver = {
377 .name = "s3c24xx-pwm",
378 .owner = THIS_MODULE,
380 .probe = s3c_pwm_probe,
381 .remove = __devexit_p(s3c_pwm_remove),
384 static int __init pwm_init(void)
388 clk_scaler[0] = clk_get(NULL, "pwm-scaler0");
389 clk_scaler[1] = clk_get(NULL, "pwm-scaler1");
391 if (IS_ERR(clk_scaler[0]) || IS_ERR(clk_scaler[1])) {
392 printk(KERN_ERR "%s: failed to get scaler clocks\n", __func__);
396 ret = platform_driver_register(&s3c_pwm_driver);
398 printk(KERN_ERR "%s: failed to add pwm driver\n", __func__);
403 arch_initcall(pwm_init);