pwm-sprd.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2019 Spreadtrum Communications Inc.
  4. */
  5. #include <linux/clk.h>
  6. #include <linux/err.h>
  7. #include <linux/io.h>
  8. #include <linux/math64.h>
  9. #include <linux/module.h>
  10. #include <linux/platform_device.h>
  11. #include <linux/pwm.h>
  12. #define SPRD_PWM_PRESCALE 0x0
  13. #define SPRD_PWM_MOD 0x4
  14. #define SPRD_PWM_DUTY 0x8
  15. #define SPRD_PWM_ENABLE 0x18
  16. #define SPRD_PWM_MOD_MAX GENMASK(7, 0)
  17. #define SPRD_PWM_DUTY_MSK GENMASK(15, 0)
  18. #define SPRD_PWM_PRESCALE_MSK GENMASK(7, 0)
  19. #define SPRD_PWM_ENABLE_BIT BIT(0)
  20. #define SPRD_PWM_CHN_NUM 4
  21. #define SPRD_PWM_REGS_SHIFT 5
  22. #define SPRD_PWM_CHN_CLKS_NUM 2
  23. #define SPRD_PWM_CHN_OUTPUT_CLK 1
  24. struct sprd_pwm_chn {
  25. struct clk_bulk_data clks[SPRD_PWM_CHN_CLKS_NUM];
  26. u32 clk_rate;
  27. };
  28. struct sprd_pwm_chip {
  29. void __iomem *base;
  30. struct device *dev;
  31. struct pwm_chip chip;
  32. int num_pwms;
  33. struct sprd_pwm_chn chn[SPRD_PWM_CHN_NUM];
  34. };
  35. /*
  36. * The list of clocks required by PWM channels, and each channel has 2 clocks:
  37. * enable clock and pwm clock.
  38. */
  39. static const char * const sprd_pwm_clks[] = {
  40. "enable0", "pwm0",
  41. "enable1", "pwm1",
  42. "enable2", "pwm2",
  43. "enable3", "pwm3",
  44. };
  45. static u32 sprd_pwm_read(struct sprd_pwm_chip *spc, u32 hwid, u32 reg)
  46. {
  47. u32 offset = reg + (hwid << SPRD_PWM_REGS_SHIFT);
  48. return readl_relaxed(spc->base + offset);
  49. }
  50. static void sprd_pwm_write(struct sprd_pwm_chip *spc, u32 hwid,
  51. u32 reg, u32 val)
  52. {
  53. u32 offset = reg + (hwid << SPRD_PWM_REGS_SHIFT);
  54. writel_relaxed(val, spc->base + offset);
  55. }
  56. static int sprd_pwm_get_state(struct pwm_chip *chip, struct pwm_device *pwm,
  57. struct pwm_state *state)
  58. {
  59. struct sprd_pwm_chip *spc =
  60. container_of(chip, struct sprd_pwm_chip, chip);
  61. struct sprd_pwm_chn *chn = &spc->chn[pwm->hwpwm];
  62. u32 val, duty, prescale;
  63. u64 tmp;
  64. int ret;
  65. /*
  66. * The clocks to PWM channel has to be enabled first before
  67. * reading to the registers.
  68. */
  69. ret = clk_bulk_prepare_enable(SPRD_PWM_CHN_CLKS_NUM, chn->clks);
  70. if (ret) {
  71. dev_err(spc->dev, "failed to enable pwm%u clocks\n",
  72. pwm->hwpwm);
  73. return 0;
  74. }
  75. val = sprd_pwm_read(spc, pwm->hwpwm, SPRD_PWM_ENABLE);
  76. if (val & SPRD_PWM_ENABLE_BIT)
  77. state->enabled = true;
  78. else
  79. state->enabled = false;
  80. /*
  81. * The hardware provides a counter that is feed by the source clock.
  82. * The period length is (PRESCALE + 1) * MOD counter steps.
  83. * The duty cycle length is (PRESCALE + 1) * DUTY counter steps.
  84. * Thus the period_ns and duty_ns calculation formula should be:
  85. * period_ns = NSEC_PER_SEC * (prescale + 1) * mod / clk_rate
  86. * duty_ns = NSEC_PER_SEC * (prescale + 1) * duty / clk_rate
  87. */
  88. val = sprd_pwm_read(spc, pwm->hwpwm, SPRD_PWM_PRESCALE);
  89. prescale = val & SPRD_PWM_PRESCALE_MSK;
  90. tmp = (prescale + 1) * NSEC_PER_SEC * SPRD_PWM_MOD_MAX;
  91. state->period = DIV_ROUND_CLOSEST_ULL(tmp, chn->clk_rate);
  92. val = sprd_pwm_read(spc, pwm->hwpwm, SPRD_PWM_DUTY);
  93. duty = val & SPRD_PWM_DUTY_MSK;
  94. tmp = (prescale + 1) * NSEC_PER_SEC * duty;
  95. state->duty_cycle = DIV_ROUND_CLOSEST_ULL(tmp, chn->clk_rate);
  96. state->polarity = PWM_POLARITY_NORMAL;
  97. /* Disable PWM clocks if the PWM channel is not in enable state. */
  98. if (!state->enabled)
  99. clk_bulk_disable_unprepare(SPRD_PWM_CHN_CLKS_NUM, chn->clks);
  100. return 0;
  101. }
  102. static int sprd_pwm_config(struct sprd_pwm_chip *spc, struct pwm_device *pwm,
  103. int duty_ns, int period_ns)
  104. {
  105. struct sprd_pwm_chn *chn = &spc->chn[pwm->hwpwm];
  106. u32 prescale, duty;
  107. u64 tmp;
  108. /*
  109. * The hardware provides a counter that is feed by the source clock.
  110. * The period length is (PRESCALE + 1) * MOD counter steps.
  111. * The duty cycle length is (PRESCALE + 1) * DUTY counter steps.
  112. *
  113. * To keep the maths simple we're always using MOD = SPRD_PWM_MOD_MAX.
  114. * The value for PRESCALE is selected such that the resulting period
  115. * gets the maximal length not bigger than the requested one with the
  116. * given settings (MOD = SPRD_PWM_MOD_MAX and input clock).
  117. */
  118. duty = duty_ns * SPRD_PWM_MOD_MAX / period_ns;
  119. tmp = (u64)chn->clk_rate * period_ns;
  120. do_div(tmp, NSEC_PER_SEC);
  121. prescale = DIV_ROUND_CLOSEST_ULL(tmp, SPRD_PWM_MOD_MAX) - 1;
  122. if (prescale > SPRD_PWM_PRESCALE_MSK)
  123. prescale = SPRD_PWM_PRESCALE_MSK;
  124. /*
  125. * Note: Writing DUTY triggers the hardware to actually apply the
  126. * values written to MOD and DUTY to the output, so must keep writing
  127. * DUTY last.
  128. *
  129. * The hardware can ensures that current running period is completed
  130. * before changing a new configuration to avoid mixed settings.
  131. */
  132. sprd_pwm_write(spc, pwm->hwpwm, SPRD_PWM_PRESCALE, prescale);
  133. sprd_pwm_write(spc, pwm->hwpwm, SPRD_PWM_MOD, SPRD_PWM_MOD_MAX);
  134. sprd_pwm_write(spc, pwm->hwpwm, SPRD_PWM_DUTY, duty);
  135. return 0;
  136. }
  137. static int sprd_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm,
  138. const struct pwm_state *state)
  139. {
  140. struct sprd_pwm_chip *spc =
  141. container_of(chip, struct sprd_pwm_chip, chip);
  142. struct sprd_pwm_chn *chn = &spc->chn[pwm->hwpwm];
  143. struct pwm_state *cstate = &pwm->state;
  144. int ret;
  145. if (state->polarity != PWM_POLARITY_NORMAL)
  146. return -EINVAL;
  147. if (state->enabled) {
  148. if (!cstate->enabled) {
  149. /*
  150. * The clocks to PWM channel has to be enabled first
  151. * before writing to the registers.
  152. */
  153. ret = clk_bulk_prepare_enable(SPRD_PWM_CHN_CLKS_NUM,
  154. chn->clks);
  155. if (ret) {
  156. dev_err(spc->dev,
  157. "failed to enable pwm%u clocks\n",
  158. pwm->hwpwm);
  159. return ret;
  160. }
  161. }
  162. ret = sprd_pwm_config(spc, pwm, state->duty_cycle,
  163. state->period);
  164. if (ret)
  165. return ret;
  166. sprd_pwm_write(spc, pwm->hwpwm, SPRD_PWM_ENABLE, 1);
  167. } else if (cstate->enabled) {
  168. /*
  169. * Note: After setting SPRD_PWM_ENABLE to zero, the controller
  170. * will not wait for current period to be completed, instead it
  171. * will stop the PWM channel immediately.
  172. */
  173. sprd_pwm_write(spc, pwm->hwpwm, SPRD_PWM_ENABLE, 0);
  174. clk_bulk_disable_unprepare(SPRD_PWM_CHN_CLKS_NUM, chn->clks);
  175. }
  176. return 0;
  177. }
  178. static const struct pwm_ops sprd_pwm_ops = {
  179. .apply = sprd_pwm_apply,
  180. .get_state = sprd_pwm_get_state,
  181. .owner = THIS_MODULE,
  182. };
  183. static int sprd_pwm_clk_init(struct sprd_pwm_chip *spc)
  184. {
  185. struct clk *clk_pwm;
  186. int ret, i;
  187. for (i = 0; i < SPRD_PWM_CHN_NUM; i++) {
  188. struct sprd_pwm_chn *chn = &spc->chn[i];
  189. int j;
  190. for (j = 0; j < SPRD_PWM_CHN_CLKS_NUM; ++j)
  191. chn->clks[j].id =
  192. sprd_pwm_clks[i * SPRD_PWM_CHN_CLKS_NUM + j];
  193. ret = devm_clk_bulk_get(spc->dev, SPRD_PWM_CHN_CLKS_NUM,
  194. chn->clks);
  195. if (ret) {
  196. if (ret == -ENOENT)
  197. break;
  198. return dev_err_probe(spc->dev, ret,
  199. "failed to get channel clocks\n");
  200. }
  201. clk_pwm = chn->clks[SPRD_PWM_CHN_OUTPUT_CLK].clk;
  202. chn->clk_rate = clk_get_rate(clk_pwm);
  203. }
  204. if (!i) {
  205. dev_err(spc->dev, "no available PWM channels\n");
  206. return -ENODEV;
  207. }
  208. spc->num_pwms = i;
  209. return 0;
  210. }
  211. static int sprd_pwm_probe(struct platform_device *pdev)
  212. {
  213. struct sprd_pwm_chip *spc;
  214. int ret;
  215. spc = devm_kzalloc(&pdev->dev, sizeof(*spc), GFP_KERNEL);
  216. if (!spc)
  217. return -ENOMEM;
  218. spc->base = devm_platform_ioremap_resource(pdev, 0);
  219. if (IS_ERR(spc->base))
  220. return PTR_ERR(spc->base);
  221. spc->dev = &pdev->dev;
  222. platform_set_drvdata(pdev, spc);
  223. ret = sprd_pwm_clk_init(spc);
  224. if (ret)
  225. return ret;
  226. spc->chip.dev = &pdev->dev;
  227. spc->chip.ops = &sprd_pwm_ops;
  228. spc->chip.npwm = spc->num_pwms;
  229. ret = pwmchip_add(&spc->chip);
  230. if (ret)
  231. dev_err(&pdev->dev, "failed to add PWM chip\n");
  232. return ret;
  233. }
  234. static int sprd_pwm_remove(struct platform_device *pdev)
  235. {
  236. struct sprd_pwm_chip *spc = platform_get_drvdata(pdev);
  237. pwmchip_remove(&spc->chip);
  238. return 0;
  239. }
  240. static const struct of_device_id sprd_pwm_of_match[] = {
  241. { .compatible = "sprd,ums512-pwm", },
  242. { },
  243. };
  244. MODULE_DEVICE_TABLE(of, sprd_pwm_of_match);
  245. static struct platform_driver sprd_pwm_driver = {
  246. .driver = {
  247. .name = "sprd-pwm",
  248. .of_match_table = sprd_pwm_of_match,
  249. },
  250. .probe = sprd_pwm_probe,
  251. .remove = sprd_pwm_remove,
  252. };
  253. module_platform_driver(sprd_pwm_driver);
  254. MODULE_DESCRIPTION("Spreadtrum PWM Driver");
  255. MODULE_LICENSE("GPL v2");