xref: /freebsd/sys/dev/iicbus/mux/pca9547.c (revision c66ec88fed842fbaad62c30d510644ceb7bd2d71)
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 <dev/ofw/ofw_bus.h>
42 #include <dev/ofw/ofw_bus_subr.h>
43 #include <dev/ofw/openfirm.h>
44 
45 #include "iicbus_if.h"
46 #include "iicmux_if.h"
47 
48 
49 static struct ofw_compat_data compat_data[] = {
50 	{"nxp,pca9547",		1},
51 	{NULL,			0}
52 };
53 IICBUS_FDT_PNP_INFO(compat_data);
54 
55 #include <dev/iicbus/mux/iicmux.h>
56 
57 struct pca9547_softc {
58 	struct iicmux_softc mux;
59 	device_t	dev;
60 	bool idle_disconnect;
61 };
62 
63 static int
64 pca9547_bus_select(device_t dev, int busidx, struct iic_reqbus_data *rd)
65 {
66 	struct pca9547_softc *sc = device_get_softc(dev);
67 	uint8_t busbits;
68 
69 	/*
70 	 * The iicmux caller ensures busidx is between 0 and the number of buses
71 	 * we passed to iicmux_init_softc(), no need for validation here.  If
72 	 * the fdt data has the idle_disconnect property we idle the bus by
73 	 * selecting no downstream buses, otherwise we just leave the current
74 	 * bus active.  The upper bits of control register 3 activate the
75 	 * downstream buses; bit 7 is the first bus, bit 6 the second, etc.
76 	 */
77 	if (busidx == IICMUX_SELECT_IDLE) {
78 		if (sc->idle_disconnect)
79 			busbits = 0;
80 		else
81 			return (0);
82 	} else {
83 		busbits = 0x8 | (busidx & 0x7);
84 	}
85 
86 	/*
87 	 * We have to add the IIC_RECURSIVE flag because the iicmux core has
88 	 * already reserved the bus for us, and iicdev_writeto() is going to try
89 	 * to reserve it again, which is allowed with the recursive flag.
90 	 */
91 
92 	return (iicdev_writeto(dev, busbits, NULL, 0,
93 	    rd->flags | IIC_RECURSIVE));
94 }
95 
96 static int
97 pca9547_probe(device_t dev)
98 {
99 	if (!ofw_bus_status_okay(dev))
100 		return (ENXIO);
101 
102 	if (!ofw_bus_search_compatible(dev, compat_data)->ocd_data)
103 		return (ENXIO);
104 
105 	device_set_desc(dev, "PCA9547 IIC bus multiplexor");
106 	return (BUS_PROBE_DEFAULT);
107 }
108 
109 static int
110 pca9547_attach(device_t dev)
111 {
112 	struct pca9547_softc *sc;
113 	phandle_t node;
114 	int rv;
115 
116 	sc = device_get_softc(dev);
117 	sc ->dev = dev;
118 
119 	node = ofw_bus_get_node(dev);
120 	sc->idle_disconnect = OF_hasprop(node, "i2c-mux-idle-disconnect");
121 
122 	rv = iicmux_attach(sc->dev, device_get_parent(dev), 8);
123 	if (rv != 0)
124 		return (rv);
125 	rv = bus_generic_attach(dev);
126 
127 	return (rv);
128 }
129 
130 static int
131 pca9547_detach(device_t dev)
132 {
133 	int rv;
134 
135 	rv = iicmux_detach(dev);
136 	if (rv != 0)
137 		return (rv);
138 
139 	return (0);
140 }
141 
142 static device_method_t pca9547_methods[] = {
143 	/* device methods */
144 	DEVMETHOD(device_probe,			pca9547_probe),
145 	DEVMETHOD(device_attach,		pca9547_attach),
146 	DEVMETHOD(device_detach,		pca9547_detach),
147 
148 	/* iicmux methods */
149 	DEVMETHOD(iicmux_bus_select,		pca9547_bus_select),
150 
151 	DEVMETHOD_END
152 };
153 
154 static devclass_t pca9547_devclass;
155 DEFINE_CLASS_1(iicmux, pca9547_driver, pca9547_methods,
156     sizeof(struct pca9547_softc), iicmux_driver);
157 DRIVER_MODULE(pca_iicmux, iicbus, pca9547_driver, pca9547_devclass, 0, 0);
158 DRIVER_MODULE(iicbus, iicmux, iicbus_driver, iicbus_devclass, 0, 0);
159 DRIVER_MODULE(ofw_iicbus, iicmux, ofw_iicbus_driver, ofw_iicbus_devclass,
160     0, 0);
161 MODULE_DEPEND(pca9547, iicmux, 1, 1, 1);
162 MODULE_DEPEND(pca9547, iicbus, 1, 1, 1);
163