1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * helpers.c -- Voltage/Current Regulator framework helper functions. 4 * 5 * Copyright 2007, 2008 Wolfson Microelectronics PLC. 6 * Copyright 2008 SlimLogic Ltd. 7 */ 8 9 #include <linux/kernel.h> 10 #include <linux/err.h> 11 #include <linux/delay.h> 12 #include <linux/regmap.h> 13 #include <linux/regulator/consumer.h> 14 #include <linux/regulator/driver.h> 15 #include <linux/module.h> 16 17 /** 18 * regulator_is_enabled_regmap - standard is_enabled() for regmap users 19 * 20 * @rdev: regulator to operate on 21 * 22 * Regulators that use regmap for their register I/O can set the 23 * enable_reg and enable_mask fields in their descriptor and then use 24 * this as their is_enabled operation, saving some code. 25 */ 26 int regulator_is_enabled_regmap(struct regulator_dev *rdev) 27 { 28 unsigned int val; 29 int ret; 30 31 ret = regmap_read(rdev->regmap, rdev->desc->enable_reg, &val); 32 if (ret != 0) 33 return ret; 34 35 val &= rdev->desc->enable_mask; 36 37 if (rdev->desc->enable_is_inverted) { 38 if (rdev->desc->enable_val) 39 return val != rdev->desc->enable_val; 40 return val == 0; 41 } else { 42 if (rdev->desc->enable_val) 43 return val == rdev->desc->enable_val; 44 return val != 0; 45 } 46 } 47 EXPORT_SYMBOL_GPL(regulator_is_enabled_regmap); 48 49 /** 50 * regulator_enable_regmap - standard enable() for regmap users 51 * 52 * @rdev: regulator to operate on 53 * 54 * Regulators that use regmap for their register I/O can set the 55 * enable_reg and enable_mask fields in their descriptor and then use 56 * this as their enable() operation, saving some code. 57 */ 58 int regulator_enable_regmap(struct regulator_dev *rdev) 59 { 60 unsigned int val; 61 62 if (rdev->desc->enable_is_inverted) { 63 val = rdev->desc->disable_val; 64 } else { 65 val = rdev->desc->enable_val; 66 if (!val) 67 val = rdev->desc->enable_mask; 68 } 69 70 return regmap_update_bits(rdev->regmap, rdev->desc->enable_reg, 71 rdev->desc->enable_mask, val); 72 } 73 EXPORT_SYMBOL_GPL(regulator_enable_regmap); 74 75 /** 76 * regulator_disable_regmap - standard disable() for regmap users 77 * 78 * @rdev: regulator to operate on 79 * 80 * Regulators that use regmap for their register I/O can set the 81 * enable_reg and enable_mask fields in their descriptor and then use 82 * this as their disable() operation, saving some code. 83 */ 84 int regulator_disable_regmap(struct regulator_dev *rdev) 85 { 86 unsigned int val; 87 88 if (rdev->desc->enable_is_inverted) { 89 val = rdev->desc->enable_val; 90 if (!val) 91 val = rdev->desc->enable_mask; 92 } else { 93 val = rdev->desc->disable_val; 94 } 95 96 return regmap_update_bits(rdev->regmap, rdev->desc->enable_reg, 97 rdev->desc->enable_mask, val); 98 } 99 EXPORT_SYMBOL_GPL(regulator_disable_regmap); 100 101 static int regulator_range_selector_to_index(struct regulator_dev *rdev, 102 unsigned int rval) 103 { 104 int i; 105 106 if (!rdev->desc->linear_range_selectors) 107 return -EINVAL; 108 109 rval &= rdev->desc->vsel_range_mask; 110 111 for (i = 0; i < rdev->desc->n_linear_ranges; i++) { 112 if (rdev->desc->linear_range_selectors[i] == rval) 113 return i; 114 } 115 return -EINVAL; 116 } 117 118 /** 119 * regulator_get_voltage_sel_pickable_regmap - pickable range get_voltage_sel 120 * 121 * @rdev: regulator to operate on 122 * 123 * Regulators that use regmap for their register I/O and use pickable 124 * ranges can set the vsel_reg, vsel_mask, vsel_range_reg and vsel_range_mask 125 * fields in their descriptor and then use this as their get_voltage_vsel 126 * operation, saving some code. 127 */ 128 int regulator_get_voltage_sel_pickable_regmap(struct regulator_dev *rdev) 129 { 130 unsigned int r_val; 131 int range; 132 unsigned int val; 133 int ret, i; 134 unsigned int voltages_in_range = 0; 135 136 if (!rdev->desc->linear_ranges) 137 return -EINVAL; 138 139 ret = regmap_read(rdev->regmap, rdev->desc->vsel_reg, &val); 140 if (ret != 0) 141 return ret; 142 143 ret = regmap_read(rdev->regmap, rdev->desc->vsel_range_reg, &r_val); 144 if (ret != 0) 145 return ret; 146 147 val &= rdev->desc->vsel_mask; 148 val >>= ffs(rdev->desc->vsel_mask) - 1; 149 150 range = regulator_range_selector_to_index(rdev, r_val); 151 if (range < 0) 152 return -EINVAL; 153 154 for (i = 0; i < range; i++) 155 voltages_in_range += (rdev->desc->linear_ranges[i].max_sel - 156 rdev->desc->linear_ranges[i].min_sel) + 1; 157 158 return val + voltages_in_range; 159 } 160 EXPORT_SYMBOL_GPL(regulator_get_voltage_sel_pickable_regmap); 161 162 /** 163 * regulator_set_voltage_sel_pickable_regmap - pickable range set_voltage_sel 164 * 165 * @rdev: regulator to operate on 166 * @sel: Selector to set 167 * 168 * Regulators that use regmap for their register I/O and use pickable 169 * ranges can set the vsel_reg, vsel_mask, vsel_range_reg and vsel_range_mask 170 * fields in their descriptor and then use this as their set_voltage_vsel 171 * operation, saving some code. 172 */ 173 int regulator_set_voltage_sel_pickable_regmap(struct regulator_dev *rdev, 174 unsigned int sel) 175 { 176 unsigned int range; 177 int ret, i; 178 unsigned int voltages_in_range = 0; 179 180 for (i = 0; i < rdev->desc->n_linear_ranges; i++) { 181 voltages_in_range = (rdev->desc->linear_ranges[i].max_sel - 182 rdev->desc->linear_ranges[i].min_sel) + 1; 183 if (sel < voltages_in_range) 184 break; 185 sel -= voltages_in_range; 186 } 187 188 if (i == rdev->desc->n_linear_ranges) 189 return -EINVAL; 190 191 sel <<= ffs(rdev->desc->vsel_mask) - 1; 192 sel += rdev->desc->linear_ranges[i].min_sel; 193 194 range = rdev->desc->linear_range_selectors[i]; 195 196 if (rdev->desc->vsel_reg == rdev->desc->vsel_range_reg) { 197 ret = regmap_update_bits(rdev->regmap, 198 rdev->desc->vsel_reg, 199 rdev->desc->vsel_range_mask | 200 rdev->desc->vsel_mask, sel | range); 201 } else { 202 ret = regmap_update_bits(rdev->regmap, 203 rdev->desc->vsel_range_reg, 204 rdev->desc->vsel_range_mask, range); 205 if (ret) 206 return ret; 207 208 ret = regmap_update_bits(rdev->regmap, rdev->desc->vsel_reg, 209 rdev->desc->vsel_mask, sel); 210 } 211 212 if (ret) 213 return ret; 214 215 if (rdev->desc->apply_bit) 216 ret = regmap_update_bits(rdev->regmap, rdev->desc->apply_reg, 217 rdev->desc->apply_bit, 218 rdev->desc->apply_bit); 219 return ret; 220 } 221 EXPORT_SYMBOL_GPL(regulator_set_voltage_sel_pickable_regmap); 222 223 /** 224 * regulator_get_voltage_sel_regmap - standard get_voltage_sel for regmap users 225 * 226 * @rdev: regulator to operate on 227 * 228 * Regulators that use regmap for their register I/O can set the 229 * vsel_reg and vsel_mask fields in their descriptor and then use this 230 * as their get_voltage_vsel operation, saving some code. 231 */ 232 int regulator_get_voltage_sel_regmap(struct regulator_dev *rdev) 233 { 234 unsigned int val; 235 int ret; 236 237 ret = regmap_read(rdev->regmap, rdev->desc->vsel_reg, &val); 238 if (ret != 0) 239 return ret; 240 241 val &= rdev->desc->vsel_mask; 242 val >>= ffs(rdev->desc->vsel_mask) - 1; 243 244 return val; 245 } 246 EXPORT_SYMBOL_GPL(regulator_get_voltage_sel_regmap); 247 248 /** 249 * regulator_set_voltage_sel_regmap - standard set_voltage_sel for regmap users 250 * 251 * @rdev: regulator to operate on 252 * @sel: Selector to set 253 * 254 * Regulators that use regmap for their register I/O can set the 255 * vsel_reg and vsel_mask fields in their descriptor and then use this 256 * as their set_voltage_vsel operation, saving some code. 257 */ 258 int regulator_set_voltage_sel_regmap(struct regulator_dev *rdev, unsigned sel) 259 { 260 int ret; 261 262 sel <<= ffs(rdev->desc->vsel_mask) - 1; 263 264 ret = regmap_update_bits(rdev->regmap, rdev->desc->vsel_reg, 265 rdev->desc->vsel_mask, sel); 266 if (ret) 267 return ret; 268 269 if (rdev->desc->apply_bit) 270 ret = regmap_update_bits(rdev->regmap, rdev->desc->apply_reg, 271 rdev->desc->apply_bit, 272 rdev->desc->apply_bit); 273 return ret; 274 } 275 EXPORT_SYMBOL_GPL(regulator_set_voltage_sel_regmap); 276 277 /** 278 * regulator_map_voltage_iterate - map_voltage() based on list_voltage() 279 * 280 * @rdev: Regulator to operate on 281 * @min_uV: Lower bound for voltage 282 * @max_uV: Upper bound for voltage 283 * 284 * Drivers implementing set_voltage_sel() and list_voltage() can use 285 * this as their map_voltage() operation. It will find a suitable 286 * voltage by calling list_voltage() until it gets something in bounds 287 * for the requested voltages. 288 */ 289 int regulator_map_voltage_iterate(struct regulator_dev *rdev, 290 int min_uV, int max_uV) 291 { 292 int best_val = INT_MAX; 293 int selector = 0; 294 int i, ret; 295 296 /* Find the smallest voltage that falls within the specified 297 * range. 298 */ 299 for (i = 0; i < rdev->desc->n_voltages; i++) { 300 ret = rdev->desc->ops->list_voltage(rdev, i); 301 if (ret < 0) 302 continue; 303 304 if (ret < best_val && ret >= min_uV && ret <= max_uV) { 305 best_val = ret; 306 selector = i; 307 } 308 } 309 310 if (best_val != INT_MAX) 311 return selector; 312 else 313 return -EINVAL; 314 } 315 EXPORT_SYMBOL_GPL(regulator_map_voltage_iterate); 316 317 /** 318 * regulator_map_voltage_ascend - map_voltage() for ascendant voltage list 319 * 320 * @rdev: Regulator to operate on 321 * @min_uV: Lower bound for voltage 322 * @max_uV: Upper bound for voltage 323 * 324 * Drivers that have ascendant voltage list can use this as their 325 * map_voltage() operation. 326 */ 327 int regulator_map_voltage_ascend(struct regulator_dev *rdev, 328 int min_uV, int max_uV) 329 { 330 int i, ret; 331 332 for (i = 0; i < rdev->desc->n_voltages; i++) { 333 ret = rdev->desc->ops->list_voltage(rdev, i); 334 if (ret < 0) 335 continue; 336 337 if (ret > max_uV) 338 break; 339 340 if (ret >= min_uV && ret <= max_uV) 341 return i; 342 } 343 344 return -EINVAL; 345 } 346 EXPORT_SYMBOL_GPL(regulator_map_voltage_ascend); 347 348 /** 349 * regulator_map_voltage_linear - map_voltage() for simple linear mappings 350 * 351 * @rdev: Regulator to operate on 352 * @min_uV: Lower bound for voltage 353 * @max_uV: Upper bound for voltage 354 * 355 * Drivers providing min_uV and uV_step in their regulator_desc can 356 * use this as their map_voltage() operation. 357 */ 358 int regulator_map_voltage_linear(struct regulator_dev *rdev, 359 int min_uV, int max_uV) 360 { 361 int ret, voltage; 362 363 /* Allow uV_step to be 0 for fixed voltage */ 364 if (rdev->desc->n_voltages == 1 && rdev->desc->uV_step == 0) { 365 if (min_uV <= rdev->desc->min_uV && rdev->desc->min_uV <= max_uV) 366 return 0; 367 else 368 return -EINVAL; 369 } 370 371 if (!rdev->desc->uV_step) { 372 BUG_ON(!rdev->desc->uV_step); 373 return -EINVAL; 374 } 375 376 if (min_uV < rdev->desc->min_uV) 377 min_uV = rdev->desc->min_uV; 378 379 ret = DIV_ROUND_UP(min_uV - rdev->desc->min_uV, rdev->desc->uV_step); 380 if (ret < 0) 381 return ret; 382 383 ret += rdev->desc->linear_min_sel; 384 385 /* Map back into a voltage to verify we're still in bounds */ 386 voltage = rdev->desc->ops->list_voltage(rdev, ret); 387 if (voltage < min_uV || voltage > max_uV) 388 return -EINVAL; 389 390 return ret; 391 } 392 EXPORT_SYMBOL_GPL(regulator_map_voltage_linear); 393 394 /** 395 * regulator_map_voltage_linear_range - map_voltage() for multiple linear ranges 396 * 397 * @rdev: Regulator to operate on 398 * @min_uV: Lower bound for voltage 399 * @max_uV: Upper bound for voltage 400 * 401 * Drivers providing linear_ranges in their descriptor can use this as 402 * their map_voltage() callback. 403 */ 404 int regulator_map_voltage_linear_range(struct regulator_dev *rdev, 405 int min_uV, int max_uV) 406 { 407 const struct regulator_linear_range *range; 408 int ret = -EINVAL; 409 int voltage, i; 410 411 if (!rdev->desc->n_linear_ranges) { 412 BUG_ON(!rdev->desc->n_linear_ranges); 413 return -EINVAL; 414 } 415 416 for (i = 0; i < rdev->desc->n_linear_ranges; i++) { 417 int linear_max_uV; 418 419 range = &rdev->desc->linear_ranges[i]; 420 linear_max_uV = range->min_uV + 421 (range->max_sel - range->min_sel) * range->uV_step; 422 423 if (!(min_uV <= linear_max_uV && max_uV >= range->min_uV)) 424 continue; 425 426 if (min_uV <= range->min_uV) 427 min_uV = range->min_uV; 428 429 /* range->uV_step == 0 means fixed voltage range */ 430 if (range->uV_step == 0) { 431 ret = 0; 432 } else { 433 ret = DIV_ROUND_UP(min_uV - range->min_uV, 434 range->uV_step); 435 if (ret < 0) 436 return ret; 437 } 438 439 ret += range->min_sel; 440 441 /* 442 * Map back into a voltage to verify we're still in bounds. 443 * If we are not, then continue checking rest of the ranges. 444 */ 445 voltage = rdev->desc->ops->list_voltage(rdev, ret); 446 if (voltage >= min_uV && voltage <= max_uV) 447 break; 448 } 449 450 if (i == rdev->desc->n_linear_ranges) 451 return -EINVAL; 452 453 return ret; 454 } 455 EXPORT_SYMBOL_GPL(regulator_map_voltage_linear_range); 456 457 /** 458 * regulator_map_voltage_pickable_linear_range - map_voltage, pickable ranges 459 * 460 * @rdev: Regulator to operate on 461 * @min_uV: Lower bound for voltage 462 * @max_uV: Upper bound for voltage 463 * 464 * Drivers providing pickable linear_ranges in their descriptor can use 465 * this as their map_voltage() callback. 466 */ 467 int regulator_map_voltage_pickable_linear_range(struct regulator_dev *rdev, 468 int min_uV, int max_uV) 469 { 470 const struct regulator_linear_range *range; 471 int ret = -EINVAL; 472 int voltage, i; 473 unsigned int selector = 0; 474 475 if (!rdev->desc->n_linear_ranges) { 476 BUG_ON(!rdev->desc->n_linear_ranges); 477 return -EINVAL; 478 } 479 480 for (i = 0; i < rdev->desc->n_linear_ranges; i++) { 481 int linear_max_uV; 482 483 range = &rdev->desc->linear_ranges[i]; 484 linear_max_uV = range->min_uV + 485 (range->max_sel - range->min_sel) * range->uV_step; 486 487 if (!(min_uV <= linear_max_uV && max_uV >= range->min_uV)) { 488 selector += (range->max_sel - range->min_sel + 1); 489 continue; 490 } 491 492 if (min_uV <= range->min_uV) 493 min_uV = range->min_uV; 494 495 /* range->uV_step == 0 means fixed voltage range */ 496 if (range->uV_step == 0) { 497 ret = 0; 498 } else { 499 ret = DIV_ROUND_UP(min_uV - range->min_uV, 500 range->uV_step); 501 if (ret < 0) 502 return ret; 503 } 504 505 ret += selector; 506 507 voltage = rdev->desc->ops->list_voltage(rdev, ret); 508 509 /* 510 * Map back into a voltage to verify we're still in bounds. 511 * We may have overlapping voltage ranges. Hence we don't 512 * exit but retry until we have checked all ranges. 513 */ 514 if (voltage < min_uV || voltage > max_uV) 515 selector += (range->max_sel - range->min_sel + 1); 516 else 517 break; 518 } 519 520 if (i == rdev->desc->n_linear_ranges) 521 return -EINVAL; 522 523 return ret; 524 } 525 EXPORT_SYMBOL_GPL(regulator_map_voltage_pickable_linear_range); 526 527 /** 528 * regulator_list_voltage_linear - List voltages with simple calculation 529 * 530 * @rdev: Regulator device 531 * @selector: Selector to convert into a voltage 532 * 533 * Regulators with a simple linear mapping between voltages and 534 * selectors can set min_uV and uV_step in the regulator descriptor 535 * and then use this function as their list_voltage() operation, 536 */ 537 int regulator_list_voltage_linear(struct regulator_dev *rdev, 538 unsigned int selector) 539 { 540 if (selector >= rdev->desc->n_voltages) 541 return -EINVAL; 542 if (selector < rdev->desc->linear_min_sel) 543 return 0; 544 545 selector -= rdev->desc->linear_min_sel; 546 547 return rdev->desc->min_uV + (rdev->desc->uV_step * selector); 548 } 549 EXPORT_SYMBOL_GPL(regulator_list_voltage_linear); 550 551 /** 552 * regulator_list_voltage_pickable_linear_range - pickable range list voltages 553 * 554 * @rdev: Regulator device 555 * @selector: Selector to convert into a voltage 556 * 557 * list_voltage() operation, intended to be used by drivers utilizing pickable 558 * ranges helpers. 559 */ 560 int regulator_list_voltage_pickable_linear_range(struct regulator_dev *rdev, 561 unsigned int selector) 562 { 563 const struct regulator_linear_range *range; 564 int i; 565 unsigned int all_sels = 0; 566 567 if (!rdev->desc->n_linear_ranges) { 568 BUG_ON(!rdev->desc->n_linear_ranges); 569 return -EINVAL; 570 } 571 572 for (i = 0; i < rdev->desc->n_linear_ranges; i++) { 573 unsigned int sels_in_range; 574 575 range = &rdev->desc->linear_ranges[i]; 576 577 sels_in_range = range->max_sel - range->min_sel; 578 579 if (all_sels + sels_in_range >= selector) { 580 selector -= all_sels; 581 return range->min_uV + (range->uV_step * selector); 582 } 583 584 all_sels += (sels_in_range + 1); 585 } 586 587 return -EINVAL; 588 } 589 EXPORT_SYMBOL_GPL(regulator_list_voltage_pickable_linear_range); 590 591 /** 592 * regulator_desc_list_voltage_linear_range - List voltages for linear ranges 593 * 594 * @desc: Regulator desc for regulator which volatges are to be listed 595 * @selector: Selector to convert into a voltage 596 * 597 * Regulators with a series of simple linear mappings between voltages 598 * and selectors who have set linear_ranges in the regulator descriptor 599 * can use this function prior regulator registration to list voltages. 600 * This is useful when voltages need to be listed during device-tree 601 * parsing. 602 */ 603 int regulator_desc_list_voltage_linear_range(const struct regulator_desc *desc, 604 unsigned int selector) 605 { 606 const struct regulator_linear_range *range; 607 int i; 608 609 if (!desc->n_linear_ranges) { 610 BUG_ON(!desc->n_linear_ranges); 611 return -EINVAL; 612 } 613 614 for (i = 0; i < desc->n_linear_ranges; i++) { 615 range = &desc->linear_ranges[i]; 616 617 if (!(selector >= range->min_sel && 618 selector <= range->max_sel)) 619 continue; 620 621 selector -= range->min_sel; 622 623 return range->min_uV + (range->uV_step * selector); 624 } 625 626 return -EINVAL; 627 } 628 EXPORT_SYMBOL_GPL(regulator_desc_list_voltage_linear_range); 629 630 /** 631 * regulator_list_voltage_linear_range - List voltages for linear ranges 632 * 633 * @rdev: Regulator device 634 * @selector: Selector to convert into a voltage 635 * 636 * Regulators with a series of simple linear mappings between voltages 637 * and selectors can set linear_ranges in the regulator descriptor and 638 * then use this function as their list_voltage() operation, 639 */ 640 int regulator_list_voltage_linear_range(struct regulator_dev *rdev, 641 unsigned int selector) 642 { 643 return regulator_desc_list_voltage_linear_range(rdev->desc, selector); 644 } 645 EXPORT_SYMBOL_GPL(regulator_list_voltage_linear_range); 646 647 /** 648 * regulator_list_voltage_table - List voltages with table based mapping 649 * 650 * @rdev: Regulator device 651 * @selector: Selector to convert into a voltage 652 * 653 * Regulators with table based mapping between voltages and 654 * selectors can set volt_table in the regulator descriptor 655 * and then use this function as their list_voltage() operation. 656 */ 657 int regulator_list_voltage_table(struct regulator_dev *rdev, 658 unsigned int selector) 659 { 660 if (!rdev->desc->volt_table) { 661 BUG_ON(!rdev->desc->volt_table); 662 return -EINVAL; 663 } 664 665 if (selector >= rdev->desc->n_voltages) 666 return -EINVAL; 667 668 return rdev->desc->volt_table[selector]; 669 } 670 EXPORT_SYMBOL_GPL(regulator_list_voltage_table); 671 672 /** 673 * regulator_set_bypass_regmap - Default set_bypass() using regmap 674 * 675 * @rdev: device to operate on. 676 * @enable: state to set. 677 */ 678 int regulator_set_bypass_regmap(struct regulator_dev *rdev, bool enable) 679 { 680 unsigned int val; 681 682 if (enable) { 683 val = rdev->desc->bypass_val_on; 684 if (!val) 685 val = rdev->desc->bypass_mask; 686 } else { 687 val = rdev->desc->bypass_val_off; 688 } 689 690 return regmap_update_bits(rdev->regmap, rdev->desc->bypass_reg, 691 rdev->desc->bypass_mask, val); 692 } 693 EXPORT_SYMBOL_GPL(regulator_set_bypass_regmap); 694 695 /** 696 * regulator_set_soft_start_regmap - Default set_soft_start() using regmap 697 * 698 * @rdev: device to operate on. 699 */ 700 int regulator_set_soft_start_regmap(struct regulator_dev *rdev) 701 { 702 unsigned int val; 703 704 val = rdev->desc->soft_start_val_on; 705 if (!val) 706 val = rdev->desc->soft_start_mask; 707 708 return regmap_update_bits(rdev->regmap, rdev->desc->soft_start_reg, 709 rdev->desc->soft_start_mask, val); 710 } 711 EXPORT_SYMBOL_GPL(regulator_set_soft_start_regmap); 712 713 /** 714 * regulator_set_pull_down_regmap - Default set_pull_down() using regmap 715 * 716 * @rdev: device to operate on. 717 */ 718 int regulator_set_pull_down_regmap(struct regulator_dev *rdev) 719 { 720 unsigned int val; 721 722 val = rdev->desc->pull_down_val_on; 723 if (!val) 724 val = rdev->desc->pull_down_mask; 725 726 return regmap_update_bits(rdev->regmap, rdev->desc->pull_down_reg, 727 rdev->desc->pull_down_mask, val); 728 } 729 EXPORT_SYMBOL_GPL(regulator_set_pull_down_regmap); 730 731 /** 732 * regulator_get_bypass_regmap - Default get_bypass() using regmap 733 * 734 * @rdev: device to operate on. 735 * @enable: current state. 736 */ 737 int regulator_get_bypass_regmap(struct regulator_dev *rdev, bool *enable) 738 { 739 unsigned int val; 740 unsigned int val_on = rdev->desc->bypass_val_on; 741 int ret; 742 743 ret = regmap_read(rdev->regmap, rdev->desc->bypass_reg, &val); 744 if (ret != 0) 745 return ret; 746 747 if (!val_on) 748 val_on = rdev->desc->bypass_mask; 749 750 *enable = (val & rdev->desc->bypass_mask) == val_on; 751 752 return 0; 753 } 754 EXPORT_SYMBOL_GPL(regulator_get_bypass_regmap); 755 756 /** 757 * regulator_set_active_discharge_regmap - Default set_active_discharge() 758 * using regmap 759 * 760 * @rdev: device to operate on. 761 * @enable: state to set, 0 to disable and 1 to enable. 762 */ 763 int regulator_set_active_discharge_regmap(struct regulator_dev *rdev, 764 bool enable) 765 { 766 unsigned int val; 767 768 if (enable) 769 val = rdev->desc->active_discharge_on; 770 else 771 val = rdev->desc->active_discharge_off; 772 773 return regmap_update_bits(rdev->regmap, 774 rdev->desc->active_discharge_reg, 775 rdev->desc->active_discharge_mask, val); 776 } 777 EXPORT_SYMBOL_GPL(regulator_set_active_discharge_regmap); 778 779 /** 780 * regulator_set_current_limit_regmap - set_current_limit for regmap users 781 * 782 * @rdev: regulator to operate on 783 * @min_uA: Lower bound for current limit 784 * @max_uA: Upper bound for current limit 785 * 786 * Regulators that use regmap for their register I/O can set curr_table, 787 * csel_reg and csel_mask fields in their descriptor and then use this 788 * as their set_current_limit operation, saving some code. 789 */ 790 int regulator_set_current_limit_regmap(struct regulator_dev *rdev, 791 int min_uA, int max_uA) 792 { 793 unsigned int n_currents = rdev->desc->n_current_limits; 794 int i, sel = -1; 795 796 if (n_currents == 0) 797 return -EINVAL; 798 799 if (rdev->desc->curr_table) { 800 const unsigned int *curr_table = rdev->desc->curr_table; 801 bool ascend = curr_table[n_currents - 1] > curr_table[0]; 802 803 /* search for closest to maximum */ 804 if (ascend) { 805 for (i = n_currents - 1; i >= 0; i--) { 806 if (min_uA <= curr_table[i] && 807 curr_table[i] <= max_uA) { 808 sel = i; 809 break; 810 } 811 } 812 } else { 813 for (i = 0; i < n_currents; i++) { 814 if (min_uA <= curr_table[i] && 815 curr_table[i] <= max_uA) { 816 sel = i; 817 break; 818 } 819 } 820 } 821 } 822 823 if (sel < 0) 824 return -EINVAL; 825 826 sel <<= ffs(rdev->desc->csel_mask) - 1; 827 828 return regmap_update_bits(rdev->regmap, rdev->desc->csel_reg, 829 rdev->desc->csel_mask, sel); 830 } 831 EXPORT_SYMBOL_GPL(regulator_set_current_limit_regmap); 832 833 /** 834 * regulator_get_current_limit_regmap - get_current_limit for regmap users 835 * 836 * @rdev: regulator to operate on 837 * 838 * Regulators that use regmap for their register I/O can set the 839 * csel_reg and csel_mask fields in their descriptor and then use this 840 * as their get_current_limit operation, saving some code. 841 */ 842 int regulator_get_current_limit_regmap(struct regulator_dev *rdev) 843 { 844 unsigned int val; 845 int ret; 846 847 ret = regmap_read(rdev->regmap, rdev->desc->csel_reg, &val); 848 if (ret != 0) 849 return ret; 850 851 val &= rdev->desc->csel_mask; 852 val >>= ffs(rdev->desc->csel_mask) - 1; 853 854 if (rdev->desc->curr_table) { 855 if (val >= rdev->desc->n_current_limits) 856 return -EINVAL; 857 858 return rdev->desc->curr_table[val]; 859 } 860 861 return -EINVAL; 862 } 863 EXPORT_SYMBOL_GPL(regulator_get_current_limit_regmap); 864