16844eecfSMichal Meloun /*- 26844eecfSMichal Meloun * SPDX-License-Identifier: BSD-2-Clause 36844eecfSMichal Meloun * 46844eecfSMichal Meloun * Copyright (c) 2019 Ian Lepore <ian@freebsd.org> 56844eecfSMichal Meloun * 66844eecfSMichal Meloun * Redistribution and use in source and binary forms, with or without 76844eecfSMichal Meloun * modification, are permitted provided that the following conditions 86844eecfSMichal Meloun * are met: 96844eecfSMichal Meloun * 1. Redistributions of source code must retain the above copyright 106844eecfSMichal Meloun * notice, this list of conditions and the following disclaimer. 116844eecfSMichal Meloun * 2. Redistributions in binary form must reproduce the above copyright 126844eecfSMichal Meloun * notice, this list of conditions and the following disclaimer in the 136844eecfSMichal Meloun * documentation and/or other materials provided with the distribution. 146844eecfSMichal Meloun * 156844eecfSMichal Meloun * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 166844eecfSMichal Meloun * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 176844eecfSMichal Meloun * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 186844eecfSMichal Meloun * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 196844eecfSMichal Meloun * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 206844eecfSMichal Meloun * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 216844eecfSMichal Meloun * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 226844eecfSMichal Meloun * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 236844eecfSMichal Meloun * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 246844eecfSMichal Meloun * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 256844eecfSMichal Meloun * SUCH DAMAGE. 266844eecfSMichal Meloun */ 276844eecfSMichal Meloun 286844eecfSMichal Meloun #include <sys/cdefs.h> 296844eecfSMichal Meloun __FBSDID("$FreeBSD$"); 306844eecfSMichal Meloun 316844eecfSMichal Meloun #include "opt_platform.h" 326844eecfSMichal Meloun 336844eecfSMichal Meloun #include <sys/param.h> 346844eecfSMichal Meloun #include <sys/bus.h> 356844eecfSMichal Meloun #include <sys/kernel.h> 366844eecfSMichal Meloun #include <sys/module.h> 376844eecfSMichal Meloun #include <sys/systm.h> 386844eecfSMichal Meloun 396844eecfSMichal Meloun #include <dev/iicbus/iicbus.h> 406844eecfSMichal Meloun #include <dev/iicbus/iiconf.h> 416844eecfSMichal Meloun #include <dev/ofw/ofw_bus.h> 426844eecfSMichal Meloun #include <dev/ofw/ofw_bus_subr.h> 436844eecfSMichal Meloun #include <dev/ofw/openfirm.h> 446844eecfSMichal Meloun 456844eecfSMichal Meloun #include "iicbus_if.h" 466844eecfSMichal Meloun #include "iicmux_if.h" 476844eecfSMichal Meloun 486844eecfSMichal Meloun 496844eecfSMichal Meloun static struct ofw_compat_data compat_data[] = { 506844eecfSMichal Meloun {"nxp,pca9547", 1}, 516844eecfSMichal Meloun {NULL, 0} 526844eecfSMichal Meloun }; 53519b64e2SMark Johnston IICBUS_FDT_PNP_INFO(compat_data); 546844eecfSMichal Meloun 556844eecfSMichal Meloun #include <dev/iicbus/mux/iicmux.h> 566844eecfSMichal Meloun 576844eecfSMichal Meloun struct pca9547_softc { 586844eecfSMichal Meloun struct iicmux_softc mux; 596844eecfSMichal Meloun device_t dev; 606844eecfSMichal Meloun bool idle_disconnect; 616844eecfSMichal Meloun }; 626844eecfSMichal Meloun 636844eecfSMichal Meloun static int 646844eecfSMichal Meloun pca9547_bus_select(device_t dev, int busidx, struct iic_reqbus_data *rd) 656844eecfSMichal Meloun { 666844eecfSMichal Meloun struct pca9547_softc *sc = device_get_softc(dev); 676844eecfSMichal Meloun uint8_t busbits; 686844eecfSMichal Meloun 696844eecfSMichal Meloun /* 706844eecfSMichal Meloun * The iicmux caller ensures busidx is between 0 and the number of buses 716844eecfSMichal Meloun * we passed to iicmux_init_softc(), no need for validation here. If 726844eecfSMichal Meloun * the fdt data has the idle_disconnect property we idle the bus by 736844eecfSMichal Meloun * selecting no downstream buses, otherwise we just leave the current 746844eecfSMichal Meloun * bus active. The upper bits of control register 3 activate the 756844eecfSMichal Meloun * downstream buses; bit 7 is the first bus, bit 6 the second, etc. 766844eecfSMichal Meloun */ 776844eecfSMichal Meloun if (busidx == IICMUX_SELECT_IDLE) { 786844eecfSMichal Meloun if (sc->idle_disconnect) 796844eecfSMichal Meloun busbits = 0; 806844eecfSMichal Meloun else 816844eecfSMichal Meloun return (0); 826844eecfSMichal Meloun } else { 836844eecfSMichal Meloun busbits = 0x8 | (busidx & 0x7); 846844eecfSMichal Meloun } 856844eecfSMichal Meloun 866844eecfSMichal Meloun /* 876844eecfSMichal Meloun * We have to add the IIC_RECURSIVE flag because the iicmux core has 886844eecfSMichal Meloun * already reserved the bus for us, and iicdev_writeto() is going to try 896844eecfSMichal Meloun * to reserve it again, which is allowed with the recursive flag. 906844eecfSMichal Meloun */ 916844eecfSMichal Meloun 926844eecfSMichal Meloun return (iicdev_writeto(dev, busbits, NULL, 0, 936844eecfSMichal Meloun rd->flags | IIC_RECURSIVE)); 946844eecfSMichal Meloun } 956844eecfSMichal Meloun 966844eecfSMichal Meloun static int 976844eecfSMichal Meloun pca9547_probe(device_t dev) 986844eecfSMichal Meloun { 996844eecfSMichal Meloun if (!ofw_bus_status_okay(dev)) 1006844eecfSMichal Meloun return (ENXIO); 1016844eecfSMichal Meloun 1026844eecfSMichal Meloun if (!ofw_bus_search_compatible(dev, compat_data)->ocd_data) 1036844eecfSMichal Meloun return (ENXIO); 1046844eecfSMichal Meloun 1056844eecfSMichal Meloun device_set_desc(dev, "PCA9547 IIC bus multiplexor"); 1066844eecfSMichal Meloun return (BUS_PROBE_DEFAULT); 1076844eecfSMichal Meloun } 1086844eecfSMichal Meloun 1096844eecfSMichal Meloun static int 1106844eecfSMichal Meloun pca9547_attach(device_t dev) 1116844eecfSMichal Meloun { 1126844eecfSMichal Meloun struct pca9547_softc *sc; 1136844eecfSMichal Meloun phandle_t node; 1146844eecfSMichal Meloun int rv; 1156844eecfSMichal Meloun 1166844eecfSMichal Meloun sc = device_get_softc(dev); 1176844eecfSMichal Meloun sc ->dev = dev; 1186844eecfSMichal Meloun 1196844eecfSMichal Meloun node = ofw_bus_get_node(dev); 1206844eecfSMichal Meloun sc->idle_disconnect = OF_hasprop(node, "i2c-mux-idle-disconnect"); 1216844eecfSMichal Meloun 1226844eecfSMichal Meloun rv = iicmux_attach(sc->dev, device_get_parent(dev), 8); 1236844eecfSMichal Meloun if (rv != 0) 1246844eecfSMichal Meloun return (rv); 1256844eecfSMichal Meloun rv = bus_generic_attach(dev); 1266844eecfSMichal Meloun 1276844eecfSMichal Meloun return (rv); 1286844eecfSMichal Meloun } 1296844eecfSMichal Meloun 1306844eecfSMichal Meloun static int 1316844eecfSMichal Meloun pca9547_detach(device_t dev) 1326844eecfSMichal Meloun { 1336844eecfSMichal Meloun int rv; 1346844eecfSMichal Meloun 1356844eecfSMichal Meloun rv = iicmux_detach(dev); 1366844eecfSMichal Meloun if (rv != 0) 1376844eecfSMichal Meloun return (rv); 1386844eecfSMichal Meloun 1396844eecfSMichal Meloun return (0); 1406844eecfSMichal Meloun } 1416844eecfSMichal Meloun 1426844eecfSMichal Meloun static device_method_t pca9547_methods[] = { 1436844eecfSMichal Meloun /* device methods */ 1446844eecfSMichal Meloun DEVMETHOD(device_probe, pca9547_probe), 1456844eecfSMichal Meloun DEVMETHOD(device_attach, pca9547_attach), 1466844eecfSMichal Meloun DEVMETHOD(device_detach, pca9547_detach), 1476844eecfSMichal Meloun 1486844eecfSMichal Meloun /* iicmux methods */ 1496844eecfSMichal Meloun DEVMETHOD(iicmux_bus_select, pca9547_bus_select), 1506844eecfSMichal Meloun 1516844eecfSMichal Meloun DEVMETHOD_END 1526844eecfSMichal Meloun }; 1536844eecfSMichal Meloun 1546844eecfSMichal Meloun static devclass_t pca9547_devclass; 1556844eecfSMichal Meloun DEFINE_CLASS_1(iicmux, pca9547_driver, pca9547_methods, 1566844eecfSMichal Meloun sizeof(struct pca9547_softc), iicmux_driver); 1576844eecfSMichal Meloun DRIVER_MODULE(pca_iicmux, iicbus, pca9547_driver, pca9547_devclass, 0, 0); 158*676ea8e1SJohn Baldwin DRIVER_MODULE(iicbus, iicmux, iicbus_driver, 0, 0); 1596844eecfSMichal Meloun DRIVER_MODULE(ofw_iicbus, iicmux, ofw_iicbus_driver, ofw_iicbus_devclass, 1606844eecfSMichal Meloun 0, 0); 1616844eecfSMichal Meloun MODULE_DEPEND(pca9547, iicmux, 1, 1, 1); 1626844eecfSMichal Meloun MODULE_DEPEND(pca9547, iicbus, 1, 1, 1); 163