acpi_platform.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. static const struct acpi_device_id forbidden_id_list[] = {
  22. {"PNP0000", 0}, /* PIC */
  23. {"PNP0100", 0}, /* Timer */
  24. {"PNP0200", 0}, /* AT DMA Controller */
  25. {"", 0},
  26. };
  27. /**
  28. * acpi_create_platform_device - Create platform device for ACPI device node
  29. * @adev: ACPI device node to create a platform device for.
  30. *
  31. * Check if the given @adev can be represented as a platform device and, if
  32. * that's the case, create and register a platform device, populate its common
  33. * resources and returns a pointer to it. Otherwise, return %NULL.
  34. *
  35. * Name of the platform device will be the same as @adev's.
  36. */
  37. struct platform_device *acpi_create_platform_device(struct acpi_device *adev)
  38. {
  39. struct platform_device *pdev = NULL;
  40. struct acpi_device *acpi_parent;
  41. struct platform_device_info pdevinfo;
  42. struct resource_list_entry *rentry;
  43. struct list_head resource_list;
  44. struct resource *resources = NULL;
  45. int count;
  46. /* If the ACPI node already has a physical device attached, skip it. */
  47. if (adev->physical_node_count)
  48. return NULL;
  49. if (!acpi_match_device_ids(adev, forbidden_id_list))
  50. return ERR_PTR(-EINVAL);
  51. INIT_LIST_HEAD(&resource_list);
  52. count = acpi_dev_get_resources(adev, &resource_list, NULL, NULL);
  53. if (count < 0) {
  54. return NULL;
  55. } else if (count > 0) {
  56. resources = kmalloc(count * sizeof(struct resource),
  57. GFP_KERNEL);
  58. if (!resources) {
  59. dev_err(&adev->dev, "No memory for resources\n");
  60. acpi_dev_free_resource_list(&resource_list);
  61. return ERR_PTR(-ENOMEM);
  62. }
  63. count = 0;
  64. list_for_each_entry(rentry, &resource_list, node)
  65. resources[count++] = rentry->res;
  66. acpi_dev_free_resource_list(&resource_list);
  67. }
  68. memset(&pdevinfo, 0, sizeof(pdevinfo));
  69. /*
  70. * If the ACPI node has a parent and that parent has a physical device
  71. * attached to it, that physical device should be the parent of the
  72. * platform device we are about to create.
  73. */
  74. pdevinfo.parent = NULL;
  75. acpi_parent = adev->parent;
  76. if (acpi_parent) {
  77. struct acpi_device_physical_node *entry;
  78. struct list_head *list;
  79. mutex_lock(&acpi_parent->physical_node_lock);
  80. list = &acpi_parent->physical_node_list;
  81. if (!list_empty(list)) {
  82. entry = list_first_entry(list,
  83. struct acpi_device_physical_node,
  84. node);
  85. pdevinfo.parent = entry->dev;
  86. }
  87. mutex_unlock(&acpi_parent->physical_node_lock);
  88. }
  89. pdevinfo.name = dev_name(&adev->dev);
  90. pdevinfo.id = -1;
  91. pdevinfo.res = resources;
  92. pdevinfo.num_res = count;
  93. pdevinfo.acpi_node.companion = adev;
  94. pdev = platform_device_register_full(&pdevinfo);
  95. if (IS_ERR(pdev))
  96. dev_err(&adev->dev, "platform device creation failed: %ld\n",
  97. PTR_ERR(pdev));
  98. else
  99. dev_dbg(&adev->dev, "created platform device %s\n",
  100. dev_name(&pdev->dev));
  101. kfree(resources);
  102. return pdev;
  103. }