mfd: wm8350-i2c: Make sure the i2c regmap functions are compiled
[linux/fpc-iii.git] / drivers / mmc / host / sdhci-acpi.c
blob854fcfbd75742f640520701a744976da665d45e1
1 /*
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
13 * more details.
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>
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/gpio.h>
35 #include <linux/interrupt.h>
36 #include <linux/acpi.h>
37 #include <linux/acpi_gpio.h>
38 #include <linux/pm.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>
46 #include "sdhci.h"
48 enum {
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;
55 unsigned int quirks;
56 unsigned int quirks2;
57 unsigned long caps;
58 unsigned int caps2;
59 mmc_pm_flag_t pm_caps;
62 struct sdhci_acpi_slot {
63 const struct sdhci_acpi_chip *chip;
64 unsigned int quirks;
65 unsigned int quirks2;
66 unsigned long caps;
67 unsigned int caps2;
68 mmc_pm_flag_t pm_caps;
69 unsigned int flags;
72 struct sdhci_acpi_host {
73 struct sdhci_host *host;
74 const struct sdhci_acpi_slot *slot;
75 struct platform_device *pdev;
76 bool use_runtime_pm;
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)
86 return 0;
89 static void sdhci_acpi_int_hw_reset(struct sdhci_host *host)
91 u8 reg;
93 reg = sdhci_readb(host, SDHCI_POWER_CONTROL);
94 reg |= 0x10;
95 sdhci_writeb(host, reg, SDHCI_POWER_CONTROL);
96 /* For eMMC, minimum is 1us but give it 9us for good measure */
97 udelay(9);
98 reg &= ~0x10;
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 {
139 const char *hid;
140 const char *uid;
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 },
153 { "PNP0D40" },
154 { },
157 static const struct acpi_device_id sdhci_acpi_ids[] = {
158 { "80860F14" },
159 { "80860F16" },
160 { "INT33BB" },
161 { "INT33C6" },
162 { "INT3436" },
163 { "INT344D" },
164 { "PNP0D40" },
165 { },
167 MODULE_DEVICE_TABLE(acpi, sdhci_acpi_ids);
169 static const struct sdhci_acpi_slot *sdhci_acpi_get_slot_by_ids(const char *hid,
170 const char *uid)
172 const struct sdhci_acpi_uid_slot *u;
174 for (u = sdhci_acpi_uids; u->hid; u++) {
175 if (strcmp(u->hid, hid))
176 continue;
177 if (!u->uid)
178 return u->slot;
179 if (uid && !strcmp(u->uid, uid))
180 return u->slot;
182 return NULL;
185 static const struct sdhci_acpi_slot *sdhci_acpi_get_slot(acpi_handle handle,
186 const char *hid)
188 const struct sdhci_acpi_slot *slot;
189 struct acpi_device_info *info;
190 const char *uid = NULL;
191 acpi_status status;
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);
199 kfree(info);
200 return slot;
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));
208 return IRQ_HANDLED;
211 static int sdhci_acpi_add_own_cd(struct device *dev, int gpio,
212 struct mmc_host *mmc)
214 unsigned long flags;
215 int err, irq;
217 if (gpio < 0) {
218 err = gpio;
219 goto out;
222 err = devm_gpio_request_one(dev, gpio, GPIOF_DIR_IN, "sd_cd");
223 if (err)
224 goto out;
226 irq = gpio_to_irq(gpio);
227 if (irq < 0) {
228 err = irq;
229 goto out_free;
232 flags = IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING;
233 err = devm_request_irq(dev, irq, sdhci_acpi_sd_cd, flags, "sd_cd", mmc);
234 if (err)
235 goto out_free;
237 return 0;
239 out_free:
240 devm_gpio_free(dev, gpio);
241 out:
242 dev_warn(dev, "failed to setup card detect wake up\n");
243 return err;
246 #else
248 static int sdhci_acpi_add_own_cd(struct device *dev, int gpio,
249 struct mmc_host *mmc)
251 return 0;
254 #endif
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;
264 resource_size_t len;
265 const char *hid;
266 int err, gpio;
268 if (acpi_bus_get_device(handle, &device))
269 return -ENODEV;
271 if (acpi_bus_get_status(device) || !device->status.present)
272 return -ENODEV;
274 hid = acpi_device_hid(device);
276 iomem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
277 if (!iomem)
278 return -ENOMEM;
280 len = resource_size(iomem);
281 if (len < 0x100)
282 dev_err(dev, "Invalid iomem size!\n");
284 if (!devm_request_mem_region(dev, iomem->start, len, dev_name(dev)))
285 return -ENOMEM;
287 host = sdhci_alloc_host(dev, sizeof(struct sdhci_acpi_host));
288 if (IS_ERR(host))
289 return PTR_ERR(host);
291 gpio = acpi_get_gpio_by_index(dev, 0, NULL);
293 c = sdhci_priv(host);
294 c->host = host;
295 c->slot = sdhci_acpi_get_slot(handle, hid);
296 c->pdev = pdev;
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) {
308 err = -ENOMEM;
309 goto err_free;
312 if (!dev->dma_mask) {
313 u64 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);
318 } else {
319 dma_mask = DMA_BIT_MASK(32);
322 dev->dma_mask = &dev->coherent_dma_mask;
323 dev->coherent_dma_mask = dma_mask;
326 if (c->slot) {
327 if (c->slot->chip) {
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);
345 if (err)
346 goto err_free;
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);
361 return 0;
363 err_free:
364 sdhci_free_host(c->host);
365 return err;
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;
372 int dead;
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);
384 return 0;
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);
403 #else
405 #define sdhci_acpi_suspend NULL
406 #define sdhci_acpi_resume NULL
408 #endif
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)
428 return 0;
431 #else
433 #define sdhci_acpi_runtime_suspend NULL
434 #define sdhci_acpi_runtime_resume NULL
435 #define sdhci_acpi_runtime_idle NULL
437 #endif
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 = {
448 .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");