acpi_platform.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /*
  2. * ACPI support for platform bus type.
  3. *
  4. * Copyright (C) 2012, Intel Corporation
  5. * Authors: Mika Westerberg <mika.westerberg@linux.intel.com>
  6. * Mathias Nyman <mathias.nyman@linux.intel.com>
  7. * Rafael J. Wysocki <rafael.j.wysocki@intel.com>
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2 as
  11. * published by the Free Software Foundation.
  12. */
  13. #include <linux/acpi.h>
  14. #include <linux/device.h>
  15. #include <linux/kernel.h>
  16. #include <linux/module.h>
  17. #include <linux/platform_device.h>
  18. #include "internal.h"
  19. ACPI_MODULE_NAME("platform");
  20. /**
  21. * acpi_create_platform_device - Create platform device for ACPI device node
  22. * @adev: ACPI device node to create a platform device for.
  23. *
  24. * Check if the given @adev can be represented as a platform device and, if
  25. * that's the case, create and register a platform device, populate its common
  26. * resources and returns a pointer to it. Otherwise, return %NULL.
  27. *
  28. * The platform device's name will be taken from the @adev's _HID and _UID.
  29. */
  30. struct platform_device *acpi_create_platform_device(struct acpi_device *adev)
  31. {
  32. struct platform_device *pdev = NULL;
  33. struct acpi_device *acpi_parent;
  34. struct platform_device_info pdevinfo;
  35. struct resource_list_entry *rentry;
  36. struct list_head resource_list;
  37. struct resource *resources;
  38. int count;
  39. /* If the ACPI node already has a physical device attached, skip it. */
  40. if (adev->physical_node_count)
  41. return NULL;
  42. INIT_LIST_HEAD(&resource_list);
  43. count = acpi_dev_get_resources(adev, &resource_list, NULL, NULL);
  44. if (count <= 0)
  45. return NULL;
  46. resources = kmalloc(count * sizeof(struct resource), GFP_KERNEL);
  47. if (!resources) {
  48. dev_err(&adev->dev, "No memory for resources\n");
  49. acpi_dev_free_resource_list(&resource_list);
  50. return NULL;
  51. }
  52. count = 0;
  53. list_for_each_entry(rentry, &resource_list, node)
  54. resources[count++] = rentry->res;
  55. acpi_dev_free_resource_list(&resource_list);
  56. memset(&pdevinfo, 0, sizeof(pdevinfo));
  57. /*
  58. * If the ACPI node has a parent and that parent has a physical device
  59. * attached to it, that physical device should be the parent of the
  60. * platform device we are about to create.
  61. */
  62. pdevinfo.parent = NULL;
  63. acpi_parent = adev->parent;
  64. if (acpi_parent) {
  65. struct acpi_device_physical_node *entry;
  66. struct list_head *list;
  67. mutex_lock(&acpi_parent->physical_node_lock);
  68. list = &acpi_parent->physical_node_list;
  69. if (!list_empty(list)) {
  70. entry = list_first_entry(list,
  71. struct acpi_device_physical_node,
  72. node);
  73. pdevinfo.parent = entry->dev;
  74. }
  75. mutex_unlock(&acpi_parent->physical_node_lock);
  76. }
  77. pdevinfo.name = dev_name(&adev->dev);
  78. pdevinfo.id = -1;
  79. pdevinfo.res = resources;
  80. pdevinfo.num_res = count;
  81. pdevinfo.acpi_node.handle = adev->handle;
  82. pdev = platform_device_register_full(&pdevinfo);
  83. if (IS_ERR(pdev)) {
  84. dev_err(&adev->dev, "platform device creation failed: %ld\n",
  85. PTR_ERR(pdev));
  86. pdev = NULL;
  87. } else {
  88. dev_dbg(&adev->dev, "created platform device %s\n",
  89. dev_name(&pdev->dev));
  90. }
  91. kfree(resources);
  92. return pdev;
  93. }