mmc: atmel-mci: add runtime pm support

Add runtime pm support to atmel mci controller.
Use runtime pm APIs to enable/disable atmel mci's clock.
Use runtime autosuspend APIs to enable auto suspend delay.

Signed-off-by: Wenyou Yang <wenyou.yang@atmel.com>
Acked-by: Ludovic Desroches <ludovic.desroches@atmel.com>
Signed-off-by: Ulf Hansson <ulf.hansson@linaro.org>
[Ulf: Fixed compile error]
This commit is contained in:
Wenyou Yang 2014-10-30 12:00:41 +08:00 committed by Ulf Hansson
parent 3ee14bf79d
commit ae552ab02c

View file

@ -37,6 +37,8 @@
#include <linux/atmel-mci.h> #include <linux/atmel-mci.h>
#include <linux/atmel_pdc.h> #include <linux/atmel_pdc.h>
#include <linux/pm.h>
#include <linux/pm_runtime.h>
#include <asm/cacheflush.h> #include <asm/cacheflush.h>
#include <asm/io.h> #include <asm/io.h>
@ -44,6 +46,8 @@
#include "atmel-mci-regs.h" #include "atmel-mci-regs.h"
#define AUTOSUSPEND_DELAY 50
#define ATMCI_DATA_ERROR_FLAGS (ATMCI_DCRCE | ATMCI_DTOE | ATMCI_OVRE | ATMCI_UNRE) #define ATMCI_DATA_ERROR_FLAGS (ATMCI_DCRCE | ATMCI_DTOE | ATMCI_OVRE | ATMCI_UNRE)
#define ATMCI_DMA_THRESHOLD 16 #define ATMCI_DMA_THRESHOLD 16
@ -386,20 +390,19 @@ static int atmci_regs_show(struct seq_file *s, void *v)
if (!buf) if (!buf)
return -ENOMEM; return -ENOMEM;
pm_runtime_get_sync(&host->pdev->dev);
/* /*
* Grab a more or less consistent snapshot. Note that we're * Grab a more or less consistent snapshot. Note that we're
* not disabling interrupts, so IMR and SR may not be * not disabling interrupts, so IMR and SR may not be
* consistent. * consistent.
*/ */
ret = clk_prepare_enable(host->mck);
if (ret)
goto out;
spin_lock_bh(&host->lock); spin_lock_bh(&host->lock);
memcpy_fromio(buf, host->regs, ATMCI_REGS_SIZE); memcpy_fromio(buf, host->regs, ATMCI_REGS_SIZE);
spin_unlock_bh(&host->lock); spin_unlock_bh(&host->lock);
clk_disable_unprepare(host->mck); pm_runtime_mark_last_busy(&host->pdev->dev);
pm_runtime_put_autosuspend(&host->pdev->dev);
seq_printf(s, "MR:\t0x%08x%s%s ", seq_printf(s, "MR:\t0x%08x%s%s ",
buf[ATMCI_MR / 4], buf[ATMCI_MR / 4],
@ -449,7 +452,6 @@ static int atmci_regs_show(struct seq_file *s, void *v)
val & ATMCI_CFG_LSYNC ? " LSYNC" : ""); val & ATMCI_CFG_LSYNC ? " LSYNC" : "");
} }
out:
kfree(buf); kfree(buf);
return ret; return ret;
@ -1255,6 +1257,8 @@ static void atmci_request(struct mmc_host *mmc, struct mmc_request *mrq)
WARN_ON(slot->mrq); WARN_ON(slot->mrq);
dev_dbg(&host->pdev->dev, "MRQ: cmd %u\n", mrq->cmd->opcode); dev_dbg(&host->pdev->dev, "MRQ: cmd %u\n", mrq->cmd->opcode);
pm_runtime_get_sync(&host->pdev->dev);
/* /*
* We may "know" the card is gone even though there's still an * We may "know" the card is gone even though there's still an
* electrical connection. If so, we really need to communicate * electrical connection. If so, we really need to communicate
@ -1284,7 +1288,8 @@ static void atmci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
struct atmel_mci_slot *slot = mmc_priv(mmc); struct atmel_mci_slot *slot = mmc_priv(mmc);
struct atmel_mci *host = slot->host; struct atmel_mci *host = slot->host;
unsigned int i; unsigned int i;
bool unprepare_clk;
pm_runtime_get_sync(&host->pdev->dev);
slot->sdc_reg &= ~ATMCI_SDCBUS_MASK; slot->sdc_reg &= ~ATMCI_SDCBUS_MASK;
switch (ios->bus_width) { switch (ios->bus_width) {
@ -1300,13 +1305,8 @@ static void atmci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
unsigned int clock_min = ~0U; unsigned int clock_min = ~0U;
u32 clkdiv; u32 clkdiv;
clk_prepare(host->mck);
unprepare_clk = true;
spin_lock_bh(&host->lock); spin_lock_bh(&host->lock);
if (!host->mode_reg) { if (!host->mode_reg) {
clk_enable(host->mck);
unprepare_clk = false;
atmci_writel(host, ATMCI_CR, ATMCI_CR_SWRST); atmci_writel(host, ATMCI_CR, ATMCI_CR_SWRST);
atmci_writel(host, ATMCI_CR, ATMCI_CR_MCIEN); atmci_writel(host, ATMCI_CR, ATMCI_CR_MCIEN);
if (host->caps.has_cfg_reg) if (host->caps.has_cfg_reg)
@ -1374,8 +1374,6 @@ static void atmci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
} else { } else {
bool any_slot_active = false; bool any_slot_active = false;
unprepare_clk = false;
spin_lock_bh(&host->lock); spin_lock_bh(&host->lock);
slot->clock = 0; slot->clock = 0;
for (i = 0; i < ATMCI_MAX_NR_SLOTS; i++) { for (i = 0; i < ATMCI_MAX_NR_SLOTS; i++) {
@ -1388,17 +1386,12 @@ static void atmci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
atmci_writel(host, ATMCI_CR, ATMCI_CR_MCIDIS); atmci_writel(host, ATMCI_CR, ATMCI_CR_MCIDIS);
if (host->mode_reg) { if (host->mode_reg) {
atmci_readl(host, ATMCI_MR); atmci_readl(host, ATMCI_MR);
clk_disable(host->mck);
unprepare_clk = true;
} }
host->mode_reg = 0; host->mode_reg = 0;
} }
spin_unlock_bh(&host->lock); spin_unlock_bh(&host->lock);
} }
if (unprepare_clk)
clk_unprepare(host->mck);
switch (ios->power_mode) { switch (ios->power_mode) {
case MMC_POWER_OFF: case MMC_POWER_OFF:
if (!IS_ERR(mmc->supply.vmmc)) if (!IS_ERR(mmc->supply.vmmc))
@ -1424,6 +1417,9 @@ static void atmci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
*/ */
break; break;
} }
pm_runtime_mark_last_busy(&host->pdev->dev);
pm_runtime_put_autosuspend(&host->pdev->dev);
} }
static int atmci_get_ro(struct mmc_host *mmc) static int atmci_get_ro(struct mmc_host *mmc)
@ -1515,6 +1511,9 @@ static void atmci_request_end(struct atmel_mci *host, struct mmc_request *mrq)
spin_unlock(&host->lock); spin_unlock(&host->lock);
mmc_request_done(prev_mmc, mrq); mmc_request_done(prev_mmc, mrq);
spin_lock(&host->lock); spin_lock(&host->lock);
pm_runtime_mark_last_busy(&host->pdev->dev);
pm_runtime_put_autosuspend(&host->pdev->dev);
} }
static void atmci_command_complete(struct atmel_mci *host, static void atmci_command_complete(struct atmel_mci *host,
@ -2424,15 +2423,16 @@ static int __init atmci_probe(struct platform_device *pdev)
atmci_writel(host, ATMCI_CR, ATMCI_CR_SWRST); atmci_writel(host, ATMCI_CR, ATMCI_CR_SWRST);
host->bus_hz = clk_get_rate(host->mck); host->bus_hz = clk_get_rate(host->mck);
clk_disable_unprepare(host->mck);
host->mapbase = regs->start; host->mapbase = regs->start;
tasklet_init(&host->tasklet, atmci_tasklet_func, (unsigned long)host); tasklet_init(&host->tasklet, atmci_tasklet_func, (unsigned long)host);
ret = request_irq(irq, atmci_interrupt, 0, dev_name(&pdev->dev), host); ret = request_irq(irq, atmci_interrupt, 0, dev_name(&pdev->dev), host);
if (ret) if (ret) {
clk_disable_unprepare(host->mck);
return ret; return ret;
}
/* Get MCI capabilities and set operations according to it */ /* Get MCI capabilities and set operations according to it */
atmci_get_cap(host); atmci_get_cap(host);
@ -2456,6 +2456,12 @@ static int __init atmci_probe(struct platform_device *pdev)
setup_timer(&host->timer, atmci_timeout_timer, (unsigned long)host); setup_timer(&host->timer, atmci_timeout_timer, (unsigned long)host);
pm_runtime_get_noresume(&pdev->dev);
pm_runtime_set_active(&pdev->dev);
pm_runtime_set_autosuspend_delay(&pdev->dev, AUTOSUSPEND_DELAY);
pm_runtime_use_autosuspend(&pdev->dev);
pm_runtime_enable(&pdev->dev);
/* We need at least one slot to succeed */ /* We need at least one slot to succeed */
nr_slots = 0; nr_slots = 0;
ret = -ENODEV; ret = -ENODEV;
@ -2498,6 +2504,9 @@ static int __init atmci_probe(struct platform_device *pdev)
"Atmel MCI controller at 0x%08lx irq %d, %u slots\n", "Atmel MCI controller at 0x%08lx irq %d, %u slots\n",
host->mapbase, irq, nr_slots); host->mapbase, irq, nr_slots);
pm_runtime_mark_last_busy(&host->pdev->dev);
pm_runtime_put_autosuspend(&pdev->dev);
return 0; return 0;
err_dma_alloc: err_dma_alloc:
@ -2506,6 +2515,11 @@ static int __init atmci_probe(struct platform_device *pdev)
atmci_cleanup_slot(host->slot[i], i); atmci_cleanup_slot(host->slot[i], i);
} }
err_init_slot: err_init_slot:
clk_disable_unprepare(host->mck);
pm_runtime_disable(&pdev->dev);
pm_runtime_put_noidle(&pdev->dev);
del_timer_sync(&host->timer); del_timer_sync(&host->timer);
if (host->dma.chan) if (host->dma.chan)
dma_release_channel(host->dma.chan); dma_release_channel(host->dma.chan);
@ -2518,6 +2532,8 @@ static int __exit atmci_remove(struct platform_device *pdev)
struct atmel_mci *host = platform_get_drvdata(pdev); struct atmel_mci *host = platform_get_drvdata(pdev);
unsigned int i; unsigned int i;
pm_runtime_get_sync(&pdev->dev);
if (host->buffer) if (host->buffer)
dma_free_coherent(&pdev->dev, host->buf_size, dma_free_coherent(&pdev->dev, host->buf_size,
host->buffer, host->buf_phys_addr); host->buffer, host->buf_phys_addr);
@ -2527,11 +2543,9 @@ static int __exit atmci_remove(struct platform_device *pdev)
atmci_cleanup_slot(host->slot[i], i); atmci_cleanup_slot(host->slot[i], i);
} }
clk_prepare_enable(host->mck);
atmci_writel(host, ATMCI_IDR, ~0UL); atmci_writel(host, ATMCI_IDR, ~0UL);
atmci_writel(host, ATMCI_CR, ATMCI_CR_MCIDIS); atmci_writel(host, ATMCI_CR, ATMCI_CR_MCIDIS);
atmci_readl(host, ATMCI_SR); atmci_readl(host, ATMCI_SR);
clk_disable_unprepare(host->mck);
del_timer_sync(&host->timer); del_timer_sync(&host->timer);
if (host->dma.chan) if (host->dma.chan)
@ -2539,14 +2553,44 @@ static int __exit atmci_remove(struct platform_device *pdev)
free_irq(platform_get_irq(pdev, 0), host); free_irq(platform_get_irq(pdev, 0), host);
clk_disable_unprepare(host->mck);
pm_runtime_disable(&pdev->dev);
pm_runtime_put_noidle(&pdev->dev);
return 0; return 0;
} }
#ifdef CONFIG_PM
static int atmci_runtime_suspend(struct device *dev)
{
struct atmel_mci *host = dev_get_drvdata(dev);
clk_disable_unprepare(host->mck);
return 0;
}
static int atmci_runtime_resume(struct device *dev)
{
struct atmel_mci *host = dev_get_drvdata(dev);
return clk_prepare_enable(host->mck);
}
#endif
static const struct dev_pm_ops atmci_dev_pm_ops = {
SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
pm_runtime_force_resume)
SET_PM_RUNTIME_PM_OPS(atmci_runtime_suspend, atmci_runtime_resume, NULL)
};
static struct platform_driver atmci_driver = { static struct platform_driver atmci_driver = {
.remove = __exit_p(atmci_remove), .remove = __exit_p(atmci_remove),
.driver = { .driver = {
.name = "atmel_mci", .name = "atmel_mci",
.of_match_table = of_match_ptr(atmci_dt_ids), .of_match_table = of_match_ptr(atmci_dt_ids),
.pm = &atmci_dev_pm_ops,
}, },
}; };