pwm-lpss.c 7.3 KB

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