pwm-sl28cpld.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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/pwm.h>
  41. #include <linux/regmap.h>
  42. /*
  43. * PWM timer block registers.
  44. */
  45. #define SL28CPLD_PWM_CTRL 0x00
  46. #define SL28CPLD_PWM_CTRL_ENABLE BIT(7)
  47. #define SL28CPLD_PWM_CTRL_PRESCALER_MASK GENMASK(1, 0)
  48. #define SL28CPLD_PWM_CYCLE 0x01
  49. #define SL28CPLD_PWM_CYCLE_MAX GENMASK(6, 0)
  50. #define SL28CPLD_PWM_CLK 32000 /* 32 kHz */
  51. #define SL28CPLD_PWM_MAX_DUTY_CYCLE(prescaler) (1 << (7 - (prescaler)))
  52. #define SL28CPLD_PWM_PERIOD(prescaler) \
  53. (NSEC_PER_SEC / SL28CPLD_PWM_CLK * SL28CPLD_PWM_MAX_DUTY_CYCLE(prescaler))
  54. /*
  55. * We calculate the duty cycle like this:
  56. * duty_cycle_ns = pwm_cycle_reg * max_period_ns / max_duty_cycle
  57. *
  58. * With
  59. * max_period_ns = 1 << (7 - prescaler) / SL28CPLD_PWM_CLK * NSEC_PER_SEC
  60. * max_duty_cycle = 1 << (7 - prescaler)
  61. * this then simplifies to:
  62. * duty_cycle_ns = pwm_cycle_reg / SL28CPLD_PWM_CLK * NSEC_PER_SEC
  63. * = NSEC_PER_SEC / SL28CPLD_PWM_CLK * pwm_cycle_reg
  64. *
  65. * NSEC_PER_SEC is a multiple of SL28CPLD_PWM_CLK, therefore we're not losing
  66. * precision by doing the divison first.
  67. */
  68. #define SL28CPLD_PWM_TO_DUTY_CYCLE(reg) \
  69. (NSEC_PER_SEC / SL28CPLD_PWM_CLK * (reg))
  70. #define SL28CPLD_PWM_FROM_DUTY_CYCLE(duty_cycle) \
  71. (DIV_ROUND_DOWN_ULL((duty_cycle), NSEC_PER_SEC / SL28CPLD_PWM_CLK))
  72. #define sl28cpld_pwm_read(priv, reg, val) \
  73. regmap_read((priv)->regmap, (priv)->offset + (reg), (val))
  74. #define sl28cpld_pwm_write(priv, reg, val) \
  75. regmap_write((priv)->regmap, (priv)->offset + (reg), (val))
  76. struct sl28cpld_pwm {
  77. struct pwm_chip pwm_chip;
  78. struct regmap *regmap;
  79. u32 offset;
  80. };
  81. #define sl28cpld_pwm_from_chip(_chip) \
  82. container_of(_chip, struct sl28cpld_pwm, pwm_chip)
  83. static void sl28cpld_pwm_get_state(struct pwm_chip *chip,
  84. struct pwm_device *pwm,
  85. struct pwm_state *state)
  86. {
  87. struct sl28cpld_pwm *priv = sl28cpld_pwm_from_chip(chip);
  88. unsigned int reg;
  89. int prescaler;
  90. sl28cpld_pwm_read(priv, SL28CPLD_PWM_CTRL, &reg);
  91. state->enabled = reg & SL28CPLD_PWM_CTRL_ENABLE;
  92. prescaler = FIELD_GET(SL28CPLD_PWM_CTRL_PRESCALER_MASK, reg);
  93. state->period = SL28CPLD_PWM_PERIOD(prescaler);
  94. sl28cpld_pwm_read(priv, SL28CPLD_PWM_CYCLE, &reg);
  95. state->duty_cycle = SL28CPLD_PWM_TO_DUTY_CYCLE(reg);
  96. state->polarity = PWM_POLARITY_NORMAL;
  97. /*
  98. * Sanitize values for the PWM core. Depending on the prescaler it
  99. * might happen that we calculate a duty_cycle greater than the actual
  100. * period. This might happen if someone (e.g. the bootloader) sets an
  101. * invalid combination of values. The behavior of the hardware is
  102. * undefined in this case. But we need to report sane values back to
  103. * the PWM core.
  104. */
  105. state->duty_cycle = min(state->duty_cycle, state->period);
  106. }
  107. static int sl28cpld_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm,
  108. const struct pwm_state *state)
  109. {
  110. struct sl28cpld_pwm *priv = sl28cpld_pwm_from_chip(chip);
  111. unsigned int cycle, prescaler;
  112. bool write_duty_cycle_first;
  113. int ret;
  114. u8 ctrl;
  115. /* Polarity inversion is not supported */
  116. if (state->polarity != PWM_POLARITY_NORMAL)
  117. return -EINVAL;
  118. /*
  119. * Calculate the prescaler. Pick the biggest period that isn't
  120. * bigger than the requested period.
  121. */
  122. prescaler = DIV_ROUND_UP_ULL(SL28CPLD_PWM_PERIOD(0), state->period);
  123. prescaler = order_base_2(prescaler);
  124. if (prescaler > field_max(SL28CPLD_PWM_CTRL_PRESCALER_MASK))
  125. return -ERANGE;
  126. ctrl = FIELD_PREP(SL28CPLD_PWM_CTRL_PRESCALER_MASK, prescaler);
  127. if (state->enabled)
  128. ctrl |= SL28CPLD_PWM_CTRL_ENABLE;
  129. cycle = SL28CPLD_PWM_FROM_DUTY_CYCLE(state->duty_cycle);
  130. cycle = min_t(unsigned int, cycle, SL28CPLD_PWM_MAX_DUTY_CYCLE(prescaler));
  131. /*
  132. * Work around the hardware limitation. See also above. Trap 100% duty
  133. * cycle if the prescaler is 0. Set prescaler to 1 instead. We don't
  134. * care about the frequency because its "all-one" in either case.
  135. *
  136. * We don't need to check the actual prescaler setting, because only
  137. * if the prescaler is 0 we can have this particular value.
  138. */
  139. if (cycle == SL28CPLD_PWM_MAX_DUTY_CYCLE(0)) {
  140. ctrl &= ~SL28CPLD_PWM_CTRL_PRESCALER_MASK;
  141. ctrl |= FIELD_PREP(SL28CPLD_PWM_CTRL_PRESCALER_MASK, 1);
  142. cycle = SL28CPLD_PWM_MAX_DUTY_CYCLE(1);
  143. }
  144. /*
  145. * To avoid glitches when we switch the prescaler, we have to make sure
  146. * we have a valid duty cycle for the new mode.
  147. *
  148. * Take the current prescaler (or the current period length) into
  149. * account to decide whether we have to write the duty cycle or the new
  150. * prescaler first. If the period length is decreasing we have to
  151. * write the duty cycle first.
  152. */
  153. write_duty_cycle_first = pwm->state.period > state->period;
  154. if (write_duty_cycle_first) {
  155. ret = sl28cpld_pwm_write(priv, SL28CPLD_PWM_CYCLE, cycle);
  156. if (ret)
  157. return ret;
  158. }
  159. ret = sl28cpld_pwm_write(priv, SL28CPLD_PWM_CTRL, ctrl);
  160. if (ret)
  161. return ret;
  162. if (!write_duty_cycle_first) {
  163. ret = sl28cpld_pwm_write(priv, SL28CPLD_PWM_CYCLE, cycle);
  164. if (ret)
  165. return ret;
  166. }
  167. return 0;
  168. }
  169. static const struct pwm_ops sl28cpld_pwm_ops = {
  170. .apply = sl28cpld_pwm_apply,
  171. .get_state = sl28cpld_pwm_get_state,
  172. .owner = THIS_MODULE,
  173. };
  174. static int sl28cpld_pwm_probe(struct platform_device *pdev)
  175. {
  176. struct sl28cpld_pwm *priv;
  177. struct pwm_chip *chip;
  178. int ret;
  179. if (!pdev->dev.parent) {
  180. dev_err(&pdev->dev, "no parent device\n");
  181. return -ENODEV;
  182. }
  183. priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
  184. if (!priv)
  185. return -ENOMEM;
  186. priv->regmap = dev_get_regmap(pdev->dev.parent, NULL);
  187. if (!priv->regmap) {
  188. dev_err(&pdev->dev, "could not get parent regmap\n");
  189. return -ENODEV;
  190. }
  191. ret = device_property_read_u32(&pdev->dev, "reg", &priv->offset);
  192. if (ret) {
  193. dev_err(&pdev->dev, "no 'reg' property found (%pe)\n",
  194. ERR_PTR(ret));
  195. return -EINVAL;
  196. }
  197. /* Initialize the pwm_chip structure */
  198. chip = &priv->pwm_chip;
  199. chip->dev = &pdev->dev;
  200. chip->ops = &sl28cpld_pwm_ops;
  201. chip->base = -1;
  202. chip->npwm = 1;
  203. ret = pwmchip_add(&priv->pwm_chip);
  204. if (ret) {
  205. dev_err(&pdev->dev, "failed to add PWM chip (%pe)",
  206. ERR_PTR(ret));
  207. return ret;
  208. }
  209. platform_set_drvdata(pdev, priv);
  210. return 0;
  211. }
  212. static int sl28cpld_pwm_remove(struct platform_device *pdev)
  213. {
  214. struct sl28cpld_pwm *priv = platform_get_drvdata(pdev);
  215. return pwmchip_remove(&priv->pwm_chip);
  216. }
  217. static const struct of_device_id sl28cpld_pwm_of_match[] = {
  218. { .compatible = "kontron,sl28cpld-pwm" },
  219. {}
  220. };
  221. MODULE_DEVICE_TABLE(of, sl28cpld_pwm_of_match);
  222. static struct platform_driver sl28cpld_pwm_driver = {
  223. .probe = sl28cpld_pwm_probe,
  224. .remove = sl28cpld_pwm_remove,
  225. .driver = {
  226. .name = "sl28cpld-pwm",
  227. .of_match_table = sl28cpld_pwm_of_match,
  228. },
  229. };
  230. module_platform_driver(sl28cpld_pwm_driver);
  231. MODULE_DESCRIPTION("sl28cpld PWM Driver");
  232. MODULE_AUTHOR("Michael Walle <michael@walle.cc>");
  233. MODULE_LICENSE("GPL");