Merge branch 'for-linus' of master.kernel.org:/home/rmk/linux-2.6-arm
[sfrench/cifs-2.6.git] / drivers / i2c / busses / i2c-pxa.c
index 81050d3c9b21935512f3db5f2a810245ebaafb4d..873544ab598e9b92695d9c9e6aa3ed0f09a6161b 100644 (file)
@@ -34,6 +34,7 @@
 
 #include <asm/hardware.h>
 #include <asm/irq.h>
+#include <asm/io.h>
 #include <asm/arch/i2c.h>
 #include <asm/arch/pxa-regs.h>
 
@@ -54,8 +55,21 @@ struct pxa_i2c {
        unsigned int            irqlogidx;
        u32                     isrlog[32];
        u32                     icrlog[32];
+
+       void __iomem            *reg_base;
+
+       unsigned long           iobase;
+       unsigned long           iosize;
+
+       int                     irq;
 };
 
+#define _IBMR(i2c)     ((i2c)->reg_base + 0)
+#define _IDBR(i2c)     ((i2c)->reg_base + 8)
+#define _ICR(i2c)      ((i2c)->reg_base + 0x10)
+#define _ISR(i2c)      ((i2c)->reg_base + 0x18)
+#define _ISAR(i2c)     ((i2c)->reg_base + 0x20)
+
 /*
  * I2C Slave mode address
  */
@@ -130,7 +144,8 @@ static unsigned int i2c_debug = DEBUG;
 
 static void i2c_pxa_show_state(struct pxa_i2c *i2c, int lno, const char *fname)
 {
-       dev_dbg(&i2c->adap.dev, "state:%s:%d: ISR=%08x, ICR=%08x, IBMR=%02x\n", fname, lno, ISR, ICR, IBMR);
+       dev_dbg(&i2c->adap.dev, "state:%s:%d: ISR=%08x, ICR=%08x, IBMR=%02x\n", fname, lno,
+               readl(_ISR(i2c)), readl(_ICR(i2c)), readl(_IBMR(i2c)));
 }
 
 #define show_state(i2c) i2c_pxa_show_state(i2c, __LINE__, __FUNCTION__)
@@ -153,7 +168,7 @@ static void i2c_pxa_scream_blue_murder(struct pxa_i2c *i2c, const char *why)
        printk("i2c: msg_num: %d msg_idx: %d msg_ptr: %d\n",
                i2c->msg_num, i2c->msg_idx, i2c->msg_ptr);
        printk("i2c: ICR: %08x ISR: %08x\n"
-              "i2c: log: ", ICR, ISR);
+              "i2c: log: ", readl(_ICR(i2c)), readl(_ISR(i2c)));
        for (i = 0; i < i2c->irqlogidx; i++)
                printk("[%08x:%08x] ", i2c->isrlog[i], i2c->icrlog[i]);
        printk("\n");
@@ -161,7 +176,7 @@ static void i2c_pxa_scream_blue_murder(struct pxa_i2c *i2c, const char *why)
 
 static inline int i2c_pxa_is_slavemode(struct pxa_i2c *i2c)
 {
-       return !(ICR & ICR_SCLE);
+       return !(readl(_ICR(i2c)) & ICR_SCLE);
 }
 
 static void i2c_pxa_abort(struct pxa_i2c *i2c)
@@ -173,28 +188,29 @@ static void i2c_pxa_abort(struct pxa_i2c *i2c)
                return;
        }
 
-       while (time_before(jiffies, timeout) && (IBMR & 0x1) == 0) {
-               unsigned long icr = ICR;
+       while (time_before(jiffies, timeout) && (readl(_IBMR(i2c)) & 0x1) == 0) {
+               unsigned long icr = readl(_ICR(i2c));
 
                icr &= ~ICR_START;
                icr |= ICR_ACKNAK | ICR_STOP | ICR_TB;
 
-               ICR = icr;
+               writel(icr, _ICR(i2c));
 
                show_state(i2c);
 
                msleep(1);
        }
 
-       ICR &= ~(ICR_MA | ICR_START | ICR_STOP);
+       writel(readl(_ICR(i2c)) & ~(ICR_MA | ICR_START | ICR_STOP),
+              _ICR(i2c));
 }
 
 static int i2c_pxa_wait_bus_not_busy(struct pxa_i2c *i2c)
 {
        int timeout = DEF_TIMEOUT;
 
-       while (timeout-- && ISR & (ISR_IBB | ISR_UB)) {
-               if ((ISR & ISR_SAD) != 0)
+       while (timeout-- && readl(_ISR(i2c)) & (ISR_IBB | ISR_UB)) {
+               if ((readl(_ISR(i2c)) & ISR_SAD) != 0)
                        timeout += 4;
 
                msleep(2);
@@ -214,9 +230,9 @@ static int i2c_pxa_wait_master(struct pxa_i2c *i2c)
        while (time_before(jiffies, timeout)) {
                if (i2c_debug > 1)
                        dev_dbg(&i2c->adap.dev, "%s: %ld: ISR=%08x, ICR=%08x, IBMR=%02x\n",
-                               __func__, (long)jiffies, ISR, ICR, IBMR);
+                               __func__, (long)jiffies, readl(_ISR(i2c)), readl(_ICR(i2c)), readl(_IBMR(i2c)));
 
-               if (ISR & ISR_SAD) {
+               if (readl(_ISR(i2c)) & ISR_SAD) {
                        if (i2c_debug > 0)
                                dev_dbg(&i2c->adap.dev, "%s: Slave detected\n", __func__);
                        goto out;
@@ -226,7 +242,7 @@ static int i2c_pxa_wait_master(struct pxa_i2c *i2c)
                 * quick check of the i2c lines themselves to ensure they've
                 * gone high...
                 */
-               if ((ISR & (ISR_UB | ISR_IBB)) == 0 && IBMR == 3) {
+               if ((readl(_ISR(i2c)) & (ISR_UB | ISR_IBB)) == 0 && readl(_IBMR(i2c)) == 3) {
                        if (i2c_debug > 0)
                                dev_dbg(&i2c->adap.dev, "%s: done\n", __func__);
                        return 1;
@@ -246,7 +262,7 @@ static int i2c_pxa_set_master(struct pxa_i2c *i2c)
        if (i2c_debug)
                dev_dbg(&i2c->adap.dev, "setting to bus master\n");
 
-       if ((ISR & (ISR_UB | ISR_IBB)) != 0) {
+       if ((readl(_ISR(i2c)) & (ISR_UB | ISR_IBB)) != 0) {
                dev_dbg(&i2c->adap.dev, "%s: unit is busy\n", __func__);
                if (!i2c_pxa_wait_master(i2c)) {
                        dev_dbg(&i2c->adap.dev, "%s: error: unit busy\n", __func__);
@@ -254,7 +270,7 @@ static int i2c_pxa_set_master(struct pxa_i2c *i2c)
                }
        }
 
-       ICR |= ICR_SCLE;
+       writel(readl(_ICR(i2c)) | ICR_SCLE, _ICR(i2c));
        return 0;
 }
 
@@ -270,10 +286,11 @@ static int i2c_pxa_wait_slave(struct pxa_i2c *i2c)
        while (time_before(jiffies, timeout)) {
                if (i2c_debug > 1)
                        dev_dbg(&i2c->adap.dev, "%s: %ld: ISR=%08x, ICR=%08x, IBMR=%02x\n",
-                               __func__, (long)jiffies, ISR, ICR, IBMR);
+                               __func__, (long)jiffies, readl(_ISR(i2c)), readl(_ICR(i2c)), readl(_IBMR(i2c)));
 
-               if ((ISR & (ISR_UB|ISR_IBB|ISR_SAD)) == ISR_SAD ||
-                   (ICR & ICR_SCLE) == 0) {
+               if ((readl(_ISR(i2c)) & (ISR_UB|ISR_IBB)) == 0 ||
+                   (readl(_ISR(i2c)) & ISR_SAD) != 0 ||
+                   (readl(_ICR(i2c)) & ICR_SCLE) == 0) {
                        if (i2c_debug > 1)
                                dev_dbg(&i2c->adap.dev, "%s: done\n", __func__);
                        return 1;
@@ -301,9 +318,9 @@ static void i2c_pxa_set_slave(struct pxa_i2c *i2c, int errcode)
                /* we need to wait for the stop condition to end */
 
                /* if we where in stop, then clear... */
-               if (ICR & ICR_STOP) {
+               if (readl(_ICR(i2c)) & ICR_STOP) {
                        udelay(100);
-                       ICR &= ~ICR_STOP;
+                       writel(readl(_ICR(i2c)) & ~ICR_STOP, _ICR(i2c));
                }
 
                if (!i2c_pxa_wait_slave(i2c)) {
@@ -313,12 +330,12 @@ static void i2c_pxa_set_slave(struct pxa_i2c *i2c, int errcode)
                }
        }
 
-       ICR &= ~(ICR_STOP|ICR_ACKNAK|ICR_MA);
-       ICR &= ~ICR_SCLE;
+       writel(readl(_ICR(i2c)) & ~(ICR_STOP|ICR_ACKNAK|ICR_MA), _ICR(i2c));
+       writel(readl(_ICR(i2c)) & ~ICR_SCLE, _ICR(i2c));
 
        if (i2c_debug) {
-               dev_dbg(&i2c->adap.dev, "ICR now %08x, ISR %08x\n", ICR, ISR);
-               decode_ICR(ICR);
+               dev_dbg(&i2c->adap.dev, "ICR now %08x, ISR %08x\n", readl(_ICR(i2c)), readl(_ISR(i2c)));
+               decode_ICR(readl(_ICR(i2c)));
        }
 }
 #else
@@ -333,156 +350,29 @@ static void i2c_pxa_reset(struct pxa_i2c *i2c)
        i2c_pxa_abort(i2c);
 
        /* reset according to 9.8 */
-       ICR = ICR_UR;
-       ISR = I2C_ISR_INIT;
-       ICR &= ~ICR_UR;
+       writel(ICR_UR, _ICR(i2c));
+       writel(I2C_ISR_INIT, _ISR(i2c));
+       writel(readl(_ICR(i2c)) & ~ICR_UR, _ICR(i2c));
 
-       ISAR = i2c->slave_addr;
+       writel(i2c->slave_addr, _ISAR(i2c));
 
        /* set control register values */
-       ICR = I2C_ICR_INIT;
+       writel(I2C_ICR_INIT, _ICR(i2c));
 
 #ifdef CONFIG_I2C_PXA_SLAVE
        dev_info(&i2c->adap.dev, "Enabling slave mode\n");
-       ICR |= ICR_SADIE | ICR_ALDIE | ICR_SSDIE;
+       writel(readl(_ICR(i2c)) | ICR_SADIE | ICR_ALDIE | ICR_SSDIE, _ICR(i2c));
 #endif
 
        i2c_pxa_set_slave(i2c, 0);
 
        /* enable unit */
-       ICR |= ICR_IUE;
+       writel(readl(_ICR(i2c)) | ICR_IUE, _ICR(i2c));
        udelay(100);
 }
 
 
 #ifdef CONFIG_I2C_PXA_SLAVE
-/*
- * I2C EEPROM emulation.
- */
-static struct i2c_eeprom_emu eeprom = {
-       .size = I2C_EEPROM_EMU_SIZE,
-       .watch = LIST_HEAD_INIT(eeprom.watch),
-};
-
-struct i2c_eeprom_emu *i2c_pxa_get_eeprom(void)
-{
-       return &eeprom;
-}
-
-int i2c_eeprom_emu_addwatcher(struct i2c_eeprom_emu *emu, void *data,
-                             unsigned int addr, unsigned int size,
-                             struct i2c_eeprom_emu_watcher *watcher)
-{
-       struct i2c_eeprom_emu_watch *watch;
-       unsigned long flags;
-
-       if (addr + size > emu->size)
-               return -EINVAL;
-
-       watch = kmalloc(sizeof(struct i2c_eeprom_emu_watch), GFP_KERNEL);
-       if (watch) {
-               watch->start = addr;
-               watch->end = addr + size - 1;
-               watch->ops = watcher;
-               watch->data = data;
-
-               local_irq_save(flags);
-               list_add(&watch->node, &emu->watch);
-               local_irq_restore(flags);
-       }
-
-       return watch ? 0 : -ENOMEM;
-}
-
-void i2c_eeprom_emu_delwatcher(struct i2c_eeprom_emu *emu, void *data,
-                              struct i2c_eeprom_emu_watcher *watcher)
-{
-       struct i2c_eeprom_emu_watch *watch, *n;
-       unsigned long flags;
-
-       list_for_each_entry_safe(watch, n, &emu->watch, node) {
-               if (watch->ops == watcher && watch->data == data) {
-                       local_irq_save(flags);
-                       list_del(&watch->node);
-                       local_irq_restore(flags);
-                       kfree(watch);
-               }
-       }
-}
-
-static void i2c_eeprom_emu_event(void *ptr, i2c_slave_event_t event)
-{
-       struct i2c_eeprom_emu *emu = ptr;
-
-       eedbg(3, "i2c_eeprom_emu_event: %d\n", event);
-
-       switch (event) {
-       case I2C_SLAVE_EVENT_START_WRITE:
-               emu->seen_start = 1;
-               eedbg(2, "i2c_eeprom: write initiated\n");
-               break;
-
-       case I2C_SLAVE_EVENT_START_READ:
-               emu->seen_start = 0;
-               eedbg(2, "i2c_eeprom: read initiated\n");
-               break;
-
-       case I2C_SLAVE_EVENT_STOP:
-               emu->seen_start = 0;
-               eedbg(2, "i2c_eeprom: received stop\n");
-               break;
-
-       default:
-               eedbg(0, "i2c_eeprom: unhandled event\n");
-               break;
-       }
-}
-
-static int i2c_eeprom_emu_read(void *ptr)
-{
-       struct i2c_eeprom_emu *emu = ptr;
-       int ret;
-
-       ret = emu->bytes[emu->ptr];
-       emu->ptr = (emu->ptr + 1) % emu->size;
-
-       return ret;
-}
-
-static void i2c_eeprom_emu_write(void *ptr, unsigned int val)
-{
-       struct i2c_eeprom_emu *emu = ptr;
-       struct i2c_eeprom_emu_watch *watch;
-
-       if (emu->seen_start != 0) {
-               eedbg(2, "i2c_eeprom_emu_write: setting ptr %02x\n", val);
-               emu->ptr = val;
-               emu->seen_start = 0;
-               return;
-       }
-
-       emu->bytes[emu->ptr] = val;
-
-       eedbg(1, "i2c_eeprom_emu_write: ptr=0x%02x, val=0x%02x\n",
-             emu->ptr, val);
-
-       list_for_each_entry(watch, &emu->watch, node) {
-               if (!watch->ops || !watch->ops->write)
-                       continue;
-               if (watch->start <= emu->ptr && watch->end >= emu->ptr)
-                       watch->ops->write(watch->data, emu->ptr, val);
-       }
-
-       emu->ptr = (emu->ptr + 1) % emu->size;
-}
-
-struct i2c_slave_client eeprom_client = {
-       .data   = &eeprom,
-       .event  = i2c_eeprom_emu_event,
-       .read   = i2c_eeprom_emu_read,
-       .write  = i2c_eeprom_emu_write
-};
-
 /*
  * PXA I2C Slave mode
  */
@@ -492,21 +382,24 @@ static void i2c_pxa_slave_txempty(struct pxa_i2c *i2c, u32 isr)
        if (isr & ISR_BED) {
                /* what should we do here? */
        } else {
-               int ret = i2c->slave->read(i2c->slave->data);
+               int ret = 0;
 
-               IDBR = ret;
-               ICR |= ICR_TB;   /* allow next byte */
+               if (i2c->slave != NULL)
+                       ret = i2c->slave->read(i2c->slave->data);
+
+               writel(ret, _IDBR(i2c));
+               writel(readl(_ICR(i2c)) | ICR_TB, _ICR(i2c));   /* allow next byte */
        }
 }
 
 static void i2c_pxa_slave_rxfull(struct pxa_i2c *i2c, u32 isr)
 {
-       unsigned int byte = IDBR;
+       unsigned int byte = readl(_IDBR(i2c));
 
        if (i2c->slave != NULL)
                i2c->slave->write(i2c->slave->data, byte);
 
-       ICR |= ICR_TB;
+       writel(readl(_ICR(i2c)) | ICR_TB, _ICR(i2c));
 }
 
 static void i2c_pxa_slave_start(struct pxa_i2c *i2c, u32 isr)
@@ -526,13 +419,13 @@ static void i2c_pxa_slave_start(struct pxa_i2c *i2c, u32 isr)
         * start condition... if this happens, we'd better back off
         * and stop holding the poor thing up
         */
-       ICR &= ~(ICR_START|ICR_STOP);
-       ICR |= ICR_TB;
+       writel(readl(_ICR(i2c)) & ~(ICR_START|ICR_STOP), _ICR(i2c));
+       writel(readl(_ICR(i2c)) | ICR_TB, _ICR(i2c));
 
        timeout = 0x10000;
 
        while (1) {
-               if ((IBMR & 2) == 2)
+               if ((readl(_IBMR(i2c)) & 2) == 2)
                        break;
 
                timeout--;
@@ -543,7 +436,7 @@ static void i2c_pxa_slave_start(struct pxa_i2c *i2c, u32 isr)
                }
        }
 
-       ICR &= ~ICR_SCLE;
+       writel(readl(_ICR(i2c)) & ~ICR_SCLE, _ICR(i2c));
 }
 
 static void i2c_pxa_slave_stop(struct pxa_i2c *i2c)
@@ -570,14 +463,14 @@ static void i2c_pxa_slave_txempty(struct pxa_i2c *i2c, u32 isr)
        if (isr & ISR_BED) {
                /* what should we do here? */
        } else {
-               IDBR = 0;
-               ICR |= ICR_TB;
+               writel(0, _IDBR(i2c));
+               writel(readl(_ICR(i2c)) | ICR_TB, _ICR(i2c));
        }
 }
 
 static void i2c_pxa_slave_rxfull(struct pxa_i2c *i2c, u32 isr)
 {
-       ICR |= ICR_TB | ICR_ACKNAK;
+       writel(readl(_ICR(i2c)) | ICR_TB | ICR_ACKNAK, _ICR(i2c));
 }
 
 static void i2c_pxa_slave_start(struct pxa_i2c *i2c, u32 isr)
@@ -589,13 +482,13 @@ static void i2c_pxa_slave_start(struct pxa_i2c *i2c, u32 isr)
         * start condition... if this happens, we'd better back off
         * and stop holding the poor thing up
         */
-       ICR &= ~(ICR_START|ICR_STOP);
-       ICR |= ICR_TB | ICR_ACKNAK;
+       writel(readl(_ICR(i2c)) & ~(ICR_START|ICR_STOP), _ICR(i2c));
+       writel(readl(_ICR(i2c)) | ICR_TB | ICR_ACKNAK, _ICR(i2c));
 
        timeout = 0x10000;
 
        while (1) {
-               if ((IBMR & 2) == 2)
+               if ((readl(_IBMR(i2c)) & 2) == 2)
                        break;
 
                timeout--;
@@ -606,7 +499,7 @@ static void i2c_pxa_slave_start(struct pxa_i2c *i2c, u32 isr)
                }
        }
 
-       ICR &= ~ICR_SCLE;
+       writel(readl(_ICR(i2c)) & ~ICR_SCLE, _ICR(i2c));
 }
 
 static void i2c_pxa_slave_stop(struct pxa_i2c *i2c)
@@ -637,13 +530,25 @@ static inline void i2c_pxa_start_message(struct pxa_i2c *i2c)
        /*
         * Step 1: target slave address into IDBR
         */
-       IDBR = i2c_pxa_addr_byte(i2c->msg);
+       writel(i2c_pxa_addr_byte(i2c->msg), _IDBR(i2c));
 
        /*
         * Step 2: initiate the write.
         */
-       icr = ICR & ~(ICR_STOP | ICR_ALDIE);
-       ICR = icr | ICR_START | ICR_TB;
+       icr = readl(_ICR(i2c)) & ~(ICR_STOP | ICR_ALDIE);
+       writel(icr | ICR_START | ICR_TB, _ICR(i2c));
+}
+
+static inline void i2c_pxa_stop_message(struct pxa_i2c *i2c)
+{
+       u32 icr;
+
+       /*
+        * Clear the STOP and ACK flags
+        */
+       icr = readl(_ICR(i2c));
+       icr &= ~(ICR_STOP | ICR_ACKNAK);
+       writel(icr, _IRC(i2c));
 }
 
 /*
@@ -688,6 +593,7 @@ static int i2c_pxa_do_xfer(struct pxa_i2c *i2c, struct i2c_msg *msg, int num)
         * The rest of the processing occurs in the interrupt handler.
         */
        timeout = wait_event_timeout(i2c->wait, i2c->msg_num == 0, HZ * 5);
+       i2c_pxa_stop_message(i2c);
 
        /*
         * We place the return code in i2c->msg_idx.
@@ -717,7 +623,7 @@ static void i2c_pxa_master_complete(struct pxa_i2c *i2c, int ret)
 
 static void i2c_pxa_irq_txempty(struct pxa_i2c *i2c, u32 isr)
 {
-       u32 icr = ICR & ~(ICR_START|ICR_STOP|ICR_ACKNAK|ICR_TB);
+       u32 icr = readl(_ICR(i2c)) & ~(ICR_START|ICR_STOP|ICR_ACKNAK|ICR_TB);
 
  again:
        /*
@@ -768,7 +674,7 @@ static void i2c_pxa_irq_txempty(struct pxa_i2c *i2c, u32 isr)
                /*
                 * Write mode.  Write the next data byte.
                 */
-               IDBR = i2c->msg->buf[i2c->msg_ptr++];
+               writel(i2c->msg->buf[i2c->msg_ptr++], _IDBR(i2c));
 
                icr |= ICR_ALDIE | ICR_TB;
 
@@ -798,7 +704,7 @@ static void i2c_pxa_irq_txempty(struct pxa_i2c *i2c, u32 isr)
                /*
                 * Write the next address.
                 */
-               IDBR = i2c_pxa_addr_byte(i2c->msg);
+               writel(i2c_pxa_addr_byte(i2c->msg), _IDBR(i2c));
 
                /*
                 * And trigger a repeated start, and send the byte.
@@ -819,18 +725,18 @@ static void i2c_pxa_irq_txempty(struct pxa_i2c *i2c, u32 isr)
 
        i2c->icrlog[i2c->irqlogidx-1] = icr;
 
-       ICR = icr;
+       writel(icr, _ICR(i2c));
        show_state(i2c);
 }
 
 static void i2c_pxa_irq_rxfull(struct pxa_i2c *i2c, u32 isr)
 {
-       u32 icr = ICR & ~(ICR_START|ICR_STOP|ICR_ACKNAK|ICR_TB);
+       u32 icr = readl(_ICR(i2c)) & ~(ICR_START|ICR_STOP|ICR_ACKNAK|ICR_TB);
 
        /*
         * Read the byte.
         */
-       i2c->msg->buf[i2c->msg_ptr++] = IDBR;
+       i2c->msg->buf[i2c->msg_ptr++] = readl(_IDBR(i2c));
 
        if (i2c->msg_ptr < i2c->msg->len) {
                /*
@@ -847,17 +753,17 @@ static void i2c_pxa_irq_rxfull(struct pxa_i2c *i2c, u32 isr)
 
        i2c->icrlog[i2c->irqlogidx-1] = icr;
 
-       ICR = icr;
+       writel(icr, _ICR(i2c));
 }
 
 static irqreturn_t i2c_pxa_handler(int this_irq, void *dev_id)
 {
        struct pxa_i2c *i2c = dev_id;
-       u32 isr = ISR;
+       u32 isr = readl(_ISR(i2c));
 
        if (i2c_debug > 2 && 0) {
                dev_dbg(&i2c->adap.dev, "%s: ISR=%08x, ICR=%08x, IBMR=%02x\n",
-                       __func__, isr, ICR, IBMR);
+                       __func__, isr, readl(_ICR(i2c)), readl(_IBMR(i2c)));
                decode_ISR(isr);
        }
 
@@ -869,7 +775,7 @@ static irqreturn_t i2c_pxa_handler(int this_irq, void *dev_id)
        /*
         * Always clear all pending IRQs.
         */
-       ISR = isr & (ISR_SSD|ISR_ALD|ISR_ITE|ISR_IRF|ISR_SAD|ISR_BED);
+       writel(isr & (ISR_SSD|ISR_ALD|ISR_ITE|ISR_IRF|ISR_SAD|ISR_BED), _ISR(i2c));
 
        if (isr & ISR_SAD)
                i2c_pxa_slave_start(i2c, isr);
@@ -902,7 +808,7 @@ static int i2c_pxa_xfer(struct i2c_adapter *adap, struct i2c_msg msgs[], int num
        /* If the I2C controller is disabled we need to reset it (probably due
           to a suspend/resume destroying state). We do this here as we can then
           avoid worrying about resuming the controller before its users. */
-       if (!(ICR & ICR_IUE))
+       if (!(readl(_ICR(i2c)) & ICR_IUE))
                i2c_pxa_reset(i2c);
 
        for (i = adap->retries; i >= 0; i--) {
@@ -932,56 +838,98 @@ static const struct i2c_algorithm i2c_pxa_algorithm = {
 };
 
 static struct pxa_i2c i2c_pxa = {
-       .lock   = SPIN_LOCK_UNLOCKED,
-       .wait   = __WAIT_QUEUE_HEAD_INITIALIZER(i2c_pxa.wait),
+       .lock   = __SPIN_LOCK_UNLOCKED(i2c_pxa.lock),
        .adap   = {
                .owner          = THIS_MODULE,
                .algo           = &i2c_pxa_algorithm,
-               .name           = "pxa2xx-i2c",
+               .name           = "pxa2xx-i2c.0",
                .retries        = 5,
        },
 };
 
+#define res_len(r)             ((r)->end - (r)->start + 1)
 static int i2c_pxa_probe(struct platform_device *dev)
 {
        struct pxa_i2c *i2c = &i2c_pxa;
-#ifdef CONFIG_I2C_PXA_SLAVE
+       struct resource *res;
        struct i2c_pxa_platform_data *plat = dev->dev.platform_data;
-#endif
        int ret;
+       int irq;
 
-#ifdef CONFIG_PXA27x
-       pxa_gpio_mode(GPIO117_I2CSCL_MD);
-       pxa_gpio_mode(GPIO118_I2CSDA_MD);
-       udelay(100);
-#endif
+       res = platform_get_resource(dev, IORESOURCE_MEM, 0);
+       irq = platform_get_irq(dev, 0);
+       if (res == NULL || irq < 0)
+               return -ENODEV;
+
+       if (!request_mem_region(res->start, res_len(res), res->name))
+               return -ENOMEM;
+
+       i2c = kmalloc(sizeof(struct pxa_i2c), GFP_KERNEL);
+       if (!i2c) {
+               ret = -ENOMEM;
+               goto emalloc;
+       }
+
+       memcpy(i2c, &i2c_pxa, sizeof(struct pxa_i2c));
+       init_waitqueue_head(&i2c->wait);
+       i2c->adap.name[strlen(i2c->adap.name) - 1] = '0' + dev->id % 10;
+
+       i2c->reg_base = ioremap(res->start, res_len(res));
+       if (!i2c->reg_base) {
+               ret = -EIO;
+               goto eremap;
+       }
+
+       i2c->iobase = res->start;
+       i2c->iosize = res_len(res);
+
+       i2c->irq = irq;
 
        i2c->slave_addr = I2C_PXA_SLAVE_ADDR;
 
 #ifdef CONFIG_I2C_PXA_SLAVE
-       i2c->slave = &eeprom_client;
        if (plat) {
                i2c->slave_addr = plat->slave_addr;
-               if (plat->slave)
-                       i2c->slave = plat->slave;
+               i2c->slave = plat->slave;
        }
 #endif
 
-       pxa_set_cken(CKEN14_I2C, 1);
-       ret = request_irq(IRQ_I2C, i2c_pxa_handler, IRQF_DISABLED,
-                         "pxa2xx-i2c", i2c);
+       switch (dev->id) {
+       case 0:
+#ifdef CONFIG_PXA27x
+               pxa_gpio_mode(GPIO117_I2CSCL_MD);
+               pxa_gpio_mode(GPIO118_I2CSDA_MD);
+#endif
+               pxa_set_cken(CKEN_I2C, 1);
+               break;
+#ifdef CONFIG_PXA27x
+       case 1:
+               local_irq_disable();
+               PCFR |= PCFR_PI2CEN;
+               local_irq_enable();
+               pxa_set_cken(CKEN_PWRI2C, 1);
+#endif
+       }
+
+       ret = request_irq(irq, i2c_pxa_handler, IRQF_DISABLED,
+                         i2c->adap.name, i2c);
        if (ret)
-               goto out;
+               goto ereqirq;
+
 
        i2c_pxa_reset(i2c);
 
        i2c->adap.algo_data = i2c;
        i2c->adap.dev.parent = &dev->dev;
 
+       if (plat) {
+               i2c->adap.class = plat->class;
+       }
+
        ret = i2c_add_adapter(&i2c->adap);
        if (ret < 0) {
                printk(KERN_INFO "I2C: Failed to add bus\n");
-               goto err_irq;
+               goto eadapt;
        }
 
        platform_set_drvdata(dev, i2c);
@@ -995,9 +943,25 @@ static int i2c_pxa_probe(struct platform_device *dev)
 #endif
        return 0;
 
- err_irq:
-       free_irq(IRQ_I2C, i2c);
- out:
+eadapt:
+       free_irq(irq, i2c);
+ereqirq:
+       switch (dev->id) {
+       case 0:
+               pxa_set_cken(CKEN_I2C, 0);
+               break;
+#ifdef CONFIG_PXA27x
+       case 1:
+               pxa_set_cken(CKEN_PWRI2C, 0);
+               local_irq_disable();
+               PCFR &= ~PCFR_PI2CEN;
+               local_irq_enable();
+#endif
+       }
+eremap:
+       kfree(i2c);
+emalloc:
+       release_mem_region(res->start, res_len(res));
        return ret;
 }
 
@@ -1008,8 +972,21 @@ static int i2c_pxa_remove(struct platform_device *dev)
        platform_set_drvdata(dev, NULL);
 
        i2c_del_adapter(&i2c->adap);
-       free_irq(IRQ_I2C, i2c);
-       pxa_set_cken(CKEN14_I2C, 0);
+       free_irq(i2c->irq, i2c);
+       switch (dev->id) {
+       case 0:
+               pxa_set_cken(CKEN_I2C, 0);
+               break;
+#ifdef CONFIG_PXA27x
+       case 1:
+               pxa_set_cken(CKEN_PWRI2C, 0);
+               local_irq_disable();
+               PCFR &= ~PCFR_PI2CEN;
+               local_irq_enable();
+#endif
+       }
+       release_mem_region(i2c->iobase, i2c->iosize);
+       kfree(i2c);
 
        return 0;
 }