ep93xx_pwm.c 9.5 KB

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