[PATCH 06/16] of: implement of_alias_from_compatible

Ahmad Fatoum a.fatoum at barebox.org
Thu Jun 5 14:07:16 PDT 2025


This is going to be used in the incoming MIPI DSI support.

Signed-off-by: Ahmad Fatoum <a.fatoum at barebox.org>
---
 drivers/of/base.c | 31 +++++++++++++++++++++++++++++++
 include/of.h      |  1 +
 2 files changed, 32 insertions(+)

diff --git a/drivers/of/base.c b/drivers/of/base.c
index 1439e55a0aac..eb1f0b44f22d 100644
--- a/drivers/of/base.c
+++ b/drivers/of/base.c
@@ -3569,6 +3569,37 @@ int of_graph_port_is_available(struct device_node *node)
 }
 EXPORT_SYMBOL(of_graph_port_is_available);
 
+/**
+ * of_alias_from_compatible - Lookup appropriate alias for a device node
+ *			      depending on compatible
+ * @node:	pointer to a device tree node
+ * @alias:	Pointer to buffer that alias value will be copied into
+ * @len:	Length of alias value
+ *
+ * Based on the value of the compatible property, this routine will attempt
+ * to choose an appropriate alias value for a particular device tree node.
+ * It does this by stripping the manufacturer prefix (as delimited by a ',')
+ * from the first entry in the compatible list property.
+ *
+ * Note: The matching on just the "product" side of the compatible is a relic
+ * from I2C and SPI. Please do not add any new user.
+ *
+ * Return: This routine returns 0 on success, <0 on failure.
+ */
+int of_alias_from_compatible(const struct device_node *node, char *alias, int len)
+{
+	const char *compatible, *p;
+	int cplen;
+
+	compatible = of_get_property(node, "compatible", &cplen);
+	if (!compatible || strlen(compatible) > cplen)
+		return -ENODEV;
+	p = strchr(compatible, ',');
+	strscpy(alias, p ? p + 1 : compatible, len);
+	return 0;
+}
+EXPORT_SYMBOL_GPL(of_alias_from_compatible);
+
 /**
  * of_get_machine_compatible - get first compatible string from the root node.
  *
diff --git a/include/of.h b/include/of.h
index 2258cd501b72..fa2401fc8ded 100644
--- a/include/of.h
+++ b/include/of.h
@@ -196,6 +196,7 @@ extern struct device_node *of_copy_node(struct device_node *parent,
 extern struct device_node *of_dup(const struct device_node *root);
 extern void of_delete_node(struct device_node *node);
 
+extern int of_alias_from_compatible(const struct device_node *node, char *alias, int len);
 extern const char *of_get_machine_compatible(void);
 extern int of_machine_is_compatible(const char *compat);
 extern int of_device_is_compatible(const struct device_node *device,
-- 
2.39.5




More information about the barebox mailing list