2 * Copyright (c) 2015 Google, Inc
3 * Written by Simon Glass <sjg@chromium.org>
5 * SPDX-License-Identifier: GPL-2.0+
15 DECLARE_GLOBAL_DATA_PTR;
18 * struct i2c_mux: Information the uclass stores about an I2C mux
20 * @selected: Currently selected mux, or -1 for none
21 * @i2c_bus: I2C bus to use for communcation
25 struct udevice *i2c_bus;
29 * struct i2c_mux_bus: Information about each bus the mux controls
31 * @channel: Channel number used to select this bus
37 /* Find out the mux channel number */
38 static int i2c_mux_child_post_bind(struct udevice *dev)
40 struct i2c_mux_bus *plat = dev_get_parent_platdata(dev);
43 channel = fdtdec_get_int(gd->fdt_blob, dev->of_offset, "reg", -1);
46 plat->channel = channel;
51 /* Find the I2C buses selected by this mux */
52 static int i2c_mux_post_bind(struct udevice *mux)
54 const void *blob = gd->fdt_blob;
58 debug("%s: %s\n", __func__, mux->name);
60 * There is no compatible string in the sub-nodes, so we must manually
63 for (offset = fdt_first_subnode(blob, mux->of_offset);
65 offset = fdt_next_subnode(blob, offset)) {
69 name = fdt_get_name(blob, offset, NULL);
70 ret = device_bind_driver_to_node(mux, "i2c_mux_bus_drv", name,
72 debug(" - bind ret=%d, %s\n", ret, dev ? dev->name : NULL);
80 /* Set up the mux ready for use */
81 static int i2c_mux_post_probe(struct udevice *mux)
83 struct i2c_mux *priv = dev_get_uclass_priv(mux);
86 debug("%s: %s\n", __func__, mux->name);
89 ret = uclass_get_device_by_phandle(UCLASS_I2C, mux, "i2c-parent",
93 debug("%s: bus=%p/%s\n", __func__, priv->i2c_bus, priv->i2c_bus->name);
98 int i2c_mux_select(struct udevice *dev)
100 struct i2c_mux_bus *plat = dev_get_parent_platdata(dev);
101 struct udevice *mux = dev->parent;
102 struct i2c_mux_ops *ops = i2c_mux_get_ops(mux);
107 return ops->select(mux, dev, plat->channel);
110 int i2c_mux_deselect(struct udevice *dev)
112 struct i2c_mux_bus *plat = dev_get_parent_platdata(dev);
113 struct udevice *mux = dev->parent;
114 struct i2c_mux_ops *ops = i2c_mux_get_ops(mux);
119 return ops->deselect(mux, dev, plat->channel);
122 static int i2c_mux_bus_set_bus_speed(struct udevice *dev, unsigned int speed)
124 struct udevice *mux = dev->parent;
125 struct i2c_mux *priv = dev_get_uclass_priv(mux);
128 ret = i2c_mux_select(dev);
131 ret = dm_i2c_set_bus_speed(priv->i2c_bus, speed);
132 ret2 = i2c_mux_deselect(dev);
134 return ret ? ret : ret2;
137 static int i2c_mux_bus_probe(struct udevice *dev, uint chip_addr,
140 struct udevice *mux = dev->parent;
141 struct i2c_mux *priv = dev_get_uclass_priv(mux);
142 struct dm_i2c_ops *ops = i2c_get_ops(priv->i2c_bus);
145 debug("%s: %s, bus %s\n", __func__, dev->name, priv->i2c_bus->name);
146 if (!ops->probe_chip)
148 ret = i2c_mux_select(dev);
151 ret = ops->probe_chip(priv->i2c_bus, chip_addr, chip_flags);
152 ret2 = i2c_mux_deselect(dev);
154 return ret ? ret : ret2;
157 static int i2c_mux_bus_xfer(struct udevice *dev, struct i2c_msg *msg,
160 struct udevice *mux = dev->parent;
161 struct i2c_mux *priv = dev_get_uclass_priv(mux);
162 struct dm_i2c_ops *ops = i2c_get_ops(priv->i2c_bus);
165 debug("%s: %s, bus %s\n", __func__, dev->name, priv->i2c_bus->name);
168 ret = i2c_mux_select(dev);
171 ret = ops->xfer(priv->i2c_bus, msg, nmsgs);
172 ret2 = i2c_mux_deselect(dev);
174 return ret ? ret : ret2;
177 static const struct dm_i2c_ops i2c_mux_bus_ops = {
178 .xfer = i2c_mux_bus_xfer,
179 .probe_chip = i2c_mux_bus_probe,
180 .set_bus_speed = i2c_mux_bus_set_bus_speed,
183 U_BOOT_DRIVER(i2c_mux_bus) = {
184 .name = "i2c_mux_bus_drv",
186 .per_child_auto_alloc_size = sizeof(struct dm_i2c_chip),
187 .ops = &i2c_mux_bus_ops,
190 UCLASS_DRIVER(i2c_mux) = {
191 .id = UCLASS_I2C_MUX,
193 .post_bind = i2c_mux_post_bind,
194 .post_probe = i2c_mux_post_probe,
195 .per_device_auto_alloc_size = sizeof(struct i2c_mux),
196 .per_child_platdata_auto_alloc_size = sizeof(struct i2c_mux_bus),
197 .child_post_bind = i2c_mux_child_post_bind,