forked from torvalds/linux
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsoc-core.c
3500 lines (2946 loc) · 85.8 KB
/
soc-core.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// SPDX-License-Identifier: GPL-2.0+
//
// soc-core.c -- ALSA SoC Audio Layer
//
// Copyright 2005 Wolfson Microelectronics PLC.
// Copyright 2005 Openedhand Ltd.
// Copyright (C) 2010 Slimlogic Ltd.
// Copyright (C) 2010 Texas Instruments Inc.
//
// Author: Liam Girdwood <[email protected]>
// with code, comments and ideas from :-
// Richard Purdie <[email protected]>
//
// TODO:
// o Add hw rules to enforce rates, etc.
// o More testing with other codecs/machines.
// o Add more codecs and platforms to ensure good API coverage.
// o Support TDM on PCM and I2S
#include <linux/module.h>
#include <linux/moduleparam.h>
#include <linux/init.h>
#include <linux/delay.h>
#include <linux/pm.h>
#include <linux/bitops.h>
#include <linux/debugfs.h>
#include <linux/platform_device.h>
#include <linux/pinctrl/consumer.h>
#include <linux/ctype.h>
#include <linux/slab.h>
#include <linux/of.h>
#include <linux/of_graph.h>
#include <linux/dmi.h>
#include <linux/acpi.h>
#include <sound/core.h>
#include <sound/pcm.h>
#include <sound/pcm_params.h>
#include <sound/soc.h>
#include <sound/soc-dpcm.h>
#include <sound/soc-topology.h>
#include <sound/soc-link.h>
#include <sound/initval.h>
#define CREATE_TRACE_POINTS
#include <trace/events/asoc.h>
static DEFINE_MUTEX(client_mutex);
static LIST_HEAD(component_list);
static LIST_HEAD(unbind_card_list);
#define for_each_component(component) \
list_for_each_entry(component, &component_list, list)
/*
* This is used if driver don't need to have CPU/Codec/Platform
* dai_link. see soc.h
*/
struct snd_soc_dai_link_component null_dailink_component[0];
EXPORT_SYMBOL_GPL(null_dailink_component);
/*
* This is a timeout to do a DAPM powerdown after a stream is closed().
* It can be used to eliminate pops between different playback streams, e.g.
* between two audio tracks.
*/
static int pmdown_time = 5000;
module_param(pmdown_time, int, 0);
MODULE_PARM_DESC(pmdown_time, "DAPM stream powerdown time (msecs)");
static ssize_t pmdown_time_show(struct device *dev,
struct device_attribute *attr, char *buf)
{
struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
return sprintf(buf, "%ld\n", rtd->pmdown_time);
}
static ssize_t pmdown_time_store(struct device *dev,
struct device_attribute *attr,
const char *buf, size_t count)
{
struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
int ret;
ret = kstrtol(buf, 10, &rtd->pmdown_time);
if (ret)
return ret;
return count;
}
static DEVICE_ATTR_RW(pmdown_time);
static struct attribute *soc_dev_attrs[] = {
&dev_attr_pmdown_time.attr,
NULL
};
static umode_t soc_dev_attr_is_visible(struct kobject *kobj,
struct attribute *attr, int idx)
{
struct device *dev = kobj_to_dev(kobj);
struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
if (!rtd)
return 0;
if (attr == &dev_attr_pmdown_time.attr)
return attr->mode; /* always visible */
return rtd->num_codecs ? attr->mode : 0; /* enabled only with codec */
}
static const struct attribute_group soc_dapm_dev_group = {
.attrs = soc_dapm_dev_attrs,
.is_visible = soc_dev_attr_is_visible,
};
static const struct attribute_group soc_dev_group = {
.attrs = soc_dev_attrs,
.is_visible = soc_dev_attr_is_visible,
};
static const struct attribute_group *soc_dev_attr_groups[] = {
&soc_dapm_dev_group,
&soc_dev_group,
NULL
};
#ifdef CONFIG_DEBUG_FS
struct dentry *snd_soc_debugfs_root;
EXPORT_SYMBOL_GPL(snd_soc_debugfs_root);
static void soc_init_component_debugfs(struct snd_soc_component *component)
{
if (!component->card->debugfs_card_root)
return;
if (component->debugfs_prefix) {
char *name;
name = kasprintf(GFP_KERNEL, "%s:%s",
component->debugfs_prefix, component->name);
if (name) {
component->debugfs_root = debugfs_create_dir(name,
component->card->debugfs_card_root);
kfree(name);
}
} else {
component->debugfs_root = debugfs_create_dir(component->name,
component->card->debugfs_card_root);
}
snd_soc_dapm_debugfs_init(snd_soc_component_get_dapm(component),
component->debugfs_root);
}
static void soc_cleanup_component_debugfs(struct snd_soc_component *component)
{
if (!component->debugfs_root)
return;
debugfs_remove_recursive(component->debugfs_root);
component->debugfs_root = NULL;
}
static int dai_list_show(struct seq_file *m, void *v)
{
struct snd_soc_component *component;
struct snd_soc_dai *dai;
mutex_lock(&client_mutex);
for_each_component(component)
for_each_component_dais(component, dai)
seq_printf(m, "%s\n", dai->name);
mutex_unlock(&client_mutex);
return 0;
}
DEFINE_SHOW_ATTRIBUTE(dai_list);
static int component_list_show(struct seq_file *m, void *v)
{
struct snd_soc_component *component;
mutex_lock(&client_mutex);
for_each_component(component)
seq_printf(m, "%s\n", component->name);
mutex_unlock(&client_mutex);
return 0;
}
DEFINE_SHOW_ATTRIBUTE(component_list);
static void soc_init_card_debugfs(struct snd_soc_card *card)
{
card->debugfs_card_root = debugfs_create_dir(card->name,
snd_soc_debugfs_root);
debugfs_create_u32("dapm_pop_time", 0644, card->debugfs_card_root,
&card->pop_time);
snd_soc_dapm_debugfs_init(&card->dapm, card->debugfs_card_root);
}
static void soc_cleanup_card_debugfs(struct snd_soc_card *card)
{
debugfs_remove_recursive(card->debugfs_card_root);
card->debugfs_card_root = NULL;
}
static void snd_soc_debugfs_init(void)
{
snd_soc_debugfs_root = debugfs_create_dir("asoc", NULL);
debugfs_create_file("dais", 0444, snd_soc_debugfs_root, NULL,
&dai_list_fops);
debugfs_create_file("components", 0444, snd_soc_debugfs_root, NULL,
&component_list_fops);
}
static void snd_soc_debugfs_exit(void)
{
debugfs_remove_recursive(snd_soc_debugfs_root);
}
#else
static inline void soc_init_component_debugfs(struct snd_soc_component *component) { }
static inline void soc_cleanup_component_debugfs(struct snd_soc_component *component) { }
static inline void soc_init_card_debugfs(struct snd_soc_card *card) { }
static inline void soc_cleanup_card_debugfs(struct snd_soc_card *card) { }
static inline void snd_soc_debugfs_init(void) { }
static inline void snd_soc_debugfs_exit(void) { }
#endif
static int snd_soc_rtd_add_component(struct snd_soc_pcm_runtime *rtd,
struct snd_soc_component *component)
{
struct snd_soc_component *comp;
int i;
for_each_rtd_components(rtd, i, comp) {
/* already connected */
if (comp == component)
return 0;
}
/* see for_each_rtd_components */
rtd->components[rtd->num_components] = component;
rtd->num_components++;
return 0;
}
struct snd_soc_component *snd_soc_rtdcom_lookup(struct snd_soc_pcm_runtime *rtd,
const char *driver_name)
{
struct snd_soc_component *component;
int i;
if (!driver_name)
return NULL;
/*
* NOTE
*
* snd_soc_rtdcom_lookup() will find component from rtd by using
* specified driver name.
* But, if many components which have same driver name are connected
* to 1 rtd, this function will return 1st found component.
*/
for_each_rtd_components(rtd, i, component) {
const char *component_name = component->driver->name;
if (!component_name)
continue;
if ((component_name == driver_name) ||
strcmp(component_name, driver_name) == 0)
return component;
}
return NULL;
}
EXPORT_SYMBOL_GPL(snd_soc_rtdcom_lookup);
struct snd_soc_component
*snd_soc_lookup_component_nolocked(struct device *dev, const char *driver_name)
{
struct snd_soc_component *component;
struct snd_soc_component *found_component;
found_component = NULL;
for_each_component(component) {
if ((dev == component->dev) &&
(!driver_name ||
(driver_name == component->driver->name) ||
(strcmp(component->driver->name, driver_name) == 0))) {
found_component = component;
break;
}
}
return found_component;
}
EXPORT_SYMBOL_GPL(snd_soc_lookup_component_nolocked);
struct snd_soc_component *snd_soc_lookup_component(struct device *dev,
const char *driver_name)
{
struct snd_soc_component *component;
mutex_lock(&client_mutex);
component = snd_soc_lookup_component_nolocked(dev, driver_name);
mutex_unlock(&client_mutex);
return component;
}
EXPORT_SYMBOL_GPL(snd_soc_lookup_component);
struct snd_soc_pcm_runtime
*snd_soc_get_pcm_runtime(struct snd_soc_card *card,
struct snd_soc_dai_link *dai_link)
{
struct snd_soc_pcm_runtime *rtd;
for_each_card_rtds(card, rtd) {
if (rtd->dai_link == dai_link)
return rtd;
}
dev_dbg(card->dev, "ASoC: failed to find rtd %s\n", dai_link->name);
return NULL;
}
EXPORT_SYMBOL_GPL(snd_soc_get_pcm_runtime);
/*
* Power down the audio subsystem pmdown_time msecs after close is called.
* This is to ensure there are no pops or clicks in between any music tracks
* due to DAPM power cycling.
*/
void snd_soc_close_delayed_work(struct snd_soc_pcm_runtime *rtd)
{
struct snd_soc_dai *codec_dai = asoc_rtd_to_codec(rtd, 0);
int playback = SNDRV_PCM_STREAM_PLAYBACK;
mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass);
dev_dbg(rtd->dev,
"ASoC: pop wq checking: %s status: %s waiting: %s\n",
codec_dai->driver->playback.stream_name,
snd_soc_dai_stream_active(codec_dai, playback) ?
"active" : "inactive",
rtd->pop_wait ? "yes" : "no");
/* are we waiting on this codec DAI stream */
if (rtd->pop_wait == 1) {
rtd->pop_wait = 0;
snd_soc_dapm_stream_event(rtd, playback,
SND_SOC_DAPM_STREAM_STOP);
}
mutex_unlock(&rtd->card->pcm_mutex);
}
EXPORT_SYMBOL_GPL(snd_soc_close_delayed_work);
static void soc_release_rtd_dev(struct device *dev)
{
/* "dev" means "rtd->dev" */
kfree(dev);
}
static void soc_free_pcm_runtime(struct snd_soc_pcm_runtime *rtd)
{
if (!rtd)
return;
list_del(&rtd->list);
if (delayed_work_pending(&rtd->delayed_work))
flush_delayed_work(&rtd->delayed_work);
snd_soc_pcm_component_free(rtd);
/*
* we don't need to call kfree() for rtd->dev
* see
* soc_release_rtd_dev()
*
* We don't need rtd->dev NULL check, because
* it is alloced *before* rtd.
* see
* soc_new_pcm_runtime()
*
* We don't need to mind freeing for rtd,
* because it was created from dev (= rtd->dev)
* see
* soc_new_pcm_runtime()
*
* rtd = devm_kzalloc(dev, ...);
* rtd->dev = dev
*/
device_unregister(rtd->dev);
}
static void close_delayed_work(struct work_struct *work) {
struct snd_soc_pcm_runtime *rtd =
container_of(work, struct snd_soc_pcm_runtime,
delayed_work.work);
if (rtd->close_delayed_work_func)
rtd->close_delayed_work_func(rtd);
}
static struct snd_soc_pcm_runtime *soc_new_pcm_runtime(
struct snd_soc_card *card, struct snd_soc_dai_link *dai_link)
{
struct snd_soc_pcm_runtime *rtd;
struct snd_soc_component *component;
struct device *dev;
int ret;
int stream;
/*
* for rtd->dev
*/
dev = kzalloc(sizeof(struct device), GFP_KERNEL);
if (!dev)
return NULL;
dev->parent = card->dev;
dev->release = soc_release_rtd_dev;
dev_set_name(dev, "%s", dai_link->name);
ret = device_register(dev);
if (ret < 0) {
put_device(dev); /* soc_release_rtd_dev */
return NULL;
}
/*
* for rtd
*/
rtd = devm_kzalloc(dev,
sizeof(*rtd) +
sizeof(*component) * (dai_link->num_cpus +
dai_link->num_codecs +
dai_link->num_platforms),
GFP_KERNEL);
if (!rtd) {
device_unregister(dev);
return NULL;
}
rtd->dev = dev;
INIT_LIST_HEAD(&rtd->list);
for_each_pcm_streams(stream) {
INIT_LIST_HEAD(&rtd->dpcm[stream].be_clients);
INIT_LIST_HEAD(&rtd->dpcm[stream].fe_clients);
}
dev_set_drvdata(dev, rtd);
INIT_DELAYED_WORK(&rtd->delayed_work, close_delayed_work);
/*
* for rtd->dais
*/
rtd->dais = devm_kcalloc(dev, dai_link->num_cpus + dai_link->num_codecs,
sizeof(struct snd_soc_dai *),
GFP_KERNEL);
if (!rtd->dais)
goto free_rtd;
/*
* dais = [][][][][][][][][][][][][][][][][][]
* ^cpu_dais ^codec_dais
* |--- num_cpus ---|--- num_codecs --|
* see
* asoc_rtd_to_cpu()
* asoc_rtd_to_codec()
*/
rtd->num_cpus = dai_link->num_cpus;
rtd->num_codecs = dai_link->num_codecs;
rtd->card = card;
rtd->dai_link = dai_link;
rtd->num = card->num_rtd++;
/* see for_each_card_rtds */
list_add_tail(&rtd->list, &card->rtd_list);
ret = device_add_groups(dev, soc_dev_attr_groups);
if (ret < 0)
goto free_rtd;
return rtd;
free_rtd:
soc_free_pcm_runtime(rtd);
return NULL;
}
static void snd_soc_flush_all_delayed_work(struct snd_soc_card *card)
{
struct snd_soc_pcm_runtime *rtd;
for_each_card_rtds(card, rtd)
flush_delayed_work(&rtd->delayed_work);
}
#ifdef CONFIG_PM_SLEEP
static void soc_playback_digital_mute(struct snd_soc_card *card, int mute)
{
struct snd_soc_pcm_runtime *rtd;
struct snd_soc_dai *dai;
int playback = SNDRV_PCM_STREAM_PLAYBACK;
int i;
for_each_card_rtds(card, rtd) {
if (rtd->dai_link->ignore_suspend)
continue;
for_each_rtd_dais(rtd, i, dai) {
if (snd_soc_dai_stream_active(dai, playback))
snd_soc_dai_digital_mute(dai, mute, playback);
}
}
}
static void soc_dapm_suspend_resume(struct snd_soc_card *card, int event)
{
struct snd_soc_pcm_runtime *rtd;
int stream;
for_each_card_rtds(card, rtd) {
if (rtd->dai_link->ignore_suspend)
continue;
for_each_pcm_streams(stream)
snd_soc_dapm_stream_event(rtd, stream, event);
}
}
/* powers down audio subsystem for suspend */
int snd_soc_suspend(struct device *dev)
{
struct snd_soc_card *card = dev_get_drvdata(dev);
struct snd_soc_component *component;
struct snd_soc_pcm_runtime *rtd;
int i;
/* If the card is not initialized yet there is nothing to do */
if (!card->instantiated)
return 0;
/*
* Due to the resume being scheduled into a workqueue we could
* suspend before that's finished - wait for it to complete.
*/
snd_power_wait(card->snd_card);
/* we're going to block userspace touching us until resume completes */
snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D3hot);
/* mute any active DACs */
soc_playback_digital_mute(card, 1);
/* suspend all pcms */
for_each_card_rtds(card, rtd) {
if (rtd->dai_link->ignore_suspend)
continue;
snd_pcm_suspend_all(rtd->pcm);
}
snd_soc_card_suspend_pre(card);
/* close any waiting streams */
snd_soc_flush_all_delayed_work(card);
soc_dapm_suspend_resume(card, SND_SOC_DAPM_STREAM_SUSPEND);
/* Recheck all endpoints too, their state is affected by suspend */
dapm_mark_endpoints_dirty(card);
snd_soc_dapm_sync(&card->dapm);
/* suspend all COMPONENTs */
for_each_card_rtds(card, rtd) {
if (rtd->dai_link->ignore_suspend)
continue;
for_each_rtd_components(rtd, i, component) {
struct snd_soc_dapm_context *dapm =
snd_soc_component_get_dapm(component);
/*
* ignore if component was already suspended
*/
if (snd_soc_component_is_suspended(component))
continue;
/*
* If there are paths active then the COMPONENT will be
* held with bias _ON and should not be suspended.
*/
switch (snd_soc_dapm_get_bias_level(dapm)) {
case SND_SOC_BIAS_STANDBY:
/*
* If the COMPONENT is capable of idle
* bias off then being in STANDBY
* means it's doing something,
* otherwise fall through.
*/
if (dapm->idle_bias_off) {
dev_dbg(component->dev,
"ASoC: idle_bias_off CODEC on over suspend\n");
break;
}
fallthrough;
case SND_SOC_BIAS_OFF:
snd_soc_component_suspend(component);
if (component->regmap)
regcache_mark_dirty(component->regmap);
/* deactivate pins to sleep state */
pinctrl_pm_select_sleep_state(component->dev);
break;
default:
dev_dbg(component->dev,
"ASoC: COMPONENT is on over suspend\n");
break;
}
}
}
snd_soc_card_suspend_post(card);
return 0;
}
EXPORT_SYMBOL_GPL(snd_soc_suspend);
/*
* deferred resume work, so resume can complete before we finished
* setting our codec back up, which can be very slow on I2C
*/
static void soc_resume_deferred(struct work_struct *work)
{
struct snd_soc_card *card =
container_of(work, struct snd_soc_card,
deferred_resume_work);
struct snd_soc_component *component;
/*
* our power state is still SNDRV_CTL_POWER_D3hot from suspend time,
* so userspace apps are blocked from touching us
*/
dev_dbg(card->dev, "ASoC: starting resume work\n");
/* Bring us up into D2 so that DAPM starts enabling things */
snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D2);
snd_soc_card_resume_pre(card);
for_each_card_components(card, component) {
if (snd_soc_component_is_suspended(component))
snd_soc_component_resume(component);
}
soc_dapm_suspend_resume(card, SND_SOC_DAPM_STREAM_RESUME);
/* unmute any active DACs */
soc_playback_digital_mute(card, 0);
snd_soc_card_resume_post(card);
dev_dbg(card->dev, "ASoC: resume work completed\n");
/* Recheck all endpoints too, their state is affected by suspend */
dapm_mark_endpoints_dirty(card);
snd_soc_dapm_sync(&card->dapm);
/* userspace can access us now we are back as we were before */
snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D0);
}
/* powers up audio subsystem after a suspend */
int snd_soc_resume(struct device *dev)
{
struct snd_soc_card *card = dev_get_drvdata(dev);
struct snd_soc_component *component;
/* If the card is not initialized yet there is nothing to do */
if (!card->instantiated)
return 0;
/* activate pins from sleep state */
for_each_card_components(card, component)
if (snd_soc_component_active(component))
pinctrl_pm_select_default_state(component->dev);
dev_dbg(dev, "ASoC: Scheduling resume work\n");
if (!schedule_work(&card->deferred_resume_work))
dev_err(dev, "ASoC: resume work item may be lost\n");
return 0;
}
EXPORT_SYMBOL_GPL(snd_soc_resume);
static void soc_resume_init(struct snd_soc_card *card)
{
/* deferred resume work */
INIT_WORK(&card->deferred_resume_work, soc_resume_deferred);
}
#else
#define snd_soc_suspend NULL
#define snd_soc_resume NULL
static inline void soc_resume_init(struct snd_soc_card *card) { }
#endif
static struct device_node
*soc_component_to_node(struct snd_soc_component *component)
{
struct device_node *of_node;
of_node = component->dev->of_node;
if (!of_node && component->dev->parent)
of_node = component->dev->parent->of_node;
return of_node;
}
static int snd_soc_is_matching_component(
const struct snd_soc_dai_link_component *dlc,
struct snd_soc_component *component)
{
struct device_node *component_of_node;
if (!dlc)
return 0;
component_of_node = soc_component_to_node(component);
if (dlc->of_node && component_of_node != dlc->of_node)
return 0;
if (dlc->name && strcmp(component->name, dlc->name))
return 0;
return 1;
}
static struct snd_soc_component *soc_find_component(
const struct snd_soc_dai_link_component *dlc)
{
struct snd_soc_component *component;
lockdep_assert_held(&client_mutex);
/*
* NOTE
*
* It returns *1st* found component, but some driver
* has few components by same of_node/name
* ex)
* CPU component and generic DMAEngine component
*/
for_each_component(component)
if (snd_soc_is_matching_component(dlc, component))
return component;
return NULL;
}
/**
* snd_soc_find_dai - Find a registered DAI
*
* @dlc: name of the DAI or the DAI driver and optional component info to match
*
* This function will search all registered components and their DAIs to
* find the DAI of the same name. The component's of_node and name
* should also match if being specified.
*
* Return: pointer of DAI, or NULL if not found.
*/
struct snd_soc_dai *snd_soc_find_dai(
const struct snd_soc_dai_link_component *dlc)
{
struct snd_soc_component *component;
struct snd_soc_dai *dai;
lockdep_assert_held(&client_mutex);
/* Find CPU DAI from registered DAIs */
for_each_component(component) {
if (!snd_soc_is_matching_component(dlc, component))
continue;
for_each_component_dais(component, dai) {
if (dlc->dai_name && strcmp(dai->name, dlc->dai_name)
&& (!dai->driver->name
|| strcmp(dai->driver->name, dlc->dai_name)))
continue;
return dai;
}
}
return NULL;
}
EXPORT_SYMBOL_GPL(snd_soc_find_dai);
struct snd_soc_dai *snd_soc_find_dai_with_mutex(
const struct snd_soc_dai_link_component *dlc)
{
struct snd_soc_dai *dai;
mutex_lock(&client_mutex);
dai = snd_soc_find_dai(dlc);
mutex_unlock(&client_mutex);
return dai;
}
EXPORT_SYMBOL_GPL(snd_soc_find_dai_with_mutex);
static int soc_dai_link_sanity_check(struct snd_soc_card *card,
struct snd_soc_dai_link *link)
{
int i;
struct snd_soc_dai_link_component *cpu, *codec, *platform;
for_each_link_codecs(link, i, codec) {
/*
* Codec must be specified by 1 of name or OF node,
* not both or neither.
*/
if (!!codec->name == !!codec->of_node) {
dev_err(card->dev, "ASoC: Neither/both codec name/of_node are set for %s\n",
link->name);
return -EINVAL;
}
/* Codec DAI name must be specified */
if (!codec->dai_name) {
dev_err(card->dev, "ASoC: codec_dai_name not set for %s\n",
link->name);
return -EINVAL;
}
/*
* Defer card registration if codec component is not added to
* component list.
*/
if (!soc_find_component(codec)) {
dev_dbg(card->dev,
"ASoC: codec component %s not found for link %s\n",
codec->name, link->name);
return -EPROBE_DEFER;
}
}
for_each_link_platforms(link, i, platform) {
/*
* Platform may be specified by either name or OF node, but it
* can be left unspecified, then no components will be inserted
* in the rtdcom list
*/
if (!!platform->name == !!platform->of_node) {
dev_err(card->dev,
"ASoC: Neither/both platform name/of_node are set for %s\n",
link->name);
return -EINVAL;
}
/*
* Defer card registration if platform component is not added to
* component list.
*/
if (!soc_find_component(platform)) {
dev_dbg(card->dev,
"ASoC: platform component %s not found for link %s\n",
platform->name, link->name);
return -EPROBE_DEFER;
}
}
for_each_link_cpus(link, i, cpu) {
/*
* CPU device may be specified by either name or OF node, but
* can be left unspecified, and will be matched based on DAI
* name alone..
*/
if (cpu->name && cpu->of_node) {
dev_err(card->dev,
"ASoC: Neither/both cpu name/of_node are set for %s\n",
link->name);
return -EINVAL;
}
/*
* Defer card registration if cpu dai component is not added to
* component list.
*/
if ((cpu->of_node || cpu->name) &&
!soc_find_component(cpu)) {
dev_dbg(card->dev,
"ASoC: cpu component %s not found for link %s\n",
cpu->name, link->name);
return -EPROBE_DEFER;
}
/*
* At least one of CPU DAI name or CPU device name/node must be
* specified
*/
if (!cpu->dai_name &&
!(cpu->name || cpu->of_node)) {
dev_err(card->dev,
"ASoC: Neither cpu_dai_name nor cpu_name/of_node are set for %s\n",
link->name);
return -EINVAL;
}
}
return 0;
}
/**
* snd_soc_remove_pcm_runtime - Remove a pcm_runtime from card
* @card: The ASoC card to which the pcm_runtime has
* @rtd: The pcm_runtime to remove
*
* This function removes a pcm_runtime from the ASoC card.
*/
void snd_soc_remove_pcm_runtime(struct snd_soc_card *card,
struct snd_soc_pcm_runtime *rtd)
{
lockdep_assert_held(&client_mutex);
/* release machine specific resources */
snd_soc_link_exit(rtd);
/*
* Notify the machine driver for extra destruction
*/
snd_soc_card_remove_dai_link(card, rtd->dai_link);
soc_free_pcm_runtime(rtd);
}
EXPORT_SYMBOL_GPL(snd_soc_remove_pcm_runtime);
/**
* snd_soc_add_pcm_runtime - Add a pcm_runtime dynamically via dai_link
* @card: The ASoC card to which the pcm_runtime is added
* @dai_link: The DAI link to find pcm_runtime
*
* This function adds a pcm_runtime ASoC card by using dai_link.
*
* Note: Topology can use this API to add pcm_runtime when probing the
* topology component. And machine drivers can still define static
* DAI links in dai_link array.
*/
int snd_soc_add_pcm_runtime(struct snd_soc_card *card,
struct snd_soc_dai_link *dai_link)
{
struct snd_soc_pcm_runtime *rtd;
struct snd_soc_dai_link_component *codec, *platform, *cpu;
struct snd_soc_component *component;
int i, ret;
lockdep_assert_held(&client_mutex);
/*
* Notify the machine driver for extra initialization
*/
ret = snd_soc_card_add_dai_link(card, dai_link);
if (ret < 0)
return ret;
if (dai_link->ignore)
return 0;
dev_dbg(card->dev, "ASoC: binding %s\n", dai_link->name);
ret = soc_dai_link_sanity_check(card, dai_link);
if (ret < 0)
return ret;
rtd = soc_new_pcm_runtime(card, dai_link);
if (!rtd)
return -ENOMEM;
for_each_link_cpus(dai_link, i, cpu) {
asoc_rtd_to_cpu(rtd, i) = snd_soc_find_dai(cpu);
if (!asoc_rtd_to_cpu(rtd, i)) {
dev_info(card->dev, "ASoC: CPU DAI %s not registered\n",
cpu->dai_name);