summaryrefslogtreecommitdiff
path: root/sound/soc/mediatek/common/mtk-btcvsd.c
blob: 1b8bcdaf02d116cbc124aac6597f3a8a501b468d (plain)
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
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
// SPDX-License-Identifier: GPL-2.0
//
// Mediatek ALSA BT SCO CVSD/MSBC Driver
//
// Copyright (c) 2019 MediaTek Inc.
// Author: KaiChieh Chuang <kaichieh.chuang@mediatek.com>

#include <linux/mfd/syscon.h>
#include <linux/module.h>
#include <linux/of_address.h>
#include <linux/sched/clock.h>

#include <sound/soc.h>

#define BTCVSD_SND_NAME "mtk-btcvsd-snd"

#define BT_CVSD_TX_NREADY	BIT(21)
#define BT_CVSD_RX_READY	BIT(22)
#define BT_CVSD_TX_UNDERFLOW	BIT(23)
#define BT_CVSD_RX_OVERFLOW	BIT(24)
#define BT_CVSD_INTERRUPT	BIT(31)

#define BT_CVSD_CLEAR \
	(BT_CVSD_TX_NREADY | BT_CVSD_RX_READY | BT_CVSD_TX_UNDERFLOW |\
	 BT_CVSD_RX_OVERFLOW | BT_CVSD_INTERRUPT)

/* TX */
#define SCO_TX_ENCODE_SIZE (60)
/* 18 = 6 * 180 / SCO_TX_ENCODE_SIZE */
#define SCO_TX_PACKER_BUF_NUM (18)

/* RX */
#define SCO_RX_PLC_SIZE (30)
#define SCO_RX_PACKER_BUF_NUM (64)
#define SCO_RX_PACKET_MASK (0x3F)

#define SCO_CVSD_PACKET_VALID_SIZE 2

#define SCO_PACKET_120 120
#define SCO_PACKET_180 180

#define BTCVSD_RX_PACKET_SIZE (SCO_RX_PLC_SIZE + SCO_CVSD_PACKET_VALID_SIZE)
#define BTCVSD_TX_PACKET_SIZE (SCO_TX_ENCODE_SIZE)

#define BTCVSD_RX_BUF_SIZE (BTCVSD_RX_PACKET_SIZE * SCO_RX_PACKER_BUF_NUM)
#define BTCVSD_TX_BUF_SIZE (BTCVSD_TX_PACKET_SIZE * SCO_TX_PACKER_BUF_NUM)

enum bt_sco_state {
	BT_SCO_STATE_IDLE,
	BT_SCO_STATE_RUNNING,
	BT_SCO_STATE_ENDING,
};

enum bt_sco_direct {
	BT_SCO_DIRECT_BT2ARM,
	BT_SCO_DIRECT_ARM2BT,
};

enum bt_sco_packet_len {
	BT_SCO_CVSD_30 = 0,
	BT_SCO_CVSD_60,
	BT_SCO_CVSD_90,
	BT_SCO_CVSD_120,
	BT_SCO_CVSD_10,
	BT_SCO_CVSD_20,
	BT_SCO_CVSD_MAX,
};

enum BT_SCO_BAND {
	BT_SCO_NB,
	BT_SCO_WB,
};

struct mtk_btcvsd_snd_hw_info {
	unsigned int num_valid_addr;
	unsigned long bt_sram_addr[20];
	unsigned int packet_length;
	unsigned int packet_num;
};

struct mtk_btcvsd_snd_stream {
	struct snd_pcm_substream *substream;
	int stream;

	enum bt_sco_state state;

	unsigned int packet_size;
	unsigned int buf_size;
	u8 temp_packet_buf[SCO_PACKET_180];

	int packet_w;
	int packet_r;
	snd_pcm_uframes_t prev_frame;
	int prev_packet_idx;

	unsigned int xrun:1;
	unsigned int timeout:1;
	unsigned int mute:1;
	unsigned int trigger_start:1;
	unsigned int wait_flag:1;
	unsigned int rw_cnt;

	unsigned long long time_stamp;
	unsigned long long buf_data_equivalent_time;

	struct mtk_btcvsd_snd_hw_info buffer_info;
};

struct mtk_btcvsd_snd {
	struct device *dev;
	int irq_id;

	struct regmap *infra;
	void __iomem *bt_pkv_base;
	void __iomem *bt_sram_bank2_base;

	unsigned int infra_misc_offset;
	unsigned int conn_bt_cvsd_mask;
	unsigned int cvsd_mcu_read_offset;
	unsigned int cvsd_mcu_write_offset;
	unsigned int cvsd_packet_indicator;

	u32 *bt_reg_pkt_r;
	u32 *bt_reg_pkt_w;
	u32 *bt_reg_ctl;

	unsigned int irq_disabled:1;

	spinlock_t tx_lock;	/* spinlock for bt tx stream control */
	spinlock_t rx_lock;	/* spinlock for bt rx stream control */
	wait_queue_head_t tx_wait;
	wait_queue_head_t rx_wait;

	struct mtk_btcvsd_snd_stream *tx;
	struct mtk_btcvsd_snd_stream *rx;
	u8 tx_packet_buf[BTCVSD_TX_BUF_SIZE];
	u8 rx_packet_buf[BTCVSD_RX_BUF_SIZE];

	enum BT_SCO_BAND band;
};

struct mtk_btcvsd_snd_time_buffer_info {
	unsigned long long data_count_equi_time;
	unsigned long long time_stamp_us;
};

static const unsigned int btsco_packet_valid_mask[BT_SCO_CVSD_MAX][6] = {
	{0x1, 0x1 << 1, 0x1 << 2, 0x1 << 3, 0x1 << 4, 0x1 << 5},
	{0x1, 0x1, 0x2, 0x2, 0x4, 0x4},
	{0x1, 0x1, 0x1, 0x2, 0x2, 0x2},
	{0x1, 0x1, 0x1, 0x1, 0x0, 0x0},
	{0x7, 0x7 << 3, 0x7 << 6, 0x7 << 9, 0x7 << 12, 0x7 << 15},
	{0x3, 0x3 << 1, 0x3 << 3, 0x3 << 4, 0x3 << 6, 0x3 << 7},
};

static const unsigned int btsco_packet_info[BT_SCO_CVSD_MAX][4] = {
	{30, 6, SCO_PACKET_180 / SCO_TX_ENCODE_SIZE,
	 SCO_PACKET_180 / SCO_RX_PLC_SIZE},
	{60, 3, SCO_PACKET_180 / SCO_TX_ENCODE_SIZE,
	 SCO_PACKET_180 / SCO_RX_PLC_SIZE},
	{90, 2, SCO_PACKET_180 / SCO_TX_ENCODE_SIZE,
	 SCO_PACKET_180 / SCO_RX_PLC_SIZE},
	{120, 1, SCO_PACKET_120 / SCO_TX_ENCODE_SIZE,
	 SCO_PACKET_120 / SCO_RX_PLC_SIZE},
	{10, 18, SCO_PACKET_180 / SCO_TX_ENCODE_SIZE,
	 SCO_PACKET_180 / SCO_RX_PLC_SIZE},
	{20, 9, SCO_PACKET_180 / SCO_TX_ENCODE_SIZE,
	 SCO_PACKET_180 / SCO_RX_PLC_SIZE},
};

static const u8 table_msbc_silence[SCO_PACKET_180] = {
	0x01, 0x38, 0xad, 0x00, 0x00, 0xc5, 0x00, 0x00, 0x00, 0x00,
	0x77, 0x6d, 0xb6, 0xdd, 0xdb, 0x6d, 0xb7, 0x76, 0xdb, 0x6d,
	0xdd, 0xb6, 0xdb, 0x77, 0x6d, 0xb6, 0xdd, 0xdb, 0x6d, 0xb7,
	0x76, 0xdb, 0x6d, 0xdd, 0xb6, 0xdb, 0x77, 0x6d, 0xb6, 0xdd,
	0xdb, 0x6d, 0xb7, 0x76, 0xdb, 0x6d, 0xdd, 0xb6, 0xdb, 0x77,
	0x6d, 0xb6, 0xdd, 0xdb, 0x6d, 0xb7, 0x76, 0xdb, 0x6c, 0x00,
	0x01, 0xc8, 0xad, 0x00, 0x00, 0xc5, 0x00, 0x00, 0x00, 0x00,
	0x77, 0x6d, 0xb6, 0xdd, 0xdb, 0x6d, 0xb7, 0x76, 0xdb, 0x6d,
	0xdd, 0xb6, 0xdb, 0x77, 0x6d, 0xb6, 0xdd, 0xdb, 0x6d, 0xb7,
	0x76, 0xdb, 0x6d, 0xdd, 0xb6, 0xdb, 0x77, 0x6d, 0xb6, 0xdd,
	0xdb, 0x6d, 0xb7, 0x76, 0xdb, 0x6d, 0xdd, 0xb6, 0xdb, 0x77,
	0x6d, 0xb6, 0xdd, 0xdb, 0x6d, 0xb7, 0x76, 0xdb, 0x6c, 0x00,
	0x01, 0xf8, 0xad, 0x00, 0x00, 0xc5, 0x00, 0x00, 0x00, 0x00,
	0x77, 0x6d, 0xb6, 0xdd, 0xdb, 0x6d, 0xb7, 0x76, 0xdb, 0x6d,
	0xdd, 0xb6, 0xdb, 0x77, 0x6d, 0xb6, 0xdd, 0xdb, 0x6d, 0xb7,
	0x76, 0xdb, 0x6d, 0xdd, 0xb6, 0xdb, 0x77, 0x6d, 0xb6, 0xdd,
	0xdb, 0x6d, 0xb7, 0x76, 0xdb, 0x6d, 0xdd, 0xb6, 0xdb, 0x77,
	0x6d, 0xb6, 0xdd, 0xdb, 0x6d, 0xb7, 0x76, 0xdb, 0x6c, 0x00
};

static void mtk_btcvsd_snd_irq_enable(struct mtk_btcvsd_snd *bt)
{
	regmap_update_bits(bt->infra, bt->infra_misc_offset,
			   bt->conn_bt_cvsd_mask, bt->conn_bt_cvsd_mask);
}

static void mtk_btcvsd_snd_irq_disable(struct mtk_btcvsd_snd *bt)
{
	regmap_update_bits(bt->infra, bt->infra_misc_offset,
			   bt->conn_bt_cvsd_mask, 0);
}

static void mtk_btcvsd_snd_set_state(struct mtk_btcvsd_snd *bt,
				     struct mtk_btcvsd_snd_stream *bt_stream,
				     int state)
{
	dev_dbg(bt->dev, "%s(), stream %d, state %d, tx->state %d, rx->state %d, irq_disabled %d\n",
		__func__,
		bt_stream->stream, state,
		bt->tx->state, bt->rx->state, bt->irq_disabled);

	bt_stream->state = state;

	if (bt->tx->state == BT_SCO_STATE_IDLE &&
	    bt->rx->state == BT_SCO_STATE_IDLE) {
		if (!bt->irq_disabled) {
			disable_irq(bt->irq_id);
			mtk_btcvsd_snd_irq_disable(bt);
			bt->irq_disabled = 1;
		}
	} else {
		if (bt->irq_disabled) {
			enable_irq(bt->irq_id);
			mtk_btcvsd_snd_irq_enable(bt);
			bt->irq_disabled = 0;
		}
	}
}

static int mtk_btcvsd_snd_tx_init(struct mtk_btcvsd_snd *bt)
{
	memset(bt->tx, 0, sizeof(*bt->tx));
	memset(bt->tx_packet_buf, 0, sizeof(bt->tx_packet_buf));

	bt->tx->packet_size = BTCVSD_TX_PACKET_SIZE;
	bt->tx->buf_size = BTCVSD_TX_BUF_SIZE;
	bt->tx->timeout = 0;
	bt->tx->rw_cnt = 0;
	bt->tx->stream = SNDRV_PCM_STREAM_PLAYBACK;
	return 0;
}

static int mtk_btcvsd_snd_rx_init(struct mtk_btcvsd_snd *bt)
{
	memset(bt->rx, 0, sizeof(*bt->rx));
	memset(bt->rx_packet_buf, 0, sizeof(bt->rx_packet_buf));

	bt->rx->packet_size = BTCVSD_RX_PACKET_SIZE;
	bt->rx->buf_size = BTCVSD_RX_BUF_SIZE;
	bt->rx->timeout = 0;
	bt->rx->rw_cnt = 0;
	bt->rx->stream = SNDRV_PCM_STREAM_CAPTURE;
	return 0;
}

static void get_tx_time_stamp(struct mtk_btcvsd_snd *bt,
			      struct mtk_btcvsd_snd_time_buffer_info *ts)
{
	ts->time_stamp_us = bt->tx->time_stamp;
	ts->data_count_equi_time = bt->tx->buf_data_equivalent_time;
}

static void get_rx_time_stamp(struct mtk_btcvsd_snd *bt,
			      struct mtk_btcvsd_snd_time_buffer_info *ts)
{
	ts->time_stamp_us = bt->rx->time_stamp;
	ts->data_count_equi_time = bt->rx->buf_data_equivalent_time;
}

static int btcvsd_bytes_to_frame(struct snd_pcm_substream *substream,
				 int bytes)
{
	int count = bytes;
	struct snd_pcm_runtime *runtime = substream->runtime;

	if (runtime->format == SNDRV_PCM_FORMAT_S32_LE ||
	    runtime->format == SNDRV_PCM_FORMAT_U32_LE)
		count = count >> 2;
	else
		count = count >> 1;

	count = count / runtime->channels;
	return count;
}

static void mtk_btcvsd_snd_data_transfer(enum bt_sco_direct dir,
					 u8 *src, u8 *dst,
					 unsigned int blk_size,
					 unsigned int blk_num)
{
	unsigned int i, j;

	if (blk_size == 60 || blk_size == 120 || blk_size == 20) {
		u32 *src_32 = (u32 *)src;
		u32 *dst_32 = (u32 *)dst;

		for (i = 0; i < (blk_size * blk_num / 4); i++)
			*dst_32++ = *src_32++;
	} else {
		u16 *src_16 = (u16 *)src;
		u16 *dst_16 = (u16 *)dst;

		for (j = 0; j < blk_num; j++) {
			for (i = 0; i < (blk_size / 2); i++)
				*dst_16++ = *src_16++;

			if (dir == BT_SCO_DIRECT_BT2ARM)
				src_16++;
			else
				dst_16++;
		}
	}
}

/* write encoded mute data to bt sram */
static int btcvsd_tx_clean_buffer(struct mtk_btcvsd_snd *bt)
{
	unsigned int i;
	unsigned int num_valid_addr;
	unsigned long flags;
	enum BT_SCO_BAND band = bt->band;

	/* prepare encoded mute data */
	if (band == BT_SCO_NB)
		memset(bt->tx->temp_packet_buf, 170, SCO_PACKET_180);
	else
		memcpy(bt->tx->temp_packet_buf,
		       table_msbc_silence, SCO_PACKET_180);

	/* write mute data to bt tx sram buffer */
	spin_lock_irqsave(&bt->tx_lock, flags);
	num_valid_addr = bt->tx->buffer_info.num_valid_addr;

	dev_info(bt->dev, "%s(), band %d, num_valid_addr %u\n",
		 __func__, band, num_valid_addr);

	for (i = 0; i < num_valid_addr; i++) {
		void *dst;

		dev_info(bt->dev, "%s(), clean addr 0x%lx\n", __func__,
			 bt->tx->buffer_info.bt_sram_addr[i]);

		dst = (void *)bt->tx->buffer_info.bt_sram_addr[i];

		mtk_btcvsd_snd_data_transfer(BT_SCO_DIRECT_ARM2BT,
					     bt->tx->temp_packet_buf, dst,
					     bt->tx->buffer_info.packet_length,
					     bt->tx->buffer_info.packet_num);
	}
	spin_unlock_irqrestore(&bt->tx_lock, flags);

	return 0;
}

static int mtk_btcvsd_read_from_bt(struct mtk_btcvsd_snd *bt,
				   enum bt_sco_packet_len packet_type,
				   unsigned int packet_length,
				   unsigned int packet_num,
				   unsigned int blk_size,
				   unsigned int control)
{
	unsigned int i;
	int pv;
	u8 *src;
	unsigned int packet_buf_ofs;
	unsigned long flags;
	unsigned long connsys_addr_rx, ap_addr_rx;

	connsys_addr_rx = *bt->bt_reg_pkt_r;
	ap_addr_rx = (unsigned long)bt->bt_sram_bank2_base +
		     (connsys_addr_rx & 0xFFFF);

	if (connsys_addr_rx == 0xdeadfeed) {
		/* bt return 0xdeadfeed if read register during bt sleep */
		dev_warn(bt->dev, "%s(), connsys_addr_rx == 0xdeadfeed",
			 __func__);
		return -EIO;
	}

	src = (u8 *)ap_addr_rx;

	mtk_btcvsd_snd_data_transfer(BT_SCO_DIRECT_BT2ARM, src,
				     bt->rx->temp_packet_buf, packet_length,
				     packet_num);

	spin_lock_irqsave(&bt->rx_lock, flags);
	for (i = 0; i < blk_size; i++) {
		packet_buf_ofs = (bt->rx->packet_w & SCO_RX_PACKET_MASK) *
				 bt->rx->packet_size;
		memcpy(bt->rx_packet_buf + packet_buf_ofs,
		       bt->rx->temp_packet_buf + (SCO_RX_PLC_SIZE * i),
		       SCO_RX_PLC_SIZE);
		if ((control & btsco_packet_valid_mask[packet_type][i]) ==
		    btsco_packet_valid_mask[packet_type][i])
			pv = 1;
		else
			pv = 0;

		packet_buf_ofs += SCO_RX_PLC_SIZE;
		memcpy(bt->rx_packet_buf + packet_buf_ofs, (void *)&pv,
		       SCO_CVSD_PACKET_VALID_SIZE);
		bt->rx->packet_w++;
	}
	spin_unlock_irqrestore(&bt->rx_lock, flags);
	return 0;
}

int mtk_btcvsd_write_to_bt(struct mtk_btcvsd_snd *bt,
			   enum bt_sco_packet_len packet_type,
			   unsigned int packet_length,
			   unsigned int packet_num,
			   unsigned int blk_size)
{
	unsigned int i;
	unsigned long flags;
	u8 *dst;
	unsigned long connsys_addr_tx, ap_addr_tx;
	bool new_ap_addr_tx = true;

	connsys_addr_tx = *bt->bt_reg_pkt_w;
	ap_addr_tx = (unsigned long)bt->bt_sram_bank2_base +
		     (connsys_addr_tx & 0xFFFF);

	if (connsys_addr_tx == 0xdeadfeed) {
		/* bt return 0xdeadfeed if read register during bt sleep */
		dev_warn(bt->dev, "%s(), connsys_addr_tx == 0xdeadfeed\n",
			 __func__);
		return -EIO;
	}

	spin_lock_irqsave(&bt->tx_lock, flags);
	for (i = 0; i < blk_size; i++) {
		memcpy(bt->tx->temp_packet_buf + (bt->tx->packet_size * i),
		       (bt->tx_packet_buf +
			(bt->tx->packet_r % SCO_TX_PACKER_BUF_NUM) *
			bt->tx->packet_size),
		       bt->tx->packet_size);

		bt->tx->packet_r++;
	}
	spin_unlock_irqrestore(&bt->tx_lock, flags);

	dst = (u8 *)ap_addr_tx;

	if (!bt->tx->mute) {
		mtk_btcvsd_snd_data_transfer(BT_SCO_DIRECT_ARM2BT,
					     bt->tx->temp_packet_buf, dst,
					     packet_length, packet_num);
	}

	/* store bt tx buffer sram info */
	bt->tx->buffer_info.packet_length = packet_length;
	bt->tx->buffer_info.packet_num = packet_num;
	for (i = 0; i < bt->tx->buffer_info.num_valid_addr; i++) {
		if (bt->tx->buffer_info.bt_sram_addr[i] == ap_addr_tx) {
			new_ap_addr_tx = false;
			break;
		}
	}
	if (new_ap_addr_tx) {
		unsigned int next_idx;

		spin_lock_irqsave(&bt->tx_lock, flags);
		bt->tx->buffer_info.num_valid_addr++;
		next_idx = bt->tx->buffer_info.num_valid_addr - 1;
		bt->tx->buffer_info.bt_sram_addr[next_idx] = ap_addr_tx;
		spin_unlock_irqrestore(&bt->tx_lock, flags);
		dev_info(bt->dev, "%s(), new ap_addr_tx = 0x%lx, num_valid_addr %d\n",
			 __func__, ap_addr_tx,
			 bt->tx->buffer_info.num_valid_addr);
	}

	if (bt->tx->mute)
		btcvsd_tx_clean_buffer(bt);

	return 0;
}

static irqreturn_t mtk_btcvsd_snd_irq_handler(int irq_id, void *dev)
{
	struct mtk_btcvsd_snd *bt = dev;
	unsigned int packet_type, packet_num, packet_length;
	unsigned int buf_cnt_tx, buf_cnt_rx, control;

	if (bt->rx->state != BT_SCO_STATE_RUNNING &&
	    bt->rx->state != BT_SCO_STATE_ENDING &&
	    bt->tx->state != BT_SCO_STATE_RUNNING &&
	    bt->tx->state != BT_SCO_STATE_ENDING) {
		dev_warn(bt->dev, "%s(), in idle state: rx->state: %d, tx->state: %d\n",
			 __func__, bt->rx->state, bt->tx->state);
		goto irq_handler_exit;
	}

	control = *bt->bt_reg_ctl;
	packet_type = (control >> 18) & 0x7;

	if (((control >> 31) & 1) == 0) {
		dev_warn(bt->dev, "%s(), ((control >> 31) & 1) == 0, control 0x%x\n",
			 __func__, control);
		goto irq_handler_exit;
	}

	if (packet_type >= BT_SCO_CVSD_MAX) {
		dev_warn(bt->dev, "%s(), invalid packet_type %u, exit\n",
			 __func__, packet_type);
		goto irq_handler_exit;
	}

	packet_length = btsco_packet_info[packet_type][0];
	packet_num = btsco_packet_info[packet_type][1];
	buf_cnt_tx = btsco_packet_info[packet_type][2];
	buf_cnt_rx = btsco_packet_info[packet_type][3];

	if (bt->rx->state == BT_SCO_STATE_RUNNING ||
	    bt->rx->state == BT_SCO_STATE_ENDING) {
		if (bt->rx->xrun) {
			if (bt->rx->packet_w - bt->rx->packet_r <=
			    SCO_RX_PACKER_BUF_NUM - 2 * buf_cnt_rx) {
				/*
				 * free space is larger then
				 * twice interrupt rx data size
				 */
				bt->rx->xrun = 0;
				dev_warn(bt->dev, "%s(), rx->xrun 0!\n",
					 __func__);
			}
		}

		if (!bt->rx->xrun &&
		    (bt->rx->packet_w - bt->rx->packet_r <=
		     SCO_RX_PACKER_BUF_NUM - buf_cnt_rx)) {
			mtk_btcvsd_read_from_bt(bt,
						packet_type,
						packet_length,
						packet_num,
						buf_cnt_rx,
						control);
			bt->rx->rw_cnt++;
		} else {
			bt->rx->xrun = 1;
			dev_warn(bt->dev, "%s(), rx->xrun 1\n", __func__);
		}
	}

	/* tx */
	bt->tx->timeout = 0;
	if ((bt->tx->state == BT_SCO_STATE_RUNNING ||
	     bt->tx->state == BT_SCO_STATE_ENDING) &&
	    bt->tx->trigger_start) {
		if (bt->tx->xrun) {
			/* prepared data is larger then twice
			 * interrupt tx data size
			 */
			if (bt->tx->packet_w - bt->tx->packet_r >=
			    2 * buf_cnt_tx) {
				bt->tx->xrun = 0;
				dev_warn(bt->dev, "%s(), tx->xrun 0\n",
					 __func__);
			}
		}

		if ((!bt->tx->xrun &&
		     (bt->tx->packet_w - bt->tx->packet_r >= buf_cnt_tx)) ||
		    bt->tx->state == BT_SCO_STATE_ENDING) {
			mtk_btcvsd_write_to_bt(bt,
					       packet_type,
					       packet_length,
					       packet_num,
					       buf_cnt_tx);
			bt->tx->rw_cnt++;
		} else {
			bt->tx->xrun = 1;
			dev_warn(bt->dev, "%s(), tx->xrun 1\n", __func__);
		}
	}

	*bt->bt_reg_ctl &= ~BT_CVSD_CLEAR;

	if (bt->rx->state == BT_SCO_STATE_RUNNING ||
	    bt->rx->state == BT_SCO_STATE_ENDING) {
		bt->rx->wait_flag = 1;
		wake_up_interruptible(&bt->rx_wait);
		snd_pcm_period_elapsed(bt->rx->substream);
	}
	if (bt->tx->state == BT_SCO_STATE_RUNNING ||
	    bt->tx->state == BT_SCO_STATE_ENDING) {
		bt->tx->wait_flag = 1;
		wake_up_interruptible(&bt->tx_wait);
		snd_pcm_period_elapsed(bt->tx->substream);
	}

	return IRQ_HANDLED;
irq_handler_exit:
	*bt->bt_reg_ctl &= ~BT_CVSD_CLEAR;
	return IRQ_HANDLED;
}

static int wait_for_bt_irq(struct mtk_btcvsd_snd *bt,
			   struct mtk_btcvsd_snd_stream *bt_stream)
{
	unsigned long long t1, t2;
	/* one interrupt period = 22.5ms */
	unsigned long long timeout_limit = 22500000;
	int max_timeout_trial = 2;
	int ret;

	bt_stream->wait_flag = 0;

	while (max_timeout_trial && !bt_stream->wait_flag) {
		t1 = sched_clock();
		if (bt_stream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
			ret = wait_event_interruptible_timeout(bt->tx_wait,
				bt_stream->wait_flag,
				nsecs_to_jiffies(timeout_limit));
		} else {
			ret = wait_event_interruptible_timeout(bt->rx_wait,
				bt_stream->wait_flag,
				nsecs_to_jiffies(timeout_limit));
		}

		t2 = sched_clock();
		t2 = t2 - t1; /* in ns (10^9) */

		if (t2 > timeout_limit) {
			dev_warn(bt->dev, "%s(), stream %d, timeout %llu, limit %llu, ret %d, flag %d\n",
				 __func__, bt_stream->stream,
				 t2, timeout_limit, ret,
				 bt_stream->wait_flag);
		}

		if (ret < 0) {
			/*
			 * error, -ERESTARTSYS if it was interrupted by
			 * a signal
			 */
			dev_warn(bt->dev, "%s(), stream %d, error, trial left %d\n",
				 __func__,
				 bt_stream->stream, max_timeout_trial);

			bt_stream->timeout = 1;
			return ret;
		} else if (ret == 0) {
			/* conidtion is false after timeout */
			max_timeout_trial--;
			dev_warn(bt->dev, "%s(), stream %d, error, timeout, condition is false, trial left %d\n",
				 __func__,
				 bt_stream->stream, max_timeout_trial);

			if (max_timeout_trial <= 0) {
				bt_stream->timeout = 1;
				return -ETIME;
			}
		}
	}

	return 0;
}

ssize_t mtk_btcvsd_snd_read(struct mtk_btcvsd_snd *bt,
			    char __user *buf,
			    size_t count)
{
	ssize_t read_size = 0, read_count = 0, cur_read_idx, cont;
	unsigned int cur_buf_ofs = 0;
	unsigned long avail;
	unsigned long flags;
	unsigned int packet_size = bt->rx->packet_size;

	while (count) {
		spin_lock_irqsave(&bt->rx_lock, flags);
		/* available data in RX packet buffer */
		avail = (bt->rx->packet_w - bt->rx->packet_r) * packet_size;

		cur_read_idx = (bt->rx->packet_r & SCO_RX_PACKET_MASK) *
			       packet_size;
		spin_unlock_irqrestore(&bt->rx_lock, flags);

		if (!avail) {
			int ret = wait_for_bt_irq(bt, bt->rx);

			if (ret)
				return read_count;

			continue;
		}

		/* count must be multiple of packet_size */
		if (count % packet_size != 0 ||
		    avail % packet_size != 0) {
			dev_warn(bt->dev, "%s(), count %zu or d %lu is not multiple of packet_size %dd\n",
				 __func__, count, avail, packet_size);

			count -= count % packet_size;
			avail -= avail % packet_size;
		}

		if (count > avail)
			read_size = avail;
		else
			read_size = count;

		/* calculate continue space */
		cont = bt->rx->buf_size - cur_read_idx;
		if (read_size > cont)
			read_size = cont;

		if (copy_to_user(buf + cur_buf_ofs,
				 bt->rx_packet_buf + cur_read_idx,
				 read_size)) {
			dev_warn(bt->dev, "%s(), copy_to_user fail\n",
				 __func__);
			return -EFAULT;
		}

		spin_lock_irqsave(&bt->rx_lock, flags);
		bt->rx->packet_r += read_size / packet_size;
		spin_unlock_irqrestore(&bt->rx_lock, flags);

		read_count += read_size;
		cur_buf_ofs += read_size;
		count -= read_size;
	}

	/*
	 * save current timestamp & buffer time in times_tamp and
	 * buf_data_equivalent_time
	 */
	bt->rx->time_stamp = sched_clock();
	bt->rx->buf_data_equivalent_time =
		(unsigned long long)(bt->rx->packet_w - bt->rx->packet_r) *
		SCO_RX_PLC_SIZE * 16 * 1000 / 2 / 64;
	bt->rx->buf_data_equivalent_time += read_count * SCO_RX_PLC_SIZE *
					    16 * 1000 / packet_size / 2 / 64;
	/* return equivalent time(us) to data count */
	bt->rx->buf_data_equivalent_time *= 1000;

	return read_count;
}

ssize_t mtk_btcvsd_snd_write(struct mtk_btcvsd_snd *bt,
			     char __user *buf,
			     size_t count)
{
	int written_size = count, avail = 0, cur_write_idx, write_size, cont;
	unsigned int cur_buf_ofs = 0;
	unsigned long flags;
	unsigned int packet_size = bt->tx->packet_size;

	/*
	 * save current timestamp & buffer time in time_stamp and
	 * buf_data_equivalent_time
	 */
	bt->tx->time_stamp = sched_clock();
	bt->tx->buf_data_equivalent_time =
		(unsigned long long)(bt->tx->packet_w - bt->tx->packet_r) *
		packet_size * 16 * 1000 / 2 / 64;

	/* return equivalent time(us) to data count */
	bt->tx->buf_data_equivalent_time *= 1000;

	while (count) {
		spin_lock_irqsave(&bt->tx_lock, flags);
		/* free space of TX packet buffer */
		avail = bt->tx->buf_size -
			(bt->tx->packet_w - bt->tx->packet_r) * packet_size;

		cur_write_idx = (bt->tx->packet_w % SCO_TX_PACKER_BUF_NUM) *
				packet_size;
		spin_unlock_irqrestore(&bt->tx_lock, flags);

		if (!avail) {
			int ret = wait_for_bt_irq(bt, bt->rx);

			if (ret)
				return written_size;

			continue;
		}

		/* count must be multiple of bt->tx->packet_size */
		if (count % packet_size != 0 ||
		    avail % packet_size != 0) {
			dev_warn(bt->dev, "%s(), count %zu or avail %d is not multiple of packet_size %d\n",
				 __func__, count, avail, packet_size);
			count -= count % packet_size;
			avail -= avail % packet_size;
		}

		if (count > avail)
			write_size = avail;
		else
			write_size = count;

		/* calculate continue space */
		cont = bt->tx->buf_size - cur_write_idx;
		if (write_size > cont)
			write_size = cont;

		if (copy_from_user(bt->tx_packet_buf +
				   cur_write_idx,
				   buf + cur_buf_ofs,
				   write_size)) {
			dev_warn(bt->dev, "%s(), copy_from_user fail\n",
				 __func__);
			return -EFAULT;
		}

		spin_lock_irqsave(&bt->tx_lock, flags);
		bt->tx->packet_w += write_size / packet_size;
		spin_unlock_irqrestore(&bt->tx_lock, flags);
		cur_buf_ofs += write_size;
		count -= write_size;
	}

	return written_size;
}

static struct mtk_btcvsd_snd_stream *get_bt_stream
	(struct mtk_btcvsd_snd *bt, struct snd_pcm_substream *substream)
{
	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
		return bt->tx;
	else
		return bt->rx;
}

/* pcm ops */
static const struct snd_pcm_hardware mtk_btcvsd_hardware = {
	.info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
		 SNDRV_PCM_INFO_RESUME),
	.formats = SNDRV_PCM_FMTBIT_S16_LE,
	.buffer_bytes_max = 24 * 1024,
	.period_bytes_max = 24 * 1024,
	.periods_min = 2,
	.periods_max = 16,
	.fifo_size = 0,
};

static int mtk_pcm_btcvsd_open(struct snd_pcm_substream *substream)
{
	struct snd_soc_pcm_runtime *rtd = substream->private_data;
	struct snd_soc_component *component =
		snd_soc_rtdcom_lookup(rtd, BTCVSD_SND_NAME);
	struct mtk_btcvsd_snd *bt = snd_soc_component_get_drvdata(component);
	int ret;

	dev_dbg(bt->dev, "%s(), stream %d, substream %p\n",
		__func__, substream->stream, substream);

	snd_soc_set_runtime_hwparams(substream, &mtk_btcvsd_hardware);

	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
		ret = mtk_btcvsd_snd_tx_init(bt);
		bt->tx->substream = substream;
	} else {
		ret = mtk_btcvsd_snd_rx_init(bt);
		bt->rx->substream = substream;
	}

	return ret;
}

static int mtk_pcm_btcvsd_close(struct snd_pcm_substream *substream)
{
	struct snd_soc_pcm_runtime *rtd = substream->private_data;
	struct snd_soc_component *component =
		snd_soc_rtdcom_lookup(rtd, BTCVSD_SND_NAME);
	struct mtk_btcvsd_snd *bt = snd_soc_component_get_drvdata(component);
	struct mtk_btcvsd_snd_stream *bt_stream = get_bt_stream(bt, substream);

	dev_dbg(bt->dev, "%s(), stream %d\n", __func__, substream->stream);

	mtk_btcvsd_snd_set_state(bt, bt_stream, BT_SCO_STATE_IDLE);
	bt_stream->substream = NULL;
	return 0;
}

static int mtk_pcm_btcvsd_hw_params(struct snd_pcm_substream *substream,
				    struct snd_pcm_hw_params *hw_params)
{
	struct snd_soc_pcm_runtime *rtd = substream->private_data;
	struct snd_soc_component *component =
		snd_soc_rtdcom_lookup(rtd, BTCVSD_SND_NAME);
	struct mtk_btcvsd_snd *bt = snd_soc_component_get_drvdata(component);

	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
	    params_buffer_bytes(hw_params) % bt->tx->packet_size != 0) {
		dev_warn(bt->dev, "%s(), error, buffer size %d not valid\n",
			 __func__,
			 params_buffer_bytes(hw_params));
		return -EINVAL;
	}

	substream->runtime->dma_bytes = params_buffer_bytes(hw_params);
	return 0;
}

static int mtk_pcm_btcvsd_hw_free(struct snd_pcm_substream *substream)
{
	struct snd_soc_pcm_runtime *rtd = substream->private_data;
	struct snd_soc_component *component =
		snd_soc_rtdcom_lookup(rtd, BTCVSD_SND_NAME);
	struct mtk_btcvsd_snd *bt = snd_soc_component_get_drvdata(component);

	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
		btcvsd_tx_clean_buffer(bt);

	return 0;
}

static int mtk_pcm_btcvsd_prepare(struct snd_pcm_substream *substream)
{
	struct snd_soc_pcm_runtime *rtd = substream->private_data;
	struct snd_soc_component *component =
		snd_soc_rtdcom_lookup(rtd, BTCVSD_SND_NAME);
	struct mtk_btcvsd_snd *bt = snd_soc_component_get_drvdata(component);
	struct mtk_btcvsd_snd_stream *bt_stream = get_bt_stream(bt, substream);

	dev_dbg(bt->dev, "%s(), stream %d\n", __func__, substream->stream);

	mtk_btcvsd_snd_set_state(bt, bt_stream, BT_SCO_STATE_RUNNING);
	return 0;
}

static int mtk_pcm_btcvsd_trigger(struct snd_pcm_substream *substream, int cmd)
{
	struct snd_soc_pcm_runtime *rtd = substream->private_data;
	struct snd_soc_component *component =
		snd_soc_rtdcom_lookup(rtd, BTCVSD_SND_NAME);
	struct mtk_btcvsd_snd *bt = snd_soc_component_get_drvdata(component);
	struct mtk_btcvsd_snd_stream *bt_stream = get_bt_stream(bt, substream);
	int stream = substream->stream;
	int hw_packet_ptr;

	dev_dbg(bt->dev, "%s(), stream %d, cmd %d\n",
		__func__, substream->stream, cmd);

	switch (cmd) {
	case SNDRV_PCM_TRIGGER_START:
	case SNDRV_PCM_TRIGGER_RESUME:
		hw_packet_ptr = stream == SNDRV_PCM_STREAM_PLAYBACK ?
				bt_stream->packet_r : bt_stream->packet_w;
		bt_stream->prev_packet_idx = hw_packet_ptr;
		bt_stream->prev_frame = 0;
		bt_stream->trigger_start = 1;
		return 0;
	case SNDRV_PCM_TRIGGER_STOP:
	case SNDRV_PCM_TRIGGER_SUSPEND:
		bt_stream->trigger_start = 0;
		mtk_btcvsd_snd_set_state(bt, bt_stream, BT_SCO_STATE_ENDING);
		return 0;
	default:
		return -EINVAL;
	}
}

static snd_pcm_uframes_t mtk_pcm_btcvsd_pointer
	(struct snd_pcm_substream *substream)
{
	struct snd_soc_pcm_runtime *rtd = substream->private_data;
	struct snd_soc_component *component =
		snd_soc_rtdcom_lookup(rtd, BTCVSD_SND_NAME);
	struct mtk_btcvsd_snd *bt = snd_soc_component_get_drvdata(component);
	struct mtk_btcvsd_snd_stream *bt_stream;
	snd_pcm_uframes_t frame = 0;
	int byte = 0;
	int hw_packet_ptr;
	int packet_diff;
	spinlock_t *lock;	/* spinlock for bt stream control */
	unsigned long flags;

	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
		lock = &bt->tx_lock;
		bt_stream = bt->tx;
	} else {
		lock = &bt->rx_lock;
		bt_stream = bt->rx;
	}

	spin_lock_irqsave(lock, flags);
	hw_packet_ptr = substream->stream == SNDRV_PCM_STREAM_PLAYBACK ?
			bt->tx->packet_r : bt->rx->packet_w;

	/* get packet diff from last time */
	if (hw_packet_ptr >= bt_stream->prev_packet_idx) {
		packet_diff = hw_packet_ptr - bt_stream->prev_packet_idx;
	} else {
		/* integer overflow */
		packet_diff = (INT_MAX - bt_stream->prev_packet_idx) +
			      (hw_packet_ptr - INT_MIN) + 1;
	}
	bt_stream->prev_packet_idx = hw_packet_ptr;

	/* increased bytes */
	byte = packet_diff * bt_stream->packet_size;

	frame = btcvsd_bytes_to_frame(substream, byte);
	frame += bt_stream->prev_frame;
	frame %= substream->runtime->buffer_size;

	bt_stream->prev_frame = frame;

	spin_unlock_irqrestore(lock, flags);

	return frame;
}

static int mtk_pcm_btcvsd_copy(struct snd_pcm_substream *substream,
			       int channel, unsigned long pos,
			       void __user *buf, unsigned long count)
{
	struct snd_soc_pcm_runtime *rtd = substream->private_data;
	struct snd_soc_component *component =
		snd_soc_rtdcom_lookup(rtd, BTCVSD_SND_NAME);
	struct mtk_btcvsd_snd *bt = snd_soc_component_get_drvdata(component);

	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
		mtk_btcvsd_snd_write(bt, buf, count);
	else
		mtk_btcvsd_snd_read(bt, buf, count);

	return 0;
}

static struct snd_pcm_ops mtk_btcvsd_ops = {
	.open = mtk_pcm_btcvsd_open,
	.close = mtk_pcm_btcvsd_close,
	.ioctl = snd_pcm_lib_ioctl,
	.hw_params = mtk_pcm_btcvsd_hw_params,
	.hw_free = mtk_pcm_btcvsd_hw_free,
	.prepare = mtk_pcm_btcvsd_prepare,
	.trigger = mtk_pcm_btcvsd_trigger,
	.pointer = mtk_pcm_btcvsd_pointer,
	.copy_user = mtk_pcm_btcvsd_copy,
};

/* kcontrol */
static const char *const btsco_band_str[] = {"NB", "WB"};

static const struct soc_enum btcvsd_enum[] = {
	SOC_ENUM_SINGLE_EXT(ARRAY_SIZE(btsco_band_str), btsco_band_str),
};

static int btcvsd_band_get(struct snd_kcontrol *kcontrol,
			   struct snd_ctl_elem_value *ucontrol)
{
	struct snd_soc_component *cmpnt = snd_soc_kcontrol_component(kcontrol);
	struct mtk_btcvsd_snd *bt = snd_soc_component_get_drvdata(cmpnt);

	ucontrol->value.integer.value[0] = bt->band;
	return 0;
}

static int btcvsd_band_set(struct snd_kcontrol *kcontrol,
			   struct snd_ctl_elem_value *ucontrol)
{
	struct snd_soc_component *cmpnt = snd_soc_kcontrol_component(kcontrol);
	struct mtk_btcvsd_snd *bt = snd_soc_component_get_drvdata(cmpnt);
	struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;

	if (ucontrol->value.enumerated.item[0] >= e->items)
		return -EINVAL;

	bt->band = ucontrol->value.integer.value[0];
	dev_dbg(bt->dev, "%s(), band %d\n", __func__, bt->band);
	return 0;
}

static int btcvsd_tx_mute_get(struct snd_kcontrol *kcontrol,
			      struct snd_ctl_elem_value *ucontrol)
{
	struct snd_soc_component *cmpnt = snd_soc_kcontrol_component(kcontrol);
	struct mtk_btcvsd_snd *bt = snd_soc_component_get_drvdata(cmpnt);

	if (!bt->tx) {
		ucontrol->value.integer.value[0] = 0;
		return 0;
	}

	ucontrol->value.integer.value[0] = bt->tx->mute;
	return 0;
}

static int btcvsd_tx_mute_set(struct snd_kcontrol *kcontrol,
			      struct snd_ctl_elem_value *ucontrol)
{
	struct snd_soc_component *cmpnt = snd_soc_kcontrol_component(kcontrol);
	struct mtk_btcvsd_snd *bt = snd_soc_component_get_drvdata(cmpnt);

	if (!bt->tx)
		return 0;

	bt->tx->mute = ucontrol->value.integer.value[0];
	return 0;
}

static int btcvsd_rx_irq_received_get(struct snd_kcontrol *kcontrol,
				      struct snd_ctl_elem_value *ucontrol)
{
	struct snd_soc_component *cmpnt = snd_soc_kcontrol_component(kcontrol);
	struct mtk_btcvsd_snd *bt = snd_soc_component_get_drvdata(cmpnt);

	if (!bt->rx)
		return 0;

	ucontrol->value.integer.value[0] = bt->rx->rw_cnt ? 1 : 0;
	return 0;
}

static int btcvsd_rx_timeout_get(struct snd_kcontrol *kcontrol,
				 struct snd_ctl_elem_value *ucontrol)
{
	struct snd_soc_component *cmpnt = snd_soc_kcontrol_component(kcontrol);
	struct mtk_btcvsd_snd *bt = snd_soc_component_get_drvdata(cmpnt);

	if (!bt->rx)
		return 0;

	ucontrol->value.integer.value[0] = bt->rx->timeout;
	bt->rx->timeout = 0;
	return 0;
}

static int btcvsd_rx_timestamp_get(struct snd_kcontrol *kcontrol,
				   unsigned int __user *data, unsigned int size)
{
	struct snd_soc_component *cmpnt = snd_soc_kcontrol_component(kcontrol);
	struct mtk_btcvsd_snd *bt = snd_soc_component_get_drvdata(cmpnt);
	int ret = 0;
	struct mtk_btcvsd_snd_time_buffer_info time_buffer_info_rx;

	if (size > sizeof(struct mtk_btcvsd_snd_time_buffer_info))
		return -EINVAL;

	get_rx_time_stamp(bt, &time_buffer_info_rx);

	dev_dbg(bt->dev, "%s(), time_stamp_us %llu, data_count_equi_time %llu",
		__func__,
		time_buffer_info_rx.time_stamp_us,
		time_buffer_info_rx.data_count_equi_time);

	if (copy_to_user(data, &time_buffer_info_rx,
			 sizeof(struct mtk_btcvsd_snd_time_buffer_info))) {
		dev_warn(bt->dev, "%s(), copy_to_user fail", __func__);
		ret = -EFAULT;
	}

	return ret;
}

static int btcvsd_tx_irq_received_get(struct snd_kcontrol *kcontrol,
				      struct snd_ctl_elem_value *ucontrol)
{
	struct snd_soc_component *cmpnt = snd_soc_kcontrol_component(kcontrol);
	struct mtk_btcvsd_snd *bt = snd_soc_component_get_drvdata(cmpnt);

	if (!bt->tx)
		return 0;

	ucontrol->value.integer.value[0] = bt->tx->rw_cnt ? 1 : 0;
	return 0;
}

static int btcvsd_tx_timeout_get(struct snd_kcontrol *kcontrol,
				 struct snd_ctl_elem_value *ucontrol)
{
	struct snd_soc_component *cmpnt = snd_soc_kcontrol_component(kcontrol);
	struct mtk_btcvsd_snd *bt = snd_soc_component_get_drvdata(cmpnt);

	ucontrol->value.integer.value[0] = bt->tx->timeout;
	return 0;
}

static int btcvsd_tx_timestamp_get(struct snd_kcontrol *kcontrol,
				   unsigned int __user *data, unsigned int size)
{
	struct snd_soc_component *cmpnt = snd_soc_kcontrol_component(kcontrol);
	struct mtk_btcvsd_snd *bt = snd_soc_component_get_drvdata(cmpnt);
	int ret = 0;
	struct mtk_btcvsd_snd_time_buffer_info time_buffer_info_tx;

	if (size > sizeof(struct mtk_btcvsd_snd_time_buffer_info))
		return -EINVAL;

	get_tx_time_stamp(bt, &time_buffer_info_tx);

	dev_dbg(bt->dev, "%s(), time_stamp_us %llu, data_count_equi_time %llu",
		__func__,
		time_buffer_info_tx.time_stamp_us,
		time_buffer_info_tx.data_count_equi_time);

	if (copy_to_user(data, &time_buffer_info_tx,
			 sizeof(struct mtk_btcvsd_snd_time_buffer_info))) {
		dev_warn(bt->dev, "%s(), copy_to_user fail", __func__);
		ret = -EFAULT;
	}

	return ret;
}

static const struct snd_kcontrol_new mtk_btcvsd_snd_controls[] = {
	SOC_ENUM_EXT("BTCVSD Band", btcvsd_enum[0],
		     btcvsd_band_get, btcvsd_band_set),
	SOC_SINGLE_BOOL_EXT("BTCVSD Tx Mute Switch", 0,
			    btcvsd_tx_mute_get, btcvsd_tx_mute_set),
	SOC_SINGLE_BOOL_EXT("BTCVSD Tx Irq Received Switch", 0,
			    btcvsd_tx_irq_received_get, NULL),
	SOC_SINGLE_BOOL_EXT("BTCVSD Tx Timeout Switch", 0,
			    btcvsd_tx_timeout_get, NULL),
	SOC_SINGLE_BOOL_EXT("BTCVSD Rx Irq Received Switch", 0,
			    btcvsd_rx_irq_received_get, NULL),
	SOC_SINGLE_BOOL_EXT("BTCVSD Rx Timeout Switch", 0,
			    btcvsd_rx_timeout_get, NULL),
	SND_SOC_BYTES_TLV("BTCVSD Rx Timestamp",
			  sizeof(struct mtk_btcvsd_snd_time_buffer_info),
			  btcvsd_rx_timestamp_get, NULL),
	SND_SOC_BYTES_TLV("BTCVSD Tx Timestamp",
			  sizeof(struct mtk_btcvsd_snd_time_buffer_info),
			  btcvsd_tx_timestamp_get, NULL),
};

static int mtk_btcvsd_snd_component_probe(struct snd_soc_component *component)
{
	return snd_soc_add_component_controls(component,
		mtk_btcvsd_snd_controls,
		ARRAY_SIZE(mtk_btcvsd_snd_controls));
}

static const struct snd_soc_component_driver mtk_btcvsd_snd_platform = {
	.name = BTCVSD_SND_NAME,
	.ops = &mtk_btcvsd_ops,
	.probe = mtk_btcvsd_snd_component_probe,
};

static int mtk_btcvsd_snd_probe(struct platform_device *pdev)
{
	int ret = 0;
	int irq_id;
	u32 offset[5] = {0, 0, 0, 0, 0};
	struct mtk_btcvsd_snd *btcvsd;
	struct device *dev = &pdev->dev;

	/* init btcvsd private data */
	btcvsd = devm_kzalloc(dev, sizeof(*btcvsd), GFP_KERNEL);
	if (!btcvsd)
		return -ENOMEM;
	platform_set_drvdata(pdev, btcvsd);
	btcvsd->dev = dev;

	/* init tx/rx */
	btcvsd->rx = devm_kzalloc(btcvsd->dev, sizeof(*btcvsd->rx), GFP_KERNEL);
	if (!btcvsd->rx)
		return -ENOMEM;

	btcvsd->tx = devm_kzalloc(btcvsd->dev, sizeof(*btcvsd->tx), GFP_KERNEL);
	if (!btcvsd->tx)
		return -ENOMEM;

	spin_lock_init(&btcvsd->tx_lock);
	spin_lock_init(&btcvsd->rx_lock);

	init_waitqueue_head(&btcvsd->tx_wait);
	init_waitqueue_head(&btcvsd->rx_wait);

	mtk_btcvsd_snd_tx_init(btcvsd);
	mtk_btcvsd_snd_rx_init(btcvsd);

	/* irq */
	irq_id = platform_get_irq(pdev, 0);
	if (irq_id <= 0) {
		dev_err(dev, "%pOFn no irq found\n", dev->of_node);
		return irq_id < 0 ? irq_id : -ENXIO;
	}

	ret = devm_request_irq(dev, irq_id, mtk_btcvsd_snd_irq_handler,
			       IRQF_TRIGGER_LOW, "BTCVSD_ISR_Handle",
			       (void *)btcvsd);
	if (ret) {
		dev_err(dev, "could not request_irq for BTCVSD_ISR_Handle\n");
		return ret;
	}

	btcvsd->irq_id = irq_id;

	/* iomap */
	btcvsd->bt_pkv_base = of_iomap(dev->of_node, 0);
	if (!btcvsd->bt_pkv_base) {
		dev_err(dev, "iomap bt_pkv_base fail\n");
		return -EIO;
	}

	btcvsd->bt_sram_bank2_base = of_iomap(dev->of_node, 1);
	if (!btcvsd->bt_sram_bank2_base) {
		dev_err(dev, "iomap bt_sram_bank2_base fail\n");
		return -EIO;
	}

	btcvsd->infra = syscon_regmap_lookup_by_phandle(dev->of_node,
							"mediatek,infracfg");
	if (IS_ERR(btcvsd->infra)) {
		dev_err(dev, "cannot find infra controller: %ld\n",
			PTR_ERR(btcvsd->infra));
		return PTR_ERR(btcvsd->infra);
	}

	/* get offset */
	ret = of_property_read_u32_array(dev->of_node, "mediatek,offset",
					 offset,
					 ARRAY_SIZE(offset));
	if (ret) {
		dev_warn(dev, "%s(), get offset fail, ret %d\n", __func__, ret);
		return ret;
	}
	btcvsd->infra_misc_offset = offset[0];
	btcvsd->conn_bt_cvsd_mask = offset[1];
	btcvsd->cvsd_mcu_read_offset = offset[2];
	btcvsd->cvsd_mcu_write_offset = offset[3];
	btcvsd->cvsd_packet_indicator = offset[4];

	btcvsd->bt_reg_pkt_r = btcvsd->bt_pkv_base +
			       btcvsd->cvsd_mcu_read_offset;
	btcvsd->bt_reg_pkt_w = btcvsd->bt_pkv_base +
			       btcvsd->cvsd_mcu_write_offset;
	btcvsd->bt_reg_ctl = btcvsd->bt_pkv_base +
			     btcvsd->cvsd_packet_indicator;

	/* init state */
	mtk_btcvsd_snd_set_state(btcvsd, btcvsd->tx, BT_SCO_STATE_IDLE);
	mtk_btcvsd_snd_set_state(btcvsd, btcvsd->rx, BT_SCO_STATE_IDLE);

	return devm_snd_soc_register_component(dev, &mtk_btcvsd_snd_platform,
					       NULL, 0);
}

static int mtk_btcvsd_snd_remove(struct platform_device *pdev)
{
	struct mtk_btcvsd_snd *btcvsd = dev_get_drvdata(&pdev->dev);

	iounmap(btcvsd->bt_pkv_base);
	iounmap(btcvsd->bt_sram_bank2_base);
	return 0;
}

static const struct of_device_id mtk_btcvsd_snd_dt_match[] = {
	{ .compatible = "mediatek,mtk-btcvsd-snd", },
	{},
};
MODULE_DEVICE_TABLE(of, mtk_btcvsd_snd_dt_match);

static struct platform_driver mtk_btcvsd_snd_driver = {
	.driver = {
		.name = "mtk-btcvsd-snd",
		.of_match_table = mtk_btcvsd_snd_dt_match,
	},
	.probe = mtk_btcvsd_snd_probe,
	.remove = mtk_btcvsd_snd_remove,
};

module_platform_driver(mtk_btcvsd_snd_driver);

MODULE_DESCRIPTION("Mediatek ALSA BT SCO CVSD/MSBC Driver");
MODULE_AUTHOR("KaiChieh Chuang <kaichieh.chuang@mediatek.com>");
MODULE_LICENSE("GPL v2");