intel-sdw-acpi.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. // SPDX-License-Identifier: (GPL-2.0 OR BSD-3-Clause)
  2. // Copyright(c) 2015-2021 Intel Corporation.
  3. /*
  4. * SDW Intel ACPI scan helpers
  5. */
  6. #include <linux/acpi.h>
  7. #include <linux/bits.h>
  8. #include <linux/bitfield.h>
  9. #include <linux/device.h>
  10. #include <linux/errno.h>
  11. #include <linux/export.h>
  12. #include <linux/fwnode.h>
  13. #include <linux/module.h>
  14. #include <linux/soundwire/sdw_intel.h>
  15. #include <linux/string.h>
  16. #define SDW_LINK_TYPE 4 /* from Intel ACPI documentation */
  17. static int ctrl_link_mask;
  18. module_param_named(sdw_link_mask, ctrl_link_mask, int, 0444);
  19. MODULE_PARM_DESC(sdw_link_mask, "Intel link mask (one bit per link)");
  20. static ulong ctrl_addr = 0x40000000;
  21. module_param_named(sdw_ctrl_addr, ctrl_addr, ulong, 0444);
  22. MODULE_PARM_DESC(sdw_ctrl_addr, "Intel SoundWire Controller _ADR");
  23. static bool is_link_enabled(struct fwnode_handle *fw_node, u8 idx)
  24. {
  25. struct fwnode_handle *link;
  26. char name[32];
  27. u32 quirk_mask = 0;
  28. /* Find master handle */
  29. snprintf(name, sizeof(name),
  30. "mipi-sdw-link-%hhu-subproperties", idx);
  31. link = fwnode_get_named_child_node(fw_node, name);
  32. if (!link)
  33. return false;
  34. fwnode_property_read_u32(link,
  35. "intel-quirk-mask",
  36. &quirk_mask);
  37. fwnode_handle_put(link);
  38. if (quirk_mask & SDW_INTEL_QUIRK_MASK_BUS_DISABLE)
  39. return false;
  40. return true;
  41. }
  42. static int
  43. sdw_intel_scan_controller(struct sdw_intel_acpi_info *info)
  44. {
  45. struct acpi_device *adev = acpi_fetch_acpi_dev(info->handle);
  46. struct fwnode_handle *fwnode;
  47. unsigned long list;
  48. unsigned int i;
  49. u32 count;
  50. u32 tmp;
  51. int ret;
  52. if (!adev)
  53. return -EINVAL;
  54. fwnode = acpi_fwnode_handle(adev);
  55. /*
  56. * Found controller, find links supported
  57. *
  58. * In theory we could check the number of links supported in
  59. * hardware, but in that step we cannot assume SoundWire IP is
  60. * powered.
  61. *
  62. * In addition, if the BIOS doesn't even provide this
  63. * 'master-count' property then all the inits based on link
  64. * masks will fail as well.
  65. *
  66. * We will check the hardware capabilities in the startup() step
  67. */
  68. ret = fwnode_property_read_u32(fwnode, "mipi-sdw-manager-list", &tmp);
  69. if (ret) {
  70. ret = fwnode_property_read_u32(fwnode, "mipi-sdw-master-count", &count);
  71. if (ret) {
  72. dev_err(&adev->dev,
  73. "Failed to read mipi-sdw-master-count: %d\n",
  74. ret);
  75. return ret;
  76. }
  77. list = GENMASK(count - 1, 0);
  78. } else {
  79. list = tmp;
  80. count = hweight32(list);
  81. }
  82. /* Check count is within bounds */
  83. if (count > SDW_INTEL_MAX_LINKS) {
  84. dev_err(&adev->dev, "Link count %d exceeds max %d\n",
  85. count, SDW_INTEL_MAX_LINKS);
  86. return -EINVAL;
  87. }
  88. if (!count) {
  89. dev_warn(&adev->dev, "No SoundWire links detected\n");
  90. return -EINVAL;
  91. }
  92. dev_dbg(&adev->dev, "ACPI reports %d SDW Link devices\n", count);
  93. info->count = count;
  94. info->link_mask = 0;
  95. for_each_set_bit(i, &list, SDW_INTEL_MAX_LINKS) {
  96. if (ctrl_link_mask && !(ctrl_link_mask & BIT(i))) {
  97. dev_dbg(&adev->dev,
  98. "Link %d masked, will not be enabled\n", i);
  99. continue;
  100. }
  101. if (!is_link_enabled(fwnode, i)) {
  102. dev_dbg(&adev->dev,
  103. "Link %d not selected in firmware\n", i);
  104. continue;
  105. }
  106. info->link_mask |= BIT(i);
  107. }
  108. return 0;
  109. }
  110. static acpi_status sdw_intel_acpi_cb(acpi_handle handle, u32 level,
  111. void *cdata, void **return_value)
  112. {
  113. struct sdw_intel_acpi_info *info = cdata;
  114. u64 adr;
  115. int ret;
  116. ret = acpi_get_local_u64_address(handle, &adr);
  117. if (ret < 0)
  118. return AE_OK; /* keep going */
  119. if (!acpi_fetch_acpi_dev(handle)) {
  120. pr_err("%s: Couldn't find ACPI handle\n", __func__);
  121. return AE_NOT_FOUND;
  122. }
  123. /*
  124. * On some Intel platforms, multiple children of the HDAS
  125. * device can be found, but only one of them is the SoundWire
  126. * controller. The SNDW device is always exposed with
  127. * Name(_ADR, 0x40000000), with bits 31..28 representing the
  128. * SoundWire link so filter accordingly
  129. */
  130. if (FIELD_GET(GENMASK(31, 28), adr) != SDW_LINK_TYPE)
  131. return AE_OK; /* keep going */
  132. if (adr != ctrl_addr)
  133. return AE_OK; /* keep going */
  134. /* found the correct SoundWire controller */
  135. info->handle = handle;
  136. /* device found, stop namespace walk */
  137. return AE_CTRL_TERMINATE;
  138. }
  139. /**
  140. * sdw_intel_acpi_scan() - SoundWire Intel init routine
  141. * @parent_handle: ACPI parent handle
  142. * @info: description of what firmware/DSDT tables expose
  143. *
  144. * This scans the namespace and queries firmware to figure out which
  145. * links to enable. A follow-up use of sdw_intel_probe() and
  146. * sdw_intel_startup() is required for creation of devices and bus
  147. * startup
  148. */
  149. int sdw_intel_acpi_scan(acpi_handle *parent_handle,
  150. struct sdw_intel_acpi_info *info)
  151. {
  152. acpi_status status;
  153. info->handle = NULL;
  154. /*
  155. * In the HDAS ACPI scope, 'SNDW' may be either the child of
  156. * 'HDAS' or the grandchild of 'HDAS'. So let's go through
  157. * the ACPI from 'HDAS' at max depth of 2 to find the 'SNDW'
  158. * device.
  159. */
  160. status = acpi_walk_namespace(ACPI_TYPE_DEVICE,
  161. parent_handle, 2,
  162. sdw_intel_acpi_cb,
  163. NULL, info, NULL);
  164. if (ACPI_FAILURE(status) || info->handle == NULL)
  165. return -ENODEV;
  166. return sdw_intel_scan_controller(info);
  167. }
  168. EXPORT_SYMBOL_NS(sdw_intel_acpi_scan, SND_INTEL_SOUNDWIRE_ACPI);
  169. MODULE_LICENSE("Dual BSD/GPL");
  170. MODULE_DESCRIPTION("Intel Soundwire ACPI helpers");