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 void generic_new_peripheral_assigned(struct sdw_bus *bus, int dev_num)
65 struct sdw_cdns *cdns = bus_to_cdns(bus);
66 struct sdw_intel *sdw = cdns_to_intel(cdns);
68 /* paranoia check, this should never happen */
69 if (dev_num < INTEL_DEV_NUM_IDA_MIN || dev_num > SDW_MAX_DEVICES) {
70 dev_err(bus->dev, "%s: invalid dev_num %d\n", __func__, dev_num);
74 if (sdw->link_res->hw_ops->program_sdi)
75 sdw->link_res->hw_ops->program_sdi(sdw, dev_num);
78 static int sdw_master_read_intel_prop(struct sdw_bus *bus)
80 struct sdw_master_prop *prop = &bus->prop;
81 struct fwnode_handle *link;
85 /* Find master handle */
86 snprintf(name, sizeof(name),
87 "mipi-sdw-link-%d-subproperties", bus->link_id);
89 link = device_get_named_child_node(bus->dev, name);
91 dev_err(bus->dev, "Master node %s not found\n", name);
95 fwnode_property_read_u32(link,
99 /* the values reported by BIOS are the 2x clock, not the bus clock */
100 prop->mclk_freq /= 2;
102 fwnode_property_read_u32(link,
106 if (quirk_mask & SDW_INTEL_QUIRK_MASK_BUS_DISABLE)
107 prop->hw_disabled = true;
109 prop->quirks = SDW_MASTER_QUIRKS_CLEAR_INITIAL_CLASH |
110 SDW_MASTER_QUIRKS_CLEAR_INITIAL_PARITY;
115 static int intel_prop_read(struct sdw_bus *bus)
117 /* Initialize with default handler to read all DisCo properties */
118 sdw_master_read_prop(bus);
120 /* read Intel-specific properties */
121 sdw_master_read_intel_prop(bus);
126 static struct sdw_master_ops sdw_intel_ops = {
127 .read_prop = intel_prop_read,
128 .override_adr = sdw_dmi_override_adr,
129 .xfer_msg = cdns_xfer_msg,
130 .xfer_msg_defer = cdns_xfer_msg_defer,
131 .set_bus_conf = cdns_bus_conf,
132 .pre_bank_switch = generic_pre_bank_switch,
133 .post_bank_switch = generic_post_bank_switch,
134 .read_ping_status = cdns_read_ping_status,
135 .new_peripheral_assigned = generic_new_peripheral_assigned,
139 * probe and init (aux_dev_id argument is required by function prototype but not used)
141 static int intel_link_probe(struct auxiliary_device *auxdev,
142 const struct auxiliary_device_id *aux_dev_id)
145 struct device *dev = &auxdev->dev;
146 struct sdw_intel_link_dev *ldev = auxiliary_dev_to_sdw_intel_link_dev(auxdev);
147 struct sdw_intel *sdw;
148 struct sdw_cdns *cdns;
152 sdw = devm_kzalloc(dev, sizeof(*sdw), GFP_KERNEL);
159 sdw->instance = auxdev->id;
160 sdw->link_res = &ldev->link_res;
162 cdns->registers = sdw->link_res->registers;
163 cdns->ip_offset = sdw->link_res->ip_offset;
164 cdns->instance = sdw->instance;
167 bus->link_id = auxdev->id;
168 bus->dev_num_ida_min = INTEL_DEV_NUM_IDA_MIN;
169 bus->clk_stop_timeout = 1;
171 sdw_cdns_probe(cdns);
174 bus->ops = &sdw_intel_ops;
176 /* set driver data, accessed by snd_soc_dai_get_drvdata() */
177 auxiliary_set_drvdata(auxdev, cdns);
179 /* use generic bandwidth allocation algorithm */
180 sdw->cdns.bus.compute_params = sdw_compute_params;
182 /* avoid resuming from pm_runtime suspend if it's not required */
183 dev_pm_set_driver_flags(dev, DPM_FLAG_SMART_SUSPEND);
185 ret = sdw_bus_master_add(bus, dev, dev->fwnode);
187 dev_err(dev, "sdw_bus_master_add fail: %d\n", ret);
191 if (bus->prop.hw_disabled)
193 "SoundWire master %d is disabled, will be ignored\n",
196 * Ignore BIOS err_threshold, it's a really bad idea when dealing
197 * with multiple hardware synchronized links
199 bus->prop.err_threshold = 0;
204 int intel_link_startup(struct auxiliary_device *auxdev)
206 struct device *dev = &auxdev->dev;
207 struct sdw_cdns *cdns = auxiliary_get_drvdata(auxdev);
208 struct sdw_intel *sdw = cdns_to_intel(cdns);
209 struct sdw_bus *bus = &cdns->bus;
212 u32 clock_stop_quirks;
215 if (bus->prop.hw_disabled) {
217 "SoundWire master %d is disabled, ignoring\n",
222 link_flags = md_flags >> (bus->link_id * 8);
223 multi_link = !(link_flags & SDW_INTEL_MASTER_DISABLE_MULTI_LINK);
225 dev_dbg(dev, "Multi-link is disabled\n");
228 * hardware-based synchronization is required regardless
229 * of the number of segments used by a stream: SSP-based
230 * synchronization is gated by gsync when the multi-master
233 bus->hw_sync_min_links = 1;
235 bus->multi_link = multi_link;
237 /* Initialize shim, controller */
238 ret = sdw_intel_link_power_up(sdw);
243 ret = sdw_intel_register_dai(sdw);
245 dev_err(dev, "DAI registration failed: %d\n", ret);
249 sdw_intel_debugfs_init(sdw);
252 ret = sdw_intel_start_bus(sdw);
254 dev_err(dev, "bus start failed: %d\n", ret);
258 /* Enable runtime PM */
259 if (!(link_flags & SDW_INTEL_MASTER_DISABLE_PM_RUNTIME)) {
260 pm_runtime_set_autosuspend_delay(dev,
261 INTEL_MASTER_SUSPEND_DELAY_MS);
262 pm_runtime_use_autosuspend(dev);
263 pm_runtime_mark_last_busy(dev);
265 pm_runtime_set_active(dev);
266 pm_runtime_enable(dev);
269 clock_stop_quirks = sdw->link_res->clock_stop_quirks;
270 if (clock_stop_quirks & SDW_INTEL_CLK_STOP_NOT_ALLOWED) {
272 * To keep the clock running we need to prevent
273 * pm_runtime suspend from happening by increasing the
275 * This quirk is specified by the parent PCI device in
276 * case of specific latency requirements. It will have
277 * no effect if pm_runtime is disabled by the user via
278 * a module parameter for testing purposes.
280 pm_runtime_get_noresume(dev);
284 * The runtime PM status of Slave devices is "Unsupported"
285 * until they report as ATTACHED. If they don't, e.g. because
286 * there are no Slave devices populated or if the power-on is
287 * delayed or dependent on a power switch, the Master will
288 * remain active and prevent its parent from suspending.
290 * Conditionally force the pm_runtime core to re-evaluate the
291 * Master status in the absence of any Slave activity. A quirk
292 * is provided to e.g. deal with Slaves that may be powered on
293 * with a delay. A more complete solution would require the
294 * definition of Master properties.
296 if (!(link_flags & SDW_INTEL_MASTER_DISABLE_PM_RUNTIME_IDLE))
297 pm_runtime_idle(dev);
299 sdw->startup_done = true;
303 sdw_intel_link_power_down(sdw);
308 static void intel_link_remove(struct auxiliary_device *auxdev)
310 struct sdw_cdns *cdns = auxiliary_get_drvdata(auxdev);
311 struct sdw_intel *sdw = cdns_to_intel(cdns);
312 struct sdw_bus *bus = &cdns->bus;
315 * Since pm_runtime is already disabled, we don't decrease
316 * the refcount when the clock_stop_quirk is
317 * SDW_INTEL_CLK_STOP_NOT_ALLOWED
319 if (!bus->prop.hw_disabled) {
320 sdw_intel_debugfs_exit(sdw);
321 sdw_cdns_enable_interrupt(cdns, false);
323 sdw_bus_master_delete(bus);
326 int intel_link_process_wakeen_event(struct auxiliary_device *auxdev)
328 struct device *dev = &auxdev->dev;
329 struct sdw_intel *sdw;
332 sdw = auxiliary_get_drvdata(auxdev);
333 bus = &sdw->cdns.bus;
335 if (bus->prop.hw_disabled || !sdw->startup_done) {
336 dev_dbg(dev, "SoundWire master %d is disabled or not-started, ignoring\n",
341 if (!sdw_intel_shim_check_wake(sdw))
344 /* disable WAKEEN interrupt ASAP to prevent interrupt flood */
345 sdw_intel_shim_wake(sdw, false);
348 * resume the Master, which will generate a bus reset and result in
349 * Slaves re-attaching and be re-enumerated. The SoundWire physical
350 * device which generated the wake will trigger an interrupt, which
351 * will in turn cause the corresponding Linux Slave device to be
352 * resumed and the Slave codec driver to check the status.
354 pm_request_resume(dev);
363 static int intel_resume_child_device(struct device *dev, void *data)
366 struct sdw_slave *slave = dev_to_sdw_dev(dev);
368 if (!slave->probed) {
369 dev_dbg(dev, "skipping device, no probed driver\n");
372 if (!slave->dev_num_sticky) {
373 dev_dbg(dev, "skipping device, never detected on bus\n");
377 ret = pm_request_resume(dev);
379 dev_err(dev, "%s: pm_request_resume failed: %d\n", __func__, ret);
386 static int __maybe_unused intel_pm_prepare(struct device *dev)
388 struct sdw_cdns *cdns = dev_get_drvdata(dev);
389 struct sdw_intel *sdw = cdns_to_intel(cdns);
390 struct sdw_bus *bus = &cdns->bus;
391 u32 clock_stop_quirks;
394 if (bus->prop.hw_disabled || !sdw->startup_done) {
395 dev_dbg(dev, "SoundWire master %d is disabled or not-started, ignoring\n",
400 clock_stop_quirks = sdw->link_res->clock_stop_quirks;
402 if (pm_runtime_suspended(dev) &&
403 pm_runtime_suspended(dev->parent) &&
404 ((clock_stop_quirks & SDW_INTEL_CLK_STOP_BUS_RESET) ||
405 !clock_stop_quirks)) {
407 * if we've enabled clock stop, and the parent is suspended, the SHIM registers
408 * are not accessible and the shim wake cannot be disabled.
409 * The only solution is to resume the entire bus to full power
413 * If any operation in this block fails, we keep going since we don't want
414 * to prevent system suspend from happening and errors should be recoverable
419 * first resume the device for this link. This will also by construction
420 * resume the PCI parent device.
422 ret = pm_request_resume(dev);
424 dev_err(dev, "%s: pm_request_resume failed: %d\n", __func__, ret);
429 * Continue resuming the entire bus (parent + child devices) to exit
430 * the clock stop mode. If there are no devices connected on this link
432 * The resume to full power could have been implemented with a .prepare
433 * step in SoundWire codec drivers. This would however require a lot
434 * of code to handle an Intel-specific corner case. It is simpler in
435 * practice to add a loop at the link level.
437 ret = device_for_each_child(bus->dev, NULL, intel_resume_child_device);
440 dev_err(dev, "%s: intel_resume_child_device failed: %d\n", __func__, ret);
446 static int __maybe_unused intel_suspend(struct device *dev)
448 struct sdw_cdns *cdns = dev_get_drvdata(dev);
449 struct sdw_intel *sdw = cdns_to_intel(cdns);
450 struct sdw_bus *bus = &cdns->bus;
451 u32 clock_stop_quirks;
454 if (bus->prop.hw_disabled || !sdw->startup_done) {
455 dev_dbg(dev, "SoundWire master %d is disabled or not-started, ignoring\n",
460 if (pm_runtime_suspended(dev)) {
461 dev_dbg(dev, "pm_runtime status: suspended\n");
463 clock_stop_quirks = sdw->link_res->clock_stop_quirks;
465 if ((clock_stop_quirks & SDW_INTEL_CLK_STOP_BUS_RESET) ||
466 !clock_stop_quirks) {
468 if (pm_runtime_suspended(dev->parent)) {
470 * paranoia check: this should not happen with the .prepare
471 * resume to full power
473 dev_err(dev, "%s: invalid config: parent is suspended\n", __func__);
475 sdw_intel_shim_wake(sdw, false);
482 ret = sdw_intel_stop_bus(sdw, false);
484 dev_err(dev, "%s: cannot stop bus: %d\n", __func__, ret);
491 static int __maybe_unused intel_suspend_runtime(struct device *dev)
493 struct sdw_cdns *cdns = dev_get_drvdata(dev);
494 struct sdw_intel *sdw = cdns_to_intel(cdns);
495 struct sdw_bus *bus = &cdns->bus;
496 u32 clock_stop_quirks;
499 if (bus->prop.hw_disabled || !sdw->startup_done) {
500 dev_dbg(dev, "SoundWire master %d is disabled or not-started, ignoring\n",
505 clock_stop_quirks = sdw->link_res->clock_stop_quirks;
507 if (clock_stop_quirks & SDW_INTEL_CLK_STOP_TEARDOWN) {
508 ret = sdw_intel_stop_bus(sdw, false);
510 dev_err(dev, "%s: cannot stop bus during teardown: %d\n",
514 } else if (clock_stop_quirks & SDW_INTEL_CLK_STOP_BUS_RESET || !clock_stop_quirks) {
515 ret = sdw_intel_stop_bus(sdw, true);
517 dev_err(dev, "%s: cannot stop bus during clock_stop: %d\n",
522 dev_err(dev, "%s clock_stop_quirks %x unsupported\n",
523 __func__, clock_stop_quirks);
530 static int __maybe_unused intel_resume(struct device *dev)
532 struct sdw_cdns *cdns = dev_get_drvdata(dev);
533 struct sdw_intel *sdw = cdns_to_intel(cdns);
534 struct sdw_bus *bus = &cdns->bus;
538 if (bus->prop.hw_disabled || !sdw->startup_done) {
539 dev_dbg(dev, "SoundWire master %d is disabled or not-started, ignoring\n",
544 link_flags = md_flags >> (bus->link_id * 8);
546 if (pm_runtime_suspended(dev)) {
547 dev_dbg(dev, "pm_runtime status was suspended, forcing active\n");
549 /* follow required sequence from runtime_pm.rst */
550 pm_runtime_disable(dev);
551 pm_runtime_set_active(dev);
552 pm_runtime_mark_last_busy(dev);
553 pm_runtime_enable(dev);
555 link_flags = md_flags >> (bus->link_id * 8);
557 if (!(link_flags & SDW_INTEL_MASTER_DISABLE_PM_RUNTIME_IDLE))
558 pm_runtime_idle(dev);
561 ret = sdw_intel_link_power_up(sdw);
563 dev_err(dev, "%s failed: %d\n", __func__, ret);
568 * make sure all Slaves are tagged as UNATTACHED and provide
569 * reason for reinitialization
571 sdw_clear_slave_status(bus, SDW_UNATTACH_REQUEST_MASTER_RESET);
573 ret = sdw_intel_start_bus(sdw);
575 dev_err(dev, "cannot start bus during resume\n");
576 sdw_intel_link_power_down(sdw);
581 * after system resume, the pm_runtime suspend() may kick in
582 * during the enumeration, before any children device force the
583 * master device to remain active. Using pm_runtime_get()
584 * routines is not really possible, since it'd prevent the
585 * master from suspending.
586 * A reasonable compromise is to update the pm_runtime
587 * counters and delay the pm_runtime suspend by several
588 * seconds, by when all enumeration should be complete.
590 pm_runtime_mark_last_busy(dev);
595 static int __maybe_unused intel_resume_runtime(struct device *dev)
597 struct sdw_cdns *cdns = dev_get_drvdata(dev);
598 struct sdw_intel *sdw = cdns_to_intel(cdns);
599 struct sdw_bus *bus = &cdns->bus;
600 u32 clock_stop_quirks;
603 if (bus->prop.hw_disabled || !sdw->startup_done) {
604 dev_dbg(dev, "SoundWire master %d is disabled or not-started, ignoring\n",
609 /* unconditionally disable WAKEEN interrupt */
610 sdw_intel_shim_wake(sdw, false);
612 clock_stop_quirks = sdw->link_res->clock_stop_quirks;
614 if (clock_stop_quirks & SDW_INTEL_CLK_STOP_TEARDOWN) {
615 ret = sdw_intel_link_power_up(sdw);
617 dev_err(dev, "%s: power_up failed after teardown: %d\n", __func__, ret);
622 * make sure all Slaves are tagged as UNATTACHED and provide
623 * reason for reinitialization
625 sdw_clear_slave_status(bus, SDW_UNATTACH_REQUEST_MASTER_RESET);
627 ret = sdw_intel_start_bus(sdw);
629 dev_err(dev, "%s: cannot start bus after teardown: %d\n", __func__, ret);
630 sdw_intel_link_power_down(sdw);
634 } else if (clock_stop_quirks & SDW_INTEL_CLK_STOP_BUS_RESET) {
635 ret = sdw_intel_link_power_up(sdw);
637 dev_err(dev, "%s: power_up failed after bus reset: %d\n", __func__, ret);
641 ret = sdw_intel_start_bus_after_reset(sdw);
643 dev_err(dev, "%s: cannot start bus after reset: %d\n", __func__, ret);
644 sdw_intel_link_power_down(sdw);
647 } else if (!clock_stop_quirks) {
649 sdw_intel_check_clock_stop(sdw);
651 ret = sdw_intel_link_power_up(sdw);
653 dev_err(dev, "%s: power_up failed: %d\n", __func__, ret);
657 ret = sdw_intel_start_bus_after_clock_stop(sdw);
659 dev_err(dev, "%s: cannot start bus after clock stop: %d\n", __func__, ret);
660 sdw_intel_link_power_down(sdw);
664 dev_err(dev, "%s: clock_stop_quirks %x unsupported\n",
665 __func__, clock_stop_quirks);
672 static const struct dev_pm_ops intel_pm = {
673 .prepare = intel_pm_prepare,
674 SET_SYSTEM_SLEEP_PM_OPS(intel_suspend, intel_resume)
675 SET_RUNTIME_PM_OPS(intel_suspend_runtime, intel_resume_runtime, NULL)
678 static const struct auxiliary_device_id intel_link_id_table[] = {
679 { .name = "soundwire_intel.link" },
682 MODULE_DEVICE_TABLE(auxiliary, intel_link_id_table);
684 static struct auxiliary_driver sdw_intel_drv = {
685 .probe = intel_link_probe,
686 .remove = intel_link_remove,
688 /* auxiliary_driver_register() sets .name to be the modname */
691 .id_table = intel_link_id_table
693 module_auxiliary_driver(sdw_intel_drv);
695 MODULE_LICENSE("Dual BSD/GPL");
696 MODULE_DESCRIPTION("Intel Soundwire Link Driver");