pwm-lpss.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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/kernel.h>
  16. #include <linux/module.h>
  17. #include "pwm-lpss.h"
  18. #define PWM 0x00000000
  19. #define PWM_ENABLE BIT(31)
  20. #define PWM_SW_UPDATE BIT(30)
  21. #define PWM_BASE_UNIT_SHIFT 8
  22. #define PWM_BASE_UNIT_MASK 0x00ffff00
  23. #define PWM_ON_TIME_DIV_MASK 0x000000ff
  24. #define PWM_DIVISION_CORRECTION 0x2
  25. #define PWM_LIMIT (0x8000 + PWM_DIVISION_CORRECTION)
  26. #define NSECS_PER_SEC 1000000000UL
  27. struct pwm_lpss_chip {
  28. struct pwm_chip chip;
  29. void __iomem *regs;
  30. unsigned long clk_rate;
  31. };
  32. /* BayTrail */
  33. const struct pwm_lpss_boardinfo pwm_lpss_byt_info = {
  34. 25000000
  35. };
  36. EXPORT_SYMBOL_GPL(pwm_lpss_byt_info);
  37. /* Braswell */
  38. const struct pwm_lpss_boardinfo pwm_lpss_bsw_info = {
  39. 19200000
  40. };
  41. EXPORT_SYMBOL_GPL(pwm_lpss_bsw_info);
  42. static inline struct pwm_lpss_chip *to_lpwm(struct pwm_chip *chip)
  43. {
  44. return container_of(chip, struct pwm_lpss_chip, chip);
  45. }
  46. static int pwm_lpss_config(struct pwm_chip *chip, struct pwm_device *pwm,
  47. int duty_ns, int period_ns)
  48. {
  49. struct pwm_lpss_chip *lpwm = to_lpwm(chip);
  50. u8 on_time_div;
  51. unsigned long c;
  52. unsigned long long base_unit, freq = NSECS_PER_SEC;
  53. u32 ctrl;
  54. do_div(freq, period_ns);
  55. /* The equation is: base_unit = ((freq / c) * 65536) + correction */
  56. base_unit = freq * 65536;
  57. c = lpwm->clk_rate;
  58. if (!c)
  59. return -EINVAL;
  60. do_div(base_unit, c);
  61. base_unit += PWM_DIVISION_CORRECTION;
  62. if (base_unit > PWM_LIMIT)
  63. return -EINVAL;
  64. if (duty_ns <= 0)
  65. duty_ns = 1;
  66. on_time_div = 255 - (255 * duty_ns / period_ns);
  67. ctrl = readl(lpwm->regs + PWM);
  68. ctrl &= ~(PWM_BASE_UNIT_MASK | PWM_ON_TIME_DIV_MASK);
  69. ctrl |= (u16) base_unit << PWM_BASE_UNIT_SHIFT;
  70. ctrl |= on_time_div;
  71. /* request PWM to update on next cycle */
  72. ctrl |= PWM_SW_UPDATE;
  73. writel(ctrl, lpwm->regs + PWM);
  74. return 0;
  75. }
  76. static int pwm_lpss_enable(struct pwm_chip *chip, struct pwm_device *pwm)
  77. {
  78. struct pwm_lpss_chip *lpwm = to_lpwm(chip);
  79. u32 ctrl;
  80. ctrl = readl(lpwm->regs + PWM);
  81. writel(ctrl | PWM_ENABLE, lpwm->regs + PWM);
  82. return 0;
  83. }
  84. static void pwm_lpss_disable(struct pwm_chip *chip, struct pwm_device *pwm)
  85. {
  86. struct pwm_lpss_chip *lpwm = to_lpwm(chip);
  87. u32 ctrl;
  88. ctrl = readl(lpwm->regs + PWM);
  89. writel(ctrl & ~PWM_ENABLE, lpwm->regs + PWM);
  90. }
  91. static const struct pwm_ops pwm_lpss_ops = {
  92. .config = pwm_lpss_config,
  93. .enable = pwm_lpss_enable,
  94. .disable = pwm_lpss_disable,
  95. .owner = THIS_MODULE,
  96. };
  97. struct pwm_lpss_chip *pwm_lpss_probe(struct device *dev, struct resource *r,
  98. const struct pwm_lpss_boardinfo *info)
  99. {
  100. struct pwm_lpss_chip *lpwm;
  101. int ret;
  102. lpwm = devm_kzalloc(dev, sizeof(*lpwm), GFP_KERNEL);
  103. if (!lpwm)
  104. return ERR_PTR(-ENOMEM);
  105. lpwm->regs = devm_ioremap_resource(dev, r);
  106. if (IS_ERR(lpwm->regs))
  107. return ERR_CAST(lpwm->regs);
  108. lpwm->clk_rate = info->clk_rate;
  109. lpwm->chip.dev = dev;
  110. lpwm->chip.ops = &pwm_lpss_ops;
  111. lpwm->chip.base = -1;
  112. lpwm->chip.npwm = 1;
  113. ret = pwmchip_add(&lpwm->chip);
  114. if (ret) {
  115. dev_err(dev, "failed to add PWM chip: %d\n", ret);
  116. return ERR_PTR(ret);
  117. }
  118. return lpwm;
  119. }
  120. EXPORT_SYMBOL_GPL(pwm_lpss_probe);
  121. int pwm_lpss_remove(struct pwm_lpss_chip *lpwm)
  122. {
  123. u32 ctrl;
  124. ctrl = readl(lpwm->regs + PWM);
  125. writel(ctrl & ~PWM_ENABLE, lpwm->regs + PWM);
  126. return pwmchip_remove(&lpwm->chip);
  127. }
  128. EXPORT_SYMBOL_GPL(pwm_lpss_remove);
  129. MODULE_DESCRIPTION("PWM driver for Intel LPSS");
  130. MODULE_AUTHOR("Mika Westerberg <mika.westerberg@linux.intel.com>");
  131. MODULE_LICENSE("GPL v2");