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