pwm-lpss.c 5.5 KB

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