pwm-lpss.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Intel Low Power Subsystem PWM controller driver
  4. *
  5. * Copyright (C) 2014, Intel Corporation
  6. * Author: Mika Westerberg <mika.westerberg@linux.intel.com>
  7. * Author: Chew Kean Ho <kean.ho.chew@intel.com>
  8. * Author: Chang Rebecca Swee Fun <rebecca.swee.fun.chang@intel.com>
  9. * Author: Chew Chiau Ee <chiau.ee.chew@intel.com>
  10. * Author: Alan Cox <alan@linux.intel.com>
  11. */
  12. #define DEFAULT_SYMBOL_NAMESPACE "PWM_LPSS"
  13. #include <linux/bits.h>
  14. #include <linux/delay.h>
  15. #include <linux/io.h>
  16. #include <linux/iopoll.h>
  17. #include <linux/kernel.h>
  18. #include <linux/module.h>
  19. #include <linux/pm_runtime.h>
  20. #include <linux/pwm.h>
  21. #include <linux/time.h>
  22. #include "pwm-lpss.h"
  23. #define PWM 0x00000000
  24. #define PWM_ENABLE BIT(31)
  25. #define PWM_SW_UPDATE BIT(30)
  26. #define PWM_BASE_UNIT_SHIFT 8
  27. #define PWM_ON_TIME_DIV_MASK GENMASK(7, 0)
  28. /* Size of each PWM register space if multiple */
  29. #define PWM_SIZE 0x400
  30. /* BayTrail */
  31. const struct pwm_lpss_boardinfo pwm_lpss_byt_info = {
  32. .clk_rate = 25000000,
  33. .npwm = 1,
  34. .base_unit_bits = 16,
  35. };
  36. EXPORT_SYMBOL_GPL(pwm_lpss_byt_info);
  37. /* Braswell */
  38. const struct pwm_lpss_boardinfo pwm_lpss_bsw_info = {
  39. .clk_rate = 19200000,
  40. .npwm = 1,
  41. .base_unit_bits = 16,
  42. .other_devices_aml_touches_pwm_regs = true,
  43. };
  44. EXPORT_SYMBOL_GPL(pwm_lpss_bsw_info);
  45. /* Broxton */
  46. const struct pwm_lpss_boardinfo pwm_lpss_bxt_info = {
  47. .clk_rate = 19200000,
  48. .npwm = 4,
  49. .base_unit_bits = 22,
  50. .bypass = true,
  51. };
  52. EXPORT_SYMBOL_GPL(pwm_lpss_bxt_info);
  53. /* Tangier */
  54. const struct pwm_lpss_boardinfo pwm_lpss_tng_info = {
  55. .clk_rate = 19200000,
  56. .npwm = 4,
  57. .base_unit_bits = 22,
  58. };
  59. EXPORT_SYMBOL_GPL(pwm_lpss_tng_info);
  60. static inline struct pwm_lpss_chip *to_lpwm(struct pwm_chip *chip)
  61. {
  62. return pwmchip_get_drvdata(chip);
  63. }
  64. static inline u32 pwm_lpss_read(const struct pwm_device *pwm)
  65. {
  66. struct pwm_lpss_chip *lpwm = to_lpwm(pwm->chip);
  67. return readl(lpwm->regs + pwm->hwpwm * PWM_SIZE + PWM);
  68. }
  69. static inline void pwm_lpss_write(const struct pwm_device *pwm, u32 value)
  70. {
  71. struct pwm_lpss_chip *lpwm = to_lpwm(pwm->chip);
  72. writel(value, lpwm->regs + pwm->hwpwm * PWM_SIZE + PWM);
  73. }
  74. static int pwm_lpss_wait_for_update(struct pwm_device *pwm)
  75. {
  76. struct pwm_lpss_chip *lpwm = to_lpwm(pwm->chip);
  77. const void __iomem *addr = lpwm->regs + pwm->hwpwm * PWM_SIZE + PWM;
  78. const unsigned int ms = 500 * USEC_PER_MSEC;
  79. u32 val;
  80. int err;
  81. /*
  82. * PWM Configuration register has SW_UPDATE bit that is set when a new
  83. * configuration is written to the register. The bit is automatically
  84. * cleared at the start of the next output cycle by the IP block.
  85. *
  86. * If one writes a new configuration to the register while it still has
  87. * the bit enabled, PWM may freeze. That is, while one can still write
  88. * to the register, it won't have an effect. Thus, we try to sleep long
  89. * enough that the bit gets cleared and make sure the bit is not
  90. * enabled while we update the configuration.
  91. */
  92. err = readl_poll_timeout(addr, val, !(val & PWM_SW_UPDATE), 40, ms);
  93. if (err)
  94. dev_err(pwmchip_parent(pwm->chip), "PWM_SW_UPDATE was not cleared\n");
  95. return err;
  96. }
  97. static inline int pwm_lpss_is_updating(struct pwm_device *pwm)
  98. {
  99. if (pwm_lpss_read(pwm) & PWM_SW_UPDATE) {
  100. dev_err(pwmchip_parent(pwm->chip), "PWM_SW_UPDATE is still set, skipping update\n");
  101. return -EBUSY;
  102. }
  103. return 0;
  104. }
  105. static void pwm_lpss_prepare(struct pwm_lpss_chip *lpwm, struct pwm_device *pwm,
  106. int duty_ns, int period_ns)
  107. {
  108. unsigned long long on_time_div;
  109. unsigned long c = lpwm->info->clk_rate, base_unit_range;
  110. unsigned long long base_unit, freq = NSEC_PER_SEC;
  111. u32 ctrl;
  112. do_div(freq, period_ns);
  113. /*
  114. * The equation is:
  115. * base_unit = round(base_unit_range * freq / c)
  116. */
  117. base_unit_range = BIT(lpwm->info->base_unit_bits);
  118. freq *= base_unit_range;
  119. base_unit = DIV_ROUND_CLOSEST_ULL(freq, c);
  120. /* base_unit must not be 0 and we also want to avoid overflowing it */
  121. base_unit = clamp_val(base_unit, 1, base_unit_range - 1);
  122. on_time_div = 255ULL * duty_ns;
  123. do_div(on_time_div, period_ns);
  124. on_time_div = 255ULL - on_time_div;
  125. ctrl = pwm_lpss_read(pwm);
  126. ctrl &= ~PWM_ON_TIME_DIV_MASK;
  127. ctrl &= ~((base_unit_range - 1) << PWM_BASE_UNIT_SHIFT);
  128. ctrl |= (u32) base_unit << PWM_BASE_UNIT_SHIFT;
  129. ctrl |= on_time_div;
  130. pwm_lpss_write(pwm, ctrl);
  131. pwm_lpss_write(pwm, ctrl | PWM_SW_UPDATE);
  132. }
  133. static inline void pwm_lpss_cond_enable(struct pwm_device *pwm, bool cond)
  134. {
  135. if (cond)
  136. pwm_lpss_write(pwm, pwm_lpss_read(pwm) | PWM_ENABLE);
  137. }
  138. static int pwm_lpss_prepare_enable(struct pwm_lpss_chip *lpwm,
  139. struct pwm_device *pwm,
  140. const struct pwm_state *state)
  141. {
  142. int ret;
  143. ret = pwm_lpss_is_updating(pwm);
  144. if (ret)
  145. return ret;
  146. pwm_lpss_prepare(lpwm, pwm, state->duty_cycle, state->period);
  147. pwm_lpss_cond_enable(pwm, lpwm->info->bypass == false);
  148. ret = pwm_lpss_wait_for_update(pwm);
  149. if (ret)
  150. return ret;
  151. pwm_lpss_cond_enable(pwm, lpwm->info->bypass == true);
  152. return 0;
  153. }
  154. static int pwm_lpss_apply(struct pwm_chip *chip, struct pwm_device *pwm,
  155. const struct pwm_state *state)
  156. {
  157. struct pwm_lpss_chip *lpwm = to_lpwm(chip);
  158. int ret = 0;
  159. if (state->enabled) {
  160. if (!pwm_is_enabled(pwm)) {
  161. pm_runtime_get_sync(pwmchip_parent(chip));
  162. ret = pwm_lpss_prepare_enable(lpwm, pwm, state);
  163. if (ret)
  164. pm_runtime_put(pwmchip_parent(chip));
  165. } else {
  166. ret = pwm_lpss_prepare_enable(lpwm, pwm, state);
  167. }
  168. } else if (pwm_is_enabled(pwm)) {
  169. pwm_lpss_write(pwm, pwm_lpss_read(pwm) & ~PWM_ENABLE);
  170. pm_runtime_put(pwmchip_parent(chip));
  171. }
  172. return ret;
  173. }
  174. static int pwm_lpss_get_state(struct pwm_chip *chip, struct pwm_device *pwm,
  175. struct pwm_state *state)
  176. {
  177. struct pwm_lpss_chip *lpwm = to_lpwm(chip);
  178. unsigned long base_unit_range;
  179. unsigned long long base_unit, freq, on_time_div;
  180. u32 ctrl;
  181. pm_runtime_get_sync(pwmchip_parent(chip));
  182. base_unit_range = BIT(lpwm->info->base_unit_bits);
  183. ctrl = pwm_lpss_read(pwm);
  184. on_time_div = 255 - (ctrl & PWM_ON_TIME_DIV_MASK);
  185. base_unit = (ctrl >> PWM_BASE_UNIT_SHIFT) & (base_unit_range - 1);
  186. freq = base_unit * lpwm->info->clk_rate;
  187. do_div(freq, base_unit_range);
  188. if (freq == 0)
  189. state->period = NSEC_PER_SEC;
  190. else
  191. state->period = NSEC_PER_SEC / (unsigned long)freq;
  192. on_time_div *= state->period;
  193. do_div(on_time_div, 255);
  194. state->duty_cycle = on_time_div;
  195. state->polarity = PWM_POLARITY_NORMAL;
  196. state->enabled = !!(ctrl & PWM_ENABLE);
  197. pm_runtime_put(pwmchip_parent(chip));
  198. return 0;
  199. }
  200. static const struct pwm_ops pwm_lpss_ops = {
  201. .apply = pwm_lpss_apply,
  202. .get_state = pwm_lpss_get_state,
  203. };
  204. struct pwm_chip *devm_pwm_lpss_probe(struct device *dev, void __iomem *base,
  205. const struct pwm_lpss_boardinfo *info)
  206. {
  207. struct pwm_lpss_chip *lpwm;
  208. struct pwm_chip *chip;
  209. unsigned long c;
  210. int i, ret;
  211. u32 ctrl;
  212. if (WARN_ON(info->npwm > LPSS_MAX_PWMS))
  213. return ERR_PTR(-ENODEV);
  214. chip = devm_pwmchip_alloc(dev, info->npwm, sizeof(*lpwm));
  215. if (IS_ERR(chip))
  216. return chip;
  217. lpwm = to_lpwm(chip);
  218. lpwm->regs = base;
  219. lpwm->info = info;
  220. c = lpwm->info->clk_rate;
  221. if (!c)
  222. return ERR_PTR(-EINVAL);
  223. chip->ops = &pwm_lpss_ops;
  224. ret = devm_pwmchip_add(dev, chip);
  225. if (ret) {
  226. dev_err(dev, "failed to add PWM chip: %d\n", ret);
  227. return ERR_PTR(ret);
  228. }
  229. for (i = 0; i < lpwm->info->npwm; i++) {
  230. ctrl = pwm_lpss_read(&chip->pwms[i]);
  231. if (ctrl & PWM_ENABLE)
  232. pm_runtime_get(dev);
  233. }
  234. return chip;
  235. }
  236. EXPORT_SYMBOL_GPL(devm_pwm_lpss_probe);
  237. MODULE_DESCRIPTION("PWM driver for Intel LPSS");
  238. MODULE_AUTHOR("Mika Westerberg <mika.westerberg@linux.intel.com>");
  239. MODULE_LICENSE("GPL v2");