regulator.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  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 (IS_ERR(supply))
  97. return 0;
  98. if (vdd_bit) {
  99. mmc_ocrbitnum_to_vdd(vdd_bit, &min_uV, &max_uV);
  100. result = regulator_set_voltage(supply, min_uV, max_uV);
  101. if (result == 0 && !mmc->regulator_enabled) {
  102. result = regulator_enable(supply);
  103. if (!result)
  104. mmc->regulator_enabled = true;
  105. }
  106. } else if (mmc->regulator_enabled) {
  107. result = regulator_disable(supply);
  108. if (result == 0)
  109. mmc->regulator_enabled = false;
  110. }
  111. if (result)
  112. dev_err(mmc_dev(mmc),
  113. "could not set regulator OCR (%d)\n", result);
  114. return result;
  115. }
  116. EXPORT_SYMBOL_GPL(mmc_regulator_set_ocr);
  117. static int mmc_regulator_set_voltage_if_supported(struct regulator *regulator,
  118. int min_uV, int target_uV,
  119. int max_uV)
  120. {
  121. int current_uV;
  122. /*
  123. * Check if supported first to avoid errors since we may try several
  124. * signal levels during power up and don't want to show errors.
  125. */
  126. if (!regulator_is_supported_voltage(regulator, min_uV, max_uV))
  127. return -EINVAL;
  128. /*
  129. * The voltage is already set, no need to switch.
  130. * Return 1 to indicate that no switch happened.
  131. */
  132. current_uV = regulator_get_voltage(regulator);
  133. if (current_uV == target_uV)
  134. return 1;
  135. return regulator_set_voltage_triplet(regulator, min_uV, target_uV,
  136. max_uV);
  137. }
  138. /**
  139. * mmc_regulator_set_vqmmc - Set VQMMC as per the ios
  140. * @mmc: the host to regulate
  141. * @ios: io bus settings
  142. *
  143. * For 3.3V signaling, we try to match VQMMC to VMMC as closely as possible.
  144. * That will match the behavior of old boards where VQMMC and VMMC were supplied
  145. * by the same supply. The Bus Operating conditions for 3.3V signaling in the
  146. * SD card spec also define VQMMC in terms of VMMC.
  147. * If this is not possible we'll try the full 2.7-3.6V of the spec.
  148. *
  149. * For 1.2V and 1.8V signaling we'll try to get as close as possible to the
  150. * requested voltage. This is definitely a good idea for UHS where there's a
  151. * separate regulator on the card that's trying to make 1.8V and it's best if
  152. * we match.
  153. *
  154. * This function is expected to be used by a controller's
  155. * start_signal_voltage_switch() function.
  156. */
  157. int mmc_regulator_set_vqmmc(struct mmc_host *mmc, struct mmc_ios *ios)
  158. {
  159. struct device *dev = mmc_dev(mmc);
  160. int ret, volt, min_uV, max_uV;
  161. /* If no vqmmc supply then we can't change the voltage */
  162. if (IS_ERR(mmc->supply.vqmmc))
  163. return -EINVAL;
  164. switch (ios->signal_voltage) {
  165. case MMC_SIGNAL_VOLTAGE_120:
  166. return mmc_regulator_set_voltage_if_supported(mmc->supply.vqmmc,
  167. 1100000, 1200000, 1300000);
  168. case MMC_SIGNAL_VOLTAGE_180:
  169. return mmc_regulator_set_voltage_if_supported(mmc->supply.vqmmc,
  170. 1700000, 1800000, 1950000);
  171. case MMC_SIGNAL_VOLTAGE_330:
  172. ret = mmc_ocrbitnum_to_vdd(mmc->ios.vdd, &volt, &max_uV);
  173. if (ret < 0)
  174. return ret;
  175. dev_dbg(dev, "%s: found vmmc voltage range of %d-%duV\n",
  176. __func__, volt, max_uV);
  177. min_uV = max(volt - 300000, 2700000);
  178. max_uV = min(max_uV + 200000, 3600000);
  179. /*
  180. * Due to a limitation in the current implementation of
  181. * regulator_set_voltage_triplet() which is taking the lowest
  182. * voltage possible if below the target, search for a suitable
  183. * voltage in two steps and try to stay close to vmmc
  184. * with a 0.3V tolerance at first.
  185. */
  186. ret = mmc_regulator_set_voltage_if_supported(mmc->supply.vqmmc,
  187. min_uV, volt, max_uV);
  188. if (ret >= 0)
  189. return ret;
  190. return mmc_regulator_set_voltage_if_supported(mmc->supply.vqmmc,
  191. 2700000, volt, 3600000);
  192. default:
  193. return -EINVAL;
  194. }
  195. }
  196. EXPORT_SYMBOL_GPL(mmc_regulator_set_vqmmc);
  197. #else
  198. static inline int mmc_regulator_get_ocrmask(struct regulator *supply)
  199. {
  200. return 0;
  201. }
  202. #endif /* CONFIG_REGULATOR */
  203. /**
  204. * mmc_regulator_get_supply - try to get VMMC and VQMMC regulators for a host
  205. * @mmc: the host to regulate
  206. *
  207. * Returns 0 or errno. errno should be handled, it is either a critical error
  208. * or -EPROBE_DEFER. 0 means no critical error but it does not mean all
  209. * regulators have been found because they all are optional. If you require
  210. * certain regulators, you need to check separately in your driver if they got
  211. * populated after calling this function.
  212. */
  213. int mmc_regulator_get_supply(struct mmc_host *mmc)
  214. {
  215. struct device *dev = mmc_dev(mmc);
  216. int ret;
  217. mmc->supply.vmmc = devm_regulator_get_optional(dev, "vmmc");
  218. mmc->supply.vqmmc = devm_regulator_get_optional(dev, "vqmmc");
  219. if (IS_ERR(mmc->supply.vmmc)) {
  220. if (PTR_ERR(mmc->supply.vmmc) == -EPROBE_DEFER)
  221. return dev_err_probe(dev, -EPROBE_DEFER,
  222. "vmmc regulator not available\n");
  223. dev_dbg(dev, "No vmmc regulator found\n");
  224. } else {
  225. ret = mmc_regulator_get_ocrmask(mmc->supply.vmmc);
  226. if (ret > 0)
  227. mmc->ocr_avail = ret;
  228. else
  229. dev_warn(dev, "Failed getting OCR mask: %d\n", ret);
  230. }
  231. if (IS_ERR(mmc->supply.vqmmc)) {
  232. if (PTR_ERR(mmc->supply.vqmmc) == -EPROBE_DEFER)
  233. return dev_err_probe(dev, -EPROBE_DEFER,
  234. "vqmmc regulator not available\n");
  235. dev_dbg(dev, "No vqmmc regulator found\n");
  236. }
  237. return 0;
  238. }
  239. EXPORT_SYMBOL_GPL(mmc_regulator_get_supply);
  240. /**
  241. * mmc_regulator_enable_vqmmc - enable VQMMC regulator for a host
  242. * @mmc: the host to regulate
  243. *
  244. * Returns 0 or errno. Enables the regulator for vqmmc.
  245. * Keeps track of the enable status for ensuring that calls to
  246. * regulator_enable/disable are balanced.
  247. */
  248. int mmc_regulator_enable_vqmmc(struct mmc_host *mmc)
  249. {
  250. int ret = 0;
  251. if (!IS_ERR(mmc->supply.vqmmc) && !mmc->vqmmc_enabled) {
  252. ret = regulator_enable(mmc->supply.vqmmc);
  253. if (ret < 0)
  254. dev_err(mmc_dev(mmc), "enabling vqmmc regulator failed\n");
  255. else
  256. mmc->vqmmc_enabled = true;
  257. }
  258. return ret;
  259. }
  260. EXPORT_SYMBOL_GPL(mmc_regulator_enable_vqmmc);
  261. /**
  262. * mmc_regulator_disable_vqmmc - disable VQMMC regulator for a host
  263. * @mmc: the host to regulate
  264. *
  265. * Returns 0 or errno. Disables the regulator for vqmmc.
  266. * Keeps track of the enable status for ensuring that calls to
  267. * regulator_enable/disable are balanced.
  268. */
  269. void mmc_regulator_disable_vqmmc(struct mmc_host *mmc)
  270. {
  271. if (!IS_ERR(mmc->supply.vqmmc) && mmc->vqmmc_enabled) {
  272. regulator_disable(mmc->supply.vqmmc);
  273. mmc->vqmmc_enabled = false;
  274. }
  275. }
  276. EXPORT_SYMBOL_GPL(mmc_regulator_disable_vqmmc);