2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 1998, 2001 Nicolas Souchu, Marc Bouget
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
35 * I2C Bit-Banging over parallel port
37 * See the Official Philips interface description in lpbb(4)
40 #include <sys/param.h>
43 #include <sys/kernel.h>
44 #include <sys/module.h>
45 #include <sys/mutex.h>
46 #include <sys/systm.h>
50 #include <dev/ppbus/ppbconf.h>
52 #include <dev/ppbus/ppbio.h>
54 #include <dev/iicbus/iiconf.h>
55 #include <dev/iicbus/iicbus.h>
59 static int lpbb_detect(device_t dev);
62 lpbb_identify(driver_t *driver, device_t parent)
67 dev = device_find_child(parent, "lpbb", -1);
69 BUS_ADD_CHILD(parent, 0, "lpbb", -1);
73 lpbb_probe(device_t dev)
76 /* Perhaps call this during identify instead? */
77 if (!lpbb_detect(dev))
80 device_set_desc(dev, "Parallel I2C bit-banging interface");
86 lpbb_attach(device_t dev)
90 /* add generic bit-banging code */
91 bitbang = device_add_child(dev, "iicbb", -1);
92 device_probe_and_attach(bitbang);
98 lpbb_callback(device_t dev, int index, caddr_t data)
100 device_t ppbus = device_get_parent(dev);
105 case IIC_REQUEST_BUS:
106 /* request the ppbus */
109 error = ppb_request_bus(ppbus, dev, how);
113 case IIC_RELEASE_BUS:
114 /* release the ppbus */
116 error = ppb_release_bus(ppbus, dev);
134 /* Reset bus by setting SDA first and then SCL. */
136 lpbb_reset_bus(device_t dev)
138 device_t ppbus = device_get_parent(dev);
140 ppb_assert_locked(ppbus);
141 ppb_wdtr(ppbus, (u_char)~SDA_out);
142 ppb_wctr(ppbus, (u_char)(ppb_rctr(ppbus) | SCL_out));
146 lpbb_getscl(device_t dev)
148 device_t ppbus = device_get_parent(dev);
152 rval = ((ppb_rstr(ppbus) & SCL_in) == SCL_in);
158 lpbb_getsda(device_t dev)
160 device_t ppbus = device_get_parent(dev);
164 rval = ((ppb_rstr(ppbus) & SDA_in) == SDA_in);
170 lpbb_setsda(device_t dev, int val)
172 device_t ppbus = device_get_parent(dev);
176 ppb_wdtr(ppbus, (u_char)SDA_out);
178 ppb_wdtr(ppbus, (u_char)~SDA_out);
183 lpbb_setscl(device_t dev, int val)
185 device_t ppbus = device_get_parent(dev);
189 ppb_wctr(ppbus, (u_char)(ppb_rctr(ppbus) & ~SCL_out));
191 ppb_wctr(ppbus, (u_char)(ppb_rctr(ppbus) | SCL_out));
196 lpbb_detect(device_t dev)
198 device_t ppbus = device_get_parent(dev);
201 if (ppb_request_bus(ppbus, dev, PPB_DONTWAIT)) {
203 device_printf(dev, "can't allocate ppbus\n");
209 if ((ppb_rstr(ppbus) & I2CKEY) ||
210 ((ppb_rstr(ppbus) & ALIM) != ALIM)) {
211 ppb_release_bus(ppbus, dev);
216 ppb_release_bus(ppbus, dev);
223 lpbb_reset(device_t dev, u_char speed, u_char addr, u_char * oldaddr)
225 device_t ppbus = device_get_parent(dev);
228 if (ppb_request_bus(ppbus, dev, PPB_DONTWAIT)) {
230 device_printf(dev, "can't allocate ppbus\n");
236 ppb_release_bus(ppbus, dev);
239 return (IIC_ENOADDR);
242 static devclass_t lpbb_devclass;
244 static device_method_t lpbb_methods[] = {
245 /* device interface */
246 DEVMETHOD(device_identify, lpbb_identify),
247 DEVMETHOD(device_probe, lpbb_probe),
248 DEVMETHOD(device_attach, lpbb_attach),
250 /* iicbb interface */
251 DEVMETHOD(iicbb_callback, lpbb_callback),
252 DEVMETHOD(iicbb_setsda, lpbb_setsda),
253 DEVMETHOD(iicbb_setscl, lpbb_setscl),
254 DEVMETHOD(iicbb_getsda, lpbb_getsda),
255 DEVMETHOD(iicbb_getscl, lpbb_getscl),
256 DEVMETHOD(iicbb_reset, lpbb_reset),
261 static driver_t lpbb_driver = {
267 DRIVER_MODULE(lpbb, ppbus, lpbb_driver, lpbb_devclass, 0, 0);
268 DRIVER_MODULE(iicbb, lpbb, iicbb_driver, iicbb_devclass, 0, 0);
269 MODULE_DEPEND(lpbb, ppbus, 1, 1, 1);
270 MODULE_DEPEND(lpbb, iicbb, IICBB_MINVER, IICBB_PREFVER, IICBB_MAXVER);
271 MODULE_VERSION(lpbb, 1);