pwm-lpss.c 3.9 KB

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