]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/dev/iicbus/mux/iic_gpiomux.c
zfs: merge OpenZFS master-bf156c966
[FreeBSD/FreeBSD.git] / sys / dev / iicbus / mux / iic_gpiomux.c
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 /*
29  * Driver for i2c bus muxes controlled by one or more gpio pins.
30  *
31  * This driver has #ifdef FDT sections in it, as if it supports both fdt and
32  * hinted attachment, but there is currently no support for hinted attachment.
33  */
34
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
37
38 #include "opt_platform.h"
39
40 #include <sys/param.h>
41 #include <sys/bus.h>
42 #include <sys/gpio.h>
43 #include <sys/kernel.h>
44 #include <sys/module.h>
45 #include <sys/systm.h>
46
47 #include <dev/gpio/gpiobusvar.h>
48
49 #include <dev/iicbus/iicbus.h>
50 #include <dev/iicbus/mux/iicmux.h>
51
52 #ifdef FDT
53 #include <dev/ofw/ofw_bus.h>
54 #include <dev/ofw/ofw_bus_subr.h>
55 #include <dev/ofw/openfirm.h>
56
57 static struct ofw_compat_data compat_data[] = {
58         {"i2c-mux-gpio",  true},
59         {NULL,            false}
60 };
61 OFWBUS_PNP_INFO(compat_data);
62 SIMPLEBUS_PNP_INFO(compat_data);
63 #endif /* FDT */
64
65 #include <dev/iicbus/iiconf.h>
66 #include "iicmux.h"
67 #include "iicmux_if.h"
68
69 struct gpiomux_softc {
70         struct iicmux_softc mux;
71         int     idleidx;
72         int     numpins;
73         gpio_pin_t pins[IICMUX_MAX_BUSES];
74 };
75
76 #define IDLE_NOOP       (-1) /* When asked to idle the bus, do nothing. */
77
78 static int
79 gpiomux_bus_select(device_t dev, int busidx, struct iic_reqbus_data *rd)
80 {
81         struct gpiomux_softc *sc = device_get_softc(dev);
82         int i;
83
84         /*
85          * The iicmux caller ensures busidx is between 0 and the number of buses
86          * we passed to iicmux_init_softc(), no need for validation here.  The
87          * bits in the index number are transcribed to the state of the pins,
88          * except when we're asked to idle the bus.  In that case, we transcribe
89          * sc->idleidx to the pins, unless that is IDLE_NOOP (leave the current
90          * bus selected), in which case we just bail.
91          */
92         if (busidx == IICMUX_SELECT_IDLE) {
93                 if (sc->idleidx == IDLE_NOOP)
94                         return (0);
95                 busidx = sc->idleidx;
96         }
97
98         for (i = 0; i < sc->numpins; ++i)
99                 gpio_pin_set_active(sc->pins[i], busidx & (1u << i));
100
101         return (0);
102 }
103
104 static int
105 gpiomux_probe(device_t dev)
106 {
107         int rv;
108
109         rv = ENXIO;
110
111 #ifdef FDT
112         if (ofw_bus_status_okay(dev) &&
113             ofw_bus_search_compatible(dev, compat_data)->ocd_data)
114                 rv = BUS_PROBE_DEFAULT;
115 #endif
116
117         device_set_desc(dev, "I2C GPIO Mux");
118
119         return (rv);
120 }
121
122 static void
123 gpiomux_release_pins(struct gpiomux_softc *sc)
124 {
125         int i;
126
127         for (i = 0; i < sc->numpins; ++i)
128                 gpio_pin_release(sc->pins[i]);
129 }
130
131 static int
132 gpiomux_attach(device_t dev)
133 {
134         struct gpiomux_softc *sc = device_get_softc(dev);
135         ssize_t len;
136         device_t busdev;
137         int err, i, idlebits, numchannels;
138         pcell_t propval;
139         phandle_t node;
140
141         node = ofw_bus_get_node(dev);
142
143         /*
144          * Locate the gpio pin(s) that control the mux hardware.  There can be
145          * multiple pins, but there must be at least one.
146          */
147         for (i = 0; ; ++i) {
148                 err = gpio_pin_get_by_ofw_propidx(dev, node, "mux-gpios", i,
149                     &sc->pins[i]);
150                 if (err != 0) {
151                         break;
152                 }
153         }
154         sc->numpins = i;
155         if (sc->numpins == 0) {
156                 device_printf(dev, "cannot acquire pins listed in mux-gpios\n");
157                 if (err == 0)
158                         err = ENXIO;
159                 goto errexit;
160         }
161         numchannels = 1u << sc->numpins;
162         if (numchannels > IICMUX_MAX_BUSES) {
163                 device_printf(dev, "too many mux-gpios pins for max %d buses\n",
164                     IICMUX_MAX_BUSES);
165                 err = EINVAL;
166                 goto errexit;
167         }
168
169         /*
170          * We don't have a parent/child relationship to the upstream bus, we
171          * have to locate it via the i2c-parent property.  Explicitly tell the
172          * user which upstream we're associated with, since the normal attach
173          * message is going to mention only our actual parent.
174          */
175         len = OF_getencprop(node, "i2c-parent", &propval, sizeof(propval));
176         if (len != sizeof(propval)) {
177                 device_printf(dev, "cannot obtain i2c-parent property\n");
178                 err = ENXIO;
179                 goto errexit;
180         }
181         busdev = OF_device_from_xref((phandle_t)propval);
182         if (busdev == NULL) {
183                 device_printf(dev,
184                     "cannot find device referenced by i2c-parent property\n");
185                 err = ENXIO;
186                 goto errexit;
187         }
188         device_printf(dev, "upstream bus is %s\n", device_get_nameunit(busdev));
189
190         /*
191          * If there is an idle-state property, that is the value we set the pins
192          * to when the bus is idle, otherwise idling the bus is a no-op
193          * (whichever bus was last accessed remains active).
194          */
195         len = OF_getencprop(node, "idle-state", &propval, sizeof(propval));
196         if (len == sizeof(propval)) {
197                 if ((int)propval >= numchannels) {
198                         device_printf(dev,
199                             "idle-state property %d exceeds channel count\n",
200                             propval);
201                 }
202                 sc->idleidx = (int)propval;
203                 idlebits = sc->idleidx;
204         } else {
205                 sc->idleidx = IDLE_NOOP;
206                 idlebits = 0;
207         }
208
209         /* Preset the mux to the idle state to get things started. */
210         for (i = 0; i < sc->numpins; ++i) {
211                 gpio_pin_setflags(sc->pins[i], GPIO_PIN_OUTPUT);
212                 gpio_pin_set_active(sc->pins[i], idlebits & (1u << i));
213         }
214
215         /* Init the core driver, have it add our child downstream buses. */
216         if ((err = iicmux_attach(dev, busdev, numchannels)) == 0)
217                 bus_generic_attach(dev);
218
219 errexit:
220
221         if (err != 0)
222                 gpiomux_release_pins(sc);
223
224         return (err);
225 }
226
227 static int
228 gpiomux_detach(device_t dev)
229 {
230         struct gpiomux_softc *sc = device_get_softc(dev);
231         int err;
232
233         if ((err = iicmux_detach(dev)) != 0)
234                 return (err);
235
236         gpiomux_release_pins(sc);
237
238         return (0);
239 }
240
241 static device_method_t gpiomux_methods[] = {
242         /* device methods */
243         DEVMETHOD(device_probe,                 gpiomux_probe),
244         DEVMETHOD(device_attach,                gpiomux_attach),
245         DEVMETHOD(device_detach,                gpiomux_detach),
246
247         /* iicmux methods */
248         DEVMETHOD(iicmux_bus_select,            gpiomux_bus_select),
249
250         DEVMETHOD_END
251 };
252
253 static devclass_t gpiomux_devclass;
254
255 DEFINE_CLASS_1(iic_gpiomux, iic_gpiomux_driver, gpiomux_methods,
256     sizeof(struct gpiomux_softc), iicmux_driver);
257 DRIVER_MODULE(iic_gpiomux, simplebus, iic_gpiomux_driver, gpiomux_devclass, 0, 0);
258 DRIVER_MODULE(iic_gpiomux, ofw_simplebus, iic_gpiomux_driver, gpiomux_devclass, 0, 0);
259
260 #ifdef FDT
261 DRIVER_MODULE(ofw_iicbus, iic_gpiomux, ofw_iicbus_driver, ofw_iicbus_devclass, 0, 0);
262 #else
263 DRIVER_MODULE(iicbus, iic_gpiomux, iicbus_driver, iicbus_devclass, 0, 0);
264 #endif
265
266 MODULE_DEPEND(iic_gpiomux, iicmux, 1, 1, 1);
267 MODULE_DEPEND(iic_gpiomux, iicbus, 1, 1, 1);