OMAP3: PM: Ensure MUSB block can idle when driver not loaded
[linux-ginger.git] / arch / arm / mach-pxa / pwm.c
blobfcdd374437a866c5b8fcd0203bac9a95f6ec9d65
1 /*
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>
19 #include <linux/io.h>
20 #include <linux/pwm.h>
22 #include <asm/div64.h>
24 /* PWM registers and bits definitions */
25 #define PWMCR (0x00)
26 #define PWMDCR (0x04)
27 #define PWMPCR (0x08)
29 #define PWMCR_SD (1 << 6)
30 #define PWMDCR_FD (1 << 10)
32 struct pwm_device {
33 struct list_head node;
34 struct platform_device *pdev;
36 const char *label;
37 struct clk *clk;
38 int clk_enabled;
39 void __iomem *mmio_base;
41 unsigned int use_count;
42 unsigned int pwm_id;
46 * period_ns = 10^9 * (PRESCALE + 1) * (PV + 1) / PWM_CLK_RATE
47 * duty_ns = 10^9 * (PRESCALE + 1) * DC / PWM_CLK_RATE
49 int pwm_config(struct pwm_device *pwm, int duty_ns, int period_ns)
51 unsigned long long c;
52 unsigned long period_cycles, prescale, pv, dc;
54 if (pwm == NULL || period_ns == 0 || duty_ns > period_ns)
55 return -EINVAL;
57 c = clk_get_rate(pwm->clk);
58 c = c * period_ns;
59 do_div(c, 1000000000);
60 period_cycles = c;
62 if (period_cycles < 1)
63 period_cycles = 1;
64 prescale = (period_cycles - 1) / 1024;
65 pv = period_cycles / (prescale + 1) - 1;
67 if (prescale > 63)
68 return -EINVAL;
70 if (duty_ns == period_ns)
71 dc = PWMDCR_FD;
72 else
73 dc = (pv + 1) * duty_ns / period_ns;
75 /* NOTE: the clock to PWM has to be enabled first
76 * before writing to the registers
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);
84 return 0;
86 EXPORT_SYMBOL(pwm_config);
88 int pwm_enable(struct pwm_device *pwm)
90 int rc = 0;
92 if (!pwm->clk_enabled) {
93 rc = clk_enable(pwm->clk);
94 if (!rc)
95 pwm->clk_enabled = 1;
97 return rc;
99 EXPORT_SYMBOL(pwm_enable);
101 void pwm_disable(struct pwm_device *pwm)
103 if (pwm->clk_enabled) {
104 clk_disable(pwm->clk);
105 pwm->clk_enabled = 0;
108 EXPORT_SYMBOL(pwm_disable);
110 static DEFINE_MUTEX(pwm_lock);
111 static LIST_HEAD(pwm_list);
113 struct pwm_device *pwm_request(int pwm_id, const char *label)
115 struct pwm_device *pwm;
116 int found = 0;
118 mutex_lock(&pwm_lock);
120 list_for_each_entry(pwm, &pwm_list, node) {
121 if (pwm->pwm_id == pwm_id) {
122 found = 1;
123 break;
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);
136 mutex_unlock(&pwm_lock);
137 return pwm;
139 EXPORT_SYMBOL(pwm_request);
141 void pwm_free(struct pwm_device *pwm)
143 mutex_lock(&pwm_lock);
145 if (pwm->use_count) {
146 pwm->use_count--;
147 pwm->label = NULL;
148 } else
149 pr_warning("PWM device already freed\n");
151 mutex_unlock(&pwm_lock);
153 EXPORT_SYMBOL(pwm_free);
155 static inline void __add_pwm(struct pwm_device *pwm)
157 mutex_lock(&pwm_lock);
158 list_add_tail(&pwm->node, &pwm_list);
159 mutex_unlock(&pwm_lock);
162 static struct pwm_device *pwm_probe(struct platform_device *pdev,
163 unsigned int pwm_id, struct pwm_device *parent_pwm)
165 struct pwm_device *pwm;
166 struct resource *r;
167 int ret = 0;
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);
175 pwm->clk = clk_get(&pdev->dev, NULL);
176 if (IS_ERR(pwm->clk)) {
177 ret = PTR_ERR(pwm->clk);
178 goto err_free;
180 pwm->clk_enabled = 0;
182 pwm->use_count = 0;
183 pwm->pwm_id = pwm_id;
184 pwm->pdev = pdev;
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;
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;
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;
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;
214 __add_pwm(pwm);
215 platform_set_drvdata(pdev, pwm);
216 return pwm;
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);
227 static int __devinit pxa25x_pwm_probe(struct platform_device *pdev)
229 struct pwm_device *pwm = pwm_probe(pdev, pdev->id, NULL);
231 if (IS_ERR(pwm))
232 return PTR_ERR(pwm);
234 return 0;
237 static int __devinit pxa27x_pwm_probe(struct platform_device *pdev)
239 struct pwm_device *pwm;
241 pwm = pwm_probe(pdev, pdev->id, NULL);
242 if (IS_ERR(pwm))
243 return PTR_ERR(pwm);
245 pwm = pwm_probe(pdev, pdev->id + 2, pwm);
246 if (IS_ERR(pwm))
247 return PTR_ERR(pwm);
249 return 0;
252 static int __devexit pwm_remove(struct platform_device *pdev)
254 struct pwm_device *pwm;
255 struct resource *r;
257 pwm = platform_get_drvdata(pdev);
258 if (pwm == NULL)
259 return -ENODEV;
261 mutex_lock(&pwm_lock);
262 list_del(&pwm->node);
263 mutex_unlock(&pwm_lock);
265 iounmap(pwm->mmio_base);
267 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
268 release_mem_region(r->start, r->end - r->start + 1);
270 clk_put(pwm->clk);
271 kfree(pwm);
272 return 0;
275 static struct platform_driver pxa25x_pwm_driver = {
276 .driver = {
277 .name = "pxa25x-pwm",
279 .probe = pxa25x_pwm_probe,
280 .remove = __devexit_p(pwm_remove),
283 static struct platform_driver pxa27x_pwm_driver = {
284 .driver = {
285 .name = "pxa27x-pwm",
287 .probe = pxa27x_pwm_probe,
288 .remove = __devexit_p(pwm_remove),
291 static int __init pwm_init(void)
293 int ret = 0;
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;
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;
307 return ret;
309 arch_initcall(pwm_init);
311 static void __exit pwm_exit(void)
313 platform_driver_unregister(&pxa25x_pwm_driver);
314 platform_driver_unregister(&pxa27x_pwm_driver);
316 module_exit(pwm_exit);
318 MODULE_LICENSE("GPL v2");