pwm-tiehrpwm.c 16 KB

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