2 * Copyright (c) 1998 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
26 * $FreeBSD: src/sys/dev/ppbus/lpbb.c,v 1.11.2.1 2000/05/24 00:20:57 n_hibma Exp $
31 * I2C Bit-Banging over parallel port
33 * See the Official Philips interface description in lpbb(4)
36 #include <sys/param.h>
37 #include <sys/kernel.h>
38 #include <sys/systm.h>
39 #include <sys/module.h>
43 #include <machine/clock.h>
45 #include <dev/ppbus/ppbconf.h>
47 #include <dev/ppbus/ppbio.h>
49 #include <dev/iicbus/iiconf.h>
50 #include <dev/iicbus/iicbus.h>
54 static int lpbb_detect(device_t dev);
57 lpbb_identify(driver_t *driver, device_t parent)
60 BUS_ADD_CHILD(parent, 0, "lpbb", 0);
64 lpbb_probe(device_t dev)
67 /* Perhaps call this during identify instead? */
68 if (!lpbb_detect(dev))
71 device_set_desc(dev, "Parallel I2C bit-banging interface");
77 lpbb_attach(device_t dev)
79 device_t bitbang, iicbus;
81 /* add generic bit-banging code */
82 bitbang = device_add_child(dev, "iicbb", -1);
84 /* add the iicbus to the tree */
85 iicbus = iicbus_alloc_bus(bitbang);
87 device_probe_and_attach(bitbang);
89 /* XXX should be in iicbb_attach! */
90 device_probe_and_attach(iicbus);
96 lpbb_callback(device_t dev, int index, caddr_t *data)
98 device_t ppbus = device_get_parent(dev);
103 case IIC_REQUEST_BUS:
104 /* request the ppbus */
106 error = ppb_request_bus(ppbus, dev, how);
109 case IIC_RELEASE_BUS:
110 /* release the ppbus */
111 error = ppb_release_bus(ppbus, dev);
128 static int getSDA(device_t ppbus)
130 if((ppb_rstr(ppbus)&SDA_in)==SDA_in)
136 static void setSDA(device_t ppbus, char val)
139 ppb_wdtr(ppbus, (u_char)SDA_out);
141 ppb_wdtr(ppbus, (u_char)~SDA_out);
144 static void setSCL(device_t ppbus, unsigned char val)
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);
186 return (IIC_ENOADDR);
190 lpbb_setlines(device_t dev, int ctrl, int data)
192 device_t ppbus = device_get_parent(dev);
199 lpbb_getdataline(device_t dev)
201 device_t ppbus = device_get_parent(dev);
203 return (getSDA(ppbus));
206 static devclass_t lpbb_devclass;
208 static device_method_t lpbb_methods[] = {
209 /* device interface */
210 DEVMETHOD(device_identify, lpbb_identify),
211 DEVMETHOD(device_probe, lpbb_probe),
212 DEVMETHOD(device_attach, lpbb_attach),
215 DEVMETHOD(bus_print_child, bus_generic_print_child),
217 /* iicbb interface */
218 DEVMETHOD(iicbb_callback, lpbb_callback),
219 DEVMETHOD(iicbb_setlines, lpbb_setlines),
220 DEVMETHOD(iicbb_getdataline, lpbb_getdataline),
221 DEVMETHOD(iicbb_reset, lpbb_reset),
226 static driver_t lpbb_driver = {
232 DRIVER_MODULE(lpbb, ppbus, lpbb_driver, lpbb_devclass, 0, 0);