1 /*- 2 * Copyright (c) 1998, 2001 Nicolas Souchu, Marc Bouget 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 * SUCH DAMAGE. 25 * 26 * 27 */ 28 29 #include <sys/cdefs.h> 30 __FBSDID("$FreeBSD$"); 31 32 /* 33 * I2C Bit-Banging over parallel port 34 * 35 * See the Official Philips interface description in lpbb(4) 36 */ 37 38 #include <sys/param.h> 39 #include <sys/bus.h> 40 #include <sys/lock.h> 41 #include <sys/kernel.h> 42 #include <sys/module.h> 43 #include <sys/mutex.h> 44 #include <sys/systm.h> 45 #include <sys/uio.h> 46 47 48 #include <dev/ppbus/ppbconf.h> 49 #include "ppbus_if.h" 50 #include <dev/ppbus/ppbio.h> 51 52 #include <dev/iicbus/iiconf.h> 53 #include <dev/iicbus/iicbus.h> 54 55 #include "iicbb_if.h" 56 57 static int lpbb_detect(device_t dev); 58 59 static void 60 lpbb_identify(driver_t *driver, device_t parent) 61 { 62 63 device_t dev; 64 65 dev = device_find_child(parent, "lpbb", -1); 66 if (!dev) 67 BUS_ADD_CHILD(parent, 0, "lpbb", -1); 68 } 69 70 static int 71 lpbb_probe(device_t dev) 72 { 73 74 /* Perhaps call this during identify instead? */ 75 if (!lpbb_detect(dev)) 76 return (ENXIO); 77 78 device_set_desc(dev, "Parallel I2C bit-banging interface"); 79 80 return (0); 81 } 82 83 static int 84 lpbb_attach(device_t dev) 85 { 86 device_t bitbang; 87 88 /* add generic bit-banging code */ 89 bitbang = device_add_child(dev, "iicbb", -1); 90 device_probe_and_attach(bitbang); 91 92 return (0); 93 } 94 95 static int 96 lpbb_callback(device_t dev, int index, caddr_t *data) 97 { 98 device_t ppbus = device_get_parent(dev); 99 int error = 0; 100 int how; 101 102 switch (index) { 103 case IIC_REQUEST_BUS: 104 /* request the ppbus */ 105 how = *(int *)data; 106 mtx_lock(&Giant); 107 error = ppb_request_bus(ppbus, dev, how); 108 mtx_unlock(&Giant); 109 break; 110 111 case IIC_RELEASE_BUS: 112 /* release the ppbus */ 113 mtx_lock(&Giant); 114 error = ppb_release_bus(ppbus, dev); 115 mtx_unlock(&Giant); 116 break; 117 118 default: 119 error = EINVAL; 120 } 121 122 return (error); 123 } 124 125 #define SDA_out 0x80 126 #define SCL_out 0x08 127 #define SDA_in 0x80 128 #define SCL_in 0x08 129 #define ALIM 0x20 130 #define I2CKEY 0x50 131 132 static int 133 lpbb_getscl(device_t dev) 134 { 135 int rval; 136 137 mtx_lock(&Giant); 138 rval = ((ppb_rstr(device_get_parent(dev)) & SCL_in) == SCL_in); 139 mtx_unlock(&Giant); 140 return (rval); 141 } 142 143 static int 144 lpbb_getsda(device_t dev) 145 { 146 int rval; 147 148 mtx_lock(&Giant); 149 rval = ((ppb_rstr(device_get_parent(dev)) & SDA_in) == SDA_in); 150 mtx_unlock(&Giant); 151 return (rval); 152 } 153 154 static void 155 lpbb_setsda(device_t dev, char val) 156 { 157 device_t ppbus = device_get_parent(dev); 158 159 mtx_lock(&Giant); 160 if (val == 0) 161 ppb_wdtr(ppbus, (u_char)SDA_out); 162 else 163 ppb_wdtr(ppbus, (u_char)~SDA_out); 164 mtx_unlock(&Giant); 165 } 166 167 static void 168 lpbb_setscl(device_t dev, unsigned char val) 169 { 170 device_t ppbus = device_get_parent(dev); 171 172 mtx_lock(&Giant); 173 if (val == 0) 174 ppb_wctr(ppbus, (u_char)(ppb_rctr(ppbus) & ~SCL_out)); 175 else 176 ppb_wctr(ppbus, (u_char)(ppb_rctr(ppbus) | SCL_out)); 177 mtx_unlock(&Giant); 178 } 179 180 static int 181 lpbb_detect(device_t dev) 182 { 183 device_t ppbus = device_get_parent(dev); 184 185 if (ppb_request_bus(ppbus, dev, PPB_DONTWAIT)) { 186 device_printf(dev, "can't allocate ppbus\n"); 187 return (0); 188 } 189 190 /* reset bus */ 191 lpbb_setsda(dev, 1); 192 lpbb_setscl(dev, 1); 193 194 if ((ppb_rstr(ppbus) & I2CKEY) || 195 ((ppb_rstr(ppbus) & ALIM) != ALIM)) { 196 197 ppb_release_bus(ppbus, dev); 198 return (0); 199 } 200 201 ppb_release_bus(ppbus, dev); 202 203 return (1); 204 } 205 206 static int 207 lpbb_reset(device_t dev, u_char speed, u_char addr, u_char * oldaddr) 208 { 209 device_t ppbus = device_get_parent(dev); 210 211 mtx_lock(&Giant); 212 if (ppb_request_bus(ppbus, dev, PPB_DONTWAIT)) { 213 device_printf(dev, "can't allocate ppbus\n"); 214 return (0); 215 } 216 217 /* reset bus */ 218 lpbb_setsda(dev, 1); 219 lpbb_setscl(dev, 1); 220 221 ppb_release_bus(ppbus, dev); 222 mtx_unlock(&Giant); 223 224 return (IIC_ENOADDR); 225 } 226 227 static devclass_t lpbb_devclass; 228 229 static device_method_t lpbb_methods[] = { 230 /* device interface */ 231 DEVMETHOD(device_identify, lpbb_identify), 232 DEVMETHOD(device_probe, lpbb_probe), 233 DEVMETHOD(device_attach, lpbb_attach), 234 235 /* bus interface */ 236 DEVMETHOD(bus_print_child, bus_generic_print_child), 237 238 /* iicbb interface */ 239 DEVMETHOD(iicbb_callback, lpbb_callback), 240 DEVMETHOD(iicbb_setsda, lpbb_setsda), 241 DEVMETHOD(iicbb_setscl, lpbb_setscl), 242 DEVMETHOD(iicbb_getsda, lpbb_getsda), 243 DEVMETHOD(iicbb_getscl, lpbb_getscl), 244 DEVMETHOD(iicbb_reset, lpbb_reset), 245 246 { 0, 0 } 247 }; 248 249 static driver_t lpbb_driver = { 250 "lpbb", 251 lpbb_methods, 252 1, 253 }; 254 255 DRIVER_MODULE(lpbb, ppbus, lpbb_driver, lpbb_devclass, 0, 0); 256 DRIVER_MODULE(iicbb, lpbb, iicbb_driver, iicbb_devclass, 0, 0); 257 MODULE_DEPEND(lpbb, ppbus, 1, 1, 1); 258 MODULE_DEPEND(lpbb, iicbb, IICBB_MINVER, IICBB_PREFVER, IICBB_MAXVER); 259 MODULE_VERSION(lpbb, 1); 260