]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blame - drivers/mmc/host/sdhci-acpi.c
mmc: sdhci-pci: Use SET_RUNTIME_PM_OPS macro to set runtime pm callbacks
[mirror_ubuntu-zesty-kernel.git] / drivers / mmc / host / sdhci-acpi.c
CommitLineData
c4e05037
AH
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/interrupt.h>
35#include <linux/acpi.h>
36#include <linux/pm.h>
37#include <linux/pm_runtime.h>
b04fa064 38#include <linux/delay.h>
c4e05037
AH
39
40#include <linux/mmc/host.h>
41#include <linux/mmc/pm.h>
4fd4409c 42#include <linux/mmc/slot-gpio.h>
c4e05037
AH
43#include <linux/mmc/sdhci.h>
44
45#include "sdhci.h"
46
47enum {
4fd4409c
AH
48 SDHCI_ACPI_SD_CD = BIT(0),
49 SDHCI_ACPI_RUNTIME_PM = BIT(1),
50 SDHCI_ACPI_SD_CD_OVERRIDE_LEVEL = BIT(2),
c4e05037
AH
51};
52
53struct 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;
60};
61
62struct 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;
70};
71
72struct 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;
77};
78
79static inline bool sdhci_acpi_flag(struct sdhci_acpi_host *c, unsigned int flag)
80{
81 return c->slot && (c->slot->flags & flag);
82}
83
84static int sdhci_acpi_enable_dma(struct sdhci_host *host)
85{
86 return 0;
87}
88
b04fa064
AH
89static void sdhci_acpi_int_hw_reset(struct sdhci_host *host)
90{
91 u8 reg;
92
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);
102}
103
c4e05037 104static const struct sdhci_ops sdhci_acpi_ops_dflt = {
1771059c 105 .set_clock = sdhci_set_clock,
c4e05037 106 .enable_dma = sdhci_acpi_enable_dma,
2317f56c 107 .set_bus_width = sdhci_set_bus_width,
03231f9b 108 .reset = sdhci_reset,
96d7b78c 109 .set_uhs_signaling = sdhci_set_uhs_signaling,
c4e05037
AH
110};
111
b04fa064 112static const struct sdhci_ops sdhci_acpi_ops_int = {
1771059c 113 .set_clock = sdhci_set_clock,
b04fa064 114 .enable_dma = sdhci_acpi_enable_dma,
2317f56c 115 .set_bus_width = sdhci_set_bus_width,
03231f9b 116 .reset = sdhci_reset,
96d7b78c 117 .set_uhs_signaling = sdhci_set_uhs_signaling,
b04fa064
AH
118 .hw_reset = sdhci_acpi_int_hw_reset,
119};
120
121static const struct sdhci_acpi_chip sdhci_acpi_chip_int = {
122 .ops = &sdhci_acpi_ops_int,
123};
124
07a58883 125static const struct sdhci_acpi_slot sdhci_acpi_slot_int_emmc = {
b04fa064 126 .chip = &sdhci_acpi_chip_int,
f25c3372
MP
127 .caps = MMC_CAP_8_BIT_DATA | MMC_CAP_NONREMOVABLE |
128 MMC_CAP_HW_RESET | MMC_CAP_1_8V_DDR,
07a58883
AH
129 .caps2 = MMC_CAP2_HC_ERASE_SZ,
130 .flags = SDHCI_ACPI_RUNTIME_PM,
d61b5946 131 .quirks2 = SDHCI_QUIRK2_PRESET_VALUE_BROKEN,
07a58883
AH
132};
133
e5571397 134static const struct sdhci_acpi_slot sdhci_acpi_slot_int_sdio = {
c6748017 135 .quirks = SDHCI_QUIRK_BROKEN_CARD_DETECTION,
e5571397
AH
136 .quirks2 = SDHCI_QUIRK2_HOST_OFF_CARD_ON,
137 .caps = MMC_CAP_NONREMOVABLE | MMC_CAP_POWER_OFF_CARD,
138 .flags = SDHCI_ACPI_RUNTIME_PM,
139 .pm_caps = MMC_PM_KEEP_POWER,
140};
141
07a58883 142static const struct sdhci_acpi_slot sdhci_acpi_slot_int_sd = {
4fd4409c
AH
143 .flags = SDHCI_ACPI_SD_CD | SDHCI_ACPI_SD_CD_OVERRIDE_LEVEL |
144 SDHCI_ACPI_RUNTIME_PM,
a61abe6e 145 .quirks2 = SDHCI_QUIRK2_CARD_ON_NEEDS_BUS_ON,
07a58883
AH
146};
147
148struct sdhci_acpi_uid_slot {
149 const char *hid;
150 const char *uid;
151 const struct sdhci_acpi_slot *slot;
152};
153
154static const struct sdhci_acpi_uid_slot sdhci_acpi_uids[] = {
155 { "80860F14" , "1" , &sdhci_acpi_slot_int_emmc },
156 { "80860F14" , "3" , &sdhci_acpi_slot_int_sd },
aad95dc4 157 { "80860F16" , NULL, &sdhci_acpi_slot_int_sd },
07a58883
AH
158 { "INT33BB" , "2" , &sdhci_acpi_slot_int_sdio },
159 { "INT33C6" , NULL, &sdhci_acpi_slot_int_sdio },
07c001c1 160 { "INT3436" , NULL, &sdhci_acpi_slot_int_sdio },
07a58883
AH
161 { "PNP0D40" },
162 { },
163};
164
c4e05037 165static const struct acpi_device_id sdhci_acpi_ids[] = {
07a58883 166 { "80860F14" },
aad95dc4 167 { "80860F16" },
07a58883
AH
168 { "INT33BB" },
169 { "INT33C6" },
07c001c1 170 { "INT3436" },
07a58883 171 { "PNP0D40" },
c4e05037
AH
172 { },
173};
174MODULE_DEVICE_TABLE(acpi, sdhci_acpi_ids);
175
07a58883
AH
176static const struct sdhci_acpi_slot *sdhci_acpi_get_slot_by_ids(const char *hid,
177 const char *uid)
c4e05037 178{
07a58883
AH
179 const struct sdhci_acpi_uid_slot *u;
180
181 for (u = sdhci_acpi_uids; u->hid; u++) {
182 if (strcmp(u->hid, hid))
183 continue;
184 if (!u->uid)
185 return u->slot;
186 if (uid && !strcmp(u->uid, uid))
187 return u->slot;
188 }
c4e05037
AH
189 return NULL;
190}
191
07a58883
AH
192static const struct sdhci_acpi_slot *sdhci_acpi_get_slot(acpi_handle handle,
193 const char *hid)
194{
195 const struct sdhci_acpi_slot *slot;
196 struct acpi_device_info *info;
197 const char *uid = NULL;
198 acpi_status status;
199
200 status = acpi_get_object_info(handle, &info);
201 if (!ACPI_FAILURE(status) && (info->valid & ACPI_VALID_UID))
202 uid = info->unique_id.string;
203
204 slot = sdhci_acpi_get_slot_by_ids(hid, uid);
205
206 kfree(info);
207 return slot;
208}
209
4e608e4e 210static int sdhci_acpi_probe(struct platform_device *pdev)
c4e05037
AH
211{
212 struct device *dev = &pdev->dev;
213 acpi_handle handle = ACPI_HANDLE(dev);
214 struct acpi_device *device;
215 struct sdhci_acpi_host *c;
216 struct sdhci_host *host;
217 struct resource *iomem;
218 resource_size_t len;
219 const char *hid;
87875655 220 int err;
c4e05037
AH
221
222 if (acpi_bus_get_device(handle, &device))
223 return -ENODEV;
224
225 if (acpi_bus_get_status(device) || !device->status.present)
226 return -ENODEV;
227
228 hid = acpi_device_hid(device);
229
230 iomem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
231 if (!iomem)
232 return -ENOMEM;
233
234 len = resource_size(iomem);
235 if (len < 0x100)
236 dev_err(dev, "Invalid iomem size!\n");
237
238 if (!devm_request_mem_region(dev, iomem->start, len, dev_name(dev)))
239 return -ENOMEM;
240
241 host = sdhci_alloc_host(dev, sizeof(struct sdhci_acpi_host));
242 if (IS_ERR(host))
243 return PTR_ERR(host);
244
245 c = sdhci_priv(host);
246 c->host = host;
07a58883 247 c->slot = sdhci_acpi_get_slot(handle, hid);
c4e05037
AH
248 c->pdev = pdev;
249 c->use_runtime_pm = sdhci_acpi_flag(c, SDHCI_ACPI_RUNTIME_PM);
250
251 platform_set_drvdata(pdev, c);
252
253 host->hw_name = "ACPI";
254 host->ops = &sdhci_acpi_ops_dflt;
255 host->irq = platform_get_irq(pdev, 0);
256
257 host->ioaddr = devm_ioremap_nocache(dev, iomem->start,
258 resource_size(iomem));
259 if (host->ioaddr == NULL) {
260 err = -ENOMEM;
261 goto err_free;
262 }
263
264 if (!dev->dma_mask) {
265 u64 dma_mask;
266
267 if (sdhci_readl(host, SDHCI_CAPABILITIES) & SDHCI_CAN_64BIT) {
268 /* 64-bit DMA is not supported at present */
269 dma_mask = DMA_BIT_MASK(32);
270 } else {
271 dma_mask = DMA_BIT_MASK(32);
272 }
273
07f4450c
RK
274 err = dma_coerce_mask_and_coherent(dev, dma_mask);
275 if (err)
276 goto err_free;
c4e05037
AH
277 }
278
279 if (c->slot) {
280 if (c->slot->chip) {
281 host->ops = c->slot->chip->ops;
282 host->quirks |= c->slot->chip->quirks;
283 host->quirks2 |= c->slot->chip->quirks2;
284 host->mmc->caps |= c->slot->chip->caps;
285 host->mmc->caps2 |= c->slot->chip->caps2;
286 host->mmc->pm_caps |= c->slot->chip->pm_caps;
287 }
288 host->quirks |= c->slot->quirks;
289 host->quirks2 |= c->slot->quirks2;
290 host->mmc->caps |= c->slot->caps;
291 host->mmc->caps2 |= c->slot->caps2;
292 host->mmc->pm_caps |= c->slot->pm_caps;
293 }
294
0d3e3350
AH
295 host->mmc->caps2 |= MMC_CAP2_NO_PRESCAN_POWERUP;
296
a61abe6e 297 if (sdhci_acpi_flag(c, SDHCI_ACPI_SD_CD)) {
4fd4409c
AH
298 bool v = sdhci_acpi_flag(c, SDHCI_ACPI_SD_CD_OVERRIDE_LEVEL);
299
300 if (mmc_gpiod_request_cd(host->mmc, NULL, 0, v, 0)) {
301 dev_warn(dev, "failed to setup card detect gpio\n");
a61abe6e 302 c->use_runtime_pm = false;
4fd4409c 303 }
a61abe6e
AH
304 }
305
4fd4409c
AH
306 err = sdhci_add_host(host);
307 if (err)
308 goto err_free;
309
c4e05037 310 if (c->use_runtime_pm) {
1d1ff458 311 pm_runtime_set_active(dev);
c4e05037
AH
312 pm_suspend_ignore_children(dev, 1);
313 pm_runtime_set_autosuspend_delay(dev, 50);
314 pm_runtime_use_autosuspend(dev);
315 pm_runtime_enable(dev);
316 }
317
318 return 0;
319
320err_free:
c4e05037
AH
321 sdhci_free_host(c->host);
322 return err;
323}
324
4e608e4e 325static int sdhci_acpi_remove(struct platform_device *pdev)
c4e05037
AH
326{
327 struct sdhci_acpi_host *c = platform_get_drvdata(pdev);
328 struct device *dev = &pdev->dev;
329 int dead;
330
331 if (c->use_runtime_pm) {
332 pm_runtime_get_sync(dev);
333 pm_runtime_disable(dev);
334 pm_runtime_put_noidle(dev);
335 }
336
337 dead = (sdhci_readl(c->host, SDHCI_INT_STATUS) == ~0);
338 sdhci_remove_host(c->host, dead);
c4e05037
AH
339 sdhci_free_host(c->host);
340
341 return 0;
342}
343
344#ifdef CONFIG_PM_SLEEP
345
346static int sdhci_acpi_suspend(struct device *dev)
347{
348 struct sdhci_acpi_host *c = dev_get_drvdata(dev);
349
350 return sdhci_suspend_host(c->host);
351}
352
353static int sdhci_acpi_resume(struct device *dev)
354{
355 struct sdhci_acpi_host *c = dev_get_drvdata(dev);
356
357 return sdhci_resume_host(c->host);
358}
359
360#else
361
362#define sdhci_acpi_suspend NULL
363#define sdhci_acpi_resume NULL
364
365#endif
366
367#ifdef CONFIG_PM_RUNTIME
368
369static int sdhci_acpi_runtime_suspend(struct device *dev)
370{
371 struct sdhci_acpi_host *c = dev_get_drvdata(dev);
372
373 return sdhci_runtime_suspend_host(c->host);
374}
375
376static int sdhci_acpi_runtime_resume(struct device *dev)
377{
378 struct sdhci_acpi_host *c = dev_get_drvdata(dev);
379
380 return sdhci_runtime_resume_host(c->host);
381}
382
383static int sdhci_acpi_runtime_idle(struct device *dev)
384{
385 return 0;
386}
387
388#else
389
390#define sdhci_acpi_runtime_suspend NULL
391#define sdhci_acpi_runtime_resume NULL
392#define sdhci_acpi_runtime_idle NULL
393
394#endif
395
396static const struct dev_pm_ops sdhci_acpi_pm_ops = {
397 .suspend = sdhci_acpi_suspend,
398 .resume = sdhci_acpi_resume,
399 .runtime_suspend = sdhci_acpi_runtime_suspend,
400 .runtime_resume = sdhci_acpi_runtime_resume,
401 .runtime_idle = sdhci_acpi_runtime_idle,
402};
403
404static struct platform_driver sdhci_acpi_driver = {
405 .driver = {
406 .name = "sdhci-acpi",
407 .owner = THIS_MODULE,
408 .acpi_match_table = sdhci_acpi_ids,
409 .pm = &sdhci_acpi_pm_ops,
410 },
411 .probe = sdhci_acpi_probe,
4e608e4e 412 .remove = sdhci_acpi_remove,
c4e05037
AH
413};
414
415module_platform_driver(sdhci_acpi_driver);
416
417MODULE_DESCRIPTION("Secure Digital Host Controller Interface ACPI driver");
418MODULE_AUTHOR("Adrian Hunter");
419MODULE_LICENSE("GPL v2");