1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Generic OPP OF helpers 4 * 5 * Copyright (C) 2009-2010 Texas Instruments Incorporated. 6 * Nishanth Menon 7 * Romit Dasgupta 8 * Kevin Hilman 9 */ 10 11 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 12 13 #include <linux/cpu.h> 14 #include <linux/errno.h> 15 #include <linux/device.h> 16 #include <linux/of.h> 17 #include <linux/pm_domain.h> 18 #include <linux/slab.h> 19 #include <linux/export.h> 20 #include <linux/energy_model.h> 21 22 #include "opp.h" 23 24 /* OPP tables with uninitialized required OPPs, protected by opp_table_lock */ 25 static LIST_HEAD(lazy_opp_tables); 26 27 /* 28 * Returns opp descriptor node for a device node, caller must 29 * do of_node_put(). 30 */ 31 static struct device_node *_opp_of_get_opp_desc_node(struct device_node *np, 32 int index) 33 { 34 /* "operating-points-v2" can be an array for power domain providers */ 35 return of_parse_phandle(np, "operating-points-v2", index); 36 } 37 38 /* Returns opp descriptor node for a device, caller must do of_node_put() */ 39 struct device_node *dev_pm_opp_of_get_opp_desc_node(struct device *dev) 40 { 41 return _opp_of_get_opp_desc_node(dev->of_node, 0); 42 } 43 EXPORT_SYMBOL_GPL(dev_pm_opp_of_get_opp_desc_node); 44 45 struct opp_table *_managed_opp(struct device *dev, int index) 46 { 47 struct opp_table *opp_table, *managed_table = NULL; 48 struct device_node *np __free(device_node); 49 50 np = _opp_of_get_opp_desc_node(dev->of_node, index); 51 if (!np) 52 return NULL; 53 54 list_for_each_entry(opp_table, &opp_tables, node) { 55 if (opp_table->np == np) { 56 /* 57 * Multiple devices can point to the same OPP table and 58 * so will have same node-pointer, np. 59 * 60 * But the OPPs will be considered as shared only if the 61 * OPP table contains a "opp-shared" property. 62 */ 63 if (opp_table->shared_opp == OPP_TABLE_ACCESS_SHARED) 64 managed_table = dev_pm_opp_get_opp_table_ref(opp_table); 65 66 break; 67 } 68 } 69 70 return managed_table; 71 } 72 73 /* The caller must call dev_pm_opp_put() after the OPP is used */ 74 static struct dev_pm_opp *_find_opp_of_np(struct opp_table *opp_table, 75 struct device_node *opp_np) 76 { 77 struct dev_pm_opp *opp; 78 79 guard(mutex)(&opp_table->lock); 80 81 list_for_each_entry(opp, &opp_table->opp_list, node) { 82 if (opp->np == opp_np) 83 return dev_pm_opp_get(opp); 84 } 85 86 return NULL; 87 } 88 89 static struct device_node *of_parse_required_opp(struct device_node *np, 90 int index) 91 { 92 return of_parse_phandle(np, "required-opps", index); 93 } 94 95 /* The caller must call dev_pm_opp_put_opp_table() after the table is used */ 96 static struct opp_table *_find_table_of_opp_np(struct device_node *opp_np) 97 { 98 struct device_node *opp_table_np __free(device_node); 99 struct opp_table *opp_table; 100 101 opp_table_np = of_get_parent(opp_np); 102 if (!opp_table_np) 103 return ERR_PTR(-ENODEV); 104 105 guard(mutex)(&opp_table_lock); 106 107 list_for_each_entry(opp_table, &opp_tables, node) { 108 if (opp_table_np == opp_table->np) 109 return dev_pm_opp_get_opp_table_ref(opp_table); 110 } 111 112 return ERR_PTR(-ENODEV); 113 } 114 115 /* Free resources previously acquired by _opp_table_alloc_required_tables() */ 116 static void _opp_table_free_required_tables(struct opp_table *opp_table) 117 { 118 struct opp_table **required_opp_tables = opp_table->required_opp_tables; 119 int i; 120 121 if (!required_opp_tables) 122 return; 123 124 for (i = 0; i < opp_table->required_opp_count; i++) { 125 if (IS_ERR_OR_NULL(required_opp_tables[i])) 126 continue; 127 128 dev_pm_opp_put_opp_table(required_opp_tables[i]); 129 } 130 131 kfree(required_opp_tables); 132 133 opp_table->required_opp_count = 0; 134 opp_table->required_opp_tables = NULL; 135 136 guard(mutex)(&opp_table_lock); 137 list_del(&opp_table->lazy); 138 } 139 140 /* 141 * Populate all devices and opp tables which are part of "required-opps" list. 142 * Checking only the first OPP node should be enough. 143 */ 144 static void _opp_table_alloc_required_tables(struct opp_table *opp_table, 145 struct device *dev, 146 struct device_node *opp_np) 147 { 148 struct opp_table **required_opp_tables; 149 struct device_node *np __free(device_node); 150 bool lazy = false; 151 int count, i, size; 152 153 /* Traversing the first OPP node is all we need */ 154 np = of_get_next_available_child(opp_np, NULL); 155 if (!np) { 156 dev_warn(dev, "Empty OPP table\n"); 157 return; 158 } 159 160 count = of_count_phandle_with_args(np, "required-opps", NULL); 161 if (count <= 0) 162 return; 163 164 size = sizeof(*required_opp_tables) + sizeof(*opp_table->required_devs); 165 required_opp_tables = kcalloc(count, size, GFP_KERNEL); 166 if (!required_opp_tables) 167 return; 168 169 opp_table->required_opp_tables = required_opp_tables; 170 opp_table->required_devs = (void *)(required_opp_tables + count); 171 opp_table->required_opp_count = count; 172 173 for (i = 0; i < count; i++) { 174 struct device_node *required_np __free(device_node); 175 176 required_np = of_parse_required_opp(np, i); 177 if (!required_np) { 178 _opp_table_free_required_tables(opp_table); 179 return; 180 } 181 182 required_opp_tables[i] = _find_table_of_opp_np(required_np); 183 184 if (IS_ERR(required_opp_tables[i])) 185 lazy = true; 186 } 187 188 /* Let's do the linking later on */ 189 if (lazy) { 190 /* 191 * The OPP table is not held while allocating the table, take it 192 * now to avoid corruption to the lazy_opp_tables list. 193 */ 194 guard(mutex)(&opp_table_lock); 195 list_add(&opp_table->lazy, &lazy_opp_tables); 196 } 197 } 198 199 void _of_init_opp_table(struct opp_table *opp_table, struct device *dev, 200 int index) 201 { 202 struct device_node *np __free(device_node), *opp_np; 203 u32 val; 204 205 /* 206 * Only required for backward compatibility with v1 bindings, but isn't 207 * harmful for other cases. And so we do it unconditionally. 208 */ 209 np = of_node_get(dev->of_node); 210 if (!np) 211 return; 212 213 if (!of_property_read_u32(np, "clock-latency", &val)) 214 opp_table->clock_latency_ns_max = val; 215 of_property_read_u32(np, "voltage-tolerance", 216 &opp_table->voltage_tolerance_v1); 217 218 if (of_property_present(np, "#power-domain-cells")) 219 opp_table->is_genpd = true; 220 221 /* Get OPP table node */ 222 opp_np = _opp_of_get_opp_desc_node(np, index); 223 if (!opp_np) 224 return; 225 226 if (of_property_read_bool(opp_np, "opp-shared")) 227 opp_table->shared_opp = OPP_TABLE_ACCESS_SHARED; 228 else 229 opp_table->shared_opp = OPP_TABLE_ACCESS_EXCLUSIVE; 230 231 opp_table->np = opp_np; 232 233 _opp_table_alloc_required_tables(opp_table, dev, opp_np); 234 } 235 236 void _of_clear_opp_table(struct opp_table *opp_table) 237 { 238 _opp_table_free_required_tables(opp_table); 239 of_node_put(opp_table->np); 240 } 241 242 /* 243 * Release all resources previously acquired with a call to 244 * _of_opp_alloc_required_opps(). 245 */ 246 static void _of_opp_free_required_opps(struct opp_table *opp_table, 247 struct dev_pm_opp *opp) 248 { 249 struct dev_pm_opp **required_opps = opp->required_opps; 250 int i; 251 252 if (!required_opps) 253 return; 254 255 for (i = 0; i < opp_table->required_opp_count; i++) { 256 if (!required_opps[i]) 257 continue; 258 259 /* Put the reference back */ 260 dev_pm_opp_put(required_opps[i]); 261 } 262 263 opp->required_opps = NULL; 264 kfree(required_opps); 265 } 266 267 void _of_clear_opp(struct opp_table *opp_table, struct dev_pm_opp *opp) 268 { 269 _of_opp_free_required_opps(opp_table, opp); 270 of_node_put(opp->np); 271 } 272 273 static int _link_required_opps(struct dev_pm_opp *opp, 274 struct opp_table *required_table, int index) 275 { 276 struct device_node *np __free(device_node); 277 278 np = of_parse_required_opp(opp->np, index); 279 if (unlikely(!np)) 280 return -ENODEV; 281 282 opp->required_opps[index] = _find_opp_of_np(required_table, np); 283 if (!opp->required_opps[index]) { 284 pr_err("%s: Unable to find required OPP node: %pOF (%d)\n", 285 __func__, opp->np, index); 286 return -ENODEV; 287 } 288 289 return 0; 290 } 291 292 /* Populate all required OPPs which are part of "required-opps" list */ 293 static int _of_opp_alloc_required_opps(struct opp_table *opp_table, 294 struct dev_pm_opp *opp) 295 { 296 struct opp_table *required_table; 297 int i, ret, count = opp_table->required_opp_count; 298 299 if (!count) 300 return 0; 301 302 opp->required_opps = kcalloc(count, sizeof(*opp->required_opps), GFP_KERNEL); 303 if (!opp->required_opps) 304 return -ENOMEM; 305 306 for (i = 0; i < count; i++) { 307 required_table = opp_table->required_opp_tables[i]; 308 309 /* Required table not added yet, we will link later */ 310 if (IS_ERR_OR_NULL(required_table)) 311 continue; 312 313 ret = _link_required_opps(opp, required_table, i); 314 if (ret) 315 goto free_required_opps; 316 } 317 318 return 0; 319 320 free_required_opps: 321 _of_opp_free_required_opps(opp_table, opp); 322 323 return ret; 324 } 325 326 /* Link required OPPs for an individual OPP */ 327 static int lazy_link_required_opps(struct opp_table *opp_table, 328 struct opp_table *new_table, int index) 329 { 330 struct dev_pm_opp *opp; 331 int ret; 332 333 list_for_each_entry(opp, &opp_table->opp_list, node) { 334 ret = _link_required_opps(opp, new_table, index); 335 if (ret) 336 return ret; 337 } 338 339 return 0; 340 } 341 342 /* Link required OPPs for all OPPs of the newly added OPP table */ 343 static void lazy_link_required_opp_table(struct opp_table *new_table) 344 { 345 struct opp_table *opp_table, *temp, **required_opp_tables; 346 struct dev_pm_opp *opp; 347 int i, ret; 348 349 guard(mutex)(&opp_table_lock); 350 351 list_for_each_entry_safe(opp_table, temp, &lazy_opp_tables, lazy) { 352 struct device_node *opp_np __free(device_node); 353 bool lazy = false; 354 355 /* opp_np can't be invalid here */ 356 opp_np = of_get_next_available_child(opp_table->np, NULL); 357 358 for (i = 0; i < opp_table->required_opp_count; i++) { 359 struct device_node *required_np __free(device_node) = NULL; 360 struct device_node *required_table_np __free(device_node) = NULL; 361 362 required_opp_tables = opp_table->required_opp_tables; 363 364 /* Required opp-table is already parsed */ 365 if (!IS_ERR(required_opp_tables[i])) 366 continue; 367 368 /* required_np can't be invalid here */ 369 required_np = of_parse_required_opp(opp_np, i); 370 required_table_np = of_get_parent(required_np); 371 372 /* 373 * Newly added table isn't the required opp-table for 374 * opp_table. 375 */ 376 if (required_table_np != new_table->np) { 377 lazy = true; 378 continue; 379 } 380 381 required_opp_tables[i] = dev_pm_opp_get_opp_table_ref(new_table); 382 383 /* Link OPPs now */ 384 ret = lazy_link_required_opps(opp_table, new_table, i); 385 if (ret) { 386 /* The OPPs will be marked unusable */ 387 lazy = false; 388 break; 389 } 390 } 391 392 /* All required opp-tables found, remove from lazy list */ 393 if (!lazy) { 394 list_del_init(&opp_table->lazy); 395 396 list_for_each_entry(opp, &opp_table->opp_list, node) 397 _required_opps_available(opp, opp_table->required_opp_count); 398 } 399 } 400 } 401 402 static int _bandwidth_supported(struct device *dev, struct opp_table *opp_table) 403 { 404 struct device_node *opp_np __free(device_node) = NULL; 405 struct device_node *np __free(device_node) = NULL; 406 struct property *prop; 407 408 if (!opp_table) { 409 struct device_node *np __free(device_node); 410 411 np = of_node_get(dev->of_node); 412 if (!np) 413 return -ENODEV; 414 415 opp_np = _opp_of_get_opp_desc_node(np, 0); 416 } else { 417 opp_np = of_node_get(opp_table->np); 418 } 419 420 /* Lets not fail in case we are parsing opp-v1 bindings */ 421 if (!opp_np) 422 return 0; 423 424 /* Checking only first OPP is sufficient */ 425 np = of_get_next_available_child(opp_np, NULL); 426 if (!np) { 427 dev_err(dev, "OPP table empty\n"); 428 return -EINVAL; 429 } 430 431 prop = of_find_property(np, "opp-peak-kBps", NULL); 432 if (!prop || !prop->length) 433 return 0; 434 435 return 1; 436 } 437 438 int dev_pm_opp_of_find_icc_paths(struct device *dev, 439 struct opp_table *opp_table) 440 { 441 struct device_node *np __free(device_node) = of_node_get(dev->of_node); 442 int ret, i, count, num_paths; 443 struct icc_path **paths; 444 445 ret = _bandwidth_supported(dev, opp_table); 446 if (ret == -EINVAL) 447 return 0; /* Empty OPP table is a valid corner-case, let's not fail */ 448 else if (ret <= 0) 449 return ret; 450 451 if (!np) 452 return 0; 453 454 ret = 0; 455 456 count = of_count_phandle_with_args(np, "interconnects", 457 "#interconnect-cells"); 458 if (count < 0) 459 return 0; 460 461 /* two phandles when #interconnect-cells = <1> */ 462 if (count % 2) { 463 dev_err(dev, "%s: Invalid interconnects values\n", __func__); 464 return -EINVAL; 465 } 466 467 num_paths = count / 2; 468 paths = kcalloc(num_paths, sizeof(*paths), GFP_KERNEL); 469 if (!paths) 470 return -ENOMEM; 471 472 for (i = 0; i < num_paths; i++) { 473 paths[i] = of_icc_get_by_index(dev, i); 474 if (IS_ERR(paths[i])) { 475 ret = dev_err_probe(dev, PTR_ERR(paths[i]), "%s: Unable to get path%d\n", __func__, i); 476 goto err; 477 } 478 } 479 480 if (opp_table) { 481 opp_table->paths = paths; 482 opp_table->path_count = num_paths; 483 return 0; 484 } 485 486 err: 487 while (i--) 488 icc_put(paths[i]); 489 490 kfree(paths); 491 492 return ret; 493 } 494 EXPORT_SYMBOL_GPL(dev_pm_opp_of_find_icc_paths); 495 496 static bool _opp_is_supported(struct device *dev, struct opp_table *opp_table, 497 struct device_node *np) 498 { 499 unsigned int levels = opp_table->supported_hw_count; 500 int count, versions, ret, i, j; 501 u32 val; 502 503 if (!opp_table->supported_hw) { 504 /* 505 * In the case that no supported_hw has been set by the 506 * platform but there is an opp-supported-hw value set for 507 * an OPP then the OPP should not be enabled as there is 508 * no way to see if the hardware supports it. 509 */ 510 if (of_property_present(np, "opp-supported-hw")) 511 return false; 512 else 513 return true; 514 } 515 516 count = of_property_count_u32_elems(np, "opp-supported-hw"); 517 if (count <= 0 || count % levels) { 518 dev_err(dev, "%s: Invalid opp-supported-hw property (%d)\n", 519 __func__, count); 520 return false; 521 } 522 523 versions = count / levels; 524 525 /* All levels in at least one of the versions should match */ 526 for (i = 0; i < versions; i++) { 527 bool supported = true; 528 529 for (j = 0; j < levels; j++) { 530 ret = of_property_read_u32_index(np, "opp-supported-hw", 531 i * levels + j, &val); 532 if (ret) { 533 dev_warn(dev, "%s: failed to read opp-supported-hw property at index %d: %d\n", 534 __func__, i * levels + j, ret); 535 return false; 536 } 537 538 /* Check if the level is supported */ 539 if (!(val & opp_table->supported_hw[j])) { 540 supported = false; 541 break; 542 } 543 } 544 545 if (supported) 546 return true; 547 } 548 549 return false; 550 } 551 552 static u32 *_parse_named_prop(struct dev_pm_opp *opp, struct device *dev, 553 struct opp_table *opp_table, 554 const char *prop_type, bool *triplet) 555 { 556 struct property *prop = NULL; 557 char name[NAME_MAX]; 558 int count, ret; 559 u32 *out; 560 561 /* Search for "opp-<prop_type>-<name>" */ 562 if (opp_table->prop_name) { 563 snprintf(name, sizeof(name), "opp-%s-%s", prop_type, 564 opp_table->prop_name); 565 prop = of_find_property(opp->np, name, NULL); 566 } 567 568 if (!prop) { 569 /* Search for "opp-<prop_type>" */ 570 snprintf(name, sizeof(name), "opp-%s", prop_type); 571 prop = of_find_property(opp->np, name, NULL); 572 if (!prop) 573 return NULL; 574 } 575 576 count = of_property_count_u32_elems(opp->np, name); 577 if (count < 0) { 578 dev_err(dev, "%s: Invalid %s property (%d)\n", __func__, name, 579 count); 580 return ERR_PTR(count); 581 } 582 583 /* 584 * Initialize regulator_count, if regulator information isn't provided 585 * by the platform. Now that one of the properties is available, fix the 586 * regulator_count to 1. 587 */ 588 if (unlikely(opp_table->regulator_count == -1)) 589 opp_table->regulator_count = 1; 590 591 if (count != opp_table->regulator_count && 592 (!triplet || count != opp_table->regulator_count * 3)) { 593 dev_err(dev, "%s: Invalid number of elements in %s property (%u) with supplies (%d)\n", 594 __func__, prop_type, count, opp_table->regulator_count); 595 return ERR_PTR(-EINVAL); 596 } 597 598 out = kmalloc_array(count, sizeof(*out), GFP_KERNEL); 599 if (!out) 600 return ERR_PTR(-EINVAL); 601 602 ret = of_property_read_u32_array(opp->np, name, out, count); 603 if (ret) { 604 dev_err(dev, "%s: error parsing %s: %d\n", __func__, name, ret); 605 kfree(out); 606 return ERR_PTR(-EINVAL); 607 } 608 609 if (triplet) 610 *triplet = count != opp_table->regulator_count; 611 612 return out; 613 } 614 615 static u32 *opp_parse_microvolt(struct dev_pm_opp *opp, struct device *dev, 616 struct opp_table *opp_table, bool *triplet) 617 { 618 u32 *microvolt; 619 620 microvolt = _parse_named_prop(opp, dev, opp_table, "microvolt", triplet); 621 if (IS_ERR(microvolt)) 622 return microvolt; 623 624 if (!microvolt) { 625 /* 626 * Missing property isn't a problem, but an invalid 627 * entry is. This property isn't optional if regulator 628 * information is provided. Check only for the first OPP, as 629 * regulator_count may get initialized after that to a valid 630 * value. 631 */ 632 if (list_empty(&opp_table->opp_list) && 633 opp_table->regulator_count > 0) { 634 dev_err(dev, "%s: opp-microvolt missing although OPP managing regulators\n", 635 __func__); 636 return ERR_PTR(-EINVAL); 637 } 638 } 639 640 return microvolt; 641 } 642 643 static int opp_parse_supplies(struct dev_pm_opp *opp, struct device *dev, 644 struct opp_table *opp_table) 645 { 646 u32 *microvolt, *microamp, *microwatt; 647 int ret = 0, i, j; 648 bool triplet; 649 650 microvolt = opp_parse_microvolt(opp, dev, opp_table, &triplet); 651 if (IS_ERR(microvolt)) 652 return PTR_ERR(microvolt); 653 654 microamp = _parse_named_prop(opp, dev, opp_table, "microamp", NULL); 655 if (IS_ERR(microamp)) { 656 ret = PTR_ERR(microamp); 657 goto free_microvolt; 658 } 659 660 microwatt = _parse_named_prop(opp, dev, opp_table, "microwatt", NULL); 661 if (IS_ERR(microwatt)) { 662 ret = PTR_ERR(microwatt); 663 goto free_microamp; 664 } 665 666 /* 667 * Initialize regulator_count if it is uninitialized and no properties 668 * are found. 669 */ 670 if (unlikely(opp_table->regulator_count == -1)) { 671 opp_table->regulator_count = 0; 672 return 0; 673 } 674 675 for (i = 0, j = 0; i < opp_table->regulator_count; i++) { 676 if (microvolt) { 677 opp->supplies[i].u_volt = microvolt[j++]; 678 679 if (triplet) { 680 opp->supplies[i].u_volt_min = microvolt[j++]; 681 opp->supplies[i].u_volt_max = microvolt[j++]; 682 } else { 683 opp->supplies[i].u_volt_min = opp->supplies[i].u_volt; 684 opp->supplies[i].u_volt_max = opp->supplies[i].u_volt; 685 } 686 } 687 688 if (microamp) 689 opp->supplies[i].u_amp = microamp[i]; 690 691 if (microwatt) 692 opp->supplies[i].u_watt = microwatt[i]; 693 } 694 695 kfree(microwatt); 696 free_microamp: 697 kfree(microamp); 698 free_microvolt: 699 kfree(microvolt); 700 701 return ret; 702 } 703 704 /** 705 * dev_pm_opp_of_remove_table() - Free OPP table entries created from static DT 706 * entries 707 * @dev: device pointer used to lookup OPP table. 708 * 709 * Free OPPs created using static entries present in DT. 710 */ 711 void dev_pm_opp_of_remove_table(struct device *dev) 712 { 713 dev_pm_opp_remove_table(dev); 714 } 715 EXPORT_SYMBOL_GPL(dev_pm_opp_of_remove_table); 716 717 static int _read_rate(struct dev_pm_opp *new_opp, struct opp_table *opp_table, 718 struct device_node *np) 719 { 720 struct property *prop; 721 int i, count, ret; 722 u64 *rates; 723 724 prop = of_find_property(np, "opp-hz", NULL); 725 if (!prop) 726 return -ENODEV; 727 728 count = prop->length / sizeof(u64); 729 if (opp_table->clk_count != count) { 730 pr_err("%s: Count mismatch between opp-hz and clk_count (%d %d)\n", 731 __func__, count, opp_table->clk_count); 732 return -EINVAL; 733 } 734 735 rates = kmalloc_array(count, sizeof(*rates), GFP_KERNEL); 736 if (!rates) 737 return -ENOMEM; 738 739 ret = of_property_read_u64_array(np, "opp-hz", rates, count); 740 if (ret) { 741 pr_err("%s: Error parsing opp-hz: %d\n", __func__, ret); 742 } else { 743 /* 744 * Rate is defined as an unsigned long in clk API, and so 745 * casting explicitly to its type. Must be fixed once rate is 64 746 * bit guaranteed in clk API. 747 */ 748 for (i = 0; i < count; i++) { 749 new_opp->rates[i] = (unsigned long)rates[i]; 750 751 /* This will happen for frequencies > 4.29 GHz */ 752 WARN_ON(new_opp->rates[i] != rates[i]); 753 } 754 } 755 756 kfree(rates); 757 758 return ret; 759 } 760 761 static int _read_bw(struct dev_pm_opp *new_opp, struct opp_table *opp_table, 762 struct device_node *np, bool peak) 763 { 764 const char *name = peak ? "opp-peak-kBps" : "opp-avg-kBps"; 765 struct property *prop; 766 int i, count, ret; 767 u32 *bw; 768 769 prop = of_find_property(np, name, NULL); 770 if (!prop) 771 return -ENODEV; 772 773 count = prop->length / sizeof(u32); 774 if (opp_table->path_count != count) { 775 pr_err("%s: Mismatch between %s and paths (%d %d)\n", 776 __func__, name, count, opp_table->path_count); 777 return -EINVAL; 778 } 779 780 bw = kmalloc_array(count, sizeof(*bw), GFP_KERNEL); 781 if (!bw) 782 return -ENOMEM; 783 784 ret = of_property_read_u32_array(np, name, bw, count); 785 if (ret) { 786 pr_err("%s: Error parsing %s: %d\n", __func__, name, ret); 787 goto out; 788 } 789 790 for (i = 0; i < count; i++) { 791 if (peak) 792 new_opp->bandwidth[i].peak = kBps_to_icc(bw[i]); 793 else 794 new_opp->bandwidth[i].avg = kBps_to_icc(bw[i]); 795 } 796 797 out: 798 kfree(bw); 799 return ret; 800 } 801 802 static int _read_opp_key(struct dev_pm_opp *new_opp, 803 struct opp_table *opp_table, struct device_node *np) 804 { 805 bool found = false; 806 int ret; 807 808 ret = _read_rate(new_opp, opp_table, np); 809 if (!ret) 810 found = true; 811 else if (ret != -ENODEV) 812 return ret; 813 814 /* 815 * Bandwidth consists of peak and average (optional) values: 816 * opp-peak-kBps = <path1_value path2_value>; 817 * opp-avg-kBps = <path1_value path2_value>; 818 */ 819 ret = _read_bw(new_opp, opp_table, np, true); 820 if (!ret) { 821 found = true; 822 ret = _read_bw(new_opp, opp_table, np, false); 823 } 824 825 /* The properties were found but we failed to parse them */ 826 if (ret && ret != -ENODEV) 827 return ret; 828 829 if (!of_property_read_u32(np, "opp-level", &new_opp->level)) 830 found = true; 831 832 if (found) 833 return 0; 834 835 return ret; 836 } 837 838 /** 839 * _opp_add_static_v2() - Allocate static OPPs (As per 'v2' DT bindings) 840 * @opp_table: OPP table 841 * @dev: device for which we do this operation 842 * @np: device node 843 * 844 * This function adds an opp definition to the opp table and returns status. The 845 * opp can be controlled using dev_pm_opp_enable/disable functions and may be 846 * removed by dev_pm_opp_remove. 847 * 848 * Return: 849 * Valid OPP pointer: 850 * On success 851 * NULL: 852 * Duplicate OPPs (both freq and volt are same) and opp->available 853 * OR if the OPP is not supported by hardware. 854 * ERR_PTR(-EEXIST): 855 * Freq are same and volt are different OR 856 * Duplicate OPPs (both freq and volt are same) and !opp->available 857 * ERR_PTR(-ENOMEM): 858 * Memory allocation failure 859 * ERR_PTR(-EINVAL): 860 * Failed parsing the OPP node 861 */ 862 static struct dev_pm_opp *_opp_add_static_v2(struct opp_table *opp_table, 863 struct device *dev, struct device_node *np) 864 { 865 struct dev_pm_opp *new_opp; 866 u32 val; 867 int ret; 868 869 new_opp = _opp_allocate(opp_table); 870 if (!new_opp) 871 return ERR_PTR(-ENOMEM); 872 873 ret = _read_opp_key(new_opp, opp_table, np); 874 if (ret < 0) { 875 dev_err(dev, "%s: opp key field not found\n", __func__); 876 goto free_opp; 877 } 878 879 /* Check if the OPP supports hardware's hierarchy of versions or not */ 880 if (!_opp_is_supported(dev, opp_table, np)) { 881 dev_dbg(dev, "OPP not supported by hardware: %s\n", 882 of_node_full_name(np)); 883 goto free_opp; 884 } 885 886 new_opp->turbo = of_property_read_bool(np, "turbo-mode"); 887 888 new_opp->np = of_node_get(np); 889 new_opp->dynamic = false; 890 new_opp->available = true; 891 892 ret = _of_opp_alloc_required_opps(opp_table, new_opp); 893 if (ret) 894 goto put_node; 895 896 if (!of_property_read_u32(np, "clock-latency-ns", &val)) 897 new_opp->clock_latency_ns = val; 898 899 ret = opp_parse_supplies(new_opp, dev, opp_table); 900 if (ret) 901 goto free_required_opps; 902 903 ret = _opp_add(dev, new_opp, opp_table); 904 if (ret) { 905 /* Don't return error for duplicate OPPs */ 906 if (ret == -EBUSY) 907 ret = 0; 908 goto free_required_opps; 909 } 910 911 /* OPP to select on device suspend */ 912 if (of_property_read_bool(np, "opp-suspend")) { 913 if (opp_table->suspend_opp) { 914 /* Pick the OPP with higher rate/bw/level as suspend OPP */ 915 if (_opp_compare_key(opp_table, new_opp, opp_table->suspend_opp) == 1) { 916 opp_table->suspend_opp->suspend = false; 917 new_opp->suspend = true; 918 opp_table->suspend_opp = new_opp; 919 } 920 } else { 921 new_opp->suspend = true; 922 opp_table->suspend_opp = new_opp; 923 } 924 } 925 926 if (new_opp->clock_latency_ns > opp_table->clock_latency_ns_max) 927 opp_table->clock_latency_ns_max = new_opp->clock_latency_ns; 928 929 pr_debug("%s: turbo:%d rate:%lu uv:%lu uvmin:%lu uvmax:%lu latency:%lu level:%u\n", 930 __func__, new_opp->turbo, new_opp->rates[0], 931 new_opp->supplies[0].u_volt, new_opp->supplies[0].u_volt_min, 932 new_opp->supplies[0].u_volt_max, new_opp->clock_latency_ns, 933 new_opp->level); 934 935 /* 936 * Notify the changes in the availability of the operable 937 * frequency/voltage list. 938 */ 939 blocking_notifier_call_chain(&opp_table->head, OPP_EVENT_ADD, new_opp); 940 return new_opp; 941 942 free_required_opps: 943 _of_opp_free_required_opps(opp_table, new_opp); 944 put_node: 945 of_node_put(np); 946 free_opp: 947 _opp_free(new_opp); 948 949 return ret ? ERR_PTR(ret) : NULL; 950 } 951 952 /* Initializes OPP tables based on new bindings */ 953 static int _of_add_opp_table_v2(struct device *dev, struct opp_table *opp_table) 954 { 955 struct device_node *np; 956 int ret, count = 0; 957 struct dev_pm_opp *opp; 958 959 /* OPP table is already initialized for the device */ 960 scoped_guard(mutex, &opp_table->lock) { 961 if (opp_table->parsed_static_opps) { 962 opp_table->parsed_static_opps++; 963 return 0; 964 } 965 966 opp_table->parsed_static_opps = 1; 967 } 968 969 /* We have opp-table node now, iterate over it and add OPPs */ 970 for_each_available_child_of_node(opp_table->np, np) { 971 opp = _opp_add_static_v2(opp_table, dev, np); 972 if (IS_ERR(opp)) { 973 ret = PTR_ERR(opp); 974 dev_err(dev, "%s: Failed to add OPP, %d\n", __func__, 975 ret); 976 of_node_put(np); 977 goto remove_static_opp; 978 } else if (opp) { 979 count++; 980 } 981 } 982 983 /* There should be one or more OPPs defined */ 984 if (!count) { 985 dev_err(dev, "%s: no supported OPPs", __func__); 986 ret = -ENOENT; 987 goto remove_static_opp; 988 } 989 990 lazy_link_required_opp_table(opp_table); 991 992 return 0; 993 994 remove_static_opp: 995 _opp_remove_all_static(opp_table); 996 997 return ret; 998 } 999 1000 /* Initializes OPP tables based on old-deprecated bindings */ 1001 static int _of_add_opp_table_v1(struct device *dev, struct opp_table *opp_table) 1002 { 1003 const struct property *prop; 1004 const __be32 *val; 1005 int nr, ret = 0; 1006 1007 scoped_guard(mutex, &opp_table->lock) { 1008 if (opp_table->parsed_static_opps) { 1009 opp_table->parsed_static_opps++; 1010 return 0; 1011 } 1012 1013 opp_table->parsed_static_opps = 1; 1014 } 1015 1016 prop = of_find_property(dev->of_node, "operating-points", NULL); 1017 if (!prop) { 1018 ret = -ENODEV; 1019 goto remove_static_opp; 1020 } 1021 if (!prop->value) { 1022 ret = -ENODATA; 1023 goto remove_static_opp; 1024 } 1025 1026 /* 1027 * Each OPP is a set of tuples consisting of frequency and 1028 * voltage like <freq-kHz vol-uV>. 1029 */ 1030 nr = prop->length / sizeof(u32); 1031 if (nr % 2) { 1032 dev_err(dev, "%s: Invalid OPP table\n", __func__); 1033 ret = -EINVAL; 1034 goto remove_static_opp; 1035 } 1036 1037 val = prop->value; 1038 while (nr) { 1039 unsigned long freq = be32_to_cpup(val++) * 1000; 1040 unsigned long volt = be32_to_cpup(val++); 1041 struct dev_pm_opp_data data = { 1042 .freq = freq, 1043 .u_volt = volt, 1044 }; 1045 1046 ret = _opp_add_v1(opp_table, dev, &data, false); 1047 if (ret) { 1048 dev_err(dev, "%s: Failed to add OPP %ld (%d)\n", 1049 __func__, data.freq, ret); 1050 goto remove_static_opp; 1051 } 1052 nr -= 2; 1053 } 1054 1055 return 0; 1056 1057 remove_static_opp: 1058 _opp_remove_all_static(opp_table); 1059 1060 return ret; 1061 } 1062 1063 static int _of_add_table_indexed(struct device *dev, int index) 1064 { 1065 struct opp_table *opp_table; 1066 int ret, count; 1067 1068 if (index) { 1069 /* 1070 * If only one phandle is present, then the same OPP table 1071 * applies for all index requests. 1072 */ 1073 count = of_count_phandle_with_args(dev->of_node, 1074 "operating-points-v2", NULL); 1075 if (count == 1) 1076 index = 0; 1077 } 1078 1079 opp_table = _add_opp_table_indexed(dev, index, true); 1080 if (IS_ERR(opp_table)) 1081 return PTR_ERR(opp_table); 1082 1083 /* 1084 * OPPs have two version of bindings now. Also try the old (v1) 1085 * bindings for backward compatibility with older dtbs. 1086 */ 1087 if (opp_table->np) 1088 ret = _of_add_opp_table_v2(dev, opp_table); 1089 else 1090 ret = _of_add_opp_table_v1(dev, opp_table); 1091 1092 if (ret) 1093 dev_pm_opp_put_opp_table(opp_table); 1094 1095 return ret; 1096 } 1097 1098 static void devm_pm_opp_of_table_release(void *data) 1099 { 1100 dev_pm_opp_of_remove_table(data); 1101 } 1102 1103 static int _devm_of_add_table_indexed(struct device *dev, int index) 1104 { 1105 int ret; 1106 1107 ret = _of_add_table_indexed(dev, index); 1108 if (ret) 1109 return ret; 1110 1111 return devm_add_action_or_reset(dev, devm_pm_opp_of_table_release, dev); 1112 } 1113 1114 /** 1115 * devm_pm_opp_of_add_table() - Initialize opp table from device tree 1116 * @dev: device pointer used to lookup OPP table. 1117 * 1118 * Register the initial OPP table with the OPP library for given device. 1119 * 1120 * The opp_table structure will be freed after the device is destroyed. 1121 * 1122 * Return: 1123 * 0 On success OR 1124 * Duplicate OPPs (both freq and volt are same) and opp->available 1125 * -EEXIST Freq are same and volt are different OR 1126 * Duplicate OPPs (both freq and volt are same) and !opp->available 1127 * -ENOMEM Memory allocation failure 1128 * -ENODEV when 'operating-points' property is not found or is invalid data 1129 * in device node. 1130 * -ENODATA when empty 'operating-points' property is found 1131 * -EINVAL when invalid entries are found in opp-v2 table 1132 */ 1133 int devm_pm_opp_of_add_table(struct device *dev) 1134 { 1135 return _devm_of_add_table_indexed(dev, 0); 1136 } 1137 EXPORT_SYMBOL_GPL(devm_pm_opp_of_add_table); 1138 1139 /** 1140 * dev_pm_opp_of_add_table() - Initialize opp table from device tree 1141 * @dev: device pointer used to lookup OPP table. 1142 * 1143 * Register the initial OPP table with the OPP library for given device. 1144 * 1145 * Return: 1146 * 0 On success OR 1147 * Duplicate OPPs (both freq and volt are same) and opp->available 1148 * -EEXIST Freq are same and volt are different OR 1149 * Duplicate OPPs (both freq and volt are same) and !opp->available 1150 * -ENOMEM Memory allocation failure 1151 * -ENODEV when 'operating-points' property is not found or is invalid data 1152 * in device node. 1153 * -ENODATA when empty 'operating-points' property is found 1154 * -EINVAL when invalid entries are found in opp-v2 table 1155 */ 1156 int dev_pm_opp_of_add_table(struct device *dev) 1157 { 1158 return _of_add_table_indexed(dev, 0); 1159 } 1160 EXPORT_SYMBOL_GPL(dev_pm_opp_of_add_table); 1161 1162 /** 1163 * dev_pm_opp_of_add_table_indexed() - Initialize indexed opp table from device tree 1164 * @dev: device pointer used to lookup OPP table. 1165 * @index: Index number. 1166 * 1167 * Register the initial OPP table with the OPP library for given device only 1168 * using the "operating-points-v2" property. 1169 * 1170 * Return: Refer to dev_pm_opp_of_add_table() for return values. 1171 */ 1172 int dev_pm_opp_of_add_table_indexed(struct device *dev, int index) 1173 { 1174 return _of_add_table_indexed(dev, index); 1175 } 1176 EXPORT_SYMBOL_GPL(dev_pm_opp_of_add_table_indexed); 1177 1178 /** 1179 * devm_pm_opp_of_add_table_indexed() - Initialize indexed opp table from device tree 1180 * @dev: device pointer used to lookup OPP table. 1181 * @index: Index number. 1182 * 1183 * This is a resource-managed variant of dev_pm_opp_of_add_table_indexed(). 1184 */ 1185 int devm_pm_opp_of_add_table_indexed(struct device *dev, int index) 1186 { 1187 return _devm_of_add_table_indexed(dev, index); 1188 } 1189 EXPORT_SYMBOL_GPL(devm_pm_opp_of_add_table_indexed); 1190 1191 /* CPU device specific helpers */ 1192 1193 /** 1194 * dev_pm_opp_of_cpumask_remove_table() - Removes OPP table for @cpumask 1195 * @cpumask: cpumask for which OPP table needs to be removed 1196 * 1197 * This removes the OPP tables for CPUs present in the @cpumask. 1198 * This should be used only to remove static entries created from DT. 1199 */ 1200 void dev_pm_opp_of_cpumask_remove_table(const struct cpumask *cpumask) 1201 { 1202 _dev_pm_opp_cpumask_remove_table(cpumask, -1); 1203 } 1204 EXPORT_SYMBOL_GPL(dev_pm_opp_of_cpumask_remove_table); 1205 1206 /** 1207 * dev_pm_opp_of_cpumask_add_table() - Adds OPP table for @cpumask 1208 * @cpumask: cpumask for which OPP table needs to be added. 1209 * 1210 * This adds the OPP tables for CPUs present in the @cpumask. 1211 */ 1212 int dev_pm_opp_of_cpumask_add_table(const struct cpumask *cpumask) 1213 { 1214 struct device *cpu_dev; 1215 int cpu, ret; 1216 1217 if (WARN_ON(cpumask_empty(cpumask))) 1218 return -ENODEV; 1219 1220 for_each_cpu(cpu, cpumask) { 1221 cpu_dev = get_cpu_device(cpu); 1222 if (!cpu_dev) { 1223 pr_err("%s: failed to get cpu%d device\n", __func__, 1224 cpu); 1225 ret = -ENODEV; 1226 goto remove_table; 1227 } 1228 1229 ret = dev_pm_opp_of_add_table(cpu_dev); 1230 if (ret) { 1231 /* 1232 * OPP may get registered dynamically, don't print error 1233 * message here. 1234 */ 1235 pr_debug("%s: couldn't find opp table for cpu:%d, %d\n", 1236 __func__, cpu, ret); 1237 1238 goto remove_table; 1239 } 1240 } 1241 1242 return 0; 1243 1244 remove_table: 1245 /* Free all other OPPs */ 1246 _dev_pm_opp_cpumask_remove_table(cpumask, cpu); 1247 1248 return ret; 1249 } 1250 EXPORT_SYMBOL_GPL(dev_pm_opp_of_cpumask_add_table); 1251 1252 /* 1253 * Works only for OPP v2 bindings. 1254 * 1255 * Returns -ENOENT if operating-points-v2 bindings aren't supported. 1256 */ 1257 /** 1258 * dev_pm_opp_of_get_sharing_cpus() - Get cpumask of CPUs sharing OPPs with 1259 * @cpu_dev using operating-points-v2 1260 * bindings. 1261 * 1262 * @cpu_dev: CPU device for which we do this operation 1263 * @cpumask: cpumask to update with information of sharing CPUs 1264 * 1265 * This updates the @cpumask with CPUs that are sharing OPPs with @cpu_dev. 1266 * 1267 * Returns -ENOENT if operating-points-v2 isn't present for @cpu_dev. 1268 */ 1269 int dev_pm_opp_of_get_sharing_cpus(struct device *cpu_dev, 1270 struct cpumask *cpumask) 1271 { 1272 struct device_node *np __free(device_node); 1273 int cpu; 1274 1275 /* Get OPP descriptor node */ 1276 np = dev_pm_opp_of_get_opp_desc_node(cpu_dev); 1277 if (!np) { 1278 dev_dbg(cpu_dev, "%s: Couldn't find opp node.\n", __func__); 1279 return -ENOENT; 1280 } 1281 1282 cpumask_set_cpu(cpu_dev->id, cpumask); 1283 1284 /* OPPs are shared ? */ 1285 if (!of_property_read_bool(np, "opp-shared")) 1286 return 0; 1287 1288 for_each_possible_cpu(cpu) { 1289 struct device_node *cpu_np __free(device_node) = NULL; 1290 struct device_node *tmp_np __free(device_node) = NULL; 1291 1292 if (cpu == cpu_dev->id) 1293 continue; 1294 1295 cpu_np = of_cpu_device_node_get(cpu); 1296 if (!cpu_np) { 1297 dev_err(cpu_dev, "%s: failed to get cpu%d node\n", 1298 __func__, cpu); 1299 return -ENOENT; 1300 } 1301 1302 /* Get OPP descriptor node */ 1303 tmp_np = _opp_of_get_opp_desc_node(cpu_np, 0); 1304 if (!tmp_np) { 1305 pr_err("%pOF: Couldn't find opp node\n", cpu_np); 1306 return -ENOENT; 1307 } 1308 1309 /* CPUs are sharing opp node */ 1310 if (np == tmp_np) 1311 cpumask_set_cpu(cpu, cpumask); 1312 } 1313 1314 return 0; 1315 } 1316 EXPORT_SYMBOL_GPL(dev_pm_opp_of_get_sharing_cpus); 1317 1318 /** 1319 * of_get_required_opp_performance_state() - Search for required OPP and return its performance state. 1320 * @np: Node that contains the "required-opps" property. 1321 * @index: Index of the phandle to parse. 1322 * 1323 * Returns the performance state of the OPP pointed out by the "required-opps" 1324 * property at @index in @np. 1325 * 1326 * Return: Zero or positive performance state on success, otherwise negative 1327 * value on errors. 1328 */ 1329 int of_get_required_opp_performance_state(struct device_node *np, int index) 1330 { 1331 struct device_node *required_np __free(device_node); 1332 struct opp_table *opp_table __free(put_opp_table) = NULL; 1333 struct dev_pm_opp *opp __free(put_opp) = NULL; 1334 int pstate = -EINVAL; 1335 1336 required_np = of_parse_required_opp(np, index); 1337 if (!required_np) 1338 return -ENODEV; 1339 1340 opp_table = _find_table_of_opp_np(required_np); 1341 if (IS_ERR(opp_table)) { 1342 pr_err("%s: Failed to find required OPP table %pOF: %ld\n", 1343 __func__, np, PTR_ERR(opp_table)); 1344 return PTR_ERR(opp_table); 1345 } 1346 1347 /* The OPP tables must belong to a genpd */ 1348 if (unlikely(!opp_table->is_genpd)) { 1349 pr_err("%s: Performance state is only valid for genpds.\n", __func__); 1350 return -EINVAL; 1351 } 1352 1353 opp = _find_opp_of_np(opp_table, required_np); 1354 if (opp) { 1355 if (opp->level == OPP_LEVEL_UNSET) { 1356 pr_err("%s: OPP levels aren't available for %pOF\n", 1357 __func__, np); 1358 } else { 1359 pstate = opp->level; 1360 } 1361 } 1362 1363 return pstate; 1364 } 1365 EXPORT_SYMBOL_GPL(of_get_required_opp_performance_state); 1366 1367 /** 1368 * dev_pm_opp_of_has_required_opp - Find out if a required-opps exists. 1369 * @dev: The device to investigate. 1370 * 1371 * Returns true if the device's node has a "operating-points-v2" property and if 1372 * the corresponding node for the opp-table describes opp nodes that uses the 1373 * "required-opps" property. 1374 * 1375 * Return: True if a required-opps is present, else false. 1376 */ 1377 bool dev_pm_opp_of_has_required_opp(struct device *dev) 1378 { 1379 struct device_node *np __free(device_node) = NULL, *opp_np __free(device_node); 1380 int count; 1381 1382 opp_np = _opp_of_get_opp_desc_node(dev->of_node, 0); 1383 if (!opp_np) 1384 return false; 1385 1386 np = of_get_next_available_child(opp_np, NULL); 1387 if (!np) { 1388 dev_warn(dev, "Empty OPP table\n"); 1389 return false; 1390 } 1391 1392 count = of_count_phandle_with_args(np, "required-opps", NULL); 1393 1394 return count > 0; 1395 } 1396 1397 /** 1398 * dev_pm_opp_get_of_node() - Gets the DT node corresponding to an opp 1399 * @opp: opp for which DT node has to be returned for 1400 * 1401 * Return: DT node corresponding to the opp, else 0 on success. 1402 * 1403 * The caller needs to put the node with of_node_put() after using it. 1404 */ 1405 struct device_node *dev_pm_opp_get_of_node(struct dev_pm_opp *opp) 1406 { 1407 if (IS_ERR_OR_NULL(opp)) { 1408 pr_err("%s: Invalid parameters\n", __func__); 1409 return NULL; 1410 } 1411 1412 return of_node_get(opp->np); 1413 } 1414 EXPORT_SYMBOL_GPL(dev_pm_opp_get_of_node); 1415 1416 /* 1417 * Callback function provided to the Energy Model framework upon registration. 1418 * It provides the power used by @dev at @kHz if it is the frequency of an 1419 * existing OPP, or at the frequency of the first OPP above @kHz otherwise 1420 * (see dev_pm_opp_find_freq_ceil()). This function updates @kHz to the ceiled 1421 * frequency and @uW to the associated power. 1422 * 1423 * Returns 0 on success or a proper -EINVAL value in case of error. 1424 */ 1425 static int __maybe_unused 1426 _get_dt_power(struct device *dev, unsigned long *uW, unsigned long *kHz) 1427 { 1428 struct dev_pm_opp *opp __free(put_opp); 1429 unsigned long opp_freq, opp_power; 1430 1431 /* Find the right frequency and related OPP */ 1432 opp_freq = *kHz * 1000; 1433 opp = dev_pm_opp_find_freq_ceil(dev, &opp_freq); 1434 if (IS_ERR(opp)) 1435 return -EINVAL; 1436 1437 opp_power = dev_pm_opp_get_power(opp); 1438 if (!opp_power) 1439 return -EINVAL; 1440 1441 *kHz = opp_freq / 1000; 1442 *uW = opp_power; 1443 1444 return 0; 1445 } 1446 1447 /** 1448 * dev_pm_opp_calc_power() - Calculate power value for device with EM 1449 * @dev : Device for which an Energy Model has to be registered 1450 * @uW : New power value that is calculated 1451 * @kHz : Frequency for which the new power is calculated 1452 * 1453 * This computes the power estimated by @dev at @kHz if it is the frequency 1454 * of an existing OPP, or at the frequency of the first OPP above @kHz otherwise 1455 * (see dev_pm_opp_find_freq_ceil()). This function updates @kHz to the ceiled 1456 * frequency and @uW to the associated power. The power is estimated as 1457 * P = C * V^2 * f with C being the device's capacitance and V and f 1458 * respectively the voltage and frequency of the OPP. 1459 * It is also used as a callback function provided to the Energy Model 1460 * framework upon registration. 1461 * 1462 * Returns -EINVAL if the power calculation failed because of missing 1463 * parameters, 0 otherwise. 1464 */ 1465 int dev_pm_opp_calc_power(struct device *dev, unsigned long *uW, 1466 unsigned long *kHz) 1467 { 1468 struct dev_pm_opp *opp __free(put_opp) = NULL; 1469 struct device_node *np __free(device_node); 1470 unsigned long mV, Hz; 1471 u32 cap; 1472 u64 tmp; 1473 int ret; 1474 1475 np = of_node_get(dev->of_node); 1476 if (!np) 1477 return -EINVAL; 1478 1479 ret = of_property_read_u32(np, "dynamic-power-coefficient", &cap); 1480 if (ret) 1481 return -EINVAL; 1482 1483 Hz = *kHz * 1000; 1484 opp = dev_pm_opp_find_freq_ceil(dev, &Hz); 1485 if (IS_ERR(opp)) 1486 return -EINVAL; 1487 1488 mV = dev_pm_opp_get_voltage(opp) / 1000; 1489 if (!mV) 1490 return -EINVAL; 1491 1492 tmp = (u64)cap * mV * mV * (Hz / 1000000); 1493 /* Provide power in micro-Watts */ 1494 do_div(tmp, 1000000); 1495 1496 *uW = (unsigned long)tmp; 1497 *kHz = Hz / 1000; 1498 1499 return 0; 1500 } 1501 EXPORT_SYMBOL_GPL(dev_pm_opp_calc_power); 1502 1503 static bool _of_has_opp_microwatt_property(struct device *dev) 1504 { 1505 struct dev_pm_opp *opp __free(put_opp); 1506 unsigned long freq = 0; 1507 1508 /* Check if at least one OPP has needed property */ 1509 opp = dev_pm_opp_find_freq_ceil(dev, &freq); 1510 if (IS_ERR(opp)) 1511 return false; 1512 1513 return !!dev_pm_opp_get_power(opp); 1514 } 1515 1516 /** 1517 * dev_pm_opp_of_register_em() - Attempt to register an Energy Model 1518 * @dev : Device for which an Energy Model has to be registered 1519 * @cpus : CPUs for which an Energy Model has to be registered. For 1520 * other type of devices it should be set to NULL. 1521 * 1522 * This checks whether the "dynamic-power-coefficient" devicetree property has 1523 * been specified, and tries to register an Energy Model with it if it has. 1524 * Having this property means the voltages are known for OPPs and the EM 1525 * might be calculated. 1526 */ 1527 int dev_pm_opp_of_register_em(struct device *dev, struct cpumask *cpus) 1528 { 1529 struct device_node *np __free(device_node) = NULL; 1530 struct em_data_callback em_cb; 1531 int ret, nr_opp; 1532 u32 cap; 1533 1534 if (IS_ERR_OR_NULL(dev)) { 1535 ret = -EINVAL; 1536 goto failed; 1537 } 1538 1539 nr_opp = dev_pm_opp_get_opp_count(dev); 1540 if (nr_opp <= 0) { 1541 ret = -EINVAL; 1542 goto failed; 1543 } 1544 1545 /* First, try to find more precised Energy Model in DT */ 1546 if (_of_has_opp_microwatt_property(dev)) { 1547 EM_SET_ACTIVE_POWER_CB(em_cb, _get_dt_power); 1548 goto register_em; 1549 } 1550 1551 np = of_node_get(dev->of_node); 1552 if (!np) { 1553 ret = -EINVAL; 1554 goto failed; 1555 } 1556 1557 /* 1558 * Register an EM only if the 'dynamic-power-coefficient' property is 1559 * set in devicetree. It is assumed the voltage values are known if that 1560 * property is set since it is useless otherwise. If voltages are not 1561 * known, just let the EM registration fail with an error to alert the 1562 * user about the inconsistent configuration. 1563 */ 1564 ret = of_property_read_u32(np, "dynamic-power-coefficient", &cap); 1565 if (ret || !cap) { 1566 dev_dbg(dev, "Couldn't find proper 'dynamic-power-coefficient' in DT\n"); 1567 ret = -EINVAL; 1568 goto failed; 1569 } 1570 1571 EM_SET_ACTIVE_POWER_CB(em_cb, dev_pm_opp_calc_power); 1572 1573 register_em: 1574 ret = em_dev_register_perf_domain(dev, nr_opp, &em_cb, cpus, true); 1575 if (ret) 1576 goto failed; 1577 1578 return 0; 1579 1580 failed: 1581 dev_dbg(dev, "Couldn't register Energy Model %d\n", ret); 1582 return ret; 1583 } 1584 EXPORT_SYMBOL_GPL(dev_pm_opp_of_register_em); 1585