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(dev), "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, dev_of_offset(mux));
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 /* if parent is of i2c uclass already, we'll take that, otherwise
90 * look if we find an i2c-parent phandle
92 if (UCLASS_I2C == device_get_uclass_id(mux->parent)) {
93 priv->i2c_bus = dev_get_parent(mux);
94 debug("%s: bus=%p/%s\n", __func__, priv->i2c_bus,
99 ret = uclass_get_device_by_phandle(UCLASS_I2C, mux, "i2c-parent",
103 debug("%s: bus=%p/%s\n", __func__, priv->i2c_bus, priv->i2c_bus->name);
108 int i2c_mux_select(struct udevice *dev)
110 struct i2c_mux_bus *plat = dev_get_parent_platdata(dev);
111 struct udevice *mux = dev->parent;
112 struct i2c_mux_ops *ops = i2c_mux_get_ops(mux);
117 return ops->select(mux, dev, plat->channel);
120 int i2c_mux_deselect(struct udevice *dev)
122 struct i2c_mux_bus *plat = dev_get_parent_platdata(dev);
123 struct udevice *mux = dev->parent;
124 struct i2c_mux_ops *ops = i2c_mux_get_ops(mux);
129 return ops->deselect(mux, dev, plat->channel);
132 static int i2c_mux_bus_set_bus_speed(struct udevice *dev, unsigned int speed)
134 struct udevice *mux = dev->parent;
135 struct i2c_mux *priv = dev_get_uclass_priv(mux);
138 ret = i2c_mux_select(dev);
141 ret = dm_i2c_set_bus_speed(priv->i2c_bus, speed);
142 ret2 = i2c_mux_deselect(dev);
144 return ret ? ret : ret2;
147 static int i2c_mux_bus_probe(struct udevice *dev, uint chip_addr,
150 struct udevice *mux = dev->parent;
151 struct i2c_mux *priv = dev_get_uclass_priv(mux);
152 struct dm_i2c_ops *ops = i2c_get_ops(priv->i2c_bus);
155 debug("%s: %s, bus %s\n", __func__, dev->name, priv->i2c_bus->name);
156 if (!ops->probe_chip)
158 ret = i2c_mux_select(dev);
161 ret = ops->probe_chip(priv->i2c_bus, chip_addr, chip_flags);
162 ret2 = i2c_mux_deselect(dev);
164 return ret ? ret : ret2;
167 static int i2c_mux_bus_xfer(struct udevice *dev, struct i2c_msg *msg,
170 struct udevice *mux = dev->parent;
171 struct i2c_mux *priv = dev_get_uclass_priv(mux);
172 struct dm_i2c_ops *ops = i2c_get_ops(priv->i2c_bus);
175 debug("%s: %s, bus %s\n", __func__, dev->name, priv->i2c_bus->name);
178 ret = i2c_mux_select(dev);
181 ret = ops->xfer(priv->i2c_bus, msg, nmsgs);
182 ret2 = i2c_mux_deselect(dev);
184 return ret ? ret : ret2;
187 static const struct dm_i2c_ops i2c_mux_bus_ops = {
188 .xfer = i2c_mux_bus_xfer,
189 .probe_chip = i2c_mux_bus_probe,
190 .set_bus_speed = i2c_mux_bus_set_bus_speed,
193 U_BOOT_DRIVER(i2c_mux_bus) = {
194 .name = "i2c_mux_bus_drv",
196 .ops = &i2c_mux_bus_ops,
199 UCLASS_DRIVER(i2c_mux) = {
200 .id = UCLASS_I2C_MUX,
202 .post_bind = i2c_mux_post_bind,
203 .post_probe = i2c_mux_post_probe,
204 .per_device_auto_alloc_size = sizeof(struct i2c_mux),
205 .per_child_platdata_auto_alloc_size = sizeof(struct i2c_mux_bus),
206 .child_post_bind = i2c_mux_child_post_bind,