pwm-rockchip.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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_INACTIVE_NEGATIVE (0 << 4)
  25. #define PWM_OUTPUT_LEFT (0 << 5)
  26. #define PWM_LP_DISABLE (0 << 8)
  27. struct rockchip_pwm_chip {
  28. struct pwm_chip chip;
  29. struct clk *clk;
  30. const struct rockchip_pwm_data *data;
  31. void __iomem *base;
  32. };
  33. struct rockchip_pwm_regs {
  34. unsigned long duty;
  35. unsigned long period;
  36. unsigned long cntr;
  37. unsigned long ctrl;
  38. };
  39. struct rockchip_pwm_data {
  40. struct rockchip_pwm_regs regs;
  41. unsigned int prescaler;
  42. void (*set_enable)(struct pwm_chip *chip, bool enable);
  43. };
  44. static inline struct rockchip_pwm_chip *to_rockchip_pwm_chip(struct pwm_chip *c)
  45. {
  46. return container_of(c, struct rockchip_pwm_chip, chip);
  47. }
  48. static void rockchip_pwm_set_enable_v1(struct pwm_chip *chip, bool enable)
  49. {
  50. struct rockchip_pwm_chip *pc = to_rockchip_pwm_chip(chip);
  51. u32 enable_conf = PWM_CTRL_OUTPUT_EN | PWM_CTRL_TIMER_EN;
  52. u32 val;
  53. val = readl_relaxed(pc->base + pc->data->regs.ctrl);
  54. if (enable)
  55. val |= enable_conf;
  56. else
  57. val &= ~enable_conf;
  58. writel_relaxed(val, pc->base + pc->data->regs.ctrl);
  59. }
  60. static void rockchip_pwm_set_enable_v2(struct pwm_chip *chip, bool enable)
  61. {
  62. struct rockchip_pwm_chip *pc = to_rockchip_pwm_chip(chip);
  63. u32 enable_conf = PWM_OUTPUT_LEFT | PWM_LP_DISABLE | PWM_ENABLE |
  64. PWM_CONTINUOUS | PWM_DUTY_POSITIVE |
  65. PWM_INACTIVE_NEGATIVE;
  66. u32 val;
  67. val = readl_relaxed(pc->base + pc->data->regs.ctrl);
  68. if (enable)
  69. val |= enable_conf;
  70. else
  71. val &= ~enable_conf;
  72. writel_relaxed(val, pc->base + pc->data->regs.ctrl);
  73. }
  74. static int rockchip_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm,
  75. int duty_ns, int period_ns)
  76. {
  77. struct rockchip_pwm_chip *pc = to_rockchip_pwm_chip(chip);
  78. unsigned long period, duty;
  79. u64 clk_rate, div;
  80. int ret;
  81. clk_rate = clk_get_rate(pc->clk);
  82. /*
  83. * Since period and duty cycle registers have a width of 32
  84. * bits, every possible input period can be obtained using the
  85. * default prescaler value for all practical clock rate values.
  86. */
  87. div = clk_rate * period_ns;
  88. do_div(div, pc->data->prescaler * NSEC_PER_SEC);
  89. period = div;
  90. div = clk_rate * duty_ns;
  91. do_div(div, pc->data->prescaler * NSEC_PER_SEC);
  92. duty = div;
  93. ret = clk_enable(pc->clk);
  94. if (ret)
  95. return ret;
  96. writel(period, pc->base + pc->data->regs.period);
  97. writel(duty, pc->base + pc->data->regs.duty);
  98. writel(0, pc->base + pc->data->regs.cntr);
  99. clk_disable(pc->clk);
  100. return 0;
  101. }
  102. static int rockchip_pwm_enable(struct pwm_chip *chip, struct pwm_device *pwm)
  103. {
  104. struct rockchip_pwm_chip *pc = to_rockchip_pwm_chip(chip);
  105. int ret;
  106. ret = clk_enable(pc->clk);
  107. if (ret)
  108. return ret;
  109. pc->data->set_enable(chip, true);
  110. return 0;
  111. }
  112. static void rockchip_pwm_disable(struct pwm_chip *chip, struct pwm_device *pwm)
  113. {
  114. struct rockchip_pwm_chip *pc = to_rockchip_pwm_chip(chip);
  115. pc->data->set_enable(chip, false);
  116. clk_disable(pc->clk);
  117. }
  118. static const struct pwm_ops rockchip_pwm_ops = {
  119. .config = rockchip_pwm_config,
  120. .enable = rockchip_pwm_enable,
  121. .disable = rockchip_pwm_disable,
  122. .owner = THIS_MODULE,
  123. };
  124. static const struct rockchip_pwm_data pwm_data_v1 = {
  125. .regs = {
  126. .duty = 0x04,
  127. .period = 0x08,
  128. .cntr = 0x00,
  129. .ctrl = 0x0c,
  130. },
  131. .prescaler = 2,
  132. .set_enable = rockchip_pwm_set_enable_v1,
  133. };
  134. static const struct rockchip_pwm_data pwm_data_v2 = {
  135. .regs = {
  136. .duty = 0x08,
  137. .period = 0x04,
  138. .cntr = 0x00,
  139. .ctrl = 0x0c,
  140. },
  141. .prescaler = 1,
  142. .set_enable = rockchip_pwm_set_enable_v2,
  143. };
  144. static const struct rockchip_pwm_data pwm_data_vop = {
  145. .regs = {
  146. .duty = 0x08,
  147. .period = 0x04,
  148. .cntr = 0x0c,
  149. .ctrl = 0x00,
  150. },
  151. .prescaler = 1,
  152. .set_enable = rockchip_pwm_set_enable_v2,
  153. };
  154. static const struct of_device_id rockchip_pwm_dt_ids[] = {
  155. { .compatible = "rockchip,rk2928-pwm", .data = &pwm_data_v1},
  156. { .compatible = "rockchip,rk3288-pwm", .data = &pwm_data_v2},
  157. { .compatible = "rockchip,vop-pwm", .data = &pwm_data_vop},
  158. { /* sentinel */ }
  159. };
  160. MODULE_DEVICE_TABLE(of, rockchip_pwm_dt_ids);
  161. static int rockchip_pwm_probe(struct platform_device *pdev)
  162. {
  163. const struct of_device_id *id;
  164. struct rockchip_pwm_chip *pc;
  165. struct resource *r;
  166. int ret;
  167. id = of_match_device(rockchip_pwm_dt_ids, &pdev->dev);
  168. if (!id)
  169. return -EINVAL;
  170. pc = devm_kzalloc(&pdev->dev, sizeof(*pc), GFP_KERNEL);
  171. if (!pc)
  172. return -ENOMEM;
  173. r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  174. pc->base = devm_ioremap_resource(&pdev->dev, r);
  175. if (IS_ERR(pc->base))
  176. return PTR_ERR(pc->base);
  177. pc->clk = devm_clk_get(&pdev->dev, NULL);
  178. if (IS_ERR(pc->clk))
  179. return PTR_ERR(pc->clk);
  180. ret = clk_prepare(pc->clk);
  181. if (ret)
  182. return ret;
  183. platform_set_drvdata(pdev, pc);
  184. pc->data = id->data;
  185. pc->chip.dev = &pdev->dev;
  186. pc->chip.ops = &rockchip_pwm_ops;
  187. pc->chip.base = -1;
  188. pc->chip.npwm = 1;
  189. ret = pwmchip_add(&pc->chip);
  190. if (ret < 0) {
  191. clk_unprepare(pc->clk);
  192. dev_err(&pdev->dev, "pwmchip_add() failed: %d\n", ret);
  193. }
  194. return ret;
  195. }
  196. static int rockchip_pwm_remove(struct platform_device *pdev)
  197. {
  198. struct rockchip_pwm_chip *pc = platform_get_drvdata(pdev);
  199. clk_unprepare(pc->clk);
  200. return pwmchip_remove(&pc->chip);
  201. }
  202. static struct platform_driver rockchip_pwm_driver = {
  203. .driver = {
  204. .name = "rockchip-pwm",
  205. .of_match_table = rockchip_pwm_dt_ids,
  206. },
  207. .probe = rockchip_pwm_probe,
  208. .remove = rockchip_pwm_remove,
  209. };
  210. module_platform_driver(rockchip_pwm_driver);
  211. MODULE_AUTHOR("Beniamino Galvani <b.galvani@gmail.com>");
  212. MODULE_DESCRIPTION("Rockchip SoC PWM driver");
  213. MODULE_LICENSE("GPL v2");