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/platform_device.h>
29 #include <linux/pwm.h>
30 #include <linux/slab.h>
32 /* The following is global registers for PWM controller */
38 #define PWM_SR_ALL_CH_MASK 0x0F
40 /* The following register is PWM channel related registers */
41 #define PWM_CH_REG_OFFSET 0x200
42 #define PWM_CH_REG_SIZE 0x20
45 /* Bit field in CMR */
46 #define PWM_CMR_CPOL (1 << 9)
47 #define PWM_CMR_UPD_CDTY (1 << 10)
48 #define PWM_CMR_CPRE_MSK 0xF
50 /* The following registers for PWM v1 */
51 #define PWMV1_CDTY 0x04
52 #define PWMV1_CPRD 0x08
53 #define PWMV1_CUPD 0x10
55 /* The following registers for PWM v2 */
56 #define PWMV2_CDTY 0x04
57 #define PWMV2_CDTYUPD 0x08
58 #define PWMV2_CPRD 0x0C
59 #define PWMV2_CPRDUPD 0x10
61 #define PWM_MAX_PRES 10
63 struct atmel_pwm_registers
{
70 struct atmel_pwm_config
{
74 struct atmel_pwm_data
{
75 struct atmel_pwm_registers regs
;
76 struct atmel_pwm_config cfg
;
79 struct atmel_pwm_chip
{
82 const struct atmel_pwm_data
*data
;
85 * The hardware supports a mechanism to update a channel's duty cycle at
86 * the end of the currently running period. When such an update is
87 * pending we delay disabling the PWM until the new configuration is
88 * active because otherwise pmw_config(duty_cycle=0); pwm_disable();
89 * might not result in an inactive output.
90 * This bitmask tracks for which channels an update is pending in
95 /* Protects .update_pending */
99 static inline struct atmel_pwm_chip
*to_atmel_pwm_chip(struct pwm_chip
*chip
)
101 return pwmchip_get_drvdata(chip
);
104 static inline u32
atmel_pwm_readl(struct atmel_pwm_chip
*chip
,
105 unsigned long offset
)
107 return readl_relaxed(chip
->base
+ offset
);
110 static inline void atmel_pwm_writel(struct atmel_pwm_chip
*chip
,
111 unsigned long offset
, unsigned long val
)
113 writel_relaxed(val
, chip
->base
+ offset
);
116 static inline u32
atmel_pwm_ch_readl(struct atmel_pwm_chip
*chip
,
117 unsigned int ch
, unsigned long offset
)
119 unsigned long base
= PWM_CH_REG_OFFSET
+ ch
* PWM_CH_REG_SIZE
;
121 return atmel_pwm_readl(chip
, base
+ offset
);
124 static inline void atmel_pwm_ch_writel(struct atmel_pwm_chip
*chip
,
125 unsigned int ch
, unsigned long offset
,
128 unsigned long base
= PWM_CH_REG_OFFSET
+ ch
* PWM_CH_REG_SIZE
;
130 atmel_pwm_writel(chip
, base
+ offset
, val
);
133 static void atmel_pwm_update_pending(struct atmel_pwm_chip
*chip
)
136 * Each channel that has its bit in ISR set started a new period since
137 * ISR was cleared and so there is no more update pending. Note that
138 * reading ISR clears it, so this needs to handle all channels to not
141 u32 isr
= atmel_pwm_readl(chip
, PWM_ISR
);
143 chip
->update_pending
&= ~isr
;
146 static void atmel_pwm_set_pending(struct atmel_pwm_chip
*chip
, unsigned int ch
)
148 spin_lock(&chip
->lock
);
151 * Clear pending flags in hardware because otherwise there might still
152 * be a stale flag in ISR.
154 atmel_pwm_update_pending(chip
);
156 chip
->update_pending
|= (1 << ch
);
158 spin_unlock(&chip
->lock
);
161 static int atmel_pwm_test_pending(struct atmel_pwm_chip
*chip
, unsigned int ch
)
165 spin_lock(&chip
->lock
);
167 if (chip
->update_pending
& (1 << ch
)) {
168 atmel_pwm_update_pending(chip
);
170 if (chip
->update_pending
& (1 << ch
))
174 spin_unlock(&chip
->lock
);
179 static int atmel_pwm_wait_nonpending(struct atmel_pwm_chip
*chip
, unsigned int ch
)
181 unsigned long timeout
= jiffies
+ 2 * HZ
;
184 while ((ret
= atmel_pwm_test_pending(chip
, ch
)) &&
185 time_before(jiffies
, timeout
))
186 usleep_range(10, 100);
188 return ret
? -ETIMEDOUT
: 0;
191 static int atmel_pwm_calculate_cprd_and_pres(struct pwm_chip
*chip
,
192 unsigned long clkrate
,
193 const struct pwm_state
*state
,
194 unsigned long *cprd
, u32
*pres
)
196 struct atmel_pwm_chip
*atmel_pwm
= to_atmel_pwm_chip(chip
);
197 unsigned long long cycles
= state
->period
;
200 /* Calculate the period cycles and prescale value */
202 do_div(cycles
, NSEC_PER_SEC
);
205 * The register for the period length is cfg.period_bits bits wide.
206 * So for each bit the number of clock cycles is wider divide the input
207 * clock frequency by two using pres and shift cprd accordingly.
209 shift
= fls(cycles
) - atmel_pwm
->data
->cfg
.period_bits
;
211 if (shift
> PWM_MAX_PRES
) {
212 dev_err(pwmchip_parent(chip
), "pres exceeds the maximum value\n");
214 } else if (shift
> 0) {
226 static void atmel_pwm_calculate_cdty(const struct pwm_state
*state
,
227 unsigned long clkrate
, unsigned long cprd
,
228 u32 pres
, unsigned long *cdty
)
230 unsigned long long cycles
= state
->duty_cycle
;
233 do_div(cycles
, NSEC_PER_SEC
);
235 *cdty
= cprd
- cycles
;
238 static void atmel_pwm_update_cdty(struct pwm_chip
*chip
, struct pwm_device
*pwm
,
241 struct atmel_pwm_chip
*atmel_pwm
= to_atmel_pwm_chip(chip
);
244 if (atmel_pwm
->data
->regs
.duty_upd
==
245 atmel_pwm
->data
->regs
.period_upd
) {
246 val
= atmel_pwm_ch_readl(atmel_pwm
, pwm
->hwpwm
, PWM_CMR
);
247 val
&= ~PWM_CMR_UPD_CDTY
;
248 atmel_pwm_ch_writel(atmel_pwm
, pwm
->hwpwm
, PWM_CMR
, val
);
251 atmel_pwm_ch_writel(atmel_pwm
, pwm
->hwpwm
,
252 atmel_pwm
->data
->regs
.duty_upd
, cdty
);
253 atmel_pwm_set_pending(atmel_pwm
, pwm
->hwpwm
);
256 static void atmel_pwm_set_cprd_cdty(struct pwm_chip
*chip
,
257 struct pwm_device
*pwm
,
258 unsigned long cprd
, unsigned long cdty
)
260 struct atmel_pwm_chip
*atmel_pwm
= to_atmel_pwm_chip(chip
);
262 atmel_pwm_ch_writel(atmel_pwm
, pwm
->hwpwm
,
263 atmel_pwm
->data
->regs
.duty
, cdty
);
264 atmel_pwm_ch_writel(atmel_pwm
, pwm
->hwpwm
,
265 atmel_pwm
->data
->regs
.period
, cprd
);
268 static void atmel_pwm_disable(struct pwm_chip
*chip
, struct pwm_device
*pwm
,
271 struct atmel_pwm_chip
*atmel_pwm
= to_atmel_pwm_chip(chip
);
272 unsigned long timeout
;
274 atmel_pwm_wait_nonpending(atmel_pwm
, pwm
->hwpwm
);
276 atmel_pwm_writel(atmel_pwm
, PWM_DIS
, 1 << pwm
->hwpwm
);
279 * Wait for the PWM channel disable operation to be effective before
280 * stopping the clock.
282 timeout
= jiffies
+ 2 * HZ
;
284 while ((atmel_pwm_readl(atmel_pwm
, PWM_SR
) & (1 << pwm
->hwpwm
)) &&
285 time_before(jiffies
, timeout
))
286 usleep_range(10, 100);
289 clk_disable(atmel_pwm
->clk
);
292 static int atmel_pwm_apply(struct pwm_chip
*chip
, struct pwm_device
*pwm
,
293 const struct pwm_state
*state
)
295 struct atmel_pwm_chip
*atmel_pwm
= to_atmel_pwm_chip(chip
);
296 unsigned long cprd
, cdty
;
300 if (state
->enabled
) {
301 unsigned long clkrate
= clk_get_rate(atmel_pwm
->clk
);
303 if (pwm
->state
.enabled
&&
304 pwm
->state
.polarity
== state
->polarity
&&
305 pwm
->state
.period
== state
->period
) {
306 u32 cmr
= atmel_pwm_ch_readl(atmel_pwm
, pwm
->hwpwm
, PWM_CMR
);
308 cprd
= atmel_pwm_ch_readl(atmel_pwm
, pwm
->hwpwm
,
309 atmel_pwm
->data
->regs
.period
);
310 pres
= cmr
& PWM_CMR_CPRE_MSK
;
312 atmel_pwm_calculate_cdty(state
, clkrate
, cprd
, pres
, &cdty
);
313 atmel_pwm_update_cdty(chip
, pwm
, cdty
);
317 ret
= atmel_pwm_calculate_cprd_and_pres(chip
, clkrate
, state
, &cprd
,
320 dev_err(pwmchip_parent(chip
),
321 "failed to calculate cprd and prescaler\n");
325 atmel_pwm_calculate_cdty(state
, clkrate
, cprd
, pres
, &cdty
);
327 if (pwm
->state
.enabled
) {
328 atmel_pwm_disable(chip
, pwm
, false);
330 ret
= clk_enable(atmel_pwm
->clk
);
332 dev_err(pwmchip_parent(chip
), "failed to enable clock\n");
337 /* It is necessary to preserve CPOL, inside CMR */
338 val
= atmel_pwm_ch_readl(atmel_pwm
, pwm
->hwpwm
, PWM_CMR
);
339 val
= (val
& ~PWM_CMR_CPRE_MSK
) | (pres
& PWM_CMR_CPRE_MSK
);
340 if (state
->polarity
== PWM_POLARITY_NORMAL
)
341 val
&= ~PWM_CMR_CPOL
;
344 atmel_pwm_ch_writel(atmel_pwm
, pwm
->hwpwm
, PWM_CMR
, val
);
345 atmel_pwm_set_cprd_cdty(chip
, pwm
, cprd
, cdty
);
346 atmel_pwm_writel(atmel_pwm
, PWM_ENA
, 1 << pwm
->hwpwm
);
347 } else if (pwm
->state
.enabled
) {
348 atmel_pwm_disable(chip
, pwm
, true);
354 static int atmel_pwm_get_state(struct pwm_chip
*chip
, struct pwm_device
*pwm
,
355 struct pwm_state
*state
)
357 struct atmel_pwm_chip
*atmel_pwm
= to_atmel_pwm_chip(chip
);
360 sr
= atmel_pwm_readl(atmel_pwm
, PWM_SR
);
361 cmr
= atmel_pwm_ch_readl(atmel_pwm
, pwm
->hwpwm
, PWM_CMR
);
363 if (sr
& (1 << pwm
->hwpwm
)) {
364 unsigned long rate
= clk_get_rate(atmel_pwm
->clk
);
365 u32 cdty
, cprd
, pres
;
368 pres
= cmr
& PWM_CMR_CPRE_MSK
;
370 cprd
= atmel_pwm_ch_readl(atmel_pwm
, pwm
->hwpwm
,
371 atmel_pwm
->data
->regs
.period
);
372 tmp
= (u64
)cprd
* NSEC_PER_SEC
;
374 state
->period
= DIV64_U64_ROUND_UP(tmp
, rate
);
376 /* Wait for an updated duty_cycle queued in hardware */
377 atmel_pwm_wait_nonpending(atmel_pwm
, pwm
->hwpwm
);
379 cdty
= atmel_pwm_ch_readl(atmel_pwm
, pwm
->hwpwm
,
380 atmel_pwm
->data
->regs
.duty
);
381 tmp
= (u64
)(cprd
- cdty
) * NSEC_PER_SEC
;
383 state
->duty_cycle
= DIV64_U64_ROUND_UP(tmp
, rate
);
385 state
->enabled
= true;
387 state
->enabled
= false;
390 if (cmr
& PWM_CMR_CPOL
)
391 state
->polarity
= PWM_POLARITY_INVERSED
;
393 state
->polarity
= PWM_POLARITY_NORMAL
;
398 static const struct pwm_ops atmel_pwm_ops
= {
399 .apply
= atmel_pwm_apply
,
400 .get_state
= atmel_pwm_get_state
,
403 static const struct atmel_pwm_data atmel_sam9rl_pwm_data
= {
405 .period
= PWMV1_CPRD
,
406 .period_upd
= PWMV1_CUPD
,
408 .duty_upd
= PWMV1_CUPD
,
411 /* 16 bits to keep period and duty. */
416 static const struct atmel_pwm_data atmel_sama5_pwm_data
= {
418 .period
= PWMV2_CPRD
,
419 .period_upd
= PWMV2_CPRDUPD
,
421 .duty_upd
= PWMV2_CDTYUPD
,
424 /* 16 bits to keep period and duty. */
429 static const struct atmel_pwm_data mchp_sam9x60_pwm_data
= {
431 .period
= PWMV1_CPRD
,
432 .period_upd
= PWMV1_CUPD
,
434 .duty_upd
= PWMV1_CUPD
,
437 /* 32 bits to keep period and duty. */
442 static const struct of_device_id atmel_pwm_dt_ids
[] = {
444 .compatible
= "atmel,at91sam9rl-pwm",
445 .data
= &atmel_sam9rl_pwm_data
,
447 .compatible
= "atmel,sama5d3-pwm",
448 .data
= &atmel_sama5_pwm_data
,
450 .compatible
= "atmel,sama5d2-pwm",
451 .data
= &atmel_sama5_pwm_data
,
453 .compatible
= "microchip,sam9x60-pwm",
454 .data
= &mchp_sam9x60_pwm_data
,
459 MODULE_DEVICE_TABLE(of
, atmel_pwm_dt_ids
);
461 static int atmel_pwm_enable_clk_if_on(struct pwm_chip
*chip
, bool on
)
463 struct atmel_pwm_chip
*atmel_pwm
= to_atmel_pwm_chip(chip
);
464 unsigned int i
, cnt
= 0;
468 sr
= atmel_pwm_readl(atmel_pwm
, PWM_SR
) & PWM_SR_ALL_CH_MASK
;
472 cnt
= bitmap_weight(&sr
, chip
->npwm
);
477 for (i
= 0; i
< cnt
; i
++) {
478 ret
= clk_enable(atmel_pwm
->clk
);
480 dev_err(pwmchip_parent(chip
),
481 "failed to enable clock for pwm %pe\n",
493 clk_disable(atmel_pwm
->clk
);
498 static int atmel_pwm_probe(struct platform_device
*pdev
)
500 struct atmel_pwm_chip
*atmel_pwm
;
501 struct pwm_chip
*chip
;
504 chip
= devm_pwmchip_alloc(&pdev
->dev
, 4, sizeof(*atmel_pwm
));
506 return PTR_ERR(chip
);
508 atmel_pwm
= to_atmel_pwm_chip(chip
);
509 atmel_pwm
->data
= of_device_get_match_data(&pdev
->dev
);
511 atmel_pwm
->update_pending
= 0;
512 spin_lock_init(&atmel_pwm
->lock
);
514 atmel_pwm
->base
= devm_platform_ioremap_resource(pdev
, 0);
515 if (IS_ERR(atmel_pwm
->base
))
516 return PTR_ERR(atmel_pwm
->base
);
518 atmel_pwm
->clk
= devm_clk_get_prepared(&pdev
->dev
, NULL
);
519 if (IS_ERR(atmel_pwm
->clk
))
520 return dev_err_probe(&pdev
->dev
, PTR_ERR(atmel_pwm
->clk
),
521 "failed to get prepared PWM clock\n");
523 chip
->ops
= &atmel_pwm_ops
;
525 ret
= atmel_pwm_enable_clk_if_on(chip
, true);
529 ret
= devm_pwmchip_add(&pdev
->dev
, chip
);
531 dev_err_probe(&pdev
->dev
, ret
, "failed to add PWM chip\n");
538 atmel_pwm_enable_clk_if_on(chip
, false);
543 static struct platform_driver atmel_pwm_driver
= {
546 .of_match_table
= atmel_pwm_dt_ids
,
548 .probe
= atmel_pwm_probe
,
550 module_platform_driver(atmel_pwm_driver
);
552 MODULE_ALIAS("platform:atmel-pwm");
553 MODULE_AUTHOR("Bo Shen <voice.shen@atmel.com>");
554 MODULE_DESCRIPTION("Atmel PWM driver");
555 MODULE_LICENSE("GPL v2");