pwm-tiehrpwm.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  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. /* EHRPWM registers and bits definitions */
  28. /* Time base module registers */
  29. #define TBCTL 0x00
  30. #define TBPRD 0x0A
  31. #define TBCTL_RUN_MASK (BIT(15) | BIT(14))
  32. #define TBCTL_STOP_NEXT 0
  33. #define TBCTL_STOP_ON_CYCLE BIT(14)
  34. #define TBCTL_FREE_RUN (BIT(15) | BIT(14))
  35. #define TBCTL_PRDLD_MASK BIT(3)
  36. #define TBCTL_PRDLD_SHDW 0
  37. #define TBCTL_PRDLD_IMDT BIT(3)
  38. #define TBCTL_CLKDIV_MASK (BIT(12) | BIT(11) | BIT(10) | BIT(9) | \
  39. BIT(8) | BIT(7))
  40. #define TBCTL_CTRMODE_MASK (BIT(1) | BIT(0))
  41. #define TBCTL_CTRMODE_UP 0
  42. #define TBCTL_CTRMODE_DOWN BIT(0)
  43. #define TBCTL_CTRMODE_UPDOWN BIT(1)
  44. #define TBCTL_CTRMODE_FREEZE (BIT(1) | BIT(0))
  45. #define TBCTL_HSPCLKDIV_SHIFT 7
  46. #define TBCTL_CLKDIV_SHIFT 10
  47. #define CLKDIV_MAX 7
  48. #define HSPCLKDIV_MAX 7
  49. #define PERIOD_MAX 0xFFFF
  50. /* compare module registers */
  51. #define CMPA 0x12
  52. #define CMPB 0x14
  53. /* Action qualifier module registers */
  54. #define AQCTLA 0x16
  55. #define AQCTLB 0x18
  56. #define AQSFRC 0x1A
  57. #define AQCSFRC 0x1C
  58. #define AQCTL_CBU_MASK (BIT(9) | BIT(8))
  59. #define AQCTL_CBU_FRCLOW BIT(8)
  60. #define AQCTL_CBU_FRCHIGH BIT(9)
  61. #define AQCTL_CBU_FRCTOGGLE (BIT(9) | BIT(8))
  62. #define AQCTL_CAU_MASK (BIT(5) | BIT(4))
  63. #define AQCTL_CAU_FRCLOW BIT(4)
  64. #define AQCTL_CAU_FRCHIGH BIT(5)
  65. #define AQCTL_CAU_FRCTOGGLE (BIT(5) | BIT(4))
  66. #define AQCTL_PRD_MASK (BIT(3) | BIT(2))
  67. #define AQCTL_PRD_FRCLOW BIT(2)
  68. #define AQCTL_PRD_FRCHIGH BIT(3)
  69. #define AQCTL_PRD_FRCTOGGLE (BIT(3) | BIT(2))
  70. #define AQCTL_ZRO_MASK (BIT(1) | BIT(0))
  71. #define AQCTL_ZRO_FRCLOW BIT(0)
  72. #define AQCTL_ZRO_FRCHIGH BIT(1)
  73. #define AQCTL_ZRO_FRCTOGGLE (BIT(1) | BIT(0))
  74. #define AQCTL_CHANA_POLNORMAL (AQCTL_CAU_FRCLOW | AQCTL_PRD_FRCHIGH | \
  75. AQCTL_ZRO_FRCHIGH)
  76. #define AQCTL_CHANA_POLINVERSED (AQCTL_CAU_FRCHIGH | AQCTL_PRD_FRCLOW | \
  77. AQCTL_ZRO_FRCLOW)
  78. #define AQCTL_CHANB_POLNORMAL (AQCTL_CBU_FRCLOW | AQCTL_PRD_FRCHIGH | \
  79. AQCTL_ZRO_FRCHIGH)
  80. #define AQCTL_CHANB_POLINVERSED (AQCTL_CBU_FRCHIGH | AQCTL_PRD_FRCLOW | \
  81. AQCTL_ZRO_FRCLOW)
  82. #define AQSFRC_RLDCSF_MASK (BIT(7) | BIT(6))
  83. #define AQSFRC_RLDCSF_ZRO 0
  84. #define AQSFRC_RLDCSF_PRD BIT(6)
  85. #define AQSFRC_RLDCSF_ZROPRD BIT(7)
  86. #define AQSFRC_RLDCSF_IMDT (BIT(7) | BIT(6))
  87. #define AQCSFRC_CSFB_MASK (BIT(3) | BIT(2))
  88. #define AQCSFRC_CSFB_FRCDIS 0
  89. #define AQCSFRC_CSFB_FRCLOW BIT(2)
  90. #define AQCSFRC_CSFB_FRCHIGH BIT(3)
  91. #define AQCSFRC_CSFB_DISSWFRC (BIT(3) | BIT(2))
  92. #define AQCSFRC_CSFA_MASK (BIT(1) | BIT(0))
  93. #define AQCSFRC_CSFA_FRCDIS 0
  94. #define AQCSFRC_CSFA_FRCLOW BIT(0)
  95. #define AQCSFRC_CSFA_FRCHIGH BIT(1)
  96. #define AQCSFRC_CSFA_DISSWFRC (BIT(1) | BIT(0))
  97. #define NUM_PWM_CHANNEL 2 /* EHRPWM channels */
  98. struct ehrpwm_pwm_chip {
  99. struct pwm_chip chip;
  100. unsigned int clk_rate;
  101. void __iomem *mmio_base;
  102. unsigned long period_cycles[NUM_PWM_CHANNEL];
  103. enum pwm_polarity polarity[NUM_PWM_CHANNEL];
  104. };
  105. static inline struct ehrpwm_pwm_chip *to_ehrpwm_pwm_chip(struct pwm_chip *chip)
  106. {
  107. return container_of(chip, struct ehrpwm_pwm_chip, chip);
  108. }
  109. static void ehrpwm_write(void *base, int offset, unsigned int val)
  110. {
  111. writew(val & 0xFFFF, base + offset);
  112. }
  113. static void ehrpwm_modify(void *base, int offset,
  114. unsigned short mask, unsigned short val)
  115. {
  116. unsigned short regval;
  117. regval = readw(base + offset);
  118. regval &= ~mask;
  119. regval |= val & mask;
  120. writew(regval, base + offset);
  121. }
  122. /**
  123. * set_prescale_div - Set up the prescaler divider function
  124. * @rqst_prescaler: prescaler value min
  125. * @prescale_div: prescaler value set
  126. * @tb_clk_div: Time Base Control prescaler bits
  127. */
  128. static int set_prescale_div(unsigned long rqst_prescaler,
  129. unsigned short *prescale_div, unsigned short *tb_clk_div)
  130. {
  131. unsigned int clkdiv, hspclkdiv;
  132. for (clkdiv = 0; clkdiv <= CLKDIV_MAX; clkdiv++) {
  133. for (hspclkdiv = 0; hspclkdiv <= HSPCLKDIV_MAX; hspclkdiv++) {
  134. /*
  135. * calculations for prescaler value :
  136. * prescale_div = HSPCLKDIVIDER * CLKDIVIDER.
  137. * HSPCLKDIVIDER = 2 ** hspclkdiv
  138. * CLKDIVIDER = (1), if clkdiv == 0 *OR*
  139. * (2 * clkdiv), if clkdiv != 0
  140. *
  141. * Configure prescale_div value such that period
  142. * register value is less than 65535.
  143. */
  144. *prescale_div = (1 << clkdiv) *
  145. (hspclkdiv ? (hspclkdiv * 2) : 1);
  146. if (*prescale_div > rqst_prescaler) {
  147. *tb_clk_div = (clkdiv << TBCTL_CLKDIV_SHIFT) |
  148. (hspclkdiv << TBCTL_HSPCLKDIV_SHIFT);
  149. return 0;
  150. }
  151. }
  152. }
  153. return 1;
  154. }
  155. static void configure_polarity(struct ehrpwm_pwm_chip *pc, int chan)
  156. {
  157. int aqctl_reg;
  158. unsigned short aqctl_val, aqctl_mask;
  159. /*
  160. * Configure PWM output to HIGH/LOW level on counter
  161. * reaches compare register value and LOW/HIGH level
  162. * on counter value reaches period register value and
  163. * zero value on counter
  164. */
  165. if (chan == 1) {
  166. aqctl_reg = AQCTLB;
  167. aqctl_mask = AQCTL_CBU_MASK;
  168. if (pc->polarity[chan] == PWM_POLARITY_INVERSED)
  169. aqctl_val = AQCTL_CHANB_POLINVERSED;
  170. else
  171. aqctl_val = AQCTL_CHANB_POLNORMAL;
  172. } else {
  173. aqctl_reg = AQCTLA;
  174. aqctl_mask = AQCTL_CAU_MASK;
  175. if (pc->polarity[chan] == PWM_POLARITY_INVERSED)
  176. aqctl_val = AQCTL_CHANA_POLINVERSED;
  177. else
  178. aqctl_val = AQCTL_CHANA_POLNORMAL;
  179. }
  180. aqctl_mask |= AQCTL_PRD_MASK | AQCTL_ZRO_MASK;
  181. ehrpwm_modify(pc->mmio_base, aqctl_reg, aqctl_mask, aqctl_val);
  182. }
  183. /*
  184. * period_ns = 10^9 * (ps_divval * period_cycles) / PWM_CLK_RATE
  185. * duty_ns = 10^9 * (ps_divval * duty_cycles) / PWM_CLK_RATE
  186. */
  187. static int ehrpwm_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm,
  188. int duty_ns, int period_ns)
  189. {
  190. struct ehrpwm_pwm_chip *pc = to_ehrpwm_pwm_chip(chip);
  191. unsigned long long c;
  192. unsigned long period_cycles, duty_cycles;
  193. unsigned short ps_divval, tb_divval;
  194. int i, cmp_reg;
  195. if (period_ns > NSEC_PER_SEC)
  196. return -ERANGE;
  197. c = pc->clk_rate;
  198. c = c * period_ns;
  199. do_div(c, NSEC_PER_SEC);
  200. period_cycles = (unsigned long)c;
  201. if (period_cycles < 1) {
  202. period_cycles = 1;
  203. duty_cycles = 1;
  204. } else {
  205. c = pc->clk_rate;
  206. c = c * duty_ns;
  207. do_div(c, NSEC_PER_SEC);
  208. duty_cycles = (unsigned long)c;
  209. }
  210. /*
  211. * Period values should be same for multiple PWM channels as IP uses
  212. * same period register for multiple channels.
  213. */
  214. for (i = 0; i < NUM_PWM_CHANNEL; i++) {
  215. if (pc->period_cycles[i] &&
  216. (pc->period_cycles[i] != period_cycles)) {
  217. /*
  218. * Allow channel to reconfigure period if no other
  219. * channels being configured.
  220. */
  221. if (i == pwm->hwpwm)
  222. continue;
  223. dev_err(chip->dev, "Period value conflicts with channel %d\n",
  224. i);
  225. return -EINVAL;
  226. }
  227. }
  228. pc->period_cycles[pwm->hwpwm] = period_cycles;
  229. /* Configure clock prescaler to support Low frequency PWM wave */
  230. if (set_prescale_div(period_cycles/PERIOD_MAX, &ps_divval,
  231. &tb_divval)) {
  232. dev_err(chip->dev, "Unsupported values\n");
  233. return -EINVAL;
  234. }
  235. pm_runtime_get_sync(chip->dev);
  236. /* Update clock prescaler values */
  237. ehrpwm_modify(pc->mmio_base, TBCTL, TBCTL_CLKDIV_MASK, tb_divval);
  238. /* Update period & duty cycle with presacler division */
  239. period_cycles = period_cycles / ps_divval;
  240. duty_cycles = duty_cycles / ps_divval;
  241. /* Configure shadow loading on Period register */
  242. ehrpwm_modify(pc->mmio_base, TBCTL, TBCTL_PRDLD_MASK, TBCTL_PRDLD_SHDW);
  243. ehrpwm_write(pc->mmio_base, TBPRD, period_cycles);
  244. /* Configure ehrpwm counter for up-count mode */
  245. ehrpwm_modify(pc->mmio_base, TBCTL, TBCTL_CTRMODE_MASK,
  246. TBCTL_CTRMODE_UP);
  247. if (pwm->hwpwm == 1)
  248. /* Channel 1 configured with compare B register */
  249. cmp_reg = CMPB;
  250. else
  251. /* Channel 0 configured with compare A register */
  252. cmp_reg = CMPA;
  253. ehrpwm_write(pc->mmio_base, cmp_reg, duty_cycles);
  254. pm_runtime_put_sync(chip->dev);
  255. return 0;
  256. }
  257. static int ehrpwm_pwm_set_polarity(struct pwm_chip *chip,
  258. struct pwm_device *pwm, enum pwm_polarity polarity)
  259. {
  260. struct ehrpwm_pwm_chip *pc = to_ehrpwm_pwm_chip(chip);
  261. /* Configuration of polarity in hardware delayed, do at enable */
  262. pc->polarity[pwm->hwpwm] = polarity;
  263. return 0;
  264. }
  265. static int ehrpwm_pwm_enable(struct pwm_chip *chip, struct pwm_device *pwm)
  266. {
  267. struct ehrpwm_pwm_chip *pc = to_ehrpwm_pwm_chip(chip);
  268. unsigned short aqcsfrc_val, aqcsfrc_mask;
  269. /* Leave clock enabled on enabling PWM */
  270. pm_runtime_get_sync(chip->dev);
  271. /* Disabling Action Qualifier on PWM output */
  272. if (pwm->hwpwm) {
  273. aqcsfrc_val = AQCSFRC_CSFB_FRCDIS;
  274. aqcsfrc_mask = AQCSFRC_CSFB_MASK;
  275. } else {
  276. aqcsfrc_val = AQCSFRC_CSFA_FRCDIS;
  277. aqcsfrc_mask = AQCSFRC_CSFA_MASK;
  278. }
  279. /* Changes to shadow mode */
  280. ehrpwm_modify(pc->mmio_base, AQSFRC, AQSFRC_RLDCSF_MASK,
  281. AQSFRC_RLDCSF_ZRO);
  282. ehrpwm_modify(pc->mmio_base, AQCSFRC, aqcsfrc_mask, aqcsfrc_val);
  283. /* Channels polarity can be configured from action qualifier module */
  284. configure_polarity(pc, pwm->hwpwm);
  285. /* Enable time counter for free_run */
  286. ehrpwm_modify(pc->mmio_base, TBCTL, TBCTL_RUN_MASK, TBCTL_FREE_RUN);
  287. return 0;
  288. }
  289. static void ehrpwm_pwm_disable(struct pwm_chip *chip, struct pwm_device *pwm)
  290. {
  291. struct ehrpwm_pwm_chip *pc = to_ehrpwm_pwm_chip(chip);
  292. unsigned short aqcsfrc_val, aqcsfrc_mask;
  293. /* Action Qualifier puts PWM output low forcefully */
  294. if (pwm->hwpwm) {
  295. aqcsfrc_val = AQCSFRC_CSFB_FRCLOW;
  296. aqcsfrc_mask = AQCSFRC_CSFB_MASK;
  297. } else {
  298. aqcsfrc_val = AQCSFRC_CSFA_FRCLOW;
  299. aqcsfrc_mask = AQCSFRC_CSFA_MASK;
  300. }
  301. /*
  302. * Changes to immediate action on Action Qualifier. This puts
  303. * Action Qualifier control on PWM output from next TBCLK
  304. */
  305. ehrpwm_modify(pc->mmio_base, AQSFRC, AQSFRC_RLDCSF_MASK,
  306. AQSFRC_RLDCSF_IMDT);
  307. ehrpwm_modify(pc->mmio_base, AQCSFRC, aqcsfrc_mask, aqcsfrc_val);
  308. /* Stop Time base counter */
  309. ehrpwm_modify(pc->mmio_base, TBCTL, TBCTL_RUN_MASK, TBCTL_STOP_NEXT);
  310. /* Disable clock on PWM disable */
  311. pm_runtime_put_sync(chip->dev);
  312. }
  313. static void ehrpwm_pwm_free(struct pwm_chip *chip, struct pwm_device *pwm)
  314. {
  315. struct ehrpwm_pwm_chip *pc = to_ehrpwm_pwm_chip(chip);
  316. if (test_bit(PWMF_ENABLED, &pwm->flags)) {
  317. dev_warn(chip->dev, "Removing PWM device without disabling\n");
  318. pm_runtime_put_sync(chip->dev);
  319. }
  320. /* set period value to zero on free */
  321. pc->period_cycles[pwm->hwpwm] = 0;
  322. }
  323. static const struct pwm_ops ehrpwm_pwm_ops = {
  324. .free = ehrpwm_pwm_free,
  325. .config = ehrpwm_pwm_config,
  326. .set_polarity = ehrpwm_pwm_set_polarity,
  327. .enable = ehrpwm_pwm_enable,
  328. .disable = ehrpwm_pwm_disable,
  329. .owner = THIS_MODULE,
  330. };
  331. static int __devinit ehrpwm_pwm_probe(struct platform_device *pdev)
  332. {
  333. int ret;
  334. struct resource *r;
  335. struct clk *clk;
  336. struct ehrpwm_pwm_chip *pc;
  337. pc = devm_kzalloc(&pdev->dev, sizeof(*pc), GFP_KERNEL);
  338. if (!pc) {
  339. dev_err(&pdev->dev, "failed to allocate memory\n");
  340. return -ENOMEM;
  341. }
  342. clk = devm_clk_get(&pdev->dev, "fck");
  343. if (IS_ERR(clk)) {
  344. dev_err(&pdev->dev, "failed to get clock\n");
  345. return PTR_ERR(clk);
  346. }
  347. pc->clk_rate = clk_get_rate(clk);
  348. if (!pc->clk_rate) {
  349. dev_err(&pdev->dev, "failed to get clock rate\n");
  350. return -EINVAL;
  351. }
  352. pc->chip.dev = &pdev->dev;
  353. pc->chip.ops = &ehrpwm_pwm_ops;
  354. pc->chip.base = -1;
  355. pc->chip.npwm = NUM_PWM_CHANNEL;
  356. r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  357. if (!r) {
  358. dev_err(&pdev->dev, "no memory resource defined\n");
  359. return -ENODEV;
  360. }
  361. pc->mmio_base = devm_request_and_ioremap(&pdev->dev, r);
  362. if (!pc->mmio_base)
  363. return -EADDRNOTAVAIL;
  364. ret = pwmchip_add(&pc->chip);
  365. if (ret < 0) {
  366. dev_err(&pdev->dev, "pwmchip_add() failed: %d\n", ret);
  367. return ret;
  368. }
  369. pm_runtime_enable(&pdev->dev);
  370. platform_set_drvdata(pdev, pc);
  371. return 0;
  372. }
  373. static int __devexit ehrpwm_pwm_remove(struct platform_device *pdev)
  374. {
  375. struct ehrpwm_pwm_chip *pc = platform_get_drvdata(pdev);
  376. pm_runtime_put_sync(&pdev->dev);
  377. pm_runtime_disable(&pdev->dev);
  378. return pwmchip_remove(&pc->chip);
  379. }
  380. static struct platform_driver ehrpwm_pwm_driver = {
  381. .driver = {
  382. .name = "ehrpwm",
  383. },
  384. .probe = ehrpwm_pwm_probe,
  385. .remove = __devexit_p(ehrpwm_pwm_remove),
  386. };
  387. module_platform_driver(ehrpwm_pwm_driver);
  388. MODULE_DESCRIPTION("EHRPWM PWM driver");
  389. MODULE_AUTHOR("Texas Instruments");
  390. MODULE_LICENSE("GPL");