blob: c559d8438c70ee2d873afc43a7d14ee1fc2002c2 [file] [log] [blame]
Peter Korsgaardb5ead1c2009-07-01 17:47:07 +02001/* arch/arm/plat-s3c/pwm.c
Ben Dooks6fc601e2008-07-01 13:17:24 +01002 *
3 * Copyright (c) 2007 Ben Dooks
4 * Copyright (c) 2008 Simtec Electronics
5 * Ben Dooks <ben@simtec.co.uk>, <ben-linux@fluff.org>
6 *
Peter Korsgaardb5ead1c2009-07-01 17:47:07 +02007 * S3C series PWM device core
Ben Dooks6fc601e2008-07-01 13:17:24 +01008 *
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.
12*/
13
Kukjin Kima69e4c22011-11-17 01:14:38 +090014#include <linux/export.h>
Ben Dooks6fc601e2008-07-01 13:17:24 +010015#include <linux/kernel.h>
16#include <linux/platform_device.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090017#include <linux/slab.h>
Ben Dooks6fc601e2008-07-01 13:17:24 +010018#include <linux/err.h>
19#include <linux/clk.h>
20#include <linux/io.h>
21#include <linux/pwm.h>
22
Peter Korsgaardb5ead1c2009-07-01 17:47:07 +020023#include <mach/map.h>
Russell King80b02c12009-01-08 10:01:47 +000024
Ben Dooksa2b7ba92008-10-07 22:26:09 +010025#include <plat/regs-timer.h>
Ben Dooks6fc601e2008-07-01 13:17:24 +010026
27struct pwm_device {
28 struct list_head list;
29 struct platform_device *pdev;
30
31 struct clk *clk_div;
32 struct clk *clk;
33 const char *label;
34
35 unsigned int period_ns;
36 unsigned int duty_ns;
37
38 unsigned char tcon_base;
39 unsigned char running;
40 unsigned char use_count;
41 unsigned char pwm_id;
42};
43
Ben Dooks66592ee2008-08-26 22:54:06 +010044#define pwm_dbg(_pwm, msg...) dev_dbg(&(_pwm)->pdev->dev, msg)
Ben Dooks6fc601e2008-07-01 13:17:24 +010045
46static struct clk *clk_scaler[2];
47
Ben Dooks6fc601e2008-07-01 13:17:24 +010048static inline int pwm_is_tdiv(struct pwm_device *pwm)
49{
50 return clk_get_parent(pwm->clk) == pwm->clk_div;
51}
52
53static DEFINE_MUTEX(pwm_lock);
54static LIST_HEAD(pwm_list);
55
56struct pwm_device *pwm_request(int pwm_id, const char *label)
57{
58 struct pwm_device *pwm;
59 int found = 0;
60
61 mutex_lock(&pwm_lock);
62
63 list_for_each_entry(pwm, &pwm_list, list) {
64 if (pwm->pwm_id == pwm_id) {
65 found = 1;
66 break;
67 }
68 }
69
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);
78
79 mutex_unlock(&pwm_lock);
80 return pwm;
81}
82
83EXPORT_SYMBOL(pwm_request);
84
85
86void pwm_free(struct pwm_device *pwm)
87{
88 mutex_lock(&pwm_lock);
89
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);
95
96 mutex_unlock(&pwm_lock);
97}
98
99EXPORT_SYMBOL(pwm_free);
100
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))
105
106int pwm_enable(struct pwm_device *pwm)
107{
108 unsigned long flags;
109 unsigned long tcon;
110
111 local_irq_save(flags);
112
113 tcon = __raw_readl(S3C2410_TCON);
114 tcon |= pwm_tcon_start(pwm);
115 __raw_writel(tcon, S3C2410_TCON);
116
117 local_irq_restore(flags);
118
119 pwm->running = 1;
120 return 0;
121}
122
123EXPORT_SYMBOL(pwm_enable);
124
125void pwm_disable(struct pwm_device *pwm)
126{
127 unsigned long flags;
128 unsigned long tcon;
129
130 local_irq_save(flags);
131
132 tcon = __raw_readl(S3C2410_TCON);
133 tcon &= ~pwm_tcon_start(pwm);
134 __raw_writel(tcon, S3C2410_TCON);
135
136 local_irq_restore(flags);
137
138 pwm->running = 0;
139}
140
141EXPORT_SYMBOL(pwm_disable);
142
Ben Dooks10389592008-08-26 22:54:05 +0100143static unsigned long pwm_calc_tin(struct pwm_device *pwm, unsigned long freq)
Ben Dooks6fc601e2008-07-01 13:17:24 +0100144{
145 unsigned long tin_parent_rate;
146 unsigned int div;
147
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);
150
151 for (div = 2; div <= 16; div *= 2) {
152 if ((tin_parent_rate / (div << 16)) < freq)
153 return tin_parent_rate / div;
154 }
155
156 return tin_parent_rate / 16;
157}
158
159#define NS_IN_HZ (1000000000UL)
160
161int pwm_config(struct pwm_device *pwm, int duty_ns, int period_ns)
162{
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;
170
171 /* We currently avoid using 64bit arithmetic by using the
172 * fact that anything faster than 1Hz is easily representable
173 * by 32bits. */
174
175 if (period_ns > NS_IN_HZ || duty_ns > NS_IN_HZ)
176 return -ERANGE;
177
178 if (duty_ns > period_ns)
179 return -EINVAL;
180
181 if (period_ns == pwm->period_ns &&
182 duty_ns == pwm->duty_ns)
183 return 0;
184
185 /* The TCMP and TCNT can be read without a lock, they're not
186 * shared between the timers. */
187
188 tcmp = __raw_readl(S3C2410_TCMPB(pwm->pwm_id));
189 tcnt = __raw_readl(S3C2410_TCNTB(pwm->pwm_id));
190
191 period = NS_IN_HZ / period_ns;
192
193 pwm_dbg(pwm, "duty_ns=%d, period_ns=%d (%lu)\n",
194 duty_ns, period_ns, period);
195
196 /* Check to see if we are changing the clock rate of the PWM */
197
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);
204
205 pwm->period_ns = period_ns;
206
207 pwm_dbg(pwm, "tin_rate=%lu\n", tin_rate);
208
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);
213
214 /* Note, counters count down */
215
216 tcmp = duty_ns / tin_ns;
217 tcmp = tcnt - tcmp;
Peter Korsgaard165f5f62009-07-01 17:47:08 +0200218 /* 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--;
Ben Dooks6fc601e2008-07-01 13:17:24 +0100222
223 pwm_dbg(pwm, "tin_ns=%lu, tcmp=%ld/%lu\n", tin_ns, tcmp, tcnt);
224
225 if (tcmp < 0)
226 tcmp = 0;
227
228 /* Update the PWM register block. */
229
230 local_irq_save(flags);
231
232 __raw_writel(tcmp, S3C2410_TCMPB(pwm->pwm_id));
233 __raw_writel(tcnt, S3C2410_TCNTB(pwm->pwm_id));
234
235 tcon = __raw_readl(S3C2410_TCON);
236 tcon |= pwm_tcon_manulupdate(pwm);
237 tcon |= pwm_tcon_autoreload(pwm);
238 __raw_writel(tcon, S3C2410_TCON);
239
240 tcon &= ~pwm_tcon_manulupdate(pwm);
241 __raw_writel(tcon, S3C2410_TCON);
242
243 local_irq_restore(flags);
244
245 return 0;
246}
247
248EXPORT_SYMBOL(pwm_config);
249
250static int pwm_register(struct pwm_device *pwm)
251{
252 pwm->duty_ns = -1;
253 pwm->period_ns = -1;
254
255 mutex_lock(&pwm_lock);
256 list_add_tail(&pwm->list, &pwm_list);
257 mutex_unlock(&pwm_lock);
258
259 return 0;
260}
261
262static int s3c_pwm_probe(struct platform_device *pdev)
263{
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;
270
271 if (id == 4) {
272 dev_err(dev, "TIMER4 is currently not supported\n");
273 return -ENXIO;
274 }
275
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;
280 }
281
282 pwm->pdev = pdev;
283 pwm->pwm_id = id;
284
285 /* calculate base of control bits in TCON */
286 pwm->tcon_base = id == 0 ? 0 : (id * 4) + 4;
287
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;
293 }
294
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;
300 }
301
Joonyoung Shimd8633c12011-11-04 10:39:34 +0900302 clk_enable(pwm->clk);
303 clk_enable(pwm->clk_div);
304
Ben Dooks6fc601e2008-07-01 13:17:24 +0100305 local_irq_save(flags);
306
307 tcon = __raw_readl(S3C2410_TCON);
308 tcon |= pwm_tcon_invert(pwm);
309 __raw_writel(tcon, S3C2410_TCON);
310
311 local_irq_restore(flags);
312
313
314 ret = pwm_register(pwm);
315 if (ret) {
316 dev_err(dev, "failed to register pwm\n");
317 goto err_clk_tdiv;
318 }
319
320 pwm_dbg(pwm, "config bits %02x\n",
321 (__raw_readl(S3C2410_TCON) >> pwm->tcon_base) & 0x0f);
322
323 dev_info(dev, "tin at %lu, tdiv at %lu, tin=%sclk, base %d\n",
324 clk_get_rate(pwm->clk),
325 clk_get_rate(pwm->clk_div),
326 pwm_is_tdiv(pwm) ? "div" : "ext", pwm->tcon_base);
327
328 platform_set_drvdata(pdev, pwm);
329 return 0;
330
331 err_clk_tdiv:
Joonyoung Shimd8633c12011-11-04 10:39:34 +0900332 clk_disable(pwm->clk_div);
333 clk_disable(pwm->clk);
Ben Dooks6fc601e2008-07-01 13:17:24 +0100334 clk_put(pwm->clk_div);
335
336 err_clk_tin:
337 clk_put(pwm->clk);
338
339 err_alloc:
340 kfree(pwm);
341 return ret;
342}
343
Uwe Kleine-König93b0d8c2009-11-24 22:07:11 +0100344static int __devexit s3c_pwm_remove(struct platform_device *pdev)
Ben Dooks6fc601e2008-07-01 13:17:24 +0100345{
346 struct pwm_device *pwm = platform_get_drvdata(pdev);
347
Joonyoung Shimd8633c12011-11-04 10:39:34 +0900348 clk_disable(pwm->clk_div);
349 clk_disable(pwm->clk);
Ben Dooks6fc601e2008-07-01 13:17:24 +0100350 clk_put(pwm->clk_div);
351 clk_put(pwm->clk);
352 kfree(pwm);
353
354 return 0;
355}
356
Vasily Khoruzhicke79032a2010-03-15 16:27:08 +0200357#ifdef CONFIG_PM
358static int s3c_pwm_suspend(struct platform_device *pdev, pm_message_t state)
359{
360 struct pwm_device *pwm = platform_get_drvdata(pdev);
361
362 /* No one preserve these values during suspend so reset them
363 * Otherwise driver leaves PWM unconfigured if same values
364 * passed to pwm_config
365 */
366 pwm->period_ns = 0;
367 pwm->duty_ns = 0;
368
369 return 0;
370}
371
372static int s3c_pwm_resume(struct platform_device *pdev)
373{
374 struct pwm_device *pwm = platform_get_drvdata(pdev);
375 unsigned long tcon;
376
377 /* Restore invertion */
378 tcon = __raw_readl(S3C2410_TCON);
379 tcon |= pwm_tcon_invert(pwm);
380 __raw_writel(tcon, S3C2410_TCON);
381
382 return 0;
383}
384
385#else
386#define s3c_pwm_suspend NULL
387#define s3c_pwm_resume NULL
388#endif
389
Ben Dooks6fc601e2008-07-01 13:17:24 +0100390static struct platform_driver s3c_pwm_driver = {
391 .driver = {
392 .name = "s3c24xx-pwm",
393 .owner = THIS_MODULE,
394 },
395 .probe = s3c_pwm_probe,
396 .remove = __devexit_p(s3c_pwm_remove),
Vasily Khoruzhicke79032a2010-03-15 16:27:08 +0200397 .suspend = s3c_pwm_suspend,
398 .resume = s3c_pwm_resume,
Ben Dooks6fc601e2008-07-01 13:17:24 +0100399};
400
401static int __init pwm_init(void)
402{
403 int ret;
404
405 clk_scaler[0] = clk_get(NULL, "pwm-scaler0");
406 clk_scaler[1] = clk_get(NULL, "pwm-scaler1");
407
408 if (IS_ERR(clk_scaler[0]) || IS_ERR(clk_scaler[1])) {
409 printk(KERN_ERR "%s: failed to get scaler clocks\n", __func__);
410 return -EINVAL;
411 }
412
413 ret = platform_driver_register(&s3c_pwm_driver);
414 if (ret)
415 printk(KERN_ERR "%s: failed to add pwm driver\n", __func__);
416
417 return ret;
418}
419
420arch_initcall(pwm_init);