pwm.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. /*
  2. * arch/arm/mach-vt8500/pwm.c
  3. *
  4. * Copyright (C) 2010 Alexey Charkov <alchark@gmail.com>
  5. *
  6. * This software is licensed under the terms of the GNU General Public
  7. * License version 2, as published by the Free Software Foundation, and
  8. * may be copied, distributed, and modified under those terms.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. */
  15. #include <linux/module.h>
  16. #include <linux/kernel.h>
  17. #include <linux/platform_device.h>
  18. #include <linux/slab.h>
  19. #include <linux/err.h>
  20. #include <linux/io.h>
  21. #include <linux/pwm.h>
  22. #include <linux/delay.h>
  23. #include <asm/div64.h>
  24. #define VT8500_NR_PWMS 4
  25. static DEFINE_MUTEX(pwm_lock);
  26. static LIST_HEAD(pwm_list);
  27. struct pwm_device {
  28. struct list_head node;
  29. struct platform_device *pdev;
  30. const char *label;
  31. void __iomem *regbase;
  32. unsigned int use_count;
  33. unsigned int pwm_id;
  34. };
  35. #define msecs_to_loops(t) (loops_per_jiffy / 1000 * HZ * t)
  36. static inline void pwm_busy_wait(void __iomem *reg, u8 bitmask)
  37. {
  38. int loops = msecs_to_loops(10);
  39. while ((readb(reg) & bitmask) && --loops)
  40. cpu_relax();
  41. if (unlikely(!loops))
  42. pr_warning("Waiting for status bits 0x%x to clear timed out\n",
  43. bitmask);
  44. }
  45. int pwm_config(struct pwm_device *pwm, int duty_ns, int period_ns)
  46. {
  47. unsigned long long c;
  48. unsigned long period_cycles, prescale, pv, dc;
  49. if (pwm == NULL || period_ns == 0 || duty_ns > period_ns)
  50. return -EINVAL;
  51. c = 25000000/2; /* wild guess --- need to implement clocks */
  52. c = c * period_ns;
  53. do_div(c, 1000000000);
  54. period_cycles = c;
  55. if (period_cycles < 1)
  56. period_cycles = 1;
  57. prescale = (period_cycles - 1) / 4096;
  58. pv = period_cycles / (prescale + 1) - 1;
  59. if (pv > 4095)
  60. pv = 4095;
  61. if (prescale > 1023)
  62. return -EINVAL;
  63. c = (unsigned long long)pv * duty_ns;
  64. do_div(c, period_ns);
  65. dc = c;
  66. pwm_busy_wait(pwm->regbase + 0x40 + pwm->pwm_id, (1 << 1));
  67. writel(prescale, pwm->regbase + 0x4 + (pwm->pwm_id << 4));
  68. pwm_busy_wait(pwm->regbase + 0x40 + pwm->pwm_id, (1 << 2));
  69. writel(pv, pwm->regbase + 0x8 + (pwm->pwm_id << 4));
  70. pwm_busy_wait(pwm->regbase + 0x40 + pwm->pwm_id, (1 << 3));
  71. writel(dc, pwm->regbase + 0xc + (pwm->pwm_id << 4));
  72. return 0;
  73. }
  74. EXPORT_SYMBOL(pwm_config);
  75. int pwm_enable(struct pwm_device *pwm)
  76. {
  77. pwm_busy_wait(pwm->regbase + 0x40 + pwm->pwm_id, (1 << 0));
  78. writel(5, pwm->regbase + (pwm->pwm_id << 4));
  79. return 0;
  80. }
  81. EXPORT_SYMBOL(pwm_enable);
  82. void pwm_disable(struct pwm_device *pwm)
  83. {
  84. pwm_busy_wait(pwm->regbase + 0x40 + pwm->pwm_id, (1 << 0));
  85. writel(0, pwm->regbase + (pwm->pwm_id << 4));
  86. }
  87. EXPORT_SYMBOL(pwm_disable);
  88. struct pwm_device *pwm_request(int pwm_id, const char *label)
  89. {
  90. struct pwm_device *pwm;
  91. int found = 0;
  92. mutex_lock(&pwm_lock);
  93. list_for_each_entry(pwm, &pwm_list, node) {
  94. if (pwm->pwm_id == pwm_id) {
  95. found = 1;
  96. break;
  97. }
  98. }
  99. if (found) {
  100. if (pwm->use_count == 0) {
  101. pwm->use_count++;
  102. pwm->label = label;
  103. } else {
  104. pwm = ERR_PTR(-EBUSY);
  105. }
  106. } else {
  107. pwm = ERR_PTR(-ENOENT);
  108. }
  109. mutex_unlock(&pwm_lock);
  110. return pwm;
  111. }
  112. EXPORT_SYMBOL(pwm_request);
  113. void pwm_free(struct pwm_device *pwm)
  114. {
  115. mutex_lock(&pwm_lock);
  116. if (pwm->use_count) {
  117. pwm->use_count--;
  118. pwm->label = NULL;
  119. } else {
  120. pr_warning("PWM device already freed\n");
  121. }
  122. mutex_unlock(&pwm_lock);
  123. }
  124. EXPORT_SYMBOL(pwm_free);
  125. static inline void __add_pwm(struct pwm_device *pwm)
  126. {
  127. mutex_lock(&pwm_lock);
  128. list_add_tail(&pwm->node, &pwm_list);
  129. mutex_unlock(&pwm_lock);
  130. }
  131. static int __devinit pwm_probe(struct platform_device *pdev)
  132. {
  133. struct pwm_device *pwms;
  134. struct resource *r;
  135. int ret = 0;
  136. int i;
  137. pwms = kzalloc(sizeof(struct pwm_device) * VT8500_NR_PWMS, GFP_KERNEL);
  138. if (pwms == NULL) {
  139. dev_err(&pdev->dev, "failed to allocate memory\n");
  140. return -ENOMEM;
  141. }
  142. for (i = 0; i < VT8500_NR_PWMS; i++) {
  143. pwms[i].use_count = 0;
  144. pwms[i].pwm_id = i;
  145. pwms[i].pdev = pdev;
  146. }
  147. r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  148. if (r == NULL) {
  149. dev_err(&pdev->dev, "no memory resource defined\n");
  150. ret = -ENODEV;
  151. goto err_free;
  152. }
  153. r = request_mem_region(r->start, resource_size(r), pdev->name);
  154. if (r == NULL) {
  155. dev_err(&pdev->dev, "failed to request memory resource\n");
  156. ret = -EBUSY;
  157. goto err_free;
  158. }
  159. pwms[0].regbase = ioremap(r->start, resource_size(r));
  160. if (pwms[0].regbase == NULL) {
  161. dev_err(&pdev->dev, "failed to ioremap() registers\n");
  162. ret = -ENODEV;
  163. goto err_free_mem;
  164. }
  165. for (i = 1; i < VT8500_NR_PWMS; i++)
  166. pwms[i].regbase = pwms[0].regbase;
  167. for (i = 0; i < VT8500_NR_PWMS; i++)
  168. __add_pwm(&pwms[i]);
  169. platform_set_drvdata(pdev, pwms);
  170. return 0;
  171. err_free_mem:
  172. release_mem_region(r->start, resource_size(r));
  173. err_free:
  174. kfree(pwms);
  175. return ret;
  176. }
  177. static int __devexit pwm_remove(struct platform_device *pdev)
  178. {
  179. struct pwm_device *pwms;
  180. struct resource *r;
  181. int i;
  182. pwms = platform_get_drvdata(pdev);
  183. if (pwms == NULL)
  184. return -ENODEV;
  185. mutex_lock(&pwm_lock);
  186. for (i = 0; i < VT8500_NR_PWMS; i++)
  187. list_del(&pwms[i].node);
  188. mutex_unlock(&pwm_lock);
  189. iounmap(pwms[0].regbase);
  190. r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  191. release_mem_region(r->start, resource_size(r));
  192. kfree(pwms);
  193. return 0;
  194. }
  195. static struct platform_driver pwm_driver = {
  196. .driver = {
  197. .name = "vt8500-pwm",
  198. .owner = THIS_MODULE,
  199. },
  200. .probe = pwm_probe,
  201. .remove = __devexit_p(pwm_remove),
  202. };
  203. static int __init pwm_init(void)
  204. {
  205. return platform_driver_register(&pwm_driver);
  206. }
  207. arch_initcall(pwm_init);
  208. static void __exit pwm_exit(void)
  209. {
  210. platform_driver_unregister(&pwm_driver);
  211. }
  212. module_exit(pwm_exit);
  213. MODULE_LICENSE("GPL");