iicbb.c revision 161516
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: head/sys/dev/iicbus/iicbb.c 161516 2006-08-21 17:32:50Z imp $");
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
62struct iicbb_softc {
63	device_t iicbus;
64};
65
66static int iicbb_probe(device_t);
67static int iicbb_attach(device_t);
68static int iicbb_detach(device_t);
69static int iicbb_print_child(device_t, device_t);
70
71static int iicbb_callback(device_t, int, caddr_t);
72static int iicbb_start(device_t, u_char, int);
73static int iicbb_stop(device_t);
74static int iicbb_write(device_t, char *, int, int *, int);
75static int iicbb_read(device_t, char *, int, int *, int, int);
76static int iicbb_reset(device_t, u_char, u_char, u_char *);
77
78static device_method_t iicbb_methods[] = {
79	/* device interface */
80	DEVMETHOD(device_probe,		iicbb_probe),
81	DEVMETHOD(device_attach,	iicbb_attach),
82	DEVMETHOD(device_detach,	iicbb_detach),
83
84	/* bus interface */
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
96	{ 0, 0 }
97};
98
99driver_t iicbb_driver = {
100	"iicbb",
101	iicbb_methods,
102	sizeof(struct iicbb_softc),
103};
104
105devclass_t iicbb_devclass;
106
107static int
108iicbb_probe(device_t dev)
109{
110	device_set_desc(dev, "I2C bit-banging driver");
111
112	return (0);
113}
114
115static int
116iicbb_attach(device_t dev)
117{
118	struct iicbb_softc *sc = (struct iicbb_softc *)device_get_softc(dev);
119
120	sc->iicbus = device_add_child(dev, "iicbus", -1);
121	if (!sc->iicbus)
122		return (ENXIO);
123	bus_generic_attach(dev);
124
125	return (0);
126}
127
128static int
129iicbb_detach(device_t dev)
130{
131	struct iicbb_softc *sc = (struct iicbb_softc *)device_get_softc(dev);
132
133	if (sc->iicbus) {
134		bus_generic_detach(dev);
135		device_delete_child(dev, sc->iicbus);
136	}
137
138	return (0);
139}
140
141static int
142iicbb_print_child(device_t bus, device_t dev)
143{
144	int error;
145	int retval = 0;
146	u_char oldaddr;
147
148	retval += bus_print_child_header(bus, dev);
149	/* retrieve the interface I2C address */
150	error = IICBB_RESET(device_get_parent(bus), IIC_FASTEST, 0, &oldaddr);
151	if (error == IIC_ENOADDR) {
152		retval += printf(" on %s master-only\n",
153				 device_get_nameunit(bus));
154	} else {
155		/* restore the address */
156		IICBB_RESET(device_get_parent(bus), IIC_FASTEST, oldaddr, NULL);
157
158		retval += printf(" on %s addr 0x%x\n",
159				 device_get_nameunit(bus), oldaddr & 0xff);
160	}
161
162	return (retval);
163}
164
165#define IIC_DELAY	10
166
167#define I2C_SETSDA(dev,val) do {			\
168	IICBB_SETSDA(device_get_parent(dev), val);	\
169	DELAY(IIC_DELAY);				\
170	} while (0)
171
172#define I2C_SETSCL(dev,val) do {			\
173	iicbb_setscl(dev, val, 100);			\
174	} while (0)
175
176#define I2C_SET(dev,ctrl,data) do {			\
177	I2C_SETSCL(dev, ctrl);				\
178	I2C_SETSDA(dev, data);				\
179	} while (0)
180
181#define I2C_GETSDA(dev) (IICBB_GETSDA(device_get_parent(dev)))
182
183#define I2C_GETSCL(dev) (IICBB_GETSCL(device_get_parent(dev)))
184
185static int i2c_debug = 0;
186#define I2C_DEBUG(x)	do {					\
187				if (i2c_debug) (x);		\
188			} while (0)
189
190#define I2C_LOG(format,args...)	do {				\
191					printf(format, args);	\
192				} while (0)
193
194static void
195iicbb_setscl(device_t dev, int val, int timeout)
196{
197	int k = 0;
198
199	IICBB_SETSCL(device_get_parent(dev), val);
200	DELAY(IIC_DELAY);
201
202	while (val && !I2C_GETSCL(dev) && k++ < timeout) {
203		IICBB_SETSCL(device_get_parent(dev), val);
204		DELAY(IIC_DELAY);
205	}
206
207	return;
208}
209
210static void
211iicbb_one(device_t dev, int timeout)
212{
213	I2C_SET(dev,0,1);
214	I2C_SET(dev,1,1);
215	I2C_SET(dev,0,1);
216	return;
217}
218
219static void
220iicbb_zero(device_t dev, int timeout)
221{
222	I2C_SET(dev,0,0);
223	I2C_SET(dev,1,0);
224	I2C_SET(dev,0,0);
225	return;
226}
227
228/*
229 * Waiting for ACKNOWLEDGE.
230 *
231 * When a chip is being addressed or has received data it will issue an
232 * ACKNOWLEDGE pulse. Therefore the MASTER must release the DATA line
233 * (set it to high level) and then release the CLOCK line.
234 * Now it must wait for the SLAVE to pull the DATA line low.
235 * Actually on the bus this looks like a START condition so nothing happens
236 * because of the fact that the IC's that have not been addressed are doing
237 * nothing.
238 *
239 * When the SLAVE has pulled this line low the MASTER will take the CLOCK
240 * line low and then the SLAVE will release the SDA (data) line.
241 */
242static int
243iicbb_ack(device_t dev, int timeout)
244{
245	int noack;
246	int k = 0;
247
248	I2C_SET(dev,0,1);
249	I2C_SET(dev,1,1);
250	do {
251		noack = I2C_GETSDA(dev);
252		if (!noack)
253			break;
254		DELAY(10);
255		k += 10;
256	} while (k < timeout);
257
258	I2C_SET(dev,0,1);
259	I2C_DEBUG(printf("%c ",noack?'-':'+'));
260
261	return (noack);
262}
263
264static void
265iicbb_sendbyte(device_t dev, u_char data, int timeout)
266{
267	int i;
268
269	for (i=7; i>=0; i--) {
270		if (data&(1<<i)) {
271			iicbb_one(dev, timeout);
272		} else {
273			iicbb_zero(dev, timeout);
274		}
275	}
276	I2C_DEBUG(printf("w%02x",(int)data));
277	return;
278}
279
280static u_char
281iicbb_readbyte(device_t dev, int last, int timeout)
282{
283	int i;
284	unsigned char data=0;
285
286	I2C_SET(dev,0,1);
287	for (i=7; i>=0; i--)
288	{
289		I2C_SET(dev,1,1);
290		if (I2C_GETSDA(dev))
291			data |= (1<<i);
292		I2C_SET(dev,0,1);
293	}
294	if (last) {
295		iicbb_one(dev, timeout);
296	} else {
297		iicbb_zero(dev, timeout);
298	}
299	I2C_DEBUG(printf("r%02x%c ",(int)data,last?'-':'+'));
300	return data;
301}
302
303static int
304iicbb_callback(device_t dev, int index, caddr_t data)
305{
306	return (IICBB_CALLBACK(device_get_parent(dev), index, data));
307}
308
309static int
310iicbb_reset(device_t dev, u_char speed, u_char addr, u_char *oldaddr)
311{
312	return (IICBB_RESET(device_get_parent(dev), speed, addr, oldaddr));
313}
314
315static int
316iicbb_start(device_t dev, u_char slave, int timeout)
317{
318	int error;
319
320	I2C_DEBUG(printf("<"));
321
322	I2C_SET(dev,1,1);
323	I2C_SET(dev,1,0);
324	I2C_SET(dev,0,0);
325
326	/* send address */
327	iicbb_sendbyte(dev, slave, timeout);
328
329	/* check for ack */
330	if (iicbb_ack(dev, timeout)) {
331		error = IIC_ENOACK;
332		goto error;
333	}
334
335	return(0);
336
337error:
338	iicbb_stop(dev);
339	return (error);
340}
341
342static int
343iicbb_stop(device_t dev)
344{
345	I2C_SET(dev,0,0);
346	I2C_SET(dev,1,0);
347	I2C_SET(dev,1,1);
348	I2C_DEBUG(printf(">"));
349	return (0);
350}
351
352static int
353iicbb_write(device_t dev, char * buf, int len, int *sent, int timeout)
354{
355	int bytes, error = 0;
356
357	bytes = 0;
358	while (len) {
359		/* send byte */
360		iicbb_sendbyte(dev,(u_char)*buf++, timeout);
361
362		/* check for ack */
363		if (iicbb_ack(dev, timeout)) {
364			error = IIC_ENOACK;
365			goto error;
366		}
367		bytes ++;
368		len --;
369	}
370
371error:
372	*sent = bytes;
373	return (error);
374}
375
376static int
377iicbb_read(device_t dev, char * buf, int len, int *read, int last, int delay)
378{
379	int bytes;
380
381	bytes = 0;
382	while (len) {
383		/* XXX should insert delay here */
384		*buf++ = (char)iicbb_readbyte(dev, (len == 1) ? last : 0, delay);
385
386		bytes ++;
387		len --;
388	}
389
390	*read = bytes;
391	return (0);
392}
393
394DRIVER_MODULE(iicbb, bktr, iicbb_driver, iicbb_devclass, 0, 0);
395DRIVER_MODULE(iicbb, lpbb, iicbb_driver, iicbb_devclass, 0, 0);
396DRIVER_MODULE(iicbb, viapm, iicbb_driver, iicbb_devclass, 0, 0);
397
398MODULE_DEPEND(iicbb, iicbus, IICBUS_MINVER, IICBUS_PREFVER, IICBUS_MAXVER);
399MODULE_VERSION(iicbb, IICBB_MODVER);
400