Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * Helper functions for MMC regulators.
0004  */
0005 
0006 #include <linux/device.h>
0007 #include <linux/err.h>
0008 #include <linux/log2.h>
0009 #include <linux/regulator/consumer.h>
0010 
0011 #include <linux/mmc/host.h>
0012 
0013 #include "core.h"
0014 #include "host.h"
0015 
0016 #ifdef CONFIG_REGULATOR
0017 
0018 /**
0019  * mmc_ocrbitnum_to_vdd - Convert a OCR bit number to its voltage
0020  * @vdd_bit:    OCR bit number
0021  * @min_uV: minimum voltage value (mV)
0022  * @max_uV: maximum voltage value (mV)
0023  *
0024  * This function returns the voltage range according to the provided OCR
0025  * bit number. If conversion is not possible a negative errno value returned.
0026  */
0027 static int mmc_ocrbitnum_to_vdd(int vdd_bit, int *min_uV, int *max_uV)
0028 {
0029     int     tmp;
0030 
0031     if (!vdd_bit)
0032         return -EINVAL;
0033 
0034     /*
0035      * REVISIT mmc_vddrange_to_ocrmask() may have set some
0036      * bits this regulator doesn't quite support ... don't
0037      * be too picky, most cards and regulators are OK with
0038      * a 0.1V range goof (it's a small error percentage).
0039      */
0040     tmp = vdd_bit - ilog2(MMC_VDD_165_195);
0041     if (tmp == 0) {
0042         *min_uV = 1650 * 1000;
0043         *max_uV = 1950 * 1000;
0044     } else {
0045         *min_uV = 1900 * 1000 + tmp * 100 * 1000;
0046         *max_uV = *min_uV + 100 * 1000;
0047     }
0048 
0049     return 0;
0050 }
0051 
0052 /**
0053  * mmc_regulator_get_ocrmask - return mask of supported voltages
0054  * @supply: regulator to use
0055  *
0056  * This returns either a negative errno, or a mask of voltages that
0057  * can be provided to MMC/SD/SDIO devices using the specified voltage
0058  * regulator.  This would normally be called before registering the
0059  * MMC host adapter.
0060  */
0061 static int mmc_regulator_get_ocrmask(struct regulator *supply)
0062 {
0063     int         result = 0;
0064     int         count;
0065     int         i;
0066     int         vdd_uV;
0067     int         vdd_mV;
0068 
0069     count = regulator_count_voltages(supply);
0070     if (count < 0)
0071         return count;
0072 
0073     for (i = 0; i < count; i++) {
0074         vdd_uV = regulator_list_voltage(supply, i);
0075         if (vdd_uV <= 0)
0076             continue;
0077 
0078         vdd_mV = vdd_uV / 1000;
0079         result |= mmc_vddrange_to_ocrmask(vdd_mV, vdd_mV);
0080     }
0081 
0082     if (!result) {
0083         vdd_uV = regulator_get_voltage(supply);
0084         if (vdd_uV <= 0)
0085             return vdd_uV;
0086 
0087         vdd_mV = vdd_uV / 1000;
0088         result = mmc_vddrange_to_ocrmask(vdd_mV, vdd_mV);
0089     }
0090 
0091     return result;
0092 }
0093 
0094 /**
0095  * mmc_regulator_set_ocr - set regulator to match host->ios voltage
0096  * @mmc: the host to regulate
0097  * @supply: regulator to use
0098  * @vdd_bit: zero for power off, else a bit number (host->ios.vdd)
0099  *
0100  * Returns zero on success, else negative errno.
0101  *
0102  * MMC host drivers may use this to enable or disable a regulator using
0103  * a particular supply voltage.  This would normally be called from the
0104  * set_ios() method.
0105  */
0106 int mmc_regulator_set_ocr(struct mmc_host *mmc,
0107             struct regulator *supply,
0108             unsigned short vdd_bit)
0109 {
0110     int         result = 0;
0111     int         min_uV, max_uV;
0112 
0113     if (vdd_bit) {
0114         mmc_ocrbitnum_to_vdd(vdd_bit, &min_uV, &max_uV);
0115 
0116         result = regulator_set_voltage(supply, min_uV, max_uV);
0117         if (result == 0 && !mmc->regulator_enabled) {
0118             result = regulator_enable(supply);
0119             if (!result)
0120                 mmc->regulator_enabled = true;
0121         }
0122     } else if (mmc->regulator_enabled) {
0123         result = regulator_disable(supply);
0124         if (result == 0)
0125             mmc->regulator_enabled = false;
0126     }
0127 
0128     if (result)
0129         dev_err(mmc_dev(mmc),
0130             "could not set regulator OCR (%d)\n", result);
0131     return result;
0132 }
0133 EXPORT_SYMBOL_GPL(mmc_regulator_set_ocr);
0134 
0135 static int mmc_regulator_set_voltage_if_supported(struct regulator *regulator,
0136                           int min_uV, int target_uV,
0137                           int max_uV)
0138 {
0139     int current_uV;
0140 
0141     /*
0142      * Check if supported first to avoid errors since we may try several
0143      * signal levels during power up and don't want to show errors.
0144      */
0145     if (!regulator_is_supported_voltage(regulator, min_uV, max_uV))
0146         return -EINVAL;
0147 
0148     /*
0149      * The voltage is already set, no need to switch.
0150      * Return 1 to indicate that no switch happened.
0151      */
0152     current_uV = regulator_get_voltage(regulator);
0153     if (current_uV == target_uV)
0154         return 1;
0155 
0156     return regulator_set_voltage_triplet(regulator, min_uV, target_uV,
0157                          max_uV);
0158 }
0159 
0160 /**
0161  * mmc_regulator_set_vqmmc - Set VQMMC as per the ios
0162  * @mmc: the host to regulate
0163  * @ios: io bus settings
0164  *
0165  * For 3.3V signaling, we try to match VQMMC to VMMC as closely as possible.
0166  * That will match the behavior of old boards where VQMMC and VMMC were supplied
0167  * by the same supply.  The Bus Operating conditions for 3.3V signaling in the
0168  * SD card spec also define VQMMC in terms of VMMC.
0169  * If this is not possible we'll try the full 2.7-3.6V of the spec.
0170  *
0171  * For 1.2V and 1.8V signaling we'll try to get as close as possible to the
0172  * requested voltage.  This is definitely a good idea for UHS where there's a
0173  * separate regulator on the card that's trying to make 1.8V and it's best if
0174  * we match.
0175  *
0176  * This function is expected to be used by a controller's
0177  * start_signal_voltage_switch() function.
0178  */
0179 int mmc_regulator_set_vqmmc(struct mmc_host *mmc, struct mmc_ios *ios)
0180 {
0181     struct device *dev = mmc_dev(mmc);
0182     int ret, volt, min_uV, max_uV;
0183 
0184     /* If no vqmmc supply then we can't change the voltage */
0185     if (IS_ERR(mmc->supply.vqmmc))
0186         return -EINVAL;
0187 
0188     switch (ios->signal_voltage) {
0189     case MMC_SIGNAL_VOLTAGE_120:
0190         return mmc_regulator_set_voltage_if_supported(mmc->supply.vqmmc,
0191                         1100000, 1200000, 1300000);
0192     case MMC_SIGNAL_VOLTAGE_180:
0193         return mmc_regulator_set_voltage_if_supported(mmc->supply.vqmmc,
0194                         1700000, 1800000, 1950000);
0195     case MMC_SIGNAL_VOLTAGE_330:
0196         ret = mmc_ocrbitnum_to_vdd(mmc->ios.vdd, &volt, &max_uV);
0197         if (ret < 0)
0198             return ret;
0199 
0200         dev_dbg(dev, "%s: found vmmc voltage range of %d-%duV\n",
0201             __func__, volt, max_uV);
0202 
0203         min_uV = max(volt - 300000, 2700000);
0204         max_uV = min(max_uV + 200000, 3600000);
0205 
0206         /*
0207          * Due to a limitation in the current implementation of
0208          * regulator_set_voltage_triplet() which is taking the lowest
0209          * voltage possible if below the target, search for a suitable
0210          * voltage in two steps and try to stay close to vmmc
0211          * with a 0.3V tolerance at first.
0212          */
0213         ret = mmc_regulator_set_voltage_if_supported(mmc->supply.vqmmc,
0214                             min_uV, volt, max_uV);
0215         if (ret >= 0)
0216             return ret;
0217 
0218         return mmc_regulator_set_voltage_if_supported(mmc->supply.vqmmc,
0219                         2700000, volt, 3600000);
0220     default:
0221         return -EINVAL;
0222     }
0223 }
0224 EXPORT_SYMBOL_GPL(mmc_regulator_set_vqmmc);
0225 
0226 #else
0227 
0228 static inline int mmc_regulator_get_ocrmask(struct regulator *supply)
0229 {
0230     return 0;
0231 }
0232 
0233 #endif /* CONFIG_REGULATOR */
0234 
0235 /**
0236  * mmc_regulator_get_supply - try to get VMMC and VQMMC regulators for a host
0237  * @mmc: the host to regulate
0238  *
0239  * Returns 0 or errno. errno should be handled, it is either a critical error
0240  * or -EPROBE_DEFER. 0 means no critical error but it does not mean all
0241  * regulators have been found because they all are optional. If you require
0242  * certain regulators, you need to check separately in your driver if they got
0243  * populated after calling this function.
0244  */
0245 int mmc_regulator_get_supply(struct mmc_host *mmc)
0246 {
0247     struct device *dev = mmc_dev(mmc);
0248     int ret;
0249 
0250     mmc->supply.vmmc = devm_regulator_get_optional(dev, "vmmc");
0251     mmc->supply.vqmmc = devm_regulator_get_optional(dev, "vqmmc");
0252 
0253     if (IS_ERR(mmc->supply.vmmc)) {
0254         if (PTR_ERR(mmc->supply.vmmc) == -EPROBE_DEFER)
0255             return -EPROBE_DEFER;
0256         dev_dbg(dev, "No vmmc regulator found\n");
0257     } else {
0258         ret = mmc_regulator_get_ocrmask(mmc->supply.vmmc);
0259         if (ret > 0)
0260             mmc->ocr_avail = ret;
0261         else
0262             dev_warn(dev, "Failed getting OCR mask: %d\n", ret);
0263     }
0264 
0265     if (IS_ERR(mmc->supply.vqmmc)) {
0266         if (PTR_ERR(mmc->supply.vqmmc) == -EPROBE_DEFER)
0267             return -EPROBE_DEFER;
0268         dev_dbg(dev, "No vqmmc regulator found\n");
0269     }
0270 
0271     return 0;
0272 }
0273 EXPORT_SYMBOL_GPL(mmc_regulator_get_supply);