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