acpi_platform.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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/err.h>
  16. #include <linux/kernel.h>
  17. #include <linux/module.h>
  18. #include <linux/platform_device.h>
  19. #include "internal.h"
  20. ACPI_MODULE_NAME("platform");
  21. /*
  22. * The following ACPI IDs are known to be suitable for representing as
  23. * platform devices.
  24. */
  25. static const struct acpi_device_id acpi_platform_device_ids[] = {
  26. { "PNP0D40" },
  27. { "ACPI0003" },
  28. { "VPC2004" },
  29. { }
  30. };
  31. /**
  32. * acpi_create_platform_device - Create platform device for ACPI device node
  33. * @adev: ACPI device node to create a platform device for.
  34. * @id: ACPI device ID used to match @adev.
  35. *
  36. * Check if the given @adev can be represented as a platform device and, if
  37. * that's the case, create and register a platform device, populate its common
  38. * resources and returns a pointer to it. Otherwise, return %NULL.
  39. *
  40. * Name of the platform device will be the same as @adev's.
  41. */
  42. int acpi_create_platform_device(struct acpi_device *adev,
  43. const struct acpi_device_id *id)
  44. {
  45. struct platform_device *pdev = NULL;
  46. struct acpi_device *acpi_parent;
  47. struct platform_device_info pdevinfo;
  48. struct resource_list_entry *rentry;
  49. struct list_head resource_list;
  50. struct resource *resources = NULL;
  51. int count;
  52. /* If the ACPI node already has a physical device attached, skip it. */
  53. if (adev->physical_node_count)
  54. return 0;
  55. INIT_LIST_HEAD(&resource_list);
  56. count = acpi_dev_get_resources(adev, &resource_list, NULL, NULL);
  57. if (count < 0) {
  58. return 0;
  59. } else if (count > 0) {
  60. resources = kmalloc(count * sizeof(struct resource),
  61. GFP_KERNEL);
  62. if (!resources) {
  63. dev_err(&adev->dev, "No memory for resources\n");
  64. acpi_dev_free_resource_list(&resource_list);
  65. return -ENOMEM;
  66. }
  67. count = 0;
  68. list_for_each_entry(rentry, &resource_list, node)
  69. resources[count++] = rentry->res;
  70. acpi_dev_free_resource_list(&resource_list);
  71. }
  72. memset(&pdevinfo, 0, sizeof(pdevinfo));
  73. /*
  74. * If the ACPI node has a parent and that parent has a physical device
  75. * attached to it, that physical device should be the parent of the
  76. * platform device we are about to create.
  77. */
  78. pdevinfo.parent = NULL;
  79. acpi_parent = adev->parent;
  80. if (acpi_parent) {
  81. struct acpi_device_physical_node *entry;
  82. struct list_head *list;
  83. mutex_lock(&acpi_parent->physical_node_lock);
  84. list = &acpi_parent->physical_node_list;
  85. if (!list_empty(list)) {
  86. entry = list_first_entry(list,
  87. struct acpi_device_physical_node,
  88. node);
  89. pdevinfo.parent = entry->dev;
  90. }
  91. mutex_unlock(&acpi_parent->physical_node_lock);
  92. }
  93. pdevinfo.name = dev_name(&adev->dev);
  94. pdevinfo.id = -1;
  95. pdevinfo.res = resources;
  96. pdevinfo.num_res = count;
  97. pdevinfo.acpi_node.handle = adev->handle;
  98. pdev = platform_device_register_full(&pdevinfo);
  99. if (IS_ERR(pdev)) {
  100. dev_err(&adev->dev, "platform device creation failed: %ld\n",
  101. PTR_ERR(pdev));
  102. pdev = NULL;
  103. } else {
  104. dev_dbg(&adev->dev, "created platform device %s\n",
  105. dev_name(&pdev->dev));
  106. }
  107. kfree(resources);
  108. return 1;
  109. }
  110. static struct acpi_scan_handler platform_handler = {
  111. .ids = acpi_platform_device_ids,
  112. .attach = acpi_create_platform_device,
  113. };
  114. void __init acpi_platform_init(void)
  115. {
  116. acpi_scan_add_handler(&platform_handler);
  117. }