1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (c) 2015 Google, Inc
4 * Written by Simon Glass <sjg@chromium.org>
14 DECLARE_GLOBAL_DATA_PTR
;
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
= fdtdec_get_int(gd
->fdt_blob
, dev_of_offset(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
) {
65 name
= ofnode_get_name(node
);
66 ret
= device_bind_driver_to_node(mux
, "i2c_mux_bus_drv", name
,
68 debug(" - bind ret=%d, %s\n", ret
, dev
? dev
->name
: NULL
);
76 /* Set up the mux ready for use */
77 static int i2c_mux_post_probe(struct udevice
*mux
)
79 struct i2c_mux
*priv
= dev_get_uclass_priv(mux
);
82 debug("%s: %s\n", __func__
, mux
->name
);
85 /* if parent is of i2c uclass already, we'll take that, otherwise
86 * look if we find an i2c-parent phandle
88 if (UCLASS_I2C
== device_get_uclass_id(mux
->parent
)) {
89 priv
->i2c_bus
= dev_get_parent(mux
);
90 debug("%s: bus=%p/%s\n", __func__
, priv
->i2c_bus
,
95 ret
= uclass_get_device_by_phandle(UCLASS_I2C
, mux
, "i2c-parent",
99 debug("%s: bus=%p/%s\n", __func__
, priv
->i2c_bus
, priv
->i2c_bus
->name
);
104 int i2c_mux_select(struct udevice
*dev
)
106 struct i2c_mux_bus
*plat
= dev_get_parent_platdata(dev
);
107 struct udevice
*mux
= dev
->parent
;
108 struct i2c_mux_ops
*ops
= i2c_mux_get_ops(mux
);
113 return ops
->select(mux
, dev
, plat
->channel
);
116 int i2c_mux_deselect(struct udevice
*dev
)
118 struct i2c_mux_bus
*plat
= dev_get_parent_platdata(dev
);
119 struct udevice
*mux
= dev
->parent
;
120 struct i2c_mux_ops
*ops
= i2c_mux_get_ops(mux
);
125 return ops
->deselect(mux
, dev
, plat
->channel
);
128 static int i2c_mux_bus_set_bus_speed(struct udevice
*dev
, unsigned int speed
)
130 struct udevice
*mux
= dev
->parent
;
131 struct i2c_mux
*priv
= dev_get_uclass_priv(mux
);
134 ret
= i2c_mux_select(dev
);
137 ret
= dm_i2c_set_bus_speed(priv
->i2c_bus
, speed
);
138 ret2
= i2c_mux_deselect(dev
);
140 return ret
? ret
: ret2
;
143 static int i2c_mux_bus_probe(struct udevice
*dev
, uint chip_addr
,
146 struct udevice
*mux
= dev
->parent
;
147 struct i2c_mux
*priv
= dev_get_uclass_priv(mux
);
148 struct dm_i2c_ops
*ops
= i2c_get_ops(priv
->i2c_bus
);
151 debug("%s: %s, bus %s\n", __func__
, dev
->name
, priv
->i2c_bus
->name
);
152 if (!ops
->probe_chip
)
154 ret
= i2c_mux_select(dev
);
157 ret
= ops
->probe_chip(priv
->i2c_bus
, chip_addr
, chip_flags
);
158 ret2
= i2c_mux_deselect(dev
);
160 return ret
? ret
: ret2
;
163 static int i2c_mux_bus_xfer(struct udevice
*dev
, struct i2c_msg
*msg
,
166 struct udevice
*mux
= dev
->parent
;
167 struct i2c_mux
*priv
= dev_get_uclass_priv(mux
);
168 struct dm_i2c_ops
*ops
= i2c_get_ops(priv
->i2c_bus
);
171 debug("%s: %s, bus %s\n", __func__
, dev
->name
, priv
->i2c_bus
->name
);
174 ret
= i2c_mux_select(dev
);
177 ret
= ops
->xfer(priv
->i2c_bus
, msg
, nmsgs
);
178 ret2
= i2c_mux_deselect(dev
);
180 return ret
? ret
: ret2
;
183 static const struct dm_i2c_ops i2c_mux_bus_ops
= {
184 .xfer
= i2c_mux_bus_xfer
,
185 .probe_chip
= i2c_mux_bus_probe
,
186 .set_bus_speed
= i2c_mux_bus_set_bus_speed
,
189 U_BOOT_DRIVER(i2c_mux_bus
) = {
190 .name
= "i2c_mux_bus_drv",
192 .ops
= &i2c_mux_bus_ops
,
195 UCLASS_DRIVER(i2c_mux
) = {
196 .id
= UCLASS_I2C_MUX
,
198 .post_bind
= i2c_mux_post_bind
,
199 .post_probe
= i2c_mux_post_probe
,
200 .per_device_auto_alloc_size
= sizeof(struct i2c_mux
),
201 .per_child_platdata_auto_alloc_size
= sizeof(struct i2c_mux_bus
),
202 .child_post_bind
= i2c_mux_child_post_bind
,