pwm-tiecap.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. /*
  2. * ECAP 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/io.h>
  23. #include <linux/err.h>
  24. #include <linux/clk.h>
  25. #include <linux/pm_runtime.h>
  26. #include <linux/pwm.h>
  27. #include <linux/of_device.h>
  28. #include <linux/pinctrl/consumer.h>
  29. #include "pwm-tipwmss.h"
  30. /* ECAP registers and bits definitions */
  31. #define CAP1 0x08
  32. #define CAP2 0x0C
  33. #define CAP3 0x10
  34. #define CAP4 0x14
  35. #define ECCTL2 0x2A
  36. #define ECCTL2_APWM_POL_LOW BIT(10)
  37. #define ECCTL2_APWM_MODE BIT(9)
  38. #define ECCTL2_SYNC_SEL_DISA (BIT(7) | BIT(6))
  39. #define ECCTL2_TSCTR_FREERUN BIT(4)
  40. struct ecap_context {
  41. u32 cap3;
  42. u32 cap4;
  43. u16 ecctl2;
  44. };
  45. struct ecap_pwm_chip {
  46. struct pwm_chip chip;
  47. unsigned int clk_rate;
  48. void __iomem *mmio_base;
  49. struct ecap_context ctx;
  50. };
  51. static inline struct ecap_pwm_chip *to_ecap_pwm_chip(struct pwm_chip *chip)
  52. {
  53. return container_of(chip, struct ecap_pwm_chip, chip);
  54. }
  55. /*
  56. * period_ns = 10^9 * period_cycles / PWM_CLK_RATE
  57. * duty_ns = 10^9 * duty_cycles / PWM_CLK_RATE
  58. */
  59. static int ecap_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm,
  60. int duty_ns, int period_ns)
  61. {
  62. struct ecap_pwm_chip *pc = to_ecap_pwm_chip(chip);
  63. unsigned long long c;
  64. unsigned long period_cycles, duty_cycles;
  65. unsigned int reg_val;
  66. if (period_ns > NSEC_PER_SEC)
  67. return -ERANGE;
  68. c = pc->clk_rate;
  69. c = c * period_ns;
  70. do_div(c, NSEC_PER_SEC);
  71. period_cycles = (unsigned long)c;
  72. if (period_cycles < 1) {
  73. period_cycles = 1;
  74. duty_cycles = 1;
  75. } else {
  76. c = pc->clk_rate;
  77. c = c * duty_ns;
  78. do_div(c, NSEC_PER_SEC);
  79. duty_cycles = (unsigned long)c;
  80. }
  81. pm_runtime_get_sync(pc->chip.dev);
  82. reg_val = readw(pc->mmio_base + ECCTL2);
  83. /* Configure APWM mode & disable sync option */
  84. reg_val |= ECCTL2_APWM_MODE | ECCTL2_SYNC_SEL_DISA;
  85. writew(reg_val, pc->mmio_base + ECCTL2);
  86. if (!test_bit(PWMF_ENABLED, &pwm->flags)) {
  87. /* Update active registers if not running */
  88. writel(duty_cycles, pc->mmio_base + CAP2);
  89. writel(period_cycles, pc->mmio_base + CAP1);
  90. } else {
  91. /*
  92. * Update shadow registers to configure period and
  93. * compare values. This helps current PWM period to
  94. * complete on reconfiguring
  95. */
  96. writel(duty_cycles, pc->mmio_base + CAP4);
  97. writel(period_cycles, pc->mmio_base + CAP3);
  98. }
  99. if (!test_bit(PWMF_ENABLED, &pwm->flags)) {
  100. reg_val = readw(pc->mmio_base + ECCTL2);
  101. /* Disable APWM mode to put APWM output Low */
  102. reg_val &= ~ECCTL2_APWM_MODE;
  103. writew(reg_val, pc->mmio_base + ECCTL2);
  104. }
  105. pm_runtime_put_sync(pc->chip.dev);
  106. return 0;
  107. }
  108. static int ecap_pwm_set_polarity(struct pwm_chip *chip, struct pwm_device *pwm,
  109. enum pwm_polarity polarity)
  110. {
  111. struct ecap_pwm_chip *pc = to_ecap_pwm_chip(chip);
  112. unsigned short reg_val;
  113. pm_runtime_get_sync(pc->chip.dev);
  114. reg_val = readw(pc->mmio_base + ECCTL2);
  115. if (polarity == PWM_POLARITY_INVERSED)
  116. /* Duty cycle defines LOW period of PWM */
  117. reg_val |= ECCTL2_APWM_POL_LOW;
  118. else
  119. /* Duty cycle defines HIGH period of PWM */
  120. reg_val &= ~ECCTL2_APWM_POL_LOW;
  121. writew(reg_val, pc->mmio_base + ECCTL2);
  122. pm_runtime_put_sync(pc->chip.dev);
  123. return 0;
  124. }
  125. static int ecap_pwm_enable(struct pwm_chip *chip, struct pwm_device *pwm)
  126. {
  127. struct ecap_pwm_chip *pc = to_ecap_pwm_chip(chip);
  128. unsigned int reg_val;
  129. /* Leave clock enabled on enabling PWM */
  130. pm_runtime_get_sync(pc->chip.dev);
  131. /*
  132. * Enable 'Free run Time stamp counter mode' to start counter
  133. * and 'APWM mode' to enable APWM output
  134. */
  135. reg_val = readw(pc->mmio_base + ECCTL2);
  136. reg_val |= ECCTL2_TSCTR_FREERUN | ECCTL2_APWM_MODE;
  137. writew(reg_val, pc->mmio_base + ECCTL2);
  138. return 0;
  139. }
  140. static void ecap_pwm_disable(struct pwm_chip *chip, struct pwm_device *pwm)
  141. {
  142. struct ecap_pwm_chip *pc = to_ecap_pwm_chip(chip);
  143. unsigned int reg_val;
  144. /*
  145. * Disable 'Free run Time stamp counter mode' to stop counter
  146. * and 'APWM mode' to put APWM output to low
  147. */
  148. reg_val = readw(pc->mmio_base + ECCTL2);
  149. reg_val &= ~(ECCTL2_TSCTR_FREERUN | ECCTL2_APWM_MODE);
  150. writew(reg_val, pc->mmio_base + ECCTL2);
  151. /* Disable clock on PWM disable */
  152. pm_runtime_put_sync(pc->chip.dev);
  153. }
  154. static void ecap_pwm_free(struct pwm_chip *chip, struct pwm_device *pwm)
  155. {
  156. if (test_bit(PWMF_ENABLED, &pwm->flags)) {
  157. dev_warn(chip->dev, "Removing PWM device without disabling\n");
  158. pm_runtime_put_sync(chip->dev);
  159. }
  160. }
  161. static const struct pwm_ops ecap_pwm_ops = {
  162. .free = ecap_pwm_free,
  163. .config = ecap_pwm_config,
  164. .set_polarity = ecap_pwm_set_polarity,
  165. .enable = ecap_pwm_enable,
  166. .disable = ecap_pwm_disable,
  167. .owner = THIS_MODULE,
  168. };
  169. static const struct of_device_id ecap_of_match[] = {
  170. { .compatible = "ti,am33xx-ecap" },
  171. {},
  172. };
  173. MODULE_DEVICE_TABLE(of, ecap_of_match);
  174. static int ecap_pwm_probe(struct platform_device *pdev)
  175. {
  176. int ret;
  177. struct resource *r;
  178. struct clk *clk;
  179. struct ecap_pwm_chip *pc;
  180. u16 status;
  181. struct pinctrl *pinctrl;
  182. pinctrl = devm_pinctrl_get_select_default(&pdev->dev);
  183. if (IS_ERR(pinctrl))
  184. dev_warn(&pdev->dev, "unable to select pin group\n");
  185. pc = devm_kzalloc(&pdev->dev, sizeof(*pc), GFP_KERNEL);
  186. if (!pc) {
  187. dev_err(&pdev->dev, "failed to allocate memory\n");
  188. return -ENOMEM;
  189. }
  190. clk = devm_clk_get(&pdev->dev, "fck");
  191. if (IS_ERR(clk)) {
  192. dev_err(&pdev->dev, "failed to get clock\n");
  193. return PTR_ERR(clk);
  194. }
  195. pc->clk_rate = clk_get_rate(clk);
  196. if (!pc->clk_rate) {
  197. dev_err(&pdev->dev, "failed to get clock rate\n");
  198. return -EINVAL;
  199. }
  200. pc->chip.dev = &pdev->dev;
  201. pc->chip.ops = &ecap_pwm_ops;
  202. pc->chip.of_xlate = of_pwm_xlate_with_flags;
  203. pc->chip.of_pwm_n_cells = 3;
  204. pc->chip.base = -1;
  205. pc->chip.npwm = 1;
  206. r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  207. if (!r) {
  208. dev_err(&pdev->dev, "no memory resource defined\n");
  209. return -ENODEV;
  210. }
  211. pc->mmio_base = devm_ioremap_resource(&pdev->dev, r);
  212. if (IS_ERR(pc->mmio_base))
  213. return PTR_ERR(pc->mmio_base);
  214. ret = pwmchip_add(&pc->chip);
  215. if (ret < 0) {
  216. dev_err(&pdev->dev, "pwmchip_add() failed: %d\n", ret);
  217. return ret;
  218. }
  219. pm_runtime_enable(&pdev->dev);
  220. pm_runtime_get_sync(&pdev->dev);
  221. status = pwmss_submodule_state_change(pdev->dev.parent,
  222. PWMSS_ECAPCLK_EN);
  223. if (!(status & PWMSS_ECAPCLK_EN_ACK)) {
  224. dev_err(&pdev->dev, "PWMSS config space clock enable failed\n");
  225. ret = -EINVAL;
  226. goto pwmss_clk_failure;
  227. }
  228. pm_runtime_put_sync(&pdev->dev);
  229. platform_set_drvdata(pdev, pc);
  230. return 0;
  231. pwmss_clk_failure:
  232. pm_runtime_put_sync(&pdev->dev);
  233. pm_runtime_disable(&pdev->dev);
  234. pwmchip_remove(&pc->chip);
  235. return ret;
  236. }
  237. static int ecap_pwm_remove(struct platform_device *pdev)
  238. {
  239. struct ecap_pwm_chip *pc = platform_get_drvdata(pdev);
  240. pm_runtime_get_sync(&pdev->dev);
  241. /*
  242. * Due to hardware misbehaviour, acknowledge of the stop_req
  243. * is missing. Hence checking of the status bit skipped.
  244. */
  245. pwmss_submodule_state_change(pdev->dev.parent, PWMSS_ECAPCLK_STOP_REQ);
  246. pm_runtime_put_sync(&pdev->dev);
  247. pm_runtime_put_sync(&pdev->dev);
  248. pm_runtime_disable(&pdev->dev);
  249. return pwmchip_remove(&pc->chip);
  250. }
  251. void ecap_pwm_save_context(struct ecap_pwm_chip *pc)
  252. {
  253. pm_runtime_get_sync(pc->chip.dev);
  254. pc->ctx.ecctl2 = readw(pc->mmio_base + ECCTL2);
  255. pc->ctx.cap4 = readl(pc->mmio_base + CAP4);
  256. pc->ctx.cap3 = readl(pc->mmio_base + CAP3);
  257. pm_runtime_put_sync(pc->chip.dev);
  258. }
  259. void ecap_pwm_restore_context(struct ecap_pwm_chip *pc)
  260. {
  261. writel(pc->ctx.cap3, pc->mmio_base + CAP3);
  262. writel(pc->ctx.cap4, pc->mmio_base + CAP4);
  263. writew(pc->ctx.ecctl2, pc->mmio_base + ECCTL2);
  264. }
  265. static int ecap_pwm_suspend(struct device *dev)
  266. {
  267. struct ecap_pwm_chip *pc = dev_get_drvdata(dev);
  268. struct pwm_device *pwm = pc->chip.pwms;
  269. ecap_pwm_save_context(pc);
  270. /* Disable explicitly if PWM is running */
  271. if (test_bit(PWMF_ENABLED, &pwm->flags))
  272. pm_runtime_put_sync(dev);
  273. return 0;
  274. }
  275. static int ecap_pwm_resume(struct device *dev)
  276. {
  277. struct ecap_pwm_chip *pc = dev_get_drvdata(dev);
  278. struct pwm_device *pwm = pc->chip.pwms;
  279. /* Enable explicitly if PWM was running */
  280. if (test_bit(PWMF_ENABLED, &pwm->flags))
  281. pm_runtime_get_sync(dev);
  282. ecap_pwm_restore_context(pc);
  283. return 0;
  284. }
  285. static SIMPLE_DEV_PM_OPS(ecap_pwm_pm_ops, ecap_pwm_suspend, ecap_pwm_resume);
  286. static struct platform_driver ecap_pwm_driver = {
  287. .driver = {
  288. .name = "ecap",
  289. .owner = THIS_MODULE,
  290. .of_match_table = ecap_of_match,
  291. .pm = &ecap_pwm_pm_ops,
  292. },
  293. .probe = ecap_pwm_probe,
  294. .remove = ecap_pwm_remove,
  295. };
  296. module_platform_driver(ecap_pwm_driver);
  297. MODULE_DESCRIPTION("ECAP PWM driver");
  298. MODULE_AUTHOR("Texas Instruments");
  299. MODULE_LICENSE("GPL");