i2cmux_fdt.c revision 1.3 1 /* $NetBSD: i2cmux_fdt.c,v 1.3 2020/12/28 15:08:06 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_fdt.c,v 1.3 2020/12/28 15:08:06 thorpej Exp $");
34
35 #include <sys/types.h>
36 #include <sys/device.h>
37 #include <sys/kernel.h>
38 #include <sys/kmem.h>
39 #include <sys/bus.h>
40 #include <sys/gpio.h>
41
42 #include <dev/fdt/fdtvar.h>
43 #include <dev/i2c/i2cvar.h>
44
45 /*
46 * i2c mux
47 *
48 * This works by interposing a virtual controller in front of the
49 * real i2c controller. We provide our own acquire release functions
50 * that 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
66 struct iicmux_softc;
67 struct iicmux_bus;
68
69 struct iicmux_config {
70 const char *desc;
71 int (*get_mux_info)(struct iicmux_softc *);
72 int (*get_bus_info)(struct iicmux_bus *);
73 int (*acquire_bus)(struct iicmux_bus *, int);
74 void (*release_bus)(struct iicmux_bus *, int);
75 };
76
77 struct iicmux_bus {
78 struct i2c_controller controller;
79 struct iicmux_softc *mux;
80 int phandle;
81 int busidx;
82
83 union {
84 struct {
85 bus_addr_t value;
86 } gpio;
87
88 struct {
89 bus_addr_t idx;
90 } pinctrl;
91 };
92 };
93
94 struct iicmux_softc {
95 device_t sc_dev;
96 int sc_phandle;
97 int sc_i2c_mux_phandle;
98 const struct iicmux_config * sc_config;
99 i2c_tag_t sc_i2c_parent;
100 struct iicmux_bus * sc_busses;
101 int sc_nbusses;
102
103 union {
104 struct {
105 struct fdtbus_gpio_pin **pins;
106 int npins;
107 uint32_t idle_value;
108 bool has_idle_value;
109 } sc_gpio;
110
111 struct {
112 u_int idle_idx;
113 bool has_idle_idx;
114 } sc_pinctrl;
115 };
116 };
117
118 /*****************************************************************************/
119
120 static int
121 iicmux_acquire_bus(void * const v, int const flags)
122 {
123 struct iicmux_bus * const bus = v;
124 struct iicmux_softc * const sc = bus->mux;
125 int error;
126
127 error = iic_acquire_bus(sc->sc_i2c_parent, flags);
128 if (error) {
129 return error;
130 }
131
132 error = sc->sc_config->acquire_bus(bus, flags);
133 if (error) {
134 iic_release_bus(sc->sc_i2c_parent, flags);
135 }
136
137 return error;
138 }
139
140 static void
141 iicmux_release_bus(void * const v, int const flags)
142 {
143 struct iicmux_bus * const bus = v;
144 struct iicmux_softc * const sc = bus->mux;
145
146 sc->sc_config->release_bus(bus, flags);
147 iic_release_bus(sc->sc_i2c_parent, flags);
148 }
149
150 static int
151 iicmux_exec(void * const v, i2c_op_t const op, i2c_addr_t const addr,
152 const void * const cmdbuf, size_t const cmdlen, void * const databuf,
153 size_t const datalen, int const flags)
154 {
155 struct iicmux_bus * const bus = v;
156 struct iicmux_softc * const sc = bus->mux;
157
158 return iic_exec(sc->sc_i2c_parent, op, addr, cmdbuf, cmdlen,
159 databuf, datalen, flags);
160 }
161
162 /*****************************************************************************/
163
164 static int
165 iicmux_gpio_get_mux_info(struct iicmux_softc * const sc)
166 {
167 int i;
168
169 sc->sc_gpio.npins = fdtbus_gpio_count(sc->sc_phandle, "mux-gpios");
170 if (sc->sc_gpio.npins == 0) {
171 aprint_error_dev(sc->sc_dev,
172 "unable to get mux-gpios property\n");
173 return ENXIO;
174 }
175
176 sc->sc_gpio.pins =
177 kmem_zalloc(sizeof(*sc->sc_gpio.pins) * sc->sc_gpio.npins,
178 KM_SLEEP);
179 for (i = 0; i < sc->sc_gpio.npins; i++) {
180 sc->sc_gpio.pins[i] = fdtbus_gpio_acquire_index(sc->sc_phandle,
181 "mux-gpios", i, GPIO_PIN_OUTPUT);
182 if (sc->sc_gpio.pins[i] == NULL) {
183 aprint_error_dev(sc->sc_dev,
184 "unable to acquire gpio #%d\n", i);
185 return ENXIO;
186 }
187 }
188
189 sc->sc_gpio.has_idle_value =
190 of_getprop_uint32(sc->sc_phandle, "idle-state",
191 &sc->sc_gpio.idle_value) == 0;
192
193 return 0;
194 }
195
196 static int
197 iicmux_gpio_get_bus_info(struct iicmux_bus * const bus)
198 {
199 struct iicmux_softc * const sc = bus->mux;
200 int error;
201
202 error = fdtbus_get_reg(bus->phandle, 0, &bus->gpio.value, NULL);
203 if (error) {
204 aprint_error_dev(sc->sc_dev,
205 "unable to get reg property for bus %d\n", bus->busidx);
206 }
207
208 return error;
209 }
210
211 static void
212 iicmux_gpio_set_value(struct iicmux_softc * const sc, bus_addr_t value)
213 {
214 int i;
215
216 for (i = 0; i < sc->sc_gpio.npins; i++, value >>= 1) {
217 fdtbus_gpio_write(sc->sc_gpio.pins[i], value & 1);
218 }
219 }
220
221 static int
222 iicmux_gpio_acquire_bus(struct iicmux_bus * const bus, int const flags __unused)
223 {
224 iicmux_gpio_set_value(bus->mux, bus->gpio.value);
225
226 return 0;
227 }
228
229 static void
230 iicmux_gpio_release_bus(struct iicmux_bus * const bus, int const flags __unused)
231 {
232 struct iicmux_softc *sc = bus->mux;
233
234 if (sc->sc_gpio.has_idle_value) {
235 iicmux_gpio_set_value(sc, sc->sc_gpio.idle_value);
236 }
237 }
238
239 static const struct iicmux_config iicmux_gpio_config = {
240 .desc = "GPIO",
241 .get_mux_info = iicmux_gpio_get_mux_info,
242 .get_bus_info = iicmux_gpio_get_bus_info,
243 .acquire_bus = iicmux_gpio_acquire_bus,
244 .release_bus = iicmux_gpio_release_bus,
245 };
246
247 /*****************************************************************************/
248
249 static int
250 iicmux_pinctrl_get_mux_info(struct iicmux_softc * const sc)
251 {
252
253 sc->sc_pinctrl.has_idle_idx =
254 fdtbus_get_index(sc->sc_phandle, "pinctrl-names", "idle",
255 &sc->sc_pinctrl.idle_idx) == 0;
256
257 return 0;
258 }
259
260 static int
261 iicmux_pinctrl_get_bus_info(struct iicmux_bus * const bus)
262 {
263 struct iicmux_softc * const sc = bus->mux;
264 int error;
265
266 error = fdtbus_get_reg(bus->phandle, 0, &bus->pinctrl.idx,
267 NULL);
268 if (error) {
269 aprint_error_dev(sc->sc_dev,
270 "unable to get reg property for bus %d\n", bus->busidx);
271 }
272
273 return error;
274 }
275
276 static int
277 iicmux_pinctrl_acquire_bus(struct iicmux_bus * const bus,
278 int const flags __unused)
279 {
280 struct iicmux_softc * const sc = bus->mux;
281
282 return fdtbus_pinctrl_set_config_index(sc->sc_phandle,
283 bus->pinctrl.idx);
284 }
285
286 static void
287 iicmux_pinctrl_release_bus(struct iicmux_bus * const bus,
288 int const flags __unused)
289 {
290 struct iicmux_softc * const sc = bus->mux;
291
292 if (sc->sc_pinctrl.has_idle_idx) {
293 (void) fdtbus_pinctrl_set_config_index(sc->sc_phandle,
294 sc->sc_pinctrl.idle_idx);
295 }
296 }
297
298 static const struct iicmux_config iicmux_pinctrl_config = {
299 .desc = "PinMux",
300 .get_mux_info = iicmux_pinctrl_get_mux_info,
301 .get_bus_info = iicmux_pinctrl_get_bus_info,
302 .acquire_bus = iicmux_pinctrl_acquire_bus,
303 .release_bus = iicmux_pinctrl_release_bus,
304 };
305
306 /*****************************************************************************/
307
308 static const struct of_compat_data compat_data[] = {
309 { .compat = "i2c-mux-gpio",
310 .data = (uintptr_t)&iicmux_gpio_config },
311
312 { .compat = "i2c-mux-pinctrl",
313 .data = (uintptr_t)&iicmux_pinctrl_config },
314
315 { NULL }
316 };
317
318 static int
319 iicmux_count_children(struct iicmux_softc * const sc)
320 {
321 char name[32];
322 int child, count;
323
324 restart:
325 for (child = OF_child(sc->sc_i2c_mux_phandle), count = 0; child;
326 child = OF_peer(child)) {
327 if (OF_getprop(child, "name", name, sizeof(name)) <= 0) {
328 continue;
329 }
330 if (strcmp(name, "i2c-mux") == 0) {
331 /*
332 * The node we encountered is the acutal parent
333 * of the i2c bus children. Stash its phandle
334 * and restart the enumeration.
335 */
336 sc->sc_i2c_mux_phandle = child;
337 goto restart;
338 }
339 count++;
340 }
341
342 return count;
343 }
344
345 /* XXX iicbus_print() should be able to do this. */
346 static int
347 iicmux_print(void * const aux, const char * const pnp)
348 {
349 i2c_tag_t const tag = aux;
350 struct iicmux_bus * const bus = tag->ic_cookie;
351 int rv;
352
353 rv = iicbus_print(aux, pnp);
354 aprint_normal(" bus %d", bus->busidx);
355
356 return rv;
357 }
358
359 static void
360 iicmux_attach_bus(struct iicmux_softc * const sc,
361 int const phandle, int const busidx)
362 {
363 struct iicmux_bus * const bus = &sc->sc_busses[busidx];
364 int error;
365
366 bus->mux = sc;
367 bus->busidx = busidx;
368 bus->phandle = phandle;
369
370 error = sc->sc_config->get_bus_info(bus);
371 if (error) {
372 aprint_error_dev(sc->sc_dev,
373 "unable to get info for bus %d, error %d\n",
374 busidx, error);
375 return;
376 }
377
378 iic_tag_init(&bus->controller);
379 bus->controller.ic_cookie = bus;
380 bus->controller.ic_acquire_bus = iicmux_acquire_bus;
381 bus->controller.ic_release_bus = iicmux_release_bus;
382 bus->controller.ic_exec = iicmux_exec;
383
384 fdtbus_register_i2c_controller(&bus->controller, bus->phandle);
385
386 fdtbus_attach_i2cbus(sc->sc_dev, bus->phandle, &bus->controller,
387 iicmux_print);
388 }
389
390 static int
391 iicmux_fdt_match(device_t const parent, cfdata_t const match, void * const aux)
392 {
393 struct fdt_attach_args * const faa = aux;
394
395 return of_match_compat_data(faa->faa_phandle, compat_data);
396 }
397
398 static void
399 iicmux_fdt_attach(device_t const parent, device_t const self, void * const aux)
400 {
401 struct iicmux_softc * const sc = device_private(self);
402 struct fdt_attach_args * const faa = aux;
403
404 sc->sc_dev = self;
405 sc->sc_phandle = faa->faa_phandle;
406 sc->sc_config = (const struct iicmux_config *)
407 of_search_compatible(sc->sc_phandle, compat_data)->data;
408
409 aprint_naive("\n");
410 aprint_normal(": %s I2C mux\n", sc->sc_config->desc);
411
412 /*
413 * We start out assuming that the i2c bus nodes are children of
414 * our own node. We'll adjust later if we encounter an "i2c-mux"
415 * node when counting our children. If we encounter such a node,
416 * then it's that node that is the parent of the i2c bus children.
417 */
418 sc->sc_i2c_mux_phandle = sc->sc_phandle;
419
420 sc->sc_i2c_parent = fdtbus_i2c_acquire(sc->sc_phandle, "i2c-parent");
421 if (sc->sc_i2c_parent == NULL) {
422 aprint_error_dev(sc->sc_dev, "unable to acquire i2c-parent\n");
423 return;
424 }
425
426 const int nchildren = iicmux_count_children(sc);
427 if (nchildren == 0) {
428 return;
429 }
430
431 sc->sc_busses = kmem_zalloc(sizeof(*sc->sc_busses) * nchildren,
432 KM_SLEEP);
433
434 int child, idx;
435 for (child = OF_child(sc->sc_i2c_mux_phandle), idx = 0; child;
436 child = OF_peer(child), idx++) {
437 KASSERT(idx < nchildren);
438 iicmux_attach_bus(sc, child, idx);
439 }
440 }
441
442 CFATTACH_DECL_NEW(iicmux_fdt, sizeof(struct iicmux_softc),
443 iicmux_fdt_match, iicmux_fdt_attach, NULL, NULL);
444