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