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