pwm-rockchip.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  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_enable(struct pwm_chip *chip,
  158. struct pwm_device *pwm,
  159. bool enable,
  160. enum pwm_polarity polarity)
  161. {
  162. struct rockchip_pwm_chip *pc = to_rockchip_pwm_chip(chip);
  163. int ret;
  164. if (enable) {
  165. ret = clk_enable(pc->clk);
  166. if (ret)
  167. return ret;
  168. }
  169. pc->data->set_enable(chip, pwm, enable, polarity);
  170. if (!enable)
  171. clk_disable(pc->clk);
  172. return 0;
  173. }
  174. static int rockchip_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm,
  175. struct pwm_state *state)
  176. {
  177. struct rockchip_pwm_chip *pc = to_rockchip_pwm_chip(chip);
  178. struct pwm_state curstate;
  179. bool enabled;
  180. int ret;
  181. pwm_get_state(pwm, &curstate);
  182. enabled = curstate.enabled;
  183. ret = clk_enable(pc->clk);
  184. if (ret)
  185. return ret;
  186. if (state->polarity != curstate.polarity && enabled) {
  187. ret = rockchip_pwm_enable(chip, pwm, false, state->polarity);
  188. if (ret)
  189. goto out;
  190. enabled = false;
  191. }
  192. ret = rockchip_pwm_config(chip, pwm, state->duty_cycle, state->period);
  193. if (ret) {
  194. if (enabled != curstate.enabled)
  195. rockchip_pwm_enable(chip, pwm, !enabled,
  196. state->polarity);
  197. goto out;
  198. }
  199. if (state->enabled != enabled) {
  200. ret = rockchip_pwm_enable(chip, pwm, state->enabled,
  201. state->polarity);
  202. if (ret)
  203. goto out;
  204. }
  205. /*
  206. * Update the state with the real hardware, which can differ a bit
  207. * because of period/duty_cycle approximation.
  208. */
  209. rockchip_pwm_get_state(chip, pwm, state);
  210. out:
  211. clk_disable(pc->clk);
  212. return ret;
  213. }
  214. static const struct pwm_ops rockchip_pwm_ops_v1 = {
  215. .get_state = rockchip_pwm_get_state,
  216. .apply = rockchip_pwm_apply,
  217. .owner = THIS_MODULE,
  218. };
  219. static const struct pwm_ops rockchip_pwm_ops_v2 = {
  220. .get_state = rockchip_pwm_get_state,
  221. .apply = rockchip_pwm_apply,
  222. .owner = THIS_MODULE,
  223. };
  224. static const struct rockchip_pwm_data pwm_data_v1 = {
  225. .regs = {
  226. .duty = 0x04,
  227. .period = 0x08,
  228. .cntr = 0x00,
  229. .ctrl = 0x0c,
  230. },
  231. .prescaler = 2,
  232. .ops = &rockchip_pwm_ops_v1,
  233. .set_enable = rockchip_pwm_set_enable_v1,
  234. .get_state = rockchip_pwm_get_state_v1,
  235. };
  236. static const struct rockchip_pwm_data pwm_data_v2 = {
  237. .regs = {
  238. .duty = 0x08,
  239. .period = 0x04,
  240. .cntr = 0x00,
  241. .ctrl = 0x0c,
  242. },
  243. .prescaler = 1,
  244. .supports_polarity = true,
  245. .ops = &rockchip_pwm_ops_v2,
  246. .set_enable = rockchip_pwm_set_enable_v2,
  247. .get_state = rockchip_pwm_get_state_v2,
  248. };
  249. static const struct rockchip_pwm_data pwm_data_vop = {
  250. .regs = {
  251. .duty = 0x08,
  252. .period = 0x04,
  253. .cntr = 0x0c,
  254. .ctrl = 0x00,
  255. },
  256. .prescaler = 1,
  257. .supports_polarity = true,
  258. .ops = &rockchip_pwm_ops_v2,
  259. .set_enable = rockchip_pwm_set_enable_v2,
  260. .get_state = rockchip_pwm_get_state_v2,
  261. };
  262. static const struct of_device_id rockchip_pwm_dt_ids[] = {
  263. { .compatible = "rockchip,rk2928-pwm", .data = &pwm_data_v1},
  264. { .compatible = "rockchip,rk3288-pwm", .data = &pwm_data_v2},
  265. { .compatible = "rockchip,vop-pwm", .data = &pwm_data_vop},
  266. { /* sentinel */ }
  267. };
  268. MODULE_DEVICE_TABLE(of, rockchip_pwm_dt_ids);
  269. static int rockchip_pwm_probe(struct platform_device *pdev)
  270. {
  271. const struct of_device_id *id;
  272. struct rockchip_pwm_chip *pc;
  273. struct resource *r;
  274. int ret;
  275. id = of_match_device(rockchip_pwm_dt_ids, &pdev->dev);
  276. if (!id)
  277. return -EINVAL;
  278. pc = devm_kzalloc(&pdev->dev, sizeof(*pc), GFP_KERNEL);
  279. if (!pc)
  280. return -ENOMEM;
  281. r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  282. pc->base = devm_ioremap_resource(&pdev->dev, r);
  283. if (IS_ERR(pc->base))
  284. return PTR_ERR(pc->base);
  285. pc->clk = devm_clk_get(&pdev->dev, NULL);
  286. if (IS_ERR(pc->clk))
  287. return PTR_ERR(pc->clk);
  288. ret = clk_prepare_enable(pc->clk);
  289. if (ret)
  290. return ret;
  291. platform_set_drvdata(pdev, pc);
  292. pc->data = id->data;
  293. pc->chip.dev = &pdev->dev;
  294. pc->chip.ops = pc->data->ops;
  295. pc->chip.base = -1;
  296. pc->chip.npwm = 1;
  297. if (pc->data->supports_polarity) {
  298. pc->chip.of_xlate = of_pwm_xlate_with_flags;
  299. pc->chip.of_pwm_n_cells = 3;
  300. }
  301. ret = pwmchip_add(&pc->chip);
  302. if (ret < 0) {
  303. clk_unprepare(pc->clk);
  304. dev_err(&pdev->dev, "pwmchip_add() failed: %d\n", ret);
  305. }
  306. /* Keep the PWM clk enabled if the PWM appears to be up and running. */
  307. if (!pwm_is_enabled(pc->chip.pwms))
  308. clk_disable(pc->clk);
  309. return ret;
  310. }
  311. static int rockchip_pwm_remove(struct platform_device *pdev)
  312. {
  313. struct rockchip_pwm_chip *pc = platform_get_drvdata(pdev);
  314. /*
  315. * Disable the PWM clk before unpreparing it if the PWM device is still
  316. * running. This should only happen when the last PWM user left it
  317. * enabled, or when nobody requested a PWM that was previously enabled
  318. * by the bootloader.
  319. *
  320. * FIXME: Maybe the core should disable all PWM devices in
  321. * pwmchip_remove(). In this case we'd only have to call
  322. * clk_unprepare() after pwmchip_remove().
  323. *
  324. */
  325. if (pwm_is_enabled(pc->chip.pwms))
  326. clk_disable(pc->clk);
  327. clk_unprepare(pc->clk);
  328. return pwmchip_remove(&pc->chip);
  329. }
  330. static struct platform_driver rockchip_pwm_driver = {
  331. .driver = {
  332. .name = "rockchip-pwm",
  333. .of_match_table = rockchip_pwm_dt_ids,
  334. },
  335. .probe = rockchip_pwm_probe,
  336. .remove = rockchip_pwm_remove,
  337. };
  338. module_platform_driver(rockchip_pwm_driver);
  339. MODULE_AUTHOR("Beniamino Galvani <b.galvani@gmail.com>");
  340. MODULE_DESCRIPTION("Rockchip SoC PWM driver");
  341. MODULE_LICENSE("GPL v2");