sdhci-spear.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  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. BUG_ON(pdev == NULL);
  68. iomem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  69. if (!iomem) {
  70. ret = -ENOMEM;
  71. dev_dbg(&pdev->dev, "memory resource not defined\n");
  72. goto err;
  73. }
  74. if (!request_mem_region(iomem->start, resource_size(iomem),
  75. "spear-sdhci")) {
  76. ret = -EBUSY;
  77. dev_dbg(&pdev->dev, "cannot request region\n");
  78. goto err;
  79. }
  80. sdhci = kzalloc(sizeof(*sdhci), GFP_KERNEL);
  81. if (!sdhci) {
  82. ret = -ENOMEM;
  83. dev_dbg(&pdev->dev, "cannot allocate memory for sdhci\n");
  84. goto err_kzalloc;
  85. }
  86. /* clk enable */
  87. sdhci->clk = clk_get(&pdev->dev, NULL);
  88. if (IS_ERR(sdhci->clk)) {
  89. ret = PTR_ERR(sdhci->clk);
  90. dev_dbg(&pdev->dev, "Error getting clock\n");
  91. goto err_clk_get;
  92. }
  93. ret = clk_enable(sdhci->clk);
  94. if (ret) {
  95. dev_dbg(&pdev->dev, "Error enabling clock\n");
  96. goto err_clk_enb;
  97. }
  98. /* overwrite platform_data */
  99. sdhci->data = dev_get_platdata(&pdev->dev);
  100. pdev->dev.platform_data = sdhci;
  101. if (pdev->dev.parent)
  102. host = sdhci_alloc_host(pdev->dev.parent, 0);
  103. else
  104. host = sdhci_alloc_host(&pdev->dev, 0);
  105. if (IS_ERR(host)) {
  106. ret = PTR_ERR(host);
  107. dev_dbg(&pdev->dev, "error allocating host\n");
  108. goto err_alloc_host;
  109. }
  110. host->hw_name = "sdhci";
  111. host->ops = &sdhci_pltfm_ops;
  112. host->irq = platform_get_irq(pdev, 0);
  113. host->quirks = SDHCI_QUIRK_BROKEN_ADMA;
  114. host->ioaddr = ioremap(iomem->start, resource_size(iomem));
  115. if (!host->ioaddr) {
  116. ret = -ENOMEM;
  117. dev_dbg(&pdev->dev, "failed to remap registers\n");
  118. goto err_ioremap;
  119. }
  120. ret = sdhci_add_host(host);
  121. if (ret) {
  122. dev_dbg(&pdev->dev, "error adding host\n");
  123. goto err_add_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 = gpio_request(sdhci->data->card_power_gpio, "sdhci");
  139. if (ret < 0) {
  140. dev_dbg(&pdev->dev, "gpio request fail: %d\n",
  141. sdhci->data->card_power_gpio);
  142. goto err_pgpio_request;
  143. }
  144. if (sdhci->data->power_always_enb)
  145. val = sdhci->data->power_active_high;
  146. else
  147. val = !sdhci->data->power_active_high;
  148. ret = gpio_direction_output(sdhci->data->card_power_gpio, val);
  149. if (ret) {
  150. dev_dbg(&pdev->dev, "gpio set direction fail: %d\n",
  151. sdhci->data->card_power_gpio);
  152. goto err_pgpio_direction;
  153. }
  154. }
  155. if (sdhci->data->card_int_gpio >= 0) {
  156. ret = gpio_request(sdhci->data->card_int_gpio, "sdhci");
  157. if (ret < 0) {
  158. dev_dbg(&pdev->dev, "gpio request fail: %d\n",
  159. sdhci->data->card_int_gpio);
  160. goto err_igpio_request;
  161. }
  162. ret = gpio_direction_input(sdhci->data->card_int_gpio);
  163. if (ret) {
  164. dev_dbg(&pdev->dev, "gpio set direction fail: %d\n",
  165. sdhci->data->card_int_gpio);
  166. goto err_igpio_direction;
  167. }
  168. ret = request_irq(gpio_to_irq(sdhci->data->card_int_gpio),
  169. sdhci_gpio_irq, IRQF_TRIGGER_LOW,
  170. mmc_hostname(host->mmc), pdev);
  171. if (ret) {
  172. dev_dbg(&pdev->dev, "gpio request irq fail: %d\n",
  173. sdhci->data->card_int_gpio);
  174. goto err_igpio_request_irq;
  175. }
  176. }
  177. return 0;
  178. err_igpio_request_irq:
  179. err_igpio_direction:
  180. if (sdhci->data->card_int_gpio >= 0)
  181. gpio_free(sdhci->data->card_int_gpio);
  182. err_igpio_request:
  183. err_pgpio_direction:
  184. if (sdhci->data->card_power_gpio >= 0)
  185. gpio_free(sdhci->data->card_power_gpio);
  186. err_pgpio_request:
  187. platform_set_drvdata(pdev, NULL);
  188. sdhci_remove_host(host, 1);
  189. err_add_host:
  190. iounmap(host->ioaddr);
  191. err_ioremap:
  192. sdhci_free_host(host);
  193. err_alloc_host:
  194. clk_disable(sdhci->clk);
  195. err_clk_enb:
  196. clk_put(sdhci->clk);
  197. err_clk_get:
  198. kfree(sdhci);
  199. err_kzalloc:
  200. release_mem_region(iomem->start, resource_size(iomem));
  201. err:
  202. dev_err(&pdev->dev, "spear-sdhci probe failed: %d\n", ret);
  203. return ret;
  204. }
  205. static int __devexit sdhci_remove(struct platform_device *pdev)
  206. {
  207. struct sdhci_host *host = platform_get_drvdata(pdev);
  208. struct resource *iomem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  209. struct spear_sdhci *sdhci = dev_get_platdata(&pdev->dev);
  210. int dead;
  211. u32 scratch;
  212. if (sdhci->data) {
  213. if (sdhci->data->card_int_gpio >= 0) {
  214. free_irq(gpio_to_irq(sdhci->data->card_int_gpio), pdev);
  215. gpio_free(sdhci->data->card_int_gpio);
  216. }
  217. if (sdhci->data->card_power_gpio >= 0)
  218. gpio_free(sdhci->data->card_power_gpio);
  219. }
  220. platform_set_drvdata(pdev, NULL);
  221. dead = 0;
  222. scratch = readl(host->ioaddr + SDHCI_INT_STATUS);
  223. if (scratch == (u32)-1)
  224. dead = 1;
  225. sdhci_remove_host(host, dead);
  226. iounmap(host->ioaddr);
  227. sdhci_free_host(host);
  228. clk_disable(sdhci->clk);
  229. clk_put(sdhci->clk);
  230. kfree(sdhci);
  231. if (iomem)
  232. release_mem_region(iomem->start, resource_size(iomem));
  233. return 0;
  234. }
  235. #ifdef CONFIG_PM
  236. static int sdhci_suspend(struct device *dev)
  237. {
  238. struct sdhci_host *host = dev_get_drvdata(dev);
  239. struct spear_sdhci *sdhci = dev_get_platdata(dev);
  240. int ret;
  241. ret = sdhci_suspend_host(host);
  242. if (!ret)
  243. clk_disable(sdhci->clk);
  244. return ret;
  245. }
  246. static int sdhci_resume(struct device *dev)
  247. {
  248. struct sdhci_host *host = dev_get_drvdata(dev);
  249. struct spear_sdhci *sdhci = dev_get_platdata(dev);
  250. int ret;
  251. ret = clk_enable(sdhci->clk);
  252. if (ret) {
  253. dev_dbg(dev, "Resume: Error enabling clock\n");
  254. return ret;
  255. }
  256. return sdhci_resume_host(host);
  257. }
  258. #endif
  259. static SIMPLE_DEV_PM_OPS(sdhci_pm_ops, sdhci_suspend, sdhci_resume);
  260. static struct platform_driver sdhci_driver = {
  261. .driver = {
  262. .name = "sdhci",
  263. .owner = THIS_MODULE,
  264. .pm = &sdhci_pm_ops,
  265. },
  266. .probe = sdhci_probe,
  267. .remove = __devexit_p(sdhci_remove),
  268. };
  269. module_platform_driver(sdhci_driver);
  270. MODULE_DESCRIPTION("SPEAr Secure Digital Host Controller Interface driver");
  271. MODULE_AUTHOR("Viresh Kumar <viresh.kumar@st.com>");
  272. MODULE_LICENSE("GPL v2");