xref: /linux/drivers/soundwire/intel_auxdevice.c (revision 3ff78451b8e446e9a548b98a0d4dd8d24dc5780b)
1 // SPDX-License-Identifier: (GPL-2.0 OR BSD-3-Clause)
2 // Copyright(c) 2015-22 Intel Corporation.
3 
4 /*
5  * Soundwire Intel Manager Driver
6  */
7 
8 #include <linux/acpi.h>
9 #include <linux/debugfs.h>
10 #include <linux/delay.h>
11 #include <linux/module.h>
12 #include <linux/interrupt.h>
13 #include <linux/io.h>
14 #include <linux/auxiliary_bus.h>
15 #include <sound/pcm_params.h>
16 #include <linux/pm_runtime.h>
17 #include <sound/soc.h>
18 #include <linux/soundwire/sdw_registers.h>
19 #include <linux/soundwire/sdw.h>
20 #include <linux/soundwire/sdw_intel.h>
21 #include "cadence_master.h"
22 #include "bus.h"
23 #include "intel.h"
24 #include "intel_auxdevice.h"
25 
26 #define INTEL_MASTER_SUSPEND_DELAY_MS	3000
27 
28 /*
29  * debug/config flags for the Intel SoundWire Master.
30  *
31  * Since we may have multiple masters active, we can have up to 8
32  * flags reused in each byte, with master0 using the ls-byte, etc.
33  */
34 
35 #define SDW_INTEL_MASTER_DISABLE_PM_RUNTIME		BIT(0)
36 #define SDW_INTEL_MASTER_DISABLE_CLOCK_STOP		BIT(1)
37 #define SDW_INTEL_MASTER_DISABLE_PM_RUNTIME_IDLE	BIT(2)
38 #define SDW_INTEL_MASTER_DISABLE_MULTI_LINK		BIT(3)
39 
40 static int md_flags;
41 module_param_named(sdw_md_flags, md_flags, int, 0444);
42 MODULE_PARM_DESC(sdw_md_flags, "SoundWire Intel Master device flags (0x0 all off)");
43 
44 struct wake_capable_part {
45 	const u16 mfg_id;
46 	const u16 part_id;
47 };
48 
49 static struct wake_capable_part wake_capable_list[] = {
50 	{0x025d, 0x5682},
51 	{0x025d, 0x700},
52 	{0x025d, 0x711},
53 	{0x025d, 0x1712},
54 	{0x025d, 0x1713},
55 	{0x025d, 0x1716},
56 	{0x025d, 0x1717},
57 	{0x025d, 0x712},
58 	{0x025d, 0x713},
59 	{0x025d, 0x714},
60 	{0x025d, 0x715},
61 	{0x025d, 0x716},
62 	{0x025d, 0x717},
63 	{0x025d, 0x722},
64 };
65 
66 static bool is_wake_capable(struct sdw_slave *slave)
67 {
68 	int i;
69 
70 	for (i = 0; i < ARRAY_SIZE(wake_capable_list); i++)
71 		if (slave->id.part_id == wake_capable_list[i].part_id &&
72 		    slave->id.mfg_id == wake_capable_list[i].mfg_id)
73 			return true;
74 	return false;
75 }
76 
77 static int generic_pre_bank_switch(struct sdw_bus *bus)
78 {
79 	struct sdw_cdns *cdns = bus_to_cdns(bus);
80 	struct sdw_intel *sdw = cdns_to_intel(cdns);
81 
82 	return sdw->link_res->hw_ops->pre_bank_switch(sdw);
83 }
84 
85 static int generic_post_bank_switch(struct sdw_bus *bus)
86 {
87 	struct sdw_cdns *cdns = bus_to_cdns(bus);
88 	struct sdw_intel *sdw = cdns_to_intel(cdns);
89 
90 	return sdw->link_res->hw_ops->post_bank_switch(sdw);
91 }
92 
93 static void generic_new_peripheral_assigned(struct sdw_bus *bus,
94 					    struct sdw_slave *slave,
95 					    int dev_num)
96 {
97 	struct sdw_cdns *cdns = bus_to_cdns(bus);
98 	struct sdw_intel *sdw = cdns_to_intel(cdns);
99 	int dev_num_min;
100 	int dev_num_max;
101 	bool wake_capable = slave->prop.wake_capable || is_wake_capable(slave);
102 
103 	if (wake_capable) {
104 		dev_num_min = SDW_INTEL_DEV_NUM_IDA_MIN;
105 		dev_num_max = SDW_MAX_DEVICES;
106 	} else {
107 		dev_num_min = 1;
108 		dev_num_max = SDW_INTEL_DEV_NUM_IDA_MIN - 1;
109 	}
110 
111 	/* paranoia check, this should never happen */
112 	if (dev_num < dev_num_min || dev_num > dev_num_max)  {
113 		dev_err(bus->dev, "%s: invalid dev_num %d, wake supported %d\n",
114 			__func__, dev_num, slave->prop.wake_capable);
115 		return;
116 	}
117 
118 	if (sdw->link_res->hw_ops->program_sdi && wake_capable)
119 		sdw->link_res->hw_ops->program_sdi(sdw, dev_num);
120 }
121 
122 static int sdw_master_read_intel_prop(struct sdw_bus *bus)
123 {
124 	struct sdw_master_prop *prop = &bus->prop;
125 	struct sdw_intel_prop *intel_prop;
126 	struct fwnode_handle *link;
127 	char name[32];
128 	u32 quirk_mask;
129 
130 	/* Find master handle */
131 	snprintf(name, sizeof(name),
132 		 "mipi-sdw-link-%d-subproperties", bus->link_id);
133 
134 	link = device_get_named_child_node(bus->dev, name);
135 	if (!link) {
136 		dev_err(bus->dev, "Master node %s not found\n", name);
137 		return -EIO;
138 	}
139 
140 	fwnode_property_read_u32(link,
141 				 "intel-sdw-ip-clock",
142 				 &prop->mclk_freq);
143 
144 	/* the values reported by BIOS are the 2x clock, not the bus clock */
145 	prop->mclk_freq /= 2;
146 
147 	fwnode_property_read_u32(link,
148 				 "intel-quirk-mask",
149 				 &quirk_mask);
150 
151 	if (quirk_mask & SDW_INTEL_QUIRK_MASK_BUS_DISABLE)
152 		prop->hw_disabled = true;
153 
154 	prop->quirks = SDW_MASTER_QUIRKS_CLEAR_INITIAL_CLASH |
155 		SDW_MASTER_QUIRKS_CLEAR_INITIAL_PARITY;
156 
157 	intel_prop = devm_kzalloc(bus->dev, sizeof(*intel_prop), GFP_KERNEL);
158 	if (!intel_prop)
159 		return -ENOMEM;
160 
161 	/* initialize with hardware defaults, in case the properties are not found */
162 	intel_prop->doaise = 0x1;
163 	intel_prop->doais = 0x3;
164 	intel_prop->dodse  = 0x0;
165 	intel_prop->dods  = 0x1;
166 
167 	fwnode_property_read_u16(link,
168 				 "intel-sdw-doaise",
169 				 &intel_prop->doaise);
170 	fwnode_property_read_u16(link,
171 				 "intel-sdw-doais",
172 				 &intel_prop->doais);
173 	fwnode_property_read_u16(link,
174 				 "intel-sdw-dodse",
175 				 &intel_prop->dodse);
176 	fwnode_property_read_u16(link,
177 				 "intel-sdw-dods",
178 				 &intel_prop->dods);
179 	bus->vendor_specific_prop = intel_prop;
180 
181 	dev_dbg(bus->dev, "doaise %#x doais %#x dodse %#x dods %#x\n",
182 		intel_prop->doaise,
183 		intel_prop->doais,
184 		intel_prop->dodse,
185 		intel_prop->dods);
186 
187 	return 0;
188 }
189 
190 static int intel_prop_read(struct sdw_bus *bus)
191 {
192 	/* Initialize with default handler to read all DisCo properties */
193 	sdw_master_read_prop(bus);
194 
195 	/* read Intel-specific properties */
196 	sdw_master_read_intel_prop(bus);
197 
198 	return 0;
199 }
200 
201 static DEFINE_IDA(intel_peripheral_ida);
202 
203 static int intel_get_device_num_ida(struct sdw_bus *bus, struct sdw_slave *slave)
204 {
205 	int bit;
206 
207 	if (slave->prop.wake_capable || is_wake_capable(slave))
208 		return ida_alloc_range(&intel_peripheral_ida,
209 				       SDW_INTEL_DEV_NUM_IDA_MIN, SDW_MAX_DEVICES,
210 				       GFP_KERNEL);
211 
212 	bit = find_first_zero_bit(slave->bus->assigned, SDW_MAX_DEVICES);
213 	if (bit == SDW_MAX_DEVICES)
214 		return -ENODEV;
215 
216 	return bit;
217 }
218 
219 static void intel_put_device_num_ida(struct sdw_bus *bus, struct sdw_slave *slave)
220 {
221 	if (slave->prop.wake_capable || is_wake_capable(slave))
222 		ida_free(&intel_peripheral_ida, slave->dev_num);
223 }
224 
225 static struct sdw_master_ops sdw_intel_ops = {
226 	.read_prop = intel_prop_read,
227 	.override_adr = sdw_dmi_override_adr,
228 	.xfer_msg = cdns_xfer_msg,
229 	.xfer_msg_defer = cdns_xfer_msg_defer,
230 	.set_bus_conf = cdns_bus_conf,
231 	.pre_bank_switch = generic_pre_bank_switch,
232 	.post_bank_switch = generic_post_bank_switch,
233 	.read_ping_status = cdns_read_ping_status,
234 	.get_device_num =  intel_get_device_num_ida,
235 	.put_device_num = intel_put_device_num_ida,
236 	.new_peripheral_assigned = generic_new_peripheral_assigned,
237 };
238 
239 /*
240  * probe and init (aux_dev_id argument is required by function prototype but not used)
241  */
242 static int intel_link_probe(struct auxiliary_device *auxdev,
243 			    const struct auxiliary_device_id *aux_dev_id)
244 
245 {
246 	struct device *dev = &auxdev->dev;
247 	struct sdw_intel_link_dev *ldev = auxiliary_dev_to_sdw_intel_link_dev(auxdev);
248 	struct sdw_intel *sdw;
249 	struct sdw_cdns *cdns;
250 	struct sdw_bus *bus;
251 	int ret;
252 
253 	sdw = devm_kzalloc(dev, sizeof(*sdw), GFP_KERNEL);
254 	if (!sdw)
255 		return -ENOMEM;
256 
257 	cdns = &sdw->cdns;
258 	bus = &cdns->bus;
259 
260 	sdw->instance = auxdev->id;
261 	sdw->link_res = &ldev->link_res;
262 	cdns->dev = dev;
263 	cdns->registers = sdw->link_res->registers;
264 	cdns->ip_offset = sdw->link_res->ip_offset;
265 	cdns->instance = sdw->instance;
266 	cdns->msg_count = 0;
267 
268 	/* single controller for all SoundWire links */
269 	bus->controller_id = 0;
270 
271 	bus->link_id = auxdev->id;
272 	bus->clk_stop_timeout = 1;
273 
274 	sdw_cdns_probe(cdns);
275 
276 	/* Set ops */
277 	bus->ops = &sdw_intel_ops;
278 
279 	/* set driver data, accessed by snd_soc_dai_get_drvdata() */
280 	auxiliary_set_drvdata(auxdev, cdns);
281 
282 	/* use generic bandwidth allocation algorithm */
283 	sdw->cdns.bus.compute_params = sdw_compute_params;
284 
285 	/* avoid resuming from pm_runtime suspend if it's not required */
286 	dev_pm_set_driver_flags(dev, DPM_FLAG_SMART_SUSPEND);
287 
288 	ret = sdw_bus_master_add(bus, dev, dev->fwnode);
289 	if (ret) {
290 		dev_err(dev, "sdw_bus_master_add fail: %d\n", ret);
291 		return ret;
292 	}
293 
294 	if (bus->prop.hw_disabled)
295 		dev_info(dev,
296 			 "SoundWire master %d is disabled, will be ignored\n",
297 			 bus->link_id);
298 	/*
299 	 * Ignore BIOS err_threshold, it's a really bad idea when dealing
300 	 * with multiple hardware synchronized links
301 	 */
302 	bus->prop.err_threshold = 0;
303 
304 	return 0;
305 }
306 
307 int intel_link_startup(struct auxiliary_device *auxdev)
308 {
309 	struct device *dev = &auxdev->dev;
310 	struct sdw_cdns *cdns = auxiliary_get_drvdata(auxdev);
311 	struct sdw_intel *sdw = cdns_to_intel(cdns);
312 	struct sdw_bus *bus = &cdns->bus;
313 	int link_flags;
314 	bool multi_link;
315 	u32 clock_stop_quirks;
316 	int ret;
317 
318 	if (bus->prop.hw_disabled) {
319 		dev_info(dev,
320 			 "SoundWire master %d is disabled, ignoring\n",
321 			 sdw->instance);
322 		return 0;
323 	}
324 
325 	link_flags = md_flags >> (bus->link_id * 8);
326 	multi_link = !(link_flags & SDW_INTEL_MASTER_DISABLE_MULTI_LINK);
327 	if (!multi_link) {
328 		dev_dbg(dev, "Multi-link is disabled\n");
329 	} else {
330 		/*
331 		 * hardware-based synchronization is required regardless
332 		 * of the number of segments used by a stream: SSP-based
333 		 * synchronization is gated by gsync when the multi-master
334 		 * mode is set.
335 		 */
336 		bus->hw_sync_min_links = 1;
337 	}
338 	bus->multi_link = multi_link;
339 
340 	/* Initialize shim, controller */
341 	ret = sdw_intel_link_power_up(sdw);
342 	if (ret)
343 		goto err_init;
344 
345 	/* Register DAIs */
346 	ret = sdw_intel_register_dai(sdw);
347 	if (ret) {
348 		dev_err(dev, "DAI registration failed: %d\n", ret);
349 		goto err_power_up;
350 	}
351 
352 	sdw_intel_debugfs_init(sdw);
353 
354 	/* Enable runtime PM */
355 	if (!(link_flags & SDW_INTEL_MASTER_DISABLE_PM_RUNTIME)) {
356 		pm_runtime_set_autosuspend_delay(dev,
357 						 INTEL_MASTER_SUSPEND_DELAY_MS);
358 		pm_runtime_use_autosuspend(dev);
359 		pm_runtime_mark_last_busy(dev);
360 
361 		pm_runtime_set_active(dev);
362 		pm_runtime_enable(dev);
363 
364 		pm_runtime_resume(bus->dev);
365 	}
366 
367 	/* start bus */
368 	ret = sdw_intel_start_bus(sdw);
369 	if (ret) {
370 		dev_err(dev, "bus start failed: %d\n", ret);
371 		goto err_pm_runtime;
372 	}
373 
374 	clock_stop_quirks = sdw->link_res->clock_stop_quirks;
375 	if (clock_stop_quirks & SDW_INTEL_CLK_STOP_NOT_ALLOWED) {
376 		/*
377 		 * To keep the clock running we need to prevent
378 		 * pm_runtime suspend from happening by increasing the
379 		 * reference count.
380 		 * This quirk is specified by the parent PCI device in
381 		 * case of specific latency requirements. It will have
382 		 * no effect if pm_runtime is disabled by the user via
383 		 * a module parameter for testing purposes.
384 		 */
385 		pm_runtime_get_noresume(dev);
386 	}
387 
388 	/*
389 	 * The runtime PM status of Slave devices is "Unsupported"
390 	 * until they report as ATTACHED. If they don't, e.g. because
391 	 * there are no Slave devices populated or if the power-on is
392 	 * delayed or dependent on a power switch, the Master will
393 	 * remain active and prevent its parent from suspending.
394 	 *
395 	 * Conditionally force the pm_runtime core to re-evaluate the
396 	 * Master status in the absence of any Slave activity. A quirk
397 	 * is provided to e.g. deal with Slaves that may be powered on
398 	 * with a delay. A more complete solution would require the
399 	 * definition of Master properties.
400 	 */
401 	if (!(link_flags & SDW_INTEL_MASTER_DISABLE_PM_RUNTIME_IDLE)) {
402 		pm_runtime_mark_last_busy(bus->dev);
403 		pm_runtime_mark_last_busy(dev);
404 		pm_runtime_idle(dev);
405 	}
406 
407 	sdw->startup_done = true;
408 	return 0;
409 
410 err_pm_runtime:
411 	if (!(link_flags & SDW_INTEL_MASTER_DISABLE_PM_RUNTIME))
412 		pm_runtime_disable(dev);
413 err_power_up:
414 	sdw_intel_link_power_down(sdw);
415 err_init:
416 	return ret;
417 }
418 
419 static void intel_link_remove(struct auxiliary_device *auxdev)
420 {
421 	struct sdw_cdns *cdns = auxiliary_get_drvdata(auxdev);
422 	struct sdw_intel *sdw = cdns_to_intel(cdns);
423 	struct sdw_bus *bus = &cdns->bus;
424 
425 	/*
426 	 * Since pm_runtime is already disabled, we don't decrease
427 	 * the refcount when the clock_stop_quirk is
428 	 * SDW_INTEL_CLK_STOP_NOT_ALLOWED
429 	 */
430 	if (!bus->prop.hw_disabled) {
431 		sdw_intel_debugfs_exit(sdw);
432 		sdw_cdns_enable_interrupt(cdns, false);
433 	}
434 	sdw_bus_master_delete(bus);
435 }
436 
437 int intel_link_process_wakeen_event(struct auxiliary_device *auxdev)
438 {
439 	struct device *dev = &auxdev->dev;
440 	struct sdw_intel *sdw;
441 	struct sdw_bus *bus;
442 
443 	sdw = auxiliary_get_drvdata(auxdev);
444 	bus = &sdw->cdns.bus;
445 
446 	if (bus->prop.hw_disabled || !sdw->startup_done) {
447 		dev_dbg(dev, "SoundWire master %d is disabled or not-started, ignoring\n",
448 			bus->link_id);
449 		return 0;
450 	}
451 
452 	if (!sdw_intel_shim_check_wake(sdw))
453 		return 0;
454 
455 	/* disable WAKEEN interrupt ASAP to prevent interrupt flood */
456 	sdw_intel_shim_wake(sdw, false);
457 
458 	/*
459 	 * resume the Master, which will generate a bus reset and result in
460 	 * Slaves re-attaching and be re-enumerated. The SoundWire physical
461 	 * device which generated the wake will trigger an interrupt, which
462 	 * will in turn cause the corresponding Linux Slave device to be
463 	 * resumed and the Slave codec driver to check the status.
464 	 */
465 	pm_request_resume(dev);
466 
467 	return 0;
468 }
469 
470 /*
471  * PM calls
472  */
473 
474 int intel_resume_child_device(struct device *dev, void *data)
475 {
476 	int ret;
477 	struct sdw_slave *slave = dev_to_sdw_dev(dev);
478 
479 	if (!slave->probed) {
480 		dev_dbg(dev, "skipping device, no probed driver\n");
481 		return 0;
482 	}
483 	if (!slave->dev_num_sticky) {
484 		dev_dbg(dev, "skipping device, never detected on bus\n");
485 		return 0;
486 	}
487 
488 	ret = pm_runtime_resume(dev);
489 	if (ret < 0) {
490 		dev_err(dev, "%s: pm_runtime_resume failed: %d\n", __func__, ret);
491 		return ret;
492 	}
493 
494 	return 0;
495 }
496 
497 static int __maybe_unused intel_pm_prepare(struct device *dev)
498 {
499 	struct sdw_cdns *cdns = dev_get_drvdata(dev);
500 	struct sdw_intel *sdw = cdns_to_intel(cdns);
501 	struct sdw_bus *bus = &cdns->bus;
502 	u32 clock_stop_quirks;
503 	int ret;
504 
505 	if (bus->prop.hw_disabled || !sdw->startup_done) {
506 		dev_dbg(dev, "SoundWire master %d is disabled or not-started, ignoring\n",
507 			bus->link_id);
508 		return 0;
509 	}
510 
511 	clock_stop_quirks = sdw->link_res->clock_stop_quirks;
512 
513 	if (pm_runtime_suspended(dev) &&
514 	    pm_runtime_suspended(dev->parent) &&
515 	    ((clock_stop_quirks & SDW_INTEL_CLK_STOP_BUS_RESET) ||
516 	     !clock_stop_quirks)) {
517 		/*
518 		 * if we've enabled clock stop, and the parent is suspended, the SHIM registers
519 		 * are not accessible and the shim wake cannot be disabled.
520 		 * The only solution is to resume the entire bus to full power
521 		 */
522 
523 		/*
524 		 * If any operation in this block fails, we keep going since we don't want
525 		 * to prevent system suspend from happening and errors should be recoverable
526 		 * on resume.
527 		 */
528 
529 		/*
530 		 * first resume the device for this link. This will also by construction
531 		 * resume the PCI parent device.
532 		 */
533 		ret = pm_runtime_resume(dev);
534 		if (ret < 0) {
535 			dev_err(dev, "%s: pm_runtime_resume failed: %d\n", __func__, ret);
536 			return 0;
537 		}
538 
539 		/*
540 		 * Continue resuming the entire bus (parent + child devices) to exit
541 		 * the clock stop mode. If there are no devices connected on this link
542 		 * this is a no-op.
543 		 * The resume to full power could have been implemented with a .prepare
544 		 * step in SoundWire codec drivers. This would however require a lot
545 		 * of code to handle an Intel-specific corner case. It is simpler in
546 		 * practice to add a loop at the link level.
547 		 */
548 		ret = device_for_each_child(bus->dev, NULL, intel_resume_child_device);
549 
550 		if (ret < 0)
551 			dev_err(dev, "%s: intel_resume_child_device failed: %d\n", __func__, ret);
552 	}
553 
554 	return 0;
555 }
556 
557 static int __maybe_unused intel_suspend(struct device *dev)
558 {
559 	struct sdw_cdns *cdns = dev_get_drvdata(dev);
560 	struct sdw_intel *sdw = cdns_to_intel(cdns);
561 	struct sdw_bus *bus = &cdns->bus;
562 	u32 clock_stop_quirks;
563 	int ret;
564 
565 	if (bus->prop.hw_disabled || !sdw->startup_done) {
566 		dev_dbg(dev, "SoundWire master %d is disabled or not-started, ignoring\n",
567 			bus->link_id);
568 		return 0;
569 	}
570 
571 	if (pm_runtime_suspended(dev)) {
572 		dev_dbg(dev, "pm_runtime status: suspended\n");
573 
574 		clock_stop_quirks = sdw->link_res->clock_stop_quirks;
575 
576 		if ((clock_stop_quirks & SDW_INTEL_CLK_STOP_BUS_RESET) ||
577 		    !clock_stop_quirks) {
578 
579 			if (pm_runtime_suspended(dev->parent)) {
580 				/*
581 				 * paranoia check: this should not happen with the .prepare
582 				 * resume to full power
583 				 */
584 				dev_err(dev, "%s: invalid config: parent is suspended\n", __func__);
585 			} else {
586 				sdw_intel_shim_wake(sdw, false);
587 			}
588 		}
589 
590 		return 0;
591 	}
592 
593 	ret = sdw_intel_stop_bus(sdw, false);
594 	if (ret < 0) {
595 		dev_err(dev, "%s: cannot stop bus: %d\n", __func__, ret);
596 		return ret;
597 	}
598 
599 	return 0;
600 }
601 
602 static int __maybe_unused intel_suspend_runtime(struct device *dev)
603 {
604 	struct sdw_cdns *cdns = dev_get_drvdata(dev);
605 	struct sdw_intel *sdw = cdns_to_intel(cdns);
606 	struct sdw_bus *bus = &cdns->bus;
607 	u32 clock_stop_quirks;
608 	int ret;
609 
610 	if (bus->prop.hw_disabled || !sdw->startup_done) {
611 		dev_dbg(dev, "SoundWire master %d is disabled or not-started, ignoring\n",
612 			bus->link_id);
613 		return 0;
614 	}
615 
616 	clock_stop_quirks = sdw->link_res->clock_stop_quirks;
617 
618 	if (clock_stop_quirks & SDW_INTEL_CLK_STOP_TEARDOWN) {
619 		ret = sdw_intel_stop_bus(sdw, false);
620 		if (ret < 0) {
621 			dev_err(dev, "%s: cannot stop bus during teardown: %d\n",
622 				__func__, ret);
623 			return ret;
624 		}
625 	} else if (clock_stop_quirks & SDW_INTEL_CLK_STOP_BUS_RESET || !clock_stop_quirks) {
626 		ret = sdw_intel_stop_bus(sdw, true);
627 		if (ret < 0) {
628 			dev_err(dev, "%s: cannot stop bus during clock_stop: %d\n",
629 				__func__, ret);
630 			return ret;
631 		}
632 	} else {
633 		dev_err(dev, "%s clock_stop_quirks %x unsupported\n",
634 			__func__, clock_stop_quirks);
635 		ret = -EINVAL;
636 	}
637 
638 	return ret;
639 }
640 
641 static int __maybe_unused intel_resume(struct device *dev)
642 {
643 	struct sdw_cdns *cdns = dev_get_drvdata(dev);
644 	struct sdw_intel *sdw = cdns_to_intel(cdns);
645 	struct sdw_bus *bus = &cdns->bus;
646 	int link_flags;
647 	int ret;
648 
649 	if (bus->prop.hw_disabled || !sdw->startup_done) {
650 		dev_dbg(dev, "SoundWire master %d is disabled or not-started, ignoring\n",
651 			bus->link_id);
652 		return 0;
653 	}
654 
655 	if (pm_runtime_suspended(dev)) {
656 		dev_dbg(dev, "pm_runtime status was suspended, forcing active\n");
657 
658 		/* follow required sequence from runtime_pm.rst */
659 		pm_runtime_disable(dev);
660 		pm_runtime_set_active(dev);
661 		pm_runtime_mark_last_busy(dev);
662 		pm_runtime_enable(dev);
663 
664 		pm_runtime_resume(bus->dev);
665 
666 		link_flags = md_flags >> (bus->link_id * 8);
667 
668 		if (!(link_flags & SDW_INTEL_MASTER_DISABLE_PM_RUNTIME_IDLE))
669 			pm_runtime_idle(dev);
670 	}
671 
672 	ret = sdw_intel_link_power_up(sdw);
673 	if (ret) {
674 		dev_err(dev, "%s failed: %d\n", __func__, ret);
675 		return ret;
676 	}
677 
678 	/*
679 	 * make sure all Slaves are tagged as UNATTACHED and provide
680 	 * reason for reinitialization
681 	 */
682 	sdw_clear_slave_status(bus, SDW_UNATTACH_REQUEST_MASTER_RESET);
683 
684 	ret = sdw_intel_start_bus(sdw);
685 	if (ret < 0) {
686 		dev_err(dev, "cannot start bus during resume\n");
687 		sdw_intel_link_power_down(sdw);
688 		return ret;
689 	}
690 
691 	/*
692 	 * after system resume, the pm_runtime suspend() may kick in
693 	 * during the enumeration, before any children device force the
694 	 * master device to remain active.  Using pm_runtime_get()
695 	 * routines is not really possible, since it'd prevent the
696 	 * master from suspending.
697 	 * A reasonable compromise is to update the pm_runtime
698 	 * counters and delay the pm_runtime suspend by several
699 	 * seconds, by when all enumeration should be complete.
700 	 */
701 	pm_runtime_mark_last_busy(bus->dev);
702 	pm_runtime_mark_last_busy(dev);
703 
704 	return 0;
705 }
706 
707 static int __maybe_unused intel_resume_runtime(struct device *dev)
708 {
709 	struct sdw_cdns *cdns = dev_get_drvdata(dev);
710 	struct sdw_intel *sdw = cdns_to_intel(cdns);
711 	struct sdw_bus *bus = &cdns->bus;
712 	u32 clock_stop_quirks;
713 	int ret;
714 
715 	if (bus->prop.hw_disabled || !sdw->startup_done) {
716 		dev_dbg(dev, "SoundWire master %d is disabled or not-started, ignoring\n",
717 			bus->link_id);
718 		return 0;
719 	}
720 
721 	/* unconditionally disable WAKEEN interrupt */
722 	sdw_intel_shim_wake(sdw, false);
723 
724 	clock_stop_quirks = sdw->link_res->clock_stop_quirks;
725 
726 	if (clock_stop_quirks & SDW_INTEL_CLK_STOP_TEARDOWN) {
727 		ret = sdw_intel_link_power_up(sdw);
728 		if (ret) {
729 			dev_err(dev, "%s: power_up failed after teardown: %d\n", __func__, ret);
730 			return ret;
731 		}
732 
733 		/*
734 		 * make sure all Slaves are tagged as UNATTACHED and provide
735 		 * reason for reinitialization
736 		 */
737 		sdw_clear_slave_status(bus, SDW_UNATTACH_REQUEST_MASTER_RESET);
738 
739 		ret = sdw_intel_start_bus(sdw);
740 		if (ret < 0) {
741 			dev_err(dev, "%s: cannot start bus after teardown: %d\n", __func__, ret);
742 			sdw_intel_link_power_down(sdw);
743 			return ret;
744 		}
745 
746 	} else if (clock_stop_quirks & SDW_INTEL_CLK_STOP_BUS_RESET) {
747 		ret = sdw_intel_link_power_up(sdw);
748 		if (ret) {
749 			dev_err(dev, "%s: power_up failed after bus reset: %d\n", __func__, ret);
750 			return ret;
751 		}
752 
753 		ret = sdw_intel_start_bus_after_reset(sdw);
754 		if (ret < 0) {
755 			dev_err(dev, "%s: cannot start bus after reset: %d\n", __func__, ret);
756 			sdw_intel_link_power_down(sdw);
757 			return ret;
758 		}
759 	} else if (!clock_stop_quirks) {
760 
761 		sdw_intel_check_clock_stop(sdw);
762 
763 		ret = sdw_intel_link_power_up(sdw);
764 		if (ret) {
765 			dev_err(dev, "%s: power_up failed: %d\n", __func__, ret);
766 			return ret;
767 		}
768 
769 		ret = sdw_intel_start_bus_after_clock_stop(sdw);
770 		if (ret < 0) {
771 			dev_err(dev, "%s: cannot start bus after clock stop: %d\n", __func__, ret);
772 			sdw_intel_link_power_down(sdw);
773 			return ret;
774 		}
775 	} else {
776 		dev_err(dev, "%s: clock_stop_quirks %x unsupported\n",
777 			__func__, clock_stop_quirks);
778 		ret = -EINVAL;
779 	}
780 
781 	return ret;
782 }
783 
784 static const struct dev_pm_ops intel_pm = {
785 	.prepare = intel_pm_prepare,
786 	SET_SYSTEM_SLEEP_PM_OPS(intel_suspend, intel_resume)
787 	SET_RUNTIME_PM_OPS(intel_suspend_runtime, intel_resume_runtime, NULL)
788 };
789 
790 static const struct auxiliary_device_id intel_link_id_table[] = {
791 	{ .name = "soundwire_intel.link" },
792 	{},
793 };
794 MODULE_DEVICE_TABLE(auxiliary, intel_link_id_table);
795 
796 static struct auxiliary_driver sdw_intel_drv = {
797 	.probe = intel_link_probe,
798 	.remove = intel_link_remove,
799 	.driver = {
800 		/* auxiliary_driver_register() sets .name to be the modname */
801 		.pm = &intel_pm,
802 	},
803 	.id_table = intel_link_id_table
804 };
805 module_auxiliary_driver(sdw_intel_drv);
806 
807 MODULE_LICENSE("Dual BSD/GPL");
808 MODULE_DESCRIPTION("Intel Soundwire Link Driver");
809