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