1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (c) 2015 Google, Inc
4 * Written by Simon Glass <sjg@chromium.org>
16 * struct i2c_mux: Information the uclass stores about an I2C mux
18 * @selected: Currently selected mux, or -1 for none
19 * @i2c_bus: I2C bus to use for communcation
23 struct udevice *i2c_bus;
27 * struct i2c_mux_bus: Information about each bus the mux controls
29 * @channel: Channel number used to select this bus
35 /* Find out the mux channel number */
36 static int i2c_mux_child_post_bind(struct udevice *dev)
38 struct i2c_mux_bus *plat = dev_get_parent_platdata(dev);
41 channel = dev_read_u32_default(dev, "reg", -1);
44 plat->channel = channel;
49 /* Find the I2C buses selected by this mux */
50 static int i2c_mux_post_bind(struct udevice *mux)
55 debug("%s: %s\n", __func__, mux->name);
57 * There is no compatible string in the sub-nodes, so we must manually
60 dev_for_each_subnode(node, mux) {
63 const char *arrow = "->";
65 int parent_name_len, arrow_len, mux_name_len, name_len;
67 name = ofnode_get_name(node);
69 /* Calculate lenghts of strings */
70 parent_name_len = strlen(mux->parent->name);
71 arrow_len = strlen(arrow);
72 mux_name_len = strlen(mux->name);
73 name_len = strlen(name);
75 full_name = calloc(1, parent_name_len + arrow_len +
76 mux_name_len + arrow_len + name_len + 1);
80 /* Compose bus name */
81 strcat(full_name, mux->parent->name);
82 strcat(full_name, arrow);
83 strcat(full_name, mux->name);
84 strcat(full_name, arrow);
85 strcat(full_name, name);
87 ret = device_bind_driver_to_node(mux, "i2c_mux_bus_drv",
88 full_name, node, &dev);
89 debug(" - bind ret=%d, %s, req_seq %d\n", ret,
90 dev ? dev->name : NULL, dev->req_seq);
98 /* Set up the mux ready for use */
99 static int i2c_mux_post_probe(struct udevice *mux)
101 struct i2c_mux *priv = dev_get_uclass_priv(mux);
104 debug("%s: %s\n", __func__, mux->name);
107 /* if parent is of i2c uclass already, we'll take that, otherwise
108 * look if we find an i2c-parent phandle
110 if (UCLASS_I2C == device_get_uclass_id(mux->parent)) {
111 priv->i2c_bus = dev_get_parent(mux);
112 debug("%s: bus=%p/%s\n", __func__, priv->i2c_bus,
113 priv->i2c_bus->name);
117 ret = uclass_get_device_by_phandle(UCLASS_I2C, mux, "i2c-parent",
121 debug("%s: bus=%p/%s\n", __func__, priv->i2c_bus, priv->i2c_bus->name);
126 int i2c_mux_select(struct udevice *dev)
128 struct i2c_mux_bus *plat = dev_get_parent_platdata(dev);
129 struct udevice *mux = dev->parent;
130 struct i2c_mux_ops *ops = i2c_mux_get_ops(mux);
135 return ops->select(mux, dev, plat->channel);
138 int i2c_mux_deselect(struct udevice *dev)
140 struct i2c_mux_bus *plat = dev_get_parent_platdata(dev);
141 struct udevice *mux = dev->parent;
142 struct i2c_mux_ops *ops = i2c_mux_get_ops(mux);
147 return ops->deselect(mux, dev, plat->channel);
150 static int i2c_mux_bus_set_bus_speed(struct udevice *dev, unsigned int speed)
152 struct udevice *mux = dev->parent;
153 struct i2c_mux *priv = dev_get_uclass_priv(mux);
156 ret = i2c_mux_select(dev);
159 ret = dm_i2c_set_bus_speed(priv->i2c_bus, speed);
160 ret2 = i2c_mux_deselect(dev);
162 return ret ? ret : ret2;
165 static int i2c_mux_bus_probe(struct udevice *dev, uint chip_addr,
168 struct udevice *mux = dev->parent;
169 struct i2c_mux *priv = dev_get_uclass_priv(mux);
170 struct dm_i2c_ops *ops = i2c_get_ops(priv->i2c_bus);
173 debug("%s: %s, bus %s\n", __func__, dev->name, priv->i2c_bus->name);
174 if (!ops->probe_chip)
176 ret = i2c_mux_select(dev);
179 ret = ops->probe_chip(priv->i2c_bus, chip_addr, chip_flags);
180 ret2 = i2c_mux_deselect(dev);
182 return ret ? ret : ret2;
185 static int i2c_mux_bus_xfer(struct udevice *dev, struct i2c_msg *msg,
188 struct udevice *mux = dev->parent;
189 struct i2c_mux *priv = dev_get_uclass_priv(mux);
190 struct dm_i2c_ops *ops = i2c_get_ops(priv->i2c_bus);
193 debug("%s: %s, bus %s\n", __func__, dev->name, priv->i2c_bus->name);
196 ret = i2c_mux_select(dev);
199 ret = ops->xfer(priv->i2c_bus, msg, nmsgs);
200 ret2 = i2c_mux_deselect(dev);
202 return ret ? ret : ret2;
205 static const struct dm_i2c_ops i2c_mux_bus_ops = {
206 .xfer = i2c_mux_bus_xfer,
207 .probe_chip = i2c_mux_bus_probe,
208 .set_bus_speed = i2c_mux_bus_set_bus_speed,
211 U_BOOT_DRIVER(i2c_mux_bus) = {
212 .name = "i2c_mux_bus_drv",
214 .ops = &i2c_mux_bus_ops,
217 UCLASS_DRIVER(i2c_mux) = {
218 .id = UCLASS_I2C_MUX,
220 .post_bind = i2c_mux_post_bind,
221 .post_probe = i2c_mux_post_probe,
222 .per_device_auto_alloc_size = sizeof(struct i2c_mux),
223 .per_child_platdata_auto_alloc_size = sizeof(struct i2c_mux_bus),
224 .child_post_bind = i2c_mux_child_post_bind,