blob: 4ac899d8008cf50d99b597c17c3c270db3541cd5 [file] [log] [blame]
Bo Shen32b16d42013-12-13 14:41:49 +08001/*
2 * Driver for Atmel Pulse Width Modulation Controller
3 *
4 * Copyright (C) 2013 Atmel Corporation
5 * Bo Shen <voice.shen@atmel.com>
6 *
7 * Licensed under GPLv2.
8 */
9
10#include <linux/clk.h>
Alexandre Belloni472ac3d2015-05-25 18:11:49 +020011#include <linux/delay.h>
Bo Shen32b16d42013-12-13 14:41:49 +080012#include <linux/err.h>
13#include <linux/io.h>
14#include <linux/module.h>
Alexandre Belloni472ac3d2015-05-25 18:11:49 +020015#include <linux/mutex.h>
Bo Shen32b16d42013-12-13 14:41:49 +080016#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
22/* The following is global registers for PWM controller */
23#define PWM_ENA 0x04
24#define PWM_DIS 0x08
25#define PWM_SR 0x0C
Alexandre Belloni472ac3d2015-05-25 18:11:49 +020026#define PWM_ISR 0x1C
Bo Shen32b16d42013-12-13 14:41:49 +080027/* Bit field in SR */
28#define PWM_SR_ALL_CH_ON 0x0F
29
30/* The following register is PWM channel related registers */
31#define PWM_CH_REG_OFFSET 0x200
32#define PWM_CH_REG_SIZE 0x20
33
34#define PWM_CMR 0x0
35/* Bit field in CMR */
36#define PWM_CMR_CPOL (1 << 9)
37#define PWM_CMR_UPD_CDTY (1 << 10)
Alexandre Belloni8db9e292014-03-14 15:19:08 +010038#define PWM_CMR_CPRE_MSK 0xF
Bo Shen32b16d42013-12-13 14:41:49 +080039
40/* The following registers for PWM v1 */
41#define PWMV1_CDTY 0x04
42#define PWMV1_CPRD 0x08
43#define PWMV1_CUPD 0x10
44
45/* The following registers for PWM v2 */
46#define PWMV2_CDTY 0x04
47#define PWMV2_CDTYUPD 0x08
48#define PWMV2_CPRD 0x0C
49#define PWMV2_CPRDUPD 0x10
50
Claudiu Beznea0285827d2019-02-25 16:44:37 +000051/* Max values for period and prescaler */
52
53/* Only the LSB 16 bits are significant. */
54#define PWM_MAXV1_PRD 0xFFFF
55#define PRD_MAXV1_PRES 10
Bo Shen32b16d42013-12-13 14:41:49 +080056
Claudiu Beznea1a722aa2017-03-22 15:29:34 +020057struct atmel_pwm_registers {
58 u8 period;
59 u8 period_upd;
60 u8 duty;
61 u8 duty_upd;
62};
63
Claudiu Beznea0285827d2019-02-25 16:44:37 +000064struct atmel_pwm_config {
65 u32 max_period;
66 u32 max_pres;
67};
68
Claudiu Beznea53784152019-02-25 16:44:33 +000069struct atmel_pwm_data {
70 struct atmel_pwm_registers regs;
Claudiu Beznea0285827d2019-02-25 16:44:37 +000071 struct atmel_pwm_config cfg;
Claudiu Beznea53784152019-02-25 16:44:33 +000072};
73
Bo Shen32b16d42013-12-13 14:41:49 +080074struct atmel_pwm_chip {
75 struct pwm_chip chip;
76 struct clk *clk;
77 void __iomem *base;
Claudiu Beznea53784152019-02-25 16:44:33 +000078 const struct atmel_pwm_data *data;
Bo Shen32b16d42013-12-13 14:41:49 +080079
Alexandre Belloni472ac3d2015-05-25 18:11:49 +020080 unsigned int updated_pwms;
Thierry Reding313b78e2016-07-11 12:14:34 +020081 /* ISR is cleared when read, ensure only one thread does that */
82 struct mutex isr_lock;
Bo Shen32b16d42013-12-13 14:41:49 +080083};
84
85static inline struct atmel_pwm_chip *to_atmel_pwm_chip(struct pwm_chip *chip)
86{
87 return container_of(chip, struct atmel_pwm_chip, chip);
88}
89
90static inline u32 atmel_pwm_readl(struct atmel_pwm_chip *chip,
91 unsigned long offset)
92{
93 return readl_relaxed(chip->base + offset);
94}
95
96static inline void atmel_pwm_writel(struct atmel_pwm_chip *chip,
97 unsigned long offset, unsigned long val)
98{
99 writel_relaxed(val, chip->base + offset);
100}
101
102static inline u32 atmel_pwm_ch_readl(struct atmel_pwm_chip *chip,
103 unsigned int ch, unsigned long offset)
104{
105 unsigned long base = PWM_CH_REG_OFFSET + ch * PWM_CH_REG_SIZE;
106
107 return readl_relaxed(chip->base + base + offset);
108}
109
110static inline void atmel_pwm_ch_writel(struct atmel_pwm_chip *chip,
111 unsigned int ch, unsigned long offset,
112 unsigned long val)
113{
114 unsigned long base = PWM_CH_REG_OFFSET + ch * PWM_CH_REG_SIZE;
115
116 writel_relaxed(val, chip->base + base + offset);
117}
118
Claudiu Beznea1a722aa2017-03-22 15:29:34 +0200119static int atmel_pwm_calculate_cprd_and_pres(struct pwm_chip *chip,
120 const struct pwm_state *state,
121 unsigned long *cprd, u32 *pres)
Bo Shen32b16d42013-12-13 14:41:49 +0800122{
123 struct atmel_pwm_chip *atmel_pwm = to_atmel_pwm_chip(chip);
Claudiu Beznea1a722aa2017-03-22 15:29:34 +0200124 unsigned long long cycles = state->period;
Bo Shen32b16d42013-12-13 14:41:49 +0800125
Nikolaus Vosse2e08972014-09-23 15:30:21 +0200126 /* Calculate the period cycles and prescale value */
Claudiu Beznea1a722aa2017-03-22 15:29:34 +0200127 cycles *= clk_get_rate(atmel_pwm->clk);
128 do_div(cycles, NSEC_PER_SEC);
Bo Shen32b16d42013-12-13 14:41:49 +0800129
Claudiu Beznea0285827d2019-02-25 16:44:37 +0000130 for (*pres = 0; cycles > atmel_pwm->data->cfg.max_period; cycles >>= 1)
Claudiu Beznea1a722aa2017-03-22 15:29:34 +0200131 (*pres)++;
Bo Shen32b16d42013-12-13 14:41:49 +0800132
Claudiu Beznea0285827d2019-02-25 16:44:37 +0000133 if (*pres > atmel_pwm->data->cfg.max_pres) {
Nikolaus Vosse2e08972014-09-23 15:30:21 +0200134 dev_err(chip->dev, "pres exceeds the maximum value\n");
135 return -EINVAL;
Bo Shen32b16d42013-12-13 14:41:49 +0800136 }
137
Claudiu Beznea1a722aa2017-03-22 15:29:34 +0200138 *cprd = cycles;
Bo Shen32b16d42013-12-13 14:41:49 +0800139
Claudiu Beznea1a722aa2017-03-22 15:29:34 +0200140 return 0;
Bo Shen32b16d42013-12-13 14:41:49 +0800141}
142
Claudiu Beznea1a722aa2017-03-22 15:29:34 +0200143static void atmel_pwm_calculate_cdty(const struct pwm_state *state,
144 unsigned long cprd, unsigned long *cdty)
Bo Shen32b16d42013-12-13 14:41:49 +0800145{
Claudiu Beznea1a722aa2017-03-22 15:29:34 +0200146 unsigned long long cycles = state->duty_cycle;
Bo Shen32b16d42013-12-13 14:41:49 +0800147
Claudiu Beznea1a722aa2017-03-22 15:29:34 +0200148 cycles *= cprd;
149 do_div(cycles, state->period);
150 *cdty = cprd - cycles;
Bo Shen32b16d42013-12-13 14:41:49 +0800151}
152
Claudiu Beznea1a722aa2017-03-22 15:29:34 +0200153static void atmel_pwm_update_cdty(struct pwm_chip *chip, struct pwm_device *pwm,
154 unsigned long cdty)
Bo Shen32b16d42013-12-13 14:41:49 +0800155{
156 struct atmel_pwm_chip *atmel_pwm = to_atmel_pwm_chip(chip);
157 u32 val;
Bo Shen32b16d42013-12-13 14:41:49 +0800158
Claudiu Beznea53784152019-02-25 16:44:33 +0000159 if (atmel_pwm->data->regs.duty_upd ==
160 atmel_pwm->data->regs.period_upd) {
Claudiu Beznea1a722aa2017-03-22 15:29:34 +0200161 val = atmel_pwm_ch_readl(atmel_pwm, pwm->hwpwm, PWM_CMR);
162 val &= ~PWM_CMR_UPD_CDTY;
163 atmel_pwm_ch_writel(atmel_pwm, pwm->hwpwm, PWM_CMR, val);
Bo Shen32b16d42013-12-13 14:41:49 +0800164 }
165
Claudiu Beznea1a722aa2017-03-22 15:29:34 +0200166 atmel_pwm_ch_writel(atmel_pwm, pwm->hwpwm,
Claudiu Beznea53784152019-02-25 16:44:33 +0000167 atmel_pwm->data->regs.duty_upd, cdty);
Bo Shen32b16d42013-12-13 14:41:49 +0800168}
169
Claudiu Beznea1a722aa2017-03-22 15:29:34 +0200170static void atmel_pwm_set_cprd_cdty(struct pwm_chip *chip,
171 struct pwm_device *pwm,
172 unsigned long cprd, unsigned long cdty)
Bo Shen32b16d42013-12-13 14:41:49 +0800173{
174 struct atmel_pwm_chip *atmel_pwm = to_atmel_pwm_chip(chip);
Bo Shen32b16d42013-12-13 14:41:49 +0800175
Claudiu Beznea1a722aa2017-03-22 15:29:34 +0200176 atmel_pwm_ch_writel(atmel_pwm, pwm->hwpwm,
Claudiu Beznea53784152019-02-25 16:44:33 +0000177 atmel_pwm->data->regs.duty, cdty);
Claudiu Beznea1a722aa2017-03-22 15:29:34 +0200178 atmel_pwm_ch_writel(atmel_pwm, pwm->hwpwm,
Claudiu Beznea53784152019-02-25 16:44:33 +0000179 atmel_pwm->data->regs.period, cprd);
Bo Shen32b16d42013-12-13 14:41:49 +0800180}
181
Claudiu Beznea1a722aa2017-03-22 15:29:34 +0200182static void atmel_pwm_disable(struct pwm_chip *chip, struct pwm_device *pwm,
183 bool disable_clk)
Bo Shen32b16d42013-12-13 14:41:49 +0800184{
185 struct atmel_pwm_chip *atmel_pwm = to_atmel_pwm_chip(chip);
Alexandre Belloni472ac3d2015-05-25 18:11:49 +0200186 unsigned long timeout = jiffies + 2 * HZ;
Bo Shen32b16d42013-12-13 14:41:49 +0800187
Alexandre Belloni472ac3d2015-05-25 18:11:49 +0200188 /*
189 * Wait for at least a complete period to have passed before disabling a
190 * channel to be sure that CDTY has been updated
191 */
192 mutex_lock(&atmel_pwm->isr_lock);
193 atmel_pwm->updated_pwms |= atmel_pwm_readl(atmel_pwm, PWM_ISR);
194
195 while (!(atmel_pwm->updated_pwms & (1 << pwm->hwpwm)) &&
196 time_before(jiffies, timeout)) {
197 usleep_range(10, 100);
198 atmel_pwm->updated_pwms |= atmel_pwm_readl(atmel_pwm, PWM_ISR);
199 }
200
201 mutex_unlock(&atmel_pwm->isr_lock);
Bo Shen32b16d42013-12-13 14:41:49 +0800202 atmel_pwm_writel(atmel_pwm, PWM_DIS, 1 << pwm->hwpwm);
203
Guillermo Rodriguezf718c542016-05-13 13:09:37 +0200204 /*
205 * Wait for the PWM channel disable operation to be effective before
206 * stopping the clock.
207 */
208 timeout = jiffies + 2 * HZ;
209
210 while ((atmel_pwm_readl(atmel_pwm, PWM_SR) & (1 << pwm->hwpwm)) &&
211 time_before(jiffies, timeout))
212 usleep_range(10, 100);
213
Claudiu Beznea1a722aa2017-03-22 15:29:34 +0200214 if (disable_clk)
215 clk_disable(atmel_pwm->clk);
216}
217
218static int atmel_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm,
219 struct pwm_state *state)
220{
221 struct atmel_pwm_chip *atmel_pwm = to_atmel_pwm_chip(chip);
222 struct pwm_state cstate;
223 unsigned long cprd, cdty;
224 u32 pres, val;
225 int ret;
226
227 pwm_get_state(pwm, &cstate);
228
229 if (state->enabled) {
230 if (cstate.enabled &&
231 cstate.polarity == state->polarity &&
232 cstate.period == state->period) {
233 cprd = atmel_pwm_ch_readl(atmel_pwm, pwm->hwpwm,
Claudiu Beznea53784152019-02-25 16:44:33 +0000234 atmel_pwm->data->regs.period);
Claudiu Beznea1a722aa2017-03-22 15:29:34 +0200235 atmel_pwm_calculate_cdty(state, cprd, &cdty);
236 atmel_pwm_update_cdty(chip, pwm, cdty);
237 return 0;
238 }
239
240 ret = atmel_pwm_calculate_cprd_and_pres(chip, state, &cprd,
241 &pres);
242 if (ret) {
243 dev_err(chip->dev,
244 "failed to calculate cprd and prescaler\n");
245 return ret;
246 }
247
248 atmel_pwm_calculate_cdty(state, cprd, &cdty);
249
250 if (cstate.enabled) {
251 atmel_pwm_disable(chip, pwm, false);
252 } else {
253 ret = clk_enable(atmel_pwm->clk);
254 if (ret) {
255 dev_err(chip->dev, "failed to enable clock\n");
256 return ret;
257 }
258 }
259
260 /* It is necessary to preserve CPOL, inside CMR */
261 val = atmel_pwm_ch_readl(atmel_pwm, pwm->hwpwm, PWM_CMR);
262 val = (val & ~PWM_CMR_CPRE_MSK) | (pres & PWM_CMR_CPRE_MSK);
263 if (state->polarity == PWM_POLARITY_NORMAL)
264 val &= ~PWM_CMR_CPOL;
265 else
266 val |= PWM_CMR_CPOL;
267 atmel_pwm_ch_writel(atmel_pwm, pwm->hwpwm, PWM_CMR, val);
268 atmel_pwm_set_cprd_cdty(chip, pwm, cprd, cdty);
269 mutex_lock(&atmel_pwm->isr_lock);
270 atmel_pwm->updated_pwms |= atmel_pwm_readl(atmel_pwm, PWM_ISR);
271 atmel_pwm->updated_pwms &= ~(1 << pwm->hwpwm);
272 mutex_unlock(&atmel_pwm->isr_lock);
273 atmel_pwm_writel(atmel_pwm, PWM_ENA, 1 << pwm->hwpwm);
274 } else if (cstate.enabled) {
275 atmel_pwm_disable(chip, pwm, true);
276 }
277
278 return 0;
Bo Shen32b16d42013-12-13 14:41:49 +0800279}
280
281static const struct pwm_ops atmel_pwm_ops = {
Claudiu Beznea1a722aa2017-03-22 15:29:34 +0200282 .apply = atmel_pwm_apply,
Bo Shen32b16d42013-12-13 14:41:49 +0800283 .owner = THIS_MODULE,
284};
285
Claudiu Bezneaabcbe372019-02-25 16:44:41 +0000286static const struct atmel_pwm_data atmel_sam9rl_pwm_data = {
Claudiu Beznea53784152019-02-25 16:44:33 +0000287 .regs = {
288 .period = PWMV1_CPRD,
289 .period_upd = PWMV1_CUPD,
290 .duty = PWMV1_CDTY,
291 .duty_upd = PWMV1_CUPD,
292 },
Claudiu Beznea0285827d2019-02-25 16:44:37 +0000293 .cfg = {
294 /* 16 bits to keep period and duty. */
295 .max_period = PWM_MAXV1_PRD,
296 .max_pres = PRD_MAXV1_PRES,
297 },
Bo Shen32b16d42013-12-13 14:41:49 +0800298};
299
Claudiu Bezneaabcbe372019-02-25 16:44:41 +0000300static const struct atmel_pwm_data atmel_sama5_pwm_data = {
Claudiu Beznea53784152019-02-25 16:44:33 +0000301 .regs = {
302 .period = PWMV2_CPRD,
303 .period_upd = PWMV2_CPRDUPD,
304 .duty = PWMV2_CDTY,
305 .duty_upd = PWMV2_CDTYUPD,
306 },
Claudiu Beznea0285827d2019-02-25 16:44:37 +0000307 .cfg = {
308 /* 16 bits to keep period and duty. */
309 .max_period = PWM_MAXV1_PRD,
310 .max_pres = PRD_MAXV1_PRES,
311 },
Bo Shen32b16d42013-12-13 14:41:49 +0800312};
313
314static const struct platform_device_id atmel_pwm_devtypes[] = {
315 {
316 .name = "at91sam9rl-pwm",
Claudiu Bezneaabcbe372019-02-25 16:44:41 +0000317 .driver_data = (kernel_ulong_t)&atmel_sam9rl_pwm_data,
Bo Shen32b16d42013-12-13 14:41:49 +0800318 }, {
319 .name = "sama5d3-pwm",
Claudiu Bezneaabcbe372019-02-25 16:44:41 +0000320 .driver_data = (kernel_ulong_t)&atmel_sama5_pwm_data,
Bo Shen32b16d42013-12-13 14:41:49 +0800321 }, {
322 /* sentinel */
323 },
324};
325MODULE_DEVICE_TABLE(platform, atmel_pwm_devtypes);
326
327static const struct of_device_id atmel_pwm_dt_ids[] = {
328 {
329 .compatible = "atmel,at91sam9rl-pwm",
Claudiu Bezneaabcbe372019-02-25 16:44:41 +0000330 .data = &atmel_sam9rl_pwm_data,
Bo Shen32b16d42013-12-13 14:41:49 +0800331 }, {
332 .compatible = "atmel,sama5d3-pwm",
Claudiu Bezneaabcbe372019-02-25 16:44:41 +0000333 .data = &atmel_sama5_pwm_data,
Bo Shen32b16d42013-12-13 14:41:49 +0800334 }, {
Claudiu Beznea44521af2017-03-22 15:29:35 +0200335 .compatible = "atmel,sama5d2-pwm",
Claudiu Bezneaabcbe372019-02-25 16:44:41 +0000336 .data = &atmel_sama5_pwm_data,
Claudiu Beznea44521af2017-03-22 15:29:35 +0200337 }, {
Bo Shen32b16d42013-12-13 14:41:49 +0800338 /* sentinel */
339 },
340};
341MODULE_DEVICE_TABLE(of, atmel_pwm_dt_ids);
342
Claudiu Beznea53784152019-02-25 16:44:33 +0000343static inline const struct atmel_pwm_data *
Bo Shen32b16d42013-12-13 14:41:49 +0800344atmel_pwm_get_driver_data(struct platform_device *pdev)
345{
Thierry Reding313b78e2016-07-11 12:14:34 +0200346 const struct platform_device_id *id;
347
Thierry Reding017bb042016-07-11 12:16:01 +0200348 if (pdev->dev.of_node)
349 return of_device_get_match_data(&pdev->dev);
Thierry Reding313b78e2016-07-11 12:14:34 +0200350
351 id = platform_get_device_id(pdev);
352
Claudiu Beznea53784152019-02-25 16:44:33 +0000353 return (struct atmel_pwm_data *)id->driver_data;
Bo Shen32b16d42013-12-13 14:41:49 +0800354}
355
356static int atmel_pwm_probe(struct platform_device *pdev)
357{
Claudiu Beznea53784152019-02-25 16:44:33 +0000358 const struct atmel_pwm_data *data;
Bo Shen32b16d42013-12-13 14:41:49 +0800359 struct atmel_pwm_chip *atmel_pwm;
360 struct resource *res;
361 int ret;
362
Claudiu Beznea53784152019-02-25 16:44:33 +0000363 data = atmel_pwm_get_driver_data(pdev);
364 if (!data)
Bo Shen32b16d42013-12-13 14:41:49 +0800365 return -ENODEV;
366
367 atmel_pwm = devm_kzalloc(&pdev->dev, sizeof(*atmel_pwm), GFP_KERNEL);
368 if (!atmel_pwm)
369 return -ENOMEM;
370
371 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
372 atmel_pwm->base = devm_ioremap_resource(&pdev->dev, res);
373 if (IS_ERR(atmel_pwm->base))
374 return PTR_ERR(atmel_pwm->base);
375
376 atmel_pwm->clk = devm_clk_get(&pdev->dev, NULL);
377 if (IS_ERR(atmel_pwm->clk))
378 return PTR_ERR(atmel_pwm->clk);
379
380 ret = clk_prepare(atmel_pwm->clk);
381 if (ret) {
382 dev_err(&pdev->dev, "failed to prepare PWM clock\n");
383 return ret;
384 }
385
386 atmel_pwm->chip.dev = &pdev->dev;
387 atmel_pwm->chip.ops = &atmel_pwm_ops;
388
389 if (pdev->dev.of_node) {
390 atmel_pwm->chip.of_xlate = of_pwm_xlate_with_flags;
391 atmel_pwm->chip.of_pwm_n_cells = 3;
392 }
393
394 atmel_pwm->chip.base = -1;
395 atmel_pwm->chip.npwm = 4;
Claudiu Beznea53784152019-02-25 16:44:33 +0000396 atmel_pwm->data = data;
Alexandre Belloni472ac3d2015-05-25 18:11:49 +0200397 atmel_pwm->updated_pwms = 0;
398 mutex_init(&atmel_pwm->isr_lock);
Bo Shen32b16d42013-12-13 14:41:49 +0800399
400 ret = pwmchip_add(&atmel_pwm->chip);
401 if (ret < 0) {
402 dev_err(&pdev->dev, "failed to add PWM chip %d\n", ret);
403 goto unprepare_clk;
404 }
405
406 platform_set_drvdata(pdev, atmel_pwm);
407
Bo Shen6a683352013-12-19 11:42:22 +0800408 return ret;
409
Bo Shen32b16d42013-12-13 14:41:49 +0800410unprepare_clk:
411 clk_unprepare(atmel_pwm->clk);
412 return ret;
413}
414
415static int atmel_pwm_remove(struct platform_device *pdev)
416{
417 struct atmel_pwm_chip *atmel_pwm = platform_get_drvdata(pdev);
418
419 clk_unprepare(atmel_pwm->clk);
Alexandre Belloni472ac3d2015-05-25 18:11:49 +0200420 mutex_destroy(&atmel_pwm->isr_lock);
Bo Shen32b16d42013-12-13 14:41:49 +0800421
422 return pwmchip_remove(&atmel_pwm->chip);
423}
424
425static struct platform_driver atmel_pwm_driver = {
426 .driver = {
427 .name = "atmel-pwm",
428 .of_match_table = of_match_ptr(atmel_pwm_dt_ids),
429 },
430 .id_table = atmel_pwm_devtypes,
431 .probe = atmel_pwm_probe,
432 .remove = atmel_pwm_remove,
433};
434module_platform_driver(atmel_pwm_driver);
435
436MODULE_ALIAS("platform:atmel-pwm");
437MODULE_AUTHOR("Bo Shen <voice.shen@atmel.com>");
438MODULE_DESCRIPTION("Atmel PWM driver");
439MODULE_LICENSE("GPL v2");