blob: 7e86a5266eb6d666da5a08b475709492a9cb8001 [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
51/*
52 * Max value for duty and period
53 *
54 * Although the duty and period register is 32 bit,
55 * however only the LSB 16 bits are significant.
56 */
57#define PWM_MAX_DTY 0xFFFF
58#define PWM_MAX_PRD 0xFFFF
59#define PRD_MAX_PRES 10
60
Claudiu Beznea1a722aa2017-03-22 15:29:34 +020061struct atmel_pwm_registers {
62 u8 period;
63 u8 period_upd;
64 u8 duty;
65 u8 duty_upd;
66};
67
Claudiu Beznea53784152019-02-25 16:44:33 +000068struct atmel_pwm_data {
69 struct atmel_pwm_registers regs;
70};
71
Bo Shen32b16d42013-12-13 14:41:49 +080072struct atmel_pwm_chip {
73 struct pwm_chip chip;
74 struct clk *clk;
75 void __iomem *base;
Claudiu Beznea53784152019-02-25 16:44:33 +000076 const struct atmel_pwm_data *data;
Bo Shen32b16d42013-12-13 14:41:49 +080077
Alexandre Belloni472ac3d2015-05-25 18:11:49 +020078 unsigned int updated_pwms;
Thierry Reding313b78e2016-07-11 12:14:34 +020079 /* ISR is cleared when read, ensure only one thread does that */
80 struct mutex isr_lock;
Bo Shen32b16d42013-12-13 14:41:49 +080081};
82
83static inline struct atmel_pwm_chip *to_atmel_pwm_chip(struct pwm_chip *chip)
84{
85 return container_of(chip, struct atmel_pwm_chip, chip);
86}
87
88static inline u32 atmel_pwm_readl(struct atmel_pwm_chip *chip,
89 unsigned long offset)
90{
91 return readl_relaxed(chip->base + offset);
92}
93
94static inline void atmel_pwm_writel(struct atmel_pwm_chip *chip,
95 unsigned long offset, unsigned long val)
96{
97 writel_relaxed(val, chip->base + offset);
98}
99
100static inline u32 atmel_pwm_ch_readl(struct atmel_pwm_chip *chip,
101 unsigned int ch, unsigned long offset)
102{
103 unsigned long base = PWM_CH_REG_OFFSET + ch * PWM_CH_REG_SIZE;
104
105 return readl_relaxed(chip->base + base + offset);
106}
107
108static inline void atmel_pwm_ch_writel(struct atmel_pwm_chip *chip,
109 unsigned int ch, unsigned long offset,
110 unsigned long val)
111{
112 unsigned long base = PWM_CH_REG_OFFSET + ch * PWM_CH_REG_SIZE;
113
114 writel_relaxed(val, chip->base + base + offset);
115}
116
Claudiu Beznea1a722aa2017-03-22 15:29:34 +0200117static int atmel_pwm_calculate_cprd_and_pres(struct pwm_chip *chip,
118 const struct pwm_state *state,
119 unsigned long *cprd, u32 *pres)
Bo Shen32b16d42013-12-13 14:41:49 +0800120{
121 struct atmel_pwm_chip *atmel_pwm = to_atmel_pwm_chip(chip);
Claudiu Beznea1a722aa2017-03-22 15:29:34 +0200122 unsigned long long cycles = state->period;
Bo Shen32b16d42013-12-13 14:41:49 +0800123
Nikolaus Vosse2e08972014-09-23 15:30:21 +0200124 /* Calculate the period cycles and prescale value */
Claudiu Beznea1a722aa2017-03-22 15:29:34 +0200125 cycles *= clk_get_rate(atmel_pwm->clk);
126 do_div(cycles, NSEC_PER_SEC);
Bo Shen32b16d42013-12-13 14:41:49 +0800127
Claudiu Beznea1a722aa2017-03-22 15:29:34 +0200128 for (*pres = 0; cycles > PWM_MAX_PRD; cycles >>= 1)
129 (*pres)++;
Bo Shen32b16d42013-12-13 14:41:49 +0800130
Claudiu Beznea1a722aa2017-03-22 15:29:34 +0200131 if (*pres > PRD_MAX_PRES) {
Nikolaus Vosse2e08972014-09-23 15:30:21 +0200132 dev_err(chip->dev, "pres exceeds the maximum value\n");
133 return -EINVAL;
Bo Shen32b16d42013-12-13 14:41:49 +0800134 }
135
Claudiu Beznea1a722aa2017-03-22 15:29:34 +0200136 *cprd = cycles;
Bo Shen32b16d42013-12-13 14:41:49 +0800137
Claudiu Beznea1a722aa2017-03-22 15:29:34 +0200138 return 0;
Bo Shen32b16d42013-12-13 14:41:49 +0800139}
140
Claudiu Beznea1a722aa2017-03-22 15:29:34 +0200141static void atmel_pwm_calculate_cdty(const struct pwm_state *state,
142 unsigned long cprd, unsigned long *cdty)
Bo Shen32b16d42013-12-13 14:41:49 +0800143{
Claudiu Beznea1a722aa2017-03-22 15:29:34 +0200144 unsigned long long cycles = state->duty_cycle;
Bo Shen32b16d42013-12-13 14:41:49 +0800145
Claudiu Beznea1a722aa2017-03-22 15:29:34 +0200146 cycles *= cprd;
147 do_div(cycles, state->period);
148 *cdty = cprd - cycles;
Bo Shen32b16d42013-12-13 14:41:49 +0800149}
150
Claudiu Beznea1a722aa2017-03-22 15:29:34 +0200151static void atmel_pwm_update_cdty(struct pwm_chip *chip, struct pwm_device *pwm,
152 unsigned long cdty)
Bo Shen32b16d42013-12-13 14:41:49 +0800153{
154 struct atmel_pwm_chip *atmel_pwm = to_atmel_pwm_chip(chip);
155 u32 val;
Bo Shen32b16d42013-12-13 14:41:49 +0800156
Claudiu Beznea53784152019-02-25 16:44:33 +0000157 if (atmel_pwm->data->regs.duty_upd ==
158 atmel_pwm->data->regs.period_upd) {
Claudiu Beznea1a722aa2017-03-22 15:29:34 +0200159 val = atmel_pwm_ch_readl(atmel_pwm, pwm->hwpwm, PWM_CMR);
160 val &= ~PWM_CMR_UPD_CDTY;
161 atmel_pwm_ch_writel(atmel_pwm, pwm->hwpwm, PWM_CMR, val);
Bo Shen32b16d42013-12-13 14:41:49 +0800162 }
163
Claudiu Beznea1a722aa2017-03-22 15:29:34 +0200164 atmel_pwm_ch_writel(atmel_pwm, pwm->hwpwm,
Claudiu Beznea53784152019-02-25 16:44:33 +0000165 atmel_pwm->data->regs.duty_upd, cdty);
Bo Shen32b16d42013-12-13 14:41:49 +0800166}
167
Claudiu Beznea1a722aa2017-03-22 15:29:34 +0200168static void atmel_pwm_set_cprd_cdty(struct pwm_chip *chip,
169 struct pwm_device *pwm,
170 unsigned long cprd, unsigned long cdty)
Bo Shen32b16d42013-12-13 14:41:49 +0800171{
172 struct atmel_pwm_chip *atmel_pwm = to_atmel_pwm_chip(chip);
Bo Shen32b16d42013-12-13 14:41:49 +0800173
Claudiu Beznea1a722aa2017-03-22 15:29:34 +0200174 atmel_pwm_ch_writel(atmel_pwm, pwm->hwpwm,
Claudiu Beznea53784152019-02-25 16:44:33 +0000175 atmel_pwm->data->regs.duty, cdty);
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.period, cprd);
Bo Shen32b16d42013-12-13 14:41:49 +0800178}
179
Claudiu Beznea1a722aa2017-03-22 15:29:34 +0200180static void atmel_pwm_disable(struct pwm_chip *chip, struct pwm_device *pwm,
181 bool disable_clk)
Bo Shen32b16d42013-12-13 14:41:49 +0800182{
183 struct atmel_pwm_chip *atmel_pwm = to_atmel_pwm_chip(chip);
Alexandre Belloni472ac3d2015-05-25 18:11:49 +0200184 unsigned long timeout = jiffies + 2 * HZ;
Bo Shen32b16d42013-12-13 14:41:49 +0800185
Alexandre Belloni472ac3d2015-05-25 18:11:49 +0200186 /*
187 * Wait for at least a complete period to have passed before disabling a
188 * channel to be sure that CDTY has been updated
189 */
190 mutex_lock(&atmel_pwm->isr_lock);
191 atmel_pwm->updated_pwms |= atmel_pwm_readl(atmel_pwm, PWM_ISR);
192
193 while (!(atmel_pwm->updated_pwms & (1 << pwm->hwpwm)) &&
194 time_before(jiffies, timeout)) {
195 usleep_range(10, 100);
196 atmel_pwm->updated_pwms |= atmel_pwm_readl(atmel_pwm, PWM_ISR);
197 }
198
199 mutex_unlock(&atmel_pwm->isr_lock);
Bo Shen32b16d42013-12-13 14:41:49 +0800200 atmel_pwm_writel(atmel_pwm, PWM_DIS, 1 << pwm->hwpwm);
201
Guillermo Rodriguezf718c542016-05-13 13:09:37 +0200202 /*
203 * Wait for the PWM channel disable operation to be effective before
204 * stopping the clock.
205 */
206 timeout = jiffies + 2 * HZ;
207
208 while ((atmel_pwm_readl(atmel_pwm, PWM_SR) & (1 << pwm->hwpwm)) &&
209 time_before(jiffies, timeout))
210 usleep_range(10, 100);
211
Claudiu Beznea1a722aa2017-03-22 15:29:34 +0200212 if (disable_clk)
213 clk_disable(atmel_pwm->clk);
214}
215
216static int atmel_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm,
217 struct pwm_state *state)
218{
219 struct atmel_pwm_chip *atmel_pwm = to_atmel_pwm_chip(chip);
220 struct pwm_state cstate;
221 unsigned long cprd, cdty;
222 u32 pres, val;
223 int ret;
224
225 pwm_get_state(pwm, &cstate);
226
227 if (state->enabled) {
228 if (cstate.enabled &&
229 cstate.polarity == state->polarity &&
230 cstate.period == state->period) {
231 cprd = atmel_pwm_ch_readl(atmel_pwm, pwm->hwpwm,
Claudiu Beznea53784152019-02-25 16:44:33 +0000232 atmel_pwm->data->regs.period);
Claudiu Beznea1a722aa2017-03-22 15:29:34 +0200233 atmel_pwm_calculate_cdty(state, cprd, &cdty);
234 atmel_pwm_update_cdty(chip, pwm, cdty);
235 return 0;
236 }
237
238 ret = atmel_pwm_calculate_cprd_and_pres(chip, state, &cprd,
239 &pres);
240 if (ret) {
241 dev_err(chip->dev,
242 "failed to calculate cprd and prescaler\n");
243 return ret;
244 }
245
246 atmel_pwm_calculate_cdty(state, cprd, &cdty);
247
248 if (cstate.enabled) {
249 atmel_pwm_disable(chip, pwm, false);
250 } else {
251 ret = clk_enable(atmel_pwm->clk);
252 if (ret) {
253 dev_err(chip->dev, "failed to enable clock\n");
254 return ret;
255 }
256 }
257
258 /* It is necessary to preserve CPOL, inside CMR */
259 val = atmel_pwm_ch_readl(atmel_pwm, pwm->hwpwm, PWM_CMR);
260 val = (val & ~PWM_CMR_CPRE_MSK) | (pres & PWM_CMR_CPRE_MSK);
261 if (state->polarity == PWM_POLARITY_NORMAL)
262 val &= ~PWM_CMR_CPOL;
263 else
264 val |= PWM_CMR_CPOL;
265 atmel_pwm_ch_writel(atmel_pwm, pwm->hwpwm, PWM_CMR, val);
266 atmel_pwm_set_cprd_cdty(chip, pwm, cprd, cdty);
267 mutex_lock(&atmel_pwm->isr_lock);
268 atmel_pwm->updated_pwms |= atmel_pwm_readl(atmel_pwm, PWM_ISR);
269 atmel_pwm->updated_pwms &= ~(1 << pwm->hwpwm);
270 mutex_unlock(&atmel_pwm->isr_lock);
271 atmel_pwm_writel(atmel_pwm, PWM_ENA, 1 << pwm->hwpwm);
272 } else if (cstate.enabled) {
273 atmel_pwm_disable(chip, pwm, true);
274 }
275
276 return 0;
Bo Shen32b16d42013-12-13 14:41:49 +0800277}
278
279static const struct pwm_ops atmel_pwm_ops = {
Claudiu Beznea1a722aa2017-03-22 15:29:34 +0200280 .apply = atmel_pwm_apply,
Bo Shen32b16d42013-12-13 14:41:49 +0800281 .owner = THIS_MODULE,
282};
283
Claudiu Beznea53784152019-02-25 16:44:33 +0000284static const struct atmel_pwm_data atmel_pwm_data_v1 = {
285 .regs = {
286 .period = PWMV1_CPRD,
287 .period_upd = PWMV1_CUPD,
288 .duty = PWMV1_CDTY,
289 .duty_upd = PWMV1_CUPD,
290 },
Bo Shen32b16d42013-12-13 14:41:49 +0800291};
292
Claudiu Beznea53784152019-02-25 16:44:33 +0000293static const struct atmel_pwm_data atmel_pwm_data_v2 = {
294 .regs = {
295 .period = PWMV2_CPRD,
296 .period_upd = PWMV2_CPRDUPD,
297 .duty = PWMV2_CDTY,
298 .duty_upd = PWMV2_CDTYUPD,
299 },
Bo Shen32b16d42013-12-13 14:41:49 +0800300};
301
302static const struct platform_device_id atmel_pwm_devtypes[] = {
303 {
304 .name = "at91sam9rl-pwm",
Claudiu Beznea53784152019-02-25 16:44:33 +0000305 .driver_data = (kernel_ulong_t)&atmel_pwm_data_v1,
Bo Shen32b16d42013-12-13 14:41:49 +0800306 }, {
307 .name = "sama5d3-pwm",
Claudiu Beznea53784152019-02-25 16:44:33 +0000308 .driver_data = (kernel_ulong_t)&atmel_pwm_data_v2,
Bo Shen32b16d42013-12-13 14:41:49 +0800309 }, {
310 /* sentinel */
311 },
312};
313MODULE_DEVICE_TABLE(platform, atmel_pwm_devtypes);
314
315static const struct of_device_id atmel_pwm_dt_ids[] = {
316 {
317 .compatible = "atmel,at91sam9rl-pwm",
Claudiu Beznea53784152019-02-25 16:44:33 +0000318 .data = &atmel_pwm_data_v1,
Bo Shen32b16d42013-12-13 14:41:49 +0800319 }, {
320 .compatible = "atmel,sama5d3-pwm",
Claudiu Beznea53784152019-02-25 16:44:33 +0000321 .data = &atmel_pwm_data_v2,
Bo Shen32b16d42013-12-13 14:41:49 +0800322 }, {
Claudiu Beznea44521af2017-03-22 15:29:35 +0200323 .compatible = "atmel,sama5d2-pwm",
Claudiu Beznea53784152019-02-25 16:44:33 +0000324 .data = &atmel_pwm_data_v2,
Claudiu Beznea44521af2017-03-22 15:29:35 +0200325 }, {
Bo Shen32b16d42013-12-13 14:41:49 +0800326 /* sentinel */
327 },
328};
329MODULE_DEVICE_TABLE(of, atmel_pwm_dt_ids);
330
Claudiu Beznea53784152019-02-25 16:44:33 +0000331static inline const struct atmel_pwm_data *
Bo Shen32b16d42013-12-13 14:41:49 +0800332atmel_pwm_get_driver_data(struct platform_device *pdev)
333{
Thierry Reding313b78e2016-07-11 12:14:34 +0200334 const struct platform_device_id *id;
335
Thierry Reding017bb042016-07-11 12:16:01 +0200336 if (pdev->dev.of_node)
337 return of_device_get_match_data(&pdev->dev);
Thierry Reding313b78e2016-07-11 12:14:34 +0200338
339 id = platform_get_device_id(pdev);
340
Claudiu Beznea53784152019-02-25 16:44:33 +0000341 return (struct atmel_pwm_data *)id->driver_data;
Bo Shen32b16d42013-12-13 14:41:49 +0800342}
343
344static int atmel_pwm_probe(struct platform_device *pdev)
345{
Claudiu Beznea53784152019-02-25 16:44:33 +0000346 const struct atmel_pwm_data *data;
Bo Shen32b16d42013-12-13 14:41:49 +0800347 struct atmel_pwm_chip *atmel_pwm;
348 struct resource *res;
349 int ret;
350
Claudiu Beznea53784152019-02-25 16:44:33 +0000351 data = atmel_pwm_get_driver_data(pdev);
352 if (!data)
Bo Shen32b16d42013-12-13 14:41:49 +0800353 return -ENODEV;
354
355 atmel_pwm = devm_kzalloc(&pdev->dev, sizeof(*atmel_pwm), GFP_KERNEL);
356 if (!atmel_pwm)
357 return -ENOMEM;
358
359 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
360 atmel_pwm->base = devm_ioremap_resource(&pdev->dev, res);
361 if (IS_ERR(atmel_pwm->base))
362 return PTR_ERR(atmel_pwm->base);
363
364 atmel_pwm->clk = devm_clk_get(&pdev->dev, NULL);
365 if (IS_ERR(atmel_pwm->clk))
366 return PTR_ERR(atmel_pwm->clk);
367
368 ret = clk_prepare(atmel_pwm->clk);
369 if (ret) {
370 dev_err(&pdev->dev, "failed to prepare PWM clock\n");
371 return ret;
372 }
373
374 atmel_pwm->chip.dev = &pdev->dev;
375 atmel_pwm->chip.ops = &atmel_pwm_ops;
376
377 if (pdev->dev.of_node) {
378 atmel_pwm->chip.of_xlate = of_pwm_xlate_with_flags;
379 atmel_pwm->chip.of_pwm_n_cells = 3;
380 }
381
382 atmel_pwm->chip.base = -1;
383 atmel_pwm->chip.npwm = 4;
Claudiu Beznea53784152019-02-25 16:44:33 +0000384 atmel_pwm->data = data;
Alexandre Belloni472ac3d2015-05-25 18:11:49 +0200385 atmel_pwm->updated_pwms = 0;
386 mutex_init(&atmel_pwm->isr_lock);
Bo Shen32b16d42013-12-13 14:41:49 +0800387
388 ret = pwmchip_add(&atmel_pwm->chip);
389 if (ret < 0) {
390 dev_err(&pdev->dev, "failed to add PWM chip %d\n", ret);
391 goto unprepare_clk;
392 }
393
394 platform_set_drvdata(pdev, atmel_pwm);
395
Bo Shen6a683352013-12-19 11:42:22 +0800396 return ret;
397
Bo Shen32b16d42013-12-13 14:41:49 +0800398unprepare_clk:
399 clk_unprepare(atmel_pwm->clk);
400 return ret;
401}
402
403static int atmel_pwm_remove(struct platform_device *pdev)
404{
405 struct atmel_pwm_chip *atmel_pwm = platform_get_drvdata(pdev);
406
407 clk_unprepare(atmel_pwm->clk);
Alexandre Belloni472ac3d2015-05-25 18:11:49 +0200408 mutex_destroy(&atmel_pwm->isr_lock);
Bo Shen32b16d42013-12-13 14:41:49 +0800409
410 return pwmchip_remove(&atmel_pwm->chip);
411}
412
413static struct platform_driver atmel_pwm_driver = {
414 .driver = {
415 .name = "atmel-pwm",
416 .of_match_table = of_match_ptr(atmel_pwm_dt_ids),
417 },
418 .id_table = atmel_pwm_devtypes,
419 .probe = atmel_pwm_probe,
420 .remove = atmel_pwm_remove,
421};
422module_platform_driver(atmel_pwm_driver);
423
424MODULE_ALIAS("platform:atmel-pwm");
425MODULE_AUTHOR("Bo Shen <voice.shen@atmel.com>");
426MODULE_DESCRIPTION("Atmel PWM driver");
427MODULE_LICENSE("GPL v2");