regulator.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Helper functions for MMC regulators.
  4. */
  5. #include <linux/device.h>
  6. #include <linux/err.h>
  7. #include <linux/log2.h>
  8. #include <linux/regulator/consumer.h>
  9. #include <linux/mmc/host.h>
  10. #include "core.h"
  11. #include "host.h"
  12. #ifdef CONFIG_REGULATOR
  13. /**
  14. * mmc_ocrbitnum_to_vdd - Convert a OCR bit number to its voltage
  15. * @vdd_bit: OCR bit number
  16. * @min_uV: minimum voltage value (mV)
  17. * @max_uV: maximum voltage value (mV)
  18. *
  19. * This function returns the voltage range according to the provided OCR
  20. * bit number. If conversion is not possible a negative errno value returned.
  21. */
  22. static int mmc_ocrbitnum_to_vdd(int vdd_bit, int *min_uV, int *max_uV)
  23. {
  24. int tmp;
  25. if (!vdd_bit)
  26. return -EINVAL;
  27. /*
  28. * REVISIT mmc_vddrange_to_ocrmask() may have set some
  29. * bits this regulator doesn't quite support ... don't
  30. * be too picky, most cards and regulators are OK with
  31. * a 0.1V range goof (it's a small error percentage).
  32. */
  33. tmp = vdd_bit - ilog2(MMC_VDD_165_195);
  34. if (tmp == 0) {
  35. *min_uV = 1650 * 1000;
  36. *max_uV = 1950 * 1000;
  37. } else {
  38. *min_uV = 1900 * 1000 + tmp * 100 * 1000;
  39. *max_uV = *min_uV + 100 * 1000;
  40. }
  41. return 0;
  42. }
  43. /**
  44. * mmc_regulator_get_ocrmask - return mask of supported voltages
  45. * @supply: regulator to use
  46. *
  47. * This returns either a negative errno, or a mask of voltages that
  48. * can be provided to MMC/SD/SDIO devices using the specified voltage
  49. * regulator. This would normally be called before registering the
  50. * MMC host adapter.
  51. */
  52. static int mmc_regulator_get_ocrmask(struct regulator *supply)
  53. {
  54. int result = 0;
  55. int count;
  56. int i;
  57. int vdd_uV;
  58. int vdd_mV;
  59. count = regulator_count_voltages(supply);
  60. if (count < 0)
  61. return count;
  62. for (i = 0; i < count; i++) {
  63. vdd_uV = regulator_list_voltage(supply, i);
  64. if (vdd_uV <= 0)
  65. continue;
  66. vdd_mV = vdd_uV / 1000;
  67. result |= mmc_vddrange_to_ocrmask(vdd_mV, vdd_mV);
  68. }
  69. if (!result) {
  70. vdd_uV = regulator_get_voltage(supply);
  71. if (vdd_uV <= 0)
  72. return vdd_uV;
  73. vdd_mV = vdd_uV / 1000;
  74. result = mmc_vddrange_to_ocrmask(vdd_mV, vdd_mV);
  75. }
  76. return result;
  77. }
  78. /**
  79. * mmc_regulator_set_ocr - set regulator to match host->ios voltage
  80. * @mmc: the host to regulate
  81. * @supply: regulator to use
  82. * @vdd_bit: zero for power off, else a bit number (host->ios.vdd)
  83. *
  84. * Returns zero on success, else negative errno.
  85. *
  86. * MMC host drivers may use this to enable or disable a regulator using
  87. * a particular supply voltage. This would normally be called from the
  88. * set_ios() method.
  89. */
  90. int mmc_regulator_set_ocr(struct mmc_host *mmc,
  91. struct regulator *supply,
  92. unsigned short vdd_bit)
  93. {
  94. int result = 0;
  95. int min_uV, max_uV;
  96. if (vdd_bit) {
  97. mmc_ocrbitnum_to_vdd(vdd_bit, &min_uV, &max_uV);
  98. result = regulator_set_voltage(supply, min_uV, max_uV);
  99. if (result == 0 && !mmc->regulator_enabled) {
  100. result = regulator_enable(supply);
  101. if (!result)
  102. mmc->regulator_enabled = true;
  103. }
  104. } else if (mmc->regulator_enabled) {
  105. result = regulator_disable(supply);
  106. if (result == 0)
  107. mmc->regulator_enabled = false;
  108. }
  109. if (result)
  110. dev_err(mmc_dev(mmc),
  111. "could not set regulator OCR (%d)\n", result);
  112. return result;
  113. }
  114. EXPORT_SYMBOL_GPL(mmc_regulator_set_ocr);
  115. static int mmc_regulator_set_voltage_if_supported(struct regulator *regulator,
  116. int min_uV, int target_uV,
  117. int max_uV)
  118. {
  119. int current_uV;
  120. /*
  121. * Check if supported first to avoid errors since we may try several
  122. * signal levels during power up and don't want to show errors.
  123. */
  124. if (!regulator_is_supported_voltage(regulator, min_uV, max_uV))
  125. return -EINVAL;
  126. /*
  127. * The voltage is already set, no need to switch.
  128. * Return 1 to indicate that no switch happened.
  129. */
  130. current_uV = regulator_get_voltage(regulator);
  131. if (current_uV == target_uV)
  132. return 1;
  133. return regulator_set_voltage_triplet(regulator, min_uV, target_uV,
  134. max_uV);
  135. }
  136. /**
  137. * mmc_regulator_set_vqmmc - Set VQMMC as per the ios
  138. * @mmc: the host to regulate
  139. * @ios: io bus settings
  140. *
  141. * For 3.3V signaling, we try to match VQMMC to VMMC as closely as possible.
  142. * That will match the behavior of old boards where VQMMC and VMMC were supplied
  143. * by the same supply. The Bus Operating conditions for 3.3V signaling in the
  144. * SD card spec also define VQMMC in terms of VMMC.
  145. * If this is not possible we'll try the full 2.7-3.6V of the spec.
  146. *
  147. * For 1.2V and 1.8V signaling we'll try to get as close as possible to the
  148. * requested voltage. This is definitely a good idea for UHS where there's a
  149. * separate regulator on the card that's trying to make 1.8V and it's best if
  150. * we match.
  151. *
  152. * This function is expected to be used by a controller's
  153. * start_signal_voltage_switch() function.
  154. */
  155. int mmc_regulator_set_vqmmc(struct mmc_host *mmc, struct mmc_ios *ios)
  156. {
  157. struct device *dev = mmc_dev(mmc);
  158. int ret, volt, min_uV, max_uV;
  159. /* If no vqmmc supply then we can't change the voltage */
  160. if (IS_ERR(mmc->supply.vqmmc))
  161. return -EINVAL;
  162. switch (ios->signal_voltage) {
  163. case MMC_SIGNAL_VOLTAGE_120:
  164. return mmc_regulator_set_voltage_if_supported(mmc->supply.vqmmc,
  165. 1100000, 1200000, 1300000);
  166. case MMC_SIGNAL_VOLTAGE_180:
  167. return mmc_regulator_set_voltage_if_supported(mmc->supply.vqmmc,
  168. 1700000, 1800000, 1950000);
  169. case MMC_SIGNAL_VOLTAGE_330:
  170. ret = mmc_ocrbitnum_to_vdd(mmc->ios.vdd, &volt, &max_uV);
  171. if (ret < 0)
  172. return ret;
  173. dev_dbg(dev, "%s: found vmmc voltage range of %d-%duV\n",
  174. __func__, volt, max_uV);
  175. min_uV = max(volt - 300000, 2700000);
  176. max_uV = min(max_uV + 200000, 3600000);
  177. /*
  178. * Due to a limitation in the current implementation of
  179. * regulator_set_voltage_triplet() which is taking the lowest
  180. * voltage possible if below the target, search for a suitable
  181. * voltage in two steps and try to stay close to vmmc
  182. * with a 0.3V tolerance at first.
  183. */
  184. ret = mmc_regulator_set_voltage_if_supported(mmc->supply.vqmmc,
  185. min_uV, volt, max_uV);
  186. if (ret >= 0)
  187. return ret;
  188. return mmc_regulator_set_voltage_if_supported(mmc->supply.vqmmc,
  189. 2700000, volt, 3600000);
  190. default:
  191. return -EINVAL;
  192. }
  193. }
  194. EXPORT_SYMBOL_GPL(mmc_regulator_set_vqmmc);
  195. #else
  196. static inline int mmc_regulator_get_ocrmask(struct regulator *supply)
  197. {
  198. return 0;
  199. }
  200. #endif /* CONFIG_REGULATOR */
  201. /**
  202. * mmc_regulator_get_supply - try to get VMMC and VQMMC regulators for a host
  203. * @mmc: the host to regulate
  204. *
  205. * Returns 0 or errno. errno should be handled, it is either a critical error
  206. * or -EPROBE_DEFER. 0 means no critical error but it does not mean all
  207. * regulators have been found because they all are optional. If you require
  208. * certain regulators, you need to check separately in your driver if they got
  209. * populated after calling this function.
  210. */
  211. int mmc_regulator_get_supply(struct mmc_host *mmc)
  212. {
  213. struct device *dev = mmc_dev(mmc);
  214. int ret;
  215. mmc->supply.vmmc = devm_regulator_get_optional(dev, "vmmc");
  216. mmc->supply.vqmmc = devm_regulator_get_optional(dev, "vqmmc");
  217. if (IS_ERR(mmc->supply.vmmc)) {
  218. if (PTR_ERR(mmc->supply.vmmc) == -EPROBE_DEFER)
  219. return -EPROBE_DEFER;
  220. dev_dbg(dev, "No vmmc regulator found\n");
  221. } else {
  222. ret = mmc_regulator_get_ocrmask(mmc->supply.vmmc);
  223. if (ret > 0)
  224. mmc->ocr_avail = ret;
  225. else
  226. dev_warn(dev, "Failed getting OCR mask: %d\n", ret);
  227. }
  228. if (IS_ERR(mmc->supply.vqmmc)) {
  229. if (PTR_ERR(mmc->supply.vqmmc) == -EPROBE_DEFER)
  230. return -EPROBE_DEFER;
  231. dev_dbg(dev, "No vqmmc regulator found\n");
  232. }
  233. return 0;
  234. }
  235. EXPORT_SYMBOL_GPL(mmc_regulator_get_supply);