2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2009 Oleksandr Tymoshenko <gonzo@freebsd.org>
5 * Copyright (c) 2010 Luiz Otavio O Souza
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
33 #include "opt_platform.h"
35 #include <sys/param.h>
36 #include <sys/systm.h>
39 #include <sys/kernel.h>
40 #include <sys/module.h>
43 #include <dev/fdt/fdt_common.h>
44 #include <dev/ofw/ofw_bus.h>
47 #include <dev/gpio/gpiobusvar.h>
48 #include <dev/iicbus/iiconf.h>
49 #include <dev/iicbus/iicbus.h>
51 #include "gpiobus_if.h"
54 #define GPIOIIC_SCL_DFLT 0
55 #define GPIOIIC_SDA_DFLT 1
56 #define GPIOIIC_MIN_PINS 2
66 static int gpioiic_probe(device_t);
67 static int gpioiic_attach(device_t);
70 static void gpioiic_reset_bus(device_t);
71 static void gpioiic_setsda(device_t, int);
72 static void gpioiic_setscl(device_t, int);
73 static int gpioiic_getsda(device_t);
74 static int gpioiic_getscl(device_t);
75 static int gpioiic_reset(device_t, u_char, u_char, u_char *);
78 gpioiic_probe(device_t dev)
80 struct gpiobus_ivar *devi;
83 if (!ofw_bus_status_okay(dev))
85 if (!ofw_bus_is_compatible(dev, "gpioiic"))
88 devi = GPIOBUS_IVAR(dev);
89 if (devi->npins < GPIOIIC_MIN_PINS) {
91 "gpioiic needs at least %d GPIO pins (only %d given).\n",
92 GPIOIIC_MIN_PINS, devi->npins);
95 device_set_desc(dev, "GPIO I2C bit-banging driver");
97 return (BUS_PROBE_DEFAULT);
101 gpioiic_attach(device_t dev)
108 struct gpiobus_ivar *devi;
109 struct gpioiic_softc *sc;
111 sc = device_get_softc(dev);
113 sc->sc_busdev = device_get_parent(dev);
114 if (resource_int_value(device_get_name(dev),
115 device_get_unit(dev), "scl", &sc->scl_pin))
116 sc->scl_pin = GPIOIIC_SCL_DFLT;
117 if (resource_int_value(device_get_name(dev),
118 device_get_unit(dev), "sda", &sc->sda_pin))
119 sc->sda_pin = GPIOIIC_SDA_DFLT;
122 if ((node = ofw_bus_get_node(dev)) == -1)
124 if (OF_getencprop(node, "scl", &pin, sizeof(pin)) > 0)
125 sc->scl_pin = (int)pin;
126 if (OF_getencprop(node, "sda", &pin, sizeof(pin)) > 0)
127 sc->sda_pin = (int)pin;
130 if (sc->scl_pin < 0 || sc->scl_pin > 1)
131 sc->scl_pin = GPIOIIC_SCL_DFLT;
132 if (sc->sda_pin < 0 || sc->sda_pin > 1)
133 sc->sda_pin = GPIOIIC_SDA_DFLT;
135 devi = GPIOBUS_IVAR(dev);
136 device_printf(dev, "SCL pin: %d, SDA pin: %d\n",
137 devi->pins[sc->scl_pin], devi->pins[sc->sda_pin]);
139 /* add generic bit-banging code */
140 bitbang = device_add_child(dev, "iicbb", -1);
141 device_probe_and_attach(bitbang);
147 * Reset bus by setting SDA first and then SCL.
148 * Must always be called with gpio bus locked.
151 gpioiic_reset_bus(device_t dev)
153 struct gpioiic_softc *sc = device_get_softc(dev);
155 GPIOBUS_PIN_SETFLAGS(sc->sc_busdev, sc->sc_dev, sc->sda_pin,
157 GPIOBUS_PIN_SETFLAGS(sc->sc_busdev, sc->sc_dev, sc->scl_pin,
162 gpioiic_setsda(device_t dev, int val)
164 struct gpioiic_softc *sc = device_get_softc(dev);
167 GPIOBUS_PIN_SET(sc->sc_busdev, sc->sc_dev, sc->sda_pin, 0);
168 GPIOBUS_PIN_SETFLAGS(sc->sc_busdev, sc->sc_dev, sc->sda_pin,
171 GPIOBUS_PIN_SETFLAGS(sc->sc_busdev, sc->sc_dev, sc->sda_pin,
177 gpioiic_setscl(device_t dev, int val)
179 struct gpioiic_softc *sc = device_get_softc(dev);
182 GPIOBUS_PIN_SET(sc->sc_busdev, sc->sc_dev, sc->scl_pin, 0);
183 GPIOBUS_PIN_SETFLAGS(sc->sc_busdev, sc->sc_dev, sc->scl_pin,
186 GPIOBUS_PIN_SETFLAGS(sc->sc_busdev, sc->sc_dev, sc->scl_pin,
192 gpioiic_getscl(device_t dev)
194 struct gpioiic_softc *sc = device_get_softc(dev);
197 GPIOBUS_PIN_SETFLAGS(sc->sc_busdev, sc->sc_dev, sc->scl_pin,
199 GPIOBUS_PIN_GET(sc->sc_busdev, sc->sc_dev, sc->scl_pin, &val);
205 gpioiic_getsda(device_t dev)
207 struct gpioiic_softc *sc = device_get_softc(dev);
210 GPIOBUS_PIN_SETFLAGS(sc->sc_busdev, sc->sc_dev, sc->sda_pin,
212 GPIOBUS_PIN_GET(sc->sc_busdev, sc->sc_dev, sc->sda_pin, &val);
218 gpioiic_reset(device_t dev, u_char speed, u_char addr, u_char *oldaddr)
220 struct gpioiic_softc *sc;
222 sc = device_get_softc(dev);
223 gpioiic_reset_bus(sc->sc_dev);
225 return (IIC_ENOADDR);
230 gpioiic_get_node(device_t bus, device_t dev)
233 /* We only have one child, the iicbb, which needs our own node. */
234 return (ofw_bus_get_node(bus));
238 static devclass_t gpioiic_devclass;
240 static device_method_t gpioiic_methods[] = {
241 /* Device interface */
242 DEVMETHOD(device_probe, gpioiic_probe),
243 DEVMETHOD(device_attach, gpioiic_attach),
244 DEVMETHOD(device_detach, bus_generic_detach),
246 /* iicbb interface */
247 DEVMETHOD(iicbb_setsda, gpioiic_setsda),
248 DEVMETHOD(iicbb_setscl, gpioiic_setscl),
249 DEVMETHOD(iicbb_getsda, gpioiic_getsda),
250 DEVMETHOD(iicbb_getscl, gpioiic_getscl),
251 DEVMETHOD(iicbb_reset, gpioiic_reset),
254 /* OFW bus interface */
255 DEVMETHOD(ofw_bus_get_node, gpioiic_get_node),
261 static driver_t gpioiic_driver = {
264 sizeof(struct gpioiic_softc),
267 DRIVER_MODULE(gpioiic, gpiobus, gpioiic_driver, gpioiic_devclass, 0, 0);
268 DRIVER_MODULE(iicbb, gpioiic, iicbb_driver, iicbb_devclass, 0, 0);
269 MODULE_DEPEND(gpioiic, iicbb, IICBB_MINVER, IICBB_PREFVER, IICBB_MAXVER);
270 MODULE_DEPEND(gpioiic, gpiobus, 1, 1, 1);