acpi_platform.c 3.6 KB

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