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