pwm-mxs.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright 2012 Freescale Semiconductor, Inc.
  4. */
  5. #include <linux/clk.h>
  6. #include <linux/err.h>
  7. #include <linux/io.h>
  8. #include <linux/kernel.h>
  9. #include <linux/module.h>
  10. #include <linux/of.h>
  11. #include <linux/platform_device.h>
  12. #include <linux/pwm.h>
  13. #include <linux/slab.h>
  14. #include <linux/stmp_device.h>
  15. #define SET 0x4
  16. #define CLR 0x8
  17. #define TOG 0xc
  18. #define PWM_CTRL 0x0
  19. #define PWM_ACTIVE0 0x10
  20. #define PWM_PERIOD0 0x20
  21. #define PERIOD_PERIOD(p) ((p) & 0xffff)
  22. #define PERIOD_PERIOD_MAX 0x10000
  23. #define PERIOD_ACTIVE_HIGH (3 << 16)
  24. #define PERIOD_ACTIVE_LOW (2 << 16)
  25. #define PERIOD_INACTIVE_HIGH (3 << 18)
  26. #define PERIOD_INACTIVE_LOW (2 << 18)
  27. #define PERIOD_POLARITY_NORMAL (PERIOD_ACTIVE_HIGH | PERIOD_INACTIVE_LOW)
  28. #define PERIOD_POLARITY_INVERSE (PERIOD_ACTIVE_LOW | PERIOD_INACTIVE_HIGH)
  29. #define PERIOD_CDIV(div) (((div) & 0x7) << 20)
  30. #define PERIOD_CDIV_MAX 8
  31. static const u8 cdiv_shift[PERIOD_CDIV_MAX] = {
  32. 0, 1, 2, 3, 4, 6, 8, 10
  33. };
  34. struct mxs_pwm_chip {
  35. struct clk *clk;
  36. void __iomem *base;
  37. };
  38. static inline struct mxs_pwm_chip *to_mxs_pwm_chip(struct pwm_chip *chip)
  39. {
  40. return pwmchip_get_drvdata(chip);
  41. }
  42. static int mxs_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm,
  43. const struct pwm_state *state)
  44. {
  45. struct mxs_pwm_chip *mxs = to_mxs_pwm_chip(chip);
  46. int ret, div = 0;
  47. unsigned int period_cycles, duty_cycles;
  48. unsigned long rate;
  49. unsigned long long c;
  50. unsigned int pol_bits;
  51. /*
  52. * If the PWM channel is disabled, make sure to turn on the
  53. * clock before calling clk_get_rate() and writing to the
  54. * registers. Otherwise, just keep it enabled.
  55. */
  56. if (!pwm_is_enabled(pwm)) {
  57. ret = clk_prepare_enable(mxs->clk);
  58. if (ret)
  59. return ret;
  60. }
  61. if (!state->enabled && pwm_is_enabled(pwm))
  62. writel(1 << pwm->hwpwm, mxs->base + PWM_CTRL + CLR);
  63. rate = clk_get_rate(mxs->clk);
  64. while (1) {
  65. c = rate >> cdiv_shift[div];
  66. c = c * state->period;
  67. do_div(c, 1000000000);
  68. if (c < PERIOD_PERIOD_MAX)
  69. break;
  70. div++;
  71. if (div >= PERIOD_CDIV_MAX)
  72. return -EINVAL;
  73. }
  74. period_cycles = c;
  75. c *= state->duty_cycle;
  76. do_div(c, state->period);
  77. duty_cycles = c;
  78. /*
  79. * The data sheet the says registers must be written to in
  80. * this order (ACTIVEn, then PERIODn). Also, the new settings
  81. * only take effect at the beginning of a new period, avoiding
  82. * glitches.
  83. */
  84. pol_bits = state->polarity == PWM_POLARITY_NORMAL ?
  85. PERIOD_POLARITY_NORMAL : PERIOD_POLARITY_INVERSE;
  86. writel(duty_cycles << 16,
  87. mxs->base + PWM_ACTIVE0 + pwm->hwpwm * 0x20);
  88. writel(PERIOD_PERIOD(period_cycles) | pol_bits | PERIOD_CDIV(div),
  89. mxs->base + PWM_PERIOD0 + pwm->hwpwm * 0x20);
  90. if (state->enabled) {
  91. if (!pwm_is_enabled(pwm)) {
  92. /*
  93. * The clock was enabled above. Just enable
  94. * the channel in the control register.
  95. */
  96. writel(1 << pwm->hwpwm, mxs->base + PWM_CTRL + SET);
  97. }
  98. } else {
  99. clk_disable_unprepare(mxs->clk);
  100. }
  101. return 0;
  102. }
  103. static const struct pwm_ops mxs_pwm_ops = {
  104. .apply = mxs_pwm_apply,
  105. };
  106. static int mxs_pwm_probe(struct platform_device *pdev)
  107. {
  108. struct device_node *np = pdev->dev.of_node;
  109. struct pwm_chip *chip;
  110. struct mxs_pwm_chip *mxs;
  111. u32 npwm;
  112. int ret;
  113. ret = of_property_read_u32(np, "fsl,pwm-number", &npwm);
  114. if (ret < 0) {
  115. dev_err(&pdev->dev, "failed to get pwm number: %d\n", ret);
  116. return ret;
  117. }
  118. chip = devm_pwmchip_alloc(&pdev->dev, npwm, sizeof(*mxs));
  119. if (IS_ERR(chip))
  120. return PTR_ERR(chip);
  121. mxs = to_mxs_pwm_chip(chip);
  122. mxs->base = devm_platform_ioremap_resource(pdev, 0);
  123. if (IS_ERR(mxs->base))
  124. return PTR_ERR(mxs->base);
  125. mxs->clk = devm_clk_get(&pdev->dev, NULL);
  126. if (IS_ERR(mxs->clk))
  127. return PTR_ERR(mxs->clk);
  128. chip->ops = &mxs_pwm_ops;
  129. /* FIXME: Only do this if the PWM isn't already running */
  130. ret = stmp_reset_block(mxs->base);
  131. if (ret)
  132. return dev_err_probe(&pdev->dev, ret, "failed to reset PWM\n");
  133. ret = devm_pwmchip_add(&pdev->dev, chip);
  134. if (ret < 0) {
  135. dev_err(&pdev->dev, "failed to add pwm chip %d\n", ret);
  136. return ret;
  137. }
  138. return 0;
  139. }
  140. static const struct of_device_id mxs_pwm_dt_ids[] = {
  141. { .compatible = "fsl,imx23-pwm", },
  142. { /* sentinel */ }
  143. };
  144. MODULE_DEVICE_TABLE(of, mxs_pwm_dt_ids);
  145. static struct platform_driver mxs_pwm_driver = {
  146. .driver = {
  147. .name = "mxs-pwm",
  148. .of_match_table = mxs_pwm_dt_ids,
  149. },
  150. .probe = mxs_pwm_probe,
  151. };
  152. module_platform_driver(mxs_pwm_driver);
  153. MODULE_ALIAS("platform:mxs-pwm");
  154. MODULE_AUTHOR("Shawn Guo <shawn.guo@linaro.org>");
  155. MODULE_DESCRIPTION("Freescale MXS PWM Driver");
  156. MODULE_LICENSE("GPL v2");