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>
23 #include <mach/irqs.h>
26 #include <plat/devs.h>
27 #include <plat/regs-timer.h>
30 struct list_head list
;
31 struct platform_device
*pdev
;
37 unsigned int period_ns
;
40 unsigned char tcon_base
;
41 unsigned char running
;
42 unsigned char use_count
;
46 #define pwm_dbg(_pwm, msg...) dev_dbg(&(_pwm)->pdev->dev, msg)
48 static struct clk
*clk_scaler
[2];
50 /* Standard setup for a timer block. */
52 #define TIMER_RESOURCE_SIZE (1)
54 #define TIMER_RESOURCE(_tmr, _irq) \
55 (struct resource [TIMER_RESOURCE_SIZE]) { \
59 .flags = IORESOURCE_IRQ \
63 #define DEFINE_S3C_TIMER(_tmr_no, _irq) \
64 .name = "s3c24xx-pwm", \
66 .num_resources = TIMER_RESOURCE_SIZE, \
67 .resource = TIMER_RESOURCE(_tmr_no, _irq), \
69 /* since we already have an static mapping for the timer, we do not
70 * bother setting any IO resource for the base.
73 struct platform_device s3c_device_timer
[] = {
74 [0] = { DEFINE_S3C_TIMER(0, IRQ_TIMER0
) },
75 [1] = { DEFINE_S3C_TIMER(1, IRQ_TIMER1
) },
76 [2] = { DEFINE_S3C_TIMER(2, IRQ_TIMER2
) },
77 [3] = { DEFINE_S3C_TIMER(3, IRQ_TIMER3
) },
78 [4] = { DEFINE_S3C_TIMER(4, IRQ_TIMER4
) },
81 static inline int pwm_is_tdiv(struct pwm_device
*pwm
)
83 return clk_get_parent(pwm
->clk
) == pwm
->clk_div
;
86 static DEFINE_MUTEX(pwm_lock
);
87 static LIST_HEAD(pwm_list
);
89 struct pwm_device
*pwm_request(int pwm_id
, const char *label
)
91 struct pwm_device
*pwm
;
94 mutex_lock(&pwm_lock
);
96 list_for_each_entry(pwm
, &pwm_list
, list
) {
97 if (pwm
->pwm_id
== pwm_id
) {
104 if (pwm
->use_count
== 0) {
108 pwm
= ERR_PTR(-EBUSY
);
110 pwm
= ERR_PTR(-ENOENT
);
112 mutex_unlock(&pwm_lock
);
116 EXPORT_SYMBOL(pwm_request
);
119 void pwm_free(struct pwm_device
*pwm
)
121 mutex_lock(&pwm_lock
);
123 if (pwm
->use_count
) {
127 printk(KERN_ERR
"PWM%d device already freed\n", pwm
->pwm_id
);
129 mutex_unlock(&pwm_lock
);
132 EXPORT_SYMBOL(pwm_free
);
134 #define pwm_tcon_start(pwm) (1 << (pwm->tcon_base + 0))
135 #define pwm_tcon_invert(pwm) (1 << (pwm->tcon_base + 2))
136 #define pwm_tcon_autoreload(pwm) (1 << (pwm->tcon_base + 3))
137 #define pwm_tcon_manulupdate(pwm) (1 << (pwm->tcon_base + 1))
139 int pwm_enable(struct pwm_device
*pwm
)
144 local_irq_save(flags
);
146 tcon
= __raw_readl(S3C2410_TCON
);
147 tcon
|= pwm_tcon_start(pwm
);
148 __raw_writel(tcon
, S3C2410_TCON
);
150 local_irq_restore(flags
);
156 EXPORT_SYMBOL(pwm_enable
);
158 void pwm_disable(struct pwm_device
*pwm
)
163 local_irq_save(flags
);
165 tcon
= __raw_readl(S3C2410_TCON
);
166 tcon
&= ~pwm_tcon_start(pwm
);
167 __raw_writel(tcon
, S3C2410_TCON
);
169 local_irq_restore(flags
);
174 EXPORT_SYMBOL(pwm_disable
);
176 static unsigned long pwm_calc_tin(struct pwm_device
*pwm
, unsigned long freq
)
178 unsigned long tin_parent_rate
;
181 tin_parent_rate
= clk_get_rate(clk_get_parent(pwm
->clk_div
));
182 pwm_dbg(pwm
, "tin parent at %lu\n", tin_parent_rate
);
184 for (div
= 2; div
<= 16; div
*= 2) {
185 if ((tin_parent_rate
/ (div
<< 16)) < freq
)
186 return tin_parent_rate
/ div
;
189 return tin_parent_rate
/ 16;
192 #define NS_IN_HZ (1000000000UL)
194 int pwm_config(struct pwm_device
*pwm
, int duty_ns
, int period_ns
)
196 unsigned long tin_rate
;
197 unsigned long tin_ns
;
198 unsigned long period
;
204 /* We currently avoid using 64bit arithmetic by using the
205 * fact that anything faster than 1Hz is easily representable
208 if (period_ns
> NS_IN_HZ
|| duty_ns
> NS_IN_HZ
)
211 if (duty_ns
> period_ns
)
214 if (period_ns
== pwm
->period_ns
&&
215 duty_ns
== pwm
->duty_ns
)
218 /* The TCMP and TCNT can be read without a lock, they're not
219 * shared between the timers. */
221 tcmp
= __raw_readl(S3C2410_TCMPB(pwm
->pwm_id
));
222 tcnt
= __raw_readl(S3C2410_TCNTB(pwm
->pwm_id
));
224 period
= NS_IN_HZ
/ period_ns
;
226 pwm_dbg(pwm
, "duty_ns=%d, period_ns=%d (%lu)\n",
227 duty_ns
, period_ns
, period
);
229 /* Check to see if we are changing the clock rate of the PWM */
231 if (pwm
->period_ns
!= period_ns
) {
232 if (pwm_is_tdiv(pwm
)) {
233 tin_rate
= pwm_calc_tin(pwm
, period
);
234 clk_set_rate(pwm
->clk_div
, tin_rate
);
236 tin_rate
= clk_get_rate(pwm
->clk
);
238 pwm
->period_ns
= period_ns
;
240 pwm_dbg(pwm
, "tin_rate=%lu\n", tin_rate
);
242 tin_ns
= NS_IN_HZ
/ tin_rate
;
243 tcnt
= period_ns
/ tin_ns
;
245 tin_ns
= NS_IN_HZ
/ clk_get_rate(pwm
->clk
);
247 /* Note, counters count down */
249 tcmp
= duty_ns
/ tin_ns
;
251 /* the pwm hw only checks the compare register after a decrement,
252 so the pin never toggles if tcmp = tcnt */
256 pwm_dbg(pwm
, "tin_ns=%lu, tcmp=%ld/%lu\n", tin_ns
, tcmp
, tcnt
);
261 /* Update the PWM register block. */
263 local_irq_save(flags
);
265 __raw_writel(tcmp
, S3C2410_TCMPB(pwm
->pwm_id
));
266 __raw_writel(tcnt
, S3C2410_TCNTB(pwm
->pwm_id
));
268 tcon
= __raw_readl(S3C2410_TCON
);
269 tcon
|= pwm_tcon_manulupdate(pwm
);
270 tcon
|= pwm_tcon_autoreload(pwm
);
271 __raw_writel(tcon
, S3C2410_TCON
);
273 tcon
&= ~pwm_tcon_manulupdate(pwm
);
274 __raw_writel(tcon
, S3C2410_TCON
);
276 local_irq_restore(flags
);
281 EXPORT_SYMBOL(pwm_config
);
283 static int pwm_register(struct pwm_device
*pwm
)
288 mutex_lock(&pwm_lock
);
289 list_add_tail(&pwm
->list
, &pwm_list
);
290 mutex_unlock(&pwm_lock
);
295 static int s3c_pwm_probe(struct platform_device
*pdev
)
297 struct device
*dev
= &pdev
->dev
;
298 struct pwm_device
*pwm
;
301 unsigned int id
= pdev
->id
;
305 dev_err(dev
, "TIMER4 is currently not supported\n");
309 pwm
= kzalloc(sizeof(struct pwm_device
), GFP_KERNEL
);
311 dev_err(dev
, "failed to allocate pwm_device\n");
318 /* calculate base of control bits in TCON */
319 pwm
->tcon_base
= id
== 0 ? 0 : (id
* 4) + 4;
321 pwm
->clk
= clk_get(dev
, "pwm-tin");
322 if (IS_ERR(pwm
->clk
)) {
323 dev_err(dev
, "failed to get pwm tin clk\n");
324 ret
= PTR_ERR(pwm
->clk
);
328 pwm
->clk_div
= clk_get(dev
, "pwm-tdiv");
329 if (IS_ERR(pwm
->clk_div
)) {
330 dev_err(dev
, "failed to get pwm tdiv clk\n");
331 ret
= PTR_ERR(pwm
->clk_div
);
335 local_irq_save(flags
);
337 tcon
= __raw_readl(S3C2410_TCON
);
338 tcon
|= pwm_tcon_invert(pwm
);
339 __raw_writel(tcon
, S3C2410_TCON
);
341 local_irq_restore(flags
);
344 ret
= pwm_register(pwm
);
346 dev_err(dev
, "failed to register pwm\n");
350 pwm_dbg(pwm
, "config bits %02x\n",
351 (__raw_readl(S3C2410_TCON
) >> pwm
->tcon_base
) & 0x0f);
353 dev_info(dev
, "tin at %lu, tdiv at %lu, tin=%sclk, base %d\n",
354 clk_get_rate(pwm
->clk
),
355 clk_get_rate(pwm
->clk_div
),
356 pwm_is_tdiv(pwm
) ? "div" : "ext", pwm
->tcon_base
);
358 platform_set_drvdata(pdev
, pwm
);
362 clk_put(pwm
->clk_div
);
372 static int __devexit
s3c_pwm_remove(struct platform_device
*pdev
)
374 struct pwm_device
*pwm
= platform_get_drvdata(pdev
);
376 clk_put(pwm
->clk_div
);
384 static int s3c_pwm_suspend(struct platform_device
*pdev
, pm_message_t state
)
386 struct pwm_device
*pwm
= platform_get_drvdata(pdev
);
388 /* No one preserve these values during suspend so reset them
389 * Otherwise driver leaves PWM unconfigured if same values
390 * passed to pwm_config
398 static int s3c_pwm_resume(struct platform_device
*pdev
)
400 struct pwm_device
*pwm
= platform_get_drvdata(pdev
);
403 /* Restore invertion */
404 tcon
= __raw_readl(S3C2410_TCON
);
405 tcon
|= pwm_tcon_invert(pwm
);
406 __raw_writel(tcon
, S3C2410_TCON
);
412 #define s3c_pwm_suspend NULL
413 #define s3c_pwm_resume NULL
416 static struct platform_driver s3c_pwm_driver
= {
418 .name
= "s3c24xx-pwm",
419 .owner
= THIS_MODULE
,
421 .probe
= s3c_pwm_probe
,
422 .remove
= __devexit_p(s3c_pwm_remove
),
423 .suspend
= s3c_pwm_suspend
,
424 .resume
= s3c_pwm_resume
,
427 static int __init
pwm_init(void)
431 clk_scaler
[0] = clk_get(NULL
, "pwm-scaler0");
432 clk_scaler
[1] = clk_get(NULL
, "pwm-scaler1");
434 if (IS_ERR(clk_scaler
[0]) || IS_ERR(clk_scaler
[1])) {
435 printk(KERN_ERR
"%s: failed to get scaler clocks\n", __func__
);
439 ret
= platform_driver_register(&s3c_pwm_driver
);
441 printk(KERN_ERR
"%s: failed to add pwm driver\n", __func__
);
446 arch_initcall(pwm_init
);