From: Peng Fan <peng.fan@nxp.com>
Introduce new helper functions of_property_read_u32_default() and
of_property_read_s32_default() to simplify reading optional device tree
properties with a default value.
A very common pattern in drivers is to provide a default value and let
of_property_read_*() override it when the property is present, e.g.:
Y = Y_DEFAULT;
of_property_read_u32(np, "prop", &Y);
or equivalently, checking the return value explicitly:
ret = of_property_read_u32(np, "prop", &val);
if (ret)
Y = Y_DEFAULT;
else
Y = val;
Both forms express the same intent: the property is optional and a
well-defined default should be used if it cannot be read.
With the new helper, this can be expressed more directly as:
Y = of_property_read_u32_default(np, "prop", Y_DEFAULT);
The helpers intentionally ignore the error code and return either the
parsed value or the supplied default. They are meant for optional
properties only. Callers that need to handle or propagate errors should
continue using of_property_read_*() directly.
Signed-off-by: Peng Fan <peng.fan@nxp.com>
---
include/linux/of.h | 38 ++++++++++++++++++++++++++++++++++++++
1 file changed, 38 insertions(+)
diff --git a/include/linux/of.h b/include/linux/of.h
index 9bbdcf25a2b448ba4ec5ddee8b35a105ca4aab8b..ef9f63755b20722969b682eae98a0dbd8cb21d58 100644
--- a/include/linux/of.h
+++ b/include/linux/of.h
@@ -1450,6 +1450,25 @@ static inline int of_property_read_u32(const struct device_node *np,
return of_property_read_u32_array(np, propname, out_value, 1);
}
+
+/**
+ * of_property_read_u32_default() - Read a u32 DT property or return a default.
+ * @np: device node
+ * @propname: property name
+ * @def: default value to return if the property cannot be read
+ *
+ * Return: The property value on success, or @def if the property is missing
+ * or invalid. This helper intentionally ignores the error code; it
+ * is intended for optional properties with a sensible default.
+ */
+static inline u32 of_property_read_u32_default(const struct device_node *np,
+ const char *propname,
+ u32 def)
+{
+ of_property_read_u32(np, propname, &def);
+ return def;
+}
+
static inline int of_property_read_s32(const struct device_node *np,
const char *propname,
s32 *out_value)
@@ -1457,6 +1476,25 @@ static inline int of_property_read_s32(const struct device_node *np,
return of_property_read_u32(np, propname, (u32*) out_value);
}
+
+/**
+ * of_property_read_s32_default() - Read an s32 DT property or return a default.
+ * @np: device node
+ * @propname: property name
+ * @def: default value to return if the property cannot be read
+ *
+ * Return: The property value on success, or @def if the property is missing
+ * or invalid. This helper intentionally ignores the error code; it
+ * is intended for optional properties with a sensible default.
+ */
+static inline s32 of_property_read_s32_default(const struct device_node *np,
+ const char *propname,
+ s32 def)
+{
+ of_property_read_s32(np, propname, &def);
+ return def;
+}
+
#define of_for_each_phandle(it, err, np, ln, cn, cc) \
for (of_phandle_iterator_init((it), (np), (ln), (cn), (cc)), \
err = of_phandle_iterator_next(it); \
--
2.37.1
On Mon, Jan 19, 2026 at 10:02:54AM +0800, Peng Fan (OSS) wrote: > From: Peng Fan <peng.fan@nxp.com> > > Introduce new helper functions of_property_read_u32_default() and > of_property_read_s32_default() to simplify reading optional device tree > properties with a default value. > > A very common pattern in drivers is to provide a default value and let > of_property_read_*() override it when the property is present, e.g.: > > Y = Y_DEFAULT; > of_property_read_u32(np, "prop", &Y); This is how defaults were intended to be handled. > > or equivalently, checking the return value explicitly: > > ret = of_property_read_u32(np, "prop", &val); > if (ret) > Y = Y_DEFAULT; > else > Y = val; This is usually only needed if the variable type is different. Probably the better fix is fix the type difference. > Both forms express the same intent: the property is optional and a > well-defined default should be used if it cannot be read. > > With the new helper, this can be expressed more directly as: > > Y = of_property_read_u32_default(np, "prop", Y_DEFAULT); > > The helpers intentionally ignore the error code and return either the > parsed value or the supplied default. They are meant for optional > properties only. Callers that need to handle or propagate errors should > continue using of_property_read_*() directly. What about u8, u16, etc. and device_property_read_*? I'm really on the fence whether this is all worth it... We may also want to do something like of_property_read() implemented using C11 _Generic(). Not sure if that's worth the churn either. It would make doing some type checks harder. For example I could extract all property names from of_property_read_u32() calls and check their size against the schemas. (I have the first half of that already.) Using _Generic() would make that harder or impossible. Rob
Hi Rob, On Tue, Jan 20, 2026 at 04:21:55PM -0600, Rob Herring wrote: >On Mon, Jan 19, 2026 at 10:02:54AM +0800, Peng Fan (OSS) wrote: >> From: Peng Fan <peng.fan@nxp.com> >> >> Introduce new helper functions of_property_read_u32_default() and >> of_property_read_s32_default() to simplify reading optional device tree >> properties with a default value. >> >> A very common pattern in drivers is to provide a default value and let >> of_property_read_*() override it when the property is present, e.g.: >> >> Y = Y_DEFAULT; >> of_property_read_u32(np, "prop", &Y); > >This is how defaults were intended to be handled. >> >> or equivalently, checking the return value explicitly: >> >> ret = of_property_read_u32(np, "prop", &val); >> if (ret) >> Y = Y_DEFAULT; >> else >> Y = val; > >This is usually only needed if the variable type is different. Probably >the better fix is fix the type difference. I see. There are a few places that use above style to set default value, no type conversion. such as drivers/clk/socfpga/clk-gate-a10.c:71 drivers/clk/socfpga/clk-periph-a10.c:90 > >> Both forms express the same intent: the property is optional and a >> well-defined default should be used if it cannot be read. >> >> With the new helper, this can be expressed more directly as: >> >> Y = of_property_read_u32_default(np, "prop", Y_DEFAULT); >> >> The helpers intentionally ignore the error code and return either the >> parsed value or the supplied default. They are meant for optional >> properties only. Callers that need to handle or propagate errors should >> continue using of_property_read_*() directly. > >What about u8, u16, etc. and device_property_read_*? I'm really on the >fence whether this is all worth it... My cocci only reports some u32 usage using style "if.. else..". Let me write a cocci pattern to scan device_property_read_* usage. > >We may also want to do something like of_property_read() implemented >using C11 _Generic(). Not sure if that's worth the churn either. It >would make doing some type checks harder. For example I could extract >all property names from of_property_read_u32() calls and check their >size against the schemas. (I have the first half of that already.) Using >_Generic() would make that harder or impossible. I see. _Generic may not be a good idea if it makes type checks harder. Thanks Peng > >Rob >
© 2016 - 2026 Red Hat, Inc.