Version:  2.0.40 2.2.26 2.4.37 3.13 3.14 3.15 3.16 3.17 3.18 3.19 4.0 4.1 4.2 4.3 4.4 4.5 4.6 4.7 4.8 4.9 4.10

Linux/drivers/of/platform.c

  1 /*
  2  *    Copyright (C) 2006 Benjamin Herrenschmidt, IBM Corp.
  3  *                       <benh@kernel.crashing.org>
  4  *    and                Arnd Bergmann, IBM Corp.
  5  *    Merged from powerpc/kernel/of_platform.c and
  6  *    sparc{,64}/kernel/of_device.c by Stephen Rothwell
  7  *
  8  *  This program is free software; you can redistribute it and/or
  9  *  modify it under the terms of the GNU General Public License
 10  *  as published by the Free Software Foundation; either version
 11  *  2 of the License, or (at your option) any later version.
 12  *
 13  */
 14 
 15 #define pr_fmt(fmt)     "OF: " fmt
 16 
 17 #include <linux/errno.h>
 18 #include <linux/module.h>
 19 #include <linux/amba/bus.h>
 20 #include <linux/device.h>
 21 #include <linux/dma-mapping.h>
 22 #include <linux/slab.h>
 23 #include <linux/of_address.h>
 24 #include <linux/of_device.h>
 25 #include <linux/of_irq.h>
 26 #include <linux/of_platform.h>
 27 #include <linux/platform_device.h>
 28 
 29 const struct of_device_id of_default_bus_match_table[] = {
 30         { .compatible = "simple-bus", },
 31         { .compatible = "simple-mfd", },
 32         { .compatible = "isa", },
 33 #ifdef CONFIG_ARM_AMBA
 34         { .compatible = "arm,amba-bus", },
 35 #endif /* CONFIG_ARM_AMBA */
 36         {} /* Empty terminated list */
 37 };
 38 
 39 static int of_dev_node_match(struct device *dev, void *data)
 40 {
 41         return dev->of_node == data;
 42 }
 43 
 44 /**
 45  * of_find_device_by_node - Find the platform_device associated with a node
 46  * @np: Pointer to device tree node
 47  *
 48  * Takes a reference to the embedded struct device which needs to be dropped
 49  * after use.
 50  *
 51  * Returns platform_device pointer, or NULL if not found
 52  */
 53 struct platform_device *of_find_device_by_node(struct device_node *np)
 54 {
 55         struct device *dev;
 56 
 57         dev = bus_find_device(&platform_bus_type, NULL, np, of_dev_node_match);
 58         return dev ? to_platform_device(dev) : NULL;
 59 }
 60 EXPORT_SYMBOL(of_find_device_by_node);
 61 
 62 #ifdef CONFIG_OF_ADDRESS
 63 /*
 64  * The following routines scan a subtree and registers a device for
 65  * each applicable node.
 66  *
 67  * Note: sparc doesn't use these routines because it has a different
 68  * mechanism for creating devices from device tree nodes.
 69  */
 70 
 71 /**
 72  * of_device_make_bus_id - Use the device node data to assign a unique name
 73  * @dev: pointer to device structure that is linked to a device tree node
 74  *
 75  * This routine will first try using the translated bus address to
 76  * derive a unique name. If it cannot, then it will prepend names from
 77  * parent nodes until a unique name can be derived.
 78  */
 79 void of_device_make_bus_id(struct device *dev)
 80 {
 81         struct device_node *node = dev->of_node;
 82         const __be32 *reg;
 83         u64 addr;
 84 
 85         /* Construct the name, using parent nodes if necessary to ensure uniqueness */
 86         while (node->parent) {
 87                 /*
 88                  * If the address can be translated, then that is as much
 89                  * uniqueness as we need. Make it the first component and return
 90                  */
 91                 reg = of_get_property(node, "reg", NULL);
 92                 if (reg && (addr = of_translate_address(node, reg)) != OF_BAD_ADDR) {
 93                         dev_set_name(dev, dev_name(dev) ? "%llx.%s:%s" : "%llx.%s",
 94                                      (unsigned long long)addr, node->name,
 95                                      dev_name(dev));
 96                         return;
 97                 }
 98 
 99                 /* format arguments only used if dev_name() resolves to NULL */
100                 dev_set_name(dev, dev_name(dev) ? "%s:%s" : "%s",
101                              strrchr(node->full_name, '/') + 1, dev_name(dev));
102                 node = node->parent;
103         }
104 }
105 
106 /**
107  * of_device_alloc - Allocate and initialize an of_device
108  * @np: device node to assign to device
109  * @bus_id: Name to assign to the device.  May be null to use default name.
110  * @parent: Parent device.
111  */
112 struct platform_device *of_device_alloc(struct device_node *np,
113                                   const char *bus_id,
114                                   struct device *parent)
115 {
116         struct platform_device *dev;
117         int rc, i, num_reg = 0, num_irq;
118         struct resource *res, temp_res;
119 
120         dev = platform_device_alloc("", -1);
121         if (!dev)
122                 return NULL;
123 
124         /* count the io and irq resources */
125         while (of_address_to_resource(np, num_reg, &temp_res) == 0)
126                 num_reg++;
127         num_irq = of_irq_count(np);
128 
129         /* Populate the resource table */
130         if (num_irq || num_reg) {
131                 res = kzalloc(sizeof(*res) * (num_irq + num_reg), GFP_KERNEL);
132                 if (!res) {
133                         platform_device_put(dev);
134                         return NULL;
135                 }
136 
137                 dev->num_resources = num_reg + num_irq;
138                 dev->resource = res;
139                 for (i = 0; i < num_reg; i++, res++) {
140                         rc = of_address_to_resource(np, i, res);
141                         WARN_ON(rc);
142                 }
143                 if (of_irq_to_resource_table(np, res, num_irq) != num_irq)
144                         pr_debug("not all legacy IRQ resources mapped for %s\n",
145                                  np->name);
146         }
147 
148         dev->dev.of_node = of_node_get(np);
149         dev->dev.fwnode = &np->fwnode;
150         dev->dev.parent = parent ? : &platform_bus;
151 
152         if (bus_id)
153                 dev_set_name(&dev->dev, "%s", bus_id);
154         else
155                 of_device_make_bus_id(&dev->dev);
156 
157         return dev;
158 }
159 EXPORT_SYMBOL(of_device_alloc);
160 
161 static void of_dma_deconfigure(struct device *dev)
162 {
163         arch_teardown_dma_ops(dev);
164 }
165 
166 /**
167  * of_platform_device_create_pdata - Alloc, initialize and register an of_device
168  * @np: pointer to node to create device for
169  * @bus_id: name to assign device