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 $
27 * $DragonFly: src/sys/dev/misc/lpbb/lpbb.c,v 1.3 2003/08/07 21:16:56 dillon Exp $
32 * I2C Bit-Banging over parallel port
34 * See the Official Philips interface description in lpbb(4)
37 #include <sys/param.h>
38 #include <sys/kernel.h>
39 #include <sys/systm.h>
40 #include <sys/module.h>
44 #include <machine/clock.h>
46 #include <bus/ppbus/ppbconf.h>
48 #include <bus/ppbus/ppbio.h>
50 #include <bus/iicbus/iiconf.h>
51 #include <bus/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", 0);
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)
80 device_t bitbang, iicbus;
82 /* add generic bit-banging code */
83 bitbang = device_add_child(dev, "iicbb", -1);
85 /* add the iicbus to the tree */
86 iicbus = iicbus_alloc_bus(bitbang);
88 device_probe_and_attach(bitbang);
90 /* XXX should be in iicbb_attach! */
91 device_probe_and_attach(iicbus);
97 lpbb_callback(device_t dev, int index, caddr_t *data)
99 device_t ppbus = device_get_parent(dev);
104 case IIC_REQUEST_BUS:
105 /* request the ppbus */
107 error = ppb_request_bus(ppbus, dev, how);
110 case IIC_RELEASE_BUS:
111 /* release the ppbus */
112 error = ppb_release_bus(ppbus, dev);
129 static int getSDA(device_t ppbus)
131 if((ppb_rstr(ppbus)&SDA_in)==SDA_in)
137 static void setSDA(device_t ppbus, char val)
140 ppb_wdtr(ppbus, (u_char)SDA_out);
142 ppb_wdtr(ppbus, (u_char)~SDA_out);
145 static void setSCL(device_t ppbus, unsigned char val)
148 ppb_wctr(ppbus, (u_char)(ppb_rctr(ppbus)&~SCL_out));
150 ppb_wctr(ppbus, (u_char)(ppb_rctr(ppbus)|SCL_out));
153 static int lpbb_detect(device_t dev)
155 device_t ppbus = device_get_parent(dev);
157 if (ppb_request_bus(ppbus, dev, PPB_DONTWAIT)) {
158 device_printf(dev, "can't allocate ppbus\n");
166 if ((ppb_rstr(ppbus) & I2CKEY) ||
167 ((ppb_rstr(ppbus) & ALIM) != ALIM)) {
169 ppb_release_bus(ppbus, dev);
173 ppb_release_bus(ppbus, dev);
179 lpbb_reset(device_t dev, u_char speed, u_char addr, u_char * oldaddr)
181 device_t ppbus = device_get_parent(dev);
187 return (IIC_ENOADDR);
191 lpbb_setlines(device_t dev, int ctrl, int data)
193 device_t ppbus = device_get_parent(dev);
200 lpbb_getdataline(device_t dev)
202 device_t ppbus = device_get_parent(dev);
204 return (getSDA(ppbus));
207 static devclass_t lpbb_devclass;
209 static device_method_t lpbb_methods[] = {
210 /* device interface */
211 DEVMETHOD(device_identify, lpbb_identify),
212 DEVMETHOD(device_probe, lpbb_probe),
213 DEVMETHOD(device_attach, lpbb_attach),
216 DEVMETHOD(bus_print_child, bus_generic_print_child),
218 /* iicbb interface */
219 DEVMETHOD(iicbb_callback, lpbb_callback),
220 DEVMETHOD(iicbb_setlines, lpbb_setlines),
221 DEVMETHOD(iicbb_getdataline, lpbb_getdataline),
222 DEVMETHOD(iicbb_reset, lpbb_reset),
227 static driver_t lpbb_driver = {
233 DRIVER_MODULE(lpbb, ppbus, lpbb_driver, lpbb_devclass, 0, 0);