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>
32 struct sdhci_plat_data
*data
;
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
;
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
);
70 static int __devinit
sdhci_probe(struct platform_device
*pdev
)
72 struct sdhci_host
*host
;
73 struct resource
*iomem
;
74 struct spear_sdhci
*sdhci
;
79 iomem
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
82 dev_dbg(&pdev
->dev
, "memory resource not defined\n");
86 if (!request_mem_region(iomem
->start
, resource_size(iomem
),
89 dev_dbg(&pdev
->dev
, "cannot request region\n");
93 sdhci
= kzalloc(sizeof(*sdhci
), GFP_KERNEL
);
96 dev_dbg(&pdev
->dev
, "cannot allocate memory for sdhci\n");
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");
108 ret
= clk_enable(sdhci
->clk
);
110 dev_dbg(&pdev
->dev
, "Error enabling clock\n");
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);
121 host
= sdhci_alloc_host(&pdev
->dev
, 0);
125 dev_dbg(&pdev
->dev
, "error allocating host\n");
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
));
137 dev_dbg(&pdev
->dev
, "failed to remap registers\n");
141 ret
= sdhci_add_host(host
);
143 dev_dbg(&pdev
->dev
, "error adding host\n");
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
160 if (sdhci
->data
->card_power_gpio
>= 0) {
163 ret
= gpio_request(sdhci
->data
->card_power_gpio
, "sdhci");
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
;
173 val
= !sdhci
->data
->power_active_high
;
175 ret
= gpio_direction_output(sdhci
->data
->card_power_gpio
, val
);
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");
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
);
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
);
203 dev_dbg(&pdev
->dev
, "gpio request irq fail: %d\n",
204 sdhci
->data
->card_int_gpio
);
205 goto err_igpio_request_irq
;
212 err_igpio_request_irq
:
214 if (sdhci
->data
->card_int_gpio
>= 0)
215 gpio_free(sdhci
->data
->card_int_gpio
);
218 if (sdhci
->data
->card_power_gpio
>= 0)
219 gpio_free(sdhci
->data
->card_power_gpio
);
221 platform_set_drvdata(pdev
, NULL
);
222 sdhci_remove_host(host
, 1);
224 iounmap(host
->ioaddr
);
226 sdhci_free_host(host
);
228 clk_disable(sdhci
->clk
);
234 release_mem_region(iomem
->start
, resource_size(iomem
));
236 dev_err(&pdev
->dev
, "spear-sdhci probe failed: %d\n", 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
);
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
);
260 scratch
= readl(host
->ioaddr
+ SDHCI_INT_STATUS
);
261 if (scratch
== (u32
)-1)
264 sdhci_remove_host(host
, dead
);
265 iounmap(host
->ioaddr
);
266 sdhci_free_host(host
);
267 clk_disable(sdhci
->clk
);
271 release_mem_region(iomem
->start
, resource_size(iomem
));
276 static struct platform_driver sdhci_driver
= {
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");