1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (c) 2015 Google, Inc
4 * Written by Simon Glass <sjg@chromium.org>
7 #define LOG_CATEGORY UCLASS_I2C_MUX
19 * struct i2c_mux: Information the uclass stores about an I2C mux
21 * @selected: Currently selected mux, or -1 for none
22 * @i2c_bus: I2C bus to use for communcation
26 struct udevice *i2c_bus;
30 * struct i2c_mux_bus: Information about each bus the mux controls
32 * @channel: Channel number used to select this bus
38 /* Find out the mux channel number */
39 static int i2c_mux_child_post_bind(struct udevice *dev)
41 struct i2c_mux_bus *plat = dev_get_parent_plat(dev);
44 channel = dev_read_u32_default(dev, "reg", -1);
47 plat->channel = channel;
52 /* Find the I2C buses selected by this mux */
53 static int i2c_mux_post_bind(struct udevice *mux)
58 debug("%s: %s\n", __func__, mux->name);
60 * There is no compatible string in the sub-nodes, so we must manually
63 dev_for_each_subnode(node, mux) {
66 const char *arrow = "->";
68 int parent_name_len, arrow_len, mux_name_len, name_len;
70 name = ofnode_get_name(node);
72 /* Calculate lenghts of strings */
73 parent_name_len = strlen(mux->parent->name);
74 arrow_len = strlen(arrow);
75 mux_name_len = strlen(mux->name);
76 name_len = strlen(name);
78 full_name = calloc(1, parent_name_len + arrow_len +
79 mux_name_len + arrow_len + name_len + 1);
83 /* Compose bus name */
84 strcat(full_name, mux->parent->name);
85 strcat(full_name, arrow);
86 strcat(full_name, mux->name);
87 strcat(full_name, arrow);
88 strcat(full_name, name);
90 ret = device_bind_driver_to_node(mux, "i2c_mux_bus_drv",
91 full_name, node, &dev);
92 debug(" - bind ret=%d, %s, seq %d\n", ret,
93 dev ? dev->name : NULL, dev_seq(dev));
101 /* Set up the mux ready for use */
102 static int i2c_mux_post_probe(struct udevice *mux)
104 struct i2c_mux *priv = dev_get_uclass_priv(mux);
107 debug("%s: %s\n", __func__, mux->name);
110 /* if parent is of i2c uclass already, we'll take that, otherwise
111 * look if we find an i2c-parent phandle
113 if (UCLASS_I2C == device_get_uclass_id(mux->parent)) {
114 priv->i2c_bus = dev_get_parent(mux);
115 debug("%s: bus=%p/%s\n", __func__, priv->i2c_bus,
116 priv->i2c_bus->name);
120 ret = uclass_get_device_by_phandle(UCLASS_I2C, mux, "i2c-parent",
124 debug("%s: bus=%p/%s\n", __func__, priv->i2c_bus, priv->i2c_bus->name);
129 int i2c_mux_select(struct udevice *dev)
131 struct i2c_mux_bus *plat = dev_get_parent_plat(dev);
132 struct udevice *mux = dev->parent;
133 struct i2c_mux_ops *ops = i2c_mux_get_ops(mux);
138 return ops->select(mux, dev, plat->channel);
141 int i2c_mux_deselect(struct udevice *dev)
143 struct i2c_mux_bus *plat = dev_get_parent_plat(dev);
144 struct udevice *mux = dev->parent;
145 struct i2c_mux_ops *ops = i2c_mux_get_ops(mux);
150 return ops->deselect(mux, dev, plat->channel);
153 static int i2c_mux_bus_set_bus_speed(struct udevice *dev, unsigned int speed)
155 struct udevice *mux = dev->parent;
156 struct i2c_mux *priv = dev_get_uclass_priv(mux);
159 ret = i2c_mux_select(dev);
162 ret = dm_i2c_set_bus_speed(priv->i2c_bus, speed);
163 ret2 = i2c_mux_deselect(dev);
165 return ret ? ret : ret2;
168 static int i2c_mux_bus_probe(struct udevice *dev, uint chip_addr,
171 struct udevice *mux = dev->parent;
172 struct i2c_mux *priv = dev_get_uclass_priv(mux);
173 struct dm_i2c_ops *ops = i2c_get_ops(priv->i2c_bus);
176 debug("%s: %s, bus %s\n", __func__, dev->name, priv->i2c_bus->name);
177 if (!ops->probe_chip)
179 ret = i2c_mux_select(dev);
182 ret = ops->probe_chip(priv->i2c_bus, chip_addr, chip_flags);
183 ret2 = i2c_mux_deselect(dev);
185 return ret ? ret : ret2;
188 static int i2c_mux_bus_xfer(struct udevice *dev, struct i2c_msg *msg,
191 struct udevice *mux = dev->parent;
192 struct i2c_mux *priv = dev_get_uclass_priv(mux);
193 struct dm_i2c_ops *ops = i2c_get_ops(priv->i2c_bus);
196 debug("%s: %s, bus %s\n", __func__, dev->name, priv->i2c_bus->name);
199 ret = i2c_mux_select(dev);
202 ret = ops->xfer(priv->i2c_bus, msg, nmsgs);
203 ret2 = i2c_mux_deselect(dev);
205 return ret ? ret : ret2;
208 static const struct dm_i2c_ops i2c_mux_bus_ops = {
209 .xfer = i2c_mux_bus_xfer,
210 .probe_chip = i2c_mux_bus_probe,
211 .set_bus_speed = i2c_mux_bus_set_bus_speed,
214 U_BOOT_DRIVER(i2c_mux_bus) = {
215 .name = "i2c_mux_bus_drv",
217 .ops = &i2c_mux_bus_ops,
220 UCLASS_DRIVER(i2c_mux) = {
221 .id = UCLASS_I2C_MUX,
223 .post_bind = i2c_mux_post_bind,
224 .post_probe = i2c_mux_post_probe,
225 .per_device_auto = sizeof(struct i2c_mux),
226 .per_child_plat_auto = sizeof(struct i2c_mux_bus),
227 .child_post_bind = i2c_mux_child_post_bind,