acpi_platform.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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. /* Flags for acpi_create_platform_device */
  22. #define ACPI_PLATFORM_CLK BIT(0)
  23. /*
  24. * The following ACPI IDs are known to be suitable for representing as
  25. * platform devices.
  26. */
  27. static const struct acpi_device_id acpi_platform_device_ids[] = {
  28. { "PNP0D40" },
  29. /* Haswell LPSS devices */
  30. { "INT33C0", ACPI_PLATFORM_CLK },
  31. { "INT33C1", ACPI_PLATFORM_CLK },
  32. { "INT33C2", ACPI_PLATFORM_CLK },
  33. { "INT33C3", ACPI_PLATFORM_CLK },
  34. { "INT33C4", ACPI_PLATFORM_CLK },
  35. { "INT33C5", ACPI_PLATFORM_CLK },
  36. { "INT33C6", ACPI_PLATFORM_CLK },
  37. { "INT33C7", ACPI_PLATFORM_CLK },
  38. { }
  39. };
  40. static int acpi_create_platform_clks(struct acpi_device *adev)
  41. {
  42. static struct platform_device *pdev;
  43. /* Create Lynxpoint LPSS clocks */
  44. if (!pdev && !strncmp(acpi_device_hid(adev), "INT33C", 6)) {
  45. pdev = platform_device_register_simple("clk-lpt", -1, NULL, 0);
  46. if (IS_ERR(pdev))
  47. return PTR_ERR(pdev);
  48. }
  49. return 0;
  50. }
  51. /**
  52. * acpi_create_platform_device - Create platform device for ACPI device node
  53. * @adev: ACPI device node to create a platform device for.
  54. * @id: ACPI device ID used to match @adev.
  55. *
  56. * Check if the given @adev can be represented as a platform device and, if
  57. * that's the case, create and register a platform device, populate its common
  58. * resources and returns a pointer to it. Otherwise, return %NULL.
  59. *
  60. * Name of the platform device will be the same as @adev's.
  61. */
  62. static int acpi_create_platform_device(struct acpi_device *adev,
  63. const struct acpi_device_id *id)
  64. {
  65. unsigned long flags = id->driver_data;
  66. struct platform_device *pdev = NULL;
  67. struct acpi_device *acpi_parent;
  68. struct platform_device_info pdevinfo;
  69. struct resource_list_entry *rentry;
  70. struct list_head resource_list;
  71. struct resource *resources;
  72. int count;
  73. if (flags & ACPI_PLATFORM_CLK) {
  74. int ret = acpi_create_platform_clks(adev);
  75. if (ret) {
  76. dev_err(&adev->dev, "failed to create clocks\n");
  77. return ret;
  78. }
  79. }
  80. /* If the ACPI node already has a physical device attached, skip it. */
  81. if (adev->physical_node_count)
  82. return 0;
  83. INIT_LIST_HEAD(&resource_list);
  84. count = acpi_dev_get_resources(adev, &resource_list, NULL, NULL);
  85. if (count <= 0)
  86. return 0;
  87. resources = kmalloc(count * sizeof(struct resource), GFP_KERNEL);
  88. if (!resources) {
  89. dev_err(&adev->dev, "No memory for resources\n");
  90. acpi_dev_free_resource_list(&resource_list);
  91. return -ENOMEM;
  92. }
  93. count = 0;
  94. list_for_each_entry(rentry, &resource_list, node)
  95. resources[count++] = rentry->res;
  96. acpi_dev_free_resource_list(&resource_list);
  97. memset(&pdevinfo, 0, sizeof(pdevinfo));
  98. /*
  99. * If the ACPI node has a parent and that parent has a physical device
  100. * attached to it, that physical device should be the parent of the
  101. * platform device we are about to create.
  102. */
  103. pdevinfo.parent = NULL;
  104. acpi_parent = adev->parent;
  105. if (acpi_parent) {
  106. struct acpi_device_physical_node *entry;
  107. struct list_head *list;
  108. mutex_lock(&acpi_parent->physical_node_lock);
  109. list = &acpi_parent->physical_node_list;
  110. if (!list_empty(list)) {
  111. entry = list_first_entry(list,
  112. struct acpi_device_physical_node,
  113. node);
  114. pdevinfo.parent = entry->dev;
  115. }
  116. mutex_unlock(&acpi_parent->physical_node_lock);
  117. }
  118. pdevinfo.name = dev_name(&adev->dev);
  119. pdevinfo.id = -1;
  120. pdevinfo.res = resources;
  121. pdevinfo.num_res = count;
  122. pdevinfo.acpi_node.handle = adev->handle;
  123. pdev = platform_device_register_full(&pdevinfo);
  124. if (IS_ERR(pdev)) {
  125. dev_err(&adev->dev, "platform device creation failed: %ld\n",
  126. PTR_ERR(pdev));
  127. pdev = NULL;
  128. } else {
  129. dev_dbg(&adev->dev, "created platform device %s\n",
  130. dev_name(&pdev->dev));
  131. }
  132. kfree(resources);
  133. return 1;
  134. }
  135. static struct acpi_scan_handler platform_handler = {
  136. .ids = acpi_platform_device_ids,
  137. .attach = acpi_create_platform_device,
  138. };
  139. void __init acpi_platform_init(void)
  140. {
  141. acpi_scan_add_handler(&platform_handler);
  142. }