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/auxiliary_bus.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 void intel_link_dev_release(struct device *dev)
29 struct auxiliary_device *auxdev = to_auxiliary_dev(dev);
30 struct sdw_intel_link_dev *ldev = auxiliary_dev_to_sdw_intel_link_dev(auxdev);
35 /* alloc, init and add link devices */
36 static struct sdw_intel_link_dev *intel_link_dev_register(struct sdw_intel_res *res,
37 struct sdw_intel_ctx *ctx,
38 struct fwnode_handle *fwnode,
42 struct sdw_intel_link_dev *ldev;
43 struct sdw_intel_link_res *link;
44 struct auxiliary_device *auxdev;
47 ldev = kzalloc(sizeof(*ldev), GFP_KERNEL);
49 return ERR_PTR(-ENOMEM);
51 auxdev = &ldev->auxdev;
53 auxdev->dev.parent = res->parent;
54 auxdev->dev.fwnode = fwnode;
55 auxdev->dev.release = intel_link_dev_release;
57 /* we don't use an IDA since we already have a link ID */
61 * keep a handle on the allocated memory, to be used in all other functions.
62 * Since the same pattern is used to skip links that are not enabled, there is
63 * no need to check if ctx->ldev[i] is NULL later on.
65 ctx->ldev[link_id] = ldev;
67 /* Add link information used in the driver probe */
68 link = &ldev->link_res;
69 link->mmio_base = res->mmio_base;
70 link->registers = res->mmio_base + SDW_LINK_BASE
71 + (SDW_LINK_SIZE * link_id);
72 link->shim = res->mmio_base + SDW_SHIM_BASE;
73 link->alh = res->mmio_base + SDW_ALH_BASE;
78 link->clock_stop_quirks = res->clock_stop_quirks;
79 link->shim_lock = &ctx->shim_lock;
80 link->shim_mask = &ctx->shim_mask;
81 link->link_mask = ctx->link_mask;
83 /* now follow the two-step init/add sequence */
84 ret = auxiliary_device_init(auxdev);
86 dev_err(res->parent, "failed to initialize link dev %s link_id %d\n",
92 ret = auxiliary_device_add(&ldev->auxdev);
94 dev_err(res->parent, "failed to add link dev %s link_id %d\n",
95 ldev->auxdev.name, link_id);
96 /* ldev will be freed with the put_device() and .release sequence */
97 auxiliary_device_uninit(&ldev->auxdev);
104 static void intel_link_dev_unregister(struct sdw_intel_link_dev *ldev)
106 auxiliary_device_delete(&ldev->auxdev);
107 auxiliary_device_uninit(&ldev->auxdev);
110 static int sdw_intel_cleanup(struct sdw_intel_ctx *ctx)
112 struct sdw_intel_link_dev *ldev;
116 link_mask = ctx->link_mask;
118 for (i = 0; i < ctx->count; i++) {
119 if (!(link_mask & BIT(i)))
124 pm_runtime_disable(&ldev->auxdev.dev);
125 if (!ldev->link_res.clock_stop_quirks)
126 pm_runtime_put_noidle(ldev->link_res.dev);
128 intel_link_dev_unregister(ldev);
134 #define HDA_DSP_REG_ADSPIC2 (0x10)
135 #define HDA_DSP_REG_ADSPIS2 (0x14)
136 #define HDA_DSP_REG_ADSPIC2_SNDW BIT(5)
139 * sdw_intel_enable_irq() - enable/disable Intel SoundWire IRQ
140 * @mmio_base: The mmio base of the control register
141 * @enable: true if enable
143 void sdw_intel_enable_irq(void __iomem *mmio_base, bool enable)
147 val = readl(mmio_base + HDA_DSP_REG_ADSPIC2);
150 val |= HDA_DSP_REG_ADSPIC2_SNDW;
152 val &= ~HDA_DSP_REG_ADSPIC2_SNDW;
154 writel(val, mmio_base + HDA_DSP_REG_ADSPIC2);
156 EXPORT_SYMBOL_NS(sdw_intel_enable_irq, SOUNDWIRE_INTEL_INIT);
158 irqreturn_t sdw_intel_thread(int irq, void *dev_id)
160 struct sdw_intel_ctx *ctx = dev_id;
161 struct sdw_intel_link_res *link;
163 list_for_each_entry(link, &ctx->link_list, list)
164 sdw_cdns_irq(irq, link->cdns);
166 sdw_intel_enable_irq(ctx->mmio_base, true);
169 EXPORT_SYMBOL_NS(sdw_intel_thread, SOUNDWIRE_INTEL_INIT);
171 static struct sdw_intel_ctx
172 *sdw_intel_probe_controller(struct sdw_intel_res *res)
174 struct sdw_intel_link_res *link;
175 struct sdw_intel_link_dev *ldev;
176 struct sdw_intel_ctx *ctx;
177 struct acpi_device *adev;
178 struct sdw_slave *slave;
179 struct list_head *node;
189 if (acpi_bus_get_device(res->handle, &adev))
196 dev_dbg(&adev->dev, "Creating %d SDW Link devices\n", count);
199 * we need to alloc/free memory manually and can't use devm:
200 * this routine may be called from a workqueue, and not from
202 * If devm_ was used, the memory might never be freed on errors.
204 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
211 * allocate the array of pointers. The link-specific data is allocated
212 * as part of the first loop below and released with the auxiliary_device_uninit().
213 * If some links are disabled, the link pointer will remain NULL. Given that the
214 * number of links is small, this is simpler than using a list to keep track of links.
216 ctx->ldev = kcalloc(ctx->count, sizeof(*ctx->ldev), 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);
227 link_mask = ctx->link_mask;
229 INIT_LIST_HEAD(&ctx->link_list);
231 for (i = 0; i < count; i++) {
232 if (!(link_mask & BIT(i)))
236 * init and add a device for each link
238 * The name of the device will be soundwire_intel.link.[i],
239 * with the "soundwire_intel" module prefix automatically added
240 * by the auxiliary bus core.
242 ldev = intel_link_dev_register(res,
244 acpi_fwnode_handle(adev),
250 link = &ldev->link_res;
251 link->cdns = dev_get_drvdata(&ldev->auxdev.dev);
254 dev_err(&adev->dev, "failed to get link->cdns\n");
256 * 1 will be subtracted from i in the err label, but we need to call
257 * intel_link_dev_unregister for this ldev, so plus 1 now
262 list_add_tail(&link->list, &ctx->link_list);
263 bus = &link->cdns->bus;
264 /* Calculate number of slaves */
265 list_for_each(node, &bus->slaves)
269 ctx->ids = kcalloc(num_slaves, sizeof(*ctx->ids), GFP_KERNEL);
273 ctx->num_slaves = num_slaves;
275 list_for_each_entry(link, &ctx->link_list, list) {
276 bus = &link->cdns->bus;
277 list_for_each_entry(slave, &bus->slaves, node) {
278 ctx->ids[i].id = slave->id;
279 ctx->ids[i].link_id = bus->link_id;
288 if (!(link_mask & BIT(i)))
291 intel_link_dev_unregister(ldev);
299 sdw_intel_startup_controller(struct sdw_intel_ctx *ctx)
301 struct acpi_device *adev;
302 struct sdw_intel_link_dev *ldev;
307 if (acpi_bus_get_device(ctx->handle, &adev))
310 /* Check SNDWLCAP.LCOUNT */
311 caps = ioread32(ctx->mmio_base + SDW_SHIM_BASE + SDW_SHIM_LCAP);
312 caps &= GENMASK(2, 0);
314 /* Check HW supported vs property value */
315 if (caps < ctx->count) {
317 "BIOS master count is larger than hardware capabilities\n");
324 link_mask = ctx->link_mask;
326 /* Startup SDW Master devices */
327 for (i = 0; i < ctx->count; i++) {
328 if (!(link_mask & BIT(i)))
333 intel_link_startup(&ldev->auxdev);
335 if (!ldev->link_res.clock_stop_quirks) {
337 * we need to prevent the parent PCI device
338 * from entering pm_runtime suspend, so that
339 * power rails to the SoundWire IP are not
342 pm_runtime_get_noresume(ldev->link_res.dev);
350 * sdw_intel_probe() - SoundWire Intel probe routine
351 * @res: resource data
353 * This registers an auxiliary device for each Master handled by the controller,
354 * and SoundWire Master and Slave devices will be created by the auxiliary
355 * device probe. All the information necessary is stored in the context, and
356 * the res argument pointer can be freed after this step.
357 * This function will be called after sdw_intel_acpi_scan() by SOF probe.
360 *sdw_intel_probe(struct sdw_intel_res *res)
362 return sdw_intel_probe_controller(res);
364 EXPORT_SYMBOL_NS(sdw_intel_probe, SOUNDWIRE_INTEL_INIT);
367 * sdw_intel_startup() - SoundWire Intel startup
368 * @ctx: SoundWire context allocated in the probe
370 * Startup Intel SoundWire controller. This function will be called after
371 * Intel Audio DSP is powered up.
373 int sdw_intel_startup(struct sdw_intel_ctx *ctx)
375 return sdw_intel_startup_controller(ctx);
377 EXPORT_SYMBOL_NS(sdw_intel_startup, SOUNDWIRE_INTEL_INIT);
379 * sdw_intel_exit() - SoundWire Intel exit
380 * @ctx: SoundWire context allocated in the probe
382 * Delete the controller instances created and cleanup
384 void sdw_intel_exit(struct sdw_intel_ctx *ctx)
386 sdw_intel_cleanup(ctx);
391 EXPORT_SYMBOL_NS(sdw_intel_exit, SOUNDWIRE_INTEL_INIT);
393 void sdw_intel_process_wakeen_event(struct sdw_intel_ctx *ctx)
395 struct sdw_intel_link_dev *ldev;
402 link_mask = ctx->link_mask;
404 /* Startup SDW Master devices */
405 for (i = 0; i < ctx->count; i++) {
406 if (!(link_mask & BIT(i)))
411 intel_link_process_wakeen_event(&ldev->auxdev);
414 EXPORT_SYMBOL_NS(sdw_intel_process_wakeen_event, SOUNDWIRE_INTEL_INIT);
416 MODULE_LICENSE("Dual BSD/GPL");
417 MODULE_DESCRIPTION("Intel Soundwire Init Library");