sdhci-pltfm.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /*
  2. * sdhci-pltfm.c Support for SDHCI platform devices
  3. * Copyright (c) 2009 Intel Corporation
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 as
  7. * published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17. */
  18. /* Supports:
  19. * SDHCI platform devices
  20. *
  21. * Inspired by sdhci-pci.c, by Pierre Ossman
  22. */
  23. #include <linux/err.h>
  24. #include "sdhci.h"
  25. #include "sdhci-pltfm.h"
  26. static struct sdhci_ops sdhci_pltfm_ops = {
  27. };
  28. struct sdhci_host *sdhci_pltfm_init(struct platform_device *pdev,
  29. struct sdhci_pltfm_data *pdata)
  30. {
  31. struct sdhci_host *host;
  32. struct sdhci_pltfm_host *pltfm_host;
  33. struct resource *iomem;
  34. int ret;
  35. iomem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  36. if (!iomem) {
  37. ret = -ENOMEM;
  38. goto err;
  39. }
  40. if (resource_size(iomem) < 0x100)
  41. dev_err(&pdev->dev, "Invalid iomem size!\n");
  42. /* Some PCI-based MFD need the parent here */
  43. if (pdev->dev.parent != &platform_bus)
  44. host = sdhci_alloc_host(pdev->dev.parent, sizeof(*pltfm_host));
  45. else
  46. host = sdhci_alloc_host(&pdev->dev, sizeof(*pltfm_host));
  47. if (IS_ERR(host)) {
  48. ret = PTR_ERR(host);
  49. goto err;
  50. }
  51. pltfm_host = sdhci_priv(host);
  52. host->hw_name = dev_name(&pdev->dev);
  53. if (pdata && pdata->ops)
  54. host->ops = pdata->ops;
  55. else
  56. host->ops = &sdhci_pltfm_ops;
  57. if (pdata)
  58. host->quirks = pdata->quirks;
  59. host->irq = platform_get_irq(pdev, 0);
  60. if (!request_mem_region(iomem->start, resource_size(iomem),
  61. mmc_hostname(host->mmc))) {
  62. dev_err(&pdev->dev, "cannot request region\n");
  63. ret = -EBUSY;
  64. goto err_request;
  65. }
  66. host->ioaddr = ioremap(iomem->start, resource_size(iomem));
  67. if (!host->ioaddr) {
  68. dev_err(&pdev->dev, "failed to remap registers\n");
  69. ret = -ENOMEM;
  70. goto err_remap;
  71. }
  72. platform_set_drvdata(pdev, host);
  73. return host;
  74. err_remap:
  75. release_mem_region(iomem->start, resource_size(iomem));
  76. err_request:
  77. sdhci_free_host(host);
  78. err:
  79. dev_err(&pdev->dev, "%s failed %d\n", __func__, ret);
  80. return ERR_PTR(ret);
  81. }
  82. void sdhci_pltfm_free(struct platform_device *pdev)
  83. {
  84. struct sdhci_host *host = platform_get_drvdata(pdev);
  85. struct resource *iomem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  86. iounmap(host->ioaddr);
  87. release_mem_region(iomem->start, resource_size(iomem));
  88. sdhci_free_host(host);
  89. platform_set_drvdata(pdev, NULL);
  90. }
  91. int sdhci_pltfm_register(struct platform_device *pdev,
  92. struct sdhci_pltfm_data *pdata)
  93. {
  94. struct sdhci_host *host;
  95. int ret = 0;
  96. host = sdhci_pltfm_init(pdev, pdata);
  97. if (IS_ERR(host))
  98. return PTR_ERR(host);
  99. ret = sdhci_add_host(host);
  100. if (ret)
  101. sdhci_pltfm_free(pdev);
  102. return ret;
  103. }
  104. int sdhci_pltfm_unregister(struct platform_device *pdev)
  105. {
  106. struct sdhci_host *host = platform_get_drvdata(pdev);
  107. int dead = (readl(host->ioaddr + SDHCI_INT_STATUS) == 0xffffffff);
  108. sdhci_remove_host(host, dead);
  109. sdhci_pltfm_free(pdev);
  110. return 0;
  111. }
  112. #ifdef CONFIG_PM
  113. int sdhci_pltfm_suspend(struct platform_device *dev, pm_message_t state)
  114. {
  115. struct sdhci_host *host = platform_get_drvdata(dev);
  116. return sdhci_suspend_host(host, state);
  117. }
  118. int sdhci_pltfm_resume(struct platform_device *dev)
  119. {
  120. struct sdhci_host *host = platform_get_drvdata(dev);
  121. return sdhci_resume_host(host);
  122. }
  123. #endif /* CONFIG_PM */