Merge tag 'fbdev-3.14' of git://git.kernel.org/pub/scm/linux/kernel/git/tomba/linux
[linux-2.6-block.git] / drivers / mmc / host / sdhci-acpi.c
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/gpio/consumer.h>
35 #include <linux/interrupt.h>
36 #include <linux/acpi.h>
37 #include <linux/pm.h>
38 #include <linux/pm_runtime.h>
39 #include <linux/delay.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
47 enum {
48         SDHCI_ACPI_SD_CD        = BIT(0),
49         SDHCI_ACPI_RUNTIME_PM   = BIT(1),
50 };
51
52 struct 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
61 struct 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
71 struct 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
78 static inline bool sdhci_acpi_flag(struct sdhci_acpi_host *c, unsigned int flag)
79 {
80         return c->slot && (c->slot->flags & flag);
81 }
82
83 static int sdhci_acpi_enable_dma(struct sdhci_host *host)
84 {
85         return 0;
86 }
87
88 static void sdhci_acpi_int_hw_reset(struct sdhci_host *host)
89 {
90         u8 reg;
91
92         reg = sdhci_readb(host, SDHCI_POWER_CONTROL);
93         reg |= 0x10;
94         sdhci_writeb(host, reg, SDHCI_POWER_CONTROL);
95         /* For eMMC, minimum is 1us but give it 9us for good measure */
96         udelay(9);
97         reg &= ~0x10;
98         sdhci_writeb(host, reg, SDHCI_POWER_CONTROL);
99         /* For eMMC, minimum is 200us but give it 300us for good measure */
100         usleep_range(300, 1000);
101 }
102
103 static const struct sdhci_ops sdhci_acpi_ops_dflt = {
104         .enable_dma = sdhci_acpi_enable_dma,
105 };
106
107 static const struct sdhci_ops sdhci_acpi_ops_int = {
108         .enable_dma = sdhci_acpi_enable_dma,
109         .hw_reset   = sdhci_acpi_int_hw_reset,
110 };
111
112 static const struct sdhci_acpi_chip sdhci_acpi_chip_int = {
113         .ops = &sdhci_acpi_ops_int,
114 };
115
116 static const struct sdhci_acpi_slot sdhci_acpi_slot_int_emmc = {
117         .chip    = &sdhci_acpi_chip_int,
118         .caps    = MMC_CAP_8_BIT_DATA | MMC_CAP_NONREMOVABLE | MMC_CAP_HW_RESET,
119         .caps2   = MMC_CAP2_HC_ERASE_SZ,
120         .flags   = SDHCI_ACPI_RUNTIME_PM,
121 };
122
123 static const struct sdhci_acpi_slot sdhci_acpi_slot_int_sdio = {
124         .quirks2 = SDHCI_QUIRK2_HOST_OFF_CARD_ON,
125         .caps    = MMC_CAP_NONREMOVABLE | MMC_CAP_POWER_OFF_CARD,
126         .flags   = SDHCI_ACPI_RUNTIME_PM,
127         .pm_caps = MMC_PM_KEEP_POWER,
128 };
129
130 static const struct sdhci_acpi_slot sdhci_acpi_slot_int_sd = {
131         .flags   = SDHCI_ACPI_SD_CD | SDHCI_ACPI_RUNTIME_PM,
132         .quirks2 = SDHCI_QUIRK2_CARD_ON_NEEDS_BUS_ON,
133 };
134
135 struct sdhci_acpi_uid_slot {
136         const char *hid;
137         const char *uid;
138         const struct sdhci_acpi_slot *slot;
139 };
140
141 static const struct sdhci_acpi_uid_slot sdhci_acpi_uids[] = {
142         { "80860F14" , "1" , &sdhci_acpi_slot_int_emmc },
143         { "80860F14" , "3" , &sdhci_acpi_slot_int_sd   },
144         { "INT33BB"  , "2" , &sdhci_acpi_slot_int_sdio },
145         { "INT33C6"  , NULL, &sdhci_acpi_slot_int_sdio },
146         { "PNP0D40"  },
147         { },
148 };
149
150 static const struct acpi_device_id sdhci_acpi_ids[] = {
151         { "80860F14" },
152         { "INT33BB"  },
153         { "INT33C6"  },
154         { "PNP0D40"  },
155         { },
156 };
157 MODULE_DEVICE_TABLE(acpi, sdhci_acpi_ids);
158
159 static const struct sdhci_acpi_slot *sdhci_acpi_get_slot_by_ids(const char *hid,
160                                                                 const char *uid)
161 {
162         const struct sdhci_acpi_uid_slot *u;
163
164         for (u = sdhci_acpi_uids; u->hid; u++) {
165                 if (strcmp(u->hid, hid))
166                         continue;
167                 if (!u->uid)
168                         return u->slot;
169                 if (uid && !strcmp(u->uid, uid))
170                         return u->slot;
171         }
172         return NULL;
173 }
174
175 static const struct sdhci_acpi_slot *sdhci_acpi_get_slot(acpi_handle handle,
176                                                          const char *hid)
177 {
178         const struct sdhci_acpi_slot *slot;
179         struct acpi_device_info *info;
180         const char *uid = NULL;
181         acpi_status status;
182
183         status = acpi_get_object_info(handle, &info);
184         if (!ACPI_FAILURE(status) && (info->valid & ACPI_VALID_UID))
185                 uid = info->unique_id.string;
186
187         slot = sdhci_acpi_get_slot_by_ids(hid, uid);
188
189         kfree(info);
190         return slot;
191 }
192
193 #ifdef CONFIG_PM_RUNTIME
194
195 static irqreturn_t sdhci_acpi_sd_cd(int irq, void *dev_id)
196 {
197         mmc_detect_change(dev_id, msecs_to_jiffies(200));
198         return IRQ_HANDLED;
199 }
200
201 static int sdhci_acpi_add_own_cd(struct device *dev, struct mmc_host *mmc)
202 {
203         struct gpio_desc *desc;
204         unsigned long flags;
205         int err, irq;
206
207         desc = devm_gpiod_get_index(dev, "sd_cd", 0);
208         if (IS_ERR(desc)) {
209                 err = PTR_ERR(desc);
210                 goto out;
211         }
212
213         err = gpiod_direction_input(desc);
214         if (err)
215                 goto out_free;
216
217         irq = gpiod_to_irq(desc);
218         if (irq < 0) {
219                 err = irq;
220                 goto out_free;
221         }
222
223         flags = IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING;
224         err = devm_request_irq(dev, irq, sdhci_acpi_sd_cd, flags, "sd_cd", mmc);
225         if (err)
226                 goto out_free;
227
228         return 0;
229
230 out_free:
231         devm_gpiod_put(dev, desc);
232 out:
233         dev_warn(dev, "failed to setup card detect wake up\n");
234         return err;
235 }
236
237 #else
238
239 static int sdhci_acpi_add_own_cd(struct device *dev, struct mmc_host *mmc)
240 {
241         return 0;
242 }
243
244 #endif
245
246 static int sdhci_acpi_probe(struct platform_device *pdev)
247 {
248         struct device *dev = &pdev->dev;
249         acpi_handle handle = ACPI_HANDLE(dev);
250         struct acpi_device *device;
251         struct sdhci_acpi_host *c;
252         struct sdhci_host *host;
253         struct resource *iomem;
254         resource_size_t len;
255         const char *hid;
256         int err;
257
258         if (acpi_bus_get_device(handle, &device))
259                 return -ENODEV;
260
261         if (acpi_bus_get_status(device) || !device->status.present)
262                 return -ENODEV;
263
264         hid = acpi_device_hid(device);
265
266         iomem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
267         if (!iomem)
268                 return -ENOMEM;
269
270         len = resource_size(iomem);
271         if (len < 0x100)
272                 dev_err(dev, "Invalid iomem size!\n");
273
274         if (!devm_request_mem_region(dev, iomem->start, len, dev_name(dev)))
275                 return -ENOMEM;
276
277         host = sdhci_alloc_host(dev, sizeof(struct sdhci_acpi_host));
278         if (IS_ERR(host))
279                 return PTR_ERR(host);
280
281         c = sdhci_priv(host);
282         c->host = host;
283         c->slot = sdhci_acpi_get_slot(handle, hid);
284         c->pdev = pdev;
285         c->use_runtime_pm = sdhci_acpi_flag(c, SDHCI_ACPI_RUNTIME_PM);
286
287         platform_set_drvdata(pdev, c);
288
289         host->hw_name   = "ACPI";
290         host->ops       = &sdhci_acpi_ops_dflt;
291         host->irq       = platform_get_irq(pdev, 0);
292
293         host->ioaddr = devm_ioremap_nocache(dev, iomem->start,
294                                             resource_size(iomem));
295         if (host->ioaddr == NULL) {
296                 err = -ENOMEM;
297                 goto err_free;
298         }
299
300         if (!dev->dma_mask) {
301                 u64 dma_mask;
302
303                 if (sdhci_readl(host, SDHCI_CAPABILITIES) & SDHCI_CAN_64BIT) {
304                         /* 64-bit DMA is not supported at present */
305                         dma_mask = DMA_BIT_MASK(32);
306                 } else {
307                         dma_mask = DMA_BIT_MASK(32);
308                 }
309
310                 err = dma_coerce_mask_and_coherent(dev, dma_mask);
311                 if (err)
312                         goto err_free;
313         }
314
315         if (c->slot) {
316                 if (c->slot->chip) {
317                         host->ops            = c->slot->chip->ops;
318                         host->quirks        |= c->slot->chip->quirks;
319                         host->quirks2       |= c->slot->chip->quirks2;
320                         host->mmc->caps     |= c->slot->chip->caps;
321                         host->mmc->caps2    |= c->slot->chip->caps2;
322                         host->mmc->pm_caps  |= c->slot->chip->pm_caps;
323                 }
324                 host->quirks        |= c->slot->quirks;
325                 host->quirks2       |= c->slot->quirks2;
326                 host->mmc->caps     |= c->slot->caps;
327                 host->mmc->caps2    |= c->slot->caps2;
328                 host->mmc->pm_caps  |= c->slot->pm_caps;
329         }
330
331         host->mmc->caps2 |= MMC_CAP2_NO_PRESCAN_POWERUP;
332
333         err = sdhci_add_host(host);
334         if (err)
335                 goto err_free;
336
337         if (sdhci_acpi_flag(c, SDHCI_ACPI_SD_CD)) {
338                 if (sdhci_acpi_add_own_cd(dev, host->mmc))
339                         c->use_runtime_pm = false;
340         }
341
342         if (c->use_runtime_pm) {
343                 pm_runtime_set_active(dev);
344                 pm_suspend_ignore_children(dev, 1);
345                 pm_runtime_set_autosuspend_delay(dev, 50);
346                 pm_runtime_use_autosuspend(dev);
347                 pm_runtime_enable(dev);
348         }
349
350         return 0;
351
352 err_free:
353         sdhci_free_host(c->host);
354         return err;
355 }
356
357 static int sdhci_acpi_remove(struct platform_device *pdev)
358 {
359         struct sdhci_acpi_host *c = platform_get_drvdata(pdev);
360         struct device *dev = &pdev->dev;
361         int dead;
362
363         if (c->use_runtime_pm) {
364                 pm_runtime_get_sync(dev);
365                 pm_runtime_disable(dev);
366                 pm_runtime_put_noidle(dev);
367         }
368
369         dead = (sdhci_readl(c->host, SDHCI_INT_STATUS) == ~0);
370         sdhci_remove_host(c->host, dead);
371         sdhci_free_host(c->host);
372
373         return 0;
374 }
375
376 #ifdef CONFIG_PM_SLEEP
377
378 static int sdhci_acpi_suspend(struct device *dev)
379 {
380         struct sdhci_acpi_host *c = dev_get_drvdata(dev);
381
382         return sdhci_suspend_host(c->host);
383 }
384
385 static int sdhci_acpi_resume(struct device *dev)
386 {
387         struct sdhci_acpi_host *c = dev_get_drvdata(dev);
388
389         return sdhci_resume_host(c->host);
390 }
391
392 #else
393
394 #define sdhci_acpi_suspend      NULL
395 #define sdhci_acpi_resume       NULL
396
397 #endif
398
399 #ifdef CONFIG_PM_RUNTIME
400
401 static int sdhci_acpi_runtime_suspend(struct device *dev)
402 {
403         struct sdhci_acpi_host *c = dev_get_drvdata(dev);
404
405         return sdhci_runtime_suspend_host(c->host);
406 }
407
408 static int sdhci_acpi_runtime_resume(struct device *dev)
409 {
410         struct sdhci_acpi_host *c = dev_get_drvdata(dev);
411
412         return sdhci_runtime_resume_host(c->host);
413 }
414
415 static int sdhci_acpi_runtime_idle(struct device *dev)
416 {
417         return 0;
418 }
419
420 #else
421
422 #define sdhci_acpi_runtime_suspend      NULL
423 #define sdhci_acpi_runtime_resume       NULL
424 #define sdhci_acpi_runtime_idle         NULL
425
426 #endif
427
428 static const struct dev_pm_ops sdhci_acpi_pm_ops = {
429         .suspend                = sdhci_acpi_suspend,
430         .resume                 = sdhci_acpi_resume,
431         .runtime_suspend        = sdhci_acpi_runtime_suspend,
432         .runtime_resume         = sdhci_acpi_runtime_resume,
433         .runtime_idle           = sdhci_acpi_runtime_idle,
434 };
435
436 static struct platform_driver sdhci_acpi_driver = {
437         .driver = {
438                 .name                   = "sdhci-acpi",
439                 .owner                  = THIS_MODULE,
440                 .acpi_match_table       = sdhci_acpi_ids,
441                 .pm                     = &sdhci_acpi_pm_ops,
442         },
443         .probe  = sdhci_acpi_probe,
444         .remove = sdhci_acpi_remove,
445 };
446
447 module_platform_driver(sdhci_acpi_driver);
448
449 MODULE_DESCRIPTION("Secure Digital Host Controller Interface ACPI driver");
450 MODULE_AUTHOR("Adrian Hunter");
451 MODULE_LICENSE("GPL v2");