Linux Audio

Check our new training course

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