pwm-crc.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2015 Intel Corporation. All rights reserved.
  4. *
  5. * Author: Shobhit Kumar <shobhit.kumar@intel.com>
  6. */
  7. #include <linux/platform_device.h>
  8. #include <linux/regmap.h>
  9. #include <linux/mfd/intel_soc_pmic.h>
  10. #include <linux/pwm.h>
  11. #define PWM0_CLK_DIV 0x4B
  12. #define PWM_OUTPUT_ENABLE BIT(7)
  13. #define PWM_DIV_CLK_0 0x00 /* DIVIDECLK = BASECLK */
  14. #define PWM_DIV_CLK_100 0x63 /* DIVIDECLK = BASECLK/100 */
  15. #define PWM_DIV_CLK_128 0x7F /* DIVIDECLK = BASECLK/128 */
  16. #define PWM0_DUTY_CYCLE 0x4E
  17. #define BACKLIGHT_EN 0x51
  18. #define PWM_MAX_LEVEL 0xFF
  19. #define PWM_BASE_CLK_MHZ 6 /* 6 MHz */
  20. #define PWM_MAX_PERIOD_NS 5461334 /* 183 Hz */
  21. /**
  22. * struct crystalcove_pwm - Crystal Cove PWM controller
  23. * @regmap: the regmap from the parent device.
  24. */
  25. struct crystalcove_pwm {
  26. struct regmap *regmap;
  27. };
  28. static inline struct crystalcove_pwm *to_crc_pwm(struct pwm_chip *chip)
  29. {
  30. return pwmchip_get_drvdata(chip);
  31. }
  32. static int crc_pwm_calc_clk_div(int period_ns)
  33. {
  34. int clk_div;
  35. clk_div = PWM_BASE_CLK_MHZ * period_ns / (256 * NSEC_PER_USEC);
  36. /* clk_div 1 - 128, maps to register values 0-127 */
  37. if (clk_div > 0)
  38. clk_div--;
  39. return clk_div;
  40. }
  41. static int crc_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm,
  42. const struct pwm_state *state)
  43. {
  44. struct crystalcove_pwm *crc_pwm = to_crc_pwm(chip);
  45. struct device *dev = pwmchip_parent(chip);
  46. int err;
  47. if (state->period > PWM_MAX_PERIOD_NS) {
  48. dev_err(dev, "un-supported period_ns\n");
  49. return -EINVAL;
  50. }
  51. if (state->polarity != PWM_POLARITY_NORMAL)
  52. return -EINVAL;
  53. if (pwm_is_enabled(pwm) && !state->enabled) {
  54. err = regmap_write(crc_pwm->regmap, BACKLIGHT_EN, 0);
  55. if (err) {
  56. dev_err(dev, "Error writing BACKLIGHT_EN %d\n", err);
  57. return err;
  58. }
  59. }
  60. if (pwm_get_duty_cycle(pwm) != state->duty_cycle ||
  61. pwm_get_period(pwm) != state->period) {
  62. u64 level = state->duty_cycle * PWM_MAX_LEVEL;
  63. do_div(level, state->period);
  64. err = regmap_write(crc_pwm->regmap, PWM0_DUTY_CYCLE, level);
  65. if (err) {
  66. dev_err(dev, "Error writing PWM0_DUTY_CYCLE %d\n", err);
  67. return err;
  68. }
  69. }
  70. if (pwm_is_enabled(pwm) && state->enabled &&
  71. pwm_get_period(pwm) != state->period) {
  72. /* changing the clk divisor, clear PWM_OUTPUT_ENABLE first */
  73. err = regmap_write(crc_pwm->regmap, PWM0_CLK_DIV, 0);
  74. if (err) {
  75. dev_err(dev, "Error writing PWM0_CLK_DIV %d\n", err);
  76. return err;
  77. }
  78. }
  79. if (pwm_get_period(pwm) != state->period ||
  80. pwm_is_enabled(pwm) != state->enabled) {
  81. int clk_div = crc_pwm_calc_clk_div(state->period);
  82. int pwm_output_enable = state->enabled ? PWM_OUTPUT_ENABLE : 0;
  83. err = regmap_write(crc_pwm->regmap, PWM0_CLK_DIV,
  84. clk_div | pwm_output_enable);
  85. if (err) {
  86. dev_err(dev, "Error writing PWM0_CLK_DIV %d\n", err);
  87. return err;
  88. }
  89. }
  90. if (!pwm_is_enabled(pwm) && state->enabled) {
  91. err = regmap_write(crc_pwm->regmap, BACKLIGHT_EN, 1);
  92. if (err) {
  93. dev_err(dev, "Error writing BACKLIGHT_EN %d\n", err);
  94. return err;
  95. }
  96. }
  97. return 0;
  98. }
  99. static int crc_pwm_get_state(struct pwm_chip *chip, struct pwm_device *pwm,
  100. struct pwm_state *state)
  101. {
  102. struct crystalcove_pwm *crc_pwm = to_crc_pwm(chip);
  103. struct device *dev = pwmchip_parent(chip);
  104. unsigned int clk_div, clk_div_reg, duty_cycle_reg;
  105. int error;
  106. error = regmap_read(crc_pwm->regmap, PWM0_CLK_DIV, &clk_div_reg);
  107. if (error) {
  108. dev_err(dev, "Error reading PWM0_CLK_DIV %d\n", error);
  109. return error;
  110. }
  111. error = regmap_read(crc_pwm->regmap, PWM0_DUTY_CYCLE, &duty_cycle_reg);
  112. if (error) {
  113. dev_err(dev, "Error reading PWM0_DUTY_CYCLE %d\n", error);
  114. return error;
  115. }
  116. clk_div = (clk_div_reg & ~PWM_OUTPUT_ENABLE) + 1;
  117. state->period =
  118. DIV_ROUND_UP(clk_div * NSEC_PER_USEC * 256, PWM_BASE_CLK_MHZ);
  119. state->duty_cycle =
  120. DIV_ROUND_UP_ULL(duty_cycle_reg * state->period, PWM_MAX_LEVEL);
  121. state->polarity = PWM_POLARITY_NORMAL;
  122. state->enabled = !!(clk_div_reg & PWM_OUTPUT_ENABLE);
  123. return 0;
  124. }
  125. static const struct pwm_ops crc_pwm_ops = {
  126. .apply = crc_pwm_apply,
  127. .get_state = crc_pwm_get_state,
  128. };
  129. static int crystalcove_pwm_probe(struct platform_device *pdev)
  130. {
  131. struct pwm_chip *chip;
  132. struct crystalcove_pwm *crc_pwm;
  133. struct device *dev = pdev->dev.parent;
  134. struct intel_soc_pmic *pmic = dev_get_drvdata(dev);
  135. chip = devm_pwmchip_alloc(&pdev->dev, 1, sizeof(*crc_pwm));
  136. if (IS_ERR(chip))
  137. return PTR_ERR(chip);
  138. crc_pwm = to_crc_pwm(chip);
  139. chip->ops = &crc_pwm_ops;
  140. /* get the PMIC regmap */
  141. crc_pwm->regmap = pmic->regmap;
  142. return devm_pwmchip_add(&pdev->dev, chip);
  143. }
  144. static struct platform_driver crystalcove_pwm_driver = {
  145. .probe = crystalcove_pwm_probe,
  146. .driver = {
  147. .name = "crystal_cove_pwm",
  148. },
  149. };
  150. module_platform_driver(crystalcove_pwm_driver);
  151. MODULE_ALIAS("platform:crystal_cove_pwm");
  152. MODULE_DESCRIPTION("Intel Crystalcove (CRC) PWM support");
  153. MODULE_LICENSE("GPL");