pwm-rockchip.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * PWM driver for Rockchip SoCs
  4. *
  5. * Copyright (C) 2014 Beniamino Galvani <b.galvani@gmail.com>
  6. * Copyright (C) 2014 ROCKCHIP, Inc.
  7. */
  8. #include <linux/clk.h>
  9. #include <linux/io.h>
  10. #include <linux/limits.h>
  11. #include <linux/math64.h>
  12. #include <linux/module.h>
  13. #include <linux/of.h>
  14. #include <linux/platform_device.h>
  15. #include <linux/property.h>
  16. #include <linux/pwm.h>
  17. #include <linux/time.h>
  18. #define PWM_CTRL_TIMER_EN (1 << 0)
  19. #define PWM_CTRL_OUTPUT_EN (1 << 3)
  20. #define PWM_ENABLE (1 << 0)
  21. #define PWM_CONTINUOUS (1 << 1)
  22. #define PWM_DUTY_POSITIVE (1 << 3)
  23. #define PWM_DUTY_NEGATIVE (0 << 3)
  24. #define PWM_INACTIVE_NEGATIVE (0 << 4)
  25. #define PWM_INACTIVE_POSITIVE (1 << 4)
  26. #define PWM_POLARITY_MASK (PWM_DUTY_POSITIVE | PWM_INACTIVE_POSITIVE)
  27. #define PWM_OUTPUT_LEFT (0 << 5)
  28. #define PWM_LOCK_EN (1 << 6)
  29. #define PWM_LP_DISABLE (0 << 8)
  30. struct rockchip_pwm_chip {
  31. struct clk *clk;
  32. struct clk *pclk;
  33. const struct rockchip_pwm_data *data;
  34. void __iomem *base;
  35. };
  36. struct rockchip_pwm_regs {
  37. unsigned long duty;
  38. unsigned long period;
  39. unsigned long cntr;
  40. unsigned long ctrl;
  41. };
  42. struct rockchip_pwm_data {
  43. struct rockchip_pwm_regs regs;
  44. unsigned int prescaler;
  45. bool supports_polarity;
  46. bool supports_lock;
  47. u32 enable_conf;
  48. };
  49. static inline struct rockchip_pwm_chip *to_rockchip_pwm_chip(struct pwm_chip *chip)
  50. {
  51. return pwmchip_get_drvdata(chip);
  52. }
  53. static int rockchip_pwm_get_state(struct pwm_chip *chip,
  54. struct pwm_device *pwm,
  55. struct pwm_state *state)
  56. {
  57. struct rockchip_pwm_chip *pc = to_rockchip_pwm_chip(chip);
  58. u64 prescaled_ns = (u64)pc->data->prescaler * NSEC_PER_SEC;
  59. u32 enable_conf = pc->data->enable_conf;
  60. unsigned long clk_rate;
  61. u64 tmp;
  62. u32 val;
  63. int ret;
  64. ret = clk_enable(pc->pclk);
  65. if (ret)
  66. return ret;
  67. ret = clk_enable(pc->clk);
  68. if (ret)
  69. return ret;
  70. clk_rate = clk_get_rate(pc->clk);
  71. tmp = readl_relaxed(pc->base + pc->data->regs.period);
  72. tmp *= prescaled_ns;
  73. state->period = DIV_U64_ROUND_UP(tmp, clk_rate);
  74. tmp = readl_relaxed(pc->base + pc->data->regs.duty);
  75. tmp *= prescaled_ns;
  76. state->duty_cycle = DIV_U64_ROUND_UP(tmp, clk_rate);
  77. val = readl_relaxed(pc->base + pc->data->regs.ctrl);
  78. state->enabled = (val & enable_conf) == enable_conf;
  79. if (pc->data->supports_polarity && !(val & PWM_DUTY_POSITIVE))
  80. state->polarity = PWM_POLARITY_INVERSED;
  81. else
  82. state->polarity = PWM_POLARITY_NORMAL;
  83. clk_disable(pc->clk);
  84. clk_disable(pc->pclk);
  85. return 0;
  86. }
  87. static void rockchip_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm,
  88. const struct pwm_state *state)
  89. {
  90. struct rockchip_pwm_chip *pc = to_rockchip_pwm_chip(chip);
  91. u64 prescaled_ns = (u64)pc->data->prescaler * NSEC_PER_SEC;
  92. u64 clk_rate, tmp;
  93. u32 period_ticks, duty_ticks;
  94. u32 ctrl;
  95. clk_rate = clk_get_rate(pc->clk);
  96. /*
  97. * Since period and duty cycle registers have a width of 32
  98. * bits, every possible input period can be obtained using the
  99. * default prescaler value for all practical clock rate values.
  100. */
  101. tmp = mul_u64_u64_div_u64(clk_rate, state->period, prescaled_ns);
  102. if (tmp > U32_MAX)
  103. tmp = U32_MAX;
  104. period_ticks = tmp;
  105. tmp = mul_u64_u64_div_u64(clk_rate, state->duty_cycle, prescaled_ns);
  106. if (tmp > U32_MAX)
  107. tmp = U32_MAX;
  108. duty_ticks = tmp;
  109. /*
  110. * Lock the period and duty of previous configuration, then
  111. * change the duty and period, that would not be effective.
  112. */
  113. ctrl = readl_relaxed(pc->base + pc->data->regs.ctrl);
  114. if (pc->data->supports_lock) {
  115. ctrl |= PWM_LOCK_EN;
  116. writel_relaxed(ctrl, pc->base + pc->data->regs.ctrl);
  117. }
  118. writel(period_ticks, pc->base + pc->data->regs.period);
  119. writel(duty_ticks, pc->base + pc->data->regs.duty);
  120. if (pc->data->supports_polarity) {
  121. ctrl &= ~PWM_POLARITY_MASK;
  122. if (state->polarity == PWM_POLARITY_INVERSED)
  123. ctrl |= PWM_DUTY_NEGATIVE | PWM_INACTIVE_POSITIVE;
  124. else
  125. ctrl |= PWM_DUTY_POSITIVE | PWM_INACTIVE_NEGATIVE;
  126. }
  127. /*
  128. * Unlock and set polarity at the same time,
  129. * the configuration of duty, period and polarity
  130. * would be effective together at next period.
  131. */
  132. if (pc->data->supports_lock)
  133. ctrl &= ~PWM_LOCK_EN;
  134. writel(ctrl, pc->base + pc->data->regs.ctrl);
  135. }
  136. static int rockchip_pwm_enable(struct pwm_chip *chip,
  137. struct pwm_device *pwm,
  138. bool enable)
  139. {
  140. struct rockchip_pwm_chip *pc = to_rockchip_pwm_chip(chip);
  141. u32 enable_conf = pc->data->enable_conf;
  142. int ret;
  143. u32 val;
  144. if (enable) {
  145. ret = clk_enable(pc->clk);
  146. if (ret)
  147. return ret;
  148. }
  149. val = readl_relaxed(pc->base + pc->data->regs.ctrl);
  150. if (enable)
  151. val |= enable_conf;
  152. else
  153. val &= ~enable_conf;
  154. writel_relaxed(val, pc->base + pc->data->regs.ctrl);
  155. if (!enable)
  156. clk_disable(pc->clk);
  157. return 0;
  158. }
  159. static int rockchip_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm,
  160. const struct pwm_state *state)
  161. {
  162. struct rockchip_pwm_chip *pc = to_rockchip_pwm_chip(chip);
  163. struct pwm_state curstate;
  164. bool enabled;
  165. int ret = 0;
  166. ret = clk_enable(pc->pclk);
  167. if (ret)
  168. return ret;
  169. ret = clk_enable(pc->clk);
  170. if (ret)
  171. return ret;
  172. pwm_get_state(pwm, &curstate);
  173. enabled = curstate.enabled;
  174. if (state->polarity != curstate.polarity && enabled &&
  175. !pc->data->supports_lock) {
  176. ret = rockchip_pwm_enable(chip, pwm, false);
  177. if (ret)
  178. goto out;
  179. enabled = false;
  180. }
  181. rockchip_pwm_config(chip, pwm, state);
  182. if (state->enabled != enabled) {
  183. ret = rockchip_pwm_enable(chip, pwm, state->enabled);
  184. if (ret)
  185. goto out;
  186. }
  187. out:
  188. clk_disable(pc->clk);
  189. clk_disable(pc->pclk);
  190. return ret;
  191. }
  192. static const struct pwm_ops rockchip_pwm_ops = {
  193. .get_state = rockchip_pwm_get_state,
  194. .apply = rockchip_pwm_apply,
  195. };
  196. static const struct rockchip_pwm_data pwm_data_v1 = {
  197. .regs = {
  198. .duty = 0x04,
  199. .period = 0x08,
  200. .cntr = 0x00,
  201. .ctrl = 0x0c,
  202. },
  203. .prescaler = 2,
  204. .supports_polarity = false,
  205. .supports_lock = false,
  206. .enable_conf = PWM_CTRL_OUTPUT_EN | PWM_CTRL_TIMER_EN,
  207. };
  208. static const struct rockchip_pwm_data pwm_data_v2 = {
  209. .regs = {
  210. .duty = 0x08,
  211. .period = 0x04,
  212. .cntr = 0x00,
  213. .ctrl = 0x0c,
  214. },
  215. .prescaler = 1,
  216. .supports_polarity = true,
  217. .supports_lock = false,
  218. .enable_conf = PWM_OUTPUT_LEFT | PWM_LP_DISABLE | PWM_ENABLE |
  219. PWM_CONTINUOUS,
  220. };
  221. static const struct rockchip_pwm_data pwm_data_vop = {
  222. .regs = {
  223. .duty = 0x08,
  224. .period = 0x04,
  225. .cntr = 0x0c,
  226. .ctrl = 0x00,
  227. },
  228. .prescaler = 1,
  229. .supports_polarity = true,
  230. .supports_lock = false,
  231. .enable_conf = PWM_OUTPUT_LEFT | PWM_LP_DISABLE | PWM_ENABLE |
  232. PWM_CONTINUOUS,
  233. };
  234. static const struct rockchip_pwm_data pwm_data_v3 = {
  235. .regs = {
  236. .duty = 0x08,
  237. .period = 0x04,
  238. .cntr = 0x00,
  239. .ctrl = 0x0c,
  240. },
  241. .prescaler = 1,
  242. .supports_polarity = true,
  243. .supports_lock = true,
  244. .enable_conf = PWM_OUTPUT_LEFT | PWM_LP_DISABLE | PWM_ENABLE |
  245. PWM_CONTINUOUS,
  246. };
  247. static const struct of_device_id rockchip_pwm_dt_ids[] = {
  248. { .compatible = "rockchip,rk2928-pwm", .data = &pwm_data_v1},
  249. { .compatible = "rockchip,rk3288-pwm", .data = &pwm_data_v2},
  250. { .compatible = "rockchip,vop-pwm", .data = &pwm_data_vop},
  251. { .compatible = "rockchip,rk3328-pwm", .data = &pwm_data_v3},
  252. { /* sentinel */ }
  253. };
  254. MODULE_DEVICE_TABLE(of, rockchip_pwm_dt_ids);
  255. static int rockchip_pwm_probe(struct platform_device *pdev)
  256. {
  257. struct pwm_chip *chip;
  258. struct rockchip_pwm_chip *pc;
  259. u32 enable_conf, ctrl;
  260. bool enabled;
  261. int ret, count;
  262. chip = devm_pwmchip_alloc(&pdev->dev, 1, sizeof(*pc));
  263. if (IS_ERR(chip))
  264. return PTR_ERR(chip);
  265. pc = to_rockchip_pwm_chip(chip);
  266. pc->base = devm_platform_ioremap_resource(pdev, 0);
  267. if (IS_ERR(pc->base))
  268. return PTR_ERR(pc->base);
  269. pc->clk = devm_clk_get(&pdev->dev, "pwm");
  270. if (IS_ERR(pc->clk)) {
  271. pc->clk = devm_clk_get(&pdev->dev, NULL);
  272. if (IS_ERR(pc->clk))
  273. return dev_err_probe(&pdev->dev, PTR_ERR(pc->clk),
  274. "Can't get PWM clk\n");
  275. }
  276. count = of_count_phandle_with_args(pdev->dev.of_node,
  277. "clocks", "#clock-cells");
  278. if (count == 2)
  279. pc->pclk = devm_clk_get(&pdev->dev, "pclk");
  280. else
  281. pc->pclk = pc->clk;
  282. if (IS_ERR(pc->pclk))
  283. return dev_err_probe(&pdev->dev, PTR_ERR(pc->pclk), "Can't get APB clk\n");
  284. ret = clk_prepare_enable(pc->clk);
  285. if (ret)
  286. return dev_err_probe(&pdev->dev, ret, "Can't prepare enable PWM clk\n");
  287. ret = clk_prepare_enable(pc->pclk);
  288. if (ret) {
  289. dev_err_probe(&pdev->dev, ret, "Can't prepare enable APB clk\n");
  290. goto err_clk;
  291. }
  292. platform_set_drvdata(pdev, chip);
  293. pc->data = device_get_match_data(&pdev->dev);
  294. chip->ops = &rockchip_pwm_ops;
  295. enable_conf = pc->data->enable_conf;
  296. ctrl = readl_relaxed(pc->base + pc->data->regs.ctrl);
  297. enabled = (ctrl & enable_conf) == enable_conf;
  298. ret = pwmchip_add(chip);
  299. if (ret < 0) {
  300. dev_err_probe(&pdev->dev, ret, "pwmchip_add() failed\n");
  301. goto err_pclk;
  302. }
  303. /* Keep the PWM clk enabled if the PWM appears to be up and running. */
  304. if (!enabled)
  305. clk_disable(pc->clk);
  306. clk_disable(pc->pclk);
  307. return 0;
  308. err_pclk:
  309. clk_disable_unprepare(pc->pclk);
  310. err_clk:
  311. clk_disable_unprepare(pc->clk);
  312. return ret;
  313. }
  314. static void rockchip_pwm_remove(struct platform_device *pdev)
  315. {
  316. struct pwm_chip *chip = platform_get_drvdata(pdev);
  317. struct rockchip_pwm_chip *pc = to_rockchip_pwm_chip(chip);
  318. pwmchip_remove(chip);
  319. clk_unprepare(pc->pclk);
  320. clk_unprepare(pc->clk);
  321. }
  322. static struct platform_driver rockchip_pwm_driver = {
  323. .driver = {
  324. .name = "rockchip-pwm",
  325. .of_match_table = rockchip_pwm_dt_ids,
  326. },
  327. .probe = rockchip_pwm_probe,
  328. .remove = rockchip_pwm_remove,
  329. };
  330. module_platform_driver(rockchip_pwm_driver);
  331. MODULE_AUTHOR("Beniamino Galvani <b.galvani@gmail.com>");
  332. MODULE_DESCRIPTION("Rockchip SoC PWM driver");
  333. MODULE_LICENSE("GPL v2");