pwm-mxs.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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 pwm_chip chip;
  36. struct clk *clk;
  37. void __iomem *base;
  38. };
  39. #define to_mxs_pwm_chip(_chip) container_of(_chip, struct mxs_pwm_chip, chip)
  40. static int mxs_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm,
  41. const struct pwm_state *state)
  42. {
  43. struct mxs_pwm_chip *mxs = to_mxs_pwm_chip(chip);
  44. int ret, div = 0;
  45. unsigned int period_cycles, duty_cycles;
  46. unsigned long rate;
  47. unsigned long long c;
  48. unsigned int pol_bits;
  49. /*
  50. * If the PWM channel is disabled, make sure to turn on the
  51. * clock before calling clk_get_rate() and writing to the
  52. * registers. Otherwise, just keep it enabled.
  53. */
  54. if (!pwm_is_enabled(pwm)) {
  55. ret = clk_prepare_enable(mxs->clk);
  56. if (ret)
  57. return ret;
  58. }
  59. if (!state->enabled && pwm_is_enabled(pwm))
  60. writel(1 << pwm->hwpwm, mxs->base + PWM_CTRL + CLR);
  61. rate = clk_get_rate(mxs->clk);
  62. while (1) {
  63. c = rate >> cdiv_shift[div];
  64. c = c * state->period;
  65. do_div(c, 1000000000);
  66. if (c < PERIOD_PERIOD_MAX)
  67. break;
  68. div++;
  69. if (div >= PERIOD_CDIV_MAX)
  70. return -EINVAL;
  71. }
  72. period_cycles = c;
  73. c *= state->duty_cycle;
  74. do_div(c, state->period);
  75. duty_cycles = c;
  76. /*
  77. * The data sheet the says registers must be written to in
  78. * this order (ACTIVEn, then PERIODn). Also, the new settings
  79. * only take effect at the beginning of a new period, avoiding
  80. * glitches.
  81. */
  82. pol_bits = state->polarity == PWM_POLARITY_NORMAL ?
  83. PERIOD_POLARITY_NORMAL : PERIOD_POLARITY_INVERSE;
  84. writel(duty_cycles << 16,
  85. mxs->base + PWM_ACTIVE0 + pwm->hwpwm * 0x20);
  86. writel(PERIOD_PERIOD(period_cycles) | pol_bits | PERIOD_CDIV(div),
  87. mxs->base + PWM_PERIOD0 + pwm->hwpwm * 0x20);
  88. if (state->enabled) {
  89. if (!pwm_is_enabled(pwm)) {
  90. /*
  91. * The clock was enabled above. Just enable
  92. * the channel in the control register.
  93. */
  94. writel(1 << pwm->hwpwm, mxs->base + PWM_CTRL + SET);
  95. }
  96. } else {
  97. clk_disable_unprepare(mxs->clk);
  98. }
  99. return 0;
  100. }
  101. static const struct pwm_ops mxs_pwm_ops = {
  102. .apply = mxs_pwm_apply,
  103. .owner = THIS_MODULE,
  104. };
  105. static int mxs_pwm_probe(struct platform_device *pdev)
  106. {
  107. struct device_node *np = pdev->dev.of_node;
  108. struct mxs_pwm_chip *mxs;
  109. int ret;
  110. mxs = devm_kzalloc(&pdev->dev, sizeof(*mxs), GFP_KERNEL);
  111. if (!mxs)
  112. return -ENOMEM;
  113. mxs->base = devm_platform_ioremap_resource(pdev, 0);
  114. if (IS_ERR(mxs->base))
  115. return PTR_ERR(mxs->base);
  116. mxs->clk = devm_clk_get(&pdev->dev, NULL);
  117. if (IS_ERR(mxs->clk))
  118. return PTR_ERR(mxs->clk);
  119. mxs->chip.dev = &pdev->dev;
  120. mxs->chip.ops = &mxs_pwm_ops;
  121. ret = of_property_read_u32(np, "fsl,pwm-number", &mxs->chip.npwm);
  122. if (ret < 0) {
  123. dev_err(&pdev->dev, "failed to get pwm number: %d\n", ret);
  124. return ret;
  125. }
  126. /* FIXME: Only do this if the PWM isn't already running */
  127. ret = stmp_reset_block(mxs->base);
  128. if (ret)
  129. return dev_err_probe(&pdev->dev, ret, "failed to reset PWM\n");
  130. ret = devm_pwmchip_add(&pdev->dev, &mxs->chip);
  131. if (ret < 0) {
  132. dev_err(&pdev->dev, "failed to add pwm chip %d\n", ret);
  133. return ret;
  134. }
  135. return 0;
  136. }
  137. static const struct of_device_id mxs_pwm_dt_ids[] = {
  138. { .compatible = "fsl,imx23-pwm", },
  139. { /* sentinel */ }
  140. };
  141. MODULE_DEVICE_TABLE(of, mxs_pwm_dt_ids);
  142. static struct platform_driver mxs_pwm_driver = {
  143. .driver = {
  144. .name = "mxs-pwm",
  145. .of_match_table = mxs_pwm_dt_ids,
  146. },
  147. .probe = mxs_pwm_probe,
  148. };
  149. module_platform_driver(mxs_pwm_driver);
  150. MODULE_ALIAS("platform:mxs-pwm");
  151. MODULE_AUTHOR("Shawn Guo <[email protected]>");
  152. MODULE_DESCRIPTION("Freescale MXS PWM Driver");
  153. MODULE_LICENSE("GPL v2");