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 #include "opt_platform.h"
306844eecfSMichal Meloun
316844eecfSMichal Meloun #include <sys/param.h>
326844eecfSMichal Meloun #include <sys/bus.h>
336844eecfSMichal Meloun #include <sys/kernel.h>
346844eecfSMichal Meloun #include <sys/module.h>
356844eecfSMichal Meloun #include <sys/systm.h>
366844eecfSMichal Meloun
376844eecfSMichal Meloun #include <dev/iicbus/iicbus.h>
386844eecfSMichal Meloun #include <dev/iicbus/iiconf.h>
396844eecfSMichal Meloun #include <dev/ofw/ofw_bus.h>
406844eecfSMichal Meloun #include <dev/ofw/ofw_bus_subr.h>
416844eecfSMichal Meloun #include <dev/ofw/openfirm.h>
426844eecfSMichal Meloun
436844eecfSMichal Meloun #include "iicbus_if.h"
446844eecfSMichal Meloun #include "iicmux_if.h"
456844eecfSMichal Meloun
466844eecfSMichal Meloun
476844eecfSMichal Meloun static struct ofw_compat_data compat_data[] = {
486844eecfSMichal Meloun {"nxp,pca9547", 1},
496844eecfSMichal Meloun {NULL, 0}
506844eecfSMichal Meloun };
51519b64e2SMark Johnston IICBUS_FDT_PNP_INFO(compat_data);
526844eecfSMichal Meloun
536844eecfSMichal Meloun #include <dev/iicbus/mux/iicmux.h>
546844eecfSMichal Meloun
556844eecfSMichal Meloun struct pca9547_softc {
566844eecfSMichal Meloun struct iicmux_softc mux;
576844eecfSMichal Meloun device_t dev;
586844eecfSMichal Meloun bool idle_disconnect;
596844eecfSMichal Meloun };
606844eecfSMichal Meloun
616844eecfSMichal Meloun static int
pca9547_bus_select(device_t dev,int busidx,struct iic_reqbus_data * rd)626844eecfSMichal Meloun pca9547_bus_select(device_t dev, int busidx, struct iic_reqbus_data *rd)
636844eecfSMichal Meloun {
646844eecfSMichal Meloun struct pca9547_softc *sc = device_get_softc(dev);
656844eecfSMichal Meloun uint8_t busbits;
666844eecfSMichal Meloun
676844eecfSMichal Meloun /*
686844eecfSMichal Meloun * The iicmux caller ensures busidx is between 0 and the number of buses
696844eecfSMichal Meloun * we passed to iicmux_init_softc(), no need for validation here. If
706844eecfSMichal Meloun * the fdt data has the idle_disconnect property we idle the bus by
716844eecfSMichal Meloun * selecting no downstream buses, otherwise we just leave the current
726844eecfSMichal Meloun * bus active. The upper bits of control register 3 activate the
736844eecfSMichal Meloun * downstream buses; bit 7 is the first bus, bit 6 the second, etc.
746844eecfSMichal Meloun */
756844eecfSMichal Meloun if (busidx == IICMUX_SELECT_IDLE) {
766844eecfSMichal Meloun if (sc->idle_disconnect)
776844eecfSMichal Meloun busbits = 0;
786844eecfSMichal Meloun else
796844eecfSMichal Meloun return (0);
806844eecfSMichal Meloun } else {
816844eecfSMichal Meloun busbits = 0x8 | (busidx & 0x7);
826844eecfSMichal Meloun }
836844eecfSMichal Meloun
846844eecfSMichal Meloun /*
856844eecfSMichal Meloun * We have to add the IIC_RECURSIVE flag because the iicmux core has
866844eecfSMichal Meloun * already reserved the bus for us, and iicdev_writeto() is going to try
876844eecfSMichal Meloun * to reserve it again, which is allowed with the recursive flag.
886844eecfSMichal Meloun */
896844eecfSMichal Meloun
906844eecfSMichal Meloun return (iicdev_writeto(dev, busbits, NULL, 0,
916844eecfSMichal Meloun rd->flags | IIC_RECURSIVE));
926844eecfSMichal Meloun }
936844eecfSMichal Meloun
946844eecfSMichal Meloun static int
pca9547_probe(device_t dev)956844eecfSMichal Meloun pca9547_probe(device_t dev)
966844eecfSMichal Meloun {
976844eecfSMichal Meloun if (!ofw_bus_status_okay(dev))
986844eecfSMichal Meloun return (ENXIO);
996844eecfSMichal Meloun
1006844eecfSMichal Meloun if (!ofw_bus_search_compatible(dev, compat_data)->ocd_data)
1016844eecfSMichal Meloun return (ENXIO);
1026844eecfSMichal Meloun
1036844eecfSMichal Meloun device_set_desc(dev, "PCA9547 IIC bus multiplexor");
1046844eecfSMichal Meloun return (BUS_PROBE_DEFAULT);
1056844eecfSMichal Meloun }
1066844eecfSMichal Meloun
1076844eecfSMichal Meloun static int
pca9547_attach(device_t dev)1086844eecfSMichal Meloun pca9547_attach(device_t dev)
1096844eecfSMichal Meloun {
1106844eecfSMichal Meloun struct pca9547_softc *sc;
1116844eecfSMichal Meloun phandle_t node;
1126844eecfSMichal Meloun int rv;
1136844eecfSMichal Meloun
1146844eecfSMichal Meloun sc = device_get_softc(dev);
1156844eecfSMichal Meloun sc ->dev = dev;
1166844eecfSMichal Meloun
1176844eecfSMichal Meloun node = ofw_bus_get_node(dev);
1186844eecfSMichal Meloun sc->idle_disconnect = OF_hasprop(node, "i2c-mux-idle-disconnect");
1196844eecfSMichal Meloun
1206844eecfSMichal Meloun rv = iicmux_attach(sc->dev, device_get_parent(dev), 8);
1216844eecfSMichal Meloun if (rv != 0)
1226844eecfSMichal Meloun return (rv);
1236844eecfSMichal Meloun rv = bus_generic_attach(dev);
1246844eecfSMichal Meloun
1256844eecfSMichal Meloun return (rv);
1266844eecfSMichal Meloun }
1276844eecfSMichal Meloun
1286844eecfSMichal Meloun static int
pca9547_detach(device_t dev)1296844eecfSMichal Meloun pca9547_detach(device_t dev)
1306844eecfSMichal Meloun {
1316844eecfSMichal Meloun int rv;
1326844eecfSMichal Meloun
1336844eecfSMichal Meloun rv = iicmux_detach(dev);
1346844eecfSMichal Meloun if (rv != 0)
1356844eecfSMichal Meloun return (rv);
1366844eecfSMichal Meloun
1376844eecfSMichal Meloun return (0);
1386844eecfSMichal Meloun }
1396844eecfSMichal Meloun
1406844eecfSMichal Meloun static device_method_t pca9547_methods[] = {
1416844eecfSMichal Meloun /* device methods */
1426844eecfSMichal Meloun DEVMETHOD(device_probe, pca9547_probe),
1436844eecfSMichal Meloun DEVMETHOD(device_attach, pca9547_attach),
1446844eecfSMichal Meloun DEVMETHOD(device_detach, pca9547_detach),
1456844eecfSMichal Meloun
1466844eecfSMichal Meloun /* iicmux methods */
1476844eecfSMichal Meloun DEVMETHOD(iicmux_bus_select, pca9547_bus_select),
1486844eecfSMichal Meloun
1496844eecfSMichal Meloun DEVMETHOD_END
1506844eecfSMichal Meloun };
1516844eecfSMichal Meloun
1526844eecfSMichal Meloun DEFINE_CLASS_1(iicmux, pca9547_driver, pca9547_methods,
1536844eecfSMichal Meloun sizeof(struct pca9547_softc), iicmux_driver);
154*3a866152SJohn Baldwin DRIVER_MODULE(pca_iicmux, iicbus, pca9547_driver, 0, 0);
155676ea8e1SJohn Baldwin DRIVER_MODULE(iicbus, iicmux, iicbus_driver, 0, 0);
15685447c52SJohn Baldwin DRIVER_MODULE(ofw_iicbus, iicmux, ofw_iicbus_driver, 0, 0);
1576844eecfSMichal Meloun MODULE_DEPEND(pca9547, iicmux, 1, 1, 1);
1586844eecfSMichal Meloun MODULE_DEPEND(pca9547, iicbus, 1, 1, 1);
159