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