Skip to content

Commit fd6868d

Browse files
committed
Merge tag 'devicetree-fixes-for-4.19' of git://git.kernel.org/pub/scm/linux/kernel/git/robh/linux
Pull devicetree updates from Rob Herring: "A couple of new helper functions in preparation for some tree wide clean-ups. I'm sending these new helpers now for rc2 in order to simplify the dependencies on subsequent cleanups across the tree in 4.20" * tag 'devicetree-fixes-for-4.19' of git://git.kernel.org/pub/scm/linux/kernel/git/robh/linux: of: Add device_type access helper functions of: add node name compare helper functions of: add helper to lookup compatible child node
2 parents a3ea991 + 0413bed commit fd6868d

File tree

2 files changed

+80
-0
lines changed

2 files changed

+80
-0
lines changed

drivers/of/base.c

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,28 @@ DEFINE_MUTEX(of_mutex);
5454
*/
5555
DEFINE_RAW_SPINLOCK(devtree_lock);
5656

57+
bool of_node_name_eq(const struct device_node *np, const char *name)
58+
{
59+
const char *node_name;
60+
size_t len;
61+
62+
if (!np)
63+
return false;
64+
65+
node_name = kbasename(np->full_name);
66+
len = strchrnul(node_name, '@') - node_name;
67+
68+
return (strlen(name) == len) && (strncmp(node_name, name, len) == 0);
69+
}
70+
71+
bool of_node_name_prefix(const struct device_node *np, const char *prefix)
72+
{
73+
if (!np)
74+
return false;
75+
76+
return strncmp(kbasename(np->full_name), prefix, strlen(prefix)) == 0;
77+
}
78+
5779
int of_n_addr_cells(struct device_node *np)
5880
{
5981
u32 cells;
@@ -719,6 +741,31 @@ struct device_node *of_get_next_available_child(const struct device_node *node,
719741
}
720742
EXPORT_SYMBOL(of_get_next_available_child);
721743

744+
/**
745+
* of_get_compatible_child - Find compatible child node
746+
* @parent: parent node
747+
* @compatible: compatible string
748+
*
749+
* Lookup child node whose compatible property contains the given compatible
750+
* string.
751+
*
752+
* Returns a node pointer with refcount incremented, use of_node_put() on it
753+
* when done; or NULL if not found.
754+
*/
755+
struct device_node *of_get_compatible_child(const struct device_node *parent,
756+
const char *compatible)
757+
{
758+
struct device_node *child;
759+
760+
for_each_child_of_node(parent, child) {
761+
if (of_device_is_compatible(child, compatible))
762+
break;
763+
}
764+
765+
return child;
766+
}
767+
EXPORT_SYMBOL(of_get_compatible_child);
768+
722769
/**
723770
* of_get_child_by_name - Find the child node by name for a given parent
724771
* @node: parent node

include/linux/of.h

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,9 @@ static inline unsigned long of_read_ulong(const __be32 *cell, int size)
256256
#define OF_IS_DYNAMIC(x) test_bit(OF_DYNAMIC, &x->_flags)
257257
#define OF_MARK_DYNAMIC(x) set_bit(OF_DYNAMIC, &x->_flags)
258258

259+
extern bool of_node_name_eq(const struct device_node *np, const char *name);
260+
extern bool of_node_name_prefix(const struct device_node *np, const char *prefix);
261+
259262
static inline const char *of_node_full_name(const struct device_node *np)
260263
{
261264
return np ? np->full_name : "<no-node>";
@@ -290,6 +293,8 @@ extern struct device_node *of_get_next_child(const struct device_node *node,
290293
extern struct device_node *of_get_next_available_child(
291294
const struct device_node *node, struct device_node *prev);
292295

296+
extern struct device_node *of_get_compatible_child(const struct device_node *parent,
297+
const char *compatible);
293298
extern struct device_node *of_get_child_by_name(const struct device_node *node,
294299
const char *name);
295300

@@ -561,6 +566,16 @@ static inline struct device_node *to_of_node(const struct fwnode_handle *fwnode)
561566
return NULL;
562567
}
563568

569+
static inline bool of_node_name_eq(const struct device_node *np, const char *name)
570+
{
571+
return false;
572+
}
573+
574+
static inline bool of_node_name_prefix(const struct device_node *np, const char *prefix)
575+
{
576+
return false;
577+
}
578+
564579
static inline const char* of_node_full_name(const struct device_node *np)
565580
{
566581
return "<no-node>";
@@ -632,6 +647,12 @@ static inline bool of_have_populated_dt(void)
632647
return false;
633648
}
634649

650+
static inline struct device_node *of_get_compatible_child(const struct device_node *parent,
651+
const char *compatible)
652+
{
653+
return NULL;
654+
}
655+
635656
static inline struct device_node *of_get_child_by_name(
636657
const struct device_node *node,
637658
const char *name)
@@ -967,6 +988,18 @@ static inline struct device_node *of_find_matching_node(
967988
return of_find_matching_node_and_match(from, matches, NULL);
968989
}
969990

991+
static inline const char *of_node_get_device_type(const struct device_node *np)
992+
{
993+
return of_get_property(np, "type", NULL);
994+
}
995+
996+
static inline bool of_node_is_type(const struct device_node *np, const char *type)
997+
{
998+
const char *match = of_node_get_device_type(np);
999+
1000+
return np && match && type && !strcmp(match, type);
1001+
}
1002+
9701003
/**
9711004
* of_property_count_u8_elems - Count the number of u8 elements in a property
9721005
*

0 commit comments

Comments
 (0)