blob: 42e6f0873d1c9978e6d36c0e9d999c58ca21c7c7 [file] [log] [blame]
Alexandre Belloni09853ce2014-12-17 22:15:39 +01001/*
2 * Driver for Allwinner sun4i Pulse Width Modulation Controller
3 *
4 * Copyright (C) 2014 Alexandre Belloni <alexandre.belloni@free-electrons.com>
5 *
6 * Licensed under GPLv2.
7 */
8
9#include <linux/bitops.h>
10#include <linux/clk.h>
Alexandre Bellonic32c5c52017-05-30 21:32:08 +020011#include <linux/delay.h>
Alexandre Belloni09853ce2014-12-17 22:15:39 +010012#include <linux/err.h>
13#include <linux/io.h>
Alexandre Bellonic32c5c52017-05-30 21:32:08 +020014#include <linux/jiffies.h>
Alexandre Belloni09853ce2014-12-17 22:15:39 +010015#include <linux/module.h>
16#include <linux/of.h>
17#include <linux/of_device.h>
18#include <linux/platform_device.h>
19#include <linux/pwm.h>
20#include <linux/slab.h>
21#include <linux/spinlock.h>
22#include <linux/time.h>
23
24#define PWM_CTRL_REG 0x0
25
26#define PWM_CH_PRD_BASE 0x4
27#define PWM_CH_PRD_OFFSET 0x4
28#define PWM_CH_PRD(ch) (PWM_CH_PRD_BASE + PWM_CH_PRD_OFFSET * (ch))
29
30#define PWMCH_OFFSET 15
31#define PWM_PRESCAL_MASK GENMASK(3, 0)
32#define PWM_PRESCAL_OFF 0
33#define PWM_EN BIT(4)
34#define PWM_ACT_STATE BIT(5)
35#define PWM_CLK_GATING BIT(6)
36#define PWM_MODE BIT(7)
37#define PWM_PULSE BIT(8)
38#define PWM_BYPASS BIT(9)
39
40#define PWM_RDY_BASE 28
41#define PWM_RDY_OFFSET 1
42#define PWM_RDY(ch) BIT(PWM_RDY_BASE + PWM_RDY_OFFSET * (ch))
43
44#define PWM_PRD(prd) (((prd) - 1) << 16)
45#define PWM_PRD_MASK GENMASK(15, 0)
46
47#define PWM_DTY_MASK GENMASK(15, 0)
48
Alexandre Belloni93e0dfb2017-05-30 21:32:07 +020049#define PWM_REG_PRD(reg) ((((reg) >> 16) & PWM_PRD_MASK) + 1)
50#define PWM_REG_DTY(reg) ((reg) & PWM_DTY_MASK)
51#define PWM_REG_PRESCAL(reg, chan) (((reg) >> ((chan) * PWMCH_OFFSET)) & PWM_PRESCAL_MASK)
52
Alexandre Belloni09853ce2014-12-17 22:15:39 +010053#define BIT_CH(bit, chan) ((bit) << ((chan) * PWMCH_OFFSET))
54
55static const u32 prescaler_table[] = {
56 120,
57 180,
58 240,
59 360,
60 480,
61 0,
62 0,
63 0,
64 12000,
65 24000,
66 36000,
67 48000,
68 72000,
69 0,
70 0,
71 0, /* Actually 1 but tested separately */
72};
73
74struct sun4i_pwm_data {
75 bool has_prescaler_bypass;
76 bool has_rdy;
Hans de Goedef6649f72015-10-11 11:49:57 +020077 unsigned int npwm;
Alexandre Belloni09853ce2014-12-17 22:15:39 +010078};
79
80struct sun4i_pwm_chip {
81 struct pwm_chip chip;
82 struct clk *clk;
83 void __iomem *base;
84 spinlock_t ctrl_lock;
85 const struct sun4i_pwm_data *data;
Alexandre Bellonic32c5c52017-05-30 21:32:08 +020086 unsigned long next_period[2];
87 bool needs_delay[2];
Alexandre Belloni09853ce2014-12-17 22:15:39 +010088};
89
90static inline struct sun4i_pwm_chip *to_sun4i_pwm_chip(struct pwm_chip *chip)
91{
92 return container_of(chip, struct sun4i_pwm_chip, chip);
93}
94
95static inline u32 sun4i_pwm_readl(struct sun4i_pwm_chip *chip,
96 unsigned long offset)
97{
98 return readl(chip->base + offset);
99}
100
101static inline void sun4i_pwm_writel(struct sun4i_pwm_chip *chip,
102 u32 val, unsigned long offset)
103{
104 writel(val, chip->base + offset);
105}
106
Alexandre Belloni93e0dfb2017-05-30 21:32:07 +0200107static void sun4i_pwm_get_state(struct pwm_chip *chip,
108 struct pwm_device *pwm,
109 struct pwm_state *state)
110{
111 struct sun4i_pwm_chip *sun4i_pwm = to_sun4i_pwm_chip(chip);
112 u64 clk_rate, tmp;
113 u32 val;
114 unsigned int prescaler;
115
116 clk_rate = clk_get_rate(sun4i_pwm->clk);
117
118 val = sun4i_pwm_readl(sun4i_pwm, PWM_CTRL_REG);
119
Alexandre Belloni989ae7a2018-02-25 02:55:58 +0100120 if ((PWM_REG_PRESCAL(val, pwm->hwpwm) == PWM_PRESCAL_MASK) &&
121 sun4i_pwm->data->has_prescaler_bypass)
Alexandre Belloni93e0dfb2017-05-30 21:32:07 +0200122 prescaler = 1;
123 else
124 prescaler = prescaler_table[PWM_REG_PRESCAL(val, pwm->hwpwm)];
125
126 if (prescaler == 0)
127 return;
128
129 if (val & BIT_CH(PWM_ACT_STATE, pwm->hwpwm))
130 state->polarity = PWM_POLARITY_NORMAL;
131 else
132 state->polarity = PWM_POLARITY_INVERSED;
133
Alexandre Belloni989ae7a2018-02-25 02:55:58 +0100134 if ((val & BIT_CH(PWM_CLK_GATING | PWM_EN, pwm->hwpwm)) ==
135 BIT_CH(PWM_CLK_GATING | PWM_EN, pwm->hwpwm))
Alexandre Belloni93e0dfb2017-05-30 21:32:07 +0200136 state->enabled = true;
137 else
138 state->enabled = false;
139
140 val = sun4i_pwm_readl(sun4i_pwm, PWM_CH_PRD(pwm->hwpwm));
141
142 tmp = prescaler * NSEC_PER_SEC * PWM_REG_DTY(val);
143 state->duty_cycle = DIV_ROUND_CLOSEST_ULL(tmp, clk_rate);
144
145 tmp = prescaler * NSEC_PER_SEC * PWM_REG_PRD(val);
146 state->period = DIV_ROUND_CLOSEST_ULL(tmp, clk_rate);
147}
148
Alexandre Bellonic32c5c52017-05-30 21:32:08 +0200149static int sun4i_pwm_calculate(struct sun4i_pwm_chip *sun4i_pwm,
150 struct pwm_state *state,
151 u32 *dty, u32 *prd, unsigned int *prsclr)
152{
153 u64 clk_rate, div = 0;
154 unsigned int pval, prescaler = 0;
155
156 clk_rate = clk_get_rate(sun4i_pwm->clk);
157
158 if (sun4i_pwm->data->has_prescaler_bypass) {
159 /* First, test without any prescaler when available */
160 prescaler = PWM_PRESCAL_MASK;
161 pval = 1;
162 /*
163 * When not using any prescaler, the clock period in nanoseconds
164 * is not an integer so round it half up instead of
165 * truncating to get less surprising values.
166 */
167 div = clk_rate * state->period + NSEC_PER_SEC / 2;
168 do_div(div, NSEC_PER_SEC);
169 if (div - 1 > PWM_PRD_MASK)
170 prescaler = 0;
171 }
172
173 if (prescaler == 0) {
174 /* Go up from the first divider */
175 for (prescaler = 0; prescaler < PWM_PRESCAL_MASK; prescaler++) {
176 if (!prescaler_table[prescaler])
177 continue;
178 pval = prescaler_table[prescaler];
179 div = clk_rate;
180 do_div(div, pval);
181 div = div * state->period;
182 do_div(div, NSEC_PER_SEC);
183 if (div - 1 <= PWM_PRD_MASK)
184 break;
185 }
186
187 if (div - 1 > PWM_PRD_MASK)
188 return -EINVAL;
189 }
190
191 *prd = div;
192 div *= state->duty_cycle;
193 do_div(div, state->period);
194 *dty = div;
195 *prsclr = prescaler;
196
197 div = (u64)pval * NSEC_PER_SEC * *prd;
198 state->period = DIV_ROUND_CLOSEST_ULL(div, clk_rate);
199
200 div = (u64)pval * NSEC_PER_SEC * *dty;
201 state->duty_cycle = DIV_ROUND_CLOSEST_ULL(div, clk_rate);
202
203 return 0;
204}
205
206static int sun4i_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm,
207 struct pwm_state *state)
208{
209 struct sun4i_pwm_chip *sun4i_pwm = to_sun4i_pwm_chip(chip);
210 struct pwm_state cstate;
211 u32 ctrl;
212 int ret;
213 unsigned int delay_us;
214 unsigned long now;
215
216 pwm_get_state(pwm, &cstate);
217
218 if (!cstate.enabled) {
219 ret = clk_prepare_enable(sun4i_pwm->clk);
220 if (ret) {
221 dev_err(chip->dev, "failed to enable PWM clock\n");
222 return ret;
223 }
224 }
225
226 spin_lock(&sun4i_pwm->ctrl_lock);
227 ctrl = sun4i_pwm_readl(sun4i_pwm, PWM_CTRL_REG);
228
229 if ((cstate.period != state->period) ||
230 (cstate.duty_cycle != state->duty_cycle)) {
231 u32 period, duty, val;
232 unsigned int prescaler;
233
234 ret = sun4i_pwm_calculate(sun4i_pwm, state,
235 &duty, &period, &prescaler);
236 if (ret) {
237 dev_err(chip->dev, "period exceeds the maximum value\n");
238 spin_unlock(&sun4i_pwm->ctrl_lock);
239 if (!cstate.enabled)
240 clk_disable_unprepare(sun4i_pwm->clk);
241 return ret;
242 }
243
244 if (PWM_REG_PRESCAL(ctrl, pwm->hwpwm) != prescaler) {
245 /* Prescaler changed, the clock has to be gated */
246 ctrl &= ~BIT_CH(PWM_CLK_GATING, pwm->hwpwm);
247 sun4i_pwm_writel(sun4i_pwm, ctrl, PWM_CTRL_REG);
248
249 ctrl &= ~BIT_CH(PWM_PRESCAL_MASK, pwm->hwpwm);
250 ctrl |= BIT_CH(prescaler, pwm->hwpwm);
251 }
252
253 val = (duty & PWM_DTY_MASK) | PWM_PRD(period);
254 sun4i_pwm_writel(sun4i_pwm, val, PWM_CH_PRD(pwm->hwpwm));
255 sun4i_pwm->next_period[pwm->hwpwm] = jiffies +
256 usecs_to_jiffies(cstate.period / 1000 + 1);
257 sun4i_pwm->needs_delay[pwm->hwpwm] = true;
258 }
259
260 if (state->polarity != PWM_POLARITY_NORMAL)
261 ctrl &= ~BIT_CH(PWM_ACT_STATE, pwm->hwpwm);
262 else
263 ctrl |= BIT_CH(PWM_ACT_STATE, pwm->hwpwm);
264
265 ctrl |= BIT_CH(PWM_CLK_GATING, pwm->hwpwm);
266 if (state->enabled) {
267 ctrl |= BIT_CH(PWM_EN, pwm->hwpwm);
268 } else if (!sun4i_pwm->needs_delay[pwm->hwpwm]) {
269 ctrl &= ~BIT_CH(PWM_EN, pwm->hwpwm);
270 ctrl &= ~BIT_CH(PWM_CLK_GATING, pwm->hwpwm);
271 }
272
273 sun4i_pwm_writel(sun4i_pwm, ctrl, PWM_CTRL_REG);
274
275 spin_unlock(&sun4i_pwm->ctrl_lock);
276
277 if (state->enabled)
278 return 0;
279
280 if (!sun4i_pwm->needs_delay[pwm->hwpwm]) {
281 clk_disable_unprepare(sun4i_pwm->clk);
282 return 0;
283 }
284
285 /* We need a full period to elapse before disabling the channel. */
286 now = jiffies;
287 if (sun4i_pwm->needs_delay[pwm->hwpwm] &&
288 time_before(now, sun4i_pwm->next_period[pwm->hwpwm])) {
289 delay_us = jiffies_to_usecs(sun4i_pwm->next_period[pwm->hwpwm] -
290 now);
291 if ((delay_us / 500) > MAX_UDELAY_MS)
292 msleep(delay_us / 1000 + 1);
293 else
294 usleep_range(delay_us, delay_us * 2);
295 }
296 sun4i_pwm->needs_delay[pwm->hwpwm] = false;
297
298 spin_lock(&sun4i_pwm->ctrl_lock);
299 ctrl = sun4i_pwm_readl(sun4i_pwm, PWM_CTRL_REG);
300 ctrl &= ~BIT_CH(PWM_CLK_GATING, pwm->hwpwm);
301 ctrl &= ~BIT_CH(PWM_EN, pwm->hwpwm);
302 sun4i_pwm_writel(sun4i_pwm, ctrl, PWM_CTRL_REG);
303 spin_unlock(&sun4i_pwm->ctrl_lock);
304
305 clk_disable_unprepare(sun4i_pwm->clk);
306
307 return 0;
308}
309
Alexandre Belloni09853ce2014-12-17 22:15:39 +0100310static const struct pwm_ops sun4i_pwm_ops = {
Alexandre Bellonic32c5c52017-05-30 21:32:08 +0200311 .apply = sun4i_pwm_apply,
Alexandre Belloni93e0dfb2017-05-30 21:32:07 +0200312 .get_state = sun4i_pwm_get_state,
Alexandre Belloni09853ce2014-12-17 22:15:39 +0100313 .owner = THIS_MODULE,
314};
315
316static const struct sun4i_pwm_data sun4i_pwm_data_a10 = {
317 .has_prescaler_bypass = false,
318 .has_rdy = false,
Hans de Goedef6649f72015-10-11 11:49:57 +0200319 .npwm = 2,
320};
321
322static const struct sun4i_pwm_data sun4i_pwm_data_a10s = {
323 .has_prescaler_bypass = true,
324 .has_rdy = true,
325 .npwm = 2,
326};
327
328static const struct sun4i_pwm_data sun4i_pwm_data_a13 = {
329 .has_prescaler_bypass = true,
330 .has_rdy = true,
331 .npwm = 1,
Alexandre Belloni09853ce2014-12-17 22:15:39 +0100332};
333
334static const struct sun4i_pwm_data sun4i_pwm_data_a20 = {
335 .has_prescaler_bypass = true,
336 .has_rdy = true,
Hans de Goedef6649f72015-10-11 11:49:57 +0200337 .npwm = 2,
Alexandre Belloni09853ce2014-12-17 22:15:39 +0100338};
339
Milo Kim42ddcf42016-08-31 17:25:20 +0900340static const struct sun4i_pwm_data sun4i_pwm_data_h3 = {
341 .has_prescaler_bypass = true,
342 .has_rdy = true,
343 .npwm = 1,
344};
345
Alexandre Belloni09853ce2014-12-17 22:15:39 +0100346static const struct of_device_id sun4i_pwm_dt_ids[] = {
347 {
348 .compatible = "allwinner,sun4i-a10-pwm",
349 .data = &sun4i_pwm_data_a10,
350 }, {
Hans de Goedef6649f72015-10-11 11:49:57 +0200351 .compatible = "allwinner,sun5i-a10s-pwm",
352 .data = &sun4i_pwm_data_a10s,
353 }, {
354 .compatible = "allwinner,sun5i-a13-pwm",
355 .data = &sun4i_pwm_data_a13,
356 }, {
Alexandre Belloni09853ce2014-12-17 22:15:39 +0100357 .compatible = "allwinner,sun7i-a20-pwm",
358 .data = &sun4i_pwm_data_a20,
359 }, {
Milo Kim42ddcf42016-08-31 17:25:20 +0900360 .compatible = "allwinner,sun8i-h3-pwm",
361 .data = &sun4i_pwm_data_h3,
362 }, {
Alexandre Belloni09853ce2014-12-17 22:15:39 +0100363 /* sentinel */
364 },
365};
366MODULE_DEVICE_TABLE(of, sun4i_pwm_dt_ids);
367
368static int sun4i_pwm_probe(struct platform_device *pdev)
369{
370 struct sun4i_pwm_chip *pwm;
371 struct resource *res;
Alexandre Belloni93e0dfb2017-05-30 21:32:07 +0200372 int ret;
Alexandre Belloni09853ce2014-12-17 22:15:39 +0100373
374 pwm = devm_kzalloc(&pdev->dev, sizeof(*pwm), GFP_KERNEL);
375 if (!pwm)
376 return -ENOMEM;
377
Corentin Labbedf4f6e82017-10-21 19:38:12 +0200378 pwm->data = of_device_get_match_data(&pdev->dev);
379 if (!pwm->data)
380 return -ENODEV;
381
Alexandre Belloni09853ce2014-12-17 22:15:39 +0100382 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
383 pwm->base = devm_ioremap_resource(&pdev->dev, res);
384 if (IS_ERR(pwm->base))
385 return PTR_ERR(pwm->base);
386
387 pwm->clk = devm_clk_get(&pdev->dev, NULL);
388 if (IS_ERR(pwm->clk))
389 return PTR_ERR(pwm->clk);
390
391 pwm->chip.dev = &pdev->dev;
392 pwm->chip.ops = &sun4i_pwm_ops;
393 pwm->chip.base = -1;
Hans de Goedef6649f72015-10-11 11:49:57 +0200394 pwm->chip.npwm = pwm->data->npwm;
Alexandre Belloni09853ce2014-12-17 22:15:39 +0100395 pwm->chip.of_xlate = of_pwm_xlate_with_flags;
396 pwm->chip.of_pwm_n_cells = 3;
Alexandre Belloni09853ce2014-12-17 22:15:39 +0100397
398 spin_lock_init(&pwm->ctrl_lock);
399
400 ret = pwmchip_add(&pwm->chip);
401 if (ret < 0) {
402 dev_err(&pdev->dev, "failed to add PWM chip: %d\n", ret);
403 return ret;
404 }
405
406 platform_set_drvdata(pdev, pwm);
407
Alexandre Belloni09853ce2014-12-17 22:15:39 +0100408 return 0;
Alexandre Belloni09853ce2014-12-17 22:15:39 +0100409}
410
411static int sun4i_pwm_remove(struct platform_device *pdev)
412{
413 struct sun4i_pwm_chip *pwm = platform_get_drvdata(pdev);
414
415 return pwmchip_remove(&pwm->chip);
416}
417
418static struct platform_driver sun4i_pwm_driver = {
419 .driver = {
420 .name = "sun4i-pwm",
421 .of_match_table = sun4i_pwm_dt_ids,
422 },
423 .probe = sun4i_pwm_probe,
424 .remove = sun4i_pwm_remove,
425};
426module_platform_driver(sun4i_pwm_driver);
427
428MODULE_ALIAS("platform:sun4i-pwm");
429MODULE_AUTHOR("Alexandre Belloni <alexandre.belloni@free-electrons.com>");
430MODULE_DESCRIPTION("Allwinner sun4i PWM driver");
431MODULE_LICENSE("GPL v2");