Merge branch 'for-2.6.30' into for-2.6.31
[linux-2.6] / arch / arm / mach-pxa / pwm.c
1 /*
2  * linux/arch/arm/mach-pxa/pwm.c
3  *
4  * simple driver for PWM (Pulse Width Modulator) controller
5  *
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.
9  *
10  * 2008-02-13   initial version
11  *              eric miao <eric.miao@marvell.com>
12  */
13
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>
19 #include <linux/io.h>
20 #include <linux/pwm.h>
21
22 #include <asm/div64.h>
23
24 /* PWM registers and bits definitions */
25 #define PWMCR           (0x00)
26 #define PWMDCR          (0x04)
27 #define PWMPCR          (0x08)
28
29 #define PWMCR_SD        (1 << 6)
30 #define PWMDCR_FD       (1 << 10)
31
32 struct pwm_device {
33         struct list_head        node;
34         struct platform_device *pdev;
35
36         const char      *label;
37         struct clk      *clk;
38         int             clk_enabled;
39         void __iomem    *mmio_base;
40
41         unsigned int    use_count;
42         unsigned int    pwm_id;
43 };
44
45 /*
46  * period_ns = 10^9 * (PRESCALE + 1) * (PV + 1) / PWM_CLK_RATE
47  * duty_ns   = 10^9 * (PRESCALE + 1) * DC / PWM_CLK_RATE
48  */
49 int pwm_config(struct pwm_device *pwm, int duty_ns, int period_ns)
50 {
51         unsigned long long c;
52         unsigned long period_cycles, prescale, pv, dc;
53
54         if (pwm == NULL || period_ns == 0 || duty_ns > period_ns)
55                 return -EINVAL;
56
57         c = clk_get_rate(pwm->clk);
58         c = c * period_ns;
59         do_div(c, 1000000000);
60         period_cycles = c;
61
62         if (period_cycles < 1)
63                 period_cycles = 1;
64         prescale = (period_cycles - 1) / 1024;
65         pv = period_cycles / (prescale + 1) - 1;
66
67         if (prescale > 63)
68                 return -EINVAL;
69
70         if (duty_ns == period_ns)
71                 dc = PWMDCR_FD;
72         else
73                 dc = (pv + 1) * duty_ns / period_ns;
74
75         /* NOTE: the clock to PWM has to be enabled first
76          * before writing to the registers
77          */
78         clk_enable(pwm->clk);
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);
83
84         return 0;
85 }
86 EXPORT_SYMBOL(pwm_config);
87
88 int pwm_enable(struct pwm_device *pwm)
89 {
90         int rc = 0;
91
92         if (!pwm->clk_enabled) {
93                 rc = clk_enable(pwm->clk);
94                 if (!rc)
95                         pwm->clk_enabled = 1;
96         }
97         return rc;
98 }
99 EXPORT_SYMBOL(pwm_enable);
100
101 void pwm_disable(struct pwm_device *pwm)
102 {
103         if (pwm->clk_enabled) {
104                 clk_disable(pwm->clk);
105                 pwm->clk_enabled = 0;
106         }
107 }
108 EXPORT_SYMBOL(pwm_disable);
109
110 static DEFINE_MUTEX(pwm_lock);
111 static LIST_HEAD(pwm_list);
112
113 struct pwm_device *pwm_request(int pwm_id, const char *label)
114 {
115         struct pwm_device *pwm;
116         int found = 0;
117
118         mutex_lock(&pwm_lock);
119
120         list_for_each_entry(pwm, &pwm_list, node) {
121                 if (pwm->pwm_id == pwm_id) {
122                         found = 1;
123                         break;
124                 }
125         }
126
127         if (found) {
128                 if (pwm->use_count == 0) {
129                         pwm->use_count++;
130                         pwm->label = label;
131                 } else
132                         pwm = ERR_PTR(-EBUSY);
133         } else
134                 pwm = ERR_PTR(-ENOENT);
135
136         mutex_unlock(&pwm_lock);
137         return pwm;
138 }
139 EXPORT_SYMBOL(pwm_request);
140
141 void pwm_free(struct pwm_device *pwm)
142 {
143         mutex_lock(&pwm_lock);
144
145         if (pwm->use_count) {
146                 pwm->use_count--;
147                 pwm->label = NULL;
148         } else
149                 pr_warning("PWM device already freed\n");
150
151         mutex_unlock(&pwm_lock);
152 }
153 EXPORT_SYMBOL(pwm_free);
154
155 static inline void __add_pwm(struct pwm_device *pwm)
156 {
157         mutex_lock(&pwm_lock);
158         list_add_tail(&pwm->node, &pwm_list);
159         mutex_unlock(&pwm_lock);
160 }
161
162 static struct pwm_device *pwm_probe(struct platform_device *pdev,
163                 unsigned int pwm_id, struct pwm_device *parent_pwm)
164 {
165         struct pwm_device *pwm;
166         struct resource *r;
167         int ret = 0;
168
169         pwm = kzalloc(sizeof(struct pwm_device), GFP_KERNEL);
170         if (pwm == NULL) {
171                 dev_err(&pdev->dev, "failed to allocate memory\n");
172                 return ERR_PTR(-ENOMEM);
173         }
174
175         pwm->clk = clk_get(&pdev->dev, NULL);
176         if (IS_ERR(pwm->clk)) {
177                 ret = PTR_ERR(pwm->clk);
178                 goto err_free;
179         }
180         pwm->clk_enabled = 0;
181
182         pwm->use_count = 0;
183         pwm->pwm_id = pwm_id;
184         pwm->pdev = pdev;
185
186         if (parent_pwm != NULL) {
187                 /* registers for the second PWM has offset of 0x10 */
188                 pwm->mmio_base = parent_pwm->mmio_base + 0x10;
189                 __add_pwm(pwm);
190                 return pwm;
191         }
192
193         r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
194         if (r == NULL) {
195                 dev_err(&pdev->dev, "no memory resource defined\n");
196                 ret = -ENODEV;
197                 goto err_free_clk;
198         }
199
200         r = request_mem_region(r->start, r->end - r->start + 1, pdev->name);
201         if (r == NULL) {
202                 dev_err(&pdev->dev, "failed to request memory resource\n");
203                 ret = -EBUSY;
204                 goto err_free_clk;
205         }
206
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");
210                 ret = -ENODEV;
211                 goto err_free_mem;
212         }
213
214         __add_pwm(pwm);
215         platform_set_drvdata(pdev, pwm);
216         return pwm;
217
218 err_free_mem:
219         release_mem_region(r->start, r->end - r->start + 1);
220 err_free_clk:
221         clk_put(pwm->clk);
222 err_free:
223         kfree(pwm);
224         return ERR_PTR(ret);
225 }
226
227 static int __devinit pxa25x_pwm_probe(struct platform_device *pdev)
228 {
229         struct pwm_device *pwm = pwm_probe(pdev, pdev->id, NULL);
230
231         if (IS_ERR(pwm))
232                 return PTR_ERR(pwm);
233
234         return 0;
235 }
236
237 static int __devinit pxa27x_pwm_probe(struct platform_device *pdev)
238 {
239         struct pwm_device *pwm;
240
241         pwm = pwm_probe(pdev, pdev->id, NULL);
242         if (IS_ERR(pwm))
243                 return PTR_ERR(pwm);
244
245         pwm = pwm_probe(pdev, pdev->id + 2, pwm);
246         if (IS_ERR(pwm))
247                 return PTR_ERR(pwm);
248
249         return 0;
250 }
251
252 static int __devexit pwm_remove(struct platform_device *pdev)
253 {
254         struct pwm_device *pwm;
255         struct resource *r;
256
257         pwm = platform_get_drvdata(pdev);
258         if (pwm == NULL)
259                 return -ENODEV;
260
261         mutex_lock(&pwm_lock);
262         list_del(&pwm->node);
263         mutex_unlock(&pwm_lock);
264
265         iounmap(pwm->mmio_base);
266
267         r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
268         release_mem_region(r->start, r->end - r->start + 1);
269
270         clk_put(pwm->clk);
271         kfree(pwm);
272         return 0;
273 }
274
275 static struct platform_driver pxa25x_pwm_driver = {
276         .driver         = {
277                 .name   = "pxa25x-pwm",
278         },
279         .probe          = pxa25x_pwm_probe,
280         .remove         = __devexit_p(pwm_remove),
281 };
282
283 static struct platform_driver pxa27x_pwm_driver = {
284         .driver         = {
285                 .name   = "pxa27x-pwm",
286         },
287         .probe          = pxa27x_pwm_probe,
288         .remove         = __devexit_p(pwm_remove),
289 };
290
291 static int __init pwm_init(void)
292 {
293         int ret = 0;
294
295         ret = platform_driver_register(&pxa25x_pwm_driver);
296         if (ret) {
297                 printk(KERN_ERR "failed to register pxa25x_pwm_driver\n");
298                 return ret;
299         }
300
301         ret = platform_driver_register(&pxa27x_pwm_driver);
302         if (ret) {
303                 printk(KERN_ERR "failed to register pxa27x_pwm_driver\n");
304                 return ret;
305         }
306
307         return ret;
308 }
309 arch_initcall(pwm_init);
310
311 static void __exit pwm_exit(void)
312 {
313         platform_driver_unregister(&pxa25x_pwm_driver);
314         platform_driver_unregister(&pxa27x_pwm_driver);
315 }
316 module_exit(pwm_exit);
317
318 MODULE_LICENSE("GPL v2");