pwm-imx.c 7.2 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 <linux/of_device.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. #define MX1_PWMC_EN (1 << 4)
  24. /* i.MX27, i.MX31, i.MX35 share the same PWM function block: */
  25. #define MX3_PWMCR 0x00 /* PWM Control Register */
  26. #define MX3_PWMSAR 0x0C /* PWM Sample Register */
  27. #define MX3_PWMPR 0x10 /* PWM Period Register */
  28. #define MX3_PWMCR_PRESCALER(x) (((x - 1) & 0xFFF) << 4)
  29. #define MX3_PWMCR_DOZEEN (1 << 24)
  30. #define MX3_PWMCR_WAITEN (1 << 23)
  31. #define MX3_PWMCR_DBGEN (1 << 22)
  32. #define MX3_PWMCR_CLKSRC_IPG_HIGH (2 << 16)
  33. #define MX3_PWMCR_CLKSRC_IPG (1 << 16)
  34. #define MX3_PWMCR_EN (1 << 0)
  35. struct imx_chip {
  36. struct clk *clk;
  37. int enabled;
  38. void __iomem *mmio_base;
  39. struct pwm_chip chip;
  40. int (*config)(struct pwm_chip *chip,
  41. struct pwm_device *pwm, int duty_ns, int period_ns);
  42. void (*set_enable)(struct pwm_chip *chip, bool enable);
  43. };
  44. #define to_imx_chip(chip) container_of(chip, struct imx_chip, chip)
  45. static int imx_pwm_config_v1(struct pwm_chip *chip,
  46. struct pwm_device *pwm, int duty_ns, int period_ns)
  47. {
  48. struct imx_chip *imx = to_imx_chip(chip);
  49. /*
  50. * The PWM subsystem allows for exact frequencies. However,
  51. * I cannot connect a scope on my device to the PWM line and
  52. * thus cannot provide the program the PWM controller
  53. * exactly. Instead, I'm relying on the fact that the
  54. * Bootloader (u-boot or WinCE+haret) has programmed the PWM
  55. * function group already. So I'll just modify the PWM sample
  56. * register to follow the ratio of duty_ns vs. period_ns
  57. * accordingly.
  58. *
  59. * This is good enough for programming the brightness of
  60. * the LCD backlight.
  61. *
  62. * The real implementation would divide PERCLK[0] first by
  63. * both the prescaler (/1 .. /128) and then by CLKSEL
  64. * (/2 .. /16).
  65. */
  66. u32 max = readl(imx->mmio_base + MX1_PWMP);
  67. u32 p = max * duty_ns / period_ns;
  68. writel(max - p, imx->mmio_base + MX1_PWMS);
  69. return 0;
  70. }
  71. static void imx_pwm_set_enable_v1(struct pwm_chip *chip, bool enable)
  72. {
  73. struct imx_chip *imx = to_imx_chip(chip);
  74. u32 val;
  75. val = readl(imx->mmio_base + MX1_PWMC);
  76. if (enable)
  77. val |= MX1_PWMC_EN;
  78. else
  79. val &= ~MX1_PWMC_EN;
  80. writel(val, imx->mmio_base + MX1_PWMC);
  81. }
  82. static int imx_pwm_config_v2(struct pwm_chip *chip,
  83. struct pwm_device *pwm, int duty_ns, int period_ns)
  84. {
  85. struct imx_chip *imx = to_imx_chip(chip);
  86. unsigned long long c;
  87. unsigned long period_cycles, duty_cycles, prescale;
  88. u32 cr;
  89. c = clk_get_rate(imx->clk);
  90. c = c * period_ns;
  91. do_div(c, 1000000000);
  92. period_cycles = c;
  93. prescale = period_cycles / 0x10000 + 1;
  94. period_cycles /= prescale;
  95. c = (unsigned long long)period_cycles * duty_ns;
  96. do_div(c, period_ns);
  97. duty_cycles = c;
  98. /*
  99. * according to imx pwm RM, the real period value should be
  100. * PERIOD value in PWMPR plus 2.
  101. */
  102. if (period_cycles > 2)
  103. period_cycles -= 2;
  104. else
  105. period_cycles = 0;
  106. writel(duty_cycles, imx->mmio_base + MX3_PWMSAR);
  107. writel(period_cycles, imx->mmio_base + MX3_PWMPR);
  108. cr = MX3_PWMCR_PRESCALER(prescale) |
  109. MX3_PWMCR_DOZEEN | MX3_PWMCR_WAITEN |
  110. MX3_PWMCR_DBGEN | MX3_PWMCR_CLKSRC_IPG_HIGH;
  111. if (imx->enabled)
  112. cr |= MX3_PWMCR_EN;
  113. writel(cr, imx->mmio_base + MX3_PWMCR);
  114. return 0;
  115. }
  116. static void imx_pwm_set_enable_v2(struct pwm_chip *chip, bool enable)
  117. {
  118. struct imx_chip *imx = to_imx_chip(chip);
  119. u32 val;
  120. val = readl(imx->mmio_base + MX3_PWMCR);
  121. if (enable)
  122. val |= MX3_PWMCR_EN;
  123. else
  124. val &= ~MX3_PWMCR_EN;
  125. writel(val, imx->mmio_base + MX3_PWMCR);
  126. }
  127. static int imx_pwm_config(struct pwm_chip *chip,
  128. struct pwm_device *pwm, int duty_ns, int period_ns)
  129. {
  130. struct imx_chip *imx = to_imx_chip(chip);
  131. return imx->config(chip, pwm, duty_ns, period_ns);
  132. }
  133. static int imx_pwm_enable(struct pwm_chip *chip, struct pwm_device *pwm)
  134. {
  135. struct imx_chip *imx = to_imx_chip(chip);
  136. int ret;
  137. ret = clk_prepare_enable(imx->clk);
  138. if (ret)
  139. return ret;
  140. imx->set_enable(chip, true);
  141. imx->enabled = 1;
  142. return 0;
  143. }
  144. static void imx_pwm_disable(struct pwm_chip *chip, struct pwm_device *pwm)
  145. {
  146. struct imx_chip *imx = to_imx_chip(chip);
  147. imx->set_enable(chip, false);
  148. clk_disable_unprepare(imx->clk);
  149. imx->enabled = 0;
  150. }
  151. static struct pwm_ops imx_pwm_ops = {
  152. .enable = imx_pwm_enable,
  153. .disable = imx_pwm_disable,
  154. .config = imx_pwm_config,
  155. .owner = THIS_MODULE,
  156. };
  157. struct imx_pwm_data {
  158. int (*config)(struct pwm_chip *chip,
  159. struct pwm_device *pwm, int duty_ns, int period_ns);
  160. void (*set_enable)(struct pwm_chip *chip, bool enable);
  161. };
  162. static struct imx_pwm_data imx_pwm_data_v1 = {
  163. .config = imx_pwm_config_v1,
  164. .set_enable = imx_pwm_set_enable_v1,
  165. };
  166. static struct imx_pwm_data imx_pwm_data_v2 = {
  167. .config = imx_pwm_config_v2,
  168. .set_enable = imx_pwm_set_enable_v2,
  169. };
  170. static const struct of_device_id imx_pwm_dt_ids[] = {
  171. { .compatible = "fsl,imx1-pwm", .data = &imx_pwm_data_v1, },
  172. { .compatible = "fsl,imx27-pwm", .data = &imx_pwm_data_v2, },
  173. { /* sentinel */ }
  174. };
  175. MODULE_DEVICE_TABLE(of, imx_pwm_dt_ids);
  176. static int __devinit imx_pwm_probe(struct platform_device *pdev)
  177. {
  178. const struct of_device_id *of_id =
  179. of_match_device(imx_pwm_dt_ids, &pdev->dev);
  180. struct imx_pwm_data *data;
  181. struct imx_chip *imx;
  182. struct resource *r;
  183. int ret = 0;
  184. if (!of_id)
  185. return -ENODEV;
  186. imx = devm_kzalloc(&pdev->dev, sizeof(*imx), GFP_KERNEL);
  187. if (imx == NULL) {
  188. dev_err(&pdev->dev, "failed to allocate memory\n");
  189. return -ENOMEM;
  190. }
  191. imx->clk = devm_clk_get(&pdev->dev, "pwm");
  192. if (IS_ERR(imx->clk))
  193. return PTR_ERR(imx->clk);
  194. imx->chip.ops = &imx_pwm_ops;
  195. imx->chip.dev = &pdev->dev;
  196. imx->chip.base = -1;
  197. imx->chip.npwm = 1;
  198. r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  199. if (r == NULL) {
  200. dev_err(&pdev->dev, "no memory resource defined\n");
  201. return -ENODEV;
  202. }
  203. imx->mmio_base = devm_request_and_ioremap(&pdev->dev, r);
  204. if (imx->mmio_base == NULL)
  205. return -EADDRNOTAVAIL;
  206. data = of_id->data;
  207. imx->config = data->config;
  208. imx->set_enable = data->set_enable;
  209. ret = pwmchip_add(&imx->chip);
  210. if (ret < 0)
  211. return ret;
  212. platform_set_drvdata(pdev, imx);
  213. return 0;
  214. }
  215. static int __devexit imx_pwm_remove(struct platform_device *pdev)
  216. {
  217. struct imx_chip *imx;
  218. imx = platform_get_drvdata(pdev);
  219. if (imx == NULL)
  220. return -ENODEV;
  221. return pwmchip_remove(&imx->chip);
  222. }
  223. static struct platform_driver imx_pwm_driver = {
  224. .driver = {
  225. .name = "imx-pwm",
  226. .of_match_table = of_match_ptr(imx_pwm_dt_ids),
  227. },
  228. .probe = imx_pwm_probe,
  229. .remove = __devexit_p(imx_pwm_remove),
  230. };
  231. module_platform_driver(imx_pwm_driver);
  232. MODULE_LICENSE("GPL v2");
  233. MODULE_AUTHOR("Sascha Hauer <s.hauer@pengutronix.de>");