pwm-hibvt.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * PWM Controller Driver for HiSilicon BVT SoCs
  4. *
  5. * Copyright (c) 2016 HiSilicon Technologies Co., Ltd.
  6. */
  7. #include <linux/bitops.h>
  8. #include <linux/clk.h>
  9. #include <linux/delay.h>
  10. #include <linux/io.h>
  11. #include <linux/module.h>
  12. #include <linux/of.h>
  13. #include <linux/platform_device.h>
  14. #include <linux/pwm.h>
  15. #include <linux/reset.h>
  16. #define PWM_CFG0_ADDR(x) (((x) * 0x20) + 0x0)
  17. #define PWM_CFG1_ADDR(x) (((x) * 0x20) + 0x4)
  18. #define PWM_CFG2_ADDR(x) (((x) * 0x20) + 0x8)
  19. #define PWM_CTRL_ADDR(x) (((x) * 0x20) + 0xC)
  20. #define PWM_ENABLE_SHIFT 0
  21. #define PWM_ENABLE_MASK BIT(0)
  22. #define PWM_POLARITY_SHIFT 1
  23. #define PWM_POLARITY_MASK BIT(1)
  24. #define PWM_KEEP_SHIFT 2
  25. #define PWM_KEEP_MASK BIT(2)
  26. #define PWM_PERIOD_MASK GENMASK(31, 0)
  27. #define PWM_DUTY_MASK GENMASK(31, 0)
  28. struct hibvt_pwm_chip {
  29. struct clk *clk;
  30. void __iomem *base;
  31. struct reset_control *rstc;
  32. const struct hibvt_pwm_soc *soc;
  33. };
  34. struct hibvt_pwm_soc {
  35. u32 num_pwms;
  36. bool quirk_force_enable;
  37. };
  38. static const struct hibvt_pwm_soc hi3516cv300_soc_info = {
  39. .num_pwms = 4,
  40. };
  41. static const struct hibvt_pwm_soc hi3519v100_soc_info = {
  42. .num_pwms = 8,
  43. };
  44. static const struct hibvt_pwm_soc hi3559v100_shub_soc_info = {
  45. .num_pwms = 8,
  46. .quirk_force_enable = true,
  47. };
  48. static const struct hibvt_pwm_soc hi3559v100_soc_info = {
  49. .num_pwms = 2,
  50. .quirk_force_enable = true,
  51. };
  52. static inline struct hibvt_pwm_chip *to_hibvt_pwm_chip(struct pwm_chip *chip)
  53. {
  54. return pwmchip_get_drvdata(chip);
  55. }
  56. static void hibvt_pwm_set_bits(void __iomem *base, u32 offset,
  57. u32 mask, u32 data)
  58. {
  59. void __iomem *address = base + offset;
  60. u32 value;
  61. value = readl(address);
  62. value &= ~mask;
  63. value |= (data & mask);
  64. writel(value, address);
  65. }
  66. static void hibvt_pwm_enable(struct pwm_chip *chip, struct pwm_device *pwm)
  67. {
  68. struct hibvt_pwm_chip *hi_pwm_chip = to_hibvt_pwm_chip(chip);
  69. hibvt_pwm_set_bits(hi_pwm_chip->base, PWM_CTRL_ADDR(pwm->hwpwm),
  70. PWM_ENABLE_MASK, 0x1);
  71. }
  72. static void hibvt_pwm_disable(struct pwm_chip *chip, struct pwm_device *pwm)
  73. {
  74. struct hibvt_pwm_chip *hi_pwm_chip = to_hibvt_pwm_chip(chip);
  75. hibvt_pwm_set_bits(hi_pwm_chip->base, PWM_CTRL_ADDR(pwm->hwpwm),
  76. PWM_ENABLE_MASK, 0x0);
  77. }
  78. static void hibvt_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm,
  79. int duty_cycle_ns, int period_ns)
  80. {
  81. struct hibvt_pwm_chip *hi_pwm_chip = to_hibvt_pwm_chip(chip);
  82. u32 freq, period, duty;
  83. freq = div_u64(clk_get_rate(hi_pwm_chip->clk), 1000000);
  84. period = div_u64(freq * period_ns, 1000);
  85. duty = div_u64(period * duty_cycle_ns, period_ns);
  86. hibvt_pwm_set_bits(hi_pwm_chip->base, PWM_CFG0_ADDR(pwm->hwpwm),
  87. PWM_PERIOD_MASK, period);
  88. hibvt_pwm_set_bits(hi_pwm_chip->base, PWM_CFG1_ADDR(pwm->hwpwm),
  89. PWM_DUTY_MASK, duty);
  90. }
  91. static void hibvt_pwm_set_polarity(struct pwm_chip *chip,
  92. struct pwm_device *pwm,
  93. enum pwm_polarity polarity)
  94. {
  95. struct hibvt_pwm_chip *hi_pwm_chip = to_hibvt_pwm_chip(chip);
  96. if (polarity == PWM_POLARITY_INVERSED)
  97. hibvt_pwm_set_bits(hi_pwm_chip->base, PWM_CTRL_ADDR(pwm->hwpwm),
  98. PWM_POLARITY_MASK, (0x1 << PWM_POLARITY_SHIFT));
  99. else
  100. hibvt_pwm_set_bits(hi_pwm_chip->base, PWM_CTRL_ADDR(pwm->hwpwm),
  101. PWM_POLARITY_MASK, (0x0 << PWM_POLARITY_SHIFT));
  102. }
  103. static int hibvt_pwm_get_state(struct pwm_chip *chip, struct pwm_device *pwm,
  104. struct pwm_state *state)
  105. {
  106. struct hibvt_pwm_chip *hi_pwm_chip = to_hibvt_pwm_chip(chip);
  107. void __iomem *base;
  108. u32 freq, value;
  109. freq = div_u64(clk_get_rate(hi_pwm_chip->clk), 1000000);
  110. base = hi_pwm_chip->base;
  111. value = readl(base + PWM_CFG0_ADDR(pwm->hwpwm));
  112. state->period = div_u64(value * 1000, freq);
  113. value = readl(base + PWM_CFG1_ADDR(pwm->hwpwm));
  114. state->duty_cycle = div_u64(value * 1000, freq);
  115. value = readl(base + PWM_CTRL_ADDR(pwm->hwpwm));
  116. state->enabled = (PWM_ENABLE_MASK & value);
  117. state->polarity = (PWM_POLARITY_MASK & value) ? PWM_POLARITY_INVERSED : PWM_POLARITY_NORMAL;
  118. return 0;
  119. }
  120. static int hibvt_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm,
  121. const struct pwm_state *state)
  122. {
  123. struct hibvt_pwm_chip *hi_pwm_chip = to_hibvt_pwm_chip(chip);
  124. if (state->polarity != pwm->state.polarity)
  125. hibvt_pwm_set_polarity(chip, pwm, state->polarity);
  126. if (state->period != pwm->state.period ||
  127. state->duty_cycle != pwm->state.duty_cycle) {
  128. hibvt_pwm_config(chip, pwm, state->duty_cycle, state->period);
  129. /*
  130. * Some implementations require the PWM to be enabled twice
  131. * each time the duty cycle is refreshed.
  132. */
  133. if (hi_pwm_chip->soc->quirk_force_enable && state->enabled)
  134. hibvt_pwm_enable(chip, pwm);
  135. }
  136. if (state->enabled != pwm->state.enabled) {
  137. if (state->enabled)
  138. hibvt_pwm_enable(chip, pwm);
  139. else
  140. hibvt_pwm_disable(chip, pwm);
  141. }
  142. return 0;
  143. }
  144. static const struct pwm_ops hibvt_pwm_ops = {
  145. .get_state = hibvt_pwm_get_state,
  146. .apply = hibvt_pwm_apply,
  147. };
  148. static int hibvt_pwm_probe(struct platform_device *pdev)
  149. {
  150. const struct hibvt_pwm_soc *soc =
  151. of_device_get_match_data(&pdev->dev);
  152. struct pwm_chip *chip;
  153. struct hibvt_pwm_chip *hi_pwm_chip;
  154. int ret, i;
  155. chip = devm_pwmchip_alloc(&pdev->dev, soc->num_pwms, sizeof(*hi_pwm_chip));
  156. if (IS_ERR(chip))
  157. return PTR_ERR(chip);
  158. hi_pwm_chip = to_hibvt_pwm_chip(chip);
  159. hi_pwm_chip->clk = devm_clk_get(&pdev->dev, NULL);
  160. if (IS_ERR(hi_pwm_chip->clk)) {
  161. dev_err(&pdev->dev, "getting clock failed with %ld\n",
  162. PTR_ERR(hi_pwm_chip->clk));
  163. return PTR_ERR(hi_pwm_chip->clk);
  164. }
  165. chip->ops = &hibvt_pwm_ops;
  166. hi_pwm_chip->soc = soc;
  167. hi_pwm_chip->base = devm_platform_ioremap_resource(pdev, 0);
  168. if (IS_ERR(hi_pwm_chip->base))
  169. return PTR_ERR(hi_pwm_chip->base);
  170. ret = clk_prepare_enable(hi_pwm_chip->clk);
  171. if (ret < 0)
  172. return ret;
  173. hi_pwm_chip->rstc = devm_reset_control_get_exclusive(&pdev->dev, NULL);
  174. if (IS_ERR(hi_pwm_chip->rstc)) {
  175. clk_disable_unprepare(hi_pwm_chip->clk);
  176. return PTR_ERR(hi_pwm_chip->rstc);
  177. }
  178. reset_control_assert(hi_pwm_chip->rstc);
  179. msleep(30);
  180. reset_control_deassert(hi_pwm_chip->rstc);
  181. ret = pwmchip_add(chip);
  182. if (ret < 0) {
  183. clk_disable_unprepare(hi_pwm_chip->clk);
  184. return ret;
  185. }
  186. for (i = 0; i < chip->npwm; i++) {
  187. hibvt_pwm_set_bits(hi_pwm_chip->base, PWM_CTRL_ADDR(i),
  188. PWM_KEEP_MASK, (0x1 << PWM_KEEP_SHIFT));
  189. }
  190. platform_set_drvdata(pdev, chip);
  191. return 0;
  192. }
  193. static void hibvt_pwm_remove(struct platform_device *pdev)
  194. {
  195. struct pwm_chip *chip = platform_get_drvdata(pdev);
  196. struct hibvt_pwm_chip *hi_pwm_chip = to_hibvt_pwm_chip(chip);
  197. pwmchip_remove(chip);
  198. reset_control_assert(hi_pwm_chip->rstc);
  199. msleep(30);
  200. reset_control_deassert(hi_pwm_chip->rstc);
  201. clk_disable_unprepare(hi_pwm_chip->clk);
  202. }
  203. static const struct of_device_id hibvt_pwm_of_match[] = {
  204. { .compatible = "hisilicon,hi3516cv300-pwm",
  205. .data = &hi3516cv300_soc_info },
  206. { .compatible = "hisilicon,hi3519v100-pwm",
  207. .data = &hi3519v100_soc_info },
  208. { .compatible = "hisilicon,hi3559v100-shub-pwm",
  209. .data = &hi3559v100_shub_soc_info },
  210. { .compatible = "hisilicon,hi3559v100-pwm",
  211. .data = &hi3559v100_soc_info },
  212. { }
  213. };
  214. MODULE_DEVICE_TABLE(of, hibvt_pwm_of_match);
  215. static struct platform_driver hibvt_pwm_driver = {
  216. .driver = {
  217. .name = "hibvt-pwm",
  218. .of_match_table = hibvt_pwm_of_match,
  219. },
  220. .probe = hibvt_pwm_probe,
  221. .remove = hibvt_pwm_remove,
  222. };
  223. module_platform_driver(hibvt_pwm_driver);
  224. MODULE_AUTHOR("Jian Yuan");
  225. MODULE_DESCRIPTION("HiSilicon BVT SoCs PWM driver");
  226. MODULE_LICENSE("GPL");