pwm-sl28cpld.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * sl28cpld PWM driver
  4. *
  5. * Copyright (c) 2020 Michael Walle <michael@walle.cc>
  6. *
  7. * There is no public datasheet available for this PWM core. But it is easy
  8. * enough to be briefly explained. It consists of one 8-bit counter. The PWM
  9. * supports four distinct frequencies by selecting when to reset the counter.
  10. * With the prescaler setting you can select which bit of the counter is used
  11. * to reset it. This implies that the higher the frequency the less remaining
  12. * bits are available for the actual counter.
  13. *
  14. * Let cnt[7:0] be the counter, clocked at 32kHz:
  15. * +-----------+--------+--------------+-----------+---------------+
  16. * | prescaler | reset | counter bits | frequency | period length |
  17. * +-----------+--------+--------------+-----------+---------------+
  18. * | 0 | cnt[7] | cnt[6:0] | 250 Hz | 4000000 ns |
  19. * | 1 | cnt[6] | cnt[5:0] | 500 Hz | 2000000 ns |
  20. * | 2 | cnt[5] | cnt[4:0] | 1 kHz | 1000000 ns |
  21. * | 3 | cnt[4] | cnt[3:0] | 2 kHz | 500000 ns |
  22. * +-----------+--------+--------------+-----------+---------------+
  23. *
  24. * Limitations:
  25. * - The hardware cannot generate a 100% duty cycle if the prescaler is 0.
  26. * - The hardware cannot atomically set the prescaler and the counter value,
  27. * which might lead to glitches and inconsistent states if a write fails.
  28. * - The counter is not reset if you switch the prescaler which leads
  29. * to glitches, too.
  30. * - The duty cycle will switch immediately and not after a complete cycle.
  31. * - Depending on the actual implementation, disabling the PWM might have
  32. * side effects. For example, if the output pin is shared with a GPIO pin
  33. * it will automatically switch back to GPIO mode.
  34. */
  35. #include <linux/bitfield.h>
  36. #include <linux/kernel.h>
  37. #include <linux/mod_devicetable.h>
  38. #include <linux/module.h>
  39. #include <linux/platform_device.h>
  40. #include <linux/property.h>
  41. #include <linux/pwm.h>
  42. #include <linux/regmap.h>
  43. /*
  44. * PWM timer block registers.
  45. */
  46. #define SL28CPLD_PWM_CTRL 0x00
  47. #define SL28CPLD_PWM_CTRL_ENABLE BIT(7)
  48. #define SL28CPLD_PWM_CTRL_PRESCALER_MASK GENMASK(1, 0)
  49. #define SL28CPLD_PWM_CYCLE 0x01
  50. #define SL28CPLD_PWM_CYCLE_MAX GENMASK(6, 0)
  51. #define SL28CPLD_PWM_CLK 32000 /* 32 kHz */
  52. #define SL28CPLD_PWM_MAX_DUTY_CYCLE(prescaler) (1 << (7 - (prescaler)))
  53. #define SL28CPLD_PWM_PERIOD(prescaler) \
  54. (NSEC_PER_SEC / SL28CPLD_PWM_CLK * SL28CPLD_PWM_MAX_DUTY_CYCLE(prescaler))
  55. /*
  56. * We calculate the duty cycle like this:
  57. * duty_cycle_ns = pwm_cycle_reg * max_period_ns / max_duty_cycle
  58. *
  59. * With
  60. * max_period_ns = 1 << (7 - prescaler) / SL28CPLD_PWM_CLK * NSEC_PER_SEC
  61. * max_duty_cycle = 1 << (7 - prescaler)
  62. * this then simplifies to:
  63. * duty_cycle_ns = pwm_cycle_reg / SL28CPLD_PWM_CLK * NSEC_PER_SEC
  64. * = NSEC_PER_SEC / SL28CPLD_PWM_CLK * pwm_cycle_reg
  65. *
  66. * NSEC_PER_SEC is a multiple of SL28CPLD_PWM_CLK, therefore we're not losing
  67. * precision by doing the divison first.
  68. */
  69. #define SL28CPLD_PWM_TO_DUTY_CYCLE(reg) \
  70. (NSEC_PER_SEC / SL28CPLD_PWM_CLK * (reg))
  71. #define SL28CPLD_PWM_FROM_DUTY_CYCLE(duty_cycle) \
  72. (DIV_ROUND_DOWN_ULL((duty_cycle), NSEC_PER_SEC / SL28CPLD_PWM_CLK))
  73. #define sl28cpld_pwm_read(priv, reg, val) \
  74. regmap_read((priv)->regmap, (priv)->offset + (reg), (val))
  75. #define sl28cpld_pwm_write(priv, reg, val) \
  76. regmap_write((priv)->regmap, (priv)->offset + (reg), (val))
  77. struct sl28cpld_pwm {
  78. struct regmap *regmap;
  79. u32 offset;
  80. };
  81. static inline struct sl28cpld_pwm *sl28cpld_pwm_from_chip(struct pwm_chip *chip)
  82. {
  83. return pwmchip_get_drvdata(chip);
  84. }
  85. static int sl28cpld_pwm_get_state(struct pwm_chip *chip,
  86. struct pwm_device *pwm,
  87. struct pwm_state *state)
  88. {
  89. struct sl28cpld_pwm *priv = sl28cpld_pwm_from_chip(chip);
  90. unsigned int reg;
  91. int prescaler;
  92. sl28cpld_pwm_read(priv, SL28CPLD_PWM_CTRL, &reg);
  93. state->enabled = reg & SL28CPLD_PWM_CTRL_ENABLE;
  94. prescaler = FIELD_GET(SL28CPLD_PWM_CTRL_PRESCALER_MASK, reg);
  95. state->period = SL28CPLD_PWM_PERIOD(prescaler);
  96. sl28cpld_pwm_read(priv, SL28CPLD_PWM_CYCLE, &reg);
  97. state->duty_cycle = SL28CPLD_PWM_TO_DUTY_CYCLE(reg);
  98. state->polarity = PWM_POLARITY_NORMAL;
  99. /*
  100. * Sanitize values for the PWM core. Depending on the prescaler it
  101. * might happen that we calculate a duty_cycle greater than the actual
  102. * period. This might happen if someone (e.g. the bootloader) sets an
  103. * invalid combination of values. The behavior of the hardware is
  104. * undefined in this case. But we need to report sane values back to
  105. * the PWM core.
  106. */
  107. state->duty_cycle = min(state->duty_cycle, state->period);
  108. return 0;
  109. }
  110. static int sl28cpld_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm,
  111. const struct pwm_state *state)
  112. {
  113. struct sl28cpld_pwm *priv = sl28cpld_pwm_from_chip(chip);
  114. unsigned int cycle, prescaler;
  115. bool write_duty_cycle_first;
  116. int ret;
  117. u8 ctrl;
  118. /* Polarity inversion is not supported */
  119. if (state->polarity != PWM_POLARITY_NORMAL)
  120. return -EINVAL;
  121. /*
  122. * Calculate the prescaler. Pick the biggest period that isn't
  123. * bigger than the requested period.
  124. */
  125. prescaler = DIV_ROUND_UP_ULL(SL28CPLD_PWM_PERIOD(0), state->period);
  126. prescaler = order_base_2(prescaler);
  127. if (prescaler > field_max(SL28CPLD_PWM_CTRL_PRESCALER_MASK))
  128. return -ERANGE;
  129. ctrl = FIELD_PREP(SL28CPLD_PWM_CTRL_PRESCALER_MASK, prescaler);
  130. if (state->enabled)
  131. ctrl |= SL28CPLD_PWM_CTRL_ENABLE;
  132. cycle = SL28CPLD_PWM_FROM_DUTY_CYCLE(state->duty_cycle);
  133. cycle = min_t(unsigned int, cycle, SL28CPLD_PWM_MAX_DUTY_CYCLE(prescaler));
  134. /*
  135. * Work around the hardware limitation. See also above. Trap 100% duty
  136. * cycle if the prescaler is 0. Set prescaler to 1 instead. We don't
  137. * care about the frequency because its "all-one" in either case.
  138. *
  139. * We don't need to check the actual prescaler setting, because only
  140. * if the prescaler is 0 we can have this particular value.
  141. */
  142. if (cycle == SL28CPLD_PWM_MAX_DUTY_CYCLE(0)) {
  143. ctrl &= ~SL28CPLD_PWM_CTRL_PRESCALER_MASK;
  144. ctrl |= FIELD_PREP(SL28CPLD_PWM_CTRL_PRESCALER_MASK, 1);
  145. cycle = SL28CPLD_PWM_MAX_DUTY_CYCLE(1);
  146. }
  147. /*
  148. * To avoid glitches when we switch the prescaler, we have to make sure
  149. * we have a valid duty cycle for the new mode.
  150. *
  151. * Take the current prescaler (or the current period length) into
  152. * account to decide whether we have to write the duty cycle or the new
  153. * prescaler first. If the period length is decreasing we have to
  154. * write the duty cycle first.
  155. */
  156. write_duty_cycle_first = pwm->state.period > state->period;
  157. if (write_duty_cycle_first) {
  158. ret = sl28cpld_pwm_write(priv, SL28CPLD_PWM_CYCLE, cycle);
  159. if (ret)
  160. return ret;
  161. }
  162. ret = sl28cpld_pwm_write(priv, SL28CPLD_PWM_CTRL, ctrl);
  163. if (ret)
  164. return ret;
  165. if (!write_duty_cycle_first) {
  166. ret = sl28cpld_pwm_write(priv, SL28CPLD_PWM_CYCLE, cycle);
  167. if (ret)
  168. return ret;
  169. }
  170. return 0;
  171. }
  172. static const struct pwm_ops sl28cpld_pwm_ops = {
  173. .apply = sl28cpld_pwm_apply,
  174. .get_state = sl28cpld_pwm_get_state,
  175. };
  176. static int sl28cpld_pwm_probe(struct platform_device *pdev)
  177. {
  178. struct sl28cpld_pwm *priv;
  179. struct pwm_chip *chip;
  180. int ret;
  181. if (!pdev->dev.parent) {
  182. dev_err(&pdev->dev, "no parent device\n");
  183. return -ENODEV;
  184. }
  185. chip = devm_pwmchip_alloc(&pdev->dev, 1, sizeof(*priv));
  186. if (IS_ERR(chip))
  187. return PTR_ERR(chip);
  188. priv = sl28cpld_pwm_from_chip(chip);
  189. priv->regmap = dev_get_regmap(pdev->dev.parent, NULL);
  190. if (!priv->regmap) {
  191. dev_err(&pdev->dev, "could not get parent regmap\n");
  192. return -ENODEV;
  193. }
  194. ret = device_property_read_u32(&pdev->dev, "reg", &priv->offset);
  195. if (ret) {
  196. dev_err(&pdev->dev, "no 'reg' property found (%pe)\n",
  197. ERR_PTR(ret));
  198. return -EINVAL;
  199. }
  200. /* Initialize the pwm_chip structure */
  201. chip->ops = &sl28cpld_pwm_ops;
  202. ret = devm_pwmchip_add(&pdev->dev, chip);
  203. if (ret) {
  204. dev_err(&pdev->dev, "failed to add PWM chip (%pe)",
  205. ERR_PTR(ret));
  206. return ret;
  207. }
  208. return 0;
  209. }
  210. static const struct of_device_id sl28cpld_pwm_of_match[] = {
  211. { .compatible = "kontron,sl28cpld-pwm" },
  212. {}
  213. };
  214. MODULE_DEVICE_TABLE(of, sl28cpld_pwm_of_match);
  215. static struct platform_driver sl28cpld_pwm_driver = {
  216. .probe = sl28cpld_pwm_probe,
  217. .driver = {
  218. .name = "sl28cpld-pwm",
  219. .of_match_table = sl28cpld_pwm_of_match,
  220. },
  221. };
  222. module_platform_driver(sl28cpld_pwm_driver);
  223. MODULE_DESCRIPTION("sl28cpld PWM Driver");
  224. MODULE_AUTHOR("Michael Walle <michael@walle.cc>");
  225. MODULE_LICENSE("GPL");