1 // SPDX-License-Identifier: (GPL-2.0 OR BSD-3-Clause)
2 // Copyright(c) 2015-22 Intel Corporation.
5 * Soundwire Intel Manager Driver
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>
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"
24 #include "intel_auxdevice.h"
26 /* IDA min selected to avoid conflicts with HDaudio/iDISP SDI values */
27 #define INTEL_DEV_NUM_IDA_MIN 4
29 #define INTEL_MASTER_SUSPEND_DELAY_MS 3000
32 * debug/config flags for the Intel SoundWire Master.
34 * Since we may have multiple masters active, we can have up to 8
35 * flags reused in each byte, with master0 using the ls-byte, etc.
38 #define SDW_INTEL_MASTER_DISABLE_PM_RUNTIME BIT(0)
39 #define SDW_INTEL_MASTER_DISABLE_CLOCK_STOP BIT(1)
40 #define SDW_INTEL_MASTER_DISABLE_PM_RUNTIME_IDLE BIT(2)
41 #define SDW_INTEL_MASTER_DISABLE_MULTI_LINK BIT(3)
44 module_param_named(sdw_md_flags, md_flags, int, 0444);
45 MODULE_PARM_DESC(sdw_md_flags, "SoundWire Intel Master device flags (0x0 all off)");
47 static int generic_pre_bank_switch(struct sdw_bus *bus)
49 struct sdw_cdns *cdns = bus_to_cdns(bus);
50 struct sdw_intel *sdw = cdns_to_intel(cdns);
52 return sdw->link_res->hw_ops->pre_bank_switch(sdw);
55 static int generic_post_bank_switch(struct sdw_bus *bus)
57 struct sdw_cdns *cdns = bus_to_cdns(bus);
58 struct sdw_intel *sdw = cdns_to_intel(cdns);
60 return sdw->link_res->hw_ops->post_bank_switch(sdw);
63 static int sdw_master_read_intel_prop(struct sdw_bus *bus)
65 struct sdw_master_prop *prop = &bus->prop;
66 struct fwnode_handle *link;
70 /* Find master handle */
71 snprintf(name, sizeof(name),
72 "mipi-sdw-link-%d-subproperties", bus->link_id);
74 link = device_get_named_child_node(bus->dev, name);
76 dev_err(bus->dev, "Master node %s not found\n", name);
80 fwnode_property_read_u32(link,
84 /* the values reported by BIOS are the 2x clock, not the bus clock */
87 fwnode_property_read_u32(link,
91 if (quirk_mask & SDW_INTEL_QUIRK_MASK_BUS_DISABLE)
92 prop->hw_disabled = true;
94 prop->quirks = SDW_MASTER_QUIRKS_CLEAR_INITIAL_CLASH |
95 SDW_MASTER_QUIRKS_CLEAR_INITIAL_PARITY;
100 static int intel_prop_read(struct sdw_bus *bus)
102 /* Initialize with default handler to read all DisCo properties */
103 sdw_master_read_prop(bus);
105 /* read Intel-specific properties */
106 sdw_master_read_intel_prop(bus);
111 static struct sdw_master_ops sdw_intel_ops = {
112 .read_prop = intel_prop_read,
113 .override_adr = sdw_dmi_override_adr,
114 .xfer_msg = cdns_xfer_msg,
115 .xfer_msg_defer = cdns_xfer_msg_defer,
116 .set_bus_conf = cdns_bus_conf,
117 .pre_bank_switch = generic_pre_bank_switch,
118 .post_bank_switch = generic_post_bank_switch,
119 .read_ping_status = cdns_read_ping_status,
123 * probe and init (aux_dev_id argument is required by function prototype but not used)
125 static int intel_link_probe(struct auxiliary_device *auxdev,
126 const struct auxiliary_device_id *aux_dev_id)
129 struct device *dev = &auxdev->dev;
130 struct sdw_intel_link_dev *ldev = auxiliary_dev_to_sdw_intel_link_dev(auxdev);
131 struct sdw_intel *sdw;
132 struct sdw_cdns *cdns;
136 sdw = devm_kzalloc(dev, sizeof(*sdw), GFP_KERNEL);
143 sdw->instance = auxdev->id;
144 sdw->link_res = &ldev->link_res;
146 cdns->registers = sdw->link_res->registers;
147 cdns->instance = sdw->instance;
150 bus->link_id = auxdev->id;
151 bus->dev_num_ida_min = INTEL_DEV_NUM_IDA_MIN;
152 bus->clk_stop_timeout = 1;
154 sdw_cdns_probe(cdns);
157 bus->ops = &sdw_intel_ops;
159 /* set driver data, accessed by snd_soc_dai_get_drvdata() */
160 auxiliary_set_drvdata(auxdev, cdns);
162 /* use generic bandwidth allocation algorithm */
163 sdw->cdns.bus.compute_params = sdw_compute_params;
165 /* avoid resuming from pm_runtime suspend if it's not required */
166 dev_pm_set_driver_flags(dev, DPM_FLAG_SMART_SUSPEND);
168 ret = sdw_bus_master_add(bus, dev, dev->fwnode);
170 dev_err(dev, "sdw_bus_master_add fail: %d\n", ret);
174 if (bus->prop.hw_disabled)
176 "SoundWire master %d is disabled, will be ignored\n",
179 * Ignore BIOS err_threshold, it's a really bad idea when dealing
180 * with multiple hardware synchronized links
182 bus->prop.err_threshold = 0;
187 int intel_link_startup(struct auxiliary_device *auxdev)
189 struct device *dev = &auxdev->dev;
190 struct sdw_cdns *cdns = auxiliary_get_drvdata(auxdev);
191 struct sdw_intel *sdw = cdns_to_intel(cdns);
192 struct sdw_bus *bus = &cdns->bus;
195 u32 clock_stop_quirks;
198 if (bus->prop.hw_disabled) {
200 "SoundWire master %d is disabled, ignoring\n",
205 link_flags = md_flags >> (bus->link_id * 8);
206 multi_link = !(link_flags & SDW_INTEL_MASTER_DISABLE_MULTI_LINK);
208 dev_dbg(dev, "Multi-link is disabled\n");
211 * hardware-based synchronization is required regardless
212 * of the number of segments used by a stream: SSP-based
213 * synchronization is gated by gsync when the multi-master
216 bus->hw_sync_min_links = 1;
218 bus->multi_link = multi_link;
220 /* Initialize shim, controller */
221 ret = sdw_intel_link_power_up(sdw);
226 ret = sdw_intel_register_dai(sdw);
228 dev_err(dev, "DAI registration failed: %d\n", ret);
232 sdw_intel_debugfs_init(sdw);
235 ret = sdw_intel_start_bus(sdw);
237 dev_err(dev, "bus start failed: %d\n", ret);
241 /* Enable runtime PM */
242 if (!(link_flags & SDW_INTEL_MASTER_DISABLE_PM_RUNTIME)) {
243 pm_runtime_set_autosuspend_delay(dev,
244 INTEL_MASTER_SUSPEND_DELAY_MS);
245 pm_runtime_use_autosuspend(dev);
246 pm_runtime_mark_last_busy(dev);
248 pm_runtime_set_active(dev);
249 pm_runtime_enable(dev);
252 clock_stop_quirks = sdw->link_res->clock_stop_quirks;
253 if (clock_stop_quirks & SDW_INTEL_CLK_STOP_NOT_ALLOWED) {
255 * To keep the clock running we need to prevent
256 * pm_runtime suspend from happening by increasing the
258 * This quirk is specified by the parent PCI device in
259 * case of specific latency requirements. It will have
260 * no effect if pm_runtime is disabled by the user via
261 * a module parameter for testing purposes.
263 pm_runtime_get_noresume(dev);
267 * The runtime PM status of Slave devices is "Unsupported"
268 * until they report as ATTACHED. If they don't, e.g. because
269 * there are no Slave devices populated or if the power-on is
270 * delayed or dependent on a power switch, the Master will
271 * remain active and prevent its parent from suspending.
273 * Conditionally force the pm_runtime core to re-evaluate the
274 * Master status in the absence of any Slave activity. A quirk
275 * is provided to e.g. deal with Slaves that may be powered on
276 * with a delay. A more complete solution would require the
277 * definition of Master properties.
279 if (!(link_flags & SDW_INTEL_MASTER_DISABLE_PM_RUNTIME_IDLE))
280 pm_runtime_idle(dev);
282 sdw->startup_done = true;
286 sdw_intel_link_power_down(sdw);
291 static void intel_link_remove(struct auxiliary_device *auxdev)
293 struct sdw_cdns *cdns = auxiliary_get_drvdata(auxdev);
294 struct sdw_intel *sdw = cdns_to_intel(cdns);
295 struct sdw_bus *bus = &cdns->bus;
298 * Since pm_runtime is already disabled, we don't decrease
299 * the refcount when the clock_stop_quirk is
300 * SDW_INTEL_CLK_STOP_NOT_ALLOWED
302 if (!bus->prop.hw_disabled) {
303 sdw_intel_debugfs_exit(sdw);
304 sdw_cdns_enable_interrupt(cdns, false);
306 sdw_bus_master_delete(bus);
309 int intel_link_process_wakeen_event(struct auxiliary_device *auxdev)
311 struct device *dev = &auxdev->dev;
312 struct sdw_intel *sdw;
315 sdw = auxiliary_get_drvdata(auxdev);
316 bus = &sdw->cdns.bus;
318 if (bus->prop.hw_disabled || !sdw->startup_done) {
319 dev_dbg(dev, "SoundWire master %d is disabled or not-started, ignoring\n",
324 if (!sdw_intel_shim_check_wake(sdw))
327 /* disable WAKEEN interrupt ASAP to prevent interrupt flood */
328 sdw_intel_shim_wake(sdw, false);
331 * resume the Master, which will generate a bus reset and result in
332 * Slaves re-attaching and be re-enumerated. The SoundWire physical
333 * device which generated the wake will trigger an interrupt, which
334 * will in turn cause the corresponding Linux Slave device to be
335 * resumed and the Slave codec driver to check the status.
337 pm_request_resume(dev);
346 static int intel_resume_child_device(struct device *dev, void *data)
349 struct sdw_slave *slave = dev_to_sdw_dev(dev);
351 if (!slave->probed) {
352 dev_dbg(dev, "skipping device, no probed driver\n");
355 if (!slave->dev_num_sticky) {
356 dev_dbg(dev, "skipping device, never detected on bus\n");
360 ret = pm_request_resume(dev);
362 dev_err(dev, "%s: pm_request_resume failed: %d\n", __func__, ret);
369 static int __maybe_unused intel_pm_prepare(struct device *dev)
371 struct sdw_cdns *cdns = dev_get_drvdata(dev);
372 struct sdw_intel *sdw = cdns_to_intel(cdns);
373 struct sdw_bus *bus = &cdns->bus;
374 u32 clock_stop_quirks;
377 if (bus->prop.hw_disabled || !sdw->startup_done) {
378 dev_dbg(dev, "SoundWire master %d is disabled or not-started, ignoring\n",
383 clock_stop_quirks = sdw->link_res->clock_stop_quirks;
385 if (pm_runtime_suspended(dev) &&
386 pm_runtime_suspended(dev->parent) &&
387 ((clock_stop_quirks & SDW_INTEL_CLK_STOP_BUS_RESET) ||
388 !clock_stop_quirks)) {
390 * if we've enabled clock stop, and the parent is suspended, the SHIM registers
391 * are not accessible and the shim wake cannot be disabled.
392 * The only solution is to resume the entire bus to full power
396 * If any operation in this block fails, we keep going since we don't want
397 * to prevent system suspend from happening and errors should be recoverable
402 * first resume the device for this link. This will also by construction
403 * resume the PCI parent device.
405 ret = pm_request_resume(dev);
407 dev_err(dev, "%s: pm_request_resume failed: %d\n", __func__, ret);
412 * Continue resuming the entire bus (parent + child devices) to exit
413 * the clock stop mode. If there are no devices connected on this link
415 * The resume to full power could have been implemented with a .prepare
416 * step in SoundWire codec drivers. This would however require a lot
417 * of code to handle an Intel-specific corner case. It is simpler in
418 * practice to add a loop at the link level.
420 ret = device_for_each_child(bus->dev, NULL, intel_resume_child_device);
423 dev_err(dev, "%s: intel_resume_child_device failed: %d\n", __func__, ret);
429 static int __maybe_unused intel_suspend(struct device *dev)
431 struct sdw_cdns *cdns = dev_get_drvdata(dev);
432 struct sdw_intel *sdw = cdns_to_intel(cdns);
433 struct sdw_bus *bus = &cdns->bus;
434 u32 clock_stop_quirks;
437 if (bus->prop.hw_disabled || !sdw->startup_done) {
438 dev_dbg(dev, "SoundWire master %d is disabled or not-started, ignoring\n",
443 if (pm_runtime_suspended(dev)) {
444 dev_dbg(dev, "pm_runtime status: suspended\n");
446 clock_stop_quirks = sdw->link_res->clock_stop_quirks;
448 if ((clock_stop_quirks & SDW_INTEL_CLK_STOP_BUS_RESET) ||
449 !clock_stop_quirks) {
451 if (pm_runtime_suspended(dev->parent)) {
453 * paranoia check: this should not happen with the .prepare
454 * resume to full power
456 dev_err(dev, "%s: invalid config: parent is suspended\n", __func__);
458 sdw_intel_shim_wake(sdw, false);
465 ret = sdw_intel_stop_bus(sdw, false);
467 dev_err(dev, "%s: cannot stop bus: %d\n", __func__, ret);
474 static int __maybe_unused intel_suspend_runtime(struct device *dev)
476 struct sdw_cdns *cdns = dev_get_drvdata(dev);
477 struct sdw_intel *sdw = cdns_to_intel(cdns);
478 struct sdw_bus *bus = &cdns->bus;
479 u32 clock_stop_quirks;
482 if (bus->prop.hw_disabled || !sdw->startup_done) {
483 dev_dbg(dev, "SoundWire master %d is disabled or not-started, ignoring\n",
488 clock_stop_quirks = sdw->link_res->clock_stop_quirks;
490 if (clock_stop_quirks & SDW_INTEL_CLK_STOP_TEARDOWN) {
491 ret = sdw_intel_stop_bus(sdw, false);
493 dev_err(dev, "%s: cannot stop bus during teardown: %d\n",
497 } else if (clock_stop_quirks & SDW_INTEL_CLK_STOP_BUS_RESET || !clock_stop_quirks) {
498 ret = sdw_intel_stop_bus(sdw, true);
500 dev_err(dev, "%s: cannot stop bus during clock_stop: %d\n",
505 dev_err(dev, "%s clock_stop_quirks %x unsupported\n",
506 __func__, clock_stop_quirks);
513 static int __maybe_unused intel_resume(struct device *dev)
515 struct sdw_cdns *cdns = dev_get_drvdata(dev);
516 struct sdw_intel *sdw = cdns_to_intel(cdns);
517 struct sdw_bus *bus = &cdns->bus;
521 if (bus->prop.hw_disabled || !sdw->startup_done) {
522 dev_dbg(dev, "SoundWire master %d is disabled or not-started, ignoring\n",
527 link_flags = md_flags >> (bus->link_id * 8);
529 if (pm_runtime_suspended(dev)) {
530 dev_dbg(dev, "pm_runtime status was suspended, forcing active\n");
532 /* follow required sequence from runtime_pm.rst */
533 pm_runtime_disable(dev);
534 pm_runtime_set_active(dev);
535 pm_runtime_mark_last_busy(dev);
536 pm_runtime_enable(dev);
538 link_flags = md_flags >> (bus->link_id * 8);
540 if (!(link_flags & SDW_INTEL_MASTER_DISABLE_PM_RUNTIME_IDLE))
541 pm_runtime_idle(dev);
544 ret = sdw_intel_link_power_up(sdw);
546 dev_err(dev, "%s failed: %d\n", __func__, ret);
551 * make sure all Slaves are tagged as UNATTACHED and provide
552 * reason for reinitialization
554 sdw_clear_slave_status(bus, SDW_UNATTACH_REQUEST_MASTER_RESET);
556 ret = sdw_intel_start_bus(sdw);
558 dev_err(dev, "cannot start bus during resume\n");
559 sdw_intel_link_power_down(sdw);
564 * after system resume, the pm_runtime suspend() may kick in
565 * during the enumeration, before any children device force the
566 * master device to remain active. Using pm_runtime_get()
567 * routines is not really possible, since it'd prevent the
568 * master from suspending.
569 * A reasonable compromise is to update the pm_runtime
570 * counters and delay the pm_runtime suspend by several
571 * seconds, by when all enumeration should be complete.
573 pm_runtime_mark_last_busy(dev);
578 static int __maybe_unused intel_resume_runtime(struct device *dev)
580 struct sdw_cdns *cdns = dev_get_drvdata(dev);
581 struct sdw_intel *sdw = cdns_to_intel(cdns);
582 struct sdw_bus *bus = &cdns->bus;
583 u32 clock_stop_quirks;
586 if (bus->prop.hw_disabled || !sdw->startup_done) {
587 dev_dbg(dev, "SoundWire master %d is disabled or not-started, ignoring\n",
592 /* unconditionally disable WAKEEN interrupt */
593 sdw_intel_shim_wake(sdw, false);
595 clock_stop_quirks = sdw->link_res->clock_stop_quirks;
597 if (clock_stop_quirks & SDW_INTEL_CLK_STOP_TEARDOWN) {
598 ret = sdw_intel_link_power_up(sdw);
600 dev_err(dev, "%s: power_up failed after teardown: %d\n", __func__, ret);
605 * make sure all Slaves are tagged as UNATTACHED and provide
606 * reason for reinitialization
608 sdw_clear_slave_status(bus, SDW_UNATTACH_REQUEST_MASTER_RESET);
610 ret = sdw_intel_start_bus(sdw);
612 dev_err(dev, "%s: cannot start bus after teardown: %d\n", __func__, ret);
613 sdw_intel_link_power_down(sdw);
617 } else if (clock_stop_quirks & SDW_INTEL_CLK_STOP_BUS_RESET) {
618 ret = sdw_intel_link_power_up(sdw);
620 dev_err(dev, "%s: power_up failed after bus reset: %d\n", __func__, ret);
624 ret = sdw_intel_start_bus_after_reset(sdw);
626 dev_err(dev, "%s: cannot start bus after reset: %d\n", __func__, ret);
627 sdw_intel_link_power_down(sdw);
630 } else if (!clock_stop_quirks) {
632 sdw_intel_check_clock_stop(sdw);
634 ret = sdw_intel_link_power_up(sdw);
636 dev_err(dev, "%s: power_up failed: %d\n", __func__, ret);
640 ret = sdw_intel_start_bus_after_clock_stop(sdw);
642 dev_err(dev, "%s: cannot start bus after clock stop: %d\n", __func__, ret);
643 sdw_intel_link_power_down(sdw);
647 dev_err(dev, "%s: clock_stop_quirks %x unsupported\n",
648 __func__, clock_stop_quirks);
655 static const struct dev_pm_ops intel_pm = {
656 .prepare = intel_pm_prepare,
657 SET_SYSTEM_SLEEP_PM_OPS(intel_suspend, intel_resume)
658 SET_RUNTIME_PM_OPS(intel_suspend_runtime, intel_resume_runtime, NULL)
661 static const struct auxiliary_device_id intel_link_id_table[] = {
662 { .name = "soundwire_intel.link" },
665 MODULE_DEVICE_TABLE(auxiliary, intel_link_id_table);
667 static struct auxiliary_driver sdw_intel_drv = {
668 .probe = intel_link_probe,
669 .remove = intel_link_remove,
671 /* auxiliary_driver_register() sets .name to be the modname */
674 .id_table = intel_link_id_table
676 module_auxiliary_driver(sdw_intel_drv);
678 MODULE_LICENSE("Dual BSD/GPL");
679 MODULE_DESCRIPTION("Intel Soundwire Link Driver");