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