blob: 3b775348b4702fbe9df2b1357f4067a213881cfd [file] [log] [blame]
Barry Songb3b665b2013-03-21 16:27:19 +08001/*
2 * SDHCI support for SiRF primaII and marco SoCs
3 *
4 * Copyright (c) 2011 Cambridge Silicon Radio Limited, a CSR plc group company.
5 *
6 * Licensed under GPLv2 or later.
7 */
8
9#include <linux/delay.h>
10#include <linux/device.h>
11#include <linux/mmc/host.h>
12#include <linux/module.h>
13#include <linux/of.h>
14#include <linux/of_gpio.h>
15#include <linux/mmc/slot-gpio.h>
Barry Songb3b665b2013-03-21 16:27:19 +080016#include "sdhci-pltfm.h"
17
18struct sdhci_sirf_priv {
19 struct clk *clk;
20 int gpio_cd;
21};
22
23static unsigned int sdhci_sirf_get_max_clk(struct sdhci_host *host)
24{
25 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
Arnd Bergmanne2f6aac2013-06-27 11:17:25 -040026 struct sdhci_sirf_priv *priv = sdhci_pltfm_priv(pltfm_host);
Barry Songb3b665b2013-03-21 16:27:19 +080027 return clk_get_rate(priv->clk);
28}
29
30static struct sdhci_ops sdhci_sirf_ops = {
Russell King17710592014-04-25 12:58:55 +010031 .set_clock = sdhci_set_clock,
Barry Songb3b665b2013-03-21 16:27:19 +080032 .get_max_clock = sdhci_sirf_get_max_clk,
Russell King2317f562014-04-25 12:57:07 +010033 .set_bus_width = sdhci_set_bus_width,
Russell King03231f92014-04-25 12:57:12 +010034 .reset = sdhci_reset,
Barry Songb3b665b2013-03-21 16:27:19 +080035};
36
37static struct sdhci_pltfm_data sdhci_sirf_pdata = {
38 .ops = &sdhci_sirf_ops,
39 .quirks = SDHCI_QUIRK_BROKEN_TIMEOUT_VAL |
40 SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK |
41 SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN |
42 SDHCI_QUIRK_INVERTED_WRITE_PROTECT |
43 SDHCI_QUIRK_DELAY_AFTER_POWER,
44};
45
46static int sdhci_sirf_probe(struct platform_device *pdev)
47{
48 struct sdhci_host *host;
49 struct sdhci_pltfm_host *pltfm_host;
50 struct sdhci_sirf_priv *priv;
Arnd Bergmanne2f6aac2013-06-27 11:17:25 -040051 struct clk *clk;
52 int gpio_cd;
Barry Songb3b665b2013-03-21 16:27:19 +080053 int ret;
54
Arnd Bergmanne2f6aac2013-06-27 11:17:25 -040055 clk = devm_clk_get(&pdev->dev, NULL);
56 if (IS_ERR(clk)) {
Barry Songb3b665b2013-03-21 16:27:19 +080057 dev_err(&pdev->dev, "unable to get clock");
Arnd Bergmanne2f6aac2013-06-27 11:17:25 -040058 return PTR_ERR(clk);
Barry Songb3b665b2013-03-21 16:27:19 +080059 }
60
Arnd Bergmanne2f6aac2013-06-27 11:17:25 -040061 if (pdev->dev.of_node)
62 gpio_cd = of_get_named_gpio(pdev->dev.of_node, "cd-gpios", 0);
63 else
64 gpio_cd = -EINVAL;
Barry Songb3b665b2013-03-21 16:27:19 +080065
Arnd Bergmanne2f6aac2013-06-27 11:17:25 -040066 host = sdhci_pltfm_init(pdev, &sdhci_sirf_pdata, sizeof(struct sdhci_sirf_priv));
67 if (IS_ERR(host))
68 return PTR_ERR(host);
Barry Songb3b665b2013-03-21 16:27:19 +080069
70 pltfm_host = sdhci_priv(host);
Arnd Bergmanne2f6aac2013-06-27 11:17:25 -040071 priv = sdhci_pltfm_priv(pltfm_host);
72 priv->clk = clk;
73 priv->gpio_cd = gpio_cd;
Barry Songb3b665b2013-03-21 16:27:19 +080074
75 sdhci_get_of_property(pdev);
76
Arnd Bergmanne2f6aac2013-06-27 11:17:25 -040077 ret = clk_prepare_enable(priv->clk);
78 if (ret)
79 goto err_clk_prepare;
Barry Songb3b665b2013-03-21 16:27:19 +080080
81 ret = sdhci_add_host(host);
82 if (ret)
83 goto err_sdhci_add;
84
85 /*
86 * We must request the IRQ after sdhci_add_host(), as the tasklet only
87 * gets setup in sdhci_add_host() and we oops.
88 */
89 if (gpio_is_valid(priv->gpio_cd)) {
Laurent Pinchart214fc302013-08-08 12:38:31 +020090 ret = mmc_gpio_request_cd(host->mmc, priv->gpio_cd, 0);
Barry Songb3b665b2013-03-21 16:27:19 +080091 if (ret) {
92 dev_err(&pdev->dev, "card detect irq request failed: %d\n",
93 ret);
94 goto err_request_cd;
95 }
96 }
97
98 return 0;
99
100err_request_cd:
101 sdhci_remove_host(host, 0);
102err_sdhci_add:
103 clk_disable_unprepare(priv->clk);
Arnd Bergmanne2f6aac2013-06-27 11:17:25 -0400104err_clk_prepare:
Barry Songb3b665b2013-03-21 16:27:19 +0800105 sdhci_pltfm_free(pdev);
Barry Songb3b665b2013-03-21 16:27:19 +0800106 return ret;
107}
108
109static int sdhci_sirf_remove(struct platform_device *pdev)
110{
111 struct sdhci_host *host = platform_get_drvdata(pdev);
112 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
Arnd Bergmanne2f6aac2013-06-27 11:17:25 -0400113 struct sdhci_sirf_priv *priv = sdhci_pltfm_priv(pltfm_host);
Barry Songb3b665b2013-03-21 16:27:19 +0800114
115 sdhci_pltfm_unregister(pdev);
116
117 if (gpio_is_valid(priv->gpio_cd))
118 mmc_gpio_free_cd(host->mmc);
119
120 clk_disable_unprepare(priv->clk);
121 return 0;
122}
123
124#ifdef CONFIG_PM_SLEEP
125static int sdhci_sirf_suspend(struct device *dev)
126{
127 struct sdhci_host *host = dev_get_drvdata(dev);
128 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
Arnd Bergmanne2f6aac2013-06-27 11:17:25 -0400129 struct sdhci_sirf_priv *priv = sdhci_pltfm_priv(pltfm_host);
Barry Songb3b665b2013-03-21 16:27:19 +0800130 int ret;
131
132 ret = sdhci_suspend_host(host);
133 if (ret)
134 return ret;
135
136 clk_disable(priv->clk);
137
138 return 0;
139}
140
141static int sdhci_sirf_resume(struct device *dev)
142{
143 struct sdhci_host *host = dev_get_drvdata(dev);
144 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
Arnd Bergmanne2f6aac2013-06-27 11:17:25 -0400145 struct sdhci_sirf_priv *priv = sdhci_pltfm_priv(pltfm_host);
Barry Songb3b665b2013-03-21 16:27:19 +0800146 int ret;
147
148 ret = clk_enable(priv->clk);
149 if (ret) {
150 dev_dbg(dev, "Resume: Error enabling clock\n");
151 return ret;
152 }
153
154 return sdhci_resume_host(host);
155}
156
157static SIMPLE_DEV_PM_OPS(sdhci_sirf_pm_ops, sdhci_sirf_suspend, sdhci_sirf_resume);
158#endif
159
160static const struct of_device_id sdhci_sirf_of_match[] = {
161 { .compatible = "sirf,prima2-sdhc" },
162 { }
163};
164MODULE_DEVICE_TABLE(of, sdhci_sirf_of_match);
165
166static struct platform_driver sdhci_sirf_driver = {
167 .driver = {
168 .name = "sdhci-sirf",
169 .owner = THIS_MODULE,
170 .of_match_table = sdhci_sirf_of_match,
171#ifdef CONFIG_PM_SLEEP
172 .pm = &sdhci_sirf_pm_ops,
173#endif
174 },
175 .probe = sdhci_sirf_probe,
176 .remove = sdhci_sirf_remove,
177};
178
179module_platform_driver(sdhci_sirf_driver);
180
181MODULE_DESCRIPTION("SDHCI driver for SiRFprimaII/SiRFmarco");
182MODULE_AUTHOR("Barry Song <21cnbao@gmail.com>");
183MODULE_LICENSE("GPL v2");