File Index
Files and directories
Sign in
FILE

of.c

File preview with syntax highlighting.

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