diff options
Diffstat (limited to 'drivers/pwm/pwm-vt8500.c')
-rw-r--r-- | drivers/pwm/pwm-vt8500.c | 98 |
1 files changed, 74 insertions, 24 deletions
diff --git a/drivers/pwm/pwm-vt8500.c b/drivers/pwm/pwm-vt8500.c index ad14389b7144..b0ba2d403439 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) |
@@ -51,8 +62,15 @@ static int vt8500_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm, | |||
51 | struct vt8500_chip *vt8500 = to_vt8500_chip(chip); | 62 | struct vt8500_chip *vt8500 = to_vt8500_chip(chip); |
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; |
65 | int err; | ||
54 | 66 | ||
55 | c = 25000000/2; /* wild guess --- need to implement clocks */ | 67 | err = clk_enable(vt8500->clk); |
68 | if (err < 0) { | ||
69 | dev_err(chip->dev, "failed to enable clock\n"); | ||
70 | return err; | ||
71 | } | ||
72 | |||
73 | c = clk_get_rate(vt8500->clk); | ||
56 | c = c * period_ns; | 74 | c = c * period_ns; |
57 | do_div(c, 1000000000); | 75 | do_div(c, 1000000000); |
58 | period_cycles = c; | 76 | period_cycles = c; |
@@ -64,8 +82,10 @@ static int vt8500_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm, | |||
64 | if (pv > 4095) | 82 | if (pv > 4095) |
65 | pv = 4095; | 83 | pv = 4095; |
66 | 84 | ||
67 | if (prescale > 1023) | 85 | if (prescale > 1023) { |
86 | clk_disable(vt8500->clk); | ||
68 | return -EINVAL; | 87 | return -EINVAL; |
88 | } | ||
69 | 89 | ||
70 | c = (unsigned long long)pv * duty_ns; | 90 | c = (unsigned long long)pv * duty_ns; |
71 | do_div(c, period_ns); | 91 | do_div(c, period_ns); |
@@ -80,13 +100,21 @@ static int vt8500_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm, | |||
80 | pwm_busy_wait(vt8500->base + 0x40 + pwm->hwpwm, (1 << 3)); | 100 | pwm_busy_wait(vt8500->base + 0x40 + pwm->hwpwm, (1 << 3)); |
81 | writel(dc, vt8500->base + 0xc + (pwm->hwpwm << 4)); | 101 | writel(dc, vt8500->base + 0xc + (pwm->hwpwm << 4)); |
82 | 102 | ||
103 | clk_disable(vt8500->clk); | ||
83 | return 0; | 104 | return 0; |
84 | } | 105 | } |
85 | 106 | ||
86 | static int vt8500_pwm_enable(struct pwm_chip *chip, struct pwm_device *pwm) | 107 | static int vt8500_pwm_enable(struct pwm_chip *chip, struct pwm_device *pwm) |
87 | { | 108 | { |
109 | int err; | ||
88 | struct vt8500_chip *vt8500 = to_vt8500_chip(chip); | 110 | struct vt8500_chip *vt8500 = to_vt8500_chip(chip); |
89 | 111 | ||
112 | err = clk_enable(vt8500->clk); | ||
113 | if (err < 0) { | ||
114 | dev_err(chip->dev, "failed to enable clock\n"); | ||
115 | return err; | ||
116 | } | ||
117 | |||
90 | pwm_busy_wait(vt8500->base + 0x40 + pwm->hwpwm, (1 << 0)); | 118 | pwm_busy_wait(vt8500->base + 0x40 + pwm->hwpwm, (1 << 0)); |
91 | writel(5, vt8500->base + (pwm->hwpwm << 4)); | 119 | writel(5, vt8500->base + (pwm->hwpwm << 4)); |
92 | return 0; | 120 | return 0; |
@@ -98,6 +126,8 @@ static void vt8500_pwm_disable(struct pwm_chip *chip, struct pwm_device *pwm) | |||
98 | 126 | ||
99 | pwm_busy_wait(vt8500->base + 0x40 + pwm->hwpwm, (1 << 0)); | 127 | pwm_busy_wait(vt8500->base + 0x40 + pwm->hwpwm, (1 << 0)); |
100 | writel(0, vt8500->base + (pwm->hwpwm << 4)); | 128 | writel(0, vt8500->base + (pwm->hwpwm << 4)); |
129 | |||
130 | clk_disable(vt8500->clk); | ||
101 | } | 131 | } |
102 | 132 | ||
103 | static struct pwm_ops vt8500_pwm_ops = { | 133 | static struct pwm_ops vt8500_pwm_ops = { |
@@ -107,12 +137,24 @@ static struct pwm_ops vt8500_pwm_ops = { | |||
107 | .owner = THIS_MODULE, | 137 | .owner = THIS_MODULE, |
108 | }; | 138 | }; |
109 | 139 | ||
110 | static int __devinit pwm_probe(struct platform_device *pdev) | 140 | static const struct of_device_id vt8500_pwm_dt_ids[] = { |
141 | { .compatible = "via,vt8500-pwm", }, | ||
142 | { /* Sentinel */ } | ||
143 | }; | ||
144 | MODULE_DEVICE_TABLE(of, vt8500_pwm_dt_ids); | ||
145 | |||
146 | static int vt8500_pwm_probe(struct platform_device *pdev) | ||
111 | { | 147 | { |
112 | struct vt8500_chip *chip; | 148 | struct vt8500_chip *chip; |
113 | struct resource *r; | 149 | struct resource *r; |
150 | struct device_node *np = pdev->dev.of_node; | ||
114 | int ret; | 151 | int ret; |
115 | 152 | ||
153 | if (!np) { | ||
154 | dev_err(&pdev->dev, "invalid devicetree node\n"); | ||
155 | return -EINVAL; | ||
156 | } | ||
157 | |||
116 | chip = devm_kzalloc(&pdev->dev, sizeof(*chip), GFP_KERNEL); | 158 | chip = devm_kzalloc(&pdev->dev, sizeof(*chip), GFP_KERNEL); |
117 | if (chip == NULL) { | 159 | if (chip == NULL) { |
118 | dev_err(&pdev->dev, "failed to allocate memory\n"); | 160 | dev_err(&pdev->dev, "failed to allocate memory\n"); |
@@ -124,6 +166,12 @@ static int __devinit pwm_probe(struct platform_device *pdev) | |||
124 | chip->chip.base = -1; | 166 | chip->chip.base = -1; |
125 | chip->chip.npwm = VT8500_NR_PWMS; | 167 | chip->chip.npwm = VT8500_NR_PWMS; |
126 | 168 | ||
169 | chip->clk = devm_clk_get(&pdev->dev, NULL); | ||
170 | if (IS_ERR(chip->clk)) { | ||
171 | dev_err(&pdev->dev, "clock source not specified\n"); | ||
172 | return PTR_ERR(chip->clk); | ||
173 | } | ||
174 | |||
127 | r = platform_get_resource(pdev, IORESOURCE_MEM, 0); | 175 | r = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
128 | if (r == NULL) { | 176 | if (r == NULL) { |
129 | dev_err(&pdev->dev, "no memory resource defined\n"); | 177 | dev_err(&pdev->dev, "no memory resource defined\n"); |
@@ -131,18 +179,26 @@ static int __devinit pwm_probe(struct platform_device *pdev) | |||
131 | } | 179 | } |
132 | 180 | ||
133 | chip->base = devm_request_and_ioremap(&pdev->dev, r); | 181 | chip->base = devm_request_and_ioremap(&pdev->dev, r); |
134 | if (chip->base == NULL) | 182 | if (!chip->base) |
135 | return -EADDRNOTAVAIL; | 183 | return -EADDRNOTAVAIL; |
136 | 184 | ||
185 | ret = clk_prepare(chip->clk); | ||
186 | if (ret < 0) { | ||
187 | dev_err(&pdev->dev, "failed to prepare clock\n"); | ||
188 | return ret; | ||
189 | } | ||
190 | |||
137 | ret = pwmchip_add(&chip->chip); | 191 | ret = pwmchip_add(&chip->chip); |
138 | if (ret < 0) | 192 | if (ret < 0) { |
193 | dev_err(&pdev->dev, "failed to add PWM chip\n"); | ||
139 | return ret; | 194 | return ret; |
195 | } | ||
140 | 196 | ||
141 | platform_set_drvdata(pdev, chip); | 197 | platform_set_drvdata(pdev, chip); |
142 | return ret; | 198 | return ret; |
143 | } | 199 | } |
144 | 200 | ||
145 | static int __devexit pwm_remove(struct platform_device *pdev) | 201 | static int vt8500_pwm_remove(struct platform_device *pdev) |
146 | { | 202 | { |
147 | struct vt8500_chip *chip; | 203 | struct vt8500_chip *chip; |
148 | 204 | ||
@@ -150,28 +206,22 @@ static int __devexit pwm_remove(struct platform_device *pdev) | |||
150 | if (chip == NULL) | 206 | if (chip == NULL) |
151 | return -ENODEV; | 207 | return -ENODEV; |
152 | 208 | ||
209 | clk_unprepare(chip->clk); | ||
210 | |||
153 | return pwmchip_remove(&chip->chip); | 211 | return pwmchip_remove(&chip->chip); |
154 | } | 212 | } |
155 | 213 | ||
156 | static struct platform_driver pwm_driver = { | 214 | static struct platform_driver vt8500_pwm_driver = { |
215 | .probe = vt8500_pwm_probe, | ||
216 | .remove = vt8500_pwm_remove, | ||
157 | .driver = { | 217 | .driver = { |
158 | .name = "vt8500-pwm", | 218 | .name = "vt8500-pwm", |
159 | .owner = THIS_MODULE, | 219 | .owner = THIS_MODULE, |
220 | .of_match_table = vt8500_pwm_dt_ids, | ||
160 | }, | 221 | }, |
161 | .probe = pwm_probe, | ||
162 | .remove = __devexit_p(pwm_remove), | ||
163 | }; | 222 | }; |
223 | module_platform_driver(vt8500_pwm_driver); | ||
164 | 224 | ||
165 | static int __init pwm_init(void) | 225 | MODULE_DESCRIPTION("VT8500 PWM Driver"); |
166 | { | 226 | MODULE_AUTHOR("Tony Prisk <linux@prisktech.co.nz>"); |
167 | return platform_driver_register(&pwm_driver); | 227 | 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"); | ||