cmake: dts: Add dt_comp_path() cmake function

Add a new cmake extension function:

  dt_comp_path(<var> COMPATIBLE <compatible> [INDEX <idx>])

to get a list of paths for the nodes with the given <compatible>. This
is useful when we have to cycle through several nodes with the same
compatible.

Signed-off-by: Carlo Caione <ccaione@baylibre.com>
diff --git a/scripts/dts/gen_dts_cmake.py b/scripts/dts/gen_dts_cmake.py
index f86d2a8..8afc598 100755
--- a/scripts/dts/gen_dts_cmake.py
+++ b/scripts/dts/gen_dts_cmake.py
@@ -43,6 +43,7 @@
 import os
 import pickle
 import sys
+from collections import defaultdict
 
 sys.path.append(os.path.join(os.path.dirname(__file__), 'python-devicetree',
                              'src'))
@@ -95,6 +96,7 @@
         for alias in node.aliases:
             cmake_props.append(f'"DT_ALIAS|{alias}" "{path}"')
 
+    compatible2paths = defaultdict(list)
     for node in edt.nodes:
         cmake_props.append(f'"DT_NODE|{node.path}" TRUE')
 
@@ -117,6 +119,11 @@
                 cmake_prop = f'DT_PROP|{node.path}|{item}'
                 cmake_props.append(f'"{cmake_prop}" "{cmake_value}"')
 
+                if item == 'compatible':
+                    # compatibles is always an array
+                    for comp in node.props[item].val:
+                        compatible2paths[comp].append(node.path)
+
         if node.regs is not None:
             cmake_props.append(f'"DT_REG|{node.path}|NUM" "{len(node.regs)}"')
             cmake_addr = ''
@@ -136,6 +143,14 @@
             cmake_props.append(f'"DT_REG|{node.path}|ADDR" "{cmake_addr}"')
             cmake_props.append(f'"DT_REG|{node.path}|SIZE" "{cmake_size}"')
 
+    for comp in compatible2paths.keys():
+        cmake_path = ''
+        for path in compatible2paths[comp]:
+            cmake_path = f'{cmake_path}{path};'
+
+        cmake_comp = f'DT_COMP|{comp}'
+        cmake_props.append(f'"{cmake_comp}" "{cmake_path}"')
+
     with open(args.cmake_out, "w", encoding="utf-8") as cmake_file:
         print('add_custom_target(devicetree_target)', file=cmake_file)
         print(file=cmake_file)