of: Add and use of_machine_get_match() helper
Subsystems (6)
Geert Uytterhoeven
maintainercover of: Add and use of_machine_get_match() helper
Hi all,
Currently, there are two helpers to match the root compatible value against an of_device_id array:
- of_machine_device_match() returns true if a match is found,
- of_machine_get_match_data() returns the match data if a match is found.
However, there is no helper that returns the actual of_device_id structure corresponding to the match, leading to code duplication in various drivers. Worse, with the plan to make of_root private[1], more open-coded users may appear.
Hence this series adds a new helper of_machine_get_match(), which returns the match entry, and converts several drivers to make use of it. Note that the new wrapper comes at no cost (binary size-wise), as the variant returning bool can be a trivial inline wrapper.
One could argue we don't even need the variant that returns bool, as
"if (of_machine_device_match(...))" and
"if (of_machine_get_match(...))",
and
"if (!of_machine_device_match(...))" and
"if (!of_machine_get_match(...))"are equivalent.
The return type only matters when assigning to or returning an explicit type, like in drivers/soc/tegra/common.c:
bool soc_is_tegra(void)
{
return of_machine_device_match(tegra_machine_match);
}Only the Renesas driver patch was tested on actual hardware.
Thanks for your comments!
[1] "[PATCH v2 0/9] soc: remove direct accesses to of_root from
drivers/soc/"
https://lore.kernel.org/[email protected]/Geert Uytterhoeven (7):
of: Add of_machine_get_match() helper
of: Convert to of_machine_get_match()
cpufreq: airoha: Convert to of_machine_get_match()
cpufreq: qcom-nvmem: Convert to of_machine_get_match()
cpufreq: ti-cpufreq: Convert to of_machine_get_match()
soc: qcom: pd-mapper: Convert to of_machine_get_match()
soc: renesas: Convert to of_machine_get_match()
drivers/cpufreq/airoha-cpufreq.c | 7 +------ drivers/cpufreq/qcom-cpufreq-nvmem.c | 16 ++-------------- drivers/cpufreq/ti-cpufreq.c | 12 +----------- drivers/of/base.c | 20 ++++++-------------- drivers/soc/qcom/qcom_pd_mapper.c | 8 +------- drivers/soc/renesas/renesas-soc.c | 2 +- include/linux/of.h | 11 ++++++++--- 7 files changed, 20 insertions(+), 56 deletions(-)
Signature · 12 lines
2.43.0
Gr{oetje,eeting}s,
Geert
--
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- [email protected]
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 TorvaldsGeert Uytterhoeven
maintainerPATCH 1/7 of: Add of_machine_get_match() helper
Currently, there are two helpers to match the root compatible value against an of_device_id array:
- of_machine_device_match() returns true if a match is found,
- of_machine_get_match_data() returns the match data if a match is found.
However, there is no helper that returns the actual of_device_id structure corresponding to the match, leading to code duplication in various drivers.
Fix this by reworking of_machine_device_match() to return the actual match structure, and renaming it to of_machine_get_match(). Retain the old of_machine_device_match() functionality using a cheap static inline wrapper around the new of_machine_get_match() helper.
--- drivers/of/base.c | 11 +++++------ include/linux/of.h | 11 ++++++++--- 2 files changed, 13 insertions(+), 9 deletions(-)
drivers/of/base.c +5 -6
@@ -435,13 +435,12 @@ bool of_machine_compatible_match(const char *const *compats)EXPORT_SYMBOL (of_machine_compatible_match);/** -* of_machine_device_match - Test root of device tree against a of_device_id array +* of_machine_get_match - Test root of device tree against an of_device_id array * @matches: NULL terminated array of of_device_id match structures to search in * -* Returns true if the root node has any of the given compatible values in its -* compatible property. +* Returns matched entry or NULL */ -bool of_machine_device_match (const struct of_device_id *matches) +const struct of_device_id *of_machine_get_match (const struct of_device_id *matches) {struct device_node *root;const struct of_device_id *match =NULL ;
@@ -452,9 +451,9 @@ bool of_machine_device_match(const struct of_device_id *matches)of_node_put (root); } -return match !=NULL ; +return match; } -EXPORT_SYMBOL (of_machine_device_match); +EXPORT_SYMBOL (of_machine_get_match);/** * of_machine_get_match_data - Tell if root of device tree has a matching of_match structure
include/linux/of.h +8 -3
@@ -410,7 +410,7 @@ extern int of_alias_get_id(const struct device_node *np, const char *stem);extern int of_alias_get_highest_id (const char *stem);bool of_machine_compatible_match (const char *const *compats); -bool of_machine_device_match (const struct of_device_id *matches); +const struct of_device_id *of_machine_get_match (const struct of_device_id *matches);const void *of_machine_get_match_data (const struct of_device_id *matches);/**
@@ -866,9 +866,9 @@ static inline bool of_machine_compatible_match(const char *const *compats)return false; } -static inline bool of_machine_device_match (const struct of_device_id *matches) +static inline const struct of_device_id *of_machine_get_match (const struct of_device_id *matches) { -return false; +return NULL ; } static inline const void *
@@ -976,6 +976,11 @@ static inline int of_numa_init(void) }#endif +static inline bool of_machine_device_match (const struct of_device_id *matches) +{ +return of_machine_get_match (matches) !=NULL ; +} +static inline struct device_node *of_find_matching_node (struct device_node *from,const struct of_device_id *matches)
Signature · 1 line
2.43.0
Geert Uytterhoeven
maintainerPATCH 2/7 of: Convert to of_machine_get_match()
In reply to Geert UytterhoevenUse the of_machine_get_match() helper instead of open-coding the same operation.
--- drivers/of/base.c | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-)
drivers/of/base.c +1 -8
@@ -504,15 +504,8 @@ EXPORT_SYMBOL(of_machine_get_match);const void *of_machine_get_match_data (const struct of_device_id *matches) {const struct of_device_id *match; -struct device_node *root; - - root =of_find_node_by_path ("/" ); -if (!root) -return NULL ; - - match =of_match_node (matches, root); -of_node_put (root); + match =of_machine_get_match (matches);if (!match)return NULL ;
Signature · 1 line
2.43.0
Geert Uytterhoeven
maintainerPATCH 3/7 cpufreq: airoha: Convert to of_machine_get_match()
In reply to Geert UytterhoevenUse the of_machine_get_match() helper instead of open-coding the same operation.
---
Compile-tested only.
--- drivers/cpufreq/airoha-cpufreq.c | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-)
drivers/cpufreq/airoha-cpufreq.c +1 -6
@@ -115,15 +115,10 @@ MODULE_DEVICE_TABLE(of, airoha_cpufreq_match_list);static int __init airoha_cpufreq_init (void ) { -struct device_node *np =of_find_node_by_path ("/" );const struct of_device_id *match;int ret; -if (!np) -return -ENODEV ; - - match =of_match_node (airoha_cpufreq_match_list, np); -of_node_put (np); + match =of_machine_get_match (airoha_cpufreq_match_list);if (!match)return -ENODEV ;
Signature · 1 line
2.43.0
Geert Uytterhoeven
maintainerPATCH 4/7 cpufreq: qcom-nvmem: Convert to of_machine_get_match()
In reply to Geert UytterhoevenUse the of_machine_get_match() helper instead of open-coding the same operation.
---
Compile-tested only.
--- drivers/cpufreq/qcom-cpufreq-nvmem.c | 16 ++-------------- 1 file changed, 2 insertions(+), 14 deletions(-)
drivers/cpufreq/qcom-cpufreq-nvmem.c +2 -14
@@ -291,17 +291,9 @@ static int qcom_cpufreq_ipq8064_name_version(struct device *cpu_dev, ret =qcom_smem_get_soc_id (&msm_id);if (ret == -ENODEV ) {const struct of_device_id *match; -struct device_node *root; - - root =of_find_node_by_path ("/" ); -if (!root) { - ret = -ENODEV ; - goto exit; - }/* Fallback to compatible match with no SMEM initialized */ - match =of_match_node (qcom_cpufreq_ipq806x_match_list, root); -of_node_put (root); + match =of_machine_get_match (qcom_cpufreq_ipq806x_match_list);if (!match) { ret = -ENODEV ; goto exit;
@@ -647,14 +639,10 @@ MODULE_DEVICE_TABLE(of, qcom_cpufreq_match_list);*/ static int __init qcom_cpufreq_init (void ) { - struct device_node *np __free(device_node) = of_find_node_by_path("/");const struct of_device_id *match;int ret; -if (!np) -return -ENODEV ; - - match =of_match_node (qcom_cpufreq_match_list, np); + match =of_machine_get_match (qcom_cpufreq_match_list);if (!match)return -ENODEV ;
Signature · 1 line
2.43.0
Geert Uytterhoeven
maintainerPATCH 5/7 cpufreq: ti-cpufreq: Convert to of_machine_get_match()
In reply to Geert UytterhoevenUse the of_machine_get_match() helper instead of open-coding the same operation.
---
Compile-tested only.
--- drivers/cpufreq/ti-cpufreq.c | 12 +----------- 1 file changed, 1 insertion(+), 11 deletions(-)
drivers/cpufreq/ti-cpufreq.c +1 -11
@@ -502,16 +502,6 @@ static const struct of_device_id ti_cpufreq_of_match[] __maybe_unused = { {}, }; -static const struct of_device_id *ti_cpufreq_match_node (void ) -{ - struct device_node *np __free(device_node) = of_find_node_by_path("/"); -const struct of_device_id *match; - - match =of_match_node (ti_cpufreq_of_match, np); - -return match; -} -static int ti_cpufreq_probe (struct platform_device *pdev) {u32 version[VERSION_COUNT ];
@@ -596,7 +586,7 @@ static int __init ti_cpufreq_init(void)const struct of_device_id *match;/* Check to ensure we are on a compatible platform */ - match =ti_cpufreq_match_node (); + match =of_machine_get_match (ti_cpufreq_of_match);if (match)platform_device_register_data (NULL ,"ti-cpufreq" ,-1 , match,sizeof (*match));
Signature · 1 line
2.43.0
Geert Uytterhoeven
maintainerPATCH 6/7 soc: qcom: pd-mapper: Convert to of_machine_get_match()
In reply to Geert UytterhoevenUse the of_machine_get_match() helper instead of open-coding the same operation.
---
Compile-tested only.
--- drivers/soc/qcom/qcom_pd_mapper.c | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-)
drivers/soc/qcom/qcom_pd_mapper.c +1 -7
@@ -615,15 +615,9 @@ static struct qcom_pdm_data *qcom_pdm_start(void)const struct qcom_pdm_domain_data *const *domains;const struct of_device_id *match;struct qcom_pdm_data *data; -struct device_node *root;int ret, i; - root =of_find_node_by_path ("/" ); -if (!root) -return ERR_PTR (-ENODEV ); - - match =of_match_node (qcom_pdm_domains, root); -of_node_put (root); + match =of_machine_get_match (qcom_pdm_domains);if (!match) {pr_notice ("PDM: no support for the platform, userspace daemon might be required.\n" );return ERR_PTR (-ENODEV );
Signature · 1 line
2.43.0
Re: [PATCH 6/7] soc: qcom: pd-mapper: Convert to of_machine_get_match()
Quoted 7 lines · Geert Uytterhoeven
Use the
of_machine_get_match()helper instead of open-coding the same operation.Signed-off-by Geert Uytterhoeven <[email protected]>---
Compile-tested only.
---
Konrad
Re: [PATCH 6/7] soc: qcom: pd-mapper: Convert to of_machine_get_match()
In reply to Geert UytterhoevenQuoted 4 lines · Geert Uytterhoeven
Use the
of_machine_get_match()helper instead of open-coding the same operation.Signed-off-by Geert Uytterhoeven <[email protected]>
Regards,
Bjorn
Quoted 30 lines
---
Compile-tested only.--- drivers/soc/qcom/qcom_pd_mapper.c | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-)drivers/soc/qcom/qcom_pd_mapper.c +1
-7@@ -615,15 +615,9 @@ static struct qcom_pdm_data *qcom_pdm_start(void)const struct qcom_pdm_domain_data *const *domains;const struct of_device_id *match;struct qcom_pdm_data *data; -struct device_node *root;int ret, i; - root =of_find_node_by_path ("/" ); -if (!root) -return ERR_PTR (-ENODEV ); - - match =of_match_node (qcom_pdm_domains, root); -of_node_put (root); + match =of_machine_get_match (qcom_pdm_domains);if (!match) {pr_notice ("PDM: no support for the platform, userspace daemon might be required.\n" );return ERR_PTR (-ENODEV );Signature · 1 line
2.43.0
Geert Uytterhoeven
maintainerPATCH 7/7 soc: renesas: Convert to of_machine_get_match()
In reply to Geert UytterhoevenUse the of_machine_get_match() helper to avoid accessing of_root directly, which is planned to become private.
---
This is an alternative solution to "[PATCH v2 8/9] soc: renesas: don't access of_root directly"
https://lore.kernel.org/[email protected]
--- drivers/soc/renesas/renesas-soc.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/soc/renesas/renesas-soc.c +1 -1
@@ -488,7 +488,7 @@ static int __init renesas_soc_init(void)const char *soc_id;int ret; - match =of_match_node (renesas_socs, of_root); + match =of_machine_get_match (renesas_socs);if (!match)return -ENODEV ;
Signature · 1 line
2.43.0
Rob Herring
maintainerRe: [PATCH 7/7] soc: renesas: Convert to of_machine_get_match()
Quoted 8 lines · Geert Uytterhoeven
Use the
of_machine_get_match()helper to avoid accessingof_rootdirectly, which is planned to become private.Signed-off-by Geert Uytterhoeven <[email protected]>---
This is an alternative solution to "[PATCH v2 8/9] soc: renesas: don't accessof_rootdirectly"
https://lore.kernel.org/[email protected]
Greg applied this, so you'll have to respin on top of that. Next cycle I guess. Unless you get him to revert it.
I'm applying the rest of the series.
Rob
Re: [PATCH 7/7] soc: renesas: Convert to of_machine_get_match()
Hi Rob,
Quoted 4 lines · Rob Herring
Quoted 3 lines · Geert Uytterhoeven
Use the
of_machine_get_match()helper to avoid accessingof_rootdirectly, which is planned to become private.
Quoted 8 lines
Quoted 5 lines
Signed-off-by Geert Uytterhoeven <[email protected]>---
This is an alternative solution to "[PATCH v2 8/9] soc: renesas: don't accessof_rootdirectly"
https://lore.kernel.org/[email protected]Greg applied this, so you'll have to respin on top of that. Next cycle I guess. Unless you get him to revert it.
That was my impression, too, but apparently he skipped that patch. So you can still apply this patch, too.
Quoted 1 line
I'm applying the rest of the series.
Thanks!
Gr{oetje,eeting}s,
Geert
Signature · 5 lines
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- [email protected] 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
Re: [PATCH 7/7] soc: renesas: Convert to of_machine_get_match()
Quoted 11 lines · Geert Uytterhoeven
Quoted 10 lines · Rob Herring
Quoted 8 lines · Geert Uytterhoeven
Use the
of_machine_get_match()helper to avoid accessingof_rootdirectly, which is planned to become private.Signed-off-by Geert Uytterhoeven <[email protected]>---
This is an alternative solution to "[PATCH v2 8/9] soc: renesas: don't accessof_rootdirectly"
https://lore.kernel.org/[email protected]
Quoted 5 lines
Quoted 2 lines
Greg applied this, so you'll have to respin on top of that. Next cycle I guess. Unless you get him to revert it.
That was my impression, too, but apparently he skipped that patch. So you can still apply this patch, too.
Seems like it fell through the cracks... Will queue in renesas-devel for v7.2.
Gr{oetje,eeting}s,
Geert
Signature · 5 lines
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- [email protected] 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
Quoted 4 lines · Geert Uytterhoeven
Geert Uytterhoeven (7):
cpufreq: airoha: Convert toof_machine_get_match()
cpufreq: qcom-nvmem: Convert toof_machine_get_match()
cpufreq: ti-cpufreq: Convert toof_machine_get_match()
Signature · 1 line
viresh