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>
21 #include <linux/pwm.h>
25 #include <plat/regs-timer.h>
28 struct list_head list;
29 struct platform_device *pdev;
35 unsigned int period_ns;
38 unsigned char tcon_base;
39 unsigned char running;
40 unsigned char use_count;
44 #define pwm_dbg(_pwm, msg...) dev_dbg(&(_pwm)->pdev->dev, msg)
46 static struct clk *clk_scaler[2];
48 static inline int pwm_is_tdiv(struct pwm_device *pwm)
50 return clk_get_parent(pwm->clk) == pwm->clk_div;
53 static DEFINE_MUTEX(pwm_lock);
54 static LIST_HEAD(pwm_list);
56 struct pwm_device *pwm_request(int pwm_id, const char *label)
58 struct pwm_device *pwm;
61 mutex_lock(&pwm_lock);
63 list_for_each_entry(pwm, &pwm_list, list) {
64 if (pwm->pwm_id == pwm_id) {
71 if (pwm->use_count == 0) {
75 pwm = ERR_PTR(-EBUSY);
77 pwm = ERR_PTR(-ENOENT);
79 mutex_unlock(&pwm_lock);
83 EXPORT_SYMBOL(pwm_request);
86 void pwm_free(struct pwm_device *pwm)
88 mutex_lock(&pwm_lock);
94 printk(KERN_ERR "PWM%d device already freed\n", pwm->pwm_id);
96 mutex_unlock(&pwm_lock);
99 EXPORT_SYMBOL(pwm_free);
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))
106 int pwm_enable(struct pwm_device *pwm)
111 local_irq_save(flags);
113 tcon = __raw_readl(S3C2410_TCON);
114 tcon |= pwm_tcon_start(pwm);
115 __raw_writel(tcon, S3C2410_TCON);
117 local_irq_restore(flags);
123 EXPORT_SYMBOL(pwm_enable);
125 void pwm_disable(struct pwm_device *pwm)
130 local_irq_save(flags);
132 tcon = __raw_readl(S3C2410_TCON);
133 tcon &= ~pwm_tcon_start(pwm);
134 __raw_writel(tcon, S3C2410_TCON);
136 local_irq_restore(flags);
141 EXPORT_SYMBOL(pwm_disable);
143 static unsigned long pwm_calc_tin(struct pwm_device *pwm, unsigned long freq)
145 unsigned long tin_parent_rate;
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);
151 for (div = 2; div <= 16; div *= 2) {
152 if ((tin_parent_rate / (div << 16)) < freq)
153 return tin_parent_rate / div;
156 return tin_parent_rate / 16;
159 #define NS_IN_HZ (1000000000UL)
161 int pwm_config(struct pwm_device *pwm, int duty_ns, int period_ns)
163 unsigned long tin_rate;
164 unsigned long tin_ns;
165 unsigned long period;
171 /* We currently avoid using 64bit arithmetic by using the
172 * fact that anything faster than 1Hz is easily representable
175 if (period_ns > NS_IN_HZ || duty_ns > NS_IN_HZ)
178 if (duty_ns > period_ns)
181 if (period_ns == pwm->period_ns &&
182 duty_ns == pwm->duty_ns)
185 /* The TCMP and TCNT can be read without a lock, they're not
186 * shared between the timers. */
188 tcmp = __raw_readl(S3C2410_TCMPB(pwm->pwm_id));
189 tcnt = __raw_readl(S3C2410_TCNTB(pwm->pwm_id));
191 period = NS_IN_HZ / period_ns;
193 pwm_dbg(pwm, "duty_ns=%d, period_ns=%d (%lu)\n",
194 duty_ns, period_ns, period);
196 /* Check to see if we are changing the clock rate of the PWM */
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);
203 tin_rate = clk_get_rate(pwm->clk);
205 pwm->period_ns = period_ns;
207 pwm_dbg(pwm, "tin_rate=%lu\n", tin_rate);
209 tin_ns = NS_IN_HZ / tin_rate;
210 tcnt = period_ns / tin_ns;
212 tin_ns = NS_IN_HZ / clk_get_rate(pwm->clk);
214 /* Note, counters count down */
216 tcmp = duty_ns / tin_ns;
218 /* the pwm hw only checks the compare register after a decrement,
219 so the pin never toggles if tcmp = tcnt */
223 pwm_dbg(pwm, "tin_ns=%lu, tcmp=%ld/%lu\n", tin_ns, tcmp, tcnt);
228 /* Update the PWM register block. */
230 local_irq_save(flags);
232 __raw_writel(tcmp, S3C2410_TCMPB(pwm->pwm_id));
233 __raw_writel(tcnt, S3C2410_TCNTB(pwm->pwm_id));
235 tcon = __raw_readl(S3C2410_TCON);
236 tcon |= pwm_tcon_manulupdate(pwm);
237 tcon |= pwm_tcon_autoreload(pwm);
238 __raw_writel(tcon, S3C2410_TCON);
240 tcon &= ~pwm_tcon_manulupdate(pwm);
241 __raw_writel(tcon, S3C2410_TCON);
243 local_irq_restore(flags);
248 EXPORT_SYMBOL(pwm_config);
250 static int pwm_register(struct pwm_device *pwm)
255 mutex_lock(&pwm_lock);
256 list_add_tail(&pwm->list, &pwm_list);
257 mutex_unlock(&pwm_lock);
262 static int s3c_pwm_probe(struct platform_device *pdev)
264 struct device *dev = &pdev->dev;
265 struct pwm_device *pwm;
268 unsigned int id = pdev->id;
272 dev_err(dev, "TIMER4 is currently not supported\n");
276 pwm = kzalloc(sizeof(struct pwm_device), GFP_KERNEL);
278 dev_err(dev, "failed to allocate pwm_device\n");
285 /* calculate base of control bits in TCON */
286 pwm->tcon_base = id == 0 ? 0 : (id * 4) + 4;
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);
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);
302 local_irq_save(flags);
304 tcon = __raw_readl(S3C2410_TCON);
305 tcon |= pwm_tcon_invert(pwm);
306 __raw_writel(tcon, S3C2410_TCON);
308 local_irq_restore(flags);
311 ret = pwm_register(pwm);
313 dev_err(dev, "failed to register pwm\n");
317 pwm_dbg(pwm, "config bits %02x\n",
318 (__raw_readl(S3C2410_TCON) >> pwm->tcon_base) & 0x0f);
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);
325 platform_set_drvdata(pdev, pwm);
329 clk_put(pwm->clk_div);
339 static int __devexit s3c_pwm_remove(struct platform_device *pdev)
341 struct pwm_device *pwm = platform_get_drvdata(pdev);
343 clk_put(pwm->clk_div);
351 static int s3c_pwm_suspend(struct platform_device *pdev, pm_message_t state)
353 struct pwm_device *pwm = platform_get_drvdata(pdev);
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
365 static int s3c_pwm_resume(struct platform_device *pdev)
367 struct pwm_device *pwm = platform_get_drvdata(pdev);
370 /* Restore invertion */
371 tcon = __raw_readl(S3C2410_TCON);
372 tcon |= pwm_tcon_invert(pwm);
373 __raw_writel(tcon, S3C2410_TCON);
379 #define s3c_pwm_suspend NULL
380 #define s3c_pwm_resume NULL
383 static struct platform_driver s3c_pwm_driver = {
385 .name = "s3c24xx-pwm",
386 .owner = THIS_MODULE,
388 .probe = s3c_pwm_probe,
389 .remove = __devexit_p(s3c_pwm_remove),
390 .suspend = s3c_pwm_suspend,
391 .resume = s3c_pwm_resume,
394 static int __init pwm_init(void)
398 clk_scaler[0] = clk_get(NULL, "pwm-scaler0");
399 clk_scaler[1] = clk_get(NULL, "pwm-scaler1");
401 if (IS_ERR(clk_scaler[0]) || IS_ERR(clk_scaler[1])) {
402 printk(KERN_ERR "%s: failed to get scaler clocks\n", __func__);
406 ret = platform_driver_register(&s3c_pwm_driver);
408 printk(KERN_ERR "%s: failed to add pwm driver\n", __func__);
413 arch_initcall(pwm_init);