mmc: sdhci-spear: fix platform_data usage
[deliverable/linux.git] / drivers / mmc / host / sdhci-spear.c
CommitLineData
c63b3cba
VK
1/*
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
10d8935f 7 * Viresh Kumar <viresh.linux@gmail.com>
c63b3cba
VK
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>
88b47679 20#include <linux/module.h>
c63b3cba
VK
21#include <linux/interrupt.h>
22#include <linux/irq.h>
067bf748
VK
23#include <linux/of.h>
24#include <linux/of_gpio.h>
c63b3cba 25#include <linux/platform_device.h>
b70a7fab 26#include <linux/pm.h>
c63b3cba
VK
27#include <linux/slab.h>
28#include <linux/mmc/host.h>
29#include <linux/mmc/sdhci-spear.h>
30#include <linux/io.h>
31#include "sdhci.h"
32
33struct spear_sdhci {
34 struct clk *clk;
35 struct sdhci_plat_data *data;
36};
37
38/* sdhci ops */
c915568d 39static const struct sdhci_ops sdhci_pltfm_ops = {
c63b3cba
VK
40 /* Nothing to do for now. */
41};
42
43/* gpio card detection interrupt handler */
44static irqreturn_t sdhci_gpio_irq(int irq, void *dev_id)
45{
46 struct platform_device *pdev = dev_id;
47 struct sdhci_host *host = platform_get_drvdata(pdev);
48 struct spear_sdhci *sdhci = dev_get_platdata(&pdev->dev);
49 unsigned long gpio_irq_type;
50 int val;
51
52 val = gpio_get_value(sdhci->data->card_int_gpio);
53
54 /* val == 1 -> card removed, val == 0 -> card inserted */
55 /* if card removed - set irq for low level, else vice versa */
56 gpio_irq_type = val ? IRQF_TRIGGER_LOW : IRQF_TRIGGER_HIGH;
dced35ae 57 irq_set_irq_type(irq, gpio_irq_type);
c63b3cba
VK
58
59 if (sdhci->data->card_power_gpio >= 0) {
60 if (!sdhci->data->power_always_enb) {
61 /* if card inserted, give power, otherwise remove it */
62 val = sdhci->data->power_active_high ? !val : val ;
63 gpio_set_value(sdhci->data->card_power_gpio, val);
64 }
65 }
66
67 /* inform sdhci driver about card insertion/removal */
68 tasklet_schedule(&host->card_tasklet);
69
70 return IRQ_HANDLED;
71}
72
067bf748 73#ifdef CONFIG_OF
c3be1efd 74static struct sdhci_plat_data *sdhci_probe_config_dt(struct platform_device *pdev)
067bf748
VK
75{
76 struct device_node *np = pdev->dev.of_node;
77 struct sdhci_plat_data *pdata = NULL;
78 int cd_gpio;
79
80 cd_gpio = of_get_named_gpio(np, "cd-gpios", 0);
81 if (!gpio_is_valid(cd_gpio))
82 cd_gpio = -1;
83
84 /* If pdata is required */
85 if (cd_gpio != -1) {
86 pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
eab36b24 87 if (!pdata)
067bf748 88 dev_err(&pdev->dev, "DT: kzalloc failed\n");
eab36b24
SK
89 else
90 pdata->card_int_gpio = cd_gpio;
067bf748
VK
91 }
92
067bf748
VK
93 return pdata;
94}
95#else
c3be1efd 96static struct sdhci_plat_data *sdhci_probe_config_dt(struct platform_device *pdev)
067bf748
VK
97{
98 return ERR_PTR(-ENOSYS);
99}
100#endif
101
c3be1efd 102static int sdhci_probe(struct platform_device *pdev)
c63b3cba 103{
067bf748 104 struct device_node *np = pdev->dev.of_node;
c63b3cba
VK
105 struct sdhci_host *host;
106 struct resource *iomem;
107 struct spear_sdhci *sdhci;
fcdb7c8f 108 struct device *dev;
c63b3cba
VK
109 int ret;
110
c63b3cba
VK
111 iomem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
112 if (!iomem) {
113 ret = -ENOMEM;
114 dev_dbg(&pdev->dev, "memory resource not defined\n");
115 goto err;
116 }
117
6ebaf8f2
VK
118 if (!devm_request_mem_region(&pdev->dev, iomem->start,
119 resource_size(iomem), "spear-sdhci")) {
c63b3cba
VK
120 ret = -EBUSY;
121 dev_dbg(&pdev->dev, "cannot request region\n");
122 goto err;
123 }
124
fcdb7c8f
RK
125 dev = pdev->dev.parent ? pdev->dev.parent : &pdev->dev;
126 host = sdhci_alloc_host(dev, sizeof(*sdhci));
127 if (IS_ERR(host)) {
128 ret = PTR_ERR(host);
c63b3cba 129 dev_dbg(&pdev->dev, "cannot allocate memory for sdhci\n");
6ebaf8f2 130 goto err;
c63b3cba
VK
131 }
132
fcdb7c8f
RK
133 sdhci = sdhci_priv(host);
134
c63b3cba 135 /* clk enable */
142dbab9 136 sdhci->clk = devm_clk_get(&pdev->dev, NULL);
c63b3cba
VK
137 if (IS_ERR(sdhci->clk)) {
138 ret = PTR_ERR(sdhci->clk);
139 dev_dbg(&pdev->dev, "Error getting clock\n");
fcdb7c8f 140 goto err_host;
c63b3cba
VK
141 }
142
da764f97 143 ret = clk_prepare_enable(sdhci->clk);
c63b3cba
VK
144 if (ret) {
145 dev_dbg(&pdev->dev, "Error enabling clock\n");
fcdb7c8f 146 goto err_host;
c63b3cba
VK
147 }
148
257f9df1
VKS
149 ret = clk_set_rate(sdhci->clk, 50000000);
150 if (ret)
151 dev_dbg(&pdev->dev, "Error setting desired clk, clk=%lu\n",
152 clk_get_rate(sdhci->clk));
153
067bf748
VK
154 if (np) {
155 sdhci->data = sdhci_probe_config_dt(pdev);
156 if (IS_ERR(sdhci->data)) {
157 dev_err(&pdev->dev, "DT: Failed to get pdata\n");
142dbab9 158 goto disable_clk;
067bf748
VK
159 }
160 } else {
161 sdhci->data = dev_get_platdata(&pdev->dev);
162 }
163
c63b3cba
VK
164 host->hw_name = "sdhci";
165 host->ops = &sdhci_pltfm_ops;
166 host->irq = platform_get_irq(pdev, 0);
167 host->quirks = SDHCI_QUIRK_BROKEN_ADMA;
168
6ebaf8f2
VK
169 host->ioaddr = devm_ioremap(&pdev->dev, iomem->start,
170 resource_size(iomem));
c63b3cba
VK
171 if (!host->ioaddr) {
172 ret = -ENOMEM;
173 dev_dbg(&pdev->dev, "failed to remap registers\n");
fcdb7c8f 174 goto disable_clk;
c63b3cba
VK
175 }
176
177 ret = sdhci_add_host(host);
178 if (ret) {
179 dev_dbg(&pdev->dev, "error adding host\n");
fcdb7c8f 180 goto disable_clk;
c63b3cba
VK
181 }
182
183 platform_set_drvdata(pdev, host);
184
185 /*
186 * It is optional to use GPIOs for sdhci Power control & sdhci card
187 * interrupt detection. If sdhci->data is NULL, then use original sdhci
188 * lines otherwise GPIO lines.
189 * If GPIO is selected for power control, then power should be disabled
190 * after card removal and should be enabled when card insertion
191 * interrupt occurs
192 */
193 if (!sdhci->data)
194 return 0;
195
196 if (sdhci->data->card_power_gpio >= 0) {
197 int val = 0;
198
6ebaf8f2
VK
199 ret = devm_gpio_request(&pdev->dev,
200 sdhci->data->card_power_gpio, "sdhci");
c63b3cba
VK
201 if (ret < 0) {
202 dev_dbg(&pdev->dev, "gpio request fail: %d\n",
203 sdhci->data->card_power_gpio);
6ebaf8f2 204 goto set_drvdata;
c63b3cba
VK
205 }
206
207 if (sdhci->data->power_always_enb)
208 val = sdhci->data->power_active_high;
209 else
210 val = !sdhci->data->power_active_high;
211
212 ret = gpio_direction_output(sdhci->data->card_power_gpio, val);
213 if (ret) {
214 dev_dbg(&pdev->dev, "gpio set direction fail: %d\n",
215 sdhci->data->card_power_gpio);
6ebaf8f2 216 goto set_drvdata;
c63b3cba 217 }
c63b3cba
VK
218 }
219
220 if (sdhci->data->card_int_gpio >= 0) {
6ebaf8f2
VK
221 ret = devm_gpio_request(&pdev->dev, sdhci->data->card_int_gpio,
222 "sdhci");
c63b3cba
VK
223 if (ret < 0) {
224 dev_dbg(&pdev->dev, "gpio request fail: %d\n",
225 sdhci->data->card_int_gpio);
6ebaf8f2 226 goto set_drvdata;
c63b3cba
VK
227 }
228
229 ret = gpio_direction_input(sdhci->data->card_int_gpio);
230 if (ret) {
231 dev_dbg(&pdev->dev, "gpio set direction fail: %d\n",
232 sdhci->data->card_int_gpio);
6ebaf8f2 233 goto set_drvdata;
c63b3cba 234 }
6ebaf8f2
VK
235 ret = devm_request_irq(&pdev->dev,
236 gpio_to_irq(sdhci->data->card_int_gpio),
c63b3cba
VK
237 sdhci_gpio_irq, IRQF_TRIGGER_LOW,
238 mmc_hostname(host->mmc), pdev);
239 if (ret) {
240 dev_dbg(&pdev->dev, "gpio request irq fail: %d\n",
241 sdhci->data->card_int_gpio);
6ebaf8f2 242 goto set_drvdata;
c63b3cba
VK
243 }
244
245 }
246
247 return 0;
248
6ebaf8f2 249set_drvdata:
c63b3cba 250 sdhci_remove_host(host, 1);
6ebaf8f2 251disable_clk:
da764f97 252 clk_disable_unprepare(sdhci->clk);
fcdb7c8f
RK
253err_host:
254 sdhci_free_host(host);
c63b3cba
VK
255err:
256 dev_err(&pdev->dev, "spear-sdhci probe failed: %d\n", ret);
257 return ret;
258}
259
6e0ee714 260static int sdhci_remove(struct platform_device *pdev)
c63b3cba
VK
261{
262 struct sdhci_host *host = platform_get_drvdata(pdev);
fcdb7c8f 263 struct spear_sdhci *sdhci = sdhci_priv(host);
6ebaf8f2 264 int dead = 0;
c63b3cba
VK
265 u32 scratch;
266
c63b3cba
VK
267 scratch = readl(host->ioaddr + SDHCI_INT_STATUS);
268 if (scratch == (u32)-1)
269 dead = 1;
270
271 sdhci_remove_host(host, dead);
da764f97 272 clk_disable_unprepare(sdhci->clk);
fcdb7c8f 273 sdhci_free_host(host);
c63b3cba
VK
274
275 return 0;
276}
277
b0dd099c 278#ifdef CONFIG_PM_SLEEP
b70a7fab
VK
279static int sdhci_suspend(struct device *dev)
280{
281 struct sdhci_host *host = dev_get_drvdata(dev);
fcdb7c8f 282 struct spear_sdhci *sdhci = sdhci_priv(host);
b70a7fab
VK
283 int ret;
284
984589e5 285 ret = sdhci_suspend_host(host);
b70a7fab 286 if (!ret)
06960a1b 287 clk_disable(sdhci->clk);
b70a7fab
VK
288
289 return ret;
290}
291
292static int sdhci_resume(struct device *dev)
293{
294 struct sdhci_host *host = dev_get_drvdata(dev);
fcdb7c8f 295 struct spear_sdhci *sdhci = sdhci_priv(host);
b70a7fab
VK
296 int ret;
297
06960a1b 298 ret = clk_enable(sdhci->clk);
b70a7fab
VK
299 if (ret) {
300 dev_dbg(dev, "Resume: Error enabling clock\n");
301 return ret;
302 }
303
304 return sdhci_resume_host(host);
305}
b70a7fab
VK
306#endif
307
4b1a6170
SH
308static SIMPLE_DEV_PM_OPS(sdhci_pm_ops, sdhci_suspend, sdhci_resume);
309
067bf748
VK
310#ifdef CONFIG_OF
311static const struct of_device_id sdhci_spear_id_table[] = {
312 { .compatible = "st,spear300-sdhci" },
313 {}
314};
315MODULE_DEVICE_TABLE(of, sdhci_spear_id_table);
316#endif
317
c63b3cba
VK
318static struct platform_driver sdhci_driver = {
319 .driver = {
320 .name = "sdhci",
321 .owner = THIS_MODULE,
b70a7fab 322 .pm = &sdhci_pm_ops,
067bf748 323 .of_match_table = of_match_ptr(sdhci_spear_id_table),
c63b3cba
VK
324 },
325 .probe = sdhci_probe,
0433c143 326 .remove = sdhci_remove,
c63b3cba
VK
327};
328
d1f81a64 329module_platform_driver(sdhci_driver);
c63b3cba
VK
330
331MODULE_DESCRIPTION("SPEAr Secure Digital Host Controller Interface driver");
10d8935f 332MODULE_AUTHOR("Viresh Kumar <viresh.linux@gmail.com>");
c63b3cba 333MODULE_LICENSE("GPL v2");
This page took 0.236927 seconds and 5 git commands to generate.