pwm-dwc-core.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * DesignWare PWM Controller driver core
  4. *
  5. * Copyright (C) 2018-2020 Intel Corporation
  6. *
  7. * Author: Felipe Balbi (Intel)
  8. * Author: Jarkko Nikula <jarkko.nikula@linux.intel.com>
  9. * Author: Raymond Tan <raymond.tan@intel.com>
  10. */
  11. #define DEFAULT_SYMBOL_NAMESPACE "dwc_pwm"
  12. #include <linux/bitops.h>
  13. #include <linux/export.h>
  14. #include <linux/kernel.h>
  15. #include <linux/module.h>
  16. #include <linux/pci.h>
  17. #include <linux/pm_runtime.h>
  18. #include <linux/pwm.h>
  19. #include "pwm-dwc.h"
  20. static void __dwc_pwm_set_enable(struct dwc_pwm *dwc, int pwm, int enabled)
  21. {
  22. u32 reg;
  23. reg = dwc_pwm_readl(dwc, DWC_TIM_CTRL(pwm));
  24. if (enabled)
  25. reg |= DWC_TIM_CTRL_EN;
  26. else
  27. reg &= ~DWC_TIM_CTRL_EN;
  28. dwc_pwm_writel(dwc, reg, DWC_TIM_CTRL(pwm));
  29. }
  30. static int __dwc_pwm_configure_timer(struct dwc_pwm *dwc,
  31. struct pwm_device *pwm,
  32. const struct pwm_state *state)
  33. {
  34. u64 tmp;
  35. u32 ctrl;
  36. u32 high;
  37. u32 low;
  38. /*
  39. * Calculate width of low and high period in terms of input clock
  40. * periods and check are the result within HW limits between 1 and
  41. * 2^32 periods.
  42. */
  43. tmp = DIV_ROUND_CLOSEST_ULL(state->duty_cycle, dwc->clk_ns);
  44. if (tmp < 1 || tmp > (1ULL << 32))
  45. return -ERANGE;
  46. low = tmp - 1;
  47. tmp = DIV_ROUND_CLOSEST_ULL(state->period - state->duty_cycle,
  48. dwc->clk_ns);
  49. if (tmp < 1 || tmp > (1ULL << 32))
  50. return -ERANGE;
  51. high = tmp - 1;
  52. /*
  53. * Specification says timer usage flow is to disable timer, then
  54. * program it followed by enable. It also says Load Count is loaded
  55. * into timer after it is enabled - either after a disable or
  56. * a reset. Based on measurements it happens also without disable
  57. * whenever Load Count is updated. But follow the specification.
  58. */
  59. __dwc_pwm_set_enable(dwc, pwm->hwpwm, false);
  60. /*
  61. * Write Load Count and Load Count 2 registers. Former defines the
  62. * width of low period and latter the width of high period in terms
  63. * multiple of input clock periods:
  64. * Width = ((Count + 1) * input clock period).
  65. */
  66. dwc_pwm_writel(dwc, low, DWC_TIM_LD_CNT(pwm->hwpwm));
  67. dwc_pwm_writel(dwc, high, DWC_TIM_LD_CNT2(pwm->hwpwm));
  68. /*
  69. * Set user-defined mode, timer reloads from Load Count registers
  70. * when it counts down to 0.
  71. * Set PWM mode, it makes output to toggle and width of low and high
  72. * periods are set by Load Count registers.
  73. */
  74. ctrl = DWC_TIM_CTRL_MODE_USER | DWC_TIM_CTRL_PWM;
  75. dwc_pwm_writel(dwc, ctrl, DWC_TIM_CTRL(pwm->hwpwm));
  76. /*
  77. * Enable timer. Output starts from low period.
  78. */
  79. __dwc_pwm_set_enable(dwc, pwm->hwpwm, state->enabled);
  80. return 0;
  81. }
  82. static int dwc_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm,
  83. const struct pwm_state *state)
  84. {
  85. struct dwc_pwm *dwc = to_dwc_pwm(chip);
  86. if (state->polarity != PWM_POLARITY_INVERSED)
  87. return -EINVAL;
  88. if (state->enabled) {
  89. if (!pwm->state.enabled)
  90. pm_runtime_get_sync(pwmchip_parent(chip));
  91. return __dwc_pwm_configure_timer(dwc, pwm, state);
  92. } else {
  93. if (pwm->state.enabled) {
  94. __dwc_pwm_set_enable(dwc, pwm->hwpwm, false);
  95. pm_runtime_put_sync(pwmchip_parent(chip));
  96. }
  97. }
  98. return 0;
  99. }
  100. static int dwc_pwm_get_state(struct pwm_chip *chip, struct pwm_device *pwm,
  101. struct pwm_state *state)
  102. {
  103. struct dwc_pwm *dwc = to_dwc_pwm(chip);
  104. u64 duty, period;
  105. u32 ctrl, ld, ld2;
  106. pm_runtime_get_sync(pwmchip_parent(chip));
  107. ctrl = dwc_pwm_readl(dwc, DWC_TIM_CTRL(pwm->hwpwm));
  108. ld = dwc_pwm_readl(dwc, DWC_TIM_LD_CNT(pwm->hwpwm));
  109. ld2 = dwc_pwm_readl(dwc, DWC_TIM_LD_CNT2(pwm->hwpwm));
  110. state->enabled = !!(ctrl & DWC_TIM_CTRL_EN);
  111. /*
  112. * If we're not in PWM, technically the output is a 50-50
  113. * based on the timer load-count only.
  114. */
  115. if (ctrl & DWC_TIM_CTRL_PWM) {
  116. duty = (ld + 1) * dwc->clk_ns;
  117. period = (ld2 + 1) * dwc->clk_ns;
  118. period += duty;
  119. } else {
  120. duty = (ld + 1) * dwc->clk_ns;
  121. period = duty * 2;
  122. }
  123. state->polarity = PWM_POLARITY_INVERSED;
  124. state->period = period;
  125. state->duty_cycle = duty;
  126. pm_runtime_put_sync(pwmchip_parent(chip));
  127. return 0;
  128. }
  129. static const struct pwm_ops dwc_pwm_ops = {
  130. .apply = dwc_pwm_apply,
  131. .get_state = dwc_pwm_get_state,
  132. };
  133. struct pwm_chip *dwc_pwm_alloc(struct device *dev)
  134. {
  135. struct pwm_chip *chip;
  136. struct dwc_pwm *dwc;
  137. chip = devm_pwmchip_alloc(dev, DWC_TIMERS_TOTAL, sizeof(*dwc));
  138. if (IS_ERR(chip))
  139. return chip;
  140. dwc = to_dwc_pwm(chip);
  141. dwc->clk_ns = 10;
  142. chip->ops = &dwc_pwm_ops;
  143. return chip;
  144. }
  145. EXPORT_SYMBOL_GPL(dwc_pwm_alloc);
  146. MODULE_AUTHOR("Felipe Balbi (Intel)");
  147. MODULE_AUTHOR("Jarkko Nikula <jarkko.nikula@linux.intel.com>");
  148. MODULE_AUTHOR("Raymond Tan <raymond.tan@intel.com>");
  149. MODULE_DESCRIPTION("DesignWare PWM Controller");
  150. MODULE_LICENSE("GPL");