9
0
Fork 0

of: base: Use scoring in DT device matching

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>
Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
This commit is contained in:
Andrey Smirnov 2017-03-08 14:08:58 -08:00 committed by Sascha Hauer
parent a8b41f2c72
commit 3494084017
1 changed files with 21 additions and 16 deletions

View File

@ -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;
}
/**