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 */ 28 29 /* 30 * I2C Bit-Banging over parallel port 31 * 32 * See the Official Philips interface description in lpbb(4) 33 */ 34 35 #include <sys/param.h> 36 #include <sys/kernel.h> 37 #include <sys/systm.h> 38 #include <sys/module.h> 39 #include <sys/bus.h> 40 #include <sys/uio.h> 41 42 #include <bus/ppbus/ppbconf.h> 43 #include "ppbus_if.h" 44 #include <bus/ppbus/ppbio.h> 45 46 #include <bus/iicbus/iiconf.h> 47 #include <bus/iicbus/iicbus.h> 48 49 #include "iicbb_if.h" 50 51 static int lpbb_detect(device_t dev); 52 53 static void 54 lpbb_identify(driver_t *driver, device_t parent) 55 { 56 57 device_t dev; 58 59 dev = device_find_child(parent, "lpbb", 0); 60 if (!dev) 61 BUS_ADD_CHILD(parent, parent, 0, "lpbb", -1); 62 } 63 64 static int 65 lpbb_probe(device_t dev) 66 { 67 68 /* Perhaps call this during identify instead? */ 69 if (!lpbb_detect(dev)) 70 return (ENXIO); 71 72 device_set_desc(dev, "Parallel I2C bit-banging interface"); 73 74 return (0); 75 } 76 77 static int 78 lpbb_attach(device_t dev) 79 { 80 device_t bitbang; 81 82 /* add generic bit-banging code */ 83 bitbang = device_add_child(dev, "iicbb", -1); 84 device_probe_and_attach(bitbang); 85 86 return (0); 87 } 88 89 static int 90 lpbb_callback(device_t dev, int index, caddr_t *data) 91 { 92 device_t ppbus = device_get_parent(dev); 93 int error = 0; 94 int how; 95 96 switch (index) { 97 case IIC_REQUEST_BUS: 98 /* request the ppbus */ 99 how = *(int *)data; 100 error = ppb_request_bus(ppbus, dev, how); 101 break; 102 103 case IIC_RELEASE_BUS: 104 /* release the ppbus */ 105 error = ppb_release_bus(ppbus, dev); 106 break; 107 108 default: 109 error = EINVAL; 110 } 111 112 return (error); 113 } 114 115 #define SDA_out 0x80 116 #define SCL_out 0x08 117 #define SDA_in 0x80 118 #define SCL_in 0x08 119 #define ALIM 0x20 120 #define I2CKEY 0x50 121 122 static int 123 lpbb_getscl(device_t dev) 124 { 125 return ((ppb_rstr(device_get_parent(dev)) & SCL_in) == SCL_in); 126 } 127 128 static int 129 lpbb_getsda(device_t dev) 130 { 131 return ((ppb_rstr(device_get_parent(dev)) & SDA_in) == SDA_in); 132 } 133 134 static void 135 lpbb_setsda(device_t dev, char val) 136 { 137 device_t ppbus = device_get_parent(dev); 138 139 if(val==0) 140 ppb_wdtr(ppbus, (u_char)SDA_out); 141 else 142 ppb_wdtr(ppbus, (u_char)~SDA_out); 143 } 144 145 static void 146 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, NULL, NULL); 229 MODULE_DEPEND(lpbb, ppbus, 1, 1, 1); 230 MODULE_DEPEND(lpbb, iicbb, IICBB_MINVER, IICBB_PREFVER, IICBB_MAXVER); 231 MODULE_VERSION(lpbb, 1); 232