[U-Boot] [PATCH v2 01/12] drivers: core: device: add support to check dt compatible for a device/machine

Mugunthan V N
2016-04-12T08:46:45Z

Provide an api to check whether the given device or machine is
compatible with the given compat string which helps in making
decisions in drivers based on device or machine compatible.

Idea taken from Linux.

Signed-off-by: Mugunthan V N <mugunthanvnm@ti.com>
---

Changes from initial version:
* used fdt_node_check_compatible() to check if the device or
machine is compatible with the given compatible string.
* change first argument from node offset to device pointer so
that in future it is will be easy to migrate out of indexing
DT by offsets.

---
drivers/core/device.c | 14 ++++++++++++++
include/dm/device.h | 23 +++++++++++++++++++++++
2 files changed, 37 insertions(+)

diff --git a/drivers/core/device.c b/drivers/core/device.c
index cb24a61..d6ca704 100644
--- a/drivers/core/device.c
+++ b/drivers/core/device.c
@@ -691,3 +691,17 @@ int device_set_name(struct udevice *dev, const char *name)

return 0;
}
+
+bool of_device_is_compatible(struct udevice *dev, const char *compat)
+{
+ const void *fdt = gd->fdt_blob;
+
+ return !fdt_node_check_compatible(fdt, dev->of_offset, compat);
+}
+
+bool of_machine_is_compatible(const char *compat)
+{
+ const void *fdt = gd->fdt_blob;
+
+ return !fdt_node_check_compatible(fdt, 0, compat);
+}
diff --git a/include/dm/device.h b/include/dm/device.h
index 1cf8150..a913b51 100644
--- a/include/dm/device.h
+++ b/include/dm/device.h
@@ -510,6 +510,29 @@ bool device_is_last_sibling(struct udevice *dev);
int device_set_name(struct udevice *dev, const char *name);

/**
+ * of_device_is_compatible() - check if the device is compatible with the compat
+ *
+ * This allows to check whether the device is comaptible with the compat.
+ *
+ * @dev: udevice pointer for which compatible needs to be verified.
+ * @compat: Compatible string which needs to verified in the given
+ * device
+ * @return true if OK, false if the compatible is not found
+ */
+bool of_device_is_compatible(struct udevice *dev, const char *compat);
+
+/**
+ * of_machine_is_compatible() - check if the machine is compatible with
+ * the compat
+ *
+ * This allows to check whether the machine is comaptible with the compat.
+ *
+ * @compat: Compatible string which needs to verified
+ * @return true if OK, false if the compatible is not found
+ */
+bool of_machine_is_compatible(const char *compat);
+
+/**
* device_is_on_pci_bus - Test if a device is on a PCI bus
*
* @dev: device to test
--
2.8.1.101.g72d917a