sh_mobile_sdhi.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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/platform_device.h>
  23. #include <linux/mfd/core.h>
  24. #include <linux/mfd/tmio.h>
  25. #include <linux/mfd/sh_mobile_sdhi.h>
  26. struct sh_mobile_sdhi {
  27. struct clk *clk;
  28. struct tmio_mmc_data mmc_data;
  29. struct mfd_cell cell_mmc;
  30. };
  31. static struct resource sh_mobile_sdhi_resources[] = {
  32. {
  33. .start = 0x000,
  34. .end = 0x1ff,
  35. .flags = IORESOURCE_MEM,
  36. },
  37. {
  38. .start = 0,
  39. .end = 0,
  40. .flags = IORESOURCE_IRQ,
  41. },
  42. };
  43. static struct mfd_cell sh_mobile_sdhi_cell = {
  44. .name = "tmio-mmc",
  45. .num_resources = ARRAY_SIZE(sh_mobile_sdhi_resources),
  46. .resources = sh_mobile_sdhi_resources,
  47. };
  48. static void sh_mobile_sdhi_set_pwr(struct platform_device *tmio, int state)
  49. {
  50. struct platform_device *pdev = to_platform_device(tmio->dev.parent);
  51. struct sh_mobile_sdhi_info *p = pdev->dev.platform_data;
  52. if (p && p->set_pwr)
  53. p->set_pwr(pdev, state);
  54. }
  55. static int __init sh_mobile_sdhi_probe(struct platform_device *pdev)
  56. {
  57. struct sh_mobile_sdhi *priv;
  58. struct resource *mem;
  59. char clk_name[8];
  60. int ret, irq;
  61. mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  62. if (!mem)
  63. dev_err(&pdev->dev, "missing MEM resource\n");
  64. irq = platform_get_irq(pdev, 0);
  65. if (irq < 0)
  66. dev_err(&pdev->dev, "missing IRQ resource\n");
  67. if (!mem || (irq < 0))
  68. return -EINVAL;
  69. priv = kzalloc(sizeof(struct sh_mobile_sdhi), GFP_KERNEL);
  70. if (priv == NULL) {
  71. dev_err(&pdev->dev, "kzalloc failed\n");
  72. return -ENOMEM;
  73. }
  74. snprintf(clk_name, sizeof(clk_name), "sdhi%d", pdev->id);
  75. priv->clk = clk_get(&pdev->dev, clk_name);
  76. if (IS_ERR(priv->clk)) {
  77. dev_err(&pdev->dev, "cannot get clock \"%s\"\n", clk_name);
  78. ret = PTR_ERR(priv->clk);
  79. kfree(priv);
  80. return ret;
  81. }
  82. clk_enable(priv->clk);
  83. /* FIXME: silly const unsigned int hclk */
  84. *(unsigned int *)&priv->mmc_data.hclk = clk_get_rate(priv->clk);
  85. priv->mmc_data.set_pwr = sh_mobile_sdhi_set_pwr;
  86. memcpy(&priv->cell_mmc, &sh_mobile_sdhi_cell, sizeof(priv->cell_mmc));
  87. priv->cell_mmc.driver_data = &priv->mmc_data;
  88. priv->cell_mmc.platform_data = &priv->cell_mmc;
  89. priv->cell_mmc.data_size = sizeof(priv->cell_mmc);
  90. platform_set_drvdata(pdev, priv);
  91. ret = mfd_add_devices(&pdev->dev, pdev->id,
  92. &priv->cell_mmc, 1, mem, irq);
  93. if (ret) {
  94. clk_disable(priv->clk);
  95. clk_put(priv->clk);
  96. kfree(priv);
  97. }
  98. return ret;
  99. }
  100. static int sh_mobile_sdhi_remove(struct platform_device *pdev)
  101. {
  102. struct sh_mobile_sdhi *priv = platform_get_drvdata(pdev);
  103. mfd_remove_devices(&pdev->dev);
  104. clk_disable(priv->clk);
  105. clk_put(priv->clk);
  106. kfree(priv);
  107. return 0;
  108. }
  109. static struct platform_driver sh_mobile_sdhi_driver = {
  110. .driver = {
  111. .name = "sh_mobile_sdhi",
  112. .owner = THIS_MODULE,
  113. },
  114. .probe = sh_mobile_sdhi_probe,
  115. .remove = __devexit_p(sh_mobile_sdhi_remove),
  116. };
  117. static int __init sh_mobile_sdhi_init(void)
  118. {
  119. return platform_driver_register(&sh_mobile_sdhi_driver);
  120. }
  121. static void __exit sh_mobile_sdhi_exit(void)
  122. {
  123. platform_driver_unregister(&sh_mobile_sdhi_driver);
  124. }
  125. module_init(sh_mobile_sdhi_init);
  126. module_exit(sh_mobile_sdhi_exit);
  127. MODULE_DESCRIPTION("SuperH Mobile SDHI driver");
  128. MODULE_AUTHOR("Magnus Damm");
  129. MODULE_LICENSE("GPL v2");