iicbb.c revision 67164
1/*-
2 * Copyright (c) 1998 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: head/sys/dev/iicbus/iicbb.c 67164 2000-10-15 14:19:01Z phk $
27 *
28 */
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 * TODO: port Peter's generic bit-banging code <dufault@hda.com>
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
55#include <dev/iicbus/iiconf.h>
56#include <dev/iicbus/iicbus.h>
57
58#include <dev/smbus/smbconf.h>
59
60#include "iicbus_if.h"
61#include "iicbb_if.h"
62
63struct iicbb_softc {
64	int dummy;
65};
66
67static int iicbb_probe(device_t);
68static int iicbb_attach(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,	bus_generic_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
99static driver_t iicbb_driver = {
100	"iicbb",
101	iicbb_methods,
102	sizeof(struct iicbb_softc),
103};
104
105static devclass_t iicbb_devclass;
106
107static int iicbb_probe(device_t dev)
108{
109	device_set_desc(dev, "I2C generic bit-banging driver");
110
111	return (0);
112}
113
114static int iicbb_attach(device_t dev)
115{
116	return (0);
117}
118
119static int
120iicbb_print_child(device_t bus, device_t dev)
121{
122	int error;
123	int retval = 0;
124	u_char oldaddr;
125
126	retval += bus_print_child_header(bus, dev);
127	/* retrieve the interface I2C address */
128	error = IICBB_RESET(device_get_parent(bus), IIC_FASTEST, 0, &oldaddr);
129	if (error == IIC_ENOADDR) {
130		retval += printf(" on %s master-only\n",
131				 device_get_nameunit(bus));
132	} else {
133		/* restore the address */
134		IICBB_RESET(device_get_parent(bus), IIC_FASTEST, oldaddr, NULL);
135
136		retval += printf(" on %s addr 0x%x\n",
137				 device_get_nameunit(bus), oldaddr & 0xff);
138	}
139
140	return (retval);
141}
142
143#define I2C_SET(dev,ctrl,data) \
144	IICBB_SETLINES(device_get_parent(dev), ctrl, data)
145
146#define I2C_GET(dev) (IICBB_GETDATALINE(device_get_parent(dev)))
147
148static int i2c_debug = 0;
149#define I2C_DEBUG(x) if (i2c_debug) (x)
150
151static void iicbb_one(device_t dev)
152{
153	I2C_SET(dev,0,1);
154	I2C_SET(dev,1,1);
155	I2C_SET(dev,0,1);
156	return;
157}
158
159static void iicbb_zero(device_t dev)
160{
161	I2C_SET(dev,0,0);
162	I2C_SET(dev,1,0);
163	I2C_SET(dev,0,0);
164	return;
165}
166
167/*
168 * Waiting for ACKNOWLEDGE.
169 *
170 * When a chip is being addressed or has received data it will issue an
171 * ACKNOWLEDGE pulse. Therefore the MASTER must release the DATA line
172 * (set it to high level) and then release the CLOCK line.
173 * Now it must wait for the SLAVE to pull the DATA line low.
174 * Actually on the bus this looks like a START condition so nothing happens
175 * because of the fact that the IC's that have not been addressed are doing
176 * nothing.
177 *
178 * When the SLAVE has pulled this line low the MASTER will take the CLOCK
179 * line low and then the SLAVE will release the SDA (data) line.
180 */
181static int iicbb_ack(device_t dev, int timeout)
182{
183	int noack;
184	int k = timeout/10;
185
186	I2C_SET(dev,0,1);
187	I2C_SET(dev,1,1);
188
189	do {
190		noack = I2C_GET(dev);
191		if (!noack)
192			break;
193		DELAY(10);		/* XXX wait 10us */
194	} while (k--);
195
196	I2C_SET(dev,0,1);
197	I2C_DEBUG(printf("%c ",noack?'-':'+'));
198
199	return (noack);
200}
201
202static void iicbb_sendbyte(device_t dev, u_char data)
203{
204	int i;
205
206	I2C_SET(dev,0,0);
207	for (i=7; i>=0; i--)
208		(data&(1<<i)) ? iicbb_one(dev) : iicbb_zero(dev);
209	I2C_DEBUG(printf("w%02x",(int)data));
210	return;
211}
212
213static u_char iicbb_readbyte(device_t dev, int last)
214{
215	int i;
216	unsigned char data=0;
217
218	I2C_SET(dev,0,1);
219	for (i=7; i>=0; i--)
220	{
221		I2C_SET(dev,1,1);
222		if (I2C_GET(dev))
223			data |= (1<<i);
224		I2C_SET(dev,0,1);
225	}
226	last ? iicbb_one(dev) : iicbb_zero(dev);
227	I2C_DEBUG(printf("r%02x%c ",(int)data,last?'-':'+'));
228	return data;
229}
230
231static int iicbb_callback(device_t dev, int index, caddr_t data)
232{
233	return (IICBB_CALLBACK(device_get_parent(dev), index, data));
234}
235
236static int iicbb_reset(device_t dev, u_char speed, u_char addr, u_char *oldaddr)
237{
238	return (IICBB_RESET(device_get_parent(dev), speed, addr, oldaddr));
239}
240
241static int iicbb_start(device_t dev, u_char slave, int timeout)
242{
243	int error;
244
245	I2C_DEBUG(printf("<"));
246
247	I2C_SET(dev,0,1);
248	I2C_SET(dev,1,1);
249	I2C_SET(dev,1,0);
250	I2C_SET(dev,0,0);
251
252	/* send address */
253	iicbb_sendbyte(dev, slave);
254
255	/* check for ack */
256	if (iicbb_ack(dev, timeout)) {
257		error = IIC_ENOACK;
258		goto error;
259	}
260
261	return(0);
262
263error:
264	iicbb_stop(dev);
265	return (error);
266}
267
268static int iicbb_stop(device_t dev)
269{
270	I2C_SET(dev,0,0);
271	I2C_SET(dev,1,0);
272	I2C_SET(dev,1,1);
273	I2C_DEBUG(printf(">"));
274	return (0);
275}
276
277static int iicbb_write(device_t dev, char * buf, int len, int *sent,
278			int timeout)
279{
280	int bytes, error = 0;
281
282	bytes = 0;
283	while (len) {
284		/* send byte */
285		iicbb_sendbyte(dev,(u_char)*buf++);
286
287		/* check for ack */
288		if (iicbb_ack(dev, timeout)) {
289			error = IIC_ENOACK;
290			goto error;
291		}
292		bytes ++;
293		len --;
294	}
295
296error:
297	*sent = bytes;
298	return (error);
299}
300
301static int iicbb_read(device_t dev, char * buf, int len, int *read,
302			int last, int delay)
303{
304	int bytes;
305
306	bytes = 0;
307	while (len) {
308		/* XXX should insert delay here */
309		*buf++ = (char)iicbb_readbyte(dev, (len == 1) ? last : 0);
310
311		bytes ++;
312		len --;
313	}
314
315	*read = bytes;
316	return (0);
317}
318
319DRIVER_MODULE(iicbb, bti2c, iicbb_driver, iicbb_devclass, 0, 0);
320DRIVER_MODULE(iicbb, lpbb, iicbb_driver, iicbb_devclass, 0, 0);
321