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 .reset_page_addr = cdns_reset_page_addr,
117 .set_bus_conf = cdns_bus_conf,
118 .pre_bank_switch = generic_pre_bank_switch,
119 .post_bank_switch = generic_post_bank_switch,
120 .read_ping_status = cdns_read_ping_status,
124 * probe and init (aux_dev_id argument is required by function prototype but not used)
126 static int intel_link_probe(struct auxiliary_device *auxdev,
127 const struct auxiliary_device_id *aux_dev_id)
130 struct device *dev = &auxdev->dev;
131 struct sdw_intel_link_dev *ldev = auxiliary_dev_to_sdw_intel_link_dev(auxdev);
132 struct sdw_intel *sdw;
133 struct sdw_cdns *cdns;
137 sdw = devm_kzalloc(dev, sizeof(*sdw), GFP_KERNEL);
144 sdw->instance = auxdev->id;
145 sdw->link_res = &ldev->link_res;
147 cdns->registers = sdw->link_res->registers;
148 cdns->instance = sdw->instance;
151 bus->link_id = auxdev->id;
152 bus->dev_num_ida_min = INTEL_DEV_NUM_IDA_MIN;
153 bus->clk_stop_timeout = 1;
155 sdw_cdns_probe(cdns);
158 bus->ops = &sdw_intel_ops;
160 /* set driver data, accessed by snd_soc_dai_get_drvdata() */
161 auxiliary_set_drvdata(auxdev, cdns);
163 /* use generic bandwidth allocation algorithm */
164 sdw->cdns.bus.compute_params = sdw_compute_params;
166 /* avoid resuming from pm_runtime suspend if it's not required */
167 dev_pm_set_driver_flags(dev, DPM_FLAG_SMART_SUSPEND);
169 ret = sdw_bus_master_add(bus, dev, dev->fwnode);
171 dev_err(dev, "sdw_bus_master_add fail: %d\n", ret);
175 if (bus->prop.hw_disabled)
177 "SoundWire master %d is disabled, will be ignored\n",
180 * Ignore BIOS err_threshold, it's a really bad idea when dealing
181 * with multiple hardware synchronized links
183 bus->prop.err_threshold = 0;
188 int intel_link_startup(struct auxiliary_device *auxdev)
190 struct device *dev = &auxdev->dev;
191 struct sdw_cdns *cdns = auxiliary_get_drvdata(auxdev);
192 struct sdw_intel *sdw = cdns_to_intel(cdns);
193 struct sdw_bus *bus = &cdns->bus;
196 u32 clock_stop_quirks;
199 if (bus->prop.hw_disabled) {
201 "SoundWire master %d is disabled, ignoring\n",
206 link_flags = md_flags >> (bus->link_id * 8);
207 multi_link = !(link_flags & SDW_INTEL_MASTER_DISABLE_MULTI_LINK);
209 dev_dbg(dev, "Multi-link is disabled\n");
212 * hardware-based synchronization is required regardless
213 * of the number of segments used by a stream: SSP-based
214 * synchronization is gated by gsync when the multi-master
217 bus->hw_sync_min_links = 1;
219 bus->multi_link = multi_link;
221 /* Initialize shim, controller */
222 ret = sdw_intel_link_power_up(sdw);
227 ret = sdw_intel_register_dai(sdw);
229 dev_err(dev, "DAI registration failed: %d\n", ret);
233 sdw_intel_debugfs_init(sdw);
236 ret = sdw_intel_start_bus(sdw);
238 dev_err(dev, "bus start failed: %d\n", ret);
242 /* Enable runtime PM */
243 if (!(link_flags & SDW_INTEL_MASTER_DISABLE_PM_RUNTIME)) {
244 pm_runtime_set_autosuspend_delay(dev,
245 INTEL_MASTER_SUSPEND_DELAY_MS);
246 pm_runtime_use_autosuspend(dev);
247 pm_runtime_mark_last_busy(dev);
249 pm_runtime_set_active(dev);
250 pm_runtime_enable(dev);
253 clock_stop_quirks = sdw->link_res->clock_stop_quirks;
254 if (clock_stop_quirks & SDW_INTEL_CLK_STOP_NOT_ALLOWED) {
256 * To keep the clock running we need to prevent
257 * pm_runtime suspend from happening by increasing the
259 * This quirk is specified by the parent PCI device in
260 * case of specific latency requirements. It will have
261 * no effect if pm_runtime is disabled by the user via
262 * a module parameter for testing purposes.
264 pm_runtime_get_noresume(dev);
268 * The runtime PM status of Slave devices is "Unsupported"
269 * until they report as ATTACHED. If they don't, e.g. because
270 * there are no Slave devices populated or if the power-on is
271 * delayed or dependent on a power switch, the Master will
272 * remain active and prevent its parent from suspending.
274 * Conditionally force the pm_runtime core to re-evaluate the
275 * Master status in the absence of any Slave activity. A quirk
276 * is provided to e.g. deal with Slaves that may be powered on
277 * with a delay. A more complete solution would require the
278 * definition of Master properties.
280 if (!(link_flags & SDW_INTEL_MASTER_DISABLE_PM_RUNTIME_IDLE))
281 pm_runtime_idle(dev);
283 sdw->startup_done = true;
287 sdw_intel_link_power_down(sdw);
292 static void intel_link_remove(struct auxiliary_device *auxdev)
294 struct sdw_cdns *cdns = auxiliary_get_drvdata(auxdev);
295 struct sdw_intel *sdw = cdns_to_intel(cdns);
296 struct sdw_bus *bus = &cdns->bus;
299 * Since pm_runtime is already disabled, we don't decrease
300 * the refcount when the clock_stop_quirk is
301 * SDW_INTEL_CLK_STOP_NOT_ALLOWED
303 if (!bus->prop.hw_disabled) {
304 sdw_intel_debugfs_exit(sdw);
305 sdw_cdns_enable_interrupt(cdns, false);
307 sdw_bus_master_delete(bus);
310 int intel_link_process_wakeen_event(struct auxiliary_device *auxdev)
312 struct device *dev = &auxdev->dev;
313 struct sdw_intel *sdw;
316 sdw = auxiliary_get_drvdata(auxdev);
317 bus = &sdw->cdns.bus;
319 if (bus->prop.hw_disabled || !sdw->startup_done) {
320 dev_dbg(dev, "SoundWire master %d is disabled or not-started, ignoring\n",
325 if (!sdw_intel_shim_check_wake(sdw))
328 /* disable WAKEEN interrupt ASAP to prevent interrupt flood */
329 sdw_intel_shim_wake(sdw, false);
332 * resume the Master, which will generate a bus reset and result in
333 * Slaves re-attaching and be re-enumerated. The SoundWire physical
334 * device which generated the wake will trigger an interrupt, which
335 * will in turn cause the corresponding Linux Slave device to be
336 * resumed and the Slave codec driver to check the status.
338 pm_request_resume(dev);
347 static int intel_resume_child_device(struct device *dev, void *data)
350 struct sdw_slave *slave = dev_to_sdw_dev(dev);
352 if (!slave->probed) {
353 dev_dbg(dev, "skipping device, no probed driver\n");
356 if (!slave->dev_num_sticky) {
357 dev_dbg(dev, "skipping device, never detected on bus\n");
361 ret = pm_request_resume(dev);
363 dev_err(dev, "%s: pm_request_resume failed: %d\n", __func__, ret);
368 static int __maybe_unused intel_pm_prepare(struct device *dev)
370 struct sdw_cdns *cdns = dev_get_drvdata(dev);
371 struct sdw_intel *sdw = cdns_to_intel(cdns);
372 struct sdw_bus *bus = &cdns->bus;
373 u32 clock_stop_quirks;
376 if (bus->prop.hw_disabled || !sdw->startup_done) {
377 dev_dbg(dev, "SoundWire master %d is disabled or not-started, ignoring\n",
382 clock_stop_quirks = sdw->link_res->clock_stop_quirks;
384 if (pm_runtime_suspended(dev) &&
385 pm_runtime_suspended(dev->parent) &&
386 ((clock_stop_quirks & SDW_INTEL_CLK_STOP_BUS_RESET) ||
387 !clock_stop_quirks)) {
389 * if we've enabled clock stop, and the parent is suspended, the SHIM registers
390 * are not accessible and the shim wake cannot be disabled.
391 * The only solution is to resume the entire bus to full power
395 * If any operation in this block fails, we keep going since we don't want
396 * to prevent system suspend from happening and errors should be recoverable
401 * first resume the device for this link. This will also by construction
402 * resume the PCI parent device.
404 ret = pm_request_resume(dev);
406 dev_err(dev, "%s: pm_request_resume failed: %d\n", __func__, ret);
411 * Continue resuming the entire bus (parent + child devices) to exit
412 * the clock stop mode. If there are no devices connected on this link
414 * The resume to full power could have been implemented with a .prepare
415 * step in SoundWire codec drivers. This would however require a lot
416 * of code to handle an Intel-specific corner case. It is simpler in
417 * practice to add a loop at the link level.
419 ret = device_for_each_child(bus->dev, NULL, intel_resume_child_device);
422 dev_err(dev, "%s: intel_resume_child_device failed: %d\n", __func__, ret);
428 static int __maybe_unused intel_suspend(struct device *dev)
430 struct sdw_cdns *cdns = dev_get_drvdata(dev);
431 struct sdw_intel *sdw = cdns_to_intel(cdns);
432 struct sdw_bus *bus = &cdns->bus;
433 u32 clock_stop_quirks;
436 if (bus->prop.hw_disabled || !sdw->startup_done) {
437 dev_dbg(dev, "SoundWire master %d is disabled or not-started, ignoring\n",
442 if (pm_runtime_suspended(dev)) {
443 dev_dbg(dev, "pm_runtime status: suspended\n");
445 clock_stop_quirks = sdw->link_res->clock_stop_quirks;
447 if ((clock_stop_quirks & SDW_INTEL_CLK_STOP_BUS_RESET) ||
448 !clock_stop_quirks) {
450 if (pm_runtime_suspended(dev->parent)) {
452 * paranoia check: this should not happen with the .prepare
453 * resume to full power
455 dev_err(dev, "%s: invalid config: parent is suspended\n", __func__);
457 sdw_intel_shim_wake(sdw, false);
464 ret = sdw_intel_stop_bus(sdw, false);
466 dev_err(dev, "%s: cannot stop bus: %d\n", __func__, ret);
473 static int __maybe_unused intel_suspend_runtime(struct device *dev)
475 struct sdw_cdns *cdns = dev_get_drvdata(dev);
476 struct sdw_intel *sdw = cdns_to_intel(cdns);
477 struct sdw_bus *bus = &cdns->bus;
478 u32 clock_stop_quirks;
481 if (bus->prop.hw_disabled || !sdw->startup_done) {
482 dev_dbg(dev, "SoundWire master %d is disabled or not-started, ignoring\n",
487 clock_stop_quirks = sdw->link_res->clock_stop_quirks;
489 if (clock_stop_quirks & SDW_INTEL_CLK_STOP_TEARDOWN) {
490 ret = sdw_intel_stop_bus(sdw, false);
492 dev_err(dev, "%s: cannot stop bus during teardown: %d\n",
496 } else if (clock_stop_quirks & SDW_INTEL_CLK_STOP_BUS_RESET || !clock_stop_quirks) {
497 ret = sdw_intel_stop_bus(sdw, true);
499 dev_err(dev, "%s: cannot stop bus during clock_stop: %d\n",
504 dev_err(dev, "%s clock_stop_quirks %x unsupported\n",
505 __func__, clock_stop_quirks);
512 static int __maybe_unused intel_resume(struct device *dev)
514 struct sdw_cdns *cdns = dev_get_drvdata(dev);
515 struct sdw_intel *sdw = cdns_to_intel(cdns);
516 struct sdw_bus *bus = &cdns->bus;
520 if (bus->prop.hw_disabled || !sdw->startup_done) {
521 dev_dbg(dev, "SoundWire master %d is disabled or not-started, ignoring\n",
526 link_flags = md_flags >> (bus->link_id * 8);
528 if (pm_runtime_suspended(dev)) {
529 dev_dbg(dev, "pm_runtime status was suspended, forcing active\n");
531 /* follow required sequence from runtime_pm.rst */
532 pm_runtime_disable(dev);
533 pm_runtime_set_active(dev);
534 pm_runtime_mark_last_busy(dev);
535 pm_runtime_enable(dev);
537 link_flags = md_flags >> (bus->link_id * 8);
539 if (!(link_flags & SDW_INTEL_MASTER_DISABLE_PM_RUNTIME_IDLE))
540 pm_runtime_idle(dev);
543 ret = sdw_intel_link_power_up(sdw);
545 dev_err(dev, "%s failed: %d\n", __func__, ret);
550 * make sure all Slaves are tagged as UNATTACHED and provide
551 * reason for reinitialization
553 sdw_clear_slave_status(bus, SDW_UNATTACH_REQUEST_MASTER_RESET);
555 ret = sdw_intel_start_bus(sdw);
557 dev_err(dev, "cannot start bus during resume\n");
558 sdw_intel_link_power_down(sdw);
563 * after system resume, the pm_runtime suspend() may kick in
564 * during the enumeration, before any children device force the
565 * master device to remain active. Using pm_runtime_get()
566 * routines is not really possible, since it'd prevent the
567 * master from suspending.
568 * A reasonable compromise is to update the pm_runtime
569 * counters and delay the pm_runtime suspend by several
570 * seconds, by when all enumeration should be complete.
572 pm_runtime_mark_last_busy(dev);
577 static int __maybe_unused intel_resume_runtime(struct device *dev)
579 struct sdw_cdns *cdns = dev_get_drvdata(dev);
580 struct sdw_intel *sdw = cdns_to_intel(cdns);
581 struct sdw_bus *bus = &cdns->bus;
582 u32 clock_stop_quirks;
585 if (bus->prop.hw_disabled || !sdw->startup_done) {
586 dev_dbg(dev, "SoundWire master %d is disabled or not-started, ignoring\n",
591 /* unconditionally disable WAKEEN interrupt */
592 sdw_intel_shim_wake(sdw, false);
594 clock_stop_quirks = sdw->link_res->clock_stop_quirks;
596 if (clock_stop_quirks & SDW_INTEL_CLK_STOP_TEARDOWN) {
597 ret = sdw_intel_link_power_up(sdw);
599 dev_err(dev, "%s: power_up failed after teardown: %d\n", __func__, ret);
604 * make sure all Slaves are tagged as UNATTACHED and provide
605 * reason for reinitialization
607 sdw_clear_slave_status(bus, SDW_UNATTACH_REQUEST_MASTER_RESET);
609 ret = sdw_intel_start_bus(sdw);
611 dev_err(dev, "%s: cannot start bus after teardown: %d\n", __func__, ret);
612 sdw_intel_link_power_down(sdw);
616 } else if (clock_stop_quirks & SDW_INTEL_CLK_STOP_BUS_RESET) {
617 ret = sdw_intel_link_power_up(sdw);
619 dev_err(dev, "%s: power_up failed after bus reset: %d\n", __func__, ret);
623 ret = sdw_intel_start_bus_after_reset(sdw);
625 dev_err(dev, "%s: cannot start bus after reset: %d\n", __func__, ret);
626 sdw_intel_link_power_down(sdw);
629 } else if (!clock_stop_quirks) {
631 sdw_intel_check_clock_stop(sdw);
633 ret = sdw_intel_link_power_up(sdw);
635 dev_err(dev, "%s: power_up failed: %d\n", __func__, ret);
639 ret = sdw_intel_start_bus_after_clock_stop(sdw);
641 dev_err(dev, "%s: cannot start bus after clock stop: %d\n", __func__, ret);
642 sdw_intel_link_power_down(sdw);
646 dev_err(dev, "%s: clock_stop_quirks %x unsupported\n",
647 __func__, clock_stop_quirks);
654 static const struct dev_pm_ops intel_pm = {
655 .prepare = intel_pm_prepare,
656 SET_SYSTEM_SLEEP_PM_OPS(intel_suspend, intel_resume)
657 SET_RUNTIME_PM_OPS(intel_suspend_runtime, intel_resume_runtime, NULL)
660 static const struct auxiliary_device_id intel_link_id_table[] = {
661 { .name = "soundwire_intel.link" },
664 MODULE_DEVICE_TABLE(auxiliary, intel_link_id_table);
666 static struct auxiliary_driver sdw_intel_drv = {
667 .probe = intel_link_probe,
668 .remove = intel_link_remove,
670 /* auxiliary_driver_register() sets .name to be the modname */
673 .id_table = intel_link_id_table
675 module_auxiliary_driver(sdw_intel_drv);
677 MODULE_LICENSE("Dual BSD/GPL");
678 MODULE_DESCRIPTION("Intel Soundwire Link Driver");