pwm-hibvt.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. /*
  2. * PWM Controller Driver for HiSilicon BVT SoCs
  3. *
  4. * Copyright (c) 2016 HiSilicon Technologies Co., Ltd.
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include <linux/bitops.h>
  20. #include <linux/clk.h>
  21. #include <linux/delay.h>
  22. #include <linux/io.h>
  23. #include <linux/module.h>
  24. #include <linux/of_device.h>
  25. #include <linux/platform_device.h>
  26. #include <linux/pwm.h>
  27. #include <linux/reset.h>
  28. #define PWM_CFG0_ADDR(x) (((x) * 0x20) + 0x0)
  29. #define PWM_CFG1_ADDR(x) (((x) * 0x20) + 0x4)
  30. #define PWM_CFG2_ADDR(x) (((x) * 0x20) + 0x8)
  31. #define PWM_CTRL_ADDR(x) (((x) * 0x20) + 0xC)
  32. #define PWM_ENABLE_SHIFT 0
  33. #define PWM_ENABLE_MASK BIT(0)
  34. #define PWM_POLARITY_SHIFT 1
  35. #define PWM_POLARITY_MASK BIT(1)
  36. #define PWM_KEEP_SHIFT 2
  37. #define PWM_KEEP_MASK BIT(2)
  38. #define PWM_PERIOD_MASK GENMASK(31, 0)
  39. #define PWM_DUTY_MASK GENMASK(31, 0)
  40. struct hibvt_pwm_chip {
  41. struct pwm_chip chip;
  42. struct clk *clk;
  43. void __iomem *base;
  44. struct reset_control *rstc;
  45. };
  46. struct hibvt_pwm_soc {
  47. u32 num_pwms;
  48. };
  49. static const struct hibvt_pwm_soc pwm_soc[2] = {
  50. { .num_pwms = 4 },
  51. { .num_pwms = 8 },
  52. };
  53. static inline struct hibvt_pwm_chip *to_hibvt_pwm_chip(struct pwm_chip *chip)
  54. {
  55. return container_of(chip, struct hibvt_pwm_chip, chip);
  56. }
  57. static void hibvt_pwm_set_bits(void __iomem *base, u32 offset,
  58. u32 mask, u32 data)
  59. {
  60. void __iomem *address = base + offset;
  61. u32 value;
  62. value = readl(address);
  63. value &= ~mask;
  64. value |= (data & mask);
  65. writel(value, address);
  66. }
  67. static void hibvt_pwm_enable(struct pwm_chip *chip, struct pwm_device *pwm)
  68. {
  69. struct hibvt_pwm_chip *hi_pwm_chip = to_hibvt_pwm_chip(chip);
  70. hibvt_pwm_set_bits(hi_pwm_chip->base, PWM_CTRL_ADDR(pwm->hwpwm),
  71. PWM_ENABLE_MASK, 0x1);
  72. }
  73. static void hibvt_pwm_disable(struct pwm_chip *chip, struct pwm_device *pwm)
  74. {
  75. struct hibvt_pwm_chip *hi_pwm_chip = to_hibvt_pwm_chip(chip);
  76. hibvt_pwm_set_bits(hi_pwm_chip->base, PWM_CTRL_ADDR(pwm->hwpwm),
  77. PWM_ENABLE_MASK, 0x0);
  78. }
  79. static void hibvt_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm,
  80. int duty_cycle_ns, int period_ns)
  81. {
  82. struct hibvt_pwm_chip *hi_pwm_chip = to_hibvt_pwm_chip(chip);
  83. u32 freq, period, duty;
  84. freq = div_u64(clk_get_rate(hi_pwm_chip->clk), 1000000);
  85. period = div_u64(freq * period_ns, 1000);
  86. duty = div_u64(period * duty_cycle_ns, period_ns);
  87. hibvt_pwm_set_bits(hi_pwm_chip->base, PWM_CFG0_ADDR(pwm->hwpwm),
  88. PWM_PERIOD_MASK, period);
  89. hibvt_pwm_set_bits(hi_pwm_chip->base, PWM_CFG1_ADDR(pwm->hwpwm),
  90. PWM_DUTY_MASK, duty);
  91. }
  92. static void hibvt_pwm_set_polarity(struct pwm_chip *chip,
  93. struct pwm_device *pwm,
  94. enum pwm_polarity polarity)
  95. {
  96. struct hibvt_pwm_chip *hi_pwm_chip = to_hibvt_pwm_chip(chip);
  97. if (polarity == PWM_POLARITY_INVERSED)
  98. hibvt_pwm_set_bits(hi_pwm_chip->base, PWM_CTRL_ADDR(pwm->hwpwm),
  99. PWM_POLARITY_MASK, (0x1 << PWM_POLARITY_SHIFT));
  100. else
  101. hibvt_pwm_set_bits(hi_pwm_chip->base, PWM_CTRL_ADDR(pwm->hwpwm),
  102. PWM_POLARITY_MASK, (0x0 << PWM_POLARITY_SHIFT));
  103. }
  104. static void hibvt_pwm_get_state(struct pwm_chip *chip, struct pwm_device *pwm,
  105. struct pwm_state *state)
  106. {
  107. struct hibvt_pwm_chip *hi_pwm_chip = to_hibvt_pwm_chip(chip);
  108. void __iomem *base;
  109. u32 freq, value;
  110. freq = div_u64(clk_get_rate(hi_pwm_chip->clk), 1000000);
  111. base = hi_pwm_chip->base;
  112. value = readl(base + PWM_CFG0_ADDR(pwm->hwpwm));
  113. state->period = div_u64(value * 1000, freq);
  114. value = readl(base + PWM_CFG1_ADDR(pwm->hwpwm));
  115. state->duty_cycle = div_u64(value * 1000, freq);
  116. value = readl(base + PWM_CTRL_ADDR(pwm->hwpwm));
  117. state->enabled = (PWM_ENABLE_MASK & value);
  118. }
  119. static int hibvt_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm,
  120. struct pwm_state *state)
  121. {
  122. if (state->polarity != pwm->state.polarity)
  123. hibvt_pwm_set_polarity(chip, pwm, state->polarity);
  124. if (state->period != pwm->state.period ||
  125. state->duty_cycle != pwm->state.duty_cycle)
  126. hibvt_pwm_config(chip, pwm, state->duty_cycle, state->period);
  127. if (state->enabled != pwm->state.enabled) {
  128. if (state->enabled)
  129. hibvt_pwm_enable(chip, pwm);
  130. else
  131. hibvt_pwm_disable(chip, pwm);
  132. }
  133. return 0;
  134. }
  135. static const struct pwm_ops hibvt_pwm_ops = {
  136. .get_state = hibvt_pwm_get_state,
  137. .apply = hibvt_pwm_apply,
  138. .owner = THIS_MODULE,
  139. };
  140. static int hibvt_pwm_probe(struct platform_device *pdev)
  141. {
  142. const struct hibvt_pwm_soc *soc =
  143. of_device_get_match_data(&pdev->dev);
  144. struct hibvt_pwm_chip *pwm_chip;
  145. struct resource *res;
  146. int ret;
  147. int i;
  148. pwm_chip = devm_kzalloc(&pdev->dev, sizeof(*pwm_chip), GFP_KERNEL);
  149. if (pwm_chip == NULL)
  150. return -ENOMEM;
  151. pwm_chip->clk = devm_clk_get(&pdev->dev, NULL);
  152. if (IS_ERR(pwm_chip->clk)) {
  153. dev_err(&pdev->dev, "getting clock failed with %ld\n",
  154. PTR_ERR(pwm_chip->clk));
  155. return PTR_ERR(pwm_chip->clk);
  156. }
  157. pwm_chip->chip.ops = &hibvt_pwm_ops;
  158. pwm_chip->chip.dev = &pdev->dev;
  159. pwm_chip->chip.base = -1;
  160. pwm_chip->chip.npwm = soc->num_pwms;
  161. pwm_chip->chip.of_xlate = of_pwm_xlate_with_flags;
  162. pwm_chip->chip.of_pwm_n_cells = 3;
  163. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  164. pwm_chip->base = devm_ioremap_resource(&pdev->dev, res);
  165. if (IS_ERR(pwm_chip->base))
  166. return PTR_ERR(pwm_chip->base);
  167. ret = clk_prepare_enable(pwm_chip->clk);
  168. if (ret < 0)
  169. return ret;
  170. pwm_chip->rstc = devm_reset_control_get_exclusive(&pdev->dev, NULL);
  171. if (IS_ERR(pwm_chip->rstc)) {
  172. clk_disable_unprepare(pwm_chip->clk);
  173. return PTR_ERR(pwm_chip->rstc);
  174. }
  175. reset_control_assert(pwm_chip->rstc);
  176. msleep(30);
  177. reset_control_deassert(pwm_chip->rstc);
  178. ret = pwmchip_add(&pwm_chip->chip);
  179. if (ret < 0) {
  180. clk_disable_unprepare(pwm_chip->clk);
  181. return ret;
  182. }
  183. for (i = 0; i < pwm_chip->chip.npwm; i++) {
  184. hibvt_pwm_set_bits(pwm_chip->base, PWM_CTRL_ADDR(i),
  185. PWM_KEEP_MASK, (0x1 << PWM_KEEP_SHIFT));
  186. }
  187. platform_set_drvdata(pdev, pwm_chip);
  188. return 0;
  189. }
  190. static int hibvt_pwm_remove(struct platform_device *pdev)
  191. {
  192. struct hibvt_pwm_chip *pwm_chip;
  193. pwm_chip = platform_get_drvdata(pdev);
  194. reset_control_assert(pwm_chip->rstc);
  195. msleep(30);
  196. reset_control_deassert(pwm_chip->rstc);
  197. clk_disable_unprepare(pwm_chip->clk);
  198. return pwmchip_remove(&pwm_chip->chip);
  199. }
  200. static const struct of_device_id hibvt_pwm_of_match[] = {
  201. { .compatible = "hisilicon,hi3516cv300-pwm", .data = &pwm_soc[0] },
  202. { .compatible = "hisilicon,hi3519v100-pwm", .data = &pwm_soc[1] },
  203. { }
  204. };
  205. MODULE_DEVICE_TABLE(of, hibvt_pwm_of_match);
  206. static struct platform_driver hibvt_pwm_driver = {
  207. .driver = {
  208. .name = "hibvt-pwm",
  209. .of_match_table = hibvt_pwm_of_match,
  210. },
  211. .probe = hibvt_pwm_probe,
  212. .remove = hibvt_pwm_remove,
  213. };
  214. module_platform_driver(hibvt_pwm_driver);
  215. MODULE_AUTHOR("Jian Yuan");
  216. MODULE_DESCRIPTION("HiSilicon BVT SoCs PWM driver");
  217. MODULE_LICENSE("GPL");