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 #define INTEL_MASTER_SUSPEND_DELAY_MS 3000
29 * debug/config flags for the Intel SoundWire Master.
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.
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)
41 module_param_named(sdw_md_flags, md_flags, int, 0444);
42 MODULE_PARM_DESC(sdw_md_flags, "SoundWire Intel Master device flags (0x0 all off)");
44 struct wake_capable_part {
49 static struct wake_capable_part wake_capable_list[] = {
67 static bool is_wake_capable(struct sdw_slave *slave)
71 for (i = 0; i < ARRAY_SIZE(wake_capable_list); i++)
72 if (slave->id.part_id == wake_capable_list[i].part_id &&
73 slave->id.mfg_id == wake_capable_list[i].mfg_id)
78 static int generic_pre_bank_switch(struct sdw_bus *bus)
80 struct sdw_cdns *cdns = bus_to_cdns(bus);
81 struct sdw_intel *sdw = cdns_to_intel(cdns);
83 return sdw->link_res->hw_ops->pre_bank_switch(sdw);
86 static int generic_post_bank_switch(struct sdw_bus *bus)
88 struct sdw_cdns *cdns = bus_to_cdns(bus);
89 struct sdw_intel *sdw = cdns_to_intel(cdns);
91 return sdw->link_res->hw_ops->post_bank_switch(sdw);
94 static void generic_new_peripheral_assigned(struct sdw_bus *bus,
95 struct sdw_slave *slave,
98 struct sdw_cdns *cdns = bus_to_cdns(bus);
99 struct sdw_intel *sdw = cdns_to_intel(cdns);
102 bool wake_capable = slave->prop.wake_capable || is_wake_capable(slave);
105 dev_num_min = SDW_INTEL_DEV_NUM_IDA_MIN;
106 dev_num_max = SDW_MAX_DEVICES;
109 dev_num_max = SDW_INTEL_DEV_NUM_IDA_MIN - 1;
112 /* paranoia check, this should never happen */
113 if (dev_num < dev_num_min || dev_num > dev_num_max) {
114 dev_err(bus->dev, "%s: invalid dev_num %d, wake supported %d\n",
115 __func__, dev_num, slave->prop.wake_capable);
119 if (sdw->link_res->hw_ops->program_sdi && wake_capable)
120 sdw->link_res->hw_ops->program_sdi(sdw, dev_num);
123 static int sdw_master_read_intel_prop(struct sdw_bus *bus)
125 struct sdw_master_prop *prop = &bus->prop;
126 struct sdw_intel_prop *intel_prop;
127 struct fwnode_handle *link;
131 /* Find master handle */
132 snprintf(name, sizeof(name),
133 "mipi-sdw-link-%d-subproperties", bus->link_id);
135 link = device_get_named_child_node(bus->dev, name);
137 dev_err(bus->dev, "Master node %s not found\n", name);
141 fwnode_property_read_u32(link,
142 "intel-sdw-ip-clock",
145 /* the values reported by BIOS are the 2x clock, not the bus clock */
146 prop->mclk_freq /= 2;
148 fwnode_property_read_u32(link,
152 if (quirk_mask & SDW_INTEL_QUIRK_MASK_BUS_DISABLE)
153 prop->hw_disabled = true;
155 prop->quirks = SDW_MASTER_QUIRKS_CLEAR_INITIAL_CLASH |
156 SDW_MASTER_QUIRKS_CLEAR_INITIAL_PARITY;
158 intel_prop = devm_kzalloc(bus->dev, sizeof(*intel_prop), GFP_KERNEL);
160 fwnode_handle_put(link);
164 /* initialize with hardware defaults, in case the properties are not found */
165 intel_prop->clde = 0x0;
166 intel_prop->doaise2 = 0x0;
167 intel_prop->dodse2 = 0x0;
168 intel_prop->clds = 0x0;
169 intel_prop->clss = 0x0;
170 intel_prop->doaise = 0x1;
171 intel_prop->doais = 0x3;
172 intel_prop->dodse = 0x0;
173 intel_prop->dods = 0x1;
175 fwnode_property_read_u16(link,
178 fwnode_property_read_u16(link,
180 &intel_prop->doaise2);
181 fwnode_property_read_u16(link,
183 &intel_prop->dodse2);
184 fwnode_property_read_u16(link,
187 fwnode_property_read_u16(link,
190 fwnode_property_read_u16(link,
192 &intel_prop->doaise);
193 fwnode_property_read_u16(link,
196 fwnode_property_read_u16(link,
199 fwnode_property_read_u16(link,
202 bus->vendor_specific_prop = intel_prop;
204 dev_dbg(bus->dev, "doaise %#x doais %#x dodse %#x dods %#x\n",
210 fwnode_handle_put(link);
215 static int intel_prop_read(struct sdw_bus *bus)
217 struct sdw_master_prop *prop;
219 /* Initialize with default handler to read all DisCo properties */
220 sdw_master_read_prop(bus);
223 * Only one bus frequency is supported so far, filter
224 * frequencies reported in the DSDT
227 if (prop->clk_freq && prop->num_clk_freq > 1) {
228 unsigned int default_bus_frequency;
230 default_bus_frequency =
231 prop->default_frame_rate *
234 SDW_DOUBLE_RATE_FACTOR;
236 prop->num_clk_freq = 1;
237 prop->clk_freq[0] = default_bus_frequency;
238 prop->max_clk_freq = default_bus_frequency;
241 /* read Intel-specific properties */
242 sdw_master_read_intel_prop(bus);
247 static DEFINE_IDA(intel_peripheral_ida);
249 static int intel_get_device_num_ida(struct sdw_bus *bus, struct sdw_slave *slave)
253 if (slave->prop.wake_capable || is_wake_capable(slave))
254 return ida_alloc_range(&intel_peripheral_ida,
255 SDW_INTEL_DEV_NUM_IDA_MIN, SDW_MAX_DEVICES,
258 bit = find_first_zero_bit(slave->bus->assigned, SDW_MAX_DEVICES);
259 if (bit == SDW_MAX_DEVICES)
265 static void intel_put_device_num_ida(struct sdw_bus *bus, struct sdw_slave *slave)
267 if (slave->prop.wake_capable || is_wake_capable(slave))
268 ida_free(&intel_peripheral_ida, slave->dev_num);
271 static struct sdw_master_ops sdw_intel_ops = {
272 .read_prop = intel_prop_read,
273 .override_adr = sdw_dmi_override_adr,
274 .xfer_msg = cdns_xfer_msg,
275 .xfer_msg_defer = cdns_xfer_msg_defer,
276 .set_bus_conf = cdns_bus_conf,
277 .pre_bank_switch = generic_pre_bank_switch,
278 .post_bank_switch = generic_post_bank_switch,
279 .read_ping_status = cdns_read_ping_status,
280 .get_device_num = intel_get_device_num_ida,
281 .put_device_num = intel_put_device_num_ida,
282 .new_peripheral_assigned = generic_new_peripheral_assigned,
286 * probe and init (aux_dev_id argument is required by function prototype but not used)
288 static int intel_link_probe(struct auxiliary_device *auxdev,
289 const struct auxiliary_device_id *aux_dev_id)
292 struct device *dev = &auxdev->dev;
293 struct sdw_intel_link_dev *ldev = auxiliary_dev_to_sdw_intel_link_dev(auxdev);
294 struct sdw_intel *sdw;
295 struct sdw_cdns *cdns;
299 sdw = devm_kzalloc(dev, sizeof(*sdw), GFP_KERNEL);
306 sdw->instance = auxdev->id;
307 sdw->link_res = &ldev->link_res;
309 cdns->registers = sdw->link_res->registers;
310 cdns->ip_offset = sdw->link_res->ip_offset;
311 cdns->instance = sdw->instance;
314 /* single controller for all SoundWire links */
315 bus->controller_id = 0;
317 bus->link_id = auxdev->id;
318 bus->clk_stop_timeout = 1;
320 sdw_cdns_probe(cdns);
323 bus->ops = &sdw_intel_ops;
325 /* set driver data, accessed by snd_soc_dai_get_drvdata() */
326 auxiliary_set_drvdata(auxdev, cdns);
328 /* use generic bandwidth allocation algorithm */
329 sdw->cdns.bus.compute_params = sdw_compute_params;
331 /* avoid resuming from pm_runtime suspend if it's not required */
332 dev_pm_set_driver_flags(dev, DPM_FLAG_SMART_SUSPEND);
334 ret = sdw_bus_master_add(bus, dev, dev->fwnode);
336 dev_err(dev, "sdw_bus_master_add fail: %d\n", ret);
340 if (bus->prop.hw_disabled)
342 "SoundWire master %d is disabled, will be ignored\n",
345 * Ignore BIOS err_threshold, it's a really bad idea when dealing
346 * with multiple hardware synchronized links
348 bus->prop.err_threshold = 0;
353 int intel_link_startup(struct auxiliary_device *auxdev)
355 struct device *dev = &auxdev->dev;
356 struct sdw_cdns *cdns = auxiliary_get_drvdata(auxdev);
357 struct sdw_intel *sdw = cdns_to_intel(cdns);
358 struct sdw_bus *bus = &cdns->bus;
361 u32 clock_stop_quirks;
364 if (bus->prop.hw_disabled) {
366 "SoundWire master %d is disabled, ignoring\n",
371 link_flags = md_flags >> (bus->link_id * 8);
372 multi_link = !(link_flags & SDW_INTEL_MASTER_DISABLE_MULTI_LINK);
374 dev_dbg(dev, "Multi-link is disabled\n");
377 * hardware-based synchronization is required regardless
378 * of the number of segments used by a stream: SSP-based
379 * synchronization is gated by gsync when the multi-master
382 bus->hw_sync_min_links = 1;
384 bus->multi_link = multi_link;
386 /* Initialize shim, controller */
387 ret = sdw_intel_link_power_up(sdw);
392 ret = sdw_intel_register_dai(sdw);
394 dev_err(dev, "DAI registration failed: %d\n", ret);
398 sdw_intel_debugfs_init(sdw);
400 /* Enable runtime PM */
401 if (!(link_flags & SDW_INTEL_MASTER_DISABLE_PM_RUNTIME)) {
402 pm_runtime_set_autosuspend_delay(dev,
403 INTEL_MASTER_SUSPEND_DELAY_MS);
404 pm_runtime_use_autosuspend(dev);
405 pm_runtime_mark_last_busy(dev);
407 pm_runtime_set_active(dev);
408 pm_runtime_enable(dev);
410 pm_runtime_resume(bus->dev);
414 ret = sdw_intel_start_bus(sdw);
416 dev_err(dev, "bus start failed: %d\n", ret);
420 clock_stop_quirks = sdw->link_res->clock_stop_quirks;
421 if (clock_stop_quirks & SDW_INTEL_CLK_STOP_NOT_ALLOWED) {
423 * To keep the clock running we need to prevent
424 * pm_runtime suspend from happening by increasing the
426 * This quirk is specified by the parent PCI device in
427 * case of specific latency requirements. It will have
428 * no effect if pm_runtime is disabled by the user via
429 * a module parameter for testing purposes.
431 pm_runtime_get_noresume(dev);
435 * The runtime PM status of Slave devices is "Unsupported"
436 * until they report as ATTACHED. If they don't, e.g. because
437 * there are no Slave devices populated or if the power-on is
438 * delayed or dependent on a power switch, the Master will
439 * remain active and prevent its parent from suspending.
441 * Conditionally force the pm_runtime core to re-evaluate the
442 * Master status in the absence of any Slave activity. A quirk
443 * is provided to e.g. deal with Slaves that may be powered on
444 * with a delay. A more complete solution would require the
445 * definition of Master properties.
447 if (!(link_flags & SDW_INTEL_MASTER_DISABLE_PM_RUNTIME_IDLE)) {
448 pm_runtime_mark_last_busy(bus->dev);
449 pm_runtime_mark_last_busy(dev);
450 pm_runtime_idle(dev);
453 sdw->startup_done = true;
457 if (!(link_flags & SDW_INTEL_MASTER_DISABLE_PM_RUNTIME))
458 pm_runtime_disable(dev);
460 sdw_intel_link_power_down(sdw);
465 static void intel_link_remove(struct auxiliary_device *auxdev)
467 struct sdw_cdns *cdns = auxiliary_get_drvdata(auxdev);
468 struct sdw_intel *sdw = cdns_to_intel(cdns);
469 struct sdw_bus *bus = &cdns->bus;
472 * Since pm_runtime is already disabled, we don't decrease
473 * the refcount when the clock_stop_quirk is
474 * SDW_INTEL_CLK_STOP_NOT_ALLOWED
476 if (!bus->prop.hw_disabled) {
477 sdw_intel_debugfs_exit(sdw);
478 sdw_cdns_enable_interrupt(cdns, false);
480 sdw_bus_master_delete(bus);
483 int intel_link_process_wakeen_event(struct auxiliary_device *auxdev)
485 struct device *dev = &auxdev->dev;
486 struct sdw_intel *sdw;
489 sdw = auxiliary_get_drvdata(auxdev);
490 bus = &sdw->cdns.bus;
492 if (bus->prop.hw_disabled || !sdw->startup_done) {
493 dev_dbg(dev, "SoundWire master %d is disabled or not-started, ignoring\n",
498 if (!sdw_intel_shim_check_wake(sdw))
501 /* disable WAKEEN interrupt ASAP to prevent interrupt flood */
502 sdw_intel_shim_wake(sdw, false);
505 * resume the Master, which will generate a bus reset and result in
506 * Slaves re-attaching and be re-enumerated. The SoundWire physical
507 * device which generated the wake will trigger an interrupt, which
508 * will in turn cause the corresponding Linux Slave device to be
509 * resumed and the Slave codec driver to check the status.
511 pm_request_resume(dev);
520 int intel_resume_child_device(struct device *dev, void *data)
523 struct sdw_slave *slave = dev_to_sdw_dev(dev);
525 if (!slave->probed) {
526 dev_dbg(dev, "skipping device, no probed driver\n");
529 if (!slave->dev_num_sticky) {
530 dev_dbg(dev, "skipping device, never detected on bus\n");
534 ret = pm_runtime_resume(dev);
536 dev_err(dev, "%s: pm_runtime_resume failed: %d\n", __func__, ret);
543 static int __maybe_unused intel_pm_prepare(struct device *dev)
545 struct sdw_cdns *cdns = dev_get_drvdata(dev);
546 struct sdw_intel *sdw = cdns_to_intel(cdns);
547 struct sdw_bus *bus = &cdns->bus;
548 u32 clock_stop_quirks;
551 if (bus->prop.hw_disabled || !sdw->startup_done) {
552 dev_dbg(dev, "SoundWire master %d is disabled or not-started, ignoring\n",
557 clock_stop_quirks = sdw->link_res->clock_stop_quirks;
559 if (pm_runtime_suspended(dev) &&
560 pm_runtime_suspended(dev->parent) &&
561 ((clock_stop_quirks & SDW_INTEL_CLK_STOP_BUS_RESET) ||
562 !clock_stop_quirks)) {
564 * if we've enabled clock stop, and the parent is suspended, the SHIM registers
565 * are not accessible and the shim wake cannot be disabled.
566 * The only solution is to resume the entire bus to full power
570 * If any operation in this block fails, we keep going since we don't want
571 * to prevent system suspend from happening and errors should be recoverable
576 * first resume the device for this link. This will also by construction
577 * resume the PCI parent device.
579 ret = pm_runtime_resume(dev);
581 dev_err(dev, "%s: pm_runtime_resume failed: %d\n", __func__, ret);
586 * Continue resuming the entire bus (parent + child devices) to exit
587 * the clock stop mode. If there are no devices connected on this link
589 * The resume to full power could have been implemented with a .prepare
590 * step in SoundWire codec drivers. This would however require a lot
591 * of code to handle an Intel-specific corner case. It is simpler in
592 * practice to add a loop at the link level.
594 ret = device_for_each_child(bus->dev, NULL, intel_resume_child_device);
597 dev_err(dev, "%s: intel_resume_child_device failed: %d\n", __func__, ret);
603 static int __maybe_unused intel_suspend(struct device *dev)
605 struct sdw_cdns *cdns = dev_get_drvdata(dev);
606 struct sdw_intel *sdw = cdns_to_intel(cdns);
607 struct sdw_bus *bus = &cdns->bus;
608 u32 clock_stop_quirks;
611 if (bus->prop.hw_disabled || !sdw->startup_done) {
612 dev_dbg(dev, "SoundWire master %d is disabled or not-started, ignoring\n",
617 if (pm_runtime_suspended(dev)) {
618 dev_dbg(dev, "pm_runtime status: suspended\n");
620 clock_stop_quirks = sdw->link_res->clock_stop_quirks;
622 if ((clock_stop_quirks & SDW_INTEL_CLK_STOP_BUS_RESET) ||
623 !clock_stop_quirks) {
625 if (pm_runtime_suspended(dev->parent)) {
627 * paranoia check: this should not happen with the .prepare
628 * resume to full power
630 dev_err(dev, "%s: invalid config: parent is suspended\n", __func__);
632 sdw_intel_shim_wake(sdw, false);
639 ret = sdw_intel_stop_bus(sdw, false);
641 dev_err(dev, "%s: cannot stop bus: %d\n", __func__, ret);
648 static int __maybe_unused intel_suspend_runtime(struct device *dev)
650 struct sdw_cdns *cdns = dev_get_drvdata(dev);
651 struct sdw_intel *sdw = cdns_to_intel(cdns);
652 struct sdw_bus *bus = &cdns->bus;
653 u32 clock_stop_quirks;
656 if (bus->prop.hw_disabled || !sdw->startup_done) {
657 dev_dbg(dev, "SoundWire master %d is disabled or not-started, ignoring\n",
662 clock_stop_quirks = sdw->link_res->clock_stop_quirks;
664 if (clock_stop_quirks & SDW_INTEL_CLK_STOP_TEARDOWN) {
665 ret = sdw_intel_stop_bus(sdw, false);
667 dev_err(dev, "%s: cannot stop bus during teardown: %d\n",
671 } else if (clock_stop_quirks & SDW_INTEL_CLK_STOP_BUS_RESET || !clock_stop_quirks) {
672 ret = sdw_intel_stop_bus(sdw, true);
674 dev_err(dev, "%s: cannot stop bus during clock_stop: %d\n",
679 dev_err(dev, "%s clock_stop_quirks %x unsupported\n",
680 __func__, clock_stop_quirks);
687 static int __maybe_unused intel_resume(struct device *dev)
689 struct sdw_cdns *cdns = dev_get_drvdata(dev);
690 struct sdw_intel *sdw = cdns_to_intel(cdns);
691 struct sdw_bus *bus = &cdns->bus;
695 if (bus->prop.hw_disabled || !sdw->startup_done) {
696 dev_dbg(dev, "SoundWire master %d is disabled or not-started, ignoring\n",
701 if (pm_runtime_suspended(dev)) {
702 dev_dbg(dev, "pm_runtime status was suspended, forcing active\n");
704 /* follow required sequence from runtime_pm.rst */
705 pm_runtime_disable(dev);
706 pm_runtime_set_active(dev);
707 pm_runtime_mark_last_busy(dev);
708 pm_runtime_enable(dev);
710 pm_runtime_resume(bus->dev);
712 link_flags = md_flags >> (bus->link_id * 8);
714 if (!(link_flags & SDW_INTEL_MASTER_DISABLE_PM_RUNTIME_IDLE))
715 pm_runtime_idle(dev);
718 ret = sdw_intel_link_power_up(sdw);
720 dev_err(dev, "%s failed: %d\n", __func__, ret);
725 * make sure all Slaves are tagged as UNATTACHED and provide
726 * reason for reinitialization
728 sdw_clear_slave_status(bus, SDW_UNATTACH_REQUEST_MASTER_RESET);
730 ret = sdw_intel_start_bus(sdw);
732 dev_err(dev, "cannot start bus during resume\n");
733 sdw_intel_link_power_down(sdw);
738 * after system resume, the pm_runtime suspend() may kick in
739 * during the enumeration, before any children device force the
740 * master device to remain active. Using pm_runtime_get()
741 * routines is not really possible, since it'd prevent the
742 * master from suspending.
743 * A reasonable compromise is to update the pm_runtime
744 * counters and delay the pm_runtime suspend by several
745 * seconds, by when all enumeration should be complete.
747 pm_runtime_mark_last_busy(bus->dev);
748 pm_runtime_mark_last_busy(dev);
753 static int __maybe_unused intel_resume_runtime(struct device *dev)
755 struct sdw_cdns *cdns = dev_get_drvdata(dev);
756 struct sdw_intel *sdw = cdns_to_intel(cdns);
757 struct sdw_bus *bus = &cdns->bus;
758 u32 clock_stop_quirks;
761 if (bus->prop.hw_disabled || !sdw->startup_done) {
762 dev_dbg(dev, "SoundWire master %d is disabled or not-started, ignoring\n",
767 /* unconditionally disable WAKEEN interrupt */
768 sdw_intel_shim_wake(sdw, false);
770 clock_stop_quirks = sdw->link_res->clock_stop_quirks;
772 if (clock_stop_quirks & SDW_INTEL_CLK_STOP_TEARDOWN) {
773 ret = sdw_intel_link_power_up(sdw);
775 dev_err(dev, "%s: power_up failed after teardown: %d\n", __func__, ret);
780 * make sure all Slaves are tagged as UNATTACHED and provide
781 * reason for reinitialization
783 sdw_clear_slave_status(bus, SDW_UNATTACH_REQUEST_MASTER_RESET);
785 ret = sdw_intel_start_bus(sdw);
787 dev_err(dev, "%s: cannot start bus after teardown: %d\n", __func__, ret);
788 sdw_intel_link_power_down(sdw);
792 } else if (clock_stop_quirks & SDW_INTEL_CLK_STOP_BUS_RESET) {
793 ret = sdw_intel_link_power_up(sdw);
795 dev_err(dev, "%s: power_up failed after bus reset: %d\n", __func__, ret);
799 ret = sdw_intel_start_bus_after_reset(sdw);
801 dev_err(dev, "%s: cannot start bus after reset: %d\n", __func__, ret);
802 sdw_intel_link_power_down(sdw);
805 } else if (!clock_stop_quirks) {
807 sdw_intel_check_clock_stop(sdw);
809 ret = sdw_intel_link_power_up(sdw);
811 dev_err(dev, "%s: power_up failed: %d\n", __func__, ret);
815 ret = sdw_intel_start_bus_after_clock_stop(sdw);
817 dev_err(dev, "%s: cannot start bus after clock stop: %d\n", __func__, ret);
818 sdw_intel_link_power_down(sdw);
822 dev_err(dev, "%s: clock_stop_quirks %x unsupported\n",
823 __func__, clock_stop_quirks);
830 static const struct dev_pm_ops intel_pm = {
831 .prepare = intel_pm_prepare,
832 SET_SYSTEM_SLEEP_PM_OPS(intel_suspend, intel_resume)
833 SET_RUNTIME_PM_OPS(intel_suspend_runtime, intel_resume_runtime, NULL)
836 static const struct auxiliary_device_id intel_link_id_table[] = {
837 { .name = "soundwire_intel.link" },
840 MODULE_DEVICE_TABLE(auxiliary, intel_link_id_table);
842 static struct auxiliary_driver sdw_intel_drv = {
843 .probe = intel_link_probe,
844 .remove = intel_link_remove,
846 /* auxiliary_driver_register() sets .name to be the modname */
849 .id_table = intel_link_id_table
851 module_auxiliary_driver(sdw_intel_drv);
853 MODULE_LICENSE("Dual BSD/GPL");
854 MODULE_DESCRIPTION("Intel Soundwire Link Driver");