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/err.h>
18 #include <linux/clk.h>
20 #include <linux/pwm.h>
22 #include <mach/irqs.h>
25 #include <plat/devs.h>
26 #include <plat/regs-timer.h>
29 struct list_head list
;
30 struct platform_device
*pdev
;
36 unsigned int period_ns
;
39 unsigned char tcon_base
;
40 unsigned char running
;
41 unsigned char use_count
;
45 #define pwm_dbg(_pwm, msg...) dev_dbg(&(_pwm)->pdev->dev, msg)
47 static struct clk
*clk_scaler
[2];
49 /* Standard setup for a timer block. */
51 #define TIMER_RESOURCE_SIZE (1)
53 #define TIMER_RESOURCE(_tmr, _irq) \
54 (struct resource [TIMER_RESOURCE_SIZE]) { \
58 .flags = IORESOURCE_IRQ \
62 #define DEFINE_S3C_TIMER(_tmr_no, _irq) \
63 .name = "s3c24xx-pwm", \
65 .num_resources = TIMER_RESOURCE_SIZE, \
66 .resource = TIMER_RESOURCE(_tmr_no, _irq), \
68 /* since we already have an static mapping for the timer, we do not
69 * bother setting any IO resource for the base.
72 struct platform_device s3c_device_timer
[] = {
73 [0] = { DEFINE_S3C_TIMER(0, IRQ_TIMER0
) },
74 [1] = { DEFINE_S3C_TIMER(1, IRQ_TIMER1
) },
75 [2] = { DEFINE_S3C_TIMER(2, IRQ_TIMER2
) },
76 [3] = { DEFINE_S3C_TIMER(3, IRQ_TIMER3
) },
77 [4] = { DEFINE_S3C_TIMER(4, IRQ_TIMER4
) },
80 static inline int pwm_is_tdiv(struct pwm_device
*pwm
)
82 return clk_get_parent(pwm
->clk
) == pwm
->clk_div
;
85 static DEFINE_MUTEX(pwm_lock
);
86 static LIST_HEAD(pwm_list
);
88 struct pwm_device
*pwm_request(int pwm_id
, const char *label
)
90 struct pwm_device
*pwm
;
93 mutex_lock(&pwm_lock
);
95 list_for_each_entry(pwm
, &pwm_list
, list
) {
96 if (pwm
->pwm_id
== pwm_id
) {
103 if (pwm
->use_count
== 0) {
107 pwm
= ERR_PTR(-EBUSY
);
109 pwm
= ERR_PTR(-ENOENT
);
111 mutex_unlock(&pwm_lock
);
115 EXPORT_SYMBOL(pwm_request
);
118 void pwm_free(struct pwm_device
*pwm
)
120 mutex_lock(&pwm_lock
);
122 if (pwm
->use_count
) {
126 printk(KERN_ERR
"PWM%d device already freed\n", pwm
->pwm_id
);
128 mutex_unlock(&pwm_lock
);
131 EXPORT_SYMBOL(pwm_free
);
133 #define pwm_tcon_start(pwm) (1 << (pwm->tcon_base + 0))
134 #define pwm_tcon_invert(pwm) (1 << (pwm->tcon_base + 2))
135 #define pwm_tcon_autoreload(pwm) (1 << (pwm->tcon_base + 3))
136 #define pwm_tcon_manulupdate(pwm) (1 << (pwm->tcon_base + 1))
138 int pwm_enable(struct pwm_device
*pwm
)
143 local_irq_save(flags
);
145 tcon
= __raw_readl(S3C2410_TCON
);
146 tcon
|= pwm_tcon_start(pwm
);
147 __raw_writel(tcon
, S3C2410_TCON
);
149 local_irq_restore(flags
);
155 EXPORT_SYMBOL(pwm_enable
);
157 void pwm_disable(struct pwm_device
*pwm
)
162 local_irq_save(flags
);
164 tcon
= __raw_readl(S3C2410_TCON
);
165 tcon
&= ~pwm_tcon_start(pwm
);
166 __raw_writel(tcon
, S3C2410_TCON
);
168 local_irq_restore(flags
);
173 EXPORT_SYMBOL(pwm_disable
);
175 static unsigned long pwm_calc_tin(struct pwm_device
*pwm
, unsigned long freq
)
177 unsigned long tin_parent_rate
;
180 tin_parent_rate
= clk_get_rate(clk_get_parent(pwm
->clk_div
));
181 pwm_dbg(pwm
, "tin parent at %lu\n", tin_parent_rate
);
183 for (div
= 2; div
<= 16; div
*= 2) {
184 if ((tin_parent_rate
/ (div
<< 16)) < freq
)
185 return tin_parent_rate
/ div
;
188 return tin_parent_rate
/ 16;
191 #define NS_IN_HZ (1000000000UL)
193 int pwm_config(struct pwm_device
*pwm
, int duty_ns
, int period_ns
)
195 unsigned long tin_rate
;
196 unsigned long tin_ns
;
197 unsigned long period
;
203 /* We currently avoid using 64bit arithmetic by using the
204 * fact that anything faster than 1Hz is easily representable
207 if (period_ns
> NS_IN_HZ
|| duty_ns
> NS_IN_HZ
)
210 if (duty_ns
> period_ns
)
213 if (period_ns
== pwm
->period_ns
&&
214 duty_ns
== pwm
->duty_ns
)
217 /* The TCMP and TCNT can be read without a lock, they're not
218 * shared between the timers. */
220 tcmp
= __raw_readl(S3C2410_TCMPB(pwm
->pwm_id
));
221 tcnt
= __raw_readl(S3C2410_TCNTB(pwm
->pwm_id
));
223 period
= NS_IN_HZ
/ period_ns
;
225 pwm_dbg(pwm
, "duty_ns=%d, period_ns=%d (%lu)\n",
226 duty_ns
, period_ns
, period
);
228 /* Check to see if we are changing the clock rate of the PWM */
230 if (pwm
->period_ns
!= period_ns
) {
231 if (pwm_is_tdiv(pwm
)) {
232 tin_rate
= pwm_calc_tin(pwm
, period
);
233 clk_set_rate(pwm
->clk_div
, tin_rate
);
235 tin_rate
= clk_get_rate(pwm
->clk
);
237 pwm
->period_ns
= period_ns
;
239 pwm_dbg(pwm
, "tin_rate=%lu\n", tin_rate
);
241 tin_ns
= NS_IN_HZ
/ tin_rate
;
242 tcnt
= period_ns
/ tin_ns
;
244 tin_ns
= NS_IN_HZ
/ clk_get_rate(pwm
->clk
);
246 /* Note, counters count down */
248 tcmp
= duty_ns
/ tin_ns
;
250 /* the pwm hw only checks the compare register after a decrement,
251 so the pin never toggles if tcmp = tcnt */
255 pwm_dbg(pwm
, "tin_ns=%lu, tcmp=%ld/%lu\n", tin_ns
, tcmp
, tcnt
);
260 /* Update the PWM register block. */
262 local_irq_save(flags
);
264 __raw_writel(tcmp
, S3C2410_TCMPB(pwm
->pwm_id
));
265 __raw_writel(tcnt
, S3C2410_TCNTB(pwm
->pwm_id
));
267 tcon
= __raw_readl(S3C2410_TCON
);
268 tcon
|= pwm_tcon_manulupdate(pwm
);
269 tcon
|= pwm_tcon_autoreload(pwm
);
270 __raw_writel(tcon
, S3C2410_TCON
);
272 tcon
&= ~pwm_tcon_manulupdate(pwm
);
273 __raw_writel(tcon
, S3C2410_TCON
);
275 local_irq_restore(flags
);
280 EXPORT_SYMBOL(pwm_config
);
282 static int pwm_register(struct pwm_device
*pwm
)
287 mutex_lock(&pwm_lock
);
288 list_add_tail(&pwm
->list
, &pwm_list
);
289 mutex_unlock(&pwm_lock
);
294 static int s3c_pwm_probe(struct platform_device
*pdev
)
296 struct device
*dev
= &pdev
->dev
;
297 struct pwm_device
*pwm
;
300 unsigned int id
= pdev
->id
;
304 dev_err(dev
, "TIMER4 is currently not supported\n");
308 pwm
= kzalloc(sizeof(struct pwm_device
), GFP_KERNEL
);
310 dev_err(dev
, "failed to allocate pwm_device\n");
317 /* calculate base of control bits in TCON */
318 pwm
->tcon_base
= id
== 0 ? 0 : (id
* 4) + 4;
320 pwm
->clk
= clk_get(dev
, "pwm-tin");
321 if (IS_ERR(pwm
->clk
)) {
322 dev_err(dev
, "failed to get pwm tin clk\n");
323 ret
= PTR_ERR(pwm
->clk
);
327 pwm
->clk_div
= clk_get(dev
, "pwm-tdiv");
328 if (IS_ERR(pwm
->clk_div
)) {
329 dev_err(dev
, "failed to get pwm tdiv clk\n");
330 ret
= PTR_ERR(pwm
->clk_div
);
334 local_irq_save(flags
);
336 tcon
= __raw_readl(S3C2410_TCON
);
337 tcon
|= pwm_tcon_invert(pwm
);
338 __raw_writel(tcon
, S3C2410_TCON
);
340 local_irq_restore(flags
);
343 ret
= pwm_register(pwm
);
345 dev_err(dev
, "failed to register pwm\n");
349 pwm_dbg(pwm
, "config bits %02x\n",
350 (__raw_readl(S3C2410_TCON
) >> pwm
->tcon_base
) & 0x0f);
352 dev_info(dev
, "tin at %lu, tdiv at %lu, tin=%sclk, base %d\n",
353 clk_get_rate(pwm
->clk
),
354 clk_get_rate(pwm
->clk_div
),
355 pwm_is_tdiv(pwm
) ? "div" : "ext", pwm
->tcon_base
);
357 platform_set_drvdata(pdev
, pwm
);
361 clk_put(pwm
->clk_div
);
371 static int s3c_pwm_remove(struct platform_device
*pdev
)
373 struct pwm_device
*pwm
= platform_get_drvdata(pdev
);
375 clk_put(pwm
->clk_div
);
382 static struct platform_driver s3c_pwm_driver
= {
384 .name
= "s3c24xx-pwm",
385 .owner
= THIS_MODULE
,
387 .probe
= s3c_pwm_probe
,
388 .remove
= __devexit_p(s3c_pwm_remove
),
391 static int __init
pwm_init(void)
395 clk_scaler
[0] = clk_get(NULL
, "pwm-scaler0");
396 clk_scaler
[1] = clk_get(NULL
, "pwm-scaler1");
398 if (IS_ERR(clk_scaler
[0]) || IS_ERR(clk_scaler
[1])) {
399 printk(KERN_ERR
"%s: failed to get scaler clocks\n", __func__
);
403 ret
= platform_driver_register(&s3c_pwm_driver
);
405 printk(KERN_ERR
"%s: failed to add pwm driver\n", __func__
);
410 arch_initcall(pwm_init
);