pwm-stm32.c 8.9 KB

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