Merge remote-tracking branches 'spi/topic/omap-uwire', 'spi/topic/omap100k', 'spi...
[cascardo/linux.git] / drivers / spi / spi-omap2-mcspi.c
index a72127f..2941c5b 100644 (file)
@@ -22,7 +22,6 @@
  */
 
 #include <linux/kernel.h>
-#include <linux/init.h>
 #include <linux/interrupt.h>
 #include <linux/module.h>
 #include <linux/device.h>
@@ -45,6 +44,7 @@
 #include <linux/platform_data/spi-omap2-mcspi.h>
 
 #define OMAP2_MCSPI_MAX_FREQ           48000000
+#define OMAP2_MCSPI_MAX_DIVIDER                4096
 #define OMAP2_MCSPI_MAX_FIFODEPTH      64
 #define OMAP2_MCSPI_MAX_FIFOWCNT       0xFFFF
 #define SPI_AUTOSUSPEND_TIMEOUT                2000
@@ -89,6 +89,7 @@
 #define OMAP2_MCSPI_CHCONF_FORCE       BIT(20)
 #define OMAP2_MCSPI_CHCONF_FFET                BIT(27)
 #define OMAP2_MCSPI_CHCONF_FFER                BIT(28)
+#define OMAP2_MCSPI_CHCONF_CLKG                BIT(29)
 
 #define OMAP2_MCSPI_CHSTAT_RXS         BIT(0)
 #define OMAP2_MCSPI_CHSTAT_TXS         BIT(1)
@@ -96,6 +97,7 @@
 #define OMAP2_MCSPI_CHSTAT_TXFFE       BIT(3)
 
 #define OMAP2_MCSPI_CHCTRL_EN          BIT(0)
+#define OMAP2_MCSPI_CHCTRL_EXTCLK_MASK (0xff << 8)
 
 #define OMAP2_MCSPI_WAKEUPENABLE_WKEN  BIT(0)
 
@@ -149,7 +151,7 @@ struct omap2_mcspi_cs {
        int                     word_len;
        struct list_head        node;
        /* Context save and restore shadow register */
-       u32                     chconf0;
+       u32                     chconf0, chctrl0;
 };
 
 static inline void mcspi_write_reg(struct spi_master *master,
@@ -230,10 +232,16 @@ static void omap2_mcspi_set_dma_req(const struct spi_device *spi,
 
 static void omap2_mcspi_set_enable(const struct spi_device *spi, int enable)
 {
+       struct omap2_mcspi_cs *cs = spi->controller_state;
        u32 l;
 
-       l = enable ? OMAP2_MCSPI_CHCTRL_EN : 0;
-       mcspi_write_cs_reg(spi, OMAP2_MCSPI_CHCTRL0, l);
+       l = cs->chctrl0;
+       if (enable)
+               l |= OMAP2_MCSPI_CHCTRL_EN;
+       else
+               l &= ~OMAP2_MCSPI_CHCTRL_EN;
+       cs->chctrl0 = l;
+       mcspi_write_cs_reg(spi, OMAP2_MCSPI_CHCTRL0, cs->chctrl0);
        /* Flash post-writes */
        mcspi_read_cs_reg(spi, OMAP2_MCSPI_CHCTRL0);
 }
@@ -840,7 +848,7 @@ static int omap2_mcspi_setup_transfer(struct spi_device *spi,
        struct omap2_mcspi_cs *cs = spi->controller_state;
        struct omap2_mcspi *mcspi;
        struct spi_master *spi_cntrl;
-       u32 l = 0, div = 0;
+       u32 l = 0, clkd = 0, div, extclk = 0, clkg = 0;
        u8 word_len = spi->bits_per_word;
        u32 speed_hz = spi->max_speed_hz;
 
@@ -856,7 +864,17 @@ static int omap2_mcspi_setup_transfer(struct spi_device *spi,
                speed_hz = t->speed_hz;
 
        speed_hz = min_t(u32, speed_hz, OMAP2_MCSPI_MAX_FREQ);
-       div = omap2_mcspi_calc_divisor(speed_hz);
+       if (speed_hz < (OMAP2_MCSPI_MAX_FREQ / OMAP2_MCSPI_MAX_DIVIDER)) {
+               clkd = omap2_mcspi_calc_divisor(speed_hz);
+               speed_hz = OMAP2_MCSPI_MAX_FREQ >> clkd;
+               clkg = 0;
+       } else {
+               div = (OMAP2_MCSPI_MAX_FREQ + speed_hz - 1) / speed_hz;
+               speed_hz = OMAP2_MCSPI_MAX_FREQ / div;
+               clkd = (div - 1) & 0xf;
+               extclk = (div - 1) >> 4;
+               clkg = OMAP2_MCSPI_CHCONF_CLKG;
+       }
 
        l = mcspi_cached_chconf0(spi);
 
@@ -885,7 +903,16 @@ static int omap2_mcspi_setup_transfer(struct spi_device *spi,
 
        /* set clock divisor */
        l &= ~OMAP2_MCSPI_CHCONF_CLKD_MASK;
-       l |= div << 2;
+       l |= clkd << 2;
+
+       /* set clock granularity */
+       l &= ~OMAP2_MCSPI_CHCONF_CLKG;
+       l |= clkg;
+       if (clkg) {
+               cs->chctrl0 &= ~OMAP2_MCSPI_CHCTRL_EXTCLK_MASK;
+               cs->chctrl0 |= extclk << 8;
+               mcspi_write_cs_reg(spi, OMAP2_MCSPI_CHCTRL0, cs->chctrl0);
+       }
 
        /* set SPI mode 0..3 */
        if (spi->mode & SPI_CPOL)
@@ -900,7 +927,7 @@ static int omap2_mcspi_setup_transfer(struct spi_device *spi,
        mcspi_write_chconf0(spi, l);
 
        dev_dbg(&spi->dev, "setup: speed %d, sample %s edge, clk %s\n",
-                       OMAP2_MCSPI_MAX_FREQ >> div,
+                       speed_hz,
                        (spi->mode & SPI_CPHA) ? "trailing" : "leading",
                        (spi->mode & SPI_CPOL) ? "inverted" : "normal");
 
@@ -972,6 +999,7 @@ static int omap2_mcspi_setup(struct spi_device *spi)
                cs->base = mcspi->base + spi->chip_select * 0x14;
                cs->phys = mcspi->phys + spi->chip_select * 0x14;
                cs->chconf0 = 0;
+               cs->chctrl0 = 0;
                spi->controller_state = cs;
                /* Link this to context save list */
                list_add_tail(&cs->node, &ctx->cs);
@@ -1057,12 +1085,15 @@ static void omap2_mcspi_work(struct omap2_mcspi *mcspi, struct spi_message *m)
                        status = -EINVAL;
                        break;
                }
-               if (par_override || t->speed_hz || t->bits_per_word) {
+               if (par_override ||
+                   (t->speed_hz != spi->max_speed_hz) ||
+                   (t->bits_per_word != spi->bits_per_word)) {
                        par_override = 1;
                        status = omap2_mcspi_setup_transfer(spi, t);
                        if (status < 0)
                                break;
-                       if (!t->speed_hz && !t->bits_per_word)
+                       if (t->speed_hz == spi->max_speed_hz &&
+                           t->bits_per_word == spi->bits_per_word)
                                par_override = 0;
                }
                if (cd && cd->cs_per_word) {
@@ -1176,16 +1207,12 @@ static int omap2_mcspi_transfer_one_message(struct spi_master *master,
        m->actual_length = 0;
        m->status = 0;
 
-       /* reject invalid messages and transfers */
-       if (list_empty(&m->transfers))
-               return -EINVAL;
        list_for_each_entry(t, &m->transfers, transfer_list) {
                const void      *tx_buf = t->tx_buf;
                void            *rx_buf = t->rx_buf;
                unsigned        len = t->len;
 
-               if (t->speed_hz > OMAP2_MCSPI_MAX_FREQ
-                               || (len && !(rx_buf || tx_buf))) {
+               if ((len && !(rx_buf || tx_buf))) {
                        dev_dbg(mcspi->dev, "transfer: %d Hz, %d %s%s, %d bpw\n",
                                        t->speed_hz,
                                        len,
@@ -1194,12 +1221,6 @@ static int omap2_mcspi_transfer_one_message(struct spi_master *master,
                                        t->bits_per_word);
                        return -EINVAL;
                }
-               if (t->speed_hz && t->speed_hz < (OMAP2_MCSPI_MAX_FREQ >> 15)) {
-                       dev_dbg(mcspi->dev, "speed_hz %d below minimum %d Hz\n",
-                                       t->speed_hz,
-                                       OMAP2_MCSPI_MAX_FREQ >> 15);
-                       return -EINVAL;
-               }
 
                if (m->is_dma_mapped || len < DMA_MIN_BYTES)
                        continue;
@@ -1311,6 +1332,8 @@ static int omap2_mcspi_probe(struct platform_device *pdev)
        master->transfer_one_message = omap2_mcspi_transfer_one_message;
        master->cleanup = omap2_mcspi_cleanup;
        master->dev.of_node = node;
+       master->max_speed_hz = OMAP2_MCSPI_MAX_FREQ;
+       master->min_speed_hz = OMAP2_MCSPI_MAX_FREQ >> 15;
 
        platform_set_drvdata(pdev, master);