pwm-lpss.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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. #define MAX_PWMS 4
  31. struct pwm_lpss_chip {
  32. struct pwm_chip chip;
  33. void __iomem *regs;
  34. const struct pwm_lpss_boardinfo *info;
  35. u32 saved_ctrl[MAX_PWMS];
  36. };
  37. static inline struct pwm_lpss_chip *to_lpwm(struct pwm_chip *chip)
  38. {
  39. return container_of(chip, struct pwm_lpss_chip, chip);
  40. }
  41. static inline u32 pwm_lpss_read(const struct pwm_device *pwm)
  42. {
  43. struct pwm_lpss_chip *lpwm = to_lpwm(pwm->chip);
  44. return readl(lpwm->regs + pwm->hwpwm * PWM_SIZE + PWM);
  45. }
  46. static inline void pwm_lpss_write(const struct pwm_device *pwm, u32 value)
  47. {
  48. struct pwm_lpss_chip *lpwm = to_lpwm(pwm->chip);
  49. writel(value, lpwm->regs + pwm->hwpwm * PWM_SIZE + PWM);
  50. }
  51. static int pwm_lpss_wait_for_update(struct pwm_device *pwm)
  52. {
  53. struct pwm_lpss_chip *lpwm = to_lpwm(pwm->chip);
  54. const void __iomem *addr = lpwm->regs + pwm->hwpwm * PWM_SIZE + PWM;
  55. const unsigned int ms = 500 * USEC_PER_MSEC;
  56. u32 val;
  57. int err;
  58. /*
  59. * PWM Configuration register has SW_UPDATE bit that is set when a new
  60. * configuration is written to the register. The bit is automatically
  61. * cleared at the start of the next output cycle by the IP block.
  62. *
  63. * If one writes a new configuration to the register while it still has
  64. * the bit enabled, PWM may freeze. That is, while one can still write
  65. * to the register, it won't have an effect. Thus, we try to sleep long
  66. * enough that the bit gets cleared and make sure the bit is not
  67. * enabled while we update the configuration.
  68. */
  69. err = readl_poll_timeout(addr, val, !(val & PWM_SW_UPDATE), 40, ms);
  70. if (err)
  71. dev_err(pwm->chip->dev, "PWM_SW_UPDATE was not cleared\n");
  72. return err;
  73. }
  74. static inline int pwm_lpss_is_updating(struct pwm_device *pwm)
  75. {
  76. return (pwm_lpss_read(pwm) & PWM_SW_UPDATE) ? -EBUSY : 0;
  77. }
  78. static void pwm_lpss_prepare(struct pwm_lpss_chip *lpwm, struct pwm_device *pwm,
  79. int duty_ns, int period_ns)
  80. {
  81. unsigned long long on_time_div;
  82. unsigned long c = lpwm->info->clk_rate, base_unit_range;
  83. unsigned long long base_unit, freq = NSEC_PER_SEC;
  84. u32 orig_ctrl, ctrl;
  85. do_div(freq, period_ns);
  86. /*
  87. * The equation is:
  88. * base_unit = round(base_unit_range * freq / c)
  89. */
  90. base_unit_range = BIT(lpwm->info->base_unit_bits);
  91. freq *= base_unit_range;
  92. base_unit = DIV_ROUND_CLOSEST_ULL(freq, c);
  93. /* base_unit must not be 0 and we also want to avoid overflowing it */
  94. base_unit = clamp_val(base_unit, 1, base_unit_range - 1);
  95. on_time_div = 255ULL * duty_ns;
  96. do_div(on_time_div, period_ns);
  97. on_time_div = 255ULL - on_time_div;
  98. orig_ctrl = ctrl = pwm_lpss_read(pwm);
  99. ctrl &= ~PWM_ON_TIME_DIV_MASK;
  100. ctrl &= ~((base_unit_range - 1) << PWM_BASE_UNIT_SHIFT);
  101. ctrl |= (u32) base_unit << PWM_BASE_UNIT_SHIFT;
  102. ctrl |= on_time_div;
  103. if (orig_ctrl != ctrl) {
  104. pwm_lpss_write(pwm, ctrl);
  105. pwm_lpss_write(pwm, ctrl | PWM_SW_UPDATE);
  106. }
  107. }
  108. static inline void pwm_lpss_cond_enable(struct pwm_device *pwm, bool cond)
  109. {
  110. if (cond)
  111. pwm_lpss_write(pwm, pwm_lpss_read(pwm) | PWM_ENABLE);
  112. }
  113. static int pwm_lpss_apply(struct pwm_chip *chip, struct pwm_device *pwm,
  114. struct pwm_state *state)
  115. {
  116. struct pwm_lpss_chip *lpwm = to_lpwm(chip);
  117. int ret;
  118. if (state->enabled) {
  119. if (!pwm_is_enabled(pwm)) {
  120. pm_runtime_get_sync(chip->dev);
  121. ret = pwm_lpss_is_updating(pwm);
  122. if (ret) {
  123. pm_runtime_put(chip->dev);
  124. return ret;
  125. }
  126. pwm_lpss_prepare(lpwm, pwm, state->duty_cycle, state->period);
  127. pwm_lpss_cond_enable(pwm, lpwm->info->bypass == false);
  128. ret = pwm_lpss_wait_for_update(pwm);
  129. if (ret) {
  130. pm_runtime_put(chip->dev);
  131. return ret;
  132. }
  133. pwm_lpss_cond_enable(pwm, lpwm->info->bypass == true);
  134. } else {
  135. ret = pwm_lpss_is_updating(pwm);
  136. if (ret)
  137. return ret;
  138. pwm_lpss_prepare(lpwm, pwm, state->duty_cycle, state->period);
  139. return pwm_lpss_wait_for_update(pwm);
  140. }
  141. } else if (pwm_is_enabled(pwm)) {
  142. pwm_lpss_write(pwm, pwm_lpss_read(pwm) & ~PWM_ENABLE);
  143. pm_runtime_put(chip->dev);
  144. }
  145. return 0;
  146. }
  147. static const struct pwm_ops pwm_lpss_ops = {
  148. .apply = pwm_lpss_apply,
  149. .owner = THIS_MODULE,
  150. };
  151. struct pwm_lpss_chip *pwm_lpss_probe(struct device *dev, struct resource *r,
  152. const struct pwm_lpss_boardinfo *info)
  153. {
  154. struct pwm_lpss_chip *lpwm;
  155. unsigned long c;
  156. int ret;
  157. if (WARN_ON(info->npwm > MAX_PWMS))
  158. return ERR_PTR(-ENODEV);
  159. lpwm = devm_kzalloc(dev, sizeof(*lpwm), GFP_KERNEL);
  160. if (!lpwm)
  161. return ERR_PTR(-ENOMEM);
  162. lpwm->regs = devm_ioremap_resource(dev, r);
  163. if (IS_ERR(lpwm->regs))
  164. return ERR_CAST(lpwm->regs);
  165. lpwm->info = info;
  166. c = lpwm->info->clk_rate;
  167. if (!c)
  168. return ERR_PTR(-EINVAL);
  169. lpwm->chip.dev = dev;
  170. lpwm->chip.ops = &pwm_lpss_ops;
  171. lpwm->chip.base = -1;
  172. lpwm->chip.npwm = info->npwm;
  173. ret = pwmchip_add(&lpwm->chip);
  174. if (ret) {
  175. dev_err(dev, "failed to add PWM chip: %d\n", ret);
  176. return ERR_PTR(ret);
  177. }
  178. return lpwm;
  179. }
  180. EXPORT_SYMBOL_GPL(pwm_lpss_probe);
  181. int pwm_lpss_remove(struct pwm_lpss_chip *lpwm)
  182. {
  183. int i;
  184. for (i = 0; i < lpwm->info->npwm; i++) {
  185. if (pwm_is_enabled(&lpwm->chip.pwms[i]))
  186. pm_runtime_put(lpwm->chip.dev);
  187. }
  188. return pwmchip_remove(&lpwm->chip);
  189. }
  190. EXPORT_SYMBOL_GPL(pwm_lpss_remove);
  191. int pwm_lpss_suspend(struct device *dev)
  192. {
  193. struct pwm_lpss_chip *lpwm = dev_get_drvdata(dev);
  194. int i;
  195. for (i = 0; i < lpwm->info->npwm; i++)
  196. lpwm->saved_ctrl[i] = readl(lpwm->regs + i * PWM_SIZE + PWM);
  197. return 0;
  198. }
  199. EXPORT_SYMBOL_GPL(pwm_lpss_suspend);
  200. int pwm_lpss_resume(struct device *dev)
  201. {
  202. struct pwm_lpss_chip *lpwm = dev_get_drvdata(dev);
  203. int i;
  204. for (i = 0; i < lpwm->info->npwm; i++)
  205. writel(lpwm->saved_ctrl[i], lpwm->regs + i * PWM_SIZE + PWM);
  206. return 0;
  207. }
  208. EXPORT_SYMBOL_GPL(pwm_lpss_resume);
  209. MODULE_DESCRIPTION("PWM driver for Intel LPSS");
  210. MODULE_AUTHOR("Mika Westerberg <mika.westerberg@linux.intel.com>");
  211. MODULE_LICENSE("GPL v2");