1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (c) 2015 Google, Inc
4 * Written by Simon Glass <sjg@chromium.org>
17 * struct i2c_mux: Information the uclass stores about an I2C mux
19 * @selected: Currently selected mux, or -1 for none
20 * @i2c_bus: I2C bus to use for communcation
24 struct udevice *i2c_bus;
28 * struct i2c_mux_bus: Information about each bus the mux controls
30 * @channel: Channel number used to select this bus
36 /* Find out the mux channel number */
37 static int i2c_mux_child_post_bind(struct udevice *dev)
39 struct i2c_mux_bus *plat = dev_get_parent_platdata(dev);
42 channel = dev_read_u32_default(dev, "reg", -1);
45 plat->channel = channel;
50 /* Find the I2C buses selected by this mux */
51 static int i2c_mux_post_bind(struct udevice *mux)
56 debug("%s: %s\n", __func__, mux->name);
58 * There is no compatible string in the sub-nodes, so we must manually
61 dev_for_each_subnode(node, mux) {
64 const char *arrow = "->";
66 int parent_name_len, arrow_len, mux_name_len, name_len;
68 name = ofnode_get_name(node);
70 /* Calculate lenghts of strings */
71 parent_name_len = strlen(mux->parent->name);
72 arrow_len = strlen(arrow);
73 mux_name_len = strlen(mux->name);
74 name_len = strlen(name);
76 full_name = calloc(1, parent_name_len + arrow_len +
77 mux_name_len + arrow_len + name_len + 1);
81 /* Compose bus name */
82 strcat(full_name, mux->parent->name);
83 strcat(full_name, arrow);
84 strcat(full_name, mux->name);
85 strcat(full_name, arrow);
86 strcat(full_name, name);
88 ret = device_bind_driver_to_node(mux, "i2c_mux_bus_drv",
89 full_name, node, &dev);
90 debug(" - bind ret=%d, %s, req_seq %d\n", ret,
91 dev ? dev->name : NULL, dev->req_seq);
99 /* Set up the mux ready for use */
100 static int i2c_mux_post_probe(struct udevice *mux)
102 struct i2c_mux *priv = dev_get_uclass_priv(mux);
105 debug("%s: %s\n", __func__, mux->name);
108 /* if parent is of i2c uclass already, we'll take that, otherwise
109 * look if we find an i2c-parent phandle
111 if (UCLASS_I2C == device_get_uclass_id(mux->parent)) {
112 priv->i2c_bus = dev_get_parent(mux);
113 debug("%s: bus=%p/%s\n", __func__, priv->i2c_bus,
114 priv->i2c_bus->name);
118 ret = uclass_get_device_by_phandle(UCLASS_I2C, mux, "i2c-parent",
122 debug("%s: bus=%p/%s\n", __func__, priv->i2c_bus, priv->i2c_bus->name);
127 int i2c_mux_select(struct udevice *dev)
129 struct i2c_mux_bus *plat = dev_get_parent_platdata(dev);
130 struct udevice *mux = dev->parent;
131 struct i2c_mux_ops *ops = i2c_mux_get_ops(mux);
136 return ops->select(mux, dev, plat->channel);
139 int i2c_mux_deselect(struct udevice *dev)
141 struct i2c_mux_bus *plat = dev_get_parent_platdata(dev);
142 struct udevice *mux = dev->parent;
143 struct i2c_mux_ops *ops = i2c_mux_get_ops(mux);
148 return ops->deselect(mux, dev, plat->channel);
151 static int i2c_mux_bus_set_bus_speed(struct udevice *dev, unsigned int speed)
153 struct udevice *mux = dev->parent;
154 struct i2c_mux *priv = dev_get_uclass_priv(mux);
157 ret = i2c_mux_select(dev);
160 ret = dm_i2c_set_bus_speed(priv->i2c_bus, speed);
161 ret2 = i2c_mux_deselect(dev);
163 return ret ? ret : ret2;
166 static int i2c_mux_bus_probe(struct udevice *dev, uint chip_addr,
169 struct udevice *mux = dev->parent;
170 struct i2c_mux *priv = dev_get_uclass_priv(mux);
171 struct dm_i2c_ops *ops = i2c_get_ops(priv->i2c_bus);
174 debug("%s: %s, bus %s\n", __func__, dev->name, priv->i2c_bus->name);
175 if (!ops->probe_chip)
177 ret = i2c_mux_select(dev);
180 ret = ops->probe_chip(priv->i2c_bus, chip_addr, chip_flags);
181 ret2 = i2c_mux_deselect(dev);
183 return ret ? ret : ret2;
186 static int i2c_mux_bus_xfer(struct udevice *dev, struct i2c_msg *msg,
189 struct udevice *mux = dev->parent;
190 struct i2c_mux *priv = dev_get_uclass_priv(mux);
191 struct dm_i2c_ops *ops = i2c_get_ops(priv->i2c_bus);
194 debug("%s: %s, bus %s\n", __func__, dev->name, priv->i2c_bus->name);
197 ret = i2c_mux_select(dev);
200 ret = ops->xfer(priv->i2c_bus, msg, nmsgs);
201 ret2 = i2c_mux_deselect(dev);
203 return ret ? ret : ret2;
206 static const struct dm_i2c_ops i2c_mux_bus_ops = {
207 .xfer = i2c_mux_bus_xfer,
208 .probe_chip = i2c_mux_bus_probe,
209 .set_bus_speed = i2c_mux_bus_set_bus_speed,
212 U_BOOT_DRIVER(i2c_mux_bus) = {
213 .name = "i2c_mux_bus_drv",
215 .ops = &i2c_mux_bus_ops,
218 UCLASS_DRIVER(i2c_mux) = {
219 .id = UCLASS_I2C_MUX,
221 .post_bind = i2c_mux_post_bind,
222 .post_probe = i2c_mux_post_probe,
223 .per_device_auto_alloc_size = sizeof(struct i2c_mux),
224 .per_child_platdata_auto_alloc_size = sizeof(struct i2c_mux_bus),
225 .child_post_bind = i2c_mux_child_post_bind,