blob: 5513c51ff98495f077860865707e08587a765c26 [file] [log] [blame]
eric miao75540c12008-04-13 21:44:04 +01001/*
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>
eric miao75540c12008-04-13 21:44:04 +010023
Eric Miao3d2a98c2009-04-13 15:59:03 +080024#define HAS_SECONDARY_PWM 0x10
Eric Miaoa757ad82009-04-13 18:51:31 +080025#define PWM_ID_BASE(d) ((d) & 0xf)
Eric Miao3d2a98c2009-04-13 15:59:03 +080026
27static const struct platform_device_id pwm_id_table[] = {
28 /* PWM has_secondary_pwm? */
29 { "pxa25x-pwm", 0 },
Eric Miaoa757ad82009-04-13 18:51:31 +080030 { "pxa27x-pwm", 0 | HAS_SECONDARY_PWM },
Eric Miao3d2a98c2009-04-13 15:59:03 +080031 { },
32};
33MODULE_DEVICE_TABLE(platform, pwm_id_table);
34
eric miao75540c12008-04-13 21:44:04 +010035/* PWM registers and bits definitions */
36#define PWMCR (0x00)
37#define PWMDCR (0x04)
38#define PWMPCR (0x08)
39
40#define PWMCR_SD (1 << 6)
41#define PWMDCR_FD (1 << 10)
42
43struct pwm_device {
44 struct list_head node;
Eric Miao3d2a98c2009-04-13 15:59:03 +080045 struct pwm_device *secondary;
46 struct platform_device *pdev;
eric miao75540c12008-04-13 21:44:04 +010047
48 const char *label;
49 struct clk *clk;
Robert Jarzmikc860d702008-06-10 23:02:31 +010050 int clk_enabled;
eric miao75540c12008-04-13 21:44:04 +010051 void __iomem *mmio_base;
52
53 unsigned int use_count;
54 unsigned int pwm_id;
55};
56
57/*
58 * period_ns = 10^9 * (PRESCALE + 1) * (PV + 1) / PWM_CLK_RATE
59 * duty_ns = 10^9 * (PRESCALE + 1) * DC / PWM_CLK_RATE
60 */
61int pwm_config(struct pwm_device *pwm, int duty_ns, int period_ns)
62{
63 unsigned long long c;
64 unsigned long period_cycles, prescale, pv, dc;
65
66 if (pwm == NULL || period_ns == 0 || duty_ns > period_ns)
67 return -EINVAL;
68
69 c = clk_get_rate(pwm->clk);
70 c = c * period_ns;
71 do_div(c, 1000000000);
72 period_cycles = c;
73
roelkluin71a35d72008-10-14 21:28:51 +010074 if (period_cycles < 1)
eric miao75540c12008-04-13 21:44:04 +010075 period_cycles = 1;
76 prescale = (period_cycles - 1) / 1024;
77 pv = period_cycles / (prescale + 1) - 1;
78
79 if (prescale > 63)
80 return -EINVAL;
81
82 if (duty_ns == period_ns)
83 dc = PWMDCR_FD;
84 else
85 dc = (pv + 1) * duty_ns / period_ns;
86
87 /* NOTE: the clock to PWM has to be enabled first
88 * before writing to the registers
89 */
90 clk_enable(pwm->clk);
91 __raw_writel(prescale, pwm->mmio_base + PWMCR);
92 __raw_writel(dc, pwm->mmio_base + PWMDCR);
93 __raw_writel(pv, pwm->mmio_base + PWMPCR);
94 clk_disable(pwm->clk);
95
96 return 0;
97}
98EXPORT_SYMBOL(pwm_config);
99
100int pwm_enable(struct pwm_device *pwm)
101{
Robert Jarzmikc860d702008-06-10 23:02:31 +0100102 int rc = 0;
103
104 if (!pwm->clk_enabled) {
105 rc = clk_enable(pwm->clk);
106 if (!rc)
107 pwm->clk_enabled = 1;
108 }
109 return rc;
eric miao75540c12008-04-13 21:44:04 +0100110}
111EXPORT_SYMBOL(pwm_enable);
112
113void pwm_disable(struct pwm_device *pwm)
114{
Robert Jarzmikc860d702008-06-10 23:02:31 +0100115 if (pwm->clk_enabled) {
116 clk_disable(pwm->clk);
117 pwm->clk_enabled = 0;
118 }
eric miao75540c12008-04-13 21:44:04 +0100119}
120EXPORT_SYMBOL(pwm_disable);
121
122static DEFINE_MUTEX(pwm_lock);
123static LIST_HEAD(pwm_list);
124
125struct pwm_device *pwm_request(int pwm_id, const char *label)
126{
127 struct pwm_device *pwm;
128 int found = 0;
129
130 mutex_lock(&pwm_lock);
131
132 list_for_each_entry(pwm, &pwm_list, node) {
Ben Dooks43bda1a2008-07-01 14:18:27 +0100133 if (pwm->pwm_id == pwm_id) {
eric miao75540c12008-04-13 21:44:04 +0100134 found = 1;
135 break;
136 }
137 }
138
Ben Dooks43bda1a2008-07-01 14:18:27 +0100139 if (found) {
140 if (pwm->use_count == 0) {
141 pwm->use_count++;
142 pwm->label = label;
143 } else
144 pwm = ERR_PTR(-EBUSY);
145 } else
146 pwm = ERR_PTR(-ENOENT);
eric miao75540c12008-04-13 21:44:04 +0100147
Ben Dooks43bda1a2008-07-01 14:18:27 +0100148 mutex_unlock(&pwm_lock);
149 return pwm;
eric miao75540c12008-04-13 21:44:04 +0100150}
151EXPORT_SYMBOL(pwm_request);
152
153void pwm_free(struct pwm_device *pwm)
154{
155 mutex_lock(&pwm_lock);
156
157 if (pwm->use_count) {
158 pwm->use_count--;
159 pwm->label = NULL;
160 } else
161 pr_warning("PWM device already freed\n");
162
163 mutex_unlock(&pwm_lock);
164}
165EXPORT_SYMBOL(pwm_free);
166
167static inline void __add_pwm(struct pwm_device *pwm)
168{
169 mutex_lock(&pwm_lock);
170 list_add_tail(&pwm->node, &pwm_list);
171 mutex_unlock(&pwm_lock);
172}
173
Eric Miao3d2a98c2009-04-13 15:59:03 +0800174static int __devinit pwm_probe(struct platform_device *pdev)
eric miao75540c12008-04-13 21:44:04 +0100175{
Eric Miao3d2a98c2009-04-13 15:59:03 +0800176 struct platform_device_id *id = platform_get_device_id(pdev);
177 struct pwm_device *pwm, *secondary = NULL;
eric miao75540c12008-04-13 21:44:04 +0100178 struct resource *r;
179 int ret = 0;
180
181 pwm = kzalloc(sizeof(struct pwm_device), GFP_KERNEL);
182 if (pwm == NULL) {
183 dev_err(&pdev->dev, "failed to allocate memory\n");
Eric Miao3d2a98c2009-04-13 15:59:03 +0800184 return -ENOMEM;
eric miao75540c12008-04-13 21:44:04 +0100185 }
186
Russell Kinge0d8b132008-11-11 17:52:32 +0000187 pwm->clk = clk_get(&pdev->dev, NULL);
eric miao75540c12008-04-13 21:44:04 +0100188 if (IS_ERR(pwm->clk)) {
189 ret = PTR_ERR(pwm->clk);
190 goto err_free;
191 }
Robert Jarzmikc860d702008-06-10 23:02:31 +0100192 pwm->clk_enabled = 0;
eric miao75540c12008-04-13 21:44:04 +0100193
194 pwm->use_count = 0;
Eric Miaoa757ad82009-04-13 18:51:31 +0800195 pwm->pwm_id = PWM_ID_BASE(id->driver_data) + pdev->id;
eric miao75540c12008-04-13 21:44:04 +0100196 pwm->pdev = pdev;
197
eric miao75540c12008-04-13 21:44:04 +0100198 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
199 if (r == NULL) {
200 dev_err(&pdev->dev, "no memory resource defined\n");
201 ret = -ENODEV;
202 goto err_free_clk;
203 }
204
205 r = request_mem_region(r->start, r->end - r->start + 1, pdev->name);
206 if (r == NULL) {
207 dev_err(&pdev->dev, "failed to request memory resource\n");
208 ret = -EBUSY;
209 goto err_free_clk;
210 }
211
212 pwm->mmio_base = ioremap(r->start, r->end - r->start + 1);
213 if (pwm->mmio_base == NULL) {
214 dev_err(&pdev->dev, "failed to ioremap() registers\n");
215 ret = -ENODEV;
216 goto err_free_mem;
217 }
218
Eric Miao3d2a98c2009-04-13 15:59:03 +0800219 if (id->driver_data & HAS_SECONDARY_PWM) {
220 secondary = kzalloc(sizeof(struct pwm_device), GFP_KERNEL);
221 if (secondary == NULL) {
222 ret = -ENOMEM;
223 goto err_free_mem;
224 }
225
226 *secondary = *pwm;
227 pwm->secondary = secondary;
228
229 /* registers for the second PWM has offset of 0x10 */
230 secondary->mmio_base = pwm->mmio_base + 0x10;
231 secondary->pwm_id = pdev->id + 2;
232 }
233
eric miao75540c12008-04-13 21:44:04 +0100234 __add_pwm(pwm);
Eric Miao3d2a98c2009-04-13 15:59:03 +0800235 if (secondary)
236 __add_pwm(secondary);
237
eric miao75540c12008-04-13 21:44:04 +0100238 platform_set_drvdata(pdev, pwm);
Eric Miao3d2a98c2009-04-13 15:59:03 +0800239 return 0;
eric miao75540c12008-04-13 21:44:04 +0100240
241err_free_mem:
242 release_mem_region(r->start, r->end - r->start + 1);
243err_free_clk:
244 clk_put(pwm->clk);
245err_free:
246 kfree(pwm);
Eric Miao3d2a98c2009-04-13 15:59:03 +0800247 return ret;
eric miao75540c12008-04-13 21:44:04 +0100248}
249
250static int __devexit pwm_remove(struct platform_device *pdev)
251{
252 struct pwm_device *pwm;
253 struct resource *r;
254
255 pwm = platform_get_drvdata(pdev);
256 if (pwm == NULL)
257 return -ENODEV;
258
259 mutex_lock(&pwm_lock);
Eric Miao3d2a98c2009-04-13 15:59:03 +0800260
261 if (pwm->secondary) {
262 list_del(&pwm->secondary->node);
263 kfree(pwm->secondary);
264 }
265
eric miao75540c12008-04-13 21:44:04 +0100266 list_del(&pwm->node);
267 mutex_unlock(&pwm_lock);
268
269 iounmap(pwm->mmio_base);
270
271 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
272 release_mem_region(r->start, r->end - r->start + 1);
273
274 clk_put(pwm->clk);
275 kfree(pwm);
276 return 0;
277}
278
Eric Miao3d2a98c2009-04-13 15:59:03 +0800279static struct platform_driver pwm_driver = {
eric miao75540c12008-04-13 21:44:04 +0100280 .driver = {
281 .name = "pxa25x-pwm",
Eric Miao3d2a98c2009-04-13 15:59:03 +0800282 .owner = THIS_MODULE,
eric miao75540c12008-04-13 21:44:04 +0100283 },
Eric Miao3d2a98c2009-04-13 15:59:03 +0800284 .probe = pwm_probe,
eric miao75540c12008-04-13 21:44:04 +0100285 .remove = __devexit_p(pwm_remove),
Eric Miao3d2a98c2009-04-13 15:59:03 +0800286 .id_table = pwm_id_table,
eric miao75540c12008-04-13 21:44:04 +0100287};
288
289static int __init pwm_init(void)
290{
Eric Miao3d2a98c2009-04-13 15:59:03 +0800291 return platform_driver_register(&pwm_driver);
eric miao75540c12008-04-13 21:44:04 +0100292}
293arch_initcall(pwm_init);
294
295static void __exit pwm_exit(void)
296{
Eric Miao3d2a98c2009-04-13 15:59:03 +0800297 platform_driver_unregister(&pwm_driver);
eric miao75540c12008-04-13 21:44:04 +0100298}
299module_exit(pwm_exit);
Guennadi Liakhovetskib5f02282008-06-05 10:45:02 +0100300
301MODULE_LICENSE("GPL v2");