pwm.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. /*
  2. * linux/arch/unicore32/kernel/pwm.c
  3. *
  4. * Code specific to PKUnity SoC and UniCore ISA
  5. *
  6. * Maintained by GUAN Xue-tao <gxt@mprc.pku.edu.cn>
  7. * Copyright (C) 2001-2010 Guan Xuetao
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2 as
  11. * published by the Free Software Foundation.
  12. */
  13. #include <linux/module.h>
  14. #include <linux/kernel.h>
  15. #include <linux/platform_device.h>
  16. #include <linux/slab.h>
  17. #include <linux/err.h>
  18. #include <linux/clk.h>
  19. #include <linux/io.h>
  20. #include <linux/pwm.h>
  21. #include <asm/div64.h>
  22. #include <mach/hardware.h>
  23. struct pwm_device {
  24. struct list_head node;
  25. struct platform_device *pdev;
  26. const char *label;
  27. struct clk *clk;
  28. int clk_enabled;
  29. unsigned int use_count;
  30. unsigned int pwm_id;
  31. };
  32. /*
  33. * period_ns = 10^9 * (PRESCALE + 1) * (PV + 1) / PWM_CLK_RATE
  34. * duty_ns = 10^9 * (PRESCALE + 1) * DC / PWM_CLK_RATE
  35. */
  36. int pwm_config(struct pwm_device *pwm, int duty_ns, int period_ns)
  37. {
  38. unsigned long long c;
  39. unsigned long period_cycles, prescale, pv, dc;
  40. if (pwm == NULL || period_ns == 0 || duty_ns > period_ns)
  41. return -EINVAL;
  42. c = clk_get_rate(pwm->clk);
  43. c = c * period_ns;
  44. do_div(c, 1000000000);
  45. period_cycles = c;
  46. if (period_cycles < 1)
  47. period_cycles = 1;
  48. prescale = (period_cycles - 1) / 1024;
  49. pv = period_cycles / (prescale + 1) - 1;
  50. if (prescale > 63)
  51. return -EINVAL;
  52. if (duty_ns == period_ns)
  53. dc = OST_PWMDCCR_FDCYCLE;
  54. else
  55. dc = (pv + 1) * duty_ns / period_ns;
  56. /* NOTE: the clock to PWM has to be enabled first
  57. * before writing to the registers
  58. */
  59. clk_enable(pwm->clk);
  60. OST_PWMPWCR = prescale;
  61. OST_PWMDCCR = pv - dc;
  62. OST_PWMPCR = pv;
  63. clk_disable(pwm->clk);
  64. return 0;
  65. }
  66. EXPORT_SYMBOL(pwm_config);
  67. int pwm_enable(struct pwm_device *pwm)
  68. {
  69. int rc = 0;
  70. if (!pwm->clk_enabled) {
  71. rc = clk_enable(pwm->clk);
  72. if (!rc)
  73. pwm->clk_enabled = 1;
  74. }
  75. return rc;
  76. }
  77. EXPORT_SYMBOL(pwm_enable);
  78. void pwm_disable(struct pwm_device *pwm)
  79. {
  80. if (pwm->clk_enabled) {
  81. clk_disable(pwm->clk);
  82. pwm->clk_enabled = 0;
  83. }
  84. }
  85. EXPORT_SYMBOL(pwm_disable);
  86. static DEFINE_MUTEX(pwm_lock);
  87. static LIST_HEAD(pwm_list);
  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. } else
  106. pwm = ERR_PTR(-ENOENT);
  107. mutex_unlock(&pwm_lock);
  108. return pwm;
  109. }
  110. EXPORT_SYMBOL(pwm_request);
  111. void pwm_free(struct pwm_device *pwm)
  112. {
  113. mutex_lock(&pwm_lock);
  114. if (pwm->use_count) {
  115. pwm->use_count--;
  116. pwm->label = NULL;
  117. } else
  118. pr_warning("PWM device already freed\n");
  119. mutex_unlock(&pwm_lock);
  120. }
  121. EXPORT_SYMBOL(pwm_free);
  122. static inline void __add_pwm(struct pwm_device *pwm)
  123. {
  124. mutex_lock(&pwm_lock);
  125. list_add_tail(&pwm->node, &pwm_list);
  126. mutex_unlock(&pwm_lock);
  127. }
  128. static struct pwm_device *pwm_probe(struct platform_device *pdev,
  129. unsigned int pwm_id, struct pwm_device *parent_pwm)
  130. {
  131. struct pwm_device *pwm;
  132. struct resource *r;
  133. int ret = 0;
  134. pwm = kzalloc(sizeof(struct pwm_device), GFP_KERNEL);
  135. if (pwm == NULL) {
  136. dev_err(&pdev->dev, "failed to allocate memory\n");
  137. return ERR_PTR(-ENOMEM);
  138. }
  139. pwm->clk = clk_get(NULL, "OST_CLK");
  140. if (IS_ERR(pwm->clk)) {
  141. ret = PTR_ERR(pwm->clk);
  142. goto err_free;
  143. }
  144. pwm->clk_enabled = 0;
  145. pwm->use_count = 0;
  146. pwm->pwm_id = pwm_id;
  147. pwm->pdev = pdev;
  148. r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  149. if (r == NULL) {
  150. dev_err(&pdev->dev, "no memory resource defined\n");
  151. ret = -ENODEV;
  152. goto err_free_clk;
  153. }
  154. r = request_mem_region(r->start, resource_size(r), pdev->name);
  155. if (r == NULL) {
  156. dev_err(&pdev->dev, "failed to request memory resource\n");
  157. ret = -EBUSY;
  158. goto err_free_clk;
  159. }
  160. __add_pwm(pwm);
  161. platform_set_drvdata(pdev, pwm);
  162. return pwm;
  163. err_free_clk:
  164. clk_put(pwm->clk);
  165. err_free:
  166. kfree(pwm);
  167. return ERR_PTR(ret);
  168. }
  169. static int __devinit puv3_pwm_probe(struct platform_device *pdev)
  170. {
  171. struct pwm_device *pwm = pwm_probe(pdev, pdev->id, NULL);
  172. if (IS_ERR(pwm))
  173. return PTR_ERR(pwm);
  174. return 0;
  175. }
  176. static int __devexit pwm_remove(struct platform_device *pdev)
  177. {
  178. struct pwm_device *pwm;
  179. struct resource *r;
  180. pwm = platform_get_drvdata(pdev);
  181. if (pwm == NULL)
  182. return -ENODEV;
  183. mutex_lock(&pwm_lock);
  184. list_del(&pwm->node);
  185. mutex_unlock(&pwm_lock);
  186. r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  187. release_mem_region(r->start, resource_size(r));
  188. clk_put(pwm->clk);
  189. kfree(pwm);
  190. return 0;
  191. }
  192. static struct platform_driver puv3_pwm_driver = {
  193. .driver = {
  194. .name = "PKUnity-v3-PWM",
  195. },
  196. .probe = puv3_pwm_probe,
  197. .remove = __devexit_p(pwm_remove),
  198. };
  199. static int __init pwm_init(void)
  200. {
  201. int ret = 0;
  202. ret = platform_driver_register(&puv3_pwm_driver);
  203. if (ret) {
  204. printk(KERN_ERR "failed to register puv3_pwm_driver\n");
  205. return ret;
  206. }
  207. return ret;
  208. }
  209. arch_initcall(pwm_init);
  210. static void __exit pwm_exit(void)
  211. {
  212. platform_driver_unregister(&puv3_pwm_driver);
  213. }
  214. module_exit(pwm_exit);
  215. MODULE_LICENSE("GPL v2");