1 // SPDX-License-Identifier: (GPL-2.0 OR BSD-3-Clause)
2 // Copyright(c) 2015-17 Intel Corporation.
5 * SDW Intel Init Routines
7 * Initializes and creates SDW devices based on ACPI and Hardware values
10 #include <linux/acpi.h>
11 #include <linux/export.h>
12 #include <linux/interrupt.h>
14 #include <linux/module.h>
15 #include <linux/platform_device.h>
16 #include <linux/soundwire/sdw_intel.h>
17 #include "cadence_master.h"
20 #define SDW_LINK_TYPE 4 /* from Intel ACPI documentation */
21 #define SDW_MAX_LINKS 4
22 #define SDW_SHIM_LCAP 0x0
23 #define SDW_SHIM_BASE 0x2C000
24 #define SDW_ALH_BASE 0x2C800
25 #define SDW_LINK_BASE 0x30000
26 #define SDW_LINK_SIZE 0x10000
28 static int ctrl_link_mask;
29 module_param_named(sdw_link_mask, ctrl_link_mask, int, 0444);
30 MODULE_PARM_DESC(sdw_link_mask, "Intel link mask (one bit per link)");
32 static bool is_link_enabled(struct fwnode_handle *fw_node, int i)
34 struct fwnode_handle *link;
38 /* Find master handle */
39 snprintf(name, sizeof(name),
40 "mipi-sdw-link-%d-subproperties", i);
42 link = fwnode_get_named_child_node(fw_node, name);
46 fwnode_property_read_u32(link,
50 if (quirk_mask & SDW_INTEL_QUIRK_MASK_BUS_DISABLE)
56 static int sdw_intel_cleanup(struct sdw_intel_ctx *ctx)
58 struct sdw_intel_link_res *link = ctx->links;
65 link_mask = ctx->link_mask;
67 for (i = 0; i < ctx->count; i++, link++) {
68 if (!(link_mask & BIT(i)))
72 platform_device_unregister(link->pdev);
79 sdw_intel_scan_controller(struct sdw_intel_acpi_info *info)
81 struct acpi_device *adev;
85 if (acpi_bus_get_device(info->handle, &adev))
88 /* Found controller, find links supported */
90 ret = fwnode_property_read_u8_array(acpi_fwnode_handle(adev),
91 "mipi-sdw-master-count", &count, 1);
94 * In theory we could check the number of links supported in
95 * hardware, but in that step we cannot assume SoundWire IP is
98 * In addition, if the BIOS doesn't even provide this
99 * 'master-count' property then all the inits based on link
100 * masks will fail as well.
102 * We will check the hardware capabilities in the startup() step
107 "Failed to read mipi-sdw-master-count: %d\n", ret);
111 /* Check count is within bounds */
112 if (count > SDW_MAX_LINKS) {
113 dev_err(&adev->dev, "Link count %d exceeds max %d\n",
114 count, SDW_MAX_LINKS);
119 dev_warn(&adev->dev, "No SoundWire links detected\n");
122 dev_dbg(&adev->dev, "ACPI reports %d SDW Link devices\n", count);
127 for (i = 0; i < count; i++) {
128 if (ctrl_link_mask && !(ctrl_link_mask & BIT(i))) {
130 "Link %d masked, will not be enabled\n", i);
134 if (!is_link_enabled(acpi_fwnode_handle(adev), i)) {
136 "Link %d not selected in firmware\n", i);
140 info->link_mask |= BIT(i);
146 #define HDA_DSP_REG_ADSPIC2 (0x10)
147 #define HDA_DSP_REG_ADSPIS2 (0x14)
148 #define HDA_DSP_REG_ADSPIC2_SNDW BIT(5)
151 * sdw_intel_enable_irq() - enable/disable Intel SoundWire IRQ
152 * @mmio_base: The mmio base of the control register
153 * @enable: true if enable
155 void sdw_intel_enable_irq(void __iomem *mmio_base, bool enable)
159 val = readl(mmio_base + HDA_DSP_REG_ADSPIC2);
162 val |= HDA_DSP_REG_ADSPIC2_SNDW;
164 val &= ~HDA_DSP_REG_ADSPIC2_SNDW;
166 writel(val, mmio_base + HDA_DSP_REG_ADSPIC2);
168 EXPORT_SYMBOL_NS(sdw_intel_enable_irq, SOUNDWIRE_INTEL_INIT);
170 irqreturn_t sdw_intel_thread(int irq, void *dev_id)
172 struct sdw_intel_ctx *ctx = dev_id;
173 struct sdw_intel_link_res *link;
175 list_for_each_entry(link, &ctx->link_list, list)
176 sdw_cdns_irq(irq, link->cdns);
178 sdw_intel_enable_irq(ctx->mmio_base, true);
181 EXPORT_SYMBOL_NS(sdw_intel_thread, SOUNDWIRE_INTEL_INIT);
183 static struct sdw_intel_ctx
184 *sdw_intel_probe_controller(struct sdw_intel_res *res)
186 struct platform_device_info pdevinfo;
187 struct platform_device *pdev;
188 struct sdw_intel_link_res *link;
189 struct sdw_intel_ctx *ctx;
190 struct acpi_device *adev;
191 struct sdw_slave *slave;
192 struct list_head *node;
202 if (acpi_bus_get_device(res->handle, &adev))
209 dev_dbg(&adev->dev, "Creating %d SDW Link devices\n", count);
211 ctx = devm_kzalloc(&adev->dev, sizeof(*ctx), GFP_KERNEL);
216 ctx->links = devm_kcalloc(&adev->dev, ctx->count,
217 sizeof(*ctx->links), GFP_KERNEL);
222 ctx->mmio_base = res->mmio_base;
223 ctx->link_mask = res->link_mask;
224 ctx->handle = res->handle;
225 mutex_init(&ctx->shim_lock);
228 link_mask = ctx->link_mask;
230 INIT_LIST_HEAD(&ctx->link_list);
232 /* Create SDW Master devices */
233 for (i = 0; i < count; i++, link++) {
234 if (!(link_mask & BIT(i))) {
236 "Link %d masked, will not be enabled\n", i);
240 link->mmio_base = res->mmio_base;
241 link->registers = res->mmio_base + SDW_LINK_BASE
242 + (SDW_LINK_SIZE * i);
243 link->shim = res->mmio_base + SDW_SHIM_BASE;
244 link->alh = res->mmio_base + SDW_ALH_BASE;
246 link->ops = res->ops;
247 link->dev = res->dev;
249 link->shim_lock = &ctx->shim_lock;
250 link->shim_mask = &ctx->shim_mask;
252 memset(&pdevinfo, 0, sizeof(pdevinfo));
254 pdevinfo.parent = res->parent;
255 pdevinfo.name = "intel-sdw";
257 pdevinfo.fwnode = acpi_fwnode_handle(adev);
258 pdevinfo.data = link;
259 pdevinfo.size_data = sizeof(*link);
261 pdev = platform_device_register_full(&pdevinfo);
264 "platform device creation failed: %ld\n",
269 link->cdns = platform_get_drvdata(pdev);
271 list_add_tail(&link->list, &ctx->link_list);
272 bus = &link->cdns->bus;
273 /* Calculate number of slaves */
274 list_for_each(node, &bus->slaves)
278 ctx->ids = devm_kcalloc(&adev->dev, num_slaves,
279 sizeof(*ctx->ids), GFP_KERNEL);
283 ctx->num_slaves = num_slaves;
285 list_for_each_entry(link, &ctx->link_list, list) {
286 bus = &link->cdns->bus;
287 list_for_each_entry(slave, &bus->slaves, node) {
288 ctx->ids[i].id = slave->id;
289 ctx->ids[i].link_id = bus->link_id;
298 sdw_intel_cleanup(ctx);
303 sdw_intel_startup_controller(struct sdw_intel_ctx *ctx)
305 struct acpi_device *adev;
306 struct sdw_intel_link_res *link;
311 if (acpi_bus_get_device(ctx->handle, &adev))
314 /* Check SNDWLCAP.LCOUNT */
315 caps = ioread32(ctx->mmio_base + SDW_SHIM_BASE + SDW_SHIM_LCAP);
316 caps &= GENMASK(2, 0);
318 /* Check HW supported vs property value */
319 if (caps < ctx->count) {
321 "BIOS master count is larger than hardware capabilities\n");
329 link_mask = ctx->link_mask;
331 /* Startup SDW Master devices */
332 for (i = 0; i < ctx->count; i++, link++) {
333 if (!(link_mask & BIT(i)))
336 intel_master_startup(link->pdev);
342 static acpi_status sdw_intel_acpi_cb(acpi_handle handle, u32 level,
343 void *cdata, void **return_value)
345 struct sdw_intel_acpi_info *info = cdata;
346 struct acpi_device *adev;
350 status = acpi_evaluate_integer(handle, METHOD_NAME__ADR, NULL, &adr);
351 if (ACPI_FAILURE(status))
352 return AE_OK; /* keep going */
354 if (acpi_bus_get_device(handle, &adev)) {
355 pr_err("%s: Couldn't find ACPI handle\n", __func__);
359 info->handle = handle;
362 * On some Intel platforms, multiple children of the HDAS
363 * device can be found, but only one of them is the SoundWire
364 * controller. The SNDW device is always exposed with
365 * Name(_ADR, 0x40000000), with bits 31..28 representing the
366 * SoundWire link so filter accordingly
368 if ((adr & GENMASK(31, 28)) >> 28 != SDW_LINK_TYPE)
369 return AE_OK; /* keep going */
371 /* device found, stop namespace walk */
372 return AE_CTRL_TERMINATE;
376 * sdw_intel_acpi_scan() - SoundWire Intel init routine
377 * @parent_handle: ACPI parent handle
378 * @info: description of what firmware/DSDT tables expose
380 * This scans the namespace and queries firmware to figure out which
381 * links to enable. A follow-up use of sdw_intel_probe() and
382 * sdw_intel_startup() is required for creation of devices and bus
385 int sdw_intel_acpi_scan(acpi_handle *parent_handle,
386 struct sdw_intel_acpi_info *info)
390 status = acpi_walk_namespace(ACPI_TYPE_DEVICE,
394 if (ACPI_FAILURE(status))
397 return sdw_intel_scan_controller(info);
399 EXPORT_SYMBOL_NS(sdw_intel_acpi_scan, SOUNDWIRE_INTEL_INIT);
402 * sdw_intel_probe() - SoundWire Intel probe routine
403 * @res: resource data
405 * This registers a platform device for each Master handled by the controller,
406 * and SoundWire Master and Slave devices will be created by the platform
407 * device probe. All the information necessary is stored in the context, and
408 * the res argument pointer can be freed after this step.
409 * This function will be called after sdw_intel_acpi_scan() by SOF probe.
412 *sdw_intel_probe(struct sdw_intel_res *res)
414 return sdw_intel_probe_controller(res);
416 EXPORT_SYMBOL_NS(sdw_intel_probe, SOUNDWIRE_INTEL_INIT);
419 * sdw_intel_startup() - SoundWire Intel startup
420 * @ctx: SoundWire context allocated in the probe
422 * Startup Intel SoundWire controller. This function will be called after
423 * Intel Audio DSP is powered up.
425 int sdw_intel_startup(struct sdw_intel_ctx *ctx)
427 return sdw_intel_startup_controller(ctx);
429 EXPORT_SYMBOL_NS(sdw_intel_startup, SOUNDWIRE_INTEL_INIT);
431 * sdw_intel_exit() - SoundWire Intel exit
432 * @ctx: SoundWire context allocated in the probe
434 * Delete the controller instances created and cleanup
436 void sdw_intel_exit(struct sdw_intel_ctx *ctx)
438 sdw_intel_cleanup(ctx);
440 EXPORT_SYMBOL_NS(sdw_intel_exit, SOUNDWIRE_INTEL_INIT);
442 void sdw_intel_process_wakeen_event(struct sdw_intel_ctx *ctx)
444 struct sdw_intel_link_res *link;
452 link_mask = ctx->link_mask;
454 /* Startup SDW Master devices */
455 for (i = 0; i < ctx->count; i++, link++) {
456 if (!(link_mask & BIT(i)))
459 intel_master_process_wakeen_event(link->pdev);
462 EXPORT_SYMBOL_NS(sdw_intel_process_wakeen_event, SOUNDWIRE_INTEL_INIT);
464 MODULE_LICENSE("Dual BSD/GPL");
465 MODULE_DESCRIPTION("Intel Soundwire Init Library");