xref: /openbmc/u-boot/drivers/i2c/cros_ec_ldo.c (revision 872cfa20)
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright (c) 2015 Google, Inc
4  * Written by Simon Glass <sjg@chromium.org>
5  */
6 
7 #include <common.h>
8 #include <dm.h>
9 #include <cros_ec.h>
10 #include <errno.h>
11 #include <i2c.h>
12 #include <power/tps65090.h>
13 
14 static int cros_ec_ldo_set_bus_speed(struct udevice *dev, unsigned int speed)
15 {
16 	return 0;
17 }
18 
19 static int cros_ec_ldo_xfer(struct udevice *dev, struct i2c_msg *msg,
20 			    int nmsgs)
21 {
22 	bool is_read = nmsgs > 1;
23 	int fet_id, ret;
24 
25 	/*
26 	 * Look for reads and writes of the LDO registers. In either case the
27 	 * first message is a write with the register number as the first byte.
28 	 */
29 	if (!nmsgs || !msg->len || (msg->flags & I2C_M_RD)) {
30 		debug("%s: Invalid message\n", __func__);
31 		goto err;
32 	}
33 
34 	fet_id = msg->buf[0] - REG_FET_BASE;
35 	if (fet_id < 1 || fet_id > MAX_FET_NUM) {
36 		debug("%s: Invalid FET %d\n", __func__, fet_id);
37 		goto err;
38 	}
39 
40 	if (is_read) {
41 		uint8_t state;
42 
43 		ret = cros_ec_get_ldo(dev->parent, fet_id, &state);
44 		if (!ret)
45 			msg[1].buf[0] = state ?
46 				FET_CTRL_ENFET | FET_CTRL_PGFET : 0;
47 	} else {
48 		bool on = msg->buf[1] & FET_CTRL_ENFET;
49 
50 		ret = cros_ec_set_ldo(dev->parent, fet_id, on);
51 	}
52 
53 	return ret;
54 
55 err:
56 	/* Indicate that the message is unimplemented */
57 	return -ENOSYS;
58 }
59 
60 static const struct dm_i2c_ops cros_ec_i2c_ops = {
61 	.xfer		= cros_ec_ldo_xfer,
62 	.set_bus_speed	= cros_ec_ldo_set_bus_speed,
63 };
64 
65 static const struct udevice_id cros_ec_i2c_ids[] = {
66 	{ .compatible = "google,cros-ec-ldo-tunnel" },
67 	{ }
68 };
69 
70 U_BOOT_DRIVER(cros_ec_ldo) = {
71 	.name	= "cros_ec_ldo_tunnel",
72 	.id	= UCLASS_I2C,
73 	.of_match = cros_ec_i2c_ids,
74 	.ops	= &cros_ec_i2c_ops,
75 };
76