Loading...
1// SPDX-License-Identifier: GPL-2.0-only
2/*
3 * Driver for Atmel Pulse Width Modulation Controller
4 *
5 * Copyright (C) 2013 Atmel Corporation
6 * Bo Shen <voice.shen@atmel.com>
7 *
8 * Links to reference manuals for the supported PWM chips can be found in
9 * Documentation/arm/microchip.rst.
10 *
11 * Limitations:
12 * - Periods start with the inactive level.
13 * - Hardware has to be stopped in general to update settings.
14 *
15 * Software bugs/possible improvements:
16 * - When atmel_pwm_apply() is called with state->enabled=false a change in
17 * state->polarity isn't honored.
18 * - Instead of sleeping to wait for a completed period, the interrupt
19 * functionality could be used.
20 */
21
22#include <linux/clk.h>
23#include <linux/delay.h>
24#include <linux/err.h>
25#include <linux/io.h>
26#include <linux/module.h>
27#include <linux/of.h>
28#include <linux/of_device.h>
29#include <linux/platform_device.h>
30#include <linux/pwm.h>
31#include <linux/slab.h>
32
33/* The following is global registers for PWM controller */
34#define PWM_ENA 0x04
35#define PWM_DIS 0x08
36#define PWM_SR 0x0C
37#define PWM_ISR 0x1C
38/* Bit field in SR */
39#define PWM_SR_ALL_CH_ON 0x0F
40
41/* The following register is PWM channel related registers */
42#define PWM_CH_REG_OFFSET 0x200
43#define PWM_CH_REG_SIZE 0x20
44
45#define PWM_CMR 0x0
46/* Bit field in CMR */
47#define PWM_CMR_CPOL (1 << 9)
48#define PWM_CMR_UPD_CDTY (1 << 10)
49#define PWM_CMR_CPRE_MSK 0xF
50
51/* The following registers for PWM v1 */
52#define PWMV1_CDTY 0x04
53#define PWMV1_CPRD 0x08
54#define PWMV1_CUPD 0x10
55
56/* The following registers for PWM v2 */
57#define PWMV2_CDTY 0x04
58#define PWMV2_CDTYUPD 0x08
59#define PWMV2_CPRD 0x0C
60#define PWMV2_CPRDUPD 0x10
61
62#define PWM_MAX_PRES 10
63
64struct atmel_pwm_registers {
65 u8 period;
66 u8 period_upd;
67 u8 duty;
68 u8 duty_upd;
69};
70
71struct atmel_pwm_config {
72 u32 period_bits;
73};
74
75struct atmel_pwm_data {
76 struct atmel_pwm_registers regs;
77 struct atmel_pwm_config cfg;
78};
79
80struct atmel_pwm_chip {
81 struct pwm_chip chip;
82 struct clk *clk;
83 void __iomem *base;
84 const struct atmel_pwm_data *data;
85
86 /*
87 * The hardware supports a mechanism to update a channel's duty cycle at
88 * the end of the currently running period. When such an update is
89 * pending we delay disabling the PWM until the new configuration is
90 * active because otherwise pmw_config(duty_cycle=0); pwm_disable();
91 * might not result in an inactive output.
92 * This bitmask tracks for which channels an update is pending in
93 * hardware.
94 */
95 u32 update_pending;
96
97 /* Protects .update_pending */
98 spinlock_t lock;
99};
100
101static inline struct atmel_pwm_chip *to_atmel_pwm_chip(struct pwm_chip *chip)
102{
103 return container_of(chip, struct atmel_pwm_chip, chip);
104}
105
106static inline u32 atmel_pwm_readl(struct atmel_pwm_chip *chip,
107 unsigned long offset)
108{
109 return readl_relaxed(chip->base + offset);
110}
111
112static inline void atmel_pwm_writel(struct atmel_pwm_chip *chip,
113 unsigned long offset, unsigned long val)
114{
115 writel_relaxed(val, chip->base + offset);
116}
117
118static inline u32 atmel_pwm_ch_readl(struct atmel_pwm_chip *chip,
119 unsigned int ch, unsigned long offset)
120{
121 unsigned long base = PWM_CH_REG_OFFSET + ch * PWM_CH_REG_SIZE;
122
123 return atmel_pwm_readl(chip, base + offset);
124}
125
126static inline void atmel_pwm_ch_writel(struct atmel_pwm_chip *chip,
127 unsigned int ch, unsigned long offset,
128 unsigned long val)
129{
130 unsigned long base = PWM_CH_REG_OFFSET + ch * PWM_CH_REG_SIZE;
131
132 atmel_pwm_writel(chip, base + offset, val);
133}
134
135static void atmel_pwm_update_pending(struct atmel_pwm_chip *chip)
136{
137 /*
138 * Each channel that has its bit in ISR set started a new period since
139 * ISR was cleared and so there is no more update pending. Note that
140 * reading ISR clears it, so this needs to handle all channels to not
141 * loose information.
142 */
143 u32 isr = atmel_pwm_readl(chip, PWM_ISR);
144
145 chip->update_pending &= ~isr;
146}
147
148static void atmel_pwm_set_pending(struct atmel_pwm_chip *chip, unsigned int ch)
149{
150 spin_lock(&chip->lock);
151
152 /*
153 * Clear pending flags in hardware because otherwise there might still
154 * be a stale flag in ISR.
155 */
156 atmel_pwm_update_pending(chip);
157
158 chip->update_pending |= (1 << ch);
159
160 spin_unlock(&chip->lock);
161}
162
163static int atmel_pwm_test_pending(struct atmel_pwm_chip *chip, unsigned int ch)
164{
165 int ret = 0;
166
167 spin_lock(&chip->lock);
168
169 if (chip->update_pending & (1 << ch)) {
170 atmel_pwm_update_pending(chip);
171
172 if (chip->update_pending & (1 << ch))
173 ret = 1;
174 }
175
176 spin_unlock(&chip->lock);
177
178 return ret;
179}
180
181static int atmel_pwm_wait_nonpending(struct atmel_pwm_chip *chip, unsigned int ch)
182{
183 unsigned long timeout = jiffies + 2 * HZ;
184 int ret;
185
186 while ((ret = atmel_pwm_test_pending(chip, ch)) &&
187 time_before(jiffies, timeout))
188 usleep_range(10, 100);
189
190 return ret ? -ETIMEDOUT : 0;
191}
192
193static int atmel_pwm_calculate_cprd_and_pres(struct pwm_chip *chip,
194 unsigned long clkrate,
195 const struct pwm_state *state,
196 unsigned long *cprd, u32 *pres)
197{
198 struct atmel_pwm_chip *atmel_pwm = to_atmel_pwm_chip(chip);
199 unsigned long long cycles = state->period;
200 int shift;
201
202 /* Calculate the period cycles and prescale value */
203 cycles *= clkrate;
204 do_div(cycles, NSEC_PER_SEC);
205
206 /*
207 * The register for the period length is cfg.period_bits bits wide.
208 * So for each bit the number of clock cycles is wider divide the input
209 * clock frequency by two using pres and shift cprd accordingly.
210 */
211 shift = fls(cycles) - atmel_pwm->data->cfg.period_bits;
212
213 if (shift > PWM_MAX_PRES) {
214 dev_err(chip->dev, "pres exceeds the maximum value\n");
215 return -EINVAL;
216 } else if (shift > 0) {
217 *pres = shift;
218 cycles >>= *pres;
219 } else {
220 *pres = 0;
221 }
222
223 *cprd = cycles;
224
225 return 0;
226}
227
228static void atmel_pwm_calculate_cdty(const struct pwm_state *state,
229 unsigned long clkrate, unsigned long cprd,
230 u32 pres, unsigned long *cdty)
231{
232 unsigned long long cycles = state->duty_cycle;
233
234 cycles *= clkrate;
235 do_div(cycles, NSEC_PER_SEC);
236 cycles >>= pres;
237 *cdty = cprd - cycles;
238}
239
240static void atmel_pwm_update_cdty(struct pwm_chip *chip, struct pwm_device *pwm,
241 unsigned long cdty)
242{
243 struct atmel_pwm_chip *atmel_pwm = to_atmel_pwm_chip(chip);
244 u32 val;
245
246 if (atmel_pwm->data->regs.duty_upd ==
247 atmel_pwm->data->regs.period_upd) {
248 val = atmel_pwm_ch_readl(atmel_pwm, pwm->hwpwm, PWM_CMR);
249 val &= ~PWM_CMR_UPD_CDTY;
250 atmel_pwm_ch_writel(atmel_pwm, pwm->hwpwm, PWM_CMR, val);
251 }
252
253 atmel_pwm_ch_writel(atmel_pwm, pwm->hwpwm,
254 atmel_pwm->data->regs.duty_upd, cdty);
255 atmel_pwm_set_pending(atmel_pwm, pwm->hwpwm);
256}
257
258static void atmel_pwm_set_cprd_cdty(struct pwm_chip *chip,
259 struct pwm_device *pwm,
260 unsigned long cprd, unsigned long cdty)
261{
262 struct atmel_pwm_chip *atmel_pwm = to_atmel_pwm_chip(chip);
263
264 atmel_pwm_ch_writel(atmel_pwm, pwm->hwpwm,
265 atmel_pwm->data->regs.duty, cdty);
266 atmel_pwm_ch_writel(atmel_pwm, pwm->hwpwm,
267 atmel_pwm->data->regs.period, cprd);
268}
269
270static void atmel_pwm_disable(struct pwm_chip *chip, struct pwm_device *pwm,
271 bool disable_clk)
272{
273 struct atmel_pwm_chip *atmel_pwm = to_atmel_pwm_chip(chip);
274 unsigned long timeout;
275
276 atmel_pwm_wait_nonpending(atmel_pwm, pwm->hwpwm);
277
278 atmel_pwm_writel(atmel_pwm, PWM_DIS, 1 << pwm->hwpwm);
279
280 /*
281 * Wait for the PWM channel disable operation to be effective before
282 * stopping the clock.
283 */
284 timeout = jiffies + 2 * HZ;
285
286 while ((atmel_pwm_readl(atmel_pwm, PWM_SR) & (1 << pwm->hwpwm)) &&
287 time_before(jiffies, timeout))
288 usleep_range(10, 100);
289
290 if (disable_clk)
291 clk_disable(atmel_pwm->clk);
292}
293
294static int atmel_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm,
295 const struct pwm_state *state)
296{
297 struct atmel_pwm_chip *atmel_pwm = to_atmel_pwm_chip(chip);
298 struct pwm_state cstate;
299 unsigned long cprd, cdty;
300 u32 pres, val;
301 int ret;
302
303 pwm_get_state(pwm, &cstate);
304
305 if (state->enabled) {
306 unsigned long clkrate = clk_get_rate(atmel_pwm->clk);
307
308 if (cstate.enabled &&
309 cstate.polarity == state->polarity &&
310 cstate.period == state->period) {
311 u32 cmr = atmel_pwm_ch_readl(atmel_pwm, pwm->hwpwm, PWM_CMR);
312
313 cprd = atmel_pwm_ch_readl(atmel_pwm, pwm->hwpwm,
314 atmel_pwm->data->regs.period);
315 pres = cmr & PWM_CMR_CPRE_MSK;
316
317 atmel_pwm_calculate_cdty(state, clkrate, cprd, pres, &cdty);
318 atmel_pwm_update_cdty(chip, pwm, cdty);
319 return 0;
320 }
321
322 ret = atmel_pwm_calculate_cprd_and_pres(chip, clkrate, state, &cprd,
323 &pres);
324 if (ret) {
325 dev_err(chip->dev,
326 "failed to calculate cprd and prescaler\n");
327 return ret;
328 }
329
330 atmel_pwm_calculate_cdty(state, clkrate, cprd, pres, &cdty);
331
332 if (cstate.enabled) {
333 atmel_pwm_disable(chip, pwm, false);
334 } else {
335 ret = clk_enable(atmel_pwm->clk);
336 if (ret) {
337 dev_err(chip->dev, "failed to enable clock\n");
338 return ret;
339 }
340 }
341
342 /* It is necessary to preserve CPOL, inside CMR */
343 val = atmel_pwm_ch_readl(atmel_pwm, pwm->hwpwm, PWM_CMR);
344 val = (val & ~PWM_CMR_CPRE_MSK) | (pres & PWM_CMR_CPRE_MSK);
345 if (state->polarity == PWM_POLARITY_NORMAL)
346 val &= ~PWM_CMR_CPOL;
347 else
348 val |= PWM_CMR_CPOL;
349 atmel_pwm_ch_writel(atmel_pwm, pwm->hwpwm, PWM_CMR, val);
350 atmel_pwm_set_cprd_cdty(chip, pwm, cprd, cdty);
351 atmel_pwm_writel(atmel_pwm, PWM_ENA, 1 << pwm->hwpwm);
352 } else if (cstate.enabled) {
353 atmel_pwm_disable(chip, pwm, true);
354 }
355
356 return 0;
357}
358
359static int atmel_pwm_get_state(struct pwm_chip *chip, struct pwm_device *pwm,
360 struct pwm_state *state)
361{
362 struct atmel_pwm_chip *atmel_pwm = to_atmel_pwm_chip(chip);
363 u32 sr, cmr;
364
365 sr = atmel_pwm_readl(atmel_pwm, PWM_SR);
366 cmr = atmel_pwm_ch_readl(atmel_pwm, pwm->hwpwm, PWM_CMR);
367
368 if (sr & (1 << pwm->hwpwm)) {
369 unsigned long rate = clk_get_rate(atmel_pwm->clk);
370 u32 cdty, cprd, pres;
371 u64 tmp;
372
373 pres = cmr & PWM_CMR_CPRE_MSK;
374
375 cprd = atmel_pwm_ch_readl(atmel_pwm, pwm->hwpwm,
376 atmel_pwm->data->regs.period);
377 tmp = (u64)cprd * NSEC_PER_SEC;
378 tmp <<= pres;
379 state->period = DIV64_U64_ROUND_UP(tmp, rate);
380
381 /* Wait for an updated duty_cycle queued in hardware */
382 atmel_pwm_wait_nonpending(atmel_pwm, pwm->hwpwm);
383
384 cdty = atmel_pwm_ch_readl(atmel_pwm, pwm->hwpwm,
385 atmel_pwm->data->regs.duty);
386 tmp = (u64)(cprd - cdty) * NSEC_PER_SEC;
387 tmp <<= pres;
388 state->duty_cycle = DIV64_U64_ROUND_UP(tmp, rate);
389
390 state->enabled = true;
391 } else {
392 state->enabled = false;
393 }
394
395 if (cmr & PWM_CMR_CPOL)
396 state->polarity = PWM_POLARITY_INVERSED;
397 else
398 state->polarity = PWM_POLARITY_NORMAL;
399
400 return 0;
401}
402
403static const struct pwm_ops atmel_pwm_ops = {
404 .apply = atmel_pwm_apply,
405 .get_state = atmel_pwm_get_state,
406 .owner = THIS_MODULE,
407};
408
409static const struct atmel_pwm_data atmel_sam9rl_pwm_data = {
410 .regs = {
411 .period = PWMV1_CPRD,
412 .period_upd = PWMV1_CUPD,
413 .duty = PWMV1_CDTY,
414 .duty_upd = PWMV1_CUPD,
415 },
416 .cfg = {
417 /* 16 bits to keep period and duty. */
418 .period_bits = 16,
419 },
420};
421
422static const struct atmel_pwm_data atmel_sama5_pwm_data = {
423 .regs = {
424 .period = PWMV2_CPRD,
425 .period_upd = PWMV2_CPRDUPD,
426 .duty = PWMV2_CDTY,
427 .duty_upd = PWMV2_CDTYUPD,
428 },
429 .cfg = {
430 /* 16 bits to keep period and duty. */
431 .period_bits = 16,
432 },
433};
434
435static const struct atmel_pwm_data mchp_sam9x60_pwm_data = {
436 .regs = {
437 .period = PWMV1_CPRD,
438 .period_upd = PWMV1_CUPD,
439 .duty = PWMV1_CDTY,
440 .duty_upd = PWMV1_CUPD,
441 },
442 .cfg = {
443 /* 32 bits to keep period and duty. */
444 .period_bits = 32,
445 },
446};
447
448static const struct of_device_id atmel_pwm_dt_ids[] = {
449 {
450 .compatible = "atmel,at91sam9rl-pwm",
451 .data = &atmel_sam9rl_pwm_data,
452 }, {
453 .compatible = "atmel,sama5d3-pwm",
454 .data = &atmel_sama5_pwm_data,
455 }, {
456 .compatible = "atmel,sama5d2-pwm",
457 .data = &atmel_sama5_pwm_data,
458 }, {
459 .compatible = "microchip,sam9x60-pwm",
460 .data = &mchp_sam9x60_pwm_data,
461 }, {
462 /* sentinel */
463 },
464};
465MODULE_DEVICE_TABLE(of, atmel_pwm_dt_ids);
466
467static int atmel_pwm_probe(struct platform_device *pdev)
468{
469 struct atmel_pwm_chip *atmel_pwm;
470 int ret;
471
472 atmel_pwm = devm_kzalloc(&pdev->dev, sizeof(*atmel_pwm), GFP_KERNEL);
473 if (!atmel_pwm)
474 return -ENOMEM;
475
476 atmel_pwm->data = of_device_get_match_data(&pdev->dev);
477
478 atmel_pwm->update_pending = 0;
479 spin_lock_init(&atmel_pwm->lock);
480
481 atmel_pwm->base = devm_platform_ioremap_resource(pdev, 0);
482 if (IS_ERR(atmel_pwm->base))
483 return PTR_ERR(atmel_pwm->base);
484
485 atmel_pwm->clk = devm_clk_get(&pdev->dev, NULL);
486 if (IS_ERR(atmel_pwm->clk))
487 return PTR_ERR(atmel_pwm->clk);
488
489 ret = clk_prepare(atmel_pwm->clk);
490 if (ret) {
491 dev_err(&pdev->dev, "failed to prepare PWM clock\n");
492 return ret;
493 }
494
495 atmel_pwm->chip.dev = &pdev->dev;
496 atmel_pwm->chip.ops = &atmel_pwm_ops;
497 atmel_pwm->chip.npwm = 4;
498
499 ret = pwmchip_add(&atmel_pwm->chip);
500 if (ret < 0) {
501 dev_err(&pdev->dev, "failed to add PWM chip %d\n", ret);
502 goto unprepare_clk;
503 }
504
505 platform_set_drvdata(pdev, atmel_pwm);
506
507 return ret;
508
509unprepare_clk:
510 clk_unprepare(atmel_pwm->clk);
511 return ret;
512}
513
514static int atmel_pwm_remove(struct platform_device *pdev)
515{
516 struct atmel_pwm_chip *atmel_pwm = platform_get_drvdata(pdev);
517
518 pwmchip_remove(&atmel_pwm->chip);
519
520 clk_unprepare(atmel_pwm->clk);
521
522 return 0;
523}
524
525static struct platform_driver atmel_pwm_driver = {
526 .driver = {
527 .name = "atmel-pwm",
528 .of_match_table = of_match_ptr(atmel_pwm_dt_ids),
529 },
530 .probe = atmel_pwm_probe,
531 .remove = atmel_pwm_remove,
532};
533module_platform_driver(atmel_pwm_driver);
534
535MODULE_ALIAS("platform:atmel-pwm");
536MODULE_AUTHOR("Bo Shen <voice.shen@atmel.com>");
537MODULE_DESCRIPTION("Atmel PWM driver");
538MODULE_LICENSE("GPL v2");
1/*
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>
11#include <linux/err.h>
12#include <linux/io.h>
13#include <linux/module.h>
14#include <linux/of.h>
15#include <linux/of_device.h>
16#include <linux/platform_device.h>
17#include <linux/pwm.h>
18#include <linux/slab.h>
19
20/* The following is global registers for PWM controller */
21#define PWM_ENA 0x04
22#define PWM_DIS 0x08
23#define PWM_SR 0x0C
24/* Bit field in SR */
25#define PWM_SR_ALL_CH_ON 0x0F
26
27/* The following register is PWM channel related registers */
28#define PWM_CH_REG_OFFSET 0x200
29#define PWM_CH_REG_SIZE 0x20
30
31#define PWM_CMR 0x0
32/* Bit field in CMR */
33#define PWM_CMR_CPOL (1 << 9)
34#define PWM_CMR_UPD_CDTY (1 << 10)
35#define PWM_CMR_CPRE_MSK 0xF
36
37/* The following registers for PWM v1 */
38#define PWMV1_CDTY 0x04
39#define PWMV1_CPRD 0x08
40#define PWMV1_CUPD 0x10
41
42/* The following registers for PWM v2 */
43#define PWMV2_CDTY 0x04
44#define PWMV2_CDTYUPD 0x08
45#define PWMV2_CPRD 0x0C
46#define PWMV2_CPRDUPD 0x10
47
48/*
49 * Max value for duty and period
50 *
51 * Although the duty and period register is 32 bit,
52 * however only the LSB 16 bits are significant.
53 */
54#define PWM_MAX_DTY 0xFFFF
55#define PWM_MAX_PRD 0xFFFF
56#define PRD_MAX_PRES 10
57
58struct atmel_pwm_chip {
59 struct pwm_chip chip;
60 struct clk *clk;
61 void __iomem *base;
62
63 void (*config)(struct pwm_chip *chip, struct pwm_device *pwm,
64 unsigned long dty, unsigned long prd);
65};
66
67static inline struct atmel_pwm_chip *to_atmel_pwm_chip(struct pwm_chip *chip)
68{
69 return container_of(chip, struct atmel_pwm_chip, chip);
70}
71
72static inline u32 atmel_pwm_readl(struct atmel_pwm_chip *chip,
73 unsigned long offset)
74{
75 return readl_relaxed(chip->base + offset);
76}
77
78static inline void atmel_pwm_writel(struct atmel_pwm_chip *chip,
79 unsigned long offset, unsigned long val)
80{
81 writel_relaxed(val, chip->base + offset);
82}
83
84static inline u32 atmel_pwm_ch_readl(struct atmel_pwm_chip *chip,
85 unsigned int ch, unsigned long offset)
86{
87 unsigned long base = PWM_CH_REG_OFFSET + ch * PWM_CH_REG_SIZE;
88
89 return readl_relaxed(chip->base + base + offset);
90}
91
92static inline void atmel_pwm_ch_writel(struct atmel_pwm_chip *chip,
93 unsigned int ch, unsigned long offset,
94 unsigned long val)
95{
96 unsigned long base = PWM_CH_REG_OFFSET + ch * PWM_CH_REG_SIZE;
97
98 writel_relaxed(val, chip->base + base + offset);
99}
100
101static int atmel_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm,
102 int duty_ns, int period_ns)
103{
104 struct atmel_pwm_chip *atmel_pwm = to_atmel_pwm_chip(chip);
105 unsigned long clk_rate, prd, dty;
106 unsigned long long div;
107 unsigned int pres = 0;
108 u32 val;
109 int ret;
110
111 if (test_bit(PWMF_ENABLED, &pwm->flags) && (period_ns != pwm->period)) {
112 dev_err(chip->dev, "cannot change PWM period while enabled\n");
113 return -EBUSY;
114 }
115
116 clk_rate = clk_get_rate(atmel_pwm->clk);
117 div = clk_rate;
118
119 /* Calculate the period cycles */
120 while (div > PWM_MAX_PRD) {
121 div = clk_rate / (1 << pres);
122 div = div * period_ns;
123 /* 1/Hz = 100000000 ns */
124 do_div(div, 1000000000);
125
126 if (pres++ > PRD_MAX_PRES) {
127 dev_err(chip->dev, "pres exceeds the maximum value\n");
128 return -EINVAL;
129 }
130 }
131
132 /* Calculate the duty cycles */
133 prd = div;
134 div *= duty_ns;
135 do_div(div, period_ns);
136 dty = prd - div;
137
138 ret = clk_enable(atmel_pwm->clk);
139 if (ret) {
140 dev_err(chip->dev, "failed to enable PWM clock\n");
141 return ret;
142 }
143
144 /* It is necessary to preserve CPOL, inside CMR */
145 val = atmel_pwm_ch_readl(atmel_pwm, pwm->hwpwm, PWM_CMR);
146 val = (val & ~PWM_CMR_CPRE_MSK) | (pres & PWM_CMR_CPRE_MSK);
147 atmel_pwm_ch_writel(atmel_pwm, pwm->hwpwm, PWM_CMR, val);
148 atmel_pwm->config(chip, pwm, dty, prd);
149
150 clk_disable(atmel_pwm->clk);
151 return ret;
152}
153
154static void atmel_pwm_config_v1(struct pwm_chip *chip, struct pwm_device *pwm,
155 unsigned long dty, unsigned long prd)
156{
157 struct atmel_pwm_chip *atmel_pwm = to_atmel_pwm_chip(chip);
158 unsigned int val;
159
160 if (test_bit(PWMF_ENABLED, &pwm->flags)) {
161 /*
162 * If the PWM channel is enabled, using the update register,
163 * it needs to set bit 10 of CMR to 0
164 */
165 atmel_pwm_ch_writel(atmel_pwm, pwm->hwpwm, PWMV1_CUPD, dty);
166
167 val = atmel_pwm_ch_readl(atmel_pwm, pwm->hwpwm, PWM_CMR);
168 val &= ~PWM_CMR_UPD_CDTY;
169 atmel_pwm_ch_writel(atmel_pwm, pwm->hwpwm, PWM_CMR, val);
170 } else {
171 /*
172 * If the PWM channel is disabled, write value to duty and
173 * period registers directly.
174 */
175 atmel_pwm_ch_writel(atmel_pwm, pwm->hwpwm, PWMV1_CDTY, dty);
176 atmel_pwm_ch_writel(atmel_pwm, pwm->hwpwm, PWMV1_CPRD, prd);
177 }
178}
179
180static void atmel_pwm_config_v2(struct pwm_chip *chip, struct pwm_device *pwm,
181 unsigned long dty, unsigned long prd)
182{
183 struct atmel_pwm_chip *atmel_pwm = to_atmel_pwm_chip(chip);
184
185 if (test_bit(PWMF_ENABLED, &pwm->flags)) {
186 /*
187 * If the PWM channel is enabled, using the duty update register
188 * to update the value.
189 */
190 atmel_pwm_ch_writel(atmel_pwm, pwm->hwpwm, PWMV2_CDTYUPD, dty);
191 } else {
192 /*
193 * If the PWM channel is disabled, write value to duty and
194 * period registers directly.
195 */
196 atmel_pwm_ch_writel(atmel_pwm, pwm->hwpwm, PWMV2_CDTY, dty);
197 atmel_pwm_ch_writel(atmel_pwm, pwm->hwpwm, PWMV2_CPRD, prd);
198 }
199}
200
201static int atmel_pwm_set_polarity(struct pwm_chip *chip, struct pwm_device *pwm,
202 enum pwm_polarity polarity)
203{
204 struct atmel_pwm_chip *atmel_pwm = to_atmel_pwm_chip(chip);
205 u32 val;
206 int ret;
207
208 val = atmel_pwm_ch_readl(atmel_pwm, pwm->hwpwm, PWM_CMR);
209
210 if (polarity == PWM_POLARITY_NORMAL)
211 val &= ~PWM_CMR_CPOL;
212 else
213 val |= PWM_CMR_CPOL;
214
215 ret = clk_enable(atmel_pwm->clk);
216 if (ret) {
217 dev_err(chip->dev, "failed to enable PWM clock\n");
218 return ret;
219 }
220
221 atmel_pwm_ch_writel(atmel_pwm, pwm->hwpwm, PWM_CMR, val);
222
223 clk_disable(atmel_pwm->clk);
224
225 return 0;
226}
227
228static int atmel_pwm_enable(struct pwm_chip *chip, struct pwm_device *pwm)
229{
230 struct atmel_pwm_chip *atmel_pwm = to_atmel_pwm_chip(chip);
231 int ret;
232
233 ret = clk_enable(atmel_pwm->clk);
234 if (ret) {
235 dev_err(chip->dev, "failed to enable PWM clock\n");
236 return ret;
237 }
238
239 atmel_pwm_writel(atmel_pwm, PWM_ENA, 1 << pwm->hwpwm);
240
241 return 0;
242}
243
244static void atmel_pwm_disable(struct pwm_chip *chip, struct pwm_device *pwm)
245{
246 struct atmel_pwm_chip *atmel_pwm = to_atmel_pwm_chip(chip);
247
248 atmel_pwm_writel(atmel_pwm, PWM_DIS, 1 << pwm->hwpwm);
249
250 clk_disable(atmel_pwm->clk);
251}
252
253static const struct pwm_ops atmel_pwm_ops = {
254 .config = atmel_pwm_config,
255 .set_polarity = atmel_pwm_set_polarity,
256 .enable = atmel_pwm_enable,
257 .disable = atmel_pwm_disable,
258 .owner = THIS_MODULE,
259};
260
261struct atmel_pwm_data {
262 void (*config)(struct pwm_chip *chip, struct pwm_device *pwm,
263 unsigned long dty, unsigned long prd);
264};
265
266static const struct atmel_pwm_data atmel_pwm_data_v1 = {
267 .config = atmel_pwm_config_v1,
268};
269
270static const struct atmel_pwm_data atmel_pwm_data_v2 = {
271 .config = atmel_pwm_config_v2,
272};
273
274static const struct platform_device_id atmel_pwm_devtypes[] = {
275 {
276 .name = "at91sam9rl-pwm",
277 .driver_data = (kernel_ulong_t)&atmel_pwm_data_v1,
278 }, {
279 .name = "sama5d3-pwm",
280 .driver_data = (kernel_ulong_t)&atmel_pwm_data_v2,
281 }, {
282 /* sentinel */
283 },
284};
285MODULE_DEVICE_TABLE(platform, atmel_pwm_devtypes);
286
287static const struct of_device_id atmel_pwm_dt_ids[] = {
288 {
289 .compatible = "atmel,at91sam9rl-pwm",
290 .data = &atmel_pwm_data_v1,
291 }, {
292 .compatible = "atmel,sama5d3-pwm",
293 .data = &atmel_pwm_data_v2,
294 }, {
295 /* sentinel */
296 },
297};
298MODULE_DEVICE_TABLE(of, atmel_pwm_dt_ids);
299
300static inline const struct atmel_pwm_data *
301atmel_pwm_get_driver_data(struct platform_device *pdev)
302{
303 if (pdev->dev.of_node) {
304 const struct of_device_id *match;
305
306 match = of_match_device(atmel_pwm_dt_ids, &pdev->dev);
307 if (!match)
308 return NULL;
309
310 return match->data;
311 } else {
312 const struct platform_device_id *id;
313
314 id = platform_get_device_id(pdev);
315
316 return (struct atmel_pwm_data *)id->driver_data;
317 }
318}
319
320static int atmel_pwm_probe(struct platform_device *pdev)
321{
322 const struct atmel_pwm_data *data;
323 struct atmel_pwm_chip *atmel_pwm;
324 struct resource *res;
325 int ret;
326
327 data = atmel_pwm_get_driver_data(pdev);
328 if (!data)
329 return -ENODEV;
330
331 atmel_pwm = devm_kzalloc(&pdev->dev, sizeof(*atmel_pwm), GFP_KERNEL);
332 if (!atmel_pwm)
333 return -ENOMEM;
334
335 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
336 atmel_pwm->base = devm_ioremap_resource(&pdev->dev, res);
337 if (IS_ERR(atmel_pwm->base))
338 return PTR_ERR(atmel_pwm->base);
339
340 atmel_pwm->clk = devm_clk_get(&pdev->dev, NULL);
341 if (IS_ERR(atmel_pwm->clk))
342 return PTR_ERR(atmel_pwm->clk);
343
344 ret = clk_prepare(atmel_pwm->clk);
345 if (ret) {
346 dev_err(&pdev->dev, "failed to prepare PWM clock\n");
347 return ret;
348 }
349
350 atmel_pwm->chip.dev = &pdev->dev;
351 atmel_pwm->chip.ops = &atmel_pwm_ops;
352
353 if (pdev->dev.of_node) {
354 atmel_pwm->chip.of_xlate = of_pwm_xlate_with_flags;
355 atmel_pwm->chip.of_pwm_n_cells = 3;
356 }
357
358 atmel_pwm->chip.base = -1;
359 atmel_pwm->chip.npwm = 4;
360 atmel_pwm->config = data->config;
361
362 ret = pwmchip_add(&atmel_pwm->chip);
363 if (ret < 0) {
364 dev_err(&pdev->dev, "failed to add PWM chip %d\n", ret);
365 goto unprepare_clk;
366 }
367
368 platform_set_drvdata(pdev, atmel_pwm);
369
370 return ret;
371
372unprepare_clk:
373 clk_unprepare(atmel_pwm->clk);
374 return ret;
375}
376
377static int atmel_pwm_remove(struct platform_device *pdev)
378{
379 struct atmel_pwm_chip *atmel_pwm = platform_get_drvdata(pdev);
380
381 clk_unprepare(atmel_pwm->clk);
382
383 return pwmchip_remove(&atmel_pwm->chip);
384}
385
386static struct platform_driver atmel_pwm_driver = {
387 .driver = {
388 .name = "atmel-pwm",
389 .of_match_table = of_match_ptr(atmel_pwm_dt_ids),
390 },
391 .id_table = atmel_pwm_devtypes,
392 .probe = atmel_pwm_probe,
393 .remove = atmel_pwm_remove,
394};
395module_platform_driver(atmel_pwm_driver);
396
397MODULE_ALIAS("platform:atmel-pwm");
398MODULE_AUTHOR("Bo Shen <voice.shen@atmel.com>");
399MODULE_DESCRIPTION("Atmel PWM driver");
400MODULE_LICENSE("GPL v2");