1320 lines · 32.3 KB · C
RawDownload
// SPDX-License-Identifier: GPL-2.0-only/* * Generic OPP OF helpers * * Copyright (C) 2009-2010 Texas Instruments Incorporated. *	Nishanth Menon *	Romit Dasgupta *	Kevin Hilman */ #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt #include <linux/cpu.h>#include <linux/errno.h>#include <linux/device.h>#include <linux/of_device.h>#include <linux/pm_domain.h>#include <linux/slab.h>#include <linux/export.h>#include <linux/energy_model.h> #include "opp.h" /* * Returns opp descriptor node for a device node, caller must * do of_node_put(). */static struct device_node *_opp_of_get_opp_desc_node(struct device_node *np,						     int index){	/* "operating-points-v2" can be an array for power domain providers */	return of_parse_phandle(np, "operating-points-v2", index);} /* Returns opp descriptor node for a device, caller must do of_node_put() */struct device_node *dev_pm_opp_of_get_opp_desc_node(struct device *dev){	return _opp_of_get_opp_desc_node(dev->of_node, 0);}EXPORT_SYMBOL_GPL(dev_pm_opp_of_get_opp_desc_node); struct opp_table *_managed_opp(struct device *dev, int index){	struct opp_table *opp_table, *managed_table = NULL;	struct device_node *np; 	np = _opp_of_get_opp_desc_node(dev->of_node, index);	if (!np)		return NULL; 	list_for_each_entry(opp_table, &opp_tables, node) {		if (opp_table->np == np) {			/*			 * Multiple devices can point to the same OPP table and			 * so will have same node-pointer, np.			 *			 * But the OPPs will be considered as shared only if the			 * OPP table contains a "opp-shared" property.			 */			if (opp_table->shared_opp == OPP_TABLE_ACCESS_SHARED) {				_get_opp_table_kref(opp_table);				managed_table = opp_table;			} 			break;		}	} 	of_node_put(np); 	return managed_table;} /* The caller must call dev_pm_opp_put() after the OPP is used */static struct dev_pm_opp *_find_opp_of_np(struct opp_table *opp_table,					  struct device_node *opp_np){	struct dev_pm_opp *opp; 	mutex_lock(&opp_table->lock); 	list_for_each_entry(opp, &opp_table->opp_list, node) {		if (opp->np == opp_np) {			dev_pm_opp_get(opp);			mutex_unlock(&opp_table->lock);			return opp;		}	} 	mutex_unlock(&opp_table->lock); 	return NULL;} static struct device_node *of_parse_required_opp(struct device_node *np,						 int index){	struct device_node *required_np; 	required_np = of_parse_phandle(np, "required-opps", index);	if (unlikely(!required_np)) {		pr_err("%s: Unable to parse required-opps: %pOF, index: %d\n",		       __func__, np, index);	} 	return required_np;} /* The caller must call dev_pm_opp_put_opp_table() after the table is used */static struct opp_table *_find_table_of_opp_np(struct device_node *opp_np){	struct opp_table *opp_table;	struct device_node *opp_table_np; 	lockdep_assert_held(&opp_table_lock); 	opp_table_np = of_get_parent(opp_np);	if (!opp_table_np)		goto err; 	/* It is safe to put the node now as all we need now is its address */	of_node_put(opp_table_np); 	list_for_each_entry(opp_table, &opp_tables, node) {		if (opp_table_np == opp_table->np) {			_get_opp_table_kref(opp_table);			return opp_table;		}	} err:	return ERR_PTR(-ENODEV);} /* Free resources previously acquired by _opp_table_alloc_required_tables() */static void _opp_table_free_required_tables(struct opp_table *opp_table){	struct opp_table **required_opp_tables = opp_table->required_opp_tables;	int i; 	if (!required_opp_tables)		return; 	for (i = 0; i < opp_table->required_opp_count; i++) {		if (IS_ERR_OR_NULL(required_opp_tables[i]))			break; 		dev_pm_opp_put_opp_table(required_opp_tables[i]);	} 	kfree(required_opp_tables); 	opp_table->required_opp_count = 0;	opp_table->required_opp_tables = NULL;} /* * Populate all devices and opp tables which are part of "required-opps" list. * Checking only the first OPP node should be enough. */static void _opp_table_alloc_required_tables(struct opp_table *opp_table,					     struct device *dev,					     struct device_node *opp_np){	struct opp_table **required_opp_tables;	struct device_node *required_np, *np;	int count, i; 	/* Traversing the first OPP node is all we need */	np = of_get_next_available_child(opp_np, NULL);	if (!np) {		dev_err(dev, "Empty OPP table\n");		return;	} 	count = of_count_phandle_with_args(np, "required-opps", NULL);	if (!count)		goto put_np; 	required_opp_tables = kcalloc(count, sizeof(*required_opp_tables),				      GFP_KERNEL);	if (!required_opp_tables)		goto put_np; 	opp_table->required_opp_tables = required_opp_tables;	opp_table->required_opp_count = count; 	for (i = 0; i < count; i++) {		required_np = of_parse_required_opp(np, i);		if (!required_np)			goto free_required_tables; 		required_opp_tables[i] = _find_table_of_opp_np(required_np);		of_node_put(required_np); 		if (IS_ERR(required_opp_tables[i]))			goto free_required_tables; 		/*		 * We only support genpd's OPPs in the "required-opps" for now,		 * as we don't know how much about other cases. Error out if the		 * required OPP doesn't belong to a genpd.		 */		if (!required_opp_tables[i]->is_genpd) {			dev_err(dev, "required-opp doesn't belong to genpd: %pOF\n",				required_np);			goto free_required_tables;		}	} 	goto put_np; free_required_tables:	_opp_table_free_required_tables(opp_table);put_np:	of_node_put(np);} void _of_init_opp_table(struct opp_table *opp_table, struct device *dev,			int index){	struct device_node *np, *opp_np;	u32 val; 	/*	 * Only required for backward compatibility with v1 bindings, but isn't	 * harmful for other cases. And so we do it unconditionally.	 */	np = of_node_get(dev->of_node);	if (!np)		return; 	if (!of_property_read_u32(np, "clock-latency", &val))		opp_table->clock_latency_ns_max = val;	of_property_read_u32(np, "voltage-tolerance",			     &opp_table->voltage_tolerance_v1); 	if (of_find_property(np, "#power-domain-cells", NULL))		opp_table->is_genpd = true; 	/* Get OPP table node */	opp_np = _opp_of_get_opp_desc_node(np, index);	of_node_put(np); 	if (!opp_np)		return; 	if (of_property_read_bool(opp_np, "opp-shared"))		opp_table->shared_opp = OPP_TABLE_ACCESS_SHARED;	else		opp_table->shared_opp = OPP_TABLE_ACCESS_EXCLUSIVE; 	opp_table->np = opp_np; 	_opp_table_alloc_required_tables(opp_table, dev, opp_np);	of_node_put(opp_np);} void _of_clear_opp_table(struct opp_table *opp_table){	_opp_table_free_required_tables(opp_table);} /* * Release all resources previously acquired with a call to * _of_opp_alloc_required_opps(). */void _of_opp_free_required_opps(struct opp_table *opp_table,				struct dev_pm_opp *opp){	struct dev_pm_opp **required_opps = opp->required_opps;	int i; 	if (!required_opps)		return; 	for (i = 0; i < opp_table->required_opp_count; i++) {		if (!required_opps[i])			break; 		/* Put the reference back */		dev_pm_opp_put(required_opps[i]);	} 	kfree(required_opps);	opp->required_opps = NULL;} /* Populate all required OPPs which are part of "required-opps" list */static int _of_opp_alloc_required_opps(struct opp_table *opp_table,				       struct dev_pm_opp *opp){	struct dev_pm_opp **required_opps;	struct opp_table *required_table;	struct device_node *np;	int i, ret, count = opp_table->required_opp_count; 	if (!count)		return 0; 	required_opps = kcalloc(count, sizeof(*required_opps), GFP_KERNEL);	if (!required_opps)		return -ENOMEM; 	opp->required_opps = required_opps; 	for (i = 0; i < count; i++) {		required_table = opp_table->required_opp_tables[i]; 		np = of_parse_required_opp(opp->np, i);		if (unlikely(!np)) {			ret = -ENODEV;			goto free_required_opps;		} 		required_opps[i] = _find_opp_of_np(required_table, np);		of_node_put(np); 		if (!required_opps[i]) {			pr_err("%s: Unable to find required OPP node: %pOF (%d)\n",			       __func__, opp->np, i);			ret = -ENODEV;			goto free_required_opps;		}	} 	return 0; free_required_opps:	_of_opp_free_required_opps(opp_table, opp); 	return ret;} static int _bandwidth_supported(struct device *dev, struct opp_table *opp_table){	struct device_node *np, *opp_np;	struct property *prop; 	if (!opp_table) {		np = of_node_get(dev->of_node);		if (!np)			return -ENODEV; 		opp_np = _opp_of_get_opp_desc_node(np, 0);		of_node_put(np);	} else {		opp_np = of_node_get(opp_table->np);	} 	/* Lets not fail in case we are parsing opp-v1 bindings */	if (!opp_np)		return 0; 	/* Checking only first OPP is sufficient */	np = of_get_next_available_child(opp_np, NULL);	if (!np) {		dev_err(dev, "OPP table empty\n");		return -EINVAL;	}	of_node_put(opp_np); 	prop = of_find_property(np, "opp-peak-kBps", NULL);	of_node_put(np); 	if (!prop || !prop->length)		return 0; 	return 1;} int dev_pm_opp_of_find_icc_paths(struct device *dev,				 struct opp_table *opp_table){	struct device_node *np;	int ret, i, count, num_paths;	struct icc_path **paths; 	ret = _bandwidth_supported(dev, opp_table);	if (ret <= 0)		return ret; 	ret = 0; 	np = of_node_get(dev->of_node);	if (!np)		return 0; 	count = of_count_phandle_with_args(np, "interconnects",					   "#interconnect-cells");	of_node_put(np);	if (count < 0)		return 0; 	/* two phandles when #interconnect-cells = <1> */	if (count % 2) {		dev_err(dev, "%s: Invalid interconnects values\n", __func__);		return -EINVAL;	} 	num_paths = count / 2;	paths = kcalloc(num_paths, sizeof(*paths), GFP_KERNEL);	if (!paths)		return -ENOMEM; 	for (i = 0; i < num_paths; i++) {		paths[i] = of_icc_get_by_index(dev, i);		if (IS_ERR(paths[i])) {			ret = PTR_ERR(paths[i]);			if (ret != -EPROBE_DEFER) {				dev_err(dev, "%s: Unable to get path%d: %d\n",					__func__, i, ret);			}			goto err;		}	} 	if (opp_table) {		opp_table->paths = paths;		opp_table->path_count = num_paths;		return 0;	} err:	while (i--)		icc_put(paths[i]); 	kfree(paths); 	return ret;}EXPORT_SYMBOL_GPL(dev_pm_opp_of_find_icc_paths); static bool _opp_is_supported(struct device *dev, struct opp_table *opp_table,			      struct device_node *np){	unsigned int count = opp_table->supported_hw_count;	u32 version;	int ret; 	if (!opp_table->supported_hw) {		/*		 * In the case that no supported_hw has been set by the		 * platform but there is an opp-supported-hw value set for		 * an OPP then the OPP should not be enabled as there is		 * no way to see if the hardware supports it.		 */		if (of_find_property(np, "opp-supported-hw", NULL))			return false;		else			return true;	} 	while (count--) {		ret = of_property_read_u32_index(np, "opp-supported-hw", count,						 &version);		if (ret) {			dev_warn(dev, "%s: failed to read opp-supported-hw property at index %d: %d\n",				 __func__, count, ret);			return false;		} 		/* Both of these are bitwise masks of the versions */		if (!(version & opp_table->supported_hw[count]))			return false;	} 	return true;} static int opp_parse_supplies(struct dev_pm_opp *opp, struct device *dev,			      struct opp_table *opp_table){	u32 *microvolt, *microamp = NULL;	int supplies = opp_table->regulator_count, vcount, icount, ret, i, j;	struct property *prop = NULL;	char name[NAME_MAX]; 	/* Search for "opp-microvolt-<name>" */	if (opp_table->prop_name) {		snprintf(name, sizeof(name), "opp-microvolt-%s",			 opp_table->prop_name);		prop = of_find_property(opp->np, name, NULL);	} 	if (!prop) {		/* Search for "opp-microvolt" */		sprintf(name, "opp-microvolt");		prop = of_find_property(opp->np, name, NULL); 		/* Missing property isn't a problem, but an invalid entry is */		if (!prop) {			if (unlikely(supplies == -1)) {				/* Initialize regulator_count */				opp_table->regulator_count = 0;				return 0;			} 			if (!supplies)				return 0;

Lines 1–500 of 1320.

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t