dw_mmc-k3.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. /*
  2. * Copyright (c) 2013 Linaro Ltd.
  3. * Copyright (c) 2013 Hisilicon Limited.
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. */
  10. #include <linux/clk.h>
  11. #include <linux/mfd/syscon.h>
  12. #include <linux/mmc/host.h>
  13. #include <linux/mmc/dw_mmc.h>
  14. #include <linux/module.h>
  15. #include <linux/of_address.h>
  16. #include <linux/platform_device.h>
  17. #include <linux/regmap.h>
  18. #include <linux/regulator/consumer.h>
  19. #include "dw_mmc.h"
  20. #include "dw_mmc-pltfm.h"
  21. /*
  22. * hi6220 sd only support io voltage 1.8v and 3v
  23. * Also need config AO_SCTRL_SEL18 accordingly
  24. */
  25. #define AO_SCTRL_SEL18 BIT(10)
  26. #define AO_SCTRL_CTRL3 0x40C
  27. struct k3_priv {
  28. struct regmap *reg;
  29. };
  30. static unsigned long dw_mci_hi6220_caps[] = {
  31. MMC_CAP_CMD23,
  32. MMC_CAP_CMD23,
  33. 0
  34. };
  35. static void dw_mci_k3_set_ios(struct dw_mci *host, struct mmc_ios *ios)
  36. {
  37. int ret;
  38. ret = clk_set_rate(host->ciu_clk, ios->clock);
  39. if (ret)
  40. dev_warn(host->dev, "failed to set rate %uHz\n", ios->clock);
  41. host->bus_hz = clk_get_rate(host->ciu_clk);
  42. }
  43. static const struct dw_mci_drv_data k3_drv_data = {
  44. .set_ios = dw_mci_k3_set_ios,
  45. };
  46. static int dw_mci_hi6220_parse_dt(struct dw_mci *host)
  47. {
  48. struct k3_priv *priv;
  49. priv = devm_kzalloc(host->dev, sizeof(*priv), GFP_KERNEL);
  50. if (!priv)
  51. return -ENOMEM;
  52. priv->reg = syscon_regmap_lookup_by_phandle(host->dev->of_node,
  53. "hisilicon,peripheral-syscon");
  54. if (IS_ERR(priv->reg))
  55. priv->reg = NULL;
  56. host->priv = priv;
  57. return 0;
  58. }
  59. static int dw_mci_hi6220_switch_voltage(struct mmc_host *mmc, struct mmc_ios *ios)
  60. {
  61. struct dw_mci_slot *slot = mmc_priv(mmc);
  62. struct k3_priv *priv;
  63. struct dw_mci *host;
  64. int min_uv, max_uv;
  65. int ret;
  66. host = slot->host;
  67. priv = host->priv;
  68. if (!priv || !priv->reg)
  69. return 0;
  70. if (ios->signal_voltage == MMC_SIGNAL_VOLTAGE_330) {
  71. ret = regmap_update_bits(priv->reg, AO_SCTRL_CTRL3,
  72. AO_SCTRL_SEL18, 0);
  73. min_uv = 3000000;
  74. max_uv = 3000000;
  75. } else if (ios->signal_voltage == MMC_SIGNAL_VOLTAGE_180) {
  76. ret = regmap_update_bits(priv->reg, AO_SCTRL_CTRL3,
  77. AO_SCTRL_SEL18, AO_SCTRL_SEL18);
  78. min_uv = 1800000;
  79. max_uv = 1800000;
  80. } else {
  81. dev_dbg(host->dev, "voltage not supported\n");
  82. return -EINVAL;
  83. }
  84. if (ret) {
  85. dev_dbg(host->dev, "switch voltage failed\n");
  86. return ret;
  87. }
  88. if (IS_ERR_OR_NULL(mmc->supply.vqmmc))
  89. return 0;
  90. ret = regulator_set_voltage(mmc->supply.vqmmc, min_uv, max_uv);
  91. if (ret) {
  92. dev_dbg(host->dev, "Regulator set error %d: %d - %d\n",
  93. ret, min_uv, max_uv);
  94. return ret;
  95. }
  96. return 0;
  97. }
  98. static void dw_mci_hi6220_set_ios(struct dw_mci *host, struct mmc_ios *ios)
  99. {
  100. int ret;
  101. unsigned int clock;
  102. clock = (ios->clock <= 25000000) ? 25000000 : ios->clock;
  103. ret = clk_set_rate(host->biu_clk, clock);
  104. if (ret)
  105. dev_warn(host->dev, "failed to set rate %uHz\n", clock);
  106. host->bus_hz = clk_get_rate(host->biu_clk);
  107. }
  108. static int dw_mci_hi6220_execute_tuning(struct dw_mci_slot *slot, u32 opcode)
  109. {
  110. return 0;
  111. }
  112. static const struct dw_mci_drv_data hi6220_data = {
  113. .caps = dw_mci_hi6220_caps,
  114. .switch_voltage = dw_mci_hi6220_switch_voltage,
  115. .set_ios = dw_mci_hi6220_set_ios,
  116. .parse_dt = dw_mci_hi6220_parse_dt,
  117. .execute_tuning = dw_mci_hi6220_execute_tuning,
  118. };
  119. static const struct of_device_id dw_mci_k3_match[] = {
  120. { .compatible = "hisilicon,hi4511-dw-mshc", .data = &k3_drv_data, },
  121. { .compatible = "hisilicon,hi6220-dw-mshc", .data = &hi6220_data, },
  122. {},
  123. };
  124. MODULE_DEVICE_TABLE(of, dw_mci_k3_match);
  125. static int dw_mci_k3_probe(struct platform_device *pdev)
  126. {
  127. const struct dw_mci_drv_data *drv_data;
  128. const struct of_device_id *match;
  129. match = of_match_node(dw_mci_k3_match, pdev->dev.of_node);
  130. drv_data = match->data;
  131. return dw_mci_pltfm_register(pdev, drv_data);
  132. }
  133. #ifdef CONFIG_PM_SLEEP
  134. static int dw_mci_k3_suspend(struct device *dev)
  135. {
  136. struct dw_mci *host = dev_get_drvdata(dev);
  137. int ret;
  138. ret = dw_mci_suspend(host);
  139. if (!ret)
  140. clk_disable_unprepare(host->ciu_clk);
  141. return ret;
  142. }
  143. static int dw_mci_k3_resume(struct device *dev)
  144. {
  145. struct dw_mci *host = dev_get_drvdata(dev);
  146. int ret;
  147. ret = clk_prepare_enable(host->ciu_clk);
  148. if (ret) {
  149. dev_err(host->dev, "failed to enable ciu clock\n");
  150. return ret;
  151. }
  152. return dw_mci_resume(host);
  153. }
  154. #endif /* CONFIG_PM_SLEEP */
  155. static SIMPLE_DEV_PM_OPS(dw_mci_k3_pmops, dw_mci_k3_suspend, dw_mci_k3_resume);
  156. static struct platform_driver dw_mci_k3_pltfm_driver = {
  157. .probe = dw_mci_k3_probe,
  158. .remove = dw_mci_pltfm_remove,
  159. .driver = {
  160. .name = "dwmmc_k3",
  161. .of_match_table = dw_mci_k3_match,
  162. .pm = &dw_mci_k3_pmops,
  163. },
  164. };
  165. module_platform_driver(dw_mci_k3_pltfm_driver);
  166. MODULE_DESCRIPTION("K3 Specific DW-MSHC Driver Extension");
  167. MODULE_LICENSE("GPL v2");
  168. MODULE_ALIAS("platform:dwmmc_k3");