1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (c) 2015 Google, Inc
9 #include <power/pmic.h>
10 #include <power/regulator.h>
11 #include <power/tps65090.h>
13 static int tps65090_fet_probe(struct udevice *dev)
15 struct dm_regulator_uclass_platdata *uc_pdata;
17 uc_pdata = dev_get_uclass_platdata(dev);
19 uc_pdata->type = REGULATOR_TYPE_OTHER;
20 uc_pdata->mode_count = 0;
25 static int tps65090_fet_get_enable(struct udevice *dev)
27 struct udevice *pmic = dev_get_parent(dev);
30 fet_id = dev->driver_data;
31 debug("%s: fet_id=%d\n", __func__, fet_id);
33 ret = pmic_reg_read(pmic, REG_FET_BASE + fet_id);
37 return ret & FET_CTRL_ENFET;
41 * Set the power state for a FET
43 * @param pmic pmic structure for the tps65090
44 * @param fet_id FET number to set (1..MAX_FET_NUM)
45 * @param set 1 to power on FET, 0 to power off
46 * @return -EIO if we got a comms error, -EAGAIN if the FET failed to
47 * change state. If all is ok, returns 0.
49 static int tps65090_fet_set(struct udevice *pmic, int fet_id, bool set)
55 value = FET_CTRL_ADENFET | FET_CTRL_WAIT;
57 value |= FET_CTRL_ENFET;
59 if (pmic_reg_write(pmic, REG_FET_BASE + fet_id, value))
62 /* Try reading until we get a result */
63 for (retry = 0; retry < MAX_CTRL_READ_TRIES; retry++) {
64 ret = pmic_reg_read(pmic, REG_FET_BASE + fet_id);
68 /* Check that the FET went into the expected state */
69 debug("%s: flags=%x\n", __func__, ret);
70 if (!!(ret & FET_CTRL_PGFET) == set)
73 /* If we got a timeout, there is no point in waiting longer */
74 if (ret & FET_CTRL_TOFET)
80 debug("FET %d: Power good should have set to %d but reg=%#02x\n",
85 static int tps65090_fet_set_enable(struct udevice *dev, bool enable)
87 struct udevice *pmic = dev_get_parent(dev);
92 fet_id = dev->driver_data;
93 debug("%s: fet_id=%d, enable=%d\n", __func__, fet_id, enable);
96 for (loops = 0;; loops++) {
97 ret = tps65090_fet_set(pmic, fet_id, enable);
101 if (get_timer(start) > 100)
104 /* Turn it off and try again until we time out */
105 tps65090_fet_set(pmic, fet_id, false);
109 debug("%s: FET%d failed to power on: time=%lums, loops=%d\n",
110 __func__, fet_id, get_timer(start), loops);
112 debug("%s: FET%d powered on after %lums, loops=%d\n",
113 __func__, fet_id, get_timer(start), loops);
116 * Unfortunately there are some conditions where the power-good bit
117 * will be 0, but the FET still comes up. One such case occurs with
118 * the LCD backlight on snow. We'll just return 0 here and assume
119 * that the FET will eventually come up.
127 static const struct dm_regulator_ops tps65090_fet_ops = {
128 .get_enable = tps65090_fet_get_enable,
129 .set_enable = tps65090_fet_set_enable,
132 U_BOOT_DRIVER(tps65090_fet) = {
133 .name = TPS65090_FET_DRIVER,
134 .id = UCLASS_REGULATOR,
135 .ops = &tps65090_fet_ops,
136 .probe = tps65090_fet_probe,