dw_mmc-k3.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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/pm_runtime.h>
  18. #include <linux/regmap.h>
  19. #include <linux/regulator/consumer.h>
  20. #include "dw_mmc.h"
  21. #include "dw_mmc-pltfm.h"
  22. /*
  23. * hi6220 sd only support io voltage 1.8v and 3v
  24. * Also need config AO_SCTRL_SEL18 accordingly
  25. */
  26. #define AO_SCTRL_SEL18 BIT(10)
  27. #define AO_SCTRL_CTRL3 0x40C
  28. struct k3_priv {
  29. struct regmap *reg;
  30. };
  31. static unsigned long dw_mci_hi6220_caps[] = {
  32. MMC_CAP_CMD23,
  33. MMC_CAP_CMD23,
  34. 0
  35. };
  36. static void dw_mci_k3_set_ios(struct dw_mci *host, struct mmc_ios *ios)
  37. {
  38. int ret;
  39. ret = clk_set_rate(host->ciu_clk, ios->clock);
  40. if (ret)
  41. dev_warn(host->dev, "failed to set rate %uHz\n", ios->clock);
  42. host->bus_hz = clk_get_rate(host->ciu_clk);
  43. }
  44. static const struct dw_mci_drv_data k3_drv_data = {
  45. .set_ios = dw_mci_k3_set_ios,
  46. };
  47. static int dw_mci_hi6220_parse_dt(struct dw_mci *host)
  48. {
  49. struct k3_priv *priv;
  50. priv = devm_kzalloc(host->dev, sizeof(*priv), GFP_KERNEL);
  51. if (!priv)
  52. return -ENOMEM;
  53. priv->reg = syscon_regmap_lookup_by_phandle(host->dev->of_node,
  54. "hisilicon,peripheral-syscon");
  55. if (IS_ERR(priv->reg))
  56. priv->reg = NULL;
  57. host->priv = priv;
  58. return 0;
  59. }
  60. static int dw_mci_hi6220_switch_voltage(struct mmc_host *mmc, struct mmc_ios *ios)
  61. {
  62. struct dw_mci_slot *slot = mmc_priv(mmc);
  63. struct k3_priv *priv;
  64. struct dw_mci *host;
  65. int min_uv, max_uv;
  66. int ret;
  67. host = slot->host;
  68. priv = host->priv;
  69. if (!priv || !priv->reg)
  70. return 0;
  71. if (ios->signal_voltage == MMC_SIGNAL_VOLTAGE_330) {
  72. ret = regmap_update_bits(priv->reg, AO_SCTRL_CTRL3,
  73. AO_SCTRL_SEL18, 0);
  74. min_uv = 3000000;
  75. max_uv = 3000000;
  76. } else if (ios->signal_voltage == MMC_SIGNAL_VOLTAGE_180) {
  77. ret = regmap_update_bits(priv->reg, AO_SCTRL_CTRL3,
  78. AO_SCTRL_SEL18, AO_SCTRL_SEL18);
  79. min_uv = 1800000;
  80. max_uv = 1800000;
  81. } else {
  82. dev_dbg(host->dev, "voltage not supported\n");
  83. return -EINVAL;
  84. }
  85. if (ret) {
  86. dev_dbg(host->dev, "switch voltage failed\n");
  87. return ret;
  88. }
  89. if (IS_ERR_OR_NULL(mmc->supply.vqmmc))
  90. return 0;
  91. ret = regulator_set_voltage(mmc->supply.vqmmc, min_uv, max_uv);
  92. if (ret) {
  93. dev_dbg(host->dev, "Regulator set error %d: %d - %d\n",
  94. ret, min_uv, max_uv);
  95. return ret;
  96. }
  97. return 0;
  98. }
  99. static void dw_mci_hi6220_set_ios(struct dw_mci *host, struct mmc_ios *ios)
  100. {
  101. int ret;
  102. unsigned int clock;
  103. clock = (ios->clock <= 25000000) ? 25000000 : ios->clock;
  104. ret = clk_set_rate(host->biu_clk, clock);
  105. if (ret)
  106. dev_warn(host->dev, "failed to set rate %uHz\n", clock);
  107. host->bus_hz = clk_get_rate(host->biu_clk);
  108. }
  109. static int dw_mci_hi6220_execute_tuning(struct dw_mci_slot *slot, u32 opcode)
  110. {
  111. return 0;
  112. }
  113. static const struct dw_mci_drv_data hi6220_data = {
  114. .caps = dw_mci_hi6220_caps,
  115. .switch_voltage = dw_mci_hi6220_switch_voltage,
  116. .set_ios = dw_mci_hi6220_set_ios,
  117. .parse_dt = dw_mci_hi6220_parse_dt,
  118. .execute_tuning = dw_mci_hi6220_execute_tuning,
  119. };
  120. static const struct of_device_id dw_mci_k3_match[] = {
  121. { .compatible = "hisilicon,hi4511-dw-mshc", .data = &k3_drv_data, },
  122. { .compatible = "hisilicon,hi6220-dw-mshc", .data = &hi6220_data, },
  123. {},
  124. };
  125. MODULE_DEVICE_TABLE(of, dw_mci_k3_match);
  126. static int dw_mci_k3_probe(struct platform_device *pdev)
  127. {
  128. const struct dw_mci_drv_data *drv_data;
  129. const struct of_device_id *match;
  130. match = of_match_node(dw_mci_k3_match, pdev->dev.of_node);
  131. drv_data = match->data;
  132. return dw_mci_pltfm_register(pdev, drv_data);
  133. }
  134. static const struct dev_pm_ops dw_mci_k3_dev_pm_ops = {
  135. SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
  136. pm_runtime_force_resume)
  137. SET_RUNTIME_PM_OPS(dw_mci_runtime_suspend,
  138. dw_mci_runtime_resume,
  139. NULL)
  140. };
  141. static struct platform_driver dw_mci_k3_pltfm_driver = {
  142. .probe = dw_mci_k3_probe,
  143. .remove = dw_mci_pltfm_remove,
  144. .driver = {
  145. .name = "dwmmc_k3",
  146. .of_match_table = dw_mci_k3_match,
  147. .pm = &dw_mci_k3_dev_pm_ops,
  148. },
  149. };
  150. module_platform_driver(dw_mci_k3_pltfm_driver);
  151. MODULE_DESCRIPTION("K3 Specific DW-MSHC Driver Extension");
  152. MODULE_LICENSE("GPL v2");
  153. MODULE_ALIAS("platform:dwmmc_k3");