i2cmux.c revision 1.10 1 /* $NetBSD: i2cmux.c,v 1.10 2025/09/16 11:41:26 thorpej Exp $ */
2
3 /*-
4 * Copyright (c) 2020 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Jason R. Thorpe.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 #if defined(__i386__) || defined(__amd64__) || defined(__aarch64__)
33 #include "acpica.h"
34 #endif
35
36 #include <sys/cdefs.h>
37 __KERNEL_RCSID(0, "$NetBSD: i2cmux.c,v 1.10 2025/09/16 11:41:26 thorpej Exp $");
38
39 #include <sys/types.h>
40 #include <sys/device.h>
41 #include <sys/kernel.h>
42 #include <sys/kmem.h>
43
44 #include <dev/fdt/fdtvar.h>
45 #include <dev/i2c/i2cvar.h>
46 #include <dev/i2c/i2cmuxvar.h>
47
48 #if NACPICA > 0
49 #include <dev/acpi/acpivar.h>
50 #endif
51
52 /*
53 * i2c mux
54 *
55 * This works by interposing a set of virtual controllers behind the real
56 * i2c controller. We provide our own acquire and release functions that
57 * perform the following tasks:
58 *
59 * acquire -> acquire parent controller, program mux
60 *
61 * release -> idle mux, release parent controller
62 *
63 * All of the actual I/O operations are transparently passed through.
64 *
65 * N.B. the locking order; the generic I2C layer has already acquired
66 * our virtual controller's mutex before calling our acquire function,
67 * and we will then acquire the real controller's mutex when we acquire
68 * the bus, so the order is:
69 *
70 * mux virtual controller -> parent controller
71 *
72 * These are common routines used by various i2c mux controller
73 * implementations (gpio, pin mux, i2c device, etc.).
74 */
75
76 /*****************************************************************************/
77
78 static int
79 iicmux_acquire_bus(void * const v, int const flags)
80 {
81 struct iicmux_bus * const bus = v;
82 struct iicmux_softc * const sc = bus->mux;
83 int error;
84
85 error = iic_acquire_bus(sc->sc_i2c_parent, flags);
86 if (error) {
87 return error;
88 }
89
90 error = sc->sc_config->acquire_bus(bus, flags);
91 if (error) {
92 iic_release_bus(sc->sc_i2c_parent, flags);
93 }
94
95 return error;
96 }
97
98 static void
99 iicmux_release_bus(void * const v, int const flags)
100 {
101 struct iicmux_bus * const bus = v;
102 struct iicmux_softc * const sc = bus->mux;
103
104 sc->sc_config->release_bus(bus, flags);
105 iic_release_bus(sc->sc_i2c_parent, flags);
106 }
107
108 static int
109 iicmux_exec(void * const v, i2c_op_t const op, i2c_addr_t const addr,
110 const void * const cmdbuf, size_t const cmdlen, void * const databuf,
111 size_t const datalen, int const flags)
112 {
113 struct iicmux_bus * const bus = v;
114 struct iicmux_softc * const sc = bus->mux;
115
116 return iic_exec(sc->sc_i2c_parent, op, addr, cmdbuf, cmdlen,
117 databuf, datalen, flags);
118 }
119
120 /*****************************************************************************/
121
122 static int
123 iicmux_count_children(struct iicmux_softc * const sc)
124 {
125 char name[32];
126 int child, count;
127
128 restart:
129 for (child = OF_child(sc->sc_i2c_mux_phandle), count = 0; child;
130 child = OF_peer(child)) {
131 if (OF_getprop(child, "name", name, sizeof(name)) <= 0) {
132 continue;
133 }
134 if (strcmp(name, "i2c-mux") == 0) {
135 /*
136 * The node we encountered is the actual parent
137 * of the i2c bus children. Stash its phandle
138 * and restart the enumeration.
139 */
140 sc->sc_i2c_mux_phandle = child;
141 goto restart;
142 }
143 count++;
144 }
145
146 return count;
147 }
148
149 #if 0
150 /* XXX iicbus_print() should be able to do this. */
151 static int
152 iicmux_print(void * const aux, const char * const pnp)
153 {
154 i2c_tag_t const tag = aux;
155 struct iicmux_bus * const bus = tag->ic_cookie;
156 int rv;
157
158 rv = iicbus_print(aux, pnp);
159 aprint_normal(" bus %d", bus->busidx);
160
161 return rv;
162 }
163 #endif
164
165 static void
166 iicmux_attach_bus(struct iicmux_softc * const sc,
167 uintptr_t const handle, enum i2c_cookie_type handletype, int const busidx)
168 {
169 struct iicmux_bus * const bus = &sc->sc_busses[busidx];
170
171 bus->mux = sc;
172 bus->busidx = busidx;
173 bus->handle = handle;
174 bus->handletype = handletype;
175
176 bus->bus_data = sc->sc_config->get_bus_info(bus);
177 if (bus->bus_data == NULL) {
178 aprint_error_dev(sc->sc_dev,
179 "unable to get info for bus %d\n", busidx);
180 return;
181 }
182
183 iic_tag_init(&bus->controller);
184 bus->controller.ic_cookie = bus;
185 bus->controller.ic_acquire_bus = iicmux_acquire_bus;
186 bus->controller.ic_release_bus = iicmux_release_bus;
187 bus->controller.ic_exec = iicmux_exec;
188
189 switch (handletype) {
190 case I2C_COOKIE_OF:
191 fdtbus_register_i2c_controller(&bus->controller,
192 (int)bus->handle);
193
194 iicbus_attach_with_devhandle(sc->sc_dev, &bus->controller,
195 devhandle_from_of(device_handle(sc->sc_dev),
196 (int)bus->handle));
197 break;
198 #if NACPICA > 0
199 case I2C_COOKIE_ACPI: {
200 iicbus_attach_with_devhandle(sc->sc_dev, &bus->controller,
201 devhandle_from_acpi(device_handle(sc->sc_dev),
202 (ACPI_HANDLE)handle));
203 } break;
204 #endif
205 default:
206 aprint_error_dev(sc->sc_dev, "unknown handle type\n");
207 break;
208 }
209 }
210
211 static void
212 iicmux_attach_fdt(struct iicmux_softc * const sc)
213 {
214 /*
215 * We start out assuming that the i2c bus nodes are children of
216 * our own node. We'll adjust later if we encounter an "i2c-mux"
217 * node when counting our children. If we encounter such a node,
218 * then it's that node that is the parent of the i2c bus children.
219 */
220 sc->sc_i2c_mux_phandle = (int)sc->sc_handle;
221
222 sc->sc_nbusses = iicmux_count_children(sc);
223 if (sc->sc_nbusses == 0) {
224 return;
225 }
226
227 sc->sc_busses = kmem_zalloc(sizeof(*sc->sc_busses) * sc->sc_nbusses,
228 KM_SLEEP);
229
230 int child, idx;
231 for (child = OF_child(sc->sc_i2c_mux_phandle), idx = 0; child;
232 child = OF_peer(child), idx++) {
233 KASSERT(idx < sc->sc_nbusses);
234 iicmux_attach_bus(sc, child, I2C_COOKIE_OF, idx);
235 }
236 }
237
238 #if NACPICA > 0
239 static void
240 iicmux_attach_acpi(struct iicmux_softc * const sc)
241 {
242 ACPI_HANDLE hdl = (ACPI_HANDLE)sc->sc_handle;
243 struct acpi_devnode *devnode, *ad;
244 int idx;
245
246 devnode = acpi_match_node(hdl);
247 KASSERT(devnode != NULL);
248
249 /* Count child busses */
250 sc->sc_nbusses = 0;
251 SIMPLEQ_FOREACH(ad, &devnode->ad_child_head, ad_child_list) {
252 if (ad->ad_devinfo->Type != ACPI_TYPE_DEVICE ||
253 !acpi_device_present(ad->ad_handle)) {
254 continue;
255 }
256 sc->sc_nbusses++;
257 }
258
259 sc->sc_busses = kmem_zalloc(sizeof(*sc->sc_busses) * sc->sc_nbusses,
260 KM_SLEEP);
261
262 /* Attach child busses */
263 idx = 0;
264 SIMPLEQ_FOREACH(ad, &devnode->ad_child_head, ad_child_list) {
265 if (ad->ad_devinfo->Type != ACPI_TYPE_DEVICE ||
266 !acpi_device_present(ad->ad_handle)) {
267 continue;
268 }
269 iicmux_attach_bus(sc, (uintptr_t)ad->ad_handle,
270 I2C_COOKIE_ACPI, idx);
271 idx++;
272 }
273 }
274 #endif
275
276 void
277 iicmux_attach(struct iicmux_softc * const sc)
278 {
279 /*
280 * We expect sc->sc_handle, sc->sc_config, and sc->sc_i2c_parent
281 * to be initialized by the front-end.
282 */
283 KASSERT(sc->sc_handle > 0);
284 KASSERT(sc->sc_config != NULL);
285 KASSERT(sc->sc_i2c_parent != NULL);
286
287 /*
288 * Gather up all of the various bits of information needed
289 * for this particular type of i2c mux.
290 */
291 sc->sc_mux_data = sc->sc_config->get_mux_info(sc);
292 if (sc->sc_mux_data == NULL) {
293 aprint_error_dev(sc->sc_dev, "unable to get info for mux\n");
294 return;
295 }
296
297 /*
298 * Do configuration method (OF, ACPI) specific setup.
299 */
300 switch (sc->sc_handletype) {
301 case I2C_COOKIE_OF:
302 iicmux_attach_fdt(sc);
303 break;
304 #if NACPICA > 0
305 case I2C_COOKIE_ACPI:
306 iicmux_attach_acpi(sc);
307 break;
308 #endif
309 default:
310 aprint_error_dev(sc->sc_dev, "could not configure mux: "
311 "handle type %u not supported\n", sc->sc_handletype);
312 break;
313 }
314 }
315