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