Home | History | Annotate | Line # | Download | only in fdt
i2cmux_fdt.c revision 1.1
      1 /*	$NetBSD: i2cmux_fdt.c,v 1.1 2020/12/23 04:09:32 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.1 2020/12/23 04:09:32 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 	const struct iicmux_config *	sc_config;
     98 	i2c_tag_t			sc_i2c_parent;
     99 	struct iicmux_bus *		sc_busses;
    100 	int				sc_nbusses;
    101 
    102 	union {
    103 		struct {
    104 			struct fdtbus_gpio_pin **pins;
    105 			int npins;
    106 			uint32_t idle_value;
    107 			bool has_idle_value;
    108 		} sc_gpio;
    109 
    110 		struct {
    111 			u_int idle_idx;
    112 			bool has_idle_idx;
    113 		} sc_pinctrl;
    114 	};
    115 };
    116 
    117 /*****************************************************************************/
    118 
    119 static int
    120 iicmux_acquire_bus(void * const v, int const flags)
    121 {
    122 	struct iicmux_bus * const bus = v;
    123 	struct iicmux_softc * const sc = bus->mux;
    124 	int error;
    125 
    126 	error = iic_acquire_bus(sc->sc_i2c_parent, flags);
    127 	if (error) {
    128 		return error;
    129 	}
    130 
    131 	error = sc->sc_config->acquire_bus(bus, flags);
    132 	if (error) {
    133 		iic_release_bus(sc->sc_i2c_parent, flags);
    134 	}
    135 
    136 	return error;
    137 }
    138 
    139 static void
    140 iicmux_release_bus(void * const v, int const flags)
    141 {
    142 	struct iicmux_bus * const bus = v;
    143 	struct iicmux_softc * const sc = bus->mux;
    144 
    145 	sc->sc_config->release_bus(bus, flags);
    146 	iic_release_bus(sc->sc_i2c_parent, flags);
    147 }
    148 
    149 static int
    150 iicmux_exec(void * const v, i2c_op_t const op, i2c_addr_t const addr,
    151     const void * const cmdbuf, size_t const cmdlen, void * const databuf,
    152     size_t const datalen, int const flags)
    153 {
    154 	struct iicmux_bus * const bus = v;
    155 	struct iicmux_softc * const sc = bus->mux;
    156 
    157 	return iic_exec(sc->sc_i2c_parent, op, addr, cmdbuf, cmdlen,
    158 			databuf, datalen, flags);
    159 }
    160 
    161 /*****************************************************************************/
    162 
    163 static int
    164 iicmux_gpio_get_mux_info(struct iicmux_softc * const sc)
    165 {
    166 	int i;
    167 
    168 	sc->sc_gpio.npins = fdtbus_gpio_count(sc->sc_phandle, "mux-gpios");
    169 	if (sc->sc_gpio.npins == 0) {
    170 		aprint_error_dev(sc->sc_dev,
    171 		    "unable to get mux-gpios property\n");
    172 		return ENXIO;
    173 	}
    174 
    175 	sc->sc_gpio.pins =
    176 	    kmem_zalloc(sizeof(*sc->sc_gpio.pins) * sc->sc_gpio.npins,
    177 	    		       KM_SLEEP);
    178 	for (i = 0; i < sc->sc_gpio.npins; i++) {
    179 		sc->sc_gpio.pins[i] = fdtbus_gpio_acquire_index(sc->sc_phandle,
    180 		    "mux-gpios", i, GPIO_PIN_OUTPUT);
    181 		if (sc->sc_gpio.pins[i] == NULL) {
    182 			aprint_error_dev(sc->sc_dev,
    183 			    "unable to acquire gpio #%d\n", i);
    184 			return ENXIO;
    185 		}
    186 	}
    187 
    188 	sc->sc_gpio.has_idle_value =
    189 	    of_getprop_uint32(sc->sc_phandle, "idle-state",
    190 			      &sc->sc_gpio.idle_value) == 0;
    191 
    192 	return 0;
    193 }
    194 
    195 static int
    196 iicmux_gpio_get_bus_info(struct iicmux_bus * const bus)
    197 {
    198 	struct iicmux_softc * const sc = bus->mux;
    199 	int error;
    200 
    201 	error = fdtbus_get_reg(bus->phandle, 0, &bus->gpio.value, NULL);
    202 	if (error) {
    203 		aprint_error_dev(sc->sc_dev,
    204 		    "unable to get reg property for bus %d\n", bus->busidx);
    205 	}
    206 
    207 	return error;
    208 }
    209 
    210 static void
    211 iicmux_gpio_set_value(struct iicmux_softc * const sc, bus_addr_t value)
    212 {
    213 	int i;
    214 
    215 	for (i = 0; i < sc->sc_gpio.npins; i++, value >>= 1) {
    216 		fdtbus_gpio_write(sc->sc_gpio.pins[i], value & 1);
    217 	}
    218 }
    219 
    220 static int
    221 iicmux_gpio_acquire_bus(struct iicmux_bus * const bus, int const flags __unused)
    222 {
    223 	iicmux_gpio_set_value(bus->mux, bus->gpio.value);
    224 
    225 	return 0;
    226 }
    227 
    228 static void
    229 iicmux_gpio_release_bus(struct iicmux_bus * const bus, int const flags __unused)
    230 {
    231 	struct iicmux_softc *sc = bus->mux;
    232 
    233 	if (sc->sc_gpio.has_idle_value) {
    234 		iicmux_gpio_set_value(sc, sc->sc_gpio.idle_value);
    235 	}
    236 }
    237 
    238 static const struct iicmux_config iicmux_gpio_config = {
    239 	.desc = "GPIO",
    240 	.get_mux_info = iicmux_gpio_get_mux_info,
    241 	.get_bus_info = iicmux_gpio_get_bus_info,
    242 	.acquire_bus = iicmux_gpio_acquire_bus,
    243 	.release_bus = iicmux_gpio_release_bus,
    244 };
    245 
    246 /*****************************************************************************/
    247 
    248 static int
    249 iicmux_pinctrl_get_mux_info(struct iicmux_softc * const sc)
    250 {
    251 
    252 	sc->sc_pinctrl.has_idle_idx =
    253 	    fdtbus_get_index(sc->sc_phandle, "pinctrl-names", "idle",
    254 			     &sc->sc_pinctrl.idle_idx) == 0;
    255 
    256 	return 0;
    257 }
    258 
    259 static int
    260 iicmux_pinctrl_get_bus_info(struct iicmux_bus * const bus)
    261 {
    262 	struct iicmux_softc * const sc = bus->mux;
    263 	int error;
    264 
    265 	error = fdtbus_get_reg(bus->phandle, 0, &bus->pinctrl.idx,
    266 			       NULL);
    267 	if (error) {
    268 		aprint_error_dev(sc->sc_dev,
    269 		    "unable to get reg property for bus %d\n", bus->busidx);
    270 	}
    271 
    272 	return error;
    273 }
    274 
    275 static int
    276 iicmux_pinctrl_acquire_bus(struct iicmux_bus * const bus,
    277     int const flags __unused)
    278 {
    279 	struct iicmux_softc * const sc = bus->mux;
    280 
    281 	return fdtbus_pinctrl_set_config_index(sc->sc_phandle,
    282 	    bus->pinctrl.idx);
    283 }
    284 
    285 static void
    286 iicmux_pinctrl_release_bus(struct iicmux_bus * const bus,
    287     int const flags __unused)
    288 {
    289 	struct iicmux_softc * const sc = bus->mux;
    290 
    291 	if (sc->sc_pinctrl.has_idle_idx) {
    292 		(void) fdtbus_pinctrl_set_config_index(sc->sc_phandle,
    293 		    sc->sc_pinctrl.idle_idx);
    294 	}
    295 }
    296 
    297 static const struct iicmux_config iicmux_pinctrl_config = {
    298 	.desc = "PinMux",
    299 	.get_mux_info = iicmux_pinctrl_get_mux_info,
    300 	.get_bus_info = iicmux_pinctrl_get_bus_info,
    301 	.acquire_bus = iicmux_pinctrl_acquire_bus,
    302 	.release_bus = iicmux_pinctrl_release_bus,
    303 };
    304 
    305 /*****************************************************************************/
    306 
    307 static const struct of_compat_data compat_data[] = {
    308 	{ .compat = "i2c-mux-gpio",
    309 	  .data = (uintptr_t)&iicmux_gpio_config },
    310 
    311 	{ .compat = "i2c-mux-pinctrl",
    312 	  .data = (uintptr_t)&iicmux_pinctrl_config },
    313 
    314 	{ NULL }
    315 };
    316 
    317 static int
    318 iicmux_count_children(struct iicmux_softc * const sc)
    319 {
    320 	int child, count;
    321 
    322 	for (child = OF_child(sc->sc_phandle), count = 0; child;
    323 	     child = OF_peer(child)) {
    324 		count++;
    325 	}
    326 
    327 	return count;
    328 }
    329 
    330 /* XXX iicbus_print() should be able to do this. */
    331 static int
    332 iicmux_print(void * const aux, const char * const pnp)
    333 {
    334 	i2c_tag_t const tag = aux;
    335 	struct iicmux_bus * const bus = tag->ic_cookie;
    336 	int rv;
    337 
    338 	rv = iicbus_print(aux, pnp);
    339 	aprint_normal(" bus %d", bus->busidx);
    340 
    341 	return rv;
    342 }
    343 
    344 static void
    345 iicmux_attach_bus(struct iicmux_softc * const sc,
    346     int const phandle, int const busidx)
    347 {
    348 	struct iicmux_bus * const bus = &sc->sc_busses[busidx];
    349 	int error;
    350 
    351 	bus->mux = sc;
    352 	bus->busidx = busidx;
    353 	bus->phandle = phandle;
    354 
    355 	error = sc->sc_config->get_bus_info(bus);
    356 	if (error) {
    357 		aprint_error_dev(sc->sc_dev,
    358 		    "unable to get info for bus %d, error %d\n",
    359 		    busidx, error);
    360 		return;
    361 	}
    362 
    363 	iic_tag_init(&bus->controller);
    364 	bus->controller.ic_cookie = bus;
    365 	bus->controller.ic_acquire_bus = iicmux_acquire_bus;
    366 	bus->controller.ic_release_bus = iicmux_release_bus;
    367 	bus->controller.ic_exec = iicmux_exec;
    368 
    369 	fdtbus_attach_i2cbus(sc->sc_dev, bus->phandle, &bus->controller,
    370 	    iicmux_print);
    371 }
    372 
    373 static int
    374 iicmux_fdt_match(device_t const parent, cfdata_t const match, void * const aux)
    375 {
    376 	struct fdt_attach_args * const faa = aux;
    377 
    378 	return of_match_compat_data(faa->faa_phandle, compat_data);
    379 }
    380 
    381 static void
    382 iicmux_fdt_attach(device_t const parent, device_t const self, void * const aux)
    383 {
    384 	struct iicmux_softc * const sc = device_private(self);
    385 	struct fdt_attach_args * const faa = aux;
    386 
    387 	sc->sc_dev = self;
    388 	sc->sc_phandle = faa->faa_phandle;
    389 	sc->sc_config = (const struct iicmux_config *)
    390 	    of_search_compatible(sc->sc_phandle, compat_data)->data;
    391 
    392 	aprint_naive("\n");
    393 	aprint_normal(": %s I2C mux\n", sc->sc_config->desc);
    394 
    395 	sc->sc_i2c_parent = fdtbus_i2c_acquire(sc->sc_phandle, "i2c-parent");
    396 	if (sc->sc_i2c_parent == NULL) {
    397 		aprint_error_dev(sc->sc_dev, "unable to acquire i2c-parent\n");
    398 		return;
    399 	}
    400 
    401 	const int nchildren = iicmux_count_children(sc);
    402 	if (nchildren == 0) {
    403 		return;
    404 	}
    405 
    406 	sc->sc_busses = kmem_zalloc(sizeof(*sc->sc_busses) * nchildren,
    407 	    KM_SLEEP);
    408 
    409 	int child, idx;
    410 	for (child = OF_child(sc->sc_phandle), idx = 0; child;
    411 	     child = OF_peer(child), idx++) {
    412 		KASSERT(idx < nchildren);
    413 		iicmux_attach_bus(sc, child, idx);
    414 	}
    415 }
    416 
    417 CFATTACH_DECL_NEW(iicmux_fdt, sizeof(struct iicmux_softc),
    418     iicmux_fdt_match, iicmux_fdt_attach, NULL, NULL);
    419