2 * arch/arm/mach-vt8500/pwm.c
4 * Copyright (C) 2010 Alexey Charkov <alchark@gmail.com>
6 * This software is licensed under the terms of the GNU General Public
7 * License version 2, as published by the Free Software Foundation, and
8 * may be copied, distributed, and modified under those terms.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
16 #include <linux/module.h>
17 #include <linux/kernel.h>
18 #include <linux/platform_device.h>
19 #include <linux/slab.h>
20 #include <linux/err.h>
22 #include <linux/pwm.h>
23 #include <linux/delay.h>
25 #include <asm/div64.h>
27 #define VT8500_NR_PWMS 4
29 static DEFINE_MUTEX(pwm_lock
);
30 static LIST_HEAD(pwm_list
);
33 struct list_head node
;
34 struct platform_device
*pdev
;
38 void __iomem
*regbase
;
40 unsigned int use_count
;
44 #define msecs_to_loops(t) (loops_per_jiffy / 1000 * HZ * t)
45 static inline void pwm_busy_wait(void __iomem
*reg
, u8 bitmask
)
47 int loops
= msecs_to_loops(10);
48 while ((readb(reg
) & bitmask
) && --loops
)
52 pr_warning("Waiting for status bits 0x%x to clear timed out\n",
56 int pwm_config(struct pwm_device
*pwm
, int duty_ns
, int period_ns
)
59 unsigned long period_cycles
, prescale
, pv
, dc
;
61 if (pwm
== NULL
|| period_ns
== 0 || duty_ns
> period_ns
)
64 c
= 25000000/2; /* wild guess --- need to implement clocks */
66 do_div(c
, 1000000000);
69 if (period_cycles
< 1)
71 prescale
= (period_cycles
- 1) / 4096;
72 pv
= period_cycles
/ (prescale
+ 1) - 1;
79 c
= (unsigned long long)pv
* duty_ns
;
83 pwm_busy_wait(pwm
->regbase
+ 0x40 + pwm
->pwm_id
, (1 << 1));
84 writel(prescale
, pwm
->regbase
+ 0x4 + (pwm
->pwm_id
<< 4));
86 pwm_busy_wait(pwm
->regbase
+ 0x40 + pwm
->pwm_id
, (1 << 2));
87 writel(pv
, pwm
->regbase
+ 0x8 + (pwm
->pwm_id
<< 4));
89 pwm_busy_wait(pwm
->regbase
+ 0x40 + pwm
->pwm_id
, (1 << 3));
90 writel(dc
, pwm
->regbase
+ 0xc + (pwm
->pwm_id
<< 4));
94 EXPORT_SYMBOL(pwm_config
);
96 int pwm_enable(struct pwm_device
*pwm
)
98 pwm_busy_wait(pwm
->regbase
+ 0x40 + pwm
->pwm_id
, (1 << 0));
99 writel(5, pwm
->regbase
+ (pwm
->pwm_id
<< 4));
102 EXPORT_SYMBOL(pwm_enable
);
104 void pwm_disable(struct pwm_device
*pwm
)
106 pwm_busy_wait(pwm
->regbase
+ 0x40 + pwm
->pwm_id
, (1 << 0));
107 writel(0, pwm
->regbase
+ (pwm
->pwm_id
<< 4));
109 EXPORT_SYMBOL(pwm_disable
);
111 struct pwm_device
*pwm_request(int pwm_id
, const char *label
)
113 struct pwm_device
*pwm
;
116 mutex_lock(&pwm_lock
);
118 list_for_each_entry(pwm
, &pwm_list
, node
) {
119 if (pwm
->pwm_id
== pwm_id
) {
126 if (pwm
->use_count
== 0) {
130 pwm
= ERR_PTR(-EBUSY
);
133 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");
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 int __devinit
pwm_probe(struct platform_device
*pdev
)
165 struct pwm_device
*pwms
;
170 pwms
= kzalloc(sizeof(struct pwm_device
) * VT8500_NR_PWMS
, GFP_KERNEL
);
172 dev_err(&pdev
->dev
, "failed to allocate memory\n");
176 for (i
= 0; i
< VT8500_NR_PWMS
; i
++) {
177 pwms
[i
].use_count
= 0;
182 r
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
184 dev_err(&pdev
->dev
, "no memory resource defined\n");
189 r
= request_mem_region(r
->start
, resource_size(r
), pdev
->name
);
191 dev_err(&pdev
->dev
, "failed to request memory resource\n");
196 pwms
[0].regbase
= ioremap(r
->start
, resource_size(r
));
197 if (pwms
[0].regbase
== NULL
) {
198 dev_err(&pdev
->dev
, "failed to ioremap() registers\n");
203 for (i
= 1; i
< VT8500_NR_PWMS
; i
++)
204 pwms
[i
].regbase
= pwms
[0].regbase
;
206 for (i
= 0; i
< VT8500_NR_PWMS
; i
++)
209 platform_set_drvdata(pdev
, pwms
);
213 release_mem_region(r
->start
, resource_size(r
));
219 static int __devexit
pwm_remove(struct platform_device
*pdev
)
221 struct pwm_device
*pwms
;
225 pwms
= platform_get_drvdata(pdev
);
229 mutex_lock(&pwm_lock
);
231 for (i
= 0; i
< VT8500_NR_PWMS
; i
++)
232 list_del(&pwms
[i
].node
);
233 mutex_unlock(&pwm_lock
);
235 iounmap(pwms
[0].regbase
);
237 r
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
238 release_mem_region(r
->start
, resource_size(r
));
244 static struct platform_driver pwm_driver
= {
246 .name
= "vt8500-pwm",
247 .owner
= THIS_MODULE
,
250 .remove
= __devexit_p(pwm_remove
),
253 static int __init
pwm_init(void)
255 return platform_driver_register(&pwm_driver
);
257 arch_initcall(pwm_init
);
259 static void __exit
pwm_exit(void)
261 platform_driver_unregister(&pwm_driver
);
263 module_exit(pwm_exit
);
265 MODULE_LICENSE("GPL");