pwm-imx1.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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 <[email protected]>
  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, u64 duty_ns, u64 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 = mul_u64_u64_div_u64(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 int pwm_imx1_apply(struct pwm_chip *chip, struct pwm_device *pwm,
  101. const struct pwm_state *state)
  102. {
  103. int err;
  104. if (state->polarity != PWM_POLARITY_NORMAL)
  105. return -EINVAL;
  106. if (!state->enabled) {
  107. if (pwm->state.enabled)
  108. pwm_imx1_disable(chip, pwm);
  109. return 0;
  110. }
  111. err = pwm_imx1_config(chip, pwm, state->duty_cycle, state->period);
  112. if (err)
  113. return err;
  114. if (!pwm->state.enabled)
  115. return pwm_imx1_enable(chip, pwm);
  116. return 0;
  117. }
  118. static const struct pwm_ops pwm_imx1_ops = {
  119. .apply = pwm_imx1_apply,
  120. .owner = THIS_MODULE,
  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_imx1_chip *imx;
  130. imx = devm_kzalloc(&pdev->dev, sizeof(*imx), GFP_KERNEL);
  131. if (!imx)
  132. return -ENOMEM;
  133. imx->clk_ipg = devm_clk_get(&pdev->dev, "ipg");
  134. if (IS_ERR(imx->clk_ipg))
  135. return dev_err_probe(&pdev->dev, PTR_ERR(imx->clk_ipg),
  136. "getting ipg clock failed\n");
  137. imx->clk_per = devm_clk_get(&pdev->dev, "per");
  138. if (IS_ERR(imx->clk_per))
  139. return dev_err_probe(&pdev->dev, PTR_ERR(imx->clk_per),
  140. "failed to get peripheral clock\n");
  141. imx->chip.ops = &pwm_imx1_ops;
  142. imx->chip.dev = &pdev->dev;
  143. imx->chip.npwm = 1;
  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, &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. };
  156. module_platform_driver(pwm_imx1_driver);
  157. MODULE_LICENSE("GPL v2");
  158. MODULE_AUTHOR("Sascha Hauer <[email protected]>");