i2cmux.c revision 1.5.2.2 1 /* $NetBSD: i2cmux.c,v 1.5.2.2 2021/05/08 14:23:15 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.5.2.2 2021/05/08 14:23:15 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_acquire_bus = iicmux_acquire_bus;
135 bus->controller.ic_release_bus = iicmux_release_bus;
136 bus->controller.ic_exec = iicmux_exec;
137
138 #if defined(I2CMUX_USE_FDT)
139 if (devhandle_type(devhandle) == DEVHANDLE_TYPE_OF) {
140 fdtbus_register_i2c_controller(&bus->controller,
141 devhandle_to_of(devhandle));
142 }
143 #endif /* I2CMUX_USE_FDT */
144
145 struct i2cbus_attach_args iba = {
146 .iba_tag = &bus->controller,
147 .iba_bus = bus->busidx,
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 CFARG_SUBMATCH, config_stdsubmatch,
155 CFARG_LOCATORS, locs,
156 CFARG_DEVHANDLE, devhandle,
157 CFARG_EOL);
158 }
159
160 #if defined(I2CMUX_USE_FDT)
161 static int
162 iicmux_fdt_count_children(struct iicmux_softc * const sc)
163 {
164 int phandle = devhandle_to_of(sc->sc_i2c_mux_devhandle);
165 char name[32];
166 int child, count;
167
168 restart:
169 for (child = OF_child(phandle), count = 0; child;
170 child = OF_peer(child)) {
171 if (OF_getprop(child, "name", name, sizeof(name)) <= 0) {
172 continue;
173 }
174 if (strcmp(name, "i2c-mux") == 0) {
175 phandle = child;
176 goto restart;
177 }
178 count++;
179 }
180
181 /* phandle may have changed. */
182 sc->sc_i2c_mux_devhandle = devhandle_from_of(phandle);
183 return count;
184 }
185
186 static void
187 iicmux_attach_fdt(struct iicmux_softc * const sc)
188 {
189 int phandle = devhandle_to_of(sc->sc_i2c_mux_devhandle);
190
191 sc->sc_nbusses = iicmux_fdt_count_children(sc);
192 if (sc->sc_nbusses == 0) {
193 return;
194 }
195
196 /* sc_i2c_mux_devhandle may have changed. */
197 phandle = devhandle_to_of(sc->sc_i2c_mux_devhandle);
198
199 sc->sc_busses = kmem_zalloc(sizeof(*sc->sc_busses) * sc->sc_nbusses,
200 KM_SLEEP);
201
202 int child, idx;
203 for (child = OF_child(phandle), idx = 0; child;
204 child = OF_peer(child), idx++) {
205 KASSERT(idx < sc->sc_nbusses);
206 iicmux_attach_bus(sc, devhandle_from_of(child), idx);
207 }
208 }
209 #endif /* I2CMUX_USE_FDT */
210
211 #if defined(I2CMUX_USE_ACPI)
212 static void
213 iicmux_attach_acpi(struct iicmux_softc * const sc)
214 {
215 ACPI_HANDLE hdl = devhandle_to_acpi(sc->sc_i2c_mux_devhandle);
216 struct acpi_devnode *devnode, *ad;
217 int idx;
218
219 devnode = acpi_match_node(hdl);
220 KASSERT(devnode != NULL);
221
222 /* Count child busses */
223 sc->sc_nbusses = 0;
224 SIMPLEQ_FOREACH(ad, &devnode->ad_child_head, ad_child_list) {
225 if (ad->ad_devinfo->Type != ACPI_TYPE_DEVICE ||
226 !acpi_device_present(ad->ad_handle)) {
227 continue;
228 }
229 sc->sc_nbusses++;
230 }
231
232 sc->sc_busses = kmem_zalloc(sizeof(*sc->sc_busses) * sc->sc_nbusses,
233 KM_SLEEP);
234
235 /* Attach child busses */
236 idx = 0;
237 SIMPLEQ_FOREACH(ad, &devnode->ad_child_head, ad_child_list) {
238 if (ad->ad_devinfo->Type != ACPI_TYPE_DEVICE ||
239 !acpi_device_present(ad->ad_handle)) {
240 continue;
241 }
242 iicmux_attach_bus(sc, devhandle_from_acpi(ad->ad_handle), idx);
243 idx++;
244 }
245 }
246 #endif /* I2CMUX_USE_ACPI */
247
248 void
249 iicmux_attach(struct iicmux_softc * const sc)
250 {
251 devhandle_t devhandle = device_handle(sc->sc_dev);
252
253 /*
254 * We expect sc->sc_config and sc->sc_i2c_parent to be initialized
255 * by the front-end.
256 */
257 KASSERT(sc->sc_config != NULL);
258 KASSERT(sc->sc_i2c_parent != NULL);
259
260 /*
261 * We start out assuming that the i2c bus nodes are children of
262 * our own node. We'll adjust later if we encounter a subnode
263 * while enumerating our child busses that itself is defined as
264 * the mux node according to our device tree bindings.
265 */
266 sc->sc_i2c_mux_devhandle = devhandle;
267
268 /*
269 * Gather up all of the various bits of information needed
270 * for this particular type of i2c mux.
271 */
272 sc->sc_mux_data = sc->sc_config->get_mux_info(sc);
273 if (sc->sc_mux_data == NULL) {
274 aprint_error_dev(sc->sc_dev, "unable to get info for mux\n");
275 return;
276 }
277
278 /*
279 * Do configuration method (OF, ACPI) specific setup.
280 */
281 switch (devhandle_type(devhandle)) {
282 #if defined(I2CMUX_USE_FDT)
283 case DEVHANDLE_TYPE_OF:
284 iicmux_attach_fdt(sc);
285 break;
286 #endif /* I2CMUX_USE_FDT */
287
288 #if defined(I2CMUX_USE_ACPI)
289 case DEVHANDLE_TYPE_ACPI:
290 iicmux_attach_acpi(sc);
291 break;
292 #endif /* I2CMUX_USE_ACPI */
293
294 default:
295 aprint_error_dev(sc->sc_dev, "could not configure mux: "
296 "handle type %d not supported\n",
297 devhandle_type(devhandle));
298 break;
299 }
300 }
301