i2cmux_fdt.c revision 1.1 1 1.1 thorpej /* $NetBSD: i2cmux_fdt.c,v 1.1 2020/12/23 04:09:32 thorpej Exp $ */
2 1.1 thorpej
3 1.1 thorpej /*-
4 1.1 thorpej * Copyright (c) 2020 The NetBSD Foundation, Inc.
5 1.1 thorpej * All rights reserved.
6 1.1 thorpej *
7 1.1 thorpej * This code is derived from software contributed to The NetBSD Foundation
8 1.1 thorpej * by Jason R. Thorpe.
9 1.1 thorpej *
10 1.1 thorpej * Redistribution and use in source and binary forms, with or without
11 1.1 thorpej * modification, are permitted provided that the following conditions
12 1.1 thorpej * are met:
13 1.1 thorpej * 1. Redistributions of source code must retain the above copyright
14 1.1 thorpej * notice, this list of conditions and the following disclaimer.
15 1.1 thorpej * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 thorpej * notice, this list of conditions and the following disclaimer in the
17 1.1 thorpej * documentation and/or other materials provided with the distribution.
18 1.1 thorpej *
19 1.1 thorpej * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.1 thorpej * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.1 thorpej * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.1 thorpej * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.1 thorpej * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.1 thorpej * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.1 thorpej * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.1 thorpej * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.1 thorpej * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.1 thorpej * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.1 thorpej * POSSIBILITY OF SUCH DAMAGE.
30 1.1 thorpej */
31 1.1 thorpej
32 1.1 thorpej #include <sys/cdefs.h>
33 1.1 thorpej __KERNEL_RCSID(0, "$NetBSD: i2cmux_fdt.c,v 1.1 2020/12/23 04:09:32 thorpej Exp $");
34 1.1 thorpej
35 1.1 thorpej #include <sys/types.h>
36 1.1 thorpej #include <sys/device.h>
37 1.1 thorpej #include <sys/kernel.h>
38 1.1 thorpej #include <sys/kmem.h>
39 1.1 thorpej #include <sys/bus.h>
40 1.1 thorpej #include <sys/gpio.h>
41 1.1 thorpej
42 1.1 thorpej #include <dev/fdt/fdtvar.h>
43 1.1 thorpej #include <dev/i2c/i2cvar.h>
44 1.1 thorpej
45 1.1 thorpej /*
46 1.1 thorpej * i2c mux
47 1.1 thorpej *
48 1.1 thorpej * This works by interposing a virtual controller in front of the
49 1.1 thorpej * real i2c controller. We provide our own acquire release functions
50 1.1 thorpej * that perform the following tasks:
51 1.1 thorpej *
52 1.1 thorpej * acquire -> acquire parent controller, program mux
53 1.1 thorpej *
54 1.1 thorpej * release -> idle mux, release parent controller
55 1.1 thorpej *
56 1.1 thorpej * All of the actual I/O operations are transparently passed through.
57 1.1 thorpej *
58 1.1 thorpej * N.B. the locking order; the generic I2C layer has already acquired
59 1.1 thorpej * our virtual controller's mutex before calling our acquire function,
60 1.1 thorpej * and we will then acquire the real controller's mutex when we acquire
61 1.1 thorpej * the bus, so the order is:
62 1.1 thorpej *
63 1.1 thorpej * mux virtual controller -> parent controller
64 1.1 thorpej */
65 1.1 thorpej
66 1.1 thorpej struct iicmux_softc;
67 1.1 thorpej struct iicmux_bus;
68 1.1 thorpej
69 1.1 thorpej struct iicmux_config {
70 1.1 thorpej const char *desc;
71 1.1 thorpej int (*get_mux_info)(struct iicmux_softc *);
72 1.1 thorpej int (*get_bus_info)(struct iicmux_bus *);
73 1.1 thorpej int (*acquire_bus)(struct iicmux_bus *, int);
74 1.1 thorpej void (*release_bus)(struct iicmux_bus *, int);
75 1.1 thorpej };
76 1.1 thorpej
77 1.1 thorpej struct iicmux_bus {
78 1.1 thorpej struct i2c_controller controller;
79 1.1 thorpej struct iicmux_softc *mux;
80 1.1 thorpej int phandle;
81 1.1 thorpej int busidx;
82 1.1 thorpej
83 1.1 thorpej union {
84 1.1 thorpej struct {
85 1.1 thorpej bus_addr_t value;
86 1.1 thorpej } gpio;
87 1.1 thorpej
88 1.1 thorpej struct {
89 1.1 thorpej bus_addr_t idx;
90 1.1 thorpej } pinctrl;
91 1.1 thorpej };
92 1.1 thorpej };
93 1.1 thorpej
94 1.1 thorpej struct iicmux_softc {
95 1.1 thorpej device_t sc_dev;
96 1.1 thorpej int sc_phandle;
97 1.1 thorpej const struct iicmux_config * sc_config;
98 1.1 thorpej i2c_tag_t sc_i2c_parent;
99 1.1 thorpej struct iicmux_bus * sc_busses;
100 1.1 thorpej int sc_nbusses;
101 1.1 thorpej
102 1.1 thorpej union {
103 1.1 thorpej struct {
104 1.1 thorpej struct fdtbus_gpio_pin **pins;
105 1.1 thorpej int npins;
106 1.1 thorpej uint32_t idle_value;
107 1.1 thorpej bool has_idle_value;
108 1.1 thorpej } sc_gpio;
109 1.1 thorpej
110 1.1 thorpej struct {
111 1.1 thorpej u_int idle_idx;
112 1.1 thorpej bool has_idle_idx;
113 1.1 thorpej } sc_pinctrl;
114 1.1 thorpej };
115 1.1 thorpej };
116 1.1 thorpej
117 1.1 thorpej /*****************************************************************************/
118 1.1 thorpej
119 1.1 thorpej static int
120 1.1 thorpej iicmux_acquire_bus(void * const v, int const flags)
121 1.1 thorpej {
122 1.1 thorpej struct iicmux_bus * const bus = v;
123 1.1 thorpej struct iicmux_softc * const sc = bus->mux;
124 1.1 thorpej int error;
125 1.1 thorpej
126 1.1 thorpej error = iic_acquire_bus(sc->sc_i2c_parent, flags);
127 1.1 thorpej if (error) {
128 1.1 thorpej return error;
129 1.1 thorpej }
130 1.1 thorpej
131 1.1 thorpej error = sc->sc_config->acquire_bus(bus, flags);
132 1.1 thorpej if (error) {
133 1.1 thorpej iic_release_bus(sc->sc_i2c_parent, flags);
134 1.1 thorpej }
135 1.1 thorpej
136 1.1 thorpej return error;
137 1.1 thorpej }
138 1.1 thorpej
139 1.1 thorpej static void
140 1.1 thorpej iicmux_release_bus(void * const v, int const flags)
141 1.1 thorpej {
142 1.1 thorpej struct iicmux_bus * const bus = v;
143 1.1 thorpej struct iicmux_softc * const sc = bus->mux;
144 1.1 thorpej
145 1.1 thorpej sc->sc_config->release_bus(bus, flags);
146 1.1 thorpej iic_release_bus(sc->sc_i2c_parent, flags);
147 1.1 thorpej }
148 1.1 thorpej
149 1.1 thorpej static int
150 1.1 thorpej iicmux_exec(void * const v, i2c_op_t const op, i2c_addr_t const addr,
151 1.1 thorpej const void * const cmdbuf, size_t const cmdlen, void * const databuf,
152 1.1 thorpej size_t const datalen, int const flags)
153 1.1 thorpej {
154 1.1 thorpej struct iicmux_bus * const bus = v;
155 1.1 thorpej struct iicmux_softc * const sc = bus->mux;
156 1.1 thorpej
157 1.1 thorpej return iic_exec(sc->sc_i2c_parent, op, addr, cmdbuf, cmdlen,
158 1.1 thorpej databuf, datalen, flags);
159 1.1 thorpej }
160 1.1 thorpej
161 1.1 thorpej /*****************************************************************************/
162 1.1 thorpej
163 1.1 thorpej static int
164 1.1 thorpej iicmux_gpio_get_mux_info(struct iicmux_softc * const sc)
165 1.1 thorpej {
166 1.1 thorpej int i;
167 1.1 thorpej
168 1.1 thorpej sc->sc_gpio.npins = fdtbus_gpio_count(sc->sc_phandle, "mux-gpios");
169 1.1 thorpej if (sc->sc_gpio.npins == 0) {
170 1.1 thorpej aprint_error_dev(sc->sc_dev,
171 1.1 thorpej "unable to get mux-gpios property\n");
172 1.1 thorpej return ENXIO;
173 1.1 thorpej }
174 1.1 thorpej
175 1.1 thorpej sc->sc_gpio.pins =
176 1.1 thorpej kmem_zalloc(sizeof(*sc->sc_gpio.pins) * sc->sc_gpio.npins,
177 1.1 thorpej KM_SLEEP);
178 1.1 thorpej for (i = 0; i < sc->sc_gpio.npins; i++) {
179 1.1 thorpej sc->sc_gpio.pins[i] = fdtbus_gpio_acquire_index(sc->sc_phandle,
180 1.1 thorpej "mux-gpios", i, GPIO_PIN_OUTPUT);
181 1.1 thorpej if (sc->sc_gpio.pins[i] == NULL) {
182 1.1 thorpej aprint_error_dev(sc->sc_dev,
183 1.1 thorpej "unable to acquire gpio #%d\n", i);
184 1.1 thorpej return ENXIO;
185 1.1 thorpej }
186 1.1 thorpej }
187 1.1 thorpej
188 1.1 thorpej sc->sc_gpio.has_idle_value =
189 1.1 thorpej of_getprop_uint32(sc->sc_phandle, "idle-state",
190 1.1 thorpej &sc->sc_gpio.idle_value) == 0;
191 1.1 thorpej
192 1.1 thorpej return 0;
193 1.1 thorpej }
194 1.1 thorpej
195 1.1 thorpej static int
196 1.1 thorpej iicmux_gpio_get_bus_info(struct iicmux_bus * const bus)
197 1.1 thorpej {
198 1.1 thorpej struct iicmux_softc * const sc = bus->mux;
199 1.1 thorpej int error;
200 1.1 thorpej
201 1.1 thorpej error = fdtbus_get_reg(bus->phandle, 0, &bus->gpio.value, NULL);
202 1.1 thorpej if (error) {
203 1.1 thorpej aprint_error_dev(sc->sc_dev,
204 1.1 thorpej "unable to get reg property for bus %d\n", bus->busidx);
205 1.1 thorpej }
206 1.1 thorpej
207 1.1 thorpej return error;
208 1.1 thorpej }
209 1.1 thorpej
210 1.1 thorpej static void
211 1.1 thorpej iicmux_gpio_set_value(struct iicmux_softc * const sc, bus_addr_t value)
212 1.1 thorpej {
213 1.1 thorpej int i;
214 1.1 thorpej
215 1.1 thorpej for (i = 0; i < sc->sc_gpio.npins; i++, value >>= 1) {
216 1.1 thorpej fdtbus_gpio_write(sc->sc_gpio.pins[i], value & 1);
217 1.1 thorpej }
218 1.1 thorpej }
219 1.1 thorpej
220 1.1 thorpej static int
221 1.1 thorpej iicmux_gpio_acquire_bus(struct iicmux_bus * const bus, int const flags __unused)
222 1.1 thorpej {
223 1.1 thorpej iicmux_gpio_set_value(bus->mux, bus->gpio.value);
224 1.1 thorpej
225 1.1 thorpej return 0;
226 1.1 thorpej }
227 1.1 thorpej
228 1.1 thorpej static void
229 1.1 thorpej iicmux_gpio_release_bus(struct iicmux_bus * const bus, int const flags __unused)
230 1.1 thorpej {
231 1.1 thorpej struct iicmux_softc *sc = bus->mux;
232 1.1 thorpej
233 1.1 thorpej if (sc->sc_gpio.has_idle_value) {
234 1.1 thorpej iicmux_gpio_set_value(sc, sc->sc_gpio.idle_value);
235 1.1 thorpej }
236 1.1 thorpej }
237 1.1 thorpej
238 1.1 thorpej static const struct iicmux_config iicmux_gpio_config = {
239 1.1 thorpej .desc = "GPIO",
240 1.1 thorpej .get_mux_info = iicmux_gpio_get_mux_info,
241 1.1 thorpej .get_bus_info = iicmux_gpio_get_bus_info,
242 1.1 thorpej .acquire_bus = iicmux_gpio_acquire_bus,
243 1.1 thorpej .release_bus = iicmux_gpio_release_bus,
244 1.1 thorpej };
245 1.1 thorpej
246 1.1 thorpej /*****************************************************************************/
247 1.1 thorpej
248 1.1 thorpej static int
249 1.1 thorpej iicmux_pinctrl_get_mux_info(struct iicmux_softc * const sc)
250 1.1 thorpej {
251 1.1 thorpej
252 1.1 thorpej sc->sc_pinctrl.has_idle_idx =
253 1.1 thorpej fdtbus_get_index(sc->sc_phandle, "pinctrl-names", "idle",
254 1.1 thorpej &sc->sc_pinctrl.idle_idx) == 0;
255 1.1 thorpej
256 1.1 thorpej return 0;
257 1.1 thorpej }
258 1.1 thorpej
259 1.1 thorpej static int
260 1.1 thorpej iicmux_pinctrl_get_bus_info(struct iicmux_bus * const bus)
261 1.1 thorpej {
262 1.1 thorpej struct iicmux_softc * const sc = bus->mux;
263 1.1 thorpej int error;
264 1.1 thorpej
265 1.1 thorpej error = fdtbus_get_reg(bus->phandle, 0, &bus->pinctrl.idx,
266 1.1 thorpej NULL);
267 1.1 thorpej if (error) {
268 1.1 thorpej aprint_error_dev(sc->sc_dev,
269 1.1 thorpej "unable to get reg property for bus %d\n", bus->busidx);
270 1.1 thorpej }
271 1.1 thorpej
272 1.1 thorpej return error;
273 1.1 thorpej }
274 1.1 thorpej
275 1.1 thorpej static int
276 1.1 thorpej iicmux_pinctrl_acquire_bus(struct iicmux_bus * const bus,
277 1.1 thorpej int const flags __unused)
278 1.1 thorpej {
279 1.1 thorpej struct iicmux_softc * const sc = bus->mux;
280 1.1 thorpej
281 1.1 thorpej return fdtbus_pinctrl_set_config_index(sc->sc_phandle,
282 1.1 thorpej bus->pinctrl.idx);
283 1.1 thorpej }
284 1.1 thorpej
285 1.1 thorpej static void
286 1.1 thorpej iicmux_pinctrl_release_bus(struct iicmux_bus * const bus,
287 1.1 thorpej int const flags __unused)
288 1.1 thorpej {
289 1.1 thorpej struct iicmux_softc * const sc = bus->mux;
290 1.1 thorpej
291 1.1 thorpej if (sc->sc_pinctrl.has_idle_idx) {
292 1.1 thorpej (void) fdtbus_pinctrl_set_config_index(sc->sc_phandle,
293 1.1 thorpej sc->sc_pinctrl.idle_idx);
294 1.1 thorpej }
295 1.1 thorpej }
296 1.1 thorpej
297 1.1 thorpej static const struct iicmux_config iicmux_pinctrl_config = {
298 1.1 thorpej .desc = "PinMux",
299 1.1 thorpej .get_mux_info = iicmux_pinctrl_get_mux_info,
300 1.1 thorpej .get_bus_info = iicmux_pinctrl_get_bus_info,
301 1.1 thorpej .acquire_bus = iicmux_pinctrl_acquire_bus,
302 1.1 thorpej .release_bus = iicmux_pinctrl_release_bus,
303 1.1 thorpej };
304 1.1 thorpej
305 1.1 thorpej /*****************************************************************************/
306 1.1 thorpej
307 1.1 thorpej static const struct of_compat_data compat_data[] = {
308 1.1 thorpej { .compat = "i2c-mux-gpio",
309 1.1 thorpej .data = (uintptr_t)&iicmux_gpio_config },
310 1.1 thorpej
311 1.1 thorpej { .compat = "i2c-mux-pinctrl",
312 1.1 thorpej .data = (uintptr_t)&iicmux_pinctrl_config },
313 1.1 thorpej
314 1.1 thorpej { NULL }
315 1.1 thorpej };
316 1.1 thorpej
317 1.1 thorpej static int
318 1.1 thorpej iicmux_count_children(struct iicmux_softc * const sc)
319 1.1 thorpej {
320 1.1 thorpej int child, count;
321 1.1 thorpej
322 1.1 thorpej for (child = OF_child(sc->sc_phandle), count = 0; child;
323 1.1 thorpej child = OF_peer(child)) {
324 1.1 thorpej count++;
325 1.1 thorpej }
326 1.1 thorpej
327 1.1 thorpej return count;
328 1.1 thorpej }
329 1.1 thorpej
330 1.1 thorpej /* XXX iicbus_print() should be able to do this. */
331 1.1 thorpej static int
332 1.1 thorpej iicmux_print(void * const aux, const char * const pnp)
333 1.1 thorpej {
334 1.1 thorpej i2c_tag_t const tag = aux;
335 1.1 thorpej struct iicmux_bus * const bus = tag->ic_cookie;
336 1.1 thorpej int rv;
337 1.1 thorpej
338 1.1 thorpej rv = iicbus_print(aux, pnp);
339 1.1 thorpej aprint_normal(" bus %d", bus->busidx);
340 1.1 thorpej
341 1.1 thorpej return rv;
342 1.1 thorpej }
343 1.1 thorpej
344 1.1 thorpej static void
345 1.1 thorpej iicmux_attach_bus(struct iicmux_softc * const sc,
346 1.1 thorpej int const phandle, int const busidx)
347 1.1 thorpej {
348 1.1 thorpej struct iicmux_bus * const bus = &sc->sc_busses[busidx];
349 1.1 thorpej int error;
350 1.1 thorpej
351 1.1 thorpej bus->mux = sc;
352 1.1 thorpej bus->busidx = busidx;
353 1.1 thorpej bus->phandle = phandle;
354 1.1 thorpej
355 1.1 thorpej error = sc->sc_config->get_bus_info(bus);
356 1.1 thorpej if (error) {
357 1.1 thorpej aprint_error_dev(sc->sc_dev,
358 1.1 thorpej "unable to get info for bus %d, error %d\n",
359 1.1 thorpej busidx, error);
360 1.1 thorpej return;
361 1.1 thorpej }
362 1.1 thorpej
363 1.1 thorpej iic_tag_init(&bus->controller);
364 1.1 thorpej bus->controller.ic_cookie = bus;
365 1.1 thorpej bus->controller.ic_acquire_bus = iicmux_acquire_bus;
366 1.1 thorpej bus->controller.ic_release_bus = iicmux_release_bus;
367 1.1 thorpej bus->controller.ic_exec = iicmux_exec;
368 1.1 thorpej
369 1.1 thorpej fdtbus_attach_i2cbus(sc->sc_dev, bus->phandle, &bus->controller,
370 1.1 thorpej iicmux_print);
371 1.1 thorpej }
372 1.1 thorpej
373 1.1 thorpej static int
374 1.1 thorpej iicmux_fdt_match(device_t const parent, cfdata_t const match, void * const aux)
375 1.1 thorpej {
376 1.1 thorpej struct fdt_attach_args * const faa = aux;
377 1.1 thorpej
378 1.1 thorpej return of_match_compat_data(faa->faa_phandle, compat_data);
379 1.1 thorpej }
380 1.1 thorpej
381 1.1 thorpej static void
382 1.1 thorpej iicmux_fdt_attach(device_t const parent, device_t const self, void * const aux)
383 1.1 thorpej {
384 1.1 thorpej struct iicmux_softc * const sc = device_private(self);
385 1.1 thorpej struct fdt_attach_args * const faa = aux;
386 1.1 thorpej
387 1.1 thorpej sc->sc_dev = self;
388 1.1 thorpej sc->sc_phandle = faa->faa_phandle;
389 1.1 thorpej sc->sc_config = (const struct iicmux_config *)
390 1.1 thorpej of_search_compatible(sc->sc_phandle, compat_data)->data;
391 1.1 thorpej
392 1.1 thorpej aprint_naive("\n");
393 1.1 thorpej aprint_normal(": %s I2C mux\n", sc->sc_config->desc);
394 1.1 thorpej
395 1.1 thorpej sc->sc_i2c_parent = fdtbus_i2c_acquire(sc->sc_phandle, "i2c-parent");
396 1.1 thorpej if (sc->sc_i2c_parent == NULL) {
397 1.1 thorpej aprint_error_dev(sc->sc_dev, "unable to acquire i2c-parent\n");
398 1.1 thorpej return;
399 1.1 thorpej }
400 1.1 thorpej
401 1.1 thorpej const int nchildren = iicmux_count_children(sc);
402 1.1 thorpej if (nchildren == 0) {
403 1.1 thorpej return;
404 1.1 thorpej }
405 1.1 thorpej
406 1.1 thorpej sc->sc_busses = kmem_zalloc(sizeof(*sc->sc_busses) * nchildren,
407 1.1 thorpej KM_SLEEP);
408 1.1 thorpej
409 1.1 thorpej int child, idx;
410 1.1 thorpej for (child = OF_child(sc->sc_phandle), idx = 0; child;
411 1.1 thorpej child = OF_peer(child), idx++) {
412 1.1 thorpej KASSERT(idx < nchildren);
413 1.1 thorpej iicmux_attach_bus(sc, child, idx);
414 1.1 thorpej }
415 1.1 thorpej }
416 1.1 thorpej
417 1.1 thorpej CFATTACH_DECL_NEW(iicmux_fdt, sizeof(struct iicmux_softc),
418 1.1 thorpej iicmux_fdt_match, iicmux_fdt_attach, NULL, NULL);
419