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 static void intel_link_dev_release(struct device *dev)
23 struct auxiliary_device *auxdev = to_auxiliary_dev(dev);
24 struct sdw_intel_link_dev *ldev = auxiliary_dev_to_sdw_intel_link_dev(auxdev);
29 /* alloc, init and add link devices */
30 static struct sdw_intel_link_dev *intel_link_dev_register(struct sdw_intel_res *res,
31 struct sdw_intel_ctx *ctx,
32 struct fwnode_handle *fwnode,
36 struct sdw_intel_link_dev *ldev;
37 struct sdw_intel_link_res *link;
38 struct auxiliary_device *auxdev;
41 ldev = kzalloc(sizeof(*ldev), GFP_KERNEL);
43 return ERR_PTR(-ENOMEM);
45 auxdev = &ldev->auxdev;
47 auxdev->dev.parent = res->parent;
48 auxdev->dev.fwnode = fwnode;
49 auxdev->dev.release = intel_link_dev_release;
51 /* we don't use an IDA since we already have a link ID */
55 * keep a handle on the allocated memory, to be used in all other functions.
56 * Since the same pattern is used to skip links that are not enabled, there is
57 * no need to check if ctx->ldev[i] is NULL later on.
59 ctx->ldev[link_id] = ldev;
61 /* Add link information used in the driver probe */
62 link = &ldev->link_res;
63 link->mmio_base = res->mmio_base;
64 link->registers = res->mmio_base + SDW_LINK_BASE
65 + (SDW_LINK_SIZE * link_id);
66 link->shim = res->mmio_base + res->shim_base;
67 link->alh = res->mmio_base + res->alh_base;
72 link->clock_stop_quirks = res->clock_stop_quirks;
73 link->shim_lock = &ctx->shim_lock;
74 link->shim_mask = &ctx->shim_mask;
75 link->link_mask = ctx->link_mask;
77 /* now follow the two-step init/add sequence */
78 ret = auxiliary_device_init(auxdev);
80 dev_err(res->parent, "failed to initialize link dev %s link_id %d\n",
86 ret = auxiliary_device_add(&ldev->auxdev);
88 dev_err(res->parent, "failed to add link dev %s link_id %d\n",
89 ldev->auxdev.name, link_id);
90 /* ldev will be freed with the put_device() and .release sequence */
91 auxiliary_device_uninit(&ldev->auxdev);
98 static void intel_link_dev_unregister(struct sdw_intel_link_dev *ldev)
100 auxiliary_device_delete(&ldev->auxdev);
101 auxiliary_device_uninit(&ldev->auxdev);
104 static int sdw_intel_cleanup(struct sdw_intel_ctx *ctx)
106 struct sdw_intel_link_dev *ldev;
110 link_mask = ctx->link_mask;
112 for (i = 0; i < ctx->count; i++) {
113 if (!(link_mask & BIT(i)))
118 pm_runtime_disable(&ldev->auxdev.dev);
119 if (!ldev->link_res.clock_stop_quirks)
120 pm_runtime_put_noidle(ldev->link_res.dev);
122 intel_link_dev_unregister(ldev);
128 #define HDA_DSP_REG_ADSPIC2 (0x10)
129 #define HDA_DSP_REG_ADSPIS2 (0x14)
130 #define HDA_DSP_REG_ADSPIC2_SNDW BIT(5)
133 * sdw_intel_enable_irq() - enable/disable Intel SoundWire IRQ
134 * @mmio_base: The mmio base of the control register
135 * @enable: true if enable
137 void sdw_intel_enable_irq(void __iomem *mmio_base, bool enable)
141 val = readl(mmio_base + HDA_DSP_REG_ADSPIC2);
144 val |= HDA_DSP_REG_ADSPIC2_SNDW;
146 val &= ~HDA_DSP_REG_ADSPIC2_SNDW;
148 writel(val, mmio_base + HDA_DSP_REG_ADSPIC2);
150 EXPORT_SYMBOL_NS(sdw_intel_enable_irq, SOUNDWIRE_INTEL_INIT);
152 irqreturn_t sdw_intel_thread(int irq, void *dev_id)
154 struct sdw_intel_ctx *ctx = dev_id;
155 struct sdw_intel_link_res *link;
157 list_for_each_entry(link, &ctx->link_list, list)
158 sdw_cdns_irq(irq, link->cdns);
160 sdw_intel_enable_irq(ctx->mmio_base, true);
163 EXPORT_SYMBOL_NS(sdw_intel_thread, SOUNDWIRE_INTEL_INIT);
165 static struct sdw_intel_ctx
166 *sdw_intel_probe_controller(struct sdw_intel_res *res)
168 struct sdw_intel_link_res *link;
169 struct sdw_intel_link_dev *ldev;
170 struct sdw_intel_ctx *ctx;
171 struct acpi_device *adev;
172 struct sdw_slave *slave;
173 struct list_head *node;
183 adev = acpi_fetch_acpi_dev(res->handle);
191 dev_dbg(&adev->dev, "Creating %d SDW Link devices\n", count);
194 * we need to alloc/free memory manually and can't use devm:
195 * this routine may be called from a workqueue, and not from
197 * If devm_ was used, the memory might never be freed on errors.
199 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
206 * allocate the array of pointers. The link-specific data is allocated
207 * as part of the first loop below and released with the auxiliary_device_uninit().
208 * If some links are disabled, the link pointer will remain NULL. Given that the
209 * number of links is small, this is simpler than using a list to keep track of links.
211 ctx->ldev = kcalloc(ctx->count, sizeof(*ctx->ldev), GFP_KERNEL);
217 ctx->mmio_base = res->mmio_base;
218 ctx->shim_base = res->shim_base;
219 ctx->alh_base = res->alh_base;
220 ctx->link_mask = res->link_mask;
221 ctx->handle = res->handle;
222 mutex_init(&ctx->shim_lock);
224 link_mask = ctx->link_mask;
226 INIT_LIST_HEAD(&ctx->link_list);
228 for (i = 0; i < count; i++) {
229 if (!(link_mask & BIT(i)))
233 * init and add a device for each link
235 * The name of the device will be soundwire_intel.link.[i],
236 * with the "soundwire_intel" module prefix automatically added
237 * by the auxiliary bus core.
239 ldev = intel_link_dev_register(res,
241 acpi_fwnode_handle(adev),
247 link = &ldev->link_res;
248 link->cdns = auxiliary_get_drvdata(&ldev->auxdev);
251 dev_err(&adev->dev, "failed to get link->cdns\n");
253 * 1 will be subtracted from i in the err label, but we need to call
254 * intel_link_dev_unregister for this ldev, so plus 1 now
259 list_add_tail(&link->list, &ctx->link_list);
260 bus = &link->cdns->bus;
261 /* Calculate number of slaves */
262 list_for_each(node, &bus->slaves)
266 ctx->ids = kcalloc(num_slaves, sizeof(*ctx->ids), GFP_KERNEL);
270 ctx->num_slaves = num_slaves;
272 list_for_each_entry(link, &ctx->link_list, list) {
273 bus = &link->cdns->bus;
274 list_for_each_entry(slave, &bus->slaves, node) {
275 ctx->ids[i].id = slave->id;
276 ctx->ids[i].link_id = bus->link_id;
285 if (!(link_mask & BIT(i)))
288 intel_link_dev_unregister(ldev);
296 sdw_intel_startup_controller(struct sdw_intel_ctx *ctx)
298 struct acpi_device *adev = acpi_fetch_acpi_dev(ctx->handle);
299 struct sdw_intel_link_dev *ldev;
307 /* Check SNDWLCAP.LCOUNT */
308 caps = ioread32(ctx->mmio_base + ctx->shim_base + SDW_SHIM_LCAP);
309 caps &= SDW_SHIM_LCAP_LCOUNT_MASK;
311 /* Check HW supported vs property value */
312 if (caps < ctx->count) {
314 "BIOS master count is larger than hardware capabilities\n");
321 link_mask = ctx->link_mask;
323 /* Startup SDW Master devices */
324 for (i = 0; i < ctx->count; i++) {
325 if (!(link_mask & BIT(i)))
330 intel_link_startup(&ldev->auxdev);
332 if (!ldev->link_res.clock_stop_quirks) {
334 * we need to prevent the parent PCI device
335 * from entering pm_runtime suspend, so that
336 * power rails to the SoundWire IP are not
339 pm_runtime_get_noresume(ldev->link_res.dev);
347 * sdw_intel_probe() - SoundWire Intel probe routine
348 * @res: resource data
350 * This registers an auxiliary device for each Master handled by the controller,
351 * and SoundWire Master and Slave devices will be created by the auxiliary
352 * device probe. All the information necessary is stored in the context, and
353 * the res argument pointer can be freed after this step.
354 * This function will be called after sdw_intel_acpi_scan() by SOF probe.
357 *sdw_intel_probe(struct sdw_intel_res *res)
359 return sdw_intel_probe_controller(res);
361 EXPORT_SYMBOL_NS(sdw_intel_probe, SOUNDWIRE_INTEL_INIT);
364 * sdw_intel_startup() - SoundWire Intel startup
365 * @ctx: SoundWire context allocated in the probe
367 * Startup Intel SoundWire controller. This function will be called after
368 * Intel Audio DSP is powered up.
370 int sdw_intel_startup(struct sdw_intel_ctx *ctx)
372 return sdw_intel_startup_controller(ctx);
374 EXPORT_SYMBOL_NS(sdw_intel_startup, SOUNDWIRE_INTEL_INIT);
376 * sdw_intel_exit() - SoundWire Intel exit
377 * @ctx: SoundWire context allocated in the probe
379 * Delete the controller instances created and cleanup
381 void sdw_intel_exit(struct sdw_intel_ctx *ctx)
383 sdw_intel_cleanup(ctx);
388 EXPORT_SYMBOL_NS(sdw_intel_exit, SOUNDWIRE_INTEL_INIT);
390 void sdw_intel_process_wakeen_event(struct sdw_intel_ctx *ctx)
392 struct sdw_intel_link_dev *ldev;
399 link_mask = ctx->link_mask;
401 /* Startup SDW Master devices */
402 for (i = 0; i < ctx->count; i++) {
403 if (!(link_mask & BIT(i)))
408 intel_link_process_wakeen_event(&ldev->auxdev);
411 EXPORT_SYMBOL_NS(sdw_intel_process_wakeen_event, SOUNDWIRE_INTEL_INIT);
413 MODULE_LICENSE("Dual BSD/GPL");
414 MODULE_DESCRIPTION("Intel Soundwire Init Library");