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>
41 #include <sys/kernel.h>
42 #include <sys/module.h>
43 #include <sys/mutex.h>
44 #include <sys/systm.h>
48 #include <dev/ppbus/ppbconf.h>
50 #include <dev/ppbus/ppbio.h>
52 #include <dev/iicbus/iiconf.h>
53 #include <dev/iicbus/iicbus.h>
57 static int lpbb_detect(device_t dev);
60 lpbb_identify(driver_t *driver, device_t parent)
65 dev = device_find_child(parent, "lpbb", -1);
67 BUS_ADD_CHILD(parent, 0, "lpbb", -1);
71 lpbb_probe(device_t dev)
74 /* Perhaps call this during identify instead? */
75 if (!lpbb_detect(dev))
78 device_set_desc(dev, "Parallel I2C bit-banging interface");
84 lpbb_attach(device_t dev)
88 /* add generic bit-banging code */
89 bitbang = device_add_child(dev, "iicbb", -1);
90 device_probe_and_attach(bitbang);
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 */
107 error = ppb_request_bus(ppbus, dev, how);
111 case IIC_RELEASE_BUS:
112 /* release the ppbus */
114 error = ppb_release_bus(ppbus, dev);
132 /* Reset bus by setting SDA first and then SCL. */
134 lpbb_reset_bus(device_t dev)
136 device_t ppbus = device_get_parent(dev);
138 ppb_assert_locked(ppbus);
139 ppb_wdtr(ppbus, (u_char)~SDA_out);
140 ppb_wctr(ppbus, (u_char)(ppb_rctr(ppbus) | SCL_out));
144 lpbb_getscl(device_t dev)
146 device_t ppbus = device_get_parent(dev);
150 rval = ((ppb_rstr(ppbus) & SCL_in) == SCL_in);
156 lpbb_getsda(device_t dev)
158 device_t ppbus = device_get_parent(dev);
162 rval = ((ppb_rstr(ppbus) & SDA_in) == SDA_in);
168 lpbb_setsda(device_t dev, int val)
170 device_t ppbus = device_get_parent(dev);
174 ppb_wdtr(ppbus, (u_char)SDA_out);
176 ppb_wdtr(ppbus, (u_char)~SDA_out);
181 lpbb_setscl(device_t dev, int val)
183 device_t ppbus = device_get_parent(dev);
187 ppb_wctr(ppbus, (u_char)(ppb_rctr(ppbus) & ~SCL_out));
189 ppb_wctr(ppbus, (u_char)(ppb_rctr(ppbus) | SCL_out));
194 lpbb_detect(device_t dev)
196 device_t ppbus = device_get_parent(dev);
199 if (ppb_request_bus(ppbus, dev, PPB_DONTWAIT)) {
201 device_printf(dev, "can't allocate ppbus\n");
207 if ((ppb_rstr(ppbus) & I2CKEY) ||
208 ((ppb_rstr(ppbus) & ALIM) != ALIM)) {
209 ppb_release_bus(ppbus, dev);
214 ppb_release_bus(ppbus, dev);
221 lpbb_reset(device_t dev, u_char speed, u_char addr, u_char * oldaddr)
223 device_t ppbus = device_get_parent(dev);
226 if (ppb_request_bus(ppbus, dev, PPB_DONTWAIT)) {
228 device_printf(dev, "can't allocate ppbus\n");
234 ppb_release_bus(ppbus, dev);
237 return (IIC_ENOADDR);
240 static devclass_t lpbb_devclass;
242 static device_method_t lpbb_methods[] = {
243 /* device interface */
244 DEVMETHOD(device_identify, lpbb_identify),
245 DEVMETHOD(device_probe, lpbb_probe),
246 DEVMETHOD(device_attach, lpbb_attach),
248 /* iicbb interface */
249 DEVMETHOD(iicbb_callback, lpbb_callback),
250 DEVMETHOD(iicbb_setsda, lpbb_setsda),
251 DEVMETHOD(iicbb_setscl, lpbb_setscl),
252 DEVMETHOD(iicbb_getsda, lpbb_getsda),
253 DEVMETHOD(iicbb_getscl, lpbb_getscl),
254 DEVMETHOD(iicbb_reset, lpbb_reset),
259 static driver_t lpbb_driver = {
265 DRIVER_MODULE(lpbb, ppbus, lpbb_driver, lpbb_devclass, 0, 0);
266 DRIVER_MODULE(iicbb, lpbb, iicbb_driver, iicbb_devclass, 0, 0);
267 MODULE_DEPEND(lpbb, ppbus, 1, 1, 1);
268 MODULE_DEPEND(lpbb, iicbb, IICBB_MINVER, IICBB_PREFVER, IICBB_MAXVER);
269 MODULE_VERSION(lpbb, 1);