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 <sys/param.h>
32 #include <sys/systm.h>
36 #include <sys/kernel.h>
37 #include <sys/kthread.h>
39 #include <sys/malloc.h>
40 #include <sys/module.h>
41 #include <sys/mutex.h>
44 #include "gpiobus_if.h"
46 #include <dev/iicbus/iiconf.h>
47 #include <dev/iicbus/iicbus.h>
51 #define SCL_PIN 0 /* gpiobus mapped pin 6 */
52 #define SDA_PIN 1 /* gpiobus mapped pin 7 */
59 struct cdev *sc_leddev;
62 static int gpioiic_probe(device_t);
63 static int gpioiic_attach(device_t);
66 static void gpioiic_reset_bus(device_t);
67 static int gpioiic_callback(device_t, int, caddr_t);
68 static void gpioiic_setsda(device_t, int);
69 static void gpioiic_setscl(device_t, int);
70 static int gpioiic_getsda(device_t);
71 static int gpioiic_getscl(device_t);
72 static int gpioiic_reset(device_t, u_char, u_char, u_char *);
76 gpioiic_probe(device_t dev)
79 device_set_desc(dev, "GPIO I2C bit-banging driver");
84 gpioiic_attach(device_t dev)
86 struct gpioiic_softc *sc = device_get_softc(dev);
90 sc->sc_busdev = device_get_parent(dev);
92 /* add generic bit-banging code */
93 bitbang = device_add_child(dev, "iicbb", -1);
94 device_probe_and_attach(bitbang);
100 * Reset bus by setting SDA first and then SCL.
101 * Must always be called with gpio bus locked.
104 gpioiic_reset_bus(device_t dev)
106 struct gpioiic_softc *sc = device_get_softc(dev);
108 GPIOBUS_PIN_SETFLAGS(sc->sc_busdev, sc->sc_dev, SDA_PIN,
110 GPIOBUS_PIN_SETFLAGS(sc->sc_busdev, sc->sc_dev, SCL_PIN,
115 gpioiic_callback(device_t dev, int index, caddr_t data)
117 struct gpioiic_softc *sc = device_get_softc(dev);
121 case IIC_REQUEST_BUS:
122 GPIOBUS_LOCK_BUS(sc->sc_busdev);
123 GPIOBUS_ACQUIRE_BUS(sc->sc_busdev, sc->sc_dev);
124 GPIOBUS_UNLOCK_BUS(sc->sc_busdev);
126 case IIC_RELEASE_BUS:
127 GPIOBUS_LOCK_BUS(sc->sc_busdev);
128 GPIOBUS_RELEASE_BUS(sc->sc_busdev, sc->sc_dev);
129 GPIOBUS_UNLOCK_BUS(sc->sc_busdev);
139 gpioiic_setsda(device_t dev, int val)
141 struct gpioiic_softc *sc = device_get_softc(dev);
143 GPIOBUS_LOCK_BUS(sc->sc_busdev);
145 GPIOBUS_PIN_SET(sc->sc_busdev, sc->sc_dev, SDA_PIN, 0);
146 GPIOBUS_PIN_SETFLAGS(sc->sc_busdev, sc->sc_dev, SDA_PIN,
149 GPIOBUS_PIN_SETFLAGS(sc->sc_busdev, sc->sc_dev, SDA_PIN,
152 GPIOBUS_UNLOCK_BUS(sc->sc_busdev);
156 gpioiic_setscl(device_t dev, int val)
158 struct gpioiic_softc *sc = device_get_softc(dev);
160 GPIOBUS_LOCK_BUS(sc->sc_busdev);
162 GPIOBUS_PIN_SET(sc->sc_busdev, sc->sc_dev, SCL_PIN, 0);
163 GPIOBUS_PIN_SETFLAGS(sc->sc_busdev, sc->sc_dev, SCL_PIN,
166 GPIOBUS_PIN_SETFLAGS(sc->sc_busdev, sc->sc_dev, SCL_PIN,
169 GPIOBUS_UNLOCK_BUS(sc->sc_busdev);
173 gpioiic_getscl(device_t dev)
175 struct gpioiic_softc *sc = device_get_softc(dev);
178 GPIOBUS_LOCK_BUS(sc->sc_busdev);
179 GPIOBUS_PIN_SETFLAGS(sc->sc_busdev, sc->sc_dev, SCL_PIN,
181 GPIOBUS_PIN_GET(sc->sc_busdev, sc->sc_dev, SCL_PIN, &val);
182 GPIOBUS_UNLOCK_BUS(sc->sc_busdev);
188 gpioiic_getsda(device_t dev)
190 struct gpioiic_softc *sc = device_get_softc(dev);
193 GPIOBUS_LOCK_BUS(sc->sc_busdev);
194 GPIOBUS_PIN_SETFLAGS(sc->sc_busdev, sc->sc_dev, SDA_PIN,
196 GPIOBUS_PIN_GET(sc->sc_busdev, sc->sc_dev, SDA_PIN, &val);
197 GPIOBUS_UNLOCK_BUS(sc->sc_busdev);
203 gpioiic_reset(device_t dev, u_char speed, u_char addr, u_char *oldaddr)
205 struct gpioiic_softc *sc = device_get_softc(dev);
207 GPIOBUS_LOCK_BUS(sc->sc_busdev);
208 GPIOBUS_ACQUIRE_BUS(sc->sc_busdev, sc->sc_dev);
210 gpioiic_reset_bus(sc->sc_dev);
212 GPIOBUS_RELEASE_BUS(sc->sc_busdev, sc->sc_dev);
213 GPIOBUS_UNLOCK_BUS(sc->sc_busdev);
215 return (IIC_ENOADDR);
218 static devclass_t gpioiic_devclass;
220 static device_method_t gpioiic_methods[] = {
221 /* Device interface */
222 DEVMETHOD(device_probe, gpioiic_probe),
223 DEVMETHOD(device_attach, gpioiic_attach),
224 DEVMETHOD(device_detach, bus_generic_detach),
226 /* iicbb interface */
227 DEVMETHOD(iicbb_callback, gpioiic_callback),
228 DEVMETHOD(iicbb_setsda, gpioiic_setsda),
229 DEVMETHOD(iicbb_setscl, gpioiic_setscl),
230 DEVMETHOD(iicbb_getsda, gpioiic_getsda),
231 DEVMETHOD(iicbb_getscl, gpioiic_getscl),
232 DEVMETHOD(iicbb_reset, gpioiic_reset),
237 static driver_t gpioiic_driver = {
240 sizeof(struct gpioiic_softc),
243 DRIVER_MODULE(gpioiic, gpiobus, gpioiic_driver, gpioiic_devclass, 0, 0);
244 DRIVER_MODULE(iicbb, gpioiic, iicbb_driver, iicbb_devclass, 0, 0);
245 MODULE_DEPEND(gpioiic, iicbb, IICBB_MINVER, IICBB_PREFVER, IICBB_MAXVER);
246 MODULE_DEPEND(gpioiic, gpiobus, 1, 1, 1);