Adrian Hunter | c4e0503 | 2012-11-23 21:17:34 +0100 | [diff] [blame] | 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> |
Adrian Hunter | b04fa06 | 2013-06-13 11:50:27 +0300 | [diff] [blame] | 38 | #include <linux/delay.h> |
Adrian Hunter | c4e0503 | 2012-11-23 21:17:34 +0100 | [diff] [blame] | 39 | |
| 40 | #include <linux/mmc/host.h> |
| 41 | #include <linux/mmc/pm.h> |
Adrian Hunter | 4fd4409 | 2014-03-10 15:02:42 +0200 | [diff] [blame] | 42 | #include <linux/mmc/slot-gpio.h> |
Adrian Hunter | c4e0503 | 2012-11-23 21:17:34 +0100 | [diff] [blame] | 43 | |
| 44 | #include "sdhci.h" |
| 45 | |
| 46 | enum { |
Adrian Hunter | 4fd4409 | 2014-03-10 15:02:42 +0200 | [diff] [blame] | 47 | SDHCI_ACPI_SD_CD = BIT(0), |
| 48 | SDHCI_ACPI_RUNTIME_PM = BIT(1), |
| 49 | SDHCI_ACPI_SD_CD_OVERRIDE_LEVEL = BIT(2), |
Adrian Hunter | c4e0503 | 2012-11-23 21:17:34 +0100 | [diff] [blame] | 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; |
Adrian Hunter | 7dafca8 | 2014-10-06 15:23:06 +0300 | [diff] [blame] | 69 | int (*probe_slot)(struct platform_device *, const char *, const char *); |
Gao, Yunpeng | 578b36b | 2014-09-01 11:35:40 +0800 | [diff] [blame] | 70 | int (*remove_slot)(struct platform_device *); |
Adrian Hunter | c4e0503 | 2012-11-23 21:17:34 +0100 | [diff] [blame] | 71 | }; |
| 72 | |
| 73 | struct sdhci_acpi_host { |
| 74 | struct sdhci_host *host; |
| 75 | const struct sdhci_acpi_slot *slot; |
| 76 | struct platform_device *pdev; |
| 77 | bool use_runtime_pm; |
Adrian Hunter | 4f64f84 | 2014-11-04 12:42:47 +0200 | [diff] [blame] | 78 | bool dma_setup; |
Adrian Hunter | c4e0503 | 2012-11-23 21:17:34 +0100 | [diff] [blame] | 79 | }; |
| 80 | |
| 81 | static inline bool sdhci_acpi_flag(struct sdhci_acpi_host *c, unsigned int flag) |
| 82 | { |
| 83 | return c->slot && (c->slot->flags & flag); |
| 84 | } |
| 85 | |
| 86 | static int sdhci_acpi_enable_dma(struct sdhci_host *host) |
| 87 | { |
Adrian Hunter | 4f64f84 | 2014-11-04 12:42:47 +0200 | [diff] [blame] | 88 | struct sdhci_acpi_host *c = sdhci_priv(host); |
| 89 | struct device *dev = &c->pdev->dev; |
| 90 | int err = -1; |
| 91 | |
| 92 | if (c->dma_setup) |
| 93 | return 0; |
| 94 | |
| 95 | if (host->flags & SDHCI_USE_64_BIT_DMA) { |
| 96 | if (host->quirks2 & SDHCI_QUIRK2_BROKEN_64_BIT_DMA) { |
| 97 | host->flags &= ~SDHCI_USE_64_BIT_DMA; |
| 98 | } else { |
| 99 | err = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(64)); |
| 100 | if (err) |
| 101 | dev_warn(dev, "Failed to set 64-bit DMA mask\n"); |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | if (err) |
| 106 | err = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(32)); |
| 107 | |
| 108 | c->dma_setup = !err; |
| 109 | |
| 110 | return err; |
Adrian Hunter | c4e0503 | 2012-11-23 21:17:34 +0100 | [diff] [blame] | 111 | } |
| 112 | |
Adrian Hunter | b04fa06 | 2013-06-13 11:50:27 +0300 | [diff] [blame] | 113 | static void sdhci_acpi_int_hw_reset(struct sdhci_host *host) |
| 114 | { |
| 115 | u8 reg; |
| 116 | |
| 117 | reg = sdhci_readb(host, SDHCI_POWER_CONTROL); |
| 118 | reg |= 0x10; |
| 119 | sdhci_writeb(host, reg, SDHCI_POWER_CONTROL); |
| 120 | /* For eMMC, minimum is 1us but give it 9us for good measure */ |
| 121 | udelay(9); |
| 122 | reg &= ~0x10; |
| 123 | sdhci_writeb(host, reg, SDHCI_POWER_CONTROL); |
| 124 | /* For eMMC, minimum is 200us but give it 300us for good measure */ |
| 125 | usleep_range(300, 1000); |
| 126 | } |
| 127 | |
Adrian Hunter | c4e0503 | 2012-11-23 21:17:34 +0100 | [diff] [blame] | 128 | static const struct sdhci_ops sdhci_acpi_ops_dflt = { |
Russell King | 1771059 | 2014-04-25 12:58:55 +0100 | [diff] [blame] | 129 | .set_clock = sdhci_set_clock, |
Adrian Hunter | c4e0503 | 2012-11-23 21:17:34 +0100 | [diff] [blame] | 130 | .enable_dma = sdhci_acpi_enable_dma, |
Russell King | 2317f56 | 2014-04-25 12:57:07 +0100 | [diff] [blame] | 131 | .set_bus_width = sdhci_set_bus_width, |
Russell King | 03231f9 | 2014-04-25 12:57:12 +0100 | [diff] [blame] | 132 | .reset = sdhci_reset, |
Russell King | 96d7b78 | 2014-04-25 12:59:26 +0100 | [diff] [blame] | 133 | .set_uhs_signaling = sdhci_set_uhs_signaling, |
Adrian Hunter | c4e0503 | 2012-11-23 21:17:34 +0100 | [diff] [blame] | 134 | }; |
| 135 | |
Adrian Hunter | b04fa06 | 2013-06-13 11:50:27 +0300 | [diff] [blame] | 136 | static const struct sdhci_ops sdhci_acpi_ops_int = { |
Russell King | 1771059 | 2014-04-25 12:58:55 +0100 | [diff] [blame] | 137 | .set_clock = sdhci_set_clock, |
Adrian Hunter | b04fa06 | 2013-06-13 11:50:27 +0300 | [diff] [blame] | 138 | .enable_dma = sdhci_acpi_enable_dma, |
Russell King | 2317f56 | 2014-04-25 12:57:07 +0100 | [diff] [blame] | 139 | .set_bus_width = sdhci_set_bus_width, |
Russell King | 03231f9 | 2014-04-25 12:57:12 +0100 | [diff] [blame] | 140 | .reset = sdhci_reset, |
Russell King | 96d7b78 | 2014-04-25 12:59:26 +0100 | [diff] [blame] | 141 | .set_uhs_signaling = sdhci_set_uhs_signaling, |
Adrian Hunter | b04fa06 | 2013-06-13 11:50:27 +0300 | [diff] [blame] | 142 | .hw_reset = sdhci_acpi_int_hw_reset, |
| 143 | }; |
| 144 | |
| 145 | static const struct sdhci_acpi_chip sdhci_acpi_chip_int = { |
| 146 | .ops = &sdhci_acpi_ops_int, |
| 147 | }; |
| 148 | |
Adrian Hunter | 7dafca8 | 2014-10-06 15:23:06 +0300 | [diff] [blame] | 149 | static int sdhci_acpi_emmc_probe_slot(struct platform_device *pdev, |
| 150 | const char *hid, const char *uid) |
Gao, Yunpeng | 578b36b | 2014-09-01 11:35:40 +0800 | [diff] [blame] | 151 | { |
| 152 | struct sdhci_acpi_host *c = platform_get_drvdata(pdev); |
| 153 | struct sdhci_host *host; |
| 154 | |
| 155 | if (!c || !c->host) |
| 156 | return 0; |
| 157 | |
| 158 | host = c->host; |
| 159 | |
Andy Shevchenko | 9420304 | 2015-01-12 19:37:25 +0200 | [diff] [blame] | 160 | /* Platform specific code during emmc probe slot goes here */ |
Gao, Yunpeng | 578b36b | 2014-09-01 11:35:40 +0800 | [diff] [blame] | 161 | |
Adrian Hunter | 8024379 | 2014-10-06 15:23:07 +0300 | [diff] [blame] | 162 | if (hid && uid && !strcmp(hid, "80860F14") && !strcmp(uid, "1") && |
| 163 | sdhci_readl(host, SDHCI_CAPABILITIES) == 0x446cc8b2 && |
| 164 | sdhci_readl(host, SDHCI_CAPABILITIES_1) == 0x00000807) |
| 165 | host->timeout_clk = 1000; /* 1000 kHz i.e. 1 MHz */ |
| 166 | |
Gao, Yunpeng | 578b36b | 2014-09-01 11:35:40 +0800 | [diff] [blame] | 167 | return 0; |
| 168 | } |
| 169 | |
Adrian Hunter | 7dafca8 | 2014-10-06 15:23:06 +0300 | [diff] [blame] | 170 | static int sdhci_acpi_sdio_probe_slot(struct platform_device *pdev, |
| 171 | const char *hid, const char *uid) |
Gao, Yunpeng | 578b36b | 2014-09-01 11:35:40 +0800 | [diff] [blame] | 172 | { |
| 173 | struct sdhci_acpi_host *c = platform_get_drvdata(pdev); |
| 174 | struct sdhci_host *host; |
| 175 | |
| 176 | if (!c || !c->host) |
| 177 | return 0; |
| 178 | |
| 179 | host = c->host; |
| 180 | |
Andy Shevchenko | 9420304 | 2015-01-12 19:37:25 +0200 | [diff] [blame] | 181 | /* Platform specific code during sdio probe slot goes here */ |
Gao, Yunpeng | 578b36b | 2014-09-01 11:35:40 +0800 | [diff] [blame] | 182 | |
| 183 | return 0; |
| 184 | } |
| 185 | |
Adrian Hunter | 7dafca8 | 2014-10-06 15:23:06 +0300 | [diff] [blame] | 186 | static int sdhci_acpi_sd_probe_slot(struct platform_device *pdev, |
| 187 | const char *hid, const char *uid) |
Gao, Yunpeng | 578b36b | 2014-09-01 11:35:40 +0800 | [diff] [blame] | 188 | { |
| 189 | struct sdhci_acpi_host *c = platform_get_drvdata(pdev); |
| 190 | struct sdhci_host *host; |
| 191 | |
| 192 | if (!c || !c->host || !c->slot) |
| 193 | return 0; |
| 194 | |
| 195 | host = c->host; |
| 196 | |
Andy Shevchenko | 9420304 | 2015-01-12 19:37:25 +0200 | [diff] [blame] | 197 | /* Platform specific code during sd probe slot goes here */ |
Gao, Yunpeng | 578b36b | 2014-09-01 11:35:40 +0800 | [diff] [blame] | 198 | |
| 199 | return 0; |
| 200 | } |
| 201 | |
Adrian Hunter | 07a5888 | 2013-04-26 11:27:22 +0300 | [diff] [blame] | 202 | static const struct sdhci_acpi_slot sdhci_acpi_slot_int_emmc = { |
Adrian Hunter | b04fa06 | 2013-06-13 11:50:27 +0300 | [diff] [blame] | 203 | .chip = &sdhci_acpi_chip_int, |
Maurice Petallo | f25c337 | 2014-07-08 19:11:01 +0800 | [diff] [blame] | 204 | .caps = MMC_CAP_8_BIT_DATA | MMC_CAP_NONREMOVABLE | |
Adrian Hunter | 9d65cb8 | 2014-12-01 15:51:19 +0200 | [diff] [blame] | 205 | MMC_CAP_HW_RESET | MMC_CAP_1_8V_DDR | |
| 206 | MMC_CAP_BUS_WIDTH_TEST | MMC_CAP_WAIT_WHILE_BUSY, |
Adrian Hunter | 07a5888 | 2013-04-26 11:27:22 +0300 | [diff] [blame] | 207 | .caps2 = MMC_CAP2_HC_ERASE_SZ, |
| 208 | .flags = SDHCI_ACPI_RUNTIME_PM, |
Adrian Hunter | e1f5633 | 2014-12-01 15:51:17 +0200 | [diff] [blame] | 209 | .quirks = SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC, |
Adrian Hunter | e839b13 | 2015-10-21 11:15:46 +0300 | [diff] [blame] | 210 | .quirks2 = SDHCI_QUIRK2_PRESET_VALUE_BROKEN | |
| 211 | SDHCI_QUIRK2_STOP_WITH_TC | |
| 212 | SDHCI_QUIRK2_CAPS_BIT63_FOR_HS400, |
Gao, Yunpeng | 578b36b | 2014-09-01 11:35:40 +0800 | [diff] [blame] | 213 | .probe_slot = sdhci_acpi_emmc_probe_slot, |
Adrian Hunter | 07a5888 | 2013-04-26 11:27:22 +0300 | [diff] [blame] | 214 | }; |
| 215 | |
Adrian Hunter | e557139 | 2012-12-10 21:18:48 +0100 | [diff] [blame] | 216 | static const struct sdhci_acpi_slot sdhci_acpi_slot_int_sdio = { |
Adrian Hunter | e1f5633 | 2014-12-01 15:51:17 +0200 | [diff] [blame] | 217 | .quirks = SDHCI_QUIRK_BROKEN_CARD_DETECTION | |
| 218 | SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC, |
Adrian Hunter | e557139 | 2012-12-10 21:18:48 +0100 | [diff] [blame] | 219 | .quirks2 = SDHCI_QUIRK2_HOST_OFF_CARD_ON, |
Adrian Hunter | 9d65cb8 | 2014-12-01 15:51:19 +0200 | [diff] [blame] | 220 | .caps = MMC_CAP_NONREMOVABLE | MMC_CAP_POWER_OFF_CARD | |
| 221 | MMC_CAP_BUS_WIDTH_TEST | MMC_CAP_WAIT_WHILE_BUSY, |
Adrian Hunter | e557139 | 2012-12-10 21:18:48 +0100 | [diff] [blame] | 222 | .flags = SDHCI_ACPI_RUNTIME_PM, |
| 223 | .pm_caps = MMC_PM_KEEP_POWER, |
Gao, Yunpeng | 578b36b | 2014-09-01 11:35:40 +0800 | [diff] [blame] | 224 | .probe_slot = sdhci_acpi_sdio_probe_slot, |
Adrian Hunter | e557139 | 2012-12-10 21:18:48 +0100 | [diff] [blame] | 225 | }; |
| 226 | |
Adrian Hunter | 07a5888 | 2013-04-26 11:27:22 +0300 | [diff] [blame] | 227 | static const struct sdhci_acpi_slot sdhci_acpi_slot_int_sd = { |
Adrian Hunter | 4fd4409 | 2014-03-10 15:02:42 +0200 | [diff] [blame] | 228 | .flags = SDHCI_ACPI_SD_CD | SDHCI_ACPI_SD_CD_OVERRIDE_LEVEL | |
| 229 | SDHCI_ACPI_RUNTIME_PM, |
Adrian Hunter | e1f5633 | 2014-12-01 15:51:17 +0200 | [diff] [blame] | 230 | .quirks = SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC, |
Adrian Hunter | 934e31b | 2014-09-24 10:27:28 +0300 | [diff] [blame] | 231 | .quirks2 = SDHCI_QUIRK2_CARD_ON_NEEDS_BUS_ON | |
| 232 | SDHCI_QUIRK2_STOP_WITH_TC, |
Adrian Hunter | 9d65cb8 | 2014-12-01 15:51:19 +0200 | [diff] [blame] | 233 | .caps = MMC_CAP_BUS_WIDTH_TEST | MMC_CAP_WAIT_WHILE_BUSY, |
Gao, Yunpeng | 578b36b | 2014-09-01 11:35:40 +0800 | [diff] [blame] | 234 | .probe_slot = sdhci_acpi_sd_probe_slot, |
Adrian Hunter | 07a5888 | 2013-04-26 11:27:22 +0300 | [diff] [blame] | 235 | }; |
| 236 | |
| 237 | struct sdhci_acpi_uid_slot { |
| 238 | const char *hid; |
| 239 | const char *uid; |
| 240 | const struct sdhci_acpi_slot *slot; |
| 241 | }; |
| 242 | |
| 243 | static const struct sdhci_acpi_uid_slot sdhci_acpi_uids[] = { |
Adrian Hunter | e839b13 | 2015-10-21 11:15:46 +0300 | [diff] [blame] | 244 | { "80865ACA", NULL, &sdhci_acpi_slot_int_sd }, |
| 245 | { "80865ACC", NULL, &sdhci_acpi_slot_int_emmc }, |
| 246 | { "80865AD0", NULL, &sdhci_acpi_slot_int_sdio }, |
Adrian Hunter | 07a5888 | 2013-04-26 11:27:22 +0300 | [diff] [blame] | 247 | { "80860F14" , "1" , &sdhci_acpi_slot_int_emmc }, |
| 248 | { "80860F14" , "3" , &sdhci_acpi_slot_int_sd }, |
Adrian Hunter | aad95dc | 2014-03-10 15:02:43 +0200 | [diff] [blame] | 249 | { "80860F16" , NULL, &sdhci_acpi_slot_int_sd }, |
Adrian Hunter | 07a5888 | 2013-04-26 11:27:22 +0300 | [diff] [blame] | 250 | { "INT33BB" , "2" , &sdhci_acpi_slot_int_sdio }, |
Adrian Hunter | 7147eaf | 2014-09-24 10:27:29 +0300 | [diff] [blame] | 251 | { "INT33BB" , "3" , &sdhci_acpi_slot_int_sd }, |
Adrian Hunter | 07a5888 | 2013-04-26 11:27:22 +0300 | [diff] [blame] | 252 | { "INT33C6" , NULL, &sdhci_acpi_slot_int_sdio }, |
Mika Westerberg | 07c001c | 2013-11-12 12:01:33 +0200 | [diff] [blame] | 253 | { "INT3436" , NULL, &sdhci_acpi_slot_int_sdio }, |
Adrian Hunter | d0ed8e6 | 2015-01-05 14:47:57 +0200 | [diff] [blame] | 254 | { "INT344D" , NULL, &sdhci_acpi_slot_int_sdio }, |
Michele Curti | 0cd2f04 | 2015-09-05 08:49:52 +0200 | [diff] [blame] | 255 | { "PNP0FFF" , "3" , &sdhci_acpi_slot_int_sd }, |
Adrian Hunter | 07a5888 | 2013-04-26 11:27:22 +0300 | [diff] [blame] | 256 | { "PNP0D40" }, |
| 257 | { }, |
| 258 | }; |
| 259 | |
Adrian Hunter | c4e0503 | 2012-11-23 21:17:34 +0100 | [diff] [blame] | 260 | static const struct acpi_device_id sdhci_acpi_ids[] = { |
Adrian Hunter | e839b13 | 2015-10-21 11:15:46 +0300 | [diff] [blame] | 261 | { "80865ACA" }, |
| 262 | { "80865ACC" }, |
| 263 | { "80865AD0" }, |
Adrian Hunter | 07a5888 | 2013-04-26 11:27:22 +0300 | [diff] [blame] | 264 | { "80860F14" }, |
Adrian Hunter | aad95dc | 2014-03-10 15:02:43 +0200 | [diff] [blame] | 265 | { "80860F16" }, |
Adrian Hunter | 07a5888 | 2013-04-26 11:27:22 +0300 | [diff] [blame] | 266 | { "INT33BB" }, |
| 267 | { "INT33C6" }, |
Mika Westerberg | 07c001c | 2013-11-12 12:01:33 +0200 | [diff] [blame] | 268 | { "INT3436" }, |
Adrian Hunter | d0ed8e6 | 2015-01-05 14:47:57 +0200 | [diff] [blame] | 269 | { "INT344D" }, |
Adrian Hunter | 07a5888 | 2013-04-26 11:27:22 +0300 | [diff] [blame] | 270 | { "PNP0D40" }, |
Adrian Hunter | c4e0503 | 2012-11-23 21:17:34 +0100 | [diff] [blame] | 271 | { }, |
| 272 | }; |
| 273 | MODULE_DEVICE_TABLE(acpi, sdhci_acpi_ids); |
| 274 | |
Adrian Hunter | 3db3525 | 2014-10-06 15:23:05 +0300 | [diff] [blame] | 275 | static const struct sdhci_acpi_slot *sdhci_acpi_get_slot(const char *hid, |
| 276 | const char *uid) |
Adrian Hunter | c4e0503 | 2012-11-23 21:17:34 +0100 | [diff] [blame] | 277 | { |
Adrian Hunter | 07a5888 | 2013-04-26 11:27:22 +0300 | [diff] [blame] | 278 | const struct sdhci_acpi_uid_slot *u; |
Adrian Hunter | c4e0503 | 2012-11-23 21:17:34 +0100 | [diff] [blame] | 279 | |
Adrian Hunter | 07a5888 | 2013-04-26 11:27:22 +0300 | [diff] [blame] | 280 | for (u = sdhci_acpi_uids; u->hid; u++) { |
| 281 | if (strcmp(u->hid, hid)) |
| 282 | continue; |
| 283 | if (!u->uid) |
| 284 | return u->slot; |
| 285 | if (uid && !strcmp(u->uid, uid)) |
| 286 | return u->slot; |
| 287 | } |
Adrian Hunter | c4e0503 | 2012-11-23 21:17:34 +0100 | [diff] [blame] | 288 | return NULL; |
| 289 | } |
| 290 | |
Greg Kroah-Hartman | 4e608e4 | 2012-12-21 15:05:47 -0800 | [diff] [blame] | 291 | static int sdhci_acpi_probe(struct platform_device *pdev) |
Adrian Hunter | c4e0503 | 2012-11-23 21:17:34 +0100 | [diff] [blame] | 292 | { |
| 293 | struct device *dev = &pdev->dev; |
| 294 | acpi_handle handle = ACPI_HANDLE(dev); |
| 295 | struct acpi_device *device; |
| 296 | struct sdhci_acpi_host *c; |
| 297 | struct sdhci_host *host; |
| 298 | struct resource *iomem; |
| 299 | resource_size_t len; |
| 300 | const char *hid; |
Adrian Hunter | 3db3525 | 2014-10-06 15:23:05 +0300 | [diff] [blame] | 301 | const char *uid; |
Mika Westerberg | 8787565 | 2014-01-08 12:40:55 +0200 | [diff] [blame] | 302 | int err; |
Adrian Hunter | c4e0503 | 2012-11-23 21:17:34 +0100 | [diff] [blame] | 303 | |
| 304 | if (acpi_bus_get_device(handle, &device)) |
| 305 | return -ENODEV; |
| 306 | |
| 307 | if (acpi_bus_get_status(device) || !device->status.present) |
| 308 | return -ENODEV; |
| 309 | |
| 310 | hid = acpi_device_hid(device); |
Adrian Hunter | 3db3525 | 2014-10-06 15:23:05 +0300 | [diff] [blame] | 311 | uid = device->pnp.unique_id; |
Adrian Hunter | c4e0503 | 2012-11-23 21:17:34 +0100 | [diff] [blame] | 312 | |
| 313 | iomem = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 314 | if (!iomem) |
| 315 | return -ENOMEM; |
| 316 | |
| 317 | len = resource_size(iomem); |
| 318 | if (len < 0x100) |
| 319 | dev_err(dev, "Invalid iomem size!\n"); |
| 320 | |
| 321 | if (!devm_request_mem_region(dev, iomem->start, len, dev_name(dev))) |
| 322 | return -ENOMEM; |
| 323 | |
| 324 | host = sdhci_alloc_host(dev, sizeof(struct sdhci_acpi_host)); |
| 325 | if (IS_ERR(host)) |
| 326 | return PTR_ERR(host); |
| 327 | |
| 328 | c = sdhci_priv(host); |
| 329 | c->host = host; |
Adrian Hunter | 3db3525 | 2014-10-06 15:23:05 +0300 | [diff] [blame] | 330 | c->slot = sdhci_acpi_get_slot(hid, uid); |
Adrian Hunter | c4e0503 | 2012-11-23 21:17:34 +0100 | [diff] [blame] | 331 | c->pdev = pdev; |
| 332 | c->use_runtime_pm = sdhci_acpi_flag(c, SDHCI_ACPI_RUNTIME_PM); |
| 333 | |
| 334 | platform_set_drvdata(pdev, c); |
| 335 | |
| 336 | host->hw_name = "ACPI"; |
| 337 | host->ops = &sdhci_acpi_ops_dflt; |
| 338 | host->irq = platform_get_irq(pdev, 0); |
| 339 | |
| 340 | host->ioaddr = devm_ioremap_nocache(dev, iomem->start, |
| 341 | resource_size(iomem)); |
| 342 | if (host->ioaddr == NULL) { |
| 343 | err = -ENOMEM; |
| 344 | goto err_free; |
| 345 | } |
| 346 | |
Adrian Hunter | c4e0503 | 2012-11-23 21:17:34 +0100 | [diff] [blame] | 347 | if (c->slot) { |
Gao, Yunpeng | 578b36b | 2014-09-01 11:35:40 +0800 | [diff] [blame] | 348 | if (c->slot->probe_slot) { |
Adrian Hunter | 7dafca8 | 2014-10-06 15:23:06 +0300 | [diff] [blame] | 349 | err = c->slot->probe_slot(pdev, hid, uid); |
Gao, Yunpeng | 578b36b | 2014-09-01 11:35:40 +0800 | [diff] [blame] | 350 | if (err) |
| 351 | goto err_free; |
| 352 | } |
Adrian Hunter | c4e0503 | 2012-11-23 21:17:34 +0100 | [diff] [blame] | 353 | if (c->slot->chip) { |
| 354 | host->ops = c->slot->chip->ops; |
| 355 | host->quirks |= c->slot->chip->quirks; |
| 356 | host->quirks2 |= c->slot->chip->quirks2; |
| 357 | host->mmc->caps |= c->slot->chip->caps; |
| 358 | host->mmc->caps2 |= c->slot->chip->caps2; |
| 359 | host->mmc->pm_caps |= c->slot->chip->pm_caps; |
| 360 | } |
| 361 | host->quirks |= c->slot->quirks; |
| 362 | host->quirks2 |= c->slot->quirks2; |
| 363 | host->mmc->caps |= c->slot->caps; |
| 364 | host->mmc->caps2 |= c->slot->caps2; |
| 365 | host->mmc->pm_caps |= c->slot->pm_caps; |
| 366 | } |
| 367 | |
Adrian Hunter | 0d3e335 | 2013-04-04 16:41:06 +0300 | [diff] [blame] | 368 | host->mmc->caps2 |= MMC_CAP2_NO_PRESCAN_POWERUP; |
| 369 | |
Adrian Hunter | 4fd4409 | 2014-03-10 15:02:42 +0200 | [diff] [blame] | 370 | if (sdhci_acpi_flag(c, SDHCI_ACPI_SD_CD)) { |
| 371 | bool v = sdhci_acpi_flag(c, SDHCI_ACPI_SD_CD_OVERRIDE_LEVEL); |
| 372 | |
Linus Walleij | 89168b4 | 2014-10-02 09:08:46 +0200 | [diff] [blame] | 373 | if (mmc_gpiod_request_cd(host->mmc, NULL, 0, v, 0, NULL)) { |
Adrian Hunter | 4fd4409 | 2014-03-10 15:02:42 +0200 | [diff] [blame] | 374 | dev_warn(dev, "failed to setup card detect gpio\n"); |
| 375 | c->use_runtime_pm = false; |
| 376 | } |
| 377 | } |
| 378 | |
Adrian Hunter | c4e0503 | 2012-11-23 21:17:34 +0100 | [diff] [blame] | 379 | err = sdhci_add_host(host); |
| 380 | if (err) |
| 381 | goto err_free; |
| 382 | |
| 383 | if (c->use_runtime_pm) { |
Adrian Hunter | 1d1ff45 | 2013-04-26 11:27:21 +0300 | [diff] [blame] | 384 | pm_runtime_set_active(dev); |
Adrian Hunter | c4e0503 | 2012-11-23 21:17:34 +0100 | [diff] [blame] | 385 | pm_suspend_ignore_children(dev, 1); |
| 386 | pm_runtime_set_autosuspend_delay(dev, 50); |
| 387 | pm_runtime_use_autosuspend(dev); |
| 388 | pm_runtime_enable(dev); |
| 389 | } |
| 390 | |
| 391 | return 0; |
| 392 | |
| 393 | err_free: |
Adrian Hunter | c4e0503 | 2012-11-23 21:17:34 +0100 | [diff] [blame] | 394 | sdhci_free_host(c->host); |
| 395 | return err; |
| 396 | } |
| 397 | |
Greg Kroah-Hartman | 4e608e4 | 2012-12-21 15:05:47 -0800 | [diff] [blame] | 398 | static int sdhci_acpi_remove(struct platform_device *pdev) |
Adrian Hunter | c4e0503 | 2012-11-23 21:17:34 +0100 | [diff] [blame] | 399 | { |
| 400 | struct sdhci_acpi_host *c = platform_get_drvdata(pdev); |
| 401 | struct device *dev = &pdev->dev; |
| 402 | int dead; |
| 403 | |
| 404 | if (c->use_runtime_pm) { |
| 405 | pm_runtime_get_sync(dev); |
| 406 | pm_runtime_disable(dev); |
| 407 | pm_runtime_put_noidle(dev); |
| 408 | } |
| 409 | |
Gao, Yunpeng | 578b36b | 2014-09-01 11:35:40 +0800 | [diff] [blame] | 410 | if (c->slot && c->slot->remove_slot) |
| 411 | c->slot->remove_slot(pdev); |
| 412 | |
Adrian Hunter | c4e0503 | 2012-11-23 21:17:34 +0100 | [diff] [blame] | 413 | dead = (sdhci_readl(c->host, SDHCI_INT_STATUS) == ~0); |
| 414 | sdhci_remove_host(c->host, dead); |
Adrian Hunter | c4e0503 | 2012-11-23 21:17:34 +0100 | [diff] [blame] | 415 | sdhci_free_host(c->host); |
| 416 | |
| 417 | return 0; |
| 418 | } |
| 419 | |
| 420 | #ifdef CONFIG_PM_SLEEP |
| 421 | |
| 422 | static int sdhci_acpi_suspend(struct device *dev) |
| 423 | { |
| 424 | struct sdhci_acpi_host *c = dev_get_drvdata(dev); |
| 425 | |
| 426 | return sdhci_suspend_host(c->host); |
| 427 | } |
| 428 | |
| 429 | static int sdhci_acpi_resume(struct device *dev) |
| 430 | { |
| 431 | struct sdhci_acpi_host *c = dev_get_drvdata(dev); |
| 432 | |
| 433 | return sdhci_resume_host(c->host); |
| 434 | } |
| 435 | |
| 436 | #else |
| 437 | |
| 438 | #define sdhci_acpi_suspend NULL |
| 439 | #define sdhci_acpi_resume NULL |
| 440 | |
| 441 | #endif |
| 442 | |
Rafael J. Wysocki | 162d6f9 | 2014-12-05 03:05:33 +0100 | [diff] [blame] | 443 | #ifdef CONFIG_PM |
Adrian Hunter | c4e0503 | 2012-11-23 21:17:34 +0100 | [diff] [blame] | 444 | |
| 445 | static int sdhci_acpi_runtime_suspend(struct device *dev) |
| 446 | { |
| 447 | struct sdhci_acpi_host *c = dev_get_drvdata(dev); |
| 448 | |
| 449 | return sdhci_runtime_suspend_host(c->host); |
| 450 | } |
| 451 | |
| 452 | static int sdhci_acpi_runtime_resume(struct device *dev) |
| 453 | { |
| 454 | struct sdhci_acpi_host *c = dev_get_drvdata(dev); |
| 455 | |
| 456 | return sdhci_runtime_resume_host(c->host); |
| 457 | } |
| 458 | |
Adrian Hunter | c4e0503 | 2012-11-23 21:17:34 +0100 | [diff] [blame] | 459 | #endif |
| 460 | |
| 461 | static const struct dev_pm_ops sdhci_acpi_pm_ops = { |
| 462 | .suspend = sdhci_acpi_suspend, |
| 463 | .resume = sdhci_acpi_resume, |
Peter Griffin | 1d75f74 | 2014-08-12 17:14:29 +0100 | [diff] [blame] | 464 | SET_RUNTIME_PM_OPS(sdhci_acpi_runtime_suspend, |
Ulf Hansson | 9b449e9 | 2015-01-02 14:47:00 +0100 | [diff] [blame] | 465 | sdhci_acpi_runtime_resume, NULL) |
Adrian Hunter | c4e0503 | 2012-11-23 21:17:34 +0100 | [diff] [blame] | 466 | }; |
| 467 | |
| 468 | static struct platform_driver sdhci_acpi_driver = { |
| 469 | .driver = { |
| 470 | .name = "sdhci-acpi", |
Adrian Hunter | c4e0503 | 2012-11-23 21:17:34 +0100 | [diff] [blame] | 471 | .acpi_match_table = sdhci_acpi_ids, |
| 472 | .pm = &sdhci_acpi_pm_ops, |
| 473 | }, |
| 474 | .probe = sdhci_acpi_probe, |
Greg Kroah-Hartman | 4e608e4 | 2012-12-21 15:05:47 -0800 | [diff] [blame] | 475 | .remove = sdhci_acpi_remove, |
Adrian Hunter | c4e0503 | 2012-11-23 21:17:34 +0100 | [diff] [blame] | 476 | }; |
| 477 | |
| 478 | module_platform_driver(sdhci_acpi_driver); |
| 479 | |
| 480 | MODULE_DESCRIPTION("Secure Digital Host Controller Interface ACPI driver"); |
| 481 | MODULE_AUTHOR("Adrian Hunter"); |
| 482 | MODULE_LICENSE("GPL v2"); |