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", 0);
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);
133 lpbb_getscl(device_t dev)
138 rval = ((ppb_rstr(device_get_parent(dev)) & SCL_in) == SCL_in);
144 lpbb_getsda(device_t dev)
149 rval = ((ppb_rstr(device_get_parent(dev)) & SDA_in) == SDA_in);
155 lpbb_setsda(device_t dev, char val)
157 device_t ppbus = device_get_parent(dev);
161 ppb_wdtr(ppbus, (u_char)SDA_out);
163 ppb_wdtr(ppbus, (u_char)~SDA_out);
168 lpbb_setscl(device_t dev, unsigned char val)
170 device_t ppbus = device_get_parent(dev);
174 ppb_wctr(ppbus, (u_char)(ppb_rctr(ppbus) & ~SCL_out));
176 ppb_wctr(ppbus, (u_char)(ppb_rctr(ppbus) | SCL_out));
181 lpbb_detect(device_t dev)
183 device_t ppbus = device_get_parent(dev);
185 if (ppb_request_bus(ppbus, dev, PPB_DONTWAIT)) {
186 device_printf(dev, "can't allocate ppbus\n");
194 if ((ppb_rstr(ppbus) & I2CKEY) ||
195 ((ppb_rstr(ppbus) & ALIM) != ALIM)) {
197 ppb_release_bus(ppbus, dev);
201 ppb_release_bus(ppbus, dev);
207 lpbb_reset(device_t dev, u_char speed, u_char addr, u_char * oldaddr)
209 device_t ppbus = device_get_parent(dev);
212 if (ppb_request_bus(ppbus, dev, PPB_DONTWAIT)) {
213 device_printf(dev, "can't allocate ppbus\n");
221 ppb_release_bus(ppbus, dev);
224 return (IIC_ENOADDR);
227 static devclass_t lpbb_devclass;
229 static device_method_t lpbb_methods[] = {
230 /* device interface */
231 DEVMETHOD(device_identify, lpbb_identify),
232 DEVMETHOD(device_probe, lpbb_probe),
233 DEVMETHOD(device_attach, lpbb_attach),
236 DEVMETHOD(bus_print_child, bus_generic_print_child),
238 /* iicbb interface */
239 DEVMETHOD(iicbb_callback, lpbb_callback),
240 DEVMETHOD(iicbb_setsda, lpbb_setsda),
241 DEVMETHOD(iicbb_setscl, lpbb_setscl),
242 DEVMETHOD(iicbb_getsda, lpbb_getsda),
243 DEVMETHOD(iicbb_getscl, lpbb_getscl),
244 DEVMETHOD(iicbb_reset, lpbb_reset),
249 static driver_t lpbb_driver = {
255 DRIVER_MODULE(lpbb, ppbus, lpbb_driver, lpbb_devclass, 0, 0);
256 DRIVER_MODULE(iicbb, lpbb, iicbb_driver, iicbb_devclass, 0, 0);
257 MODULE_DEPEND(lpbb, ppbus, 1, 1, 1);
258 MODULE_DEPEND(lpbb, iicbb, IICBB_MINVER, IICBB_PREFVER, IICBB_MAXVER);
259 MODULE_VERSION(lpbb, 1);