acpi_platform.c 3.5 KB

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