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