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