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