pwm-rockchip.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  1. /*
  2. * PWM driver for Rockchip SoCs
  3. *
  4. * Copyright (C) 2014 Beniamino Galvani <b.galvani@gmail.com>
  5. * Copyright (C) 2014 ROCKCHIP, Inc.
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License
  9. * version 2 as published by the Free Software Foundation.
  10. */
  11. #include <linux/clk.h>
  12. #include <linux/io.h>
  13. #include <linux/module.h>
  14. #include <linux/of.h>
  15. #include <linux/of_device.h>
  16. #include <linux/platform_device.h>
  17. #include <linux/pwm.h>
  18. #include <linux/time.h>
  19. #define PWM_CTRL_TIMER_EN (1 << 0)
  20. #define PWM_CTRL_OUTPUT_EN (1 << 3)
  21. #define PWM_ENABLE (1 << 0)
  22. #define PWM_CONTINUOUS (1 << 1)
  23. #define PWM_DUTY_POSITIVE (1 << 3)
  24. #define PWM_DUTY_NEGATIVE (0 << 3)
  25. #define PWM_INACTIVE_NEGATIVE (0 << 4)
  26. #define PWM_INACTIVE_POSITIVE (1 << 4)
  27. #define PWM_OUTPUT_LEFT (0 << 5)
  28. #define PWM_LP_DISABLE (0 << 8)
  29. struct rockchip_pwm_chip {
  30. struct pwm_chip chip;
  31. struct clk *clk;
  32. const struct rockchip_pwm_data *data;
  33. void __iomem *base;
  34. };
  35. struct rockchip_pwm_regs {
  36. unsigned long duty;
  37. unsigned long period;
  38. unsigned long cntr;
  39. unsigned long ctrl;
  40. };
  41. struct rockchip_pwm_data {
  42. struct rockchip_pwm_regs regs;
  43. unsigned int prescaler;
  44. bool supports_polarity;
  45. const struct pwm_ops *ops;
  46. void (*set_enable)(struct pwm_chip *chip,
  47. struct pwm_device *pwm, bool enable,
  48. enum pwm_polarity polarity);
  49. void (*get_state)(struct pwm_chip *chip, struct pwm_device *pwm,
  50. struct pwm_state *state);
  51. };
  52. static inline struct rockchip_pwm_chip *to_rockchip_pwm_chip(struct pwm_chip *c)
  53. {
  54. return container_of(c, struct rockchip_pwm_chip, chip);
  55. }
  56. static void rockchip_pwm_set_enable_v1(struct pwm_chip *chip,
  57. struct pwm_device *pwm, bool enable,
  58. enum pwm_polarity polarity)
  59. {
  60. struct rockchip_pwm_chip *pc = to_rockchip_pwm_chip(chip);
  61. u32 enable_conf = PWM_CTRL_OUTPUT_EN | PWM_CTRL_TIMER_EN;
  62. u32 val;
  63. val = readl_relaxed(pc->base + pc->data->regs.ctrl);
  64. if (enable)
  65. val |= enable_conf;
  66. else
  67. val &= ~enable_conf;
  68. writel_relaxed(val, pc->base + pc->data->regs.ctrl);
  69. }
  70. static void rockchip_pwm_get_state_v1(struct pwm_chip *chip,
  71. struct pwm_device *pwm,
  72. struct pwm_state *state)
  73. {
  74. struct rockchip_pwm_chip *pc = to_rockchip_pwm_chip(chip);
  75. u32 enable_conf = PWM_CTRL_OUTPUT_EN | PWM_CTRL_TIMER_EN;
  76. u32 val;
  77. val = readl_relaxed(pc->base + pc->data->regs.ctrl);
  78. if ((val & enable_conf) == enable_conf)
  79. state->enabled = true;
  80. }
  81. static void rockchip_pwm_set_enable_v2(struct pwm_chip *chip,
  82. struct pwm_device *pwm, bool enable,
  83. enum pwm_polarity polarity)
  84. {
  85. struct rockchip_pwm_chip *pc = to_rockchip_pwm_chip(chip);
  86. u32 enable_conf = PWM_OUTPUT_LEFT | PWM_LP_DISABLE | PWM_ENABLE |
  87. PWM_CONTINUOUS;
  88. u32 val;
  89. if (polarity == PWM_POLARITY_INVERSED)
  90. enable_conf |= PWM_DUTY_NEGATIVE | PWM_INACTIVE_POSITIVE;
  91. else
  92. enable_conf |= PWM_DUTY_POSITIVE | PWM_INACTIVE_NEGATIVE;
  93. val = readl_relaxed(pc->base + pc->data->regs.ctrl);
  94. if (enable)
  95. val |= enable_conf;
  96. else
  97. val &= ~enable_conf;
  98. writel_relaxed(val, pc->base + pc->data->regs.ctrl);
  99. }
  100. static void rockchip_pwm_get_state_v2(struct pwm_chip *chip,
  101. struct pwm_device *pwm,
  102. struct pwm_state *state)
  103. {
  104. struct rockchip_pwm_chip *pc = to_rockchip_pwm_chip(chip);
  105. u32 enable_conf = PWM_OUTPUT_LEFT | PWM_LP_DISABLE | PWM_ENABLE |
  106. PWM_CONTINUOUS;
  107. u32 val;
  108. val = readl_relaxed(pc->base + pc->data->regs.ctrl);
  109. if ((val & enable_conf) != enable_conf)
  110. return;
  111. state->enabled = true;
  112. if (!(val & PWM_DUTY_POSITIVE))
  113. state->polarity = PWM_POLARITY_INVERSED;
  114. }
  115. static void rockchip_pwm_get_state(struct pwm_chip *chip,
  116. struct pwm_device *pwm,
  117. struct pwm_state *state)
  118. {
  119. struct rockchip_pwm_chip *pc = to_rockchip_pwm_chip(chip);
  120. unsigned long clk_rate;
  121. u64 tmp;
  122. int ret;
  123. ret = clk_enable(pc->clk);
  124. if (ret)
  125. return;
  126. clk_rate = clk_get_rate(pc->clk);
  127. tmp = readl_relaxed(pc->base + pc->data->regs.period);
  128. tmp *= pc->data->prescaler * NSEC_PER_SEC;
  129. state->period = DIV_ROUND_CLOSEST_ULL(tmp, clk_rate);
  130. tmp = readl_relaxed(pc->base + pc->data->regs.duty);
  131. tmp *= pc->data->prescaler * NSEC_PER_SEC;
  132. state->duty_cycle = DIV_ROUND_CLOSEST_ULL(tmp, clk_rate);
  133. pc->data->get_state(chip, pwm, state);
  134. clk_disable(pc->clk);
  135. }
  136. static int rockchip_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm,
  137. int duty_ns, int period_ns)
  138. {
  139. struct rockchip_pwm_chip *pc = to_rockchip_pwm_chip(chip);
  140. unsigned long period, duty;
  141. u64 clk_rate, div;
  142. clk_rate = clk_get_rate(pc->clk);
  143. /*
  144. * Since period and duty cycle registers have a width of 32
  145. * bits, every possible input period can be obtained using the
  146. * default prescaler value for all practical clock rate values.
  147. */
  148. div = clk_rate * period_ns;
  149. period = DIV_ROUND_CLOSEST_ULL(div,
  150. pc->data->prescaler * NSEC_PER_SEC);
  151. div = clk_rate * duty_ns;
  152. duty = DIV_ROUND_CLOSEST_ULL(div, pc->data->prescaler * NSEC_PER_SEC);
  153. writel(period, pc->base + pc->data->regs.period);
  154. writel(duty, pc->base + pc->data->regs.duty);
  155. return 0;
  156. }
  157. static int rockchip_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm,
  158. struct pwm_state *state)
  159. {
  160. struct rockchip_pwm_chip *pc = to_rockchip_pwm_chip(chip);
  161. struct pwm_state curstate;
  162. bool enabled;
  163. int ret;
  164. pwm_get_state(pwm, &curstate);
  165. enabled = curstate.enabled;
  166. ret = clk_enable(pc->clk);
  167. if (ret)
  168. return ret;
  169. if (state->polarity != curstate.polarity && enabled) {
  170. pc->data->set_enable(chip, pwm, false, state->polarity);
  171. enabled = false;
  172. }
  173. ret = rockchip_pwm_config(chip, pwm, state->duty_cycle, state->period);
  174. if (ret) {
  175. if (enabled != curstate.enabled)
  176. pc->data->set_enable(chip, pwm, !enabled,
  177. state->polarity);
  178. goto out;
  179. }
  180. if (state->enabled != enabled)
  181. pc->data->set_enable(chip, pwm, state->enabled,
  182. state->polarity);
  183. /*
  184. * Update the state with the real hardware, which can differ a bit
  185. * because of period/duty_cycle approximation.
  186. */
  187. rockchip_pwm_get_state(chip, pwm, state);
  188. out:
  189. clk_disable(pc->clk);
  190. return ret;
  191. }
  192. static const struct pwm_ops rockchip_pwm_ops_v1 = {
  193. .get_state = rockchip_pwm_get_state,
  194. .apply = rockchip_pwm_apply,
  195. .owner = THIS_MODULE,
  196. };
  197. static const struct pwm_ops rockchip_pwm_ops_v2 = {
  198. .get_state = rockchip_pwm_get_state,
  199. .apply = rockchip_pwm_apply,
  200. .owner = THIS_MODULE,
  201. };
  202. static const struct rockchip_pwm_data pwm_data_v1 = {
  203. .regs = {
  204. .duty = 0x04,
  205. .period = 0x08,
  206. .cntr = 0x00,
  207. .ctrl = 0x0c,
  208. },
  209. .prescaler = 2,
  210. .ops = &rockchip_pwm_ops_v1,
  211. .set_enable = rockchip_pwm_set_enable_v1,
  212. .get_state = rockchip_pwm_get_state_v1,
  213. };
  214. static const struct rockchip_pwm_data pwm_data_v2 = {
  215. .regs = {
  216. .duty = 0x08,
  217. .period = 0x04,
  218. .cntr = 0x00,
  219. .ctrl = 0x0c,
  220. },
  221. .prescaler = 1,
  222. .supports_polarity = true,
  223. .ops = &rockchip_pwm_ops_v2,
  224. .set_enable = rockchip_pwm_set_enable_v2,
  225. .get_state = rockchip_pwm_get_state_v2,
  226. };
  227. static const struct rockchip_pwm_data pwm_data_vop = {
  228. .regs = {
  229. .duty = 0x08,
  230. .period = 0x04,
  231. .cntr = 0x0c,
  232. .ctrl = 0x00,
  233. },
  234. .prescaler = 1,
  235. .supports_polarity = true,
  236. .ops = &rockchip_pwm_ops_v2,
  237. .set_enable = rockchip_pwm_set_enable_v2,
  238. .get_state = rockchip_pwm_get_state_v2,
  239. };
  240. static const struct of_device_id rockchip_pwm_dt_ids[] = {
  241. { .compatible = "rockchip,rk2928-pwm", .data = &pwm_data_v1},
  242. { .compatible = "rockchip,rk3288-pwm", .data = &pwm_data_v2},
  243. { .compatible = "rockchip,vop-pwm", .data = &pwm_data_vop},
  244. { /* sentinel */ }
  245. };
  246. MODULE_DEVICE_TABLE(of, rockchip_pwm_dt_ids);
  247. static int rockchip_pwm_probe(struct platform_device *pdev)
  248. {
  249. const struct of_device_id *id;
  250. struct rockchip_pwm_chip *pc;
  251. struct resource *r;
  252. int ret;
  253. id = of_match_device(rockchip_pwm_dt_ids, &pdev->dev);
  254. if (!id)
  255. return -EINVAL;
  256. pc = devm_kzalloc(&pdev->dev, sizeof(*pc), GFP_KERNEL);
  257. if (!pc)
  258. return -ENOMEM;
  259. r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  260. pc->base = devm_ioremap_resource(&pdev->dev, r);
  261. if (IS_ERR(pc->base))
  262. return PTR_ERR(pc->base);
  263. pc->clk = devm_clk_get(&pdev->dev, NULL);
  264. if (IS_ERR(pc->clk))
  265. return PTR_ERR(pc->clk);
  266. ret = clk_prepare_enable(pc->clk);
  267. if (ret)
  268. return ret;
  269. platform_set_drvdata(pdev, pc);
  270. pc->data = id->data;
  271. pc->chip.dev = &pdev->dev;
  272. pc->chip.ops = pc->data->ops;
  273. pc->chip.base = -1;
  274. pc->chip.npwm = 1;
  275. if (pc->data->supports_polarity) {
  276. pc->chip.of_xlate = of_pwm_xlate_with_flags;
  277. pc->chip.of_pwm_n_cells = 3;
  278. }
  279. ret = pwmchip_add(&pc->chip);
  280. if (ret < 0) {
  281. clk_unprepare(pc->clk);
  282. dev_err(&pdev->dev, "pwmchip_add() failed: %d\n", ret);
  283. }
  284. /* Keep the PWM clk enabled if the PWM appears to be up and running. */
  285. if (!pwm_is_enabled(pc->chip.pwms))
  286. clk_disable(pc->clk);
  287. return ret;
  288. }
  289. static int rockchip_pwm_remove(struct platform_device *pdev)
  290. {
  291. struct rockchip_pwm_chip *pc = platform_get_drvdata(pdev);
  292. /*
  293. * Disable the PWM clk before unpreparing it if the PWM device is still
  294. * running. This should only happen when the last PWM user left it
  295. * enabled, or when nobody requested a PWM that was previously enabled
  296. * by the bootloader.
  297. *
  298. * FIXME: Maybe the core should disable all PWM devices in
  299. * pwmchip_remove(). In this case we'd only have to call
  300. * clk_unprepare() after pwmchip_remove().
  301. *
  302. */
  303. if (pwm_is_enabled(pc->chip.pwms))
  304. clk_disable(pc->clk);
  305. clk_unprepare(pc->clk);
  306. return pwmchip_remove(&pc->chip);
  307. }
  308. static struct platform_driver rockchip_pwm_driver = {
  309. .driver = {
  310. .name = "rockchip-pwm",
  311. .of_match_table = rockchip_pwm_dt_ids,
  312. },
  313. .probe = rockchip_pwm_probe,
  314. .remove = rockchip_pwm_remove,
  315. };
  316. module_platform_driver(rockchip_pwm_driver);
  317. MODULE_AUTHOR("Beniamino Galvani <b.galvani@gmail.com>");
  318. MODULE_DESCRIPTION("Rockchip SoC PWM driver");
  319. MODULE_LICENSE("GPL v2");