pwm-tiecap.c 8.0 KB

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