pwm-adp5585.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Analog Devices ADP5585 PWM driver
  4. *
  5. * Copyright 2022 NXP
  6. * Copyright 2024 Ideas on Board Oy
  7. *
  8. * Limitations:
  9. * - The .apply() operation executes atomically, but may not wait for the
  10. * period to complete (this is not documented and would need to be tested).
  11. * - Disabling the PWM drives the output pin to a low level immediately.
  12. * - The hardware can only generate normal polarity output.
  13. */
  14. #include <asm/byteorder.h>
  15. #include <linux/device.h>
  16. #include <linux/err.h>
  17. #include <linux/math64.h>
  18. #include <linux/mfd/adp5585.h>
  19. #include <linux/minmax.h>
  20. #include <linux/module.h>
  21. #include <linux/platform_device.h>
  22. #include <linux/pwm.h>
  23. #include <linux/regmap.h>
  24. #include <linux/time.h>
  25. #include <linux/types.h>
  26. #define ADP5585_PWM_CHAN_NUM 1
  27. #define ADP5585_PWM_OSC_FREQ_HZ 1000000U
  28. #define ADP5585_PWM_MIN_PERIOD_NS (2ULL * NSEC_PER_SEC / ADP5585_PWM_OSC_FREQ_HZ)
  29. #define ADP5585_PWM_MAX_PERIOD_NS (2ULL * 0xffff * NSEC_PER_SEC / ADP5585_PWM_OSC_FREQ_HZ)
  30. static int pwm_adp5585_request(struct pwm_chip *chip, struct pwm_device *pwm)
  31. {
  32. struct regmap *regmap = pwmchip_get_drvdata(chip);
  33. /* Configure the R3 pin as PWM output. */
  34. return regmap_update_bits(regmap, ADP5585_PIN_CONFIG_C,
  35. ADP5585_R3_EXTEND_CFG_MASK,
  36. ADP5585_R3_EXTEND_CFG_PWM_OUT);
  37. }
  38. static void pwm_adp5585_free(struct pwm_chip *chip, struct pwm_device *pwm)
  39. {
  40. struct regmap *regmap = pwmchip_get_drvdata(chip);
  41. regmap_update_bits(regmap, ADP5585_PIN_CONFIG_C,
  42. ADP5585_R3_EXTEND_CFG_MASK,
  43. ADP5585_R3_EXTEND_CFG_GPIO4);
  44. }
  45. static int pwm_adp5585_apply(struct pwm_chip *chip,
  46. struct pwm_device *pwm,
  47. const struct pwm_state *state)
  48. {
  49. struct regmap *regmap = pwmchip_get_drvdata(chip);
  50. u64 period, duty_cycle;
  51. u32 on, off;
  52. __le16 val;
  53. int ret;
  54. if (!state->enabled) {
  55. regmap_clear_bits(regmap, ADP5585_GENERAL_CFG, ADP5585_OSC_EN);
  56. regmap_clear_bits(regmap, ADP5585_PWM_CFG, ADP5585_PWM_EN);
  57. return 0;
  58. }
  59. if (state->polarity != PWM_POLARITY_NORMAL)
  60. return -EINVAL;
  61. if (state->period < ADP5585_PWM_MIN_PERIOD_NS)
  62. return -EINVAL;
  63. period = min(state->period, ADP5585_PWM_MAX_PERIOD_NS);
  64. duty_cycle = min(state->duty_cycle, period);
  65. /*
  66. * Compute the on and off time. As the internal oscillator frequency is
  67. * 1MHz, the calculation can be simplified without loss of precision.
  68. */
  69. on = div_u64(duty_cycle, NSEC_PER_SEC / ADP5585_PWM_OSC_FREQ_HZ);
  70. off = div_u64(period, NSEC_PER_SEC / ADP5585_PWM_OSC_FREQ_HZ) - on;
  71. val = cpu_to_le16(off);
  72. ret = regmap_bulk_write(regmap, ADP5585_PWM_OFFT_LOW, &val, 2);
  73. if (ret)
  74. return ret;
  75. val = cpu_to_le16(on);
  76. ret = regmap_bulk_write(regmap, ADP5585_PWM_ONT_LOW, &val, 2);
  77. if (ret)
  78. return ret;
  79. /* Enable PWM in continuous mode and no external AND'ing. */
  80. ret = regmap_update_bits(regmap, ADP5585_PWM_CFG,
  81. ADP5585_PWM_IN_AND | ADP5585_PWM_MODE |
  82. ADP5585_PWM_EN, ADP5585_PWM_EN);
  83. if (ret)
  84. return ret;
  85. ret = regmap_set_bits(regmap, ADP5585_GENERAL_CFG, ADP5585_OSC_EN);
  86. if (ret)
  87. return ret;
  88. return regmap_set_bits(regmap, ADP5585_PWM_CFG, ADP5585_PWM_EN);
  89. }
  90. static int pwm_adp5585_get_state(struct pwm_chip *chip,
  91. struct pwm_device *pwm,
  92. struct pwm_state *state)
  93. {
  94. struct regmap *regmap = pwmchip_get_drvdata(chip);
  95. unsigned int on, off;
  96. unsigned int val;
  97. __le16 on_off;
  98. int ret;
  99. ret = regmap_bulk_read(regmap, ADP5585_PWM_OFFT_LOW, &on_off, 2);
  100. if (ret)
  101. return ret;
  102. off = le16_to_cpu(on_off);
  103. ret = regmap_bulk_read(regmap, ADP5585_PWM_ONT_LOW, &on_off, 2);
  104. if (ret)
  105. return ret;
  106. on = le16_to_cpu(on_off);
  107. state->duty_cycle = on * (NSEC_PER_SEC / ADP5585_PWM_OSC_FREQ_HZ);
  108. state->period = (on + off) * (NSEC_PER_SEC / ADP5585_PWM_OSC_FREQ_HZ);
  109. state->polarity = PWM_POLARITY_NORMAL;
  110. regmap_read(regmap, ADP5585_PWM_CFG, &val);
  111. state->enabled = !!(val & ADP5585_PWM_EN);
  112. return 0;
  113. }
  114. static const struct pwm_ops adp5585_pwm_ops = {
  115. .request = pwm_adp5585_request,
  116. .free = pwm_adp5585_free,
  117. .apply = pwm_adp5585_apply,
  118. .get_state = pwm_adp5585_get_state,
  119. };
  120. static int adp5585_pwm_probe(struct platform_device *pdev)
  121. {
  122. struct device *dev = &pdev->dev;
  123. struct adp5585_dev *adp5585 = dev_get_drvdata(dev->parent);
  124. struct pwm_chip *chip;
  125. int ret;
  126. chip = devm_pwmchip_alloc(dev, ADP5585_PWM_CHAN_NUM, 0);
  127. if (IS_ERR(chip))
  128. return PTR_ERR(chip);
  129. device_set_of_node_from_dev(dev, dev->parent);
  130. pwmchip_set_drvdata(chip, adp5585->regmap);
  131. chip->ops = &adp5585_pwm_ops;
  132. ret = devm_pwmchip_add(dev, chip);
  133. if (ret)
  134. return dev_err_probe(dev, ret, "failed to add PWM chip\n");
  135. return 0;
  136. }
  137. static const struct platform_device_id adp5585_pwm_id_table[] = {
  138. { "adp5585-pwm" },
  139. { /* Sentinel */ }
  140. };
  141. MODULE_DEVICE_TABLE(platform, adp5585_pwm_id_table);
  142. static struct platform_driver adp5585_pwm_driver = {
  143. .driver = {
  144. .name = "adp5585-pwm",
  145. },
  146. .probe = adp5585_pwm_probe,
  147. .id_table = adp5585_pwm_id_table,
  148. };
  149. module_platform_driver(adp5585_pwm_driver);
  150. MODULE_AUTHOR("Xiaoning Wang <xiaoning.wang@nxp.com>");
  151. MODULE_DESCRIPTION("ADP5585 PWM Driver");
  152. MODULE_LICENSE("GPL");