acpi_platform.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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/kernel.h>
  16. #include <linux/module.h>
  17. #include <linux/platform_device.h>
  18. ACPI_MODULE_NAME("platform");
  19. struct resource_info {
  20. struct device *dev;
  21. struct resource *res;
  22. size_t n, cur;
  23. };
  24. static acpi_status acpi_platform_count_resources(struct acpi_resource *res,
  25. void *data)
  26. {
  27. struct acpi_resource_extended_irq *acpi_xirq;
  28. struct acpi_resource_irq *acpi_irq;
  29. struct resource_info *ri = data;
  30. switch (res->type) {
  31. case ACPI_RESOURCE_TYPE_IRQ:
  32. acpi_irq = &res->data.irq;
  33. ri->n += acpi_irq->interrupt_count;
  34. break;
  35. case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
  36. acpi_xirq = &res->data.extended_irq;
  37. ri->n += acpi_xirq->interrupt_count;
  38. break;
  39. default:
  40. ri->n++;
  41. }
  42. return AE_OK;
  43. }
  44. static acpi_status acpi_platform_add_resources(struct acpi_resource *res,
  45. void *data)
  46. {
  47. struct resource_info *ri = data;
  48. struct resource *r;
  49. r = ri->res + ri->cur;
  50. if (acpi_dev_resource_memory(res, r)
  51. || acpi_dev_resource_io(res, r)
  52. || acpi_dev_resource_address_space(res, r)
  53. || acpi_dev_resource_ext_address_space(res, r)) {
  54. ri->cur++;
  55. return AE_OK;
  56. }
  57. if (acpi_dev_resource_interrupt(res, 0, r)) {
  58. int i;
  59. r++;
  60. for (i = 1; acpi_dev_resource_interrupt(res, i, r); i++)
  61. r++;
  62. ri->cur += i;
  63. }
  64. return AE_OK;
  65. }
  66. /**
  67. * acpi_create_platform_device - Create platform device for ACPI device node
  68. * @adev: ACPI device node to create a platform device for.
  69. *
  70. * Check if the given @adev can be represented as a platform device and, if
  71. * that's the case, create and register a platform device, populate its common
  72. * resources and returns a pointer to it. Otherwise, return %NULL.
  73. *
  74. * The platform device's name will be taken from the @adev's _HID and _UID.
  75. */
  76. struct platform_device *acpi_create_platform_device(struct acpi_device *adev)
  77. {
  78. struct platform_device *pdev = NULL;
  79. struct acpi_device *acpi_parent;
  80. struct device *parent = NULL;
  81. struct resource_info ri;
  82. acpi_status status;
  83. /* If the ACPI node already has a physical device attached, skip it. */
  84. if (adev->physical_node_count)
  85. return NULL;
  86. memset(&ri, 0, sizeof(ri));
  87. /* First, count the resources. */
  88. status = acpi_walk_resources(adev->handle, METHOD_NAME__CRS,
  89. acpi_platform_count_resources, &ri);
  90. if (ACPI_FAILURE(status) || !ri.n)
  91. return NULL;
  92. /* Next, allocate memory for all the resources and populate it. */
  93. ri.dev = &adev->dev;
  94. ri.res = kzalloc(ri.n * sizeof(struct resource), GFP_KERNEL);
  95. if (!ri.res) {
  96. dev_err(&adev->dev,
  97. "failed to allocate memory for resources\n");
  98. return NULL;
  99. }
  100. status = acpi_walk_resources(adev->handle, METHOD_NAME__CRS,
  101. acpi_platform_add_resources, &ri);
  102. if (ACPI_FAILURE(status)) {
  103. dev_err(&adev->dev, "failed to walk resources\n");
  104. goto out;
  105. }
  106. /*
  107. * If the ACPI node has a parent and that parent has a physical device
  108. * attached to it, that physical device should be the parent of the
  109. * platform device we are about to create.
  110. */
  111. acpi_parent = adev->parent;
  112. if (acpi_parent) {
  113. struct acpi_device_physical_node *entry;
  114. struct list_head *list;
  115. mutex_lock(&acpi_parent->physical_node_lock);
  116. list = &acpi_parent->physical_node_list;
  117. if (!list_empty(list)) {
  118. entry = list_first_entry(list,
  119. struct acpi_device_physical_node,
  120. node);
  121. parent = entry->dev;
  122. }
  123. mutex_unlock(&acpi_parent->physical_node_lock);
  124. }
  125. pdev = platform_device_register_resndata(parent, dev_name(&adev->dev),
  126. -1, ri.res, ri.cur, NULL, 0);
  127. if (IS_ERR(pdev)) {
  128. dev_err(&adev->dev, "platform device creation failed: %ld\n",
  129. PTR_ERR(pdev));
  130. pdev = NULL;
  131. } else {
  132. dev_dbg(&adev->dev, "created platform device %s\n",
  133. dev_name(&pdev->dev));
  134. }
  135. out:
  136. kfree(ri.res);
  137. return pdev;
  138. }
  139. static acpi_status acpi_platform_match(acpi_handle handle, u32 depth,
  140. void *data, void **return_value)
  141. {
  142. struct platform_device *pdev = data;
  143. struct acpi_device *adev;
  144. acpi_status status;
  145. status = acpi_bus_get_device(handle, &adev);
  146. if (ACPI_FAILURE(status))
  147. return status;
  148. /* Skip ACPI devices that have physical device attached */
  149. if (adev->physical_node_count)
  150. return AE_OK;
  151. if (!strcmp(dev_name(&pdev->dev), dev_name(&adev->dev))) {
  152. *(acpi_handle *)return_value = handle;
  153. return AE_CTRL_TERMINATE;
  154. }
  155. return AE_OK;
  156. }
  157. static int acpi_platform_find_device(struct device *dev, acpi_handle *handle)
  158. {
  159. struct platform_device *pdev = to_platform_device(dev);
  160. char *name, *tmp, *hid;
  161. /*
  162. * The platform device is named using the ACPI device name
  163. * _HID:INSTANCE so we strip the INSTANCE out in order to find the
  164. * correct device using its _HID.
  165. */
  166. name = kstrdup(dev_name(dev), GFP_KERNEL);
  167. if (!name)
  168. return -ENOMEM;
  169. tmp = name;
  170. hid = strsep(&tmp, ":");
  171. if (!hid) {
  172. kfree(name);
  173. return -ENODEV;
  174. }
  175. *handle = NULL;
  176. acpi_get_devices(hid, acpi_platform_match, pdev, handle);
  177. kfree(name);
  178. return *handle ? 0 : -ENODEV;
  179. }
  180. static struct acpi_bus_type acpi_platform_bus = {
  181. .bus = &platform_bus_type,
  182. .find_device = acpi_platform_find_device,
  183. };
  184. static int __init acpi_platform_init(void)
  185. {
  186. return register_acpi_bus_type(&acpi_platform_bus);
  187. }
  188. arch_initcall(acpi_platform_init);