blob: d9d0ec49e60d2149b3a653856de4703674f9baa3 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* ------------------------------------------------------------------------- */
2/* i2c-algo-bit.c i2c driver algorithms for bit-shift adapters */
3/* ------------------------------------------------------------------------- */
4/* Copyright (C) 1995-2000 Simon G. Vogl
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
19/* ------------------------------------------------------------------------- */
20
21/* With some changes from Frodo Looijaard <frodol@dds.nl>, Kyösti Mälkki
22 <kmalkki@cc.hut.fi> and Jean Delvare <khali@linux-fr.org> */
23
24#include <linux/kernel.h>
25#include <linux/module.h>
26#include <linux/delay.h>
27#include <linux/slab.h>
28#include <linux/init.h>
29#include <linux/errno.h>
30#include <linux/sched.h>
31#include <linux/i2c.h>
32#include <linux/i2c-algo-bit.h>
33
34
35/* ----- global defines ----------------------------------------------- */
36#define DEB(x) if (i2c_debug>=1) x;
37#define DEB2(x) if (i2c_debug>=2) x;
38#define DEBSTAT(x) if (i2c_debug>=3) x; /* print several statistical values*/
39#define DEBPROTO(x) if (i2c_debug>=9) { x; }
40 /* debug the protocol by showing transferred bits */
41
42
43/* ----- global variables --------------------------------------------- */
44
45/* module parameters:
46 */
47static int i2c_debug;
48static int bit_test; /* see if the line-setting functions work */
49
50/* --- setting states on the bus with the right timing: --------------- */
51
52#define setsda(adap,val) adap->setsda(adap->data, val)
53#define setscl(adap,val) adap->setscl(adap->data, val)
54#define getsda(adap) adap->getsda(adap->data)
55#define getscl(adap) adap->getscl(adap->data)
56
57static inline void sdalo(struct i2c_algo_bit_data *adap)
58{
59 setsda(adap,0);
Jean Delvare424ed672007-05-01 23:26:33 +020060 udelay((adap->udelay + 1) / 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -070061}
62
63static inline void sdahi(struct i2c_algo_bit_data *adap)
64{
65 setsda(adap,1);
Jean Delvare424ed672007-05-01 23:26:33 +020066 udelay((adap->udelay + 1) / 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -070067}
68
69static inline void scllo(struct i2c_algo_bit_data *adap)
70{
71 setscl(adap,0);
Jean Delvare424ed672007-05-01 23:26:33 +020072 udelay(adap->udelay / 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -070073}
74
75/*
76 * Raise scl line, and do checking for delays. This is necessary for slower
77 * devices.
78 */
Jean Delvare7b288a02006-09-03 22:22:12 +020079static int sclhi(struct i2c_algo_bit_data *adap)
Linus Torvalds1da177e2005-04-16 15:20:36 -070080{
81 unsigned long start;
82
83 setscl(adap,1);
84
85 /* Not all adapters have scl sense line... */
Jean Delvare7b288a02006-09-03 22:22:12 +020086 if (!adap->getscl)
87 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -070088
89 start=jiffies;
90 while (! getscl(adap) ) {
91 /* the hw knows how to read the clock line,
92 * so we wait until it actually gets high.
93 * This is safer as some chips may hold it low
94 * while they are processing data internally.
95 */
96 if (time_after_eq(jiffies, start+adap->timeout)) {
97 return -ETIMEDOUT;
98 }
99 cond_resched();
100 }
101 DEBSTAT(printk(KERN_DEBUG "needed %ld jiffies\n", jiffies-start));
Jean Delvare7b288a02006-09-03 22:22:12 +0200102
103done:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 udelay(adap->udelay);
105 return 0;
106}
107
108
109/* --- other auxiliary functions -------------------------------------- */
110static void i2c_start(struct i2c_algo_bit_data *adap)
111{
112 /* assert: scl, sda are high */
113 DEBPROTO(printk("S "));
Jean Delvare424ed672007-05-01 23:26:33 +0200114 setsda(adap, 0);
115 udelay(adap->udelay);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 scllo(adap);
117}
118
119static void i2c_repstart(struct i2c_algo_bit_data *adap)
120{
Jean Delvare424ed672007-05-01 23:26:33 +0200121 /* assert: scl is low */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 DEBPROTO(printk(" Sr "));
Jean Delvare424ed672007-05-01 23:26:33 +0200123 sdahi(adap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 sclhi(adap);
Jean Delvare424ed672007-05-01 23:26:33 +0200125 setsda(adap, 0);
126 udelay(adap->udelay);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 scllo(adap);
128}
129
130
131static void i2c_stop(struct i2c_algo_bit_data *adap)
132{
133 DEBPROTO(printk("P\n"));
134 /* assert: scl is low */
135 sdalo(adap);
136 sclhi(adap);
Jean Delvare424ed672007-05-01 23:26:33 +0200137 setsda(adap, 1);
138 udelay(adap->udelay);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139}
140
141
142
143/* send a byte without start cond., look for arbitration,
144 check ackn. from slave */
145/* returns:
146 * 1 if the device acknowledged
147 * 0 if the device did not ack
148 * -ETIMEDOUT if an error occurred (while raising the scl line)
149 */
150static int i2c_outb(struct i2c_adapter *i2c_adap, char c)
151{
152 int i;
153 int sb;
154 int ack;
155 struct i2c_algo_bit_data *adap = i2c_adap->algo_data;
156
157 /* assert: scl is low */
158 for ( i=7 ; i>=0 ; i-- ) {
159 sb = c & ( 1 << i );
160 setsda(adap,sb);
Jean Delvare424ed672007-05-01 23:26:33 +0200161 udelay((adap->udelay + 1) / 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 DEBPROTO(printk(KERN_DEBUG "%d",sb!=0));
163 if (sclhi(adap)<0) { /* timed out */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 DEB2(printk(KERN_DEBUG " i2c_outb: 0x%02x, timeout at bit #%d\n", c&0xff, i));
165 return -ETIMEDOUT;
166 };
167 /* do arbitration here:
168 * if ( sb && ! getsda(adap) ) -> ouch! Get out of here.
169 */
Jean Delvare424ed672007-05-01 23:26:33 +0200170 scllo(adap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 }
172 sdahi(adap);
173 if (sclhi(adap)<0){ /* timeout */
174 DEB2(printk(KERN_DEBUG " i2c_outb: 0x%02x, timeout at ack\n", c&0xff));
175 return -ETIMEDOUT;
176 };
177 /* read ack: SDA should be pulled down by slave */
178 ack=getsda(adap); /* ack: sda is pulled low ->success. */
179 DEB2(printk(KERN_DEBUG " i2c_outb: 0x%02x , getsda() = %d\n", c & 0xff, ack));
180
181 DEBPROTO( printk(KERN_DEBUG "[%2.2x]",c&0xff) );
182 DEBPROTO(if (0==ack){ printk(KERN_DEBUG " A ");} else printk(KERN_DEBUG " NA ") );
183 scllo(adap);
184 return 0==ack; /* return 1 if device acked */
185 /* assert: scl is low (sda undef) */
186}
187
188
189static int i2c_inb(struct i2c_adapter *i2c_adap)
190{
191 /* read byte via i2c port, without start/stop sequence */
192 /* acknowledge is sent in i2c_read. */
193 int i;
194 unsigned char indata=0;
195 struct i2c_algo_bit_data *adap = i2c_adap->algo_data;
196
197 /* assert: scl is low */
198 sdahi(adap);
199 for (i=0;i<8;i++) {
200 if (sclhi(adap)<0) { /* timeout */
201 DEB2(printk(KERN_DEBUG " i2c_inb: timeout at bit #%d\n", 7-i));
202 return -ETIMEDOUT;
203 };
204 indata *= 2;
205 if ( getsda(adap) )
206 indata |= 0x01;
Jean Delvare424ed672007-05-01 23:26:33 +0200207 setscl(adap, 0);
208 udelay(i == 7 ? adap->udelay / 2 : adap->udelay);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 }
210 /* assert: scl is low */
211 DEB2(printk(KERN_DEBUG "i2c_inb: 0x%02x\n", indata & 0xff));
212
213 DEBPROTO(printk(KERN_DEBUG " 0x%02x", indata & 0xff));
214 return (int) (indata & 0xff);
215}
216
217/*
218 * Sanity check for the adapter hardware - check the reaction of
219 * the bus lines only if it seems to be idle.
220 */
221static int test_bus(struct i2c_algo_bit_data *adap, char* name) {
222 int scl,sda;
223
224 if (adap->getscl==NULL)
225 printk(KERN_INFO "i2c-algo-bit.o: Testing SDA only, "
226 "SCL is not readable.\n");
227
228 sda=getsda(adap);
229 scl=(adap->getscl==NULL?1:getscl(adap));
230 printk(KERN_DEBUG "i2c-algo-bit.o: (0) scl=%d, sda=%d\n",scl,sda);
231 if (!scl || !sda ) {
232 printk(KERN_WARNING "i2c-algo-bit.o: %s seems to be busy.\n", name);
233 goto bailout;
234 }
235
236 sdalo(adap);
237 sda=getsda(adap);
238 scl=(adap->getscl==NULL?1:getscl(adap));
239 printk(KERN_DEBUG "i2c-algo-bit.o: (1) scl=%d, sda=%d\n",scl,sda);
240 if ( 0 != sda ) {
241 printk(KERN_WARNING "i2c-algo-bit.o: SDA stuck high!\n");
242 goto bailout;
243 }
244 if ( 0 == scl ) {
245 printk(KERN_WARNING "i2c-algo-bit.o: SCL unexpected low "
246 "while pulling SDA low!\n");
247 goto bailout;
248 }
249
250 sdahi(adap);
251 sda=getsda(adap);
252 scl=(adap->getscl==NULL?1:getscl(adap));
253 printk(KERN_DEBUG "i2c-algo-bit.o: (2) scl=%d, sda=%d\n",scl,sda);
254 if ( 0 == sda ) {
255 printk(KERN_WARNING "i2c-algo-bit.o: SDA stuck low!\n");
256 goto bailout;
257 }
258 if ( 0 == scl ) {
259 printk(KERN_WARNING "i2c-algo-bit.o: SCL unexpected low "
260 "while pulling SDA high!\n");
261 goto bailout;
262 }
263
264 scllo(adap);
265 sda=getsda(adap);
266 scl=(adap->getscl==NULL?0:getscl(adap));
267 printk(KERN_DEBUG "i2c-algo-bit.o: (3) scl=%d, sda=%d\n",scl,sda);
268 if ( 0 != scl ) {
269 printk(KERN_WARNING "i2c-algo-bit.o: SCL stuck high!\n");
270 goto bailout;
271 }
272 if ( 0 == sda ) {
273 printk(KERN_WARNING "i2c-algo-bit.o: SDA unexpected low "
274 "while pulling SCL low!\n");
275 goto bailout;
276 }
277
278 sclhi(adap);
279 sda=getsda(adap);
280 scl=(adap->getscl==NULL?1:getscl(adap));
281 printk(KERN_DEBUG "i2c-algo-bit.o: (4) scl=%d, sda=%d\n",scl,sda);
282 if ( 0 == scl ) {
283 printk(KERN_WARNING "i2c-algo-bit.o: SCL stuck low!\n");
284 goto bailout;
285 }
286 if ( 0 == sda ) {
287 printk(KERN_WARNING "i2c-algo-bit.o: SDA unexpected low "
288 "while pulling SCL high!\n");
289 goto bailout;
290 }
291 printk(KERN_INFO "i2c-algo-bit.o: %s passed test.\n",name);
292 return 0;
293bailout:
294 sdahi(adap);
295 sclhi(adap);
296 return -ENODEV;
297}
298
299/* ----- Utility functions
300 */
301
302/* try_address tries to contact a chip for a number of
303 * times before it gives up.
304 * return values:
305 * 1 chip answered
306 * 0 chip did not answer
307 * -x transmission error
308 */
Jean Delvare7b288a02006-09-03 22:22:12 +0200309static int try_address(struct i2c_adapter *i2c_adap,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 unsigned char addr, int retries)
311{
312 struct i2c_algo_bit_data *adap = i2c_adap->algo_data;
313 int i,ret = -1;
314 for (i=0;i<=retries;i++) {
315 ret = i2c_outb(i2c_adap,addr);
Jean Delvare1ecac072007-05-01 23:26:28 +0200316 if (ret == 1 || i == retries)
317 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 i2c_stop(adap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 udelay(adap->udelay);
Jean Delvare424ed672007-05-01 23:26:33 +0200320 yield();
321 i2c_start(adap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 }
323 DEB2(if (i)
324 printk(KERN_DEBUG "i2c-algo-bit.o: Used %d tries to %s client at 0x%02x : %s\n",
325 i+1, addr & 1 ? "read" : "write", addr>>1,
326 ret==1 ? "success" : ret==0 ? "no ack" : "failed, timeout?" )
327 );
328 return ret;
329}
330
331static int sendbytes(struct i2c_adapter *i2c_adap, struct i2c_msg *msg)
332{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 char c;
334 const char *temp = msg->buf;
335 int count = msg->len;
336 unsigned short nak_ok = msg->flags & I2C_M_IGNORE_NAK;
337 int retval;
338 int wrcount=0;
339
340 while (count > 0) {
341 c = *temp;
342 DEB2(dev_dbg(&i2c_adap->dev, "sendbytes: writing %2.2X\n", c&0xff));
343 retval = i2c_outb(i2c_adap,c);
344 if ((retval>0) || (nak_ok && (retval==0))) { /* ok or ignored NAK */
345 count--;
346 temp++;
347 wrcount++;
348 } else { /* arbitration or no acknowledge */
349 dev_err(&i2c_adap->dev, "sendbytes: error - bailout.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 return (retval<0)? retval : -EFAULT;
351 /* got a better one ?? */
352 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 }
354 return wrcount;
355}
356
Jean Delvare7b288a02006-09-03 22:22:12 +0200357static int readbytes(struct i2c_adapter *i2c_adap, struct i2c_msg *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358{
359 int inval;
360 int rdcount=0; /* counts bytes read */
361 struct i2c_algo_bit_data *adap = i2c_adap->algo_data;
362 char *temp = msg->buf;
363 int count = msg->len;
364
365 while (count > 0) {
366 inval = i2c_inb(i2c_adap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 if (inval>=0) {
368 *temp = inval;
369 rdcount++;
370 } else { /* read timed out */
371 printk(KERN_ERR "i2c-algo-bit.o: readbytes: i2c_inb timed out.\n");
372 break;
373 }
374
375 temp++;
376 count--;
377
378 if (msg->flags & I2C_M_NO_RD_ACK)
379 continue;
380
Jean Delvare424ed672007-05-01 23:26:33 +0200381 /* assert: sda is high */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 if ( count > 0 ) { /* send ack */
Jean Delvare424ed672007-05-01 23:26:33 +0200383 setsda(adap, 0);
384 udelay((adap->udelay + 1) / 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 DEBPROTO(printk(" Am "));
386 } else {
Jean Delvare424ed672007-05-01 23:26:33 +0200387 /* neg. ack on last byte */
388 udelay((adap->udelay + 1) / 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 DEBPROTO(printk(" NAm "));
390 }
391 if (sclhi(adap)<0) { /* timeout */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 printk(KERN_ERR "i2c-algo-bit.o: readbytes: Timeout at ack\n");
393 return -ETIMEDOUT;
394 };
395 scllo(adap);
Jean Delvare3c4bb242007-05-01 23:26:29 +0200396
397 /* Some SMBus transactions require that we receive the
398 transaction length as the first read byte. */
399 if (rdcount == 1 && (msg->flags & I2C_M_RECV_LEN)) {
400 if (inval <= 0 || inval > I2C_SMBUS_BLOCK_MAX) {
401 printk(KERN_ERR "i2c-algo-bit: readbytes: "
402 "invalid block length (%d)\n", inval);
403 return -EREMOTEIO;
404 }
405 /* The original count value accounts for the extra
406 bytes, that is, either 1 for a regular transaction,
407 or 2 for a PEC transaction. */
408 count += inval;
409 msg->len += inval;
410 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 }
412 return rdcount;
413}
414
415/* doAddress initiates the transfer by generating the start condition (in
416 * try_address) and transmits the address in the necessary format to handle
417 * reads, writes as well as 10bit-addresses.
418 * returns:
419 * 0 everything went okay, the chip ack'ed, or IGNORE_NAK flag was set
420 * -x an error occurred (like: -EREMOTEIO if the device did not answer, or
421 * -ETIMEDOUT, for example if the lines are stuck...)
422 */
Jean Delvare7b288a02006-09-03 22:22:12 +0200423static int bit_doAddress(struct i2c_adapter *i2c_adap, struct i2c_msg *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424{
425 unsigned short flags = msg->flags;
426 unsigned short nak_ok = msg->flags & I2C_M_IGNORE_NAK;
427 struct i2c_algo_bit_data *adap = i2c_adap->algo_data;
428
429 unsigned char addr;
430 int ret, retries;
431
432 retries = nak_ok ? 0 : i2c_adap->retries;
433
434 if ( (flags & I2C_M_TEN) ) {
435 /* a ten bit address */
436 addr = 0xf0 | (( msg->addr >> 7) & 0x03);
437 DEB2(printk(KERN_DEBUG "addr0: %d\n",addr));
438 /* try extended address code...*/
439 ret = try_address(i2c_adap, addr, retries);
440 if ((ret != 1) && !nak_ok) {
441 printk(KERN_ERR "died at extended address code.\n");
442 return -EREMOTEIO;
443 }
444 /* the remaining 8 bit address */
445 ret = i2c_outb(i2c_adap,msg->addr & 0x7f);
446 if ((ret != 1) && !nak_ok) {
447 /* the chip did not ack / xmission error occurred */
448 printk(KERN_ERR "died at 2nd address code.\n");
449 return -EREMOTEIO;
450 }
451 if ( flags & I2C_M_RD ) {
452 i2c_repstart(adap);
453 /* okay, now switch into reading mode */
454 addr |= 0x01;
455 ret = try_address(i2c_adap, addr, retries);
456 if ((ret!=1) && !nak_ok) {
457 printk(KERN_ERR "died at extended address code.\n");
458 return -EREMOTEIO;
459 }
460 }
461 } else { /* normal 7bit address */
462 addr = ( msg->addr << 1 );
463 if (flags & I2C_M_RD )
464 addr |= 1;
465 if (flags & I2C_M_REV_DIR_ADDR )
466 addr ^= 1;
467 ret = try_address(i2c_adap, addr, retries);
468 if ((ret!=1) && !nak_ok)
469 return -EREMOTEIO;
470 }
471
472 return 0;
473}
474
475static int bit_xfer(struct i2c_adapter *i2c_adap,
476 struct i2c_msg msgs[], int num)
477{
478 struct i2c_msg *pmsg;
479 struct i2c_algo_bit_data *adap = i2c_adap->algo_data;
480
481 int i,ret;
482 unsigned short nak_ok;
483
484 i2c_start(adap);
485 for (i=0;i<num;i++) {
486 pmsg = &msgs[i];
487 nak_ok = pmsg->flags & I2C_M_IGNORE_NAK;
488 if (!(pmsg->flags & I2C_M_NOSTART)) {
489 if (i) {
490 i2c_repstart(adap);
491 }
492 ret = bit_doAddress(i2c_adap, pmsg);
493 if ((ret != 0) && !nak_ok) {
494 DEB2(printk(KERN_DEBUG "i2c-algo-bit.o: NAK from device addr %2.2x msg #%d\n"
495 ,msgs[i].addr,i));
Jean Delvare1ecac072007-05-01 23:26:28 +0200496 goto bailout;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 }
498 }
499 if (pmsg->flags & I2C_M_RD ) {
500 /* read bytes into buffer*/
501 ret = readbytes(i2c_adap, pmsg);
502 DEB2(printk(KERN_DEBUG "i2c-algo-bit.o: read %d bytes.\n",ret));
Jean Delvare1ecac072007-05-01 23:26:28 +0200503 if (ret < pmsg->len) {
504 if (ret >= 0)
505 ret = -EREMOTEIO;
506 goto bailout;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 }
508 } else {
509 /* write bytes from buffer */
510 ret = sendbytes(i2c_adap, pmsg);
511 DEB2(printk(KERN_DEBUG "i2c-algo-bit.o: wrote %d bytes.\n",ret));
Jean Delvare1ecac072007-05-01 23:26:28 +0200512 if (ret < pmsg->len) {
513 if (ret >= 0)
514 ret = -EREMOTEIO;
515 goto bailout;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 }
517 }
518 }
Jean Delvare1ecac072007-05-01 23:26:28 +0200519 ret = i;
520
521bailout:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 i2c_stop(adap);
Jean Delvare1ecac072007-05-01 23:26:28 +0200523 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524}
525
526static u32 bit_func(struct i2c_adapter *adap)
527{
528 return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL |
Jean Delvare3c4bb242007-05-01 23:26:29 +0200529 I2C_FUNC_SMBUS_READ_BLOCK_DATA |
530 I2C_FUNC_SMBUS_BLOCK_PROC_CALL |
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 I2C_FUNC_10BIT_ADDR | I2C_FUNC_PROTOCOL_MANGLING;
532}
533
534
535/* -----exported algorithm data: ------------------------------------- */
536
Jean Delvare9e11a9f2006-09-03 22:38:52 +0200537static const struct i2c_algorithm i2c_bit_algo = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 .master_xfer = bit_xfer,
539 .functionality = bit_func,
540};
541
542/*
543 * registering functions to load algorithms at runtime
544 */
Jean Delvare0f3b4832007-05-01 23:26:31 +0200545static int i2c_bit_prepare_bus(struct i2c_adapter *adap)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546{
547 struct i2c_algo_bit_data *bit_adap = adap->algo_data;
548
549 if (bit_test) {
550 int ret = test_bus(bit_adap, adap->name);
551 if (ret<0)
552 return -ENODEV;
553 }
554
555 DEB2(dev_dbg(&adap->dev, "hw routines registered.\n"));
556
557 /* register new adapter to i2c module... */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 adap->algo = &i2c_bit_algo;
559
560 adap->timeout = 100; /* default values, should */
561 adap->retries = 3; /* be replaced by defines */
562
Jean Delvare0f3b4832007-05-01 23:26:31 +0200563 return 0;
564}
565
566int i2c_bit_add_bus(struct i2c_adapter *adap)
567{
568 int err;
569
570 err = i2c_bit_prepare_bus(adap);
571 if (err)
572 return err;
573
Mark M. Hoffmanb39ad0c2006-07-01 17:16:06 +0200574 return i2c_add_adapter(adap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576EXPORT_SYMBOL(i2c_bit_add_bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577
Jean Delvare0f3b4832007-05-01 23:26:31 +0200578int i2c_bit_add_numbered_bus(struct i2c_adapter *adap)
579{
580 int err;
581
582 err = i2c_bit_prepare_bus(adap);
583 if (err)
584 return err;
585
586 return i2c_add_numbered_adapter(adap);
587}
588EXPORT_SYMBOL(i2c_bit_add_numbered_bus);
589
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590MODULE_AUTHOR("Simon G. Vogl <simon@tk.uni-linz.ac.at>");
591MODULE_DESCRIPTION("I2C-Bus bit-banging algorithm");
592MODULE_LICENSE("GPL");
593
594module_param(bit_test, bool, 0);
595module_param(i2c_debug, int, S_IRUGO | S_IWUSR);
596
597MODULE_PARM_DESC(bit_test, "Test the lines of the bus to see if it is stuck");
598MODULE_PARM_DESC(i2c_debug,
599 "debug level - 0 off; 1 normal; 2,3 more verbose; 9 bit-protocol");