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 #define HAS_SECONDARY_PWM 0x10
25 #define PWM_ID_BASE(d) ((d) & 0xf)
27 static const struct platform_device_id pwm_id_table
[] = {
28 /* PWM has_secondary_pwm? */
30 { "pxa27x-pwm", 0 | HAS_SECONDARY_PWM
},
35 MODULE_DEVICE_TABLE(platform
, pwm_id_table
);
37 /* PWM registers and bits definitions */
42 #define PWMCR_SD (1 << 6)
43 #define PWMDCR_FD (1 << 10)
46 struct list_head node
;
47 struct pwm_device
*secondary
;
48 struct platform_device
*pdev
;
53 void __iomem
*mmio_base
;
55 unsigned int use_count
;
60 * period_ns = 10^9 * (PRESCALE + 1) * (PV + 1) / PWM_CLK_RATE
61 * duty_ns = 10^9 * (PRESCALE + 1) * DC / PWM_CLK_RATE
63 int pwm_config(struct pwm_device
*pwm
, int duty_ns
, int period_ns
)
66 unsigned long period_cycles
, prescale
, pv
, dc
;
68 if (pwm
== NULL
|| period_ns
== 0 || duty_ns
> period_ns
)
71 c
= clk_get_rate(pwm
->clk
);
73 do_div(c
, 1000000000);
76 if (period_cycles
< 1)
78 prescale
= (period_cycles
- 1) / 1024;
79 pv
= period_cycles
/ (prescale
+ 1) - 1;
84 if (duty_ns
== period_ns
)
87 dc
= (pv
+ 1) * duty_ns
/ period_ns
;
89 /* NOTE: the clock to PWM has to be enabled first
90 * before writing to the registers
93 __raw_writel(prescale
, pwm
->mmio_base
+ PWMCR
);
94 __raw_writel(dc
, pwm
->mmio_base
+ PWMDCR
);
95 __raw_writel(pv
, pwm
->mmio_base
+ PWMPCR
);
96 clk_disable(pwm
->clk
);
100 EXPORT_SYMBOL(pwm_config
);
102 int pwm_enable(struct pwm_device
*pwm
)
106 if (!pwm
->clk_enabled
) {
107 rc
= clk_enable(pwm
->clk
);
109 pwm
->clk_enabled
= 1;
113 EXPORT_SYMBOL(pwm_enable
);
115 void pwm_disable(struct pwm_device
*pwm
)
117 if (pwm
->clk_enabled
) {
118 clk_disable(pwm
->clk
);
119 pwm
->clk_enabled
= 0;
122 EXPORT_SYMBOL(pwm_disable
);
124 static DEFINE_MUTEX(pwm_lock
);
125 static LIST_HEAD(pwm_list
);
127 struct pwm_device
*pwm_request(int pwm_id
, const char *label
)
129 struct pwm_device
*pwm
;
132 mutex_lock(&pwm_lock
);
134 list_for_each_entry(pwm
, &pwm_list
, node
) {
135 if (pwm
->pwm_id
== pwm_id
) {
142 if (pwm
->use_count
== 0) {
146 pwm
= ERR_PTR(-EBUSY
);
148 pwm
= ERR_PTR(-ENOENT
);
150 mutex_unlock(&pwm_lock
);
153 EXPORT_SYMBOL(pwm_request
);
155 void pwm_free(struct pwm_device
*pwm
)
157 mutex_lock(&pwm_lock
);
159 if (pwm
->use_count
) {
163 pr_warning("PWM device already freed\n");
165 mutex_unlock(&pwm_lock
);
167 EXPORT_SYMBOL(pwm_free
);
169 static inline void __add_pwm(struct pwm_device
*pwm
)
171 mutex_lock(&pwm_lock
);
172 list_add_tail(&pwm
->node
, &pwm_list
);
173 mutex_unlock(&pwm_lock
);
176 static int __devinit
pwm_probe(struct platform_device
*pdev
)
178 struct platform_device_id
*id
= platform_get_device_id(pdev
);
179 struct pwm_device
*pwm
, *secondary
= NULL
;
183 pwm
= kzalloc(sizeof(struct pwm_device
), GFP_KERNEL
);
185 dev_err(&pdev
->dev
, "failed to allocate memory\n");
189 pwm
->clk
= clk_get(&pdev
->dev
, NULL
);
190 if (IS_ERR(pwm
->clk
)) {
191 ret
= PTR_ERR(pwm
->clk
);
194 pwm
->clk_enabled
= 0;
197 pwm
->pwm_id
= PWM_ID_BASE(id
->driver_data
) + pdev
->id
;
200 r
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
202 dev_err(&pdev
->dev
, "no memory resource defined\n");
207 r
= request_mem_region(r
->start
, r
->end
- r
->start
+ 1, pdev
->name
);
209 dev_err(&pdev
->dev
, "failed to request memory resource\n");
214 pwm
->mmio_base
= ioremap(r
->start
, r
->end
- r
->start
+ 1);
215 if (pwm
->mmio_base
== NULL
) {
216 dev_err(&pdev
->dev
, "failed to ioremap() registers\n");
221 if (id
->driver_data
& HAS_SECONDARY_PWM
) {
222 secondary
= kzalloc(sizeof(struct pwm_device
), GFP_KERNEL
);
223 if (secondary
== NULL
) {
229 pwm
->secondary
= secondary
;
231 /* registers for the second PWM has offset of 0x10 */
232 secondary
->mmio_base
= pwm
->mmio_base
+ 0x10;
233 secondary
->pwm_id
= pdev
->id
+ 2;
238 __add_pwm(secondary
);
240 platform_set_drvdata(pdev
, pwm
);
244 release_mem_region(r
->start
, r
->end
- r
->start
+ 1);
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
);
263 if (pwm
->secondary
) {
264 list_del(&pwm
->secondary
->node
);
265 kfree(pwm
->secondary
);
268 list_del(&pwm
->node
);
269 mutex_unlock(&pwm_lock
);
271 iounmap(pwm
->mmio_base
);
273 r
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
274 release_mem_region(r
->start
, r
->end
- r
->start
+ 1);
281 static struct platform_driver pwm_driver
= {
283 .name
= "pxa25x-pwm",
284 .owner
= THIS_MODULE
,
287 .remove
= __devexit_p(pwm_remove
),
288 .id_table
= pwm_id_table
,
291 static int __init
pwm_init(void)
293 return platform_driver_register(&pwm_driver
);
295 arch_initcall(pwm_init
);
297 static void __exit
pwm_exit(void)
299 platform_driver_unregister(&pwm_driver
);
301 module_exit(pwm_exit
);
303 MODULE_LICENSE("GPL v2");