summaryrefslogtreecommitdiffstats
path: root/drivers/of
diff options
context:
space:
mode:
authorAhmad Fatoum <a.fatoum@pengutronix.de>2021-12-09 11:57:08 +0100
committerSascha Hauer <s.hauer@pengutronix.de>2021-12-14 08:26:54 +0100
commit20192eb8b7914ee25d7f0dc9e19ff1d6d1e5fa44 (patch)
tree20a3f099a0bb006ad8795fcd9f884f5679b99a10 /drivers/of
parent3df8dadc8efe1c65da9487f81a084447da2ebdf8 (diff)
downloadbarebox-20192eb8b7914ee25d7f0dc9e19ff1d6d1e5fa44.tar.gz
barebox-20192eb8b7914ee25d7f0dc9e19ff1d6d1e5fa44.tar.xz
of: implement new of_property_sprintf
Board code may compute values for device tree properties and write them as strings. Make this easier by adding a of_property_write_string variant that does formatted output. This also saves an allocation, because asprintf buffer is reused. Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de> Link: https://lore.barebox.org/20211209105708.3517684-3-a.fatoum@pengutronix.de Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
Diffstat (limited to 'drivers/of')
-rw-r--r--drivers/of/base.c57
1 files changed, 47 insertions, 10 deletions
diff --git a/drivers/of/base.c b/drivers/of/base.c
index dd82243752..cc9c95aa1e 100644
--- a/drivers/of/base.c
+++ b/drivers/of/base.c
@@ -2158,6 +2158,21 @@ struct device_node *of_new_node(struct device_node *parent, const char *name)
return node;
}
+static struct property *__of_new_property(struct device_node *node, const char *name,
+ void *data, int len)
+{
+ struct property *prop;
+
+ prop = xzalloc(sizeof(*prop));
+ prop->name = xstrdup(name);
+ prop->length = len;
+ prop->value = data;
+
+ list_add_tail(&prop->list, &node->properties);
+
+ return prop;
+}
+
/**
* of_new_property - Add a new property to a node
* @node: device node to which the property is added
@@ -2173,19 +2188,13 @@ struct device_node *of_new_node(struct device_node *parent, const char *name)
struct property *of_new_property(struct device_node *node, const char *name,
const void *data, int len)
{
- struct property *prop;
-
- prop = xzalloc(sizeof(*prop));
- prop->name = xstrdup(name);
- prop->length = len;
- prop->value = xzalloc(len);
+ char *buf;
+ buf = xzalloc(len);
if (data)
- memcpy(prop->value, data, len);
+ memcpy(buf, data, len);
- list_add_tail(&prop->list, &node->properties);
-
- return prop;
+ return __of_new_property(node, name, buf, len);
}
/**
@@ -2257,6 +2266,34 @@ int of_set_property(struct device_node *np, const char *name, const void *val, i
return 0;
}
+int of_property_sprintf(struct device_node *np,
+ const char *propname, const char *fmt, ...)
+{
+ struct property *pp;
+ struct va_format vaf;
+ char *buf = NULL;
+ va_list args;
+ int len;
+
+ if (!np)
+ return -ENOENT;
+
+ va_start(args, fmt);
+ vaf.fmt = fmt;
+ vaf.va = &args;
+ len = asprintf(&buf, "%pV", &vaf);
+ va_end(args);
+
+ if (len < 0)
+ return -ENOMEM;
+
+ pp = of_find_property(np, propname, NULL);
+ of_delete_property(pp);
+
+ __of_new_property(np, propname, buf, len);
+ return len;
+}
+
static int mem_bank_num;
int of_add_memory(struct device_node *node, bool dump)