pwm-imx.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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 imx_chip {
  35. struct clk *clk;
  36. int enabled;
  37. void __iomem *mmio_base;
  38. struct pwm_chip chip;
  39. int (*config)(struct pwm_chip *chip,
  40. struct pwm_device *pwm, int duty_ns, int period_ns);
  41. };
  42. #define to_imx_chip(chip) container_of(chip, struct imx_chip, chip)
  43. static int imx_pwm_config_v1(struct pwm_chip *chip,
  44. struct pwm_device *pwm, int duty_ns, int period_ns)
  45. {
  46. struct imx_chip *imx = to_imx_chip(chip);
  47. /*
  48. * The PWM subsystem allows for exact frequencies. However,
  49. * I cannot connect a scope on my device to the PWM line and
  50. * thus cannot provide the program the PWM controller
  51. * exactly. Instead, I'm relying on the fact that the
  52. * Bootloader (u-boot or WinCE+haret) has programmed the PWM
  53. * function group already. So I'll just modify the PWM sample
  54. * register to follow the ratio of duty_ns vs. period_ns
  55. * accordingly.
  56. *
  57. * This is good enough for programming the brightness of
  58. * the LCD backlight.
  59. *
  60. * The real implementation would divide PERCLK[0] first by
  61. * both the prescaler (/1 .. /128) and then by CLKSEL
  62. * (/2 .. /16).
  63. */
  64. u32 max = readl(imx->mmio_base + MX1_PWMP);
  65. u32 p = max * duty_ns / period_ns;
  66. writel(max - p, imx->mmio_base + MX1_PWMS);
  67. return 0;
  68. }
  69. static int imx_pwm_config_v2(struct pwm_chip *chip,
  70. struct pwm_device *pwm, int duty_ns, int period_ns)
  71. {
  72. struct imx_chip *imx = to_imx_chip(chip);
  73. unsigned long long c;
  74. unsigned long period_cycles, duty_cycles, prescale;
  75. u32 cr;
  76. c = clk_get_rate(imx->clk);
  77. c = c * period_ns;
  78. do_div(c, 1000000000);
  79. period_cycles = c;
  80. prescale = period_cycles / 0x10000 + 1;
  81. period_cycles /= prescale;
  82. c = (unsigned long long)period_cycles * duty_ns;
  83. do_div(c, period_ns);
  84. duty_cycles = c;
  85. /*
  86. * according to imx pwm RM, the real period value should be
  87. * PERIOD value in PWMPR plus 2.
  88. */
  89. if (period_cycles > 2)
  90. period_cycles -= 2;
  91. else
  92. period_cycles = 0;
  93. writel(duty_cycles, imx->mmio_base + MX3_PWMSAR);
  94. writel(period_cycles, imx->mmio_base + MX3_PWMPR);
  95. cr = MX3_PWMCR_PRESCALER(prescale) |
  96. MX3_PWMCR_DOZEEN | MX3_PWMCR_WAITEN |
  97. MX3_PWMCR_DBGEN | MX3_PWMCR_EN;
  98. if (cpu_is_mx25())
  99. cr |= MX3_PWMCR_CLKSRC_IPG;
  100. else
  101. cr |= MX3_PWMCR_CLKSRC_IPG_HIGH;
  102. writel(cr, imx->mmio_base + MX3_PWMCR);
  103. return 0;
  104. }
  105. static int imx_pwm_config(struct pwm_chip *chip,
  106. struct pwm_device *pwm, int duty_ns, int period_ns)
  107. {
  108. struct imx_chip *imx = to_imx_chip(chip);
  109. return imx->config(chip, pwm, duty_ns, period_ns);
  110. }
  111. static int imx_pwm_enable(struct pwm_chip *chip, struct pwm_device *pwm)
  112. {
  113. struct imx_chip *imx = to_imx_chip(chip);
  114. int ret;
  115. ret = clk_prepare_enable(imx->clk);
  116. if (ret)
  117. return ret;
  118. imx->enabled = 1;
  119. return 0;
  120. }
  121. static void imx_pwm_disable(struct pwm_chip *chip, struct pwm_device *pwm)
  122. {
  123. struct imx_chip *imx = to_imx_chip(chip);
  124. writel(0, imx->mmio_base + MX3_PWMCR);
  125. clk_disable_unprepare(imx->clk);
  126. imx->enabled = 0;
  127. }
  128. static struct pwm_ops imx_pwm_ops = {
  129. .enable = imx_pwm_enable,
  130. .disable = imx_pwm_disable,
  131. .config = imx_pwm_config,
  132. .owner = THIS_MODULE,
  133. };
  134. static int __devinit imx_pwm_probe(struct platform_device *pdev)
  135. {
  136. struct imx_chip *imx;
  137. struct resource *r;
  138. int ret = 0;
  139. imx = devm_kzalloc(&pdev->dev, sizeof(*imx), GFP_KERNEL);
  140. if (imx == NULL) {
  141. dev_err(&pdev->dev, "failed to allocate memory\n");
  142. return -ENOMEM;
  143. }
  144. imx->clk = devm_clk_get(&pdev->dev, "pwm");
  145. if (IS_ERR(imx->clk))
  146. return PTR_ERR(imx->clk);
  147. imx->chip.ops = &imx_pwm_ops;
  148. imx->chip.dev = &pdev->dev;
  149. imx->chip.base = -1;
  150. imx->chip.npwm = 1;
  151. r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  152. if (r == NULL) {
  153. dev_err(&pdev->dev, "no memory resource defined\n");
  154. return -ENODEV;
  155. }
  156. imx->mmio_base = devm_request_and_ioremap(&pdev->dev, r);
  157. if (imx->mmio_base == NULL)
  158. return -EADDRNOTAVAIL;
  159. if (cpu_is_mx1() || cpu_is_mx21())
  160. imx->config = imx_pwm_config_v1;
  161. else
  162. imx->config = imx_pwm_config_v2;
  163. ret = pwmchip_add(&imx->chip);
  164. if (ret < 0)
  165. return ret;
  166. platform_set_drvdata(pdev, imx);
  167. return 0;
  168. }
  169. static int __devexit imx_pwm_remove(struct platform_device *pdev)
  170. {
  171. struct imx_chip *imx;
  172. imx = platform_get_drvdata(pdev);
  173. if (imx == NULL)
  174. return -ENODEV;
  175. return pwmchip_remove(&imx->chip);
  176. }
  177. static struct platform_driver imx_pwm_driver = {
  178. .driver = {
  179. .name = "mxc_pwm",
  180. },
  181. .probe = imx_pwm_probe,
  182. .remove = __devexit_p(imx_pwm_remove),
  183. };
  184. static int __init imx_pwm_init(void)
  185. {
  186. return platform_driver_register(&imx_pwm_driver);
  187. }
  188. arch_initcall(imx_pwm_init);
  189. static void __exit imx_pwm_exit(void)
  190. {
  191. platform_driver_unregister(&imx_pwm_driver);
  192. }
  193. module_exit(imx_pwm_exit);
  194. MODULE_LICENSE("GPL v2");
  195. MODULE_AUTHOR("Sascha Hauer <s.hauer@pengutronix.de>");