MT6188A1_drv.c 129 KB
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 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585
/*****************************************************************************
*  Copyright Statement:
*  --------------------
*  This software is protected by Copyright and the information contained
*  herein is confidential. The software may not be copied and the information
*  contained herein may not be used or disclosed except with the written
*  permission of MediaTek Inc. (C) 2005
*
*  BY OPENING THIS FILE, BUYER HEREBY UNEQUIVOCALLY ACKNOWLEDGES AND AGREES
*  THAT THE SOFTWARE/FIRMWARE AND ITS DOCUMENTATIONS ("MEDIATEK SOFTWARE")
*  RECEIVED FROM MEDIATEK AND/OR ITS REPRESENTATIVES ARE PROVIDED TO BUYER ON
*  AN "AS-IS" BASIS ONLY. MEDIATEK EXPRESSLY DISCLAIMS ANY AND ALL WARRANTIES,
*  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF
*  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR NONINFRINGEMENT.
*  NEITHER DOES MEDIATEK PROVIDE ANY WARRANTY WHATSOEVER WITH RESPECT TO THE
*  SOFTWARE OF ANY THIRD PARTY WHICH MAY BE USED BY, INCORPORATED IN, OR
*  SUPPLIED WITH THE MEDIATEK SOFTWARE, AND BUYER AGREES TO LOOK ONLY TO SUCH
*  THIRD PARTY FOR ANY WARRANTY CLAIM RELATING THERETO. MEDIATEK SHALL ALSO
*  NOT BE RESPONSIBLE FOR ANY MEDIATEK SOFTWARE RELEASES MADE TO BUYER'S
*  SPECIFICATION OR TO CONFORM TO A PARTICULAR STANDARD OR OPEN FORUM.
*
*  BUYER'S SOLE AND EXCLUSIVE REMEDY AND MEDIATEK'S ENTIRE AND CUMULATIVE
*  LIABILITY WITH RESPECT TO THE MEDIATEK SOFTWARE RELEASED HEREUNDER WILL BE,
*  AT MEDIATEK'S OPTION, TO REVISE OR REPLACE THE MEDIATEK SOFTWARE AT ISSUE,
*  OR REFUND ANY SOFTWARE LICENSE FEES OR SERVICE CHARGE PAID BY BUYER TO
*  MEDIATEK FOR SUCH MEDIATEK SOFTWARE AT ISSUE. 
*
*  THE TRANSACTION CONTEMPLATED HEREUNDER SHALL BE CONSTRUED IN ACCORDANCE
*  WITH THE LAWS OF THE STATE OF CALIFORNIA, USA, EXCLUDING ITS CONFLICT OF
*  LAWS PRINCIPLES.  ANY DISPUTES, CONTROVERSIES OR CLAIMS ARISING THEREOF AND
*  RELATED THERETO SHALL BE SETTLED BY ARBITRATION IN SAN FRANCISCO, CA, UNDER
*  THE RULES OF THE INTERNATIONAL CHAMBER OF COMMERCE (ICC).
*
*****************************************************************************/

/*******************************************************************************
 
 *******************************************************************************/
//#include "l1audio_def.h"
#include "kal_general_types.h"
#include "kal_public_api.h"

#if (defined(MT6188A1))



//#define force_mono
#define USE_I2C
/// #define MT6188_DEBUG
//#define MT6188_DEBUG_DUMP_LOG       //Option log file
//#define MT6188_timing_cal
//#define __MT6188CR_DISABLE_SOFTMUTE__

#if defined MT6188_DEBUG
static kal_uint8 dbg_cw_readback = 0;
#endif

#if (defined(MT6205B) || defined(MT6208)) && defined(MT6188_DEBUG_DUMP_LOG)
#error "No file system on MT6205!"
#endif

/* MT6188 Control Word */
#define CW(_A,_B)  (const)(kal_uint8)((_A << 1) + _B)

/* General propose constant */
#define MT6188                96    /// Device ID of MT6188 = 96
#define OP_WRITE              0     /// Write to MT6188
#define OP_READ               1     /// Read from MT6188
#define MT6188_WRITE          192
#define MT6188_READ           193
#define SET_FREQ_FAIL         -1
#define LO_INJECTION          0
#define HI_INJECTION          1
#define RSSI_CHECK_FAIL       2
#define FM_TUNER_GRID         100
#define PRESET_IF             280   /// Preset IF: 280 khz
#define IF_MEASURE_COUNT      5     /// 3 * 1.9 ms = 5.7 ms
#define VCO_LOCKING_TIME      4     /// 4 * 4.615 ms
#define CALIBRATION_FAIL_COUNT_THRESHOLD 5

#if !defined( MT6205B ) && !defined( MT6208 )
#define REF_CLK_32K
#define PRESET_IF_CNT         547   //280000*64/32768 = 546.875274   
#define PRESET_IF_DELTA_O     50//52
#define PRESET_IF_OFFSET      15	//offset 8Khz
#define IF_CONVERT_FACTOR     512   /// 32768 / 64 = 512
#else
#define USE_MT6188_AMP
#define REF_CLK_26M
#define PRESET_IF_CNT         529   //280000 * 49152 / 26000000 = 529.33 //265   
#define PRESET_IF_DELTA_O     50
#define PRESET_IF_OFFSET      15	//offset 8Khz
#define IF_CONVERT_FACTOR     529   /// 26000000 / 49152 = 529
#endif


#ifdef USE_MT6188_AMP
void ExtSwitchInit(void);
void SwitchExtFMPath(kal_uint8 if_on);
#endif

/* Serial communication interfaces */
void  SerialCommInit(void);
void  SerialCommRelease(void);
void  SerialCommCryClkOn(void);
void  SerialCommCryClkOff(void);
void  GPIO_WriteIO(char data,char port);

#if defined USE_I2C
void  SerialCommStart(void);
void  SerialCommStop(void);
kal_uint8 SerialCommTxByte(kal_uint8 data);
void  SerialCommRxByte(kal_uint8 *data, kal_uint8 ack);
#elif defined USE_3_WIRE
kal_uint8 SerialCommTxByte(kal_uint8 cw, kal_uint8 data);
void  SerialCommRxByte(kal_uint8 cw, kal_uint8 *data);
#endif

typedef struct {
   kal_uint8 addr;
   kal_uint8 value;
} ctrl_word_setting;

typedef struct {
   kal_uint8 addr;
   kal_uint8 and;
   kal_uint8 or;
} ctrl_word_operation;

extern kal_uint8 const FM_RADIO_INPUT_LEVEL_THRESHOLD;
void FMDrv_Mute(kal_uint8 mute);
void FMDrv_EvaluateRSSIThreshold(void);

static kal_uint8 cw_cache[74] = {0};

static kal_int32 freq_IF_array[2][3]={{0,0,0},{0,0,0}};//for auto search
static kal_int8 index_count=0; 
static kal_int8 max_count=2;

/// Global variables for current FM status
static kal_int16 _current_frequency = -1;
static kal_uint8 _current_level = 0x80;
static kal_bool  _is_fm_on = KAL_FALSE;
static kal_bool  _is_fm_mute = KAL_FALSE;
static kal_uint8 _rssi_threshold;
static kal_uint8 _valid_rssi = 0;
static kal_uint16 PRESET_IF_CNT_PLUS;		            //add to adjust IF rounding
static kal_uint8 PRESET_IF_DELTA = PRESET_IF_DELTA_O;
static kal_uint8 PRESET_IF_DELTA_check = 52;
static kal_uint8 RSSI_offset = 0;                       //RSSI_offset;
static kal_uint8 _rssi_threshold = 0;                   //FM_RADIO_INPUT_LEVEL_THRESHOLD for engineer mode;
static kal_uint8 mono_flag = 0;
static kal_uint8 Chip_ID = 0;


#ifdef MT6188_DEBUG_DUMP_LOG
extern kal_uint32 video_get_current_time(void);
extern kal_uint32 video_get_duration_ms(kal_uint32 t1);
static kal_uint8 _file_name[] = {"D\0:\0\\\0f\0m\0_\0l\0o\0g\0.\0t\0x\0t\0\0\0"};
static kal_uint32 _data_written;
static kal_uint8  _dbg_str[128];
static FS_HANDLE  _file_handle = 0;

static kal_uint8 _channel_name[] = {"D\0:\0\\\0l\0i\0s\0_\0l\0o\0g\0.\0t\0x\0t\0\0\0"};
static kal_uint32 _list_written;
static kal_uint8  _list_str[128];
static FS_HANDLE  _file_list = 0;
#endif

static void MT6188_Mute(kal_uint8 mute);
void FMDrv_EvaluateRSSIThreshold(void);

// enginner mode struct
typedef enum group_idx {
	   mono=0,
	   stereo,
	   RSSI_threshold,
	   IF_count_delta,
	   GROUP_TOTAL_NUMS
	} FACTORY_GROUP_ENUM;
	
typedef enum item_idx {
	   Sblend_OFF=0,
	   Sblend_ON,  
	   ITEM_TOTAL_NUMS
	} FACTORY_ITEM_INDEX;
// enginner mode struct

#define POWER_ON_COMMAND_COUNT_V5 62
static const ctrl_word_setting PowerOnSetting_V5[POWER_ON_COMMAND_COUNT_V5] = {
   { CW(   1, 1),    5 },
   { CW(   2, 1),   64 },{ CW(   2, 0),    4 },
   { CW(   3, 1),   28 },{ CW(   3, 0),   49 },
   { CW(   4, 1),  128 },{ CW(   4, 0),  175 },//cw(4,0)199
   { CW(   5, 1),  228 },{ CW(   5, 0),   47 },//47 AGC_off, by joetsou  //cw(5,0)175
   { CW(   7, 1),    0 },{ CW(   7, 0),  119 },
   { CW(   8, 1),  134 },{ CW(   8, 0),   89 }, 
   { CW(   9, 1),  152 },{ CW(   9, 0),   18 },
   { CW(  10, 1),   8 },{ CW(  10, 0),    3 },
   { CW(  11, 1),  129 },{ CW(  11, 0),    5 },
   { CW(  12, 1),    4 },{ CW(  12, 0),    3 },//cw(13, 0)136
   { CW(  13, 1),   32 },{ CW(  13, 0),  137 },//cw(14,0)254,110
   { CW(  14, 1),  255 },{ CW(  14, 0),  255 },
   { CW(  15, 1),  192 },{ CW(  15, 0),    8 },//cw(17, 1)40
   { CW(  16, 1),    8 },{ CW(  16, 0),  104 },
   { CW(  17, 1),   76 },{ CW(  17, 0),   14 },//cw(18, 0)0
   { CW(  18, 1),    6 },{ CW(  18, 0),  138 },//cw(19, 0)16
   { CW(  19, 1),    3 },{ CW(  19, 0),   56 },
   { CW(  21, 1),  167 },{ CW(  21, 0),   68 },//69//CW21_0[0] 0	//diable auto AGC,check with joetsou
   { CW(  22, 1),  68 },{ CW(  22, 0),    71 },
   { CW(  23, 1),   76 },{ CW(  23, 0),  233 },//cw(23, 0)239/233
   { CW(  24, 1),    3 },{ CW(  24, 0),    0 },
   { CW(  30, 1),  136 },{ CW(  30, 0),  136 },
   { CW(  31, 1),    0 },{ CW(  31, 0),    0 },
   { CW(  32, 1),    0 },{ CW(  32, 0),    0 },
   { CW(  33, 1),    0 },{ CW(  33, 0),    0 },//cw(23, 0)239/233
   { CW(  34, 1),    0 },{ CW(  34, 0),    1 },
   { CW(  97, 1),   16 },{ CW(  97, 0),    0 },
   { CW(  98, 1),  128 },{ CW(  98, 0),    0 },
   { CW(  99, 1),  128 },{ CW(  99, 0),    0 },
   			 { CW( 101, 0),    4 },
   { CW( 103, 1),    4 },{ CW( 103, 0),   32 },
};

#define POWER_ON_COMMAND_COUNT_V4 52
static const ctrl_word_setting PowerOnSetting_V4[POWER_ON_COMMAND_COUNT_V4] = {
   { CW(  1, 1),  0 },	{ CW(  1, 0),  10 },
   { CW(  2, 1),  64 },	{ CW(  3, 1),  28 },
   { CW(  3, 0),  53 },	{ CW(  4, 1),  128 },
   { CW(  4, 0),  183 },{ CW(  5, 1),  100 },//cw(4,0)199
   { CW(  5, 0),  175 },{ CW(  7, 1),  0 }, //175, off_AGC, joetsou //cw(5,0)175
   { CW(  7, 0),  247 },{ CW(  8, 1),  134 },
   { CW(  8, 0),  89 }, { CW(  9, 1),  152 },
   { CW(  9, 0),  18 }, { CW(  10, 1), 0 }, 
   { CW(  10, 0), 3 },  { CW(  11, 1), 1 },
   { CW(  11, 0), 5 },	{ CW(  12, 1), 4 },
   { CW(  12, 0), 3 },	{ CW(  13, 1), 0 },
   { CW(  13, 0), 0 },{ CW(  14, 1), 255 },//cw(13, 0)136
   { CW(  14, 0), 246 },{ CW(  15, 1), 255 },//cw(14,0)254,110
   { CW(  15, 0), 252 },{ CW(  16, 1), 108 },
   { CW(  16, 0), 108 },{ CW(  17, 1), 72 },//cw(17, 1)40
   { CW(  17, 0), 204 },{ CW(  18, 1), 6 },
   { CW(  18, 0), 34 },	{ CW(  19, 1), 3 },//cw(18, 0)0
   { CW(  19, 0), 92 }, { CW(  21, 1), 0 },//cw(19, 0)16
   { CW(  21, 0), 26 }, { CW(  22, 1), 0 },
   { CW(  22, 0), 2 },  { CW(  23, 1), 3 },
   { CW(  23, 0), 228 },{ CW(  24, 1), 3 },//cw(23, 0)239/233
   { CW(  24, 0), 0 },	{ CW(  97, 1), 16 },
   { CW(  97, 0), 0 },	{ CW(  98, 1), 128 },
   { CW(  98, 0), 0 },	{ CW(  99, 1), 128 },
   { CW(  99, 0), 0 },	{ CW(  101,0), 4 },
   { CW(  103,1), 4 },	{ CW(  103,0), 32 },
};

#define POWER_ON_COMMAND_COUNT_V3 48
static const ctrl_word_setting PowerOnSetting_V3[POWER_ON_COMMAND_COUNT_V3] = {
   { CW(  1, 1),  0 },	{ CW(  1, 0),  10 },
   { CW(  2, 1),  64 },	{ CW(  3, 1),  28 },
   { CW(  3, 0),  53 },	{ CW(  4, 1),  128 },
   { CW(  4, 0),  199 },{ CW(  5, 1),  100 },
   { CW(  5, 0),  175 },{ CW(  7, 1),  0 },//175 off_AGC_joetsou  //cw(5,0)175
   { CW(  7, 0),  247 },{ CW(  8, 1),  134 },
   { CW(  8, 0),  89 }, { CW(  9, 1),  152 },
   { CW(  9, 0),  18 }, { CW(  10, 1), 0 }, 
   { CW(  10, 0), 3 },  { CW(  11, 1), 1 },
   { CW(  11, 0), 5 },	{ CW(  12, 1), 4 },
   { CW(  12, 0), 3 },	{ CW(  13, 1), 0 },
   { CW(  13, 0), 136 },{ CW(  14, 1), 255 },
   { CW(  14, 0), 110 },{ CW(  15, 1), 255 },//cw(14,0)254
   { CW(  15, 0), 252 },{ CW(  16, 1), 108 },
   { CW(  16, 0), 108 },{ CW(  17, 1), 40 },
   { CW(  17, 0), 204 },{ CW(  18, 1), 6 },
   { CW(  18, 0), 0 },	{ CW(  19, 1), 3 },
   { CW(  19, 0), 16 }, { CW(  23, 1), 3 },
   { CW(  23, 0), 239 },{ CW(  24, 1), 3 },
   { CW(  24, 0), 0 },	{ CW(  97, 1), 16 },
   { CW(  97, 0), 0 },	{ CW(  98, 1), 128 },
   { CW(  98, 0), 0 },	{ CW(  99, 1), 128 },
   { CW(  99, 0), 0 },	{ CW(  101,0), 4 },
   { CW(  103,1), 4 },	{ CW(  103,0), 32 },
}; 


#define POWER_OFF_COMMAND_COUNT 2
static const ctrl_word_operation PowerOffProc[POWER_OFF_COMMAND_COUNT] = {
   { CW(  1, 1), 0xBF, 0x40 },	//CW1_1		[6:6]	1
   { CW(  1, 1), 0x9F, 0x20 },	//CW1_1		[6:5]	1	
};

// start chip ID = 5
#define CAL_PRESET_COUNT_V5 28
static const ctrl_word_operation CalPresetProcess_V5[CAL_PRESET_COUNT_V5] = {
   { CW( 14, 0), 0x7F, 0x00 },	//CW14_0	[7:7]	0  	//S_AUTO,STEREO
   { CW( 14, 0), 0xEF, 0x10 },	//CW14_0	[4:4]	1
   { CW(  5, 1), 0xFB, 0x04 },	//CW5_1		[2:2]	1	//Window_L
   { CW(  3, 0), 0xF8, 0x05 },	//CW3_0		[2:0]	5	//AMP_P,S_DIV_3
   { CW(  3, 0), 0xFE, 0x01 },	//CW3_0		[0:0]	1	//S_DIV_256,S_DIV_128,S_DIV_3
   { CW(  3, 1), 0xE3, 0x1C },	//CW3_1		[4:2]	7	//ISEL
   { CW( 12, 0), 0xFE, 0x01 },	//CW12_0	[0:0]	1	//PLL19K_P
   { CW( 12, 0), 0xFD, 0x02 },	//CW12_0	[1:1]	1	//PLL_19K_P
   { CW(  7, 0), 0xE0, 0x17 },	//CW7_0		[4:0]	23	//VCO_PW,PRE_PW,LO_PW,VER
   { CW( 10, 1), 0xDF, 0x20 },	//CW10_1	[5:5]	1	//FRCKTEST
   { CW( 12, 1), 0xEF, 0x00 },	//CW12_1	[4:4]	0	//EN_TESTMODE_PLL19K
   { CW( 12, 1), 0x1F, 0x00 },	//CW12_1	[7:5]	0	//TEST_OUTSEL_19K[2:0]
   { CW(  6, 1), 0x0F, 0x10 },	//CW6_1		[7:4]	1	//TAR[3:0]
   { CW(  7, 1), 0xFC, 0x00 },	//CW7_1		[1:0]	0	//CAL[1:0]
   { CW( 13, 0), 0x00, 0x88 },	//CW13_0	[7:0]	136	//pilot
   { CW( 97, 1), 0xE0, 0x10 },	//CW97_1	[4:0]	16	//FRTUNE1[4:0]
   { CW( 97, 0), 0xE0, 0x00 },	//CW97_0	[4:0]	0	//FRTUNE2[4:0]
   { CW( 98, 1), 0x00, 0x80 },	//CW98_1	[7:0]	128	//DMTUNE1I[7:0]
   { CW( 98, 0), 0x00, 0x00 },	//CW98_0	[7:0]	0	//DMTUNE2I[3:0]
   { CW( 99, 1), 0x00, 0x80 },	//CW99_1	[7:0]	128	//DMTUNE1Q[7:0]
   { CW( 99, 0), 0xF0, 0x00 },	//CW99_0	[3:0]	0	//DMTUNE2Q[3:0]
   { CW(101, 0), 0xF8, 0x04 },	//CW101_0	[2:0]	4	//LPF[2:0]
   { CW(103, 1), 0x00, 0x04 },	//CW103_1	[7:0]	4	//FB_19K2[2:0]
   { CW(103, 0), 0x00, 0x20 },	//CW103_0	[7:0]	32	//IBAND_19K2[5:0]
#if defined   REF_CLK_32K //==============================================
   { CW(  3, 0), 0x1F, 0x20 }, 	//CW3_0		[7:5]  	1, 
#elif defined REF_CLK_13M
   { CW(  3, 0), 0x1F, 0x40 }, 	//CW3_0    	[7:5]  	2, 
#elif defined REF_CLK_26M
   { CW(  3, 0), 0x1F, 0xC0 }, 	//CW3_0    	[7:5]  	6, 
#endif //=================================================================
#if defined   REF_CLK_32K //==============================================
   { CW(  2, 1), 0xBF, 0x40 }, 	//CW2_1    	[6:6]  	1	//INSEL 
#else
   { CW(  2, 1), 0xBF, 0x00 }, 	//CW2_1    	[6:6]  	0	//INSEL
#endif   	
   { CW(  5, 1), 0x7F, 0x00 },	//CW5_1		[7:7]	0	//PRE_CH
   { CW(  8, 0), 0xF7, 0x08 },	//CW8_0		[3:3]	1	//AFC_CAL
};
// end chip ID = 5

#define CAL_PRESET_COUNT 27
static const ctrl_word_operation CalPresetProcess[CAL_PRESET_COUNT] = {
   { CW( 14, 0), 0x7F, 0x00 },	//CW14_0	[7:7]	0  	//S_AUTO,STEREO
   { CW( 14, 0), 0xEF, 0x10 },	//CW14_0	[4:4]	1
   { CW(  5, 1), 0xFB, 0x04 },	//CW5_1		[2:2]	1	//Window_L
   { CW(  3, 0), 0xF8, 0x05 },	//CW3_0		[2:0]	5	//AMP_P,S_DIV_3
   { CW(  3, 0), 0xFE, 0x01 },	//CW3_0		[0:0]	1	//S_DIV_256,S_DIV_128,S_DIV_3
   { CW(  3, 1), 0xE3, 0x1C },	//CW3_1		[4:2]	7	//ISEL
   { CW( 12, 0), 0xFE, 0x01 },	//CW12_0	[0:0]	1	//PLL19K_P
   { CW( 12, 0), 0xFD, 0x02 },	//CW12_0	[1:1]	1	//PLL_19K_P
   { CW(  7, 0), 0xE0, 0x17 },	//CW7_0		[4:0]	23	//VCO_PW,PRE_PW,LO_PW,VER
   { CW( 10, 1), 0xDF, 0x20 },	//CW10_1	[5:5]	1	//FRCKTEST
   { CW( 12, 1), 0xEF, 0x00 },	//CW12_1	[4:4]	0	//EN_TESTMODE_PLL19K
   { CW( 12, 1), 0x1F, 0x00 },	//CW12_1	[7:5]	0	//TEST_OUTSEL_19K[2:0]
   { CW(  6, 1), 0x0F, 0x10 },	//CW6_1		[7:4]	1	//TAR[3:0]
   { CW(  7, 1), 0xFC, 0x00 },	//CW7_1		[1:0]	0	//CAL[1:0]
   { CW( 97, 1), 0xE0, 0x10 },	//CW97_1	[4:0]	16	//FRTUNE1[4:0]
   { CW( 97, 0), 0xE0, 0x00 },	//CW97_0	[4:0]	0	//FRTUNE2[4:0]
   { CW( 98, 1), 0x00, 0x80 },	//CW98_1	[7:0]	128	//DMTUNE1I[7:0]
   { CW( 98, 0), 0x00, 0x00 },	//CW98_0	[7:0]	0	//DMTUNE2I[3:0]
   { CW( 99, 1), 0x00, 0x80 },	//CW99_1	[7:0]	128	//DMTUNE1Q[7:0]
   { CW( 99, 0), 0xF0, 0x00 },	//CW99_0	[3:0]	0	//DMTUNE2Q[3:0]
   { CW(101, 0), 0xF8, 0x04 },	//CW101_0	[2:0]	4	//LPF[2:0]
   { CW(103, 1), 0x00, 0x04 },	//CW103_1	[7:0]	4	//FB_19K2[2:0]
   { CW(103, 0), 0x00, 0x20 },	//CW103_0	[7:0]	32	//IBAND_19K2[5:0]
#if defined   REF_CLK_32K //==============================================
   { CW(  3, 0), 0x1F, 0x20 }, 	//CW3_0		[7:5]  	1, 
#elif defined REF_CLK_13M
   { CW(  3, 0), 0x1F, 0x40 }, 	//CW3_0    	[7:5]  	2, 
#elif defined REF_CLK_26M
   { CW(  3, 0), 0x1F, 0xC0 }, 	//CW3_0    	[7:5]  	6, 
#endif //=================================================================
#if defined   REF_CLK_32K //==============================================
   { CW(  2, 1), 0xBF, 0x40 }, 	//CW2_1    	[6:6]  	1	//INSEL 
#else
   { CW(  2, 1), 0xBF, 0x00 }, 	//CW2_1    	[6:6]  	0	//INSEL
#endif   	
   { CW(  5, 1), 0x7F, 0x00 },	//CW5_1		[7:7]	0	//PRE_CH
   { CW(  8, 0), 0xF7, 0x08 },	//CW8_0		[3:3]	1	//AFC_CAL
};

#define CAL_PLL_COMMAND_COUNT 21
static const ctrl_word_operation CalPLLProcess[CAL_PLL_COMMAND_COUNT] = {
   { CW( 12, 1), 0xFC, 0x03 }, 	//CW12_1   	[1:0]  	3	
   { CW(  6, 0), 0xFC, 0x01 }, 	//CW6_0    	[1:0]  	1	//WIN_S[1:0],set window source
   { CW(  7, 1), 0xFC, 0x03 }, 	//CW7_1    	[1:0]  	3	//CAL[1:0],Enable calibration
   { CW(  6, 1), 0x00, 0x66 }, 	//CW6_1    	[7:0]	102	//CNT_S, set clock source
   { CW( 25, 1), 0x00, 0x03 }, 	//CW25_1   	[7:0] 	3	//CNT_T[11:8], set target value
#if defined   REF_CLK_32K //==============================================
   { CW( 25, 0), 0x00, 0x7A }, 	//CW25_0   	[7:0] 	122	//CNT_T[7:0], set target value
#elif defined REF_CLK_13M
   { CW( 25, 0), 0x00, 0x5E }, 	//CW25_0   	[7:0]  	94
#elif defined REF_CLK_26M
   { CW( 25, 0), 0x00, 0x5E }, 	//CW25_0   	[7:0]  	94
#endif //=================================================================
   { CW(  1, 0), 0x00, 0x00 }, 	//CW1_0    	[7:0]   0	//Clear all Flag register
   { CW(  6, 1), 0xFE, 0x01 }, 	//CW6_1    	[0:0]   1	//WIN_EN, start calibration
   { CW(127, 1), 0x00, 0x01 }, 	// <----------------- polling SARIFG
   { CW(  1, 0), 0x00, 0x00 }, 	//CW1_0    	[7:0]   0	//reset CW1_0
   { CW(  6, 1), 0x00, 0x76 }, 	//CW6_1    	[7:0]   118	//CNT_S, set clock source
   { CW(  1, 0), 0x00, 0x00 }, 	//CW1_0    	[7:0]   0	//Clear all Flag register
   { CW(  6, 1), 0xFE, 0x01 }, 	//CW6_1    	[0:0]   1	//WIN_EN, start calibration
   { CW(127, 1), 0x00, 0x01 }, 	// <----------------- polling SARIFG
   { CW(  1, 0), 0x00, 0x00 }, 	//CW1_0    	[7:0]   0	//reset CW1_0
   { CW(  6, 1), 0x00, 0x56 }, 	//CW6_1    	[7:0]   86	//CAL, Enable calibration	
   { CW(  1, 0), 0x00, 0x00 }, 	//CW1_0    	[7:0]   0	//Clear all Flag register
   { CW(  6, 1), 0xFE, 0x01 }, 	//CW6_1    	[0:0]   1	//WIN_EN, start calibration
   { CW(127, 1), 0x00, 0x01 }, 	// <----------------- polling SARIFG
   { CW(  1, 0), 0x00, 0x00 }, 	//CW1_0    	[7:0]   0	//reset CW1_0
   { CW( 12, 1), 0xF8, 0x04 }, 	//CW12_1   	[2:0]   4	//Set Divided vaule=24 and disable calibration mode of PLL19K
};

#define CAL_DEMOD_I_COMMAND_COUNT 31
static const ctrl_word_operation CalDemodIProcess[CAL_DEMOD_I_COMMAND_COUNT] = {
   { CW(  8, 0), 0xFD, 0x02 }, 	//CW8_0   	[1:1]  1	//FM_CALI, Enalbe calibation mode of I  channel demodulator
   { CW(  8, 0), 0xFB, 0x04 }, 	//CW8_0   	[2:2]  1	//FM_CALQ, Enalbe calibation mode of Q channel demodulator
   { CW(  8, 0), 0xEF, 0x00 }, 	//CW8_0   	[4:4]  0	//DEM_RSB, Reset demodulator
   { CW(  8, 0), 0xEF, 0x10 }, 	//CW8_0   	[4:4]  1	//DEM_RSB, Reset demodulator
   { CW(  7, 1), 0xFC, 0x03 }, 	//CW7_1   	[1:0]  3	//CAL[1:0], Enable calibraton process
   { CW(  6, 0), 0xFC, 0x01 }, 	//CW6_0   	[1:0]  1	//WIN_S[1:0], Set window soure
   { CW(  6, 1), 0x00, 0xAA }, 	//CW6_1   	[7:0]  170	//CNTS[2:0],TAR[3:0], Set clock source and traget register
#if defined   REF_CLK_32K //==============================================
   { CW( 25, 0), 0x00, 0x45 }, 	//CW25_0  	[7:0]  69	//CNT_T[7:0]
#elif defined REF_CLK_13M
   { CW( 25, 0), 0x00, 0x23 }, 	//CW25_0  	[7:0]  35
#elif defined REF_CLK_26M
   { CW( 25, 0), 0x00, 0x23 }, 	//CW25_0  	[7:0]  35
#endif //=================================================================
   { CW( 25, 1), 0x00, 0x04 }, 	//CW25_1  	[7:0]  4+	//CNT_T[11:8]
   { CW(  1, 0), 0x00, 0x00 }, 	//CW1_0   	[7:0]  0	//Clear all FLG registers
   { CW(  6, 1), 0xFE, 0x01 }, 	//CW6_1   	[0:0]  1	//WIN_EN, Start calibration process
   { CW(127, 1), 0x00, 0x01 }, 	// <----------------- polling SARIFG
   { CW(  1, 0), 0x00, 0x00 }, 	//CW1_0   	[7:0]  0 	//reset CW1_0
   { CW(  6, 1), 0x00, 0xBA }, 	//CW6_1   	[7:0]  186	//CNTS[2:0],TAR[3:0],Set clock source and traget register   
   { CW(  1, 0), 0x00, 0x00 }, 	//CW1_0   	[7:0]  0,	//Clear all FLG registers
   { CW(  6, 1), 0xFE, 0x01 }, 	//CW6_1   	[0:0]  1	//WIN_EN, Start calibration process 
   { CW(127, 1), 0x00, 0x01 }, 	// <----------------- polling SARIFG
   { CW(  1, 0), 0x00, 0x00 }, 	//CW1_0   	[7:0]  0 	//reset CW1_0
   { CW(  6, 1), 0x00, 0xCC }, 	//CW6_1   	[7:0]  204	//CNTS[2:0],TAR[3:0],Set clock source and traget register   
#if defined   REF_CLK_32K //==============================================
   { CW( 25, 0), 0x00, 0xDA }, 	//CW25_0  	[7:0]  218	//CNT_T[7:0]
#elif defined REF_CLK_13M
   { CW( 25, 0), 0x00, 0xD4 }, 	//CW25_0  	[7:0]  212
#elif defined REF_CLK_26M
   { CW( 25, 0), 0x00, 0xD4 }, 	//CW25_0  	[7:0]  212
#endif //=================================================================
   { CW( 25, 1), 0x00, 0x00 }, 	//CW25_1  	[7:0]  0+	//CNT_T[11:8]
   { CW(  1, 0), 0x00, 0x00 }, 	//CW1_0   	[7:0]  0	//Clear all FLG registers
   { CW(  6, 1), 0xFE, 0x01 }, 	//CW6_1   	[0:0]  1	//WIN_EN, Start calibration process
   { CW(127, 1), 0x00, 0x01 }, 	// <----------------- polling SARIFG
   { CW(  1, 0), 0x00, 0x00 }, 	//CW1_0   	[7:0]  0 	//reset CW1_0
   { CW(  6, 1), 0x0F, 0xD0 }, 	//CW6_1   	[7:4]  13	//CNTS[2:0],TAR[3:0],Set clock source and traget register  
   { CW(  1, 0), 0x00, 0x00 }, 	//CW1_0   	[7:0]  0	//Clear all FLG registers
   { CW(  6, 1), 0xFE, 0x01 }, 	//CW6_1   	[0:0]  1	//WIN_EN,Start calibration process 
   { CW(127, 1), 0x00, 0x01 }, 	// <----------------- polling SARIFG
   { CW(  1, 0), 0x00, 0x00 }, 	//CW1_0   	[7:0]  0 	//reset CW1_0
   { CW(  8, 0), 0xF9, 0x00 }, 	//CW8_0   	[2:1]  0	//FM_CALI,Q, disable I, Q calibration mode
};

// start chip ID = 5
#define CHANNEL_FILTER_COUNT 13
static const ctrl_word_operation ChannelFilter[CHANNEL_FILTER_COUNT] = {
   { CW( 10, 1), 0xFE, 0x01 }, 	//CW10_0 	[0:0]   1	//Enable channel filter calibraton
   { CW(  7, 1), 0xFC, 0x03 }, 	//CW7_1 	[1:0]   3	//Enable calibraton process 
   { CW(  6, 0), 0xFC, 0x01 }, 	//CW6_0 	[1:0]   1	//Set window soure 
   { CW(  6, 1), 0x00, 0x28 }, 	//CW6_1 	[7:0]   40	//Set clock ource and target register  
   { CW( 25, 1), 0x00, 0x00 }, 	//CW25_1 	[7:0]   0	// 
   { CW( 25, 0), 0x00, 0x31 }, 	//CW25_0 	[7:0]   49	//(Please set this field as an user-defined value on the control program) 
   { CW(  1, 0), 0x00, 0x00 }, 	//CW1_0 	[7:0]   0	//Clear all FLG registers 
   { CW(  6, 1), 0xFE, 0x01 }, 	//CW6_1 	[0:0]   1	//Start calibration process 
   { CW(127, 1), 0x00, 0x01 }, 	// <----------------- polling SARIFG
   { CW(  1, 0), 0x00, 0x00 }, 	//CW1_0   	[7:0]  0 	//reset CW1_0
   { CW(  6, 1), 0x0F, 0x10 }, 	//CW6_1 	[7:4]   1	//Set clock source and target register 
   { CW(  7, 1), 0xFC, 0x00 }, 	//CW7_1 	[1:0]   0	//Disable calibration process 
   { CW( 10, 1), 0xFE, 0x00 }, 	//CW10_1 	[0:0]   0	//Disable channel filter calibraton  
};

#define MUXOFF_MN_COUNT 24
static const ctrl_word_operation MuxOff_MN[MUXOFF_MN_COUNT] = {
   { CW(  8, 0), 0xFE, 0x00 }, 	//CW8_0 	[0:0]   0	//Turn off Demod.
   { CW( 15, 0), 0xFD, 0x02 }, 	//CW15_0 	[1:1]   1	//Enable MUX calibraton (MUX_CON_CAL)
   { CW( 11, 1), 0x3F, 0x80 }, 	//CW11_1 	[7:6]   2	//Gain option (D2S_MODE, I2V_MODE)
   { CW( 14, 0), 0x6F, 0x00 }, 	//CW14_0 	[7,4]   	//Force mono mode
   { CW( 11, 1), 0xFB, 0x00 }, 	//CW11_1 	[2:2]   0	//Enable MUX mono calibration (ST_CAL) 
   { CW( 18, 1), 0x3F, 0x80 }, 	//CW18_1 	[7:6]   2	//Enable MUX L calibration (MUXLOFFCAL, TEST_LCH)
   { CW( 19, 1), 0x8F, 0x10 }, 	//CW19_1 	[6:4]   1	//Enable MUX L calibration (MUXLOFFCAL, TEST_LCH)
   { CW( 14, 1), 0xFE, 0x00 }, 	//CW14_1 	[0:0]   0	//Turn off AMP1
   { CW(  7, 1), 0xFC, 0x03 }, 	//CW7_1   	[1:0]   3 	//Enable calibraton process
   { CW(  6, 0), 0xFC, 0x01 }, 	//CW6_0 	[1:0]   1	//Set window soure
   { CW(  6, 1), 0x00, 0x90 }, 	//CW6_1 	[7:0]   144	//Set clock ource and target register 
   { CW(  9, 0), 0xBF, 0x40 }, 	//CW9_0 	[6:6]   1	//
   { CW(  1, 0), 0x00, 0x00 }, 	//CW1_0   	[7:0]   0 	//Clear all FLG registers
   { CW(  6, 1), 0xFE, 0x01 }, 	//CW6_1 	[0:0]   1	//Start calibration process
   { CW(127, 1), 0x00, 0x01 }, 	// <----------------- polling SARIFG
   { CW(  1, 0), 0x00, 0x00 }, 	//CW1_0   	[7:0]  0 	//reset
   { CW( 18, 1), 0x3F, 0x40 }, 	//CW18_1 	[7:6]   1	//Enable MUX R calibration (MUXROFFCAL, TEST_RCH)
   { CW( 19, 1), 0x8F, 0x20 }, 	//CW19_1 	[6:4]   2	//Enable MUX R calibration (MUXROFFCAL, TEST_RCH)
   { CW(  6, 1), 0x00, 0xA0 }, 	//CW6_1 	[7:0]   160	//Set clock source and target register
   { CW(  9, 0), 0xBF, 0x40 }, 	//CW9_0 	[6:6]   1	// 
   { CW(  1, 0), 0x00, 0x00 }, 	//CW1_0   	[7:0]   0 	//Clear all FLG registers
   { CW(  6, 1), 0xFE, 0x01 }, 	//CW6_1 	[0:0]   1	//Start calibration process
   { CW(127, 1), 0x00, 0x01 }, 	// <----------------- polling SARIFG
   { CW(  1, 0), 0x00, 0x00 }, 	//CW1_0   	[7:0]  0 	//reset
};

#define MUXOFF_ST_COUNT 26
static const ctrl_word_operation MuxOff_ST[MUXOFF_ST_COUNT] = {
   { CW( 14, 0), 0x6F, 0x10 }, 	//CW14_0 	[4:4]   1	//Force stereo mode
   { CW( 11, 1), 0xFB, 0x04 }, 	//CW11_1 	[2:2]   1	//Enable MUX stereo calibration (ST_CAL)
   { CW( 18, 1), 0x3F, 0x80 }, 	//CW18_1 	[7:6]   2	//Enable MUX L calibration (MUXLOFFCAL, TEST_LCH)
   { CW( 19, 1), 0x8F, 0x10 }, 	//CW19_1 	[6:4]   1	//Enable MUX L calibration (MUXLOFFCAL, TEST_LCH)
   { CW(  6, 1), 0x00, 0xB0 }, 	//CW6_1 	[7:0]   176	//Set clock source and target register
   { CW(  9, 0), 0xBF, 0x40 }, 	//CW9_0 	[6:6]   1	//
   { CW(  1, 0), 0x00, 0x00 }, 	//CW1_0   	[7:0]   0 	//Clear all FLG registers
   { CW(  6, 1), 0xFE, 0x01 }, 	//CW6_1 	[0:0]   1	//Start calibration process
   { CW(127, 1), 0x00, 0x01 }, 	// <----------------- polling SARIFG
   { CW(  1, 0), 0x00, 0x00 }, 	//CW1_0   	[7:0]  0 	//reset
   { CW( 18, 1), 0x3F, 0x40 }, 	//CW18_1 	[7:6]   1	//Enable MUX R calibration (MUXROFFCAL, TEST_RCH)
   { CW( 19, 1), 0x8F, 0x20 }, 	//CW19_1 	[6:4]   2	//Enable MUX R calibration (MUXROFFCAL, TEST_RCH)
   { CW(  6, 1), 0x00, 0xC0 }, 	//CW6_1 	[7:0]   192	//Set clock source and target register
   { CW(  9, 0), 0xBF, 0x40 }, 	//CW9_0 	[6:6]   1	//
   { CW(  1, 0), 0x00, 0x00 }, 	//CW1_0   	[7:0]   0 	//Clear all FLG registers
   { CW(  6, 1), 0xFE, 0x01 }, 	//CW6_1 	[0:0]   1	//Start calibration process
   { CW(127, 1), 0x00, 0x01 }, 	// <----------------- polling SARIFG
   { CW(  1, 0), 0x00, 0x00 }, 	//CW1_0   	[7:0]   0 	//reset
   { CW(  6, 1), 0x0F, 0x10 }, 	//CW6_1 	[7:4]   1	//Set clock source and target register
   { CW(  7, 1), 0xFC, 0x00 }, 	//CW7_1 	[1:0]   0	//Disable calibration process
   { CW(  9, 0), 0xBF, 0x00 }, 	//CW9_0 	[6:6]   0	//Resume to default value
   { CW( 18, 1), 0x3F, 0x00 }, 	//CW18_1   	[7:6]   0 	//Disable MUX calibration 
   { CW( 19, 1), 0x8F, 0x00 }, 	//CW19_1 	[6:4]   0	//Disable MUX calibration 
   { CW( 11, 1), 0xFB, 0x00 }, 	//CW11_1   	[2:2]   0 	//Disable MUX stereo calibration (ST_CAL)
   { CW(  8, 0), 0xFE, 0x01 }, 	//CW8_0 	[0:0]   1	//Turn on Demod.
   { CW( 14, 1), 0xFE, 0x01 }, 	//CW14_1   	[0:0]   1 	//Turn on AMP1
};

#define PFOSC_560K_COUNT 26
static const ctrl_word_operation Pfosc_560K[PFOSC_560K_COUNT] = {
   { CW( 10, 0), 0xFE, 0x01 }, 	//CW10_0 	[0:0]   1	//Enable PTOSC calibraton and power on PTOSC
   { CW( 10, 0), 0xFD, 0x02 }, 	//CW10_0 	[1:1]   1	//Enable PTOSC calibraton and power on PTOSC
   { CW( 10, 0), 0xFD, 0x00 }, 	//CW10_0 	[1:1]   0	//Reset PTOSC
   { CW( 10, 0), 0xFD, 0x02 }, 	//CW10_0 	[1:1]   1	//Reset PTOSC
   { CW(  6, 1), 0x00, 0xEE }, 	//CW6_1 	[7:0]   238	//Set clock ource and target register 
   { CW( 25, 1), 0x00, 0x04 }, 	//CW25_1  	[7:0]   3+,4	//CNT_T[11:8]
#if defined   REF_CLK_32K //==============================================
   { CW( 25, 0), 0x00, 0x46 }, 	//CW25_0  	[7:0]  123,70	//CNT_T[7:0]
#elif defined REF_CLK_13M
   { CW( 25, 0), 0x00, 0x5E }, 	//CW25_0  	[7:0]  94
#elif defined REF_CLK_26M
   { CW( 25, 0), 0x00, 0x5E }, 	//CW25_0  	[7:0]  94
#endif //=================================================================
   { CW(  7, 1), 0xFC, 0x03 }, 	//CW7_0   	[1:0]   3 	//
   { CW(  3, 0), 0xEF, 0x10 }, 	//CW3_0 	[4:4]   1	//
   { CW(  5, 1), 0x7F, 0x00 }, 	//CW5_1   	[7:7]   0 	//
   { CW(  1, 0), 0x00, 0x00 }, 	//CW1_0   	[7:0]   0 	//Clear all FLG registers
   { CW(  6, 1), 0xFE, 0x01 }, 	//CW6_1 	[0:0]   1	//Start calibration process
   { CW(127, 1), 0x00, 0x01 }, 	// <----------------- polling SARIFG
   { CW(  1, 0), 0x00, 0x00 }, 	//CW1_0   	[7:0]   0 	//reset
   { CW(  6, 1), 0x00, 0xFE }, 	//CW6_1 	[7:0]   254	//Set clock source and target register
   { CW(  1, 0), 0x00, 0x00 }, 	//CW1_0   	[7:0]   0 	//Clear all FLG registers
   { CW(  6, 1), 0xFE, 0x01 }, 	//CW6_1 	[0:0]   1	//Start calibration process
   { CW(127, 1), 0x00, 0x01 }, 	// <----------------- polling SARIFG
   { CW(  1, 0), 0x00, 0x00 }, 	//CW1_0   	[7:0]   0 	//reset
   { CW(  6, 1), 0x0F, 0x10 }, 	//CW6_1 	[7:4]   1	//Set clock source and target register
   { CW(  7, 1), 0xFC, 0x00 }, 	//CW7_1 	[1:0]   0	//Disable calibration process
   { CW(  3, 0), 0xEF, 0x00 }, 	//CW3_0   	[4:4]   0 	//Disable RFPLL calibration mode
   { CW(  5, 1), 0x7F, 0x80 }, 	//CW5_1 	[7:7]   1	//PRE_CH=1
   { CW(  3, 0), 0xEF, 0x10 }, 	//CW3_0   	[4:4]   1 	//CAL_PLL=1
#if defined (__MT6188CR_DISABLE_SOFTMUTE__)
   { CW( 14, 0), 0xF7, 0x00 }, 	//CW14_0 	[3:3]   1	//SMUTE=1
#else
   { CW( 14, 0), 0xF7, 0x08 }, 	//CW14_0 	[3:3]   1	//SMUTE=1
#endif
   { CW( 34, 0), 0xFE, 0x01 }, 	//CW34_0 	[0:0]   1	//
};

#define SM_CAL_OFFSET_COUNT 20
static const ctrl_word_operation SM_Cal_Offset[SM_CAL_OFFSET_COUNT] = {
   { CW( 22, 1), 0xFE, 0x01 }, 	//CW22_1  	[0:0]   1	//
   { CW( 17, 1), 0x7F, 0x00 }, 	//CW17_1   	[7:7]   0 	//
   { CW( 18, 0), 0xF7, 0x00 }, 	//CW18_0 	[3:3]   0	//
   { CW( 18, 1), 0xF7, 0x00 }, 	//CW18_1   	[3:3]   0 	//
   { CW(  6, 1), 0x0F, 0x10 }, 	//CW6_1 	[7:4]   1	//Set clock source and target register
   { CW(  7, 1), 0xFC, 0x00 }, 	//CW7_1   	[1:0]   0 	//Disable calibration process
   { CW(101, 1), 0xF0, 0x08 }, 	//CW101_1 	[3:0]   8	//
   { CW( 19, 0), 0xFD, 0x02 }, 	//CW19_0   	[1:1]   1 	//Enable and wait 20ms
   { CW(  7, 1), 0xFC, 0x03 }, 	//CW7_1 	[1:0]   3	//Enable calibration process
   { CW(  6, 1), 0x00, 0x40 }, 	//CW6_1 	[7:0]   64	//Set clock source and target register
   { CW(  9, 0), 0xBF, 0x00 }, 	//CW9_0   	[6:6]   0 	//
   { CW(  1, 0), 0x00, 0x00 }, 	//CW1_0   	[7:0]   0 	//Clear all FLG registers
   { CW(  6, 1), 0xFE, 0x01 }, 	//CW6_1 	[0:0]   1	//Start calibration process
   { CW(127, 1), 0x00, 0x01 }, 	// <----------------- polling SARIFG
   { CW(  1, 0), 0x00, 0x00 }, 	//CW1_0   	[7:0]   0 	//reset
   { CW(  6, 1), 0x0F, 0x10 }, 	//CW6_1 	[7:4]   1	//Set clock source and target register
   { CW(  7, 1), 0xFC, 0x00 }, 	//CW7_1 	[1:0]   0	//Disable calibration process
   { CW(  9, 0), 0xBF, 0x00 }, 	//CW9_0   	[6:6]   0 	//Resume to default value
   { CW( 19, 0), 0xFD, 0x00 }, 	//CW19_0 	[1:1]   0	//
   { CW( 22, 1), 0xFE, 0x00 }, 	//CW22_1   	[0:0]   0 	//
};

#define SM_CAL_SLOPE_COUNT 20
static const ctrl_word_operation SM_Cal_Slope[SM_CAL_SLOPE_COUNT] = {
   { CW( 22, 1), 0xFE, 0x01 }, 	//CW22_1  	[0:0]   1	//
   { CW( 17, 1), 0x7F, 0x00 }, 	//CW17_1   	[7:7]   0 	//
   { CW( 18, 0), 0xF7, 0x08 }, 	//CW18_0 	[3:3]   1	//
   { CW( 18, 1), 0xF7, 0x00 }, 	//CW18_1   	[3:3]   0 	//
   { CW( 19, 0), 0xFC, 0x03 }, 	//CW19_0 	[1:0]   3	//
   { CW(  6, 1), 0x00, 0x80 }, 	//CW6_1   	[7:0]   128 	//Set clock source and target register
   { CW(  9, 0), 0xBF, 0x40 }, 	//CW9_0   	[6:6]   1 	//
   { CW(  7, 1), 0xFC, 0x03 }, 	//CW7_1 	[1:0]   3	//Enable calibration process
   { CW(  1, 0), 0x00, 0x00 }, 	//CW1_0   	[7:0]   0 	//Clear all FLG registers
   { CW(  6, 1), 0xFE, 0x01 }, 	//CW6_1 	[0:0]   1	//Start calibration process
   { CW(127, 1), 0x00, 0x01 }, 	// <----------------- polling SARIFG
   { CW(  1, 0), 0x00, 0x00 }, 	//CW1_0   	[7:0]   0 	//reset
   { CW(  6, 1), 0x0F, 0x10 }, 	//CW6_1 	[7:4]   1	//Set clock source and target register
   { CW(  7, 1), 0xFC, 0x00 }, 	//CW7_1 	[1:0]   0	//Disable calibration process
   { CW(  9, 0), 0xBF, 0x00 }, 	//CW9_0   	[6:6]   0 	//Resume to default value
   { CW( 17, 1), 0x7F, 0x00 }, 	//CW17_1 	[7:7]   0	//
   { CW( 18, 0), 0xF7, 0x00 }, 	//CW18_0   	[3:3]   0 	//
   { CW( 18, 1), 0xF7, 0x00 }, 	//CW18_1 	[3:3]   0	//
   { CW( 19, 0), 0xFC, 0x00 }, 	//CW19_0 	[1:0]   0	//
   { CW( 22, 1), 0xFE, 0x00 }, 	//CW22_1   	[0:0]   0 	//
};

#define AAMP1_LCHOFF_MN_COUNT 21
static const ctrl_word_operation Aamp1_Lchoff_MN[AAMP1_LCHOFF_MN_COUNT] = {
   { CW( 19, 0), 0xFD, 0x02 }, 	//CW19_0  	[1:1]   1	//Feedthrough test signal
   { CW(  5, 0), 0xFD, 0x00 }, 	//CW5_0   	[1:1]   0 	//Turn off LNA input path
   { CW( 30, 1), 0x00, 0x00 }, 	//CW30_1   	[7:0]   0 	//
   { CW( 30, 0), 0x00, 0x00 }, 	//CW30_0   	[7:0]   0 	//
   { CW( 11, 1), 0x3F, 0x80 }, 	//CW11_1 	[7:6]   2	//Gain option (D2S_MODE, I2V_MODE)
   { CW( 14, 0), 0x6F, 0x00 }, 	//CW14_0 	[7,4]   	//Force mono mode
   { CW( 17, 0), 0x3F, 0x80 }, 	//CW17_0   	[7:6]   2 	//Enable AAMP1 L calibration
   { CW(  7, 1), 0xFC, 0x03 }, 	//CW7_0 	[1:0]   3	//Enable calibraton process
   { CW(  6, 0), 0xFC, 0x01 }, 	//CW6_0   	[1:0]   1 	//Set window soure
   { CW(  6, 1), 0x00, 0x00 }, 	//CW6_1   	[7:0]   0 	//Set clock source and target register (coarse tune)
   { CW(  9, 0), 0xBF, 0x40 }, 	//CW9_0 	[6:6]   1	//
   { CW(  1, 0), 0x00, 0x00 }, 	//CW1_0   	[7:0]   0 	//Clear all FLG registers
   { CW(  6, 1), 0xFE, 0x01 }, 	//CW6_1 	[0:0]   1	//Start calibration process
   { CW(127, 1), 0x00, 0x01 }, 	// <----------------- polling SARIFG
   { CW(  1, 0), 0x00, 0x00 }, 	//CW1_0   	[7:0]   0 	//reset
   { CW(  6, 1), 0x00, 0x10 }, 	//CW6_1 	[7:0]   16	//Set clock source and target register
   { CW(  9, 0), 0xBF, 0x40 }, 	//CW9_0   	[6:6]   1 	//
   { CW(  1, 0), 0x00, 0x00 }, 	//CW1_0   	[7:0]   0 	//Clear all FLG registers
   { CW(  6, 1), 0xFE, 0x01 }, 	//CW6_1 	[0:0]   1	//Start calibration process
   { CW(127, 1), 0x00, 0x01 }, 	// <----------------- polling SARIFG
   { CW(  1, 0), 0x00, 0x00 }, 	//CW1_0   	[7:0]   0 	//reset
};

#define AAMP1_RCHOFF_MN_COUNT 19
static const ctrl_word_operation Aamp1_Rchoff_MN[AAMP1_RCHOFF_MN_COUNT] = {
   { CW( 17, 0), 0x3F, 0x40 }, 	//CW17_0  	[7:6]   1	//Enable AAMP1 R calibration
   { CW(  6, 1), 0x00, 0x20 }, 	//CW6_1   	[7:0]   32 	//Set clock source and target register (coarse tune)
   { CW(  9, 0), 0xBF, 0x40 }, 	//CW9_0 	[6:6]   1	//
   { CW(  1, 0), 0x00, 0x00 }, 	//CW1_0   	[7:0]   0 	//Clear all FLG registers
   { CW(  6, 1), 0xFE, 0x01 }, 	//CW6_1 	[0:0]   1	//Start calibration process
   { CW(127, 1), 0x00, 0x01 }, 	// <----------------- polling SARIFG
   { CW(  1, 0), 0x00, 0x00 }, 	//CW1_0   	[7:0]   0 	//reset
   { CW(  6, 1), 0x00, 0x30 }, 	//CW6_1 	[7:0]   48	//Set clock source and target register
   { CW(  9, 0), 0xBF, 0x40 }, 	//CW9_0 	[6:6]   1	//
   { CW(  1, 0), 0x00, 0x00 }, 	//CW1_0   	[7:0]   0 	//Clear all FLG registers
   { CW(  6, 1), 0xFE, 0x01 }, 	//CW6_1 	[0:0]   1	//Start calibration process
   { CW(127, 1), 0x00, 0x01 }, 	// <----------------- polling SARIFG
   { CW(  1, 0), 0x00, 0x00 }, 	//CW1_0   	[7:0]   0 	//reset
   { CW(  6, 1), 0x0F, 0x10 }, 	//CW6_1 	[7:4]   1	//Set clock source and target register
   { CW(  7, 1), 0xFC, 0x00 }, 	//CW7_1   	[1:0]   0 	//Disable calibration process
   { CW(  9, 0), 0xBF, 0x00 }, 	//CW9_0   	[6:6]   0 	//Resume to default value
   { CW( 17, 0), 0x3F, 0x00 }, 	//CW17_0 	[7:6]   0	//Disable AAMP1 calibration
   { CW(  5, 0), 0xFD, 0x02 }, 	//CW5_0   	[1:1]   1 	//Turn on LNA input path
   { CW( 19, 0), 0xFD, 0x00 }, 	//CW19_0   	[1:1]   0 	//Disconnect test signal
};

#define CAL_PFOSC_COMMAND_COUNT_V5 22
static const ctrl_word_operation CalPfoscProcess_V5[CAL_PFOSC_COMMAND_COUNT_V5] = {
   { CW( 10, 0), 0xFE, 0x01 }, 	//CW10_0 	[0:0]   1	//RFOSC_P,FROSC_CAL 
   { CW( 10, 0), 0xFD, 0x02 }, 	//CW10_0 	[1:1]   1 	//Enable PTOSC calibraton and power on PTOSC
   { CW( 10, 0), 0xFD, 0x00 }, 	//CW10_0 	[1:1]   0 	//Reset PTOSC
   { CW( 10, 0), 0xFD, 0x02 }, 	//CW10_0 	[1:1]   1 	//Reset PTOSC
   { CW(  6, 1), 0x00, 0xEE }, 	//CW6_1   	[7:0]  	238	//Set clock ource and target register 
   { CW( 25, 1), 0x00, 0x03 }, 	//CW25_1  	[7:0]   3	//CNT_T_[11:8]
#if defined   REF_CLK_32K //==============================================
   { CW( 25, 0), 0x00, 0x7B }, 	//CW25_0  	[7:0]   123	//CNT_T_[7:0]
#elif defined REF_CLK_13M
   { CW( 25, 0), 0x00, 0x5E }, 	//CW25_0  	[7:0]   94
#elif defined REF_CLK_26M
   { CW( 25, 0), 0x00, 0x5E }, 	//CW25_0  	[7:0]   94
#endif //=================================================================
   { CW( 97, 0), 0x00, 0x08 }, 	//CW97_0 	[7:0]   8 	//
   { CW(  7, 1), 0xFC, 0x03 }, 	//CW7_0 	[1:0]   3 	//
   { CW(  3, 0), 0xEF, 0x10 }, 	//CW3_0   	[4:4]  	1	//
   { CW(  5, 1), 0x7F, 0x00 }, 	//CW5_1  	[7:7]   0	//
   { CW(  1, 0), 0x00, 0x00 }, 	//CW1_0   	[7:0]   0
   { CW(  6, 1), 0xFE, 0x01 }, 	//CW6_1   	[0:0]   1	//WIN_EN
   { CW(127, 1), 0x00, 0x01 }, 	//<----------------- polling SARIFG
   { CW(  1, 0), 0x00, 0x00 }, 	//CW1_0   	[7:0]   0 	//reset CW1_0
   { CW(  6, 1), 0x00, 0xFE }, 	//CW6_1   	[7:0]  	254	//Set clock source and target register
   { CW(  1, 0), 0x00, 0x00 }, 	//CW1_0   	[7:0]   0
   { CW(  6, 1), 0xFE, 0x01 }, 	//CW6_1   	[0:0]   1	//WIN_EN
   { CW(127, 1), 0x00, 0x01 }, 	//<----------------- polling SARIFG
   { CW(  1, 0), 0x00, 0x00 }, 	//CW1_0   	[7:0]   0 	//reset CW1_0
   { CW(  6, 1), 0x00, 0x82 }, 	//CW6_1   	[7:4]  	130	//Set clock source and target register
   { CW(  7, 1), 0xFC, 0x00 }, 	//CW7_1   	[1:0]   0	//Disable calibration process
//   { CW( 97, 0), 0xE0, 0x0F }, /// CW97_0  [4:0]  k=
//   { CW(  3, 0), 0xEF, 0x00 }, /// CW3_0   [4:4]   0   
};

#define CAL_DelayCalibration_COMMAND_COUNT_V5 18
static const ctrl_word_operation CalDelayCalibrationProcess_V5[CAL_DelayCalibration_COMMAND_COUNT_V5] = {
   { CW( 11, 1), 0xCF, 0x20 }, 	//CW11_1 	[5:4]   2	//Enable delay calibration loop 
   { CW(  6, 1), 0x0F, 0x80 }, 	//CW6_1 	[7:4]   8 	//Set target register(REFDI)
   { CW(  7, 1), 0xFC, 0x00 }, 	//CW7_1 	[1:0]   0 	//Disable calibration process
   { CW( 12, 1), 0xFC, 0x00 }, 	//CW12_1 	[1:0]   0 	//Enable PLL loop
   { CW(  6, 0), 0xFC, 0x01 }, 	//CW6_0   	[1:0]  	1	//Set windows source
   { CW(  7, 1), 0xFC, 0x03 }, 	//CW7_1  	[1:0]   3	//Enable calibration
   { CW(  1, 0), 0x00, 0x00 }, 	//CW1_0   	[7:0]   0	//Clear all flag register
   { CW(  6, 1), 0xFE, 0x01 }, 	//CW6_1   	[0:0]   1	//Start calibration
   { CW(127, 1), 0x00, 0x01 }, 	//<----------------- polling SARIFG
   { CW(  1, 0), 0x00, 0x00 }, 	//CW1_0   	[7:0]   0 	//reset CW1_0
   { CW(  6, 1), 0x0F, 0x90 }, 	//CW6_1   	[7:4]  	9	//Set target register(FBDI)
   { CW(  7, 1), 0xFC, 0x00 }, 	//CW7_1   	[1:0]   0	//Disable calibraton porcess
   { CW(  6, 0), 0xFC, 0x01 }, 	//CW6_0   	[1:0]   1	//Set windows source
   { CW(  7, 1), 0xFC, 0x03 }, 	//CW7_1   	[1:0]   3	//Enable calibration
   { CW(  1, 0), 0x00, 0x00 }, 	//CW1_0   	[7:0]   0 	//Clear all FLAG register
   { CW(  6, 1), 0xFE, 0x01 }, 	//CW6_1   	[0:0]  	1	//Start calibration
   { CW(127, 1), 0x00, 0x01 }, 	//<----------------- polling SARIFG
   { CW(  1, 0), 0x00, 0x00 }, 	//CW1_0   	[7:0]   0 	//reset CW1_0
//   { CW( 11, 1), 0xCF, 0x10 }, 	//CW11_1   	[5:4]  	0	//Disable delay calibration loop

};
//end chip ID = 5

#define CAL_PFOSC_COMMAND_COUNT 18
static const ctrl_word_operation CalPfoscProcess[CAL_PFOSC_COMMAND_COUNT] = {
   { CW( 10, 0), 0xFE, 0x01 }, 	//CW10_0 	[0:0]   1	//RFOSC_P,FROSC_CAL 
   { CW( 10, 0), 0xFD, 0x02 }, 	//CW10_0 	[1:1]   1 	//FROSC_CAL
   { CW( 10, 0), 0xFD, 0x00 }, 	//CW10_0 	[1:1]   0 
   { CW( 10, 0), 0xFD, 0x02 }, 	//CW10_0 	[1:1]   1 
   { CW(  6, 1), 0x00, 0xEE }, 	//CW6_1   	[7:0]  	238	//CNT_S[2:0],TAR[3:0]
   { CW( 25, 1), 0xF0, 0x03 }, 	//CW25_1  	[3:0]   3	//CNT_T_[11:8]
#if defined   REF_CLK_32K //==============================================
   { CW( 25, 0), 0x00, 0x7A }, 	//CW25_0  	[7:0]   122	//CNT_T_[7:0]
#elif defined REF_CLK_13M
   { CW( 25, 0), 0x00, 0x5E }, 	//CW25_0  	[7:0]   94
#elif defined REF_CLK_26M
   { CW( 25, 0), 0x00, 0x5E }, 	//CW25_0  	[7:0]   94
#endif //=================================================================
   { CW(  1, 0), 0x00, 0x00 }, 	//CW1_0   	[7:0]   0
   { CW(  6, 1), 0xFE, 0x01 }, 	//CW6_1   	[0:0]   1	//WIN_EN
   { CW(127, 1), 0x00, 0x01 }, 	//<----------------- polling SARIFG
   { CW(  1, 0), 0x00, 0x00 }, 	//CW1_0   	[7:0]   0 	//reset CW1_0
   { CW(  6, 1), 0x00, 0xFE }, 	//CW6_1   	[7:0]  	254	//CNT_S[2:0],TAR[3:0]
   { CW(  1, 0), 0x00, 0x00 }, 	//CW1_0   	[7:0]   0
   { CW(  6, 1), 0xFE, 0x01 }, 	//CW6_1   	[0:0]   1	//WIN_EN
   { CW(127, 1), 0x00, 0x01 }, 	//<----------------- polling SARIFG
   { CW(  1, 0), 0x00, 0x00 }, 	//CW1_0   	[7:0]   0 	//reset CW1_0
   { CW(  6, 1), 0x0F, 0x10 }, 	//CW6_1   	[7:4]  	1	//CNT_S[2:0],TAR[3:0]
   { CW(  7, 1), 0xFC, 0x00 }, 	//CW7_1   	[1:0]   0	//CAL[1:0]
//   { CW( 97, 0), 0xE0, 0x0F }, /// CW97_0  [4:0]  k=
//   { CW(  3, 0), 0xEF, 0x00 }, /// CW3_0   [4:4]   0   
};

#define CAL_DelayCalibration_COMMAND_COUNT 19
static const ctrl_word_operation CalDelayCalibrationProcess[CAL_DelayCalibration_COMMAND_COUNT] = {
   { CW( 11, 1), 0xCF, 0x20 }, 	//CW11_1 	[5:4]   2	//Enable delay calibration loop 
   { CW(  6, 1), 0x0F, 0x80 }, 	//CW6_1 	[7:4]   8 	//Set target register(REFDI)
   { CW(  7, 1), 0xFC, 0x00 }, 	//CW7_1 	[1:0]   0 	//Disable calibration process
   { CW( 12, 1), 0xFC, 0x00 }, 	//CW12_1 	[1:0]   0 	//Enable PLL loop
   { CW(  6, 0), 0xFC, 0x01 }, 	//CW6_0   	[1:0]  	1	//Set windows source
   { CW(  7, 1), 0xFC, 0x03 }, 	//CW7_1  	[1:0]   3	//Enable calibration
   { CW(  1, 0), 0x00, 0x00 }, 	//CW1_0   	[7:0]   0	//Clear all flag register
   { CW(  6, 1), 0xFE, 0x01 }, 	//CW6_1   	[0:0]   1	//Start calibration
   { CW(127, 1), 0x00, 0x01 }, 	//<----------------- polling SARIFG
   { CW(  1, 0), 0x00, 0x00 }, 	//CW1_0   	[7:0]   0 	//reset CW1_0
   { CW(  6, 1), 0x0F, 0x90 }, 	//CW6_1   	[7:4]  	9	//Set target register(FBDI)
   { CW(  7, 1), 0xFC, 0x00 }, 	//CW7_1   	[1:0]   0	//Disable calibraton porcess
   { CW(  6, 0), 0xFC, 0x01 }, 	//CW6_0   	[1:0]   1	//Set windows source
   { CW(  7, 1), 0xFC, 0x03 }, 	//CW7_1   	[1:0]   3	//Enable calibration
   { CW(  1, 0), 0x00, 0x00 }, 	//CW1_0   	[7:0]   0 	//Clear all FLAG register
   { CW(  6, 1), 0xFE, 0x01 }, 	//CW6_1   	[0:0]  	1	//Start calibration
   { CW(127, 1), 0x00, 0x01 }, 	//<----------------- polling SARIFG
   { CW(  1, 0), 0x00, 0x00 }, 	//CW1_0   	[7:0]   0 	//reset CW1_0
   { CW( 11, 1), 0xCF, 0x10 }, 	//CW11_1   	[5:4]  	0	//Disable delay calibration loop

};


static kal_uint8 MT6188_ReadCache(kal_uint8 CW)
{
   if (CW < 58)
      return cw_cache[CW];
   else if((CW >= 134)&&(CW < 208))
      return cw_cache[CW-134];
   else
   	  return 0;
}


static kal_bool MT6188_ReadByte(kal_uint8 CW, kal_uint8 *data)
{
   if (CW == 255) {
      *data = 0;
      return KAL_TRUE;
   }
#if defined USE_I2C
   SerialCommStart();                /// send the start sequence
   SerialCommTxByte(MT6188_WRITE);   /// device ID and R/W bit
   SerialCommTxByte(CW);             /// control word
   SerialCommStart();                /// resend the start sequence
   SerialCommTxByte(MT6188_READ);    /// device ID and R/W bit
   SerialCommRxByte(data, 0);        /// read data and send ACK
   SerialCommStop();                 /// send the stop sequence
#elif defined USE_3_WIRE
   SerialCommRxByte(CW, data);
#else
#error "Must define USE_I2C or USE_3_WIRE"
#endif

   return KAL_TRUE;
}


/* MT6188 does not support sequential write. */
static kal_bool MT6188_WriteByte(kal_uint8 CW, kal_uint8 data)
{
   if (CW == 255) {
      kal_uint8 i = 0;
      kal_uint8 dataRead;
      do {
         kal_sleep_task(1);
         MT6188_ReadByte(CW(1,0), &dataRead);
         if (i++ == 40) {
            return KAL_FALSE;
         }
      } while ((dataRead & data)==0);
      return KAL_TRUE;
   }
#if defined USE_I2C
   SerialCommStart();                /// send the start sequence
   SerialCommTxByte(MT6188_WRITE);   /// device ID and R/W bit
   SerialCommTxByte(CW);             /// control word   
   SerialCommTxByte(data);           /// data to be written
   SerialCommStop();                 /// send the stop sequence
#elif defined USE_3_WIRE
   SerialCommTxByte(CW, data);
#else
#error "Must define USE_I2C or USE_3_WIRE"
#endif

if (CW < 58)
      cw_cache[CW] = data;
   else if((CW >= 134)&&(CW < 208))
      cw_cache[CW-134] = data;


#if defined MT6188_DEBUG
   MT6188_ReadByte(CW, &dbg_cw_readback);
#endif
  
   return KAL_TRUE;
}

#if defined MT6188_DEBUG
static void MT6188_DumpCtrlWord(void) {
   kal_uint32 i;
   for (i=0; i<29; i++) 
   {
      MT6188_ReadByte((i<<1), &(cw_cache[i << 1]));
      MT6188_ReadByte((i<<1)+1, &(cw_cache[(i << 1)+1]));
   }

   for (i=96; i<104; i++) {
      MT6189_ReadByte((i<<1), &(cw_cache[(i-67) << 1]));
      MT6189_ReadByte((i<<1)+1, &(cw_cache[((i-67) << 1) + 1]));
   }
}
#endif

/***********************************************************************
*  Engineer mode function (API)
*
*  parameter-->group_idx: mono\stereo\RSSI_threshold\IF_count_delta
*              item_idx: sub select index
*              item_value: set parameter value
***********************************************************************/
void FMDrv_radio_item_info(kal_uint16 group_idx, kal_uint16 item_idx, kal_uint32 item_value)
{
	kal_uint8 TmpReg,TmpReg1;
	
	SerialCommInit();
	switch (group_idx)
	{
		case mono:
			if(item_value == 1)
  			{
      				MT6188_ReadByte(CW(14, 0), &TmpReg1);
      				TmpReg1&=0x2F;
      				MT6188_WriteByte(CW(14, 0),TmpReg1|0x40);
      				mono_flag = 1;
      			}
      			else
      			{
      				MT6188_ReadByte(CW(14, 0), &TmpReg1);
      				TmpReg1&=0x2F;
      				MT6188_WriteByte(CW(14, 0),TmpReg1|0x80);
      				mono_flag = 2;
      			}
   
		break;
		case stereo:
			if(item_value == 0)
			{
				    MT6188_ReadByte(CW(14, 0), &TmpReg1);
      				TmpReg1&=0x2F;
      				MT6188_WriteByte(CW(14, 0),TmpReg1|0x40);
      				mono_flag = 1;
			}
			else
			{
				switch (item_idx)
				{
					case Sblend_ON:
						MT6188_ReadByte(CW(14, 0), &TmpReg1);
      					TmpReg1&=0x2F;
      					MT6188_WriteByte(CW(14, 0),TmpReg1|0xD0);
      					mono_flag = 3;
					break;
					case Sblend_OFF:
						MT6188_ReadByte(CW(14, 0), &TmpReg1);
      					TmpReg1&=0x2F;
      					MT6188_WriteByte(CW(14, 0),TmpReg1|0x90);
      					mono_flag = 4;
					break;
				}
			}
		break;
		case RSSI_threshold:
			if (item_value == 1)
		        	RSSI_offset = 1;
		    	else if (item_value == 2)
		        	RSSI_offset = 2;
		    	else if (item_value == 3)
		        	RSSI_offset = 3;
		    	else if (item_value == 4)
		        	RSSI_offset = 4;
		    	else if (item_value == 5)
		        	RSSI_offset = 5;
		    	else if (item_value == 6)
		        	RSSI_offset = 6;
		    	else
		    		RSSI_offset = 0;//FM_RADIO_INPUT_LEVEL_THRESHOLD;
		break;
		case IF_count_delta:
			if(item_value == 10)
			{
#if !defined( MT6205B ) && !defined( MT6208 )
				PRESET_IF_DELTA = 20;
#else
				PRESET_IF_DELTA = 19;
#endif
			}
			else if(item_value == 15)
			{
#if !defined( MT6205B ) && !defined( MT6208 )
				PRESET_IF_DELTA = 30;
#else
				PRESET_IF_DELTA = 28;
#endif
			}
			else if(item_value == 20)
			{
#if !defined( MT6205B ) && !defined( MT6208 )
				PRESET_IF_DELTA = 39;
#else
				PRESET_IF_DELTA = 38;
#endif
			}
			else if(item_value == 25)
			{
#if !defined( MT6205B ) && !defined( MT6208 )
				PRESET_IF_DELTA = 52;
#else
				PRESET_IF_DELTA = 50;
#endif
			}
			else if(item_value == 30)
			{
#if !defined( MT6205B ) && !defined( MT6208 )
				PRESET_IF_DELTA = 60;
#else
				PRESET_IF_DELTA = 58;
#endif
			}
			else
			{
#if !defined( MT6205B ) && !defined( MT6208 )
				PRESET_IF_DELTA = 52;
#else
				PRESET_IF_DELTA = 50;
#endif
			}
		break;
		
	}
	SerialCommRelease();
}

/***********************************************************************
*  Get RSSI Value (internal)
*
*  parameter-->
***********************************************************************/
static kal_uint16 MT6188_GetCurRSSI(void)
{
   kal_uint8 TmpReg;

   MT6188_ReadByte(CW(4, 1), &TmpReg);
   return (TmpReg & 0x1F);
}

/***********************************************************************
*  FM flag clear (internal)
*
*  parameter-->ReadClear: 0: read flag
*                         1: set flag to 0
***********************************************************************/
static kal_uint8 MT6188_ReadIntFlags(kal_bool ReadClear)
{
   kal_uint8 TmpReg;
   MT6188_ReadByte(CW(1, 0), &TmpReg);
   if (ReadClear)
      MT6188_WriteByte(CW(1, 0), 0x00);
   return TmpReg;
}

/***********************************************************************
*  Get IF count (internal)
*
*  parameter-->early_check: 0: don't check IF_count variation
*                           1: check IF_count variation
*             Timing window for 32K clock: 64/32768 = 0.00195 second        
*             Timing window for 26M clock: 49152/26000000 = 0.00189 second  
***********************************************************************/
static kal_int32 MT6188_GetCurIFCnt(kal_uint8 count, kal_uint8 early_check)
{
   kal_uint32 CurIF = 0;
   kal_uint32 WaitingCounter = 0;
   kal_uint32 if_cnt;
   kal_uint8  TmpReg;
   kal_int32  i = (kal_int32)count;
   kal_int32  rssi_consecutive_fail = 0;
   kal_int32  if_consecutive_fail = 0;


//	_valid_rssi = 0;
#ifdef MT6188_DEBUG_DUMP_LOG
   kal_sprintf((void*)_dbg_str, "   MT6188_GetCurIFCnt(%d, %d);\n   \0", count, early_check);
   FS_Write(_file_handle, _dbg_str, strlen((void*)_dbg_str), &_data_written);
#endif
   
	MT6188_WriteByte(CW(24, 0), 0x00);	//Stop real-time calibration; disable 1-bit SAR

   	MT6188_WriteByte(CW(5, 1), (MT6188_ReadCache(CW(5, 1)) & 0xFB) | 0x04);	//CW5_1	[2:2]	0
   
   while (--i >= 0) 
    {
        if (Chip_ID == 5)
            MT6188_WriteByte(CW(21, 0), (MT6188_ReadCache(CW(21, 0)) & 0x7F) | 0x80);//enable IF counter
   	
	    MT6188_WriteByte(CW(6, 1), 0x14);
	    MT6188_WriteByte(CW(1, 0), 0x00);	//clear flag
	
      	MT6188_WriteByte(CW(7, 1), MT6188_ReadCache(CW(7, 1)) & 0xFC);
      	
      	MT6188_WriteByte(CW(6, 0), 0x01);
      	
      	MT6188_WriteByte(CW(1, 0), 0x00);
      	
      	MT6188_WriteByte(CW(1, 1), (MT6188_ReadCache(CW(1, 1)) & 0xF7) | 0x08);	//Enable IF counter interrupt
      	
      	MT6188_WriteByte(CW(20, 0), 0x00);	//clear IF counter
	    MT6188_WriteByte(CW(20, 1), 0x00);
      	

     	MT6188_WriteByte(CW(6, 1), (MT6188_ReadCache(CW(6, 1)) & 0xFE) | 0x01);	//Enable time window
     	
     	/// Make sure that the counting is done
      	do {
         	if (++WaitingCounter == 4096) {
            		ASSERT(0);
            		return 0;
         	}
         	MT6188_ReadByte(CW(1, 0), &TmpReg);
      	} while ((TmpReg & 0x08) == 0);
      	
      	MT6188_ReadByte(CW(20, 1), &TmpReg);
      	if_cnt = (TmpReg & 0x03);
      	if_cnt <<= 8;
      
      	MT6188_ReadByte(CW(20, 0), &TmpReg);
      	if_cnt += TmpReg;
      	     	
      	CurIF += if_cnt;
      	     	

     	MT6188_WriteByte(CW(6, 1), MT6188_ReadCache(CW(6, 1)) & 0xFE);	//Disable time window
     	
	    MT6188_WriteByte(CW(1, 0), 0x00);	//clear flag
     	      	
      	
#ifdef MT6188_DEBUG_DUMP_LOG
      kal_sprintf((void*)_dbg_str, "%d,\0", if_cnt);
      FS_Write(_file_handle, _dbg_str, strlen((void*)_dbg_str), &_data_written);
#endif


      if (early_check) 
      {
        
         if (if_cnt > (PRESET_IF_CNT + 120) ||
             if_cnt < (PRESET_IF_CNT - 120)) 
        {
            if (++if_consecutive_fail == 1) 
            {
               CurIF /= (count-i);
           
#ifdef MT6188_DEBUG_DUMP_LOG
               kal_sprintf((void*)_dbg_str, "\n   IF Early Check Fail!\n\0");
               FS_Write(_file_handle, _dbg_str, strlen((void*)_dbg_str), &_data_written);
#endif
               return -CurIF;
            }
         } 
         else 
         {
            if_consecutive_fail = 0;
         }
      }


    }
    
    CurIF = (CurIF/count);
    if (Chip_ID == 5)
        MT6188_WriteByte(CW(21, 0), 0x00);//0x01//disable IF counter
        																	//disable auto AGC joetsou
    
#ifdef MT6188_DEBUG_DUMP_LOG
   kal_sprintf((void*)_dbg_str, "\n   IF count returned: %d\n\0", CurIF);
   FS_Write(_file_handle, _dbg_str, strlen((void*)_dbg_str), &_data_written);
#endif

   return CurIF;
}

/// Timing window for 32K clock: 64/32768 = 0.00195 second
/// Timing window for 26M clock: 49152/26000000 = 0.00189 second
static kal_uint16 MT6188_GetCurIF(void)
{
   kal_uint32 CurIF = MT6188_GetCurIFCnt(IF_MEASURE_COUNT,0);
   CurIF *= IF_CONVERT_FACTOR;
   CurIF += 500;
   CurIF /= 1000;
   return (kal_uint16) CurIF;
}

/***********************************************************************
*  Set radio frequency (internal)
*
*  parameter-->CurFreq:set frequency
*              HiLoInj:set frequency is forward or backward
***********************************************************************/
static kal_bool MT6188_SetFreq(kal_int32 CurFreq, kal_uint8 HiLoInj)
{
if (Chip_ID == 4 || Chip_ID ==5)       //MT6188_ECO_V4/MT6188D
{
	kal_uint16 Divider, N2, Ns, Target;
	kal_int32 CurFreqLSB,VCO_LSB=0,pf_LSB=0;
   	kal_uint8  TmpReg;
   	kal_uint16 WaitingCounter = 0,freq = 0,CurFreq_PLUS = 0;
kal_uint32 save_irq_mask;

#ifdef MT6188_DEBUG_DUMP_LOG
   kal_uint32 start_t, duration_t;
   kal_sprintf((void*)_dbg_str, "\nMT6188_SetFreq(%d, %d);\n\0", CurFreq, HiLoInj);
   FS_Write(_file_handle, _dbg_str, strlen((void*)_dbg_str), &_data_written);
   start_t = video_get_current_time();
#endif   
   
   	
   	_current_frequency = CurFreq/FM_TUNER_GRID;
	freq = CurFreq;

   	VCO_LSB = CurFreq;
   // Disable PLL calibration	
   {	   	
  
   	/// DividerNumber = (CurFreq +- PresetIF) * 4 / RefClock 
   	if (HiLoInj == LO_INJECTION) 
   	{
     	 	VCO_LSB -= PRESET_IF;
   	} 
   	else 
   	{
      		VCO_LSB += PRESET_IF;
   	}
   		VCO_LSB *= 4000;

#if defined   REF_CLK_32K
   	VCO_LSB += 16384;    	// rounding
   	VCO_LSB /= 32768;		// 1/32768
   	
   	pf_LSB = 3562;		//456*64*4/32.768
//   	pf_LSB += 64;		//rounding
//   	pf_LSB /= 128;		//64*4/32768
#else
   	VCO_LSB += 16927;    	// rounding
   	VCO_LSB /= 33854;	// 384/13000000
   	
   	pf_LSB = 3448;		// 456*64*4/(13000/384)
//   	pf_LSB += 66;		//rounding
//   	pf_LSB /= 132;		//64*384*4/13000000
#endif

//save_irq_mask=SaveAndSetIRQMask();        //Set IRQ to increase process speed   

	MT6188_WriteByte(CW(24, 0), 0x00);	//Stop real-time calibration

   	MT6188_WriteByte(CW(6, 1), MT6188_ReadCache(CW(6, 1)) & 0xFE);	//Reset to default without calibration   
	
   }	
   	
   	MT6188_ReadByte(CW(7, 0), &TmpReg);
   	TmpReg &= 0x1F;
   	if(CurFreq <= 84000)
   		MT6188_WriteByte(CW(7, 0), TmpReg|0xE0);  	// CW7_0  [7:5]  7
   	else if(CurFreq > 84000 && CurFreq <= 92000)
   		MT6188_WriteByte(CW(7, 0), TmpReg|0x60);  	// CW7_0  [7:5]  3
   	else if(CurFreq > 92000 && CurFreq <=100000)
   		MT6188_WriteByte(CW(7, 0), TmpReg|0x20);  	// CW7_0  [7:5]  1	
 	else if(CurFreq > 100000)
 		MT6188_WriteByte(CW(7, 0), TmpReg|0x00);  	// CW7_0  [7:5]  0
   	

   	MT6188_WriteByte(CW(5, 1), (MT6188_ReadCache(CW(5, 1)) & 0xFB) | 0x04);	//CW5_1		[2:2]	1	
   	
   	MT6188_WriteByte(CW(7, 1), MT6188_ReadCache(CW(7, 1)) & 0xFC);		//CW7_1		[1:0]	0	

  	
   	MT6188_WriteByte(CW(5, 1), (MT6188_ReadCache(CW(5, 1)) & 0x7F) | 0x80);	//CW5_1		[7:7]	1
   	
   	MT6188_WriteByte(CW(8, 0), (MT6188_ReadCache(CW(8, 0)) & 0xF7) | 0x08);	//CW8_0		[3:3]	1	

 		   	
   	/// DividerNumber = (CurFreq +- PresetIF) * 4 / RefClock 
   	if (HiLoInj == LO_INJECTION) 
   	{
     	 	CurFreq -= PRESET_IF;
   	} 
   	else 
   	{
      		CurFreq += PRESET_IF;
   	}
   		CurFreq *= 4000;
   		CurFreqLSB = CurFreq;

#if defined   REF_CLK_32K
	CurFreq += 262144;	// rounding
	CurFreq >>= 19;		// 1/16*32768
   	CurFreqLSB += 16384;    // rounding
   	CurFreqLSB /= 32768;	// 1/32768
   	
#else
	CurFreq += 270834;	// rounding
	CurFreq /= 541667; 	// 24/13000000
   	CurFreqLSB += 16927;    // rounding
   	CurFreqLSB /= 33854;	// 384/13000000
   	
#endif
   	
   	MT6188_WriteByte(CW(12, 1), (MT6188_ReadCache(CW(12, 1)) & 0xFC) | 0x03);  	// CW12_1  [1:0]  0x03	turn-on digital clock

   	MT6188_WriteByte(CW(100, 1), (MT6188_ReadCache(CW(100, 1)) & 0xC0) | 0x10);  	// CW100_1 [5:0]  0x10	Set DAC at half range
   	
   	MT6188_WriteByte(CW(100, 0), MT6188_ReadCache(CW(100, 0)) & 0xF8);  		// CW100_0 [2:0]  0	Set DAC at half range
 
   	MT6188_ReadByte(CW(4, 0), &TmpReg);
   	TmpReg &= 0x7F;
   	if (HiLoInj == LO_INJECTION) 
   	{		
   		MT6188_WriteByte(CW(4, 0), TmpReg);  	// CW4_0    [7:7] 0:Lside
   	}
   	else
   	{
   		MT6188_WriteByte(CW(4, 0), TmpReg|0x80);  // CW4_0  [7:7] 1:Hside
   	}
   	

   	MT6188_WriteByte(CW(9, 1), MT6188_ReadCache(CW(9, 1)) & 0xF9);  		// CW9_1    [2:1]  0x00	turn-off AFC loop
   	
   	MT6188_WriteByte(CW(2, 1), MT6188_ReadCache(CW(2, 1)) & 0xC0);  		// CW2_1  [5:0]  0x00	Fix VCO divider value for calibration
   	
   	MT6188_WriteByte(CW(2, 0), 0x04);  		// CW2_0  [7:0]  0x04	Fix original calibration speed
   	
   	MT6188_WriteByte(CW(18, 0), (MT6188_ReadCache(CW(18, 0)) & 0x7F) | 0x80);  	// CW18_0  [7:7] 0x80	Fix original calibration speed
   	
   	MT6188_WriteByte(CW(3, 0), (MT6188_ReadCache(CW(3, 0)) & 0xEF) | 0x10);  	// CW3_0   [4:4] 0x10	Disconnect RFPLL
   	
   	MT6188_WriteByte(CW(3, 0), (MT6188_ReadCache(CW(3, 0)) & 0xF8) | 0x05);  	// CW3_0   [2:0] 0x05	Turn-on reference clock path
   	
   	MT6188_WriteByte(CW(7, 1), (MT6188_ReadCache(CW(7, 1)) & 0xFC) | 0x03);  	// CW7_1   [1:0] 0x03  Calibration standby

   	MT6188_WriteByte(CW(6, 0), MT6188_ReadCache(CW(6, 0)) & 0xFC);  		// CW6_0   [1:0] 0x00  select calibration window
   	
   	MT6188_WriteByte(CW(6, 1), 0x00);  		// CW6_1   [7:0] 0x00  select calibratio MSB of VCO

	MT6188_WriteByte(CW(25, 0), (kal_uint8)(CurFreq&0xFF));
   	MT6188_WriteByte(CW(25, 1), (kal_uint8)(CurFreq>>8));  	// CW25   Set calibration target frequency

 
   	MT6188_WriteByte(CW(1, 0), 0x00);  		// CW1_0   [7:0]  clear interrupt flag
   	
   	MT6188_WriteByte(CW(6, 1), (MT6188_ReadCache(CW(6, 1)) & 0xFE) | 0x01);  	// CW6_1   [0:0]  0x01	start calibration

#ifdef MT6188_timing_cal
#ifdef MT6188_DEBUG_DUMP_LOG
   duration_t = video_get_duration_ms(start_t);
   kal_sprintf((void*)_dbg_str, "  Set_freq1: %d ms.\n\0", duration_t);
   FS_Write(_file_handle, _dbg_str, strlen((void*)_dbg_str), &_data_written);
#endif   
#endif   

   	
   	/// Make sure that the counting is done
   	do {
   		
   	   	if ((WaitingCounter >> 8) == 1) {
   	      		return KAL_FALSE;
   	   }
   	   	if ((++WaitingCounter & 0x00FF) != 0) {
   	      		MT6188_ReadByte(CW(1, 0), &TmpReg);
   	   }
   	} while ((TmpReg & 0x01) == 0);
   		
   	MT6188_WriteByte(CW(1, 0), 0x00);    		// Reset CW1_0	Clear interrupt flag

#ifdef MT6188_timing_cal
#ifdef MT6188_DEBUG_DUMP_LOG
   duration_t = video_get_duration_ms(start_t);
   kal_sprintf((void*)_dbg_str, "  Set_freq2: %d ms.\n\0", duration_t);
   FS_Write(_file_handle, _dbg_str, strlen((void*)_dbg_str), &_data_written);
#endif   
#endif   

 
 	MT6188_ReadByte(CW(96, 0), &TmpReg);
   	if(TmpReg <= 63)
   		MT6188_WriteByte(CW(96, 0), TmpReg+1);	// 
   		

   	MT6188_WriteByte(CW(5, 1), MT6188_ReadCache(CW(5, 1)) & 0xFB);  		// CW5_1  [2:2]  0x00	fit LSB of VCO
	
	MT6188_WriteByte(CW(1, 0), 0x00);    		// Reset CW1_0	Clear interrupt flag 	
	
   	MT6188_WriteByte(CW(6, 0), (MT6188_ReadCache(CW(6, 0)) & 0xFC) | 0x01);  	// CW6_0  [1:0]  0x01	Select calibration window for LSB
   	
   	MT6188_WriteByte(CW(6, 1), 0x30);  		// CW6_1  [7:0]  0x30(32)  select calibration LSB1 of VCO
   	
   	MT6188_WriteByte(CW(25, 0), (kal_uint8)(CurFreqLSB&0xFF));
   	MT6188_WriteByte(CW(25, 1), (kal_uint8)(CurFreqLSB>>8));  	// CW25 [11:0]   Set calibration target frequency for VCO LSB


   	MT6188_WriteByte(CW(6, 1), (MT6188_ReadCache(CW(6, 1)) & 0xFE) | 0x01);  	// CW6_1  [0:0]  0x01	start calibration

#ifdef MT6188_timing_cal
#ifdef MT6188_DEBUG_DUMP_LOG
   duration_t = video_get_duration_ms(start_t);
   kal_sprintf((void*)_dbg_str, "  Set_freq3: %d ms.\n\0", duration_t);
   FS_Write(_file_handle, _dbg_str, strlen((void*)_dbg_str), &_data_written);
#endif   
#endif
   	  	
   	/// Make sure that the counting is done
   	do {
   	   	if ((WaitingCounter >> 12) == 1) {
   	      		return KAL_FALSE;
   	   }
   	   	if ((++WaitingCounter & 0x0FFF) != 0) {
   	      		MT6188_ReadByte(CW(1, 0), &TmpReg);
   	   }
   	} while ((TmpReg & 0x01) == 0);
   		
   	MT6188_WriteByte(CW(1, 0), 0x00);    		// Reset CW1_0

    if (Chip_ID == 5)
        MT6188_WriteByte(CW(6, 1),  (MT6188_ReadCache(CW(6, 1)) & 0x0F) | 0x10);  	// CW6_1  [7:4]  0x01

   	MT6188_WriteByte(CW(7, 1), MT6188_ReadCache(CW(7, 1)) & 0xFC);  		// CW7_1  [1:0]  0	reset to default without cali
   	
   	MT6188_WriteByte(CW(12, 1), MT6188_ReadCache(CW(12, 1)) & 0xFC);  		// CW12_1 [1:0]  0	reset to default without cali
   	
   	MT6188_WriteByte(CW(5, 1), (MT6188_ReadCache(CW(5, 1)) & 0xFB) | 0x04);  	// CW5_1  [2:2]  0x04  	reset to default without cali
   	
   	MT6188_WriteByte(CW(8, 0), (MT6188_ReadCache(CW(8, 0)) & 0xF7) | 0x08);  	// CW8_0  [3:3]  0x08  	reset to default without cali

    MT6188_WriteByte(CW(5, 1), MT6188_ReadCache(CW(5, 1)) & 0x7F);	//CW5_1		[7:7]	0

#ifdef MT6188_timing_cal
#ifdef MT6188_DEBUG_DUMP_LOG
   duration_t = video_get_duration_ms(start_t);
   kal_sprintf((void*)_dbg_str, "  Set_freq4: %d ms.\n\0", duration_t);
   FS_Write(_file_handle, _dbg_str, strlen((void*)_dbg_str), &_data_written);
#endif   
#endif   

   
   // Enable (1-bit SAR)
   {

   	MT6188_WriteByte(CW(6, 1), MT6188_ReadCache(CW(6, 1)) & 0x0F);	//CW6_1	[7:4]	0   
	
   	MT6188_WriteByte(CW(7, 1), MT6188_ReadCache(CW(7, 1)) & 0xFC);	//CW7_1 [1:0]	0
   	
   	MT6188_WriteByte(CW(5, 1), MT6188_ReadCache(CW(5, 1)) & 0xFB);	//CW5_1	[2:2]	0

   	MT6188_WriteByte(CW(18, 0), MT6188_ReadCache(CW(18, 0)) & 0x7F);  	// CW18_0  [7:7] 0x00	Fix original calibration speed
   	   	
   	MT6188_WriteByte(CW(24, 1), (MT6188_ReadCache(CW(24, 1)) & 0xF8) | 0x05);	//CW24_1	[2:0]	3
   	
   	MT6188_WriteByte(CW(25, 0), (kal_uint8)(VCO_LSB&0xFF));
   	MT6188_WriteByte(CW(25, 1), (kal_uint8)(VCO_LSB>>8));  	// CW25 [11:0]   Set calibration target frequency for VCO LSB


   	MT6188_WriteByte(CW(26, 0), (kal_uint8)(pf_LSB&0xFF));
   	MT6188_WriteByte(CW(26, 1), (kal_uint8)(pf_LSB>>8));  	// CW26 [11:0]   Set calibration target frequency for pf
	
   	
   	MT6188_WriteByte(CW(10, 0), (MT6188_ReadCache(CW(10, 0)) & 0xFC) | 0x03);	//CW10_0	[1:0]	3
   	

	MT6188_WriteByte(CW(6, 1), 0);	//CW6_1	[0:0]	0
   	MT6188_WriteByte(CW(6, 1), 1);	//CW6_1	[0:0]	1
   	

   	MT6188_WriteByte(CW(24, 0), MT6188_ReadCache(CW(24, 0)) & 0xF8);	//CW24_0	[2:0]	M=0
   	
   	MT6188_WriteByte(CW(24, 0), (MT6188_ReadCache(CW(24, 0)) & 0xEF) | 0x10);	//CW24_0	[4:4]	1
   	
   	MT6188_WriteByte(CW(24, 0), (MT6188_ReadCache(CW(24, 0)) & 0xDF) | 0x20);	//CW24_0	[5:5]	1

//RestoreIRQMask(save_irq_mask);        //Set IRQ to increase process speed       	   	
   }
  kal_sleep_task(1);// after set frequency need to wait 5ms
   
#if defined MT6188_DEBUG
   MT6188_DumpCtrlWord();
#endif
   return KAL_TRUE;
}                       //Chip_ID=4,MT6188_ECO_V4
else if (Chip_ID == 3)  //Chip_ID=3,MT6188_V3
{
    kal_uint16 Divider, N2, Ns, Target;
	kal_int32 CurFreqLSB,VCO_LSB=0,pf_LSB=0;
   	kal_uint8  TmpReg;
   	kal_uint16 WaitingCounter = 0,freq = 0,CurFreq_PLUS = 0;
kal_uint32 save_irq_mask;
#ifdef MT6188_DEBUG_DUMP_LOG
   kal_uint32 start_t, duration_t;
   kal_sprintf((void*)_dbg_str, "\nFMDrv_SetFreq(%d, %d);\n\0", CurFreq, HiLoInj);
   FS_Write(_file_handle, _dbg_str, strlen((void*)_dbg_str), &_data_written);
   start_t = video_get_current_time();
#endif   
   
   	
   	_current_frequency = CurFreq/FM_TUNER_GRID;
	freq = CurFreq;

   	VCO_LSB = CurFreq;
   // Disable PLL calibration	
   {	   	
  
   	/// DividerNumber = (CurFreq +- PresetIF) * 4 / RefClock 
   	if (HiLoInj == LO_INJECTION) 
   	{
     	 	VCO_LSB -= PRESET_IF;
   	} 
   	else 
   	{
      		VCO_LSB += PRESET_IF;
   	}
   		VCO_LSB *= 4000;

#if defined   REF_CLK_32K
   	VCO_LSB += 16384;    	// rounding
   	VCO_LSB /= 32768;		// 1/32768
   	
   	pf_LSB = 3562;		//456*64*4/32.768
//   	pf_LSB += 64;		//rounding
//   	pf_LSB /= 128;		//64*4/32768
#else
   	VCO_LSB += 16927;    	// rounding
   	VCO_LSB /= 33854;	// 384/13000000
   	
   	pf_LSB = 3448;		// 456*64*4/(13000/384)
//   	pf_LSB += 66;		//rounding
//   	pf_LSB /= 132;		//64*384*4/13000000
#endif

//save_irq_mask=SaveAndSetIRQMask();        //Set IRQ to increase process speed    

	MT6188_WriteByte(CW(24, 0), 0x00);	//Stop real-time calibration

   	MT6188_WriteByte(CW(6, 1), MT6188_ReadCache(CW(6, 1)) & 0xFE);	//Reset to default without calibration   
	
   }	
   	
   	MT6188_ReadByte(CW(7, 0), &TmpReg);
   	TmpReg &= 0x1F;
   	if(CurFreq <= 84000)
   		MT6188_WriteByte(CW(7, 0), TmpReg|0xE0);  	// CW7_0  [7:5]  7
   	else if(CurFreq > 84000 && CurFreq <= 92000)
   		MT6188_WriteByte(CW(7, 0), TmpReg|0x60);  	// CW7_0  [7:5]  3
   	else if(CurFreq > 92000 && CurFreq <=100000)
   		MT6188_WriteByte(CW(7, 0), TmpReg|0x20);  	// CW7_0  [7:5]  1	
 	else if(CurFreq > 100000)
 		MT6188_WriteByte(CW(7, 0), TmpReg|0x00);  	// CW7_0  [7:5]  0
   	

   	MT6188_WriteByte(CW(5, 1), (MT6188_ReadCache(CW(5, 1)) & 0xFB) | 0x04);	//CW5_1		[2:2]	1	
   	
   	MT6188_WriteByte(CW(7, 1), MT6188_ReadCache(CW(7, 1)) & 0xFC);		//CW7_1		[1:0]	0	
   	
   	MT6188_WriteByte(CW(5, 1), MT6188_ReadCache(CW(5, 1)) & 0x7F);		//CW5_1		[7:7]	0
   	
   	MT6188_WriteByte(CW(8, 0), MT6188_ReadCache(CW(8, 0)) & 0xF7);		//CW8_0		[3:3]	0	
   		   	
   	/// DividerNumber = (CurFreq +- PresetIF) * 4 / RefClock 
   	if (HiLoInj == LO_INJECTION) 
   	{
     	 	CurFreq -= PRESET_IF;
   	} 
   	else 
   	{
      		CurFreq += PRESET_IF;
   	}
   		CurFreq *= 4000;
   		CurFreqLSB = CurFreq;

#if defined   REF_CLK_32K
	CurFreq += 262144;	// rounding
	CurFreq >>= 19;		// 1/16*32768
   	CurFreqLSB += 16384;    // rounding
   	CurFreqLSB /= 32768;	// 1/32768
   	
#else
	CurFreq += 270834;	// rounding
	CurFreq /= 541667; 	// 24/13000000
   	CurFreqLSB += 16927;    // rounding
   	CurFreqLSB /= 33854;	// 384/13000000
   	
#endif
   	
   	MT6188_WriteByte(CW(12, 1), (MT6188_ReadCache(CW(12, 1)) & 0xFC) | 0x03);  	// CW12_1  [1:0]  0x03	turn-on digital clock

   	MT6188_WriteByte(CW(100, 1), (MT6188_ReadCache(CW(100, 1)) & 0xC0) | 0x10);  	// CW100_1 [5:0]  0x10	Set DAC at half range
   	
   	MT6188_WriteByte(CW(100, 0), MT6188_ReadCache(CW(100, 0)) & 0xF8);  		// CW100_0 [2:0]  0	Set DAC at half range
 
   	MT6188_ReadByte(CW(4, 0), &TmpReg);
   	TmpReg &= 0x7F;
   	if (HiLoInj == LO_INJECTION) 
   	{		
   		MT6188_WriteByte(CW(4, 0), TmpReg);  	// CW4_0    [7:7] 0:Lside
   	}
   	else
   	{
   		MT6188_WriteByte(CW(4, 0), TmpReg|0x80);  // CW4_0  [7:7] 1:Hside
   	}
   	

   	MT6188_WriteByte(CW(9, 1), MT6188_ReadCache(CW(9, 1)) & 0xF9);  		// CW9_1    [2:1]  0x00	turn-off AFC loop
   	
   	MT6188_WriteByte(CW(2, 1), MT6188_ReadCache(CW(2, 1)) & 0xC0);  		// CW2_1  [5:0]  0x00	Fix VCO divider value for calibration
   	
   	MT6188_WriteByte(CW(2, 0), 0x04);  		// CW2_0  [7:0]  0x04	Fix original calibration speed
   	
   	MT6188_WriteByte(CW(18, 0), (MT6188_ReadCache(CW(18, 0)) & 0x7F) | 0x80);  	// CW18_0  [7:7] 0x80	Fix original calibration speed
   	
   	MT6188_WriteByte(CW(3, 0), (MT6188_ReadCache(CW(3, 0)) & 0xEF) | 0x10);  	// CW3_0   [4:4] 0x10	Disconnect RFPLL
   	
   	MT6188_WriteByte(CW(3, 0), (MT6188_ReadCache(CW(3, 0)) & 0xF8) | 0x05);  	// CW3_0   [2:0] 0x05	Turn-on reference clock path
   	
   	MT6188_WriteByte(CW(7, 1), (MT6188_ReadCache(CW(7, 1)) & 0xFC) | 0x03);  	// CW7_1   [1:0] 0x03  Calibration standby

   	MT6188_WriteByte(CW(6, 0), MT6188_ReadCache(CW(6, 0)) & 0xFC);  		// CW6_0   [1:0] 0x00  select calibration window
   	
   	MT6188_WriteByte(CW(6, 1), 0x00);  		// CW6_1   [7:0] 0x00  select calibratio MSB of VCO

	MT6188_WriteByte(CW(25, 0), (kal_uint8)(CurFreq&0xFF));
   	MT6188_WriteByte(CW(25, 1), (kal_uint8)(CurFreq>>8));  	// CW25   Set calibration target frequency

   	MT6188_WriteByte(CW(1, 0), 0x00);  		// CW1_0   [7:0]  clear interrupt flag
   	
   	MT6188_WriteByte(CW(6, 1), (MT6188_ReadCache(CW(6, 1)) & 0xFE) | 0x01);  	// CW6_1   [0:0]  0x01	start calibration

#ifdef MT6188_timing_cal
#ifdef MT6188_DEBUG_DUMP_LOG
   duration_t = video_get_duration_ms(start_t);
   kal_sprintf((void*)_dbg_str, "  Set_freq1: %d ms.\n\0", duration_t);
   FS_Write(_file_handle, _dbg_str, strlen((void*)_dbg_str), &_data_written);
#endif   
#endif   

   	
   	/// Make sure that the counting is done
   	do {
   		
   	   	if ((WaitingCounter >> 8) == 1) {
   	      		return KAL_FALSE;
   	   }
   	   	if ((++WaitingCounter & 0x00FF) != 0) {
   	      		MT6188_ReadByte(CW(1, 0), &TmpReg);
   	   }
   	} while ((TmpReg & 0x01) == 0);
   		
   	MT6188_WriteByte(CW(1, 0), 0x00);    		// Reset CW1_0	Clear interrupt flag

#ifdef MT6188_timing_cal
#ifdef MT6188_DEBUG_DUMP_LOG
   duration_t = video_get_duration_ms(start_t);
   kal_sprintf((void*)_dbg_str, "  Set_freq2: %d ms.\n\0", duration_t);
   FS_Write(_file_handle, _dbg_str, strlen((void*)_dbg_str), &_data_written);
#endif   
#endif   

 
 	MT6188_ReadByte(CW(96, 0), &TmpReg);
   	if(TmpReg <= 63)
   		MT6188_WriteByte(CW(96, 0), TmpReg+1);	// 
   		

   	MT6188_WriteByte(CW(5, 1), MT6188_ReadCache(CW(5, 1)) & 0xFB);  		// CW5_1  [2:2]  0x00	fit LSB of VCO
	
	MT6188_WriteByte(CW(1, 0), 0x00);    		// Reset CW1_0	Clear interrupt flag 	
	
   	MT6188_WriteByte(CW(6, 0), (MT6188_ReadCache(CW(6, 0)) & 0xFC) | 0x01);  	// CW6_0  [1:0]  0x01	Select calibration window for LSB
   	
   	MT6188_WriteByte(CW(6, 1), 0x32);  		// CW6_1  [7:0]  0x32(50)  select calibration LSB1 of VCO
   	
   	MT6188_WriteByte(CW(25, 0), (kal_uint8)(CurFreqLSB&0xFF));
   	MT6188_WriteByte(CW(25, 1), (kal_uint8)(CurFreqLSB>>8));  	// CW25 [11:0]   Set calibration target frequency for VCO LSB

   	MT6188_WriteByte(CW(6, 1), (MT6188_ReadCache(CW(6, 1)) & 0xFE) | 0x01);  	// CW6_1  [0:0]  0x01	start calibration

#ifdef MT6188_timing_cal
#ifdef MT6188_DEBUG_DUMP_LOG
   duration_t = video_get_duration_ms(start_t);
   kal_sprintf((void*)_dbg_str, "  Set_freq3: %d ms.\n\0", duration_t);
   FS_Write(_file_handle, _dbg_str, strlen((void*)_dbg_str), &_data_written);
#endif   
#endif   

   	 	
   	/// Make sure that the counting is done
   	do {
   	   	if ((WaitingCounter >> 12) == 1) {
   	      		return KAL_FALSE;
   	   }
   	   	if ((++WaitingCounter & 0x0FFF) != 0) {
   	      		MT6188_ReadByte(CW(1, 0), &TmpReg);
   	   }
   	} while ((TmpReg & 0x01) == 0);
   		
   	MT6188_WriteByte(CW(1, 0), 0x00);    		// Reset CW1_0
 
   	MT6188_WriteByte(CW(6, 1), (MT6188_ReadCache(CW(6, 1)) & 0x0F) | 0x20);  	// CW6_1  [7:4]  0x20	Select calibrate as LSB2 of VCO
   	
   	MT6188_WriteByte(CW(6, 1), (MT6188_ReadCache(CW(6, 1)) & 0xFE) | 0x01);  	// CW6_1  [0:0]  0x01	start calibration

#ifdef MT6188_timing_cal
#ifdef MT6188_DEBUG_DUMP_LOG
   duration_t = video_get_duration_ms(start_t);
   kal_sprintf((void*)_dbg_str, "  Set_freq4: %d ms.\n\0", duration_t);
   FS_Write(_file_handle, _dbg_str, strlen((void*)_dbg_str), &_data_written);
#endif   
#endif   
 	  	
	/// Make sure that the counting is done
   	do {
   	   	if ((WaitingCounter >> 12) == 1) {
   	      		return KAL_FALSE;
   	   }
   	   	if ((++WaitingCounter & 0x0FFF) != 0) {
   	      		MT6188_ReadByte(CW(1, 0), &TmpReg);
   	   }
   	} while ((TmpReg & 0x01) == 0);
   		
   	MT6188_WriteByte(CW(1, 0), 0x00);    		// Reset CW1_0

#ifdef MT6188_timing_cal
#ifdef MT6188_DEBUG_DUMP_LOG
   duration_t = video_get_duration_ms(start_t);
   kal_sprintf((void*)_dbg_str, "  Set_freq5: %d ms.\n\0", duration_t);
   FS_Write(_file_handle, _dbg_str, strlen((void*)_dbg_str), &_data_written);
#endif   
#endif   
 
   	MT6188_WriteByte(CW(7, 1), MT6188_ReadCache(CW(7, 1)) & 0xFC);  		// CW7_1  [1:0]  0	reset to default without cali
   	
   	MT6188_WriteByte(CW(12, 1), MT6188_ReadCache(CW(12, 1)) & 0xFC);  		// CW12_1 [1:0]  0	reset to default without cali
   	
   	MT6188_WriteByte(CW(5, 1), (MT6188_ReadCache(CW(5, 1)) & 0xFB) | 0x04);  	// CW5_1  [2:2]  0x04  	reset to default without cali
   	
   	MT6188_WriteByte(CW(8, 0), (MT6188_ReadCache(CW(8, 0)) & 0xF7) | 0x08);  	// CW8_0  [3:3]  0x08  	reset to default without cali


   
   // Enable PLL calibration
   {

   	MT6188_WriteByte(CW(18, 0), MT6188_ReadCache(CW(18, 0)) & 0x7F);  	// CW18_0  [7:7] 0x00	Fix original calibration speed
   	
	//Reset to default without calibration
   	MT6188_WriteByte(CW(6, 1), MT6188_ReadCache(CW(6, 1)) & 0x0F);	//CW6_1	[7:4]	0   
	
	//Disable normal calibration
   	MT6188_WriteByte(CW(7, 1), MT6188_ReadCache(CW(7, 1)) & 0xFC);	//CW7_1 [1:0]	0
   	
	//Reset to default without calibration
   	MT6188_WriteByte(CW(5, 1), MT6188_ReadCache(CW(5, 1)) & 0xFB);	//CW5_1	[2:2]	0
   	
	//VCO 1-bit SAR threshold
   	MT6188_WriteByte(CW(24, 1), (MT6188_ReadCache(CW(24, 1)) & 0xF8) | 0x05);	//CW24_1	[2:0]	3
   	
   	MT6188_WriteByte(CW(25, 0), (kal_uint8)(VCO_LSB&0xFF));
   	MT6188_WriteByte(CW(25, 1), (kal_uint8)(VCO_LSB>>8));  	// CW25 [11:0]   Set calibration target frequency for VCO LSB


   	MT6188_WriteByte(CW(26, 0), (kal_uint8)(pf_LSB&0xFF));
   	MT6188_WriteByte(CW(26, 1), (kal_uint8)(pf_LSB>>8));  	// CW26 [11:0]   Set calibration target frequency for pf

   	MT6188_WriteByte(CW(10, 0), (MT6188_ReadCache(CW(10, 0)) & 0xFC) | 0x03);	//CW10_0	[1:0]	3
   	
	//Calibration standby
	MT6188_WriteByte(CW(6, 1), 0);	//CW6_1	[0:0]	0
   	MT6188_WriteByte(CW(6, 1), 1);	//CW6_1	[0:0]	1
   	
	//Set speed of real-time calibration  		
   	MT6188_WriteByte(CW(24, 0), MT6188_ReadCache(CW(24, 0)) & 0xF8);	//CW24_0	[2:0]	M=0
   	
	//Start VCO real-time calibration
   	MT6188_WriteByte(CW(24, 0), (MT6188_ReadCache(CW(24, 0)) & 0xEF) | 0x10);	//CW24_0	[4:4]	1
   	
	//Start PF real-time calibration
   	MT6188_WriteByte(CW(24, 0), (MT6188_ReadCache(CW(24, 0)) & 0xDF) | 0x20);	//CW24_0	[5:5]	1

//RestoreIRQMask(save_irq_mask);        //Set IRQ to increase process speed       	   	
   }
  kal_sleep_task(1);// after set frequency need to wait 5ms
   
#if defined MT6188_DEBUG
   MT6188_DumpCtrlWord();
#endif
   return KAL_TRUE;
}//Chip_ID=3,MT6188_V3
	return KAL_TRUE;
}//MT6188_SetFreq


/******************************************************************************
*  Verify set frequency is forward or backward in Radio frequency select
*
*  parameter-->CurFreq:set frequency
               SignalLevel:threshold value
*******************************************************************************/
static kal_int16 MT6188_HiLoInjectOptimizer(kal_int32 CurFreq, kal_int16 SignalLevel)
{
   kal_uint16 LevelHigh, LevelLow;

   if (MT6188_SetFreq(CurFreq + 2 * PRESET_IF, HI_INJECTION) == KAL_FALSE)
      return SET_FREQ_FAIL;
   LevelHigh = MT6188_GetCurRSSI();

   if (MT6188_SetFreq(CurFreq - 2 * PRESET_IF, HI_INJECTION) == KAL_FALSE)
      return SET_FREQ_FAIL;
   LevelLow = MT6188_GetCurRSSI();

   if (LevelHigh < SignalLevel && LevelLow < SignalLevel)
      return RSSI_CHECK_FAIL;
   if (LevelHigh > LevelLow)
      return LO_INJECTION;
   else
      return HI_INJECTION;
}



static void MT6188_SetVolumeLevel(kal_uint8 level) {
   /// Bug fix: Fix the mono/stereo gain issue
   const kal_uint8 prev_level = _current_level;
   ASSERT((level & 0x7F) < 13);

   _current_level = level;

   if ((KAL_FALSE == _is_fm_on) || _is_fm_mute) 
   {
      return;
   }

   if (prev_level == 0 && level != 0) 
   {
      MT6188_Mute(0);
   } 
   else if (prev_level != 0 && level == 0) 
   {
      /// This is a special case.
      /// If the level is set to zero, there is still sound leakage due to the HW design of MT6188.
      /// Thus, we use MT6188_Mute to force it keeps silent.
      /// However, the _is_fm_mute flag is not raised since it is not really a mute situation.
      MT6188_Mute(1);
      _is_fm_mute = 0;
      return;
   }
#if defined force_mono
mono_flag = 1;
#endif

   SerialCommInit();
   {
        kal_uint8 volume_count;
        kal_uint8 TmpReg;
//      MT6188_ReadByte(CW(18, 1), &TmpReg);
//      if (TmpReg & 0x80) {
         /// Stereo
//         MT6188_WriteByte(CW(16, 0), 64 | (level & 0x7F));
//         MT6188_WriteByte(CW(16, 1), 64 | (level & 0x7F));
//      } else {
         /// Mono
if (Chip_ID == 5)
{
#if !defined( MT6205B ) && !defined( MT6208 )  
#if defined (__MT6188CR_DISABLE_SOFTMUTE__)
    if(mono_flag == 1)
    {
         MT6188_WriteByte(CW(14, 0), 0x66);     // only mono
    }
    else if(mono_flag == 2)
    {
        MT6188_WriteByte(CW(14, 0), 0xE6);     // mono off and S_auto
    }
    else if (mono_flag == 3)
    {
        MT6188_WriteByte(CW(14, 0), 0x76);     // stereo + sblend_on
    }
    else if (mono_flag == 4)
    {
        MT6188_WriteByte(CW(14, 0), 0x36);     // stereo + sblend_off
    }
    
    else
    {
        MT6188_WriteByte(CW(14, 0), 0xE6);     // S_auto
    }
#else
    if(mono_flag == 1)
    {
         MT6188_WriteByte(CW(14, 0), 0x6E);     // only mono
    }
    else if(mono_flag == 2)
    {
        MT6188_WriteByte(CW(14, 0), 0xEE);     // mono off and S_auto
    }
    else if (mono_flag == 3)
    {
        MT6188_WriteByte(CW(14, 0), 0x7E);     // stereo + sblend_on
    }
    else if (mono_flag == 4)
    {
        MT6188_WriteByte(CW(14, 0), 0x3E);     // stereo + sblend_off
    }
    
    else
    {
        MT6188_WriteByte(CW(14, 0), 0xEE);     // S_auto
    }
#endif  //__MT6188CR_DISABLE_SOFTMUTE__  
    
    for(volume_count= 96; volume_count<=103; volume_count+=1)
    {      
         MT6188_WriteByte(CW(16, 0), volume_count);//108);
         MT6188_WriteByte(CW(16, 1), volume_count);//108);
    }
#else	//6205 or 6208
#if defined (__MT6188CR_DISABLE_SOFTMUTE__)
    if(mono_flag == 1)
    {
         MT6188_WriteByte(CW(14, 0), 0x66);     // only mono
    }
    else if(mono_flag == 2)
    {
        MT6188_WriteByte(CW(14, 0), 0xE6);     // mono off and S_auto
    }
    else if (mono_flag == 3)
    {
        MT6188_WriteByte(CW(14, 0), 0x76);     // stereo + sblend_on
    }
    else if (mono_flag == 4)
    {
        MT6188_WriteByte(CW(14, 0), 0x36);     // stereo + sblend_off
    }
    
    else
    {
        MT6188_WriteByte(CW(14, 0), 0xE6);     // S_auto
    }
#else
    if(mono_flag == 1)
    {
         MT6188_WriteByte(CW(14, 0), 0x6E);     // only mono
    }
    else if(mono_flag == 2)
    {
        MT6188_WriteByte(CW(14, 0), 0xEE);     // mono off and S_auto
    }
    else if (mono_flag == 3)
    {
        MT6188_WriteByte(CW(14, 0), 0x7E);     // stereo + sblend_on
    }
    else if (mono_flag == 4)
    {
        MT6188_WriteByte(CW(14, 0), 0x3E);     // stereo + sblend_off
    }
    
    else
    {
        MT6188_WriteByte(CW(14, 0), 0xEE);     // S_auto
    }
#endif  //__MT6188CR_DISABLE_SOFTMUTE__  
	     MT6188_WriteByte(CW(16, 0), 96 | (level & 0x7F));
         MT6188_WriteByte(CW(16, 1), 96 | (level & 0x7F));

#endif
}   //end Chip_ID = 5
else    //start Chip_ID = 4/3
{
    #if !defined( MT6205B ) && !defined( MT6208 )  
    if(mono_flag == 1)
    {
         MT6188_WriteByte(CW(14, 0), 0x66);     // only mono
    }
    else if(mono_flag == 2)
    {
        MT6188_WriteByte(CW(14, 0), 0xE6);     // mono off and S_auto
    }
    else if (mono_flag == 3)
    {
        MT6188_WriteByte(CW(14, 0), 0x76);     // stereo + sblend_on
    }
    else if (mono_flag == 4)
    {
        MT6188_WriteByte(CW(14, 0), 0x36);     // stereo + sblend_off
    }
    
    else
    {
        MT6188_WriteByte(CW(14, 0), 0xE6);     // S_auto
    }
    
    for(volume_count= 96; volume_count<=103; volume_count+=1)
    {      
         MT6188_WriteByte(CW(16, 0), volume_count);//108);
         MT6188_WriteByte(CW(16, 1), volume_count);//108);
    }
#else
    if(mono_flag == 1)
    {
         MT6188_WriteByte(CW(14, 0), 0x66);     // only mono
    }
    else if(mono_flag == 2)
    {
        MT6188_WriteByte(CW(14, 0), 0xE6);     // mono off and S_auto
    }
    else if (mono_flag == 3)
    {
        MT6188_WriteByte(CW(14, 0), 0x76);     // stereo + sblend_on
    }
    else if (mono_flag == 4)
    {
        MT6188_WriteByte(CW(14, 0), 0x36);     // stereo + sblend_off
    }
    
    else
    {
        MT6188_WriteByte(CW(14, 0), 0xE6);     // S_auto
    }
	     MT6188_WriteByte(CW(16, 0), 96 | (level & 0x7F));
         MT6188_WriteByte(CW(16, 1), 96 | (level & 0x7F));

#endif
}   //end Chip_ID = 4/3

#ifdef MT6188_DEBUG_DUMP_LOG
   MT6188_ReadByte(CW(14, 0), &TmpReg);
   kal_sprintf((void*)_dbg_str, "   mono_flag: %d ms.\n CW14_0 = %d \n\0", mono_flag, TmpReg);
   FS_Write(_file_handle, _dbg_str, strlen((void*)_dbg_str), &_data_written);
#endif   

		//close A switch(bit0) and open B switch(bit2)<--fix 1uF issue
   MT6188_ReadByte(CW( 34, 0), &TmpReg);	
   TmpReg &= 0xFA;
   MT6188_WriteByte(CW( 34, 0), TmpReg|0x01); 
   
   MT6188_WriteByte(CW(21, 0), (MT6188_ReadCache(CW(21, 0)) & 0xFE));//disable auto AGC
   
   }  
   SerialCommRelease();

}

static void MT6188_Mute(kal_uint8 mute) 
{
   kal_uint8 TmpReg;

   _is_fm_mute = (kal_bool)mute;
   if (KAL_FALSE == _is_fm_on)
      return;

   if (_current_level == 0 && mute == 0)
      return;

   SerialCommInit();
/*
   TmpReg = MT6188_ReadCache(CW(14, 1)) & 0xF8;
#ifndef USE_MT6188_AMP
   TmpReg |= 0x03;
#endif
   if (mute != 0)
      TmpReg |= 0x07;
   MT6188_WriteByte(CW(14, 1), TmpReg);
*/
   if (mute == 0) 
   {
      MT6188_SetVolumeLevel(_current_level);
   } 
   else 
   {
      MT6188_WriteByte(CW(16, 0), 0);
      MT6188_WriteByte(CW(16, 1), 0);
   }
   SerialCommRelease();
}


/// level ranges from 0 to 12
void FMDrv_SetVolumeLevel(kal_uint8 level)
{
  #ifdef MT6188_DEBUG_DUMP_LOG
   kal_sprintf((void*)_dbg_str, "\nFMDrv_SetVolumeLevel(%d);\n\0", level);
   FS_Write(_file_handle, _dbg_str, strlen((void*)_dbg_str), &_data_written);
#endif
   MT6188_SetVolumeLevel(level);
}

void FMDrv_Mute(kal_uint8 mute)
{
  #ifdef MT6188_DEBUG_DUMP_LOG
   kal_sprintf((void*)_dbg_str, "\nFMDrv_Mute(%d);\n\0", mute);
   FS_Write(_file_handle, _dbg_str, strlen((void*)_dbg_str), &_data_written);
#endif
   MT6188_Mute(mute);
}


/*************************************************************
*  Radio power on Reset
*
*************************************************************/
void FMDrv_PowerOnReset(void)
{
   kal_uint8 fail_count;
   kal_uint8 tmp_reg,pp=0;
   kal_int16 i;

#ifdef MT6188_DEBUG_DUMP_LOG
   kal_uint32 start_t, duration_t;
   _file_handle = FS_Open((const WCHAR *)_file_name, FS_CREATE);
   kal_sprintf((void*)_dbg_str, "\nFMDrv_PowerOnReset();\n\0");
   FS_Write(_file_handle, _dbg_str, strlen((void*)_dbg_str), &_data_written);
   start_t = video_get_current_time();
#endif

#ifdef MT6188_DEBUG_DUMP_LOG
    _file_list = FS_Open((const WCHAR *)_channel_name, FS_CREATE);
   kal_sprintf((void*)_list_str, "\nchannel list;\n\0");
   FS_Write(_file_list, _list_str, strlen((void*)_list_str), &_list_written);
#endif   
   
   SerialCommInit();
   SerialCommCryClkOn();

    MT6188_ReadByte(CW(0, 1), &Chip_ID);
    Chip_ID &= 0x07;
/// Power On
    if (Chip_ID == 5)
    {        
       for (i=0; i<POWER_ON_COMMAND_COUNT_V5; i++) 
       {
          MT6188_WriteByte(PowerOnSetting_V5[i].addr, PowerOnSetting_V5[i].value);
       }
    }
    else if (Chip_ID == 4)
    {
       for (i=0; i<POWER_ON_COMMAND_COUNT_V4; i++) 
       {
          MT6188_WriteByte(PowerOnSetting_V4[i].addr, PowerOnSetting_V4[i].value);
       }
    }
    else if (Chip_ID == 3)
    {
       for (i=0; i<POWER_ON_COMMAND_COUNT_V3; i++) 
       {
          MT6188_WriteByte(PowerOnSetting_V3[i].addr, PowerOnSetting_V3[i].value);
       }
    } //end Chip_ID select
    else
    {
    	ASSERT(0);
    }	//end Chip_ID select
 
#if defined MT6188_DEBUG
   MT6188_DumpCtrlWord();
#endif
   
/// Calibrate Preset
    if (Chip_ID == 5)
    {
        fail_count = 0;
        for (i=0; i<CAL_PRESET_COUNT_V5; i++)
        {
        	MT6188_ReadByte(CalPresetProcess_V5[i].addr, &tmp_reg);
        	tmp_reg &= CalPresetProcess_V5[i].and;
        	tmp_reg |= CalPresetProcess_V5[i].or;
        	if (KAL_FALSE == MT6188_WriteByte(CalPresetProcess_V5[i].addr, tmp_reg)) 
        	{
        		if (++fail_count == CALIBRATION_FAIL_COUNT_THRESHOLD)
        		{
        			ASSERT(0);
        		}
        		i = 0;
        	}
        }
    }
    else
    {
        fail_count = 0;
        for (i=0; i<CAL_PRESET_COUNT; i++)
        {
        	MT6188_ReadByte(CalPresetProcess[i].addr, &tmp_reg);
        	tmp_reg &= CalPresetProcess[i].and;
        	tmp_reg |= CalPresetProcess[i].or;
        	if (KAL_FALSE == MT6188_WriteByte(CalPresetProcess[i].addr, tmp_reg)) 
        	{
        		if (++fail_count == CALIBRATION_FAIL_COUNT_THRESHOLD)
        		{
        			ASSERT(0);
        		}
        		i = 0;
        	}
        }
    }

#if defined MT6188_DEBUG
   MT6188_DumpCtrlWord();
#endif
   
   /// Calibrate PLL
   fail_count = 0;
   for (i=0; i<CAL_PLL_COMMAND_COUNT; i++) 
   {
      MT6188_ReadByte(CalPLLProcess[i].addr, &tmp_reg);
      tmp_reg &= CalPLLProcess[i].and;
      tmp_reg |= CalPLLProcess[i].or;
      if (KAL_FALSE == MT6188_WriteByte(CalPLLProcess[i].addr, tmp_reg)) 
      {
         if (++fail_count == CALIBRATION_FAIL_COUNT_THRESHOLD)
         {
            ASSERT(0);
         }
         i = 0;
      }
   }

#if defined MT6188_DEBUG
   MT6188_DumpCtrlWord();
#endif

   /// Calibrate demod I
   fail_count = 0;
   for (i=0; i<CAL_DEMOD_I_COMMAND_COUNT; i++) 
   {
      MT6188_ReadByte(CalDemodIProcess[i].addr, &tmp_reg);
      tmp_reg &= CalDemodIProcess[i].and;
      tmp_reg |= CalDemodIProcess[i].or;
      if (KAL_FALSE == MT6188_WriteByte(CalDemodIProcess[i].addr, tmp_reg)) 
      {
         if (++fail_count == CALIBRATION_FAIL_COUNT_THRESHOLD)
         {
            ASSERT(0);
         }
         i = 0;
      }
   }

#if defined MT6188_DEBUG
   MT6188_DumpCtrlWord();
#endif

    if (Chip_ID == 5)
    {
        /// Channel filter
        fail_count = 0;
        for (i=0; i<CHANNEL_FILTER_COUNT; i++) 
        {
           MT6188_ReadByte(ChannelFilter[i].addr, &tmp_reg);
           tmp_reg &= ChannelFilter[i].and;
           tmp_reg |= ChannelFilter[i].or;
           if (KAL_FALSE == MT6188_WriteByte(ChannelFilter[i].addr, tmp_reg)) 
           {
              if (++fail_count == CALIBRATION_FAIL_COUNT_THRESHOLD)
              {
                 ASSERT(0);
              }
              i = 0;
           }
        }

#if defined MT6188_DEBUG
   MT6188_DumpCtrlWord();
#endif

        /// MUXOFF_MN
        fail_count = 0;
        for (i=0; i<MUXOFF_MN_COUNT; i++) 
        {
           MT6188_ReadByte(MuxOff_MN[i].addr, &tmp_reg);
           tmp_reg &= MuxOff_MN[i].and;
           tmp_reg |= MuxOff_MN[i].or;
           if (KAL_FALSE == MT6188_WriteByte(MuxOff_MN[i].addr, tmp_reg)) 
           {
              if (++fail_count == CALIBRATION_FAIL_COUNT_THRESHOLD)
              {
                 ASSERT(0);
              }
              i = 0;
           }
        }

#if defined MT6188_DEBUG
   MT6188_DumpCtrlWord();
#endif

        /// MUXOFF_ST
        fail_count = 0;
        for (i=0; i<MUXOFF_ST_COUNT; i++) 
        {
           MT6188_ReadByte(MuxOff_ST[i].addr, &tmp_reg);
           tmp_reg &= MuxOff_ST[i].and;
           tmp_reg |= MuxOff_ST[i].or;
           if (KAL_FALSE == MT6188_WriteByte(MuxOff_ST[i].addr, tmp_reg)) 
           {
              if (++fail_count == CALIBRATION_FAIL_COUNT_THRESHOLD)
              {
                 ASSERT(0);
              }
              i = 0;
           }
        }

#if defined MT6188_DEBUG
   MT6188_DumpCtrlWord();
#endif

        /// PFOSC_560K
        fail_count = 0;
        for (i=0; i<PFOSC_560K_COUNT; i++) 
        {
           MT6188_ReadByte(Pfosc_560K[i].addr, &tmp_reg);
           tmp_reg &= Pfosc_560K[i].and;
           tmp_reg |= Pfosc_560K[i].or;
           if (KAL_FALSE == MT6188_WriteByte(Pfosc_560K[i].addr, tmp_reg)) 
           {
              if (++fail_count == CALIBRATION_FAIL_COUNT_THRESHOLD)
              {
                 ASSERT(0);
              }
              i = 0;
           }
        }

#if defined MT6188_DEBUG
   MT6188_DumpCtrlWord();
#endif

        /// SM_CAL_OFFSET
        fail_count = 0;
        for (i=0; i<SM_CAL_OFFSET_COUNT; i++) 
        {
           MT6188_ReadByte(SM_Cal_Offset[i].addr, &tmp_reg);
           tmp_reg &= SM_Cal_Offset[i].and;
           tmp_reg |= SM_Cal_Offset[i].or;
           if (KAL_FALSE == MT6188_WriteByte(SM_Cal_Offset[i].addr, tmp_reg)) 
           {
              if (++fail_count == CALIBRATION_FAIL_COUNT_THRESHOLD)
              {
                 ASSERT(0);
              }
              i = 0;
           }
        }

#if defined MT6188_DEBUG
   MT6188_DumpCtrlWord();
#endif

        /// SM_CAL_SLOPE
        fail_count = 0;
        for (i=0; i<SM_CAL_SLOPE_COUNT; i++) 
        {
           MT6188_ReadByte(SM_Cal_Slope[i].addr, &tmp_reg);
           tmp_reg &= SM_Cal_Slope[i].and;
           tmp_reg |= SM_Cal_Slope[i].or;
           if (KAL_FALSE == MT6188_WriteByte(SM_Cal_Slope[i].addr, tmp_reg)) 
           {
              if (++fail_count == CALIBRATION_FAIL_COUNT_THRESHOLD)
              {
                 ASSERT(0);
              }
              i = 0;
           }
        }

#if defined MT6188_DEBUG
   MT6188_DumpCtrlWord();
#endif

        /// AAMP1_LCHOFF_MN
        fail_count = 0;
        for (i=0; i<AAMP1_LCHOFF_MN_COUNT; i++) 
        {
           MT6188_ReadByte(Aamp1_Lchoff_MN[i].addr, &tmp_reg);
           tmp_reg &= Aamp1_Lchoff_MN[i].and;
           tmp_reg |= Aamp1_Lchoff_MN[i].or;
           if (KAL_FALSE == MT6188_WriteByte(Aamp1_Lchoff_MN[i].addr, tmp_reg)) 
           {
              if (++fail_count == CALIBRATION_FAIL_COUNT_THRESHOLD)
              {
                 ASSERT(0);
              }
              i = 0;
           }
        }

#if defined MT6188_DEBUG
   MT6188_DumpCtrlWord();
#endif

        /// AAMP1_RCHOFF_MN
        fail_count = 0;
        for (i=0; i<AAMP1_RCHOFF_MN_COUNT; i++) 
        {
           MT6188_ReadByte(Aamp1_Rchoff_MN[i].addr, &tmp_reg);
           tmp_reg &= Aamp1_Rchoff_MN[i].and;
           tmp_reg |= Aamp1_Rchoff_MN[i].or;
           if (KAL_FALSE == MT6188_WriteByte(Aamp1_Rchoff_MN[i].addr, tmp_reg)) 
           {
              if (++fail_count == CALIBRATION_FAIL_COUNT_THRESHOLD)
              {
                 ASSERT(0);
              }
              i = 0;
           }
        }

        MT6188_ReadByte(CW( 30, 0), &tmp_reg);
        MT6188_WriteByte(CW( 31, 0), tmp_reg);
        
        MT6188_ReadByte(CW( 30, 1), &tmp_reg);
        MT6188_WriteByte(CW( 31, 1), tmp_reg);

#if defined MT6188_DEBUG
   MT6188_DumpCtrlWord();
#endif
    }
    
/// Calibrate Pfosc
    if (Chip_ID ==5)
    {
            kal_uint8 k;
           fail_count = 0;
           for (i=0; i<CAL_PFOSC_COMMAND_COUNT_V5; i++) 
           {
              MT6188_ReadByte(CalPfoscProcess_V5[i].addr, &tmp_reg);
              tmp_reg &= CalPfoscProcess_V5[i].and;
              tmp_reg |= CalPfoscProcess_V5[i].or;
              if (KAL_FALSE == MT6188_WriteByte(CalPfoscProcess_V5[i].addr, tmp_reg)) 
              {
                 if (++fail_count == CALIBRATION_FAIL_COUNT_THRESHOLD)
                 {
                    ASSERT(0);
                 }
                 i = 0;
              }
           }
        
           MT6188_ReadByte(CW( 3, 0), &tmp_reg);
           tmp_reg &= 0xEF;
           MT6188_WriteByte(CW( 3, 0), tmp_reg);
             
    }
    else    //start Chip_ID == 3/4
    {
           kal_uint8 k;
           fail_count = 0;
           for (i=0; i<CAL_PFOSC_COMMAND_COUNT; i++) 
           {
              MT6188_ReadByte(CalPfoscProcess[i].addr, &tmp_reg);
              tmp_reg &= CalPfoscProcess[i].and;
              tmp_reg |= CalPfoscProcess[i].or;
              if (KAL_FALSE == MT6188_WriteByte(CalPfoscProcess[i].addr, tmp_reg)) 
              {
                 if (++fail_count == CALIBRATION_FAIL_COUNT_THRESHOLD)
                 {
                    ASSERT(0);
                 }
                 i = 0;
              }
           }
        
           MT6188_ReadByte(CW( 3, 0), &tmp_reg);
           tmp_reg &= 0xEF;
           MT6188_WriteByte(CW( 3, 0), tmp_reg);
        if (Chip_ID == 4)
        {      
              MT6188_ReadByte(CW( 98, 1), &tmp_reg);	//CW98_1
            if(tmp_reg <= 61)
            {
              	MT6188_WriteByte(CW(4, 0), (MT6188_ReadCache(CW(4, 0)) & 0x8F) | 0x70);//cw4_0 [6:4] 7 	
	        }
	        else if(tmp_reg >= 62 && tmp_reg <= 65)
	        {
	        	MT6188_WriteByte(CW(4, 0), (MT6188_ReadCache(CW(4, 0)) & 0x8F) | 0x60);//cw4_0 [6:4] 6	
	        }
	        else if(tmp_reg >= 66 && tmp_reg <= 69)
	        {
	        	MT6188_WriteByte(CW(4, 0), (MT6188_ReadCache(CW(4, 0)) & 0x8F) | 0x50);//cw4_0 [6:4] 5	
	        }
	        else if(tmp_reg >= 70 && tmp_reg <= 73)
	        {
	        	MT6188_WriteByte(CW(4, 0), (MT6188_ReadCache(CW(4, 0)) & 0x8F) | 0x40);//cw4_0 [6:4] 4 	
	        }
	        else if(tmp_reg >= 74 && tmp_reg <= 79)
	        {
	        	MT6188_WriteByte(CW(4, 0), (MT6188_ReadCache(CW(4, 0)) & 0x8F) | 0x30);//cw4_0 [6:4] 3 	
	        }
	        else if(tmp_reg >= 80 && tmp_reg <= 83)
	        {
	        	MT6188_WriteByte(CW(4, 0), (MT6188_ReadCache(CW(4, 0)) & 0x8F) | 0x20);//cw4_0 [6:4] 2 	
	        }	
	        else if(tmp_reg >= 84 && tmp_reg <= 87)
	        {
	        	MT6188_WriteByte(CW(4, 0), (MT6188_ReadCache(CW(4, 0)) & 0x8F) | 0x10);//cw4_0 [6:4] 1 	
	        }
	        else if(tmp_reg >= 88)
	        {
	        	MT6188_WriteByte(CW(4, 0), (MT6188_ReadCache(CW(4, 0)) & 0x8F));//cw4_0 [6:4] 0 	
	        }
        }       //Chip_ID=4,MT6188_ECO
   }    //end Chip_ID == 3/4   
#if defined MT6188_DEBUG
   MT6188_DumpCtrlWord();
#endif

//Delay calibration
    if (Chip_ID == 5)
    {
        fail_count = 0;
      for (i=0; i<CAL_DelayCalibration_COMMAND_COUNT_V5; i++) 
      {
         MT6188_ReadByte(CalDelayCalibrationProcess_V5[i].addr, &tmp_reg);
         tmp_reg &= CalDelayCalibrationProcess_V5[i].and;
         tmp_reg |= CalDelayCalibrationProcess_V5[i].or;
         if (KAL_FALSE == MT6188_WriteByte(CalDelayCalibrationProcess_V5[i].addr, tmp_reg)) 
         {
            if (++fail_count == CALIBRATION_FAIL_COUNT_THRESHOLD)
            {
               ASSERT(0);
            }
            i = 0;
         }
      }
   	
         	
   //RSSI calibration
//      	MT6188_WriteByte(CW( 5, 0), 0);
	MT6188_ReadByte(CW( 5, 0), &tmp_reg);	//CW5_0		[1:1]=0, 
	tmp_reg &= 0xFD;
      	MT6188_WriteByte(CW( 5, 0), (tmp_reg |= 0x00));
      	
      	kal_sleep_task(11);//wait 50ms
      	
      	MT6188_ReadByte(CW( 4, 1), &tmp_reg);	//CW4_1		[4:0]read RSSI value
      	tmp_reg &= 0x1F;
      	pp = tmp_reg;
      	MT6188_ReadByte(CW( 13, 1), &tmp_reg);	//CW13_1	
      	tmp_reg &= 0xE1;
      	MT6188_WriteByte(CW( 13, 1), (tmp_reg |= (pp << 1)));//   	write to cw(13, 1) [4:1]
      	
        MT6188_ReadByte(CW( 5, 0), &tmp_reg);	//CW5_0		[1:1]=1, 
        tmp_reg &= 0xFD;                                                                                
        MT6188_WriteByte(CW( 5, 0), (tmp_reg |= 0x02));                                                 
            
   // Disable Calibration
      MT6188_ReadByte(CW( 14, 0), &tmp_reg);	//CW14_0	[7:7]1,S_AUTO,STEREO
      tmp_reg &= 0x7F;
      MT6188_WriteByte(CW( 14, 0), (tmp_reg |= 0x80));
      
      MT6188_ReadByte(CW( 10, 1), &tmp_reg);	//CW10_1	[5:5]0,FRCKTEST
      tmp_reg &= 0xDF;
      MT6188_WriteByte(CW( 10, 1), tmp_reg);
      
      MT6188_ReadByte(CW( 11, 1), &tmp_reg);	//CW11_1	[5:4]0,FR_TEST[1:0]
      tmp_reg &= 0xCF;
      MT6188_WriteByte(CW( 11, 1), tmp_reg);
   
   	  MT6188_WriteByte(CW( 14, 0), 0xFE);	//force S_auto/S_mute/S_blend = KAL_TRUE
   	 
#if defined MT6188_DEBUG
   MT6188_DumpCtrlWord();
#endif
    }   //end Chip_ID = 5
    else if (Chip_ID == 4)
    {
       
       	fail_count = 0;
          for (i=0; i<CAL_DelayCalibration_COMMAND_COUNT; i++) 
          {
             MT6188_ReadByte(CalDelayCalibrationProcess[i].addr, &tmp_reg);
             tmp_reg &= CalDelayCalibrationProcess[i].and;
             tmp_reg |= CalDelayCalibrationProcess[i].or;
             if (KAL_FALSE == MT6188_WriteByte(CalDelayCalibrationProcess[i].addr, tmp_reg)) 
             {
                if (++fail_count == CALIBRATION_FAIL_COUNT_THRESHOLD)
                {
                   ASSERT(0);
                }
                i = 0;
             }
          }
       	
             	
       //RSSI calibration
          	MT6188_WriteByte(CW( 5, 0), 0);
          	
          	kal_sleep_task(11);//wait 50ms
          	
          	MT6188_ReadByte(CW( 4, 1), &tmp_reg);	//CW4_1		[4:0]read RSSI value
          	tmp_reg &= 0x1F;
          	pp = tmp_reg;
          	MT6188_ReadByte(CW( 13, 1), &tmp_reg);	//CW13_1	
          	tmp_reg &= 0xE1;
          	MT6188_WriteByte(CW( 13, 1), (tmp_reg |= (pp << 1)));//   	write to cw(13, 1) [4:1]
          	
          	MT6188_ReadByte(CW( 5, 0), &tmp_reg);	//CW5_0		[7:0]=175, Enable delay calibration loop
          	tmp_reg &= 0x00;
          	MT6188_WriteByte(CW( 5, 0), (tmp_reg |= 0xAF));//0xAF off_AGC joetsou
                
       // Disable Calibration
          MT6188_ReadByte(CW( 14, 0), &tmp_reg);	//CW14_0	[7:7]1,S_AUTO,STEREO
          tmp_reg &= 0x7F;
          MT6188_WriteByte(CW( 14, 0), (tmp_reg |= 0x80));
          
          MT6188_ReadByte(CW( 10, 1), &tmp_reg);	//CW10_1	[5:5]0,FRCKTEST
          tmp_reg &= 0xDF;
          MT6188_WriteByte(CW( 10, 1), tmp_reg);
          
          MT6188_ReadByte(CW( 11, 1), &tmp_reg);	//CW11_1	[5:4]0,FR_TEST[1:0]
          tmp_reg &= 0xCF;
          MT6188_WriteByte(CW( 11, 1), tmp_reg);
   
  	MT6188_ReadByte(CW( 101, 1), &tmp_reg);	
          MT6188_WriteByte(CW( 101, 1), (tmp_reg-5));  

#if defined MT6188_DEBUG
   MT6188_DumpCtrlWord();
#endif
    }       //end Chip_ID=4,MT6188_ECO

   SerialCommRelease();
   _is_fm_on = KAL_TRUE;
   _current_frequency = -1;
   _rssi_threshold = FM_RADIO_INPUT_LEVEL_THRESHOLD;
   if(RSSI_offset != 0)
    		_rssi_threshold = RSSI_offset; 
     
   
#ifdef MT6188_DEBUG_DUMP_LOG
   duration_t = video_get_duration_ms(start_t);
   kal_sprintf((void*)_dbg_str, "   time cost: %d ms.\n Chip_ID = %d \n\0", duration_t, Chip_ID);
   FS_Write(_file_handle, _dbg_str, strlen((void*)_dbg_str), &_data_written);
#endif

}


/*****************************************************************
*  Radio power off
*
*****************************************************************/
void FMDrv_PowerOffProc(void)
{
   kal_int16 i;
   kal_uint8 tmp_reg;
   
#ifdef MT6188_DEBUG_DUMP_LOG
   if (_file_handle == 0)
      ASSERT(0);
   kal_sprintf((void*)_dbg_str, "\nFMDrv_PowerOffProc();\n\0");
   FS_Write(_file_handle, _dbg_str, strlen((void*)_dbg_str), &_data_written);
   FS_Close(_file_handle);
   _file_handle = 0;
#endif   

#ifdef MT6188_DEBUG_DUMP_LOG
   if (_file_list == 0)
      ASSERT(0);
   kal_sprintf((void*)_list_str, "\nFMDrv_PowerOffProc();\n\0");
   FS_Write(_file_list, _list_str, strlen((void*)_list_str), &_list_written);
   FS_Close(_file_list);
   _file_list = 0;
#endif   
   
   
#ifdef USE_MT6188_AMP
   SwitchExtFMPath(0);
#endif

   SerialCommInit();
   for (i=0; i<POWER_OFF_COMMAND_COUNT; i++) 
   {
      MT6188_ReadByte(PowerOffProc[i].addr, &tmp_reg);
      tmp_reg &= PowerOffProc[i].and;
      tmp_reg |= PowerOffProc[i].or;
      MT6188_WriteByte(PowerOffProc[i].addr, tmp_reg);
   }
   SerialCommCryClkOff();
   SerialCommRelease();
   _is_fm_on = KAL_FALSE;
   _current_frequency = -1;
   index_count=0;
}


/*********************************************************************
*  Radio set frquency
*
*  parameter-->curf:setting frequency value
                    input value: 875 - 1080 ( 87.5 MHz - 108.0 MHz)
*********************************************************************/
void FMDrv_SetFreq( kal_int16 curf )  /* input value: 875 - 1080 ( 87.5 MHz - 108.0 MHz)*/
{
   kal_int16 HiLo;
   kal_int32 curFreq = (kal_int32)curf * FM_TUNER_GRID;
      
#ifdef MT6188_DEBUG_DUMP_LOG
   kal_uint32 start_t, duration_t;
   kal_sprintf((void*)_dbg_str, "\nFMDrv_SetFreq(%d);\n\0", curf);
   FS_Write(_file_handle, _dbg_str, strlen((void*)_dbg_str), &_data_written);
   start_t = video_get_current_time();
#endif      
   if (_is_fm_on != KAL_TRUE)
	    FMDrv_PowerOnReset();
    
   FMDrv_Mute(1);
   SerialCommInit();
   HiLo = MT6188_HiLoInjectOptimizer(curFreq, 0);
//HiLo = HI_INJECTION;
   if (KAL_FALSE == MT6188_SetFreq(curFreq, HiLo)) 
   {
      ASSERT(0);
   }
   SerialCommRelease();
   FMDrv_Mute(0);
    
#ifdef USE_MT6188_AMP
   SwitchExtFMPath(1);
#endif

#ifdef MT6188_DEBUG_DUMP_LOG
   duration_t = video_get_duration_ms(start_t);
   kal_sprintf((void*)_dbg_str, "   time cost: %d ms.\n\0", duration_t);
   FS_Write(_file_handle, _dbg_str, strlen((void*)_dbg_str), &_data_written);
#endif

}


/**********************************************************************
*  Get really signal level in current frequency
*
*  parameter-->curf:frequency value of radio now
**********************************************************************/
kal_uint16 FMDrv_GetSigLvl( kal_int16 curf )
{
   kal_uint16 rssi;
   
#ifdef MT6188_DEBUG_DUMP_LOG
   kal_uint32 start_t, duration_t;
   kal_sprintf((void*)_dbg_str, "\nFMDrv_GetSigLvl(%d);\n\0", curf);
   FS_Write(_file_handle, _dbg_str, strlen((void*)_dbg_str), &_data_written);
   start_t = video_get_current_time();
#endif   
   
   if (curf != _current_frequency)
   	FMDrv_SetFreq( curf );
   SerialCommInit();
   rssi = MT6188_GetCurRSSI();
   SerialCommRelease();
   
#ifdef MT6188_DEBUG_DUMP_LOG
   duration_t = video_get_duration_ms(start_t);
   kal_sprintf((void*)_dbg_str, "   time cost: %d ms.\n   RSSI returned: %d.\n\0", duration_t, rssi);
   FS_Write(_file_handle, _dbg_str, strlen((void*)_dbg_str), &_data_written);
#endif

   return rssi;
}


/**********************************************************************
*  Get really IF count in current frequency
*
*  parameter-->curf:frequency value of radio now
**********************************************************************/
kal_uint16 FMDrv_GetIF( kal_int16 curf)
{
   kal_uint16 IF;

#ifdef MT6188_DEBUG_DUMP_LOG
   kal_uint32 start_t, duration_t;
   kal_sprintf((void*)_dbg_str, "\nFMDrv_GetIF(%d);\n\0", curf);
   FS_Write(_file_handle, _dbg_str, strlen((void*)_dbg_str), &_data_written);
   start_t = video_get_current_time();
#endif

   if (curf != _current_frequency)
      FMDrv_SetFreq( curf );
   SerialCommInit();
   IF = MT6188_GetCurIF();
   SerialCommRelease();

#ifdef MT6188_DEBUG_DUMP_LOG
   duration_t = video_get_duration_ms(start_t);
   kal_sprintf((void*)_dbg_str, "   time cost: %d ms.\n   IF returned: %d.\n\0", duration_t, IF );
   FS_Write(_file_handle, _dbg_str, strlen((void*)_dbg_str), &_data_written);
#endif
   return IF;
}

void FMDrv_EvaluateRSSIThreshold(void) {
#if 0
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
#ifdef MT6188_DEBUG_DUMP_LOG
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
#endif
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
#ifdef MT6188_DEBUG_DUMP_LOG
/* under construction !*/
/* under construction !*/
#endif
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
#ifdef MT6188_DEBUG_DUMP_LOG
/* under construction !*/
/* under construction !*/
/* under construction !*/
#endif
/* under construction !*/
#endif
}


/**********************************************************************
*  Radio valid station,used in auto search frequency to verify 
*  valid positon
*
*  parameter-->freq: frequency
               signalv1:signal level range is 0 ~ 15 
               is_step_up:return value(Reserved)
**********************************************************************/
kal_uint8 FMDrv_ValidStop(kal_int16 freq, kal_int8 signalvl, kal_bool is_step_up)
{
   kal_int32 targetFreq;
   kal_int16 IF_cnt;
   kal_int16 RSSI;
   kal_int32 previous_freq;
   kal_uint8 tmp_reg;
//   kal_int8 pt;
    
//kal_uint32 save_irq_mask;
   
#ifdef MT6188_DEBUG_DUMP_LOG
   kal_uint32 start_t, duration_t;
   kal_sprintf((void*)_dbg_str, "\nFMDrv_ValidStop(%d, %d, %d);\n\0", freq, signalvl, is_step_up);
   FS_Write(_file_handle, _dbg_str, strlen((void*)_dbg_str), &_data_written);
   start_t = video_get_current_time();
#endif   

//save_irq_mask=SaveAndSetIRQMask();   
   SerialCommInit();
   
   //open A switch(bit0) and close B switch(bit2)<-- fix 1uF issue
   MT6188_ReadByte(CW( 34, 0), &tmp_reg);	
   tmp_reg &= 0xFA;
   MT6188_WriteByte(CW( 34, 0), tmp_reg|0x04); 
   
   MT6188_WriteByte(CW(21, 0), (MT6188_ReadCache(CW(21, 0)) & 0xFE) | 0x01);//enable auto AGC
   
   targetFreq = (kal_int32)freq;
   targetFreq *= FM_TUNER_GRID;
   previous_freq = _current_frequency;
   
   if (targetFreq == 104000 || targetFreq == 87500)
   	{
   		return 0;
   	}
 
   /// check RSSI at hi side
   MT6188_SetFreq(targetFreq, HI_INJECTION);

#ifdef MT6188_timing_cal
#ifdef MT6188_DEBUG_DUMP_LOG
   duration_t = video_get_duration_ms(start_t);
   kal_sprintf((void*)_dbg_str, "  Set_freq_Hi time cost: %d ms.\n\0", duration_t);
   FS_Write(_file_handle, _dbg_str, strlen((void*)_dbg_str), &_data_written);
#endif   
#endif
   
   if ((RSSI = (kal_int16)MT6188_GetCurRSSI()) < _rssi_threshold) 
   {
#ifdef MT6188_DEBUG_DUMP_LOG
      duration_t = video_get_duration_ms(start_t);
      kal_sprintf((void*)_dbg_str, "   time cost: %d ms.\n   #1 RSSI Check Fail! (%d < %d)\n RESULT:(Freq_Hi,RSSI):(%d,%d)\n\0", duration_t, RSSI, _rssi_threshold, targetFreq, RSSI);
      FS_Write(_file_handle, _dbg_str, strlen((void*)_dbg_str), &_data_written);
#endif

      SerialCommRelease();
      return 0;
   }
   else
   {
#ifdef MT6188_DEBUG_DUMP_LOG
      duration_t = video_get_duration_ms(start_t);
      kal_sprintf((void*)_dbg_str, "   time cost: %d ms.\n   #1 RSSI Check OK! (%d > %d)\n RESULT:(Freq_Hi,RSSI):(%d,%d)\n\0", duration_t, RSSI, _rssi_threshold, targetFreq, RSSI);
      FS_Write(_file_handle, _dbg_str, strlen((void*)_dbg_str), &_data_written);
#endif
   }
   
   IF_cnt = MT6188_GetCurIFCnt(IF_MEASURE_COUNT,0);

#ifdef MT6188_timing_cal
#ifdef MT6188_DEBUG_DUMP_LOG
      duration_t = video_get_duration_ms(start_t);
      kal_sprintf((void*)_dbg_str, "  IF_RSSI time cost: %d ms.\n   IF/RSSI Check (%d,%d)\n RESULT:(Freq_Hi,IF_cnt,RSSI):(%d,%d,%d)\n\0", duration_t, IF_cnt, RSSI,targetFreq, IF_cnt, RSSI);
      FS_Write(_file_handle, _dbg_str, strlen((void*)_dbg_str), &_data_written);
#endif
#endif
	
	index_count++;
	if (index_count > max_count)                           
		index_count = 0;                       
	
	                                  
	freq_IF_array[0][index_count] = targetFreq;
	freq_IF_array[1][index_count] = IF_cnt; 
	           
              
if (Chip_ID ==5)
{
    if (IF_cnt > (PRESET_IF_CNT + PRESET_IF_DELTA_check ) || 
       IF_cnt < (PRESET_IF_CNT - PRESET_IF_DELTA_check )) 
   {
   	
      SerialCommRelease();
#ifdef MT6188_timing_cal
#ifdef MT6188_DEBUG_DUMP_LOG
      duration_t = video_get_duration_ms(start_t);
      kal_sprintf((void*)_dbg_str, "  IF_RSSI time cost: %d ms.\n   IF/RSSI Check Fail! (%d,%d)\n	IF range(%d,%d)\n\0", duration_t, IF_cnt, RSSI,PRESET_IF_CNT - PRESET_IF_DELTA_check,PRESET_IF_CNT + PRESET_IF_DELTA_check );
      FS_Write(_file_handle, _dbg_str, strlen((void*)_dbg_str), &_data_written);
#endif
#endif
      return 0;
   }
   else
   {
#ifdef MT6188_timing_cal
#ifdef MT6188_DEBUG_DUMP_LOG
      duration_t = video_get_duration_ms(start_t);
      kal_sprintf((void*)_dbg_str, "  IF_RSSI time cost: %d ms.\n   IF/RSSI Check OK! (%d,%d)\n	IF range(%d,%d)\n\0", duration_t, IF_cnt, RSSI,PRESET_IF_CNT - PRESET_IF_DELTA_check,PRESET_IF_CNT + PRESET_IF_DELTA_check );
      FS_Write(_file_handle, _dbg_str, strlen((void*)_dbg_str), &_data_written);
#endif
#endif
   }
}   //end Chip_ID = 5
else    //Chip_ID = 4/3
{   
   if (IF_cnt > (PRESET_IF_CNT + PRESET_IF_DELTA_check ) || 
       IF_cnt < (PRESET_IF_CNT - PRESET_IF_DELTA_check - PRESET_IF_OFFSET)) 
   {
      SerialCommRelease();
#ifdef MT6188_timing_cal
#ifdef MT6188_DEBUG_DUMP_LOG
      duration_t = video_get_duration_ms(start_t);
      kal_sprintf((void*)_dbg_str, "  IF_RSSI time cost: %d ms.\n   IF/RSSI Check Fail! (%d,%d)\n	IF range(%d,%d)\n\0", duration_t, IF_cnt, RSSI,PRESET_IF_CNT - PRESET_IF_DELTA_check - PRESET_IF_OFFSET,PRESET_IF_CNT + PRESET_IF_DELTA_check );
      FS_Write(_file_handle, _dbg_str, strlen((void*)_dbg_str), &_data_written);
#endif
#endif
      return 0;
   }
   else
   {
#ifdef MT6188_timing_cal
#ifdef MT6188_DEBUG_DUMP_LOG
      duration_t = video_get_duration_ms(start_t);
      kal_sprintf((void*)_dbg_str, "  IF_RSSI time cost: %d ms.\n   IF/RSSI Check OK! (%d,%d)\n	IF range(%d,%d)\n\0", duration_t, IF_cnt, RSSI,PRESET_IF_CNT - PRESET_IF_DELTA_check - PRESET_IF_OFFSET,PRESET_IF_CNT + PRESET_IF_DELTA_check );
      FS_Write(_file_handle, _dbg_str, strlen((void*)_dbg_str), &_data_written);
#endif
#endif
   }
}   //end Chip_ID = 4/3

if (Chip_ID == 5)
{
#ifdef MT6188_DEBUG_DUMP_LOG
    kal_sprintf((void*)_list_str, "  channel list/RSSI Check! (%d,%d)\n	IF range(%d,%d)\n\0", targetFreq, RSSI,PRESET_IF_CNT - PRESET_IF_DELTA_check,PRESET_IF_CNT + PRESET_IF_DELTA_check );
    FS_Write(_file_list, _list_str, strlen((void*)_list_str), &_list_written);
#endif   
}
else    //Chip_ID = 4/3
{
#ifdef MT6188_DEBUG_DUMP_LOG
    kal_sprintf((void*)_list_str, "  channel list/RSSI Check! (%d,%d)\n	IF range(%d,%d)\n\0", targetFreq, RSSI,PRESET_IF_CNT - PRESET_IF_DELTA_check - PRESET_IF_OFFSET,PRESET_IF_CNT + PRESET_IF_DELTA_check );
    FS_Write(_file_list, _list_str, strlen((void*)_list_str), &_list_written);
#endif   
}


	if((previous_freq <= freq)||((previous_freq >= 1080)))
	{
		if((index_count <= max_count)&&((index_count-1) < 0))
		{
			if(freq_IF_array[0][max_count] == (targetFreq-100))
			{
				MT6188_SetFreq(targetFreq+100, HI_INJECTION);
				IF_cnt = MT6188_GetCurIFCnt(IF_MEASURE_COUNT,0);        
				freq_IF_array[0][index_count+1] = targetFreq+100;      
				freq_IF_array[1][index_count+1]= IF_cnt;   

#ifdef MT6188_DEBUG_DUMP_LOG
    kal_sprintf((void*)_list_str, "  freq Check1! (%d,%d,%d)\n IF_count1(%d,%d,%d,%d,%d)\n\0",freq_IF_array[0][max_count],freq_IF_array[0][index_count],freq_IF_array[0][index_count+1],freq_IF_array[1][max_count],freq_IF_array[1][index_count],freq_IF_array[1][index_count+1],PRESET_IF_DELTA,index_count);
    FS_Write(_file_list, _list_str, strlen((void*)_list_str), &_list_written);
#endif   				 
				
				if(((freq_IF_array[1][index_count]-freq_IF_array[1][max_count]) < PRESET_IF_DELTA)||
				((freq_IF_array[1][index_count+1]-freq_IF_array[1][index_count]) < PRESET_IF_DELTA)) 
				{
#ifdef MT6188_DEBUG_DUMP_LOG
    kal_sprintf((void*)_list_str, "  freq Check1 FAILE! \n ");
    FS_Write(_file_list, _list_str, strlen((void*)_list_str), &_list_written);
#endif   

			
			SerialCommRelease();
					return 0;   
				}      
			}
			else
			{
				MT6188_SetFreq(targetFreq-100, HI_INJECTION);
				IF_cnt = MT6188_GetCurIFCnt(IF_MEASURE_COUNT,0); 
				freq_IF_array[0][max_count] = targetFreq-100;      
				freq_IF_array[1][max_count] = IF_cnt; 
				
				MT6188_SetFreq(targetFreq+100, HI_INJECTION);
				IF_cnt = MT6188_GetCurIFCnt(IF_MEASURE_COUNT,0);        
				freq_IF_array[0][index_count+1] = targetFreq+100;      
				freq_IF_array[1][index_count+1]= IF_cnt;    

#ifdef MT6188_DEBUG_DUMP_LOG
    kal_sprintf((void*)_list_str, "  freq Check2! (%d,%d,%d)\n IF_count3(%d,%d,%d,%d,%d)\n\0",freq_IF_array[0][max_count],freq_IF_array[0][index_count],freq_IF_array[0][index_count+1],freq_IF_array[1][max_count],freq_IF_array[1][index_count],freq_IF_array[1][index_count+1],PRESET_IF_DELTA,index_count);
    FS_Write(_file_list, _list_str, strlen((void*)_list_str), &_list_written);
#endif  

				
				if(((freq_IF_array[1][index_count]-freq_IF_array[1][max_count]) < PRESET_IF_DELTA)||
				((freq_IF_array[1][index_count+1]-freq_IF_array[1][index_count]) < PRESET_IF_DELTA)) 
				{
#ifdef MT6188_DEBUG_DUMP_LOG
    kal_sprintf((void*)_list_str, "  freq Check2 FAILE! \n ");
    FS_Write(_file_list, _list_str, strlen((void*)_list_str), &_list_written);
#endif   

		
			SerialCommRelease();
					return 0; 
				}       		
			}
		}
		else if((index_count <= max_count)&&((index_count+1) > max_count))
		{
			if(freq_IF_array[0][index_count-1] == (targetFreq-100))
			{
				MT6188_SetFreq(targetFreq+100, HI_INJECTION);
				IF_cnt = MT6188_GetCurIFCnt(IF_MEASURE_COUNT,0);        
				freq_IF_array[0][0] = targetFreq+100;      
				freq_IF_array[1][0] = IF_cnt;    

#ifdef MT6188_DEBUG_DUMP_LOG
    kal_sprintf((void*)_list_str, "  freq Check3! (%d,%d,%d)\n IF_count3(%d,%d,%d,%d,%d)\n\0",freq_IF_array[0][index_count-1],freq_IF_array[0][index_count],freq_IF_array[0][0],freq_IF_array[1][index_count-1],freq_IF_array[1][index_count],freq_IF_array[1][0],PRESET_IF_DELTA,index_count);
    FS_Write(_file_list, _list_str, strlen((void*)_list_str), &_list_written);
#endif  

				
				if(((freq_IF_array[1][index_count]-freq_IF_array[1][index_count-1]) < PRESET_IF_DELTA)||
				((freq_IF_array[1][0]-freq_IF_array[1][index_count]) < PRESET_IF_DELTA)) 
				{
#ifdef MT6188_DEBUG_DUMP_LOG
    kal_sprintf((void*)_list_str, "  freq Check3 FAILE! \n ");
    FS_Write(_file_list, _list_str, strlen((void*)_list_str), &_list_written);
#endif   

		
			SerialCommRelease();
					return 0;
				}         
			}
			else
			{
				MT6188_SetFreq(targetFreq-100, HI_INJECTION);
				IF_cnt = MT6188_GetCurIFCnt(IF_MEASURE_COUNT,0); 
				freq_IF_array[0][index_count-1] = targetFreq-100;      
				freq_IF_array[1][index_count-1] = IF_cnt; 
				
				MT6188_SetFreq(targetFreq+100, HI_INJECTION);
				IF_cnt = MT6188_GetCurIFCnt(IF_MEASURE_COUNT,0);        
				freq_IF_array[0][0] = targetFreq+100;      
				freq_IF_array[1][0] = IF_cnt;    

#ifdef MT6188_DEBUG_DUMP_LOG
    kal_sprintf((void*)_list_str, "  freq Check4! (%d,%d,%d)\n IF_count4(%d,%d,%d,%d,%d)\n\0",freq_IF_array[0][index_count-1],freq_IF_array[0][index_count],freq_IF_array[0][0],freq_IF_array[1][index_count-1],freq_IF_array[1][index_count],freq_IF_array[1][0],PRESET_IF_DELTA,index_count);
    FS_Write(_file_list, _list_str, strlen((void*)_list_str), &_list_written);
#endif 
				
				if(((freq_IF_array[1][index_count]-freq_IF_array[1][index_count-1]) < PRESET_IF_DELTA)||
				((freq_IF_array[1][0]-freq_IF_array[1][index_count]) < PRESET_IF_DELTA)) 
				{
#ifdef MT6188_DEBUG_DUMP_LOG
    kal_sprintf((void*)_list_str, "  freq Check4 FAILE! \n ");
    FS_Write(_file_list, _list_str, strlen((void*)_list_str), &_list_written);
#endif   		

		
			SerialCommRelease();				
					return 0;
				}    
			}
		}
		else
		{
			if(freq_IF_array[0][index_count-1] == (targetFreq-100))
			{
				MT6188_SetFreq(targetFreq+100, HI_INJECTION);
				IF_cnt = MT6188_GetCurIFCnt(IF_MEASURE_COUNT,0);        
				freq_IF_array[0][index_count+1] = targetFreq+100;      
				freq_IF_array[1][index_count+1]= IF_cnt;    

#ifdef MT6188_DEBUG_DUMP_LOG
    kal_sprintf((void*)_list_str, "  freq Check5! (%d,%d,%d)\n IF_count5(%d,%d,%d,%d,%d)\n\0",freq_IF_array[0][index_count-1],freq_IF_array[0][index_count],freq_IF_array[0][index_count+1],freq_IF_array[1][index_count-1],freq_IF_array[1][index_count],freq_IF_array[1][index_count+1],PRESET_IF_DELTA,index_count);
    FS_Write(_file_list, _list_str, strlen((void*)_list_str), &_list_written);
#endif 
				
				if(((freq_IF_array[1][index_count]-freq_IF_array[1][index_count-1]) < PRESET_IF_DELTA)||
				((freq_IF_array[1][index_count+1]-freq_IF_array[1][index_count]) < PRESET_IF_DELTA)) 
				{
#ifdef MT6188_DEBUG_DUMP_LOG
    kal_sprintf((void*)_list_str, "  freq Check5 FAILE! \n ");
    FS_Write(_file_list, _list_str, strlen((void*)_list_str), &_list_written);
#endif   		

	
			SerialCommRelease();				
					return 0;
				}          
			}
			else
			{
				MT6188_SetFreq(targetFreq-100, HI_INJECTION);
				IF_cnt = MT6188_GetCurIFCnt(IF_MEASURE_COUNT,0); 
				freq_IF_array[0][index_count-1] = targetFreq-100;      
				freq_IF_array[1][index_count-1] = IF_cnt; 
				
				MT6188_SetFreq(targetFreq+100, HI_INJECTION);
				IF_cnt = MT6188_GetCurIFCnt(IF_MEASURE_COUNT,0);        
				freq_IF_array[0][index_count+1] = targetFreq+100;      
				freq_IF_array[1][index_count+1]= IF_cnt;    

#ifdef MT6188_DEBUG_DUMP_LOG
    kal_sprintf((void*)_list_str, "  freq Check6! (%d,%d,%d)\n IF_count6(%d,%d,%d,%d,%d)\n\0",freq_IF_array[0][index_count-1],freq_IF_array[0][index_count],freq_IF_array[0][index_count+1],freq_IF_array[1][index_count-1],freq_IF_array[1][index_count],freq_IF_array[1][index_count+1],PRESET_IF_DELTA,index_count);
    FS_Write(_file_list, _list_str, strlen((void*)_list_str), &_list_written);
#endif 
				
				if(((freq_IF_array[1][index_count]-freq_IF_array[1][index_count-1]) < PRESET_IF_DELTA)||
				((freq_IF_array[1][index_count+1]-freq_IF_array[1][index_count]) < PRESET_IF_DELTA)) 
				{
#ifdef MT6188_DEBUG_DUMP_LOG
    kal_sprintf((void*)_list_str, "  freq Check6 FAILE! \n ");
    FS_Write(_file_list, _list_str, strlen((void*)_list_str), &_list_written);
#endif   		

		
			SerialCommRelease();				
					return 0;
				}    
			}
		}
	}
	else if ((previous_freq >= freq)||((previous_freq <= 875)))
	{
		if((index_count <= max_count)&&((index_count-1) < 0))
		{
			if(freq_IF_array[0][max_count] == (targetFreq+100))
			{
				MT6188_SetFreq(targetFreq-100, HI_INJECTION);
				IF_cnt = MT6188_GetCurIFCnt(IF_MEASURE_COUNT,0);        
				freq_IF_array[0][index_count+1] = targetFreq-100;      
				freq_IF_array[1][index_count+1]= IF_cnt;    

#ifdef MT6188_DEBUG_DUMP_LOG
    kal_sprintf((void*)_list_str, "  freq Check7! (%d,%d,%d)\n IF_count7(%d,%d,%d,%d,%d)\n\0",freq_IF_array[0][max_count],freq_IF_array[0][index_count],freq_IF_array[0][index_count+1],freq_IF_array[1][max_count],freq_IF_array[1][index_count],freq_IF_array[1][index_count+1],PRESET_IF_DELTA,index_count);
    FS_Write(_file_list, _list_str, strlen((void*)_list_str), &_list_written);
#endif  
				
				if(((freq_IF_array[1][max_count]-freq_IF_array[1][index_count]) < PRESET_IF_DELTA)||
				((freq_IF_array[1][index_count]-freq_IF_array[1][index_count+1]) < PRESET_IF_DELTA)) 
				{
#ifdef MT6188_DEBUG_DUMP_LOG
    kal_sprintf((void*)_list_str, "  freq Check7 FAILE! \n ");
    FS_Write(_file_list, _list_str, strlen((void*)_list_str), &_list_written);
#endif   			

	
			SerialCommRelease();			
					return 0;
				}         
			}
			else
			{
				MT6188_SetFreq(targetFreq+100, HI_INJECTION);
				IF_cnt = MT6188_GetCurIFCnt(IF_MEASURE_COUNT,0); 
				freq_IF_array[0][max_count] = targetFreq+100;      
				freq_IF_array[1][max_count] = IF_cnt; 
				
				MT6188_SetFreq(targetFreq-100, HI_INJECTION);
				IF_cnt = MT6188_GetCurIFCnt(IF_MEASURE_COUNT,0);        
				freq_IF_array[0][index_count+1] = targetFreq-100;      
				freq_IF_array[1][index_count+1]= IF_cnt;    

#ifdef MT6188_DEBUG_DUMP_LOG
    kal_sprintf((void*)_list_str, "  freq Check8! (%d,%d,%d)\n IF_count8(%d,%d,%d,%d,%d)\n\0",freq_IF_array[0][max_count],freq_IF_array[0][index_count],freq_IF_array[0][index_count+1],freq_IF_array[1][max_count],freq_IF_array[1][index_count],freq_IF_array[1][index_count+1],PRESET_IF_DELTA,index_count);
    FS_Write(_file_list, _list_str, strlen((void*)_list_str), &_list_written);
#endif  
				
				if(((freq_IF_array[1][max_count]-freq_IF_array[1][index_count]) < PRESET_IF_DELTA)||
				((freq_IF_array[1][index_count]-freq_IF_array[1][index_count+1]) < PRESET_IF_DELTA)) 
				{
#ifdef MT6188_DEBUG_DUMP_LOG
    kal_sprintf((void*)_list_str, "  freq Check8 FAILE! \n ");
    FS_Write(_file_list, _list_str, strlen((void*)_list_str), &_list_written);
#endif   			

	
			SerialCommRelease();			
					return 0;
				}   
			}
		}
		else if((index_count <= max_count)&&((index_count+1) > max_count))
		{
			if(freq_IF_array[0][index_count-1] == (targetFreq+100))
			{
				MT6188_SetFreq(targetFreq-100, HI_INJECTION);
				IF_cnt = MT6188_GetCurIFCnt(IF_MEASURE_COUNT,0);        
				freq_IF_array[0][0] = targetFreq-100;      
				freq_IF_array[1][0] = IF_cnt;    

#ifdef MT6188_DEBUG_DUMP_LOG
    kal_sprintf((void*)_list_str, "  freq Check9! (%d,%d,%d)\n IF_count9(%d,%d,%d,%d,%d)\n\0",freq_IF_array[0][index_count-1],freq_IF_array[0][index_count],freq_IF_array[0][0],freq_IF_array[1][index_count-1],freq_IF_array[1][index_count],freq_IF_array[1][0],PRESET_IF_DELTA,index_count);
    FS_Write(_file_list, _list_str, strlen((void*)_list_str), &_list_written);
#endif 
				
				if(((freq_IF_array[1][index_count-1]-freq_IF_array[1][index_count]) < PRESET_IF_DELTA)||
				((freq_IF_array[1][index_count]-freq_IF_array[1][0]) < PRESET_IF_DELTA)) 
				{
#ifdef MT6188_DEBUG_DUMP_LOG
    kal_sprintf((void*)_list_str, "  freq Check9 FAILE! \n ");
    FS_Write(_file_list, _list_str, strlen((void*)_list_str), &_list_written);
#endif   	

	
			SerialCommRelease();					
					return 0;
				}         
			}
			else
			{
				MT6188_SetFreq(targetFreq+100, HI_INJECTION);
				IF_cnt = MT6188_GetCurIFCnt(IF_MEASURE_COUNT,0); 
				freq_IF_array[0][index_count-1] = targetFreq+100;      
				freq_IF_array[1][index_count-1] = IF_cnt; 
				
				MT6188_SetFreq(targetFreq-100, HI_INJECTION);
				IF_cnt = MT6188_GetCurIFCnt(IF_MEASURE_COUNT,0);        
				freq_IF_array[0][0] = targetFreq-100;      
				freq_IF_array[1][0] = IF_cnt;    

#ifdef MT6188_DEBUG_DUMP_LOG
    kal_sprintf((void*)_list_str, "  freq Check10! (%d,%d,%d)\n IF_count10(%d,%d,%d,%d,%d)\n\0",freq_IF_array[0][index_count-1],freq_IF_array[0][index_count],freq_IF_array[0][0],freq_IF_array[1][index_count-1],freq_IF_array[1][index_count],freq_IF_array[1][0],PRESET_IF_DELTA,index_count);
    FS_Write(_file_list, _list_str, strlen((void*)_list_str), &_list_written);
#endif 
				
				if(((freq_IF_array[1][index_count-1]-freq_IF_array[1][index_count]) < PRESET_IF_DELTA)||
				((freq_IF_array[1][index_count]-freq_IF_array[1][0]) < PRESET_IF_DELTA)) 
				{
#ifdef MT6188_DEBUG_DUMP_LOG
    kal_sprintf((void*)_list_str, "  freq Check10 FAILE! \n ");
    FS_Write(_file_list, _list_str, strlen((void*)_list_str), &_list_written);
#endif   	

	
			SerialCommRelease();					
					return 0;
				}   
			}
		}
		else
		{
			if(freq_IF_array[0][index_count-1] == (targetFreq+100))
			{
				MT6188_SetFreq(targetFreq-100, HI_INJECTION);
				IF_cnt = MT6188_GetCurIFCnt(IF_MEASURE_COUNT,0);        
				freq_IF_array[0][index_count+1] = targetFreq-100;      
				freq_IF_array[1][index_count+1]= IF_cnt;    

#ifdef MT6188_DEBUG_DUMP_LOG
    kal_sprintf((void*)_list_str, "  freq Check11! (%d,%d,%d)\n IF_count11(%d,%d,%d,%d,%d)\n\0",freq_IF_array[0][index_count-1],freq_IF_array[0][index_count],freq_IF_array[0][index_count+1],freq_IF_array[1][index_count-1],freq_IF_array[1][index_count],freq_IF_array[1][index_count+1],PRESET_IF_DELTA,index_count);
    FS_Write(_file_list, _list_str, strlen((void*)_list_str), &_list_written);
#endif 

				
				if(((freq_IF_array[1][index_count-1]-freq_IF_array[1][index_count]) < PRESET_IF_DELTA)||
				((freq_IF_array[1][index_count]-freq_IF_array[1][index_count+1]) < PRESET_IF_DELTA)) 
				{
#ifdef MT6188_DEBUG_DUMP_LOG
    kal_sprintf((void*)_list_str, "  freq Check11 FAILE! \n ");
    FS_Write(_file_list, _list_str, strlen((void*)_list_str), &_list_written);
#endif   		

	
			SerialCommRelease();				
					return 0;
				}          
			}
			else
			{
				MT6188_SetFreq(targetFreq+100, HI_INJECTION);
				IF_cnt = MT6188_GetCurIFCnt(IF_MEASURE_COUNT,0); 
				freq_IF_array[0][max_count-1] = targetFreq+100;      
				freq_IF_array[1][max_count-1] = IF_cnt; 
				
				MT6188_SetFreq(targetFreq-100, HI_INJECTION);
				IF_cnt = MT6188_GetCurIFCnt(IF_MEASURE_COUNT,0);        
				freq_IF_array[0][index_count+1] = targetFreq-100;      
				freq_IF_array[1][index_count+1]= IF_cnt;    

#ifdef MT6188_DEBUG_DUMP_LOG
    kal_sprintf((void*)_list_str, "  freq Check12! (%d,%d,%d)\n IF_count12(%d,%d,%d,%d,%d)\n\0",freq_IF_array[0][index_count-1],freq_IF_array[0][index_count],freq_IF_array[0][index_count+1],freq_IF_array[1][index_count-1],freq_IF_array[1][index_count],freq_IF_array[1][index_count+1],PRESET_IF_DELTA,index_count);
    FS_Write(_file_list, _list_str, strlen((void*)_list_str), &_list_written);
#endif 
				
				if(((freq_IF_array[1][index_count-1]-freq_IF_array[1][index_count]) < PRESET_IF_DELTA)||
				((freq_IF_array[1][index_count]-freq_IF_array[1][index_count+1]) < PRESET_IF_DELTA)) 
				{
#ifdef MT6188_DEBUG_DUMP_LOG
    kal_sprintf((void*)_list_str, "  freq Check12 FAILE! \n ");
    FS_Write(_file_list, _list_str, strlen((void*)_list_str), &_list_written);
#endif   	

	
			SerialCommRelease();					
					return 0;
			}   
		}
	}
}

   FMDrv_SetVolumeLevel(_current_level);
   
   SerialCommRelease();

//RestoreIRQMask(save_irq_mask);   

#ifdef MT6188_DEBUG_DUMP_LOG
      duration_t = video_get_duration_ms(start_t);
      kal_sprintf((void*)_dbg_str, "   time cost: %d ms.\n   Pass! RESULT:(Freq, IF, RSSI) = (%d, %d, %d)\n\0", duration_t, targetFreq, IF_cnt, RSSI);
      FS_Write(_file_handle, _dbg_str, strlen((void*)_dbg_str), &_data_written);
#endif

   return 1;
}

void FMDrv_ChipInit(void)
{
   /// power down the chip
   kal_int32 i;
   kal_uint8 tmp_reg;
   SerialCommInit();
   
   MT6188_ReadByte(CW(0, 1), &Chip_ID);
    Chip_ID &= 0x07;
   
   for (i=0; i<POWER_OFF_COMMAND_COUNT; i++) 
   {
      MT6188_ReadByte(PowerOffProc[i].addr, &tmp_reg);
      tmp_reg &= PowerOffProc[i].and;
      tmp_reg |= PowerOffProc[i].or;
      MT6188_WriteByte(PowerOffProc[i].addr, tmp_reg);
   }
   SerialCommCryClkOff();
   SerialCommRelease();
#ifdef USE_MT6188_AMP
   ExtSwitchInit();
#endif
}

kal_bool FMDrv_IsChipValid( void )
{
	/// anything to do?
	return KAL_TRUE;
}

kal_uint16 FMDrv_ReadByte(kal_uint8 addr)
{
	kal_uint16 Data;
	kal_uint8 TmpReg1, TmpReg2;
	MT6188_ReadByte(CW(addr, 0), &TmpReg1);
	MT6188_ReadByte(CW(addr, 1), &TmpReg2);
	Data = ((TmpReg2<<8)|TmpReg1);
	return Data;
}

kal_bool FMDrv_WriteByte(kal_uint8 addr, kal_uint16 data)
{
	kal_uint8 Data_M, Data_L;
	Data_M = data>>8;
	Data_L = data&0xFF;
	if((KAL_FALSE == MT6188_WriteByte(CW(addr,0), Data_L))&&(KAL_FALSE == MT6188_WriteByte(CW(addr,1), Data_M)))
		return KAL_FALSE;
	else
		return KAL_TRUE;
}

void FMDrv_SoftMuteOnOff(kal_uint8 On_Off)
{
    kal_uint8 TmpReg;
    if (Chip_ID == 5)
    {
        if (On_Off)
        {
            MT6188_ReadByte(CW(14, 0), &TmpReg);
      	    TmpReg&=0xF7;
      	    MT6188_WriteByte(CW(14, 0),TmpReg|0x08);
      	}
      	else
      	{
      	    MT6188_ReadByte(CW(14, 0), &TmpReg);
      	    TmpReg&=0xF7;
      	    MT6188_WriteByte(CW(14, 0),TmpReg);
      	}
    }
}

void FMDrv_SoftMuteStage(kal_uint8 stage)
{
	kal_uint8 TmpReg;
	if (Chip_ID == 5)
	{
		MT6188_ReadByte(CW(17, 1), &TmpReg);//CW17_1 [15]0
      	    	TmpReg&=0x7F;
      	    	MT6188_WriteByte(CW(17, 1),TmpReg);
      	    		
      	    	MT6188_ReadByte(CW(18, 0), &TmpReg);//CW18_0 [3] 1
      	    	TmpReg&=0xF7;
      	    	MT6188_WriteByte(CW(18, 0),TmpReg|0x08);
      	    		
      	    	MT6188_ReadByte(CW(18, 1), &TmpReg);//CW18_1 [11] 0
      	    	TmpReg&=0xF7;
      	    	MT6188_WriteByte(CW(18, 1),TmpReg);
      	    	
      	    	MT6188_ReadByte(CW(22, 1), &TmpReg);//CW22_1 [14:11] 1000
      	    	TmpReg&=0x87;
      	    	MT6188_WriteByte(CW(22, 1),TmpReg|0x40);
      	    	
		switch (stage)
		{
		    case 1:			      	    		
      	        		MT6188_ReadByte(CW(22, 1), &TmpReg);//CW22_1 [15] 1
      	        		TmpReg&=0x7F;
      	        		MT6188_WriteByte(CW(22, 1),TmpReg|0x80);
      	        		
      	        		MT6188_ReadByte(CW(22, 0), &TmpReg);//CW22_0 [7:5] 0
      	        		TmpReg&=0x1F;
      	        		MT6188_WriteByte(CW(22, 0),TmpReg);
		    break;
		    case 2:
		    	MT6188_ReadByte(CW(22, 1), &TmpReg);//CW22_1 [15] 0
      	        		TmpReg&=0x7F;
      	        		MT6188_WriteByte(CW(22, 1),TmpReg);
      	        		
      	        		MT6188_ReadByte(CW(22, 0), &TmpReg);//CW22_0 [7:5] 0
      	        		TmpReg&=0x1F;
      	        		MT6188_WriteByte(CW(22, 0),TmpReg);
		    break;
		    case 3:
		    	MT6188_ReadByte(CW(22, 1), &TmpReg);//CW22_1 [15] 0
      	        		TmpReg&=0x7F;
      	        		MT6188_WriteByte(CW(22, 1),TmpReg);
      	        		
      	        		MT6188_ReadByte(CW(22, 0), &TmpReg);//CW22_0 [7:5] 2
      	        		TmpReg&=0x1F;
      	        		MT6188_WriteByte(CW(22, 0),TmpReg|0x40);
		    break;
	    }
	}
}

void FMDrv_StereoBlendStage(kal_uint8 stage)
{
	kal_uint8 TmpReg;
	if (Chip_ID == 5)
	{
		switch (stage)
		{
		    case 1:
		    	MT6188_ReadByte(CW(17, 0), &TmpReg);//CW17_0 [4:2] 3 [0:1] 2
      	        		TmpReg&=0xE0;
      	        		MT6188_WriteByte(CW(17, 0),TmpReg|0x0E);
      	        		
      	        		MT6188_ReadByte(CW(19, 0), &TmpReg);//CW19_0 [4:2] 6 [6:5] 1
      	        		TmpReg&=0x83;
      	        		MT6188_WriteByte(CW(19, 0),TmpReg|0x38);
      	        		
      	        		MT6188_ReadByte(CW(22, 0), &TmpReg);//CW22_0 [4:2] 1
      	        		TmpReg&=0xE3;
      	        		MT6188_WriteByte(CW(22, 0),TmpReg|0x04);
      	        		
      	        		MT6188_ReadByte(CW(10, 1), &TmpReg);//CW10_1 [12:9] 4
      	        		TmpReg&=0xE1;
      	        		MT6188_WriteByte(CW(10, 1),TmpReg|0x08);
		    break;
		    case 2:
		    	MT6188_ReadByte(CW(17, 0), &TmpReg);//CW17_0 [4:2] 4 [0:1] 0
      	        		TmpReg&=0xE0;
      	        		MT6188_WriteByte(CW(17, 0),TmpReg|0x10);
      	        		
      	        		MT6188_ReadByte(CW(19, 0), &TmpReg);//CW19_0 [4:2] 7 [6:5] 1
      	        		TmpReg&=0x83;
      	        		MT6188_WriteByte(CW(19, 0),TmpReg|0x3C);
      	        		
      	        		MT6188_ReadByte(CW(22, 0), &TmpReg);//CW22_0 [4:2] 1
      	        		TmpReg&=0xE3;
      	        		MT6188_WriteByte(CW(22, 0),TmpReg|0x04);
      	        		
      	        		MT6188_ReadByte(CW(10, 1), &TmpReg);//CW10_1 [12:9] 4
      	        		TmpReg&=0xE1;
      	        		MT6188_WriteByte(CW(10, 1),TmpReg|0x08);
		    break;
		    case 3:
		    	MT6188_ReadByte(CW(17, 0), &TmpReg);//CW17_0 [4:2] 0 [0:1] 0
      	        		TmpReg&=0xE0;
      	        		MT6188_WriteByte(CW(17, 0),TmpReg);
      	        		
      	        		MT6188_ReadByte(CW(19, 0), &TmpReg);//CW19_0 [4:2] 0 [6:5] 0
      	        		TmpReg&=0x83;
      	        		MT6188_WriteByte(CW(19, 0),TmpReg);
      	        		
      	        		MT6188_ReadByte(CW(22, 0), &TmpReg);//CW22_0 [4:2] 0
      	        		TmpReg&=0xE3;
      	        		MT6188_WriteByte(CW(22, 0),TmpReg);
      	        		
      	        		MT6188_ReadByte(CW(10, 1), &TmpReg);//CW10_1 [12:9] 0
      	        		TmpReg&=0xE1;
      	        		MT6188_WriteByte(CW(10, 1),TmpReg);
		    break;
	    }
	}
}

kal_uint8 FMDrv_GetChipID(void)
{
	kal_uint8 ChipID;
	MT6188_ReadByte(CW(0, 1), &ChipID);
    	ChipID &= 0x07;
    	
    	return ChipID;
}

kal_uint8 FMDrv_Get_H_L_side(kal_int16 curf) /* input value: 875 - 1080 ( 87.5 MHz - 108.0 MHz)*/
{
	kal_uint8 tempreg;
	MT6188_ReadByte(CW(4, 0), &tempreg);
	if (tempreg & 0x80)
		return 1;
	else
		return 0;	
}

kal_uint8 FMDrv_Get_stereo_mono(void)
{
	kal_uint8 tempreg;
	MT6188_ReadByte(CW(14, 0), &tempreg);
	if (tempreg & 0x01)
		return 1;
	else
		return 0;	
}
#endif // defined(MT6188A1)