2 * Copyright (c) 2013 The Chromium OS Authors.
4 * SPDX-License-Identifier: GPL-2.0+
10 #include <power/tps65090_pmic.h>
12 DECLARE_GLOBAL_DATA_PTR;
14 #define TPS65090_ADDR 0x48
16 static struct tps65090 {
17 struct cros_ec_dev *dev; /* The CROS_EC device */
20 /* TPS65090 register addresses */
37 CG_CTRL0_ENC_MASK = 0x01,
40 MAX_CTRL_READ_TRIES = 5,
42 /* TPS65090 FET_CTRL register values */
43 FET_CTRL_TOFET = 1 << 7, /* Timeout, startup, overload */
44 FET_CTRL_PGFET = 1 << 4, /* Power good for FET status */
45 FET_CTRL_WAIT = 3 << 2, /* Overcurrent timeout max */
46 FET_CTRL_ADENFET = 1 << 1, /* Enable output auto discharge */
47 FET_CTRL_ENFET = 1 << 0, /* Enable FET */
51 * tps65090_read - read a byte from tps6090
53 * @param reg The register address to read from.
54 * @param val We'll return value value read here.
55 * @return 0 if ok; error if EC returns failure.
57 static int tps65090_read(u32 reg, u8 *val)
59 return cros_ec_i2c_xfer(config.dev, TPS65090_ADDR, reg, 1,
64 * tps65090_write - write a byte to tps6090
66 * @param reg The register address to write to.
67 * @param val The value to write.
68 * @return 0 if ok; error if EC returns failure.
70 static int tps65090_write(u32 reg, u8 val)
72 return cros_ec_i2c_xfer(config.dev, TPS65090_ADDR, reg, 1,
77 * Checks for a valid FET number
79 * @param fet_id FET number to check
80 * @return 0 if ok, -EINVAL if FET value is out of range
82 static int tps65090_check_fet(unsigned int fet_id)
84 if (fet_id == 0 || fet_id > MAX_FET_NUM) {
85 debug("parameter fet_id is out of range, %u not in 1 ~ %u\n",
94 * Set the power state for a FET
96 * @param fet_id Fet number to set (1..MAX_FET_NUM)
97 * @param set 1 to power on FET, 0 to power off
98 * @return -EIO if we got a comms error, -EAGAIN if the FET failed to
99 * change state. If all is ok, returns 0.
101 static int tps65090_fet_set(int fet_id, bool set)
106 value = FET_CTRL_ADENFET | FET_CTRL_WAIT;
108 value |= FET_CTRL_ENFET;
110 if (tps65090_write(REG_FET1_CTRL + fet_id - 1, value))
113 /* Try reading until we get a result */
114 for (retry = 0; retry < MAX_CTRL_READ_TRIES; retry++) {
115 if (tps65090_read(REG_FET1_CTRL + fet_id - 1, ®))
118 /* Check that the fet went into the expected state */
119 if (!!(reg & FET_CTRL_PGFET) == set)
122 /* If we got a timeout, there is no point in waiting longer */
123 if (reg & FET_CTRL_TOFET)
129 debug("FET %d: Power good should have set to %d but reg=%#02x\n",
134 int tps65090_fet_enable(unsigned int fet_id)
140 ret = tps65090_check_fet(fet_id);
144 start = get_timer(0);
145 for (loops = 0;; loops++) {
146 ret = tps65090_fet_set(fet_id, true);
150 if (get_timer(start) > 100)
153 /* Turn it off and try again until we time out */
154 tps65090_fet_set(fet_id, false);
158 debug("%s: FET%d failed to power on: time=%lums, loops=%d\n",
159 __func__, fet_id, get_timer(start), loops);
161 debug("%s: FET%d powered on after %lums, loops=%d\n",
162 __func__, fet_id, get_timer(start), loops);
165 * Unfortunately, there are some conditions where the power
166 * good bit will be 0, but the fet still comes up. One such
167 * case occurs with the lcd backlight. We'll just return 0 here
168 * and assume that the fet will eventually come up.
176 int tps65090_fet_disable(unsigned int fet_id)
180 ret = tps65090_check_fet(fet_id);
184 ret = tps65090_fet_set(fet_id, false);
189 int tps65090_fet_is_enabled(unsigned int fet_id)
194 ret = tps65090_check_fet(fet_id);
197 ret = tps65090_read(REG_FET1_CTRL + fet_id - 1, ®);
199 debug("fail to read FET%u_CTRL register over I2C", fet_id);
203 return reg & FET_CTRL_ENFET;
206 int tps65090_init(void)
208 puts("TPS65090 PMIC EC init\n");
210 config.dev = board_get_cros_ec_dev();
212 debug("%s: no cros_ec device: cannot init tps65090\n",