1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) STMicroelectronics 2016
5 * Author: Gerald Baeza <gerald.baeza@st.com>
7 * Inspired by timer-stm32.c from Maxime Coquelin
8 * pwm-atmel.c from Bo Shen
11 #include <linux/bitfield.h>
12 #include <linux/mfd/stm32-timers.h>
13 #include <linux/module.h>
15 #include <linux/pinctrl/consumer.h>
16 #include <linux/platform_device.h>
17 #include <linux/pwm.h>
19 #define CCMR_CHANNEL_SHIFT 8
20 #define CCMR_CHANNEL_MASK 0xFF
21 #define MAX_BREAKINPUT 2
23 struct stm32_breakinput
{
30 struct mutex lock
; /* protect pwm config/enable */
32 struct regmap
*regmap
;
34 bool have_complementary_output
;
35 struct stm32_breakinput breakinputs
[MAX_BREAKINPUT
];
36 unsigned int num_breakinputs
;
37 u32 capture
[4] ____cacheline_aligned
; /* DMA'able buffer */
40 static inline struct stm32_pwm
*to_stm32_pwm_dev(struct pwm_chip
*chip
)
42 return pwmchip_get_drvdata(chip
);
45 static u32
active_channels(struct stm32_pwm
*dev
)
49 regmap_read(dev
->regmap
, TIM_CCER
, &ccer
);
51 return ccer
& TIM_CCER_CCXE
;
54 struct stm32_pwm_waveform
{
61 static int stm32_pwm_round_waveform_tohw(struct pwm_chip
*chip
,
62 struct pwm_device
*pwm
,
63 const struct pwm_waveform
*wf
,
66 struct stm32_pwm_waveform
*wfhw
= _wfhw
;
67 struct stm32_pwm
*priv
= to_stm32_pwm_dev(chip
);
68 unsigned int ch
= pwm
->hwpwm
;
73 if (wf
->period_length_ns
== 0) {
74 *wfhw
= (struct stm32_pwm_waveform
){
81 ret
= clk_enable(priv
->clk
);
85 wfhw
->ccer
= TIM_CCER_CCxE(ch
+ 1);
86 if (priv
->have_complementary_output
)
87 wfhw
->ccer
= TIM_CCER_CCxNE(ch
+ 1);
89 rate
= clk_get_rate(priv
->clk
);
91 if (active_channels(priv
) & ~(1 << ch
* 4)) {
95 * Other channels are already enabled, so the configured PSC and
96 * ARR must be used for this channel, too.
98 ret
= regmap_read(priv
->regmap
, TIM_PSC
, &wfhw
->psc
);
102 ret
= regmap_read(priv
->regmap
, TIM_ARR
, &wfhw
->arr
);
107 * calculate the best value for ARR for the given PSC, refuse if
108 * the resulting period gets bigger than the requested one.
110 arr
= mul_u64_u64_div_u64(wf
->period_length_ns
, rate
,
111 (u64
)NSEC_PER_SEC
* (wfhw
->psc
+ 1));
112 if (arr
<= wfhw
->arr
) {
114 * requested period is small than the currently
115 * configured and unchangable period, report back the smallest
116 * possible period, i.e. the current state; Initialize
117 * ccr to anything valid.
126 * .probe() asserted that clk_get_rate() is not bigger than 1 GHz, so
127 * the calculations here won't overflow.
128 * First we need to find the minimal value for prescaler such that
130 * period_ns * clkrate
131 * ------------------------------ < max_arr + 1
132 * NSEC_PER_SEC * (prescaler + 1)
134 * This equation is equivalent to
136 * period_ns * clkrate
137 * ---------------------------- < prescaler + 1
138 * NSEC_PER_SEC * (max_arr + 1)
140 * Using integer division and knowing that the right hand side is
141 * integer, this is further equivalent to
143 * (period_ns * clkrate) // (NSEC_PER_SEC * (max_arr + 1)) ≤ prescaler
145 u64 psc
= mul_u64_u64_div_u64(wf
->period_length_ns
, rate
,
146 (u64
)NSEC_PER_SEC
* ((u64
)priv
->max_arr
+ 1));
149 wfhw
->psc
= min_t(u64
, psc
, MAX_TIM_PSC
);
151 arr
= mul_u64_u64_div_u64(wf
->period_length_ns
, rate
,
152 (u64
)NSEC_PER_SEC
* (wfhw
->psc
+ 1));
155 * requested period is too small, report back the smallest
156 * possible period, i.e. ARR = 0. The only valid CCR
157 * value is then zero, too.
166 * ARR is limited intentionally to values less than
167 * priv->max_arr to allow 100% duty cycle.
169 wfhw
->arr
= min_t(u64
, arr
, priv
->max_arr
) - 1;
172 duty
= mul_u64_u64_div_u64(wf
->duty_length_ns
, rate
,
173 (u64
)NSEC_PER_SEC
* (wfhw
->psc
+ 1));
174 duty
= min_t(u64
, duty
, wfhw
->arr
+ 1);
176 if (wf
->duty_length_ns
&& wf
->duty_offset_ns
&&
177 wf
->duty_length_ns
+ wf
->duty_offset_ns
>= wf
->period_length_ns
) {
178 wfhw
->ccer
|= TIM_CCER_CCxP(ch
+ 1);
179 if (priv
->have_complementary_output
)
180 wfhw
->ccer
|= TIM_CCER_CCxNP(ch
+ 1);
182 ccr
= wfhw
->arr
+ 1 - duty
;
187 wfhw
->ccr
= min_t(u64
, ccr
, wfhw
->arr
+ 1);
189 dev_dbg(&chip
->dev
, "pwm#%u: %lld/%lld [+%lld] @%lu -> CCER: %08x, PSC: %08x, ARR: %08x, CCR: %08x\n",
190 pwm
->hwpwm
, wf
->duty_length_ns
, wf
->period_length_ns
, wf
->duty_offset_ns
,
191 rate
, wfhw
->ccer
, wfhw
->psc
, wfhw
->arr
, wfhw
->ccr
);
194 clk_disable(priv
->clk
);
200 * This should be moved to lib/math/div64.c. Currently there are some changes
201 * pending to mul_u64_u64_div_u64. Uwe will care for that when the dust settles.
203 static u64
stm32_pwm_mul_u64_u64_div_u64_roundup(u64 a
, u64 b
, u64 c
)
205 u64 res
= mul_u64_u64_div_u64(a
, b
, c
);
206 /* Those multiplications might overflow but it doesn't matter */
207 u64 rem
= a
* b
- c
* res
;
215 static int stm32_pwm_round_waveform_fromhw(struct pwm_chip
*chip
,
216 struct pwm_device
*pwm
,
218 struct pwm_waveform
*wf
)
220 const struct stm32_pwm_waveform
*wfhw
= _wfhw
;
221 struct stm32_pwm
*priv
= to_stm32_pwm_dev(chip
);
222 unsigned int ch
= pwm
->hwpwm
;
224 if (wfhw
->ccer
& TIM_CCER_CCxE(ch
+ 1)) {
225 unsigned long rate
= clk_get_rate(priv
->clk
);
228 /* The result doesn't overflow for rate >= 15259 */
229 wf
->period_length_ns
= stm32_pwm_mul_u64_u64_div_u64_roundup(((u64
)wfhw
->psc
+ 1) * (wfhw
->arr
+ 1),
232 ccr_ns
= stm32_pwm_mul_u64_u64_div_u64_roundup(((u64
)wfhw
->psc
+ 1) * wfhw
->ccr
,
235 if (wfhw
->ccer
& TIM_CCER_CCxP(ch
+ 1)) {
237 stm32_pwm_mul_u64_u64_div_u64_roundup(((u64
)wfhw
->psc
+ 1) * (wfhw
->arr
+ 1 - wfhw
->ccr
),
240 wf
->duty_offset_ns
= ccr_ns
;
242 wf
->duty_length_ns
= ccr_ns
;
243 wf
->duty_offset_ns
= 0;
246 dev_dbg(&chip
->dev
, "pwm#%u: CCER: %08x, PSC: %08x, ARR: %08x, CCR: %08x @%lu -> %lld/%lld [+%lld]\n",
247 pwm
->hwpwm
, wfhw
->ccer
, wfhw
->psc
, wfhw
->arr
, wfhw
->ccr
, rate
,
248 wf
->duty_length_ns
, wf
->period_length_ns
, wf
->duty_offset_ns
);
251 *wf
= (struct pwm_waveform
){
252 .period_length_ns
= 0,
259 static int stm32_pwm_read_waveform(struct pwm_chip
*chip
,
260 struct pwm_device
*pwm
,
263 struct stm32_pwm_waveform
*wfhw
= _wfhw
;
264 struct stm32_pwm
*priv
= to_stm32_pwm_dev(chip
);
265 unsigned int ch
= pwm
->hwpwm
;
268 ret
= clk_enable(priv
->clk
);
272 ret
= regmap_read(priv
->regmap
, TIM_CCER
, &wfhw
->ccer
);
276 if (wfhw
->ccer
& TIM_CCER_CCxE(ch
+ 1)) {
277 ret
= regmap_read(priv
->regmap
, TIM_PSC
, &wfhw
->psc
);
281 ret
= regmap_read(priv
->regmap
, TIM_ARR
, &wfhw
->arr
);
285 if (wfhw
->arr
== U32_MAX
)
288 ret
= regmap_read(priv
->regmap
, TIM_CCRx(ch
+ 1), &wfhw
->ccr
);
292 if (wfhw
->ccr
> wfhw
->arr
+ 1)
293 wfhw
->ccr
= wfhw
->arr
+ 1;
297 clk_disable(priv
->clk
);
302 static int stm32_pwm_write_waveform(struct pwm_chip
*chip
,
303 struct pwm_device
*pwm
,
306 const struct stm32_pwm_waveform
*wfhw
= _wfhw
;
307 struct stm32_pwm
*priv
= to_stm32_pwm_dev(chip
);
308 unsigned int ch
= pwm
->hwpwm
;
311 ret
= clk_enable(priv
->clk
);
315 if (wfhw
->ccer
& TIM_CCER_CCxE(ch
+ 1)) {
320 ret
= regmap_read(priv
->regmap
, TIM_CCER
, &ccer
);
324 /* If there are other channels enabled, don't update PSC and ARR */
325 if (ccer
& ~TIM_CCER_CCxE(ch
+ 1) & TIM_CCER_CCXE
) {
328 ret
= regmap_read(priv
->regmap
, TIM_PSC
, &psc
);
332 if (psc
!= wfhw
->psc
) {
337 ret
= regmap_read(priv
->regmap
, TIM_ARR
, &arr
);
341 if (arr
!= wfhw
->arr
) {
346 ret
= regmap_write(priv
->regmap
, TIM_PSC
, wfhw
->psc
);
350 ret
= regmap_write(priv
->regmap
, TIM_ARR
, wfhw
->arr
);
354 ret
= regmap_set_bits(priv
->regmap
, TIM_CR1
, TIM_CR1_ARPE
);
361 mask
= TIM_CCER_CCxP(ch
+ 1) | TIM_CCER_CCxNP(ch
+ 1);
362 ret
= regmap_update_bits(priv
->regmap
, TIM_CCER
, mask
, wfhw
->ccer
);
366 ret
= regmap_write(priv
->regmap
, TIM_CCRx(ch
+ 1), wfhw
->ccr
);
370 /* Configure output mode */
371 shift
= (ch
& 0x1) * CCMR_CHANNEL_SHIFT
;
372 ccmr
= (TIM_CCMR_PE
| TIM_CCMR_M1
) << shift
;
373 mask
= CCMR_CHANNEL_MASK
<< shift
;
376 ret
= regmap_update_bits(priv
->regmap
, TIM_CCMR1
, mask
, ccmr
);
378 ret
= regmap_update_bits(priv
->regmap
, TIM_CCMR2
, mask
, ccmr
);
382 ret
= regmap_set_bits(priv
->regmap
, TIM_BDTR
, TIM_BDTR_MOE
);
386 if (!(ccer
& TIM_CCER_CCxE(ch
+ 1))) {
387 mask
= TIM_CCER_CCxE(ch
+ 1) | TIM_CCER_CCxNE(ch
+ 1);
389 ret
= clk_enable(priv
->clk
);
393 ccer
= (ccer
& ~mask
) | (wfhw
->ccer
& mask
);
394 regmap_write(priv
->regmap
, TIM_CCER
, ccer
);
396 /* Make sure that registers are updated */
397 regmap_set_bits(priv
->regmap
, TIM_EGR
, TIM_EGR_UG
);
399 /* Enable controller */
400 regmap_set_bits(priv
->regmap
, TIM_CR1
, TIM_CR1_CEN
);
404 /* disable channel */
407 mask
= TIM_CCER_CCxE(ch
+ 1);
408 if (priv
->have_complementary_output
)
409 mask
|= TIM_CCER_CCxNE(ch
+ 1);
411 ret
= regmap_read(priv
->regmap
, TIM_CCER
, &ccer
);
418 ret
= regmap_write(priv
->regmap
, TIM_CCER
, ccer
);
422 if (!(ccer
& TIM_CCER_CCXE
)) {
423 /* When all channels are disabled, we can disable the controller */
424 ret
= regmap_clear_bits(priv
->regmap
, TIM_CR1
, TIM_CR1_CEN
);
429 clk_disable(priv
->clk
);
434 clk_disable(priv
->clk
);
439 #define TIM_CCER_CC12P (TIM_CCER_CC1P | TIM_CCER_CC2P)
440 #define TIM_CCER_CC12E (TIM_CCER_CC1E | TIM_CCER_CC2E)
441 #define TIM_CCER_CC34P (TIM_CCER_CC3P | TIM_CCER_CC4P)
442 #define TIM_CCER_CC34E (TIM_CCER_CC3E | TIM_CCER_CC4E)
445 * Capture using PWM input mode:
447 * TI[1, 2, 3 or 4]: ........._| |________|
454 * COUNTER: ______XXXXX . . . |_XXX
459 * CCR1/CCR3: tx..........t0...........t2
460 * CCR2/CCR4: tx..............t1.........
462 * DMA burst transfer: | |
464 * DMA buffer: { t0, tx } { t2, t1 }
467 * 0: IC1/3 snapchot on rising edge: counter value -> CCR1/CCR3
468 * + DMA transfer CCR[1/3] & CCR[2/4] values (t0, tx: doesn't care)
469 * 1: IC2/4 snapchot on falling edge: counter value -> CCR2/CCR4
470 * 2: IC1/3 snapchot on rising edge: counter value -> CCR1/CCR3
471 * + DMA transfer CCR[1/3] & CCR[2/4] values (t2, t1)
475 * - Duty cycle = t1 - t0
477 static int stm32_pwm_raw_capture(struct pwm_chip
*chip
, struct pwm_device
*pwm
,
478 unsigned long tmo_ms
, u32
*raw_prd
,
481 struct stm32_pwm
*priv
= to_stm32_pwm_dev(chip
);
482 struct device
*parent
= pwmchip_parent(chip
)->parent
;
483 enum stm32_timers_dmas dma_id
;
487 /* Ensure registers have been updated, enable counter and capture */
488 regmap_set_bits(priv
->regmap
, TIM_EGR
, TIM_EGR_UG
);
489 regmap_set_bits(priv
->regmap
, TIM_CR1
, TIM_CR1_CEN
);
491 /* Use cc1 or cc3 DMA resp for PWM input channels 1 & 2 or 3 & 4 */
492 dma_id
= pwm
->hwpwm
< 2 ? STM32_TIMERS_DMA_CH1
: STM32_TIMERS_DMA_CH3
;
493 ccen
= pwm
->hwpwm
< 2 ? TIM_CCER_CC12E
: TIM_CCER_CC34E
;
494 ccr
= pwm
->hwpwm
< 2 ? TIM_CCR1
: TIM_CCR3
;
495 regmap_set_bits(priv
->regmap
, TIM_CCER
, ccen
);
498 * Timer DMA burst mode. Request 2 registers, 2 bursts, to get both
499 * CCR1 & CCR2 (or CCR3 & CCR4) on each capture event.
500 * We'll get two capture snapchots: { CCR1, CCR2 }, { CCR1, CCR2 }
501 * or { CCR3, CCR4 }, { CCR3, CCR4 }
503 ret
= stm32_timers_dma_burst_read(parent
, priv
->capture
, dma_id
, ccr
, 2,
508 /* Period: t2 - t0 (take care of counter overflow) */
509 if (priv
->capture
[0] <= priv
->capture
[2])
510 *raw_prd
= priv
->capture
[2] - priv
->capture
[0];
512 *raw_prd
= priv
->max_arr
- priv
->capture
[0] + priv
->capture
[2];
514 /* Duty cycle capture requires at least two capture units */
515 if (pwm
->chip
->npwm
< 2)
517 else if (priv
->capture
[0] <= priv
->capture
[3])
518 *raw_dty
= priv
->capture
[3] - priv
->capture
[0];
520 *raw_dty
= priv
->max_arr
- priv
->capture
[0] + priv
->capture
[3];
522 if (*raw_dty
> *raw_prd
) {
524 * Race beetween PWM input and DMA: it may happen
525 * falling edge triggers new capture on TI2/4 before DMA
526 * had a chance to read CCR2/4. It means capture[1]
527 * contains period + duty_cycle. So, subtract period.
529 *raw_dty
-= *raw_prd
;
533 regmap_clear_bits(priv
->regmap
, TIM_CCER
, ccen
);
534 regmap_clear_bits(priv
->regmap
, TIM_CR1
, TIM_CR1_CEN
);
539 static int stm32_pwm_capture(struct pwm_chip
*chip
, struct pwm_device
*pwm
,
540 struct pwm_capture
*result
, unsigned long tmo_ms
)
542 struct stm32_pwm
*priv
= to_stm32_pwm_dev(chip
);
543 unsigned long long prd
, div
, dty
;
545 unsigned int psc
= 0, icpsc
, scale
;
546 u32 raw_prd
= 0, raw_dty
= 0;
549 mutex_lock(&priv
->lock
);
551 if (active_channels(priv
)) {
556 ret
= clk_enable(priv
->clk
);
558 dev_err(pwmchip_parent(chip
), "failed to enable counter clock\n");
562 rate
= clk_get_rate(priv
->clk
);
568 /* prescaler: fit timeout window provided by upper layer */
569 div
= (unsigned long long)rate
* (unsigned long long)tmo_ms
;
570 do_div(div
, MSEC_PER_SEC
);
572 while ((div
> priv
->max_arr
) && (psc
< MAX_TIM_PSC
)) {
575 do_div(div
, psc
+ 1);
577 regmap_write(priv
->regmap
, TIM_ARR
, priv
->max_arr
);
578 regmap_write(priv
->regmap
, TIM_PSC
, psc
);
580 /* Reset input selector to its default input and disable slave mode */
581 regmap_write(priv
->regmap
, TIM_TISEL
, 0x0);
582 regmap_write(priv
->regmap
, TIM_SMCR
, 0x0);
584 /* Map TI1 or TI2 PWM input to IC1 & IC2 (or TI3/4 to IC3 & IC4) */
585 regmap_update_bits(priv
->regmap
,
586 pwm
->hwpwm
< 2 ? TIM_CCMR1
: TIM_CCMR2
,
587 TIM_CCMR_CC1S
| TIM_CCMR_CC2S
, pwm
->hwpwm
& 0x1 ?
588 TIM_CCMR_CC1S_TI2
| TIM_CCMR_CC2S_TI2
:
589 TIM_CCMR_CC1S_TI1
| TIM_CCMR_CC2S_TI1
);
591 /* Capture period on IC1/3 rising edge, duty cycle on IC2/4 falling. */
592 regmap_update_bits(priv
->regmap
, TIM_CCER
, pwm
->hwpwm
< 2 ?
593 TIM_CCER_CC12P
: TIM_CCER_CC34P
, pwm
->hwpwm
< 2 ?
594 TIM_CCER_CC2P
: TIM_CCER_CC4P
);
596 ret
= stm32_pwm_raw_capture(chip
, pwm
, tmo_ms
, &raw_prd
, &raw_dty
);
601 * Got a capture. Try to improve accuracy at high rates:
602 * - decrease counter clock prescaler, scale up to max rate.
603 * - use input prescaler, capture once every /2 /4 or /8 edges.
606 u32 max_arr
= priv
->max_arr
- 0x1000; /* arbitrary margin */
608 scale
= max_arr
/ min(max_arr
, raw_prd
);
610 scale
= priv
->max_arr
; /* below resolution, use max scale */
613 if (psc
&& scale
> 1) {
614 /* 2nd measure with new scale */
616 regmap_write(priv
->regmap
, TIM_PSC
, psc
);
617 ret
= stm32_pwm_raw_capture(chip
, pwm
, tmo_ms
, &raw_prd
,
623 /* Compute intermediate period not to exceed timeout at low rates */
624 prd
= (unsigned long long)raw_prd
* (psc
+ 1) * NSEC_PER_SEC
;
627 for (icpsc
= 0; icpsc
< MAX_TIM_ICPSC
; icpsc
++) {
628 /* input prescaler: also keep arbitrary margin */
629 if (raw_prd
>= (priv
->max_arr
- 0x1000) >> (icpsc
+ 1))
631 if (prd
>= (tmo_ms
* NSEC_PER_MSEC
) >> (icpsc
+ 2))
638 /* Last chance to improve period accuracy, using input prescaler */
639 regmap_update_bits(priv
->regmap
,
640 pwm
->hwpwm
< 2 ? TIM_CCMR1
: TIM_CCMR2
,
641 TIM_CCMR_IC1PSC
| TIM_CCMR_IC2PSC
,
642 FIELD_PREP(TIM_CCMR_IC1PSC
, icpsc
) |
643 FIELD_PREP(TIM_CCMR_IC2PSC
, icpsc
));
645 ret
= stm32_pwm_raw_capture(chip
, pwm
, tmo_ms
, &raw_prd
, &raw_dty
);
649 if (raw_dty
>= (raw_prd
>> icpsc
)) {
651 * We may fall here using input prescaler, when input
652 * capture starts on high side (before falling edge).
653 * Example with icpsc to capture on each 4 events:
655 * start 1st capture 2nd capture
657 * ___ _____ _____ _____ _____ ____
658 * TI1..4 |__| |__| |__| |__| |__|
660 * icpsc1/3: . 0 . 1 . 2 . 3 . 0
661 * icpsc2/4: 0 1 2 3 0
663 * CCR1/3 ......t0..............................t2
664 * CCR2/4 ..t1..............................t1'...
666 * Capture0: .<----------------------------->.
667 * Capture1: .<-------------------------->. .
669 * Period: .<------> . .
673 * - Period = Capture0 / icpsc
674 * - Duty = Period - Low side = Period - (Capture0 - Capture1)
676 raw_dty
= (raw_prd
>> icpsc
) - (raw_prd
- raw_dty
);
680 prd
= (unsigned long long)raw_prd
* (psc
+ 1) * NSEC_PER_SEC
;
681 result
->period
= DIV_ROUND_UP_ULL(prd
, rate
<< icpsc
);
682 dty
= (unsigned long long)raw_dty
* (psc
+ 1) * NSEC_PER_SEC
;
683 result
->duty_cycle
= DIV_ROUND_UP_ULL(dty
, rate
);
685 regmap_write(priv
->regmap
, TIM_CCER
, 0);
686 regmap_write(priv
->regmap
, pwm
->hwpwm
< 2 ? TIM_CCMR1
: TIM_CCMR2
, 0);
687 regmap_write(priv
->regmap
, TIM_PSC
, 0);
689 clk_disable(priv
->clk
);
691 mutex_unlock(&priv
->lock
);
696 static const struct pwm_ops stm32pwm_ops
= {
697 .sizeof_wfhw
= sizeof(struct stm32_pwm_waveform
),
698 .round_waveform_tohw
= stm32_pwm_round_waveform_tohw
,
699 .round_waveform_fromhw
= stm32_pwm_round_waveform_fromhw
,
700 .read_waveform
= stm32_pwm_read_waveform
,
701 .write_waveform
= stm32_pwm_write_waveform
,
703 .capture
= IS_ENABLED(CONFIG_DMA_ENGINE
) ? stm32_pwm_capture
: NULL
,
706 static int stm32_pwm_set_breakinput(struct stm32_pwm
*priv
,
707 const struct stm32_breakinput
*bi
)
709 u32 shift
= TIM_BDTR_BKF_SHIFT(bi
->index
);
710 u32 bke
= TIM_BDTR_BKE(bi
->index
);
711 u32 bkp
= TIM_BDTR_BKP(bi
->index
);
712 u32 bkf
= TIM_BDTR_BKF(bi
->index
);
713 u32 mask
= bkf
| bkp
| bke
;
716 bdtr
= (bi
->filter
& TIM_BDTR_BKF_MASK
) << shift
| bke
;
721 regmap_update_bits(priv
->regmap
, TIM_BDTR
, mask
, bdtr
);
723 regmap_read(priv
->regmap
, TIM_BDTR
, &bdtr
);
725 return (bdtr
& bke
) ? 0 : -EINVAL
;
728 static int stm32_pwm_apply_breakinputs(struct stm32_pwm
*priv
)
733 for (i
= 0; i
< priv
->num_breakinputs
; i
++) {
734 ret
= stm32_pwm_set_breakinput(priv
, &priv
->breakinputs
[i
]);
742 static int stm32_pwm_probe_breakinputs(struct stm32_pwm
*priv
,
743 struct device_node
*np
)
745 int nb
, ret
, array_size
;
748 nb
= of_property_count_elems_of_size(np
, "st,breakinput",
749 sizeof(struct stm32_breakinput
));
752 * Because "st,breakinput" parameter is optional do not make probe
753 * failed if it doesn't exist.
758 if (nb
> MAX_BREAKINPUT
)
761 priv
->num_breakinputs
= nb
;
762 array_size
= nb
* sizeof(struct stm32_breakinput
) / sizeof(u32
);
763 ret
= of_property_read_u32_array(np
, "st,breakinput",
764 (u32
*)priv
->breakinputs
, array_size
);
768 for (i
= 0; i
< priv
->num_breakinputs
; i
++) {
769 if (priv
->breakinputs
[i
].index
> 1 ||
770 priv
->breakinputs
[i
].level
> 1 ||
771 priv
->breakinputs
[i
].filter
> 15)
775 return stm32_pwm_apply_breakinputs(priv
);
778 static void stm32_pwm_detect_complementary(struct stm32_pwm
*priv
)
783 * If complementary bit doesn't exist writing 1 will have no
784 * effect so we can detect it.
786 regmap_set_bits(priv
->regmap
, TIM_CCER
, TIM_CCER_CC1NE
);
787 regmap_read(priv
->regmap
, TIM_CCER
, &ccer
);
788 regmap_clear_bits(priv
->regmap
, TIM_CCER
, TIM_CCER_CC1NE
);
790 priv
->have_complementary_output
= (ccer
!= 0);
793 static unsigned int stm32_pwm_detect_channels(struct regmap
*regmap
,
794 unsigned int *num_enabled
)
796 u32 ccer
, ccer_backup
;
799 * If channels enable bits don't exist writing 1 will have no
800 * effect so we can detect and count them.
802 regmap_read(regmap
, TIM_CCER
, &ccer_backup
);
803 regmap_set_bits(regmap
, TIM_CCER
, TIM_CCER_CCXE
);
804 regmap_read(regmap
, TIM_CCER
, &ccer
);
805 regmap_write(regmap
, TIM_CCER
, ccer_backup
);
807 *num_enabled
= hweight32(ccer_backup
& TIM_CCER_CCXE
);
809 return hweight32(ccer
& TIM_CCER_CCXE
);
812 static int stm32_pwm_probe(struct platform_device
*pdev
)
814 struct device
*dev
= &pdev
->dev
;
815 struct device_node
*np
= dev
->of_node
;
816 struct stm32_timers
*ddata
= dev_get_drvdata(pdev
->dev
.parent
);
817 struct pwm_chip
*chip
;
818 struct stm32_pwm
*priv
;
819 unsigned int npwm
, num_enabled
;
823 npwm
= stm32_pwm_detect_channels(ddata
->regmap
, &num_enabled
);
825 chip
= devm_pwmchip_alloc(dev
, npwm
, sizeof(*priv
));
827 return PTR_ERR(chip
);
828 priv
= to_stm32_pwm_dev(chip
);
830 mutex_init(&priv
->lock
);
831 priv
->regmap
= ddata
->regmap
;
832 priv
->clk
= ddata
->clk
;
833 priv
->max_arr
= ddata
->max_arr
;
835 if (!priv
->regmap
|| !priv
->clk
)
836 return dev_err_probe(dev
, -EINVAL
, "Failed to get %s\n",
837 priv
->regmap
? "clk" : "regmap");
839 ret
= stm32_pwm_probe_breakinputs(priv
, np
);
841 return dev_err_probe(dev
, ret
,
842 "Failed to configure breakinputs\n");
844 stm32_pwm_detect_complementary(priv
);
846 ret
= devm_clk_rate_exclusive_get(dev
, priv
->clk
);
848 return dev_err_probe(dev
, ret
, "Failed to lock clock\n");
851 * With the clk running with not more than 1 GHz the calculations in
852 * .apply() won't overflow.
854 if (clk_get_rate(priv
->clk
) > 1000000000)
855 return dev_err_probe(dev
, -EINVAL
, "Clock freq too high (%lu)\n",
856 clk_get_rate(priv
->clk
));
858 chip
->ops
= &stm32pwm_ops
;
860 /* Initialize clock refcount to number of enabled PWM channels. */
861 for (i
= 0; i
< num_enabled
; i
++)
862 clk_enable(priv
->clk
);
864 ret
= devm_pwmchip_add(dev
, chip
);
866 return dev_err_probe(dev
, ret
,
867 "Failed to register pwmchip\n");
869 platform_set_drvdata(pdev
, chip
);
874 static int stm32_pwm_suspend(struct device
*dev
)
876 struct pwm_chip
*chip
= dev_get_drvdata(dev
);
877 struct stm32_pwm
*priv
= to_stm32_pwm_dev(chip
);
881 /* Look for active channels */
882 ccer
= active_channels(priv
);
884 for (i
= 0; i
< chip
->npwm
; i
++) {
885 mask
= TIM_CCER_CCxE(i
+ 1);
887 dev_err(dev
, "PWM %u still in use by consumer %s\n",
888 i
, chip
->pwms
[i
].label
);
893 return pinctrl_pm_select_sleep_state(dev
);
896 static int stm32_pwm_resume(struct device
*dev
)
898 struct pwm_chip
*chip
= dev_get_drvdata(dev
);
899 struct stm32_pwm
*priv
= to_stm32_pwm_dev(chip
);
902 ret
= pinctrl_pm_select_default_state(dev
);
906 /* restore breakinput registers that may have been lost in low power */
907 return stm32_pwm_apply_breakinputs(priv
);
910 static DEFINE_SIMPLE_DEV_PM_OPS(stm32_pwm_pm_ops
, stm32_pwm_suspend
, stm32_pwm_resume
);
912 static const struct of_device_id stm32_pwm_of_match
[] = {
913 { .compatible
= "st,stm32-pwm", },
916 MODULE_DEVICE_TABLE(of
, stm32_pwm_of_match
);
918 static struct platform_driver stm32_pwm_driver
= {
919 .probe
= stm32_pwm_probe
,
922 .of_match_table
= stm32_pwm_of_match
,
923 .pm
= pm_ptr(&stm32_pwm_pm_ops
),
926 module_platform_driver(stm32_pwm_driver
);
928 MODULE_ALIAS("platform:stm32-pwm");
929 MODULE_DESCRIPTION("STMicroelectronics STM32 PWM driver");
930 MODULE_LICENSE("GPL v2");