aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/pwm
diff options
context:
space:
mode:
authorSascha Hauer <s.hauer@pengutronix.de>2012-03-15 05:04:37 -0400
committerThierry Reding <thierry.reding@avionic-design.de>2012-07-02 16:06:33 -0400
commita245ccebb4aad9fae60597d5cbad158c0de4483e (patch)
tree99f2cecbbacfb6c06d803b4e5e0e5cba16bf5d2d /drivers/pwm
parent215c29d3d0e925189ade522d1ea6052a320d7692 (diff)
ARM vt8500: Move vt8500 pwm driver to pwm framework
Move the driver to drivers/pwm/ and convert it to use the framework. Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de> Cc: Alexey Charkov <alchark@gmail.com> Signed-off-by: Thierry Reding <thierry.reding@avionic-design.de>
Diffstat (limited to 'drivers/pwm')
-rw-r--r--drivers/pwm/Kconfig9
-rw-r--r--drivers/pwm/Makefile1
-rw-r--r--drivers/pwm/pwm-vt8500.c208
3 files changed, 218 insertions, 0 deletions
diff --git a/drivers/pwm/Kconfig b/drivers/pwm/Kconfig
index 0d9aa92636c3..a93feffbc36a 100644
--- a/drivers/pwm/Kconfig
+++ b/drivers/pwm/Kconfig
@@ -55,4 +55,13 @@ config PWM_TEGRA
55 To compile this driver as a module, choose M here: the module 55 To compile this driver as a module, choose M here: the module
56 will be called pwm-tegra. 56 will be called pwm-tegra.
57 57
58config PWM_VT8500
59 tristate "vt8500 pwm support"
60 depends on ARCH_VT8500
61 help
62 Generic PWM framework driver for vt8500.
63
64 To compile this driver as a module, choose M here: the module
65 will be called pwm-vt8500.
66
58endif 67endif
diff --git a/drivers/pwm/Makefile b/drivers/pwm/Makefile
index a1d87b173312..b7c0fcf4de16 100644
--- a/drivers/pwm/Makefile
+++ b/drivers/pwm/Makefile
@@ -4,3 +4,4 @@ obj-$(CONFIG_PWM_IMX) += pwm-imx.o
4obj-$(CONFIG_PWM_PXA) += pwm-pxa.o 4obj-$(CONFIG_PWM_PXA) += pwm-pxa.o
5obj-$(CONFIG_PWM_SAMSUNG) += pwm-samsung.o 5obj-$(CONFIG_PWM_SAMSUNG) += pwm-samsung.o
6obj-$(CONFIG_PWM_TEGRA) += pwm-tegra.o 6obj-$(CONFIG_PWM_TEGRA) += pwm-tegra.o
7obj-$(CONFIG_PWM_VT8500) += pwm-vt8500.o
diff --git a/drivers/pwm/pwm-vt8500.c b/drivers/pwm/pwm-vt8500.c
new file mode 100644
index 000000000000..3db0746f7200
--- /dev/null
+++ b/drivers/pwm/pwm-vt8500.c
@@ -0,0 +1,208 @@
1/*
2 * arch/arm/mach-vt8500/pwm.c
3 *
4 * Copyright (C) 2010 Alexey Charkov <alchark@gmail.com>
5 *
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.
9 *
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.
14 */
15
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>
24
25#include <asm/div64.h>
26
27#define VT8500_NR_PWMS 4
28
29struct vt8500_chip {
30 struct pwm_chip chip;
31 void __iomem *base;
32};
33
34#define to_vt8500_chip(chip) container_of(chip, struct vt8500_chip, chip)
35
36#define msecs_to_loops(t) (loops_per_jiffy / 1000 * HZ * t)
37static inline void pwm_busy_wait(void __iomem *reg, u8 bitmask)
38{
39 int loops = msecs_to_loops(10);
40 while ((readb(reg) & bitmask) && --loops)
41 cpu_relax();
42
43 if (unlikely(!loops))
44 pr_warning("Waiting for status bits 0x%x to clear timed out\n",
45 bitmask);
46}
47
48static int vt8500_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm,
49 int duty_ns, int period_ns)
50{
51 struct vt8500_chip *vt8500 = to_vt8500_chip(chip);
52 unsigned long long c;
53 unsigned long period_cycles, prescale, pv, dc;
54
55 c = 25000000/2; /* wild guess --- need to implement clocks */
56 c = c * period_ns;
57 do_div(c, 1000000000);
58 period_cycles = c;
59
60 if (period_cycles < 1)
61 period_cycles = 1;
62 prescale = (period_cycles - 1) / 4096;
63 pv = period_cycles / (prescale + 1) - 1;
64 if (pv > 4095)
65 pv = 4095;
66
67 if (prescale > 1023)
68 return -EINVAL;
69
70 c = (unsigned long long)pv * duty_ns;
71 do_div(c, period_ns);
72 dc = c;
73
74 pwm_busy_wait(vt8500->base + 0x40 + pwm->hwpwm, (1 << 1));
75 writel(prescale, vt8500->base + 0x4 + (pwm->hwpwm << 4));
76
77 pwm_busy_wait(vt8500->base + 0x40 + pwm->hwpwm, (1 << 2));
78 writel(pv, vt8500->base + 0x8 + (pwm->hwpwm << 4));
79
80 pwm_busy_wait(vt8500->base + 0x40 + pwm->hwpwm, (1 << 3));
81 writel(dc, vt8500->base + 0xc + (pwm->hwpwm << 4));
82
83 return 0;
84}
85
86static int vt8500_pwm_enable(struct pwm_chip *chip, struct pwm_device *pwm)
87{
88 struct vt8500_chip *vt8500 = to_vt8500_chip(chip);
89
90 pwm_busy_wait(vt8500->base + 0x40 + pwm->hwpwm, (1 << 0));
91 writel(5, vt8500->base + (pwm->hwpwm << 4));
92 return 0;
93}
94
95static void vt8500_pwm_disable(struct pwm_chip *chip, struct pwm_device *pwm)
96{
97 struct vt8500_chip *vt8500 = to_vt8500_chip(chip);
98
99 pwm_busy_wait(vt8500->base + 0x40 + pwm->hwpwm, (1 << 0));
100 writel(0, vt8500->base + (pwm->hwpwm << 4));
101}
102
103static struct pwm_ops vt8500_pwm_ops = {
104 .enable = vt8500_pwm_enable,
105 .disable = vt8500_pwm_disable,
106 .config = vt8500_pwm_config,
107 .owner = THIS_MODULE,
108};
109
110static int __devinit pwm_probe(struct platform_device *pdev)
111{
112 struct vt8500_chip *chip;
113 struct resource *r;
114 int ret;
115
116 chip = kzalloc(sizeof(*chip), GFP_KERNEL);
117 if (chip == NULL) {
118 dev_err(&pdev->dev, "failed to allocate memory\n");
119 return -ENOMEM;
120 }
121
122 chip->chip.dev = &pdev->dev;
123 chip->chip.ops = &vt8500_pwm_ops;
124 chip->chip.base = -1;
125 chip->chip.npwm = VT8500_NR_PWMS;
126
127 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
128 if (r == NULL) {
129 dev_err(&pdev->dev, "no memory resource defined\n");
130 ret = -ENODEV;
131 goto err_free;
132 }
133
134 r = request_mem_region(r->start, resource_size(r), pdev->name);
135 if (r == NULL) {
136 dev_err(&pdev->dev, "failed to request memory resource\n");
137 ret = -EBUSY;
138 goto err_free;
139 }
140
141 chip->base = ioremap(r->start, resource_size(r));
142 if (chip->base == NULL) {
143 dev_err(&pdev->dev, "failed to ioremap() registers\n");
144 ret = -ENODEV;
145 goto err_free_mem;
146 }
147
148 ret = pwmchip_add(&chip->chip);
149 if (ret < 0)
150 goto err_unmap;
151
152 platform_set_drvdata(pdev, chip);
153 return ret;
154
155err_unmap:
156 iounmap(chip->base);
157err_free_mem:
158 release_mem_region(r->start, resource_size(r));
159err_free:
160 kfree(chip);
161 return ret;
162}
163
164static int __devexit pwm_remove(struct platform_device *pdev)
165{
166 struct vt8500_chip *chip;
167 struct resource *r;
168 int err;
169
170 chip = platform_get_drvdata(pdev);
171 if (chip == NULL)
172 return -ENODEV;
173
174 err = pwmchip_remove(&chip->chip);
175 if (err < 0)
176 return err;
177
178 iounmap(chip->base);
179
180 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
181 release_mem_region(r->start, resource_size(r));
182
183 kfree(chip);
184 return 0;
185}
186
187static struct platform_driver pwm_driver = {
188 .driver = {
189 .name = "vt8500-pwm",
190 .owner = THIS_MODULE,
191 },
192 .probe = pwm_probe,
193 .remove = __devexit_p(pwm_remove),
194};
195
196static int __init pwm_init(void)
197{
198 return platform_driver_register(&pwm_driver);
199}
200arch_initcall(pwm_init);
201
202static void __exit pwm_exit(void)
203{
204 platform_driver_unregister(&pwm_driver);
205}
206module_exit(pwm_exit);
207
208MODULE_LICENSE("GPL");