blob: 394714f71b2f6522147814e460ba8b857578c466 [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 */
Lars-Peter Clausenc9155682013-03-13 19:26:05 +010039static const struct sdhci_ops sdhci_pltfm_ops = {
Viresh KUMARc63b3cb2010-05-26 14:42:10 -070040 /* 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
Bill Pembertonc3be1ef2012-11-19 13:23:06 -050074static struct sdhci_plat_data *sdhci_probe_config_dt(struct platform_device *pdev)
Viresh Kumar067bf742012-09-28 15:58:22 +053075{
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);
Sachin Kamateab36b22014-02-25 15:18:21 +053087 if (!pdata)
Viresh Kumar067bf742012-09-28 15:58:22 +053088 dev_err(&pdev->dev, "DT: kzalloc failed\n");
Sachin Kamateab36b22014-02-25 15:18:21 +053089 else
90 pdata->card_int_gpio = cd_gpio;
Viresh Kumar067bf742012-09-28 15:58:22 +053091 }
92
Viresh Kumar067bf742012-09-28 15:58:22 +053093 return pdata;
94}
95#else
Bill Pembertonc3be1ef2012-11-19 13:23:06 -050096static struct sdhci_plat_data *sdhci_probe_config_dt(struct platform_device *pdev)
Viresh Kumar067bf742012-09-28 15:58:22 +053097{
98 return ERR_PTR(-ENOSYS);
99}
100#endif
101
Bill Pembertonc3be1ef2012-11-19 13:23:06 -0500102static int sdhci_probe(struct platform_device *pdev)
Viresh KUMARc63b3cb2010-05-26 14:42:10 -0700103{
Viresh Kumar067bf742012-09-28 15:58:22 +0530104 struct device_node *np = pdev->dev.of_node;
Viresh KUMARc63b3cb2010-05-26 14:42:10 -0700105 struct sdhci_host *host;
106 struct resource *iomem;
107 struct spear_sdhci *sdhci;
108 int ret;
109
Viresh KUMARc63b3cb2010-05-26 14:42:10 -0700110 iomem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
111 if (!iomem) {
112 ret = -ENOMEM;
113 dev_dbg(&pdev->dev, "memory resource not defined\n");
114 goto err;
115 }
116
Viresh Kumar6ebaf8f2012-03-27 08:40:35 +0530117 if (!devm_request_mem_region(&pdev->dev, iomem->start,
118 resource_size(iomem), "spear-sdhci")) {
Viresh KUMARc63b3cb2010-05-26 14:42:10 -0700119 ret = -EBUSY;
120 dev_dbg(&pdev->dev, "cannot request region\n");
121 goto err;
122 }
123
Viresh Kumar6ebaf8f2012-03-27 08:40:35 +0530124 sdhci = devm_kzalloc(&pdev->dev, sizeof(*sdhci), GFP_KERNEL);
Viresh KUMARc63b3cb2010-05-26 14:42:10 -0700125 if (!sdhci) {
126 ret = -ENOMEM;
127 dev_dbg(&pdev->dev, "cannot allocate memory for sdhci\n");
Viresh Kumar6ebaf8f2012-03-27 08:40:35 +0530128 goto err;
Viresh KUMARc63b3cb2010-05-26 14:42:10 -0700129 }
130
131 /* clk enable */
Russell King142dbab92014-02-28 21:32:29 +0000132 sdhci->clk = devm_clk_get(&pdev->dev, NULL);
Viresh KUMARc63b3cb2010-05-26 14:42:10 -0700133 if (IS_ERR(sdhci->clk)) {
134 ret = PTR_ERR(sdhci->clk);
135 dev_dbg(&pdev->dev, "Error getting clock\n");
Viresh Kumar6ebaf8f2012-03-27 08:40:35 +0530136 goto err;
Viresh KUMARc63b3cb2010-05-26 14:42:10 -0700137 }
138
Viresh Kumarda764f92012-09-28 15:58:23 +0530139 ret = clk_prepare_enable(sdhci->clk);
Viresh KUMARc63b3cb2010-05-26 14:42:10 -0700140 if (ret) {
141 dev_dbg(&pdev->dev, "Error enabling clock\n");
Russell King142dbab92014-02-28 21:32:29 +0000142 goto err;
Viresh KUMARc63b3cb2010-05-26 14:42:10 -0700143 }
144
Vipul Kumar Samar257f9df2012-11-08 20:39:09 +0530145 ret = clk_set_rate(sdhci->clk, 50000000);
146 if (ret)
147 dev_dbg(&pdev->dev, "Error setting desired clk, clk=%lu\n",
148 clk_get_rate(sdhci->clk));
149
Viresh Kumar067bf742012-09-28 15:58:22 +0530150 if (np) {
151 sdhci->data = sdhci_probe_config_dt(pdev);
152 if (IS_ERR(sdhci->data)) {
153 dev_err(&pdev->dev, "DT: Failed to get pdata\n");
Russell King142dbab92014-02-28 21:32:29 +0000154 goto disable_clk;
Viresh Kumar067bf742012-09-28 15:58:22 +0530155 }
156 } else {
157 sdhci->data = dev_get_platdata(&pdev->dev);
158 }
159
Viresh KUMARc63b3cb2010-05-26 14:42:10 -0700160 pdev->dev.platform_data = sdhci;
161
162 if (pdev->dev.parent)
163 host = sdhci_alloc_host(pdev->dev.parent, 0);
164 else
165 host = sdhci_alloc_host(&pdev->dev, 0);
166
167 if (IS_ERR(host)) {
168 ret = PTR_ERR(host);
169 dev_dbg(&pdev->dev, "error allocating host\n");
Viresh Kumar6ebaf8f2012-03-27 08:40:35 +0530170 goto disable_clk;
Viresh KUMARc63b3cb2010-05-26 14:42:10 -0700171 }
172
173 host->hw_name = "sdhci";
174 host->ops = &sdhci_pltfm_ops;
175 host->irq = platform_get_irq(pdev, 0);
176 host->quirks = SDHCI_QUIRK_BROKEN_ADMA;
177
Viresh Kumar6ebaf8f2012-03-27 08:40:35 +0530178 host->ioaddr = devm_ioremap(&pdev->dev, iomem->start,
179 resource_size(iomem));
Viresh KUMARc63b3cb2010-05-26 14:42:10 -0700180 if (!host->ioaddr) {
181 ret = -ENOMEM;
182 dev_dbg(&pdev->dev, "failed to remap registers\n");
Viresh Kumar6ebaf8f2012-03-27 08:40:35 +0530183 goto free_host;
Viresh KUMARc63b3cb2010-05-26 14:42:10 -0700184 }
185
186 ret = sdhci_add_host(host);
187 if (ret) {
188 dev_dbg(&pdev->dev, "error adding host\n");
Viresh Kumar6ebaf8f2012-03-27 08:40:35 +0530189 goto free_host;
Viresh KUMARc63b3cb2010-05-26 14:42:10 -0700190 }
191
192 platform_set_drvdata(pdev, host);
193
194 /*
195 * It is optional to use GPIOs for sdhci Power control & sdhci card
196 * interrupt detection. If sdhci->data is NULL, then use original sdhci
197 * lines otherwise GPIO lines.
198 * If GPIO is selected for power control, then power should be disabled
199 * after card removal and should be enabled when card insertion
200 * interrupt occurs
201 */
202 if (!sdhci->data)
203 return 0;
204
205 if (sdhci->data->card_power_gpio >= 0) {
206 int val = 0;
207
Viresh Kumar6ebaf8f2012-03-27 08:40:35 +0530208 ret = devm_gpio_request(&pdev->dev,
209 sdhci->data->card_power_gpio, "sdhci");
Viresh KUMARc63b3cb2010-05-26 14:42:10 -0700210 if (ret < 0) {
211 dev_dbg(&pdev->dev, "gpio request fail: %d\n",
212 sdhci->data->card_power_gpio);
Viresh Kumar6ebaf8f2012-03-27 08:40:35 +0530213 goto set_drvdata;
Viresh KUMARc63b3cb2010-05-26 14:42:10 -0700214 }
215
216 if (sdhci->data->power_always_enb)
217 val = sdhci->data->power_active_high;
218 else
219 val = !sdhci->data->power_active_high;
220
221 ret = gpio_direction_output(sdhci->data->card_power_gpio, val);
222 if (ret) {
223 dev_dbg(&pdev->dev, "gpio set direction fail: %d\n",
224 sdhci->data->card_power_gpio);
Viresh Kumar6ebaf8f2012-03-27 08:40:35 +0530225 goto set_drvdata;
Viresh KUMARc63b3cb2010-05-26 14:42:10 -0700226 }
Viresh KUMARc63b3cb2010-05-26 14:42:10 -0700227 }
228
229 if (sdhci->data->card_int_gpio >= 0) {
Viresh Kumar6ebaf8f2012-03-27 08:40:35 +0530230 ret = devm_gpio_request(&pdev->dev, sdhci->data->card_int_gpio,
231 "sdhci");
Viresh KUMARc63b3cb2010-05-26 14:42:10 -0700232 if (ret < 0) {
233 dev_dbg(&pdev->dev, "gpio request fail: %d\n",
234 sdhci->data->card_int_gpio);
Viresh Kumar6ebaf8f2012-03-27 08:40:35 +0530235 goto set_drvdata;
Viresh KUMARc63b3cb2010-05-26 14:42:10 -0700236 }
237
238 ret = gpio_direction_input(sdhci->data->card_int_gpio);
239 if (ret) {
240 dev_dbg(&pdev->dev, "gpio set direction fail: %d\n",
241 sdhci->data->card_int_gpio);
Viresh Kumar6ebaf8f2012-03-27 08:40:35 +0530242 goto set_drvdata;
Viresh KUMARc63b3cb2010-05-26 14:42:10 -0700243 }
Viresh Kumar6ebaf8f2012-03-27 08:40:35 +0530244 ret = devm_request_irq(&pdev->dev,
245 gpio_to_irq(sdhci->data->card_int_gpio),
Viresh KUMARc63b3cb2010-05-26 14:42:10 -0700246 sdhci_gpio_irq, IRQF_TRIGGER_LOW,
247 mmc_hostname(host->mmc), pdev);
248 if (ret) {
249 dev_dbg(&pdev->dev, "gpio request irq fail: %d\n",
250 sdhci->data->card_int_gpio);
Viresh Kumar6ebaf8f2012-03-27 08:40:35 +0530251 goto set_drvdata;
Viresh KUMARc63b3cb2010-05-26 14:42:10 -0700252 }
253
254 }
255
256 return 0;
257
Viresh Kumar6ebaf8f2012-03-27 08:40:35 +0530258set_drvdata:
Viresh KUMARc63b3cb2010-05-26 14:42:10 -0700259 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 Kumarda764f92012-09-28 15:58:23 +0530263 clk_disable_unprepare(sdhci->clk);
Viresh KUMARc63b3cb2010-05-26 14:42:10 -0700264err:
265 dev_err(&pdev->dev, "spear-sdhci probe failed: %d\n", ret);
266 return ret;
267}
268
Bill Pemberton6e0ee712012-11-19 13:26:03 -0500269static int sdhci_remove(struct platform_device *pdev)
Viresh KUMARc63b3cb2010-05-26 14:42:10 -0700270{
271 struct sdhci_host *host = platform_get_drvdata(pdev);
Viresh KUMARc63b3cb2010-05-26 14:42:10 -0700272 struct spear_sdhci *sdhci = dev_get_platdata(&pdev->dev);
Viresh Kumar6ebaf8f2012-03-27 08:40:35 +0530273 int dead = 0;
Viresh KUMARc63b3cb2010-05-26 14:42:10 -0700274 u32 scratch;
275
Viresh KUMARc63b3cb2010-05-26 14:42:10 -0700276 scratch = readl(host->ioaddr + SDHCI_INT_STATUS);
277 if (scratch == (u32)-1)
278 dead = 1;
279
280 sdhci_remove_host(host, dead);
Viresh KUMARc63b3cb2010-05-26 14:42:10 -0700281 sdhci_free_host(host);
Viresh Kumarda764f92012-09-28 15:58:23 +0530282 clk_disable_unprepare(sdhci->clk);
Viresh KUMARc63b3cb2010-05-26 14:42:10 -0700283
284 return 0;
285}
286
Jingoo Hanb0dd0992013-03-29 16:08:00 +0900287#ifdef CONFIG_PM_SLEEP
Viresh Kumarb70a7fa2011-11-15 16:24:40 +0530288static int sdhci_suspend(struct device *dev)
289{
290 struct sdhci_host *host = dev_get_drvdata(dev);
291 struct spear_sdhci *sdhci = dev_get_platdata(dev);
Viresh Kumarb70a7fa2011-11-15 16:24:40 +0530292 int ret;
293
Viresh Kumar984589e2012-01-04 11:48:42 +0530294 ret = sdhci_suspend_host(host);
Viresh Kumarb70a7fa2011-11-15 16:24:40 +0530295 if (!ret)
Viresh Kumar06960a12012-11-08 20:39:10 +0530296 clk_disable(sdhci->clk);
Viresh Kumarb70a7fa2011-11-15 16:24:40 +0530297
298 return ret;
299}
300
301static int sdhci_resume(struct device *dev)
302{
303 struct sdhci_host *host = dev_get_drvdata(dev);
304 struct spear_sdhci *sdhci = dev_get_platdata(dev);
305 int ret;
306
Viresh Kumar06960a12012-11-08 20:39:10 +0530307 ret = clk_enable(sdhci->clk);
Viresh Kumarb70a7fa2011-11-15 16:24:40 +0530308 if (ret) {
309 dev_dbg(dev, "Resume: Error enabling clock\n");
310 return ret;
311 }
312
313 return sdhci_resume_host(host);
314}
Viresh Kumarb70a7fa2011-11-15 16:24:40 +0530315#endif
316
Shiraz Hashim4b1a6172012-02-24 11:55:35 +0530317static SIMPLE_DEV_PM_OPS(sdhci_pm_ops, sdhci_suspend, sdhci_resume);
318
Viresh Kumar067bf742012-09-28 15:58:22 +0530319#ifdef CONFIG_OF
320static const struct of_device_id sdhci_spear_id_table[] = {
321 { .compatible = "st,spear300-sdhci" },
322 {}
323};
324MODULE_DEVICE_TABLE(of, sdhci_spear_id_table);
325#endif
326
Viresh KUMARc63b3cb2010-05-26 14:42:10 -0700327static struct platform_driver sdhci_driver = {
328 .driver = {
329 .name = "sdhci",
330 .owner = THIS_MODULE,
Viresh Kumarb70a7fa2011-11-15 16:24:40 +0530331 .pm = &sdhci_pm_ops,
Viresh Kumar067bf742012-09-28 15:58:22 +0530332 .of_match_table = of_match_ptr(sdhci_spear_id_table),
Viresh KUMARc63b3cb2010-05-26 14:42:10 -0700333 },
334 .probe = sdhci_probe,
Bill Pemberton0433c142012-11-19 13:20:26 -0500335 .remove = sdhci_remove,
Viresh KUMARc63b3cb2010-05-26 14:42:10 -0700336};
337
Axel Lind1f81a62011-11-26 12:55:43 +0800338module_platform_driver(sdhci_driver);
Viresh KUMARc63b3cb2010-05-26 14:42:10 -0700339
340MODULE_DESCRIPTION("SPEAr Secure Digital Host Controller Interface driver");
Viresh Kumar10d89352012-06-20 12:53:02 -0700341MODULE_AUTHOR("Viresh Kumar <viresh.linux@gmail.com>");
Viresh KUMARc63b3cb2010-05-26 14:42:10 -0700342MODULE_LICENSE("GPL v2");