xref: /freebsd/sys/dev/iicbus/iicbb.c (revision f05cddf9)
1 /*-
2  * Copyright (c) 1998, 2001 Nicolas Souchu
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 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29 
30 /*
31  * Generic I2C bit-banging code
32  *
33  * Example:
34  *
35  *	iicbus
36  *	 /  \
37  *    iicbb pcf
38  *     |  \
39  *   bti2c lpbb
40  *
41  * From Linux I2C generic interface
42  * (c) 1998 Gerd Knorr <kraxel@cs.tu-berlin.de>
43  *
44  */
45 
46 #include <sys/param.h>
47 #include <sys/kernel.h>
48 #include <sys/systm.h>
49 #include <sys/module.h>
50 #include <sys/bus.h>
51 #include <sys/uio.h>
52 
53 
54 #include <dev/iicbus/iiconf.h>
55 #include <dev/iicbus/iicbus.h>
56 
57 #include <dev/smbus/smbconf.h>
58 
59 #include "iicbus_if.h"
60 #include "iicbb_if.h"
61 
62 struct iicbb_softc {
63 	device_t iicbus;
64 	int udelay;		/* signal toggle delay in usec */
65 };
66 
67 static int iicbb_attach(device_t);
68 static void iicbb_child_detached(device_t, device_t);
69 static int iicbb_detach(device_t);
70 static int iicbb_print_child(device_t, device_t);
71 static int iicbb_probe(device_t);
72 
73 static int iicbb_callback(device_t, int, caddr_t);
74 static int iicbb_start(device_t, u_char, int);
75 static int iicbb_stop(device_t);
76 static int iicbb_write(device_t, const char *, int, int *, int);
77 static int iicbb_read(device_t, char *, int, int *, int, int);
78 static int iicbb_reset(device_t, u_char, u_char, u_char *);
79 static int iicbb_transfer(device_t dev, struct iic_msg *msgs, uint32_t nmsgs);
80 
81 static device_method_t iicbb_methods[] = {
82 	/* device interface */
83 	DEVMETHOD(device_probe,		iicbb_probe),
84 	DEVMETHOD(device_attach,	iicbb_attach),
85 	DEVMETHOD(device_detach,	iicbb_detach),
86 
87 	/* bus interface */
88 	DEVMETHOD(bus_child_detached,	iicbb_child_detached),
89 	DEVMETHOD(bus_print_child,	iicbb_print_child),
90 
91 	/* iicbus interface */
92 	DEVMETHOD(iicbus_callback,	iicbb_callback),
93 	DEVMETHOD(iicbus_start,		iicbb_start),
94 	DEVMETHOD(iicbus_repeated_start, iicbb_start),
95 	DEVMETHOD(iicbus_stop,		iicbb_stop),
96 	DEVMETHOD(iicbus_write,		iicbb_write),
97 	DEVMETHOD(iicbus_read,		iicbb_read),
98 	DEVMETHOD(iicbus_reset,		iicbb_reset),
99 	DEVMETHOD(iicbus_transfer,	iicbb_transfer),
100 
101 	{ 0, 0 }
102 };
103 
104 driver_t iicbb_driver = {
105 	"iicbb",
106 	iicbb_methods,
107 	sizeof(struct iicbb_softc),
108 };
109 
110 devclass_t iicbb_devclass;
111 
112 static int
113 iicbb_probe(device_t dev)
114 {
115 	device_set_desc(dev, "I2C bit-banging driver");
116 
117 	return (0);
118 }
119 
120 static int
121 iicbb_attach(device_t dev)
122 {
123 	struct iicbb_softc *sc = (struct iicbb_softc *)device_get_softc(dev);
124 
125 	sc->iicbus = device_add_child(dev, "iicbus", -1);
126 	if (!sc->iicbus)
127 		return (ENXIO);
128 	sc->udelay = 10;		/* 10 uS default */
129 	bus_generic_attach(dev);
130 
131 	return (0);
132 }
133 
134 static int
135 iicbb_detach(device_t dev)
136 {
137 	struct iicbb_softc *sc = (struct iicbb_softc *)device_get_softc(dev);
138 	device_t child;
139 
140 	/*
141 	 * We need to save child because the detach indirectly causes
142 	 * sc->iicbus to be zeroed.  Since we added the device
143 	 * unconditionally in iicbb_attach, we need to make sure we
144 	 * delete it here.  See iicbb_child_detached.  We need that
145 	 * callback in case newbus detached our children w/o detaching
146 	 * us (say iicbus is a module and unloaded w/o iicbb being
147 	 * unloaded).
148 	 */
149 	child = sc->iicbus;
150 	bus_generic_detach(dev);
151 	if (child)
152 		device_delete_child(dev, child);
153 
154 	return (0);
155 }
156 
157 static void
158 iicbb_child_detached( device_t dev, device_t child )
159 {
160 	struct iicbb_softc *sc = (struct iicbb_softc *)device_get_softc(dev);
161 
162 	if (child == sc->iicbus)
163 		sc->iicbus = NULL;
164 }
165 
166 static int
167 iicbb_print_child(device_t bus, device_t dev)
168 {
169 	int error;
170 	int retval = 0;
171 	u_char oldaddr;
172 
173 	retval += bus_print_child_header(bus, dev);
174 	/* retrieve the interface I2C address */
175 	error = IICBB_RESET(device_get_parent(bus), IIC_FASTEST, 0, &oldaddr);
176 	if (error == IIC_ENOADDR) {
177 		retval += printf(" on %s master-only\n",
178 				 device_get_nameunit(bus));
179 	} else {
180 		/* restore the address */
181 		IICBB_RESET(device_get_parent(bus), IIC_FASTEST, oldaddr, NULL);
182 
183 		retval += printf(" on %s addr 0x%x\n",
184 				 device_get_nameunit(bus), oldaddr & 0xff);
185 	}
186 
187 	return (retval);
188 }
189 
190 #define I2C_SETSDA(sc,dev,val) do {			\
191 	IICBB_SETSDA(device_get_parent(dev), val);	\
192 	DELAY(sc->udelay);				\
193 	} while (0)
194 
195 #define I2C_SETSCL(dev,val) do {			\
196 	iicbb_setscl(dev, val, 100);			\
197 	} while (0)
198 
199 #define I2C_SET(sc,dev,ctrl,data) do {			\
200 	I2C_SETSCL(dev, ctrl);				\
201 	I2C_SETSDA(sc, dev, data);			\
202 	} while (0)
203 
204 #define I2C_GETSDA(dev) (IICBB_GETSDA(device_get_parent(dev)))
205 
206 #define I2C_GETSCL(dev) (IICBB_GETSCL(device_get_parent(dev)))
207 
208 static int i2c_debug = 0;
209 #define I2C_DEBUG(x)	do {					\
210 				if (i2c_debug) (x);		\
211 			} while (0)
212 
213 #define I2C_LOG(format,args...)	do {				\
214 					printf(format, args);	\
215 				} while (0)
216 
217 static void
218 iicbb_setscl(device_t dev, int val, int timeout)
219 {
220 	struct iicbb_softc *sc = device_get_softc(dev);
221 	int k = 0;
222 
223 	IICBB_SETSCL(device_get_parent(dev), val);
224 	DELAY(sc->udelay);
225 
226 	while (val && !I2C_GETSCL(dev) && k++ < timeout) {
227 		IICBB_SETSCL(device_get_parent(dev), val);
228 		DELAY(sc->udelay);
229 	}
230 
231 	return;
232 }
233 
234 static void
235 iicbb_one(device_t dev, int timeout)
236 {
237 	struct iicbb_softc *sc = device_get_softc(dev);
238 
239 	I2C_SET(sc,dev,0,1);
240 	I2C_SET(sc,dev,1,1);
241 	I2C_SET(sc,dev,0,1);
242 	return;
243 }
244 
245 static void
246 iicbb_zero(device_t dev, int timeout)
247 {
248 	struct iicbb_softc *sc = device_get_softc(dev);
249 
250 	I2C_SET(sc,dev,0,0);
251 	I2C_SET(sc,dev,1,0);
252 	I2C_SET(sc,dev,0,0);
253 	return;
254 }
255 
256 /*
257  * Waiting for ACKNOWLEDGE.
258  *
259  * When a chip is being addressed or has received data it will issue an
260  * ACKNOWLEDGE pulse. Therefore the MASTER must release the DATA line
261  * (set it to high level) and then release the CLOCK line.
262  * Now it must wait for the SLAVE to pull the DATA line low.
263  * Actually on the bus this looks like a START condition so nothing happens
264  * because of the fact that the IC's that have not been addressed are doing
265  * nothing.
266  *
267  * When the SLAVE has pulled this line low the MASTER will take the CLOCK
268  * line low and then the SLAVE will release the SDA (data) line.
269  */
270 static int
271 iicbb_ack(device_t dev, int timeout)
272 {
273 	struct iicbb_softc *sc = device_get_softc(dev);
274 	int noack;
275 	int k = 0;
276 
277 	I2C_SET(sc,dev,0,1);
278 	I2C_SET(sc,dev,1,1);
279 	do {
280 		noack = I2C_GETSDA(dev);
281 		if (!noack)
282 			break;
283 		DELAY(1);
284 		k++;
285 	} while (k < timeout);
286 
287 	I2C_SET(sc,dev,0,1);
288 	I2C_DEBUG(printf("%c ",noack?'-':'+'));
289 
290 	return (noack);
291 }
292 
293 static void
294 iicbb_sendbyte(device_t dev, u_char data, int timeout)
295 {
296 	int i;
297 
298 	for (i=7; i>=0; i--) {
299 		if (data&(1<<i)) {
300 			iicbb_one(dev, timeout);
301 		} else {
302 			iicbb_zero(dev, timeout);
303 		}
304 	}
305 	I2C_DEBUG(printf("w%02x",(int)data));
306 	return;
307 }
308 
309 static u_char
310 iicbb_readbyte(device_t dev, int last, int timeout)
311 {
312 	struct iicbb_softc *sc = device_get_softc(dev);
313 	int i;
314 	unsigned char data=0;
315 
316 	I2C_SET(sc,dev,0,1);
317 	for (i=7; i>=0; i--)
318 	{
319 		I2C_SET(sc,dev,1,1);
320 		if (I2C_GETSDA(dev))
321 			data |= (1<<i);
322 		I2C_SET(sc,dev,0,1);
323 	}
324 	if (last) {
325 		iicbb_one(dev, timeout);
326 	} else {
327 		iicbb_zero(dev, timeout);
328 	}
329 	I2C_DEBUG(printf("r%02x%c ",(int)data,last?'-':'+'));
330 	return data;
331 }
332 
333 static int
334 iicbb_callback(device_t dev, int index, caddr_t data)
335 {
336 	return (IICBB_CALLBACK(device_get_parent(dev), index, data));
337 }
338 
339 static int
340 iicbb_reset(device_t dev, u_char speed, u_char addr, u_char *oldaddr)
341 {
342 	return (IICBB_RESET(device_get_parent(dev), speed, addr, oldaddr));
343 }
344 
345 static int
346 iicbb_start(device_t dev, u_char slave, int timeout)
347 {
348 	struct iicbb_softc *sc = device_get_softc(dev);
349 	int error;
350 
351 	I2C_DEBUG(printf("<"));
352 
353 	I2C_SET(sc,dev,1,1);
354 	I2C_SET(sc,dev,1,0);
355 	I2C_SET(sc,dev,0,0);
356 
357 	/* send address */
358 	iicbb_sendbyte(dev, slave, timeout);
359 
360 	/* check for ack */
361 	if (iicbb_ack(dev, timeout)) {
362 		error = IIC_ENOACK;
363 		goto error;
364 	}
365 
366 	return(0);
367 
368 error:
369 	iicbb_stop(dev);
370 	return (error);
371 }
372 
373 static int
374 iicbb_stop(device_t dev)
375 {
376 	struct iicbb_softc *sc = device_get_softc(dev);
377 
378 	I2C_SET(sc,dev,0,0);
379 	I2C_SET(sc,dev,1,0);
380 	I2C_SET(sc,dev,1,1);
381 	I2C_DEBUG(printf(">"));
382 	I2C_DEBUG(printf("\n"));
383 	return (0);
384 }
385 
386 static int
387 iicbb_write(device_t dev, const char *buf, int len, int *sent, int timeout)
388 {
389 	int bytes, error = 0;
390 
391 	bytes = 0;
392 	while (len) {
393 		/* send byte */
394 		iicbb_sendbyte(dev,(u_char)*buf++, timeout);
395 
396 		/* check for ack */
397 		if (iicbb_ack(dev, timeout)) {
398 			error = IIC_ENOACK;
399 			goto error;
400 		}
401 		bytes ++;
402 		len --;
403 	}
404 
405 error:
406 	*sent = bytes;
407 	return (error);
408 }
409 
410 static int
411 iicbb_read(device_t dev, char * buf, int len, int *read, int last, int delay)
412 {
413 	int bytes;
414 
415 	bytes = 0;
416 	while (len) {
417 		/* XXX should insert delay here */
418 		*buf++ = (char)iicbb_readbyte(dev, (len == 1) ? last : 0, delay);
419 
420 		bytes ++;
421 		len --;
422 	}
423 
424 	*read = bytes;
425 	return (0);
426 }
427 
428 static int
429 iicbb_transfer(device_t dev, struct iic_msg *msgs, uint32_t nmsgs)
430 {
431 	int error;
432 
433 	error = IICBB_PRE_XFER(device_get_parent(dev));
434 	if (error)
435 		return (error);
436 
437 	error = iicbus_transfer_gen(dev, msgs, nmsgs);
438 
439 	IICBB_POST_XFER(device_get_parent(dev));
440 	return (error);
441 }
442 
443 DRIVER_MODULE(iicbus, iicbb, iicbus_driver, iicbus_devclass, 0, 0);
444 
445 MODULE_DEPEND(iicbb, iicbus, IICBUS_MINVER, IICBUS_PREFVER, IICBUS_MAXVER);
446 MODULE_VERSION(iicbb, IICBB_MODVER);
447