]> pilppa.org Git - linux-2.6-omap-h63xx.git/blobdiff - drivers/i2c/busses/i2c-pxa.c
afs: the AFS RPC op CBGetCapabilities is actually CBTellMeAboutYourself
[linux-2.6-omap-h63xx.git] / drivers / i2c / busses / i2c-pxa.c
index bb5466b27b59ab6796a28788ed2b3575274f9538..eb69fbadc9cb273a77111a0fe43a55debf13f4da 100644 (file)
 #include <linux/interrupt.h>
 #include <linux/i2c-pxa.h>
 #include <linux/platform_device.h>
+#include <linux/err.h>
+#include <linux/clk.h>
 
 #include <asm/hardware.h>
 #include <asm/irq.h>
 #include <asm/io.h>
 #include <asm/arch/i2c.h>
 #include <asm/arch/pxa-regs.h>
+#include <asm/arch/pxa2xx-gpio.h>
 
 struct pxa_i2c {
        spinlock_t              lock;
@@ -48,6 +51,7 @@ struct pxa_i2c {
        unsigned int            slave_addr;
 
        struct i2c_adapter      adap;
+       struct clk              *clk;
 #ifdef CONFIG_I2C_PXA_SLAVE
        struct i2c_slave_client *slave;
 #endif
@@ -62,6 +66,7 @@ struct pxa_i2c {
        unsigned long           iosize;
 
        int                     irq;
+       int                     use_pio;
 };
 
 #define _IBMR(i2c)     ((i2c)->reg_base + 0)
@@ -82,7 +87,7 @@ struct bits {
        const char *set;
        const char *unset;
 };
-#define BIT(m, s, u)   { .mask = m, .set = s, .unset = u }
+#define PXA_BIT(m, s, u)       { .mask = m, .set = s, .unset = u }
 
 static inline void
 decode_bits(const char *prefix, const struct bits *bits, int num, u32 val)
@@ -97,17 +102,17 @@ decode_bits(const char *prefix, const struct bits *bits, int num, u32 val)
 }
 
 static const struct bits isr_bits[] = {
-       BIT(ISR_RWM,    "RX",           "TX"),
-       BIT(ISR_ACKNAK, "NAK",          "ACK"),
-       BIT(ISR_UB,     "Bsy",          "Rdy"),
-       BIT(ISR_IBB,    "BusBsy",       "BusRdy"),
-       BIT(ISR_SSD,    "SlaveStop",    NULL),
-       BIT(ISR_ALD,    "ALD",          NULL),
-       BIT(ISR_ITE,    "TxEmpty",      NULL),
-       BIT(ISR_IRF,    "RxFull",       NULL),
-       BIT(ISR_GCAD,   "GenCall",      NULL),
-       BIT(ISR_SAD,    "SlaveAddr",    NULL),
-       BIT(ISR_BED,    "BusErr",       NULL),
+       PXA_BIT(ISR_RWM,        "RX",           "TX"),
+       PXA_BIT(ISR_ACKNAK,     "NAK",          "ACK"),
+       PXA_BIT(ISR_UB,         "Bsy",          "Rdy"),
+       PXA_BIT(ISR_IBB,        "BusBsy",       "BusRdy"),
+       PXA_BIT(ISR_SSD,        "SlaveStop",    NULL),
+       PXA_BIT(ISR_ALD,        "ALD",          NULL),
+       PXA_BIT(ISR_ITE,        "TxEmpty",      NULL),
+       PXA_BIT(ISR_IRF,        "RxFull",       NULL),
+       PXA_BIT(ISR_GCAD,       "GenCall",      NULL),
+       PXA_BIT(ISR_SAD,        "SlaveAddr",    NULL),
+       PXA_BIT(ISR_BED,        "BusErr",       NULL),
 };
 
 static void decode_ISR(unsigned int val)
@@ -117,28 +122,30 @@ static void decode_ISR(unsigned int val)
 }
 
 static const struct bits icr_bits[] = {
-       BIT(ICR_START,  "START",        NULL),
-       BIT(ICR_STOP,   "STOP",         NULL),
-       BIT(ICR_ACKNAK, "ACKNAK",       NULL),
-       BIT(ICR_TB,     "TB",           NULL),
-       BIT(ICR_MA,     "MA",           NULL),
-       BIT(ICR_SCLE,   "SCLE",         "scle"),
-       BIT(ICR_IUE,    "IUE",          "iue"),
-       BIT(ICR_GCD,    "GCD",          NULL),
-       BIT(ICR_ITEIE,  "ITEIE",        NULL),
-       BIT(ICR_IRFIE,  "IRFIE",        NULL),
-       BIT(ICR_BEIE,   "BEIE",         NULL),
-       BIT(ICR_SSDIE,  "SSDIE",        NULL),
-       BIT(ICR_ALDIE,  "ALDIE",        NULL),
-       BIT(ICR_SADIE,  "SADIE",        NULL),
-       BIT(ICR_UR,     "UR",           "ur"),
+       PXA_BIT(ICR_START,  "START",    NULL),
+       PXA_BIT(ICR_STOP,   "STOP",     NULL),
+       PXA_BIT(ICR_ACKNAK, "ACKNAK",   NULL),
+       PXA_BIT(ICR_TB,     "TB",       NULL),
+       PXA_BIT(ICR_MA,     "MA",       NULL),
+       PXA_BIT(ICR_SCLE,   "SCLE",     "scle"),
+       PXA_BIT(ICR_IUE,    "IUE",      "iue"),
+       PXA_BIT(ICR_GCD,    "GCD",      NULL),
+       PXA_BIT(ICR_ITEIE,  "ITEIE",    NULL),
+       PXA_BIT(ICR_IRFIE,  "IRFIE",    NULL),
+       PXA_BIT(ICR_BEIE,   "BEIE",     NULL),
+       PXA_BIT(ICR_SSDIE,  "SSDIE",    NULL),
+       PXA_BIT(ICR_ALDIE,  "ALDIE",    NULL),
+       PXA_BIT(ICR_SADIE,  "SADIE",    NULL),
+       PXA_BIT(ICR_UR,     "UR",               "ur"),
 };
 
+#ifdef CONFIG_I2C_PXA_SLAVE
 static void decode_ICR(unsigned int val)
 {
        decode_bits(KERN_DEBUG "ICR", icr_bits, ARRAY_SIZE(icr_bits), val);
        printk("\n");
 }
+#endif
 
 static unsigned int i2c_debug = DEBUG;
 
@@ -148,7 +155,7 @@ static void i2c_pxa_show_state(struct pxa_i2c *i2c, int lno, const char *fname)
                readl(_ISR(i2c)), readl(_ICR(i2c)), readl(_IBMR(i2c)));
 }
 
-#define show_state(i2c) i2c_pxa_show_state(i2c, __LINE__, __FUNCTION__)
+#define show_state(i2c) i2c_pxa_show_state(i2c, __LINE__, __func__)
 #else
 #define i2c_debug      0
 
@@ -160,6 +167,7 @@ static void i2c_pxa_show_state(struct pxa_i2c *i2c, int lno, const char *fname)
 #define eedbg(lvl, x...) do { if ((lvl) < 1) { printk(KERN_DEBUG "" x); } } while(0)
 
 static void i2c_pxa_master_complete(struct pxa_i2c *i2c, int ret);
+static irqreturn_t i2c_pxa_handler(int this_irq, void *dev_id);
 
 static void i2c_pxa_scream_blue_murder(struct pxa_i2c *i2c, const char *why)
 {
@@ -551,6 +559,71 @@ static inline void i2c_pxa_stop_message(struct pxa_i2c *i2c)
        writel(icr, _ICR(i2c));
 }
 
+static int i2c_pxa_pio_set_master(struct pxa_i2c *i2c)
+{
+       /* make timeout the same as for interrupt based functions */
+       long timeout = 2 * DEF_TIMEOUT;
+
+       /*
+        * Wait for the bus to become free.
+        */
+       while (timeout-- && readl(_ISR(i2c)) & (ISR_IBB | ISR_UB)) {
+               udelay(1000);
+               show_state(i2c);
+       }
+
+       if (timeout <= 0) {
+               show_state(i2c);
+               dev_err(&i2c->adap.dev,
+                       "i2c_pxa: timeout waiting for bus free\n");
+               return I2C_RETRY;
+       }
+
+       /*
+        * Set master mode.
+        */
+       writel(readl(_ICR(i2c)) | ICR_SCLE, _ICR(i2c));
+
+       return 0;
+}
+
+static int i2c_pxa_do_pio_xfer(struct pxa_i2c *i2c,
+                              struct i2c_msg *msg, int num)
+{
+       unsigned long timeout = 500000; /* 5 seconds */
+       int ret = 0;
+
+       ret = i2c_pxa_pio_set_master(i2c);
+       if (ret)
+               goto out;
+
+       i2c->msg = msg;
+       i2c->msg_num = num;
+       i2c->msg_idx = 0;
+       i2c->msg_ptr = 0;
+       i2c->irqlogidx = 0;
+
+       i2c_pxa_start_message(i2c);
+
+       while (timeout-- && i2c->msg_num > 0) {
+               i2c_pxa_handler(0, i2c);
+               udelay(10);
+       }
+
+       i2c_pxa_stop_message(i2c);
+
+       /*
+        * We place the return code in i2c->msg_idx.
+        */
+       ret = i2c->msg_idx;
+
+out:
+       if (timeout == 0)
+               i2c_pxa_scream_blue_murder(i2c, "timeout");
+
+       return ret;
+}
+
 /*
  * We are protected by the adapter bus mutex.
  */
@@ -607,6 +680,35 @@ static int i2c_pxa_do_xfer(struct pxa_i2c *i2c, struct i2c_msg *msg, int num)
        return ret;
 }
 
+static int i2c_pxa_pio_xfer(struct i2c_adapter *adap,
+                           struct i2c_msg msgs[], int num)
+{
+       struct pxa_i2c *i2c = adap->algo_data;
+       int ret, i;
+
+       /* 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 (!(readl(_ICR(i2c)) & ICR_IUE))
+               i2c_pxa_reset(i2c);
+
+       for (i = adap->retries; i >= 0; i--) {
+               ret = i2c_pxa_do_pio_xfer(i2c, msgs, num);
+               if (ret != I2C_RETRY)
+                       goto out;
+
+               if (i2c_debug)
+                       dev_dbg(&adap->dev, "Retrying transmission\n");
+               udelay(100);
+       }
+       i2c_pxa_scream_blue_murder(i2c, "exhausted retries");
+       ret = -EREMOTEIO;
+ out:
+       i2c_pxa_set_slave(i2c, ret);
+       return ret;
+}
+
 /*
  * i2c_pxa_master_complete - complete the message and wake up.
  */
@@ -618,7 +720,8 @@ static void i2c_pxa_master_complete(struct pxa_i2c *i2c, int ret)
        i2c->msg_num = 0;
        if (ret)
                i2c->msg_idx = ret;
-       wake_up(&i2c->wait);
+       if (!i2c->use_pio)
+               wake_up(&i2c->wait);
 }
 
 static void i2c_pxa_irq_txempty(struct pxa_i2c *i2c, u32 isr)
@@ -837,6 +940,37 @@ static const struct i2c_algorithm i2c_pxa_algorithm = {
        .functionality  = i2c_pxa_functionality,
 };
 
+static const struct i2c_algorithm i2c_pxa_pio_algorithm = {
+       .master_xfer    = i2c_pxa_pio_xfer,
+       .functionality  = i2c_pxa_functionality,
+};
+
+static void i2c_pxa_enable(struct platform_device *dev)
+{
+       if (cpu_is_pxa27x()) {
+               switch (dev->id) {
+               case 0:
+                       pxa_gpio_mode(GPIO117_I2CSCL_MD);
+                       pxa_gpio_mode(GPIO118_I2CSDA_MD);
+                       break;
+               case 1:
+                       local_irq_disable();
+                       PCFR |= PCFR_PI2CEN;
+                       local_irq_enable();
+                       break;
+               }
+       }
+}
+
+static void i2c_pxa_disable(struct platform_device *dev)
+{
+       if (cpu_is_pxa27x() && dev->id == 1) {
+               local_irq_disable();
+               PCFR &= ~PCFR_PI2CEN;
+               local_irq_enable();
+       }
+}
+
 #define res_len(r)             ((r)->end - (r)->start + 1)
 static int i2c_pxa_probe(struct platform_device *dev)
 {
@@ -861,13 +995,25 @@ static int i2c_pxa_probe(struct platform_device *dev)
        }
 
        i2c->adap.owner   = THIS_MODULE;
-       i2c->adap.algo    = &i2c_pxa_algorithm;
        i2c->adap.retries = 5;
 
        spin_lock_init(&i2c->lock);
        init_waitqueue_head(&i2c->wait);
 
-       sprintf(i2c->adap.name, "pxa_i2c-i2c.%u", dev->id);
+       /*
+        * If "dev->id" is negative we consider it as zero.
+        * The reason to do so is to avoid sysfs names that only make
+        * sense when there are multiple adapters.
+        */
+       i2c->adap.nr = dev->id != -1 ? dev->id : 0;
+       snprintf(i2c->adap.name, sizeof(i2c->adap.name), "pxa_i2c-i2c.%u",
+                i2c->adap.nr);
+
+       i2c->clk = clk_get(&dev->dev, "I2CCLK");
+       if (IS_ERR(i2c->clk)) {
+               ret = PTR_ERR(i2c->clk);
+               goto eclk;
+       }
 
        i2c->reg_base = ioremap(res->start, res_len(res));
        if (!i2c->reg_base) {
@@ -889,45 +1035,29 @@ static int i2c_pxa_probe(struct platform_device *dev)
        }
 #endif
 
-       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
-       }
+       clk_enable(i2c->clk);
+       i2c_pxa_enable(dev);
 
-       ret = request_irq(irq, i2c_pxa_handler, IRQF_DISABLED,
-                         i2c->adap.name, i2c);
-       if (ret)
-               goto ereqirq;
+       if (plat) {
+               i2c->adap.class = plat->class;
+               i2c->use_pio = plat->use_pio;
+       }
 
+       if (i2c->use_pio) {
+               i2c->adap.algo = &i2c_pxa_pio_algorithm;
+       } else {
+               i2c->adap.algo = &i2c_pxa_algorithm;
+               ret = request_irq(irq, i2c_pxa_handler, IRQF_DISABLED,
+                                 i2c->adap.name, i2c);
+               if (ret)
+                       goto ereqirq;
+       }
 
        i2c_pxa_reset(i2c);
 
        i2c->adap.algo_data = i2c;
        i2c->adap.dev.parent = &dev->dev;
 
-       if (plat) {
-               i2c->adap.class = plat->class;
-       }
-
-       /*
-        * If "dev->id" is negative we consider it as zero.
-        * The reason to do so is to avoid sysfs names that only make
-        * sense when there are multiple adapters.
-        */
-       i2c->adap.nr = dev->id != -1 ? dev->id : 0;
-
        ret = i2c_add_numbered_adapter(&i2c->adap);
        if (ret < 0) {
                printk(KERN_INFO "I2C: Failed to add bus\n");
@@ -946,47 +1076,36 @@ static int i2c_pxa_probe(struct platform_device *dev)
        return 0;
 
 eadapt:
-       free_irq(irq, i2c);
+       if (!i2c->use_pio)
+               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
-       }
+       clk_disable(i2c->clk);
+       i2c_pxa_disable(dev);
+       iounmap(i2c->reg_base);
 eremap:
+       clk_put(i2c->clk);
+eclk:
        kfree(i2c);
 emalloc:
        release_mem_region(res->start, res_len(res));
        return ret;
 }
 
-static int i2c_pxa_remove(struct platform_device *dev)
+static int __exit i2c_pxa_remove(struct platform_device *dev)
 {
        struct pxa_i2c *i2c = platform_get_drvdata(dev);
 
        platform_set_drvdata(dev, NULL);
 
        i2c_del_adapter(&i2c->adap);
-       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
-       }
+       if (!i2c->use_pio)
+               free_irq(i2c->irq, i2c);
+
+       clk_disable(i2c->clk);
+       clk_put(i2c->clk);
+       i2c_pxa_disable(dev);
+
+       iounmap(i2c->reg_base);
        release_mem_region(i2c->iobase, i2c->iosize);
        kfree(i2c);
 
@@ -995,9 +1114,10 @@ static int i2c_pxa_remove(struct platform_device *dev)
 
 static struct platform_driver i2c_pxa_driver = {
        .probe          = i2c_pxa_probe,
-       .remove         = i2c_pxa_remove,
+       .remove         = __exit_p(i2c_pxa_remove),
        .driver         = {
                .name   = "pxa2xx-i2c",
+               .owner  = THIS_MODULE,
        },
 };
 
@@ -1006,12 +1126,13 @@ static int __init i2c_adap_pxa_init(void)
        return platform_driver_register(&i2c_pxa_driver);
 }
 
-static void i2c_adap_pxa_exit(void)
+static void __exit i2c_adap_pxa_exit(void)
 {
-       return platform_driver_unregister(&i2c_pxa_driver);
+       platform_driver_unregister(&i2c_pxa_driver);
 }
 
 MODULE_LICENSE("GPL");
+MODULE_ALIAS("platform:pxa2xx-i2c");
 
 module_init(i2c_adap_pxa_init);
 module_exit(i2c_adap_pxa_exit);