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