2 * Copyright (c) 2009 Oleksandr Tymoshenko <gonzo@freebsd.org>
3 * Copyright (c) 2010 Luiz Otavio O Souza
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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.
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
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
31 #include "opt_platform.h"
33 #include <sys/param.h>
34 #include <sys/systm.h>
37 #include <sys/kernel.h>
38 #include <sys/module.h>
41 #include <dev/fdt/fdt_common.h>
42 #include <dev/ofw/ofw_bus.h>
45 #include <dev/gpio/gpiobusvar.h>
46 #include <dev/iicbus/iiconf.h>
47 #include <dev/iicbus/iicbus.h>
49 #include "gpiobus_if.h"
52 #define GPIOIIC_SCL_DFLT 0
53 #define GPIOIIC_SDA_DFLT 1
54 #define GPIOIIC_MIN_PINS 2
64 static int gpioiic_probe(device_t);
65 static int gpioiic_attach(device_t);
68 static void gpioiic_reset_bus(device_t);
69 static void gpioiic_setsda(device_t, int);
70 static void gpioiic_setscl(device_t, int);
71 static int gpioiic_getsda(device_t);
72 static int gpioiic_getscl(device_t);
73 static int gpioiic_reset(device_t, u_char, u_char, u_char *);
76 gpioiic_probe(device_t dev)
78 struct gpiobus_ivar *devi;
81 if (!ofw_bus_status_okay(dev))
83 if (!ofw_bus_is_compatible(dev, "gpioiic"))
86 devi = GPIOBUS_IVAR(dev);
87 if (devi->npins < GPIOIIC_MIN_PINS) {
89 "gpioiic needs at least %d GPIO pins (only %d given).\n",
90 GPIOIIC_MIN_PINS, devi->npins);
93 device_set_desc(dev, "GPIO I2C bit-banging driver");
95 return (BUS_PROBE_DEFAULT);
99 gpioiic_attach(device_t dev)
106 struct gpiobus_ivar *devi;
107 struct gpioiic_softc *sc;
109 sc = device_get_softc(dev);
111 sc->sc_busdev = device_get_parent(dev);
112 if (resource_int_value(device_get_name(dev),
113 device_get_unit(dev), "scl", &sc->scl_pin))
114 sc->scl_pin = GPIOIIC_SCL_DFLT;
115 if (resource_int_value(device_get_name(dev),
116 device_get_unit(dev), "sda", &sc->sda_pin))
117 sc->sda_pin = GPIOIIC_SDA_DFLT;
120 if ((node = ofw_bus_get_node(dev)) == -1)
122 if (OF_getencprop(node, "scl", &pin, sizeof(pin)) > 0)
123 sc->scl_pin = (int)pin;
124 if (OF_getencprop(node, "sda", &pin, sizeof(pin)) > 0)
125 sc->sda_pin = (int)pin;
128 if (sc->scl_pin < 0 || sc->scl_pin > 1)
129 sc->scl_pin = GPIOIIC_SCL_DFLT;
130 if (sc->sda_pin < 0 || sc->sda_pin > 1)
131 sc->sda_pin = GPIOIIC_SDA_DFLT;
133 devi = GPIOBUS_IVAR(dev);
134 device_printf(dev, "SCL pin: %d, SDA pin: %d\n",
135 devi->pins[sc->scl_pin], devi->pins[sc->sda_pin]);
137 /* add generic bit-banging code */
138 bitbang = device_add_child(dev, "iicbb", -1);
139 device_probe_and_attach(bitbang);
145 * Reset bus by setting SDA first and then SCL.
146 * Must always be called with gpio bus locked.
149 gpioiic_reset_bus(device_t dev)
151 struct gpioiic_softc *sc = device_get_softc(dev);
153 GPIOBUS_PIN_SETFLAGS(sc->sc_busdev, sc->sc_dev, sc->sda_pin,
155 GPIOBUS_PIN_SETFLAGS(sc->sc_busdev, sc->sc_dev, sc->scl_pin,
160 gpioiic_setsda(device_t dev, int val)
162 struct gpioiic_softc *sc = device_get_softc(dev);
165 GPIOBUS_PIN_SET(sc->sc_busdev, sc->sc_dev, sc->sda_pin, 0);
166 GPIOBUS_PIN_SETFLAGS(sc->sc_busdev, sc->sc_dev, sc->sda_pin,
169 GPIOBUS_PIN_SETFLAGS(sc->sc_busdev, sc->sc_dev, sc->sda_pin,
175 gpioiic_setscl(device_t dev, int val)
177 struct gpioiic_softc *sc = device_get_softc(dev);
180 GPIOBUS_PIN_SET(sc->sc_busdev, sc->sc_dev, sc->scl_pin, 0);
181 GPIOBUS_PIN_SETFLAGS(sc->sc_busdev, sc->sc_dev, sc->scl_pin,
184 GPIOBUS_PIN_SETFLAGS(sc->sc_busdev, sc->sc_dev, sc->scl_pin,
190 gpioiic_getscl(device_t dev)
192 struct gpioiic_softc *sc = device_get_softc(dev);
195 GPIOBUS_PIN_SETFLAGS(sc->sc_busdev, sc->sc_dev, sc->scl_pin,
197 GPIOBUS_PIN_GET(sc->sc_busdev, sc->sc_dev, sc->scl_pin, &val);
203 gpioiic_getsda(device_t dev)
205 struct gpioiic_softc *sc = device_get_softc(dev);
208 GPIOBUS_PIN_SETFLAGS(sc->sc_busdev, sc->sc_dev, sc->sda_pin,
210 GPIOBUS_PIN_GET(sc->sc_busdev, sc->sc_dev, sc->sda_pin, &val);
216 gpioiic_reset(device_t dev, u_char speed, u_char addr, u_char *oldaddr)
218 struct gpioiic_softc *sc;
220 sc = device_get_softc(dev);
221 gpioiic_reset_bus(sc->sc_dev);
223 return (IIC_ENOADDR);
228 gpioiic_get_node(device_t bus, device_t dev)
231 /* We only have one child, the iicbb, which needs our own node. */
232 return (ofw_bus_get_node(bus));
236 static devclass_t gpioiic_devclass;
238 static device_method_t gpioiic_methods[] = {
239 /* Device interface */
240 DEVMETHOD(device_probe, gpioiic_probe),
241 DEVMETHOD(device_attach, gpioiic_attach),
242 DEVMETHOD(device_detach, bus_generic_detach),
244 /* iicbb interface */
245 DEVMETHOD(iicbb_setsda, gpioiic_setsda),
246 DEVMETHOD(iicbb_setscl, gpioiic_setscl),
247 DEVMETHOD(iicbb_getsda, gpioiic_getsda),
248 DEVMETHOD(iicbb_getscl, gpioiic_getscl),
249 DEVMETHOD(iicbb_reset, gpioiic_reset),
252 /* OFW bus interface */
253 DEVMETHOD(ofw_bus_get_node, gpioiic_get_node),
259 static driver_t gpioiic_driver = {
262 sizeof(struct gpioiic_softc),
265 DRIVER_MODULE(gpioiic, gpiobus, gpioiic_driver, gpioiic_devclass, 0, 0);
266 DRIVER_MODULE(iicbb, gpioiic, iicbb_driver, iicbb_devclass, 0, 0);
267 MODULE_DEPEND(gpioiic, iicbb, IICBB_MINVER, IICBB_PREFVER, IICBB_MAXVER);
268 MODULE_DEPEND(gpioiic, gpiobus, 1, 1, 1);