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
26 * $FreeBSD: src/sys/dev/ppbus/lpbb.c,v 1.18.8.1 2006/07/19 16:31:12 kib Exp $
30 * I2C Bit-Banging over parallel port
32 * See the Official Philips interface description in lpbb(4)
35 #include <sys/param.h>
36 #include <sys/kernel.h>
37 #include <sys/systm.h>
38 #include <sys/module.h>
42 #include <bus/ppbus/ppbconf.h>
44 #include <bus/ppbus/ppbio.h>
46 #include <bus/iicbus/iiconf.h>
47 #include <bus/iicbus/iicbus.h>
51 static int lpbb_detect(device_t dev);
54 lpbb_identify(driver_t *driver, device_t parent)
59 dev = device_find_child(parent, "lpbb", 0);
61 BUS_ADD_CHILD(parent, 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);
123 lpbb_getscl(device_t dev)
125 return ((ppb_rstr(device_get_parent(dev)) & SCL_in) == SCL_in);
129 lpbb_getsda(device_t dev)
131 return ((ppb_rstr(device_get_parent(dev)) & SDA_in) == SDA_in);
135 lpbb_setsda(device_t dev, char val)
137 device_t ppbus = device_get_parent(dev);
140 ppb_wdtr(ppbus, (u_char)SDA_out);
142 ppb_wdtr(ppbus, (u_char)~SDA_out);
146 lpbb_setscl(device_t dev, unsigned char val)
148 device_t ppbus = device_get_parent(dev);
151 ppb_wctr(ppbus, (u_char)(ppb_rctr(ppbus)&~SCL_out));
153 ppb_wctr(ppbus, (u_char)(ppb_rctr(ppbus)|SCL_out));
156 static int lpbb_detect(device_t dev)
158 device_t ppbus = device_get_parent(dev);
160 if (ppb_request_bus(ppbus, dev, PPB_DONTWAIT)) {
161 device_printf(dev, "can't allocate ppbus\n");
169 if ((ppb_rstr(ppbus) & I2CKEY) ||
170 ((ppb_rstr(ppbus) & ALIM) != ALIM)) {
172 ppb_release_bus(ppbus, dev);
176 ppb_release_bus(ppbus, dev);
182 lpbb_reset(device_t dev, u_char speed, u_char addr, u_char * oldaddr)
184 device_t ppbus = device_get_parent(dev);
186 if (ppb_request_bus(ppbus, dev, PPB_DONTWAIT)) {
187 device_printf(dev, "can't allocate ppbus\n");
195 ppb_release_bus(ppbus, dev);
197 return (IIC_ENOADDR);
200 static devclass_t lpbb_devclass;
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),
209 DEVMETHOD(bus_print_child, bus_generic_print_child),
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),
222 static driver_t lpbb_driver = {
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);