Add linux-next specific files for 20110831
[linux-2.6/next.git] / drivers / mmc / host / sdhci-spear.c
bloba152b5c4235cbdd845dd7a30efd35bd06bd764fb
1 /*
2 * drivers/mmc/host/sdhci-spear.c
4 * Support of SDHCI platform devices for spear soc family
6 * Copyright (C) 2010 ST Microelectronics
7 * Viresh Kumar<viresh.kumar@st.com>
9 * Inspired by sdhci-pltfm.c
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.
16 #include <linux/clk.h>
17 #include <linux/delay.h>
18 #include <linux/gpio.h>
19 #include <linux/highmem.h>
20 #include <linux/module.h>
21 #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"
30 struct spear_sdhci {
31 struct clk *clk;
32 struct sdhci_plat_data *data;
35 /* sdhci ops */
36 static struct sdhci_ops sdhci_pltfm_ops = {
37 /* Nothing to do for now. */
40 /* gpio card detection interrupt handler */
41 static irqreturn_t sdhci_gpio_irq(int irq, void *dev_id)
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;
49 val = gpio_get_value(sdhci->data->card_int_gpio);
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;
54 irq_set_irq_type(irq, gpio_irq_type);
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);
64 /* inform sdhci driver about card insertion/removal */
65 tasklet_schedule(&host->card_tasklet);
67 return IRQ_HANDLED;
70 static int __devinit sdhci_probe(struct platform_device *pdev)
72 struct sdhci_host *host;
73 struct resource *iomem;
74 struct spear_sdhci *sdhci;
75 int ret;
77 BUG_ON(pdev == NULL);
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;
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;
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;
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;
108 ret = clk_enable(sdhci->clk);
109 if (ret) {
110 dev_dbg(&pdev->dev, "Error enabling clock\n");
111 goto err_clk_enb;
114 /* overwrite platform_data */
115 sdhci->data = dev_get_platdata(&pdev->dev);
116 pdev->dev.platform_data = sdhci;
118 if (pdev->dev.parent)
119 host = sdhci_alloc_host(pdev->dev.parent, 0);
120 else
121 host = sdhci_alloc_host(&pdev->dev, 0);
123 if (IS_ERR(host)) {
124 ret = PTR_ERR(host);
125 dev_dbg(&pdev->dev, "error allocating host\n");
126 goto err_alloc_host;
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;
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;
141 ret = sdhci_add_host(host);
142 if (ret) {
143 dev_dbg(&pdev->dev, "error adding host\n");
144 goto err_add_host;
147 platform_set_drvdata(pdev, host);
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
157 if (!sdhci->data)
158 return 0;
160 if (sdhci->data->card_power_gpio >= 0) {
161 int val = 0;
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;
170 if (sdhci->data->power_always_enb)
171 val = sdhci->data->power_active_high;
172 else
173 val = !sdhci->data->power_active_high;
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;
182 gpio_set_value(sdhci->data->card_power_gpio, 1);
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;
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;
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;
210 return 0;
212 err_igpio_request_irq:
213 err_igpio_direction:
214 if (sdhci->data->card_int_gpio >= 0)
215 gpio_free(sdhci->data->card_int_gpio);
216 err_igpio_request:
217 err_pgpio_direction:
218 if (sdhci->data->card_power_gpio >= 0)
219 gpio_free(sdhci->data->card_power_gpio);
220 err_pgpio_request:
221 platform_set_drvdata(pdev, NULL);
222 sdhci_remove_host(host, 1);
223 err_add_host:
224 iounmap(host->ioaddr);
225 err_ioremap:
226 sdhci_free_host(host);
227 err_alloc_host:
228 clk_disable(sdhci->clk);
229 err_clk_enb:
230 clk_put(sdhci->clk);
231 err_clk_get:
232 kfree(sdhci);
233 err_kzalloc:
234 release_mem_region(iomem->start, resource_size(iomem));
235 err:
236 dev_err(&pdev->dev, "spear-sdhci probe failed: %d\n", ret);
237 return ret;
240 static int __devexit sdhci_remove(struct platform_device *pdev)
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;
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);
254 if (sdhci->data->card_power_gpio >= 0)
255 gpio_free(sdhci->data->card_power_gpio);
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;
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));
273 return 0;
276 static struct platform_driver sdhci_driver = {
277 .driver = {
278 .name = "sdhci",
279 .owner = THIS_MODULE,
281 .probe = sdhci_probe,
282 .remove = __devexit_p(sdhci_remove),
285 static int __init sdhci_init(void)
287 return platform_driver_register(&sdhci_driver);
289 module_init(sdhci_init);
291 static void __exit sdhci_exit(void)
293 platform_driver_unregister(&sdhci_driver);
295 module_exit(sdhci_exit);
297 MODULE_DESCRIPTION("SPEAr Secure Digital Host Controller Interface driver");
298 MODULE_AUTHOR("Viresh Kumar <viresh.kumar@st.com>");
299 MODULE_LICENSE("GPL v2");