drm/panthor: Don't add write fences to the shared BOs
[drm/drm-misc.git] / drivers / soundwire / intel_init.c
bloba09134b97cd6667b0b5f6471cdff329cc1148ca4
1 // SPDX-License-Identifier: (GPL-2.0 OR BSD-3-Clause)
2 // Copyright(c) 2015-17 Intel Corporation.
4 /*
5 * SDW Intel Init Routines
7 * Initializes and creates SDW devices based on ACPI and Hardware values
8 */
10 #include <linux/acpi.h>
11 #include <linux/export.h>
12 #include <linux/interrupt.h>
13 #include <linux/io.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"
19 #include "bus.h"
20 #include "intel.h"
21 #include "intel_auxdevice.h"
23 static void intel_link_dev_release(struct device *dev)
25 struct auxiliary_device *auxdev = to_auxiliary_dev(dev);
26 struct sdw_intel_link_dev *ldev = auxiliary_dev_to_sdw_intel_link_dev(auxdev);
28 kfree(ldev);
31 /* alloc, init and add link devices */
32 static struct sdw_intel_link_dev *intel_link_dev_register(struct sdw_intel_res *res,
33 struct sdw_intel_ctx *ctx,
34 struct fwnode_handle *fwnode,
35 const char *name,
36 int link_id)
38 struct sdw_intel_link_dev *ldev;
39 struct sdw_intel_link_res *link;
40 struct auxiliary_device *auxdev;
41 int ret;
43 ldev = kzalloc(sizeof(*ldev), GFP_KERNEL);
44 if (!ldev)
45 return ERR_PTR(-ENOMEM);
47 auxdev = &ldev->auxdev;
48 auxdev->name = name;
49 auxdev->dev.parent = res->parent;
50 auxdev->dev.fwnode = fwnode;
51 auxdev->dev.release = intel_link_dev_release;
53 /* we don't use an IDA since we already have a link ID */
54 auxdev->id = link_id;
57 * keep a handle on the allocated memory, to be used in all other functions.
58 * Since the same pattern is used to skip links that are not enabled, there is
59 * no need to check if ctx->ldev[i] is NULL later on.
61 ctx->ldev[link_id] = ldev;
63 /* Add link information used in the driver probe */
64 link = &ldev->link_res;
65 link->hw_ops = res->hw_ops;
66 link->mmio_base = res->mmio_base;
67 if (!res->ext) {
68 link->registers = res->mmio_base + SDW_LINK_BASE
69 + (SDW_LINK_SIZE * link_id);
70 link->ip_offset = 0;
71 link->shim = res->mmio_base + res->shim_base;
72 link->alh = res->mmio_base + res->alh_base;
73 link->shim_lock = &ctx->shim_lock;
74 } else {
75 link->registers = res->mmio_base + SDW_IP_BASE(link_id);
76 link->ip_offset = SDW_CADENCE_MCP_IP_OFFSET;
77 link->shim = res->mmio_base + SDW_SHIM2_GENERIC_BASE(link_id);
78 link->shim_vs = res->mmio_base + SDW_SHIM2_VS_BASE(link_id);
79 link->shim_lock = res->eml_lock;
82 link->ops = res->ops;
83 link->dev = res->dev;
85 link->clock_stop_quirks = res->clock_stop_quirks;
86 link->shim_mask = &ctx->shim_mask;
87 link->link_mask = ctx->link_mask;
89 link->hbus = res->hbus;
91 /* now follow the two-step init/add sequence */
92 ret = auxiliary_device_init(auxdev);
93 if (ret < 0) {
94 dev_err(res->parent, "failed to initialize link dev %s link_id %d\n",
95 name, link_id);
96 kfree(ldev);
97 return ERR_PTR(ret);
100 ret = auxiliary_device_add(&ldev->auxdev);
101 if (ret < 0) {
102 dev_err(res->parent, "failed to add link dev %s link_id %d\n",
103 ldev->auxdev.name, link_id);
104 /* ldev will be freed with the put_device() and .release sequence */
105 auxiliary_device_uninit(&ldev->auxdev);
106 return ERR_PTR(ret);
109 return ldev;
112 static void intel_link_dev_unregister(struct sdw_intel_link_dev *ldev)
114 auxiliary_device_delete(&ldev->auxdev);
115 auxiliary_device_uninit(&ldev->auxdev);
118 static int sdw_intel_cleanup(struct sdw_intel_ctx *ctx)
120 struct sdw_intel_link_dev *ldev;
121 u32 link_mask;
122 int i;
124 link_mask = ctx->link_mask;
126 for (i = 0; i < ctx->count; i++) {
127 if (!(link_mask & BIT(i)))
128 continue;
130 ldev = ctx->ldev[i];
132 pm_runtime_disable(&ldev->auxdev.dev);
133 if (!ldev->link_res.clock_stop_quirks)
134 pm_runtime_put_noidle(ldev->link_res.dev);
136 intel_link_dev_unregister(ldev);
139 return 0;
142 irqreturn_t sdw_intel_thread(int irq, void *dev_id)
144 struct sdw_intel_ctx *ctx = dev_id;
145 struct sdw_intel_link_res *link;
147 list_for_each_entry(link, &ctx->link_list, list)
148 sdw_cdns_irq(irq, link->cdns);
150 return IRQ_HANDLED;
152 EXPORT_SYMBOL_NS(sdw_intel_thread, SOUNDWIRE_INTEL_INIT);
154 static struct sdw_intel_ctx
155 *sdw_intel_probe_controller(struct sdw_intel_res *res)
157 struct sdw_intel_link_res *link;
158 struct sdw_intel_link_dev *ldev;
159 struct sdw_intel_ctx *ctx;
160 struct acpi_device *adev;
161 struct sdw_slave *slave;
162 struct list_head *node;
163 struct sdw_bus *bus;
164 u32 link_mask;
165 int num_slaves = 0;
166 int count;
167 int i;
169 if (!res)
170 return NULL;
172 adev = acpi_fetch_acpi_dev(res->handle);
173 if (!adev)
174 return NULL;
176 if (!res->count)
177 return NULL;
179 count = res->count;
180 dev_dbg(&adev->dev, "Creating %d SDW Link devices\n", count);
183 * we need to alloc/free memory manually and can't use devm:
184 * this routine may be called from a workqueue, and not from
185 * the parent .probe.
186 * If devm_ was used, the memory might never be freed on errors.
188 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
189 if (!ctx)
190 return NULL;
192 ctx->count = count;
195 * allocate the array of pointers. The link-specific data is allocated
196 * as part of the first loop below and released with the auxiliary_device_uninit().
197 * If some links are disabled, the link pointer will remain NULL. Given that the
198 * number of links is small, this is simpler than using a list to keep track of links.
200 ctx->ldev = kcalloc(ctx->count, sizeof(*ctx->ldev), GFP_KERNEL);
201 if (!ctx->ldev) {
202 kfree(ctx);
203 return NULL;
206 ctx->mmio_base = res->mmio_base;
207 ctx->shim_base = res->shim_base;
208 ctx->alh_base = res->alh_base;
209 ctx->link_mask = res->link_mask;
210 ctx->handle = res->handle;
211 mutex_init(&ctx->shim_lock);
213 link_mask = ctx->link_mask;
215 INIT_LIST_HEAD(&ctx->link_list);
217 for (i = 0; i < count; i++) {
218 if (!(link_mask & BIT(i)))
219 continue;
222 * init and add a device for each link
224 * The name of the device will be soundwire_intel.link.[i],
225 * with the "soundwire_intel" module prefix automatically added
226 * by the auxiliary bus core.
228 ldev = intel_link_dev_register(res,
229 ctx,
230 acpi_fwnode_handle(adev),
231 "link",
233 if (IS_ERR(ldev))
234 goto err;
236 link = &ldev->link_res;
237 link->cdns = auxiliary_get_drvdata(&ldev->auxdev);
239 if (!link->cdns) {
240 dev_err(&adev->dev, "failed to get link->cdns\n");
242 * 1 will be subtracted from i in the err label, but we need to call
243 * intel_link_dev_unregister for this ldev, so plus 1 now
245 i++;
246 goto err;
248 list_add_tail(&link->list, &ctx->link_list);
249 bus = &link->cdns->bus;
250 /* Calculate number of slaves */
251 list_for_each(node, &bus->slaves)
252 num_slaves++;
255 ctx->ids = kcalloc(num_slaves, sizeof(*ctx->ids), GFP_KERNEL);
256 if (!ctx->ids)
257 goto err;
259 ctx->num_slaves = num_slaves;
260 i = 0;
261 list_for_each_entry(link, &ctx->link_list, list) {
262 bus = &link->cdns->bus;
263 list_for_each_entry(slave, &bus->slaves, node) {
264 ctx->ids[i].id = slave->id;
265 ctx->ids[i].link_id = bus->link_id;
266 i++;
270 return ctx;
272 err:
273 while (i--) {
274 if (!(link_mask & BIT(i)))
275 continue;
276 ldev = ctx->ldev[i];
277 intel_link_dev_unregister(ldev);
279 kfree(ctx->ldev);
280 kfree(ctx);
281 return NULL;
284 static int
285 sdw_intel_startup_controller(struct sdw_intel_ctx *ctx)
287 struct acpi_device *adev = acpi_fetch_acpi_dev(ctx->handle);
288 struct sdw_intel_link_dev *ldev;
289 u32 link_mask;
290 int i;
292 if (!adev)
293 return -EINVAL;
295 if (!ctx->ldev)
296 return -EINVAL;
298 link_mask = ctx->link_mask;
300 /* Startup SDW Master devices */
301 for (i = 0; i < ctx->count; i++) {
302 if (!(link_mask & BIT(i)))
303 continue;
305 ldev = ctx->ldev[i];
307 intel_link_startup(&ldev->auxdev);
309 if (!ldev->link_res.clock_stop_quirks) {
311 * we need to prevent the parent PCI device
312 * from entering pm_runtime suspend, so that
313 * power rails to the SoundWire IP are not
314 * turned off.
316 pm_runtime_get_noresume(ldev->link_res.dev);
320 return 0;
324 * sdw_intel_probe() - SoundWire Intel probe routine
325 * @res: resource data
327 * This registers an auxiliary device for each Master handled by the controller,
328 * and SoundWire Master and Slave devices will be created by the auxiliary
329 * device probe. All the information necessary is stored in the context, and
330 * the res argument pointer can be freed after this step.
331 * This function will be called after sdw_intel_acpi_scan() by SOF probe.
333 struct sdw_intel_ctx
334 *sdw_intel_probe(struct sdw_intel_res *res)
336 return sdw_intel_probe_controller(res);
338 EXPORT_SYMBOL_NS(sdw_intel_probe, SOUNDWIRE_INTEL_INIT);
341 * sdw_intel_startup() - SoundWire Intel startup
342 * @ctx: SoundWire context allocated in the probe
344 * Startup Intel SoundWire controller. This function will be called after
345 * Intel Audio DSP is powered up.
347 int sdw_intel_startup(struct sdw_intel_ctx *ctx)
349 return sdw_intel_startup_controller(ctx);
351 EXPORT_SYMBOL_NS(sdw_intel_startup, SOUNDWIRE_INTEL_INIT);
353 * sdw_intel_exit() - SoundWire Intel exit
354 * @ctx: SoundWire context allocated in the probe
356 * Delete the controller instances created and cleanup
358 void sdw_intel_exit(struct sdw_intel_ctx *ctx)
360 struct sdw_intel_link_res *link;
362 /* we first resume links and devices and wait synchronously before the cleanup */
363 list_for_each_entry(link, &ctx->link_list, list) {
364 struct sdw_bus *bus = &link->cdns->bus;
365 int ret;
367 ret = device_for_each_child(bus->dev, NULL, intel_resume_child_device);
368 if (ret < 0)
369 dev_err(bus->dev, "%s: intel_resume_child_device failed: %d\n",
370 __func__, ret);
373 sdw_intel_cleanup(ctx);
374 kfree(ctx->ids);
375 kfree(ctx->ldev);
376 kfree(ctx);
378 EXPORT_SYMBOL_NS(sdw_intel_exit, SOUNDWIRE_INTEL_INIT);
380 void sdw_intel_process_wakeen_event(struct sdw_intel_ctx *ctx)
382 struct sdw_intel_link_dev *ldev;
383 u32 link_mask;
384 int i;
386 if (!ctx->ldev)
387 return;
389 link_mask = ctx->link_mask;
391 /* Startup SDW Master devices */
392 for (i = 0; i < ctx->count; i++) {
393 if (!(link_mask & BIT(i)))
394 continue;
396 ldev = ctx->ldev[i];
398 intel_link_process_wakeen_event(&ldev->auxdev);
401 EXPORT_SYMBOL_NS(sdw_intel_process_wakeen_event, SOUNDWIRE_INTEL_INIT);
403 MODULE_LICENSE("Dual BSD/GPL");
404 MODULE_DESCRIPTION("Intel Soundwire Init Library");