pwm-rockchip.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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. const struct pwm_ops *ops;
  45. void (*set_enable)(struct pwm_chip *chip,
  46. struct pwm_device *pwm, bool enable);
  47. };
  48. static inline struct rockchip_pwm_chip *to_rockchip_pwm_chip(struct pwm_chip *c)
  49. {
  50. return container_of(c, struct rockchip_pwm_chip, chip);
  51. }
  52. static void rockchip_pwm_set_enable_v1(struct pwm_chip *chip,
  53. struct pwm_device *pwm, bool enable)
  54. {
  55. struct rockchip_pwm_chip *pc = to_rockchip_pwm_chip(chip);
  56. u32 enable_conf = PWM_CTRL_OUTPUT_EN | PWM_CTRL_TIMER_EN;
  57. u32 val;
  58. val = readl_relaxed(pc->base + pc->data->regs.ctrl);
  59. if (enable)
  60. val |= enable_conf;
  61. else
  62. val &= ~enable_conf;
  63. writel_relaxed(val, pc->base + pc->data->regs.ctrl);
  64. }
  65. static void rockchip_pwm_set_enable_v2(struct pwm_chip *chip,
  66. struct pwm_device *pwm, bool enable)
  67. {
  68. struct rockchip_pwm_chip *pc = to_rockchip_pwm_chip(chip);
  69. u32 enable_conf = PWM_OUTPUT_LEFT | PWM_LP_DISABLE | PWM_ENABLE |
  70. PWM_CONTINUOUS;
  71. u32 val;
  72. if (pwm_get_polarity(pwm) == PWM_POLARITY_INVERSED)
  73. enable_conf |= PWM_DUTY_NEGATIVE | PWM_INACTIVE_POSITIVE;
  74. else
  75. enable_conf |= PWM_DUTY_POSITIVE | PWM_INACTIVE_NEGATIVE;
  76. val = readl_relaxed(pc->base + pc->data->regs.ctrl);
  77. if (enable)
  78. val |= enable_conf;
  79. else
  80. val &= ~enable_conf;
  81. writel_relaxed(val, pc->base + pc->data->regs.ctrl);
  82. }
  83. static int rockchip_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm,
  84. int duty_ns, int period_ns)
  85. {
  86. struct rockchip_pwm_chip *pc = to_rockchip_pwm_chip(chip);
  87. unsigned long period, duty;
  88. u64 clk_rate, div;
  89. int ret;
  90. clk_rate = clk_get_rate(pc->clk);
  91. /*
  92. * Since period and duty cycle registers have a width of 32
  93. * bits, every possible input period can be obtained using the
  94. * default prescaler value for all practical clock rate values.
  95. */
  96. div = clk_rate * period_ns;
  97. do_div(div, pc->data->prescaler * NSEC_PER_SEC);
  98. period = div;
  99. div = clk_rate * duty_ns;
  100. do_div(div, pc->data->prescaler * NSEC_PER_SEC);
  101. duty = div;
  102. ret = clk_enable(pc->clk);
  103. if (ret)
  104. return ret;
  105. writel(period, pc->base + pc->data->regs.period);
  106. writel(duty, pc->base + pc->data->regs.duty);
  107. writel(0, pc->base + pc->data->regs.cntr);
  108. clk_disable(pc->clk);
  109. return 0;
  110. }
  111. static int rockchip_pwm_set_polarity(struct pwm_chip *chip,
  112. struct pwm_device *pwm,
  113. enum pwm_polarity polarity)
  114. {
  115. /*
  116. * No action needed here because pwm->polarity will be set by the core
  117. * and the core will only change polarity when the PWM is not enabled.
  118. * We'll handle things in set_enable().
  119. */
  120. return 0;
  121. }
  122. static int rockchip_pwm_enable(struct pwm_chip *chip, struct pwm_device *pwm)
  123. {
  124. struct rockchip_pwm_chip *pc = to_rockchip_pwm_chip(chip);
  125. int ret;
  126. ret = clk_enable(pc->clk);
  127. if (ret)
  128. return ret;
  129. pc->data->set_enable(chip, pwm, true);
  130. return 0;
  131. }
  132. static void rockchip_pwm_disable(struct pwm_chip *chip, struct pwm_device *pwm)
  133. {
  134. struct rockchip_pwm_chip *pc = to_rockchip_pwm_chip(chip);
  135. pc->data->set_enable(chip, pwm, false);
  136. clk_disable(pc->clk);
  137. }
  138. static const struct pwm_ops rockchip_pwm_ops_v1 = {
  139. .config = rockchip_pwm_config,
  140. .enable = rockchip_pwm_enable,
  141. .disable = rockchip_pwm_disable,
  142. .owner = THIS_MODULE,
  143. };
  144. static const struct pwm_ops rockchip_pwm_ops_v2 = {
  145. .config = rockchip_pwm_config,
  146. .set_polarity = rockchip_pwm_set_polarity,
  147. .enable = rockchip_pwm_enable,
  148. .disable = rockchip_pwm_disable,
  149. .owner = THIS_MODULE,
  150. };
  151. static const struct rockchip_pwm_data pwm_data_v1 = {
  152. .regs = {
  153. .duty = 0x04,
  154. .period = 0x08,
  155. .cntr = 0x00,
  156. .ctrl = 0x0c,
  157. },
  158. .prescaler = 2,
  159. .ops = &rockchip_pwm_ops_v1,
  160. .set_enable = rockchip_pwm_set_enable_v1,
  161. };
  162. static const struct rockchip_pwm_data pwm_data_v2 = {
  163. .regs = {
  164. .duty = 0x08,
  165. .period = 0x04,
  166. .cntr = 0x00,
  167. .ctrl = 0x0c,
  168. },
  169. .prescaler = 1,
  170. .ops = &rockchip_pwm_ops_v2,
  171. .set_enable = rockchip_pwm_set_enable_v2,
  172. };
  173. static const struct rockchip_pwm_data pwm_data_vop = {
  174. .regs = {
  175. .duty = 0x08,
  176. .period = 0x04,
  177. .cntr = 0x0c,
  178. .ctrl = 0x00,
  179. },
  180. .prescaler = 1,
  181. .ops = &rockchip_pwm_ops_v2,
  182. .set_enable = rockchip_pwm_set_enable_v2,
  183. };
  184. static const struct of_device_id rockchip_pwm_dt_ids[] = {
  185. { .compatible = "rockchip,rk2928-pwm", .data = &pwm_data_v1},
  186. { .compatible = "rockchip,rk3288-pwm", .data = &pwm_data_v2},
  187. { .compatible = "rockchip,vop-pwm", .data = &pwm_data_vop},
  188. { /* sentinel */ }
  189. };
  190. MODULE_DEVICE_TABLE(of, rockchip_pwm_dt_ids);
  191. static int rockchip_pwm_probe(struct platform_device *pdev)
  192. {
  193. const struct of_device_id *id;
  194. struct rockchip_pwm_chip *pc;
  195. struct resource *r;
  196. int ret;
  197. id = of_match_device(rockchip_pwm_dt_ids, &pdev->dev);
  198. if (!id)
  199. return -EINVAL;
  200. pc = devm_kzalloc(&pdev->dev, sizeof(*pc), GFP_KERNEL);
  201. if (!pc)
  202. return -ENOMEM;
  203. r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  204. pc->base = devm_ioremap_resource(&pdev->dev, r);
  205. if (IS_ERR(pc->base))
  206. return PTR_ERR(pc->base);
  207. pc->clk = devm_clk_get(&pdev->dev, NULL);
  208. if (IS_ERR(pc->clk))
  209. return PTR_ERR(pc->clk);
  210. ret = clk_prepare(pc->clk);
  211. if (ret)
  212. return ret;
  213. platform_set_drvdata(pdev, pc);
  214. pc->data = id->data;
  215. pc->chip.dev = &pdev->dev;
  216. pc->chip.ops = pc->data->ops;
  217. pc->chip.base = -1;
  218. pc->chip.npwm = 1;
  219. if (pc->data->ops->set_polarity) {
  220. pc->chip.of_xlate = of_pwm_xlate_with_flags;
  221. pc->chip.of_pwm_n_cells = 3;
  222. }
  223. ret = pwmchip_add(&pc->chip);
  224. if (ret < 0) {
  225. clk_unprepare(pc->clk);
  226. dev_err(&pdev->dev, "pwmchip_add() failed: %d\n", ret);
  227. }
  228. return ret;
  229. }
  230. static int rockchip_pwm_remove(struct platform_device *pdev)
  231. {
  232. struct rockchip_pwm_chip *pc = platform_get_drvdata(pdev);
  233. clk_unprepare(pc->clk);
  234. return pwmchip_remove(&pc->chip);
  235. }
  236. static struct platform_driver rockchip_pwm_driver = {
  237. .driver = {
  238. .name = "rockchip-pwm",
  239. .of_match_table = rockchip_pwm_dt_ids,
  240. },
  241. .probe = rockchip_pwm_probe,
  242. .remove = rockchip_pwm_remove,
  243. };
  244. module_platform_driver(rockchip_pwm_driver);
  245. MODULE_AUTHOR("Beniamino Galvani <b.galvani@gmail.com>");
  246. MODULE_DESCRIPTION("Rockchip SoC PWM driver");
  247. MODULE_LICENSE("GPL v2");