pwm-stm32.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) STMicroelectronics 2016
  4. *
  5. * Author: Gerald Baeza <gerald.baeza@st.com>
  6. *
  7. * Inspired by timer-stm32.c from Maxime Coquelin
  8. * pwm-atmel.c from Bo Shen
  9. */
  10. #include <linux/mfd/stm32-timers.h>
  11. #include <linux/module.h>
  12. #include <linux/of.h>
  13. #include <linux/platform_device.h>
  14. #include <linux/pwm.h>
  15. #define CCMR_CHANNEL_SHIFT 8
  16. #define CCMR_CHANNEL_MASK 0xFF
  17. #define MAX_BREAKINPUT 2
  18. struct stm32_pwm {
  19. struct pwm_chip chip;
  20. struct mutex lock; /* protect pwm config/enable */
  21. struct clk *clk;
  22. struct regmap *regmap;
  23. u32 max_arr;
  24. bool have_complementary_output;
  25. u32 capture[4] ____cacheline_aligned; /* DMA'able buffer */
  26. };
  27. struct stm32_breakinput {
  28. u32 index;
  29. u32 level;
  30. u32 filter;
  31. };
  32. static inline struct stm32_pwm *to_stm32_pwm_dev(struct pwm_chip *chip)
  33. {
  34. return container_of(chip, struct stm32_pwm, chip);
  35. }
  36. static u32 active_channels(struct stm32_pwm *dev)
  37. {
  38. u32 ccer;
  39. regmap_read(dev->regmap, TIM_CCER, &ccer);
  40. return ccer & TIM_CCER_CCXE;
  41. }
  42. static int write_ccrx(struct stm32_pwm *dev, int ch, u32 value)
  43. {
  44. switch (ch) {
  45. case 0:
  46. return regmap_write(dev->regmap, TIM_CCR1, value);
  47. case 1:
  48. return regmap_write(dev->regmap, TIM_CCR2, value);
  49. case 2:
  50. return regmap_write(dev->regmap, TIM_CCR3, value);
  51. case 3:
  52. return regmap_write(dev->regmap, TIM_CCR4, value);
  53. }
  54. return -EINVAL;
  55. }
  56. #define TIM_CCER_CC12P (TIM_CCER_CC1P | TIM_CCER_CC2P)
  57. #define TIM_CCER_CC12E (TIM_CCER_CC1E | TIM_CCER_CC2E)
  58. #define TIM_CCER_CC34P (TIM_CCER_CC3P | TIM_CCER_CC4P)
  59. #define TIM_CCER_CC34E (TIM_CCER_CC3E | TIM_CCER_CC4E)
  60. /*
  61. * Capture using PWM input mode:
  62. * ___ ___
  63. * TI[1, 2, 3 or 4]: ........._| |________|
  64. * ^0 ^1 ^2
  65. * . . .
  66. * . . XXXXX
  67. * . . XXXXX |
  68. * . XXXXX . |
  69. * XXXXX . . |
  70. * COUNTER: ______XXXXX . . . |_XXX
  71. * start^ . . . ^stop
  72. * . . . .
  73. * v v . v
  74. * v
  75. * CCR1/CCR3: tx..........t0...........t2
  76. * CCR2/CCR4: tx..............t1.........
  77. *
  78. * DMA burst transfer: | |
  79. * v v
  80. * DMA buffer: { t0, tx } { t2, t1 }
  81. * DMA done: ^
  82. *
  83. * 0: IC1/3 snapchot on rising edge: counter value -> CCR1/CCR3
  84. * + DMA transfer CCR[1/3] & CCR[2/4] values (t0, tx: doesn't care)
  85. * 1: IC2/4 snapchot on falling edge: counter value -> CCR2/CCR4
  86. * 2: IC1/3 snapchot on rising edge: counter value -> CCR1/CCR3
  87. * + DMA transfer CCR[1/3] & CCR[2/4] values (t2, t1)
  88. *
  89. * DMA done, compute:
  90. * - Period = t2 - t0
  91. * - Duty cycle = t1 - t0
  92. */
  93. static int stm32_pwm_raw_capture(struct stm32_pwm *priv, struct pwm_device *pwm,
  94. unsigned long tmo_ms, u32 *raw_prd,
  95. u32 *raw_dty)
  96. {
  97. struct device *parent = priv->chip.dev->parent;
  98. enum stm32_timers_dmas dma_id;
  99. u32 ccen, ccr;
  100. int ret;
  101. /* Ensure registers have been updated, enable counter and capture */
  102. regmap_update_bits(priv->regmap, TIM_EGR, TIM_EGR_UG, TIM_EGR_UG);
  103. regmap_update_bits(priv->regmap, TIM_CR1, TIM_CR1_CEN, TIM_CR1_CEN);
  104. /* Use cc1 or cc3 DMA resp for PWM input channels 1 & 2 or 3 & 4 */
  105. dma_id = pwm->hwpwm < 2 ? STM32_TIMERS_DMA_CH1 : STM32_TIMERS_DMA_CH3;
  106. ccen = pwm->hwpwm < 2 ? TIM_CCER_CC12E : TIM_CCER_CC34E;
  107. ccr = pwm->hwpwm < 2 ? TIM_CCR1 : TIM_CCR3;
  108. regmap_update_bits(priv->regmap, TIM_CCER, ccen, ccen);
  109. /*
  110. * Timer DMA burst mode. Request 2 registers, 2 bursts, to get both
  111. * CCR1 & CCR2 (or CCR3 & CCR4) on each capture event.
  112. * We'll get two capture snapchots: { CCR1, CCR2 }, { CCR1, CCR2 }
  113. * or { CCR3, CCR4 }, { CCR3, CCR4 }
  114. */
  115. ret = stm32_timers_dma_burst_read(parent, priv->capture, dma_id, ccr, 2,
  116. 2, tmo_ms);
  117. if (ret)
  118. goto stop;
  119. /* Period: t2 - t0 (take care of counter overflow) */
  120. if (priv->capture[0] <= priv->capture[2])
  121. *raw_prd = priv->capture[2] - priv->capture[0];
  122. else
  123. *raw_prd = priv->max_arr - priv->capture[0] + priv->capture[2];
  124. /* Duty cycle capture requires at least two capture units */
  125. if (pwm->chip->npwm < 2)
  126. *raw_dty = 0;
  127. else if (priv->capture[0] <= priv->capture[3])
  128. *raw_dty = priv->capture[3] - priv->capture[0];
  129. else
  130. *raw_dty = priv->max_arr - priv->capture[0] + priv->capture[3];
  131. if (*raw_dty > *raw_prd) {
  132. /*
  133. * Race beetween PWM input and DMA: it may happen
  134. * falling edge triggers new capture on TI2/4 before DMA
  135. * had a chance to read CCR2/4. It means capture[1]
  136. * contains period + duty_cycle. So, subtract period.
  137. */
  138. *raw_dty -= *raw_prd;
  139. }
  140. stop:
  141. regmap_update_bits(priv->regmap, TIM_CCER, ccen, 0);
  142. regmap_update_bits(priv->regmap, TIM_CR1, TIM_CR1_CEN, 0);
  143. return ret;
  144. }
  145. static int stm32_pwm_capture(struct pwm_chip *chip, struct pwm_device *pwm,
  146. struct pwm_capture *result, unsigned long tmo_ms)
  147. {
  148. struct stm32_pwm *priv = to_stm32_pwm_dev(chip);
  149. unsigned long long prd, div, dty;
  150. unsigned long rate;
  151. unsigned int psc = 0;
  152. u32 raw_prd, raw_dty;
  153. int ret = 0;
  154. mutex_lock(&priv->lock);
  155. if (active_channels(priv)) {
  156. ret = -EBUSY;
  157. goto unlock;
  158. }
  159. ret = clk_enable(priv->clk);
  160. if (ret) {
  161. dev_err(priv->chip.dev, "failed to enable counter clock\n");
  162. goto unlock;
  163. }
  164. rate = clk_get_rate(priv->clk);
  165. if (!rate) {
  166. ret = -EINVAL;
  167. goto clk_dis;
  168. }
  169. /* prescaler: fit timeout window provided by upper layer */
  170. div = (unsigned long long)rate * (unsigned long long)tmo_ms;
  171. do_div(div, MSEC_PER_SEC);
  172. prd = div;
  173. while ((div > priv->max_arr) && (psc < MAX_TIM_PSC)) {
  174. psc++;
  175. div = prd;
  176. do_div(div, psc + 1);
  177. }
  178. regmap_write(priv->regmap, TIM_ARR, priv->max_arr);
  179. regmap_write(priv->regmap, TIM_PSC, psc);
  180. /* Map TI1 or TI2 PWM input to IC1 & IC2 (or TI3/4 to IC3 & IC4) */
  181. regmap_update_bits(priv->regmap,
  182. pwm->hwpwm < 2 ? TIM_CCMR1 : TIM_CCMR2,
  183. TIM_CCMR_CC1S | TIM_CCMR_CC2S, pwm->hwpwm & 0x1 ?
  184. TIM_CCMR_CC1S_TI2 | TIM_CCMR_CC2S_TI2 :
  185. TIM_CCMR_CC1S_TI1 | TIM_CCMR_CC2S_TI1);
  186. /* Capture period on IC1/3 rising edge, duty cycle on IC2/4 falling. */
  187. regmap_update_bits(priv->regmap, TIM_CCER, pwm->hwpwm < 2 ?
  188. TIM_CCER_CC12P : TIM_CCER_CC34P, pwm->hwpwm < 2 ?
  189. TIM_CCER_CC2P : TIM_CCER_CC4P);
  190. ret = stm32_pwm_raw_capture(priv, pwm, tmo_ms, &raw_prd, &raw_dty);
  191. if (ret)
  192. goto stop;
  193. prd = (unsigned long long)raw_prd * (psc + 1) * NSEC_PER_SEC;
  194. result->period = DIV_ROUND_UP_ULL(prd, rate);
  195. dty = (unsigned long long)raw_dty * (psc + 1) * NSEC_PER_SEC;
  196. result->duty_cycle = DIV_ROUND_UP_ULL(dty, rate);
  197. stop:
  198. regmap_write(priv->regmap, TIM_CCER, 0);
  199. regmap_write(priv->regmap, pwm->hwpwm < 2 ? TIM_CCMR1 : TIM_CCMR2, 0);
  200. regmap_write(priv->regmap, TIM_PSC, 0);
  201. clk_dis:
  202. clk_disable(priv->clk);
  203. unlock:
  204. mutex_unlock(&priv->lock);
  205. return ret;
  206. }
  207. static int stm32_pwm_config(struct stm32_pwm *priv, int ch,
  208. int duty_ns, int period_ns)
  209. {
  210. unsigned long long prd, div, dty;
  211. unsigned int prescaler = 0;
  212. u32 ccmr, mask, shift;
  213. /* Period and prescaler values depends on clock rate */
  214. div = (unsigned long long)clk_get_rate(priv->clk) * period_ns;
  215. do_div(div, NSEC_PER_SEC);
  216. prd = div;
  217. while (div > priv->max_arr) {
  218. prescaler++;
  219. div = prd;
  220. do_div(div, prescaler + 1);
  221. }
  222. prd = div;
  223. if (prescaler > MAX_TIM_PSC)
  224. return -EINVAL;
  225. /*
  226. * All channels share the same prescaler and counter so when two
  227. * channels are active at the same time we can't change them
  228. */
  229. if (active_channels(priv) & ~(1 << ch * 4)) {
  230. u32 psc, arr;
  231. regmap_read(priv->regmap, TIM_PSC, &psc);
  232. regmap_read(priv->regmap, TIM_ARR, &arr);
  233. if ((psc != prescaler) || (arr != prd - 1))
  234. return -EBUSY;
  235. }
  236. regmap_write(priv->regmap, TIM_PSC, prescaler);
  237. regmap_write(priv->regmap, TIM_ARR, prd - 1);
  238. regmap_update_bits(priv->regmap, TIM_CR1, TIM_CR1_ARPE, TIM_CR1_ARPE);
  239. /* Calculate the duty cycles */
  240. dty = prd * duty_ns;
  241. do_div(dty, period_ns);
  242. write_ccrx(priv, ch, dty);
  243. /* Configure output mode */
  244. shift = (ch & 0x1) * CCMR_CHANNEL_SHIFT;
  245. ccmr = (TIM_CCMR_PE | TIM_CCMR_M1) << shift;
  246. mask = CCMR_CHANNEL_MASK << shift;
  247. if (ch < 2)
  248. regmap_update_bits(priv->regmap, TIM_CCMR1, mask, ccmr);
  249. else
  250. regmap_update_bits(priv->regmap, TIM_CCMR2, mask, ccmr);
  251. regmap_update_bits(priv->regmap, TIM_BDTR,
  252. TIM_BDTR_MOE | TIM_BDTR_AOE,
  253. TIM_BDTR_MOE | TIM_BDTR_AOE);
  254. return 0;
  255. }
  256. static int stm32_pwm_set_polarity(struct stm32_pwm *priv, int ch,
  257. enum pwm_polarity polarity)
  258. {
  259. u32 mask;
  260. mask = TIM_CCER_CC1P << (ch * 4);
  261. if (priv->have_complementary_output)
  262. mask |= TIM_CCER_CC1NP << (ch * 4);
  263. regmap_update_bits(priv->regmap, TIM_CCER, mask,
  264. polarity == PWM_POLARITY_NORMAL ? 0 : mask);
  265. return 0;
  266. }
  267. static int stm32_pwm_enable(struct stm32_pwm *priv, int ch)
  268. {
  269. u32 mask;
  270. int ret;
  271. ret = clk_enable(priv->clk);
  272. if (ret)
  273. return ret;
  274. /* Enable channel */
  275. mask = TIM_CCER_CC1E << (ch * 4);
  276. if (priv->have_complementary_output)
  277. mask |= TIM_CCER_CC1NE << (ch * 4);
  278. regmap_update_bits(priv->regmap, TIM_CCER, mask, mask);
  279. /* Make sure that registers are updated */
  280. regmap_update_bits(priv->regmap, TIM_EGR, TIM_EGR_UG, TIM_EGR_UG);
  281. /* Enable controller */
  282. regmap_update_bits(priv->regmap, TIM_CR1, TIM_CR1_CEN, TIM_CR1_CEN);
  283. return 0;
  284. }
  285. static void stm32_pwm_disable(struct stm32_pwm *priv, int ch)
  286. {
  287. u32 mask;
  288. /* Disable channel */
  289. mask = TIM_CCER_CC1E << (ch * 4);
  290. if (priv->have_complementary_output)
  291. mask |= TIM_CCER_CC1NE << (ch * 4);
  292. regmap_update_bits(priv->regmap, TIM_CCER, mask, 0);
  293. /* When all channels are disabled, we can disable the controller */
  294. if (!active_channels(priv))
  295. regmap_update_bits(priv->regmap, TIM_CR1, TIM_CR1_CEN, 0);
  296. clk_disable(priv->clk);
  297. }
  298. static int stm32_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm,
  299. struct pwm_state *state)
  300. {
  301. bool enabled;
  302. struct stm32_pwm *priv = to_stm32_pwm_dev(chip);
  303. int ret;
  304. enabled = pwm->state.enabled;
  305. if (enabled && !state->enabled) {
  306. stm32_pwm_disable(priv, pwm->hwpwm);
  307. return 0;
  308. }
  309. if (state->polarity != pwm->state.polarity)
  310. stm32_pwm_set_polarity(priv, pwm->hwpwm, state->polarity);
  311. ret = stm32_pwm_config(priv, pwm->hwpwm,
  312. state->duty_cycle, state->period);
  313. if (ret)
  314. return ret;
  315. if (!enabled && state->enabled)
  316. ret = stm32_pwm_enable(priv, pwm->hwpwm);
  317. return ret;
  318. }
  319. static int stm32_pwm_apply_locked(struct pwm_chip *chip, struct pwm_device *pwm,
  320. struct pwm_state *state)
  321. {
  322. struct stm32_pwm *priv = to_stm32_pwm_dev(chip);
  323. int ret;
  324. /* protect common prescaler for all active channels */
  325. mutex_lock(&priv->lock);
  326. ret = stm32_pwm_apply(chip, pwm, state);
  327. mutex_unlock(&priv->lock);
  328. return ret;
  329. }
  330. static const struct pwm_ops stm32pwm_ops = {
  331. .owner = THIS_MODULE,
  332. .apply = stm32_pwm_apply_locked,
  333. #if IS_ENABLED(CONFIG_DMA_ENGINE)
  334. .capture = stm32_pwm_capture,
  335. #endif
  336. };
  337. static int stm32_pwm_set_breakinput(struct stm32_pwm *priv,
  338. int index, int level, int filter)
  339. {
  340. u32 bke = (index == 0) ? TIM_BDTR_BKE : TIM_BDTR_BK2E;
  341. int shift = (index == 0) ? TIM_BDTR_BKF_SHIFT : TIM_BDTR_BK2F_SHIFT;
  342. u32 mask = (index == 0) ? TIM_BDTR_BKE | TIM_BDTR_BKP | TIM_BDTR_BKF
  343. : TIM_BDTR_BK2E | TIM_BDTR_BK2P | TIM_BDTR_BK2F;
  344. u32 bdtr = bke;
  345. /*
  346. * The both bits could be set since only one will be wrote
  347. * due to mask value.
  348. */
  349. if (level)
  350. bdtr |= TIM_BDTR_BKP | TIM_BDTR_BK2P;
  351. bdtr |= (filter & TIM_BDTR_BKF_MASK) << shift;
  352. regmap_update_bits(priv->regmap, TIM_BDTR, mask, bdtr);
  353. regmap_read(priv->regmap, TIM_BDTR, &bdtr);
  354. return (bdtr & bke) ? 0 : -EINVAL;
  355. }
  356. static int stm32_pwm_apply_breakinputs(struct stm32_pwm *priv,
  357. struct device_node *np)
  358. {
  359. struct stm32_breakinput breakinput[MAX_BREAKINPUT];
  360. int nb, ret, i, array_size;
  361. nb = of_property_count_elems_of_size(np, "st,breakinput",
  362. sizeof(struct stm32_breakinput));
  363. /*
  364. * Because "st,breakinput" parameter is optional do not make probe
  365. * failed if it doesn't exist.
  366. */
  367. if (nb <= 0)
  368. return 0;
  369. if (nb > MAX_BREAKINPUT)
  370. return -EINVAL;
  371. array_size = nb * sizeof(struct stm32_breakinput) / sizeof(u32);
  372. ret = of_property_read_u32_array(np, "st,breakinput",
  373. (u32 *)breakinput, array_size);
  374. if (ret)
  375. return ret;
  376. for (i = 0; i < nb && !ret; i++) {
  377. ret = stm32_pwm_set_breakinput(priv,
  378. breakinput[i].index,
  379. breakinput[i].level,
  380. breakinput[i].filter);
  381. }
  382. return ret;
  383. }
  384. static void stm32_pwm_detect_complementary(struct stm32_pwm *priv)
  385. {
  386. u32 ccer;
  387. /*
  388. * If complementary bit doesn't exist writing 1 will have no
  389. * effect so we can detect it.
  390. */
  391. regmap_update_bits(priv->regmap,
  392. TIM_CCER, TIM_CCER_CC1NE, TIM_CCER_CC1NE);
  393. regmap_read(priv->regmap, TIM_CCER, &ccer);
  394. regmap_update_bits(priv->regmap, TIM_CCER, TIM_CCER_CC1NE, 0);
  395. priv->have_complementary_output = (ccer != 0);
  396. }
  397. static int stm32_pwm_detect_channels(struct stm32_pwm *priv)
  398. {
  399. u32 ccer;
  400. int npwm = 0;
  401. /*
  402. * If channels enable bits don't exist writing 1 will have no
  403. * effect so we can detect and count them.
  404. */
  405. regmap_update_bits(priv->regmap,
  406. TIM_CCER, TIM_CCER_CCXE, TIM_CCER_CCXE);
  407. regmap_read(priv->regmap, TIM_CCER, &ccer);
  408. regmap_update_bits(priv->regmap, TIM_CCER, TIM_CCER_CCXE, 0);
  409. if (ccer & TIM_CCER_CC1E)
  410. npwm++;
  411. if (ccer & TIM_CCER_CC2E)
  412. npwm++;
  413. if (ccer & TIM_CCER_CC3E)
  414. npwm++;
  415. if (ccer & TIM_CCER_CC4E)
  416. npwm++;
  417. return npwm;
  418. }
  419. static int stm32_pwm_probe(struct platform_device *pdev)
  420. {
  421. struct device *dev = &pdev->dev;
  422. struct device_node *np = dev->of_node;
  423. struct stm32_timers *ddata = dev_get_drvdata(pdev->dev.parent);
  424. struct stm32_pwm *priv;
  425. int ret;
  426. priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
  427. if (!priv)
  428. return -ENOMEM;
  429. mutex_init(&priv->lock);
  430. priv->regmap = ddata->regmap;
  431. priv->clk = ddata->clk;
  432. priv->max_arr = ddata->max_arr;
  433. if (!priv->regmap || !priv->clk)
  434. return -EINVAL;
  435. ret = stm32_pwm_apply_breakinputs(priv, np);
  436. if (ret)
  437. return ret;
  438. stm32_pwm_detect_complementary(priv);
  439. priv->chip.base = -1;
  440. priv->chip.dev = dev;
  441. priv->chip.ops = &stm32pwm_ops;
  442. priv->chip.npwm = stm32_pwm_detect_channels(priv);
  443. ret = pwmchip_add(&priv->chip);
  444. if (ret < 0)
  445. return ret;
  446. platform_set_drvdata(pdev, priv);
  447. return 0;
  448. }
  449. static int stm32_pwm_remove(struct platform_device *pdev)
  450. {
  451. struct stm32_pwm *priv = platform_get_drvdata(pdev);
  452. unsigned int i;
  453. for (i = 0; i < priv->chip.npwm; i++)
  454. pwm_disable(&priv->chip.pwms[i]);
  455. pwmchip_remove(&priv->chip);
  456. return 0;
  457. }
  458. static const struct of_device_id stm32_pwm_of_match[] = {
  459. { .compatible = "st,stm32-pwm", },
  460. { /* end node */ },
  461. };
  462. MODULE_DEVICE_TABLE(of, stm32_pwm_of_match);
  463. static struct platform_driver stm32_pwm_driver = {
  464. .probe = stm32_pwm_probe,
  465. .remove = stm32_pwm_remove,
  466. .driver = {
  467. .name = "stm32-pwm",
  468. .of_match_table = stm32_pwm_of_match,
  469. },
  470. };
  471. module_platform_driver(stm32_pwm_driver);
  472. MODULE_ALIAS("platform:stm32-pwm");
  473. MODULE_DESCRIPTION("STMicroelectronics STM32 PWM driver");
  474. MODULE_LICENSE("GPL v2");