1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (c) 2015 Google, Inc
10 #include <linux/delay.h>
11 #include <power/pmic.h>
12 #include <power/regulator.h>
13 #include <power/tps65090.h>
15 static int tps65090_fet_probe(struct udevice *dev)
17 struct dm_regulator_uclass_platdata *uc_pdata;
19 uc_pdata = dev_get_uclass_platdata(dev);
21 uc_pdata->type = REGULATOR_TYPE_OTHER;
22 uc_pdata->mode_count = 0;
27 static int tps65090_fet_get_enable(struct udevice *dev)
29 struct udevice *pmic = dev_get_parent(dev);
32 fet_id = dev->driver_data;
33 debug("%s: fet_id=%d\n", __func__, fet_id);
35 ret = pmic_reg_read(pmic, REG_FET_BASE + fet_id);
39 return ret & FET_CTRL_ENFET;
43 * Set the power state for a FET
45 * @param pmic pmic structure for the tps65090
46 * @param fet_id FET number to set (1..MAX_FET_NUM)
47 * @param set 1 to power on FET, 0 to power off
48 * @return -EIO if we got a comms error, -EAGAIN if the FET failed to
49 * change state. If all is ok, returns 0.
51 static int tps65090_fet_set(struct udevice *pmic, int fet_id, bool set)
57 value = FET_CTRL_ADENFET | FET_CTRL_WAIT;
59 value |= FET_CTRL_ENFET;
61 if (pmic_reg_write(pmic, REG_FET_BASE + fet_id, value))
64 /* Try reading until we get a result */
65 for (retry = 0; retry < MAX_CTRL_READ_TRIES; retry++) {
66 ret = pmic_reg_read(pmic, REG_FET_BASE + fet_id);
70 /* Check that the FET went into the expected state */
71 debug("%s: flags=%x\n", __func__, ret);
72 if (!!(ret & FET_CTRL_PGFET) == set)
75 /* If we got a timeout, there is no point in waiting longer */
76 if (ret & FET_CTRL_TOFET)
82 debug("FET %d: Power good should have set to %d but reg=%#02x\n",
87 static int tps65090_fet_set_enable(struct udevice *dev, bool enable)
89 struct udevice *pmic = dev_get_parent(dev);
94 fet_id = dev->driver_data;
95 debug("%s: fet_id=%d, enable=%d\n", __func__, fet_id, enable);
98 for (loops = 0;; loops++) {
99 ret = tps65090_fet_set(pmic, fet_id, enable);
103 if (get_timer(start) > 100)
106 /* Turn it off and try again until we time out */
107 tps65090_fet_set(pmic, fet_id, false);
111 debug("%s: FET%d failed to power on: time=%lums, loops=%d\n",
112 __func__, fet_id, get_timer(start), loops);
114 debug("%s: FET%d powered on after %lums, loops=%d\n",
115 __func__, fet_id, get_timer(start), loops);
118 * Unfortunately there are some conditions where the power-good bit
119 * will be 0, but the FET still comes up. One such case occurs with
120 * the LCD backlight on snow. We'll just return 0 here and assume
121 * that the FET will eventually come up.
129 static const struct dm_regulator_ops tps65090_fet_ops = {
130 .get_enable = tps65090_fet_get_enable,
131 .set_enable = tps65090_fet_set_enable,
134 U_BOOT_DRIVER(tps65090_fet) = {
135 .name = TPS65090_FET_DRIVER,
136 .id = UCLASS_REGULATOR,
137 .ops = &tps65090_fet_ops,
138 .probe = tps65090_fet_probe,