sdhci-spear.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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. if (host->irq <= 0) {
  76. ret = -EINVAL;
  77. goto err_host;
  78. }
  79. host->quirks = SDHCI_QUIRK_BROKEN_ADMA;
  80. sdhci = sdhci_priv(host);
  81. /* clk enable */
  82. sdhci->clk = devm_clk_get(&pdev->dev, NULL);
  83. if (IS_ERR(sdhci->clk)) {
  84. ret = PTR_ERR(sdhci->clk);
  85. dev_dbg(&pdev->dev, "Error getting clock\n");
  86. goto err_host;
  87. }
  88. ret = clk_prepare_enable(sdhci->clk);
  89. if (ret) {
  90. dev_dbg(&pdev->dev, "Error enabling clock\n");
  91. goto err_host;
  92. }
  93. ret = clk_set_rate(sdhci->clk, 50000000);
  94. if (ret)
  95. dev_dbg(&pdev->dev, "Error setting desired clk, clk=%lu\n",
  96. clk_get_rate(sdhci->clk));
  97. sdhci_probe_config_dt(pdev->dev.of_node, sdhci);
  98. /*
  99. * It is optional to use GPIOs for sdhci card detection. If
  100. * sdhci->card_int_gpio < 0, then use original sdhci lines otherwise
  101. * GPIO lines. We use the built-in GPIO support for this.
  102. */
  103. if (sdhci->card_int_gpio >= 0) {
  104. ret = mmc_gpio_request_cd(host->mmc, sdhci->card_int_gpio, 0);
  105. if (ret < 0) {
  106. dev_dbg(&pdev->dev,
  107. "failed to request card-detect gpio%d\n",
  108. sdhci->card_int_gpio);
  109. goto disable_clk;
  110. }
  111. }
  112. ret = sdhci_add_host(host);
  113. if (ret)
  114. goto disable_clk;
  115. platform_set_drvdata(pdev, host);
  116. return 0;
  117. disable_clk:
  118. clk_disable_unprepare(sdhci->clk);
  119. err_host:
  120. sdhci_free_host(host);
  121. err:
  122. dev_err(&pdev->dev, "spear-sdhci probe failed: %d\n", ret);
  123. return ret;
  124. }
  125. static int sdhci_remove(struct platform_device *pdev)
  126. {
  127. struct sdhci_host *host = platform_get_drvdata(pdev);
  128. struct spear_sdhci *sdhci = sdhci_priv(host);
  129. int dead = 0;
  130. u32 scratch;
  131. scratch = readl(host->ioaddr + SDHCI_INT_STATUS);
  132. if (scratch == (u32)-1)
  133. dead = 1;
  134. sdhci_remove_host(host, dead);
  135. clk_disable_unprepare(sdhci->clk);
  136. sdhci_free_host(host);
  137. return 0;
  138. }
  139. #ifdef CONFIG_PM_SLEEP
  140. static int sdhci_suspend(struct device *dev)
  141. {
  142. struct sdhci_host *host = dev_get_drvdata(dev);
  143. struct spear_sdhci *sdhci = sdhci_priv(host);
  144. int ret;
  145. if (host->tuning_mode != SDHCI_TUNING_MODE_3)
  146. mmc_retune_needed(host->mmc);
  147. ret = sdhci_suspend_host(host);
  148. if (!ret)
  149. clk_disable(sdhci->clk);
  150. return ret;
  151. }
  152. static int sdhci_resume(struct device *dev)
  153. {
  154. struct sdhci_host *host = dev_get_drvdata(dev);
  155. struct spear_sdhci *sdhci = sdhci_priv(host);
  156. int ret;
  157. ret = clk_enable(sdhci->clk);
  158. if (ret) {
  159. dev_dbg(dev, "Resume: Error enabling clock\n");
  160. return ret;
  161. }
  162. return sdhci_resume_host(host);
  163. }
  164. #endif
  165. static SIMPLE_DEV_PM_OPS(sdhci_pm_ops, sdhci_suspend, sdhci_resume);
  166. #ifdef CONFIG_OF
  167. static const struct of_device_id sdhci_spear_id_table[] = {
  168. { .compatible = "st,spear300-sdhci" },
  169. {}
  170. };
  171. MODULE_DEVICE_TABLE(of, sdhci_spear_id_table);
  172. #endif
  173. static struct platform_driver sdhci_driver = {
  174. .driver = {
  175. .name = "sdhci",
  176. .pm = &sdhci_pm_ops,
  177. .of_match_table = of_match_ptr(sdhci_spear_id_table),
  178. },
  179. .probe = sdhci_probe,
  180. .remove = sdhci_remove,
  181. };
  182. module_platform_driver(sdhci_driver);
  183. MODULE_DESCRIPTION("SPEAr Secure Digital Host Controller Interface driver");
  184. MODULE_AUTHOR("Viresh Kumar <vireshk@kernel.org>");
  185. MODULE_LICENSE("GPL v2");