aboutsummaryrefslogtreecommitdiffstats
path: root/arch/arm/plat-pxa/pwm.c
diff options
context:
space:
mode:
authorEric Miao <eric.miao@marvell.com>2009-04-13 04:28:02 -0400
committerEric Miao <eric.y.miao@gmail.com>2009-06-04 22:32:04 -0400
commit3852ac7113f313d96af80257a070fd8ab2eea5c6 (patch)
tree832da261752cf561fba59119e5570cec035b82d6 /arch/arm/plat-pxa/pwm.c
parenta71ef16327a534a88fb1277d28dcd362dbb1992f (diff)
[ARM] pxa: move pwm.c to common plat-pxa directory
Signed-off-by: Eric Miao <eric.miao@marvell.com>
Diffstat (limited to 'arch/arm/plat-pxa/pwm.c')
-rw-r--r--arch/arm/plat-pxa/pwm.c301
1 files changed, 301 insertions, 0 deletions
diff --git a/arch/arm/plat-pxa/pwm.c b/arch/arm/plat-pxa/pwm.c
new file mode 100644
index 000000000000..5513c51ff984
--- /dev/null
+++ b/arch/arm/plat-pxa/pwm.c
@@ -0,0 +1,301 @@
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#define HAS_SECONDARY_PWM 0x10
25#define PWM_ID_BASE(d) ((d) & 0xf)
26
27static const struct platform_device_id pwm_id_table[] = {
28 /* PWM has_secondary_pwm? */
29 { "pxa25x-pwm", 0 },
30 { "pxa27x-pwm", 0 | HAS_SECONDARY_PWM },
31 { },
32};
33MODULE_DEVICE_TABLE(platform, pwm_id_table);
34
35/* 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;
45 struct pwm_device *secondary;
46 struct platform_device *pdev;
47
48 const char *label;
49 struct clk *clk;
50 int clk_enabled;
51 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
74 if (period_cycles < 1)
75 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{
102 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;
110}
111EXPORT_SYMBOL(pwm_enable);
112
113void pwm_disable(struct pwm_device *pwm)
114{
115 if (pwm->clk_enabled) {
116 clk_disable(pwm->clk);
117 pwm->clk_enabled = 0;
118 }
119}
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) {
133 if (pwm->pwm_id == pwm_id) {
134 found = 1;
135 break;
136 }
137 }
138
139 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);
147
148 mutex_unlock(&pwm_lock);
149 return pwm;
150}
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
174static int __devinit pwm_probe(struct platform_device *pdev)
175{
176 struct platform_device_id *id = platform_get_device_id(pdev);
177 struct pwm_device *pwm, *secondary = NULL;
178 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");
184 return -ENOMEM;
185 }
186
187 pwm->clk = clk_get(&pdev->dev, NULL);
188 if (IS_ERR(pwm->clk)) {
189 ret = PTR_ERR(pwm->clk);
190 goto err_free;
191 }
192 pwm->clk_enabled = 0;
193
194 pwm->use_count = 0;
195 pwm->pwm_id = PWM_ID_BASE(id->driver_data) + pdev->id;
196 pwm->pdev = pdev;
197
198 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
219 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
234 __add_pwm(pwm);
235 if (secondary)
236 __add_pwm(secondary);
237
238 platform_set_drvdata(pdev, pwm);
239 return 0;
240
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);
247 return ret;
248}
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);
260
261 if (pwm->secondary) {
262 list_del(&pwm->secondary->node);
263 kfree(pwm->secondary);
264 }
265
266 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
279static struct platform_driver pwm_driver = {
280 .driver = {
281 .name = "pxa25x-pwm",
282 .owner = THIS_MODULE,
283 },
284 .probe = pwm_probe,
285 .remove = __devexit_p(pwm_remove),
286 .id_table = pwm_id_table,
287};
288
289static int __init pwm_init(void)
290{
291 return platform_driver_register(&pwm_driver);
292}
293arch_initcall(pwm_init);
294
295static void __exit pwm_exit(void)
296{
297 platform_driver_unregister(&pwm_driver);
298}
299module_exit(pwm_exit);
300
301MODULE_LICENSE("GPL v2");