pwm-lpss.c 4.9 KB

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