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/pm_runtime.h>
17 #include <linux/soundwire/sdw_intel.h>
18 #include "cadence_master.h"
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
27 static int sdw_intel_cleanup(struct sdw_intel_ctx *ctx)
29 struct sdw_intel_link_res *link = ctx->links;
36 link_mask = ctx->link_mask;
38 for (i = 0; i < ctx->count; i++, link++) {
39 if (!(link_mask & BIT(i)))
43 pm_runtime_disable(&link->pdev->dev);
44 platform_device_unregister(link->pdev);
47 if (!link->clock_stop_quirks)
48 pm_runtime_put_noidle(link->dev);
54 #define HDA_DSP_REG_ADSPIC2 (0x10)
55 #define HDA_DSP_REG_ADSPIS2 (0x14)
56 #define HDA_DSP_REG_ADSPIC2_SNDW BIT(5)
59 * sdw_intel_enable_irq() - enable/disable Intel SoundWire IRQ
60 * @mmio_base: The mmio base of the control register
61 * @enable: true if enable
63 void sdw_intel_enable_irq(void __iomem *mmio_base, bool enable)
67 val = readl(mmio_base + HDA_DSP_REG_ADSPIC2);
70 val |= HDA_DSP_REG_ADSPIC2_SNDW;
72 val &= ~HDA_DSP_REG_ADSPIC2_SNDW;
74 writel(val, mmio_base + HDA_DSP_REG_ADSPIC2);
76 EXPORT_SYMBOL_NS(sdw_intel_enable_irq, SOUNDWIRE_INTEL_INIT);
78 irqreturn_t sdw_intel_thread(int irq, void *dev_id)
80 struct sdw_intel_ctx *ctx = dev_id;
81 struct sdw_intel_link_res *link;
83 list_for_each_entry(link, &ctx->link_list, list)
84 sdw_cdns_irq(irq, link->cdns);
86 sdw_intel_enable_irq(ctx->mmio_base, true);
89 EXPORT_SYMBOL_NS(sdw_intel_thread, SOUNDWIRE_INTEL_INIT);
91 static struct sdw_intel_ctx
92 *sdw_intel_probe_controller(struct sdw_intel_res *res)
94 struct platform_device_info pdevinfo;
95 struct platform_device *pdev;
96 struct sdw_intel_link_res *link;
97 struct sdw_intel_ctx *ctx;
98 struct acpi_device *adev;
99 struct sdw_slave *slave;
100 struct list_head *node;
110 if (acpi_bus_get_device(res->handle, &adev))
117 dev_dbg(&adev->dev, "Creating %d SDW Link devices\n", count);
119 ctx = devm_kzalloc(&adev->dev, sizeof(*ctx), GFP_KERNEL);
124 ctx->links = devm_kcalloc(&adev->dev, ctx->count,
125 sizeof(*ctx->links), GFP_KERNEL);
130 ctx->mmio_base = res->mmio_base;
131 ctx->link_mask = res->link_mask;
132 ctx->handle = res->handle;
133 mutex_init(&ctx->shim_lock);
136 link_mask = ctx->link_mask;
138 INIT_LIST_HEAD(&ctx->link_list);
140 /* Create SDW Master devices */
141 for (i = 0; i < count; i++, link++) {
142 if (!(link_mask & BIT(i))) {
144 "Link %d masked, will not be enabled\n", i);
148 link->mmio_base = res->mmio_base;
149 link->registers = res->mmio_base + SDW_LINK_BASE
150 + (SDW_LINK_SIZE * i);
151 link->shim = res->mmio_base + SDW_SHIM_BASE;
152 link->alh = res->mmio_base + SDW_ALH_BASE;
154 link->ops = res->ops;
155 link->dev = res->dev;
157 link->clock_stop_quirks = res->clock_stop_quirks;
158 link->shim_lock = &ctx->shim_lock;
159 link->shim_mask = &ctx->shim_mask;
160 link->link_mask = link_mask;
162 memset(&pdevinfo, 0, sizeof(pdevinfo));
164 pdevinfo.parent = res->parent;
165 pdevinfo.name = "intel-sdw";
167 pdevinfo.fwnode = acpi_fwnode_handle(adev);
168 pdevinfo.data = link;
169 pdevinfo.size_data = sizeof(*link);
171 pdev = platform_device_register_full(&pdevinfo);
174 "platform device creation failed: %ld\n",
179 link->cdns = platform_get_drvdata(pdev);
182 dev_err(&adev->dev, "failed to get link->cdns\n");
184 * 1 will be subtracted from i in the err label, but we need to call
185 * intel_link_dev_unregister for this ldev, so plus 1 now
190 list_add_tail(&link->list, &ctx->link_list);
191 bus = &link->cdns->bus;
192 /* Calculate number of slaves */
193 list_for_each(node, &bus->slaves)
197 ctx->ids = devm_kcalloc(&adev->dev, num_slaves,
198 sizeof(*ctx->ids), GFP_KERNEL);
202 ctx->num_slaves = num_slaves;
204 list_for_each_entry(link, &ctx->link_list, list) {
205 bus = &link->cdns->bus;
206 list_for_each_entry(slave, &bus->slaves, node) {
207 ctx->ids[i].id = slave->id;
208 ctx->ids[i].link_id = bus->link_id;
217 sdw_intel_cleanup(ctx);
222 sdw_intel_startup_controller(struct sdw_intel_ctx *ctx)
224 struct acpi_device *adev;
225 struct sdw_intel_link_res *link;
230 if (acpi_bus_get_device(ctx->handle, &adev))
233 /* Check SNDWLCAP.LCOUNT */
234 caps = ioread32(ctx->mmio_base + SDW_SHIM_BASE + SDW_SHIM_LCAP);
235 caps &= GENMASK(2, 0);
237 /* Check HW supported vs property value */
238 if (caps < ctx->count) {
240 "BIOS master count is larger than hardware capabilities\n");
248 link_mask = ctx->link_mask;
250 /* Startup SDW Master devices */
251 for (i = 0; i < ctx->count; i++, link++) {
252 if (!(link_mask & BIT(i)))
255 intel_master_startup(link->pdev);
257 if (!link->clock_stop_quirks) {
259 * we need to prevent the parent PCI device
260 * from entering pm_runtime suspend, so that
261 * power rails to the SoundWire IP are not
264 pm_runtime_get_noresume(link->dev);
272 * sdw_intel_probe() - SoundWire Intel probe routine
273 * @res: resource data
275 * This registers a platform device for each Master handled by the controller,
276 * and SoundWire Master and Slave devices will be created by the platform
277 * device probe. All the information necessary is stored in the context, and
278 * the res argument pointer can be freed after this step.
279 * This function will be called after sdw_intel_acpi_scan() by SOF probe.
282 *sdw_intel_probe(struct sdw_intel_res *res)
284 return sdw_intel_probe_controller(res);
286 EXPORT_SYMBOL_NS(sdw_intel_probe, SOUNDWIRE_INTEL_INIT);
289 * sdw_intel_startup() - SoundWire Intel startup
290 * @ctx: SoundWire context allocated in the probe
292 * Startup Intel SoundWire controller. This function will be called after
293 * Intel Audio DSP is powered up.
295 int sdw_intel_startup(struct sdw_intel_ctx *ctx)
297 return sdw_intel_startup_controller(ctx);
299 EXPORT_SYMBOL_NS(sdw_intel_startup, SOUNDWIRE_INTEL_INIT);
301 * sdw_intel_exit() - SoundWire Intel exit
302 * @ctx: SoundWire context allocated in the probe
304 * Delete the controller instances created and cleanup
306 void sdw_intel_exit(struct sdw_intel_ctx *ctx)
308 sdw_intel_cleanup(ctx);
310 EXPORT_SYMBOL_NS(sdw_intel_exit, SOUNDWIRE_INTEL_INIT);
312 void sdw_intel_process_wakeen_event(struct sdw_intel_ctx *ctx)
314 struct sdw_intel_link_res *link;
322 link_mask = ctx->link_mask;
324 /* Startup SDW Master devices */
325 for (i = 0; i < ctx->count; i++, link++) {
326 if (!(link_mask & BIT(i)))
329 intel_master_process_wakeen_event(link->pdev);
332 EXPORT_SYMBOL_NS(sdw_intel_process_wakeen_event, SOUNDWIRE_INTEL_INIT);
334 MODULE_LICENSE("Dual BSD/GPL");
335 MODULE_DESCRIPTION("Intel Soundwire Init Library");