1712 lines · 49.2 KB · C
RawDownload
// SPDX-License-Identifier: GPL-2.0+/* * drivers/of/property.c - Procedures for accessing and interpreting *			   Devicetree properties and graphs. * * Initially created by copying procedures from drivers/of/base.c. This * file contains the OF property as well as the OF graph interface * functions. * * Paul Mackerras	August 1996. * Copyright (C) 1996-2005 Paul Mackerras. * *  Adapted for 64bit PowerPC by Dave Engebretsen and Peter Bergner. *    {engebret|bergner}@us.ibm.com * *  Adapted for sparc and sparc64 by David S. Miller davem@davemloft.net * *  Reconsolidated from arch/x/kernel/prom.c by Stephen Rothwell and *  Grant Likely. */ #define pr_fmt(fmt)	"OF: " fmt #include <linux/ctype.h>#include <linux/of.h>#include <linux/of_address.h>#include <linux/of_device.h>#include <linux/of_graph.h>#include <linux/of_irq.h>#include <linux/string.h>#include <linux/moduleparam.h> #include "of_private.h" /** * of_property_read_bool - Find a property * @np:		device node from which the property value is to be read. * @propname:	name of the property to be searched. * * Search for a boolean property in a device node. Usage on non-boolean * property types is deprecated. * * Return: true if the property exists false otherwise. */bool of_property_read_bool(const struct device_node *np, const char *propname){	struct property *prop = of_find_property(np, propname, NULL); 	/*	 * Boolean properties should not have a value. Testing for property	 * presence should either use of_property_present() or just read the	 * property value and check the returned error code.	 */	if (prop && prop->length)		pr_warn("%pOF: Read of boolean property '%s' with a value.\n", np, propname); 	return prop ? true : false;}EXPORT_SYMBOL(of_property_read_bool); /** * of_graph_is_present() - check graph's presence * @node: pointer to device_node containing graph port * * Return: True if @node has a port or ports (with a port) sub-node, * false otherwise. */bool of_graph_is_present(const struct device_node *node){	struct device_node *ports __free(device_node) = of_get_child_by_name(node, "ports"); 	if (ports)		node = ports; 	struct device_node *port __free(device_node) = of_get_child_by_name(node, "port"); 	return !!port;}EXPORT_SYMBOL(of_graph_is_present); /** * of_property_count_elems_of_size - Count the number of elements in a property * * @np:		device node from which the property value is to be read. * @propname:	name of the property to be searched. * @elem_size:	size of the individual element * * Search for a property in a device node and count the number of elements of * size elem_size in it. * * Return: The number of elements on success, -EINVAL if the property does not * exist or its length does not match a multiple of elem_size and -ENODATA if * the property does not have a value. */int of_property_count_elems_of_size(const struct device_node *np,				const char *propname, int elem_size){	const struct property *prop = of_find_property(np, propname, NULL); 	if (!prop)		return -EINVAL;	if (!prop->value)		return -ENODATA; 	if (prop->length % elem_size != 0) {		pr_err("size of %s in node %pOF is not a multiple of %d\n",		       propname, np, elem_size);		return -EINVAL;	} 	return prop->length / elem_size;}EXPORT_SYMBOL_GPL(of_property_count_elems_of_size); /** * of_find_property_value_of_size * * @np:		device node from which the property value is to be read. * @propname:	name of the property to be searched. * @min:	minimum allowed length of property value * @max:	maximum allowed length of property value (0 means unlimited) * @len:	if !=NULL, actual length is written to here * * Search for a property in a device node and valid the requested size. * * Return: The property value on success, -EINVAL if the property does not * exist, -ENODATA if property does not have a value, and -EOVERFLOW if the * property data is too small or too large. * */static void *of_find_property_value_of_size(const struct device_node *np,			const char *propname, u32 min, u32 max, size_t *len){	const struct property *prop = of_find_property(np, propname, NULL); 	if (!prop)		return ERR_PTR(-EINVAL);	if (!prop->value)		return ERR_PTR(-ENODATA);	if (prop->length < min)		return ERR_PTR(-EOVERFLOW);	if (max && prop->length > max)		return ERR_PTR(-EOVERFLOW); 	if (len)		*len = prop->length; 	return prop->value;} /** * of_property_read_u8_index - Find and read a u8 from a multi-value property. * * @np:		device node from which the property value is to be read. * @propname:	name of the property to be searched. * @index:	index of the u8 in the list of values * @out_value:	pointer to return value, modified only if no error. * * Search for a property in a device node and read nth 8-bit value from * it. * * Return: 0 on success, -EINVAL if the property does not exist, * -ENODATA if property does not have a value, and -EOVERFLOW if the * property data isn't large enough. * * The out_value is modified only if a valid u8 value can be decoded. */int of_property_read_u8_index(const struct device_node *np,				       const char *propname,				       u32 index, u8 *out_value){	const u8 *val = of_find_property_value_of_size(np, propname,					((index + 1) * sizeof(*out_value)),					0, NULL); 	if (IS_ERR(val))		return PTR_ERR(val); 	*out_value = val[index];	return 0;}EXPORT_SYMBOL_GPL(of_property_read_u8_index); /** * of_property_read_u16_index - Find and read a u16 from a multi-value property. * * @np:		device node from which the property value is to be read. * @propname:	name of the property to be searched. * @index:	index of the u16 in the list of values * @out_value:	pointer to return value, modified only if no error. * * Search for a property in a device node and read nth 16-bit value from * it. * * Return: 0 on success, -EINVAL if the property does not exist, * -ENODATA if property does not have a value, and -EOVERFLOW if the * property data isn't large enough. * * The out_value is modified only if a valid u16 value can be decoded. */int of_property_read_u16_index(const struct device_node *np,				       const char *propname,				       u32 index, u16 *out_value){	const u16 *val = of_find_property_value_of_size(np, propname,					((index + 1) * sizeof(*out_value)),					0, NULL); 	if (IS_ERR(val))		return PTR_ERR(val); 	*out_value = be16_to_cpup(((__be16 *)val) + index);	return 0;}EXPORT_SYMBOL_GPL(of_property_read_u16_index); /** * of_property_read_u32_index - Find and read a u32 from a multi-value property. * * @np:		device node from which the property value is to be read. * @propname:	name of the property to be searched. * @index:	index of the u32 in the list of values * @out_value:	pointer to return value, modified only if no error. * * Search for a property in a device node and read nth 32-bit value from * it. * * Return: 0 on success, -EINVAL if the property does not exist, * -ENODATA if property does not have a value, and -EOVERFLOW if the * property data isn't large enough. * * The out_value is modified only if a valid u32 value can be decoded. */int of_property_read_u32_index(const struct device_node *np,				       const char *propname,				       u32 index, u32 *out_value){	const u32 *val = of_find_property_value_of_size(np, propname,					((index + 1) * sizeof(*out_value)),					0,					NULL); 	if (IS_ERR(val))		return PTR_ERR(val); 	*out_value = be32_to_cpup(((__be32 *)val) + index);	return 0;}EXPORT_SYMBOL_GPL(of_property_read_u32_index); /** * of_property_read_u64_index - Find and read a u64 from a multi-value property. * * @np:		device node from which the property value is to be read. * @propname:	name of the property to be searched. * @index:	index of the u64 in the list of values * @out_value:	pointer to return value, modified only if no error. * * Search for a property in a device node and read nth 64-bit value from * it. * * Return: 0 on success, -EINVAL if the property does not exist, * -ENODATA if property does not have a value, and -EOVERFLOW if the * property data isn't large enough. * * The out_value is modified only if a valid u64 value can be decoded. */int of_property_read_u64_index(const struct device_node *np,				       const char *propname,				       u32 index, u64 *out_value){	const u64 *val = of_find_property_value_of_size(np, propname,					((index + 1) * sizeof(*out_value)),					0, NULL); 	if (IS_ERR(val))		return PTR_ERR(val); 	*out_value = be64_to_cpup(((__be64 *)val) + index);	return 0;}EXPORT_SYMBOL_GPL(of_property_read_u64_index); /** * of_property_read_variable_u8_array - Find and read an array of u8 from a * property, with bounds on the minimum and maximum array size. * * @np:		device node from which the property value is to be read. * @propname:	name of the property to be searched. * @out_values:	pointer to found values. * @sz_min:	minimum number of array elements to read * @sz_max:	maximum number of array elements to read, if zero there is no *		upper limit on the number of elements in the dts entry but only *		sz_min will be read. * * Search for a property in a device node and read 8-bit value(s) from * it. * * dts entry of array should be like: *  ``property = /bits/ 8 <0x50 0x60 0x70>;`` * * Return: The number of elements read on success, -EINVAL if the property * does not exist, -ENODATA if property does not have a value, and -EOVERFLOW * if the property data is smaller than sz_min or longer than sz_max. * * The out_values is modified only if a valid u8 value can be decoded. */int of_property_read_variable_u8_array(const struct device_node *np,					const char *propname, u8 *out_values,					size_t sz_min, size_t sz_max){	size_t sz, count;	const u8 *val = of_find_property_value_of_size(np, propname,						(sz_min * sizeof(*out_values)),						(sz_max * sizeof(*out_values)),						&sz); 	if (IS_ERR(val))		return PTR_ERR(val); 	if (!sz_max)		sz = sz_min;	else		sz /= sizeof(*out_values); 	count = sz;	while (count--)		*out_values++ = *val++; 	return sz;}EXPORT_SYMBOL_GPL(of_property_read_variable_u8_array); /** * of_property_read_variable_u16_array - Find and read an array of u16 from a * property, with bounds on the minimum and maximum array size. * * @np:		device node from which the property value is to be read. * @propname:	name of the property to be searched. * @out_values:	pointer to found values. * @sz_min:	minimum number of array elements to read * @sz_max:	maximum number of array elements to read, if zero there is no *		upper limit on the number of elements in the dts entry but only *		sz_min will be read. * * Search for a property in a device node and read 16-bit value(s) from * it. * * dts entry of array should be like: *  ``property = /bits/ 16 <0x5000 0x6000 0x7000>;`` * * Return: The number of elements read on success, -EINVAL if the property * does not exist, -ENODATA if property does not have a value, and -EOVERFLOW * if the property data is smaller than sz_min or longer than sz_max. * * The out_values is modified only if a valid u16 value can be decoded. */int of_property_read_variable_u16_array(const struct device_node *np,					const char *propname, u16 *out_values,					size_t sz_min, size_t sz_max){	size_t sz, count;	const __be16 *val = of_find_property_value_of_size(np, propname,						(sz_min * sizeof(*out_values)),						(sz_max * sizeof(*out_values)),						&sz); 	if (IS_ERR(val))		return PTR_ERR(val); 	if (!sz_max)		sz = sz_min;	else		sz /= sizeof(*out_values); 	count = sz;	while (count--)		*out_values++ = be16_to_cpup(val++); 	return sz;}EXPORT_SYMBOL_GPL(of_property_read_variable_u16_array); /** * of_property_read_variable_u32_array - Find and read an array of 32 bit * integers from a property, with bounds on the minimum and maximum array size. * * @np:		device node from which the property value is to be read. * @propname:	name of the property to be searched. * @out_values:	pointer to return found values. * @sz_min:	minimum number of array elements to read * @sz_max:	maximum number of array elements to read, if zero there is no *		upper limit on the number of elements in the dts entry but only *		sz_min will be read. * * Search for a property in a device node and read 32-bit value(s) from * it. * * Return: The number of elements read on success, -EINVAL if the property * does not exist, -ENODATA if property does not have a value, and -EOVERFLOW * if the property data is smaller than sz_min or longer than sz_max. * * The out_values is modified only if a valid u32 value can be decoded. */int of_property_read_variable_u32_array(const struct device_node *np,			       const char *propname, u32 *out_values,			       size_t sz_min, size_t sz_max){	size_t sz, count;	const __be32 *val = of_find_property_value_of_size(np, propname,						(sz_min * sizeof(*out_values)),						(sz_max * sizeof(*out_values)),						&sz); 	if (IS_ERR(val))		return PTR_ERR(val); 	if (!sz_max)		sz = sz_min;	else		sz /= sizeof(*out_values); 	count = sz;	while (count--)		*out_values++ = be32_to_cpup(val++); 	return sz;}EXPORT_SYMBOL_GPL(of_property_read_variable_u32_array); /** * of_property_read_u64 - Find and read a 64 bit integer from a property * @np:		device node from which the property value is to be read. * @propname:	name of the property to be searched. * @out_value:	pointer to return value, modified only if return value is 0. * * Search for a property in a device node and read a 64-bit value from * it. * * Return: 0 on success, -EINVAL if the property does not exist, * -ENODATA if property does not have a value, and -EOVERFLOW if the * property data isn't large enough. * * The out_value is modified only if a valid u64 value can be decoded. */int of_property_read_u64(const struct device_node *np, const char *propname,			 u64 *out_value){	const __be32 *val = of_find_property_value_of_size(np, propname,						sizeof(*out_value),						0,						NULL); 	if (IS_ERR(val))		return PTR_ERR(val); 	*out_value = of_read_number(val, 2);	return 0;}EXPORT_SYMBOL_GPL(of_property_read_u64); /** * of_property_read_variable_u64_array - Find and read an array of 64 bit * integers from a property, with bounds on the minimum and maximum array size. * * @np:		device node from which the property value is to be read. * @propname:	name of the property to be searched. * @out_values:	pointer to found values. * @sz_min:	minimum number of array elements to read * @sz_max:	maximum number of array elements to read, if zero there is no *		upper limit on the number of elements in the dts entry but only *		sz_min will be read. * * Search for a property in a device node and read 64-bit value(s) from * it. * * Return: The number of elements read on success, -EINVAL if the property * does not exist, -ENODATA if property does not have a value, and -EOVERFLOW * if the property data is smaller than sz_min or longer than sz_max. * * The out_values is modified only if a valid u64 value can be decoded. */int of_property_read_variable_u64_array(const struct device_node *np,			       const char *propname, u64 *out_values,			       size_t sz_min, size_t sz_max){	size_t sz, count;	const __be32 *val = of_find_property_value_of_size(np, propname,						(sz_min * sizeof(*out_values)),						(sz_max * sizeof(*out_values)),						&sz); 	if (IS_ERR(val))		return PTR_ERR(val); 	if (!sz_max)		sz = sz_min;	else		sz /= sizeof(*out_values); 

Lines 1–500 of 1712.