xref: /freebsd/sys/dev/ppbus/lpbb.c (revision 3193579b66fd7067f898dbc54bdea81a0e6f9bd0)
1 /*-
2  * Copyright (c) 1998, 2001 Nicolas Souchu, Marc Bouget
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
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.
13  *
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
24  * SUCH DAMAGE.
25  *
26  *
27  */
28 
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
31 
32 /*
33  * I2C Bit-Banging over parallel port
34  *
35  * See the Official Philips interface description in lpbb(4)
36  */
37 
38 #include <sys/param.h>
39 #include <sys/kernel.h>
40 #include <sys/systm.h>
41 #include <sys/module.h>
42 #include <sys/bus.h>
43 #include <sys/uio.h>
44 
45 
46 #include <dev/ppbus/ppbconf.h>
47 #include "ppbus_if.h"
48 #include <dev/ppbus/ppbio.h>
49 
50 #include <dev/iicbus/iiconf.h>
51 #include <dev/iicbus/iicbus.h>
52 
53 #include "iicbb_if.h"
54 
55 static int lpbb_detect(device_t dev);
56 
57 static void
58 lpbb_identify(driver_t *driver, device_t parent)
59 {
60 
61 	BUS_ADD_CHILD(parent, 0, "lpbb", -1);
62 }
63 
64 static int
65 lpbb_probe(device_t dev)
66 {
67 
68 	/* Perhaps call this during identify instead? */
69 	if (!lpbb_detect(dev))
70 		return (ENXIO);
71 
72 	device_set_desc(dev, "Parallel I2C bit-banging interface");
73 
74 	return (0);
75 }
76 
77 static int
78 lpbb_attach(device_t dev)
79 {
80 	device_t bitbang;
81 
82 	/* add generic bit-banging code */
83 	bitbang = device_add_child(dev, "iicbb", -1);
84 	device_probe_and_attach(bitbang);
85 
86 	return (0);
87 }
88 
89 static int
90 lpbb_callback(device_t dev, int index, caddr_t *data)
91 {
92 	device_t ppbus = device_get_parent(dev);
93 	int error = 0;
94 	int how;
95 
96 	switch (index) {
97 	case IIC_REQUEST_BUS:
98 		/* request the ppbus */
99 		how = *(int *)data;
100 		error = ppb_request_bus(ppbus, dev, how);
101 		break;
102 
103 	case IIC_RELEASE_BUS:
104 		/* release the ppbus */
105 		error = ppb_release_bus(ppbus, dev);
106 		break;
107 
108 	default:
109 		error = EINVAL;
110 	}
111 
112 	return (error);
113 }
114 
115 #define SDA_out 0x80
116 #define SCL_out 0x08
117 #define SDA_in  0x80
118 #define SCL_in  0x08
119 #define ALIM    0x20
120 #define I2CKEY  0x50
121 
122 static int lpbb_getscl(device_t dev)
123 {
124 	return ((ppb_rstr(device_get_parent(dev)) & SCL_in) == SCL_in);
125 }
126 
127 static int lpbb_getsda(device_t dev)
128 {
129 	return ((ppb_rstr(device_get_parent(dev)) & SDA_in) == SDA_in);
130 }
131 
132 static void lpbb_setsda(device_t dev, char val)
133 {
134 	device_t ppbus = device_get_parent(dev);
135 
136 	if(val==0)
137 		ppb_wdtr(ppbus, (u_char)SDA_out);
138 	else
139 		ppb_wdtr(ppbus, (u_char)~SDA_out);
140 }
141 
142 static void lpbb_setscl(device_t dev, unsigned char val)
143 {
144 	device_t ppbus = device_get_parent(dev);
145 
146 	if(val==0)
147 		ppb_wctr(ppbus, (u_char)(ppb_rctr(ppbus)&~SCL_out));
148 	else
149 		ppb_wctr(ppbus, (u_char)(ppb_rctr(ppbus)|SCL_out));
150 }
151 
152 static int lpbb_detect(device_t dev)
153 {
154 	device_t ppbus = device_get_parent(dev);
155 
156 	if (ppb_request_bus(ppbus, dev, PPB_DONTWAIT)) {
157 		device_printf(dev, "can't allocate ppbus\n");
158 		return (0);
159 	}
160 
161 	/* reset bus */
162 	lpbb_setsda(dev, 1);
163 	lpbb_setscl(dev, 1);
164 
165 	if ((ppb_rstr(ppbus) & I2CKEY) ||
166 		((ppb_rstr(ppbus) & ALIM) != ALIM)) {
167 
168 		ppb_release_bus(ppbus, dev);
169 		return (0);
170 	}
171 
172 	ppb_release_bus(ppbus, dev);
173 
174 	return (1);
175 }
176 
177 static int
178 lpbb_reset(device_t dev, u_char speed, u_char addr, u_char * oldaddr)
179 {
180 	device_t ppbus = device_get_parent(dev);
181 
182 	if (ppb_request_bus(ppbus, dev, PPB_DONTWAIT)) {
183 		device_printf(dev, "can't allocate ppbus\n");
184 		return (0);
185 	}
186 
187 	/* reset bus */
188 	lpbb_setsda(dev, 1);
189 	lpbb_setscl(dev, 1);
190 
191 	ppb_release_bus(ppbus, dev);
192 
193 	return (IIC_ENOADDR);
194 }
195 
196 static devclass_t lpbb_devclass;
197 
198 static device_method_t lpbb_methods[] = {
199 	/* device interface */
200 	DEVMETHOD(device_identify,	lpbb_identify),
201 	DEVMETHOD(device_probe,		lpbb_probe),
202 	DEVMETHOD(device_attach,	lpbb_attach),
203 
204 	/* bus interface */
205 	DEVMETHOD(bus_print_child,	bus_generic_print_child),
206 
207 	/* iicbb interface */
208 	DEVMETHOD(iicbb_callback,	lpbb_callback),
209 	DEVMETHOD(iicbb_setsda,		lpbb_setsda),
210 	DEVMETHOD(iicbb_setscl,		lpbb_setscl),
211 	DEVMETHOD(iicbb_getsda,		lpbb_getsda),
212 	DEVMETHOD(iicbb_getscl,		lpbb_getscl),
213 	DEVMETHOD(iicbb_reset,		lpbb_reset),
214 
215 	{ 0, 0 }
216 };
217 
218 static driver_t lpbb_driver = {
219 	"lpbb",
220 	lpbb_methods,
221 	1,
222 };
223 
224 DRIVER_MODULE(lpbb, ppbus, lpbb_driver, lpbb_devclass, 0, 0);
225 MODULE_DEPEND(lpbb, iicbb, IICBB_MINVER, IICBB_PREFVER, IICBB_MAXVER);
226 MODULE_VERSION(lpbb, 1);
227