ep93xx_pwm.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. /*
  2. * Simple PWM driver for EP93XX
  3. *
  4. * (c) Copyright 2009 Matthieu Crapet <mcrapet@gmail.com>
  5. * (c) Copyright 2009 H Hartley Sweeten <hsweeten@visionengravers.com>
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License
  9. * as published by the Free Software Foundation; either version
  10. * 2 of the License, or (at your option) any later version.
  11. *
  12. * EP9307 has only one channel:
  13. * - PWMOUT
  14. *
  15. * EP9301/02/12/15 have two channels:
  16. * - PWMOUT
  17. * - PWMOUT1 (alternate function for EGPIO14)
  18. */
  19. #include <linux/module.h>
  20. #include <linux/platform_device.h>
  21. #include <linux/slab.h>
  22. #include <linux/clk.h>
  23. #include <linux/err.h>
  24. #include <linux/io.h>
  25. #include <mach/platform.h>
  26. #define EP93XX_PWMx_TERM_COUNT 0x00
  27. #define EP93XX_PWMx_DUTY_CYCLE 0x04
  28. #define EP93XX_PWMx_ENABLE 0x08
  29. #define EP93XX_PWMx_INVERT 0x0C
  30. #define EP93XX_PWM_MAX_COUNT 0xFFFF
  31. struct ep93xx_pwm {
  32. void __iomem *mmio_base;
  33. struct clk *clk;
  34. u32 duty_percent;
  35. };
  36. static inline u16 ep93xx_pwm_read_tc(struct ep93xx_pwm *pwm)
  37. {
  38. return readl(pwm->mmio_base + EP93XX_PWMx_TERM_COUNT);
  39. }
  40. static inline void ep93xx_pwm_enable(struct ep93xx_pwm *pwm)
  41. {
  42. writel(0x1, pwm->mmio_base + EP93XX_PWMx_ENABLE);
  43. }
  44. static inline void ep93xx_pwm_disable(struct ep93xx_pwm *pwm)
  45. {
  46. writel(0x0, pwm->mmio_base + EP93XX_PWMx_ENABLE);
  47. }
  48. static inline int ep93xx_pwm_is_enabled(struct ep93xx_pwm *pwm)
  49. {
  50. return readl(pwm->mmio_base + EP93XX_PWMx_ENABLE) & 0x1;
  51. }
  52. static inline void ep93xx_pwm_invert(struct ep93xx_pwm *pwm)
  53. {
  54. writel(0x1, pwm->mmio_base + EP93XX_PWMx_INVERT);
  55. }
  56. static inline void ep93xx_pwm_normal(struct ep93xx_pwm *pwm)
  57. {
  58. writel(0x0, pwm->mmio_base + EP93XX_PWMx_INVERT);
  59. }
  60. static inline int ep93xx_pwm_is_inverted(struct ep93xx_pwm *pwm)
  61. {
  62. return readl(pwm->mmio_base + EP93XX_PWMx_INVERT) & 0x1;
  63. }
  64. /*
  65. * /sys/devices/platform/ep93xx-pwm.N
  66. * /min_freq read-only minimum pwm output frequency
  67. * /max_req read-only maximum pwm output frequency
  68. * /freq read-write pwm output frequency (0 = disable output)
  69. * /duty_percent read-write pwm duty cycle percent (1..99)
  70. * /invert read-write invert pwm output
  71. */
  72. static ssize_t ep93xx_pwm_get_min_freq(struct device *dev,
  73. struct device_attribute *attr, char *buf)
  74. {
  75. struct platform_device *pdev = to_platform_device(dev);
  76. struct ep93xx_pwm *pwm = platform_get_drvdata(pdev);
  77. unsigned long rate = clk_get_rate(pwm->clk);
  78. return sprintf(buf, "%ld\n", rate / (EP93XX_PWM_MAX_COUNT + 1));
  79. }
  80. static ssize_t ep93xx_pwm_get_max_freq(struct device *dev,
  81. struct device_attribute *attr, char *buf)
  82. {
  83. struct platform_device *pdev = to_platform_device(dev);
  84. struct ep93xx_pwm *pwm = platform_get_drvdata(pdev);
  85. unsigned long rate = clk_get_rate(pwm->clk);
  86. return sprintf(buf, "%ld\n", rate / 2);
  87. }
  88. static ssize_t ep93xx_pwm_get_freq(struct device *dev,
  89. struct device_attribute *attr, char *buf)
  90. {
  91. struct platform_device *pdev = to_platform_device(dev);
  92. struct ep93xx_pwm *pwm = platform_get_drvdata(pdev);
  93. if (ep93xx_pwm_is_enabled(pwm)) {
  94. unsigned long rate = clk_get_rate(pwm->clk);
  95. u16 term = ep93xx_pwm_read_tc(pwm);
  96. return sprintf(buf, "%ld\n", rate / (term + 1));
  97. } else {
  98. return sprintf(buf, "disabled\n");
  99. }
  100. }
  101. static ssize_t ep93xx_pwm_set_freq(struct device *dev,
  102. struct device_attribute *attr, const char *buf, size_t count)
  103. {
  104. struct platform_device *pdev = to_platform_device(dev);
  105. struct ep93xx_pwm *pwm = platform_get_drvdata(pdev);
  106. long val;
  107. int err;
  108. err = strict_strtol(buf, 10, &val);
  109. if (err)
  110. return -EINVAL;
  111. if (val == 0) {
  112. ep93xx_pwm_disable(pwm);
  113. } else if (val <= (clk_get_rate(pwm->clk) / 2)) {
  114. u32 term, duty;
  115. val = (clk_get_rate(pwm->clk) / val) - 1;
  116. if (val > EP93XX_PWM_MAX_COUNT)
  117. val = EP93XX_PWM_MAX_COUNT;
  118. if (val < 1)
  119. val = 1;
  120. term = ep93xx_pwm_read_tc(pwm);
  121. duty = ((val + 1) * pwm->duty_percent / 100) - 1;
  122. /* If pwm is running, order is important */
  123. if (val > term) {
  124. writel(val, pwm->mmio_base + EP93XX_PWMx_TERM_COUNT);
  125. writel(duty, pwm->mmio_base + EP93XX_PWMx_DUTY_CYCLE);
  126. } else {
  127. writel(duty, pwm->mmio_base + EP93XX_PWMx_DUTY_CYCLE);
  128. writel(val, pwm->mmio_base + EP93XX_PWMx_TERM_COUNT);
  129. }
  130. if (!ep93xx_pwm_is_enabled(pwm))
  131. ep93xx_pwm_enable(pwm);
  132. } else {
  133. return -EINVAL;
  134. }
  135. return count;
  136. }
  137. static ssize_t ep93xx_pwm_get_duty_percent(struct device *dev,
  138. struct device_attribute *attr, char *buf)
  139. {
  140. struct platform_device *pdev = to_platform_device(dev);
  141. struct ep93xx_pwm *pwm = platform_get_drvdata(pdev);
  142. return sprintf(buf, "%d\n", pwm->duty_percent);
  143. }
  144. static ssize_t ep93xx_pwm_set_duty_percent(struct device *dev,
  145. struct device_attribute *attr, const char *buf, size_t count)
  146. {
  147. struct platform_device *pdev = to_platform_device(dev);
  148. struct ep93xx_pwm *pwm = platform_get_drvdata(pdev);
  149. long val;
  150. int err;
  151. err = strict_strtol(buf, 10, &val);
  152. if (err)
  153. return -EINVAL;
  154. if (val > 0 && val < 100) {
  155. u32 term = ep93xx_pwm_read_tc(pwm);
  156. u32 duty = ((term + 1) * val / 100) - 1;
  157. writel(duty, pwm->mmio_base + EP93XX_PWMx_DUTY_CYCLE);
  158. pwm->duty_percent = val;
  159. return count;
  160. }
  161. return -EINVAL;
  162. }
  163. static ssize_t ep93xx_pwm_get_invert(struct device *dev,
  164. struct device_attribute *attr, char *buf)
  165. {
  166. struct platform_device *pdev = to_platform_device(dev);
  167. struct ep93xx_pwm *pwm = platform_get_drvdata(pdev);
  168. return sprintf(buf, "%d\n", ep93xx_pwm_is_inverted(pwm));
  169. }
  170. static ssize_t ep93xx_pwm_set_invert(struct device *dev,
  171. struct device_attribute *attr, const char *buf, size_t count)
  172. {
  173. struct platform_device *pdev = to_platform_device(dev);
  174. struct ep93xx_pwm *pwm = platform_get_drvdata(pdev);
  175. long val;
  176. int err;
  177. err = strict_strtol(buf, 10, &val);
  178. if (err)
  179. return -EINVAL;
  180. if (val == 0)
  181. ep93xx_pwm_normal(pwm);
  182. else if (val == 1)
  183. ep93xx_pwm_invert(pwm);
  184. else
  185. return -EINVAL;
  186. return count;
  187. }
  188. static DEVICE_ATTR(min_freq, S_IRUGO, ep93xx_pwm_get_min_freq, NULL);
  189. static DEVICE_ATTR(max_freq, S_IRUGO, ep93xx_pwm_get_max_freq, NULL);
  190. static DEVICE_ATTR(freq, S_IWUSR | S_IRUGO,
  191. ep93xx_pwm_get_freq, ep93xx_pwm_set_freq);
  192. static DEVICE_ATTR(duty_percent, S_IWUSR | S_IRUGO,
  193. ep93xx_pwm_get_duty_percent, ep93xx_pwm_set_duty_percent);
  194. static DEVICE_ATTR(invert, S_IWUSR | S_IRUGO,
  195. ep93xx_pwm_get_invert, ep93xx_pwm_set_invert);
  196. static struct attribute *ep93xx_pwm_attrs[] = {
  197. &dev_attr_min_freq.attr,
  198. &dev_attr_max_freq.attr,
  199. &dev_attr_freq.attr,
  200. &dev_attr_duty_percent.attr,
  201. &dev_attr_invert.attr,
  202. NULL
  203. };
  204. static const struct attribute_group ep93xx_pwm_sysfs_files = {
  205. .attrs = ep93xx_pwm_attrs,
  206. };
  207. static int __init ep93xx_pwm_probe(struct platform_device *pdev)
  208. {
  209. struct ep93xx_pwm *pwm;
  210. struct resource *res;
  211. int ret;
  212. pwm = devm_kzalloc(&pdev->dev, sizeof(*pwm), GFP_KERNEL);
  213. if (!pwm)
  214. return -ENOMEM;
  215. pwm->clk = devm_clk_get(&pdev->dev, "pwm_clk");
  216. if (IS_ERR(pwm->clk))
  217. return PTR_ERR(pwm->clk);
  218. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  219. pwm->mmio_base = devm_ioremap_resource(&pdev->dev, res);
  220. if (IS_ERR(pwm->mmio_base))
  221. return PTR_ERR(pwm->mmio_base);
  222. ret = ep93xx_pwm_acquire_gpio(pdev);
  223. if (ret)
  224. return ret;
  225. ret = sysfs_create_group(&pdev->dev.kobj, &ep93xx_pwm_sysfs_files);
  226. if (ret) {
  227. ep93xx_pwm_release_gpio(pdev);
  228. return ret;
  229. }
  230. pwm->duty_percent = 50;
  231. /* disable pwm at startup. Avoids zero value. */
  232. ep93xx_pwm_disable(pwm);
  233. writel(EP93XX_PWM_MAX_COUNT, pwm->mmio_base + EP93XX_PWMx_TERM_COUNT);
  234. writel(EP93XX_PWM_MAX_COUNT/2, pwm->mmio_base + EP93XX_PWMx_DUTY_CYCLE);
  235. clk_enable(pwm->clk);
  236. platform_set_drvdata(pdev, pwm);
  237. return 0;
  238. }
  239. static int __exit ep93xx_pwm_remove(struct platform_device *pdev)
  240. {
  241. struct ep93xx_pwm *pwm = platform_get_drvdata(pdev);
  242. ep93xx_pwm_disable(pwm);
  243. clk_disable(pwm->clk);
  244. sysfs_remove_group(&pdev->dev.kobj, &ep93xx_pwm_sysfs_files);
  245. ep93xx_pwm_release_gpio(pdev);
  246. return 0;
  247. }
  248. static struct platform_driver ep93xx_pwm_driver = {
  249. .driver = {
  250. .name = "ep93xx-pwm",
  251. .owner = THIS_MODULE,
  252. },
  253. .remove = __exit_p(ep93xx_pwm_remove),
  254. };
  255. module_platform_driver_probe(ep93xx_pwm_driver, ep93xx_pwm_probe);
  256. MODULE_AUTHOR("Matthieu Crapet <mcrapet@gmail.com>, "
  257. "H Hartley Sweeten <hsweeten@visionengravers.com>");
  258. MODULE_DESCRIPTION("EP93xx PWM driver");
  259. MODULE_LICENSE("GPL");
  260. MODULE_ALIAS("platform:ep93xx-pwm");