spi-topcliff-pch: supports a spi mode setup and bit order setup by IO control
[zen-stable.git] / arch / arm / mach-vt8500 / pwm.c
blob8ad825e935921bf8f7be46e4993acddd69d4416d
1 /*
2 * arch/arm/mach-vt8500/pwm.c
4 * Copyright (C) 2010 Alexey Charkov <alchark@gmail.com>
6 * This software is licensed under the terms of the GNU General Public
7 * License version 2, as published by the Free Software Foundation, and
8 * may be copied, distributed, and modified under those terms.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
16 #include <linux/module.h>
17 #include <linux/kernel.h>
18 #include <linux/platform_device.h>
19 #include <linux/slab.h>
20 #include <linux/err.h>
21 #include <linux/io.h>
22 #include <linux/pwm.h>
23 #include <linux/delay.h>
25 #include <asm/div64.h>
27 #define VT8500_NR_PWMS 4
29 static DEFINE_MUTEX(pwm_lock);
30 static LIST_HEAD(pwm_list);
32 struct pwm_device {
33 struct list_head node;
34 struct platform_device *pdev;
36 const char *label;
38 void __iomem *regbase;
40 unsigned int use_count;
41 unsigned int pwm_id;
44 #define msecs_to_loops(t) (loops_per_jiffy / 1000 * HZ * t)
45 static inline void pwm_busy_wait(void __iomem *reg, u8 bitmask)
47 int loops = msecs_to_loops(10);
48 while ((readb(reg) & bitmask) && --loops)
49 cpu_relax();
51 if (unlikely(!loops))
52 pr_warning("Waiting for status bits 0x%x to clear timed out\n",
53 bitmask);
56 int pwm_config(struct pwm_device *pwm, int duty_ns, int period_ns)
58 unsigned long long c;
59 unsigned long period_cycles, prescale, pv, dc;
61 if (pwm == NULL || period_ns == 0 || duty_ns > period_ns)
62 return -EINVAL;
64 c = 25000000/2; /* wild guess --- need to implement clocks */
65 c = c * period_ns;
66 do_div(c, 1000000000);
67 period_cycles = c;
69 if (period_cycles < 1)
70 period_cycles = 1;
71 prescale = (period_cycles - 1) / 4096;
72 pv = period_cycles / (prescale + 1) - 1;
73 if (pv > 4095)
74 pv = 4095;
76 if (prescale > 1023)
77 return -EINVAL;
79 c = (unsigned long long)pv * duty_ns;
80 do_div(c, period_ns);
81 dc = c;
83 pwm_busy_wait(pwm->regbase + 0x40 + pwm->pwm_id, (1 << 1));
84 writel(prescale, pwm->regbase + 0x4 + (pwm->pwm_id << 4));
86 pwm_busy_wait(pwm->regbase + 0x40 + pwm->pwm_id, (1 << 2));
87 writel(pv, pwm->regbase + 0x8 + (pwm->pwm_id << 4));
89 pwm_busy_wait(pwm->regbase + 0x40 + pwm->pwm_id, (1 << 3));
90 writel(dc, pwm->regbase + 0xc + (pwm->pwm_id << 4));
92 return 0;
94 EXPORT_SYMBOL(pwm_config);
96 int pwm_enable(struct pwm_device *pwm)
98 pwm_busy_wait(pwm->regbase + 0x40 + pwm->pwm_id, (1 << 0));
99 writel(5, pwm->regbase + (pwm->pwm_id << 4));
100 return 0;
102 EXPORT_SYMBOL(pwm_enable);
104 void pwm_disable(struct pwm_device *pwm)
106 pwm_busy_wait(pwm->regbase + 0x40 + pwm->pwm_id, (1 << 0));
107 writel(0, pwm->regbase + (pwm->pwm_id << 4));
109 EXPORT_SYMBOL(pwm_disable);
111 struct pwm_device *pwm_request(int pwm_id, const char *label)
113 struct pwm_device *pwm;
114 int found = 0;
116 mutex_lock(&pwm_lock);
118 list_for_each_entry(pwm, &pwm_list, node) {
119 if (pwm->pwm_id == pwm_id) {
120 found = 1;
121 break;
125 if (found) {
126 if (pwm->use_count == 0) {
127 pwm->use_count++;
128 pwm->label = label;
129 } else {
130 pwm = ERR_PTR(-EBUSY);
132 } else {
133 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");
152 mutex_unlock(&pwm_lock);
154 EXPORT_SYMBOL(pwm_free);
156 static inline void __add_pwm(struct pwm_device *pwm)
158 mutex_lock(&pwm_lock);
159 list_add_tail(&pwm->node, &pwm_list);
160 mutex_unlock(&pwm_lock);
163 static int __devinit pwm_probe(struct platform_device *pdev)
165 struct pwm_device *pwms;
166 struct resource *r;
167 int ret = 0;
168 int i;
170 pwms = kzalloc(sizeof(struct pwm_device) * VT8500_NR_PWMS, GFP_KERNEL);
171 if (pwms == NULL) {
172 dev_err(&pdev->dev, "failed to allocate memory\n");
173 return -ENOMEM;
176 for (i = 0; i < VT8500_NR_PWMS; i++) {
177 pwms[i].use_count = 0;
178 pwms[i].pwm_id = i;
179 pwms[i].pdev = pdev;
182 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
183 if (r == NULL) {
184 dev_err(&pdev->dev, "no memory resource defined\n");
185 ret = -ENODEV;
186 goto err_free;
189 r = request_mem_region(r->start, resource_size(r), pdev->name);
190 if (r == NULL) {
191 dev_err(&pdev->dev, "failed to request memory resource\n");
192 ret = -EBUSY;
193 goto err_free;
196 pwms[0].regbase = ioremap(r->start, resource_size(r));
197 if (pwms[0].regbase == NULL) {
198 dev_err(&pdev->dev, "failed to ioremap() registers\n");
199 ret = -ENODEV;
200 goto err_free_mem;
203 for (i = 1; i < VT8500_NR_PWMS; i++)
204 pwms[i].regbase = pwms[0].regbase;
206 for (i = 0; i < VT8500_NR_PWMS; i++)
207 __add_pwm(&pwms[i]);
209 platform_set_drvdata(pdev, pwms);
210 return 0;
212 err_free_mem:
213 release_mem_region(r->start, resource_size(r));
214 err_free:
215 kfree(pwms);
216 return ret;
219 static int __devexit pwm_remove(struct platform_device *pdev)
221 struct pwm_device *pwms;
222 struct resource *r;
223 int i;
225 pwms = platform_get_drvdata(pdev);
226 if (pwms == NULL)
227 return -ENODEV;
229 mutex_lock(&pwm_lock);
231 for (i = 0; i < VT8500_NR_PWMS; i++)
232 list_del(&pwms[i].node);
233 mutex_unlock(&pwm_lock);
235 iounmap(pwms[0].regbase);
237 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
238 release_mem_region(r->start, resource_size(r));
240 kfree(pwms);
241 return 0;
244 static struct platform_driver pwm_driver = {
245 .driver = {
246 .name = "vt8500-pwm",
247 .owner = THIS_MODULE,
249 .probe = pwm_probe,
250 .remove = __devexit_p(pwm_remove),
253 static int __init pwm_init(void)
255 return platform_driver_register(&pwm_driver);
257 arch_initcall(pwm_init);
259 static void __exit pwm_exit(void)
261 platform_driver_unregister(&pwm_driver);
263 module_exit(pwm_exit);
265 MODULE_LICENSE("GPL");