pwm-imx1.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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/platform_device.h>
  17. #include <linux/pwm.h>
  18. #include <linux/slab.h>
  19. #define MX1_PWMC 0x00 /* PWM Control Register */
  20. #define MX1_PWMS 0x04 /* PWM Sample Register */
  21. #define MX1_PWMP 0x08 /* PWM Period Register */
  22. #define MX1_PWMC_EN BIT(4)
  23. struct pwm_imx1_chip {
  24. struct clk *clk_ipg;
  25. struct clk *clk_per;
  26. void __iomem *mmio_base;
  27. };
  28. static inline struct pwm_imx1_chip *to_pwm_imx1_chip(struct pwm_chip *chip)
  29. {
  30. return pwmchip_get_drvdata(chip);
  31. }
  32. static int pwm_imx1_clk_prepare_enable(struct pwm_chip *chip)
  33. {
  34. struct pwm_imx1_chip *imx = to_pwm_imx1_chip(chip);
  35. int ret;
  36. ret = clk_prepare_enable(imx->clk_ipg);
  37. if (ret)
  38. return ret;
  39. ret = clk_prepare_enable(imx->clk_per);
  40. if (ret) {
  41. clk_disable_unprepare(imx->clk_ipg);
  42. return ret;
  43. }
  44. return 0;
  45. }
  46. static void pwm_imx1_clk_disable_unprepare(struct pwm_chip *chip)
  47. {
  48. struct pwm_imx1_chip *imx = to_pwm_imx1_chip(chip);
  49. clk_disable_unprepare(imx->clk_per);
  50. clk_disable_unprepare(imx->clk_ipg);
  51. }
  52. static int pwm_imx1_config(struct pwm_chip *chip,
  53. struct pwm_device *pwm, u64 duty_ns, u64 period_ns)
  54. {
  55. struct pwm_imx1_chip *imx = to_pwm_imx1_chip(chip);
  56. u32 max, p;
  57. /*
  58. * The PWM subsystem allows for exact frequencies. However,
  59. * I cannot connect a scope on my device to the PWM line and
  60. * thus cannot provide the program the PWM controller
  61. * exactly. Instead, I'm relying on the fact that the
  62. * Bootloader (u-boot or WinCE+haret) has programmed the PWM
  63. * function group already. So I'll just modify the PWM sample
  64. * register to follow the ratio of duty_ns vs. period_ns
  65. * accordingly.
  66. *
  67. * This is good enough for programming the brightness of
  68. * the LCD backlight.
  69. *
  70. * The real implementation would divide PERCLK[0] first by
  71. * both the prescaler (/1 .. /128) and then by CLKSEL
  72. * (/2 .. /16).
  73. */
  74. max = readl(imx->mmio_base + MX1_PWMP);
  75. p = mul_u64_u64_div_u64(max, duty_ns, period_ns);
  76. writel(max - p, imx->mmio_base + MX1_PWMS);
  77. return 0;
  78. }
  79. static int pwm_imx1_enable(struct pwm_chip *chip, struct pwm_device *pwm)
  80. {
  81. struct pwm_imx1_chip *imx = to_pwm_imx1_chip(chip);
  82. u32 value;
  83. int ret;
  84. ret = pwm_imx1_clk_prepare_enable(chip);
  85. if (ret < 0)
  86. return ret;
  87. value = readl(imx->mmio_base + MX1_PWMC);
  88. value |= MX1_PWMC_EN;
  89. writel(value, imx->mmio_base + MX1_PWMC);
  90. return 0;
  91. }
  92. static void pwm_imx1_disable(struct pwm_chip *chip, struct pwm_device *pwm)
  93. {
  94. struct pwm_imx1_chip *imx = to_pwm_imx1_chip(chip);
  95. u32 value;
  96. value = readl(imx->mmio_base + MX1_PWMC);
  97. value &= ~MX1_PWMC_EN;
  98. writel(value, imx->mmio_base + MX1_PWMC);
  99. pwm_imx1_clk_disable_unprepare(chip);
  100. }
  101. static int pwm_imx1_apply(struct pwm_chip *chip, struct pwm_device *pwm,
  102. const struct pwm_state *state)
  103. {
  104. int err;
  105. if (state->polarity != PWM_POLARITY_NORMAL)
  106. return -EINVAL;
  107. if (!state->enabled) {
  108. if (pwm->state.enabled)
  109. pwm_imx1_disable(chip, pwm);
  110. return 0;
  111. }
  112. err = pwm_imx1_config(chip, pwm, state->duty_cycle, state->period);
  113. if (err)
  114. return err;
  115. if (!pwm->state.enabled)
  116. return pwm_imx1_enable(chip, pwm);
  117. return 0;
  118. }
  119. static const struct pwm_ops pwm_imx1_ops = {
  120. .apply = pwm_imx1_apply,
  121. };
  122. static const struct of_device_id pwm_imx1_dt_ids[] = {
  123. { .compatible = "fsl,imx1-pwm", },
  124. { /* sentinel */ }
  125. };
  126. MODULE_DEVICE_TABLE(of, pwm_imx1_dt_ids);
  127. static int pwm_imx1_probe(struct platform_device *pdev)
  128. {
  129. struct pwm_chip *chip;
  130. struct pwm_imx1_chip *imx;
  131. chip = devm_pwmchip_alloc(&pdev->dev, 1, sizeof(*imx));
  132. if (IS_ERR(chip))
  133. return PTR_ERR(chip);
  134. imx = to_pwm_imx1_chip(chip);
  135. imx->clk_ipg = devm_clk_get(&pdev->dev, "ipg");
  136. if (IS_ERR(imx->clk_ipg))
  137. return dev_err_probe(&pdev->dev, PTR_ERR(imx->clk_ipg),
  138. "getting ipg clock failed\n");
  139. imx->clk_per = devm_clk_get(&pdev->dev, "per");
  140. if (IS_ERR(imx->clk_per))
  141. return dev_err_probe(&pdev->dev, PTR_ERR(imx->clk_per),
  142. "failed to get peripheral clock\n");
  143. chip->ops = &pwm_imx1_ops;
  144. imx->mmio_base = devm_platform_ioremap_resource(pdev, 0);
  145. if (IS_ERR(imx->mmio_base))
  146. return PTR_ERR(imx->mmio_base);
  147. return devm_pwmchip_add(&pdev->dev, 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. };
  156. module_platform_driver(pwm_imx1_driver);
  157. MODULE_DESCRIPTION("i.MX1 and i.MX21 Pulse Width Modulator driver");
  158. MODULE_LICENSE("GPL v2");
  159. MODULE_AUTHOR("Sascha Hauer <s.hauer@pengutronix.de>");