pwm_regulator.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (C) 2016 Rockchip Electronics Co., Ltd
  4. *
  5. * Based on kernel drivers/regulator/pwm-regulator.c
  6. * Copyright (C) 2014 - STMicroelectronics Inc.
  7. * Author: Lee Jones <lee.jones@linaro.org>
  8. */
  9. #include <common.h>
  10. #include <dm.h>
  11. #include <errno.h>
  12. #include <log.h>
  13. #include <pwm.h>
  14. #include <dm/device_compat.h>
  15. #include <power/regulator.h>
  16. DECLARE_GLOBAL_DATA_PTR;
  17. struct pwm_regulator_info {
  18. /* pwm id corresponding to the PWM driver */
  19. int pwm_id;
  20. /* the period of one PWM cycle */
  21. int period_ns;
  22. /*
  23. * the polarity of one PWM
  24. * 0: normal polarity
  25. * 1: inverted polarity
  26. */
  27. bool polarity;
  28. struct udevice *pwm;
  29. /* initialize voltage of regulator */
  30. int init_voltage;
  31. /* the maximum voltage of regulator */
  32. int max_voltage;
  33. /* the minimum voltage of regulator */
  34. int min_voltage;
  35. /* the current voltage of regulator */
  36. int volt_uV;
  37. };
  38. static int pwm_regulator_enable(struct udevice *dev, bool enable)
  39. {
  40. struct pwm_regulator_info *priv = dev_get_priv(dev);
  41. return pwm_set_enable(priv->pwm, priv->pwm_id, enable);
  42. }
  43. static int pwm_voltage_to_duty_cycle_percentage(struct udevice *dev, int req_uV)
  44. {
  45. struct pwm_regulator_info *priv = dev_get_priv(dev);
  46. int min_uV = priv->min_voltage;
  47. int max_uV = priv->max_voltage;
  48. int diff = max_uV - min_uV;
  49. return ((req_uV * 100) - (min_uV * 100)) / diff;
  50. }
  51. static int pwm_regulator_get_voltage(struct udevice *dev)
  52. {
  53. struct pwm_regulator_info *priv = dev_get_priv(dev);
  54. return priv->volt_uV;
  55. }
  56. static int pwm_regulator_set_voltage(struct udevice *dev, int uvolt)
  57. {
  58. struct pwm_regulator_info *priv = dev_get_priv(dev);
  59. int duty_cycle;
  60. int ret = 0;
  61. duty_cycle = pwm_voltage_to_duty_cycle_percentage(dev, uvolt);
  62. ret = pwm_set_invert(priv->pwm, priv->pwm_id, priv->polarity);
  63. if (ret) {
  64. dev_err(dev, "Failed to init PWM\n");
  65. return ret;
  66. }
  67. ret = pwm_set_config(priv->pwm, priv->pwm_id,
  68. priv->period_ns, (priv->period_ns / 100) * duty_cycle);
  69. if (ret) {
  70. dev_err(dev, "Failed to configure PWM\n");
  71. return ret;
  72. }
  73. priv->volt_uV = uvolt;
  74. return ret;
  75. }
  76. static int pwm_regulator_ofdata_to_platdata(struct udevice *dev)
  77. {
  78. struct pwm_regulator_info *priv = dev_get_priv(dev);
  79. struct ofnode_phandle_args args;
  80. int ret;
  81. ret = dev_read_phandle_with_args(dev, "pwms", "#pwm-cells", 0, 0, &args);
  82. if (ret) {
  83. debug("%s: Cannot get PWM phandle: ret=%d\n", __func__, ret);
  84. return ret;
  85. }
  86. priv->period_ns = args.args[1];
  87. priv->polarity = args.args[2];
  88. priv->init_voltage = dev_read_u32_default(dev, "regulator-init-microvolt", -1);
  89. if (priv->init_voltage < 0) {
  90. printf("Cannot find regulator pwm init_voltage\n");
  91. return -EINVAL;
  92. }
  93. ret = uclass_get_device_by_ofnode(UCLASS_PWM, args.node, &priv->pwm);
  94. if (ret) {
  95. debug("%s: Cannot get PWM: ret=%d\n", __func__, ret);
  96. return ret;
  97. }
  98. return 0;
  99. }
  100. static int pwm_regulator_probe(struct udevice *dev)
  101. {
  102. struct pwm_regulator_info *priv = dev_get_priv(dev);
  103. struct dm_regulator_uclass_platdata *uc_pdata;
  104. uc_pdata = dev_get_uclass_platdata(dev);
  105. uc_pdata->type = REGULATOR_TYPE_BUCK;
  106. uc_pdata->mode_count = 0;
  107. priv->max_voltage = uc_pdata->max_uV;
  108. priv->min_voltage = uc_pdata->min_uV;
  109. if (priv->init_voltage)
  110. pwm_regulator_set_voltage(dev, priv->init_voltage);
  111. return 0;
  112. }
  113. static const struct dm_regulator_ops pwm_regulator_ops = {
  114. .get_value = pwm_regulator_get_voltage,
  115. .set_value = pwm_regulator_set_voltage,
  116. .set_enable = pwm_regulator_enable,
  117. };
  118. static const struct udevice_id pwm_regulator_ids[] = {
  119. { .compatible = "pwm-regulator" },
  120. { }
  121. };
  122. U_BOOT_DRIVER(pwm_regulator) = {
  123. .name = "pwm_regulator",
  124. .id = UCLASS_REGULATOR,
  125. .ops = &pwm_regulator_ops,
  126. .probe = pwm_regulator_probe,
  127. .of_match = pwm_regulator_ids,
  128. .ofdata_to_platdata = pwm_regulator_ofdata_to_platdata,
  129. .priv_auto_alloc_size = sizeof(struct pwm_regulator_info),
  130. };