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/arch/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>
28 #include <linux/of_device.h>
29 #include <linux/platform_device.h>
30 #include <linux/pwm.h>
31 #include <linux/slab.h>
33 /* The following is global registers for PWM controller */
39 #define PWM_SR_ALL_CH_ON 0x0F
41 /* The following register is PWM channel related registers */
42 #define PWM_CH_REG_OFFSET 0x200
43 #define PWM_CH_REG_SIZE 0x20
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
51 /* The following registers for PWM v1 */
52 #define PWMV1_CDTY 0x04
53 #define PWMV1_CPRD 0x08
54 #define PWMV1_CUPD 0x10
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
62 #define PWM_MAX_PRES 10
64 struct atmel_pwm_registers {
71 struct atmel_pwm_config {
75 struct atmel_pwm_data {
76 struct atmel_pwm_registers regs;
77 struct atmel_pwm_config cfg;
80 struct atmel_pwm_chip {
84 const struct atmel_pwm_data *data;
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
97 /* Protects .update_pending */
101 static inline struct atmel_pwm_chip *to_atmel_pwm_chip(struct pwm_chip *chip)
103 return container_of(chip, struct atmel_pwm_chip, chip);
106 static inline u32 atmel_pwm_readl(struct atmel_pwm_chip *chip,
107 unsigned long offset)
109 return readl_relaxed(chip->base + offset);
112 static inline void atmel_pwm_writel(struct atmel_pwm_chip *chip,
113 unsigned long offset, unsigned long val)
115 writel_relaxed(val, chip->base + offset);
118 static inline u32 atmel_pwm_ch_readl(struct atmel_pwm_chip *chip,
119 unsigned int ch, unsigned long offset)
121 unsigned long base = PWM_CH_REG_OFFSET + ch * PWM_CH_REG_SIZE;
123 return atmel_pwm_readl(chip, base + offset);
126 static inline void atmel_pwm_ch_writel(struct atmel_pwm_chip *chip,
127 unsigned int ch, unsigned long offset,
130 unsigned long base = PWM_CH_REG_OFFSET + ch * PWM_CH_REG_SIZE;
132 atmel_pwm_writel(chip, base + offset, val);
135 static void atmel_pwm_update_pending(struct atmel_pwm_chip *chip)
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
143 u32 isr = atmel_pwm_readl(chip, PWM_ISR);
145 chip->update_pending &= ~isr;
148 static void atmel_pwm_set_pending(struct atmel_pwm_chip *chip, unsigned int ch)
150 spin_lock(&chip->lock);
153 * Clear pending flags in hardware because otherwise there might still
154 * be a stale flag in ISR.
156 atmel_pwm_update_pending(chip);
158 chip->update_pending |= (1 << ch);
160 spin_unlock(&chip->lock);
163 static int atmel_pwm_test_pending(struct atmel_pwm_chip *chip, unsigned int ch)
167 spin_lock(&chip->lock);
169 if (chip->update_pending & (1 << ch)) {
170 atmel_pwm_update_pending(chip);
172 if (chip->update_pending & (1 << ch))
176 spin_unlock(&chip->lock);
181 static int atmel_pwm_wait_nonpending(struct atmel_pwm_chip *chip, unsigned int ch)
183 unsigned long timeout = jiffies + 2 * HZ;
186 while ((ret = atmel_pwm_test_pending(chip, ch)) &&
187 time_before(jiffies, timeout))
188 usleep_range(10, 100);
190 return ret ? -ETIMEDOUT : 0;
193 static 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)
198 struct atmel_pwm_chip *atmel_pwm = to_atmel_pwm_chip(chip);
199 unsigned long long cycles = state->period;
202 /* Calculate the period cycles and prescale value */
204 do_div(cycles, NSEC_PER_SEC);
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.
211 shift = fls(cycles) - atmel_pwm->data->cfg.period_bits;
213 if (shift > PWM_MAX_PRES) {
214 dev_err(chip->dev, "pres exceeds the maximum value\n");
216 } else if (shift > 0) {
228 static void atmel_pwm_calculate_cdty(const struct pwm_state *state,
229 unsigned long clkrate, unsigned long cprd,
230 u32 pres, unsigned long *cdty)
232 unsigned long long cycles = state->duty_cycle;
235 do_div(cycles, NSEC_PER_SEC);
237 *cdty = cprd - cycles;
240 static void atmel_pwm_update_cdty(struct pwm_chip *chip, struct pwm_device *pwm,
243 struct atmel_pwm_chip *atmel_pwm = to_atmel_pwm_chip(chip);
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);
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);
258 static void atmel_pwm_set_cprd_cdty(struct pwm_chip *chip,
259 struct pwm_device *pwm,
260 unsigned long cprd, unsigned long cdty)
262 struct atmel_pwm_chip *atmel_pwm = to_atmel_pwm_chip(chip);
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);
270 static void atmel_pwm_disable(struct pwm_chip *chip, struct pwm_device *pwm,
273 struct atmel_pwm_chip *atmel_pwm = to_atmel_pwm_chip(chip);
274 unsigned long timeout;
276 atmel_pwm_wait_nonpending(atmel_pwm, pwm->hwpwm);
278 atmel_pwm_writel(atmel_pwm, PWM_DIS, 1 << pwm->hwpwm);
281 * Wait for the PWM channel disable operation to be effective before
282 * stopping the clock.
284 timeout = jiffies + 2 * HZ;
286 while ((atmel_pwm_readl(atmel_pwm, PWM_SR) & (1 << pwm->hwpwm)) &&
287 time_before(jiffies, timeout))
288 usleep_range(10, 100);
291 clk_disable(atmel_pwm->clk);
294 static int atmel_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm,
295 const struct pwm_state *state)
297 struct atmel_pwm_chip *atmel_pwm = to_atmel_pwm_chip(chip);
298 struct pwm_state cstate;
299 unsigned long cprd, cdty;
303 pwm_get_state(pwm, &cstate);
305 if (state->enabled) {
306 unsigned long clkrate = clk_get_rate(atmel_pwm->clk);
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);
313 cprd = atmel_pwm_ch_readl(atmel_pwm, pwm->hwpwm,
314 atmel_pwm->data->regs.period);
315 pres = cmr & PWM_CMR_CPRE_MSK;
317 atmel_pwm_calculate_cdty(state, clkrate, cprd, pres, &cdty);
318 atmel_pwm_update_cdty(chip, pwm, cdty);
322 ret = atmel_pwm_calculate_cprd_and_pres(chip, clkrate, state, &cprd,
326 "failed to calculate cprd and prescaler\n");
330 atmel_pwm_calculate_cdty(state, clkrate, cprd, pres, &cdty);
332 if (cstate.enabled) {
333 atmel_pwm_disable(chip, pwm, false);
335 ret = clk_enable(atmel_pwm->clk);
337 dev_err(chip->dev, "failed to enable clock\n");
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;
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);
359 static int atmel_pwm_get_state(struct pwm_chip *chip, struct pwm_device *pwm,
360 struct pwm_state *state)
362 struct atmel_pwm_chip *atmel_pwm = to_atmel_pwm_chip(chip);
365 sr = atmel_pwm_readl(atmel_pwm, PWM_SR);
366 cmr = atmel_pwm_ch_readl(atmel_pwm, pwm->hwpwm, PWM_CMR);
368 if (sr & (1 << pwm->hwpwm)) {
369 unsigned long rate = clk_get_rate(atmel_pwm->clk);
370 u32 cdty, cprd, pres;
373 pres = cmr & PWM_CMR_CPRE_MSK;
375 cprd = atmel_pwm_ch_readl(atmel_pwm, pwm->hwpwm,
376 atmel_pwm->data->regs.period);
377 tmp = (u64)cprd * NSEC_PER_SEC;
379 state->period = DIV64_U64_ROUND_UP(tmp, rate);
381 /* Wait for an updated duty_cycle queued in hardware */
382 atmel_pwm_wait_nonpending(atmel_pwm, pwm->hwpwm);
384 cdty = atmel_pwm_ch_readl(atmel_pwm, pwm->hwpwm,
385 atmel_pwm->data->regs.duty);
386 tmp = (u64)(cprd - cdty) * NSEC_PER_SEC;
388 state->duty_cycle = DIV64_U64_ROUND_UP(tmp, rate);
390 state->enabled = true;
392 state->enabled = false;
395 if (cmr & PWM_CMR_CPOL)
396 state->polarity = PWM_POLARITY_INVERSED;
398 state->polarity = PWM_POLARITY_NORMAL;
403 static const struct pwm_ops atmel_pwm_ops = {
404 .apply = atmel_pwm_apply,
405 .get_state = atmel_pwm_get_state,
406 .owner = THIS_MODULE,
409 static const struct atmel_pwm_data atmel_sam9rl_pwm_data = {
411 .period = PWMV1_CPRD,
412 .period_upd = PWMV1_CUPD,
414 .duty_upd = PWMV1_CUPD,
417 /* 16 bits to keep period and duty. */
422 static const struct atmel_pwm_data atmel_sama5_pwm_data = {
424 .period = PWMV2_CPRD,
425 .period_upd = PWMV2_CPRDUPD,
427 .duty_upd = PWMV2_CDTYUPD,
430 /* 16 bits to keep period and duty. */
435 static const struct atmel_pwm_data mchp_sam9x60_pwm_data = {
437 .period = PWMV1_CPRD,
438 .period_upd = PWMV1_CUPD,
440 .duty_upd = PWMV1_CUPD,
443 /* 32 bits to keep period and duty. */
448 static const struct of_device_id atmel_pwm_dt_ids[] = {
450 .compatible = "atmel,at91sam9rl-pwm",
451 .data = &atmel_sam9rl_pwm_data,
453 .compatible = "atmel,sama5d3-pwm",
454 .data = &atmel_sama5_pwm_data,
456 .compatible = "atmel,sama5d2-pwm",
457 .data = &atmel_sama5_pwm_data,
459 .compatible = "microchip,sam9x60-pwm",
460 .data = &mchp_sam9x60_pwm_data,
465 MODULE_DEVICE_TABLE(of, atmel_pwm_dt_ids);
467 static int atmel_pwm_probe(struct platform_device *pdev)
469 struct atmel_pwm_chip *atmel_pwm;
472 atmel_pwm = devm_kzalloc(&pdev->dev, sizeof(*atmel_pwm), GFP_KERNEL);
476 atmel_pwm->data = of_device_get_match_data(&pdev->dev);
478 atmel_pwm->update_pending = 0;
479 spin_lock_init(&atmel_pwm->lock);
481 atmel_pwm->base = devm_platform_ioremap_resource(pdev, 0);
482 if (IS_ERR(atmel_pwm->base))
483 return PTR_ERR(atmel_pwm->base);
485 atmel_pwm->clk = devm_clk_get(&pdev->dev, NULL);
486 if (IS_ERR(atmel_pwm->clk))
487 return PTR_ERR(atmel_pwm->clk);
489 ret = clk_prepare(atmel_pwm->clk);
491 dev_err(&pdev->dev, "failed to prepare PWM clock\n");
495 atmel_pwm->chip.dev = &pdev->dev;
496 atmel_pwm->chip.ops = &atmel_pwm_ops;
497 atmel_pwm->chip.npwm = 4;
499 ret = pwmchip_add(&atmel_pwm->chip);
501 dev_err(&pdev->dev, "failed to add PWM chip %d\n", ret);
505 platform_set_drvdata(pdev, atmel_pwm);
510 clk_unprepare(atmel_pwm->clk);
514 static void atmel_pwm_remove(struct platform_device *pdev)
516 struct atmel_pwm_chip *atmel_pwm = platform_get_drvdata(pdev);
518 pwmchip_remove(&atmel_pwm->chip);
520 clk_unprepare(atmel_pwm->clk);
523 static struct platform_driver atmel_pwm_driver = {
526 .of_match_table = of_match_ptr(atmel_pwm_dt_ids),
528 .probe = atmel_pwm_probe,
529 .remove_new = atmel_pwm_remove,
531 module_platform_driver(atmel_pwm_driver);
533 MODULE_ALIAS("platform:atmel-pwm");
534 MODULE_AUTHOR("Bo Shen <voice.shen@atmel.com>");
535 MODULE_DESCRIPTION("Atmel PWM driver");
536 MODULE_LICENSE("GPL v2");