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
|
// SPDX-License-Identifier: GPL-2.0-or-later
//
// sma1303.c -- sma1303 ALSA SoC Audio driver
//
// Copyright 2023 Iron Device Corporation
//
// Auther: Gyuhwa Park <gyuhwa.park@irondevice.com>
// Kiseok Jo <kiseok.jo@irondevice.com>
#include <linux/module.h>
#include <linux/moduleparam.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/delay.h>
#include <linux/pm.h>
#include <linux/i2c.h>
#include <linux/regmap.h>
#include <sound/core.h>
#include <sound/pcm.h>
#include <sound/pcm_params.h>
#include <sound/soc.h>
#include <sound/initval.h>
#include <sound/tlv.h>
#include <linux/of_device.h>
#include <linux/slab.h>
#include <asm/div64.h>
#include "sma1303.h"
#define CHECK_PERIOD_TIME 1 /* sec per HZ */
#define MAX_CONTROL_NAME 48
#define PLL_MATCH(_input_clk_name, _output_clk_name, _input_clk,\
_post_n, _n, _vco, _p_cp)\
{\
.input_clk_name = _input_clk_name,\
.output_clk_name = _output_clk_name,\
.input_clk = _input_clk,\
.post_n = _post_n,\
.n = _n,\
.vco = _vco,\
.p_cp = _p_cp,\
}
enum sma1303_type {
SMA1303,
};
struct sma1303_pll_match {
char *input_clk_name;
char *output_clk_name;
unsigned int input_clk;
unsigned int post_n;
unsigned int n;
unsigned int vco;
unsigned int p_cp;
};
struct sma1303_priv {
enum sma1303_type devtype;
struct attribute_group *attr_grp;
struct delayed_work check_fault_work;
struct device *dev;
struct kobject *kobj;
struct regmap *regmap;
struct sma1303_pll_match *pll_matches;
bool amp_power_status;
bool force_mute_status;
int num_of_pll_matches;
int retry_cnt;
unsigned int amp_mode;
unsigned int cur_vol;
unsigned int format;
unsigned int frame_size;
unsigned int init_vol;
unsigned int last_bclk;
unsigned int last_ocp_val;
unsigned int last_over_temp;
unsigned int rev_num;
unsigned int sys_clk_id;
unsigned int tdm_slot_rx;
unsigned int tdm_slot_tx;
unsigned int tsdw_cnt;
long check_fault_period;
long check_fault_status;
};
static struct sma1303_pll_match sma1303_pll_matches[] = {
PLL_MATCH("1.411MHz", "24.595MHz", 1411200, 0x07, 0xF4, 0x8B, 0x03),
PLL_MATCH("1.536MHz", "24.576MHz", 1536000, 0x07, 0xE0, 0x8B, 0x03),
PLL_MATCH("3.072MHz", "24.576MHz", 3072000, 0x07, 0x70, 0x8B, 0x03),
PLL_MATCH("6.144MHz", "24.576MHz", 6144000, 0x07, 0x70, 0x8B, 0x07),
PLL_MATCH("12.288MHz", "24.576MHz", 12288000, 0x07, 0x70, 0x8B, 0x0B),
PLL_MATCH("19.2MHz", "24.343MHz", 19200000, 0x07, 0x47, 0x8B, 0x0A),
PLL_MATCH("24.576MHz", "24.576MHz", 24576000, 0x07, 0x70, 0x8B, 0x0F),
};
static int sma1303_startup(struct snd_soc_component *);
static int sma1303_shutdown(struct snd_soc_component *);
static const struct reg_default sma1303_reg_def[] = {
{ 0x00, 0x80 },
{ 0x01, 0x00 },
{ 0x02, 0x00 },
{ 0x03, 0x11 },
{ 0x04, 0x17 },
{ 0x09, 0x00 },
{ 0x0A, 0x31 },
{ 0x0B, 0x98 },
{ 0x0C, 0x84 },
{ 0x0D, 0x07 },
{ 0x0E, 0x3F },
{ 0x10, 0x00 },
{ 0x11, 0x00 },
{ 0x12, 0x00 },
{ 0x14, 0x5C },
{ 0x15, 0x01 },
{ 0x16, 0x0F },
{ 0x17, 0x0F },
{ 0x18, 0x0F },
{ 0x19, 0x00 },
{ 0x1A, 0x00 },
{ 0x1B, 0x00 },
{ 0x23, 0x19 },
{ 0x24, 0x00 },
{ 0x25, 0x00 },
{ 0x26, 0x04 },
{ 0x33, 0x00 },
{ 0x36, 0x92 },
{ 0x37, 0x27 },
{ 0x3B, 0x5A },
{ 0x3C, 0x20 },
{ 0x3D, 0x00 },
{ 0x3E, 0x03 },
{ 0x3F, 0x0C },
{ 0x8B, 0x07 },
{ 0x8C, 0x70 },
{ 0x8D, 0x8B },
{ 0x8E, 0x6F },
{ 0x8F, 0x03 },
{ 0x90, 0x26 },
{ 0x91, 0x42 },
{ 0x92, 0xE0 },
{ 0x94, 0x35 },
{ 0x95, 0x0C },
{ 0x96, 0x42 },
{ 0x97, 0x95 },
{ 0xA0, 0x00 },
{ 0xA1, 0x3B },
{ 0xA2, 0xC8 },
{ 0xA3, 0x28 },
{ 0xA4, 0x40 },
{ 0xA5, 0x01 },
{ 0xA6, 0x41 },
{ 0xA7, 0x00 },
};
static bool sma1303_readable_register(struct device *dev, unsigned int reg)
{
bool result;
if (reg > SMA1303_FF_DEVICE_INDEX)
return false;
switch (reg) {
case SMA1303_00_SYSTEM_CTRL ... SMA1303_04_INPUT1_CTRL4:
case SMA1303_09_OUTPUT_CTRL ... SMA1303_0E_MUTE_VOL_CTRL:
case SMA1303_10_SYSTEM_CTRL1 ... SMA1303_12_SYSTEM_CTRL3:
case SMA1303_14_MODULATOR ... SMA1303_1B_BASS_SPK7:
case SMA1303_23_COMP_LIM1 ... SMA1303_26_COMP_LIM4:
case SMA1303_33_SDM_CTRL ... SMA1303_34_OTP_DATA1:
case SMA1303_36_PROTECTION ... SMA1303_38_OTP_TRM0:
case SMA1303_3B_TEST1 ... SMA1303_3F_ATEST2:
case SMA1303_8B_PLL_POST_N ... SMA1303_92_FDPEC_CTRL:
case SMA1303_94_BOOST_CTRL1 ... SMA1303_97_BOOST_CTRL4:
case SMA1303_A0_PAD_CTRL0 ... SMA1303_A7_CLK_MON:
case SMA1303_FA_STATUS1 ... SMA1303_FB_STATUS2:
result = true;
break;
case SMA1303_FF_DEVICE_INDEX:
result = true;
break;
default:
result = false;
break;
}
return result;
}
static bool sma1303_writeable_register(struct device *dev, unsigned int reg)
{
bool result;
if (reg > SMA1303_FF_DEVICE_INDEX)
return false;
switch (reg) {
case SMA1303_00_SYSTEM_CTRL ... SMA1303_04_INPUT1_CTRL4:
case SMA1303_09_OUTPUT_CTRL ... SMA1303_0E_MUTE_VOL_CTRL:
case SMA1303_10_SYSTEM_CTRL1 ... SMA1303_12_SYSTEM_CTRL3:
case SMA1303_14_MODULATOR ... SMA1303_1B_BASS_SPK7:
case SMA1303_23_COMP_LIM1 ... SMA1303_26_COMP_LIM4:
case SMA1303_33_SDM_CTRL:
case SMA1303_36_PROTECTION ... SMA1303_37_SLOPE_CTRL:
case SMA1303_3B_TEST1 ... SMA1303_3F_ATEST2:
case SMA1303_8B_PLL_POST_N ... SMA1303_92_FDPEC_CTRL:
case SMA1303_94_BOOST_CTRL1 ... SMA1303_97_BOOST_CTRL4:
case SMA1303_A0_PAD_CTRL0 ... SMA1303_A7_CLK_MON:
result = true;
break;
default:
result = false;
break;
}
return result;
}
static bool sma1303_volatile_register(struct device *dev, unsigned int reg)
{
bool result;
switch (reg) {
case SMA1303_FA_STATUS1 ... SMA1303_FB_STATUS2:
result = true;
break;
case SMA1303_FF_DEVICE_INDEX:
result = true;
break;
default:
result = false;
break;
}
return result;
}
static const DECLARE_TLV_DB_SCALE(sma1303_spk_tlv, -6000, 50, 0);
static int sma1303_regmap_write(struct sma1303_priv *sma1303,
unsigned int reg, unsigned int val)
{
int ret = 0;
int cnt = sma1303->retry_cnt;
while (cnt--) {
ret = regmap_write(sma1303->regmap, reg, val);
if (ret < 0) {
dev_err(sma1303->dev,
"Failed to write [0x%02X]\n", reg);
} else
break;
}
return ret;
}
static int sma1303_regmap_update_bits(struct sma1303_priv *sma1303,
unsigned int reg, unsigned int mask, unsigned int val, bool *change)
{
int ret = 0;
int cnt = sma1303->retry_cnt;
while (cnt--) {
ret = regmap_update_bits_check(sma1303->regmap, reg,
mask, val, change);
if (ret < 0) {
dev_err(sma1303->dev,
"Failed to update [0x%02X]\n", reg);
} else
break;
}
return ret;
}
static int sma1303_regmap_read(struct sma1303_priv *sma1303,
unsigned int reg, unsigned int *val)
{
int ret = 0;
int cnt = sma1303->retry_cnt;
while (cnt--) {
ret = regmap_read(sma1303->regmap, reg, val);
if (ret < 0) {
dev_err(sma1303->dev,
"Failed to read [0x%02X]\n", reg);
} else
break;
}
return ret;
}
static const char * const sma1303_aif_in_source_text[] = {
"Mono", "Left", "Right"};
static const char * const sma1303_aif_out_source_text[] = {
"Disable", "After_FmtC", "After_Mixer", "After_DSP", "After_Post",
"Clk_PLL", "Clk_OSC"};
static const struct soc_enum sma1303_aif_in_source_enum =
SOC_ENUM_SINGLE_EXT(ARRAY_SIZE(sma1303_aif_in_source_text),
sma1303_aif_in_source_text);
static const struct soc_enum sma1303_aif_out_source_enum =
SOC_ENUM_SINGLE_EXT(ARRAY_SIZE(sma1303_aif_out_source_text),
sma1303_aif_out_source_text);
static int sma1303_force_mute_get(struct snd_kcontrol *kcontrol,
struct snd_ctl_elem_value *ucontrol)
{
struct snd_soc_component *component =
snd_soc_kcontrol_component(kcontrol);
struct sma1303_priv *sma1303 = snd_soc_component_get_drvdata(component);
ucontrol->value.integer.value[0] = (int)sma1303->force_mute_status;
dev_dbg(sma1303->dev, "%s : Force Mute %s\n", __func__,
sma1303->force_mute_status ? "ON" : "OFF");
return 0;
}
static int sma1303_force_mute_put(struct snd_kcontrol *kcontrol,
struct snd_ctl_elem_value *ucontrol)
{
struct snd_soc_component *component =
snd_soc_kcontrol_component(kcontrol);
struct sma1303_priv *sma1303 = snd_soc_component_get_drvdata(component);
bool change = false, val = (bool)ucontrol->value.integer.value[0];
if (sma1303->force_mute_status == val)
change = false;
else {
change = true;
sma1303->force_mute_status = val;
}
dev_dbg(sma1303->dev, "%s : Force Mute %s\n", __func__,
sma1303->force_mute_status ? "ON" : "OFF");
return change;
}
static int sma1303_postscaler_get(struct snd_kcontrol *kcontrol,
struct snd_ctl_elem_value *ucontrol)
{
struct snd_soc_component *component =
snd_soc_kcontrol_component(kcontrol);
struct sma1303_priv *sma1303 = snd_soc_component_get_drvdata(component);
int val, ret;
ret = sma1303_regmap_read(sma1303, SMA1303_90_POSTSCALER, &val);
if (ret < 0)
return -EINVAL;
ucontrol->value.integer.value[0] = (val & 0x7E) >> 1;
return 0;
}
static int sma1303_postscaler_put(struct snd_kcontrol *kcontrol,
struct snd_ctl_elem_value *ucontrol)
{
struct snd_soc_component *component =
snd_soc_kcontrol_component(kcontrol);
struct sma1303_priv *sma1303 = snd_soc_component_get_drvdata(component);
int ret, val = (int)ucontrol->value.integer.value[0];
bool change;
ret = sma1303_regmap_update_bits(sma1303,
SMA1303_90_POSTSCALER, 0x7E, (val << 1), &change);
if (ret < 0)
return -EINVAL;
return change;
}
static int sma1303_startup(struct snd_soc_component *component)
{
struct sma1303_priv *sma1303 = snd_soc_component_get_drvdata(component);
bool change = false, temp = false;
sma1303_regmap_update_bits(sma1303, SMA1303_8E_PLL_CTRL,
SMA1303_PLL_PD2_MASK, SMA1303_PLL_OPERATION2, &temp);
if (temp == true)
change = true;
sma1303_regmap_update_bits(sma1303, SMA1303_00_SYSTEM_CTRL,
SMA1303_POWER_MASK, SMA1303_POWER_ON, &temp);
if (temp == true)
change = true;
if (sma1303->amp_mode == SMA1303_MONO) {
sma1303_regmap_update_bits(sma1303,
SMA1303_10_SYSTEM_CTRL1,
SMA1303_SPK_MODE_MASK,
SMA1303_SPK_MONO,
&temp);
if (temp == true)
change = true;
} else {
sma1303_regmap_update_bits(sma1303,
SMA1303_10_SYSTEM_CTRL1,
SMA1303_SPK_MODE_MASK,
SMA1303_SPK_STEREO,
&temp);
if (temp == true)
change = true;
}
if (sma1303->check_fault_status) {
if (sma1303->check_fault_period > 0)
queue_delayed_work(system_freezable_wq,
&sma1303->check_fault_work,
sma1303->check_fault_period * HZ);
else
queue_delayed_work(system_freezable_wq,
&sma1303->check_fault_work,
CHECK_PERIOD_TIME * HZ);
}
sma1303->amp_power_status = true;
return change;
}
static int sma1303_shutdown(struct snd_soc_component *component)
{
struct sma1303_priv *sma1303 = snd_soc_component_get_drvdata(component);
bool change = false, temp = false;
cancel_delayed_work_sync(&sma1303->check_fault_work);
sma1303_regmap_update_bits(sma1303, SMA1303_10_SYSTEM_CTRL1,
SMA1303_SPK_MODE_MASK, SMA1303_SPK_OFF, &temp);
if (temp == true)
change = true;
sma1303_regmap_update_bits(sma1303, SMA1303_00_SYSTEM_CTRL,
SMA1303_POWER_MASK, SMA1303_POWER_OFF, &temp);
if (temp == true)
change = true;
sma1303_regmap_update_bits(sma1303, SMA1303_8E_PLL_CTRL,
SMA1303_PLL_PD2_MASK, SMA1303_PLL_PD2, &temp);
if (temp == true)
change = true;
sma1303->amp_power_status = false;
return change;
}
static int sma1303_aif_in_event(struct snd_soc_dapm_widget *w,
struct snd_kcontrol *kcontrol, int event)
{
struct snd_soc_component *component =
snd_soc_dapm_to_component(w->dapm);
struct sma1303_priv *sma1303 = snd_soc_component_get_drvdata(component);
unsigned int mux = dapm_kcontrol_get_value(w->kcontrols[0]);
int ret = 0;
bool change = false, temp = false;
switch (event) {
case SND_SOC_DAPM_PRE_PMU:
switch (mux) {
case 0:
ret += sma1303_regmap_update_bits(sma1303,
SMA1303_11_SYSTEM_CTRL2,
SMA1303_MONOMIX_MASK,
SMA1303_MONOMIX_ON,
&change);
sma1303->amp_mode = SMA1303_MONO;
break;
case 1:
ret += sma1303_regmap_update_bits(sma1303,
SMA1303_11_SYSTEM_CTRL2,
SMA1303_MONOMIX_MASK,
SMA1303_MONOMIX_OFF,
&temp);
if (temp == true)
change = true;
ret += sma1303_regmap_update_bits(sma1303,
SMA1303_11_SYSTEM_CTRL2,
SMA1303_LR_DATA_SW_MASK,
SMA1303_LR_DATA_SW_NORMAL,
&temp);
if (temp == true)
change = true;
sma1303->amp_mode = SMA1303_STEREO;
break;
case 2:
ret += sma1303_regmap_update_bits(sma1303,
SMA1303_11_SYSTEM_CTRL2,
SMA1303_MONOMIX_MASK,
SMA1303_MONOMIX_OFF,
&temp);
if (temp == true)
change = true;
ret += sma1303_regmap_update_bits(sma1303,
SMA1303_11_SYSTEM_CTRL2,
SMA1303_LR_DATA_SW_MASK,
SMA1303_LR_DATA_SW_NORMAL,
&temp);
if (temp == true)
change = true;
sma1303->amp_mode = SMA1303_STEREO;
break;
default:
dev_err(sma1303->dev, "%s : Invalid value (%d)\n",
__func__, mux);
return -EINVAL;
}
dev_dbg(sma1303->dev, "%s : Source : %s\n", __func__,
sma1303_aif_in_source_text[mux]);
break;
}
if (ret < 0)
return -EINVAL;
return change;
}
static int sma1303_aif_out_event(struct snd_soc_dapm_widget *w,
struct snd_kcontrol *kcontrol, int event)
{
struct snd_soc_component *component =
snd_soc_dapm_to_component(w->dapm);
struct sma1303_priv *sma1303 = snd_soc_component_get_drvdata(component);
unsigned int mux = dapm_kcontrol_get_value(w->kcontrols[0]);
int ret = 0;
bool change = false, temp = false;
switch (event) {
case SND_SOC_DAPM_PRE_PMU:
switch (mux) {
case 0:
ret += sma1303_regmap_update_bits(sma1303,
SMA1303_A3_TOP_MAN2,
SMA1303_TEST_CLKO_EN_MASK,
SMA1303_NORMAL_SDO,
&temp);
if (temp == true)
change = true;
ret += sma1303_regmap_update_bits(sma1303,
SMA1303_09_OUTPUT_CTRL,
SMA1303_PORT_OUT_SEL_MASK,
SMA1303_OUT_SEL_DISABLE,
&temp);
if (temp == true)
change = true;
break;
case 1:
ret += sma1303_regmap_update_bits(sma1303,
SMA1303_A3_TOP_MAN2,
SMA1303_TEST_CLKO_EN_MASK,
SMA1303_NORMAL_SDO,
&temp);
if (temp == true)
change = true;
ret += sma1303_regmap_update_bits(sma1303,
SMA1303_09_OUTPUT_CTRL,
SMA1303_PORT_OUT_SEL_MASK,
SMA1303_FORMAT_CONVERTER,
&temp);
if (temp == true)
change = true;
break;
case 2:
ret += sma1303_regmap_update_bits(sma1303,
SMA1303_A3_TOP_MAN2,
SMA1303_TEST_CLKO_EN_MASK,
SMA1303_NORMAL_SDO,
&temp);
if (temp == true)
change = true;
ret += sma1303_regmap_update_bits(sma1303,
SMA1303_09_OUTPUT_CTRL,
SMA1303_PORT_OUT_SEL_MASK,
SMA1303_MIXER_OUTPUT,
&temp);
if (temp == true)
change = true;
break;
case 3:
ret += sma1303_regmap_update_bits(sma1303,
SMA1303_A3_TOP_MAN2,
SMA1303_TEST_CLKO_EN_MASK,
SMA1303_NORMAL_SDO,
&temp);
if (temp == true)
change = true;
ret += sma1303_regmap_update_bits(sma1303,
SMA1303_09_OUTPUT_CTRL,
SMA1303_PORT_OUT_SEL_MASK,
SMA1303_SPEAKER_PATH,
&temp);
if (temp == true)
change = true;
break;
case 4:
ret += sma1303_regmap_update_bits(sma1303,
SMA1303_A3_TOP_MAN2,
SMA1303_TEST_CLKO_EN_MASK,
SMA1303_NORMAL_SDO,
&temp);
if (temp == true)
change = true;
ret += sma1303_regmap_update_bits(sma1303,
SMA1303_09_OUTPUT_CTRL,
SMA1303_PORT_OUT_SEL_MASK,
SMA1303_POSTSCALER_OUTPUT,
&temp);
if (temp == true)
change = true;
break;
case 5:
ret += sma1303_regmap_update_bits(sma1303,
SMA1303_A3_TOP_MAN2,
SMA1303_TEST_CLKO_EN_MASK,
SMA1303_CLK_OUT_SDO,
&temp);
if (temp == true)
change = true;
ret += sma1303_regmap_update_bits(sma1303,
SMA1303_A3_TOP_MAN2,
SMA1303_MON_OSC_PLL_MASK,
SMA1303_PLL_SDO,
&temp);
if (temp == true)
change = true;
break;
case 6:
ret += sma1303_regmap_update_bits(sma1303,
SMA1303_A3_TOP_MAN2,
SMA1303_TEST_CLKO_EN_MASK,
SMA1303_CLK_OUT_SDO,
&temp);
if (temp == true)
change = true;
ret += sma1303_regmap_update_bits(sma1303,
SMA1303_A3_TOP_MAN2,
SMA1303_MON_OSC_PLL_MASK,
SMA1303_OSC_SDO,
&temp);
if (temp == true)
change = true;
break;
default:
dev_err(sma1303->dev, "%s : Invalid value (%d)\n",
__func__, mux);
return -EINVAL;
}
dev_dbg(sma1303->dev, "%s : Source : %s\n", __func__,
sma1303_aif_out_source_text[mux]);
break;
}
if (ret < 0)
return -EINVAL;
return change;
}
static int sma1303_sdo_event(struct snd_soc_dapm_widget *w,
struct snd_kcontrol *kcontrol, int event)
{
struct snd_soc_component *component =
snd_soc_dapm_to_component(w->dapm);
struct sma1303_priv *sma1303 = snd_soc_component_get_drvdata(component);
int ret = 0;
bool change = false, temp = false;
switch (event) {
case SND_SOC_DAPM_PRE_PMU:
dev_dbg(sma1303->dev,
"%s : SND_SOC_DAPM_PRE_PMU\n", __func__);
ret += sma1303_regmap_update_bits(sma1303,
SMA1303_09_OUTPUT_CTRL,
SMA1303_PORT_CONFIG_MASK,
SMA1303_OUTPUT_PORT_ENABLE,
&temp);
if (temp == true)
change = true;
ret += sma1303_regmap_update_bits(sma1303,
SMA1303_A3_TOP_MAN2,
SMA1303_SDO_OUTPUT_MASK,
SMA1303_NORMAL_OUT,
&temp);
if (temp == true)
change = true;
break;
case SND_SOC_DAPM_POST_PMD:
dev_dbg(sma1303->dev,
"%s : SND_SOC_DAPM_POST_PMD\n", __func__);
ret += sma1303_regmap_update_bits(sma1303,
SMA1303_09_OUTPUT_CTRL,
SMA1303_PORT_CONFIG_MASK,
SMA1303_INPUT_PORT_ONLY,
&temp);
if (temp == true)
change = true;
ret += sma1303_regmap_update_bits(sma1303,
SMA1303_A3_TOP_MAN2,
SMA1303_SDO_OUTPUT_MASK,
SMA1303_HIGH_Z_OUT,
&temp);
if (temp == true)
change = true;
break;
}
if (ret < 0)
return -EINVAL;
return change;
}
static int sma1303_post_scaler_event(struct snd_soc_dapm_widget *w,
struct snd_kcontrol *kcontrol, int event)
{
struct snd_soc_component *component =
snd_soc_dapm_to_component(w->dapm);
struct sma1303_priv *sma1303 = snd_soc_component_get_drvdata(component);
int ret = 0;
bool change = false;
switch (event) {
case SND_SOC_DAPM_PRE_PMU:
dev_dbg(sma1303->dev,
"%s : SND_SOC_DAPM_PRE_PMU\n", __func__);
ret += sma1303_regmap_update_bits(sma1303,
SMA1303_90_POSTSCALER,
SMA1303_BYP_POST_MASK,
SMA1303_EN_POST_SCALER,
&change);
break;
case SND_SOC_DAPM_POST_PMD:
dev_dbg(sma1303->dev,
"%s : SND_SOC_DAPM_POST_PMD\n", __func__);
ret += sma1303_regmap_update_bits(sma1303,
SMA1303_90_POSTSCALER,
SMA1303_BYP_POST_MASK,
SMA1303_BYP_POST_SCALER,
&change);
break;
}
if (ret < 0)
return -EINVAL;
return change;
}
static int sma1303_power_event(struct snd_soc_dapm_widget *w,
struct snd_kcontrol *kcontrol, int event)
{
struct snd_soc_component *component =
snd_soc_dapm_to_component(w->dapm);
struct sma1303_priv *sma1303 = snd_soc_component_get_drvdata(component);
int ret = 0;
switch (event) {
case SND_SOC_DAPM_POST_PMU:
dev_dbg(sma1303->dev,
"%s : SND_SOC_DAPM_POST_PMU\n", __func__);
ret = sma1303_startup(component);
break;
case SND_SOC_DAPM_PRE_PMD:
dev_dbg(sma1303->dev,
"%s : SND_SOC_DAPM_PRE_PMD\n", __func__);
ret = sma1303_shutdown(component);
break;
}
return ret;
}
static const struct snd_kcontrol_new sma1303_aif_in_source_control =
SOC_DAPM_ENUM("AIF IN Source", sma1303_aif_in_source_enum);
static const struct snd_kcontrol_new sma1303_aif_out_source_control =
SOC_DAPM_ENUM("AIF OUT Source", sma1303_aif_out_source_enum);
static const struct snd_kcontrol_new sma1303_sdo_control =
SOC_DAPM_SINGLE_VIRT("Switch", 1);
static const struct snd_kcontrol_new sma1303_post_scaler_control =
SOC_DAPM_SINGLE_VIRT("Switch", 1);
static const struct snd_kcontrol_new sma1303_enable_control =
SOC_DAPM_SINGLE_VIRT("Switch", 1);
static const struct snd_kcontrol_new sma1303_snd_controls[] = {
SOC_SINGLE_TLV("Speaker Volume", SMA1303_0A_SPK_VOL,
0, 167, 1, sma1303_spk_tlv),
SOC_SINGLE_BOOL_EXT("Force Mute Switch", 0,
sma1303_force_mute_get, sma1303_force_mute_put),
SOC_SINGLE_EXT("Postscaler Gain", SMA1303_90_POSTSCALER, 1, 0x30, 0,
sma1303_postscaler_get, sma1303_postscaler_put),
};
static const struct snd_soc_dapm_widget sma1303_dapm_widgets[] = {
/* platform domain */
SND_SOC_DAPM_OUTPUT("SPK"),
SND_SOC_DAPM_INPUT("SDO"),
/* path domain */
SND_SOC_DAPM_MUX_E("AIF IN Source", SND_SOC_NOPM, 0, 0,
&sma1303_aif_in_source_control,
sma1303_aif_in_event,
SND_SOC_DAPM_PRE_PMU),
SND_SOC_DAPM_MUX_E("AIF OUT Source", SND_SOC_NOPM, 0, 0,
&sma1303_aif_out_source_control,
sma1303_aif_out_event,
SND_SOC_DAPM_PRE_PMU),
SND_SOC_DAPM_SWITCH_E("SDO Enable", SND_SOC_NOPM, 0, 0,
&sma1303_sdo_control,
sma1303_sdo_event,
SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
SND_SOC_DAPM_MIXER("Entry", SND_SOC_NOPM, 0, 0, NULL, 0),
SND_SOC_DAPM_SWITCH_E("Post Scaler", SND_SOC_NOPM, 0, 1,
&sma1303_post_scaler_control,
sma1303_post_scaler_event,
SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
SND_SOC_DAPM_OUT_DRV_E("AMP Power", SND_SOC_NOPM, 0, 0, NULL, 0,
sma1303_power_event,
SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
SND_SOC_DAPM_SWITCH("AMP Enable", SND_SOC_NOPM, 0, 1,
&sma1303_enable_control),
/* stream domain */
SND_SOC_DAPM_AIF_IN("AIF IN", "Playback", 0, SND_SOC_NOPM, 0, 0),
SND_SOC_DAPM_AIF_OUT("AIF OUT", "Capture", 0, SND_SOC_NOPM, 0, 0),
};
static const struct snd_soc_dapm_route sma1303_audio_map[] = {
/* Playback */
{"AIF IN Source", "Mono", "AIF IN"},
{"AIF IN Source", "Left", "AIF IN"},
{"AIF IN Source", "Right", "AIF IN"},
{"SDO Enable", "Switch", "AIF IN"},
{"AIF OUT Source", "Disable", "SDO Enable"},
{"AIF OUT Source", "After_FmtC", "SDO Enable"},
{"AIF OUT Source", "After_Mixer", "SDO Enable"},
{"AIF OUT Source", "After_DSP", "SDO Enable"},
{"AIF OUT Source", "After_Post", "SDO Enable"},
{"AIF OUT Source", "Clk_PLL", "SDO Enable"},
{"AIF OUT Source", "Clk_OSC", "SDO Enable"},
{"Entry", NULL, "AIF OUT Source"},
{"Entry", NULL, "AIF IN Source"},
{"Post Scaler", "Switch", "Entry"},
{"AMP Power", NULL, "Entry"},
{"AMP Power", NULL, "Entry"},
{"AMP Enable", "Switch", "AMP Power"},
{"SPK", NULL, "AMP Enable"},
/* Capture */
{"AIF OUT", NULL, "AMP Enable"},
};
static int sma1303_setup_pll(struct snd_soc_component *component,
unsigned int bclk)
{
struct sma1303_priv *sma1303 = snd_soc_component_get_drvdata(component);
int i = 0, ret = 0;
dev_dbg(component->dev, "%s : BCLK = %dHz\n",
__func__, bclk);
if (sma1303->sys_clk_id == SMA1303_PLL_CLKIN_MCLK) {
dev_dbg(component->dev, "%s : MCLK is not supported\n",
__func__);
} else if (sma1303->sys_clk_id == SMA1303_PLL_CLKIN_BCLK) {
for (i = 0; i < sma1303->num_of_pll_matches; i++) {
if (sma1303->pll_matches[i].input_clk == bclk)
break;
}
if (i == sma1303->num_of_pll_matches) {
dev_dbg(component->dev, "%s : No matching value between pll table and SCK\n",
__func__);
return -EINVAL;
}
ret += sma1303_regmap_update_bits(sma1303,
SMA1303_A2_TOP_MAN1,
SMA1303_PLL_PD_MASK|SMA1303_PLL_REF_CLK_MASK,
SMA1303_PLL_OPERATION|SMA1303_PLL_SCK,
NULL);
}
ret += sma1303_regmap_write(sma1303,
SMA1303_8B_PLL_POST_N,
sma1303->pll_matches[i].post_n);
ret += sma1303_regmap_write(sma1303,
SMA1303_8C_PLL_N,
sma1303->pll_matches[i].n);
ret += sma1303_regmap_write(sma1303,
SMA1303_8D_PLL_A_SETTING,
sma1303->pll_matches[i].vco);
ret += sma1303_regmap_write(sma1303,
SMA1303_8F_PLL_P_CP,
sma1303->pll_matches[i].p_cp);
if (ret < 0)
return -EINVAL;
return 0;
}
static int sma1303_dai_hw_params_amp(struct snd_pcm_substream *substream,
struct snd_pcm_hw_params *params, struct snd_soc_dai *dai)
{
struct snd_soc_component *component = dai->component;
struct sma1303_priv *sma1303 = snd_soc_component_get_drvdata(component);
unsigned int bclk = 0;
int ret = 0;
if (sma1303->format == SND_SOC_DAIFMT_DSP_A)
bclk = params_rate(params) * sma1303->frame_size;
else
bclk = params_rate(params) * params_physical_width(params)
* params_channels(params);
dev_dbg(component->dev,
"%s : rate = %d : bit size = %d : channel = %d\n",
__func__, params_rate(params), params_width(params),
params_channels(params));
if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
if (sma1303->sys_clk_id == SMA1303_PLL_CLKIN_MCLK
|| sma1303->sys_clk_id == SMA1303_PLL_CLKIN_BCLK) {
if (sma1303->last_bclk != bclk) {
sma1303_setup_pll(component, bclk);
sma1303->last_bclk = bclk;
}
}
switch (params_rate(params)) {
case 8000:
case 12000:
case 16000:
case 24000:
case 32000:
case 44100:
case 48000:
case 96000:
ret += sma1303_regmap_update_bits(sma1303,
SMA1303_A2_TOP_MAN1,
SMA1303_DAC_DN_CONV_MASK,
SMA1303_DAC_DN_CONV_DISABLE,
NULL);
ret += sma1303_regmap_update_bits(sma1303,
SMA1303_01_INPUT1_CTRL1,
SMA1303_LEFTPOL_MASK,
SMA1303_LOW_FIRST_CH,
NULL);
break;
case 192000:
ret += sma1303_regmap_update_bits(sma1303,
SMA1303_A2_TOP_MAN1,
SMA1303_DAC_DN_CONV_MASK,
SMA1303_DAC_DN_CONV_ENABLE,
NULL);
ret += sma1303_regmap_update_bits(sma1303,
SMA1303_01_INPUT1_CTRL1,
SMA1303_LEFTPOL_MASK,
SMA1303_HIGH_FIRST_CH,
NULL);
break;
default:
dev_err(component->dev, "%s not support rate : %d\n",
__func__, params_rate(params));
return -EINVAL;
}
} else {
switch (params_format(params)) {
case SNDRV_PCM_FORMAT_S16_LE:
dev_dbg(component->dev,
"%s set format SNDRV_PCM_FORMAT_S16_LE\n",
__func__);
ret += sma1303_regmap_update_bits(sma1303,
SMA1303_A4_TOP_MAN3,
SMA1303_SCK_RATE_MASK,
SMA1303_SCK_32FS,
NULL);
break;
case SNDRV_PCM_FORMAT_S24_LE:
dev_dbg(component->dev,
"%s set format SNDRV_PCM_FORMAT_S24_LE\n",
__func__);
ret += sma1303_regmap_update_bits(sma1303,
SMA1303_A4_TOP_MAN3,
SMA1303_SCK_RATE_MASK,
SMA1303_SCK_64FS,
NULL);
break;
case SNDRV_PCM_FORMAT_S32_LE:
dev_dbg(component->dev,
"%s set format SNDRV_PCM_FORMAT_S32_LE\n",
__func__);
ret += sma1303_regmap_update_bits(sma1303,
SMA1303_A4_TOP_MAN3,
SMA1303_SCK_RATE_MASK,
SMA1303_SCK_64FS,
NULL);
break;
default:
dev_err(component->dev,
"%s not support data bit : %d\n", __func__,
params_format(params));
return -EINVAL;
}
}
switch (sma1303->format) {
case SND_SOC_DAIFMT_I2S:
ret += sma1303_regmap_update_bits(sma1303,
SMA1303_01_INPUT1_CTRL1,
SMA1303_I2S_MODE_MASK,
SMA1303_STANDARD_I2S,
NULL);
ret += sma1303_regmap_update_bits(sma1303,
SMA1303_A4_TOP_MAN3,
SMA1303_O_FORMAT_MASK,
SMA1303_O_FMT_I2S,
NULL);
break;
case SND_SOC_DAIFMT_LEFT_J:
ret += sma1303_regmap_update_bits(sma1303,
SMA1303_01_INPUT1_CTRL1,
SMA1303_I2S_MODE_MASK,
SMA1303_LJ,
NULL);
ret += sma1303_regmap_update_bits(sma1303,
SMA1303_A4_TOP_MAN3,
SMA1303_O_FORMAT_MASK,
SMA1303_O_FMT_LJ,
NULL);
break;
case SND_SOC_DAIFMT_RIGHT_J:
switch (params_width(params)) {
case 16:
ret += sma1303_regmap_update_bits(sma1303,
SMA1303_01_INPUT1_CTRL1,
SMA1303_I2S_MODE_MASK,
SMA1303_RJ_16BIT,
NULL);
break;
case 24:
case 32:
ret += sma1303_regmap_update_bits(sma1303,
SMA1303_01_INPUT1_CTRL1,
SMA1303_I2S_MODE_MASK,
SMA1303_RJ_24BIT,
NULL);
break;
}
break;
case SND_SOC_DAIFMT_DSP_A:
ret += sma1303_regmap_update_bits(sma1303,
SMA1303_01_INPUT1_CTRL1,
SMA1303_I2S_MODE_MASK,
SMA1303_STANDARD_I2S,
NULL);
ret += sma1303_regmap_update_bits(sma1303,
SMA1303_A4_TOP_MAN3,
SMA1303_O_FORMAT_MASK,
SMA1303_O_FMT_TDM,
NULL);
break;
}
switch (params_width(params)) {
case 16:
case 24:
case 32:
break;
default:
dev_err(component->dev,
"%s not support data bit : %d\n", __func__,
params_format(params));
return -EINVAL;
}
if (ret < 0)
return -EINVAL;
return 0;
}
static int sma1303_dai_set_sysclk_amp(struct snd_soc_dai *dai,
int clk_id, unsigned int freq, int dir)
{
struct snd_soc_component *component = dai->component;
struct sma1303_priv *sma1303 = snd_soc_component_get_drvdata(component);
switch (clk_id) {
case SMA1303_EXTERNAL_CLOCK_19_2:
break;
case SMA1303_EXTERNAL_CLOCK_24_576:
break;
case SMA1303_PLL_CLKIN_MCLK:
break;
case SMA1303_PLL_CLKIN_BCLK:
break;
default:
dev_err(component->dev, "Invalid clk id: %d\n", clk_id);
return -EINVAL;
}
sma1303->sys_clk_id = clk_id;
return 0;
}
static int sma1303_dai_mute(struct snd_soc_dai *dai, int mute, int stream)
{
struct snd_soc_component *component = dai->component;
struct sma1303_priv *sma1303 = snd_soc_component_get_drvdata(component);
int ret = 0;
if (stream == SNDRV_PCM_STREAM_CAPTURE)
return ret;
if (mute) {
dev_dbg(component->dev, "%s : %s\n", __func__, "MUTE");
ret += sma1303_regmap_update_bits(sma1303,
SMA1303_0E_MUTE_VOL_CTRL,
SMA1303_SPK_MUTE_MASK,
SMA1303_SPK_MUTE,
NULL);
/* Need to wait time for mute slope */
msleep(55);
} else {
if (!sma1303->force_mute_status) {
dev_dbg(component->dev, "%s : %s\n",
__func__, "UNMUTE");
ret += sma1303_regmap_update_bits(sma1303,
SMA1303_0E_MUTE_VOL_CTRL,
SMA1303_SPK_MUTE_MASK,
SMA1303_SPK_UNMUTE,
NULL);
} else {
dev_dbg(sma1303->dev,
"%s : FORCE MUTE!!!\n", __func__);
}
}
if (ret < 0)
return -EINVAL;
return 0;
}
static int sma1303_dai_set_fmt_amp(struct snd_soc_dai *dai,
unsigned int fmt)
{
struct snd_soc_component *component = dai->component;
struct sma1303_priv *sma1303 = snd_soc_component_get_drvdata(component);
int ret = 0;
switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
case SND_SOC_DAIFMT_CBC_CFC:
dev_dbg(component->dev,
"%s : %s\n", __func__, "I2S/TDM Device mode");
ret += sma1303_regmap_update_bits(sma1303,
SMA1303_01_INPUT1_CTRL1,
SMA1303_CONTROLLER_DEVICE_MASK,
SMA1303_DEVICE_MODE,
NULL);
break;
case SND_SOC_DAIFMT_CBP_CFP:
dev_dbg(component->dev,
"%s : %s\n", __func__, "I2S/TDM Controller mode");
ret += sma1303_regmap_update_bits(sma1303,
SMA1303_01_INPUT1_CTRL1,
SMA1303_CONTROLLER_DEVICE_MASK,
SMA1303_CONTROLLER_MODE,
NULL);
break;
default:
dev_err(component->dev,
"Unsupported Controller/Device : 0x%x\n", fmt);
return -EINVAL;
}
switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
case SND_SOC_DAIFMT_I2S:
case SND_SOC_DAIFMT_RIGHT_J:
case SND_SOC_DAIFMT_LEFT_J:
case SND_SOC_DAIFMT_DSP_A:
case SND_SOC_DAIFMT_DSP_B:
sma1303->format = fmt & SND_SOC_DAIFMT_FORMAT_MASK;
break;
default:
dev_err(component->dev,
"Unsupported Audio Interface Format : 0x%x\n", fmt);
return -EINVAL;
}
switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
case SND_SOC_DAIFMT_IB_NF:
dev_dbg(component->dev, "%s : %s\n",
__func__, "Invert BCLK + Normal Frame");
ret += sma1303_regmap_update_bits(sma1303,
SMA1303_01_INPUT1_CTRL1,
SMA1303_SCK_RISING_MASK,
SMA1303_SCK_RISING_EDGE,
NULL);
break;
case SND_SOC_DAIFMT_IB_IF:
dev_dbg(component->dev, "%s : %s\n",
__func__, "Invert BCLK + Invert Frame");
ret += sma1303_regmap_update_bits(sma1303,
SMA1303_01_INPUT1_CTRL1,
SMA1303_LEFTPOL_MASK|SMA1303_SCK_RISING_MASK,
SMA1303_HIGH_FIRST_CH|SMA1303_SCK_RISING_EDGE,
NULL);
break;
case SND_SOC_DAIFMT_NB_IF:
dev_dbg(component->dev, "%s : %s\n",
__func__, "Normal BCLK + Invert Frame");
ret += sma1303_regmap_update_bits(sma1303,
SMA1303_01_INPUT1_CTRL1,
SMA1303_LEFTPOL_MASK,
SMA1303_HIGH_FIRST_CH,
NULL);
break;
case SND_SOC_DAIFMT_NB_NF:
dev_dbg(component->dev, "%s : %s\n",
__func__, "Normal BCLK + Normal Frame");
break;
default:
dev_err(component->dev,
"Unsupported Bit & Frameclock : 0x%x\n", fmt);
return -EINVAL;
}
if (ret < 0)
return -EINVAL;
return 0;
}
static int sma1303_dai_set_tdm_slot(struct snd_soc_dai *dai,
unsigned int tx_mask, unsigned int rx_mask,
int slots, int slot_width)
{
struct snd_soc_component *component = dai->component;
struct sma1303_priv *sma1303 = snd_soc_component_get_drvdata(component);
int ret = 0;
dev_dbg(component->dev, "%s : slots = %d, slot_width - %d\n",
__func__, slots, slot_width);
sma1303->frame_size = slot_width * slots;
ret += sma1303_regmap_update_bits(sma1303,
SMA1303_A4_TOP_MAN3,
SMA1303_O_FORMAT_MASK,
SMA1303_O_FMT_TDM,
NULL);
switch (slot_width) {
case 16:
ret += sma1303_regmap_update_bits(sma1303,
SMA1303_A6_TDM2,
SMA1303_TDM_DL_MASK,
SMA1303_TDM_DL_16,
NULL);
break;
case 32:
ret += sma1303_regmap_update_bits(sma1303,
SMA1303_A6_TDM2,
SMA1303_TDM_DL_MASK,
SMA1303_TDM_DL_32,
NULL);
break;
default:
dev_err(component->dev, "%s not support TDM %d slot_width\n",
__func__, slot_width);
break;
}
switch (slots) {
case 4:
ret += sma1303_regmap_update_bits(sma1303,
SMA1303_A6_TDM2,
SMA1303_TDM_N_SLOT_MASK,
SMA1303_TDM_N_SLOT_4,
NULL);
break;
case 8:
ret += sma1303_regmap_update_bits(sma1303,
SMA1303_A6_TDM2,
SMA1303_TDM_N_SLOT_MASK,
SMA1303_TDM_N_SLOT_8,
NULL);
break;
default:
dev_err(component->dev, "%s not support TDM %d slots\n",
__func__, slots);
break;
}
if (sma1303->tdm_slot_rx < slots)
ret += sma1303_regmap_update_bits(sma1303,
SMA1303_A5_TDM1,
SMA1303_TDM_SLOT1_RX_POS_MASK,
(sma1303->tdm_slot_rx) << 3,
NULL);
else
dev_err(component->dev, "%s Incorrect tdm-slot-rx %d set\n",
__func__, sma1303->tdm_slot_rx);
ret += sma1303_regmap_update_bits(sma1303,
SMA1303_A5_TDM1,
SMA1303_TDM_CLK_POL_MASK,
SMA1303_TDM_CLK_POL_RISE,
NULL);
ret += sma1303_regmap_update_bits(sma1303,
SMA1303_A5_TDM1,
SMA1303_TDM_TX_MODE_MASK,
SMA1303_TDM_TX_MONO,
NULL);
if (sma1303->tdm_slot_tx < slots)
ret += sma1303_regmap_update_bits(sma1303,
SMA1303_A6_TDM2,
SMA1303_TDM_SLOT1_TX_POS_MASK,
(sma1303->tdm_slot_tx) << 3,
NULL);
else
dev_err(component->dev, "%s Incorrect tdm-slot-tx %d set\n",
__func__, sma1303->tdm_slot_tx);
if (ret < 0)
return -EINVAL;
return 0;
}
static const struct snd_soc_dai_ops sma1303_dai_ops_amp = {
.set_sysclk = sma1303_dai_set_sysclk_amp,
.set_fmt = sma1303_dai_set_fmt_amp,
.hw_params = sma1303_dai_hw_params_amp,
.mute_stream = sma1303_dai_mute,
.set_tdm_slot = sma1303_dai_set_tdm_slot,
};
#define SMA1303_RATES SNDRV_PCM_RATE_8000_192000
#define SMA1303_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S24_LE | \
SNDRV_PCM_FMTBIT_S32_LE)
static struct snd_soc_dai_driver sma1303_dai[] = {
{
.name = "sma1303-amplifier",
.id = 0,
.playback = {
.stream_name = "Playback",
.channels_min = 1,
.channels_max = 2,
.rates = SMA1303_RATES,
.formats = SMA1303_FORMATS,
},
.capture = {
.stream_name = "Capture",
.channels_min = 1,
.channels_max = 2,
.rates = SMA1303_RATES,
.formats = SMA1303_FORMATS,
},
.ops = &sma1303_dai_ops_amp,
},
};
static void sma1303_check_fault_worker(struct work_struct *work)
{
struct sma1303_priv *sma1303 =
container_of(work, struct sma1303_priv, check_fault_work.work);
int ret = 0;
unsigned int over_temp, ocp_val, uvlo_val;
if (sma1303->tsdw_cnt)
ret = sma1303_regmap_read(sma1303,
SMA1303_0A_SPK_VOL, &sma1303->cur_vol);
else
ret = sma1303_regmap_read(sma1303,
SMA1303_0A_SPK_VOL, &sma1303->init_vol);
if (ret != 0) {
dev_err(sma1303->dev,
"failed to read SMA1303_0A_SPK_VOL : %d\n", ret);
return;
}
ret = sma1303_regmap_read(sma1303, SMA1303_FA_STATUS1, &over_temp);
if (ret != 0) {
dev_err(sma1303->dev,
"failed to read SMA1303_FA_STATUS1 : %d\n", ret);
return;
}
ret = sma1303_regmap_read(sma1303, SMA1303_FB_STATUS2, &ocp_val);
if (ret != 0) {
dev_err(sma1303->dev,
"failed to read SMA1303_FB_STATUS2 : %d\n", ret);
return;
}
ret = sma1303_regmap_read(sma1303, SMA1303_FF_DEVICE_INDEX, &uvlo_val);
if (ret != 0) {
dev_err(sma1303->dev,
"failed to read SMA1303_FF_DEVICE_INDEX : %d\n", ret);
return;
}
if (~over_temp & SMA1303_OT1_OK_STATUS) {
dev_crit(sma1303->dev,
"%s : OT1(Over Temperature Level 1)\n", __func__);
if ((sma1303->cur_vol + 6) <= 0xFF)
sma1303_regmap_write(sma1303,
SMA1303_0A_SPK_VOL, sma1303->cur_vol + 6);
sma1303->tsdw_cnt++;
} else if (sma1303->tsdw_cnt) {
sma1303_regmap_write(sma1303,
SMA1303_0A_SPK_VOL, sma1303->init_vol);
sma1303->tsdw_cnt = 0;
sma1303->cur_vol = sma1303->init_vol;
}
if (~over_temp & SMA1303_OT2_OK_STATUS) {
dev_crit(sma1303->dev,
"%s : OT2(Over Temperature Level 2)\n", __func__);
}
if (ocp_val & SMA1303_OCP_SPK_STATUS) {
dev_crit(sma1303->dev,
"%s : OCP_SPK(Over Current Protect SPK)\n", __func__);
}
if (ocp_val & SMA1303_OCP_BST_STATUS) {
dev_crit(sma1303->dev,
"%s : OCP_BST(Over Current Protect Boost)\n", __func__);
}
if ((ocp_val & SMA1303_CLK_MON_STATUS) && (sma1303->amp_power_status)) {
dev_crit(sma1303->dev,
"%s : CLK_FAULT(No clock input)\n", __func__);
}
if (uvlo_val & SMA1303_UVLO_BST_STATUS) {
dev_crit(sma1303->dev,
"%s : UVLO(Under Voltage Lock Out)\n", __func__);
}
if ((over_temp != sma1303->last_over_temp) ||
(ocp_val != sma1303->last_ocp_val)) {
dev_crit(sma1303->dev, "Please check AMP status");
dev_dbg(sma1303->dev, "STATUS1=0x%02X : STATUS2=0x%02X\n",
over_temp, ocp_val);
sma1303->last_over_temp = over_temp;
sma1303->last_ocp_val = ocp_val;
}
if (sma1303->check_fault_status) {
if (sma1303->check_fault_period > 0)
queue_delayed_work(system_freezable_wq,
&sma1303->check_fault_work,
sma1303->check_fault_period * HZ);
else
queue_delayed_work(system_freezable_wq,
&sma1303->check_fault_work,
CHECK_PERIOD_TIME * HZ);
}
if (!(~over_temp & SMA1303_OT1_OK_STATUS)
&& !(~over_temp & SMA1303_OT2_OK_STATUS)
&& !(ocp_val & SMA1303_OCP_SPK_STATUS)
&& !(ocp_val & SMA1303_OCP_BST_STATUS)
&& !(ocp_val & SMA1303_CLK_MON_STATUS)
&& !(uvlo_val & SMA1303_UVLO_BST_STATUS)) {
}
}
static int sma1303_probe(struct snd_soc_component *component)
{
struct snd_soc_dapm_context *dapm =
snd_soc_component_get_dapm(component);
snd_soc_dapm_sync(dapm);
return 0;
}
static void sma1303_remove(struct snd_soc_component *component)
{
struct sma1303_priv *sma1303 = snd_soc_component_get_drvdata(component);
cancel_delayed_work_sync(&sma1303->check_fault_work);
}
static const struct snd_soc_component_driver sma1303_component = {
.probe = sma1303_probe,
.remove = sma1303_remove,
.controls = sma1303_snd_controls,
.num_controls = ARRAY_SIZE(sma1303_snd_controls),
.dapm_widgets = sma1303_dapm_widgets,
.num_dapm_widgets = ARRAY_SIZE(sma1303_dapm_widgets),
.dapm_routes = sma1303_audio_map,
.num_dapm_routes = ARRAY_SIZE(sma1303_audio_map),
};
const struct regmap_config sma_i2c_regmap = {
.reg_bits = 8,
.val_bits = 8,
.max_register = SMA1303_FF_DEVICE_INDEX,
.readable_reg = sma1303_readable_register,
.writeable_reg = sma1303_writeable_register,
.volatile_reg = sma1303_volatile_register,
.cache_type = REGCACHE_NONE,
.reg_defaults = sma1303_reg_def,
.num_reg_defaults = ARRAY_SIZE(sma1303_reg_def),
};
static ssize_t check_fault_period_show(struct device *dev,
struct device_attribute *devattr, char *buf)
{
struct sma1303_priv *sma1303 = dev_get_drvdata(dev);
return sysfs_emit(buf, "%ld\n", sma1303->check_fault_period);
}
static ssize_t check_fault_period_store(struct device *dev,
struct device_attribute *devattr, const char *buf, size_t count)
{
struct sma1303_priv *sma1303 = dev_get_drvdata(dev);
int ret;
ret = kstrtol(buf, 10, &sma1303->check_fault_period);
if (ret)
return -EINVAL;
return (ssize_t)count;
}
static DEVICE_ATTR_RW(check_fault_period);
static ssize_t check_fault_status_show(struct device *dev,
struct device_attribute *devattr, char *buf)
{
struct sma1303_priv *sma1303 = dev_get_drvdata(dev);
return sysfs_emit(buf, "%ld\n", sma1303->check_fault_status);
}
static ssize_t check_fault_status_store(struct device *dev,
struct device_attribute *devattr, const char *buf, size_t count)
{
struct sma1303_priv *sma1303 = dev_get_drvdata(dev);
int ret;
ret = kstrtol(buf, 10, &sma1303->check_fault_status);
if (ret)
return -EINVAL;
if (sma1303->check_fault_status) {
if (sma1303->check_fault_period > 0)
queue_delayed_work(system_freezable_wq,
&sma1303->check_fault_work,
sma1303->check_fault_period * HZ);
else
queue_delayed_work(system_freezable_wq,
&sma1303->check_fault_work,
CHECK_PERIOD_TIME * HZ);
}
return (ssize_t)count;
}
static DEVICE_ATTR_RW(check_fault_status);
static struct attribute *sma1303_attr[] = {
&dev_attr_check_fault_period.attr,
&dev_attr_check_fault_status.attr,
NULL,
};
static struct attribute_group sma1303_attr_group = {
.attrs = sma1303_attr,
};
static int sma1303_i2c_probe(struct i2c_client *client)
{
struct sma1303_priv *sma1303;
struct device_node *np = client->dev.of_node;
int ret, i = 0;
u32 value = 0;
unsigned int device_info, status, otp_stat;
sma1303 = devm_kzalloc(&client->dev,
sizeof(struct sma1303_priv), GFP_KERNEL);
if (!sma1303)
return -ENOMEM;
sma1303->dev = &client->dev;
sma1303->regmap = devm_regmap_init_i2c(client, &sma_i2c_regmap);
if (IS_ERR(sma1303->regmap)) {
ret = PTR_ERR(sma1303->regmap);
dev_err(&client->dev,
"Failed to allocate register map: %d\n", ret);
return ret;
}
if (np) {
if (!of_property_read_u32(np, "tdm-slot-rx", &value)) {
dev_dbg(&client->dev,
"tdm slot rx is '%d' from DT\n", value);
sma1303->tdm_slot_rx = value;
} else {
dev_dbg(&client->dev,
"Default setting of tdm slot rx is '0'\n");
sma1303->tdm_slot_rx = 0;
}
if (!of_property_read_u32(np, "tdm-slot-tx", &value)) {
dev_dbg(&client->dev,
"tdm slot tx is '%u' from DT\n", value);
sma1303->tdm_slot_tx = value;
} else {
dev_dbg(&client->dev,
"Default setting of tdm slot tx is '0'\n");
sma1303->tdm_slot_tx = 0;
}
if (!of_property_read_u32(np, "sys-clk-id", &value)) {
switch (value) {
case SMA1303_EXTERNAL_CLOCK_19_2:
case SMA1303_EXTERNAL_CLOCK_24_576:
case SMA1303_PLL_CLKIN_MCLK:
dev_dbg(&client->dev, "MCLK is not supported\n");
break;
case SMA1303_PLL_CLKIN_BCLK:
dev_dbg(&client->dev,
"Take an BCLK(SCK) and covert it to an internal PLL for use\n");
break;
default:
dev_err(&client->dev,
"Invalid sys-clk-id: %u\n", value);
return -EINVAL;
}
sma1303->sys_clk_id = value;
} else {
dev_dbg(&client->dev, "Use the internal PLL clock by default\n");
sma1303->sys_clk_id = SMA1303_PLL_CLKIN_BCLK;
}
} else {
dev_err(&client->dev,
"device node initialization error\n");
devm_kfree(&client->dev, sma1303);
return -ENODEV;
}
ret = sma1303_regmap_read(sma1303,
SMA1303_FF_DEVICE_INDEX, &device_info);
if ((ret != 0) || ((device_info & 0xF8) != SMA1303_DEVICE_ID)) {
dev_err(&client->dev, "device initialization error (%d 0x%02X)",
ret, device_info);
}
dev_dbg(&client->dev, "chip version 0x%02X\n", device_info);
ret += sma1303_regmap_update_bits(sma1303,
SMA1303_00_SYSTEM_CTRL,
SMA1303_RESETBYI2C_MASK, SMA1303_RESETBYI2C_RESET,
NULL);
ret += sma1303_regmap_read(sma1303, SMA1303_FF_DEVICE_INDEX, &status);
sma1303->rev_num = status & SMA1303_REV_NUM_STATUS;
if (sma1303->rev_num == SMA1303_REV_NUM_TV0)
dev_dbg(&client->dev, "SMA1303 Trimming Version 0\n");
else if (sma1303->rev_num == SMA1303_REV_NUM_TV1)
dev_dbg(&client->dev, "SMA1303 Trimming Version 1\n");
ret += sma1303_regmap_read(sma1303, SMA1303_FB_STATUS2, &otp_stat);
if (ret < 0)
dev_err(&client->dev,
"failed to read, register: %02X, ret: %d\n",
SMA1303_FF_DEVICE_INDEX, ret);
if (((sma1303->rev_num == SMA1303_REV_NUM_TV0) &&
((otp_stat & 0x0E) == SMA1303_OTP_STAT_OK_0)) ||
((sma1303->rev_num != SMA1303_REV_NUM_TV0) &&
((otp_stat & 0x0C) == SMA1303_OTP_STAT_OK_1)))
dev_dbg(&client->dev, "SMA1303 OTP Status Successful\n");
else
dev_dbg(&client->dev, "SMA1303 OTP Status Fail\n");
for (i = 0; i < (unsigned int)ARRAY_SIZE(sma1303_reg_def); i++)
ret += sma1303_regmap_write(sma1303,
sma1303_reg_def[i].reg,
sma1303_reg_def[i].def);
sma1303->amp_mode = SMA1303_MONO;
sma1303->amp_power_status = false;
sma1303->check_fault_period = CHECK_PERIOD_TIME;
sma1303->check_fault_status = true;
sma1303->force_mute_status = false;
sma1303->init_vol = 0x31;
sma1303->cur_vol = sma1303->init_vol;
sma1303->last_bclk = 0;
sma1303->last_ocp_val = 0x08;
sma1303->last_over_temp = 0xC0;
sma1303->tsdw_cnt = 0;
sma1303->retry_cnt = SMA1303_I2C_RETRY_COUNT;
sma1303->dev = &client->dev;
sma1303->kobj = &client->dev.kobj;
INIT_DELAYED_WORK(&sma1303->check_fault_work,
sma1303_check_fault_worker);
i2c_set_clientdata(client, sma1303);
sma1303->pll_matches = sma1303_pll_matches;
sma1303->num_of_pll_matches =
ARRAY_SIZE(sma1303_pll_matches);
ret = devm_snd_soc_register_component(&client->dev,
&sma1303_component, sma1303_dai, 1);
if (ret) {
dev_err(&client->dev, "Failed to register component");
return ret;
}
sma1303->attr_grp = &sma1303_attr_group;
ret = sysfs_create_group(sma1303->kobj, sma1303->attr_grp);
if (ret) {
dev_err(&client->dev,
"failed to create attribute group [%d]\n", ret);
sma1303->attr_grp = NULL;
}
return ret;
}
static void sma1303_i2c_remove(struct i2c_client *client)
{
struct sma1303_priv *sma1303 =
(struct sma1303_priv *) i2c_get_clientdata(client);
cancel_delayed_work_sync(&sma1303->check_fault_work);
}
static const struct i2c_device_id sma1303_i2c_id[] = {
{"sma1303", 0},
{}
};
MODULE_DEVICE_TABLE(i2c, sma1303_i2c_id);
static const struct of_device_id sma1303_of_match[] = {
{ .compatible = "irondevice,sma1303", },
{ }
};
MODULE_DEVICE_TABLE(of, sma1303_of_match);
static struct i2c_driver sma1303_i2c_driver = {
.driver = {
.name = "sma1303",
.of_match_table = sma1303_of_match,
},
.probe_new = sma1303_i2c_probe,
.remove = sma1303_i2c_remove,
.id_table = sma1303_i2c_id,
};
module_i2c_driver(sma1303_i2c_driver);
MODULE_DESCRIPTION("ALSA SoC SMA1303 driver");
MODULE_AUTHOR("Gyuhwa Park, <gyuhwa.park@irondevice.com>");
MODULE_AUTHOR("Kiseok Jo, <kiseok.jo@irondevice.com>");
MODULE_LICENSE("GPL v2");
|