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