acpi_platform.c 3.6 KB

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