pwm-tiehrpwm.c 16 KB

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