acpi_platform.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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. ACPI_MODULE_NAME("platform");
  19. /**
  20. * acpi_create_platform_device - Create platform device for ACPI device node
  21. * @adev: ACPI device node to create a platform device for.
  22. *
  23. * Check if the given @adev can be represented as a platform device and, if
  24. * that's the case, create and register a platform device, populate its common
  25. * resources and returns a pointer to it. Otherwise, return %NULL.
  26. *
  27. * The platform device's name will be taken from the @adev's _HID and _UID.
  28. */
  29. struct platform_device *acpi_create_platform_device(struct acpi_device *adev)
  30. {
  31. struct platform_device *pdev = NULL;
  32. struct acpi_device *acpi_parent;
  33. struct device *parent = NULL;
  34. struct resource_list_entry *rentry;
  35. struct list_head resource_list;
  36. struct resource *resources;
  37. int count;
  38. /* If the ACPI node already has a physical device attached, skip it. */
  39. if (adev->physical_node_count)
  40. return NULL;
  41. INIT_LIST_HEAD(&resource_list);
  42. count = acpi_dev_get_resources(adev, &resource_list, NULL, NULL);
  43. if (count <= 0)
  44. return NULL;
  45. resources = kmalloc(count * sizeof(struct resource), GFP_KERNEL);
  46. if (!resources) {
  47. dev_err(&adev->dev, "No memory for resources\n");
  48. acpi_dev_free_resource_list(&resource_list);
  49. return NULL;
  50. }
  51. count = 0;
  52. list_for_each_entry(rentry, &resource_list, node)
  53. resources[count++] = rentry->res;
  54. acpi_dev_free_resource_list(&resource_list);
  55. /*
  56. * If the ACPI node has a parent and that parent has a physical device
  57. * attached to it, that physical device should be the parent of the
  58. * platform device we are about to create.
  59. */
  60. acpi_parent = adev->parent;
  61. if (acpi_parent) {
  62. struct acpi_device_physical_node *entry;
  63. struct list_head *list;
  64. mutex_lock(&acpi_parent->physical_node_lock);
  65. list = &acpi_parent->physical_node_list;
  66. if (!list_empty(list)) {
  67. entry = list_first_entry(list,
  68. struct acpi_device_physical_node,
  69. node);
  70. parent = entry->dev;
  71. }
  72. mutex_unlock(&acpi_parent->physical_node_lock);
  73. }
  74. pdev = platform_device_register_resndata(parent, dev_name(&adev->dev),
  75. -1, resources, count, NULL, 0);
  76. if (IS_ERR(pdev)) {
  77. dev_err(&adev->dev, "platform device creation failed: %ld\n",
  78. PTR_ERR(pdev));
  79. pdev = NULL;
  80. } else {
  81. dev_dbg(&adev->dev, "created platform device %s\n",
  82. dev_name(&pdev->dev));
  83. }
  84. kfree(resources);
  85. return pdev;
  86. }
  87. static acpi_status acpi_platform_match(acpi_handle handle, u32 depth,
  88. void *data, void **return_value)
  89. {
  90. struct platform_device *pdev = data;
  91. struct acpi_device *adev;
  92. acpi_status status;
  93. status = acpi_bus_get_device(handle, &adev);
  94. if (ACPI_FAILURE(status))
  95. return status;
  96. /* Skip ACPI devices that have physical device attached */
  97. if (adev->physical_node_count)
  98. return AE_OK;
  99. if (!strcmp(dev_name(&pdev->dev), dev_name(&adev->dev))) {
  100. *(acpi_handle *)return_value = handle;
  101. return AE_CTRL_TERMINATE;
  102. }
  103. return AE_OK;
  104. }
  105. static int acpi_platform_find_device(struct device *dev, acpi_handle *handle)
  106. {
  107. struct platform_device *pdev = to_platform_device(dev);
  108. char *name, *tmp, *hid;
  109. /*
  110. * The platform device is named using the ACPI device name
  111. * _HID:INSTANCE so we strip the INSTANCE out in order to find the
  112. * correct device using its _HID.
  113. */
  114. name = kstrdup(dev_name(dev), GFP_KERNEL);
  115. if (!name)
  116. return -ENOMEM;
  117. tmp = name;
  118. hid = strsep(&tmp, ":");
  119. if (!hid) {
  120. kfree(name);
  121. return -ENODEV;
  122. }
  123. *handle = NULL;
  124. acpi_get_devices(hid, acpi_platform_match, pdev, handle);
  125. kfree(name);
  126. return *handle ? 0 : -ENODEV;
  127. }
  128. static struct acpi_bus_type acpi_platform_bus = {
  129. .bus = &platform_bus_type,
  130. .find_device = acpi_platform_find_device,
  131. };
  132. static int __init acpi_platform_init(void)
  133. {
  134. return register_acpi_bus_type(&acpi_platform_bus);
  135. }
  136. arch_initcall(acpi_platform_init);