xref: /freebsd/sys/dev/ppbus/lpbb.c (revision c807777a43ef2b59786fa8a1a35c1f154fd069e5)
1 /*-
2  * Copyright (c) 1998 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  * $FreeBSD$
27  *
28  */
29 
30 /*
31  * I2C Bit-Banging over parallel port
32  *
33  * See the Official Philips interface description in lpbb(4)
34  */
35 
36 #include <sys/param.h>
37 #include <sys/kernel.h>
38 #include <sys/systm.h>
39 #include <sys/module.h>
40 #include <sys/bus.h>
41 #include <sys/conf.h>
42 #include <sys/buf.h>
43 #include <sys/uio.h>
44 #include <sys/malloc.h>
45 
46 #include <machine/clock.h>
47 
48 #include <dev/ppbus/ppbconf.h>
49 #include "ppbus_if.h"
50 #include <dev/ppbus/ppbio.h>
51 
52 #include <dev/iicbus/iiconf.h>
53 #include <dev/iicbus/iicbus.h>
54 
55 #include "iicbb_if.h"
56 
57 struct lpbb_softc {
58 	int dummy;
59 };
60 
61 static int lpbb_detect(device_t dev);
62 
63 static int lpbb_probe(device_t);
64 static int lpbb_attach(device_t);
65 
66 static int lpbb_callback(device_t, int, caddr_t *);
67 static void lpbb_setlines(device_t, int, int);
68 static int lpbb_getdataline(device_t);
69 static int lpbb_reset(device_t, u_char, u_char, u_char *);
70 
71 static devclass_t lpbb_devclass;
72 
73 static device_method_t lpbb_methods[] = {
74 	/* device interface */
75 	DEVMETHOD(device_probe,		lpbb_probe),
76 	DEVMETHOD(device_attach,	lpbb_attach),
77 
78 	/* bus interface */
79 	DEVMETHOD(bus_print_child,	bus_generic_print_child),
80 
81 	/* iicbb interface */
82 	DEVMETHOD(iicbb_callback,	lpbb_callback),
83 	DEVMETHOD(iicbb_setlines,	lpbb_setlines),
84 	DEVMETHOD(iicbb_getdataline,	lpbb_getdataline),
85 	DEVMETHOD(iicbb_reset,		lpbb_reset),
86 
87 	{ 0, 0 }
88 };
89 
90 static driver_t lpbb_driver = {
91 	"lpbb",
92 	lpbb_methods,
93 	sizeof(struct lpbb_softc),
94 };
95 
96 static int
97 lpbb_probe(device_t dev)
98 {
99 	device_set_desc(dev, "Parallel I2C bit-banging interface");
100 
101 	if (!lpbb_detect(dev))
102 		return (ENXIO);
103 
104 	return (0);
105 }
106 
107 static int
108 lpbb_attach(device_t dev)
109 {
110 	device_t bitbang, iicbus;
111 
112 	/* add generic bit-banging code */
113 	bitbang = device_add_child(dev, "iicbb", -1);
114 
115 	/* add the iicbus to the tree */
116 	iicbus = iicbus_alloc_bus(bitbang);
117 
118 	device_probe_and_attach(bitbang);
119 
120 	/* XXX should be in iicbb_attach! */
121 	device_probe_and_attach(iicbus);
122 
123 	return (0);
124 }
125 
126 static int
127 lpbb_callback(device_t dev, int index, caddr_t *data)
128 {
129 	device_t ppbus = device_get_parent(dev);
130 	int error = 0;
131 	int how;
132 
133 	switch (index) {
134 	case IIC_REQUEST_BUS:
135 		/* request the ppbus */
136 		how = *(int *)data;
137 		error = ppb_request_bus(ppbus, dev, how);
138 		break;
139 
140 	case IIC_RELEASE_BUS:
141 		/* release the ppbus */
142 		error = ppb_release_bus(ppbus, dev);
143 		break;
144 
145 	default:
146 		error = EINVAL;
147 	}
148 
149 	return (error);
150 }
151 
152 #define SDA_out 0x80
153 #define SCL_out 0x08
154 #define SDA_in  0x80
155 #define SCL_in  0x08
156 #define ALIM    0x20
157 #define I2CKEY  0x50
158 
159 static int getSDA(device_t ppbus)
160 {
161 	if((ppb_rstr(ppbus)&SDA_in)==SDA_in)
162 		return 1;
163 	else
164 		return 0;
165 }
166 
167 static void setSDA(device_t ppbus, char val)
168 {
169 	if(val==0)
170 		ppb_wdtr(ppbus, (u_char)SDA_out);
171 	else
172 		ppb_wdtr(ppbus, (u_char)~SDA_out);
173 }
174 
175 static void setSCL(device_t ppbus, unsigned char val)
176 {
177 	if(val==0)
178 		ppb_wctr(ppbus, (u_char)(ppb_rctr(ppbus)&~SCL_out));
179 	else
180 		ppb_wctr(ppbus, (u_char)(ppb_rctr(ppbus)|SCL_out));
181 }
182 
183 static int lpbb_detect(device_t dev)
184 {
185 	device_t ppbus = device_get_parent(dev);
186 
187 	if (ppb_request_bus(ppbus, dev, PPB_DONTWAIT)) {
188 		device_printf(dev, "can't allocate ppbus\n");
189 		return (0);
190 	}
191 
192 	/* reset bus */
193 	setSDA(ppbus, 1);
194 	setSCL(ppbus, 1);
195 
196 	if ((ppb_rstr(ppbus) & I2CKEY) ||
197 		((ppb_rstr(ppbus) & ALIM) != ALIM)) {
198 
199 		ppb_release_bus(ppbus, dev);
200 		return (0);
201 	}
202 
203 	ppb_release_bus(ppbus, dev);
204 
205 	return (1);
206 }
207 
208 static int
209 lpbb_reset(device_t dev, u_char speed, u_char addr, u_char * oldaddr)
210 {
211 	device_t ppbus = device_get_parent(dev);
212 
213 	/* reset bus */
214 	setSDA(ppbus, 1);
215 	setSCL(ppbus, 1);
216 
217 	return (IIC_ENOADDR);
218 }
219 
220 static void
221 lpbb_setlines(device_t dev, int ctrl, int data)
222 {
223 	device_t ppbus = device_get_parent(dev);
224 
225 	setSCL(ppbus, ctrl);
226 	setSDA(ppbus, data);
227 }
228 
229 static int
230 lpbb_getdataline(device_t dev)
231 {
232 	device_t ppbus = device_get_parent(dev);
233 
234 	return (getSDA(ppbus));
235 }
236 
237 DRIVER_MODULE(lpbb, ppbus, lpbb_driver, lpbb_devclass, 0, 0);
238