blob: 96c6b2112feb6f37042dc8990ccae9d20207fa57 [file] [log] [blame]
Pierre-Louis Bossart7cbf00b2022-11-11 09:31:34 +08001// SPDX-License-Identifier: (GPL-2.0 OR BSD-3-Clause)
2// Copyright(c) 2015-22 Intel Corporation.
3
4/*
5 * Soundwire Intel Manager Driver
6 */
7
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>
13#include <linux/io.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"
22#include "bus.h"
23#include "intel.h"
24#include "intel_auxdevice.h"
25
26/* IDA min selected to avoid conflicts with HDaudio/iDISP SDI values */
27#define INTEL_DEV_NUM_IDA_MIN 4
28
29#define INTEL_MASTER_SUSPEND_DELAY_MS 3000
30
31/*
32 * debug/config flags for the Intel SoundWire Master.
33 *
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.
36 */
37
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)
42
43static int md_flags;
44module_param_named(sdw_md_flags, md_flags, int, 0444);
45MODULE_PARM_DESC(sdw_md_flags, "SoundWire Intel Master device flags (0x0 all off)");
46
47static int generic_pre_bank_switch(struct sdw_bus *bus)
48{
49 struct sdw_cdns *cdns = bus_to_cdns(bus);
50 struct sdw_intel *sdw = cdns_to_intel(cdns);
51
52 return sdw->link_res->hw_ops->pre_bank_switch(sdw);
53}
54
55static int generic_post_bank_switch(struct sdw_bus *bus)
56{
57 struct sdw_cdns *cdns = bus_to_cdns(bus);
58 struct sdw_intel *sdw = cdns_to_intel(cdns);
59
60 return sdw->link_res->hw_ops->post_bank_switch(sdw);
61}
62
63static int sdw_master_read_intel_prop(struct sdw_bus *bus)
64{
65 struct sdw_master_prop *prop = &bus->prop;
66 struct fwnode_handle *link;
67 char name[32];
68 u32 quirk_mask;
69
70 /* Find master handle */
71 snprintf(name, sizeof(name),
72 "mipi-sdw-link-%d-subproperties", bus->link_id);
73
74 link = device_get_named_child_node(bus->dev, name);
75 if (!link) {
76 dev_err(bus->dev, "Master node %s not found\n", name);
77 return -EIO;
78 }
79
80 fwnode_property_read_u32(link,
81 "intel-sdw-ip-clock",
82 &prop->mclk_freq);
83
84 /* the values reported by BIOS are the 2x clock, not the bus clock */
85 prop->mclk_freq /= 2;
86
87 fwnode_property_read_u32(link,
88 "intel-quirk-mask",
89 &quirk_mask);
90
91 if (quirk_mask & SDW_INTEL_QUIRK_MASK_BUS_DISABLE)
92 prop->hw_disabled = true;
93
94 prop->quirks = SDW_MASTER_QUIRKS_CLEAR_INITIAL_CLASH |
95 SDW_MASTER_QUIRKS_CLEAR_INITIAL_PARITY;
96
97 return 0;
98}
99
100static int intel_prop_read(struct sdw_bus *bus)
101{
102 /* Initialize with default handler to read all DisCo properties */
103 sdw_master_read_prop(bus);
104
105 /* read Intel-specific properties */
106 sdw_master_read_intel_prop(bus);
107
108 return 0;
109}
110
111static 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,
121};
122
123/*
124 * probe and init (aux_dev_id argument is required by function prototype but not used)
125 */
126static int intel_link_probe(struct auxiliary_device *auxdev,
127 const struct auxiliary_device_id *aux_dev_id)
128
129{
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;
134 struct sdw_bus *bus;
135 int ret;
136
137 sdw = devm_kzalloc(dev, sizeof(*sdw), GFP_KERNEL);
138 if (!sdw)
139 return -ENOMEM;
140
141 cdns = &sdw->cdns;
142 bus = &cdns->bus;
143
144 sdw->instance = auxdev->id;
145 sdw->link_res = &ldev->link_res;
146 cdns->dev = dev;
147 cdns->registers = sdw->link_res->registers;
148 cdns->instance = sdw->instance;
149 cdns->msg_count = 0;
150
151 bus->link_id = auxdev->id;
152 bus->dev_num_ida_min = INTEL_DEV_NUM_IDA_MIN;
153 bus->clk_stop_timeout = 1;
154
155 sdw_cdns_probe(cdns);
156
157 /* Set ops */
158 bus->ops = &sdw_intel_ops;
159
160 /* set driver data, accessed by snd_soc_dai_get_drvdata() */
161 auxiliary_set_drvdata(auxdev, cdns);
162
163 /* use generic bandwidth allocation algorithm */
164 sdw->cdns.bus.compute_params = sdw_compute_params;
165
166 /* avoid resuming from pm_runtime suspend if it's not required */
167 dev_pm_set_driver_flags(dev, DPM_FLAG_SMART_SUSPEND);
168
169 ret = sdw_bus_master_add(bus, dev, dev->fwnode);
170 if (ret) {
171 dev_err(dev, "sdw_bus_master_add fail: %d\n", ret);
172 return ret;
173 }
174
175 if (bus->prop.hw_disabled)
176 dev_info(dev,
177 "SoundWire master %d is disabled, will be ignored\n",
178 bus->link_id);
179 /*
180 * Ignore BIOS err_threshold, it's a really bad idea when dealing
181 * with multiple hardware synchronized links
182 */
183 bus->prop.err_threshold = 0;
184
185 return 0;
186}
187
188int intel_link_startup(struct auxiliary_device *auxdev)
189{
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;
194 int link_flags;
195 bool multi_link;
196 u32 clock_stop_quirks;
197 int ret;
198
199 if (bus->prop.hw_disabled) {
200 dev_info(dev,
201 "SoundWire master %d is disabled, ignoring\n",
202 sdw->instance);
203 return 0;
204 }
205
206 link_flags = md_flags >> (bus->link_id * 8);
207 multi_link = !(link_flags & SDW_INTEL_MASTER_DISABLE_MULTI_LINK);
208 if (!multi_link) {
209 dev_dbg(dev, "Multi-link is disabled\n");
210 } else {
211 /*
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
215 * mode is set.
216 */
217 bus->hw_sync_min_links = 1;
218 }
219 bus->multi_link = multi_link;
220
221 /* Initialize shim, controller */
222 ret = sdw_intel_link_power_up(sdw);
223 if (ret)
224 goto err_init;
225
226 /* Register DAIs */
227 ret = sdw_intel_register_dai(sdw);
228 if (ret) {
229 dev_err(dev, "DAI registration failed: %d\n", ret);
230 goto err_power_up;
231 }
232
233 sdw_intel_debugfs_init(sdw);
234
235 /* start bus */
236 ret = sdw_intel_start_bus(sdw);
237 if (ret) {
238 dev_err(dev, "bus start failed: %d\n", ret);
239 goto err_power_up;
240 }
241
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);
248
249 pm_runtime_set_active(dev);
250 pm_runtime_enable(dev);
251 }
252
253 clock_stop_quirks = sdw->link_res->clock_stop_quirks;
254 if (clock_stop_quirks & SDW_INTEL_CLK_STOP_NOT_ALLOWED) {
255 /*
256 * To keep the clock running we need to prevent
257 * pm_runtime suspend from happening by increasing the
258 * reference count.
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.
263 */
264 pm_runtime_get_noresume(dev);
265 }
266
267 /*
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.
273 *
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.
279 */
280 if (!(link_flags & SDW_INTEL_MASTER_DISABLE_PM_RUNTIME_IDLE))
281 pm_runtime_idle(dev);
282
283 sdw->startup_done = true;
284 return 0;
285
286err_power_up:
287 sdw_intel_link_power_down(sdw);
288err_init:
289 return ret;
290}
291
292static void intel_link_remove(struct auxiliary_device *auxdev)
293{
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;
297
298 /*
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
302 */
303 if (!bus->prop.hw_disabled) {
304 sdw_intel_debugfs_exit(sdw);
305 sdw_cdns_enable_interrupt(cdns, false);
306 }
307 sdw_bus_master_delete(bus);
308}
309
310int intel_link_process_wakeen_event(struct auxiliary_device *auxdev)
311{
312 struct device *dev = &auxdev->dev;
313 struct sdw_intel *sdw;
314 struct sdw_bus *bus;
315
316 sdw = auxiliary_get_drvdata(auxdev);
317 bus = &sdw->cdns.bus;
318
319 if (bus->prop.hw_disabled || !sdw->startup_done) {
320 dev_dbg(dev, "SoundWire master %d is disabled or not-started, ignoring\n",
321 bus->link_id);
322 return 0;
323 }
324
325 if (!sdw_intel_shim_check_wake(sdw))
326 return 0;
327
328 /* disable WAKEEN interrupt ASAP to prevent interrupt flood */
329 sdw_intel_shim_wake(sdw, false);
330
331 /*
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.
337 */
338 pm_request_resume(dev);
339
340 return 0;
341}
342
343/*
344 * PM calls
345 */
346
347static int intel_resume_child_device(struct device *dev, void *data)
348{
349 int ret;
350 struct sdw_slave *slave = dev_to_sdw_dev(dev);
351
352 if (!slave->probed) {
353 dev_dbg(dev, "skipping device, no probed driver\n");
354 return 0;
355 }
356 if (!slave->dev_num_sticky) {
357 dev_dbg(dev, "skipping device, never detected on bus\n");
358 return 0;
359 }
360
361 ret = pm_request_resume(dev);
362 if (ret < 0)
363 dev_err(dev, "%s: pm_request_resume failed: %d\n", __func__, ret);
364
365 return ret;
366}
367
368static int __maybe_unused intel_pm_prepare(struct device *dev)
369{
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;
374 int ret;
375
376 if (bus->prop.hw_disabled || !sdw->startup_done) {
377 dev_dbg(dev, "SoundWire master %d is disabled or not-started, ignoring\n",
378 bus->link_id);
379 return 0;
380 }
381
382 clock_stop_quirks = sdw->link_res->clock_stop_quirks;
383
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)) {
388 /*
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
392 */
393
394 /*
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
397 * on resume.
398 */
399
400 /*
401 * first resume the device for this link. This will also by construction
402 * resume the PCI parent device.
403 */
404 ret = pm_request_resume(dev);
405 if (ret < 0) {
406 dev_err(dev, "%s: pm_request_resume failed: %d\n", __func__, ret);
407 return 0;
408 }
409
410 /*
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
413 * this is a no-op.
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.
418 */
419 ret = device_for_each_child(bus->dev, NULL, intel_resume_child_device);
420
421 if (ret < 0)
422 dev_err(dev, "%s: intel_resume_child_device failed: %d\n", __func__, ret);
423 }
424
425 return 0;
426}
427
428static int __maybe_unused intel_suspend(struct device *dev)
429{
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;
434 int ret;
435
436 if (bus->prop.hw_disabled || !sdw->startup_done) {
437 dev_dbg(dev, "SoundWire master %d is disabled or not-started, ignoring\n",
438 bus->link_id);
439 return 0;
440 }
441
442 if (pm_runtime_suspended(dev)) {
443 dev_dbg(dev, "pm_runtime status: suspended\n");
444
445 clock_stop_quirks = sdw->link_res->clock_stop_quirks;
446
447 if ((clock_stop_quirks & SDW_INTEL_CLK_STOP_BUS_RESET) ||
448 !clock_stop_quirks) {
449
450 if (pm_runtime_suspended(dev->parent)) {
451 /*
452 * paranoia check: this should not happen with the .prepare
453 * resume to full power
454 */
455 dev_err(dev, "%s: invalid config: parent is suspended\n", __func__);
456 } else {
457 sdw_intel_shim_wake(sdw, false);
458 }
459 }
460
461 return 0;
462 }
463
464 ret = sdw_intel_stop_bus(sdw, false);
465 if (ret < 0) {
466 dev_err(dev, "%s: cannot stop bus: %d\n", __func__, ret);
467 return ret;
468 }
469
470 return 0;
471}
472
473static int __maybe_unused intel_suspend_runtime(struct device *dev)
474{
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;
479 int ret;
480
481 if (bus->prop.hw_disabled || !sdw->startup_done) {
482 dev_dbg(dev, "SoundWire master %d is disabled or not-started, ignoring\n",
483 bus->link_id);
484 return 0;
485 }
486
487 clock_stop_quirks = sdw->link_res->clock_stop_quirks;
488
489 if (clock_stop_quirks & SDW_INTEL_CLK_STOP_TEARDOWN) {
490 ret = sdw_intel_stop_bus(sdw, false);
491 if (ret < 0) {
492 dev_err(dev, "%s: cannot stop bus during teardown: %d\n",
493 __func__, ret);
494 return ret;
495 }
496 } else if (clock_stop_quirks & SDW_INTEL_CLK_STOP_BUS_RESET || !clock_stop_quirks) {
497 ret = sdw_intel_stop_bus(sdw, true);
498 if (ret < 0) {
499 dev_err(dev, "%s: cannot stop bus during clock_stop: %d\n",
500 __func__, ret);
501 return ret;
502 }
503 } else {
504 dev_err(dev, "%s clock_stop_quirks %x unsupported\n",
505 __func__, clock_stop_quirks);
506 ret = -EINVAL;
507 }
508
509 return ret;
510}
511
512static int __maybe_unused intel_resume(struct device *dev)
513{
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;
517 int link_flags;
518 int ret;
519
520 if (bus->prop.hw_disabled || !sdw->startup_done) {
521 dev_dbg(dev, "SoundWire master %d is disabled or not-started, ignoring\n",
522 bus->link_id);
523 return 0;
524 }
525
526 link_flags = md_flags >> (bus->link_id * 8);
527
528 if (pm_runtime_suspended(dev)) {
529 dev_dbg(dev, "pm_runtime status was suspended, forcing active\n");
530
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);
536
537 link_flags = md_flags >> (bus->link_id * 8);
538
539 if (!(link_flags & SDW_INTEL_MASTER_DISABLE_PM_RUNTIME_IDLE))
540 pm_runtime_idle(dev);
541 }
542
543 ret = sdw_intel_link_power_up(sdw);
544 if (ret) {
545 dev_err(dev, "%s failed: %d\n", __func__, ret);
546 return ret;
547 }
548
549 /*
550 * make sure all Slaves are tagged as UNATTACHED and provide
551 * reason for reinitialization
552 */
553 sdw_clear_slave_status(bus, SDW_UNATTACH_REQUEST_MASTER_RESET);
554
555 ret = sdw_intel_start_bus(sdw);
556 if (ret < 0) {
557 dev_err(dev, "cannot start bus during resume\n");
558 sdw_intel_link_power_down(sdw);
559 return ret;
560 }
561
562 /*
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.
571 */
572 pm_runtime_mark_last_busy(dev);
573
574 return 0;
575}
576
577static int __maybe_unused intel_resume_runtime(struct device *dev)
578{
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;
583 int ret;
584
585 if (bus->prop.hw_disabled || !sdw->startup_done) {
586 dev_dbg(dev, "SoundWire master %d is disabled or not-started, ignoring\n",
587 bus->link_id);
588 return 0;
589 }
590
591 /* unconditionally disable WAKEEN interrupt */
592 sdw_intel_shim_wake(sdw, false);
593
594 clock_stop_quirks = sdw->link_res->clock_stop_quirks;
595
596 if (clock_stop_quirks & SDW_INTEL_CLK_STOP_TEARDOWN) {
597 ret = sdw_intel_link_power_up(sdw);
598 if (ret) {
599 dev_err(dev, "%s: power_up failed after teardown: %d\n", __func__, ret);
600 return ret;
601 }
602
603 /*
604 * make sure all Slaves are tagged as UNATTACHED and provide
605 * reason for reinitialization
606 */
607 sdw_clear_slave_status(bus, SDW_UNATTACH_REQUEST_MASTER_RESET);
608
609 ret = sdw_intel_start_bus(sdw);
610 if (ret < 0) {
611 dev_err(dev, "%s: cannot start bus after teardown: %d\n", __func__, ret);
612 sdw_intel_link_power_down(sdw);
613 return ret;
614 }
615
616 } else if (clock_stop_quirks & SDW_INTEL_CLK_STOP_BUS_RESET) {
617 ret = sdw_intel_link_power_up(sdw);
618 if (ret) {
619 dev_err(dev, "%s: power_up failed after bus reset: %d\n", __func__, ret);
620 return ret;
621 }
622
623 ret = sdw_intel_start_bus_after_reset(sdw);
624 if (ret < 0) {
625 dev_err(dev, "%s: cannot start bus after reset: %d\n", __func__, ret);
626 sdw_intel_link_power_down(sdw);
627 return ret;
628 }
629 } else if (!clock_stop_quirks) {
630
631 sdw_intel_check_clock_stop(sdw);
632
633 ret = sdw_intel_link_power_up(sdw);
634 if (ret) {
635 dev_err(dev, "%s: power_up failed: %d\n", __func__, ret);
636 return ret;
637 }
638
639 ret = sdw_intel_start_bus_after_clock_stop(sdw);
640 if (ret < 0) {
641 dev_err(dev, "%s: cannot start bus after clock stop: %d\n", __func__, ret);
642 sdw_intel_link_power_down(sdw);
643 return ret;
644 }
645 } else {
646 dev_err(dev, "%s: clock_stop_quirks %x unsupported\n",
647 __func__, clock_stop_quirks);
648 ret = -EINVAL;
649 }
650
651 return ret;
652}
653
654static 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)
658};
659
660static const struct auxiliary_device_id intel_link_id_table[] = {
661 { .name = "soundwire_intel.link" },
662 {},
663};
664MODULE_DEVICE_TABLE(auxiliary, intel_link_id_table);
665
666static struct auxiliary_driver sdw_intel_drv = {
667 .probe = intel_link_probe,
668 .remove = intel_link_remove,
669 .driver = {
670 /* auxiliary_driver_register() sets .name to be the modname */
671 .pm = &intel_pm,
672 },
673 .id_table = intel_link_id_table
674};
675module_auxiliary_driver(sdw_intel_drv);
676
677MODULE_LICENSE("Dual BSD/GPL");
678MODULE_DESCRIPTION("Intel Soundwire Link Driver");