2010-05-27 05:42:10 +08:00
|
|
|
/*
|
|
|
|
* drivers/mmc/host/sdhci-spear.c
|
|
|
|
*
|
|
|
|
* Support of SDHCI platform devices for spear soc family
|
|
|
|
*
|
|
|
|
* Copyright (C) 2010 ST Microelectronics
|
|
|
|
* Viresh Kumar<viresh.kumar@st.com>
|
|
|
|
*
|
|
|
|
* Inspired by sdhci-pltfm.c
|
|
|
|
*
|
|
|
|
* This file is licensed under the terms of the GNU General Public
|
|
|
|
* License version 2. This program is licensed "as is" without any
|
|
|
|
* warranty of any kind, whether express or implied.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <linux/clk.h>
|
|
|
|
#include <linux/delay.h>
|
|
|
|
#include <linux/gpio.h>
|
|
|
|
#include <linux/highmem.h>
|
2011-07-04 03:15:51 +08:00
|
|
|
#include <linux/module.h>
|
2010-05-27 05:42:10 +08:00
|
|
|
#include <linux/interrupt.h>
|
|
|
|
#include <linux/irq.h>
|
|
|
|
#include <linux/platform_device.h>
|
2011-11-15 18:54:40 +08:00
|
|
|
#include <linux/pm.h>
|
2010-05-27 05:42:10 +08:00
|
|
|
#include <linux/slab.h>
|
|
|
|
#include <linux/mmc/host.h>
|
|
|
|
#include <linux/mmc/sdhci-spear.h>
|
|
|
|
#include <linux/io.h>
|
|
|
|
#include "sdhci.h"
|
|
|
|
|
|
|
|
struct spear_sdhci {
|
|
|
|
struct clk *clk;
|
|
|
|
struct sdhci_plat_data *data;
|
|
|
|
};
|
|
|
|
|
|
|
|
/* sdhci ops */
|
|
|
|
static struct sdhci_ops sdhci_pltfm_ops = {
|
|
|
|
/* Nothing to do for now. */
|
|
|
|
};
|
|
|
|
|
|
|
|
/* gpio card detection interrupt handler */
|
|
|
|
static irqreturn_t sdhci_gpio_irq(int irq, void *dev_id)
|
|
|
|
{
|
|
|
|
struct platform_device *pdev = dev_id;
|
|
|
|
struct sdhci_host *host = platform_get_drvdata(pdev);
|
|
|
|
struct spear_sdhci *sdhci = dev_get_platdata(&pdev->dev);
|
|
|
|
unsigned long gpio_irq_type;
|
|
|
|
int val;
|
|
|
|
|
|
|
|
val = gpio_get_value(sdhci->data->card_int_gpio);
|
|
|
|
|
|
|
|
/* val == 1 -> card removed, val == 0 -> card inserted */
|
|
|
|
/* if card removed - set irq for low level, else vice versa */
|
|
|
|
gpio_irq_type = val ? IRQF_TRIGGER_LOW : IRQF_TRIGGER_HIGH;
|
2011-03-28 23:49:12 +08:00
|
|
|
irq_set_irq_type(irq, gpio_irq_type);
|
2010-05-27 05:42:10 +08:00
|
|
|
|
|
|
|
if (sdhci->data->card_power_gpio >= 0) {
|
|
|
|
if (!sdhci->data->power_always_enb) {
|
|
|
|
/* if card inserted, give power, otherwise remove it */
|
|
|
|
val = sdhci->data->power_active_high ? !val : val ;
|
|
|
|
gpio_set_value(sdhci->data->card_power_gpio, val);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* inform sdhci driver about card insertion/removal */
|
|
|
|
tasklet_schedule(&host->card_tasklet);
|
|
|
|
|
|
|
|
return IRQ_HANDLED;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int __devinit sdhci_probe(struct platform_device *pdev)
|
|
|
|
{
|
|
|
|
struct sdhci_host *host;
|
|
|
|
struct resource *iomem;
|
|
|
|
struct spear_sdhci *sdhci;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
iomem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
|
|
|
|
if (!iomem) {
|
|
|
|
ret = -ENOMEM;
|
|
|
|
dev_dbg(&pdev->dev, "memory resource not defined\n");
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
2012-03-27 11:10:35 +08:00
|
|
|
if (!devm_request_mem_region(&pdev->dev, iomem->start,
|
|
|
|
resource_size(iomem), "spear-sdhci")) {
|
2010-05-27 05:42:10 +08:00
|
|
|
ret = -EBUSY;
|
|
|
|
dev_dbg(&pdev->dev, "cannot request region\n");
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
2012-03-27 11:10:35 +08:00
|
|
|
sdhci = devm_kzalloc(&pdev->dev, sizeof(*sdhci), GFP_KERNEL);
|
2010-05-27 05:42:10 +08:00
|
|
|
if (!sdhci) {
|
|
|
|
ret = -ENOMEM;
|
|
|
|
dev_dbg(&pdev->dev, "cannot allocate memory for sdhci\n");
|
2012-03-27 11:10:35 +08:00
|
|
|
goto err;
|
2010-05-27 05:42:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* clk enable */
|
|
|
|
sdhci->clk = clk_get(&pdev->dev, NULL);
|
|
|
|
if (IS_ERR(sdhci->clk)) {
|
|
|
|
ret = PTR_ERR(sdhci->clk);
|
|
|
|
dev_dbg(&pdev->dev, "Error getting clock\n");
|
2012-03-27 11:10:35 +08:00
|
|
|
goto err;
|
2010-05-27 05:42:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
ret = clk_enable(sdhci->clk);
|
|
|
|
if (ret) {
|
|
|
|
dev_dbg(&pdev->dev, "Error enabling clock\n");
|
2012-03-27 11:10:35 +08:00
|
|
|
goto put_clk;
|
2010-05-27 05:42:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* overwrite platform_data */
|
|
|
|
sdhci->data = dev_get_platdata(&pdev->dev);
|
|
|
|
pdev->dev.platform_data = sdhci;
|
|
|
|
|
|
|
|
if (pdev->dev.parent)
|
|
|
|
host = sdhci_alloc_host(pdev->dev.parent, 0);
|
|
|
|
else
|
|
|
|
host = sdhci_alloc_host(&pdev->dev, 0);
|
|
|
|
|
|
|
|
if (IS_ERR(host)) {
|
|
|
|
ret = PTR_ERR(host);
|
|
|
|
dev_dbg(&pdev->dev, "error allocating host\n");
|
2012-03-27 11:10:35 +08:00
|
|
|
goto disable_clk;
|
2010-05-27 05:42:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
host->hw_name = "sdhci";
|
|
|
|
host->ops = &sdhci_pltfm_ops;
|
|
|
|
host->irq = platform_get_irq(pdev, 0);
|
|
|
|
host->quirks = SDHCI_QUIRK_BROKEN_ADMA;
|
|
|
|
|
2012-03-27 11:10:35 +08:00
|
|
|
host->ioaddr = devm_ioremap(&pdev->dev, iomem->start,
|
|
|
|
resource_size(iomem));
|
2010-05-27 05:42:10 +08:00
|
|
|
if (!host->ioaddr) {
|
|
|
|
ret = -ENOMEM;
|
|
|
|
dev_dbg(&pdev->dev, "failed to remap registers\n");
|
2012-03-27 11:10:35 +08:00
|
|
|
goto free_host;
|
2010-05-27 05:42:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
ret = sdhci_add_host(host);
|
|
|
|
if (ret) {
|
|
|
|
dev_dbg(&pdev->dev, "error adding host\n");
|
2012-03-27 11:10:35 +08:00
|
|
|
goto free_host;
|
2010-05-27 05:42:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
platform_set_drvdata(pdev, host);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* It is optional to use GPIOs for sdhci Power control & sdhci card
|
|
|
|
* interrupt detection. If sdhci->data is NULL, then use original sdhci
|
|
|
|
* lines otherwise GPIO lines.
|
|
|
|
* If GPIO is selected for power control, then power should be disabled
|
|
|
|
* after card removal and should be enabled when card insertion
|
|
|
|
* interrupt occurs
|
|
|
|
*/
|
|
|
|
if (!sdhci->data)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (sdhci->data->card_power_gpio >= 0) {
|
|
|
|
int val = 0;
|
|
|
|
|
2012-03-27 11:10:35 +08:00
|
|
|
ret = devm_gpio_request(&pdev->dev,
|
|
|
|
sdhci->data->card_power_gpio, "sdhci");
|
2010-05-27 05:42:10 +08:00
|
|
|
if (ret < 0) {
|
|
|
|
dev_dbg(&pdev->dev, "gpio request fail: %d\n",
|
|
|
|
sdhci->data->card_power_gpio);
|
2012-03-27 11:10:35 +08:00
|
|
|
goto set_drvdata;
|
2010-05-27 05:42:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (sdhci->data->power_always_enb)
|
|
|
|
val = sdhci->data->power_active_high;
|
|
|
|
else
|
|
|
|
val = !sdhci->data->power_active_high;
|
|
|
|
|
|
|
|
ret = gpio_direction_output(sdhci->data->card_power_gpio, val);
|
|
|
|
if (ret) {
|
|
|
|
dev_dbg(&pdev->dev, "gpio set direction fail: %d\n",
|
|
|
|
sdhci->data->card_power_gpio);
|
2012-03-27 11:10:35 +08:00
|
|
|
goto set_drvdata;
|
2010-05-27 05:42:10 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sdhci->data->card_int_gpio >= 0) {
|
2012-03-27 11:10:35 +08:00
|
|
|
ret = devm_gpio_request(&pdev->dev, sdhci->data->card_int_gpio,
|
|
|
|
"sdhci");
|
2010-05-27 05:42:10 +08:00
|
|
|
if (ret < 0) {
|
|
|
|
dev_dbg(&pdev->dev, "gpio request fail: %d\n",
|
|
|
|
sdhci->data->card_int_gpio);
|
2012-03-27 11:10:35 +08:00
|
|
|
goto set_drvdata;
|
2010-05-27 05:42:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
ret = gpio_direction_input(sdhci->data->card_int_gpio);
|
|
|
|
if (ret) {
|
|
|
|
dev_dbg(&pdev->dev, "gpio set direction fail: %d\n",
|
|
|
|
sdhci->data->card_int_gpio);
|
2012-03-27 11:10:35 +08:00
|
|
|
goto set_drvdata;
|
2010-05-27 05:42:10 +08:00
|
|
|
}
|
2012-03-27 11:10:35 +08:00
|
|
|
ret = devm_request_irq(&pdev->dev,
|
|
|
|
gpio_to_irq(sdhci->data->card_int_gpio),
|
2010-05-27 05:42:10 +08:00
|
|
|
sdhci_gpio_irq, IRQF_TRIGGER_LOW,
|
|
|
|
mmc_hostname(host->mmc), pdev);
|
|
|
|
if (ret) {
|
|
|
|
dev_dbg(&pdev->dev, "gpio request irq fail: %d\n",
|
|
|
|
sdhci->data->card_int_gpio);
|
2012-03-27 11:10:35 +08:00
|
|
|
goto set_drvdata;
|
2010-05-27 05:42:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
2012-03-27 11:10:35 +08:00
|
|
|
set_drvdata:
|
2010-05-27 05:42:10 +08:00
|
|
|
platform_set_drvdata(pdev, NULL);
|
|
|
|
sdhci_remove_host(host, 1);
|
2012-03-27 11:10:35 +08:00
|
|
|
free_host:
|
2010-05-27 05:42:10 +08:00
|
|
|
sdhci_free_host(host);
|
2012-03-27 11:10:35 +08:00
|
|
|
disable_clk:
|
2010-05-27 05:42:10 +08:00
|
|
|
clk_disable(sdhci->clk);
|
2012-03-27 11:10:35 +08:00
|
|
|
put_clk:
|
2010-05-27 05:42:10 +08:00
|
|
|
clk_put(sdhci->clk);
|
|
|
|
err:
|
|
|
|
dev_err(&pdev->dev, "spear-sdhci probe failed: %d\n", ret);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int __devexit sdhci_remove(struct platform_device *pdev)
|
|
|
|
{
|
|
|
|
struct sdhci_host *host = platform_get_drvdata(pdev);
|
|
|
|
struct spear_sdhci *sdhci = dev_get_platdata(&pdev->dev);
|
2012-03-27 11:10:35 +08:00
|
|
|
int dead = 0;
|
2010-05-27 05:42:10 +08:00
|
|
|
u32 scratch;
|
|
|
|
|
|
|
|
platform_set_drvdata(pdev, NULL);
|
|
|
|
scratch = readl(host->ioaddr + SDHCI_INT_STATUS);
|
|
|
|
if (scratch == (u32)-1)
|
|
|
|
dead = 1;
|
|
|
|
|
|
|
|
sdhci_remove_host(host, dead);
|
|
|
|
sdhci_free_host(host);
|
|
|
|
clk_disable(sdhci->clk);
|
|
|
|
clk_put(sdhci->clk);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-11-15 18:54:40 +08:00
|
|
|
#ifdef CONFIG_PM
|
|
|
|
static int sdhci_suspend(struct device *dev)
|
|
|
|
{
|
|
|
|
struct sdhci_host *host = dev_get_drvdata(dev);
|
|
|
|
struct spear_sdhci *sdhci = dev_get_platdata(dev);
|
|
|
|
int ret;
|
|
|
|
|
2012-01-04 14:18:42 +08:00
|
|
|
ret = sdhci_suspend_host(host);
|
2011-11-15 18:54:40 +08:00
|
|
|
if (!ret)
|
|
|
|
clk_disable(sdhci->clk);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int sdhci_resume(struct device *dev)
|
|
|
|
{
|
|
|
|
struct sdhci_host *host = dev_get_drvdata(dev);
|
|
|
|
struct spear_sdhci *sdhci = dev_get_platdata(dev);
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = clk_enable(sdhci->clk);
|
|
|
|
if (ret) {
|
|
|
|
dev_dbg(dev, "Resume: Error enabling clock\n");
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
return sdhci_resume_host(host);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2012-02-24 14:25:35 +08:00
|
|
|
static SIMPLE_DEV_PM_OPS(sdhci_pm_ops, sdhci_suspend, sdhci_resume);
|
|
|
|
|
2010-05-27 05:42:10 +08:00
|
|
|
static struct platform_driver sdhci_driver = {
|
|
|
|
.driver = {
|
|
|
|
.name = "sdhci",
|
|
|
|
.owner = THIS_MODULE,
|
2011-11-15 18:54:40 +08:00
|
|
|
.pm = &sdhci_pm_ops,
|
2010-05-27 05:42:10 +08:00
|
|
|
},
|
|
|
|
.probe = sdhci_probe,
|
|
|
|
.remove = __devexit_p(sdhci_remove),
|
|
|
|
};
|
|
|
|
|
2011-11-26 12:55:43 +08:00
|
|
|
module_platform_driver(sdhci_driver);
|
2010-05-27 05:42:10 +08:00
|
|
|
|
|
|
|
MODULE_DESCRIPTION("SPEAr Secure Digital Host Controller Interface driver");
|
|
|
|
MODULE_AUTHOR("Viresh Kumar <viresh.kumar@st.com>");
|
|
|
|
MODULE_LICENSE("GPL v2");
|