Home | History | Annotate | Line # | Download | only in i2c
i2cmux.c revision 1.6.2.1
      1  1.6.2.1   thorpej /*	$NetBSD: i2cmux.c,v 1.6.2.1 2021/08/09 00:30:09 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.6.2.1   thorpej __KERNEL_RCSID(0, "$NetBSD: i2cmux.c,v 1.6.2.1 2021/08/09 00:30:09 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.6.2.1   thorpej #include "locators.h"
     44      1.3  jmcneill 
     45      1.2  jmcneill /*
     46      1.2  jmcneill  * i2c mux
     47      1.2  jmcneill  *
     48      1.1   thorpej  * This works by interposing a set of virtual controllers behind the real
     49      1.1   thorpej  * i2c controller.  We provide our own acquire and release functions that
     50      1.1   thorpej  * perform the following tasks:
     51      1.2  jmcneill  *
     52      1.1   thorpej  *	acquire -> acquire parent controller, program mux
     53      1.2  jmcneill  *
     54      1.1   thorpej  *	release -> idle mux, release parent controller
     55      1.2  jmcneill  *
     56      1.1   thorpej  * All of the actual I/O operations are transparently passed through.
     57      1.2  jmcneill  *
     58      1.2  jmcneill  * 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.2  jmcneill  * 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.2  jmcneill  *
     63      1.1   thorpej  *	mux virtual controller -> parent controller
     64      1.1   thorpej  *
     65      1.1   thorpej  * These are common routines used by various i2c mux controller
     66      1.1   thorpej  * implementations (gpio, pin mux, i2c device, etc.).
     67      1.2  jmcneill  */
     68      1.1   thorpej 
     69      1.1   thorpej /*****************************************************************************/
     70      1.1   thorpej 
     71      1.1   thorpej static int
     72      1.1   thorpej iicmux_acquire_bus(void * const v, int const flags)
     73      1.1   thorpej {
     74      1.1   thorpej 	struct iicmux_bus * const bus = v;
     75      1.1   thorpej 	struct iicmux_softc * const sc = bus->mux;
     76      1.1   thorpej 	int error;
     77      1.1   thorpej 
     78      1.1   thorpej 	error = iic_acquire_bus(sc->sc_i2c_parent, flags);
     79      1.1   thorpej 	if (error) {
     80      1.1   thorpej 		return error;
     81      1.1   thorpej 	}
     82      1.1   thorpej 
     83      1.1   thorpej 	error = sc->sc_config->acquire_bus(bus, flags);
     84      1.1   thorpej 	if (error) {
     85      1.1   thorpej 		iic_release_bus(sc->sc_i2c_parent, flags);
     86      1.1   thorpej 	}
     87      1.1   thorpej 
     88      1.1   thorpej 	return error;
     89      1.1   thorpej }
     90      1.1   thorpej 
     91      1.1   thorpej static void
     92      1.1   thorpej iicmux_release_bus(void * const v, int const flags)
     93      1.1   thorpej {
     94      1.1   thorpej 	struct iicmux_bus * const bus = v;
     95      1.1   thorpej 	struct iicmux_softc * const sc = bus->mux;
     96      1.1   thorpej 
     97      1.1   thorpej 	sc->sc_config->release_bus(bus, flags);
     98      1.1   thorpej 	iic_release_bus(sc->sc_i2c_parent, flags);
     99      1.1   thorpej }
    100      1.1   thorpej 
    101      1.1   thorpej static int
    102      1.1   thorpej iicmux_exec(void * const v, i2c_op_t const op, i2c_addr_t const addr,
    103      1.1   thorpej     const void * const cmdbuf, size_t const cmdlen, void * const databuf,
    104      1.1   thorpej     size_t const datalen, int const flags)
    105      1.1   thorpej {
    106      1.1   thorpej 	struct iicmux_bus * const bus = v;
    107      1.1   thorpej 	struct iicmux_softc * const sc = bus->mux;
    108      1.1   thorpej 
    109      1.1   thorpej 	return iic_exec(sc->sc_i2c_parent, op, addr, cmdbuf, cmdlen,
    110      1.1   thorpej 			databuf, datalen, flags);
    111      1.1   thorpej }
    112      1.1   thorpej 
    113      1.1   thorpej /*****************************************************************************/
    114      1.1   thorpej 
    115      1.2  jmcneill static void
    116  1.6.2.1   thorpej iicmux_attach_bus(struct iicmux_softc * const sc, devhandle_t devhandle,
    117  1.6.2.1   thorpej     int const busidx)
    118      1.1   thorpej {
    119      1.1   thorpej 	struct iicmux_bus * const bus = &sc->sc_busses[busidx];
    120      1.1   thorpej 
    121      1.1   thorpej 	bus->mux = sc;
    122      1.1   thorpej 	bus->busidx = busidx;
    123  1.6.2.1   thorpej 	bus->devhandle = devhandle;
    124      1.1   thorpej 
    125      1.1   thorpej 	bus->bus_data = sc->sc_config->get_bus_info(bus);
    126      1.1   thorpej 	if (bus->bus_data == NULL) {
    127      1.1   thorpej 		aprint_error_dev(sc->sc_dev,
    128      1.1   thorpej 		    "unable to get info for bus %d\n", busidx);
    129      1.1   thorpej 		return;
    130      1.1   thorpej 	}
    131      1.1   thorpej 
    132      1.1   thorpej 	iic_tag_init(&bus->controller);
    133      1.1   thorpej 	bus->controller.ic_cookie = bus;
    134  1.6.2.1   thorpej 	bus->controller.ic_channel = bus->busidx;
    135      1.1   thorpej 	bus->controller.ic_acquire_bus = iicmux_acquire_bus;
    136      1.1   thorpej 	bus->controller.ic_release_bus = iicmux_release_bus;
    137      1.1   thorpej 	bus->controller.ic_exec = iicmux_exec;
    138      1.1   thorpej 
    139  1.6.2.1   thorpej #if defined(I2CMUX_USE_FDT)
    140  1.6.2.1   thorpej 	if (devhandle_type(devhandle) == DEVHANDLE_TYPE_OF) {
    141      1.3  jmcneill 		fdtbus_register_i2c_controller(&bus->controller,
    142  1.6.2.1   thorpej 		    devhandle_to_of(devhandle));
    143      1.3  jmcneill 	}
    144  1.6.2.1   thorpej #endif /* I2CMUX_USE_FDT */
    145  1.6.2.1   thorpej 
    146  1.6.2.1   thorpej 	struct i2cbus_attach_args iba = {
    147  1.6.2.1   thorpej 		.iba_tag = &bus->controller,
    148  1.6.2.1   thorpej 	};
    149  1.6.2.1   thorpej 
    150  1.6.2.1   thorpej 	int locs[I2CBUSCF_NLOCS];
    151  1.6.2.1   thorpej 	locs[I2CBUSCF_BUS] = bus->busidx;
    152  1.6.2.1   thorpej 
    153  1.6.2.1   thorpej 	config_found(sc->sc_dev, &iba, iicbus_print_multi,
    154  1.6.2.1   thorpej 	    CFARGS(.subatch = config_stdsubmatch,
    155  1.6.2.1   thorpej 		   .locators = locs,
    156  1.6.2.1   thorpej 		   .devhandle = devhandle));
    157      1.3  jmcneill }
    158      1.3  jmcneill 
    159  1.6.2.1   thorpej static bool
    160  1.6.2.1   thorpej iicmux_count_busses_callback(device_t self, devhandle_t devhandle,
    161  1.6.2.1   thorpej     void *v __unused)
    162      1.3  jmcneill {
    163  1.6.2.1   thorpej 	struct iicmux_softc *sc = device_private(self);
    164      1.3  jmcneill 
    165  1.6.2.1   thorpej #if defined(I2CMUX_USE_FDT)
    166  1.6.2.1   thorpej 	if (devhandle_type(devhandle) == DEVHANDLE_TYPE_OF) {
    167  1.6.2.1   thorpej 		char name[32];
    168  1.6.2.1   thorpej 
    169  1.6.2.1   thorpej 		if (OF_getprop(devhandle_to_of(devhandle), "name", name,
    170  1.6.2.1   thorpej 			       sizeof(name)) <= 0) {
    171  1.6.2.1   thorpej 			/* Skip this DT node (shouldn't happen). */
    172  1.6.2.1   thorpej 			return true;	/* keep enumerating */
    173  1.6.2.1   thorpej 		}
    174  1.6.2.1   thorpej 		if (strcmp(name, "i2c-mux") == 0) {
    175  1.6.2.1   thorpej 			/*
    176  1.6.2.1   thorpej 			 * This DT node is the actual mux node; reset the
    177  1.6.2.1   thorpej 			 * our devhandle and restart enumeration.
    178  1.6.2.1   thorpej 			 */
    179  1.6.2.1   thorpej 			device_set_handle(self, devhandle);
    180  1.6.2.1   thorpej 			sc->sc_nbusses = -1;
    181  1.6.2.1   thorpej 			return false;	/* stop enumerating */
    182  1.6.2.1   thorpej 		}
    183      1.3  jmcneill 	}
    184  1.6.2.1   thorpej #endif /* I2CMUX_USE_FDT */
    185      1.3  jmcneill 
    186  1.6.2.1   thorpej 	sc->sc_nbusses++;
    187  1.6.2.1   thorpej 	return true;			/* keep enumerating */
    188      1.3  jmcneill }
    189      1.3  jmcneill 
    190  1.6.2.1   thorpej static bool
    191  1.6.2.1   thorpej iicmux_attach_busses_callback(device_t self, devhandle_t devhandle, void *v)
    192      1.3  jmcneill {
    193  1.6.2.1   thorpej 	struct iicmux_softc *sc = device_private(self);
    194  1.6.2.1   thorpej 	int * const idxp = v;
    195      1.3  jmcneill 
    196  1.6.2.1   thorpej 	KASSERT(*idxp < sc->sc_nbusses);
    197  1.6.2.1   thorpej 	iicmux_attach_bus(sc, devhandle, (*idxp)++);
    198      1.1   thorpej 
    199  1.6.2.1   thorpej 	return true;			/* keep enumerating */
    200      1.1   thorpej }
    201      1.1   thorpej 
    202      1.1   thorpej void
    203      1.1   thorpej iicmux_attach(struct iicmux_softc * const sc)
    204      1.1   thorpej {
    205  1.6.2.1   thorpej 	int error, idx;
    206  1.6.2.1   thorpej 
    207      1.1   thorpej 	/*
    208  1.6.2.1   thorpej 	 * We expect sc->sc_config and sc->sc_i2c_parent to be initialized
    209  1.6.2.1   thorpej 	 * by the front-end.
    210      1.1   thorpej 	 */
    211      1.1   thorpej 	KASSERT(sc->sc_config != NULL);
    212      1.1   thorpej 	KASSERT(sc->sc_i2c_parent != NULL);
    213      1.1   thorpej 
    214      1.1   thorpej 	/*
    215      1.1   thorpej 	 * Gather up all of the various bits of information needed
    216      1.1   thorpej 	 * for this particular type of i2c mux.
    217      1.1   thorpej 	 */
    218      1.1   thorpej 	sc->sc_mux_data = sc->sc_config->get_mux_info(sc);
    219      1.1   thorpej 	if (sc->sc_mux_data == NULL) {
    220      1.1   thorpej 		aprint_error_dev(sc->sc_dev, "unable to get info for mux\n");
    221      1.1   thorpej 		return;
    222      1.1   thorpej 	}
    223      1.1   thorpej 
    224      1.3  jmcneill 	/*
    225  1.6.2.1   thorpej 	 * Count the number of busses behind this mux.
    226      1.3  jmcneill 	 */
    227  1.6.2.1   thorpej  count_again:
    228  1.6.2.1   thorpej 	error = device_enumerate_children(sc->sc_dev,
    229  1.6.2.1   thorpej 	    iicmux_count_busses_callback, NULL);
    230  1.6.2.1   thorpej 	if (error) {
    231  1.6.2.1   thorpej 		aprint_error_dev(sc->sc_dev,
    232  1.6.2.1   thorpej 		    "unable to enumerate busses to count, error = %d\n", error);
    233  1.6.2.1   thorpej 		return;
    234  1.6.2.1   thorpej 	}
    235  1.6.2.1   thorpej 	if (sc->sc_nbusses == -1) {
    236  1.6.2.1   thorpej 		/*
    237  1.6.2.1   thorpej 		 * We had to reset our devhandle to a different device
    238  1.6.2.1   thorpej 		 * tree node.  We need to try counting again.
    239  1.6.2.1   thorpej 		 */
    240  1.6.2.1   thorpej 		sc->sc_nbusses = 0;
    241  1.6.2.1   thorpej 		goto count_again;
    242  1.6.2.1   thorpej 	}
    243  1.6.2.1   thorpej 	if (sc->sc_nbusses == 0) {
    244  1.6.2.1   thorpej 		/* No busses; no more work to do. */
    245  1.6.2.1   thorpej 		return;
    246  1.6.2.1   thorpej 	}
    247  1.6.2.1   thorpej 
    248  1.6.2.1   thorpej 	sc->sc_busses = kmem_zalloc(sizeof(*sc->sc_busses) * sc->sc_nbusses,
    249  1.6.2.1   thorpej 	    KM_SLEEP);
    250  1.6.2.1   thorpej 
    251  1.6.2.1   thorpej 	/* Now attach them. */
    252  1.6.2.1   thorpej 	idx = 0;
    253  1.6.2.1   thorpej 	error = device_enumerate_children(sc->sc_dev,
    254  1.6.2.1   thorpej 	    iicmux_attach_busses_callback, &idx);
    255  1.6.2.1   thorpej 	if (error) {
    256  1.6.2.1   thorpej 		aprint_error_dev(sc->sc_dev,
    257  1.6.2.1   thorpej 		    "unable to enumerate busses to attach, error = %d\n",
    258  1.6.2.1   thorpej 		    error);
    259      1.1   thorpej 	}
    260      1.1   thorpej }
    261