2 * Copyright (c) 1998, 2001 Nicolas Souchu, Marc Bouget
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
33 * I2C Bit-Banging over parallel port
35 * See the Official Philips interface description in lpbb(4)
38 #include <sys/param.h>
39 #include <sys/kernel.h>
40 #include <sys/systm.h>
41 #include <sys/module.h>
46 #include <dev/ppbus/ppbconf.h>
48 #include <dev/ppbus/ppbio.h>
50 #include <dev/iicbus/iiconf.h>
51 #include <dev/iicbus/iicbus.h>
55 static int lpbb_detect(device_t dev);
58 lpbb_identify(driver_t *driver, device_t parent)
61 BUS_ADD_CHILD(parent, 0, "lpbb", -1);
65 lpbb_probe(device_t dev)
68 /* Perhaps call this during identify instead? */
69 if (!lpbb_detect(dev))
72 device_set_desc(dev, "Parallel I2C bit-banging interface");
78 lpbb_attach(device_t dev)
82 /* add generic bit-banging code */
83 bitbang = device_add_child(dev, "iicbb", -1);
84 device_probe_and_attach(bitbang);
90 lpbb_callback(device_t dev, int index, caddr_t *data)
92 device_t ppbus = device_get_parent(dev);
98 /* request the ppbus */
100 error = ppb_request_bus(ppbus, dev, how);
103 case IIC_RELEASE_BUS:
104 /* release the ppbus */
105 error = ppb_release_bus(ppbus, dev);
122 static int lpbb_getscl(device_t dev)
124 return ((ppb_rstr(device_get_parent(dev)) & SCL_in) == SCL_in);
127 static int lpbb_getsda(device_t dev)
129 return ((ppb_rstr(device_get_parent(dev)) & SDA_in) == SDA_in);
132 static void lpbb_setsda(device_t dev, char val)
134 device_t ppbus = device_get_parent(dev);
137 ppb_wdtr(ppbus, (u_char)SDA_out);
139 ppb_wdtr(ppbus, (u_char)~SDA_out);
142 static void lpbb_setscl(device_t dev, unsigned char val)
144 device_t ppbus = device_get_parent(dev);
147 ppb_wctr(ppbus, (u_char)(ppb_rctr(ppbus)&~SCL_out));
149 ppb_wctr(ppbus, (u_char)(ppb_rctr(ppbus)|SCL_out));
152 static int lpbb_detect(device_t dev)
154 device_t ppbus = device_get_parent(dev);
156 if (ppb_request_bus(ppbus, dev, PPB_DONTWAIT)) {
157 device_printf(dev, "can't allocate ppbus\n");
165 if ((ppb_rstr(ppbus) & I2CKEY) ||
166 ((ppb_rstr(ppbus) & ALIM) != ALIM)) {
168 ppb_release_bus(ppbus, dev);
172 ppb_release_bus(ppbus, dev);
178 lpbb_reset(device_t dev, u_char speed, u_char addr, u_char * oldaddr)
180 device_t ppbus = device_get_parent(dev);
182 if (ppb_request_bus(ppbus, dev, PPB_DONTWAIT)) {
183 device_printf(dev, "can't allocate ppbus\n");
191 ppb_release_bus(ppbus, dev);
193 return (IIC_ENOADDR);
196 static devclass_t lpbb_devclass;
198 static device_method_t lpbb_methods[] = {
199 /* device interface */
200 DEVMETHOD(device_identify, lpbb_identify),
201 DEVMETHOD(device_probe, lpbb_probe),
202 DEVMETHOD(device_attach, lpbb_attach),
205 DEVMETHOD(bus_print_child, bus_generic_print_child),
207 /* iicbb interface */
208 DEVMETHOD(iicbb_callback, lpbb_callback),
209 DEVMETHOD(iicbb_setsda, lpbb_setsda),
210 DEVMETHOD(iicbb_setscl, lpbb_setscl),
211 DEVMETHOD(iicbb_getsda, lpbb_getsda),
212 DEVMETHOD(iicbb_getscl, lpbb_getscl),
213 DEVMETHOD(iicbb_reset, lpbb_reset),
218 static driver_t lpbb_driver = {
224 DRIVER_MODULE(lpbb, ppbus, lpbb_driver, lpbb_devclass, 0, 0);
225 MODULE_DEPEND(lpbb, iicbb, IICBB_MINVER, IICBB_PREFVER, IICBB_MAXVER);
226 MODULE_VERSION(lpbb, 1);