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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
|
/*
* audio codec driver
* Copyright 2016 Google Inc.
* Copyright 2016 Linaro Ltd.
*
* Released under the GPLv2 only.
*/
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/pm_runtime.h>
#include <sound/soc.h>
#include <sound/pcm_params.h>
#include <uapi/linux/input.h>
#include "audio_codec.h"
#include "audio_apbridgea.h"
#include "audio_manager.h"
static struct gbaudio_codec_info *gbcodec;
static struct gbaudio_data_connection *
find_data(struct gbaudio_module_info *module, const char *name)
{
struct gbaudio_data_connection *data;
list_for_each_entry(data, &module->data_list, list) {
if (name && !strncmp(data->name, name, NAME_SIZE))
return data;
}
return NULL;
}
static int find_stream(const char *name)
{
int stream = 0;
if (strnstr(name, "SPK Amp", NAME_SIZE))
stream |= GB_PLAYBACK;
return stream;
}
static int gbaudio_module_disable(struct gbaudio_codec_info *codec,
struct gbaudio_module_info *module,
int dir)
{
int ret = 0;
uint16_t data_cport, cportid, i2s_port;
int codec_state, module_state;
struct gbaudio_data_connection *data;
const char *dai_name;
mutex_lock(&codec->lock);
codec_state = codec->stream[dir].state;
if (codec_state == GBAUDIO_CODEC_SHUTDOWN) {
mutex_unlock(&codec->lock);
return 0;
}
dai_name = codec->stream[dir].dai_name;
module_state = module->ctrlstate[dir];
if (module_state == GBAUDIO_CODEC_SHUTDOWN) {
dev_dbg(codec->dev, "%s: module already configured\n",
module->name);
goto func_exit;
}
/* find the dai */
data = find_data(module, dai_name);
if (!data) {
dev_err(codec->dev, "%s:%s DATA connection missing\n",
dai_name, module->name);
ret = -ENODEV;
goto func_exit;
}
if (codec_state > GBAUDIO_CODEC_HWPARAMS) {
data_cport = data->connection->intf_cport_id;
switch(dir) {
case SNDRV_PCM_STREAM_CAPTURE:
ret = gb_audio_gb_deactivate_rx(
module->mgmt_connection,
data_cport);
break;
case SNDRV_PCM_STREAM_PLAYBACK:
ret = gb_audio_gb_deactivate_tx(
module->mgmt_connection,
data_cport);
break;
default:
ret = -EINVAL;
}
if (ret) {
dev_err_ratelimited(codec->dev, "deactivate for %s failed:%d\n",
module->name, ret);
goto func_exit;
}
dev_dbg(codec->dev, "Dynamic deactivate %s:%d DAI\n", dai_name,
data_cport);
}
if (codec_state > GBAUDIO_CODEC_SHUTDOWN) {
cportid = data->connection->hd_cport_id;
switch(dir) {
case SNDRV_PCM_STREAM_CAPTURE:
ret = gb_audio_apbridgea_unregister_cport(
data->connection,
i2s_port, cportid,
AUDIO_APBRIDGEA_DIRECTION_RX);
break;
case SNDRV_PCM_STREAM_PLAYBACK:
ret = gb_audio_apbridgea_unregister_cport(
data->connection,
i2s_port, cportid,
AUDIO_APBRIDGEA_DIRECTION_TX);
break;
default:
ret = -EINVAL;
}
if (ret) {
dev_err_ratelimited(codec->dev, "unregister_cport for %s failed:%d\n",
module->name, ret);
goto func_exit;
}
dev_dbg(codec->dev, "Dynamic Unregister %s:%d DAI\n", dai_name,
cportid);
}
module->ctrlstate[dir] = GBAUDIO_CODEC_SHUTDOWN;
func_exit:
mutex_unlock(&codec->lock);
return ret;
}
static int gbaudio_module_enable(struct gbaudio_codec_info *codec,
struct gbaudio_module_info *module, int dir)
{
int ret = 0;
__u16 i2s_port, cportid;
int codec_state, module_state;
uint16_t data_cport;
uint8_t sig_bits, channels;
uint32_t format, rate;
struct gbaudio_data_connection *data;
const char *dai_name;
mutex_lock(&codec->lock);
codec_state = codec->stream[dir].state;
if (codec_state == GBAUDIO_CODEC_SHUTDOWN) {
mutex_unlock(&codec->lock);
return 0;
}
dai_name = codec->stream[dir].dai_name;
format = codec->stream[dir].format;
channels = codec->stream[dir].channels;
rate = codec->stream[dir].rate;
sig_bits = codec->stream[dir].sig_bits;
module_state = module->ctrlstate[dir];
if (module_state == codec_state) {
dev_dbg(codec->dev, "%s: module already configured\n",
module->name);
goto func_exit;
}
/* find the dai */
data = find_data(module, dai_name);
if (!data) {
dev_err(codec->dev, "%s:%s DATA connection missing\n",
dai_name, module->name);
ret = -ENODEV;
goto func_exit;
}
/* register cport */
if (module_state < codec_state) {
i2s_port = 0; /* fixed for now */
cportid = data->connection->hd_cport_id;
switch(dir) {
case SNDRV_PCM_STREAM_CAPTURE:
ret = gb_audio_apbridgea_register_cport(
data->connection,
i2s_port, cportid,
AUDIO_APBRIDGEA_DIRECTION_RX);
break;
case SNDRV_PCM_STREAM_PLAYBACK:
ret = gb_audio_apbridgea_register_cport(
data->connection,
i2s_port, cportid,
AUDIO_APBRIDGEA_DIRECTION_TX);
break;
default:
ret = -EINVAL;
}
if (ret) {
dev_err_ratelimited(codec->dev, "reg_cport for %s\n", module->name);
goto func_exit;
}
module_state = GBAUDIO_CODEC_STARTUP;
dev_dbg(codec->dev, "Dynamic Register %s:%d DAI\n", dai_name,
cportid);
}
/* hw_params */
if (module_state < codec_state) {
data_cport = data->connection->intf_cport_id;
ret = gb_audio_gb_set_pcm(module->mgmt_connection, data_cport,
format, rate, channels, sig_bits);
if (ret) {
dev_err_ratelimited(codec->dev, "set_pcm for %s\n", module->name);
goto func_exit;
}
module_state = GBAUDIO_CODEC_HWPARAMS;
dev_dbg(codec->dev, "Dynamic hw_params %s:%d DAI\n", dai_name,
data_cport);
}
/* prepare */
if (module_state < codec_state) {
data_cport = data->connection->intf_cport_id;
switch(dir) {
case SNDRV_PCM_STREAM_CAPTURE:
ret = gb_audio_gb_set_rx_data_size(
module->mgmt_connection,
data_cport, 192);
if (ret) {
dev_err_ratelimited(codec->dev,
"set_rx_data_size for %s\n",
module->name);
goto func_exit;
}
ret = gb_audio_gb_activate_rx(module->mgmt_connection,
data_cport);
if (ret) {
dev_err_ratelimited(codec->dev, "activate_rx for %s\n",
module->name);
goto func_exit;
}
break;
case SNDRV_PCM_STREAM_PLAYBACK:
ret = gb_audio_gb_set_tx_data_size(
module->mgmt_connection,
data_cport, 192);
if (ret) {
dev_err_ratelimited(codec->dev,
"set_tx_data_size for %s\n",
module->name);
goto func_exit;
}
ret = gb_audio_gb_activate_tx(module->mgmt_connection,
data_cport);
if (ret) {
dev_err_ratelimited(codec->dev, "activate_tx for %s\n",
module->name);
goto func_exit;
}
break;
default:
dev_err(codec->dev, "Inavlid stream direction\n");
ret = -EINVAL;
goto func_exit;
}
module_state = GBAUDIO_CODEC_PREPARE;
dev_dbg(codec->dev, "Dynamic prepare %s:%d DAI\n", dai_name,
data_cport);
}
func_exit:
module->ctrlstate[dir] = module_state;
mutex_unlock(&codec->lock);
return ret;
}
int gbaudio_module_update(struct gbaudio_codec_info *codec,
const char *w_name,
struct gbaudio_module_info *module, int enable)
{
int stream, ret = 0;
int pb_state, cap_state;
dev_dbg(module->dev, "Module update %s sequence\n",
enable ? "Enable":"Disable");
stream = find_stream(w_name);
if (!stream) {
dev_dbg(codec->dev, "No action required for %s\n", w_name);
return 0;
}
/* check if playback active */
pb_state = codec->stream[SNDRV_PCM_STREAM_PLAYBACK].state;
if ((stream & GB_PLAYBACK) && (pb_state > GBAUDIO_CODEC_SHUTDOWN)) {
if (enable)
ret = gbaudio_module_enable(codec, module,
SNDRV_PCM_STREAM_PLAYBACK);
else
ret = gbaudio_module_disable(codec, module,
SNDRV_PCM_STREAM_PLAYBACK);
}
/* check if capture active */
cap_state = codec->stream[SNDRV_PCM_STREAM_CAPTURE].state;
if ((stream & GB_CAPTURE) && (cap_state > GBAUDIO_CODEC_SHUTDOWN)) {
if (enable)
ret = gbaudio_module_enable(codec, module,
SNDRV_PCM_STREAM_CAPTURE);
else
ret = gbaudio_module_disable(codec, module,
SNDRV_PCM_STREAM_CAPTURE);
}
return ret;
}
EXPORT_SYMBOL(gbaudio_module_update);
/*
* codec DAI ops
*/
static int gbcodec_startup(struct snd_pcm_substream *substream,
struct snd_soc_dai *dai)
{
int ret = 0;
__u16 i2s_port, cportid;
int state;
struct gbaudio_data_connection *data;
struct gbaudio_module_info *module;
struct gbaudio_codec_info *codec = dev_get_drvdata(dai->dev);
mutex_lock(&codec->lock);
if (list_empty(&codec->module_list)) {
dev_err(codec->dev, "No codec module available\n");
mutex_unlock(&codec->lock);
return -ENODEV;
}
state = codec->stream[substream->stream].state;
list_for_each_entry(module, &codec->module_list, list) {
/* find the dai */
data = find_data(module, dai->name);
if (!data) {
dev_err(dai->dev, "%s:%s DATA connection missing\n",
dai->name, module->name);
continue;
}
/* register cport */
i2s_port = 0; /* fixed for now */
cportid = data->connection->hd_cport_id;
switch (substream->stream) {
case SNDRV_PCM_STREAM_CAPTURE:
ret = gb_audio_apbridgea_register_cport(
data->connection,
i2s_port, cportid,
AUDIO_APBRIDGEA_DIRECTION_RX);
break;
case SNDRV_PCM_STREAM_PLAYBACK:
ret = gb_audio_apbridgea_register_cport(
data->connection,
i2s_port, cportid,
AUDIO_APBRIDGEA_DIRECTION_TX);
break;
default:
dev_err(dai->dev, "Inavlid stream\n");
mutex_unlock(&codec->lock);
return -EINVAL;
}
dev_dbg(dai->dev, "Register %s:%d DAI, ret:%d\n", dai->name,
cportid, ret);
state = GBAUDIO_CODEC_STARTUP;
module->ctrlstate[substream->stream] = state;
dev_dbg(dai->dev, "%s: state:%d\n", module->name, state);
}
codec->stream[substream->stream].state = state;
codec->stream[substream->stream].dai_name = dai->name;
mutex_unlock(&codec->lock);
/* to prevent suspend in case of active audio */
pm_stay_awake(dai->dev);
return ret;
}
static int gbmodule_shutdown_tx(struct gbaudio_module_info *module,
struct gbaudio_data_connection *data,
int codec_state, struct device *dev)
{
int ret, module_state;
__u16 i2s_port, cportid;
module_state = module->ctrlstate[0];
if (module_state == GBAUDIO_CODEC_SHUTDOWN) {
dev_dbg(dev, "%s: module already configured\n",
module->name);
return 0;
}
/* deactivate */
cportid = data->connection->intf_cport_id;
if (module_state >= GBAUDIO_CODEC_PREPARE) {
ret = gb_audio_gb_deactivate_tx(module->mgmt_connection,
cportid);
if (ret)
return ret;
}
/* unregister cport */
i2s_port = 0; /* fixed for now */
cportid = data->connection->hd_cport_id;
ret = gb_audio_apbridgea_unregister_cport(data->connection, i2s_port,
cportid,
AUDIO_APBRIDGEA_DIRECTION_TX);
return ret;
}
static int gbmodule_shutdown_rx(struct gbaudio_module_info *module,
struct gbaudio_data_connection *data,
int codec_state, struct device *dev)
{
int ret, module_state;
__u16 i2s_port, cportid;
module_state = module->ctrlstate[1];
if (module_state == GBAUDIO_CODEC_SHUTDOWN) {
dev_dbg(dev, "%s: module already configured\n",
module->name);
return 0;
}
/* deactivate */
cportid = data->connection->intf_cport_id;
if (module_state >= GBAUDIO_CODEC_PREPARE) {
ret = gb_audio_gb_deactivate_rx(module->mgmt_connection,
cportid);
if (ret)
return ret;
}
/* unregister cport */
i2s_port = 0; /* fixed for now */
cportid = data->connection->hd_cport_id;
ret = gb_audio_apbridgea_unregister_cport(data->connection, i2s_port,
cportid,
AUDIO_APBRIDGEA_DIRECTION_RX);
return ret;
}
static void gbcodec_shutdown(struct snd_pcm_substream *substream,
struct snd_soc_dai *dai)
{
int ret, state;
struct gbaudio_module_info *module;
struct gbaudio_data_connection *data;
struct gbaudio_codec_info *codec = dev_get_drvdata(dai->dev);
mutex_lock(&codec->lock);
if (list_empty(&codec->module_list)) {
dev_err(codec->dev, "No codec module available\n");
codec->stream[substream->stream].state = GBAUDIO_CODEC_SHUTDOWN;
codec->stream[substream->stream].dai_name = NULL;
mutex_unlock(&codec->lock);
pm_relax(dai->dev);
return;
}
state = codec->stream[substream->stream].state;
list_for_each_entry(module, &codec->module_list, list) {
/* find the dai */
data = find_data(module, dai->name);
if (!data) {
dev_err(dai->dev, "%s:%s DATA connection missing\n",
dai->name, module->name);
continue;
}
switch (substream->stream) {
case SNDRV_PCM_STREAM_PLAYBACK:
ret = gbmodule_shutdown_tx(module, data, state,
dai->dev);
break;
case SNDRV_PCM_STREAM_CAPTURE:
ret = gbmodule_shutdown_rx(module, data, state,
dai->dev);
break;
}
dev_dbg(dai->dev, "Unregister %s DAI, ret:%d\n", dai->name,
ret);
state = GBAUDIO_CODEC_SHUTDOWN;
module->ctrlstate[substream->stream] = state;
dev_dbg(dai->dev, "%s: state:%d\n", module->name, state);
}
codec->stream[substream->stream].state = state;
codec->stream[substream->stream].dai_name = NULL;
mutex_unlock(&codec->lock);
pm_relax(dai->dev);
return;
}
static int gbcodec_hw_params(struct snd_pcm_substream *substream,
struct snd_pcm_hw_params *hwparams,
struct snd_soc_dai *dai)
{
int ret;
uint8_t sig_bits, channels;
uint32_t format, rate;
uint16_t data_cport;
struct gbaudio_module_info *module;
struct gbaudio_data_connection *data;
int state;
struct gbaudio_codec_info *codec = dev_get_drvdata(dai->dev);
mutex_lock(&codec->lock);
if (list_empty(&codec->module_list)) {
dev_err(codec->dev, "No codec module available\n");
mutex_unlock(&codec->lock);
return -ENODEV;
}
/*
* assuming, currently only 48000 Hz, 16BIT_LE, stereo
* is supported, validate params before configuring codec
*/
if (params_channels(hwparams) != 2) {
dev_err(dai->dev, "Invalid channel count:%d\n",
params_channels(hwparams));
mutex_unlock(&codec->lock);
return -EINVAL;
}
channels = params_channels(hwparams);
if (params_rate(hwparams) != 48000) {
dev_err(dai->dev, "Invalid sampling rate:%d\n",
params_rate(hwparams));
mutex_unlock(&codec->lock);
return -EINVAL;
}
rate = GB_AUDIO_PCM_RATE_48000;
if (params_format(hwparams) != SNDRV_PCM_FORMAT_S16_LE) {
dev_err(dai->dev, "Invalid format:%d\n",
params_format(hwparams));
mutex_unlock(&codec->lock);
return -EINVAL;
}
format = GB_AUDIO_PCM_FMT_S16_LE;
state = codec->stream[substream->stream].state;
list_for_each_entry(module, &codec->module_list, list) {
/* find the data connection */
data = find_data(module, dai->name);
if (!data) {
dev_err(dai->dev, "%s:%s DATA connection missing\n",
dai->name, module->name);
continue;
}
data_cport = data->connection->intf_cport_id;
/* XXX check impact of sig_bit
* it should not change ideally
*/
dev_dbg(dai->dev,
"cport:%d, rate:%d, channel %d, format %d, sig_bits:%d\n",
data_cport, rate, channels, format, sig_bits);
ret = gb_audio_gb_set_pcm(module->mgmt_connection, data_cport,
format, rate, channels, sig_bits);
if (ret) {
dev_err_ratelimited(dai->dev, "%d: Error during set_pcm\n", ret);
goto func_exit;
}
if (state < GBAUDIO_CODEC_HWPARAMS) {
ret = gb_audio_apbridgea_set_config(data->connection, 0,
AUDIO_APBRIDGEA_PCM_FMT_16,
AUDIO_APBRIDGEA_PCM_RATE_48000,
6144000);
if (ret) {
dev_err_ratelimited(dai->dev,
"%d: Error during set_config\n", ret);
goto func_exit;
}
}
state = GBAUDIO_CODEC_HWPARAMS;
module->ctrlstate[substream->stream] = state;
dev_dbg(dai->dev, "%s: state:%d\n", module->name, state);
}
codec->stream[substream->stream].state = state;
codec->stream[substream->stream].format = format;
codec->stream[substream->stream].rate = rate;
codec->stream[substream->stream].channels = channels;
codec->stream[substream->stream].sig_bits = sig_bits;
func_exit:
mutex_unlock(&codec->lock);
return ret;
}
static int gbmodule_prepare_tx(struct gbaudio_module_info *module,
struct gbaudio_data_connection *data,
int codec_state, struct device *dev)
{
int ret;
uint16_t data_cport;
data_cport = data->connection->intf_cport_id;
ret = gb_audio_gb_set_tx_data_size(module->mgmt_connection, data_cport,
192);
if (ret) {
dev_err_ratelimited(dev, "%d:Error during set_tx_data_size, cport:%d\n",
ret, data_cport);
return ret;
}
if (codec_state < GBAUDIO_CODEC_PREPARE) {
ret = gb_audio_apbridgea_set_tx_data_size(data->connection, 0,
192);
if (ret) {
dev_err_ratelimited(dev,
"%d:Error during apbridgea set_tx_data_size, cport\n",
ret);
return ret;
}
}
ret = gb_audio_gb_activate_tx(module->mgmt_connection,
data_cport);
if (ret)
dev_err_ratelimited(dev, "%s:Error during activate stream,%d\n",
module->name, ret);
return ret;
}
static int gbmodule_prepare_rx(struct gbaudio_module_info *module,
struct gbaudio_data_connection *data,
int codec_state, struct device *dev)
{
int ret;
uint16_t data_cport;
data_cport = data->connection->intf_cport_id;
ret = gb_audio_gb_set_rx_data_size(module->mgmt_connection, data_cport,
192);
if (ret) {
dev_err_ratelimited(dev, "%d:Error during set_rx_data_size, cport:%d\n",
ret, data_cport);
return ret;
}
if (codec_state < GBAUDIO_CODEC_PREPARE) {
ret = gb_audio_apbridgea_set_rx_data_size(data->connection, 0,
192);
if (ret) {
dev_err_ratelimited(dev,
"%d:Error during apbridgea_set_rx_data_size\n",
ret);
return ret;
}
}
ret = gb_audio_gb_activate_rx(module->mgmt_connection,
data_cport);
if (ret)
dev_err_ratelimited(dev, "%s:Error during activate stream,%d\n",
module->name, ret);
return ret;
}
static int gbcodec_prepare(struct snd_pcm_substream *substream,
struct snd_soc_dai *dai)
{
int ret;
struct gbaudio_module_info *module;
int state;
struct gbaudio_data_connection *data;
struct gbaudio_codec_info *codec = dev_get_drvdata(dai->dev);
mutex_lock(&codec->lock);
if (list_empty(&codec->module_list)) {
dev_err(codec->dev, "No codec module available\n");
mutex_unlock(&codec->lock);
return -ENODEV;
}
state = codec->stream[substream->stream].state;
list_for_each_entry(module, &codec->module_list, list) {
/* find the dai */
data = find_data(module, dai->name);
if (!data) {
dev_err(dai->dev, "%s:%s DATA connection missing\n",
dai->name, module->name);
continue;
}
switch (substream->stream) {
case SNDRV_PCM_STREAM_PLAYBACK:
ret = gbmodule_prepare_tx(module, data, state,
dai->dev);
break;
case SNDRV_PCM_STREAM_CAPTURE:
ret = gbmodule_prepare_rx(module, data, state,
dai->dev);
break;
}
if (ret == -ENODEV)
continue;
if (ret) {
goto func_exit;
}
state = GBAUDIO_CODEC_PREPARE;
module->ctrlstate[substream->stream] = state;
dev_dbg(dai->dev, "%s: state:%d\n", module->name, state);
}
codec->stream[substream->stream].state = state;
func_exit:
mutex_unlock(&codec->lock);
return ret;
}
static int gbcodec_mute_stream(struct snd_soc_dai *dai, int mute, int stream)
{
int ret;
struct gbaudio_data_connection *data;
struct gbaudio_module_info *module;
struct gbaudio_codec_info *codec = dev_get_drvdata(dai->dev);
dev_dbg(dai->dev, "Mute:%d, Direction:%s\n", mute,
stream ? "CAPTURE":"PLAYBACK");
mutex_lock(&codec->lock);
if (list_empty(&codec->module_list)) {
dev_err(codec->dev, "No codec module available\n");
if (mute) {
codec->stream[stream].state = GBAUDIO_CODEC_STOP;
ret = 0;
} else {
ret = -ENODEV;
}
mutex_unlock(&codec->lock);
return ret;
}
list_for_each_entry(module, &codec->module_list, list) {
/* find the dai */
data = find_data(module, dai->name);
if (data)
break;
}
if (!data) {
dev_err(dai->dev, "%s:%s DATA connection missing\n",
dai->name, module->name);
ret = -ENODEV;
goto func_exit;
}
if (!mute && !stream) {/* start playback */
ret = gb_audio_apbridgea_prepare_tx(data->connection,
0);
if (!ret)
ret = gb_audio_apbridgea_start_tx(data->connection,
0, 0);
codec->stream[stream].state = GBAUDIO_CODEC_START;
} else if (!mute && stream) {/* start capture */
ret = gb_audio_apbridgea_prepare_rx(data->connection,
0);
if (!ret)
ret = gb_audio_apbridgea_start_rx(data->connection,
0);
codec->stream[stream].state = GBAUDIO_CODEC_START;
} else if (mute && !stream) {/* stop playback */
ret = gb_audio_apbridgea_stop_tx(data->connection, 0);
if (!ret)
ret = gb_audio_apbridgea_shutdown_tx(data->connection,
0);
codec->stream[stream].state = GBAUDIO_CODEC_STOP;
} else if (mute && stream) {/* stop capture */
ret = gb_audio_apbridgea_stop_rx(data->connection, 0);
if (!ret)
ret = gb_audio_apbridgea_shutdown_rx(data->connection,
0);
codec->stream[stream].state = GBAUDIO_CODEC_STOP;
} else
ret = -EINVAL;
if (ret)
dev_err_ratelimited(dai->dev,
"%s:Error during %s %s stream:%d\n",
module->name, mute ? "Mute" : "Unmute",
stream ? "Capture" : "Playback", ret);
func_exit:
mutex_unlock(&codec->lock);
return 0;
}
static struct snd_soc_dai_ops gbcodec_dai_ops = {
.startup = gbcodec_startup,
.shutdown = gbcodec_shutdown,
.hw_params = gbcodec_hw_params,
.prepare = gbcodec_prepare,
.mute_stream = gbcodec_mute_stream,
};
static int gbaudio_init_jack(struct gbaudio_module_info *module,
struct snd_soc_codec *codec)
{
int ret;
if (!module->num_jacks)
return 0;
/* register jack(s) in case any */
if (module->num_jacks > 1) {
dev_err(module->dev, "Currently supports max=1 jack\n");
return -EINVAL;
}
snprintf(module->jack_name, NAME_SIZE, "GB %d Headset Jack",
module->dev_id);
ret = snd_soc_jack_new(codec, module->jack_name, GBCODEC_JACK_MASK,
&module->headset_jack);
if (ret) {
dev_err(module->dev, "Failed to create new jack\n");
return ret;
}
snprintf(module->button_name, NAME_SIZE, "GB %d Button Jack",
module->dev_id);
ret = snd_soc_jack_new(codec, module->button_name,
GBCODEC_JACK_BUTTON_MASK, &module->button_jack);
if (ret) {
dev_err(module->dev, "Failed to create button jack\n");
return ret;
}
ret = snd_jack_set_key(module->button_jack.jack, SND_JACK_BTN_0,
KEY_MEDIA);
if (ret) {
dev_err(module->dev, "Failed to set BTN_0\n");
return ret;
}
ret = snd_jack_set_key(module->button_jack.jack, SND_JACK_BTN_1,
KEY_VOICECOMMAND);
if (ret) {
dev_err(module->dev, "Failed to set BTN_1\n");
return ret;
}
ret = snd_jack_set_key(module->button_jack.jack, SND_JACK_BTN_2,
KEY_VOLUMEUP);
if (ret) {
dev_err(module->dev, "Failed to set BTN_2\n");
return ret;
}
ret = snd_jack_set_key(module->button_jack.jack, SND_JACK_BTN_3,
KEY_VOLUMEDOWN);
if (ret) {
dev_err(module->dev, "Failed to set BTN_0\n");
return ret;
}
/* FIXME
* verify if this is really required
set_bit(INPUT_PROP_NO_DUMMY_RELEASE,
module->button_jack.jack->input_dev->propbit);
*/
return 0;
}
int gbaudio_register_module(struct gbaudio_module_info *module)
{
int ret;
struct snd_soc_codec *codec;
struct snd_card *card;
struct snd_soc_jack *jack = NULL;
if (!gbcodec) {
dev_err(module->dev, "GB Codec not yet probed\n");
return -EAGAIN;
}
codec = gbcodec->codec;
card = codec->card->snd_card;
down_write(&card->controls_rwsem);
if (module->num_dais) {
dev_err(gbcodec->dev,
"%d:DAIs not supported via gbcodec driver\n",
module->num_dais);
up_write(&card->controls_rwsem);
return -EINVAL;
}
ret = gbaudio_init_jack(module, codec);
if (ret) {
up_write(&card->controls_rwsem);
return ret;
}
if (module->dapm_widgets)
snd_soc_dapm_new_controls(&codec->dapm, module->dapm_widgets,
module->num_dapm_widgets);
if (module->controls)
snd_soc_add_codec_controls(codec, module->controls,
module->num_controls);
if (module->dapm_routes)
snd_soc_dapm_add_routes(&codec->dapm, module->dapm_routes,
module->num_dapm_routes);
/* card already instantiated, create widgets here only */
if (codec->card->instantiated) {
snd_soc_dapm_link_component_dai_widgets(codec->card,
&codec->dapm);
#ifdef CONFIG_SND_JACK
/* register jack devices for this module from codec->jack_list */
list_for_each_entry(jack, &codec->jack_list, list) {
if ((jack == &module->headset_jack)
|| (jack == &module->button_jack))
snd_device_register(codec->card->snd_card,
jack->jack);
}
#endif
}
mutex_lock(&gbcodec->lock);
list_add(&module->list, &gbcodec->module_list);
mutex_unlock(&gbcodec->lock);
if (codec->card->instantiated)
ret = snd_soc_dapm_new_widgets(&codec->dapm);
dev_dbg(codec->dev, "Registered %s module\n", module->name);
up_write(&card->controls_rwsem);
return ret;
}
EXPORT_SYMBOL(gbaudio_register_module);
static void gbaudio_codec_cleanup(struct gbaudio_module_info *module)
{
struct gbaudio_data_connection *data;
int pb_state = gbcodec->stream[0].state;
int cap_state = gbcodec->stream[1].state;
int ret;
uint16_t i2s_port, cportid;
/* locks already acquired */
if (!pb_state && !cap_state)
return;
dev_dbg(gbcodec->dev, "%s: removed, cleanup APBridge\n", module->name);
if (pb_state == GBAUDIO_CODEC_START) {
/* cleanup PB path, only APBridge specific */
data = find_data(module, gbcodec->stream[0].dai_name);
if (!data) {
dev_err(gbcodec->dev, "%s: Missing data pointer\n",
__func__);
return;
}
if (list_is_singular(&gbcodec->module_list)) {
ret = gb_audio_apbridgea_stop_tx(data->connection, 0);
if (ret)
return;
ret = gb_audio_apbridgea_shutdown_tx(data->connection,
0);
if (ret)
return;
}
i2s_port = 0; /* fixed for now */
cportid = data->connection->hd_cport_id;
ret = gb_audio_apbridgea_unregister_cport(data->connection,
i2s_port, cportid,
AUDIO_APBRIDGEA_DIRECTION_TX);
module->ctrlstate[0] = GBAUDIO_CODEC_SHUTDOWN;
}
if (cap_state == GBAUDIO_CODEC_START) {
/* cleanup CAP path, only APBridge specific */
data = find_data(module, gbcodec->stream[1].dai_name);
if (!data) {
dev_err(gbcodec->dev, "%s: Missing data pointer\n",
__func__);
return;
}
if (list_is_singular(&gbcodec->module_list)) {
ret = gb_audio_apbridgea_stop_rx(data->connection, 0);
if (ret)
return;
ret = gb_audio_apbridgea_shutdown_rx(data->connection,
0);
if (ret)
return;
}
i2s_port = 0; /* fixed for now */
cportid = data->connection->hd_cport_id;
ret = gb_audio_apbridgea_unregister_cport(data->connection,
i2s_port, cportid,
AUDIO_APBRIDGEA_DIRECTION_RX);
module->ctrlstate[1] = GBAUDIO_CODEC_SHUTDOWN;
}
}
void gbaudio_unregister_module(struct gbaudio_module_info *module)
{
struct snd_soc_codec *codec = gbcodec->codec;
struct snd_card *card = codec->card->snd_card;
struct snd_soc_jack *jack, *next_j;
dev_dbg(codec->dev, "Unregister %s module\n", module->name);
down_write(&card->controls_rwsem);
mutex_lock(&gbcodec->lock);
gbaudio_codec_cleanup(module);
list_del(&module->list);
dev_dbg(codec->dev, "Process Unregister %s module\n", module->name);
mutex_unlock(&gbcodec->lock);
#ifdef CONFIG_SND_JACK
/* free jack devices for this module from codec->jack_list */
list_for_each_entry_safe(jack, next_j, &codec->jack_list, list) {
if ((jack == &module->headset_jack)
|| (jack == &module->button_jack)) {
snd_device_free(codec->card->snd_card, jack->jack);
list_del(&jack->list);
}
}
#endif
if (module->dapm_routes) {
dev_dbg(codec->dev, "Removing %d routes\n",
module->num_dapm_routes);
snd_soc_dapm_del_routes(&codec->dapm, module->dapm_routes,
module->num_dapm_routes);
}
if (module->controls) {
dev_dbg(codec->dev, "Removing %d controls\n",
module->num_controls);
snd_soc_remove_codec_controls(codec, module->controls,
module->num_controls);
}
if (module->dapm_widgets) {
dev_dbg(codec->dev, "Removing %d widgets\n",
module->num_dapm_widgets);
snd_soc_dapm_free_controls(&codec->dapm, module->dapm_widgets,
module->num_dapm_widgets);
}
dev_dbg(codec->dev, "Unregistered %s module\n", module->name);
up_write(&card->controls_rwsem);
}
EXPORT_SYMBOL(gbaudio_unregister_module);
/*
* codec driver ops
*/
static int gbcodec_probe(struct snd_soc_codec *codec)
{
struct gbaudio_codec_info *info;
info = devm_kzalloc(codec->dev, sizeof(*info), GFP_KERNEL);
if (!info)
return -ENOMEM;
info->dev = codec->dev;
INIT_LIST_HEAD(&info->module_list);
mutex_init(&info->lock);
info->codec = codec;
snd_soc_codec_set_drvdata(codec, info);
gbcodec = info;
device_init_wakeup(codec->dev, 1);
return 0;
}
static int gbcodec_remove(struct snd_soc_codec *codec)
{
/* Empty function for now */
return 0;
}
static u8 gbcodec_reg[GBCODEC_REG_COUNT] = {
[GBCODEC_CTL_REG] = GBCODEC_CTL_REG_DEFAULT,
[GBCODEC_MUTE_REG] = GBCODEC_MUTE_REG_DEFAULT,
[GBCODEC_PB_LVOL_REG] = GBCODEC_PB_VOL_REG_DEFAULT,
[GBCODEC_PB_RVOL_REG] = GBCODEC_PB_VOL_REG_DEFAULT,
[GBCODEC_CAP_LVOL_REG] = GBCODEC_CAP_VOL_REG_DEFAULT,
[GBCODEC_CAP_RVOL_REG] = GBCODEC_CAP_VOL_REG_DEFAULT,
[GBCODEC_APB1_MUX_REG] = GBCODEC_APB1_MUX_REG_DEFAULT,
[GBCODEC_APB2_MUX_REG] = GBCODEC_APB2_MUX_REG_DEFAULT,
};
static int gbcodec_write(struct snd_soc_codec *codec, unsigned int reg,
unsigned int value)
{
int ret = 0;
if (reg == SND_SOC_NOPM)
return 0;
BUG_ON(reg >= GBCODEC_REG_COUNT);
gbcodec_reg[reg] = value;
dev_dbg(codec->dev, "reg[%d] = 0x%x\n", reg, value);
return ret;
}
static unsigned int gbcodec_read(struct snd_soc_codec *codec,
unsigned int reg)
{
unsigned int val = 0;
if (reg == SND_SOC_NOPM)
return 0;
BUG_ON(reg >= GBCODEC_REG_COUNT);
val = gbcodec_reg[reg];
dev_dbg(codec->dev, "reg[%d] = 0x%x\n", reg, val);
return val;
}
static struct snd_soc_dai_driver gbaudio_dai[] = {
{
.name = "greybus-apb1",
.id = 0,
.playback = {
.stream_name = "GB Audio Playback",
.rates = SNDRV_PCM_RATE_48000,
.formats = SNDRV_PCM_FORMAT_S16_LE,
.rate_max = 48000,
.rate_min = 48000,
.channels_min = 1,
.channels_max = 2,
},
.capture = {
.stream_name = "GB Audio Capture",
.rates = SNDRV_PCM_RATE_48000,
.formats = SNDRV_PCM_FORMAT_S16_LE,
.rate_max = 48000,
.rate_min = 48000,
.channels_min = 1,
.channels_max = 2,
},
.ops = &gbcodec_dai_ops,
},
};
static struct snd_soc_codec_driver soc_codec_dev_gbaudio = {
.probe = gbcodec_probe,
.remove = gbcodec_remove,
.read = gbcodec_read,
.write = gbcodec_write,
.reg_cache_size = GBCODEC_REG_COUNT,
.reg_cache_default = gbcodec_reg_defaults,
.reg_word_size = 1,
.idle_bias_off = true,
.ignore_pmdown_time = 1,
};
#ifdef CONFIG_PM
static int gbaudio_codec_suspend(struct device *dev)
{
dev_dbg(dev, "%s: suspend\n", __func__);
return 0;
}
static int gbaudio_codec_resume(struct device *dev)
{
dev_dbg(dev, "%s: resume\n", __func__);
return 0;
}
static const struct dev_pm_ops gbaudio_codec_pm_ops = {
.suspend = gbaudio_codec_suspend,
.resume = gbaudio_codec_resume,
};
#endif
static int gbaudio_codec_probe(struct platform_device *pdev)
{
return snd_soc_register_codec(&pdev->dev, &soc_codec_dev_gbaudio,
gbaudio_dai, ARRAY_SIZE(gbaudio_dai));
}
static int gbaudio_codec_remove(struct platform_device *pdev)
{
snd_soc_unregister_codec(&pdev->dev);
return 0;
}
static const struct of_device_id greybus_asoc_machine_of_match[] = {
{ .compatible = "qcom,ara-codec", },
{},
};
static struct platform_driver gbaudio_codec_driver = {
.driver = {
.name = "gb-codec",
.owner = THIS_MODULE,
#ifdef CONFIG_PM
.pm = &gbaudio_codec_pm_ops,
#endif
.of_match_table = greybus_asoc_machine_of_match,
},
.probe = gbaudio_codec_probe,
.remove = gbaudio_codec_remove,
};
module_platform_driver(gbaudio_codec_driver);
MODULE_DESCRIPTION("Greybus codec driver");
MODULE_AUTHOR("Vaibhav Agarwal <vaibhav.agarwal@linaro.org>");
MODULE_LICENSE("GPL v2");
MODULE_ALIAS("platform:gbaudio-codec");
|