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);
251 /* Enable runtime PM */
252 if (!(link_flags & SDW_INTEL_MASTER_DISABLE_PM_RUNTIME)) {
253 pm_runtime_set_autosuspend_delay(dev,
254 INTEL_MASTER_SUSPEND_DELAY_MS);
255 pm_runtime_use_autosuspend(dev);
256 pm_runtime_mark_last_busy(dev);
258 pm_runtime_set_active(dev);
259 pm_runtime_enable(dev);
263 ret = sdw_intel_start_bus(sdw);
265 dev_err(dev, "bus start failed: %d\n", ret);
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_mark_last_busy(dev);
298 pm_runtime_idle(dev);
301 sdw->startup_done = true;
305 if (!(link_flags & SDW_INTEL_MASTER_DISABLE_PM_RUNTIME))
306 pm_runtime_disable(dev);
308 sdw_intel_link_power_down(sdw);
313 static void intel_link_remove(struct auxiliary_device *auxdev)
315 struct sdw_cdns *cdns = auxiliary_get_drvdata(auxdev);
316 struct sdw_intel *sdw = cdns_to_intel(cdns);
317 struct sdw_bus *bus = &cdns->bus;
320 * Since pm_runtime is already disabled, we don't decrease
321 * the refcount when the clock_stop_quirk is
322 * SDW_INTEL_CLK_STOP_NOT_ALLOWED
324 if (!bus->prop.hw_disabled) {
325 sdw_intel_debugfs_exit(sdw);
326 sdw_cdns_enable_interrupt(cdns, false);
328 sdw_bus_master_delete(bus);
331 int intel_link_process_wakeen_event(struct auxiliary_device *auxdev)
333 struct device *dev = &auxdev->dev;
334 struct sdw_intel *sdw;
337 sdw = auxiliary_get_drvdata(auxdev);
338 bus = &sdw->cdns.bus;
340 if (bus->prop.hw_disabled || !sdw->startup_done) {
341 dev_dbg(dev, "SoundWire master %d is disabled or not-started, ignoring\n",
346 if (!sdw_intel_shim_check_wake(sdw))
349 /* disable WAKEEN interrupt ASAP to prevent interrupt flood */
350 sdw_intel_shim_wake(sdw, false);
353 * resume the Master, which will generate a bus reset and result in
354 * Slaves re-attaching and be re-enumerated. The SoundWire physical
355 * device which generated the wake will trigger an interrupt, which
356 * will in turn cause the corresponding Linux Slave device to be
357 * resumed and the Slave codec driver to check the status.
359 pm_request_resume(dev);
368 static int intel_resume_child_device(struct device *dev, void *data)
371 struct sdw_slave *slave = dev_to_sdw_dev(dev);
373 if (!slave->probed) {
374 dev_dbg(dev, "skipping device, no probed driver\n");
377 if (!slave->dev_num_sticky) {
378 dev_dbg(dev, "skipping device, never detected on bus\n");
382 ret = pm_request_resume(dev);
384 dev_err(dev, "%s: pm_request_resume failed: %d\n", __func__, ret);
391 static int __maybe_unused intel_pm_prepare(struct device *dev)
393 struct sdw_cdns *cdns = dev_get_drvdata(dev);
394 struct sdw_intel *sdw = cdns_to_intel(cdns);
395 struct sdw_bus *bus = &cdns->bus;
396 u32 clock_stop_quirks;
399 if (bus->prop.hw_disabled || !sdw->startup_done) {
400 dev_dbg(dev, "SoundWire master %d is disabled or not-started, ignoring\n",
405 clock_stop_quirks = sdw->link_res->clock_stop_quirks;
407 if (pm_runtime_suspended(dev) &&
408 pm_runtime_suspended(dev->parent) &&
409 ((clock_stop_quirks & SDW_INTEL_CLK_STOP_BUS_RESET) ||
410 !clock_stop_quirks)) {
412 * if we've enabled clock stop, and the parent is suspended, the SHIM registers
413 * are not accessible and the shim wake cannot be disabled.
414 * The only solution is to resume the entire bus to full power
418 * If any operation in this block fails, we keep going since we don't want
419 * to prevent system suspend from happening and errors should be recoverable
424 * first resume the device for this link. This will also by construction
425 * resume the PCI parent device.
427 ret = pm_request_resume(dev);
429 dev_err(dev, "%s: pm_request_resume failed: %d\n", __func__, ret);
434 * Continue resuming the entire bus (parent + child devices) to exit
435 * the clock stop mode. If there are no devices connected on this link
437 * The resume to full power could have been implemented with a .prepare
438 * step in SoundWire codec drivers. This would however require a lot
439 * of code to handle an Intel-specific corner case. It is simpler in
440 * practice to add a loop at the link level.
442 ret = device_for_each_child(bus->dev, NULL, intel_resume_child_device);
445 dev_err(dev, "%s: intel_resume_child_device failed: %d\n", __func__, ret);
451 static int __maybe_unused intel_suspend(struct device *dev)
453 struct sdw_cdns *cdns = dev_get_drvdata(dev);
454 struct sdw_intel *sdw = cdns_to_intel(cdns);
455 struct sdw_bus *bus = &cdns->bus;
456 u32 clock_stop_quirks;
459 if (bus->prop.hw_disabled || !sdw->startup_done) {
460 dev_dbg(dev, "SoundWire master %d is disabled or not-started, ignoring\n",
465 if (pm_runtime_suspended(dev)) {
466 dev_dbg(dev, "pm_runtime status: suspended\n");
468 clock_stop_quirks = sdw->link_res->clock_stop_quirks;
470 if ((clock_stop_quirks & SDW_INTEL_CLK_STOP_BUS_RESET) ||
471 !clock_stop_quirks) {
473 if (pm_runtime_suspended(dev->parent)) {
475 * paranoia check: this should not happen with the .prepare
476 * resume to full power
478 dev_err(dev, "%s: invalid config: parent is suspended\n", __func__);
480 sdw_intel_shim_wake(sdw, false);
487 ret = sdw_intel_stop_bus(sdw, false);
489 dev_err(dev, "%s: cannot stop bus: %d\n", __func__, ret);
496 static int __maybe_unused intel_suspend_runtime(struct device *dev)
498 struct sdw_cdns *cdns = dev_get_drvdata(dev);
499 struct sdw_intel *sdw = cdns_to_intel(cdns);
500 struct sdw_bus *bus = &cdns->bus;
501 u32 clock_stop_quirks;
504 if (bus->prop.hw_disabled || !sdw->startup_done) {
505 dev_dbg(dev, "SoundWire master %d is disabled or not-started, ignoring\n",
510 clock_stop_quirks = sdw->link_res->clock_stop_quirks;
512 if (clock_stop_quirks & SDW_INTEL_CLK_STOP_TEARDOWN) {
513 ret = sdw_intel_stop_bus(sdw, false);
515 dev_err(dev, "%s: cannot stop bus during teardown: %d\n",
519 } else if (clock_stop_quirks & SDW_INTEL_CLK_STOP_BUS_RESET || !clock_stop_quirks) {
520 ret = sdw_intel_stop_bus(sdw, true);
522 dev_err(dev, "%s: cannot stop bus during clock_stop: %d\n",
527 dev_err(dev, "%s clock_stop_quirks %x unsupported\n",
528 __func__, clock_stop_quirks);
535 static int __maybe_unused intel_resume(struct device *dev)
537 struct sdw_cdns *cdns = dev_get_drvdata(dev);
538 struct sdw_intel *sdw = cdns_to_intel(cdns);
539 struct sdw_bus *bus = &cdns->bus;
543 if (bus->prop.hw_disabled || !sdw->startup_done) {
544 dev_dbg(dev, "SoundWire master %d is disabled or not-started, ignoring\n",
549 link_flags = md_flags >> (bus->link_id * 8);
551 if (pm_runtime_suspended(dev)) {
552 dev_dbg(dev, "pm_runtime status was suspended, forcing active\n");
554 /* follow required sequence from runtime_pm.rst */
555 pm_runtime_disable(dev);
556 pm_runtime_set_active(dev);
557 pm_runtime_mark_last_busy(dev);
558 pm_runtime_enable(dev);
560 link_flags = md_flags >> (bus->link_id * 8);
562 if (!(link_flags & SDW_INTEL_MASTER_DISABLE_PM_RUNTIME_IDLE))
563 pm_runtime_idle(dev);
566 ret = sdw_intel_link_power_up(sdw);
568 dev_err(dev, "%s failed: %d\n", __func__, ret);
573 * make sure all Slaves are tagged as UNATTACHED and provide
574 * reason for reinitialization
576 sdw_clear_slave_status(bus, SDW_UNATTACH_REQUEST_MASTER_RESET);
578 ret = sdw_intel_start_bus(sdw);
580 dev_err(dev, "cannot start bus during resume\n");
581 sdw_intel_link_power_down(sdw);
586 * after system resume, the pm_runtime suspend() may kick in
587 * during the enumeration, before any children device force the
588 * master device to remain active. Using pm_runtime_get()
589 * routines is not really possible, since it'd prevent the
590 * master from suspending.
591 * A reasonable compromise is to update the pm_runtime
592 * counters and delay the pm_runtime suspend by several
593 * seconds, by when all enumeration should be complete.
595 pm_runtime_mark_last_busy(dev);
600 static int __maybe_unused intel_resume_runtime(struct device *dev)
602 struct sdw_cdns *cdns = dev_get_drvdata(dev);
603 struct sdw_intel *sdw = cdns_to_intel(cdns);
604 struct sdw_bus *bus = &cdns->bus;
605 u32 clock_stop_quirks;
608 if (bus->prop.hw_disabled || !sdw->startup_done) {
609 dev_dbg(dev, "SoundWire master %d is disabled or not-started, ignoring\n",
614 /* unconditionally disable WAKEEN interrupt */
615 sdw_intel_shim_wake(sdw, false);
617 clock_stop_quirks = sdw->link_res->clock_stop_quirks;
619 if (clock_stop_quirks & SDW_INTEL_CLK_STOP_TEARDOWN) {
620 ret = sdw_intel_link_power_up(sdw);
622 dev_err(dev, "%s: power_up failed after teardown: %d\n", __func__, ret);
627 * make sure all Slaves are tagged as UNATTACHED and provide
628 * reason for reinitialization
630 sdw_clear_slave_status(bus, SDW_UNATTACH_REQUEST_MASTER_RESET);
632 ret = sdw_intel_start_bus(sdw);
634 dev_err(dev, "%s: cannot start bus after teardown: %d\n", __func__, ret);
635 sdw_intel_link_power_down(sdw);
639 } else if (clock_stop_quirks & SDW_INTEL_CLK_STOP_BUS_RESET) {
640 ret = sdw_intel_link_power_up(sdw);
642 dev_err(dev, "%s: power_up failed after bus reset: %d\n", __func__, ret);
646 ret = sdw_intel_start_bus_after_reset(sdw);
648 dev_err(dev, "%s: cannot start bus after reset: %d\n", __func__, ret);
649 sdw_intel_link_power_down(sdw);
652 } else if (!clock_stop_quirks) {
654 sdw_intel_check_clock_stop(sdw);
656 ret = sdw_intel_link_power_up(sdw);
658 dev_err(dev, "%s: power_up failed: %d\n", __func__, ret);
662 ret = sdw_intel_start_bus_after_clock_stop(sdw);
664 dev_err(dev, "%s: cannot start bus after clock stop: %d\n", __func__, ret);
665 sdw_intel_link_power_down(sdw);
669 dev_err(dev, "%s: clock_stop_quirks %x unsupported\n",
670 __func__, clock_stop_quirks);
677 static const struct dev_pm_ops intel_pm = {
678 .prepare = intel_pm_prepare,
679 SET_SYSTEM_SLEEP_PM_OPS(intel_suspend, intel_resume)
680 SET_RUNTIME_PM_OPS(intel_suspend_runtime, intel_resume_runtime, NULL)
683 static const struct auxiliary_device_id intel_link_id_table[] = {
684 { .name = "soundwire_intel.link" },
687 MODULE_DEVICE_TABLE(auxiliary, intel_link_id_table);
689 static struct auxiliary_driver sdw_intel_drv = {
690 .probe = intel_link_probe,
691 .remove = intel_link_remove,
693 /* auxiliary_driver_register() sets .name to be the modname */
696 .id_table = intel_link_id_table
698 module_auxiliary_driver(sdw_intel_drv);
700 MODULE_LICENSE("Dual BSD/GPL");
701 MODULE_DESCRIPTION("Intel Soundwire Link Driver");