Linux Audio

Check our new training course

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