xref: /freebsd/sys/dev/ppbus/lpbb.c (revision 6e8394b8baa7d5d9153ab90de6824bcd19b3b4e1)
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  *	$Id: lpbb.c,v 1.4 1999/01/10 12:04:54 nsouch Exp $
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 
50 #include <dev/iicbus/iiconf.h>
51 #include <dev/iicbus/iicbus.h>
52 
53 #include "iicbb_if.h"
54 
55 /* iicbus softc */
56 struct lpbb_softc {
57 
58 	struct ppb_device lpbb_dev;
59 };
60 
61 static int lpbb_detect(struct lpbb_softc *);
62 
63 static int lpbb_probe(device_t);
64 static int lpbb_attach(device_t);
65 static void lpbb_print_child(device_t, device_t);
66 
67 static int lpbb_callback(device_t, int, caddr_t *);
68 static void lpbb_setlines(device_t, int, int);
69 static int lpbb_getdataline(device_t);
70 static int lpbb_reset(device_t, u_char, u_char, u_char *);
71 
72 static devclass_t lpbb_devclass;
73 
74 static device_method_t lpbb_methods[] = {
75 	/* device interface */
76 	DEVMETHOD(device_probe,		lpbb_probe),
77 	DEVMETHOD(device_attach,	lpbb_attach),
78 
79 	/* bus interface */
80 	DEVMETHOD(bus_print_child,	lpbb_print_child),
81 
82 	/* iicbb interface */
83 	DEVMETHOD(iicbb_callback,	lpbb_callback),
84 	DEVMETHOD(iicbb_setlines,	lpbb_setlines),
85 	DEVMETHOD(iicbb_getdataline,	lpbb_getdataline),
86 	DEVMETHOD(iicbb_reset,		lpbb_reset),
87 
88 	{ 0, 0 }
89 };
90 
91 static driver_t lpbb_driver = {
92 	"lpbb",
93 	lpbb_methods,
94 	sizeof(struct lpbb_softc),
95 };
96 
97 /*
98  * Make ourselves visible as a ppbus driver
99  */
100 static struct ppb_device	*lpbb_ppb_probe(struct ppb_data *ppb);
101 static int			lpbb_ppb_attach(struct ppb_device *dev);
102 
103 #define MAXLPBB	8			/* XXX not much better! */
104 static struct lpbb_softc *lpbbdata[MAXLPBB];
105 static int nlpbb = 0;
106 
107 #ifdef KERNEL
108 
109 static struct ppb_driver lpbbdriver = {
110     lpbb_ppb_probe, lpbb_ppb_attach, "lpbb"
111 };
112 DATA_SET(ppbdriver_set, lpbbdriver);
113 
114 #endif /* KERNEL */
115 
116 static int
117 lpbb_probe(device_t dev)
118 {
119 	struct lpbb_softc *sc = lpbbdata[device_get_unit(dev)];
120 	struct lpbb_softc *scdst = (struct lpbb_softc *)device_get_softc(dev);
121 
122 	/* XXX copy softc. Yet, ppbus device is sc->lpbb_dev, but will be
123 	 * dev->parent when ppbus will be ported to the new bus architecture */
124 	bcopy(sc, scdst, sizeof(struct lpbb_softc));
125 
126 	device_set_desc(dev, "parallel I2C bit-banging interface");
127 
128 	/* probe done by ppbus initialization */
129 	return (0);
130 }
131 
132 static int
133 lpbb_attach(device_t dev)
134 {
135 	device_t bitbang, iicbus;
136 
137 	/* add generic bit-banging code */
138 	bitbang = device_add_child(dev, "iicbb", -1, NULL);
139 
140 	/* add the iicbus to the tree */
141 	iicbus = iicbus_alloc_bus(bitbang);
142 
143 	device_probe_and_attach(bitbang);
144 
145 	/* XXX should be in iicbb_attach! */
146 	device_probe_and_attach(iicbus);
147 
148 	return (0);
149 }
150 
151 /*
152  * lppbb_ppb_probe()
153  */
154 static struct ppb_device *
155 lpbb_ppb_probe(struct ppb_data *ppb)
156 {
157 	struct lpbb_softc *sc;
158 
159 	sc = (struct lpbb_softc *) malloc(sizeof(struct lpbb_softc),
160 							M_TEMP, M_NOWAIT);
161 	if (!sc) {
162 		printf("lpbb: cannot malloc!\n");
163 		return (0);
164 	}
165 	bzero(sc, sizeof(struct lpbb_softc));
166 
167 	lpbbdata[nlpbb] = sc;
168 
169 	/*
170 	 * ppbus dependent initialisation.
171 	 */
172 	sc->lpbb_dev.id_unit = nlpbb;
173 	sc->lpbb_dev.name = lpbbdriver.name;
174 	sc->lpbb_dev.ppb = ppb;
175 	sc->lpbb_dev.intr = 0;
176 
177 	if (!lpbb_detect(sc)) {
178 		free(sc, M_TEMP);
179 		return (NULL);
180 	}
181 
182 	/* Ok, go to next device on next probe */
183 	nlpbb ++;
184 
185 	/* XXX wrong according to new bus architecture. ppbus needs to be
186 	 * ported
187 	 */
188 	return (&sc->lpbb_dev);
189 }
190 
191 static int
192 lpbb_ppb_attach(struct ppb_device *dev)
193 {
194 	/* add the parallel port I2C interface to the bus tree */
195 	if (!device_add_child(root_bus, "lpbb", dev->id_unit, NULL))
196 		return (0);
197 
198 	return (1);
199 }
200 
201 static int
202 lpbb_callback(device_t dev, int index, caddr_t *data)
203 {
204 	struct lpbb_softc *sc = (struct lpbb_softc *)device_get_softc(dev);
205 	int error = 0;
206 	int how;
207 
208 	switch (index) {
209 	case IIC_REQUEST_BUS:
210 		/* request the ppbus */
211 		how = *(int *)data;
212 		error = ppb_request_bus(&sc->lpbb_dev, how);
213 		break;
214 
215 	case IIC_RELEASE_BUS:
216 		/* release the ppbus */
217 		error = ppb_release_bus(&sc->lpbb_dev);
218 		break;
219 
220 	default:
221 		error = EINVAL;
222 	}
223 
224 	return (error);
225 }
226 
227 #define SDA_out 0x80
228 #define SCL_out 0x08
229 #define SDA_in  0x80
230 #define SCL_in  0x08
231 #define ALIM    0x20
232 #define I2CKEY  0x50
233 
234 static int getSDA(struct lpbb_softc *sc)
235 {
236 if((ppb_rstr(&sc->lpbb_dev)&SDA_in)==SDA_in)
237         return 1;
238 else
239         return 0;
240 }
241 
242 static void setSDA(struct lpbb_softc *sc, char val)
243 {
244 if(val==0)
245         ppb_wdtr(&sc->lpbb_dev, (u_char)SDA_out);
246 else
247         ppb_wdtr(&sc->lpbb_dev, (u_char)~SDA_out);
248 }
249 
250 static void setSCL(struct lpbb_softc *sc, unsigned char val)
251 {
252 if(val==0)
253         ppb_wctr(&sc->lpbb_dev, (u_char)(ppb_rctr(&sc->lpbb_dev)&~SCL_out));
254 else
255         ppb_wctr(&sc->lpbb_dev, (u_char)(ppb_rctr(&sc->lpbb_dev)|SCL_out));
256 }
257 
258 static int lpbb_detect(struct lpbb_softc *sc)
259 {
260 	if (ppb_request_bus(&sc->lpbb_dev, PPB_DONTWAIT)) {
261 		printf("lpbb: can't allocate ppbus\n");
262 		return (0);
263 	}
264 
265 	/* reset bus */
266 	setSDA(sc, 1);
267 	setSCL(sc, 1);
268 
269 	if ((ppb_rstr(&sc->lpbb_dev) & I2CKEY) ||
270 		((ppb_rstr(&sc->lpbb_dev) & ALIM) != ALIM)) {
271 
272 		ppb_release_bus(&sc->lpbb_dev);
273 		return (0);
274 	}
275 
276 	ppb_release_bus(&sc->lpbb_dev);
277 
278 	return (1);
279 }
280 
281 static int
282 lpbb_reset(device_t dev, u_char speed, u_char addr, u_char * oldaddr)
283 {
284 	struct lpbb_softc *sc = (struct lpbb_softc *)device_get_softc(dev);
285 
286 	/* reset bus */
287 	setSDA(sc, 1);
288 	setSCL(sc, 1);
289 
290 	return (IIC_ENOADDR);
291 }
292 
293 static void
294 lpbb_setlines(device_t dev, int ctrl, int data)
295 {
296 	struct lpbb_softc *sc = (struct lpbb_softc *)device_get_softc(dev);
297 
298 	setSCL(sc, ctrl);
299 	setSDA(sc, data);
300 }
301 
302 static int
303 lpbb_getdataline(device_t dev)
304 {
305 	struct lpbb_softc *sc = (struct lpbb_softc *)device_get_softc(dev);
306 
307 	return (getSDA(sc));
308 }
309 
310 static void
311 lpbb_print_child(device_t bus, device_t dev)
312 {
313 	printf(" on %s%d", device_get_name(bus), device_get_unit(bus));
314 
315 	return;
316 }
317 
318 DRIVER_MODULE(lpbb, root, lpbb_driver, lpbb_devclass, 0, 0);
319