of_device.c 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. #include <linux/string.h>
  2. #include <linux/kernel.h>
  3. #include <linux/of.h>
  4. #include <linux/init.h>
  5. #include <linux/module.h>
  6. #include <linux/mod_devicetable.h>
  7. #include <linux/slab.h>
  8. #include <linux/of_device.h>
  9. #include <linux/errno.h>
  10. void of_device_make_bus_id(struct of_device *dev)
  11. {
  12. static atomic_t bus_no_reg_magic;
  13. struct device_node *node = dev->dev.of_node;
  14. const u32 *reg;
  15. u64 addr;
  16. int magic;
  17. /*
  18. * For MMIO, get the physical address
  19. */
  20. reg = of_get_property(node, "reg", NULL);
  21. if (reg) {
  22. addr = of_translate_address(node, reg);
  23. if (addr != OF_BAD_ADDR) {
  24. dev_set_name(&dev->dev, "%llx.%s",
  25. (unsigned long long)addr, node->name);
  26. return;
  27. }
  28. }
  29. /*
  30. * No BusID, use the node name and add a globally incremented
  31. * counter (and pray...)
  32. */
  33. magic = atomic_add_return(1, &bus_no_reg_magic);
  34. dev_set_name(&dev->dev, "%s.%d", node->name, magic - 1);
  35. }
  36. EXPORT_SYMBOL(of_device_make_bus_id);
  37. struct of_device *of_device_alloc(struct device_node *np,
  38. const char *bus_id,
  39. struct device *parent)
  40. {
  41. struct of_device *dev;
  42. dev = kzalloc(sizeof(*dev), GFP_KERNEL);
  43. if (!dev)
  44. return NULL;
  45. dev->dev.of_node = of_node_get(np);
  46. dev->dev.dma_mask = &dev->archdata.dma_mask;
  47. dev->dev.parent = parent;
  48. dev->dev.release = of_release_dev;
  49. if (bus_id)
  50. dev_set_name(&dev->dev, bus_id);
  51. else
  52. of_device_make_bus_id(dev);
  53. return dev;
  54. }
  55. EXPORT_SYMBOL(of_device_alloc);