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