pwm-tiehrpwm.c 16 KB

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