Provide a helper function allowing users to read the compatible string
of the machine, hiding the access to the root node.
Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@oss.qualcomm.com>
---
drivers/of/base.c | 13 +++++++++++++
include/linux/of.h | 2 ++
2 files changed, 15 insertions(+)
diff --git a/drivers/of/base.c b/drivers/of/base.c
index 0b65039ece53aa90f30da2420a893a02ab4c6dd8..a7e27d5355929abd6d156b80c52f8f8b08fe6da1 100644
--- a/drivers/of/base.c
+++ b/drivers/of/base.c
@@ -434,6 +434,19 @@ bool of_machine_compatible_match(const char *const *compats)
}
EXPORT_SYMBOL(of_machine_compatible_match);
+/**
+ * of_machine_get_compatible - Get the compatible string of this machine
+ * @compatible: address at which the compatible string will be stored
+ *
+ * Returns:
+ * 0 on success, negative error number on failure.
+ */
+int of_machine_get_compatible(const char **compatible)
+{
+ return of_property_read_string(of_root, "compatible", compatible);
+}
+EXPORT_SYMBOL_GPL(of_machine_get_compatible);
+
/**
* of_machine_device_match - Test root of device tree against a of_device_id array
* @matches: NULL terminated array of of_device_id match structures to search in
diff --git a/include/linux/of.h b/include/linux/of.h
index 9bbdcf25a2b448ba4ec5ddee8b35a105ca4aab8b..75423fb556ee4c108ce25144a0bdc252a89f7d1d 100644
--- a/include/linux/of.h
+++ b/include/linux/of.h
@@ -426,6 +426,8 @@ static inline bool of_machine_is_compatible(const char *compat)
return of_machine_compatible_match(compats);
}
+int of_machine_get_compatible(const char **compatible);
+
extern int of_add_property(struct device_node *np, struct property *prop);
extern int of_remove_property(struct device_node *np, struct property *prop);
extern int of_update_property(struct device_node *np, struct property *newprop);
--
2.47.3
Hi Bartosz,
On Mon, 19 Jan 2026 at 11:40, Bartosz Golaszewski
<bartosz.golaszewski@oss.qualcomm.com> wrote:
> Provide a helper function allowing users to read the compatible string
> of the machine, hiding the access to the root node.
>
> Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@oss.qualcomm.com>
Thanks for your patch!
> --- a/drivers/of/base.c
> +++ b/drivers/of/base.c
> @@ -434,6 +434,19 @@ bool of_machine_compatible_match(const char *const *compats)
> }
> EXPORT_SYMBOL(of_machine_compatible_match);
>
> +/**
> + * of_machine_get_compatible - Get the compatible string of this machine
... the first compatible string...
Do you see a need for adding an index parameter?
> + * @compatible: address at which the compatible string will be stored
> + *
> + * Returns:
> + * 0 on success, negative error number on failure.
> + */
> +int of_machine_get_compatible(const char **compatible)
> +{
> + return of_property_read_string(of_root, "compatible", compatible);
> +}
> +EXPORT_SYMBOL_GPL(of_machine_get_compatible);
> +
> /**
> * of_machine_device_match - Test root of device tree against a of_device_id array
> * @matches: NULL terminated array of of_device_id match structures to search in
> --- a/include/linux/of.h
> +++ b/include/linux/of.h
> @@ -426,6 +426,8 @@ static inline bool of_machine_is_compatible(const char *compat)
> return of_machine_compatible_match(compats);
> }
>
> +int of_machine_get_compatible(const char **compatible);
> +
> extern int of_add_property(struct device_node *np, struct property *prop);
> extern int of_remove_property(struct device_node *np, struct property *prop);
> extern int of_update_property(struct device_node *np, struct property *newprop);
>
Do you need a dummy for the !CONFIG_OF case?
This is only used by drivers/soc/fsl/guts.c, and FSL_GUTS is selected
by MMC_SDHCI_OF_ESDHC, which is OF-only, but can be enabled
when PPC || COMPILE_TEST.
Gr{oetje,eeting}s,
Geert
--
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- geert@linux-m68k.org
In personal conversations with technical people, I call myself a hacker. But
when I'm talking to journalists I just say "programmer" or something like that.
-- Linus Torvalds
On Mon Jan 19, 2026 at 11:40 AM CET, Bartosz Golaszewski wrote:
> +/**
> + * of_machine_get_compatible - Get the compatible string of this machine
> + * @compatible: address at which the compatible string will be stored
> + *
> + * Returns:
> + * 0 on success, negative error number on failure.
> + */
> +int of_machine_get_compatible(const char **compatible)
I think the name of this function is not ideal. 'get' usually indicates that a
reference count will be taken, but this is not the case here.
I'm also not sure about the machine prefix. If we really want this helper I'd
suggest something along the lines of e.g. of_root_read_compatible().
> +{
> + return of_property_read_string(of_root, "compatible", compatible);
> +}
> +EXPORT_SYMBOL_GPL(of_machine_get_compatible);
On Mon, Jan 19, 2026 at 12:26 PM Danilo Krummrich <dakr@kernel.org> wrote: > > On Mon Jan 19, 2026 at 11:40 AM CET, Bartosz Golaszewski wrote: > > +/** > > + * of_machine_get_compatible - Get the compatible string of this machine > > + * @compatible: address at which the compatible string will be stored > > + * > > + * Returns: > > + * 0 on success, negative error number on failure. > > + */ > > +int of_machine_get_compatible(const char **compatible) > > I think the name of this function is not ideal. 'get' usually indicates that a > reference count will be taken, but this is not the case here. > > I'm also not sure about the machine prefix. If we really want this helper I'd > suggest something along the lines of e.g. of_root_read_compatible(). > Makes sense for the "read" part but I'm not sure about the "root" bit. We already have a whole set of "of_machine_" interfaces, like of_machine_is_compatible(). How about of_machine_read_compatible()? Bartosz
On Mon Jan 19, 2026 at 2:00 PM CET, Bartosz Golaszewski wrote: > How about of_machine_read_compatible()? SGTM.
Le 19/01/2026 à 11:40, Bartosz Golaszewski a écrit :
> Provide a helper function allowing users to read the compatible string
> of the machine, hiding the access to the root node.
>
> Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@oss.qualcomm.com>
Reviewed-by: Christophe Leroy (CS GROUP) <chleroy@kernel.org>
> ---
> drivers/of/base.c | 13 +++++++++++++
> include/linux/of.h | 2 ++
> 2 files changed, 15 insertions(+)
>
> diff --git a/drivers/of/base.c b/drivers/of/base.c
> index 0b65039ece53aa90f30da2420a893a02ab4c6dd8..a7e27d5355929abd6d156b80c52f8f8b08fe6da1 100644
> --- a/drivers/of/base.c
> +++ b/drivers/of/base.c
> @@ -434,6 +434,19 @@ bool of_machine_compatible_match(const char *const *compats)
> }
> EXPORT_SYMBOL(of_machine_compatible_match);
>
> +/**
> + * of_machine_get_compatible - Get the compatible string of this machine
> + * @compatible: address at which the compatible string will be stored
> + *
> + * Returns:
> + * 0 on success, negative error number on failure.
> + */
> +int of_machine_get_compatible(const char **compatible)
> +{
> + return of_property_read_string(of_root, "compatible", compatible);
> +}
> +EXPORT_SYMBOL_GPL(of_machine_get_compatible);
> +
> /**
> * of_machine_device_match - Test root of device tree against a of_device_id array
> * @matches: NULL terminated array of of_device_id match structures to search in
> diff --git a/include/linux/of.h b/include/linux/of.h
> index 9bbdcf25a2b448ba4ec5ddee8b35a105ca4aab8b..75423fb556ee4c108ce25144a0bdc252a89f7d1d 100644
> --- a/include/linux/of.h
> +++ b/include/linux/of.h
> @@ -426,6 +426,8 @@ static inline bool of_machine_is_compatible(const char *compat)
> return of_machine_compatible_match(compats);
> }
>
> +int of_machine_get_compatible(const char **compatible);
> +
> extern int of_add_property(struct device_node *np, struct property *prop);
> extern int of_remove_property(struct device_node *np, struct property *prop);
> extern int of_update_property(struct device_node *np, struct property *newprop);
>
© 2016 - 2026 Red Hat, Inc.