blob: 5d79e10e1ba2c7cf7139c467559ddad4b3090ce3 [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 = {
31 .get_max_clock = sdhci_sirf_get_max_clk,
Russell King2317f562014-04-25 12:57:07 +010032 .set_bus_width = sdhci_set_bus_width,
Russell King03231f92014-04-25 12:57:12 +010033 .reset = sdhci_reset,
Barry Songb3b665b2013-03-21 16:27:19 +080034};
35
36static struct sdhci_pltfm_data sdhci_sirf_pdata = {
37 .ops = &sdhci_sirf_ops,
38 .quirks = SDHCI_QUIRK_BROKEN_TIMEOUT_VAL |
39 SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK |
40 SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN |
41 SDHCI_QUIRK_INVERTED_WRITE_PROTECT |
42 SDHCI_QUIRK_DELAY_AFTER_POWER,
43};
44
45static int sdhci_sirf_probe(struct platform_device *pdev)
46{
47 struct sdhci_host *host;
48 struct sdhci_pltfm_host *pltfm_host;
49 struct sdhci_sirf_priv *priv;
Arnd Bergmanne2f6aac2013-06-27 11:17:25 -040050 struct clk *clk;
51 int gpio_cd;
Barry Songb3b665b2013-03-21 16:27:19 +080052 int ret;
53
Arnd Bergmanne2f6aac2013-06-27 11:17:25 -040054 clk = devm_clk_get(&pdev->dev, NULL);
55 if (IS_ERR(clk)) {
Barry Songb3b665b2013-03-21 16:27:19 +080056 dev_err(&pdev->dev, "unable to get clock");
Arnd Bergmanne2f6aac2013-06-27 11:17:25 -040057 return PTR_ERR(clk);
Barry Songb3b665b2013-03-21 16:27:19 +080058 }
59
Arnd Bergmanne2f6aac2013-06-27 11:17:25 -040060 if (pdev->dev.of_node)
61 gpio_cd = of_get_named_gpio(pdev->dev.of_node, "cd-gpios", 0);
62 else
63 gpio_cd = -EINVAL;
Barry Songb3b665b2013-03-21 16:27:19 +080064
Arnd Bergmanne2f6aac2013-06-27 11:17:25 -040065 host = sdhci_pltfm_init(pdev, &sdhci_sirf_pdata, sizeof(struct sdhci_sirf_priv));
66 if (IS_ERR(host))
67 return PTR_ERR(host);
Barry Songb3b665b2013-03-21 16:27:19 +080068
69 pltfm_host = sdhci_priv(host);
Arnd Bergmanne2f6aac2013-06-27 11:17:25 -040070 priv = sdhci_pltfm_priv(pltfm_host);
71 priv->clk = clk;
72 priv->gpio_cd = gpio_cd;
Barry Songb3b665b2013-03-21 16:27:19 +080073
74 sdhci_get_of_property(pdev);
75
Arnd Bergmanne2f6aac2013-06-27 11:17:25 -040076 ret = clk_prepare_enable(priv->clk);
77 if (ret)
78 goto err_clk_prepare;
Barry Songb3b665b2013-03-21 16:27:19 +080079
80 ret = sdhci_add_host(host);
81 if (ret)
82 goto err_sdhci_add;
83
84 /*
85 * We must request the IRQ after sdhci_add_host(), as the tasklet only
86 * gets setup in sdhci_add_host() and we oops.
87 */
88 if (gpio_is_valid(priv->gpio_cd)) {
Laurent Pinchart214fc302013-08-08 12:38:31 +020089 ret = mmc_gpio_request_cd(host->mmc, priv->gpio_cd, 0);
Barry Songb3b665b2013-03-21 16:27:19 +080090 if (ret) {
91 dev_err(&pdev->dev, "card detect irq request failed: %d\n",
92 ret);
93 goto err_request_cd;
94 }
95 }
96
97 return 0;
98
99err_request_cd:
100 sdhci_remove_host(host, 0);
101err_sdhci_add:
102 clk_disable_unprepare(priv->clk);
Arnd Bergmanne2f6aac2013-06-27 11:17:25 -0400103err_clk_prepare:
Barry Songb3b665b2013-03-21 16:27:19 +0800104 sdhci_pltfm_free(pdev);
Barry Songb3b665b2013-03-21 16:27:19 +0800105 return ret;
106}
107
108static int sdhci_sirf_remove(struct platform_device *pdev)
109{
110 struct sdhci_host *host = platform_get_drvdata(pdev);
111 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
Arnd Bergmanne2f6aac2013-06-27 11:17:25 -0400112 struct sdhci_sirf_priv *priv = sdhci_pltfm_priv(pltfm_host);
Barry Songb3b665b2013-03-21 16:27:19 +0800113
114 sdhci_pltfm_unregister(pdev);
115
116 if (gpio_is_valid(priv->gpio_cd))
117 mmc_gpio_free_cd(host->mmc);
118
119 clk_disable_unprepare(priv->clk);
120 return 0;
121}
122
123#ifdef CONFIG_PM_SLEEP
124static int sdhci_sirf_suspend(struct device *dev)
125{
126 struct sdhci_host *host = dev_get_drvdata(dev);
127 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
Arnd Bergmanne2f6aac2013-06-27 11:17:25 -0400128 struct sdhci_sirf_priv *priv = sdhci_pltfm_priv(pltfm_host);
Barry Songb3b665b2013-03-21 16:27:19 +0800129 int ret;
130
131 ret = sdhci_suspend_host(host);
132 if (ret)
133 return ret;
134
135 clk_disable(priv->clk);
136
137 return 0;
138}
139
140static int sdhci_sirf_resume(struct device *dev)
141{
142 struct sdhci_host *host = dev_get_drvdata(dev);
143 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
Arnd Bergmanne2f6aac2013-06-27 11:17:25 -0400144 struct sdhci_sirf_priv *priv = sdhci_pltfm_priv(pltfm_host);
Barry Songb3b665b2013-03-21 16:27:19 +0800145 int ret;
146
147 ret = clk_enable(priv->clk);
148 if (ret) {
149 dev_dbg(dev, "Resume: Error enabling clock\n");
150 return ret;
151 }
152
153 return sdhci_resume_host(host);
154}
155
156static SIMPLE_DEV_PM_OPS(sdhci_sirf_pm_ops, sdhci_sirf_suspend, sdhci_sirf_resume);
157#endif
158
159static const struct of_device_id sdhci_sirf_of_match[] = {
160 { .compatible = "sirf,prima2-sdhc" },
161 { }
162};
163MODULE_DEVICE_TABLE(of, sdhci_sirf_of_match);
164
165static struct platform_driver sdhci_sirf_driver = {
166 .driver = {
167 .name = "sdhci-sirf",
168 .owner = THIS_MODULE,
169 .of_match_table = sdhci_sirf_of_match,
170#ifdef CONFIG_PM_SLEEP
171 .pm = &sdhci_sirf_pm_ops,
172#endif
173 },
174 .probe = sdhci_sirf_probe,
175 .remove = sdhci_sirf_remove,
176};
177
178module_platform_driver(sdhci_sirf_driver);
179
180MODULE_DESCRIPTION("SDHCI driver for SiRFprimaII/SiRFmarco");
181MODULE_AUTHOR("Barry Song <21cnbao@gmail.com>");
182MODULE_LICENSE("GPL v2");