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