1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Generic OPP Interface 4 * 5 * Copyright (C) 2009-2010 Texas Instruments Incorporated. 6 * Nishanth Menon 7 * Romit Dasgupta 8 * Kevin Hilman 9 */ 10 11 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 12 13 #include <linux/clk.h> 14 #include <linux/errno.h> 15 #include <linux/err.h> 16 #include <linux/slab.h> 17 #include <linux/device.h> 18 #include <linux/export.h> 19 #include <linux/pm_domain.h> 20 #include <linux/regulator/consumer.h> 21 22 #include "opp.h" 23 24 /* 25 * The root of the list of all opp-tables. All opp_table structures branch off 26 * from here, with each opp_table containing the list of opps it supports in 27 * various states of availability. 28 */ 29 LIST_HEAD(opp_tables); 30 31 /* OPP tables with uninitialized required OPPs */ 32 LIST_HEAD(lazy_opp_tables); 33 34 /* Lock to allow exclusive modification to the device and opp lists */ 35 DEFINE_MUTEX(opp_table_lock); 36 /* Flag indicating that opp_tables list is being updated at the moment */ 37 static bool opp_tables_busy; 38 39 static bool _find_opp_dev(const struct device *dev, struct opp_table *opp_table) 40 { 41 struct opp_device *opp_dev; 42 bool found = false; 43 44 mutex_lock(&opp_table->lock); 45 list_for_each_entry(opp_dev, &opp_table->dev_list, node) 46 if (opp_dev->dev == dev) { 47 found = true; 48 break; 49 } 50 51 mutex_unlock(&opp_table->lock); 52 return found; 53 } 54 55 static struct opp_table *_find_opp_table_unlocked(struct device *dev) 56 { 57 struct opp_table *opp_table; 58 59 list_for_each_entry(opp_table, &opp_tables, node) { 60 if (_find_opp_dev(dev, opp_table)) { 61 _get_opp_table_kref(opp_table); 62 return opp_table; 63 } 64 } 65 66 return ERR_PTR(-ENODEV); 67 } 68 69 /** 70 * _find_opp_table() - find opp_table struct using device pointer 71 * @dev: device pointer used to lookup OPP table 72 * 73 * Search OPP table for one containing matching device. 74 * 75 * Return: pointer to 'struct opp_table' if found, otherwise -ENODEV or 76 * -EINVAL based on type of error. 77 * 78 * The callers must call dev_pm_opp_put_opp_table() after the table is used. 79 */ 80 struct opp_table *_find_opp_table(struct device *dev) 81 { 82 struct opp_table *opp_table; 83 84 if (IS_ERR_OR_NULL(dev)) { 85 pr_err("%s: Invalid parameters\n", __func__); 86 return ERR_PTR(-EINVAL); 87 } 88 89 mutex_lock(&opp_table_lock); 90 opp_table = _find_opp_table_unlocked(dev); 91 mutex_unlock(&opp_table_lock); 92 93 return opp_table; 94 } 95 96 /** 97 * dev_pm_opp_get_voltage() - Gets the voltage corresponding to an opp 98 * @opp: opp for which voltage has to be returned for 99 * 100 * Return: voltage in micro volt corresponding to the opp, else 101 * return 0 102 * 103 * This is useful only for devices with single power supply. 104 */ 105 unsigned long dev_pm_opp_get_voltage(struct dev_pm_opp *opp) 106 { 107 if (IS_ERR_OR_NULL(opp)) { 108 pr_err("%s: Invalid parameters\n", __func__); 109 return 0; 110 } 111 112 return opp->supplies[0].u_volt; 113 } 114 EXPORT_SYMBOL_GPL(dev_pm_opp_get_voltage); 115 116 /** 117 * dev_pm_opp_get_power() - Gets the power corresponding to an opp 118 * @opp: opp for which power has to be returned for 119 * 120 * Return: power in micro watt corresponding to the opp, else 121 * return 0 122 * 123 * This is useful only for devices with single power supply. 124 */ 125 unsigned long dev_pm_opp_get_power(struct dev_pm_opp *opp) 126 { 127 unsigned long opp_power = 0; 128 int i; 129 130 if (IS_ERR_OR_NULL(opp)) { 131 pr_err("%s: Invalid parameters\n", __func__); 132 return 0; 133 } 134 for (i = 0; i < opp->opp_table->regulator_count; i++) 135 opp_power += opp->supplies[i].u_watt; 136 137 return opp_power; 138 } 139 EXPORT_SYMBOL_GPL(dev_pm_opp_get_power); 140 141 /** 142 * dev_pm_opp_get_freq() - Gets the frequency corresponding to an available opp 143 * @opp: opp for which frequency has to be returned for 144 * 145 * Return: frequency in hertz corresponding to the opp, else 146 * return 0 147 */ 148 unsigned long dev_pm_opp_get_freq(struct dev_pm_opp *opp) 149 { 150 if (IS_ERR_OR_NULL(opp)) { 151 pr_err("%s: Invalid parameters\n", __func__); 152 return 0; 153 } 154 155 return opp->rate; 156 } 157 EXPORT_SYMBOL_GPL(dev_pm_opp_get_freq); 158 159 /** 160 * dev_pm_opp_get_level() - Gets the level corresponding to an available opp 161 * @opp: opp for which level value has to be returned for 162 * 163 * Return: level read from device tree corresponding to the opp, else 164 * return 0. 165 */ 166 unsigned int dev_pm_opp_get_level(struct dev_pm_opp *opp) 167 { 168 if (IS_ERR_OR_NULL(opp) || !opp->available) { 169 pr_err("%s: Invalid parameters\n", __func__); 170 return 0; 171 } 172 173 return opp->level; 174 } 175 EXPORT_SYMBOL_GPL(dev_pm_opp_get_level); 176 177 /** 178 * dev_pm_opp_get_required_pstate() - Gets the required performance state 179 * corresponding to an available opp 180 * @opp: opp for which performance state has to be returned for 181 * @index: index of the required opp 182 * 183 * Return: performance state read from device tree corresponding to the 184 * required opp, else return 0. 185 */ 186 unsigned int dev_pm_opp_get_required_pstate(struct dev_pm_opp *opp, 187 unsigned int index) 188 { 189 if (IS_ERR_OR_NULL(opp) || !opp->available || 190 index >= opp->opp_table->required_opp_count) { 191 pr_err("%s: Invalid parameters\n", __func__); 192 return 0; 193 } 194 195 /* required-opps not fully initialized yet */ 196 if (lazy_linking_pending(opp->opp_table)) 197 return 0; 198 199 return opp->required_opps[index]->pstate; 200 } 201 EXPORT_SYMBOL_GPL(dev_pm_opp_get_required_pstate); 202 203 /** 204 * dev_pm_opp_is_turbo() - Returns if opp is turbo OPP or not 205 * @opp: opp for which turbo mode is being verified 206 * 207 * Turbo OPPs are not for normal use, and can be enabled (under certain 208 * conditions) for short duration of times to finish high throughput work 209 * quickly. Running on them for longer times may overheat the chip. 210 * 211 * Return: true if opp is turbo opp, else false. 212 */ 213 bool dev_pm_opp_is_turbo(struct dev_pm_opp *opp) 214 { 215 if (IS_ERR_OR_NULL(opp) || !opp->available) { 216 pr_err("%s: Invalid parameters\n", __func__); 217 return false; 218 } 219 220 return opp->turbo; 221 } 222 EXPORT_SYMBOL_GPL(dev_pm_opp_is_turbo); 223 224 /** 225 * dev_pm_opp_get_max_clock_latency() - Get max clock latency in nanoseconds 226 * @dev: device for which we do this operation 227 * 228 * Return: This function returns the max clock latency in nanoseconds. 229 */ 230 unsigned long dev_pm_opp_get_max_clock_latency(struct device *dev) 231 { 232 struct opp_table *opp_table; 233 unsigned long clock_latency_ns; 234 235 opp_table = _find_opp_table(dev); 236 if (IS_ERR(opp_table)) 237 return 0; 238 239 clock_latency_ns = opp_table->clock_latency_ns_max; 240 241 dev_pm_opp_put_opp_table(opp_table); 242 243 return clock_latency_ns; 244 } 245 EXPORT_SYMBOL_GPL(dev_pm_opp_get_max_clock_latency); 246 247 /** 248 * dev_pm_opp_get_max_volt_latency() - Get max voltage latency in nanoseconds 249 * @dev: device for which we do this operation 250 * 251 * Return: This function returns the max voltage latency in nanoseconds. 252 */ 253 unsigned long dev_pm_opp_get_max_volt_latency(struct device *dev) 254 { 255 struct opp_table *opp_table; 256 struct dev_pm_opp *opp; 257 struct regulator *reg; 258 unsigned long latency_ns = 0; 259 int ret, i, count; 260 struct { 261 unsigned long min; 262 unsigned long max; 263 } *uV; 264 265 opp_table = _find_opp_table(dev); 266 if (IS_ERR(opp_table)) 267 return 0; 268 269 /* Regulator may not be required for the device */ 270 if (!opp_table->regulators) 271 goto put_opp_table; 272 273 count = opp_table->regulator_count; 274 275 uV = kmalloc_array(count, sizeof(*uV), GFP_KERNEL); 276 if (!uV) 277 goto put_opp_table; 278 279 mutex_lock(&opp_table->lock); 280 281 for (i = 0; i < count; i++) { 282 uV[i].min = ~0; 283 uV[i].max = 0; 284 285 list_for_each_entry(opp, &opp_table->opp_list, node) { 286 if (!opp->available) 287 continue; 288 289 if (opp->supplies[i].u_volt_min < uV[i].min) 290 uV[i].min = opp->supplies[i].u_volt_min; 291 if (opp->supplies[i].u_volt_max > uV[i].max) 292 uV[i].max = opp->supplies[i].u_volt_max; 293 } 294 } 295 296 mutex_unlock(&opp_table->lock); 297 298 /* 299 * The caller needs to ensure that opp_table (and hence the regulator) 300 * isn't freed, while we are executing this routine. 301 */ 302 for (i = 0; i < count; i++) { 303 reg = opp_table->regulators[i]; 304 ret = regulator_set_voltage_time(reg, uV[i].min, uV[i].max); 305 if (ret > 0) 306 latency_ns += ret * 1000; 307 } 308 309 kfree(uV); 310 put_opp_table: 311 dev_pm_opp_put_opp_table(opp_table); 312 313 return latency_ns; 314 } 315 EXPORT_SYMBOL_GPL(dev_pm_opp_get_max_volt_latency); 316 317 /** 318 * dev_pm_opp_get_max_transition_latency() - Get max transition latency in 319 * nanoseconds 320 * @dev: device for which we do this operation 321 * 322 * Return: This function returns the max transition latency, in nanoseconds, to 323 * switch from one OPP to other. 324 */ 325 unsigned long dev_pm_opp_get_max_transition_latency(struct device *dev) 326 { 327 return dev_pm_opp_get_max_volt_latency(dev) + 328 dev_pm_opp_get_max_clock_latency(dev); 329 } 330 EXPORT_SYMBOL_GPL(dev_pm_opp_get_max_transition_latency); 331 332 /** 333 * dev_pm_opp_get_suspend_opp_freq() - Get frequency of suspend opp in Hz 334 * @dev: device for which we do this operation 335 * 336 * Return: This function returns the frequency of the OPP marked as suspend_opp 337 * if one is available, else returns 0; 338 */ 339 unsigned long dev_pm_opp_get_suspend_opp_freq(struct device *dev) 340 { 341 struct opp_table *opp_table; 342 unsigned long freq = 0; 343 344 opp_table = _find_opp_table(dev); 345 if (IS_ERR(opp_table)) 346 return 0; 347 348 if (opp_table->suspend_opp && opp_table->suspend_opp->available) 349 freq = dev_pm_opp_get_freq(opp_table->suspend_opp); 350 351 dev_pm_opp_put_opp_table(opp_table); 352 353 return freq; 354 } 355 EXPORT_SYMBOL_GPL(dev_pm_opp_get_suspend_opp_freq); 356 357 int _get_opp_count(struct opp_table *opp_table) 358 { 359 struct dev_pm_opp *opp; 360 int count = 0; 361 362 mutex_lock(&opp_table->lock); 363 364 list_for_each_entry(opp, &opp_table->opp_list, node) { 365 if (opp->available) 366 count++; 367 } 368 369 mutex_unlock(&opp_table->lock); 370 371 return count; 372 } 373 374 /** 375 * dev_pm_opp_get_opp_count() - Get number of opps available in the opp table 376 * @dev: device for which we do this operation 377 * 378 * Return: This function returns the number of available opps if there are any, 379 * else returns 0 if none or the corresponding error value. 380 */ 381 int dev_pm_opp_get_opp_count(struct device *dev) 382 { 383 struct opp_table *opp_table; 384 int count; 385 386 opp_table = _find_opp_table(dev); 387 if (IS_ERR(opp_table)) { 388 count = PTR_ERR(opp_table); 389 dev_dbg(dev, "%s: OPP table not found (%d)\n", 390 __func__, count); 391 return count; 392 } 393 394 count = _get_opp_count(opp_table); 395 dev_pm_opp_put_opp_table(opp_table); 396 397 return count; 398 } 399 EXPORT_SYMBOL_GPL(dev_pm_opp_get_opp_count); 400 401 /** 402 * dev_pm_opp_find_freq_exact() - search for an exact frequency 403 * @dev: device for which we do this operation 404 * @freq: frequency to search for 405 * @available: true/false - match for available opp 406 * 407 * Return: Searches for exact match in the opp table and returns pointer to the 408 * matching opp if found, else returns ERR_PTR in case of error and should 409 * be handled using IS_ERR. Error return values can be: 410 * EINVAL: for bad pointer 411 * ERANGE: no match found for search 412 * ENODEV: if device not found in list of registered devices 413 * 414 * Note: available is a modifier for the search. if available=true, then the 415 * match is for exact matching frequency and is available in the stored OPP 416 * table. if false, the match is for exact frequency which is not available. 417 * 418 * This provides a mechanism to enable an opp which is not available currently 419 * or the opposite as well. 420 * 421 * The callers are required to call dev_pm_opp_put() for the returned OPP after 422 * use. 423 */ 424 struct dev_pm_opp *dev_pm_opp_find_freq_exact(struct device *dev, 425 unsigned long freq, 426 bool available) 427 { 428 struct opp_table *opp_table; 429 struct dev_pm_opp *temp_opp, *opp = ERR_PTR(-ERANGE); 430 431 opp_table = _find_opp_table(dev); 432 if (IS_ERR(opp_table)) { 433 int r = PTR_ERR(opp_table); 434 435 dev_err(dev, "%s: OPP table not found (%d)\n", __func__, r); 436 return ERR_PTR(r); 437 } 438 439 mutex_lock(&opp_table->lock); 440 441 list_for_each_entry(temp_opp, &opp_table->opp_list, node) { 442 if (temp_opp->available == available && 443 temp_opp->rate == freq) { 444 opp = temp_opp; 445 446 /* Increment the reference count of OPP */ 447 dev_pm_opp_get(opp); 448 break; 449 } 450 } 451 452 mutex_unlock(&opp_table->lock); 453 dev_pm_opp_put_opp_table(opp_table); 454 455 return opp; 456 } 457 EXPORT_SYMBOL_GPL(dev_pm_opp_find_freq_exact); 458 459 /** 460 * dev_pm_opp_find_level_exact() - search for an exact level 461 * @dev: device for which we do this operation 462 * @level: level to search for 463 * 464 * Return: Searches for exact match in the opp table and returns pointer to the 465 * matching opp if found, else returns ERR_PTR in case of error and should 466 * be handled using IS_ERR. Error return values can be: 467 * EINVAL: for bad pointer 468 * ERANGE: no match found for search 469 * ENODEV: if device not found in list of registered devices 470 * 471 * The callers are required to call dev_pm_opp_put() for the returned OPP after 472 * use. 473 */ 474 struct dev_pm_opp *dev_pm_opp_find_level_exact(struct device *dev, 475 unsigned int level) 476 { 477 struct opp_table *opp_table; 478 struct dev_pm_opp *temp_opp, *opp = ERR_PTR(-ERANGE); 479 480 opp_table = _find_opp_table(dev); 481 if (IS_ERR(opp_table)) { 482 int r = PTR_ERR(opp_table); 483 484 dev_err(dev, "%s: OPP table not found (%d)\n", __func__, r); 485 return ERR_PTR(r); 486 } 487 488 mutex_lock(&opp_table->lock); 489 490 list_for_each_entry(temp_opp, &opp_table->opp_list, node) { 491 if (temp_opp->level == level) { 492 opp = temp_opp; 493 494 /* Increment the reference count of OPP */ 495 dev_pm_opp_get(opp); 496 break; 497 } 498 } 499 500 mutex_unlock(&opp_table->lock); 501 dev_pm_opp_put_opp_table(opp_table); 502 503 return opp; 504 } 505 EXPORT_SYMBOL_GPL(dev_pm_opp_find_level_exact); 506 507 /** 508 * dev_pm_opp_find_level_ceil() - search for an rounded up level 509 * @dev: device for which we do this operation 510 * @level: level to search for 511 * 512 * Return: Searches for rounded up match in the opp table and returns pointer 513 * to the matching opp if found, else returns ERR_PTR in case of error and 514 * should be handled using IS_ERR. Error return values can be: 515 * EINVAL: for bad pointer 516 * ERANGE: no match found for search 517 * ENODEV: if device not found in list of registered devices 518 * 519 * The callers are required to call dev_pm_opp_put() for the returned OPP after 520 * use. 521 */ 522 struct dev_pm_opp *dev_pm_opp_find_level_ceil(struct device *dev, 523 unsigned int *level) 524 { 525 struct opp_table *opp_table; 526 struct dev_pm_opp *temp_opp, *opp = ERR_PTR(-ERANGE); 527 528 opp_table = _find_opp_table(dev); 529 if (IS_ERR(opp_table)) { 530 int r = PTR_ERR(opp_table); 531 532 dev_err(dev, "%s: OPP table not found (%d)\n", __func__, r); 533 return ERR_PTR(r); 534 } 535 536 mutex_lock(&opp_table->lock); 537 538 list_for_each_entry(temp_opp, &opp_table->opp_list, node) { 539 if (temp_opp->available && temp_opp->level >= *level) { 540 opp = temp_opp; 541 *level = opp->level; 542 543 /* Increment the reference count of OPP */ 544 dev_pm_opp_get(opp); 545 break; 546 } 547 } 548 549 mutex_unlock(&opp_table->lock); 550 dev_pm_opp_put_opp_table(opp_table); 551 552 return opp; 553 } 554 EXPORT_SYMBOL_GPL(dev_pm_opp_find_level_ceil); 555 556 static noinline struct dev_pm_opp *_find_freq_ceil(struct opp_table *opp_table, 557 unsigned long *freq) 558 { 559 struct dev_pm_opp *temp_opp, *opp = ERR_PTR(-ERANGE); 560 561 mutex_lock(&opp_table->lock); 562 563 list_for_each_entry(temp_opp, &opp_table->opp_list, node) { 564 if (temp_opp->available && temp_opp->rate >= *freq) { 565 opp = temp_opp; 566 *freq = opp->rate; 567 568 /* Increment the reference count of OPP */ 569 dev_pm_opp_get(opp); 570 break; 571 } 572 } 573 574 mutex_unlock(&opp_table->lock); 575 576 return opp; 577 } 578 579 /** 580 * dev_pm_opp_find_freq_ceil() - Search for an rounded ceil freq 581 * @dev: device for which we do this operation 582 * @freq: Start frequency 583 * 584 * Search for the matching ceil *available* OPP from a starting freq 585 * for a device. 586 * 587 * Return: matching *opp and refreshes *freq accordingly, else returns 588 * ERR_PTR in case of error and should be handled using IS_ERR. Error return 589 * values can be: 590 * EINVAL: for bad pointer 591 * ERANGE: no match found for search 592 * ENODEV: if device not found in list of registered devices 593 * 594 * The callers are required to call dev_pm_opp_put() for the returned OPP after 595 * use. 596 */ 597 struct dev_pm_opp *dev_pm_opp_find_freq_ceil(struct device *dev, 598 unsigned long *freq) 599 { 600 struct opp_table *opp_table; 601 struct dev_pm_opp *opp; 602 603 if (!dev || !freq) { 604 dev_err(dev, "%s: Invalid argument freq=%p\n", __func__, freq); 605 return ERR_PTR(-EINVAL); 606 } 607 608 opp_table = _find_opp_table(dev); 609 if (IS_ERR(opp_table)) 610 return ERR_CAST(opp_table); 611 612 opp = _find_freq_ceil(opp_table, freq); 613 614 dev_pm_opp_put_opp_table(opp_table); 615 616 return opp; 617 } 618 EXPORT_SYMBOL_GPL(dev_pm_opp_find_freq_ceil); 619 620 /** 621 * dev_pm_opp_find_freq_floor() - Search for a rounded floor freq 622 * @dev: device for which we do this operation 623 * @freq: Start frequency 624 * 625 * Search for the matching floor *available* OPP from a starting freq 626 * for a device. 627 * 628 * Return: matching *opp and refreshes *freq accordingly, else returns 629 * ERR_PTR in case of error and should be handled using IS_ERR. Error return 630 * values can be: 631 * EINVAL: for bad pointer 632 * ERANGE: no match found for search 633 * ENODEV: if device not found in list of registered devices 634 * 635 * The callers are required to call dev_pm_opp_put() for the returned OPP after 636 * use. 637 */ 638 struct dev_pm_opp *dev_pm_opp_find_freq_floor(struct device *dev, 639 unsigned long *freq) 640 { 641 struct opp_table *opp_table; 642 struct dev_pm_opp *temp_opp, *opp = ERR_PTR(-ERANGE); 643 644 if (!dev || !freq) { 645 dev_err(dev, "%s: Invalid argument freq=%p\n", __func__, freq); 646 return ERR_PTR(-EINVAL); 647 } 648 649 opp_table = _find_opp_table(dev); 650 if (IS_ERR(opp_table)) 651 return ERR_CAST(opp_table); 652 653 mutex_lock(&opp_table->lock); 654 655 list_for_each_entry(temp_opp, &opp_table->opp_list, node) { 656 if (temp_opp->available) { 657 /* go to the next node, before choosing prev */ 658 if (temp_opp->rate > *freq) 659 break; 660 else 661 opp = temp_opp; 662 } 663 } 664 665 /* Increment the reference count of OPP */ 666 if (!IS_ERR(opp)) 667 dev_pm_opp_get(opp); 668 mutex_unlock(&opp_table->lock); 669 dev_pm_opp_put_opp_table(opp_table); 670 671 if (!IS_ERR(opp)) 672 *freq = opp->rate; 673 674 return opp; 675 } 676 EXPORT_SYMBOL_GPL(dev_pm_opp_find_freq_floor); 677 678 /** 679 * dev_pm_opp_find_freq_ceil_by_volt() - Find OPP with highest frequency for 680 * target voltage. 681 * @dev: Device for which we do this operation. 682 * @u_volt: Target voltage. 683 * 684 * Search for OPP with highest (ceil) frequency and has voltage <= u_volt. 685 * 686 * Return: matching *opp, else returns ERR_PTR in case of error which should be 687 * handled using IS_ERR. 688 * 689 * Error return values can be: 690 * EINVAL: bad parameters 691 * 692 * The callers are required to call dev_pm_opp_put() for the returned OPP after 693 * use. 694 */ 695 struct dev_pm_opp *dev_pm_opp_find_freq_ceil_by_volt(struct device *dev, 696 unsigned long u_volt) 697 { 698 struct opp_table *opp_table; 699 struct dev_pm_opp *temp_opp, *opp = ERR_PTR(-ERANGE); 700 701 if (!dev || !u_volt) { 702 dev_err(dev, "%s: Invalid argument volt=%lu\n", __func__, 703 u_volt); 704 return ERR_PTR(-EINVAL); 705 } 706 707 opp_table = _find_opp_table(dev); 708 if (IS_ERR(opp_table)) 709 return ERR_CAST(opp_table); 710 711 mutex_lock(&opp_table->lock); 712 713 list_for_each_entry(temp_opp, &opp_table->opp_list, node) { 714 if (temp_opp->available) { 715 if (temp_opp->supplies[0].u_volt > u_volt) 716 break; 717 opp = temp_opp; 718 } 719 } 720 721 /* Increment the reference count of OPP */ 722 if (!IS_ERR(opp)) 723 dev_pm_opp_get(opp); 724 725 mutex_unlock(&opp_table->lock); 726 dev_pm_opp_put_opp_table(opp_table); 727 728 return opp; 729 } 730 EXPORT_SYMBOL_GPL(dev_pm_opp_find_freq_ceil_by_volt); 731 732 static int _set_opp_voltage(struct device *dev, struct regulator *reg, 733 struct dev_pm_opp_supply *supply) 734 { 735 int ret; 736 737 /* Regulator not available for device */ 738 if (IS_ERR(reg)) { 739 dev_dbg(dev, "%s: regulator not available: %ld\n", __func__, 740 PTR_ERR(reg)); 741 return 0; 742 } 743 744 dev_dbg(dev, "%s: voltages (mV): %lu %lu %lu\n", __func__, 745 supply->u_volt_min, supply->u_volt, supply->u_volt_max); 746 747 ret = regulator_set_voltage_triplet(reg, supply->u_volt_min, 748 supply->u_volt, supply->u_volt_max); 749 if (ret) 750 dev_err(dev, "%s: failed to set voltage (%lu %lu %lu mV): %d\n", 751 __func__, supply->u_volt_min, supply->u_volt, 752 supply->u_volt_max, ret); 753 754 return ret; 755 } 756 757 static inline int _generic_set_opp_clk_only(struct device *dev, struct clk *clk, 758 unsigned long freq) 759 { 760 int ret; 761 762 /* We may reach here for devices which don't change frequency */ 763 if (IS_ERR(clk)) 764 return 0; 765 766 ret = clk_set_rate(clk, freq); 767 if (ret) { 768 dev_err(dev, "%s: failed to set clock rate: %d\n", __func__, 769 ret); 770 } 771 772 return ret; 773 } 774 775 static int _generic_set_opp_regulator(struct opp_table *opp_table, 776 struct device *dev, 777 struct dev_pm_opp *opp, 778 unsigned long freq, 779 int scaling_down) 780 { 781 struct regulator *reg = opp_table->regulators[0]; 782 struct dev_pm_opp *old_opp = opp_table->current_opp; 783 int ret; 784 785 /* This function only supports single regulator per device */ 786 if (WARN_ON(opp_table->regulator_count > 1)) { 787 dev_err(dev, "multiple regulators are not supported\n"); 788 return -EINVAL; 789 } 790 791 /* Scaling up? Scale voltage before frequency */ 792 if (!scaling_down) { 793 ret = _set_opp_voltage(dev, reg, opp->supplies); 794 if (ret) 795 goto restore_voltage; 796 } 797 798 /* Change frequency */ 799 ret = _generic_set_opp_clk_only(dev, opp_table->clk, freq); 800 if (ret) 801 goto restore_voltage; 802 803 /* Scaling down? Scale voltage after frequency */ 804 if (scaling_down) { 805 ret = _set_opp_voltage(dev, reg, opp->supplies); 806 if (ret) 807 goto restore_freq; 808 } 809 810 /* 811 * Enable the regulator after setting its voltages, otherwise it breaks 812 * some boot-enabled regulators. 813 */ 814 if (unlikely(!opp_table->enabled)) { 815 ret = regulator_enable(reg); 816 if (ret < 0) 817 dev_warn(dev, "Failed to enable regulator: %d", ret); 818 } 819 820 return 0; 821 822 restore_freq: 823 if (_generic_set_opp_clk_only(dev, opp_table->clk, old_opp->rate)) 824 dev_err(dev, "%s: failed to restore old-freq (%lu Hz)\n", 825 __func__, old_opp->rate); 826 restore_voltage: 827 /* This shouldn't harm even if the voltages weren't updated earlier */ 828 _set_opp_voltage(dev, reg, old_opp->supplies); 829 830 return ret; 831 } 832 833 static int _set_opp_bw(const struct opp_table *opp_table, 834 struct dev_pm_opp *opp, struct device *dev) 835 { 836 u32 avg, peak; 837 int i, ret; 838 839 if (!opp_table->paths) 840 return 0; 841 842 for (i = 0; i < opp_table->path_count; i++) { 843 if (!opp) { 844 avg = 0; 845 peak = 0; 846 } else { 847 avg = opp->bandwidth[i].avg; 848 peak = opp->bandwidth[i].peak; 849 } 850 ret = icc_set_bw(opp_table->paths[i], avg, peak); 851 if (ret) { 852 dev_err(dev, "Failed to %s bandwidth[%d]: %d\n", 853 opp ? "set" : "remove", i, ret); 854 return ret; 855 } 856 } 857 858 return 0; 859 } 860 861 static int _set_opp_custom(const struct opp_table *opp_table, 862 struct device *dev, struct dev_pm_opp *opp, 863 unsigned long freq) 864 { 865 struct dev_pm_set_opp_data *data = opp_table->set_opp_data; 866 struct dev_pm_opp *old_opp = opp_table->current_opp; 867 int size; 868 869 /* 870 * We support this only if dev_pm_opp_set_regulators() was called 871 * earlier. 872 */ 873 if (opp_table->sod_supplies) { 874 size = sizeof(*old_opp->supplies) * opp_table->regulator_count; 875 memcpy(data->old_opp.supplies, old_opp->supplies, size); 876 memcpy(data->new_opp.supplies, opp->supplies, size); 877 data->regulator_count = opp_table->regulator_count; 878 } else { 879 data->regulator_count = 0; 880 } 881 882 data->regulators = opp_table->regulators; 883 data->clk = opp_table->clk; 884 data->dev = dev; 885 data->old_opp.rate = old_opp->rate; 886 data->new_opp.rate = freq; 887 888 return opp_table->set_opp(data); 889 } 890 891 static int _set_required_opp(struct device *dev, struct device *pd_dev, 892 struct dev_pm_opp *opp, int i) 893 { 894 unsigned int pstate = likely(opp) ? opp->required_opps[i]->pstate : 0; 895 int ret; 896 897 if (!pd_dev) 898 return 0; 899 900 ret = dev_pm_genpd_set_performance_state(pd_dev, pstate); 901 if (ret) { 902 dev_err(dev, "Failed to set performance rate of %s: %d (%d)\n", 903 dev_name(pd_dev), pstate, ret); 904 } 905 906 return ret; 907 } 908 909 /* This is only called for PM domain for now */ 910 static int _set_required_opps(struct device *dev, 911 struct opp_table *opp_table, 912 struct dev_pm_opp *opp, bool up) 913 { 914 struct opp_table **required_opp_tables = opp_table->required_opp_tables; 915 struct device **genpd_virt_devs = opp_table->genpd_virt_devs; 916 int i, ret = 0; 917 918 if (!required_opp_tables) 919 return 0; 920 921 /* required-opps not fully initialized yet */ 922 if (lazy_linking_pending(opp_table)) 923 return -EBUSY; 924 925 /* 926 * We only support genpd's OPPs in the "required-opps" for now, as we 927 * don't know much about other use cases. Error out if the required OPP 928 * doesn't belong to a genpd. 929 */ 930 if (unlikely(!required_opp_tables[0]->is_genpd)) { 931 dev_err(dev, "required-opps don't belong to a genpd\n"); 932 return -ENOENT; 933 } 934 935 /* Single genpd case */ 936 if (!genpd_virt_devs) 937 return _set_required_opp(dev, dev, opp, 0); 938 939 /* Multiple genpd case */ 940 941 /* 942 * Acquire genpd_virt_dev_lock to make sure we don't use a genpd_dev 943 * after it is freed from another thread. 944 */ 945 mutex_lock(&opp_table->genpd_virt_dev_lock); 946 947 /* Scaling up? Set required OPPs in normal order, else reverse */ 948 if (up) { 949 for (i = 0; i < opp_table->required_opp_count; i++) { 950 ret = _set_required_opp(dev, genpd_virt_devs[i], opp, i); 951 if (ret) 952 break; 953 } 954 } else { 955 for (i = opp_table->required_opp_count - 1; i >= 0; i--) { 956 ret = _set_required_opp(dev, genpd_virt_devs[i], opp, i); 957 if (ret) 958 break; 959 } 960 } 961 962 mutex_unlock(&opp_table->genpd_virt_dev_lock); 963 964 return ret; 965 } 966 967 static void _find_current_opp(struct device *dev, struct opp_table *opp_table) 968 { 969 struct dev_pm_opp *opp = ERR_PTR(-ENODEV); 970 unsigned long freq; 971 972 if (!IS_ERR(opp_table->clk)) { 973 freq = clk_get_rate(opp_table->clk); 974 opp = _find_freq_ceil(opp_table, &freq); 975 } 976 977 /* 978 * Unable to find the current OPP ? Pick the first from the list since 979 * it is in ascending order, otherwise rest of the code will need to 980 * make special checks to validate current_opp. 981 */ 982 if (IS_ERR(opp)) { 983 mutex_lock(&opp_table->lock); 984 opp = list_first_entry(&opp_table->opp_list, struct dev_pm_opp, node); 985 dev_pm_opp_get(opp); 986 mutex_unlock(&opp_table->lock); 987 } 988 989 opp_table->current_opp = opp; 990 } 991 992 static int _disable_opp_table(struct device *dev, struct opp_table *opp_table) 993 { 994 int ret; 995 996 if (!opp_table->enabled) 997 return 0; 998 999 /* 1000 * Some drivers need to support cases where some platforms may 1001 * have OPP table for the device, while others don't and 1002 * opp_set_rate() just needs to behave like clk_set_rate(). 1003 */ 1004 if (!_get_opp_count(opp_table)) 1005 return 0; 1006 1007 ret = _set_opp_bw(opp_table, NULL, dev); 1008 if (ret) 1009 return ret; 1010 1011 if (opp_table->regulators) 1012 regulator_disable(opp_table->regulators[0]); 1013 1014 ret = _set_required_opps(dev, opp_table, NULL, false); 1015 1016 opp_table->enabled = false; 1017 return ret; 1018 } 1019 1020 static int _set_opp(struct device *dev, struct opp_table *opp_table, 1021 struct dev_pm_opp *opp, unsigned long freq) 1022 { 1023 struct dev_pm_opp *old_opp; 1024 int scaling_down, ret; 1025 1026 if (unlikely(!opp)) 1027 return _disable_opp_table(dev, opp_table); 1028 1029 /* Find the currently set OPP if we don't know already */ 1030 if (unlikely(!opp_table->current_opp)) 1031 _find_current_opp(dev, opp_table); 1032 1033 old_opp = opp_table->current_opp; 1034 1035 /* Return early if nothing to do */ 1036 if (old_opp == opp && opp_table->current_rate == freq && 1037 opp_table->enabled) { 1038 dev_dbg(dev, "%s: OPPs are same, nothing to do\n", __func__); 1039 return 0; 1040 } 1041 1042 dev_dbg(dev, "%s: switching OPP: Freq %lu -> %lu Hz, Level %u -> %u, Bw %u -> %u\n", 1043 __func__, opp_table->current_rate, freq, old_opp->level, 1044 opp->level, old_opp->bandwidth ? old_opp->bandwidth[0].peak : 0, 1045 opp->bandwidth ? opp->bandwidth[0].peak : 0); 1046 1047 scaling_down = _opp_compare_key(old_opp, opp); 1048 if (scaling_down == -1) 1049 scaling_down = 0; 1050 1051 /* Scaling up? Configure required OPPs before frequency */ 1052 if (!scaling_down) { 1053 ret = _set_required_opps(dev, opp_table, opp, true); 1054 if (ret) { 1055 dev_err(dev, "Failed to set required opps: %d\n", ret); 1056 return ret; 1057 } 1058 1059 ret = _set_opp_bw(opp_table, opp, dev); 1060 if (ret) { 1061 dev_err(dev, "Failed to set bw: %d\n", ret); 1062 return ret; 1063 } 1064 } 1065 1066 if (opp_table->set_opp) { 1067 ret = _set_opp_custom(opp_table, dev, opp, freq); 1068 } else if (opp_table->regulators) { 1069 ret = _generic_set_opp_regulator(opp_table, dev, opp, freq, 1070 scaling_down); 1071 } else { 1072 /* Only frequency scaling */ 1073 ret = _generic_set_opp_clk_only(dev, opp_table->clk, freq); 1074 } 1075 1076 if (ret) 1077 return ret; 1078 1079 /* Scaling down? Configure required OPPs after frequency */ 1080 if (scaling_down) { 1081 ret = _set_opp_bw(opp_table, opp, dev); 1082 if (ret) { 1083 dev_err(dev, "Failed to set bw: %d\n", ret); 1084 return ret; 1085 } 1086 1087 ret = _set_required_opps(dev, opp_table, opp, false); 1088 if (ret) { 1089 dev_err(dev, "Failed to set required opps: %d\n", ret); 1090 return ret; 1091 } 1092 } 1093 1094 opp_table->enabled = true; 1095 dev_pm_opp_put(old_opp); 1096 1097 /* Make sure current_opp doesn't get freed */ 1098 dev_pm_opp_get(opp); 1099 opp_table->current_opp = opp; 1100 opp_table->current_rate = freq; 1101 1102 return ret; 1103 } 1104 1105 /** 1106 * dev_pm_opp_set_rate() - Configure new OPP based on frequency 1107 * @dev: device for which we do this operation 1108 * @target_freq: frequency to achieve 1109 * 1110 * This configures the power-supplies to the levels specified by the OPP 1111 * corresponding to the target_freq, and programs the clock to a value <= 1112 * target_freq, as rounded by clk_round_rate(). Device wanting to run at fmax 1113 * provided by the opp, should have already rounded to the target OPP's 1114 * frequency. 1115 */ 1116 int dev_pm_opp_set_rate(struct device *dev, unsigned long target_freq) 1117 { 1118 struct opp_table *opp_table; 1119 unsigned long freq = 0, temp_freq; 1120 struct dev_pm_opp *opp = NULL; 1121 int ret; 1122 1123 opp_table = _find_opp_table(dev); 1124 if (IS_ERR(opp_table)) { 1125 dev_err(dev, "%s: device's opp table doesn't exist\n", __func__); 1126 return PTR_ERR(opp_table); 1127 } 1128 1129 if (target_freq) { 1130 /* 1131 * For IO devices which require an OPP on some platforms/SoCs 1132 * while just needing to scale the clock on some others 1133 * we look for empty OPP tables with just a clock handle and 1134 * scale only the clk. This makes dev_pm_opp_set_rate() 1135 * equivalent to a clk_set_rate() 1136 */ 1137 if (!_get_opp_count(opp_table)) { 1138 ret = _generic_set_opp_clk_only(dev, opp_table->clk, target_freq); 1139 goto put_opp_table; 1140 } 1141 1142 freq = clk_round_rate(opp_table->clk, target_freq); 1143 if ((long)freq <= 0) 1144 freq = target_freq; 1145 1146 /* 1147 * The clock driver may support finer resolution of the 1148 * frequencies than the OPP table, don't update the frequency we 1149 * pass to clk_set_rate() here. 1150 */ 1151 temp_freq = freq; 1152 opp = _find_freq_ceil(opp_table, &temp_freq); 1153 if (IS_ERR(opp)) { 1154 ret = PTR_ERR(opp); 1155 dev_err(dev, "%s: failed to find OPP for freq %lu (%d)\n", 1156 __func__, freq, ret); 1157 goto put_opp_table; 1158 } 1159 } 1160 1161 ret = _set_opp(dev, opp_table, opp, freq); 1162 1163 if (target_freq) 1164 dev_pm_opp_put(opp); 1165 put_opp_table: 1166 dev_pm_opp_put_opp_table(opp_table); 1167 return ret; 1168 } 1169 EXPORT_SYMBOL_GPL(dev_pm_opp_set_rate); 1170 1171 /** 1172 * dev_pm_opp_set_opp() - Configure device for OPP 1173 * @dev: device for which we do this operation 1174 * @opp: OPP to set to 1175 * 1176 * This configures the device based on the properties of the OPP passed to this 1177 * routine. 1178 * 1179 * Return: 0 on success, a negative error number otherwise. 1180 */ 1181 int dev_pm_opp_set_opp(struct device *dev, struct dev_pm_opp *opp) 1182 { 1183 struct opp_table *opp_table; 1184 int ret; 1185 1186 opp_table = _find_opp_table(dev); 1187 if (IS_ERR(opp_table)) { 1188 dev_err(dev, "%s: device opp doesn't exist\n", __func__); 1189 return PTR_ERR(opp_table); 1190 } 1191 1192 ret = _set_opp(dev, opp_table, opp, opp ? opp->rate : 0); 1193 dev_pm_opp_put_opp_table(opp_table); 1194 1195 return ret; 1196 } 1197 EXPORT_SYMBOL_GPL(dev_pm_opp_set_opp); 1198 1199 /* OPP-dev Helpers */ 1200 static void _remove_opp_dev(struct opp_device *opp_dev, 1201 struct opp_table *opp_table) 1202 { 1203 opp_debug_unregister(opp_dev, opp_table); 1204 list_del(&opp_dev->node); 1205 kfree(opp_dev); 1206 } 1207 1208 struct opp_device *_add_opp_dev(const struct device *dev, 1209 struct opp_table *opp_table) 1210 { 1211 struct opp_device *opp_dev; 1212 1213 opp_dev = kzalloc(sizeof(*opp_dev), GFP_KERNEL); 1214 if (!opp_dev) 1215 return NULL; 1216 1217 /* Initialize opp-dev */ 1218 opp_dev->dev = dev; 1219 1220 mutex_lock(&opp_table->lock); 1221 list_add(&opp_dev->node, &opp_table->dev_list); 1222 mutex_unlock(&opp_table->lock); 1223 1224 /* Create debugfs entries for the opp_table */ 1225 opp_debug_register(opp_dev, opp_table); 1226 1227 return opp_dev; 1228 } 1229 1230 static struct opp_table *_allocate_opp_table(struct device *dev, int index) 1231 { 1232 struct opp_table *opp_table; 1233 struct opp_device *opp_dev; 1234 int ret; 1235 1236 /* 1237 * Allocate a new OPP table. In the infrequent case where a new 1238 * device is needed to be added, we pay this penalty. 1239 */ 1240 opp_table = kzalloc(sizeof(*opp_table), GFP_KERNEL); 1241 if (!opp_table) 1242 return ERR_PTR(-ENOMEM); 1243 1244 mutex_init(&opp_table->lock); 1245 mutex_init(&opp_table->genpd_virt_dev_lock); 1246 INIT_LIST_HEAD(&opp_table->dev_list); 1247 INIT_LIST_HEAD(&opp_table->lazy); 1248 1249 /* Mark regulator count uninitialized */ 1250 opp_table->regulator_count = -1; 1251 1252 opp_dev = _add_opp_dev(dev, opp_table); 1253 if (!opp_dev) { 1254 ret = -ENOMEM; 1255 goto err; 1256 } 1257 1258 _of_init_opp_table(opp_table, dev, index); 1259 1260 /* Find interconnect path(s) for the device */ 1261 ret = dev_pm_opp_of_find_icc_paths(dev, opp_table); 1262 if (ret) { 1263 if (ret == -EPROBE_DEFER) 1264 goto remove_opp_dev; 1265 1266 dev_warn(dev, "%s: Error finding interconnect paths: %d\n", 1267 __func__, ret); 1268 } 1269 1270 BLOCKING_INIT_NOTIFIER_HEAD(&opp_table->head); 1271 INIT_LIST_HEAD(&opp_table->opp_list); 1272 kref_init(&opp_table->kref); 1273 1274 return opp_table; 1275 1276 remove_opp_dev: 1277 _remove_opp_dev(opp_dev, opp_table); 1278 err: 1279 kfree(opp_table); 1280 return ERR_PTR(ret); 1281 } 1282 1283 void _get_opp_table_kref(struct opp_table *opp_table) 1284 { 1285 kref_get(&opp_table->kref); 1286 } 1287 1288 static struct opp_table *_update_opp_table_clk(struct device *dev, 1289 struct opp_table *opp_table, 1290 bool getclk) 1291 { 1292 int ret; 1293 1294 /* 1295 * Return early if we don't need to get clk or we have already tried it 1296 * earlier. 1297 */ 1298 if (!getclk || IS_ERR(opp_table) || opp_table->clk) 1299 return opp_table; 1300 1301 /* Find clk for the device */ 1302 opp_table->clk = clk_get(dev, NULL); 1303 1304 ret = PTR_ERR_OR_ZERO(opp_table->clk); 1305 if (!ret) 1306 return opp_table; 1307 1308 if (ret == -ENOENT) { 1309 dev_dbg(dev, "%s: Couldn't find clock: %d\n", __func__, ret); 1310 return opp_table; 1311 } 1312 1313 dev_pm_opp_put_opp_table(opp_table); 1314 dev_err_probe(dev, ret, "Couldn't find clock\n"); 1315 1316 return ERR_PTR(ret); 1317 } 1318 1319 /* 1320 * We need to make sure that the OPP table for a device doesn't get added twice, 1321 * if this routine gets called in parallel with the same device pointer. 1322 * 1323 * The simplest way to enforce that is to perform everything (find existing 1324 * table and if not found, create a new one) under the opp_table_lock, so only 1325 * one creator gets access to the same. But that expands the critical section 1326 * under the lock and may end up causing circular dependencies with frameworks 1327 * like debugfs, interconnect or clock framework as they may be direct or 1328 * indirect users of OPP core. 1329 * 1330 * And for that reason we have to go for a bit tricky implementation here, which 1331 * uses the opp_tables_busy flag to indicate if another creator is in the middle 1332 * of adding an OPP table and others should wait for it to finish. 1333 */ 1334 struct opp_table *_add_opp_table_indexed(struct device *dev, int index, 1335 bool getclk) 1336 { 1337 struct opp_table *opp_table; 1338 1339 again: 1340 mutex_lock(&opp_table_lock); 1341 1342 opp_table = _find_opp_table_unlocked(dev); 1343 if (!IS_ERR(opp_table)) 1344 goto unlock; 1345 1346 /* 1347 * The opp_tables list or an OPP table's dev_list is getting updated by 1348 * another user, wait for it to finish. 1349 */ 1350 if (unlikely(opp_tables_busy)) { 1351 mutex_unlock(&opp_table_lock); 1352 cpu_relax(); 1353 goto again; 1354 } 1355 1356 opp_tables_busy = true; 1357 opp_table = _managed_opp(dev, index); 1358 1359 /* Drop the lock to reduce the size of critical section */ 1360 mutex_unlock(&opp_table_lock); 1361 1362 if (opp_table) { 1363 if (!_add_opp_dev(dev, opp_table)) { 1364 dev_pm_opp_put_opp_table(opp_table); 1365 opp_table = ERR_PTR(-ENOMEM); 1366 } 1367 1368 mutex_lock(&opp_table_lock); 1369 } else { 1370 opp_table = _allocate_opp_table(dev, index); 1371 1372 mutex_lock(&opp_table_lock); 1373 if (!IS_ERR(opp_table)) 1374 list_add(&opp_table->node, &opp_tables); 1375 } 1376 1377 opp_tables_busy = false; 1378 1379 unlock: 1380 mutex_unlock(&opp_table_lock); 1381 1382 return _update_opp_table_clk(dev, opp_table, getclk); 1383 } 1384 1385 static struct opp_table *_add_opp_table(struct device *dev, bool getclk) 1386 { 1387 return _add_opp_table_indexed(dev, 0, getclk); 1388 } 1389 1390 struct opp_table *dev_pm_opp_get_opp_table(struct device *dev) 1391 { 1392 return _find_opp_table(dev); 1393 } 1394 EXPORT_SYMBOL_GPL(dev_pm_opp_get_opp_table); 1395 1396 static void _opp_table_kref_release(struct kref *kref) 1397 { 1398 struct opp_table *opp_table = container_of(kref, struct opp_table, kref); 1399 struct opp_device *opp_dev, *temp; 1400 int i; 1401 1402 /* Drop the lock as soon as we can */ 1403 list_del(&opp_table->node); 1404 mutex_unlock(&opp_table_lock); 1405 1406 if (opp_table->current_opp) 1407 dev_pm_opp_put(opp_table->current_opp); 1408 1409 _of_clear_opp_table(opp_table); 1410 1411 /* Release clk */ 1412 if (!IS_ERR(opp_table->clk)) 1413 clk_put(opp_table->clk); 1414 1415 if (opp_table->paths) { 1416 for (i = 0; i < opp_table->path_count; i++) 1417 icc_put(opp_table->paths[i]); 1418 kfree(opp_table->paths); 1419 } 1420 1421 WARN_ON(!list_empty(&opp_table->opp_list)); 1422 1423 list_for_each_entry_safe(opp_dev, temp, &opp_table->dev_list, node) { 1424 /* 1425 * The OPP table is getting removed, drop the performance state 1426 * constraints. 1427 */ 1428 if (opp_table->genpd_performance_state) 1429 dev_pm_genpd_set_performance_state((struct device *)(opp_dev->dev), 0); 1430 1431 _remove_opp_dev(opp_dev, opp_table); 1432 } 1433 1434 mutex_destroy(&opp_table->genpd_virt_dev_lock); 1435 mutex_destroy(&opp_table->lock); 1436 kfree(opp_table); 1437 } 1438 1439 void dev_pm_opp_put_opp_table(struct opp_table *opp_table) 1440 { 1441 kref_put_mutex(&opp_table->kref, _opp_table_kref_release, 1442 &opp_table_lock); 1443 } 1444 EXPORT_SYMBOL_GPL(dev_pm_opp_put_opp_table); 1445 1446 void _opp_free(struct dev_pm_opp *opp) 1447 { 1448 kfree(opp); 1449 } 1450 1451 static void _opp_kref_release(struct kref *kref) 1452 { 1453 struct dev_pm_opp *opp = container_of(kref, struct dev_pm_opp, kref); 1454 struct opp_table *opp_table = opp->opp_table; 1455 1456 list_del(&opp->node); 1457 mutex_unlock(&opp_table->lock); 1458 1459 /* 1460 * Notify the changes in the availability of the operable 1461 * frequency/voltage list. 1462 */ 1463 blocking_notifier_call_chain(&opp_table->head, OPP_EVENT_REMOVE, opp); 1464 _of_opp_free_required_opps(opp_table, opp); 1465 opp_debug_remove_one(opp); 1466 kfree(opp); 1467 } 1468 1469 void dev_pm_opp_get(struct dev_pm_opp *opp) 1470 { 1471 kref_get(&opp->kref); 1472 } 1473 1474 void dev_pm_opp_put(struct dev_pm_opp *opp) 1475 { 1476 kref_put_mutex(&opp->kref, _opp_kref_release, &opp->opp_table->lock); 1477 } 1478 EXPORT_SYMBOL_GPL(dev_pm_opp_put); 1479 1480 /** 1481 * dev_pm_opp_remove() - Remove an OPP from OPP table 1482 * @dev: device for which we do this operation 1483 * @freq: OPP to remove with matching 'freq' 1484 * 1485 * This function removes an opp from the opp table. 1486 */ 1487 void dev_pm_opp_remove(struct device *dev, unsigned long freq) 1488 { 1489 struct dev_pm_opp *opp = NULL, *iter; 1490 struct opp_table *opp_table; 1491 1492 opp_table = _find_opp_table(dev); 1493 if (IS_ERR(opp_table)) 1494 return; 1495 1496 mutex_lock(&opp_table->lock); 1497 1498 list_for_each_entry(iter, &opp_table->opp_list, node) { 1499 if (iter->rate == freq) { 1500 opp = iter; 1501 break; 1502 } 1503 } 1504 1505 mutex_unlock(&opp_table->lock); 1506 1507 if (opp) { 1508 dev_pm_opp_put(opp); 1509 1510 /* Drop the reference taken by dev_pm_opp_add() */ 1511 dev_pm_opp_put_opp_table(opp_table); 1512 } else { 1513 dev_warn(dev, "%s: Couldn't find OPP with freq: %lu\n", 1514 __func__, freq); 1515 } 1516 1517 /* Drop the reference taken by _find_opp_table() */ 1518 dev_pm_opp_put_opp_table(opp_table); 1519 } 1520 EXPORT_SYMBOL_GPL(dev_pm_opp_remove); 1521 1522 static struct dev_pm_opp *_opp_get_next(struct opp_table *opp_table, 1523 bool dynamic) 1524 { 1525 struct dev_pm_opp *opp = NULL, *temp; 1526 1527 mutex_lock(&opp_table->lock); 1528 list_for_each_entry(temp, &opp_table->opp_list, node) { 1529 /* 1530 * Refcount must be dropped only once for each OPP by OPP core, 1531 * do that with help of "removed" flag. 1532 */ 1533 if (!temp->removed && dynamic == temp->dynamic) { 1534 opp = temp; 1535 break; 1536 } 1537 } 1538 1539 mutex_unlock(&opp_table->lock); 1540 return opp; 1541 } 1542 1543 /* 1544 * Can't call dev_pm_opp_put() from under the lock as debugfs removal needs to 1545 * happen lock less to avoid circular dependency issues. This routine must be 1546 * called without the opp_table->lock held. 1547 */ 1548 static void _opp_remove_all(struct opp_table *opp_table, bool dynamic) 1549 { 1550 struct dev_pm_opp *opp; 1551 1552 while ((opp = _opp_get_next(opp_table, dynamic))) { 1553 opp->removed = true; 1554 dev_pm_opp_put(opp); 1555 1556 /* Drop the references taken by dev_pm_opp_add() */ 1557 if (dynamic) 1558 dev_pm_opp_put_opp_table(opp_table); 1559 } 1560 } 1561 1562 bool _opp_remove_all_static(struct opp_table *opp_table) 1563 { 1564 mutex_lock(&opp_table->lock); 1565 1566 if (!opp_table->parsed_static_opps) { 1567 mutex_unlock(&opp_table->lock); 1568 return false; 1569 } 1570 1571 if (--opp_table->parsed_static_opps) { 1572 mutex_unlock(&opp_table->lock); 1573 return true; 1574 } 1575 1576 mutex_unlock(&opp_table->lock); 1577 1578 _opp_remove_all(opp_table, false); 1579 return true; 1580 } 1581 1582 /** 1583 * dev_pm_opp_remove_all_dynamic() - Remove all dynamically created OPPs 1584 * @dev: device for which we do this operation 1585 * 1586 * This function removes all dynamically created OPPs from the opp table. 1587 */ 1588 void dev_pm_opp_remove_all_dynamic(struct device *dev) 1589 { 1590 struct opp_table *opp_table; 1591 1592 opp_table = _find_opp_table(dev); 1593 if (IS_ERR(opp_table)) 1594 return; 1595 1596 _opp_remove_all(opp_table, true); 1597 1598 /* Drop the reference taken by _find_opp_table() */ 1599 dev_pm_opp_put_opp_table(opp_table); 1600 } 1601 EXPORT_SYMBOL_GPL(dev_pm_opp_remove_all_dynamic); 1602 1603 struct dev_pm_opp *_opp_allocate(struct opp_table *table) 1604 { 1605 struct dev_pm_opp *opp; 1606 int supply_count, supply_size, icc_size; 1607 1608 /* Allocate space for at least one supply */ 1609 supply_count = table->regulator_count > 0 ? table->regulator_count : 1; 1610 supply_size = sizeof(*opp->supplies) * supply_count; 1611 icc_size = sizeof(*opp->bandwidth) * table->path_count; 1612 1613 /* allocate new OPP node and supplies structures */ 1614 opp = kzalloc(sizeof(*opp) + supply_size + icc_size, GFP_KERNEL); 1615 1616 if (!opp) 1617 return NULL; 1618 1619 /* Put the supplies at the end of the OPP structure as an empty array */ 1620 opp->supplies = (struct dev_pm_opp_supply *)(opp + 1); 1621 if (icc_size) 1622 opp->bandwidth = (struct dev_pm_opp_icc_bw *)(opp->supplies + supply_count); 1623 INIT_LIST_HEAD(&opp->node); 1624 1625 return opp; 1626 } 1627 1628 static bool _opp_supported_by_regulators(struct dev_pm_opp *opp, 1629 struct opp_table *opp_table) 1630 { 1631 struct regulator *reg; 1632 int i; 1633 1634 if (!opp_table->regulators) 1635 return true; 1636 1637 for (i = 0; i < opp_table->regulator_count; i++) { 1638 reg = opp_table->regulators[i]; 1639 1640 if (!regulator_is_supported_voltage(reg, 1641 opp->supplies[i].u_volt_min, 1642 opp->supplies[i].u_volt_max)) { 1643 pr_warn("%s: OPP minuV: %lu maxuV: %lu, not supported by regulator\n", 1644 __func__, opp->supplies[i].u_volt_min, 1645 opp->supplies[i].u_volt_max); 1646 return false; 1647 } 1648 } 1649 1650 return true; 1651 } 1652 1653 int _opp_compare_key(struct dev_pm_opp *opp1, struct dev_pm_opp *opp2) 1654 { 1655 if (opp1->rate != opp2->rate) 1656 return opp1->rate < opp2->rate ? -1 : 1; 1657 if (opp1->bandwidth && opp2->bandwidth && 1658 opp1->bandwidth[0].peak != opp2->bandwidth[0].peak) 1659 return opp1->bandwidth[0].peak < opp2->bandwidth[0].peak ? -1 : 1; 1660 if (opp1->level != opp2->level) 1661 return opp1->level < opp2->level ? -1 : 1; 1662 return 0; 1663 } 1664 1665 static int _opp_is_duplicate(struct device *dev, struct dev_pm_opp *new_opp, 1666 struct opp_table *opp_table, 1667 struct list_head **head) 1668 { 1669 struct dev_pm_opp *opp; 1670 int opp_cmp; 1671 1672 /* 1673 * Insert new OPP in order of increasing frequency and discard if 1674 * already present. 1675 * 1676 * Need to use &opp_table->opp_list in the condition part of the 'for' 1677 * loop, don't replace it with head otherwise it will become an infinite 1678 * loop. 1679 */ 1680 list_for_each_entry(opp, &opp_table->opp_list, node) { 1681 opp_cmp = _opp_compare_key(new_opp, opp); 1682 if (opp_cmp > 0) { 1683 *head = &opp->node; 1684 continue; 1685 } 1686 1687 if (opp_cmp < 0) 1688 return 0; 1689 1690 /* Duplicate OPPs */ 1691 dev_warn(dev, "%s: duplicate OPPs detected. Existing: freq: %lu, volt: %lu, enabled: %d. New: freq: %lu, volt: %lu, enabled: %d\n", 1692 __func__, opp->rate, opp->supplies[0].u_volt, 1693 opp->available, new_opp->rate, 1694 new_opp->supplies[0].u_volt, new_opp->available); 1695 1696 /* Should we compare voltages for all regulators here ? */ 1697 return opp->available && 1698 new_opp->supplies[0].u_volt == opp->supplies[0].u_volt ? -EBUSY : -EEXIST; 1699 } 1700 1701 return 0; 1702 } 1703 1704 void _required_opps_available(struct dev_pm_opp *opp, int count) 1705 { 1706 int i; 1707 1708 for (i = 0; i < count; i++) { 1709 if (opp->required_opps[i]->available) 1710 continue; 1711 1712 opp->available = false; 1713 pr_warn("%s: OPP not supported by required OPP %pOF (%lu)\n", 1714 __func__, opp->required_opps[i]->np, opp->rate); 1715 return; 1716 } 1717 } 1718 1719 /* 1720 * Returns: 1721 * 0: On success. And appropriate error message for duplicate OPPs. 1722 * -EBUSY: For OPP with same freq/volt and is available. The callers of 1723 * _opp_add() must return 0 if they receive -EBUSY from it. This is to make 1724 * sure we don't print error messages unnecessarily if different parts of 1725 * kernel try to initialize the OPP table. 1726 * -EEXIST: For OPP with same freq but different volt or is unavailable. This 1727 * should be considered an error by the callers of _opp_add(). 1728 */ 1729 int _opp_add(struct device *dev, struct dev_pm_opp *new_opp, 1730 struct opp_table *opp_table, bool rate_not_available) 1731 { 1732 struct list_head *head; 1733 int ret; 1734 1735 mutex_lock(&opp_table->lock); 1736 head = &opp_table->opp_list; 1737 1738 ret = _opp_is_duplicate(dev, new_opp, opp_table, &head); 1739 if (ret) { 1740 mutex_unlock(&opp_table->lock); 1741 return ret; 1742 } 1743 1744 list_add(&new_opp->node, head); 1745 mutex_unlock(&opp_table->lock); 1746 1747 new_opp->opp_table = opp_table; 1748 kref_init(&new_opp->kref); 1749 1750 opp_debug_create_one(new_opp, opp_table); 1751 1752 if (!_opp_supported_by_regulators(new_opp, opp_table)) { 1753 new_opp->available = false; 1754 dev_warn(dev, "%s: OPP not supported by regulators (%lu)\n", 1755 __func__, new_opp->rate); 1756 } 1757 1758 /* required-opps not fully initialized yet */ 1759 if (lazy_linking_pending(opp_table)) 1760 return 0; 1761 1762 _required_opps_available(new_opp, opp_table->required_opp_count); 1763 1764 return 0; 1765 } 1766 1767 /** 1768 * _opp_add_v1() - Allocate a OPP based on v1 bindings. 1769 * @opp_table: OPP table 1770 * @dev: device for which we do this operation 1771 * @freq: Frequency in Hz for this OPP 1772 * @u_volt: Voltage in uVolts for this OPP 1773 * @dynamic: Dynamically added OPPs. 1774 * 1775 * This function adds an opp definition to the opp table and returns status. 1776 * The opp is made available by default and it can be controlled using 1777 * dev_pm_opp_enable/disable functions and may be removed by dev_pm_opp_remove. 1778 * 1779 * NOTE: "dynamic" parameter impacts OPPs added by the dev_pm_opp_of_add_table 1780 * and freed by dev_pm_opp_of_remove_table. 1781 * 1782 * Return: 1783 * 0 On success OR 1784 * Duplicate OPPs (both freq and volt are same) and opp->available 1785 * -EEXIST Freq are same and volt are different OR 1786 * Duplicate OPPs (both freq and volt are same) and !opp->available 1787 * -ENOMEM Memory allocation failure 1788 */ 1789 int _opp_add_v1(struct opp_table *opp_table, struct device *dev, 1790 unsigned long freq, long u_volt, bool dynamic) 1791 { 1792 struct dev_pm_opp *new_opp; 1793 unsigned long tol; 1794 int ret; 1795 1796 new_opp = _opp_allocate(opp_table); 1797 if (!new_opp) 1798 return -ENOMEM; 1799 1800 /* populate the opp table */ 1801 new_opp->rate = freq; 1802 tol = u_volt * opp_table->voltage_tolerance_v1 / 100; 1803 new_opp->supplies[0].u_volt = u_volt; 1804 new_opp->supplies[0].u_volt_min = u_volt - tol; 1805 new_opp->supplies[0].u_volt_max = u_volt + tol; 1806 new_opp->available = true; 1807 new_opp->dynamic = dynamic; 1808 1809 ret = _opp_add(dev, new_opp, opp_table, false); 1810 if (ret) { 1811 /* Don't return error for duplicate OPPs */ 1812 if (ret == -EBUSY) 1813 ret = 0; 1814 goto free_opp; 1815 } 1816 1817 /* 1818 * Notify the changes in the availability of the operable 1819 * frequency/voltage list. 1820 */ 1821 blocking_notifier_call_chain(&opp_table->head, OPP_EVENT_ADD, new_opp); 1822 return 0; 1823 1824 free_opp: 1825 _opp_free(new_opp); 1826 1827 return ret; 1828 } 1829 1830 /** 1831 * dev_pm_opp_set_supported_hw() - Set supported platforms 1832 * @dev: Device for which supported-hw has to be set. 1833 * @versions: Array of hierarchy of versions to match. 1834 * @count: Number of elements in the array. 1835 * 1836 * This is required only for the V2 bindings, and it enables a platform to 1837 * specify the hierarchy of versions it supports. OPP layer will then enable 1838 * OPPs, which are available for those versions, based on its 'opp-supported-hw' 1839 * property. 1840 */ 1841 struct opp_table *dev_pm_opp_set_supported_hw(struct device *dev, 1842 const u32 *versions, unsigned int count) 1843 { 1844 struct opp_table *opp_table; 1845 1846 opp_table = _add_opp_table(dev, false); 1847 if (IS_ERR(opp_table)) 1848 return opp_table; 1849 1850 /* Make sure there are no concurrent readers while updating opp_table */ 1851 WARN_ON(!list_empty(&opp_table->opp_list)); 1852 1853 /* Another CPU that shares the OPP table has set the property ? */ 1854 if (opp_table->supported_hw) 1855 return opp_table; 1856 1857 opp_table->supported_hw = kmemdup(versions, count * sizeof(*versions), 1858 GFP_KERNEL); 1859 if (!opp_table->supported_hw) { 1860 dev_pm_opp_put_opp_table(opp_table); 1861 return ERR_PTR(-ENOMEM); 1862 } 1863 1864 opp_table->supported_hw_count = count; 1865 1866 return opp_table; 1867 } 1868 EXPORT_SYMBOL_GPL(dev_pm_opp_set_supported_hw); 1869 1870 /** 1871 * dev_pm_opp_put_supported_hw() - Releases resources blocked for supported hw 1872 * @opp_table: OPP table returned by dev_pm_opp_set_supported_hw(). 1873 * 1874 * This is required only for the V2 bindings, and is called for a matching 1875 * dev_pm_opp_set_supported_hw(). Until this is called, the opp_table structure 1876 * will not be freed. 1877 */ 1878 void dev_pm_opp_put_supported_hw(struct opp_table *opp_table) 1879 { 1880 if (unlikely(!opp_table)) 1881 return; 1882 1883 kfree(opp_table->supported_hw); 1884 opp_table->supported_hw = NULL; 1885 opp_table->supported_hw_count = 0; 1886 1887 dev_pm_opp_put_opp_table(opp_table); 1888 } 1889 EXPORT_SYMBOL_GPL(dev_pm_opp_put_supported_hw); 1890 1891 static void devm_pm_opp_supported_hw_release(void *data) 1892 { 1893 dev_pm_opp_put_supported_hw(data); 1894 } 1895 1896 /** 1897 * devm_pm_opp_set_supported_hw() - Set supported platforms 1898 * @dev: Device for which supported-hw has to be set. 1899 * @versions: Array of hierarchy of versions to match. 1900 * @count: Number of elements in the array. 1901 * 1902 * This is a resource-managed variant of dev_pm_opp_set_supported_hw(). 1903 * 1904 * Return: 0 on success and errorno otherwise. 1905 */ 1906 int devm_pm_opp_set_supported_hw(struct device *dev, const u32 *versions, 1907 unsigned int count) 1908 { 1909 struct opp_table *opp_table; 1910 1911 opp_table = dev_pm_opp_set_supported_hw(dev, versions, count); 1912 if (IS_ERR(opp_table)) 1913 return PTR_ERR(opp_table); 1914 1915 return devm_add_action_or_reset(dev, devm_pm_opp_supported_hw_release, 1916 opp_table); 1917 } 1918 EXPORT_SYMBOL_GPL(devm_pm_opp_set_supported_hw); 1919 1920 /** 1921 * dev_pm_opp_set_prop_name() - Set prop-extn name 1922 * @dev: Device for which the prop-name has to be set. 1923 * @name: name to postfix to properties. 1924 * 1925 * This is required only for the V2 bindings, and it enables a platform to 1926 * specify the extn to be used for certain property names. The properties to 1927 * which the extension will apply are opp-microvolt and opp-microamp. OPP core 1928 * should postfix the property name with -<name> while looking for them. 1929 */ 1930 struct opp_table *dev_pm_opp_set_prop_name(struct device *dev, const char *name) 1931 { 1932 struct opp_table *opp_table; 1933 1934 opp_table = _add_opp_table(dev, false); 1935 if (IS_ERR(opp_table)) 1936 return opp_table; 1937 1938 /* Make sure there are no concurrent readers while updating opp_table */ 1939 WARN_ON(!list_empty(&opp_table->opp_list)); 1940 1941 /* Another CPU that shares the OPP table has set the property ? */ 1942 if (opp_table->prop_name) 1943 return opp_table; 1944 1945 opp_table->prop_name = kstrdup(name, GFP_KERNEL); 1946 if (!opp_table->prop_name) { 1947 dev_pm_opp_put_opp_table(opp_table); 1948 return ERR_PTR(-ENOMEM); 1949 } 1950 1951 return opp_table; 1952 } 1953 EXPORT_SYMBOL_GPL(dev_pm_opp_set_prop_name); 1954 1955 /** 1956 * dev_pm_opp_put_prop_name() - Releases resources blocked for prop-name 1957 * @opp_table: OPP table returned by dev_pm_opp_set_prop_name(). 1958 * 1959 * This is required only for the V2 bindings, and is called for a matching 1960 * dev_pm_opp_set_prop_name(). Until this is called, the opp_table structure 1961 * will not be freed. 1962 */ 1963 void dev_pm_opp_put_prop_name(struct opp_table *opp_table) 1964 { 1965 if (unlikely(!opp_table)) 1966 return; 1967 1968 kfree(opp_table->prop_name); 1969 opp_table->prop_name = NULL; 1970 1971 dev_pm_opp_put_opp_table(opp_table); 1972 } 1973 EXPORT_SYMBOL_GPL(dev_pm_opp_put_prop_name); 1974 1975 /** 1976 * dev_pm_opp_set_regulators() - Set regulator names for the device 1977 * @dev: Device for which regulator name is being set. 1978 * @names: Array of pointers to the names of the regulator. 1979 * @count: Number of regulators. 1980 * 1981 * In order to support OPP switching, OPP layer needs to know the name of the 1982 * device's regulators, as the core would be required to switch voltages as 1983 * well. 1984 * 1985 * This must be called before any OPPs are initialized for the device. 1986 */ 1987 struct opp_table *dev_pm_opp_set_regulators(struct device *dev, 1988 const char * const names[], 1989 unsigned int count) 1990 { 1991 struct dev_pm_opp_supply *supplies; 1992 struct opp_table *opp_table; 1993 struct regulator *reg; 1994 int ret, i; 1995 1996 opp_table = _add_opp_table(dev, false); 1997 if (IS_ERR(opp_table)) 1998 return opp_table; 1999 2000 /* This should be called before OPPs are initialized */ 2001 if (WARN_ON(!list_empty(&opp_table->opp_list))) { 2002 ret = -EBUSY; 2003 goto err; 2004 } 2005 2006 /* Another CPU that shares the OPP table has set the regulators ? */ 2007 if (opp_table->regulators) 2008 return opp_table; 2009 2010 opp_table->regulators = kmalloc_array(count, 2011 sizeof(*opp_table->regulators), 2012 GFP_KERNEL); 2013 if (!opp_table->regulators) { 2014 ret = -ENOMEM; 2015 goto err; 2016 } 2017 2018 for (i = 0; i < count; i++) { 2019 reg = regulator_get_optional(dev, names[i]); 2020 if (IS_ERR(reg)) { 2021 ret = dev_err_probe(dev, PTR_ERR(reg), 2022 "%s: no regulator (%s) found\n", 2023 __func__, names[i]); 2024 goto free_regulators; 2025 } 2026 2027 opp_table->regulators[i] = reg; 2028 } 2029 2030 opp_table->regulator_count = count; 2031 2032 supplies = kmalloc_array(count * 2, sizeof(*supplies), GFP_KERNEL); 2033 if (!supplies) { 2034 ret = -ENOMEM; 2035 goto free_regulators; 2036 } 2037 2038 mutex_lock(&opp_table->lock); 2039 opp_table->sod_supplies = supplies; 2040 if (opp_table->set_opp_data) { 2041 opp_table->set_opp_data->old_opp.supplies = supplies; 2042 opp_table->set_opp_data->new_opp.supplies = supplies + count; 2043 } 2044 mutex_unlock(&opp_table->lock); 2045 2046 return opp_table; 2047 2048 free_regulators: 2049 while (i != 0) 2050 regulator_put(opp_table->regulators[--i]); 2051 2052 kfree(opp_table->regulators); 2053 opp_table->regulators = NULL; 2054 opp_table->regulator_count = -1; 2055 err: 2056 dev_pm_opp_put_opp_table(opp_table); 2057 2058 return ERR_PTR(ret); 2059 } 2060 EXPORT_SYMBOL_GPL(dev_pm_opp_set_regulators); 2061 2062 /** 2063 * dev_pm_opp_put_regulators() - Releases resources blocked for regulator 2064 * @opp_table: OPP table returned from dev_pm_opp_set_regulators(). 2065 */ 2066 void dev_pm_opp_put_regulators(struct opp_table *opp_table) 2067 { 2068 int i; 2069 2070 if (unlikely(!opp_table)) 2071 return; 2072 2073 if (!opp_table->regulators) 2074 goto put_opp_table; 2075 2076 if (opp_table->enabled) { 2077 for (i = opp_table->regulator_count - 1; i >= 0; i--) 2078 regulator_disable(opp_table->regulators[i]); 2079 } 2080 2081 for (i = opp_table->regulator_count - 1; i >= 0; i--) 2082 regulator_put(opp_table->regulators[i]); 2083 2084 mutex_lock(&opp_table->lock); 2085 if (opp_table->set_opp_data) { 2086 opp_table->set_opp_data->old_opp.supplies = NULL; 2087 opp_table->set_opp_data->new_opp.supplies = NULL; 2088 } 2089 2090 kfree(opp_table->sod_supplies); 2091 opp_table->sod_supplies = NULL; 2092 mutex_unlock(&opp_table->lock); 2093 2094 kfree(opp_table->regulators); 2095 opp_table->regulators = NULL; 2096 opp_table->regulator_count = -1; 2097 2098 put_opp_table: 2099 dev_pm_opp_put_opp_table(opp_table); 2100 } 2101 EXPORT_SYMBOL_GPL(dev_pm_opp_put_regulators); 2102 2103 static void devm_pm_opp_regulators_release(void *data) 2104 { 2105 dev_pm_opp_put_regulators(data); 2106 } 2107 2108 /** 2109 * devm_pm_opp_set_regulators() - Set regulator names for the device 2110 * @dev: Device for which regulator name is being set. 2111 * @names: Array of pointers to the names of the regulator. 2112 * @count: Number of regulators. 2113 * 2114 * This is a resource-managed variant of dev_pm_opp_set_regulators(). 2115 * 2116 * Return: 0 on success and errorno otherwise. 2117 */ 2118 int devm_pm_opp_set_regulators(struct device *dev, 2119 const char * const names[], 2120 unsigned int count) 2121 { 2122 struct opp_table *opp_table; 2123 2124 opp_table = dev_pm_opp_set_regulators(dev, names, count); 2125 if (IS_ERR(opp_table)) 2126 return PTR_ERR(opp_table); 2127 2128 return devm_add_action_or_reset(dev, devm_pm_opp_regulators_release, 2129 opp_table); 2130 } 2131 EXPORT_SYMBOL_GPL(devm_pm_opp_set_regulators); 2132 2133 /** 2134 * dev_pm_opp_set_clkname() - Set clk name for the device 2135 * @dev: Device for which clk name is being set. 2136 * @name: Clk name. 2137 * 2138 * In order to support OPP switching, OPP layer needs to get pointer to the 2139 * clock for the device. Simple cases work fine without using this routine (i.e. 2140 * by passing connection-id as NULL), but for a device with multiple clocks 2141 * available, the OPP core needs to know the exact name of the clk to use. 2142 * 2143 * This must be called before any OPPs are initialized for the device. 2144 */ 2145 struct opp_table *dev_pm_opp_set_clkname(struct device *dev, const char *name) 2146 { 2147 struct opp_table *opp_table; 2148 int ret; 2149 2150 opp_table = _add_opp_table(dev, false); 2151 if (IS_ERR(opp_table)) 2152 return opp_table; 2153 2154 /* This should be called before OPPs are initialized */ 2155 if (WARN_ON(!list_empty(&opp_table->opp_list))) { 2156 ret = -EBUSY; 2157 goto err; 2158 } 2159 2160 /* clk shouldn't be initialized at this point */ 2161 if (WARN_ON(opp_table->clk)) { 2162 ret = -EBUSY; 2163 goto err; 2164 } 2165 2166 /* Find clk for the device */ 2167 opp_table->clk = clk_get(dev, name); 2168 if (IS_ERR(opp_table->clk)) { 2169 ret = dev_err_probe(dev, PTR_ERR(opp_table->clk), 2170 "%s: Couldn't find clock\n", __func__); 2171 goto err; 2172 } 2173 2174 return opp_table; 2175 2176 err: 2177 dev_pm_opp_put_opp_table(opp_table); 2178 2179 return ERR_PTR(ret); 2180 } 2181 EXPORT_SYMBOL_GPL(dev_pm_opp_set_clkname); 2182 2183 /** 2184 * dev_pm_opp_put_clkname() - Releases resources blocked for clk. 2185 * @opp_table: OPP table returned from dev_pm_opp_set_clkname(). 2186 */ 2187 void dev_pm_opp_put_clkname(struct opp_table *opp_table) 2188 { 2189 if (unlikely(!opp_table)) 2190 return; 2191 2192 clk_put(opp_table->clk); 2193 opp_table->clk = ERR_PTR(-EINVAL); 2194 2195 dev_pm_opp_put_opp_table(opp_table); 2196 } 2197 EXPORT_SYMBOL_GPL(dev_pm_opp_put_clkname); 2198 2199 static void devm_pm_opp_clkname_release(void *data) 2200 { 2201 dev_pm_opp_put_clkname(data); 2202 } 2203 2204 /** 2205 * devm_pm_opp_set_clkname() - Set clk name for the device 2206 * @dev: Device for which clk name is being set. 2207 * @name: Clk name. 2208 * 2209 * This is a resource-managed variant of dev_pm_opp_set_clkname(). 2210 * 2211 * Return: 0 on success and errorno otherwise. 2212 */ 2213 int devm_pm_opp_set_clkname(struct device *dev, const char *name) 2214 { 2215 struct opp_table *opp_table; 2216 2217 opp_table = dev_pm_opp_set_clkname(dev, name); 2218 if (IS_ERR(opp_table)) 2219 return PTR_ERR(opp_table); 2220 2221 return devm_add_action_or_reset(dev, devm_pm_opp_clkname_release, 2222 opp_table); 2223 } 2224 EXPORT_SYMBOL_GPL(devm_pm_opp_set_clkname); 2225 2226 /** 2227 * dev_pm_opp_register_set_opp_helper() - Register custom set OPP helper 2228 * @dev: Device for which the helper is getting registered. 2229 * @set_opp: Custom set OPP helper. 2230 * 2231 * This is useful to support complex platforms (like platforms with multiple 2232 * regulators per device), instead of the generic OPP set rate helper. 2233 * 2234 * This must be called before any OPPs are initialized for the device. 2235 */ 2236 struct opp_table *dev_pm_opp_register_set_opp_helper(struct device *dev, 2237 int (*set_opp)(struct dev_pm_set_opp_data *data)) 2238 { 2239 struct dev_pm_set_opp_data *data; 2240 struct opp_table *opp_table; 2241 2242 if (!set_opp) 2243 return ERR_PTR(-EINVAL); 2244 2245 opp_table = _add_opp_table(dev, false); 2246 if (IS_ERR(opp_table)) 2247 return opp_table; 2248 2249 /* This should be called before OPPs are initialized */ 2250 if (WARN_ON(!list_empty(&opp_table->opp_list))) { 2251 dev_pm_opp_put_opp_table(opp_table); 2252 return ERR_PTR(-EBUSY); 2253 } 2254 2255 /* Another CPU that shares the OPP table has set the helper ? */ 2256 if (opp_table->set_opp) 2257 return opp_table; 2258 2259 data = kzalloc(sizeof(*data), GFP_KERNEL); 2260 if (!data) 2261 return ERR_PTR(-ENOMEM); 2262 2263 mutex_lock(&opp_table->lock); 2264 opp_table->set_opp_data = data; 2265 if (opp_table->sod_supplies) { 2266 data->old_opp.supplies = opp_table->sod_supplies; 2267 data->new_opp.supplies = opp_table->sod_supplies + 2268 opp_table->regulator_count; 2269 } 2270 mutex_unlock(&opp_table->lock); 2271 2272 opp_table->set_opp = set_opp; 2273 2274 return opp_table; 2275 } 2276 EXPORT_SYMBOL_GPL(dev_pm_opp_register_set_opp_helper); 2277 2278 /** 2279 * dev_pm_opp_unregister_set_opp_helper() - Releases resources blocked for 2280 * set_opp helper 2281 * @opp_table: OPP table returned from dev_pm_opp_register_set_opp_helper(). 2282 * 2283 * Release resources blocked for platform specific set_opp helper. 2284 */ 2285 void dev_pm_opp_unregister_set_opp_helper(struct opp_table *opp_table) 2286 { 2287 if (unlikely(!opp_table)) 2288 return; 2289 2290 opp_table->set_opp = NULL; 2291 2292 mutex_lock(&opp_table->lock); 2293 kfree(opp_table->set_opp_data); 2294 opp_table->set_opp_data = NULL; 2295 mutex_unlock(&opp_table->lock); 2296 2297 dev_pm_opp_put_opp_table(opp_table); 2298 } 2299 EXPORT_SYMBOL_GPL(dev_pm_opp_unregister_set_opp_helper); 2300 2301 static void devm_pm_opp_unregister_set_opp_helper(void *data) 2302 { 2303 dev_pm_opp_unregister_set_opp_helper(data); 2304 } 2305 2306 /** 2307 * devm_pm_opp_register_set_opp_helper() - Register custom set OPP helper 2308 * @dev: Device for which the helper is getting registered. 2309 * @set_opp: Custom set OPP helper. 2310 * 2311 * This is a resource-managed version of dev_pm_opp_register_set_opp_helper(). 2312 * 2313 * Return: 0 on success and errorno otherwise. 2314 */ 2315 int devm_pm_opp_register_set_opp_helper(struct device *dev, 2316 int (*set_opp)(struct dev_pm_set_opp_data *data)) 2317 { 2318 struct opp_table *opp_table; 2319 2320 opp_table = dev_pm_opp_register_set_opp_helper(dev, set_opp); 2321 if (IS_ERR(opp_table)) 2322 return PTR_ERR(opp_table); 2323 2324 return devm_add_action_or_reset(dev, devm_pm_opp_unregister_set_opp_helper, 2325 opp_table); 2326 } 2327 EXPORT_SYMBOL_GPL(devm_pm_opp_register_set_opp_helper); 2328 2329 static void _opp_detach_genpd(struct opp_table *opp_table) 2330 { 2331 int index; 2332 2333 if (!opp_table->genpd_virt_devs) 2334 return; 2335 2336 for (index = 0; index < opp_table->required_opp_count; index++) { 2337 if (!opp_table->genpd_virt_devs[index]) 2338 continue; 2339 2340 dev_pm_domain_detach(opp_table->genpd_virt_devs[index], false); 2341 opp_table->genpd_virt_devs[index] = NULL; 2342 } 2343 2344 kfree(opp_table->genpd_virt_devs); 2345 opp_table->genpd_virt_devs = NULL; 2346 } 2347 2348 /** 2349 * dev_pm_opp_attach_genpd - Attach genpd(s) for the device and save virtual device pointer 2350 * @dev: Consumer device for which the genpd is getting attached. 2351 * @names: Null terminated array of pointers containing names of genpd to attach. 2352 * @virt_devs: Pointer to return the array of virtual devices. 2353 * 2354 * Multiple generic power domains for a device are supported with the help of 2355 * virtual genpd devices, which are created for each consumer device - genpd 2356 * pair. These are the device structures which are attached to the power domain 2357 * and are required by the OPP core to set the performance state of the genpd. 2358 * The same API also works for the case where single genpd is available and so 2359 * we don't need to support that separately. 2360 * 2361 * This helper will normally be called by the consumer driver of the device 2362 * "dev", as only that has details of the genpd names. 2363 * 2364 * This helper needs to be called once with a list of all genpd to attach. 2365 * Otherwise the original device structure will be used instead by the OPP core. 2366 * 2367 * The order of entries in the names array must match the order in which 2368 * "required-opps" are added in DT. 2369 */ 2370 struct opp_table *dev_pm_opp_attach_genpd(struct device *dev, 2371 const char * const *names, struct device ***virt_devs) 2372 { 2373 struct opp_table *opp_table; 2374 struct device *virt_dev; 2375 int index = 0, ret = -EINVAL; 2376 const char * const *name = names; 2377 2378 opp_table = _add_opp_table(dev, false); 2379 if (IS_ERR(opp_table)) 2380 return opp_table; 2381 2382 if (opp_table->genpd_virt_devs) 2383 return opp_table; 2384 2385 /* 2386 * If the genpd's OPP table isn't already initialized, parsing of the 2387 * required-opps fail for dev. We should retry this after genpd's OPP 2388 * table is added. 2389 */ 2390 if (!opp_table->required_opp_count) { 2391 ret = -EPROBE_DEFER; 2392 goto put_table; 2393 } 2394 2395 mutex_lock(&opp_table->genpd_virt_dev_lock); 2396 2397 opp_table->genpd_virt_devs = kcalloc(opp_table->required_opp_count, 2398 sizeof(*opp_table->genpd_virt_devs), 2399 GFP_KERNEL); 2400 if (!opp_table->genpd_virt_devs) 2401 goto unlock; 2402 2403 while (*name) { 2404 if (index >= opp_table->required_opp_count) { 2405 dev_err(dev, "Index can't be greater than required-opp-count - 1, %s (%d : %d)\n", 2406 *name, opp_table->required_opp_count, index); 2407 goto err; 2408 } 2409 2410 virt_dev = dev_pm_domain_attach_by_name(dev, *name); 2411 if (IS_ERR(virt_dev)) { 2412 ret = PTR_ERR(virt_dev); 2413 dev_err(dev, "Couldn't attach to pm_domain: %d\n", ret); 2414 goto err; 2415 } 2416 2417 opp_table->genpd_virt_devs[index] = virt_dev; 2418 index++; 2419 name++; 2420 } 2421 2422 if (virt_devs) 2423 *virt_devs = opp_table->genpd_virt_devs; 2424 mutex_unlock(&opp_table->genpd_virt_dev_lock); 2425 2426 return opp_table; 2427 2428 err: 2429 _opp_detach_genpd(opp_table); 2430 unlock: 2431 mutex_unlock(&opp_table->genpd_virt_dev_lock); 2432 2433 put_table: 2434 dev_pm_opp_put_opp_table(opp_table); 2435 2436 return ERR_PTR(ret); 2437 } 2438 EXPORT_SYMBOL_GPL(dev_pm_opp_attach_genpd); 2439 2440 /** 2441 * dev_pm_opp_detach_genpd() - Detach genpd(s) from the device. 2442 * @opp_table: OPP table returned by dev_pm_opp_attach_genpd(). 2443 * 2444 * This detaches the genpd(s), resets the virtual device pointers, and puts the 2445 * OPP table. 2446 */ 2447 void dev_pm_opp_detach_genpd(struct opp_table *opp_table) 2448 { 2449 if (unlikely(!opp_table)) 2450 return; 2451 2452 /* 2453 * Acquire genpd_virt_dev_lock to make sure virt_dev isn't getting 2454 * used in parallel. 2455 */ 2456 mutex_lock(&opp_table->genpd_virt_dev_lock); 2457 _opp_detach_genpd(opp_table); 2458 mutex_unlock(&opp_table->genpd_virt_dev_lock); 2459 2460 dev_pm_opp_put_opp_table(opp_table); 2461 } 2462 EXPORT_SYMBOL_GPL(dev_pm_opp_detach_genpd); 2463 2464 static void devm_pm_opp_detach_genpd(void *data) 2465 { 2466 dev_pm_opp_detach_genpd(data); 2467 } 2468 2469 /** 2470 * devm_pm_opp_attach_genpd - Attach genpd(s) for the device and save virtual 2471 * device pointer 2472 * @dev: Consumer device for which the genpd is getting attached. 2473 * @names: Null terminated array of pointers containing names of genpd to attach. 2474 * @virt_devs: Pointer to return the array of virtual devices. 2475 * 2476 * This is a resource-managed version of dev_pm_opp_attach_genpd(). 2477 * 2478 * Return: 0 on success and errorno otherwise. 2479 */ 2480 int devm_pm_opp_attach_genpd(struct device *dev, const char * const *names, 2481 struct device ***virt_devs) 2482 { 2483 struct opp_table *opp_table; 2484 2485 opp_table = dev_pm_opp_attach_genpd(dev, names, virt_devs); 2486 if (IS_ERR(opp_table)) 2487 return PTR_ERR(opp_table); 2488 2489 return devm_add_action_or_reset(dev, devm_pm_opp_detach_genpd, 2490 opp_table); 2491 } 2492 EXPORT_SYMBOL_GPL(devm_pm_opp_attach_genpd); 2493 2494 /** 2495 * dev_pm_opp_xlate_required_opp() - Find required OPP for @src_table OPP. 2496 * @src_table: OPP table which has @dst_table as one of its required OPP table. 2497 * @dst_table: Required OPP table of the @src_table. 2498 * @src_opp: OPP from the @src_table. 2499 * 2500 * This function returns the OPP (present in @dst_table) pointed out by the 2501 * "required-opps" property of the @src_opp (present in @src_table). 2502 * 2503 * The callers are required to call dev_pm_opp_put() for the returned OPP after 2504 * use. 2505 * 2506 * Return: pointer to 'struct dev_pm_opp' on success and errorno otherwise. 2507 */ 2508 struct dev_pm_opp *dev_pm_opp_xlate_required_opp(struct opp_table *src_table, 2509 struct opp_table *dst_table, 2510 struct dev_pm_opp *src_opp) 2511 { 2512 struct dev_pm_opp *opp, *dest_opp = ERR_PTR(-ENODEV); 2513 int i; 2514 2515 if (!src_table || !dst_table || !src_opp || 2516 !src_table->required_opp_tables) 2517 return ERR_PTR(-EINVAL); 2518 2519 /* required-opps not fully initialized yet */ 2520 if (lazy_linking_pending(src_table)) 2521 return ERR_PTR(-EBUSY); 2522 2523 for (i = 0; i < src_table->required_opp_count; i++) { 2524 if (src_table->required_opp_tables[i] == dst_table) { 2525 mutex_lock(&src_table->lock); 2526 2527 list_for_each_entry(opp, &src_table->opp_list, node) { 2528 if (opp == src_opp) { 2529 dest_opp = opp->required_opps[i]; 2530 dev_pm_opp_get(dest_opp); 2531 break; 2532 } 2533 } 2534 2535 mutex_unlock(&src_table->lock); 2536 break; 2537 } 2538 } 2539 2540 if (IS_ERR(dest_opp)) { 2541 pr_err("%s: Couldn't find matching OPP (%p: %p)\n", __func__, 2542 src_table, dst_table); 2543 } 2544 2545 return dest_opp; 2546 } 2547 EXPORT_SYMBOL_GPL(dev_pm_opp_xlate_required_opp); 2548 2549 /** 2550 * dev_pm_opp_xlate_performance_state() - Find required OPP's pstate for src_table. 2551 * @src_table: OPP table which has dst_table as one of its required OPP table. 2552 * @dst_table: Required OPP table of the src_table. 2553 * @pstate: Current performance state of the src_table. 2554 * 2555 * This Returns pstate of the OPP (present in @dst_table) pointed out by the 2556 * "required-opps" property of the OPP (present in @src_table) which has 2557 * performance state set to @pstate. 2558 * 2559 * Return: Zero or positive performance state on success, otherwise negative 2560 * value on errors. 2561 */ 2562 int dev_pm_opp_xlate_performance_state(struct opp_table *src_table, 2563 struct opp_table *dst_table, 2564 unsigned int pstate) 2565 { 2566 struct dev_pm_opp *opp; 2567 int dest_pstate = -EINVAL; 2568 int i; 2569 2570 /* 2571 * Normally the src_table will have the "required_opps" property set to 2572 * point to one of the OPPs in the dst_table, but in some cases the 2573 * genpd and its master have one to one mapping of performance states 2574 * and so none of them have the "required-opps" property set. Return the 2575 * pstate of the src_table as it is in such cases. 2576 */ 2577 if (!src_table || !src_table->required_opp_count) 2578 return pstate; 2579 2580 /* required-opps not fully initialized yet */ 2581 if (lazy_linking_pending(src_table)) 2582 return -EBUSY; 2583 2584 for (i = 0; i < src_table->required_opp_count; i++) { 2585 if (src_table->required_opp_tables[i]->np == dst_table->np) 2586 break; 2587 } 2588 2589 if (unlikely(i == src_table->required_opp_count)) { 2590 pr_err("%s: Couldn't find matching OPP table (%p: %p)\n", 2591 __func__, src_table, dst_table); 2592 return -EINVAL; 2593 } 2594 2595 mutex_lock(&src_table->lock); 2596 2597 list_for_each_entry(opp, &src_table->opp_list, node) { 2598 if (opp->pstate == pstate) { 2599 dest_pstate = opp->required_opps[i]->pstate; 2600 goto unlock; 2601 } 2602 } 2603 2604 pr_err("%s: Couldn't find matching OPP (%p: %p)\n", __func__, src_table, 2605 dst_table); 2606 2607 unlock: 2608 mutex_unlock(&src_table->lock); 2609 2610 return dest_pstate; 2611 } 2612 2613 /** 2614 * dev_pm_opp_add() - Add an OPP table from a table definitions 2615 * @dev: device for which we do this operation 2616 * @freq: Frequency in Hz for this OPP 2617 * @u_volt: Voltage in uVolts for this OPP 2618 * 2619 * This function adds an opp definition to the opp table and returns status. 2620 * The opp is made available by default and it can be controlled using 2621 * dev_pm_opp_enable/disable functions. 2622 * 2623 * Return: 2624 * 0 On success OR 2625 * Duplicate OPPs (both freq and volt are same) and opp->available 2626 * -EEXIST Freq are same and volt are different OR 2627 * Duplicate OPPs (both freq and volt are same) and !opp->available 2628 * -ENOMEM Memory allocation failure 2629 */ 2630 int dev_pm_opp_add(struct device *dev, unsigned long freq, unsigned long u_volt) 2631 { 2632 struct opp_table *opp_table; 2633 int ret; 2634 2635 opp_table = _add_opp_table(dev, true); 2636 if (IS_ERR(opp_table)) 2637 return PTR_ERR(opp_table); 2638 2639 /* Fix regulator count for dynamic OPPs */ 2640 opp_table->regulator_count = 1; 2641 2642 ret = _opp_add_v1(opp_table, dev, freq, u_volt, true); 2643 if (ret) 2644 dev_pm_opp_put_opp_table(opp_table); 2645 2646 return ret; 2647 } 2648 EXPORT_SYMBOL_GPL(dev_pm_opp_add); 2649 2650 /** 2651 * _opp_set_availability() - helper to set the availability of an opp 2652 * @dev: device for which we do this operation 2653 * @freq: OPP frequency to modify availability 2654 * @availability_req: availability status requested for this opp 2655 * 2656 * Set the availability of an OPP, opp_{enable,disable} share a common logic 2657 * which is isolated here. 2658 * 2659 * Return: -EINVAL for bad pointers, -ENOMEM if no memory available for the 2660 * copy operation, returns 0 if no modification was done OR modification was 2661 * successful. 2662 */ 2663 static int _opp_set_availability(struct device *dev, unsigned long freq, 2664 bool availability_req) 2665 { 2666 struct opp_table *opp_table; 2667 struct dev_pm_opp *tmp_opp, *opp = ERR_PTR(-ENODEV); 2668 int r = 0; 2669 2670 /* Find the opp_table */ 2671 opp_table = _find_opp_table(dev); 2672 if (IS_ERR(opp_table)) { 2673 r = PTR_ERR(opp_table); 2674 dev_warn(dev, "%s: Device OPP not found (%d)\n", __func__, r); 2675 return r; 2676 } 2677 2678 mutex_lock(&opp_table->lock); 2679 2680 /* Do we have the frequency? */ 2681 list_for_each_entry(tmp_opp, &opp_table->opp_list, node) { 2682 if (tmp_opp->rate == freq) { 2683 opp = tmp_opp; 2684 break; 2685 } 2686 } 2687 2688 if (IS_ERR(opp)) { 2689 r = PTR_ERR(opp); 2690 goto unlock; 2691 } 2692 2693 /* Is update really needed? */ 2694 if (opp->available == availability_req) 2695 goto unlock; 2696 2697 opp->available = availability_req; 2698 2699 dev_pm_opp_get(opp); 2700 mutex_unlock(&opp_table->lock); 2701 2702 /* Notify the change of the OPP availability */ 2703 if (availability_req) 2704 blocking_notifier_call_chain(&opp_table->head, OPP_EVENT_ENABLE, 2705 opp); 2706 else 2707 blocking_notifier_call_chain(&opp_table->head, 2708 OPP_EVENT_DISABLE, opp); 2709 2710 dev_pm_opp_put(opp); 2711 goto put_table; 2712 2713 unlock: 2714 mutex_unlock(&opp_table->lock); 2715 put_table: 2716 dev_pm_opp_put_opp_table(opp_table); 2717 return r; 2718 } 2719 2720 /** 2721 * dev_pm_opp_adjust_voltage() - helper to change the voltage of an OPP 2722 * @dev: device for which we do this operation 2723 * @freq: OPP frequency to adjust voltage of 2724 * @u_volt: new OPP target voltage 2725 * @u_volt_min: new OPP min voltage 2726 * @u_volt_max: new OPP max voltage 2727 * 2728 * Return: -EINVAL for bad pointers, -ENOMEM if no memory available for the 2729 * copy operation, returns 0 if no modifcation was done OR modification was 2730 * successful. 2731 */ 2732 int dev_pm_opp_adjust_voltage(struct device *dev, unsigned long freq, 2733 unsigned long u_volt, unsigned long u_volt_min, 2734 unsigned long u_volt_max) 2735 2736 { 2737 struct opp_table *opp_table; 2738 struct dev_pm_opp *tmp_opp, *opp = ERR_PTR(-ENODEV); 2739 int r = 0; 2740 2741 /* Find the opp_table */ 2742 opp_table = _find_opp_table(dev); 2743 if (IS_ERR(opp_table)) { 2744 r = PTR_ERR(opp_table); 2745 dev_warn(dev, "%s: Device OPP not found (%d)\n", __func__, r); 2746 return r; 2747 } 2748 2749 mutex_lock(&opp_table->lock); 2750 2751 /* Do we have the frequency? */ 2752 list_for_each_entry(tmp_opp, &opp_table->opp_list, node) { 2753 if (tmp_opp->rate == freq) { 2754 opp = tmp_opp; 2755 break; 2756 } 2757 } 2758 2759 if (IS_ERR(opp)) { 2760 r = PTR_ERR(opp); 2761 goto adjust_unlock; 2762 } 2763 2764 /* Is update really needed? */ 2765 if (opp->supplies->u_volt == u_volt) 2766 goto adjust_unlock; 2767 2768 opp->supplies->u_volt = u_volt; 2769 opp->supplies->u_volt_min = u_volt_min; 2770 opp->supplies->u_volt_max = u_volt_max; 2771 2772 dev_pm_opp_get(opp); 2773 mutex_unlock(&opp_table->lock); 2774 2775 /* Notify the voltage change of the OPP */ 2776 blocking_notifier_call_chain(&opp_table->head, OPP_EVENT_ADJUST_VOLTAGE, 2777 opp); 2778 2779 dev_pm_opp_put(opp); 2780 goto adjust_put_table; 2781 2782 adjust_unlock: 2783 mutex_unlock(&opp_table->lock); 2784 adjust_put_table: 2785 dev_pm_opp_put_opp_table(opp_table); 2786 return r; 2787 } 2788 EXPORT_SYMBOL_GPL(dev_pm_opp_adjust_voltage); 2789 2790 /** 2791 * dev_pm_opp_enable() - Enable a specific OPP 2792 * @dev: device for which we do this operation 2793 * @freq: OPP frequency to enable 2794 * 2795 * Enables a provided opp. If the operation is valid, this returns 0, else the 2796 * corresponding error value. It is meant to be used for users an OPP available 2797 * after being temporarily made unavailable with dev_pm_opp_disable. 2798 * 2799 * Return: -EINVAL for bad pointers, -ENOMEM if no memory available for the 2800 * copy operation, returns 0 if no modification was done OR modification was 2801 * successful. 2802 */ 2803 int dev_pm_opp_enable(struct device *dev, unsigned long freq) 2804 { 2805 return _opp_set_availability(dev, freq, true); 2806 } 2807 EXPORT_SYMBOL_GPL(dev_pm_opp_enable); 2808 2809 /** 2810 * dev_pm_opp_disable() - Disable a specific OPP 2811 * @dev: device for which we do this operation 2812 * @freq: OPP frequency to disable 2813 * 2814 * Disables a provided opp. If the operation is valid, this returns 2815 * 0, else the corresponding error value. It is meant to be a temporary 2816 * control by users to make this OPP not available until the circumstances are 2817 * right to make it available again (with a call to dev_pm_opp_enable). 2818 * 2819 * Return: -EINVAL for bad pointers, -ENOMEM if no memory available for the 2820 * copy operation, returns 0 if no modification was done OR modification was 2821 * successful. 2822 */ 2823 int dev_pm_opp_disable(struct device *dev, unsigned long freq) 2824 { 2825 return _opp_set_availability(dev, freq, false); 2826 } 2827 EXPORT_SYMBOL_GPL(dev_pm_opp_disable); 2828 2829 /** 2830 * dev_pm_opp_register_notifier() - Register OPP notifier for the device 2831 * @dev: Device for which notifier needs to be registered 2832 * @nb: Notifier block to be registered 2833 * 2834 * Return: 0 on success or a negative error value. 2835 */ 2836 int dev_pm_opp_register_notifier(struct device *dev, struct notifier_block *nb) 2837 { 2838 struct opp_table *opp_table; 2839 int ret; 2840 2841 opp_table = _find_opp_table(dev); 2842 if (IS_ERR(opp_table)) 2843 return PTR_ERR(opp_table); 2844 2845 ret = blocking_notifier_chain_register(&opp_table->head, nb); 2846 2847 dev_pm_opp_put_opp_table(opp_table); 2848 2849 return ret; 2850 } 2851 EXPORT_SYMBOL(dev_pm_opp_register_notifier); 2852 2853 /** 2854 * dev_pm_opp_unregister_notifier() - Unregister OPP notifier for the device 2855 * @dev: Device for which notifier needs to be unregistered 2856 * @nb: Notifier block to be unregistered 2857 * 2858 * Return: 0 on success or a negative error value. 2859 */ 2860 int dev_pm_opp_unregister_notifier(struct device *dev, 2861 struct notifier_block *nb) 2862 { 2863 struct opp_table *opp_table; 2864 int ret; 2865 2866 opp_table = _find_opp_table(dev); 2867 if (IS_ERR(opp_table)) 2868 return PTR_ERR(opp_table); 2869 2870 ret = blocking_notifier_chain_unregister(&opp_table->head, nb); 2871 2872 dev_pm_opp_put_opp_table(opp_table); 2873 2874 return ret; 2875 } 2876 EXPORT_SYMBOL(dev_pm_opp_unregister_notifier); 2877 2878 /** 2879 * dev_pm_opp_remove_table() - Free all OPPs associated with the device 2880 * @dev: device pointer used to lookup OPP table. 2881 * 2882 * Free both OPPs created using static entries present in DT and the 2883 * dynamically added entries. 2884 */ 2885 void dev_pm_opp_remove_table(struct device *dev) 2886 { 2887 struct opp_table *opp_table; 2888 2889 /* Check for existing table for 'dev' */ 2890 opp_table = _find_opp_table(dev); 2891 if (IS_ERR(opp_table)) { 2892 int error = PTR_ERR(opp_table); 2893 2894 if (error != -ENODEV) 2895 WARN(1, "%s: opp_table: %d\n", 2896 IS_ERR_OR_NULL(dev) ? 2897 "Invalid device" : dev_name(dev), 2898 error); 2899 return; 2900 } 2901 2902 /* 2903 * Drop the extra reference only if the OPP table was successfully added 2904 * with dev_pm_opp_of_add_table() earlier. 2905 **/ 2906 if (_opp_remove_all_static(opp_table)) 2907 dev_pm_opp_put_opp_table(opp_table); 2908 2909 /* Drop reference taken by _find_opp_table() */ 2910 dev_pm_opp_put_opp_table(opp_table); 2911 } 2912 EXPORT_SYMBOL_GPL(dev_pm_opp_remove_table); 2913 2914 /** 2915 * dev_pm_opp_sync_regulators() - Sync state of voltage regulators 2916 * @dev: device for which we do this operation 2917 * 2918 * Sync voltage state of the OPP table regulators. 2919 * 2920 * Return: 0 on success or a negative error value. 2921 */ 2922 int dev_pm_opp_sync_regulators(struct device *dev) 2923 { 2924 struct opp_table *opp_table; 2925 struct regulator *reg; 2926 int i, ret = 0; 2927 2928 /* Device may not have OPP table */ 2929 opp_table = _find_opp_table(dev); 2930 if (IS_ERR(opp_table)) 2931 return 0; 2932 2933 /* Regulator may not be required for the device */ 2934 if (unlikely(!opp_table->regulators)) 2935 goto put_table; 2936 2937 /* Nothing to sync if voltage wasn't changed */ 2938 if (!opp_table->enabled) 2939 goto put_table; 2940 2941 for (i = 0; i < opp_table->regulator_count; i++) { 2942 reg = opp_table->regulators[i]; 2943 ret = regulator_sync_voltage(reg); 2944 if (ret) 2945 break; 2946 } 2947 put_table: 2948 /* Drop reference taken by _find_opp_table() */ 2949 dev_pm_opp_put_opp_table(opp_table); 2950 2951 return ret; 2952 } 2953 EXPORT_SYMBOL_GPL(dev_pm_opp_sync_regulators); 2954