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[] = {
66 static bool is_wake_capable(struct sdw_slave *slave)
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)
77 static int generic_pre_bank_switch(struct sdw_bus *bus)
79 struct sdw_cdns *cdns = bus_to_cdns(bus);
80 struct sdw_intel *sdw = cdns_to_intel(cdns);
82 return sdw->link_res->hw_ops->pre_bank_switch(sdw);
85 static int generic_post_bank_switch(struct sdw_bus *bus)
87 struct sdw_cdns *cdns = bus_to_cdns(bus);
88 struct sdw_intel *sdw = cdns_to_intel(cdns);
90 return sdw->link_res->hw_ops->post_bank_switch(sdw);
93 static void generic_new_peripheral_assigned(struct sdw_bus *bus,
94 struct sdw_slave *slave,
97 struct sdw_cdns *cdns = bus_to_cdns(bus);
98 struct sdw_intel *sdw = cdns_to_intel(cdns);
101 bool wake_capable = slave->prop.wake_capable || is_wake_capable(slave);
104 dev_num_min = SDW_INTEL_DEV_NUM_IDA_MIN;
105 dev_num_max = SDW_MAX_DEVICES;
108 dev_num_max = SDW_INTEL_DEV_NUM_IDA_MIN - 1;
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);
118 if (sdw->link_res->hw_ops->program_sdi && wake_capable)
119 sdw->link_res->hw_ops->program_sdi(sdw, dev_num);
122 static int sdw_master_read_intel_prop(struct sdw_bus *bus)
124 struct sdw_master_prop *prop = &bus->prop;
125 struct sdw_intel_prop *intel_prop;
126 struct fwnode_handle *link;
130 /* Find master handle */
131 snprintf(name, sizeof(name),
132 "mipi-sdw-link-%d-subproperties", bus->link_id);
134 link = device_get_named_child_node(bus->dev, name);
136 dev_err(bus->dev, "Master node %s not found\n", name);
140 fwnode_property_read_u32(link,
141 "intel-sdw-ip-clock",
144 /* the values reported by BIOS are the 2x clock, not the bus clock */
145 prop->mclk_freq /= 2;
147 fwnode_property_read_u32(link,
151 if (quirk_mask & SDW_INTEL_QUIRK_MASK_BUS_DISABLE)
152 prop->hw_disabled = true;
154 prop->quirks = SDW_MASTER_QUIRKS_CLEAR_INITIAL_CLASH |
155 SDW_MASTER_QUIRKS_CLEAR_INITIAL_PARITY;
157 intel_prop = devm_kzalloc(bus->dev, sizeof(*intel_prop), GFP_KERNEL);
161 /* initialize with hardware defaults, in case the properties are not found */
162 intel_prop->doaise = 0x1;
163 intel_prop->doais = 0x3;
164 intel_prop->dodse = 0x0;
165 intel_prop->dods = 0x1;
167 fwnode_property_read_u16(link,
169 &intel_prop->doaise);
170 fwnode_property_read_u16(link,
173 fwnode_property_read_u16(link,
176 fwnode_property_read_u16(link,
179 bus->vendor_specific_prop = intel_prop;
181 dev_dbg(bus->dev, "doaise %#x doais %#x dodse %#x dods %#x\n",
190 static int intel_prop_read(struct sdw_bus *bus)
192 /* Initialize with default handler to read all DisCo properties */
193 sdw_master_read_prop(bus);
195 /* read Intel-specific properties */
196 sdw_master_read_intel_prop(bus);
201 static DEFINE_IDA(intel_peripheral_ida);
203 static int intel_get_device_num_ida(struct sdw_bus *bus, struct sdw_slave *slave)
207 if (slave->prop.wake_capable || is_wake_capable(slave))
208 return ida_alloc_range(&intel_peripheral_ida,
209 SDW_INTEL_DEV_NUM_IDA_MIN, SDW_MAX_DEVICES,
212 bit = find_first_zero_bit(slave->bus->assigned, SDW_MAX_DEVICES);
213 if (bit == SDW_MAX_DEVICES)
219 static void intel_put_device_num_ida(struct sdw_bus *bus, struct sdw_slave *slave)
221 if (slave->prop.wake_capable || is_wake_capable(slave))
222 ida_free(&intel_peripheral_ida, slave->dev_num);
225 static struct sdw_master_ops sdw_intel_ops = {
226 .read_prop = intel_prop_read,
227 .override_adr = sdw_dmi_override_adr,
228 .xfer_msg = cdns_xfer_msg,
229 .xfer_msg_defer = cdns_xfer_msg_defer,
230 .set_bus_conf = cdns_bus_conf,
231 .pre_bank_switch = generic_pre_bank_switch,
232 .post_bank_switch = generic_post_bank_switch,
233 .read_ping_status = cdns_read_ping_status,
234 .get_device_num = intel_get_device_num_ida,
235 .put_device_num = intel_put_device_num_ida,
236 .new_peripheral_assigned = generic_new_peripheral_assigned,
240 * probe and init (aux_dev_id argument is required by function prototype but not used)
242 static int intel_link_probe(struct auxiliary_device *auxdev,
243 const struct auxiliary_device_id *aux_dev_id)
246 struct device *dev = &auxdev->dev;
247 struct sdw_intel_link_dev *ldev = auxiliary_dev_to_sdw_intel_link_dev(auxdev);
248 struct sdw_intel *sdw;
249 struct sdw_cdns *cdns;
253 sdw = devm_kzalloc(dev, sizeof(*sdw), GFP_KERNEL);
260 sdw->instance = auxdev->id;
261 sdw->link_res = &ldev->link_res;
263 cdns->registers = sdw->link_res->registers;
264 cdns->ip_offset = sdw->link_res->ip_offset;
265 cdns->instance = sdw->instance;
268 /* single controller for all SoundWire links */
269 bus->controller_id = 0;
271 bus->link_id = auxdev->id;
272 bus->clk_stop_timeout = 1;
274 sdw_cdns_probe(cdns);
277 bus->ops = &sdw_intel_ops;
279 /* set driver data, accessed by snd_soc_dai_get_drvdata() */
280 auxiliary_set_drvdata(auxdev, cdns);
282 /* use generic bandwidth allocation algorithm */
283 sdw->cdns.bus.compute_params = sdw_compute_params;
285 /* avoid resuming from pm_runtime suspend if it's not required */
286 dev_pm_set_driver_flags(dev, DPM_FLAG_SMART_SUSPEND);
288 ret = sdw_bus_master_add(bus, dev, dev->fwnode);
290 dev_err(dev, "sdw_bus_master_add fail: %d\n", ret);
294 if (bus->prop.hw_disabled)
296 "SoundWire master %d is disabled, will be ignored\n",
299 * Ignore BIOS err_threshold, it's a really bad idea when dealing
300 * with multiple hardware synchronized links
302 bus->prop.err_threshold = 0;
307 int intel_link_startup(struct auxiliary_device *auxdev)
309 struct device *dev = &auxdev->dev;
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 u32 clock_stop_quirks;
318 if (bus->prop.hw_disabled) {
320 "SoundWire master %d is disabled, ignoring\n",
325 link_flags = md_flags >> (bus->link_id * 8);
326 multi_link = !(link_flags & SDW_INTEL_MASTER_DISABLE_MULTI_LINK);
328 dev_dbg(dev, "Multi-link is disabled\n");
331 * hardware-based synchronization is required regardless
332 * of the number of segments used by a stream: SSP-based
333 * synchronization is gated by gsync when the multi-master
336 bus->hw_sync_min_links = 1;
338 bus->multi_link = multi_link;
340 /* Initialize shim, controller */
341 ret = sdw_intel_link_power_up(sdw);
346 ret = sdw_intel_register_dai(sdw);
348 dev_err(dev, "DAI registration failed: %d\n", ret);
352 sdw_intel_debugfs_init(sdw);
354 /* Enable runtime PM */
355 if (!(link_flags & SDW_INTEL_MASTER_DISABLE_PM_RUNTIME)) {
356 pm_runtime_set_autosuspend_delay(dev,
357 INTEL_MASTER_SUSPEND_DELAY_MS);
358 pm_runtime_use_autosuspend(dev);
359 pm_runtime_mark_last_busy(dev);
361 pm_runtime_set_active(dev);
362 pm_runtime_enable(dev);
364 pm_runtime_resume(bus->dev);
368 ret = sdw_intel_start_bus(sdw);
370 dev_err(dev, "bus start failed: %d\n", ret);
374 clock_stop_quirks = sdw->link_res->clock_stop_quirks;
375 if (clock_stop_quirks & SDW_INTEL_CLK_STOP_NOT_ALLOWED) {
377 * To keep the clock running we need to prevent
378 * pm_runtime suspend from happening by increasing the
380 * This quirk is specified by the parent PCI device in
381 * case of specific latency requirements. It will have
382 * no effect if pm_runtime is disabled by the user via
383 * a module parameter for testing purposes.
385 pm_runtime_get_noresume(dev);
389 * The runtime PM status of Slave devices is "Unsupported"
390 * until they report as ATTACHED. If they don't, e.g. because
391 * there are no Slave devices populated or if the power-on is
392 * delayed or dependent on a power switch, the Master will
393 * remain active and prevent its parent from suspending.
395 * Conditionally force the pm_runtime core to re-evaluate the
396 * Master status in the absence of any Slave activity. A quirk
397 * is provided to e.g. deal with Slaves that may be powered on
398 * with a delay. A more complete solution would require the
399 * definition of Master properties.
401 if (!(link_flags & SDW_INTEL_MASTER_DISABLE_PM_RUNTIME_IDLE)) {
402 pm_runtime_mark_last_busy(bus->dev);
403 pm_runtime_mark_last_busy(dev);
404 pm_runtime_idle(dev);
407 sdw->startup_done = true;
411 if (!(link_flags & SDW_INTEL_MASTER_DISABLE_PM_RUNTIME))
412 pm_runtime_disable(dev);
414 sdw_intel_link_power_down(sdw);
419 static void intel_link_remove(struct auxiliary_device *auxdev)
421 struct sdw_cdns *cdns = auxiliary_get_drvdata(auxdev);
422 struct sdw_intel *sdw = cdns_to_intel(cdns);
423 struct sdw_bus *bus = &cdns->bus;
426 * Since pm_runtime is already disabled, we don't decrease
427 * the refcount when the clock_stop_quirk is
428 * SDW_INTEL_CLK_STOP_NOT_ALLOWED
430 if (!bus->prop.hw_disabled) {
431 sdw_intel_debugfs_exit(sdw);
432 sdw_cdns_enable_interrupt(cdns, false);
434 sdw_bus_master_delete(bus);
437 int intel_link_process_wakeen_event(struct auxiliary_device *auxdev)
439 struct device *dev = &auxdev->dev;
440 struct sdw_intel *sdw;
443 sdw = auxiliary_get_drvdata(auxdev);
444 bus = &sdw->cdns.bus;
446 if (bus->prop.hw_disabled || !sdw->startup_done) {
447 dev_dbg(dev, "SoundWire master %d is disabled or not-started, ignoring\n",
452 if (!sdw_intel_shim_check_wake(sdw))
455 /* disable WAKEEN interrupt ASAP to prevent interrupt flood */
456 sdw_intel_shim_wake(sdw, false);
459 * resume the Master, which will generate a bus reset and result in
460 * Slaves re-attaching and be re-enumerated. The SoundWire physical
461 * device which generated the wake will trigger an interrupt, which
462 * will in turn cause the corresponding Linux Slave device to be
463 * resumed and the Slave codec driver to check the status.
465 pm_request_resume(dev);
474 int intel_resume_child_device(struct device *dev, void *data)
477 struct sdw_slave *slave = dev_to_sdw_dev(dev);
479 if (!slave->probed) {
480 dev_dbg(dev, "skipping device, no probed driver\n");
483 if (!slave->dev_num_sticky) {
484 dev_dbg(dev, "skipping device, never detected on bus\n");
488 ret = pm_runtime_resume(dev);
490 dev_err(dev, "%s: pm_runtime_resume failed: %d\n", __func__, ret);
497 static int __maybe_unused intel_pm_prepare(struct device *dev)
499 struct sdw_cdns *cdns = dev_get_drvdata(dev);
500 struct sdw_intel *sdw = cdns_to_intel(cdns);
501 struct sdw_bus *bus = &cdns->bus;
502 u32 clock_stop_quirks;
505 if (bus->prop.hw_disabled || !sdw->startup_done) {
506 dev_dbg(dev, "SoundWire master %d is disabled or not-started, ignoring\n",
511 clock_stop_quirks = sdw->link_res->clock_stop_quirks;
513 if (pm_runtime_suspended(dev) &&
514 pm_runtime_suspended(dev->parent) &&
515 ((clock_stop_quirks & SDW_INTEL_CLK_STOP_BUS_RESET) ||
516 !clock_stop_quirks)) {
518 * if we've enabled clock stop, and the parent is suspended, the SHIM registers
519 * are not accessible and the shim wake cannot be disabled.
520 * The only solution is to resume the entire bus to full power
524 * If any operation in this block fails, we keep going since we don't want
525 * to prevent system suspend from happening and errors should be recoverable
530 * first resume the device for this link. This will also by construction
531 * resume the PCI parent device.
533 ret = pm_runtime_resume(dev);
535 dev_err(dev, "%s: pm_runtime_resume failed: %d\n", __func__, ret);
540 * Continue resuming the entire bus (parent + child devices) to exit
541 * the clock stop mode. If there are no devices connected on this link
543 * The resume to full power could have been implemented with a .prepare
544 * step in SoundWire codec drivers. This would however require a lot
545 * of code to handle an Intel-specific corner case. It is simpler in
546 * practice to add a loop at the link level.
548 ret = device_for_each_child(bus->dev, NULL, intel_resume_child_device);
551 dev_err(dev, "%s: intel_resume_child_device failed: %d\n", __func__, ret);
557 static int __maybe_unused intel_suspend(struct device *dev)
559 struct sdw_cdns *cdns = dev_get_drvdata(dev);
560 struct sdw_intel *sdw = cdns_to_intel(cdns);
561 struct sdw_bus *bus = &cdns->bus;
562 u32 clock_stop_quirks;
565 if (bus->prop.hw_disabled || !sdw->startup_done) {
566 dev_dbg(dev, "SoundWire master %d is disabled or not-started, ignoring\n",
571 if (pm_runtime_suspended(dev)) {
572 dev_dbg(dev, "pm_runtime status: suspended\n");
574 clock_stop_quirks = sdw->link_res->clock_stop_quirks;
576 if ((clock_stop_quirks & SDW_INTEL_CLK_STOP_BUS_RESET) ||
577 !clock_stop_quirks) {
579 if (pm_runtime_suspended(dev->parent)) {
581 * paranoia check: this should not happen with the .prepare
582 * resume to full power
584 dev_err(dev, "%s: invalid config: parent is suspended\n", __func__);
586 sdw_intel_shim_wake(sdw, false);
593 ret = sdw_intel_stop_bus(sdw, false);
595 dev_err(dev, "%s: cannot stop bus: %d\n", __func__, ret);
602 static int __maybe_unused intel_suspend_runtime(struct device *dev)
604 struct sdw_cdns *cdns = dev_get_drvdata(dev);
605 struct sdw_intel *sdw = cdns_to_intel(cdns);
606 struct sdw_bus *bus = &cdns->bus;
607 u32 clock_stop_quirks;
610 if (bus->prop.hw_disabled || !sdw->startup_done) {
611 dev_dbg(dev, "SoundWire master %d is disabled or not-started, ignoring\n",
616 clock_stop_quirks = sdw->link_res->clock_stop_quirks;
618 if (clock_stop_quirks & SDW_INTEL_CLK_STOP_TEARDOWN) {
619 ret = sdw_intel_stop_bus(sdw, false);
621 dev_err(dev, "%s: cannot stop bus during teardown: %d\n",
625 } else if (clock_stop_quirks & SDW_INTEL_CLK_STOP_BUS_RESET || !clock_stop_quirks) {
626 ret = sdw_intel_stop_bus(sdw, true);
628 dev_err(dev, "%s: cannot stop bus during clock_stop: %d\n",
633 dev_err(dev, "%s clock_stop_quirks %x unsupported\n",
634 __func__, clock_stop_quirks);
641 static int __maybe_unused intel_resume(struct device *dev)
643 struct sdw_cdns *cdns = dev_get_drvdata(dev);
644 struct sdw_intel *sdw = cdns_to_intel(cdns);
645 struct sdw_bus *bus = &cdns->bus;
649 if (bus->prop.hw_disabled || !sdw->startup_done) {
650 dev_dbg(dev, "SoundWire master %d is disabled or not-started, ignoring\n",
655 if (pm_runtime_suspended(dev)) {
656 dev_dbg(dev, "pm_runtime status was suspended, forcing active\n");
658 /* follow required sequence from runtime_pm.rst */
659 pm_runtime_disable(dev);
660 pm_runtime_set_active(dev);
661 pm_runtime_mark_last_busy(dev);
662 pm_runtime_enable(dev);
664 pm_runtime_resume(bus->dev);
666 link_flags = md_flags >> (bus->link_id * 8);
668 if (!(link_flags & SDW_INTEL_MASTER_DISABLE_PM_RUNTIME_IDLE))
669 pm_runtime_idle(dev);
672 ret = sdw_intel_link_power_up(sdw);
674 dev_err(dev, "%s failed: %d\n", __func__, ret);
679 * make sure all Slaves are tagged as UNATTACHED and provide
680 * reason for reinitialization
682 sdw_clear_slave_status(bus, SDW_UNATTACH_REQUEST_MASTER_RESET);
684 ret = sdw_intel_start_bus(sdw);
686 dev_err(dev, "cannot start bus during resume\n");
687 sdw_intel_link_power_down(sdw);
692 * after system resume, the pm_runtime suspend() may kick in
693 * during the enumeration, before any children device force the
694 * master device to remain active. Using pm_runtime_get()
695 * routines is not really possible, since it'd prevent the
696 * master from suspending.
697 * A reasonable compromise is to update the pm_runtime
698 * counters and delay the pm_runtime suspend by several
699 * seconds, by when all enumeration should be complete.
701 pm_runtime_mark_last_busy(bus->dev);
702 pm_runtime_mark_last_busy(dev);
707 static int __maybe_unused intel_resume_runtime(struct device *dev)
709 struct sdw_cdns *cdns = dev_get_drvdata(dev);
710 struct sdw_intel *sdw = cdns_to_intel(cdns);
711 struct sdw_bus *bus = &cdns->bus;
712 u32 clock_stop_quirks;
715 if (bus->prop.hw_disabled || !sdw->startup_done) {
716 dev_dbg(dev, "SoundWire master %d is disabled or not-started, ignoring\n",
721 /* unconditionally disable WAKEEN interrupt */
722 sdw_intel_shim_wake(sdw, false);
724 clock_stop_quirks = sdw->link_res->clock_stop_quirks;
726 if (clock_stop_quirks & SDW_INTEL_CLK_STOP_TEARDOWN) {
727 ret = sdw_intel_link_power_up(sdw);
729 dev_err(dev, "%s: power_up failed after teardown: %d\n", __func__, ret);
734 * make sure all Slaves are tagged as UNATTACHED and provide
735 * reason for reinitialization
737 sdw_clear_slave_status(bus, SDW_UNATTACH_REQUEST_MASTER_RESET);
739 ret = sdw_intel_start_bus(sdw);
741 dev_err(dev, "%s: cannot start bus after teardown: %d\n", __func__, ret);
742 sdw_intel_link_power_down(sdw);
746 } else if (clock_stop_quirks & SDW_INTEL_CLK_STOP_BUS_RESET) {
747 ret = sdw_intel_link_power_up(sdw);
749 dev_err(dev, "%s: power_up failed after bus reset: %d\n", __func__, ret);
753 ret = sdw_intel_start_bus_after_reset(sdw);
755 dev_err(dev, "%s: cannot start bus after reset: %d\n", __func__, ret);
756 sdw_intel_link_power_down(sdw);
759 } else if (!clock_stop_quirks) {
761 sdw_intel_check_clock_stop(sdw);
763 ret = sdw_intel_link_power_up(sdw);
765 dev_err(dev, "%s: power_up failed: %d\n", __func__, ret);
769 ret = sdw_intel_start_bus_after_clock_stop(sdw);
771 dev_err(dev, "%s: cannot start bus after clock stop: %d\n", __func__, ret);
772 sdw_intel_link_power_down(sdw);
776 dev_err(dev, "%s: clock_stop_quirks %x unsupported\n",
777 __func__, clock_stop_quirks);
784 static const struct dev_pm_ops intel_pm = {
785 .prepare = intel_pm_prepare,
786 SET_SYSTEM_SLEEP_PM_OPS(intel_suspend, intel_resume)
787 SET_RUNTIME_PM_OPS(intel_suspend_runtime, intel_resume_runtime, NULL)
790 static const struct auxiliary_device_id intel_link_id_table[] = {
791 { .name = "soundwire_intel.link" },
794 MODULE_DEVICE_TABLE(auxiliary, intel_link_id_table);
796 static struct auxiliary_driver sdw_intel_drv = {
797 .probe = intel_link_probe,
798 .remove = intel_link_remove,
800 /* auxiliary_driver_register() sets .name to be the modname */
803 .id_table = intel_link_id_table
805 module_auxiliary_driver(sdw_intel_drv);
807 MODULE_LICENSE("Dual BSD/GPL");
808 MODULE_DESCRIPTION("Intel Soundwire Link Driver");