| 1 |
// SPDX-License-Identifier: GPL-2.0 |
| 2 |
/* |
| 3 |
* Copyright (C) STMicroelectronics 2016 |
| 4 |
* |
| 5 |
* Author: Gerald Baeza <[email protected]> |
| 6 |
* |
| 7 |
* Inspired by timer-stm32.c from Maxime Coquelin |
| 8 |
* pwm-atmel.c from Bo Shen |
| 9 |
*/ |
| 10 |
|
| 11 |
#include <linux/bitfield.h> |
| 12 |
#include <linux/mfd/stm32-timers.h> |
| 13 |
#include <linux/module.h> |
| 14 |
#include <linux/of.h> |
| 15 |
#include <linux/pinctrl/consumer.h> |
| 16 |
#include <linux/platform_device.h> |
| 17 |
#include <linux/pwm.h> |
| 18 |
|
| 19 |
#define CCMR_CHANNEL_SHIFT 8 |
| 20 |
#define CCMR_CHANNEL_MASK 0xFF |
| 21 |
#define MAX_BREAKINPUT 2 |
| 22 |
#define STM32_MAX_PWM_OUTPUT 4 |
| 23 |
|
| 24 |
struct stm32_breakinput { |
| 25 |
u32 index; |
| 26 |
u32 level; |
| 27 |
u32 filter; |
| 28 |
}; |
| 29 |
|
| 30 |
struct stm32_pwm { |
| 31 |
struct mutex lock; /* protect pwm config/enable */ |
| 32 |
struct clk *clk; |
| 33 |
struct regmap *regmap; |
| 34 |
u32 max_arr; |
| 35 |
bool have_complementary_output; |
| 36 |
struct stm32_breakinput breakinputs[MAX_BREAKINPUT]; |
| 37 |
unsigned int num_breakinputs; |
| 38 |
u32 capture[4] ____cacheline_aligned; /* DMA'able buffer */ |
| 39 |
}; |
| 40 |
|
| 41 |
static inline struct stm32_pwm *to_stm32_pwm_dev(struct pwm_chip *chip) |
| 42 |
{ |
| 43 |
return pwmchip_get_drvdata(chip); |
| 44 |
} |
| 45 |
|
| 46 |
static u32 active_channels(struct stm32_pwm *dev) |
| 47 |
{ |
| 48 |
u32 ccer; |
| 49 |
|
| 50 |
regmap_read(dev->regmap, TIM_CCER, &ccer); |
| 51 |
|
| 52 |
return ccer & TIM_CCER_CCXE; |
| 53 |
} |
| 54 |
|
| 55 |
struct stm32_pwm_waveform { |
| 56 |
u32 ccer; |
| 57 |
u32 psc; |
| 58 |
u32 arr; |
| 59 |
u32 ccr; |
| 60 |
}; |
| 61 |
|
| 62 |
static int stm32_pwm_round_waveform_tohw(struct pwm_chip *chip, |
| 63 |
struct pwm_device *pwm, |
| 64 |
const struct pwm_waveform *wf, |
| 65 |
void *_wfhw) |
| 66 |
{ |
| 67 |
struct stm32_pwm_waveform *wfhw = _wfhw; |
| 68 |
struct stm32_pwm *priv = to_stm32_pwm_dev(chip); |
| 69 |
unsigned int ch = pwm->hwpwm; |
| 70 |
unsigned long rate; |
| 71 |
u64 duty_ticks, offset_ticks; |
| 72 |
int ret; |
| 73 |
|
| 74 |
if (wf->period_length_ns == 0) { |
| 75 |
*wfhw = (struct stm32_pwm_waveform){ |
| 76 |
.ccer = 0, |
| 77 |
}; |
| 78 |
|
| 79 |
return 0; |
| 80 |
} |
| 81 |
|
| 82 |
ret = clk_enable(priv->clk); |
| 83 |
if (ret) |
| 84 |
return ret; |
| 85 |
|
| 86 |
wfhw->ccer = TIM_CCER_CCxE(ch + 1); |
| 87 |
if (priv->have_complementary_output) |
| 88 |
wfhw->ccer |= TIM_CCER_CCxNE(ch + 1); |
| 89 |
|
| 90 |
rate = clk_get_rate(priv->clk); |
| 91 |
|
| 92 |
if (active_channels(priv) & ~TIM_CCER_CCxE(ch + 1)) { |
| 93 |
u64 arr; |
| 94 |
|
| 95 |
/* |
| 96 |
* Other channels are already enabled, so the configured PSC and |
| 97 |
* ARR must be used for this channel, too. |
| 98 |
*/ |
| 99 |
ret = regmap_read(priv->regmap, TIM_PSC, &wfhw->psc); |
| 100 |
if (ret) |
| 101 |
goto out; |
| 102 |
|
| 103 |
ret = regmap_read(priv->regmap, TIM_ARR, &wfhw->arr); |
| 104 |
if (ret) |
| 105 |
goto out; |
| 106 |
|
| 107 |
arr = mul_u64_u64_div_u64(wf->period_length_ns, rate, |
| 108 |
(u64)NSEC_PER_SEC * (wfhw->psc + 1)); |
| 109 |
if (arr <= wfhw->arr) { |
| 110 |
/* |
| 111 |
* requested period is smaller than the currently |
| 112 |
* configured and unchangable period, report back the smallest |
| 113 |
* possible period, i.e. the current state and return 1 |
| 114 |
* to indicate the wrong rounding direction. |
| 115 |
*/ |
| 116 |
ret = 1; |
| 117 |
} |
| 118 |
|
| 119 |
} else { |
| 120 |
/* |
| 121 |
* .probe() asserted that clk_get_rate() is not bigger than 1 GHz, so |
| 122 |
* the calculations here won't overflow. |
| 123 |
* First we need to find the minimal value for prescaler such that |
| 124 |
* |
| 125 |
* period_ns * clkrate |
| 126 |
* ------------------------------ < max_arr + 1 |
| 127 |
* NSEC_PER_SEC * (prescaler + 1) |
| 128 |
* |
| 129 |
* This equation is equivalent to |
| 130 |
* |
| 131 |
* period_ns * clkrate |
| 132 |
* ---------------------------- < prescaler + 1 |
| 133 |
* NSEC_PER_SEC * (max_arr + 1) |
| 134 |
* |
| 135 |
* Using integer division and knowing that the right hand side is |
| 136 |
* integer, this is further equivalent to |
| 137 |
* |
| 138 |
* (period_ns * clkrate) // (NSEC_PER_SEC * (max_arr + 1)) ≤ prescaler |
| 139 |
*/ |
| 140 |
u64 psc = mul_u64_u64_div_u64(wf->period_length_ns, rate, |
| 141 |
(u64)NSEC_PER_SEC * ((u64)priv->max_arr + 1)); |
| 142 |
u64 arr; |
| 143 |
|
| 144 |
wfhw->psc = min_t(u64, psc, MAX_TIM_PSC); |
| 145 |
|
| 146 |
arr = mul_u64_u64_div_u64(wf->period_length_ns, rate, |
| 147 |
(u64)NSEC_PER_SEC * (wfhw->psc + 1)); |
| 148 |
if (!arr) { |
| 149 |
/* |
| 150 |
* requested period is too small, report back the smallest |
| 151 |
* possible period, i.e. ARR = 0. The only valid CCR |
| 152 |
* value is then zero, too. |
| 153 |
*/ |
| 154 |
wfhw->arr = 0; |
| 155 |
wfhw->ccr = 0; |
| 156 |
ret = 1; |
| 157 |
goto out; |
| 158 |
} |
| 159 |
|
| 160 |
/* |
| 161 |
* ARR is limited intentionally to values less than |
| 162 |
* priv->max_arr to allow 100% duty cycle. |
| 163 |
*/ |
| 164 |
wfhw->arr = min_t(u64, arr, priv->max_arr) - 1; |
| 165 |
} |
| 166 |
|
| 167 |
duty_ticks = mul_u64_u64_div_u64(wf->duty_length_ns, rate, |
| 168 |
(u64)NSEC_PER_SEC * (wfhw->psc + 1)); |
| 169 |
duty_ticks = min_t(u64, duty_ticks, wfhw->arr + 1); |
| 170 |
|
| 171 |
offset_ticks = mul_u64_u64_div_u64(wf->duty_offset_ns, rate, |
| 172 |
(u64)NSEC_PER_SEC * (wfhw->psc + 1)); |
| 173 |
offset_ticks = min_t(u64, offset_ticks, wfhw->arr + 1); |
| 174 |
|
| 175 |
if (duty_ticks && offset_ticks && |
| 176 |
duty_ticks + offset_ticks >= wfhw->arr + 1) { |
| 177 |
wfhw->ccer |= TIM_CCER_CCxP(ch + 1); |
| 178 |
if (priv->have_complementary_output) |
| 179 |
wfhw->ccer |= TIM_CCER_CCxNP(ch + 1); |
| 180 |
|
| 181 |
wfhw->ccr = wfhw->arr + 1 - duty_ticks; |
| 182 |
} else { |
| 183 |
wfhw->ccr = duty_ticks; |
| 184 |
} |
| 185 |
|
| 186 |
out: |
| 187 |
dev_dbg(&chip->dev, "pwm#%u: %lld/%lld [+%lld] @%lu -> CCER: %08x, PSC: %08x, ARR: %08x, CCR: %08x\n", |
| 188 |
pwm->hwpwm, wf->duty_length_ns, wf->period_length_ns, wf->duty_offset_ns, |
| 189 |
rate, wfhw->ccer, wfhw->psc, wfhw->arr, wfhw->ccr); |
| 190 |
|
| 191 |
clk_disable(priv->clk); |
| 192 |
|
| 193 |
return ret; |
| 194 |
} |
| 195 |
|
| 196 |
/* |
| 197 |
* This should be moved to lib/math/div64.c. Currently there are some changes |
| 198 |
* pending to mul_u64_u64_div_u64. Uwe will care for that when the dust settles. |
| 199 |
*/ |
| 200 |
static u64 stm32_pwm_mul_u64_u64_div_u64_roundup(u64 a, u64 b, u64 c) |
| 201 |
{ |
| 202 |
u64 res = mul_u64_u64_div_u64(a, b, c); |
| 203 |
/* Those multiplications might overflow but it doesn't matter */ |
| 204 |
u64 rem = a * b - c * res; |
| 205 |
|
| 206 |
if (rem) |
| 207 |
res += 1; |
| 208 |
|
| 209 |
return res; |
| 210 |
} |
| 211 |
|
| 212 |
static int stm32_pwm_round_waveform_fromhw(struct pwm_chip *chip, |
| 213 |
struct pwm_device *pwm, |
| 214 |
const void *_wfhw, |
| 215 |
struct pwm_waveform *wf) |
| 216 |
{ |
| 217 |
const struct stm32_pwm_waveform *wfhw = _wfhw; |
| 218 |
struct stm32_pwm *priv = to_stm32_pwm_dev(chip); |
| 219 |
unsigned long rate = clk_get_rate(priv->clk); |
| 220 |
unsigned int ch = pwm->hwpwm; |
| 221 |
|
| 222 |
if (wfhw->ccer & TIM_CCER_CCxE(ch + 1)) { |
| 223 |
u64 ccr_ns; |
| 224 |
|
| 225 |
/* The result doesn't overflow for rate >= 15259 */ |
| 226 |
wf->period_length_ns = stm32_pwm_mul_u64_u64_div_u64_roundup(((u64)wfhw->psc + 1) * (wfhw->arr + 1), |
| 227 |
NSEC_PER_SEC, rate); |
| 228 |
|
| 229 |
ccr_ns = stm32_pwm_mul_u64_u64_div_u64_roundup(((u64)wfhw->psc + 1) * wfhw->ccr, |
| 230 |
NSEC_PER_SEC, rate); |
| 231 |
|
| 232 |
if (wfhw->ccer & TIM_CCER_CCxP(ch + 1)) { |
| 233 |
wf->duty_length_ns = |
| 234 |
stm32_pwm_mul_u64_u64_div_u64_roundup(((u64)wfhw->psc + 1) * (wfhw->arr + 1 - wfhw->ccr), |
| 235 |
NSEC_PER_SEC, rate); |
| 236 |
|
| 237 |
wf->duty_offset_ns = ccr_ns; |
| 238 |
} else { |
| 239 |
wf->duty_length_ns = ccr_ns; |
| 240 |
wf->duty_offset_ns = 0; |
| 241 |
} |
| 242 |
} else { |
| 243 |
*wf = (struct pwm_waveform){ |
| 244 |
.period_length_ns = 0, |
| 245 |
}; |
| 246 |
} |
| 247 |
|
| 248 |
dev_dbg(&chip->dev, "pwm#%u: CCER: %08x, PSC: %08x, ARR: %08x, CCR: %08x @%lu -> %lld/%lld [+%lld]\n", |
| 249 |
pwm->hwpwm, wfhw->ccer, wfhw->psc, wfhw->arr, wfhw->ccr, rate, |
| 250 |
wf->duty_length_ns, wf->period_length_ns, wf->duty_offset_ns); |
| 251 |
|
| 252 |
return 0; |
| 253 |
} |
| 254 |
|
| 255 |
static int stm32_pwm_read_waveform(struct pwm_chip *chip, |
| 256 |
struct pwm_device *pwm, |
| 257 |
void *_wfhw) |
| 258 |
{ |
| 259 |
struct stm32_pwm_waveform *wfhw = _wfhw; |
| 260 |
struct stm32_pwm *priv = to_stm32_pwm_dev(chip); |
| 261 |
unsigned int ch = pwm->hwpwm; |
| 262 |
int ret; |
| 263 |
|
| 264 |
ret = clk_enable(priv->clk); |
| 265 |
if (ret) |
| 266 |
return ret; |
| 267 |
|
| 268 |
ret = regmap_read(priv->regmap, TIM_CCER, &wfhw->ccer); |
| 269 |
if (ret) |
| 270 |
goto out; |
| 271 |
|
| 272 |
if (wfhw->ccer & TIM_CCER_CCxE(ch + 1)) { |
| 273 |
ret = regmap_read(priv->regmap, TIM_PSC, &wfhw->psc); |
| 274 |
if (ret) |
| 275 |
goto out; |
| 276 |
|
| 277 |
ret = regmap_read(priv->regmap, TIM_ARR, &wfhw->arr); |
| 278 |
if (ret) |
| 279 |
goto out; |
| 280 |
|
| 281 |
if (wfhw->arr == U32_MAX) |
| 282 |
wfhw->arr -= 1; |
| 283 |
|
| 284 |
ret = regmap_read(priv->regmap, TIM_CCRx(ch + 1), &wfhw->ccr); |
| 285 |
if (ret) |
| 286 |
goto out; |
| 287 |
|
| 288 |
if (wfhw->ccr > wfhw->arr + 1) |
| 289 |
wfhw->ccr = wfhw->arr + 1; |
| 290 |
} |
| 291 |
|
| 292 |
out: |
| 293 |
clk_disable(priv->clk); |
| 294 |
|
| 295 |
return ret; |
| 296 |
} |
| 297 |
|
| 298 |
static int stm32_pwm_write_waveform(struct pwm_chip *chip, |
| 299 |
struct pwm_device *pwm, |
| 300 |
const void *_wfhw) |
| 301 |
{ |
| 302 |
const struct stm32_pwm_waveform *wfhw = _wfhw; |
| 303 |
struct stm32_pwm *priv = to_stm32_pwm_dev(chip); |
| 304 |
unsigned int ch = pwm->hwpwm; |
| 305 |
int ret; |
| 306 |
|
| 307 |
ret = clk_enable(priv->clk); |
| 308 |
if (ret) |
| 309 |
return ret; |
| 310 |
|
| 311 |
if (wfhw->ccer & TIM_CCER_CCxE(ch + 1)) { |
| 312 |
u32 ccer, mask; |
| 313 |
unsigned int shift; |
| 314 |
u32 ccmr; |
| 315 |
|
| 316 |
ret = regmap_read(priv->regmap, TIM_CCER, &ccer); |
| 317 |
if (ret) |
| 318 |
goto out; |
| 319 |
|
| 320 |
/* If there are other channels enabled, don't update PSC and ARR */ |
| 321 |
if (ccer & ~TIM_CCER_CCxE(ch + 1) & TIM_CCER_CCXE) { |
| 322 |
u32 psc, arr; |
| 323 |
|
| 324 |
ret = regmap_read(priv->regmap, TIM_PSC, &psc); |
| 325 |
if (ret) |
| 326 |
goto out; |
| 327 |
|
| 328 |
if (psc != wfhw->psc) { |
| 329 |
ret = -EBUSY; |
| 330 |
goto out; |
| 331 |
} |
| 332 |
|
| 333 |
ret = regmap_read(priv->regmap, TIM_ARR, &arr); |
| 334 |
if (ret) |
| 335 |
goto out; |
| 336 |
|
| 337 |
if (arr != wfhw->arr) { |
| 338 |
ret = -EBUSY; |
| 339 |
goto out; |
| 340 |
} |
| 341 |
} else { |
| 342 |
ret = regmap_write(priv->regmap, TIM_PSC, wfhw->psc); |
| 343 |
if (ret) |
| 344 |
goto out; |
| 345 |
|
| 346 |
ret = regmap_write(priv->regmap, TIM_ARR, wfhw->arr); |
| 347 |
if (ret) |
| 348 |
goto out; |
| 349 |
|
| 350 |
ret = regmap_set_bits(priv->regmap, TIM_CR1, TIM_CR1_ARPE); |
| 351 |
if (ret) |
| 352 |
goto out; |
| 353 |
|
| 354 |
} |
| 355 |
|
| 356 |
/* set polarity */ |
| 357 |
mask = TIM_CCER_CCxP(ch + 1) | TIM_CCER_CCxNP(ch + 1); |
| 358 |
ret = regmap_update_bits(priv->regmap, TIM_CCER, mask, wfhw->ccer); |
| 359 |
if (ret) |
| 360 |
goto out; |
| 361 |
|
| 362 |
ret = regmap_write(priv->regmap, TIM_CCRx(ch + 1), wfhw->ccr); |
| 363 |
if (ret) |
| 364 |
goto out; |
| 365 |
|
| 366 |
/* Configure output mode */ |
| 367 |
shift = (ch & 0x1) * CCMR_CHANNEL_SHIFT; |
| 368 |
ccmr = (TIM_CCMR_PE | TIM_CCMR_M1) << shift; |
| 369 |
mask = CCMR_CHANNEL_MASK << shift; |
| 370 |
|
| 371 |
if (ch < 2) |
| 372 |
ret = regmap_update_bits(priv->regmap, TIM_CCMR1, mask, ccmr); |
| 373 |
else |
| 374 |
ret = regmap_update_bits(priv->regmap, TIM_CCMR2, mask, ccmr); |
| 375 |
if (ret) |
| 376 |
goto out; |
| 377 |
|
| 378 |
ret = regmap_set_bits(priv->regmap, TIM_BDTR, TIM_BDTR_MOE); |
| 379 |
if (ret) |
| 380 |
goto out; |
| 381 |
|
| 382 |
if (!(ccer & TIM_CCER_CCxE(ch + 1))) { |
| 383 |
mask = TIM_CCER_CCxE(ch + 1) | TIM_CCER_CCxNE(ch + 1); |
| 384 |
|
| 385 |
ret = clk_enable(priv->clk); |
| 386 |
if (ret) |
| 387 |
goto out; |
| 388 |
|
| 389 |
ccer = (ccer & ~mask) | (wfhw->ccer & mask); |
| 390 |
regmap_write(priv->regmap, TIM_CCER, ccer); |
| 391 |
|
| 392 |
/* Make sure that registers are updated */ |
| 393 |
regmap_set_bits(priv->regmap, TIM_EGR, TIM_EGR_UG); |
| 394 |
|
| 395 |
/* Enable controller */ |
| 396 |
regmap_set_bits(priv->regmap, TIM_CR1, TIM_CR1_CEN); |
| 397 |
} |
| 398 |
|
| 399 |
} else { |
| 400 |
/* disable channel */ |
| 401 |
u32 mask, ccer; |
| 402 |
|
| 403 |
mask = TIM_CCER_CCxE(ch + 1); |
| 404 |
if (priv->have_complementary_output) |
| 405 |
mask |= TIM_CCER_CCxNE(ch + 1); |
| 406 |
|
| 407 |
ret = regmap_read(priv->regmap, TIM_CCER, &ccer); |
| 408 |
if (ret) |
| 409 |
goto out; |
| 410 |
|
| 411 |
if (ccer & mask) { |
| 412 |
ccer = ccer & ~mask; |
| 413 |
|
| 414 |
ret = regmap_write(priv->regmap, TIM_CCER, ccer); |
| 415 |
if (ret) |
| 416 |
goto out; |
| 417 |
|
| 418 |
if (!(ccer & TIM_CCER_CCXE)) { |
| 419 |
/* When all channels are disabled, we can disable the controller */ |
| 420 |
ret = regmap_clear_bits(priv->regmap, TIM_CR1, TIM_CR1_CEN); |
| 421 |
if (ret) |
| 422 |
goto out; |
| 423 |
} |
| 424 |
|
| 425 |
clk_disable(priv->clk); |
| 426 |
} |
| 427 |
} |
| 428 |
|
| 429 |
out: |
| 430 |
clk_disable(priv->clk); |
| 431 |
|
| 432 |
return ret; |
| 433 |
} |
| 434 |
|
| 435 |
#define TIM_CCER_CC12P (TIM_CCER_CC1P | TIM_CCER_CC2P) |
| 436 |
#define TIM_CCER_CC12E (TIM_CCER_CC1E | TIM_CCER_CC2E) |
| 437 |
#define TIM_CCER_CC34P (TIM_CCER_CC3P | TIM_CCER_CC4P) |
| 438 |
#define TIM_CCER_CC34E (TIM_CCER_CC3E | TIM_CCER_CC4E) |
| 439 |
|
| 440 |
/* |
| 441 |
* Capture using PWM input mode: |
| 442 |
* ___ ___ |
| 443 |
* TI[1, 2, 3 or 4]: ........._| |________| |
| 444 |
* ^0 ^1 ^2 |
| 445 |
* . . . |
| 446 |
* . . XXXXX |
| 447 |
* . . XXXXX | |
| 448 |
* . XXXXX . | |
| 449 |
* XXXXX . . | |
| 450 |
* COUNTER: ______XXXXX . . . |_XXX |
| 451 |
* start^ . . . ^stop |
| 452 |
* . . . . |
| 453 |
* v v . v |
| 454 |
* v |
| 455 |
* CCR1/CCR3: tx..........t0...........t2 |
| 456 |
* CCR2/CCR4: tx..............t1......... |
| 457 |
* |
| 458 |
* DMA burst transfer: | | |
| 459 |
* v v |
| 460 |
* DMA buffer: { t0, tx } { t2, t1 } |
| 461 |
* DMA done: ^ |
| 462 |
* |
| 463 |
* 0: IC1/3 snapchot on rising edge: counter value -> CCR1/CCR3 |
| 464 |
* + DMA transfer CCR[1/3] & CCR[2/4] values (t0, tx: doesn't care) |
| 465 |
* 1: IC2/4 snapchot on falling edge: counter value -> CCR2/CCR4 |
| 466 |
* 2: IC1/3 snapchot on rising edge: counter value -> CCR1/CCR3 |
| 467 |
* + DMA transfer CCR[1/3] & CCR[2/4] values (t2, t1) |
| 468 |
* |
| 469 |
* DMA done, compute: |
| 470 |
* - Period = t2 - t0 |
| 471 |
* - Duty cycle = t1 - t0 |
| 472 |
*/ |
| 473 |
static int stm32_pwm_raw_capture(struct pwm_chip *chip, struct pwm_device *pwm, |
| 474 |
unsigned long tmo_ms, u32 *raw_prd, |
| 475 |
u32 *raw_dty) |
| 476 |
{ |
| 477 |
struct stm32_pwm *priv = to_stm32_pwm_dev(chip); |
| 478 |
struct device *parent = pwmchip_parent(chip)->parent; |
| 479 |
enum stm32_timers_dmas dma_id; |
| 480 |
u32 ccen, ccr; |
| 481 |
int ret; |
| 482 |
|
| 483 |
/* Ensure registers have been updated, enable counter and capture */ |
| 484 |
regmap_set_bits(priv->regmap, TIM_EGR, TIM_EGR_UG); |
| 485 |
regmap_set_bits(priv->regmap, TIM_CR1, TIM_CR1_CEN); |
| 486 |
|
| 487 |
/* Use cc1 or cc3 DMA resp for PWM input channels 1 & 2 or 3 & 4 */ |
| 488 |
dma_id = pwm->hwpwm < 2 ? STM32_TIMERS_DMA_CH1 : STM32_TIMERS_DMA_CH3; |
| 489 |
ccen = pwm->hwpwm < 2 ? TIM_CCER_CC12E : TIM_CCER_CC34E; |
| 490 |
ccr = pwm->hwpwm < 2 ? TIM_CCR1 : TIM_CCR3; |
| 491 |
regmap_set_bits(priv->regmap, TIM_CCER, ccen); |
| 492 |
|
| 493 |
/* |
| 494 |
* Timer DMA burst mode. Request 2 registers, 2 bursts, to get both |
| 495 |
* CCR1 & CCR2 (or CCR3 & CCR4) on each capture event. |
| 496 |
* We'll get two capture snapchots: { CCR1, CCR2 }, { CCR1, CCR2 } |
| 497 |
* or { CCR3, CCR4 }, { CCR3, CCR4 } |
| 498 |
*/ |
| 499 |
ret = stm32_timers_dma_burst_read(parent, priv->capture, dma_id, ccr, 2, |
| 500 |
2, tmo_ms); |
| 501 |
if (ret) |
| 502 |
goto stop; |
| 503 |
|
| 504 |
/* Period: t2 - t0 (take care of counter overflow) */ |
| 505 |
if (priv->capture[0] <= priv->capture[2]) |
| 506 |
*raw_prd = priv->capture[2] - priv->capture[0]; |
| 507 |
else |
| 508 |
*raw_prd = priv->max_arr - priv->capture[0] + priv->capture[2]; |
| 509 |
|
| 510 |
/* Duty cycle capture requires at least two capture units */ |
| 511 |
if (pwm->chip->npwm < 2) |
| 512 |
*raw_dty = 0; |
| 513 |
else if (priv->capture[0] <= priv->capture[3]) |
| 514 |
*raw_dty = priv->capture[3] - priv->capture[0]; |
| 515 |
else |
| 516 |
*raw_dty = priv->max_arr - priv->capture[0] + priv->capture[3]; |
| 517 |
|
| 518 |
if (*raw_dty > *raw_prd) { |
| 519 |
/* |
| 520 |
* Race beetween PWM input and DMA: it may happen |
| 521 |
* falling edge triggers new capture on TI2/4 before DMA |
| 522 |
* had a chance to read CCR2/4. It means capture[1] |
| 523 |
* contains period + duty_cycle. So, subtract period. |
| 524 |
*/ |
| 525 |
*raw_dty -= *raw_prd; |
| 526 |
} |
| 527 |
|
| 528 |
stop: |
| 529 |
regmap_clear_bits(priv->regmap, TIM_CCER, ccen); |
| 530 |
regmap_clear_bits(priv->regmap, TIM_CR1, TIM_CR1_CEN); |
| 531 |
|
| 532 |
return ret; |
| 533 |
} |
| 534 |
|
| 535 |
static int stm32_pwm_capture(struct pwm_chip *chip, struct pwm_device *pwm, |
| 536 |
struct pwm_capture *result, unsigned long tmo_ms) |
| 537 |
{ |
| 538 |
struct stm32_pwm *priv = to_stm32_pwm_dev(chip); |
| 539 |
unsigned long long prd, div, dty; |
| 540 |
unsigned long rate; |
| 541 |
unsigned int psc = 0, icpsc, scale; |
| 542 |
u32 raw_prd = 0, raw_dty = 0; |
| 543 |
int ret = 0; |
| 544 |
|
| 545 |
mutex_lock(&priv->lock); |
| 546 |
|
| 547 |
if (active_channels(priv)) { |
| 548 |
ret = -EBUSY; |
| 549 |
goto unlock; |
| 550 |
} |
| 551 |
|
| 552 |
ret = clk_enable(priv->clk); |
| 553 |
if (ret) { |
| 554 |
dev_err(pwmchip_parent(chip), "failed to enable counter clock\n"); |
| 555 |
goto unlock; |
| 556 |
} |
| 557 |
|
| 558 |
rate = clk_get_rate(priv->clk); |
| 559 |
if (!rate) { |
| 560 |
ret = -EINVAL; |
| 561 |
goto clk_dis; |
| 562 |
} |
| 563 |
|
| 564 |
/* prescaler: fit timeout window provided by upper layer */ |
| 565 |
div = (unsigned long long)rate * (unsigned long long)tmo_ms; |
| 566 |
do_div(div, MSEC_PER_SEC); |
| 567 |
prd = div; |
| 568 |
while ((div > priv->max_arr) && (psc < MAX_TIM_PSC)) { |
| 569 |
psc++; |
| 570 |
div = prd; |
| 571 |
do_div(div, psc + 1); |
| 572 |
} |
| 573 |
regmap_write(priv->regmap, TIM_ARR, priv->max_arr); |
| 574 |
regmap_write(priv->regmap, TIM_PSC, psc); |
| 575 |
|
| 576 |
/* Reset input selector to its default input and disable slave mode */ |
| 577 |
regmap_write(priv->regmap, TIM_TISEL, 0x0); |
| 578 |
regmap_write(priv->regmap, TIM_SMCR, 0x0); |
| 579 |
|
| 580 |
/* Map TI1 or TI2 PWM input to IC1 & IC2 (or TI3/4 to IC3 & IC4) */ |
| 581 |
regmap_update_bits(priv->regmap, |
| 582 |
pwm->hwpwm < 2 ? TIM_CCMR1 : TIM_CCMR2, |
| 583 |
TIM_CCMR_CC1S | TIM_CCMR_CC2S, pwm->hwpwm & 0x1 ? |
| 584 |
TIM_CCMR_CC1S_TI2 | TIM_CCMR_CC2S_TI2 : |
| 585 |
TIM_CCMR_CC1S_TI1 | TIM_CCMR_CC2S_TI1); |
| 586 |
|
| 587 |
/* Capture period on IC1/3 rising edge, duty cycle on IC2/4 falling. */ |
| 588 |
regmap_update_bits(priv->regmap, TIM_CCER, pwm->hwpwm < 2 ? |
| 589 |
TIM_CCER_CC12P : TIM_CCER_CC34P, pwm->hwpwm < 2 ? |
| 590 |
TIM_CCER_CC2P : TIM_CCER_CC4P); |
| 591 |
|
| 592 |
ret = stm32_pwm_raw_capture(chip, pwm, tmo_ms, &raw_prd, &raw_dty); |
| 593 |
if (ret) |
| 594 |
goto stop; |
| 595 |
|
| 596 |
/* |
| 597 |
* Got a capture. Try to improve accuracy at high rates: |
| 598 |
* - decrease counter clock prescaler, scale up to max rate. |
| 599 |
* - use input prescaler, capture once every /2 /4 or /8 edges. |
| 600 |
*/ |
| 601 |
if (raw_prd) { |
| 602 |
u32 max_arr = priv->max_arr - 0x1000; /* arbitrary margin */ |
| 603 |
|
| 604 |
scale = max_arr / min(max_arr, raw_prd); |
| 605 |
} else { |
| 606 |
scale = priv->max_arr; /* below resolution, use max scale */ |
| 607 |
} |
| 608 |
|
| 609 |
if (psc && scale > 1) { |
| 610 |
/* 2nd measure with new scale */ |
| 611 |
psc /= scale; |
| 612 |
regmap_write(priv->regmap, TIM_PSC, psc); |
| 613 |
ret = stm32_pwm_raw_capture(chip, pwm, tmo_ms, &raw_prd, |
| 614 |
&raw_dty); |
| 615 |
if (ret) |
| 616 |
goto stop; |
| 617 |
} |
| 618 |
|
| 619 |
/* Compute intermediate period not to exceed timeout at low rates */ |
| 620 |
prd = (unsigned long long)raw_prd * (psc + 1) * NSEC_PER_SEC; |
| 621 |
do_div(prd, rate); |
| 622 |
|
| 623 |
for (icpsc = 0; icpsc < MAX_TIM_ICPSC ; icpsc++) { |
| 624 |
/* input prescaler: also keep arbitrary margin */ |
| 625 |
if (raw_prd >= (priv->max_arr - 0x1000) >> (icpsc + 1)) |
| 626 |
break; |
| 627 |
if (prd >= (tmo_ms * NSEC_PER_MSEC) >> (icpsc + 2)) |
| 628 |
break; |
| 629 |
} |
| 630 |
|
| 631 |
if (!icpsc) |
| 632 |
goto done; |
| 633 |
|
| 634 |
/* Last chance to improve period accuracy, using input prescaler */ |
| 635 |
regmap_update_bits(priv->regmap, |
| 636 |
pwm->hwpwm < 2 ? TIM_CCMR1 : TIM_CCMR2, |
| 637 |
TIM_CCMR_IC1PSC | TIM_CCMR_IC2PSC, |
| 638 |
FIELD_PREP(TIM_CCMR_IC1PSC, icpsc) | |
| 639 |
FIELD_PREP(TIM_CCMR_IC2PSC, icpsc)); |
| 640 |
|
| 641 |
ret = stm32_pwm_raw_capture(chip, pwm, tmo_ms, &raw_prd, &raw_dty); |
| 642 |
if (ret) |
| 643 |
goto stop; |
| 644 |
|
| 645 |
if (raw_dty >= (raw_prd >> icpsc)) { |
| 646 |
/* |
| 647 |
* We may fall here using input prescaler, when input |
| 648 |
* capture starts on high side (before falling edge). |
| 649 |
* Example with icpsc to capture on each 4 events: |
| 650 |
* |
| 651 |
* start 1st capture 2nd capture |
| 652 |
* v v v |
| 653 |
* ___ _____ _____ _____ _____ ____ |
| 654 |
* TI1..4 |__| |__| |__| |__| |__| |
| 655 |
* v v . . . . . v v |
| 656 |
* icpsc1/3: . 0 . 1 . 2 . 3 . 0 |
| 657 |
* icpsc2/4: 0 1 2 3 0 |
| 658 |
* v v v v |
| 659 |
* CCR1/3 ......t0..............................t2 |
| 660 |
* CCR2/4 ..t1..............................t1'... |
| 661 |
* . . . |
| 662 |
* Capture0: .<----------------------------->. |
| 663 |
* Capture1: .<-------------------------->. . |
| 664 |
* . . . |
| 665 |
* Period: .<------> . . |
| 666 |
* Low side: .<>. |
| 667 |
* |
| 668 |
* Result: |
| 669 |
* - Period = Capture0 / icpsc |
| 670 |
* - Duty = Period - Low side = Period - (Capture0 - Capture1) |
| 671 |
*/ |
| 672 |
raw_dty = (raw_prd >> icpsc) - (raw_prd - raw_dty); |
| 673 |
} |
| 674 |
|
| 675 |
done: |
| 676 |
prd = (unsigned long long)raw_prd * (psc + 1) * NSEC_PER_SEC; |
| 677 |
result->period = DIV_ROUND_UP_ULL(prd, rate << icpsc); |
| 678 |
dty = (unsigned long long)raw_dty * (psc + 1) * NSEC_PER_SEC; |
| 679 |
result->duty_cycle = DIV_ROUND_UP_ULL(dty, rate); |
| 680 |
stop: |
| 681 |
regmap_write(priv->regmap, TIM_CCER, 0); |
| 682 |
regmap_write(priv->regmap, pwm->hwpwm < 2 ? TIM_CCMR1 : TIM_CCMR2, 0); |
| 683 |
regmap_write(priv->regmap, TIM_PSC, 0); |
| 684 |
clk_dis: |
| 685 |
clk_disable(priv->clk); |
| 686 |
unlock: |
| 687 |
mutex_unlock(&priv->lock); |
| 688 |
|
| 689 |
return ret; |
| 690 |
} |
| 691 |
|
| 692 |
static const struct pwm_ops stm32pwm_ops = { |
| 693 |
.sizeof_wfhw = sizeof(struct stm32_pwm_waveform), |
| 694 |
.round_waveform_tohw = stm32_pwm_round_waveform_tohw, |
| 695 |
.round_waveform_fromhw = stm32_pwm_round_waveform_fromhw, |
| 696 |
.read_waveform = stm32_pwm_read_waveform, |
| 697 |
.write_waveform = stm32_pwm_write_waveform, |
| 698 |
|
| 699 |
.capture = IS_ENABLED(CONFIG_DMA_ENGINE) ? stm32_pwm_capture : NULL, |
| 700 |
}; |
| 701 |
|
| 702 |
static int stm32_pwm_set_breakinput(struct stm32_pwm *priv, |
| 703 |
const struct stm32_breakinput *bi) |
| 704 |
{ |
| 705 |
u32 shift = TIM_BDTR_BKF_SHIFT(bi->index); |
| 706 |
u32 bke = TIM_BDTR_BKE(bi->index); |
| 707 |
u32 bkp = TIM_BDTR_BKP(bi->index); |
| 708 |
u32 bkf = TIM_BDTR_BKF(bi->index); |
| 709 |
u32 mask = bkf | bkp | bke; |
| 710 |
u32 bdtr; |
| 711 |
|
| 712 |
bdtr = (bi->filter & TIM_BDTR_BKF_MASK) << shift | bke; |
| 713 |
|
| 714 |
if (bi->level) |
| 715 |
bdtr |= bkp; |
| 716 |
|
| 717 |
regmap_update_bits(priv->regmap, TIM_BDTR, mask, bdtr); |
| 718 |
|
| 719 |
regmap_read(priv->regmap, TIM_BDTR, &bdtr); |
| 720 |
|
| 721 |
return (bdtr & bke) ? 0 : -EINVAL; |
| 722 |
} |
| 723 |
|
| 724 |
static int stm32_pwm_apply_breakinputs(struct stm32_pwm *priv) |
| 725 |
{ |
| 726 |
unsigned int i; |
| 727 |
int ret; |
| 728 |
|
| 729 |
for (i = 0; i < priv->num_breakinputs; i++) { |
| 730 |
ret = stm32_pwm_set_breakinput(priv, &priv->breakinputs[i]); |
| 731 |
if (ret < 0) |
| 732 |
return ret; |
| 733 |
} |
| 734 |
|
| 735 |
return 0; |
| 736 |
} |
| 737 |
|
| 738 |
static int stm32_pwm_probe_breakinputs(struct stm32_pwm *priv, |
| 739 |
struct device_node *np) |
| 740 |
{ |
| 741 |
int nb, ret, array_size; |
| 742 |
unsigned int i; |
| 743 |
|
| 744 |
nb = of_property_count_elems_of_size(np, "st,breakinput", |
| 745 |
sizeof(struct stm32_breakinput)); |
| 746 |
|
| 747 |
/* |
| 748 |
* Because "st,breakinput" parameter is optional do not make probe |
| 749 |
* failed if it doesn't exist. |
| 750 |
*/ |
| 751 |
if (nb <= 0) |
| 752 |
return 0; |
| 753 |
|
| 754 |
if (nb > MAX_BREAKINPUT) |
| 755 |
return -EINVAL; |
| 756 |
|
| 757 |
priv->num_breakinputs = nb; |
| 758 |
array_size = nb * sizeof(struct stm32_breakinput) / sizeof(u32); |
| 759 |
ret = of_property_read_u32_array(np, "st,breakinput", |
| 760 |
(u32 *)priv->breakinputs, array_size); |
| 761 |
if (ret) |
| 762 |
return ret; |
| 763 |
|
| 764 |
for (i = 0; i < priv->num_breakinputs; i++) { |
| 765 |
if (priv->breakinputs[i].index > 1 || |
| 766 |
priv->breakinputs[i].level > 1 || |
| 767 |
priv->breakinputs[i].filter > 15) |
| 768 |
return -EINVAL; |
| 769 |
} |
| 770 |
|
| 771 |
return stm32_pwm_apply_breakinputs(priv); |
| 772 |
} |
| 773 |
|
| 774 |
static void stm32_pwm_detect_complementary(struct stm32_pwm *priv, struct stm32_timers *ddata) |
| 775 |
{ |
| 776 |
u32 ccer; |
| 777 |
|
| 778 |
if (ddata->ipidr) { |
| 779 |
u32 val; |
| 780 |
|
| 781 |
/* Simply read from HWCFGR the number of complementary outputs (MP25). */ |
| 782 |
regmap_read(priv->regmap, TIM_HWCFGR1, &val); |
| 783 |
priv->have_complementary_output = !!FIELD_GET(TIM_HWCFGR1_NB_OF_DT, val); |
| 784 |
return; |
| 785 |
} |
| 786 |
|
| 787 |
/* |
| 788 |
* If complementary bit doesn't exist writing 1 will have no |
| 789 |
* effect so we can detect it. |
| 790 |
*/ |
| 791 |
regmap_set_bits(priv->regmap, TIM_CCER, TIM_CCER_CC1NE); |
| 792 |
regmap_read(priv->regmap, TIM_CCER, &ccer); |
| 793 |
regmap_clear_bits(priv->regmap, TIM_CCER, TIM_CCER_CC1NE); |
| 794 |
|
| 795 |
priv->have_complementary_output = (ccer != 0); |
| 796 |
} |
| 797 |
|
| 798 |
static unsigned int stm32_pwm_detect_channels(struct stm32_timers *ddata, |
| 799 |
unsigned int *num_enabled) |
| 800 |
{ |
| 801 |
struct regmap *regmap = ddata->regmap; |
| 802 |
u32 ccer, ccer_backup; |
| 803 |
|
| 804 |
regmap_read(regmap, TIM_CCER, &ccer_backup); |
| 805 |
*num_enabled = hweight32(ccer_backup & TIM_CCER_CCXE); |
| 806 |
|
| 807 |
if (ddata->ipidr) { |
| 808 |
u32 hwcfgr; |
| 809 |
unsigned int npwm; |
| 810 |
|
| 811 |
/* Deduce from HWCFGR the number of outputs (MP25). */ |
| 812 |
regmap_read(regmap, TIM_HWCFGR1, &hwcfgr); |
| 813 |
|
| 814 |
/* |
| 815 |
* Timers may have more capture/compare channels than the |
| 816 |
* actual number of PWM channel outputs (e.g. TIM_CH[1..4]). |
| 817 |
*/ |
| 818 |
npwm = FIELD_GET(TIM_HWCFGR1_NB_OF_CC, hwcfgr); |
| 819 |
|
| 820 |
return npwm < STM32_MAX_PWM_OUTPUT ? npwm : STM32_MAX_PWM_OUTPUT; |
| 821 |
} |
| 822 |
|
| 823 |
/* |
| 824 |
* If channels enable bits don't exist writing 1 will have no |
| 825 |
* effect so we can detect and count them. |
| 826 |
*/ |
| 827 |
regmap_set_bits(regmap, TIM_CCER, TIM_CCER_CCXE); |
| 828 |
regmap_read(regmap, TIM_CCER, &ccer); |
| 829 |
regmap_write(regmap, TIM_CCER, ccer_backup); |
| 830 |
|
| 831 |
return hweight32(ccer & TIM_CCER_CCXE); |
| 832 |
} |
| 833 |
|
| 834 |
static int stm32_pwm_probe(struct platform_device *pdev) |
| 835 |
{ |
| 836 |
struct device *dev = &pdev->dev; |
| 837 |
struct device_node *np = dev->of_node; |
| 838 |
struct stm32_timers *ddata = dev_get_drvdata(pdev->dev.parent); |
| 839 |
struct pwm_chip *chip; |
| 840 |
struct stm32_pwm *priv; |
| 841 |
unsigned int npwm, num_enabled; |
| 842 |
unsigned int i; |
| 843 |
int ret; |
| 844 |
|
| 845 |
npwm = stm32_pwm_detect_channels(ddata, &num_enabled); |
| 846 |
|
| 847 |
chip = devm_pwmchip_alloc(dev, npwm, sizeof(*priv)); |
| 848 |
if (IS_ERR(chip)) |
| 849 |
return PTR_ERR(chip); |
| 850 |
priv = to_stm32_pwm_dev(chip); |
| 851 |
|
| 852 |
mutex_init(&priv->lock); |
| 853 |
priv->regmap = ddata->regmap; |
| 854 |
priv->clk = ddata->clk; |
| 855 |
priv->max_arr = ddata->max_arr; |
| 856 |
|
| 857 |
if (!priv->regmap || !priv->clk) |
| 858 |
return dev_err_probe(dev, -EINVAL, "Failed to get %s\n", |
| 859 |
priv->regmap ? "clk" : "regmap"); |
| 860 |
|
| 861 |
ret = stm32_pwm_probe_breakinputs(priv, np); |
| 862 |
if (ret) |
| 863 |
return dev_err_probe(dev, ret, |
| 864 |
"Failed to configure breakinputs\n"); |
| 865 |
|
| 866 |
stm32_pwm_detect_complementary(priv, ddata); |
| 867 |
|
| 868 |
ret = devm_clk_rate_exclusive_get(dev, priv->clk); |
| 869 |
if (ret) |
| 870 |
return dev_err_probe(dev, ret, "Failed to lock clock\n"); |
| 871 |
|
| 872 |
/* |
| 873 |
* With the clk running with not more than 1 GHz the calculations in |
| 874 |
* .apply() won't overflow. |
| 875 |
*/ |
| 876 |
if (clk_get_rate(priv->clk) > 1000000000) |
| 877 |
return dev_err_probe(dev, -EINVAL, "Clock freq too high (%lu)\n", |
| 878 |
clk_get_rate(priv->clk)); |
| 879 |
|
| 880 |
chip->ops = &stm32pwm_ops; |
| 881 |
|
| 882 |
/* Initialize clock refcount to number of enabled PWM channels. */ |
| 883 |
for (i = 0; i < num_enabled; i++) { |
| 884 |
ret = clk_enable(priv->clk); |
| 885 |
if (ret) |
| 886 |
return ret; |
| 887 |
} |
| 888 |
|
| 889 |
ret = devm_pwmchip_add(dev, chip); |
| 890 |
if (ret < 0) |
| 891 |
return dev_err_probe(dev, ret, |
| 892 |
"Failed to register pwmchip\n"); |
| 893 |
|
| 894 |
platform_set_drvdata(pdev, chip); |
| 895 |
|
| 896 |
return 0; |
| 897 |
} |
| 898 |
|
| 899 |
static int stm32_pwm_suspend(struct device *dev) |
| 900 |
{ |
| 901 |
struct pwm_chip *chip = dev_get_drvdata(dev); |
| 902 |
struct stm32_pwm *priv = to_stm32_pwm_dev(chip); |
| 903 |
unsigned int i; |
| 904 |
u32 ccer, mask; |
| 905 |
|
| 906 |
/* Look for active channels */ |
| 907 |
ccer = active_channels(priv); |
| 908 |
|
| 909 |
for (i = 0; i < chip->npwm; i++) { |
| 910 |
mask = TIM_CCER_CCxE(i + 1); |
| 911 |
if (ccer & mask) { |
| 912 |
dev_err(dev, "PWM %u still in use by consumer %s\n", |
| 913 |
i, chip->pwms[i].label); |
| 914 |
return -EBUSY; |
| 915 |
} |
| 916 |
} |
| 917 |
|
| 918 |
return pinctrl_pm_select_sleep_state(dev); |
| 919 |
} |
| 920 |
|
| 921 |
static int stm32_pwm_resume(struct device *dev) |
| 922 |
{ |
| 923 |
struct pwm_chip *chip = dev_get_drvdata(dev); |
| 924 |
struct stm32_pwm *priv = to_stm32_pwm_dev(chip); |
| 925 |
int ret; |
| 926 |
|
| 927 |
ret = pinctrl_pm_select_default_state(dev); |
| 928 |
if (ret) |
| 929 |
return ret; |
| 930 |
|
| 931 |
/* restore breakinput registers that may have been lost in low power */ |
| 932 |
return stm32_pwm_apply_breakinputs(priv); |
| 933 |
} |
| 934 |
|
| 935 |
static DEFINE_SIMPLE_DEV_PM_OPS(stm32_pwm_pm_ops, stm32_pwm_suspend, stm32_pwm_resume); |
| 936 |
|
| 937 |
static const struct of_device_id stm32_pwm_of_match[] = { |
| 938 |
{ .compatible = "st,stm32-pwm", }, |
| 939 |
{ .compatible = "st,stm32mp25-pwm", }, |
| 940 |
{ /* end node */ }, |
| 941 |
}; |
| 942 |
MODULE_DEVICE_TABLE(of, stm32_pwm_of_match); |
| 943 |
|
| 944 |
static struct platform_driver stm32_pwm_driver = { |
| 945 |
.probe = stm32_pwm_probe, |
| 946 |
.driver = { |
| 947 |
.name = "stm32-pwm", |
| 948 |
.of_match_table = stm32_pwm_of_match, |
| 949 |
.pm = pm_ptr(&stm32_pwm_pm_ops), |
| 950 |
}, |
| 951 |
}; |
| 952 |
module_platform_driver(stm32_pwm_driver); |
| 953 |
|
| 954 |
MODULE_ALIAS("platform:stm32-pwm"); |
| 955 |
MODULE_DESCRIPTION("STMicroelectronics STM32 PWM driver"); |
| 956 |
MODULE_LICENSE("GPL v2"); |