blob: 9a3dd7ec4a94659b71ff268b981efce56f9639cb [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
7#include <common.h>
8#include <dm.h>
9#include <errno.h>
10#include <i2c.h>
Simon Glass9bc15642020-02-03 07:36:16 -070011#include <malloc.h>
Simon Glass2a80c402015-08-03 08:19:21 -060012#include <dm/lists.h>
13#include <dm/root.h>
14
Simon Glass2a80c402015-08-03 08:19:21 -060015/**
16 * struct i2c_mux: Information the uclass stores about an I2C mux
17 *
18 * @selected: Currently selected mux, or -1 for none
19 * @i2c_bus: I2C bus to use for communcation
20 */
21struct i2c_mux {
22 int selected;
23 struct udevice *i2c_bus;
24};
25
26/**
27 * struct i2c_mux_bus: Information about each bus the mux controls
28 *
29 * @channel: Channel number used to select this bus
30 */
31struct i2c_mux_bus {
32 uint channel;
33};
34
35/* Find out the mux channel number */
36static int i2c_mux_child_post_bind(struct udevice *dev)
37{
38 struct i2c_mux_bus *plat = dev_get_parent_platdata(dev);
39 int channel;
40
Michal Simekd5c97ac2019-01-09 11:58:24 +010041 channel = dev_read_u32_default(dev, "reg", -1);
Simon Glass2a80c402015-08-03 08:19:21 -060042 if (channel < 0)
43 return -EINVAL;
44 plat->channel = channel;
45
46 return 0;
47}
48
49/* Find the I2C buses selected by this mux */
50static int i2c_mux_post_bind(struct udevice *mux)
51{
Simon Glasse6dd8da2017-05-18 20:09:07 -060052 ofnode node;
Simon Glass2a80c402015-08-03 08:19:21 -060053 int ret;
Simon Glass2a80c402015-08-03 08:19:21 -060054
55 debug("%s: %s\n", __func__, mux->name);
56 /*
57 * There is no compatible string in the sub-nodes, so we must manually
58 * bind these
59 */
Simon Glasse6dd8da2017-05-18 20:09:07 -060060 dev_for_each_subnode(node, mux) {
Simon Glass2a80c402015-08-03 08:19:21 -060061 struct udevice *dev;
62 const char *name;
Michal Simek236dde62019-01-31 16:31:03 +010063 const char *arrow = "->";
64 char *full_name;
65 int parent_name_len, arrow_len, mux_name_len, name_len;
Simon Glass2a80c402015-08-03 08:19:21 -060066
Simon Glasse6dd8da2017-05-18 20:09:07 -060067 name = ofnode_get_name(node);
Michal Simek236dde62019-01-31 16:31:03 +010068
69 /* Calculate lenghts of strings */
70 parent_name_len = strlen(mux->parent->name);
71 arrow_len = strlen(arrow);
72 mux_name_len = strlen(mux->name);
73 name_len = strlen(name);
74
75 full_name = calloc(1, parent_name_len + arrow_len +
76 mux_name_len + arrow_len + name_len + 1);
77 if (!full_name)
78 return -ENOMEM;
79
80 /* Compose bus name */
81 strcat(full_name, mux->parent->name);
82 strcat(full_name, arrow);
83 strcat(full_name, mux->name);
84 strcat(full_name, arrow);
85 strcat(full_name, name);
86
87 ret = device_bind_driver_to_node(mux, "i2c_mux_bus_drv",
88 full_name, node, &dev);
89 debug(" - bind ret=%d, %s, req_seq %d\n", ret,
90 dev ? dev->name : NULL, dev->req_seq);
Simon Glass2a80c402015-08-03 08:19:21 -060091 if (ret)
92 return ret;
93 }
94
95 return 0;
96}
97
98/* Set up the mux ready for use */
99static int i2c_mux_post_probe(struct udevice *mux)
100{
101 struct i2c_mux *priv = dev_get_uclass_priv(mux);
102 int ret;
103
104 debug("%s: %s\n", __func__, mux->name);
105 priv->selected = -1;
106
Moritz Fischerc5d3a5e2017-01-16 09:46:06 -0800107 /* if parent is of i2c uclass already, we'll take that, otherwise
108 * look if we find an i2c-parent phandle
109 */
110 if (UCLASS_I2C == device_get_uclass_id(mux->parent)) {
111 priv->i2c_bus = dev_get_parent(mux);
112 debug("%s: bus=%p/%s\n", __func__, priv->i2c_bus,
113 priv->i2c_bus->name);
114 return 0;
115 }
116
Simon Glass2a80c402015-08-03 08:19:21 -0600117 ret = uclass_get_device_by_phandle(UCLASS_I2C, mux, "i2c-parent",
118 &priv->i2c_bus);
119 if (ret)
120 return ret;
121 debug("%s: bus=%p/%s\n", __func__, priv->i2c_bus, priv->i2c_bus->name);
122
123 return 0;
124}
125
126int i2c_mux_select(struct udevice *dev)
127{
128 struct i2c_mux_bus *plat = dev_get_parent_platdata(dev);
129 struct udevice *mux = dev->parent;
130 struct i2c_mux_ops *ops = i2c_mux_get_ops(mux);
131
132 if (!ops->select)
133 return -ENOSYS;
134
135 return ops->select(mux, dev, plat->channel);
136}
137
138int i2c_mux_deselect(struct udevice *dev)
139{
140 struct i2c_mux_bus *plat = dev_get_parent_platdata(dev);
141 struct udevice *mux = dev->parent;
142 struct i2c_mux_ops *ops = i2c_mux_get_ops(mux);
143
144 if (!ops->deselect)
145 return -ENOSYS;
146
147 return ops->deselect(mux, dev, plat->channel);
148}
149
150static int i2c_mux_bus_set_bus_speed(struct udevice *dev, unsigned int speed)
151{
152 struct udevice *mux = dev->parent;
153 struct i2c_mux *priv = dev_get_uclass_priv(mux);
154 int ret, ret2;
155
156 ret = i2c_mux_select(dev);
157 if (ret)
158 return ret;
159 ret = dm_i2c_set_bus_speed(priv->i2c_bus, speed);
160 ret2 = i2c_mux_deselect(dev);
161
162 return ret ? ret : ret2;
163}
164
165static int i2c_mux_bus_probe(struct udevice *dev, uint chip_addr,
166 uint chip_flags)
167{
168 struct udevice *mux = dev->parent;
169 struct i2c_mux *priv = dev_get_uclass_priv(mux);
170 struct dm_i2c_ops *ops = i2c_get_ops(priv->i2c_bus);
171 int ret, ret2;
172
173 debug("%s: %s, bus %s\n", __func__, dev->name, priv->i2c_bus->name);
174 if (!ops->probe_chip)
175 return -ENOSYS;
176 ret = i2c_mux_select(dev);
177 if (ret)
178 return ret;
179 ret = ops->probe_chip(priv->i2c_bus, chip_addr, chip_flags);
180 ret2 = i2c_mux_deselect(dev);
181
182 return ret ? ret : ret2;
183}
184
185static int i2c_mux_bus_xfer(struct udevice *dev, struct i2c_msg *msg,
186 int nmsgs)
187{
188 struct udevice *mux = dev->parent;
189 struct i2c_mux *priv = dev_get_uclass_priv(mux);
190 struct dm_i2c_ops *ops = i2c_get_ops(priv->i2c_bus);
191 int ret, ret2;
192
193 debug("%s: %s, bus %s\n", __func__, dev->name, priv->i2c_bus->name);
194 if (!ops->xfer)
195 return -ENOSYS;
196 ret = i2c_mux_select(dev);
197 if (ret)
198 return ret;
199 ret = ops->xfer(priv->i2c_bus, msg, nmsgs);
200 ret2 = i2c_mux_deselect(dev);
201
202 return ret ? ret : ret2;
203}
204
205static const struct dm_i2c_ops i2c_mux_bus_ops = {
206 .xfer = i2c_mux_bus_xfer,
207 .probe_chip = i2c_mux_bus_probe,
208 .set_bus_speed = i2c_mux_bus_set_bus_speed,
209};
210
211U_BOOT_DRIVER(i2c_mux_bus) = {
212 .name = "i2c_mux_bus_drv",
213 .id = UCLASS_I2C,
Simon Glass2a80c402015-08-03 08:19:21 -0600214 .ops = &i2c_mux_bus_ops,
215};
216
217UCLASS_DRIVER(i2c_mux) = {
218 .id = UCLASS_I2C_MUX,
219 .name = "i2c_mux",
220 .post_bind = i2c_mux_post_bind,
221 .post_probe = i2c_mux_post_probe,
222 .per_device_auto_alloc_size = sizeof(struct i2c_mux),
223 .per_child_platdata_auto_alloc_size = sizeof(struct i2c_mux_bus),
224 .child_post_bind = i2c_mux_child_post_bind,
225};