FreeBSD kernel IICBUS device code
iic_gpiomux.c
Go to the documentation of this file.
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>
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
57static struct ofw_compat_data compat_data[] = {
58 {"i2c-mux-gpio", true},
59 {NULL, false}
60};
61OFWBUS_PNP_INFO(compat_data);
63#endif /* FDT */
64
65#include <dev/iicbus/iiconf.h>
66#include "iicmux.h"
67#include "iicmux_if.h"
68
74};
75
76#define IDLE_NOOP (-1) /* When asked to idle the bus, do nothing. */
77
78static int
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 */
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
104static int
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
122static void
124{
125 int i;
126
127 for (i = 0; i < sc->numpins; ++i)
128 gpio_pin_release(sc->pins[i]);
129}
130
131static int
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",
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
219errexit:
220
221 if (err != 0)
223
224 return (err);
225}
226
227static int
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
237
238 return (0);
239}
240
241static 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
253static devclass_t gpiomux_devclass;
254
255DEFINE_CLASS_1(iic_gpiomux, iic_gpiomux_driver, gpiomux_methods,
256 sizeof(struct gpiomux_softc), iicmux_driver);
257DRIVER_MODULE(iic_gpiomux, simplebus, iic_gpiomux_driver, gpiomux_devclass, 0, 0);
258DRIVER_MODULE(iic_gpiomux, ofw_simplebus, iic_gpiomux_driver, gpiomux_devclass, 0, 0);
259
260#ifdef FDT
262#else
264#endif
265
266MODULE_DEPEND(iic_gpiomux, iicmux, 1, 1, 1);
267MODULE_DEPEND(iic_gpiomux, iicbus, 1, 1, 1);
static ds13_compat_data compat_data[]
Definition: ds13rtc.c:187
static int gpiomux_bus_select(device_t dev, int busidx, struct iic_reqbus_data *rd)
Definition: iic_gpiomux.c:79
static void gpiomux_release_pins(struct gpiomux_softc *sc)
Definition: iic_gpiomux.c:123
static device_method_t gpiomux_methods[]
Definition: iic_gpiomux.c:241
#define IDLE_NOOP
Definition: iic_gpiomux.c:76
__FBSDID("$FreeBSD$")
static devclass_t gpiomux_devclass
Definition: iic_gpiomux.c:253
static int gpiomux_probe(device_t dev)
Definition: iic_gpiomux.c:105
static int gpiomux_detach(device_t dev)
Definition: iic_gpiomux.c:228
static int gpiomux_attach(device_t dev)
Definition: iic_gpiomux.c:132
DEFINE_CLASS_1(iic_gpiomux, iic_gpiomux_driver, gpiomux_methods, sizeof(struct gpiomux_softc), iicmux_driver)
MODULE_DEPEND(iic_gpiomux, iicmux, 1, 1, 1)
DRIVER_MODULE(iic_gpiomux, simplebus, iic_gpiomux_driver, gpiomux_devclass, 0, 0)
devclass_t iicbus_devclass
Definition: iicbus.c:386
driver_t iicbus_driver
Definition: iicbus.c:380
devclass_t ofw_iicbus_devclass
Definition: ofw_iicbus.c:85
driver_t ofw_iicbus_driver
INTERFACE iicbus
Definition: iicbus_if.m:32
int len
Definition: iicbus_if.m:102
int iicmux_detach(device_t dev)
Definition: iicmux.c:335
int iicmux_attach(device_t dev, device_t busdev, int numbuses)
Definition: iicmux.c:308
#define IICMUX_SELECT_IDLE
Definition: iicmux.h:43
#define IICMUX_MAX_BUSES
Definition: iicmux.h:33
INTERFACE iicmux
Definition: iicmux_if.m:32
int busidx
Definition: iicmux_if.m:47
struct iic_reqbus_data * rd
Definition: iicmux_if.m:48
SIMPLEBUS_PNP_INFO(compat_data)
device_t dev
Definition: ofw_iicbus_if.m:38
INTERFACE ofw_iicbus
Definition: ofw_iicbus_if.m:31
struct iicmux_softc mux
Definition: iic_gpiomux.c:70
gpio_pin_t pins[IICMUX_MAX_BUSES]
Definition: iic_gpiomux.c:73