tps65090_regulator.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (c) 2015 Google, Inc
  4. */
  5. #include <common.h>
  6. #include <dm.h>
  7. #include <errno.h>
  8. #include <log.h>
  9. #include <linux/delay.h>
  10. #include <power/pmic.h>
  11. #include <power/regulator.h>
  12. #include <power/tps65090.h>
  13. static int tps65090_fet_probe(struct udevice *dev)
  14. {
  15. struct dm_regulator_uclass_plat *uc_pdata;
  16. uc_pdata = dev_get_uclass_plat(dev);
  17. uc_pdata->type = REGULATOR_TYPE_OTHER;
  18. uc_pdata->mode_count = 0;
  19. return 0;
  20. }
  21. static int tps65090_fet_get_enable(struct udevice *dev)
  22. {
  23. struct udevice *pmic = dev_get_parent(dev);
  24. int ret, fet_id;
  25. fet_id = dev->driver_data;
  26. debug("%s: fet_id=%d\n", __func__, fet_id);
  27. ret = pmic_reg_read(pmic, REG_FET_BASE + fet_id);
  28. if (ret < 0)
  29. return ret;
  30. return ret & FET_CTRL_ENFET;
  31. }
  32. /**
  33. * Set the power state for a FET
  34. *
  35. * @param pmic pmic structure for the tps65090
  36. * @param fet_id FET number to set (1..MAX_FET_NUM)
  37. * @param set 1 to power on FET, 0 to power off
  38. * @return -EIO if we got a comms error, -EAGAIN if the FET failed to
  39. * change state. If all is ok, returns 0.
  40. */
  41. static int tps65090_fet_set(struct udevice *pmic, int fet_id, bool set)
  42. {
  43. int retry;
  44. u32 value;
  45. int ret;
  46. value = FET_CTRL_ADENFET | FET_CTRL_WAIT;
  47. if (set)
  48. value |= FET_CTRL_ENFET;
  49. if (pmic_reg_write(pmic, REG_FET_BASE + fet_id, value))
  50. return -EIO;
  51. /* Try reading until we get a result */
  52. for (retry = 0; retry < MAX_CTRL_READ_TRIES; retry++) {
  53. ret = pmic_reg_read(pmic, REG_FET_BASE + fet_id);
  54. if (ret < 0)
  55. return ret;
  56. /* Check that the FET went into the expected state */
  57. debug("%s: flags=%x\n", __func__, ret);
  58. if (!!(ret & FET_CTRL_PGFET) == set)
  59. return 0;
  60. /* If we got a timeout, there is no point in waiting longer */
  61. if (ret & FET_CTRL_TOFET)
  62. break;
  63. mdelay(1);
  64. }
  65. debug("FET %d: Power good should have set to %d but reg=%#02x\n",
  66. fet_id, set, ret);
  67. return -EAGAIN;
  68. }
  69. static int tps65090_fet_set_enable(struct udevice *dev, bool enable)
  70. {
  71. struct udevice *pmic = dev_get_parent(dev);
  72. int ret, fet_id;
  73. ulong start;
  74. int loops;
  75. fet_id = dev->driver_data;
  76. debug("%s: fet_id=%d, enable=%d\n", __func__, fet_id, enable);
  77. start = get_timer(0);
  78. for (loops = 0;; loops++) {
  79. ret = tps65090_fet_set(pmic, fet_id, enable);
  80. if (!ret)
  81. break;
  82. if (get_timer(start) > 100)
  83. break;
  84. /* Turn it off and try again until we time out */
  85. tps65090_fet_set(pmic, fet_id, false);
  86. }
  87. if (ret)
  88. debug("%s: FET%d failed to power on: time=%lums, loops=%d\n",
  89. __func__, fet_id, get_timer(start), loops);
  90. else if (loops)
  91. debug("%s: FET%d powered on after %lums, loops=%d\n",
  92. __func__, fet_id, get_timer(start), loops);
  93. /*
  94. * Unfortunately there are some conditions where the power-good bit
  95. * will be 0, but the FET still comes up. One such case occurs with
  96. * the LCD backlight on snow. We'll just return 0 here and assume
  97. * that the FET will eventually come up.
  98. */
  99. if (ret == -EAGAIN)
  100. ret = 0;
  101. return ret;
  102. }
  103. static const struct dm_regulator_ops tps65090_fet_ops = {
  104. .get_enable = tps65090_fet_get_enable,
  105. .set_enable = tps65090_fet_set_enable,
  106. };
  107. U_BOOT_DRIVER(tps65090_fet) = {
  108. .name = TPS65090_FET_DRIVER,
  109. .id = UCLASS_REGULATOR,
  110. .ops = &tps65090_fet_ops,
  111. .probe = tps65090_fet_probe,
  112. };