sdhci-spear.c 8.3 KB

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