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
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
|
/*
* Copyright (c) 2012-2015, The Linux Foundation. All rights reserved.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 and
* only version 2 as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/
#include "tfa_service.h"
#include "tfa_container.h"
#include "config.h"
#include "tfa.h"
#include "tfa_dsp_fw.h"
#include "tfa98xx_tfafieldnames.h"
/* module globals */
static int tfa98xx_cnt_verbose;
static nxpTfaContainer_t *g_cont; /* = NULL;*/ /* container file */
static int g_devs = -1; /* nr of devices TODO use direct access to cont? */
static nxpTfaDeviceList_t *g_dev[TFACONT_MAXDEVS];
static int g_profs[TFACONT_MAXDEVS];
static int g_liveds[TFACONT_MAXDEVS];
static nxpTfaProfileList_t *g_prof[TFACONT_MAXDEVS][TFACONT_MAXPROFS];
static nxpTfaLiveDataList_t *g_lived[TFACONT_MAXDEVS][TFACONT_MAXPROFS];
static int nxp_tfa_vstep[TFACONT_MAXDEVS];
static char errorname[] = "!ERROR!";
static char nonename[] = "NONE";
static void cont_get_devs(nxpTfaContainer_t *cont);
static int float_to_int(uint32_t x)
{
unsigned e = (0x7F + 31) - ((*(unsigned *) &x & 0x7F800000) >> 23);
unsigned m = 0x80000000 | (*(unsigned *) &x << 8);
return -(int)((m >> e) & -(e < 32));
}
/*
* check the container file and set module global
*/
enum tfa_error tfa_load_cnt(void *cnt, int length)
{
nxpTfaContainer_t *cntbuf = (nxpTfaContainer_t *)cnt;
g_cont = NULL;
if (length > TFA_MAX_CNT_LENGTH)
return tfa_error_container;
if ((HDR(cntbuf->id[0], cntbuf->id[1])) != paramsHdr) {
pr_err("wrong header type: 0x%02x 0x%02x\n", cntbuf->id[0],
cntbuf->id[1]);
return tfa_error_container;
}
/* check CRC */
if (tfaContCrcCheckContainer(cntbuf)) {
pr_err("CRC error\n");
return tfa_error_container;
}
/* check sub version level */
if ((cntbuf->subversion[1] == NXPTFA_PM_SUBVERSION) &&
(cntbuf->subversion[0] == '0')) {
g_cont = cntbuf;
cont_get_devs(g_cont);
} else {
pr_err("container sub-version not supported: %c%c\n",
cntbuf->subversion[0], cntbuf->subversion[1]);
return tfa_error_container;
}
return tfa_error_ok;
}
void tfa_deinit(void)
{
g_cont = NULL;
g_devs = -1;
}
/*
* Set the debug option
*/
void tfa_cnt_verbose(int level)
{
tfa98xx_cnt_verbose = level;
}
/* start count from 1, 0 is invalid */
void tfaContSetCurrentVstep(int channel, int vstep_idx)
{
if (channel < TFACONT_MAXDEVS)
nxp_tfa_vstep[channel] = vstep_idx+1;
else
pr_err("channel nr %d>%d\n", channel, TFACONT_MAXDEVS-1);
}
/* start count from 1, 0 is invalid */
int tfaContGetCurrentVstep(int channel)
{
if (channel < TFACONT_MAXDEVS)
return nxp_tfa_vstep[channel]-1;
pr_err("channel nr %d>%d\n", channel, TFACONT_MAXDEVS-1);
return (-1);
}
nxpTfaContainer_t *tfa98xx_get_cnt(void)
{
return g_cont;
}
/*
* Dump the contents of the file header
*/
void tfaContShowHeader(nxpTfaHeader_t *hdr)
{
char _id[2];
pr_debug("File header\n");
_id[1] = hdr->id >> 8;
_id[0] = hdr->id & 0xff;
pr_debug("\tid:%.2s version:%.2s subversion:%.2s\n", _id,
hdr->version, hdr->subversion);
pr_debug("\tsize:%d CRC:0x%08x \n", hdr->size, hdr->CRC);
pr_debug("\tcustomer:%.8s application:%.8s type:%.8s\n", hdr->customer,
hdr->application, hdr->type);
}
/*
* return device list dsc from index
*/
nxpTfaDeviceList_t *tfaContGetDevList(nxpTfaContainer_t *cont, int dev_idx)
{
uint8_t *base = (uint8_t *) cont;
if ((dev_idx < 0) || (dev_idx >= cont->ndev))
return NULL;
if (cont->index[dev_idx].type != dscDevice)
return NULL;
base += cont->index[dev_idx].offset;
return (nxpTfaDeviceList_t *) base;
}
/*
* get the Nth profile for the Nth device
*/
nxpTfaProfileList_t *tfaContGetDevProfList(nxpTfaContainer_t *cont, int devIdx,
int profIdx)
{
nxpTfaDeviceList_t *dev;
int idx, hit;
uint8_t *base = (uint8_t *) cont;
dev = tfaContGetDevList(cont, devIdx);
if (dev) {
for (idx = 0, hit = 0; idx < dev->length; idx++) {
if (dev->list[idx].type == dscProfile) {
if (profIdx == hit++)
return (nxpTfaProfileList_t *) (dev->
list
[idx].
offset +
base);
}
}
}
return NULL;
}
/*
* get the Nth lifedata for the Nth device
*/
nxpTfaLiveDataList_t *tfaContGetDevLiveDataList(nxpTfaContainer_t *cont, int devIdx,
int lifeDataIdx)
{
nxpTfaDeviceList_t *dev;
int idx, hit;
uint8_t *base = (uint8_t *) cont;
dev = tfaContGetDevList(cont, devIdx);
if (dev) {
for (idx = 0, hit = 0; idx < dev->length; idx++) {
if (dev->list[idx].type == dscLiveData) {
if (lifeDataIdx == hit++)
return (nxpTfaLiveDataList_t *)(dev->list[idx].offset + base);
}
}
}
return NULL;
}
/*
* Get the max volume step associated with Nth profile for the Nth device
*/
int tfacont_get_max_vstep(int dev_idx, int prof_idx)
{
nxpTfaVolumeStep2File_t *vp;
struct nxpTfaVolumeStepMax2File *vp3;
int vstep_count = 0;
vp = (nxpTfaVolumeStep2File_t *) tfacont_getfiledata(dev_idx, prof_idx, volstepHdr);
if (vp == NULL)
return 0;
/* check the header type to load different NrOfVStep appropriately */
if (tfa98xx_dev_family(dev_idx) == 2) {
/* this is actually tfa2, so re-read the buffer*/
vp3 = (struct nxpTfaVolumeStepMax2File *)
tfacont_getfiledata(dev_idx, prof_idx, volstepHdr);
if (vp3) {
vstep_count = vp3->NrOfVsteps;
}
} else {
/* this is max1*/
if (vp) {
vstep_count = vp->vsteps;
}
}
return vstep_count;
}
/**
* Get the file contents associated with the device or profile
* Search within the device tree, if not found, search within the profile
* tree. There can only be one type of file within profile or device.
*/
nxpTfaFileDsc_t *tfacont_getfiledata(int dev_idx, int prof_idx, enum nxpTfaHeaderType type)
{
nxpTfaDeviceList_t *dev;
nxpTfaProfileList_t *prof;
nxpTfaFileDsc_t *file;
nxpTfaHeader_t *hdr;
unsigned int i;
if (g_cont == 0)
return NULL;
dev = tfaContGetDevList(g_cont, dev_idx);
if (dev == 0)
return NULL;
/* process the device list until a file type is encountered */
for (i = 0; i < dev->length; i++) {
if (dev->list[i].type == dscFile) {
file = (nxpTfaFileDsc_t *)(dev->list[i].offset+(uint8_t *)g_cont);
hdr = (nxpTfaHeader_t *)file->data;
/* check for file type */
if (hdr->id == type) {
/* pr_debug("%s: file found of type %d in device %s \n", __FUNCTION__, type, tfaContDeviceName(devIdx)); */
return (nxpTfaFileDsc_t *)&file->data;
}
}
}
/* File not found in device tree.
* So, look in the profile list until the file type is encountered
*/
prof = tfaContGetDevProfList(g_cont, dev_idx, prof_idx);
for (i = 0; i < prof->length; i++) {
if (prof->list[i].type == dscFile) {
file = (nxpTfaFileDsc_t *)(prof->list[i].offset+(uint8_t *)g_cont);
hdr = (nxpTfaHeader_t *)file->data;
/* check for file type */
if (hdr->id == type) {
/* pr_debug("%s: file found of type %d in profile %s\n", __FUNCTION__, type, tfaContProfileName(devIdx, profIdx)); */
return (nxpTfaFileDsc_t *)&file->data;
}
}
}
if (tfa98xx_cnt_verbose)
pr_debug("%s: no file found of type %d\n", __FUNCTION__, type);
return NULL;
}
/*
* fill globals
*/
static void cont_get_devs(nxpTfaContainer_t *cont)
{
nxpTfaProfileList_t *prof;
nxpTfaLiveDataList_t *liveD;
int i, j;
int count;
/* get nr of devlists+1 */
for (i = 0 ; i < cont->ndev ; i++) {
g_dev[i] = tfaContGetDevList(cont, i); /* cache it */
}
g_devs = cont->ndev;
/* walk through devices and get the profile lists */
for (i = 0; i < g_devs; i++) {
j = 0;
count = 0;
while ((prof = tfaContGetDevProfList(cont, i, j)) != NULL) {
count++;
g_prof[i][j++] = prof;
}
g_profs[i] = count; /* count the nr of profiles per device */
}
g_devs = cont->ndev;
/* walk through devices and get the livedata lists */
for (i = 0; i < g_devs; i++) {
j = 0;
count = 0;
while ((liveD = tfaContGetDevLiveDataList(cont, i, j)) != NULL) {
count++;
g_lived[i][j++] = liveD;
}
g_liveds[i] = count; /* count the nr of livedata per device */
}
}
static char nostring[] = "Undefined string";
/* TODO add to API */
#define MODULE_BIQUADFILTERBANK 2
#define BIQUAD_COEFF_SIZE 6
/*
* write a parameter file to the device
*/
static enum Tfa98xx_Error tfaContWriteVstep(int dev_idx, nxpTfaVolumeStep2File_t *vp, int vstep)
{
enum Tfa98xx_Error err;
float voldB = 0.0;
unsigned short vol;
if (vstep < vp->vsteps) {
voldB = vp->vstep[vstep].attenuation;
/* vol = (unsigned short)(voldB / (-0.5f)); */
vol = (unsigned short)(-2 * float_to_int(*((uint32_t *)&voldB)));
if (vol > 255) /* restricted to 8 bits */
vol = 255;
err = tfa98xx_set_volume_level(dev_idx, vol);
if (err != Tfa98xx_Error_Ok)
return err;
err = tfa98xx_dsp_write_preset(dev_idx, sizeof(vp->vstep[0].preset), vp->vstep[vstep].preset);
if (err != Tfa98xx_Error_Ok)
return err;
err = tfa_cont_write_filterbank(dev_idx, vp->vstep[vstep].filter);
} else {
pr_err("Incorrect volume given. The value vstep[%d] >= %d\n", nxp_tfa_vstep[dev_idx] , vp->vsteps);
err = Tfa98xx_Error_Bad_Parameter;
}
if (tfa98xx_cnt_verbose)
pr_debug("vstep[%d][%d]\n", dev_idx, vstep);
return err;
}
static enum Tfa98xx_Error tfaContWriteVstepMax2(int dev_idx, nxpTfaVolumeStepMax2File_t *vp, int vstep_idx, int vstep_msg_idx)
{
enum Tfa98xx_Error err = Tfa98xx_Error_Ok;
struct nxpTfaVolumeStepRegisterInfo *regInfo = {0};
struct nxpTfaVolumeStepMessageInfo *msgInfo = {0};
nxpTfaBitfield_t bitF;
int msgLength = 0, i, j, size = 0, nrMessages, modified = 0;
uint8_t cmdid_changed[3];
if (vstep_idx >= vp->NrOfVsteps) {
pr_debug("Volumestep %d is not available \n", vstep_idx);
return Tfa98xx_Error_Bad_Parameter;
}
for (i = 0; i <= vstep_idx; i++) {
regInfo = (struct nxpTfaVolumeStepRegisterInfo *)(vp->vstepsBin + size);
msgInfo = (struct nxpTfaVolumeStepMessageInfo *)(vp->vstepsBin+
(regInfo->NrOfRegisters * sizeof(uint32_t)+sizeof(regInfo->NrOfRegisters)+size));
nrMessages = msgInfo->NrOfMessages;
for (j = 0; j < nrMessages; j++) {
/* location of message j, from vstep i */
msgInfo = (struct nxpTfaVolumeStepMessageInfo *)(vp->vstepsBin+
(regInfo->NrOfRegisters * sizeof(uint32_t)+sizeof(regInfo->NrOfRegisters)+size));
/* message length */
msgLength = ((msgInfo->MessageLength.b[0] << 16) + (msgInfo->MessageLength.b[1] << 8) + msgInfo->MessageLength.b[2]);
if (i == vstep_idx) {
/* If no vstepMsgIndex is passed on, all message needs to be send */
if ((vstep_msg_idx >= TFA_MAX_VSTEP_MSG_MARKER) || (vstep_msg_idx == j)) {
/*
* The algoparams and mbdrc msg id will be changed to the reset type when SBSL=0
* if SBSL=1 the msg will remain unchanged. It's up to the tuning engineer to choose the 'without_reset'
* types inside the vstep. In other words: the reset msg is applied during SBSL==0 else it remains unchanged.
*/
if (TFA_GET_BF(dev_idx, SBSL) == 0) {
if (msgInfo->MessageType == 0) { /* If the messagetype(0) is AlgoParams */
/* Only do this when not set already */
if (msgInfo->CmdId[2] != SB_PARAM_SET_ALGO_PARAMS) {
cmdid_changed[0] = msgInfo->CmdId[0];
cmdid_changed[1] = msgInfo->CmdId[1];
cmdid_changed[2] = SB_PARAM_SET_ALGO_PARAMS;
modified = 1;
}
} else if (msgInfo->MessageType == 2) { /* If the messagetype(2) is MBDrc */
/* Only do this when not set already */
if (msgInfo->CmdId[2] != SB_PARAM_SET_MBDRC) {
cmdid_changed[0] = msgInfo->CmdId[0];
cmdid_changed[1] = msgInfo->CmdId[1];
cmdid_changed[2] = SB_PARAM_SET_MBDRC;
modified = 1;
}
}
}
/* Messagetype(3) is Smartstudio Info! Dont send this! */
if (msgInfo->MessageType != 3) {
if (modified == 1) {
if (tfa98xx_cnt_verbose) {
if (cmdid_changed[2] == SB_PARAM_SET_ALGO_PARAMS)
pr_debug("P-ID for SetAlgoParams modified!: ");
else
pr_debug("P-ID for SetMBDrc modified!: ");
pr_debug("Command-ID used: 0x%02x%02x%02x \n",
cmdid_changed[0], cmdid_changed[1], cmdid_changed[2]);
}
/* Send payload to dsp (Remove 1 from the length for cmdid) */
err = tfa_dsp_msg_id(dev_idx, (msgLength-1) * 3, (const char *)msgInfo->ParameterData, cmdid_changed);
if (err != Tfa98xx_Error_Ok)
return err;
} else {
/* Send cmdId + payload to dsp */
err = tfa_dsp_msg(dev_idx, msgLength * 3, (const char *)msgInfo->CmdId);
if (err != Tfa98xx_Error_Ok)
return err;
}
/* Set back to zero every time */
modified = 0;
}
}
}
if (msgInfo->MessageType == 3) {
/* MessageLength is in bytes */
size += sizeof(msgInfo->MessageType) + sizeof(msgInfo->MessageLength) + msgLength;
} else {
/* MessageLength is in words (3 bytes) */
size += sizeof(msgInfo->MessageType) + sizeof(msgInfo->MessageLength) + sizeof(msgInfo->CmdId) + ((msgLength-1) * 3);
}
}
size += sizeof(regInfo->NrOfRegisters) + (regInfo->NrOfRegisters * sizeof(uint32_t)) + sizeof(msgInfo->NrOfMessages);
}
if (regInfo->NrOfRegisters == 0) {
pr_debug("No registers in selected vstep (%d)!\n", vstep_idx);
return Tfa98xx_Error_Bad_Parameter;
}
for (i = 0; i < regInfo->NrOfRegisters*2; i++) {
/* Byte swap the datasheetname */
bitF.field = (uint16_t)(regInfo->registerInfo[i]>>8) | (regInfo->registerInfo[i]<<8);
i++;
bitF.value = (uint16_t)regInfo->registerInfo[i]>>8;
err = tfaRunWriteBitfield(dev_idx , bitF);
if (err != Tfa98xx_Error_Ok)
return err;
}
/* Save the current vstep */
tfa_set_swvstep(dev_idx, (unsigned short)vstep_idx);
return err;
}
/*
* Write DRC message to the dsp
* If needed modify the cmd-id
*/
enum Tfa98xx_Error tfaContWriteDrcFile(int dev_idx, int size, uint8_t data[])
{
enum Tfa98xx_Error err = Tfa98xx_Error_Ok;
uint8_t cmdid_changed[3], modified = 0;
if (TFA_GET_BF(dev_idx, SBSL) == 0) {
/* Only do this when not set already */
if (data[2] != SB_PARAM_SET_MBDRC) {
cmdid_changed[0] = data[0];
cmdid_changed[1] = data[1];
cmdid_changed[2] = SB_PARAM_SET_MBDRC;
modified = 1;
if (tfa98xx_cnt_verbose) {
pr_debug("P-ID for SetMBDrc modified!: ");
pr_debug("Command-ID used: 0x%02x%02x%02x \n",
cmdid_changed[0], cmdid_changed[1], cmdid_changed[2]);
}
}
}
if (modified == 1) {
/* Send payload to dsp (Remove 3 from the length for cmdid) */
err = tfa_dsp_msg_id(dev_idx, size-3, (const char *)data, cmdid_changed);
} else {
/* Send cmdId + payload to dsp */
err = tfa_dsp_msg(dev_idx, size, (const char *)data);
}
return err;
}
/*
* write a parameter file to the device
* The VstepIndex and VstepMsgIndex are only used to write a specific msg from the vstep file.
*/
enum Tfa98xx_Error tfaContWriteFile(int dev_idx, nxpTfaFileDsc_t *file, int vstep_idx, int vstep_msg_idx)
{
enum Tfa98xx_Error err = Tfa98xx_Error_Ok;
nxpTfaHeader_t *hdr = (nxpTfaHeader_t *)file->data;
nxpTfaHeaderType_t type;
int size;
if (tfa98xx_cnt_verbose) {
tfaContShowHeader(hdr);
}
type = (nxpTfaHeaderType_t) hdr->id;
switch (type) {
case msgHdr: /* generic DSP message */
size = hdr->size - sizeof(nxpTfaMsgFile_t);
err = tfa_dsp_msg(dev_idx, size, (const char *)((nxpTfaMsgFile_t *)hdr)->data);
break;
case volstepHdr:
if (tfa98xx_dev_family(dev_idx) == 2) {
err = tfaContWriteVstepMax2(dev_idx, (nxpTfaVolumeStepMax2File_t *)hdr, vstep_idx, vstep_msg_idx);
} else {
err = tfaContWriteVstep(dev_idx, (nxpTfaVolumeStep2File_t *)hdr, vstep_idx);
}
/* If writing the vstep was succesfull, set new current vstep */
if (err == Tfa98xx_Error_Ok) {
tfaContSetCurrentVstep(dev_idx, vstep_idx);
}
break;
case speakerHdr:
if (tfa98xx_dev_family(dev_idx) == 2) {
/* Remove header and xml_id */
size = hdr->size - sizeof(struct nxpTfaSpkHeader) - sizeof(struct nxpTfaFWVer);
err = tfa_dsp_msg(dev_idx, size,
(const char *)(((nxpTfaSpeakerFile_t *)hdr)->data + (sizeof(struct nxpTfaFWVer))));
} else {
size = hdr->size - sizeof(nxpTfaSpeakerFile_t);
err = tfa98xx_dsp_write_speaker_parameters(dev_idx, size,
(const unsigned char *)((nxpTfaSpeakerFile_t *)hdr)->data);
}
break;
case presetHdr:
size = hdr->size - sizeof(nxpTfaPreset_t);
err = tfa98xx_dsp_write_preset(dev_idx, size, (const unsigned char *)((nxpTfaPreset_t *)hdr)->data);
break;
case equalizerHdr:
err = tfa_cont_write_filterbank(dev_idx, ((nxpTfaEqualizerFile_t *)hdr)->filter);
break;
case patchHdr:
size = hdr->size - sizeof(nxpTfaPatch_t); /* size is total length */
err = tfa_dsp_patch(dev_idx, size, (const unsigned char *) ((nxpTfaPatch_t *)hdr)->data);
break;
case configHdr:
size = hdr->size - sizeof(nxpTfaConfig_t);
err = tfa98xx_dsp_write_config(dev_idx, size, (const unsigned char *)((nxpTfaConfig_t *)hdr)->data);
break;
case drcHdr:
if (hdr->version[0] == NXPTFA_DR3_VERSION) {
/* Size is total size - hdrsize(36) - xmlversion(3) */
size = hdr->size - sizeof(nxpTfaDrc2_t);
err = tfaContWriteDrcFile(dev_idx, size, ((nxpTfaDrc2_t *)hdr)->data);
} else {
/*
* The DRC file is split as:
* 36 bytes for generic header (customer, application, and type)
* 127x3 (381) bytes first block contains the device and sample rate
* independent settings
* 127x3 (381) bytes block the device and sample rate specific values.
* The second block can always be recalculated from the first block,
* if vlsCal and the sample rate are known.
*/
/* size = hdr->size - sizeof(nxpTfaDrc_t); */
size = 381; /* fixed size for first block */
/* +381 is done to only send the second part of the drc block */
err = tfa98xx_dsp_write_drc(dev_idx, size, ((const unsigned char *)((nxpTfaDrc_t *)hdr)->data+381));
}
break;
case infoHdr:
/* Ignore */
break;
default:
pr_err("Header is of unknown type: 0x%x\n", type);
return Tfa98xx_Error_Bad_Parameter;
}
return err;
}
/**
* get the 1st of this dsc type this devicelist
*/
nxpTfaDescPtr_t *tfa_cnt_get_dsc(nxpTfaContainer_t *cnt, nxpTfaDescriptorType_t type, int dev_idx)
{
nxpTfaDeviceList_t *dev = tfaContDevice (dev_idx);
nxpTfaDescPtr_t *this;
int i;
if (!dev) {
return NULL;
}
/* process the list until a the type is encountered */
for (i = 0; i < dev->length; i++) {
if (dev->list[i].type == (uint32_t)type) {
this = (nxpTfaDescPtr_t *)(dev->list[i].offset+(uint8_t *)cnt);
return this;
}
}
return NULL;
}
/**
* get the device type from the patch in this devicelist
* - find the patch file for this devidx
* - return the devid from the patch or 0 if not found
*/
int tfa_cnt_get_devid(nxpTfaContainer_t *cnt, int dev_idx)
{
nxpTfaPatch_t *patchfile;
nxpTfaDescPtr_t *patchdsc;
uint8_t *patchheader;
unsigned short devid, checkaddress;
int checkvalue;
patchdsc = tfa_cnt_get_dsc(cnt, dscPatch, dev_idx);
patchdsc += 2; /* first the filename dsc and filesize, so skip them */
patchfile = (nxpTfaPatch_t *)patchdsc;
if (patchfile == NULL)
return 0;
patchheader = patchfile->data;
checkaddress = (patchheader[1] << 8) + patchheader[2];
checkvalue =
(patchheader[3] << 16) + (patchheader[4] << 8) + patchheader[5];
devid = patchheader[0];
if (checkaddress == 0xFFFF && checkvalue != 0xFFFFFF && checkvalue != 0) {
devid = patchheader[5]<<8 | patchheader[0]; /* full revid */
}
return devid;
}
/*
* get the slave for the device if it exists
*/
enum Tfa98xx_Error tfaContGetSlave(int dev_idx, uint8_t *slave_addr)
{
nxpTfaDeviceList_t *dev = tfaContDevice (dev_idx);
if (dev == 0) {
return Tfa98xx_Error_Bad_Parameter;
}
*slave_addr = dev->dev;
return Tfa98xx_Error_Ok;
}
/*
* write a bit field
*/
enum Tfa98xx_Error tfaRunWriteBitfield(Tfa98xx_handle_t dev_idx, nxpTfaBitfield_t bf)
{
enum Tfa98xx_Error error;
uint16_t value;
union {
uint16_t field;
nxpTfaBfEnum_t Enum;
} bfUni;
value = bf.value;
bfUni.field = bf.field;
#ifdef TFA_DEBUG
if (tfa98xx_cnt_verbose)
pr_debug("bitfield: %s=%d (0x%x[%d..%d]=0x%x)\n", tfaContBfName(bfUni.field, tfa98xx_dev_revision(dev_idx)), value,
bfUni.Enum.address, bfUni.Enum.pos, bfUni.Enum.pos+bfUni.Enum.len, value);
#endif
error = tfa_set_bf(dev_idx, bfUni.field, value);
return error;
}
/*
* read a bit field
*/
enum Tfa98xx_Error tfaRunReadBitfield(Tfa98xx_handle_t dev_idx, nxpTfaBitfield_t *bf)
{
enum Tfa98xx_Error error;
union {
uint16_t field;
nxpTfaBfEnum_t Enum;
} bfUni;
uint16_t regvalue, msk;
bfUni.field = bf->field;
error = tfa98xx_read_register16(dev_idx, (unsigned char)(bfUni.Enum.address), ®value);
if (error)
return error;
msk = ((1<<(bfUni.Enum.len+1))-1)<<bfUni.Enum.pos;
regvalue &= msk;
bf->value = regvalue>>bfUni.Enum.pos;
return error;
}
/*
dsp mem direct write
*/
enum Tfa98xx_Error tfaRunWriteDspMem(Tfa98xx_handle_t dev, nxpTfaDspMem_t *cfmem)
{
enum Tfa98xx_Error error = Tfa98xx_Error_Ok;
int i;
for (i = 0; i < cfmem->size; i++) {
if (tfa98xx_cnt_verbose)
pr_debug("dsp mem (%d): 0x%02x=0x%04x\n", cfmem->type, cfmem->address, cfmem->words[i]);
error = tfa98xx_dsp_write_mem_word(dev, cfmem->address++, cfmem->words[i], cfmem->type);
if (error)
return error;
}
return error;
}
/*
* write filter payload to DSP
* note that the data is in an aligned union for all filter variants
* the aa data is used but it's the same for all of them
*/
enum Tfa98xx_Error tfaRunWriteFilter(Tfa98xx_handle_t dev, nxpTfaContBiquad_t *bq)
{
enum Tfa98xx_Error error = Tfa98xx_Error_Ok;
enum Tfa98xx_DMEM dmem;
uint16_t address;
uint8_t data[3*3+sizeof(bq->aa.bytes)];
int i, channel = 0, runs = 1;
int8_t saved_index = bq->aa.index; /* This is used to set back the index */
/* Channel=1 is primary, Channel=2 is secondary*/
if (bq->aa.index > 100) {
bq->aa.index -= 100;
channel = 2;
} else if (bq->aa.index > 50) {
bq->aa.index -= 50;
channel = 1;
} else if (tfa98xx_dev_family(dev) == 2) {
runs = 2;
}
if (tfa98xx_cnt_verbose) {
if (channel == 2)
pr_debug("filter[%d,S]", bq->aa.index);
else if (channel == 1)
pr_debug("filter[%d,P]", bq->aa.index);
else
pr_debug("filter[%d]", bq->aa.index);
}
for (i = 0; i < runs; i++) {
if (runs == 2)
channel++;
/* get the target address for the filter on this device */
dmem = tfa98xx_filter_mem(dev, bq->aa.index, &address, channel);
if (dmem < 0)
return Tfa98xx_Error_Bad_Parameter;
/* send a DSP memory message that targets the devices specific memory for the filter
* msg params: which_mem, start_offset, num_words
*/
memset(data, 0, 3*3);
data[2] = dmem; /* output[0] = which_mem */
data[4] = address >> 8; /* output[1] = start_offset */
data[5] = address & 0xff;
data[8] = sizeof(bq->aa.bytes)/3; /*output[2] = num_words */
memcpy(&data[9], bq->aa.bytes, sizeof(bq->aa.bytes)); /* payload */
if (tfa98xx_dev_family(dev) == 2) {
error = tfa_dsp_cmd_id_write(dev, MODULE_FRAMEWORK, FW_PAR_ID_SET_MEMORY, sizeof(data), data);
} else {
error = tfa_dsp_cmd_id_write(dev, MODULE_FRAMEWORK, 4 /* param */ , sizeof(data), data);
}
}
#ifdef TFA_DEBUG
if (tfa98xx_cnt_verbose) {
if (bq->aa.index == 13) {
pr_debug("=%d,%.0f,%.2f \n",
bq->in.type, bq->in.cutOffFreq, bq->in.leakage);
} else if (bq->aa.index >= 10 && bq->aa.index <= 12) {
pr_debug("=%d,%.0f,%.1f,%.1f \n", bq->aa.type,
bq->aa.cutOffFreq, bq->aa.rippleDb, bq->aa.rolloff);
} else {
pr_debug("= unsupported filter index \n");
}
}
#endif
/* Because we can load the same filters multiple times
* For example: When we switch profile we re-write in operating mode.
* We then need to remember the index (primary, secondary or both)
*/
bq->aa.index = saved_index;
return error;
}
/*
* write the register based on the input address, value and mask
* only the part that is masked will be updated
*/
enum Tfa98xx_Error tfaRunWriteRegister(Tfa98xx_handle_t handle, nxpTfaRegpatch_t *reg)
{
enum Tfa98xx_Error error;
uint16_t value, newvalue;
if (tfa98xx_cnt_verbose)
pr_debug("register: 0x%02x=0x%04x (msk=0x%04x)\n", reg->address, reg->value, reg->mask);
error = tfa98xx_read_register16(handle, reg->address, &value);
if (error)
return error;
value &= ~reg->mask;
newvalue = reg->value & reg->mask;
value |= newvalue;
error = tfa98xx_write_register16(handle, reg->address, value);
return error;
}
/*
* return the bitfield
*/
nxpTfaBitfield_t tfaContDsc2Bf(nxpTfaDescPtr_t dsc)
{
uint32_t *ptr = (uint32_t *) (&dsc);
union {
nxpTfaBitfield_t bf;
uint32_t num;
} num_bf;
num_bf.num = *ptr; /* & TFA_BITFIELDDSCMSK; */
return num_bf.bf;
}
/* write reg and bitfield items in the devicelist to the target */
enum Tfa98xx_Error tfaContWriteRegsDev(int dev_idx)
{
nxpTfaDeviceList_t *dev = tfaContDevice (dev_idx);
nxpTfaBitfield_t *bitF;
int i;
enum Tfa98xx_Error err = Tfa98xx_Error_Ok;
if (!dev) {
return Tfa98xx_Error_Bad_Parameter;
}
/* process the list until a patch, file of profile is encountered */
for (i = 0; i < dev->length; i++) {
if (dev->list[i].type == dscPatch ||
dev->list[i].type == dscFile ||
dev->list[i].type == dscProfile)
break;
if (dev->list[i].type == dscBitfield) {
bitF = (nxpTfaBitfield_t *)(dev->list[i].offset+(uint8_t *)g_cont);
err = tfaRunWriteBitfield(dev_idx, *bitF);
}
if (dev->list[i].type == dscRegister) {
err = tfaRunWriteRegister(dev_idx,
(nxpTfaRegpatch_t *)(dev->list[i].offset+(char *)g_cont));
}
if (err)
break;
}
return err;
}
/* write reg and bitfield items in the profilelist the target */
enum Tfa98xx_Error tfaContWriteRegsProf(int dev_idx, int prof_idx)
{
nxpTfaProfileList_t *prof = tfaContProfile(dev_idx, prof_idx);
nxpTfaBitfield_t *bitf;
unsigned int i;
enum Tfa98xx_Error err = Tfa98xx_Error_Ok;
if (!prof) {
return Tfa98xx_Error_Bad_Parameter;
}
if (tfa98xx_cnt_verbose)
pr_debug("----- profile: %s (%d) -----\n", tfaContGetString(&prof->name), prof_idx);
/* process the list until the end of the profile or the default section */
for (i = 0; i < prof->length; i++) {
/* We only want to write the values before the default section when we switch profile */
if (prof->list[i].type == dscDefault)
break;
if (prof->list[i].type == dscBitfield) {
bitf = (nxpTfaBitfield_t *)(prof->list[i].offset+(uint8_t *)g_cont);
err = tfaRunWriteBitfield(dev_idx , *bitf);
}
if (prof->list[i].type == dscRegister) {
err = tfaRunWriteRegister(dev_idx, (nxpTfaRegpatch_t *)(!prof->list[i].offset+g_cont));
}
if (err)
break;
}
return err;
}
/* write patchfile in the devicelist to the target */
enum Tfa98xx_Error tfaContWritePatch(int dev_idx)
{
nxpTfaDeviceList_t *dev = tfaContDevice(dev_idx);
nxpTfaFileDsc_t *file;
nxpTfaPatch_t *patchfile;
int size;
int i;
if (!dev) {
return Tfa98xx_Error_Bad_Parameter;
}
/* process the list until a patch is encountered */
for (i = 0; i < dev->length; i++) {
if (dev->list[i].type == dscPatch) {
file = (nxpTfaFileDsc_t *)(dev->list[i].offset+(uint8_t *)g_cont);
patchfile = (nxpTfaPatch_t *)&file->data;
if (tfa98xx_cnt_verbose)
tfaContShowHeader(&patchfile->hdr);
size = patchfile->hdr.size - sizeof(nxpTfaPatch_t); /* size is total length */
return tfa_dsp_patch(dev_idx, size, (const unsigned char *) patchfile->data);
}
}
return Tfa98xx_Error_Bad_Parameter; /* patch not in the list */
}
/* write all param files in the devicelist to the target */
enum Tfa98xx_Error tfaContWriteFiles(int dev_idx)
{
nxpTfaDeviceList_t *dev = tfaContDevice(dev_idx);
nxpTfaFileDsc_t *file;
nxpTfaCmd_t *cmd;
enum Tfa98xx_Error err = Tfa98xx_Error_Ok;
char buffer[(MEMTRACK_MAX_WORDS * 3) + 3] = {0}; /* every word requires 3 bytes, and 3 is the msg */
int i, size = 0;
if (!dev) {
return Tfa98xx_Error_Bad_Parameter;
}
/* process the list and write all files */
for (i = 0; i < dev->length; i++) {
if (dev->list[i].type == dscFile) {
file = (nxpTfaFileDsc_t *)(dev->list[i].offset+(uint8_t *)g_cont);
if (tfaContWriteFile(dev_idx, file, 0 , TFA_MAX_VSTEP_MSG_MARKER)) {
return Tfa98xx_Error_Bad_Parameter;
}
}
if (dev->list[i].type == dscSetInputSelect ||
dev->list[i].type == dscSetOutputSelect ||
dev->list[i].type == dscSetProgramConfig ||
dev->list[i].type == dscSetLagW ||
dev->list[i].type == dscSetGains ||
dev->list[i].type == dscSetvBatFactors ||
dev->list[i].type == dscSetSensesCal ||
dev->list[i].type == dscSetSensesDelay ||
dev->list[i].type == dscSetMBDrc) {
create_dsp_buffer_msg((nxpTfaMsg_t *)
(dev->list[i].offset+(char *)g_cont), buffer, &size);
err = tfa_dsp_msg(dev_idx, size, buffer);
if (tfa98xx_cnt_verbose) {
pr_debug("command: %s=0x%02x%02x%02x \n",
tfaContGetCommandString(dev->list[i].type),
(unsigned char)buffer[0], (unsigned char)buffer[1], (unsigned char)buffer[2]);
}
}
if (dev->list[i].type == dscCmd) {
size = *(uint16_t *)(dev->list[i].offset+(char *)g_cont);
err = tfa_dsp_msg(dev_idx, size, dev->list[i].offset+2+(char *)g_cont);
if (tfa98xx_cnt_verbose) {
cmd = (nxpTfaCmd_t *)(dev->list[i].offset+(uint8_t *)g_cont);
pr_debug("Writing cmd=0x%02x%02x%02x \n", cmd->value[0], cmd->value[1], cmd->value[2]);
}
}
if (err != Tfa98xx_Error_Ok)
break;
if (dev->list[i].type == dscCfMem) {
err = tfaRunWriteDspMem(dev_idx, (nxpTfaDspMem_t *)(dev->list[i].offset+(uint8_t *)g_cont));
}
if (err != Tfa98xx_Error_Ok)
break;
}
return err;
}
/*
* write all param files in the profilelist to the target
* this is used during startup when maybe ACS is set
*/
enum Tfa98xx_Error tfaContWriteFilesProf(int dev_idx, int prof_idx, int vstep_idx)
{
enum Tfa98xx_Error err = Tfa98xx_Error_Ok;
nxpTfaProfileList_t *prof = tfaContProfile(dev_idx, prof_idx);
unsigned int i;
nxpTfaFileDsc_t *file;
nxpTfaPatch_t *patchfile;
int size;
if (!prof) {
return Tfa98xx_Error_Bad_Parameter;
}
/* process the list and write all files */
for (i = 0; i < prof->length; i++) {
switch (prof->list[i].type) {
case dscFile:
file = (nxpTfaFileDsc_t *)(prof->list[i].offset+(uint8_t *)g_cont);
err = tfaContWriteFile(dev_idx, file, vstep_idx, TFA_MAX_VSTEP_MSG_MARKER);
break;
case dscFilter:
/* Filters are not written during coldstart
* Since calibration, SBSL and many other actions can overwrite them again
* They are written during operating mode (tfa_start)
*/
/* err = tfaRunWriteFilter(dev_idx, (nxpTfaContBiquad_t *)(prof->list[i].offset+(uint8_t *)g_cont)); */
break;
case dscPatch:
file = (nxpTfaFileDsc_t *)(prof->list[i].offset+(uint8_t *)g_cont);
patchfile = (nxpTfaPatch_t *)&file->data;
if (tfa98xx_cnt_verbose)
tfaContShowHeader(&patchfile->hdr);
size = patchfile->hdr.size - sizeof(nxpTfaPatch_t); /* size is total length */
err = tfa_dsp_patch(dev_idx, size, (const unsigned char *) patchfile->data);
break;
case dscCfMem:
err = tfaRunWriteDspMem(dev_idx, (nxpTfaDspMem_t *)(prof->list[i].offset+(uint8_t *)g_cont));
break;
default:
/* ignore any other type */
break;
}
}
return err;
}
enum Tfa98xx_Error tfaContWriteItem(int dev_idx, nxpTfaDescPtr_t *dsc)
{
enum Tfa98xx_Error err = Tfa98xx_Error_Ok;
/* nxpTfaFileDsc_t *file; */
nxpTfaRegpatch_t *reg;
nxpTfaMode_t *cas;
nxpTfaBitfield_t *bitf;
switch (dsc->type) {
case dscDefault:
case dscDevice: /* ignore */
case dscProfile: /* profile list */
break;
case dscRegister: /* register patch */
reg = (nxpTfaRegpatch_t *)(dsc->offset+(uint8_t *)g_cont);
return tfaRunWriteRegister(dev_idx, reg);
/* pr_debug("$0x%2x=0x%02x,0x%02x\n", reg->address, reg->mask, reg->value); */
break;
case dscString: /* ascii: zero terminated string */
pr_debug(";string: %s\n", tfaContGetString(dsc));
break;
case dscFile: /* filename + file contents */
case dscPatch:
break;
case dscMode:
cas = (nxpTfaMode_t *)(dsc->offset+(uint8_t *)g_cont);
if (cas->value == Tfa98xx_Mode_RCV)
tfa98xx_select_mode(dev_idx, Tfa98xx_Mode_RCV);
else
tfa98xx_select_mode(dev_idx, Tfa98xx_Mode_Normal);
break;
case dscCfMem:
err = tfaRunWriteDspMem(dev_idx, (nxpTfaDspMem_t *)(dsc->offset+(uint8_t *)g_cont));
break;
case dscBitfield:
bitf = (nxpTfaBitfield_t *)(dsc->offset+(uint8_t *)g_cont);
return tfaRunWriteBitfield(dev_idx , *bitf);
break;
case dscFilter:
return tfaRunWriteFilter(dev_idx, (nxpTfaContBiquad_t *)(dsc->offset+(uint8_t *)g_cont));
break;
}
return err;
}
static unsigned int tfa98xx_sr_from_field(unsigned int field)
{
switch (field) {
case 0:
return 8000;
case 1:
return 11025;
case 2:
return 12000;
case 3:
return 16000;
case 4:
return 22050;
case 5:
return 24000;
case 6:
return 32000;
case 7:
return 44100;
case 8:
return 48000;
default:
return 0;
}
}
enum Tfa98xx_Error tfa_write_filters(int dev_idx, int prof_idx)
{
enum Tfa98xx_Error err = Tfa98xx_Error_Ok;
nxpTfaProfileList_t *prof = tfaContProfile(dev_idx, prof_idx);
unsigned int i;
int status;
if (!prof) {
return Tfa98xx_Error_Bad_Parameter;
}
if (tfa98xx_cnt_verbose) {
pr_debug("----- profile: %s (%d) -----\n", tfaContGetString(&prof->name), prof_idx);
pr_debug("Waiting for CLKS... \n");
}
for (i = 10; i > 0; i--) {
err = tfa98xx_dsp_system_stable(dev_idx, &status);
if (status)
break;
else
msleep_interruptible(10);
}
if (i == 0) {
if (tfa98xx_cnt_verbose)
pr_err("Unable to write filters, CLKS=0 \n");
return Tfa98xx_Error_StateTimedOut;
}
/* process the list until the end of the profile or the default section */
for (i = 0; i < prof->length; i++) {
if (prof->list[i].type == dscFilter) {
if (tfaContWriteItem(dev_idx, &prof->list[i]) != Tfa98xx_Error_Ok)
return Tfa98xx_Error_Bad_Parameter;
}
}
return err;
}
unsigned int tfa98xx_get_profile_sr(int dev_idx, unsigned int prof_idx)
{
nxpTfaBitfield_t *bitf;
unsigned int i;
nxpTfaDeviceList_t *dev;
nxpTfaProfileList_t *prof;
int fs_profile = -1;
dev = tfaContDevice (dev_idx);
if (!dev)
return 0;
prof = tfaContProfile(dev_idx, prof_idx);
if (!prof)
return 0;
/* Check profile fields first */
for (i = 0; i < prof->length; i++) {
if (prof->list[i].type == dscDefault)
break;
/* check for profile settingd (AUDFS) */
if (prof->list[i].type == dscBitfield) {
bitf = (nxpTfaBitfield_t *)(prof->list[i].offset+(uint8_t *)g_cont);
if (bitf->field == TFA_FAM(dev_idx, AUDFS)) {
fs_profile = bitf->value;
break;
}
}
}
pr_debug("%s - profile fs: 0x%x = %dHz (%d - %d)\n", __FUNCTION__, fs_profile,
tfa98xx_sr_from_field(fs_profile),
dev_idx, prof_idx);
if (fs_profile != -1)
return tfa98xx_sr_from_field(fs_profile);
/* Check for container default setting */
/* process the list until a patch, file of profile is encountered */
for (i = 0; i < dev->length; i++) {
if (dev->list[i].type == dscPatch ||
dev->list[i].type == dscFile ||
dev->list[i].type == dscProfile)
break;
if (dev->list[i].type == dscBitfield) {
bitf = (nxpTfaBitfield_t *)(dev->list[i].offset+(uint8_t *)g_cont);
if (bitf->field == TFA_FAM(dev_idx, AUDFS)) {
fs_profile = bitf->value;
break;
}
}
/* Ignore register case */
}
pr_debug("%s - default fs: 0x%x = %dHz (%d - %d)\n", __FUNCTION__, fs_profile,
tfa98xx_sr_from_field(fs_profile),
dev_idx, prof_idx);
if (fs_profile != -1)
return tfa98xx_sr_from_field(fs_profile);
return 48000;
}
enum Tfa98xx_Error get_sample_rate_info(int dev_idx, nxpTfaProfileList_t *prof, nxpTfaProfileList_t *previous_prof, int fs_previous_profile)
{
enum Tfa98xx_Error err = Tfa98xx_Error_Ok;
nxpTfaBitfield_t *bitf;
unsigned int i;
int fs_default_profile = 8; /* default is 48kHz */
int fs_next_profile = 8; /* default is 48kHz */
/* ---------- default settings previous profile ---------- */
for (i = 0; i < previous_prof->length; i++) {
/* Search for the default section */
if (i == 0) {
while (previous_prof->list[i].type != dscDefault && i < previous_prof->length) {
i++;
}
i++;
}
/* Only if we found the default section search for AUDFS */
if (i < previous_prof->length) {
if (previous_prof->list[i].type == dscBitfield) {
bitf = (nxpTfaBitfield_t *)(previous_prof->list[i].offset+(uint8_t *)g_cont);
if (bitf->field == TFA_FAM(dev_idx, AUDFS)) {
fs_default_profile = bitf->value;
break;
}
}
}
}
/* ---------- settings next profile ---------- */
for (i = 0; i < prof->length; i++) {
/* We only want to write the values before the default section */
if (prof->list[i].type == dscDefault)
break;
/* search for AUDFS */
if (prof->list[i].type == dscBitfield) {
bitf = (nxpTfaBitfield_t *)(prof->list[i].offset+(uint8_t *)g_cont);
if (bitf->field == TFA_FAM(dev_idx, AUDFS)) {
fs_next_profile = bitf->value;
break;
}
}
}
/* Enable if needed for debugging!
if (tfa98xx_cnt_verbose) {
pr_debug("sample rate from the previous profile: %d \n", fs_previous_profile);
pr_debug("sample rate in the default section: %d \n", fs_default_profile);
pr_debug("sample rate for the next profile: %d \n", fs_next_profile);
}
*/
if (fs_next_profile != fs_default_profile) {
if (tfa98xx_cnt_verbose)
pr_debug("Writing delay tables for AUDFS=%d \n", fs_next_profile);
/* If the AUDFS from the next profile is not the same as
* the AUDFS from the default we need to write new delay tables
*/
err = tfa98xx_dsp_write_tables(dev_idx, fs_next_profile);
} else if (fs_default_profile != fs_previous_profile) {
if (tfa98xx_cnt_verbose)
pr_debug("Writing delay tables for AUDFS=%d \n", fs_default_profile);
/* But if we do not have a new AUDFS in the next profile and
* the AUDFS from the default profile is not the same as the AUDFS
* from the previous profile we also need to write new delay tables
*/
err = tfa98xx_dsp_write_tables(dev_idx, fs_default_profile);
}
return err;
}
/*
* process all items in the profilelist
* NOTE an error return during processing will leave the device muted
*
*/
enum Tfa98xx_Error tfaContWriteProfile(int dev_idx, int prof_idx, int vstep_idx)
{
enum Tfa98xx_Error err = Tfa98xx_Error_Ok;
nxpTfaProfileList_t *prof = tfaContProfile(dev_idx, prof_idx);
nxpTfaProfileList_t *previous_prof = tfaContProfile(dev_idx, tfa_get_swprof(dev_idx));
char buffer[(MEMTRACK_MAX_WORDS * 3) + 3] = {0}; /* every word requires 3 bytes, and 3 is the msg */
unsigned int i, k = 0, j = 0, tries = 0;
nxpTfaFileDsc_t *file;
nxpTfaCmd_t *cmd;
int size = 0, ready, fs_previous_profile = 8; /* default fs is 48kHz*/
if (!prof)
return Tfa98xx_Error_Bad_Parameter;
if (tfa98xx_cnt_verbose) {
tfa98xx_trace_printk("device:%s profile:%s vstep:%d\n", tfaContDeviceName(dev_idx),
tfaContProfileName(dev_idx, prof_idx), vstep_idx);
}
/* We only make a power cycle when the profiles are not in the same group */
if (prof->group == previous_prof->group && prof->group != 0) {
if (tfa98xx_cnt_verbose) {
pr_debug("The new profile (%s) is in the same group as the current profile (%s) \n",
tfaContGetString(&prof->name), tfaContGetString(&previous_prof->name));
}
} else {
/* mute */
tfaRunMute(dev_idx);
/* Get current sample rate before we start switching */
fs_previous_profile = TFA_GET_BF(dev_idx, AUDFS);
/* clear SBSL to make sure we stay in initCF state */
if (tfa98xx_dev_family(dev_idx) == 2) {
TFA_SET_BF_VOLATILE(dev_idx, SBSL, 0);
}
/* When we switch profile we first power down the subsystem
* This should only be done when we are in operating mode
* */
if (TFA_GET_BF(dev_idx, MANSTATE) == 9) {
err = tfa98xx_powerdown(dev_idx, 1);
if (err)
return err;
/* Wait until we are in PLL powerdown */
do {
err = tfa98xx_dsp_system_stable(dev_idx, &ready);
if (!ready)
break;
else
msleep_interruptible(10); /* wait 10ms to avoid busload */
tries++;
} while (tries <= 100);
if (tries > 100) {
pr_debug("Wait for PLL powerdown timed out!\n");
return Tfa98xx_Error_StateTimedOut;
}
} else {
pr_debug("No need to go to powerdown now \n");
}
}
/* set all bitfield settings */
/* First set all default settings */
if (tfa98xx_cnt_verbose) {
pr_debug("---------- default settings profile: %s (%d) ---------- \n",
tfaContGetString(&previous_prof->name), tfa_get_swprof(dev_idx));
if (tfa98xx_dev_family(dev_idx) == 2)
err = show_current_state(dev_idx);
}
/* Loop profile length */
for (i = 0; i < previous_prof->length; i++) {
/* Search for the default section */
if (i == 0) {
while (previous_prof->list[i].type != dscDefault && i < previous_prof->length) {
i++;
}
i++;
}
/* Only if we found the default section try writing the items */
if (i < previous_prof->length) {
if (tfaContWriteItem(dev_idx, &previous_prof->list[i]) != Tfa98xx_Error_Ok)
return Tfa98xx_Error_Bad_Parameter;
}
}
if (tfa98xx_cnt_verbose)
pr_debug("---------- new settings profile: %s (%d) ---------- \n",
tfaContGetString(&prof->name), prof_idx);
/* set new settings */
for (i = 0; i < prof->length; i++) {
/* Remember where we currently are with writing items*/
j = i;
/* We only want to write the values before the default section when we switch profile */
/* process and write all non-file items */
switch (prof->list[i].type) {
case dscFile:
case dscPatch:
case dscSetInputSelect:
case dscSetOutputSelect:
case dscSetProgramConfig:
case dscSetLagW:
case dscSetGains:
case dscSetvBatFactors:
case dscSetSensesCal:
case dscSetSensesDelay:
case dscSetMBDrc:
case dscCmd:
case dscFilter:
case dscDefault:
/* When one of these files are found, we exit */
i = prof->length;
break;
default:
err = tfaContWriteItem(dev_idx, &prof->list[i]);
if (err != Tfa98xx_Error_Ok)
return Tfa98xx_Error_Bad_Parameter;
break;
}
}
if (prof->group != previous_prof->group || prof->group == 0) {
if (tfa98xx_dev_family(dev_idx) == 2)
TFA_SET_BF_VOLATILE(dev_idx, MANSCONF, 1);
/* Leave powerdown state */
err = tfa_cf_powerup(dev_idx);
if (err)
return err;
if (tfa98xx_cnt_verbose && tfa98xx_dev_family(dev_idx) == 2)
err = show_current_state(dev_idx);
if (tfa98xx_dev_family(dev_idx) == 2) {
/* Reset SBSL to 0 (workaround of enbl_powerswitch=0) */
TFA_SET_BF_VOLATILE(dev_idx, SBSL, 0);
/* When using internal clock, make sure DSP is out of Reset mode before setting files */
if (TFA_GET_BF(dev_idx, REFCKSEL) == 1)
TFA_SET_BF(dev_idx, RST, 0);
}
}
/* Check if there are sample rate changes */
err = get_sample_rate_info(dev_idx, prof, previous_prof, fs_previous_profile);
if (err)
return err;
/* Write files from previous profile (default section)
* Should only be used for the patch&trap patch (file)
*/
if (tfa98xx_dev_family(dev_idx) == 2) {
for (i = 0; i < previous_prof->length; i++) {
/* Search for the default section */
if (i == 0) {
while (previous_prof->list[i].type != dscDefault && i < previous_prof->length) {
i++;
}
i++;
}
/* Only if we found the default section try writing the file */
if (i < previous_prof->length) {
if (previous_prof->list[i].type == dscFile || previous_prof->list[i].type == dscPatch) {
/* Only write this once */
if (tfa98xx_cnt_verbose && k == 0) {
pr_debug("---------- files default profile: %s (%d) ---------- \n",
tfaContGetString(&previous_prof->name), prof_idx);
k++;
}
file = (nxpTfaFileDsc_t *)(previous_prof->list[i].offset+(uint8_t *)g_cont);
err = tfaContWriteFile(dev_idx, file, vstep_idx, TFA_MAX_VSTEP_MSG_MARKER);
}
}
}
}
if (tfa98xx_cnt_verbose) {
pr_debug("---------- files new profile: %s (%d) ---------- \n",
tfaContGetString(&prof->name), prof_idx);
}
/* write everything until end or the default section starts
* Start where we currenly left */
for (i = j; i < prof->length; i++) {
/* We only want to write the values before the default section when we switch profile */
if (prof->list[i].type == dscDefault)
break;
switch (prof->list[i].type) {
case dscFile:
case dscPatch:
file = (nxpTfaFileDsc_t *)(prof->list[i].offset+(uint8_t *)g_cont);
err = tfaContWriteFile(dev_idx, file, vstep_idx, TFA_MAX_VSTEP_MSG_MARKER);
break;
case dscSetInputSelect:
case dscSetOutputSelect:
case dscSetProgramConfig:
case dscSetLagW:
case dscSetGains:
case dscSetvBatFactors:
case dscSetSensesCal:
case dscSetSensesDelay:
case dscSetMBDrc:
create_dsp_buffer_msg((nxpTfaMsg_t *)
(prof->list[i].offset+(char *)g_cont), buffer, &size);
err = tfa_dsp_msg(dev_idx, size, buffer);
if (tfa98xx_cnt_verbose)
pr_debug("command: %s=0x%02x%02x%02x \n",
tfaContGetCommandString(prof->list[i].type),
(unsigned char)buffer[0], (unsigned char)buffer[1], (unsigned char)buffer[2]);
break;
case dscCmd:
size = *(uint16_t *)(prof->list[i].offset+(char *)g_cont);
err = tfa_dsp_msg(dev_idx, size, prof->list[i].offset+2+(char *)g_cont);
if (tfa98xx_cnt_verbose) {
cmd = (nxpTfaCmd_t *)(prof->list[i].offset+(uint8_t *)g_cont);
pr_debug("Writing cmd=0x%02x%02x%02x \n", cmd->value[0], cmd->value[1], cmd->value[2]);
}
break;
default:
/* This allows us to write bitfield, registers or xmem after files */
if (tfaContWriteItem(dev_idx, &prof->list[i]) != Tfa98xx_Error_Ok)
return Tfa98xx_Error_Bad_Parameter;
break;
}
if (err != Tfa98xx_Error_Ok)
return err;
}
if ((prof->group != previous_prof->group || prof->group == 0) && tfa98xx_dev_family(dev_idx) == 2) {
if (TFA_GET_BF(dev_idx, REFCKSEL) == 0) {
/* set SBSL to go to operation mode */
TFA_SET_BF_VOLATILE(dev_idx, SBSL, 1);
}
}
return err;
}
/*
* process only vstep in the profilelist
*
*/
enum Tfa98xx_Error tfaContWriteFilesVstep(int dev_idx, int prof_idx, int vstep_idx)
{
nxpTfaProfileList_t *prof = tfaContProfile(dev_idx, prof_idx);
unsigned int i;
nxpTfaFileDsc_t *file;
nxpTfaHeader_t *hdr;
nxpTfaHeaderType_t type;
enum Tfa98xx_Error err = Tfa98xx_Error_Ok;
if (!prof)
return Tfa98xx_Error_Bad_Parameter;
if (tfa98xx_cnt_verbose)
tfa98xx_trace_printk("device:%s profile:%s vstep:%d\n", tfaContDeviceName(dev_idx),
tfaContProfileName(dev_idx, prof_idx), vstep_idx);
/* write vstep file only! */
for (i = 0; i < prof->length; i++) {
if (prof->list[i].type == dscFile) {
file = (nxpTfaFileDsc_t *)(prof->list[i].offset+(uint8_t *)g_cont);
hdr = (nxpTfaHeader_t *)file->data;
type = (nxpTfaHeaderType_t) hdr->id;
switch (type) {
case volstepHdr:
if (tfaContWriteFile(dev_idx, file, vstep_idx, TFA_MAX_VSTEP_MSG_MARKER))
return Tfa98xx_Error_Bad_Parameter;
break;
default:
break;
}
}
}
return err;
}
char *tfaContGetString(nxpTfaDescPtr_t *dsc)
{
if (dsc->type != dscString)
return nostring;
return dsc->offset+(char *)g_cont;
}
void individual_calibration_results(Tfa98xx_handle_t handle)
{
int value_P, value_S;
/* Read the calibration result in xmem (529=primary channel) (530=secondary channel) */
tfa98xx_dsp_read_mem(handle, 529, 1, &value_P);
tfa98xx_dsp_read_mem(handle, 530, 1, &value_S);
if (value_P != 1 && value_S != 1)
pr_debug("Calibration failed on both channels! \n");
else if (value_P != 1) {
pr_debug("Calibration failed on Primary (Left) channel! \n");
TFA_SET_BF_VOLATILE(handle, SSLEFTE, 0); /* Disable the sound for the left speaker */
} else if (value_S != 1) {
pr_debug("Calibration failed on Secondary (Right) channel! \n");
TFA_SET_BF_VOLATILE(handle, SSRIGHTE, 0); /* Disable the sound for the right speaker */
}
TFA_SET_BF_VOLATILE(handle, AMPINSEL, 0); /* Set amplifier input to TDM */
TFA_SET_BF_VOLATILE(handle, SBSL, 1);
}
char *tfaContGetCommandString(uint32_t type)
{
if (type == dscSetInputSelect)
return "setInputSelector";
else if (type == dscSetOutputSelect)
return "setOutputSelector";
else if (type == dscSetProgramConfig)
return "setProgramConfig";
else if (type == dscSetLagW)
return "setLagW";
else if (type == dscSetGains)
return "setGains";
else if (type == dscSetvBatFactors)
return "setvBatFactors";
else if (type == dscSetSensesCal)
return "setSensesCal";
else if (type == dscSetSensesDelay)
return "setSensesDelay";
else if (type == dscSetMBDrc)
return "setMBDrc";
else if (type == dscFilter)
return "filter";
else
return nostring;
}
/*
* Get the name of the device at a certain index in the container file
* return device name
*/
char *tfaContDeviceName(int dev_idx)
{
nxpTfaDeviceList_t *dev;
if (dev_idx >= tfa98xx_cnt_max_device())
return errorname;
dev = tfaContDevice(dev_idx);
if (dev == NULL)
return errorname;
return tfaContGetString(&dev->name);
}
/*
* Get the application name from the container file application field
* note that the input stringbuffer should be sizeof(application field)+1
*
*/
int tfa_cnt_get_app_name(char *name)
{
unsigned int i;
int len;
for (i = 0; i < sizeof(g_cont->application); i++) {
if (isalpha(g_cont->application[i])) /* copy char if valid */
name[i] = g_cont->application[i];
if (g_cont->application[i] == '\0') {
len = i;
name[i] = '\0';
break;
}
}
len = i;
name[i] = '\0';
return len;
}
/*
* Get profile index of the calibration profile.
* Returns: (profile index) if found, (-2) if no
* calibration profile is found or (-1) on error
*/
int tfaContGetCalProfile(int dev_idx)
{
int prof, nprof, cal_idx = -2;
if ((dev_idx < 0) || (dev_idx >= tfa98xx_cnt_max_device()))
return (-1);
nprof = tfaContMaxProfile(dev_idx);
/* search for the calibration profile in the list of profiles */
for (prof = 0; prof < nprof; prof++) {
if (strnstr(tfaContProfileName(dev_idx, prof), ".cal", strlen(tfaContProfileName(dev_idx, prof))) != NULL) {
cal_idx = prof;
pr_debug("Using calibration profile: '%s'\n", tfaContProfileName(dev_idx, prof));
break;
}
}
return cal_idx;
}
/**
* Is the profile a tap profile ?
* @param dev_idx the index of the device
* @param prof_idx the index of the profile
* @return 1 if the profile is a tap profile or 0 if not
*/
int tfaContIsTapProfile(int dev_idx, int prof_idx)
{
int nprof;
if ((dev_idx < 0) || (dev_idx >= tfa98xx_cnt_max_device()))
return (-1);
nprof = tfaContMaxProfile(dev_idx);
/* Check if next profile is tap profile */
if (strnstr(tfaContProfileName(dev_idx, prof_idx), ".tap", strlen(tfaContProfileName(dev_idx, prof_idx))) != NULL) {
pr_debug("Using Tap profile: '%s'\n", tfaContProfileName(dev_idx, prof_idx));
return 1;
}
return 0;
}
/*
* Get the name of the profile at certain index for a device in the container file
* return profile name
*/
char *tfaContProfileName(int dev_idx, int prof_idx)
{
nxpTfaProfileList_t *prof;
if ((dev_idx < 0) || (dev_idx >= tfa98xx_cnt_max_device()))
return errorname;
if ((prof_idx < 0) || (prof_idx >= tfaContMaxProfile(dev_idx)))
return nonename;
/* the Nth profiles for this device */
prof = tfaContGetDevProfList(g_cont, dev_idx, prof_idx);
return tfaContGetString(&prof->name);
}
/*
* return 1st profile list
*/
nxpTfaProfileList_t *tfaContGet1stProfList(nxpTfaContainer_t *cont)
{
nxpTfaProfileList_t *prof;
uint8_t *b = (uint8_t *) cont;
int maxdev = 0;
nxpTfaDeviceList_t *dev;
/* get nr of devlists */
maxdev = cont->ndev;
/* get last devlist */
dev = tfaContGetDevList(cont, maxdev - 1);
if (dev == NULL)
return NULL;
/* the 1st profile starts after the last device list */
b = (uint8_t *) dev + sizeof(nxpTfaDeviceList_t) + dev->length * (sizeof(nxpTfaDescPtr_t));
prof = (nxpTfaProfileList_t *) b;
return prof;
}
/*
* return 1st livedata list
*/
nxpTfaLiveDataList_t *tfaContGet1stLiveDataList(nxpTfaContainer_t *cont)
{
nxpTfaLiveDataList_t *ldata;
nxpTfaProfileList_t *prof;
nxpTfaDeviceList_t *dev;
uint8_t *b = (uint8_t *) cont;
int maxdev, maxprof;
/* get nr of devlists+1 */
maxdev = cont->ndev;
/* get nr of proflists */
maxprof = cont->nprof;
/* get last devlist */
dev = tfaContGetDevList(cont, maxdev - 1);
/* the 1st livedata starts after the last device list */
b = (uint8_t *) dev + sizeof(nxpTfaDeviceList_t) +
dev->length * (sizeof(nxpTfaDescPtr_t));
while (maxprof != 0) {
/* get last proflist */
prof = (nxpTfaProfileList_t *) b;
b += sizeof(nxpTfaProfileList_t) +
((prof->length-1) * (sizeof(nxpTfaDescPtr_t)));
maxprof--;
}
/* Else the marker falls off */
b += 4; /* bytes */
ldata = (nxpTfaLiveDataList_t *) b;
return ldata;
}
enum Tfa98xx_Error tfaContOpen(int dev_idx)
{
return tfa98xx_open((Tfa98xx_handle_t)dev_idx);
}
enum Tfa98xx_Error tfaContClose(int dev_idx)
{
return tfa98xx_close(dev_idx);
}
/*
* return the device count in the container file
*/
int tfa98xx_cnt_max_device(void)
{
return g_cont != NULL ? g_cont->ndev : 0;
}
/*
* lookup slave and return device index
*/
int tfa98xx_cnt_slave2idx(int slave_addr)
{
int idx;
for (idx = 0; idx < g_devs; idx++) {
if (g_dev[idx]->dev == slave_addr)
return idx;
}
return (-1);
}
/*
* lookup slave and return device revid
*/
int tfa98xx_cnt_slave2revid(int slave_addr)
{
int idx = tfa98xx_cnt_slave2idx(slave_addr);
uint16_t revid;
if (idx < 0)
return idx;
/* note that the device must have been opened before */
revid = tfa98xx_get_device_revision(idx);
/* quick check for valid contents */
return (revid&0xFF) >= 0x12 ? revid : -1 ;
}
/*
* return the device list pointer
*/
nxpTfaDeviceList_t *tfaContDevice(int dev_idx)
{
if (dev_idx < g_devs)
return g_dev[dev_idx];
/* pr_err("Devlist index too high:%d!", idx); */
return NULL;
}
/*
* return the per device profile count
*/
int tfaContMaxProfile(int dev_idx)
{
if (dev_idx >= g_devs) {
/* pr_err("Devlist index too high:%d!", ndev); */
return 0;
}
return g_profs[dev_idx];
}
/*
* return the next profile:
* - assume that all profiles are adjacent
* - calculate the total length of the input
* - the input profile + its length is the next profile
*/
nxpTfaProfileList_t *tfaContNextProfile(nxpTfaProfileList_t *prof)
{
uint8_t *this, *next; /* byte pointers for byte pointer arithmetic */
nxpTfaProfileList_t *nextprof;
int listlength; /* total length of list in bytes */
if (prof == NULL)
return NULL;
if (prof->ID != TFA_PROFID)
return NULL; /* invalid input */
this = (uint8_t *)prof;
/* nr of items in the list, length includes name dsc so - 1*/
listlength = (prof->length - 1)*sizeof(nxpTfaDescPtr_t);
/* the sizeof(nxpTfaProfileList_t) includes the list[0] length */
next = this + listlength + sizeof(nxpTfaProfileList_t);/* - sizeof(nxpTfaDescPtr_t); */
nextprof = (nxpTfaProfileList_t *)next;
if (nextprof->ID != TFA_PROFID)
return NULL;
return nextprof;
}
/*
* return the next livedata
*/
nxpTfaLiveDataList_t *tfaContNextLiveData(nxpTfaLiveDataList_t *livedata)
{
nxpTfaLiveDataList_t *nextlivedata = (nxpTfaLiveDataList_t *)((char *)livedata + (livedata->length*4) +
sizeof(nxpTfaLiveDataList_t) - 4);
if (nextlivedata->ID == TFA_LIVEDATAID)
return nextlivedata;
return NULL;
}
/*
* return the device list pointer
*/
nxpTfaProfileList_t *tfaContProfile(int dev_idx, int prof_ipx)
{
if (dev_idx >= g_devs) {
/* pr_err("Devlist index too high:%d!", ndev); */
return NULL;
}
if (prof_ipx >= g_profs[dev_idx]) {
/* pr_err("Proflist index too high:%d!", nprof); */
return NULL;
}
return g_prof[dev_idx][prof_ipx];
}
/*
* check CRC for container
* CRC is calculated over the bytes following the CRC field
*
* return 0 on error
*/
int tfaContCrcCheckContainer(nxpTfaContainer_t *cont)
{
uint8_t *base;
size_t size;
uint32_t crc;
base = (uint8_t *)&cont->CRC + 4; /* ptr to bytes following the CRC field */
size = (size_t)(cont->size - (base - (uint8_t *)cont)); /* nr of bytes following the CRC field */
crc = ~crc32_le(~0u, base, size);
return crc != cont->CRC;
}
/**
* Create a buffer which can be used to send to the dsp.
*/
void create_dsp_buffer_msg(nxpTfaMsg_t *msg, char *buffer, int *size) /* TODO cleanup */
{
int i, j = 0;
/* Copy cmdId. Remember that the cmdId is reversed */
buffer[0] = msg->cmdId[2];
buffer[1] = msg->cmdId[1];
buffer[2] = msg->cmdId[0];
/* Copy the data to the buffer */
for (i = 3; i < 3+(msg->msg_size*3); i++) {
buffer[i] = (uint8_t) ((msg->data[j] >> 16) & 0xffff);
i++;
buffer[i] = (uint8_t) ((msg->data[j] >> 8) & 0xff);
i++;
buffer[i] = (uint8_t) (msg->data[j] & 0xff);
j++;
}
*size = (3+(msg->msg_size*3)) * sizeof(char);
}
void get_all_features_from_cnt(Tfa98xx_handle_t dev_idx, int *hw_feature_register, int sw_feature_register[2])
{
nxpTfaFeatures_t *features;
int i;
nxpTfaDeviceList_t *dev = tfaContDevice(dev_idx);
/* Init values in case no keyword is defined in cnt file: */
*hw_feature_register = -1;
sw_feature_register[0] = -1;
sw_feature_register[1] = -1;
if (dev == NULL)
return;
/* process the device list */
for (i = 0; i < dev->length; i++) {
if (dev->list[i].type == dscFeatures) {
features = (nxpTfaFeatures_t *)(dev->list[i].offset+(uint8_t *)g_cont);
*hw_feature_register = features->value[0];
sw_feature_register[0] = features->value[1];
sw_feature_register[1] = features->value[2];
break;
}
}
}
/* wrapper function */
void get_hw_features_from_cnt(Tfa98xx_handle_t dev_idx, int *hw_feature_register)
{
int sw_feature_register[2];
get_all_features_from_cnt(dev_idx, hw_feature_register, sw_feature_register);
}
/* wrapper function */
void get_sw_features_from_cnt(Tfa98xx_handle_t dev_idx, int sw_feature_register[2])
{
int hw_feature_register;
get_all_features_from_cnt(dev_idx, &hw_feature_register, sw_feature_register);
}
/* Factory trimming for the Boost converter */
void tfa_factory_trimmer(Tfa98xx_handle_t dev_idx)
{
unsigned short currentValue, delta;
int result;
/* Factory trimming for the Boost converter */
/* check if there is a correction needed */
result = TFA_GET_BF(dev_idx, DCMCCAPI);
if (result) {
/* Get currentvalue of DCMCC and the Delta value */
currentValue = (unsigned short)TFA_GET_BF(dev_idx, DCMCC);
delta = (unsigned short)TFA_GET_BF(dev_idx, USERDEF);
/* check the sign bit (+/-) */
result = TFA_GET_BF(dev_idx, DCMCCSB);
if (result == 0) {
/* Do not exceed the maximum value of 15 */
if (currentValue + delta < 15) {
TFA_SET_BF_VOLATILE(dev_idx, DCMCC, currentValue + delta);
if (tfa98xx_cnt_verbose)
pr_debug("Max coil current is set to: %d \n", currentValue + delta);
} else {
TFA_SET_BF_VOLATILE(dev_idx, DCMCC, 15);
if (tfa98xx_cnt_verbose)
pr_debug("Max coil current is set to: 15 \n");
}
} else if (result == 1) {
/* Do not exceed the minimum value of 0 */
if (currentValue - delta > 0) {
TFA_SET_BF_VOLATILE(dev_idx, DCMCC, currentValue - delta);
if (tfa98xx_cnt_verbose)
pr_debug("Max coil current is set to: %d \n", currentValue - delta);
} else {
TFA_SET_BF_VOLATILE(dev_idx, DCMCC, 0);
if (tfa98xx_cnt_verbose)
pr_debug("Max coil current is set to: 0 \n");
}
}
}
}
enum Tfa98xx_Error tfa_set_filters(int dev_idx, int prof_idx)
{
enum Tfa98xx_Error err = Tfa98xx_Error_Ok;
nxpTfaProfileList_t *prof = tfaContProfile(dev_idx, prof_idx);
unsigned int i;
if (!prof)
return Tfa98xx_Error_Bad_Parameter;
/* If we are in powerdown there is no need to set filters */
if (TFA_GET_BF(dev_idx, PWDN) == 1)
return Tfa98xx_Error_Ok;
/* loop the profile to find filter settings */
for (i = 0; i < prof->length; i++) {
/* We only want to write the values before the default section */
if (prof->list[i].type == dscDefault)
break;
/* write all filter settings */
if (prof->list[i].type == dscFilter) {
if (tfaContWriteItem(dev_idx, &prof->list[i]) != Tfa98xx_Error_Ok)
return err;
}
}
return err;
}
|