1 /*- 2 * Copyright (c) 2009 Oleksandr Tymoshenko <gonzo@freebsd.org> 3 * Copyright (c) 2010 Luiz Otavio O Souza 4 * All rights reserved. 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 #include <sys/cdefs.h> 29 __FBSDID("$FreeBSD$"); 30 31 #include <sys/param.h> 32 #include <sys/systm.h> 33 #include <sys/bio.h> 34 #include <sys/bus.h> 35 #include <sys/conf.h> 36 #include <sys/kernel.h> 37 #include <sys/kthread.h> 38 #include <sys/lock.h> 39 #include <sys/malloc.h> 40 #include <sys/module.h> 41 #include <sys/mutex.h> 42 43 #include <sys/gpio.h> 44 #include "gpiobus_if.h" 45 46 #include <dev/iicbus/iiconf.h> 47 #include <dev/iicbus/iicbus.h> 48 49 #include "iicbb_if.h" 50 51 #define SCL_PIN_DEFAULT 0 /* default index of SCL pin on gpiobus */ 52 #define SDA_PIN_DEFAULT 1 53 54 struct gpioiic_softc 55 { 56 device_t sc_dev; 57 device_t sc_busdev; 58 struct mtx sc_mtx; 59 struct cdev *sc_leddev; 60 int scl_pin; 61 int sda_pin; 62 }; 63 64 static int gpioiic_probe(device_t); 65 static int gpioiic_attach(device_t); 66 67 /* iicbb interface */ 68 static void gpioiic_reset_bus(device_t); 69 static int gpioiic_callback(device_t, int, caddr_t); 70 static void gpioiic_setsda(device_t, int); 71 static void gpioiic_setscl(device_t, int); 72 static int gpioiic_getsda(device_t); 73 static int gpioiic_getscl(device_t); 74 static int gpioiic_reset(device_t, u_char, u_char, u_char *); 75 76 77 static int 78 gpioiic_probe(device_t dev) 79 { 80 81 device_set_desc(dev, "GPIO I2C bit-banging driver"); 82 return (0); 83 } 84 85 static int 86 gpioiic_attach(device_t dev) 87 { 88 struct gpioiic_softc *sc = device_get_softc(dev); 89 device_t bitbang; 90 91 sc->sc_dev = dev; 92 sc->sc_busdev = device_get_parent(dev); 93 if (resource_int_value(device_get_name(dev), 94 device_get_unit(dev), "scl", &sc->scl_pin)) 95 sc->scl_pin = SCL_PIN_DEFAULT; 96 if (resource_int_value(device_get_name(dev), 97 device_get_unit(dev), "sda", &sc->sda_pin)) 98 sc->sda_pin = SDA_PIN_DEFAULT; 99 100 /* add generic bit-banging code */ 101 bitbang = device_add_child(dev, "iicbb", -1); 102 device_probe_and_attach(bitbang); 103 104 return (0); 105 } 106 107 /* 108 * Reset bus by setting SDA first and then SCL. 109 * Must always be called with gpio bus locked. 110 */ 111 static void 112 gpioiic_reset_bus(device_t dev) 113 { 114 struct gpioiic_softc *sc = device_get_softc(dev); 115 116 GPIOBUS_PIN_SETFLAGS(sc->sc_busdev, sc->sc_dev, sc->sda_pin, 117 GPIO_PIN_INPUT); 118 GPIOBUS_PIN_SETFLAGS(sc->sc_busdev, sc->sc_dev, sc->scl_pin, 119 GPIO_PIN_INPUT); 120 } 121 122 static int 123 gpioiic_callback(device_t dev, int index, caddr_t data) 124 { 125 struct gpioiic_softc *sc = device_get_softc(dev); 126 int error = 0; 127 128 switch (index) { 129 case IIC_REQUEST_BUS: 130 GPIOBUS_LOCK_BUS(sc->sc_busdev); 131 GPIOBUS_ACQUIRE_BUS(sc->sc_busdev, sc->sc_dev); 132 GPIOBUS_UNLOCK_BUS(sc->sc_busdev); 133 break; 134 case IIC_RELEASE_BUS: 135 GPIOBUS_LOCK_BUS(sc->sc_busdev); 136 GPIOBUS_RELEASE_BUS(sc->sc_busdev, sc->sc_dev); 137 GPIOBUS_UNLOCK_BUS(sc->sc_busdev); 138 break; 139 default: 140 error = EINVAL; 141 } 142 143 return(error); 144 } 145 146 static void 147 gpioiic_setsda(device_t dev, int val) 148 { 149 struct gpioiic_softc *sc = device_get_softc(dev); 150 151 GPIOBUS_LOCK_BUS(sc->sc_busdev); 152 if (val == 0) { 153 GPIOBUS_PIN_SET(sc->sc_busdev, sc->sc_dev, sc->sda_pin, 0); 154 GPIOBUS_PIN_SETFLAGS(sc->sc_busdev, sc->sc_dev, sc->sda_pin, 155 GPIO_PIN_OUTPUT); 156 } else { 157 GPIOBUS_PIN_SETFLAGS(sc->sc_busdev, sc->sc_dev, sc->sda_pin, 158 GPIO_PIN_INPUT); 159 } 160 GPIOBUS_UNLOCK_BUS(sc->sc_busdev); 161 } 162 163 static void 164 gpioiic_setscl(device_t dev, int val) 165 { 166 struct gpioiic_softc *sc = device_get_softc(dev); 167 168 GPIOBUS_LOCK_BUS(sc->sc_busdev); 169 if (val == 0) { 170 GPIOBUS_PIN_SET(sc->sc_busdev, sc->sc_dev, sc->scl_pin, 0); 171 GPIOBUS_PIN_SETFLAGS(sc->sc_busdev, sc->sc_dev, sc->scl_pin, 172 GPIO_PIN_OUTPUT); 173 } else { 174 GPIOBUS_PIN_SETFLAGS(sc->sc_busdev, sc->sc_dev, sc->scl_pin, 175 GPIO_PIN_INPUT); 176 } 177 GPIOBUS_UNLOCK_BUS(sc->sc_busdev); 178 } 179 180 static int 181 gpioiic_getscl(device_t dev) 182 { 183 struct gpioiic_softc *sc = device_get_softc(dev); 184 unsigned int val; 185 186 GPIOBUS_LOCK_BUS(sc->sc_busdev); 187 GPIOBUS_PIN_SETFLAGS(sc->sc_busdev, sc->sc_dev, sc->scl_pin, 188 GPIO_PIN_INPUT); 189 GPIOBUS_PIN_GET(sc->sc_busdev, sc->sc_dev, sc->scl_pin, &val); 190 GPIOBUS_UNLOCK_BUS(sc->sc_busdev); 191 192 return ((int)val); 193 } 194 195 static int 196 gpioiic_getsda(device_t dev) 197 { 198 struct gpioiic_softc *sc = device_get_softc(dev); 199 unsigned int val; 200 201 GPIOBUS_LOCK_BUS(sc->sc_busdev); 202 GPIOBUS_PIN_SETFLAGS(sc->sc_busdev, sc->sc_dev, sc->sda_pin, 203 GPIO_PIN_INPUT); 204 GPIOBUS_PIN_GET(sc->sc_busdev, sc->sc_dev, sc->sda_pin, &val); 205 GPIOBUS_UNLOCK_BUS(sc->sc_busdev); 206 207 return ((int)val); 208 } 209 210 static int 211 gpioiic_reset(device_t dev, u_char speed, u_char addr, u_char *oldaddr) 212 { 213 struct gpioiic_softc *sc = device_get_softc(dev); 214 215 GPIOBUS_LOCK_BUS(sc->sc_busdev); 216 GPIOBUS_ACQUIRE_BUS(sc->sc_busdev, sc->sc_dev); 217 218 gpioiic_reset_bus(sc->sc_dev); 219 220 GPIOBUS_RELEASE_BUS(sc->sc_busdev, sc->sc_dev); 221 GPIOBUS_UNLOCK_BUS(sc->sc_busdev); 222 223 return (IIC_ENOADDR); 224 } 225 226 static devclass_t gpioiic_devclass; 227 228 static device_method_t gpioiic_methods[] = { 229 /* Device interface */ 230 DEVMETHOD(device_probe, gpioiic_probe), 231 DEVMETHOD(device_attach, gpioiic_attach), 232 DEVMETHOD(device_detach, bus_generic_detach), 233 234 /* iicbb interface */ 235 DEVMETHOD(iicbb_callback, gpioiic_callback), 236 DEVMETHOD(iicbb_setsda, gpioiic_setsda), 237 DEVMETHOD(iicbb_setscl, gpioiic_setscl), 238 DEVMETHOD(iicbb_getsda, gpioiic_getsda), 239 DEVMETHOD(iicbb_getscl, gpioiic_getscl), 240 DEVMETHOD(iicbb_reset, gpioiic_reset), 241 242 { 0, 0 } 243 }; 244 245 static driver_t gpioiic_driver = { 246 "gpioiic", 247 gpioiic_methods, 248 sizeof(struct gpioiic_softc), 249 }; 250 251 DRIVER_MODULE(gpioiic, gpiobus, gpioiic_driver, gpioiic_devclass, 0, 0); 252 DRIVER_MODULE(iicbb, gpioiic, iicbb_driver, iicbb_devclass, 0, 0); 253 MODULE_DEPEND(gpioiic, iicbb, IICBB_MINVER, IICBB_PREFVER, IICBB_MAXVER); 254 MODULE_DEPEND(gpioiic, gpiobus, 1, 1, 1); 255