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