]>
Commit | Line | Data |
---|---|---|
6192fa87 | 1 | /* drivers/pwm/pwm-samsung.c |
6fc601e3 BD |
2 | * |
3 | * Copyright (c) 2007 Ben Dooks | |
4 | * Copyright (c) 2008 Simtec Electronics | |
5 | * Ben Dooks <ben@simtec.co.uk>, <ben-linux@fluff.org> | |
6 | * | |
b5ead1cd | 7 | * S3C series PWM device core |
6fc601e3 BD |
8 | * |
9 | * This program is free software; you can redistribute it and/or modify | |
10 | * it under the terms of the GNU General Public License as published by | |
11 | * the Free Software Foundation; either version 2 of the License. | |
12 | */ | |
13 | ||
2437b0d9 SK |
14 | #define pr_fmt(fmt) "pwm-samsung: " fmt |
15 | ||
a69e4c28 | 16 | #include <linux/export.h> |
6fc601e3 BD |
17 | #include <linux/kernel.h> |
18 | #include <linux/platform_device.h> | |
5a0e3ad6 | 19 | #include <linux/slab.h> |
6fc601e3 BD |
20 | #include <linux/err.h> |
21 | #include <linux/clk.h> | |
22 | #include <linux/io.h> | |
23 | #include <linux/pwm.h> | |
24 | ||
b5ead1cd | 25 | #include <mach/map.h> |
80b02c17 | 26 | |
a2b7ba9c | 27 | #include <plat/regs-timer.h> |
6fc601e3 | 28 | |
215c29d3 | 29 | struct s3c_chip { |
6fc601e3 BD |
30 | struct platform_device *pdev; |
31 | ||
32 | struct clk *clk_div; | |
33 | struct clk *clk; | |
34 | const char *label; | |
35 | ||
36 | unsigned int period_ns; | |
37 | unsigned int duty_ns; | |
38 | ||
39 | unsigned char tcon_base; | |
6fc601e3 | 40 | unsigned char pwm_id; |
215c29d3 | 41 | struct pwm_chip chip; |
6fc601e3 BD |
42 | }; |
43 | ||
215c29d3 SH |
44 | #define to_s3c_chip(chip) container_of(chip, struct s3c_chip, chip) |
45 | ||
66592eee | 46 | #define pwm_dbg(_pwm, msg...) dev_dbg(&(_pwm)->pdev->dev, msg) |
6fc601e3 BD |
47 | |
48 | static struct clk *clk_scaler[2]; | |
49 | ||
215c29d3 | 50 | static inline int pwm_is_tdiv(struct s3c_chip *chip) |
6fc601e3 | 51 | { |
215c29d3 | 52 | return clk_get_parent(chip->clk) == chip->clk_div; |
6fc601e3 BD |
53 | } |
54 | ||
6fc601e3 BD |
55 | #define pwm_tcon_start(pwm) (1 << (pwm->tcon_base + 0)) |
56 | #define pwm_tcon_invert(pwm) (1 << (pwm->tcon_base + 2)) | |
57 | #define pwm_tcon_autoreload(pwm) (1 << (pwm->tcon_base + 3)) | |
58 | #define pwm_tcon_manulupdate(pwm) (1 << (pwm->tcon_base + 1)) | |
59 | ||
215c29d3 | 60 | static int s3c_pwm_enable(struct pwm_chip *chip, struct pwm_device *pwm) |
6fc601e3 | 61 | { |
215c29d3 | 62 | struct s3c_chip *s3c = to_s3c_chip(chip); |
6fc601e3 BD |
63 | unsigned long flags; |
64 | unsigned long tcon; | |
65 | ||
66 | local_irq_save(flags); | |
67 | ||
68 | tcon = __raw_readl(S3C2410_TCON); | |
215c29d3 | 69 | tcon |= pwm_tcon_start(s3c); |
6fc601e3 BD |
70 | __raw_writel(tcon, S3C2410_TCON); |
71 | ||
72 | local_irq_restore(flags); | |
73 | ||
6fc601e3 BD |
74 | return 0; |
75 | } | |
76 | ||
215c29d3 | 77 | static void s3c_pwm_disable(struct pwm_chip *chip, struct pwm_device *pwm) |
6fc601e3 | 78 | { |
215c29d3 | 79 | struct s3c_chip *s3c = to_s3c_chip(chip); |
6fc601e3 BD |
80 | unsigned long flags; |
81 | unsigned long tcon; | |
82 | ||
83 | local_irq_save(flags); | |
84 | ||
85 | tcon = __raw_readl(S3C2410_TCON); | |
215c29d3 | 86 | tcon &= ~pwm_tcon_start(s3c); |
6fc601e3 BD |
87 | __raw_writel(tcon, S3C2410_TCON); |
88 | ||
89 | local_irq_restore(flags); | |
6fc601e3 BD |
90 | } |
91 | ||
215c29d3 | 92 | static unsigned long pwm_calc_tin(struct s3c_chip *s3c, unsigned long freq) |
6fc601e3 BD |
93 | { |
94 | unsigned long tin_parent_rate; | |
95 | unsigned int div; | |
96 | ||
215c29d3 SH |
97 | tin_parent_rate = clk_get_rate(clk_get_parent(s3c->clk_div)); |
98 | pwm_dbg(s3c, "tin parent at %lu\n", tin_parent_rate); | |
6fc601e3 BD |
99 | |
100 | for (div = 2; div <= 16; div *= 2) { | |
101 | if ((tin_parent_rate / (div << 16)) < freq) | |
102 | return tin_parent_rate / div; | |
103 | } | |
104 | ||
105 | return tin_parent_rate / 16; | |
106 | } | |
107 | ||
108 | #define NS_IN_HZ (1000000000UL) | |
109 | ||
215c29d3 SH |
110 | static int s3c_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm, |
111 | int duty_ns, int period_ns) | |
6fc601e3 | 112 | { |
215c29d3 | 113 | struct s3c_chip *s3c = to_s3c_chip(chip); |
6fc601e3 BD |
114 | unsigned long tin_rate; |
115 | unsigned long tin_ns; | |
116 | unsigned long period; | |
117 | unsigned long flags; | |
118 | unsigned long tcon; | |
119 | unsigned long tcnt; | |
120 | long tcmp; | |
121 | ||
122 | /* We currently avoid using 64bit arithmetic by using the | |
123 | * fact that anything faster than 1Hz is easily representable | |
124 | * by 32bits. */ | |
125 | ||
126 | if (period_ns > NS_IN_HZ || duty_ns > NS_IN_HZ) | |
127 | return -ERANGE; | |
128 | ||
215c29d3 SH |
129 | if (period_ns == s3c->period_ns && |
130 | duty_ns == s3c->duty_ns) | |
6fc601e3 BD |
131 | return 0; |
132 | ||
133 | /* The TCMP and TCNT can be read without a lock, they're not | |
134 | * shared between the timers. */ | |
135 | ||
215c29d3 SH |
136 | tcmp = __raw_readl(S3C2410_TCMPB(s3c->pwm_id)); |
137 | tcnt = __raw_readl(S3C2410_TCNTB(s3c->pwm_id)); | |
6fc601e3 BD |
138 | |
139 | period = NS_IN_HZ / period_ns; | |
140 | ||
215c29d3 | 141 | pwm_dbg(s3c, "duty_ns=%d, period_ns=%d (%lu)\n", |
6fc601e3 BD |
142 | duty_ns, period_ns, period); |
143 | ||
144 | /* Check to see if we are changing the clock rate of the PWM */ | |
145 | ||
215c29d3 SH |
146 | if (s3c->period_ns != period_ns) { |
147 | if (pwm_is_tdiv(s3c)) { | |
148 | tin_rate = pwm_calc_tin(s3c, period); | |
149 | clk_set_rate(s3c->clk_div, tin_rate); | |
6fc601e3 | 150 | } else |
215c29d3 | 151 | tin_rate = clk_get_rate(s3c->clk); |
6fc601e3 | 152 | |
215c29d3 | 153 | s3c->period_ns = period_ns; |
6fc601e3 | 154 | |
215c29d3 | 155 | pwm_dbg(s3c, "tin_rate=%lu\n", tin_rate); |
6fc601e3 BD |
156 | |
157 | tin_ns = NS_IN_HZ / tin_rate; | |
158 | tcnt = period_ns / tin_ns; | |
159 | } else | |
215c29d3 | 160 | tin_ns = NS_IN_HZ / clk_get_rate(s3c->clk); |
6fc601e3 BD |
161 | |
162 | /* Note, counters count down */ | |
163 | ||
164 | tcmp = duty_ns / tin_ns; | |
165 | tcmp = tcnt - tcmp; | |
165f5f64 PK |
166 | /* the pwm hw only checks the compare register after a decrement, |
167 | so the pin never toggles if tcmp = tcnt */ | |
168 | if (tcmp == tcnt) | |
169 | tcmp--; | |
6fc601e3 | 170 | |
215c29d3 | 171 | pwm_dbg(s3c, "tin_ns=%lu, tcmp=%ld/%lu\n", tin_ns, tcmp, tcnt); |
6fc601e3 BD |
172 | |
173 | if (tcmp < 0) | |
174 | tcmp = 0; | |
175 | ||
176 | /* Update the PWM register block. */ | |
177 | ||
178 | local_irq_save(flags); | |
179 | ||
215c29d3 SH |
180 | __raw_writel(tcmp, S3C2410_TCMPB(s3c->pwm_id)); |
181 | __raw_writel(tcnt, S3C2410_TCNTB(s3c->pwm_id)); | |
6fc601e3 BD |
182 | |
183 | tcon = __raw_readl(S3C2410_TCON); | |
215c29d3 SH |
184 | tcon |= pwm_tcon_manulupdate(s3c); |
185 | tcon |= pwm_tcon_autoreload(s3c); | |
6fc601e3 BD |
186 | __raw_writel(tcon, S3C2410_TCON); |
187 | ||
215c29d3 | 188 | tcon &= ~pwm_tcon_manulupdate(s3c); |
6fc601e3 BD |
189 | __raw_writel(tcon, S3C2410_TCON); |
190 | ||
191 | local_irq_restore(flags); | |
192 | ||
193 | return 0; | |
194 | } | |
195 | ||
215c29d3 SH |
196 | static struct pwm_ops s3c_pwm_ops = { |
197 | .enable = s3c_pwm_enable, | |
198 | .disable = s3c_pwm_disable, | |
199 | .config = s3c_pwm_config, | |
200 | .owner = THIS_MODULE, | |
201 | }; | |
6fc601e3 BD |
202 | |
203 | static int s3c_pwm_probe(struct platform_device *pdev) | |
204 | { | |
205 | struct device *dev = &pdev->dev; | |
215c29d3 | 206 | struct s3c_chip *s3c; |
6fc601e3 BD |
207 | unsigned long flags; |
208 | unsigned long tcon; | |
209 | unsigned int id = pdev->id; | |
210 | int ret; | |
211 | ||
212 | if (id == 4) { | |
213 | dev_err(dev, "TIMER4 is currently not supported\n"); | |
214 | return -ENXIO; | |
215 | } | |
216 | ||
6192fa87 | 217 | s3c = devm_kzalloc(&pdev->dev, sizeof(*s3c), GFP_KERNEL); |
215c29d3 | 218 | if (s3c == NULL) { |
6fc601e3 BD |
219 | dev_err(dev, "failed to allocate pwm_device\n"); |
220 | return -ENOMEM; | |
221 | } | |
222 | ||
6fc601e3 | 223 | /* calculate base of control bits in TCON */ |
215c29d3 | 224 | s3c->tcon_base = id == 0 ? 0 : (id * 4) + 4; |
ecefeb79 | 225 | s3c->chip.dev = &pdev->dev; |
215c29d3 SH |
226 | s3c->chip.ops = &s3c_pwm_ops; |
227 | s3c->chip.base = -1; | |
228 | s3c->chip.npwm = 1; | |
6fc601e3 | 229 | |
6192fa87 | 230 | s3c->clk = devm_clk_get(dev, "pwm-tin"); |
215c29d3 | 231 | if (IS_ERR(s3c->clk)) { |
6fc601e3 | 232 | dev_err(dev, "failed to get pwm tin clk\n"); |
6192fa87 | 233 | return PTR_ERR(s3c->clk); |
6fc601e3 BD |
234 | } |
235 | ||
6192fa87 | 236 | s3c->clk_div = devm_clk_get(dev, "pwm-tdiv"); |
215c29d3 | 237 | if (IS_ERR(s3c->clk_div)) { |
6fc601e3 | 238 | dev_err(dev, "failed to get pwm tdiv clk\n"); |
6192fa87 | 239 | return PTR_ERR(s3c->clk_div); |
6fc601e3 BD |
240 | } |
241 | ||
215c29d3 SH |
242 | clk_enable(s3c->clk); |
243 | clk_enable(s3c->clk_div); | |
d8633c1d | 244 | |
6fc601e3 BD |
245 | local_irq_save(flags); |
246 | ||
247 | tcon = __raw_readl(S3C2410_TCON); | |
215c29d3 | 248 | tcon |= pwm_tcon_invert(s3c); |
6fc601e3 BD |
249 | __raw_writel(tcon, S3C2410_TCON); |
250 | ||
251 | local_irq_restore(flags); | |
252 | ||
215c29d3 SH |
253 | ret = pwmchip_add(&s3c->chip); |
254 | if (ret < 0) { | |
6fc601e3 BD |
255 | dev_err(dev, "failed to register pwm\n"); |
256 | goto err_clk_tdiv; | |
257 | } | |
258 | ||
215c29d3 SH |
259 | pwm_dbg(s3c, "config bits %02x\n", |
260 | (__raw_readl(S3C2410_TCON) >> s3c->tcon_base) & 0x0f); | |
6fc601e3 BD |
261 | |
262 | dev_info(dev, "tin at %lu, tdiv at %lu, tin=%sclk, base %d\n", | |
215c29d3 SH |
263 | clk_get_rate(s3c->clk), |
264 | clk_get_rate(s3c->clk_div), | |
265 | pwm_is_tdiv(s3c) ? "div" : "ext", s3c->tcon_base); | |
6fc601e3 | 266 | |
215c29d3 | 267 | platform_set_drvdata(pdev, s3c); |
6fc601e3 BD |
268 | return 0; |
269 | ||
270 | err_clk_tdiv: | |
215c29d3 SH |
271 | clk_disable(s3c->clk_div); |
272 | clk_disable(s3c->clk); | |
6fc601e3 BD |
273 | return ret; |
274 | } | |
275 | ||
77f37917 | 276 | static int s3c_pwm_remove(struct platform_device *pdev) |
6fc601e3 | 277 | { |
215c29d3 SH |
278 | struct s3c_chip *s3c = platform_get_drvdata(pdev); |
279 | int err; | |
280 | ||
281 | err = pwmchip_remove(&s3c->chip); | |
282 | if (err < 0) | |
283 | return err; | |
6fc601e3 | 284 | |
215c29d3 SH |
285 | clk_disable(s3c->clk_div); |
286 | clk_disable(s3c->clk); | |
6fc601e3 BD |
287 | |
288 | return 0; | |
289 | } | |
290 | ||
e79032aa VK |
291 | #ifdef CONFIG_PM |
292 | static int s3c_pwm_suspend(struct platform_device *pdev, pm_message_t state) | |
293 | { | |
215c29d3 | 294 | struct s3c_chip *s3c = platform_get_drvdata(pdev); |
e79032aa VK |
295 | |
296 | /* No one preserve these values during suspend so reset them | |
297 | * Otherwise driver leaves PWM unconfigured if same values | |
298 | * passed to pwm_config | |
299 | */ | |
215c29d3 SH |
300 | s3c->period_ns = 0; |
301 | s3c->duty_ns = 0; | |
e79032aa VK |
302 | |
303 | return 0; | |
304 | } | |
305 | ||
306 | static int s3c_pwm_resume(struct platform_device *pdev) | |
307 | { | |
215c29d3 | 308 | struct s3c_chip *s3c = platform_get_drvdata(pdev); |
e79032aa VK |
309 | unsigned long tcon; |
310 | ||
311 | /* Restore invertion */ | |
312 | tcon = __raw_readl(S3C2410_TCON); | |
215c29d3 | 313 | tcon |= pwm_tcon_invert(s3c); |
e79032aa VK |
314 | __raw_writel(tcon, S3C2410_TCON); |
315 | ||
316 | return 0; | |
317 | } | |
318 | ||
319 | #else | |
320 | #define s3c_pwm_suspend NULL | |
321 | #define s3c_pwm_resume NULL | |
322 | #endif | |
323 | ||
6fc601e3 BD |
324 | static struct platform_driver s3c_pwm_driver = { |
325 | .driver = { | |
326 | .name = "s3c24xx-pwm", | |
327 | .owner = THIS_MODULE, | |
328 | }, | |
329 | .probe = s3c_pwm_probe, | |
fd109112 | 330 | .remove = s3c_pwm_remove, |
e79032aa VK |
331 | .suspend = s3c_pwm_suspend, |
332 | .resume = s3c_pwm_resume, | |
6fc601e3 BD |
333 | }; |
334 | ||
335 | static int __init pwm_init(void) | |
336 | { | |
337 | int ret; | |
338 | ||
339 | clk_scaler[0] = clk_get(NULL, "pwm-scaler0"); | |
340 | clk_scaler[1] = clk_get(NULL, "pwm-scaler1"); | |
341 | ||
342 | if (IS_ERR(clk_scaler[0]) || IS_ERR(clk_scaler[1])) { | |
2437b0d9 | 343 | pr_err("failed to get scaler clocks\n"); |
6fc601e3 BD |
344 | return -EINVAL; |
345 | } | |
346 | ||
347 | ret = platform_driver_register(&s3c_pwm_driver); | |
348 | if (ret) | |
2437b0d9 | 349 | pr_err("failed to add pwm driver\n"); |
6fc601e3 BD |
350 | |
351 | return ret; | |
352 | } | |
353 | ||
354 | arch_initcall(pwm_init); |