1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause 3 * 4 * Copyright (c) 2019 Ian Lepore <ian@freebsd.org> 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 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. 14 * 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 25 * SUCH DAMAGE. 26 */ 27 28 /* 29 * Driver for i2c bus muxes controlled by one or more gpio pins. 30 * 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. 33 */ 34 35 #include <sys/cdefs.h> 36 __FBSDID("$FreeBSD$"); 37 38 #include "opt_platform.h" 39 40 #include <sys/param.h> 41 #include <sys/bus.h> 42 #include <sys/gpio.h> 43 #include <sys/kernel.h> 44 #include <sys/module.h> 45 #include <sys/systm.h> 46 47 #include <dev/gpio/gpiobusvar.h> 48 49 #include <dev/iicbus/iicbus.h> 50 #include <dev/iicbus/mux/iicmux.h> 51 52 #ifdef FDT 53 #include <dev/ofw/ofw_bus.h> 54 #include <dev/ofw/ofw_bus_subr.h> 55 #include <dev/ofw/openfirm.h> 56 57 static struct ofw_compat_data compat_data[] = { 58 {"i2c-mux-gpio", true}, 59 {NULL, false} 60 }; 61 OFWBUS_PNP_INFO(compat_data); 62 SIMPLEBUS_PNP_INFO(compat_data); 63 #endif /* FDT */ 64 65 #include <dev/iicbus/iiconf.h> 66 #include "iicmux.h" 67 #include "iicmux_if.h" 68 69 struct gpiomux_softc { 70 struct iicmux_softc mux; 71 int idleidx; 72 int numpins; 73 gpio_pin_t pins[IICMUX_MAX_BUSES]; 74 }; 75 76 #define IDLE_NOOP (-1) /* When asked to idle the bus, do nothing. */ 77 78 static int 79 gpiomux_bus_select(device_t dev, int busidx, struct iic_reqbus_data *rd) 80 { 81 struct gpiomux_softc *sc = device_get_softc(dev); 82 int i; 83 84 /* 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. 91 */ 92 if (busidx == IICMUX_SELECT_IDLE) { 93 if (sc->idleidx == IDLE_NOOP) 94 return (0); 95 busidx = sc->idleidx; 96 } 97 98 for (i = 0; i < sc->numpins; ++i) 99 gpio_pin_set_active(sc->pins[i], busidx & (1u << i)); 100 101 return (0); 102 } 103 104 static int 105 gpiomux_probe(device_t dev) 106 { 107 int rv; 108 109 rv = ENXIO; 110 111 #ifdef FDT 112 if (ofw_bus_status_okay(dev) && 113 ofw_bus_search_compatible(dev, compat_data)->ocd_data) 114 rv = BUS_PROBE_DEFAULT; 115 #endif 116 117 device_set_desc(dev, "I2C GPIO Mux"); 118 119 return (rv); 120 } 121 122 static int 123 gpiomux_attach(device_t dev) 124 { 125 struct gpiomux_softc *sc = device_get_softc(dev); 126 ssize_t len; 127 device_t busdev; 128 int err, i, idlebits, numchannels; 129 pcell_t propval; 130 phandle_t node; 131 132 node = ofw_bus_get_node(dev); 133 134 /* 135 * Locate the gpio pin(s) that control the mux hardware. There can be 136 * multiple pins, but there must be at least one. 137 */ 138 for (i = 0; ; ++i) { 139 err = gpio_pin_get_by_ofw_propidx(dev, node, "mux-gpios", i, 140 &sc->pins[i]); 141 if (err != 0) { 142 break; 143 } 144 } 145 sc->numpins = i; 146 if (sc->numpins == 0) { 147 device_printf(dev, "cannot acquire pins listed in mux-gpios\n"); 148 return ((err == 0) ? ENXIO : err); 149 } 150 numchannels = 1u << sc->numpins; 151 if (numchannels > IICMUX_MAX_BUSES) { 152 device_printf(dev, "too many mux-gpios pins for max %d buses\n", 153 IICMUX_MAX_BUSES); 154 return (EINVAL); 155 } 156 157 /* 158 * We don't have a parent/child relationship to the upstream bus, we 159 * have to locate it via the i2c-parent property. Explicitly tell the 160 * user which upstream we're associated with, since the normal attach 161 * message is going to mention only our actual parent. 162 */ 163 len = OF_getencprop(node, "i2c-parent", &propval, sizeof(propval)); 164 if (len != sizeof(propval)) { 165 device_printf(dev, "cannot obtain i2c-parent property\n"); 166 return (ENXIO); 167 } 168 busdev = OF_device_from_xref((phandle_t)propval); 169 if (busdev == NULL) { 170 device_printf(dev, 171 "cannot find device referenced by i2c-parent property\n"); 172 return (ENXIO); 173 } 174 device_printf(dev, "upstream bus is %s\n", device_get_nameunit(busdev)); 175 176 /* 177 * If there is an idle-state property, that is the value we set the pins 178 * to when the bus is idle, otherwise idling the bus is a no-op 179 * (whichever bus was last accessed remains active). 180 */ 181 len = OF_getencprop(node, "idle-state", &propval, sizeof(propval)); 182 if (len == sizeof(propval)) { 183 if ((int)propval >= numchannels) { 184 device_printf(dev, 185 "idle-state property %d exceeds channel count\n", 186 propval); 187 } 188 sc->idleidx = (int)propval; 189 idlebits = sc->idleidx; 190 } else { 191 sc->idleidx = IDLE_NOOP; 192 idlebits = 0; 193 } 194 195 /* Preset the mux to the idle state to get things started. */ 196 for (i = 0; i < sc->numpins; ++i) { 197 gpio_pin_setflags(sc->pins[i], GPIO_PIN_OUTPUT); 198 gpio_pin_set_active(sc->pins[i], idlebits & (1u << i)); 199 } 200 201 /* Init the core driver, have it add our child downstream buses. */ 202 if ((err = iicmux_attach(dev, busdev, numchannels)) == 0) 203 bus_generic_attach(dev); 204 205 return (err); 206 } 207 208 static int 209 gpiomux_detach(device_t dev) 210 { 211 struct gpiomux_softc *sc = device_get_softc(dev); 212 int err, i; 213 214 if ((err = iicmux_detach(dev)) != 0) 215 return (err); 216 217 for (i = 0; i < sc->numpins; ++i) 218 gpio_pin_release(sc->pins[i]); 219 220 return (0); 221 } 222 223 static device_method_t gpiomux_methods[] = { 224 /* device methods */ 225 DEVMETHOD(device_probe, gpiomux_probe), 226 DEVMETHOD(device_attach, gpiomux_attach), 227 DEVMETHOD(device_detach, gpiomux_detach), 228 229 /* iicmux methods */ 230 DEVMETHOD(iicmux_bus_select, gpiomux_bus_select), 231 232 DEVMETHOD_END 233 }; 234 235 static devclass_t gpiomux_devclass; 236 237 DEFINE_CLASS_1(iic_gpiomux, iic_gpiomux_driver, gpiomux_methods, 238 sizeof(struct gpiomux_softc), iicmux_driver); 239 DRIVER_MODULE(iic_gpiomux, simplebus, iic_gpiomux_driver, gpiomux_devclass, 0, 0); 240 DRIVER_MODULE(iic_gpiomux, ofw_simplebus, iic_gpiomux_driver, gpiomux_devclass, 0, 0); 241 242 #ifdef FDT 243 DRIVER_MODULE(ofw_iicbus, iic_gpiomux, ofw_iicbus_driver, ofw_iicbus_devclass, 0, 0); 244 #else 245 DRIVER_MODULE(iicbus, iic_gpiomux, iicbus_driver, iicbus_devclass, 0, 0); 246 #endif 247 248 MODULE_DEPEND(iic_gpiomux, iicmux, 1, 1, 1); 249 MODULE_DEPEND(iic_gpiomux, iicbus, 1, 1, 1); 250