xref: /openbmc/linux/drivers/mmc/host/sdhci-acpi.c (revision 68198dca)
1 /*
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>
34 #include <linux/interrupt.h>
35 #include <linux/acpi.h>
36 #include <linux/pm.h>
37 #include <linux/pm_runtime.h>
38 #include <linux/delay.h>
39 
40 #include <linux/mmc/host.h>
41 #include <linux/mmc/pm.h>
42 #include <linux/mmc/slot-gpio.h>
43 
44 #ifdef CONFIG_X86
45 #include <asm/cpu_device_id.h>
46 #include <asm/intel-family.h>
47 #include <asm/iosf_mbi.h>
48 #include <linux/pci.h>
49 #endif
50 
51 #include "sdhci.h"
52 
53 enum {
54 	SDHCI_ACPI_SD_CD		= BIT(0),
55 	SDHCI_ACPI_RUNTIME_PM		= BIT(1),
56 	SDHCI_ACPI_SD_CD_OVERRIDE_LEVEL	= BIT(2),
57 };
58 
59 struct sdhci_acpi_chip {
60 	const struct	sdhci_ops *ops;
61 	unsigned int	quirks;
62 	unsigned int	quirks2;
63 	unsigned long	caps;
64 	unsigned int	caps2;
65 	mmc_pm_flag_t	pm_caps;
66 };
67 
68 struct sdhci_acpi_slot {
69 	const struct	sdhci_acpi_chip *chip;
70 	unsigned int	quirks;
71 	unsigned int	quirks2;
72 	unsigned long	caps;
73 	unsigned int	caps2;
74 	mmc_pm_flag_t	pm_caps;
75 	unsigned int	flags;
76 	size_t		priv_size;
77 	int (*probe_slot)(struct platform_device *, const char *, const char *);
78 	int (*remove_slot)(struct platform_device *);
79 };
80 
81 struct sdhci_acpi_host {
82 	struct sdhci_host		*host;
83 	const struct sdhci_acpi_slot	*slot;
84 	struct platform_device		*pdev;
85 	bool				use_runtime_pm;
86 	unsigned long			private[0] ____cacheline_aligned;
87 };
88 
89 static inline void *sdhci_acpi_priv(struct sdhci_acpi_host *c)
90 {
91 	return (void *)c->private;
92 }
93 
94 static inline bool sdhci_acpi_flag(struct sdhci_acpi_host *c, unsigned int flag)
95 {
96 	return c->slot && (c->slot->flags & flag);
97 }
98 
99 enum {
100 	INTEL_DSM_FNS		=  0,
101 	INTEL_DSM_V18_SWITCH	=  3,
102 	INTEL_DSM_V33_SWITCH	=  4,
103 };
104 
105 struct intel_host {
106 	u32	dsm_fns;
107 };
108 
109 static const guid_t intel_dsm_guid =
110 	GUID_INIT(0xF6C13EA5, 0x65CD, 0x461F,
111 		  0xAB, 0x7A, 0x29, 0xF7, 0xE8, 0xD5, 0xBD, 0x61);
112 
113 static int __intel_dsm(struct intel_host *intel_host, struct device *dev,
114 		       unsigned int fn, u32 *result)
115 {
116 	union acpi_object *obj;
117 	int err = 0;
118 
119 	obj = acpi_evaluate_dsm(ACPI_HANDLE(dev), &intel_dsm_guid, 0, fn, NULL);
120 	if (!obj)
121 		return -EOPNOTSUPP;
122 
123 	if (obj->type == ACPI_TYPE_INTEGER) {
124 		*result = obj->integer.value;
125 	} else if (obj->type == ACPI_TYPE_BUFFER && obj->buffer.length > 0) {
126 		size_t len = min_t(size_t, obj->buffer.length, 4);
127 
128 		*result = 0;
129 		memcpy(result, obj->buffer.pointer, len);
130 	} else {
131 		dev_err(dev, "%s DSM fn %u obj->type %d obj->buffer.length %d\n",
132 			__func__, fn, obj->type, obj->buffer.length);
133 		err = -EINVAL;
134 	}
135 
136 	ACPI_FREE(obj);
137 
138 	return err;
139 }
140 
141 static int intel_dsm(struct intel_host *intel_host, struct device *dev,
142 		     unsigned int fn, u32 *result)
143 {
144 	if (fn > 31 || !(intel_host->dsm_fns & (1 << fn)))
145 		return -EOPNOTSUPP;
146 
147 	return __intel_dsm(intel_host, dev, fn, result);
148 }
149 
150 static void intel_dsm_init(struct intel_host *intel_host, struct device *dev,
151 			   struct mmc_host *mmc)
152 {
153 	int err;
154 
155 	err = __intel_dsm(intel_host, dev, INTEL_DSM_FNS, &intel_host->dsm_fns);
156 	if (err) {
157 		pr_debug("%s: DSM not supported, error %d\n",
158 			 mmc_hostname(mmc), err);
159 		return;
160 	}
161 
162 	pr_debug("%s: DSM function mask %#x\n",
163 		 mmc_hostname(mmc), intel_host->dsm_fns);
164 }
165 
166 static int intel_start_signal_voltage_switch(struct mmc_host *mmc,
167 					     struct mmc_ios *ios)
168 {
169 	struct device *dev = mmc_dev(mmc);
170 	struct sdhci_acpi_host *c = dev_get_drvdata(dev);
171 	struct intel_host *intel_host = sdhci_acpi_priv(c);
172 	unsigned int fn;
173 	u32 result = 0;
174 	int err;
175 
176 	err = sdhci_start_signal_voltage_switch(mmc, ios);
177 	if (err)
178 		return err;
179 
180 	switch (ios->signal_voltage) {
181 	case MMC_SIGNAL_VOLTAGE_330:
182 		fn = INTEL_DSM_V33_SWITCH;
183 		break;
184 	case MMC_SIGNAL_VOLTAGE_180:
185 		fn = INTEL_DSM_V18_SWITCH;
186 		break;
187 	default:
188 		return 0;
189 	}
190 
191 	err = intel_dsm(intel_host, dev, fn, &result);
192 	pr_debug("%s: %s DSM fn %u error %d result %u\n",
193 		 mmc_hostname(mmc), __func__, fn, err, result);
194 
195 	return 0;
196 }
197 
198 static void sdhci_acpi_int_hw_reset(struct sdhci_host *host)
199 {
200 	u8 reg;
201 
202 	reg = sdhci_readb(host, SDHCI_POWER_CONTROL);
203 	reg |= 0x10;
204 	sdhci_writeb(host, reg, SDHCI_POWER_CONTROL);
205 	/* For eMMC, minimum is 1us but give it 9us for good measure */
206 	udelay(9);
207 	reg &= ~0x10;
208 	sdhci_writeb(host, reg, SDHCI_POWER_CONTROL);
209 	/* For eMMC, minimum is 200us but give it 300us for good measure */
210 	usleep_range(300, 1000);
211 }
212 
213 static const struct sdhci_ops sdhci_acpi_ops_dflt = {
214 	.set_clock = sdhci_set_clock,
215 	.set_bus_width = sdhci_set_bus_width,
216 	.reset = sdhci_reset,
217 	.set_uhs_signaling = sdhci_set_uhs_signaling,
218 };
219 
220 static const struct sdhci_ops sdhci_acpi_ops_int = {
221 	.set_clock = sdhci_set_clock,
222 	.set_bus_width = sdhci_set_bus_width,
223 	.reset = sdhci_reset,
224 	.set_uhs_signaling = sdhci_set_uhs_signaling,
225 	.hw_reset   = sdhci_acpi_int_hw_reset,
226 };
227 
228 static const struct sdhci_acpi_chip sdhci_acpi_chip_int = {
229 	.ops = &sdhci_acpi_ops_int,
230 };
231 
232 #ifdef CONFIG_X86
233 
234 static bool sdhci_acpi_byt(void)
235 {
236 	static const struct x86_cpu_id byt[] = {
237 		{ X86_VENDOR_INTEL, 6, INTEL_FAM6_ATOM_SILVERMONT1 },
238 		{}
239 	};
240 
241 	return x86_match_cpu(byt);
242 }
243 
244 static bool sdhci_acpi_cht(void)
245 {
246 	static const struct x86_cpu_id cht[] = {
247 		{ X86_VENDOR_INTEL, 6, INTEL_FAM6_ATOM_AIRMONT },
248 		{}
249 	};
250 
251 	return x86_match_cpu(cht);
252 }
253 
254 #define BYT_IOSF_SCCEP			0x63
255 #define BYT_IOSF_OCP_NETCTRL0		0x1078
256 #define BYT_IOSF_OCP_TIMEOUT_BASE	GENMASK(10, 8)
257 
258 static void sdhci_acpi_byt_setting(struct device *dev)
259 {
260 	u32 val = 0;
261 
262 	if (!sdhci_acpi_byt())
263 		return;
264 
265 	if (iosf_mbi_read(BYT_IOSF_SCCEP, MBI_CR_READ, BYT_IOSF_OCP_NETCTRL0,
266 			  &val)) {
267 		dev_err(dev, "%s read error\n", __func__);
268 		return;
269 	}
270 
271 	if (!(val & BYT_IOSF_OCP_TIMEOUT_BASE))
272 		return;
273 
274 	val &= ~BYT_IOSF_OCP_TIMEOUT_BASE;
275 
276 	if (iosf_mbi_write(BYT_IOSF_SCCEP, MBI_CR_WRITE, BYT_IOSF_OCP_NETCTRL0,
277 			   val)) {
278 		dev_err(dev, "%s write error\n", __func__);
279 		return;
280 	}
281 
282 	dev_dbg(dev, "%s completed\n", __func__);
283 }
284 
285 static bool sdhci_acpi_byt_defer(struct device *dev)
286 {
287 	if (!sdhci_acpi_byt())
288 		return false;
289 
290 	if (!iosf_mbi_available())
291 		return true;
292 
293 	sdhci_acpi_byt_setting(dev);
294 
295 	return false;
296 }
297 
298 static bool sdhci_acpi_cht_pci_wifi(unsigned int vendor, unsigned int device,
299 				    unsigned int slot, unsigned int parent_slot)
300 {
301 	struct pci_dev *dev, *parent, *from = NULL;
302 
303 	while (1) {
304 		dev = pci_get_device(vendor, device, from);
305 		pci_dev_put(from);
306 		if (!dev)
307 			break;
308 		parent = pci_upstream_bridge(dev);
309 		if (ACPI_COMPANION(&dev->dev) && PCI_SLOT(dev->devfn) == slot &&
310 		    parent && PCI_SLOT(parent->devfn) == parent_slot &&
311 		    !pci_upstream_bridge(parent)) {
312 			pci_dev_put(dev);
313 			return true;
314 		}
315 		from = dev;
316 	}
317 
318 	return false;
319 }
320 
321 /*
322  * GPDwin uses PCI wifi which conflicts with SDIO's use of
323  * acpi_device_fix_up_power() on child device nodes. Identifying GPDwin is
324  * problematic, but since SDIO is only used for wifi, the presence of the PCI
325  * wifi card in the expected slot with an ACPI companion node, is used to
326  * indicate that acpi_device_fix_up_power() should be avoided.
327  */
328 static inline bool sdhci_acpi_no_fixup_child_power(const char *hid,
329 						   const char *uid)
330 {
331 	return sdhci_acpi_cht() &&
332 	       !strcmp(hid, "80860F14") &&
333 	       !strcmp(uid, "2") &&
334 	       sdhci_acpi_cht_pci_wifi(0x14e4, 0x43ec, 0, 28);
335 }
336 
337 #else
338 
339 static inline void sdhci_acpi_byt_setting(struct device *dev)
340 {
341 }
342 
343 static inline bool sdhci_acpi_byt_defer(struct device *dev)
344 {
345 	return false;
346 }
347 
348 static inline bool sdhci_acpi_no_fixup_child_power(const char *hid,
349 						   const char *uid)
350 {
351 	return false;
352 }
353 
354 #endif
355 
356 static int bxt_get_cd(struct mmc_host *mmc)
357 {
358 	int gpio_cd = mmc_gpio_get_cd(mmc);
359 	struct sdhci_host *host = mmc_priv(mmc);
360 	unsigned long flags;
361 	int ret = 0;
362 
363 	if (!gpio_cd)
364 		return 0;
365 
366 	spin_lock_irqsave(&host->lock, flags);
367 
368 	if (host->flags & SDHCI_DEVICE_DEAD)
369 		goto out;
370 
371 	ret = !!(sdhci_readl(host, SDHCI_PRESENT_STATE) & SDHCI_CARD_PRESENT);
372 out:
373 	spin_unlock_irqrestore(&host->lock, flags);
374 
375 	return ret;
376 }
377 
378 static int intel_probe_slot(struct platform_device *pdev, const char *hid,
379 			    const char *uid)
380 {
381 	struct sdhci_acpi_host *c = platform_get_drvdata(pdev);
382 	struct intel_host *intel_host = sdhci_acpi_priv(c);
383 	struct sdhci_host *host = c->host;
384 
385 	if (hid && uid && !strcmp(hid, "80860F14") && !strcmp(uid, "1") &&
386 	    sdhci_readl(host, SDHCI_CAPABILITIES) == 0x446cc8b2 &&
387 	    sdhci_readl(host, SDHCI_CAPABILITIES_1) == 0x00000807)
388 		host->timeout_clk = 1000; /* 1000 kHz i.e. 1 MHz */
389 
390 	if (hid && !strcmp(hid, "80865ACA"))
391 		host->mmc_host_ops.get_cd = bxt_get_cd;
392 
393 	intel_dsm_init(intel_host, &pdev->dev, host->mmc);
394 
395 	host->mmc_host_ops.start_signal_voltage_switch =
396 					intel_start_signal_voltage_switch;
397 
398 	return 0;
399 }
400 
401 static const struct sdhci_acpi_slot sdhci_acpi_slot_int_emmc = {
402 	.chip    = &sdhci_acpi_chip_int,
403 	.caps    = MMC_CAP_8_BIT_DATA | MMC_CAP_NONREMOVABLE |
404 		   MMC_CAP_HW_RESET | MMC_CAP_1_8V_DDR |
405 		   MMC_CAP_CMD_DURING_TFR | MMC_CAP_WAIT_WHILE_BUSY,
406 	.flags   = SDHCI_ACPI_RUNTIME_PM,
407 	.quirks  = SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC,
408 	.quirks2 = SDHCI_QUIRK2_PRESET_VALUE_BROKEN |
409 		   SDHCI_QUIRK2_STOP_WITH_TC |
410 		   SDHCI_QUIRK2_CAPS_BIT63_FOR_HS400,
411 	.probe_slot	= intel_probe_slot,
412 	.priv_size	= sizeof(struct intel_host),
413 };
414 
415 static const struct sdhci_acpi_slot sdhci_acpi_slot_int_sdio = {
416 	.quirks  = SDHCI_QUIRK_BROKEN_CARD_DETECTION |
417 		   SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC,
418 	.quirks2 = SDHCI_QUIRK2_HOST_OFF_CARD_ON,
419 	.caps    = MMC_CAP_NONREMOVABLE | MMC_CAP_POWER_OFF_CARD |
420 		   MMC_CAP_WAIT_WHILE_BUSY,
421 	.flags   = SDHCI_ACPI_RUNTIME_PM,
422 	.pm_caps = MMC_PM_KEEP_POWER,
423 	.probe_slot	= intel_probe_slot,
424 	.priv_size	= sizeof(struct intel_host),
425 };
426 
427 static const struct sdhci_acpi_slot sdhci_acpi_slot_int_sd = {
428 	.flags   = SDHCI_ACPI_SD_CD | SDHCI_ACPI_SD_CD_OVERRIDE_LEVEL |
429 		   SDHCI_ACPI_RUNTIME_PM,
430 	.quirks  = SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC,
431 	.quirks2 = SDHCI_QUIRK2_CARD_ON_NEEDS_BUS_ON |
432 		   SDHCI_QUIRK2_STOP_WITH_TC,
433 	.caps    = MMC_CAP_WAIT_WHILE_BUSY | MMC_CAP_AGGRESSIVE_PM,
434 	.probe_slot	= intel_probe_slot,
435 	.priv_size	= sizeof(struct intel_host),
436 };
437 
438 static const struct sdhci_acpi_slot sdhci_acpi_slot_qcom_sd_3v = {
439 	.quirks  = SDHCI_QUIRK_BROKEN_CARD_DETECTION,
440 	.quirks2 = SDHCI_QUIRK2_NO_1_8_V,
441 	.caps    = MMC_CAP_NONREMOVABLE,
442 };
443 
444 static const struct sdhci_acpi_slot sdhci_acpi_slot_qcom_sd = {
445 	.quirks  = SDHCI_QUIRK_BROKEN_CARD_DETECTION,
446 	.caps    = MMC_CAP_NONREMOVABLE,
447 };
448 
449 struct sdhci_acpi_uid_slot {
450 	const char *hid;
451 	const char *uid;
452 	const struct sdhci_acpi_slot *slot;
453 };
454 
455 static const struct sdhci_acpi_uid_slot sdhci_acpi_uids[] = {
456 	{ "80865ACA", NULL, &sdhci_acpi_slot_int_sd },
457 	{ "80865ACC", NULL, &sdhci_acpi_slot_int_emmc },
458 	{ "80865AD0", NULL, &sdhci_acpi_slot_int_sdio },
459 	{ "80860F14" , "1" , &sdhci_acpi_slot_int_emmc },
460 	{ "80860F14" , "2" , &sdhci_acpi_slot_int_sdio },
461 	{ "80860F14" , "3" , &sdhci_acpi_slot_int_sd   },
462 	{ "80860F16" , NULL, &sdhci_acpi_slot_int_sd   },
463 	{ "INT33BB"  , "2" , &sdhci_acpi_slot_int_sdio },
464 	{ "INT33BB"  , "3" , &sdhci_acpi_slot_int_sd },
465 	{ "INT33C6"  , NULL, &sdhci_acpi_slot_int_sdio },
466 	{ "INT3436"  , NULL, &sdhci_acpi_slot_int_sdio },
467 	{ "INT344D"  , NULL, &sdhci_acpi_slot_int_sdio },
468 	{ "PNP0FFF"  , "3" , &sdhci_acpi_slot_int_sd   },
469 	{ "PNP0D40"  },
470 	{ "QCOM8051", NULL, &sdhci_acpi_slot_qcom_sd_3v },
471 	{ "QCOM8052", NULL, &sdhci_acpi_slot_qcom_sd },
472 	{ },
473 };
474 
475 static const struct acpi_device_id sdhci_acpi_ids[] = {
476 	{ "80865ACA" },
477 	{ "80865ACC" },
478 	{ "80865AD0" },
479 	{ "80860F14" },
480 	{ "80860F16" },
481 	{ "INT33BB"  },
482 	{ "INT33C6"  },
483 	{ "INT3436"  },
484 	{ "INT344D"  },
485 	{ "PNP0D40"  },
486 	{ "QCOM8051" },
487 	{ "QCOM8052" },
488 	{ },
489 };
490 MODULE_DEVICE_TABLE(acpi, sdhci_acpi_ids);
491 
492 static const struct sdhci_acpi_slot *sdhci_acpi_get_slot(const char *hid,
493 							 const char *uid)
494 {
495 	const struct sdhci_acpi_uid_slot *u;
496 
497 	for (u = sdhci_acpi_uids; u->hid; u++) {
498 		if (strcmp(u->hid, hid))
499 			continue;
500 		if (!u->uid)
501 			return u->slot;
502 		if (uid && !strcmp(u->uid, uid))
503 			return u->slot;
504 	}
505 	return NULL;
506 }
507 
508 static int sdhci_acpi_probe(struct platform_device *pdev)
509 {
510 	struct device *dev = &pdev->dev;
511 	const struct sdhci_acpi_slot *slot;
512 	struct acpi_device *device, *child;
513 	struct sdhci_acpi_host *c;
514 	struct sdhci_host *host;
515 	struct resource *iomem;
516 	resource_size_t len;
517 	size_t priv_size;
518 	const char *hid;
519 	const char *uid;
520 	int err;
521 
522 	device = ACPI_COMPANION(dev);
523 	if (!device)
524 		return -ENODEV;
525 
526 	hid = acpi_device_hid(device);
527 	uid = acpi_device_uid(device);
528 
529 	slot = sdhci_acpi_get_slot(hid, uid);
530 
531 	/* Power on the SDHCI controller and its children */
532 	acpi_device_fix_up_power(device);
533 	if (!sdhci_acpi_no_fixup_child_power(hid, uid)) {
534 		list_for_each_entry(child, &device->children, node)
535 			if (child->status.present && child->status.enabled)
536 				acpi_device_fix_up_power(child);
537 	}
538 
539 	if (sdhci_acpi_byt_defer(dev))
540 		return -EPROBE_DEFER;
541 
542 	iomem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
543 	if (!iomem)
544 		return -ENOMEM;
545 
546 	len = resource_size(iomem);
547 	if (len < 0x100)
548 		dev_err(dev, "Invalid iomem size!\n");
549 
550 	if (!devm_request_mem_region(dev, iomem->start, len, dev_name(dev)))
551 		return -ENOMEM;
552 
553 	priv_size = slot ? slot->priv_size : 0;
554 	host = sdhci_alloc_host(dev, sizeof(struct sdhci_acpi_host) + priv_size);
555 	if (IS_ERR(host))
556 		return PTR_ERR(host);
557 
558 	c = sdhci_priv(host);
559 	c->host = host;
560 	c->slot = slot;
561 	c->pdev = pdev;
562 	c->use_runtime_pm = sdhci_acpi_flag(c, SDHCI_ACPI_RUNTIME_PM);
563 
564 	platform_set_drvdata(pdev, c);
565 
566 	host->hw_name	= "ACPI";
567 	host->ops	= &sdhci_acpi_ops_dflt;
568 	host->irq	= platform_get_irq(pdev, 0);
569 
570 	host->ioaddr = devm_ioremap_nocache(dev, iomem->start,
571 					    resource_size(iomem));
572 	if (host->ioaddr == NULL) {
573 		err = -ENOMEM;
574 		goto err_free;
575 	}
576 
577 	if (c->slot) {
578 		if (c->slot->probe_slot) {
579 			err = c->slot->probe_slot(pdev, hid, uid);
580 			if (err)
581 				goto err_free;
582 		}
583 		if (c->slot->chip) {
584 			host->ops            = c->slot->chip->ops;
585 			host->quirks        |= c->slot->chip->quirks;
586 			host->quirks2       |= c->slot->chip->quirks2;
587 			host->mmc->caps     |= c->slot->chip->caps;
588 			host->mmc->caps2    |= c->slot->chip->caps2;
589 			host->mmc->pm_caps  |= c->slot->chip->pm_caps;
590 		}
591 		host->quirks        |= c->slot->quirks;
592 		host->quirks2       |= c->slot->quirks2;
593 		host->mmc->caps     |= c->slot->caps;
594 		host->mmc->caps2    |= c->slot->caps2;
595 		host->mmc->pm_caps  |= c->slot->pm_caps;
596 	}
597 
598 	host->mmc->caps2 |= MMC_CAP2_NO_PRESCAN_POWERUP;
599 
600 	if (sdhci_acpi_flag(c, SDHCI_ACPI_SD_CD)) {
601 		bool v = sdhci_acpi_flag(c, SDHCI_ACPI_SD_CD_OVERRIDE_LEVEL);
602 
603 		err = mmc_gpiod_request_cd(host->mmc, NULL, 0, v, 0, NULL);
604 		if (err) {
605 			if (err == -EPROBE_DEFER)
606 				goto err_free;
607 			dev_warn(dev, "failed to setup card detect gpio\n");
608 			c->use_runtime_pm = false;
609 		}
610 	}
611 
612 	err = sdhci_add_host(host);
613 	if (err)
614 		goto err_free;
615 
616 	if (c->use_runtime_pm) {
617 		pm_runtime_set_active(dev);
618 		pm_suspend_ignore_children(dev, 1);
619 		pm_runtime_set_autosuspend_delay(dev, 50);
620 		pm_runtime_use_autosuspend(dev);
621 		pm_runtime_enable(dev);
622 	}
623 
624 	device_enable_async_suspend(dev);
625 
626 	return 0;
627 
628 err_free:
629 	sdhci_free_host(c->host);
630 	return err;
631 }
632 
633 static int sdhci_acpi_remove(struct platform_device *pdev)
634 {
635 	struct sdhci_acpi_host *c = platform_get_drvdata(pdev);
636 	struct device *dev = &pdev->dev;
637 	int dead;
638 
639 	if (c->use_runtime_pm) {
640 		pm_runtime_get_sync(dev);
641 		pm_runtime_disable(dev);
642 		pm_runtime_put_noidle(dev);
643 	}
644 
645 	if (c->slot && c->slot->remove_slot)
646 		c->slot->remove_slot(pdev);
647 
648 	dead = (sdhci_readl(c->host, SDHCI_INT_STATUS) == ~0);
649 	sdhci_remove_host(c->host, dead);
650 	sdhci_free_host(c->host);
651 
652 	return 0;
653 }
654 
655 #ifdef CONFIG_PM_SLEEP
656 
657 static int sdhci_acpi_suspend(struct device *dev)
658 {
659 	struct sdhci_acpi_host *c = dev_get_drvdata(dev);
660 	struct sdhci_host *host = c->host;
661 
662 	if (host->tuning_mode != SDHCI_TUNING_MODE_3)
663 		mmc_retune_needed(host->mmc);
664 
665 	return sdhci_suspend_host(host);
666 }
667 
668 static int sdhci_acpi_resume(struct device *dev)
669 {
670 	struct sdhci_acpi_host *c = dev_get_drvdata(dev);
671 
672 	sdhci_acpi_byt_setting(&c->pdev->dev);
673 
674 	return sdhci_resume_host(c->host);
675 }
676 
677 #endif
678 
679 #ifdef CONFIG_PM
680 
681 static int sdhci_acpi_runtime_suspend(struct device *dev)
682 {
683 	struct sdhci_acpi_host *c = dev_get_drvdata(dev);
684 	struct sdhci_host *host = c->host;
685 
686 	if (host->tuning_mode != SDHCI_TUNING_MODE_3)
687 		mmc_retune_needed(host->mmc);
688 
689 	return sdhci_runtime_suspend_host(host);
690 }
691 
692 static int sdhci_acpi_runtime_resume(struct device *dev)
693 {
694 	struct sdhci_acpi_host *c = dev_get_drvdata(dev);
695 
696 	sdhci_acpi_byt_setting(&c->pdev->dev);
697 
698 	return sdhci_runtime_resume_host(c->host);
699 }
700 
701 #endif
702 
703 static const struct dev_pm_ops sdhci_acpi_pm_ops = {
704 	SET_SYSTEM_SLEEP_PM_OPS(sdhci_acpi_suspend, sdhci_acpi_resume)
705 	SET_RUNTIME_PM_OPS(sdhci_acpi_runtime_suspend,
706 			sdhci_acpi_runtime_resume, NULL)
707 };
708 
709 static struct platform_driver sdhci_acpi_driver = {
710 	.driver = {
711 		.name			= "sdhci-acpi",
712 		.acpi_match_table	= sdhci_acpi_ids,
713 		.pm			= &sdhci_acpi_pm_ops,
714 	},
715 	.probe	= sdhci_acpi_probe,
716 	.remove	= sdhci_acpi_remove,
717 };
718 
719 module_platform_driver(sdhci_acpi_driver);
720 
721 MODULE_DESCRIPTION("Secure Digital Host Controller Interface ACPI driver");
722 MODULE_AUTHOR("Adrian Hunter");
723 MODULE_LICENSE("GPL v2");
724