blob: ecad282741d69a0cb28a6196f68fc36a7b7a9c4f [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
Viresh Kumar10d89352012-06-20 12:53:02 -07007 * Viresh Kumar <viresh.linux@gmail.com>
Viresh KUMARc63b3cb2010-05-26 14:42:10 -07008 *
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 Gortmaker88b47672011-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>
Viresh Kumar067bf742012-09-28 15:58:22 +053023#include <linux/of.h>
24#include <linux/of_gpio.h>
Viresh KUMARc63b3cb2010-05-26 14:42:10 -070025#include <linux/platform_device.h>
Viresh Kumarb70a7fa2011-11-15 16:24:40 +053026#include <linux/pm.h>
Viresh KUMARc63b3cb2010-05-26 14:42:10 -070027#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 */
39static struct sdhci_ops sdhci_pltfm_ops = {
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;
Thomas Gleixnerdced35a2011-03-28 17:49:12 +020057 irq_set_irq_type(irq, gpio_irq_type);
Viresh KUMARc63b3cb2010-05-26 14:42:10 -070058
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
Viresh Kumar067bf742012-09-28 15:58:22 +053073#ifdef CONFIG_OF
74static struct sdhci_plat_data * __devinit
75sdhci_probe_config_dt(struct platform_device *pdev)
76{
77 struct device_node *np = pdev->dev.of_node;
78 struct sdhci_plat_data *pdata = NULL;
79 int cd_gpio;
80
81 cd_gpio = of_get_named_gpio(np, "cd-gpios", 0);
82 if (!gpio_is_valid(cd_gpio))
83 cd_gpio = -1;
84
85 /* If pdata is required */
86 if (cd_gpio != -1) {
87 pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
88 if (!pdata) {
89 dev_err(&pdev->dev, "DT: kzalloc failed\n");
90 return ERR_PTR(-ENOMEM);
91 }
92 }
93
94 pdata->card_int_gpio = cd_gpio;
95
96 return pdata;
97}
98#else
99static struct sdhci_plat_data * __devinit
100sdhci_probe_config_dt(struct platform_device *pdev)
101{
102 return ERR_PTR(-ENOSYS);
103}
104#endif
105
Viresh KUMARc63b3cb2010-05-26 14:42:10 -0700106static int __devinit sdhci_probe(struct platform_device *pdev)
107{
Viresh Kumar067bf742012-09-28 15:58:22 +0530108 struct device_node *np = pdev->dev.of_node;
Viresh KUMARc63b3cb2010-05-26 14:42:10 -0700109 struct sdhci_host *host;
110 struct resource *iomem;
111 struct spear_sdhci *sdhci;
112 int ret;
113
Viresh KUMARc63b3cb2010-05-26 14:42:10 -0700114 iomem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
115 if (!iomem) {
116 ret = -ENOMEM;
117 dev_dbg(&pdev->dev, "memory resource not defined\n");
118 goto err;
119 }
120
Viresh Kumar6ebaf8f2012-03-27 08:40:35 +0530121 if (!devm_request_mem_region(&pdev->dev, iomem->start,
122 resource_size(iomem), "spear-sdhci")) {
Viresh KUMARc63b3cb2010-05-26 14:42:10 -0700123 ret = -EBUSY;
124 dev_dbg(&pdev->dev, "cannot request region\n");
125 goto err;
126 }
127
Viresh Kumar6ebaf8f2012-03-27 08:40:35 +0530128 sdhci = devm_kzalloc(&pdev->dev, sizeof(*sdhci), GFP_KERNEL);
Viresh KUMARc63b3cb2010-05-26 14:42:10 -0700129 if (!sdhci) {
130 ret = -ENOMEM;
131 dev_dbg(&pdev->dev, "cannot allocate memory for sdhci\n");
Viresh Kumar6ebaf8f2012-03-27 08:40:35 +0530132 goto err;
Viresh KUMARc63b3cb2010-05-26 14:42:10 -0700133 }
134
135 /* clk enable */
136 sdhci->clk = clk_get(&pdev->dev, NULL);
137 if (IS_ERR(sdhci->clk)) {
138 ret = PTR_ERR(sdhci->clk);
139 dev_dbg(&pdev->dev, "Error getting clock\n");
Viresh Kumar6ebaf8f2012-03-27 08:40:35 +0530140 goto err;
Viresh KUMARc63b3cb2010-05-26 14:42:10 -0700141 }
142
143 ret = clk_enable(sdhci->clk);
144 if (ret) {
145 dev_dbg(&pdev->dev, "Error enabling clock\n");
Viresh Kumar6ebaf8f2012-03-27 08:40:35 +0530146 goto put_clk;
Viresh KUMARc63b3cb2010-05-26 14:42:10 -0700147 }
148
Viresh Kumar067bf742012-09-28 15:58:22 +0530149 if (np) {
150 sdhci->data = sdhci_probe_config_dt(pdev);
151 if (IS_ERR(sdhci->data)) {
152 dev_err(&pdev->dev, "DT: Failed to get pdata\n");
153 return -ENODEV;
154 }
155 } else {
156 sdhci->data = dev_get_platdata(&pdev->dev);
157 }
158
Viresh KUMARc63b3cb2010-05-26 14:42:10 -0700159 pdev->dev.platform_data = sdhci;
160
161 if (pdev->dev.parent)
162 host = sdhci_alloc_host(pdev->dev.parent, 0);
163 else
164 host = sdhci_alloc_host(&pdev->dev, 0);
165
166 if (IS_ERR(host)) {
167 ret = PTR_ERR(host);
168 dev_dbg(&pdev->dev, "error allocating host\n");
Viresh Kumar6ebaf8f2012-03-27 08:40:35 +0530169 goto disable_clk;
Viresh KUMARc63b3cb2010-05-26 14:42:10 -0700170 }
171
172 host->hw_name = "sdhci";
173 host->ops = &sdhci_pltfm_ops;
174 host->irq = platform_get_irq(pdev, 0);
175 host->quirks = SDHCI_QUIRK_BROKEN_ADMA;
176
Viresh Kumar6ebaf8f2012-03-27 08:40:35 +0530177 host->ioaddr = devm_ioremap(&pdev->dev, iomem->start,
178 resource_size(iomem));
Viresh KUMARc63b3cb2010-05-26 14:42:10 -0700179 if (!host->ioaddr) {
180 ret = -ENOMEM;
181 dev_dbg(&pdev->dev, "failed to remap registers\n");
Viresh Kumar6ebaf8f2012-03-27 08:40:35 +0530182 goto free_host;
Viresh KUMARc63b3cb2010-05-26 14:42:10 -0700183 }
184
185 ret = sdhci_add_host(host);
186 if (ret) {
187 dev_dbg(&pdev->dev, "error adding host\n");
Viresh Kumar6ebaf8f2012-03-27 08:40:35 +0530188 goto free_host;
Viresh KUMARc63b3cb2010-05-26 14:42:10 -0700189 }
190
191 platform_set_drvdata(pdev, host);
192
193 /*
194 * It is optional to use GPIOs for sdhci Power control & sdhci card
195 * interrupt detection. If sdhci->data is NULL, then use original sdhci
196 * lines otherwise GPIO lines.
197 * If GPIO is selected for power control, then power should be disabled
198 * after card removal and should be enabled when card insertion
199 * interrupt occurs
200 */
201 if (!sdhci->data)
202 return 0;
203
204 if (sdhci->data->card_power_gpio >= 0) {
205 int val = 0;
206
Viresh Kumar6ebaf8f2012-03-27 08:40:35 +0530207 ret = devm_gpio_request(&pdev->dev,
208 sdhci->data->card_power_gpio, "sdhci");
Viresh KUMARc63b3cb2010-05-26 14:42:10 -0700209 if (ret < 0) {
210 dev_dbg(&pdev->dev, "gpio request fail: %d\n",
211 sdhci->data->card_power_gpio);
Viresh Kumar6ebaf8f2012-03-27 08:40:35 +0530212 goto set_drvdata;
Viresh KUMARc63b3cb2010-05-26 14:42:10 -0700213 }
214
215 if (sdhci->data->power_always_enb)
216 val = sdhci->data->power_active_high;
217 else
218 val = !sdhci->data->power_active_high;
219
220 ret = gpio_direction_output(sdhci->data->card_power_gpio, val);
221 if (ret) {
222 dev_dbg(&pdev->dev, "gpio set direction fail: %d\n",
223 sdhci->data->card_power_gpio);
Viresh Kumar6ebaf8f2012-03-27 08:40:35 +0530224 goto set_drvdata;
Viresh KUMARc63b3cb2010-05-26 14:42:10 -0700225 }
Viresh KUMARc63b3cb2010-05-26 14:42:10 -0700226 }
227
228 if (sdhci->data->card_int_gpio >= 0) {
Viresh Kumar6ebaf8f2012-03-27 08:40:35 +0530229 ret = devm_gpio_request(&pdev->dev, sdhci->data->card_int_gpio,
230 "sdhci");
Viresh KUMARc63b3cb2010-05-26 14:42:10 -0700231 if (ret < 0) {
232 dev_dbg(&pdev->dev, "gpio request fail: %d\n",
233 sdhci->data->card_int_gpio);
Viresh Kumar6ebaf8f2012-03-27 08:40:35 +0530234 goto set_drvdata;
Viresh KUMARc63b3cb2010-05-26 14:42:10 -0700235 }
236
237 ret = gpio_direction_input(sdhci->data->card_int_gpio);
238 if (ret) {
239 dev_dbg(&pdev->dev, "gpio set direction fail: %d\n",
240 sdhci->data->card_int_gpio);
Viresh Kumar6ebaf8f2012-03-27 08:40:35 +0530241 goto set_drvdata;
Viresh KUMARc63b3cb2010-05-26 14:42:10 -0700242 }
Viresh Kumar6ebaf8f2012-03-27 08:40:35 +0530243 ret = devm_request_irq(&pdev->dev,
244 gpio_to_irq(sdhci->data->card_int_gpio),
Viresh KUMARc63b3cb2010-05-26 14:42:10 -0700245 sdhci_gpio_irq, IRQF_TRIGGER_LOW,
246 mmc_hostname(host->mmc), pdev);
247 if (ret) {
248 dev_dbg(&pdev->dev, "gpio request irq fail: %d\n",
249 sdhci->data->card_int_gpio);
Viresh Kumar6ebaf8f2012-03-27 08:40:35 +0530250 goto set_drvdata;
Viresh KUMARc63b3cb2010-05-26 14:42:10 -0700251 }
252
253 }
254
255 return 0;
256
Viresh Kumar6ebaf8f2012-03-27 08:40:35 +0530257set_drvdata:
Viresh KUMARc63b3cb2010-05-26 14:42:10 -0700258 platform_set_drvdata(pdev, NULL);
259 sdhci_remove_host(host, 1);
Viresh Kumar6ebaf8f2012-03-27 08:40:35 +0530260free_host:
Viresh KUMARc63b3cb2010-05-26 14:42:10 -0700261 sdhci_free_host(host);
Viresh Kumar6ebaf8f2012-03-27 08:40:35 +0530262disable_clk:
Viresh KUMARc63b3cb2010-05-26 14:42:10 -0700263 clk_disable(sdhci->clk);
Viresh Kumar6ebaf8f2012-03-27 08:40:35 +0530264put_clk:
Viresh KUMARc63b3cb2010-05-26 14:42:10 -0700265 clk_put(sdhci->clk);
Viresh KUMARc63b3cb2010-05-26 14:42:10 -0700266err:
267 dev_err(&pdev->dev, "spear-sdhci probe failed: %d\n", ret);
268 return ret;
269}
270
271static int __devexit sdhci_remove(struct platform_device *pdev)
272{
273 struct sdhci_host *host = platform_get_drvdata(pdev);
Viresh KUMARc63b3cb2010-05-26 14:42:10 -0700274 struct spear_sdhci *sdhci = dev_get_platdata(&pdev->dev);
Viresh Kumar6ebaf8f2012-03-27 08:40:35 +0530275 int dead = 0;
Viresh KUMARc63b3cb2010-05-26 14:42:10 -0700276 u32 scratch;
277
Viresh KUMARc63b3cb2010-05-26 14:42:10 -0700278 platform_set_drvdata(pdev, NULL);
Viresh KUMARc63b3cb2010-05-26 14:42:10 -0700279 scratch = readl(host->ioaddr + SDHCI_INT_STATUS);
280 if (scratch == (u32)-1)
281 dead = 1;
282
283 sdhci_remove_host(host, dead);
Viresh KUMARc63b3cb2010-05-26 14:42:10 -0700284 sdhci_free_host(host);
285 clk_disable(sdhci->clk);
286 clk_put(sdhci->clk);
Viresh KUMARc63b3cb2010-05-26 14:42:10 -0700287
288 return 0;
289}
290
Viresh Kumarb70a7fa2011-11-15 16:24:40 +0530291#ifdef CONFIG_PM
292static int sdhci_suspend(struct device *dev)
293{
294 struct sdhci_host *host = dev_get_drvdata(dev);
295 struct spear_sdhci *sdhci = dev_get_platdata(dev);
Viresh Kumarb70a7fa2011-11-15 16:24:40 +0530296 int ret;
297
Viresh Kumar984589e2012-01-04 11:48:42 +0530298 ret = sdhci_suspend_host(host);
Viresh Kumarb70a7fa2011-11-15 16:24:40 +0530299 if (!ret)
300 clk_disable(sdhci->clk);
301
302 return ret;
303}
304
305static int sdhci_resume(struct device *dev)
306{
307 struct sdhci_host *host = dev_get_drvdata(dev);
308 struct spear_sdhci *sdhci = dev_get_platdata(dev);
309 int ret;
310
311 ret = clk_enable(sdhci->clk);
312 if (ret) {
313 dev_dbg(dev, "Resume: Error enabling clock\n");
314 return ret;
315 }
316
317 return sdhci_resume_host(host);
318}
Viresh Kumarb70a7fa2011-11-15 16:24:40 +0530319#endif
320
Shiraz Hashim4b1a6172012-02-24 11:55:35 +0530321static SIMPLE_DEV_PM_OPS(sdhci_pm_ops, sdhci_suspend, sdhci_resume);
322
Viresh Kumar067bf742012-09-28 15:58:22 +0530323#ifdef CONFIG_OF
324static const struct of_device_id sdhci_spear_id_table[] = {
325 { .compatible = "st,spear300-sdhci" },
326 {}
327};
328MODULE_DEVICE_TABLE(of, sdhci_spear_id_table);
329#endif
330
Viresh KUMARc63b3cb2010-05-26 14:42:10 -0700331static struct platform_driver sdhci_driver = {
332 .driver = {
333 .name = "sdhci",
334 .owner = THIS_MODULE,
Viresh Kumarb70a7fa2011-11-15 16:24:40 +0530335 .pm = &sdhci_pm_ops,
Viresh Kumar067bf742012-09-28 15:58:22 +0530336 .of_match_table = of_match_ptr(sdhci_spear_id_table),
Viresh KUMARc63b3cb2010-05-26 14:42:10 -0700337 },
338 .probe = sdhci_probe,
339 .remove = __devexit_p(sdhci_remove),
340};
341
Axel Lind1f81a62011-11-26 12:55:43 +0800342module_platform_driver(sdhci_driver);
Viresh KUMARc63b3cb2010-05-26 14:42:10 -0700343
344MODULE_DESCRIPTION("SPEAr Secure Digital Host Controller Interface driver");
Viresh Kumar10d89352012-06-20 12:53:02 -0700345MODULE_AUTHOR("Viresh Kumar <viresh.linux@gmail.com>");
Viresh KUMARc63b3cb2010-05-26 14:42:10 -0700346MODULE_LICENSE("GPL v2");