blob: a51e603acbc5e00d9317d6422388ccf88d4d3dd1 [file] [log] [blame]
Adrian Hunterc4e05032012-11-23 21:17:34 +01001/*
2 * Secure Digital Host Controller Interface ACPI driver.
3 *
4 * Copyright (c) 2012, Intel Corporation.
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms and conditions of the GNU General Public License,
8 * version 2, as published by the Free Software Foundation.
9 *
10 * This program is distributed in the hope it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * more details.
14 *
15 * You should have received a copy of the GNU General Public License along with
16 * this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 */
20
21#include <linux/init.h>
22#include <linux/export.h>
23#include <linux/module.h>
24#include <linux/device.h>
25#include <linux/platform_device.h>
26#include <linux/ioport.h>
27#include <linux/io.h>
28#include <linux/dma-mapping.h>
29#include <linux/compiler.h>
30#include <linux/stddef.h>
31#include <linux/bitops.h>
32#include <linux/types.h>
33#include <linux/err.h>
Adrian Huntera61abe62013-05-06 12:17:33 +030034#include <linux/gpio.h>
Adrian Hunterc4e05032012-11-23 21:17:34 +010035#include <linux/interrupt.h>
36#include <linux/acpi.h>
Adrian Huntera61abe62013-05-06 12:17:33 +030037#include <linux/acpi_gpio.h>
Adrian Hunterc4e05032012-11-23 21:17:34 +010038#include <linux/pm.h>
39#include <linux/pm_runtime.h>
40
41#include <linux/mmc/host.h>
42#include <linux/mmc/pm.h>
43#include <linux/mmc/sdhci.h>
44
45#include "sdhci.h"
46
47enum {
48 SDHCI_ACPI_SD_CD = BIT(0),
49 SDHCI_ACPI_RUNTIME_PM = BIT(1),
50};
51
52struct sdhci_acpi_chip {
53 const struct sdhci_ops *ops;
54 unsigned int quirks;
55 unsigned int quirks2;
56 unsigned long caps;
57 unsigned int caps2;
58 mmc_pm_flag_t pm_caps;
59};
60
61struct sdhci_acpi_slot {
62 const struct sdhci_acpi_chip *chip;
63 unsigned int quirks;
64 unsigned int quirks2;
65 unsigned long caps;
66 unsigned int caps2;
67 mmc_pm_flag_t pm_caps;
68 unsigned int flags;
69};
70
71struct sdhci_acpi_host {
72 struct sdhci_host *host;
73 const struct sdhci_acpi_slot *slot;
74 struct platform_device *pdev;
75 bool use_runtime_pm;
76};
77
78static inline bool sdhci_acpi_flag(struct sdhci_acpi_host *c, unsigned int flag)
79{
80 return c->slot && (c->slot->flags & flag);
81}
82
83static int sdhci_acpi_enable_dma(struct sdhci_host *host)
84{
85 return 0;
86}
87
88static const struct sdhci_ops sdhci_acpi_ops_dflt = {
89 .enable_dma = sdhci_acpi_enable_dma,
90};
91
Adrian Hunter07a58882013-04-26 11:27:22 +030092static const struct sdhci_acpi_slot sdhci_acpi_slot_int_emmc = {
93 .caps = MMC_CAP_8_BIT_DATA | MMC_CAP_NONREMOVABLE,
94 .caps2 = MMC_CAP2_HC_ERASE_SZ,
95 .flags = SDHCI_ACPI_RUNTIME_PM,
96};
97
Adrian Huntere5571392012-12-10 21:18:48 +010098static const struct sdhci_acpi_slot sdhci_acpi_slot_int_sdio = {
99 .quirks2 = SDHCI_QUIRK2_HOST_OFF_CARD_ON,
100 .caps = MMC_CAP_NONREMOVABLE | MMC_CAP_POWER_OFF_CARD,
101 .flags = SDHCI_ACPI_RUNTIME_PM,
102 .pm_caps = MMC_PM_KEEP_POWER,
103};
104
Adrian Hunter07a58882013-04-26 11:27:22 +0300105static const struct sdhci_acpi_slot sdhci_acpi_slot_int_sd = {
Adrian Huntera61abe62013-05-06 12:17:33 +0300106 .flags = SDHCI_ACPI_SD_CD | SDHCI_ACPI_RUNTIME_PM,
107 .quirks2 = SDHCI_QUIRK2_CARD_ON_NEEDS_BUS_ON,
Adrian Hunter07a58882013-04-26 11:27:22 +0300108};
109
110struct sdhci_acpi_uid_slot {
111 const char *hid;
112 const char *uid;
113 const struct sdhci_acpi_slot *slot;
114};
115
116static const struct sdhci_acpi_uid_slot sdhci_acpi_uids[] = {
117 { "80860F14" , "1" , &sdhci_acpi_slot_int_emmc },
118 { "80860F14" , "3" , &sdhci_acpi_slot_int_sd },
119 { "INT33BB" , "2" , &sdhci_acpi_slot_int_sdio },
120 { "INT33C6" , NULL, &sdhci_acpi_slot_int_sdio },
121 { "PNP0D40" },
122 { },
123};
124
Adrian Hunterc4e05032012-11-23 21:17:34 +0100125static const struct acpi_device_id sdhci_acpi_ids[] = {
Adrian Hunter07a58882013-04-26 11:27:22 +0300126 { "80860F14" },
127 { "INT33BB" },
128 { "INT33C6" },
129 { "PNP0D40" },
Adrian Hunterc4e05032012-11-23 21:17:34 +0100130 { },
131};
132MODULE_DEVICE_TABLE(acpi, sdhci_acpi_ids);
133
Adrian Hunter07a58882013-04-26 11:27:22 +0300134static const struct sdhci_acpi_slot *sdhci_acpi_get_slot_by_ids(const char *hid,
135 const char *uid)
Adrian Hunterc4e05032012-11-23 21:17:34 +0100136{
Adrian Hunter07a58882013-04-26 11:27:22 +0300137 const struct sdhci_acpi_uid_slot *u;
Adrian Hunterc4e05032012-11-23 21:17:34 +0100138
Adrian Hunter07a58882013-04-26 11:27:22 +0300139 for (u = sdhci_acpi_uids; u->hid; u++) {
140 if (strcmp(u->hid, hid))
141 continue;
142 if (!u->uid)
143 return u->slot;
144 if (uid && !strcmp(u->uid, uid))
145 return u->slot;
146 }
Adrian Hunterc4e05032012-11-23 21:17:34 +0100147 return NULL;
148}
149
Adrian Hunter07a58882013-04-26 11:27:22 +0300150static const struct sdhci_acpi_slot *sdhci_acpi_get_slot(acpi_handle handle,
151 const char *hid)
152{
153 const struct sdhci_acpi_slot *slot;
154 struct acpi_device_info *info;
155 const char *uid = NULL;
156 acpi_status status;
157
158 status = acpi_get_object_info(handle, &info);
159 if (!ACPI_FAILURE(status) && (info->valid & ACPI_VALID_UID))
160 uid = info->unique_id.string;
161
162 slot = sdhci_acpi_get_slot_by_ids(hid, uid);
163
164 kfree(info);
165 return slot;
166}
167
Adrian Huntera61abe62013-05-06 12:17:33 +0300168#ifdef CONFIG_PM_RUNTIME
169
170static irqreturn_t sdhci_acpi_sd_cd(int irq, void *dev_id)
171{
172 mmc_detect_change(dev_id, msecs_to_jiffies(200));
173 return IRQ_HANDLED;
174}
175
176static int sdhci_acpi_add_own_cd(struct device *dev, int gpio,
177 struct mmc_host *mmc)
178{
179 unsigned long flags;
180 int err, irq;
181
182 if (gpio < 0) {
183 err = gpio;
184 goto out;
185 }
186
187 err = devm_gpio_request_one(dev, gpio, GPIOF_DIR_IN, "sd_cd");
188 if (err)
189 goto out;
190
191 irq = gpio_to_irq(gpio);
192 if (irq < 0)
193 goto out_free;
194
195 flags = IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING;
196 err = devm_request_irq(dev, irq, sdhci_acpi_sd_cd, flags, "sd_cd", mmc);
197 if (err)
198 goto out_free;
199
200 return 0;
201
202out_free:
203 devm_gpio_free(dev, gpio);
204out:
205 dev_warn(dev, "failed to setup card detect wake up\n");
206 return err;
207}
208
209#else
210
211static int sdhci_acpi_add_own_cd(struct device *dev, int gpio,
212 struct mmc_host *mmc)
213{
214 return 0;
215}
216
217#endif
218
Greg Kroah-Hartman4e608e42012-12-21 15:05:47 -0800219static int sdhci_acpi_probe(struct platform_device *pdev)
Adrian Hunterc4e05032012-11-23 21:17:34 +0100220{
221 struct device *dev = &pdev->dev;
222 acpi_handle handle = ACPI_HANDLE(dev);
223 struct acpi_device *device;
224 struct sdhci_acpi_host *c;
225 struct sdhci_host *host;
226 struct resource *iomem;
227 resource_size_t len;
228 const char *hid;
Adrian Huntera61abe62013-05-06 12:17:33 +0300229 int err, gpio;
Adrian Hunterc4e05032012-11-23 21:17:34 +0100230
231 if (acpi_bus_get_device(handle, &device))
232 return -ENODEV;
233
234 if (acpi_bus_get_status(device) || !device->status.present)
235 return -ENODEV;
236
237 hid = acpi_device_hid(device);
238
239 iomem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
240 if (!iomem)
241 return -ENOMEM;
242
243 len = resource_size(iomem);
244 if (len < 0x100)
245 dev_err(dev, "Invalid iomem size!\n");
246
247 if (!devm_request_mem_region(dev, iomem->start, len, dev_name(dev)))
248 return -ENOMEM;
249
250 host = sdhci_alloc_host(dev, sizeof(struct sdhci_acpi_host));
251 if (IS_ERR(host))
252 return PTR_ERR(host);
253
Adrian Huntera61abe62013-05-06 12:17:33 +0300254 gpio = acpi_get_gpio_by_index(dev, 0, NULL);
255
Adrian Hunterc4e05032012-11-23 21:17:34 +0100256 c = sdhci_priv(host);
257 c->host = host;
Adrian Hunter07a58882013-04-26 11:27:22 +0300258 c->slot = sdhci_acpi_get_slot(handle, hid);
Adrian Hunterc4e05032012-11-23 21:17:34 +0100259 c->pdev = pdev;
260 c->use_runtime_pm = sdhci_acpi_flag(c, SDHCI_ACPI_RUNTIME_PM);
261
262 platform_set_drvdata(pdev, c);
263
264 host->hw_name = "ACPI";
265 host->ops = &sdhci_acpi_ops_dflt;
266 host->irq = platform_get_irq(pdev, 0);
267
268 host->ioaddr = devm_ioremap_nocache(dev, iomem->start,
269 resource_size(iomem));
270 if (host->ioaddr == NULL) {
271 err = -ENOMEM;
272 goto err_free;
273 }
274
275 if (!dev->dma_mask) {
276 u64 dma_mask;
277
278 if (sdhci_readl(host, SDHCI_CAPABILITIES) & SDHCI_CAN_64BIT) {
279 /* 64-bit DMA is not supported at present */
280 dma_mask = DMA_BIT_MASK(32);
281 } else {
282 dma_mask = DMA_BIT_MASK(32);
283 }
284
285 dev->dma_mask = &dev->coherent_dma_mask;
286 dev->coherent_dma_mask = dma_mask;
287 }
288
289 if (c->slot) {
290 if (c->slot->chip) {
291 host->ops = c->slot->chip->ops;
292 host->quirks |= c->slot->chip->quirks;
293 host->quirks2 |= c->slot->chip->quirks2;
294 host->mmc->caps |= c->slot->chip->caps;
295 host->mmc->caps2 |= c->slot->chip->caps2;
296 host->mmc->pm_caps |= c->slot->chip->pm_caps;
297 }
298 host->quirks |= c->slot->quirks;
299 host->quirks2 |= c->slot->quirks2;
300 host->mmc->caps |= c->slot->caps;
301 host->mmc->caps2 |= c->slot->caps2;
302 host->mmc->pm_caps |= c->slot->pm_caps;
303 }
304
Adrian Hunter0d3e3352013-04-04 16:41:06 +0300305 host->mmc->caps2 |= MMC_CAP2_NO_PRESCAN_POWERUP;
306
Adrian Hunterc4e05032012-11-23 21:17:34 +0100307 err = sdhci_add_host(host);
308 if (err)
309 goto err_free;
310
Adrian Huntera61abe62013-05-06 12:17:33 +0300311 if (sdhci_acpi_flag(c, SDHCI_ACPI_SD_CD)) {
312 if (sdhci_acpi_add_own_cd(dev, gpio, host->mmc))
313 c->use_runtime_pm = false;
314 }
315
Adrian Hunterc4e05032012-11-23 21:17:34 +0100316 if (c->use_runtime_pm) {
Adrian Hunter1d1ff452013-04-26 11:27:21 +0300317 pm_runtime_set_active(dev);
Adrian Hunterc4e05032012-11-23 21:17:34 +0100318 pm_suspend_ignore_children(dev, 1);
319 pm_runtime_set_autosuspend_delay(dev, 50);
320 pm_runtime_use_autosuspend(dev);
321 pm_runtime_enable(dev);
322 }
323
324 return 0;
325
326err_free:
Adrian Hunterc4e05032012-11-23 21:17:34 +0100327 sdhci_free_host(c->host);
328 return err;
329}
330
Greg Kroah-Hartman4e608e42012-12-21 15:05:47 -0800331static int sdhci_acpi_remove(struct platform_device *pdev)
Adrian Hunterc4e05032012-11-23 21:17:34 +0100332{
333 struct sdhci_acpi_host *c = platform_get_drvdata(pdev);
334 struct device *dev = &pdev->dev;
335 int dead;
336
337 if (c->use_runtime_pm) {
338 pm_runtime_get_sync(dev);
339 pm_runtime_disable(dev);
340 pm_runtime_put_noidle(dev);
341 }
342
343 dead = (sdhci_readl(c->host, SDHCI_INT_STATUS) == ~0);
344 sdhci_remove_host(c->host, dead);
Adrian Hunterc4e05032012-11-23 21:17:34 +0100345 sdhci_free_host(c->host);
346
347 return 0;
348}
349
350#ifdef CONFIG_PM_SLEEP
351
352static int sdhci_acpi_suspend(struct device *dev)
353{
354 struct sdhci_acpi_host *c = dev_get_drvdata(dev);
355
356 return sdhci_suspend_host(c->host);
357}
358
359static int sdhci_acpi_resume(struct device *dev)
360{
361 struct sdhci_acpi_host *c = dev_get_drvdata(dev);
362
363 return sdhci_resume_host(c->host);
364}
365
366#else
367
368#define sdhci_acpi_suspend NULL
369#define sdhci_acpi_resume NULL
370
371#endif
372
373#ifdef CONFIG_PM_RUNTIME
374
375static int sdhci_acpi_runtime_suspend(struct device *dev)
376{
377 struct sdhci_acpi_host *c = dev_get_drvdata(dev);
378
379 return sdhci_runtime_suspend_host(c->host);
380}
381
382static int sdhci_acpi_runtime_resume(struct device *dev)
383{
384 struct sdhci_acpi_host *c = dev_get_drvdata(dev);
385
386 return sdhci_runtime_resume_host(c->host);
387}
388
389static int sdhci_acpi_runtime_idle(struct device *dev)
390{
391 return 0;
392}
393
394#else
395
396#define sdhci_acpi_runtime_suspend NULL
397#define sdhci_acpi_runtime_resume NULL
398#define sdhci_acpi_runtime_idle NULL
399
400#endif
401
402static const struct dev_pm_ops sdhci_acpi_pm_ops = {
403 .suspend = sdhci_acpi_suspend,
404 .resume = sdhci_acpi_resume,
405 .runtime_suspend = sdhci_acpi_runtime_suspend,
406 .runtime_resume = sdhci_acpi_runtime_resume,
407 .runtime_idle = sdhci_acpi_runtime_idle,
408};
409
410static struct platform_driver sdhci_acpi_driver = {
411 .driver = {
412 .name = "sdhci-acpi",
413 .owner = THIS_MODULE,
414 .acpi_match_table = sdhci_acpi_ids,
415 .pm = &sdhci_acpi_pm_ops,
416 },
417 .probe = sdhci_acpi_probe,
Greg Kroah-Hartman4e608e42012-12-21 15:05:47 -0800418 .remove = sdhci_acpi_remove,
Adrian Hunterc4e05032012-11-23 21:17:34 +0100419};
420
421module_platform_driver(sdhci_acpi_driver);
422
423MODULE_DESCRIPTION("Secure Digital Host Controller Interface ACPI driver");
424MODULE_AUTHOR("Adrian Hunter");
425MODULE_LICENSE("GPL v2");