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-s3c/regs-timer.h>
25 struct list_head list;
26 struct platform_device *pdev;
32 unsigned int period_ns;
35 unsigned char tcon_base;
36 unsigned char running;
37 unsigned char use_count;
41 #define pwm_dbg(_pwm, msg...) dev_info(&(_pwm)->pdev->dev, msg)
43 static struct clk *clk_scaler[2];
45 /* Standard setup for a timer block. */
47 #define TIMER_RESOURCE_SIZE (1)
49 #define TIMER_RESOURCE(_tmr, _irq) \
50 (struct resource [TIMER_RESOURCE_SIZE]) { \
54 .flags = IORESOURCE_IRQ \
58 #define DEFINE_TIMER(_tmr_no, _irq) \
59 .name = "s3c24xx-pwm", \
61 .num_resources = TIMER_RESOURCE_SIZE, \
62 .resource = TIMER_RESOURCE(_tmr_no, _irq), \
64 /* since we already have an static mapping for the timer, we do not
65 * bother setting any IO resource for the base.
68 struct platform_device s3c_device_timer[] = {
69 [0] = { DEFINE_TIMER(0, IRQ_TIMER0) },
70 [1] = { DEFINE_TIMER(1, IRQ_TIMER1) },
71 [2] = { DEFINE_TIMER(2, IRQ_TIMER2) },
72 [3] = { DEFINE_TIMER(3, IRQ_TIMER3) },
73 [4] = { DEFINE_TIMER(4, IRQ_TIMER4) },
76 static inline int pwm_is_tdiv(struct pwm_device *pwm)
78 return clk_get_parent(pwm->clk) == pwm->clk_div;
81 static DEFINE_MUTEX(pwm_lock);
82 static LIST_HEAD(pwm_list);
84 struct pwm_device *pwm_request(int pwm_id, const char *label)
86 struct pwm_device *pwm;
89 mutex_lock(&pwm_lock);
91 list_for_each_entry(pwm, &pwm_list, list) {
92 if (pwm->pwm_id == pwm_id) {
99 if (pwm->use_count == 0) {
103 pwm = ERR_PTR(-EBUSY);
105 pwm = ERR_PTR(-ENOENT);
107 mutex_unlock(&pwm_lock);
111 EXPORT_SYMBOL(pwm_request);
114 void pwm_free(struct pwm_device *pwm)
116 mutex_lock(&pwm_lock);
118 if (pwm->use_count) {
122 printk(KERN_ERR "PWM%d device already freed\n", pwm->pwm_id);
124 mutex_unlock(&pwm_lock);
127 EXPORT_SYMBOL(pwm_free);
129 #define pwm_tcon_start(pwm) (1 << (pwm->tcon_base + 0))
130 #define pwm_tcon_invert(pwm) (1 << (pwm->tcon_base + 2))
131 #define pwm_tcon_autoreload(pwm) (1 << (pwm->tcon_base + 3))
132 #define pwm_tcon_manulupdate(pwm) (1 << (pwm->tcon_base + 1))
134 int pwm_enable(struct pwm_device *pwm)
139 local_irq_save(flags);
141 tcon = __raw_readl(S3C2410_TCON);
142 tcon |= pwm_tcon_start(pwm);
143 __raw_writel(tcon, S3C2410_TCON);
145 local_irq_restore(flags);
151 EXPORT_SYMBOL(pwm_enable);
153 void pwm_disable(struct pwm_device *pwm)
158 local_irq_save(flags);
160 tcon = __raw_readl(S3C2410_TCON);
161 tcon &= ~pwm_tcon_start(pwm);
162 __raw_writel(tcon, S3C2410_TCON);
164 local_irq_restore(flags);
169 EXPORT_SYMBOL(pwm_disable);
171 unsigned long pwm_calc_tin(struct pwm_device *pwm, unsigned long freq)
173 unsigned long tin_parent_rate;
176 tin_parent_rate = clk_get_rate(clk_get_parent(pwm->clk_div));
177 pwm_dbg(pwm, "tin parent at %lu\n", tin_parent_rate);
179 for (div = 2; div <= 16; div *= 2) {
180 if ((tin_parent_rate / (div << 16)) < freq)
181 return tin_parent_rate / div;
184 return tin_parent_rate / 16;
187 #define NS_IN_HZ (1000000000UL)
189 int pwm_config(struct pwm_device *pwm, int duty_ns, int period_ns)
191 unsigned long tin_rate;
192 unsigned long tin_ns;
193 unsigned long period;
199 /* We currently avoid using 64bit arithmetic by using the
200 * fact that anything faster than 1Hz is easily representable
203 if (period_ns > NS_IN_HZ || duty_ns > NS_IN_HZ)
206 if (duty_ns > period_ns)
209 if (period_ns == pwm->period_ns &&
210 duty_ns == pwm->duty_ns)
213 /* The TCMP and TCNT can be read without a lock, they're not
214 * shared between the timers. */
216 tcmp = __raw_readl(S3C2410_TCMPB(pwm->pwm_id));
217 tcnt = __raw_readl(S3C2410_TCNTB(pwm->pwm_id));
219 period = NS_IN_HZ / period_ns;
221 pwm_dbg(pwm, "duty_ns=%d, period_ns=%d (%lu)\n",
222 duty_ns, period_ns, period);
224 /* Check to see if we are changing the clock rate of the PWM */
226 if (pwm->period_ns != period_ns) {
227 if (pwm_is_tdiv(pwm)) {
228 tin_rate = pwm_calc_tin(pwm, period);
229 clk_set_rate(pwm->clk_div, tin_rate);
231 tin_rate = clk_get_rate(pwm->clk);
233 pwm->period_ns = period_ns;
235 pwm_dbg(pwm, "tin_rate=%lu\n", tin_rate);
237 tin_ns = NS_IN_HZ / tin_rate;
238 tcnt = period_ns / tin_ns;
240 tin_ns = NS_IN_HZ / clk_get_rate(pwm->clk);
242 /* Note, counters count down */
244 tcmp = duty_ns / tin_ns;
247 pwm_dbg(pwm, "tin_ns=%lu, tcmp=%ld/%lu\n", tin_ns, tcmp, tcnt);
252 /* Update the PWM register block. */
254 local_irq_save(flags);
256 __raw_writel(tcmp, S3C2410_TCMPB(pwm->pwm_id));
257 __raw_writel(tcnt, S3C2410_TCNTB(pwm->pwm_id));
259 tcon = __raw_readl(S3C2410_TCON);
260 tcon |= pwm_tcon_manulupdate(pwm);
261 tcon |= pwm_tcon_autoreload(pwm);
262 __raw_writel(tcon, S3C2410_TCON);
264 tcon &= ~pwm_tcon_manulupdate(pwm);
265 __raw_writel(tcon, S3C2410_TCON);
267 local_irq_restore(flags);
272 EXPORT_SYMBOL(pwm_config);
274 static int pwm_register(struct pwm_device *pwm)
279 mutex_lock(&pwm_lock);
280 list_add_tail(&pwm->list, &pwm_list);
281 mutex_unlock(&pwm_lock);
286 static int s3c_pwm_probe(struct platform_device *pdev)
288 struct device *dev = &pdev->dev;
289 struct pwm_device *pwm;
292 unsigned int id = pdev->id;
296 dev_err(dev, "TIMER4 is currently not supported\n");
300 pwm = kzalloc(sizeof(struct pwm_device), GFP_KERNEL);
302 dev_err(dev, "failed to allocate pwm_device\n");
309 /* calculate base of control bits in TCON */
310 pwm->tcon_base = id == 0 ? 0 : (id * 4) + 4;
312 pwm->clk = clk_get(dev, "pwm-tin");
313 if (IS_ERR(pwm->clk)) {
314 dev_err(dev, "failed to get pwm tin clk\n");
315 ret = PTR_ERR(pwm->clk);
319 pwm->clk_div = clk_get(dev, "pwm-tdiv");
320 if (IS_ERR(pwm->clk_div)) {
321 dev_err(dev, "failed to get pwm tdiv clk\n");
322 ret = PTR_ERR(pwm->clk_div);
326 local_irq_save(flags);
328 tcon = __raw_readl(S3C2410_TCON);
329 tcon |= pwm_tcon_invert(pwm);
330 __raw_writel(tcon, S3C2410_TCON);
332 local_irq_restore(flags);
335 ret = pwm_register(pwm);
337 dev_err(dev, "failed to register pwm\n");
341 pwm_dbg(pwm, "config bits %02x\n",
342 (__raw_readl(S3C2410_TCON) >> pwm->tcon_base) & 0x0f);
344 dev_info(dev, "tin at %lu, tdiv at %lu, tin=%sclk, base %d\n",
345 clk_get_rate(pwm->clk),
346 clk_get_rate(pwm->clk_div),
347 pwm_is_tdiv(pwm) ? "div" : "ext", pwm->tcon_base);
349 platform_set_drvdata(pdev, pwm);
353 clk_put(pwm->clk_div);
363 static int s3c_pwm_remove(struct platform_device *pdev)
365 struct pwm_device *pwm = platform_get_drvdata(pdev);
367 clk_put(pwm->clk_div);
374 static struct platform_driver s3c_pwm_driver = {
376 .name = "s3c24xx-pwm",
377 .owner = THIS_MODULE,
379 .probe = s3c_pwm_probe,
380 .remove = __devexit_p(s3c_pwm_remove),
383 static int __init pwm_init(void)
387 clk_scaler[0] = clk_get(NULL, "pwm-scaler0");
388 clk_scaler[1] = clk_get(NULL, "pwm-scaler1");
390 if (IS_ERR(clk_scaler[0]) || IS_ERR(clk_scaler[1])) {
391 printk(KERN_ERR "%s: failed to get scaler clocks\n", __func__);
395 ret = platform_driver_register(&s3c_pwm_driver);
397 printk(KERN_ERR "%s: failed to add pwm driver\n", __func__);
402 arch_initcall(pwm_init);