i2cmux.c revision 1.6.2.2 1 /* $NetBSD: i2cmux.c,v 1.6.2.2 2021/08/09 00:57:56 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 #include <sys/cdefs.h>
33 __KERNEL_RCSID(0, "$NetBSD: i2cmux.c,v 1.6.2.2 2021/08/09 00:57:56 thorpej Exp $");
34
35 #include <sys/types.h>
36 #include <sys/device.h>
37 #include <sys/kernel.h>
38 #include <sys/kmem.h>
39
40 #include <dev/i2c/i2cvar.h>
41 #include <dev/i2c/i2cmuxvar.h>
42
43 #include "locators.h"
44
45 /*
46 * i2c mux
47 *
48 * This works by interposing a set of virtual controllers behind the real
49 * i2c controller. We provide our own acquire and release functions that
50 * perform the following tasks:
51 *
52 * acquire -> acquire parent controller, program mux
53 *
54 * release -> idle mux, release parent controller
55 *
56 * All of the actual I/O operations are transparently passed through.
57 *
58 * N.B. the locking order; the generic I2C layer has already acquired
59 * our virtual controller's mutex before calling our acquire function,
60 * and we will then acquire the real controller's mutex when we acquire
61 * the bus, so the order is:
62 *
63 * mux virtual controller -> parent controller
64 *
65 * These are common routines used by various i2c mux controller
66 * implementations (gpio, pin mux, i2c device, etc.).
67 */
68
69 /*****************************************************************************/
70
71 static int
72 iicmux_acquire_bus(void * const v, int const flags)
73 {
74 struct iicmux_bus * const bus = v;
75 struct iicmux_softc * const sc = bus->mux;
76 int error;
77
78 error = iic_acquire_bus(sc->sc_i2c_parent, flags);
79 if (error) {
80 return error;
81 }
82
83 error = sc->sc_config->acquire_bus(bus, flags);
84 if (error) {
85 iic_release_bus(sc->sc_i2c_parent, flags);
86 }
87
88 return error;
89 }
90
91 static void
92 iicmux_release_bus(void * const v, int const flags)
93 {
94 struct iicmux_bus * const bus = v;
95 struct iicmux_softc * const sc = bus->mux;
96
97 sc->sc_config->release_bus(bus, flags);
98 iic_release_bus(sc->sc_i2c_parent, flags);
99 }
100
101 static int
102 iicmux_exec(void * const v, i2c_op_t const op, i2c_addr_t const addr,
103 const void * const cmdbuf, size_t const cmdlen, void * const databuf,
104 size_t const datalen, int const flags)
105 {
106 struct iicmux_bus * const bus = v;
107 struct iicmux_softc * const sc = bus->mux;
108
109 return iic_exec(sc->sc_i2c_parent, op, addr, cmdbuf, cmdlen,
110 databuf, datalen, flags);
111 }
112
113 /*****************************************************************************/
114
115 static void
116 iicmux_attach_bus(struct iicmux_softc * const sc, devhandle_t devhandle,
117 int const busidx)
118 {
119 struct iicmux_bus * const bus = &sc->sc_busses[busidx];
120
121 bus->mux = sc;
122 bus->busidx = busidx;
123 bus->devhandle = devhandle;
124
125 bus->bus_data = sc->sc_config->get_bus_info(bus);
126 if (bus->bus_data == NULL) {
127 aprint_error_dev(sc->sc_dev,
128 "unable to get info for bus %d\n", busidx);
129 return;
130 }
131
132 iic_tag_init(&bus->controller);
133 bus->controller.ic_cookie = bus;
134 bus->controller.ic_channel = bus->busidx;
135 bus->controller.ic_acquire_bus = iicmux_acquire_bus;
136 bus->controller.ic_release_bus = iicmux_release_bus;
137 bus->controller.ic_exec = iicmux_exec;
138
139 #if defined(I2CMUX_USE_FDT)
140 if (devhandle_type(devhandle) == DEVHANDLE_TYPE_OF) {
141 fdtbus_register_i2c_controller(&bus->controller,
142 devhandle_to_of(devhandle));
143 }
144 #endif /* I2CMUX_USE_FDT */
145
146 struct i2cbus_attach_args iba = {
147 .iba_tag = &bus->controller,
148 };
149
150 int locs[I2CBUSCF_NLOCS];
151 locs[I2CBUSCF_BUS] = bus->busidx;
152
153 config_found(sc->sc_dev, &iba, iicbus_print_multi,
154 CFARGS(.submatch = config_stdsubmatch,
155 .locators = locs,
156 .devhandle = devhandle));
157 }
158
159 static bool
160 iicmux_count_busses_callback(device_t self, devhandle_t devhandle,
161 void *v __unused)
162 {
163 struct iicmux_softc *sc = device_private(self);
164
165 #if defined(I2CMUX_USE_FDT)
166 if (devhandle_type(devhandle) == DEVHANDLE_TYPE_OF) {
167 char name[32];
168
169 if (OF_getprop(devhandle_to_of(devhandle), "name", name,
170 sizeof(name)) <= 0) {
171 /* Skip this DT node (shouldn't happen). */
172 return true; /* keep enumerating */
173 }
174 if (strcmp(name, "i2c-mux") == 0) {
175 /*
176 * This DT node is the actual mux node; reset the
177 * our devhandle and restart enumeration.
178 */
179 device_set_handle(self, devhandle);
180 sc->sc_nbusses = -1;
181 return false; /* stop enumerating */
182 }
183 }
184 #endif /* I2CMUX_USE_FDT */
185
186 sc->sc_nbusses++;
187 return true; /* keep enumerating */
188 }
189
190 static bool
191 iicmux_attach_busses_callback(device_t self, devhandle_t devhandle, void *v)
192 {
193 struct iicmux_softc *sc = device_private(self);
194 int * const idxp = v;
195
196 KASSERT(*idxp < sc->sc_nbusses);
197 iicmux_attach_bus(sc, devhandle, (*idxp)++);
198
199 return true; /* keep enumerating */
200 }
201
202 void
203 iicmux_attach(struct iicmux_softc * const sc)
204 {
205 int error, idx;
206
207 /*
208 * We expect sc->sc_config and sc->sc_i2c_parent to be initialized
209 * by the front-end.
210 */
211 KASSERT(sc->sc_config != NULL);
212 KASSERT(sc->sc_i2c_parent != NULL);
213
214 /*
215 * Gather up all of the various bits of information needed
216 * for this particular type of i2c mux.
217 */
218 sc->sc_mux_data = sc->sc_config->get_mux_info(sc);
219 if (sc->sc_mux_data == NULL) {
220 aprint_error_dev(sc->sc_dev, "unable to get info for mux\n");
221 return;
222 }
223
224 /*
225 * Count the number of busses behind this mux.
226 */
227 count_again:
228 error = device_enumerate_children(sc->sc_dev,
229 iicmux_count_busses_callback, NULL);
230 if (error) {
231 aprint_error_dev(sc->sc_dev,
232 "unable to enumerate busses to count, error = %d\n", error);
233 return;
234 }
235 if (sc->sc_nbusses == -1) {
236 /*
237 * We had to reset our devhandle to a different device
238 * tree node. We need to try counting again.
239 */
240 sc->sc_nbusses = 0;
241 goto count_again;
242 }
243 if (sc->sc_nbusses == 0) {
244 /* No busses; no more work to do. */
245 return;
246 }
247
248 sc->sc_busses = kmem_zalloc(sizeof(*sc->sc_busses) * sc->sc_nbusses,
249 KM_SLEEP);
250
251 /* Now attach them. */
252 idx = 0;
253 error = device_enumerate_children(sc->sc_dev,
254 iicmux_attach_busses_callback, &idx);
255 if (error) {
256 aprint_error_dev(sc->sc_dev,
257 "unable to enumerate busses to attach, error = %d\n",
258 error);
259 }
260 }
261