2 * linux/arch/arm/mach-pxa/pwm.c
4 * simple driver for PWM (Pulse Width Modulator) controller
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
10 * 2008-02-13 initial version
11 * eric miao <eric.miao@marvell.com>
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 <asm/div64.h>
23 #include <mach/pxa-regs.h>
25 /* PWM registers and bits definitions */
30 #define PWMCR_SD (1 << 6)
31 #define PWMDCR_FD (1 << 10)
34 struct list_head node
;
35 struct platform_device
*pdev
;
40 void __iomem
*mmio_base
;
42 unsigned int use_count
;
47 * period_ns = 10^9 * (PRESCALE + 1) * (PV + 1) / PWM_CLK_RATE
48 * duty_ns = 10^9 * (PRESCALE + 1) * DC / PWM_CLK_RATE
50 int pwm_config(struct pwm_device
*pwm
, int duty_ns
, int period_ns
)
53 unsigned long period_cycles
, prescale
, pv
, dc
;
55 if (pwm
== NULL
|| period_ns
== 0 || duty_ns
> period_ns
)
58 c
= clk_get_rate(pwm
->clk
);
60 do_div(c
, 1000000000);
63 if (period_cycles
< 1)
65 prescale
= (period_cycles
- 1) / 1024;
66 pv
= period_cycles
/ (prescale
+ 1) - 1;
71 if (duty_ns
== period_ns
)
74 dc
= (pv
+ 1) * duty_ns
/ period_ns
;
76 /* NOTE: the clock to PWM has to be enabled first
77 * before writing to the registers
80 __raw_writel(prescale
, pwm
->mmio_base
+ PWMCR
);
81 __raw_writel(dc
, pwm
->mmio_base
+ PWMDCR
);
82 __raw_writel(pv
, pwm
->mmio_base
+ PWMPCR
);
83 clk_disable(pwm
->clk
);
87 EXPORT_SYMBOL(pwm_config
);
89 int pwm_enable(struct pwm_device
*pwm
)
93 if (!pwm
->clk_enabled
) {
94 rc
= clk_enable(pwm
->clk
);
100 EXPORT_SYMBOL(pwm_enable
);
102 void pwm_disable(struct pwm_device
*pwm
)
104 if (pwm
->clk_enabled
) {
105 clk_disable(pwm
->clk
);
106 pwm
->clk_enabled
= 0;
109 EXPORT_SYMBOL(pwm_disable
);
111 static DEFINE_MUTEX(pwm_lock
);
112 static LIST_HEAD(pwm_list
);
114 struct pwm_device
*pwm_request(int pwm_id
, const char *label
)
116 struct pwm_device
*pwm
;
119 mutex_lock(&pwm_lock
);
121 list_for_each_entry(pwm
, &pwm_list
, node
) {
122 if (pwm
->pwm_id
== pwm_id
) {
129 if (pwm
->use_count
== 0) {
133 pwm
= ERR_PTR(-EBUSY
);
135 pwm
= ERR_PTR(-ENOENT
);
137 mutex_unlock(&pwm_lock
);
140 EXPORT_SYMBOL(pwm_request
);
142 void pwm_free(struct pwm_device
*pwm
)
144 mutex_lock(&pwm_lock
);
146 if (pwm
->use_count
) {
150 pr_warning("PWM device already freed\n");
152 mutex_unlock(&pwm_lock
);
154 EXPORT_SYMBOL(pwm_free
);
156 static inline void __add_pwm(struct pwm_device
*pwm
)
158 mutex_lock(&pwm_lock
);
159 list_add_tail(&pwm
->node
, &pwm_list
);
160 mutex_unlock(&pwm_lock
);
163 static struct pwm_device
*pwm_probe(struct platform_device
*pdev
,
164 unsigned int pwm_id
, struct pwm_device
*parent_pwm
)
166 struct pwm_device
*pwm
;
170 pwm
= kzalloc(sizeof(struct pwm_device
), GFP_KERNEL
);
172 dev_err(&pdev
->dev
, "failed to allocate memory\n");
173 return ERR_PTR(-ENOMEM
);
176 pwm
->clk
= clk_get(&pdev
->dev
, "PWMCLK");
177 if (IS_ERR(pwm
->clk
)) {
178 ret
= PTR_ERR(pwm
->clk
);
181 pwm
->clk_enabled
= 0;
184 pwm
->pwm_id
= pwm_id
;
187 if (parent_pwm
!= NULL
) {
188 /* registers for the second PWM has offset of 0x10 */
189 pwm
->mmio_base
= parent_pwm
->mmio_base
+ 0x10;
194 r
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
196 dev_err(&pdev
->dev
, "no memory resource defined\n");
201 r
= request_mem_region(r
->start
, r
->end
- r
->start
+ 1, pdev
->name
);
203 dev_err(&pdev
->dev
, "failed to request memory resource\n");
208 pwm
->mmio_base
= ioremap(r
->start
, r
->end
- r
->start
+ 1);
209 if (pwm
->mmio_base
== NULL
) {
210 dev_err(&pdev
->dev
, "failed to ioremap() registers\n");
216 platform_set_drvdata(pdev
, pwm
);
220 release_mem_region(r
->start
, r
->end
- r
->start
+ 1);
228 static int __devinit
pxa25x_pwm_probe(struct platform_device
*pdev
)
230 struct pwm_device
*pwm
= pwm_probe(pdev
, pdev
->id
, NULL
);
238 static int __devinit
pxa27x_pwm_probe(struct platform_device
*pdev
)
240 struct pwm_device
*pwm
;
242 pwm
= pwm_probe(pdev
, pdev
->id
, NULL
);
246 pwm
= pwm_probe(pdev
, pdev
->id
+ 2, pwm
);
253 static int __devexit
pwm_remove(struct platform_device
*pdev
)
255 struct pwm_device
*pwm
;
258 pwm
= platform_get_drvdata(pdev
);
262 mutex_lock(&pwm_lock
);
263 list_del(&pwm
->node
);
264 mutex_unlock(&pwm_lock
);
266 iounmap(pwm
->mmio_base
);
268 r
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
269 release_mem_region(r
->start
, r
->end
- r
->start
+ 1);
276 static struct platform_driver pxa25x_pwm_driver
= {
278 .name
= "pxa25x-pwm",
280 .probe
= pxa25x_pwm_probe
,
281 .remove
= __devexit_p(pwm_remove
),
284 static struct platform_driver pxa27x_pwm_driver
= {
286 .name
= "pxa27x-pwm",
288 .probe
= pxa27x_pwm_probe
,
289 .remove
= __devexit_p(pwm_remove
),
292 static int __init
pwm_init(void)
296 ret
= platform_driver_register(&pxa25x_pwm_driver
);
298 printk(KERN_ERR
"failed to register pxa25x_pwm_driver\n");
302 ret
= platform_driver_register(&pxa27x_pwm_driver
);
304 printk(KERN_ERR
"failed to register pxa27x_pwm_driver\n");
310 arch_initcall(pwm_init
);
312 static void __exit
pwm_exit(void)
314 platform_driver_unregister(&pxa25x_pwm_driver
);
315 platform_driver_unregister(&pxa27x_pwm_driver
);
317 module_exit(pwm_exit
);
319 MODULE_LICENSE("GPL v2");