pwm-intel-lgm.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2020 Intel Corporation.
  4. *
  5. * Limitations:
  6. * - The hardware supports fixed period & configures only 2-wire mode.
  7. * - Supports normal polarity. Does not support changing polarity.
  8. * - When PWM is disabled, output of PWM will become 0(inactive). It doesn't
  9. * keep track of running period.
  10. * - When duty cycle is changed, PWM output may be a mix of previous setting
  11. * and new setting for the first period. From second period, the output is
  12. * based on new setting.
  13. * - It is a dedicated PWM fan controller. There are no other consumers for
  14. * this PWM controller.
  15. */
  16. #include <linux/bitfield.h>
  17. #include <linux/clk.h>
  18. #include <linux/module.h>
  19. #include <linux/platform_device.h>
  20. #include <linux/mod_devicetable.h>
  21. #include <linux/pwm.h>
  22. #include <linux/regmap.h>
  23. #include <linux/reset.h>
  24. #define LGM_PWM_FAN_CON0 0x0
  25. #define LGM_PWM_FAN_EN_EN BIT(0)
  26. #define LGM_PWM_FAN_EN_DIS 0x0
  27. #define LGM_PWM_FAN_EN_MSK BIT(0)
  28. #define LGM_PWM_FAN_MODE_2WIRE 0x0
  29. #define LGM_PWM_FAN_MODE_MSK BIT(1)
  30. #define LGM_PWM_FAN_DC_MSK GENMASK(23, 16)
  31. #define LGM_PWM_FAN_CON1 0x4
  32. #define LGM_PWM_FAN_MAX_RPM_MSK GENMASK(15, 0)
  33. #define LGM_PWM_MAX_RPM (BIT(16) - 1)
  34. #define LGM_PWM_DEFAULT_RPM 4000
  35. #define LGM_PWM_MAX_DUTY_CYCLE (BIT(8) - 1)
  36. #define LGM_PWM_DC_BITS 8
  37. #define LGM_PWM_PERIOD_2WIRE_NS (40 * NSEC_PER_MSEC)
  38. struct lgm_pwm_chip {
  39. struct pwm_chip chip;
  40. struct regmap *regmap;
  41. u32 period;
  42. };
  43. static inline struct lgm_pwm_chip *to_lgm_pwm_chip(struct pwm_chip *chip)
  44. {
  45. return container_of(chip, struct lgm_pwm_chip, chip);
  46. }
  47. static int lgm_pwm_enable(struct pwm_chip *chip, bool enable)
  48. {
  49. struct lgm_pwm_chip *pc = to_lgm_pwm_chip(chip);
  50. struct regmap *regmap = pc->regmap;
  51. return regmap_update_bits(regmap, LGM_PWM_FAN_CON0, LGM_PWM_FAN_EN_MSK,
  52. enable ? LGM_PWM_FAN_EN_EN : LGM_PWM_FAN_EN_DIS);
  53. }
  54. static int lgm_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm,
  55. const struct pwm_state *state)
  56. {
  57. struct lgm_pwm_chip *pc = to_lgm_pwm_chip(chip);
  58. u32 duty_cycle, val;
  59. int ret;
  60. /* The hardware only supports normal polarity and fixed period. */
  61. if (state->polarity != PWM_POLARITY_NORMAL || state->period < pc->period)
  62. return -EINVAL;
  63. if (!state->enabled)
  64. return lgm_pwm_enable(chip, 0);
  65. duty_cycle = min_t(u64, state->duty_cycle, pc->period);
  66. val = duty_cycle * LGM_PWM_MAX_DUTY_CYCLE / pc->period;
  67. ret = regmap_update_bits(pc->regmap, LGM_PWM_FAN_CON0, LGM_PWM_FAN_DC_MSK,
  68. FIELD_PREP(LGM_PWM_FAN_DC_MSK, val));
  69. if (ret)
  70. return ret;
  71. return lgm_pwm_enable(chip, 1);
  72. }
  73. static int lgm_pwm_get_state(struct pwm_chip *chip, struct pwm_device *pwm,
  74. struct pwm_state *state)
  75. {
  76. struct lgm_pwm_chip *pc = to_lgm_pwm_chip(chip);
  77. u32 duty, val;
  78. state->enabled = regmap_test_bits(pc->regmap, LGM_PWM_FAN_CON0,
  79. LGM_PWM_FAN_EN_EN);
  80. state->polarity = PWM_POLARITY_NORMAL;
  81. state->period = pc->period; /* fixed period */
  82. regmap_read(pc->regmap, LGM_PWM_FAN_CON0, &val);
  83. duty = FIELD_GET(LGM_PWM_FAN_DC_MSK, val);
  84. state->duty_cycle = DIV_ROUND_UP(duty * pc->period, LGM_PWM_MAX_DUTY_CYCLE);
  85. return 0;
  86. }
  87. static const struct pwm_ops lgm_pwm_ops = {
  88. .get_state = lgm_pwm_get_state,
  89. .apply = lgm_pwm_apply,
  90. .owner = THIS_MODULE,
  91. };
  92. static void lgm_pwm_init(struct lgm_pwm_chip *pc)
  93. {
  94. struct regmap *regmap = pc->regmap;
  95. u32 con0_val;
  96. con0_val = FIELD_PREP(LGM_PWM_FAN_MODE_MSK, LGM_PWM_FAN_MODE_2WIRE);
  97. pc->period = LGM_PWM_PERIOD_2WIRE_NS;
  98. regmap_update_bits(regmap, LGM_PWM_FAN_CON1, LGM_PWM_FAN_MAX_RPM_MSK,
  99. LGM_PWM_DEFAULT_RPM);
  100. regmap_update_bits(regmap, LGM_PWM_FAN_CON0, LGM_PWM_FAN_MODE_MSK,
  101. con0_val);
  102. }
  103. static const struct regmap_config lgm_pwm_regmap_config = {
  104. .reg_bits = 32,
  105. .reg_stride = 4,
  106. .val_bits = 32,
  107. };
  108. static void lgm_clk_release(void *data)
  109. {
  110. struct clk *clk = data;
  111. clk_disable_unprepare(clk);
  112. }
  113. static int lgm_clk_enable(struct device *dev, struct clk *clk)
  114. {
  115. int ret;
  116. ret = clk_prepare_enable(clk);
  117. if (ret)
  118. return ret;
  119. return devm_add_action_or_reset(dev, lgm_clk_release, clk);
  120. }
  121. static void lgm_reset_control_release(void *data)
  122. {
  123. struct reset_control *rst = data;
  124. reset_control_assert(rst);
  125. }
  126. static int lgm_reset_control_deassert(struct device *dev, struct reset_control *rst)
  127. {
  128. int ret;
  129. ret = reset_control_deassert(rst);
  130. if (ret)
  131. return ret;
  132. return devm_add_action_or_reset(dev, lgm_reset_control_release, rst);
  133. }
  134. static int lgm_pwm_probe(struct platform_device *pdev)
  135. {
  136. struct device *dev = &pdev->dev;
  137. struct reset_control *rst;
  138. struct lgm_pwm_chip *pc;
  139. void __iomem *io_base;
  140. struct clk *clk;
  141. int ret;
  142. pc = devm_kzalloc(dev, sizeof(*pc), GFP_KERNEL);
  143. if (!pc)
  144. return -ENOMEM;
  145. io_base = devm_platform_ioremap_resource(pdev, 0);
  146. if (IS_ERR(io_base))
  147. return PTR_ERR(io_base);
  148. pc->regmap = devm_regmap_init_mmio(dev, io_base, &lgm_pwm_regmap_config);
  149. if (IS_ERR(pc->regmap))
  150. return dev_err_probe(dev, PTR_ERR(pc->regmap),
  151. "failed to init register map\n");
  152. clk = devm_clk_get(dev, NULL);
  153. if (IS_ERR(clk))
  154. return dev_err_probe(dev, PTR_ERR(clk), "failed to get clock\n");
  155. ret = lgm_clk_enable(dev, clk);
  156. if (ret)
  157. return dev_err_probe(dev, ret, "failed to enable clock\n");
  158. rst = devm_reset_control_get_exclusive(dev, NULL);
  159. if (IS_ERR(rst))
  160. return dev_err_probe(dev, PTR_ERR(rst),
  161. "failed to get reset control\n");
  162. ret = lgm_reset_control_deassert(dev, rst);
  163. if (ret)
  164. return dev_err_probe(dev, ret, "cannot deassert reset control\n");
  165. pc->chip.dev = dev;
  166. pc->chip.ops = &lgm_pwm_ops;
  167. pc->chip.npwm = 1;
  168. lgm_pwm_init(pc);
  169. ret = devm_pwmchip_add(dev, &pc->chip);
  170. if (ret < 0)
  171. return dev_err_probe(dev, ret, "failed to add PWM chip\n");
  172. return 0;
  173. }
  174. static const struct of_device_id lgm_pwm_of_match[] = {
  175. { .compatible = "intel,lgm-pwm" },
  176. { }
  177. };
  178. MODULE_DEVICE_TABLE(of, lgm_pwm_of_match);
  179. static struct platform_driver lgm_pwm_driver = {
  180. .driver = {
  181. .name = "intel-pwm",
  182. .of_match_table = lgm_pwm_of_match,
  183. },
  184. .probe = lgm_pwm_probe,
  185. };
  186. module_platform_driver(lgm_pwm_driver);
  187. MODULE_LICENSE("GPL v2");