pwm-tiehrpwm.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615
  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. #include "pwm-tipwmss.h"
  29. /* EHRPWM registers and bits definitions */
  30. /* Time base module registers */
  31. #define TBCTL 0x00
  32. #define TBPRD 0x0A
  33. #define TBCTL_RUN_MASK (BIT(15) | BIT(14))
  34. #define TBCTL_STOP_NEXT 0
  35. #define TBCTL_STOP_ON_CYCLE BIT(14)
  36. #define TBCTL_FREE_RUN (BIT(15) | BIT(14))
  37. #define TBCTL_PRDLD_MASK BIT(3)
  38. #define TBCTL_PRDLD_SHDW 0
  39. #define TBCTL_PRDLD_IMDT BIT(3)
  40. #define TBCTL_CLKDIV_MASK (BIT(12) | BIT(11) | BIT(10) | BIT(9) | \
  41. BIT(8) | BIT(7))
  42. #define TBCTL_CTRMODE_MASK (BIT(1) | BIT(0))
  43. #define TBCTL_CTRMODE_UP 0
  44. #define TBCTL_CTRMODE_DOWN BIT(0)
  45. #define TBCTL_CTRMODE_UPDOWN BIT(1)
  46. #define TBCTL_CTRMODE_FREEZE (BIT(1) | BIT(0))
  47. #define TBCTL_HSPCLKDIV_SHIFT 7
  48. #define TBCTL_CLKDIV_SHIFT 10
  49. #define CLKDIV_MAX 7
  50. #define HSPCLKDIV_MAX 7
  51. #define PERIOD_MAX 0xFFFF
  52. /* compare module registers */
  53. #define CMPA 0x12
  54. #define CMPB 0x14
  55. /* Action qualifier module registers */
  56. #define AQCTLA 0x16
  57. #define AQCTLB 0x18
  58. #define AQSFRC 0x1A
  59. #define AQCSFRC 0x1C
  60. #define AQCTL_CBU_MASK (BIT(9) | BIT(8))
  61. #define AQCTL_CBU_FRCLOW BIT(8)
  62. #define AQCTL_CBU_FRCHIGH BIT(9)
  63. #define AQCTL_CBU_FRCTOGGLE (BIT(9) | BIT(8))
  64. #define AQCTL_CAU_MASK (BIT(5) | BIT(4))
  65. #define AQCTL_CAU_FRCLOW BIT(4)
  66. #define AQCTL_CAU_FRCHIGH BIT(5)
  67. #define AQCTL_CAU_FRCTOGGLE (BIT(5) | BIT(4))
  68. #define AQCTL_PRD_MASK (BIT(3) | BIT(2))
  69. #define AQCTL_PRD_FRCLOW BIT(2)
  70. #define AQCTL_PRD_FRCHIGH BIT(3)
  71. #define AQCTL_PRD_FRCTOGGLE (BIT(3) | BIT(2))
  72. #define AQCTL_ZRO_MASK (BIT(1) | BIT(0))
  73. #define AQCTL_ZRO_FRCLOW BIT(0)
  74. #define AQCTL_ZRO_FRCHIGH BIT(1)
  75. #define AQCTL_ZRO_FRCTOGGLE (BIT(1) | BIT(0))
  76. #define AQCTL_CHANA_POLNORMAL (AQCTL_CAU_FRCLOW | AQCTL_PRD_FRCHIGH | \
  77. AQCTL_ZRO_FRCHIGH)
  78. #define AQCTL_CHANA_POLINVERSED (AQCTL_CAU_FRCHIGH | AQCTL_PRD_FRCLOW | \
  79. AQCTL_ZRO_FRCLOW)
  80. #define AQCTL_CHANB_POLNORMAL (AQCTL_CBU_FRCLOW | AQCTL_PRD_FRCHIGH | \
  81. AQCTL_ZRO_FRCHIGH)
  82. #define AQCTL_CHANB_POLINVERSED (AQCTL_CBU_FRCHIGH | AQCTL_PRD_FRCLOW | \
  83. AQCTL_ZRO_FRCLOW)
  84. #define AQSFRC_RLDCSF_MASK (BIT(7) | BIT(6))
  85. #define AQSFRC_RLDCSF_ZRO 0
  86. #define AQSFRC_RLDCSF_PRD BIT(6)
  87. #define AQSFRC_RLDCSF_ZROPRD BIT(7)
  88. #define AQSFRC_RLDCSF_IMDT (BIT(7) | BIT(6))
  89. #define AQCSFRC_CSFB_MASK (BIT(3) | BIT(2))
  90. #define AQCSFRC_CSFB_FRCDIS 0
  91. #define AQCSFRC_CSFB_FRCLOW BIT(2)
  92. #define AQCSFRC_CSFB_FRCHIGH BIT(3)
  93. #define AQCSFRC_CSFB_DISSWFRC (BIT(3) | BIT(2))
  94. #define AQCSFRC_CSFA_MASK (BIT(1) | BIT(0))
  95. #define AQCSFRC_CSFA_FRCDIS 0
  96. #define AQCSFRC_CSFA_FRCLOW BIT(0)
  97. #define AQCSFRC_CSFA_FRCHIGH BIT(1)
  98. #define AQCSFRC_CSFA_DISSWFRC (BIT(1) | BIT(0))
  99. #define NUM_PWM_CHANNEL 2 /* EHRPWM channels */
  100. struct ehrpwm_context {
  101. u16 tbctl;
  102. u16 tbprd;
  103. u16 cmpa;
  104. u16 cmpb;
  105. u16 aqctla;
  106. u16 aqctlb;
  107. u16 aqsfrc;
  108. u16 aqcsfrc;
  109. };
  110. struct ehrpwm_pwm_chip {
  111. struct pwm_chip chip;
  112. unsigned int clk_rate;
  113. void __iomem *mmio_base;
  114. unsigned long period_cycles[NUM_PWM_CHANNEL];
  115. enum pwm_polarity polarity[NUM_PWM_CHANNEL];
  116. struct clk *tbclk;
  117. struct ehrpwm_context ctx;
  118. };
  119. static inline struct ehrpwm_pwm_chip *to_ehrpwm_pwm_chip(struct pwm_chip *chip)
  120. {
  121. return container_of(chip, struct ehrpwm_pwm_chip, chip);
  122. }
  123. static inline u16 ehrpwm_read(void __iomem *base, int offset)
  124. {
  125. return readw(base + offset);
  126. }
  127. static inline void ehrpwm_write(void __iomem *base, int offset, unsigned int val)
  128. {
  129. writew(val & 0xFFFF, base + offset);
  130. }
  131. static void ehrpwm_modify(void __iomem *base, int offset,
  132. unsigned short mask, unsigned short val)
  133. {
  134. unsigned short regval;
  135. regval = readw(base + offset);
  136. regval &= ~mask;
  137. regval |= val & mask;
  138. writew(regval, 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,
  147. unsigned short *prescale_div, unsigned short *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. int aqctl_reg;
  176. unsigned short aqctl_val, aqctl_mask;
  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. unsigned long long c;
  210. unsigned long period_cycles, duty_cycles;
  211. unsigned short ps_divval, tb_divval;
  212. int i, cmp_reg;
  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, "Period value conflicts with channel %d\n",
  242. i);
  243. return -EINVAL;
  244. }
  245. }
  246. pc->period_cycles[pwm->hwpwm] = period_cycles;
  247. /* Configure clock prescaler to support Low frequency PWM wave */
  248. if (set_prescale_div(period_cycles/PERIOD_MAX, &ps_divval,
  249. &tb_divval)) {
  250. dev_err(chip->dev, "Unsupported values\n");
  251. return -EINVAL;
  252. }
  253. pm_runtime_get_sync(chip->dev);
  254. /* Update clock prescaler values */
  255. ehrpwm_modify(pc->mmio_base, TBCTL, TBCTL_CLKDIV_MASK, tb_divval);
  256. /* Update period & duty cycle with presacler division */
  257. period_cycles = period_cycles / ps_divval;
  258. duty_cycles = duty_cycles / ps_divval;
  259. /* Configure shadow loading on Period register */
  260. ehrpwm_modify(pc->mmio_base, TBCTL, TBCTL_PRDLD_MASK, TBCTL_PRDLD_SHDW);
  261. ehrpwm_write(pc->mmio_base, TBPRD, period_cycles);
  262. /* Configure ehrpwm counter for up-count mode */
  263. ehrpwm_modify(pc->mmio_base, TBCTL, TBCTL_CTRMODE_MASK,
  264. TBCTL_CTRMODE_UP);
  265. if (pwm->hwpwm == 1)
  266. /* Channel 1 configured with compare B register */
  267. cmp_reg = CMPB;
  268. else
  269. /* Channel 0 configured with compare A register */
  270. cmp_reg = CMPA;
  271. ehrpwm_write(pc->mmio_base, cmp_reg, duty_cycles);
  272. pm_runtime_put_sync(chip->dev);
  273. return 0;
  274. }
  275. static int ehrpwm_pwm_set_polarity(struct pwm_chip *chip,
  276. struct pwm_device *pwm, enum pwm_polarity polarity)
  277. {
  278. struct ehrpwm_pwm_chip *pc = to_ehrpwm_pwm_chip(chip);
  279. /* Configuration of polarity in hardware delayed, do at enable */
  280. pc->polarity[pwm->hwpwm] = polarity;
  281. return 0;
  282. }
  283. static int ehrpwm_pwm_enable(struct pwm_chip *chip, struct pwm_device *pwm)
  284. {
  285. struct ehrpwm_pwm_chip *pc = to_ehrpwm_pwm_chip(chip);
  286. unsigned short aqcsfrc_val, aqcsfrc_mask;
  287. int ret;
  288. /* Leave clock enabled on enabling PWM */
  289. pm_runtime_get_sync(chip->dev);
  290. /* Disabling Action Qualifier on PWM output */
  291. if (pwm->hwpwm) {
  292. aqcsfrc_val = AQCSFRC_CSFB_FRCDIS;
  293. aqcsfrc_mask = AQCSFRC_CSFB_MASK;
  294. } else {
  295. aqcsfrc_val = AQCSFRC_CSFA_FRCDIS;
  296. aqcsfrc_mask = AQCSFRC_CSFA_MASK;
  297. }
  298. /* Changes to shadow mode */
  299. ehrpwm_modify(pc->mmio_base, AQSFRC, AQSFRC_RLDCSF_MASK,
  300. AQSFRC_RLDCSF_ZRO);
  301. ehrpwm_modify(pc->mmio_base, AQCSFRC, aqcsfrc_mask, aqcsfrc_val);
  302. /* Channels polarity can be configured from action qualifier module */
  303. configure_polarity(pc, pwm->hwpwm);
  304. /* Enable TBCLK before enabling PWM device */
  305. ret = clk_enable(pc->tbclk);
  306. if (ret) {
  307. dev_err(chip->dev, "Failed to enable TBCLK for %s\n",
  308. dev_name(pc->chip.dev));
  309. return ret;
  310. }
  311. /* Enable time counter for free_run */
  312. ehrpwm_modify(pc->mmio_base, TBCTL, TBCTL_RUN_MASK, TBCTL_FREE_RUN);
  313. return 0;
  314. }
  315. static void ehrpwm_pwm_disable(struct pwm_chip *chip, struct pwm_device *pwm)
  316. {
  317. struct ehrpwm_pwm_chip *pc = to_ehrpwm_pwm_chip(chip);
  318. unsigned short aqcsfrc_val, aqcsfrc_mask;
  319. /* Action Qualifier puts PWM output low forcefully */
  320. if (pwm->hwpwm) {
  321. aqcsfrc_val = AQCSFRC_CSFB_FRCLOW;
  322. aqcsfrc_mask = AQCSFRC_CSFB_MASK;
  323. } else {
  324. aqcsfrc_val = AQCSFRC_CSFA_FRCLOW;
  325. aqcsfrc_mask = AQCSFRC_CSFA_MASK;
  326. }
  327. /* Update shadow register first before modifying active register */
  328. ehrpwm_modify(pc->mmio_base, AQCSFRC, aqcsfrc_mask, aqcsfrc_val);
  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,am33xx-ehrpwm" },
  363. {},
  364. };
  365. MODULE_DEVICE_TABLE(of, ehrpwm_of_match);
  366. static int ehrpwm_pwm_probe(struct platform_device *pdev)
  367. {
  368. int ret;
  369. struct resource *r;
  370. struct clk *clk;
  371. struct ehrpwm_pwm_chip *pc;
  372. u16 status;
  373. pc = devm_kzalloc(&pdev->dev, sizeof(*pc), GFP_KERNEL);
  374. if (!pc)
  375. return -ENOMEM;
  376. clk = devm_clk_get(&pdev->dev, "fck");
  377. if (IS_ERR(clk)) {
  378. dev_err(&pdev->dev, "failed to get clock\n");
  379. return PTR_ERR(clk);
  380. }
  381. pc->clk_rate = clk_get_rate(clk);
  382. if (!pc->clk_rate) {
  383. dev_err(&pdev->dev, "failed to get clock rate\n");
  384. return -EINVAL;
  385. }
  386. pc->chip.dev = &pdev->dev;
  387. pc->chip.ops = &ehrpwm_pwm_ops;
  388. pc->chip.of_xlate = of_pwm_xlate_with_flags;
  389. pc->chip.of_pwm_n_cells = 3;
  390. pc->chip.base = -1;
  391. pc->chip.npwm = NUM_PWM_CHANNEL;
  392. r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  393. pc->mmio_base = devm_ioremap_resource(&pdev->dev, r);
  394. if (IS_ERR(pc->mmio_base))
  395. return PTR_ERR(pc->mmio_base);
  396. /* Acquire tbclk for Time Base EHRPWM submodule */
  397. pc->tbclk = devm_clk_get(&pdev->dev, "tbclk");
  398. if (IS_ERR(pc->tbclk)) {
  399. dev_err(&pdev->dev, "Failed to get tbclk\n");
  400. return PTR_ERR(pc->tbclk);
  401. }
  402. ret = clk_prepare(pc->tbclk);
  403. if (ret < 0) {
  404. dev_err(&pdev->dev, "clk_prepare() failed: %d\n", ret);
  405. return ret;
  406. }
  407. ret = pwmchip_add(&pc->chip);
  408. if (ret < 0) {
  409. dev_err(&pdev->dev, "pwmchip_add() failed: %d\n", ret);
  410. return ret;
  411. }
  412. pm_runtime_enable(&pdev->dev);
  413. pm_runtime_get_sync(&pdev->dev);
  414. status = pwmss_submodule_state_change(pdev->dev.parent,
  415. PWMSS_EPWMCLK_EN);
  416. if (!(status & PWMSS_EPWMCLK_EN_ACK)) {
  417. dev_err(&pdev->dev, "PWMSS config space clock enable failed\n");
  418. ret = -EINVAL;
  419. goto pwmss_clk_failure;
  420. }
  421. pm_runtime_put_sync(&pdev->dev);
  422. platform_set_drvdata(pdev, pc);
  423. return 0;
  424. pwmss_clk_failure:
  425. pm_runtime_put_sync(&pdev->dev);
  426. pm_runtime_disable(&pdev->dev);
  427. pwmchip_remove(&pc->chip);
  428. clk_unprepare(pc->tbclk);
  429. return ret;
  430. }
  431. static int ehrpwm_pwm_remove(struct platform_device *pdev)
  432. {
  433. struct ehrpwm_pwm_chip *pc = platform_get_drvdata(pdev);
  434. clk_unprepare(pc->tbclk);
  435. pm_runtime_get_sync(&pdev->dev);
  436. /*
  437. * Due to hardware misbehaviour, acknowledge of the stop_req
  438. * is missing. Hence checking of the status bit skipped.
  439. */
  440. pwmss_submodule_state_change(pdev->dev.parent, PWMSS_EPWMCLK_STOP_REQ);
  441. pm_runtime_put_sync(&pdev->dev);
  442. pm_runtime_put_sync(&pdev->dev);
  443. pm_runtime_disable(&pdev->dev);
  444. return pwmchip_remove(&pc->chip);
  445. }
  446. #ifdef CONFIG_PM_SLEEP
  447. static void ehrpwm_pwm_save_context(struct ehrpwm_pwm_chip *pc)
  448. {
  449. pm_runtime_get_sync(pc->chip.dev);
  450. pc->ctx.tbctl = ehrpwm_read(pc->mmio_base, TBCTL);
  451. pc->ctx.tbprd = ehrpwm_read(pc->mmio_base, TBPRD);
  452. pc->ctx.cmpa = ehrpwm_read(pc->mmio_base, CMPA);
  453. pc->ctx.cmpb = ehrpwm_read(pc->mmio_base, CMPB);
  454. pc->ctx.aqctla = ehrpwm_read(pc->mmio_base, AQCTLA);
  455. pc->ctx.aqctlb = ehrpwm_read(pc->mmio_base, AQCTLB);
  456. pc->ctx.aqsfrc = ehrpwm_read(pc->mmio_base, AQSFRC);
  457. pc->ctx.aqcsfrc = ehrpwm_read(pc->mmio_base, AQCSFRC);
  458. pm_runtime_put_sync(pc->chip.dev);
  459. }
  460. static void ehrpwm_pwm_restore_context(struct ehrpwm_pwm_chip *pc)
  461. {
  462. ehrpwm_write(pc->mmio_base, TBPRD, pc->ctx.tbprd);
  463. ehrpwm_write(pc->mmio_base, CMPA, pc->ctx.cmpa);
  464. ehrpwm_write(pc->mmio_base, CMPB, pc->ctx.cmpb);
  465. ehrpwm_write(pc->mmio_base, AQCTLA, pc->ctx.aqctla);
  466. ehrpwm_write(pc->mmio_base, AQCTLB, pc->ctx.aqctlb);
  467. ehrpwm_write(pc->mmio_base, AQSFRC, pc->ctx.aqsfrc);
  468. ehrpwm_write(pc->mmio_base, AQCSFRC, pc->ctx.aqcsfrc);
  469. ehrpwm_write(pc->mmio_base, TBCTL, pc->ctx.tbctl);
  470. }
  471. static int ehrpwm_pwm_suspend(struct device *dev)
  472. {
  473. struct ehrpwm_pwm_chip *pc = dev_get_drvdata(dev);
  474. int i;
  475. ehrpwm_pwm_save_context(pc);
  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. /* Disable explicitly if PWM is running */
  481. pm_runtime_put_sync(dev);
  482. }
  483. return 0;
  484. }
  485. static int ehrpwm_pwm_resume(struct device *dev)
  486. {
  487. struct ehrpwm_pwm_chip *pc = dev_get_drvdata(dev);
  488. int i;
  489. for (i = 0; i < pc->chip.npwm; i++) {
  490. struct pwm_device *pwm = &pc->chip.pwms[i];
  491. if (!pwm_is_enabled(pwm))
  492. continue;
  493. /* Enable explicitly if PWM was running */
  494. pm_runtime_get_sync(dev);
  495. }
  496. ehrpwm_pwm_restore_context(pc);
  497. return 0;
  498. }
  499. #endif
  500. static SIMPLE_DEV_PM_OPS(ehrpwm_pwm_pm_ops, ehrpwm_pwm_suspend,
  501. ehrpwm_pwm_resume);
  502. static struct platform_driver ehrpwm_pwm_driver = {
  503. .driver = {
  504. .name = "ehrpwm",
  505. .of_match_table = ehrpwm_of_match,
  506. .pm = &ehrpwm_pwm_pm_ops,
  507. },
  508. .probe = ehrpwm_pwm_probe,
  509. .remove = ehrpwm_pwm_remove,
  510. };
  511. module_platform_driver(ehrpwm_pwm_driver);
  512. MODULE_DESCRIPTION("EHRPWM PWM driver");
  513. MODULE_AUTHOR("Texas Instruments");
  514. MODULE_LICENSE("GPL");