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)
57 debug("%s: %s\n", __func__, mux->name);
59 * There is no compatible string in the sub-nodes, so we must manually
62 dev_for_each_subnode(node, mux) {
66 name = ofnode_get_name(node);
67 ret = device_bind_driver_to_node(mux, "i2c_mux_bus_drv", name,
69 debug(" - bind ret=%d, %s\n", ret, dev ? dev->name : NULL);
77 /* Set up the mux ready for use */
78 static int i2c_mux_post_probe(struct udevice *mux)
80 struct i2c_mux *priv = dev_get_uclass_priv(mux);
83 debug("%s: %s\n", __func__, mux->name);
86 /* if parent is of i2c uclass already, we'll take that, otherwise
87 * look if we find an i2c-parent phandle
89 if (UCLASS_I2C == device_get_uclass_id(mux->parent)) {
90 priv->i2c_bus = dev_get_parent(mux);
91 debug("%s: bus=%p/%s\n", __func__, priv->i2c_bus,
96 ret = uclass_get_device_by_phandle(UCLASS_I2C, mux, "i2c-parent",
100 debug("%s: bus=%p/%s\n", __func__, priv->i2c_bus, priv->i2c_bus->name);
105 int i2c_mux_select(struct udevice *dev)
107 struct i2c_mux_bus *plat = dev_get_parent_platdata(dev);
108 struct udevice *mux = dev->parent;
109 struct i2c_mux_ops *ops = i2c_mux_get_ops(mux);
114 return ops->select(mux, dev, plat->channel);
117 int i2c_mux_deselect(struct udevice *dev)
119 struct i2c_mux_bus *plat = dev_get_parent_platdata(dev);
120 struct udevice *mux = dev->parent;
121 struct i2c_mux_ops *ops = i2c_mux_get_ops(mux);
126 return ops->deselect(mux, dev, plat->channel);
129 static int i2c_mux_bus_set_bus_speed(struct udevice *dev, unsigned int speed)
131 struct udevice *mux = dev->parent;
132 struct i2c_mux *priv = dev_get_uclass_priv(mux);
135 ret = i2c_mux_select(dev);
138 ret = dm_i2c_set_bus_speed(priv->i2c_bus, speed);
139 ret2 = i2c_mux_deselect(dev);
141 return ret ? ret : ret2;
144 static int i2c_mux_bus_probe(struct udevice *dev, uint chip_addr,
147 struct udevice *mux = dev->parent;
148 struct i2c_mux *priv = dev_get_uclass_priv(mux);
149 struct dm_i2c_ops *ops = i2c_get_ops(priv->i2c_bus);
152 debug("%s: %s, bus %s\n", __func__, dev->name, priv->i2c_bus->name);
153 if (!ops->probe_chip)
155 ret = i2c_mux_select(dev);
158 ret = ops->probe_chip(priv->i2c_bus, chip_addr, chip_flags);
159 ret2 = i2c_mux_deselect(dev);
161 return ret ? ret : ret2;
164 static int i2c_mux_bus_xfer(struct udevice *dev, struct i2c_msg *msg,
167 struct udevice *mux = dev->parent;
168 struct i2c_mux *priv = dev_get_uclass_priv(mux);
169 struct dm_i2c_ops *ops = i2c_get_ops(priv->i2c_bus);
172 debug("%s: %s, bus %s\n", __func__, dev->name, priv->i2c_bus->name);
175 ret = i2c_mux_select(dev);
178 ret = ops->xfer(priv->i2c_bus, msg, nmsgs);
179 ret2 = i2c_mux_deselect(dev);
181 return ret ? ret : ret2;
184 static const struct dm_i2c_ops i2c_mux_bus_ops = {
185 .xfer = i2c_mux_bus_xfer,
186 .probe_chip = i2c_mux_bus_probe,
187 .set_bus_speed = i2c_mux_bus_set_bus_speed,
190 U_BOOT_DRIVER(i2c_mux_bus) = {
191 .name = "i2c_mux_bus_drv",
193 .ops = &i2c_mux_bus_ops,
196 UCLASS_DRIVER(i2c_mux) = {
197 .id = UCLASS_I2C_MUX,
199 .post_bind = i2c_mux_post_bind,
200 .post_probe = i2c_mux_post_probe,
201 .per_device_auto_alloc_size = sizeof(struct i2c_mux),
202 .per_child_platdata_auto_alloc_size = sizeof(struct i2c_mux_bus),
203 .child_post_bind = i2c_mux_child_post_bind,