Commit | Line | Data |
---|---|---|
b3b665b0 BS |
1 | /* |
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> | |
b3b665b0 BS |
16 | #include "sdhci-pltfm.h" |
17 | ||
18 | struct sdhci_sirf_priv { | |
19 | struct clk *clk; | |
20 | int gpio_cd; | |
21 | }; | |
22 | ||
23 | static unsigned int sdhci_sirf_get_max_clk(struct sdhci_host *host) | |
24 | { | |
25 | struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); | |
e2f6aac6 | 26 | struct sdhci_sirf_priv *priv = sdhci_pltfm_priv(pltfm_host); |
b3b665b0 BS |
27 | return clk_get_rate(priv->clk); |
28 | } | |
29 | ||
30 | static struct sdhci_ops sdhci_sirf_ops = { | |
31 | .get_max_clock = sdhci_sirf_get_max_clk, | |
2317f56c | 32 | .set_bus_width = sdhci_set_bus_width, |
03231f9b | 33 | .reset = sdhci_reset, |
b3b665b0 BS |
34 | }; |
35 | ||
36 | static 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 | ||
45 | static 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; | |
e2f6aac6 AB |
50 | struct clk *clk; |
51 | int gpio_cd; | |
b3b665b0 | 52 | int ret; |
b3b665b0 | 53 | |
e2f6aac6 AB |
54 | clk = devm_clk_get(&pdev->dev, NULL); |
55 | if (IS_ERR(clk)) { | |
b3b665b0 | 56 | dev_err(&pdev->dev, "unable to get clock"); |
e2f6aac6 | 57 | return PTR_ERR(clk); |
b3b665b0 BS |
58 | } |
59 | ||
e2f6aac6 AB |
60 | 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; | |
b3b665b0 | 64 | |
e2f6aac6 AB |
65 | host = sdhci_pltfm_init(pdev, &sdhci_sirf_pdata, sizeof(struct sdhci_sirf_priv)); |
66 | if (IS_ERR(host)) | |
67 | return PTR_ERR(host); | |
b3b665b0 BS |
68 | |
69 | pltfm_host = sdhci_priv(host); | |
e2f6aac6 AB |
70 | priv = sdhci_pltfm_priv(pltfm_host); |
71 | priv->clk = clk; | |
72 | priv->gpio_cd = gpio_cd; | |
b3b665b0 BS |
73 | |
74 | sdhci_get_of_property(pdev); | |
75 | ||
e2f6aac6 AB |
76 | ret = clk_prepare_enable(priv->clk); |
77 | if (ret) | |
78 | goto err_clk_prepare; | |
b3b665b0 BS |
79 | |
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)) { | |
214fc309 | 89 | ret = mmc_gpio_request_cd(host->mmc, priv->gpio_cd, 0); |
b3b665b0 BS |
90 | 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 | ||
99 | err_request_cd: | |
100 | sdhci_remove_host(host, 0); | |
101 | err_sdhci_add: | |
102 | clk_disable_unprepare(priv->clk); | |
e2f6aac6 | 103 | err_clk_prepare: |
b3b665b0 | 104 | sdhci_pltfm_free(pdev); |
b3b665b0 BS |
105 | return ret; |
106 | } | |
107 | ||
108 | static 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); | |
e2f6aac6 | 112 | struct sdhci_sirf_priv *priv = sdhci_pltfm_priv(pltfm_host); |
b3b665b0 BS |
113 | |
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 | |
124 | static 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); | |
e2f6aac6 | 128 | struct sdhci_sirf_priv *priv = sdhci_pltfm_priv(pltfm_host); |
b3b665b0 BS |
129 | 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 | ||
140 | static 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); | |
e2f6aac6 | 144 | struct sdhci_sirf_priv *priv = sdhci_pltfm_priv(pltfm_host); |
b3b665b0 BS |
145 | 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 | ||
156 | static SIMPLE_DEV_PM_OPS(sdhci_sirf_pm_ops, sdhci_sirf_suspend, sdhci_sirf_resume); | |
157 | #endif | |
158 | ||
159 | static const struct of_device_id sdhci_sirf_of_match[] = { | |
160 | { .compatible = "sirf,prima2-sdhc" }, | |
161 | { } | |
162 | }; | |
163 | MODULE_DEVICE_TABLE(of, sdhci_sirf_of_match); | |
164 | ||
165 | static 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 | ||
178 | module_platform_driver(sdhci_sirf_driver); | |
179 | ||
180 | MODULE_DESCRIPTION("SDHCI driver for SiRFprimaII/SiRFmarco"); | |
181 | MODULE_AUTHOR("Barry Song <21cnbao@gmail.com>"); | |
182 | MODULE_LICENSE("GPL v2"); |