Merge branch 'next' of git://git.infradead.org/users/vkoul/slave-dma
[platform/adaptation/renesas_rcar/renesas_kernel.git] / drivers / mmc / host / sh_mmcif.c
index 3ffb92f..5d81427 100644 (file)
@@ -54,6 +54,8 @@
 #include <linux/mmc/mmc.h>
 #include <linux/mmc/sdio.h>
 #include <linux/mmc/sh_mmcif.h>
+#include <linux/mmc/slot-gpio.h>
+#include <linux/mod_devicetable.h>
 #include <linux/pagemap.h>
 #include <linux/platform_device.h>
 #include <linux/pm_qos.h>
@@ -211,8 +213,6 @@ struct sh_mmcif_host {
        struct mmc_host *mmc;
        struct mmc_request *mrq;
        struct platform_device *pd;
-       struct sh_dmae_slave dma_slave_tx;
-       struct sh_dmae_slave dma_slave_rx;
        struct clk *hclk;
        unsigned int clk;
        int bus_width;
@@ -371,56 +371,69 @@ static void sh_mmcif_start_dma_tx(struct sh_mmcif_host *host)
                desc, cookie);
 }
 
-static bool sh_mmcif_filter(struct dma_chan *chan, void *arg)
-{
-       dev_dbg(chan->device->dev, "%s: slave data %p\n", __func__, arg);
-       chan->private = arg;
-       return true;
-}
-
 static void sh_mmcif_request_dma(struct sh_mmcif_host *host,
                                 struct sh_mmcif_plat_data *pdata)
 {
-       struct sh_dmae_slave *tx, *rx;
+       struct resource *res = platform_get_resource(host->pd, IORESOURCE_MEM, 0);
+       struct dma_slave_config cfg;
+       dma_cap_mask_t mask;
+       int ret;
+
        host->dma_active = false;
 
+       if (!pdata)
+               return;
+
+       if (pdata->slave_id_tx <= 0 || pdata->slave_id_rx <= 0)
+               return;
+
        /* We can only either use DMA for both Tx and Rx or not use it at all */
-       if (pdata->dma) {
-               dev_warn(&host->pd->dev,
-                        "Update your platform to use embedded DMA slave IDs\n");
-               tx = &pdata->dma->chan_priv_tx;
-               rx = &pdata->dma->chan_priv_rx;
-       } else {
-               tx = &host->dma_slave_tx;
-               tx->slave_id = pdata->slave_id_tx;
-               rx = &host->dma_slave_rx;
-               rx->slave_id = pdata->slave_id_rx;
-       }
-       if (tx->slave_id > 0 && rx->slave_id > 0) {
-               dma_cap_mask_t mask;
+       dma_cap_zero(mask);
+       dma_cap_set(DMA_SLAVE, mask);
 
-               dma_cap_zero(mask);
-               dma_cap_set(DMA_SLAVE, mask);
+       host->chan_tx = dma_request_channel(mask, shdma_chan_filter,
+                                           (void *)pdata->slave_id_tx);
+       dev_dbg(&host->pd->dev, "%s: TX: got channel %p\n", __func__,
+               host->chan_tx);
 
-               host->chan_tx = dma_request_channel(mask, sh_mmcif_filter, tx);
-               dev_dbg(&host->pd->dev, "%s: TX: got channel %p\n", __func__,
-                       host->chan_tx);
+       if (!host->chan_tx)
+               return;
 
-               if (!host->chan_tx)
-                       return;
+       cfg.slave_id = pdata->slave_id_tx;
+       cfg.direction = DMA_MEM_TO_DEV;
+       cfg.dst_addr = res->start + MMCIF_CE_DATA;
+       cfg.src_addr = 0;
+       ret = dmaengine_slave_config(host->chan_tx, &cfg);
+       if (ret < 0)
+               goto ecfgtx;
 
-               host->chan_rx = dma_request_channel(mask, sh_mmcif_filter, rx);
-               dev_dbg(&host->pd->dev, "%s: RX: got channel %p\n", __func__,
-                       host->chan_rx);
+       host->chan_rx = dma_request_channel(mask, shdma_chan_filter,
+                                           (void *)pdata->slave_id_rx);
+       dev_dbg(&host->pd->dev, "%s: RX: got channel %p\n", __func__,
+               host->chan_rx);
 
-               if (!host->chan_rx) {
-                       dma_release_channel(host->chan_tx);
-                       host->chan_tx = NULL;
-                       return;
-               }
+       if (!host->chan_rx)
+               goto erqrx;
 
-               init_completion(&host->dma_complete);
-       }
+       cfg.slave_id = pdata->slave_id_rx;
+       cfg.direction = DMA_DEV_TO_MEM;
+       cfg.dst_addr = 0;
+       cfg.src_addr = res->start + MMCIF_CE_DATA;
+       ret = dmaengine_slave_config(host->chan_rx, &cfg);
+       if (ret < 0)
+               goto ecfgrx;
+
+       init_completion(&host->dma_complete);
+
+       return;
+
+ecfgrx:
+       dma_release_channel(host->chan_rx);
+       host->chan_rx = NULL;
+erqrx:
+ecfgtx:
+       dma_release_channel(host->chan_tx);
+       host->chan_tx = NULL;
 }
 
 static void sh_mmcif_release_dma(struct sh_mmcif_host *host)
@@ -444,13 +457,14 @@ static void sh_mmcif_release_dma(struct sh_mmcif_host *host)
 static void sh_mmcif_clock_control(struct sh_mmcif_host *host, unsigned int clk)
 {
        struct sh_mmcif_plat_data *p = host->pd->dev.platform_data;
+       bool sup_pclk = p ? p->sup_pclk : false;
 
        sh_mmcif_bitclr(host, MMCIF_CE_CLK_CTRL, CLK_ENABLE);
        sh_mmcif_bitclr(host, MMCIF_CE_CLK_CTRL, CLK_CLEAR);
 
        if (!clk)
                return;
-       if (p->sup_pclk && clk == host->clk)
+       if (sup_pclk && clk == host->clk)
                sh_mmcif_bitset(host, MMCIF_CE_CLK_CTRL, CLK_SUP_PCLK);
        else
                sh_mmcif_bitset(host, MMCIF_CE_CLK_CTRL, CLK_CLEAR &
@@ -923,10 +937,22 @@ static int sh_mmcif_clk_update(struct sh_mmcif_host *host)
        return ret;
 }
 
+static void sh_mmcif_set_power(struct sh_mmcif_host *host, struct mmc_ios *ios)
+{
+       struct sh_mmcif_plat_data *pd = host->pd->dev.platform_data;
+       struct mmc_host *mmc = host->mmc;
+
+       if (pd && pd->set_pwr)
+               pd->set_pwr(host->pd, ios->power_mode != MMC_POWER_OFF);
+       if (!IS_ERR(mmc->supply.vmmc))
+               /* Errors ignored... */
+               mmc_regulator_set_ocr(mmc, mmc->supply.vmmc,
+                                     ios->power_mode ? ios->vdd : 0);
+}
+
 static void sh_mmcif_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
 {
        struct sh_mmcif_host *host = mmc_priv(mmc);
-       struct sh_mmcif_plat_data *p = host->pd->dev.platform_data;
        unsigned long flags;
 
        spin_lock_irqsave(&host->lock, flags);
@@ -944,6 +970,7 @@ static void sh_mmcif_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
                        sh_mmcif_request_dma(host, host->pd->dev.platform_data);
                        host->card_present = true;
                }
+               sh_mmcif_set_power(host, ios);
        } else if (ios->power_mode == MMC_POWER_OFF || !ios->clock) {
                /* clock stop */
                sh_mmcif_clock_control(host, 0);
@@ -957,8 +984,8 @@ static void sh_mmcif_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
                        pm_runtime_put(&host->pd->dev);
                        clk_disable(host->hclk);
                        host->power = false;
-                       if (p->down_pwr && ios->power_mode == MMC_POWER_OFF)
-                               p->down_pwr(host->pd);
+                       if (ios->power_mode == MMC_POWER_OFF)
+                               sh_mmcif_set_power(host, ios);
                }
                host->state = STATE_IDLE;
                return;
@@ -966,8 +993,6 @@ static void sh_mmcif_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
 
        if (ios->clock) {
                if (!host->power) {
-                       if (p->set_pwr)
-                               p->set_pwr(host->pd, ios->power_mode);
                        sh_mmcif_clk_update(host);
                        pm_runtime_get_sync(&host->pd->dev);
                        host->power = true;
@@ -984,8 +1009,12 @@ static int sh_mmcif_get_cd(struct mmc_host *mmc)
 {
        struct sh_mmcif_host *host = mmc_priv(mmc);
        struct sh_mmcif_plat_data *p = host->pd->dev.platform_data;
+       int ret = mmc_gpio_get_cd(mmc);
 
-       if (!p->get_cd)
+       if (ret >= 0)
+               return ret;
+
+       if (!p || !p->get_cd)
                return -ENOSYS;
        else
                return p->get_cd(host->pd);
@@ -1251,6 +1280,22 @@ static void mmcif_timeout_work(struct work_struct *work)
        mmc_request_done(host->mmc, mrq);
 }
 
+static void sh_mmcif_init_ocr(struct sh_mmcif_host *host)
+{
+       struct sh_mmcif_plat_data *pd = host->pd->dev.platform_data;
+       struct mmc_host *mmc = host->mmc;
+
+       mmc_regulator_get_supply(mmc);
+
+       if (!pd)
+               return;
+
+       if (!mmc->ocr_avail)
+               mmc->ocr_avail = pd->ocr;
+       else if (pd->ocr)
+               dev_warn(mmc_dev(mmc), "Platform OCR mask is ignored\n");
+}
+
 static int __devinit sh_mmcif_probe(struct platform_device *pdev)
 {
        int ret = 0, irq[2];
@@ -1261,11 +1306,6 @@ static int __devinit sh_mmcif_probe(struct platform_device *pdev)
        void __iomem *reg;
        char clk_name[8];
 
-       if (!pd) {
-               dev_err(&pdev->dev, "sh_mmcif plat data error.\n");
-               return -ENXIO;
-       }
-
        irq[0] = platform_get_irq(pdev, 0);
        irq[1] = platform_get_irq(pdev, 1);
        if (irq[0] < 0 || irq[1] < 0) {
@@ -1298,10 +1338,10 @@ static int __devinit sh_mmcif_probe(struct platform_device *pdev)
        spin_lock_init(&host->lock);
 
        mmc->ops = &sh_mmcif_ops;
-       if (pd->ocr)
-               mmc->ocr_avail = pd->ocr;
+       sh_mmcif_init_ocr(host);
+
        mmc->caps = MMC_CAP_MMC_HIGHSPEED;
-       if (pd->caps)
+       if (pd && pd->caps)
                mmc->caps |= pd->caps;
        mmc->max_segs = 32;
        mmc->max_blk_size = 512;
@@ -1345,6 +1385,12 @@ static int __devinit sh_mmcif_probe(struct platform_device *pdev)
                goto ereqirq1;
        }
 
+       if (pd && pd->use_cd_gpio) {
+               ret = mmc_gpio_request_cd(mmc, pd->cd_gpio);
+               if (ret < 0)
+                       goto erqcd;
+       }
+
        clk_disable(host->hclk);
        ret = mmc_add_host(mmc);
        if (ret < 0)
@@ -1358,6 +1404,9 @@ static int __devinit sh_mmcif_probe(struct platform_device *pdev)
        return ret;
 
 emmcaddh:
+       if (pd && pd->use_cd_gpio)
+               mmc_gpio_free_cd(mmc);
+erqcd:
        free_irq(irq[1], host);
 ereqirq1:
        free_irq(irq[0], host);
@@ -1378,6 +1427,7 @@ ealloch:
 static int __devexit sh_mmcif_remove(struct platform_device *pdev)
 {
        struct sh_mmcif_host *host = platform_get_drvdata(pdev);
+       struct sh_mmcif_plat_data *pd = pdev->dev.platform_data;
        int irq[2];
 
        host->dying = true;
@@ -1386,6 +1436,9 @@ static int __devexit sh_mmcif_remove(struct platform_device *pdev)
 
        dev_pm_qos_hide_latency_limit(&pdev->dev);
 
+       if (pd && pd->use_cd_gpio)
+               mmc_gpio_free_cd(host->mmc);
+
        mmc_remove_host(host->mmc);
        sh_mmcif_writel(host->addr, MMCIF_CE_INT_MASK, MASK_ALL);
 
@@ -1438,6 +1491,12 @@ static int sh_mmcif_resume(struct device *dev)
 #define sh_mmcif_resume                NULL
 #endif /* CONFIG_PM */
 
+static const struct of_device_id mmcif_of_match[] = {
+       { .compatible = "renesas,sh-mmcif" },
+       { }
+};
+MODULE_DEVICE_TABLE(of, mmcif_of_match);
+
 static const struct dev_pm_ops sh_mmcif_dev_pm_ops = {
        .suspend = sh_mmcif_suspend,
        .resume = sh_mmcif_resume,
@@ -1449,6 +1508,8 @@ static struct platform_driver sh_mmcif_driver = {
        .driver         = {
                .name   = DRIVER_NAME,
                .pm     = &sh_mmcif_dev_pm_ops,
+               .owner  = THIS_MODULE,
+               .of_match_table = mmcif_of_match,
        },
 };