pwm-tiehrpwm.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * EHRPWM PWM driver
  4. *
  5. * Copyright (C) 2012 Texas Instruments, Inc. - https://www.ti.com/
  6. */
  7. #include <linux/module.h>
  8. #include <linux/platform_device.h>
  9. #include <linux/pwm.h>
  10. #include <linux/io.h>
  11. #include <linux/err.h>
  12. #include <linux/clk.h>
  13. #include <linux/pm_runtime.h>
  14. #include <linux/of.h>
  15. /* EHRPWM registers and bits definitions */
  16. /* Time base module registers */
  17. #define TBCTL 0x00
  18. #define TBPRD 0x0A
  19. #define TBCTL_PRDLD_MASK BIT(3)
  20. #define TBCTL_PRDLD_SHDW 0
  21. #define TBCTL_PRDLD_IMDT BIT(3)
  22. #define TBCTL_CLKDIV_MASK (BIT(12) | BIT(11) | BIT(10) | BIT(9) | \
  23. BIT(8) | BIT(7))
  24. #define TBCTL_CTRMODE_MASK (BIT(1) | BIT(0))
  25. #define TBCTL_CTRMODE_UP 0
  26. #define TBCTL_CTRMODE_DOWN BIT(0)
  27. #define TBCTL_CTRMODE_UPDOWN BIT(1)
  28. #define TBCTL_CTRMODE_FREEZE (BIT(1) | BIT(0))
  29. #define TBCTL_HSPCLKDIV_SHIFT 7
  30. #define TBCTL_CLKDIV_SHIFT 10
  31. #define CLKDIV_MAX 7
  32. #define HSPCLKDIV_MAX 7
  33. #define PERIOD_MAX 0xFFFF
  34. /* compare module registers */
  35. #define CMPA 0x12
  36. #define CMPB 0x14
  37. /* Action qualifier module registers */
  38. #define AQCTLA 0x16
  39. #define AQCTLB 0x18
  40. #define AQSFRC 0x1A
  41. #define AQCSFRC 0x1C
  42. #define AQCTL_CBU_MASK (BIT(9) | BIT(8))
  43. #define AQCTL_CBU_FRCLOW BIT(8)
  44. #define AQCTL_CBU_FRCHIGH BIT(9)
  45. #define AQCTL_CBU_FRCTOGGLE (BIT(9) | BIT(8))
  46. #define AQCTL_CAU_MASK (BIT(5) | BIT(4))
  47. #define AQCTL_CAU_FRCLOW BIT(4)
  48. #define AQCTL_CAU_FRCHIGH BIT(5)
  49. #define AQCTL_CAU_FRCTOGGLE (BIT(5) | BIT(4))
  50. #define AQCTL_PRD_MASK (BIT(3) | BIT(2))
  51. #define AQCTL_PRD_FRCLOW BIT(2)
  52. #define AQCTL_PRD_FRCHIGH BIT(3)
  53. #define AQCTL_PRD_FRCTOGGLE (BIT(3) | BIT(2))
  54. #define AQCTL_ZRO_MASK (BIT(1) | BIT(0))
  55. #define AQCTL_ZRO_FRCLOW BIT(0)
  56. #define AQCTL_ZRO_FRCHIGH BIT(1)
  57. #define AQCTL_ZRO_FRCTOGGLE (BIT(1) | BIT(0))
  58. #define AQCTL_CHANA_POLNORMAL (AQCTL_CAU_FRCLOW | AQCTL_PRD_FRCHIGH | \
  59. AQCTL_ZRO_FRCHIGH)
  60. #define AQCTL_CHANA_POLINVERSED (AQCTL_CAU_FRCHIGH | AQCTL_PRD_FRCLOW | \
  61. AQCTL_ZRO_FRCLOW)
  62. #define AQCTL_CHANB_POLNORMAL (AQCTL_CBU_FRCLOW | AQCTL_PRD_FRCHIGH | \
  63. AQCTL_ZRO_FRCHIGH)
  64. #define AQCTL_CHANB_POLINVERSED (AQCTL_CBU_FRCHIGH | AQCTL_PRD_FRCLOW | \
  65. AQCTL_ZRO_FRCLOW)
  66. #define AQSFRC_RLDCSF_MASK (BIT(7) | BIT(6))
  67. #define AQSFRC_RLDCSF_ZRO 0
  68. #define AQSFRC_RLDCSF_PRD BIT(6)
  69. #define AQSFRC_RLDCSF_ZROPRD BIT(7)
  70. #define AQSFRC_RLDCSF_IMDT (BIT(7) | BIT(6))
  71. #define AQCSFRC_CSFB_MASK (BIT(3) | BIT(2))
  72. #define AQCSFRC_CSFB_FRCDIS 0
  73. #define AQCSFRC_CSFB_FRCLOW BIT(2)
  74. #define AQCSFRC_CSFB_FRCHIGH BIT(3)
  75. #define AQCSFRC_CSFB_DISSWFRC (BIT(3) | BIT(2))
  76. #define AQCSFRC_CSFA_MASK (BIT(1) | BIT(0))
  77. #define AQCSFRC_CSFA_FRCDIS 0
  78. #define AQCSFRC_CSFA_FRCLOW BIT(0)
  79. #define AQCSFRC_CSFA_FRCHIGH BIT(1)
  80. #define AQCSFRC_CSFA_DISSWFRC (BIT(1) | BIT(0))
  81. #define NUM_PWM_CHANNEL 2 /* EHRPWM channels */
  82. struct ehrpwm_context {
  83. u16 tbctl;
  84. u16 tbprd;
  85. u16 cmpa;
  86. u16 cmpb;
  87. u16 aqctla;
  88. u16 aqctlb;
  89. u16 aqsfrc;
  90. u16 aqcsfrc;
  91. };
  92. struct ehrpwm_pwm_chip {
  93. unsigned long clk_rate;
  94. void __iomem *mmio_base;
  95. unsigned long period_cycles[NUM_PWM_CHANNEL];
  96. enum pwm_polarity polarity[NUM_PWM_CHANNEL];
  97. struct clk *tbclk;
  98. struct ehrpwm_context ctx;
  99. };
  100. static inline struct ehrpwm_pwm_chip *to_ehrpwm_pwm_chip(struct pwm_chip *chip)
  101. {
  102. return pwmchip_get_drvdata(chip);
  103. }
  104. static inline u16 ehrpwm_read(void __iomem *base, unsigned int offset)
  105. {
  106. return readw(base + offset);
  107. }
  108. static inline void ehrpwm_write(void __iomem *base, unsigned int offset,
  109. u16 value)
  110. {
  111. writew(value, base + offset);
  112. }
  113. static void ehrpwm_modify(void __iomem *base, unsigned int offset, u16 mask,
  114. u16 value)
  115. {
  116. unsigned short val;
  117. val = readw(base + offset);
  118. val &= ~mask;
  119. val |= value & mask;
  120. writew(val, base + offset);
  121. }
  122. /**
  123. * set_prescale_div - Set up the prescaler divider function
  124. * @rqst_prescaler: prescaler value min
  125. * @prescale_div: prescaler value set
  126. * @tb_clk_div: Time Base Control prescaler bits
  127. */
  128. static int set_prescale_div(unsigned long rqst_prescaler, u16 *prescale_div,
  129. u16 *tb_clk_div)
  130. {
  131. unsigned int clkdiv, hspclkdiv;
  132. for (clkdiv = 0; clkdiv <= CLKDIV_MAX; clkdiv++) {
  133. for (hspclkdiv = 0; hspclkdiv <= HSPCLKDIV_MAX; hspclkdiv++) {
  134. /*
  135. * calculations for prescaler value :
  136. * prescale_div = HSPCLKDIVIDER * CLKDIVIDER.
  137. * HSPCLKDIVIDER = 2 ** hspclkdiv
  138. * CLKDIVIDER = (1), if clkdiv == 0 *OR*
  139. * (2 * clkdiv), if clkdiv != 0
  140. *
  141. * Configure prescale_div value such that period
  142. * register value is less than 65535.
  143. */
  144. *prescale_div = (1 << clkdiv) *
  145. (hspclkdiv ? (hspclkdiv * 2) : 1);
  146. if (*prescale_div > rqst_prescaler) {
  147. *tb_clk_div = (clkdiv << TBCTL_CLKDIV_SHIFT) |
  148. (hspclkdiv << TBCTL_HSPCLKDIV_SHIFT);
  149. return 0;
  150. }
  151. }
  152. }
  153. return 1;
  154. }
  155. static void configure_polarity(struct ehrpwm_pwm_chip *pc, int chan)
  156. {
  157. u16 aqctl_val, aqctl_mask;
  158. unsigned int aqctl_reg;
  159. /*
  160. * Configure PWM output to HIGH/LOW level on counter
  161. * reaches compare register value and LOW/HIGH level
  162. * on counter value reaches period register value and
  163. * zero value on counter
  164. */
  165. if (chan == 1) {
  166. aqctl_reg = AQCTLB;
  167. aqctl_mask = AQCTL_CBU_MASK;
  168. if (pc->polarity[chan] == PWM_POLARITY_INVERSED)
  169. aqctl_val = AQCTL_CHANB_POLINVERSED;
  170. else
  171. aqctl_val = AQCTL_CHANB_POLNORMAL;
  172. } else {
  173. aqctl_reg = AQCTLA;
  174. aqctl_mask = AQCTL_CAU_MASK;
  175. if (pc->polarity[chan] == PWM_POLARITY_INVERSED)
  176. aqctl_val = AQCTL_CHANA_POLINVERSED;
  177. else
  178. aqctl_val = AQCTL_CHANA_POLNORMAL;
  179. }
  180. aqctl_mask |= AQCTL_PRD_MASK | AQCTL_ZRO_MASK;
  181. ehrpwm_modify(pc->mmio_base, aqctl_reg, aqctl_mask, aqctl_val);
  182. }
  183. /*
  184. * period_ns = 10^9 * (ps_divval * period_cycles) / PWM_CLK_RATE
  185. * duty_ns = 10^9 * (ps_divval * duty_cycles) / PWM_CLK_RATE
  186. */
  187. static int ehrpwm_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm,
  188. u64 duty_ns, u64 period_ns)
  189. {
  190. struct ehrpwm_pwm_chip *pc = to_ehrpwm_pwm_chip(chip);
  191. u32 period_cycles, duty_cycles;
  192. u16 ps_divval, tb_divval;
  193. unsigned int i, cmp_reg;
  194. unsigned long long c;
  195. if (period_ns > NSEC_PER_SEC)
  196. return -ERANGE;
  197. c = pc->clk_rate;
  198. c = c * period_ns;
  199. do_div(c, NSEC_PER_SEC);
  200. period_cycles = (unsigned long)c;
  201. if (period_cycles < 1) {
  202. period_cycles = 1;
  203. duty_cycles = 1;
  204. } else {
  205. c = pc->clk_rate;
  206. c = c * duty_ns;
  207. do_div(c, NSEC_PER_SEC);
  208. duty_cycles = (unsigned long)c;
  209. }
  210. /*
  211. * Period values should be same for multiple PWM channels as IP uses
  212. * same period register for multiple channels.
  213. */
  214. for (i = 0; i < NUM_PWM_CHANNEL; i++) {
  215. if (pc->period_cycles[i] &&
  216. (pc->period_cycles[i] != period_cycles)) {
  217. /*
  218. * Allow channel to reconfigure period if no other
  219. * channels being configured.
  220. */
  221. if (i == pwm->hwpwm)
  222. continue;
  223. dev_err(pwmchip_parent(chip),
  224. "period value conflicts with channel %u\n",
  225. i);
  226. return -EINVAL;
  227. }
  228. }
  229. pc->period_cycles[pwm->hwpwm] = period_cycles;
  230. /* Configure clock prescaler to support Low frequency PWM wave */
  231. if (set_prescale_div(period_cycles/PERIOD_MAX, &ps_divval,
  232. &tb_divval)) {
  233. dev_err(pwmchip_parent(chip), "Unsupported values\n");
  234. return -EINVAL;
  235. }
  236. pm_runtime_get_sync(pwmchip_parent(chip));
  237. /* Update clock prescaler values */
  238. ehrpwm_modify(pc->mmio_base, TBCTL, TBCTL_CLKDIV_MASK, tb_divval);
  239. /* Update period & duty cycle with presacler division */
  240. period_cycles = period_cycles / ps_divval;
  241. duty_cycles = duty_cycles / ps_divval;
  242. /* Configure shadow loading on Period register */
  243. ehrpwm_modify(pc->mmio_base, TBCTL, TBCTL_PRDLD_MASK, TBCTL_PRDLD_SHDW);
  244. ehrpwm_write(pc->mmio_base, TBPRD, period_cycles);
  245. /* Configure ehrpwm counter for up-count mode */
  246. ehrpwm_modify(pc->mmio_base, TBCTL, TBCTL_CTRMODE_MASK,
  247. TBCTL_CTRMODE_UP);
  248. if (pwm->hwpwm == 1)
  249. /* Channel 1 configured with compare B register */
  250. cmp_reg = CMPB;
  251. else
  252. /* Channel 0 configured with compare A register */
  253. cmp_reg = CMPA;
  254. ehrpwm_write(pc->mmio_base, cmp_reg, duty_cycles);
  255. pm_runtime_put_sync(pwmchip_parent(chip));
  256. return 0;
  257. }
  258. static int ehrpwm_pwm_set_polarity(struct pwm_chip *chip,
  259. struct pwm_device *pwm,
  260. enum pwm_polarity polarity)
  261. {
  262. struct ehrpwm_pwm_chip *pc = to_ehrpwm_pwm_chip(chip);
  263. /* Configuration of polarity in hardware delayed, do at enable */
  264. pc->polarity[pwm->hwpwm] = polarity;
  265. return 0;
  266. }
  267. static int ehrpwm_pwm_enable(struct pwm_chip *chip, struct pwm_device *pwm)
  268. {
  269. struct ehrpwm_pwm_chip *pc = to_ehrpwm_pwm_chip(chip);
  270. u16 aqcsfrc_val, aqcsfrc_mask;
  271. int ret;
  272. /* Leave clock enabled on enabling PWM */
  273. pm_runtime_get_sync(pwmchip_parent(chip));
  274. /* Disabling Action Qualifier on PWM output */
  275. if (pwm->hwpwm) {
  276. aqcsfrc_val = AQCSFRC_CSFB_FRCDIS;
  277. aqcsfrc_mask = AQCSFRC_CSFB_MASK;
  278. } else {
  279. aqcsfrc_val = AQCSFRC_CSFA_FRCDIS;
  280. aqcsfrc_mask = AQCSFRC_CSFA_MASK;
  281. }
  282. /* Changes to shadow mode */
  283. ehrpwm_modify(pc->mmio_base, AQSFRC, AQSFRC_RLDCSF_MASK,
  284. AQSFRC_RLDCSF_ZRO);
  285. ehrpwm_modify(pc->mmio_base, AQCSFRC, aqcsfrc_mask, aqcsfrc_val);
  286. /* Channels polarity can be configured from action qualifier module */
  287. configure_polarity(pc, pwm->hwpwm);
  288. /* Enable TBCLK */
  289. ret = clk_enable(pc->tbclk);
  290. if (ret) {
  291. dev_err(pwmchip_parent(chip), "Failed to enable TBCLK for %s: %d\n",
  292. dev_name(pwmchip_parent(chip)), ret);
  293. return ret;
  294. }
  295. return 0;
  296. }
  297. static void ehrpwm_pwm_disable(struct pwm_chip *chip, struct pwm_device *pwm)
  298. {
  299. struct ehrpwm_pwm_chip *pc = to_ehrpwm_pwm_chip(chip);
  300. u16 aqcsfrc_val, aqcsfrc_mask;
  301. /* Action Qualifier puts PWM output low forcefully */
  302. if (pwm->hwpwm) {
  303. aqcsfrc_val = AQCSFRC_CSFB_FRCLOW;
  304. aqcsfrc_mask = AQCSFRC_CSFB_MASK;
  305. } else {
  306. aqcsfrc_val = AQCSFRC_CSFA_FRCLOW;
  307. aqcsfrc_mask = AQCSFRC_CSFA_MASK;
  308. }
  309. /* Update shadow register first before modifying active register */
  310. ehrpwm_modify(pc->mmio_base, AQSFRC, AQSFRC_RLDCSF_MASK,
  311. AQSFRC_RLDCSF_ZRO);
  312. ehrpwm_modify(pc->mmio_base, AQCSFRC, aqcsfrc_mask, aqcsfrc_val);
  313. /*
  314. * Changes to immediate action on Action Qualifier. This puts
  315. * Action Qualifier control on PWM output from next TBCLK
  316. */
  317. ehrpwm_modify(pc->mmio_base, AQSFRC, AQSFRC_RLDCSF_MASK,
  318. AQSFRC_RLDCSF_IMDT);
  319. ehrpwm_modify(pc->mmio_base, AQCSFRC, aqcsfrc_mask, aqcsfrc_val);
  320. /* Disabling TBCLK on PWM disable */
  321. clk_disable(pc->tbclk);
  322. /* Disable clock on PWM disable */
  323. pm_runtime_put_sync(pwmchip_parent(chip));
  324. }
  325. static void ehrpwm_pwm_free(struct pwm_chip *chip, struct pwm_device *pwm)
  326. {
  327. struct ehrpwm_pwm_chip *pc = to_ehrpwm_pwm_chip(chip);
  328. if (pwm_is_enabled(pwm)) {
  329. dev_warn(pwmchip_parent(chip), "Removing PWM device without disabling\n");
  330. pm_runtime_put_sync(pwmchip_parent(chip));
  331. }
  332. /* set period value to zero on free */
  333. pc->period_cycles[pwm->hwpwm] = 0;
  334. }
  335. static int ehrpwm_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm,
  336. const struct pwm_state *state)
  337. {
  338. int err;
  339. bool enabled = pwm->state.enabled;
  340. if (state->polarity != pwm->state.polarity) {
  341. if (enabled) {
  342. ehrpwm_pwm_disable(chip, pwm);
  343. enabled = false;
  344. }
  345. err = ehrpwm_pwm_set_polarity(chip, pwm, state->polarity);
  346. if (err)
  347. return err;
  348. }
  349. if (!state->enabled) {
  350. if (enabled)
  351. ehrpwm_pwm_disable(chip, pwm);
  352. return 0;
  353. }
  354. err = ehrpwm_pwm_config(chip, pwm, state->duty_cycle, state->period);
  355. if (err)
  356. return err;
  357. if (!enabled)
  358. err = ehrpwm_pwm_enable(chip, pwm);
  359. return err;
  360. }
  361. static const struct pwm_ops ehrpwm_pwm_ops = {
  362. .free = ehrpwm_pwm_free,
  363. .apply = ehrpwm_pwm_apply,
  364. };
  365. static const struct of_device_id ehrpwm_of_match[] = {
  366. { .compatible = "ti,am3352-ehrpwm" },
  367. { .compatible = "ti,am33xx-ehrpwm" },
  368. {},
  369. };
  370. MODULE_DEVICE_TABLE(of, ehrpwm_of_match);
  371. static int ehrpwm_pwm_probe(struct platform_device *pdev)
  372. {
  373. struct device_node *np = pdev->dev.of_node;
  374. struct ehrpwm_pwm_chip *pc;
  375. struct pwm_chip *chip;
  376. struct clk *clk;
  377. int ret;
  378. chip = devm_pwmchip_alloc(&pdev->dev, NUM_PWM_CHANNEL, sizeof(*pc));
  379. if (IS_ERR(chip))
  380. return PTR_ERR(chip);
  381. pc = to_ehrpwm_pwm_chip(chip);
  382. clk = devm_clk_get(&pdev->dev, "fck");
  383. if (IS_ERR(clk)) {
  384. if (of_device_is_compatible(np, "ti,am33xx-ecap")) {
  385. dev_warn(&pdev->dev, "Binding is obsolete.\n");
  386. clk = devm_clk_get(pdev->dev.parent, "fck");
  387. }
  388. }
  389. if (IS_ERR(clk))
  390. return dev_err_probe(&pdev->dev, PTR_ERR(clk), "Failed to get fck\n");
  391. pc->clk_rate = clk_get_rate(clk);
  392. if (!pc->clk_rate) {
  393. dev_err(&pdev->dev, "failed to get clock rate\n");
  394. return -EINVAL;
  395. }
  396. chip->ops = &ehrpwm_pwm_ops;
  397. pc->mmio_base = devm_platform_ioremap_resource(pdev, 0);
  398. if (IS_ERR(pc->mmio_base))
  399. return PTR_ERR(pc->mmio_base);
  400. /* Acquire tbclk for Time Base EHRPWM submodule */
  401. pc->tbclk = devm_clk_get(&pdev->dev, "tbclk");
  402. if (IS_ERR(pc->tbclk))
  403. return dev_err_probe(&pdev->dev, PTR_ERR(pc->tbclk), "Failed to get tbclk\n");
  404. ret = clk_prepare(pc->tbclk);
  405. if (ret < 0) {
  406. dev_err(&pdev->dev, "clk_prepare() failed: %d\n", ret);
  407. return ret;
  408. }
  409. ret = pwmchip_add(chip);
  410. if (ret < 0) {
  411. dev_err(&pdev->dev, "pwmchip_add() failed: %d\n", ret);
  412. goto err_clk_unprepare;
  413. }
  414. platform_set_drvdata(pdev, chip);
  415. pm_runtime_enable(&pdev->dev);
  416. return 0;
  417. err_clk_unprepare:
  418. clk_unprepare(pc->tbclk);
  419. return ret;
  420. }
  421. static void ehrpwm_pwm_remove(struct platform_device *pdev)
  422. {
  423. struct pwm_chip *chip = platform_get_drvdata(pdev);
  424. struct ehrpwm_pwm_chip *pc = to_ehrpwm_pwm_chip(chip);
  425. pwmchip_remove(chip);
  426. clk_unprepare(pc->tbclk);
  427. pm_runtime_disable(&pdev->dev);
  428. }
  429. static void ehrpwm_pwm_save_context(struct pwm_chip *chip)
  430. {
  431. struct ehrpwm_pwm_chip *pc = to_ehrpwm_pwm_chip(chip);
  432. pm_runtime_get_sync(pwmchip_parent(chip));
  433. pc->ctx.tbctl = ehrpwm_read(pc->mmio_base, TBCTL);
  434. pc->ctx.tbprd = ehrpwm_read(pc->mmio_base, TBPRD);
  435. pc->ctx.cmpa = ehrpwm_read(pc->mmio_base, CMPA);
  436. pc->ctx.cmpb = ehrpwm_read(pc->mmio_base, CMPB);
  437. pc->ctx.aqctla = ehrpwm_read(pc->mmio_base, AQCTLA);
  438. pc->ctx.aqctlb = ehrpwm_read(pc->mmio_base, AQCTLB);
  439. pc->ctx.aqsfrc = ehrpwm_read(pc->mmio_base, AQSFRC);
  440. pc->ctx.aqcsfrc = ehrpwm_read(pc->mmio_base, AQCSFRC);
  441. pm_runtime_put_sync(pwmchip_parent(chip));
  442. }
  443. static void ehrpwm_pwm_restore_context(struct pwm_chip *chip)
  444. {
  445. struct ehrpwm_pwm_chip *pc = to_ehrpwm_pwm_chip(chip);
  446. ehrpwm_write(pc->mmio_base, TBPRD, pc->ctx.tbprd);
  447. ehrpwm_write(pc->mmio_base, CMPA, pc->ctx.cmpa);
  448. ehrpwm_write(pc->mmio_base, CMPB, pc->ctx.cmpb);
  449. ehrpwm_write(pc->mmio_base, AQCTLA, pc->ctx.aqctla);
  450. ehrpwm_write(pc->mmio_base, AQCTLB, pc->ctx.aqctlb);
  451. ehrpwm_write(pc->mmio_base, AQSFRC, pc->ctx.aqsfrc);
  452. ehrpwm_write(pc->mmio_base, AQCSFRC, pc->ctx.aqcsfrc);
  453. ehrpwm_write(pc->mmio_base, TBCTL, pc->ctx.tbctl);
  454. }
  455. static int ehrpwm_pwm_suspend(struct device *dev)
  456. {
  457. struct pwm_chip *chip = dev_get_drvdata(dev);
  458. unsigned int i;
  459. ehrpwm_pwm_save_context(chip);
  460. for (i = 0; i < chip->npwm; i++) {
  461. struct pwm_device *pwm = &chip->pwms[i];
  462. if (!pwm_is_enabled(pwm))
  463. continue;
  464. /* Disable explicitly if PWM is running */
  465. pm_runtime_put_sync(dev);
  466. }
  467. return 0;
  468. }
  469. static int ehrpwm_pwm_resume(struct device *dev)
  470. {
  471. struct pwm_chip *chip = dev_get_drvdata(dev);
  472. unsigned int i;
  473. for (i = 0; i < chip->npwm; i++) {
  474. struct pwm_device *pwm = &chip->pwms[i];
  475. if (!pwm_is_enabled(pwm))
  476. continue;
  477. /* Enable explicitly if PWM was running */
  478. pm_runtime_get_sync(dev);
  479. }
  480. ehrpwm_pwm_restore_context(chip);
  481. return 0;
  482. }
  483. static DEFINE_SIMPLE_DEV_PM_OPS(ehrpwm_pwm_pm_ops, ehrpwm_pwm_suspend,
  484. ehrpwm_pwm_resume);
  485. static struct platform_driver ehrpwm_pwm_driver = {
  486. .driver = {
  487. .name = "ehrpwm",
  488. .of_match_table = ehrpwm_of_match,
  489. .pm = pm_ptr(&ehrpwm_pwm_pm_ops),
  490. },
  491. .probe = ehrpwm_pwm_probe,
  492. .remove = ehrpwm_pwm_remove,
  493. };
  494. module_platform_driver(ehrpwm_pwm_driver);
  495. MODULE_DESCRIPTION("EHRPWM PWM driver");
  496. MODULE_AUTHOR("Texas Instruments");
  497. MODULE_LICENSE("GPL");