1 // SPDX-License-Identifier: GPL-2.0-only
3 * Driver for Atmel Pulse Width Modulation Controller
5 * Copyright (C) 2013 Atmel Corporation
6 * Bo Shen <voice.shen@atmel.com>
8 * Links to reference manuals for the supported PWM chips can be found in
9 * Documentation/arm/microchip.rst.
12 * - Periods start with the inactive level.
13 * - Hardware has to be stopped in general to update settings.
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.
22 #include <linux/clk.h>
23 #include <linux/delay.h>
24 #include <linux/err.h>
26 #include <linux/module.h>
27 #include <linux/mutex.h>
29 #include <linux/of_device.h>
30 #include <linux/platform_device.h>
31 #include <linux/pwm.h>
32 #include <linux/slab.h>
34 /* The following is global registers for PWM controller */
40 #define PWM_SR_ALL_CH_ON 0x0F
42 /* The following register is PWM channel related registers */
43 #define PWM_CH_REG_OFFSET 0x200
44 #define PWM_CH_REG_SIZE 0x20
47 /* Bit field in CMR */
48 #define PWM_CMR_CPOL (1 << 9)
49 #define PWM_CMR_UPD_CDTY (1 << 10)
50 #define PWM_CMR_CPRE_MSK 0xF
52 /* The following registers for PWM v1 */
53 #define PWMV1_CDTY 0x04
54 #define PWMV1_CPRD 0x08
55 #define PWMV1_CUPD 0x10
57 /* The following registers for PWM v2 */
58 #define PWMV2_CDTY 0x04
59 #define PWMV2_CDTYUPD 0x08
60 #define PWMV2_CPRD 0x0C
61 #define PWMV2_CPRDUPD 0x10
63 #define PWM_MAX_PRES 10
65 struct atmel_pwm_registers {
72 struct atmel_pwm_config {
76 struct atmel_pwm_data {
77 struct atmel_pwm_registers regs;
78 struct atmel_pwm_config cfg;
81 struct atmel_pwm_chip {
85 const struct atmel_pwm_data *data;
88 * The hardware supports a mechanism to update a channel's duty cycle at
89 * the end of the currently running period. When such an update is
90 * pending we delay disabling the PWM until the new configuration is
91 * active because otherwise pmw_config(duty_cycle=0); pwm_disable();
92 * might not result in an inactive output.
93 * This bitmask tracks for which channels an update is pending in
98 /* Protects .update_pending */
102 static inline struct atmel_pwm_chip *to_atmel_pwm_chip(struct pwm_chip *chip)
104 return container_of(chip, struct atmel_pwm_chip, chip);
107 static inline u32 atmel_pwm_readl(struct atmel_pwm_chip *chip,
108 unsigned long offset)
110 return readl_relaxed(chip->base + offset);
113 static inline void atmel_pwm_writel(struct atmel_pwm_chip *chip,
114 unsigned long offset, unsigned long val)
116 writel_relaxed(val, chip->base + offset);
119 static inline u32 atmel_pwm_ch_readl(struct atmel_pwm_chip *chip,
120 unsigned int ch, unsigned long offset)
122 unsigned long base = PWM_CH_REG_OFFSET + ch * PWM_CH_REG_SIZE;
124 return atmel_pwm_readl(chip, base + offset);
127 static inline void atmel_pwm_ch_writel(struct atmel_pwm_chip *chip,
128 unsigned int ch, unsigned long offset,
131 unsigned long base = PWM_CH_REG_OFFSET + ch * PWM_CH_REG_SIZE;
133 atmel_pwm_writel(chip, base + offset, val);
136 static void atmel_pwm_update_pending(struct atmel_pwm_chip *chip)
139 * Each channel that has its bit in ISR set started a new period since
140 * ISR was cleared and so there is no more update pending. Note that
141 * reading ISR clears it, so this needs to handle all channels to not
144 u32 isr = atmel_pwm_readl(chip, PWM_ISR);
146 chip->update_pending &= ~isr;
149 static void atmel_pwm_set_pending(struct atmel_pwm_chip *chip, unsigned int ch)
151 spin_lock(&chip->lock);
154 * Clear pending flags in hardware because otherwise there might still
155 * be a stale flag in ISR.
157 atmel_pwm_update_pending(chip);
159 chip->update_pending |= (1 << ch);
161 spin_unlock(&chip->lock);
164 static int atmel_pwm_test_pending(struct atmel_pwm_chip *chip, unsigned int ch)
168 spin_lock(&chip->lock);
170 if (chip->update_pending & (1 << ch)) {
171 atmel_pwm_update_pending(chip);
173 if (chip->update_pending & (1 << ch))
177 spin_unlock(&chip->lock);
182 static int atmel_pwm_wait_nonpending(struct atmel_pwm_chip *chip, unsigned int ch)
184 unsigned long timeout = jiffies + 2 * HZ;
187 while ((ret = atmel_pwm_test_pending(chip, ch)) &&
188 time_before(jiffies, timeout))
189 usleep_range(10, 100);
191 return ret ? -ETIMEDOUT : 0;
194 static int atmel_pwm_calculate_cprd_and_pres(struct pwm_chip *chip,
195 unsigned long clkrate,
196 const struct pwm_state *state,
197 unsigned long *cprd, u32 *pres)
199 struct atmel_pwm_chip *atmel_pwm = to_atmel_pwm_chip(chip);
200 unsigned long long cycles = state->period;
203 /* Calculate the period cycles and prescale value */
205 do_div(cycles, NSEC_PER_SEC);
208 * The register for the period length is cfg.period_bits bits wide.
209 * So for each bit the number of clock cycles is wider divide the input
210 * clock frequency by two using pres and shift cprd accordingly.
212 shift = fls(cycles) - atmel_pwm->data->cfg.period_bits;
214 if (shift > PWM_MAX_PRES) {
215 dev_err(chip->dev, "pres exceeds the maximum value\n");
217 } else if (shift > 0) {
229 static void atmel_pwm_calculate_cdty(const struct pwm_state *state,
230 unsigned long clkrate, unsigned long cprd,
231 u32 pres, unsigned long *cdty)
233 unsigned long long cycles = state->duty_cycle;
236 do_div(cycles, NSEC_PER_SEC);
238 *cdty = cprd - cycles;
241 static void atmel_pwm_update_cdty(struct pwm_chip *chip, struct pwm_device *pwm,
244 struct atmel_pwm_chip *atmel_pwm = to_atmel_pwm_chip(chip);
247 if (atmel_pwm->data->regs.duty_upd ==
248 atmel_pwm->data->regs.period_upd) {
249 val = atmel_pwm_ch_readl(atmel_pwm, pwm->hwpwm, PWM_CMR);
250 val &= ~PWM_CMR_UPD_CDTY;
251 atmel_pwm_ch_writel(atmel_pwm, pwm->hwpwm, PWM_CMR, val);
254 atmel_pwm_ch_writel(atmel_pwm, pwm->hwpwm,
255 atmel_pwm->data->regs.duty_upd, cdty);
256 atmel_pwm_set_pending(atmel_pwm, pwm->hwpwm);
259 static void atmel_pwm_set_cprd_cdty(struct pwm_chip *chip,
260 struct pwm_device *pwm,
261 unsigned long cprd, unsigned long cdty)
263 struct atmel_pwm_chip *atmel_pwm = to_atmel_pwm_chip(chip);
265 atmel_pwm_ch_writel(atmel_pwm, pwm->hwpwm,
266 atmel_pwm->data->regs.duty, cdty);
267 atmel_pwm_ch_writel(atmel_pwm, pwm->hwpwm,
268 atmel_pwm->data->regs.period, cprd);
271 static void atmel_pwm_disable(struct pwm_chip *chip, struct pwm_device *pwm,
274 struct atmel_pwm_chip *atmel_pwm = to_atmel_pwm_chip(chip);
275 unsigned long timeout = jiffies + 2 * HZ;
277 atmel_pwm_wait_nonpending(atmel_pwm, pwm->hwpwm);
279 atmel_pwm_writel(atmel_pwm, PWM_DIS, 1 << pwm->hwpwm);
282 * Wait for the PWM channel disable operation to be effective before
283 * stopping the clock.
285 timeout = jiffies + 2 * HZ;
287 while ((atmel_pwm_readl(atmel_pwm, PWM_SR) & (1 << pwm->hwpwm)) &&
288 time_before(jiffies, timeout))
289 usleep_range(10, 100);
292 clk_disable(atmel_pwm->clk);
295 static int atmel_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm,
296 const struct pwm_state *state)
298 struct atmel_pwm_chip *atmel_pwm = to_atmel_pwm_chip(chip);
299 struct pwm_state cstate;
300 unsigned long cprd, cdty;
304 pwm_get_state(pwm, &cstate);
306 if (state->enabled) {
307 unsigned long clkrate = clk_get_rate(atmel_pwm->clk);
309 if (cstate.enabled &&
310 cstate.polarity == state->polarity &&
311 cstate.period == state->period) {
312 u32 cmr = atmel_pwm_ch_readl(atmel_pwm, pwm->hwpwm, PWM_CMR);
314 cprd = atmel_pwm_ch_readl(atmel_pwm, pwm->hwpwm,
315 atmel_pwm->data->regs.period);
316 pres = cmr & PWM_CMR_CPRE_MSK;
318 atmel_pwm_calculate_cdty(state, clkrate, cprd, pres, &cdty);
319 atmel_pwm_update_cdty(chip, pwm, cdty);
323 ret = atmel_pwm_calculate_cprd_and_pres(chip, clkrate, state, &cprd,
327 "failed to calculate cprd and prescaler\n");
331 atmel_pwm_calculate_cdty(state, clkrate, cprd, pres, &cdty);
333 if (cstate.enabled) {
334 atmel_pwm_disable(chip, pwm, false);
336 ret = clk_enable(atmel_pwm->clk);
338 dev_err(chip->dev, "failed to enable clock\n");
343 /* It is necessary to preserve CPOL, inside CMR */
344 val = atmel_pwm_ch_readl(atmel_pwm, pwm->hwpwm, PWM_CMR);
345 val = (val & ~PWM_CMR_CPRE_MSK) | (pres & PWM_CMR_CPRE_MSK);
346 if (state->polarity == PWM_POLARITY_NORMAL)
347 val &= ~PWM_CMR_CPOL;
350 atmel_pwm_ch_writel(atmel_pwm, pwm->hwpwm, PWM_CMR, val);
351 atmel_pwm_set_cprd_cdty(chip, pwm, cprd, cdty);
352 atmel_pwm_writel(atmel_pwm, PWM_ENA, 1 << pwm->hwpwm);
353 } else if (cstate.enabled) {
354 atmel_pwm_disable(chip, pwm, true);
360 static void atmel_pwm_get_state(struct pwm_chip *chip, struct pwm_device *pwm,
361 struct pwm_state *state)
363 struct atmel_pwm_chip *atmel_pwm = to_atmel_pwm_chip(chip);
366 sr = atmel_pwm_readl(atmel_pwm, PWM_SR);
367 cmr = atmel_pwm_ch_readl(atmel_pwm, pwm->hwpwm, PWM_CMR);
369 if (sr & (1 << pwm->hwpwm)) {
370 unsigned long rate = clk_get_rate(atmel_pwm->clk);
371 u32 cdty, cprd, pres;
374 pres = cmr & PWM_CMR_CPRE_MSK;
376 cprd = atmel_pwm_ch_readl(atmel_pwm, pwm->hwpwm,
377 atmel_pwm->data->regs.period);
378 tmp = (u64)cprd * NSEC_PER_SEC;
380 state->period = DIV64_U64_ROUND_UP(tmp, rate);
382 /* Wait for an updated duty_cycle queued in hardware */
383 atmel_pwm_wait_nonpending(atmel_pwm, pwm->hwpwm);
385 cdty = atmel_pwm_ch_readl(atmel_pwm, pwm->hwpwm,
386 atmel_pwm->data->regs.duty);
387 tmp = (u64)(cprd - cdty) * NSEC_PER_SEC;
389 state->duty_cycle = DIV64_U64_ROUND_UP(tmp, rate);
391 state->enabled = true;
393 state->enabled = false;
396 if (cmr & PWM_CMR_CPOL)
397 state->polarity = PWM_POLARITY_INVERSED;
399 state->polarity = PWM_POLARITY_NORMAL;
402 static const struct pwm_ops atmel_pwm_ops = {
403 .apply = atmel_pwm_apply,
404 .get_state = atmel_pwm_get_state,
405 .owner = THIS_MODULE,
408 static const struct atmel_pwm_data atmel_sam9rl_pwm_data = {
410 .period = PWMV1_CPRD,
411 .period_upd = PWMV1_CUPD,
413 .duty_upd = PWMV1_CUPD,
416 /* 16 bits to keep period and duty. */
421 static const struct atmel_pwm_data atmel_sama5_pwm_data = {
423 .period = PWMV2_CPRD,
424 .period_upd = PWMV2_CPRDUPD,
426 .duty_upd = PWMV2_CDTYUPD,
429 /* 16 bits to keep period and duty. */
434 static const struct atmel_pwm_data mchp_sam9x60_pwm_data = {
436 .period = PWMV1_CPRD,
437 .period_upd = PWMV1_CUPD,
439 .duty_upd = PWMV1_CUPD,
442 /* 32 bits to keep period and duty. */
447 static const struct of_device_id atmel_pwm_dt_ids[] = {
449 .compatible = "atmel,at91sam9rl-pwm",
450 .data = &atmel_sam9rl_pwm_data,
452 .compatible = "atmel,sama5d3-pwm",
453 .data = &atmel_sama5_pwm_data,
455 .compatible = "atmel,sama5d2-pwm",
456 .data = &atmel_sama5_pwm_data,
458 .compatible = "microchip,sam9x60-pwm",
459 .data = &mchp_sam9x60_pwm_data,
464 MODULE_DEVICE_TABLE(of, atmel_pwm_dt_ids);
466 static int atmel_pwm_probe(struct platform_device *pdev)
468 struct atmel_pwm_chip *atmel_pwm;
471 atmel_pwm = devm_kzalloc(&pdev->dev, sizeof(*atmel_pwm), GFP_KERNEL);
475 atmel_pwm->data = of_device_get_match_data(&pdev->dev);
477 atmel_pwm->update_pending = 0;
478 spin_lock_init(&atmel_pwm->lock);
480 atmel_pwm->base = devm_platform_ioremap_resource(pdev, 0);
481 if (IS_ERR(atmel_pwm->base))
482 return PTR_ERR(atmel_pwm->base);
484 atmel_pwm->clk = devm_clk_get(&pdev->dev, NULL);
485 if (IS_ERR(atmel_pwm->clk))
486 return PTR_ERR(atmel_pwm->clk);
488 ret = clk_prepare(atmel_pwm->clk);
490 dev_err(&pdev->dev, "failed to prepare PWM clock\n");
494 atmel_pwm->chip.dev = &pdev->dev;
495 atmel_pwm->chip.ops = &atmel_pwm_ops;
496 atmel_pwm->chip.npwm = 4;
498 ret = pwmchip_add(&atmel_pwm->chip);
500 dev_err(&pdev->dev, "failed to add PWM chip %d\n", ret);
504 platform_set_drvdata(pdev, atmel_pwm);
509 clk_unprepare(atmel_pwm->clk);
513 static int atmel_pwm_remove(struct platform_device *pdev)
515 struct atmel_pwm_chip *atmel_pwm = platform_get_drvdata(pdev);
517 pwmchip_remove(&atmel_pwm->chip);
519 clk_unprepare(atmel_pwm->clk);
524 static struct platform_driver atmel_pwm_driver = {
527 .of_match_table = of_match_ptr(atmel_pwm_dt_ids),
529 .probe = atmel_pwm_probe,
530 .remove = atmel_pwm_remove,
532 module_platform_driver(atmel_pwm_driver);
534 MODULE_ALIAS("platform:atmel-pwm");
535 MODULE_AUTHOR("Bo Shen <voice.shen@atmel.com>");
536 MODULE_DESCRIPTION("Atmel PWM driver");
537 MODULE_LICENSE("GPL v2");