pwm-stm32.c 8.9 KB

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