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