pwm-vt8500.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * drivers/pwm/pwm-vt8500.c
  4. *
  5. * Copyright (C) 2012 Tony Prisk <[email protected]>
  6. * Copyright (C) 2010 Alexey Charkov <[email protected]>
  7. */
  8. #include <linux/module.h>
  9. #include <linux/kernel.h>
  10. #include <linux/platform_device.h>
  11. #include <linux/slab.h>
  12. #include <linux/err.h>
  13. #include <linux/io.h>
  14. #include <linux/pwm.h>
  15. #include <linux/delay.h>
  16. #include <linux/clk.h>
  17. #include <asm/div64.h>
  18. #include <linux/of.h>
  19. #include <linux/of_device.h>
  20. #include <linux/of_address.h>
  21. /*
  22. * SoC architecture allocates register space for 4 PWMs but only
  23. * 2 are currently implemented.
  24. */
  25. #define VT8500_NR_PWMS 2
  26. #define REG_CTRL(pwm) (((pwm) << 4) + 0x00)
  27. #define REG_SCALAR(pwm) (((pwm) << 4) + 0x04)
  28. #define REG_PERIOD(pwm) (((pwm) << 4) + 0x08)
  29. #define REG_DUTY(pwm) (((pwm) << 4) + 0x0C)
  30. #define REG_STATUS 0x40
  31. #define CTRL_ENABLE BIT(0)
  32. #define CTRL_INVERT BIT(1)
  33. #define CTRL_AUTOLOAD BIT(2)
  34. #define CTRL_STOP_IMM BIT(3)
  35. #define CTRL_LOAD_PRESCALE BIT(4)
  36. #define CTRL_LOAD_PERIOD BIT(5)
  37. #define STATUS_CTRL_UPDATE BIT(0)
  38. #define STATUS_SCALAR_UPDATE BIT(1)
  39. #define STATUS_PERIOD_UPDATE BIT(2)
  40. #define STATUS_DUTY_UPDATE BIT(3)
  41. #define STATUS_ALL_UPDATE 0x0F
  42. struct vt8500_chip {
  43. struct pwm_chip chip;
  44. void __iomem *base;
  45. struct clk *clk;
  46. };
  47. #define to_vt8500_chip(chip) container_of(chip, struct vt8500_chip, chip)
  48. #define msecs_to_loops(t) (loops_per_jiffy / 1000 * HZ * t)
  49. static inline void vt8500_pwm_busy_wait(struct vt8500_chip *vt8500, int nr, u8 bitmask)
  50. {
  51. int loops = msecs_to_loops(10);
  52. u32 mask = bitmask << (nr << 8);
  53. while ((readl(vt8500->base + REG_STATUS) & mask) && --loops)
  54. cpu_relax();
  55. if (unlikely(!loops))
  56. dev_warn(vt8500->chip.dev, "Waiting for status bits 0x%x to clear timed out\n",
  57. mask);
  58. }
  59. static int vt8500_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm,
  60. u64 duty_ns, u64 period_ns)
  61. {
  62. struct vt8500_chip *vt8500 = to_vt8500_chip(chip);
  63. unsigned long long c;
  64. unsigned long period_cycles, prescale, pv, dc;
  65. int err;
  66. u32 val;
  67. err = clk_enable(vt8500->clk);
  68. if (err < 0) {
  69. dev_err(chip->dev, "failed to enable clock\n");
  70. return err;
  71. }
  72. c = clk_get_rate(vt8500->clk);
  73. c = c * period_ns;
  74. do_div(c, 1000000000);
  75. period_cycles = c;
  76. if (period_cycles < 1)
  77. period_cycles = 1;
  78. prescale = (period_cycles - 1) / 4096;
  79. pv = period_cycles / (prescale + 1) - 1;
  80. if (pv > 4095)
  81. pv = 4095;
  82. if (prescale > 1023) {
  83. clk_disable(vt8500->clk);
  84. return -EINVAL;
  85. }
  86. c = (unsigned long long)pv * duty_ns;
  87. dc = div64_u64(c, period_ns);
  88. writel(prescale, vt8500->base + REG_SCALAR(pwm->hwpwm));
  89. vt8500_pwm_busy_wait(vt8500, pwm->hwpwm, STATUS_SCALAR_UPDATE);
  90. writel(pv, vt8500->base + REG_PERIOD(pwm->hwpwm));
  91. vt8500_pwm_busy_wait(vt8500, pwm->hwpwm, STATUS_PERIOD_UPDATE);
  92. writel(dc, vt8500->base + REG_DUTY(pwm->hwpwm));
  93. vt8500_pwm_busy_wait(vt8500, pwm->hwpwm, STATUS_DUTY_UPDATE);
  94. val = readl(vt8500->base + REG_CTRL(pwm->hwpwm));
  95. val |= CTRL_AUTOLOAD;
  96. writel(val, vt8500->base + REG_CTRL(pwm->hwpwm));
  97. vt8500_pwm_busy_wait(vt8500, pwm->hwpwm, STATUS_CTRL_UPDATE);
  98. clk_disable(vt8500->clk);
  99. return 0;
  100. }
  101. static int vt8500_pwm_enable(struct pwm_chip *chip, struct pwm_device *pwm)
  102. {
  103. struct vt8500_chip *vt8500 = to_vt8500_chip(chip);
  104. int err;
  105. u32 val;
  106. err = clk_enable(vt8500->clk);
  107. if (err < 0) {
  108. dev_err(chip->dev, "failed to enable clock\n");
  109. return err;
  110. }
  111. val = readl(vt8500->base + REG_CTRL(pwm->hwpwm));
  112. val |= CTRL_ENABLE;
  113. writel(val, vt8500->base + REG_CTRL(pwm->hwpwm));
  114. vt8500_pwm_busy_wait(vt8500, pwm->hwpwm, STATUS_CTRL_UPDATE);
  115. return 0;
  116. }
  117. static void vt8500_pwm_disable(struct pwm_chip *chip, struct pwm_device *pwm)
  118. {
  119. struct vt8500_chip *vt8500 = to_vt8500_chip(chip);
  120. u32 val;
  121. val = readl(vt8500->base + REG_CTRL(pwm->hwpwm));
  122. val &= ~CTRL_ENABLE;
  123. writel(val, vt8500->base + REG_CTRL(pwm->hwpwm));
  124. vt8500_pwm_busy_wait(vt8500, pwm->hwpwm, STATUS_CTRL_UPDATE);
  125. clk_disable(vt8500->clk);
  126. }
  127. static int vt8500_pwm_set_polarity(struct pwm_chip *chip,
  128. struct pwm_device *pwm,
  129. enum pwm_polarity polarity)
  130. {
  131. struct vt8500_chip *vt8500 = to_vt8500_chip(chip);
  132. u32 val;
  133. val = readl(vt8500->base + REG_CTRL(pwm->hwpwm));
  134. if (polarity == PWM_POLARITY_INVERSED)
  135. val |= CTRL_INVERT;
  136. else
  137. val &= ~CTRL_INVERT;
  138. writel(val, vt8500->base + REG_CTRL(pwm->hwpwm));
  139. vt8500_pwm_busy_wait(vt8500, pwm->hwpwm, STATUS_CTRL_UPDATE);
  140. return 0;
  141. }
  142. static int vt8500_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm,
  143. const struct pwm_state *state)
  144. {
  145. int err;
  146. bool enabled = pwm->state.enabled;
  147. if (state->polarity != pwm->state.polarity) {
  148. /*
  149. * Changing the polarity of a running PWM is only allowed when
  150. * the PWM driver implements ->apply().
  151. */
  152. if (enabled) {
  153. vt8500_pwm_disable(chip, pwm);
  154. enabled = false;
  155. }
  156. err = vt8500_pwm_set_polarity(chip, pwm, state->polarity);
  157. if (err)
  158. return err;
  159. }
  160. if (!state->enabled) {
  161. if (enabled)
  162. vt8500_pwm_disable(chip, pwm);
  163. return 0;
  164. }
  165. /*
  166. * We cannot skip calling ->config even if state->period ==
  167. * pwm->state.period && state->duty_cycle == pwm->state.duty_cycle
  168. * because we might have exited early in the last call to
  169. * pwm_apply_state because of !state->enabled and so the two values in
  170. * pwm->state might not be configured in hardware.
  171. */
  172. err = vt8500_pwm_config(pwm->chip, pwm, state->duty_cycle, state->period);
  173. if (err)
  174. return err;
  175. if (!enabled)
  176. err = vt8500_pwm_enable(chip, pwm);
  177. return err;
  178. }
  179. static const struct pwm_ops vt8500_pwm_ops = {
  180. .apply = vt8500_pwm_apply,
  181. .owner = THIS_MODULE,
  182. };
  183. static const struct of_device_id vt8500_pwm_dt_ids[] = {
  184. { .compatible = "via,vt8500-pwm", },
  185. { /* Sentinel */ }
  186. };
  187. MODULE_DEVICE_TABLE(of, vt8500_pwm_dt_ids);
  188. static int vt8500_pwm_probe(struct platform_device *pdev)
  189. {
  190. struct vt8500_chip *vt8500;
  191. struct device_node *np = pdev->dev.of_node;
  192. int ret;
  193. if (!np) {
  194. dev_err(&pdev->dev, "invalid devicetree node\n");
  195. return -EINVAL;
  196. }
  197. vt8500 = devm_kzalloc(&pdev->dev, sizeof(*vt8500), GFP_KERNEL);
  198. if (vt8500 == NULL)
  199. return -ENOMEM;
  200. vt8500->chip.dev = &pdev->dev;
  201. vt8500->chip.ops = &vt8500_pwm_ops;
  202. vt8500->chip.npwm = VT8500_NR_PWMS;
  203. vt8500->clk = devm_clk_get(&pdev->dev, NULL);
  204. if (IS_ERR(vt8500->clk)) {
  205. dev_err(&pdev->dev, "clock source not specified\n");
  206. return PTR_ERR(vt8500->clk);
  207. }
  208. vt8500->base = devm_platform_ioremap_resource(pdev, 0);
  209. if (IS_ERR(vt8500->base))
  210. return PTR_ERR(vt8500->base);
  211. ret = clk_prepare(vt8500->clk);
  212. if (ret < 0) {
  213. dev_err(&pdev->dev, "failed to prepare clock\n");
  214. return ret;
  215. }
  216. ret = pwmchip_add(&vt8500->chip);
  217. if (ret < 0) {
  218. dev_err(&pdev->dev, "failed to add PWM chip\n");
  219. clk_unprepare(vt8500->clk);
  220. return ret;
  221. }
  222. platform_set_drvdata(pdev, vt8500);
  223. return ret;
  224. }
  225. static int vt8500_pwm_remove(struct platform_device *pdev)
  226. {
  227. struct vt8500_chip *vt8500 = platform_get_drvdata(pdev);
  228. pwmchip_remove(&vt8500->chip);
  229. clk_unprepare(vt8500->clk);
  230. return 0;
  231. }
  232. static struct platform_driver vt8500_pwm_driver = {
  233. .probe = vt8500_pwm_probe,
  234. .remove = vt8500_pwm_remove,
  235. .driver = {
  236. .name = "vt8500-pwm",
  237. .of_match_table = vt8500_pwm_dt_ids,
  238. },
  239. };
  240. module_platform_driver(vt8500_pwm_driver);
  241. MODULE_DESCRIPTION("VT8500 PWM Driver");
  242. MODULE_AUTHOR("Tony Prisk <[email protected]>");
  243. MODULE_LICENSE("GPL v2");