1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
|
// SPDX-License-Identifier: GPL-2.0
/*
* STM32 Low-Power Timer PWM driver
*
* Copyright (C) STMicroelectronics 2017
*
* Author: Gerald Baeza <gerald.baeza@st.com>
*
* Inspired by Gerald Baeza's pwm-stm32 driver
*/
#include <linux/bitfield.h>
#include <linux/mfd/stm32-lptimer.h>
#include <linux/module.h>
#include <linux/of.h>
#include <linux/pinctrl/consumer.h>
#include <linux/platform_device.h>
#include <linux/pwm.h>
struct stm32_pwm_lp {
struct clk *clk;
struct regmap *regmap;
unsigned int num_cc_chans;
};
static inline struct stm32_pwm_lp *to_stm32_pwm_lp(struct pwm_chip *chip)
{
return pwmchip_get_drvdata(chip);
}
/* STM32 Low-Power Timer is preceded by a configurable power-of-2 prescaler */
#define STM32_LPTIM_MAX_PRESCALER 128
static int stm32_pwm_lp_update_allowed(struct stm32_pwm_lp *priv, int channel)
{
int ret;
u32 ccmr1;
unsigned long ccmr;
/* Only one PWM on this LPTIMER: enable, prescaler and reload value can be changed */
if (!priv->num_cc_chans)
return true;
ret = regmap_read(priv->regmap, STM32_LPTIM_CCMR1, &ccmr1);
if (ret)
return ret;
ccmr = ccmr1 & (STM32_LPTIM_CC1E | STM32_LPTIM_CC2E);
/* More than one channel enabled: enable, prescaler or ARR value can't be changed */
if (bitmap_weight(&ccmr, sizeof(u32) * BITS_PER_BYTE) > 1)
return false;
/*
* Only one channel is enabled (or none): check status on the other channel, to
* report if enable, prescaler or ARR value can be changed.
*/
if (channel)
return !(ccmr1 & STM32_LPTIM_CC1E);
else
return !(ccmr1 & STM32_LPTIM_CC2E);
}
static int stm32_pwm_lp_compare_channel_apply(struct stm32_pwm_lp *priv, int channel,
bool enable, enum pwm_polarity polarity)
{
u32 ccmr1, val, mask;
bool reenable;
int ret;
/* No dedicated CC channel: nothing to do */
if (!priv->num_cc_chans)
return 0;
ret = regmap_read(priv->regmap, STM32_LPTIM_CCMR1, &ccmr1);
if (ret)
return ret;
if (channel) {
/* Must disable CC channel (CCxE) to modify polarity (CCxP), then re-enable */
reenable = (enable && FIELD_GET(STM32_LPTIM_CC2E, ccmr1)) &&
(polarity != FIELD_GET(STM32_LPTIM_CC2P, ccmr1));
mask = STM32_LPTIM_CC2SEL | STM32_LPTIM_CC2E | STM32_LPTIM_CC2P;
val = FIELD_PREP(STM32_LPTIM_CC2P, polarity);
val |= FIELD_PREP(STM32_LPTIM_CC2E, enable);
} else {
reenable = (enable && FIELD_GET(STM32_LPTIM_CC1E, ccmr1)) &&
(polarity != FIELD_GET(STM32_LPTIM_CC1P, ccmr1));
mask = STM32_LPTIM_CC1SEL | STM32_LPTIM_CC1E | STM32_LPTIM_CC1P;
val = FIELD_PREP(STM32_LPTIM_CC1P, polarity);
val |= FIELD_PREP(STM32_LPTIM_CC1E, enable);
}
if (reenable) {
u32 cfgr, presc;
unsigned long rate;
unsigned int delay_us;
ret = regmap_update_bits(priv->regmap, STM32_LPTIM_CCMR1,
channel ? STM32_LPTIM_CC2E : STM32_LPTIM_CC1E, 0);
if (ret)
return ret;
/*
* After a write to the LPTIM_CCMRx register, a new write operation can only be
* performed after a delay of at least (PRESC × 3) clock cycles
*/
ret = regmap_read(priv->regmap, STM32_LPTIM_CFGR, &cfgr);
if (ret)
return ret;
presc = FIELD_GET(STM32_LPTIM_PRESC, cfgr);
rate = clk_get_rate(priv->clk) >> presc;
if (!rate)
return -EINVAL;
delay_us = 3 * DIV_ROUND_UP(USEC_PER_SEC, rate);
usleep_range(delay_us, delay_us * 2);
}
return regmap_update_bits(priv->regmap, STM32_LPTIM_CCMR1, mask, val);
}
static int stm32_pwm_lp_apply(struct pwm_chip *chip, struct pwm_device *pwm,
const struct pwm_state *state)
{
struct stm32_pwm_lp *priv = to_stm32_pwm_lp(chip);
unsigned long long prd, div, dty;
struct pwm_state cstate;
u32 arr, val, mask, cfgr, presc = 0;
bool reenable;
int ret;
pwm_get_state(pwm, &cstate);
reenable = !cstate.enabled;
if (!state->enabled) {
if (cstate.enabled) {
/* Disable CC channel if any */
ret = stm32_pwm_lp_compare_channel_apply(priv, pwm->hwpwm, false,
state->polarity);
if (ret)
return ret;
ret = regmap_write(priv->regmap, pwm->hwpwm ?
STM32_LPTIM_CCR2 : STM32_LPTIM_CMP, 0);
if (ret)
return ret;
/* Check if the timer can be disabled */
ret = stm32_pwm_lp_update_allowed(priv, pwm->hwpwm);
if (ret < 0)
return ret;
if (ret) {
/* Disable LP timer */
ret = regmap_write(priv->regmap, STM32_LPTIM_CR, 0);
if (ret)
return ret;
}
/* disable clock to PWM counter */
clk_disable(priv->clk);
}
return 0;
}
/* Calculate the period and prescaler value */
div = (unsigned long long)clk_get_rate(priv->clk) * state->period;
do_div(div, NSEC_PER_SEC);
if (!div) {
/* Clock is too slow to achieve requested period. */
dev_dbg(pwmchip_parent(chip), "Can't reach %llu ns\n", state->period);
return -EINVAL;
}
prd = div;
while (div > STM32_LPTIM_MAX_ARR) {
presc++;
if ((1 << presc) > STM32_LPTIM_MAX_PRESCALER) {
dev_err(pwmchip_parent(chip), "max prescaler exceeded\n");
return -EINVAL;
}
div = prd >> presc;
}
prd = div;
/* Calculate the duty cycle */
dty = prd * state->duty_cycle;
do_div(dty, state->period);
ret = regmap_read(priv->regmap, STM32_LPTIM_CFGR, &cfgr);
if (ret)
return ret;
/*
* When there are several channels, they share the same prescaler and reload value.
* Check if this can be changed, or the values are the same for all channels.
*/
if (!stm32_pwm_lp_update_allowed(priv, pwm->hwpwm)) {
ret = regmap_read(priv->regmap, STM32_LPTIM_ARR, &arr);
if (ret)
return ret;
if ((FIELD_GET(STM32_LPTIM_PRESC, cfgr) != presc) || (arr != prd - 1))
return -EBUSY;
}
if (!cstate.enabled) {
/* enable clock to drive PWM counter */
ret = clk_enable(priv->clk);
if (ret)
return ret;
}
if ((FIELD_GET(STM32_LPTIM_PRESC, cfgr) != presc) ||
((FIELD_GET(STM32_LPTIM_WAVPOL, cfgr) != state->polarity) && !priv->num_cc_chans)) {
val = FIELD_PREP(STM32_LPTIM_PRESC, presc);
mask = STM32_LPTIM_PRESC;
if (!priv->num_cc_chans) {
/*
* WAVPOL bit is only available when no capature compare channel is used,
* e.g. on LPTIMER instances that have only one output channel. CCMR1 is
* used otherwise.
*/
val |= FIELD_PREP(STM32_LPTIM_WAVPOL, state->polarity);
mask |= STM32_LPTIM_WAVPOL;
}
/* Must disable LP timer to modify CFGR */
reenable = true;
ret = regmap_write(priv->regmap, STM32_LPTIM_CR, 0);
if (ret)
goto err;
ret = regmap_update_bits(priv->regmap, STM32_LPTIM_CFGR, mask,
val);
if (ret)
goto err;
}
if (reenable) {
/* Must (re)enable LP timer to modify CMP & ARR */
ret = regmap_write(priv->regmap, STM32_LPTIM_CR,
STM32_LPTIM_ENABLE);
if (ret)
goto err;
}
ret = regmap_write(priv->regmap, STM32_LPTIM_ARR, prd - 1);
if (ret)
goto err;
/* Write CMP/CCRx register and ensure it's been properly written */
ret = regmap_write(priv->regmap, pwm->hwpwm ? STM32_LPTIM_CCR2 : STM32_LPTIM_CMP,
prd - (1 + dty));
if (ret)
goto err;
/* ensure ARR and CMP/CCRx registers are properly written */
ret = regmap_read_poll_timeout(priv->regmap, STM32_LPTIM_ISR, val, pwm->hwpwm ?
(val & STM32_LPTIM_CMP2_ARROK) == STM32_LPTIM_CMP2_ARROK :
(val & STM32_LPTIM_CMPOK_ARROK) == STM32_LPTIM_CMPOK_ARROK,
100, 1000);
if (ret) {
dev_err(pwmchip_parent(chip), "ARR/CMP registers write issue\n");
goto err;
}
ret = regmap_write(priv->regmap, STM32_LPTIM_ICR, pwm->hwpwm ?
STM32_LPTIM_CMP2OKCF_ARROKCF : STM32_LPTIM_CMPOKCF_ARROKCF);
if (ret)
goto err;
ret = stm32_pwm_lp_compare_channel_apply(priv, pwm->hwpwm, true, state->polarity);
if (ret)
goto err;
if (reenable) {
/* Start LP timer in continuous mode */
ret = regmap_set_bits(priv->regmap, STM32_LPTIM_CR,
STM32_LPTIM_CNTSTRT);
if (ret) {
regmap_write(priv->regmap, STM32_LPTIM_CR, 0);
goto err;
}
}
return 0;
err:
if (!cstate.enabled)
clk_disable(priv->clk);
return ret;
}
static int stm32_pwm_lp_get_state(struct pwm_chip *chip,
struct pwm_device *pwm,
struct pwm_state *state)
{
struct stm32_pwm_lp *priv = to_stm32_pwm_lp(chip);
unsigned long rate = clk_get_rate(priv->clk);
u32 val, presc, prd, ccmr1;
bool enabled;
u64 tmp;
regmap_read(priv->regmap, STM32_LPTIM_CR, &val);
enabled = !!FIELD_GET(STM32_LPTIM_ENABLE, val);
if (priv->num_cc_chans) {
/* There's a CC chan, need to also check if it's enabled */
regmap_read(priv->regmap, STM32_LPTIM_CCMR1, &ccmr1);
if (pwm->hwpwm)
enabled &= !!FIELD_GET(STM32_LPTIM_CC2E, ccmr1);
else
enabled &= !!FIELD_GET(STM32_LPTIM_CC1E, ccmr1);
}
state->enabled = enabled;
/* Keep PWM counter clock refcount in sync with PWM initial state */
if (state->enabled) {
int ret = clk_enable(priv->clk);
if (ret)
return ret;
}
regmap_read(priv->regmap, STM32_LPTIM_CFGR, &val);
presc = FIELD_GET(STM32_LPTIM_PRESC, val);
if (priv->num_cc_chans) {
if (pwm->hwpwm)
state->polarity = FIELD_GET(STM32_LPTIM_CC2P, ccmr1);
else
state->polarity = FIELD_GET(STM32_LPTIM_CC1P, ccmr1);
} else {
state->polarity = FIELD_GET(STM32_LPTIM_WAVPOL, val);
}
regmap_read(priv->regmap, STM32_LPTIM_ARR, &prd);
tmp = prd + 1;
tmp = (tmp << presc) * NSEC_PER_SEC;
state->period = DIV_ROUND_CLOSEST_ULL(tmp, rate);
regmap_read(priv->regmap, pwm->hwpwm ? STM32_LPTIM_CCR2 : STM32_LPTIM_CMP, &val);
tmp = prd - val;
tmp = (tmp << presc) * NSEC_PER_SEC;
state->duty_cycle = DIV_ROUND_CLOSEST_ULL(tmp, rate);
return 0;
}
static const struct pwm_ops stm32_pwm_lp_ops = {
.apply = stm32_pwm_lp_apply,
.get_state = stm32_pwm_lp_get_state,
};
static int stm32_pwm_lp_probe(struct platform_device *pdev)
{
struct stm32_lptimer *ddata = dev_get_drvdata(pdev->dev.parent);
struct stm32_pwm_lp *priv;
struct pwm_chip *chip;
unsigned int npwm;
int ret;
if (!ddata->num_cc_chans) {
/* No dedicated CC channel, so there's only one PWM channel */
npwm = 1;
} else {
/* There are dedicated CC channels, each with one PWM output */
npwm = ddata->num_cc_chans;
}
chip = devm_pwmchip_alloc(&pdev->dev, npwm, sizeof(*priv));
if (IS_ERR(chip))
return PTR_ERR(chip);
priv = to_stm32_pwm_lp(chip);
priv->regmap = ddata->regmap;
priv->clk = ddata->clk;
priv->num_cc_chans = ddata->num_cc_chans;
chip->ops = &stm32_pwm_lp_ops;
ret = devm_pwmchip_add(&pdev->dev, chip);
if (ret < 0)
return ret;
platform_set_drvdata(pdev, chip);
return 0;
}
static int stm32_pwm_lp_suspend(struct device *dev)
{
struct pwm_chip *chip = dev_get_drvdata(dev);
struct pwm_state state;
unsigned int i;
for (i = 0; i < chip->npwm; i++) {
pwm_get_state(&chip->pwms[i], &state);
if (state.enabled) {
dev_err(dev, "The consumer didn't stop us (%s)\n",
chip->pwms[i].label);
return -EBUSY;
}
}
return pinctrl_pm_select_sleep_state(dev);
}
static int stm32_pwm_lp_resume(struct device *dev)
{
return pinctrl_pm_select_default_state(dev);
}
static DEFINE_SIMPLE_DEV_PM_OPS(stm32_pwm_lp_pm_ops, stm32_pwm_lp_suspend,
stm32_pwm_lp_resume);
static const struct of_device_id stm32_pwm_lp_of_match[] = {
{ .compatible = "st,stm32-pwm-lp", },
{},
};
MODULE_DEVICE_TABLE(of, stm32_pwm_lp_of_match);
static struct platform_driver stm32_pwm_lp_driver = {
.probe = stm32_pwm_lp_probe,
.driver = {
.name = "stm32-pwm-lp",
.of_match_table = stm32_pwm_lp_of_match,
.pm = pm_ptr(&stm32_pwm_lp_pm_ops),
},
};
module_platform_driver(stm32_pwm_lp_driver);
MODULE_ALIAS("platform:stm32-pwm-lp");
MODULE_DESCRIPTION("STMicroelectronics STM32 PWM LP driver");
MODULE_LICENSE("GPL v2");
|