pwm-mediatek.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * MediaTek Pulse Width Modulator driver
  4. *
  5. * Copyright (C) 2015 John Crispin <[email protected]>
  6. * Copyright (C) 2017 Zhi Mao <[email protected]>
  7. *
  8. */
  9. #include <linux/err.h>
  10. #include <linux/io.h>
  11. #include <linux/ioport.h>
  12. #include <linux/kernel.h>
  13. #include <linux/module.h>
  14. #include <linux/clk.h>
  15. #include <linux/of.h>
  16. #include <linux/of_device.h>
  17. #include <linux/platform_device.h>
  18. #include <linux/pwm.h>
  19. #include <linux/slab.h>
  20. #include <linux/types.h>
  21. /* PWM registers and bits definitions */
  22. #define PWMCON 0x00
  23. #define PWMHDUR 0x04
  24. #define PWMLDUR 0x08
  25. #define PWMGDUR 0x0c
  26. #define PWMWAVENUM 0x28
  27. #define PWMDWIDTH 0x2c
  28. #define PWM45DWIDTH_FIXUP 0x30
  29. #define PWMTHRES 0x30
  30. #define PWM45THRES_FIXUP 0x34
  31. #define PWM_CK_26M_SEL 0x210
  32. #define PWM_CLK_DIV_MAX 7
  33. struct pwm_mediatek_of_data {
  34. unsigned int num_pwms;
  35. bool pwm45_fixup;
  36. bool has_ck_26m_sel;
  37. };
  38. /**
  39. * struct pwm_mediatek_chip - struct representing PWM chip
  40. * @chip: linux PWM chip representation
  41. * @regs: base address of PWM chip
  42. * @clk_top: the top clock generator
  43. * @clk_main: the clock used by PWM core
  44. * @clk_pwms: the clock used by each PWM channel
  45. * @clk_freq: the fix clock frequency of legacy MIPS SoC
  46. * @soc: pointer to chip's platform data
  47. */
  48. struct pwm_mediatek_chip {
  49. struct pwm_chip chip;
  50. void __iomem *regs;
  51. struct clk *clk_top;
  52. struct clk *clk_main;
  53. struct clk **clk_pwms;
  54. const struct pwm_mediatek_of_data *soc;
  55. };
  56. static const unsigned int pwm_mediatek_reg_offset[] = {
  57. 0x0010, 0x0050, 0x0090, 0x00d0, 0x0110, 0x0150, 0x0190, 0x0220
  58. };
  59. static inline struct pwm_mediatek_chip *
  60. to_pwm_mediatek_chip(struct pwm_chip *chip)
  61. {
  62. return container_of(chip, struct pwm_mediatek_chip, chip);
  63. }
  64. static int pwm_mediatek_clk_enable(struct pwm_chip *chip,
  65. struct pwm_device *pwm)
  66. {
  67. struct pwm_mediatek_chip *pc = to_pwm_mediatek_chip(chip);
  68. int ret;
  69. ret = clk_prepare_enable(pc->clk_top);
  70. if (ret < 0)
  71. return ret;
  72. ret = clk_prepare_enable(pc->clk_main);
  73. if (ret < 0)
  74. goto disable_clk_top;
  75. ret = clk_prepare_enable(pc->clk_pwms[pwm->hwpwm]);
  76. if (ret < 0)
  77. goto disable_clk_main;
  78. return 0;
  79. disable_clk_main:
  80. clk_disable_unprepare(pc->clk_main);
  81. disable_clk_top:
  82. clk_disable_unprepare(pc->clk_top);
  83. return ret;
  84. }
  85. static void pwm_mediatek_clk_disable(struct pwm_chip *chip,
  86. struct pwm_device *pwm)
  87. {
  88. struct pwm_mediatek_chip *pc = to_pwm_mediatek_chip(chip);
  89. clk_disable_unprepare(pc->clk_pwms[pwm->hwpwm]);
  90. clk_disable_unprepare(pc->clk_main);
  91. clk_disable_unprepare(pc->clk_top);
  92. }
  93. static inline void pwm_mediatek_writel(struct pwm_mediatek_chip *chip,
  94. unsigned int num, unsigned int offset,
  95. u32 value)
  96. {
  97. writel(value, chip->regs + pwm_mediatek_reg_offset[num] + offset);
  98. }
  99. static int pwm_mediatek_config(struct pwm_chip *chip, struct pwm_device *pwm,
  100. int duty_ns, int period_ns)
  101. {
  102. struct pwm_mediatek_chip *pc = to_pwm_mediatek_chip(chip);
  103. u32 clkdiv = 0, cnt_period, cnt_duty, reg_width = PWMDWIDTH,
  104. reg_thres = PWMTHRES;
  105. u64 resolution;
  106. int ret;
  107. ret = pwm_mediatek_clk_enable(chip, pwm);
  108. if (ret < 0)
  109. return ret;
  110. /* Make sure we use the bus clock and not the 26MHz clock */
  111. if (pc->soc->has_ck_26m_sel)
  112. writel(0, pc->regs + PWM_CK_26M_SEL);
  113. /* Using resolution in picosecond gets accuracy higher */
  114. resolution = (u64)NSEC_PER_SEC * 1000;
  115. do_div(resolution, clk_get_rate(pc->clk_pwms[pwm->hwpwm]));
  116. cnt_period = DIV_ROUND_CLOSEST_ULL((u64)period_ns * 1000, resolution);
  117. while (cnt_period > 8191) {
  118. resolution *= 2;
  119. clkdiv++;
  120. cnt_period = DIV_ROUND_CLOSEST_ULL((u64)period_ns * 1000,
  121. resolution);
  122. }
  123. if (clkdiv > PWM_CLK_DIV_MAX) {
  124. pwm_mediatek_clk_disable(chip, pwm);
  125. dev_err(chip->dev, "period of %d ns not supported\n", period_ns);
  126. return -EINVAL;
  127. }
  128. if (pc->soc->pwm45_fixup && pwm->hwpwm > 2) {
  129. /*
  130. * PWM[4,5] has distinct offset for PWMDWIDTH and PWMTHRES
  131. * from the other PWMs on MT7623.
  132. */
  133. reg_width = PWM45DWIDTH_FIXUP;
  134. reg_thres = PWM45THRES_FIXUP;
  135. }
  136. cnt_duty = DIV_ROUND_CLOSEST_ULL((u64)duty_ns * 1000, resolution);
  137. pwm_mediatek_writel(pc, pwm->hwpwm, PWMCON, BIT(15) | clkdiv);
  138. pwm_mediatek_writel(pc, pwm->hwpwm, reg_width, cnt_period);
  139. pwm_mediatek_writel(pc, pwm->hwpwm, reg_thres, cnt_duty);
  140. pwm_mediatek_clk_disable(chip, pwm);
  141. return 0;
  142. }
  143. static int pwm_mediatek_enable(struct pwm_chip *chip, struct pwm_device *pwm)
  144. {
  145. struct pwm_mediatek_chip *pc = to_pwm_mediatek_chip(chip);
  146. u32 value;
  147. int ret;
  148. ret = pwm_mediatek_clk_enable(chip, pwm);
  149. if (ret < 0)
  150. return ret;
  151. value = readl(pc->regs);
  152. value |= BIT(pwm->hwpwm);
  153. writel(value, pc->regs);
  154. return 0;
  155. }
  156. static void pwm_mediatek_disable(struct pwm_chip *chip, struct pwm_device *pwm)
  157. {
  158. struct pwm_mediatek_chip *pc = to_pwm_mediatek_chip(chip);
  159. u32 value;
  160. value = readl(pc->regs);
  161. value &= ~BIT(pwm->hwpwm);
  162. writel(value, pc->regs);
  163. pwm_mediatek_clk_disable(chip, pwm);
  164. }
  165. static int pwm_mediatek_apply(struct pwm_chip *chip, struct pwm_device *pwm,
  166. const struct pwm_state *state)
  167. {
  168. int err;
  169. if (state->polarity != PWM_POLARITY_NORMAL)
  170. return -EINVAL;
  171. if (!state->enabled) {
  172. if (pwm->state.enabled)
  173. pwm_mediatek_disable(chip, pwm);
  174. return 0;
  175. }
  176. err = pwm_mediatek_config(pwm->chip, pwm, state->duty_cycle, state->period);
  177. if (err)
  178. return err;
  179. if (!pwm->state.enabled)
  180. err = pwm_mediatek_enable(chip, pwm);
  181. return err;
  182. }
  183. static const struct pwm_ops pwm_mediatek_ops = {
  184. .apply = pwm_mediatek_apply,
  185. .owner = THIS_MODULE,
  186. };
  187. static int pwm_mediatek_probe(struct platform_device *pdev)
  188. {
  189. struct pwm_mediatek_chip *pc;
  190. unsigned int i;
  191. int ret;
  192. pc = devm_kzalloc(&pdev->dev, sizeof(*pc), GFP_KERNEL);
  193. if (!pc)
  194. return -ENOMEM;
  195. pc->soc = of_device_get_match_data(&pdev->dev);
  196. pc->regs = devm_platform_ioremap_resource(pdev, 0);
  197. if (IS_ERR(pc->regs))
  198. return PTR_ERR(pc->regs);
  199. pc->clk_pwms = devm_kmalloc_array(&pdev->dev, pc->soc->num_pwms,
  200. sizeof(*pc->clk_pwms), GFP_KERNEL);
  201. if (!pc->clk_pwms)
  202. return -ENOMEM;
  203. pc->clk_top = devm_clk_get(&pdev->dev, "top");
  204. if (IS_ERR(pc->clk_top))
  205. return dev_err_probe(&pdev->dev, PTR_ERR(pc->clk_top),
  206. "Failed to get top clock\n");
  207. pc->clk_main = devm_clk_get(&pdev->dev, "main");
  208. if (IS_ERR(pc->clk_main))
  209. return dev_err_probe(&pdev->dev, PTR_ERR(pc->clk_main),
  210. "Failed to get main clock\n");
  211. for (i = 0; i < pc->soc->num_pwms; i++) {
  212. char name[8];
  213. snprintf(name, sizeof(name), "pwm%d", i + 1);
  214. pc->clk_pwms[i] = devm_clk_get(&pdev->dev, name);
  215. if (IS_ERR(pc->clk_pwms[i]))
  216. return dev_err_probe(&pdev->dev, PTR_ERR(pc->clk_pwms[i]),
  217. "Failed to get %s clock\n", name);
  218. }
  219. pc->chip.dev = &pdev->dev;
  220. pc->chip.ops = &pwm_mediatek_ops;
  221. pc->chip.npwm = pc->soc->num_pwms;
  222. ret = devm_pwmchip_add(&pdev->dev, &pc->chip);
  223. if (ret < 0)
  224. return dev_err_probe(&pdev->dev, ret, "pwmchip_add() failed\n");
  225. return 0;
  226. }
  227. static const struct pwm_mediatek_of_data mt2712_pwm_data = {
  228. .num_pwms = 8,
  229. .pwm45_fixup = false,
  230. .has_ck_26m_sel = false,
  231. };
  232. static const struct pwm_mediatek_of_data mt6795_pwm_data = {
  233. .num_pwms = 7,
  234. .pwm45_fixup = false,
  235. .has_ck_26m_sel = false,
  236. };
  237. static const struct pwm_mediatek_of_data mt7622_pwm_data = {
  238. .num_pwms = 6,
  239. .pwm45_fixup = false,
  240. .has_ck_26m_sel = true,
  241. };
  242. static const struct pwm_mediatek_of_data mt7623_pwm_data = {
  243. .num_pwms = 5,
  244. .pwm45_fixup = true,
  245. .has_ck_26m_sel = false,
  246. };
  247. static const struct pwm_mediatek_of_data mt7628_pwm_data = {
  248. .num_pwms = 4,
  249. .pwm45_fixup = true,
  250. .has_ck_26m_sel = false,
  251. };
  252. static const struct pwm_mediatek_of_data mt7629_pwm_data = {
  253. .num_pwms = 1,
  254. .pwm45_fixup = false,
  255. .has_ck_26m_sel = false,
  256. };
  257. static const struct pwm_mediatek_of_data mt8183_pwm_data = {
  258. .num_pwms = 4,
  259. .pwm45_fixup = false,
  260. .has_ck_26m_sel = true,
  261. };
  262. static const struct pwm_mediatek_of_data mt8365_pwm_data = {
  263. .num_pwms = 3,
  264. .pwm45_fixup = false,
  265. .has_ck_26m_sel = true,
  266. };
  267. static const struct pwm_mediatek_of_data mt8516_pwm_data = {
  268. .num_pwms = 5,
  269. .pwm45_fixup = false,
  270. .has_ck_26m_sel = true,
  271. };
  272. static const struct of_device_id pwm_mediatek_of_match[] = {
  273. { .compatible = "mediatek,mt2712-pwm", .data = &mt2712_pwm_data },
  274. { .compatible = "mediatek,mt6795-pwm", .data = &mt6795_pwm_data },
  275. { .compatible = "mediatek,mt7622-pwm", .data = &mt7622_pwm_data },
  276. { .compatible = "mediatek,mt7623-pwm", .data = &mt7623_pwm_data },
  277. { .compatible = "mediatek,mt7628-pwm", .data = &mt7628_pwm_data },
  278. { .compatible = "mediatek,mt7629-pwm", .data = &mt7629_pwm_data },
  279. { .compatible = "mediatek,mt8183-pwm", .data = &mt8183_pwm_data },
  280. { .compatible = "mediatek,mt8365-pwm", .data = &mt8365_pwm_data },
  281. { .compatible = "mediatek,mt8516-pwm", .data = &mt8516_pwm_data },
  282. { },
  283. };
  284. MODULE_DEVICE_TABLE(of, pwm_mediatek_of_match);
  285. static struct platform_driver pwm_mediatek_driver = {
  286. .driver = {
  287. .name = "pwm-mediatek",
  288. .of_match_table = pwm_mediatek_of_match,
  289. },
  290. .probe = pwm_mediatek_probe,
  291. };
  292. module_platform_driver(pwm_mediatek_driver);
  293. MODULE_AUTHOR("John Crispin <[email protected]>");
  294. MODULE_LICENSE("GPL v2");