0001
0002
0003
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
0020
0021
0022
0023
0024
0025
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
0036
0037
0038
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
0054
0055
0056
0057
0058
0059
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
0096
0097
0098
0099
0100
0101
0102
0103
0104
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
0143
0144
0145 if (!regulator_is_supported_voltage(regulator, min_uV, max_uV))
0146 return -EINVAL;
0147
0148
0149
0150
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
0162
0163
0164
0165
0166
0167
0168
0169
0170
0171
0172
0173
0174
0175
0176
0177
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
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
0208
0209
0210
0211
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
0234
0235
0236
0237
0238
0239
0240
0241
0242
0243
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);