2 * SPDX-License-Identifier: BSD-2-Clause
4 * Copyright (c) 2019 Ian Lepore <ian@freebsd.org>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * Driver for i2c bus muxes controlled by one or more gpio pins.
31 * This driver has #ifdef FDT sections in it, as if it supports both fdt and
32 * hinted attachment, but there is currently no support for hinted attachment.
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
38 #include "opt_platform.h"
40 #include <sys/param.h>
43 #include <sys/kernel.h>
44 #include <sys/module.h>
45 #include <sys/systm.h>
47 #include <dev/gpio/gpiobusvar.h>
49 #include <dev/iicbus/iicbus.h>
50 #include <dev/iicbus/mux/iicmux.h>
53 #include <dev/ofw/ofw_bus.h>
54 #include <dev/ofw/ofw_bus_subr.h>
55 #include <dev/ofw/openfirm.h>
57 static struct ofw_compat_data compat_data[] = {
58 {"i2c-mux-gpio", true},
61 OFWBUS_PNP_INFO(compat_data);
62 SIMPLEBUS_PNP_INFO(compat_data);
65 #include <dev/iicbus/iiconf.h>
67 #include "iicmux_if.h"
69 struct gpiomux_softc {
70 struct iicmux_softc mux;
73 gpio_pin_t pins[IICMUX_MAX_BUSES];
76 #define IDLE_NOOP (-1) /* When asked to idle the bus, do nothing. */
79 gpiomux_bus_select(device_t dev, int busidx, struct iic_reqbus_data *rd)
81 struct gpiomux_softc *sc = device_get_softc(dev);
85 * The iicmux caller ensures busidx is between 0 and the number of buses
86 * we passed to iicmux_init_softc(), no need for validation here. The
87 * bits in the index number are transcribed to the state of the pins,
88 * except when we're asked to idle the bus. In that case, we transcribe
89 * sc->idleidx to the pins, unless that is IDLE_NOOP (leave the current
90 * bus selected), in which case we just bail.
92 if (busidx == IICMUX_SELECT_IDLE) {
93 if (sc->idleidx == IDLE_NOOP)
98 for (i = 0; i < sc->numpins; ++i)
99 gpio_pin_set_active(sc->pins[i], busidx & (1u << i));
105 gpiomux_probe(device_t dev)
112 if (ofw_bus_status_okay(dev) &&
113 ofw_bus_search_compatible(dev, compat_data)->ocd_data)
114 rv = BUS_PROBE_DEFAULT;
117 device_set_desc(dev, "I2C GPIO Mux");
123 gpiomux_release_pins(struct gpiomux_softc *sc)
127 for (i = 0; i < sc->numpins; ++i)
128 gpio_pin_release(sc->pins[i]);
132 gpiomux_attach(device_t dev)
134 struct gpiomux_softc *sc = device_get_softc(dev);
137 int err, i, idlebits, numchannels;
141 node = ofw_bus_get_node(dev);
144 * Locate the gpio pin(s) that control the mux hardware. There can be
145 * multiple pins, but there must be at least one.
148 err = gpio_pin_get_by_ofw_propidx(dev, node, "mux-gpios", i,
155 if (sc->numpins == 0) {
156 device_printf(dev, "cannot acquire pins listed in mux-gpios\n");
161 numchannels = 1u << sc->numpins;
162 if (numchannels > IICMUX_MAX_BUSES) {
163 device_printf(dev, "too many mux-gpios pins for max %d buses\n",
170 * We don't have a parent/child relationship to the upstream bus, we
171 * have to locate it via the i2c-parent property. Explicitly tell the
172 * user which upstream we're associated with, since the normal attach
173 * message is going to mention only our actual parent.
175 len = OF_getencprop(node, "i2c-parent", &propval, sizeof(propval));
176 if (len != sizeof(propval)) {
177 device_printf(dev, "cannot obtain i2c-parent property\n");
181 busdev = OF_device_from_xref((phandle_t)propval);
182 if (busdev == NULL) {
184 "cannot find device referenced by i2c-parent property\n");
188 device_printf(dev, "upstream bus is %s\n", device_get_nameunit(busdev));
191 * If there is an idle-state property, that is the value we set the pins
192 * to when the bus is idle, otherwise idling the bus is a no-op
193 * (whichever bus was last accessed remains active).
195 len = OF_getencprop(node, "idle-state", &propval, sizeof(propval));
196 if (len == sizeof(propval)) {
197 if ((int)propval >= numchannels) {
199 "idle-state property %d exceeds channel count\n",
202 sc->idleidx = (int)propval;
203 idlebits = sc->idleidx;
205 sc->idleidx = IDLE_NOOP;
209 /* Preset the mux to the idle state to get things started. */
210 for (i = 0; i < sc->numpins; ++i) {
211 gpio_pin_setflags(sc->pins[i], GPIO_PIN_OUTPUT);
212 gpio_pin_set_active(sc->pins[i], idlebits & (1u << i));
215 /* Init the core driver, have it add our child downstream buses. */
216 if ((err = iicmux_attach(dev, busdev, numchannels)) == 0)
217 bus_generic_attach(dev);
222 gpiomux_release_pins(sc);
228 gpiomux_detach(device_t dev)
230 struct gpiomux_softc *sc = device_get_softc(dev);
233 if ((err = iicmux_detach(dev)) != 0)
236 gpiomux_release_pins(sc);
241 static device_method_t gpiomux_methods[] = {
243 DEVMETHOD(device_probe, gpiomux_probe),
244 DEVMETHOD(device_attach, gpiomux_attach),
245 DEVMETHOD(device_detach, gpiomux_detach),
248 DEVMETHOD(iicmux_bus_select, gpiomux_bus_select),
253 static devclass_t gpiomux_devclass;
255 DEFINE_CLASS_1(iic_gpiomux, iic_gpiomux_driver, gpiomux_methods,
256 sizeof(struct gpiomux_softc), iicmux_driver);
257 DRIVER_MODULE(iic_gpiomux, simplebus, iic_gpiomux_driver, gpiomux_devclass, 0, 0);
258 DRIVER_MODULE(iic_gpiomux, ofw_simplebus, iic_gpiomux_driver, gpiomux_devclass, 0, 0);
261 DRIVER_MODULE(ofw_iicbus, iic_gpiomux, ofw_iicbus_driver, ofw_iicbus_devclass, 0, 0);
263 DRIVER_MODULE(iicbus, iic_gpiomux, iicbus_driver, iicbus_devclass, 0, 0);
266 MODULE_DEPEND(iic_gpiomux, iicmux, 1, 1, 1);
267 MODULE_DEPEND(iic_gpiomux, iicbus, 1, 1, 1);