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"
20 #include "intel_auxdevice.h"
22 static void intel_link_dev_release(struct device *dev)
24 struct auxiliary_device *auxdev = to_auxiliary_dev(dev);
25 struct sdw_intel_link_dev *ldev = auxiliary_dev_to_sdw_intel_link_dev(auxdev);
30 /* alloc, init and add link devices */
31 static struct sdw_intel_link_dev *intel_link_dev_register(struct sdw_intel_res *res,
32 struct sdw_intel_ctx *ctx,
33 struct fwnode_handle *fwnode,
37 struct sdw_intel_link_dev *ldev;
38 struct sdw_intel_link_res *link;
39 struct auxiliary_device *auxdev;
42 ldev = kzalloc(sizeof(*ldev), GFP_KERNEL);
44 return ERR_PTR(-ENOMEM);
46 auxdev = &ldev->auxdev;
48 auxdev->dev.parent = res->parent;
49 auxdev->dev.fwnode = fwnode;
50 auxdev->dev.release = intel_link_dev_release;
52 /* we don't use an IDA since we already have a link ID */
56 * keep a handle on the allocated memory, to be used in all other functions.
57 * Since the same pattern is used to skip links that are not enabled, there is
58 * no need to check if ctx->ldev[i] is NULL later on.
60 ctx->ldev[link_id] = ldev;
62 /* Add link information used in the driver probe */
63 link = &ldev->link_res;
64 link->hw_ops = res->hw_ops;
65 link->mmio_base = res->mmio_base;
67 link->registers = res->mmio_base + SDW_LINK_BASE
68 + (SDW_LINK_SIZE * link_id);
70 link->shim = res->mmio_base + res->shim_base;
71 link->alh = res->mmio_base + res->alh_base;
72 link->shim_lock = &ctx->shim_lock;
74 link->registers = res->mmio_base + SDW_IP_BASE(link_id);
75 link->ip_offset = SDW_CADENCE_MCP_IP_OFFSET;
76 link->shim = res->mmio_base + SDW_SHIM2_GENERIC_BASE(link_id);
77 link->shim_vs = res->mmio_base + SDW_SHIM2_VS_BASE(link_id);
78 link->shim_lock = res->eml_lock;
84 link->clock_stop_quirks = res->clock_stop_quirks;
85 link->shim_mask = &ctx->shim_mask;
86 link->link_mask = ctx->link_mask;
88 link->hbus = res->hbus;
90 /* now follow the two-step init/add sequence */
91 ret = auxiliary_device_init(auxdev);
93 dev_err(res->parent, "failed to initialize link dev %s link_id %d\n",
99 ret = auxiliary_device_add(&ldev->auxdev);
101 dev_err(res->parent, "failed to add link dev %s link_id %d\n",
102 ldev->auxdev.name, link_id);
103 /* ldev will be freed with the put_device() and .release sequence */
104 auxiliary_device_uninit(&ldev->auxdev);
111 static void intel_link_dev_unregister(struct sdw_intel_link_dev *ldev)
113 auxiliary_device_delete(&ldev->auxdev);
114 auxiliary_device_uninit(&ldev->auxdev);
117 static int sdw_intel_cleanup(struct sdw_intel_ctx *ctx)
119 struct sdw_intel_link_dev *ldev;
123 link_mask = ctx->link_mask;
125 for (i = 0; i < ctx->count; i++) {
126 if (!(link_mask & BIT(i)))
131 pm_runtime_disable(&ldev->auxdev.dev);
132 if (!ldev->link_res.clock_stop_quirks)
133 pm_runtime_put_noidle(ldev->link_res.dev);
135 intel_link_dev_unregister(ldev);
141 irqreturn_t sdw_intel_thread(int irq, void *dev_id)
143 struct sdw_intel_ctx *ctx = dev_id;
144 struct sdw_intel_link_res *link;
146 list_for_each_entry(link, &ctx->link_list, list)
147 sdw_cdns_irq(irq, link->cdns);
151 EXPORT_SYMBOL_NS(sdw_intel_thread, SOUNDWIRE_INTEL_INIT);
153 static struct sdw_intel_ctx
154 *sdw_intel_probe_controller(struct sdw_intel_res *res)
156 struct sdw_intel_link_res *link;
157 struct sdw_intel_link_dev *ldev;
158 struct sdw_intel_ctx *ctx;
159 struct acpi_device *adev;
160 struct sdw_slave *slave;
161 struct list_head *node;
171 adev = acpi_fetch_acpi_dev(res->handle);
179 dev_dbg(&adev->dev, "Creating %d SDW Link devices\n", count);
182 * we need to alloc/free memory manually and can't use devm:
183 * this routine may be called from a workqueue, and not from
185 * If devm_ was used, the memory might never be freed on errors.
187 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
194 * allocate the array of pointers. The link-specific data is allocated
195 * as part of the first loop below and released with the auxiliary_device_uninit().
196 * If some links are disabled, the link pointer will remain NULL. Given that the
197 * number of links is small, this is simpler than using a list to keep track of links.
199 ctx->ldev = kcalloc(ctx->count, sizeof(*ctx->ldev), GFP_KERNEL);
205 ctx->mmio_base = res->mmio_base;
206 ctx->shim_base = res->shim_base;
207 ctx->alh_base = res->alh_base;
208 ctx->link_mask = res->link_mask;
209 ctx->handle = res->handle;
210 mutex_init(&ctx->shim_lock);
212 link_mask = ctx->link_mask;
214 INIT_LIST_HEAD(&ctx->link_list);
216 for (i = 0; i < count; i++) {
217 if (!(link_mask & BIT(i)))
221 * init and add a device for each link
223 * The name of the device will be soundwire_intel.link.[i],
224 * with the "soundwire_intel" module prefix automatically added
225 * by the auxiliary bus core.
227 ldev = intel_link_dev_register(res,
229 acpi_fwnode_handle(adev),
235 link = &ldev->link_res;
236 link->cdns = auxiliary_get_drvdata(&ldev->auxdev);
239 dev_err(&adev->dev, "failed to get link->cdns\n");
241 * 1 will be subtracted from i in the err label, but we need to call
242 * intel_link_dev_unregister for this ldev, so plus 1 now
247 list_add_tail(&link->list, &ctx->link_list);
248 bus = &link->cdns->bus;
249 /* Calculate number of slaves */
250 list_for_each(node, &bus->slaves)
254 ctx->ids = kcalloc(num_slaves, sizeof(*ctx->ids), GFP_KERNEL);
258 ctx->num_slaves = num_slaves;
260 list_for_each_entry(link, &ctx->link_list, list) {
261 bus = &link->cdns->bus;
262 list_for_each_entry(slave, &bus->slaves, node) {
263 ctx->ids[i].id = slave->id;
264 ctx->ids[i].link_id = bus->link_id;
273 if (!(link_mask & BIT(i)))
276 intel_link_dev_unregister(ldev);
284 sdw_intel_startup_controller(struct sdw_intel_ctx *ctx)
286 struct acpi_device *adev = acpi_fetch_acpi_dev(ctx->handle);
287 struct sdw_intel_link_dev *ldev;
297 link_mask = ctx->link_mask;
299 /* Startup SDW Master devices */
300 for (i = 0; i < ctx->count; i++) {
301 if (!(link_mask & BIT(i)))
306 intel_link_startup(&ldev->auxdev);
308 if (!ldev->link_res.clock_stop_quirks) {
310 * we need to prevent the parent PCI device
311 * from entering pm_runtime suspend, so that
312 * power rails to the SoundWire IP are not
315 pm_runtime_get_noresume(ldev->link_res.dev);
323 * sdw_intel_probe() - SoundWire Intel probe routine
324 * @res: resource data
326 * This registers an auxiliary device for each Master handled by the controller,
327 * and SoundWire Master and Slave devices will be created by the auxiliary
328 * device probe. All the information necessary is stored in the context, and
329 * the res argument pointer can be freed after this step.
330 * This function will be called after sdw_intel_acpi_scan() by SOF probe.
333 *sdw_intel_probe(struct sdw_intel_res *res)
335 return sdw_intel_probe_controller(res);
337 EXPORT_SYMBOL_NS(sdw_intel_probe, SOUNDWIRE_INTEL_INIT);
340 * sdw_intel_startup() - SoundWire Intel startup
341 * @ctx: SoundWire context allocated in the probe
343 * Startup Intel SoundWire controller. This function will be called after
344 * Intel Audio DSP is powered up.
346 int sdw_intel_startup(struct sdw_intel_ctx *ctx)
348 return sdw_intel_startup_controller(ctx);
350 EXPORT_SYMBOL_NS(sdw_intel_startup, SOUNDWIRE_INTEL_INIT);
352 * sdw_intel_exit() - SoundWire Intel exit
353 * @ctx: SoundWire context allocated in the probe
355 * Delete the controller instances created and cleanup
357 void sdw_intel_exit(struct sdw_intel_ctx *ctx)
359 sdw_intel_cleanup(ctx);
364 EXPORT_SYMBOL_NS(sdw_intel_exit, SOUNDWIRE_INTEL_INIT);
366 void sdw_intel_process_wakeen_event(struct sdw_intel_ctx *ctx)
368 struct sdw_intel_link_dev *ldev;
375 link_mask = ctx->link_mask;
377 /* Startup SDW Master devices */
378 for (i = 0; i < ctx->count; i++) {
379 if (!(link_mask & BIT(i)))
384 intel_link_process_wakeen_event(&ldev->auxdev);
387 EXPORT_SYMBOL_NS(sdw_intel_process_wakeen_event, SOUNDWIRE_INTEL_INIT);
389 MODULE_LICENSE("Dual BSD/GPL");
390 MODULE_DESCRIPTION("Intel Soundwire Init Library");