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>
49 #include <dev/ppbus/ppbconf.h>
51 #include <dev/ppbus/ppbio.h>
53 #include <dev/iicbus/iiconf.h>
54 #include <dev/iicbus/iicbus.h>
58 static int lpbb_detect(device_t dev);
61 lpbb_identify(driver_t *driver, device_t parent)
66 dev = device_find_child(parent, "lpbb", -1);
68 BUS_ADD_CHILD(parent, 0, "lpbb", -1);
72 lpbb_probe(device_t dev)
75 /* Perhaps call this during identify instead? */
76 if (!lpbb_detect(dev))
79 device_set_desc(dev, "Parallel I2C bit-banging interface");
85 lpbb_attach(device_t dev)
89 /* add generic bit-banging code */
90 bitbang = device_add_child(dev, "iicbb", -1);
91 device_probe_and_attach(bitbang);
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 */
108 error = ppb_request_bus(ppbus, dev, how);
112 case IIC_RELEASE_BUS:
113 /* release the ppbus */
115 error = ppb_release_bus(ppbus, dev);
133 /* Reset bus by setting SDA first and then SCL. */
135 lpbb_reset_bus(device_t dev)
137 device_t ppbus = device_get_parent(dev);
139 ppb_assert_locked(ppbus);
140 ppb_wdtr(ppbus, (u_char)~SDA_out);
141 ppb_wctr(ppbus, (u_char)(ppb_rctr(ppbus) | SCL_out));
145 lpbb_getscl(device_t dev)
147 device_t ppbus = device_get_parent(dev);
151 rval = ((ppb_rstr(ppbus) & SCL_in) == SCL_in);
157 lpbb_getsda(device_t dev)
159 device_t ppbus = device_get_parent(dev);
163 rval = ((ppb_rstr(ppbus) & SDA_in) == SDA_in);
169 lpbb_setsda(device_t dev, int val)
171 device_t ppbus = device_get_parent(dev);
175 ppb_wdtr(ppbus, (u_char)SDA_out);
177 ppb_wdtr(ppbus, (u_char)~SDA_out);
182 lpbb_setscl(device_t dev, int val)
184 device_t ppbus = device_get_parent(dev);
188 ppb_wctr(ppbus, (u_char)(ppb_rctr(ppbus) & ~SCL_out));
190 ppb_wctr(ppbus, (u_char)(ppb_rctr(ppbus) | SCL_out));
195 lpbb_detect(device_t dev)
197 device_t ppbus = device_get_parent(dev);
200 if (ppb_request_bus(ppbus, dev, PPB_DONTWAIT)) {
202 device_printf(dev, "can't allocate ppbus\n");
208 if ((ppb_rstr(ppbus) & I2CKEY) ||
209 ((ppb_rstr(ppbus) & ALIM) != ALIM)) {
210 ppb_release_bus(ppbus, dev);
215 ppb_release_bus(ppbus, dev);
222 lpbb_reset(device_t dev, u_char speed, u_char addr, u_char * oldaddr)
224 device_t ppbus = device_get_parent(dev);
227 if (ppb_request_bus(ppbus, dev, PPB_DONTWAIT)) {
229 device_printf(dev, "can't allocate ppbus\n");
235 ppb_release_bus(ppbus, dev);
238 return (IIC_ENOADDR);
241 static devclass_t lpbb_devclass;
243 static device_method_t lpbb_methods[] = {
244 /* device interface */
245 DEVMETHOD(device_identify, lpbb_identify),
246 DEVMETHOD(device_probe, lpbb_probe),
247 DEVMETHOD(device_attach, lpbb_attach),
249 /* iicbb interface */
250 DEVMETHOD(iicbb_callback, lpbb_callback),
251 DEVMETHOD(iicbb_setsda, lpbb_setsda),
252 DEVMETHOD(iicbb_setscl, lpbb_setscl),
253 DEVMETHOD(iicbb_getsda, lpbb_getsda),
254 DEVMETHOD(iicbb_getscl, lpbb_getscl),
255 DEVMETHOD(iicbb_reset, lpbb_reset),
260 static driver_t lpbb_driver = {
266 DRIVER_MODULE(lpbb, ppbus, lpbb_driver, lpbb_devclass, 0, 0);
267 DRIVER_MODULE(iicbb, lpbb, iicbb_driver, iicbb_devclass, 0, 0);
268 MODULE_DEPEND(lpbb, ppbus, 1, 1, 1);
269 MODULE_DEPEND(lpbb, iicbb, IICBB_MINVER, IICBB_PREFVER, IICBB_MAXVER);
270 MODULE_VERSION(lpbb, 1);