pwm-mtk.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (C) 2020 MediaTek Inc. All Rights Reserved.
  4. *
  5. * Author: Sam Shih <sam.shih@mediatek.com>
  6. */
  7. #include <common.h>
  8. #include <clk.h>
  9. #include <dm.h>
  10. #include <pwm.h>
  11. #include <div64.h>
  12. #include <linux/bitops.h>
  13. #include <linux/io.h>
  14. /* PWM registers and bits definitions */
  15. #define PWMCON 0x00
  16. #define PWMHDUR 0x04
  17. #define PWMLDUR 0x08
  18. #define PWMGDUR 0x0c
  19. #define PWMWAVENUM 0x28
  20. #define PWMDWIDTH 0x2c
  21. #define PWM45DWIDTH_FIXUP 0x30
  22. #define PWMTHRES 0x30
  23. #define PWM45THRES_FIXUP 0x34
  24. #define PWM_CLK_DIV_MAX 7
  25. #define MAX_PWM_NUM 8
  26. #define NSEC_PER_SEC 1000000000L
  27. enum mtk_pwm_reg_ver {
  28. PWM_REG_V1,
  29. PWM_REG_V2,
  30. };
  31. static const unsigned int mtk_pwm_reg_offset_v1[] = {
  32. 0x0010, 0x0050, 0x0090, 0x00d0, 0x0110, 0x0150, 0x0190, 0x0220
  33. };
  34. static const unsigned int mtk_pwm_reg_offset_v2[] = {
  35. 0x0080, 0x00c0, 0x0100, 0x0140, 0x0180, 0x01c0, 0x0200, 0x0240
  36. };
  37. struct mtk_pwm_soc {
  38. unsigned int num_pwms;
  39. bool pwm45_fixup;
  40. enum mtk_pwm_reg_ver reg_ver;
  41. };
  42. struct mtk_pwm_priv {
  43. void __iomem *base;
  44. struct clk top_clk;
  45. struct clk main_clk;
  46. struct clk pwm_clks[MAX_PWM_NUM];
  47. const struct mtk_pwm_soc *soc;
  48. };
  49. static void mtk_pwm_w32(struct udevice *dev, uint channel, uint reg, uint val)
  50. {
  51. struct mtk_pwm_priv *priv = dev_get_priv(dev);
  52. u32 offset;
  53. switch (priv->soc->reg_ver) {
  54. case PWM_REG_V2:
  55. offset = mtk_pwm_reg_offset_v2[channel];
  56. break;
  57. default:
  58. offset = mtk_pwm_reg_offset_v1[channel];
  59. }
  60. writel(val, priv->base + offset + reg);
  61. }
  62. static int mtk_pwm_set_config(struct udevice *dev, uint channel,
  63. uint period_ns, uint duty_ns)
  64. {
  65. struct mtk_pwm_priv *priv = dev_get_priv(dev);
  66. u32 clkdiv = 0, clksel = 0, cnt_period, cnt_duty,
  67. reg_width = PWMDWIDTH, reg_thres = PWMTHRES;
  68. u64 resolution;
  69. int ret = 0;
  70. clk_enable(&priv->top_clk);
  71. clk_enable(&priv->main_clk);
  72. /* Using resolution in picosecond gets accuracy higher */
  73. resolution = (u64)NSEC_PER_SEC * 1000;
  74. do_div(resolution, clk_get_rate(&priv->pwm_clks[channel]));
  75. cnt_period = DIV_ROUND_CLOSEST_ULL((u64)period_ns * 1000, resolution);
  76. while (cnt_period > 8191) {
  77. resolution *= 2;
  78. clkdiv++;
  79. cnt_period = DIV_ROUND_CLOSEST_ULL((u64)period_ns * 1000,
  80. resolution);
  81. if (clkdiv > PWM_CLK_DIV_MAX && clksel == 0) {
  82. clksel = 1;
  83. clkdiv = 0;
  84. resolution = (u64)NSEC_PER_SEC * 1000 * 1625;
  85. do_div(resolution,
  86. clk_get_rate(&priv->pwm_clks[channel]));
  87. cnt_period = DIV_ROUND_CLOSEST_ULL(
  88. (u64)period_ns * 1000, resolution);
  89. clk_enable(&priv->pwm_clks[channel]);
  90. }
  91. }
  92. if (clkdiv > PWM_CLK_DIV_MAX && clksel == 1) {
  93. printf("pwm period %u not supported\n", period_ns);
  94. return -EINVAL;
  95. }
  96. if (priv->soc->pwm45_fixup && channel > 2) {
  97. /*
  98. * PWM[4,5] has distinct offset for PWMDWIDTH and PWMTHRES
  99. * from the other PWMs on MT7623.
  100. */
  101. reg_width = PWM45DWIDTH_FIXUP;
  102. reg_thres = PWM45THRES_FIXUP;
  103. }
  104. cnt_duty = DIV_ROUND_CLOSEST_ULL((u64)duty_ns * 1000, resolution);
  105. if (clksel == 1)
  106. mtk_pwm_w32(dev, channel, PWMCON, BIT(15) | BIT(3) | clkdiv);
  107. else
  108. mtk_pwm_w32(dev, channel, PWMCON, BIT(15) | clkdiv);
  109. mtk_pwm_w32(dev, channel, reg_width, cnt_period);
  110. mtk_pwm_w32(dev, channel, reg_thres, cnt_duty);
  111. return ret;
  112. };
  113. static int mtk_pwm_set_enable(struct udevice *dev, uint channel, bool enable)
  114. {
  115. struct mtk_pwm_priv *priv = dev_get_priv(dev);
  116. u32 val = 0;
  117. val = readl(priv->base);
  118. if (enable)
  119. val |= BIT(channel);
  120. else
  121. val &= ~BIT(channel);
  122. writel(val, priv->base);
  123. return 0;
  124. };
  125. static int mtk_pwm_probe(struct udevice *dev)
  126. {
  127. struct mtk_pwm_priv *priv = dev_get_priv(dev);
  128. int ret = 0;
  129. int i;
  130. priv->soc = (struct mtk_pwm_soc *)dev_get_driver_data(dev);
  131. priv->base = dev_read_addr_ptr(dev);
  132. if (!priv->base)
  133. return -EINVAL;
  134. ret = clk_get_by_name(dev, "top", &priv->top_clk);
  135. if (ret < 0)
  136. return ret;
  137. ret = clk_get_by_name(dev, "main", &priv->main_clk);
  138. if (ret < 0)
  139. return ret;
  140. for (i = 0; i < priv->soc->num_pwms; i++) {
  141. char name[8];
  142. snprintf(name, sizeof(name), "pwm%d", i + 1);
  143. ret = clk_get_by_name(dev, name, &priv->pwm_clks[i]);
  144. if (ret < 0)
  145. return ret;
  146. }
  147. return ret;
  148. }
  149. static const struct pwm_ops mtk_pwm_ops = {
  150. .set_config = mtk_pwm_set_config,
  151. .set_enable = mtk_pwm_set_enable,
  152. };
  153. static const struct mtk_pwm_soc mt7622_data = {
  154. .num_pwms = 6,
  155. .pwm45_fixup = false,
  156. .reg_ver = PWM_REG_V1,
  157. };
  158. static const struct mtk_pwm_soc mt7623_data = {
  159. .num_pwms = 5,
  160. .pwm45_fixup = true,
  161. .reg_ver = PWM_REG_V1,
  162. };
  163. static const struct mtk_pwm_soc mt7629_data = {
  164. .num_pwms = 1,
  165. .pwm45_fixup = false,
  166. .reg_ver = PWM_REG_V1,
  167. };
  168. static const struct mtk_pwm_soc mt7981_data = {
  169. .num_pwms = 2,
  170. .pwm45_fixup = false,
  171. .reg_ver = PWM_REG_V2,
  172. };
  173. static const struct mtk_pwm_soc mt7986_data = {
  174. .num_pwms = 2,
  175. .pwm45_fixup = false,
  176. .reg_ver = PWM_REG_V1,
  177. };
  178. static const struct mtk_pwm_soc mt7988_data = {
  179. .num_pwms = 8,
  180. .pwm45_fixup = false,
  181. .reg_ver = PWM_REG_V2,
  182. };
  183. static const struct udevice_id mtk_pwm_ids[] = {
  184. { .compatible = "mediatek,mt7622-pwm", .data = (ulong)&mt7622_data },
  185. { .compatible = "mediatek,mt7623-pwm", .data = (ulong)&mt7623_data },
  186. { .compatible = "mediatek,mt7629-pwm", .data = (ulong)&mt7629_data },
  187. { .compatible = "mediatek,mt7981-pwm", .data = (ulong)&mt7981_data },
  188. { .compatible = "mediatek,mt7986-pwm", .data = (ulong)&mt7986_data },
  189. { .compatible = "mediatek,mt7988-pwm", .data = (ulong)&mt7988_data },
  190. { }
  191. };
  192. U_BOOT_DRIVER(mtk_pwm) = {
  193. .name = "mtk_pwm",
  194. .id = UCLASS_PWM,
  195. .of_match = mtk_pwm_ids,
  196. .ops = &mtk_pwm_ops,
  197. .probe = mtk_pwm_probe,
  198. .priv_auto = sizeof(struct mtk_pwm_priv),
  199. };