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