summaryrefslogtreecommitdiff
path: root/sound/soc/codecs/tas2781-i2c.c
blob: be2ca5eb6c9386f27479990cdd6bcb00c80a7902 (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
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
// SPDX-License-Identifier: GPL-2.0
//
// ALSA SoC Texas Instruments TAS2563/TAS2781 Audio Smart Amplifier
//
// Copyright (C) 2022 - 2024 Texas Instruments Incorporated
// https://www.ti.com
//
// The TAS2563/TAS2781 driver implements a flexible and configurable
// algo coefficient setting for one, two, or even multiple
// TAS2563/TAS2781 chips.
//
// Author: Shenghao Ding <shenghao-ding@ti.com>
// Author: Kevin Lu <kevin-lu@ti.com>
//

#include <linux/crc8.h>
#include <linux/firmware.h>
#include <linux/gpio/consumer.h>
#include <linux/i2c.h>
#include <linux/init.h>
#include <linux/interrupt.h>
#include <linux/module.h>
#include <linux/of.h>
#include <linux/of_address.h>
#include <linux/of_irq.h>
#include <linux/regmap.h>
#include <linux/slab.h>
#include <sound/pcm_params.h>
#include <sound/soc.h>
#include <sound/tas2781.h>
#include <sound/tlv.h>
#include <sound/tas2563-tlv.h>
#include <sound/tas2781-tlv.h>
#include <linux/unaligned.h>

#define X2563_CL_STT_VAL(xreg, xval) \
{	.reg = xreg, \
	.val = { xval }, \
	.val_len = 1, }

#define X2563_CL_STT_4BYTS(xreg, byte0, byte1, byte2, byte3) \
{	.reg = xreg, \
	.val = { byte0, byte1, byte2, byte3 }, \
	.val_len = 4, }

static const struct bulk_reg_val tas2563_cali_start_reg[] = {
	X2563_CL_STT_VAL(TAS2563_IDLE, 0x00),
	X2563_CL_STT_4BYTS(TAS2563_PRM_ENFF_REG, 0x40, 0x00, 0x00, 0x00),
	X2563_CL_STT_4BYTS(TAS2563_PRM_DISTCK_REG, 0x40, 0x00, 0x00, 0x00),
	X2563_CL_STT_4BYTS(TAS2563_PRM_TE_SCTHR_REG, 0x7f, 0xff, 0xff, 0xff),
	X2563_CL_STT_4BYTS(TAS2563_PRM_PLT_FLAG_REG, 0x40, 0x00, 0x00, 0x00),
	X2563_CL_STT_4BYTS(TAS2563_PRM_SINEGAIN_REG, 0x0a, 0x3d, 0x70, 0xa4),
	X2563_CL_STT_4BYTS(TAS2563_TE_TA1_REG, 0x00, 0x36, 0x91, 0x5e),
	X2563_CL_STT_4BYTS(TAS2563_TE_TA1_AT_REG, 0x00, 0x36, 0x91, 0x5e),
	X2563_CL_STT_4BYTS(TAS2563_TE_TA2_REG, 0x00, 0x06, 0xd3, 0x72),
	X2563_CL_STT_4BYTS(TAS2563_TE_AT_REG, 0x00, 0x36, 0x91, 0x5e),
	X2563_CL_STT_4BYTS(TAS2563_TE_DT_REG, 0x00, 0x36, 0x91, 0x5e),
};

#define X2781_CL_STT_VAL(xreg, xval, xlocked) \
{	.reg = xreg, \
	.val = { xval }, \
	.val_len = 1, \
	.is_locked = xlocked, }

#define X2781_CL_STT_4BYTS_UNLOCKED(xreg, byte0, byte1, byte2, byte3) \
{	.reg = xreg, \
	.val = { byte0, byte1, byte2, byte3 }, \
	.val_len = 4, \
	.is_locked = false, }

#define X2781_CL_STT_LEN_UNLOCKED(xreg) \
{	.reg = xreg, \
	.val_len = 4, \
	.is_locked = false, }

static const struct bulk_reg_val tas2781_cali_start_reg[] = {
	X2781_CL_STT_VAL(TAS2781_PRM_INT_MASK_REG, 0xfe, false),
	X2781_CL_STT_VAL(TAS2781_PRM_CLK_CFG_REG, 0xdd, false),
	X2781_CL_STT_VAL(TAS2781_PRM_RSVD_REG, 0x20, false),
	X2781_CL_STT_VAL(TAS2781_PRM_TEST_57_REG, 0x14, false),
	X2781_CL_STT_VAL(TAS2781_PRM_TEST_62_REG, 0x45, true),
	X2781_CL_STT_VAL(TAS2781_PRM_PVDD_UVLO_REG, 0x03, false),
	X2781_CL_STT_VAL(TAS2781_PRM_CHNL_0_REG, 0xa8, false),
	X2781_CL_STT_VAL(TAS2781_PRM_NG_CFG0_REG, 0xb9, false),
	X2781_CL_STT_VAL(TAS2781_PRM_IDLE_CH_DET_REG, 0x92, false),
	/*
	 * This register is pilot tone threshold, different with the
	 * calibration tool version, it will be updated in
	 * tas2781_calib_start_put(), set to 1mA.
	 */
	X2781_CL_STT_4BYTS_UNLOCKED(0, 0x00, 0x00, 0x00, 0x56),
	X2781_CL_STT_4BYTS_UNLOCKED(TAS2781_PRM_PLT_FLAG_REG,
		0x40, 0x00, 0x00, 0x00),
	X2781_CL_STT_LEN_UNLOCKED(TAS2781_PRM_SINEGAIN_REG),
	X2781_CL_STT_LEN_UNLOCKED(TAS2781_PRM_SINEGAIN2_REG),
};

static const struct i2c_device_id tasdevice_id[] = {
	{ "tas2563", TAS2563 },
	{ "tas2781", TAS2781 },
	{}
};
MODULE_DEVICE_TABLE(i2c, tasdevice_id);

#ifdef CONFIG_OF
static const struct of_device_id tasdevice_of_match[] = {
	{ .compatible = "ti,tas2563" },
	{ .compatible = "ti,tas2781" },
	{},
};
MODULE_DEVICE_TABLE(of, tasdevice_of_match);
#endif

/**
 * tas2781_digital_getvol - get the volum control
 * @kcontrol: control pointer
 * @ucontrol: User data
 * Customer Kcontrol for tas2781 is primarily for regmap booking, paging
 * depends on internal regmap mechanism.
 * tas2781 contains book and page two-level register map, especially
 * book switching will set the register BXXP00R7F, after switching to the
 * correct book, then leverage the mechanism for paging to access the
 * register.
 */
static int tas2781_digital_getvol(struct snd_kcontrol *kcontrol,
	struct snd_ctl_elem_value *ucontrol)
{
	struct snd_soc_component *codec = snd_soc_kcontrol_component(kcontrol);
	struct tasdevice_priv *tas_priv = snd_soc_component_get_drvdata(codec);
	struct soc_mixer_control *mc =
		(struct soc_mixer_control *)kcontrol->private_value;

	return tasdevice_digital_getvol(tas_priv, ucontrol, mc);
}

static int tas2781_digital_putvol(struct snd_kcontrol *kcontrol,
	struct snd_ctl_elem_value *ucontrol)
{
	struct snd_soc_component *codec = snd_soc_kcontrol_component(kcontrol);
	struct tasdevice_priv *tas_priv = snd_soc_component_get_drvdata(codec);
	struct soc_mixer_control *mc =
		(struct soc_mixer_control *)kcontrol->private_value;

	return tasdevice_digital_putvol(tas_priv, ucontrol, mc);
}

static int tas2781_amp_getvol(struct snd_kcontrol *kcontrol,
	struct snd_ctl_elem_value *ucontrol)
{
	struct snd_soc_component *codec = snd_soc_kcontrol_component(kcontrol);
	struct tasdevice_priv *tas_priv = snd_soc_component_get_drvdata(codec);
	struct soc_mixer_control *mc =
		(struct soc_mixer_control *)kcontrol->private_value;

	return tasdevice_amp_getvol(tas_priv, ucontrol, mc);
}

static int tas2781_amp_putvol(struct snd_kcontrol *kcontrol,
	struct snd_ctl_elem_value *ucontrol)
{
	struct snd_soc_component *codec = snd_soc_kcontrol_component(kcontrol);
	struct tasdevice_priv *tas_priv =
		snd_soc_component_get_drvdata(codec);
	struct soc_mixer_control *mc =
		(struct soc_mixer_control *)kcontrol->private_value;

	return tasdevice_amp_putvol(tas_priv, ucontrol, mc);
}

static int tasdev_force_fwload_get(struct snd_kcontrol *kcontrol,
	struct snd_ctl_elem_value *ucontrol)
{
	struct snd_soc_component *component =
		snd_soc_kcontrol_component(kcontrol);
	struct tasdevice_priv *tas_priv =
		snd_soc_component_get_drvdata(component);

	ucontrol->value.integer.value[0] = (int)tas_priv->force_fwload_status;
	dev_dbg(tas_priv->dev, "%s : Force FWload %s\n", __func__,
			tas_priv->force_fwload_status ? "ON" : "OFF");

	return 0;
}

static int tasdev_force_fwload_put(struct snd_kcontrol *kcontrol,
	struct snd_ctl_elem_value *ucontrol)
{
	struct snd_soc_component *component =
		snd_soc_kcontrol_component(kcontrol);
	struct tasdevice_priv *tas_priv =
		snd_soc_component_get_drvdata(component);
	bool change, val = (bool)ucontrol->value.integer.value[0];

	if (tas_priv->force_fwload_status == val)
		change = false;
	else {
		change = true;
		tas_priv->force_fwload_status = val;
	}
	dev_dbg(tas_priv->dev, "%s : Force FWload %s\n", __func__,
		tas_priv->force_fwload_status ? "ON" : "OFF");

	return change;
}

static int tasdev_cali_data_get(struct snd_kcontrol *kcontrol,
	struct snd_ctl_elem_value *ucontrol)
{
	struct snd_soc_component *comp = snd_soc_kcontrol_component(kcontrol);
	struct tasdevice_priv *priv = snd_soc_component_get_drvdata(comp);
	struct soc_bytes_ext *bytes_ext =
		(struct soc_bytes_ext *) kcontrol->private_value;
	struct calidata *cali_data = &priv->cali_data;
	struct cali_reg *p = &cali_data->cali_reg_array;
	unsigned char *dst = ucontrol->value.bytes.data;
	unsigned char *data = cali_data->data;
	unsigned int i = 0;
	unsigned int j, k;
	int rc;

	guard(mutex)(&priv->codec_lock);
	if (!priv->is_user_space_calidata)
		return -1;

	if (!p->r0_reg)
		return -1;

	dst[i++] = bytes_ext->max;
	dst[i++] = 'r';

	dst[i++] = TASDEVICE_BOOK_ID(p->r0_reg);
	dst[i++] = TASDEVICE_PAGE_ID(p->r0_reg);
	dst[i++] = TASDEVICE_PAGE_REG(p->r0_reg);

	dst[i++] = TASDEVICE_BOOK_ID(p->r0_low_reg);
	dst[i++] = TASDEVICE_PAGE_ID(p->r0_low_reg);
	dst[i++] = TASDEVICE_PAGE_REG(p->r0_low_reg);

	dst[i++] = TASDEVICE_BOOK_ID(p->invr0_reg);
	dst[i++] = TASDEVICE_PAGE_ID(p->invr0_reg);
	dst[i++] = TASDEVICE_PAGE_REG(p->invr0_reg);

	dst[i++] = TASDEVICE_BOOK_ID(p->pow_reg);
	dst[i++] = TASDEVICE_PAGE_ID(p->pow_reg);
	dst[i++] = TASDEVICE_PAGE_REG(p->pow_reg);

	dst[i++] = TASDEVICE_BOOK_ID(p->tlimit_reg);
	dst[i++] = TASDEVICE_PAGE_ID(p->tlimit_reg);
	dst[i++] = TASDEVICE_PAGE_REG(p->tlimit_reg);

	for (j = 0, k = 0; j < priv->ndev; j++) {
		if (j == data[k]) {
			dst[i++] = j;
			k++;
		} else {
			dev_err(priv->dev, "chn %d device %u not match\n",
				j, data[k]);
			k += 21;
			continue;
		}
		rc = tasdevice_dev_bulk_read(priv, j, p->r0_reg, &dst[i], 4);
		if (rc < 0) {
			dev_err(priv->dev, "chn %d r0_reg bulk_rd err = %d\n",
				j, rc);
			i += 20;
			k += 20;
			continue;
		}
		rc = memcmp(&dst[i], &data[k], 4);
		if (rc != 0)
			dev_dbg(priv->dev, "chn %d r0_data is not same\n", j);
		k += 4;
		i += 4;
		rc = tasdevice_dev_bulk_read(priv, j, p->r0_low_reg,
			&dst[i], 4);
		if (rc < 0) {
			dev_err(priv->dev, "chn %d r0_low bulk_rd err = %d\n",
				j, rc);
			i += 16;
			k += 16;
			continue;
		}
		rc = memcmp(&dst[i], &data[k], 4);
		if (rc != 0)
			dev_dbg(priv->dev, "chn %d r0_low is not same\n", j);
		i += 4;
		k += 4;
		rc = tasdevice_dev_bulk_read(priv, j, p->invr0_reg,
			&dst[i], 4);
		if (rc < 0) {
			dev_err(priv->dev, "chn %d invr0 bulk_rd err = %d\n",
				j, rc);
			i += 12;
			k += 12;
			continue;
		}
		rc = memcmp(&dst[i], &data[k], 4);
		if (rc != 0)
			dev_dbg(priv->dev, "chn %d invr0 is not same\n", j);
		i += 4;
		k += 4;
		rc = tasdevice_dev_bulk_read(priv, j, p->pow_reg, &dst[i], 4);
		if (rc < 0) {
			dev_err(priv->dev, "chn %d pow_reg bulk_rd err = %d\n",
				j, rc);
			i += 8;
			k += 8;
			continue;
		}
		rc = memcmp(&dst[i], &data[k], 4);
		if (rc != 0)
			dev_dbg(priv->dev, "chn %d pow_reg is not same\n", j);
		i += 4;
		k += 4;
		rc = tasdevice_dev_bulk_read(priv, j, p->tlimit_reg,
			&dst[i], 4);
		if (rc < 0) {
			dev_err(priv->dev, "chn %d tlimit bulk_rd err = %d\n",
				j, rc);
		}
		rc = memcmp(&dst[i], &data[k], 4);
		if (rc != 0)
			dev_dbg(priv->dev, "chn %d tlimit is not same\n", j);
		i += 4;
		k += 4;
	}
	return 0;
}

static int calib_data_get(struct tasdevice_priv *tas_priv, int reg,
	unsigned char *dst)
{
	struct i2c_client *clt = (struct i2c_client *)tas_priv->client;
	struct tasdevice *tasdev = tas_priv->tasdevice;
	int rc = -1;
	int i;

	for (i = 0; i < tas_priv->ndev; i++) {
		if (clt->addr == tasdev[i].dev_addr) {
			/* First byte is the device index. */
			dst[0] = i;
			rc = tasdevice_dev_bulk_read(tas_priv, i, reg, &dst[1],
				4);
			break;
		}
	}

	return rc;
}

static void sngl_calib_start(struct tasdevice_priv *tas_priv, int i,
	int *reg, unsigned char *dat)
{
	struct tasdevice *tasdev = tas_priv->tasdevice;
	struct bulk_reg_val *p = tasdev[i].cali_data_backup;
	const int sum = ARRAY_SIZE(tas2781_cali_start_reg);
	int j;

	if (p == NULL)
		return;

	/* Store the current setting from the chip */
	for (j = 0; j < sum; j++) {
		if (p[j].val_len == 1) {
			if (p[j].is_locked)
				tasdevice_dev_write(tas_priv, i,
					TAS2781_TEST_UNLOCK_REG,
					TAS2781_TEST_PAGE_UNLOCK);
			tasdevice_dev_read(tas_priv, i, p[j].reg,
				(int *)&p[j].val[0]);
		} else {
			switch (p[j].reg) {
			case 0: {
				if (!reg[0])
					continue;
				p[j].reg = reg[0];
			}
				break;
			case TAS2781_PRM_PLT_FLAG_REG:
			p[j].reg = reg[1];
				break;
			case TAS2781_PRM_SINEGAIN_REG:
			p[j].reg = reg[2];
				break;
			case TAS2781_PRM_SINEGAIN2_REG:
			p[j].reg = reg[3];
				break;
			}
			tasdevice_dev_bulk_read(tas_priv, i, p[j].reg,
				p[j].val, 4);
		}
	}

	/* Update the setting for calibration */
	for (j = 0; j < sum - 2; j++) {
		if (p[j].val_len == 1) {
			if (p[j].is_locked)
				tasdevice_dev_write(tas_priv, i,
					TAS2781_TEST_UNLOCK_REG,
					TAS2781_TEST_PAGE_UNLOCK);
			tasdevice_dev_write(tas_priv, i, p[j].reg,
				tas2781_cali_start_reg[j].val[0]);
		} else {
			if (!p[j].reg)
				continue;
			tasdevice_dev_bulk_write(tas_priv, i, p[j].reg,
				(unsigned char *)
				tas2781_cali_start_reg[j].val, 4);
		}
	}

	tasdevice_dev_bulk_write(tas_priv, i, p[j].reg, &dat[1], 4);
	tasdevice_dev_bulk_write(tas_priv, i, p[j + 1].reg, &dat[5], 4);
}

static int tas2781_calib_start_put(struct snd_kcontrol *kcontrol,
	struct snd_ctl_elem_value *ucontrol)
{
	struct snd_soc_component *comp = snd_soc_kcontrol_component(kcontrol);
	struct tasdevice_priv *priv = snd_soc_component_get_drvdata(comp);
	struct soc_bytes_ext *bytes_ext =
		(struct soc_bytes_ext *) kcontrol->private_value;
	unsigned char *dat = ucontrol->value.bytes.data;
	int i, reg[4];
	int j = 0;

	guard(mutex)(&priv->codec_lock);
	if (priv->chip_id != TAS2781 || bytes_ext->max != dat[0] ||
		dat[1] != 'r') {
		dev_err(priv->dev, "%s: package fmt or chipid incorrect\n",
			__func__);
		return 0;
	}
	j += 2;
	/* refresh pilot tone and SineGain register */
	for (i = 0; i < ARRAY_SIZE(reg); i++) {
		reg[i] = TASDEVICE_REG(dat[j], dat[j + 1], dat[j + 2]);
		j += 3;
	}

	for (i = 0; i < priv->ndev; i++) {
		int k = i * 9 + j;

		if (dat[k] != i) {
			dev_err(priv->dev, "%s:no cal-setting for dev %d\n",
				__func__, i);
			continue;
		}
		sngl_calib_start(priv, i, reg, dat + k);
	}
	return 1;
}

static void tas2781_calib_stop_put(struct tasdevice_priv *tas_priv)
{
	const int sum = ARRAY_SIZE(tas2781_cali_start_reg);
	int i, j;

	for (i = 0; i < tas_priv->ndev; i++) {
		struct tasdevice *tasdev = tas_priv->tasdevice;
		struct bulk_reg_val *p = tasdev[i].cali_data_backup;

		if (p == NULL)
			continue;

		for (j = 0; j < sum; j++) {
			if (p[j].val_len == 1) {
				if (p[j].is_locked)
					tasdevice_dev_write(tas_priv, i,
						TAS2781_TEST_UNLOCK_REG,
						TAS2781_TEST_PAGE_UNLOCK);
				tasdevice_dev_write(tas_priv, i, p[j].reg,
					p[j].val[0]);
			} else {
				if (!p[j].reg)
					continue;
				tasdevice_dev_bulk_write(tas_priv, i, p[j].reg,
					p[j].val, 4);
			}
		}
	}
}

static int tas2563_calib_start_put(struct snd_kcontrol *kcontrol,
	struct snd_ctl_elem_value *ucontrol)
{
	struct bulk_reg_val *q = (struct bulk_reg_val *)tas2563_cali_start_reg;
	struct snd_soc_component *comp = snd_soc_kcontrol_component(kcontrol);
	struct tasdevice_priv *tas_priv = snd_soc_component_get_drvdata(comp);
	const int sum = ARRAY_SIZE(tas2563_cali_start_reg);
	int rc = 1;
	int i, j;

	guard(mutex)(&tas_priv->codec_lock);
	if (tas_priv->chip_id != TAS2563) {
		rc = -1;
		goto out;
	}

	for (i = 0; i < tas_priv->ndev; i++) {
		struct tasdevice *tasdev = tas_priv->tasdevice;
		struct bulk_reg_val *p = tasdev[i].cali_data_backup;

		if (p == NULL)
			continue;
		for (j = 0; j < sum; j++) {
			if (p[j].val_len == 1)
				tasdevice_dev_read(tas_priv,
					i, p[j].reg,
					(unsigned int *)&p[j].val[0]);
			else
				tasdevice_dev_bulk_read(tas_priv,
					i, p[j].reg, p[j].val, 4);
		}

		for (j = 0; j < sum; j++) {
			if (p[j].val_len == 1)
				tasdevice_dev_write(tas_priv, i, p[j].reg,
					q[j].val[0]);
			else
				tasdevice_dev_bulk_write(tas_priv, i, p[j].reg,
					q[j].val, 4);
		}
	}
out:
	return rc;
}

static void tas2563_calib_stop_put(struct tasdevice_priv *tas_priv)
{
	const int sum = ARRAY_SIZE(tas2563_cali_start_reg);
	int i, j;

	for (i = 0; i < tas_priv->ndev; i++) {
		struct tasdevice *tasdev = tas_priv->tasdevice;
		struct bulk_reg_val *p = tasdev[i].cali_data_backup;

		if (p == NULL)
			continue;

		for (j = 0; j < sum; j++) {
			if (p[j].val_len == 1)
				tasdevice_dev_write(tas_priv, i, p[j].reg,
					p[j].val[0]);
			else
				tasdevice_dev_bulk_write(tas_priv, i, p[j].reg,
					p[j].val, 4);
		}
	}
}

static int tasdev_calib_stop_put(struct snd_kcontrol *kcontrol,
	struct snd_ctl_elem_value *ucontrol)
{
	struct snd_soc_component *comp = snd_soc_kcontrol_component(kcontrol);
	struct tasdevice_priv *priv = snd_soc_component_get_drvdata(comp);

	guard(mutex)(&priv->codec_lock);
	if (priv->chip_id == TAS2563)
		tas2563_calib_stop_put(priv);
	else
		tas2781_calib_stop_put(priv);

	return 1;
}

static int tasdev_cali_data_put(struct snd_kcontrol *kcontrol,
	struct snd_ctl_elem_value *ucontrol)
{
	struct snd_soc_component *comp = snd_soc_kcontrol_component(kcontrol);
	struct tasdevice_priv *priv = snd_soc_component_get_drvdata(comp);
	struct soc_bytes_ext *bytes_ext =
		(struct soc_bytes_ext *) kcontrol->private_value;
	struct calidata *cali_data = &priv->cali_data;
	struct cali_reg *p = &cali_data->cali_reg_array;
	unsigned char *src = ucontrol->value.bytes.data;
	unsigned char *dst = cali_data->data;
	int rc = 1, i = 0;
	int j;

	guard(mutex)(&priv->codec_lock);
	if (src[0] != bytes_ext->max || src[1] != 'r') {
		dev_err(priv->dev, "%s: pkg fmt invalid\n", __func__);
		return 0;
	}
	for (j = 0; j < priv->ndev; j++) {
		if (src[17 + j * 21] != j) {
			dev_err(priv->dev, "%s: pkg fmt invalid\n", __func__);
			return 0;
		}
	}
	i += 2;
	priv->is_user_space_calidata = true;

	p->r0_reg = TASDEVICE_REG(src[i], src[i + 1], src[i + 2]);
	i += 3;
	p->r0_low_reg = TASDEVICE_REG(src[i], src[i + 1], src[i + 2]);
	i += 3;
	p->invr0_reg = TASDEVICE_REG(src[i], src[i + 1], src[i + 2]);
	i += 3;
	p->pow_reg = TASDEVICE_REG(src[i], src[i + 1], src[i + 2]);
	i += 3;
	p->tlimit_reg = TASDEVICE_REG(src[i], src[i + 1], src[i + 2]);
	i += 3;

	memcpy(dst, &src[i], cali_data->total_sz);
	return rc;
}

static int tas2781_latch_reg_get(struct snd_kcontrol *kcontrol,
	struct snd_ctl_elem_value *ucontrol)
{
	struct snd_soc_component *comp = snd_soc_kcontrol_component(kcontrol);
	struct tasdevice_priv *tas_priv = snd_soc_component_get_drvdata(comp);
	struct i2c_client *clt = (struct i2c_client *)tas_priv->client;
	struct soc_bytes_ext *bytes_ext =
		(struct soc_bytes_ext *) kcontrol->private_value;
	struct tasdevice *tasdev = tas_priv->tasdevice;
	unsigned char *dst = ucontrol->value.bytes.data;
	int i, val, rc = -1;

	dst[0] = bytes_ext->max;
	guard(mutex)(&tas_priv->codec_lock);
	for (i = 0; i < tas_priv->ndev; i++) {
		if (clt->addr == tasdev[i].dev_addr) {
			/* First byte is the device index. */
			dst[1] = i;
			rc = tasdevice_dev_read(tas_priv, i,
				TAS2781_RUNTIME_LATCH_RE_REG, &val);
			if (rc < 0)
				dev_err(tas_priv->dev, "%s, get value error\n",
					__func__);
			else
				dst[2] = val;

			break;
		}
	}

	return rc;
}

static int tasdev_tf_data_get(struct snd_kcontrol *kcontrol,
	struct snd_ctl_elem_value *ucontrol)
{
	struct snd_soc_component *comp = snd_soc_kcontrol_component(kcontrol);
	struct tasdevice_priv *tas_priv = snd_soc_component_get_drvdata(comp);
	struct soc_bytes_ext *bytes_ext =
		(struct soc_bytes_ext *) kcontrol->private_value;
	unsigned char *dst = ucontrol->value.bytes.data;
	unsigned int reg;

	if (tas_priv->chip_id == TAS2781)
		reg = TAS2781_RUNTIME_RE_REG_TF;
	else
		reg = TAS2563_RUNTIME_RE_REG_TF;

	guard(mutex)(&tas_priv->codec_lock);
	dst[0] = bytes_ext->max;
	return calib_data_get(tas_priv, reg, &dst[1]);
}

static int tasdev_re_data_get(struct snd_kcontrol *kcontrol,
	struct snd_ctl_elem_value *ucontrol)
{
	struct snd_soc_component *comp = snd_soc_kcontrol_component(kcontrol);
	struct tasdevice_priv *tas_priv = snd_soc_component_get_drvdata(comp);
	struct soc_bytes_ext *bytes_ext =
		(struct soc_bytes_ext *) kcontrol->private_value;
	unsigned char *dst = ucontrol->value.bytes.data;
	unsigned int reg;

	if (tas_priv->chip_id == TAS2781)
		reg = TAS2781_RUNTIME_RE_REG;
	else
		reg = TAS2563_RUNTIME_RE_REG;
	guard(mutex)(&tas_priv->codec_lock);
	dst[0] = bytes_ext->max;
	return calib_data_get(tas_priv, reg, &dst[1]);
}

static int tasdev_r0_data_get(struct snd_kcontrol *kcontrol,
	struct snd_ctl_elem_value *ucontrol)
{
	struct snd_soc_component *comp = snd_soc_kcontrol_component(kcontrol);
	struct tasdevice_priv *tas_priv = snd_soc_component_get_drvdata(comp);
	struct calidata *cali_data = &tas_priv->cali_data;
	struct soc_bytes_ext *bytes_ext =
		(struct soc_bytes_ext *) kcontrol->private_value;
	unsigned char *dst = ucontrol->value.bytes.data;
	unsigned int reg;

	guard(mutex)(&tas_priv->codec_lock);

	if (tas_priv->chip_id == TAS2563)
		reg = TAS2563_PRM_R0_REG;
	else if (cali_data->cali_reg_array.r0_reg)
		reg = cali_data->cali_reg_array.r0_reg;
	else
		return -1;
	dst[0] = bytes_ext->max;
	return calib_data_get(tas_priv, reg, &dst[1]);
}

static int tasdev_XMA1_data_get(struct snd_kcontrol *kcontrol,
	struct snd_ctl_elem_value *ucontrol)
{
	struct snd_soc_component *comp = snd_soc_kcontrol_component(kcontrol);
	struct tasdevice_priv *tas_priv = snd_soc_component_get_drvdata(comp);
	struct soc_bytes_ext *bytes_ext =
		(struct soc_bytes_ext *) kcontrol->private_value;
	unsigned char *dst = ucontrol->value.bytes.data;
	unsigned int reg = TASDEVICE_XM_A1_REG;

	guard(mutex)(&tas_priv->codec_lock);
	dst[0] = bytes_ext->max;
	return calib_data_get(tas_priv, reg, &dst[1]);
}

static int tasdev_XMA2_data_get(struct snd_kcontrol *kcontrol,
	struct snd_ctl_elem_value *ucontrol)
{
	struct snd_soc_component *comp = snd_soc_kcontrol_component(kcontrol);
	struct tasdevice_priv *tas_priv = snd_soc_component_get_drvdata(comp);
	struct soc_bytes_ext *bytes_ext =
		(struct soc_bytes_ext *) kcontrol->private_value;
	unsigned char *dst = ucontrol->value.bytes.data;
	unsigned int reg = TASDEVICE_XM_A2_REG;

	guard(mutex)(&tas_priv->codec_lock);
	dst[0] = bytes_ext->max;
	return calib_data_get(tas_priv, reg, &dst[1]);
}

static int tasdev_nop_get(
	struct snd_kcontrol *kcontrol,
	struct snd_ctl_elem_value *ucontrol)
{
	return 0;
}

static int tas2563_digital_gain_get(
	struct snd_kcontrol *kcontrol,
	struct snd_ctl_elem_value *ucontrol)
{
	struct soc_mixer_control *mc =
		(struct soc_mixer_control *)kcontrol->private_value;
	struct snd_soc_component *codec = snd_soc_kcontrol_component(kcontrol);
	struct tasdevice_priv *tas_dev = snd_soc_component_get_drvdata(codec);
	unsigned int l = 0, r = mc->max;
	unsigned int target, ar_mid, mid, ar_l, ar_r;
	unsigned int reg = mc->reg;
	unsigned char data[4];
	int ret;

	mutex_lock(&tas_dev->codec_lock);
	/* Read the primary device */
	ret = tasdevice_dev_bulk_read(tas_dev, 0, reg, data, 4);
	if (ret) {
		dev_err(tas_dev->dev, "%s, get AMP vol error\n", __func__);
		goto out;
	}

	target = get_unaligned_be32(&data[0]);

	while (r > 1 + l) {
		mid = (l + r) / 2;
		ar_mid = get_unaligned_be32(tas2563_dvc_table[mid]);
		if (target < ar_mid)
			r = mid;
		else
			l = mid;
	}

	ar_l = get_unaligned_be32(tas2563_dvc_table[l]);
	ar_r = get_unaligned_be32(tas2563_dvc_table[r]);

	/* find out the member same as or closer to the current volume */
	ucontrol->value.integer.value[0] =
		abs(target - ar_l) <= abs(target - ar_r) ? l : r;
out:
	mutex_unlock(&tas_dev->codec_lock);
	return 0;
}

static int tas2563_digital_gain_put(
	struct snd_kcontrol *kcontrol,
	struct snd_ctl_elem_value *ucontrol)
{
	struct soc_mixer_control *mc =
		(struct soc_mixer_control *)kcontrol->private_value;
	struct snd_soc_component *codec = snd_soc_kcontrol_component(kcontrol);
	struct tasdevice_priv *tas_dev = snd_soc_component_get_drvdata(codec);
	int vol = ucontrol->value.integer.value[0];
	int status = 0, max = mc->max, rc = 1;
	int i, ret;
	unsigned int reg = mc->reg;
	unsigned int volrd, volwr;
	unsigned char data[4];

	vol = clamp(vol, 0, max);
	mutex_lock(&tas_dev->codec_lock);
	/* Read the primary device */
	ret = tasdevice_dev_bulk_read(tas_dev, 0, reg, data, 4);
	if (ret) {
		dev_err(tas_dev->dev, "%s, get AMP vol error\n", __func__);
		rc = -1;
		goto out;
	}

	volrd = get_unaligned_be32(&data[0]);
	volwr = get_unaligned_be32(tas2563_dvc_table[vol]);

	if (volrd == volwr) {
		rc = 0;
		goto out;
	}

	for (i = 0; i < tas_dev->ndev; i++) {
		ret = tasdevice_dev_bulk_write(tas_dev, i, reg,
			(unsigned char *)tas2563_dvc_table[vol], 4);
		if (ret) {
			dev_err(tas_dev->dev,
				"%s, set digital vol error in dev %d\n",
				__func__, i);
			status |= BIT(i);
		}
	}

	if (status)
		rc = -1;
out:
	mutex_unlock(&tas_dev->codec_lock);
	return rc;
}

static const struct snd_kcontrol_new tasdevice_snd_controls[] = {
	SOC_SINGLE_BOOL_EXT("Speaker Force Firmware Load", 0,
		tasdev_force_fwload_get, tasdev_force_fwload_put),
};

static const struct snd_kcontrol_new tasdevice_cali_controls[] = {
	SOC_SINGLE_EXT("Calibration Stop", SND_SOC_NOPM, 0, 1, 0,
		tasdev_nop_get, tasdev_calib_stop_put),
	SND_SOC_BYTES_EXT("Amp TF Data", 6, tasdev_tf_data_get, NULL),
	SND_SOC_BYTES_EXT("Amp RE Data", 6, tasdev_re_data_get, NULL),
	SND_SOC_BYTES_EXT("Amp R0 Data", 6, tasdev_r0_data_get, NULL),
	SND_SOC_BYTES_EXT("Amp XMA1 Data", 6, tasdev_XMA1_data_get, NULL),
	SND_SOC_BYTES_EXT("Amp XMA2 Data", 6, tasdev_XMA2_data_get, NULL),
};

static const struct snd_kcontrol_new tas2781_snd_controls[] = {
	SOC_SINGLE_RANGE_EXT_TLV("Speaker Analog Gain", TAS2781_AMP_LEVEL,
		1, 0, 20, 0, tas2781_amp_getvol,
		tas2781_amp_putvol, amp_vol_tlv),
	SOC_SINGLE_RANGE_EXT_TLV("Speaker Digital Gain", TAS2781_DVC_LVL,
		0, 0, 200, 1, tas2781_digital_getvol,
		tas2781_digital_putvol, dvc_tlv),
};

static const struct snd_kcontrol_new tas2781_cali_controls[] = {
	SND_SOC_BYTES_EXT("Amp Latch Data", 3, tas2781_latch_reg_get, NULL),
};

static const struct snd_kcontrol_new tas2563_snd_controls[] = {
	SOC_SINGLE_RANGE_EXT_TLV("Speaker Digital Volume", TAS2563_DVC_LVL, 0,
		0, ARRAY_SIZE(tas2563_dvc_table) - 1, 0,
		tas2563_digital_gain_get, tas2563_digital_gain_put,
		tas2563_dvc_tlv),
};

static const struct snd_kcontrol_new tas2563_cali_controls[] = {
	SOC_SINGLE_EXT("Calibration Start", SND_SOC_NOPM, 0, 1, 0,
		tasdev_nop_get, tas2563_calib_start_put),
};

static int tasdevice_set_profile_id(struct snd_kcontrol *kcontrol,
		struct snd_ctl_elem_value *ucontrol)
{
	struct snd_soc_component *codec = snd_soc_kcontrol_component(kcontrol);
	struct tasdevice_priv *tas_priv = snd_soc_component_get_drvdata(codec);
	int ret = 0;

	if (tas_priv->rcabin.profile_cfg_id !=
		ucontrol->value.integer.value[0]) {
		tas_priv->rcabin.profile_cfg_id =
			ucontrol->value.integer.value[0];
		ret = 1;
	}

	return ret;
}

static int tasdevice_info_active_num(struct snd_kcontrol *kcontrol,
			struct snd_ctl_elem_info *uinfo)
{
	struct snd_soc_component *codec = snd_soc_kcontrol_component(kcontrol);
	struct tasdevice_priv *tas_priv = snd_soc_component_get_drvdata(codec);

	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
	uinfo->count = 1;
	uinfo->value.integer.min = 0;
	uinfo->value.integer.max = tas_priv->ndev - 1;

	return 0;
}

static int tasdevice_info_chip_id(struct snd_kcontrol *kcontrol,
			struct snd_ctl_elem_info *uinfo)
{
	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
	uinfo->count = 1;
	uinfo->value.integer.min = TAS2563;
	uinfo->value.integer.max = TAS2781;

	return 0;
}

static int tasdevice_info_programs(struct snd_kcontrol *kcontrol,
			struct snd_ctl_elem_info *uinfo)
{
	struct snd_soc_component *codec = snd_soc_kcontrol_component(kcontrol);
	struct tasdevice_priv *tas_priv = snd_soc_component_get_drvdata(codec);
	struct tasdevice_fw *tas_fw = tas_priv->fmw;

	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
	uinfo->count = 1;
	uinfo->value.integer.min = 0;
	uinfo->value.integer.max = (int)tas_fw->nr_programs;

	return 0;
}

static int tasdevice_info_configurations(
	struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
{
	struct snd_soc_component *codec =
		snd_soc_kcontrol_component(kcontrol);
	struct tasdevice_priv *tas_priv = snd_soc_component_get_drvdata(codec);
	struct tasdevice_fw *tas_fw = tas_priv->fmw;

	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
	uinfo->count = 1;
	uinfo->value.integer.min = 0;
	uinfo->value.integer.max = (int)tas_fw->nr_configurations - 1;

	return 0;
}

static int tasdevice_info_profile(struct snd_kcontrol *kcontrol,
			struct snd_ctl_elem_info *uinfo)
{
	struct snd_soc_component *codec = snd_soc_kcontrol_component(kcontrol);
	struct tasdevice_priv *tas_priv = snd_soc_component_get_drvdata(codec);

	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
	uinfo->count = 1;
	uinfo->value.integer.min = 0;
	uinfo->value.integer.max = tas_priv->rcabin.ncfgs - 1;

	return 0;
}

static int tasdevice_get_profile_id(struct snd_kcontrol *kcontrol,
			struct snd_ctl_elem_value *ucontrol)
{
	struct snd_soc_component *codec = snd_soc_kcontrol_component(kcontrol);
	struct tasdevice_priv *tas_priv = snd_soc_component_get_drvdata(codec);

	ucontrol->value.integer.value[0] = tas_priv->rcabin.profile_cfg_id;

	return 0;
}

static int tasdevice_get_chip_id(struct snd_kcontrol *kcontrol,
			struct snd_ctl_elem_value *ucontrol)
{
	struct snd_soc_component *codec = snd_soc_kcontrol_component(kcontrol);
	struct tasdevice_priv *tas_priv = snd_soc_component_get_drvdata(codec);

	ucontrol->value.integer.value[0] = tas_priv->chip_id;

	return 0;
}

static int tasdevice_create_control(struct tasdevice_priv *tas_priv)
{
	struct snd_kcontrol_new *prof_ctrls;
	int nr_controls = 1;
	int mix_index = 0;
	int ret;
	char *name;

	prof_ctrls = devm_kcalloc(tas_priv->dev, nr_controls,
		sizeof(prof_ctrls[0]), GFP_KERNEL);
	if (!prof_ctrls) {
		ret = -ENOMEM;
		goto out;
	}

	/* Create a mixer item for selecting the active profile */
	name = devm_kstrdup(tas_priv->dev, "Speaker Profile Id", GFP_KERNEL);
	if (!name) {
		ret = -ENOMEM;
		goto out;
	}
	prof_ctrls[mix_index].name = name;
	prof_ctrls[mix_index].iface = SNDRV_CTL_ELEM_IFACE_MIXER;
	prof_ctrls[mix_index].info = tasdevice_info_profile;
	prof_ctrls[mix_index].get = tasdevice_get_profile_id;
	prof_ctrls[mix_index].put = tasdevice_set_profile_id;
	mix_index++;

	ret = snd_soc_add_component_controls(tas_priv->codec,
		prof_ctrls, nr_controls < mix_index ? nr_controls : mix_index);

out:
	return ret;
}

static int tasdevice_program_get(struct snd_kcontrol *kcontrol,
	struct snd_ctl_elem_value *ucontrol)
{
	struct snd_soc_component *codec = snd_soc_kcontrol_component(kcontrol);
	struct tasdevice_priv *tas_priv = snd_soc_component_get_drvdata(codec);

	ucontrol->value.integer.value[0] = tas_priv->cur_prog;

	return 0;
}

static int tasdevice_program_put(struct snd_kcontrol *kcontrol,
	struct snd_ctl_elem_value *ucontrol)
{
	struct snd_soc_component *codec = snd_soc_kcontrol_component(kcontrol);
	struct tasdevice_priv *tas_priv = snd_soc_component_get_drvdata(codec);
	unsigned int nr_program = ucontrol->value.integer.value[0];
	int ret = 0;

	if (tas_priv->cur_prog != nr_program) {
		tas_priv->cur_prog = nr_program;
		ret = 1;
	}

	return ret;
}

static int tasdevice_configuration_get(struct snd_kcontrol *kcontrol,
	struct snd_ctl_elem_value *ucontrol)
{

	struct snd_soc_component *codec = snd_soc_kcontrol_component(kcontrol);
	struct tasdevice_priv *tas_priv = snd_soc_component_get_drvdata(codec);

	ucontrol->value.integer.value[0] = tas_priv->cur_conf;

	return 0;
}

static int tasdevice_configuration_put(
	struct snd_kcontrol *kcontrol,
	struct snd_ctl_elem_value *ucontrol)
{
	struct snd_soc_component *codec = snd_soc_kcontrol_component(kcontrol);
	struct tasdevice_priv *tas_priv = snd_soc_component_get_drvdata(codec);
	unsigned int nr_configuration = ucontrol->value.integer.value[0];
	int ret = 0;

	if (tas_priv->cur_conf != nr_configuration) {
		tas_priv->cur_conf = nr_configuration;
		ret = 1;
	}

	return ret;
}

static int tasdevice_active_num_get(struct snd_kcontrol *kcontrol,
	struct snd_ctl_elem_value *ucontrol)
{
	struct snd_soc_component *codec = snd_soc_kcontrol_component(kcontrol);
	struct tasdevice_priv *tas_priv = snd_soc_component_get_drvdata(codec);
	struct i2c_client *clt = (struct i2c_client *)tas_priv->client;
	struct tasdevice *tasdev = tas_priv->tasdevice;
	int i;

	for (i = 0; i < tas_priv->ndev; i++) {
		if (clt->addr == tasdev[i].dev_addr) {
			ucontrol->value.integer.value[0] = i;
			return 0;
		}
	}

	return -1;
}

static int tasdevice_active_num_put(struct snd_kcontrol *kcontrol,
	struct snd_ctl_elem_value *ucontrol)
{
	struct snd_soc_component *codec = snd_soc_kcontrol_component(kcontrol);
	struct tasdevice_priv *tas_priv = snd_soc_component_get_drvdata(codec);
	int dev_id = ucontrol->value.integer.value[0];
	int max = tas_priv->ndev - 1;

	dev_id = clamp(dev_id, 0, max);

	guard(mutex)(&tas_priv->codec_lock);
	return tasdev_chn_switch(tas_priv, dev_id);
}

static int tasdevice_dsp_create_ctrls(struct tasdevice_priv *tas_priv)
{
	struct snd_kcontrol_new *dsp_ctrls;
	char *active_dev_num, *chip_id;
	char *conf_name, *prog_name;
	int nr_controls = 4;
	int mix_index = 0;
	int ret;

	/* Alloc kcontrol via devm_kzalloc, which don't manually
	 * free the kcontrol
	 */
	dsp_ctrls = devm_kcalloc(tas_priv->dev, nr_controls,
		sizeof(dsp_ctrls[0]), GFP_KERNEL);
	if (!dsp_ctrls) {
		ret = -ENOMEM;
		goto out;
	}

	/* Create mixer items for selecting the active Program and Config */
	prog_name = devm_kstrdup(tas_priv->dev, "Speaker Program Id",
		GFP_KERNEL);
	if (!prog_name) {
		ret = -ENOMEM;
		goto out;
	}
	dsp_ctrls[mix_index].name = prog_name;
	dsp_ctrls[mix_index].iface = SNDRV_CTL_ELEM_IFACE_MIXER;
	dsp_ctrls[mix_index].info = tasdevice_info_programs;
	dsp_ctrls[mix_index].get = tasdevice_program_get;
	dsp_ctrls[mix_index].put = tasdevice_program_put;
	mix_index++;

	conf_name = devm_kstrdup(tas_priv->dev, "Speaker Config Id",
		GFP_KERNEL);
	if (!conf_name) {
		ret = -ENOMEM;
		goto out;
	}
	dsp_ctrls[mix_index].name = conf_name;
	dsp_ctrls[mix_index].iface = SNDRV_CTL_ELEM_IFACE_MIXER;
	dsp_ctrls[mix_index].info = tasdevice_info_configurations;
	dsp_ctrls[mix_index].get = tasdevice_configuration_get;
	dsp_ctrls[mix_index].put = tasdevice_configuration_put;
	mix_index++;

	active_dev_num = devm_kstrdup(tas_priv->dev, "Activate Tasdevice Num",
		GFP_KERNEL);
	if (!active_dev_num) {
		ret = -ENOMEM;
		goto out;
	}
	dsp_ctrls[mix_index].name = active_dev_num;
	dsp_ctrls[mix_index].iface = SNDRV_CTL_ELEM_IFACE_MIXER;
	dsp_ctrls[mix_index].info = tasdevice_info_active_num;
	dsp_ctrls[mix_index].get = tasdevice_active_num_get;
	dsp_ctrls[mix_index].put = tasdevice_active_num_put;
	mix_index++;

	chip_id = devm_kstrdup(tas_priv->dev, "Tasdevice Chip Id", GFP_KERNEL);
	if (!chip_id) {
		ret = -ENOMEM;
		goto out;
	}
	dsp_ctrls[mix_index].name = chip_id;
	dsp_ctrls[mix_index].iface = SNDRV_CTL_ELEM_IFACE_MIXER;
	dsp_ctrls[mix_index].info = tasdevice_info_chip_id;
	dsp_ctrls[mix_index].get = tasdevice_get_chip_id;
	mix_index++;

	ret = snd_soc_add_component_controls(tas_priv->codec, dsp_ctrls,
		nr_controls < mix_index ? nr_controls : mix_index);

out:
	return ret;
}

static int tasdevice_create_cali_ctrls(struct tasdevice_priv *priv)
{
	struct calidata *cali_data = &priv->cali_data;
	struct tasdevice *tasdev = priv->tasdevice;
	struct soc_bytes_ext *ext_cali_data;
	struct snd_kcontrol_new *cali_ctrls;
	unsigned int nctrls;
	char *cali_name;
	int rc, i;

	rc = snd_soc_add_component_controls(priv->codec,
		tasdevice_cali_controls, ARRAY_SIZE(tasdevice_cali_controls));
	if (rc < 0) {
		dev_err(priv->dev, "%s: Add cali controls err rc = %d",
			__func__, rc);
		return rc;
	}

	if (priv->chip_id == TAS2781) {
		cali_ctrls = (struct snd_kcontrol_new *)tas2781_cali_controls;
		nctrls = ARRAY_SIZE(tas2781_cali_controls);
		for (i = 0; i < priv->ndev; i++) {
			tasdev[i].cali_data_backup =
				kmemdup(tas2781_cali_start_reg,
				sizeof(tas2781_cali_start_reg), GFP_KERNEL);
			if (!tasdev[i].cali_data_backup)
				return -ENOMEM;
		}
	} else {
		cali_ctrls = (struct snd_kcontrol_new *)tas2563_cali_controls;
		nctrls = ARRAY_SIZE(tas2563_cali_controls);
		for (i = 0; i < priv->ndev; i++) {
			tasdev[i].cali_data_backup =
				kmemdup(tas2563_cali_start_reg,
				sizeof(tas2563_cali_start_reg), GFP_KERNEL);
			if (!tasdev[i].cali_data_backup)
				return -ENOMEM;
		}
	}

	rc = snd_soc_add_component_controls(priv->codec, cali_ctrls, nctrls);
	if (rc < 0) {
		dev_err(priv->dev, "%s: Add chip cali ctrls err rc = %d",
			__func__, rc);
		return rc;
	}

	/* index for cali_ctrls */
	i = 0;
	if (priv->chip_id == TAS2781)
		nctrls = 2;
	else
		nctrls = 1;

	/*
	 * Alloc kcontrol via devm_kzalloc(), which don't manually
	 * free the kcontrol。
	 */
	cali_ctrls = devm_kcalloc(priv->dev, nctrls,
		sizeof(cali_ctrls[0]), GFP_KERNEL);
	if (!cali_ctrls)
		return -ENOMEM;

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

	cali_name = devm_kstrdup(priv->dev, "Speaker Calibrated Data",
		GFP_KERNEL);
	if (!cali_name)
		return -ENOMEM;
	/* the number of calibrated data per tas2563/tas2781 */
	cali_data->cali_dat_sz_per_dev = 20;
	/*
	 * Data structure for tas2563/tas2781 calibrated data:
	 *	Pkg len (1 byte)
	 *	Reg id (1 byte, constant 'r')
	 *	book, page, register array for calibrated data (15 bytes)
	 *	for (i = 0; i < Device-Sum; i++) {
	 *		Device #i index_info (1 byte)
	 *		Calibrated data for Device #i (20 bytes)
	 *	}
	 */
	ext_cali_data->max = priv->ndev *
		(cali_data->cali_dat_sz_per_dev + 1) + 1 + 15 + 1;
	priv->cali_data.total_sz = priv->ndev *
		(cali_data->cali_dat_sz_per_dev + 1);
	priv->cali_data.data = devm_kzalloc(priv->dev,
		ext_cali_data->max, GFP_KERNEL);
	cali_ctrls[i].name = cali_name;
	cali_ctrls[i].iface = SNDRV_CTL_ELEM_IFACE_MIXER;
	cali_ctrls[i].info = snd_soc_bytes_info_ext;
	cali_ctrls[i].get = tasdev_cali_data_get;
	cali_ctrls[i].put = tasdev_cali_data_put;
	cali_ctrls[i].private_value = (unsigned long)ext_cali_data;
	i++;

	cali_data->data = devm_kzalloc(priv->dev, cali_data->total_sz,
		GFP_KERNEL);
	if (!cali_data->data)
		return -ENOMEM;

	if (priv->chip_id == TAS2781) {
		struct soc_bytes_ext *ext_cali_start;
		char *cali_start_name;

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

		cali_start_name = devm_kstrdup(priv->dev,
			"Calibration Start", GFP_KERNEL);
		if (!cali_start_name)
			return -ENOMEM;
		/*
		 * package structure for tas2781 ftc start:
		 *	Pkg len (1 byte)
		 *	Reg id (1 byte, constant 'r')
		 *	book, page, register for pilot threshold, pilot tone
		 *		and sine gain (12 bytes)
		 *	for (i = 0; i < Device-Sum; i++) {
		 *		Device #i index_info (1 byte)
		 *		Sine gain for Device #i (8 bytes)
		 *	}
		 */
		ext_cali_start->max = 14 + priv->ndev * 9;
		cali_ctrls[i].name = cali_start_name;
		cali_ctrls[i].iface = SNDRV_CTL_ELEM_IFACE_MIXER;
		cali_ctrls[i].info = snd_soc_bytes_info_ext;
		cali_ctrls[i].put = tas2781_calib_start_put;
		cali_ctrls[i].get = tasdev_nop_get;
		cali_ctrls[i].private_value = (unsigned long)ext_cali_start;
		i++;
	}

	return snd_soc_add_component_controls(priv->codec, cali_ctrls,
		nctrls < i ? nctrls : i);
}

static void tasdevice_fw_ready(const struct firmware *fmw,
	void *context)
{
	struct tasdevice_priv *tas_priv = context;
	int ret = 0;
	int i;

	mutex_lock(&tas_priv->codec_lock);

	ret = tasdevice_rca_parser(tas_priv, fmw);
	if (ret) {
		tasdevice_config_info_remove(tas_priv);
		goto out;
	}
	tasdevice_create_control(tas_priv);

	tasdevice_dsp_remove(tas_priv);
	tasdevice_calbin_remove(tas_priv);
	/*
	 * The baseline is the RCA-only case, and then the code attempts to
	 * load DSP firmware but in case of failures just keep going, i.e.
	 * failing to load DSP firmware is NOT an error.
	 */
	tas_priv->fw_state = TASDEVICE_RCA_FW_OK;
	if (tas_priv->name_prefix)
		scnprintf(tas_priv->coef_binaryname, 64, "%s-%s_coef.bin",
			tas_priv->name_prefix, tas_priv->dev_name);
	else
		scnprintf(tas_priv->coef_binaryname, 64, "%s_coef.bin",
			tas_priv->dev_name);
	ret = tasdevice_dsp_parser(tas_priv);
	if (ret) {
		dev_err(tas_priv->dev, "dspfw load %s error\n",
			tas_priv->coef_binaryname);
		goto out;
	}

	/*
	 * If no dsp-related kcontrol created, the dsp resource will be freed.
	 */
	ret = tasdevice_dsp_create_ctrls(tas_priv);
	if (ret) {
		dev_err(tas_priv->dev, "dsp controls error\n");
		goto out;
	}

	ret = tasdevice_create_cali_ctrls(tas_priv);
	if (ret) {
		dev_err(tas_priv->dev, "cali controls error\n");
		goto out;
	}

	tas_priv->fw_state = TASDEVICE_DSP_FW_ALL_OK;

	/* If calibrated data occurs error, dsp will still works with default
	 * calibrated data inside algo.
	 */
	for (i = 0; i < tas_priv->ndev; i++) {
		if (tas_priv->name_prefix)
			scnprintf(tas_priv->cal_binaryname[i], 64,
				"%s-%s_cal_0x%02x.bin", tas_priv->name_prefix,
				tas_priv->dev_name,
				tas_priv->tasdevice[i].dev_addr);
		else
			scnprintf(tas_priv->cal_binaryname[i], 64,
				"%s_cal_0x%02x.bin", tas_priv->dev_name,
				tas_priv->tasdevice[i].dev_addr);
		ret = tas2781_load_calibration(tas_priv,
			tas_priv->cal_binaryname[i], i);
		if (ret != 0)
			dev_err(tas_priv->dev,
				"%s: load %s error, default will effect\n",
				__func__, tas_priv->cal_binaryname[i]);
	}

	tasdevice_prmg_load(tas_priv, 0);
	tas_priv->cur_prog = 0;
out:
	if (tas_priv->fw_state == TASDEVICE_RCA_FW_OK) {
		/* If DSP FW fail, DSP kcontrol won't be created. */
		tasdevice_dsp_remove(tas_priv);
	}
	mutex_unlock(&tas_priv->codec_lock);
	if (fmw)
		release_firmware(fmw);
}

static int tasdevice_dapm_event(struct snd_soc_dapm_widget *w,
			struct snd_kcontrol *kcontrol, int event)
{
	struct snd_soc_component *codec = snd_soc_dapm_to_component(w->dapm);
	struct tasdevice_priv *tas_priv = snd_soc_component_get_drvdata(codec);
	int state = 0;

	/* Codec Lock Hold */
	mutex_lock(&tas_priv->codec_lock);
	if (event == SND_SOC_DAPM_PRE_PMD)
		state = 1;
	tasdevice_tuning_switch(tas_priv, state);
	/* Codec Lock Release*/
	mutex_unlock(&tas_priv->codec_lock);

	return 0;
}

static const struct snd_soc_dapm_widget tasdevice_dapm_widgets[] = {
	SND_SOC_DAPM_AIF_IN("ASI", "ASI Playback", 0, SND_SOC_NOPM, 0, 0),
	SND_SOC_DAPM_AIF_OUT_E("ASI OUT", "ASI Capture", 0, SND_SOC_NOPM,
		0, 0, tasdevice_dapm_event,
		SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
	SND_SOC_DAPM_SPK("SPK", tasdevice_dapm_event),
	SND_SOC_DAPM_OUTPUT("OUT"),
	SND_SOC_DAPM_INPUT("DMIC"),
};

static const struct snd_soc_dapm_route tasdevice_audio_map[] = {
	{"SPK", NULL, "ASI"},
	{"OUT", NULL, "SPK"},
	{"ASI OUT", NULL, "DMIC"},
};

static int tasdevice_startup(struct snd_pcm_substream *substream,
						struct snd_soc_dai *dai)
{
	struct snd_soc_component *codec = dai->component;
	struct tasdevice_priv *tas_priv = snd_soc_component_get_drvdata(codec);

	switch (tas_priv->fw_state) {
	case TASDEVICE_RCA_FW_OK:
	case TASDEVICE_DSP_FW_ALL_OK:
		return 0;
	default:
		return -EINVAL;
	}
}

static int tasdevice_hw_params(struct snd_pcm_substream *substream,
	struct snd_pcm_hw_params *params, struct snd_soc_dai *dai)
{
	struct tasdevice_priv *tas_priv = snd_soc_dai_get_drvdata(dai);
	unsigned int slot_width;
	unsigned int fsrate;
	int bclk_rate;
	int rc = 0;

	fsrate = params_rate(params);
	switch (fsrate) {
	case 48000:
	case 44100:
		break;
	default:
		dev_err(tas_priv->dev, "%s: incorrect sample rate = %u\n",
			__func__, fsrate);
		rc = -EINVAL;
		goto out;
	}

	slot_width = params_width(params);
	switch (slot_width) {
	case 16:
	case 20:
	case 24:
	case 32:
		break;
	default:
		dev_err(tas_priv->dev, "%s: incorrect slot width = %u\n",
			__func__, slot_width);
		rc = -EINVAL;
		goto out;
	}

	bclk_rate = snd_soc_params_to_bclk(params);
	if (bclk_rate < 0) {
		dev_err(tas_priv->dev, "%s: incorrect bclk rate = %d\n",
			__func__, bclk_rate);
		rc = bclk_rate;
		goto out;
	}

out:
	return rc;
}

static int tasdevice_set_dai_sysclk(struct snd_soc_dai *codec_dai,
	int clk_id, unsigned int freq, int dir)
{
	struct tasdevice_priv *tas_priv = snd_soc_dai_get_drvdata(codec_dai);

	tas_priv->sysclk = freq;

	return 0;
}

static const struct snd_soc_dai_ops tasdevice_dai_ops = {
	.startup = tasdevice_startup,
	.hw_params = tasdevice_hw_params,
	.set_sysclk = tasdevice_set_dai_sysclk,
};

static struct snd_soc_dai_driver tasdevice_dai_driver[] = {
	{
		.name = "tasdev_codec",
		.id = 0,
		.playback = {
			.stream_name = "Playback",
			.channels_min = 1,
			.channels_max = 4,
			.rates	 = TASDEVICE_RATES,
			.formats	= TASDEVICE_FORMATS,
		},
		.capture = {
			.stream_name = "Capture",
			.channels_min = 1,
			.channels_max = 4,
			.rates	 = TASDEVICE_RATES,
			.formats	= TASDEVICE_FORMATS,
		},
		.ops = &tasdevice_dai_ops,
		.symmetric_rate = 1,
	},
};

static int tasdevice_codec_probe(struct snd_soc_component *codec)
{
	struct tasdevice_priv *tas_priv = snd_soc_component_get_drvdata(codec);
	struct snd_kcontrol_new *p;
	unsigned int size;
	int rc;

	switch (tas_priv->chip_id) {
	case TAS2781:
		p = (struct snd_kcontrol_new *)tas2781_snd_controls;
		size = ARRAY_SIZE(tas2781_snd_controls);
		break;
	default:
		p = (struct snd_kcontrol_new *)tas2563_snd_controls;
		size = ARRAY_SIZE(tas2563_snd_controls);
	}

	rc = snd_soc_add_component_controls(codec, p, size);
	if (rc < 0) {
		dev_err(tas_priv->dev, "%s: Add control err rc = %d",
			__func__, rc);
		return rc;
	}

	tas_priv->name_prefix = codec->name_prefix;
	return tascodec_init(tas_priv, codec, THIS_MODULE, tasdevice_fw_ready);
}

static void tasdevice_deinit(void *context)
{
	struct tasdevice_priv *tas_priv = (struct tasdevice_priv *) context;
	struct tasdevice *tasdev = tas_priv->tasdevice;
	int i;

	for (i = 0; i < tas_priv->ndev; i++)
		kfree(tasdev[i].cali_data_backup);

	tasdevice_config_info_remove(tas_priv);
	tasdevice_dsp_remove(tas_priv);
	tasdevice_calbin_remove(tas_priv);
	tas_priv->fw_state = TASDEVICE_DSP_FW_PENDING;
}

static void tasdevice_codec_remove(struct snd_soc_component *codec)
{
	struct tasdevice_priv *tas_priv = snd_soc_component_get_drvdata(codec);

	tasdevice_deinit(tas_priv);
}

static const struct snd_soc_component_driver
	soc_codec_driver_tasdevice = {
	.probe			= tasdevice_codec_probe,
	.remove			= tasdevice_codec_remove,
	.controls		= tasdevice_snd_controls,
	.num_controls		= ARRAY_SIZE(tasdevice_snd_controls),
	.dapm_widgets		= tasdevice_dapm_widgets,
	.num_dapm_widgets	= ARRAY_SIZE(tasdevice_dapm_widgets),
	.dapm_routes		= tasdevice_audio_map,
	.num_dapm_routes	= ARRAY_SIZE(tasdevice_audio_map),
	.idle_bias_on		= 1,
	.endianness		= 1,
};

static void tasdevice_parse_dt(struct tasdevice_priv *tas_priv)
{
	struct i2c_client *client = (struct i2c_client *)tas_priv->client;
	unsigned int dev_addrs[TASDEVICE_MAX_CHANNELS];
	int i, ndev = 0;

	if (tas_priv->isacpi) {
		ndev = device_property_read_u32_array(&client->dev,
			"ti,audio-slots", NULL, 0);
		if (ndev <= 0) {
			ndev = 1;
			dev_addrs[0] = client->addr;
		} else {
			ndev = (ndev < ARRAY_SIZE(dev_addrs))
				? ndev : ARRAY_SIZE(dev_addrs);
			ndev = device_property_read_u32_array(&client->dev,
				"ti,audio-slots", dev_addrs, ndev);
		}

		tas_priv->irq =
			acpi_dev_gpio_irq_get(ACPI_COMPANION(&client->dev), 0);
	} else if (IS_ENABLED(CONFIG_OF)) {
		struct device_node *np = tas_priv->dev->of_node;
		u64 addr;

		for (i = 0; i < TASDEVICE_MAX_CHANNELS; i++) {
			if (of_property_read_reg(np, i, &addr, NULL))
				break;
			dev_addrs[ndev++] = addr;
		}

		tas_priv->irq = of_irq_get(np, 0);
	} else {
		ndev = 1;
		dev_addrs[0] = client->addr;
	}
	tas_priv->ndev = ndev;
	for (i = 0; i < ndev; i++)
		tas_priv->tasdevice[i].dev_addr = dev_addrs[i];

	tas_priv->reset = devm_gpiod_get_optional(&client->dev,
			"reset", GPIOD_OUT_HIGH);
	if (IS_ERR(tas_priv->reset))
		dev_err(tas_priv->dev, "%s Can't get reset GPIO\n",
			__func__);

	strcpy(tas_priv->dev_name, tasdevice_id[tas_priv->chip_id].name);
}

static int tasdevice_i2c_probe(struct i2c_client *i2c)
{
	const struct i2c_device_id *id = i2c_match_id(tasdevice_id, i2c);
	const struct acpi_device_id *acpi_id;
	struct tasdevice_priv *tas_priv;
	int ret;

	tas_priv = tasdevice_kzalloc(i2c);
	if (!tas_priv)
		return -ENOMEM;

	dev_set_drvdata(&i2c->dev, tas_priv);

	if (ACPI_HANDLE(&i2c->dev)) {
		acpi_id = acpi_match_device(i2c->dev.driver->acpi_match_table,
				&i2c->dev);
		if (!acpi_id) {
			dev_err(&i2c->dev, "No driver data\n");
			ret = -EINVAL;
			goto err;
		}
		tas_priv->chip_id = acpi_id->driver_data;
		tas_priv->isacpi = true;
	} else {
		tas_priv->chip_id = id ? id->driver_data : 0;
		tas_priv->isacpi = false;
	}

	tasdevice_parse_dt(tas_priv);

	ret = tasdevice_init(tas_priv);
	if (ret)
		goto err;

	tasdevice_reset(tas_priv);

	ret = devm_snd_soc_register_component(tas_priv->dev,
		&soc_codec_driver_tasdevice,
		tasdevice_dai_driver, ARRAY_SIZE(tasdevice_dai_driver));
	if (ret) {
		dev_err(tas_priv->dev, "%s: codec register error:0x%08x\n",
			__func__, ret);
		goto err;
	}
err:
	if (ret < 0)
		tasdevice_remove(tas_priv);
	return ret;
}

static void tasdevice_i2c_remove(struct i2c_client *client)
{
	struct tasdevice_priv *tas_priv = i2c_get_clientdata(client);

	tasdevice_remove(tas_priv);
}

#ifdef CONFIG_ACPI
static const struct acpi_device_id tasdevice_acpi_match[] = {
	{ "TAS2781", TAS2781 },
	{},
};

MODULE_DEVICE_TABLE(acpi, tasdevice_acpi_match);
#endif

static struct i2c_driver tasdevice_i2c_driver = {
	.driver = {
		.name = "tasdev-codec",
		.of_match_table = of_match_ptr(tasdevice_of_match),
#ifdef CONFIG_ACPI
		.acpi_match_table = ACPI_PTR(tasdevice_acpi_match),
#endif
	},
	.probe	= tasdevice_i2c_probe,
	.remove = tasdevice_i2c_remove,
	.id_table = tasdevice_id,
};

module_i2c_driver(tasdevice_i2c_driver);

MODULE_AUTHOR("Shenghao Ding <shenghao-ding@ti.com>");
MODULE_AUTHOR("Kevin Lu <kevin-lu@ti.com>");
MODULE_DESCRIPTION("ASoC TAS2781 Driver");
MODULE_LICENSE("GPL");
MODULE_IMPORT_NS("SND_SOC_TAS2781_FMWLIB");