pwm-stm32.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666
  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/bitfield.h>
  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 mutex lock; /* protect pwm config/enable */
  22. struct clk *clk;
  23. struct regmap *regmap;
  24. u32 max_arr;
  25. bool have_complementary_output;
  26. u32 capture[4] ____cacheline_aligned; /* DMA'able buffer */
  27. };
  28. struct stm32_breakinput {
  29. u32 index;
  30. u32 level;
  31. u32 filter;
  32. };
  33. static inline struct stm32_pwm *to_stm32_pwm_dev(struct pwm_chip *chip)
  34. {
  35. return container_of(chip, struct stm32_pwm, chip);
  36. }
  37. static u32 active_channels(struct stm32_pwm *dev)
  38. {
  39. u32 ccer;
  40. regmap_read(dev->regmap, TIM_CCER, &ccer);
  41. return ccer & TIM_CCER_CCXE;
  42. }
  43. static int write_ccrx(struct stm32_pwm *dev, int ch, u32 value)
  44. {
  45. switch (ch) {
  46. case 0:
  47. return regmap_write(dev->regmap, TIM_CCR1, value);
  48. case 1:
  49. return regmap_write(dev->regmap, TIM_CCR2, value);
  50. case 2:
  51. return regmap_write(dev->regmap, TIM_CCR3, value);
  52. case 3:
  53. return regmap_write(dev->regmap, TIM_CCR4, value);
  54. }
  55. return -EINVAL;
  56. }
  57. #define TIM_CCER_CC12P (TIM_CCER_CC1P | TIM_CCER_CC2P)
  58. #define TIM_CCER_CC12E (TIM_CCER_CC1E | TIM_CCER_CC2E)
  59. #define TIM_CCER_CC34P (TIM_CCER_CC3P | TIM_CCER_CC4P)
  60. #define TIM_CCER_CC34E (TIM_CCER_CC3E | TIM_CCER_CC4E)
  61. /*
  62. * Capture using PWM input mode:
  63. * ___ ___
  64. * TI[1, 2, 3 or 4]: ........._| |________|
  65. * ^0 ^1 ^2
  66. * . . .
  67. * . . XXXXX
  68. * . . XXXXX |
  69. * . XXXXX . |
  70. * XXXXX . . |
  71. * COUNTER: ______XXXXX . . . |_XXX
  72. * start^ . . . ^stop
  73. * . . . .
  74. * v v . v
  75. * v
  76. * CCR1/CCR3: tx..........t0...........t2
  77. * CCR2/CCR4: tx..............t1.........
  78. *
  79. * DMA burst transfer: | |
  80. * v v
  81. * DMA buffer: { t0, tx } { t2, t1 }
  82. * DMA done: ^
  83. *
  84. * 0: IC1/3 snapchot on rising edge: counter value -> CCR1/CCR3
  85. * + DMA transfer CCR[1/3] & CCR[2/4] values (t0, tx: doesn't care)
  86. * 1: IC2/4 snapchot on falling edge: counter value -> CCR2/CCR4
  87. * 2: IC1/3 snapchot on rising edge: counter value -> CCR1/CCR3
  88. * + DMA transfer CCR[1/3] & CCR[2/4] values (t2, t1)
  89. *
  90. * DMA done, compute:
  91. * - Period = t2 - t0
  92. * - Duty cycle = t1 - t0
  93. */
  94. static int stm32_pwm_raw_capture(struct stm32_pwm *priv, struct pwm_device *pwm,
  95. unsigned long tmo_ms, u32 *raw_prd,
  96. u32 *raw_dty)
  97. {
  98. struct device *parent = priv->chip.dev->parent;
  99. enum stm32_timers_dmas dma_id;
  100. u32 ccen, ccr;
  101. int ret;
  102. /* Ensure registers have been updated, enable counter and capture */
  103. regmap_update_bits(priv->regmap, TIM_EGR, TIM_EGR_UG, TIM_EGR_UG);
  104. regmap_update_bits(priv->regmap, TIM_CR1, TIM_CR1_CEN, TIM_CR1_CEN);
  105. /* Use cc1 or cc3 DMA resp for PWM input channels 1 & 2 or 3 & 4 */
  106. dma_id = pwm->hwpwm < 2 ? STM32_TIMERS_DMA_CH1 : STM32_TIMERS_DMA_CH3;
  107. ccen = pwm->hwpwm < 2 ? TIM_CCER_CC12E : TIM_CCER_CC34E;
  108. ccr = pwm->hwpwm < 2 ? TIM_CCR1 : TIM_CCR3;
  109. regmap_update_bits(priv->regmap, TIM_CCER, ccen, ccen);
  110. /*
  111. * Timer DMA burst mode. Request 2 registers, 2 bursts, to get both
  112. * CCR1 & CCR2 (or CCR3 & CCR4) on each capture event.
  113. * We'll get two capture snapchots: { CCR1, CCR2 }, { CCR1, CCR2 }
  114. * or { CCR3, CCR4 }, { CCR3, CCR4 }
  115. */
  116. ret = stm32_timers_dma_burst_read(parent, priv->capture, dma_id, ccr, 2,
  117. 2, tmo_ms);
  118. if (ret)
  119. goto stop;
  120. /* Period: t2 - t0 (take care of counter overflow) */
  121. if (priv->capture[0] <= priv->capture[2])
  122. *raw_prd = priv->capture[2] - priv->capture[0];
  123. else
  124. *raw_prd = priv->max_arr - priv->capture[0] + priv->capture[2];
  125. /* Duty cycle capture requires at least two capture units */
  126. if (pwm->chip->npwm < 2)
  127. *raw_dty = 0;
  128. else if (priv->capture[0] <= priv->capture[3])
  129. *raw_dty = priv->capture[3] - priv->capture[0];
  130. else
  131. *raw_dty = priv->max_arr - priv->capture[0] + priv->capture[3];
  132. if (*raw_dty > *raw_prd) {
  133. /*
  134. * Race beetween PWM input and DMA: it may happen
  135. * falling edge triggers new capture on TI2/4 before DMA
  136. * had a chance to read CCR2/4. It means capture[1]
  137. * contains period + duty_cycle. So, subtract period.
  138. */
  139. *raw_dty -= *raw_prd;
  140. }
  141. stop:
  142. regmap_update_bits(priv->regmap, TIM_CCER, ccen, 0);
  143. regmap_update_bits(priv->regmap, TIM_CR1, TIM_CR1_CEN, 0);
  144. return ret;
  145. }
  146. static int stm32_pwm_capture(struct pwm_chip *chip, struct pwm_device *pwm,
  147. struct pwm_capture *result, unsigned long tmo_ms)
  148. {
  149. struct stm32_pwm *priv = to_stm32_pwm_dev(chip);
  150. unsigned long long prd, div, dty;
  151. unsigned long rate;
  152. unsigned int psc = 0, icpsc, scale;
  153. u32 raw_prd = 0, raw_dty = 0;
  154. int ret = 0;
  155. mutex_lock(&priv->lock);
  156. if (active_channels(priv)) {
  157. ret = -EBUSY;
  158. goto unlock;
  159. }
  160. ret = clk_enable(priv->clk);
  161. if (ret) {
  162. dev_err(priv->chip.dev, "failed to enable counter clock\n");
  163. goto unlock;
  164. }
  165. rate = clk_get_rate(priv->clk);
  166. if (!rate) {
  167. ret = -EINVAL;
  168. goto clk_dis;
  169. }
  170. /* prescaler: fit timeout window provided by upper layer */
  171. div = (unsigned long long)rate * (unsigned long long)tmo_ms;
  172. do_div(div, MSEC_PER_SEC);
  173. prd = div;
  174. while ((div > priv->max_arr) && (psc < MAX_TIM_PSC)) {
  175. psc++;
  176. div = prd;
  177. do_div(div, psc + 1);
  178. }
  179. regmap_write(priv->regmap, TIM_ARR, priv->max_arr);
  180. regmap_write(priv->regmap, TIM_PSC, psc);
  181. /* Map TI1 or TI2 PWM input to IC1 & IC2 (or TI3/4 to IC3 & IC4) */
  182. regmap_update_bits(priv->regmap,
  183. pwm->hwpwm < 2 ? TIM_CCMR1 : TIM_CCMR2,
  184. TIM_CCMR_CC1S | TIM_CCMR_CC2S, pwm->hwpwm & 0x1 ?
  185. TIM_CCMR_CC1S_TI2 | TIM_CCMR_CC2S_TI2 :
  186. TIM_CCMR_CC1S_TI1 | TIM_CCMR_CC2S_TI1);
  187. /* Capture period on IC1/3 rising edge, duty cycle on IC2/4 falling. */
  188. regmap_update_bits(priv->regmap, TIM_CCER, pwm->hwpwm < 2 ?
  189. TIM_CCER_CC12P : TIM_CCER_CC34P, pwm->hwpwm < 2 ?
  190. TIM_CCER_CC2P : TIM_CCER_CC4P);
  191. ret = stm32_pwm_raw_capture(priv, pwm, tmo_ms, &raw_prd, &raw_dty);
  192. if (ret)
  193. goto stop;
  194. /*
  195. * Got a capture. Try to improve accuracy at high rates:
  196. * - decrease counter clock prescaler, scale up to max rate.
  197. * - use input prescaler, capture once every /2 /4 or /8 edges.
  198. */
  199. if (raw_prd) {
  200. u32 max_arr = priv->max_arr - 0x1000; /* arbitrary margin */
  201. scale = max_arr / min(max_arr, raw_prd);
  202. } else {
  203. scale = priv->max_arr; /* bellow resolution, use max scale */
  204. }
  205. if (psc && scale > 1) {
  206. /* 2nd measure with new scale */
  207. psc /= scale;
  208. regmap_write(priv->regmap, TIM_PSC, psc);
  209. ret = stm32_pwm_raw_capture(priv, pwm, tmo_ms, &raw_prd,
  210. &raw_dty);
  211. if (ret)
  212. goto stop;
  213. }
  214. /* Compute intermediate period not to exceed timeout at low rates */
  215. prd = (unsigned long long)raw_prd * (psc + 1) * NSEC_PER_SEC;
  216. do_div(prd, rate);
  217. for (icpsc = 0; icpsc < MAX_TIM_ICPSC ; icpsc++) {
  218. /* input prescaler: also keep arbitrary margin */
  219. if (raw_prd >= (priv->max_arr - 0x1000) >> (icpsc + 1))
  220. break;
  221. if (prd >= (tmo_ms * NSEC_PER_MSEC) >> (icpsc + 2))
  222. break;
  223. }
  224. if (!icpsc)
  225. goto done;
  226. /* Last chance to improve period accuracy, using input prescaler */
  227. regmap_update_bits(priv->regmap,
  228. pwm->hwpwm < 2 ? TIM_CCMR1 : TIM_CCMR2,
  229. TIM_CCMR_IC1PSC | TIM_CCMR_IC2PSC,
  230. FIELD_PREP(TIM_CCMR_IC1PSC, icpsc) |
  231. FIELD_PREP(TIM_CCMR_IC2PSC, icpsc));
  232. ret = stm32_pwm_raw_capture(priv, pwm, tmo_ms, &raw_prd, &raw_dty);
  233. if (ret)
  234. goto stop;
  235. if (raw_dty >= (raw_prd >> icpsc)) {
  236. /*
  237. * We may fall here using input prescaler, when input
  238. * capture starts on high side (before falling edge).
  239. * Example with icpsc to capture on each 4 events:
  240. *
  241. * start 1st capture 2nd capture
  242. * v v v
  243. * ___ _____ _____ _____ _____ ____
  244. * TI1..4 |__| |__| |__| |__| |__|
  245. * v v . . . . . v v
  246. * icpsc1/3: . 0 . 1 . 2 . 3 . 0
  247. * icpsc2/4: 0 1 2 3 0
  248. * v v v v
  249. * CCR1/3 ......t0..............................t2
  250. * CCR2/4 ..t1..............................t1'...
  251. * . . .
  252. * Capture0: .<----------------------------->.
  253. * Capture1: .<-------------------------->. .
  254. * . . .
  255. * Period: .<------> . .
  256. * Low side: .<>.
  257. *
  258. * Result:
  259. * - Period = Capture0 / icpsc
  260. * - Duty = Period - Low side = Period - (Capture0 - Capture1)
  261. */
  262. raw_dty = (raw_prd >> icpsc) - (raw_prd - raw_dty);
  263. }
  264. done:
  265. prd = (unsigned long long)raw_prd * (psc + 1) * NSEC_PER_SEC;
  266. result->period = DIV_ROUND_UP_ULL(prd, rate << icpsc);
  267. dty = (unsigned long long)raw_dty * (psc + 1) * NSEC_PER_SEC;
  268. result->duty_cycle = DIV_ROUND_UP_ULL(dty, rate);
  269. stop:
  270. regmap_write(priv->regmap, TIM_CCER, 0);
  271. regmap_write(priv->regmap, pwm->hwpwm < 2 ? TIM_CCMR1 : TIM_CCMR2, 0);
  272. regmap_write(priv->regmap, TIM_PSC, 0);
  273. clk_dis:
  274. clk_disable(priv->clk);
  275. unlock:
  276. mutex_unlock(&priv->lock);
  277. return ret;
  278. }
  279. static int stm32_pwm_config(struct stm32_pwm *priv, int ch,
  280. int duty_ns, int period_ns)
  281. {
  282. unsigned long long prd, div, dty;
  283. unsigned int prescaler = 0;
  284. u32 ccmr, mask, shift;
  285. /* Period and prescaler values depends on clock rate */
  286. div = (unsigned long long)clk_get_rate(priv->clk) * period_ns;
  287. do_div(div, NSEC_PER_SEC);
  288. prd = div;
  289. while (div > priv->max_arr) {
  290. prescaler++;
  291. div = prd;
  292. do_div(div, prescaler + 1);
  293. }
  294. prd = div;
  295. if (prescaler > MAX_TIM_PSC)
  296. return -EINVAL;
  297. /*
  298. * All channels share the same prescaler and counter so when two
  299. * channels are active at the same time we can't change them
  300. */
  301. if (active_channels(priv) & ~(1 << ch * 4)) {
  302. u32 psc, arr;
  303. regmap_read(priv->regmap, TIM_PSC, &psc);
  304. regmap_read(priv->regmap, TIM_ARR, &arr);
  305. if ((psc != prescaler) || (arr != prd - 1))
  306. return -EBUSY;
  307. }
  308. regmap_write(priv->regmap, TIM_PSC, prescaler);
  309. regmap_write(priv->regmap, TIM_ARR, prd - 1);
  310. regmap_update_bits(priv->regmap, TIM_CR1, TIM_CR1_ARPE, TIM_CR1_ARPE);
  311. /* Calculate the duty cycles */
  312. dty = prd * duty_ns;
  313. do_div(dty, period_ns);
  314. write_ccrx(priv, ch, dty);
  315. /* Configure output mode */
  316. shift = (ch & 0x1) * CCMR_CHANNEL_SHIFT;
  317. ccmr = (TIM_CCMR_PE | TIM_CCMR_M1) << shift;
  318. mask = CCMR_CHANNEL_MASK << shift;
  319. if (ch < 2)
  320. regmap_update_bits(priv->regmap, TIM_CCMR1, mask, ccmr);
  321. else
  322. regmap_update_bits(priv->regmap, TIM_CCMR2, mask, ccmr);
  323. regmap_update_bits(priv->regmap, TIM_BDTR,
  324. TIM_BDTR_MOE | TIM_BDTR_AOE,
  325. TIM_BDTR_MOE | TIM_BDTR_AOE);
  326. return 0;
  327. }
  328. static int stm32_pwm_set_polarity(struct stm32_pwm *priv, int ch,
  329. enum pwm_polarity polarity)
  330. {
  331. u32 mask;
  332. mask = TIM_CCER_CC1P << (ch * 4);
  333. if (priv->have_complementary_output)
  334. mask |= TIM_CCER_CC1NP << (ch * 4);
  335. regmap_update_bits(priv->regmap, TIM_CCER, mask,
  336. polarity == PWM_POLARITY_NORMAL ? 0 : mask);
  337. return 0;
  338. }
  339. static int stm32_pwm_enable(struct stm32_pwm *priv, int ch)
  340. {
  341. u32 mask;
  342. int ret;
  343. ret = clk_enable(priv->clk);
  344. if (ret)
  345. return ret;
  346. /* Enable channel */
  347. mask = TIM_CCER_CC1E << (ch * 4);
  348. if (priv->have_complementary_output)
  349. mask |= TIM_CCER_CC1NE << (ch * 4);
  350. regmap_update_bits(priv->regmap, TIM_CCER, mask, mask);
  351. /* Make sure that registers are updated */
  352. regmap_update_bits(priv->regmap, TIM_EGR, TIM_EGR_UG, TIM_EGR_UG);
  353. /* Enable controller */
  354. regmap_update_bits(priv->regmap, TIM_CR1, TIM_CR1_CEN, TIM_CR1_CEN);
  355. return 0;
  356. }
  357. static void stm32_pwm_disable(struct stm32_pwm *priv, int ch)
  358. {
  359. u32 mask;
  360. /* Disable channel */
  361. mask = TIM_CCER_CC1E << (ch * 4);
  362. if (priv->have_complementary_output)
  363. mask |= TIM_CCER_CC1NE << (ch * 4);
  364. regmap_update_bits(priv->regmap, TIM_CCER, mask, 0);
  365. /* When all channels are disabled, we can disable the controller */
  366. if (!active_channels(priv))
  367. regmap_update_bits(priv->regmap, TIM_CR1, TIM_CR1_CEN, 0);
  368. clk_disable(priv->clk);
  369. }
  370. static int stm32_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm,
  371. struct pwm_state *state)
  372. {
  373. bool enabled;
  374. struct stm32_pwm *priv = to_stm32_pwm_dev(chip);
  375. int ret;
  376. enabled = pwm->state.enabled;
  377. if (enabled && !state->enabled) {
  378. stm32_pwm_disable(priv, pwm->hwpwm);
  379. return 0;
  380. }
  381. if (state->polarity != pwm->state.polarity)
  382. stm32_pwm_set_polarity(priv, pwm->hwpwm, state->polarity);
  383. ret = stm32_pwm_config(priv, pwm->hwpwm,
  384. state->duty_cycle, state->period);
  385. if (ret)
  386. return ret;
  387. if (!enabled && state->enabled)
  388. ret = stm32_pwm_enable(priv, pwm->hwpwm);
  389. return ret;
  390. }
  391. static int stm32_pwm_apply_locked(struct pwm_chip *chip, struct pwm_device *pwm,
  392. struct pwm_state *state)
  393. {
  394. struct stm32_pwm *priv = to_stm32_pwm_dev(chip);
  395. int ret;
  396. /* protect common prescaler for all active channels */
  397. mutex_lock(&priv->lock);
  398. ret = stm32_pwm_apply(chip, pwm, state);
  399. mutex_unlock(&priv->lock);
  400. return ret;
  401. }
  402. static const struct pwm_ops stm32pwm_ops = {
  403. .owner = THIS_MODULE,
  404. .apply = stm32_pwm_apply_locked,
  405. .capture = IS_ENABLED(CONFIG_DMA_ENGINE) ? stm32_pwm_capture : NULL,
  406. };
  407. static int stm32_pwm_set_breakinput(struct stm32_pwm *priv,
  408. int index, int level, int filter)
  409. {
  410. u32 bke = (index == 0) ? TIM_BDTR_BKE : TIM_BDTR_BK2E;
  411. int shift = (index == 0) ? TIM_BDTR_BKF_SHIFT : TIM_BDTR_BK2F_SHIFT;
  412. u32 mask = (index == 0) ? TIM_BDTR_BKE | TIM_BDTR_BKP | TIM_BDTR_BKF
  413. : TIM_BDTR_BK2E | TIM_BDTR_BK2P | TIM_BDTR_BK2F;
  414. u32 bdtr = bke;
  415. /*
  416. * The both bits could be set since only one will be wrote
  417. * due to mask value.
  418. */
  419. if (level)
  420. bdtr |= TIM_BDTR_BKP | TIM_BDTR_BK2P;
  421. bdtr |= (filter & TIM_BDTR_BKF_MASK) << shift;
  422. regmap_update_bits(priv->regmap, TIM_BDTR, mask, bdtr);
  423. regmap_read(priv->regmap, TIM_BDTR, &bdtr);
  424. return (bdtr & bke) ? 0 : -EINVAL;
  425. }
  426. static int stm32_pwm_apply_breakinputs(struct stm32_pwm *priv,
  427. struct device_node *np)
  428. {
  429. struct stm32_breakinput breakinput[MAX_BREAKINPUT];
  430. int nb, ret, i, array_size;
  431. nb = of_property_count_elems_of_size(np, "st,breakinput",
  432. sizeof(struct stm32_breakinput));
  433. /*
  434. * Because "st,breakinput" parameter is optional do not make probe
  435. * failed if it doesn't exist.
  436. */
  437. if (nb <= 0)
  438. return 0;
  439. if (nb > MAX_BREAKINPUT)
  440. return -EINVAL;
  441. array_size = nb * sizeof(struct stm32_breakinput) / sizeof(u32);
  442. ret = of_property_read_u32_array(np, "st,breakinput",
  443. (u32 *)breakinput, array_size);
  444. if (ret)
  445. return ret;
  446. for (i = 0; i < nb && !ret; i++) {
  447. ret = stm32_pwm_set_breakinput(priv,
  448. breakinput[i].index,
  449. breakinput[i].level,
  450. breakinput[i].filter);
  451. }
  452. return ret;
  453. }
  454. static void stm32_pwm_detect_complementary(struct stm32_pwm *priv)
  455. {
  456. u32 ccer;
  457. /*
  458. * If complementary bit doesn't exist writing 1 will have no
  459. * effect so we can detect it.
  460. */
  461. regmap_update_bits(priv->regmap,
  462. TIM_CCER, TIM_CCER_CC1NE, TIM_CCER_CC1NE);
  463. regmap_read(priv->regmap, TIM_CCER, &ccer);
  464. regmap_update_bits(priv->regmap, TIM_CCER, TIM_CCER_CC1NE, 0);
  465. priv->have_complementary_output = (ccer != 0);
  466. }
  467. static int stm32_pwm_detect_channels(struct stm32_pwm *priv)
  468. {
  469. u32 ccer;
  470. int npwm = 0;
  471. /*
  472. * If channels enable bits don't exist writing 1 will have no
  473. * effect so we can detect and count them.
  474. */
  475. regmap_update_bits(priv->regmap,
  476. TIM_CCER, TIM_CCER_CCXE, TIM_CCER_CCXE);
  477. regmap_read(priv->regmap, TIM_CCER, &ccer);
  478. regmap_update_bits(priv->regmap, TIM_CCER, TIM_CCER_CCXE, 0);
  479. if (ccer & TIM_CCER_CC1E)
  480. npwm++;
  481. if (ccer & TIM_CCER_CC2E)
  482. npwm++;
  483. if (ccer & TIM_CCER_CC3E)
  484. npwm++;
  485. if (ccer & TIM_CCER_CC4E)
  486. npwm++;
  487. return npwm;
  488. }
  489. static int stm32_pwm_probe(struct platform_device *pdev)
  490. {
  491. struct device *dev = &pdev->dev;
  492. struct device_node *np = dev->of_node;
  493. struct stm32_timers *ddata = dev_get_drvdata(pdev->dev.parent);
  494. struct stm32_pwm *priv;
  495. int ret;
  496. priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
  497. if (!priv)
  498. return -ENOMEM;
  499. mutex_init(&priv->lock);
  500. priv->regmap = ddata->regmap;
  501. priv->clk = ddata->clk;
  502. priv->max_arr = ddata->max_arr;
  503. if (!priv->regmap || !priv->clk)
  504. return -EINVAL;
  505. ret = stm32_pwm_apply_breakinputs(priv, np);
  506. if (ret)
  507. return ret;
  508. stm32_pwm_detect_complementary(priv);
  509. priv->chip.base = -1;
  510. priv->chip.dev = dev;
  511. priv->chip.ops = &stm32pwm_ops;
  512. priv->chip.npwm = stm32_pwm_detect_channels(priv);
  513. ret = pwmchip_add(&priv->chip);
  514. if (ret < 0)
  515. return ret;
  516. platform_set_drvdata(pdev, priv);
  517. return 0;
  518. }
  519. static int stm32_pwm_remove(struct platform_device *pdev)
  520. {
  521. struct stm32_pwm *priv = platform_get_drvdata(pdev);
  522. unsigned int i;
  523. for (i = 0; i < priv->chip.npwm; i++)
  524. pwm_disable(&priv->chip.pwms[i]);
  525. pwmchip_remove(&priv->chip);
  526. return 0;
  527. }
  528. static const struct of_device_id stm32_pwm_of_match[] = {
  529. { .compatible = "st,stm32-pwm", },
  530. { /* end node */ },
  531. };
  532. MODULE_DEVICE_TABLE(of, stm32_pwm_of_match);
  533. static struct platform_driver stm32_pwm_driver = {
  534. .probe = stm32_pwm_probe,
  535. .remove = stm32_pwm_remove,
  536. .driver = {
  537. .name = "stm32-pwm",
  538. .of_match_table = stm32_pwm_of_match,
  539. },
  540. };
  541. module_platform_driver(stm32_pwm_driver);
  542. MODULE_ALIAS("platform:stm32-pwm");
  543. MODULE_DESCRIPTION("STMicroelectronics STM32 PWM driver");
  544. MODULE_LICENSE("GPL v2");