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