sdhci-spear.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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_host;
  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 = -EINVAL;
  63. goto err_host;
  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_host;
  73. }
  74. ret = clk_prepare_enable(sdhci->clk);
  75. if (ret) {
  76. dev_dbg(&pdev->dev, "Error enabling clock\n");
  77. goto err_host;
  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_host:
  98. sdhci_free_host(host);
  99. err:
  100. dev_err(&pdev->dev, "spear-sdhci probe failed: %d\n", ret);
  101. return ret;
  102. }
  103. static int sdhci_remove(struct platform_device *pdev)
  104. {
  105. struct sdhci_host *host = platform_get_drvdata(pdev);
  106. struct spear_sdhci *sdhci = sdhci_priv(host);
  107. int dead = 0;
  108. u32 scratch;
  109. scratch = readl(host->ioaddr + SDHCI_INT_STATUS);
  110. if (scratch == (u32)-1)
  111. dead = 1;
  112. sdhci_remove_host(host, dead);
  113. clk_disable_unprepare(sdhci->clk);
  114. sdhci_free_host(host);
  115. return 0;
  116. }
  117. #ifdef CONFIG_PM_SLEEP
  118. static int sdhci_suspend(struct device *dev)
  119. {
  120. struct sdhci_host *host = dev_get_drvdata(dev);
  121. struct spear_sdhci *sdhci = sdhci_priv(host);
  122. int ret;
  123. if (host->tuning_mode != SDHCI_TUNING_MODE_3)
  124. mmc_retune_needed(host->mmc);
  125. ret = sdhci_suspend_host(host);
  126. if (!ret)
  127. clk_disable(sdhci->clk);
  128. return ret;
  129. }
  130. static int sdhci_resume(struct device *dev)
  131. {
  132. struct sdhci_host *host = dev_get_drvdata(dev);
  133. struct spear_sdhci *sdhci = sdhci_priv(host);
  134. int ret;
  135. ret = clk_enable(sdhci->clk);
  136. if (ret) {
  137. dev_dbg(dev, "Resume: Error enabling clock\n");
  138. return ret;
  139. }
  140. return sdhci_resume_host(host);
  141. }
  142. #endif
  143. static SIMPLE_DEV_PM_OPS(sdhci_pm_ops, sdhci_suspend, sdhci_resume);
  144. #ifdef CONFIG_OF
  145. static const struct of_device_id sdhci_spear_id_table[] = {
  146. { .compatible = "st,spear300-sdhci" },
  147. {}
  148. };
  149. MODULE_DEVICE_TABLE(of, sdhci_spear_id_table);
  150. #endif
  151. static struct platform_driver sdhci_driver = {
  152. .driver = {
  153. .name = "sdhci",
  154. .probe_type = PROBE_PREFER_ASYNCHRONOUS,
  155. .pm = &sdhci_pm_ops,
  156. .of_match_table = of_match_ptr(sdhci_spear_id_table),
  157. },
  158. .probe = sdhci_probe,
  159. .remove = sdhci_remove,
  160. };
  161. module_platform_driver(sdhci_driver);
  162. MODULE_DESCRIPTION("SPEAr Secure Digital Host Controller Interface driver");
  163. MODULE_AUTHOR("Viresh Kumar <vireshk@kernel.org>");
  164. MODULE_LICENSE("GPL v2");