cg14: use standard fields for framebuffer physical address and length
[safe/jmp/linux-2.6] / drivers / i2c / algos / i2c-algo-pca.c
index a8e51bd..78d42aa 100644 (file)
@@ -22,6 +22,7 @@
 #include <linux/module.h>
 #include <linux/moduleparam.h>
 #include <linux/delay.h>
+#include <linux/jiffies.h>
 #include <linux/init.h>
 #include <linux/errno.h>
 #include <linux/i2c.h>
@@ -59,14 +60,14 @@ static void pca9665_reset(void *pd)
  *
  * returns after the start condition has occurred
  */
-static void pca_start(struct i2c_algo_pca_data *adap)
+static int pca_start(struct i2c_algo_pca_data *adap)
 {
        int sta = pca_get_con(adap);
        DEB2("=== START\n");
        sta |= I2C_PCA_CON_STA;
        sta &= ~(I2C_PCA_CON_STO|I2C_PCA_CON_SI);
        pca_set_con(adap, sta);
-       pca_wait(adap);
+       return pca_wait(adap);
 }
 
 /*
@@ -74,14 +75,14 @@ static void pca_start(struct i2c_algo_pca_data *adap)
  *
  * return after the repeated start condition has occurred
  */
-static void pca_repeated_start(struct i2c_algo_pca_data *adap)
+static int pca_repeated_start(struct i2c_algo_pca_data *adap)
 {
        int sta = pca_get_con(adap);
        DEB2("=== REPEATED START\n");
        sta |= I2C_PCA_CON_STA;
        sta &= ~(I2C_PCA_CON_STO|I2C_PCA_CON_SI);
        pca_set_con(adap, sta);
-       pca_wait(adap);
+       return pca_wait(adap);
 }
 
 /*
@@ -107,7 +108,7 @@ static void pca_stop(struct i2c_algo_pca_data *adap)
  *
  * returns after the address has been sent
  */
-static void pca_address(struct i2c_algo_pca_data *adap,
+static int pca_address(struct i2c_algo_pca_data *adap,
                        struct i2c_msg *msg)
 {
        int sta = pca_get_con(adap);
@@ -124,7 +125,7 @@ static void pca_address(struct i2c_algo_pca_data *adap,
        sta &= ~(I2C_PCA_CON_STO|I2C_PCA_CON_STA|I2C_PCA_CON_SI);
        pca_set_con(adap, sta);
 
-       pca_wait(adap);
+       return pca_wait(adap);
 }
 
 /*
@@ -132,7 +133,7 @@ static void pca_address(struct i2c_algo_pca_data *adap,
  *
  * Returns after the byte has been transmitted
  */
-static void pca_tx_byte(struct i2c_algo_pca_data *adap,
+static int pca_tx_byte(struct i2c_algo_pca_data *adap,
                        __u8 b)
 {
        int sta = pca_get_con(adap);
@@ -142,7 +143,7 @@ static void pca_tx_byte(struct i2c_algo_pca_data *adap,
        sta &= ~(I2C_PCA_CON_STO|I2C_PCA_CON_STA|I2C_PCA_CON_SI);
        pca_set_con(adap, sta);
 
-       pca_wait(adap);
+       return pca_wait(adap);
 }
 
 /*
@@ -162,7 +163,7 @@ static void pca_rx_byte(struct i2c_algo_pca_data *adap,
  *
  * Returns after next byte has arrived.
  */
-static void pca_rx_ack(struct i2c_algo_pca_data *adap,
+static int pca_rx_ack(struct i2c_algo_pca_data *adap,
                       int ack)
 {
        int sta = pca_get_con(adap);
@@ -173,7 +174,7 @@ static void pca_rx_ack(struct i2c_algo_pca_data *adap,
                sta |= I2C_PCA_CON_AA;
 
        pca_set_con(adap, sta);
-       pca_wait(adap);
+       return pca_wait(adap);
 }
 
 static int pca_xfer(struct i2c_adapter *i2c_adap,
@@ -186,14 +187,17 @@ static int pca_xfer(struct i2c_adapter *i2c_adap,
        int numbytes = 0;
        int state;
        int ret;
-       int timeout = i2c_adap->timeout;
+       int completed = 1;
+       unsigned long timeout = jiffies + i2c_adap->timeout;
 
-       while ((state = pca_status(adap)) != 0xf8 && timeout--) {
-               msleep(10);
-       }
-       if (state != 0xf8) {
-               dev_dbg(&i2c_adap->dev, "bus is not idle. status is %#04x\n", state);
-               return -EAGAIN;
+       while ((state = pca_status(adap)) != 0xf8) {
+               if (time_before(jiffies, timeout)) {
+                       msleep(10);
+               } else {
+                       dev_dbg(&i2c_adap->dev, "bus is not idle. status is "
+                               "%#04x\n", state);
+                       return -EAGAIN;
+               }
        }
 
        DEB1("{{{ XFER %d messages\n", num);
@@ -229,18 +233,19 @@ static int pca_xfer(struct i2c_adapter *i2c_adap,
 
                switch (state) {
                case 0xf8: /* On reset or stop the bus is idle */
-                       pca_start(adap);
+                       completed = pca_start(adap);
                        break;
 
                case 0x08: /* A START condition has been transmitted */
                case 0x10: /* A repeated start condition has been transmitted */
-                       pca_address(adap, msg);
+                       completed = pca_address(adap, msg);
                        break;
 
                case 0x18: /* SLA+W has been transmitted; ACK has been received */
                case 0x28: /* Data byte in I2CDAT has been transmitted; ACK has been received */
                        if (numbytes < msg->len) {
-                               pca_tx_byte(adap, msg->buf[numbytes]);
+                               completed = pca_tx_byte(adap,
+                                                       msg->buf[numbytes]);
                                numbytes++;
                                break;
                        }
@@ -248,7 +253,7 @@ static int pca_xfer(struct i2c_adapter *i2c_adap,
                        if (curmsg == num)
                                pca_stop(adap);
                        else
-                               pca_repeated_start(adap);
+                               completed = pca_repeated_start(adap);
                        break;
 
                case 0x20: /* SLA+W has been transmitted; NOT ACK has been received */
@@ -257,21 +262,22 @@ static int pca_xfer(struct i2c_adapter *i2c_adap,
                        goto out;
 
                case 0x40: /* SLA+R has been transmitted; ACK has been received */
-                       pca_rx_ack(adap, msg->len > 1);
+                       completed = pca_rx_ack(adap, msg->len > 1);
                        break;
 
                case 0x50: /* Data bytes has been received; ACK has been returned */
                        if (numbytes < msg->len) {
                                pca_rx_byte(adap, &msg->buf[numbytes], 1);
                                numbytes++;
-                               pca_rx_ack(adap, numbytes < msg->len - 1);
+                               completed = pca_rx_ack(adap,
+                                                      numbytes < msg->len - 1);
                                break;
                        }
                        curmsg++; numbytes = 0;
                        if (curmsg == num)
                                pca_stop(adap);
                        else
-                               pca_repeated_start(adap);
+                               completed = pca_repeated_start(adap);
                        break;
 
                case 0x48: /* SLA+R has been transmitted; NOT ACK has been received */
@@ -281,10 +287,21 @@ static int pca_xfer(struct i2c_adapter *i2c_adap,
 
                case 0x30: /* Data byte in I2CDAT has been transmitted; NOT ACK has been received */
                        DEB2("NOT ACK received after data byte\n");
+                       pca_stop(adap);
                        goto out;
 
                case 0x38: /* Arbitration lost during SLA+W, SLA+R or data bytes */
                        DEB2("Arbitration lost\n");
+                       /*
+                        * The PCA9564 data sheet (2006-09-01) says "A
+                        * START condition will be transmitted when the
+                        * bus becomes free (STOP or SCL and SDA high)"
+                        * when the STA bit is set (p. 11).
+                        *
+                        * In case this won't work, try pca_reset()
+                        * instead.
+                        */
+                       pca_start(adap);
                        goto out;
 
                case 0x58: /* Data byte has been received; NOT ACK has been returned */
@@ -294,7 +311,7 @@ static int pca_xfer(struct i2c_adapter *i2c_adap,
                                if (curmsg == num)
                                        pca_stop(adap);
                                else
-                                       pca_repeated_start(adap);
+                                       completed = pca_repeated_start(adap);
                        } else {
                                DEB2("NOT ACK sent after data byte received. "
                                     "Not final byte. numbytes %d. len %d\n",
@@ -320,6 +337,8 @@ static int pca_xfer(struct i2c_adapter *i2c_adap,
                        break;
                }
 
+               if (!completed)
+                       goto out;
        }
 
        ret = curmsg;