ep93xx_pwm.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  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/clk.h>
  22. #include <linux/err.h>
  23. #include <linux/io.h>
  24. #include <mach/platform.h>
  25. #define EP93XX_PWMx_TERM_COUNT 0x00
  26. #define EP93XX_PWMx_DUTY_CYCLE 0x04
  27. #define EP93XX_PWMx_ENABLE 0x08
  28. #define EP93XX_PWMx_INVERT 0x0C
  29. #define EP93XX_PWM_MAX_COUNT 0xFFFF
  30. struct ep93xx_pwm {
  31. void __iomem *mmio_base;
  32. struct clk *clk;
  33. u32 duty_percent;
  34. };
  35. static inline void ep93xx_pwm_writel(struct ep93xx_pwm *pwm,
  36. unsigned int val, unsigned int off)
  37. {
  38. __raw_writel(val, pwm->mmio_base + off);
  39. }
  40. static inline unsigned int ep93xx_pwm_readl(struct ep93xx_pwm *pwm,
  41. unsigned int off)
  42. {
  43. return __raw_readl(pwm->mmio_base + off);
  44. }
  45. static inline void ep93xx_pwm_write_tc(struct ep93xx_pwm *pwm, u16 value)
  46. {
  47. ep93xx_pwm_writel(pwm, value, EP93XX_PWMx_TERM_COUNT);
  48. }
  49. static inline u16 ep93xx_pwm_read_tc(struct ep93xx_pwm *pwm)
  50. {
  51. return ep93xx_pwm_readl(pwm, EP93XX_PWMx_TERM_COUNT);
  52. }
  53. static inline void ep93xx_pwm_write_dc(struct ep93xx_pwm *pwm, u16 value)
  54. {
  55. ep93xx_pwm_writel(pwm, value, EP93XX_PWMx_DUTY_CYCLE);
  56. }
  57. static inline void ep93xx_pwm_enable(struct ep93xx_pwm *pwm)
  58. {
  59. ep93xx_pwm_writel(pwm, 0x1, EP93XX_PWMx_ENABLE);
  60. }
  61. static inline void ep93xx_pwm_disable(struct ep93xx_pwm *pwm)
  62. {
  63. ep93xx_pwm_writel(pwm, 0x0, EP93XX_PWMx_ENABLE);
  64. }
  65. static inline int ep93xx_pwm_is_enabled(struct ep93xx_pwm *pwm)
  66. {
  67. return ep93xx_pwm_readl(pwm, EP93XX_PWMx_ENABLE) & 0x1;
  68. }
  69. static inline void ep93xx_pwm_invert(struct ep93xx_pwm *pwm)
  70. {
  71. ep93xx_pwm_writel(pwm, 0x1, EP93XX_PWMx_INVERT);
  72. }
  73. static inline void ep93xx_pwm_normal(struct ep93xx_pwm *pwm)
  74. {
  75. ep93xx_pwm_writel(pwm, 0x0, EP93XX_PWMx_INVERT);
  76. }
  77. static inline int ep93xx_pwm_is_inverted(struct ep93xx_pwm *pwm)
  78. {
  79. return ep93xx_pwm_readl(pwm, EP93XX_PWMx_INVERT) & 0x1;
  80. }
  81. /*
  82. * /sys/devices/platform/ep93xx-pwm.N
  83. * /min_freq read-only minimum pwm output frequency
  84. * /max_req read-only maximum pwm output frequency
  85. * /freq read-write pwm output frequency (0 = disable output)
  86. * /duty_percent read-write pwm duty cycle percent (1..99)
  87. * /invert read-write invert pwm output
  88. */
  89. static ssize_t ep93xx_pwm_get_min_freq(struct device *dev,
  90. struct device_attribute *attr, char *buf)
  91. {
  92. struct platform_device *pdev = to_platform_device(dev);
  93. struct ep93xx_pwm *pwm = platform_get_drvdata(pdev);
  94. unsigned long rate = clk_get_rate(pwm->clk);
  95. return sprintf(buf, "%ld\n", rate / (EP93XX_PWM_MAX_COUNT + 1));
  96. }
  97. static ssize_t ep93xx_pwm_get_max_freq(struct device *dev,
  98. struct device_attribute *attr, char *buf)
  99. {
  100. struct platform_device *pdev = to_platform_device(dev);
  101. struct ep93xx_pwm *pwm = platform_get_drvdata(pdev);
  102. unsigned long rate = clk_get_rate(pwm->clk);
  103. return sprintf(buf, "%ld\n", rate / 2);
  104. }
  105. static ssize_t ep93xx_pwm_get_freq(struct device *dev,
  106. struct device_attribute *attr, char *buf)
  107. {
  108. struct platform_device *pdev = to_platform_device(dev);
  109. struct ep93xx_pwm *pwm = platform_get_drvdata(pdev);
  110. if (ep93xx_pwm_is_enabled(pwm)) {
  111. unsigned long rate = clk_get_rate(pwm->clk);
  112. u16 term = ep93xx_pwm_read_tc(pwm);
  113. return sprintf(buf, "%ld\n", rate / (term + 1));
  114. } else {
  115. return sprintf(buf, "disabled\n");
  116. }
  117. }
  118. static ssize_t ep93xx_pwm_set_freq(struct device *dev,
  119. struct device_attribute *attr, const char *buf, size_t count)
  120. {
  121. struct platform_device *pdev = to_platform_device(dev);
  122. struct ep93xx_pwm *pwm = platform_get_drvdata(pdev);
  123. long val;
  124. int err;
  125. err = strict_strtol(buf, 10, &val);
  126. if (err)
  127. return -EINVAL;
  128. if (val == 0) {
  129. ep93xx_pwm_disable(pwm);
  130. } else if (val <= (clk_get_rate(pwm->clk) / 2)) {
  131. u32 term, duty;
  132. val = (clk_get_rate(pwm->clk) / val) - 1;
  133. if (val > EP93XX_PWM_MAX_COUNT)
  134. val = EP93XX_PWM_MAX_COUNT;
  135. if (val < 1)
  136. val = 1;
  137. term = ep93xx_pwm_read_tc(pwm);
  138. duty = ((val + 1) * pwm->duty_percent / 100) - 1;
  139. /* If pwm is running, order is important */
  140. if (val > term) {
  141. ep93xx_pwm_write_tc(pwm, val);
  142. ep93xx_pwm_write_dc(pwm, duty);
  143. } else {
  144. ep93xx_pwm_write_dc(pwm, duty);
  145. ep93xx_pwm_write_tc(pwm, val);
  146. }
  147. if (!ep93xx_pwm_is_enabled(pwm))
  148. ep93xx_pwm_enable(pwm);
  149. } else {
  150. return -EINVAL;
  151. }
  152. return count;
  153. }
  154. static ssize_t ep93xx_pwm_get_duty_percent(struct device *dev,
  155. struct device_attribute *attr, char *buf)
  156. {
  157. struct platform_device *pdev = to_platform_device(dev);
  158. struct ep93xx_pwm *pwm = platform_get_drvdata(pdev);
  159. return sprintf(buf, "%d\n", pwm->duty_percent);
  160. }
  161. static ssize_t ep93xx_pwm_set_duty_percent(struct device *dev,
  162. struct device_attribute *attr, const char *buf, size_t count)
  163. {
  164. struct platform_device *pdev = to_platform_device(dev);
  165. struct ep93xx_pwm *pwm = platform_get_drvdata(pdev);
  166. long val;
  167. int err;
  168. err = strict_strtol(buf, 10, &val);
  169. if (err)
  170. return -EINVAL;
  171. if (val > 0 && val < 100) {
  172. u32 term = ep93xx_pwm_read_tc(pwm);
  173. ep93xx_pwm_write_dc(pwm, ((term + 1) * val / 100) - 1);
  174. pwm->duty_percent = val;
  175. return count;
  176. }
  177. return -EINVAL;
  178. }
  179. static ssize_t ep93xx_pwm_get_invert(struct device *dev,
  180. struct device_attribute *attr, char *buf)
  181. {
  182. struct platform_device *pdev = to_platform_device(dev);
  183. struct ep93xx_pwm *pwm = platform_get_drvdata(pdev);
  184. return sprintf(buf, "%d\n", ep93xx_pwm_is_inverted(pwm));
  185. }
  186. static ssize_t ep93xx_pwm_set_invert(struct device *dev,
  187. struct device_attribute *attr, const char *buf, size_t count)
  188. {
  189. struct platform_device *pdev = to_platform_device(dev);
  190. struct ep93xx_pwm *pwm = platform_get_drvdata(pdev);
  191. long val;
  192. int err;
  193. err = strict_strtol(buf, 10, &val);
  194. if (err)
  195. return -EINVAL;
  196. if (val == 0)
  197. ep93xx_pwm_normal(pwm);
  198. else if (val == 1)
  199. ep93xx_pwm_invert(pwm);
  200. else
  201. return -EINVAL;
  202. return count;
  203. }
  204. static DEVICE_ATTR(min_freq, S_IRUGO, ep93xx_pwm_get_min_freq, NULL);
  205. static DEVICE_ATTR(max_freq, S_IRUGO, ep93xx_pwm_get_max_freq, NULL);
  206. static DEVICE_ATTR(freq, S_IWUGO | S_IRUGO,
  207. ep93xx_pwm_get_freq, ep93xx_pwm_set_freq);
  208. static DEVICE_ATTR(duty_percent, S_IWUGO | S_IRUGO,
  209. ep93xx_pwm_get_duty_percent, ep93xx_pwm_set_duty_percent);
  210. static DEVICE_ATTR(invert, S_IWUGO | S_IRUGO,
  211. ep93xx_pwm_get_invert, ep93xx_pwm_set_invert);
  212. static struct attribute *ep93xx_pwm_attrs[] = {
  213. &dev_attr_min_freq.attr,
  214. &dev_attr_max_freq.attr,
  215. &dev_attr_freq.attr,
  216. &dev_attr_duty_percent.attr,
  217. &dev_attr_invert.attr,
  218. NULL
  219. };
  220. static const struct attribute_group ep93xx_pwm_sysfs_files = {
  221. .attrs = ep93xx_pwm_attrs,
  222. };
  223. static int __init ep93xx_pwm_probe(struct platform_device *pdev)
  224. {
  225. struct ep93xx_pwm *pwm;
  226. struct resource *res;
  227. int err;
  228. err = ep93xx_pwm_acquire_gpio(pdev);
  229. if (err)
  230. return err;
  231. pwm = kzalloc(sizeof(struct ep93xx_pwm), GFP_KERNEL);
  232. if (!pwm) {
  233. err = -ENOMEM;
  234. goto fail_no_mem;
  235. }
  236. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  237. if (res == NULL) {
  238. err = -ENXIO;
  239. goto fail_no_mem_resource;
  240. }
  241. res = request_mem_region(res->start, resource_size(res), pdev->name);
  242. if (res == NULL) {
  243. err = -EBUSY;
  244. goto fail_no_mem_resource;
  245. }
  246. pwm->mmio_base = ioremap(res->start, resource_size(res));
  247. if (pwm->mmio_base == NULL) {
  248. err = -ENXIO;
  249. goto fail_no_ioremap;
  250. }
  251. err = sysfs_create_group(&pdev->dev.kobj, &ep93xx_pwm_sysfs_files);
  252. if (err)
  253. goto fail_no_sysfs;
  254. pwm->clk = clk_get(&pdev->dev, "pwm_clk");
  255. if (IS_ERR(pwm->clk)) {
  256. err = PTR_ERR(pwm->clk);
  257. goto fail_no_clk;
  258. }
  259. pwm->duty_percent = 50;
  260. platform_set_drvdata(pdev, pwm);
  261. /* disable pwm at startup. Avoids zero value. */
  262. ep93xx_pwm_disable(pwm);
  263. ep93xx_pwm_write_tc(pwm, EP93XX_PWM_MAX_COUNT);
  264. ep93xx_pwm_write_dc(pwm, EP93XX_PWM_MAX_COUNT / 2);
  265. clk_enable(pwm->clk);
  266. return 0;
  267. fail_no_clk:
  268. sysfs_remove_group(&pdev->dev.kobj, &ep93xx_pwm_sysfs_files);
  269. fail_no_sysfs:
  270. iounmap(pwm->mmio_base);
  271. fail_no_ioremap:
  272. release_mem_region(res->start, resource_size(res));
  273. fail_no_mem_resource:
  274. kfree(pwm);
  275. fail_no_mem:
  276. ep93xx_pwm_release_gpio(pdev);
  277. return err;
  278. }
  279. static int __exit ep93xx_pwm_remove(struct platform_device *pdev)
  280. {
  281. struct ep93xx_pwm *pwm = platform_get_drvdata(pdev);
  282. struct resource *res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  283. ep93xx_pwm_disable(pwm);
  284. clk_disable(pwm->clk);
  285. clk_put(pwm->clk);
  286. platform_set_drvdata(pdev, NULL);
  287. sysfs_remove_group(&pdev->dev.kobj, &ep93xx_pwm_sysfs_files);
  288. iounmap(pwm->mmio_base);
  289. release_mem_region(res->start, resource_size(res));
  290. kfree(pwm);
  291. ep93xx_pwm_release_gpio(pdev);
  292. return 0;
  293. }
  294. static struct platform_driver ep93xx_pwm_driver = {
  295. .driver = {
  296. .name = "ep93xx-pwm",
  297. .owner = THIS_MODULE,
  298. },
  299. .remove = __exit_p(ep93xx_pwm_remove),
  300. };
  301. static int __init ep93xx_pwm_init(void)
  302. {
  303. return platform_driver_probe(&ep93xx_pwm_driver, ep93xx_pwm_probe);
  304. }
  305. static void __exit ep93xx_pwm_exit(void)
  306. {
  307. platform_driver_unregister(&ep93xx_pwm_driver);
  308. }
  309. module_init(ep93xx_pwm_init);
  310. module_exit(ep93xx_pwm_exit);
  311. MODULE_AUTHOR("Matthieu Crapet <mcrapet@gmail.com>, "
  312. "H Hartley Sweeten <hsweeten@visionengravers.com>");
  313. MODULE_DESCRIPTION("EP93xx PWM driver");
  314. MODULE_LICENSE("GPL");
  315. MODULE_ALIAS("platform:ep93xx-pwm");