blob: 08a85ec332246eb8eac2b74a59e0ff354f224dfa [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>
Adrian Huntera61abe62013-05-06 12:17:33 +030034#include <linux/gpio.h>
Adrian Hunterc4e05032012-11-23 21:17:34 +010035#include <linux/interrupt.h>
36#include <linux/acpi.h>
Adrian Huntera61abe62013-05-06 12:17:33 +030037#include <linux/acpi_gpio.h>
Adrian Hunterc4e05032012-11-23 21:17:34 +010038#include <linux/pm.h>
39#include <linux/pm_runtime.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
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
88static const struct sdhci_ops sdhci_acpi_ops_dflt = {
89 .enable_dma = sdhci_acpi_enable_dma,
90};
91
Adrian Hunter07a58882013-04-26 11:27:22 +030092static const struct sdhci_acpi_slot sdhci_acpi_slot_int_emmc = {
93 .caps = MMC_CAP_8_BIT_DATA | MMC_CAP_NONREMOVABLE,
94 .caps2 = MMC_CAP2_HC_ERASE_SZ,
95 .flags = SDHCI_ACPI_RUNTIME_PM,
96};
97
Adrian Huntere5571392012-12-10 21:18:48 +010098static const struct sdhci_acpi_slot sdhci_acpi_slot_int_sdio = {
99 .quirks2 = SDHCI_QUIRK2_HOST_OFF_CARD_ON,
100 .caps = MMC_CAP_NONREMOVABLE | MMC_CAP_POWER_OFF_CARD,
101 .flags = SDHCI_ACPI_RUNTIME_PM,
102 .pm_caps = MMC_PM_KEEP_POWER,
103};
104
Adrian Hunter07a58882013-04-26 11:27:22 +0300105static const struct sdhci_acpi_slot sdhci_acpi_slot_int_sd = {
Adrian Huntera61abe62013-05-06 12:17:33 +0300106 .flags = SDHCI_ACPI_SD_CD | SDHCI_ACPI_RUNTIME_PM,
107 .quirks2 = SDHCI_QUIRK2_CARD_ON_NEEDS_BUS_ON,
Adrian Hunter07a58882013-04-26 11:27:22 +0300108};
109
110struct sdhci_acpi_uid_slot {
111 const char *hid;
112 const char *uid;
113 const struct sdhci_acpi_slot *slot;
114};
115
116static const struct sdhci_acpi_uid_slot sdhci_acpi_uids[] = {
117 { "80860F14" , "1" , &sdhci_acpi_slot_int_emmc },
118 { "80860F14" , "3" , &sdhci_acpi_slot_int_sd },
119 { "INT33BB" , "2" , &sdhci_acpi_slot_int_sdio },
120 { "INT33C6" , NULL, &sdhci_acpi_slot_int_sdio },
121 { "PNP0D40" },
122 { },
123};
124
Adrian Hunterc4e05032012-11-23 21:17:34 +0100125static const struct acpi_device_id sdhci_acpi_ids[] = {
Adrian Hunter07a58882013-04-26 11:27:22 +0300126 { "80860F14" },
127 { "INT33BB" },
128 { "INT33C6" },
129 { "PNP0D40" },
Adrian Hunterc4e05032012-11-23 21:17:34 +0100130 { },
131};
132MODULE_DEVICE_TABLE(acpi, sdhci_acpi_ids);
133
Adrian Hunter07a58882013-04-26 11:27:22 +0300134static const struct sdhci_acpi_slot *sdhci_acpi_get_slot_by_ids(const char *hid,
135 const char *uid)
Adrian Hunterc4e05032012-11-23 21:17:34 +0100136{
Adrian Hunter07a58882013-04-26 11:27:22 +0300137 const struct sdhci_acpi_uid_slot *u;
Adrian Hunterc4e05032012-11-23 21:17:34 +0100138
Adrian Hunter07a58882013-04-26 11:27:22 +0300139 for (u = sdhci_acpi_uids; u->hid; u++) {
140 if (strcmp(u->hid, hid))
141 continue;
142 if (!u->uid)
143 return u->slot;
144 if (uid && !strcmp(u->uid, uid))
145 return u->slot;
146 }
Adrian Hunterc4e05032012-11-23 21:17:34 +0100147 return NULL;
148}
149
Adrian Hunter07a58882013-04-26 11:27:22 +0300150static const struct sdhci_acpi_slot *sdhci_acpi_get_slot(acpi_handle handle,
151 const char *hid)
152{
153 const struct sdhci_acpi_slot *slot;
154 struct acpi_device_info *info;
155 const char *uid = NULL;
156 acpi_status status;
157
158 status = acpi_get_object_info(handle, &info);
159 if (!ACPI_FAILURE(status) && (info->valid & ACPI_VALID_UID))
160 uid = info->unique_id.string;
161
162 slot = sdhci_acpi_get_slot_by_ids(hid, uid);
163
164 kfree(info);
165 return slot;
166}
167
Adrian Huntera61abe62013-05-06 12:17:33 +0300168#ifdef CONFIG_PM_RUNTIME
169
170static irqreturn_t sdhci_acpi_sd_cd(int irq, void *dev_id)
171{
172 mmc_detect_change(dev_id, msecs_to_jiffies(200));
173 return IRQ_HANDLED;
174}
175
176static int sdhci_acpi_add_own_cd(struct device *dev, int gpio,
177 struct mmc_host *mmc)
178{
179 unsigned long flags;
180 int err, irq;
181
182 if (gpio < 0) {
183 err = gpio;
184 goto out;
185 }
186
187 err = devm_gpio_request_one(dev, gpio, GPIOF_DIR_IN, "sd_cd");
188 if (err)
189 goto out;
190
191 irq = gpio_to_irq(gpio);
Wei Yongjun5a0e8072013-05-28 13:26:25 +0800192 if (irq < 0) {
193 err = irq;
Adrian Huntera61abe62013-05-06 12:17:33 +0300194 goto out_free;
Wei Yongjun5a0e8072013-05-28 13:26:25 +0800195 }
Adrian Huntera61abe62013-05-06 12:17:33 +0300196
197 flags = IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING;
198 err = devm_request_irq(dev, irq, sdhci_acpi_sd_cd, flags, "sd_cd", mmc);
199 if (err)
200 goto out_free;
201
202 return 0;
203
204out_free:
205 devm_gpio_free(dev, gpio);
206out:
207 dev_warn(dev, "failed to setup card detect wake up\n");
208 return err;
209}
210
211#else
212
213static int sdhci_acpi_add_own_cd(struct device *dev, int gpio,
214 struct mmc_host *mmc)
215{
216 return 0;
217}
218
219#endif
220
Greg Kroah-Hartman4e608e42012-12-21 15:05:47 -0800221static int sdhci_acpi_probe(struct platform_device *pdev)
Adrian Hunterc4e05032012-11-23 21:17:34 +0100222{
223 struct device *dev = &pdev->dev;
224 acpi_handle handle = ACPI_HANDLE(dev);
225 struct acpi_device *device;
226 struct sdhci_acpi_host *c;
227 struct sdhci_host *host;
228 struct resource *iomem;
229 resource_size_t len;
230 const char *hid;
Adrian Huntera61abe62013-05-06 12:17:33 +0300231 int err, gpio;
Adrian Hunterc4e05032012-11-23 21:17:34 +0100232
233 if (acpi_bus_get_device(handle, &device))
234 return -ENODEV;
235
236 if (acpi_bus_get_status(device) || !device->status.present)
237 return -ENODEV;
238
239 hid = acpi_device_hid(device);
240
241 iomem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
242 if (!iomem)
243 return -ENOMEM;
244
245 len = resource_size(iomem);
246 if (len < 0x100)
247 dev_err(dev, "Invalid iomem size!\n");
248
249 if (!devm_request_mem_region(dev, iomem->start, len, dev_name(dev)))
250 return -ENOMEM;
251
252 host = sdhci_alloc_host(dev, sizeof(struct sdhci_acpi_host));
253 if (IS_ERR(host))
254 return PTR_ERR(host);
255
Adrian Huntera61abe62013-05-06 12:17:33 +0300256 gpio = acpi_get_gpio_by_index(dev, 0, NULL);
257
Adrian Hunterc4e05032012-11-23 21:17:34 +0100258 c = sdhci_priv(host);
259 c->host = host;
Adrian Hunter07a58882013-04-26 11:27:22 +0300260 c->slot = sdhci_acpi_get_slot(handle, hid);
Adrian Hunterc4e05032012-11-23 21:17:34 +0100261 c->pdev = pdev;
262 c->use_runtime_pm = sdhci_acpi_flag(c, SDHCI_ACPI_RUNTIME_PM);
263
264 platform_set_drvdata(pdev, c);
265
266 host->hw_name = "ACPI";
267 host->ops = &sdhci_acpi_ops_dflt;
268 host->irq = platform_get_irq(pdev, 0);
269
270 host->ioaddr = devm_ioremap_nocache(dev, iomem->start,
271 resource_size(iomem));
272 if (host->ioaddr == NULL) {
273 err = -ENOMEM;
274 goto err_free;
275 }
276
277 if (!dev->dma_mask) {
278 u64 dma_mask;
279
280 if (sdhci_readl(host, SDHCI_CAPABILITIES) & SDHCI_CAN_64BIT) {
281 /* 64-bit DMA is not supported at present */
282 dma_mask = DMA_BIT_MASK(32);
283 } else {
284 dma_mask = DMA_BIT_MASK(32);
285 }
286
287 dev->dma_mask = &dev->coherent_dma_mask;
288 dev->coherent_dma_mask = dma_mask;
289 }
290
291 if (c->slot) {
292 if (c->slot->chip) {
293 host->ops = c->slot->chip->ops;
294 host->quirks |= c->slot->chip->quirks;
295 host->quirks2 |= c->slot->chip->quirks2;
296 host->mmc->caps |= c->slot->chip->caps;
297 host->mmc->caps2 |= c->slot->chip->caps2;
298 host->mmc->pm_caps |= c->slot->chip->pm_caps;
299 }
300 host->quirks |= c->slot->quirks;
301 host->quirks2 |= c->slot->quirks2;
302 host->mmc->caps |= c->slot->caps;
303 host->mmc->caps2 |= c->slot->caps2;
304 host->mmc->pm_caps |= c->slot->pm_caps;
305 }
306
Adrian Hunter0d3e3352013-04-04 16:41:06 +0300307 host->mmc->caps2 |= MMC_CAP2_NO_PRESCAN_POWERUP;
308
Adrian Hunterc4e05032012-11-23 21:17:34 +0100309 err = sdhci_add_host(host);
310 if (err)
311 goto err_free;
312
Adrian Huntera61abe62013-05-06 12:17:33 +0300313 if (sdhci_acpi_flag(c, SDHCI_ACPI_SD_CD)) {
314 if (sdhci_acpi_add_own_cd(dev, gpio, host->mmc))
315 c->use_runtime_pm = false;
316 }
317
Adrian Hunterc4e05032012-11-23 21:17:34 +0100318 if (c->use_runtime_pm) {
Adrian Hunter1d1ff452013-04-26 11:27:21 +0300319 pm_runtime_set_active(dev);
Adrian Hunterc4e05032012-11-23 21:17:34 +0100320 pm_suspend_ignore_children(dev, 1);
321 pm_runtime_set_autosuspend_delay(dev, 50);
322 pm_runtime_use_autosuspend(dev);
323 pm_runtime_enable(dev);
324 }
325
326 return 0;
327
328err_free:
Adrian Hunterc4e05032012-11-23 21:17:34 +0100329 sdhci_free_host(c->host);
330 return err;
331}
332
Greg Kroah-Hartman4e608e42012-12-21 15:05:47 -0800333static int sdhci_acpi_remove(struct platform_device *pdev)
Adrian Hunterc4e05032012-11-23 21:17:34 +0100334{
335 struct sdhci_acpi_host *c = platform_get_drvdata(pdev);
336 struct device *dev = &pdev->dev;
337 int dead;
338
339 if (c->use_runtime_pm) {
340 pm_runtime_get_sync(dev);
341 pm_runtime_disable(dev);
342 pm_runtime_put_noidle(dev);
343 }
344
345 dead = (sdhci_readl(c->host, SDHCI_INT_STATUS) == ~0);
346 sdhci_remove_host(c->host, dead);
Adrian Hunterc4e05032012-11-23 21:17:34 +0100347 sdhci_free_host(c->host);
348
349 return 0;
350}
351
352#ifdef CONFIG_PM_SLEEP
353
354static int sdhci_acpi_suspend(struct device *dev)
355{
356 struct sdhci_acpi_host *c = dev_get_drvdata(dev);
357
358 return sdhci_suspend_host(c->host);
359}
360
361static int sdhci_acpi_resume(struct device *dev)
362{
363 struct sdhci_acpi_host *c = dev_get_drvdata(dev);
364
365 return sdhci_resume_host(c->host);
366}
367
368#else
369
370#define sdhci_acpi_suspend NULL
371#define sdhci_acpi_resume NULL
372
373#endif
374
375#ifdef CONFIG_PM_RUNTIME
376
377static int sdhci_acpi_runtime_suspend(struct device *dev)
378{
379 struct sdhci_acpi_host *c = dev_get_drvdata(dev);
380
381 return sdhci_runtime_suspend_host(c->host);
382}
383
384static int sdhci_acpi_runtime_resume(struct device *dev)
385{
386 struct sdhci_acpi_host *c = dev_get_drvdata(dev);
387
388 return sdhci_runtime_resume_host(c->host);
389}
390
391static int sdhci_acpi_runtime_idle(struct device *dev)
392{
393 return 0;
394}
395
396#else
397
398#define sdhci_acpi_runtime_suspend NULL
399#define sdhci_acpi_runtime_resume NULL
400#define sdhci_acpi_runtime_idle NULL
401
402#endif
403
404static const struct dev_pm_ops sdhci_acpi_pm_ops = {
405 .suspend = sdhci_acpi_suspend,
406 .resume = sdhci_acpi_resume,
407 .runtime_suspend = sdhci_acpi_runtime_suspend,
408 .runtime_resume = sdhci_acpi_runtime_resume,
409 .runtime_idle = sdhci_acpi_runtime_idle,
410};
411
412static struct platform_driver sdhci_acpi_driver = {
413 .driver = {
414 .name = "sdhci-acpi",
415 .owner = THIS_MODULE,
416 .acpi_match_table = sdhci_acpi_ids,
417 .pm = &sdhci_acpi_pm_ops,
418 },
419 .probe = sdhci_acpi_probe,
Greg Kroah-Hartman4e608e42012-12-21 15:05:47 -0800420 .remove = sdhci_acpi_remove,
Adrian Hunterc4e05032012-11-23 21:17:34 +0100421};
422
423module_platform_driver(sdhci_acpi_driver);
424
425MODULE_DESCRIPTION("Secure Digital Host Controller Interface ACPI driver");
426MODULE_AUTHOR("Adrian Hunter");
427MODULE_LICENSE("GPL v2");