pwm-rockchip.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  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_POLARITY_MASK (PWM_DUTY_POSITIVE | PWM_INACTIVE_POSITIVE)
  28. #define PWM_OUTPUT_LEFT (0 << 5)
  29. #define PWM_LOCK_EN (1 << 6)
  30. #define PWM_LP_DISABLE (0 << 8)
  31. struct rockchip_pwm_chip {
  32. struct pwm_chip chip;
  33. struct clk *clk;
  34. struct clk *pclk;
  35. const struct rockchip_pwm_data *data;
  36. void __iomem *base;
  37. };
  38. struct rockchip_pwm_regs {
  39. unsigned long duty;
  40. unsigned long period;
  41. unsigned long cntr;
  42. unsigned long ctrl;
  43. };
  44. struct rockchip_pwm_data {
  45. struct rockchip_pwm_regs regs;
  46. unsigned int prescaler;
  47. bool supports_polarity;
  48. bool supports_lock;
  49. u32 enable_conf;
  50. };
  51. static inline struct rockchip_pwm_chip *to_rockchip_pwm_chip(struct pwm_chip *c)
  52. {
  53. return container_of(c, struct rockchip_pwm_chip, chip);
  54. }
  55. static void rockchip_pwm_get_state(struct pwm_chip *chip,
  56. struct pwm_device *pwm,
  57. struct pwm_state *state)
  58. {
  59. struct rockchip_pwm_chip *pc = to_rockchip_pwm_chip(chip);
  60. u32 enable_conf = pc->data->enable_conf;
  61. unsigned long clk_rate;
  62. u64 tmp;
  63. u32 val;
  64. int ret;
  65. ret = clk_enable(pc->pclk);
  66. if (ret)
  67. return;
  68. clk_rate = clk_get_rate(pc->clk);
  69. tmp = readl_relaxed(pc->base + pc->data->regs.period);
  70. tmp *= pc->data->prescaler * NSEC_PER_SEC;
  71. state->period = DIV_ROUND_CLOSEST_ULL(tmp, clk_rate);
  72. tmp = readl_relaxed(pc->base + pc->data->regs.duty);
  73. tmp *= pc->data->prescaler * NSEC_PER_SEC;
  74. state->duty_cycle = DIV_ROUND_CLOSEST_ULL(tmp, clk_rate);
  75. val = readl_relaxed(pc->base + pc->data->regs.ctrl);
  76. if (pc->data->supports_polarity)
  77. state->enabled = ((val & enable_conf) != enable_conf) ?
  78. false : true;
  79. else
  80. state->enabled = ((val & enable_conf) == enable_conf) ?
  81. true : false;
  82. if (pc->data->supports_polarity) {
  83. if (!(val & PWM_DUTY_POSITIVE))
  84. state->polarity = PWM_POLARITY_INVERSED;
  85. }
  86. clk_disable(pc->pclk);
  87. }
  88. static void rockchip_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm,
  89. struct pwm_state *state)
  90. {
  91. struct rockchip_pwm_chip *pc = to_rockchip_pwm_chip(chip);
  92. unsigned long period, duty;
  93. u64 clk_rate, div;
  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. div = clk_rate * state->period;
  102. period = DIV_ROUND_CLOSEST_ULL(div,
  103. pc->data->prescaler * NSEC_PER_SEC);
  104. div = clk_rate * state->duty_cycle;
  105. duty = DIV_ROUND_CLOSEST_ULL(div, pc->data->prescaler * NSEC_PER_SEC);
  106. /*
  107. * Lock the period and duty of previous configuration, then
  108. * change the duty and period, that would not be effective.
  109. */
  110. ctrl = readl_relaxed(pc->base + pc->data->regs.ctrl);
  111. if (pc->data->supports_lock) {
  112. ctrl |= PWM_LOCK_EN;
  113. writel_relaxed(ctrl, pc->base + pc->data->regs.ctrl);
  114. }
  115. writel(period, pc->base + pc->data->regs.period);
  116. writel(duty, pc->base + pc->data->regs.duty);
  117. if (pc->data->supports_polarity) {
  118. ctrl &= ~PWM_POLARITY_MASK;
  119. if (state->polarity == PWM_POLARITY_INVERSED)
  120. ctrl |= PWM_DUTY_NEGATIVE | PWM_INACTIVE_POSITIVE;
  121. else
  122. ctrl |= PWM_DUTY_POSITIVE | PWM_INACTIVE_NEGATIVE;
  123. }
  124. /*
  125. * Unlock and set polarity at the same time,
  126. * the configuration of duty, period and polarity
  127. * would be effective together at next period.
  128. */
  129. if (pc->data->supports_lock)
  130. ctrl &= ~PWM_LOCK_EN;
  131. writel(ctrl, pc->base + pc->data->regs.ctrl);
  132. }
  133. static int rockchip_pwm_enable(struct pwm_chip *chip,
  134. struct pwm_device *pwm,
  135. bool enable)
  136. {
  137. struct rockchip_pwm_chip *pc = to_rockchip_pwm_chip(chip);
  138. u32 enable_conf = pc->data->enable_conf;
  139. int ret;
  140. u32 val;
  141. if (enable) {
  142. ret = clk_enable(pc->clk);
  143. if (ret)
  144. return ret;
  145. }
  146. val = readl_relaxed(pc->base + pc->data->regs.ctrl);
  147. if (enable)
  148. val |= enable_conf;
  149. else
  150. val &= ~enable_conf;
  151. writel_relaxed(val, pc->base + pc->data->regs.ctrl);
  152. if (!enable)
  153. clk_disable(pc->clk);
  154. return 0;
  155. }
  156. static int rockchip_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm,
  157. struct pwm_state *state)
  158. {
  159. struct rockchip_pwm_chip *pc = to_rockchip_pwm_chip(chip);
  160. struct pwm_state curstate;
  161. bool enabled;
  162. int ret = 0;
  163. ret = clk_enable(pc->pclk);
  164. if (ret)
  165. return ret;
  166. pwm_get_state(pwm, &curstate);
  167. enabled = curstate.enabled;
  168. if (state->polarity != curstate.polarity && enabled &&
  169. !pc->data->supports_lock) {
  170. ret = rockchip_pwm_enable(chip, pwm, false);
  171. if (ret)
  172. goto out;
  173. enabled = false;
  174. }
  175. rockchip_pwm_config(chip, pwm, state);
  176. if (state->enabled != enabled) {
  177. ret = rockchip_pwm_enable(chip, pwm, state->enabled);
  178. if (ret)
  179. goto out;
  180. }
  181. /*
  182. * Update the state with the real hardware, which can differ a bit
  183. * because of period/duty_cycle approximation.
  184. */
  185. rockchip_pwm_get_state(chip, pwm, state);
  186. out:
  187. clk_disable(pc->pclk);
  188. return ret;
  189. }
  190. static const struct pwm_ops rockchip_pwm_ops = {
  191. .get_state = rockchip_pwm_get_state,
  192. .apply = rockchip_pwm_apply,
  193. .owner = THIS_MODULE,
  194. };
  195. static const struct rockchip_pwm_data pwm_data_v1 = {
  196. .regs = {
  197. .duty = 0x04,
  198. .period = 0x08,
  199. .cntr = 0x00,
  200. .ctrl = 0x0c,
  201. },
  202. .prescaler = 2,
  203. .supports_polarity = false,
  204. .supports_lock = false,
  205. .enable_conf = PWM_CTRL_OUTPUT_EN | PWM_CTRL_TIMER_EN,
  206. };
  207. static const struct rockchip_pwm_data pwm_data_v2 = {
  208. .regs = {
  209. .duty = 0x08,
  210. .period = 0x04,
  211. .cntr = 0x00,
  212. .ctrl = 0x0c,
  213. },
  214. .prescaler = 1,
  215. .supports_polarity = true,
  216. .supports_lock = false,
  217. .enable_conf = PWM_OUTPUT_LEFT | PWM_LP_DISABLE | PWM_ENABLE |
  218. PWM_CONTINUOUS,
  219. };
  220. static const struct rockchip_pwm_data pwm_data_vop = {
  221. .regs = {
  222. .duty = 0x08,
  223. .period = 0x04,
  224. .cntr = 0x0c,
  225. .ctrl = 0x00,
  226. },
  227. .prescaler = 1,
  228. .supports_polarity = true,
  229. .supports_lock = false,
  230. .enable_conf = PWM_OUTPUT_LEFT | PWM_LP_DISABLE | PWM_ENABLE |
  231. PWM_CONTINUOUS,
  232. };
  233. static const struct rockchip_pwm_data pwm_data_v3 = {
  234. .regs = {
  235. .duty = 0x08,
  236. .period = 0x04,
  237. .cntr = 0x00,
  238. .ctrl = 0x0c,
  239. },
  240. .prescaler = 1,
  241. .supports_polarity = true,
  242. .supports_lock = true,
  243. .enable_conf = PWM_OUTPUT_LEFT | PWM_LP_DISABLE | PWM_ENABLE |
  244. PWM_CONTINUOUS,
  245. };
  246. static const struct of_device_id rockchip_pwm_dt_ids[] = {
  247. { .compatible = "rockchip,rk2928-pwm", .data = &pwm_data_v1},
  248. { .compatible = "rockchip,rk3288-pwm", .data = &pwm_data_v2},
  249. { .compatible = "rockchip,vop-pwm", .data = &pwm_data_vop},
  250. { .compatible = "rockchip,rk3328-pwm", .data = &pwm_data_v3},
  251. { /* sentinel */ }
  252. };
  253. MODULE_DEVICE_TABLE(of, rockchip_pwm_dt_ids);
  254. static int rockchip_pwm_probe(struct platform_device *pdev)
  255. {
  256. const struct of_device_id *id;
  257. struct rockchip_pwm_chip *pc;
  258. struct resource *r;
  259. int ret, count;
  260. id = of_match_device(rockchip_pwm_dt_ids, &pdev->dev);
  261. if (!id)
  262. return -EINVAL;
  263. pc = devm_kzalloc(&pdev->dev, sizeof(*pc), GFP_KERNEL);
  264. if (!pc)
  265. return -ENOMEM;
  266. r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  267. pc->base = devm_ioremap_resource(&pdev->dev, r);
  268. if (IS_ERR(pc->base))
  269. return PTR_ERR(pc->base);
  270. pc->clk = devm_clk_get(&pdev->dev, "pwm");
  271. if (IS_ERR(pc->clk)) {
  272. pc->clk = devm_clk_get(&pdev->dev, NULL);
  273. if (IS_ERR(pc->clk)) {
  274. ret = PTR_ERR(pc->clk);
  275. if (ret != -EPROBE_DEFER)
  276. dev_err(&pdev->dev, "Can't get bus clk: %d\n",
  277. ret);
  278. return ret;
  279. }
  280. }
  281. count = of_count_phandle_with_args(pdev->dev.of_node,
  282. "clocks", "#clock-cells");
  283. if (count == 2)
  284. pc->pclk = devm_clk_get(&pdev->dev, "pclk");
  285. else
  286. pc->pclk = pc->clk;
  287. if (IS_ERR(pc->pclk)) {
  288. ret = PTR_ERR(pc->pclk);
  289. if (ret != -EPROBE_DEFER)
  290. dev_err(&pdev->dev, "Can't get APB clk: %d\n", ret);
  291. return ret;
  292. }
  293. ret = clk_prepare_enable(pc->clk);
  294. if (ret) {
  295. dev_err(&pdev->dev, "Can't prepare enable bus clk: %d\n", ret);
  296. return ret;
  297. }
  298. ret = clk_prepare(pc->pclk);
  299. if (ret) {
  300. dev_err(&pdev->dev, "Can't prepare APB clk: %d\n", ret);
  301. goto err_clk;
  302. }
  303. platform_set_drvdata(pdev, pc);
  304. pc->data = id->data;
  305. pc->chip.dev = &pdev->dev;
  306. pc->chip.ops = &rockchip_pwm_ops;
  307. pc->chip.base = -1;
  308. pc->chip.npwm = 1;
  309. if (pc->data->supports_polarity) {
  310. pc->chip.of_xlate = of_pwm_xlate_with_flags;
  311. pc->chip.of_pwm_n_cells = 3;
  312. }
  313. ret = pwmchip_add(&pc->chip);
  314. if (ret < 0) {
  315. dev_err(&pdev->dev, "pwmchip_add() failed: %d\n", ret);
  316. goto err_pclk;
  317. }
  318. /* Keep the PWM clk enabled if the PWM appears to be up and running. */
  319. if (!pwm_is_enabled(pc->chip.pwms))
  320. clk_disable(pc->clk);
  321. return 0;
  322. err_pclk:
  323. clk_unprepare(pc->pclk);
  324. err_clk:
  325. clk_disable_unprepare(pc->clk);
  326. return ret;
  327. }
  328. static int rockchip_pwm_remove(struct platform_device *pdev)
  329. {
  330. struct rockchip_pwm_chip *pc = platform_get_drvdata(pdev);
  331. /*
  332. * Disable the PWM clk before unpreparing it if the PWM device is still
  333. * running. This should only happen when the last PWM user left it
  334. * enabled, or when nobody requested a PWM that was previously enabled
  335. * by the bootloader.
  336. *
  337. * FIXME: Maybe the core should disable all PWM devices in
  338. * pwmchip_remove(). In this case we'd only have to call
  339. * clk_unprepare() after pwmchip_remove().
  340. *
  341. */
  342. if (pwm_is_enabled(pc->chip.pwms))
  343. clk_disable(pc->clk);
  344. clk_unprepare(pc->pclk);
  345. clk_unprepare(pc->clk);
  346. return pwmchip_remove(&pc->chip);
  347. }
  348. static struct platform_driver rockchip_pwm_driver = {
  349. .driver = {
  350. .name = "rockchip-pwm",
  351. .of_match_table = rockchip_pwm_dt_ids,
  352. },
  353. .probe = rockchip_pwm_probe,
  354. .remove = rockchip_pwm_remove,
  355. };
  356. module_platform_driver(rockchip_pwm_driver);
  357. MODULE_AUTHOR("Beniamino Galvani <b.galvani@gmail.com>");
  358. MODULE_DESCRIPTION("Rockchip SoC PWM driver");
  359. MODULE_LICENSE("GPL v2");