b8e98d7f5fe8d1c1d40e25daef3558a696798b01
[linux-2.6.git] / arch / arm / plat-mxc / pwm.c
1 /*
2  * simple driver for PWM (Pulse Width Modulator) controller
3  *
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.
7  *
8  * Derived from pxa PWM driver by eric miao <eric.miao@marvell.com>
9  */
10
11 #include <linux/module.h>
12 #include <linux/kernel.h>
13 #include <linux/platform_device.h>
14 #include <linux/slab.h>
15 #include <linux/err.h>
16 #include <linux/clk.h>
17 #include <linux/io.h>
18 #include <linux/pwm.h>
19 #include <mach/hardware.h>
20
21
22 /* i.MX1 and i.MX21 share the same PWM function block: */
23
24 #define MX1_PWMC    0x00   /* PWM Control Register */
25 #define MX1_PWMS    0x04   /* PWM Sample Register */
26 #define MX1_PWMP    0x08   /* PWM Period Register */
27
28
29 /* i.MX27, i.MX31, i.MX35 share the same PWM function block: */
30
31 #define MX3_PWMCR                 0x00    /* PWM Control Register */
32 #define MX3_PWMSAR                0x0C    /* PWM Sample Register */
33 #define MX3_PWMPR                 0x10    /* PWM Period Register */
34 #define MX3_PWMCR_PRESCALER(x)    (((x - 1) & 0xFFF) << 4)
35 #define MX3_PWMCR_DOZEEN                (1 << 24)
36 #define MX3_PWMCR_WAITEN                (1 << 23)
37 #define MX3_PWMCR_DBGEN                 (1 << 22)
38 #define MX3_PWMCR_CLKSRC_IPG_HIGH (2 << 16)
39 #define MX3_PWMCR_CLKSRC_IPG      (1 << 16)
40 #define MX3_PWMCR_EN              (1 << 0)
41
42
43
44 struct pwm_device {
45         struct list_head        node;
46         struct platform_device *pdev;
47
48         const char      *label;
49         struct clk      *clk;
50
51         int             clk_enabled;
52         void __iomem    *mmio_base;
53
54         unsigned int    use_count;
55         unsigned int    pwm_id;
56 };
57
58 int pwm_config(struct pwm_device *pwm, int duty_ns, int period_ns)
59 {
60         if (pwm == NULL || period_ns == 0 || duty_ns > period_ns)
61                 return -EINVAL;
62
63         if (cpu_is_mx27() || cpu_is_mx3() || cpu_is_mx25() || cpu_is_mx51()) {
64                 unsigned long long c;
65                 unsigned long period_cycles, duty_cycles, prescale;
66                 u32 cr;
67
68                 c = clk_get_rate(pwm->clk);
69                 c = c * period_ns;
70                 do_div(c, 1000000000);
71                 period_cycles = c;
72
73                 prescale = period_cycles / 0x10000 + 1;
74
75                 period_cycles /= prescale;
76                 c = (unsigned long long)period_cycles * duty_ns;
77                 do_div(c, period_ns);
78                 duty_cycles = c;
79
80                 writel(duty_cycles, pwm->mmio_base + MX3_PWMSAR);
81                 writel(period_cycles, pwm->mmio_base + MX3_PWMPR);
82
83                 cr = MX3_PWMCR_PRESCALER(prescale) |
84                         MX3_PWMCR_DOZEEN | MX3_PWMCR_WAITEN |
85                         MX3_PWMCR_DBGEN | MX3_PWMCR_EN;
86
87                 if (cpu_is_mx25())
88                         cr |= MX3_PWMCR_CLKSRC_IPG;
89                 else
90                         cr |= MX3_PWMCR_CLKSRC_IPG_HIGH;
91
92                 writel(cr, pwm->mmio_base + MX3_PWMCR);
93         } else if (cpu_is_mx1() || cpu_is_mx21()) {
94                 /* The PWM subsystem allows for exact frequencies. However,
95                  * I cannot connect a scope on my device to the PWM line and
96                  * thus cannot provide the program the PWM controller
97                  * exactly. Instead, I'm relying on the fact that the
98                  * Bootloader (u-boot or WinCE+haret) has programmed the PWM
99                  * function group already. So I'll just modify the PWM sample
100                  * register to follow the ratio of duty_ns vs. period_ns
101                  * accordingly.
102                  *
103                  * This is good enough for programming the brightness of
104                  * the LCD backlight.
105                  *
106                  * The real implementation would divide PERCLK[0] first by
107                  * both the prescaler (/1 .. /128) and then by CLKSEL
108                  * (/2 .. /16).
109                  */
110                 u32 max = readl(pwm->mmio_base + MX1_PWMP);
111                 u32 p = max * duty_ns / period_ns;
112                 writel(max - p, pwm->mmio_base + MX1_PWMS);
113         } else {
114                 BUG();
115         }
116
117         return 0;
118 }
119 EXPORT_SYMBOL(pwm_config);
120
121 int pwm_enable(struct pwm_device *pwm)
122 {
123         int rc = 0;
124
125         if (!pwm->clk_enabled) {
126                 rc = clk_enable(pwm->clk);
127                 if (!rc)
128                         pwm->clk_enabled = 1;
129         }
130         return rc;
131 }
132 EXPORT_SYMBOL(pwm_enable);
133
134 void pwm_disable(struct pwm_device *pwm)
135 {
136         writel(0, pwm->mmio_base + MX3_PWMCR);
137
138         if (pwm->clk_enabled) {
139                 clk_disable(pwm->clk);
140                 pwm->clk_enabled = 0;
141         }
142 }
143 EXPORT_SYMBOL(pwm_disable);
144
145 static DEFINE_MUTEX(pwm_lock);
146 static LIST_HEAD(pwm_list);
147
148 struct pwm_device *pwm_request(int pwm_id, const char *label)
149 {
150         struct pwm_device *pwm;
151         int found = 0;
152
153         mutex_lock(&pwm_lock);
154
155         list_for_each_entry(pwm, &pwm_list, node) {
156                 if (pwm->pwm_id == pwm_id) {
157                         found = 1;
158                         break;
159                 }
160         }
161
162         if (found) {
163                 if (pwm->use_count == 0) {
164                         pwm->use_count++;
165                         pwm->label = label;
166                 } else
167                         pwm = ERR_PTR(-EBUSY);
168         } else
169                 pwm = ERR_PTR(-ENOENT);
170
171         mutex_unlock(&pwm_lock);
172         return pwm;
173 }
174 EXPORT_SYMBOL(pwm_request);
175
176 void pwm_free(struct pwm_device *pwm)
177 {
178         mutex_lock(&pwm_lock);
179
180         if (pwm->use_count) {
181                 pwm->use_count--;
182                 pwm->label = NULL;
183         } else
184                 pr_warning("PWM device already freed\n");
185
186         mutex_unlock(&pwm_lock);
187 }
188 EXPORT_SYMBOL(pwm_free);
189
190 static int __devinit mxc_pwm_probe(struct platform_device *pdev)
191 {
192         struct pwm_device *pwm;
193         struct resource *r;
194         int ret = 0;
195
196         pwm = kzalloc(sizeof(struct pwm_device), GFP_KERNEL);
197         if (pwm == NULL) {
198                 dev_err(&pdev->dev, "failed to allocate memory\n");
199                 return -ENOMEM;
200         }
201
202         pwm->clk = clk_get(&pdev->dev, "pwm");
203
204         if (IS_ERR(pwm->clk)) {
205                 ret = PTR_ERR(pwm->clk);
206                 goto err_free;
207         }
208
209         pwm->clk_enabled = 0;
210
211         pwm->use_count = 0;
212         pwm->pwm_id = pdev->id;
213         pwm->pdev = pdev;
214
215         r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
216         if (r == NULL) {
217                 dev_err(&pdev->dev, "no memory resource defined\n");
218                 ret = -ENODEV;
219                 goto err_free_clk;
220         }
221
222         r = request_mem_region(r->start, resource_size(r), pdev->name);
223         if (r == NULL) {
224                 dev_err(&pdev->dev, "failed to request memory resource\n");
225                 ret = -EBUSY;
226                 goto err_free_clk;
227         }
228
229         pwm->mmio_base = ioremap(r->start, resource_size(r));
230         if (pwm->mmio_base == NULL) {
231                 dev_err(&pdev->dev, "failed to ioremap() registers\n");
232                 ret = -ENODEV;
233                 goto err_free_mem;
234         }
235
236         mutex_lock(&pwm_lock);
237         list_add_tail(&pwm->node, &pwm_list);
238         mutex_unlock(&pwm_lock);
239
240         platform_set_drvdata(pdev, pwm);
241         return 0;
242
243 err_free_mem:
244         release_mem_region(r->start, resource_size(r));
245 err_free_clk:
246         clk_put(pwm->clk);
247 err_free:
248         kfree(pwm);
249         return ret;
250 }
251
252 static int __devexit mxc_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, resource_size(r));
269
270         clk_put(pwm->clk);
271
272         kfree(pwm);
273         return 0;
274 }
275
276 static struct platform_driver mxc_pwm_driver = {
277         .driver         = {
278                 .name   = "mxc_pwm",
279         },
280         .probe          = mxc_pwm_probe,
281         .remove         = __devexit_p(mxc_pwm_remove),
282 };
283
284 static int __init mxc_pwm_init(void)
285 {
286         return platform_driver_register(&mxc_pwm_driver);
287 }
288 arch_initcall(mxc_pwm_init);
289
290 static void __exit mxc_pwm_exit(void)
291 {
292         platform_driver_unregister(&mxc_pwm_driver);
293 }
294 module_exit(mxc_pwm_exit);
295
296 MODULE_LICENSE("GPL v2");
297 MODULE_AUTHOR("Sascha Hauer <s.hauer@pengutronix.de>");