sdhci-dove.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. /*
  2. * sdhci-dove.c Support for SDHCI on Marvell's Dove SoC
  3. *
  4. * Author: Saeed Bishara <saeed@marvell.com>
  5. * Mike Rapoport <mike@compulab.co.il>
  6. * Based on sdhci-cns3xxx.c
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License version 2 as
  10. * published by the Free Software Foundation.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  20. */
  21. #include <linux/clk.h>
  22. #include <linux/err.h>
  23. #include <linux/gpio.h>
  24. #include <linux/io.h>
  25. #include <linux/mmc/host.h>
  26. #include <linux/module.h>
  27. #include <linux/of.h>
  28. #include <linux/of_gpio.h>
  29. #include "sdhci-pltfm.h"
  30. struct sdhci_dove_priv {
  31. struct clk *clk;
  32. int gpio_cd;
  33. };
  34. static irqreturn_t sdhci_dove_carddetect_irq(int irq, void *data)
  35. {
  36. struct sdhci_host *host = data;
  37. tasklet_schedule(&host->card_tasklet);
  38. return IRQ_HANDLED;
  39. }
  40. static u16 sdhci_dove_readw(struct sdhci_host *host, int reg)
  41. {
  42. u16 ret;
  43. switch (reg) {
  44. case SDHCI_HOST_VERSION:
  45. case SDHCI_SLOT_INT_STATUS:
  46. /* those registers don't exist */
  47. return 0;
  48. default:
  49. ret = readw(host->ioaddr + reg);
  50. }
  51. return ret;
  52. }
  53. static u32 sdhci_dove_readl(struct sdhci_host *host, int reg)
  54. {
  55. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  56. struct sdhci_dove_priv *priv = pltfm_host->priv;
  57. u32 ret;
  58. ret = readl(host->ioaddr + reg);
  59. switch (reg) {
  60. case SDHCI_CAPABILITIES:
  61. /* Mask the support for 3.0V */
  62. ret &= ~SDHCI_CAN_VDD_300;
  63. break;
  64. case SDHCI_PRESENT_STATE:
  65. if (gpio_is_valid(priv->gpio_cd)) {
  66. if (gpio_get_value(priv->gpio_cd) == 0)
  67. ret |= SDHCI_CARD_PRESENT;
  68. else
  69. ret &= ~SDHCI_CARD_PRESENT;
  70. }
  71. break;
  72. }
  73. return ret;
  74. }
  75. static struct sdhci_ops sdhci_dove_ops = {
  76. .read_w = sdhci_dove_readw,
  77. .read_l = sdhci_dove_readl,
  78. };
  79. static struct sdhci_pltfm_data sdhci_dove_pdata = {
  80. .ops = &sdhci_dove_ops,
  81. .quirks = SDHCI_QUIRK_NO_SIMULT_VDD_AND_POWER |
  82. SDHCI_QUIRK_NO_BUSY_IRQ |
  83. SDHCI_QUIRK_BROKEN_TIMEOUT_VAL |
  84. SDHCI_QUIRK_FORCE_DMA |
  85. SDHCI_QUIRK_NO_HISPD_BIT,
  86. };
  87. static int sdhci_dove_probe(struct platform_device *pdev)
  88. {
  89. struct sdhci_host *host;
  90. struct sdhci_pltfm_host *pltfm_host;
  91. struct sdhci_dove_priv *priv;
  92. int ret;
  93. priv = devm_kzalloc(&pdev->dev, sizeof(struct sdhci_dove_priv),
  94. GFP_KERNEL);
  95. if (!priv) {
  96. dev_err(&pdev->dev, "unable to allocate private data");
  97. return -ENOMEM;
  98. }
  99. priv->clk = devm_clk_get(&pdev->dev, NULL);
  100. if (pdev->dev.of_node) {
  101. priv->gpio_cd = of_get_named_gpio(pdev->dev.of_node,
  102. "cd-gpios", 0);
  103. } else {
  104. priv->gpio_cd = -EINVAL;
  105. }
  106. if (gpio_is_valid(priv->gpio_cd)) {
  107. ret = gpio_request(priv->gpio_cd, "sdhci-cd");
  108. if (ret) {
  109. dev_err(&pdev->dev, "card detect gpio request failed: %d\n",
  110. ret);
  111. return ret;
  112. }
  113. gpio_direction_input(priv->gpio_cd);
  114. }
  115. host = sdhci_pltfm_init(pdev, &sdhci_dove_pdata);
  116. if (IS_ERR(host)) {
  117. ret = PTR_ERR(host);
  118. goto err_sdhci_pltfm_init;
  119. }
  120. pltfm_host = sdhci_priv(host);
  121. pltfm_host->priv = priv;
  122. if (!IS_ERR(priv->clk))
  123. clk_prepare_enable(priv->clk);
  124. sdhci_get_of_property(pdev);
  125. ret = sdhci_add_host(host);
  126. if (ret)
  127. goto err_sdhci_add;
  128. /*
  129. * We must request the IRQ after sdhci_add_host(), as the tasklet only
  130. * gets setup in sdhci_add_host() and we oops.
  131. */
  132. if (gpio_is_valid(priv->gpio_cd)) {
  133. ret = request_irq(gpio_to_irq(priv->gpio_cd),
  134. sdhci_dove_carddetect_irq,
  135. IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING,
  136. mmc_hostname(host->mmc), host);
  137. if (ret) {
  138. dev_err(&pdev->dev, "card detect irq request failed: %d\n",
  139. ret);
  140. goto err_request_irq;
  141. }
  142. }
  143. return 0;
  144. err_request_irq:
  145. sdhci_remove_host(host, 0);
  146. err_sdhci_add:
  147. if (!IS_ERR(priv->clk))
  148. clk_disable_unprepare(priv->clk);
  149. sdhci_pltfm_free(pdev);
  150. err_sdhci_pltfm_init:
  151. if (gpio_is_valid(priv->gpio_cd))
  152. gpio_free(priv->gpio_cd);
  153. return ret;
  154. }
  155. static int sdhci_dove_remove(struct platform_device *pdev)
  156. {
  157. struct sdhci_host *host = platform_get_drvdata(pdev);
  158. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  159. struct sdhci_dove_priv *priv = pltfm_host->priv;
  160. sdhci_pltfm_unregister(pdev);
  161. if (gpio_is_valid(priv->gpio_cd)) {
  162. free_irq(gpio_to_irq(priv->gpio_cd), host);
  163. gpio_free(priv->gpio_cd);
  164. }
  165. if (!IS_ERR(priv->clk))
  166. clk_disable_unprepare(priv->clk);
  167. return 0;
  168. }
  169. static const struct of_device_id sdhci_dove_of_match_table[] = {
  170. { .compatible = "marvell,dove-sdhci", },
  171. {}
  172. };
  173. MODULE_DEVICE_TABLE(of, sdhci_dove_of_match_table);
  174. static struct platform_driver sdhci_dove_driver = {
  175. .driver = {
  176. .name = "sdhci-dove",
  177. .owner = THIS_MODULE,
  178. .pm = SDHCI_PLTFM_PMOPS,
  179. .of_match_table = of_match_ptr(sdhci_dove_of_match_table),
  180. },
  181. .probe = sdhci_dove_probe,
  182. .remove = sdhci_dove_remove,
  183. };
  184. module_platform_driver(sdhci_dove_driver);
  185. MODULE_DESCRIPTION("SDHCI driver for Dove");
  186. MODULE_AUTHOR("Saeed Bishara <saeed@marvell.com>, "
  187. "Mike Rapoport <mike@compulab.co.il>");
  188. MODULE_LICENSE("GPL v2");