pwm.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  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_mx27() || cpu_is_mx3() || cpu_is_mx25() || cpu_is_mx51()) {
  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. /*
  62. * according to imx pwm RM, the real period value should be
  63. * PERIOD value in PWMPR plus 2.
  64. */
  65. if (period_cycles > 2)
  66. period_cycles -= 2;
  67. else
  68. period_cycles = 0;
  69. writel(duty_cycles, pwm->mmio_base + MX3_PWMSAR);
  70. writel(period_cycles, pwm->mmio_base + MX3_PWMPR);
  71. cr = MX3_PWMCR_PRESCALER(prescale) |
  72. MX3_PWMCR_DOZEEN | MX3_PWMCR_WAITEN |
  73. MX3_PWMCR_DBGEN | MX3_PWMCR_EN;
  74. if (cpu_is_mx25())
  75. cr |= MX3_PWMCR_CLKSRC_IPG;
  76. else
  77. cr |= MX3_PWMCR_CLKSRC_IPG_HIGH;
  78. writel(cr, pwm->mmio_base + MX3_PWMCR);
  79. } else if (cpu_is_mx1() || cpu_is_mx21()) {
  80. /* The PWM subsystem allows for exact frequencies. However,
  81. * I cannot connect a scope on my device to the PWM line and
  82. * thus cannot provide the program the PWM controller
  83. * exactly. Instead, I'm relying on the fact that the
  84. * Bootloader (u-boot or WinCE+haret) has programmed the PWM
  85. * function group already. So I'll just modify the PWM sample
  86. * register to follow the ratio of duty_ns vs. period_ns
  87. * accordingly.
  88. *
  89. * This is good enough for programming the brightness of
  90. * the LCD backlight.
  91. *
  92. * The real implementation would divide PERCLK[0] first by
  93. * both the prescaler (/1 .. /128) and then by CLKSEL
  94. * (/2 .. /16).
  95. */
  96. u32 max = readl(pwm->mmio_base + MX1_PWMP);
  97. u32 p = max * duty_ns / period_ns;
  98. writel(max - p, pwm->mmio_base + MX1_PWMS);
  99. } else {
  100. BUG();
  101. }
  102. return 0;
  103. }
  104. EXPORT_SYMBOL(pwm_config);
  105. int pwm_enable(struct pwm_device *pwm)
  106. {
  107. int rc = 0;
  108. if (!pwm->clk_enabled) {
  109. rc = clk_enable(pwm->clk);
  110. if (!rc)
  111. pwm->clk_enabled = 1;
  112. }
  113. return rc;
  114. }
  115. EXPORT_SYMBOL(pwm_enable);
  116. void pwm_disable(struct pwm_device *pwm)
  117. {
  118. writel(0, pwm->mmio_base + MX3_PWMCR);
  119. if (pwm->clk_enabled) {
  120. clk_disable(pwm->clk);
  121. pwm->clk_enabled = 0;
  122. }
  123. }
  124. EXPORT_SYMBOL(pwm_disable);
  125. static DEFINE_MUTEX(pwm_lock);
  126. static LIST_HEAD(pwm_list);
  127. struct pwm_device *pwm_request(int pwm_id, const char *label)
  128. {
  129. struct pwm_device *pwm;
  130. int found = 0;
  131. mutex_lock(&pwm_lock);
  132. list_for_each_entry(pwm, &pwm_list, node) {
  133. if (pwm->pwm_id == pwm_id) {
  134. found = 1;
  135. break;
  136. }
  137. }
  138. if (found) {
  139. if (pwm->use_count == 0) {
  140. pwm->use_count++;
  141. pwm->label = label;
  142. } else
  143. pwm = ERR_PTR(-EBUSY);
  144. } else
  145. pwm = ERR_PTR(-ENOENT);
  146. mutex_unlock(&pwm_lock);
  147. return pwm;
  148. }
  149. EXPORT_SYMBOL(pwm_request);
  150. void pwm_free(struct pwm_device *pwm)
  151. {
  152. mutex_lock(&pwm_lock);
  153. if (pwm->use_count) {
  154. pwm->use_count--;
  155. pwm->label = NULL;
  156. } else
  157. pr_warning("PWM device already freed\n");
  158. mutex_unlock(&pwm_lock);
  159. }
  160. EXPORT_SYMBOL(pwm_free);
  161. static int __devinit mxc_pwm_probe(struct platform_device *pdev)
  162. {
  163. struct pwm_device *pwm;
  164. struct resource *r;
  165. int ret = 0;
  166. pwm = kzalloc(sizeof(struct pwm_device), GFP_KERNEL);
  167. if (pwm == NULL) {
  168. dev_err(&pdev->dev, "failed to allocate memory\n");
  169. return -ENOMEM;
  170. }
  171. pwm->clk = clk_get(&pdev->dev, "pwm");
  172. if (IS_ERR(pwm->clk)) {
  173. ret = PTR_ERR(pwm->clk);
  174. goto err_free;
  175. }
  176. pwm->clk_enabled = 0;
  177. pwm->use_count = 0;
  178. pwm->pwm_id = pdev->id;
  179. pwm->pdev = pdev;
  180. r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  181. if (r == NULL) {
  182. dev_err(&pdev->dev, "no memory resource defined\n");
  183. ret = -ENODEV;
  184. goto err_free_clk;
  185. }
  186. r = request_mem_region(r->start, r->end - r->start + 1, pdev->name);
  187. if (r == NULL) {
  188. dev_err(&pdev->dev, "failed to request memory resource\n");
  189. ret = -EBUSY;
  190. goto err_free_clk;
  191. }
  192. pwm->mmio_base = ioremap(r->start, r->end - r->start + 1);
  193. if (pwm->mmio_base == NULL) {
  194. dev_err(&pdev->dev, "failed to ioremap() registers\n");
  195. ret = -ENODEV;
  196. goto err_free_mem;
  197. }
  198. mutex_lock(&pwm_lock);
  199. list_add_tail(&pwm->node, &pwm_list);
  200. mutex_unlock(&pwm_lock);
  201. platform_set_drvdata(pdev, pwm);
  202. return 0;
  203. err_free_mem:
  204. release_mem_region(r->start, r->end - r->start + 1);
  205. err_free_clk:
  206. clk_put(pwm->clk);
  207. err_free:
  208. kfree(pwm);
  209. return ret;
  210. }
  211. static int __devexit mxc_pwm_remove(struct platform_device *pdev)
  212. {
  213. struct pwm_device *pwm;
  214. struct resource *r;
  215. pwm = platform_get_drvdata(pdev);
  216. if (pwm == NULL)
  217. return -ENODEV;
  218. mutex_lock(&pwm_lock);
  219. list_del(&pwm->node);
  220. mutex_unlock(&pwm_lock);
  221. iounmap(pwm->mmio_base);
  222. r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  223. release_mem_region(r->start, r->end - r->start + 1);
  224. clk_put(pwm->clk);
  225. kfree(pwm);
  226. return 0;
  227. }
  228. static struct platform_driver mxc_pwm_driver = {
  229. .driver = {
  230. .name = "mxc_pwm",
  231. },
  232. .probe = mxc_pwm_probe,
  233. .remove = __devexit_p(mxc_pwm_remove),
  234. };
  235. static int __init mxc_pwm_init(void)
  236. {
  237. return platform_driver_register(&mxc_pwm_driver);
  238. }
  239. arch_initcall(mxc_pwm_init);
  240. static void __exit mxc_pwm_exit(void)
  241. {
  242. platform_driver_unregister(&mxc_pwm_driver);
  243. }
  244. module_exit(mxc_pwm_exit);
  245. MODULE_LICENSE("GPL v2");
  246. MODULE_AUTHOR("Sascha Hauer <s.hauer@pengutronix.de>");