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