pwm-imx.c 7.6 KB

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