pwm-stm32.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  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 clk *clk;
  21. struct regmap *regmap;
  22. u32 max_arr;
  23. bool have_complementary_output;
  24. };
  25. struct stm32_breakinput {
  26. u32 index;
  27. u32 level;
  28. u32 filter;
  29. };
  30. static inline struct stm32_pwm *to_stm32_pwm_dev(struct pwm_chip *chip)
  31. {
  32. return container_of(chip, struct stm32_pwm, chip);
  33. }
  34. static u32 active_channels(struct stm32_pwm *dev)
  35. {
  36. u32 ccer;
  37. regmap_read(dev->regmap, TIM_CCER, &ccer);
  38. return ccer & TIM_CCER_CCXE;
  39. }
  40. static int write_ccrx(struct stm32_pwm *dev, int ch, u32 value)
  41. {
  42. switch (ch) {
  43. case 0:
  44. return regmap_write(dev->regmap, TIM_CCR1, value);
  45. case 1:
  46. return regmap_write(dev->regmap, TIM_CCR2, value);
  47. case 2:
  48. return regmap_write(dev->regmap, TIM_CCR3, value);
  49. case 3:
  50. return regmap_write(dev->regmap, TIM_CCR4, value);
  51. }
  52. return -EINVAL;
  53. }
  54. static int stm32_pwm_config(struct stm32_pwm *priv, int ch,
  55. int duty_ns, int period_ns)
  56. {
  57. unsigned long long prd, div, dty;
  58. unsigned int prescaler = 0;
  59. u32 ccmr, mask, shift;
  60. /* Period and prescaler values depends on clock rate */
  61. div = (unsigned long long)clk_get_rate(priv->clk) * period_ns;
  62. do_div(div, NSEC_PER_SEC);
  63. prd = div;
  64. while (div > priv->max_arr) {
  65. prescaler++;
  66. div = prd;
  67. do_div(div, prescaler + 1);
  68. }
  69. prd = div;
  70. if (prescaler > MAX_TIM_PSC)
  71. return -EINVAL;
  72. /*
  73. * All channels share the same prescaler and counter so when two
  74. * channels are active at the same time we can't change them
  75. */
  76. if (active_channels(priv) & ~(1 << ch * 4)) {
  77. u32 psc, arr;
  78. regmap_read(priv->regmap, TIM_PSC, &psc);
  79. regmap_read(priv->regmap, TIM_ARR, &arr);
  80. if ((psc != prescaler) || (arr != prd - 1))
  81. return -EBUSY;
  82. }
  83. regmap_write(priv->regmap, TIM_PSC, prescaler);
  84. regmap_write(priv->regmap, TIM_ARR, prd - 1);
  85. regmap_update_bits(priv->regmap, TIM_CR1, TIM_CR1_ARPE, TIM_CR1_ARPE);
  86. /* Calculate the duty cycles */
  87. dty = prd * duty_ns;
  88. do_div(dty, period_ns);
  89. write_ccrx(priv, ch, dty);
  90. /* Configure output mode */
  91. shift = (ch & 0x1) * CCMR_CHANNEL_SHIFT;
  92. ccmr = (TIM_CCMR_PE | TIM_CCMR_M1) << shift;
  93. mask = CCMR_CHANNEL_MASK << shift;
  94. if (ch < 2)
  95. regmap_update_bits(priv->regmap, TIM_CCMR1, mask, ccmr);
  96. else
  97. regmap_update_bits(priv->regmap, TIM_CCMR2, mask, ccmr);
  98. regmap_update_bits(priv->regmap, TIM_BDTR,
  99. TIM_BDTR_MOE | TIM_BDTR_AOE,
  100. TIM_BDTR_MOE | TIM_BDTR_AOE);
  101. return 0;
  102. }
  103. static int stm32_pwm_set_polarity(struct stm32_pwm *priv, int ch,
  104. enum pwm_polarity polarity)
  105. {
  106. u32 mask;
  107. mask = TIM_CCER_CC1P << (ch * 4);
  108. if (priv->have_complementary_output)
  109. mask |= TIM_CCER_CC1NP << (ch * 4);
  110. regmap_update_bits(priv->regmap, TIM_CCER, mask,
  111. polarity == PWM_POLARITY_NORMAL ? 0 : mask);
  112. return 0;
  113. }
  114. static int stm32_pwm_enable(struct stm32_pwm *priv, int ch)
  115. {
  116. u32 mask;
  117. int ret;
  118. ret = clk_enable(priv->clk);
  119. if (ret)
  120. return ret;
  121. /* Enable channel */
  122. mask = TIM_CCER_CC1E << (ch * 4);
  123. if (priv->have_complementary_output)
  124. mask |= TIM_CCER_CC1NE << (ch * 4);
  125. regmap_update_bits(priv->regmap, TIM_CCER, mask, mask);
  126. /* Make sure that registers are updated */
  127. regmap_update_bits(priv->regmap, TIM_EGR, TIM_EGR_UG, TIM_EGR_UG);
  128. /* Enable controller */
  129. regmap_update_bits(priv->regmap, TIM_CR1, TIM_CR1_CEN, TIM_CR1_CEN);
  130. return 0;
  131. }
  132. static void stm32_pwm_disable(struct stm32_pwm *priv, int ch)
  133. {
  134. u32 mask;
  135. /* Disable channel */
  136. mask = TIM_CCER_CC1E << (ch * 4);
  137. if (priv->have_complementary_output)
  138. mask |= TIM_CCER_CC1NE << (ch * 4);
  139. regmap_update_bits(priv->regmap, TIM_CCER, mask, 0);
  140. /* When all channels are disabled, we can disable the controller */
  141. if (!active_channels(priv))
  142. regmap_update_bits(priv->regmap, TIM_CR1, TIM_CR1_CEN, 0);
  143. clk_disable(priv->clk);
  144. }
  145. static int stm32_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm,
  146. struct pwm_state *state)
  147. {
  148. bool enabled;
  149. struct stm32_pwm *priv = to_stm32_pwm_dev(chip);
  150. int ret;
  151. enabled = pwm->state.enabled;
  152. if (enabled && !state->enabled) {
  153. stm32_pwm_disable(priv, pwm->hwpwm);
  154. return 0;
  155. }
  156. if (state->polarity != pwm->state.polarity)
  157. stm32_pwm_set_polarity(priv, pwm->hwpwm, state->polarity);
  158. ret = stm32_pwm_config(priv, pwm->hwpwm,
  159. state->duty_cycle, state->period);
  160. if (ret)
  161. return ret;
  162. if (!enabled && state->enabled)
  163. ret = stm32_pwm_enable(priv, pwm->hwpwm);
  164. return ret;
  165. }
  166. static const struct pwm_ops stm32pwm_ops = {
  167. .owner = THIS_MODULE,
  168. .apply = stm32_pwm_apply,
  169. };
  170. static int stm32_pwm_set_breakinput(struct stm32_pwm *priv,
  171. int index, int level, int filter)
  172. {
  173. u32 bke = (index == 0) ? TIM_BDTR_BKE : TIM_BDTR_BK2E;
  174. int shift = (index == 0) ? TIM_BDTR_BKF_SHIFT : TIM_BDTR_BK2F_SHIFT;
  175. u32 mask = (index == 0) ? TIM_BDTR_BKE | TIM_BDTR_BKP | TIM_BDTR_BKF
  176. : TIM_BDTR_BK2E | TIM_BDTR_BK2P | TIM_BDTR_BK2F;
  177. u32 bdtr = bke;
  178. /*
  179. * The both bits could be set since only one will be wrote
  180. * due to mask value.
  181. */
  182. if (level)
  183. bdtr |= TIM_BDTR_BKP | TIM_BDTR_BK2P;
  184. bdtr |= (filter & TIM_BDTR_BKF_MASK) << shift;
  185. regmap_update_bits(priv->regmap, TIM_BDTR, mask, bdtr);
  186. regmap_read(priv->regmap, TIM_BDTR, &bdtr);
  187. return (bdtr & bke) ? 0 : -EINVAL;
  188. }
  189. static int stm32_pwm_apply_breakinputs(struct stm32_pwm *priv,
  190. struct device_node *np)
  191. {
  192. struct stm32_breakinput breakinput[MAX_BREAKINPUT];
  193. int nb, ret, i, array_size;
  194. nb = of_property_count_elems_of_size(np, "st,breakinput",
  195. sizeof(struct stm32_breakinput));
  196. /*
  197. * Because "st,breakinput" parameter is optional do not make probe
  198. * failed if it doesn't exist.
  199. */
  200. if (nb <= 0)
  201. return 0;
  202. if (nb > MAX_BREAKINPUT)
  203. return -EINVAL;
  204. array_size = nb * sizeof(struct stm32_breakinput) / sizeof(u32);
  205. ret = of_property_read_u32_array(np, "st,breakinput",
  206. (u32 *)breakinput, array_size);
  207. if (ret)
  208. return ret;
  209. for (i = 0; i < nb && !ret; i++) {
  210. ret = stm32_pwm_set_breakinput(priv,
  211. breakinput[i].index,
  212. breakinput[i].level,
  213. breakinput[i].filter);
  214. }
  215. return ret;
  216. }
  217. static void stm32_pwm_detect_complementary(struct stm32_pwm *priv)
  218. {
  219. u32 ccer;
  220. /*
  221. * If complementary bit doesn't exist writing 1 will have no
  222. * effect so we can detect it.
  223. */
  224. regmap_update_bits(priv->regmap,
  225. TIM_CCER, TIM_CCER_CC1NE, TIM_CCER_CC1NE);
  226. regmap_read(priv->regmap, TIM_CCER, &ccer);
  227. regmap_update_bits(priv->regmap, TIM_CCER, TIM_CCER_CC1NE, 0);
  228. priv->have_complementary_output = (ccer != 0);
  229. }
  230. static int stm32_pwm_detect_channels(struct stm32_pwm *priv)
  231. {
  232. u32 ccer;
  233. int npwm = 0;
  234. /*
  235. * If channels enable bits don't exist writing 1 will have no
  236. * effect so we can detect and count them.
  237. */
  238. regmap_update_bits(priv->regmap,
  239. TIM_CCER, TIM_CCER_CCXE, TIM_CCER_CCXE);
  240. regmap_read(priv->regmap, TIM_CCER, &ccer);
  241. regmap_update_bits(priv->regmap, TIM_CCER, TIM_CCER_CCXE, 0);
  242. if (ccer & TIM_CCER_CC1E)
  243. npwm++;
  244. if (ccer & TIM_CCER_CC2E)
  245. npwm++;
  246. if (ccer & TIM_CCER_CC3E)
  247. npwm++;
  248. if (ccer & TIM_CCER_CC4E)
  249. npwm++;
  250. return npwm;
  251. }
  252. static int stm32_pwm_probe(struct platform_device *pdev)
  253. {
  254. struct device *dev = &pdev->dev;
  255. struct device_node *np = dev->of_node;
  256. struct stm32_timers *ddata = dev_get_drvdata(pdev->dev.parent);
  257. struct stm32_pwm *priv;
  258. int ret;
  259. priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
  260. if (!priv)
  261. return -ENOMEM;
  262. priv->regmap = ddata->regmap;
  263. priv->clk = ddata->clk;
  264. priv->max_arr = ddata->max_arr;
  265. if (!priv->regmap || !priv->clk)
  266. return -EINVAL;
  267. ret = stm32_pwm_apply_breakinputs(priv, np);
  268. if (ret)
  269. return ret;
  270. stm32_pwm_detect_complementary(priv);
  271. priv->chip.base = -1;
  272. priv->chip.dev = dev;
  273. priv->chip.ops = &stm32pwm_ops;
  274. priv->chip.npwm = stm32_pwm_detect_channels(priv);
  275. ret = pwmchip_add(&priv->chip);
  276. if (ret < 0)
  277. return ret;
  278. platform_set_drvdata(pdev, priv);
  279. return 0;
  280. }
  281. static int stm32_pwm_remove(struct platform_device *pdev)
  282. {
  283. struct stm32_pwm *priv = platform_get_drvdata(pdev);
  284. unsigned int i;
  285. for (i = 0; i < priv->chip.npwm; i++)
  286. pwm_disable(&priv->chip.pwms[i]);
  287. pwmchip_remove(&priv->chip);
  288. return 0;
  289. }
  290. static const struct of_device_id stm32_pwm_of_match[] = {
  291. { .compatible = "st,stm32-pwm", },
  292. { /* end node */ },
  293. };
  294. MODULE_DEVICE_TABLE(of, stm32_pwm_of_match);
  295. static struct platform_driver stm32_pwm_driver = {
  296. .probe = stm32_pwm_probe,
  297. .remove = stm32_pwm_remove,
  298. .driver = {
  299. .name = "stm32-pwm",
  300. .of_match_table = stm32_pwm_of_match,
  301. },
  302. };
  303. module_platform_driver(stm32_pwm_driver);
  304. MODULE_ALIAS("platform:stm32-pwm");
  305. MODULE_DESCRIPTION("STMicroelectronics STM32 PWM driver");
  306. MODULE_LICENSE("GPL v2");