Merge git://git.kernel.org/pub/scm/linux/kernel/git/davem/net-2.6
[safe/jmp/linux-2.6] / drivers / spi / spi_txx9.c
index b7f4bb2..dfa024b 100644 (file)
 #include <linux/spi/spi.h>
 #include <linux/err.h>
 #include <linux/clk.h>
+#include <linux/io.h>
 #include <asm/gpio.h>
 
 
 #define SPI_FIFO_SIZE 4
+#define SPI_MAX_DIVIDER 0xff   /* Max. value for SPCR1.SER */
+#define SPI_MIN_DIVIDER 1      /* Min. value for SPCR1.SER */
 
 #define TXx9_SPMCR             0x00
 #define TXx9_SPCR0             0x04
@@ -74,7 +77,6 @@ struct txx9spi {
        struct list_head queue;
        wait_queue_head_t waitq;
        void __iomem *membase;
-       int irq;
        int baseclk;
        struct clk *clk;
        u32 max_speed_hz, min_speed_hz;
@@ -110,23 +112,17 @@ static void txx9spi_cs_func(struct spi_device *spi, struct txx9spi *c,
        ndelay(cs_delay);       /* CS Setup Time / CS Recovery Time */
 }
 
-/* the spi->mode bits understood by this driver: */
-#define MODEBITS       (SPI_CS_HIGH|SPI_CPOL|SPI_CPHA)
-
 static int txx9spi_setup(struct spi_device *spi)
 {
        struct txx9spi *c = spi_master_get_devdata(spi->master);
        u8 bits_per_word;
 
-       if (spi->mode & ~MODEBITS)
-               return -EINVAL;
-
        if (!spi->max_speed_hz
                        || spi->max_speed_hz > c->max_speed_hz
                        || spi->max_speed_hz < c->min_speed_hz)
                return -EINVAL;
 
-       bits_per_word = spi->bits_per_word ? : 8;
+       bits_per_word = spi->bits_per_word;
        if (bits_per_word != 8 && bits_per_word != 16)
                return -EINVAL;
 
@@ -199,11 +195,8 @@ static void txx9spi_work_one(struct txx9spi *c, struct spi_message *m)
 
                if (prev_speed_hz != speed_hz
                                || prev_bits_per_word != bits_per_word) {
-                       u32 n = (c->baseclk + speed_hz - 1) / speed_hz;
-                       if (n < 1)
-                               n = 1;
-                       else if (n > 0xff)
-                               n = 0xff;
+                       int n = DIV_ROUND_UP(c->baseclk, speed_hz) - 1;
+                       n = clamp(n, SPI_MIN_DIVIDER, SPI_MAX_DIVIDER);
                        /* enter config mode */
                        txx9spi_wr(c, mcr | TXx9_SPMCR_CONFIG | TXx9_SPMCR_BCLR,
                                        TXx9_SPMCR);
@@ -350,12 +343,12 @@ static int __init txx9spi_probe(struct platform_device *dev)
        struct resource *res;
        int ret = -ENODEV;
        u32 mcr;
+       int irq;
 
        master = spi_alloc_master(&dev->dev, sizeof(*c));
        if (!master)
                return ret;
        c = spi_master_get_devdata(master);
-       c->irq = -1;
        platform_set_drvdata(dev, master);
 
        INIT_WORK(&c->work, txx9spi_work);
@@ -376,39 +369,45 @@ static int __init txx9spi_probe(struct platform_device *dev)
                goto exit;
        }
        c->baseclk = clk_get_rate(c->clk);
-       c->min_speed_hz = (c->baseclk + 0xff - 1) / 0xff;
-       c->max_speed_hz = c->baseclk;
+       c->min_speed_hz = DIV_ROUND_UP(c->baseclk, SPI_MAX_DIVIDER + 1);
+       c->max_speed_hz = c->baseclk / (SPI_MIN_DIVIDER + 1);
 
        res = platform_get_resource(dev, IORESOURCE_MEM, 0);
        if (!res)
-               goto exit;
-       c->membase = ioremap(res->start, res->end - res->start + 1);
+               goto exit_busy;
+       if (!devm_request_mem_region(&dev->dev, res->start, resource_size(res),
+                                    "spi_txx9"))
+               goto exit_busy;
+       c->membase = devm_ioremap(&dev->dev, res->start, resource_size(res));
        if (!c->membase)
-               goto exit;
+               goto exit_busy;
 
        /* enter config mode */
        mcr = txx9spi_rd(c, TXx9_SPMCR);
        mcr &= ~(TXx9_SPMCR_OPMODE | TXx9_SPMCR_SPSTP | TXx9_SPMCR_BCLR);
        txx9spi_wr(c, mcr | TXx9_SPMCR_CONFIG | TXx9_SPMCR_BCLR, TXx9_SPMCR);
 
-       c->irq = platform_get_irq(dev, 0);
-       if (c->irq < 0)
-               goto exit;
-       ret = request_irq(c->irq, txx9spi_interrupt, 0, dev->name, c);
-       if (ret) {
-               c->irq = -1;
+       irq = platform_get_irq(dev, 0);
+       if (irq < 0)
+               goto exit_busy;
+       ret = devm_request_irq(&dev->dev, irq, txx9spi_interrupt, 0,
+                              "spi_txx9", c);
+       if (ret)
                goto exit;
-       }
 
-       c->workqueue = create_singlethread_workqueue(master->cdev.dev->bus_id);
+       c->workqueue = create_singlethread_workqueue(
+                               dev_name(master->dev.parent));
        if (!c->workqueue)
-               goto exit;
+               goto exit_busy;
        c->last_chipselect = -1;
 
        dev_info(&dev->dev, "at %#llx, irq %d, %dMHz\n",
-                (unsigned long long)res->start, c->irq,
+                (unsigned long long)res->start, irq,
                 (c->baseclk + 500000) / 1000000);
 
+       /* the spi->mode bits understood by this driver: */
+       master->mode_bits = SPI_CS_HIGH | SPI_CPOL | SPI_CPHA;
+
        master->bus_num = dev->id;
        master->setup = txx9spi_setup;
        master->transfer = txx9spi_transfer;
@@ -418,13 +417,11 @@ static int __init txx9spi_probe(struct platform_device *dev)
        if (ret)
                goto exit;
        return 0;
+exit_busy:
+       ret = -EBUSY;
 exit:
        if (c->workqueue)
                destroy_workqueue(c->workqueue);
-       if (c->irq >= 0)
-               free_irq(c->irq, c);
-       if (c->membase)
-               iounmap(c->membase);
        if (c->clk) {
                clk_disable(c->clk);
                clk_put(c->clk);
@@ -442,14 +439,15 @@ static int __exit txx9spi_remove(struct platform_device *dev)
        spi_unregister_master(master);
        platform_set_drvdata(dev, NULL);
        destroy_workqueue(c->workqueue);
-       free_irq(c->irq, c);
-       iounmap(c->membase);
        clk_disable(c->clk);
        clk_put(c->clk);
        spi_master_put(master);
        return 0;
 }
 
+/* work with hotplug and coldplug */
+MODULE_ALIAS("platform:spi_txx9");
+
 static struct platform_driver txx9spi_driver = {
        .remove = __exit_p(txx9spi_remove),
        .driver = {