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>
diff --git a/arch/sandbox/dts/test.dts b/arch/sandbox/dts/test.dts
index 2761588..c7fffa2 100644
--- a/arch/sandbox/dts/test.dts
+++ b/arch/sandbox/dts/test.dts
@@ -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>;
diff --git a/drivers/core/of_access.c b/drivers/core/of_access.c
index a52f5a6..df007e6 100644
--- a/drivers/core/of_access.c
+++ b/drivers/core/of_access.c
@@ -488,6 +488,44 @@
 	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);
diff --git a/drivers/core/ofnode.c b/drivers/core/ofnode.c
index 45ea84e..42f3c09 100644
--- a/drivers/core/ofnode.c
+++ b/drivers/core/ofnode.c
@@ -31,6 +31,68 @@
 	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);
diff --git a/drivers/core/read.c b/drivers/core/read.c
index c73508d..07ab8ab 100644
--- a/drivers/core/read.c
+++ b/drivers/core/read.c
@@ -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);
diff --git a/include/dm/of_access.h b/include/dm/of_access.h
index 5b7821d..83f34f0 100644
--- a/include/dm/of_access.h
+++ b/include/dm/of_access.h
@@ -265,6 +265,38 @@
 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
  *
  * Search for a property in a device node and read a 32-bit value from
diff --git a/include/dm/ofnode.h b/include/dm/ofnode.h
index 7ce1e4c..f608523 100644
--- a/include/dm/ofnode.h
+++ b/include/dm/ofnode.h
@@ -222,6 +222,46 @@
 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
  *
  * @node:	valid node reference to read property from
diff --git a/include/dm/read.h b/include/dm/read.h
index 1b54b69..122b9cd 100644
--- a/include/dm/read.h
+++ b/include/dm/read.h
@@ -32,6 +32,47 @@
 
 #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
  *
  * @dev:	device to read DT property from
@@ -772,6 +813,30 @@
 #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)
 {
diff --git a/test/dm/test-fdt.c b/test/dm/test-fdt.c
index 6118ad4..012f2f4 100644
--- a/test/dm/test-fdt.c
+++ b/test/dm/test-fdt.c
@@ -815,6 +815,8 @@
 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 @@
 
 	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);