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 "gpiobus_if.h"
44 #include <dev/ofw/ofw_bus.h>
45 #include <dev/ofw/ofw_bus_subr.h>
46 #include <dev/fdt/fdt_common.h>
49 #include <dev/gpio/gpiobusvar.h>
51 #include <dev/iicbus/iiconf.h>
52 #include <dev/iicbus/iicbus.h>
56 #define SCL_PIN_DEFAULT 0 /* default index of SCL pin on gpiobus */
57 #define SDA_PIN_DEFAULT 1
67 static int gpioiic_probe(device_t);
68 static int gpioiic_attach(device_t);
71 static void gpioiic_reset_bus(device_t);
72 static int gpioiic_callback(device_t, int, caddr_t);
73 static void gpioiic_setsda(device_t, int);
74 static void gpioiic_setscl(device_t, int);
75 static int gpioiic_getsda(device_t);
76 static int gpioiic_getscl(device_t);
77 static int gpioiic_reset(device_t, u_char, u_char, u_char *);
80 gpioiic_probe(device_t dev)
84 if (!ofw_bus_is_compatible(dev, "gpioiic"))
87 device_set_desc(dev, "GPIO I2C bit-banging driver");
93 gpioiic_attach(device_t dev)
100 struct gpiobus_ivar *devi;
101 struct gpioiic_softc *sc;
103 sc = device_get_softc(dev);
105 sc->sc_busdev = device_get_parent(dev);
106 if (resource_int_value(device_get_name(dev),
107 device_get_unit(dev), "scl", &sc->scl_pin))
108 sc->scl_pin = SCL_PIN_DEFAULT;
109 if (resource_int_value(device_get_name(dev),
110 device_get_unit(dev), "sda", &sc->sda_pin))
111 sc->sda_pin = SDA_PIN_DEFAULT;
114 if ((node = ofw_bus_get_node(dev)) == -1)
116 if (OF_getencprop(node, "scl", &pin, sizeof(pin)) > 0)
117 sc->scl_pin = (int)pin;
118 if (OF_getencprop(node, "sda", &pin, sizeof(pin)) > 0)
119 sc->sda_pin = (int)pin;
122 if (sc->scl_pin < 0 || sc->scl_pin > 1)
123 sc->scl_pin = SCL_PIN_DEFAULT;
124 if (sc->sda_pin < 0 || sc->sda_pin > 1)
125 sc->sda_pin = SDA_PIN_DEFAULT;
127 devi = GPIOBUS_IVAR(dev);
128 device_printf(dev, "SCL pin: %d, SDA pin: %d\n",
129 devi->pins[sc->scl_pin], devi->pins[sc->sda_pin]);
131 /* add generic bit-banging code */
132 bitbang = device_add_child(dev, "iicbb", -1);
133 device_probe_and_attach(bitbang);
139 * Reset bus by setting SDA first and then SCL.
140 * Must always be called with gpio bus locked.
143 gpioiic_reset_bus(device_t dev)
145 struct gpioiic_softc *sc = device_get_softc(dev);
147 GPIOBUS_PIN_SETFLAGS(sc->sc_busdev, sc->sc_dev, sc->sda_pin,
149 GPIOBUS_PIN_SETFLAGS(sc->sc_busdev, sc->sc_dev, sc->scl_pin,
154 gpioiic_callback(device_t dev, int index, caddr_t data)
156 struct gpioiic_softc *sc = device_get_softc(dev);
159 how = GPIOBUS_DONTWAIT;
160 if (data != NULL && (int)*data == IIC_WAIT)
164 case IIC_REQUEST_BUS:
165 error = GPIOBUS_ACQUIRE_BUS(sc->sc_busdev, sc->sc_dev, how);
167 case IIC_RELEASE_BUS:
168 GPIOBUS_RELEASE_BUS(sc->sc_busdev, sc->sc_dev);
178 gpioiic_setsda(device_t dev, int val)
180 struct gpioiic_softc *sc = device_get_softc(dev);
183 GPIOBUS_PIN_SET(sc->sc_busdev, sc->sc_dev, sc->sda_pin, 0);
184 GPIOBUS_PIN_SETFLAGS(sc->sc_busdev, sc->sc_dev, sc->sda_pin,
187 GPIOBUS_PIN_SETFLAGS(sc->sc_busdev, sc->sc_dev, sc->sda_pin,
193 gpioiic_setscl(device_t dev, int val)
195 struct gpioiic_softc *sc = device_get_softc(dev);
198 GPIOBUS_PIN_SET(sc->sc_busdev, sc->sc_dev, sc->scl_pin, 0);
199 GPIOBUS_PIN_SETFLAGS(sc->sc_busdev, sc->sc_dev, sc->scl_pin,
202 GPIOBUS_PIN_SETFLAGS(sc->sc_busdev, sc->sc_dev, sc->scl_pin,
208 gpioiic_getscl(device_t dev)
210 struct gpioiic_softc *sc = device_get_softc(dev);
213 GPIOBUS_PIN_SETFLAGS(sc->sc_busdev, sc->sc_dev, sc->scl_pin,
215 GPIOBUS_PIN_GET(sc->sc_busdev, sc->sc_dev, sc->scl_pin, &val);
221 gpioiic_getsda(device_t dev)
223 struct gpioiic_softc *sc = device_get_softc(dev);
226 GPIOBUS_PIN_SETFLAGS(sc->sc_busdev, sc->sc_dev, sc->sda_pin,
228 GPIOBUS_PIN_GET(sc->sc_busdev, sc->sc_dev, sc->sda_pin, &val);
234 gpioiic_reset(device_t dev, u_char speed, u_char addr, u_char *oldaddr)
236 struct gpioiic_softc *sc;
238 sc = device_get_softc(dev);
239 gpioiic_reset_bus(sc->sc_dev);
241 return (IIC_ENOADDR);
246 gpioiic_get_node(device_t bus, device_t dev)
249 /* We only have one child, the iicbb, which needs our own node. */
250 return (ofw_bus_get_node(bus));
254 static devclass_t gpioiic_devclass;
256 static device_method_t gpioiic_methods[] = {
257 /* Device interface */
258 DEVMETHOD(device_probe, gpioiic_probe),
259 DEVMETHOD(device_attach, gpioiic_attach),
260 DEVMETHOD(device_detach, bus_generic_detach),
262 /* iicbb interface */
263 DEVMETHOD(iicbb_callback, gpioiic_callback),
264 DEVMETHOD(iicbb_setsda, gpioiic_setsda),
265 DEVMETHOD(iicbb_setscl, gpioiic_setscl),
266 DEVMETHOD(iicbb_getsda, gpioiic_getsda),
267 DEVMETHOD(iicbb_getscl, gpioiic_getscl),
268 DEVMETHOD(iicbb_reset, gpioiic_reset),
271 /* OFW bus interface */
272 DEVMETHOD(ofw_bus_get_node, gpioiic_get_node),
278 static driver_t gpioiic_driver = {
281 sizeof(struct gpioiic_softc),
284 DRIVER_MODULE(gpioiic, gpiobus, gpioiic_driver, gpioiic_devclass, 0, 0);
285 DRIVER_MODULE(iicbb, gpioiic, iicbb_driver, iicbb_devclass, 0, 0);
286 MODULE_DEPEND(gpioiic, iicbb, IICBB_MINVER, IICBB_PREFVER, IICBB_MAXVER);
287 MODULE_DEPEND(gpioiic, gpiobus, 1, 1, 1);