Merge tag 'for-6.3-rc3-tag' of git://git.kernel.org/pub/scm/linux/kernel/git/kdave...
[linux-block.git] / drivers / soundwire / intel_auxdevice.c
CommitLineData
7cbf00bd
PLB
1// 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,
7cbf00bd
PLB
116 .set_bus_conf = cdns_bus_conf,
117 .pre_bank_switch = generic_pre_bank_switch,
118 .post_bank_switch = generic_post_bank_switch,
119 .read_ping_status = cdns_read_ping_status,
120};
121
122/*
123 * probe and init (aux_dev_id argument is required by function prototype but not used)
124 */
125static int intel_link_probe(struct auxiliary_device *auxdev,
126 const struct auxiliary_device_id *aux_dev_id)
127
128{
129 struct device *dev = &auxdev->dev;
130 struct sdw_intel_link_dev *ldev = auxiliary_dev_to_sdw_intel_link_dev(auxdev);
131 struct sdw_intel *sdw;
132 struct sdw_cdns *cdns;
133 struct sdw_bus *bus;
134 int ret;
135
136 sdw = devm_kzalloc(dev, sizeof(*sdw), GFP_KERNEL);
137 if (!sdw)
138 return -ENOMEM;
139
140 cdns = &sdw->cdns;
141 bus = &cdns->bus;
142
143 sdw->instance = auxdev->id;
144 sdw->link_res = &ldev->link_res;
145 cdns->dev = dev;
146 cdns->registers = sdw->link_res->registers;
147 cdns->instance = sdw->instance;
148 cdns->msg_count = 0;
149
150 bus->link_id = auxdev->id;
151 bus->dev_num_ida_min = INTEL_DEV_NUM_IDA_MIN;
152 bus->clk_stop_timeout = 1;
153
154 sdw_cdns_probe(cdns);
155
156 /* Set ops */
157 bus->ops = &sdw_intel_ops;
158
159 /* set driver data, accessed by snd_soc_dai_get_drvdata() */
160 auxiliary_set_drvdata(auxdev, cdns);
161
162 /* use generic bandwidth allocation algorithm */
163 sdw->cdns.bus.compute_params = sdw_compute_params;
164
165 /* avoid resuming from pm_runtime suspend if it's not required */
166 dev_pm_set_driver_flags(dev, DPM_FLAG_SMART_SUSPEND);
167
168 ret = sdw_bus_master_add(bus, dev, dev->fwnode);
169 if (ret) {
170 dev_err(dev, "sdw_bus_master_add fail: %d\n", ret);
171 return ret;
172 }
173
174 if (bus->prop.hw_disabled)
175 dev_info(dev,
176 "SoundWire master %d is disabled, will be ignored\n",
177 bus->link_id);
178 /*
179 * Ignore BIOS err_threshold, it's a really bad idea when dealing
180 * with multiple hardware synchronized links
181 */
182 bus->prop.err_threshold = 0;
183
184 return 0;
185}
186
187int intel_link_startup(struct auxiliary_device *auxdev)
188{
189 struct device *dev = &auxdev->dev;
190 struct sdw_cdns *cdns = auxiliary_get_drvdata(auxdev);
191 struct sdw_intel *sdw = cdns_to_intel(cdns);
192 struct sdw_bus *bus = &cdns->bus;
193 int link_flags;
194 bool multi_link;
195 u32 clock_stop_quirks;
196 int ret;
197
198 if (bus->prop.hw_disabled) {
199 dev_info(dev,
200 "SoundWire master %d is disabled, ignoring\n",
201 sdw->instance);
202 return 0;
203 }
204
205 link_flags = md_flags >> (bus->link_id * 8);
206 multi_link = !(link_flags & SDW_INTEL_MASTER_DISABLE_MULTI_LINK);
207 if (!multi_link) {
208 dev_dbg(dev, "Multi-link is disabled\n");
209 } else {
210 /*
211 * hardware-based synchronization is required regardless
212 * of the number of segments used by a stream: SSP-based
213 * synchronization is gated by gsync when the multi-master
214 * mode is set.
215 */
216 bus->hw_sync_min_links = 1;
217 }
218 bus->multi_link = multi_link;
219
220 /* Initialize shim, controller */
221 ret = sdw_intel_link_power_up(sdw);
222 if (ret)
223 goto err_init;
224
225 /* Register DAIs */
226 ret = sdw_intel_register_dai(sdw);
227 if (ret) {
228 dev_err(dev, "DAI registration failed: %d\n", ret);
229 goto err_power_up;
230 }
231
232 sdw_intel_debugfs_init(sdw);
233
234 /* start bus */
235 ret = sdw_intel_start_bus(sdw);
236 if (ret) {
237 dev_err(dev, "bus start failed: %d\n", ret);
238 goto err_power_up;
239 }
240
241 /* Enable runtime PM */
242 if (!(link_flags & SDW_INTEL_MASTER_DISABLE_PM_RUNTIME)) {
243 pm_runtime_set_autosuspend_delay(dev,
244 INTEL_MASTER_SUSPEND_DELAY_MS);
245 pm_runtime_use_autosuspend(dev);
246 pm_runtime_mark_last_busy(dev);
247
248 pm_runtime_set_active(dev);
249 pm_runtime_enable(dev);
250 }
251
252 clock_stop_quirks = sdw->link_res->clock_stop_quirks;
253 if (clock_stop_quirks & SDW_INTEL_CLK_STOP_NOT_ALLOWED) {
254 /*
255 * To keep the clock running we need to prevent
256 * pm_runtime suspend from happening by increasing the
257 * reference count.
258 * This quirk is specified by the parent PCI device in
259 * case of specific latency requirements. It will have
260 * no effect if pm_runtime is disabled by the user via
261 * a module parameter for testing purposes.
262 */
263 pm_runtime_get_noresume(dev);
264 }
265
266 /*
267 * The runtime PM status of Slave devices is "Unsupported"
268 * until they report as ATTACHED. If they don't, e.g. because
269 * there are no Slave devices populated or if the power-on is
270 * delayed or dependent on a power switch, the Master will
271 * remain active and prevent its parent from suspending.
272 *
273 * Conditionally force the pm_runtime core to re-evaluate the
274 * Master status in the absence of any Slave activity. A quirk
275 * is provided to e.g. deal with Slaves that may be powered on
276 * with a delay. A more complete solution would require the
277 * definition of Master properties.
278 */
279 if (!(link_flags & SDW_INTEL_MASTER_DISABLE_PM_RUNTIME_IDLE))
280 pm_runtime_idle(dev);
281
282 sdw->startup_done = true;
283 return 0;
284
285err_power_up:
286 sdw_intel_link_power_down(sdw);
287err_init:
288 return ret;
289}
290
291static void intel_link_remove(struct auxiliary_device *auxdev)
292{
293 struct sdw_cdns *cdns = auxiliary_get_drvdata(auxdev);
294 struct sdw_intel *sdw = cdns_to_intel(cdns);
295 struct sdw_bus *bus = &cdns->bus;
296
297 /*
298 * Since pm_runtime is already disabled, we don't decrease
299 * the refcount when the clock_stop_quirk is
300 * SDW_INTEL_CLK_STOP_NOT_ALLOWED
301 */
302 if (!bus->prop.hw_disabled) {
303 sdw_intel_debugfs_exit(sdw);
304 sdw_cdns_enable_interrupt(cdns, false);
305 }
306 sdw_bus_master_delete(bus);
307}
308
309int intel_link_process_wakeen_event(struct auxiliary_device *auxdev)
310{
311 struct device *dev = &auxdev->dev;
312 struct sdw_intel *sdw;
313 struct sdw_bus *bus;
314
315 sdw = auxiliary_get_drvdata(auxdev);
316 bus = &sdw->cdns.bus;
317
318 if (bus->prop.hw_disabled || !sdw->startup_done) {
319 dev_dbg(dev, "SoundWire master %d is disabled or not-started, ignoring\n",
320 bus->link_id);
321 return 0;
322 }
323
324 if (!sdw_intel_shim_check_wake(sdw))
325 return 0;
326
327 /* disable WAKEEN interrupt ASAP to prevent interrupt flood */
328 sdw_intel_shim_wake(sdw, false);
329
330 /*
331 * resume the Master, which will generate a bus reset and result in
332 * Slaves re-attaching and be re-enumerated. The SoundWire physical
333 * device which generated the wake will trigger an interrupt, which
334 * will in turn cause the corresponding Linux Slave device to be
335 * resumed and the Slave codec driver to check the status.
336 */
337 pm_request_resume(dev);
338
339 return 0;
340}
341
342/*
343 * PM calls
344 */
345
346static int intel_resume_child_device(struct device *dev, void *data)
347{
348 int ret;
349 struct sdw_slave *slave = dev_to_sdw_dev(dev);
350
351 if (!slave->probed) {
352 dev_dbg(dev, "skipping device, no probed driver\n");
353 return 0;
354 }
355 if (!slave->dev_num_sticky) {
356 dev_dbg(dev, "skipping device, never detected on bus\n");
357 return 0;
358 }
359
360 ret = pm_request_resume(dev);
361 if (ret < 0)
362 dev_err(dev, "%s: pm_request_resume failed: %d\n", __func__, ret);
363
364 return ret;
365}
366
367static int __maybe_unused intel_pm_prepare(struct device *dev)
368{
369 struct sdw_cdns *cdns = dev_get_drvdata(dev);
370 struct sdw_intel *sdw = cdns_to_intel(cdns);
371 struct sdw_bus *bus = &cdns->bus;
372 u32 clock_stop_quirks;
373 int ret;
374
375 if (bus->prop.hw_disabled || !sdw->startup_done) {
376 dev_dbg(dev, "SoundWire master %d is disabled or not-started, ignoring\n",
377 bus->link_id);
378 return 0;
379 }
380
381 clock_stop_quirks = sdw->link_res->clock_stop_quirks;
382
383 if (pm_runtime_suspended(dev) &&
384 pm_runtime_suspended(dev->parent) &&
385 ((clock_stop_quirks & SDW_INTEL_CLK_STOP_BUS_RESET) ||
386 !clock_stop_quirks)) {
387 /*
388 * if we've enabled clock stop, and the parent is suspended, the SHIM registers
389 * are not accessible and the shim wake cannot be disabled.
390 * The only solution is to resume the entire bus to full power
391 */
392
393 /*
394 * If any operation in this block fails, we keep going since we don't want
395 * to prevent system suspend from happening and errors should be recoverable
396 * on resume.
397 */
398
399 /*
400 * first resume the device for this link. This will also by construction
401 * resume the PCI parent device.
402 */
403 ret = pm_request_resume(dev);
404 if (ret < 0) {
405 dev_err(dev, "%s: pm_request_resume failed: %d\n", __func__, ret);
406 return 0;
407 }
408
409 /*
410 * Continue resuming the entire bus (parent + child devices) to exit
411 * the clock stop mode. If there are no devices connected on this link
412 * this is a no-op.
413 * The resume to full power could have been implemented with a .prepare
414 * step in SoundWire codec drivers. This would however require a lot
415 * of code to handle an Intel-specific corner case. It is simpler in
416 * practice to add a loop at the link level.
417 */
418 ret = device_for_each_child(bus->dev, NULL, intel_resume_child_device);
419
420 if (ret < 0)
421 dev_err(dev, "%s: intel_resume_child_device failed: %d\n", __func__, ret);
422 }
423
424 return 0;
425}
426
427static int __maybe_unused intel_suspend(struct device *dev)
428{
429 struct sdw_cdns *cdns = dev_get_drvdata(dev);
430 struct sdw_intel *sdw = cdns_to_intel(cdns);
431 struct sdw_bus *bus = &cdns->bus;
432 u32 clock_stop_quirks;
433 int ret;
434
435 if (bus->prop.hw_disabled || !sdw->startup_done) {
436 dev_dbg(dev, "SoundWire master %d is disabled or not-started, ignoring\n",
437 bus->link_id);
438 return 0;
439 }
440
441 if (pm_runtime_suspended(dev)) {
442 dev_dbg(dev, "pm_runtime status: suspended\n");
443
444 clock_stop_quirks = sdw->link_res->clock_stop_quirks;
445
446 if ((clock_stop_quirks & SDW_INTEL_CLK_STOP_BUS_RESET) ||
447 !clock_stop_quirks) {
448
449 if (pm_runtime_suspended(dev->parent)) {
450 /*
451 * paranoia check: this should not happen with the .prepare
452 * resume to full power
453 */
454 dev_err(dev, "%s: invalid config: parent is suspended\n", __func__);
455 } else {
456 sdw_intel_shim_wake(sdw, false);
457 }
458 }
459
460 return 0;
461 }
462
463 ret = sdw_intel_stop_bus(sdw, false);
464 if (ret < 0) {
465 dev_err(dev, "%s: cannot stop bus: %d\n", __func__, ret);
466 return ret;
467 }
468
469 return 0;
470}
471
472static int __maybe_unused intel_suspend_runtime(struct device *dev)
473{
474 struct sdw_cdns *cdns = dev_get_drvdata(dev);
475 struct sdw_intel *sdw = cdns_to_intel(cdns);
476 struct sdw_bus *bus = &cdns->bus;
477 u32 clock_stop_quirks;
478 int ret;
479
480 if (bus->prop.hw_disabled || !sdw->startup_done) {
481 dev_dbg(dev, "SoundWire master %d is disabled or not-started, ignoring\n",
482 bus->link_id);
483 return 0;
484 }
485
486 clock_stop_quirks = sdw->link_res->clock_stop_quirks;
487
488 if (clock_stop_quirks & SDW_INTEL_CLK_STOP_TEARDOWN) {
489 ret = sdw_intel_stop_bus(sdw, false);
490 if (ret < 0) {
491 dev_err(dev, "%s: cannot stop bus during teardown: %d\n",
492 __func__, ret);
493 return ret;
494 }
495 } else if (clock_stop_quirks & SDW_INTEL_CLK_STOP_BUS_RESET || !clock_stop_quirks) {
496 ret = sdw_intel_stop_bus(sdw, true);
497 if (ret < 0) {
498 dev_err(dev, "%s: cannot stop bus during clock_stop: %d\n",
499 __func__, ret);
500 return ret;
501 }
502 } else {
503 dev_err(dev, "%s clock_stop_quirks %x unsupported\n",
504 __func__, clock_stop_quirks);
505 ret = -EINVAL;
506 }
507
508 return ret;
509}
510
511static int __maybe_unused intel_resume(struct device *dev)
512{
513 struct sdw_cdns *cdns = dev_get_drvdata(dev);
514 struct sdw_intel *sdw = cdns_to_intel(cdns);
515 struct sdw_bus *bus = &cdns->bus;
516 int link_flags;
517 int ret;
518
519 if (bus->prop.hw_disabled || !sdw->startup_done) {
520 dev_dbg(dev, "SoundWire master %d is disabled or not-started, ignoring\n",
521 bus->link_id);
522 return 0;
523 }
524
525 link_flags = md_flags >> (bus->link_id * 8);
526
527 if (pm_runtime_suspended(dev)) {
528 dev_dbg(dev, "pm_runtime status was suspended, forcing active\n");
529
530 /* follow required sequence from runtime_pm.rst */
531 pm_runtime_disable(dev);
532 pm_runtime_set_active(dev);
533 pm_runtime_mark_last_busy(dev);
534 pm_runtime_enable(dev);
535
536 link_flags = md_flags >> (bus->link_id * 8);
537
538 if (!(link_flags & SDW_INTEL_MASTER_DISABLE_PM_RUNTIME_IDLE))
539 pm_runtime_idle(dev);
540 }
541
542 ret = sdw_intel_link_power_up(sdw);
543 if (ret) {
544 dev_err(dev, "%s failed: %d\n", __func__, ret);
545 return ret;
546 }
547
548 /*
549 * make sure all Slaves are tagged as UNATTACHED and provide
550 * reason for reinitialization
551 */
552 sdw_clear_slave_status(bus, SDW_UNATTACH_REQUEST_MASTER_RESET);
553
554 ret = sdw_intel_start_bus(sdw);
555 if (ret < 0) {
556 dev_err(dev, "cannot start bus during resume\n");
557 sdw_intel_link_power_down(sdw);
558 return ret;
559 }
560
561 /*
562 * after system resume, the pm_runtime suspend() may kick in
563 * during the enumeration, before any children device force the
564 * master device to remain active. Using pm_runtime_get()
565 * routines is not really possible, since it'd prevent the
566 * master from suspending.
567 * A reasonable compromise is to update the pm_runtime
568 * counters and delay the pm_runtime suspend by several
569 * seconds, by when all enumeration should be complete.
570 */
571 pm_runtime_mark_last_busy(dev);
572
573 return 0;
574}
575
576static int __maybe_unused intel_resume_runtime(struct device *dev)
577{
578 struct sdw_cdns *cdns = dev_get_drvdata(dev);
579 struct sdw_intel *sdw = cdns_to_intel(cdns);
580 struct sdw_bus *bus = &cdns->bus;
581 u32 clock_stop_quirks;
582 int ret;
583
584 if (bus->prop.hw_disabled || !sdw->startup_done) {
585 dev_dbg(dev, "SoundWire master %d is disabled or not-started, ignoring\n",
586 bus->link_id);
587 return 0;
588 }
589
590 /* unconditionally disable WAKEEN interrupt */
591 sdw_intel_shim_wake(sdw, false);
592
593 clock_stop_quirks = sdw->link_res->clock_stop_quirks;
594
595 if (clock_stop_quirks & SDW_INTEL_CLK_STOP_TEARDOWN) {
596 ret = sdw_intel_link_power_up(sdw);
597 if (ret) {
598 dev_err(dev, "%s: power_up failed after teardown: %d\n", __func__, ret);
599 return ret;
600 }
601
602 /*
603 * make sure all Slaves are tagged as UNATTACHED and provide
604 * reason for reinitialization
605 */
606 sdw_clear_slave_status(bus, SDW_UNATTACH_REQUEST_MASTER_RESET);
607
608 ret = sdw_intel_start_bus(sdw);
609 if (ret < 0) {
610 dev_err(dev, "%s: cannot start bus after teardown: %d\n", __func__, ret);
611 sdw_intel_link_power_down(sdw);
612 return ret;
613 }
614
615 } else if (clock_stop_quirks & SDW_INTEL_CLK_STOP_BUS_RESET) {
616 ret = sdw_intel_link_power_up(sdw);
617 if (ret) {
618 dev_err(dev, "%s: power_up failed after bus reset: %d\n", __func__, ret);
619 return ret;
620 }
621
622 ret = sdw_intel_start_bus_after_reset(sdw);
623 if (ret < 0) {
624 dev_err(dev, "%s: cannot start bus after reset: %d\n", __func__, ret);
625 sdw_intel_link_power_down(sdw);
626 return ret;
627 }
628 } else if (!clock_stop_quirks) {
629
630 sdw_intel_check_clock_stop(sdw);
631
632 ret = sdw_intel_link_power_up(sdw);
633 if (ret) {
634 dev_err(dev, "%s: power_up failed: %d\n", __func__, ret);
635 return ret;
636 }
637
638 ret = sdw_intel_start_bus_after_clock_stop(sdw);
639 if (ret < 0) {
640 dev_err(dev, "%s: cannot start bus after clock stop: %d\n", __func__, ret);
641 sdw_intel_link_power_down(sdw);
642 return ret;
643 }
644 } else {
645 dev_err(dev, "%s: clock_stop_quirks %x unsupported\n",
646 __func__, clock_stop_quirks);
647 ret = -EINVAL;
648 }
649
650 return ret;
651}
652
653static const struct dev_pm_ops intel_pm = {
654 .prepare = intel_pm_prepare,
655 SET_SYSTEM_SLEEP_PM_OPS(intel_suspend, intel_resume)
656 SET_RUNTIME_PM_OPS(intel_suspend_runtime, intel_resume_runtime, NULL)
657};
658
659static const struct auxiliary_device_id intel_link_id_table[] = {
660 { .name = "soundwire_intel.link" },
661 {},
662};
663MODULE_DEVICE_TABLE(auxiliary, intel_link_id_table);
664
665static struct auxiliary_driver sdw_intel_drv = {
666 .probe = intel_link_probe,
667 .remove = intel_link_remove,
668 .driver = {
669 /* auxiliary_driver_register() sets .name to be the modname */
670 .pm = &intel_pm,
671 },
672 .id_table = intel_link_id_table
673};
674module_auxiliary_driver(sdw_intel_drv);
675
676MODULE_LICENSE("Dual BSD/GPL");
677MODULE_DESCRIPTION("Intel Soundwire Link Driver");