i2c: Share the I2C device presence detection code
[pandora-kernel.git] / drivers / i2c / i2c-core.c
index 7c469a6..f744f73 100644 (file)
@@ -418,6 +418,9 @@ i2c_new_device(struct i2c_adapter *adap, struct i2c_board_info const *info)
        client->dev.parent = &client->adapter->dev;
        client->dev.bus = &i2c_bus_type;
        client->dev.type = &i2c_client_type;
+#ifdef CONFIG_OF
+       client->dev.of_node = info->of_node;
+#endif
 
        dev_set_name(&client->dev, "%d-%04x", i2c_adapter_id(adap),
                     client->addr);
@@ -1221,10 +1224,10 @@ EXPORT_SYMBOL(i2c_transfer);
  *
  * Returns negative errno, or else the number of bytes written.
  */
-int i2c_master_send(struct i2c_client *client,const char *buf ,int count)
+int i2c_master_send(struct i2c_client *client, const char *buf, int count)
 {
        int ret;
-       struct i2c_adapter *adap=client->adapter;
+       struct i2c_adapter *adap = client->adapter;
        struct i2c_msg msg;
 
        msg.addr = client->addr;
@@ -1248,9 +1251,9 @@ EXPORT_SYMBOL(i2c_master_send);
  *
  * Returns negative errno, or else the number of bytes read.
  */
-int i2c_master_recv(struct i2c_client *client, char *buf ,int count)
+int i2c_master_recv(struct i2c_client *client, char *bufint count)
 {
-       struct i2c_adapter *adap=client->adapter;
+       struct i2c_adapter *adap = client->adapter;
        struct i2c_msg msg;
        int ret;
 
@@ -1274,6 +1277,41 @@ EXPORT_SYMBOL(i2c_master_recv);
  * ----------------------------------------------------
  */
 
+/*
+ * Legacy default probe function, mostly relevant for SMBus. The default
+ * probe method is a quick write, but it is known to corrupt the 24RF08
+ * EEPROMs due to a state machine bug, and could also irreversibly
+ * write-protect some EEPROMs, so for address ranges 0x30-0x37 and 0x50-0x5f,
+ * we use a short byte read instead. Also, some bus drivers don't implement
+ * quick write, so we fallback to a byte read in that case too.
+ * On x86, there is another special case for FSC hardware monitoring chips,
+ * which want regular byte reads (address 0x73.) Fortunately, these are the
+ * only known chips using this I2C address on PC hardware.
+ * Returns 1 if probe succeeded, 0 if not.
+ */
+static int i2c_default_probe(struct i2c_adapter *adap, unsigned short addr)
+{
+       int err;
+       union i2c_smbus_data dummy;
+
+#ifdef CONFIG_X86
+       if (addr == 0x73 && (adap->class & I2C_CLASS_HWMON)
+        && i2c_check_functionality(adap, I2C_FUNC_SMBUS_READ_BYTE_DATA))
+               err = i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_READ, 0,
+                                    I2C_SMBUS_BYTE_DATA, &dummy);
+       else
+#endif
+       if ((addr & ~0x07) == 0x30 || (addr & ~0x0f) == 0x50
+        || !i2c_check_functionality(adap, I2C_FUNC_SMBUS_QUICK))
+               err = i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_READ, 0,
+                                    I2C_SMBUS_BYTE, &dummy);
+       else
+               err = i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_WRITE, 0,
+                                    I2C_SMBUS_QUICK, NULL);
+
+       return err >= 0;
+}
+
 static int i2c_detect_address(struct i2c_client *temp_client,
                              struct i2c_driver *driver)
 {
@@ -1294,23 +1332,8 @@ static int i2c_detect_address(struct i2c_client *temp_client,
                return 0;
 
        /* Make sure there is something at this address */
-       if (addr == 0x73 && (adapter->class & I2C_CLASS_HWMON)) {
-               /* Special probe for FSC hwmon chips */
-               union i2c_smbus_data dummy;
-
-               if (i2c_smbus_xfer(adapter, addr, 0, I2C_SMBUS_READ, 0,
-                                  I2C_SMBUS_BYTE_DATA, &dummy) < 0)
-                       return 0;
-       } else {
-               if (i2c_smbus_xfer(adapter, addr, 0, I2C_SMBUS_WRITE, 0,
-                                  I2C_SMBUS_QUICK, NULL) < 0)
-                       return 0;
-
-               /* Prevent 24RF08 corruption */
-               if ((addr & ~0x0f) == 0x50)
-                       i2c_smbus_xfer(adapter, addr, 0, I2C_SMBUS_WRITE, 0,
-                                      I2C_SMBUS_QUICK, NULL);
-       }
+       if (!i2c_default_probe(adapter, addr))
+               return 0;
 
        /* Finally call the custom detection function */
        memset(&info, 0, sizeof(struct i2c_board_info));
@@ -1417,29 +1440,9 @@ i2c_new_probed_device(struct i2c_adapter *adap,
                        continue;
                }
 
-               /* Test address responsiveness
-                  The default probe method is a quick write, but it is known
-                  to corrupt the 24RF08 EEPROMs due to a state machine bug,
-                  and could also irreversibly write-protect some EEPROMs, so
-                  for address ranges 0x30-0x37 and 0x50-0x5f, we use a byte
-                  read instead. Also, some bus drivers don't implement
-                  quick write, so we fallback to a byte read it that case
-                  too. */
-               if ((addr_list[i] & ~0x07) == 0x30
-                || (addr_list[i] & ~0x0f) == 0x50
-                || !i2c_check_functionality(adap, I2C_FUNC_SMBUS_QUICK)) {
-                       union i2c_smbus_data data;
-
-                       if (i2c_smbus_xfer(adap, addr_list[i], 0,
-                                          I2C_SMBUS_READ, 0,
-                                          I2C_SMBUS_BYTE, &data) >= 0)
-                               break;
-               } else {
-                       if (i2c_smbus_xfer(adap, addr_list[i], 0,
-                                          I2C_SMBUS_WRITE, 0,
-                                          I2C_SMBUS_QUICK, NULL) >= 0)
-                               break;
-               }
+               /* Test address responsiveness */
+               if (i2c_default_probe(adap, addr_list[i]))
+                       break;
        }
 
        if (addr_list[i] == I2C_CLIENT_END) {
@@ -1452,7 +1455,7 @@ i2c_new_probed_device(struct i2c_adapter *adap,
 }
 EXPORT_SYMBOL_GPL(i2c_new_probed_device);
 
-struct i2c_adapteri2c_get_adapter(int id)
+struct i2c_adapter *i2c_get_adapter(int id)
 {
        struct i2c_adapter *adapter;
 
@@ -1479,7 +1482,7 @@ static u8 crc8(u16 data)
 {
        int i;
 
-       for(i = 0; i < 8; i++) {
+       for (i = 0; i < 8; i++) {
                if (data & 0x8000)
                        data = data ^ POLY;
                data = data << 1;
@@ -1492,7 +1495,7 @@ static u8 i2c_smbus_pec(u8 crc, u8 *p, size_t count)
 {
        int i;
 
-       for(i = 0; i < count; i++)
+       for (i = 0; i < count; i++)
                crc = crc8((crc ^ p[i]) << 8);
        return crc;
 }
@@ -1562,7 +1565,7 @@ EXPORT_SYMBOL(i2c_smbus_read_byte);
  */
 s32 i2c_smbus_write_byte(struct i2c_client *client, u8 value)
 {
-       return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
+       return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
                              I2C_SMBUS_WRITE, value, I2C_SMBUS_BYTE, NULL);
 }
 EXPORT_SYMBOL(i2c_smbus_write_byte);
@@ -1600,9 +1603,9 @@ s32 i2c_smbus_write_byte_data(struct i2c_client *client, u8 command, u8 value)
 {
        union i2c_smbus_data data;
        data.byte = value;
-       return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
-                             I2C_SMBUS_WRITE,command,
-                             I2C_SMBUS_BYTE_DATA,&data);
+       return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
+                             I2C_SMBUS_WRITE, command,
+                             I2C_SMBUS_BYTE_DATA, &data);
 }
 EXPORT_SYMBOL(i2c_smbus_write_byte_data);
 
@@ -1639,9 +1642,9 @@ s32 i2c_smbus_write_word_data(struct i2c_client *client, u8 command, u16 value)
 {
        union i2c_smbus_data data;
        data.word = value;
-       return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
-                             I2C_SMBUS_WRITE,command,
-                             I2C_SMBUS_WORD_DATA,&data);
+       return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
+                             I2C_SMBUS_WRITE, command,
+                             I2C_SMBUS_WORD_DATA, &data);
 }
 EXPORT_SYMBOL(i2c_smbus_write_word_data);
 
@@ -1718,9 +1721,9 @@ s32 i2c_smbus_write_block_data(struct i2c_client *client, u8 command,
                length = I2C_SMBUS_BLOCK_MAX;
        data.block[0] = length;
        memcpy(&data.block[1], values, length);
-       return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
-                             I2C_SMBUS_WRITE,command,
-                             I2C_SMBUS_BLOCK_DATA,&data);
+       return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
+                             I2C_SMBUS_WRITE, command,
+                             I2C_SMBUS_BLOCK_DATA, &data);
 }
 EXPORT_SYMBOL(i2c_smbus_write_block_data);
 
@@ -1762,10 +1765,10 @@ EXPORT_SYMBOL(i2c_smbus_write_i2c_block_data);
 
 /* Simulate a SMBus command using the i2c protocol
    No checking of parameters is done!  */
-static s32 i2c_smbus_xfer_emulated(struct i2c_adapter * adapter, u16 addr,
-                                   unsigned short flags,
-                                   char read_write, u8 command, int size,
-                                   union i2c_smbus_data * data)
+static s32 i2c_smbus_xfer_emulated(struct i2c_adapter *adapter, u16 addr,
+                                  unsigned short flags,
+                                  char read_write, u8 command, int size,
+                                  union i2c_smbus_data *data)
 {
        /* So we need to generate a series of msgs. In the case of writing, we
          need to use only one message; when reading, we need two. We initialize
@@ -1773,7 +1776,7 @@ static s32 i2c_smbus_xfer_emulated(struct i2c_adapter * adapter, u16 addr,
          simpler. */
        unsigned char msgbuf0[I2C_SMBUS_BLOCK_MAX+3];
        unsigned char msgbuf1[I2C_SMBUS_BLOCK_MAX+2];
-       int num = read_write == I2C_SMBUS_READ?2:1;
+       int num = read_write == I2C_SMBUS_READ ? 2 : 1;
        struct i2c_msg msg[2] = { { addr, flags, 1, msgbuf0 },
                                  { addr, flags | I2C_M_RD, 0, msgbuf1 }
                                };
@@ -1782,7 +1785,7 @@ static s32 i2c_smbus_xfer_emulated(struct i2c_adapter * adapter, u16 addr,
        int status;
 
        msgbuf0[0] = command;
-       switch(size) {
+       switch (size) {
        case I2C_SMBUS_QUICK:
                msg[0].len = 0;
                /* Special case: The read/write field is used as data */
@@ -1809,7 +1812,7 @@ static s32 i2c_smbus_xfer_emulated(struct i2c_adapter * adapter, u16 addr,
                if (read_write == I2C_SMBUS_READ)
                        msg[1].len = 2;
                else {
-                       msg[0].len=3;
+                       msg[0].len = 3;
                        msgbuf0[1] = data->word & 0xff;
                        msgbuf0[2] = data->word >> 8;
                }
@@ -1902,26 +1905,26 @@ static s32 i2c_smbus_xfer_emulated(struct i2c_adapter * adapter, u16 addr,
        }
 
        if (read_write == I2C_SMBUS_READ)
-               switch(size) {
-                       case I2C_SMBUS_BYTE:
-                               data->byte = msgbuf0[0];
-                               break;
-                       case I2C_SMBUS_BYTE_DATA:
-                               data->byte = msgbuf1[0];
-                               break;
-                       case I2C_SMBUS_WORD_DATA:
-                       case I2C_SMBUS_PROC_CALL:
-                               data->word = msgbuf1[0] | (msgbuf1[1] << 8);
-                               break;
-                       case I2C_SMBUS_I2C_BLOCK_DATA:
-                               for (i = 0; i < data->block[0]; i++)
-                                       data->block[i+1] = msgbuf1[i];
-                               break;
-                       case I2C_SMBUS_BLOCK_DATA:
-                       case I2C_SMBUS_BLOCK_PROC_CALL:
-                               for (i = 0; i < msgbuf1[0] + 1; i++)
-                                       data->block[i] = msgbuf1[i];
-                               break;
+               switch (size) {
+               case I2C_SMBUS_BYTE:
+                       data->byte = msgbuf0[0];
+                       break;
+               case I2C_SMBUS_BYTE_DATA:
+                       data->byte = msgbuf1[0];
+                       break;
+               case I2C_SMBUS_WORD_DATA:
+               case I2C_SMBUS_PROC_CALL:
+                       data->word = msgbuf1[0] | (msgbuf1[1] << 8);
+                       break;
+               case I2C_SMBUS_I2C_BLOCK_DATA:
+                       for (i = 0; i < data->block[0]; i++)
+                               data->block[i+1] = msgbuf1[i];
+                       break;
+               case I2C_SMBUS_BLOCK_DATA:
+               case I2C_SMBUS_BLOCK_PROC_CALL:
+                       for (i = 0; i < msgbuf1[0] + 1; i++)
+                               data->block[i] = msgbuf1[i];
+                       break;
                }
        return 0;
 }
@@ -1966,7 +1969,7 @@ s32 i2c_smbus_xfer(struct i2c_adapter *adapter, u16 addr, unsigned short flags,
                }
                rt_mutex_unlock(&adapter->bus_lock);
        } else
-               res = i2c_smbus_xfer_emulated(adapter,addr,flags,read_write,
+               res = i2c_smbus_xfer_emulated(adapter, addr, flags, read_write,
                                              command, protocol, data);
 
        return res;