sdhci-spear.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  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 <viresh.linux@gmail.com>
  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/platform_device.h>
  23. #include <linux/pm.h>
  24. #include <linux/slab.h>
  25. #include <linux/mmc/host.h>
  26. #include <linux/mmc/sdhci-spear.h>
  27. #include <linux/io.h>
  28. #include "sdhci.h"
  29. struct spear_sdhci {
  30. struct clk *clk;
  31. struct sdhci_plat_data *data;
  32. };
  33. /* sdhci ops */
  34. static struct sdhci_ops sdhci_pltfm_ops = {
  35. /* Nothing to do for now. */
  36. };
  37. /* gpio card detection interrupt handler */
  38. static irqreturn_t sdhci_gpio_irq(int irq, void *dev_id)
  39. {
  40. struct platform_device *pdev = dev_id;
  41. struct sdhci_host *host = platform_get_drvdata(pdev);
  42. struct spear_sdhci *sdhci = dev_get_platdata(&pdev->dev);
  43. unsigned long gpio_irq_type;
  44. int val;
  45. val = gpio_get_value(sdhci->data->card_int_gpio);
  46. /* val == 1 -> card removed, val == 0 -> card inserted */
  47. /* if card removed - set irq for low level, else vice versa */
  48. gpio_irq_type = val ? IRQF_TRIGGER_LOW : IRQF_TRIGGER_HIGH;
  49. irq_set_irq_type(irq, gpio_irq_type);
  50. if (sdhci->data->card_power_gpio >= 0) {
  51. if (!sdhci->data->power_always_enb) {
  52. /* if card inserted, give power, otherwise remove it */
  53. val = sdhci->data->power_active_high ? !val : val ;
  54. gpio_set_value(sdhci->data->card_power_gpio, val);
  55. }
  56. }
  57. /* inform sdhci driver about card insertion/removal */
  58. tasklet_schedule(&host->card_tasklet);
  59. return IRQ_HANDLED;
  60. }
  61. static int __devinit sdhci_probe(struct platform_device *pdev)
  62. {
  63. struct sdhci_host *host;
  64. struct resource *iomem;
  65. struct spear_sdhci *sdhci;
  66. int ret;
  67. iomem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  68. if (!iomem) {
  69. ret = -ENOMEM;
  70. dev_dbg(&pdev->dev, "memory resource not defined\n");
  71. goto err;
  72. }
  73. if (!devm_request_mem_region(&pdev->dev, iomem->start,
  74. resource_size(iomem), "spear-sdhci")) {
  75. ret = -EBUSY;
  76. dev_dbg(&pdev->dev, "cannot request region\n");
  77. goto err;
  78. }
  79. sdhci = devm_kzalloc(&pdev->dev, sizeof(*sdhci), GFP_KERNEL);
  80. if (!sdhci) {
  81. ret = -ENOMEM;
  82. dev_dbg(&pdev->dev, "cannot allocate memory for sdhci\n");
  83. goto err;
  84. }
  85. /* clk enable */
  86. sdhci->clk = clk_get(&pdev->dev, NULL);
  87. if (IS_ERR(sdhci->clk)) {
  88. ret = PTR_ERR(sdhci->clk);
  89. dev_dbg(&pdev->dev, "Error getting clock\n");
  90. goto err;
  91. }
  92. ret = clk_enable(sdhci->clk);
  93. if (ret) {
  94. dev_dbg(&pdev->dev, "Error enabling clock\n");
  95. goto put_clk;
  96. }
  97. /* overwrite platform_data */
  98. sdhci->data = dev_get_platdata(&pdev->dev);
  99. pdev->dev.platform_data = sdhci;
  100. if (pdev->dev.parent)
  101. host = sdhci_alloc_host(pdev->dev.parent, 0);
  102. else
  103. host = sdhci_alloc_host(&pdev->dev, 0);
  104. if (IS_ERR(host)) {
  105. ret = PTR_ERR(host);
  106. dev_dbg(&pdev->dev, "error allocating host\n");
  107. goto disable_clk;
  108. }
  109. host->hw_name = "sdhci";
  110. host->ops = &sdhci_pltfm_ops;
  111. host->irq = platform_get_irq(pdev, 0);
  112. host->quirks = SDHCI_QUIRK_BROKEN_ADMA;
  113. host->ioaddr = devm_ioremap(&pdev->dev, iomem->start,
  114. resource_size(iomem));
  115. if (!host->ioaddr) {
  116. ret = -ENOMEM;
  117. dev_dbg(&pdev->dev, "failed to remap registers\n");
  118. goto free_host;
  119. }
  120. ret = sdhci_add_host(host);
  121. if (ret) {
  122. dev_dbg(&pdev->dev, "error adding host\n");
  123. goto free_host;
  124. }
  125. platform_set_drvdata(pdev, host);
  126. /*
  127. * It is optional to use GPIOs for sdhci Power control & sdhci card
  128. * interrupt detection. If sdhci->data is NULL, then use original sdhci
  129. * lines otherwise GPIO lines.
  130. * If GPIO is selected for power control, then power should be disabled
  131. * after card removal and should be enabled when card insertion
  132. * interrupt occurs
  133. */
  134. if (!sdhci->data)
  135. return 0;
  136. if (sdhci->data->card_power_gpio >= 0) {
  137. int val = 0;
  138. ret = devm_gpio_request(&pdev->dev,
  139. sdhci->data->card_power_gpio, "sdhci");
  140. if (ret < 0) {
  141. dev_dbg(&pdev->dev, "gpio request fail: %d\n",
  142. sdhci->data->card_power_gpio);
  143. goto set_drvdata;
  144. }
  145. if (sdhci->data->power_always_enb)
  146. val = sdhci->data->power_active_high;
  147. else
  148. val = !sdhci->data->power_active_high;
  149. ret = gpio_direction_output(sdhci->data->card_power_gpio, val);
  150. if (ret) {
  151. dev_dbg(&pdev->dev, "gpio set direction fail: %d\n",
  152. sdhci->data->card_power_gpio);
  153. goto set_drvdata;
  154. }
  155. }
  156. if (sdhci->data->card_int_gpio >= 0) {
  157. ret = devm_gpio_request(&pdev->dev, sdhci->data->card_int_gpio,
  158. "sdhci");
  159. if (ret < 0) {
  160. dev_dbg(&pdev->dev, "gpio request fail: %d\n",
  161. sdhci->data->card_int_gpio);
  162. goto set_drvdata;
  163. }
  164. ret = gpio_direction_input(sdhci->data->card_int_gpio);
  165. if (ret) {
  166. dev_dbg(&pdev->dev, "gpio set direction fail: %d\n",
  167. sdhci->data->card_int_gpio);
  168. goto set_drvdata;
  169. }
  170. ret = devm_request_irq(&pdev->dev,
  171. gpio_to_irq(sdhci->data->card_int_gpio),
  172. sdhci_gpio_irq, IRQF_TRIGGER_LOW,
  173. mmc_hostname(host->mmc), pdev);
  174. if (ret) {
  175. dev_dbg(&pdev->dev, "gpio request irq fail: %d\n",
  176. sdhci->data->card_int_gpio);
  177. goto set_drvdata;
  178. }
  179. }
  180. return 0;
  181. set_drvdata:
  182. platform_set_drvdata(pdev, NULL);
  183. sdhci_remove_host(host, 1);
  184. free_host:
  185. sdhci_free_host(host);
  186. disable_clk:
  187. clk_disable(sdhci->clk);
  188. put_clk:
  189. clk_put(sdhci->clk);
  190. err:
  191. dev_err(&pdev->dev, "spear-sdhci probe failed: %d\n", ret);
  192. return ret;
  193. }
  194. static int __devexit sdhci_remove(struct platform_device *pdev)
  195. {
  196. struct sdhci_host *host = platform_get_drvdata(pdev);
  197. struct spear_sdhci *sdhci = dev_get_platdata(&pdev->dev);
  198. int dead = 0;
  199. u32 scratch;
  200. platform_set_drvdata(pdev, NULL);
  201. scratch = readl(host->ioaddr + SDHCI_INT_STATUS);
  202. if (scratch == (u32)-1)
  203. dead = 1;
  204. sdhci_remove_host(host, dead);
  205. sdhci_free_host(host);
  206. clk_disable(sdhci->clk);
  207. clk_put(sdhci->clk);
  208. return 0;
  209. }
  210. #ifdef CONFIG_PM
  211. static int sdhci_suspend(struct device *dev)
  212. {
  213. struct sdhci_host *host = dev_get_drvdata(dev);
  214. struct spear_sdhci *sdhci = dev_get_platdata(dev);
  215. int ret;
  216. ret = sdhci_suspend_host(host);
  217. if (!ret)
  218. clk_disable(sdhci->clk);
  219. return ret;
  220. }
  221. static int sdhci_resume(struct device *dev)
  222. {
  223. struct sdhci_host *host = dev_get_drvdata(dev);
  224. struct spear_sdhci *sdhci = dev_get_platdata(dev);
  225. int ret;
  226. ret = clk_enable(sdhci->clk);
  227. if (ret) {
  228. dev_dbg(dev, "Resume: Error enabling clock\n");
  229. return ret;
  230. }
  231. return sdhci_resume_host(host);
  232. }
  233. #endif
  234. static SIMPLE_DEV_PM_OPS(sdhci_pm_ops, sdhci_suspend, sdhci_resume);
  235. static struct platform_driver sdhci_driver = {
  236. .driver = {
  237. .name = "sdhci",
  238. .owner = THIS_MODULE,
  239. .pm = &sdhci_pm_ops,
  240. },
  241. .probe = sdhci_probe,
  242. .remove = __devexit_p(sdhci_remove),
  243. };
  244. module_platform_driver(sdhci_driver);
  245. MODULE_DESCRIPTION("SPEAr Secure Digital Host Controller Interface driver");
  246. MODULE_AUTHOR("Viresh Kumar <viresh.linux@gmail.com>");
  247. MODULE_LICENSE("GPL v2");