xref: /linux/drivers/soundwire/intel_init.c (revision 4a17c441c7cb06ac1e5fd3409a64d8ce78151983)
1 // SPDX-License-Identifier: (GPL-2.0 OR BSD-3-Clause)
2 // Copyright(c) 2015-17 Intel Corporation.
3 
4 /*
5  * SDW Intel Init Routines
6  *
7  * Initializes and creates SDW devices based on ACPI and Hardware values
8  */
9 
10 #include <linux/acpi.h>
11 #include <linux/export.h>
12 #include <linux/io.h>
13 #include <linux/module.h>
14 #include <linux/platform_device.h>
15 #include <linux/soundwire/sdw_intel.h>
16 #include "cadence_master.h"
17 #include "intel.h"
18 
19 #define SDW_LINK_TYPE		4 /* from Intel ACPI documentation */
20 #define SDW_MAX_LINKS		4
21 #define SDW_SHIM_LCAP		0x0
22 #define SDW_SHIM_BASE		0x2C000
23 #define SDW_ALH_BASE		0x2C800
24 #define SDW_LINK_BASE		0x30000
25 #define SDW_LINK_SIZE		0x10000
26 
27 static int ctrl_link_mask;
28 module_param_named(sdw_link_mask, ctrl_link_mask, int, 0444);
29 MODULE_PARM_DESC(sdw_link_mask, "Intel link mask (one bit per link)");
30 
31 static bool is_link_enabled(struct fwnode_handle *fw_node, int i)
32 {
33 	struct fwnode_handle *link;
34 	char name[32];
35 	u32 quirk_mask = 0;
36 
37 	/* Find master handle */
38 	snprintf(name, sizeof(name),
39 		 "mipi-sdw-link-%d-subproperties", i);
40 
41 	link = fwnode_get_named_child_node(fw_node, name);
42 	if (!link)
43 		return false;
44 
45 	fwnode_property_read_u32(link,
46 				 "intel-quirk-mask",
47 				 &quirk_mask);
48 
49 	if (quirk_mask & SDW_INTEL_QUIRK_MASK_BUS_DISABLE)
50 		return false;
51 
52 	return true;
53 }
54 
55 static int sdw_intel_cleanup(struct sdw_intel_ctx *ctx)
56 {
57 	struct sdw_intel_link_res *link = ctx->links;
58 	u32 link_mask;
59 	int i;
60 
61 	if (!link)
62 		return 0;
63 
64 	link_mask = ctx->link_mask;
65 
66 	for (i = 0; i < ctx->count; i++, link++) {
67 		if (!(link_mask & BIT(i)))
68 			continue;
69 
70 		if (link->pdev)
71 			platform_device_unregister(link->pdev);
72 	}
73 
74 	return 0;
75 }
76 
77 static int
78 sdw_intel_scan_controller(struct sdw_intel_acpi_info *info)
79 {
80 	struct acpi_device *adev;
81 	int ret, i;
82 	u8 count;
83 
84 	if (acpi_bus_get_device(info->handle, &adev))
85 		return -EINVAL;
86 
87 	/* Found controller, find links supported */
88 	count = 0;
89 	ret = fwnode_property_read_u8_array(acpi_fwnode_handle(adev),
90 					    "mipi-sdw-master-count", &count, 1);
91 
92 	/*
93 	 * In theory we could check the number of links supported in
94 	 * hardware, but in that step we cannot assume SoundWire IP is
95 	 * powered.
96 	 *
97 	 * In addition, if the BIOS doesn't even provide this
98 	 * 'master-count' property then all the inits based on link
99 	 * masks will fail as well.
100 	 *
101 	 * We will check the hardware capabilities in the startup() step
102 	 */
103 
104 	if (ret) {
105 		dev_err(&adev->dev,
106 			"Failed to read mipi-sdw-master-count: %d\n", ret);
107 		return -EINVAL;
108 	}
109 
110 	/* Check count is within bounds */
111 	if (count > SDW_MAX_LINKS) {
112 		dev_err(&adev->dev, "Link count %d exceeds max %d\n",
113 			count, SDW_MAX_LINKS);
114 		return -EINVAL;
115 	}
116 
117 	if (!count) {
118 		dev_warn(&adev->dev, "No SoundWire links detected\n");
119 		return -EINVAL;
120 	}
121 	dev_dbg(&adev->dev, "ACPI reports %d SDW Link devices\n", count);
122 
123 	info->count = count;
124 	info->link_mask = 0;
125 
126 	for (i = 0; i < count; i++) {
127 		if (ctrl_link_mask && !(ctrl_link_mask & BIT(i))) {
128 			dev_dbg(&adev->dev,
129 				"Link %d masked, will not be enabled\n", i);
130 			continue;
131 		}
132 
133 		if (!is_link_enabled(acpi_fwnode_handle(adev), i)) {
134 			dev_dbg(&adev->dev,
135 				"Link %d not selected in firmware\n", i);
136 			continue;
137 		}
138 
139 		info->link_mask |= BIT(i);
140 	}
141 
142 	return 0;
143 }
144 
145 static struct sdw_intel_ctx
146 *sdw_intel_probe_controller(struct sdw_intel_res *res)
147 {
148 	struct platform_device_info pdevinfo;
149 	struct platform_device *pdev;
150 	struct sdw_intel_link_res *link;
151 	struct sdw_intel_ctx *ctx;
152 	struct acpi_device *adev;
153 	u32 link_mask;
154 	int count;
155 	int i;
156 
157 	if (!res)
158 		return NULL;
159 
160 	if (acpi_bus_get_device(res->handle, &adev))
161 		return NULL;
162 
163 	if (!res->count)
164 		return NULL;
165 
166 	count = res->count;
167 	dev_dbg(&adev->dev, "Creating %d SDW Link devices\n", count);
168 
169 	ctx = devm_kzalloc(&adev->dev, sizeof(*ctx), GFP_KERNEL);
170 	if (!ctx)
171 		return NULL;
172 
173 	ctx->count = count;
174 	ctx->links = devm_kcalloc(&adev->dev, ctx->count,
175 				  sizeof(*ctx->links), GFP_KERNEL);
176 	if (!ctx->links)
177 		return NULL;
178 
179 	ctx->count = count;
180 	ctx->mmio_base = res->mmio_base;
181 	ctx->link_mask = res->link_mask;
182 	ctx->handle = res->handle;
183 	mutex_init(&ctx->shim_lock);
184 
185 	link = ctx->links;
186 	link_mask = ctx->link_mask;
187 
188 	/* Create SDW Master devices */
189 	for (i = 0; i < count; i++, link++) {
190 		if (!(link_mask & BIT(i))) {
191 			dev_dbg(&adev->dev,
192 				"Link %d masked, will not be enabled\n", i);
193 			continue;
194 		}
195 
196 		link->mmio_base = res->mmio_base;
197 		link->registers = res->mmio_base + SDW_LINK_BASE
198 			+ (SDW_LINK_SIZE * i);
199 		link->shim = res->mmio_base + SDW_SHIM_BASE;
200 		link->alh = res->mmio_base + SDW_ALH_BASE;
201 
202 		link->ops = res->ops;
203 		link->dev = res->dev;
204 
205 		link->shim_lock = &ctx->shim_lock;
206 		link->shim_mask = &ctx->shim_mask;
207 
208 		memset(&pdevinfo, 0, sizeof(pdevinfo));
209 
210 		pdevinfo.parent = res->parent;
211 		pdevinfo.name = "intel-sdw";
212 		pdevinfo.id = i;
213 		pdevinfo.fwnode = acpi_fwnode_handle(adev);
214 		pdevinfo.data = link;
215 		pdevinfo.size_data = sizeof(*link);
216 
217 		pdev = platform_device_register_full(&pdevinfo);
218 		if (IS_ERR(pdev)) {
219 			dev_err(&adev->dev,
220 				"platform device creation failed: %ld\n",
221 				PTR_ERR(pdev));
222 			goto err;
223 		}
224 		link->pdev = pdev;
225 	}
226 
227 	return ctx;
228 
229 err:
230 	ctx->count = i;
231 	sdw_intel_cleanup(ctx);
232 	return NULL;
233 }
234 
235 static int
236 sdw_intel_startup_controller(struct sdw_intel_ctx *ctx)
237 {
238 	struct acpi_device *adev;
239 	struct sdw_intel_link_res *link;
240 	u32 caps;
241 	u32 link_mask;
242 	int i;
243 
244 	if (acpi_bus_get_device(ctx->handle, &adev))
245 		return -EINVAL;
246 
247 	/* Check SNDWLCAP.LCOUNT */
248 	caps = ioread32(ctx->mmio_base + SDW_SHIM_BASE + SDW_SHIM_LCAP);
249 	caps &= GENMASK(2, 0);
250 
251 	/* Check HW supported vs property value */
252 	if (caps < ctx->count) {
253 		dev_err(&adev->dev,
254 			"BIOS master count is larger than hardware capabilities\n");
255 		return -EINVAL;
256 	}
257 
258 	if (!ctx->links)
259 		return -EINVAL;
260 
261 	link = ctx->links;
262 	link_mask = ctx->link_mask;
263 
264 	/* Startup SDW Master devices */
265 	for (i = 0; i < ctx->count; i++, link++) {
266 		if (!(link_mask & BIT(i)))
267 			continue;
268 
269 		intel_master_startup(link->pdev);
270 	}
271 
272 	return 0;
273 }
274 
275 static acpi_status sdw_intel_acpi_cb(acpi_handle handle, u32 level,
276 				     void *cdata, void **return_value)
277 {
278 	struct sdw_intel_acpi_info *info = cdata;
279 	struct acpi_device *adev;
280 	acpi_status status;
281 	u64 adr;
282 
283 	status = acpi_evaluate_integer(handle, METHOD_NAME__ADR, NULL, &adr);
284 	if (ACPI_FAILURE(status))
285 		return AE_OK; /* keep going */
286 
287 	if (acpi_bus_get_device(handle, &adev)) {
288 		pr_err("%s: Couldn't find ACPI handle\n", __func__);
289 		return AE_NOT_FOUND;
290 	}
291 
292 	info->handle = handle;
293 
294 	/*
295 	 * On some Intel platforms, multiple children of the HDAS
296 	 * device can be found, but only one of them is the SoundWire
297 	 * controller. The SNDW device is always exposed with
298 	 * Name(_ADR, 0x40000000), with bits 31..28 representing the
299 	 * SoundWire link so filter accordingly
300 	 */
301 	if ((adr & GENMASK(31, 28)) >> 28 != SDW_LINK_TYPE)
302 		return AE_OK; /* keep going */
303 
304 	/* device found, stop namespace walk */
305 	return AE_CTRL_TERMINATE;
306 }
307 
308 /**
309  * sdw_intel_acpi_scan() - SoundWire Intel init routine
310  * @parent_handle: ACPI parent handle
311  * @info: description of what firmware/DSDT tables expose
312  *
313  * This scans the namespace and queries firmware to figure out which
314  * links to enable. A follow-up use of sdw_intel_probe() and
315  * sdw_intel_startup() is required for creation of devices and bus
316  * startup
317  */
318 int sdw_intel_acpi_scan(acpi_handle *parent_handle,
319 			struct sdw_intel_acpi_info *info)
320 {
321 	acpi_status status;
322 
323 	status = acpi_walk_namespace(ACPI_TYPE_DEVICE,
324 				     parent_handle, 1,
325 				     sdw_intel_acpi_cb,
326 				     NULL, info, NULL);
327 	if (ACPI_FAILURE(status))
328 		return -ENODEV;
329 
330 	return sdw_intel_scan_controller(info);
331 }
332 EXPORT_SYMBOL(sdw_intel_acpi_scan);
333 
334 /**
335  * sdw_intel_probe() - SoundWire Intel probe routine
336  * @res: resource data
337  *
338  * This registers a platform device for each Master handled by the controller,
339  * and SoundWire Master and Slave devices will be created by the platform
340  * device probe. All the information necessary is stored in the context, and
341  * the res argument pointer can be freed after this step.
342  * This function will be called after sdw_intel_acpi_scan() by SOF probe.
343  */
344 struct sdw_intel_ctx
345 *sdw_intel_probe(struct sdw_intel_res *res)
346 {
347 	return sdw_intel_probe_controller(res);
348 }
349 EXPORT_SYMBOL(sdw_intel_probe);
350 
351 /**
352  * sdw_intel_startup() - SoundWire Intel startup
353  * @ctx: SoundWire context allocated in the probe
354  *
355  * Startup Intel SoundWire controller. This function will be called after
356  * Intel Audio DSP is powered up.
357  */
358 int sdw_intel_startup(struct sdw_intel_ctx *ctx)
359 {
360 	return sdw_intel_startup_controller(ctx);
361 }
362 EXPORT_SYMBOL(sdw_intel_startup);
363 /**
364  * sdw_intel_exit() - SoundWire Intel exit
365  * @ctx: SoundWire context allocated in the probe
366  *
367  * Delete the controller instances created and cleanup
368  */
369 void sdw_intel_exit(struct sdw_intel_ctx *ctx)
370 {
371 	sdw_intel_cleanup(ctx);
372 }
373 EXPORT_SYMBOL(sdw_intel_exit);
374 
375 MODULE_LICENSE("Dual BSD/GPL");
376 MODULE_DESCRIPTION("Intel Soundwire Init Library");
377