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