dm: core: Add functions to read 8/16-bit integers

Add functions to read 8/16-bit integers like the existing functions for
32/64-bit to simplify read of 8/16-bit integers from device tree
properties.

Signed-off-by: Stefan Herbrechtsmeier <stefan.herbrechtsmeier@weidmueller.com>
Reviewed-by: Marek Vasut <marex@denx.de>
Reviewed-by: Simon Glass <sjg@chromium.org>
Reviewed-by: Simon Glass <sjg@chromium.org>
This commit is contained in:
Stefan Herbrechtsmeier 2022-06-14 15:21:30 +02:00 committed by Tom Rini
parent 7a0d88076b
commit b471bdc47b
8 changed files with 279 additions and 0 deletions

View file

@ -233,6 +233,8 @@
test5-gpios = <&gpio_a 19>;
bool-value;
int8-value = /bits/ 8 <0x12>;
int16-value = /bits/ 16 <0x1234>;
int-value = <1234>;
uint-value = <(-1234)>;
int64-value = /bits/ 64 <0x1111222233334444>;

View file

@ -488,6 +488,44 @@ static void *of_find_property_value_of_size(const struct device_node *np,
return prop->value;
}
int of_read_u8(const struct device_node *np, const char *propname, u8 *outp)
{
const u8 *val;
debug("%s: %s: ", __func__, propname);
if (!np)
return -EINVAL;
val = of_find_property_value_of_size(np, propname, sizeof(*outp));
if (IS_ERR(val)) {
debug("(not found)\n");
return PTR_ERR(val);
}
*outp = *val;
debug("%#x (%d)\n", *outp, *outp);
return 0;
}
int of_read_u16(const struct device_node *np, const char *propname, u16 *outp)
{
const __be16 *val;
debug("%s: %s: ", __func__, propname);
if (!np)
return -EINVAL;
val = of_find_property_value_of_size(np, propname, sizeof(*outp));
if (IS_ERR(val)) {
debug("(not found)\n");
return PTR_ERR(val);
}
*outp = be16_to_cpup(val);
debug("%#x (%d)\n", *outp, *outp);
return 0;
}
int of_read_u32(const struct device_node *np, const char *propname, u32 *outp)
{
return of_read_u32_index(np, propname, 0, outp);

View file

@ -31,6 +31,68 @@ bool ofnode_name_eq(ofnode node, const char *name)
return (strlen(name) == len) && !strncmp(node_name, name, len);
}
int ofnode_read_u8(ofnode node, const char *propname, u8 *outp)
{
const u8 *cell;
int len;
assert(ofnode_valid(node));
debug("%s: %s: ", __func__, propname);
if (ofnode_is_np(node))
return of_read_u8(ofnode_to_np(node), propname, outp);
cell = fdt_getprop(gd->fdt_blob, ofnode_to_offset(node), propname,
&len);
if (!cell || len < sizeof(*cell)) {
debug("(not found)\n");
return -EINVAL;
}
*outp = *cell;
debug("%#x (%d)\n", *outp, *outp);
return 0;
}
u8 ofnode_read_u8_default(ofnode node, const char *propname, u8 def)
{
assert(ofnode_valid(node));
ofnode_read_u8(node, propname, &def);
return def;
}
int ofnode_read_u16(ofnode node, const char *propname, u16 *outp)
{
const fdt16_t *cell;
int len;
assert(ofnode_valid(node));
debug("%s: %s: ", __func__, propname);
if (ofnode_is_np(node))
return of_read_u16(ofnode_to_np(node), propname, outp);
cell = fdt_getprop(gd->fdt_blob, ofnode_to_offset(node), propname,
&len);
if (!cell || len < sizeof(*cell)) {
debug("(not found)\n");
return -EINVAL;
}
*outp = be16_to_cpup(cell);
debug("%#x (%d)\n", *outp, *outp);
return 0;
}
u16 ofnode_read_u16_default(ofnode node, const char *propname, u16 def)
{
assert(ofnode_valid(node));
ofnode_read_u16(node, propname, &def);
return def;
}
int ofnode_read_u32(ofnode node, const char *propname, u32 *outp)
{
return ofnode_read_u32_index(node, propname, 0, outp);

View file

@ -13,6 +13,27 @@
#include <asm/io.h>
#include <linux/ioport.h>
int dev_read_u8(const struct udevice *dev, const char *propname, u8 *outp)
{
return ofnode_read_u8(dev_ofnode(dev), propname, outp);
}
u8 dev_read_u8_default(const struct udevice *dev, const char *propname, u8 def)
{
return ofnode_read_u8_default(dev_ofnode(dev), propname, def);
}
int dev_read_u16(const struct udevice *dev, const char *propname, u16 *outp)
{
return ofnode_read_u16(dev_ofnode(dev), propname, outp);
}
u16 dev_read_u16_default(const struct udevice *dev, const char *propname,
u16 def)
{
return ofnode_read_u16_default(dev_ofnode(dev), propname, def);
}
int dev_read_u32(const struct udevice *dev, const char *propname, u32 *outp)
{
return ofnode_read_u32(dev_ofnode(dev), propname, outp);

View file

@ -264,6 +264,38 @@ struct device_node *of_find_node_by_prop_value(struct device_node *from,
*/
struct device_node *of_find_node_by_phandle(phandle handle);
/**
* of_read_u8() - Find and read a 8-bit integer from a property
*
* Search for a property in a device node and read a 8-bit value from
* it.
*
* @np: device node from which the property value is to be read.
* @propname: name of the property to be searched.
* @outp: pointer to return value, modified only if return value is 0.
*
* 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.
*/
int of_read_u8(const struct device_node *np, const char *propname, u8 *outp);
/**
* of_read_u16() - Find and read a 16-bit integer from a property
*
* Search for a property in a device node and read a 16-bit value from
* it.
*
* @np: device node from which the property value is to be read.
* @propname: name of the property to be searched.
* @outp: pointer to return value, modified only if return value is 0.
*
* 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.
*/
int of_read_u16(const struct device_node *np, const char *propname, u16 *outp);
/**
* of_read_u32() - Find and read a 32-bit integer from a property
*

View file

@ -221,6 +221,46 @@ static inline oftree oftree_default(void)
*/
bool ofnode_name_eq(ofnode node, const char *name);
/**
* ofnode_read_u8() - Read a 8-bit integer from a property
*
* @node: valid node reference to read property from
* @propname: name of the property to read from
* @outp: place to put value (if found)
* Return: 0 if OK, -ve on error
*/
int ofnode_read_u8(ofnode node, const char *propname, u8 *outp);
/**
* ofnode_read_u8_default() - Read a 8-bit integer from a property
*
* @node: valid node reference to read property from
* @propname: name of the property to read from
* @def: default value to return if the property has no value
* Return: property value, or @def if not found
*/
u8 ofnode_read_u8_default(ofnode node, const char *propname, u8 def);
/**
* ofnode_read_u16() - Read a 16-bit integer from a property
*
* @node: valid node reference to read property from
* @propname: name of the property to read from
* @outp: place to put value (if found)
* Return: 0 if OK, -ve on error
*/
int ofnode_read_u16(ofnode node, const char *propname, u16 *outp);
/**
* ofnode_read_u16_default() - Read a 16-bit integer from a property
*
* @node: valid node reference to read property from
* @propname: name of the property to read from
* @def: default value to return if the property has no value
* Return: property value, or @def if not found
*/
u16 ofnode_read_u16_default(ofnode node, const char *propname, u16 def);
/**
* ofnode_read_u32() - Read a 32-bit integer from a property
*

View file

@ -31,6 +31,47 @@ static inline const struct device_node *dev_np(const struct udevice *dev)
#endif
#if !defined(CONFIG_DM_DEV_READ_INLINE) || CONFIG_IS_ENABLED(OF_PLATDATA)
/**
* dev_read_u8() - read a 8-bit integer from a device's DT property
*
* @dev: device to read DT property from
* @propname: name of the property to read from
* @outp: place to put value (if found)
* Return: 0 if OK, -ve on error
*/
int dev_read_u8(const struct udevice *dev, const char *propname, u8 *outp);
/**
* dev_read_u8_default() - read a 8-bit integer from a device's DT property
*
* @dev: device to read DT property from
* @propname: name of the property to read from
* @def: default value to return if the property has no value
* Return: property value, or @def if not found
*/
u8 dev_read_u8_default(const struct udevice *dev, const char *propname, u8 def);
/**
* dev_read_u16() - read a 16-bit integer from a device's DT property
*
* @dev: device to read DT property from
* @propname: name of the property to read from
* @outp: place to put value (if found)
* Return: 0 if OK, -ve on error
*/
int dev_read_u16(const struct udevice *dev, const char *propname, u16 *outp);
/**
* dev_read_u16_default() - read a 16-bit integer from a device's DT property
*
* @dev: device to read DT property from
* @propname: name of the property to read from
* @def: default value to return if the property has no value
* Return: property value, or @def if not found
*/
u16 dev_read_u16_default(const struct udevice *dev, const char *propname,
u16 def);
/**
* dev_read_u32() - read a 32-bit integer from a device's DT property
*
@ -772,6 +813,30 @@ phy_interface_t dev_read_phy_mode(const struct udevice *dev);
#else /* CONFIG_DM_DEV_READ_INLINE is enabled */
#include <asm/global_data.h>
static inline int dev_read_u8(const struct udevice *dev,
const char *propname, u8 *outp)
{
return ofnode_read_u8(dev_ofnode(dev), propname, outp);
}
static inline int dev_read_u8_default(const struct udevice *dev,
const char *propname, u8 def)
{
return ofnode_read_u8_default(dev_ofnode(dev), propname, def);
}
static inline int dev_read_u16(const struct udevice *dev,
const char *propname, u16 *outp)
{
return ofnode_read_u16(dev_ofnode(dev), propname, outp);
}
static inline int dev_read_u16_default(const struct udevice *dev,
const char *propname, u16 def)
{
return ofnode_read_u16_default(dev_ofnode(dev), propname, def);
}
static inline int dev_read_u32(const struct udevice *dev,
const char *propname, u32 *outp)
{

View file

@ -815,6 +815,8 @@ DM_TEST(dm_test_first_child, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
static int dm_test_read_int(struct unit_test_state *uts)
{
struct udevice *dev;
u8 val8;
u16 val16;
u32 val32;
s32 sval;
uint val;
@ -822,6 +824,23 @@ static int dm_test_read_int(struct unit_test_state *uts)
ut_assertok(uclass_first_device_err(UCLASS_TEST_FDT, &dev));
ut_asserteq_str("a-test", dev->name);
ut_assertok(dev_read_u8(dev, "int8-value", &val8));
ut_asserteq(0x12, val8);
ut_asserteq(-EINVAL, dev_read_u8(dev, "missing", &val8));
ut_asserteq(6, dev_read_u8_default(dev, "missing", 6));
ut_asserteq(0x12, dev_read_u8_default(dev, "int8-value", 6));
ut_assertok(dev_read_u16(dev, "int16-value", &val16));
ut_asserteq(0x1234, val16);
ut_asserteq(-EINVAL, dev_read_u16(dev, "missing", &val16));
ut_asserteq(6, dev_read_u16_default(dev, "missing", 6));
ut_asserteq(0x1234, dev_read_u16_default(dev, "int16-value", 6));
ut_assertok(dev_read_u32(dev, "int-value", &val32));
ut_asserteq(1234, val32);