i2c-s3c2410: Remove unconditional 1ms delay on each transfer
[safe/jmp/linux-2.6] / drivers / i2c / busses / scx200_acb.c
index 09b4586..684395b 100644 (file)
 #include <linux/kernel.h>
 #include <linux/init.h>
 #include <linux/i2c.h>
-#include <linux/smp_lock.h>
 #include <linux/pci.h>
 #include <linux/delay.h>
 #include <linux/mutex.h>
+#include <linux/slab.h>
 #include <asm/io.h>
 
 #include <linux/scx200.h>
@@ -184,21 +184,21 @@ static void scx200_acb_machine(struct scx200_acb_iface *iface, u8 status)
                break;
 
        case state_read:
-               /* Set ACK if receiving the last byte */
-               if (iface->len == 1)
+               /* Set ACK if _next_ byte will be the last one */
+               if (iface->len == 2)
                        outb(inb(ACBCTL1) | ACBCTL1_ACK, ACBCTL1);
                else
                        outb(inb(ACBCTL1) & ~ACBCTL1_ACK, ACBCTL1);
 
-               *iface->ptr++ = inb(ACBSDA);
-               --iface->len;
-
-               if (iface->len == 0) {
+               if (iface->len == 1) {
                        iface->result = 0;
                        iface->state = state_idle;
                        outb(inb(ACBCTL1) | ACBCTL1_STOP, ACBCTL1);
                }
 
+               *iface->ptr++ = inb(ACBSDA);
+               --iface->len;
+
                break;
 
        case state_write:
@@ -218,8 +218,10 @@ static void scx200_acb_machine(struct scx200_acb_iface *iface, u8 status)
        return;
 
  error:
-       dev_err(&iface->adapter.dev, "%s in state %s\n", errmsg,
-               scx200_acb_state_name[iface->state]);
+       dev_err(&iface->adapter.dev,
+               "%s in state %s (addr=0x%02x, len=%d, status=0x%02x)\n", errmsg,
+               scx200_acb_state_name[iface->state], iface->address_byte,
+               iface->len, status);
 
        iface->state = state_idle;
        iface->result = -EIO;
@@ -232,7 +234,7 @@ static void scx200_acb_poll(struct scx200_acb_iface *iface)
        unsigned long timeout;
 
        timeout = jiffies + POLL_TIMEOUT;
-       while (time_before(jiffies, timeout)) {
+       while (1) {
                status = inb(ACBST);
 
                /* Reset the status register to avoid the hang */
@@ -242,7 +244,10 @@ static void scx200_acb_poll(struct scx200_acb_iface *iface)
                        scx200_acb_machine(iface, status);
                        return;
                }
-               yield();
+               if (time_after(jiffies, timeout))
+                       break;
+               cpu_relax();
+               cond_resched();
        }
 
        dev_err(&iface->adapter.dev, "timeout in state %s\n",
@@ -307,8 +312,10 @@ static s32 scx200_acb_smbus_xfer(struct i2c_adapter *adapter,
                buffer = (u8 *)&cur_word;
                break;
 
-       case I2C_SMBUS_BLOCK_DATA:
+       case I2C_SMBUS_I2C_BLOCK_DATA:
                len = data->block[0];
+               if (len == 0 || len > I2C_SMBUS_BLOCK_MAX)
+                       return -EINVAL;
                buffer = &data->block[1];
                break;
 
@@ -372,19 +379,19 @@ static u32 scx200_acb_func(struct i2c_adapter *adapter)
 {
        return I2C_FUNC_SMBUS_QUICK | I2C_FUNC_SMBUS_BYTE |
               I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_WORD_DATA |
-              I2C_FUNC_SMBUS_BLOCK_DATA;
+              I2C_FUNC_SMBUS_I2C_BLOCK;
 }
 
 /* For now, we only handle combined mode (smbus) */
-static struct i2c_algorithm scx200_acb_algorithm = {
+static const struct i2c_algorithm scx200_acb_algorithm = {
        .smbus_xfer     = scx200_acb_smbus_xfer,
        .functionality  = scx200_acb_func,
 };
 
 static struct scx200_acb_iface *scx200_acb_list;
-static DECLARE_MUTEX(scx200_acb_list_mutex);
+static DEFINE_MUTEX(scx200_acb_list_mutex);
 
-static int scx200_acb_probe(struct scx200_acb_iface *iface)
+static __init int scx200_acb_probe(struct scx200_acb_iface *iface)
 {
        u8 val;
 
@@ -421,7 +428,7 @@ static int scx200_acb_probe(struct scx200_acb_iface *iface)
 }
 
 static __init struct scx200_acb_iface *scx200_create_iface(const char *text,
-               int index)
+               struct device *dev, int index)
 {
        struct scx200_acb_iface *iface;
        struct i2c_adapter *adapter;
@@ -434,11 +441,11 @@ static __init struct scx200_acb_iface *scx200_create_iface(const char *text,
 
        adapter = &iface->adapter;
        i2c_set_adapdata(adapter, iface);
-       snprintf(adapter->name, I2C_NAME_SIZE, "%s ACB%d", text, index);
+       snprintf(adapter->name, sizeof(adapter->name), "%s ACB%d", text, index);
        adapter->owner = THIS_MODULE;
-       adapter->id = I2C_HW_SMBUS_SCX200;
        adapter->algo = &scx200_acb_algorithm;
-       adapter->class = I2C_CLASS_HWMON;
+       adapter->class = I2C_CLASS_HWMON | I2C_CLASS_SPD;
+       adapter->dev.parent = dev;
 
        mutex_init(&iface->mutex);
 
@@ -465,10 +472,10 @@ static int __init scx200_acb_create(struct scx200_acb_iface *iface)
                return -ENODEV;
        }
 
-       down(&scx200_acb_list_mutex);
+       mutex_lock(&scx200_acb_list_mutex);
        iface->next = scx200_acb_list;
        scx200_acb_list = iface;
-       up(&scx200_acb_list_mutex);
+       mutex_unlock(&scx200_acb_list_mutex);
 
        return 0;
 }
@@ -479,7 +486,7 @@ static __init int scx200_create_pci(const char *text, struct pci_dev *pdev,
        struct scx200_acb_iface *iface;
        int rc;
 
-       iface = scx200_create_iface(text, 0);
+       iface = scx200_create_iface(text, &pdev->dev, 0);
 
        if (iface == NULL)
                return -ENOMEM;
@@ -487,11 +494,12 @@ static __init int scx200_create_pci(const char *text, struct pci_dev *pdev,
        iface->pdev = pdev;
        iface->bar = bar;
 
-       pci_enable_device_bars(iface->pdev, 1 << iface->bar);
+       rc = pci_enable_device_io(iface->pdev);
+       if (rc)
+               goto errout_free;
 
        rc = pci_request_region(iface->pdev, iface->bar, iface->adapter.name);
-
-       if (rc != 0) {
+       if (rc) {
                printk(KERN_ERR NAME ": can't allocate PCI BAR %d\n",
                                iface->bar);
                goto errout_free;
@@ -516,12 +524,12 @@ static int __init scx200_create_isa(const char *text, unsigned long base,
        struct scx200_acb_iface *iface;
        int rc;
 
-       iface = scx200_create_iface(text, index);
+       iface = scx200_create_iface(text, NULL, index);
 
        if (iface == NULL)
                return -ENOMEM;
 
-       if (request_region(base, 8, iface->adapter.name) == 0) {
+       if (!request_region(base, 8, iface->adapter.name)) {
                printk(KERN_ERR NAME ": can't allocate io 0x%lx-0x%lx\n",
                       base, base + 8 - 1);
                rc = -EBUSY;
@@ -590,6 +598,7 @@ static __init int scx200_scan_pci(void)
                else {
                        int i;
 
+                       pci_dev_put(pdev);
                        for (i = 0; i < MAX_DEVICES; ++i) {
                                if (base[i] == 0)
                                        continue;
@@ -624,10 +633,10 @@ static void __exit scx200_acb_cleanup(void)
 {
        struct scx200_acb_iface *iface;
 
-       down(&scx200_acb_list_mutex);
+       mutex_lock(&scx200_acb_list_mutex);
        while ((iface = scx200_acb_list) != NULL) {
                scx200_acb_list = iface->next;
-               up(&scx200_acb_list_mutex);
+               mutex_unlock(&scx200_acb_list_mutex);
 
                i2c_del_adapter(&iface->adapter);
 
@@ -639,9 +648,9 @@ static void __exit scx200_acb_cleanup(void)
                        release_region(iface->base, 8);
 
                kfree(iface);
-               down(&scx200_acb_list_mutex);
+               mutex_lock(&scx200_acb_list_mutex);
        }
-       up(&scx200_acb_list_mutex);
+       mutex_unlock(&scx200_acb_list_mutex);
 }
 
 module_init(scx200_acb_init);