2 * Secure Digital Host Controller Interface ACPI driver.
4 * Copyright (c) 2012, Intel Corporation.
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.
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
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.
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>
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/gpio.h>
35 #include <linux/interrupt.h>
36 #include <linux/acpi.h>
37 #include <linux/acpi_gpio.h>
39 #include <linux/pm_runtime.h>
40 #include <linux/delay.h>
42 #include <linux/mmc/host.h>
43 #include <linux/mmc/pm.h>
44 #include <linux/mmc/sdhci.h>
49 SDHCI_ACPI_SD_CD
= BIT(0),
50 SDHCI_ACPI_RUNTIME_PM
= BIT(1),
53 struct sdhci_acpi_chip
{
54 const struct sdhci_ops
*ops
;
59 mmc_pm_flag_t pm_caps
;
62 struct sdhci_acpi_slot
{
63 const struct sdhci_acpi_chip
*chip
;
68 mmc_pm_flag_t pm_caps
;
72 struct sdhci_acpi_host
{
73 struct sdhci_host
*host
;
74 const struct sdhci_acpi_slot
*slot
;
75 struct platform_device
*pdev
;
79 static inline bool sdhci_acpi_flag(struct sdhci_acpi_host
*c
, unsigned int flag
)
81 return c
->slot
&& (c
->slot
->flags
& flag
);
84 static int sdhci_acpi_enable_dma(struct sdhci_host
*host
)
89 static void sdhci_acpi_int_hw_reset(struct sdhci_host
*host
)
93 reg
= sdhci_readb(host
, SDHCI_POWER_CONTROL
);
95 sdhci_writeb(host
, reg
, SDHCI_POWER_CONTROL
);
96 /* For eMMC, minimum is 1us but give it 9us for good measure */
99 sdhci_writeb(host
, reg
, SDHCI_POWER_CONTROL
);
100 /* For eMMC, minimum is 200us but give it 300us for good measure */
101 usleep_range(300, 1000);
104 static const struct sdhci_ops sdhci_acpi_ops_dflt
= {
105 .enable_dma
= sdhci_acpi_enable_dma
,
108 static const struct sdhci_ops sdhci_acpi_ops_int
= {
109 .enable_dma
= sdhci_acpi_enable_dma
,
110 .hw_reset
= sdhci_acpi_int_hw_reset
,
113 static const struct sdhci_acpi_chip sdhci_acpi_chip_int
= {
114 .ops
= &sdhci_acpi_ops_int
,
117 static const struct sdhci_acpi_slot sdhci_acpi_slot_int_emmc
= {
118 .chip
= &sdhci_acpi_chip_int
,
119 .caps
= MMC_CAP_8_BIT_DATA
| MMC_CAP_NONREMOVABLE
| MMC_CAP_HW_RESET
,
120 .caps2
= MMC_CAP2_HC_ERASE_SZ
,
121 .flags
= SDHCI_ACPI_RUNTIME_PM
,
122 .quirks2
= SDHCI_QUIRK2_PRESET_VALUE_BROKEN
,
125 static const struct sdhci_acpi_slot sdhci_acpi_slot_int_sdio
= {
126 .quirks
= SDHCI_QUIRK_BROKEN_CARD_DETECTION
,
127 .quirks2
= SDHCI_QUIRK2_HOST_OFF_CARD_ON
,
128 .caps
= MMC_CAP_NONREMOVABLE
| MMC_CAP_POWER_OFF_CARD
,
129 .flags
= SDHCI_ACPI_RUNTIME_PM
,
130 .pm_caps
= MMC_PM_KEEP_POWER
,
133 static const struct sdhci_acpi_slot sdhci_acpi_slot_int_sd
= {
134 .flags
= SDHCI_ACPI_SD_CD
| SDHCI_ACPI_RUNTIME_PM
,
135 .quirks2
= SDHCI_QUIRK2_CARD_ON_NEEDS_BUS_ON
,
138 struct sdhci_acpi_uid_slot
{
141 const struct sdhci_acpi_slot
*slot
;
144 static const struct sdhci_acpi_uid_slot sdhci_acpi_uids
[] = {
145 { "80860F14" , "1" , &sdhci_acpi_slot_int_emmc
},
146 { "80860F14" , "3" , &sdhci_acpi_slot_int_sd
},
147 { "80860F16" , NULL
, &sdhci_acpi_slot_int_sd
},
148 { "INT33BB" , "2" , &sdhci_acpi_slot_int_sdio
},
149 { "INT33BB" , "3" , &sdhci_acpi_slot_int_sd
},
150 { "INT33C6" , NULL
, &sdhci_acpi_slot_int_sdio
},
151 { "INT3436" , NULL
, &sdhci_acpi_slot_int_sdio
},
152 { "INT344D" , NULL
, &sdhci_acpi_slot_int_sdio
},
157 static const struct acpi_device_id sdhci_acpi_ids
[] = {
167 MODULE_DEVICE_TABLE(acpi
, sdhci_acpi_ids
);
169 static const struct sdhci_acpi_slot
*sdhci_acpi_get_slot_by_ids(const char *hid
,
172 const struct sdhci_acpi_uid_slot
*u
;
174 for (u
= sdhci_acpi_uids
; u
->hid
; u
++) {
175 if (strcmp(u
->hid
, hid
))
179 if (uid
&& !strcmp(u
->uid
, uid
))
185 static const struct sdhci_acpi_slot
*sdhci_acpi_get_slot(acpi_handle handle
,
188 const struct sdhci_acpi_slot
*slot
;
189 struct acpi_device_info
*info
;
190 const char *uid
= NULL
;
193 status
= acpi_get_object_info(handle
, &info
);
194 if (!ACPI_FAILURE(status
) && (info
->valid
& ACPI_VALID_UID
))
195 uid
= info
->unique_id
.string
;
197 slot
= sdhci_acpi_get_slot_by_ids(hid
, uid
);
203 #ifdef CONFIG_PM_RUNTIME
205 static irqreturn_t
sdhci_acpi_sd_cd(int irq
, void *dev_id
)
207 mmc_detect_change(dev_id
, msecs_to_jiffies(200));
211 static int sdhci_acpi_add_own_cd(struct device
*dev
, int gpio
,
212 struct mmc_host
*mmc
)
222 err
= devm_gpio_request_one(dev
, gpio
, GPIOF_DIR_IN
, "sd_cd");
226 irq
= gpio_to_irq(gpio
);
232 flags
= IRQF_TRIGGER_RISING
| IRQF_TRIGGER_FALLING
;
233 err
= devm_request_irq(dev
, irq
, sdhci_acpi_sd_cd
, flags
, "sd_cd", mmc
);
240 devm_gpio_free(dev
, gpio
);
242 dev_warn(dev
, "failed to setup card detect wake up\n");
248 static int sdhci_acpi_add_own_cd(struct device
*dev
, int gpio
,
249 struct mmc_host
*mmc
)
256 static int sdhci_acpi_probe(struct platform_device
*pdev
)
258 struct device
*dev
= &pdev
->dev
;
259 acpi_handle handle
= ACPI_HANDLE(dev
);
260 struct acpi_device
*device
;
261 struct sdhci_acpi_host
*c
;
262 struct sdhci_host
*host
;
263 struct resource
*iomem
;
268 if (acpi_bus_get_device(handle
, &device
))
271 if (acpi_bus_get_status(device
) || !device
->status
.present
)
274 hid
= acpi_device_hid(device
);
276 iomem
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
280 len
= resource_size(iomem
);
282 dev_err(dev
, "Invalid iomem size!\n");
284 if (!devm_request_mem_region(dev
, iomem
->start
, len
, dev_name(dev
)))
287 host
= sdhci_alloc_host(dev
, sizeof(struct sdhci_acpi_host
));
289 return PTR_ERR(host
);
291 gpio
= acpi_get_gpio_by_index(dev
, 0, NULL
);
293 c
= sdhci_priv(host
);
295 c
->slot
= sdhci_acpi_get_slot(handle
, hid
);
297 c
->use_runtime_pm
= sdhci_acpi_flag(c
, SDHCI_ACPI_RUNTIME_PM
);
299 platform_set_drvdata(pdev
, c
);
301 host
->hw_name
= "ACPI";
302 host
->ops
= &sdhci_acpi_ops_dflt
;
303 host
->irq
= platform_get_irq(pdev
, 0);
305 host
->ioaddr
= devm_ioremap_nocache(dev
, iomem
->start
,
306 resource_size(iomem
));
307 if (host
->ioaddr
== NULL
) {
312 if (!dev
->dma_mask
) {
315 if (sdhci_readl(host
, SDHCI_CAPABILITIES
) & SDHCI_CAN_64BIT
) {
316 /* 64-bit DMA is not supported at present */
317 dma_mask
= DMA_BIT_MASK(32);
319 dma_mask
= DMA_BIT_MASK(32);
322 dev
->dma_mask
= &dev
->coherent_dma_mask
;
323 dev
->coherent_dma_mask
= dma_mask
;
328 host
->ops
= c
->slot
->chip
->ops
;
329 host
->quirks
|= c
->slot
->chip
->quirks
;
330 host
->quirks2
|= c
->slot
->chip
->quirks2
;
331 host
->mmc
->caps
|= c
->slot
->chip
->caps
;
332 host
->mmc
->caps2
|= c
->slot
->chip
->caps2
;
333 host
->mmc
->pm_caps
|= c
->slot
->chip
->pm_caps
;
335 host
->quirks
|= c
->slot
->quirks
;
336 host
->quirks2
|= c
->slot
->quirks2
;
337 host
->mmc
->caps
|= c
->slot
->caps
;
338 host
->mmc
->caps2
|= c
->slot
->caps2
;
339 host
->mmc
->pm_caps
|= c
->slot
->pm_caps
;
342 host
->mmc
->caps2
|= MMC_CAP2_NO_PRESCAN_POWERUP
;
344 err
= sdhci_add_host(host
);
348 if (sdhci_acpi_flag(c
, SDHCI_ACPI_SD_CD
)) {
349 if (sdhci_acpi_add_own_cd(dev
, gpio
, host
->mmc
))
350 c
->use_runtime_pm
= false;
353 if (c
->use_runtime_pm
) {
354 pm_runtime_set_active(dev
);
355 pm_suspend_ignore_children(dev
, 1);
356 pm_runtime_set_autosuspend_delay(dev
, 50);
357 pm_runtime_use_autosuspend(dev
);
358 pm_runtime_enable(dev
);
364 sdhci_free_host(c
->host
);
368 static int sdhci_acpi_remove(struct platform_device
*pdev
)
370 struct sdhci_acpi_host
*c
= platform_get_drvdata(pdev
);
371 struct device
*dev
= &pdev
->dev
;
374 if (c
->use_runtime_pm
) {
375 pm_runtime_get_sync(dev
);
376 pm_runtime_disable(dev
);
377 pm_runtime_put_noidle(dev
);
380 dead
= (sdhci_readl(c
->host
, SDHCI_INT_STATUS
) == ~0);
381 sdhci_remove_host(c
->host
, dead
);
382 sdhci_free_host(c
->host
);
387 #ifdef CONFIG_PM_SLEEP
389 static int sdhci_acpi_suspend(struct device
*dev
)
391 struct sdhci_acpi_host
*c
= dev_get_drvdata(dev
);
393 return sdhci_suspend_host(c
->host
);
396 static int sdhci_acpi_resume(struct device
*dev
)
398 struct sdhci_acpi_host
*c
= dev_get_drvdata(dev
);
400 return sdhci_resume_host(c
->host
);
405 #define sdhci_acpi_suspend NULL
406 #define sdhci_acpi_resume NULL
410 #ifdef CONFIG_PM_RUNTIME
412 static int sdhci_acpi_runtime_suspend(struct device
*dev
)
414 struct sdhci_acpi_host
*c
= dev_get_drvdata(dev
);
416 return sdhci_runtime_suspend_host(c
->host
);
419 static int sdhci_acpi_runtime_resume(struct device
*dev
)
421 struct sdhci_acpi_host
*c
= dev_get_drvdata(dev
);
423 return sdhci_runtime_resume_host(c
->host
);
426 static int sdhci_acpi_runtime_idle(struct device
*dev
)
433 #define sdhci_acpi_runtime_suspend NULL
434 #define sdhci_acpi_runtime_resume NULL
435 #define sdhci_acpi_runtime_idle NULL
439 static const struct dev_pm_ops sdhci_acpi_pm_ops
= {
440 .suspend
= sdhci_acpi_suspend
,
441 .resume
= sdhci_acpi_resume
,
442 .runtime_suspend
= sdhci_acpi_runtime_suspend
,
443 .runtime_resume
= sdhci_acpi_runtime_resume
,
444 .runtime_idle
= sdhci_acpi_runtime_idle
,
447 static struct platform_driver sdhci_acpi_driver
= {
449 .name
= "sdhci-acpi",
450 .owner
= THIS_MODULE
,
451 .acpi_match_table
= sdhci_acpi_ids
,
452 .pm
= &sdhci_acpi_pm_ops
,
454 .probe
= sdhci_acpi_probe
,
455 .remove
= sdhci_acpi_remove
,
458 module_platform_driver(sdhci_acpi_driver
);
460 MODULE_DESCRIPTION("Secure Digital Host Controller Interface ACPI driver");
461 MODULE_AUTHOR("Adrian Hunter");
462 MODULE_LICENSE("GPL v2");