ALSA: hda - Fix silent output on ASUS A6Rp
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / arch / arm / plat-mxc / pwm.c
blobf4b68beddbb3df49a6608589357a9962799426d4
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/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>
22 /* i.MX1 and i.MX21 share the same PWM function block: */
24 #define MX1_PWMC 0x00 /* PWM Control Register */
25 #define MX1_PWMS 0x04 /* PWM Sample Register */
26 #define MX1_PWMP 0x08 /* PWM Period Register */
29 /* i.MX27, i.MX31, i.MX35 share the same PWM function block: */
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)
44 struct pwm_device {
45 struct list_head node;
46 struct platform_device *pdev;
48 const char *label;
49 struct clk *clk;
51 int clk_enabled;
52 void __iomem *mmio_base;
54 unsigned int use_count;
55 unsigned int pwm_id;
58 int pwm_config(struct pwm_device *pwm, int duty_ns, int period_ns)
60 if (pwm == NULL || period_ns == 0 || duty_ns > period_ns)
61 return -EINVAL;
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;
68 c = clk_get_rate(pwm->clk);
69 c = c * period_ns;
70 do_div(c, 1000000000);
71 period_cycles = c;
73 prescale = period_cycles / 0x10000 + 1;
75 period_cycles /= prescale;
76 c = (unsigned long long)period_cycles * duty_ns;
77 do_div(c, period_ns);
78 duty_cycles = c;
81 * according to imx pwm RM, the real period value should be
82 * PERIOD value in PWMPR plus 2.
84 if (period_cycles > 2)
85 period_cycles -= 2;
86 else
87 period_cycles = 0;
89 writel(duty_cycles, pwm->mmio_base + MX3_PWMSAR);
90 writel(period_cycles, pwm->mmio_base + MX3_PWMPR);
92 cr = MX3_PWMCR_PRESCALER(prescale) |
93 MX3_PWMCR_DOZEEN | MX3_PWMCR_WAITEN |
94 MX3_PWMCR_DBGEN | MX3_PWMCR_EN;
96 if (cpu_is_mx25())
97 cr |= MX3_PWMCR_CLKSRC_IPG;
98 else
99 cr |= MX3_PWMCR_CLKSRC_IPG_HIGH;
101 writel(cr, pwm->mmio_base + MX3_PWMCR);
102 } else if (cpu_is_mx1() || cpu_is_mx21()) {
103 /* The PWM subsystem allows for exact frequencies. However,
104 * I cannot connect a scope on my device to the PWM line and
105 * thus cannot provide the program the PWM controller
106 * exactly. Instead, I'm relying on the fact that the
107 * Bootloader (u-boot or WinCE+haret) has programmed the PWM
108 * function group already. So I'll just modify the PWM sample
109 * register to follow the ratio of duty_ns vs. period_ns
110 * accordingly.
112 * This is good enough for programming the brightness of
113 * the LCD backlight.
115 * The real implementation would divide PERCLK[0] first by
116 * both the prescaler (/1 .. /128) and then by CLKSEL
117 * (/2 .. /16).
119 u32 max = readl(pwm->mmio_base + MX1_PWMP);
120 u32 p = max * duty_ns / period_ns;
121 writel(max - p, pwm->mmio_base + MX1_PWMS);
122 } else {
123 BUG();
126 return 0;
128 EXPORT_SYMBOL(pwm_config);
130 int pwm_enable(struct pwm_device *pwm)
132 int rc = 0;
134 if (!pwm->clk_enabled) {
135 rc = clk_enable(pwm->clk);
136 if (!rc)
137 pwm->clk_enabled = 1;
139 return rc;
141 EXPORT_SYMBOL(pwm_enable);
143 void pwm_disable(struct pwm_device *pwm)
145 writel(0, pwm->mmio_base + MX3_PWMCR);
147 if (pwm->clk_enabled) {
148 clk_disable(pwm->clk);
149 pwm->clk_enabled = 0;
152 EXPORT_SYMBOL(pwm_disable);
154 static DEFINE_MUTEX(pwm_lock);
155 static LIST_HEAD(pwm_list);
157 struct pwm_device *pwm_request(int pwm_id, const char *label)
159 struct pwm_device *pwm;
160 int found = 0;
162 mutex_lock(&pwm_lock);
164 list_for_each_entry(pwm, &pwm_list, node) {
165 if (pwm->pwm_id == pwm_id) {
166 found = 1;
167 break;
171 if (found) {
172 if (pwm->use_count == 0) {
173 pwm->use_count++;
174 pwm->label = label;
175 } else
176 pwm = ERR_PTR(-EBUSY);
177 } else
178 pwm = ERR_PTR(-ENOENT);
180 mutex_unlock(&pwm_lock);
181 return pwm;
183 EXPORT_SYMBOL(pwm_request);
185 void pwm_free(struct pwm_device *pwm)
187 mutex_lock(&pwm_lock);
189 if (pwm->use_count) {
190 pwm->use_count--;
191 pwm->label = NULL;
192 } else
193 pr_warning("PWM device already freed\n");
195 mutex_unlock(&pwm_lock);
197 EXPORT_SYMBOL(pwm_free);
199 static int __devinit mxc_pwm_probe(struct platform_device *pdev)
201 struct pwm_device *pwm;
202 struct resource *r;
203 int ret = 0;
205 pwm = kzalloc(sizeof(struct pwm_device), GFP_KERNEL);
206 if (pwm == NULL) {
207 dev_err(&pdev->dev, "failed to allocate memory\n");
208 return -ENOMEM;
211 pwm->clk = clk_get(&pdev->dev, "pwm");
213 if (IS_ERR(pwm->clk)) {
214 ret = PTR_ERR(pwm->clk);
215 goto err_free;
218 pwm->clk_enabled = 0;
220 pwm->use_count = 0;
221 pwm->pwm_id = pdev->id;
222 pwm->pdev = pdev;
224 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
225 if (r == NULL) {
226 dev_err(&pdev->dev, "no memory resource defined\n");
227 ret = -ENODEV;
228 goto err_free_clk;
231 r = request_mem_region(r->start, r->end - r->start + 1, pdev->name);
232 if (r == NULL) {
233 dev_err(&pdev->dev, "failed to request memory resource\n");
234 ret = -EBUSY;
235 goto err_free_clk;
238 pwm->mmio_base = ioremap(r->start, r->end - r->start + 1);
239 if (pwm->mmio_base == NULL) {
240 dev_err(&pdev->dev, "failed to ioremap() registers\n");
241 ret = -ENODEV;
242 goto err_free_mem;
245 mutex_lock(&pwm_lock);
246 list_add_tail(&pwm->node, &pwm_list);
247 mutex_unlock(&pwm_lock);
249 platform_set_drvdata(pdev, pwm);
250 return 0;
252 err_free_mem:
253 release_mem_region(r->start, r->end - r->start + 1);
254 err_free_clk:
255 clk_put(pwm->clk);
256 err_free:
257 kfree(pwm);
258 return ret;
261 static int __devexit mxc_pwm_remove(struct platform_device *pdev)
263 struct pwm_device *pwm;
264 struct resource *r;
266 pwm = platform_get_drvdata(pdev);
267 if (pwm == NULL)
268 return -ENODEV;
270 mutex_lock(&pwm_lock);
271 list_del(&pwm->node);
272 mutex_unlock(&pwm_lock);
274 iounmap(pwm->mmio_base);
276 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
277 release_mem_region(r->start, r->end - r->start + 1);
279 clk_put(pwm->clk);
281 kfree(pwm);
282 return 0;
285 static struct platform_driver mxc_pwm_driver = {
286 .driver = {
287 .name = "mxc_pwm",
289 .probe = mxc_pwm_probe,
290 .remove = __devexit_p(mxc_pwm_remove),
293 static int __init mxc_pwm_init(void)
295 return platform_driver_register(&mxc_pwm_driver);
297 arch_initcall(mxc_pwm_init);
299 static void __exit mxc_pwm_exit(void)
301 platform_driver_unregister(&mxc_pwm_driver);
303 module_exit(mxc_pwm_exit);
305 MODULE_LICENSE("GPL v2");
306 MODULE_AUTHOR("Sascha Hauer <s.hauer@pengutronix.de>");