regulator.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  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/workqueue.h>
  10. #include <linux/mmc/host.h>
  11. #include "core.h"
  12. #include "host.h"
  13. #ifdef CONFIG_REGULATOR
  14. /**
  15. * mmc_ocrbitnum_to_vdd - Convert a OCR bit number to its voltage
  16. * @vdd_bit: OCR bit number
  17. * @min_uV: minimum voltage value (mV)
  18. * @max_uV: maximum voltage value (mV)
  19. *
  20. * This function returns the voltage range according to the provided OCR
  21. * bit number. If conversion is not possible a negative errno value returned.
  22. */
  23. static int mmc_ocrbitnum_to_vdd(int vdd_bit, int *min_uV, int *max_uV)
  24. {
  25. int tmp;
  26. if (!vdd_bit)
  27. return -EINVAL;
  28. /*
  29. * REVISIT mmc_vddrange_to_ocrmask() may have set some
  30. * bits this regulator doesn't quite support ... don't
  31. * be too picky, most cards and regulators are OK with
  32. * a 0.1V range goof (it's a small error percentage).
  33. */
  34. tmp = vdd_bit - ilog2(MMC_VDD_165_195);
  35. if (tmp == 0) {
  36. *min_uV = 1650 * 1000;
  37. *max_uV = 1950 * 1000;
  38. } else {
  39. *min_uV = 1900 * 1000 + tmp * 100 * 1000;
  40. *max_uV = *min_uV + 100 * 1000;
  41. }
  42. return 0;
  43. }
  44. /**
  45. * mmc_regulator_get_ocrmask - return mask of supported voltages
  46. * @supply: regulator to use
  47. *
  48. * This returns either a negative errno, or a mask of voltages that
  49. * can be provided to MMC/SD/SDIO devices using the specified voltage
  50. * regulator. This would normally be called before registering the
  51. * MMC host adapter.
  52. */
  53. static int mmc_regulator_get_ocrmask(struct regulator *supply)
  54. {
  55. int result = 0;
  56. int count;
  57. int i;
  58. int vdd_uV;
  59. int vdd_mV;
  60. count = regulator_count_voltages(supply);
  61. if (count < 0)
  62. return count;
  63. for (i = 0; i < count; i++) {
  64. vdd_uV = regulator_list_voltage(supply, i);
  65. if (vdd_uV <= 0)
  66. continue;
  67. vdd_mV = vdd_uV / 1000;
  68. result |= mmc_vddrange_to_ocrmask(vdd_mV, vdd_mV);
  69. }
  70. if (!result) {
  71. vdd_uV = regulator_get_voltage(supply);
  72. if (vdd_uV <= 0)
  73. return vdd_uV;
  74. vdd_mV = vdd_uV / 1000;
  75. result = mmc_vddrange_to_ocrmask(vdd_mV, vdd_mV);
  76. }
  77. return result;
  78. }
  79. /**
  80. * mmc_regulator_set_ocr - set regulator to match host->ios voltage
  81. * @mmc: the host to regulate
  82. * @supply: regulator to use
  83. * @vdd_bit: zero for power off, else a bit number (host->ios.vdd)
  84. *
  85. * Returns zero on success, else negative errno.
  86. *
  87. * MMC host drivers may use this to enable or disable a regulator using
  88. * a particular supply voltage. This would normally be called from the
  89. * set_ios() method.
  90. */
  91. int mmc_regulator_set_ocr(struct mmc_host *mmc,
  92. struct regulator *supply,
  93. unsigned short vdd_bit)
  94. {
  95. int result = 0;
  96. int min_uV, max_uV;
  97. if (IS_ERR(supply))
  98. return 0;
  99. if (vdd_bit) {
  100. mmc_ocrbitnum_to_vdd(vdd_bit, &min_uV, &max_uV);
  101. result = regulator_set_voltage(supply, min_uV, max_uV);
  102. if (result == 0 && !mmc->regulator_enabled) {
  103. result = regulator_enable(supply);
  104. if (!result)
  105. mmc->regulator_enabled = true;
  106. }
  107. } else if (mmc->regulator_enabled) {
  108. result = regulator_disable(supply);
  109. if (result == 0)
  110. mmc->regulator_enabled = false;
  111. }
  112. if (result)
  113. dev_err(mmc_dev(mmc),
  114. "could not set regulator OCR (%d)\n", result);
  115. return result;
  116. }
  117. EXPORT_SYMBOL_GPL(mmc_regulator_set_ocr);
  118. static int mmc_regulator_set_voltage_if_supported(struct regulator *regulator,
  119. int min_uV, int target_uV,
  120. int max_uV)
  121. {
  122. int current_uV;
  123. /*
  124. * Check if supported first to avoid errors since we may try several
  125. * signal levels during power up and don't want to show errors.
  126. */
  127. if (!regulator_is_supported_voltage(regulator, min_uV, max_uV))
  128. return -EINVAL;
  129. /*
  130. * The voltage is already set, no need to switch.
  131. * Return 1 to indicate that no switch happened.
  132. */
  133. current_uV = regulator_get_voltage(regulator);
  134. if (current_uV == target_uV)
  135. return 1;
  136. return regulator_set_voltage_triplet(regulator, min_uV, target_uV,
  137. max_uV);
  138. }
  139. /**
  140. * mmc_regulator_set_vqmmc - Set VQMMC as per the ios
  141. * @mmc: the host to regulate
  142. * @ios: io bus settings
  143. *
  144. * For 3.3V signaling, we try to match VQMMC to VMMC as closely as possible.
  145. * That will match the behavior of old boards where VQMMC and VMMC were supplied
  146. * by the same supply. The Bus Operating conditions for 3.3V signaling in the
  147. * SD card spec also define VQMMC in terms of VMMC.
  148. * If this is not possible we'll try the full 2.7-3.6V of the spec.
  149. *
  150. * For 1.2V and 1.8V signaling we'll try to get as close as possible to the
  151. * requested voltage. This is definitely a good idea for UHS where there's a
  152. * separate regulator on the card that's trying to make 1.8V and it's best if
  153. * we match.
  154. *
  155. * This function is expected to be used by a controller's
  156. * start_signal_voltage_switch() function.
  157. */
  158. int mmc_regulator_set_vqmmc(struct mmc_host *mmc, struct mmc_ios *ios)
  159. {
  160. struct device *dev = mmc_dev(mmc);
  161. int ret, volt, min_uV, max_uV;
  162. /* If no vqmmc supply then we can't change the voltage */
  163. if (IS_ERR(mmc->supply.vqmmc))
  164. return -EINVAL;
  165. switch (ios->signal_voltage) {
  166. case MMC_SIGNAL_VOLTAGE_120:
  167. return mmc_regulator_set_voltage_if_supported(mmc->supply.vqmmc,
  168. 1100000, 1200000, 1300000);
  169. case MMC_SIGNAL_VOLTAGE_180:
  170. return mmc_regulator_set_voltage_if_supported(mmc->supply.vqmmc,
  171. 1700000, 1800000, 1950000);
  172. case MMC_SIGNAL_VOLTAGE_330:
  173. ret = mmc_ocrbitnum_to_vdd(mmc->ios.vdd, &volt, &max_uV);
  174. if (ret < 0)
  175. return ret;
  176. dev_dbg(dev, "%s: found vmmc voltage range of %d-%duV\n",
  177. __func__, volt, max_uV);
  178. min_uV = max(volt - 300000, 2700000);
  179. max_uV = min(max_uV + 200000, 3600000);
  180. /*
  181. * Due to a limitation in the current implementation of
  182. * regulator_set_voltage_triplet() which is taking the lowest
  183. * voltage possible if below the target, search for a suitable
  184. * voltage in two steps and try to stay close to vmmc
  185. * with a 0.3V tolerance at first.
  186. */
  187. ret = mmc_regulator_set_voltage_if_supported(mmc->supply.vqmmc,
  188. min_uV, volt, max_uV);
  189. if (ret >= 0)
  190. return ret;
  191. return mmc_regulator_set_voltage_if_supported(mmc->supply.vqmmc,
  192. 2700000, volt, 3600000);
  193. default:
  194. return -EINVAL;
  195. }
  196. }
  197. EXPORT_SYMBOL_GPL(mmc_regulator_set_vqmmc);
  198. /**
  199. * mmc_regulator_set_vqmmc2 - Set vqmmc2 as per the ios->vqmmc2_voltage
  200. * @mmc: The mmc host to regulate
  201. * @ios: The io bus settings
  202. *
  203. * Sets a new voltage level for the vqmmc2 regulator, which may correspond to
  204. * the vdd2 regulator for an SD UHS-II interface. This function is expected to
  205. * be called by mmc host drivers.
  206. *
  207. * Returns a negative error code on failure, zero if the voltage level was
  208. * changed successfully or a positive value if the level didn't need to change.
  209. */
  210. int mmc_regulator_set_vqmmc2(struct mmc_host *mmc, struct mmc_ios *ios)
  211. {
  212. if (IS_ERR(mmc->supply.vqmmc2))
  213. return -EINVAL;
  214. switch (ios->vqmmc2_voltage) {
  215. case MMC_VQMMC2_VOLTAGE_180:
  216. return mmc_regulator_set_voltage_if_supported(
  217. mmc->supply.vqmmc2, 1700000, 1800000, 1950000);
  218. default:
  219. return -EINVAL;
  220. }
  221. }
  222. EXPORT_SYMBOL_GPL(mmc_regulator_set_vqmmc2);
  223. #else
  224. static inline int mmc_regulator_get_ocrmask(struct regulator *supply)
  225. {
  226. return 0;
  227. }
  228. #endif /* CONFIG_REGULATOR */
  229. /* To be called from a high-priority workqueue */
  230. void mmc_undervoltage_workfn(struct work_struct *work)
  231. {
  232. struct mmc_supply *supply;
  233. struct mmc_host *host;
  234. supply = container_of(work, struct mmc_supply, uv_work);
  235. host = container_of(supply, struct mmc_host, supply);
  236. mmc_handle_undervoltage(host);
  237. }
  238. static int mmc_handle_regulator_event(struct notifier_block *nb,
  239. unsigned long event, void *data)
  240. {
  241. struct mmc_supply *supply = container_of(nb, struct mmc_supply,
  242. vmmc_nb);
  243. struct mmc_host *host = container_of(supply, struct mmc_host, supply);
  244. unsigned long flags;
  245. switch (event) {
  246. case REGULATOR_EVENT_UNDER_VOLTAGE:
  247. spin_lock_irqsave(&host->lock, flags);
  248. if (host->undervoltage) {
  249. spin_unlock_irqrestore(&host->lock, flags);
  250. return NOTIFY_OK;
  251. }
  252. host->undervoltage = true;
  253. spin_unlock_irqrestore(&host->lock, flags);
  254. queue_work(system_highpri_wq, &host->supply.uv_work);
  255. break;
  256. default:
  257. return NOTIFY_DONE;
  258. }
  259. return NOTIFY_OK;
  260. }
  261. /**
  262. * mmc_regulator_register_undervoltage_notifier - Register for undervoltage
  263. * events
  264. * @host: MMC host
  265. *
  266. * To be called by a bus driver when a card supporting graceful shutdown
  267. * is attached.
  268. */
  269. void mmc_regulator_register_undervoltage_notifier(struct mmc_host *host)
  270. {
  271. int ret;
  272. if (IS_ERR_OR_NULL(host->supply.vmmc))
  273. return;
  274. host->supply.vmmc_nb.notifier_call = mmc_handle_regulator_event;
  275. ret = regulator_register_notifier(host->supply.vmmc,
  276. &host->supply.vmmc_nb);
  277. if (ret)
  278. dev_warn(mmc_dev(host), "Failed to register vmmc notifier: %d\n", ret);
  279. }
  280. /**
  281. * mmc_regulator_unregister_undervoltage_notifier - Unregister undervoltage
  282. * notifier
  283. * @host: MMC host
  284. */
  285. void mmc_regulator_unregister_undervoltage_notifier(struct mmc_host *host)
  286. {
  287. if (IS_ERR_OR_NULL(host->supply.vmmc))
  288. return;
  289. regulator_unregister_notifier(host->supply.vmmc, &host->supply.vmmc_nb);
  290. cancel_work_sync(&host->supply.uv_work);
  291. }
  292. /**
  293. * mmc_regulator_get_supply - try to get VMMC and VQMMC regulators for a host
  294. * @mmc: the host to regulate
  295. *
  296. * Returns 0 or errno. errno should be handled, it is either a critical error
  297. * or -EPROBE_DEFER. 0 means no critical error but it does not mean all
  298. * regulators have been found because they all are optional. If you require
  299. * certain regulators, you need to check separately in your driver if they got
  300. * populated after calling this function.
  301. */
  302. int mmc_regulator_get_supply(struct mmc_host *mmc)
  303. {
  304. struct device *dev = mmc_dev(mmc);
  305. int ret;
  306. mmc->supply.vmmc = devm_regulator_get_optional(dev, "vmmc");
  307. mmc->supply.vqmmc = devm_regulator_get_optional(dev, "vqmmc");
  308. mmc->supply.vqmmc2 = devm_regulator_get_optional(dev, "vqmmc2");
  309. if (IS_ERR(mmc->supply.vmmc)) {
  310. if (PTR_ERR(mmc->supply.vmmc) == -EPROBE_DEFER)
  311. return dev_err_probe(dev, -EPROBE_DEFER,
  312. "vmmc regulator not available\n");
  313. dev_dbg(dev, "No vmmc regulator found\n");
  314. } else {
  315. ret = mmc_regulator_get_ocrmask(mmc->supply.vmmc);
  316. if (ret > 0)
  317. mmc->ocr_avail = ret;
  318. else
  319. dev_warn(dev, "Failed getting OCR mask: %d\n", ret);
  320. }
  321. if (IS_ERR(mmc->supply.vqmmc)) {
  322. if (PTR_ERR(mmc->supply.vqmmc) == -EPROBE_DEFER)
  323. return dev_err_probe(dev, -EPROBE_DEFER,
  324. "vqmmc regulator not available\n");
  325. dev_dbg(dev, "No vqmmc regulator found\n");
  326. }
  327. if (IS_ERR(mmc->supply.vqmmc2)) {
  328. if (PTR_ERR(mmc->supply.vqmmc2) == -EPROBE_DEFER)
  329. return -EPROBE_DEFER;
  330. dev_dbg(dev, "No vqmmc2 regulator found\n");
  331. }
  332. return 0;
  333. }
  334. EXPORT_SYMBOL_GPL(mmc_regulator_get_supply);
  335. /**
  336. * mmc_regulator_enable_vqmmc - enable VQMMC regulator for a host
  337. * @mmc: the host to regulate
  338. *
  339. * Returns 0 or errno. Enables the regulator for vqmmc.
  340. * Keeps track of the enable status for ensuring that calls to
  341. * regulator_enable/disable are balanced.
  342. */
  343. int mmc_regulator_enable_vqmmc(struct mmc_host *mmc)
  344. {
  345. int ret = 0;
  346. if (!IS_ERR(mmc->supply.vqmmc) && !mmc->vqmmc_enabled) {
  347. ret = regulator_enable(mmc->supply.vqmmc);
  348. if (ret < 0)
  349. dev_err(mmc_dev(mmc), "enabling vqmmc regulator failed\n");
  350. else
  351. mmc->vqmmc_enabled = true;
  352. }
  353. return ret;
  354. }
  355. EXPORT_SYMBOL_GPL(mmc_regulator_enable_vqmmc);
  356. /**
  357. * mmc_regulator_disable_vqmmc - disable VQMMC regulator for a host
  358. * @mmc: the host to regulate
  359. *
  360. * Returns 0 or errno. Disables the regulator for vqmmc.
  361. * Keeps track of the enable status for ensuring that calls to
  362. * regulator_enable/disable are balanced.
  363. */
  364. void mmc_regulator_disable_vqmmc(struct mmc_host *mmc)
  365. {
  366. if (!IS_ERR(mmc->supply.vqmmc) && mmc->vqmmc_enabled) {
  367. regulator_disable(mmc->supply.vqmmc);
  368. mmc->vqmmc_enabled = false;
  369. }
  370. }
  371. EXPORT_SYMBOL_GPL(mmc_regulator_disable_vqmmc);