initial commit with v2.6.32.60
[linux-2.6.32.60-moxart.git] / arch / arm / plat-mxc / pwm.c
blob16404864fdee19deebe31cfd65a8e6041cb25548
1 /*
2 * simple driver for PWM (Pulse Width Modulator) controller
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
8 * Derived from pxa PWM driver by eric miao <eric.miao@marvell.com>
9 */
11 #include <linux/module.h>
12 #include <linux/kernel.h>
13 #include <linux/platform_device.h>
14 #include <linux/err.h>
15 #include <linux/clk.h>
16 #include <linux/io.h>
17 #include <linux/pwm.h>
18 #include <mach/hardware.h>
21 /* i.MX1 and i.MX21 share the same PWM function block: */
23 #define MX1_PWMC 0x00 /* PWM Control Register */
24 #define MX1_PWMS 0x04 /* PWM Sample Register */
25 #define MX1_PWMP 0x08 /* PWM Period Register */
28 /* i.MX27, i.MX31, i.MX35 share the same PWM function block: */
30 #define MX3_PWMCR 0x00 /* PWM Control Register */
31 #define MX3_PWMSAR 0x0C /* PWM Sample Register */
32 #define MX3_PWMPR 0x10 /* PWM Period Register */
33 #define MX3_PWMCR_PRESCALER(x) (((x - 1) & 0xFFF) << 4)
34 #define MX3_PWMCR_DOZEEN (1 << 24)
35 #define MX3_PWMCR_WAITEN (1 << 23)
36 #define MX3_PWMCR_DBGEN (1 << 22)
37 #define MX3_PWMCR_CLKSRC_IPG_HIGH (2 << 16)
38 #define MX3_PWMCR_CLKSRC_IPG (1 << 16)
39 #define MX3_PWMCR_EN (1 << 0)
43 struct pwm_device {
44 struct list_head node;
45 struct platform_device *pdev;
47 const char *label;
48 struct clk *clk;
50 int clk_enabled;
51 void __iomem *mmio_base;
53 unsigned int use_count;
54 unsigned int pwm_id;
57 int pwm_config(struct pwm_device *pwm, int duty_ns, int period_ns)
59 if (pwm == NULL || period_ns == 0 || duty_ns > period_ns)
60 return -EINVAL;
62 if (cpu_is_mx27() || cpu_is_mx3() || cpu_is_mx25()) {
63 unsigned long long c;
64 unsigned long period_cycles, duty_cycles, prescale;
65 u32 cr;
67 c = clk_get_rate(pwm->clk);
68 c = c * period_ns;
69 do_div(c, 1000000000);
70 period_cycles = c;
72 prescale = period_cycles / 0x10000 + 1;
74 period_cycles /= prescale;
75 c = (unsigned long long)period_cycles * duty_ns;
76 do_div(c, period_ns);
77 duty_cycles = c;
80 * according to imx pwm RM, the real period value should be
81 * PERIOD value in PWMPR plus 2.
83 if (period_cycles > 2)
84 period_cycles -= 2;
85 else
86 period_cycles = 0;
88 writel(duty_cycles, pwm->mmio_base + MX3_PWMSAR);
89 writel(period_cycles, pwm->mmio_base + MX3_PWMPR);
91 cr = MX3_PWMCR_PRESCALER(prescale) |
92 MX3_PWMCR_DOZEEN | MX3_PWMCR_WAITEN |
93 MX3_PWMCR_DBGEN | MX3_PWMCR_EN;
95 if (cpu_is_mx25())
96 cr |= MX3_PWMCR_CLKSRC_IPG;
97 else
98 cr |= MX3_PWMCR_CLKSRC_IPG_HIGH;
100 writel(cr, pwm->mmio_base + MX3_PWMCR);
101 } else if (cpu_is_mx1() || cpu_is_mx21()) {
102 /* The PWM subsystem allows for exact frequencies. However,
103 * I cannot connect a scope on my device to the PWM line and
104 * thus cannot provide the program the PWM controller
105 * exactly. Instead, I'm relying on the fact that the
106 * Bootloader (u-boot or WinCE+haret) has programmed the PWM
107 * function group already. So I'll just modify the PWM sample
108 * register to follow the ratio of duty_ns vs. period_ns
109 * accordingly.
111 * This is good enought for programming the brightness of
112 * the LCD backlight.
114 * The real implementation would divide PERCLK[0] first by
115 * both the prescaler (/1 .. /128) and then by CLKSEL
116 * (/2 .. /16).
118 u32 max = readl(pwm->mmio_base + MX1_PWMP);
119 u32 p = max * duty_ns / period_ns;
120 writel(max - p, pwm->mmio_base + MX1_PWMS);
121 } else {
122 BUG();
125 return 0;
127 EXPORT_SYMBOL(pwm_config);
129 int pwm_enable(struct pwm_device *pwm)
131 int rc = 0;
133 if (!pwm->clk_enabled) {
134 rc = clk_enable(pwm->clk);
135 if (!rc)
136 pwm->clk_enabled = 1;
138 return rc;
140 EXPORT_SYMBOL(pwm_enable);
142 void pwm_disable(struct pwm_device *pwm)
144 writel(0, pwm->mmio_base + MX3_PWMCR);
146 if (pwm->clk_enabled) {
147 clk_disable(pwm->clk);
148 pwm->clk_enabled = 0;
151 EXPORT_SYMBOL(pwm_disable);
153 static DEFINE_MUTEX(pwm_lock);
154 static LIST_HEAD(pwm_list);
156 struct pwm_device *pwm_request(int pwm_id, const char *label)
158 struct pwm_device *pwm;
159 int found = 0;
161 mutex_lock(&pwm_lock);
163 list_for_each_entry(pwm, &pwm_list, node) {
164 if (pwm->pwm_id == pwm_id) {
165 found = 1;
166 break;
170 if (found) {
171 if (pwm->use_count == 0) {
172 pwm->use_count++;
173 pwm->label = label;
174 } else
175 pwm = ERR_PTR(-EBUSY);
176 } else
177 pwm = ERR_PTR(-ENOENT);
179 mutex_unlock(&pwm_lock);
180 return pwm;
182 EXPORT_SYMBOL(pwm_request);
184 void pwm_free(struct pwm_device *pwm)
186 mutex_lock(&pwm_lock);
188 if (pwm->use_count) {
189 pwm->use_count--;
190 pwm->label = NULL;
191 } else
192 pr_warning("PWM device already freed\n");
194 mutex_unlock(&pwm_lock);
196 EXPORT_SYMBOL(pwm_free);
198 static int __devinit mxc_pwm_probe(struct platform_device *pdev)
200 struct pwm_device *pwm;
201 struct resource *r;
202 int ret = 0;
204 pwm = kzalloc(sizeof(struct pwm_device), GFP_KERNEL);
205 if (pwm == NULL) {
206 dev_err(&pdev->dev, "failed to allocate memory\n");
207 return -ENOMEM;
210 pwm->clk = clk_get(&pdev->dev, "pwm");
212 if (IS_ERR(pwm->clk)) {
213 ret = PTR_ERR(pwm->clk);
214 goto err_free;
217 pwm->clk_enabled = 0;
219 pwm->use_count = 0;
220 pwm->pwm_id = pdev->id;
221 pwm->pdev = pdev;
223 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
224 if (r == NULL) {
225 dev_err(&pdev->dev, "no memory resource defined\n");
226 ret = -ENODEV;
227 goto err_free_clk;
230 r = request_mem_region(r->start, r->end - r->start + 1, pdev->name);
231 if (r == NULL) {
232 dev_err(&pdev->dev, "failed to request memory resource\n");
233 ret = -EBUSY;
234 goto err_free_clk;
237 pwm->mmio_base = ioremap(r->start, r->end - r->start + 1);
238 if (pwm->mmio_base == NULL) {
239 dev_err(&pdev->dev, "failed to ioremap() registers\n");
240 ret = -ENODEV;
241 goto err_free_mem;
244 mutex_lock(&pwm_lock);
245 list_add_tail(&pwm->node, &pwm_list);
246 mutex_unlock(&pwm_lock);
248 platform_set_drvdata(pdev, pwm);
249 return 0;
251 err_free_mem:
252 release_mem_region(r->start, r->end - r->start + 1);
253 err_free_clk:
254 clk_put(pwm->clk);
255 err_free:
256 kfree(pwm);
257 return ret;
260 static int __devexit mxc_pwm_remove(struct platform_device *pdev)
262 struct pwm_device *pwm;
263 struct resource *r;
265 pwm = platform_get_drvdata(pdev);
266 if (pwm == NULL)
267 return -ENODEV;
269 mutex_lock(&pwm_lock);
270 list_del(&pwm->node);
271 mutex_unlock(&pwm_lock);
273 iounmap(pwm->mmio_base);
275 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
276 release_mem_region(r->start, r->end - r->start + 1);
278 clk_put(pwm->clk);
280 kfree(pwm);
281 return 0;
284 static struct platform_driver mxc_pwm_driver = {
285 .driver = {
286 .name = "mxc_pwm",
288 .probe = mxc_pwm_probe,
289 .remove = __devexit_p(mxc_pwm_remove),
292 static int __init mxc_pwm_init(void)
294 return platform_driver_register(&mxc_pwm_driver);
296 arch_initcall(mxc_pwm_init);
298 static void __exit mxc_pwm_exit(void)
300 platform_driver_unregister(&mxc_pwm_driver);
302 module_exit(mxc_pwm_exit);
304 MODULE_LICENSE("GPL v2");
305 MODULE_AUTHOR("Sascha Hauer <s.hauer@pengutronix.de>");