acpi_platform.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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/dma-mapping.h>
  19. #include <linux/pci.h>
  20. #include <linux/platform_device.h>
  21. #include "internal.h"
  22. ACPI_MODULE_NAME("platform");
  23. static const struct acpi_device_id forbidden_id_list[] = {
  24. {"PNP0000", 0}, /* PIC */
  25. {"PNP0100", 0}, /* Timer */
  26. {"PNP0200", 0}, /* AT DMA Controller */
  27. {"ACPI0009", 0}, /* IOxAPIC */
  28. {"ACPI000A", 0}, /* IOAPIC */
  29. {"SMB0001", 0}, /* ACPI SMBUS virtual device */
  30. {"", 0},
  31. };
  32. static void acpi_platform_fill_resource(struct acpi_device *adev,
  33. const struct resource *src, struct resource *dest)
  34. {
  35. struct device *parent;
  36. *dest = *src;
  37. /*
  38. * If the device has parent we need to take its resources into
  39. * account as well because this device might consume part of those.
  40. */
  41. parent = acpi_get_first_physical_node(adev->parent);
  42. if (parent && dev_is_pci(parent))
  43. dest->parent = pci_find_resource(to_pci_dev(parent), dest);
  44. }
  45. /**
  46. * acpi_create_platform_device - Create platform device for ACPI device node
  47. * @adev: ACPI device node to create a platform device for.
  48. * @properties: Optional collection of build-in properties.
  49. *
  50. * Check if the given @adev can be represented as a platform device and, if
  51. * that's the case, create and register a platform device, populate its common
  52. * resources and returns a pointer to it. Otherwise, return %NULL.
  53. *
  54. * Name of the platform device will be the same as @adev's.
  55. */
  56. struct platform_device *acpi_create_platform_device(struct acpi_device *adev,
  57. struct property_entry *properties)
  58. {
  59. struct platform_device *pdev = NULL;
  60. struct platform_device_info pdevinfo;
  61. struct resource_entry *rentry;
  62. struct list_head resource_list;
  63. struct resource *resources = NULL;
  64. int count;
  65. /* If the ACPI node already has a physical device attached, skip it. */
  66. if (adev->physical_node_count)
  67. return NULL;
  68. if (!acpi_match_device_ids(adev, forbidden_id_list))
  69. return ERR_PTR(-EINVAL);
  70. INIT_LIST_HEAD(&resource_list);
  71. count = acpi_dev_get_resources(adev, &resource_list, NULL, NULL);
  72. if (count < 0) {
  73. return NULL;
  74. } else if (count > 0) {
  75. resources = kcalloc(count, sizeof(struct resource),
  76. GFP_KERNEL);
  77. if (!resources) {
  78. dev_err(&adev->dev, "No memory for resources\n");
  79. acpi_dev_free_resource_list(&resource_list);
  80. return ERR_PTR(-ENOMEM);
  81. }
  82. count = 0;
  83. list_for_each_entry(rentry, &resource_list, node)
  84. acpi_platform_fill_resource(adev, rentry->res,
  85. &resources[count++]);
  86. acpi_dev_free_resource_list(&resource_list);
  87. }
  88. memset(&pdevinfo, 0, sizeof(pdevinfo));
  89. /*
  90. * If the ACPI node has a parent and that parent has a physical device
  91. * attached to it, that physical device should be the parent of the
  92. * platform device we are about to create.
  93. */
  94. pdevinfo.parent = adev->parent ?
  95. acpi_get_first_physical_node(adev->parent) : NULL;
  96. pdevinfo.name = dev_name(&adev->dev);
  97. pdevinfo.id = -1;
  98. pdevinfo.res = resources;
  99. pdevinfo.num_res = count;
  100. pdevinfo.fwnode = acpi_fwnode_handle(adev);
  101. pdevinfo.properties = properties;
  102. if (acpi_dma_supported(adev))
  103. pdevinfo.dma_mask = DMA_BIT_MASK(32);
  104. else
  105. pdevinfo.dma_mask = 0;
  106. pdev = platform_device_register_full(&pdevinfo);
  107. if (IS_ERR(pdev))
  108. dev_err(&adev->dev, "platform device creation failed: %ld\n",
  109. PTR_ERR(pdev));
  110. else {
  111. set_dev_node(&pdev->dev, acpi_get_node(adev->handle));
  112. dev_dbg(&adev->dev, "created platform device %s\n",
  113. dev_name(&pdev->dev));
  114. }
  115. kfree(resources);
  116. return pdev;
  117. }
  118. EXPORT_SYMBOL_GPL(acpi_create_platform_device);