pwm-vt8500.c 6.8 KB

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