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
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
|
// SPDX-License-Identifier: GPL-2.0
//
// tas2781-fmwlib.c -- TASDEVICE firmware support
//
// Copyright 2023 - 2024 Texas Instruments, Inc.
//
// Author: Shenghao Ding <shenghao-ding@ti.com>
#include <linux/crc8.h>
#include <linux/firmware.h>
#include <linux/i2c.h>
#include <linux/init.h>
#include <linux/interrupt.h>
#include <linux/module.h>
#include <linux/of.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/tlv.h>
#include <sound/tas2781.h>
#include <linux/unaligned.h>
#define ERROR_PRAM_CRCCHK 0x0000000
#define ERROR_YRAM_CRCCHK 0x0000001
#define PPC_DRIVER_CRCCHK 0x00000200
#define TAS2781_SA_COEFF_SWAP_REG TASDEVICE_REG(0, 0x35, 0x2c)
#define TAS2781_YRAM_BOOK1 140
#define TAS2781_YRAM1_PAGE 42
#define TAS2781_YRAM1_START_REG 88
#define TAS2781_YRAM2_START_PAGE 43
#define TAS2781_YRAM2_END_PAGE 49
#define TAS2781_YRAM2_START_REG 8
#define TAS2781_YRAM2_END_REG 127
#define TAS2781_YRAM3_PAGE 50
#define TAS2781_YRAM3_START_REG 8
#define TAS2781_YRAM3_END_REG 27
/*should not include B0_P53_R44-R47 */
#define TAS2781_YRAM_BOOK2 0
#define TAS2781_YRAM4_START_PAGE 50
#define TAS2781_YRAM4_END_PAGE 60
#define TAS2781_YRAM5_PAGE 61
#define TAS2781_YRAM5_START_REG TAS2781_YRAM3_START_REG
#define TAS2781_YRAM5_END_REG TAS2781_YRAM3_END_REG
#define TASDEVICE_MAXPROGRAM_NUM_KERNEL 5
#define TASDEVICE_MAXCONFIG_NUM_KERNEL_MULTIPLE_AMPS 64
#define TASDEVICE_MAXCONFIG_NUM_KERNEL 10
#define MAIN_ALL_DEVICES_1X 0x01
#define MAIN_DEVICE_A_1X 0x02
#define MAIN_DEVICE_B_1X 0x03
#define MAIN_DEVICE_C_1X 0x04
#define MAIN_DEVICE_D_1X 0x05
#define COEFF_DEVICE_A_1X 0x12
#define COEFF_DEVICE_B_1X 0x13
#define COEFF_DEVICE_C_1X 0x14
#define COEFF_DEVICE_D_1X 0x15
#define PRE_DEVICE_A_1X 0x22
#define PRE_DEVICE_B_1X 0x23
#define PRE_DEVICE_C_1X 0x24
#define PRE_DEVICE_D_1X 0x25
#define PRE_SOFTWARE_RESET_DEVICE_A 0x41
#define PRE_SOFTWARE_RESET_DEVICE_B 0x42
#define PRE_SOFTWARE_RESET_DEVICE_C 0x43
#define PRE_SOFTWARE_RESET_DEVICE_D 0x44
#define POST_SOFTWARE_RESET_DEVICE_A 0x45
#define POST_SOFTWARE_RESET_DEVICE_B 0x46
#define POST_SOFTWARE_RESET_DEVICE_C 0x47
#define POST_SOFTWARE_RESET_DEVICE_D 0x48
struct tas_crc {
unsigned char offset;
unsigned char len;
};
struct blktyp_devidx_map {
unsigned char blktyp;
unsigned char dev_idx;
};
static const char deviceNumber[TASDEVICE_DSP_TAS_MAX_DEVICE] = {
1, 2, 1, 2, 1, 1, 0, 2, 4, 3, 1, 2, 3, 4
};
/* fixed m68k compiling issue: mapping table can save code field */
static const struct blktyp_devidx_map ppc3_tas2781_mapping_table[] = {
{ MAIN_ALL_DEVICES_1X, 0x80 },
{ MAIN_DEVICE_A_1X, 0x81 },
{ COEFF_DEVICE_A_1X, 0xC1 },
{ PRE_DEVICE_A_1X, 0xC1 },
{ PRE_SOFTWARE_RESET_DEVICE_A, 0xC1 },
{ POST_SOFTWARE_RESET_DEVICE_A, 0xC1 },
{ MAIN_DEVICE_B_1X, 0x82 },
{ COEFF_DEVICE_B_1X, 0xC2 },
{ PRE_DEVICE_B_1X, 0xC2 },
{ PRE_SOFTWARE_RESET_DEVICE_B, 0xC2 },
{ POST_SOFTWARE_RESET_DEVICE_B, 0xC2 },
{ MAIN_DEVICE_C_1X, 0x83 },
{ COEFF_DEVICE_C_1X, 0xC3 },
{ PRE_DEVICE_C_1X, 0xC3 },
{ PRE_SOFTWARE_RESET_DEVICE_C, 0xC3 },
{ POST_SOFTWARE_RESET_DEVICE_C, 0xC3 },
{ MAIN_DEVICE_D_1X, 0x84 },
{ COEFF_DEVICE_D_1X, 0xC4 },
{ PRE_DEVICE_D_1X, 0xC4 },
{ PRE_SOFTWARE_RESET_DEVICE_D, 0xC4 },
{ POST_SOFTWARE_RESET_DEVICE_D, 0xC4 },
};
static const struct blktyp_devidx_map ppc3_mapping_table[] = {
{ MAIN_ALL_DEVICES_1X, 0x80 },
{ MAIN_DEVICE_A_1X, 0x81 },
{ COEFF_DEVICE_A_1X, 0xC1 },
{ PRE_DEVICE_A_1X, 0xC1 },
{ MAIN_DEVICE_B_1X, 0x82 },
{ COEFF_DEVICE_B_1X, 0xC2 },
{ PRE_DEVICE_B_1X, 0xC2 },
{ MAIN_DEVICE_C_1X, 0x83 },
{ COEFF_DEVICE_C_1X, 0xC3 },
{ PRE_DEVICE_C_1X, 0xC3 },
{ MAIN_DEVICE_D_1X, 0x84 },
{ COEFF_DEVICE_D_1X, 0xC4 },
{ PRE_DEVICE_D_1X, 0xC4 },
};
static const struct blktyp_devidx_map non_ppc3_mapping_table[] = {
{ MAIN_ALL_DEVICES, 0x80 },
{ MAIN_DEVICE_A, 0x81 },
{ COEFF_DEVICE_A, 0xC1 },
{ PRE_DEVICE_A, 0xC1 },
{ MAIN_DEVICE_B, 0x82 },
{ COEFF_DEVICE_B, 0xC2 },
{ PRE_DEVICE_B, 0xC2 },
{ MAIN_DEVICE_C, 0x83 },
{ COEFF_DEVICE_C, 0xC3 },
{ PRE_DEVICE_C, 0xC3 },
{ MAIN_DEVICE_D, 0x84 },
{ COEFF_DEVICE_D, 0xC4 },
{ PRE_DEVICE_D, 0xC4 },
};
static struct tasdevice_config_info *tasdevice_add_config(
struct tasdevice_priv *tas_priv, unsigned char *config_data,
unsigned int config_size, int *status)
{
struct tasdevice_config_info *cfg_info;
struct tasdev_blk_data **bk_da;
unsigned int config_offset = 0;
unsigned int i;
/* In most projects are many audio cases, such as music, handfree,
* receiver, games, audio-to-haptics, PMIC record, bypass mode,
* portrait, landscape, etc. Even in multiple audios, one or
* two of the chips will work for the special case, such as
* ultrasonic application. In order to support these variable-numbers
* of audio cases, flexible configs have been introduced in the
* dsp firmware.
*/
cfg_info = kzalloc(sizeof(struct tasdevice_config_info), GFP_KERNEL);
if (!cfg_info) {
*status = -ENOMEM;
goto out;
}
if (tas_priv->rcabin.fw_hdr.binary_version_num >= 0x105) {
if (config_offset + 64 > (int)config_size) {
*status = -EINVAL;
dev_err(tas_priv->dev, "add conf: Out of boundary\n");
goto out;
}
config_offset += 64;
}
if (config_offset + 4 > (int)config_size) {
*status = -EINVAL;
dev_err(tas_priv->dev, "add config: Out of boundary\n");
goto out;
}
/* convert data[offset], data[offset + 1], data[offset + 2] and
* data[offset + 3] into host
*/
cfg_info->nblocks = get_unaligned_be32(&config_data[config_offset]);
config_offset += 4;
/* Several kinds of dsp/algorithm firmwares can run on tas2781,
* the number and size of blk are not fixed and different among
* these firmwares.
*/
bk_da = cfg_info->blk_data = kcalloc(cfg_info->nblocks,
sizeof(struct tasdev_blk_data *), GFP_KERNEL);
if (!bk_da) {
*status = -ENOMEM;
goto out;
}
cfg_info->real_nblocks = 0;
for (i = 0; i < cfg_info->nblocks; i++) {
if (config_offset + 12 > config_size) {
*status = -EINVAL;
dev_err(tas_priv->dev,
"%s: Out of boundary: i = %d nblocks = %u!\n",
__func__, i, cfg_info->nblocks);
break;
}
bk_da[i] = kzalloc(sizeof(struct tasdev_blk_data), GFP_KERNEL);
if (!bk_da[i]) {
*status = -ENOMEM;
break;
}
bk_da[i]->dev_idx = config_data[config_offset];
config_offset++;
bk_da[i]->block_type = config_data[config_offset];
config_offset++;
if (bk_da[i]->block_type == TASDEVICE_BIN_BLK_PRE_POWER_UP) {
if (bk_da[i]->dev_idx == 0)
cfg_info->active_dev =
(1 << tas_priv->ndev) - 1;
else
cfg_info->active_dev |= 1 <<
(bk_da[i]->dev_idx - 1);
}
bk_da[i]->yram_checksum =
get_unaligned_be16(&config_data[config_offset]);
config_offset += 2;
bk_da[i]->block_size =
get_unaligned_be32(&config_data[config_offset]);
config_offset += 4;
bk_da[i]->n_subblks =
get_unaligned_be32(&config_data[config_offset]);
config_offset += 4;
if (config_offset + bk_da[i]->block_size > config_size) {
*status = -EINVAL;
dev_err(tas_priv->dev,
"%s: Out of boundary: i = %d blks = %u!\n",
__func__, i, cfg_info->nblocks);
break;
}
/* instead of kzalloc+memcpy */
bk_da[i]->regdata = kmemdup(&config_data[config_offset],
bk_da[i]->block_size, GFP_KERNEL);
if (!bk_da[i]->regdata) {
*status = -ENOMEM;
goto out;
}
config_offset += bk_da[i]->block_size;
cfg_info->real_nblocks += 1;
}
out:
return cfg_info;
}
int tasdevice_rca_parser(void *context, const struct firmware *fmw)
{
struct tasdevice_priv *tas_priv = context;
struct tasdevice_config_info **cfg_info;
struct tasdevice_rca_hdr *fw_hdr;
struct tasdevice_rca *rca;
unsigned int total_config_sz = 0;
unsigned char *buf;
int offset = 0;
int ret = 0;
int i;
rca = &(tas_priv->rcabin);
fw_hdr = &(rca->fw_hdr);
if (!fmw || !fmw->data) {
dev_err(tas_priv->dev, "Failed to read %s\n",
tas_priv->rca_binaryname);
tas_priv->fw_state = TASDEVICE_DSP_FW_FAIL;
ret = -EINVAL;
goto out;
}
buf = (unsigned char *)fmw->data;
fw_hdr->img_sz = get_unaligned_be32(&buf[offset]);
offset += 4;
if (fw_hdr->img_sz != fmw->size) {
dev_err(tas_priv->dev,
"File size not match, %d %u", (int)fmw->size,
fw_hdr->img_sz);
tas_priv->fw_state = TASDEVICE_DSP_FW_FAIL;
ret = -EINVAL;
goto out;
}
fw_hdr->checksum = get_unaligned_be32(&buf[offset]);
offset += 4;
fw_hdr->binary_version_num = get_unaligned_be32(&buf[offset]);
if (fw_hdr->binary_version_num < 0x103) {
dev_err(tas_priv->dev, "File version 0x%04x is too low",
fw_hdr->binary_version_num);
tas_priv->fw_state = TASDEVICE_DSP_FW_FAIL;
ret = -EINVAL;
goto out;
}
offset += 4;
fw_hdr->drv_fw_version = get_unaligned_be32(&buf[offset]);
offset += 8;
fw_hdr->plat_type = buf[offset];
offset += 1;
fw_hdr->dev_family = buf[offset];
offset += 1;
fw_hdr->reserve = buf[offset];
offset += 1;
fw_hdr->ndev = buf[offset];
offset += 1;
if (fw_hdr->ndev != tas_priv->ndev) {
dev_err(tas_priv->dev,
"ndev(%u) in rcabin mismatch ndev(%u) in DTS\n",
fw_hdr->ndev, tas_priv->ndev);
tas_priv->fw_state = TASDEVICE_DSP_FW_FAIL;
ret = -EINVAL;
goto out;
}
if (offset + TASDEVICE_DEVICE_SUM > fw_hdr->img_sz) {
dev_err(tas_priv->dev, "rca_ready: Out of boundary!\n");
ret = -EINVAL;
tas_priv->fw_state = TASDEVICE_DSP_FW_FAIL;
goto out;
}
for (i = 0; i < TASDEVICE_DEVICE_SUM; i++, offset++)
fw_hdr->devs[i] = buf[offset];
fw_hdr->nconfig = get_unaligned_be32(&buf[offset]);
offset += 4;
for (i = 0; i < TASDEVICE_CONFIG_SUM; i++) {
fw_hdr->config_size[i] = get_unaligned_be32(&buf[offset]);
offset += 4;
total_config_sz += fw_hdr->config_size[i];
}
if (fw_hdr->img_sz - total_config_sz != (unsigned int)offset) {
dev_err(tas_priv->dev, "Bin file error!\n");
ret = -EINVAL;
tas_priv->fw_state = TASDEVICE_DSP_FW_FAIL;
goto out;
}
cfg_info = kcalloc(fw_hdr->nconfig, sizeof(*cfg_info), GFP_KERNEL);
if (!cfg_info) {
ret = -ENOMEM;
tas_priv->fw_state = TASDEVICE_DSP_FW_FAIL;
goto out;
}
rca->cfg_info = cfg_info;
rca->ncfgs = 0;
for (i = 0; i < (int)fw_hdr->nconfig; i++) {
rca->ncfgs += 1;
cfg_info[i] = tasdevice_add_config(tas_priv, &buf[offset],
fw_hdr->config_size[i], &ret);
if (ret) {
tas_priv->fw_state = TASDEVICE_DSP_FW_FAIL;
goto out;
}
offset += (int)fw_hdr->config_size[i];
}
out:
return ret;
}
EXPORT_SYMBOL_NS_GPL(tasdevice_rca_parser, SND_SOC_TAS2781_FMWLIB);
/* fixed m68k compiling issue: mapping table can save code field */
static unsigned char map_dev_idx(struct tasdevice_fw *tas_fmw,
struct tasdev_blk *block)
{
struct blktyp_devidx_map *p =
(struct blktyp_devidx_map *)non_ppc3_mapping_table;
struct tasdevice_dspfw_hdr *fw_hdr = &(tas_fmw->fw_hdr);
struct tasdevice_fw_fixed_hdr *fw_fixed_hdr = &(fw_hdr->fixed_hdr);
int i, n = ARRAY_SIZE(non_ppc3_mapping_table);
unsigned char dev_idx = 0;
if (fw_fixed_hdr->ppcver >= PPC3_VERSION_TAS2781) {
p = (struct blktyp_devidx_map *)ppc3_tas2781_mapping_table;
n = ARRAY_SIZE(ppc3_tas2781_mapping_table);
} else if (fw_fixed_hdr->ppcver >= PPC3_VERSION) {
p = (struct blktyp_devidx_map *)ppc3_mapping_table;
n = ARRAY_SIZE(ppc3_mapping_table);
}
for (i = 0; i < n; i++) {
if (block->type == p[i].blktyp) {
dev_idx = p[i].dev_idx;
break;
}
}
return dev_idx;
}
static int fw_parse_block_data_kernel(struct tasdevice_fw *tas_fmw,
struct tasdev_blk *block, const struct firmware *fmw, int offset)
{
const unsigned char *data = fmw->data;
if (offset + 16 > fmw->size) {
dev_err(tas_fmw->dev, "%s: File Size error\n", __func__);
offset = -EINVAL;
goto out;
}
/* convert data[offset], data[offset + 1], data[offset + 2] and
* data[offset + 3] into host
*/
block->type = get_unaligned_be32(&data[offset]);
offset += 4;
block->is_pchksum_present = data[offset];
offset++;
block->pchksum = data[offset];
offset++;
block->is_ychksum_present = data[offset];
offset++;
block->ychksum = data[offset];
offset++;
block->blk_size = get_unaligned_be32(&data[offset]);
offset += 4;
block->nr_subblocks = get_unaligned_be32(&data[offset]);
offset += 4;
/* fixed m68k compiling issue:
* 1. mapping table can save code field.
* 2. storing the dev_idx as a member of block can reduce unnecessary
* time and system resource comsumption of dev_idx mapping every
* time the block data writing to the dsp.
*/
block->dev_idx = map_dev_idx(tas_fmw, block);
if (offset + block->blk_size > fmw->size) {
dev_err(tas_fmw->dev, "%s: nSublocks error\n", __func__);
offset = -EINVAL;
goto out;
}
/* instead of kzalloc+memcpy */
block->data = kmemdup(&data[offset], block->blk_size, GFP_KERNEL);
if (!block->data) {
offset = -ENOMEM;
goto out;
}
offset += block->blk_size;
out:
return offset;
}
static int fw_parse_data_kernel(struct tasdevice_fw *tas_fmw,
struct tasdevice_data *img_data, const struct firmware *fmw,
int offset)
{
const unsigned char *data = fmw->data;
struct tasdev_blk *blk;
unsigned int i;
if (offset + 4 > fmw->size) {
dev_err(tas_fmw->dev, "%s: File Size error\n", __func__);
offset = -EINVAL;
goto out;
}
img_data->nr_blk = get_unaligned_be32(&data[offset]);
offset += 4;
img_data->dev_blks = kcalloc(img_data->nr_blk,
sizeof(struct tasdev_blk), GFP_KERNEL);
if (!img_data->dev_blks) {
offset = -ENOMEM;
goto out;
}
for (i = 0; i < img_data->nr_blk; i++) {
blk = &(img_data->dev_blks[i]);
offset = fw_parse_block_data_kernel(tas_fmw, blk, fmw, offset);
if (offset < 0) {
offset = -EINVAL;
break;
}
}
out:
return offset;
}
static int fw_parse_program_data_kernel(
struct tasdevice_priv *tas_priv, struct tasdevice_fw *tas_fmw,
const struct firmware *fmw, int offset)
{
struct tasdevice_prog *program;
unsigned int i;
for (i = 0; i < tas_fmw->nr_programs; i++) {
program = &(tas_fmw->programs[i]);
if (offset + 72 > fmw->size) {
dev_err(tas_priv->dev, "%s: mpName error\n", __func__);
offset = -EINVAL;
goto out;
}
/*skip 72 unused byts*/
offset += 72;
offset = fw_parse_data_kernel(tas_fmw, &(program->dev_data),
fmw, offset);
if (offset < 0)
goto out;
}
out:
return offset;
}
static int fw_parse_configuration_data_kernel(
struct tasdevice_priv *tas_priv,
struct tasdevice_fw *tas_fmw, const struct firmware *fmw, int offset)
{
const unsigned char *data = fmw->data;
struct tasdevice_config *config;
unsigned int i;
for (i = 0; i < tas_fmw->nr_configurations; i++) {
config = &(tas_fmw->configs[i]);
if (offset + 80 > fmw->size) {
dev_err(tas_priv->dev, "%s: mpName error\n", __func__);
offset = -EINVAL;
goto out;
}
memcpy(config->name, &data[offset], 64);
/*skip extra 16 bytes*/
offset += 80;
offset = fw_parse_data_kernel(tas_fmw, &(config->dev_data),
fmw, offset);
if (offset < 0)
goto out;
}
out:
return offset;
}
static int fw_parse_variable_header_kernel(
struct tasdevice_priv *tas_priv, const struct firmware *fmw,
int offset)
{
struct tasdevice_fw *tas_fmw = tas_priv->fmw;
struct tasdevice_dspfw_hdr *fw_hdr = &(tas_fmw->fw_hdr);
struct tasdevice_prog *program;
struct tasdevice_config *config;
const unsigned char *buf = fmw->data;
unsigned short max_confs;
unsigned int i;
if (offset + 12 + 4 * TASDEVICE_MAXPROGRAM_NUM_KERNEL > fmw->size) {
dev_err(tas_priv->dev, "%s: File Size error\n", __func__);
offset = -EINVAL;
goto out;
}
fw_hdr->device_family = get_unaligned_be16(&buf[offset]);
if (fw_hdr->device_family != 0) {
dev_err(tas_priv->dev, "%s:not TAS device\n", __func__);
offset = -EINVAL;
goto out;
}
offset += 2;
fw_hdr->device = get_unaligned_be16(&buf[offset]);
if (fw_hdr->device >= TASDEVICE_DSP_TAS_MAX_DEVICE ||
fw_hdr->device == 6) {
dev_err(tas_priv->dev, "Unsupported dev %d\n", fw_hdr->device);
offset = -EINVAL;
goto out;
}
offset += 2;
fw_hdr->ndev = deviceNumber[fw_hdr->device];
if (fw_hdr->ndev != tas_priv->ndev) {
dev_err(tas_priv->dev,
"%s: ndev(%u) in dspbin mismatch ndev(%u) in DTS\n",
__func__, fw_hdr->ndev, tas_priv->ndev);
offset = -EINVAL;
goto out;
}
tas_fmw->nr_programs = get_unaligned_be32(&buf[offset]);
offset += 4;
if (tas_fmw->nr_programs == 0 || tas_fmw->nr_programs >
TASDEVICE_MAXPROGRAM_NUM_KERNEL) {
dev_err(tas_priv->dev, "mnPrograms is invalid\n");
offset = -EINVAL;
goto out;
}
tas_fmw->programs = kcalloc(tas_fmw->nr_programs,
sizeof(struct tasdevice_prog), GFP_KERNEL);
if (!tas_fmw->programs) {
offset = -ENOMEM;
goto out;
}
for (i = 0; i < tas_fmw->nr_programs; i++) {
program = &(tas_fmw->programs[i]);
program->prog_size = get_unaligned_be32(&buf[offset]);
offset += 4;
}
/* Skip the unused prog_size */
offset += 4 * (TASDEVICE_MAXPROGRAM_NUM_KERNEL - tas_fmw->nr_programs);
tas_fmw->nr_configurations = get_unaligned_be32(&buf[offset]);
offset += 4;
/* The max number of config in firmware greater than 4 pieces of
* tas2781s is different from the one lower than 4 pieces of
* tas2781s.
*/
max_confs = (fw_hdr->ndev >= 4) ?
TASDEVICE_MAXCONFIG_NUM_KERNEL_MULTIPLE_AMPS :
TASDEVICE_MAXCONFIG_NUM_KERNEL;
if (tas_fmw->nr_configurations == 0 ||
tas_fmw->nr_configurations > max_confs) {
dev_err(tas_priv->dev, "%s: Conf is invalid\n", __func__);
offset = -EINVAL;
goto out;
}
if (offset + 4 * max_confs > fmw->size) {
dev_err(tas_priv->dev, "%s: mpConfigurations err\n", __func__);
offset = -EINVAL;
goto out;
}
tas_fmw->configs = kcalloc(tas_fmw->nr_configurations,
sizeof(struct tasdevice_config), GFP_KERNEL);
if (!tas_fmw->configs) {
offset = -ENOMEM;
goto out;
}
for (i = 0; i < tas_fmw->nr_programs; i++) {
config = &(tas_fmw->configs[i]);
config->cfg_size = get_unaligned_be32(&buf[offset]);
offset += 4;
}
/* Skip the unused configs */
offset += 4 * (max_confs - tas_fmw->nr_programs);
out:
return offset;
}
static int tasdevice_process_block(void *context, unsigned char *data,
unsigned char dev_idx, int sublocksize)
{
struct tasdevice_priv *tas_priv = (struct tasdevice_priv *)context;
int subblk_offset, chn, chnend, rc;
unsigned char subblk_typ = data[1];
int blktyp = dev_idx & 0xC0;
int idx = dev_idx & 0x3F;
bool is_err = false;
if (idx) {
chn = idx - 1;
chnend = idx;
} else {
chn = 0;
chnend = tas_priv->ndev;
}
for (; chn < chnend; chn++) {
if (tas_priv->tasdevice[chn].is_loading == false)
continue;
is_err = false;
subblk_offset = 2;
switch (subblk_typ) {
case TASDEVICE_CMD_SING_W: {
int i;
unsigned short len = get_unaligned_be16(&data[2]);
subblk_offset += 2;
if (subblk_offset + 4 * len > sublocksize) {
dev_err(tas_priv->dev,
"process_block: Out of boundary\n");
is_err = true;
break;
}
for (i = 0; i < len; i++) {
rc = tasdevice_dev_write(tas_priv, chn,
TASDEVICE_REG(data[subblk_offset],
data[subblk_offset + 1],
data[subblk_offset + 2]),
data[subblk_offset + 3]);
if (rc < 0) {
is_err = true;
dev_err(tas_priv->dev,
"process_block: single write error\n");
}
subblk_offset += 4;
}
}
break;
case TASDEVICE_CMD_BURST: {
unsigned short len = get_unaligned_be16(&data[2]);
subblk_offset += 2;
if (subblk_offset + 4 + len > sublocksize) {
dev_err(tas_priv->dev,
"%s: BST Out of boundary\n",
__func__);
is_err = true;
break;
}
if (len % 4) {
dev_err(tas_priv->dev,
"%s:Bst-len(%u)not div by 4\n",
__func__, len);
break;
}
rc = tasdevice_dev_bulk_write(tas_priv, chn,
TASDEVICE_REG(data[subblk_offset],
data[subblk_offset + 1],
data[subblk_offset + 2]),
&(data[subblk_offset + 4]), len);
if (rc < 0) {
is_err = true;
dev_err(tas_priv->dev,
"%s: bulk_write error = %d\n",
__func__, rc);
}
subblk_offset += (len + 4);
}
break;
case TASDEVICE_CMD_DELAY: {
unsigned int sleep_time = 0;
if (subblk_offset + 2 > sublocksize) {
dev_err(tas_priv->dev,
"%s: delay Out of boundary\n",
__func__);
is_err = true;
break;
}
sleep_time = get_unaligned_be16(&data[2]) * 1000;
usleep_range(sleep_time, sleep_time + 50);
subblk_offset += 2;
}
break;
case TASDEVICE_CMD_FIELD_W:
if (subblk_offset + 6 > sublocksize) {
dev_err(tas_priv->dev,
"%s: bit write Out of boundary\n",
__func__);
is_err = true;
break;
}
rc = tasdevice_dev_update_bits(tas_priv, chn,
TASDEVICE_REG(data[subblk_offset + 2],
data[subblk_offset + 3],
data[subblk_offset + 4]),
data[subblk_offset + 1],
data[subblk_offset + 5]);
if (rc < 0) {
is_err = true;
dev_err(tas_priv->dev,
"%s: update_bits error = %d\n",
__func__, rc);
}
subblk_offset += 6;
break;
default:
break;
}
if (is_err == true && blktyp != 0) {
if (blktyp == 0x80) {
tas_priv->tasdevice[chn].cur_prog = -1;
tas_priv->tasdevice[chn].cur_conf = -1;
} else
tas_priv->tasdevice[chn].cur_conf = -1;
}
}
return subblk_offset;
}
void tasdevice_select_cfg_blk(void *pContext, int conf_no,
unsigned char block_type)
{
struct tasdevice_priv *tas_priv = (struct tasdevice_priv *) pContext;
struct tasdevice_rca *rca = &(tas_priv->rcabin);
struct tasdevice_config_info **cfg_info = rca->cfg_info;
struct tasdev_blk_data **blk_data;
int j, k, chn, chnend;
if (conf_no >= rca->ncfgs || conf_no < 0 || !cfg_info) {
dev_err(tas_priv->dev, "conf_no should be not more than %u\n",
rca->ncfgs);
return;
}
blk_data = cfg_info[conf_no]->blk_data;
for (j = 0; j < (int)cfg_info[conf_no]->real_nblocks; j++) {
unsigned int length = 0, rc = 0;
if (block_type > 5 || block_type < 2) {
dev_err(tas_priv->dev,
"block_type should be in range from 2 to 5\n");
break;
}
if (block_type != blk_data[j]->block_type)
continue;
for (k = 0; k < (int)blk_data[j]->n_subblks; k++) {
if (blk_data[j]->dev_idx) {
chn = blk_data[j]->dev_idx - 1;
chnend = blk_data[j]->dev_idx;
} else {
chn = 0;
chnend = tas_priv->ndev;
}
for (; chn < chnend; chn++)
tas_priv->tasdevice[chn].is_loading = true;
rc = tasdevice_process_block(tas_priv,
blk_data[j]->regdata + length,
blk_data[j]->dev_idx,
blk_data[j]->block_size - length);
length += rc;
if (blk_data[j]->block_size < length) {
dev_err(tas_priv->dev,
"%s: %u %u out of boundary\n",
__func__, length,
blk_data[j]->block_size);
break;
}
}
if (length != blk_data[j]->block_size)
dev_err(tas_priv->dev, "%s: %u %u size is not same\n",
__func__, length, blk_data[j]->block_size);
}
}
EXPORT_SYMBOL_NS_GPL(tasdevice_select_cfg_blk, SND_SOC_TAS2781_FMWLIB);
static int tasdevice_load_block_kernel(
struct tasdevice_priv *tasdevice, struct tasdev_blk *block)
{
const unsigned int blk_size = block->blk_size;
unsigned int i, length;
unsigned char *data = block->data;
for (i = 0, length = 0; i < block->nr_subblocks; i++) {
int rc = tasdevice_process_block(tasdevice, data + length,
block->dev_idx, blk_size - length);
if (rc < 0) {
dev_err(tasdevice->dev,
"%s: %u %u sublock write error\n",
__func__, length, blk_size);
break;
}
length += (unsigned int)rc;
if (blk_size < length) {
dev_err(tasdevice->dev, "%s: %u %u out of boundary\n",
__func__, length, blk_size);
break;
}
}
return 0;
}
static int fw_parse_variable_hdr(struct tasdevice_priv
*tas_priv, struct tasdevice_dspfw_hdr *fw_hdr,
const struct firmware *fmw, int offset)
{
const unsigned char *buf = fmw->data;
int len = strlen((char *)&buf[offset]);
len++;
if (offset + len + 8 > fmw->size) {
dev_err(tas_priv->dev, "%s: File Size error\n", __func__);
offset = -EINVAL;
goto out;
}
offset += len;
fw_hdr->device_family = get_unaligned_be32(&buf[offset]);
if (fw_hdr->device_family != 0) {
dev_err(tas_priv->dev, "%s: not TAS device\n", __func__);
offset = -EINVAL;
goto out;
}
offset += 4;
fw_hdr->device = get_unaligned_be32(&buf[offset]);
if (fw_hdr->device >= TASDEVICE_DSP_TAS_MAX_DEVICE ||
fw_hdr->device == 6) {
dev_err(tas_priv->dev, "Unsupported dev %d\n", fw_hdr->device);
offset = -EINVAL;
goto out;
}
offset += 4;
fw_hdr->ndev = deviceNumber[fw_hdr->device];
out:
return offset;
}
static int fw_parse_variable_header_git(struct tasdevice_priv
*tas_priv, const struct firmware *fmw, int offset)
{
struct tasdevice_fw *tas_fmw = tas_priv->fmw;
struct tasdevice_dspfw_hdr *fw_hdr = &(tas_fmw->fw_hdr);
offset = fw_parse_variable_hdr(tas_priv, fw_hdr, fmw, offset);
if (offset < 0)
goto out;
if (fw_hdr->ndev != tas_priv->ndev) {
dev_err(tas_priv->dev,
"%s: ndev(%u) in dspbin mismatch ndev(%u) in DTS\n",
__func__, fw_hdr->ndev, tas_priv->ndev);
offset = -EINVAL;
}
out:
return offset;
}
static int fw_parse_block_data(struct tasdevice_fw *tas_fmw,
struct tasdev_blk *block, const struct firmware *fmw, int offset)
{
unsigned char *data = (unsigned char *)fmw->data;
int n;
if (offset + 8 > fmw->size) {
dev_err(tas_fmw->dev, "%s: Type error\n", __func__);
offset = -EINVAL;
goto out;
}
block->type = get_unaligned_be32(&data[offset]);
offset += 4;
if (tas_fmw->fw_hdr.fixed_hdr.drv_ver >= PPC_DRIVER_CRCCHK) {
if (offset + 8 > fmw->size) {
dev_err(tas_fmw->dev, "PChkSumPresent error\n");
offset = -EINVAL;
goto out;
}
block->is_pchksum_present = data[offset];
offset++;
block->pchksum = data[offset];
offset++;
block->is_ychksum_present = data[offset];
offset++;
block->ychksum = data[offset];
offset++;
} else {
block->is_pchksum_present = 0;
block->is_ychksum_present = 0;
}
block->nr_cmds = get_unaligned_be32(&data[offset]);
offset += 4;
n = block->nr_cmds * 4;
if (offset + n > fmw->size) {
dev_err(tas_fmw->dev,
"%s: File Size(%lu) error offset = %d n = %d\n",
__func__, (unsigned long)fmw->size, offset, n);
offset = -EINVAL;
goto out;
}
/* instead of kzalloc+memcpy */
block->data = kmemdup(&data[offset], n, GFP_KERNEL);
if (!block->data) {
offset = -ENOMEM;
goto out;
}
offset += n;
out:
return offset;
}
/* When parsing error occurs, all the memory resource will be released
* in the end of tasdevice_rca_ready.
*/
static int fw_parse_data(struct tasdevice_fw *tas_fmw,
struct tasdevice_data *img_data, const struct firmware *fmw,
int offset)
{
const unsigned char *data = (unsigned char *)fmw->data;
struct tasdev_blk *blk;
unsigned int i;
int n;
if (offset + 64 > fmw->size) {
dev_err(tas_fmw->dev, "%s: Name error\n", __func__);
offset = -EINVAL;
goto out;
}
memcpy(img_data->name, &data[offset], 64);
offset += 64;
n = strlen((char *)&data[offset]);
n++;
if (offset + n + 2 > fmw->size) {
dev_err(tas_fmw->dev, "%s: Description error\n", __func__);
offset = -EINVAL;
goto out;
}
offset += n;
img_data->nr_blk = get_unaligned_be16(&data[offset]);
offset += 2;
img_data->dev_blks = kcalloc(img_data->nr_blk,
sizeof(struct tasdev_blk), GFP_KERNEL);
if (!img_data->dev_blks) {
offset = -ENOMEM;
goto out;
}
for (i = 0; i < img_data->nr_blk; i++) {
blk = &(img_data->dev_blks[i]);
offset = fw_parse_block_data(tas_fmw, blk, fmw, offset);
if (offset < 0) {
offset = -EINVAL;
goto out;
}
}
out:
return offset;
}
/* When parsing error occurs, all the memory resource will be released
* in the end of tasdevice_rca_ready.
*/
static int fw_parse_program_data(struct tasdevice_priv *tas_priv,
struct tasdevice_fw *tas_fmw, const struct firmware *fmw, int offset)
{
unsigned char *buf = (unsigned char *)fmw->data;
struct tasdevice_prog *program;
int i;
if (offset + 2 > fmw->size) {
dev_err(tas_priv->dev, "%s: File Size error\n", __func__);
offset = -EINVAL;
goto out;
}
tas_fmw->nr_programs = get_unaligned_be16(&buf[offset]);
offset += 2;
if (tas_fmw->nr_programs == 0) {
/*Not error in calibration Data file, return directly*/
dev_info(tas_priv->dev, "%s: No Programs data, maybe calbin\n",
__func__);
goto out;
}
tas_fmw->programs =
kcalloc(tas_fmw->nr_programs, sizeof(struct tasdevice_prog),
GFP_KERNEL);
if (!tas_fmw->programs) {
offset = -ENOMEM;
goto out;
}
for (i = 0; i < tas_fmw->nr_programs; i++) {
int n = 0;
program = &(tas_fmw->programs[i]);
if (offset + 64 > fmw->size) {
dev_err(tas_priv->dev, "%s: mpName error\n", __func__);
offset = -EINVAL;
goto out;
}
offset += 64;
n = strlen((char *)&buf[offset]);
/* skip '\0' and 5 unused bytes */
n += 6;
if (offset + n > fmw->size) {
dev_err(tas_priv->dev, "Description err\n");
offset = -EINVAL;
goto out;
}
offset += n;
offset = fw_parse_data(tas_fmw, &(program->dev_data), fmw,
offset);
if (offset < 0)
goto out;
}
out:
return offset;
}
/* When parsing error occurs, all the memory resource will be released
* in the end of tasdevice_rca_ready.
*/
static int fw_parse_configuration_data(
struct tasdevice_priv *tas_priv,
struct tasdevice_fw *tas_fmw,
const struct firmware *fmw, int offset)
{
unsigned char *data = (unsigned char *)fmw->data;
struct tasdevice_config *config;
unsigned int i;
int n;
if (offset + 2 > fmw->size) {
dev_err(tas_priv->dev, "%s: File Size error\n", __func__);
offset = -EINVAL;
goto out;
}
tas_fmw->nr_configurations = get_unaligned_be16(&data[offset]);
offset += 2;
if (tas_fmw->nr_configurations == 0) {
dev_err(tas_priv->dev, "%s: Conf is zero\n", __func__);
/*Not error for calibration Data file, return directly*/
goto out;
}
tas_fmw->configs = kcalloc(tas_fmw->nr_configurations,
sizeof(struct tasdevice_config), GFP_KERNEL);
if (!tas_fmw->configs) {
offset = -ENOMEM;
goto out;
}
for (i = 0; i < tas_fmw->nr_configurations; i++) {
config = &(tas_fmw->configs[i]);
if (offset + 64 > fmw->size) {
dev_err(tas_priv->dev, "File Size err\n");
offset = -EINVAL;
goto out;
}
memcpy(config->name, &data[offset], 64);
offset += 64;
n = strlen((char *)&data[offset]);
n += 15;
if (offset + n > fmw->size) {
dev_err(tas_priv->dev, "Description err\n");
offset = -EINVAL;
goto out;
}
offset += n;
offset = fw_parse_data(tas_fmw, &(config->dev_data),
fmw, offset);
if (offset < 0)
goto out;
}
out:
return offset;
}
static bool check_inpage_yram_rg(struct tas_crc *cd,
unsigned char reg, unsigned char len)
{
bool in = false;
if (reg <= TAS2781_YRAM5_END_REG &&
reg >= TAS2781_YRAM5_START_REG) {
if (reg + len > TAS2781_YRAM5_END_REG)
cd->len = TAS2781_YRAM5_END_REG - reg + 1;
else
cd->len = len;
cd->offset = reg;
in = true;
} else if (reg < TAS2781_YRAM5_START_REG) {
if (reg + len > TAS2781_YRAM5_START_REG) {
cd->offset = TAS2781_YRAM5_START_REG;
cd->len = len - TAS2781_YRAM5_START_REG + reg;
in = true;
}
}
return in;
}
static bool check_inpage_yram_bk1(struct tas_crc *cd,
unsigned char page, unsigned char reg, unsigned char len)
{
bool in = false;
if (page == TAS2781_YRAM1_PAGE) {
if (reg >= TAS2781_YRAM1_START_REG) {
cd->offset = reg;
cd->len = len;
in = true;
} else if (reg + len > TAS2781_YRAM1_START_REG) {
cd->offset = TAS2781_YRAM1_START_REG;
cd->len = len - TAS2781_YRAM1_START_REG + reg;
in = true;
}
} else if (page == TAS2781_YRAM3_PAGE)
in = check_inpage_yram_rg(cd, reg, len);
return in;
}
/* Return Code:
* true -- the registers are in the inpage yram
* false -- the registers are NOT in the inpage yram
*/
static bool check_inpage_yram(struct tas_crc *cd, unsigned char book,
unsigned char page, unsigned char reg, unsigned char len)
{
bool in = false;
if (book == TAS2781_YRAM_BOOK1) {
in = check_inpage_yram_bk1(cd, page, reg, len);
goto end;
}
if (book == TAS2781_YRAM_BOOK2 && page == TAS2781_YRAM5_PAGE)
in = check_inpage_yram_rg(cd, reg, len);
end:
return in;
}
static bool check_inblock_yram_bk(struct tas_crc *cd,
unsigned char page, unsigned char reg, unsigned char len)
{
bool in = false;
if ((page >= TAS2781_YRAM4_START_PAGE &&
page <= TAS2781_YRAM4_END_PAGE) ||
(page >= TAS2781_YRAM2_START_PAGE &&
page <= TAS2781_YRAM2_END_PAGE)) {
if (reg <= TAS2781_YRAM2_END_REG &&
reg >= TAS2781_YRAM2_START_REG) {
cd->offset = reg;
cd->len = len;
in = true;
} else if (reg < TAS2781_YRAM2_START_REG) {
if (reg + len - 1 >= TAS2781_YRAM2_START_REG) {
cd->offset = TAS2781_YRAM2_START_REG;
cd->len = reg + len - TAS2781_YRAM2_START_REG;
in = true;
}
}
}
return in;
}
/* Return Code:
* true -- the registers are in the inblock yram
* false -- the registers are NOT in the inblock yram
*/
static bool check_inblock_yram(struct tas_crc *cd, unsigned char book,
unsigned char page, unsigned char reg, unsigned char len)
{
bool in = false;
if (book == TAS2781_YRAM_BOOK1 || book == TAS2781_YRAM_BOOK2)
in = check_inblock_yram_bk(cd, page, reg, len);
return in;
}
static bool check_yram(struct tas_crc *cd, unsigned char book,
unsigned char page, unsigned char reg, unsigned char len)
{
bool in;
in = check_inpage_yram(cd, book, page, reg, len);
if (in)
goto end;
in = check_inblock_yram(cd, book, page, reg, len);
end:
return in;
}
static int tasdev_multibytes_chksum(struct tasdevice_priv *tasdevice,
unsigned short chn, unsigned char book, unsigned char page,
unsigned char reg, unsigned int len)
{
struct tas_crc crc_data;
unsigned char crc_chksum = 0;
unsigned char nBuf1[128];
int ret = 0;
int i;
bool in;
if ((reg + len - 1) > 127) {
ret = -EINVAL;
dev_err(tasdevice->dev, "firmware error\n");
goto end;
}
if ((book == TASDEVICE_BOOK_ID(TAS2781_SA_COEFF_SWAP_REG))
&& (page == TASDEVICE_PAGE_ID(TAS2781_SA_COEFF_SWAP_REG))
&& (reg == TASDEVICE_PAGE_REG(TAS2781_SA_COEFF_SWAP_REG))
&& (len == 4)) {
/*DSP swap command, pass */
ret = 0;
goto end;
}
in = check_yram(&crc_data, book, page, reg, len);
if (!in)
goto end;
if (len == 1) {
dev_err(tasdevice->dev, "firmware error\n");
ret = -EINVAL;
goto end;
}
ret = tasdevice_dev_bulk_read(tasdevice, chn,
TASDEVICE_REG(book, page, crc_data.offset),
nBuf1, crc_data.len);
if (ret < 0)
goto end;
for (i = 0; i < crc_data.len; i++) {
if ((book == TASDEVICE_BOOK_ID(TAS2781_SA_COEFF_SWAP_REG))
&& (page == TASDEVICE_PAGE_ID(
TAS2781_SA_COEFF_SWAP_REG))
&& ((i + crc_data.offset)
>= TASDEVICE_PAGE_REG(TAS2781_SA_COEFF_SWAP_REG))
&& ((i + crc_data.offset)
<= (TASDEVICE_PAGE_REG(TAS2781_SA_COEFF_SWAP_REG)
+ 4)))
/*DSP swap command, bypass */
continue;
else
crc_chksum += crc8(tasdevice->crc8_lkp_tbl, &nBuf1[i],
1, 0);
}
ret = crc_chksum;
end:
return ret;
}
static int do_singlereg_checksum(struct tasdevice_priv *tasdevice,
unsigned short chl, unsigned char book, unsigned char page,
unsigned char reg, unsigned char val)
{
struct tas_crc crc_data;
unsigned int nData1;
int ret = 0;
bool in;
if ((book == TASDEVICE_BOOK_ID(TAS2781_SA_COEFF_SWAP_REG))
&& (page == TASDEVICE_PAGE_ID(TAS2781_SA_COEFF_SWAP_REG))
&& (reg >= TASDEVICE_PAGE_REG(TAS2781_SA_COEFF_SWAP_REG))
&& (reg <= (TASDEVICE_PAGE_REG(
TAS2781_SA_COEFF_SWAP_REG) + 4))) {
/*DSP swap command, pass */
ret = 0;
goto end;
}
in = check_yram(&crc_data, book, page, reg, 1);
if (!in)
goto end;
ret = tasdevice_dev_read(tasdevice, chl,
TASDEVICE_REG(book, page, reg), &nData1);
if (ret < 0)
goto end;
if (nData1 != val) {
dev_err(tasdevice->dev,
"B[0x%x]P[0x%x]R[0x%x] W[0x%x], R[0x%x]\n",
book, page, reg, val, nData1);
tasdevice->tasdevice[chl].err_code |= ERROR_YRAM_CRCCHK;
ret = -EAGAIN;
goto end;
}
ret = crc8(tasdevice->crc8_lkp_tbl, &val, 1, 0);
end:
return ret;
}
static void set_err_prg_cfg(unsigned int type, struct tasdevice *dev)
{
if ((type == MAIN_ALL_DEVICES) || (type == MAIN_DEVICE_A)
|| (type == MAIN_DEVICE_B) || (type == MAIN_DEVICE_C)
|| (type == MAIN_DEVICE_D))
dev->cur_prog = -1;
else
dev->cur_conf = -1;
}
static int tasdev_bytes_chksum(struct tasdevice_priv *tas_priv,
struct tasdev_blk *block, int chn, unsigned char book,
unsigned char page, unsigned char reg, unsigned int len,
unsigned char val, unsigned char *crc_chksum)
{
int ret;
if (len > 1)
ret = tasdev_multibytes_chksum(tas_priv, chn, book, page, reg,
len);
else
ret = do_singlereg_checksum(tas_priv, chn, book, page, reg,
val);
if (ret > 0) {
*crc_chksum += (unsigned char)ret;
goto end;
}
if (ret != -EAGAIN)
goto end;
block->nr_retry--;
if (block->nr_retry > 0)
goto end;
set_err_prg_cfg(block->type, &tas_priv->tasdevice[chn]);
end:
return ret;
}
static int tasdev_multibytes_wr(struct tasdevice_priv *tas_priv,
struct tasdev_blk *block, int chn, unsigned char book,
unsigned char page, unsigned char reg, unsigned char *data,
unsigned int len, unsigned int *nr_cmds,
unsigned char *crc_chksum)
{
int ret;
if (len > 1) {
ret = tasdevice_dev_bulk_write(tas_priv, chn,
TASDEVICE_REG(book, page, reg), data + 3, len);
if (ret < 0)
goto end;
if (block->is_ychksum_present)
ret = tasdev_bytes_chksum(tas_priv, block, chn,
book, page, reg, len, 0, crc_chksum);
} else {
ret = tasdevice_dev_write(tas_priv, chn,
TASDEVICE_REG(book, page, reg), data[3]);
if (ret < 0)
goto end;
if (block->is_ychksum_present)
ret = tasdev_bytes_chksum(tas_priv, block, chn, book,
page, reg, 1, data[3], crc_chksum);
}
if (!block->is_ychksum_present || ret >= 0) {
*nr_cmds += 1;
if (len >= 2)
*nr_cmds += ((len - 2) / 4) + 1;
}
end:
return ret;
}
static int tasdev_block_chksum(struct tasdevice_priv *tas_priv,
struct tasdev_blk *block, int chn)
{
unsigned int nr_value;
int ret;
ret = tasdevice_dev_read(tas_priv, chn, TASDEVICE_I2CChecksum,
&nr_value);
if (ret < 0) {
dev_err(tas_priv->dev, "%s: Chn %d\n", __func__, chn);
set_err_prg_cfg(block->type, &tas_priv->tasdevice[chn]);
goto end;
}
if ((nr_value & 0xff) != block->pchksum) {
dev_err(tas_priv->dev, "%s: Blk PChkSum Chn %d ", __func__,
chn);
dev_err(tas_priv->dev, "PChkSum = 0x%x, Reg = 0x%x\n",
block->pchksum, (nr_value & 0xff));
tas_priv->tasdevice[chn].err_code |= ERROR_PRAM_CRCCHK;
ret = -EAGAIN;
block->nr_retry--;
if (block->nr_retry <= 0)
set_err_prg_cfg(block->type,
&tas_priv->tasdevice[chn]);
} else
tas_priv->tasdevice[chn].err_code &= ~ERROR_PRAM_CRCCHK;
end:
return ret;
}
static int tasdev_load_blk(struct tasdevice_priv *tas_priv,
struct tasdev_blk *block, int chn)
{
unsigned int sleep_time;
unsigned int len;
unsigned int nr_cmds;
unsigned char *data;
unsigned char crc_chksum = 0;
unsigned char offset;
unsigned char book;
unsigned char page;
unsigned char val;
int ret = 0;
while (block->nr_retry > 0) {
if (block->is_pchksum_present) {
ret = tasdevice_dev_write(tas_priv, chn,
TASDEVICE_I2CChecksum, 0);
if (ret < 0)
break;
}
if (block->is_ychksum_present)
crc_chksum = 0;
nr_cmds = 0;
while (nr_cmds < block->nr_cmds) {
data = block->data + nr_cmds * 4;
book = data[0];
page = data[1];
offset = data[2];
val = data[3];
nr_cmds++;
/*Single byte write*/
if (offset <= 0x7F) {
ret = tasdevice_dev_write(tas_priv, chn,
TASDEVICE_REG(book, page, offset),
val);
if (ret < 0)
goto end;
if (block->is_ychksum_present) {
ret = tasdev_bytes_chksum(tas_priv,
block, chn, book, page, offset,
1, val, &crc_chksum);
if (ret < 0)
break;
}
continue;
}
/*sleep command*/
if (offset == 0x81) {
/*book -- data[0] page -- data[1]*/
sleep_time = ((book << 8) + page)*1000;
usleep_range(sleep_time, sleep_time + 50);
continue;
}
/*Multiple bytes write*/
if (offset == 0x85) {
data += 4;
len = (book << 8) + page;
book = data[0];
page = data[1];
offset = data[2];
ret = tasdev_multibytes_wr(tas_priv,
block, chn, book, page, offset, data,
len, &nr_cmds, &crc_chksum);
if (ret < 0)
break;
}
}
if (ret == -EAGAIN) {
if (block->nr_retry > 0)
continue;
} else if (ret < 0) /*err in current device, skip it*/
break;
if (block->is_pchksum_present) {
ret = tasdev_block_chksum(tas_priv, block, chn);
if (ret == -EAGAIN) {
if (block->nr_retry > 0)
continue;
} else if (ret < 0) /*err in current device, skip it*/
break;
}
if (block->is_ychksum_present) {
/* TBD, open it when FW ready */
dev_err(tas_priv->dev,
"Blk YChkSum: FW = 0x%x, YCRC = 0x%x\n",
block->ychksum, crc_chksum);
tas_priv->tasdevice[chn].err_code &=
~ERROR_YRAM_CRCCHK;
ret = 0;
}
/*skip current blk*/
break;
}
end:
return ret;
}
static int tasdevice_load_block(struct tasdevice_priv *tas_priv,
struct tasdev_blk *block)
{
int chnend = 0;
int ret = 0;
int chn = 0;
int rc = 0;
switch (block->type) {
case MAIN_ALL_DEVICES:
chn = 0;
chnend = tas_priv->ndev;
break;
case MAIN_DEVICE_A:
case COEFF_DEVICE_A:
case PRE_DEVICE_A:
chn = 0;
chnend = 1;
break;
case MAIN_DEVICE_B:
case COEFF_DEVICE_B:
case PRE_DEVICE_B:
chn = 1;
chnend = 2;
break;
case MAIN_DEVICE_C:
case COEFF_DEVICE_C:
case PRE_DEVICE_C:
chn = 2;
chnend = 3;
break;
case MAIN_DEVICE_D:
case COEFF_DEVICE_D:
case PRE_DEVICE_D:
chn = 3;
chnend = 4;
break;
default:
dev_dbg(tas_priv->dev, "load blk: Other Type = 0x%02x\n",
block->type);
break;
}
for (; chn < chnend; chn++) {
block->nr_retry = 6;
if (tas_priv->tasdevice[chn].is_loading == false)
continue;
ret = tasdev_load_blk(tas_priv, block, chn);
if (ret < 0)
dev_err(tas_priv->dev, "dev %d, Blk (%d) load error\n",
chn, block->type);
rc |= ret;
}
return rc;
}
static int dspfw_default_callback(struct tasdevice_priv *tas_priv,
unsigned int drv_ver, unsigned int ppcver)
{
int rc = 0;
if (drv_ver == 0x100) {
if (ppcver >= PPC3_VERSION) {
tas_priv->fw_parse_variable_header =
fw_parse_variable_header_kernel;
tas_priv->fw_parse_program_data =
fw_parse_program_data_kernel;
tas_priv->fw_parse_configuration_data =
fw_parse_configuration_data_kernel;
tas_priv->tasdevice_load_block =
tasdevice_load_block_kernel;
} else {
switch (ppcver) {
case 0x00:
tas_priv->fw_parse_variable_header =
fw_parse_variable_header_git;
tas_priv->fw_parse_program_data =
fw_parse_program_data;
tas_priv->fw_parse_configuration_data =
fw_parse_configuration_data;
tas_priv->tasdevice_load_block =
tasdevice_load_block;
break;
default:
dev_err(tas_priv->dev,
"%s: PPCVer must be 0x0 or 0x%02x",
__func__, PPC3_VERSION);
dev_err(tas_priv->dev, " Current:0x%02x\n",
ppcver);
rc = -EINVAL;
break;
}
}
} else {
dev_err(tas_priv->dev,
"DrvVer must be 0x0, 0x230 or above 0x230 ");
dev_err(tas_priv->dev, "current is 0x%02x\n", drv_ver);
rc = -EINVAL;
}
return rc;
}
static int load_calib_data(struct tasdevice_priv *tas_priv,
struct tasdevice_data *dev_data)
{
struct tasdev_blk *block;
unsigned int i;
int ret = 0;
for (i = 0; i < dev_data->nr_blk; i++) {
block = &(dev_data->dev_blks[i]);
ret = tasdevice_load_block(tas_priv, block);
if (ret < 0)
break;
}
return ret;
}
static int fw_parse_header(struct tasdevice_priv *tas_priv,
struct tasdevice_fw *tas_fmw, const struct firmware *fmw, int offset)
{
struct tasdevice_dspfw_hdr *fw_hdr = &(tas_fmw->fw_hdr);
struct tasdevice_fw_fixed_hdr *fw_fixed_hdr = &(fw_hdr->fixed_hdr);
static const unsigned char magic_number[] = { 0x35, 0x35, 0x35, 0x32 };
const unsigned char *buf = (unsigned char *)fmw->data;
if (offset + 92 > fmw->size) {
dev_err(tas_priv->dev, "%s: File Size error\n", __func__);
offset = -EINVAL;
goto out;
}
if (memcmp(&buf[offset], magic_number, 4)) {
dev_err(tas_priv->dev, "%s: Magic num NOT match\n", __func__);
offset = -EINVAL;
goto out;
}
offset += 4;
/* Convert data[offset], data[offset + 1], data[offset + 2] and
* data[offset + 3] into host
*/
fw_fixed_hdr->fwsize = get_unaligned_be32(&buf[offset]);
offset += 4;
if (fw_fixed_hdr->fwsize != fmw->size) {
dev_err(tas_priv->dev, "File size not match, %lu %u",
(unsigned long)fmw->size, fw_fixed_hdr->fwsize);
offset = -EINVAL;
goto out;
}
offset += 4;
fw_fixed_hdr->ppcver = get_unaligned_be32(&buf[offset]);
offset += 8;
fw_fixed_hdr->drv_ver = get_unaligned_be32(&buf[offset]);
offset += 72;
out:
return offset;
}
static int fw_parse_variable_hdr_cal(struct tasdevice_priv *tas_priv,
struct tasdevice_fw *tas_fmw, const struct firmware *fmw, int offset)
{
struct tasdevice_dspfw_hdr *fw_hdr = &(tas_fmw->fw_hdr);
offset = fw_parse_variable_hdr(tas_priv, fw_hdr, fmw, offset);
if (offset < 0)
goto out;
if (fw_hdr->ndev != 1) {
dev_err(tas_priv->dev,
"%s: calbin must be 1, but currently ndev(%u)\n",
__func__, fw_hdr->ndev);
offset = -EINVAL;
}
out:
return offset;
}
/* When calibrated data parsing error occurs, DSP can still work with default
* calibrated data, memory resource related to calibrated data will be
* released in the tasdevice_codec_remove.
*/
static int fw_parse_calibration_data(struct tasdevice_priv *tas_priv,
struct tasdevice_fw *tas_fmw, const struct firmware *fmw, int offset)
{
struct tasdevice_calibration *calibration;
unsigned char *data = (unsigned char *)fmw->data;
unsigned int i, n;
if (offset + 2 > fmw->size) {
dev_err(tas_priv->dev, "%s: Calibrations error\n", __func__);
offset = -EINVAL;
goto out;
}
tas_fmw->nr_calibrations = get_unaligned_be16(&data[offset]);
offset += 2;
if (tas_fmw->nr_calibrations != 1) {
dev_err(tas_priv->dev,
"%s: only supports one calibration (%d)!\n",
__func__, tas_fmw->nr_calibrations);
goto out;
}
tas_fmw->calibrations = kcalloc(tas_fmw->nr_calibrations,
sizeof(struct tasdevice_calibration), GFP_KERNEL);
if (!tas_fmw->calibrations) {
offset = -ENOMEM;
goto out;
}
for (i = 0; i < tas_fmw->nr_calibrations; i++) {
if (offset + 64 > fmw->size) {
dev_err(tas_priv->dev, "Calibrations error\n");
offset = -EINVAL;
goto out;
}
calibration = &(tas_fmw->calibrations[i]);
offset += 64;
n = strlen((char *)&data[offset]);
/* skip '\0' and 2 unused bytes */
n += 3;
if (offset + n > fmw->size) {
dev_err(tas_priv->dev, "Description err\n");
offset = -EINVAL;
goto out;
}
offset += n;
offset = fw_parse_data(tas_fmw, &(calibration->dev_data), fmw,
offset);
if (offset < 0)
goto out;
}
out:
return offset;
}
int tas2781_load_calibration(void *context, char *file_name,
unsigned short i)
{
struct tasdevice_priv *tas_priv = (struct tasdevice_priv *)context;
struct tasdevice *tasdev = &(tas_priv->tasdevice[i]);
const struct firmware *fw_entry = NULL;
struct tasdevice_fw *tas_fmw;
struct firmware fmw;
int offset = 0;
int ret;
ret = request_firmware(&fw_entry, file_name, tas_priv->dev);
if (ret) {
dev_err(tas_priv->dev, "%s: Request firmware %s failed\n",
__func__, file_name);
goto out;
}
if (!fw_entry->size) {
dev_err(tas_priv->dev, "%s: file read error: size = %lu\n",
__func__, (unsigned long)fw_entry->size);
ret = -EINVAL;
goto out;
}
fmw.size = fw_entry->size;
fmw.data = fw_entry->data;
tas_fmw = tasdev->cali_data_fmw = kzalloc(sizeof(struct tasdevice_fw),
GFP_KERNEL);
if (!tasdev->cali_data_fmw) {
ret = -ENOMEM;
goto out;
}
tas_fmw->dev = tas_priv->dev;
offset = fw_parse_header(tas_priv, tas_fmw, &fmw, offset);
if (offset == -EINVAL) {
dev_err(tas_priv->dev, "fw_parse_header EXIT!\n");
ret = offset;
goto out;
}
offset = fw_parse_variable_hdr_cal(tas_priv, tas_fmw, &fmw, offset);
if (offset == -EINVAL) {
dev_err(tas_priv->dev,
"%s: fw_parse_variable_header_cal EXIT!\n", __func__);
ret = offset;
goto out;
}
offset = fw_parse_program_data(tas_priv, tas_fmw, &fmw, offset);
if (offset < 0) {
dev_err(tas_priv->dev, "fw_parse_program_data EXIT!\n");
ret = offset;
goto out;
}
offset = fw_parse_configuration_data(tas_priv, tas_fmw, &fmw, offset);
if (offset < 0) {
dev_err(tas_priv->dev, "fw_parse_configuration_data EXIT!\n");
ret = offset;
goto out;
}
offset = fw_parse_calibration_data(tas_priv, tas_fmw, &fmw, offset);
if (offset < 0) {
dev_err(tas_priv->dev, "fw_parse_calibration_data EXIT!\n");
ret = offset;
goto out;
}
out:
if (fw_entry)
release_firmware(fw_entry);
return ret;
}
EXPORT_SYMBOL_NS_GPL(tas2781_load_calibration, SND_SOC_TAS2781_FMWLIB);
static int tasdevice_dspfw_ready(const struct firmware *fmw,
void *context)
{
struct tasdevice_priv *tas_priv = (struct tasdevice_priv *) context;
struct tasdevice_fw_fixed_hdr *fw_fixed_hdr;
struct tasdevice_fw *tas_fmw;
int offset = 0;
int ret = 0;
if (!fmw || !fmw->data) {
dev_err(tas_priv->dev, "%s: Failed to read firmware %s\n",
__func__, tas_priv->coef_binaryname);
ret = -EINVAL;
goto out;
}
tas_priv->fmw = kzalloc(sizeof(struct tasdevice_fw), GFP_KERNEL);
if (!tas_priv->fmw) {
ret = -ENOMEM;
goto out;
}
tas_fmw = tas_priv->fmw;
tas_fmw->dev = tas_priv->dev;
offset = fw_parse_header(tas_priv, tas_fmw, fmw, offset);
if (offset == -EINVAL) {
ret = -EINVAL;
goto out;
}
fw_fixed_hdr = &(tas_fmw->fw_hdr.fixed_hdr);
/* Support different versions of firmware */
switch (fw_fixed_hdr->drv_ver) {
case 0x301:
case 0x302:
case 0x502:
case 0x503:
tas_priv->fw_parse_variable_header =
fw_parse_variable_header_kernel;
tas_priv->fw_parse_program_data =
fw_parse_program_data_kernel;
tas_priv->fw_parse_configuration_data =
fw_parse_configuration_data_kernel;
tas_priv->tasdevice_load_block =
tasdevice_load_block_kernel;
break;
case 0x202:
case 0x400:
case 0x401:
tas_priv->fw_parse_variable_header =
fw_parse_variable_header_git;
tas_priv->fw_parse_program_data =
fw_parse_program_data;
tas_priv->fw_parse_configuration_data =
fw_parse_configuration_data;
tas_priv->tasdevice_load_block =
tasdevice_load_block;
break;
default:
ret = dspfw_default_callback(tas_priv,
fw_fixed_hdr->drv_ver, fw_fixed_hdr->ppcver);
if (ret)
goto out;
break;
}
offset = tas_priv->fw_parse_variable_header(tas_priv, fmw, offset);
if (offset < 0) {
ret = offset;
goto out;
}
offset = tas_priv->fw_parse_program_data(tas_priv, tas_fmw, fmw,
offset);
if (offset < 0) {
ret = offset;
goto out;
}
offset = tas_priv->fw_parse_configuration_data(tas_priv,
tas_fmw, fmw, offset);
if (offset < 0)
ret = offset;
out:
return ret;
}
int tasdevice_dsp_parser(void *context)
{
struct tasdevice_priv *tas_priv = (struct tasdevice_priv *)context;
const struct firmware *fw_entry;
int ret;
ret = request_firmware(&fw_entry, tas_priv->coef_binaryname,
tas_priv->dev);
if (ret) {
dev_err(tas_priv->dev, "%s: load %s error\n", __func__,
tas_priv->coef_binaryname);
goto out;
}
ret = tasdevice_dspfw_ready(fw_entry, tas_priv);
release_firmware(fw_entry);
fw_entry = NULL;
out:
return ret;
}
EXPORT_SYMBOL_NS_GPL(tasdevice_dsp_parser, SND_SOC_TAS2781_FMWLIB);
static void tas2781_clear_calfirmware(struct tasdevice_fw *tas_fmw)
{
struct tasdevice_calibration *calibration;
struct tasdev_blk *block;
struct tasdevice_data *im;
unsigned int blks;
int i;
if (!tas_fmw->calibrations)
goto out;
for (i = 0; i < tas_fmw->nr_calibrations; i++) {
calibration = &(tas_fmw->calibrations[i]);
if (!calibration)
continue;
im = &(calibration->dev_data);
if (!im->dev_blks)
continue;
for (blks = 0; blks < im->nr_blk; blks++) {
block = &(im->dev_blks[blks]);
if (!block)
continue;
kfree(block->data);
}
kfree(im->dev_blks);
}
kfree(tas_fmw->calibrations);
out:
kfree(tas_fmw);
}
void tasdevice_calbin_remove(void *context)
{
struct tasdevice_priv *tas_priv = (struct tasdevice_priv *) context;
struct tasdevice *tasdev;
int i;
if (!tas_priv)
return;
for (i = 0; i < tas_priv->ndev; i++) {
tasdev = &(tas_priv->tasdevice[i]);
if (!tasdev->cali_data_fmw)
continue;
tas2781_clear_calfirmware(tasdev->cali_data_fmw);
tasdev->cali_data_fmw = NULL;
}
}
EXPORT_SYMBOL_NS_GPL(tasdevice_calbin_remove, SND_SOC_TAS2781_FMWLIB);
void tasdevice_config_info_remove(void *context)
{
struct tasdevice_priv *tas_priv = (struct tasdevice_priv *) context;
struct tasdevice_rca *rca = &(tas_priv->rcabin);
struct tasdevice_config_info **ci = rca->cfg_info;
int i, j;
if (!ci)
return;
for (i = 0; i < rca->ncfgs; i++) {
if (!ci[i])
continue;
if (ci[i]->blk_data) {
for (j = 0; j < (int)ci[i]->real_nblocks; j++) {
if (!ci[i]->blk_data[j])
continue;
kfree(ci[i]->blk_data[j]->regdata);
kfree(ci[i]->blk_data[j]);
}
kfree(ci[i]->blk_data);
}
kfree(ci[i]);
}
kfree(ci);
}
EXPORT_SYMBOL_NS_GPL(tasdevice_config_info_remove, SND_SOC_TAS2781_FMWLIB);
static int tasdevice_load_data(struct tasdevice_priv *tas_priv,
struct tasdevice_data *dev_data)
{
struct tasdev_blk *block;
unsigned int i;
int ret = 0;
for (i = 0; i < dev_data->nr_blk; i++) {
block = &(dev_data->dev_blks[i]);
ret = tas_priv->tasdevice_load_block(tas_priv, block);
if (ret < 0)
break;
}
return ret;
}
static void tasdev_load_calibrated_data(struct tasdevice_priv *priv, int i)
{
struct tasdevice_fw *cal_fmw = priv->tasdevice[i].cali_data_fmw;
struct calidata *cali_data = &priv->cali_data;
struct cali_reg *p = &cali_data->cali_reg_array;
unsigned char *data = cali_data->data;
struct tasdevice_calibration *cal;
int k = i * (cali_data->cali_dat_sz_per_dev + 1);
int rc;
/* Load the calibrated data from cal bin file */
if (!priv->is_user_space_calidata && cal_fmw) {
cal = cal_fmw->calibrations;
if (cal)
load_calib_data(priv, &cal->dev_data);
return;
}
if (!priv->is_user_space_calidata)
return;
/* load calibrated data from user space */
if (data[k] != i) {
dev_err(priv->dev, "%s: no cal-data for dev %d from usr-spc\n",
__func__, i);
return;
}
k++;
rc = tasdevice_dev_bulk_write(priv, i, p->r0_reg, &(data[k]), 4);
if (rc < 0) {
dev_err(priv->dev, "chn %d r0_reg bulk_wr err = %d\n", i, rc);
return;
}
k += 4;
rc = tasdevice_dev_bulk_write(priv, i, p->r0_low_reg, &(data[k]), 4);
if (rc < 0) {
dev_err(priv->dev, "chn %d r0_low_reg err = %d\n", i, rc);
return;
}
k += 4;
rc = tasdevice_dev_bulk_write(priv, i, p->invr0_reg, &(data[k]), 4);
if (rc < 0) {
dev_err(priv->dev, "chn %d invr0_reg err = %d\n", i, rc);
return;
}
k += 4;
rc = tasdevice_dev_bulk_write(priv, i, p->pow_reg, &(data[k]), 4);
if (rc < 0) {
dev_err(priv->dev, "chn %d pow_reg bulk_wr err = %d\n", i, rc);
return;
}
k += 4;
rc = tasdevice_dev_bulk_write(priv, i, p->tlimit_reg, &(data[k]), 4);
if (rc < 0) {
dev_err(priv->dev, "chn %d tlimit_reg err = %d\n", i, rc);
return;
}
}
int tasdevice_select_tuningprm_cfg(void *context, int prm_no,
int cfg_no, int rca_conf_no)
{
struct tasdevice_priv *tas_priv = (struct tasdevice_priv *) context;
struct tasdevice_rca *rca = &(tas_priv->rcabin);
struct tasdevice_config_info **cfg_info = rca->cfg_info;
struct tasdevice_fw *tas_fmw = tas_priv->fmw;
struct tasdevice_prog *program;
struct tasdevice_config *conf;
int prog_status = 0;
int status, i;
if (!tas_fmw) {
dev_err(tas_priv->dev, "%s: Firmware is NULL\n", __func__);
goto out;
}
if (cfg_no >= tas_fmw->nr_configurations) {
dev_err(tas_priv->dev,
"%s: cfg(%d) is not in range of conf %u\n",
__func__, cfg_no, tas_fmw->nr_configurations);
goto out;
}
if (prm_no >= tas_fmw->nr_programs) {
dev_err(tas_priv->dev,
"%s: prm(%d) is not in range of Programs %u\n",
__func__, prm_no, tas_fmw->nr_programs);
goto out;
}
if (rca_conf_no >= rca->ncfgs || rca_conf_no < 0 ||
!cfg_info) {
dev_err(tas_priv->dev,
"conf_no:%d should be in range from 0 to %u\n",
rca_conf_no, rca->ncfgs-1);
goto out;
}
for (i = 0, prog_status = 0; i < tas_priv->ndev; i++) {
if (cfg_info[rca_conf_no]->active_dev & (1 << i)) {
if (prm_no >= 0
&& (tas_priv->tasdevice[i].cur_prog != prm_no
|| tas_priv->force_fwload_status)) {
tas_priv->tasdevice[i].cur_conf = -1;
tas_priv->tasdevice[i].is_loading = true;
prog_status++;
}
} else
tas_priv->tasdevice[i].is_loading = false;
tas_priv->tasdevice[i].is_loaderr = false;
}
if (prog_status) {
program = &(tas_fmw->programs[prm_no]);
tasdevice_load_data(tas_priv, &(program->dev_data));
for (i = 0; i < tas_priv->ndev; i++) {
if (tas_priv->tasdevice[i].is_loaderr == true)
continue;
if (tas_priv->tasdevice[i].is_loaderr == false &&
tas_priv->tasdevice[i].is_loading == true)
tas_priv->tasdevice[i].cur_prog = prm_no;
}
}
for (i = 0, status = 0; i < tas_priv->ndev; i++) {
if (cfg_no >= 0
&& tas_priv->tasdevice[i].cur_conf != cfg_no
&& (cfg_info[rca_conf_no]->active_dev & (1 << i))
&& (tas_priv->tasdevice[i].is_loaderr == false)) {
status++;
tas_priv->tasdevice[i].is_loading = true;
} else
tas_priv->tasdevice[i].is_loading = false;
}
if (status) {
conf = &(tas_fmw->configs[cfg_no]);
status = 0;
tasdevice_load_data(tas_priv, &(conf->dev_data));
for (i = 0; i < tas_priv->ndev; i++) {
if (tas_priv->tasdevice[i].is_loaderr == true) {
status |= BIT(i + 4);
continue;
}
if (tas_priv->tasdevice[i].is_loaderr == false &&
tas_priv->tasdevice[i].is_loading == true) {
tasdev_load_calibrated_data(tas_priv, i);
tas_priv->tasdevice[i].cur_conf = cfg_no;
}
}
} else {
dev_dbg(tas_priv->dev, "%s: Unneeded loading dsp conf %d\n",
__func__, cfg_no);
}
status |= cfg_info[rca_conf_no]->active_dev;
out:
return prog_status;
}
EXPORT_SYMBOL_NS_GPL(tasdevice_select_tuningprm_cfg,
SND_SOC_TAS2781_FMWLIB);
int tasdevice_prmg_load(void *context, int prm_no)
{
struct tasdevice_priv *tas_priv = (struct tasdevice_priv *) context;
struct tasdevice_fw *tas_fmw = tas_priv->fmw;
struct tasdevice_prog *program;
int prog_status = 0;
int i;
if (!tas_fmw) {
dev_err(tas_priv->dev, "%s: Firmware is NULL\n", __func__);
goto out;
}
if (prm_no >= tas_fmw->nr_programs) {
dev_err(tas_priv->dev,
"%s: prm(%d) is not in range of Programs %u\n",
__func__, prm_no, tas_fmw->nr_programs);
goto out;
}
for (i = 0, prog_status = 0; i < tas_priv->ndev; i++) {
if (prm_no >= 0 && tas_priv->tasdevice[i].cur_prog != prm_no) {
tas_priv->tasdevice[i].cur_conf = -1;
tas_priv->tasdevice[i].is_loading = true;
prog_status++;
}
}
if (prog_status) {
program = &(tas_fmw->programs[prm_no]);
tasdevice_load_data(tas_priv, &(program->dev_data));
for (i = 0; i < tas_priv->ndev; i++) {
if (tas_priv->tasdevice[i].is_loaderr == true)
continue;
else if (tas_priv->tasdevice[i].is_loaderr == false
&& tas_priv->tasdevice[i].is_loading == true)
tas_priv->tasdevice[i].cur_prog = prm_no;
}
}
out:
return prog_status;
}
EXPORT_SYMBOL_NS_GPL(tasdevice_prmg_load, SND_SOC_TAS2781_FMWLIB);
void tasdevice_tuning_switch(void *context, int state)
{
struct tasdevice_priv *tas_priv = (struct tasdevice_priv *) context;
struct tasdevice_fw *tas_fmw = tas_priv->fmw;
int profile_cfg_id = tas_priv->rcabin.profile_cfg_id;
/*
* Only RCA-based Playback can still work with no dsp program running
* inside the chip.
*/
switch (tas_priv->fw_state) {
case TASDEVICE_RCA_FW_OK:
case TASDEVICE_DSP_FW_ALL_OK:
break;
default:
return;
}
if (state == 0) {
if (tas_fmw && tas_priv->cur_prog < tas_fmw->nr_programs) {
/* dsp mode or tuning mode */
profile_cfg_id = tas_priv->rcabin.profile_cfg_id;
tasdevice_select_tuningprm_cfg(tas_priv,
tas_priv->cur_prog, tas_priv->cur_conf,
profile_cfg_id);
}
tasdevice_select_cfg_blk(tas_priv, profile_cfg_id,
TASDEVICE_BIN_BLK_PRE_POWER_UP);
} else {
tasdevice_select_cfg_blk(tas_priv, profile_cfg_id,
TASDEVICE_BIN_BLK_PRE_SHUTDOWN);
}
}
EXPORT_SYMBOL_NS_GPL(tasdevice_tuning_switch,
SND_SOC_TAS2781_FMWLIB);
MODULE_DESCRIPTION("Texas Firmware Support");
MODULE_AUTHOR("Shenghao Ding, TI, <shenghao-ding@ti.com>");
MODULE_LICENSE("GPL");
|