blob: 014e2a759fa649feef073a987290526919a17067 [file] [log] [blame]
Benjamin Gaignardd7a131d2017-12-05 15:57:21 +01001// SPDX-License-Identifier: GPL-2.0
Benjamin Gaignard7edf7362017-01-20 10:15:05 +01002/*
3 * Copyright (C) STMicroelectronics 2016
4 *
5 * Author: Gerald Baeza <gerald.baeza@st.com>
6 *
Benjamin Gaignard7edf7362017-01-20 10:15:05 +01007 * Inspired by timer-stm32.c from Maxime Coquelin
8 * pwm-atmel.c from Bo Shen
9 */
10
11#include <linux/mfd/stm32-timers.h>
12#include <linux/module.h>
13#include <linux/of.h>
14#include <linux/platform_device.h>
15#include <linux/pwm.h>
16
17#define CCMR_CHANNEL_SHIFT 8
18#define CCMR_CHANNEL_MASK 0xFF
19#define MAX_BREAKINPUT 2
20
21struct stm32_pwm {
22 struct pwm_chip chip;
Benjamin Gaignard7edf7362017-01-20 10:15:05 +010023 struct clk *clk;
24 struct regmap *regmap;
25 u32 max_arr;
26 bool have_complementary_output;
27};
28
29struct stm32_breakinput {
30 u32 index;
31 u32 level;
32 u32 filter;
33};
34
35static inline struct stm32_pwm *to_stm32_pwm_dev(struct pwm_chip *chip)
36{
37 return container_of(chip, struct stm32_pwm, chip);
38}
39
40static u32 active_channels(struct stm32_pwm *dev)
41{
42 u32 ccer;
43
44 regmap_read(dev->regmap, TIM_CCER, &ccer);
45
46 return ccer & TIM_CCER_CCXE;
47}
48
49static int write_ccrx(struct stm32_pwm *dev, int ch, u32 value)
50{
51 switch (ch) {
52 case 0:
53 return regmap_write(dev->regmap, TIM_CCR1, value);
54 case 1:
55 return regmap_write(dev->regmap, TIM_CCR2, value);
56 case 2:
57 return regmap_write(dev->regmap, TIM_CCR3, value);
58 case 3:
59 return regmap_write(dev->regmap, TIM_CCR4, value);
60 }
61 return -EINVAL;
62}
63
64static int stm32_pwm_config(struct stm32_pwm *priv, int ch,
65 int duty_ns, int period_ns)
66{
67 unsigned long long prd, div, dty;
68 unsigned int prescaler = 0;
69 u32 ccmr, mask, shift;
70
71 /* Period and prescaler values depends on clock rate */
72 div = (unsigned long long)clk_get_rate(priv->clk) * period_ns;
73
74 do_div(div, NSEC_PER_SEC);
75 prd = div;
76
77 while (div > priv->max_arr) {
78 prescaler++;
79 div = prd;
80 do_div(div, prescaler + 1);
81 }
82
83 prd = div;
84
85 if (prescaler > MAX_TIM_PSC)
86 return -EINVAL;
87
88 /*
89 * All channels share the same prescaler and counter so when two
90 * channels are active at the same time we can't change them
91 */
92 if (active_channels(priv) & ~(1 << ch * 4)) {
93 u32 psc, arr;
94
95 regmap_read(priv->regmap, TIM_PSC, &psc);
96 regmap_read(priv->regmap, TIM_ARR, &arr);
97
98 if ((psc != prescaler) || (arr != prd - 1))
99 return -EBUSY;
100 }
101
102 regmap_write(priv->regmap, TIM_PSC, prescaler);
103 regmap_write(priv->regmap, TIM_ARR, prd - 1);
104 regmap_update_bits(priv->regmap, TIM_CR1, TIM_CR1_ARPE, TIM_CR1_ARPE);
105
106 /* Calculate the duty cycles */
107 dty = prd * duty_ns;
108 do_div(dty, period_ns);
109
110 write_ccrx(priv, ch, dty);
111
112 /* Configure output mode */
113 shift = (ch & 0x1) * CCMR_CHANNEL_SHIFT;
114 ccmr = (TIM_CCMR_PE | TIM_CCMR_M1) << shift;
115 mask = CCMR_CHANNEL_MASK << shift;
116
117 if (ch < 2)
118 regmap_update_bits(priv->regmap, TIM_CCMR1, mask, ccmr);
119 else
120 regmap_update_bits(priv->regmap, TIM_CCMR2, mask, ccmr);
121
122 regmap_update_bits(priv->regmap, TIM_BDTR,
123 TIM_BDTR_MOE | TIM_BDTR_AOE,
124 TIM_BDTR_MOE | TIM_BDTR_AOE);
125
126 return 0;
127}
128
129static int stm32_pwm_set_polarity(struct stm32_pwm *priv, int ch,
130 enum pwm_polarity polarity)
131{
132 u32 mask;
133
134 mask = TIM_CCER_CC1P << (ch * 4);
135 if (priv->have_complementary_output)
136 mask |= TIM_CCER_CC1NP << (ch * 4);
137
138 regmap_update_bits(priv->regmap, TIM_CCER, mask,
139 polarity == PWM_POLARITY_NORMAL ? 0 : mask);
140
141 return 0;
142}
143
144static int stm32_pwm_enable(struct stm32_pwm *priv, int ch)
145{
146 u32 mask;
147 int ret;
148
149 ret = clk_enable(priv->clk);
150 if (ret)
151 return ret;
152
153 /* Enable channel */
154 mask = TIM_CCER_CC1E << (ch * 4);
155 if (priv->have_complementary_output)
156 mask |= TIM_CCER_CC1NE << (ch * 4);
157
158 regmap_update_bits(priv->regmap, TIM_CCER, mask, mask);
159
160 /* Make sure that registers are updated */
161 regmap_update_bits(priv->regmap, TIM_EGR, TIM_EGR_UG, TIM_EGR_UG);
162
163 /* Enable controller */
164 regmap_update_bits(priv->regmap, TIM_CR1, TIM_CR1_CEN, TIM_CR1_CEN);
165
166 return 0;
167}
168
169static void stm32_pwm_disable(struct stm32_pwm *priv, int ch)
170{
171 u32 mask;
172
173 /* Disable channel */
174 mask = TIM_CCER_CC1E << (ch * 4);
175 if (priv->have_complementary_output)
176 mask |= TIM_CCER_CC1NE << (ch * 4);
177
178 regmap_update_bits(priv->regmap, TIM_CCER, mask, 0);
179
180 /* When all channels are disabled, we can disable the controller */
181 if (!active_channels(priv))
182 regmap_update_bits(priv->regmap, TIM_CR1, TIM_CR1_CEN, 0);
183
184 clk_disable(priv->clk);
185}
186
187static int stm32_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm,
188 struct pwm_state *state)
189{
190 bool enabled;
191 struct stm32_pwm *priv = to_stm32_pwm_dev(chip);
192 int ret;
193
194 enabled = pwm->state.enabled;
195
196 if (enabled && !state->enabled) {
197 stm32_pwm_disable(priv, pwm->hwpwm);
198 return 0;
199 }
200
201 if (state->polarity != pwm->state.polarity)
202 stm32_pwm_set_polarity(priv, pwm->hwpwm, state->polarity);
203
204 ret = stm32_pwm_config(priv, pwm->hwpwm,
205 state->duty_cycle, state->period);
206 if (ret)
207 return ret;
208
209 if (!enabled && state->enabled)
210 ret = stm32_pwm_enable(priv, pwm->hwpwm);
211
212 return ret;
213}
214
215static const struct pwm_ops stm32pwm_ops = {
216 .owner = THIS_MODULE,
217 .apply = stm32_pwm_apply,
218};
219
220static int stm32_pwm_set_breakinput(struct stm32_pwm *priv,
221 int index, int level, int filter)
222{
223 u32 bke = (index == 0) ? TIM_BDTR_BKE : TIM_BDTR_BK2E;
224 int shift = (index == 0) ? TIM_BDTR_BKF_SHIFT : TIM_BDTR_BK2F_SHIFT;
225 u32 mask = (index == 0) ? TIM_BDTR_BKE | TIM_BDTR_BKP | TIM_BDTR_BKF
226 : TIM_BDTR_BK2E | TIM_BDTR_BK2P | TIM_BDTR_BK2F;
227 u32 bdtr = bke;
228
229 /*
230 * The both bits could be set since only one will be wrote
231 * due to mask value.
232 */
233 if (level)
234 bdtr |= TIM_BDTR_BKP | TIM_BDTR_BK2P;
235
236 bdtr |= (filter & TIM_BDTR_BKF_MASK) << shift;
237
238 regmap_update_bits(priv->regmap, TIM_BDTR, mask, bdtr);
239
240 regmap_read(priv->regmap, TIM_BDTR, &bdtr);
241
242 return (bdtr & bke) ? 0 : -EINVAL;
243}
244
245static int stm32_pwm_apply_breakinputs(struct stm32_pwm *priv,
246 struct device_node *np)
247{
248 struct stm32_breakinput breakinput[MAX_BREAKINPUT];
249 int nb, ret, i, array_size;
250
251 nb = of_property_count_elems_of_size(np, "st,breakinput",
252 sizeof(struct stm32_breakinput));
253
254 /*
255 * Because "st,breakinput" parameter is optional do not make probe
256 * failed if it doesn't exist.
257 */
258 if (nb <= 0)
259 return 0;
260
261 if (nb > MAX_BREAKINPUT)
262 return -EINVAL;
263
264 array_size = nb * sizeof(struct stm32_breakinput) / sizeof(u32);
265 ret = of_property_read_u32_array(np, "st,breakinput",
266 (u32 *)breakinput, array_size);
267 if (ret)
268 return ret;
269
270 for (i = 0; i < nb && !ret; i++) {
271 ret = stm32_pwm_set_breakinput(priv,
272 breakinput[i].index,
273 breakinput[i].level,
274 breakinput[i].filter);
275 }
276
277 return ret;
278}
279
280static void stm32_pwm_detect_complementary(struct stm32_pwm *priv)
281{
282 u32 ccer;
283
284 /*
285 * If complementary bit doesn't exist writing 1 will have no
286 * effect so we can detect it.
287 */
288 regmap_update_bits(priv->regmap,
289 TIM_CCER, TIM_CCER_CC1NE, TIM_CCER_CC1NE);
290 regmap_read(priv->regmap, TIM_CCER, &ccer);
291 regmap_update_bits(priv->regmap, TIM_CCER, TIM_CCER_CC1NE, 0);
292
293 priv->have_complementary_output = (ccer != 0);
294}
295
296static int stm32_pwm_detect_channels(struct stm32_pwm *priv)
297{
298 u32 ccer;
299 int npwm = 0;
300
301 /*
302 * If channels enable bits don't exist writing 1 will have no
303 * effect so we can detect and count them.
304 */
305 regmap_update_bits(priv->regmap,
306 TIM_CCER, TIM_CCER_CCXE, TIM_CCER_CCXE);
307 regmap_read(priv->regmap, TIM_CCER, &ccer);
308 regmap_update_bits(priv->regmap, TIM_CCER, TIM_CCER_CCXE, 0);
309
310 if (ccer & TIM_CCER_CC1E)
311 npwm++;
312
313 if (ccer & TIM_CCER_CC2E)
314 npwm++;
315
316 if (ccer & TIM_CCER_CC3E)
317 npwm++;
318
319 if (ccer & TIM_CCER_CC4E)
320 npwm++;
321
322 return npwm;
323}
324
325static int stm32_pwm_probe(struct platform_device *pdev)
326{
327 struct device *dev = &pdev->dev;
328 struct device_node *np = dev->of_node;
329 struct stm32_timers *ddata = dev_get_drvdata(pdev->dev.parent);
330 struct stm32_pwm *priv;
331 int ret;
332
333 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
334 if (!priv)
335 return -ENOMEM;
336
337 priv->regmap = ddata->regmap;
338 priv->clk = ddata->clk;
339 priv->max_arr = ddata->max_arr;
340
341 if (!priv->regmap || !priv->clk)
342 return -EINVAL;
343
344 ret = stm32_pwm_apply_breakinputs(priv, np);
345 if (ret)
346 return ret;
347
348 stm32_pwm_detect_complementary(priv);
349
350 priv->chip.base = -1;
351 priv->chip.dev = dev;
352 priv->chip.ops = &stm32pwm_ops;
353 priv->chip.npwm = stm32_pwm_detect_channels(priv);
354
355 ret = pwmchip_add(&priv->chip);
356 if (ret < 0)
357 return ret;
358
359 platform_set_drvdata(pdev, priv);
360
361 return 0;
362}
363
364static int stm32_pwm_remove(struct platform_device *pdev)
365{
366 struct stm32_pwm *priv = platform_get_drvdata(pdev);
367 unsigned int i;
368
369 for (i = 0; i < priv->chip.npwm; i++)
370 pwm_disable(&priv->chip.pwms[i]);
371
372 pwmchip_remove(&priv->chip);
373
374 return 0;
375}
376
377static const struct of_device_id stm32_pwm_of_match[] = {
378 { .compatible = "st,stm32-pwm", },
379 { /* end node */ },
380};
381MODULE_DEVICE_TABLE(of, stm32_pwm_of_match);
382
383static struct platform_driver stm32_pwm_driver = {
384 .probe = stm32_pwm_probe,
385 .remove = stm32_pwm_remove,
386 .driver = {
387 .name = "stm32-pwm",
388 .of_match_table = stm32_pwm_of_match,
389 },
390};
391module_platform_driver(stm32_pwm_driver);
392
393MODULE_ALIAS("platform:stm32-pwm");
394MODULE_DESCRIPTION("STMicroelectronics STM32 PWM driver");
395MODULE_LICENSE("GPL v2");