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