pwm-sophgo-sg2042.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Sophgo SG2042 PWM Controller Driver
  4. *
  5. * Copyright (C) 2024 Sophgo Technology Inc.
  6. * Copyright (C) 2024 Chen Wang <unicorn_wang@outlook.com>
  7. *
  8. * Limitations:
  9. * - After reset, the output of the PWM channel is always high.
  10. * The value of HLPERIOD/PERIOD is 0.
  11. * - When HLPERIOD or PERIOD is reconfigured, PWM will start to
  12. * output waveforms with the new configuration after completing
  13. * the running period.
  14. * - When PERIOD and HLPERIOD is set to 0, the PWM wave output will
  15. * be stopped and the output is pulled to high.
  16. * - SG2044 supports both polarities, SG2042 only normal polarity.
  17. * See the datasheet [1] for more details.
  18. * [1]:https://github.com/sophgo/sophgo-doc/tree/main/SG2042/TRM
  19. */
  20. #include <linux/clk.h>
  21. #include <linux/err.h>
  22. #include <linux/io.h>
  23. #include <linux/math64.h>
  24. #include <linux/module.h>
  25. #include <linux/platform_device.h>
  26. #include <linux/pwm.h>
  27. #include <linux/reset.h>
  28. /*
  29. * Offset RegisterName
  30. * 0x0000 HLPERIOD0
  31. * 0x0004 PERIOD0
  32. * 0x0008 HLPERIOD1
  33. * 0x000C PERIOD1
  34. * 0x0010 HLPERIOD2
  35. * 0x0014 PERIOD2
  36. * 0x0018 HLPERIOD3
  37. * 0x001C PERIOD3
  38. * Four groups and every group is composed of HLPERIOD & PERIOD
  39. */
  40. #define SG2042_PWM_HLPERIOD(chan) ((chan) * 8 + 0)
  41. #define SG2042_PWM_PERIOD(chan) ((chan) * 8 + 4)
  42. #define SG2044_PWM_POLARITY 0x40
  43. #define SG2044_PWM_PWMSTART 0x44
  44. #define SG2044_PWM_OE 0xd0
  45. #define SG2042_PWM_CHANNELNUM 4
  46. /**
  47. * struct sg2042_pwm_ddata - private driver data
  48. * @base: base address of mapped PWM registers
  49. * @clk_rate_hz: rate of base clock in HZ
  50. */
  51. struct sg2042_pwm_ddata {
  52. void __iomem *base;
  53. unsigned long clk_rate_hz;
  54. };
  55. struct sg2042_chip_data {
  56. const struct pwm_ops ops;
  57. };
  58. /*
  59. * period_ticks: PERIOD
  60. * hlperiod_ticks: HLPERIOD
  61. */
  62. static void pwm_sg2042_config(struct sg2042_pwm_ddata *ddata, unsigned int chan,
  63. u32 period_ticks, u32 hlperiod_ticks)
  64. {
  65. void __iomem *base = ddata->base;
  66. writel(period_ticks, base + SG2042_PWM_PERIOD(chan));
  67. writel(hlperiod_ticks, base + SG2042_PWM_HLPERIOD(chan));
  68. }
  69. static void pwm_sg2042_set_dutycycle(struct pwm_chip *chip, struct pwm_device *pwm,
  70. const struct pwm_state *state)
  71. {
  72. struct sg2042_pwm_ddata *ddata = pwmchip_get_drvdata(chip);
  73. u32 hlperiod_ticks;
  74. u32 period_ticks;
  75. /*
  76. * Duration of High level (duty_cycle) = HLPERIOD x Period_of_input_clk
  77. * Duration of One Cycle (period) = PERIOD x Period_of_input_clk
  78. */
  79. period_ticks = min(mul_u64_u64_div_u64(ddata->clk_rate_hz, state->period, NSEC_PER_SEC), U32_MAX);
  80. hlperiod_ticks = min(mul_u64_u64_div_u64(ddata->clk_rate_hz, state->duty_cycle, NSEC_PER_SEC), U32_MAX);
  81. dev_dbg(pwmchip_parent(chip), "chan[%u]: ENABLE=%u, PERIOD=%u, HLPERIOD=%u, POLARITY=%u\n",
  82. pwm->hwpwm, state->enabled, period_ticks, hlperiod_ticks, state->polarity);
  83. pwm_sg2042_config(ddata, pwm->hwpwm, period_ticks, hlperiod_ticks);
  84. }
  85. static int pwm_sg2042_apply(struct pwm_chip *chip, struct pwm_device *pwm,
  86. const struct pwm_state *state)
  87. {
  88. struct sg2042_pwm_ddata *ddata = pwmchip_get_drvdata(chip);
  89. if (state->polarity == PWM_POLARITY_INVERSED)
  90. return -EINVAL;
  91. if (!state->enabled) {
  92. pwm_sg2042_config(ddata, pwm->hwpwm, 0, 0);
  93. return 0;
  94. }
  95. pwm_sg2042_set_dutycycle(chip, pwm, state);
  96. return 0;
  97. }
  98. static int pwm_sg2042_get_state(struct pwm_chip *chip, struct pwm_device *pwm,
  99. struct pwm_state *state)
  100. {
  101. struct sg2042_pwm_ddata *ddata = pwmchip_get_drvdata(chip);
  102. unsigned int chan = pwm->hwpwm;
  103. u32 hlperiod_ticks;
  104. u32 period_ticks;
  105. period_ticks = readl(ddata->base + SG2042_PWM_PERIOD(chan));
  106. hlperiod_ticks = readl(ddata->base + SG2042_PWM_HLPERIOD(chan));
  107. if (!period_ticks) {
  108. state->enabled = false;
  109. return 0;
  110. }
  111. if (hlperiod_ticks > period_ticks)
  112. hlperiod_ticks = period_ticks;
  113. state->enabled = true;
  114. state->period = DIV_ROUND_UP_ULL((u64)period_ticks * NSEC_PER_SEC, ddata->clk_rate_hz);
  115. state->duty_cycle = DIV_ROUND_UP_ULL((u64)hlperiod_ticks * NSEC_PER_SEC, ddata->clk_rate_hz);
  116. state->polarity = PWM_POLARITY_NORMAL;
  117. return 0;
  118. }
  119. static void pwm_sg2044_set_outputen(struct sg2042_pwm_ddata *ddata, struct pwm_device *pwm,
  120. bool enabled)
  121. {
  122. u32 pwmstart;
  123. pwmstart = readl(ddata->base + SG2044_PWM_PWMSTART);
  124. if (enabled)
  125. pwmstart |= BIT(pwm->hwpwm);
  126. else
  127. pwmstart &= ~BIT(pwm->hwpwm);
  128. writel(pwmstart, ddata->base + SG2044_PWM_PWMSTART);
  129. }
  130. static void pwm_sg2044_set_outputdir(struct sg2042_pwm_ddata *ddata, struct pwm_device *pwm,
  131. bool enabled)
  132. {
  133. u32 pwm_oe;
  134. pwm_oe = readl(ddata->base + SG2044_PWM_OE);
  135. if (enabled)
  136. pwm_oe |= BIT(pwm->hwpwm);
  137. else
  138. pwm_oe &= ~BIT(pwm->hwpwm);
  139. writel(pwm_oe, ddata->base + SG2044_PWM_OE);
  140. }
  141. static void pwm_sg2044_set_polarity(struct sg2042_pwm_ddata *ddata, struct pwm_device *pwm,
  142. const struct pwm_state *state)
  143. {
  144. u32 pwm_polarity;
  145. pwm_polarity = readl(ddata->base + SG2044_PWM_POLARITY);
  146. if (state->polarity == PWM_POLARITY_NORMAL)
  147. pwm_polarity &= ~BIT(pwm->hwpwm);
  148. else
  149. pwm_polarity |= BIT(pwm->hwpwm);
  150. writel(pwm_polarity, ddata->base + SG2044_PWM_POLARITY);
  151. }
  152. static int pwm_sg2044_apply(struct pwm_chip *chip, struct pwm_device *pwm,
  153. const struct pwm_state *state)
  154. {
  155. struct sg2042_pwm_ddata *ddata = pwmchip_get_drvdata(chip);
  156. pwm_sg2044_set_polarity(ddata, pwm, state);
  157. pwm_sg2042_set_dutycycle(chip, pwm, state);
  158. /*
  159. * re-enable PWMSTART to refresh the register period
  160. */
  161. pwm_sg2044_set_outputen(ddata, pwm, false);
  162. if (!state->enabled)
  163. return 0;
  164. pwm_sg2044_set_outputdir(ddata, pwm, true);
  165. pwm_sg2044_set_outputen(ddata, pwm, true);
  166. return 0;
  167. }
  168. static const struct sg2042_chip_data sg2042_chip_data = {
  169. .ops = {
  170. .apply = pwm_sg2042_apply,
  171. .get_state = pwm_sg2042_get_state,
  172. },
  173. };
  174. static const struct sg2042_chip_data sg2044_chip_data = {
  175. .ops = {
  176. .apply = pwm_sg2044_apply,
  177. .get_state = pwm_sg2042_get_state,
  178. },
  179. };
  180. static const struct of_device_id sg2042_pwm_ids[] = {
  181. {
  182. .compatible = "sophgo,sg2042-pwm",
  183. .data = &sg2042_chip_data
  184. },
  185. {
  186. .compatible = "sophgo,sg2044-pwm",
  187. .data = &sg2044_chip_data
  188. },
  189. { }
  190. };
  191. MODULE_DEVICE_TABLE(of, sg2042_pwm_ids);
  192. static int pwm_sg2042_probe(struct platform_device *pdev)
  193. {
  194. struct device *dev = &pdev->dev;
  195. const struct sg2042_chip_data *chip_data;
  196. struct sg2042_pwm_ddata *ddata;
  197. struct reset_control *rst;
  198. struct pwm_chip *chip;
  199. struct clk *clk;
  200. int ret;
  201. chip_data = device_get_match_data(dev);
  202. if (!chip_data)
  203. return -ENODEV;
  204. chip = devm_pwmchip_alloc(dev, SG2042_PWM_CHANNELNUM, sizeof(*ddata));
  205. if (IS_ERR(chip))
  206. return PTR_ERR(chip);
  207. ddata = pwmchip_get_drvdata(chip);
  208. ddata->base = devm_platform_ioremap_resource(pdev, 0);
  209. if (IS_ERR(ddata->base))
  210. return PTR_ERR(ddata->base);
  211. clk = devm_clk_get_enabled(dev, "apb");
  212. if (IS_ERR(clk))
  213. return dev_err_probe(dev, PTR_ERR(clk), "Failed to get base clk\n");
  214. ret = devm_clk_rate_exclusive_get(dev, clk);
  215. if (ret)
  216. return dev_err_probe(dev, ret, "Failed to get exclusive rate\n");
  217. ddata->clk_rate_hz = clk_get_rate(clk);
  218. /* period = PERIOD * NSEC_PER_SEC / clk_rate_hz */
  219. if (!ddata->clk_rate_hz || ddata->clk_rate_hz > NSEC_PER_SEC)
  220. return dev_err_probe(dev, -EINVAL,
  221. "Invalid clock rate: %lu\n", ddata->clk_rate_hz);
  222. rst = devm_reset_control_get_optional_shared_deasserted(dev, NULL);
  223. if (IS_ERR(rst))
  224. return dev_err_probe(dev, PTR_ERR(rst), "Failed to get reset\n");
  225. chip->ops = &chip_data->ops;
  226. chip->atomic = true;
  227. ret = devm_pwmchip_add(dev, chip);
  228. if (ret < 0)
  229. return dev_err_probe(dev, ret, "Failed to register PWM chip\n");
  230. return 0;
  231. }
  232. static struct platform_driver pwm_sg2042_driver = {
  233. .driver = {
  234. .name = "sg2042-pwm",
  235. .of_match_table = sg2042_pwm_ids,
  236. },
  237. .probe = pwm_sg2042_probe,
  238. };
  239. module_platform_driver(pwm_sg2042_driver);
  240. MODULE_AUTHOR("Chen Wang");
  241. MODULE_AUTHOR("Longbin Li <looong.bin@gmail.com>");
  242. MODULE_DESCRIPTION("Sophgo SG2042 PWM driver");
  243. MODULE_LICENSE("GPL");