pwm-stm32-lp.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. /*
  2. * STM32 Low-Power Timer PWM driver
  3. *
  4. * Copyright (C) STMicroelectronics 2017
  5. *
  6. * Author: Gerald Baeza <gerald.baeza@st.com>
  7. *
  8. * License terms: GNU General Public License (GPL), version 2
  9. *
  10. * Inspired by Gerald Baeza's pwm-stm32 driver
  11. */
  12. #include <linux/bitfield.h>
  13. #include <linux/mfd/stm32-lptimer.h>
  14. #include <linux/module.h>
  15. #include <linux/of.h>
  16. #include <linux/platform_device.h>
  17. #include <linux/pwm.h>
  18. struct stm32_pwm_lp {
  19. struct pwm_chip chip;
  20. struct clk *clk;
  21. struct regmap *regmap;
  22. };
  23. static inline struct stm32_pwm_lp *to_stm32_pwm_lp(struct pwm_chip *chip)
  24. {
  25. return container_of(chip, struct stm32_pwm_lp, 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_apply(struct pwm_chip *chip, struct pwm_device *pwm,
  30. struct pwm_state *state)
  31. {
  32. struct stm32_pwm_lp *priv = to_stm32_pwm_lp(chip);
  33. unsigned long long prd, div, dty;
  34. struct pwm_state cstate;
  35. u32 val, mask, cfgr, presc = 0;
  36. bool reenable;
  37. int ret;
  38. pwm_get_state(pwm, &cstate);
  39. reenable = !cstate.enabled;
  40. if (!state->enabled) {
  41. if (cstate.enabled) {
  42. /* Disable LP timer */
  43. ret = regmap_write(priv->regmap, STM32_LPTIM_CR, 0);
  44. if (ret)
  45. return ret;
  46. /* disable clock to PWM counter */
  47. clk_disable(priv->clk);
  48. }
  49. return 0;
  50. }
  51. /* Calculate the period and prescaler value */
  52. div = (unsigned long long)clk_get_rate(priv->clk) * state->period;
  53. do_div(div, NSEC_PER_SEC);
  54. if (!div) {
  55. /* Clock is too slow to achieve requested period. */
  56. dev_dbg(priv->chip.dev, "Can't reach %u ns\n", state->period);
  57. return -EINVAL;
  58. }
  59. prd = div;
  60. while (div > STM32_LPTIM_MAX_ARR) {
  61. presc++;
  62. if ((1 << presc) > STM32_LPTIM_MAX_PRESCALER) {
  63. dev_err(priv->chip.dev, "max prescaler exceeded\n");
  64. return -EINVAL;
  65. }
  66. div = prd >> presc;
  67. }
  68. prd = div;
  69. /* Calculate the duty cycle */
  70. dty = prd * state->duty_cycle;
  71. do_div(dty, state->period);
  72. if (!cstate.enabled) {
  73. /* enable clock to drive PWM counter */
  74. ret = clk_enable(priv->clk);
  75. if (ret)
  76. return ret;
  77. }
  78. ret = regmap_read(priv->regmap, STM32_LPTIM_CFGR, &cfgr);
  79. if (ret)
  80. goto err;
  81. if ((FIELD_GET(STM32_LPTIM_PRESC, cfgr) != presc) ||
  82. (FIELD_GET(STM32_LPTIM_WAVPOL, cfgr) != state->polarity)) {
  83. val = FIELD_PREP(STM32_LPTIM_PRESC, presc);
  84. val |= FIELD_PREP(STM32_LPTIM_WAVPOL, state->polarity);
  85. mask = STM32_LPTIM_PRESC | STM32_LPTIM_WAVPOL;
  86. /* Must disable LP timer to modify CFGR */
  87. reenable = true;
  88. ret = regmap_write(priv->regmap, STM32_LPTIM_CR, 0);
  89. if (ret)
  90. goto err;
  91. ret = regmap_update_bits(priv->regmap, STM32_LPTIM_CFGR, mask,
  92. val);
  93. if (ret)
  94. goto err;
  95. }
  96. if (reenable) {
  97. /* Must (re)enable LP timer to modify CMP & ARR */
  98. ret = regmap_write(priv->regmap, STM32_LPTIM_CR,
  99. STM32_LPTIM_ENABLE);
  100. if (ret)
  101. goto err;
  102. }
  103. ret = regmap_write(priv->regmap, STM32_LPTIM_ARR, prd - 1);
  104. if (ret)
  105. goto err;
  106. ret = regmap_write(priv->regmap, STM32_LPTIM_CMP, prd - (1 + dty));
  107. if (ret)
  108. goto err;
  109. /* ensure CMP & ARR registers are properly written */
  110. ret = regmap_read_poll_timeout(priv->regmap, STM32_LPTIM_ISR, val,
  111. (val & STM32_LPTIM_CMPOK_ARROK),
  112. 100, 1000);
  113. if (ret) {
  114. dev_err(priv->chip.dev, "ARR/CMP registers write issue\n");
  115. goto err;
  116. }
  117. ret = regmap_write(priv->regmap, STM32_LPTIM_ICR,
  118. STM32_LPTIM_CMPOKCF_ARROKCF);
  119. if (ret)
  120. goto err;
  121. if (reenable) {
  122. /* Start LP timer in continuous mode */
  123. ret = regmap_update_bits(priv->regmap, STM32_LPTIM_CR,
  124. STM32_LPTIM_CNTSTRT,
  125. STM32_LPTIM_CNTSTRT);
  126. if (ret) {
  127. regmap_write(priv->regmap, STM32_LPTIM_CR, 0);
  128. goto err;
  129. }
  130. }
  131. return 0;
  132. err:
  133. if (!cstate.enabled)
  134. clk_disable(priv->clk);
  135. return ret;
  136. }
  137. static void stm32_pwm_lp_get_state(struct pwm_chip *chip,
  138. struct pwm_device *pwm,
  139. struct pwm_state *state)
  140. {
  141. struct stm32_pwm_lp *priv = to_stm32_pwm_lp(chip);
  142. unsigned long rate = clk_get_rate(priv->clk);
  143. u32 val, presc, prd;
  144. u64 tmp;
  145. regmap_read(priv->regmap, STM32_LPTIM_CR, &val);
  146. state->enabled = !!FIELD_GET(STM32_LPTIM_ENABLE, val);
  147. /* Keep PWM counter clock refcount in sync with PWM initial state */
  148. if (state->enabled)
  149. clk_enable(priv->clk);
  150. regmap_read(priv->regmap, STM32_LPTIM_CFGR, &val);
  151. presc = FIELD_GET(STM32_LPTIM_PRESC, val);
  152. state->polarity = FIELD_GET(STM32_LPTIM_WAVPOL, val);
  153. regmap_read(priv->regmap, STM32_LPTIM_ARR, &prd);
  154. tmp = prd + 1;
  155. tmp = (tmp << presc) * NSEC_PER_SEC;
  156. state->period = DIV_ROUND_CLOSEST_ULL(tmp, rate);
  157. regmap_read(priv->regmap, STM32_LPTIM_CMP, &val);
  158. tmp = prd - val;
  159. tmp = (tmp << presc) * NSEC_PER_SEC;
  160. state->duty_cycle = DIV_ROUND_CLOSEST_ULL(tmp, rate);
  161. }
  162. static const struct pwm_ops stm32_pwm_lp_ops = {
  163. .owner = THIS_MODULE,
  164. .apply = stm32_pwm_lp_apply,
  165. .get_state = stm32_pwm_lp_get_state,
  166. };
  167. static int stm32_pwm_lp_probe(struct platform_device *pdev)
  168. {
  169. struct stm32_lptimer *ddata = dev_get_drvdata(pdev->dev.parent);
  170. struct stm32_pwm_lp *priv;
  171. int ret;
  172. priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
  173. if (!priv)
  174. return -ENOMEM;
  175. priv->regmap = ddata->regmap;
  176. priv->clk = ddata->clk;
  177. priv->chip.base = -1;
  178. priv->chip.dev = &pdev->dev;
  179. priv->chip.ops = &stm32_pwm_lp_ops;
  180. priv->chip.npwm = 1;
  181. ret = pwmchip_add(&priv->chip);
  182. if (ret < 0)
  183. return ret;
  184. platform_set_drvdata(pdev, priv);
  185. return 0;
  186. }
  187. static int stm32_pwm_lp_remove(struct platform_device *pdev)
  188. {
  189. struct stm32_pwm_lp *priv = platform_get_drvdata(pdev);
  190. unsigned int i;
  191. for (i = 0; i < priv->chip.npwm; i++)
  192. if (pwm_is_enabled(&priv->chip.pwms[i]))
  193. pwm_disable(&priv->chip.pwms[i]);
  194. return pwmchip_remove(&priv->chip);
  195. }
  196. static const struct of_device_id stm32_pwm_lp_of_match[] = {
  197. { .compatible = "st,stm32-pwm-lp", },
  198. {},
  199. };
  200. MODULE_DEVICE_TABLE(of, stm32_pwm_lp_of_match);
  201. static struct platform_driver stm32_pwm_lp_driver = {
  202. .probe = stm32_pwm_lp_probe,
  203. .remove = stm32_pwm_lp_remove,
  204. .driver = {
  205. .name = "stm32-pwm-lp",
  206. .of_match_table = of_match_ptr(stm32_pwm_lp_of_match),
  207. },
  208. };
  209. module_platform_driver(stm32_pwm_lp_driver);
  210. MODULE_ALIAS("platform:stm32-pwm-lp");
  211. MODULE_DESCRIPTION("STMicroelectronics STM32 PWM LP driver");
  212. MODULE_LICENSE("GPL v2");