blob: 012881de05bfb8652518f6b881f1cf418ac93805 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glass2a80c402015-08-03 08:19:21 -06002/*
3 * Copyright (c) 2015 Google, Inc
4 * Written by Simon Glass <sjg@chromium.org>
Simon Glass2a80c402015-08-03 08:19:21 -06005 */
6
Patrick Delaunay81313352021-04-27 11:02:19 +02007#define LOG_CATEGORY UCLASS_I2C_MUX
8
Simon Glass2a80c402015-08-03 08:19:21 -06009#include <dm.h>
10#include <errno.h>
11#include <i2c.h>
Simon Glass0f2af882020-05-10 11:40:05 -060012#include <log.h>
Simon Glass9bc15642020-02-03 07:36:16 -070013#include <malloc.h>
Simon Glass2a80c402015-08-03 08:19:21 -060014#include <dm/lists.h>
15#include <dm/root.h>
16
Simon Glass2a80c402015-08-03 08:19:21 -060017/**
18 * struct i2c_mux: Information the uclass stores about an I2C mux
19 *
20 * @selected: Currently selected mux, or -1 for none
21 * @i2c_bus: I2C bus to use for communcation
22 */
23struct i2c_mux {
24 int selected;
25 struct udevice *i2c_bus;
26};
27
28/**
29 * struct i2c_mux_bus: Information about each bus the mux controls
30 *
31 * @channel: Channel number used to select this bus
32 */
33struct i2c_mux_bus {
34 uint channel;
35};
36
37/* Find out the mux channel number */
38static int i2c_mux_child_post_bind(struct udevice *dev)
39{
Simon Glass71fa5b42020-12-03 16:55:18 -070040 struct i2c_mux_bus *plat = dev_get_parent_plat(dev);
Simon Glass2a80c402015-08-03 08:19:21 -060041 int channel;
42
Venkatesh Yadav Abbarapu24d5bed2025-06-23 13:36:07 +053043 ofnode node = dev_ofnode(dev);
44
45 if (!ofnode_has_property(node, "reg"))
46 return 0;
47
Michal Simekd5c97ac2019-01-09 11:58:24 +010048 channel = dev_read_u32_default(dev, "reg", -1);
Simon Glass2a80c402015-08-03 08:19:21 -060049 if (channel < 0)
50 return -EINVAL;
51 plat->channel = channel;
52
53 return 0;
54}
55
56/* Find the I2C buses selected by this mux */
57static int i2c_mux_post_bind(struct udevice *mux)
58{
Simon Glasse6dd8da2017-05-18 20:09:07 -060059 ofnode node;
Simon Glass2a80c402015-08-03 08:19:21 -060060 int ret;
Simon Glass2a80c402015-08-03 08:19:21 -060061
62 debug("%s: %s\n", __func__, mux->name);
63 /*
64 * There is no compatible string in the sub-nodes, so we must manually
65 * bind these
66 */
Simon Glasse6dd8da2017-05-18 20:09:07 -060067 dev_for_each_subnode(node, mux) {
Simon Glass2a80c402015-08-03 08:19:21 -060068 struct udevice *dev;
69 const char *name;
Michal Simek236dde62019-01-31 16:31:03 +010070 const char *arrow = "->";
71 char *full_name;
72 int parent_name_len, arrow_len, mux_name_len, name_len;
Simon Glass2a80c402015-08-03 08:19:21 -060073
Simon Glasse6dd8da2017-05-18 20:09:07 -060074 name = ofnode_get_name(node);
Michal Simek236dde62019-01-31 16:31:03 +010075
76 /* Calculate lenghts of strings */
77 parent_name_len = strlen(mux->parent->name);
78 arrow_len = strlen(arrow);
79 mux_name_len = strlen(mux->name);
80 name_len = strlen(name);
81
82 full_name = calloc(1, parent_name_len + arrow_len +
83 mux_name_len + arrow_len + name_len + 1);
84 if (!full_name)
85 return -ENOMEM;
86
87 /* Compose bus name */
88 strcat(full_name, mux->parent->name);
89 strcat(full_name, arrow);
90 strcat(full_name, mux->name);
91 strcat(full_name, arrow);
92 strcat(full_name, name);
93
94 ret = device_bind_driver_to_node(mux, "i2c_mux_bus_drv",
95 full_name, node, &dev);
Simon Glass4123ba02020-12-16 21:20:15 -070096 debug(" - bind ret=%d, %s, seq %d\n", ret,
97 dev ? dev->name : NULL, dev_seq(dev));
Simon Glass2a80c402015-08-03 08:19:21 -060098 if (ret)
99 return ret;
100 }
101
102 return 0;
103}
104
105/* Set up the mux ready for use */
106static int i2c_mux_post_probe(struct udevice *mux)
107{
108 struct i2c_mux *priv = dev_get_uclass_priv(mux);
109 int ret;
110
111 debug("%s: %s\n", __func__, mux->name);
112 priv->selected = -1;
113
Moritz Fischerc5d3a5e2017-01-16 09:46:06 -0800114 /* if parent is of i2c uclass already, we'll take that, otherwise
115 * look if we find an i2c-parent phandle
116 */
117 if (UCLASS_I2C == device_get_uclass_id(mux->parent)) {
118 priv->i2c_bus = dev_get_parent(mux);
119 debug("%s: bus=%p/%s\n", __func__, priv->i2c_bus,
120 priv->i2c_bus->name);
121 return 0;
122 }
123
Simon Glass2a80c402015-08-03 08:19:21 -0600124 ret = uclass_get_device_by_phandle(UCLASS_I2C, mux, "i2c-parent",
125 &priv->i2c_bus);
126 if (ret)
127 return ret;
128 debug("%s: bus=%p/%s\n", __func__, priv->i2c_bus, priv->i2c_bus->name);
129
130 return 0;
131}
132
133int i2c_mux_select(struct udevice *dev)
134{
Simon Glass71fa5b42020-12-03 16:55:18 -0700135 struct i2c_mux_bus *plat = dev_get_parent_plat(dev);
Simon Glass2a80c402015-08-03 08:19:21 -0600136 struct udevice *mux = dev->parent;
137 struct i2c_mux_ops *ops = i2c_mux_get_ops(mux);
138
139 if (!ops->select)
140 return -ENOSYS;
141
142 return ops->select(mux, dev, plat->channel);
143}
144
145int i2c_mux_deselect(struct udevice *dev)
146{
Simon Glass71fa5b42020-12-03 16:55:18 -0700147 struct i2c_mux_bus *plat = dev_get_parent_plat(dev);
Simon Glass2a80c402015-08-03 08:19:21 -0600148 struct udevice *mux = dev->parent;
149 struct i2c_mux_ops *ops = i2c_mux_get_ops(mux);
150
151 if (!ops->deselect)
152 return -ENOSYS;
153
154 return ops->deselect(mux, dev, plat->channel);
155}
156
157static int i2c_mux_bus_set_bus_speed(struct udevice *dev, unsigned int speed)
158{
159 struct udevice *mux = dev->parent;
160 struct i2c_mux *priv = dev_get_uclass_priv(mux);
161 int ret, ret2;
162
163 ret = i2c_mux_select(dev);
164 if (ret)
165 return ret;
166 ret = dm_i2c_set_bus_speed(priv->i2c_bus, speed);
167 ret2 = i2c_mux_deselect(dev);
168
169 return ret ? ret : ret2;
170}
171
172static int i2c_mux_bus_probe(struct udevice *dev, uint chip_addr,
173 uint chip_flags)
174{
175 struct udevice *mux = dev->parent;
176 struct i2c_mux *priv = dev_get_uclass_priv(mux);
177 struct dm_i2c_ops *ops = i2c_get_ops(priv->i2c_bus);
178 int ret, ret2;
179
180 debug("%s: %s, bus %s\n", __func__, dev->name, priv->i2c_bus->name);
181 if (!ops->probe_chip)
182 return -ENOSYS;
183 ret = i2c_mux_select(dev);
184 if (ret)
185 return ret;
186 ret = ops->probe_chip(priv->i2c_bus, chip_addr, chip_flags);
187 ret2 = i2c_mux_deselect(dev);
188
189 return ret ? ret : ret2;
190}
191
192static int i2c_mux_bus_xfer(struct udevice *dev, struct i2c_msg *msg,
193 int nmsgs)
194{
195 struct udevice *mux = dev->parent;
196 struct i2c_mux *priv = dev_get_uclass_priv(mux);
197 struct dm_i2c_ops *ops = i2c_get_ops(priv->i2c_bus);
198 int ret, ret2;
199
200 debug("%s: %s, bus %s\n", __func__, dev->name, priv->i2c_bus->name);
201 if (!ops->xfer)
202 return -ENOSYS;
203 ret = i2c_mux_select(dev);
204 if (ret)
205 return ret;
206 ret = ops->xfer(priv->i2c_bus, msg, nmsgs);
207 ret2 = i2c_mux_deselect(dev);
208
209 return ret ? ret : ret2;
210}
211
212static const struct dm_i2c_ops i2c_mux_bus_ops = {
213 .xfer = i2c_mux_bus_xfer,
214 .probe_chip = i2c_mux_bus_probe,
215 .set_bus_speed = i2c_mux_bus_set_bus_speed,
216};
217
218U_BOOT_DRIVER(i2c_mux_bus) = {
219 .name = "i2c_mux_bus_drv",
220 .id = UCLASS_I2C,
Simon Glass2a80c402015-08-03 08:19:21 -0600221 .ops = &i2c_mux_bus_ops,
222};
223
224UCLASS_DRIVER(i2c_mux) = {
225 .id = UCLASS_I2C_MUX,
226 .name = "i2c_mux",
227 .post_bind = i2c_mux_post_bind,
228 .post_probe = i2c_mux_post_probe,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700229 .per_device_auto = sizeof(struct i2c_mux),
Simon Glass71fa5b42020-12-03 16:55:18 -0700230 .per_child_plat_auto = sizeof(struct i2c_mux_bus),
Simon Glass2a80c402015-08-03 08:19:21 -0600231 .child_post_bind = i2c_mux_child_post_bind,
232};