xref: /linux/drivers/soundwire/intel_init.c (revision 2fc60e2ff972d3dca836bff0b08cbe503c4ca1ce)
1 // SPDX-License-Identifier: (GPL-2.0 OR BSD-3-Clause)
2 // Copyright(c) 2015-17 Intel Corporation.
3 
4 /*
5  * SDW Intel Init Routines
6  *
7  * Initializes and creates SDW devices based on ACPI and Hardware values
8  */
9 
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 "intel.h"
20 
21 static void intel_link_dev_release(struct device *dev)
22 {
23 	struct auxiliary_device *auxdev = to_auxiliary_dev(dev);
24 	struct sdw_intel_link_dev *ldev = auxiliary_dev_to_sdw_intel_link_dev(auxdev);
25 
26 	kfree(ldev);
27 }
28 
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,
33 							  const char *name,
34 							  int link_id)
35 {
36 	struct sdw_intel_link_dev *ldev;
37 	struct sdw_intel_link_res *link;
38 	struct auxiliary_device *auxdev;
39 	int ret;
40 
41 	ldev = kzalloc(sizeof(*ldev), GFP_KERNEL);
42 	if (!ldev)
43 		return ERR_PTR(-ENOMEM);
44 
45 	auxdev = &ldev->auxdev;
46 	auxdev->name = name;
47 	auxdev->dev.parent = res->parent;
48 	auxdev->dev.fwnode = fwnode;
49 	auxdev->dev.release = intel_link_dev_release;
50 
51 	/* we don't use an IDA since we already have a link ID */
52 	auxdev->id = link_id;
53 
54 	/*
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.
58 	 */
59 	ctx->ldev[link_id] = ldev;
60 
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;
68 
69 	link->ops = res->ops;
70 	link->dev = res->dev;
71 
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;
76 
77 	/* now follow the two-step init/add sequence */
78 	ret = auxiliary_device_init(auxdev);
79 	if (ret < 0) {
80 		dev_err(res->parent, "failed to initialize link dev %s link_id %d\n",
81 			name, link_id);
82 		kfree(ldev);
83 		return ERR_PTR(ret);
84 	}
85 
86 	ret = auxiliary_device_add(&ldev->auxdev);
87 	if (ret < 0) {
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);
92 		return ERR_PTR(ret);
93 	}
94 
95 	return ldev;
96 }
97 
98 static void intel_link_dev_unregister(struct sdw_intel_link_dev *ldev)
99 {
100 	auxiliary_device_delete(&ldev->auxdev);
101 	auxiliary_device_uninit(&ldev->auxdev);
102 }
103 
104 static int sdw_intel_cleanup(struct sdw_intel_ctx *ctx)
105 {
106 	struct sdw_intel_link_dev *ldev;
107 	u32 link_mask;
108 	int i;
109 
110 	link_mask = ctx->link_mask;
111 
112 	for (i = 0; i < ctx->count; i++) {
113 		if (!(link_mask & BIT(i)))
114 			continue;
115 
116 		ldev = ctx->ldev[i];
117 
118 		pm_runtime_disable(&ldev->auxdev.dev);
119 		if (!ldev->link_res.clock_stop_quirks)
120 			pm_runtime_put_noidle(ldev->link_res.dev);
121 
122 		intel_link_dev_unregister(ldev);
123 	}
124 
125 	return 0;
126 }
127 
128 irqreturn_t sdw_intel_thread(int irq, void *dev_id)
129 {
130 	struct sdw_intel_ctx *ctx = dev_id;
131 	struct sdw_intel_link_res *link;
132 
133 	list_for_each_entry(link, &ctx->link_list, list)
134 		sdw_cdns_irq(irq, link->cdns);
135 
136 	return IRQ_HANDLED;
137 }
138 EXPORT_SYMBOL_NS(sdw_intel_thread, SOUNDWIRE_INTEL_INIT);
139 
140 static struct sdw_intel_ctx
141 *sdw_intel_probe_controller(struct sdw_intel_res *res)
142 {
143 	struct sdw_intel_link_res *link;
144 	struct sdw_intel_link_dev *ldev;
145 	struct sdw_intel_ctx *ctx;
146 	struct acpi_device *adev;
147 	struct sdw_slave *slave;
148 	struct list_head *node;
149 	struct sdw_bus *bus;
150 	u32 link_mask;
151 	int num_slaves = 0;
152 	int count;
153 	int i;
154 
155 	if (!res)
156 		return NULL;
157 
158 	adev = acpi_fetch_acpi_dev(res->handle);
159 	if (!adev)
160 		return NULL;
161 
162 	if (!res->count)
163 		return NULL;
164 
165 	count = res->count;
166 	dev_dbg(&adev->dev, "Creating %d SDW Link devices\n", count);
167 
168 	/*
169 	 * we need to alloc/free memory manually and can't use devm:
170 	 * this routine may be called from a workqueue, and not from
171 	 * the parent .probe.
172 	 * If devm_ was used, the memory might never be freed on errors.
173 	 */
174 	ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
175 	if (!ctx)
176 		return NULL;
177 
178 	ctx->count = count;
179 
180 	/*
181 	 * allocate the array of pointers. The link-specific data is allocated
182 	 * as part of the first loop below and released with the auxiliary_device_uninit().
183 	 * If some links are disabled, the link pointer will remain NULL. Given that the
184 	 * number of links is small, this is simpler than using a list to keep track of links.
185 	 */
186 	ctx->ldev = kcalloc(ctx->count, sizeof(*ctx->ldev), GFP_KERNEL);
187 	if (!ctx->ldev) {
188 		kfree(ctx);
189 		return NULL;
190 	}
191 
192 	ctx->mmio_base = res->mmio_base;
193 	ctx->shim_base = res->shim_base;
194 	ctx->alh_base = res->alh_base;
195 	ctx->link_mask = res->link_mask;
196 	ctx->handle = res->handle;
197 	mutex_init(&ctx->shim_lock);
198 
199 	link_mask = ctx->link_mask;
200 
201 	INIT_LIST_HEAD(&ctx->link_list);
202 
203 	for (i = 0; i < count; i++) {
204 		if (!(link_mask & BIT(i)))
205 			continue;
206 
207 		/*
208 		 * init and add a device for each link
209 		 *
210 		 * The name of the device will be soundwire_intel.link.[i],
211 		 * with the "soundwire_intel" module prefix automatically added
212 		 * by the auxiliary bus core.
213 		 */
214 		ldev = intel_link_dev_register(res,
215 					       ctx,
216 					       acpi_fwnode_handle(adev),
217 					       "link",
218 					       i);
219 		if (IS_ERR(ldev))
220 			goto err;
221 
222 		link = &ldev->link_res;
223 		link->cdns = auxiliary_get_drvdata(&ldev->auxdev);
224 
225 		if (!link->cdns) {
226 			dev_err(&adev->dev, "failed to get link->cdns\n");
227 			/*
228 			 * 1 will be subtracted from i in the err label, but we need to call
229 			 * intel_link_dev_unregister for this ldev, so plus 1 now
230 			 */
231 			i++;
232 			goto err;
233 		}
234 		list_add_tail(&link->list, &ctx->link_list);
235 		bus = &link->cdns->bus;
236 		/* Calculate number of slaves */
237 		list_for_each(node, &bus->slaves)
238 			num_slaves++;
239 	}
240 
241 	ctx->ids = kcalloc(num_slaves, sizeof(*ctx->ids), GFP_KERNEL);
242 	if (!ctx->ids)
243 		goto err;
244 
245 	ctx->num_slaves = num_slaves;
246 	i = 0;
247 	list_for_each_entry(link, &ctx->link_list, list) {
248 		bus = &link->cdns->bus;
249 		list_for_each_entry(slave, &bus->slaves, node) {
250 			ctx->ids[i].id = slave->id;
251 			ctx->ids[i].link_id = bus->link_id;
252 			i++;
253 		}
254 	}
255 
256 	return ctx;
257 
258 err:
259 	while (i--) {
260 		if (!(link_mask & BIT(i)))
261 			continue;
262 		ldev = ctx->ldev[i];
263 		intel_link_dev_unregister(ldev);
264 	}
265 	kfree(ctx->ldev);
266 	kfree(ctx);
267 	return NULL;
268 }
269 
270 static int
271 sdw_intel_startup_controller(struct sdw_intel_ctx *ctx)
272 {
273 	struct acpi_device *adev = acpi_fetch_acpi_dev(ctx->handle);
274 	struct sdw_intel_link_dev *ldev;
275 	u32 link_mask;
276 	int i;
277 
278 	if (!adev)
279 		return -EINVAL;
280 
281 	if (!ctx->ldev)
282 		return -EINVAL;
283 
284 	link_mask = ctx->link_mask;
285 
286 	/* Startup SDW Master devices */
287 	for (i = 0; i < ctx->count; i++) {
288 		if (!(link_mask & BIT(i)))
289 			continue;
290 
291 		ldev = ctx->ldev[i];
292 
293 		intel_link_startup(&ldev->auxdev);
294 
295 		if (!ldev->link_res.clock_stop_quirks) {
296 			/*
297 			 * we need to prevent the parent PCI device
298 			 * from entering pm_runtime suspend, so that
299 			 * power rails to the SoundWire IP are not
300 			 * turned off.
301 			 */
302 			pm_runtime_get_noresume(ldev->link_res.dev);
303 		}
304 	}
305 
306 	return 0;
307 }
308 
309 /**
310  * sdw_intel_probe() - SoundWire Intel probe routine
311  * @res: resource data
312  *
313  * This registers an auxiliary device for each Master handled by the controller,
314  * and SoundWire Master and Slave devices will be created by the auxiliary
315  * device probe. All the information necessary is stored in the context, and
316  * the res argument pointer can be freed after this step.
317  * This function will be called after sdw_intel_acpi_scan() by SOF probe.
318  */
319 struct sdw_intel_ctx
320 *sdw_intel_probe(struct sdw_intel_res *res)
321 {
322 	return sdw_intel_probe_controller(res);
323 }
324 EXPORT_SYMBOL_NS(sdw_intel_probe, SOUNDWIRE_INTEL_INIT);
325 
326 /**
327  * sdw_intel_startup() - SoundWire Intel startup
328  * @ctx: SoundWire context allocated in the probe
329  *
330  * Startup Intel SoundWire controller. This function will be called after
331  * Intel Audio DSP is powered up.
332  */
333 int sdw_intel_startup(struct sdw_intel_ctx *ctx)
334 {
335 	return sdw_intel_startup_controller(ctx);
336 }
337 EXPORT_SYMBOL_NS(sdw_intel_startup, SOUNDWIRE_INTEL_INIT);
338 /**
339  * sdw_intel_exit() - SoundWire Intel exit
340  * @ctx: SoundWire context allocated in the probe
341  *
342  * Delete the controller instances created and cleanup
343  */
344 void sdw_intel_exit(struct sdw_intel_ctx *ctx)
345 {
346 	sdw_intel_cleanup(ctx);
347 	kfree(ctx->ids);
348 	kfree(ctx->ldev);
349 	kfree(ctx);
350 }
351 EXPORT_SYMBOL_NS(sdw_intel_exit, SOUNDWIRE_INTEL_INIT);
352 
353 void sdw_intel_process_wakeen_event(struct sdw_intel_ctx *ctx)
354 {
355 	struct sdw_intel_link_dev *ldev;
356 	u32 link_mask;
357 	int i;
358 
359 	if (!ctx->ldev)
360 		return;
361 
362 	link_mask = ctx->link_mask;
363 
364 	/* Startup SDW Master devices */
365 	for (i = 0; i < ctx->count; i++) {
366 		if (!(link_mask & BIT(i)))
367 			continue;
368 
369 		ldev = ctx->ldev[i];
370 
371 		intel_link_process_wakeen_event(&ldev->auxdev);
372 	}
373 }
374 EXPORT_SYMBOL_NS(sdw_intel_process_wakeen_event, SOUNDWIRE_INTEL_INIT);
375 
376 MODULE_LICENSE("Dual BSD/GPL");
377 MODULE_DESCRIPTION("Intel Soundwire Init Library");
378