mm: move shmem prototypes to shmem_fs.h
[linux/fpc-iii.git] / arch / arm / plat-samsung / pwm.c
blobf37457c52064f77b5e58139a2eb0008f695d905c
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/map.h>
25 #include <plat/regs-timer.h>
27 struct pwm_device {
28 struct list_head list;
29 struct platform_device *pdev;
31 struct clk *clk_div;
32 struct clk *clk;
33 const char *label;
35 unsigned int period_ns;
36 unsigned int duty_ns;
38 unsigned char tcon_base;
39 unsigned char running;
40 unsigned char use_count;
41 unsigned char pwm_id;
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;
59 int found = 0;
61 mutex_lock(&pwm_lock);
63 list_for_each_entry(pwm, &pwm_list, list) {
64 if (pwm->pwm_id == pwm_id) {
65 found = 1;
66 break;
70 if (found) {
71 if (pwm->use_count == 0) {
72 pwm->use_count = 1;
73 pwm->label = label;
74 } else
75 pwm = ERR_PTR(-EBUSY);
76 } else
77 pwm = ERR_PTR(-ENOENT);
79 mutex_unlock(&pwm_lock);
80 return pwm;
83 EXPORT_SYMBOL(pwm_request);
86 void pwm_free(struct pwm_device *pwm)
88 mutex_lock(&pwm_lock);
90 if (pwm->use_count) {
91 pwm->use_count--;
92 pwm->label = NULL;
93 } else
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)
108 unsigned long flags;
109 unsigned long tcon;
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);
119 pwm->running = 1;
120 return 0;
123 EXPORT_SYMBOL(pwm_enable);
125 void pwm_disable(struct pwm_device *pwm)
127 unsigned long flags;
128 unsigned long tcon;
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);
138 pwm->running = 0;
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;
146 unsigned int div;
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;
166 unsigned long flags;
167 unsigned long tcon;
168 unsigned long tcnt;
169 long tcmp;
171 /* We currently avoid using 64bit arithmetic by using the
172 * fact that anything faster than 1Hz is easily representable
173 * by 32bits. */
175 if (period_ns > NS_IN_HZ || duty_ns > NS_IN_HZ)
176 return -ERANGE;
178 if (duty_ns > period_ns)
179 return -EINVAL;
181 if (period_ns == pwm->period_ns &&
182 duty_ns == pwm->duty_ns)
183 return 0;
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);
202 } else
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;
211 } else
212 tin_ns = NS_IN_HZ / clk_get_rate(pwm->clk);
214 /* Note, counters count down */
216 tcmp = duty_ns / tin_ns;
217 tcmp = tcnt - tcmp;
218 /* the pwm hw only checks the compare register after a decrement,
219 so the pin never toggles if tcmp = tcnt */
220 if (tcmp == tcnt)
221 tcmp--;
223 pwm_dbg(pwm, "tin_ns=%lu, tcmp=%ld/%lu\n", tin_ns, tcmp, tcnt);
225 if (tcmp < 0)
226 tcmp = 0;
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);
245 return 0;
248 EXPORT_SYMBOL(pwm_config);
250 static int pwm_register(struct pwm_device *pwm)
252 pwm->duty_ns = -1;
253 pwm->period_ns = -1;
255 mutex_lock(&pwm_lock);
256 list_add_tail(&pwm->list, &pwm_list);
257 mutex_unlock(&pwm_lock);
259 return 0;
262 static int s3c_pwm_probe(struct platform_device *pdev)
264 struct device *dev = &pdev->dev;
265 struct pwm_device *pwm;
266 unsigned long flags;
267 unsigned long tcon;
268 unsigned int id = pdev->id;
269 int ret;
271 if (id == 4) {
272 dev_err(dev, "TIMER4 is currently not supported\n");
273 return -ENXIO;
276 pwm = kzalloc(sizeof(struct pwm_device), GFP_KERNEL);
277 if (pwm == NULL) {
278 dev_err(dev, "failed to allocate pwm_device\n");
279 return -ENOMEM;
282 pwm->pdev = pdev;
283 pwm->pwm_id = id;
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);
292 goto err_alloc;
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);
299 goto err_clk_tin;
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);
312 if (ret) {
313 dev_err(dev, "failed to register pwm\n");
314 goto err_clk_tdiv;
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);
326 return 0;
328 err_clk_tdiv:
329 clk_put(pwm->clk_div);
331 err_clk_tin:
332 clk_put(pwm->clk);
334 err_alloc:
335 kfree(pwm);
336 return ret;
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);
344 clk_put(pwm->clk);
345 kfree(pwm);
347 return 0;
350 #ifdef CONFIG_PM
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
359 pwm->period_ns = 0;
360 pwm->duty_ns = 0;
362 return 0;
365 static int s3c_pwm_resume(struct platform_device *pdev)
367 struct pwm_device *pwm = platform_get_drvdata(pdev);
368 unsigned long tcon;
370 /* Restore invertion */
371 tcon = __raw_readl(S3C2410_TCON);
372 tcon |= pwm_tcon_invert(pwm);
373 __raw_writel(tcon, S3C2410_TCON);
375 return 0;
378 #else
379 #define s3c_pwm_suspend NULL
380 #define s3c_pwm_resume NULL
381 #endif
383 static struct platform_driver s3c_pwm_driver = {
384 .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)
396 int ret;
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__);
403 return -EINVAL;
406 ret = platform_driver_register(&s3c_pwm_driver);
407 if (ret)
408 printk(KERN_ERR "%s: failed to add pwm driver\n", __func__);
410 return ret;
413 arch_initcall(pwm_init);