sdhci-spear.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  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/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 (!request_mem_region(iomem->start, resource_size(iomem),
  74. "spear-sdhci")) {
  75. ret = -EBUSY;
  76. dev_dbg(&pdev->dev, "cannot request region\n");
  77. goto err;
  78. }
  79. sdhci = kzalloc(sizeof(*sdhci), GFP_KERNEL);
  80. if (!sdhci) {
  81. ret = -ENOMEM;
  82. dev_dbg(&pdev->dev, "cannot allocate memory for sdhci\n");
  83. goto err_kzalloc;
  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_clk_get;
  91. }
  92. ret = clk_enable(sdhci->clk);
  93. if (ret) {
  94. dev_dbg(&pdev->dev, "Error enabling clock\n");
  95. goto err_clk_enb;
  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 err_alloc_host;
  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 = ioremap(iomem->start, resource_size(iomem));
  114. if (!host->ioaddr) {
  115. ret = -ENOMEM;
  116. dev_dbg(&pdev->dev, "failed to remap registers\n");
  117. goto err_ioremap;
  118. }
  119. ret = sdhci_add_host(host);
  120. if (ret) {
  121. dev_dbg(&pdev->dev, "error adding host\n");
  122. goto err_add_host;
  123. }
  124. platform_set_drvdata(pdev, host);
  125. /*
  126. * It is optional to use GPIOs for sdhci Power control & sdhci card
  127. * interrupt detection. If sdhci->data is NULL, then use original sdhci
  128. * lines otherwise GPIO lines.
  129. * If GPIO is selected for power control, then power should be disabled
  130. * after card removal and should be enabled when card insertion
  131. * interrupt occurs
  132. */
  133. if (!sdhci->data)
  134. return 0;
  135. if (sdhci->data->card_power_gpio >= 0) {
  136. int val = 0;
  137. ret = gpio_request(sdhci->data->card_power_gpio, "sdhci");
  138. if (ret < 0) {
  139. dev_dbg(&pdev->dev, "gpio request fail: %d\n",
  140. sdhci->data->card_power_gpio);
  141. goto err_pgpio_request;
  142. }
  143. if (sdhci->data->power_always_enb)
  144. val = sdhci->data->power_active_high;
  145. else
  146. val = !sdhci->data->power_active_high;
  147. ret = gpio_direction_output(sdhci->data->card_power_gpio, val);
  148. if (ret) {
  149. dev_dbg(&pdev->dev, "gpio set direction fail: %d\n",
  150. sdhci->data->card_power_gpio);
  151. goto err_pgpio_direction;
  152. }
  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. #ifdef CONFIG_PM
  235. static int sdhci_suspend(struct device *dev)
  236. {
  237. struct sdhci_host *host = dev_get_drvdata(dev);
  238. struct spear_sdhci *sdhci = dev_get_platdata(dev);
  239. int ret;
  240. ret = sdhci_suspend_host(host);
  241. if (!ret)
  242. clk_disable(sdhci->clk);
  243. return ret;
  244. }
  245. static int sdhci_resume(struct device *dev)
  246. {
  247. struct sdhci_host *host = dev_get_drvdata(dev);
  248. struct spear_sdhci *sdhci = dev_get_platdata(dev);
  249. int ret;
  250. ret = clk_enable(sdhci->clk);
  251. if (ret) {
  252. dev_dbg(dev, "Resume: Error enabling clock\n");
  253. return ret;
  254. }
  255. return sdhci_resume_host(host);
  256. }
  257. #endif
  258. static SIMPLE_DEV_PM_OPS(sdhci_pm_ops, sdhci_suspend, sdhci_resume);
  259. static struct platform_driver sdhci_driver = {
  260. .driver = {
  261. .name = "sdhci",
  262. .owner = THIS_MODULE,
  263. .pm = &sdhci_pm_ops,
  264. },
  265. .probe = sdhci_probe,
  266. .remove = __devexit_p(sdhci_remove),
  267. };
  268. module_platform_driver(sdhci_driver);
  269. MODULE_DESCRIPTION("SPEAr Secure Digital Host Controller Interface driver");
  270. MODULE_AUTHOR("Viresh Kumar <viresh.kumar@st.com>");
  271. MODULE_LICENSE("GPL v2");