pwm-tiehrpwm.c 16 KB

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