Subject: i2c 10-bit addressing support
To: None <tech-kern@NetBSD.org>
From: Garrett D'Amore <garrett_damore@tadpole.com>
List: tech-kern
Date: 07/13/2006 15:42:04
A while ago I posted this diff.  Posting again.  If nobody responds,
then I will just drop it on the floor, and it will never get committed.

This adds full addressing (10-bit) support to the i2c bitbang support in
NetBSD.  I don't have any 10-bit devices, so I cannot test, but it
doesn't seem to break 7-bit addressing at least:


Index: dev/i2c/i2c_bitbang.c
===================================================================
RCS file: /cvsroot/src/sys/dev/i2c/i2c_bitbang.c,v
retrieving revision 1.3
diff -u -r1.3 i2c_bitbang.c
--- dev/i2c/i2c_bitbang.c       5 Mar 2006 17:33:33 -0000       1.3
+++ dev/i2c/i2c_bitbang.c       13 Jul 2006 22:39:49 -0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: i2c_bitbang.c,v 1.3 2006/03/05 17:33:33 christos Exp $ */
+/*     $NetBSD: i2c_bitbang.c,v 1.3.8.1 2006/07/13 17:49:22 gdamore Exp
$     *
/
 
 /*
  * Copyright (c) 2003 Wasabi Systems, Inc.
@@ -88,16 +88,39 @@
 i2c_bitbang_initiate_xfer(void *v, i2c_addr_t addr, int flags,
     i2c_bitbang_ops_t ops)
 {
-       int i2caddr;
 
-       /* XXX Only support 7-bit addressing for now. */
-       if ((addr & 0x78) == 0x78)
-               return (EINVAL);
+       if (addr < 0x80) {
+               uint8_t i2caddr;
 
-       i2caddr = (addr << 1) | ((flags & I2C_F_READ) ? 1 : 0);
+               /* disallow the 10-bit address prefix */
+               if ((addr & 0x78) == 0x78)
+                       return EINVAL;
+               i2caddr = (addr << 1) | ((flags & I2C_F_READ) ? 1 : 0);
+               (void) i2c_bitbang_send_start(v, flags, ops);
+
+               return (i2c_bitbang_write_byte(v, i2caddr,
+                           flags & ~I2C_F_STOP, ops));
+
+       } else if (addr < 0x400) {
+               uint16_t        i2caddr;
+               int             rv;
+
+               i2caddr = (addr << 1) | ((flags & I2C_F_READ) ? 1 : 0) |
+                   0xf000;
+
+               (void) i2c_bitbang_send_start(v, flags, ops);
+               rv = i2c_bitbang_write_byte(v, i2caddr >> 8,
+                   flags & ~I2C_F_STOP, ops);
+               /* did a slave ack the 10-bit prefix? */
+               if (rv != 0)
+                       return rv;
+
+               /* send the lower 7-bits (+ read/write mode) */
+               return (i2c_bitbang_write_byte(v, i2caddr & 0xff,
+                           flags & ~I2C_F_STOP, ops));
 
-       (void) i2c_bitbang_send_start(v, flags, ops);
-       return (i2c_bitbang_write_byte(v, i2caddr, flags & ~I2C_F_STOP,
ops));
+       } else
+               return EINVAL;
 }
 
 int

-- 
Garrett D'Amore, Principal Software Engineer
Tadpole Computer / Computing Technologies Division,
General Dynamics C4 Systems
http://www.tadpolecomputer.com/
Phone: 951 325-2134  Fax: 951 325-2191