]> pilppa.org Git - linux-2.6-omap-h63xx.git/blobdiff - drivers/of/base.c
Merge branch 'for-linus' of git://www.jni.nu/cris
[linux-2.6-omap-h63xx.git] / drivers / of / base.c
index 23ffb7c0caf24148277ddd19c7430b5c90fb6fcb..7c79e94a35eac932a165367a11439a8af1d35cc0 100644 (file)
@@ -385,3 +385,185 @@ struct device_node *of_find_matching_node(struct device_node *from,
        return np;
 }
 EXPORT_SYMBOL(of_find_matching_node);
+
+/**
+ * of_modalias_table: Table of explicit compatible ==> modalias mappings
+ *
+ * This table allows particulare compatible property values to be mapped
+ * to modalias strings.  This is useful for busses which do not directly
+ * understand the OF device tree but are populated based on data contained
+ * within the device tree.  SPI and I2C are the two current users of this
+ * table.
+ *
+ * In most cases, devices do not need to be listed in this table because
+ * the modalias value can be derived directly from the compatible table.
+ * However, if for any reason a value cannot be derived, then this table
+ * provides a method to override the implicit derivation.
+ *
+ * At the moment, a single table is used for all bus types because it is
+ * assumed that the data size is small and that the compatible values
+ * should already be distinct enough to differentiate between SPI, I2C
+ * and other devices.
+ */
+struct of_modalias_table {
+       char *of_device;
+       char *modalias;
+};
+static struct of_modalias_table of_modalias_table[] = {
+       { "fsl,mcu-mpc8349emitx", "mcu-mpc8349emitx" },
+};
+
+/**
+ * of_modalias_node - Lookup appropriate modalias for a device node
+ * @node:      pointer to a device tree node
+ * @modalias:  Pointer to buffer that modalias value will be copied into
+ * @len:       Length of modalias value
+ *
+ * Based on the value of the compatible property, this routine will determine
+ * an appropriate modalias value for a particular device tree node.  Two
+ * separate methods are attempted to derive a modalias value.
+ *
+ * First method is to lookup the compatible value in of_modalias_table.
+ * Second is to strip off the manufacturer prefix from the first
+ * compatible entry and use the remainder as modalias
+ *
+ * This routine returns 0 on success
+ */
+int of_modalias_node(struct device_node *node, char *modalias, int len)
+{
+       int i, cplen;
+       const char *compatible;
+       const char *p;
+
+       /* 1. search for exception list entry */
+       for (i = 0; i < ARRAY_SIZE(of_modalias_table); i++) {
+               compatible = of_modalias_table[i].of_device;
+               if (!of_device_is_compatible(node, compatible))
+                       continue;
+               strlcpy(modalias, of_modalias_table[i].modalias, len);
+               return 0;
+       }
+
+       compatible = of_get_property(node, "compatible", &cplen);
+       if (!compatible)
+               return -ENODEV;
+
+       /* 2. take first compatible entry and strip manufacturer */
+       p = strchr(compatible, ',');
+       if (!p)
+               return -ENODEV;
+       p++;
+       strlcpy(modalias, p, len);
+       return 0;
+}
+EXPORT_SYMBOL_GPL(of_modalias_node);
+
+/**
+ * of_parse_phandles_with_args - Find a node pointed by phandle in a list
+ * @np:                pointer to a device tree node containing a list
+ * @list_name: property name that contains a list
+ * @cells_name:        property name that specifies phandles' arguments count
+ * @index:     index of a phandle to parse out
+ * @out_node:  pointer to device_node struct pointer (will be filled)
+ * @out_args:  pointer to arguments pointer (will be filled)
+ *
+ * This function is useful to parse lists of phandles and their arguments.
+ * Returns 0 on success and fills out_node and out_args, on error returns
+ * appropriate errno value.
+ *
+ * Example:
+ *
+ * phandle1: node1 {
+ *     #list-cells = <2>;
+ * }
+ *
+ * phandle2: node2 {
+ *     #list-cells = <1>;
+ * }
+ *
+ * node3 {
+ *     list = <&phandle1 1 2 &phandle2 3>;
+ * }
+ *
+ * To get a device_node of the `node2' node you may call this:
+ * of_parse_phandles_with_args(node3, "list", "#list-cells", 2, &node2, &args);
+ */
+int of_parse_phandles_with_args(struct device_node *np, const char *list_name,
+                               const char *cells_name, int index,
+                               struct device_node **out_node,
+                               const void **out_args)
+{
+       int ret = -EINVAL;
+       const u32 *list;
+       const u32 *list_end;
+       int size;
+       int cur_index = 0;
+       struct device_node *node = NULL;
+       const void *args;
+
+       list = of_get_property(np, list_name, &size);
+       if (!list) {
+               ret = -ENOENT;
+               goto err0;
+       }
+       list_end = list + size / sizeof(*list);
+
+       while (list < list_end) {
+               const u32 *cells;
+               const phandle *phandle;
+
+               phandle = list;
+               args = list + 1;
+
+               /* one cell hole in the list = <>; */
+               if (!*phandle) {
+                       list++;
+                       goto next;
+               }
+
+               node = of_find_node_by_phandle(*phandle);
+               if (!node) {
+                       pr_debug("%s: could not find phandle\n",
+                                np->full_name);
+                       goto err0;
+               }
+
+               cells = of_get_property(node, cells_name, &size);
+               if (!cells || size != sizeof(*cells)) {
+                       pr_debug("%s: could not get %s for %s\n",
+                                np->full_name, cells_name, node->full_name);
+                       goto err1;
+               }
+
+               /* Next phandle is at offset of one phandle cell + #cells */
+               list += 1 + *cells;
+               if (list > list_end) {
+                       pr_debug("%s: insufficient arguments length\n",
+                                np->full_name);
+                       goto err1;
+               }
+next:
+               if (cur_index == index)
+                       break;
+
+               of_node_put(node);
+               node = NULL;
+               cur_index++;
+       }
+
+       if (!node) {
+               ret = -ENOENT;
+               goto err0;
+       }
+
+       *out_node = node;
+       *out_args = args;
+
+       return 0;
+err1:
+       of_node_put(node);
+err0:
+       pr_debug("%s failed with status %d\n", __func__, ret);
+       return ret;
+}
+EXPORT_SYMBOL(of_parse_phandles_with_args);