2 * Copyright (c) 2015 Google, Inc
4 * SPDX-License-Identifier: GPL-2.0+
10 #include <power/pmic.h>
11 #include <power/regulator.h>
12 #include <power/tps65090.h>
14 static int tps65090_fet_probe(struct udevice *dev)
16 struct dm_regulator_uclass_platdata *uc_pdata;
18 uc_pdata = dev_get_uclass_platdata(dev);
20 uc_pdata->type = REGULATOR_TYPE_OTHER;
21 uc_pdata->mode_count = 0;
26 static bool tps65090_fet_get_enable(struct udevice *dev)
28 struct udevice *pmic = dev_get_parent(dev);
31 fet_id = dev->driver_data;
32 debug("%s: fet_id=%d\n", __func__, fet_id);
34 ret = pmic_reg_read(pmic, REG_FET_BASE + fet_id);
38 return ret & FET_CTRL_ENFET;
42 * Set the power state for a FET
44 * @param pmic pmic structure for the tps65090
45 * @param fet_id FET number to set (1..MAX_FET_NUM)
46 * @param set 1 to power on FET, 0 to power off
47 * @return -EIO if we got a comms error, -EAGAIN if the FET failed to
48 * change state. If all is ok, returns 0.
50 static int tps65090_fet_set(struct udevice *pmic, int fet_id, bool set)
56 value = FET_CTRL_ADENFET | FET_CTRL_WAIT;
58 value |= FET_CTRL_ENFET;
60 if (pmic_reg_write(pmic, REG_FET_BASE + fet_id, value))
63 /* Try reading until we get a result */
64 for (retry = 0; retry < MAX_CTRL_READ_TRIES; retry++) {
65 ret = pmic_reg_read(pmic, REG_FET_BASE + fet_id);
69 /* Check that the FET went into the expected state */
70 debug("%s: flags=%x\n", __func__, ret);
71 if (!!(ret & FET_CTRL_PGFET) == set)
74 /* If we got a timeout, there is no point in waiting longer */
75 if (ret & FET_CTRL_TOFET)
81 debug("FET %d: Power good should have set to %d but reg=%#02x\n",
86 static int tps65090_fet_set_enable(struct udevice *dev, bool enable)
88 struct udevice *pmic = dev_get_parent(dev);
93 fet_id = dev->driver_data;
94 debug("%s: fet_id=%d, enable=%d\n", __func__, fet_id, enable);
97 for (loops = 0;; loops++) {
98 ret = tps65090_fet_set(pmic, fet_id, enable);
102 if (get_timer(start) > 100)
105 /* Turn it off and try again until we time out */
106 tps65090_fet_set(pmic, fet_id, false);
110 debug("%s: FET%d failed to power on: time=%lums, loops=%d\n",
111 __func__, fet_id, get_timer(start), loops);
113 debug("%s: FET%d powered on after %lums, loops=%d\n",
114 __func__, fet_id, get_timer(start), loops);
117 * Unfortunately there are some conditions where the power-good bit
118 * will be 0, but the FET still comes up. One such case occurs with
119 * the LCD backlight on snow. We'll just return 0 here and assume
120 * that the FET will eventually come up.
128 static const struct dm_regulator_ops tps65090_fet_ops = {
129 .get_enable = tps65090_fet_get_enable,
130 .set_enable = tps65090_fet_set_enable,
133 U_BOOT_DRIVER(tps65090_fet) = {
134 .name = TPS65090_FET_DRIVER,
135 .id = UCLASS_REGULATOR,
136 .ops = &tps65090_fet_ops,
137 .probe = tps65090_fet_probe,