pwm-visconti.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Toshiba Visconti pulse-width-modulation controller driver
  4. *
  5. * Copyright (c) 2020 - 2021 TOSHIBA CORPORATION
  6. * Copyright (c) 2020 - 2021 Toshiba Electronic Devices & Storage Corporation
  7. *
  8. * Authors: Nobuhiro Iwamatsu <nobuhiro1.iwamatsu@toshiba.co.jp>
  9. *
  10. * Limitations:
  11. * - The fixed input clock is running at 1 MHz and is divided by either 1,
  12. * 2, 4 or 8.
  13. * - When the settings of the PWM are modified, the new values are shadowed
  14. * in hardware until the PIPGM_PCSR register is written and the currently
  15. * running period is completed. This way the hardware switches atomically
  16. * from the old setting to the new.
  17. * - Disabling the hardware completes the currently running period and keeps
  18. * the output at low level at all times.
  19. */
  20. #include <linux/err.h>
  21. #include <linux/io.h>
  22. #include <linux/module.h>
  23. #include <linux/of.h>
  24. #include <linux/platform_device.h>
  25. #include <linux/pwm.h>
  26. #define PIPGM_PCSR(ch) (0x400 + 4 * (ch))
  27. #define PIPGM_PDUT(ch) (0x420 + 4 * (ch))
  28. #define PIPGM_PWMC(ch) (0x440 + 4 * (ch))
  29. #define PIPGM_PWMC_PWMACT BIT(5)
  30. #define PIPGM_PWMC_CLK_MASK GENMASK(1, 0)
  31. #define PIPGM_PWMC_POLARITY_MASK GENMASK(5, 5)
  32. struct visconti_pwm_chip {
  33. void __iomem *base;
  34. };
  35. static inline struct visconti_pwm_chip *visconti_pwm_from_chip(struct pwm_chip *chip)
  36. {
  37. return pwmchip_get_drvdata(chip);
  38. }
  39. static int visconti_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm,
  40. const struct pwm_state *state)
  41. {
  42. struct visconti_pwm_chip *priv = visconti_pwm_from_chip(chip);
  43. u32 period, duty_cycle, pwmc0;
  44. if (!state->enabled) {
  45. writel(0, priv->base + PIPGM_PCSR(pwm->hwpwm));
  46. return 0;
  47. }
  48. /*
  49. * The biggest period the hardware can provide is
  50. * (0xffff << 3) * 1000 ns
  51. * This value fits easily in an u32, so simplify the maths by
  52. * capping the values to 32 bit integers.
  53. */
  54. if (state->period > (0xffff << 3) * 1000)
  55. period = (0xffff << 3) * 1000;
  56. else
  57. period = state->period;
  58. if (state->duty_cycle > period)
  59. duty_cycle = period;
  60. else
  61. duty_cycle = state->duty_cycle;
  62. /*
  63. * The input clock runs fixed at 1 MHz, so we have only
  64. * microsecond resolution and so can divide by
  65. * NSEC_PER_SEC / CLKFREQ = 1000 without losing precision.
  66. */
  67. period /= 1000;
  68. duty_cycle /= 1000;
  69. if (!period)
  70. return -ERANGE;
  71. /*
  72. * PWMC controls a divider that divides the input clk by a power of two
  73. * between 1 and 8. As a smaller divider yields higher precision, pick
  74. * the smallest possible one. As period is at most 0xffff << 3, pwmc0 is
  75. * in the intended range [0..3].
  76. */
  77. pwmc0 = fls(period >> 16);
  78. if (WARN_ON(pwmc0 > 3))
  79. return -EINVAL;
  80. period >>= pwmc0;
  81. duty_cycle >>= pwmc0;
  82. if (state->polarity == PWM_POLARITY_INVERSED)
  83. pwmc0 |= PIPGM_PWMC_PWMACT;
  84. writel(pwmc0, priv->base + PIPGM_PWMC(pwm->hwpwm));
  85. writel(duty_cycle, priv->base + PIPGM_PDUT(pwm->hwpwm));
  86. writel(period, priv->base + PIPGM_PCSR(pwm->hwpwm));
  87. return 0;
  88. }
  89. static int visconti_pwm_get_state(struct pwm_chip *chip, struct pwm_device *pwm,
  90. struct pwm_state *state)
  91. {
  92. struct visconti_pwm_chip *priv = visconti_pwm_from_chip(chip);
  93. u32 period, duty, pwmc0, pwmc0_clk;
  94. period = readl(priv->base + PIPGM_PCSR(pwm->hwpwm));
  95. duty = readl(priv->base + PIPGM_PDUT(pwm->hwpwm));
  96. pwmc0 = readl(priv->base + PIPGM_PWMC(pwm->hwpwm));
  97. pwmc0_clk = pwmc0 & PIPGM_PWMC_CLK_MASK;
  98. state->period = (period << pwmc0_clk) * NSEC_PER_USEC;
  99. state->duty_cycle = (duty << pwmc0_clk) * NSEC_PER_USEC;
  100. if (pwmc0 & PIPGM_PWMC_POLARITY_MASK)
  101. state->polarity = PWM_POLARITY_INVERSED;
  102. else
  103. state->polarity = PWM_POLARITY_NORMAL;
  104. state->enabled = true;
  105. return 0;
  106. }
  107. static const struct pwm_ops visconti_pwm_ops = {
  108. .apply = visconti_pwm_apply,
  109. .get_state = visconti_pwm_get_state,
  110. };
  111. static int visconti_pwm_probe(struct platform_device *pdev)
  112. {
  113. struct device *dev = &pdev->dev;
  114. struct pwm_chip *chip;
  115. struct visconti_pwm_chip *priv;
  116. int ret;
  117. chip = devm_pwmchip_alloc(dev, 4, sizeof(*priv));
  118. if (IS_ERR(chip))
  119. return PTR_ERR(chip);
  120. priv = visconti_pwm_from_chip(chip);
  121. priv->base = devm_platform_ioremap_resource(pdev, 0);
  122. if (IS_ERR(priv->base))
  123. return PTR_ERR(priv->base);
  124. chip->ops = &visconti_pwm_ops;
  125. ret = devm_pwmchip_add(&pdev->dev, chip);
  126. if (ret < 0)
  127. return dev_err_probe(&pdev->dev, ret, "Cannot register visconti PWM\n");
  128. return 0;
  129. }
  130. static const struct of_device_id visconti_pwm_of_match[] = {
  131. { .compatible = "toshiba,visconti-pwm", },
  132. { }
  133. };
  134. MODULE_DEVICE_TABLE(of, visconti_pwm_of_match);
  135. static struct platform_driver visconti_pwm_driver = {
  136. .driver = {
  137. .name = "pwm-visconti",
  138. .of_match_table = visconti_pwm_of_match,
  139. },
  140. .probe = visconti_pwm_probe,
  141. };
  142. module_platform_driver(visconti_pwm_driver);
  143. MODULE_DESCRIPTION("Toshiba Visconti Pulse Width Modulator driver");
  144. MODULE_LICENSE("GPL v2");
  145. MODULE_AUTHOR("Nobuhiro Iwamatsu <nobuhiro1.iwamatsu@toshiba.co.jp>");
  146. MODULE_ALIAS("platform:pwm-visconti");