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>
24 /* PWM registers and bits definitions */
29 #define PWMCR_SD (1 << 6)
30 #define PWMDCR_FD (1 << 10)
33 struct list_head node
;
34 struct platform_device
*pdev
;
39 void __iomem
*mmio_base
;
41 unsigned int use_count
;
46 * period_ns = 10^9 * (PRESCALE + 1) * (PV + 1) / PWM_CLK_RATE
47 * duty_ns = 10^9 * (PRESCALE + 1) * DC / PWM_CLK_RATE
49 int pwm_config(struct pwm_device
*pwm
, int duty_ns
, int period_ns
)
52 unsigned long period_cycles
, prescale
, pv
, dc
;
54 if (pwm
== NULL
|| period_ns
== 0 || duty_ns
> period_ns
)
57 c
= clk_get_rate(pwm
->clk
);
59 do_div(c
, 1000000000);
62 if (period_cycles
< 1)
64 prescale
= (period_cycles
- 1) / 1024;
65 pv
= period_cycles
/ (prescale
+ 1) - 1;
70 if (duty_ns
== period_ns
)
73 dc
= (pv
+ 1) * duty_ns
/ period_ns
;
75 /* NOTE: the clock to PWM has to be enabled first
76 * before writing to the registers
79 __raw_writel(prescale
, pwm
->mmio_base
+ PWMCR
);
80 __raw_writel(dc
, pwm
->mmio_base
+ PWMDCR
);
81 __raw_writel(pv
, pwm
->mmio_base
+ PWMPCR
);
82 clk_disable(pwm
->clk
);
86 EXPORT_SYMBOL(pwm_config
);
88 int pwm_enable(struct pwm_device
*pwm
)
92 if (!pwm
->clk_enabled
) {
93 rc
= clk_enable(pwm
->clk
);
99 EXPORT_SYMBOL(pwm_enable
);
101 void pwm_disable(struct pwm_device
*pwm
)
103 if (pwm
->clk_enabled
) {
104 clk_disable(pwm
->clk
);
105 pwm
->clk_enabled
= 0;
108 EXPORT_SYMBOL(pwm_disable
);
110 static DEFINE_MUTEX(pwm_lock
);
111 static LIST_HEAD(pwm_list
);
113 struct pwm_device
*pwm_request(int pwm_id
, const char *label
)
115 struct pwm_device
*pwm
;
118 mutex_lock(&pwm_lock
);
120 list_for_each_entry(pwm
, &pwm_list
, node
) {
121 if (pwm
->pwm_id
== pwm_id
) {
128 if (pwm
->use_count
== 0) {
132 pwm
= ERR_PTR(-EBUSY
);
134 pwm
= ERR_PTR(-ENOENT
);
136 mutex_unlock(&pwm_lock
);
139 EXPORT_SYMBOL(pwm_request
);
141 void pwm_free(struct pwm_device
*pwm
)
143 mutex_lock(&pwm_lock
);
145 if (pwm
->use_count
) {
149 pr_warning("PWM device already freed\n");
151 mutex_unlock(&pwm_lock
);
153 EXPORT_SYMBOL(pwm_free
);
155 static inline void __add_pwm(struct pwm_device
*pwm
)
157 mutex_lock(&pwm_lock
);
158 list_add_tail(&pwm
->node
, &pwm_list
);
159 mutex_unlock(&pwm_lock
);
162 static struct pwm_device
*pwm_probe(struct platform_device
*pdev
,
163 unsigned int pwm_id
, struct pwm_device
*parent_pwm
)
165 struct pwm_device
*pwm
;
169 pwm
= kzalloc(sizeof(struct pwm_device
), GFP_KERNEL
);
171 dev_err(&pdev
->dev
, "failed to allocate memory\n");
172 return ERR_PTR(-ENOMEM
);
175 pwm
->clk
= clk_get(&pdev
->dev
, NULL
);
176 if (IS_ERR(pwm
->clk
)) {
177 ret
= PTR_ERR(pwm
->clk
);
180 pwm
->clk_enabled
= 0;
183 pwm
->pwm_id
= pwm_id
;
186 if (parent_pwm
!= NULL
) {
187 /* registers for the second PWM has offset of 0x10 */
188 pwm
->mmio_base
= parent_pwm
->mmio_base
+ 0x10;
193 r
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
195 dev_err(&pdev
->dev
, "no memory resource defined\n");
200 r
= request_mem_region(r
->start
, r
->end
- r
->start
+ 1, pdev
->name
);
202 dev_err(&pdev
->dev
, "failed to request memory resource\n");
207 pwm
->mmio_base
= ioremap(r
->start
, r
->end
- r
->start
+ 1);
208 if (pwm
->mmio_base
== NULL
) {
209 dev_err(&pdev
->dev
, "failed to ioremap() registers\n");
215 platform_set_drvdata(pdev
, pwm
);
219 release_mem_region(r
->start
, r
->end
- r
->start
+ 1);
227 static int __devinit
pxa25x_pwm_probe(struct platform_device
*pdev
)
229 struct pwm_device
*pwm
= pwm_probe(pdev
, pdev
->id
, NULL
);
237 static int __devinit
pxa27x_pwm_probe(struct platform_device
*pdev
)
239 struct pwm_device
*pwm
;
241 pwm
= pwm_probe(pdev
, pdev
->id
, NULL
);
245 pwm
= pwm_probe(pdev
, pdev
->id
+ 2, pwm
);
252 static int __devexit
pwm_remove(struct platform_device
*pdev
)
254 struct pwm_device
*pwm
;
257 pwm
= platform_get_drvdata(pdev
);
261 mutex_lock(&pwm_lock
);
262 list_del(&pwm
->node
);
263 mutex_unlock(&pwm_lock
);
265 iounmap(pwm
->mmio_base
);
267 r
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
268 release_mem_region(r
->start
, r
->end
- r
->start
+ 1);
275 static struct platform_driver pxa25x_pwm_driver
= {
277 .name
= "pxa25x-pwm",
279 .probe
= pxa25x_pwm_probe
,
280 .remove
= __devexit_p(pwm_remove
),
283 static struct platform_driver pxa27x_pwm_driver
= {
285 .name
= "pxa27x-pwm",
287 .probe
= pxa27x_pwm_probe
,
288 .remove
= __devexit_p(pwm_remove
),
291 static int __init
pwm_init(void)
295 ret
= platform_driver_register(&pxa25x_pwm_driver
);
297 printk(KERN_ERR
"failed to register pxa25x_pwm_driver\n");
301 ret
= platform_driver_register(&pxa27x_pwm_driver
);
303 printk(KERN_ERR
"failed to register pxa27x_pwm_driver\n");
309 arch_initcall(pwm_init
);
311 static void __exit
pwm_exit(void)
313 platform_driver_unregister(&pxa25x_pwm_driver
);
314 platform_driver_unregister(&pxa27x_pwm_driver
);
316 module_exit(pwm_exit
);
318 MODULE_LICENSE("GPL v2");