pwm-ep93xx.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. /*
  2. * PWM framework driver for Cirrus Logic EP93xx
  3. *
  4. * Copyright (c) 2009 Matthieu Crapet <mcrapet@gmail.com>
  5. * Copyright (c) 2009, 2013 H Hartley Sweeten <hsweeten@visionengravers.com>
  6. *
  7. * EP9301/02 have only one channel:
  8. * platform device ep93xx-pwm.1 - PWMOUT1 (EGPIO14)
  9. *
  10. * EP9307 has only one channel:
  11. * platform device ep93xx-pwm.0 - PWMOUT
  12. *
  13. * EP9312/15 have two channels:
  14. * platform device ep93xx-pwm.0 - PWMOUT
  15. * platform device ep93xx-pwm.1 - PWMOUT1 (EGPIO14)
  16. *
  17. * This program is free software; you can redistribute it and/or
  18. * modify it under the terms of the GNU General Public License
  19. * as published by the Free Software Foundation; either version
  20. * 2 of the License, or (at your option) any later version.
  21. *
  22. * This program is distributed in the hope that it will be useful,
  23. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  24. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  25. * GNU General Public License for more details.
  26. */
  27. #include <linux/module.h>
  28. #include <linux/platform_device.h>
  29. #include <linux/slab.h>
  30. #include <linux/clk.h>
  31. #include <linux/err.h>
  32. #include <linux/io.h>
  33. #include <linux/pwm.h>
  34. #include <asm/div64.h>
  35. #include <mach/platform.h> /* for ep93xx_pwm_{acquire,release}_gpio() */
  36. #define EP93XX_PWMx_TERM_COUNT 0x00
  37. #define EP93XX_PWMx_DUTY_CYCLE 0x04
  38. #define EP93XX_PWMx_ENABLE 0x08
  39. #define EP93XX_PWMx_INVERT 0x0c
  40. struct ep93xx_pwm {
  41. void __iomem *base;
  42. struct clk *clk;
  43. struct pwm_chip chip;
  44. };
  45. static inline struct ep93xx_pwm *to_ep93xx_pwm(struct pwm_chip *chip)
  46. {
  47. return container_of(chip, struct ep93xx_pwm, chip);
  48. }
  49. static int ep93xx_pwm_request(struct pwm_chip *chip, struct pwm_device *pwm)
  50. {
  51. struct platform_device *pdev = to_platform_device(chip->dev);
  52. return ep93xx_pwm_acquire_gpio(pdev);
  53. }
  54. static void ep93xx_pwm_free(struct pwm_chip *chip, struct pwm_device *pwm)
  55. {
  56. struct platform_device *pdev = to_platform_device(chip->dev);
  57. ep93xx_pwm_release_gpio(pdev);
  58. }
  59. static int ep93xx_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm,
  60. int duty_ns, int period_ns)
  61. {
  62. struct ep93xx_pwm *ep93xx_pwm = to_ep93xx_pwm(chip);
  63. void __iomem *base = ep93xx_pwm->base;
  64. unsigned long long c;
  65. unsigned long period_cycles;
  66. unsigned long duty_cycles;
  67. unsigned long term;
  68. int ret = 0;
  69. /*
  70. * The clock needs to be enabled to access the PWM registers.
  71. * Configuration can be changed at any time.
  72. */
  73. if (!pwm_is_enabled(pwm)) {
  74. ret = clk_enable(ep93xx_pwm->clk);
  75. if (ret)
  76. return ret;
  77. }
  78. c = clk_get_rate(ep93xx_pwm->clk);
  79. c *= period_ns;
  80. do_div(c, 1000000000);
  81. period_cycles = c;
  82. c = period_cycles;
  83. c *= duty_ns;
  84. do_div(c, period_ns);
  85. duty_cycles = c;
  86. if (period_cycles < 0x10000 && duty_cycles < 0x10000) {
  87. term = readw(base + EP93XX_PWMx_TERM_COUNT);
  88. /* Order is important if PWM is running */
  89. if (period_cycles > term) {
  90. writew(period_cycles, base + EP93XX_PWMx_TERM_COUNT);
  91. writew(duty_cycles, base + EP93XX_PWMx_DUTY_CYCLE);
  92. } else {
  93. writew(duty_cycles, base + EP93XX_PWMx_DUTY_CYCLE);
  94. writew(period_cycles, base + EP93XX_PWMx_TERM_COUNT);
  95. }
  96. } else {
  97. ret = -EINVAL;
  98. }
  99. if (!pwm_is_enabled(pwm))
  100. clk_disable(ep93xx_pwm->clk);
  101. return ret;
  102. }
  103. static int ep93xx_pwm_polarity(struct pwm_chip *chip, struct pwm_device *pwm,
  104. enum pwm_polarity polarity)
  105. {
  106. struct ep93xx_pwm *ep93xx_pwm = to_ep93xx_pwm(chip);
  107. int ret;
  108. /*
  109. * The clock needs to be enabled to access the PWM registers.
  110. * Polarity can only be changed when the PWM is disabled.
  111. */
  112. ret = clk_enable(ep93xx_pwm->clk);
  113. if (ret)
  114. return ret;
  115. if (polarity == PWM_POLARITY_INVERSED)
  116. writew(0x1, ep93xx_pwm->base + EP93XX_PWMx_INVERT);
  117. else
  118. writew(0x0, ep93xx_pwm->base + EP93XX_PWMx_INVERT);
  119. clk_disable(ep93xx_pwm->clk);
  120. return 0;
  121. }
  122. static int ep93xx_pwm_enable(struct pwm_chip *chip, struct pwm_device *pwm)
  123. {
  124. struct ep93xx_pwm *ep93xx_pwm = to_ep93xx_pwm(chip);
  125. int ret;
  126. ret = clk_enable(ep93xx_pwm->clk);
  127. if (ret)
  128. return ret;
  129. writew(0x1, ep93xx_pwm->base + EP93XX_PWMx_ENABLE);
  130. return 0;
  131. }
  132. static void ep93xx_pwm_disable(struct pwm_chip *chip, struct pwm_device *pwm)
  133. {
  134. struct ep93xx_pwm *ep93xx_pwm = to_ep93xx_pwm(chip);
  135. writew(0x0, ep93xx_pwm->base + EP93XX_PWMx_ENABLE);
  136. clk_disable(ep93xx_pwm->clk);
  137. }
  138. static const struct pwm_ops ep93xx_pwm_ops = {
  139. .request = ep93xx_pwm_request,
  140. .free = ep93xx_pwm_free,
  141. .config = ep93xx_pwm_config,
  142. .set_polarity = ep93xx_pwm_polarity,
  143. .enable = ep93xx_pwm_enable,
  144. .disable = ep93xx_pwm_disable,
  145. .owner = THIS_MODULE,
  146. };
  147. static int ep93xx_pwm_probe(struct platform_device *pdev)
  148. {
  149. struct ep93xx_pwm *ep93xx_pwm;
  150. struct resource *res;
  151. int ret;
  152. ep93xx_pwm = devm_kzalloc(&pdev->dev, sizeof(*ep93xx_pwm), GFP_KERNEL);
  153. if (!ep93xx_pwm)
  154. return -ENOMEM;
  155. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  156. ep93xx_pwm->base = devm_ioremap_resource(&pdev->dev, res);
  157. if (IS_ERR(ep93xx_pwm->base))
  158. return PTR_ERR(ep93xx_pwm->base);
  159. ep93xx_pwm->clk = devm_clk_get(&pdev->dev, "pwm_clk");
  160. if (IS_ERR(ep93xx_pwm->clk))
  161. return PTR_ERR(ep93xx_pwm->clk);
  162. ep93xx_pwm->chip.dev = &pdev->dev;
  163. ep93xx_pwm->chip.ops = &ep93xx_pwm_ops;
  164. ep93xx_pwm->chip.base = -1;
  165. ep93xx_pwm->chip.npwm = 1;
  166. ret = pwmchip_add(&ep93xx_pwm->chip);
  167. if (ret < 0)
  168. return ret;
  169. platform_set_drvdata(pdev, ep93xx_pwm);
  170. return 0;
  171. }
  172. static int ep93xx_pwm_remove(struct platform_device *pdev)
  173. {
  174. struct ep93xx_pwm *ep93xx_pwm = platform_get_drvdata(pdev);
  175. return pwmchip_remove(&ep93xx_pwm->chip);
  176. }
  177. static struct platform_driver ep93xx_pwm_driver = {
  178. .driver = {
  179. .name = "ep93xx-pwm",
  180. },
  181. .probe = ep93xx_pwm_probe,
  182. .remove = ep93xx_pwm_remove,
  183. };
  184. module_platform_driver(ep93xx_pwm_driver);
  185. MODULE_DESCRIPTION("Cirrus Logic EP93xx PWM driver");
  186. MODULE_AUTHOR("Matthieu Crapet <mcrapet@gmail.com>");
  187. MODULE_AUTHOR("H Hartley Sweeten <hsweeten@visionengravers.com>");
  188. MODULE_ALIAS("platform:ep93xx-pwm");
  189. MODULE_LICENSE("GPL");