Skip to content

Commit d9e48dc

Browse files
committed
Merge tag 'pwm/for-5.5-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/thierry.reding/linux-pwm
Pull pwm updates from Thierry Reding: "Various changes and minor fixes across a couple of drivers" * tag 'pwm/for-5.5-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/thierry.reding/linux-pwm: pwm: stm32: Pass breakinput instead of its values pwm: stm32: Remove clutter from ternary operator pwm: stm32: Validate breakinput data from DT pwm: Update comment on struct pwm_ops::apply pwm: sun4i: Fix incorrect calculation of duty_cycle/period pwm: stm32: Add power management support pwm: stm32: Split breakinput apply routine to ease PM support dt-bindings: pwm-stm32: Document pinctrl sleep state pwm: sun4i: Drop redundant assignment to variable pval dt-bindings: pwm: mediatek: Remove gratuitous compatible string for MT7629
2 parents fb3da48 + 9e1b499 commit d9e48dc

5 files changed

Lines changed: 90 additions & 46 deletions

File tree

‎Documentation/devicetree/bindings/pwm/pwm-mediatek.txt‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ Required properties:
66
- "mediatek,mt7622-pwm": found on mt7622 SoC.
77
- "mediatek,mt7623-pwm": found on mt7623 SoC.
88
- "mediatek,mt7628-pwm": found on mt7628 SoC.
9-
- "mediatek,mt7629-pwm", "mediatek,mt7622-pwm": found on mt7629 SoC.
9+
- "mediatek,mt7629-pwm": found on mt7629 SoC.
1010
- "mediatek,mt8516-pwm": found on mt8516 SoC.
1111
- reg: physical base address and length of the controller's registers.
1212
- #pwm-cells: must be 2. See pwm.yaml in this directory for a description of

‎drivers/pwm/pwm-stm32.c‎

Lines changed: 82 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -12,29 +12,32 @@
1212
#include <linux/mfd/stm32-timers.h>
1313
#include <linux/module.h>
1414
#include <linux/of.h>
15+
#include <linux/pinctrl/consumer.h>
1516
#include <linux/platform_device.h>
1617
#include <linux/pwm.h>
1718

1819
#define CCMR_CHANNEL_SHIFT 8
1920
#define CCMR_CHANNEL_MASK 0xFF
2021
#define MAX_BREAKINPUT 2
2122

23+
struct stm32_breakinput {
24+
u32 index;
25+
u32 level;
26+
u32 filter;
27+
};
28+
2229
struct stm32_pwm {
2330
struct pwm_chip chip;
2431
struct mutex lock; /* protect pwm config/enable */
2532
struct clk *clk;
2633
struct regmap *regmap;
2734
u32 max_arr;
2835
bool have_complementary_output;
36+
struct stm32_breakinput breakinputs[MAX_BREAKINPUT];
37+
unsigned int num_breakinputs;
2938
u32 capture[4] ____cacheline_aligned; /* DMA'able buffer */
3039
};
3140

32-
struct stm32_breakinput {
33-
u32 index;
34-
u32 level;
35-
u32 filter;
36-
};
37-
3841
static inline struct stm32_pwm *to_stm32_pwm_dev(struct pwm_chip *chip)
3942
{
4043
return container_of(chip, struct stm32_pwm, chip);
@@ -488,22 +491,19 @@ static const struct pwm_ops stm32pwm_ops = {
488491
};
489492

490493
static int stm32_pwm_set_breakinput(struct stm32_pwm *priv,
491-
int index, int level, int filter)
494+
const struct stm32_breakinput *bi)
492495
{
493-
u32 bke = (index == 0) ? TIM_BDTR_BKE : TIM_BDTR_BK2E;
494-
int shift = (index == 0) ? TIM_BDTR_BKF_SHIFT : TIM_BDTR_BK2F_SHIFT;
495-
u32 mask = (index == 0) ? TIM_BDTR_BKE | TIM_BDTR_BKP | TIM_BDTR_BKF
496-
: TIM_BDTR_BK2E | TIM_BDTR_BK2P | TIM_BDTR_BK2F;
497-
u32 bdtr = bke;
496+
u32 shift = TIM_BDTR_BKF_SHIFT(bi->index);
497+
u32 bke = TIM_BDTR_BKE(bi->index);
498+
u32 bkp = TIM_BDTR_BKP(bi->index);
499+
u32 bkf = TIM_BDTR_BKF(bi->index);
500+
u32 mask = bkf | bkp | bke;
501+
u32 bdtr;
498502

499-
/*
500-
* The both bits could be set since only one will be wrote
501-
* due to mask value.
502-
*/
503-
if (level)
504-
bdtr |= TIM_BDTR_BKP | TIM_BDTR_BK2P;
503+
bdtr = (bi->filter & TIM_BDTR_BKF_MASK) << shift | bke;
505504

506-
bdtr |= (filter & TIM_BDTR_BKF_MASK) << shift;
505+
if (bi->level)
506+
bdtr |= bkp;
507507

508508
regmap_update_bits(priv->regmap, TIM_BDTR, mask, bdtr);
509509

@@ -512,11 +512,25 @@ static int stm32_pwm_set_breakinput(struct stm32_pwm *priv,
512512
return (bdtr & bke) ? 0 : -EINVAL;
513513
}
514514

515-
static int stm32_pwm_apply_breakinputs(struct stm32_pwm *priv,
515+
static int stm32_pwm_apply_breakinputs(struct stm32_pwm *priv)
516+
{
517+
unsigned int i;
518+
int ret;
519+
520+
for (i = 0; i < priv->num_breakinputs; i++) {
521+
ret = stm32_pwm_set_breakinput(priv, &priv->breakinputs[i]);
522+
if (ret < 0)
523+
return ret;
524+
}
525+
526+
return 0;
527+
}
528+
529+
static int stm32_pwm_probe_breakinputs(struct stm32_pwm *priv,
516530
struct device_node *np)
517531
{
518-
struct stm32_breakinput breakinput[MAX_BREAKINPUT];
519-
int nb, ret, i, array_size;
532+
int nb, ret, array_size;
533+
unsigned int i;
520534

521535
nb = of_property_count_elems_of_size(np, "st,breakinput",
522536
sizeof(struct stm32_breakinput));
@@ -531,20 +545,21 @@ static int stm32_pwm_apply_breakinputs(struct stm32_pwm *priv,
531545
if (nb > MAX_BREAKINPUT)
532546
return -EINVAL;
533547

548+
priv->num_breakinputs = nb;
534549
array_size = nb * sizeof(struct stm32_breakinput) / sizeof(u32);
535550
ret = of_property_read_u32_array(np, "st,breakinput",
536-
(u32 *)breakinput, array_size);
551+
(u32 *)priv->breakinputs, array_size);
537552
if (ret)
538553
return ret;
539554

540-
for (i = 0; i < nb && !ret; i++) {
541-
ret = stm32_pwm_set_breakinput(priv,
542-
breakinput[i].index,
543-
breakinput[i].level,
544-
breakinput[i].filter);
555+
for (i = 0; i < priv->num_breakinputs; i++) {
556+
if (priv->breakinputs[i].index > 1 ||
557+
priv->breakinputs[i].level > 1 ||
558+
priv->breakinputs[i].filter > 15)
559+
return -EINVAL;
545560
}
546561

547-
return ret;
562+
return stm32_pwm_apply_breakinputs(priv);
548563
}
549564

550565
static void stm32_pwm_detect_complementary(struct stm32_pwm *priv)
@@ -614,7 +629,7 @@ static int stm32_pwm_probe(struct platform_device *pdev)
614629
if (!priv->regmap || !priv->clk)
615630
return -EINVAL;
616631

617-
ret = stm32_pwm_apply_breakinputs(priv, np);
632+
ret = stm32_pwm_probe_breakinputs(priv, np);
618633
if (ret)
619634
return ret;
620635

@@ -647,6 +662,42 @@ static int stm32_pwm_remove(struct platform_device *pdev)
647662
return 0;
648663
}
649664

665+
static int __maybe_unused stm32_pwm_suspend(struct device *dev)
666+
{
667+
struct stm32_pwm *priv = dev_get_drvdata(dev);
668+
unsigned int i;
669+
u32 ccer, mask;
670+
671+
/* Look for active channels */
672+
ccer = active_channels(priv);
673+
674+
for (i = 0; i < priv->chip.npwm; i++) {
675+
mask = TIM_CCER_CC1E << (i * 4);
676+
if (ccer & mask) {
677+
dev_err(dev, "PWM %u still in use by consumer %s\n",
678+
i, priv->chip.pwms[i].label);
679+
return -EBUSY;
680+
}
681+
}
682+
683+
return pinctrl_pm_select_sleep_state(dev);
684+
}
685+
686+
static int __maybe_unused stm32_pwm_resume(struct device *dev)
687+
{
688+
struct stm32_pwm *priv = dev_get_drvdata(dev);
689+
int ret;
690+
691+
ret = pinctrl_pm_select_default_state(dev);
692+
if (ret)
693+
return ret;
694+
695+
/* restore breakinput registers that may have been lost in low power */
696+
return stm32_pwm_apply_breakinputs(priv);
697+
}
698+
699+
static SIMPLE_DEV_PM_OPS(stm32_pwm_pm_ops, stm32_pwm_suspend, stm32_pwm_resume);
700+
650701
static const struct of_device_id stm32_pwm_of_match[] = {
651702
{ .compatible = "st,stm32-pwm", },
652703
{ /* end node */ },
@@ -659,6 +710,7 @@ static struct platform_driver stm32_pwm_driver = {
659710
.driver = {
660711
.name = "stm32-pwm",
661712
.of_match_table = stm32_pwm_of_match,
713+
.pm = &stm32_pwm_pm_ops,
662714
},
663715
};
664716
module_platform_driver(stm32_pwm_driver);

‎drivers/pwm/pwm-sun4i.c‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -137,10 +137,10 @@ static void sun4i_pwm_get_state(struct pwm_chip *chip,
137137

138138
val = sun4i_pwm_readl(sun4i_pwm, PWM_CH_PRD(pwm->hwpwm));
139139

140-
tmp = prescaler * NSEC_PER_SEC * PWM_REG_DTY(val);
140+
tmp = (u64)prescaler * NSEC_PER_SEC * PWM_REG_DTY(val);
141141
state->duty_cycle = DIV_ROUND_CLOSEST_ULL(tmp, clk_rate);
142142

143-
tmp = prescaler * NSEC_PER_SEC * PWM_REG_PRD(val);
143+
tmp = (u64)prescaler * NSEC_PER_SEC * PWM_REG_PRD(val);
144144
state->period = DIV_ROUND_CLOSEST_ULL(tmp, clk_rate);
145145
}
146146

@@ -156,7 +156,6 @@ static int sun4i_pwm_calculate(struct sun4i_pwm_chip *sun4i_pwm,
156156
if (sun4i_pwm->data->has_prescaler_bypass) {
157157
/* First, test without any prescaler when available */
158158
prescaler = PWM_PRESCAL_MASK;
159-
pval = 1;
160159
/*
161160
* When not using any prescaler, the clock period in nanoseconds
162161
* is not an integer so round it half up instead of

‎include/linux/mfd/stm32-timers.h‎

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -70,14 +70,11 @@
7070
#define TIM_CCER_CC4E BIT(12) /* Capt/Comp 4 out Ena */
7171
#define TIM_CCER_CC4P BIT(13) /* Capt/Comp 4 Polarity */
7272
#define TIM_CCER_CCXE (BIT(0) | BIT(4) | BIT(8) | BIT(12))
73-
#define TIM_BDTR_BKE BIT(12) /* Break input enable */
74-
#define TIM_BDTR_BKP BIT(13) /* Break input polarity */
73+
#define TIM_BDTR_BKE(x) BIT(12 + (x) * 12) /* Break input enable */
74+
#define TIM_BDTR_BKP(x) BIT(13 + (x) * 12) /* Break input polarity */
7575
#define TIM_BDTR_AOE BIT(14) /* Automatic Output Enable */
7676
#define TIM_BDTR_MOE BIT(15) /* Main Output Enable */
77-
#define TIM_BDTR_BKF (BIT(16) | BIT(17) | BIT(18) | BIT(19))
78-
#define TIM_BDTR_BK2F (BIT(20) | BIT(21) | BIT(22) | BIT(23))
79-
#define TIM_BDTR_BK2E BIT(24) /* Break 2 input enable */
80-
#define TIM_BDTR_BK2P BIT(25) /* Break 2 input polarity */
77+
#define TIM_BDTR_BKF(x) (0xf << (16 + (x) * 4))
8178
#define TIM_DCR_DBA GENMASK(4, 0) /* DMA base addr */
8279
#define TIM_DCR_DBL GENMASK(12, 8) /* DMA burst len */
8380

@@ -87,8 +84,7 @@
8784
#define TIM_CR2_MMS2_SHIFT 20
8885
#define TIM_SMCR_TS_SHIFT 4
8986
#define TIM_BDTR_BKF_MASK 0xF
90-
#define TIM_BDTR_BKF_SHIFT 16
91-
#define TIM_BDTR_BK2F_SHIFT 20
87+
#define TIM_BDTR_BKF_SHIFT(x) (16 + (x) * 4)
9288

9389
enum stm32_timers_dmas {
9490
STM32_TIMERS_DMA_CH1,

‎include/linux/pwm.h‎

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -243,10 +243,7 @@ pwm_set_relative_duty_cycle(struct pwm_state *state, unsigned int duty_cycle,
243243
* @request: optional hook for requesting a PWM
244244
* @free: optional hook for freeing a PWM
245245
* @capture: capture and report PWM signal
246-
* @apply: atomically apply a new PWM config. The state argument
247-
* should be adjusted with the real hardware config (if the
248-
* approximate the period or duty_cycle value, state should
249-
* reflect it)
246+
* @apply: atomically apply a new PWM config
250247
* @get_state: get the current PWM state. This function is only
251248
* called once per PWM device when the PWM chip is
252249
* registered.

0 commit comments

Comments
 (0)