blob: a152b5c4235cbdd845dd7a30efd35bd06bd764fb [file] [log] [blame]
Viresh KUMARc63b3cb2010-05-26 14:42:10 -07001/*
2 * drivers/mmc/host/sdhci-spear.c
3 *
4 * Support of SDHCI platform devices for spear soc family
5 *
6 * Copyright (C) 2010 ST Microelectronics
7 * Viresh Kumar<viresh.kumar@st.com>
8 *
9 * Inspired by sdhci-pltfm.c
10 *
11 * This file is licensed under the terms of the GNU General Public
12 * License version 2. This program is licensed "as is" without any
13 * warranty of any kind, whether express or implied.
14 */
15
16#include <linux/clk.h>
17#include <linux/delay.h>
18#include <linux/gpio.h>
19#include <linux/highmem.h>
Paul Gortmakera4df3ae2011-07-03 15:15:51 -040020#include <linux/module.h>
Viresh KUMARc63b3cb2010-05-26 14:42:10 -070021#include <linux/interrupt.h>
22#include <linux/irq.h>
23#include <linux/platform_device.h>
24#include <linux/slab.h>
25#include <linux/mmc/host.h>
26#include <linux/mmc/sdhci-spear.h>
27#include <linux/io.h>
28#include "sdhci.h"
29
30struct spear_sdhci {
31 struct clk *clk;
32 struct sdhci_plat_data *data;
33};
34
35/* sdhci ops */
36static struct sdhci_ops sdhci_pltfm_ops = {
37 /* Nothing to do for now. */
38};
39
40/* gpio card detection interrupt handler */
41static irqreturn_t sdhci_gpio_irq(int irq, void *dev_id)
42{
43 struct platform_device *pdev = dev_id;
44 struct sdhci_host *host = platform_get_drvdata(pdev);
45 struct spear_sdhci *sdhci = dev_get_platdata(&pdev->dev);
46 unsigned long gpio_irq_type;
47 int val;
48
49 val = gpio_get_value(sdhci->data->card_int_gpio);
50
51 /* val == 1 -> card removed, val == 0 -> card inserted */
52 /* if card removed - set irq for low level, else vice versa */
53 gpio_irq_type = val ? IRQF_TRIGGER_LOW : IRQF_TRIGGER_HIGH;
Thomas Gleixnerdced35a2011-03-28 17:49:12 +020054 irq_set_irq_type(irq, gpio_irq_type);
Viresh KUMARc63b3cb2010-05-26 14:42:10 -070055
56 if (sdhci->data->card_power_gpio >= 0) {
57 if (!sdhci->data->power_always_enb) {
58 /* if card inserted, give power, otherwise remove it */
59 val = sdhci->data->power_active_high ? !val : val ;
60 gpio_set_value(sdhci->data->card_power_gpio, val);
61 }
62 }
63
64 /* inform sdhci driver about card insertion/removal */
65 tasklet_schedule(&host->card_tasklet);
66
67 return IRQ_HANDLED;
68}
69
70static int __devinit sdhci_probe(struct platform_device *pdev)
71{
72 struct sdhci_host *host;
73 struct resource *iomem;
74 struct spear_sdhci *sdhci;
75 int ret;
76
77 BUG_ON(pdev == NULL);
78
79 iomem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
80 if (!iomem) {
81 ret = -ENOMEM;
82 dev_dbg(&pdev->dev, "memory resource not defined\n");
83 goto err;
84 }
85
86 if (!request_mem_region(iomem->start, resource_size(iomem),
87 "spear-sdhci")) {
88 ret = -EBUSY;
89 dev_dbg(&pdev->dev, "cannot request region\n");
90 goto err;
91 }
92
93 sdhci = kzalloc(sizeof(*sdhci), GFP_KERNEL);
94 if (!sdhci) {
95 ret = -ENOMEM;
96 dev_dbg(&pdev->dev, "cannot allocate memory for sdhci\n");
97 goto err_kzalloc;
98 }
99
100 /* clk enable */
101 sdhci->clk = clk_get(&pdev->dev, NULL);
102 if (IS_ERR(sdhci->clk)) {
103 ret = PTR_ERR(sdhci->clk);
104 dev_dbg(&pdev->dev, "Error getting clock\n");
105 goto err_clk_get;
106 }
107
108 ret = clk_enable(sdhci->clk);
109 if (ret) {
110 dev_dbg(&pdev->dev, "Error enabling clock\n");
111 goto err_clk_enb;
112 }
113
114 /* overwrite platform_data */
115 sdhci->data = dev_get_platdata(&pdev->dev);
116 pdev->dev.platform_data = sdhci;
117
118 if (pdev->dev.parent)
119 host = sdhci_alloc_host(pdev->dev.parent, 0);
120 else
121 host = sdhci_alloc_host(&pdev->dev, 0);
122
123 if (IS_ERR(host)) {
124 ret = PTR_ERR(host);
125 dev_dbg(&pdev->dev, "error allocating host\n");
126 goto err_alloc_host;
127 }
128
129 host->hw_name = "sdhci";
130 host->ops = &sdhci_pltfm_ops;
131 host->irq = platform_get_irq(pdev, 0);
132 host->quirks = SDHCI_QUIRK_BROKEN_ADMA;
133
134 host->ioaddr = ioremap(iomem->start, resource_size(iomem));
135 if (!host->ioaddr) {
136 ret = -ENOMEM;
137 dev_dbg(&pdev->dev, "failed to remap registers\n");
138 goto err_ioremap;
139 }
140
141 ret = sdhci_add_host(host);
142 if (ret) {
143 dev_dbg(&pdev->dev, "error adding host\n");
144 goto err_add_host;
145 }
146
147 platform_set_drvdata(pdev, host);
148
149 /*
150 * It is optional to use GPIOs for sdhci Power control & sdhci card
151 * interrupt detection. If sdhci->data is NULL, then use original sdhci
152 * lines otherwise GPIO lines.
153 * If GPIO is selected for power control, then power should be disabled
154 * after card removal and should be enabled when card insertion
155 * interrupt occurs
156 */
157 if (!sdhci->data)
158 return 0;
159
160 if (sdhci->data->card_power_gpio >= 0) {
161 int val = 0;
162
163 ret = gpio_request(sdhci->data->card_power_gpio, "sdhci");
164 if (ret < 0) {
165 dev_dbg(&pdev->dev, "gpio request fail: %d\n",
166 sdhci->data->card_power_gpio);
167 goto err_pgpio_request;
168 }
169
170 if (sdhci->data->power_always_enb)
171 val = sdhci->data->power_active_high;
172 else
173 val = !sdhci->data->power_active_high;
174
175 ret = gpio_direction_output(sdhci->data->card_power_gpio, val);
176 if (ret) {
177 dev_dbg(&pdev->dev, "gpio set direction fail: %d\n",
178 sdhci->data->card_power_gpio);
179 goto err_pgpio_direction;
180 }
181
182 gpio_set_value(sdhci->data->card_power_gpio, 1);
183 }
184
185 if (sdhci->data->card_int_gpio >= 0) {
186 ret = gpio_request(sdhci->data->card_int_gpio, "sdhci");
187 if (ret < 0) {
188 dev_dbg(&pdev->dev, "gpio request fail: %d\n",
189 sdhci->data->card_int_gpio);
190 goto err_igpio_request;
191 }
192
193 ret = gpio_direction_input(sdhci->data->card_int_gpio);
194 if (ret) {
195 dev_dbg(&pdev->dev, "gpio set direction fail: %d\n",
196 sdhci->data->card_int_gpio);
197 goto err_igpio_direction;
198 }
199 ret = request_irq(gpio_to_irq(sdhci->data->card_int_gpio),
200 sdhci_gpio_irq, IRQF_TRIGGER_LOW,
201 mmc_hostname(host->mmc), pdev);
202 if (ret) {
203 dev_dbg(&pdev->dev, "gpio request irq fail: %d\n",
204 sdhci->data->card_int_gpio);
205 goto err_igpio_request_irq;
206 }
207
208 }
209
210 return 0;
211
212err_igpio_request_irq:
213err_igpio_direction:
214 if (sdhci->data->card_int_gpio >= 0)
215 gpio_free(sdhci->data->card_int_gpio);
216err_igpio_request:
217err_pgpio_direction:
218 if (sdhci->data->card_power_gpio >= 0)
219 gpio_free(sdhci->data->card_power_gpio);
220err_pgpio_request:
221 platform_set_drvdata(pdev, NULL);
222 sdhci_remove_host(host, 1);
223err_add_host:
224 iounmap(host->ioaddr);
225err_ioremap:
226 sdhci_free_host(host);
227err_alloc_host:
228 clk_disable(sdhci->clk);
229err_clk_enb:
230 clk_put(sdhci->clk);
231err_clk_get:
232 kfree(sdhci);
233err_kzalloc:
234 release_mem_region(iomem->start, resource_size(iomem));
235err:
236 dev_err(&pdev->dev, "spear-sdhci probe failed: %d\n", ret);
237 return ret;
238}
239
240static int __devexit sdhci_remove(struct platform_device *pdev)
241{
242 struct sdhci_host *host = platform_get_drvdata(pdev);
243 struct resource *iomem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
244 struct spear_sdhci *sdhci = dev_get_platdata(&pdev->dev);
245 int dead;
246 u32 scratch;
247
248 if (sdhci->data) {
249 if (sdhci->data->card_int_gpio >= 0) {
250 free_irq(gpio_to_irq(sdhci->data->card_int_gpio), pdev);
251 gpio_free(sdhci->data->card_int_gpio);
252 }
253
254 if (sdhci->data->card_power_gpio >= 0)
255 gpio_free(sdhci->data->card_power_gpio);
256 }
257
258 platform_set_drvdata(pdev, NULL);
259 dead = 0;
260 scratch = readl(host->ioaddr + SDHCI_INT_STATUS);
261 if (scratch == (u32)-1)
262 dead = 1;
263
264 sdhci_remove_host(host, dead);
265 iounmap(host->ioaddr);
266 sdhci_free_host(host);
267 clk_disable(sdhci->clk);
268 clk_put(sdhci->clk);
269 kfree(sdhci);
270 if (iomem)
271 release_mem_region(iomem->start, resource_size(iomem));
272
273 return 0;
274}
275
276static struct platform_driver sdhci_driver = {
277 .driver = {
278 .name = "sdhci",
279 .owner = THIS_MODULE,
280 },
281 .probe = sdhci_probe,
282 .remove = __devexit_p(sdhci_remove),
283};
284
285static int __init sdhci_init(void)
286{
287 return platform_driver_register(&sdhci_driver);
288}
289module_init(sdhci_init);
290
291static void __exit sdhci_exit(void)
292{
293 platform_driver_unregister(&sdhci_driver);
294}
295module_exit(sdhci_exit);
296
297MODULE_DESCRIPTION("SPEAr Secure Digital Host Controller Interface driver");
298MODULE_AUTHOR("Viresh Kumar <viresh.kumar@st.com>");
299MODULE_LICENSE("GPL v2");