pwm-imx1.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * simple driver for PWM (Pulse Width Modulator) controller
  4. *
  5. * Derived from pxa PWM driver by eric miao <eric.miao@marvell.com>
  6. */
  7. #include <linux/bitfield.h>
  8. #include <linux/bitops.h>
  9. #include <linux/clk.h>
  10. #include <linux/delay.h>
  11. #include <linux/err.h>
  12. #include <linux/io.h>
  13. #include <linux/kernel.h>
  14. #include <linux/module.h>
  15. #include <linux/of.h>
  16. #include <linux/of_device.h>
  17. #include <linux/platform_device.h>
  18. #include <linux/pwm.h>
  19. #include <linux/slab.h>
  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 BIT(4)
  24. struct pwm_imx1_chip {
  25. struct clk *clk_ipg;
  26. struct clk *clk_per;
  27. void __iomem *mmio_base;
  28. struct pwm_chip chip;
  29. };
  30. #define to_pwm_imx1_chip(chip) container_of(chip, struct pwm_imx1_chip, chip)
  31. static int pwm_imx1_clk_prepare_enable(struct pwm_chip *chip)
  32. {
  33. struct pwm_imx1_chip *imx = to_pwm_imx1_chip(chip);
  34. int ret;
  35. ret = clk_prepare_enable(imx->clk_ipg);
  36. if (ret)
  37. return ret;
  38. ret = clk_prepare_enable(imx->clk_per);
  39. if (ret) {
  40. clk_disable_unprepare(imx->clk_ipg);
  41. return ret;
  42. }
  43. return 0;
  44. }
  45. static void pwm_imx1_clk_disable_unprepare(struct pwm_chip *chip)
  46. {
  47. struct pwm_imx1_chip *imx = to_pwm_imx1_chip(chip);
  48. clk_disable_unprepare(imx->clk_per);
  49. clk_disable_unprepare(imx->clk_ipg);
  50. }
  51. static int pwm_imx1_config(struct pwm_chip *chip,
  52. struct pwm_device *pwm, int duty_ns, int period_ns)
  53. {
  54. struct pwm_imx1_chip *imx = to_pwm_imx1_chip(chip);
  55. u32 max, p;
  56. /*
  57. * The PWM subsystem allows for exact frequencies. However,
  58. * I cannot connect a scope on my device to the PWM line and
  59. * thus cannot provide the program the PWM controller
  60. * exactly. Instead, I'm relying on the fact that the
  61. * Bootloader (u-boot or WinCE+haret) has programmed the PWM
  62. * function group already. So I'll just modify the PWM sample
  63. * register to follow the ratio of duty_ns vs. period_ns
  64. * accordingly.
  65. *
  66. * This is good enough for programming the brightness of
  67. * the LCD backlight.
  68. *
  69. * The real implementation would divide PERCLK[0] first by
  70. * both the prescaler (/1 .. /128) and then by CLKSEL
  71. * (/2 .. /16).
  72. */
  73. max = readl(imx->mmio_base + MX1_PWMP);
  74. p = max * duty_ns / period_ns;
  75. writel(max - p, imx->mmio_base + MX1_PWMS);
  76. return 0;
  77. }
  78. static int pwm_imx1_enable(struct pwm_chip *chip, struct pwm_device *pwm)
  79. {
  80. struct pwm_imx1_chip *imx = to_pwm_imx1_chip(chip);
  81. u32 value;
  82. int ret;
  83. ret = pwm_imx1_clk_prepare_enable(chip);
  84. if (ret < 0)
  85. return ret;
  86. value = readl(imx->mmio_base + MX1_PWMC);
  87. value |= MX1_PWMC_EN;
  88. writel(value, imx->mmio_base + MX1_PWMC);
  89. return 0;
  90. }
  91. static void pwm_imx1_disable(struct pwm_chip *chip, struct pwm_device *pwm)
  92. {
  93. struct pwm_imx1_chip *imx = to_pwm_imx1_chip(chip);
  94. u32 value;
  95. value = readl(imx->mmio_base + MX1_PWMC);
  96. value &= ~MX1_PWMC_EN;
  97. writel(value, imx->mmio_base + MX1_PWMC);
  98. pwm_imx1_clk_disable_unprepare(chip);
  99. }
  100. static const struct pwm_ops pwm_imx1_ops = {
  101. .enable = pwm_imx1_enable,
  102. .disable = pwm_imx1_disable,
  103. .config = pwm_imx1_config,
  104. .owner = THIS_MODULE,
  105. };
  106. static const struct of_device_id pwm_imx1_dt_ids[] = {
  107. { .compatible = "fsl,imx1-pwm", },
  108. { /* sentinel */ }
  109. };
  110. MODULE_DEVICE_TABLE(of, pwm_imx1_dt_ids);
  111. static int pwm_imx1_probe(struct platform_device *pdev)
  112. {
  113. struct pwm_imx1_chip *imx;
  114. struct resource *r;
  115. imx = devm_kzalloc(&pdev->dev, sizeof(*imx), GFP_KERNEL);
  116. if (!imx)
  117. return -ENOMEM;
  118. platform_set_drvdata(pdev, imx);
  119. imx->clk_ipg = devm_clk_get(&pdev->dev, "ipg");
  120. if (IS_ERR(imx->clk_ipg)) {
  121. dev_err(&pdev->dev, "getting ipg clock failed with %ld\n",
  122. PTR_ERR(imx->clk_ipg));
  123. return PTR_ERR(imx->clk_ipg);
  124. }
  125. imx->clk_per = devm_clk_get(&pdev->dev, "per");
  126. if (IS_ERR(imx->clk_per)) {
  127. int ret = PTR_ERR(imx->clk_per);
  128. if (ret != -EPROBE_DEFER)
  129. dev_err(&pdev->dev,
  130. "failed to get peripheral clock: %d\n",
  131. ret);
  132. return ret;
  133. }
  134. imx->chip.ops = &pwm_imx1_ops;
  135. imx->chip.dev = &pdev->dev;
  136. imx->chip.base = -1;
  137. imx->chip.npwm = 1;
  138. r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  139. imx->mmio_base = devm_ioremap_resource(&pdev->dev, r);
  140. if (IS_ERR(imx->mmio_base))
  141. return PTR_ERR(imx->mmio_base);
  142. return pwmchip_add(&imx->chip);
  143. }
  144. static int pwm_imx1_remove(struct platform_device *pdev)
  145. {
  146. struct pwm_imx1_chip *imx = platform_get_drvdata(pdev);
  147. return pwmchip_remove(&imx->chip);
  148. }
  149. static struct platform_driver pwm_imx1_driver = {
  150. .driver = {
  151. .name = "pwm-imx1",
  152. .of_match_table = pwm_imx1_dt_ids,
  153. },
  154. .probe = pwm_imx1_probe,
  155. .remove = pwm_imx1_remove,
  156. };
  157. module_platform_driver(pwm_imx1_driver);
  158. MODULE_LICENSE("GPL v2");
  159. MODULE_AUTHOR("Sascha Hauer <s.hauer@pengutronix.de>");