diff options
author | Tony Prisk <linux@prisktech.co.nz> | 2012-10-26 21:49:57 -0400 |
---|---|---|
committer | Thierry Reding <thierry.reding@avionic-design.de> | 2012-11-01 11:23:19 -0400 |
commit | 63e1ed2364050073770c085021377d7764969b85 (patch) | |
tree | e95f1e874f5c2096ff63567fcdf00c55fdb660d3 /drivers | |
parent | 8f0d8163b50e01f398b14bcd4dc039ac5ab18d64 (diff) |
pwm: vt8500: Update vt8500 PWM driver support
This patch updates pwm-vt8500.c to support devicetree probing and
make use of the common clock subsystem.
A binding document describing the PWM controller found on
arch-vt8500 is also included.
Signed-off-by: Tony Prisk <linux@prisktech.co.nz>
Acked-by: Arnd Bergmann <arnd@arndb.de>
Signed-off-by: Thierry Reding <thierry.reding@avionic-design.de>
Diffstat (limited to 'drivers')
-rw-r--r-- | drivers/pwm/pwm-vt8500.c | 86 |
1 files changed, 63 insertions, 23 deletions
diff --git a/drivers/pwm/pwm-vt8500.c b/drivers/pwm/pwm-vt8500.c index ad14389b7144..970b0c6e268f 100644 --- a/drivers/pwm/pwm-vt8500.c +++ b/drivers/pwm/pwm-vt8500.c | |||
@@ -1,7 +1,8 @@ | |||
1 | /* | 1 | /* |
2 | * drivers/pwm/pwm-vt8500.c | 2 | * drivers/pwm/pwm-vt8500.c |
3 | * | 3 | * |
4 | * Copyright (C) 2010 Alexey Charkov <alchark@gmail.com> | 4 | * Copyright (C) 2012 Tony Prisk <linux@prisktech.co.nz> |
5 | * Copyright (C) 2010 Alexey Charkov <alchark@gmail.com> | ||
5 | * | 6 | * |
6 | * This software is licensed under the terms of the GNU General Public | 7 | * 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 | * License version 2, as published by the Free Software Foundation, and |
@@ -21,14 +22,24 @@ | |||
21 | #include <linux/io.h> | 22 | #include <linux/io.h> |
22 | #include <linux/pwm.h> | 23 | #include <linux/pwm.h> |
23 | #include <linux/delay.h> | 24 | #include <linux/delay.h> |
25 | #include <linux/clk.h> | ||
24 | 26 | ||
25 | #include <asm/div64.h> | 27 | #include <asm/div64.h> |
26 | 28 | ||
27 | #define VT8500_NR_PWMS 4 | 29 | #include <linux/of.h> |
30 | #include <linux/of_device.h> | ||
31 | #include <linux/of_address.h> | ||
32 | |||
33 | /* | ||
34 | * SoC architecture allocates register space for 4 PWMs but only | ||
35 | * 2 are currently implemented. | ||
36 | */ | ||
37 | #define VT8500_NR_PWMS 2 | ||
28 | 38 | ||
29 | struct vt8500_chip { | 39 | struct vt8500_chip { |
30 | struct pwm_chip chip; | 40 | struct pwm_chip chip; |
31 | void __iomem *base; | 41 | void __iomem *base; |
42 | struct clk *clk; | ||
32 | }; | 43 | }; |
33 | 44 | ||
34 | #define to_vt8500_chip(chip) container_of(chip, struct vt8500_chip, chip) | 45 | #define to_vt8500_chip(chip) container_of(chip, struct vt8500_chip, chip) |
@@ -52,7 +63,7 @@ static int vt8500_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm, | |||
52 | unsigned long long c; | 63 | unsigned long long c; |
53 | unsigned long period_cycles, prescale, pv, dc; | 64 | unsigned long period_cycles, prescale, pv, dc; |
54 | 65 | ||
55 | c = 25000000/2; /* wild guess --- need to implement clocks */ | 66 | c = clk_get_rate(vt8500->clk); |
56 | c = c * period_ns; | 67 | c = c * period_ns; |
57 | do_div(c, 1000000000); | 68 | do_div(c, 1000000000); |
58 | period_cycles = c; | 69 | period_cycles = c; |
@@ -85,8 +96,15 @@ static int vt8500_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm, | |||
85 | 96 | ||
86 | static int vt8500_pwm_enable(struct pwm_chip *chip, struct pwm_device *pwm) | 97 | static int vt8500_pwm_enable(struct pwm_chip *chip, struct pwm_device *pwm) |
87 | { | 98 | { |
99 | int err; | ||
88 | struct vt8500_chip *vt8500 = to_vt8500_chip(chip); | 100 | struct vt8500_chip *vt8500 = to_vt8500_chip(chip); |
89 | 101 | ||
102 | err = clk_enable(vt8500->clk); | ||
103 | if (err < 0) | ||
104 | dev_err(chip->dev, "failed to enable clock\n"); | ||
105 | return err; | ||
106 | }; | ||
107 | |||
90 | pwm_busy_wait(vt8500->base + 0x40 + pwm->hwpwm, (1 << 0)); | 108 | pwm_busy_wait(vt8500->base + 0x40 + pwm->hwpwm, (1 << 0)); |
91 | writel(5, vt8500->base + (pwm->hwpwm << 4)); | 109 | writel(5, vt8500->base + (pwm->hwpwm << 4)); |
92 | return 0; | 110 | return 0; |
@@ -98,6 +116,8 @@ static void vt8500_pwm_disable(struct pwm_chip *chip, struct pwm_device *pwm) | |||
98 | 116 | ||
99 | pwm_busy_wait(vt8500->base + 0x40 + pwm->hwpwm, (1 << 0)); | 117 | pwm_busy_wait(vt8500->base + 0x40 + pwm->hwpwm, (1 << 0)); |
100 | writel(0, vt8500->base + (pwm->hwpwm << 4)); | 118 | writel(0, vt8500->base + (pwm->hwpwm << 4)); |
119 | |||
120 | clk_disable(vt8500->clk); | ||
101 | } | 121 | } |
102 | 122 | ||
103 | static struct pwm_ops vt8500_pwm_ops = { | 123 | static struct pwm_ops vt8500_pwm_ops = { |
@@ -107,12 +127,24 @@ static struct pwm_ops vt8500_pwm_ops = { | |||
107 | .owner = THIS_MODULE, | 127 | .owner = THIS_MODULE, |
108 | }; | 128 | }; |
109 | 129 | ||
110 | static int __devinit pwm_probe(struct platform_device *pdev) | 130 | static const struct of_device_id vt8500_pwm_dt_ids[] = { |
131 | { .compatible = "via,vt8500-pwm", }, | ||
132 | { /* Sentinel */ } | ||
133 | }; | ||
134 | MODULE_DEVICE_TABLE(of, vt8500_pwm_dt_ids); | ||
135 | |||
136 | static int vt8500_pwm_probe(struct platform_device *pdev) | ||
111 | { | 137 | { |
112 | struct vt8500_chip *chip; | 138 | struct vt8500_chip *chip; |
113 | struct resource *r; | 139 | struct resource *r; |
140 | struct device_node *np = pdev->dev.of_node; | ||
114 | int ret; | 141 | int ret; |
115 | 142 | ||
143 | if (!np) { | ||
144 | dev_err(&pdev->dev, "invalid devicetree node\n"); | ||
145 | return -EINVAL; | ||
146 | } | ||
147 | |||
116 | chip = devm_kzalloc(&pdev->dev, sizeof(*chip), GFP_KERNEL); | 148 | chip = devm_kzalloc(&pdev->dev, sizeof(*chip), GFP_KERNEL); |
117 | if (chip == NULL) { | 149 | if (chip == NULL) { |
118 | dev_err(&pdev->dev, "failed to allocate memory\n"); | 150 | dev_err(&pdev->dev, "failed to allocate memory\n"); |
@@ -124,6 +156,12 @@ static int __devinit pwm_probe(struct platform_device *pdev) | |||
124 | chip->chip.base = -1; | 156 | chip->chip.base = -1; |
125 | chip->chip.npwm = VT8500_NR_PWMS; | 157 | chip->chip.npwm = VT8500_NR_PWMS; |
126 | 158 | ||
159 | chip->clk = devm_clk_get(&pdev->dev, NULL); | ||
160 | if (IS_ERR(chip->clk)) { | ||
161 | dev_err(&pdev->dev, "clock source not specified\n"); | ||
162 | return PTR_ERR(chip->clk); | ||
163 | } | ||
164 | |||
127 | r = platform_get_resource(pdev, IORESOURCE_MEM, 0); | 165 | r = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
128 | if (r == NULL) { | 166 | if (r == NULL) { |
129 | dev_err(&pdev->dev, "no memory resource defined\n"); | 167 | dev_err(&pdev->dev, "no memory resource defined\n"); |
@@ -131,18 +169,26 @@ static int __devinit pwm_probe(struct platform_device *pdev) | |||
131 | } | 169 | } |
132 | 170 | ||
133 | chip->base = devm_request_and_ioremap(&pdev->dev, r); | 171 | chip->base = devm_request_and_ioremap(&pdev->dev, r); |
134 | if (chip->base == NULL) | 172 | if (!chip->base) |
135 | return -EADDRNOTAVAIL; | 173 | return -EADDRNOTAVAIL; |
136 | 174 | ||
175 | ret = clk_prepare(chip->clk); | ||
176 | if (ret < 0) { | ||
177 | dev_err(&pdev->dev, "failed to prepare clock\n"); | ||
178 | return ret; | ||
179 | } | ||
180 | |||
137 | ret = pwmchip_add(&chip->chip); | 181 | ret = pwmchip_add(&chip->chip); |
138 | if (ret < 0) | 182 | if (ret < 0) { |
183 | dev_err(&pdev->dev, "failed to add PWM chip\n"); | ||
139 | return ret; | 184 | return ret; |
185 | } | ||
140 | 186 | ||
141 | platform_set_drvdata(pdev, chip); | 187 | platform_set_drvdata(pdev, chip); |
142 | return ret; | 188 | return ret; |
143 | } | 189 | } |
144 | 190 | ||
145 | static int __devexit pwm_remove(struct platform_device *pdev) | 191 | static int vt8500_pwm_remove(struct platform_device *pdev) |
146 | { | 192 | { |
147 | struct vt8500_chip *chip; | 193 | struct vt8500_chip *chip; |
148 | 194 | ||
@@ -150,28 +196,22 @@ static int __devexit pwm_remove(struct platform_device *pdev) | |||
150 | if (chip == NULL) | 196 | if (chip == NULL) |
151 | return -ENODEV; | 197 | return -ENODEV; |
152 | 198 | ||
199 | clk_unprepare(chip->clk); | ||
200 | |||
153 | return pwmchip_remove(&chip->chip); | 201 | return pwmchip_remove(&chip->chip); |
154 | } | 202 | } |
155 | 203 | ||
156 | static struct platform_driver pwm_driver = { | 204 | static struct platform_driver vt8500_pwm_driver = { |
205 | .probe = vt8500_pwm_probe, | ||
206 | .remove = vt8500_pwm_remove, | ||
157 | .driver = { | 207 | .driver = { |
158 | .name = "vt8500-pwm", | 208 | .name = "vt8500-pwm", |
159 | .owner = THIS_MODULE, | 209 | .owner = THIS_MODULE, |
210 | .of_match_table = vt8500_pwm_dt_ids, | ||
160 | }, | 211 | }, |
161 | .probe = pwm_probe, | ||
162 | .remove = __devexit_p(pwm_remove), | ||
163 | }; | 212 | }; |
213 | module_platform_driver(vt8500_pwm_driver); | ||
164 | 214 | ||
165 | static int __init pwm_init(void) | 215 | MODULE_DESCRIPTION("VT8500 PWM Driver"); |
166 | { | 216 | MODULE_AUTHOR("Tony Prisk <linux@prisktech.co.nz>"); |
167 | return platform_driver_register(&pwm_driver); | 217 | MODULE_LICENSE("GPL v2"); |
168 | } | ||
169 | arch_initcall(pwm_init); | ||
170 | |||
171 | static void __exit pwm_exit(void) | ||
172 | { | ||
173 | platform_driver_unregister(&pwm_driver); | ||
174 | } | ||
175 | module_exit(pwm_exit); | ||
176 | |||
177 | MODULE_LICENSE("GPL"); | ||