sdhci-acpi.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  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. #include <linux/init.h>
  21. #include <linux/export.h>
  22. #include <linux/module.h>
  23. #include <linux/device.h>
  24. #include <linux/platform_device.h>
  25. #include <linux/ioport.h>
  26. #include <linux/io.h>
  27. #include <linux/dma-mapping.h>
  28. #include <linux/compiler.h>
  29. #include <linux/stddef.h>
  30. #include <linux/bitops.h>
  31. #include <linux/types.h>
  32. #include <linux/err.h>
  33. #include <linux/interrupt.h>
  34. #include <linux/acpi.h>
  35. #include <linux/pm.h>
  36. #include <linux/pm_runtime.h>
  37. #include <linux/mmc/host.h>
  38. #include <linux/mmc/pm.h>
  39. #include <linux/mmc/sdhci.h>
  40. #include "sdhci.h"
  41. enum {
  42. SDHCI_ACPI_SD_CD = BIT(0),
  43. SDHCI_ACPI_RUNTIME_PM = BIT(1),
  44. };
  45. struct sdhci_acpi_chip {
  46. const struct sdhci_ops *ops;
  47. unsigned int quirks;
  48. unsigned int quirks2;
  49. unsigned long caps;
  50. unsigned int caps2;
  51. mmc_pm_flag_t pm_caps;
  52. };
  53. struct sdhci_acpi_slot {
  54. const struct sdhci_acpi_chip *chip;
  55. unsigned int quirks;
  56. unsigned int quirks2;
  57. unsigned long caps;
  58. unsigned int caps2;
  59. mmc_pm_flag_t pm_caps;
  60. unsigned int flags;
  61. };
  62. struct sdhci_acpi_host {
  63. struct sdhci_host *host;
  64. const struct sdhci_acpi_slot *slot;
  65. struct platform_device *pdev;
  66. bool use_runtime_pm;
  67. };
  68. static inline bool sdhci_acpi_flag(struct sdhci_acpi_host *c, unsigned int flag)
  69. {
  70. return c->slot && (c->slot->flags & flag);
  71. }
  72. static int sdhci_acpi_enable_dma(struct sdhci_host *host)
  73. {
  74. return 0;
  75. }
  76. static const struct sdhci_ops sdhci_acpi_ops_dflt = {
  77. .enable_dma = sdhci_acpi_enable_dma,
  78. };
  79. static const struct sdhci_acpi_slot sdhci_acpi_slot_int_sdio = {
  80. .quirks2 = SDHCI_QUIRK2_HOST_OFF_CARD_ON,
  81. .caps = MMC_CAP_NONREMOVABLE | MMC_CAP_POWER_OFF_CARD,
  82. .flags = SDHCI_ACPI_RUNTIME_PM,
  83. .pm_caps = MMC_PM_KEEP_POWER,
  84. };
  85. static const struct acpi_device_id sdhci_acpi_ids[] = {
  86. { "INT33C6", (kernel_ulong_t)&sdhci_acpi_slot_int_sdio },
  87. { "PNP0D40" },
  88. { },
  89. };
  90. MODULE_DEVICE_TABLE(acpi, sdhci_acpi_ids);
  91. static const struct sdhci_acpi_slot *sdhci_acpi_get_slot(const char *hid)
  92. {
  93. const struct acpi_device_id *id;
  94. for (id = sdhci_acpi_ids; id->id[0]; id++)
  95. if (!strcmp(id->id, hid))
  96. return (const struct sdhci_acpi_slot *)id->driver_data;
  97. return NULL;
  98. }
  99. static int sdhci_acpi_probe(struct platform_device *pdev)
  100. {
  101. struct device *dev = &pdev->dev;
  102. acpi_handle handle = ACPI_HANDLE(dev);
  103. struct acpi_device *device;
  104. struct sdhci_acpi_host *c;
  105. struct sdhci_host *host;
  106. struct resource *iomem;
  107. resource_size_t len;
  108. const char *hid;
  109. int err;
  110. if (acpi_bus_get_device(handle, &device))
  111. return -ENODEV;
  112. if (acpi_bus_get_status(device) || !device->status.present)
  113. return -ENODEV;
  114. hid = acpi_device_hid(device);
  115. iomem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  116. if (!iomem)
  117. return -ENOMEM;
  118. len = resource_size(iomem);
  119. if (len < 0x100)
  120. dev_err(dev, "Invalid iomem size!\n");
  121. if (!devm_request_mem_region(dev, iomem->start, len, dev_name(dev)))
  122. return -ENOMEM;
  123. host = sdhci_alloc_host(dev, sizeof(struct sdhci_acpi_host));
  124. if (IS_ERR(host))
  125. return PTR_ERR(host);
  126. c = sdhci_priv(host);
  127. c->host = host;
  128. c->slot = sdhci_acpi_get_slot(hid);
  129. c->pdev = pdev;
  130. c->use_runtime_pm = sdhci_acpi_flag(c, SDHCI_ACPI_RUNTIME_PM);
  131. platform_set_drvdata(pdev, c);
  132. host->hw_name = "ACPI";
  133. host->ops = &sdhci_acpi_ops_dflt;
  134. host->irq = platform_get_irq(pdev, 0);
  135. host->ioaddr = devm_ioremap_nocache(dev, iomem->start,
  136. resource_size(iomem));
  137. if (host->ioaddr == NULL) {
  138. err = -ENOMEM;
  139. goto err_free;
  140. }
  141. if (!dev->dma_mask) {
  142. u64 dma_mask;
  143. if (sdhci_readl(host, SDHCI_CAPABILITIES) & SDHCI_CAN_64BIT) {
  144. /* 64-bit DMA is not supported at present */
  145. dma_mask = DMA_BIT_MASK(32);
  146. } else {
  147. dma_mask = DMA_BIT_MASK(32);
  148. }
  149. dev->dma_mask = &dev->coherent_dma_mask;
  150. dev->coherent_dma_mask = dma_mask;
  151. }
  152. if (c->slot) {
  153. if (c->slot->chip) {
  154. host->ops = c->slot->chip->ops;
  155. host->quirks |= c->slot->chip->quirks;
  156. host->quirks2 |= c->slot->chip->quirks2;
  157. host->mmc->caps |= c->slot->chip->caps;
  158. host->mmc->caps2 |= c->slot->chip->caps2;
  159. host->mmc->pm_caps |= c->slot->chip->pm_caps;
  160. }
  161. host->quirks |= c->slot->quirks;
  162. host->quirks2 |= c->slot->quirks2;
  163. host->mmc->caps |= c->slot->caps;
  164. host->mmc->caps2 |= c->slot->caps2;
  165. host->mmc->pm_caps |= c->slot->pm_caps;
  166. }
  167. err = sdhci_add_host(host);
  168. if (err)
  169. goto err_free;
  170. if (c->use_runtime_pm) {
  171. pm_suspend_ignore_children(dev, 1);
  172. pm_runtime_set_autosuspend_delay(dev, 50);
  173. pm_runtime_use_autosuspend(dev);
  174. pm_runtime_enable(dev);
  175. }
  176. return 0;
  177. err_free:
  178. platform_set_drvdata(pdev, NULL);
  179. sdhci_free_host(c->host);
  180. return err;
  181. }
  182. static int sdhci_acpi_remove(struct platform_device *pdev)
  183. {
  184. struct sdhci_acpi_host *c = platform_get_drvdata(pdev);
  185. struct device *dev = &pdev->dev;
  186. int dead;
  187. if (c->use_runtime_pm) {
  188. pm_runtime_get_sync(dev);
  189. pm_runtime_disable(dev);
  190. pm_runtime_put_noidle(dev);
  191. }
  192. dead = (sdhci_readl(c->host, SDHCI_INT_STATUS) == ~0);
  193. sdhci_remove_host(c->host, dead);
  194. platform_set_drvdata(pdev, NULL);
  195. sdhci_free_host(c->host);
  196. return 0;
  197. }
  198. #ifdef CONFIG_PM_SLEEP
  199. static int sdhci_acpi_suspend(struct device *dev)
  200. {
  201. struct sdhci_acpi_host *c = dev_get_drvdata(dev);
  202. return sdhci_suspend_host(c->host);
  203. }
  204. static int sdhci_acpi_resume(struct device *dev)
  205. {
  206. struct sdhci_acpi_host *c = dev_get_drvdata(dev);
  207. return sdhci_resume_host(c->host);
  208. }
  209. #else
  210. #define sdhci_acpi_suspend NULL
  211. #define sdhci_acpi_resume NULL
  212. #endif
  213. #ifdef CONFIG_PM_RUNTIME
  214. static int sdhci_acpi_runtime_suspend(struct device *dev)
  215. {
  216. struct sdhci_acpi_host *c = dev_get_drvdata(dev);
  217. return sdhci_runtime_suspend_host(c->host);
  218. }
  219. static int sdhci_acpi_runtime_resume(struct device *dev)
  220. {
  221. struct sdhci_acpi_host *c = dev_get_drvdata(dev);
  222. return sdhci_runtime_resume_host(c->host);
  223. }
  224. static int sdhci_acpi_runtime_idle(struct device *dev)
  225. {
  226. return 0;
  227. }
  228. #else
  229. #define sdhci_acpi_runtime_suspend NULL
  230. #define sdhci_acpi_runtime_resume NULL
  231. #define sdhci_acpi_runtime_idle NULL
  232. #endif
  233. static const struct dev_pm_ops sdhci_acpi_pm_ops = {
  234. .suspend = sdhci_acpi_suspend,
  235. .resume = sdhci_acpi_resume,
  236. .runtime_suspend = sdhci_acpi_runtime_suspend,
  237. .runtime_resume = sdhci_acpi_runtime_resume,
  238. .runtime_idle = sdhci_acpi_runtime_idle,
  239. };
  240. static struct platform_driver sdhci_acpi_driver = {
  241. .driver = {
  242. .name = "sdhci-acpi",
  243. .owner = THIS_MODULE,
  244. .acpi_match_table = sdhci_acpi_ids,
  245. .pm = &sdhci_acpi_pm_ops,
  246. },
  247. .probe = sdhci_acpi_probe,
  248. .remove = sdhci_acpi_remove,
  249. };
  250. module_platform_driver(sdhci_acpi_driver);
  251. MODULE_DESCRIPTION("Secure Digital Host Controller Interface ACPI driver");
  252. MODULE_AUTHOR("Adrian Hunter");
  253. MODULE_LICENSE("GPL v2");