2 * Copyright (C) STMicroelectronics 2016
4 * Author: Gerald Baeza <gerald.baeza@st.com>
6 * License terms: GNU General Public License (GPL), version 2
8 * Inspired by timer-stm32.c from Maxime Coquelin
9 * pwm-atmel.c from Bo Shen
12 #include <linux/mfd/stm32-timers.h>
13 #include <linux/module.h>
15 #include <linux/platform_device.h>
16 #include <linux/pwm.h>
18 #define CCMR_CHANNEL_SHIFT 8
19 #define CCMR_CHANNEL_MASK 0xFF
20 #define MAX_BREAKINPUT 2
26 struct regmap
*regmap
;
28 bool have_complementary_output
;
31 struct stm32_breakinput
{
37 static inline struct stm32_pwm
*to_stm32_pwm_dev(struct pwm_chip
*chip
)
39 return container_of(chip
, struct stm32_pwm
, chip
);
42 static u32
active_channels(struct stm32_pwm
*dev
)
46 regmap_read(dev
->regmap
, TIM_CCER
, &ccer
);
48 return ccer
& TIM_CCER_CCXE
;
51 static int write_ccrx(struct stm32_pwm
*dev
, int ch
, u32 value
)
55 return regmap_write(dev
->regmap
, TIM_CCR1
, value
);
57 return regmap_write(dev
->regmap
, TIM_CCR2
, value
);
59 return regmap_write(dev
->regmap
, TIM_CCR3
, value
);
61 return regmap_write(dev
->regmap
, TIM_CCR4
, value
);
66 static int stm32_pwm_config(struct stm32_pwm
*priv
, int ch
,
67 int duty_ns
, int period_ns
)
69 unsigned long long prd
, div
, dty
;
70 unsigned int prescaler
= 0;
71 u32 ccmr
, mask
, shift
;
73 /* Period and prescaler values depends on clock rate */
74 div
= (unsigned long long)clk_get_rate(priv
->clk
) * period_ns
;
76 do_div(div
, NSEC_PER_SEC
);
79 while (div
> priv
->max_arr
) {
82 do_div(div
, prescaler
+ 1);
87 if (prescaler
> MAX_TIM_PSC
)
91 * All channels share the same prescaler and counter so when two
92 * channels are active at the same time we can't change them
94 if (active_channels(priv
) & ~(1 << ch
* 4)) {
97 regmap_read(priv
->regmap
, TIM_PSC
, &psc
);
98 regmap_read(priv
->regmap
, TIM_ARR
, &arr
);
100 if ((psc
!= prescaler
) || (arr
!= prd
- 1))
104 regmap_write(priv
->regmap
, TIM_PSC
, prescaler
);
105 regmap_write(priv
->regmap
, TIM_ARR
, prd
- 1);
106 regmap_update_bits(priv
->regmap
, TIM_CR1
, TIM_CR1_ARPE
, TIM_CR1_ARPE
);
108 /* Calculate the duty cycles */
110 do_div(dty
, period_ns
);
112 write_ccrx(priv
, ch
, dty
);
114 /* Configure output mode */
115 shift
= (ch
& 0x1) * CCMR_CHANNEL_SHIFT
;
116 ccmr
= (TIM_CCMR_PE
| TIM_CCMR_M1
) << shift
;
117 mask
= CCMR_CHANNEL_MASK
<< shift
;
120 regmap_update_bits(priv
->regmap
, TIM_CCMR1
, mask
, ccmr
);
122 regmap_update_bits(priv
->regmap
, TIM_CCMR2
, mask
, ccmr
);
124 regmap_update_bits(priv
->regmap
, TIM_BDTR
,
125 TIM_BDTR_MOE
| TIM_BDTR_AOE
,
126 TIM_BDTR_MOE
| TIM_BDTR_AOE
);
131 static int stm32_pwm_set_polarity(struct stm32_pwm
*priv
, int ch
,
132 enum pwm_polarity polarity
)
136 mask
= TIM_CCER_CC1P
<< (ch
* 4);
137 if (priv
->have_complementary_output
)
138 mask
|= TIM_CCER_CC1NP
<< (ch
* 4);
140 regmap_update_bits(priv
->regmap
, TIM_CCER
, mask
,
141 polarity
== PWM_POLARITY_NORMAL
? 0 : mask
);
146 static int stm32_pwm_enable(struct stm32_pwm
*priv
, int ch
)
151 ret
= clk_enable(priv
->clk
);
156 mask
= TIM_CCER_CC1E
<< (ch
* 4);
157 if (priv
->have_complementary_output
)
158 mask
|= TIM_CCER_CC1NE
<< (ch
* 4);
160 regmap_update_bits(priv
->regmap
, TIM_CCER
, mask
, mask
);
162 /* Make sure that registers are updated */
163 regmap_update_bits(priv
->regmap
, TIM_EGR
, TIM_EGR_UG
, TIM_EGR_UG
);
165 /* Enable controller */
166 regmap_update_bits(priv
->regmap
, TIM_CR1
, TIM_CR1_CEN
, TIM_CR1_CEN
);
171 static void stm32_pwm_disable(struct stm32_pwm
*priv
, int ch
)
175 /* Disable channel */
176 mask
= TIM_CCER_CC1E
<< (ch
* 4);
177 if (priv
->have_complementary_output
)
178 mask
|= TIM_CCER_CC1NE
<< (ch
* 4);
180 regmap_update_bits(priv
->regmap
, TIM_CCER
, mask
, 0);
182 /* When all channels are disabled, we can disable the controller */
183 if (!active_channels(priv
))
184 regmap_update_bits(priv
->regmap
, TIM_CR1
, TIM_CR1_CEN
, 0);
186 clk_disable(priv
->clk
);
189 static int stm32_pwm_apply(struct pwm_chip
*chip
, struct pwm_device
*pwm
,
190 struct pwm_state
*state
)
193 struct stm32_pwm
*priv
= to_stm32_pwm_dev(chip
);
196 enabled
= pwm
->state
.enabled
;
198 if (enabled
&& !state
->enabled
) {
199 stm32_pwm_disable(priv
, pwm
->hwpwm
);
203 if (state
->polarity
!= pwm
->state
.polarity
)
204 stm32_pwm_set_polarity(priv
, pwm
->hwpwm
, state
->polarity
);
206 ret
= stm32_pwm_config(priv
, pwm
->hwpwm
,
207 state
->duty_cycle
, state
->period
);
211 if (!enabled
&& state
->enabled
)
212 ret
= stm32_pwm_enable(priv
, pwm
->hwpwm
);
217 static const struct pwm_ops stm32pwm_ops
= {
218 .owner
= THIS_MODULE
,
219 .apply
= stm32_pwm_apply
,
222 static int stm32_pwm_set_breakinput(struct stm32_pwm
*priv
,
223 int index
, int level
, int filter
)
225 u32 bke
= (index
== 0) ? TIM_BDTR_BKE
: TIM_BDTR_BK2E
;
226 int shift
= (index
== 0) ? TIM_BDTR_BKF_SHIFT
: TIM_BDTR_BK2F_SHIFT
;
227 u32 mask
= (index
== 0) ? TIM_BDTR_BKE
| TIM_BDTR_BKP
| TIM_BDTR_BKF
228 : TIM_BDTR_BK2E
| TIM_BDTR_BK2P
| TIM_BDTR_BK2F
;
232 * The both bits could be set since only one will be wrote
236 bdtr
|= TIM_BDTR_BKP
| TIM_BDTR_BK2P
;
238 bdtr
|= (filter
& TIM_BDTR_BKF_MASK
) << shift
;
240 regmap_update_bits(priv
->regmap
, TIM_BDTR
, mask
, bdtr
);
242 regmap_read(priv
->regmap
, TIM_BDTR
, &bdtr
);
244 return (bdtr
& bke
) ? 0 : -EINVAL
;
247 static int stm32_pwm_apply_breakinputs(struct stm32_pwm
*priv
,
248 struct device_node
*np
)
250 struct stm32_breakinput breakinput
[MAX_BREAKINPUT
];
251 int nb
, ret
, i
, array_size
;
253 nb
= of_property_count_elems_of_size(np
, "st,breakinput",
254 sizeof(struct stm32_breakinput
));
257 * Because "st,breakinput" parameter is optional do not make probe
258 * failed if it doesn't exist.
263 if (nb
> MAX_BREAKINPUT
)
266 array_size
= nb
* sizeof(struct stm32_breakinput
) / sizeof(u32
);
267 ret
= of_property_read_u32_array(np
, "st,breakinput",
268 (u32
*)breakinput
, array_size
);
272 for (i
= 0; i
< nb
&& !ret
; i
++) {
273 ret
= stm32_pwm_set_breakinput(priv
,
276 breakinput
[i
].filter
);
282 static void stm32_pwm_detect_complementary(struct stm32_pwm
*priv
)
287 * If complementary bit doesn't exist writing 1 will have no
288 * effect so we can detect it.
290 regmap_update_bits(priv
->regmap
,
291 TIM_CCER
, TIM_CCER_CC1NE
, TIM_CCER_CC1NE
);
292 regmap_read(priv
->regmap
, TIM_CCER
, &ccer
);
293 regmap_update_bits(priv
->regmap
, TIM_CCER
, TIM_CCER_CC1NE
, 0);
295 priv
->have_complementary_output
= (ccer
!= 0);
298 static int stm32_pwm_detect_channels(struct stm32_pwm
*priv
)
304 * If channels enable bits don't exist writing 1 will have no
305 * effect so we can detect and count them.
307 regmap_update_bits(priv
->regmap
,
308 TIM_CCER
, TIM_CCER_CCXE
, TIM_CCER_CCXE
);
309 regmap_read(priv
->regmap
, TIM_CCER
, &ccer
);
310 regmap_update_bits(priv
->regmap
, TIM_CCER
, TIM_CCER_CCXE
, 0);
312 if (ccer
& TIM_CCER_CC1E
)
315 if (ccer
& TIM_CCER_CC2E
)
318 if (ccer
& TIM_CCER_CC3E
)
321 if (ccer
& TIM_CCER_CC4E
)
327 static int stm32_pwm_probe(struct platform_device
*pdev
)
329 struct device
*dev
= &pdev
->dev
;
330 struct device_node
*np
= dev
->of_node
;
331 struct stm32_timers
*ddata
= dev_get_drvdata(pdev
->dev
.parent
);
332 struct stm32_pwm
*priv
;
335 priv
= devm_kzalloc(dev
, sizeof(*priv
), GFP_KERNEL
);
339 priv
->regmap
= ddata
->regmap
;
340 priv
->clk
= ddata
->clk
;
341 priv
->max_arr
= ddata
->max_arr
;
343 if (!priv
->regmap
|| !priv
->clk
)
346 ret
= stm32_pwm_apply_breakinputs(priv
, np
);
350 stm32_pwm_detect_complementary(priv
);
352 priv
->chip
.base
= -1;
353 priv
->chip
.dev
= dev
;
354 priv
->chip
.ops
= &stm32pwm_ops
;
355 priv
->chip
.npwm
= stm32_pwm_detect_channels(priv
);
357 ret
= pwmchip_add(&priv
->chip
);
361 platform_set_drvdata(pdev
, priv
);
366 static int stm32_pwm_remove(struct platform_device
*pdev
)
368 struct stm32_pwm
*priv
= platform_get_drvdata(pdev
);
371 for (i
= 0; i
< priv
->chip
.npwm
; i
++)
372 pwm_disable(&priv
->chip
.pwms
[i
]);
374 pwmchip_remove(&priv
->chip
);
379 static const struct of_device_id stm32_pwm_of_match
[] = {
380 { .compatible
= "st,stm32-pwm", },
383 MODULE_DEVICE_TABLE(of
, stm32_pwm_of_match
);
385 static struct platform_driver stm32_pwm_driver
= {
386 .probe
= stm32_pwm_probe
,
387 .remove
= stm32_pwm_remove
,
390 .of_match_table
= stm32_pwm_of_match
,
393 module_platform_driver(stm32_pwm_driver
);
395 MODULE_ALIAS("platform:stm32-pwm");
396 MODULE_DESCRIPTION("STMicroelectronics STM32 PWM driver");
397 MODULE_LICENSE("GPL v2");