blob: d1999d21feb77d14a6641d4285492f6a063b5ef8 [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
Michal Simekd5c97ac2019-01-09 11:58:24 +010043 channel = dev_read_u32_default(dev, "reg", -1);
Simon Glass2a80c402015-08-03 08:19:21 -060044 if (channel < 0)
45 return -EINVAL;
46 plat->channel = channel;
47
48 return 0;
49}
50
51/* Find the I2C buses selected by this mux */
52static int i2c_mux_post_bind(struct udevice *mux)
53{
Simon Glasse6dd8da2017-05-18 20:09:07 -060054 ofnode node;
Simon Glass2a80c402015-08-03 08:19:21 -060055 int ret;
Simon Glass2a80c402015-08-03 08:19:21 -060056
57 debug("%s: %s\n", __func__, mux->name);
58 /*
59 * There is no compatible string in the sub-nodes, so we must manually
60 * bind these
61 */
Simon Glasse6dd8da2017-05-18 20:09:07 -060062 dev_for_each_subnode(node, mux) {
Simon Glass2a80c402015-08-03 08:19:21 -060063 struct udevice *dev;
64 const char *name;
Michal Simek236dde62019-01-31 16:31:03 +010065 const char *arrow = "->";
66 char *full_name;
67 int parent_name_len, arrow_len, mux_name_len, name_len;
Simon Glass2a80c402015-08-03 08:19:21 -060068
Simon Glasse6dd8da2017-05-18 20:09:07 -060069 name = ofnode_get_name(node);
Michal Simek236dde62019-01-31 16:31:03 +010070
71 /* Calculate lenghts of strings */
72 parent_name_len = strlen(mux->parent->name);
73 arrow_len = strlen(arrow);
74 mux_name_len = strlen(mux->name);
75 name_len = strlen(name);
76
77 full_name = calloc(1, parent_name_len + arrow_len +
78 mux_name_len + arrow_len + name_len + 1);
79 if (!full_name)
80 return -ENOMEM;
81
82 /* Compose bus name */
83 strcat(full_name, mux->parent->name);
84 strcat(full_name, arrow);
85 strcat(full_name, mux->name);
86 strcat(full_name, arrow);
87 strcat(full_name, name);
88
89 ret = device_bind_driver_to_node(mux, "i2c_mux_bus_drv",
90 full_name, node, &dev);
Simon Glass4123ba02020-12-16 21:20:15 -070091 debug(" - bind ret=%d, %s, seq %d\n", ret,
92 dev ? dev->name : NULL, dev_seq(dev));
Simon Glass2a80c402015-08-03 08:19:21 -060093 if (ret)
94 return ret;
95 }
96
97 return 0;
98}
99
100/* Set up the mux ready for use */
101static int i2c_mux_post_probe(struct udevice *mux)
102{
103 struct i2c_mux *priv = dev_get_uclass_priv(mux);
104 int ret;
105
106 debug("%s: %s\n", __func__, mux->name);
107 priv->selected = -1;
108
Moritz Fischerc5d3a5e2017-01-16 09:46:06 -0800109 /* if parent is of i2c uclass already, we'll take that, otherwise
110 * look if we find an i2c-parent phandle
111 */
112 if (UCLASS_I2C == device_get_uclass_id(mux->parent)) {
113 priv->i2c_bus = dev_get_parent(mux);
114 debug("%s: bus=%p/%s\n", __func__, priv->i2c_bus,
115 priv->i2c_bus->name);
116 return 0;
117 }
118
Simon Glass2a80c402015-08-03 08:19:21 -0600119 ret = uclass_get_device_by_phandle(UCLASS_I2C, mux, "i2c-parent",
120 &priv->i2c_bus);
121 if (ret)
122 return ret;
123 debug("%s: bus=%p/%s\n", __func__, priv->i2c_bus, priv->i2c_bus->name);
124
125 return 0;
126}
127
128int i2c_mux_select(struct udevice *dev)
129{
Simon Glass71fa5b42020-12-03 16:55:18 -0700130 struct i2c_mux_bus *plat = dev_get_parent_plat(dev);
Simon Glass2a80c402015-08-03 08:19:21 -0600131 struct udevice *mux = dev->parent;
132 struct i2c_mux_ops *ops = i2c_mux_get_ops(mux);
133
134 if (!ops->select)
135 return -ENOSYS;
136
137 return ops->select(mux, dev, plat->channel);
138}
139
140int i2c_mux_deselect(struct udevice *dev)
141{
Simon Glass71fa5b42020-12-03 16:55:18 -0700142 struct i2c_mux_bus *plat = dev_get_parent_plat(dev);
Simon Glass2a80c402015-08-03 08:19:21 -0600143 struct udevice *mux = dev->parent;
144 struct i2c_mux_ops *ops = i2c_mux_get_ops(mux);
145
146 if (!ops->deselect)
147 return -ENOSYS;
148
149 return ops->deselect(mux, dev, plat->channel);
150}
151
152static int i2c_mux_bus_set_bus_speed(struct udevice *dev, unsigned int speed)
153{
154 struct udevice *mux = dev->parent;
155 struct i2c_mux *priv = dev_get_uclass_priv(mux);
156 int ret, ret2;
157
158 ret = i2c_mux_select(dev);
159 if (ret)
160 return ret;
161 ret = dm_i2c_set_bus_speed(priv->i2c_bus, speed);
162 ret2 = i2c_mux_deselect(dev);
163
164 return ret ? ret : ret2;
165}
166
167static int i2c_mux_bus_probe(struct udevice *dev, uint chip_addr,
168 uint chip_flags)
169{
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);
173 int ret, ret2;
174
175 debug("%s: %s, bus %s\n", __func__, dev->name, priv->i2c_bus->name);
176 if (!ops->probe_chip)
177 return -ENOSYS;
178 ret = i2c_mux_select(dev);
179 if (ret)
180 return ret;
181 ret = ops->probe_chip(priv->i2c_bus, chip_addr, chip_flags);
182 ret2 = i2c_mux_deselect(dev);
183
184 return ret ? ret : ret2;
185}
186
187static int i2c_mux_bus_xfer(struct udevice *dev, struct i2c_msg *msg,
188 int nmsgs)
189{
190 struct udevice *mux = dev->parent;
191 struct i2c_mux *priv = dev_get_uclass_priv(mux);
192 struct dm_i2c_ops *ops = i2c_get_ops(priv->i2c_bus);
193 int ret, ret2;
194
195 debug("%s: %s, bus %s\n", __func__, dev->name, priv->i2c_bus->name);
196 if (!ops->xfer)
197 return -ENOSYS;
198 ret = i2c_mux_select(dev);
199 if (ret)
200 return ret;
201 ret = ops->xfer(priv->i2c_bus, msg, nmsgs);
202 ret2 = i2c_mux_deselect(dev);
203
204 return ret ? ret : ret2;
205}
206
207static const struct dm_i2c_ops i2c_mux_bus_ops = {
208 .xfer = i2c_mux_bus_xfer,
209 .probe_chip = i2c_mux_bus_probe,
210 .set_bus_speed = i2c_mux_bus_set_bus_speed,
211};
212
213U_BOOT_DRIVER(i2c_mux_bus) = {
214 .name = "i2c_mux_bus_drv",
215 .id = UCLASS_I2C,
Simon Glass2a80c402015-08-03 08:19:21 -0600216 .ops = &i2c_mux_bus_ops,
217};
218
219UCLASS_DRIVER(i2c_mux) = {
220 .id = UCLASS_I2C_MUX,
221 .name = "i2c_mux",
222 .post_bind = i2c_mux_post_bind,
223 .post_probe = i2c_mux_post_probe,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700224 .per_device_auto = sizeof(struct i2c_mux),
Simon Glass71fa5b42020-12-03 16:55:18 -0700225 .per_child_plat_auto = sizeof(struct i2c_mux_bus),
Simon Glass2a80c402015-08-03 08:19:21 -0600226 .child_post_bind = i2c_mux_child_post_bind,
227};