sdhci-spear.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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 spear_sdhci *sdhci;
  43. struct device *dev;
  44. int ret;
  45. dev = pdev->dev.parent ? pdev->dev.parent : &pdev->dev;
  46. host = sdhci_alloc_host(dev, sizeof(*sdhci));
  47. if (IS_ERR(host)) {
  48. ret = PTR_ERR(host);
  49. dev_dbg(&pdev->dev, "cannot allocate memory for sdhci\n");
  50. goto err;
  51. }
  52. host->ioaddr = devm_platform_ioremap_resource(pdev, 0);
  53. if (IS_ERR(host->ioaddr)) {
  54. ret = PTR_ERR(host->ioaddr);
  55. dev_dbg(&pdev->dev, "unable to map iomem: %d\n", ret);
  56. goto err;
  57. }
  58. host->hw_name = "sdhci";
  59. host->ops = &sdhci_pltfm_ops;
  60. host->irq = platform_get_irq(pdev, 0);
  61. if (host->irq < 0) {
  62. ret = host->irq;
  63. goto err;
  64. }
  65. host->quirks = SDHCI_QUIRK_BROKEN_ADMA;
  66. sdhci = sdhci_priv(host);
  67. /* clk enable */
  68. sdhci->clk = devm_clk_get(&pdev->dev, NULL);
  69. if (IS_ERR(sdhci->clk)) {
  70. ret = PTR_ERR(sdhci->clk);
  71. dev_dbg(&pdev->dev, "Error getting clock\n");
  72. goto err;
  73. }
  74. ret = clk_prepare_enable(sdhci->clk);
  75. if (ret) {
  76. dev_dbg(&pdev->dev, "Error enabling clock\n");
  77. goto err;
  78. }
  79. ret = clk_set_rate(sdhci->clk, 50000000);
  80. if (ret)
  81. dev_dbg(&pdev->dev, "Error setting desired clk, clk=%lu\n",
  82. clk_get_rate(sdhci->clk));
  83. /*
  84. * It is optional to use GPIOs for sdhci card detection. If we
  85. * find a descriptor using slot GPIO, we use it.
  86. */
  87. ret = mmc_gpiod_request_cd(host->mmc, "cd", 0, false, 0);
  88. if (ret == -EPROBE_DEFER)
  89. goto disable_clk;
  90. ret = sdhci_add_host(host);
  91. if (ret)
  92. goto disable_clk;
  93. platform_set_drvdata(pdev, host);
  94. return 0;
  95. disable_clk:
  96. clk_disable_unprepare(sdhci->clk);
  97. err:
  98. dev_err(&pdev->dev, "spear-sdhci probe failed: %d\n", ret);
  99. return ret;
  100. }
  101. static void sdhci_remove(struct platform_device *pdev)
  102. {
  103. struct sdhci_host *host = platform_get_drvdata(pdev);
  104. struct spear_sdhci *sdhci = sdhci_priv(host);
  105. int dead = 0;
  106. u32 scratch;
  107. scratch = readl(host->ioaddr + SDHCI_INT_STATUS);
  108. if (scratch == (u32)-1)
  109. dead = 1;
  110. sdhci_remove_host(host, dead);
  111. clk_disable_unprepare(sdhci->clk);
  112. }
  113. static int sdhci_suspend(struct device *dev)
  114. {
  115. struct sdhci_host *host = dev_get_drvdata(dev);
  116. struct spear_sdhci *sdhci = sdhci_priv(host);
  117. int ret;
  118. if (host->tuning_mode != SDHCI_TUNING_MODE_3)
  119. mmc_retune_needed(host->mmc);
  120. ret = sdhci_suspend_host(host);
  121. if (!ret)
  122. clk_disable(sdhci->clk);
  123. return ret;
  124. }
  125. static int sdhci_resume(struct device *dev)
  126. {
  127. struct sdhci_host *host = dev_get_drvdata(dev);
  128. struct spear_sdhci *sdhci = sdhci_priv(host);
  129. int ret;
  130. ret = clk_enable(sdhci->clk);
  131. if (ret) {
  132. dev_dbg(dev, "Resume: Error enabling clock\n");
  133. return ret;
  134. }
  135. return sdhci_resume_host(host);
  136. }
  137. static DEFINE_SIMPLE_DEV_PM_OPS(sdhci_pm_ops, sdhci_suspend, sdhci_resume);
  138. static const struct of_device_id sdhci_spear_id_table[] = {
  139. { .compatible = "st,spear300-sdhci" },
  140. {}
  141. };
  142. MODULE_DEVICE_TABLE(of, sdhci_spear_id_table);
  143. static struct platform_driver sdhci_driver = {
  144. .driver = {
  145. .name = "sdhci",
  146. .probe_type = PROBE_PREFER_ASYNCHRONOUS,
  147. .pm = pm_sleep_ptr(&sdhci_pm_ops),
  148. .of_match_table = sdhci_spear_id_table,
  149. },
  150. .probe = sdhci_probe,
  151. .remove = sdhci_remove,
  152. };
  153. module_platform_driver(sdhci_driver);
  154. MODULE_DESCRIPTION("SPEAr Secure Digital Host Controller Interface driver");
  155. MODULE_AUTHOR("Viresh Kumar <vireshk@kernel.org>");
  156. MODULE_LICENSE("GPL v2");