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/kernel.h> 40 #include <sys/systm.h> 41 #include <sys/module.h> 42 #include <sys/bus.h> 43 #include <sys/uio.h> 44 45 46 #include <dev/ppbus/ppbconf.h> 47 #include "ppbus_if.h" 48 #include <dev/ppbus/ppbio.h> 49 50 #include <dev/iicbus/iiconf.h> 51 #include <dev/iicbus/iicbus.h> 52 53 #include "iicbb_if.h" 54 55 static int lpbb_detect(device_t dev); 56 57 static void 58 lpbb_identify(driver_t *driver, device_t parent) 59 { 60 61 device_t dev; 62 63 dev = device_find_child(parent, "lpbb", 0); 64 if (!dev) 65 BUS_ADD_CHILD(parent, 0, "lpbb", -1); 66 } 67 68 static int 69 lpbb_probe(device_t dev) 70 { 71 72 /* Perhaps call this during identify instead? */ 73 if (!lpbb_detect(dev)) 74 return (ENXIO); 75 76 device_set_desc(dev, "Parallel I2C bit-banging interface"); 77 78 return (0); 79 } 80 81 static int 82 lpbb_attach(device_t dev) 83 { 84 device_t bitbang; 85 86 /* add generic bit-banging code */ 87 bitbang = device_add_child(dev, "iicbb", -1); 88 device_probe_and_attach(bitbang); 89 90 return (0); 91 } 92 93 static int 94 lpbb_callback(device_t dev, int index, caddr_t *data) 95 { 96 device_t ppbus = device_get_parent(dev); 97 int error = 0; 98 int how; 99 100 switch (index) { 101 case IIC_REQUEST_BUS: 102 /* request the ppbus */ 103 how = *(int *)data; 104 error = ppb_request_bus(ppbus, dev, how); 105 break; 106 107 case IIC_RELEASE_BUS: 108 /* release the ppbus */ 109 error = ppb_release_bus(ppbus, dev); 110 break; 111 112 default: 113 error = EINVAL; 114 } 115 116 return (error); 117 } 118 119 #define SDA_out 0x80 120 #define SCL_out 0x08 121 #define SDA_in 0x80 122 #define SCL_in 0x08 123 #define ALIM 0x20 124 #define I2CKEY 0x50 125 126 static int lpbb_getscl(device_t dev) 127 { 128 return ((ppb_rstr(device_get_parent(dev)) & SCL_in) == SCL_in); 129 } 130 131 static int lpbb_getsda(device_t dev) 132 { 133 return ((ppb_rstr(device_get_parent(dev)) & SDA_in) == SDA_in); 134 } 135 136 static void lpbb_setsda(device_t dev, char val) 137 { 138 device_t ppbus = device_get_parent(dev); 139 140 if(val==0) 141 ppb_wdtr(ppbus, (u_char)SDA_out); 142 else 143 ppb_wdtr(ppbus, (u_char)~SDA_out); 144 } 145 146 static void lpbb_setscl(device_t dev, unsigned char val) 147 { 148 device_t ppbus = device_get_parent(dev); 149 150 if(val==0) 151 ppb_wctr(ppbus, (u_char)(ppb_rctr(ppbus)&~SCL_out)); 152 else 153 ppb_wctr(ppbus, (u_char)(ppb_rctr(ppbus)|SCL_out)); 154 } 155 156 static int lpbb_detect(device_t dev) 157 { 158 device_t ppbus = device_get_parent(dev); 159 160 if (ppb_request_bus(ppbus, dev, PPB_DONTWAIT)) { 161 device_printf(dev, "can't allocate ppbus\n"); 162 return (0); 163 } 164 165 /* reset bus */ 166 lpbb_setsda(dev, 1); 167 lpbb_setscl(dev, 1); 168 169 if ((ppb_rstr(ppbus) & I2CKEY) || 170 ((ppb_rstr(ppbus) & ALIM) != ALIM)) { 171 172 ppb_release_bus(ppbus, dev); 173 return (0); 174 } 175 176 ppb_release_bus(ppbus, dev); 177 178 return (1); 179 } 180 181 static int 182 lpbb_reset(device_t dev, u_char speed, u_char addr, u_char * oldaddr) 183 { 184 device_t ppbus = device_get_parent(dev); 185 186 if (ppb_request_bus(ppbus, dev, PPB_DONTWAIT)) { 187 device_printf(dev, "can't allocate ppbus\n"); 188 return (0); 189 } 190 191 /* reset bus */ 192 lpbb_setsda(dev, 1); 193 lpbb_setscl(dev, 1); 194 195 ppb_release_bus(ppbus, dev); 196 197 return (IIC_ENOADDR); 198 } 199 200 static devclass_t lpbb_devclass; 201 202 static device_method_t lpbb_methods[] = { 203 /* device interface */ 204 DEVMETHOD(device_identify, lpbb_identify), 205 DEVMETHOD(device_probe, lpbb_probe), 206 DEVMETHOD(device_attach, lpbb_attach), 207 208 /* bus interface */ 209 DEVMETHOD(bus_print_child, bus_generic_print_child), 210 211 /* iicbb interface */ 212 DEVMETHOD(iicbb_callback, lpbb_callback), 213 DEVMETHOD(iicbb_setsda, lpbb_setsda), 214 DEVMETHOD(iicbb_setscl, lpbb_setscl), 215 DEVMETHOD(iicbb_getsda, lpbb_getsda), 216 DEVMETHOD(iicbb_getscl, lpbb_getscl), 217 DEVMETHOD(iicbb_reset, lpbb_reset), 218 219 { 0, 0 } 220 }; 221 222 static driver_t lpbb_driver = { 223 "lpbb", 224 lpbb_methods, 225 1, 226 }; 227 228 DRIVER_MODULE(lpbb, ppbus, lpbb_driver, lpbb_devclass, 0, 0); 229 MODULE_DEPEND(lpbb, iicbb, IICBB_MINVER, IICBB_PREFVER, IICBB_MAXVER); 230 MODULE_VERSION(lpbb, 1); 231