1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (c) 2015 Google, Inc
4 * Written by Simon Glass <sjg@chromium.org>
15 * struct i2c_mux: Information the uclass stores about an I2C mux
17 * @selected: Currently selected mux, or -1 for none
18 * @i2c_bus: I2C bus to use for communcation
22 struct udevice *i2c_bus;
26 * struct i2c_mux_bus: Information about each bus the mux controls
28 * @channel: Channel number used to select this bus
34 /* Find out the mux channel number */
35 static int i2c_mux_child_post_bind(struct udevice *dev)
37 struct i2c_mux_bus *plat = dev_get_parent_platdata(dev);
40 channel = dev_read_u32_default(dev, "reg", -1);
43 plat->channel = channel;
48 /* Find the I2C buses selected by this mux */
49 static int i2c_mux_post_bind(struct udevice *mux)
54 debug("%s: %s\n", __func__, mux->name);
56 * There is no compatible string in the sub-nodes, so we must manually
59 dev_for_each_subnode(node, mux) {
63 name = ofnode_get_name(node);
64 ret = device_bind_driver_to_node(mux, "i2c_mux_bus_drv", name,
66 debug(" - bind ret=%d, %s\n", ret, dev ? dev->name : NULL);
74 /* Set up the mux ready for use */
75 static int i2c_mux_post_probe(struct udevice *mux)
77 struct i2c_mux *priv = dev_get_uclass_priv(mux);
80 debug("%s: %s\n", __func__, mux->name);
83 /* if parent is of i2c uclass already, we'll take that, otherwise
84 * look if we find an i2c-parent phandle
86 if (UCLASS_I2C == device_get_uclass_id(mux->parent)) {
87 priv->i2c_bus = dev_get_parent(mux);
88 debug("%s: bus=%p/%s\n", __func__, priv->i2c_bus,
93 ret = uclass_get_device_by_phandle(UCLASS_I2C, mux, "i2c-parent",
97 debug("%s: bus=%p/%s\n", __func__, priv->i2c_bus, priv->i2c_bus->name);
102 int i2c_mux_select(struct udevice *dev)
104 struct i2c_mux_bus *plat = dev_get_parent_platdata(dev);
105 struct udevice *mux = dev->parent;
106 struct i2c_mux_ops *ops = i2c_mux_get_ops(mux);
111 return ops->select(mux, dev, plat->channel);
114 int i2c_mux_deselect(struct udevice *dev)
116 struct i2c_mux_bus *plat = dev_get_parent_platdata(dev);
117 struct udevice *mux = dev->parent;
118 struct i2c_mux_ops *ops = i2c_mux_get_ops(mux);
123 return ops->deselect(mux, dev, plat->channel);
126 static int i2c_mux_bus_set_bus_speed(struct udevice *dev, unsigned int speed)
128 struct udevice *mux = dev->parent;
129 struct i2c_mux *priv = dev_get_uclass_priv(mux);
132 ret = i2c_mux_select(dev);
135 ret = dm_i2c_set_bus_speed(priv->i2c_bus, speed);
136 ret2 = i2c_mux_deselect(dev);
138 return ret ? ret : ret2;
141 static int i2c_mux_bus_probe(struct udevice *dev, uint chip_addr,
144 struct udevice *mux = dev->parent;
145 struct i2c_mux *priv = dev_get_uclass_priv(mux);
146 struct dm_i2c_ops *ops = i2c_get_ops(priv->i2c_bus);
149 debug("%s: %s, bus %s\n", __func__, dev->name, priv->i2c_bus->name);
150 if (!ops->probe_chip)
152 ret = i2c_mux_select(dev);
155 ret = ops->probe_chip(priv->i2c_bus, chip_addr, chip_flags);
156 ret2 = i2c_mux_deselect(dev);
158 return ret ? ret : ret2;
161 static int i2c_mux_bus_xfer(struct udevice *dev, struct i2c_msg *msg,
164 struct udevice *mux = dev->parent;
165 struct i2c_mux *priv = dev_get_uclass_priv(mux);
166 struct dm_i2c_ops *ops = i2c_get_ops(priv->i2c_bus);
169 debug("%s: %s, bus %s\n", __func__, dev->name, priv->i2c_bus->name);
172 ret = i2c_mux_select(dev);
175 ret = ops->xfer(priv->i2c_bus, msg, nmsgs);
176 ret2 = i2c_mux_deselect(dev);
178 return ret ? ret : ret2;
181 static const struct dm_i2c_ops i2c_mux_bus_ops = {
182 .xfer = i2c_mux_bus_xfer,
183 .probe_chip = i2c_mux_bus_probe,
184 .set_bus_speed = i2c_mux_bus_set_bus_speed,
187 U_BOOT_DRIVER(i2c_mux_bus) = {
188 .name = "i2c_mux_bus_drv",
190 .ops = &i2c_mux_bus_ops,
193 UCLASS_DRIVER(i2c_mux) = {
194 .id = UCLASS_I2C_MUX,
196 .post_bind = i2c_mux_post_bind,
197 .post_probe = i2c_mux_post_probe,
198 .per_device_auto_alloc_size = sizeof(struct i2c_mux),
199 .per_child_platdata_auto_alloc_size = sizeof(struct i2c_mux_bus),
200 .child_post_bind = i2c_mux_child_post_bind,