pwm-lpss.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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. #define MAX_PWMS 4
  30. struct pwm_lpss_chip {
  31. struct pwm_chip chip;
  32. void __iomem *regs;
  33. const struct pwm_lpss_boardinfo *info;
  34. u32 saved_ctrl[MAX_PWMS];
  35. };
  36. /* BayTrail */
  37. const struct pwm_lpss_boardinfo pwm_lpss_byt_info = {
  38. .clk_rate = 25000000,
  39. .npwm = 1,
  40. .base_unit_bits = 16,
  41. };
  42. EXPORT_SYMBOL_GPL(pwm_lpss_byt_info);
  43. /* Braswell */
  44. const struct pwm_lpss_boardinfo pwm_lpss_bsw_info = {
  45. .clk_rate = 19200000,
  46. .npwm = 1,
  47. .base_unit_bits = 16,
  48. };
  49. EXPORT_SYMBOL_GPL(pwm_lpss_bsw_info);
  50. /* Broxton */
  51. const struct pwm_lpss_boardinfo pwm_lpss_bxt_info = {
  52. .clk_rate = 19200000,
  53. .npwm = 4,
  54. .base_unit_bits = 22,
  55. };
  56. EXPORT_SYMBOL_GPL(pwm_lpss_bxt_info);
  57. static inline struct pwm_lpss_chip *to_lpwm(struct pwm_chip *chip)
  58. {
  59. return container_of(chip, struct pwm_lpss_chip, chip);
  60. }
  61. static inline u32 pwm_lpss_read(const struct pwm_device *pwm)
  62. {
  63. struct pwm_lpss_chip *lpwm = to_lpwm(pwm->chip);
  64. return readl(lpwm->regs + pwm->hwpwm * PWM_SIZE + PWM);
  65. }
  66. static inline void pwm_lpss_write(const struct pwm_device *pwm, u32 value)
  67. {
  68. struct pwm_lpss_chip *lpwm = to_lpwm(pwm->chip);
  69. writel(value, lpwm->regs + pwm->hwpwm * PWM_SIZE + PWM);
  70. }
  71. static void pwm_lpss_update(struct pwm_device *pwm)
  72. {
  73. pwm_lpss_write(pwm, pwm_lpss_read(pwm) | PWM_SW_UPDATE);
  74. /* Give it some time to propagate */
  75. usleep_range(10, 50);
  76. }
  77. static int pwm_lpss_config(struct pwm_chip *chip, struct pwm_device *pwm,
  78. int duty_ns, int period_ns)
  79. {
  80. struct pwm_lpss_chip *lpwm = to_lpwm(chip);
  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 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. if (duty_ns <= 0)
  94. duty_ns = 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. pm_runtime_get_sync(chip->dev);
  99. ctrl = pwm_lpss_read(pwm);
  100. ctrl &= ~PWM_ON_TIME_DIV_MASK;
  101. ctrl &= ~((base_unit_range - 1) << PWM_BASE_UNIT_SHIFT);
  102. base_unit &= (base_unit_range - 1);
  103. ctrl |= (u32) base_unit << PWM_BASE_UNIT_SHIFT;
  104. ctrl |= on_time_div;
  105. pwm_lpss_write(pwm, ctrl);
  106. /*
  107. * If the PWM is already enabled we need to notify the hardware
  108. * about the change by setting PWM_SW_UPDATE.
  109. */
  110. if (pwm_is_enabled(pwm))
  111. pwm_lpss_update(pwm);
  112. pm_runtime_put(chip->dev);
  113. return 0;
  114. }
  115. static int pwm_lpss_enable(struct pwm_chip *chip, struct pwm_device *pwm)
  116. {
  117. pm_runtime_get_sync(chip->dev);
  118. /*
  119. * Hardware must first see PWM_SW_UPDATE before the PWM can be
  120. * enabled.
  121. */
  122. pwm_lpss_update(pwm);
  123. pwm_lpss_write(pwm, pwm_lpss_read(pwm) | PWM_ENABLE);
  124. return 0;
  125. }
  126. static void pwm_lpss_disable(struct pwm_chip *chip, struct pwm_device *pwm)
  127. {
  128. pwm_lpss_write(pwm, pwm_lpss_read(pwm) & ~PWM_ENABLE);
  129. pm_runtime_put(chip->dev);
  130. }
  131. static const struct pwm_ops pwm_lpss_ops = {
  132. .config = pwm_lpss_config,
  133. .enable = pwm_lpss_enable,
  134. .disable = pwm_lpss_disable,
  135. .owner = THIS_MODULE,
  136. };
  137. struct pwm_lpss_chip *pwm_lpss_probe(struct device *dev, struct resource *r,
  138. const struct pwm_lpss_boardinfo *info)
  139. {
  140. struct pwm_lpss_chip *lpwm;
  141. unsigned long c;
  142. int ret;
  143. if (WARN_ON(info->npwm > MAX_PWMS))
  144. return ERR_PTR(-ENODEV);
  145. lpwm = devm_kzalloc(dev, sizeof(*lpwm), GFP_KERNEL);
  146. if (!lpwm)
  147. return ERR_PTR(-ENOMEM);
  148. lpwm->regs = devm_ioremap_resource(dev, r);
  149. if (IS_ERR(lpwm->regs))
  150. return ERR_CAST(lpwm->regs);
  151. lpwm->info = info;
  152. c = lpwm->info->clk_rate;
  153. if (!c)
  154. return ERR_PTR(-EINVAL);
  155. lpwm->chip.dev = dev;
  156. lpwm->chip.ops = &pwm_lpss_ops;
  157. lpwm->chip.base = -1;
  158. lpwm->chip.npwm = info->npwm;
  159. ret = pwmchip_add(&lpwm->chip);
  160. if (ret) {
  161. dev_err(dev, "failed to add PWM chip: %d\n", ret);
  162. return ERR_PTR(ret);
  163. }
  164. return lpwm;
  165. }
  166. EXPORT_SYMBOL_GPL(pwm_lpss_probe);
  167. int pwm_lpss_remove(struct pwm_lpss_chip *lpwm)
  168. {
  169. return pwmchip_remove(&lpwm->chip);
  170. }
  171. EXPORT_SYMBOL_GPL(pwm_lpss_remove);
  172. int pwm_lpss_suspend(struct device *dev)
  173. {
  174. struct pwm_lpss_chip *lpwm = dev_get_drvdata(dev);
  175. int i;
  176. for (i = 0; i < lpwm->info->npwm; i++)
  177. lpwm->saved_ctrl[i] = readl(lpwm->regs + i * PWM_SIZE + PWM);
  178. return 0;
  179. }
  180. EXPORT_SYMBOL_GPL(pwm_lpss_suspend);
  181. int pwm_lpss_resume(struct device *dev)
  182. {
  183. struct pwm_lpss_chip *lpwm = dev_get_drvdata(dev);
  184. int i;
  185. for (i = 0; i < lpwm->info->npwm; i++)
  186. writel(lpwm->saved_ctrl[i], lpwm->regs + i * PWM_SIZE + PWM);
  187. return 0;
  188. }
  189. EXPORT_SYMBOL_GPL(pwm_lpss_resume);
  190. MODULE_DESCRIPTION("PWM driver for Intel LPSS");
  191. MODULE_AUTHOR("Mika Westerberg <mika.westerberg@linux.intel.com>");
  192. MODULE_LICENSE("GPL v2");