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