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
|
// SPDX-License-Identifier: GPL-2.0-only
//
// rt1318.c -- RT1318 ALSA SoC audio amplifier driver
// Author: Jack Yu <jack.yu@realtek.com>
//
// Copyright(c) 2024 Realtek Semiconductor Corp.
//
//
#include <linux/acpi.h>
#include <linux/fs.h>
#include <linux/module.h>
#include <linux/moduleparam.h>
#include <linux/init.h>
#include <linux/delay.h>
#include <linux/pm.h>
#include <linux/regmap.h>
#include <linux/i2c.h>
#include <linux/platform_device.h>
#include <linux/firmware.h>
#include <linux/gpio.h>
#include <sound/core.h>
#include <sound/pcm.h>
#include <sound/pcm_params.h>
#include <sound/soc.h>
#include <sound/soc-dapm.h>
#include <sound/initval.h>
#include <sound/tlv.h>
#include <sound/rt1318.h>
#include "rt1318.h"
static const struct reg_sequence init_list[] = {
{ 0x0000C000, 0x01},
{ 0x0000F20D, 0x00},
{ 0x0000F212, 0x3E},
{ 0x0000C001, 0x02},
{ 0x0000C003, 0x22},
{ 0x0000C004, 0x44},
{ 0x0000C005, 0x44},
{ 0x0000C007, 0x64},
{ 0x0000C00E, 0xE7},
{ 0x0000F223, 0x7F},
{ 0x0000F224, 0xDB},
{ 0x0000F225, 0xEE},
{ 0x0000F226, 0x3F},
{ 0x0000F227, 0x0F},
{ 0x0000F21A, 0x78},
{ 0x0000F242, 0x3C},
{ 0x0000C120, 0x40},
{ 0x0000C125, 0x03},
{ 0x0000C321, 0x0A},
{ 0x0000C200, 0xD8},
{ 0x0000C201, 0x27},
{ 0x0000C202, 0x0F},
{ 0x0000C400, 0x0E},
{ 0x0000C401, 0x43},
{ 0x0000C402, 0xE0},
{ 0x0000C403, 0x00},
{ 0x0000C404, 0x4C},
{ 0x0000C406, 0x40},
{ 0x0000C407, 0x02},
{ 0x0000C408, 0x3F},
{ 0x0000C300, 0x01},
{ 0x0000C125, 0x03},
{ 0x0000DF00, 0x10},
{ 0x0000F20B, 0x2A},
{ 0x0000DF5F, 0x01},
{ 0x0000DF60, 0xA7},
{ 0x0000C203, 0x84},
{ 0x0000C206, 0x78},
{ 0x0000F10A, 0x09},
{ 0x0000F10B, 0x4C},
{ 0x0000F104, 0xF4},
{ 0x0000F105, 0x03},
{ 0x0000F109, 0xE0},
{ 0x0000F10B, 0x5C},
{ 0x0000F104, 0xF4},
{ 0x0000F105, 0x04},
{ 0x0000F109, 0x65},
{ 0x0000F10B, 0x5C},
{ 0x0000F104, 0xF4},
{ 0x0000F105, 0x02},
{ 0x0000F109, 0x30},
{ 0x0000F10B, 0x5C},
{ 0x0000E706, 0x0F},
{ 0x0000E707, 0x30},
{ 0x0000E806, 0x0F},
{ 0x0000E807, 0x30},
{ 0x0000CE04, 0x03},
{ 0x0000CE05, 0x5F},
{ 0x0000CE06, 0xA2},
{ 0x0000CE07, 0x6B},
{ 0x0000CF04, 0x03},
{ 0x0000CF05, 0x5F},
{ 0x0000CF06, 0xA2},
{ 0x0000CF07, 0x6B},
{ 0x0000CE60, 0xE3},
{ 0x0000C130, 0x51},
{ 0x0000E000, 0xA8},
{ 0x0000F102, 0x00},
{ 0x0000F103, 0x00},
{ 0x0000F104, 0xF5},
{ 0x0000F105, 0x23},
{ 0x0000F109, 0x04},
{ 0x0000F10A, 0x0B},
{ 0x0000F10B, 0x4C},
{ 0x0000F10B, 0x5C},
{ 0x41001888, 0x00},
{ 0x0000C121, 0x0B},
{ 0x0000F102, 0x00},
{ 0x0000F103, 0x00},
{ 0x0000F104, 0xF5},
{ 0x0000F105, 0x23},
{ 0x0000F109, 0x00},
{ 0x0000F10A, 0x0B},
{ 0x0000F10B, 0x4C},
{ 0x0000F10B, 0x5C},
{ 0x0000F800, 0x20},
{ 0x0000CA00, 0x80},
{ 0x0000CA10, 0x00},
{ 0x0000CA02, 0x78},
{ 0x0000CA12, 0x78},
{ 0x0000ED00, 0x90},
{ 0x0000E604, 0x00},
{ 0x0000DB00, 0x0C},
{ 0x0000DD00, 0x0C},
{ 0x0000DC19, 0x00},
{ 0x0000DC1A, 0x6A},
{ 0x0000DC1B, 0xAA},
{ 0x0000DC1C, 0xAB},
{ 0x0000DC1D, 0x00},
{ 0x0000DC1E, 0x16},
{ 0x0000DC1F, 0xDB},
{ 0x0000DC20, 0x6D},
{ 0x0000DE19, 0x00},
{ 0x0000DE1A, 0x6A},
{ 0x0000DE1B, 0xAA},
{ 0x0000DE1C, 0xAB},
{ 0x0000DE1D, 0x00},
{ 0x0000DE1E, 0x16},
{ 0x0000DE1F, 0xDB},
{ 0x0000DE20, 0x6D},
{ 0x0000DB32, 0x00},
{ 0x0000DD32, 0x00},
{ 0x0000DB33, 0x0A},
{ 0x0000DD33, 0x0A},
{ 0x0000DB34, 0x1A},
{ 0x0000DD34, 0x1A},
{ 0x0000DB15, 0xEF},
{ 0x0000DD15, 0xEF},
{ 0x0000DB17, 0xEF},
{ 0x0000DD17, 0xEF},
{ 0x0000DB94, 0x70},
{ 0x0000DD94, 0x70},
{ 0x0000DB19, 0x40},
{ 0x0000DD19, 0x40},
{ 0x0000DB12, 0xC0},
{ 0x0000DD12, 0xC0},
{ 0x0000DB00, 0x4C},
{ 0x0000DB04, 0x05},
{ 0x0000DB05, 0x03},
{ 0x0000DD04, 0x05},
{ 0x0000DD05, 0x03},
{ 0x0000DBBB, 0x09},
{ 0x0000DBBC, 0x30},
{ 0x0000DBBD, 0xF0},
{ 0x0000DBBE, 0xF1},
{ 0x0000DDBB, 0x09},
{ 0x0000DDBC, 0x30},
{ 0x0000DDBD, 0xF0},
{ 0x0000DDBE, 0xF1},
{ 0x0000DB01, 0x79},
{ 0x0000DD01, 0x79},
{ 0x0000DB08, 0x40},
{ 0x0000DD08, 0x40},
{ 0x0000DC52, 0xEF},
{ 0x0000DE52, 0xEF},
{ 0x0000DB00, 0xCC},
{ 0x0000CC2C, 0x00},
{ 0x0000CC2D, 0x2A},
{ 0x0000CC2E, 0x83},
{ 0x0000CC2F, 0xA8},
{ 0x0000CD2C, 0x00},
{ 0x0000CD2D, 0x2A},
{ 0x0000CD2E, 0x83},
{ 0x0000CD2F, 0xA8},
{ 0x0000CC24, 0x00},
{ 0x0000CC25, 0x51},
{ 0x0000CC26, 0xEB},
{ 0x0000CC27, 0x85},
{ 0x0000CD24, 0x00},
{ 0x0000CD25, 0x51},
{ 0x0000CD26, 0xEB},
{ 0x0000CD27, 0x85},
{ 0x0000CC20, 0x00},
{ 0x0000CC21, 0x00},
{ 0x0000CC22, 0x43},
{ 0x0000CD20, 0x00},
{ 0x0000CD21, 0x00},
{ 0x0000CD22, 0x43},
{ 0x0000CC16, 0x0F},
{ 0x0000CC17, 0x00},
{ 0x0000CD16, 0x0F},
{ 0x0000CD17, 0x00},
{ 0x0000CC29, 0x5D},
{ 0x0000CC2A, 0xC0},
{ 0x0000CD29, 0x5D},
{ 0x0000CD2A, 0xC0},
{ 0x0000CC31, 0x20},
{ 0x0000CC32, 0x00},
{ 0x0000CC33, 0x00},
{ 0x0000CC34, 0x00},
{ 0x0000CD31, 0x20},
{ 0x0000CD32, 0x00},
{ 0x0000CD33, 0x00},
{ 0x0000CD34, 0x00},
{ 0x0000CC36, 0x79},
{ 0x0000CC37, 0x99},
{ 0x0000CC38, 0x99},
{ 0x0000CC39, 0x99},
{ 0x0000CD36, 0x79},
{ 0x0000CD37, 0x99},
{ 0x0000CD38, 0x99},
{ 0x0000CD39, 0x99},
{ 0x0000CC09, 0x00},
{ 0x0000CC0A, 0x07},
{ 0x0000CC0B, 0x5F},
{ 0x0000CC0C, 0x6F},
{ 0x0000CD09, 0x00},
{ 0x0000CD0A, 0x07},
{ 0x0000CD0B, 0x5F},
{ 0x0000CD0C, 0x6F},
{ 0x0000CC0E, 0x00},
{ 0x0000CC0F, 0x03},
{ 0x0000CC10, 0xAF},
{ 0x0000CC11, 0xB7},
{ 0x0000CD0E, 0x00},
{ 0x0000CD0F, 0x03},
{ 0x0000CD10, 0xAF},
{ 0x0000CD11, 0xB7},
{ 0x0000CCD6, 0x00},
{ 0x0000CCD7, 0x03},
{ 0x0000CDD6, 0x00},
{ 0x0000CDD7, 0x03},
{ 0x0000CCD8, 0x00},
{ 0x0000CCD9, 0x03},
{ 0x0000CDD8, 0x00},
{ 0x0000CDD9, 0x03},
{ 0x0000CCDA, 0x00},
{ 0x0000CCDB, 0x03},
{ 0x0000CDDA, 0x00},
{ 0x0000CDDB, 0x03},
{ 0x0000C320, 0x20},
{ 0x0000C203, 0x9C},
};
static const struct reg_default rt1318_reg[] = {
{ 0xc000, 0x00 },
{ 0xc001, 0x43 },
{ 0xc003, 0x22 },
{ 0xc004, 0x44 },
{ 0xc005, 0x44 },
{ 0xc006, 0x33 },
{ 0xc007, 0x64 },
{ 0xc008, 0x05 },
{ 0xc00a, 0xfc },
{ 0xc00b, 0x0f },
{ 0xc00c, 0x0e },
{ 0xc00d, 0xef },
{ 0xc00e, 0xe5 },
{ 0xc00f, 0xff },
{ 0xc120, 0xc0 },
{ 0xc121, 0x00 },
{ 0xc122, 0x00 },
{ 0xc123, 0x14 },
{ 0xc125, 0x00 },
{ 0xc130, 0x59 },
{ 0xc200, 0x00 },
{ 0xc201, 0x00 },
{ 0xc202, 0x00 },
{ 0xc203, 0x04 },
{ 0xc204, 0x00 },
{ 0xc205, 0x00 },
{ 0xc206, 0x68 },
{ 0xc207, 0x70 },
{ 0xc208, 0x00 },
{ 0xc20a, 0x00 },
{ 0xc20b, 0x01 },
{ 0xc20c, 0x7f },
{ 0xc20d, 0x01 },
{ 0xc20e, 0x7f },
{ 0xc300, 0x00 },
{ 0xc301, 0x00 },
{ 0xc303, 0x80 },
{ 0xc320, 0x00 },
{ 0xc321, 0x09 },
{ 0xc322, 0x02 },
{ 0xc400, 0x00 },
{ 0xc401, 0x00 },
{ 0xc402, 0x00 },
{ 0xc403, 0x00 },
{ 0xc404, 0x00 },
{ 0xc405, 0x00 },
{ 0xc406, 0x00 },
{ 0xc407, 0x00 },
{ 0xc408, 0x00 },
{ 0xc410, 0x04 },
{ 0xc430, 0x00 },
{ 0xc431, 0x00 },
{ 0xca00, 0x10 },
{ 0xca01, 0x00 },
{ 0xca02, 0x0b },
{ 0xca10, 0x10 },
{ 0xca11, 0x00 },
{ 0xca12, 0x0b },
{ 0xce04, 0x08 },
{ 0xce05, 0x00 },
{ 0xce06, 0x00 },
{ 0xce07, 0x00 },
{ 0xce60, 0x63 },
{ 0xcf04, 0x08 },
{ 0xcf05, 0x00 },
{ 0xcf06, 0x00 },
{ 0xcf07, 0x00 },
{ 0xdb00, 0x00 },
{ 0xdb08, 0x40 },
{ 0xdb12, 0x00 },
{ 0xdb35, 0x00 },
{ 0xdbb5, 0x00 },
{ 0xdbb6, 0x40 },
{ 0xdbb7, 0x00 },
{ 0xdbb8, 0x00 },
{ 0xdbc5, 0x00 },
{ 0xdbc6, 0x00 },
{ 0xdbc7, 0x00 },
{ 0xdbc8, 0x00 },
{ 0xdd08, 0x40 },
{ 0xdd12, 0x00 },
{ 0xdd35, 0x00 },
{ 0xddb5, 0x00 },
{ 0xddb6, 0x40 },
{ 0xddb7, 0x00 },
{ 0xddb8, 0x00 },
{ 0xddc5, 0x00 },
{ 0xddc6, 0x00 },
{ 0xddc7, 0x00 },
{ 0xddc8, 0x00 },
{ 0xdd93, 0x00 },
{ 0xdd94, 0x64 },
{ 0xdf00, 0x00 },
{ 0xdf5f, 0x00 },
{ 0xdf60, 0x00 },
{ 0xe000, 0x08 },
{ 0xe300, 0xa0 },
{ 0xe400, 0x22 },
{ 0xe706, 0x2f },
{ 0xe707, 0x2f },
{ 0xe806, 0x2f },
{ 0xe807, 0x2f },
{ 0xea00, 0x43 },
{ 0xed00, 0x80 },
{ 0xed01, 0x0f },
{ 0xed02, 0xff },
{ 0xed03, 0x00 },
{ 0xed04, 0x00 },
{ 0xed05, 0x0f },
{ 0xed06, 0xff },
{ 0xf010, 0x10 },
{ 0xf011, 0xec },
{ 0xf012, 0x68 },
{ 0xf013, 0x21 },
{ 0xf102, 0x00 },
{ 0xf103, 0x00 },
{ 0xf104, 0x00 },
{ 0xf105, 0x00 },
{ 0xf106, 0x00 },
{ 0xf107, 0x00 },
{ 0xf108, 0x00 },
{ 0xf109, 0x00 },
{ 0xf10a, 0x03 },
{ 0xf10b, 0x40 },
{ 0xf20b, 0x28 },
{ 0xf20d, 0x00 },
{ 0xf212, 0x00 },
{ 0xf21a, 0x00 },
{ 0xf223, 0x40 },
{ 0xf224, 0x00 },
{ 0xf225, 0x00 },
{ 0xf226, 0x00 },
{ 0xf227, 0x00 },
{ 0xf242, 0x0c },
{ 0xf800, 0x00 },
{ 0xf801, 0x12 },
{ 0xf802, 0xe0 },
{ 0xf803, 0x2f },
{ 0xf804, 0x00 },
{ 0xf805, 0x00 },
{ 0xf806, 0x07 },
{ 0xf807, 0xff },
};
static bool rt1318_volatile_register(struct device *dev, unsigned int reg)
{
switch (reg) {
case 0xc000:
case 0xc301:
case 0xc410:
case 0xc430 ... 0xc431:
case 0xdb06:
case 0xdb12:
case 0xdb1d ... 0xdb1f:
case 0xdb35:
case 0xdb37:
case 0xdb8a ... 0xdb92:
case 0xdbc5 ... 0xdbc8:
case 0xdc2b ... 0xdc49:
case 0xdd0b:
case 0xdd12:
case 0xdd1d ... 0xdd1f:
case 0xdd35:
case 0xdd8a ... 0xdd92:
case 0xddc5 ... 0xddc8:
case 0xde2b ... 0xde44:
case 0xdf4a ... 0xdf55:
case 0xe224 ... 0xe23b:
case 0xea01:
case 0xebc5:
case 0xebc8:
case 0xebcb ... 0xebcc:
case 0xed03 ... 0xed06:
case 0xf010 ... 0xf014:
return true;
default:
return false;
}
}
static bool rt1318_readable_register(struct device *dev, unsigned int reg)
{
switch (reg) {
case 0xc000 ... 0xc00f:
case 0xc120 ... 0xc130:
case 0xc200 ... 0xc20e:
case 0xc300 ... 0xc303:
case 0xc320 ... 0xc322:
case 0xc400 ... 0xc408:
case 0xc430 ... 0xc431:
case 0xca00 ... 0xca02:
case 0xca10 ... 0xca12:
case 0xcb00 ... 0xcb0b:
case 0xcc00 ... 0xcce5:
case 0xcd00 ... 0xcde5:
case 0xce00 ... 0xce6a:
case 0xcf00 ... 0xcf53:
case 0xd000 ... 0xd0cc:
case 0xd100 ... 0xd1b9:
case 0xdb00 ... 0xdc53:
case 0xdd00 ... 0xde53:
case 0xdf00 ... 0xdf6b:
case 0xe000:
case 0xe300:
case 0xe400:
case 0xe706 ... 0xe707:
case 0xe806 ... 0xe807:
case 0xea00:
case 0xeb00 ... 0xebcc:
case 0xec00 ... 0xecb9:
case 0xed00 ... 0xed06:
case 0xf010 ... 0xf014:
case 0xf102 ... 0xf10b:
case 0xf20b:
case 0xf20d ... 0xf242:
case 0xf800 ... 0xf807:
return true;
default:
return false;
}
}
static int rt1318_dac_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 rt1318_priv *rt1318 = snd_soc_component_get_drvdata(component);
switch (event) {
case SND_SOC_DAPM_PRE_PMU:
regmap_update_bits(rt1318->regmap, RT1318_PWR_STA1,
RT1318_PDB_CTRL_MASK, RT1318_PDB_CTRL_HIGH);
break;
case SND_SOC_DAPM_POST_PMD:
regmap_update_bits(rt1318->regmap, RT1318_PWR_STA1,
RT1318_PDB_CTRL_MASK, RT1318_PDB_CTRL_LOW);
break;
default:
break;
}
return 0;
}
static int rt1318_dvol_put(struct snd_kcontrol *kcontrol,
struct snd_ctl_elem_value *ucontrol)
{
struct snd_soc_component *component = snd_soc_kcontrol_component(kcontrol);
struct rt1318_priv *rt1318 = snd_soc_component_get_drvdata(component);
rt1318->rt1318_dvol = ucontrol->value.integer.value[0];
if (rt1318->rt1318_dvol <= RT1318_DVOL_STEP && rt1318->rt1318_dvol >= 0) {
regmap_write(rt1318->regmap, RT1318_DA_VOL_L_8,
rt1318->rt1318_dvol >> 8);
regmap_write(rt1318->regmap, RT1318_DA_VOL_L_1_7,
rt1318->rt1318_dvol & 0xff);
regmap_write(rt1318->regmap, RT1318_DA_VOL_R_8,
rt1318->rt1318_dvol >> 8);
regmap_write(rt1318->regmap, RT1318_DA_VOL_R_1_7,
rt1318->rt1318_dvol & 0xff);
return 1;
}
return 0;
}
static int rt1318_dvol_get(struct snd_kcontrol *kcontrol,
struct snd_ctl_elem_value *ucontrol)
{
struct snd_soc_component *component = snd_soc_kcontrol_component(kcontrol);
struct rt1318_priv *rt1318 = snd_soc_component_get_drvdata(component);
ucontrol->value.integer.value[0] = rt1318->rt1318_dvol;
return 0;
}
static const struct snd_kcontrol_new rt1318_snd_controls[] = {
SOC_SINGLE_EXT("Amp Playback Volume", SND_SOC_NOPM, 0, 383, 0,
rt1318_dvol_get, rt1318_dvol_put),
};
static const struct snd_soc_dapm_widget rt1318_dapm_widgets[] = {
/* Audio Interface */
SND_SOC_DAPM_AIF_IN("AIF1RX", "AIF1 Playback", 0, SND_SOC_NOPM, 0, 0),
/* DACs */
SND_SOC_DAPM_DAC_E("DAC", NULL, SND_SOC_NOPM, 0, 0,
rt1318_dac_event, SND_SOC_DAPM_PRE_PMU |
SND_SOC_DAPM_POST_PMD),
/* Output Lines */
SND_SOC_DAPM_OUTPUT("Amp"),
};
static const struct snd_soc_dapm_route rt1318_dapm_routes[] = {
{"DAC", NULL, "AIF1RX"},
{"Amp", NULL, "DAC"},
};
static int rt1318_get_clk_info(int sclk, int rate)
{
int i, pd[] = {1, 2, 4, 8, 16, 24};
if (sclk <= 0 || rate <= 0)
return -EINVAL;
rate = rate << 8;
for (i = 0; i < ARRAY_SIZE(pd); i++)
if (sclk == rate * pd[i])
return i;
return -EINVAL;
}
static int rt1318_clk_ip_info(struct snd_soc_component *component, int lrclk)
{
struct rt1318_priv *rt1318 = snd_soc_component_get_drvdata(component);
switch (lrclk) {
case RT1318_LRCLK_48000:
case RT1318_LRCLK_44100:
case RT1318_LRCLK_16000:
regmap_update_bits(rt1318->regmap, RT1318_SRC_TCON,
RT1318_SRCIN_F12288_MASK | RT1318_SRCIN_DACLK_MASK,
RT1318_SRCIN_TCON4 | RT1318_DACLK_TCON4);
break;
case RT1318_LRCLK_96000:
regmap_update_bits(rt1318->regmap, RT1318_SRC_TCON,
RT1318_SRCIN_F12288_MASK | RT1318_SRCIN_DACLK_MASK,
RT1318_SRCIN_TCON4 | RT1318_DACLK_TCON2);
break;
case RT1318_LRCLK_192000:
regmap_update_bits(rt1318->regmap, RT1318_SRC_TCON,
RT1318_SRCIN_F12288_MASK | RT1318_SRCIN_DACLK_MASK,
RT1318_SRCIN_TCON4 | RT1318_DACLK_TCON1);
break;
default:
dev_err(component->dev, "Unsupported clock rate.\n");
return -EINVAL;
}
return 0;
}
static int rt1318_hw_params(struct snd_pcm_substream *substream,
struct snd_pcm_hw_params *params, struct snd_soc_dai *dai)
{
struct snd_soc_component *component = dai->component;
struct rt1318_priv *rt1318 = snd_soc_component_get_drvdata(component);
int data_len = 0, ch_len = 0;
int pre_div, ret;
rt1318->lrck = params_rate(params);
pre_div = rt1318_get_clk_info(rt1318->sysclk, rt1318->lrck);
if (pre_div < 0) {
dev_err(component->dev, "Unsupported clock setting\n");
return -EINVAL;
}
ret = rt1318_clk_ip_info(component, rt1318->lrck);
if (ret < 0) {
dev_err(component->dev, "Unsupported clock setting\n");
return -EINVAL;
}
switch (params_width(params)) {
case 16:
break;
case 20:
data_len = RT1318_I2S_DL_20;
ch_len = RT1318_I2S_DL_20;
break;
case 24:
data_len = RT1318_I2S_DL_24;
ch_len = RT1318_I2S_DL_24;
break;
case 32:
data_len = RT1318_I2S_DL_32;
ch_len = RT1318_I2S_DL_32;
break;
case 8:
data_len = RT1318_I2S_DL_8;
ch_len = RT1318_I2S_DL_8;
break;
default:
return -EINVAL;
}
regmap_update_bits(rt1318->regmap, RT1318_CLK2,
RT1318_DIV_AP_MASK | RT1318_DIV_DAMOD_MASK,
pre_div << RT1318_DIV_AP_SFT |
pre_div << RT1318_DIV_DAMOD_SFT);
regmap_update_bits(rt1318->regmap, RT1318_CLK3,
RT1318_AD_STO1_MASK | RT1318_AD_STO2_MASK,
pre_div << RT1318_AD_STO1_SFT |
pre_div << RT1318_AD_STO2_SFT);
regmap_update_bits(rt1318->regmap, RT1318_CLK4,
RT1318_AD_ANA_STO1_MASK | RT1318_AD_ANA_STO2_MASK,
pre_div << RT1318_AD_ANA_STO1_SFT |
pre_div << RT1318_AD_ANA_STO2_SFT);
regmap_update_bits(rt1318->regmap, RT1318_CLK5,
RT1318_DIV_FIFO_IN_MASK | RT1318_DIV_FIFO_OUT_MASK,
pre_div << RT1318_DIV_FIFO_IN_SFT |
pre_div << RT1318_DIV_FIFO_OUT_SFT);
regmap_update_bits(rt1318->regmap, RT1318_CLK6,
RT1318_DIV_NLMS_MASK | RT1318_DIV_AD_MONO_MASK |
RT1318_DIV_POST_G_MASK, pre_div << RT1318_DIV_NLMS_SFT |
pre_div << RT1318_DIV_AD_MONO_SFT |
pre_div << RT1318_DIV_POST_G_SFT);
regmap_update_bits(rt1318->regmap, RT1318_TDM_CTRL2,
RT1318_I2S_DL_MASK, data_len << RT1318_I2S_DL_SFT);
regmap_update_bits(rt1318->regmap, RT1318_TDM_CTRL3,
RT1318_I2S_TX_CHL_MASK | RT1318_I2S_RX_CHL_MASK,
ch_len << RT1318_I2S_TX_CHL_SFT |
ch_len << RT1318_I2S_RX_CHL_SFT);
return 0;
}
static int rt1318_set_dai_fmt(struct snd_soc_dai *dai, unsigned int fmt)
{
struct snd_soc_component *component = dai->component;
struct rt1318_priv *rt1318 = snd_soc_component_get_drvdata(component);
unsigned int reg_val = 0, reg_val2 = 0;
switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
case SND_SOC_DAIFMT_NB_NF:
break;
case SND_SOC_DAIFMT_IB_NF:
reg_val2 |= RT1318_TDM_BCLK_INV;
break;
default:
return -EINVAL;
}
switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
case SND_SOC_DAIFMT_I2S:
break;
case SND_SOC_DAIFMT_LEFT_J:
reg_val |= RT1318_FMT_LEFT_J;
break;
case SND_SOC_DAIFMT_DSP_A:
reg_val |= RT1318_FMT_PCM_A_R;
break;
case SND_SOC_DAIFMT_DSP_B:
reg_val |= RT1318_FMT_PCM_B_R;
break;
default:
return -EINVAL;
}
regmap_update_bits(rt1318->regmap, RT1318_TDM_CTRL1,
RT1318_I2S_FMT_MASK, reg_val);
regmap_update_bits(rt1318->regmap, RT1318_TDM_CTRL1,
RT1318_TDM_BCLK_MASK, reg_val2);
return 0;
}
static int rt1318_set_dai_sysclk(struct snd_soc_dai *dai,
int clk_id, unsigned int freq, int dir)
{
struct snd_soc_component *component = dai->component;
struct rt1318_priv *rt1318 = snd_soc_component_get_drvdata(component);
int reg_val = 0;
if (freq == rt1318->sysclk && clk_id == rt1318->sysclk_src)
return 0;
switch (clk_id) {
case RT1318_SCLK_S_BCLK:
reg_val |= RT1318_SYSCLK_BCLK;
break;
case RT1318_SCLK_S_SDW:
reg_val |= RT1318_SYSCLK_SDW;
break;
case RT1318_SCLK_S_PLL2F:
reg_val |= RT1318_SYSCLK_PLL2F;
break;
case RT1318_SCLK_S_PLL2B:
reg_val |= RT1318_SYSCLK_PLL2B;
break;
case RT1318_SCLK_S_MCLK:
reg_val |= RT1318_SYSCLK_MCLK;
break;
case RT1318_SCLK_S_RC0:
reg_val |= RT1318_SYSCLK_RC1;
break;
case RT1318_SCLK_S_RC1:
reg_val |= RT1318_SYSCLK_RC2;
break;
case RT1318_SCLK_S_RC2:
reg_val |= RT1318_SYSCLK_RC3;
break;
default:
dev_err(component->dev, "Invalid clock id (%d)\n", clk_id);
return -EINVAL;
}
rt1318->sysclk = freq;
rt1318->sysclk_src = clk_id;
dev_dbg(dai->dev, "Sysclk is %dHz and clock id is %d\n", freq, clk_id);
regmap_update_bits(rt1318->regmap, RT1318_CLK1,
RT1318_SYSCLK_SEL_MASK, reg_val);
return 0;
}
static const struct pll_calc_map pll_preset_table[] = {
{512000, 4096000, 22, 190, 0, true, false},
{1024000, 4096000, 22, 94, 0, true, false},
{1024000, 16384000, 4, 190, 0, true, false},
{1411200, 11289600, 6, 62, 0, true, false},
{1536000, 12288000, 6, 62, 0, true, false},
{2822400, 11289600, 6, 62, 0, true, false},
{2822400, 45158400, 0, 62, 0, true, false},
{2822400, 49152000, 0, 62, 0, true, false},
{3072000, 12288000, 6, 62, 0, true, false},
{3072000, 24576000, 2, 62, 0, true, false},
{3072000, 49152000, 0, 62, 0, true, false},
{6144000, 24576000, 2, 94, 4, false, false},
{6144000, 49152000, 0, 30, 0, true, false},
{6144000, 98304000, 0, 94, 4, false, true},
{12288000, 49152000, 0, 62, 6, false, false},
};
static int rt1318_pll_calc(const unsigned int freq_in,
const unsigned int freq_out, struct rt1318_pll_code *pll_code)
{
int max_n = RT1318_PLL_N_MAX, max_m = RT1318_PLL_M_MAX;
int i, k, red, n_t, pll_out, in_t, out_t;
int n = 0, m = 0, m_t = 0;
int red_t = abs(freq_out - freq_in);
bool m_bypass = false, k_bypass = false;
if (RT1318_PLL_INP_MAX < freq_in || RT1318_PLL_INP_MIN > freq_in)
return -EINVAL;
for (i = 0; i < ARRAY_SIZE(pll_preset_table); i++) {
if (freq_in == pll_preset_table[i].pll_in &&
freq_out == pll_preset_table[i].pll_out) {
k = pll_preset_table[i].k;
m = pll_preset_table[i].m;
n = pll_preset_table[i].n;
m_bypass = pll_preset_table[i].m_bp;
k_bypass = pll_preset_table[i].k_bp;
goto code_find;
}
}
k = 100000000 / freq_out - 2;
if (k > RT1318_PLL_K_MAX)
k = RT1318_PLL_K_MAX;
if (k < 0) {
k = 0;
k_bypass = true;
}
for (n_t = 0; n_t <= max_n; n_t++) {
in_t = freq_in / (k_bypass ? 1 : (k + 2));
pll_out = freq_out / (n_t + 2);
if (in_t < 0)
continue;
if (in_t == pll_out) {
m_bypass = true;
n = n_t;
goto code_find;
}
red = abs(in_t - pll_out);
if (red < red_t) {
m_bypass = true;
n = n_t;
m = m_t;
if (red == 0)
goto code_find;
red_t = red;
}
for (m_t = 0; m_t <= max_m; m_t++) {
out_t = in_t / (m_t + 2);
red = abs(out_t - pll_out);
if (red < red_t) {
m_bypass = false;
n = n_t;
m = m_t;
if (red == 0)
goto code_find;
red_t = red;
}
}
}
pr_debug("Only get approximation about PLL\n");
code_find:
pll_code->m_bp = m_bypass;
pll_code->k_bp = k_bypass;
pll_code->m_code = m;
pll_code->n_code = n;
pll_code->k_code = k;
return 0;
}
static int rt1318_set_dai_pll(struct snd_soc_dai *dai, int pll_id, int source,
unsigned int freq_in, unsigned int freq_out)
{
struct snd_soc_component *component = dai->component;
struct rt1318_priv *rt1318 = snd_soc_component_get_drvdata(component);
struct rt1318_pll_code pll_code;
int ret;
if (!freq_in || !freq_out) {
dev_dbg(component->dev, "PLL disabled\n");
rt1318->pll_in = 0;
rt1318->pll_out = 0;
return 0;
}
if (source == rt1318->pll_src && freq_in == rt1318->pll_in &&
freq_out == rt1318->pll_out)
return 0;
switch (source) {
case RT1318_PLL_S_BCLK0:
regmap_update_bits(rt1318->regmap, RT1318_CLK1,
RT1318_PLLIN_MASK, RT1318_PLLIN_BCLK0);
break;
case RT1318_PLL_S_BCLK1:
regmap_update_bits(rt1318->regmap, RT1318_CLK1,
RT1318_PLLIN_MASK, RT1318_PLLIN_BCLK1);
break;
case RT1318_PLL_S_RC:
regmap_update_bits(rt1318->regmap, RT1318_CLK1,
RT1318_PLLIN_MASK, RT1318_PLLIN_RC);
break;
case RT1318_PLL_S_MCLK:
regmap_update_bits(rt1318->regmap, RT1318_CLK1,
RT1318_PLLIN_MASK, RT1318_PLLIN_MCLK);
break;
case RT1318_PLL_S_SDW_IN_PLL:
regmap_update_bits(rt1318->regmap, RT1318_CLK1,
RT1318_PLLIN_MASK, RT1318_PLLIN_SDW1);
break;
case RT1318_PLL_S_SDW_0:
regmap_update_bits(rt1318->regmap, RT1318_CLK1,
RT1318_PLLIN_MASK, RT1318_PLLIN_SDW2);
break;
case RT1318_PLL_S_SDW_1:
regmap_update_bits(rt1318->regmap, RT1318_CLK1,
RT1318_PLLIN_MASK, RT1318_PLLIN_SDW3);
break;
case RT1318_PLL_S_SDW_2:
regmap_update_bits(rt1318->regmap, RT1318_CLK1,
RT1318_PLLIN_MASK, RT1318_PLLIN_SDW4);
break;
default:
dev_err(component->dev, "Unknown PLL source %d\n", source);
return -EINVAL;
}
ret = rt1318_pll_calc(freq_in, freq_out, &pll_code);
if (ret < 0) {
dev_err(component->dev, "Unsupport input clock %d\n", freq_in);
return ret;
}
dev_dbg(component->dev, "bypass=%d m=%d n=%d k=%d\n",
pll_code.m_bp, (pll_code.m_bp ? 0 : pll_code.m_code),
pll_code.n_code, pll_code.k_code);
regmap_update_bits(rt1318->regmap, RT1318_PLL1_K,
RT1318_K_PLL1_MASK, pll_code.k_code);
regmap_update_bits(rt1318->regmap, RT1318_PLL1_M,
RT1318_M_PLL1_MASK, (pll_code.m_bp ? 0 : pll_code.m_code));
regmap_update_bits(rt1318->regmap, RT1318_PLL1_N_8,
RT1318_N_8_PLL1_MASK, pll_code.n_code >> 8);
regmap_update_bits(rt1318->regmap, RT1318_PLL1_N_7_0,
RT1318_N_7_0_PLL1_MASK, pll_code.n_code);
rt1318->pll_in = freq_in;
rt1318->pll_out = freq_out;
rt1318->pll_src = source;
return 0;
}
static int rt1318_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 rt1318_priv *rt1318 = snd_soc_component_get_drvdata(component);
unsigned int cn = 0, cl = 0, rx_slotnum;
int ret = 0, first_bit;
switch (slots) {
case 4:
cn |= RT1318_I2S_CH_TX_4CH;
cn |= RT1318_I2S_CH_RX_4CH;
break;
case 6:
cn |= RT1318_I2S_CH_TX_6CH;
cn |= RT1318_I2S_CH_RX_6CH;
break;
case 8:
cn |= RT1318_I2S_CH_TX_8CH;
cn |= RT1318_I2S_CH_RX_8CH;
break;
case 2:
break;
default:
return -EINVAL;
}
switch (slot_width) {
case 20:
cl |= RT1318_I2S_TX_CHL_20;
cl |= RT1318_I2S_RX_CHL_20;
break;
case 24:
cl |= RT1318_I2S_TX_CHL_24;
cl |= RT1318_I2S_RX_CHL_24;
break;
case 32:
cl |= RT1318_I2S_TX_CHL_32;
cl |= RT1318_I2S_RX_CHL_32;
break;
case 8:
cl |= RT1318_I2S_TX_CHL_8;
cl |= RT1318_I2S_RX_CHL_8;
break;
case 16:
break;
default:
return -EINVAL;
}
/* Rx slot configuration */
rx_slotnum = hweight_long(rx_mask);
if (rx_slotnum != 1) {
ret = -EINVAL;
dev_err(component->dev, "too many rx slots or zero slot\n");
goto _set_tdm_err_;
}
first_bit = __ffs(rx_mask);
switch (first_bit) {
case 0:
case 2:
case 4:
case 6:
regmap_update_bits(rt1318->regmap,
RT1318_TDM_CTRL9,
RT1318_TDM_I2S_TX_L_DAC1_1_MASK |
RT1318_TDM_I2S_TX_R_DAC1_1_MASK,
(first_bit << RT1318_TDM_I2S_TX_L_DAC1_1_SFT) |
((first_bit + 1) << RT1318_TDM_I2S_TX_R_DAC1_1_SFT));
break;
case 1:
case 3:
case 5:
case 7:
regmap_update_bits(rt1318->regmap,
RT1318_TDM_CTRL9,
RT1318_TDM_I2S_TX_L_DAC1_1_MASK |
RT1318_TDM_I2S_TX_R_DAC1_1_MASK,
((first_bit - 1) << RT1318_TDM_I2S_TX_L_DAC1_1_SFT) |
(first_bit << RT1318_TDM_I2S_TX_R_DAC1_1_SFT));
break;
default:
ret = -EINVAL;
goto _set_tdm_err_;
}
regmap_update_bits(rt1318->regmap, RT1318_TDM_CTRL2,
RT1318_I2S_CH_TX_MASK | RT1318_I2S_CH_RX_MASK, cn);
regmap_update_bits(rt1318->regmap, RT1318_TDM_CTRL3,
RT1318_I2S_TX_CHL_MASK | RT1318_I2S_RX_CHL_MASK, cl);
_set_tdm_err_:
return ret;
}
static int rt1318_probe(struct snd_soc_component *component)
{
struct rt1318_priv *rt1318 = snd_soc_component_get_drvdata(component);
rt1318->component = component;
schedule_work(&rt1318->cali_work);
rt1318->rt1318_dvol = RT1318_DVOL_STEP;
return 0;
}
static void rt1318_remove(struct snd_soc_component *component)
{
struct rt1318_priv *rt1318 = snd_soc_component_get_drvdata(component);
cancel_work_sync(&rt1318->cali_work);
}
#ifdef CONFIG_PM
static int rt1318_suspend(struct snd_soc_component *component)
{
struct rt1318_priv *rt1318 = snd_soc_component_get_drvdata(component);
regcache_cache_only(rt1318->regmap, true);
regcache_mark_dirty(rt1318->regmap);
return 0;
}
static int rt1318_resume(struct snd_soc_component *component)
{
struct rt1318_priv *rt1318 = snd_soc_component_get_drvdata(component);
regcache_cache_only(rt1318->regmap, false);
regcache_sync(rt1318->regmap);
return 0;
}
#else
#define rt1318_suspend NULL
#define rt1318_resume NULL
#endif
#define RT1318_STEREO_RATES SNDRV_PCM_RATE_8000_192000
#define RT1318_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE | \
SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S8)
static const struct snd_soc_dai_ops rt1318_aif_dai_ops = {
.hw_params = rt1318_hw_params,
.set_fmt = rt1318_set_dai_fmt,
.set_sysclk = rt1318_set_dai_sysclk,
.set_pll = rt1318_set_dai_pll,
.set_tdm_slot = rt1318_set_tdm_slot,
};
static struct snd_soc_dai_driver rt1318_dai[] = {
{
.name = "rt1318-aif",
.id = 0,
.playback = {
.stream_name = "AIF1 Playback",
.channels_min = 1,
.channels_max = 2,
.rates = RT1318_STEREO_RATES,
.formats = RT1318_FORMATS,
},
.ops = &rt1318_aif_dai_ops,
}
};
static const struct snd_soc_component_driver soc_component_dev_rt1318 = {
.probe = rt1318_probe,
.remove = rt1318_remove,
.suspend = rt1318_suspend,
.resume = rt1318_resume,
.controls = rt1318_snd_controls,
.num_controls = ARRAY_SIZE(rt1318_snd_controls),
.dapm_widgets = rt1318_dapm_widgets,
.num_dapm_widgets = ARRAY_SIZE(rt1318_dapm_widgets),
.dapm_routes = rt1318_dapm_routes,
.num_dapm_routes = ARRAY_SIZE(rt1318_dapm_routes),
.use_pmdown_time = 1,
.endianness = 1,
};
static const struct regmap_config rt1318_regmap = {
.reg_bits = 32,
.val_bits = 8,
.readable_reg = rt1318_readable_register,
.volatile_reg = rt1318_volatile_register,
.max_register = 0x41001888,
.reg_defaults = rt1318_reg,
.num_reg_defaults = ARRAY_SIZE(rt1318_reg),
.cache_type = REGCACHE_RBTREE,
.use_single_read = true,
.use_single_write = true,
};
static const struct i2c_device_id rt1318_i2c_id[] = {
{ "rt1318" },
{ }
};
MODULE_DEVICE_TABLE(i2c, rt1318_i2c_id);
static const struct of_device_id rt1318_of_match[] = {
{ .compatible = "realtek,rt1318", },
{ }
};
MODULE_DEVICE_TABLE(of, rt1318_of_match);
#ifdef CONFIG_ACPI
static const struct acpi_device_id rt1318_acpi_match[] = {
{ "10EC1318" },
{ }
};
MODULE_DEVICE_TABLE(acpi, rt1318_acpi_match);
#endif
static int rt1318_parse_dt(struct rt1318_priv *rt1318, struct device *dev)
{
device_property_read_u32(dev, "realtek,r0_l",
&rt1318->pdata.init_r0_l);
device_property_read_u32(dev, "realtek,r0_r",
&rt1318->pdata.init_r0_r);
return 0;
}
static void rt1318_calibration_sequence(struct rt1318_priv *rt1318)
{
regmap_write(rt1318->regmap, RT1318_CLK1, 0x22);
regmap_write(rt1318->regmap, RT1318_PLL1_N_7_0, 0x06);
regmap_write(rt1318->regmap, RT1318_STP_TEMP_L, 0xCC);
regmap_write(rt1318->regmap, RT1318_STP_SEL_L, 0x40);
regmap_write(rt1318->regmap, RT1318_STP_SEL_R, 0x40);
regmap_write(rt1318->regmap, RT1318_SINE_GEN0, 0x20);
regmap_write(rt1318->regmap, RT1318_SPK_VOL_TH, 0x00);
regmap_write(rt1318->regmap, RT1318_FEEDBACK_PATH, 0x0B);
regmap_write(rt1318->regmap, RT1318_TCON, 0x1C);
regmap_write(rt1318->regmap, RT1318_TCON_RELATE, 0x58);
regmap_write(rt1318->regmap, RT1318_TCON_RELATE, 0x78);
regmap_write(rt1318->regmap, RT1318_STP_R0_EN_L, 0xC2);
}
static void rt1318_r0_calculate(struct rt1318_priv *rt1318)
{
unsigned int r0_l, r0_l_byte0, r0_l_byte1, r0_l_byte2, r0_l_byte3;
unsigned int r0_r, r0_r_byte0, r0_r_byte1, r0_r_byte2, r0_r_byte3;
unsigned int r0_l_integer, r0_l_factor, r0_r_integer, r0_r_factor;
unsigned int format = 16777216; /* 2^24 */
regmap_read(rt1318->regmap, RT1318_R0_L_24, &r0_l_byte0);
regmap_read(rt1318->regmap, RT1318_R0_L_23_16, &r0_l_byte1);
regmap_read(rt1318->regmap, RT1318_R0_L_15_8, &r0_l_byte2);
regmap_read(rt1318->regmap, RT1318_R0_L_7_0, &r0_l_byte3);
r0_l = r0_l_byte0 << 24 | r0_l_byte1 << 16 | r0_l_byte2 << 8 | r0_l_byte3;
r0_l_integer = format / r0_l;
r0_l_factor = (format * 10) / r0_l - r0_l_integer * 10;
regmap_read(rt1318->regmap, RT1318_R0_R_24, &r0_r_byte0);
regmap_read(rt1318->regmap, RT1318_R0_R_23_16, &r0_r_byte1);
regmap_read(rt1318->regmap, RT1318_R0_R_15_8, &r0_r_byte2);
regmap_read(rt1318->regmap, RT1318_R0_R_7_0, &r0_r_byte3);
r0_r = r0_r_byte0 << 24 | r0_r_byte1 << 16 | r0_r_byte2 << 8 | r0_r_byte3;
r0_r_integer = format / r0_r;
r0_r_factor = (format * 10) / r0_r - r0_r_integer * 10;
dev_dbg(rt1318->component->dev, "r0_l_ch:%d.%d ohm\n", r0_l_integer, r0_l_factor);
dev_dbg(rt1318->component->dev, "r0_r_ch:%d.%d ohm\n", r0_r_integer, r0_r_factor);
}
static void rt1318_r0_restore(struct rt1318_priv *rt1318)
{
regmap_write(rt1318->regmap, RT1318_PRE_R0_L_24,
(rt1318->pdata.init_r0_l >> 24) & 0xff);
regmap_write(rt1318->regmap, RT1318_PRE_R0_L_23_16,
(rt1318->pdata.init_r0_l >> 16) & 0xff);
regmap_write(rt1318->regmap, RT1318_PRE_R0_L_15_8,
(rt1318->pdata.init_r0_l >> 8) & 0xff);
regmap_write(rt1318->regmap, RT1318_PRE_R0_L_7_0,
(rt1318->pdata.init_r0_l >> 0) & 0xff);
regmap_write(rt1318->regmap, RT1318_PRE_R0_R_24,
(rt1318->pdata.init_r0_r >> 24) & 0xff);
regmap_write(rt1318->regmap, RT1318_PRE_R0_R_23_16,
(rt1318->pdata.init_r0_r >> 16) & 0xff);
regmap_write(rt1318->regmap, RT1318_PRE_R0_R_15_8,
(rt1318->pdata.init_r0_r >> 8) & 0xff);
regmap_write(rt1318->regmap, RT1318_PRE_R0_R_7_0,
(rt1318->pdata.init_r0_r >> 0) & 0xff);
regmap_write(rt1318->regmap, RT1318_STP_SEL_L, 0x80);
regmap_write(rt1318->regmap, RT1318_STP_SEL_R, 0x80);
regmap_write(rt1318->regmap, RT1318_R0_CMP_L_FLAG, 0xc0);
regmap_write(rt1318->regmap, RT1318_R0_CMP_R_FLAG, 0xc0);
regmap_write(rt1318->regmap, RT1318_STP_R0_EN_L, 0xc0);
regmap_write(rt1318->regmap, RT1318_STP_R0_EN_R, 0xc0);
regmap_write(rt1318->regmap, RT1318_STP_TEMP_L, 0xcc);
regmap_write(rt1318->regmap, RT1318_TCON, 0x9c);
}
static int rt1318_calibrate(struct rt1318_priv *rt1318)
{
int chk_cnt = 30, count = 0;
int val, val2;
regmap_write(rt1318->regmap, RT1318_PWR_STA1, 0x1);
usleep_range(0, 10000);
rt1318_calibration_sequence(rt1318);
while (count < chk_cnt) {
msleep(100);
regmap_read(rt1318->regmap, RT1318_R0_CMP_L_FLAG, &val);
regmap_read(rt1318->regmap, RT1318_R0_CMP_R_FLAG, &val2);
val = (val >> 1) & 0x1;
val2 = (val2 >> 1) & 0x1;
if (val & val2) {
dev_dbg(rt1318->component->dev, "Calibration done.\n");
break;
}
count++;
if (count == chk_cnt) {
regmap_write(rt1318->regmap, RT1318_PWR_STA1, 0x0);
return RT1318_R0_CALIB_NOT_DONE;
}
}
regmap_write(rt1318->regmap, RT1318_PWR_STA1, 0x0);
regmap_read(rt1318->regmap, RT1318_R0_CMP_L_FLAG, &val);
regmap_read(rt1318->regmap, RT1318_R0_CMP_R_FLAG, &val2);
if ((val & 0x1) & (val2 & 0x1))
return RT1318_R0_IN_RANGE;
else
return RT1318_R0_OUT_OF_RANGE;
}
static void rt1318_calibration_work(struct work_struct *work)
{
struct rt1318_priv *rt1318 =
container_of(work, struct rt1318_priv, cali_work);
int ret;
if (rt1318->pdata.init_r0_l && rt1318->pdata.init_r0_r)
rt1318_r0_restore(rt1318);
else {
ret = rt1318_calibrate(rt1318);
if (ret == RT1318_R0_IN_RANGE)
rt1318_r0_calculate(rt1318);
dev_dbg(rt1318->component->dev, "Calibrate R0 result:%d\n", ret);
}
}
static int rt1318_i2c_probe(struct i2c_client *i2c)
{
struct rt1318_platform_data *pdata = dev_get_platdata(&i2c->dev);
struct rt1318_priv *rt1318;
int ret, val, val2, dev_id;
rt1318 = devm_kzalloc(&i2c->dev, sizeof(struct rt1318_priv),
GFP_KERNEL);
if (!rt1318)
return -ENOMEM;
i2c_set_clientdata(i2c, rt1318);
if (pdata)
rt1318->pdata = *pdata;
else
rt1318_parse_dt(rt1318, &i2c->dev);
rt1318->regmap = devm_regmap_init_i2c(i2c, &rt1318_regmap);
if (IS_ERR(rt1318->regmap)) {
ret = PTR_ERR(rt1318->regmap);
dev_err(&i2c->dev, "Failed to allocate register map: %d\n",
ret);
return ret;
}
regmap_read(rt1318->regmap, RT1318_DEV_ID1, &val);
regmap_read(rt1318->regmap, RT1318_DEV_ID2, &val2);
dev_id = (val << 8) | val2;
if (dev_id != 0x6821) {
dev_err(&i2c->dev,
"Device with ID register %#x is not rt1318\n",
dev_id);
return -ENODEV;
}
ret = regmap_register_patch(rt1318->regmap, init_list,
ARRAY_SIZE(init_list));
if (ret != 0)
dev_warn(&i2c->dev, "Failed to apply regmap patch: %d\n", ret);
INIT_WORK(&rt1318->cali_work, rt1318_calibration_work);
return devm_snd_soc_register_component(&i2c->dev,
&soc_component_dev_rt1318, rt1318_dai, ARRAY_SIZE(rt1318_dai));
}
static struct i2c_driver rt1318_i2c_driver = {
.driver = {
.name = "rt1318",
.of_match_table = of_match_ptr(rt1318_of_match),
.acpi_match_table = ACPI_PTR(rt1318_acpi_match),
},
.probe = rt1318_i2c_probe,
.id_table = rt1318_i2c_id,
};
module_i2c_driver(rt1318_i2c_driver);
MODULE_DESCRIPTION("ASoC RT1318 driver");
MODULE_AUTHOR("Jack Yu <jack.yu@realtek.com>");
MODULE_LICENSE("GPL");
|