sdhci-spear.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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/gpio.h>
  18. #include <linux/highmem.h>
  19. #include <linux/module.h>
  20. #include <linux/interrupt.h>
  21. #include <linux/irq.h>
  22. #include <linux/of.h>
  23. #include <linux/of_gpio.h>
  24. #include <linux/platform_device.h>
  25. #include <linux/pm.h>
  26. #include <linux/slab.h>
  27. #include <linux/mmc/host.h>
  28. #include <linux/mmc/slot-gpio.h>
  29. #include <linux/io.h>
  30. #include "sdhci.h"
  31. struct spear_sdhci {
  32. struct clk *clk;
  33. int card_int_gpio;
  34. };
  35. /* sdhci ops */
  36. static const struct sdhci_ops sdhci_pltfm_ops = {
  37. .set_clock = sdhci_set_clock,
  38. .set_bus_width = sdhci_set_bus_width,
  39. .reset = sdhci_reset,
  40. .set_uhs_signaling = sdhci_set_uhs_signaling,
  41. };
  42. static void sdhci_probe_config_dt(struct device_node *np,
  43. struct spear_sdhci *host)
  44. {
  45. int cd_gpio;
  46. cd_gpio = of_get_named_gpio(np, "cd-gpios", 0);
  47. if (!gpio_is_valid(cd_gpio))
  48. cd_gpio = -1;
  49. host->card_int_gpio = cd_gpio;
  50. }
  51. static int sdhci_probe(struct platform_device *pdev)
  52. {
  53. struct sdhci_host *host;
  54. struct resource *iomem;
  55. struct spear_sdhci *sdhci;
  56. struct device *dev;
  57. int ret;
  58. dev = pdev->dev.parent ? pdev->dev.parent : &pdev->dev;
  59. host = sdhci_alloc_host(dev, sizeof(*sdhci));
  60. if (IS_ERR(host)) {
  61. ret = PTR_ERR(host);
  62. dev_dbg(&pdev->dev, "cannot allocate memory for sdhci\n");
  63. goto err;
  64. }
  65. iomem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  66. host->ioaddr = devm_ioremap_resource(&pdev->dev, iomem);
  67. if (IS_ERR(host->ioaddr)) {
  68. ret = PTR_ERR(host->ioaddr);
  69. dev_dbg(&pdev->dev, "unable to map iomem: %d\n", ret);
  70. goto err_host;
  71. }
  72. host->hw_name = "sdhci";
  73. host->ops = &sdhci_pltfm_ops;
  74. host->irq = platform_get_irq(pdev, 0);
  75. host->quirks = SDHCI_QUIRK_BROKEN_ADMA;
  76. sdhci = sdhci_priv(host);
  77. /* clk enable */
  78. sdhci->clk = devm_clk_get(&pdev->dev, NULL);
  79. if (IS_ERR(sdhci->clk)) {
  80. ret = PTR_ERR(sdhci->clk);
  81. dev_dbg(&pdev->dev, "Error getting clock\n");
  82. goto err_host;
  83. }
  84. ret = clk_prepare_enable(sdhci->clk);
  85. if (ret) {
  86. dev_dbg(&pdev->dev, "Error enabling clock\n");
  87. goto err_host;
  88. }
  89. ret = clk_set_rate(sdhci->clk, 50000000);
  90. if (ret)
  91. dev_dbg(&pdev->dev, "Error setting desired clk, clk=%lu\n",
  92. clk_get_rate(sdhci->clk));
  93. sdhci_probe_config_dt(pdev->dev.of_node, sdhci);
  94. /*
  95. * It is optional to use GPIOs for sdhci card detection. If
  96. * sdhci->card_int_gpio < 0, then use original sdhci lines otherwise
  97. * GPIO lines. We use the built-in GPIO support for this.
  98. */
  99. if (sdhci->card_int_gpio >= 0) {
  100. ret = mmc_gpio_request_cd(host->mmc, sdhci->card_int_gpio, 0);
  101. if (ret < 0) {
  102. dev_dbg(&pdev->dev,
  103. "failed to request card-detect gpio%d\n",
  104. sdhci->card_int_gpio);
  105. goto disable_clk;
  106. }
  107. }
  108. ret = sdhci_add_host(host);
  109. if (ret) {
  110. dev_dbg(&pdev->dev, "error adding host\n");
  111. goto disable_clk;
  112. }
  113. platform_set_drvdata(pdev, host);
  114. return 0;
  115. disable_clk:
  116. clk_disable_unprepare(sdhci->clk);
  117. err_host:
  118. sdhci_free_host(host);
  119. err:
  120. dev_err(&pdev->dev, "spear-sdhci probe failed: %d\n", ret);
  121. return ret;
  122. }
  123. static int sdhci_remove(struct platform_device *pdev)
  124. {
  125. struct sdhci_host *host = platform_get_drvdata(pdev);
  126. struct spear_sdhci *sdhci = sdhci_priv(host);
  127. int dead = 0;
  128. u32 scratch;
  129. scratch = readl(host->ioaddr + SDHCI_INT_STATUS);
  130. if (scratch == (u32)-1)
  131. dead = 1;
  132. sdhci_remove_host(host, dead);
  133. clk_disable_unprepare(sdhci->clk);
  134. sdhci_free_host(host);
  135. return 0;
  136. }
  137. #ifdef CONFIG_PM_SLEEP
  138. static int sdhci_suspend(struct device *dev)
  139. {
  140. struct sdhci_host *host = dev_get_drvdata(dev);
  141. struct spear_sdhci *sdhci = sdhci_priv(host);
  142. int ret;
  143. ret = sdhci_suspend_host(host);
  144. if (!ret)
  145. clk_disable(sdhci->clk);
  146. return ret;
  147. }
  148. static int sdhci_resume(struct device *dev)
  149. {
  150. struct sdhci_host *host = dev_get_drvdata(dev);
  151. struct spear_sdhci *sdhci = sdhci_priv(host);
  152. int ret;
  153. ret = clk_enable(sdhci->clk);
  154. if (ret) {
  155. dev_dbg(dev, "Resume: Error enabling clock\n");
  156. return ret;
  157. }
  158. return sdhci_resume_host(host);
  159. }
  160. #endif
  161. static SIMPLE_DEV_PM_OPS(sdhci_pm_ops, sdhci_suspend, sdhci_resume);
  162. #ifdef CONFIG_OF
  163. static const struct of_device_id sdhci_spear_id_table[] = {
  164. { .compatible = "st,spear300-sdhci" },
  165. {}
  166. };
  167. MODULE_DEVICE_TABLE(of, sdhci_spear_id_table);
  168. #endif
  169. static struct platform_driver sdhci_driver = {
  170. .driver = {
  171. .name = "sdhci",
  172. .pm = &sdhci_pm_ops,
  173. .of_match_table = of_match_ptr(sdhci_spear_id_table),
  174. },
  175. .probe = sdhci_probe,
  176. .remove = sdhci_remove,
  177. };
  178. module_platform_driver(sdhci_driver);
  179. MODULE_DESCRIPTION("SPEAr Secure Digital Host Controller Interface driver");
  180. MODULE_AUTHOR("Viresh Kumar <vireshk@kernel.org>");
  181. MODULE_LICENSE("GPL v2");