pwm-lpss.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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/kernel.h>
  18. #include <linux/module.h>
  19. #include <linux/pm_runtime.h>
  20. #include <linux/time.h>
  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 0x000000ff
  27. /* Size of each PWM register space if multiple */
  28. #define PWM_SIZE 0x400
  29. struct pwm_lpss_chip {
  30. struct pwm_chip chip;
  31. void __iomem *regs;
  32. const struct pwm_lpss_boardinfo *info;
  33. };
  34. /* BayTrail */
  35. const struct pwm_lpss_boardinfo pwm_lpss_byt_info = {
  36. .clk_rate = 25000000,
  37. .npwm = 1,
  38. .base_unit_bits = 16,
  39. };
  40. EXPORT_SYMBOL_GPL(pwm_lpss_byt_info);
  41. /* Braswell */
  42. const struct pwm_lpss_boardinfo pwm_lpss_bsw_info = {
  43. .clk_rate = 19200000,
  44. .npwm = 1,
  45. .base_unit_bits = 16,
  46. };
  47. EXPORT_SYMBOL_GPL(pwm_lpss_bsw_info);
  48. /* Broxton */
  49. const struct pwm_lpss_boardinfo pwm_lpss_bxt_info = {
  50. .clk_rate = 19200000,
  51. .npwm = 4,
  52. .base_unit_bits = 22,
  53. };
  54. EXPORT_SYMBOL_GPL(pwm_lpss_bxt_info);
  55. static inline struct pwm_lpss_chip *to_lpwm(struct pwm_chip *chip)
  56. {
  57. return container_of(chip, struct pwm_lpss_chip, chip);
  58. }
  59. static inline u32 pwm_lpss_read(const struct pwm_device *pwm)
  60. {
  61. struct pwm_lpss_chip *lpwm = to_lpwm(pwm->chip);
  62. return readl(lpwm->regs + pwm->hwpwm * PWM_SIZE + PWM);
  63. }
  64. static inline void pwm_lpss_write(const struct pwm_device *pwm, u32 value)
  65. {
  66. struct pwm_lpss_chip *lpwm = to_lpwm(pwm->chip);
  67. writel(value, lpwm->regs + pwm->hwpwm * PWM_SIZE + PWM);
  68. }
  69. static void pwm_lpss_update(struct pwm_device *pwm)
  70. {
  71. pwm_lpss_write(pwm, pwm_lpss_read(pwm) | PWM_SW_UPDATE);
  72. /* Give it some time to propagate */
  73. usleep_range(10, 50);
  74. }
  75. static int pwm_lpss_config(struct pwm_chip *chip, struct pwm_device *pwm,
  76. int duty_ns, int period_ns)
  77. {
  78. struct pwm_lpss_chip *lpwm = to_lpwm(chip);
  79. unsigned long long on_time_div;
  80. unsigned long c = lpwm->info->clk_rate, base_unit_range;
  81. unsigned long long base_unit, freq = NSEC_PER_SEC;
  82. u32 ctrl;
  83. do_div(freq, period_ns);
  84. /*
  85. * The equation is:
  86. * base_unit = round(base_unit_range * freq / c)
  87. */
  88. base_unit_range = BIT(lpwm->info->base_unit_bits);
  89. freq *= base_unit_range;
  90. base_unit = DIV_ROUND_CLOSEST_ULL(freq, c);
  91. if (duty_ns <= 0)
  92. duty_ns = 1;
  93. on_time_div = 255ULL * duty_ns;
  94. do_div(on_time_div, period_ns);
  95. on_time_div = 255ULL - on_time_div;
  96. pm_runtime_get_sync(chip->dev);
  97. ctrl = pwm_lpss_read(pwm);
  98. ctrl &= ~PWM_ON_TIME_DIV_MASK;
  99. ctrl &= ~((base_unit_range - 1) << PWM_BASE_UNIT_SHIFT);
  100. base_unit &= (base_unit_range - 1);
  101. ctrl |= (u32) base_unit << PWM_BASE_UNIT_SHIFT;
  102. ctrl |= on_time_div;
  103. pwm_lpss_write(pwm, ctrl);
  104. /*
  105. * If the PWM is already enabled we need to notify the hardware
  106. * about the change by setting PWM_SW_UPDATE.
  107. */
  108. if (pwm_is_enabled(pwm))
  109. pwm_lpss_update(pwm);
  110. pm_runtime_put(chip->dev);
  111. return 0;
  112. }
  113. static int pwm_lpss_enable(struct pwm_chip *chip, struct pwm_device *pwm)
  114. {
  115. pm_runtime_get_sync(chip->dev);
  116. /*
  117. * Hardware must first see PWM_SW_UPDATE before the PWM can be
  118. * enabled.
  119. */
  120. pwm_lpss_update(pwm);
  121. pwm_lpss_write(pwm, pwm_lpss_read(pwm) | PWM_ENABLE);
  122. return 0;
  123. }
  124. static void pwm_lpss_disable(struct pwm_chip *chip, struct pwm_device *pwm)
  125. {
  126. pwm_lpss_write(pwm, pwm_lpss_read(pwm) & ~PWM_ENABLE);
  127. pm_runtime_put(chip->dev);
  128. }
  129. static const struct pwm_ops pwm_lpss_ops = {
  130. .config = pwm_lpss_config,
  131. .enable = pwm_lpss_enable,
  132. .disable = pwm_lpss_disable,
  133. .owner = THIS_MODULE,
  134. };
  135. struct pwm_lpss_chip *pwm_lpss_probe(struct device *dev, struct resource *r,
  136. const struct pwm_lpss_boardinfo *info)
  137. {
  138. struct pwm_lpss_chip *lpwm;
  139. unsigned long c;
  140. int ret;
  141. lpwm = devm_kzalloc(dev, sizeof(*lpwm), GFP_KERNEL);
  142. if (!lpwm)
  143. return ERR_PTR(-ENOMEM);
  144. lpwm->regs = devm_ioremap_resource(dev, r);
  145. if (IS_ERR(lpwm->regs))
  146. return ERR_CAST(lpwm->regs);
  147. lpwm->info = info;
  148. c = lpwm->info->clk_rate;
  149. if (!c)
  150. return ERR_PTR(-EINVAL);
  151. lpwm->chip.dev = dev;
  152. lpwm->chip.ops = &pwm_lpss_ops;
  153. lpwm->chip.base = -1;
  154. lpwm->chip.npwm = info->npwm;
  155. ret = pwmchip_add(&lpwm->chip);
  156. if (ret) {
  157. dev_err(dev, "failed to add PWM chip: %d\n", ret);
  158. return ERR_PTR(ret);
  159. }
  160. return lpwm;
  161. }
  162. EXPORT_SYMBOL_GPL(pwm_lpss_probe);
  163. int pwm_lpss_remove(struct pwm_lpss_chip *lpwm)
  164. {
  165. return pwmchip_remove(&lpwm->chip);
  166. }
  167. EXPORT_SYMBOL_GPL(pwm_lpss_remove);
  168. MODULE_DESCRIPTION("PWM driver for Intel LPSS");
  169. MODULE_AUTHOR("Mika Westerberg <mika.westerberg@linux.intel.com>");
  170. MODULE_LICENSE("GPL v2");