sdhci-spear.c 8.2 KB

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