pwm-tiehrpwm.c 12 KB

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