intel-sdw-acpi.c 4.7 KB

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