pwm_regulator.c 3.7 KB

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