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