1 // SPDX-License-Identifier: GPL-2.0
3 * Helper functions for MMC regulators.
6 #include <linux/device.h>
8 #include <linux/log2.h>
9 #include <linux/regulator/consumer.h>
11 #include <linux/mmc/host.h>
16 #ifdef CONFIG_REGULATOR
19 * mmc_ocrbitnum_to_vdd - Convert a OCR bit number to its voltage
20 * @vdd_bit: OCR bit number
21 * @min_uV: minimum voltage value (mV)
22 * @max_uV: maximum voltage value (mV)
24 * This function returns the voltage range according to the provided OCR
25 * bit number. If conversion is not possible a negative errno value returned.
27 static int mmc_ocrbitnum_to_vdd(int vdd_bit
, int *min_uV
, int *max_uV
)
35 * REVISIT mmc_vddrange_to_ocrmask() may have set some
36 * bits this regulator doesn't quite support ... don't
37 * be too picky, most cards and regulators are OK with
38 * a 0.1V range goof (it's a small error percentage).
40 tmp
= vdd_bit
- ilog2(MMC_VDD_165_195
);
42 *min_uV
= 1650 * 1000;
43 *max_uV
= 1950 * 1000;
45 *min_uV
= 1900 * 1000 + tmp
* 100 * 1000;
46 *max_uV
= *min_uV
+ 100 * 1000;
53 * mmc_regulator_get_ocrmask - return mask of supported voltages
54 * @supply: regulator to use
56 * This returns either a negative errno, or a mask of voltages that
57 * can be provided to MMC/SD/SDIO devices using the specified voltage
58 * regulator. This would normally be called before registering the
61 static int mmc_regulator_get_ocrmask(struct regulator
*supply
)
69 count
= regulator_count_voltages(supply
);
73 for (i
= 0; i
< count
; i
++) {
74 vdd_uV
= regulator_list_voltage(supply
, i
);
78 vdd_mV
= vdd_uV
/ 1000;
79 result
|= mmc_vddrange_to_ocrmask(vdd_mV
, vdd_mV
);
83 vdd_uV
= regulator_get_voltage(supply
);
87 vdd_mV
= vdd_uV
/ 1000;
88 result
= mmc_vddrange_to_ocrmask(vdd_mV
, vdd_mV
);
95 * mmc_regulator_set_ocr - set regulator to match host->ios voltage
96 * @mmc: the host to regulate
97 * @supply: regulator to use
98 * @vdd_bit: zero for power off, else a bit number (host->ios.vdd)
100 * Returns zero on success, else negative errno.
102 * MMC host drivers may use this to enable or disable a regulator using
103 * a particular supply voltage. This would normally be called from the
106 int mmc_regulator_set_ocr(struct mmc_host
*mmc
,
107 struct regulator
*supply
,
108 unsigned short vdd_bit
)
114 mmc_ocrbitnum_to_vdd(vdd_bit
, &min_uV
, &max_uV
);
116 result
= regulator_set_voltage(supply
, min_uV
, max_uV
);
117 if (result
== 0 && !mmc
->regulator_enabled
) {
118 result
= regulator_enable(supply
);
120 mmc
->regulator_enabled
= true;
122 } else if (mmc
->regulator_enabled
) {
123 result
= regulator_disable(supply
);
125 mmc
->regulator_enabled
= false;
129 dev_err(mmc_dev(mmc
),
130 "could not set regulator OCR (%d)\n", result
);
133 EXPORT_SYMBOL_GPL(mmc_regulator_set_ocr
);
135 static int mmc_regulator_set_voltage_if_supported(struct regulator
*regulator
,
136 int min_uV
, int target_uV
,
140 * Check if supported first to avoid errors since we may try several
141 * signal levels during power up and don't want to show errors.
143 if (!regulator_is_supported_voltage(regulator
, min_uV
, max_uV
))
146 return regulator_set_voltage_triplet(regulator
, min_uV
, target_uV
,
151 * mmc_regulator_set_vqmmc - Set VQMMC as per the ios
153 * For 3.3V signaling, we try to match VQMMC to VMMC as closely as possible.
154 * That will match the behavior of old boards where VQMMC and VMMC were supplied
155 * by the same supply. The Bus Operating conditions for 3.3V signaling in the
156 * SD card spec also define VQMMC in terms of VMMC.
157 * If this is not possible we'll try the full 2.7-3.6V of the spec.
159 * For 1.2V and 1.8V signaling we'll try to get as close as possible to the
160 * requested voltage. This is definitely a good idea for UHS where there's a
161 * separate regulator on the card that's trying to make 1.8V and it's best if
164 * This function is expected to be used by a controller's
165 * start_signal_voltage_switch() function.
167 int mmc_regulator_set_vqmmc(struct mmc_host
*mmc
, struct mmc_ios
*ios
)
169 struct device
*dev
= mmc_dev(mmc
);
170 int ret
, volt
, min_uV
, max_uV
;
172 /* If no vqmmc supply then we can't change the voltage */
173 if (IS_ERR(mmc
->supply
.vqmmc
))
176 switch (ios
->signal_voltage
) {
177 case MMC_SIGNAL_VOLTAGE_120
:
178 return mmc_regulator_set_voltage_if_supported(mmc
->supply
.vqmmc
,
179 1100000, 1200000, 1300000);
180 case MMC_SIGNAL_VOLTAGE_180
:
181 return mmc_regulator_set_voltage_if_supported(mmc
->supply
.vqmmc
,
182 1700000, 1800000, 1950000);
183 case MMC_SIGNAL_VOLTAGE_330
:
184 ret
= mmc_ocrbitnum_to_vdd(mmc
->ios
.vdd
, &volt
, &max_uV
);
188 dev_dbg(dev
, "%s: found vmmc voltage range of %d-%duV\n",
189 __func__
, volt
, max_uV
);
191 min_uV
= max(volt
- 300000, 2700000);
192 max_uV
= min(max_uV
+ 200000, 3600000);
195 * Due to a limitation in the current implementation of
196 * regulator_set_voltage_triplet() which is taking the lowest
197 * voltage possible if below the target, search for a suitable
198 * voltage in two steps and try to stay close to vmmc
199 * with a 0.3V tolerance at first.
201 if (!mmc_regulator_set_voltage_if_supported(mmc
->supply
.vqmmc
,
202 min_uV
, volt
, max_uV
))
205 return mmc_regulator_set_voltage_if_supported(mmc
->supply
.vqmmc
,
206 2700000, volt
, 3600000);
211 EXPORT_SYMBOL_GPL(mmc_regulator_set_vqmmc
);
215 static inline int mmc_regulator_get_ocrmask(struct regulator
*supply
)
220 #endif /* CONFIG_REGULATOR */
223 * mmc_regulator_get_supply - try to get VMMC and VQMMC regulators for a host
224 * @mmc: the host to regulate
226 * Returns 0 or errno. errno should be handled, it is either a critical error
227 * or -EPROBE_DEFER. 0 means no critical error but it does not mean all
228 * regulators have been found because they all are optional. If you require
229 * certain regulators, you need to check separately in your driver if they got
230 * populated after calling this function.
232 int mmc_regulator_get_supply(struct mmc_host
*mmc
)
234 struct device
*dev
= mmc_dev(mmc
);
237 mmc
->supply
.vmmc
= devm_regulator_get_optional(dev
, "vmmc");
238 mmc
->supply
.vqmmc
= devm_regulator_get_optional(dev
, "vqmmc");
240 if (IS_ERR(mmc
->supply
.vmmc
)) {
241 if (PTR_ERR(mmc
->supply
.vmmc
) == -EPROBE_DEFER
)
242 return -EPROBE_DEFER
;
243 dev_dbg(dev
, "No vmmc regulator found\n");
245 ret
= mmc_regulator_get_ocrmask(mmc
->supply
.vmmc
);
247 mmc
->ocr_avail
= ret
;
249 dev_warn(dev
, "Failed getting OCR mask: %d\n", ret
);
252 if (IS_ERR(mmc
->supply
.vqmmc
)) {
253 if (PTR_ERR(mmc
->supply
.vqmmc
) == -EPROBE_DEFER
)
254 return -EPROBE_DEFER
;
255 dev_dbg(dev
, "No vqmmc regulator found\n");
260 EXPORT_SYMBOL_GPL(mmc_regulator_get_supply
);