pwm.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. /*
  2. * simple driver for PWM (Pulse Width Modulator) controller
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2 as
  6. * published by the Free Software Foundation.
  7. *
  8. * Derived from pxa PWM driver by eric miao <eric.miao@marvell.com>
  9. */
  10. #include <linux/module.h>
  11. #include <linux/kernel.h>
  12. #include <linux/platform_device.h>
  13. #include <linux/err.h>
  14. #include <linux/clk.h>
  15. #include <linux/io.h>
  16. #include <linux/pwm.h>
  17. #include <mach/hardware.h>
  18. /* i.MX1 and i.MX21 share the same PWM function block: */
  19. #define MX1_PWMC 0x00 /* PWM Control Register */
  20. #define MX1_PWMS 0x04 /* PWM Sample Register */
  21. #define MX1_PWMP 0x08 /* PWM Period Register */
  22. /* i.MX27, i.MX31, i.MX35 share the same PWM function block: */
  23. #define MX3_PWMCR 0x00 /* PWM Control Register */
  24. #define MX3_PWMSAR 0x0C /* PWM Sample Register */
  25. #define MX3_PWMPR 0x10 /* PWM Period Register */
  26. #define MX3_PWMCR_PRESCALER(x) (((x - 1) & 0xFFF) << 4)
  27. #define MX3_PWMCR_CLKSRC_IPG_HIGH (2 << 16)
  28. #define MX3_PWMCR_CLKSRC_IPG (1 << 16)
  29. #define MX3_PWMCR_EN (1 << 0)
  30. struct pwm_device {
  31. struct list_head node;
  32. struct platform_device *pdev;
  33. const char *label;
  34. struct clk *clk;
  35. int clk_enabled;
  36. void __iomem *mmio_base;
  37. unsigned int use_count;
  38. unsigned int pwm_id;
  39. };
  40. int pwm_config(struct pwm_device *pwm, int duty_ns, int period_ns)
  41. {
  42. if (pwm == NULL || period_ns == 0 || duty_ns > period_ns)
  43. return -EINVAL;
  44. if (cpu_is_mx27() || cpu_is_mx3() || cpu_is_mx25()) {
  45. unsigned long long c;
  46. unsigned long period_cycles, duty_cycles, prescale;
  47. u32 cr;
  48. c = clk_get_rate(pwm->clk);
  49. c = c * period_ns;
  50. do_div(c, 1000000000);
  51. period_cycles = c;
  52. prescale = period_cycles / 0x10000 + 1;
  53. period_cycles /= prescale;
  54. c = (unsigned long long)period_cycles * duty_ns;
  55. do_div(c, period_ns);
  56. duty_cycles = c;
  57. writel(duty_cycles, pwm->mmio_base + MX3_PWMSAR);
  58. writel(period_cycles, pwm->mmio_base + MX3_PWMPR);
  59. cr = MX3_PWMCR_PRESCALER(prescale) | MX3_PWMCR_EN;
  60. if (cpu_is_mx25())
  61. cr |= MX3_PWMCR_CLKSRC_IPG;
  62. else
  63. cr |= MX3_PWMCR_CLKSRC_IPG_HIGH;
  64. writel(cr, pwm->mmio_base + MX3_PWMCR);
  65. } else if (cpu_is_mx1() || cpu_is_mx21()) {
  66. /* The PWM subsystem allows for exact frequencies. However,
  67. * I cannot connect a scope on my device to the PWM line and
  68. * thus cannot provide the program the PWM controller
  69. * exactly. Instead, I'm relying on the fact that the
  70. * Bootloader (u-boot or WinCE+haret) has programmed the PWM
  71. * function group already. So I'll just modify the PWM sample
  72. * register to follow the ratio of duty_ns vs. period_ns
  73. * accordingly.
  74. *
  75. * This is good enought for programming the brightness of
  76. * the LCD backlight.
  77. *
  78. * The real implementation would divide PERCLK[0] first by
  79. * both the prescaler (/1 .. /128) and then by CLKSEL
  80. * (/2 .. /16).
  81. */
  82. u32 max = readl(pwm->mmio_base + MX1_PWMP);
  83. u32 p = max * duty_ns / period_ns;
  84. writel(max - p, pwm->mmio_base + MX1_PWMS);
  85. } else {
  86. BUG();
  87. }
  88. return 0;
  89. }
  90. EXPORT_SYMBOL(pwm_config);
  91. int pwm_enable(struct pwm_device *pwm)
  92. {
  93. int rc = 0;
  94. if (!pwm->clk_enabled) {
  95. rc = clk_enable(pwm->clk);
  96. if (!rc)
  97. pwm->clk_enabled = 1;
  98. }
  99. return rc;
  100. }
  101. EXPORT_SYMBOL(pwm_enable);
  102. void pwm_disable(struct pwm_device *pwm)
  103. {
  104. writel(0, pwm->mmio_base + MX3_PWMCR);
  105. if (pwm->clk_enabled) {
  106. clk_disable(pwm->clk);
  107. pwm->clk_enabled = 0;
  108. }
  109. }
  110. EXPORT_SYMBOL(pwm_disable);
  111. static DEFINE_MUTEX(pwm_lock);
  112. static LIST_HEAD(pwm_list);
  113. struct pwm_device *pwm_request(int pwm_id, const char *label)
  114. {
  115. struct pwm_device *pwm;
  116. int found = 0;
  117. mutex_lock(&pwm_lock);
  118. list_for_each_entry(pwm, &pwm_list, node) {
  119. if (pwm->pwm_id == pwm_id) {
  120. found = 1;
  121. break;
  122. }
  123. }
  124. if (found) {
  125. if (pwm->use_count == 0) {
  126. pwm->use_count++;
  127. pwm->label = label;
  128. } else
  129. pwm = ERR_PTR(-EBUSY);
  130. } else
  131. pwm = ERR_PTR(-ENOENT);
  132. mutex_unlock(&pwm_lock);
  133. return pwm;
  134. }
  135. EXPORT_SYMBOL(pwm_request);
  136. void pwm_free(struct pwm_device *pwm)
  137. {
  138. mutex_lock(&pwm_lock);
  139. if (pwm->use_count) {
  140. pwm->use_count--;
  141. pwm->label = NULL;
  142. } else
  143. pr_warning("PWM device already freed\n");
  144. mutex_unlock(&pwm_lock);
  145. }
  146. EXPORT_SYMBOL(pwm_free);
  147. static int __devinit mxc_pwm_probe(struct platform_device *pdev)
  148. {
  149. struct pwm_device *pwm;
  150. struct resource *r;
  151. int ret = 0;
  152. pwm = kzalloc(sizeof(struct pwm_device), GFP_KERNEL);
  153. if (pwm == NULL) {
  154. dev_err(&pdev->dev, "failed to allocate memory\n");
  155. return -ENOMEM;
  156. }
  157. pwm->clk = clk_get(&pdev->dev, "pwm");
  158. if (IS_ERR(pwm->clk)) {
  159. ret = PTR_ERR(pwm->clk);
  160. goto err_free;
  161. }
  162. pwm->clk_enabled = 0;
  163. pwm->use_count = 0;
  164. pwm->pwm_id = pdev->id;
  165. pwm->pdev = pdev;
  166. r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  167. if (r == NULL) {
  168. dev_err(&pdev->dev, "no memory resource defined\n");
  169. ret = -ENODEV;
  170. goto err_free_clk;
  171. }
  172. r = request_mem_region(r->start, r->end - r->start + 1, pdev->name);
  173. if (r == NULL) {
  174. dev_err(&pdev->dev, "failed to request memory resource\n");
  175. ret = -EBUSY;
  176. goto err_free_clk;
  177. }
  178. pwm->mmio_base = ioremap(r->start, r->end - r->start + 1);
  179. if (pwm->mmio_base == NULL) {
  180. dev_err(&pdev->dev, "failed to ioremap() registers\n");
  181. ret = -ENODEV;
  182. goto err_free_mem;
  183. }
  184. mutex_lock(&pwm_lock);
  185. list_add_tail(&pwm->node, &pwm_list);
  186. mutex_unlock(&pwm_lock);
  187. platform_set_drvdata(pdev, pwm);
  188. return 0;
  189. err_free_mem:
  190. release_mem_region(r->start, r->end - r->start + 1);
  191. err_free_clk:
  192. clk_put(pwm->clk);
  193. err_free:
  194. kfree(pwm);
  195. return ret;
  196. }
  197. static int __devexit mxc_pwm_remove(struct platform_device *pdev)
  198. {
  199. struct pwm_device *pwm;
  200. struct resource *r;
  201. pwm = platform_get_drvdata(pdev);
  202. if (pwm == NULL)
  203. return -ENODEV;
  204. mutex_lock(&pwm_lock);
  205. list_del(&pwm->node);
  206. mutex_unlock(&pwm_lock);
  207. iounmap(pwm->mmio_base);
  208. r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  209. release_mem_region(r->start, r->end - r->start + 1);
  210. clk_put(pwm->clk);
  211. kfree(pwm);
  212. return 0;
  213. }
  214. static struct platform_driver mxc_pwm_driver = {
  215. .driver = {
  216. .name = "mxc_pwm",
  217. },
  218. .probe = mxc_pwm_probe,
  219. .remove = __devexit_p(mxc_pwm_remove),
  220. };
  221. static int __init mxc_pwm_init(void)
  222. {
  223. return platform_driver_register(&mxc_pwm_driver);
  224. }
  225. arch_initcall(mxc_pwm_init);
  226. static void __exit mxc_pwm_exit(void)
  227. {
  228. platform_driver_unregister(&mxc_pwm_driver);
  229. }
  230. module_exit(mxc_pwm_exit);
  231. MODULE_LICENSE("GPL v2");
  232. MODULE_AUTHOR("Sascha Hauer <s.hauer@pengutronix.de>");