sh_mobile_sdhi.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /*
  2. * SuperH Mobile SDHI
  3. *
  4. * Copyright (C) 2009 Magnus Damm
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. *
  10. * Based on "Compaq ASIC3 support":
  11. *
  12. * Copyright 2001 Compaq Computer Corporation.
  13. * Copyright 2004-2005 Phil Blundell
  14. * Copyright 2007-2008 OpenedHand Ltd.
  15. *
  16. * Authors: Phil Blundell <pb@handhelds.org>,
  17. * Samuel Ortiz <sameo@openedhand.com>
  18. *
  19. */
  20. #include <linux/kernel.h>
  21. #include <linux/clk.h>
  22. #include <linux/slab.h>
  23. #include <linux/platform_device.h>
  24. #include <linux/mmc/host.h>
  25. #include <linux/mfd/core.h>
  26. #include <linux/mfd/tmio.h>
  27. #include <linux/mfd/sh_mobile_sdhi.h>
  28. #include <linux/sh_dma.h>
  29. struct sh_mobile_sdhi {
  30. struct clk *clk;
  31. struct tmio_mmc_data mmc_data;
  32. struct mfd_cell cell_mmc;
  33. struct sh_dmae_slave param_tx;
  34. struct sh_dmae_slave param_rx;
  35. struct tmio_mmc_dma dma_priv;
  36. };
  37. static struct resource sh_mobile_sdhi_resources[] = {
  38. {
  39. .start = 0x000,
  40. .end = 0x1ff,
  41. .flags = IORESOURCE_MEM,
  42. },
  43. {
  44. .start = 0,
  45. .end = 0,
  46. .flags = IORESOURCE_IRQ,
  47. },
  48. };
  49. static struct mfd_cell sh_mobile_sdhi_cell = {
  50. .name = "tmio-mmc",
  51. .num_resources = ARRAY_SIZE(sh_mobile_sdhi_resources),
  52. .resources = sh_mobile_sdhi_resources,
  53. };
  54. static void sh_mobile_sdhi_set_pwr(struct platform_device *tmio, int state)
  55. {
  56. struct platform_device *pdev = to_platform_device(tmio->dev.parent);
  57. struct sh_mobile_sdhi_info *p = pdev->dev.platform_data;
  58. if (p && p->set_pwr)
  59. p->set_pwr(pdev, state);
  60. }
  61. static int __init sh_mobile_sdhi_probe(struct platform_device *pdev)
  62. {
  63. struct sh_mobile_sdhi *priv;
  64. struct tmio_mmc_data *mmc_data;
  65. struct sh_mobile_sdhi_info *p = pdev->dev.platform_data;
  66. struct resource *mem;
  67. char clk_name[8];
  68. int ret, irq;
  69. mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  70. if (!mem)
  71. dev_err(&pdev->dev, "missing MEM resource\n");
  72. irq = platform_get_irq(pdev, 0);
  73. if (irq < 0)
  74. dev_err(&pdev->dev, "missing IRQ resource\n");
  75. if (!mem || (irq < 0))
  76. return -EINVAL;
  77. priv = kzalloc(sizeof(struct sh_mobile_sdhi), GFP_KERNEL);
  78. if (priv == NULL) {
  79. dev_err(&pdev->dev, "kzalloc failed\n");
  80. return -ENOMEM;
  81. }
  82. mmc_data = &priv->mmc_data;
  83. snprintf(clk_name, sizeof(clk_name), "sdhi%d", pdev->id);
  84. priv->clk = clk_get(&pdev->dev, clk_name);
  85. if (IS_ERR(priv->clk)) {
  86. dev_err(&pdev->dev, "cannot get clock \"%s\"\n", clk_name);
  87. ret = PTR_ERR(priv->clk);
  88. kfree(priv);
  89. return ret;
  90. }
  91. clk_enable(priv->clk);
  92. mmc_data->hclk = clk_get_rate(priv->clk);
  93. mmc_data->set_pwr = sh_mobile_sdhi_set_pwr;
  94. mmc_data->capabilities = MMC_CAP_MMC_HIGHSPEED;
  95. if (p)
  96. mmc_data->flags = p->tmio_flags;
  97. if (p && p->dma_slave_tx >= 0 && p->dma_slave_rx >= 0) {
  98. priv->param_tx.slave_id = p->dma_slave_tx;
  99. priv->param_rx.slave_id = p->dma_slave_rx;
  100. priv->dma_priv.chan_priv_tx = &priv->param_tx;
  101. priv->dma_priv.chan_priv_rx = &priv->param_rx;
  102. mmc_data->dma = &priv->dma_priv;
  103. }
  104. memcpy(&priv->cell_mmc, &sh_mobile_sdhi_cell, sizeof(priv->cell_mmc));
  105. priv->cell_mmc.driver_data = mmc_data;
  106. priv->cell_mmc.platform_data = &priv->cell_mmc;
  107. priv->cell_mmc.data_size = sizeof(priv->cell_mmc);
  108. platform_set_drvdata(pdev, priv);
  109. ret = mfd_add_devices(&pdev->dev, pdev->id,
  110. &priv->cell_mmc, 1, mem, irq);
  111. if (ret) {
  112. clk_disable(priv->clk);
  113. clk_put(priv->clk);
  114. kfree(priv);
  115. }
  116. return ret;
  117. }
  118. static int sh_mobile_sdhi_remove(struct platform_device *pdev)
  119. {
  120. struct sh_mobile_sdhi *priv = platform_get_drvdata(pdev);
  121. mfd_remove_devices(&pdev->dev);
  122. clk_disable(priv->clk);
  123. clk_put(priv->clk);
  124. kfree(priv);
  125. return 0;
  126. }
  127. static struct platform_driver sh_mobile_sdhi_driver = {
  128. .driver = {
  129. .name = "sh_mobile_sdhi",
  130. .owner = THIS_MODULE,
  131. },
  132. .probe = sh_mobile_sdhi_probe,
  133. .remove = __devexit_p(sh_mobile_sdhi_remove),
  134. };
  135. static int __init sh_mobile_sdhi_init(void)
  136. {
  137. return platform_driver_register(&sh_mobile_sdhi_driver);
  138. }
  139. static void __exit sh_mobile_sdhi_exit(void)
  140. {
  141. platform_driver_unregister(&sh_mobile_sdhi_driver);
  142. }
  143. module_init(sh_mobile_sdhi_init);
  144. module_exit(sh_mobile_sdhi_exit);
  145. MODULE_DESCRIPTION("SuperH Mobile SDHI driver");
  146. MODULE_AUTHOR("Magnus Damm");
  147. MODULE_LICENSE("GPL v2");