1 /* 2 * Generic OPP Interface 3 * 4 * Copyright (C) 2009-2010 Texas Instruments Incorporated. 5 * Nishanth Menon 6 * Romit Dasgupta 7 * Kevin Hilman 8 * 9 * This program is free software; you can redistribute it and/or modify 10 * it under the terms of the GNU General Public License version 2 as 11 * published by the Free Software Foundation. 12 */ 13 14 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 15 16 #include <linux/clk.h> 17 #include <linux/errno.h> 18 #include <linux/err.h> 19 #include <linux/slab.h> 20 #include <linux/device.h> 21 #include <linux/export.h> 22 #include <linux/pm_domain.h> 23 #include <linux/regulator/consumer.h> 24 25 #include "opp.h" 26 27 /* 28 * The root of the list of all opp-tables. All opp_table structures branch off 29 * from here, with each opp_table containing the list of opps it supports in 30 * various states of availability. 31 */ 32 LIST_HEAD(opp_tables); 33 /* Lock to allow exclusive modification to the device and opp lists */ 34 DEFINE_MUTEX(opp_table_lock); 35 36 static struct opp_device *_find_opp_dev(const struct device *dev, 37 struct opp_table *opp_table) 38 { 39 struct opp_device *opp_dev; 40 41 list_for_each_entry(opp_dev, &opp_table->dev_list, node) 42 if (opp_dev->dev == dev) 43 return opp_dev; 44 45 return NULL; 46 } 47 48 static struct opp_table *_find_opp_table_unlocked(struct device *dev) 49 { 50 struct opp_table *opp_table; 51 bool found; 52 53 list_for_each_entry(opp_table, &opp_tables, node) { 54 mutex_lock(&opp_table->lock); 55 found = !!_find_opp_dev(dev, opp_table); 56 mutex_unlock(&opp_table->lock); 57 58 if (found) { 59 _get_opp_table_kref(opp_table); 60 61 return opp_table; 62 } 63 } 64 65 return ERR_PTR(-ENODEV); 66 } 67 68 /** 69 * _find_opp_table() - find opp_table struct using device pointer 70 * @dev: device pointer used to lookup OPP table 71 * 72 * Search OPP table for one containing matching device. 73 * 74 * Return: pointer to 'struct opp_table' if found, otherwise -ENODEV or 75 * -EINVAL based on type of error. 76 * 77 * The callers must call dev_pm_opp_put_opp_table() after the table is used. 78 */ 79 struct opp_table *_find_opp_table(struct device *dev) 80 { 81 struct opp_table *opp_table; 82 83 if (IS_ERR_OR_NULL(dev)) { 84 pr_err("%s: Invalid parameters\n", __func__); 85 return ERR_PTR(-EINVAL); 86 } 87 88 mutex_lock(&opp_table_lock); 89 opp_table = _find_opp_table_unlocked(dev); 90 mutex_unlock(&opp_table_lock); 91 92 return opp_table; 93 } 94 95 /** 96 * dev_pm_opp_get_voltage() - Gets the voltage corresponding to an opp 97 * @opp: opp for which voltage has to be returned for 98 * 99 * Return: voltage in micro volt corresponding to the opp, else 100 * return 0 101 * 102 * This is useful only for devices with single power supply. 103 */ 104 unsigned long dev_pm_opp_get_voltage(struct dev_pm_opp *opp) 105 { 106 if (IS_ERR_OR_NULL(opp)) { 107 pr_err("%s: Invalid parameters\n", __func__); 108 return 0; 109 } 110 111 return opp->supplies[0].u_volt; 112 } 113 EXPORT_SYMBOL_GPL(dev_pm_opp_get_voltage); 114 115 /** 116 * dev_pm_opp_get_freq() - Gets the frequency corresponding to an available opp 117 * @opp: opp for which frequency has to be returned for 118 * 119 * Return: frequency in hertz corresponding to the opp, else 120 * return 0 121 */ 122 unsigned long dev_pm_opp_get_freq(struct dev_pm_opp *opp) 123 { 124 if (IS_ERR_OR_NULL(opp) || !opp->available) { 125 pr_err("%s: Invalid parameters\n", __func__); 126 return 0; 127 } 128 129 return opp->rate; 130 } 131 EXPORT_SYMBOL_GPL(dev_pm_opp_get_freq); 132 133 /** 134 * dev_pm_opp_get_level() - Gets the level corresponding to an available opp 135 * @opp: opp for which level value has to be returned for 136 * 137 * Return: level read from device tree corresponding to the opp, else 138 * return 0. 139 */ 140 unsigned int dev_pm_opp_get_level(struct dev_pm_opp *opp) 141 { 142 if (IS_ERR_OR_NULL(opp) || !opp->available) { 143 pr_err("%s: Invalid parameters\n", __func__); 144 return 0; 145 } 146 147 return opp->level; 148 } 149 EXPORT_SYMBOL_GPL(dev_pm_opp_get_level); 150 151 /** 152 * dev_pm_opp_is_turbo() - Returns if opp is turbo OPP or not 153 * @opp: opp for which turbo mode is being verified 154 * 155 * Turbo OPPs are not for normal use, and can be enabled (under certain 156 * conditions) for short duration of times to finish high throughput work 157 * quickly. Running on them for longer times may overheat the chip. 158 * 159 * Return: true if opp is turbo opp, else false. 160 */ 161 bool dev_pm_opp_is_turbo(struct dev_pm_opp *opp) 162 { 163 if (IS_ERR_OR_NULL(opp) || !opp->available) { 164 pr_err("%s: Invalid parameters\n", __func__); 165 return false; 166 } 167 168 return opp->turbo; 169 } 170 EXPORT_SYMBOL_GPL(dev_pm_opp_is_turbo); 171 172 /** 173 * dev_pm_opp_get_max_clock_latency() - Get max clock latency in nanoseconds 174 * @dev: device for which we do this operation 175 * 176 * Return: This function returns the max clock latency in nanoseconds. 177 */ 178 unsigned long dev_pm_opp_get_max_clock_latency(struct device *dev) 179 { 180 struct opp_table *opp_table; 181 unsigned long clock_latency_ns; 182 183 opp_table = _find_opp_table(dev); 184 if (IS_ERR(opp_table)) 185 return 0; 186 187 clock_latency_ns = opp_table->clock_latency_ns_max; 188 189 dev_pm_opp_put_opp_table(opp_table); 190 191 return clock_latency_ns; 192 } 193 EXPORT_SYMBOL_GPL(dev_pm_opp_get_max_clock_latency); 194 195 /** 196 * dev_pm_opp_get_max_volt_latency() - Get max voltage latency in nanoseconds 197 * @dev: device for which we do this operation 198 * 199 * Return: This function returns the max voltage latency in nanoseconds. 200 */ 201 unsigned long dev_pm_opp_get_max_volt_latency(struct device *dev) 202 { 203 struct opp_table *opp_table; 204 struct dev_pm_opp *opp; 205 struct regulator *reg; 206 unsigned long latency_ns = 0; 207 int ret, i, count; 208 struct { 209 unsigned long min; 210 unsigned long max; 211 } *uV; 212 213 opp_table = _find_opp_table(dev); 214 if (IS_ERR(opp_table)) 215 return 0; 216 217 /* Regulator may not be required for the device */ 218 if (!opp_table->regulators) 219 goto put_opp_table; 220 221 count = opp_table->regulator_count; 222 223 uV = kmalloc_array(count, sizeof(*uV), GFP_KERNEL); 224 if (!uV) 225 goto put_opp_table; 226 227 mutex_lock(&opp_table->lock); 228 229 for (i = 0; i < count; i++) { 230 uV[i].min = ~0; 231 uV[i].max = 0; 232 233 list_for_each_entry(opp, &opp_table->opp_list, node) { 234 if (!opp->available) 235 continue; 236 237 if (opp->supplies[i].u_volt_min < uV[i].min) 238 uV[i].min = opp->supplies[i].u_volt_min; 239 if (opp->supplies[i].u_volt_max > uV[i].max) 240 uV[i].max = opp->supplies[i].u_volt_max; 241 } 242 } 243 244 mutex_unlock(&opp_table->lock); 245 246 /* 247 * The caller needs to ensure that opp_table (and hence the regulator) 248 * isn't freed, while we are executing this routine. 249 */ 250 for (i = 0; i < count; i++) { 251 reg = opp_table->regulators[i]; 252 ret = regulator_set_voltage_time(reg, uV[i].min, uV[i].max); 253 if (ret > 0) 254 latency_ns += ret * 1000; 255 } 256 257 kfree(uV); 258 put_opp_table: 259 dev_pm_opp_put_opp_table(opp_table); 260 261 return latency_ns; 262 } 263 EXPORT_SYMBOL_GPL(dev_pm_opp_get_max_volt_latency); 264 265 /** 266 * dev_pm_opp_get_max_transition_latency() - Get max transition latency in 267 * nanoseconds 268 * @dev: device for which we do this operation 269 * 270 * Return: This function returns the max transition latency, in nanoseconds, to 271 * switch from one OPP to other. 272 */ 273 unsigned long dev_pm_opp_get_max_transition_latency(struct device *dev) 274 { 275 return dev_pm_opp_get_max_volt_latency(dev) + 276 dev_pm_opp_get_max_clock_latency(dev); 277 } 278 EXPORT_SYMBOL_GPL(dev_pm_opp_get_max_transition_latency); 279 280 /** 281 * dev_pm_opp_get_suspend_opp_freq() - Get frequency of suspend opp in Hz 282 * @dev: device for which we do this operation 283 * 284 * Return: This function returns the frequency of the OPP marked as suspend_opp 285 * if one is available, else returns 0; 286 */ 287 unsigned long dev_pm_opp_get_suspend_opp_freq(struct device *dev) 288 { 289 struct opp_table *opp_table; 290 unsigned long freq = 0; 291 292 opp_table = _find_opp_table(dev); 293 if (IS_ERR(opp_table)) 294 return 0; 295 296 if (opp_table->suspend_opp && opp_table->suspend_opp->available) 297 freq = dev_pm_opp_get_freq(opp_table->suspend_opp); 298 299 dev_pm_opp_put_opp_table(opp_table); 300 301 return freq; 302 } 303 EXPORT_SYMBOL_GPL(dev_pm_opp_get_suspend_opp_freq); 304 305 int _get_opp_count(struct opp_table *opp_table) 306 { 307 struct dev_pm_opp *opp; 308 int count = 0; 309 310 mutex_lock(&opp_table->lock); 311 312 list_for_each_entry(opp, &opp_table->opp_list, node) { 313 if (opp->available) 314 count++; 315 } 316 317 mutex_unlock(&opp_table->lock); 318 319 return count; 320 } 321 322 /** 323 * dev_pm_opp_get_opp_count() - Get number of opps available in the opp table 324 * @dev: device for which we do this operation 325 * 326 * Return: This function returns the number of available opps if there are any, 327 * else returns 0 if none or the corresponding error value. 328 */ 329 int dev_pm_opp_get_opp_count(struct device *dev) 330 { 331 struct opp_table *opp_table; 332 int count; 333 334 opp_table = _find_opp_table(dev); 335 if (IS_ERR(opp_table)) { 336 count = PTR_ERR(opp_table); 337 dev_dbg(dev, "%s: OPP table not found (%d)\n", 338 __func__, count); 339 return count; 340 } 341 342 count = _get_opp_count(opp_table); 343 dev_pm_opp_put_opp_table(opp_table); 344 345 return count; 346 } 347 EXPORT_SYMBOL_GPL(dev_pm_opp_get_opp_count); 348 349 /** 350 * dev_pm_opp_find_freq_exact() - search for an exact frequency 351 * @dev: device for which we do this operation 352 * @freq: frequency to search for 353 * @available: true/false - match for available opp 354 * 355 * Return: Searches for exact match in the opp table and returns pointer to the 356 * matching opp if found, else returns ERR_PTR in case of error and should 357 * be handled using IS_ERR. Error return values can be: 358 * EINVAL: for bad pointer 359 * ERANGE: no match found for search 360 * ENODEV: if device not found in list of registered devices 361 * 362 * Note: available is a modifier for the search. if available=true, then the 363 * match is for exact matching frequency and is available in the stored OPP 364 * table. if false, the match is for exact frequency which is not available. 365 * 366 * This provides a mechanism to enable an opp which is not available currently 367 * or the opposite as well. 368 * 369 * The callers are required to call dev_pm_opp_put() for the returned OPP after 370 * use. 371 */ 372 struct dev_pm_opp *dev_pm_opp_find_freq_exact(struct device *dev, 373 unsigned long freq, 374 bool available) 375 { 376 struct opp_table *opp_table; 377 struct dev_pm_opp *temp_opp, *opp = ERR_PTR(-ERANGE); 378 379 opp_table = _find_opp_table(dev); 380 if (IS_ERR(opp_table)) { 381 int r = PTR_ERR(opp_table); 382 383 dev_err(dev, "%s: OPP table not found (%d)\n", __func__, r); 384 return ERR_PTR(r); 385 } 386 387 mutex_lock(&opp_table->lock); 388 389 list_for_each_entry(temp_opp, &opp_table->opp_list, node) { 390 if (temp_opp->available == available && 391 temp_opp->rate == freq) { 392 opp = temp_opp; 393 394 /* Increment the reference count of OPP */ 395 dev_pm_opp_get(opp); 396 break; 397 } 398 } 399 400 mutex_unlock(&opp_table->lock); 401 dev_pm_opp_put_opp_table(opp_table); 402 403 return opp; 404 } 405 EXPORT_SYMBOL_GPL(dev_pm_opp_find_freq_exact); 406 407 static noinline struct dev_pm_opp *_find_freq_ceil(struct opp_table *opp_table, 408 unsigned long *freq) 409 { 410 struct dev_pm_opp *temp_opp, *opp = ERR_PTR(-ERANGE); 411 412 mutex_lock(&opp_table->lock); 413 414 list_for_each_entry(temp_opp, &opp_table->opp_list, node) { 415 if (temp_opp->available && temp_opp->rate >= *freq) { 416 opp = temp_opp; 417 *freq = opp->rate; 418 419 /* Increment the reference count of OPP */ 420 dev_pm_opp_get(opp); 421 break; 422 } 423 } 424 425 mutex_unlock(&opp_table->lock); 426 427 return opp; 428 } 429 430 /** 431 * dev_pm_opp_find_freq_ceil() - Search for an rounded ceil freq 432 * @dev: device for which we do this operation 433 * @freq: Start frequency 434 * 435 * Search for the matching ceil *available* OPP from a starting freq 436 * for a device. 437 * 438 * Return: matching *opp and refreshes *freq accordingly, else returns 439 * ERR_PTR in case of error and should be handled using IS_ERR. Error return 440 * values can be: 441 * EINVAL: for bad pointer 442 * ERANGE: no match found for search 443 * ENODEV: if device not found in list of registered devices 444 * 445 * The callers are required to call dev_pm_opp_put() for the returned OPP after 446 * use. 447 */ 448 struct dev_pm_opp *dev_pm_opp_find_freq_ceil(struct device *dev, 449 unsigned long *freq) 450 { 451 struct opp_table *opp_table; 452 struct dev_pm_opp *opp; 453 454 if (!dev || !freq) { 455 dev_err(dev, "%s: Invalid argument freq=%p\n", __func__, freq); 456 return ERR_PTR(-EINVAL); 457 } 458 459 opp_table = _find_opp_table(dev); 460 if (IS_ERR(opp_table)) 461 return ERR_CAST(opp_table); 462 463 opp = _find_freq_ceil(opp_table, freq); 464 465 dev_pm_opp_put_opp_table(opp_table); 466 467 return opp; 468 } 469 EXPORT_SYMBOL_GPL(dev_pm_opp_find_freq_ceil); 470 471 /** 472 * dev_pm_opp_find_freq_floor() - Search for a rounded floor freq 473 * @dev: device for which we do this operation 474 * @freq: Start frequency 475 * 476 * Search for the matching floor *available* OPP from a starting freq 477 * for a device. 478 * 479 * Return: matching *opp and refreshes *freq accordingly, else returns 480 * ERR_PTR in case of error and should be handled using IS_ERR. Error return 481 * values can be: 482 * EINVAL: for bad pointer 483 * ERANGE: no match found for search 484 * ENODEV: if device not found in list of registered devices 485 * 486 * The callers are required to call dev_pm_opp_put() for the returned OPP after 487 * use. 488 */ 489 struct dev_pm_opp *dev_pm_opp_find_freq_floor(struct device *dev, 490 unsigned long *freq) 491 { 492 struct opp_table *opp_table; 493 struct dev_pm_opp *temp_opp, *opp = ERR_PTR(-ERANGE); 494 495 if (!dev || !freq) { 496 dev_err(dev, "%s: Invalid argument freq=%p\n", __func__, freq); 497 return ERR_PTR(-EINVAL); 498 } 499 500 opp_table = _find_opp_table(dev); 501 if (IS_ERR(opp_table)) 502 return ERR_CAST(opp_table); 503 504 mutex_lock(&opp_table->lock); 505 506 list_for_each_entry(temp_opp, &opp_table->opp_list, node) { 507 if (temp_opp->available) { 508 /* go to the next node, before choosing prev */ 509 if (temp_opp->rate > *freq) 510 break; 511 else 512 opp = temp_opp; 513 } 514 } 515 516 /* Increment the reference count of OPP */ 517 if (!IS_ERR(opp)) 518 dev_pm_opp_get(opp); 519 mutex_unlock(&opp_table->lock); 520 dev_pm_opp_put_opp_table(opp_table); 521 522 if (!IS_ERR(opp)) 523 *freq = opp->rate; 524 525 return opp; 526 } 527 EXPORT_SYMBOL_GPL(dev_pm_opp_find_freq_floor); 528 529 /** 530 * dev_pm_opp_find_freq_ceil_by_volt() - Find OPP with highest frequency for 531 * target voltage. 532 * @dev: Device for which we do this operation. 533 * @u_volt: Target voltage. 534 * 535 * Search for OPP with highest (ceil) frequency and has voltage <= u_volt. 536 * 537 * Return: matching *opp, else returns ERR_PTR in case of error which should be 538 * handled using IS_ERR. 539 * 540 * Error return values can be: 541 * EINVAL: bad parameters 542 * 543 * The callers are required to call dev_pm_opp_put() for the returned OPP after 544 * use. 545 */ 546 struct dev_pm_opp *dev_pm_opp_find_freq_ceil_by_volt(struct device *dev, 547 unsigned long u_volt) 548 { 549 struct opp_table *opp_table; 550 struct dev_pm_opp *temp_opp, *opp = ERR_PTR(-ERANGE); 551 552 if (!dev || !u_volt) { 553 dev_err(dev, "%s: Invalid argument volt=%lu\n", __func__, 554 u_volt); 555 return ERR_PTR(-EINVAL); 556 } 557 558 opp_table = _find_opp_table(dev); 559 if (IS_ERR(opp_table)) 560 return ERR_CAST(opp_table); 561 562 mutex_lock(&opp_table->lock); 563 564 list_for_each_entry(temp_opp, &opp_table->opp_list, node) { 565 if (temp_opp->available) { 566 if (temp_opp->supplies[0].u_volt > u_volt) 567 break; 568 opp = temp_opp; 569 } 570 } 571 572 /* Increment the reference count of OPP */ 573 if (!IS_ERR(opp)) 574 dev_pm_opp_get(opp); 575 576 mutex_unlock(&opp_table->lock); 577 dev_pm_opp_put_opp_table(opp_table); 578 579 return opp; 580 } 581 EXPORT_SYMBOL_GPL(dev_pm_opp_find_freq_ceil_by_volt); 582 583 static int _set_opp_voltage(struct device *dev, struct regulator *reg, 584 struct dev_pm_opp_supply *supply) 585 { 586 int ret; 587 588 /* Regulator not available for device */ 589 if (IS_ERR(reg)) { 590 dev_dbg(dev, "%s: regulator not available: %ld\n", __func__, 591 PTR_ERR(reg)); 592 return 0; 593 } 594 595 dev_dbg(dev, "%s: voltages (mV): %lu %lu %lu\n", __func__, 596 supply->u_volt_min, supply->u_volt, supply->u_volt_max); 597 598 ret = regulator_set_voltage_triplet(reg, supply->u_volt_min, 599 supply->u_volt, supply->u_volt_max); 600 if (ret) 601 dev_err(dev, "%s: failed to set voltage (%lu %lu %lu mV): %d\n", 602 __func__, supply->u_volt_min, supply->u_volt, 603 supply->u_volt_max, ret); 604 605 return ret; 606 } 607 608 static inline int _generic_set_opp_clk_only(struct device *dev, struct clk *clk, 609 unsigned long freq) 610 { 611 int ret; 612 613 ret = clk_set_rate(clk, freq); 614 if (ret) { 615 dev_err(dev, "%s: failed to set clock rate: %d\n", __func__, 616 ret); 617 } 618 619 return ret; 620 } 621 622 static int _generic_set_opp_regulator(const struct opp_table *opp_table, 623 struct device *dev, 624 unsigned long old_freq, 625 unsigned long freq, 626 struct dev_pm_opp_supply *old_supply, 627 struct dev_pm_opp_supply *new_supply) 628 { 629 struct regulator *reg = opp_table->regulators[0]; 630 int ret; 631 632 /* This function only supports single regulator per device */ 633 if (WARN_ON(opp_table->regulator_count > 1)) { 634 dev_err(dev, "multiple regulators are not supported\n"); 635 return -EINVAL; 636 } 637 638 /* Scaling up? Scale voltage before frequency */ 639 if (freq >= old_freq) { 640 ret = _set_opp_voltage(dev, reg, new_supply); 641 if (ret) 642 goto restore_voltage; 643 } 644 645 /* Change frequency */ 646 ret = _generic_set_opp_clk_only(dev, opp_table->clk, freq); 647 if (ret) 648 goto restore_voltage; 649 650 /* Scaling down? Scale voltage after frequency */ 651 if (freq < old_freq) { 652 ret = _set_opp_voltage(dev, reg, new_supply); 653 if (ret) 654 goto restore_freq; 655 } 656 657 return 0; 658 659 restore_freq: 660 if (_generic_set_opp_clk_only(dev, opp_table->clk, old_freq)) 661 dev_err(dev, "%s: failed to restore old-freq (%lu Hz)\n", 662 __func__, old_freq); 663 restore_voltage: 664 /* This shouldn't harm even if the voltages weren't updated earlier */ 665 if (old_supply) 666 _set_opp_voltage(dev, reg, old_supply); 667 668 return ret; 669 } 670 671 static int _set_opp_custom(const struct opp_table *opp_table, 672 struct device *dev, unsigned long old_freq, 673 unsigned long freq, 674 struct dev_pm_opp_supply *old_supply, 675 struct dev_pm_opp_supply *new_supply) 676 { 677 struct dev_pm_set_opp_data *data; 678 int size; 679 680 data = opp_table->set_opp_data; 681 data->regulators = opp_table->regulators; 682 data->regulator_count = opp_table->regulator_count; 683 data->clk = opp_table->clk; 684 data->dev = dev; 685 686 data->old_opp.rate = old_freq; 687 size = sizeof(*old_supply) * opp_table->regulator_count; 688 if (IS_ERR(old_supply)) 689 memset(data->old_opp.supplies, 0, size); 690 else 691 memcpy(data->old_opp.supplies, old_supply, size); 692 693 data->new_opp.rate = freq; 694 memcpy(data->new_opp.supplies, new_supply, size); 695 696 return opp_table->set_opp(data); 697 } 698 699 /* This is only called for PM domain for now */ 700 static int _set_required_opps(struct device *dev, 701 struct opp_table *opp_table, 702 struct dev_pm_opp *opp) 703 { 704 struct opp_table **required_opp_tables = opp_table->required_opp_tables; 705 struct device **genpd_virt_devs = opp_table->genpd_virt_devs; 706 unsigned int pstate; 707 int i, ret = 0; 708 709 if (!required_opp_tables) 710 return 0; 711 712 /* Single genpd case */ 713 if (!genpd_virt_devs) { 714 pstate = opp->required_opps[0]->pstate; 715 ret = dev_pm_genpd_set_performance_state(dev, pstate); 716 if (ret) { 717 dev_err(dev, "Failed to set performance state of %s: %d (%d)\n", 718 dev_name(dev), pstate, ret); 719 } 720 return ret; 721 } 722 723 /* Multiple genpd case */ 724 725 /* 726 * Acquire genpd_virt_dev_lock to make sure we don't use a genpd_dev 727 * after it is freed from another thread. 728 */ 729 mutex_lock(&opp_table->genpd_virt_dev_lock); 730 731 for (i = 0; i < opp_table->required_opp_count; i++) { 732 pstate = opp->required_opps[i]->pstate; 733 734 if (!genpd_virt_devs[i]) 735 continue; 736 737 ret = dev_pm_genpd_set_performance_state(genpd_virt_devs[i], pstate); 738 if (ret) { 739 dev_err(dev, "Failed to set performance rate of %s: %d (%d)\n", 740 dev_name(genpd_virt_devs[i]), pstate, ret); 741 break; 742 } 743 } 744 mutex_unlock(&opp_table->genpd_virt_dev_lock); 745 746 return ret; 747 } 748 749 /** 750 * dev_pm_opp_set_rate() - Configure new OPP based on frequency 751 * @dev: device for which we do this operation 752 * @target_freq: frequency to achieve 753 * 754 * This configures the power-supplies and clock source to the levels specified 755 * by the OPP corresponding to the target_freq. 756 */ 757 int dev_pm_opp_set_rate(struct device *dev, unsigned long target_freq) 758 { 759 struct opp_table *opp_table; 760 unsigned long freq, old_freq; 761 struct dev_pm_opp *old_opp, *opp; 762 struct clk *clk; 763 int ret; 764 765 if (unlikely(!target_freq)) { 766 dev_err(dev, "%s: Invalid target frequency %lu\n", __func__, 767 target_freq); 768 return -EINVAL; 769 } 770 771 opp_table = _find_opp_table(dev); 772 if (IS_ERR(opp_table)) { 773 dev_err(dev, "%s: device opp doesn't exist\n", __func__); 774 return PTR_ERR(opp_table); 775 } 776 777 clk = opp_table->clk; 778 if (IS_ERR(clk)) { 779 dev_err(dev, "%s: No clock available for the device\n", 780 __func__); 781 ret = PTR_ERR(clk); 782 goto put_opp_table; 783 } 784 785 freq = clk_round_rate(clk, target_freq); 786 if ((long)freq <= 0) 787 freq = target_freq; 788 789 old_freq = clk_get_rate(clk); 790 791 /* Return early if nothing to do */ 792 if (old_freq == freq) { 793 dev_dbg(dev, "%s: old/new frequencies (%lu Hz) are same, nothing to do\n", 794 __func__, freq); 795 ret = 0; 796 goto put_opp_table; 797 } 798 799 old_opp = _find_freq_ceil(opp_table, &old_freq); 800 if (IS_ERR(old_opp)) { 801 dev_err(dev, "%s: failed to find current OPP for freq %lu (%ld)\n", 802 __func__, old_freq, PTR_ERR(old_opp)); 803 } 804 805 opp = _find_freq_ceil(opp_table, &freq); 806 if (IS_ERR(opp)) { 807 ret = PTR_ERR(opp); 808 dev_err(dev, "%s: failed to find OPP for freq %lu (%d)\n", 809 __func__, freq, ret); 810 goto put_old_opp; 811 } 812 813 dev_dbg(dev, "%s: switching OPP: %lu Hz --> %lu Hz\n", __func__, 814 old_freq, freq); 815 816 /* Scaling up? Configure required OPPs before frequency */ 817 if (freq >= old_freq) { 818 ret = _set_required_opps(dev, opp_table, opp); 819 if (ret) 820 goto put_opp; 821 } 822 823 if (opp_table->set_opp) { 824 ret = _set_opp_custom(opp_table, dev, old_freq, freq, 825 IS_ERR(old_opp) ? NULL : old_opp->supplies, 826 opp->supplies); 827 } else if (opp_table->regulators) { 828 ret = _generic_set_opp_regulator(opp_table, dev, old_freq, freq, 829 IS_ERR(old_opp) ? NULL : old_opp->supplies, 830 opp->supplies); 831 } else { 832 /* Only frequency scaling */ 833 ret = _generic_set_opp_clk_only(dev, clk, freq); 834 } 835 836 /* Scaling down? Configure required OPPs after frequency */ 837 if (!ret && freq < old_freq) { 838 ret = _set_required_opps(dev, opp_table, opp); 839 if (ret) 840 dev_err(dev, "Failed to set required opps: %d\n", ret); 841 } 842 843 put_opp: 844 dev_pm_opp_put(opp); 845 put_old_opp: 846 if (!IS_ERR(old_opp)) 847 dev_pm_opp_put(old_opp); 848 put_opp_table: 849 dev_pm_opp_put_opp_table(opp_table); 850 return ret; 851 } 852 EXPORT_SYMBOL_GPL(dev_pm_opp_set_rate); 853 854 /* OPP-dev Helpers */ 855 static void _remove_opp_dev(struct opp_device *opp_dev, 856 struct opp_table *opp_table) 857 { 858 opp_debug_unregister(opp_dev, opp_table); 859 list_del(&opp_dev->node); 860 kfree(opp_dev); 861 } 862 863 static struct opp_device *_add_opp_dev_unlocked(const struct device *dev, 864 struct opp_table *opp_table) 865 { 866 struct opp_device *opp_dev; 867 868 opp_dev = kzalloc(sizeof(*opp_dev), GFP_KERNEL); 869 if (!opp_dev) 870 return NULL; 871 872 /* Initialize opp-dev */ 873 opp_dev->dev = dev; 874 875 list_add(&opp_dev->node, &opp_table->dev_list); 876 877 /* Create debugfs entries for the opp_table */ 878 opp_debug_register(opp_dev, opp_table); 879 880 return opp_dev; 881 } 882 883 struct opp_device *_add_opp_dev(const struct device *dev, 884 struct opp_table *opp_table) 885 { 886 struct opp_device *opp_dev; 887 888 mutex_lock(&opp_table->lock); 889 opp_dev = _add_opp_dev_unlocked(dev, opp_table); 890 mutex_unlock(&opp_table->lock); 891 892 return opp_dev; 893 } 894 895 static struct opp_table *_allocate_opp_table(struct device *dev, int index) 896 { 897 struct opp_table *opp_table; 898 struct opp_device *opp_dev; 899 int ret; 900 901 /* 902 * Allocate a new OPP table. In the infrequent case where a new 903 * device is needed to be added, we pay this penalty. 904 */ 905 opp_table = kzalloc(sizeof(*opp_table), GFP_KERNEL); 906 if (!opp_table) 907 return NULL; 908 909 mutex_init(&opp_table->lock); 910 mutex_init(&opp_table->genpd_virt_dev_lock); 911 INIT_LIST_HEAD(&opp_table->dev_list); 912 913 /* Mark regulator count uninitialized */ 914 opp_table->regulator_count = -1; 915 916 opp_dev = _add_opp_dev(dev, opp_table); 917 if (!opp_dev) { 918 kfree(opp_table); 919 return NULL; 920 } 921 922 _of_init_opp_table(opp_table, dev, index); 923 924 /* Find clk for the device */ 925 opp_table->clk = clk_get(dev, NULL); 926 if (IS_ERR(opp_table->clk)) { 927 ret = PTR_ERR(opp_table->clk); 928 if (ret != -EPROBE_DEFER) 929 dev_dbg(dev, "%s: Couldn't find clock: %d\n", __func__, 930 ret); 931 } 932 933 BLOCKING_INIT_NOTIFIER_HEAD(&opp_table->head); 934 INIT_LIST_HEAD(&opp_table->opp_list); 935 kref_init(&opp_table->kref); 936 937 /* Secure the device table modification */ 938 list_add(&opp_table->node, &opp_tables); 939 return opp_table; 940 } 941 942 void _get_opp_table_kref(struct opp_table *opp_table) 943 { 944 kref_get(&opp_table->kref); 945 } 946 947 static struct opp_table *_opp_get_opp_table(struct device *dev, int index) 948 { 949 struct opp_table *opp_table; 950 951 /* Hold our table modification lock here */ 952 mutex_lock(&opp_table_lock); 953 954 opp_table = _find_opp_table_unlocked(dev); 955 if (!IS_ERR(opp_table)) 956 goto unlock; 957 958 opp_table = _managed_opp(dev, index); 959 if (opp_table) { 960 if (!_add_opp_dev_unlocked(dev, opp_table)) { 961 dev_pm_opp_put_opp_table(opp_table); 962 opp_table = NULL; 963 } 964 goto unlock; 965 } 966 967 opp_table = _allocate_opp_table(dev, index); 968 969 unlock: 970 mutex_unlock(&opp_table_lock); 971 972 return opp_table; 973 } 974 975 struct opp_table *dev_pm_opp_get_opp_table(struct device *dev) 976 { 977 return _opp_get_opp_table(dev, 0); 978 } 979 EXPORT_SYMBOL_GPL(dev_pm_opp_get_opp_table); 980 981 struct opp_table *dev_pm_opp_get_opp_table_indexed(struct device *dev, 982 int index) 983 { 984 return _opp_get_opp_table(dev, index); 985 } 986 987 static void _opp_table_kref_release(struct kref *kref) 988 { 989 struct opp_table *opp_table = container_of(kref, struct opp_table, kref); 990 struct opp_device *opp_dev, *temp; 991 992 _of_clear_opp_table(opp_table); 993 994 /* Release clk */ 995 if (!IS_ERR(opp_table->clk)) 996 clk_put(opp_table->clk); 997 998 WARN_ON(!list_empty(&opp_table->opp_list)); 999 1000 list_for_each_entry_safe(opp_dev, temp, &opp_table->dev_list, node) { 1001 /* 1002 * The OPP table is getting removed, drop the performance state 1003 * constraints. 1004 */ 1005 if (opp_table->genpd_performance_state) 1006 dev_pm_genpd_set_performance_state((struct device *)(opp_dev->dev), 0); 1007 1008 _remove_opp_dev(opp_dev, opp_table); 1009 } 1010 1011 mutex_destroy(&opp_table->genpd_virt_dev_lock); 1012 mutex_destroy(&opp_table->lock); 1013 list_del(&opp_table->node); 1014 kfree(opp_table); 1015 1016 mutex_unlock(&opp_table_lock); 1017 } 1018 1019 void _opp_remove_all_static(struct opp_table *opp_table) 1020 { 1021 struct dev_pm_opp *opp, *tmp; 1022 1023 list_for_each_entry_safe(opp, tmp, &opp_table->opp_list, node) { 1024 if (!opp->dynamic) 1025 dev_pm_opp_put(opp); 1026 } 1027 1028 opp_table->parsed_static_opps = false; 1029 } 1030 1031 static void _opp_table_list_kref_release(struct kref *kref) 1032 { 1033 struct opp_table *opp_table = container_of(kref, struct opp_table, 1034 list_kref); 1035 1036 _opp_remove_all_static(opp_table); 1037 mutex_unlock(&opp_table_lock); 1038 } 1039 1040 void _put_opp_list_kref(struct opp_table *opp_table) 1041 { 1042 kref_put_mutex(&opp_table->list_kref, _opp_table_list_kref_release, 1043 &opp_table_lock); 1044 } 1045 1046 void dev_pm_opp_put_opp_table(struct opp_table *opp_table) 1047 { 1048 kref_put_mutex(&opp_table->kref, _opp_table_kref_release, 1049 &opp_table_lock); 1050 } 1051 EXPORT_SYMBOL_GPL(dev_pm_opp_put_opp_table); 1052 1053 void _opp_free(struct dev_pm_opp *opp) 1054 { 1055 kfree(opp); 1056 } 1057 1058 static void _opp_kref_release(struct dev_pm_opp *opp, 1059 struct opp_table *opp_table) 1060 { 1061 /* 1062 * Notify the changes in the availability of the operable 1063 * frequency/voltage list. 1064 */ 1065 blocking_notifier_call_chain(&opp_table->head, OPP_EVENT_REMOVE, opp); 1066 _of_opp_free_required_opps(opp_table, opp); 1067 opp_debug_remove_one(opp); 1068 list_del(&opp->node); 1069 kfree(opp); 1070 } 1071 1072 static void _opp_kref_release_unlocked(struct kref *kref) 1073 { 1074 struct dev_pm_opp *opp = container_of(kref, struct dev_pm_opp, kref); 1075 struct opp_table *opp_table = opp->opp_table; 1076 1077 _opp_kref_release(opp, opp_table); 1078 } 1079 1080 static void _opp_kref_release_locked(struct kref *kref) 1081 { 1082 struct dev_pm_opp *opp = container_of(kref, struct dev_pm_opp, kref); 1083 struct opp_table *opp_table = opp->opp_table; 1084 1085 _opp_kref_release(opp, opp_table); 1086 mutex_unlock(&opp_table->lock); 1087 } 1088 1089 void dev_pm_opp_get(struct dev_pm_opp *opp) 1090 { 1091 kref_get(&opp->kref); 1092 } 1093 1094 void dev_pm_opp_put(struct dev_pm_opp *opp) 1095 { 1096 kref_put_mutex(&opp->kref, _opp_kref_release_locked, 1097 &opp->opp_table->lock); 1098 } 1099 EXPORT_SYMBOL_GPL(dev_pm_opp_put); 1100 1101 static void dev_pm_opp_put_unlocked(struct dev_pm_opp *opp) 1102 { 1103 kref_put(&opp->kref, _opp_kref_release_unlocked); 1104 } 1105 1106 /** 1107 * dev_pm_opp_remove() - Remove an OPP from OPP table 1108 * @dev: device for which we do this operation 1109 * @freq: OPP to remove with matching 'freq' 1110 * 1111 * This function removes an opp from the opp table. 1112 */ 1113 void dev_pm_opp_remove(struct device *dev, unsigned long freq) 1114 { 1115 struct dev_pm_opp *opp; 1116 struct opp_table *opp_table; 1117 bool found = false; 1118 1119 opp_table = _find_opp_table(dev); 1120 if (IS_ERR(opp_table)) 1121 return; 1122 1123 mutex_lock(&opp_table->lock); 1124 1125 list_for_each_entry(opp, &opp_table->opp_list, node) { 1126 if (opp->rate == freq) { 1127 found = true; 1128 break; 1129 } 1130 } 1131 1132 mutex_unlock(&opp_table->lock); 1133 1134 if (found) { 1135 dev_pm_opp_put(opp); 1136 1137 /* Drop the reference taken by dev_pm_opp_add() */ 1138 dev_pm_opp_put_opp_table(opp_table); 1139 } else { 1140 dev_warn(dev, "%s: Couldn't find OPP with freq: %lu\n", 1141 __func__, freq); 1142 } 1143 1144 /* Drop the reference taken by _find_opp_table() */ 1145 dev_pm_opp_put_opp_table(opp_table); 1146 } 1147 EXPORT_SYMBOL_GPL(dev_pm_opp_remove); 1148 1149 /** 1150 * dev_pm_opp_remove_all_dynamic() - Remove all dynamically created OPPs 1151 * @dev: device for which we do this operation 1152 * 1153 * This function removes all dynamically created OPPs from the opp table. 1154 */ 1155 void dev_pm_opp_remove_all_dynamic(struct device *dev) 1156 { 1157 struct opp_table *opp_table; 1158 struct dev_pm_opp *opp, *temp; 1159 int count = 0; 1160 1161 opp_table = _find_opp_table(dev); 1162 if (IS_ERR(opp_table)) 1163 return; 1164 1165 mutex_lock(&opp_table->lock); 1166 list_for_each_entry_safe(opp, temp, &opp_table->opp_list, node) { 1167 if (opp->dynamic) { 1168 dev_pm_opp_put_unlocked(opp); 1169 count++; 1170 } 1171 } 1172 mutex_unlock(&opp_table->lock); 1173 1174 /* Drop the references taken by dev_pm_opp_add() */ 1175 while (count--) 1176 dev_pm_opp_put_opp_table(opp_table); 1177 1178 /* Drop the reference taken by _find_opp_table() */ 1179 dev_pm_opp_put_opp_table(opp_table); 1180 } 1181 EXPORT_SYMBOL_GPL(dev_pm_opp_remove_all_dynamic); 1182 1183 struct dev_pm_opp *_opp_allocate(struct opp_table *table) 1184 { 1185 struct dev_pm_opp *opp; 1186 int count, supply_size; 1187 1188 /* Allocate space for at least one supply */ 1189 count = table->regulator_count > 0 ? table->regulator_count : 1; 1190 supply_size = sizeof(*opp->supplies) * count; 1191 1192 /* allocate new OPP node and supplies structures */ 1193 opp = kzalloc(sizeof(*opp) + supply_size, GFP_KERNEL); 1194 if (!opp) 1195 return NULL; 1196 1197 /* Put the supplies at the end of the OPP structure as an empty array */ 1198 opp->supplies = (struct dev_pm_opp_supply *)(opp + 1); 1199 INIT_LIST_HEAD(&opp->node); 1200 1201 return opp; 1202 } 1203 1204 static bool _opp_supported_by_regulators(struct dev_pm_opp *opp, 1205 struct opp_table *opp_table) 1206 { 1207 struct regulator *reg; 1208 int i; 1209 1210 if (!opp_table->regulators) 1211 return true; 1212 1213 for (i = 0; i < opp_table->regulator_count; i++) { 1214 reg = opp_table->regulators[i]; 1215 1216 if (!regulator_is_supported_voltage(reg, 1217 opp->supplies[i].u_volt_min, 1218 opp->supplies[i].u_volt_max)) { 1219 pr_warn("%s: OPP minuV: %lu maxuV: %lu, not supported by regulator\n", 1220 __func__, opp->supplies[i].u_volt_min, 1221 opp->supplies[i].u_volt_max); 1222 return false; 1223 } 1224 } 1225 1226 return true; 1227 } 1228 1229 static int _opp_is_duplicate(struct device *dev, struct dev_pm_opp *new_opp, 1230 struct opp_table *opp_table, 1231 struct list_head **head) 1232 { 1233 struct dev_pm_opp *opp; 1234 1235 /* 1236 * Insert new OPP in order of increasing frequency and discard if 1237 * already present. 1238 * 1239 * Need to use &opp_table->opp_list in the condition part of the 'for' 1240 * loop, don't replace it with head otherwise it will become an infinite 1241 * loop. 1242 */ 1243 list_for_each_entry(opp, &opp_table->opp_list, node) { 1244 if (new_opp->rate > opp->rate) { 1245 *head = &opp->node; 1246 continue; 1247 } 1248 1249 if (new_opp->rate < opp->rate) 1250 return 0; 1251 1252 /* Duplicate OPPs */ 1253 dev_warn(dev, "%s: duplicate OPPs detected. Existing: freq: %lu, volt: %lu, enabled: %d. New: freq: %lu, volt: %lu, enabled: %d\n", 1254 __func__, opp->rate, opp->supplies[0].u_volt, 1255 opp->available, new_opp->rate, 1256 new_opp->supplies[0].u_volt, new_opp->available); 1257 1258 /* Should we compare voltages for all regulators here ? */ 1259 return opp->available && 1260 new_opp->supplies[0].u_volt == opp->supplies[0].u_volt ? -EBUSY : -EEXIST; 1261 } 1262 1263 return 0; 1264 } 1265 1266 /* 1267 * Returns: 1268 * 0: On success. And appropriate error message for duplicate OPPs. 1269 * -EBUSY: For OPP with same freq/volt and is available. The callers of 1270 * _opp_add() must return 0 if they receive -EBUSY from it. This is to make 1271 * sure we don't print error messages unnecessarily if different parts of 1272 * kernel try to initialize the OPP table. 1273 * -EEXIST: For OPP with same freq but different volt or is unavailable. This 1274 * should be considered an error by the callers of _opp_add(). 1275 */ 1276 int _opp_add(struct device *dev, struct dev_pm_opp *new_opp, 1277 struct opp_table *opp_table, bool rate_not_available) 1278 { 1279 struct list_head *head; 1280 int ret; 1281 1282 mutex_lock(&opp_table->lock); 1283 head = &opp_table->opp_list; 1284 1285 if (likely(!rate_not_available)) { 1286 ret = _opp_is_duplicate(dev, new_opp, opp_table, &head); 1287 if (ret) { 1288 mutex_unlock(&opp_table->lock); 1289 return ret; 1290 } 1291 } 1292 1293 list_add(&new_opp->node, head); 1294 mutex_unlock(&opp_table->lock); 1295 1296 new_opp->opp_table = opp_table; 1297 kref_init(&new_opp->kref); 1298 1299 opp_debug_create_one(new_opp, opp_table); 1300 1301 if (!_opp_supported_by_regulators(new_opp, opp_table)) { 1302 new_opp->available = false; 1303 dev_warn(dev, "%s: OPP not supported by regulators (%lu)\n", 1304 __func__, new_opp->rate); 1305 } 1306 1307 return 0; 1308 } 1309 1310 /** 1311 * _opp_add_v1() - Allocate a OPP based on v1 bindings. 1312 * @opp_table: OPP table 1313 * @dev: device for which we do this operation 1314 * @freq: Frequency in Hz for this OPP 1315 * @u_volt: Voltage in uVolts for this OPP 1316 * @dynamic: Dynamically added OPPs. 1317 * 1318 * This function adds an opp definition to the opp table and returns status. 1319 * The opp is made available by default and it can be controlled using 1320 * dev_pm_opp_enable/disable functions and may be removed by dev_pm_opp_remove. 1321 * 1322 * NOTE: "dynamic" parameter impacts OPPs added by the dev_pm_opp_of_add_table 1323 * and freed by dev_pm_opp_of_remove_table. 1324 * 1325 * Return: 1326 * 0 On success OR 1327 * Duplicate OPPs (both freq and volt are same) and opp->available 1328 * -EEXIST Freq are same and volt are different OR 1329 * Duplicate OPPs (both freq and volt are same) and !opp->available 1330 * -ENOMEM Memory allocation failure 1331 */ 1332 int _opp_add_v1(struct opp_table *opp_table, struct device *dev, 1333 unsigned long freq, long u_volt, bool dynamic) 1334 { 1335 struct dev_pm_opp *new_opp; 1336 unsigned long tol; 1337 int ret; 1338 1339 new_opp = _opp_allocate(opp_table); 1340 if (!new_opp) 1341 return -ENOMEM; 1342 1343 /* populate the opp table */ 1344 new_opp->rate = freq; 1345 tol = u_volt * opp_table->voltage_tolerance_v1 / 100; 1346 new_opp->supplies[0].u_volt = u_volt; 1347 new_opp->supplies[0].u_volt_min = u_volt - tol; 1348 new_opp->supplies[0].u_volt_max = u_volt + tol; 1349 new_opp->available = true; 1350 new_opp->dynamic = dynamic; 1351 1352 ret = _opp_add(dev, new_opp, opp_table, false); 1353 if (ret) { 1354 /* Don't return error for duplicate OPPs */ 1355 if (ret == -EBUSY) 1356 ret = 0; 1357 goto free_opp; 1358 } 1359 1360 /* 1361 * Notify the changes in the availability of the operable 1362 * frequency/voltage list. 1363 */ 1364 blocking_notifier_call_chain(&opp_table->head, OPP_EVENT_ADD, new_opp); 1365 return 0; 1366 1367 free_opp: 1368 _opp_free(new_opp); 1369 1370 return ret; 1371 } 1372 1373 /** 1374 * dev_pm_opp_set_supported_hw() - Set supported platforms 1375 * @dev: Device for which supported-hw has to be set. 1376 * @versions: Array of hierarchy of versions to match. 1377 * @count: Number of elements in the array. 1378 * 1379 * This is required only for the V2 bindings, and it enables a platform to 1380 * specify the hierarchy of versions it supports. OPP layer will then enable 1381 * OPPs, which are available for those versions, based on its 'opp-supported-hw' 1382 * property. 1383 */ 1384 struct opp_table *dev_pm_opp_set_supported_hw(struct device *dev, 1385 const u32 *versions, unsigned int count) 1386 { 1387 struct opp_table *opp_table; 1388 1389 opp_table = dev_pm_opp_get_opp_table(dev); 1390 if (!opp_table) 1391 return ERR_PTR(-ENOMEM); 1392 1393 /* Make sure there are no concurrent readers while updating opp_table */ 1394 WARN_ON(!list_empty(&opp_table->opp_list)); 1395 1396 /* Another CPU that shares the OPP table has set the property ? */ 1397 if (opp_table->supported_hw) 1398 return opp_table; 1399 1400 opp_table->supported_hw = kmemdup(versions, count * sizeof(*versions), 1401 GFP_KERNEL); 1402 if (!opp_table->supported_hw) { 1403 dev_pm_opp_put_opp_table(opp_table); 1404 return ERR_PTR(-ENOMEM); 1405 } 1406 1407 opp_table->supported_hw_count = count; 1408 1409 return opp_table; 1410 } 1411 EXPORT_SYMBOL_GPL(dev_pm_opp_set_supported_hw); 1412 1413 /** 1414 * dev_pm_opp_put_supported_hw() - Releases resources blocked for supported hw 1415 * @opp_table: OPP table returned by dev_pm_opp_set_supported_hw(). 1416 * 1417 * This is required only for the V2 bindings, and is called for a matching 1418 * dev_pm_opp_set_supported_hw(). Until this is called, the opp_table structure 1419 * will not be freed. 1420 */ 1421 void dev_pm_opp_put_supported_hw(struct opp_table *opp_table) 1422 { 1423 /* Make sure there are no concurrent readers while updating opp_table */ 1424 WARN_ON(!list_empty(&opp_table->opp_list)); 1425 1426 kfree(opp_table->supported_hw); 1427 opp_table->supported_hw = NULL; 1428 opp_table->supported_hw_count = 0; 1429 1430 dev_pm_opp_put_opp_table(opp_table); 1431 } 1432 EXPORT_SYMBOL_GPL(dev_pm_opp_put_supported_hw); 1433 1434 /** 1435 * dev_pm_opp_set_prop_name() - Set prop-extn name 1436 * @dev: Device for which the prop-name has to be set. 1437 * @name: name to postfix to properties. 1438 * 1439 * This is required only for the V2 bindings, and it enables a platform to 1440 * specify the extn to be used for certain property names. The properties to 1441 * which the extension will apply are opp-microvolt and opp-microamp. OPP core 1442 * should postfix the property name with -<name> while looking for them. 1443 */ 1444 struct opp_table *dev_pm_opp_set_prop_name(struct device *dev, const char *name) 1445 { 1446 struct opp_table *opp_table; 1447 1448 opp_table = dev_pm_opp_get_opp_table(dev); 1449 if (!opp_table) 1450 return ERR_PTR(-ENOMEM); 1451 1452 /* Make sure there are no concurrent readers while updating opp_table */ 1453 WARN_ON(!list_empty(&opp_table->opp_list)); 1454 1455 /* Another CPU that shares the OPP table has set the property ? */ 1456 if (opp_table->prop_name) 1457 return opp_table; 1458 1459 opp_table->prop_name = kstrdup(name, GFP_KERNEL); 1460 if (!opp_table->prop_name) { 1461 dev_pm_opp_put_opp_table(opp_table); 1462 return ERR_PTR(-ENOMEM); 1463 } 1464 1465 return opp_table; 1466 } 1467 EXPORT_SYMBOL_GPL(dev_pm_opp_set_prop_name); 1468 1469 /** 1470 * dev_pm_opp_put_prop_name() - Releases resources blocked for prop-name 1471 * @opp_table: OPP table returned by dev_pm_opp_set_prop_name(). 1472 * 1473 * This is required only for the V2 bindings, and is called for a matching 1474 * dev_pm_opp_set_prop_name(). Until this is called, the opp_table structure 1475 * will not be freed. 1476 */ 1477 void dev_pm_opp_put_prop_name(struct opp_table *opp_table) 1478 { 1479 /* Make sure there are no concurrent readers while updating opp_table */ 1480 WARN_ON(!list_empty(&opp_table->opp_list)); 1481 1482 kfree(opp_table->prop_name); 1483 opp_table->prop_name = NULL; 1484 1485 dev_pm_opp_put_opp_table(opp_table); 1486 } 1487 EXPORT_SYMBOL_GPL(dev_pm_opp_put_prop_name); 1488 1489 static int _allocate_set_opp_data(struct opp_table *opp_table) 1490 { 1491 struct dev_pm_set_opp_data *data; 1492 int len, count = opp_table->regulator_count; 1493 1494 if (WARN_ON(!opp_table->regulators)) 1495 return -EINVAL; 1496 1497 /* space for set_opp_data */ 1498 len = sizeof(*data); 1499 1500 /* space for old_opp.supplies and new_opp.supplies */ 1501 len += 2 * sizeof(struct dev_pm_opp_supply) * count; 1502 1503 data = kzalloc(len, GFP_KERNEL); 1504 if (!data) 1505 return -ENOMEM; 1506 1507 data->old_opp.supplies = (void *)(data + 1); 1508 data->new_opp.supplies = data->old_opp.supplies + count; 1509 1510 opp_table->set_opp_data = data; 1511 1512 return 0; 1513 } 1514 1515 static void _free_set_opp_data(struct opp_table *opp_table) 1516 { 1517 kfree(opp_table->set_opp_data); 1518 opp_table->set_opp_data = NULL; 1519 } 1520 1521 /** 1522 * dev_pm_opp_set_regulators() - Set regulator names for the device 1523 * @dev: Device for which regulator name is being set. 1524 * @names: Array of pointers to the names of the regulator. 1525 * @count: Number of regulators. 1526 * 1527 * In order to support OPP switching, OPP layer needs to know the name of the 1528 * device's regulators, as the core would be required to switch voltages as 1529 * well. 1530 * 1531 * This must be called before any OPPs are initialized for the device. 1532 */ 1533 struct opp_table *dev_pm_opp_set_regulators(struct device *dev, 1534 const char * const names[], 1535 unsigned int count) 1536 { 1537 struct opp_table *opp_table; 1538 struct regulator *reg; 1539 int ret, i; 1540 1541 opp_table = dev_pm_opp_get_opp_table(dev); 1542 if (!opp_table) 1543 return ERR_PTR(-ENOMEM); 1544 1545 /* This should be called before OPPs are initialized */ 1546 if (WARN_ON(!list_empty(&opp_table->opp_list))) { 1547 ret = -EBUSY; 1548 goto err; 1549 } 1550 1551 /* Another CPU that shares the OPP table has set the regulators ? */ 1552 if (opp_table->regulators) 1553 return opp_table; 1554 1555 opp_table->regulators = kmalloc_array(count, 1556 sizeof(*opp_table->regulators), 1557 GFP_KERNEL); 1558 if (!opp_table->regulators) { 1559 ret = -ENOMEM; 1560 goto err; 1561 } 1562 1563 for (i = 0; i < count; i++) { 1564 reg = regulator_get_optional(dev, names[i]); 1565 if (IS_ERR(reg)) { 1566 ret = PTR_ERR(reg); 1567 if (ret != -EPROBE_DEFER) 1568 dev_err(dev, "%s: no regulator (%s) found: %d\n", 1569 __func__, names[i], ret); 1570 goto free_regulators; 1571 } 1572 1573 opp_table->regulators[i] = reg; 1574 } 1575 1576 opp_table->regulator_count = count; 1577 1578 /* Allocate block only once to pass to set_opp() routines */ 1579 ret = _allocate_set_opp_data(opp_table); 1580 if (ret) 1581 goto free_regulators; 1582 1583 return opp_table; 1584 1585 free_regulators: 1586 while (i != 0) 1587 regulator_put(opp_table->regulators[--i]); 1588 1589 kfree(opp_table->regulators); 1590 opp_table->regulators = NULL; 1591 opp_table->regulator_count = -1; 1592 err: 1593 dev_pm_opp_put_opp_table(opp_table); 1594 1595 return ERR_PTR(ret); 1596 } 1597 EXPORT_SYMBOL_GPL(dev_pm_opp_set_regulators); 1598 1599 /** 1600 * dev_pm_opp_put_regulators() - Releases resources blocked for regulator 1601 * @opp_table: OPP table returned from dev_pm_opp_set_regulators(). 1602 */ 1603 void dev_pm_opp_put_regulators(struct opp_table *opp_table) 1604 { 1605 int i; 1606 1607 if (!opp_table->regulators) 1608 goto put_opp_table; 1609 1610 /* Make sure there are no concurrent readers while updating opp_table */ 1611 WARN_ON(!list_empty(&opp_table->opp_list)); 1612 1613 for (i = opp_table->regulator_count - 1; i >= 0; i--) 1614 regulator_put(opp_table->regulators[i]); 1615 1616 _free_set_opp_data(opp_table); 1617 1618 kfree(opp_table->regulators); 1619 opp_table->regulators = NULL; 1620 opp_table->regulator_count = -1; 1621 1622 put_opp_table: 1623 dev_pm_opp_put_opp_table(opp_table); 1624 } 1625 EXPORT_SYMBOL_GPL(dev_pm_opp_put_regulators); 1626 1627 /** 1628 * dev_pm_opp_set_clkname() - Set clk name for the device 1629 * @dev: Device for which clk name is being set. 1630 * @name: Clk name. 1631 * 1632 * In order to support OPP switching, OPP layer needs to get pointer to the 1633 * clock for the device. Simple cases work fine without using this routine (i.e. 1634 * by passing connection-id as NULL), but for a device with multiple clocks 1635 * available, the OPP core needs to know the exact name of the clk to use. 1636 * 1637 * This must be called before any OPPs are initialized for the device. 1638 */ 1639 struct opp_table *dev_pm_opp_set_clkname(struct device *dev, const char *name) 1640 { 1641 struct opp_table *opp_table; 1642 int ret; 1643 1644 opp_table = dev_pm_opp_get_opp_table(dev); 1645 if (!opp_table) 1646 return ERR_PTR(-ENOMEM); 1647 1648 /* This should be called before OPPs are initialized */ 1649 if (WARN_ON(!list_empty(&opp_table->opp_list))) { 1650 ret = -EBUSY; 1651 goto err; 1652 } 1653 1654 /* Already have default clk set, free it */ 1655 if (!IS_ERR(opp_table->clk)) 1656 clk_put(opp_table->clk); 1657 1658 /* Find clk for the device */ 1659 opp_table->clk = clk_get(dev, name); 1660 if (IS_ERR(opp_table->clk)) { 1661 ret = PTR_ERR(opp_table->clk); 1662 if (ret != -EPROBE_DEFER) { 1663 dev_err(dev, "%s: Couldn't find clock: %d\n", __func__, 1664 ret); 1665 } 1666 goto err; 1667 } 1668 1669 return opp_table; 1670 1671 err: 1672 dev_pm_opp_put_opp_table(opp_table); 1673 1674 return ERR_PTR(ret); 1675 } 1676 EXPORT_SYMBOL_GPL(dev_pm_opp_set_clkname); 1677 1678 /** 1679 * dev_pm_opp_put_clkname() - Releases resources blocked for clk. 1680 * @opp_table: OPP table returned from dev_pm_opp_set_clkname(). 1681 */ 1682 void dev_pm_opp_put_clkname(struct opp_table *opp_table) 1683 { 1684 /* Make sure there are no concurrent readers while updating opp_table */ 1685 WARN_ON(!list_empty(&opp_table->opp_list)); 1686 1687 clk_put(opp_table->clk); 1688 opp_table->clk = ERR_PTR(-EINVAL); 1689 1690 dev_pm_opp_put_opp_table(opp_table); 1691 } 1692 EXPORT_SYMBOL_GPL(dev_pm_opp_put_clkname); 1693 1694 /** 1695 * dev_pm_opp_register_set_opp_helper() - Register custom set OPP helper 1696 * @dev: Device for which the helper is getting registered. 1697 * @set_opp: Custom set OPP helper. 1698 * 1699 * This is useful to support complex platforms (like platforms with multiple 1700 * regulators per device), instead of the generic OPP set rate helper. 1701 * 1702 * This must be called before any OPPs are initialized for the device. 1703 */ 1704 struct opp_table *dev_pm_opp_register_set_opp_helper(struct device *dev, 1705 int (*set_opp)(struct dev_pm_set_opp_data *data)) 1706 { 1707 struct opp_table *opp_table; 1708 1709 if (!set_opp) 1710 return ERR_PTR(-EINVAL); 1711 1712 opp_table = dev_pm_opp_get_opp_table(dev); 1713 if (!opp_table) 1714 return ERR_PTR(-ENOMEM); 1715 1716 /* This should be called before OPPs are initialized */ 1717 if (WARN_ON(!list_empty(&opp_table->opp_list))) { 1718 dev_pm_opp_put_opp_table(opp_table); 1719 return ERR_PTR(-EBUSY); 1720 } 1721 1722 /* Another CPU that shares the OPP table has set the helper ? */ 1723 if (!opp_table->set_opp) 1724 opp_table->set_opp = set_opp; 1725 1726 return opp_table; 1727 } 1728 EXPORT_SYMBOL_GPL(dev_pm_opp_register_set_opp_helper); 1729 1730 /** 1731 * dev_pm_opp_unregister_set_opp_helper() - Releases resources blocked for 1732 * set_opp helper 1733 * @opp_table: OPP table returned from dev_pm_opp_register_set_opp_helper(). 1734 * 1735 * Release resources blocked for platform specific set_opp helper. 1736 */ 1737 void dev_pm_opp_unregister_set_opp_helper(struct opp_table *opp_table) 1738 { 1739 /* Make sure there are no concurrent readers while updating opp_table */ 1740 WARN_ON(!list_empty(&opp_table->opp_list)); 1741 1742 opp_table->set_opp = NULL; 1743 dev_pm_opp_put_opp_table(opp_table); 1744 } 1745 EXPORT_SYMBOL_GPL(dev_pm_opp_unregister_set_opp_helper); 1746 1747 static void _opp_detach_genpd(struct opp_table *opp_table) 1748 { 1749 int index; 1750 1751 for (index = 0; index < opp_table->required_opp_count; index++) { 1752 if (!opp_table->genpd_virt_devs[index]) 1753 continue; 1754 1755 dev_pm_domain_detach(opp_table->genpd_virt_devs[index], false); 1756 opp_table->genpd_virt_devs[index] = NULL; 1757 } 1758 } 1759 1760 /** 1761 * dev_pm_opp_attach_genpd - Attach genpd(s) for the device and save virtual device pointer 1762 * @dev: Consumer device for which the genpd is getting attached. 1763 * @names: Null terminated array of pointers containing names of genpd to attach. 1764 * 1765 * Multiple generic power domains for a device are supported with the help of 1766 * virtual genpd devices, which are created for each consumer device - genpd 1767 * pair. These are the device structures which are attached to the power domain 1768 * and are required by the OPP core to set the performance state of the genpd. 1769 * The same API also works for the case where single genpd is available and so 1770 * we don't need to support that separately. 1771 * 1772 * This helper will normally be called by the consumer driver of the device 1773 * "dev", as only that has details of the genpd names. 1774 * 1775 * This helper needs to be called once with a list of all genpd to attach. 1776 * Otherwise the original device structure will be used instead by the OPP core. 1777 */ 1778 struct opp_table *dev_pm_opp_attach_genpd(struct device *dev, const char **names) 1779 { 1780 struct opp_table *opp_table; 1781 struct device *virt_dev; 1782 int index, ret = -EINVAL; 1783 const char **name = names; 1784 1785 opp_table = dev_pm_opp_get_opp_table(dev); 1786 if (!opp_table) 1787 return ERR_PTR(-ENOMEM); 1788 1789 /* 1790 * If the genpd's OPP table isn't already initialized, parsing of the 1791 * required-opps fail for dev. We should retry this after genpd's OPP 1792 * table is added. 1793 */ 1794 if (!opp_table->required_opp_count) { 1795 ret = -EPROBE_DEFER; 1796 goto put_table; 1797 } 1798 1799 mutex_lock(&opp_table->genpd_virt_dev_lock); 1800 1801 while (*name) { 1802 index = of_property_match_string(dev->of_node, 1803 "power-domain-names", *name); 1804 if (index < 0) { 1805 dev_err(dev, "Failed to find power domain: %s (%d)\n", 1806 *name, index); 1807 goto err; 1808 } 1809 1810 if (index >= opp_table->required_opp_count) { 1811 dev_err(dev, "Index can't be greater than required-opp-count - 1, %s (%d : %d)\n", 1812 *name, opp_table->required_opp_count, index); 1813 goto err; 1814 } 1815 1816 if (opp_table->genpd_virt_devs[index]) { 1817 dev_err(dev, "Genpd virtual device already set %s\n", 1818 *name); 1819 goto err; 1820 } 1821 1822 virt_dev = dev_pm_domain_attach_by_name(dev, *name); 1823 if (IS_ERR(virt_dev)) { 1824 ret = PTR_ERR(virt_dev); 1825 dev_err(dev, "Couldn't attach to pm_domain: %d\n", ret); 1826 goto err; 1827 } 1828 1829 opp_table->genpd_virt_devs[index] = virt_dev; 1830 name++; 1831 } 1832 1833 mutex_unlock(&opp_table->genpd_virt_dev_lock); 1834 1835 return opp_table; 1836 1837 err: 1838 _opp_detach_genpd(opp_table); 1839 mutex_unlock(&opp_table->genpd_virt_dev_lock); 1840 1841 put_table: 1842 dev_pm_opp_put_opp_table(opp_table); 1843 1844 return ERR_PTR(ret); 1845 } 1846 EXPORT_SYMBOL_GPL(dev_pm_opp_attach_genpd); 1847 1848 /** 1849 * dev_pm_opp_detach_genpd() - Detach genpd(s) from the device. 1850 * @opp_table: OPP table returned by dev_pm_opp_attach_genpd(). 1851 * 1852 * This detaches the genpd(s), resets the virtual device pointers, and puts the 1853 * OPP table. 1854 */ 1855 void dev_pm_opp_detach_genpd(struct opp_table *opp_table) 1856 { 1857 /* 1858 * Acquire genpd_virt_dev_lock to make sure virt_dev isn't getting 1859 * used in parallel. 1860 */ 1861 mutex_lock(&opp_table->genpd_virt_dev_lock); 1862 _opp_detach_genpd(opp_table); 1863 mutex_unlock(&opp_table->genpd_virt_dev_lock); 1864 1865 dev_pm_opp_put_opp_table(opp_table); 1866 } 1867 EXPORT_SYMBOL_GPL(dev_pm_opp_detach_genpd); 1868 1869 /** 1870 * dev_pm_opp_xlate_performance_state() - Find required OPP's pstate for src_table. 1871 * @src_table: OPP table which has dst_table as one of its required OPP table. 1872 * @dst_table: Required OPP table of the src_table. 1873 * @pstate: Current performance state of the src_table. 1874 * 1875 * This Returns pstate of the OPP (present in @dst_table) pointed out by the 1876 * "required-opps" property of the OPP (present in @src_table) which has 1877 * performance state set to @pstate. 1878 * 1879 * Return: Zero or positive performance state on success, otherwise negative 1880 * value on errors. 1881 */ 1882 int dev_pm_opp_xlate_performance_state(struct opp_table *src_table, 1883 struct opp_table *dst_table, 1884 unsigned int pstate) 1885 { 1886 struct dev_pm_opp *opp; 1887 int dest_pstate = -EINVAL; 1888 int i; 1889 1890 if (!pstate) 1891 return 0; 1892 1893 /* 1894 * Normally the src_table will have the "required_opps" property set to 1895 * point to one of the OPPs in the dst_table, but in some cases the 1896 * genpd and its master have one to one mapping of performance states 1897 * and so none of them have the "required-opps" property set. Return the 1898 * pstate of the src_table as it is in such cases. 1899 */ 1900 if (!src_table->required_opp_count) 1901 return pstate; 1902 1903 for (i = 0; i < src_table->required_opp_count; i++) { 1904 if (src_table->required_opp_tables[i]->np == dst_table->np) 1905 break; 1906 } 1907 1908 if (unlikely(i == src_table->required_opp_count)) { 1909 pr_err("%s: Couldn't find matching OPP table (%p: %p)\n", 1910 __func__, src_table, dst_table); 1911 return -EINVAL; 1912 } 1913 1914 mutex_lock(&src_table->lock); 1915 1916 list_for_each_entry(opp, &src_table->opp_list, node) { 1917 if (opp->pstate == pstate) { 1918 dest_pstate = opp->required_opps[i]->pstate; 1919 goto unlock; 1920 } 1921 } 1922 1923 pr_err("%s: Couldn't find matching OPP (%p: %p)\n", __func__, src_table, 1924 dst_table); 1925 1926 unlock: 1927 mutex_unlock(&src_table->lock); 1928 1929 return dest_pstate; 1930 } 1931 1932 /** 1933 * dev_pm_opp_add() - Add an OPP table from a table definitions 1934 * @dev: device for which we do this operation 1935 * @freq: Frequency in Hz for this OPP 1936 * @u_volt: Voltage in uVolts for this OPP 1937 * 1938 * This function adds an opp definition to the opp table and returns status. 1939 * The opp is made available by default and it can be controlled using 1940 * dev_pm_opp_enable/disable functions. 1941 * 1942 * Return: 1943 * 0 On success OR 1944 * Duplicate OPPs (both freq and volt are same) and opp->available 1945 * -EEXIST Freq are same and volt are different OR 1946 * Duplicate OPPs (both freq and volt are same) and !opp->available 1947 * -ENOMEM Memory allocation failure 1948 */ 1949 int dev_pm_opp_add(struct device *dev, unsigned long freq, unsigned long u_volt) 1950 { 1951 struct opp_table *opp_table; 1952 int ret; 1953 1954 opp_table = dev_pm_opp_get_opp_table(dev); 1955 if (!opp_table) 1956 return -ENOMEM; 1957 1958 /* Fix regulator count for dynamic OPPs */ 1959 opp_table->regulator_count = 1; 1960 1961 ret = _opp_add_v1(opp_table, dev, freq, u_volt, true); 1962 if (ret) 1963 dev_pm_opp_put_opp_table(opp_table); 1964 1965 return ret; 1966 } 1967 EXPORT_SYMBOL_GPL(dev_pm_opp_add); 1968 1969 /** 1970 * _opp_set_availability() - helper to set the availability of an opp 1971 * @dev: device for which we do this operation 1972 * @freq: OPP frequency to modify availability 1973 * @availability_req: availability status requested for this opp 1974 * 1975 * Set the availability of an OPP, opp_{enable,disable} share a common logic 1976 * which is isolated here. 1977 * 1978 * Return: -EINVAL for bad pointers, -ENOMEM if no memory available for the 1979 * copy operation, returns 0 if no modification was done OR modification was 1980 * successful. 1981 */ 1982 static int _opp_set_availability(struct device *dev, unsigned long freq, 1983 bool availability_req) 1984 { 1985 struct opp_table *opp_table; 1986 struct dev_pm_opp *tmp_opp, *opp = ERR_PTR(-ENODEV); 1987 int r = 0; 1988 1989 /* Find the opp_table */ 1990 opp_table = _find_opp_table(dev); 1991 if (IS_ERR(opp_table)) { 1992 r = PTR_ERR(opp_table); 1993 dev_warn(dev, "%s: Device OPP not found (%d)\n", __func__, r); 1994 return r; 1995 } 1996 1997 mutex_lock(&opp_table->lock); 1998 1999 /* Do we have the frequency? */ 2000 list_for_each_entry(tmp_opp, &opp_table->opp_list, node) { 2001 if (tmp_opp->rate == freq) { 2002 opp = tmp_opp; 2003 break; 2004 } 2005 } 2006 2007 if (IS_ERR(opp)) { 2008 r = PTR_ERR(opp); 2009 goto unlock; 2010 } 2011 2012 /* Is update really needed? */ 2013 if (opp->available == availability_req) 2014 goto unlock; 2015 2016 opp->available = availability_req; 2017 2018 dev_pm_opp_get(opp); 2019 mutex_unlock(&opp_table->lock); 2020 2021 /* Notify the change of the OPP availability */ 2022 if (availability_req) 2023 blocking_notifier_call_chain(&opp_table->head, OPP_EVENT_ENABLE, 2024 opp); 2025 else 2026 blocking_notifier_call_chain(&opp_table->head, 2027 OPP_EVENT_DISABLE, opp); 2028 2029 dev_pm_opp_put(opp); 2030 goto put_table; 2031 2032 unlock: 2033 mutex_unlock(&opp_table->lock); 2034 put_table: 2035 dev_pm_opp_put_opp_table(opp_table); 2036 return r; 2037 } 2038 2039 /** 2040 * dev_pm_opp_enable() - Enable a specific OPP 2041 * @dev: device for which we do this operation 2042 * @freq: OPP frequency to enable 2043 * 2044 * Enables a provided opp. If the operation is valid, this returns 0, else the 2045 * corresponding error value. It is meant to be used for users an OPP available 2046 * after being temporarily made unavailable with dev_pm_opp_disable. 2047 * 2048 * Return: -EINVAL for bad pointers, -ENOMEM if no memory available for the 2049 * copy operation, returns 0 if no modification was done OR modification was 2050 * successful. 2051 */ 2052 int dev_pm_opp_enable(struct device *dev, unsigned long freq) 2053 { 2054 return _opp_set_availability(dev, freq, true); 2055 } 2056 EXPORT_SYMBOL_GPL(dev_pm_opp_enable); 2057 2058 /** 2059 * dev_pm_opp_disable() - Disable a specific OPP 2060 * @dev: device for which we do this operation 2061 * @freq: OPP frequency to disable 2062 * 2063 * Disables a provided opp. If the operation is valid, this returns 2064 * 0, else the corresponding error value. It is meant to be a temporary 2065 * control by users to make this OPP not available until the circumstances are 2066 * right to make it available again (with a call to dev_pm_opp_enable). 2067 * 2068 * Return: -EINVAL for bad pointers, -ENOMEM if no memory available for the 2069 * copy operation, returns 0 if no modification was done OR modification was 2070 * successful. 2071 */ 2072 int dev_pm_opp_disable(struct device *dev, unsigned long freq) 2073 { 2074 return _opp_set_availability(dev, freq, false); 2075 } 2076 EXPORT_SYMBOL_GPL(dev_pm_opp_disable); 2077 2078 /** 2079 * dev_pm_opp_register_notifier() - Register OPP notifier for the device 2080 * @dev: Device for which notifier needs to be registered 2081 * @nb: Notifier block to be registered 2082 * 2083 * Return: 0 on success or a negative error value. 2084 */ 2085 int dev_pm_opp_register_notifier(struct device *dev, struct notifier_block *nb) 2086 { 2087 struct opp_table *opp_table; 2088 int ret; 2089 2090 opp_table = _find_opp_table(dev); 2091 if (IS_ERR(opp_table)) 2092 return PTR_ERR(opp_table); 2093 2094 ret = blocking_notifier_chain_register(&opp_table->head, nb); 2095 2096 dev_pm_opp_put_opp_table(opp_table); 2097 2098 return ret; 2099 } 2100 EXPORT_SYMBOL(dev_pm_opp_register_notifier); 2101 2102 /** 2103 * dev_pm_opp_unregister_notifier() - Unregister OPP notifier for the device 2104 * @dev: Device for which notifier needs to be unregistered 2105 * @nb: Notifier block to be unregistered 2106 * 2107 * Return: 0 on success or a negative error value. 2108 */ 2109 int dev_pm_opp_unregister_notifier(struct device *dev, 2110 struct notifier_block *nb) 2111 { 2112 struct opp_table *opp_table; 2113 int ret; 2114 2115 opp_table = _find_opp_table(dev); 2116 if (IS_ERR(opp_table)) 2117 return PTR_ERR(opp_table); 2118 2119 ret = blocking_notifier_chain_unregister(&opp_table->head, nb); 2120 2121 dev_pm_opp_put_opp_table(opp_table); 2122 2123 return ret; 2124 } 2125 EXPORT_SYMBOL(dev_pm_opp_unregister_notifier); 2126 2127 void _dev_pm_opp_find_and_remove_table(struct device *dev) 2128 { 2129 struct opp_table *opp_table; 2130 2131 /* Check for existing table for 'dev' */ 2132 opp_table = _find_opp_table(dev); 2133 if (IS_ERR(opp_table)) { 2134 int error = PTR_ERR(opp_table); 2135 2136 if (error != -ENODEV) 2137 WARN(1, "%s: opp_table: %d\n", 2138 IS_ERR_OR_NULL(dev) ? 2139 "Invalid device" : dev_name(dev), 2140 error); 2141 return; 2142 } 2143 2144 _put_opp_list_kref(opp_table); 2145 2146 /* Drop reference taken by _find_opp_table() */ 2147 dev_pm_opp_put_opp_table(opp_table); 2148 2149 /* Drop reference taken while the OPP table was added */ 2150 dev_pm_opp_put_opp_table(opp_table); 2151 } 2152 2153 /** 2154 * dev_pm_opp_remove_table() - Free all OPPs associated with the device 2155 * @dev: device pointer used to lookup OPP table. 2156 * 2157 * Free both OPPs created using static entries present in DT and the 2158 * dynamically added entries. 2159 */ 2160 void dev_pm_opp_remove_table(struct device *dev) 2161 { 2162 _dev_pm_opp_find_and_remove_table(dev); 2163 } 2164 EXPORT_SYMBOL_GPL(dev_pm_opp_remove_table); 2165