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