Home | History | Annotate | Line # | Download | only in i2c
i2cmux.c revision 1.5.2.1
      1  1.5.2.1   thorpej /*	$NetBSD: i2cmux.c,v 1.5.2.1 2021/05/08 02:44:22 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.2  jmcneill  *
     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.2  jmcneill  * POSSIBILITY OF SUCH DAMAGE.
     30      1.2  jmcneill  */
     31      1.1   thorpej 
     32      1.1   thorpej #include <sys/cdefs.h>
     33  1.5.2.1   thorpej __KERNEL_RCSID(0, "$NetBSD: i2cmux.c,v 1.5.2.1 2021/05/08 02:44:22 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.2  jmcneill #include <sys/kmem.h>
     39      1.1   thorpej 
     40      1.1   thorpej #include <dev/i2c/i2cvar.h>
     41      1.1   thorpej #include <dev/i2c/i2cmuxvar.h>
     42      1.1   thorpej 
     43      1.2  jmcneill /*
     44      1.2  jmcneill  * i2c mux
     45      1.2  jmcneill  *
     46      1.1   thorpej  * This works by interposing a set of virtual controllers behind the real
     47      1.1   thorpej  * i2c controller.  We provide our own acquire and release functions that
     48      1.1   thorpej  * perform the following tasks:
     49      1.2  jmcneill  *
     50      1.1   thorpej  *	acquire -> acquire parent controller, program mux
     51      1.2  jmcneill  *
     52      1.1   thorpej  *	release -> idle mux, release parent controller
     53      1.2  jmcneill  *
     54      1.1   thorpej  * All of the actual I/O operations are transparently passed through.
     55      1.2  jmcneill  *
     56      1.2  jmcneill  * N.B. the locking order; the generic I2C layer has already acquired
     57      1.1   thorpej  * our virtual controller's mutex before calling our acquire function,
     58      1.2  jmcneill  * and we will then acquire the real controller's mutex when we acquire
     59      1.1   thorpej  * the bus, so the order is:
     60      1.2  jmcneill  *
     61      1.1   thorpej  *	mux virtual controller -> parent controller
     62      1.1   thorpej  *
     63      1.1   thorpej  * These are common routines used by various i2c mux controller
     64      1.1   thorpej  * implementations (gpio, pin mux, i2c device, etc.).
     65      1.2  jmcneill  */
     66      1.1   thorpej 
     67      1.1   thorpej /*****************************************************************************/
     68      1.1   thorpej 
     69      1.1   thorpej static int
     70      1.1   thorpej iicmux_acquire_bus(void * const v, int const flags)
     71      1.1   thorpej {
     72      1.1   thorpej 	struct iicmux_bus * const bus = v;
     73      1.1   thorpej 	struct iicmux_softc * const sc = bus->mux;
     74      1.1   thorpej 	int error;
     75      1.1   thorpej 
     76      1.1   thorpej 	error = iic_acquire_bus(sc->sc_i2c_parent, flags);
     77      1.1   thorpej 	if (error) {
     78      1.1   thorpej 		return error;
     79      1.1   thorpej 	}
     80      1.1   thorpej 
     81      1.1   thorpej 	error = sc->sc_config->acquire_bus(bus, flags);
     82      1.1   thorpej 	if (error) {
     83      1.1   thorpej 		iic_release_bus(sc->sc_i2c_parent, flags);
     84      1.1   thorpej 	}
     85      1.1   thorpej 
     86      1.1   thorpej 	return error;
     87      1.1   thorpej }
     88      1.1   thorpej 
     89      1.1   thorpej static void
     90      1.1   thorpej iicmux_release_bus(void * const v, int const flags)
     91      1.1   thorpej {
     92      1.1   thorpej 	struct iicmux_bus * const bus = v;
     93      1.1   thorpej 	struct iicmux_softc * const sc = bus->mux;
     94      1.1   thorpej 
     95      1.1   thorpej 	sc->sc_config->release_bus(bus, flags);
     96      1.1   thorpej 	iic_release_bus(sc->sc_i2c_parent, flags);
     97      1.1   thorpej }
     98      1.1   thorpej 
     99      1.1   thorpej static int
    100      1.1   thorpej iicmux_exec(void * const v, i2c_op_t const op, i2c_addr_t const addr,
    101      1.1   thorpej     const void * const cmdbuf, size_t const cmdlen, void * const databuf,
    102      1.1   thorpej     size_t const datalen, int const flags)
    103      1.1   thorpej {
    104      1.1   thorpej 	struct iicmux_bus * const bus = v;
    105      1.1   thorpej 	struct iicmux_softc * const sc = bus->mux;
    106      1.1   thorpej 
    107      1.1   thorpej 	return iic_exec(sc->sc_i2c_parent, op, addr, cmdbuf, cmdlen,
    108      1.1   thorpej 			databuf, datalen, flags);
    109      1.1   thorpej }
    110      1.1   thorpej 
    111      1.1   thorpej /*****************************************************************************/
    112      1.1   thorpej 
    113      1.1   thorpej /* XXX iicbus_print() should be able to do this. */
    114      1.1   thorpej static int
    115      1.1   thorpej iicmux_print(void * const aux, const char * const pnp)
    116      1.2  jmcneill {
    117      1.1   thorpej 	i2c_tag_t const tag = aux;
    118      1.1   thorpej 	struct iicmux_bus * const bus = tag->ic_cookie;
    119      1.1   thorpej 	int rv;
    120      1.1   thorpej 
    121      1.1   thorpej 	rv = iicbus_print(aux, pnp);
    122      1.1   thorpej 	aprint_normal(" bus %d", bus->busidx);
    123      1.1   thorpej 
    124      1.1   thorpej 	return rv;
    125      1.1   thorpej }
    126      1.1   thorpej 
    127      1.2  jmcneill static void
    128  1.5.2.1   thorpej iicmux_attach_bus(struct iicmux_softc * const sc, devhandle_t devhandle,
    129  1.5.2.1   thorpej     int const busidx)
    130      1.1   thorpej {
    131      1.1   thorpej 	struct iicmux_bus * const bus = &sc->sc_busses[busidx];
    132      1.1   thorpej 
    133      1.1   thorpej 	bus->mux = sc;
    134      1.1   thorpej 	bus->busidx = busidx;
    135  1.5.2.1   thorpej 	bus->devhandle = devhandle;
    136      1.1   thorpej 
    137      1.1   thorpej 	bus->bus_data = sc->sc_config->get_bus_info(bus);
    138      1.1   thorpej 	if (bus->bus_data == NULL) {
    139      1.1   thorpej 		aprint_error_dev(sc->sc_dev,
    140      1.1   thorpej 		    "unable to get info for bus %d\n", busidx);
    141      1.1   thorpej 		return;
    142      1.1   thorpej 	}
    143      1.1   thorpej 
    144      1.1   thorpej 	iic_tag_init(&bus->controller);
    145      1.1   thorpej 	bus->controller.ic_cookie = bus;
    146      1.1   thorpej 	bus->controller.ic_acquire_bus = iicmux_acquire_bus;
    147      1.1   thorpej 	bus->controller.ic_release_bus = iicmux_release_bus;
    148      1.1   thorpej 	bus->controller.ic_exec = iicmux_exec;
    149      1.1   thorpej 
    150  1.5.2.1   thorpej 	switch (devhandle_type(devhandle)) {
    151  1.5.2.1   thorpej #if defined(I2CMUX_USE_FDT)
    152  1.5.2.1   thorpej 	case DEVHANDLE_TYPE_OF:
    153      1.3  jmcneill 		fdtbus_register_i2c_controller(&bus->controller,
    154  1.5.2.1   thorpej 		    devhandle_to_of(devhandle));
    155      1.3  jmcneill 
    156  1.5.2.1   thorpej 		fdtbus_attach_i2cbus(sc->sc_dev, devhandle_to_of(devhandle),
    157      1.3  jmcneill 		    &bus->controller, iicmux_print);
    158      1.3  jmcneill 		break;
    159  1.5.2.1   thorpej #endif /* I2CMUX_USE_FDT */
    160  1.5.2.1   thorpej 
    161  1.5.2.1   thorpej 	case DEVHANDLE_TYPE_INVALID:
    162  1.5.2.1   thorpej 		aprint_error_dev(sc->sc_dev, "invalid bus device handle\n");
    163  1.5.2.1   thorpej 		return;
    164  1.5.2.1   thorpej 
    165  1.5.2.1   thorpej 	default: {
    166      1.3  jmcneill 		struct i2cbus_attach_args iba = {
    167      1.3  jmcneill 			.iba_tag = &bus->controller,
    168      1.3  jmcneill 		};
    169  1.5.2.1   thorpej 		config_found(sc->sc_dev, &iba, iicmux_print,
    170  1.5.2.1   thorpej 		    CFARG_DEVHANDLE, devhandle,
    171  1.5.2.1   thorpej 		    CFARG_EOL);
    172      1.3  jmcneill 		break;
    173  1.5.2.1   thorpej 	    }
    174      1.3  jmcneill 	}
    175      1.3  jmcneill }
    176      1.3  jmcneill 
    177  1.5.2.1   thorpej #if defined(I2CMUX_USE_FDT)
    178  1.5.2.1   thorpej static int
    179  1.5.2.1   thorpej iicmux_fdt_count_children(struct iicmux_softc * const sc)
    180  1.5.2.1   thorpej {
    181  1.5.2.1   thorpej 	int phandle = devhandle_to_of(sc->sc_i2c_mux_devhandle);
    182  1.5.2.1   thorpej 	char name[32];
    183  1.5.2.1   thorpej 	int child, count;
    184  1.5.2.1   thorpej 
    185  1.5.2.1   thorpej  restart:
    186  1.5.2.1   thorpej 	for (child = OF_child(phandle), count = 0; child;
    187  1.5.2.1   thorpej 	     child = OF_peer(child)) {
    188  1.5.2.1   thorpej 		if (OF_getprop(child, "name", name, sizeof(name)) <= 0) {
    189  1.5.2.1   thorpej 			continue;
    190  1.5.2.1   thorpej 		}
    191  1.5.2.1   thorpej 		if (strcmp(name, "i2c-mux") == 0) {
    192  1.5.2.1   thorpej 			phandle = child;
    193  1.5.2.1   thorpej 			goto restart;
    194  1.5.2.1   thorpej 		}
    195  1.5.2.1   thorpej 		count++;
    196  1.5.2.1   thorpej 	}
    197  1.5.2.1   thorpej 
    198  1.5.2.1   thorpej 	/* phandle may have changed. */
    199  1.5.2.1   thorpej 	sc->sc_i2c_mux_devhandle = devhandle_from_of(phandle);
    200  1.5.2.1   thorpej 	return count;
    201  1.5.2.1   thorpej }
    202  1.5.2.1   thorpej 
    203      1.3  jmcneill static void
    204      1.3  jmcneill iicmux_attach_fdt(struct iicmux_softc * const sc)
    205      1.3  jmcneill {
    206  1.5.2.1   thorpej 	int phandle = devhandle_to_of(sc->sc_i2c_mux_devhandle);
    207      1.3  jmcneill 
    208  1.5.2.1   thorpej 	sc->sc_nbusses = iicmux_fdt_count_children(sc);
    209      1.3  jmcneill 	if (sc->sc_nbusses == 0) {
    210      1.3  jmcneill 		return;
    211      1.3  jmcneill 	}
    212      1.3  jmcneill 
    213  1.5.2.1   thorpej 	/* sc_i2c_mux_devhandle may have changed. */
    214  1.5.2.1   thorpej 	phandle = devhandle_to_of(sc->sc_i2c_mux_devhandle);
    215  1.5.2.1   thorpej 
    216      1.3  jmcneill 	sc->sc_busses = kmem_zalloc(sizeof(*sc->sc_busses) * sc->sc_nbusses,
    217      1.3  jmcneill 	    KM_SLEEP);
    218      1.3  jmcneill 
    219      1.3  jmcneill 	int child, idx;
    220  1.5.2.1   thorpej 	for (child = OF_child(phandle), idx = 0; child;
    221      1.3  jmcneill 	     child = OF_peer(child), idx++) {
    222      1.3  jmcneill 		KASSERT(idx < sc->sc_nbusses);
    223  1.5.2.1   thorpej 		iicmux_attach_bus(sc, devhandle_from_of(child), idx);
    224      1.3  jmcneill 	}
    225      1.3  jmcneill }
    226  1.5.2.1   thorpej #endif /* I2CMUX_USE_FDT */
    227      1.3  jmcneill 
    228  1.5.2.1   thorpej #if defined(I2CMUX_USE_ACPI)
    229      1.3  jmcneill static void
    230      1.3  jmcneill iicmux_attach_acpi(struct iicmux_softc * const sc)
    231      1.3  jmcneill {
    232  1.5.2.1   thorpej 	ACPI_HANDLE hdl = devhandle_to_acpi(sc->sc_i2c_mux_devhandle);
    233      1.3  jmcneill 	struct acpi_devnode *devnode, *ad;
    234      1.3  jmcneill 	int idx;
    235      1.3  jmcneill 
    236      1.3  jmcneill 	devnode = acpi_match_node(hdl);
    237      1.3  jmcneill 	KASSERT(devnode != NULL);
    238      1.3  jmcneill 
    239      1.3  jmcneill 	/* Count child busses */
    240      1.3  jmcneill 	sc->sc_nbusses = 0;
    241      1.3  jmcneill 	SIMPLEQ_FOREACH(ad, &devnode->ad_child_head, ad_child_list) {
    242      1.3  jmcneill 		if (ad->ad_devinfo->Type != ACPI_TYPE_DEVICE ||
    243      1.3  jmcneill 		    !acpi_device_present(ad->ad_handle)) {
    244      1.3  jmcneill 			continue;
    245      1.3  jmcneill 		}
    246      1.3  jmcneill 		sc->sc_nbusses++;
    247      1.3  jmcneill 	}
    248      1.3  jmcneill 
    249      1.3  jmcneill 	sc->sc_busses = kmem_zalloc(sizeof(*sc->sc_busses) * sc->sc_nbusses,
    250      1.3  jmcneill 	    KM_SLEEP);
    251      1.1   thorpej 
    252      1.3  jmcneill 	/* Attach child busses */
    253      1.3  jmcneill 	idx = 0;
    254      1.3  jmcneill 	SIMPLEQ_FOREACH(ad, &devnode->ad_child_head, ad_child_list) {
    255      1.3  jmcneill 		if (ad->ad_devinfo->Type != ACPI_TYPE_DEVICE ||
    256      1.3  jmcneill 		    !acpi_device_present(ad->ad_handle)) {
    257      1.3  jmcneill 			continue;
    258      1.3  jmcneill 		}
    259  1.5.2.1   thorpej 		iicmux_attach_bus(sc, devhandle_from_acpi(ad->ad_handle), idx);
    260      1.3  jmcneill 		idx++;
    261      1.3  jmcneill 	}
    262      1.1   thorpej }
    263  1.5.2.1   thorpej #endif /* I2CMUX_USE_ACPI */
    264      1.1   thorpej 
    265      1.1   thorpej void
    266      1.1   thorpej iicmux_attach(struct iicmux_softc * const sc)
    267      1.1   thorpej {
    268  1.5.2.1   thorpej 	devhandle_t devhandle = device_handle(sc->sc_dev);
    269  1.5.2.1   thorpej 
    270      1.1   thorpej 	/*
    271  1.5.2.1   thorpej 	 * We expect sc->sc_config and sc->sc_i2c_parent to be initialized
    272  1.5.2.1   thorpej 	 * by the front-end.
    273      1.1   thorpej 	 */
    274      1.1   thorpej 	KASSERT(sc->sc_config != NULL);
    275      1.1   thorpej 	KASSERT(sc->sc_i2c_parent != NULL);
    276      1.1   thorpej 
    277      1.1   thorpej 	/*
    278  1.5.2.1   thorpej 	 * We start out assuming that the i2c bus nodes are children of
    279  1.5.2.1   thorpej 	 * our own node.  We'll adjust later if we encounter a subnode
    280  1.5.2.1   thorpej 	 * while enumerating our child busses that itself is defined as
    281  1.5.2.1   thorpej 	 * the mux node according to our device tree bindings.
    282  1.5.2.1   thorpej 	 */
    283  1.5.2.1   thorpej 	sc->sc_i2c_mux_devhandle = devhandle;
    284  1.5.2.1   thorpej 
    285  1.5.2.1   thorpej 	/*
    286      1.1   thorpej 	 * Gather up all of the various bits of information needed
    287      1.1   thorpej 	 * for this particular type of i2c mux.
    288      1.1   thorpej 	 */
    289      1.1   thorpej 	sc->sc_mux_data = sc->sc_config->get_mux_info(sc);
    290      1.1   thorpej 	if (sc->sc_mux_data == NULL) {
    291      1.1   thorpej 		aprint_error_dev(sc->sc_dev, "unable to get info for mux\n");
    292      1.1   thorpej 		return;
    293      1.1   thorpej 	}
    294      1.1   thorpej 
    295      1.3  jmcneill 	/*
    296      1.3  jmcneill 	 * Do configuration method (OF, ACPI) specific setup.
    297      1.3  jmcneill 	 */
    298  1.5.2.1   thorpej 	switch (devhandle_type(devhandle)) {
    299  1.5.2.1   thorpej #if defined(I2CMUX_USE_FDT)
    300  1.5.2.1   thorpej 	case DEVHANDLE_TYPE_OF:
    301      1.3  jmcneill 		iicmux_attach_fdt(sc);
    302      1.3  jmcneill 		break;
    303  1.5.2.1   thorpej #endif /* I2CMUX_USE_FDT */
    304  1.5.2.1   thorpej 
    305  1.5.2.1   thorpej #if defined(I2CMUX_USE_ACPI)
    306  1.5.2.1   thorpej 	case DEVHANDLE_TYPE_ACPI:
    307      1.3  jmcneill 		iicmux_attach_acpi(sc);
    308      1.3  jmcneill 		break;
    309  1.5.2.1   thorpej #endif /* I2CMUX_USE_ACPI */
    310  1.5.2.1   thorpej 
    311      1.3  jmcneill 	default:
    312      1.3  jmcneill 		aprint_error_dev(sc->sc_dev, "could not configure mux: "
    313  1.5.2.1   thorpej 		    "handle type %d not supported\n",
    314  1.5.2.1   thorpej 		    devhandle_type(devhandle));
    315      1.3  jmcneill 		break;
    316      1.1   thorpej 	}
    317      1.1   thorpej }
    318