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