blob: 17004531d089e6c2bce4fc33d319f6fdf3cb377f [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,
Russell King96d7b782014-04-25 12:59:26 +010035 .set_uhs_signaling = sdhci_set_uhs_signaling,
Barry Songb3b665b2013-03-21 16:27:19 +080036};
37
38static struct sdhci_pltfm_data sdhci_sirf_pdata = {
39 .ops = &sdhci_sirf_ops,
40 .quirks = SDHCI_QUIRK_BROKEN_TIMEOUT_VAL |
41 SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK |
42 SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN |
43 SDHCI_QUIRK_INVERTED_WRITE_PROTECT |
44 SDHCI_QUIRK_DELAY_AFTER_POWER,
45};
46
47static int sdhci_sirf_probe(struct platform_device *pdev)
48{
49 struct sdhci_host *host;
50 struct sdhci_pltfm_host *pltfm_host;
51 struct sdhci_sirf_priv *priv;
Arnd Bergmanne2f6aac2013-06-27 11:17:25 -040052 struct clk *clk;
53 int gpio_cd;
Barry Songb3b665b2013-03-21 16:27:19 +080054 int ret;
55
Arnd Bergmanne2f6aac2013-06-27 11:17:25 -040056 clk = devm_clk_get(&pdev->dev, NULL);
57 if (IS_ERR(clk)) {
Barry Songb3b665b2013-03-21 16:27:19 +080058 dev_err(&pdev->dev, "unable to get clock");
Arnd Bergmanne2f6aac2013-06-27 11:17:25 -040059 return PTR_ERR(clk);
Barry Songb3b665b2013-03-21 16:27:19 +080060 }
61
Arnd Bergmanne2f6aac2013-06-27 11:17:25 -040062 if (pdev->dev.of_node)
63 gpio_cd = of_get_named_gpio(pdev->dev.of_node, "cd-gpios", 0);
64 else
65 gpio_cd = -EINVAL;
Barry Songb3b665b2013-03-21 16:27:19 +080066
Arnd Bergmanne2f6aac2013-06-27 11:17:25 -040067 host = sdhci_pltfm_init(pdev, &sdhci_sirf_pdata, sizeof(struct sdhci_sirf_priv));
68 if (IS_ERR(host))
69 return PTR_ERR(host);
Barry Songb3b665b2013-03-21 16:27:19 +080070
71 pltfm_host = sdhci_priv(host);
Arnd Bergmanne2f6aac2013-06-27 11:17:25 -040072 priv = sdhci_pltfm_priv(pltfm_host);
73 priv->clk = clk;
74 priv->gpio_cd = gpio_cd;
Barry Songb3b665b2013-03-21 16:27:19 +080075
76 sdhci_get_of_property(pdev);
77
Arnd Bergmanne2f6aac2013-06-27 11:17:25 -040078 ret = clk_prepare_enable(priv->clk);
79 if (ret)
80 goto err_clk_prepare;
Barry Songb3b665b2013-03-21 16:27:19 +080081
82 ret = sdhci_add_host(host);
83 if (ret)
84 goto err_sdhci_add;
85
86 /*
87 * We must request the IRQ after sdhci_add_host(), as the tasklet only
88 * gets setup in sdhci_add_host() and we oops.
89 */
90 if (gpio_is_valid(priv->gpio_cd)) {
Laurent Pinchart214fc302013-08-08 12:38:31 +020091 ret = mmc_gpio_request_cd(host->mmc, priv->gpio_cd, 0);
Barry Songb3b665b2013-03-21 16:27:19 +080092 if (ret) {
93 dev_err(&pdev->dev, "card detect irq request failed: %d\n",
94 ret);
95 goto err_request_cd;
96 }
97 }
98
99 return 0;
100
101err_request_cd:
102 sdhci_remove_host(host, 0);
103err_sdhci_add:
104 clk_disable_unprepare(priv->clk);
Arnd Bergmanne2f6aac2013-06-27 11:17:25 -0400105err_clk_prepare:
Barry Songb3b665b2013-03-21 16:27:19 +0800106 sdhci_pltfm_free(pdev);
Barry Songb3b665b2013-03-21 16:27:19 +0800107 return ret;
108}
109
110static int sdhci_sirf_remove(struct platform_device *pdev)
111{
112 struct sdhci_host *host = platform_get_drvdata(pdev);
113 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
Arnd Bergmanne2f6aac2013-06-27 11:17:25 -0400114 struct sdhci_sirf_priv *priv = sdhci_pltfm_priv(pltfm_host);
Barry Songb3b665b2013-03-21 16:27:19 +0800115
116 sdhci_pltfm_unregister(pdev);
117
118 if (gpio_is_valid(priv->gpio_cd))
119 mmc_gpio_free_cd(host->mmc);
120
121 clk_disable_unprepare(priv->clk);
122 return 0;
123}
124
125#ifdef CONFIG_PM_SLEEP
126static int sdhci_sirf_suspend(struct device *dev)
127{
128 struct sdhci_host *host = dev_get_drvdata(dev);
129 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
Arnd Bergmanne2f6aac2013-06-27 11:17:25 -0400130 struct sdhci_sirf_priv *priv = sdhci_pltfm_priv(pltfm_host);
Barry Songb3b665b2013-03-21 16:27:19 +0800131 int ret;
132
133 ret = sdhci_suspend_host(host);
134 if (ret)
135 return ret;
136
137 clk_disable(priv->clk);
138
139 return 0;
140}
141
142static int sdhci_sirf_resume(struct device *dev)
143{
144 struct sdhci_host *host = dev_get_drvdata(dev);
145 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
Arnd Bergmanne2f6aac2013-06-27 11:17:25 -0400146 struct sdhci_sirf_priv *priv = sdhci_pltfm_priv(pltfm_host);
Barry Songb3b665b2013-03-21 16:27:19 +0800147 int ret;
148
149 ret = clk_enable(priv->clk);
150 if (ret) {
151 dev_dbg(dev, "Resume: Error enabling clock\n");
152 return ret;
153 }
154
155 return sdhci_resume_host(host);
156}
157
158static SIMPLE_DEV_PM_OPS(sdhci_sirf_pm_ops, sdhci_sirf_suspend, sdhci_sirf_resume);
159#endif
160
161static const struct of_device_id sdhci_sirf_of_match[] = {
162 { .compatible = "sirf,prima2-sdhc" },
163 { }
164};
165MODULE_DEVICE_TABLE(of, sdhci_sirf_of_match);
166
167static struct platform_driver sdhci_sirf_driver = {
168 .driver = {
169 .name = "sdhci-sirf",
170 .owner = THIS_MODULE,
171 .of_match_table = sdhci_sirf_of_match,
172#ifdef CONFIG_PM_SLEEP
173 .pm = &sdhci_sirf_pm_ops,
174#endif
175 },
176 .probe = sdhci_sirf_probe,
177 .remove = sdhci_sirf_remove,
178};
179
180module_platform_driver(sdhci_sirf_driver);
181
182MODULE_DESCRIPTION("SDHCI driver for SiRFprimaII/SiRFmarco");
183MODULE_AUTHOR("Barry Song <21cnbao@gmail.com>");
184MODULE_LICENSE("GPL v2");