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 #include <sys/cdefs.h> 29 __FBSDID("$FreeBSD$"); 30 31 #include "opt_platform.h" 32 33 #include <sys/param.h> 34 #include <sys/bus.h> 35 #include <sys/kernel.h> 36 #include <sys/module.h> 37 #include <sys/systm.h> 38 39 #include <dev/iicbus/iicbus.h> 40 #include <dev/iicbus/iiconf.h> 41 #include "iicbus_if.h" 42 #include "iicmux_if.h" 43 44 static struct chip_info { 45 const char *partname; 46 const char *description; 47 int numchannels; 48 } chip_infos[] = { 49 {"ltc4305", "LTC4305 I2C Mux", 2}, 50 {"ltc4306", "LTC4306 I2C Mux", 4}, 51 }; 52 #define CHIP_NONE (-1) 53 #define CHIP_LTC4305 0 54 #define CHIP_LTC4306 1 55 56 #ifdef FDT 57 #include <dev/ofw/ofw_bus.h> 58 #include <dev/ofw/ofw_bus_subr.h> 59 #include <dev/ofw/openfirm.h> 60 61 static struct ofw_compat_data compat_data[] = { 62 {"lltc,ltc4305", CHIP_LTC4305}, 63 {"lltc,ltc4306", CHIP_LTC4306}, 64 {NULL, CHIP_NONE} 65 }; 66 IICBUS_FDT_PNP_INFO(compat_data); 67 #endif 68 69 #include <dev/iicbus/mux/iicmux.h> 70 71 struct ltc430x_softc { 72 struct iicmux_softc mux; 73 bool idle_disconnect; 74 }; 75 76 /* 77 * The datasheet doesn't name the registers, it calls them control register 0-3. 78 */ 79 #define LTC430X_CTLREG_0 0 80 #define LTC430X_CTLREG_1 1 81 #define LTC430X_CTLREG_2 2 82 #define LTC430X_CR2_ENABLE_MW (1u << 3) /* Enable mass write address. */ 83 #define LTC430X_CTLREG_3 3 84 85 static int 86 ltc430x_bus_select(device_t dev, int busidx, struct iic_reqbus_data *rd) 87 { 88 struct ltc430x_softc *sc = device_get_softc(dev); 89 uint8_t busbits; 90 91 /* 92 * The iicmux caller ensures busidx is between 0 and the number of buses 93 * we passed to iicmux_init_softc(), no need for validation here. If 94 * the fdt data has the idle_disconnect property we idle the bus by 95 * selecting no downstream buses, otherwise we just leave the current 96 * bus active. The upper bits of control register 3 activate the 97 * downstream buses; bit 7 is the first bus, bit 6 the second, etc. 98 */ 99 if (busidx == IICMUX_SELECT_IDLE) { 100 if (sc->idle_disconnect) 101 busbits = 0; 102 else 103 return (0); 104 } else { 105 busbits = 1u << (7 - busidx); 106 } 107 108 /* 109 * We have to add the IIC_RECURSIVE flag because the iicmux core has 110 * already reserved the bus for us, and iicdev_writeto() is going to try 111 * to reserve it again, which is allowed with the recursive flag. 112 */ 113 return (iicdev_writeto(dev, LTC430X_CTLREG_3, &busbits, sizeof(busbits), 114 rd->flags | IIC_RECURSIVE)); 115 } 116 117 static int 118 ltc430x_find_chiptype(device_t dev) 119 { 120 #ifdef FDT 121 return (ofw_bus_search_compatible(dev, compat_data)->ocd_data); 122 #else 123 const char *type; 124 int i; 125 126 if (resource_string_value(device_get_name(dev), device_get_unit(dev), 127 "chip_type", &type) == 0) { 128 for (i = 0; i < nitems(chip_infos); ++i) { 129 if (strcasecmp(type, chip_infos[i].partname) == 0) { 130 return (i); 131 } 132 } 133 } 134 return (CHIP_NONE); 135 #endif 136 } 137 138 static int 139 ltc430x_probe(device_t dev) 140 { 141 int type; 142 143 #ifdef FDT 144 if (!ofw_bus_status_okay(dev)) 145 return (ENXIO); 146 #endif 147 148 type = ltc430x_find_chiptype(dev); 149 if (type == CHIP_NONE) 150 return (ENXIO); 151 152 device_set_desc(dev, chip_infos[type].description); 153 154 return (BUS_PROBE_DEFAULT); 155 } 156 157 static int 158 ltc430x_attach(device_t dev) 159 { 160 struct ltc430x_softc *sc __unused; 161 int chip, err, numchan, val; 162 uint8_t busbits, ctlreg2; 163 164 sc = device_get_softc(dev); 165 166 busbits = 0; 167 ctlreg2 = LTC430X_CR2_ENABLE_MW; 168 169 /* 170 * Check for the idle-disconnect and ctlreg2 options, first in FDT data, 171 * then allow them to be overridden by hints data. 172 */ 173 #ifdef FDT 174 phandle_t node; 175 176 node = ofw_bus_get_node(dev); 177 sc->idle_disconnect = OF_hasprop(node, "i2c-mux-idle-disconnect"); 178 179 if (OF_getprop(node, "freebsd,ctlreg2", &val, sizeof(val)) > 0) { 180 ctlreg2 = val; 181 } 182 #endif 183 184 if (resource_int_value(device_get_name(dev), device_get_unit(dev), 185 "idle_disconnect", &val) == 0) { 186 sc->idle_disconnect = val; 187 } 188 189 if (resource_int_value(device_get_name(dev), device_get_unit(dev), 190 "ctlreg2", &val) == 0) { 191 ctlreg2 = val; 192 } 193 194 /* We found the chip type when probing, so now it "can't fail". */ 195 if ((chip = ltc430x_find_chiptype(dev)) == CHIP_NONE) { 196 device_printf(dev, "impossible: can't identify chip type\n"); 197 return (ENXIO); 198 } 199 numchan = chip_infos[chip].numchannels; 200 201 /* Set control reg 2 with configured (or default) values. */ 202 iicdev_writeto(dev, LTC430X_CTLREG_2, &ctlreg2, sizeof(ctlreg2), 203 IIC_WAIT); 204 205 /* If configured for idle-disconnect, ensure we start disconnected. */ 206 if (sc->idle_disconnect) { 207 iicdev_writeto(dev, LTC430X_CTLREG_3, &busbits, sizeof(busbits), 208 IIC_WAIT); 209 } 210 211 /* 212 * Invoke the iicmux framework's attach code, and if it succeeds, invoke 213 * the probe and attach code of any child iicbus instances it added. 214 */ 215 if ((err = iicmux_attach(dev, device_get_parent(dev), numchan)) == 0) 216 bus_generic_attach(dev); 217 218 return (err); 219 } 220 221 static int 222 ltc430x_detach(device_t dev) 223 { 224 int err; 225 226 if ((err = iicmux_detach(dev)) != 0) 227 return (err); 228 229 return (0); 230 } 231 232 static device_method_t ltc430x_methods[] = { 233 /* device methods */ 234 DEVMETHOD(device_probe, ltc430x_probe), 235 DEVMETHOD(device_attach, ltc430x_attach), 236 DEVMETHOD(device_detach, ltc430x_detach), 237 238 /* iicmux methods */ 239 DEVMETHOD(iicmux_bus_select, ltc430x_bus_select), 240 241 DEVMETHOD_END 242 }; 243 244 DEFINE_CLASS_1(ltc430x, ltc430x_driver, ltc430x_methods, 245 sizeof(struct ltc430x_softc), iicmux_driver); 246 DRIVER_MODULE(ltc430x, iicbus, ltc430x_driver, 0, 0); 247 248 #ifdef FDT 249 DRIVER_MODULE(ofw_iicbus, ltc430x, ofw_iicbus_driver, 0, 0); 250 #else 251 DRIVER_MODULE(iicbus, ltc430x, iicbus_driver, 0, 0); 252 #endif 253 254 MODULE_DEPEND(ltc430x, iicmux, 1, 1, 1); 255 MODULE_DEPEND(ltc430x, iicbus, 1, 1, 1); 256 257