sdhci-spear.c 8.1 KB

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