forked from torvalds/linux
-
Notifications
You must be signed in to change notification settings - Fork 90
/
macaudio.c
1683 lines (1397 loc) · 46.5 KB
/
macaudio.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-only
/*
* ASoC machine driver for Apple Silicon Macs
*
* Copyright (C) The Asahi Linux Contributors
*
* Based on sound/soc/qcom/{sc7180.c|common.c}
* Copyright (c) 2018, Linaro Limited.
* Copyright (c) 2020, The Linux Foundation. All rights reserved.
*
*
* The platform driver has independent frontend and backend DAIs with the
* option of routing backends to any of the frontends. The platform
* driver configures the routing based on DPCM couplings in ASoC runtime
* structures, which in turn are determined from DAPM paths by ASoC. But the
* platform driver doesn't supply relevant DAPM paths and leaves that up for
* the machine driver to fill in. The filled-in virtual topology can be
* anything as long as any backend isn't connected to more than one frontend
* at any given time. (The limitation is due to the unsupported case of
* reparenting of live BEs.)
*/
/* #define DEBUG */
#include <linux/module.h>
#include <linux/of_device.h>
#include <linux/platform_device.h>
#include <sound/core.h>
#include <sound/jack.h>
#include <sound/pcm.h>
#include <sound/simple_card_utils.h>
#include <sound/soc.h>
#include <sound/soc-jack.h>
#include <uapi/linux/input-event-codes.h>
#define DRIVER_NAME "snd-soc-macaudio"
/*
* CPU side is bit and frame clock provider
* I2S has both clocks inverted
*/
#define MACAUDIO_DAI_FMT (SND_SOC_DAIFMT_I2S | \
SND_SOC_DAIFMT_CBC_CFC | \
SND_SOC_DAIFMT_GATED | \
SND_SOC_DAIFMT_IB_IF)
#define MACAUDIO_JACK_MASK (SND_JACK_HEADSET | SND_JACK_HEADPHONE)
#define MACAUDIO_SLOTWIDTH 32
/*
* Maximum BCLK frequency
*
* Codec maximums:
* CS42L42 26.0 MHz
* TAS2770 27.1 MHz
* TAS2764 24.576 MHz
*/
#define MACAUDIO_MAX_BCLK_FREQ 24576000
#define SPEAKER_MAGIC_VALUE (s32)0xdec1be15
/* milliseconds */
#define SPEAKER_LOCK_TIMEOUT 250
enum macaudio_amp_type {
AMP_NONE,
AMP_TAS5770,
AMP_SN012776,
AMP_SSM3515,
};
enum macaudio_spkr_config {
SPKR_NONE, /* No speakers */
SPKR_1W, /* 1 woofer / ch */
SPKR_2W, /* 2 woofers / ch */
SPKR_1W1T, /* 1 woofer + 1 tweeter / ch */
SPKR_2W1T, /* 2 woofers + 1 tweeter / ch */
};
struct macaudio_platform_cfg {
bool enable_speakers;
enum macaudio_amp_type amp;
enum macaudio_spkr_config speakers;
bool stereo;
int amp_gain;
int safe_vol;
};
static const char *volume_control_names[] = {
[AMP_TAS5770] = "* Speaker Playback Volume",
[AMP_SN012776] = "* Speaker Volume",
[AMP_SSM3515] = "* DAC Playback Volume",
};
#define SN012776_0DB 201
#define SN012776_DB(x) (SN012776_0DB + 2 * (x))
/* Same as SN012776 */
#define TAS5770_0DB SN012776_0DB
#define TAS5770_DB(x) SN012776_DB(x)
#define SSM3515_0DB (255 - 64) /* +24dB max, steps of 3/8 dB */
#define SSM3515_DB(x) (SSM3515_0DB + (8 * (x) / 3))
struct macaudio_snd_data {
struct snd_soc_card card;
struct snd_soc_jack jack;
int jack_plugin_state;
const struct macaudio_platform_cfg *cfg;
bool has_speakers;
bool has_sense;
bool has_safety;
unsigned int max_channels;
struct macaudio_link_props {
/* frontend props */
unsigned int bclk_ratio;
bool is_sense;
/* backend props */
bool is_speakers;
bool is_headphones;
unsigned int tdm_mask;
} *link_props;
int speaker_sample_rate;
struct snd_kcontrol *speaker_sample_rate_kctl;
struct mutex volume_lock_mutex;
bool speaker_volume_unlocked;
bool speaker_volume_was_locked;
struct snd_kcontrol *speaker_lock_kctl;
struct snd_ctl_file *speaker_lock_owner;
u64 bes_active;
bool speaker_lock_timeout_enabled;
ktime_t speaker_lock_timeout;
ktime_t speaker_lock_remain;
struct delayed_work lock_timeout_work;
struct work_struct lock_update_work;
};
static int please_blow_up_my_speakers;
module_param(please_blow_up_my_speakers, int, 0644);
MODULE_PARM_DESC(please_blow_up_my_speakers, "Allow unsafe or untested operating configurations");
SND_SOC_DAILINK_DEFS(primary,
DAILINK_COMP_ARRAY(COMP_CPU("mca-pcm-0")), // CPU
DAILINK_COMP_ARRAY(COMP_DUMMY()), // CODEC
DAILINK_COMP_ARRAY(COMP_EMPTY())); // platform (filled at runtime)
SND_SOC_DAILINK_DEFS(secondary,
DAILINK_COMP_ARRAY(COMP_CPU("mca-pcm-1")), // CPU
DAILINK_COMP_ARRAY(COMP_DUMMY()), // CODEC
DAILINK_COMP_ARRAY(COMP_EMPTY()));
SND_SOC_DAILINK_DEFS(sense,
DAILINK_COMP_ARRAY(COMP_CPU("mca-pcm-2")), // CPU
DAILINK_COMP_ARRAY(COMP_DUMMY()), // CODEC
DAILINK_COMP_ARRAY(COMP_EMPTY()));
static struct snd_soc_dai_link macaudio_fe_links[] = {
{
.name = "Primary",
.stream_name = "Primary",
.dynamic = 1,
.dpcm_playback = 1,
.dpcm_capture = 1,
.dpcm_merged_rate = 1,
.dpcm_merged_chan = 1,
.dpcm_merged_format = 1,
.dai_fmt = MACAUDIO_DAI_FMT,
SND_SOC_DAILINK_REG(primary),
},
{
.name = "Secondary",
.stream_name = "Secondary",
.dynamic = 1,
.dpcm_playback = 1,
.dpcm_merged_rate = 1,
.dpcm_merged_chan = 1,
.dpcm_merged_format = 1,
.dai_fmt = MACAUDIO_DAI_FMT,
SND_SOC_DAILINK_REG(secondary),
},
{
.name = "Speaker Sense",
.stream_name = "Speaker Sense",
.dynamic = 1,
.dpcm_capture = 1,
.dai_fmt = (SND_SOC_DAIFMT_I2S | \
SND_SOC_DAIFMT_CBP_CFP | \
SND_SOC_DAIFMT_GATED | \
SND_SOC_DAIFMT_IB_IF),
SND_SOC_DAILINK_REG(sense),
},
};
static struct macaudio_link_props macaudio_fe_link_props[] = {
{
/*
* Primary FE
*
* The bclk ratio at 64 for the primary frontend is important
* to ensure that the headphones codec's idea of left and right
* in a stereo stream over I2S fits in nicely with everyone else's.
* (This is until the headphones codec's driver supports
* set_tdm_slot.)
*
* The low bclk ratio precludes transmitting more than two
* channels over I2S, but that's okay since there is the secondary
* FE for speaker arrays anyway.
*/
.bclk_ratio = 64,
},
{
/*
* Secondary FE
*
* Here we want frames plenty long to be able to drive all
* those fancy speaker arrays.
*/
.bclk_ratio = 256,
},
{
.is_sense = 1,
}
};
static void macaudio_vlimit_unlock(struct macaudio_snd_data *ma, bool unlock)
{
int ret, max;
const char *name = volume_control_names[ma->cfg->amp];
if (!name) {
WARN_ON_ONCE(1);
return;
}
switch (ma->cfg->amp) {
case AMP_NONE:
WARN_ON_ONCE(1);
return;
case AMP_TAS5770:
if (unlock)
max = TAS5770_0DB;
else
max = 1; //TAS5770_DB(ma->cfg->safe_vol);
break;
case AMP_SN012776:
if (unlock)
max = SN012776_0DB;
else
max = 1; //SN012776_DB(ma->cfg->safe_vol);
break;
case AMP_SSM3515:
if (unlock)
max = SSM3515_0DB;
else
max = SSM3515_DB(ma->cfg->safe_vol);
break;
}
ret = snd_soc_limit_volume(&ma->card, name, max);
if (ret < 0)
dev_err(ma->card.dev, "Failed to %slock volume %s: %d\n",
unlock ? "un" : "", name, ret);
}
static void macaudio_vlimit_update(struct macaudio_snd_data *ma)
{
int i;
bool unlock = true;
struct snd_kcontrol *kctl;
const char *reason;
/* Do nothing if there is no safety configured */
if (!ma->has_safety)
return;
/* Check that someone is holding the main lock */
if (!ma->speaker_lock_owner) {
reason = "Main control not locked";
unlock = false;
}
/* Check that the control has been pinged within the timeout */
if (ma->speaker_lock_remain <= 0) {
reason = "Lock timeout";
unlock = false;
}
/* Check that *every* limited control is locked by the same owner */
list_for_each_entry(kctl, &ma->card.snd_card->controls, list) {
if(!snd_soc_control_matches(kctl, volume_control_names[ma->cfg->amp]))
continue;
for (i = 0; i < kctl->count; i++) {
if (kctl->vd[i].owner != ma->speaker_lock_owner) {
reason = "Not all child controls locked by the same process";
unlock = false;
}
}
}
if (unlock != ma->speaker_volume_unlocked) {
if (unlock) {
dev_info(ma->card.dev, "Speaker volumes unlocked\n");
} else {
dev_info(ma->card.dev, "Speaker volumes locked: %s\n", reason);
ma->speaker_volume_was_locked = true;
}
macaudio_vlimit_unlock(ma, unlock);
ma->speaker_volume_unlocked = unlock;
snd_ctl_notify(ma->card.snd_card, SNDRV_CTL_EVENT_MASK_VALUE,
&ma->speaker_lock_kctl->id);
}
}
static void macaudio_vlimit_enable_timeout(struct macaudio_snd_data *ma)
{
mutex_lock(&ma->volume_lock_mutex);
if (ma->speaker_lock_timeout_enabled) {
mutex_unlock(&ma->volume_lock_mutex);
return;
}
if (ma->speaker_lock_remain > 0) {
ma->speaker_lock_timeout = ktime_add(ktime_get(), ma->speaker_lock_remain);
schedule_delayed_work(&ma->lock_timeout_work, usecs_to_jiffies(ktime_to_us(ma->speaker_lock_remain)));
dev_dbg(ma->card.dev, "Enabling volume limit timeout: %ld us left\n",
(long)ktime_to_us(ma->speaker_lock_remain));
}
macaudio_vlimit_update(ma);
ma->speaker_lock_timeout_enabled = true;
mutex_unlock(&ma->volume_lock_mutex);
}
static void macaudio_vlimit_disable_timeout(struct macaudio_snd_data *ma)
{
ktime_t now;
mutex_lock(&ma->volume_lock_mutex);
if (!ma->speaker_lock_timeout_enabled) {
mutex_unlock(&ma->volume_lock_mutex);
return;
}
now = ktime_get();
cancel_delayed_work(&ma->lock_timeout_work);
if (ktime_after(now, ma->speaker_lock_timeout))
ma->speaker_lock_remain = 0;
else if (ma->speaker_lock_remain > 0)
ma->speaker_lock_remain = ktime_sub(ma->speaker_lock_timeout, now);
dev_dbg(ma->card.dev, "Disabling volume limit timeout: %ld us left\n",
(long)ktime_to_us(ma->speaker_lock_remain));
macaudio_vlimit_update(ma);
ma->speaker_lock_timeout_enabled = false;
mutex_unlock(&ma->volume_lock_mutex);
}
static void macaudio_vlimit_timeout_work(struct work_struct *wrk)
{
struct macaudio_snd_data *ma = container_of(to_delayed_work(wrk),
struct macaudio_snd_data, lock_timeout_work);
mutex_lock(&ma->volume_lock_mutex);
ma->speaker_lock_remain = 0;
macaudio_vlimit_update(ma);
mutex_unlock(&ma->volume_lock_mutex);
}
static void macaudio_vlimit_update_work(struct work_struct *wrk)
{
struct macaudio_snd_data *ma = container_of(wrk,
struct macaudio_snd_data, lock_update_work);
if (ma->bes_active)
macaudio_vlimit_enable_timeout(ma);
else
macaudio_vlimit_disable_timeout(ma);
}
static int macaudio_copy_link(struct device *dev, struct snd_soc_dai_link *target,
struct snd_soc_dai_link *source)
{
memcpy(target, source, sizeof(struct snd_soc_dai_link));
target->cpus = devm_kmemdup(dev, target->cpus,
sizeof(*target->cpus) * target->num_cpus,
GFP_KERNEL);
target->codecs = devm_kmemdup(dev, target->codecs,
sizeof(*target->codecs) * target->num_codecs,
GFP_KERNEL);
target->platforms = devm_kmemdup(dev, target->platforms,
sizeof(*target->platforms) * target->num_platforms,
GFP_KERNEL);
if (!target->cpus || !target->codecs || !target->platforms)
return -ENOMEM;
return 0;
}
static int macaudio_parse_of_component(struct device_node *node, int index,
struct snd_soc_dai_link_component *comp)
{
struct of_phandle_args args;
int ret;
ret = of_parse_phandle_with_args(node, "sound-dai", "#sound-dai-cells",
index, &args);
if (ret)
return ret;
comp->of_node = args.np;
return snd_soc_get_dai_name(&args, &comp->dai_name);
}
/*
* Parse one DPCM backend from the devicetree. This means taking one
* of the CPU DAIs and combining it with one or more CODEC DAIs.
*/
static int macaudio_parse_of_be_dai_link(struct macaudio_snd_data *ma,
struct snd_soc_dai_link *link,
int be_index, int ncodecs_per_be,
struct device_node *cpu,
struct device_node *codec)
{
struct snd_soc_dai_link_component *comp;
struct device *dev = ma->card.dev;
int codec_base = be_index * ncodecs_per_be;
int ret, i;
link->no_pcm = 1;
link->dpcm_playback = 1;
link->dpcm_capture = 1;
link->dai_fmt = MACAUDIO_DAI_FMT;
link->num_codecs = ncodecs_per_be;
link->codecs = devm_kcalloc(dev, ncodecs_per_be,
sizeof(*comp), GFP_KERNEL);
link->num_cpus = 1;
link->cpus = devm_kzalloc(dev, sizeof(*comp), GFP_KERNEL);
if (!link->codecs || !link->cpus)
return -ENOMEM;
link->num_platforms = 0;
for_each_link_codecs(link, i, comp) {
ret = macaudio_parse_of_component(codec, codec_base + i, comp);
if (ret)
return dev_err_probe(ma->card.dev, ret, "parsing CODEC DAI of link '%s' at %pOF\n",
link->name, codec);
}
ret = macaudio_parse_of_component(cpu, be_index, link->cpus);
if (ret)
return dev_err_probe(ma->card.dev, ret, "parsing CPU DAI of link '%s' at %pOF\n",
link->name, codec);
link->name = link->cpus[0].dai_name;
return 0;
}
static int macaudio_parse_of(struct macaudio_snd_data *ma)
{
struct device_node *codec = NULL;
struct device_node *cpu = NULL;
struct device_node *np = NULL;
struct device_node *platform = NULL;
struct snd_soc_dai_link *link = NULL;
struct snd_soc_card *card = &ma->card;
struct device *dev = card->dev;
struct macaudio_link_props *link_props;
int ret, num_links, i;
ret = snd_soc_of_parse_card_name(card, "model");
if (ret) {
dev_err_probe(dev, ret, "parsing card name\n");
return ret;
}
/* Populate links, start with the fixed number of FE links */
num_links = ARRAY_SIZE(macaudio_fe_links);
/* Now add together the (dynamic) number of BE links */
for_each_available_child_of_node(dev->of_node, np) {
int num_cpus;
cpu = of_get_child_by_name(np, "cpu");
if (!cpu) {
ret = dev_err_probe(dev, -EINVAL,
"missing CPU DAI node at %pOF\n", np);
goto err_free;
}
num_cpus = of_count_phandle_with_args(cpu, "sound-dai",
"#sound-dai-cells");
if (num_cpus <= 0) {
ret = dev_err_probe(card->dev, -EINVAL,
"missing sound-dai property at %pOF\n", cpu);
goto err_free;
}
of_node_put(cpu);
cpu = NULL;
/* Each CPU specified counts as one BE link */
num_links += num_cpus;
}
/* Allocate the DAI link array */
card->dai_link = devm_kcalloc(dev, num_links, sizeof(*link), GFP_KERNEL);
ma->link_props = devm_kcalloc(dev, num_links, sizeof(*ma->link_props), GFP_KERNEL);
if (!card->dai_link || !ma->link_props)
return -ENOMEM;
link = card->dai_link;
link_props = ma->link_props;
for (i = 0; i < ARRAY_SIZE(macaudio_fe_links); i++) {
ret = macaudio_copy_link(dev, link, &macaudio_fe_links[i]);
if (ret)
goto err_free;
memcpy(link_props, &macaudio_fe_link_props[i], sizeof(struct macaudio_link_props));
link++; link_props++;
}
for (i = 0; i < num_links; i++)
card->dai_link[i].id = i;
/* We might disable the speakers, so count again */
num_links = ARRAY_SIZE(macaudio_fe_links);
/* Fill in the BEs */
for_each_available_child_of_node(dev->of_node, np) {
const char *link_name;
bool speakers;
int be_index, num_codecs, num_bes, ncodecs_per_cpu, nchannels;
unsigned int left_mask, right_mask;
ret = of_property_read_string(np, "link-name", &link_name);
if (ret) {
dev_err_probe(card->dev, ret, "missing link name\n");
goto err_free;
}
dev_dbg(ma->card.dev, "parsing link '%s'\n", link_name);
speakers = !strcmp(link_name, "Speaker")
|| !strcmp(link_name, "Speakers");
if (speakers) {
if (!ma->cfg->enable_speakers && !please_blow_up_my_speakers) {
dev_err(card->dev, "driver can't assure safety on this model, disabling speakers\n");
continue;
}
ma->has_speakers = 1;
if (ma->cfg->amp != AMP_SSM3515 && ma->cfg->safe_vol != 0)
ma->has_sense = 1;
}
cpu = of_get_child_by_name(np, "cpu");
codec = of_get_child_by_name(np, "codec");
if (!codec || !cpu) {
ret = dev_err_probe(dev, -EINVAL,
"missing DAI specifications for '%s'\n", link_name);
goto err_free;
}
num_bes = of_count_phandle_with_args(cpu, "sound-dai",
"#sound-dai-cells");
if (num_bes <= 0) {
ret = dev_err_probe(card->dev, -EINVAL,
"missing sound-dai property at %pOF\n", cpu);
goto err_free;
}
num_codecs = of_count_phandle_with_args(codec, "sound-dai",
"#sound-dai-cells");
if (num_codecs <= 0) {
ret = dev_err_probe(card->dev, -EINVAL,
"missing sound-dai property at %pOF\n", codec);
goto err_free;
}
dev_dbg(ma->card.dev, "link '%s': %d CPUs %d CODECs\n",
link_name, num_bes, num_codecs);
if (num_codecs % num_bes != 0) {
ret = dev_err_probe(card->dev, -EINVAL,
"bad combination of CODEC (%d) and CPU (%d) number at %pOF\n",
num_codecs, num_bes, np);
goto err_free;
}
/*
* Now parse the cpu/codec lists into a number of DPCM backend links.
* In each link there will be one DAI from the cpu list paired with
* an evenly distributed number of DAIs from the codec list. (As is
* the binding semantics.)
*/
ncodecs_per_cpu = num_codecs / num_bes;
nchannels = num_codecs * (speakers ? 1 : 2);
/* Save the max number of channels on the platform */
if (nchannels > ma->max_channels)
ma->max_channels = nchannels;
/*
* If there is a single speaker, assign two channels to it, because
* it can do downmix.
*/
if (nchannels < 2)
nchannels = 2;
left_mask = 0;
for (i = 0; i < nchannels; i += 2)
left_mask = left_mask << 2 | 1;
right_mask = left_mask << 1;
for (be_index = 0; be_index < num_bes; be_index++) {
/*
* Set initial link name to be overwritten by a BE-specific
* name later so that we can use at least use the provisional
* name in error messages.
*/
link->name = link_name;
ret = macaudio_parse_of_be_dai_link(ma, link, be_index,
ncodecs_per_cpu, cpu, codec);
if (ret)
goto err_free;
link_props->is_speakers = speakers;
link_props->is_headphones = !speakers;
if (num_bes == 2)
/* This sound peripheral is split between left and right BE */
link_props->tdm_mask = be_index ? right_mask : left_mask;
else
/* One BE covers all of the peripheral */
link_props->tdm_mask = left_mask | right_mask;
/* Steal platform OF reference for use in FE links later */
platform = link->cpus->of_node;
link++; link_props++;
}
of_node_put(codec);
of_node_put(cpu);
cpu = codec = NULL;
num_links += num_bes;
}
for (i = 0; i < ARRAY_SIZE(macaudio_fe_links); i++)
card->dai_link[i].platforms->of_node = platform;
/* Skip the speaker sense PCM link if this amp has no sense (or no speakers) */
if (!ma->has_sense) {
for (i = 0; i < ARRAY_SIZE(macaudio_fe_links); i++) {
if (ma->link_props[i].is_sense) {
memmove(&card->dai_link[i], &card->dai_link[i + 1],
(num_links - i - 1) * sizeof (struct snd_soc_dai_link));
num_links--;
break;
}
}
}
card->num_links = num_links;
return 0;
err_free:
of_node_put(codec);
of_node_put(cpu);
of_node_put(np);
if (!card->dai_link)
return ret;
for (i = 0; i < num_links; i++) {
/*
* TODO: If we don't go through this path are the references
* freed inside ASoC?
*/
snd_soc_of_put_dai_link_codecs(&card->dai_link[i]);
snd_soc_of_put_dai_link_cpus(&card->dai_link[i]);
}
return ret;
}
static int macaudio_get_runtime_bclk_ratio(struct snd_pcm_substream *substream)
{
struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
struct macaudio_snd_data *ma = snd_soc_card_get_drvdata(rtd->card);
struct snd_soc_dpcm *dpcm;
/*
* If this is a FE, look it up in link_props directly.
* If this is a BE, look it up in the respective FE.
*/
if (!rtd->dai_link->no_pcm)
return ma->link_props[rtd->dai_link->id].bclk_ratio;
for_each_dpcm_fe(rtd, substream->stream, dpcm) {
int fe_id = dpcm->fe->dai_link->id;
return ma->link_props[fe_id].bclk_ratio;
}
return 0;
}
static int macaudio_dpcm_hw_params(struct snd_pcm_substream *substream,
struct snd_pcm_hw_params *params)
{
struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
struct macaudio_snd_data *ma = snd_soc_card_get_drvdata(rtd->card);
struct macaudio_link_props *props = &ma->link_props[rtd->dai_link->id];
struct snd_soc_dai *cpu_dai = snd_soc_rtd_to_cpu(rtd, 0);
struct snd_interval *rate = hw_param_interval(params,
SNDRV_PCM_HW_PARAM_RATE);
int bclk_ratio = macaudio_get_runtime_bclk_ratio(substream);
int i;
if (props->is_sense) {
rate->min = rate->max = cpu_dai->rate;
return 0;
}
/* Speakers BE */
if (props->is_speakers) {
if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) {
/* Sense PCM: keep the existing BE rate (0 if not already running) */
rate->min = rate->max = cpu_dai->rate;
return 0;
} else {
/*
* Set the sense PCM rate control to inform userspace of the
* new sample rate.
*/
ma->speaker_sample_rate = params_rate(params);
snd_ctl_notify(ma->card.snd_card, SNDRV_CTL_EVENT_MASK_VALUE,
&ma->speaker_sample_rate_kctl->id);
}
}
if (bclk_ratio) {
struct snd_soc_dai *dai;
int mclk = params_rate(params) * bclk_ratio;
for_each_rtd_codec_dais(rtd, i, dai) {
snd_soc_dai_set_sysclk(dai, 0, mclk, SND_SOC_CLOCK_IN);
snd_soc_dai_set_bclk_ratio(dai, bclk_ratio);
}
snd_soc_dai_set_sysclk(cpu_dai, 0, mclk, SND_SOC_CLOCK_OUT);
snd_soc_dai_set_bclk_ratio(cpu_dai, bclk_ratio);
}
return 0;
}
static int macaudio_fe_startup(struct snd_pcm_substream *substream)
{
struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
struct macaudio_snd_data *ma = snd_soc_card_get_drvdata(rtd->card);
struct macaudio_link_props *props = &ma->link_props[rtd->dai_link->id];
int max_rate, ret;
if (props->is_sense) {
/*
* Sense stream will not return data while playback is inactive,
* so do not time out.
*/
substream->wait_time = MAX_SCHEDULE_TIMEOUT;
return 0;
}
ret = snd_pcm_hw_constraint_minmax(substream->runtime,
SNDRV_PCM_HW_PARAM_CHANNELS,
0, ma->max_channels);
if (ret < 0)
return ret;
max_rate = MACAUDIO_MAX_BCLK_FREQ / props->bclk_ratio;
ret = snd_pcm_hw_constraint_minmax(substream->runtime,
SNDRV_PCM_HW_PARAM_RATE,
0, max_rate);
if (ret < 0)
return ret;
return 0;
}
static int macaudio_fe_hw_params(struct snd_pcm_substream *substream,
struct snd_pcm_hw_params *params)
{
struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
struct snd_soc_pcm_runtime *be;
struct snd_soc_dpcm *dpcm;
be = NULL;
for_each_dpcm_be(rtd, substream->stream, dpcm) {
be = dpcm->be;
break;
}
if (!be) {
dev_err(rtd->dev, "opening PCM device '%s' with no audio route configured by the user\n",
rtd->dai_link->name);
return -EINVAL;
}
return macaudio_dpcm_hw_params(substream, params);
}
static void macaudio_dpcm_shutdown(struct snd_pcm_substream *substream)
{
struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
struct snd_soc_dai *cpu_dai = snd_soc_rtd_to_cpu(rtd, 0);
struct snd_soc_dai *dai;
int bclk_ratio = macaudio_get_runtime_bclk_ratio(substream);
int i;
if (bclk_ratio) {
for_each_rtd_codec_dais(rtd, i, dai)
snd_soc_dai_set_sysclk(dai, 0, 0, SND_SOC_CLOCK_IN);
snd_soc_dai_set_sysclk(cpu_dai, 0, 0, SND_SOC_CLOCK_OUT);
}
}
static int macaudio_be_hw_free(struct snd_pcm_substream *substream)
{
struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
struct macaudio_snd_data *ma = snd_soc_card_get_drvdata(rtd->card);
struct macaudio_link_props *props = &ma->link_props[rtd->dai_link->id];
struct snd_soc_dai *dai;
int i;
if (props->is_speakers && substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
/*
* Clear the DAI rates, so the next open can change the sample rate.
* This won't happen automatically if the sense PCM is open.
*/
for_each_rtd_dais(rtd, i, dai) {
dai->rate = 0;
}
/* Notify userspace that the speakers are closed */
ma->speaker_sample_rate = 0;
snd_ctl_notify(ma->card.snd_card, SNDRV_CTL_EVENT_MASK_VALUE,
&ma->speaker_sample_rate_kctl->id);
}
return 0;
}
static int macaudio_be_trigger(struct snd_pcm_substream *substream, int cmd)
{
struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
struct macaudio_snd_data *ma = snd_soc_card_get_drvdata(rtd->card);
struct macaudio_link_props *props = &ma->link_props[rtd->dai_link->id];
if (props->is_speakers && substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
switch (cmd) {
case SNDRV_PCM_TRIGGER_START:
case SNDRV_PCM_TRIGGER_RESUME:
case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
ma->bes_active |= BIT(rtd->dai_link->id);
break;
case SNDRV_PCM_TRIGGER_SUSPEND:
case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
case SNDRV_PCM_TRIGGER_STOP:
ma->bes_active &= ~BIT(rtd->dai_link->id);
break;
default:
return -EINVAL;
}
schedule_work(&ma->lock_update_work);
}
return 0;
}
static const struct snd_soc_ops macaudio_fe_ops = {
.startup = macaudio_fe_startup,
.shutdown = macaudio_dpcm_shutdown,
.hw_params = macaudio_fe_hw_params,
};
static const struct snd_soc_ops macaudio_be_ops = {
.hw_free = macaudio_be_hw_free,
.shutdown = macaudio_dpcm_shutdown,
.hw_params = macaudio_dpcm_hw_params,
.trigger = macaudio_be_trigger,
};
static int macaudio_be_assign_tdm(struct snd_soc_pcm_runtime *rtd)
{
struct snd_soc_card *card = rtd->card;
struct macaudio_snd_data *ma = snd_soc_card_get_drvdata(card);
struct macaudio_link_props *props = &ma->link_props[rtd->dai_link->id];
struct snd_soc_dai *dai;
unsigned int mask;
int nslots, ret, i;
if (!props->tdm_mask)
return 0;
mask = props->tdm_mask;
nslots = __fls(mask) + 1;
if (rtd->dai_link->num_codecs == 1) {
ret = snd_soc_dai_set_tdm_slot(snd_soc_rtd_to_codec(rtd, 0), mask,
0, nslots, MACAUDIO_SLOTWIDTH);
/*
* Headphones get a pass on -ENOTSUPP (see the comment
* around bclk_ratio value for primary FE).
*/
if (ret == -ENOTSUPP && props->is_headphones)
return 0;
return ret;
}
for_each_rtd_codec_dais(rtd, i, dai) {
int slot = __ffs(mask);
mask &= ~(1 << slot);
ret = snd_soc_dai_set_tdm_slot(dai, 1 << slot, 0, nslots,
MACAUDIO_SLOTWIDTH);
if (ret)
return ret;
}
return 0;
}
static int macaudio_be_init(struct snd_soc_pcm_runtime *rtd)
{
struct snd_soc_card *card = rtd->card;
struct macaudio_snd_data *ma = snd_soc_card_get_drvdata(card);
struct macaudio_link_props *props = &ma->link_props[rtd->dai_link->id];
struct snd_soc_dai *dai;
int i, ret;
ret = macaudio_be_assign_tdm(rtd);
if (ret < 0)
return ret;
if (props->is_headphones) {
for_each_rtd_codec_dais(rtd, i, dai)
snd_soc_component_set_jack(dai->component, &ma->jack, NULL);
}
return 0;
}
static void macaudio_be_exit(struct snd_soc_pcm_runtime *rtd)
{
struct snd_soc_card *card = rtd->card;
struct macaudio_snd_data *ma = snd_soc_card_get_drvdata(card);
struct macaudio_link_props *props = &ma->link_props[rtd->dai_link->id];
struct snd_soc_dai *dai;
int i;
if (props->is_headphones) {
for_each_rtd_codec_dais(rtd, i, dai)
snd_soc_component_set_jack(dai->component, NULL, NULL);
}
}
static int macaudio_fe_init(struct snd_soc_pcm_runtime *rtd)