pwm-stm32-lp.c 11 KB

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