sdhci-spear.c 7.3 KB

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