pwm.c 6.6 KB

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