]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blobdiff - drivers/i2c/busses/i2c-omap.c
Merge current mainline tree into linux-omap tree
[linux-2.6-omap-h63xx.git] / drivers / i2c / busses / i2c-omap.c
index da6639707ea31e32f944d9cccae70e7827c4cb2a..9903e35d0b20966b3245f78869ff7b4873fd5111 100644 (file)
@@ -2,13 +2,15 @@
  * TI OMAP I2C master mode driver
  *
  * Copyright (C) 2003 MontaVista Software, Inc.
- * Copyright (C) 2004 Texas Instruments.
- *
- * Updated to work with multiple I2C interfaces on 24xx by
- * Tony Lindgren <tony@atomide.com> and Imre Deak <imre.deak@nokia.com>
  * Copyright (C) 2005 Nokia Corporation
+ * Copyright (C) 2004 - 2007 Texas Instruments.
  *
- * Cleaned up by Juha Yrjölä <juha.yrjola@nokia.com>
+ * Originally written by MontaVista Software, Inc.
+ * Additional contributions by:
+ *     Tony Lindgren <tony@atomide.com>
+ *     Imre Deak <imre.deak@nokia.com>
+ *     Juha Yrjölä <juha.yrjola@nokia.com>
+ *     Syed Khasim <x0khasim@ti.com>
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
 
 #include <asm/io.h>
 
+/* Hack to enable zero length transfers and smbus quick until clean fix
+   is available */
+#define OMAP_HACK
+
 /* timeout waiting for the controller to respond */
 #define OMAP_I2C_TIMEOUT (msecs_to_jiffies(1000))
 
 #define OMAP_I2C_SCLL_REG              0x34
 #define OMAP_I2C_SCLH_REG              0x38
 #define OMAP_I2C_SYSTEST_REG           0x3c
+#define OMAP_I2C_BUFSTAT_REG           0x40
 
 /* I2C Interrupt Enable Register (OMAP_I2C_IE): */
+#define OMAP_I2C_IE_XDR                (1 << 14)       /* TX Buffer draining int enable */
+#define OMAP_I2C_IE_RDR                (1 << 13)       /* RX Buffer draining int enable */
 #define OMAP_I2C_IE_XRDY       (1 << 4)        /* TX data ready int enable */
 #define OMAP_I2C_IE_RRDY       (1 << 3)        /* RX data ready int enable */
 #define OMAP_I2C_IE_ARDY       (1 << 2)        /* Access ready int enable */
@@ -64,7 +73,8 @@
 #define OMAP_I2C_IE_AL         (1 << 0)        /* Arbitration lost int ena */
 
 /* I2C Status Register (OMAP_I2C_STAT): */
-#define OMAP_I2C_STAT_SBD      (1 << 15)       /* Single byte data */
+#define OMAP_I2C_STAT_XDR      (1 << 14)       /* TX Buffer draining */
+#define OMAP_I2C_STAT_RDR      (1 << 13)       /* RX Buffer draining */
 #define OMAP_I2C_STAT_BB       (1 << 12)       /* Bus busy */
 #define OMAP_I2C_STAT_ROVR     (1 << 11)       /* Receive overrun */
 #define OMAP_I2C_STAT_XUDF     (1 << 10)       /* Transmit underflow */
 
 /* I2C Buffer Configuration Register (OMAP_I2C_BUF): */
 #define OMAP_I2C_BUF_RDMA_EN   (1 << 15)       /* RX DMA channel enable */
+#define OMAP_I2C_BUF_RXFIF_CLR (1 << 14)       /* RX FIFO Clear */
 #define OMAP_I2C_BUF_XDMA_EN   (1 << 7)        /* TX DMA channel enable */
+#define OMAP_I2C_BUF_TXFIF_CLR (1 << 6)        /* TX FIFO Clear */
 
 /* I2C Configuration Register (OMAP_I2C_CON): */
 #define OMAP_I2C_CON_EN                (1 << 15)       /* I2C module enable */
 #define OMAP_I2C_CON_BE                (1 << 14)       /* Big endian mode */
+#define OMAP_I2C_CON_OPMODE_HS (1 << 12)       /* High Speed support */
 #define OMAP_I2C_CON_STB       (1 << 11)       /* Start byte mode (master) */
 #define OMAP_I2C_CON_MST       (1 << 10)       /* Master/slave mode */
 #define OMAP_I2C_CON_TRX       (1 << 9)        /* TX/RX mode (master only) */
 #define OMAP_I2C_CON_STP       (1 << 1)        /* Stop cond (master only) */
 #define OMAP_I2C_CON_STT       (1 << 0)        /* Start condition (master) */
 
+/* I2C SCL time value when Master */
+#define OMAP_I2C_SCLL_HSSCLL   8
+#define OMAP_I2C_SCLH_HSSCLH   8
+
 /* I2C System Test Register (OMAP_I2C_SYSTEST): */
 #ifdef DEBUG
 #define OMAP_I2C_SYSTEST_ST_EN         (1 << 15)       /* System test enable */
 /* I2C System Configuration Register (OMAP_I2C_SYSC): */
 #define OMAP_I2C_SYSC_SRST             (1 << 1)        /* Soft Reset */
 
-/* REVISIT: Use platform_data instead of module parameters */
-/* Fast Mode = 400 kHz, Standard = 100 kHz */
-static int clock = 100; /* Default: 100 kHz */
-module_param(clock, int, 0);
-MODULE_PARM_DESC(clock, "Set I2C clock in kHz: 400=fast mode (default == 100)");
-
 struct omap_i2c_dev {
        struct device           *dev;
        void __iomem            *base;          /* virtual */
@@ -123,11 +134,17 @@ struct omap_i2c_dev {
        struct clk              *fclk;          /* Functional clock */
        struct completion       cmd_complete;
        struct resource         *ioarea;
+       u32                     speed;          /* Speed of bus in Khz */
        u16                     cmd_err;
        u8                      *buf;
        size_t                  buf_len;
        struct i2c_adapter      adapter;
+       u8                      fifo_size;      /* use as flag and value
+                                                * fifo_size==0 implies no fifo
+                                                * if set, should be trsh+1
+                                                */
        unsigned                rev1:1;
+       unsigned                b_hw:1;         /* bad h/w fixes */
 };
 
 static inline void omap_i2c_write_reg(struct omap_i2c_dev *i2c_dev,
@@ -143,24 +160,35 @@ static inline u16 omap_i2c_read_reg(struct omap_i2c_dev *i2c_dev, int reg)
 
 static int omap_i2c_get_clocks(struct omap_i2c_dev *dev)
 {
-       if (cpu_is_omap16xx() || cpu_is_omap24xx()) {
+       if (cpu_is_omap16xx() || cpu_class_is_omap2()) {
                dev->iclk = clk_get(dev->dev, "i2c_ick");
                if (IS_ERR(dev->iclk)) {
                        dev->iclk = NULL;
                        return -ENODEV;
                }
        }
-
-       dev->fclk = clk_get(dev->dev, "i2c_fck");
-       if (IS_ERR(dev->fclk)) {
-               if (dev->iclk != NULL) {
-                       clk_put(dev->iclk);
-                       dev->iclk = NULL;
+       /* For I2C operations on 2430 we need 96Mhz clock */
+       if (cpu_is_omap2430()) {
+               dev->fclk = clk_get(dev->dev, "i2chs_fck");
+               if (IS_ERR(dev->fclk)) {
+                       if (dev->iclk != NULL) {
+                               clk_put(dev->iclk);
+                               dev->iclk = NULL;
+                       }
+                       dev->fclk = NULL;
+                       return -ENODEV;
+               }
+       } else {
+               dev->fclk = clk_get(dev->dev, "i2c_fck");
+               if (IS_ERR(dev->fclk)) {
+                       if (dev->iclk != NULL) {
+                               clk_put(dev->iclk);
+                               dev->iclk = NULL;
+                       }
+                       dev->fclk = NULL;
+                       return -ENODEV;
                }
-               dev->fclk = NULL;
-               return -ENODEV;
        }
-
        return 0;
 }
 
@@ -190,9 +218,11 @@ static void omap_i2c_disable_clocks(struct omap_i2c_dev *dev)
 
 static int omap_i2c_init(struct omap_i2c_dev *dev)
 {
-       u16 psc = 0;
+       u16 psc = 0, scll = 0, sclh = 0;
+       u16 fsscll = 0, fssclh = 0, hsscll = 0, hssclh = 0;
        unsigned long fclk_rate = 12000000;
        unsigned long timeout;
+       unsigned long internal_clk = 0;
 
        if (!dev->rev1) {
                omap_i2c_write_reg(dev, OMAP_I2C_SYSC_REG, OMAP_I2C_SYSC_SRST);
@@ -235,27 +265,65 @@ static int omap_i2c_init(struct omap_i2c_dev *dev)
                        psc = fclk_rate / 12000000;
        }
 
+       if (cpu_is_omap2430() || cpu_is_omap34xx()) {
+
+               /* HSI2C controller internal clk rate should be 19.2 Mhz */
+               internal_clk = 19200;
+               fclk_rate = clk_get_rate(dev->fclk) / 1000;
+
+               /* Compute prescaler divisor */
+               psc = fclk_rate / internal_clk;
+               psc = psc - 1;
+
+               /* If configured for High Speed */
+               if (dev->speed > 400) {
+                       /* For first phase of HS mode */
+                       fsscll = internal_clk / (400 * 2) - 6;
+                       fssclh = internal_clk / (400 * 2) - 6;
+
+                       /* For second phase of HS mode */
+                       hsscll = fclk_rate / (dev->speed * 2) - 6;
+                       hssclh = fclk_rate / (dev->speed * 2) - 6;
+               } else {
+                       /* To handle F/S modes */
+                       fsscll = internal_clk / (dev->speed * 2) - 6;
+                       fssclh = internal_clk / (dev->speed * 2) - 6;
+               }
+               scll = (hsscll << OMAP_I2C_SCLL_HSSCLL) | fsscll;
+               sclh = (hssclh << OMAP_I2C_SCLH_HSSCLH) | fssclh;
+       } else {
+               /* Program desired operating rate */
+               fclk_rate /= (psc + 1) * 1000;
+               if (psc > 2)
+                       psc = 2;
+               scll = fclk_rate / (dev->speed * 2) - 7 + psc;
+               sclh = fclk_rate / (dev->speed * 2) - 7 + psc;
+       }
+
        /* Setup clock prescaler to obtain approx 12MHz I2C module clock: */
        omap_i2c_write_reg(dev, OMAP_I2C_PSC_REG, psc);
 
-       /* Program desired operating rate */
-       fclk_rate /= (psc + 1) * 1000;
-       if (psc > 2)
-               psc = 2;
+       /* SCL low and high time values */
+       omap_i2c_write_reg(dev, OMAP_I2C_SCLL_REG, scll);
+       omap_i2c_write_reg(dev, OMAP_I2C_SCLH_REG, sclh);
 
-       omap_i2c_write_reg(dev, OMAP_I2C_SCLL_REG,
-                          fclk_rate / (clock * 2) - 7 + psc);
-       omap_i2c_write_reg(dev, OMAP_I2C_SCLH_REG,
-                          fclk_rate / (clock * 2) - 7 + psc);
+       if (dev->fifo_size)
+               /* Note: setup required fifo size - 1 */
+               omap_i2c_write_reg(dev, OMAP_I2C_BUF_REG,
+                                       (dev->fifo_size - 1) << 8 | /* RTRSH */
+                                       OMAP_I2C_BUF_RXFIF_CLR |
+                                       (dev->fifo_size - 1) | /* XTRSH */
+                                       OMAP_I2C_BUF_TXFIF_CLR);
 
        /* Take the I2C module out of reset: */
        omap_i2c_write_reg(dev, OMAP_I2C_CON_REG, OMAP_I2C_CON_EN);
 
        /* Enable interrupts */
        omap_i2c_write_reg(dev, OMAP_I2C_IE_REG,
-                          (OMAP_I2C_IE_XRDY | OMAP_I2C_IE_RRDY |
-                           OMAP_I2C_IE_ARDY | OMAP_I2C_IE_NACK |
-                           OMAP_I2C_IE_AL));
+                       (OMAP_I2C_IE_XRDY | OMAP_I2C_IE_RRDY |
+                       OMAP_I2C_IE_ARDY | OMAP_I2C_IE_NACK |
+                       OMAP_I2C_IE_AL)  | ((dev->fifo_size) ?
+                               (OMAP_I2C_IE_RDR | OMAP_I2C_IE_XDR) : 0));
        return 0;
 }
 
@@ -285,12 +353,16 @@ static int omap_i2c_xfer_msg(struct i2c_adapter *adap,
                             struct i2c_msg *msg, int stop)
 {
        struct omap_i2c_dev *dev = i2c_get_adapdata(adap);
+#ifdef OMAP_HACK
+       u8 zero_byte = 0;
+#endif
        int r;
        u16 w;
 
        dev_dbg(dev->dev, "addr: 0x%04x, len: %d, flags: 0x%x, stop: %d\n",
                msg->addr, msg->len, msg->flags, stop);
 
+#ifndef OMAP_HACK
        if (msg->len == 0)
                return -EINVAL;
 
@@ -300,22 +372,64 @@ static int omap_i2c_xfer_msg(struct i2c_adapter *adap,
        dev->buf = msg->buf;
        dev->buf_len = msg->len;
 
+#else
+
+       omap_i2c_write_reg(dev, OMAP_I2C_SA_REG, msg->addr);
+       /* REVISIT: Remove this hack when we can get I2C chips from board-*.c
+        *          files
+        * Sigh, seems we can't do zero length transactions. Thus, we
+        * can't probe for devices w/o actually sending/receiving at least
+        * a single byte. So we'll set count to 1 for the zero length
+        * transaction case and hope we don't cause grief for some
+        * arbitrary device due to random byte write/read during
+        * probes.
+        */
+       if (msg->len == 0) {
+               dev->buf = &zero_byte;
+               dev->buf_len = 1;
+       } else {
+               dev->buf = msg->buf;
+               dev->buf_len = msg->len;
+       }
+#endif
+
        omap_i2c_write_reg(dev, OMAP_I2C_CNT_REG, dev->buf_len);
 
+       /* Clear the FIFO Buffers */
+       w = omap_i2c_read_reg(dev, OMAP_I2C_BUF_REG);
+       w |= OMAP_I2C_BUF_RXFIF_CLR | OMAP_I2C_BUF_TXFIF_CLR;
+       omap_i2c_write_reg(dev, OMAP_I2C_BUF_REG, w);
+
        init_completion(&dev->cmd_complete);
        dev->cmd_err = 0;
 
        w = OMAP_I2C_CON_EN | OMAP_I2C_CON_MST | OMAP_I2C_CON_STT;
+
+       /* High speed configuration */
+       if (dev->speed > 400)
+               w |= OMAP_I2C_CON_OPMODE_HS;
+
        if (msg->flags & I2C_M_TEN)
                w |= OMAP_I2C_CON_XA;
        if (!(msg->flags & I2C_M_RD))
                w |= OMAP_I2C_CON_TRX;
-       if (stop)
+
+       if (!dev->b_hw && stop)
                w |= OMAP_I2C_CON_STP;
+
        omap_i2c_write_reg(dev, OMAP_I2C_CON_REG, w);
 
-       r = wait_for_completion_interruptible_timeout(&dev->cmd_complete,
-                                                     OMAP_I2C_TIMEOUT);
+       if (dev->b_hw && stop) {
+               /* H/w behavior: dont write stt and stp together.. */
+               while (omap_i2c_read_reg(dev, OMAP_I2C_CON_REG) & OMAP_I2C_CON_STT) {
+                       /* Dont do anything - this will come in a couple of loops at max*/
+               }
+               w |= OMAP_I2C_CON_STP;
+               w &= ~OMAP_I2C_CON_STT;
+               omap_i2c_write_reg(dev, OMAP_I2C_CON_REG, w);
+       }
+       r = wait_for_completion_timeout(&dev->cmd_complete,
+                                       OMAP_I2C_TIMEOUT);
        dev->buf_len = 0;
        if (r < 0)
                return r;
@@ -381,7 +495,11 @@ out:
 static u32
 omap_i2c_func(struct i2c_adapter *adap)
 {
+#ifndef OMAP_HACK
        return I2C_FUNC_I2C | (I2C_FUNC_SMBUS_EMUL & ~I2C_FUNC_SMBUS_QUICK);
+#else
+       return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
+#endif
 }
 
 static inline void
@@ -471,35 +589,71 @@ omap_i2c_isr(int this_irq, void *dev_id)
                        omap_i2c_complete_cmd(dev, 0);
                        continue;
                }
-               if (stat & OMAP_I2C_STAT_RRDY) {
-                       w = omap_i2c_read_reg(dev, OMAP_I2C_DATA_REG);
-                       if (dev->buf_len) {
-                               *dev->buf++ = w;
-                               dev->buf_len--;
+               if (stat & (OMAP_I2C_STAT_RRDY | OMAP_I2C_STAT_RDR)) {
+                       u8 num_bytes = 1;
+                       if (dev->fifo_size) {
+                               num_bytes = (stat & OMAP_I2C_STAT_RRDY) ? dev->fifo_size :
+                                               omap_i2c_read_reg(dev, OMAP_I2C_BUFSTAT_REG);
+                       }
+                       while (num_bytes) {
+                               num_bytes--;
+                               w = omap_i2c_read_reg(dev, OMAP_I2C_DATA_REG);
                                if (dev->buf_len) {
-                                       *dev->buf++ = w >> 8;
+                                       *dev->buf++ = w;
                                        dev->buf_len--;
+                                       /* Data reg from 2430 is 8 bit wide */
+                                       if (!cpu_is_omap2430() &&
+                                                       !cpu_is_omap34xx()) {
+                                               if (dev->buf_len) {
+                                                       *dev->buf++ = w >> 8;
+                                                       dev->buf_len--;
+                                               }
+                                       }
+                               } else {
+                                       if (stat & OMAP_I2C_STAT_RRDY)
+                                               dev_err(dev->dev, "RRDY IRQ while no data "
+                                                               "requested\n");
+                                       if (stat & OMAP_I2C_STAT_RDR)
+                                               dev_err(dev->dev, "RDR IRQ while no data "
+                                                               "requested\n");
+                                       break;
                                }
-                       } else
-                               dev_err(dev->dev, "RRDY IRQ while no data "
-                                               "requested\n");
-                       omap_i2c_ack_stat(dev, OMAP_I2C_STAT_RRDY);
+                       }
+                       omap_i2c_ack_stat(dev, stat & (OMAP_I2C_STAT_RRDY | OMAP_I2C_STAT_RDR));
                        continue;
                }
-               if (stat & OMAP_I2C_STAT_XRDY) {
-                       w = 0;
-                       if (dev->buf_len) {
-                               w = *dev->buf++;
-                               dev->buf_len--;
+               if (stat & (OMAP_I2C_STAT_XRDY | OMAP_I2C_STAT_XDR)) {
+                       u8 num_bytes = 1;
+                       if (dev->fifo_size) {
+                               num_bytes = (stat & OMAP_I2C_STAT_XRDY) ? dev->fifo_size :
+                                               omap_i2c_read_reg(dev, OMAP_I2C_BUFSTAT_REG);
+                       }
+                       while (num_bytes) {
+                               num_bytes--;
+                               w = 0;
                                if (dev->buf_len) {
-                                       w |= *dev->buf++ << 8;
+                                       w = *dev->buf++;
                                        dev->buf_len--;
+                                       /* Data reg from  2430 is 8 bit wide */
+                                       if (!cpu_is_omap2430() &&
+                                                       !cpu_is_omap34xx()) {
+                                               if (dev->buf_len) {
+                                                       w |= *dev->buf++ << 8;
+                                                       dev->buf_len--;
+                                               }
+                                       }
+                               } else {
+                                       if (stat & OMAP_I2C_STAT_XRDY)
+                                               dev_err(dev->dev, "XRDY IRQ while no "
+                                                               "data to send\n");
+                                       if (stat & OMAP_I2C_STAT_XDR)
+                                               dev_err(dev->dev, "XDR IRQ while no "
+                                                               "data to send\n");
+                                       break;
                                }
-                       } else
-                               dev_err(dev->dev, "XRDY IRQ while no "
-                                       "data to send\n");
-                       omap_i2c_write_reg(dev, OMAP_I2C_DATA_REG, w);
-                       omap_i2c_ack_stat(dev, OMAP_I2C_STAT_XRDY);
+                               omap_i2c_write_reg(dev, OMAP_I2C_DATA_REG, w);
+                       }
+                       omap_i2c_ack_stat(dev, stat & (OMAP_I2C_STAT_XRDY | OMAP_I2C_STAT_XDR));
                        continue;
                }
                if (stat & OMAP_I2C_STAT_ROVR) {
@@ -536,6 +690,7 @@ omap_i2c_probe(struct platform_device *pdev)
        struct i2c_adapter      *adap;
        struct resource         *mem, *irq, *ioarea;
        int r;
+       u32 *speed = NULL;
 
        /* NOTE: driver uses the static register mapping */
        mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
@@ -556,17 +711,18 @@ omap_i2c_probe(struct platform_device *pdev)
                return -EBUSY;
        }
 
-       if (clock > 200)
-               clock = 400;    /* Fast mode */
-       else
-               clock = 100;    /* Standard mode */
-
        dev = kzalloc(sizeof(struct omap_i2c_dev), GFP_KERNEL);
        if (!dev) {
                r = -ENOMEM;
                goto err_release_region;
        }
 
+       if (pdev->dev.platform_data != NULL)
+               speed = (u32 *) pdev->dev.platform_data;
+       else
+               *speed = 100; /* Defualt speed */
+
+       dev->speed = *speed;
        dev->dev = &pdev->dev;
        dev->irq = irq->start;
        dev->base = (void __iomem *) IO_ADDRESS(mem->start);
@@ -580,6 +736,19 @@ omap_i2c_probe(struct platform_device *pdev)
        if (cpu_is_omap15xx())
                dev->rev1 = omap_i2c_read_reg(dev, OMAP_I2C_REV_REG) < 0x20;
 
+       if (cpu_is_omap2430() || cpu_is_omap34xx()) {
+               /* Set up the fifo size - Get total size */
+               dev->fifo_size = 0x8 <<
+                       ((omap_i2c_read_reg(dev, OMAP_I2C_BUFSTAT_REG) >> 14) & 0x3);
+               /*
+                * Set up notification threshold as half the total available size
+                * This is to ensure that we can handle the status on int call back
+                * latencies
+                */
+               dev->fifo_size = (dev->fifo_size / 2);
+               dev->b_hw = 1; /* Enable hardware fixes */
+       }
+
        /* reset ASAP, clearing any IRQs */
        omap_i2c_init(dev);
 
@@ -592,7 +761,7 @@ omap_i2c_probe(struct platform_device *pdev)
        }
        r = omap_i2c_read_reg(dev, OMAP_I2C_REV_REG) & 0xff;
        dev_info(dev->dev, "bus %d rev%d.%d at %d kHz\n",
-                pdev->id, r >> 4, r & 0xf, clock);
+                pdev->id, r >> 4, r & 0xf, dev->speed);
 
        adap = &dev->adapter;
        i2c_set_adapdata(adap, dev);