pwm-tiecap.c 7.8 KB

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