blob: 9ce17f6e4014d9f4fc0b45c8e6856056ddebdb52 [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>
Mika Westerberg87875652014-01-08 12:40:55 +020034#include <linux/gpio/consumer.h>
Adrian Hunterc4e05032012-11-23 21:17:34 +010035#include <linux/interrupt.h>
36#include <linux/acpi.h>
37#include <linux/pm.h>
38#include <linux/pm_runtime.h>
Adrian Hunterb04fa062013-06-13 11:50:27 +030039#include <linux/delay.h>
Adrian Hunterc4e05032012-11-23 21:17:34 +010040
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
Adrian Hunterb04fa062013-06-13 11:50:27 +030088static 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
Adrian Hunterc4e05032012-11-23 21:17:34 +0100103static const struct sdhci_ops sdhci_acpi_ops_dflt = {
104 .enable_dma = sdhci_acpi_enable_dma,
105};
106
Adrian Hunterb04fa062013-06-13 11:50:27 +0300107static 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
112static const struct sdhci_acpi_chip sdhci_acpi_chip_int = {
113 .ops = &sdhci_acpi_ops_int,
114};
115
Adrian Hunter07a58882013-04-26 11:27:22 +0300116static const struct sdhci_acpi_slot sdhci_acpi_slot_int_emmc = {
Adrian Hunterb04fa062013-06-13 11:50:27 +0300117 .chip = &sdhci_acpi_chip_int,
118 .caps = MMC_CAP_8_BIT_DATA | MMC_CAP_NONREMOVABLE | MMC_CAP_HW_RESET,
Adrian Hunter07a58882013-04-26 11:27:22 +0300119 .caps2 = MMC_CAP2_HC_ERASE_SZ,
120 .flags = SDHCI_ACPI_RUNTIME_PM,
121};
122
Adrian Huntere5571392012-12-10 21:18:48 +0100123static 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
Adrian Hunter07a58882013-04-26 11:27:22 +0300130static const struct sdhci_acpi_slot sdhci_acpi_slot_int_sd = {
Adrian Huntera61abe62013-05-06 12:17:33 +0300131 .flags = SDHCI_ACPI_SD_CD | SDHCI_ACPI_RUNTIME_PM,
132 .quirks2 = SDHCI_QUIRK2_CARD_ON_NEEDS_BUS_ON,
Adrian Hunter07a58882013-04-26 11:27:22 +0300133};
134
135struct sdhci_acpi_uid_slot {
136 const char *hid;
137 const char *uid;
138 const struct sdhci_acpi_slot *slot;
139};
140
141static 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 },
Mika Westerberg07c001c2013-11-12 12:01:33 +0200146 { "INT3436" , NULL, &sdhci_acpi_slot_int_sdio },
Adrian Hunter07a58882013-04-26 11:27:22 +0300147 { "PNP0D40" },
148 { },
149};
150
Adrian Hunterc4e05032012-11-23 21:17:34 +0100151static const struct acpi_device_id sdhci_acpi_ids[] = {
Adrian Hunter07a58882013-04-26 11:27:22 +0300152 { "80860F14" },
153 { "INT33BB" },
154 { "INT33C6" },
Mika Westerberg07c001c2013-11-12 12:01:33 +0200155 { "INT3436" },
Adrian Hunter07a58882013-04-26 11:27:22 +0300156 { "PNP0D40" },
Adrian Hunterc4e05032012-11-23 21:17:34 +0100157 { },
158};
159MODULE_DEVICE_TABLE(acpi, sdhci_acpi_ids);
160
Adrian Hunter07a58882013-04-26 11:27:22 +0300161static const struct sdhci_acpi_slot *sdhci_acpi_get_slot_by_ids(const char *hid,
162 const char *uid)
Adrian Hunterc4e05032012-11-23 21:17:34 +0100163{
Adrian Hunter07a58882013-04-26 11:27:22 +0300164 const struct sdhci_acpi_uid_slot *u;
Adrian Hunterc4e05032012-11-23 21:17:34 +0100165
Adrian Hunter07a58882013-04-26 11:27:22 +0300166 for (u = sdhci_acpi_uids; u->hid; u++) {
167 if (strcmp(u->hid, hid))
168 continue;
169 if (!u->uid)
170 return u->slot;
171 if (uid && !strcmp(u->uid, uid))
172 return u->slot;
173 }
Adrian Hunterc4e05032012-11-23 21:17:34 +0100174 return NULL;
175}
176
Adrian Hunter07a58882013-04-26 11:27:22 +0300177static const struct sdhci_acpi_slot *sdhci_acpi_get_slot(acpi_handle handle,
178 const char *hid)
179{
180 const struct sdhci_acpi_slot *slot;
181 struct acpi_device_info *info;
182 const char *uid = NULL;
183 acpi_status status;
184
185 status = acpi_get_object_info(handle, &info);
186 if (!ACPI_FAILURE(status) && (info->valid & ACPI_VALID_UID))
187 uid = info->unique_id.string;
188
189 slot = sdhci_acpi_get_slot_by_ids(hid, uid);
190
191 kfree(info);
192 return slot;
193}
194
Adrian Huntera61abe62013-05-06 12:17:33 +0300195#ifdef CONFIG_PM_RUNTIME
196
197static irqreturn_t sdhci_acpi_sd_cd(int irq, void *dev_id)
198{
199 mmc_detect_change(dev_id, msecs_to_jiffies(200));
200 return IRQ_HANDLED;
201}
202
Mika Westerberg87875652014-01-08 12:40:55 +0200203static int sdhci_acpi_add_own_cd(struct device *dev, struct mmc_host *mmc)
Adrian Huntera61abe62013-05-06 12:17:33 +0300204{
Mika Westerberg87875652014-01-08 12:40:55 +0200205 struct gpio_desc *desc;
Adrian Huntera61abe62013-05-06 12:17:33 +0300206 unsigned long flags;
207 int err, irq;
208
Mika Westerberg87875652014-01-08 12:40:55 +0200209 desc = devm_gpiod_get_index(dev, "sd_cd", 0);
210 if (IS_ERR(desc)) {
211 err = PTR_ERR(desc);
Adrian Huntera61abe62013-05-06 12:17:33 +0300212 goto out;
213 }
214
Mika Westerberg87875652014-01-08 12:40:55 +0200215 err = gpiod_direction_input(desc);
Adrian Huntera61abe62013-05-06 12:17:33 +0300216 if (err)
Mika Westerberg87875652014-01-08 12:40:55 +0200217 goto out_free;
Adrian Huntera61abe62013-05-06 12:17:33 +0300218
Mika Westerberg87875652014-01-08 12:40:55 +0200219 irq = gpiod_to_irq(desc);
Wei Yongjun5a0e8072013-05-28 13:26:25 +0800220 if (irq < 0) {
221 err = irq;
Adrian Huntera61abe62013-05-06 12:17:33 +0300222 goto out_free;
Wei Yongjun5a0e8072013-05-28 13:26:25 +0800223 }
Adrian Huntera61abe62013-05-06 12:17:33 +0300224
225 flags = IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING;
226 err = devm_request_irq(dev, irq, sdhci_acpi_sd_cd, flags, "sd_cd", mmc);
227 if (err)
228 goto out_free;
229
230 return 0;
231
232out_free:
Mika Westerberg87875652014-01-08 12:40:55 +0200233 devm_gpiod_put(dev, desc);
Adrian Huntera61abe62013-05-06 12:17:33 +0300234out:
235 dev_warn(dev, "failed to setup card detect wake up\n");
236 return err;
237}
238
239#else
240
Mika Westerberg87875652014-01-08 12:40:55 +0200241static int sdhci_acpi_add_own_cd(struct device *dev, struct mmc_host *mmc)
Adrian Huntera61abe62013-05-06 12:17:33 +0300242{
243 return 0;
244}
245
246#endif
247
Greg Kroah-Hartman4e608e42012-12-21 15:05:47 -0800248static int sdhci_acpi_probe(struct platform_device *pdev)
Adrian Hunterc4e05032012-11-23 21:17:34 +0100249{
250 struct device *dev = &pdev->dev;
251 acpi_handle handle = ACPI_HANDLE(dev);
252 struct acpi_device *device;
253 struct sdhci_acpi_host *c;
254 struct sdhci_host *host;
255 struct resource *iomem;
256 resource_size_t len;
257 const char *hid;
Mika Westerberg87875652014-01-08 12:40:55 +0200258 int err;
Adrian Hunterc4e05032012-11-23 21:17:34 +0100259
260 if (acpi_bus_get_device(handle, &device))
261 return -ENODEV;
262
263 if (acpi_bus_get_status(device) || !device->status.present)
264 return -ENODEV;
265
266 hid = acpi_device_hid(device);
267
268 iomem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
269 if (!iomem)
270 return -ENOMEM;
271
272 len = resource_size(iomem);
273 if (len < 0x100)
274 dev_err(dev, "Invalid iomem size!\n");
275
276 if (!devm_request_mem_region(dev, iomem->start, len, dev_name(dev)))
277 return -ENOMEM;
278
279 host = sdhci_alloc_host(dev, sizeof(struct sdhci_acpi_host));
280 if (IS_ERR(host))
281 return PTR_ERR(host);
282
283 c = sdhci_priv(host);
284 c->host = host;
Adrian Hunter07a58882013-04-26 11:27:22 +0300285 c->slot = sdhci_acpi_get_slot(handle, hid);
Adrian Hunterc4e05032012-11-23 21:17:34 +0100286 c->pdev = pdev;
287 c->use_runtime_pm = sdhci_acpi_flag(c, SDHCI_ACPI_RUNTIME_PM);
288
289 platform_set_drvdata(pdev, c);
290
291 host->hw_name = "ACPI";
292 host->ops = &sdhci_acpi_ops_dflt;
293 host->irq = platform_get_irq(pdev, 0);
294
295 host->ioaddr = devm_ioremap_nocache(dev, iomem->start,
296 resource_size(iomem));
297 if (host->ioaddr == NULL) {
298 err = -ENOMEM;
299 goto err_free;
300 }
301
302 if (!dev->dma_mask) {
303 u64 dma_mask;
304
305 if (sdhci_readl(host, SDHCI_CAPABILITIES) & SDHCI_CAN_64BIT) {
306 /* 64-bit DMA is not supported at present */
307 dma_mask = DMA_BIT_MASK(32);
308 } else {
309 dma_mask = DMA_BIT_MASK(32);
310 }
311
Russell King07f44502013-06-27 14:06:28 +0100312 err = dma_coerce_mask_and_coherent(dev, dma_mask);
313 if (err)
314 goto err_free;
Adrian Hunterc4e05032012-11-23 21:17:34 +0100315 }
316
317 if (c->slot) {
318 if (c->slot->chip) {
319 host->ops = c->slot->chip->ops;
320 host->quirks |= c->slot->chip->quirks;
321 host->quirks2 |= c->slot->chip->quirks2;
322 host->mmc->caps |= c->slot->chip->caps;
323 host->mmc->caps2 |= c->slot->chip->caps2;
324 host->mmc->pm_caps |= c->slot->chip->pm_caps;
325 }
326 host->quirks |= c->slot->quirks;
327 host->quirks2 |= c->slot->quirks2;
328 host->mmc->caps |= c->slot->caps;
329 host->mmc->caps2 |= c->slot->caps2;
330 host->mmc->pm_caps |= c->slot->pm_caps;
331 }
332
Adrian Hunter0d3e3352013-04-04 16:41:06 +0300333 host->mmc->caps2 |= MMC_CAP2_NO_PRESCAN_POWERUP;
334
Adrian Hunterc4e05032012-11-23 21:17:34 +0100335 err = sdhci_add_host(host);
336 if (err)
337 goto err_free;
338
Adrian Huntera61abe62013-05-06 12:17:33 +0300339 if (sdhci_acpi_flag(c, SDHCI_ACPI_SD_CD)) {
Mika Westerberg87875652014-01-08 12:40:55 +0200340 if (sdhci_acpi_add_own_cd(dev, host->mmc))
Adrian Huntera61abe62013-05-06 12:17:33 +0300341 c->use_runtime_pm = false;
342 }
343
Adrian Hunterc4e05032012-11-23 21:17:34 +0100344 if (c->use_runtime_pm) {
Adrian Hunter1d1ff452013-04-26 11:27:21 +0300345 pm_runtime_set_active(dev);
Adrian Hunterc4e05032012-11-23 21:17:34 +0100346 pm_suspend_ignore_children(dev, 1);
347 pm_runtime_set_autosuspend_delay(dev, 50);
348 pm_runtime_use_autosuspend(dev);
349 pm_runtime_enable(dev);
350 }
351
352 return 0;
353
354err_free:
Adrian Hunterc4e05032012-11-23 21:17:34 +0100355 sdhci_free_host(c->host);
356 return err;
357}
358
Greg Kroah-Hartman4e608e42012-12-21 15:05:47 -0800359static int sdhci_acpi_remove(struct platform_device *pdev)
Adrian Hunterc4e05032012-11-23 21:17:34 +0100360{
361 struct sdhci_acpi_host *c = platform_get_drvdata(pdev);
362 struct device *dev = &pdev->dev;
363 int dead;
364
365 if (c->use_runtime_pm) {
366 pm_runtime_get_sync(dev);
367 pm_runtime_disable(dev);
368 pm_runtime_put_noidle(dev);
369 }
370
371 dead = (sdhci_readl(c->host, SDHCI_INT_STATUS) == ~0);
372 sdhci_remove_host(c->host, dead);
Adrian Hunterc4e05032012-11-23 21:17:34 +0100373 sdhci_free_host(c->host);
374
375 return 0;
376}
377
378#ifdef CONFIG_PM_SLEEP
379
380static int sdhci_acpi_suspend(struct device *dev)
381{
382 struct sdhci_acpi_host *c = dev_get_drvdata(dev);
383
384 return sdhci_suspend_host(c->host);
385}
386
387static int sdhci_acpi_resume(struct device *dev)
388{
389 struct sdhci_acpi_host *c = dev_get_drvdata(dev);
390
391 return sdhci_resume_host(c->host);
392}
393
394#else
395
396#define sdhci_acpi_suspend NULL
397#define sdhci_acpi_resume NULL
398
399#endif
400
401#ifdef CONFIG_PM_RUNTIME
402
403static int sdhci_acpi_runtime_suspend(struct device *dev)
404{
405 struct sdhci_acpi_host *c = dev_get_drvdata(dev);
406
407 return sdhci_runtime_suspend_host(c->host);
408}
409
410static int sdhci_acpi_runtime_resume(struct device *dev)
411{
412 struct sdhci_acpi_host *c = dev_get_drvdata(dev);
413
414 return sdhci_runtime_resume_host(c->host);
415}
416
417static int sdhci_acpi_runtime_idle(struct device *dev)
418{
419 return 0;
420}
421
422#else
423
424#define sdhci_acpi_runtime_suspend NULL
425#define sdhci_acpi_runtime_resume NULL
426#define sdhci_acpi_runtime_idle NULL
427
428#endif
429
430static const struct dev_pm_ops sdhci_acpi_pm_ops = {
431 .suspend = sdhci_acpi_suspend,
432 .resume = sdhci_acpi_resume,
433 .runtime_suspend = sdhci_acpi_runtime_suspend,
434 .runtime_resume = sdhci_acpi_runtime_resume,
435 .runtime_idle = sdhci_acpi_runtime_idle,
436};
437
438static struct platform_driver sdhci_acpi_driver = {
439 .driver = {
440 .name = "sdhci-acpi",
441 .owner = THIS_MODULE,
442 .acpi_match_table = sdhci_acpi_ids,
443 .pm = &sdhci_acpi_pm_ops,
444 },
445 .probe = sdhci_acpi_probe,
Greg Kroah-Hartman4e608e42012-12-21 15:05:47 -0800446 .remove = sdhci_acpi_remove,
Adrian Hunterc4e05032012-11-23 21:17:34 +0100447};
448
449module_platform_driver(sdhci_acpi_driver);
450
451MODULE_DESCRIPTION("Secure Digital Host Controller Interface ACPI driver");
452MODULE_AUTHOR("Adrian Hunter");
453MODULE_LICENSE("GPL v2");