OSDN Git Service

Merge "msm: ispif: fix the OOB read issue"
[sagit-ice-cold/kernel_xiaomi_msm8998.git] / 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 #include <linux/errno.h>
15 #include <linux/module.h>
16 #include <linux/amba/bus.h>
17 #include <linux/device.h>
18 #include <linux/dma-mapping.h>
19 #include <linux/slab.h>
20 #include <linux/of_address.h>
21 #include <linux/of_device.h>
22 #include <linux/of_irq.h>
23 #include <linux/of_platform.h>
24 #include <linux/of_reserved_mem.h>
25 #include <linux/platform_device.h>
26
27 const struct of_device_id of_default_bus_match_table[] = {
28         { .compatible = "simple-bus", },
29         { .compatible = "simple-mfd", },
30 #ifdef CONFIG_ARM_AMBA
31         { .compatible = "arm,amba-bus", },
32 #endif /* CONFIG_ARM_AMBA */
33         {} /* Empty terminated list */
34 };
35
36 static int of_dev_node_match(struct device *dev, void *data)
37 {
38         return dev->of_node == data;
39 }
40
41 /**
42  * of_find_device_by_node - Find the platform_device associated with a node
43  * @np: Pointer to device tree node
44  *
45  * Returns platform_device pointer, or NULL if not found
46  */
47 struct platform_device *of_find_device_by_node(struct device_node *np)
48 {
49         struct device *dev;
50
51         dev = bus_find_device(&platform_bus_type, NULL, np, of_dev_node_match);
52         return dev ? to_platform_device(dev) : NULL;
53 }
54 EXPORT_SYMBOL(of_find_device_by_node);
55
56 #ifdef CONFIG_OF_ADDRESS
57 /*
58  * The following routines scan a subtree and registers a device for
59  * each applicable node.
60  *
61  * Note: sparc doesn't use these routines because it has a different
62  * mechanism for creating devices from device tree nodes.
63  */
64
65 /**
66  * of_device_make_bus_id - Use the device node data to assign a unique name
67  * @dev: pointer to device structure that is linked to a device tree node
68  *
69  * This routine will first try using the translated bus address to
70  * derive a unique name. If it cannot, then it will prepend names from
71  * parent nodes until a unique name can be derived.
72  */
73 void of_device_make_bus_id(struct device *dev)
74 {
75         struct device_node *node = dev->of_node;
76         const __be32 *reg;
77         u64 addr;
78
79         /* Construct the name, using parent nodes if necessary to ensure uniqueness */
80         while (node->parent) {
81                 /*
82                  * If the address can be translated, then that is as much
83                  * uniqueness as we need. Make it the first component and return
84                  */
85                 reg = of_get_property(node, "reg", NULL);
86                 if (reg && (addr = of_translate_address(node, reg)) != OF_BAD_ADDR) {
87                         dev_set_name(dev, dev_name(dev) ? "%llx.%s:%s" : "%llx.%s",
88                                      (unsigned long long)addr, node->name,
89                                      dev_name(dev));
90                         return;
91                 }
92
93                 /* format arguments only used if dev_name() resolves to NULL */
94                 dev_set_name(dev, dev_name(dev) ? "%s:%s" : "%s",
95                              strrchr(node->full_name, '/') + 1, dev_name(dev));
96                 node = node->parent;
97         }
98 }
99
100 /**
101  * of_device_alloc - Allocate and initialize an of_device
102  * @np: device node to assign to device
103  * @bus_id: Name to assign to the device.  May be null to use default name.
104  * @parent: Parent device.
105  */
106 struct platform_device *of_device_alloc(struct device_node *np,
107                                   const char *bus_id,
108                                   struct device *parent)
109 {
110         struct platform_device *dev;
111         int rc, i, num_reg = 0, num_irq;
112         struct resource *res, temp_res;
113
114         dev = platform_device_alloc("", -1);
115         if (!dev)
116                 return NULL;
117
118         /* count the io and irq resources */
119         while (of_address_to_resource(np, num_reg, &temp_res) == 0)
120                 num_reg++;
121         num_irq = of_irq_count(np);
122
123         /* Populate the resource table */
124         if (num_irq || num_reg) {
125                 res = kzalloc(sizeof(*res) * (num_irq + num_reg), GFP_KERNEL);
126                 if (!res) {
127                         platform_device_put(dev);
128                         return NULL;
129                 }
130
131                 dev->num_resources = num_reg + num_irq;
132                 dev->resource = res;
133                 for (i = 0; i < num_reg; i++, res++) {
134                         rc = of_address_to_resource(np, i, res);
135                         WARN_ON(rc);
136                 }
137                 if (of_irq_to_resource_table(np, res, num_irq) != num_irq)
138                         pr_debug("not all legacy IRQ resources mapped for %s\n",
139                                  np->name);
140         }
141
142         dev->dev.of_node = of_node_get(np);
143         dev->dev.parent = parent;
144
145         if (bus_id)
146                 dev_set_name(&dev->dev, "%s", bus_id);
147         else
148                 of_device_make_bus_id(&dev->dev);
149
150         return dev;
151 }
152 EXPORT_SYMBOL(of_device_alloc);
153
154 static void of_dma_deconfigure(struct device *dev)
155 {
156         arch_teardown_dma_ops(dev);
157 }
158
159 /**
160  * of_platform_device_create_pdata - Alloc, initialize and register an of_device
161  * @np: pointer to node to create device for
162  * @bus_id: name to assign device
163  * @platform_data: pointer to populate platform_data pointer with
164  * @parent: Linux device model parent device.
165  *
166  * Returns pointer to created platform device, or NULL if a device was not
167  * registered.  Unavailable devices will not get registered.
168  */
169 static struct platform_device *of_platform_device_create_pdata(
170                                         struct device_node *np,
171                                         const char *bus_id,
172                                         void *platform_data,
173                                         struct device *parent)
174 {
175         struct platform_device *dev;
176
177         if (!of_device_is_available(np) ||
178             of_node_test_and_set_flag(np, OF_POPULATED))
179                 return NULL;
180
181         dev = of_device_alloc(np, bus_id, parent);
182         if (!dev)
183                 goto err_clear_flag;
184
185         dev->dev.bus = &platform_bus_type;
186         dev->dev.platform_data = platform_data;
187         of_dma_configure(&dev->dev, dev->dev.of_node);
188         of_msi_configure(&dev->dev, dev->dev.of_node);
189         of_reserved_mem_device_init(&dev->dev);
190
191         if (of_device_add(dev) != 0) {
192                 of_dma_deconfigure(&dev->dev);
193                 platform_device_put(dev);
194                 goto err_clear_flag;
195         }
196
197         return dev;
198
199 err_clear_flag:
200         of_node_clear_flag(np, OF_POPULATED);
201         return NULL;
202 }
203
204 /**
205  * of_platform_device_create - Alloc, initialize and register an of_device
206  * @np: pointer to node to create device for
207  * @bus_id: name to assign device
208  * @parent: Linux device model parent device.
209  *
210  * Returns pointer to created platform device, or NULL if a device was not
211  * registered.  Unavailable devices will not get registered.
212  */
213 struct platform_device *of_platform_device_create(struct device_node *np,
214                                             const char *bus_id,
215                                             struct device *parent)
216 {
217         return of_platform_device_create_pdata(np, bus_id, NULL, parent);
218 }
219 EXPORT_SYMBOL(of_platform_device_create);
220
221 #ifdef CONFIG_ARM_AMBA
222 static struct amba_device *of_amba_device_create(struct device_node *node,
223                                                  const char *bus_id,
224                                                  void *platform_data,
225                                                  struct device *parent)
226 {
227         struct amba_device *dev;
228         const void *prop;
229         int i, ret;
230
231         pr_debug("Creating amba device %s\n", node->full_name);
232
233         if (!of_device_is_available(node) ||
234             of_node_test_and_set_flag(node, OF_POPULATED))
235                 return NULL;
236
237         dev = amba_device_alloc(NULL, 0, 0);
238         if (!dev) {
239                 pr_err("%s(): amba_device_alloc() failed for %s\n",
240                        __func__, node->full_name);
241                 goto err_clear_flag;
242         }
243
244         /* setup generic device info */
245         dev->dev.of_node = of_node_get(node);
246         dev->dev.parent = parent;
247         dev->dev.platform_data = platform_data;
248         if (bus_id)
249                 dev_set_name(&dev->dev, "%s", bus_id);
250         else
251                 of_device_make_bus_id(&dev->dev);
252         of_dma_configure(&dev->dev, dev->dev.of_node);
253
254         /* Allow the HW Peripheral ID to be overridden */
255         prop = of_get_property(node, "arm,primecell-periphid", NULL);
256         if (prop)
257                 dev->periphid = of_read_ulong(prop, 1);
258
259         /* Decode the IRQs and address ranges */
260         for (i = 0; i < AMBA_NR_IRQS; i++)
261                 dev->irq[i] = irq_of_parse_and_map(node, i);
262
263         ret = of_address_to_resource(node, 0, &dev->res);
264         if (ret) {
265                 pr_err("%s(): of_address_to_resource() failed (%d) for %s\n",
266                        __func__, ret, node->full_name);
267                 goto err_free;
268         }
269
270         ret = amba_device_add(dev, &iomem_resource);
271         if (ret) {
272                 pr_err("%s(): amba_device_add() failed (%d) for %s\n",
273                        __func__, ret, node->full_name);
274                 goto err_free;
275         }
276
277         return dev;
278
279 err_free:
280         amba_device_put(dev);
281 err_clear_flag:
282         of_node_clear_flag(node, OF_POPULATED);
283         return NULL;
284 }
285 #else /* CONFIG_ARM_AMBA */
286 static struct amba_device *of_amba_device_create(struct device_node *node,
287                                                  const char *bus_id,
288                                                  void *platform_data,
289                                                  struct device *parent)
290 {
291         return NULL;
292 }
293 #endif /* CONFIG_ARM_AMBA */
294
295 /**
296  * of_devname_lookup() - Given a device node, lookup the preferred Linux name
297  */
298 static const struct of_dev_auxdata *of_dev_lookup(const struct of_dev_auxdata *lookup,
299                                  struct device_node *np)
300 {
301         struct resource res;
302
303         if (!lookup)
304                 return NULL;
305
306         for(; lookup->compatible != NULL; lookup++) {
307                 if (!of_device_is_compatible(np, lookup->compatible))
308                         continue;
309                 if (!of_address_to_resource(np, 0, &res))
310                         if (res.start != lookup->phys_addr)
311                                 continue;
312                 pr_debug("%s: devname=%s\n", np->full_name, lookup->name);
313                 return lookup;
314         }
315
316         return NULL;
317 }
318
319 /**
320  * of_platform_bus_create() - Create a device for a node and its children.
321  * @bus: device node of the bus to instantiate
322  * @matches: match table for bus nodes
323  * @lookup: auxdata table for matching id and platform_data with device nodes
324  * @parent: parent for new device, or NULL for top level.
325  * @strict: require compatible property
326  *
327  * Creates a platform_device for the provided device_node, and optionally
328  * recursively create devices for all the child nodes.
329  */
330 static int of_platform_bus_create(struct device_node *bus,
331                                   const struct of_device_id *matches,
332                                   const struct of_dev_auxdata *lookup,
333                                   struct device *parent, bool strict)
334 {
335         const struct of_dev_auxdata *auxdata;
336         struct device_node *child;
337         struct platform_device *dev;
338         const char *bus_id = NULL;
339         void *platform_data = NULL;
340         int rc = 0;
341
342         /* Make sure it has a compatible property */
343         if (strict && (!of_get_property(bus, "compatible", NULL))) {
344                 pr_debug("%s() - skipping %s, no compatible prop\n",
345                          __func__, bus->full_name);
346                 return 0;
347         }
348
349         auxdata = of_dev_lookup(lookup, bus);
350         if (auxdata) {
351                 bus_id = auxdata->name;
352                 platform_data = auxdata->platform_data;
353         }
354
355         if (of_device_is_compatible(bus, "arm,primecell")) {
356                 /*
357                  * Don't return an error here to keep compatibility with older
358                  * device tree files.
359                  */
360                 of_amba_device_create(bus, bus_id, platform_data, parent);
361                 return 0;
362         }
363
364         dev = of_platform_device_create_pdata(bus, bus_id, platform_data, parent);
365         if (!dev || !of_match_node(matches, bus))
366                 return 0;
367
368         for_each_child_of_node(bus, child) {
369                 pr_debug("   create child: %s\n", child->full_name);
370                 rc = of_platform_bus_create(child, matches, lookup, &dev->dev, strict);
371                 if (rc) {
372                         of_node_put(child);
373                         break;
374                 }
375         }
376         of_node_set_flag(bus, OF_POPULATED_BUS);
377         return rc;
378 }
379
380 /**
381  * of_platform_bus_probe() - Probe the device-tree for platform buses
382  * @root: parent of the first level to probe or NULL for the root of the tree
383  * @matches: match table for bus nodes
384  * @parent: parent to hook devices from, NULL for toplevel
385  *
386  * Note that children of the provided root are not instantiated as devices
387  * unless the specified root itself matches the bus list and is not NULL.
388  */
389 int of_platform_bus_probe(struct device_node *root,
390                           const struct of_device_id *matches,
391                           struct device *parent)
392 {
393         struct device_node *child;
394         int rc = 0;
395
396         root = root ? of_node_get(root) : of_find_node_by_path("/");
397         if (!root)
398                 return -EINVAL;
399
400         pr_debug("of_platform_bus_probe()\n");
401         pr_debug(" starting at: %s\n", root->full_name);
402
403         /* Do a self check of bus type, if there's a match, create children */
404         if (of_match_node(matches, root)) {
405                 rc = of_platform_bus_create(root, matches, NULL, parent, false);
406         } else for_each_child_of_node(root, child) {
407                 if (!of_match_node(matches, child))
408                         continue;
409                 rc = of_platform_bus_create(child, matches, NULL, parent, false);
410                 if (rc) {
411                         of_node_put(child);
412                         break;
413                 }
414         }
415
416         of_node_put(root);
417         return rc;
418 }
419 EXPORT_SYMBOL(of_platform_bus_probe);
420
421 /**
422  * of_platform_populate() - Populate platform_devices from device tree data
423  * @root: parent of the first level to probe or NULL for the root of the tree
424  * @matches: match table, NULL to use the default
425  * @lookup: auxdata table for matching id and platform_data with device nodes
426  * @parent: parent to hook devices from, NULL for toplevel
427  *
428  * Similar to of_platform_bus_probe(), this function walks the device tree
429  * and creates devices from nodes.  It differs in that it follows the modern
430  * convention of requiring all device nodes to have a 'compatible' property,
431  * and it is suitable for creating devices which are children of the root
432  * node (of_platform_bus_probe will only create children of the root which
433  * are selected by the @matches argument).
434  *
435  * New board support should be using this function instead of
436  * of_platform_bus_probe().
437  *
438  * Returns 0 on success, < 0 on failure.
439  */
440 int of_platform_populate(struct device_node *root,
441                         const struct of_device_id *matches,
442                         const struct of_dev_auxdata *lookup,
443                         struct device *parent)
444 {
445         struct device_node *child;
446         int rc = 0;
447
448         root = root ? of_node_get(root) : of_find_node_by_path("/");
449         if (!root)
450                 return -EINVAL;
451
452         for_each_child_of_node(root, child) {
453                 rc = of_platform_bus_create(child, matches, lookup, parent, true);
454                 if (rc) {
455                         of_node_put(child);
456                         break;
457                 }
458         }
459         of_node_set_flag(root, OF_POPULATED_BUS);
460
461         of_node_put(root);
462         return rc;
463 }
464 EXPORT_SYMBOL_GPL(of_platform_populate);
465
466 int of_platform_default_populate(struct device_node *root,
467                                  const struct of_dev_auxdata *lookup,
468                                  struct device *parent)
469 {
470         return of_platform_populate(root, of_default_bus_match_table, lookup,
471                                     parent);
472 }
473 EXPORT_SYMBOL_GPL(of_platform_default_populate);
474
475 static int of_platform_device_destroy(struct device *dev, void *data)
476 {
477         /* Do not touch devices not populated from the device tree */
478         if (!dev->of_node || !of_node_check_flag(dev->of_node, OF_POPULATED))
479                 return 0;
480
481         /* Recurse for any nodes that were treated as busses */
482         if (of_node_check_flag(dev->of_node, OF_POPULATED_BUS))
483                 device_for_each_child(dev, NULL, of_platform_device_destroy);
484
485         if (dev->bus == &platform_bus_type)
486                 platform_device_unregister(to_platform_device(dev));
487 #ifdef CONFIG_ARM_AMBA
488         else if (dev->bus == &amba_bustype)
489                 amba_device_unregister(to_amba_device(dev));
490 #endif
491
492         of_dma_deconfigure(dev);
493         of_node_clear_flag(dev->of_node, OF_POPULATED);
494         of_node_clear_flag(dev->of_node, OF_POPULATED_BUS);
495         return 0;
496 }
497
498 /**
499  * of_platform_depopulate() - Remove devices populated from device tree
500  * @parent: device which children will be removed
501  *
502  * Complementary to of_platform_populate(), this function removes children
503  * of the given device (and, recurrently, their children) that have been
504  * created from their respective device tree nodes (and only those,
505  * leaving others - eg. manually created - unharmed).
506  *
507  * Returns 0 when all children devices have been removed or
508  * -EBUSY when some children remained.
509  */
510 void of_platform_depopulate(struct device *parent)
511 {
512         if (parent->of_node && of_node_check_flag(parent->of_node, OF_POPULATED_BUS)) {
513                 device_for_each_child(parent, NULL, of_platform_device_destroy);
514                 of_node_clear_flag(parent->of_node, OF_POPULATED_BUS);
515         }
516 }
517 EXPORT_SYMBOL_GPL(of_platform_depopulate);
518
519 #ifdef CONFIG_OF_DYNAMIC
520 static int of_platform_notify(struct notifier_block *nb,
521                                 unsigned long action, void *arg)
522 {
523         struct of_reconfig_data *rd = arg;
524         struct platform_device *pdev_parent, *pdev;
525         bool children_left;
526
527         switch (of_reconfig_get_state_change(action, rd)) {
528         case OF_RECONFIG_CHANGE_ADD:
529                 /* verify that the parent is a bus */
530                 if (!of_node_check_flag(rd->dn->parent, OF_POPULATED_BUS))
531                         return NOTIFY_OK;       /* not for us */
532
533                 /* already populated? (driver using of_populate manually) */
534                 if (of_node_check_flag(rd->dn, OF_POPULATED))
535                         return NOTIFY_OK;
536
537                 /* pdev_parent may be NULL when no bus platform device */
538                 pdev_parent = of_find_device_by_node(rd->dn->parent);
539                 pdev = of_platform_device_create(rd->dn, NULL,
540                                 pdev_parent ? &pdev_parent->dev : NULL);
541                 of_dev_put(pdev_parent);
542
543                 if (pdev == NULL) {
544                         pr_err("%s: failed to create for '%s'\n",
545                                         __func__, rd->dn->full_name);
546                         /* of_platform_device_create tosses the error code */
547                         return notifier_from_errno(-EINVAL);
548                 }
549                 break;
550
551         case OF_RECONFIG_CHANGE_REMOVE:
552
553                 /* already depopulated? */
554                 if (!of_node_check_flag(rd->dn, OF_POPULATED))
555                         return NOTIFY_OK;
556
557                 /* find our device by node */
558                 pdev = of_find_device_by_node(rd->dn);
559                 if (pdev == NULL)
560                         return NOTIFY_OK;       /* no? not meant for us */
561
562                 /* unregister takes one ref away */
563                 of_platform_device_destroy(&pdev->dev, &children_left);
564
565                 /* and put the reference of the find */
566                 of_dev_put(pdev);
567                 break;
568         }
569
570         return NOTIFY_OK;
571 }
572
573 static struct notifier_block platform_of_notifier = {
574         .notifier_call = of_platform_notify,
575 };
576
577 void of_platform_register_reconfig_notifier(void)
578 {
579         WARN_ON(of_reconfig_notifier_register(&platform_of_notifier));
580 }
581 #endif /* CONFIG_OF_DYNAMIC */
582
583 #endif /* CONFIG_OF_ADDRESS */