mail archive of the barebox mailing list
 help / color / mirror / Atom feed
From: Andrey Smirnov <andrew.smirnov@gmail.com>
To: barebox@lists.infradead.org
Cc: Andrey Smirnov <andrew.smirnov@gmail.com>
Subject: [PATCH 09/20] of: base: Use scoring in DT device matching
Date: Wed,  8 Mar 2017 14:08:58 -0800	[thread overview]
Message-ID: <20170308220909.4560-10-andrew.smirnov@gmail.com> (raw)
In-Reply-To: <20170308220909.4560-1-andrew.smirnov@gmail.com>

Port Linux kernel algorithm for both of_device_is_compatible() and
of_match_node(). With this change former now returns a score on the
scale of 0 to INT_MAX/2, and the latter goes through all compatiblity
entries and selects the entry that has the best matching score.

This is needed for SoCs where IP blocks are backwards compatible and
corresponding OF nodes can proclaim compatibility with several entries
found in driver's compatiblity table. One such example would be PIO
pinctrl block on AT91SAM9x5 SoCs which declare compatibility with with
both "atmel,at91sam9x5-pinctrl" and "atmel,at91rm9200-pinctrl".

Signed-off-by: Andrey Smirnov <andrew.smirnov@gmail.com>
---
 drivers/of/base.c | 37 +++++++++++++++++++++----------------
 1 file changed, 21 insertions(+), 16 deletions(-)

diff --git a/drivers/of/base.c b/drivers/of/base.c
index 0c20fcd..bef8f1d 100644
--- a/drivers/of/base.c
+++ b/drivers/of/base.c
@@ -472,21 +472,20 @@ EXPORT_SYMBOL(of_get_cpu_node);
 int of_device_is_compatible(const struct device_node *device,
 		const char *compat)
 {
+	struct property *prop;
 	const char *cp;
-	int cplen, l;
+	int index = 0, score = 0;
 
-	cp = of_get_property(device, "compatible", &cplen);
-	if (cp == NULL)
-		return 0;
-	while (cplen > 0) {
-		if (of_compat_cmp(cp, compat, strlen(compat)) == 0)
-			return 1;
-		l = strlen(cp) + 1;
-		cp += l;
-		cplen -= l;
+	prop = of_find_property(device, "compatible", NULL);
+	for (cp = of_prop_next_string(prop, NULL); cp;
+	     cp = of_prop_next_string(prop, cp), index++) {
+		if (of_compat_cmp(cp, compat, strlen(compat)) == 0) {
+			score = INT_MAX/2 - (index << 2);
+			break;
+		}
 	}
 
-	return 0;
+	return score;
 }
 EXPORT_SYMBOL(of_device_is_compatible);
 
@@ -602,16 +601,22 @@ EXPORT_SYMBOL(of_find_node_with_property);
 const struct of_device_id *of_match_node(const struct of_device_id *matches,
 					 const struct device_node *node)
 {
+	const struct of_device_id *best_match = NULL;
+	int score, best_score = 0;
+
 	if (!matches || !node)
 		return NULL;
 
-	while (matches->compatible) {
-		if (of_device_is_compatible(node, matches->compatible) == 1)
-			return matches;
-		matches++;
+	for (; matches->compatible; matches++) {
+		score = of_device_is_compatible(node, matches->compatible);
+
+		if (score > best_score) {
+			best_match = matches;
+			best_score = score;
+		}
 	}
 
-	return NULL;
+	return best_match;
 }
 
 /**
-- 
2.9.3


_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox

  parent reply	other threads:[~2017-03-08 22:09 UTC|newest]

Thread overview: 23+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-03-08 22:08 [PATCH 00/20] AT91, at91sam9x5ek updates (part I/III) Andrey Smirnov
2017-03-08 22:08 ` [PATCH 01/20] at91: Fix bug/typo in debug_ll.h Andrey Smirnov
2017-03-08 22:08 ` [PATCH 02/20] regmap: Implement syscon_node_to_regmap() Andrey Smirnov
2017-03-08 22:08 ` [PATCH 03/20] pinctrl: at91: Fix a bug in at91_pinctrl_set_conf() Andrey Smirnov
2017-03-08 22:08 ` [PATCH 04/20] pinctrl: at91: Fix a bug in at91_pinctrl_set_state() Andrey Smirnov
2017-03-08 22:08 ` [PATCH 05/20] pinctrl: at91: Implement .get_direction hook Andrey Smirnov
2017-03-08 22:08 ` [PATCH 06/20] clk: Port two helper functions from Linux Andrey Smirnov
2017-03-08 22:08 ` [PATCH 07/20] clk: Make COMMON_CLK_OF_PROVIDER depend on OFTREE Andrey Smirnov
2017-03-08 22:08 ` [PATCH 08/20] clk: No-op CLK_OF_DECLARE if not enabled Andrey Smirnov
2017-03-09 10:54   ` Sascha Hauer
2017-03-08 22:08 ` Andrey Smirnov [this message]
2017-03-08 22:08 ` [PATCH 10/20] serial: atmel: Check result of clk_get() Andrey Smirnov
2017-03-08 22:09 ` [PATCH 11/20] usb: ohci-at91: " Andrey Smirnov
2017-03-08 22:09 ` [PATCH 12/20] usb: ohci-at91: Convert global variables to private data Andrey Smirnov
2017-03-08 22:09 ` [PATCH 13/20] usb: ohci-at91: Check result of clk_enable() Andrey Smirnov
2017-03-08 22:09 ` [PATCH 14/20] usb: ehci-atmel: " Andrey Smirnov
2017-03-08 22:09 ` [PATCH 15/20] usb: echi-atmel: Convert global variables to private data Andrey Smirnov
2017-03-08 22:09 ` [PATCH 16/20] usb: ehci-atmel: Zero ehci_data before using it Andrey Smirnov
2017-03-08 22:09 ` [PATCH 17/20] usb: echi-atmel: Check result of ehci_register() Andrey Smirnov
2017-03-08 22:09 ` [PATCH 18/20] spi: atmel_spi: Configure CS GPIO as output Andrey Smirnov
2017-03-08 22:09 ` [PATCH 19/20] spi: atmel_spi: Use VERSION register instead of CPU type Andrey Smirnov
2017-03-08 22:09 ` [PATCH 20/20] clocksource: at91: Move to 'drivers/clocksource' Andrey Smirnov
2017-03-09  7:19 ` [PATCH 00/20] AT91, at91sam9x5ek updates (part I/III) Sascha Hauer

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20170308220909.4560-10-andrew.smirnov@gmail.com \
    --to=andrew.smirnov@gmail.com \
    --cc=barebox@lists.infradead.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox