sdhci-spear.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  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. ret = clk_set_rate(sdhci->clk, 50000000);
  129. if (ret)
  130. dev_dbg(&pdev->dev, "Error setting desired clk, clk=%lu\n",
  131. clk_get_rate(sdhci->clk));
  132. if (np) {
  133. sdhci->data = sdhci_probe_config_dt(pdev);
  134. if (IS_ERR(sdhci->data)) {
  135. dev_err(&pdev->dev, "DT: Failed to get pdata\n");
  136. return -ENODEV;
  137. }
  138. } else {
  139. sdhci->data = dev_get_platdata(&pdev->dev);
  140. }
  141. pdev->dev.platform_data = sdhci;
  142. if (pdev->dev.parent)
  143. host = sdhci_alloc_host(pdev->dev.parent, 0);
  144. else
  145. host = sdhci_alloc_host(&pdev->dev, 0);
  146. if (IS_ERR(host)) {
  147. ret = PTR_ERR(host);
  148. dev_dbg(&pdev->dev, "error allocating host\n");
  149. goto disable_clk;
  150. }
  151. host->hw_name = "sdhci";
  152. host->ops = &sdhci_pltfm_ops;
  153. host->irq = platform_get_irq(pdev, 0);
  154. host->quirks = SDHCI_QUIRK_BROKEN_ADMA;
  155. host->ioaddr = devm_ioremap(&pdev->dev, iomem->start,
  156. resource_size(iomem));
  157. if (!host->ioaddr) {
  158. ret = -ENOMEM;
  159. dev_dbg(&pdev->dev, "failed to remap registers\n");
  160. goto free_host;
  161. }
  162. ret = sdhci_add_host(host);
  163. if (ret) {
  164. dev_dbg(&pdev->dev, "error adding host\n");
  165. goto free_host;
  166. }
  167. platform_set_drvdata(pdev, host);
  168. /*
  169. * It is optional to use GPIOs for sdhci Power control & sdhci card
  170. * interrupt detection. If sdhci->data is NULL, then use original sdhci
  171. * lines otherwise GPIO lines.
  172. * If GPIO is selected for power control, then power should be disabled
  173. * after card removal and should be enabled when card insertion
  174. * interrupt occurs
  175. */
  176. if (!sdhci->data)
  177. return 0;
  178. if (sdhci->data->card_power_gpio >= 0) {
  179. int val = 0;
  180. ret = devm_gpio_request(&pdev->dev,
  181. sdhci->data->card_power_gpio, "sdhci");
  182. if (ret < 0) {
  183. dev_dbg(&pdev->dev, "gpio request fail: %d\n",
  184. sdhci->data->card_power_gpio);
  185. goto set_drvdata;
  186. }
  187. if (sdhci->data->power_always_enb)
  188. val = sdhci->data->power_active_high;
  189. else
  190. val = !sdhci->data->power_active_high;
  191. ret = gpio_direction_output(sdhci->data->card_power_gpio, val);
  192. if (ret) {
  193. dev_dbg(&pdev->dev, "gpio set direction fail: %d\n",
  194. sdhci->data->card_power_gpio);
  195. goto set_drvdata;
  196. }
  197. }
  198. if (sdhci->data->card_int_gpio >= 0) {
  199. ret = devm_gpio_request(&pdev->dev, sdhci->data->card_int_gpio,
  200. "sdhci");
  201. if (ret < 0) {
  202. dev_dbg(&pdev->dev, "gpio request fail: %d\n",
  203. sdhci->data->card_int_gpio);
  204. goto set_drvdata;
  205. }
  206. ret = gpio_direction_input(sdhci->data->card_int_gpio);
  207. if (ret) {
  208. dev_dbg(&pdev->dev, "gpio set direction fail: %d\n",
  209. sdhci->data->card_int_gpio);
  210. goto set_drvdata;
  211. }
  212. ret = devm_request_irq(&pdev->dev,
  213. gpio_to_irq(sdhci->data->card_int_gpio),
  214. sdhci_gpio_irq, IRQF_TRIGGER_LOW,
  215. mmc_hostname(host->mmc), pdev);
  216. if (ret) {
  217. dev_dbg(&pdev->dev, "gpio request irq fail: %d\n",
  218. sdhci->data->card_int_gpio);
  219. goto set_drvdata;
  220. }
  221. }
  222. return 0;
  223. set_drvdata:
  224. platform_set_drvdata(pdev, NULL);
  225. sdhci_remove_host(host, 1);
  226. free_host:
  227. sdhci_free_host(host);
  228. disable_clk:
  229. clk_disable_unprepare(sdhci->clk);
  230. put_clk:
  231. clk_put(sdhci->clk);
  232. err:
  233. dev_err(&pdev->dev, "spear-sdhci probe failed: %d\n", ret);
  234. return ret;
  235. }
  236. static int __devexit sdhci_remove(struct platform_device *pdev)
  237. {
  238. struct sdhci_host *host = platform_get_drvdata(pdev);
  239. struct spear_sdhci *sdhci = dev_get_platdata(&pdev->dev);
  240. int dead = 0;
  241. u32 scratch;
  242. platform_set_drvdata(pdev, NULL);
  243. scratch = readl(host->ioaddr + SDHCI_INT_STATUS);
  244. if (scratch == (u32)-1)
  245. dead = 1;
  246. sdhci_remove_host(host, dead);
  247. sdhci_free_host(host);
  248. clk_disable_unprepare(sdhci->clk);
  249. clk_put(sdhci->clk);
  250. return 0;
  251. }
  252. #ifdef CONFIG_PM
  253. static int sdhci_suspend(struct device *dev)
  254. {
  255. struct sdhci_host *host = dev_get_drvdata(dev);
  256. struct spear_sdhci *sdhci = dev_get_platdata(dev);
  257. int ret;
  258. ret = sdhci_suspend_host(host);
  259. if (!ret)
  260. clk_disable(sdhci->clk);
  261. return ret;
  262. }
  263. static int sdhci_resume(struct device *dev)
  264. {
  265. struct sdhci_host *host = dev_get_drvdata(dev);
  266. struct spear_sdhci *sdhci = dev_get_platdata(dev);
  267. int ret;
  268. ret = clk_enable(sdhci->clk);
  269. if (ret) {
  270. dev_dbg(dev, "Resume: Error enabling clock\n");
  271. return ret;
  272. }
  273. return sdhci_resume_host(host);
  274. }
  275. #endif
  276. static SIMPLE_DEV_PM_OPS(sdhci_pm_ops, sdhci_suspend, sdhci_resume);
  277. #ifdef CONFIG_OF
  278. static const struct of_device_id sdhci_spear_id_table[] = {
  279. { .compatible = "st,spear300-sdhci" },
  280. {}
  281. };
  282. MODULE_DEVICE_TABLE(of, sdhci_spear_id_table);
  283. #endif
  284. static struct platform_driver sdhci_driver = {
  285. .driver = {
  286. .name = "sdhci",
  287. .owner = THIS_MODULE,
  288. .pm = &sdhci_pm_ops,
  289. .of_match_table = of_match_ptr(sdhci_spear_id_table),
  290. },
  291. .probe = sdhci_probe,
  292. .remove = __devexit_p(sdhci_remove),
  293. };
  294. module_platform_driver(sdhci_driver);
  295. MODULE_DESCRIPTION("SPEAr Secure Digital Host Controller Interface driver");
  296. MODULE_AUTHOR("Viresh Kumar <viresh.linux@gmail.com>");
  297. MODULE_LICENSE("GPL v2");