sdhci-spear.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. /*
  2. * drivers/mmc/host/sdhci-spear.c
  3. *
  4. * Support of SDHCI platform devices for spear soc family
  5. *
  6. * Copyright (C) 2010 ST Microelectronics
  7. * Viresh Kumar <vireshk@kernel.org>
  8. *
  9. * Inspired by sdhci-pltfm.c
  10. *
  11. * This file is licensed under the terms of the GNU General Public
  12. * License version 2. This program is licensed "as is" without any
  13. * warranty of any kind, whether express or implied.
  14. */
  15. #include <linux/clk.h>
  16. #include <linux/delay.h>
  17. #include <linux/highmem.h>
  18. #include <linux/module.h>
  19. #include <linux/interrupt.h>
  20. #include <linux/irq.h>
  21. #include <linux/of.h>
  22. #include <linux/platform_device.h>
  23. #include <linux/pm.h>
  24. #include <linux/slab.h>
  25. #include <linux/mmc/host.h>
  26. #include <linux/mmc/slot-gpio.h>
  27. #include <linux/io.h>
  28. #include "sdhci.h"
  29. struct spear_sdhci {
  30. struct clk *clk;
  31. };
  32. /* sdhci ops */
  33. static const struct sdhci_ops sdhci_pltfm_ops = {
  34. .set_clock = sdhci_set_clock,
  35. .set_bus_width = sdhci_set_bus_width,
  36. .reset = sdhci_reset,
  37. .set_uhs_signaling = sdhci_set_uhs_signaling,
  38. };
  39. static int sdhci_probe(struct platform_device *pdev)
  40. {
  41. struct sdhci_host *host;
  42. struct resource *iomem;
  43. struct spear_sdhci *sdhci;
  44. struct device *dev;
  45. int ret;
  46. dev = pdev->dev.parent ? pdev->dev.parent : &pdev->dev;
  47. host = sdhci_alloc_host(dev, sizeof(*sdhci));
  48. if (IS_ERR(host)) {
  49. ret = PTR_ERR(host);
  50. dev_dbg(&pdev->dev, "cannot allocate memory for sdhci\n");
  51. goto err;
  52. }
  53. iomem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  54. host->ioaddr = devm_ioremap_resource(&pdev->dev, iomem);
  55. if (IS_ERR(host->ioaddr)) {
  56. ret = PTR_ERR(host->ioaddr);
  57. dev_dbg(&pdev->dev, "unable to map iomem: %d\n", ret);
  58. goto err_host;
  59. }
  60. host->hw_name = "sdhci";
  61. host->ops = &sdhci_pltfm_ops;
  62. host->irq = platform_get_irq(pdev, 0);
  63. if (host->irq <= 0) {
  64. ret = -EINVAL;
  65. goto err_host;
  66. }
  67. host->quirks = SDHCI_QUIRK_BROKEN_ADMA;
  68. sdhci = sdhci_priv(host);
  69. /* clk enable */
  70. sdhci->clk = devm_clk_get(&pdev->dev, NULL);
  71. if (IS_ERR(sdhci->clk)) {
  72. ret = PTR_ERR(sdhci->clk);
  73. dev_dbg(&pdev->dev, "Error getting clock\n");
  74. goto err_host;
  75. }
  76. ret = clk_prepare_enable(sdhci->clk);
  77. if (ret) {
  78. dev_dbg(&pdev->dev, "Error enabling clock\n");
  79. goto err_host;
  80. }
  81. ret = clk_set_rate(sdhci->clk, 50000000);
  82. if (ret)
  83. dev_dbg(&pdev->dev, "Error setting desired clk, clk=%lu\n",
  84. clk_get_rate(sdhci->clk));
  85. /*
  86. * It is optional to use GPIOs for sdhci card detection. If we
  87. * find a descriptor using slot GPIO, we use it.
  88. */
  89. ret = mmc_gpiod_request_cd(host->mmc, "cd", 0, false, 0, NULL);
  90. if (ret == -EPROBE_DEFER)
  91. goto disable_clk;
  92. ret = sdhci_add_host(host);
  93. if (ret)
  94. goto disable_clk;
  95. platform_set_drvdata(pdev, host);
  96. return 0;
  97. disable_clk:
  98. clk_disable_unprepare(sdhci->clk);
  99. err_host:
  100. sdhci_free_host(host);
  101. err:
  102. dev_err(&pdev->dev, "spear-sdhci probe failed: %d\n", ret);
  103. return ret;
  104. }
  105. static int sdhci_remove(struct platform_device *pdev)
  106. {
  107. struct sdhci_host *host = platform_get_drvdata(pdev);
  108. struct spear_sdhci *sdhci = sdhci_priv(host);
  109. int dead = 0;
  110. u32 scratch;
  111. scratch = readl(host->ioaddr + SDHCI_INT_STATUS);
  112. if (scratch == (u32)-1)
  113. dead = 1;
  114. sdhci_remove_host(host, dead);
  115. clk_disable_unprepare(sdhci->clk);
  116. sdhci_free_host(host);
  117. return 0;
  118. }
  119. #ifdef CONFIG_PM_SLEEP
  120. static int sdhci_suspend(struct device *dev)
  121. {
  122. struct sdhci_host *host = dev_get_drvdata(dev);
  123. struct spear_sdhci *sdhci = sdhci_priv(host);
  124. int ret;
  125. if (host->tuning_mode != SDHCI_TUNING_MODE_3)
  126. mmc_retune_needed(host->mmc);
  127. ret = sdhci_suspend_host(host);
  128. if (!ret)
  129. clk_disable(sdhci->clk);
  130. return ret;
  131. }
  132. static int sdhci_resume(struct device *dev)
  133. {
  134. struct sdhci_host *host = dev_get_drvdata(dev);
  135. struct spear_sdhci *sdhci = sdhci_priv(host);
  136. int ret;
  137. ret = clk_enable(sdhci->clk);
  138. if (ret) {
  139. dev_dbg(dev, "Resume: Error enabling clock\n");
  140. return ret;
  141. }
  142. return sdhci_resume_host(host);
  143. }
  144. #endif
  145. static SIMPLE_DEV_PM_OPS(sdhci_pm_ops, sdhci_suspend, sdhci_resume);
  146. #ifdef CONFIG_OF
  147. static const struct of_device_id sdhci_spear_id_table[] = {
  148. { .compatible = "st,spear300-sdhci" },
  149. {}
  150. };
  151. MODULE_DEVICE_TABLE(of, sdhci_spear_id_table);
  152. #endif
  153. static struct platform_driver sdhci_driver = {
  154. .driver = {
  155. .name = "sdhci",
  156. .pm = &sdhci_pm_ops,
  157. .of_match_table = of_match_ptr(sdhci_spear_id_table),
  158. },
  159. .probe = sdhci_probe,
  160. .remove = sdhci_remove,
  161. };
  162. module_platform_driver(sdhci_driver);
  163. MODULE_DESCRIPTION("SPEAr Secure Digital Host Controller Interface driver");
  164. MODULE_AUTHOR("Viresh Kumar <vireshk@kernel.org>");
  165. MODULE_LICENSE("GPL v2");