summaryrefslogtreecommitdiffstats
path: root/api/mt2266.c
blob: 4cdc225bd9cb409b5dcd64e2dfeaf4bfc0e97d65 (plain)
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
/*****************************************************************************
**
**  Name: mt2266.c
**
**  Copyright 2007 Microtune, Inc. All Rights Reserved
**
**  This source code file contains confidential information and/or trade
**  secrets of Microtune, Inc. or its affiliates and is subject to the
**  terms of your confidentiality agreement with Microtune, Inc. or one of
**  its affiliates, as applicable.
**
*****************************************************************************/

/*****************************************************************************
**
**  Name: mt2266.c
**
**  Description:    Microtune MT2266 Tuner software interface.
**                  Supports tuners with Part/Rev code: 0x85.
**
**  Functions
**  Implemented:    UData_t  MT2266_Open
**                  UData_t  MT2266_Close
**                  UData_t  MT2266_ChangeFreq
**                  UData_t  MT2266_GetLocked
**                  UData_t  MT2266_GetParam
**                  UData_t  MT2266_GetReg
**                  UData_t  MT2266_GetUHFXFreqs
**                  UData_t  MT2266_GetUserData
**                  UData_t  MT2266_ReInit
**                  UData_t  MT2266_SetParam
**                  UData_t  MT2266_SetPowerModes
**                  UData_t  MT2266_SetReg
**                  UData_t  MT2266_SetUHFXFreqs
**
**  References:     AN-00010: MicroTuner Serial Interface Application Note
**                  MicroTune, Inc.
**
**  Exports:        None
**
**  Dependencies:   MT2266_ReadSub(hUserData, IC_Addr, subAddress, *pData, cnt);
**                  - Read byte(s) of data from the two-wire bus.
**
**                  MT2266_WriteSub(hUserData, IC_Addr, subAddress, *pData, cnt);
**                  - Write byte(s) of data to the two-wire bus.
**
**                  MT_Sleep(hUserData, nMinDelayTime);
**                  - Delay execution for x milliseconds
**
**  CVS ID:         $Id: mt2266.c,v 1.5 2007/10/02 18:43:17 software Exp $
**  CVS Source:     $Source: /export/home/cvsroot/software/tuners/MT2266/mt2266.c,v $
**
**  Revision History:
**
**   SCR      Date      Author  Description
**  -------------------------------------------------------------------------
**   N/A   05-30-2006    DAD    Ver 1.0: Modified version of mt2260.c (Ver 1.01).
**   N/A   06-08-2006    JWS    Ver 1.01: Corrected problem with tuner ID check
**   N/A   11-01-2006    RSK    Ver 1.02: Adding multiple-filter support
**                                        as well as Get/Set functions.
**   N/A   11-29-2006    DAD    Ver 1.03: Parenthesis clarification for gcc
**   N/A   12-20-2006    RSK    Ver 1.04: Adding fLO_FractionalTerm() usage.
**   118   05-09-2007    RSK    Ver 1.05: Adding Standard MTxxxx_Tune() API.
**
*****************************************************************************/
#include "mt2266.h"
//#include <stdlib.h>                     /* for NULL */ // for Linux

/*  Version of this module                          */
#define VERSION 10005             /*  Version 01.05 */


#ifndef MT2266_CNT
#error You must define MT2266_CNT in the "mt_userdef.h" file
#endif

/*
**  Normally, the "reg" array in the tuner structure is used as a cache
**  containing the current value of the tuner registers.  If the user's
**  application MUST change tuner registers without using the MT2266_SetReg
**  routine provided, he may compile this code with the __NO_CACHE__
**  variable defined.
**  The PREFETCH macro will insert code code to re-read tuner registers if
**  __NO_CACHE__ is defined.  If it is not defined (normal) then PREFETCH
**  does nothing.
*/

#if defined(__NO_CACHE__)
#define PREFETCH(var, cnt) \
    if (MT_NO_ERROR(status)) \
    status |= MT2266_ReadSub(pInfo->hUserData, pInfo->address, (var), &pInfo->reg[(var)], (cnt));
#else
#define PREFETCH(var, cnt)
#endif



/*
**  Two-wire serial bus subaddresses of the tuner registers.
**  Also known as the tuner's register addresses.
*/
static enum MT2266_Register_Offsets
{
    MT2266_PART_REV = 0,   /*  0x00 */
    MT2266_LO_CTRL_1,      /*  0x01 */
    MT2266_LO_CTRL_2,      /*  0x02 */
    MT2266_LO_CTRL_3,      /*  0x03 */
    MT2266_SMART_ANT,      /*  0x04 */
    MT2266_BAND_CTRL,      /*  0x05 */
    MT2266_CLEARTUNE,      /*  0x06 */
    MT2266_IGAIN,          /*  0x07 */
    MT2266_BBFILT_1,       /*  0x08 */
    MT2266_BBFILT_2,       /*  0x09 */
    MT2266_BBFILT_3,       /*  0x0A */
    MT2266_BBFILT_4,       /*  0x0B */
    MT2266_BBFILT_5,       /*  0x0C */
    MT2266_BBFILT_6,       /*  0x0D */
    MT2266_BBFILT_7,       /*  0x0E */
    MT2266_BBFILT_8,       /*  0x0F */
    MT2266_RCC_CTRL,       /*  0x10 */
    MT2266_RSVD_11,        /*  0x11 */
    MT2266_STATUS_1,       /*  0x12 */
    MT2266_STATUS_2,       /*  0x13 */
    MT2266_STATUS_3,       /*  0x14 */
    MT2266_STATUS_4,       /*  0x15 */
    MT2266_STATUS_5,       /*  0x16 */
    MT2266_SRO_CTRL,       /*  0x17 */
    MT2266_RSVD_18,        /*  0x18 */
    MT2266_RSVD_19,        /*  0x19 */
    MT2266_RSVD_1A,        /*  0x1A */
    MT2266_RSVD_1B,        /*  0x1B */
    MT2266_ENABLES,        /*  0x1C */
    MT2266_RSVD_1D,        /*  0x1D */
    MT2266_RSVD_1E,        /*  0x1E */
    MT2266_RSVD_1F,        /*  0x1F */
    MT2266_GPO,            /*  0x20 */
    MT2266_RSVD_21,        /*  0x21 */
    MT2266_RSVD_22,        /*  0x22 */
    MT2266_RSVD_23,        /*  0x23 */
    MT2266_RSVD_24,        /*  0x24 */
    MT2266_RSVD_25,        /*  0x25 */
    MT2266_RSVD_26,        /*  0x26 */
    MT2266_RSVD_27,        /*  0x27 */
    MT2266_RSVD_28,        /*  0x28 */
    MT2266_RSVD_29,        /*  0x29 */
    MT2266_RSVD_2A,        /*  0x2A */
    MT2266_RSVD_2B,        /*  0x2B */
    MT2266_RSVD_2C,        /*  0x2C */
    MT2266_RSVD_2D,        /*  0x2D */
    MT2266_RSVD_2E,        /*  0x2E */
    MT2266_RSVD_2F,        /*  0x2F */
    MT2266_RSVD_30,        /*  0x30 */
    MT2266_RSVD_31,        /*  0x31 */
    MT2266_RSVD_32,        /*  0x32 */
    MT2266_RSVD_33,        /*  0x33 */
    MT2266_RSVD_34,        /*  0x34 */
    MT2266_RSVD_35,        /*  0x35 */
    MT2266_RSVD_36,        /*  0x36 */
    MT2266_RSVD_37,        /*  0x37 */
    MT2266_RSVD_38,        /*  0x38 */
    MT2266_RSVD_39,        /*  0x39 */
    MT2266_RSVD_3A,        /*  0x3A */
    MT2266_RSVD_3B,        /*  0x3B */
    MT2266_RSVD_3C,        /*  0x3C */
    END_REGS
};

/*
** DefaultsEntry points to an array of U8Data used to initialize
** various registers (the first byte is the starting subaddress)
** and a count of the bytes (including subaddress) in the array.
**
** DefaultsList is an array of DefaultsEntry elements terminated
** by an entry with a NULL pointer for the data array.
*/
typedef struct MT2266_DefaultsEntryTag
{
    U8Data *data;
    UData_t cnt;
} MT2266_DefaultsEntry;

typedef MT2266_DefaultsEntry MT2266_DefaultsList[];

#define DEF_LIST_ENTRY(a) {a, sizeof(a)/sizeof(U8Data) - 1}
#define END_DEF_LIST {0,0}

/*
** Constants used by the tuning algorithm
*/
                                        /* REF_FREQ is now the actual crystal frequency */
#define REF_FREQ          (30000000UL)  /* Reference oscillator Frequency (in Hz) */
#define TUNE_STEP_SIZE          (50UL)  /* Tune in steps of 50 kHz */
#define MIN_UHF_FREQ     (350000000UL)  /* Minimum UHF frequency (in Hz) */
#define MAX_UHF_FREQ     (900000000UL)  /* Maximum UHF frequency (in Hz) */
#define MIN_VHF_FREQ     (174000000UL)  /* Minimum VHF frequency (in Hz) */
#define MAX_VHF_FREQ     (230000000UL)  /* Maximum VHF frequency (in Hz) */
#define OUTPUT_BW          (8000000UL)  /* Output channel bandwidth (in Hz) */
#define UHF_DEFAULT_FREQ (600000000UL)  /* Default UHF input frequency (in Hz) */


/*
**  The number of Tuner Registers
*/
static const UData_t Num_Registers = END_REGS;

/*
**  Crossover Frequency sets for 2 filters, without and with attenuation.
*/
typedef struct
{
    MT2266_XFreq_Set    xfreq[ MT2266_NUMBER_OF_XFREQ_SETS ];

}  MT2266_XFreqs_t;


MT2266_XFreqs_t MT2266_default_XFreqs =
{
    /*  xfreq  */
    {
        /*  uhf0  */
        {                                /*          < 0 MHz: 15+1 */
            0UL,                         /*    0 ..    0 MHz: 15 */
            0UL,                         /*    0 ..  443 MHz: 14 */
            443000 / TUNE_STEP_SIZE,     /*  443 ..  470 MHz: 13 */
            470000 / TUNE_STEP_SIZE,     /*  470 ..  496 MHz: 12 */
            496000 / TUNE_STEP_SIZE,     /*  496 ..  525 MHz: 11 */
            525000 / TUNE_STEP_SIZE,     /*  525 ..  552 MHz: 10 */
            552000 / TUNE_STEP_SIZE,     /*  552 ..  580 MHz:  9 */
            580000 / TUNE_STEP_SIZE,     /*  580 ..  657 MHz:  8 */
            657000 / TUNE_STEP_SIZE,     /*  657 ..  682 MHz:  7 */
            682000 / TUNE_STEP_SIZE,     /*  682 ..  710 MHz:  6 */
            710000 / TUNE_STEP_SIZE,     /*  710 ..  735 MHz:  5 */
            735000 / TUNE_STEP_SIZE,     /*  735 ..  763 MHz:  4 */
            763000 / TUNE_STEP_SIZE,     /*  763 ..  802 MHz:  3 */
            802000 / TUNE_STEP_SIZE,     /*  802 ..  840 MHz:  2 */
            840000 / TUNE_STEP_SIZE,     /*  840 ..  877 MHz:  1 */
            877000 / TUNE_STEP_SIZE      /*  877+        MHz:  0 */
        },

        /*  uhf1  */
        {                                /*        < 443 MHz: 15+1 */
            443000 / TUNE_STEP_SIZE,     /*  443 ..  470 MHz: 15 */
            470000 / TUNE_STEP_SIZE,     /*  470 ..  496 MHz: 14 */
            496000 / TUNE_STEP_SIZE,     /*  496 ..  525 MHz: 13 */
            525000 / TUNE_STEP_SIZE,     /*  525 ..  552 MHz: 12 */
            552000 / TUNE_STEP_SIZE,     /*  552 ..  580 MHz: 11 */
            580000 / TUNE_STEP_SIZE,     /*  580 ..  605 MHz: 10 */
            605000 / TUNE_STEP_SIZE,     /*  605 ..  632 MHz:  9 */
            632000 / TUNE_STEP_SIZE,     /*  632 ..  657 MHz:  8 */
            657000 / TUNE_STEP_SIZE,     /*  657 ..  682 MHz:  7 */
            682000 / TUNE_STEP_SIZE,     /*  682 ..  710 MHz:  6 */
            710000 / TUNE_STEP_SIZE,     /*  710 ..  735 MHz:  5 */
            735000 / TUNE_STEP_SIZE,     /*  735 ..  763 MHz:  4 */
            763000 / TUNE_STEP_SIZE,     /*  763 ..  802 MHz:  3 */
            802000 / TUNE_STEP_SIZE,     /*  802 ..  840 MHz:  2 */
            840000 / TUNE_STEP_SIZE,     /*  840 ..  877 MHz:  1 */
            877000 / TUNE_STEP_SIZE      /*  877+        MHz:  0 */
        },

        /*  uhf0_a  */
        {                                /*        <   0 MHz: 15+1 */
            0UL,                         /*    0 ..    0 MHz: 15 */
            0UL,                         /*    0 ..  442 MHz: 14 */
            442000 / TUNE_STEP_SIZE,     /*  442 ..  472 MHz: 13 */
            472000 / TUNE_STEP_SIZE,     /*  472 ..  505 MHz: 12 */
            505000 / TUNE_STEP_SIZE,     /*  505 ..  535 MHz: 11 */
            535000 / TUNE_STEP_SIZE,     /*  535 ..  560 MHz: 10 */
            560000 / TUNE_STEP_SIZE,     /*  560 ..  593 MHz:  9 */
            593000 / TUNE_STEP_SIZE,     /*  593 ..  673 MHz:  8 */
            673000 / TUNE_STEP_SIZE,     /*  673 ..  700 MHz:  7 */
            700000 / TUNE_STEP_SIZE,     /*  700 ..  727 MHz:  6 */
            727000 / TUNE_STEP_SIZE,     /*  727 ..  752 MHz:  5 */
            752000 / TUNE_STEP_SIZE,     /*  752 ..  783 MHz:  4 */
            783000 / TUNE_STEP_SIZE,     /*  783 ..  825 MHz:  3 */
            825000 / TUNE_STEP_SIZE,     /*  825 ..  865 MHz:  2 */
            865000 / TUNE_STEP_SIZE,     /*  865 ..  905 MHz:  1 */
            905000 / TUNE_STEP_SIZE      /*  905+        MHz:  0 */
        },

        /*  uhf1_a  */
        {                                /*        < 442 MHz: 15+1 */
            442000 / TUNE_STEP_SIZE,     /*  442 ..  472 MHz: 15 */
            472000 / TUNE_STEP_SIZE,     /*  472 ..  505 MHz: 14 */
            505000 / TUNE_STEP_SIZE,     /*  505 ..  535 MHz: 13 */
            535000 / TUNE_STEP_SIZE,     /*  535 ..  560 MHz: 12 */
            560000 / TUNE_STEP_SIZE,     /*  560 ..  593 MHz: 11 */
            593000 / TUNE_STEP_SIZE,     /*  593 ..  620 MHz: 10 */
            620000 / TUNE_STEP_SIZE,     /*  620 ..  647 MHz:  9 */
            647000 / TUNE_STEP_SIZE,     /*  647 ..  673 MHz:  8 */
            673000 / TUNE_STEP_SIZE,     /*  673 ..  700 MHz:  7 */
            700000 / TUNE_STEP_SIZE,     /*  700 ..  727 MHz:  6 */
            727000 / TUNE_STEP_SIZE,     /*  727 ..  752 MHz:  5 */
            752000 / TUNE_STEP_SIZE,     /*  752 ..  783 MHz:  4 */
            783000 / TUNE_STEP_SIZE,     /*  783 ..  825 MHz:  3 */
            825000 / TUNE_STEP_SIZE,     /*  825 ..  865 MHz:  2 */
            865000 / TUNE_STEP_SIZE,     /*  865 ..  905 MHz:  1 */
            905000 / TUNE_STEP_SIZE      /*  905+        MHz:  0 */
        }
    }
};

typedef struct
{
    Handle_t    handle;
    Handle_t    hUserData;
    UData_t     address;
    UData_t     version;
    UData_t     tuner_id;
    UData_t     f_Ref;
    UData_t     f_Step;
    UData_t     f_in;
    UData_t     f_LO;
    UData_t     f_bw;
    UData_t     band;
    UData_t     num_regs;
    U8Data      RC2_Value;
    U8Data      RC2_Nominal;
    U8Data      reg[END_REGS];

    MT2266_XFreqs_t xfreqs;

}  MT2266_Info_t;

static UData_t nMaxTuners = MT2266_CNT;
static MT2266_Info_t MT2266_Info[MT2266_CNT];
static MT2266_Info_t *Avail[MT2266_CNT];
static UData_t nOpenTuners = 0;

/*
**  Constants used to write a minimal set of registers when changing bands.
**  If the user wants a total reset, they should call MT2266_Open() again.
**  Skip 01, 02, 03, 04  (get overwritten anyways)
**  Write 05
**  Skip 06 - 18
**  Write 19   (diff for L-Band)
**  Skip 1A 1B 1C
**  Write 1D - 2B
**  Skip 2C - 3C
*/

static U8Data MT2266_VHF_defaults1[] =
{
    0x05,              /* address 0xC0, reg 0x05 */
    0x04,              /* Reg 0x05 LBANDen = 1 (that's right)*/
};
static U8Data MT2266_VHF_defaults2[] =
{
    0x19,              /* address 0xC0, reg 0x19 */
    0x61,              /* Reg 0x19 CAPto = 3*/
};
static U8Data MT2266_VHF_defaults3[] =
{
    0x1D,              /* address 0xC0, reg 0x1D */
    0xFE,              /* reg 0x1D */
    0x00,              /* reg 0x1E */
    0x00,              /* reg 0x1F */
    0xB4,              /* Reg 0x20 GPO = 1*/
    0x03,              /* Reg 0x21 LBIASen = 1, UBIASen = 1*/
    0xA5,              /* Reg 0x22 */
    0xA5,              /* Reg 0x23 */
    0xA5,              /* Reg 0x24 */
    0xA5,              /* Reg 0x25 */
    0x82,              /* Reg 0x26 CASCM = b0001 (bits reversed)*/
    0xAA,              /* Reg 0x27 */
    0xF1,              /* Reg 0x28 */
    0x17,              /* Reg 0x29 */
    0x80,              /* Reg 0x2A MIXbiasen = 1*/
    0x1F,              /* Reg 0x2B */
};

static MT2266_DefaultsList MT2266_VHF_defaults = {
    DEF_LIST_ENTRY(MT2266_VHF_defaults1),
    DEF_LIST_ENTRY(MT2266_VHF_defaults2),
    DEF_LIST_ENTRY(MT2266_VHF_defaults3),
    END_DEF_LIST
};

static U8Data MT2266_UHF_defaults1[] =
{
    0x05,              /* address 0xC0, reg 0x05 */
    0x52,              /* Reg 0x05 */
};
static U8Data MT2266_UHF_defaults2[] =
{
    0x19,              /* address 0xC0, reg 0x19 */
    0x61,              /* Reg 0x19 CAPto = 3*/
};
static U8Data MT2266_UHF_defaults3[] =
{
    0x1D,              /* address 0xC0, reg 0x1D */
    0xDC,              /* Reg 0x1D */
    0x00,              /* Reg 0x1E */
    0x0A,              /* Reg 0x1F */
    0xD4,              /* Reg 0x20 GPO = 1*/
    0x03,              /* Reg 0x21 LBIASen = 1, UBIASen = 1*/
    0x64,              /* Reg 0x22 */
    0x64,              /* Reg 0x23 */
    0x64,              /* Reg 0x24 */
    0x64,              /* Reg 0x25 */
    0x22,              /* Reg 0x26 CASCM = b0100 (bits reversed)*/
    0xAA,              /* Reg 0x27 */
    0xF2,              /* Reg 0x28 */
    0x1E,              /* Reg 0x29 */
    0x80,              /* Reg 0x2A MIXbiasen = 1*/
    0x14,              /* Reg 0x2B */
};

static MT2266_DefaultsList MT2266_UHF_defaults = {
    DEF_LIST_ENTRY(MT2266_UHF_defaults1),
    DEF_LIST_ENTRY(MT2266_UHF_defaults2),
    DEF_LIST_ENTRY(MT2266_UHF_defaults3),
    END_DEF_LIST
};


static UData_t UncheckedSet(MT2266_Info_t* pInfo,
                            U8Data         reg,
                            U8Data         val);

static UData_t UncheckedGet(MT2266_Info_t* pInfo,
                            U8Data   reg,
                            U8Data*  val);


/******************************************************************************
**
**  Name: MT2266_Open
**
**  Description:    Initialize the tuner's register values.
**
**  Parameters:     MT2266_Addr  - Serial bus address of the tuner.
**                  hMT2266      - Tuner handle passed back.
**                  hUserData    - User-defined data, if needed for the
**                                 MT2266_ReadSub() & MT2266_WriteSub functions.
**
**  Returns:        status:
**                      MT_OK             - No errors
**                      MT_TUNER_ID_ERR   - Tuner Part/Rev code mismatch
**                      MT_TUNER_INIT_ERR - Tuner initialization failed
**                      MT_COMM_ERR       - Serial bus communications error
**                      MT_ARG_NULL       - Null pointer argument passed
**                      MT_TUNER_CNT_ERR  - Too many tuners open
**
**  Dependencies:   MT2266_ReadSub  - Read byte(s) of data from the two-wire bus
**                  MT2266_WriteSub - Write byte(s) of data to the two-wire bus
**
**  Revision History:
**
**   SCR      Date      Author  Description
**  -------------------------------------------------------------------------
**   N/A   05-30-2006    DAD    Ver 1.0: Modified version of mt2260.c (Ver 1.01).
**   N/A   11-01-2006    RSK    Ver 1.02: Initialize Crossover Tables to Default
**
******************************************************************************/
UData_t MT2266_Open(UData_t MT2266_Addr,
                    Handle_t* hMT2266,
                    Handle_t hUserData)
{
    UData_t status = MT_OK;             /*  Status to be returned.  */
    SData_t i, j;
    MT2266_Info_t* pInfo = NULL;

    /*  Check the argument before using  */
    if (hMT2266 == NULL)
        return MT_ARG_NULL;
    *hMT2266 = NULL;

    /*
    **  If this is our first tuner, initialize the address fields and
    **  the list of available control blocks.
    */
    if (nOpenTuners == 0)
    {
        for (i=MT2266_CNT-1; i>=0; i--)
        {
            MT2266_Info[i].handle = NULL;
            MT2266_Info[i].address = MAX_UDATA;
            MT2266_Info[i].hUserData = NULL;

            /* Reset the UHF Crossover Frequency tables on open/init. */
            for (j=0; j< MT2266_NUM_XFREQS; j++ )
            {
                MT2266_Info[i].xfreqs.xfreq[MT2266_UHF0][j]       = MT2266_default_XFreqs.xfreq[MT2266_UHF0][j];
                MT2266_Info[i].xfreqs.xfreq[MT2266_UHF1][j]       = MT2266_default_XFreqs.xfreq[MT2266_UHF1][j];
                MT2266_Info[i].xfreqs.xfreq[MT2266_UHF0_ATTEN][j] = MT2266_default_XFreqs.xfreq[MT2266_UHF0_ATTEN][j];
                MT2266_Info[i].xfreqs.xfreq[MT2266_UHF1_ATTEN][j] = MT2266_default_XFreqs.xfreq[MT2266_UHF1_ATTEN][j];
            }

            Avail[i] = &MT2266_Info[i];
        }
    }

    /*
    **  Look for an existing MT2266_State_t entry with this address.
    */
    for (i=MT2266_CNT-1; i>=0; i--)
    {
        /*
        **  If an open'ed handle provided, we'll re-initialize that structure.
        **
        **  We recognize an open tuner because the address and hUserData are
        **  the same as one that has already been opened
        */
        if ((MT2266_Info[i].address == MT2266_Addr) &&
            (MT2266_Info[i].hUserData == hUserData))
        {
            pInfo = &MT2266_Info[i];
            break;
        }
    }

    /*  If not found, choose an empty spot.  */
    if (pInfo == NULL)
    {
        /*  Check to see that we're not over-allocating.  */
        if (nOpenTuners == MT2266_CNT)
            return MT_TUNER_CNT_ERR;

        /* Use the next available block from the list */
        pInfo = Avail[nOpenTuners];
        nOpenTuners++;
    }

    pInfo->handle = (Handle_t) pInfo;
    pInfo->hUserData = hUserData;
    pInfo->address = MT2266_Addr;

    status |= MT2266_ReInit((Handle_t) pInfo);

    if (MT_IS_ERROR(status))
        MT2266_Close((Handle_t) pInfo);
    else
        *hMT2266 = pInfo->handle;

    return (status);
}


static UData_t IsValidHandle(MT2266_Info_t* handle)
{
    return ((handle != NULL) && (handle->handle == handle)) ? 1 : 0;
}


/******************************************************************************
**
**  Name: MT2266_Close
**
**  Description:    Release the handle to the tuner.
**
**  Parameters:     hMT2266      - Handle to the MT2266 tuner
**
**  Returns:        status:
**                      MT_OK         - No errors
**                      MT_INV_HANDLE - Invalid tuner handle
**
**  Dependencies:   mt_errordef.h - definition of error codes
**
**  Revision History:
**
**   SCR      Date      Author  Description
**  -------------------------------------------------------------------------
**   N/A   05-30-2006    DAD    Ver 1.0: Modified version of mt2260.c (Ver 1.01).
**
******************************************************************************/
UData_t MT2266_Close(Handle_t hMT2266)
{
    MT2266_Info_t* pInfo = (MT2266_Info_t*) hMT2266;

    if (IsValidHandle(pInfo) == 0)
        return (MT_INV_HANDLE);

    /* Remove the tuner from our list of tuners */
    pInfo->handle = NULL;
    pInfo->address = MAX_UDATA;
    pInfo->hUserData = NULL;
    nOpenTuners--;
    Avail[nOpenTuners] = pInfo; /* Return control block to available list */

    return MT_OK;
}


/******************************************************************************
**
**  Name: Run_BB_RC_Cal2
**
**  Description:    Run Base Band RC Calibration (Method 2)
**                  MT2266 B0 only, others return MT_OK
**
**  Parameters:     hMT2266      - Handle to the MT2266 tuner
**
**  Returns:        status:
**                      MT_OK            - No errors
**                      MT_COMM_ERR      - Serial bus communications error
**                      MT_INV_HANDLE    - Invalid tuner handle
**
**  Dependencies:   mt_errordef.h - definition of error codes
**
**  Revision History:
**
**   SCR      Date      Author  Description
**  -------------------------------------------------------------------------
**   N/A   05-30-2006    DAD    Ver 1.0: Modified version of mt2260.c (Ver 1.01).
**
******************************************************************************/
static UData_t Run_BB_RC_Cal2(Handle_t h)
{
    UData_t status = MT_OK;                  /* Status to be returned */
    U8Data tmp_rcc;
    U8Data dumy;

    MT2266_Info_t* pInfo = (MT2266_Info_t*) h;

    /*  Verify that the handle passed points to a valid tuner         */
    if (IsValidHandle(pInfo) == 0)
        return (MT_INV_HANDLE);

    /*
    ** Set the crystal frequency in the calibration register
    ** and enable RC calibration #2
    */
    PREFETCH(MT2266_RCC_CTRL, 1);  /* Fetch register(s) if __NO_CACHE__ defined */
    tmp_rcc = pInfo->reg[MT2266_RCC_CTRL];
    if (pInfo->f_Ref < (36000000 /*/ TUNE_STEP_SIZE*/))
        tmp_rcc = (tmp_rcc & 0xDF) | 0x10;
    else
        tmp_rcc |= 0x30;
    status |= UncheckedSet(pInfo, MT2266_RCC_CTRL, tmp_rcc);

    /*  Read RC Calibration value  */
    status |= UncheckedGet(pInfo, MT2266_STATUS_4, &dumy);

    /* Disable RC Cal 2 */
    status |= UncheckedSet(pInfo, MT2266_RCC_CTRL, pInfo->reg[MT2266_RCC_CTRL] & 0xEF);

    /* Store RC Cal 2 value */
    pInfo->RC2_Value = pInfo->reg[MT2266_STATUS_4];

    if (pInfo->f_Ref < (36000000 /*/ TUNE_STEP_SIZE*/))
        pInfo->RC2_Nominal = (U8Data) ((pInfo->f_Ref + 77570) / 155139);
    else
        pInfo->RC2_Nominal = (U8Data) ((pInfo->f_Ref + 93077) / 186154);

    return (status);
}


/******************************************************************************
**
**  Name: Set_BBFilt
**
**  Description:    Set Base Band Filter bandwidth
**                  Based on SRO frequency & BB RC Calibration
**                  User stores channel bw as 5-8 MHz.  This routine
**                  calculates a 3 dB corner bw based on 1/2 the bandwidth
**                  and a bandwidth related constant.
**
**  Parameters:     hMT2266      - Handle to the MT2266 tuner
**
**  Returns:        status:
**                      MT_OK            - No errors
**                      MT_COMM_ERR      - Serial bus communications error
**                      MT_INV_HANDLE    - Invalid tuner handle
**
**  Dependencies:   mt_errordef.h - definition of error codes
**
**  Revision History:
**
**   SCR      Date      Author  Description
**  -------------------------------------------------------------------------
**   N/A   05-30-2006    DAD    Ver 1.0: Modified version of mt2260.c (Ver 1.01).
**
******************************************************************************/
static UData_t Set_BBFilt(Handle_t h)
{
    UData_t f_3dB_bw;
    U8Data BBFilt = 0;
    U8Data Sel = 0;
    SData_t TmpFilt;
    SData_t i;
    UData_t status = MT_OK;                  /* Status to be returned */

    MT2266_Info_t* pInfo = (MT2266_Info_t*) h;

    /*  Verify that the handle passed points to a valid tuner         */
    if (IsValidHandle(pInfo) == 0)
        return (MT_INV_HANDLE);

	if (pInfo->RC2_Value == 0)
		return (MT_COMM_ERR);

	/*
    ** Convert the channel bandwidth into a 3 dB bw by dividing it by 2
    ** and subtracting 300, 250, 200, or 0 kHz based on 8, 7, 6, 5 MHz
    ** channel bandwidth.
    */
    f_3dB_bw = (pInfo->f_bw / 2);  /* bw -> bw/2 */
    if (pInfo->f_bw > 7500000)
    {
        /*  >3.75 MHz corner  */
        f_3dB_bw -= 300000;
        Sel = 0x00;
        TmpFilt = ((429916107 / pInfo->RC2_Value) * pInfo->RC2_Nominal) / f_3dB_bw - 81;
    }
    else if (pInfo->f_bw > 6500000)
    {
        /*  >3.25 MHz .. 3.75 MHz corner  */
        f_3dB_bw -= 250000;
        Sel = 0x00;
        TmpFilt = ((429916107 / pInfo->RC2_Value) * pInfo->RC2_Nominal) / f_3dB_bw - 81;
    }
    else if (pInfo->f_bw > 5500000)
    {
        /*  >2.75 MHz .. 3.25 MHz corner  */
        f_3dB_bw -= 200000;
        Sel = 0x80;
        TmpFilt = ((429916107 / pInfo->RC2_Value) * pInfo->RC2_Nominal) / f_3dB_bw - 113;
    }
    else
    {
        /*  <= 2.75 MHz corner  */
        Sel = 0xC0;
        TmpFilt = ((429916107 / pInfo->RC2_Value) * pInfo->RC2_Nominal) / f_3dB_bw - 129;
    }

    if (TmpFilt > 63)
        TmpFilt = 63;
    else if (TmpFilt < 0)
        TmpFilt = 0;
    BBFilt = ((U8Data) TmpFilt) | Sel;

    for ( i = MT2266_BBFILT_1; i <= MT2266_BBFILT_8; i++ )
        pInfo->reg[i] = BBFilt;

    if (MT_NO_ERROR(status))
        status |= MT2266_WriteSub(pInfo->hUserData,
                              pInfo->address,
                              MT2266_BBFILT_1,
                              &pInfo->reg[MT2266_BBFILT_1],
                              8);

    return (status);
}


/****************************************************************************
**
**  Name: MT2266_GetLocked
**
**  Description:    Checks to see if the PLL is locked.
**
**  Parameters:     h            - Open handle to the tuner (from MT2266_Open).
**
**  Returns:        status:
**                      MT_OK            - No errors
**                      MT_DNC_UNLOCK    - Downconverter PLL unlocked
**                      MT_COMM_ERR      - Serial bus communications error
**                      MT_INV_HANDLE    - Invalid tuner handle
**
**  Dependencies:   MT2266_ReadSub    - Read byte(s) of data from the serial bus
**                  MT_Sleep      - Delay execution for x milliseconds
**
**  Revision History:
**
**   SCR      Date      Author  Description
**  -------------------------------------------------------------------------
**   N/A   05-30-2006    DAD    Ver 1.0: Modified version of mt2260.c (Ver 1.01).
**
****************************************************************************/
UData_t MT2266_GetLocked(Handle_t h)
{
    const UData_t nMaxWait = 200;            /*  wait a maximum of 200 msec   */
    const UData_t nPollRate = 2;             /*  poll status bits every 2 ms */
    const UData_t nMaxLoops = nMaxWait / nPollRate;
    UData_t status = MT_OK;                  /* Status to be returned */
    UData_t nDelays = 0;
    U8Data statreg;
    MT2266_Info_t* pInfo = (MT2266_Info_t*) h;

    if (IsValidHandle(pInfo) == 0)
        return (MT_INV_HANDLE);

    do
    {
        status |= UncheckedGet(pInfo, MT2266_STATUS_1, &statreg);

        if ((MT_IS_ERROR(status)) || ((statreg & 0x40) == 0x40))
            return (status);

        MT2266_Sleep(pInfo->hUserData, nPollRate);       /*  Wait between retries  */
    }
    while (++nDelays < nMaxLoops);

    if ((statreg & 0x40) != 0x40)
        status |= MT_DNC_UNLOCK;

    return (status);
}


/****************************************************************************
**
**  Name: MT2266_GetParam
**
**  Description:    Gets a tuning algorithm parameter.
**
**                  This function provides access to the internals of the
**                  tuning algorithm - mostly for testing purposes.
**
**  Parameters:     h           - Tuner handle (returned by MT2266_Open)
**                  param       - Tuning algorithm parameter
**                                (see enum MT2266_Param)
**                  pValue      - ptr to returned value
**
**                  param                   Description
**                  ----------------------  --------------------------------
**                  MT2266_IC_ADDR          Serial Bus address of this tuner
**                  MT2266_MAX_OPEN         Max number of MT2266's that can be open
**                  MT2266_NUM_OPEN         Number of MT2266's currently open
**                  MT2266_NUM_REGS         Number of tuner registers
**                  MT2266_SRO_FREQ         crystal frequency
**                  MT2266_STEPSIZE         minimum tuning step size
**                  MT2266_INPUT_FREQ       input center frequency
**                  MT2266_LO_FREQ          LO Frequency
**                  MT2266_OUTPUT_BW        Output channel bandwidth
**                  MT2266_RC2_VALUE        Base band filter cal RC code (method 2)
**                  MT2266_RC2_NOMINAL      Base band filter nominal cal RC code
**                  MT2266_RF_ADC           RF attenuator A/D readback
**                  MT2266_RF_ATTN          RF attenuation (0-255)
**                  MT2266_RF_EXT           External control of RF atten
**                  MT2266_LNA_GAIN         LNA gain setting (0-15)
**                  MT2266_BB_ADC           BB attenuator A/D readback
**                  MT2266_BB_ATTN          Baseband attenuation (0-255)
**                  MT2266_BB_EXT           External control of BB atten
**
**  Usage:          status |= MT2266_GetParam(hMT2266,
**                                            MT2266_OUTPUT_BW,
**                                            &f_bw);
**
**  Returns:        status:
**                      MT_OK            - No errors
**                      MT_INV_HANDLE    - Invalid tuner handle
**                      MT_ARG_NULL      - Null pointer argument passed
**                      MT_ARG_RANGE     - Invalid parameter requested
**
**  Dependencies:   USERS MUST CALL MT2266_Open() FIRST!
**
**  See Also:       MT2266_SetParam, MT2266_Open
**
**  Revision History:
**
**   SCR      Date      Author  Description
**  -------------------------------------------------------------------------
**   N/A   05-30-2006    DAD    Ver 1.0: Modified version of mt2260.c (Ver 1.01).
**
****************************************************************************/
UData_t MT2266_GetParam(Handle_t     h,
                        MT2266_Param param,
                        UData_t*     pValue)
{
    UData_t status = MT_OK;                  /* Status to be returned        */
    U8Data tmp;
    MT2266_Info_t* pInfo = (MT2266_Info_t*) h;

    if (pValue == NULL)
        status |= MT_ARG_NULL;

    /*  Verify that the handle passed points to a valid tuner         */
    if (IsValidHandle(pInfo) == 0)
        return (MT_INV_HANDLE);

    if (MT_NO_ERROR(status))
    {
        switch (param)
        {
        /*  Serial Bus address of this tuner      */
        case MT2266_IC_ADDR:
            *pValue = pInfo->address;
            break;

        /*  Max # of MT2266's allowed to be open  */
        case MT2266_MAX_OPEN:
            *pValue = nMaxTuners;
            break;

        /*  # of MT2266's open                    */
        case MT2266_NUM_OPEN:
            *pValue = nOpenTuners;
            break;

        /*  Number of tuner registers             */
        case MT2266_NUM_REGS:
            *pValue = Num_Registers;
            break;

        /*  crystal frequency                     */
        case MT2266_SRO_FREQ:
            *pValue = pInfo->f_Ref;
            break;

        /*  minimum tuning step size              */
        case MT2266_STEPSIZE:
            *pValue = pInfo->f_Step;
            break;

        /*  input center frequency                */
        case MT2266_INPUT_FREQ:
            *pValue = pInfo->f_in;
            break;

        /*  LO Frequency                          */
        case MT2266_LO_FREQ:
            *pValue = pInfo->f_LO;
            break;

        /*  Output Channel Bandwidth              */
        case MT2266_OUTPUT_BW:
            *pValue = pInfo->f_bw;
            break;

        /*  Base band filter cal RC code          */
        case MT2266_RC2_VALUE:
            *pValue = (UData_t) pInfo->RC2_Value;
            break;

        /*  Base band filter nominal cal RC code          */
        case MT2266_RC2_NOMINAL:
            *pValue = (UData_t) pInfo->RC2_Nominal;
            break;

        /*  RF attenuator A/D readback            */
        case MT2266_RF_ADC:
            status |= UncheckedGet(pInfo, MT2266_STATUS_2, &tmp);
            if (MT_NO_ERROR(status))
                *pValue = (UData_t) tmp;
            break;

        /*  BB attenuator A/D readback            */
        case MT2266_BB_ADC:
            status |= UncheckedGet(pInfo, MT2266_STATUS_3, &tmp);
            if (MT_NO_ERROR(status))
                *pValue = (UData_t) tmp;
            break;

        /*  RF attenuator setting                 */
        case MT2266_RF_ATTN:
            PREFETCH(MT2266_RSVD_1F, 1);  /* Fetch register(s) if __NO_CACHE__ defined */
            if (MT_NO_ERROR(status))
                *pValue = pInfo->reg[MT2266_RSVD_1F];
            break;

        /*  BB attenuator setting                 */
        case MT2266_BB_ATTN:
            PREFETCH(MT2266_RSVD_2C, 3);  /* Fetch register(s) if __NO_CACHE__ defined */
            *pValue = pInfo->reg[MT2266_RSVD_2C]
                    + pInfo->reg[MT2266_RSVD_2D]
                    + pInfo->reg[MT2266_RSVD_2E] - 3;
            break;

        /*  RF external / internal atten control  */
        case MT2266_RF_EXT:
            PREFETCH(MT2266_GPO, 1);  /* Fetch register(s) if __NO_CACHE__ defined */
            *pValue = ((pInfo->reg[MT2266_GPO] & 0x40) != 0x00);
            break;

        /*  BB external / internal atten control  */
        case MT2266_BB_EXT:
            PREFETCH(MT2266_RSVD_33, 1);  /* Fetch register(s) if __NO_CACHE__ defined */
            *pValue = ((pInfo->reg[MT2266_RSVD_33] & 0x10) != 0x00);
            break;

        /*  LNA gain setting (0-15)               */
        case MT2266_LNA_GAIN:
            PREFETCH(MT2266_IGAIN, 1);  /* Fetch register(s) if __NO_CACHE__ defined */
            *pValue = ((pInfo->reg[MT2266_IGAIN] & 0x3C) >> 2);
            break;

        case MT2266_EOP:
        default:
            status |= MT_ARG_RANGE;
        }
    }
    return (status);
}


/****************************************************************************
**  LOCAL FUNCTION - DO NOT USE OUTSIDE OF mt2266.c
**
**  Name: UncheckedGet
**
**  Description:    Gets an MT2266 register with minimal checking
**
**                  NOTE: This is a local function that performs the same
**                  steps as the MT2266_GetReg function that is available
**                  in the external API.  It does not do any of the standard
**                  error checking that the API function provides and should
**                  not be called from outside this file.
**
**  Parameters:     *pInfo      - Tuner control structure
**                  reg         - MT2266 register/subaddress location
**                  *val        - MT2266 register/subaddress value
**
**  Returns:        status:
**                      MT_OK            - No errors
**                      MT_COMM_ERR      - Serial bus communications error
**                      MT_INV_HANDLE    - Invalid tuner handle
**                      MT_ARG_NULL      - Null pointer argument passed
**                      MT_ARG_RANGE     - Argument out of range
**
**  Dependencies:   USERS MUST CALL MT2266_Open() FIRST!
**
**                  Use this function if you need to read a register from
**                  the MT2266.
**
**  Revision History:
**
**   SCR      Date      Author  Description
**  -------------------------------------------------------------------------
**   N/A   05-30-2006    DAD    Ver 1.0: Modified version of mt2260.c (Ver 1.01).
**
****************************************************************************/
static UData_t UncheckedGet(MT2266_Info_t* pInfo,
                            U8Data   reg,
                            U8Data*  val)
{
    UData_t status;                  /* Status to be returned        */

#if defined(_DEBUG)
    status = MT_OK;
    /*  Verify that the handle passed points to a valid tuner         */
    if (IsValidHandle(pInfo) == 0)
        return (MT_INV_HANDLE);

    if (val == NULL)
        status |= MT_ARG_NULL;

    if (reg >= END_REGS)
        status |= MT_ARG_RANGE;

    if (MT_IS_ERROR(status))
        return(status);
#endif

    status = MT2266_ReadSub(pInfo->hUserData, pInfo->address, reg, &pInfo->reg[reg], 1);

    if (MT_NO_ERROR(status))
        *val = pInfo->reg[reg];

    return (status);
}


/****************************************************************************
**
**  Name: MT2266_GetReg
**
**  Description:    Gets an MT2266 register.
**
**  Parameters:     h           - Tuner handle (returned by MT2266_Open)
**                  reg         - MT2266 register/subaddress location
**                  *val        - MT2266 register/subaddress value
**
**  Returns:        status:
**                      MT_OK            - No errors
**                      MT_COMM_ERR      - Serial bus communications error
**                      MT_INV_HANDLE    - Invalid tuner handle
**                      MT_ARG_NULL      - Null pointer argument passed
**                      MT_ARG_RANGE     - Argument out of range
**
**  Dependencies:   USERS MUST CALL MT2266_Open() FIRST!
**
**                  Use this function if you need to read a register from
**                  the MT2266.
**
**  Revision History:
**
**   SCR      Date      Author  Description
**  -------------------------------------------------------------------------
**   N/A   05-30-2006    DAD    Ver 1.0: Modified version of mt2260.c (Ver 1.01).
**
****************************************************************************/
UData_t MT2266_GetReg(Handle_t h,
                      U8Data   reg,
                      U8Data*  val)
{
    UData_t status = MT_OK;                  /* Status to be returned        */
    MT2266_Info_t* pInfo = (MT2266_Info_t*) h;

    /*  Verify that the handle passed points to a valid tuner         */
    if (IsValidHandle(pInfo) == 0)
        return (MT_INV_HANDLE);

    if (val == NULL)
        status |= MT_ARG_NULL;

    if (reg >= END_REGS)
        status |= MT_ARG_RANGE;

    if (MT_NO_ERROR(status))
        status |= UncheckedGet(pInfo, reg, val);

    return (status);
}


/****************************************************************************
**
**  Name: MT2266_GetUHFXFreqs
**
**  Description:    Retrieves the specified set of UHF Crossover Frequencies
**
**  Parameters:     h            - Open handle to the tuner (from MT2266_Open).
**
**  Usage:          MT2266_Freq_Set  tmpFreqs;
**                  status = MT2266_GetUHFXFreqs(hMT2266,
**                                               MT2266_UHF1_WITH_ATTENUATION,
**                                               tmpFreqs );
**                  if (status & MT_ARG_RANGE)
**                      // error, Invalid UHF Crossover Frequency Set requested.
**                  else
**                      for( int i = 0;  i < MT2266_NUM_XFREQS; i++ )
**                         . . .
**
**
**  Returns:        status:
**                      MT_OK            - No errors
**                      MT_ARG_RANGE     - freq_type is out of range.
**                      MT_INV_HANDLE    - Invalid tuner handle
**
**  Dependencies:   freqs_buffer *must* be defined of type MT2266_Freq_Set
**                     to assure sufficient space allocation!
**
**                  USERS MUST CALL MT2266_Open() FIRST!
**
**  See Also:       MT2266_SetUHFXFreqs
**
**  Revision History:
**
**   SCR      Date      Author  Description
**  -------------------------------------------------------------------------
**   N/A   10-26-2006   RSK     Original.
**
****************************************************************************/
UData_t MT2266_GetUHFXFreqs(Handle_t h,
                            MT2266_UHFXFreq_Type freq_type,
							MT2266_XFreq_Set     freqs_buffer)
{
    UData_t status = MT_OK;                  /* Status to be returned        */
    MT2266_Info_t* pInfo = (MT2266_Info_t*) h;

    /*  Verify that the handle passed points to a valid tuner         */
    if (IsValidHandle(pInfo) == 0)
        return (MT_INV_HANDLE);

    if (freq_type >= MT2266_NUMBER_OF_XFREQ_SETS)
        status |= MT_ARG_RANGE;

    if (MT_NO_ERROR(status))
    {
        int  i;

        for( i = 0; i < MT2266_NUM_XFREQS; i++ )
        {
            freqs_buffer[i] = pInfo->xfreqs.xfreq[ freq_type ][i] * TUNE_STEP_SIZE / 1000;
        }
    }

    return (status);
}


/****************************************************************************
**
**  Name: MT2266_GetUserData
**
**  Description:    Gets the user-defined data item.
**
**  Parameters:     h           - Tuner handle (returned by MT2266_Open)
**
**  Returns:        status:
**                      MT_OK            - No errors
**                      MT_INV_HANDLE    - Invalid tuner handle
**                      MT_ARG_NULL      - Null pointer argument passed
**
**  Dependencies:   USERS MUST CALL MT2266_Open() FIRST!
**
**                  The hUserData parameter is a user-specific argument
**                  that is stored internally with the other tuner-
**                  specific information.
**
**                  For example, if additional arguments are needed
**                  for the user to identify the device communicating
**                  with the tuner, this argument can be used to supply
**                  the necessary information.
**
**                  The hUserData parameter is initialized in the tuner's
**                  Open function to NULL.
**
**  See Also:       MT2266_SetUserData, MT2266_Open
**
**  Revision History:
**
**   SCR      Date      Author  Description
**  -------------------------------------------------------------------------
**   N/A   05-30-2006    DAD    Ver 1.0: Modified version of mt2260.c (Ver 1.01).
**
****************************************************************************/
UData_t MT2266_GetUserData(Handle_t h,
                           Handle_t* hUserData)
{
    UData_t status = MT_OK;                  /* Status to be returned        */
    MT2266_Info_t* pInfo = (MT2266_Info_t*) h;

    /*  Verify that the handle passed points to a valid tuner         */
    if (IsValidHandle(pInfo) == 0)
        return (MT_INV_HANDLE);

    if (hUserData == NULL)
        status |= MT_ARG_NULL;

    if (MT_NO_ERROR(status))
        *hUserData = pInfo->hUserData;

    return (status);
}


/******************************************************************************
**
**  Name: MT2266_ReInit
**
**  Description:    Initialize the tuner's register values.
**
**  Parameters:     h           - Tuner handle (returned by MT2266_Open)
**
**  Returns:        status:
**                      MT_OK             - No errors
**                      MT_TUNER_ID_ERR   - Tuner Part/Rev code mismatch
**                      MT_TUNER_INIT_ERR - Tuner initialization failed
**                      MT_INV_HANDLE     - Invalid tuner handle
**                      MT_COMM_ERR       - Serial bus communications error
**
**  Dependencies:   MT2266_ReadSub  - Read byte(s) of data from the two-wire bus
**                  MT2266_WriteSub - Write byte(s) of data to the two-wire bus
**
**  Revision History:
**
**   SCR      Date      Author  Description
**  -------------------------------------------------------------------------
**   N/A   05-30-2006    DAD    Ver 1.0: Modified version of mt2260.c (Ver 1.01).
**   N/A   06-08-2006    JWS    Ver 1.01: Corrected problem with tuner ID check
**   N/A   11-01-2006    RSK    Ver 1.02: Initialize XFreq Tables to Default
**   N/A   11-29-2006    DAD    Ver 1.03: Parenthesis clarification
**
******************************************************************************/
UData_t MT2266_ReInit(Handle_t h)
{
    int j;

    U8Data MT2266_Init_Defaults1[] =
    {
        0x01,            /* Start w/register 0x01 */
        0x00,            /* Reg 0x01 */
        0x00,            /* Reg 0x02 */
        0x28,            /* Reg 0x03 */
        0x00,            /* Reg 0x04 */
        0x52,            /* Reg 0x05 */
        0x99,            /* Reg 0x06 */
        0x3F,            /* Reg 0x07 */
    };

    U8Data MT2266_Init_Defaults2[] =
    {
        0x17,            /* Start w/register 0x17 */
        0x6D,            /* Reg 0x17 */
        0x71,            /* Reg 0x18 */
        0x61,            /* Reg 0x19 */
        0xC0,            /* Reg 0x1A */
        0xBF,            /* Reg 0x1B */
        0xFF,            /* Reg 0x1C */
        0xDC,            /* Reg 0x1D */
        0x00,            /* Reg 0x1E */
        0x0A,            /* Reg 0x1F */
        0xD4,            /* Reg 0x20 */
        0x03,            /* Reg 0x21 */
        0x64,            /* Reg 0x22 */
        0x64,            /* Reg 0x23 */
        0x64,            /* Reg 0x24 */
        0x64,            /* Reg 0x25 */
        0x22,            /* Reg 0x26 */
        0xAA,            /* Reg 0x27 */
        0xF2,            /* Reg 0x28 */
        0x1E,            /* Reg 0x29 */
        0x80,            /* Reg 0x2A */
        0x14,            /* Reg 0x2B */
        0x01,            /* Reg 0x2C */
        0x01,            /* Reg 0x2D */
        0x01,            /* Reg 0x2E */
        0x01,            /* Reg 0x2F */
        0x01,            /* Reg 0x30 */
        0x01,            /* Reg 0x31 */
        0x7F,            /* Reg 0x32 */
        0x5E,            /* Reg 0x33 */
        0x3F,            /* Reg 0x34 */
        0xFF,            /* Reg 0x35 */
        0xFF,            /* Reg 0x36 */
        0xFF,            /* Reg 0x37 */
        0x00,            /* Reg 0x38 */
        0x77,            /* Reg 0x39 */
        0x0F,            /* Reg 0x3A */
        0x2D,            /* Reg 0x3B */
    };

    UData_t status = MT_OK;                  /* Status to be returned        */
    MT2266_Info_t* pInfo = (MT2266_Info_t*) h;
    U8Data BBVref;
    U8Data tmpreg = 0;
    U8Data statusreg = 0;

    /*  Verify that the handle passed points to a valid tuner         */
    if (IsValidHandle(pInfo) == 0)
        return (MT_INV_HANDLE);

    /*  Read the Part/Rev code from the tuner */
    if (MT_NO_ERROR(status))
        status |= UncheckedGet(pInfo, MT2266_PART_REV, &tmpreg);
    if (MT_NO_ERROR(status) && (tmpreg != 0x85))  /* MT226? B0 */
        status |= MT_TUNER_ID_ERR;
    else
    {
        /*
        **  Read the status register 5
        */
        tmpreg = pInfo->reg[MT2266_RSVD_11] |= 0x03;
        if (MT_NO_ERROR(status))
            status |= UncheckedSet(pInfo, MT2266_RSVD_11, tmpreg);
        tmpreg &= ~(0x02);
        if (MT_NO_ERROR(status))
            status |= UncheckedSet(pInfo, MT2266_RSVD_11, tmpreg);

        /*  Get and store the status 5 register value  */
        if (MT_NO_ERROR(status))
            status |= UncheckedGet(pInfo, MT2266_STATUS_5, &statusreg);

        /*  MT2266  */
        if (MT_IS_ERROR(status) || ((statusreg & 0x30) != 0x30))
                status |= MT_TUNER_ID_ERR;      /*  Wrong tuner Part/Rev code   */
    }

    if (MT_NO_ERROR(status))
    {
        /*  Initialize the tuner state.  Hold off on f_in and f_LO */
        pInfo->version = VERSION;
        pInfo->tuner_id = pInfo->reg[MT2266_PART_REV];
        pInfo->f_Ref = REF_FREQ;
        pInfo->f_Step = TUNE_STEP_SIZE * 1000;  /* kHz -> Hz */
        pInfo->f_in = UHF_DEFAULT_FREQ;
        pInfo->f_LO = UHF_DEFAULT_FREQ;
        pInfo->f_bw = OUTPUT_BW;
        pInfo->band = MT2266_UHF_BAND;
        pInfo->num_regs = END_REGS;

        /* Reset the UHF Crossover Frequency tables on open/init. */
        for (j=0; j< MT2266_NUM_XFREQS; j++ )
        {
            pInfo->xfreqs.xfreq[MT2266_UHF0][j]       = MT2266_default_XFreqs.xfreq[MT2266_UHF0][j];
            pInfo->xfreqs.xfreq[MT2266_UHF1][j]       = MT2266_default_XFreqs.xfreq[MT2266_UHF1][j];
            pInfo->xfreqs.xfreq[MT2266_UHF0_ATTEN][j] = MT2266_default_XFreqs.xfreq[MT2266_UHF0_ATTEN][j];
            pInfo->xfreqs.xfreq[MT2266_UHF1_ATTEN][j] = MT2266_default_XFreqs.xfreq[MT2266_UHF1_ATTEN][j];
        }

        /*  Write the default values to the tuner registers. Default mode is UHF */
        status |= MT2266_WriteSub(pInfo->hUserData,
                              pInfo->address,
                              MT2266_Init_Defaults1[0],
                              &MT2266_Init_Defaults1[1],
                              sizeof(MT2266_Init_Defaults1)/sizeof(U8Data)-1);
        if (MT_NO_ERROR(status))
        {
            status |= MT2266_WriteSub(pInfo->hUserData,
                                  pInfo->address,
                                  MT2266_Init_Defaults2[0],
                                  &MT2266_Init_Defaults2[1],
                                  sizeof(MT2266_Init_Defaults2)/sizeof(U8Data)-1);
        }
    }

    /*  Read back all the registers from the tuner */
    if (MT_NO_ERROR(status))
    {
        status |= MT2266_ReadSub(pInfo->hUserData, pInfo->address, 0, &pInfo->reg[0], END_REGS);
    }

    /*
    **  Set reg[0x33] based on statusreg
    */
    if (MT_NO_ERROR(status))
    {
        BBVref = (((statusreg >> 6) + 2) & 0x03);
        tmpreg = (pInfo->reg[MT2266_RSVD_33] & ~(0x60)) | (BBVref << 5);
        status |= UncheckedSet(pInfo, MT2266_RSVD_33, tmpreg);
    }

    /*  Run the baseband filter calibration  */
    if (MT_NO_ERROR(status))
        status |= Run_BB_RC_Cal2(h);

    /*  Set the baseband filter bandwidth to the default  */
    if (MT_NO_ERROR(status))
        status |= Set_BBFilt(h);

    return (status);
}


/****************************************************************************
**
**  Name: MT2266_SetParam
**
**  Description:    Sets a tuning algorithm parameter.
**
**                  This function provides access to the internals of the
**                  tuning algorithm.  You can override many of the tuning
**                  algorithm defaults using this function.
**
**  Parameters:     h           - Tuner handle (returned by MT2266_Open)
**                  param       - Tuning algorithm parameter
**                                (see enum MT2266_Param)
**                  nValue      - value to be set
**
**                  param                   Description
**                  ----------------------  --------------------------------
**                  MT2266_SRO_FREQ         crystal frequency
**                  MT2266_STEPSIZE         minimum tuning step size
**                  MT2266_INPUT_FREQ       Center of input channel
**                  MT2266_OUTPUT_BW        Output channel bandwidth
**                  MT2266_RF_ATTN          RF attenuation (0-255)
**                  MT2266_RF_EXT           External control of RF atten
**                  MT2266_LNA_GAIN         LNA gain setting (0-15)
**                  MT2266_LNA_GAIN_DECR    Decrement LNA Gain (arg=min)
**                  MT2266_LNA_GAIN_INCR    Increment LNA Gain (arg=max)
**                  MT2266_BB_ATTN          Baseband attenuation (0-255)
**                  MT2266_BB_EXT           External control of BB atten
**                  MT2266_UHF_MAXSENS      Set for UHF max sensitivity mode
**                  MT2266_UHF_NORMAL       Set for UHF normal mode
**
**  Usage:          status |= MT2266_SetParam(hMT2266,
**                                            MT2266_STEPSIZE,
**                                            50000);
**
**  Returns:        status:
**                      MT_OK            - No errors
**                      MT_INV_HANDLE    - Invalid tuner handle
**                      MT_ARG_RANGE     - Invalid parameter requested
**                                         or set value out of range
**                                         or non-writable parameter
**
**  Dependencies:   USERS MUST CALL MT2266_Open() FIRST!
**
**  See Also:       MT2266_GetParam, MT2266_Open
**
**  Revision History:
**
**   SCR      Date      Author  Description
**  -------------------------------------------------------------------------
**   N/A   05-30-2006    DAD    Ver 1.0: Modified version of mt2260.c (Ver 1.01).
**   N/A   11-29-2006    DAD    Ver 1.03: Parenthesis clarification for gcc
**
****************************************************************************/
UData_t MT2266_SetParam(Handle_t     h,
                        MT2266_Param param,
                        UData_t      nValue)
{
    UData_t status = MT_OK;                  /* Status to be returned        */
    U8Data tmpreg;
    MT2266_Info_t* pInfo = (MT2266_Info_t*) h;

    /*  Verify that the handle passed points to a valid tuner         */
    if (IsValidHandle(pInfo) == 0)
        return (MT_INV_HANDLE);

    if (MT_NO_ERROR(status))
    {
        switch (param)
        {
        /*  crystal frequency                     */
        case MT2266_SRO_FREQ:
            pInfo->f_Ref = nValue;
            if (pInfo->f_Ref < 22000000)
            {
                /*  Turn off f_SRO divide by 2  */
                status |= UncheckedSet(pInfo,
                                       MT2266_SRO_CTRL,
                                       (U8Data) (pInfo->reg[MT2266_SRO_CTRL] &= 0xFE));
            }
            else
            {
                /*  Turn on f_SRO divide by 2  */
                status |= UncheckedSet(pInfo,
                                       MT2266_SRO_CTRL,
                                       (U8Data) (pInfo->reg[MT2266_SRO_CTRL] |= 0x01));
            }
            status |= Run_BB_RC_Cal2(h);
            if (MT_NO_ERROR(status))
                status |= Set_BBFilt(h);
            break;

        /*  minimum tuning step size              */
        case MT2266_STEPSIZE:
            pInfo->f_Step = nValue;
            break;

        /*  Width of output channel               */
        case MT2266_OUTPUT_BW:
            pInfo->f_bw = nValue;
            status |= Set_BBFilt(h);
            break;

        /*  BB attenuation (0-255)                */
        case MT2266_BB_ATTN:
            if (nValue > 255)
                status |= MT_ARG_RANGE;
            else
            {
                UData_t BBA_Stage1;
                UData_t BBA_Stage2;
                UData_t BBA_Stage3;

                BBA_Stage3 = (nValue > 102) ? 103 : nValue + 1;
                BBA_Stage2 = (nValue > 175) ? 75 : nValue + 2 - BBA_Stage3;
                BBA_Stage1 = (nValue > 176) ? nValue - 175 : 1;
                pInfo->reg[MT2266_RSVD_2C] = (U8Data) BBA_Stage1;
                pInfo->reg[MT2266_RSVD_2D] = (U8Data) BBA_Stage2;
                pInfo->reg[MT2266_RSVD_2E] = (U8Data) BBA_Stage3;
                pInfo->reg[MT2266_RSVD_2F] = (U8Data) BBA_Stage1;
                pInfo->reg[MT2266_RSVD_30] = (U8Data) BBA_Stage2;
                pInfo->reg[MT2266_RSVD_31] = (U8Data) BBA_Stage3;
                status |= MT2266_WriteSub(pInfo->hUserData,
                                      pInfo->address,
                                      MT2266_RSVD_2C,
                                      &pInfo->reg[MT2266_RSVD_2C],
                                      6);
            }
            break;

        /*  RF attenuation (0-255)                */
        case MT2266_RF_ATTN:
            if (nValue > 255)
                status |= MT_ARG_RANGE;
            else
                status |= UncheckedSet(pInfo, MT2266_RSVD_1F, (U8Data) nValue);
            break;

        /*  RF external / internal atten control  */
        case MT2266_RF_EXT:
            if (nValue == 0)
                tmpreg = pInfo->reg[MT2266_GPO] &= ~0x40;
            else
                tmpreg = pInfo->reg[MT2266_GPO] |= 0x40;
            status |= UncheckedSet(pInfo, MT2266_GPO, tmpreg);
            break;

        /*  LNA gain setting (0-15)               */
        case MT2266_LNA_GAIN:
            if (nValue > 15)
                status |= MT_ARG_RANGE;
            else
            {
                tmpreg = (pInfo->reg[MT2266_IGAIN] & 0xC3) | ((U8Data)nValue << 2);
                status |= UncheckedSet(pInfo, MT2266_IGAIN, tmpreg);
            }
            break;

        /*  Decrement LNA Gain setting, argument is min LNA Gain setting  */
        case MT2266_LNA_GAIN_DECR:
            if (nValue > 15)
                status |= MT_ARG_RANGE;
            else
            {
                PREFETCH(MT2266_IGAIN, 1);
                if (MT_NO_ERROR(status) && ((U8Data) ((pInfo->reg[MT2266_IGAIN] & 0x3C) >> 2) > (U8Data) nValue))
                    status |= UncheckedSet(pInfo, MT2266_IGAIN, pInfo->reg[MT2266_IGAIN] - 0x04);
            }
            break;

        /*  Increment LNA Gain setting, argument is max LNA Gain setting  */
        case MT2266_LNA_GAIN_INCR:
            if (nValue > 15)
                status |= MT_ARG_RANGE;
            else
            {
                PREFETCH(MT2266_IGAIN, 1);
                if (MT_NO_ERROR(status) && ((U8Data) ((pInfo->reg[MT2266_IGAIN] & 0x3C) >> 2) < (U8Data) nValue))
                    status |= UncheckedSet(pInfo, MT2266_IGAIN, pInfo->reg[MT2266_IGAIN] + 0x04);
            }
            break;

        /*  BB external / internal atten control  */
        case MT2266_BB_EXT:
            if (nValue == 0)
                tmpreg = pInfo->reg[MT2266_RSVD_33] &= ~0x08;
            else
                tmpreg = pInfo->reg[MT2266_RSVD_33] |= 0x08;
            status |= UncheckedSet(pInfo, MT2266_RSVD_33, tmpreg);
            break;

        /*  Set for UHF max sensitivity mode  */
        case MT2266_UHF_MAXSENS:
            PREFETCH(MT2266_BAND_CTRL, 1);
            if (MT_NO_ERROR(status) && ((pInfo->reg[MT2266_BAND_CTRL] & 0x30) == 0x10))
                status |= UncheckedSet(pInfo, MT2266_BAND_CTRL, pInfo->reg[MT2266_BAND_CTRL] ^ 0x30);
            break;

        /*  Set for UHF normal mode  */
        case MT2266_UHF_NORMAL:
            if (MT_NO_ERROR(status) && ((pInfo->reg[MT2266_BAND_CTRL] & 0x30) == 0x20))
                status |= UncheckedSet(pInfo, MT2266_BAND_CTRL, pInfo->reg[MT2266_BAND_CTRL] ^ 0x30);
            break;

        /*  These parameters are read-only  */
        case MT2266_IC_ADDR:
        case MT2266_MAX_OPEN:
        case MT2266_NUM_OPEN:
        case MT2266_NUM_REGS:
        case MT2266_INPUT_FREQ:
        case MT2266_LO_FREQ:
        case MT2266_RC2_VALUE:
        case MT2266_RF_ADC:
        case MT2266_BB_ADC:
        case MT2266_EOP:
        default:
            status |= MT_ARG_RANGE;
        }
    }
    return (status);
}


/****************************************************************************
**
**  Name: MT2266_SetPowerModes
**
**  Description:    Sets the bits in the MT2266_ENABLES register and the
**                  SROsd bit in the MT2266_SROADC_CTRL register.
**
**  Parameters:     h           - Tuner handle (returned by MT2266_Open)
**                  flags       - Bit mask of flags to indicate enabled
**                                bits.
**
**  Usage:          status = MT2266_SetPowerModes(hMT2266, flags);
**
**  Returns:        status:
**                      MT_OK            - No errors
**                      MT_INV_HANDLE    - Invalid tuner handle
**
**  Dependencies:   USERS MUST CALL MT2266_Open() FIRST!
**
**                  The bits in the MT2266_ENABLES register and the
**                  SROsd bit are set according to the supplied flags.
**
**                  The pre-defined flags are as follows:
**                      MT2266_SROen
**                      MT2266_LOen
**                      MT2266_ADCen
**                      MT2266_PDen
**                      MT2266_DCOCen
**                      MT2266_BBen
**                      MT2266_MIXen
**                      MT2266_LNAen
**                      MT2266_ALL_ENABLES
**                      MT2266_NO_ENABLES
**                      MT2266_SROsd
**                      MT2266_SRO_NOT_sd
**
**                  ONLY the enable bits (or SROsd bit) specified in the
**                  flags parameter will be set.  Any flag which is not
**                  included, will cause that bit to be disabled.
**
**                  The ALL_ENABLES, NO_ENABLES, and SRO_NOT_sd constants
**                  are for convenience.  The NO_ENABLES and SRO_NOT_sd
**                  do not actually have to be included, but are provided
**                  for clarity.
**
**  See Also:       MT2266_Open
**
**  Revision History:
**
**   SCR      Date      Author  Description
**  -------------------------------------------------------------------------
**   N/A   05-30-2006    DAD    Ver 1.0: Modified version of mt2260.c (Ver 1.01).
**
****************************************************************************/
UData_t MT2266_SetPowerModes(Handle_t h,
                             UData_t  flags)
{
    UData_t status = MT_OK;                  /* Status to be returned */
    MT2266_Info_t* pInfo = (MT2266_Info_t*) h;
    U8Data tmpreg;

    /*  Verify that the handle passed points to a valid tuner */
    if (IsValidHandle(pInfo) == 0)
        return (MT_INV_HANDLE);

    PREFETCH(MT2266_SRO_CTRL, 1);  /* Fetch register(s) if __NO_CACHE__ defined */
    if (MT_NO_ERROR(status))
    {
        if (flags & MT2266_SROsd)
            tmpreg = pInfo->reg[MT2266_SRO_CTRL] |= 0x10;  /* set the SROsd bit */
        else
            tmpreg = pInfo->reg[MT2266_SRO_CTRL] &= 0xEF;  /* clear the SROsd bit */
        status |= UncheckedSet(pInfo, MT2266_SRO_CTRL, tmpreg);
    }

    PREFETCH(MT2266_ENABLES, 1);  /* Fetch register(s) if __NO_CACHE__ defined */

    if (MT_NO_ERROR(status))
    {
        status |= UncheckedSet(pInfo, MT2266_ENABLES, (U8Data)(flags & 0xff));
    }

    return status;
}


/****************************************************************************
**  LOCAL FUNCTION - DO NOT USE OUTSIDE OF mt2266.c
**
**  Name: UncheckedSet
**
**  Description:    Sets an MT2266 register.
**
**                  NOTE: This is a local function that performs the same
**                  steps as the MT2266_SetReg function that is available
**                  in the external API.  It does not do any of the standard
**                  error checking that the API function provides and should
**                  not be called from outside this file.
**
**  Parameters:     *pInfo      - Tuner control structure
**                  reg         - MT2266 register/subaddress location
**                  val         - MT2266 register/subaddress value
**
**  Returns:        status:
**                      MT_OK            - No errors
**                      MT_COMM_ERR      - Serial bus communications error
**                      MT_INV_HANDLE    - Invalid tuner handle
**                      MT_ARG_RANGE     - Argument out of range
**
**  Dependencies:   USERS MUST CALL MT2266_Open() FIRST!
**
**                  Sets a register value without any preliminary checking for
**                  valid handles or register numbers.
**
**  Revision History:
**
**   SCR      Date      Author  Description
**  -------------------------------------------------------------------------
**   N/A   05-30-2006    DAD    Ver 1.0: Modified version of mt2260.c (Ver 1.01).
**
****************************************************************************/
static UData_t UncheckedSet(MT2266_Info_t* pInfo,
                            U8Data         reg,
                            U8Data         val)
{
    UData_t status;                  /* Status to be returned */

#if defined(_DEBUG)
    status = MT_OK;
    /*  Verify that the handle passed points to a valid tuner         */
    if (IsValidHandle(pInfo) == 0)
        return (MT_INV_HANDLE);

    if (reg >= END_REGS)
        status |= MT_ARG_RANGE;

    if (MT_IS_ERROR(status))
        return (status);
#endif

    status = MT2266_WriteSub(pInfo->hUserData, pInfo->address, reg, &val, 1);

    if (MT_NO_ERROR(status))
        pInfo->reg[reg] = val;

    return (status);
}


/****************************************************************************
**
**  Name: MT2266_SetReg
**
**  Description:    Sets an MT2266 register.
**
**  Parameters:     h           - Tuner handle (returned by MT2266_Open)
**                  reg         - MT2266 register/subaddress location
**                  val         - MT2266 register/subaddress value
**
**  Returns:        status:
**                      MT_OK            - No errors
**                      MT_COMM_ERR      - Serial bus communications error
**                      MT_INV_HANDLE    - Invalid tuner handle
**                      MT_ARG_RANGE     - Argument out of range
**
**  Dependencies:   USERS MUST CALL MT2266_Open() FIRST!
**
**                  Use this function if you need to override a default
**                  register value
**
**  Revision History:
**
**   SCR      Date      Author  Description
**  -------------------------------------------------------------------------
**   N/A   05-30-2006    DAD    Ver 1.0: Modified version of mt2260.c (Ver 1.01).
**
****************************************************************************/
UData_t MT2266_SetReg(Handle_t h,
                      U8Data   reg,
                      U8Data   val)
{
    UData_t status = MT_OK;                  /* Status to be returned */
    MT2266_Info_t* pInfo = (MT2266_Info_t*) h;

    /*  Verify that the handle passed points to a valid tuner         */
    if (IsValidHandle(pInfo) == 0)
        return (MT_INV_HANDLE);

    if (reg >= END_REGS)
        status |= MT_ARG_RANGE;

    if (MT_NO_ERROR(status))
        status |= UncheckedSet(pInfo, reg, val);

    return (status);
}


/****************************************************************************
**
**  Name: MT2266_SetUHFXFreqs
**
**  Description:    Assigns the specified set of UHF Crossover Frequencies
**
**  Parameters:     h            - Open handle to the tuner (from MT2266_Open).
**
**  Usage:          MT2266_Freq_Set  tmpFreqs;
**                  status = MT2266_GetUHFXFreqs(hMT2266,
**                                               MT2266_UHF1_WITH_ATTENUATION,
**                                               tmpFreqs );
**                   ...
**                  tmpFreqs[i] = <desired value>
**                   ...
**                  status = MT2266_SetUHFXFreqs(hMT2266,
**                                               MT2266_UHF1_WITH_ATTENUATION,
**                                               tmpFreqs );
**
**                  if (status & MT_ARG_RANGE)
**                      // error, Invalid UHF Crossover Frequency Set requested.
**                  else
**                      for( int i = 0;  i < MT2266_NUM_XFREQS; i++ )
**                         . . .
**
**  Returns:        status:
**                      MT_OK            - No errors
**                      MT_ARG_RANGE     - freq_type is out of range.
**                      MT_INV_HANDLE    - Invalid tuner handle
**
**  Dependencies:   freqs_buffer *must* be defined of type MT2266_Freq_Set
**                     to assure sufficient space allocation!
**
**                  USERS MUST CALL MT2266_Open() FIRST!
**
**  See Also:       MT2266_SetUHFXFreqs
**
**  Revision History:
**
**   SCR      Date      Author  Description
**  -------------------------------------------------------------------------
**   N/A   10-26-2006   RSK     Original.
**
****************************************************************************/
UData_t MT2266_SetUHFXFreqs(Handle_t h,
                            MT2266_UHFXFreq_Type freq_type,
                            MT2266_XFreq_Set     freqs_buffer)
{
    UData_t status = MT_OK;                     /* Status to be returned */
    MT2266_Info_t* pInfo = (MT2266_Info_t*) h;

    /*  Verify that the handle passed points to a valid tuner         */
    if (IsValidHandle(pInfo) == 0)
        return (MT_INV_HANDLE);

    if (freq_type >= MT2266_NUMBER_OF_XFREQ_SETS)
        status |= MT_ARG_RANGE;

    if (MT_NO_ERROR(status))
    {
        int  i;

        for( i = 0; i < MT2266_NUM_XFREQS; i++ )
        {
            pInfo->xfreqs.xfreq[ freq_type ][i] = freqs_buffer[i] * 1000 / TUNE_STEP_SIZE;
        }
    }

    return (status);
}


/****************************************************************************
** LOCAL FUNCTION
**
**  Name: RoundToStep
**
**  Description:    Rounds the given frequency to the closes f_Step value
**                  given the tuner ref frequency..
**
**
**  Parameters:     freq      - Frequency to be rounded (in Hz).
**                  f_Step    - Step size for the frequency (in Hz).
**                  f_Ref     - SRO frequency (in Hz).
**
**  Returns:        Rounded frequency.
**
**  Revision History:
**
**   SCR      Date      Author  Description
**  -------------------------------------------------------------------------
**   N/A   05-30-2006    DAD    Ver 1.0: Modified version of mt2260.c (Ver 1.01).
**
****************************************************************************/
static UData_t RoundToStep(UData_t freq, UData_t f_Step, UData_t f_ref)
{
    return f_ref * (freq / f_ref)
        + f_Step * (((freq % f_ref) + (f_Step / 2)) / f_Step);
}


/****************************************************************************
**
**  Name: fLO_FractionalTerm
**
**  Description:    Calculates the portion contributed by FracN / denom.
**
**                  This function preserves maximum precision without
**                  risk of overflow.  It accurately calculates
**                  f_ref * num / denom to within 1 HZ with fixed math.
**
**  Parameters:     num       - Fractional portion of the multiplier
**                  denom     - denominator portion of the ratio
**                              This routine successfully handles denom values
**                              up to and including 2^18.
**                  f_Ref     - SRO frequency.  This calculation handles
**                              f_ref as two separate 14-bit fields.
**                              Therefore, a maximum value of 2^28-1
**                              may safely be used for f_ref.  This is
**                              the genesis of the magic number "14" and the
**                              magic mask value of 0x03FFF.
**
**  Returns:        f_ref * num / denom
**
**  Revision History:
**
**   SCR      Date      Author  Description
**  -------------------------------------------------------------------------
**   N/A   12-20-2006    RSK    Ver 1.04: Adding fLO_FractionalTerm() usage.
**
****************************************************************************/
static UData_t fLO_FractionalTerm( UData_t f_ref,
                                   UData_t num,
                                   UData_t denom )
{
    UData_t t1     = (f_ref >> 14) * num;
    UData_t term1  = t1 / denom;
    UData_t loss   = t1 % denom;
    UData_t term2  = ( ((f_ref & 0x00003FFF) * num + (loss<<14)) + (denom/2) )  / denom;
    return ((term1 << 14) + term2);
}


/****************************************************************************
** LOCAL FUNCTION
**
**  Name: CalcLOMult
**
**  Description:    Calculates Integer divider value and the numerator
**                  value for LO's FracN PLL.
**
**                  This function assumes that the f_LO and f_Ref are
**                  evenly divisible by f_LO_Step.
**
**  Parameters:     Div       - OUTPUT: Whole number portion of the multiplier
**                  FracN     - OUTPUT: Fractional portion of the multiplier
**                  f_LO      - desired LO frequency.
**                  denom     - LO FracN denominator value
**                  f_Ref     - SRO frequency.
**
**  Returns:        Recalculated LO frequency.
**
**  Revision History:
**
**   SCR      Date      Author  Description
**  -------------------------------------------------------------------------
**   N/A   05-30-2006    DAD    Ver 1.0: Modified version of mt2260.c (Ver 1.01).
**   N/A   12-20-2006    RSK    Ver 1.04: Adding fLO_FractionalTerm() usage.
**
****************************************************************************/
static UData_t CalcLOMult(UData_t *Div,
                          UData_t *FracN,
                          UData_t  f_LO,
                          UData_t  denom,
                          UData_t  f_Ref)
{
    UData_t a, b, i;
    const SData_t TwoNShift = 13;   /* bits to shift to obtain 2^n qty */
    const SData_t RoundShift = 18;  /* bits to shift before rounding */

    /*  Calculate the whole number portion of the divider */
    *Div = f_LO / f_Ref;

    /*
    **  Calculate the FracN numerator 1 bit at a time.  This keeps the
    **  integer values from overflowing when large values are multiplied.
    **  This loop calculates the fractional portion of F/20MHz accurate
    **  to 32 bits.  The 2^n factor is represented by the placement of
    **  the value in the 32-bit word.  Since we want as much accuracy
    **  as possible, we'll leave it at the top of the word.
    */
    *FracN = 0;
    a = f_LO;
    for (i=32; i>0; --i)
    {
        b = 2*(a % f_Ref);
        *FracN = (*FracN * 2) + (b >= f_Ref);
        a = b;
    }

    /*
    **  If the denominator is a 2^n - 1 value (the usual case) then the
    **  value we really need is (F/20) * 2^n - (F/20).  Shifting the
    **  calculated (F/20) value to the right and subtracting produces
    **  the desired result -- still accurate to 32 bits.
    */
    if ((denom & 0x01) != 0)
        *FracN -= (*FracN >> TwoNShift);

    /*
    ** Now shift the result so that it is 1 bit bigger than we need,
    ** use the low-order bit to round the remaining bits, and shift
    ** to make the answer the desired size.
    */
    *FracN >>= RoundShift;
    *FracN = (*FracN & 0x01) + (*FracN >> 1);

    /*  Check for rollover (cannot happen with 50 kHz step size) */
    if (*FracN == (denom | 1))
    {
        *FracN = 0;
        ++Div;
    }


    return (f_Ref * (*Div)) + fLO_FractionalTerm( f_Ref, *FracN, denom );
}


/****************************************************************************
** LOCAL FUNCTION
**
**  Name: GetCrossover
**
**  Description:    Determines the appropriate value in the set of
**                  crossover frequencies.
**
**                  This function assumes that the crossover frequency table
**                  ias been properly initialized in descending order.
**
**  Parameters:     f_in      - The input frequency to use.
**                  freqs     - The array of crossover frequency entries.
**
**  Returns:        Index of crossover frequency band to use.
**
**  Revision History:
**
**   SCR      Date      Author  Description
**  -------------------------------------------------------------------------
**   N/A   10-27-2006    RSK    Original
**
****************************************************************************/
static U8Data GetCrossover( UData_t f_in,  UData_t* freqs )
{
    U8Data idx;
    U8Data retVal = 0;

    for (idx=0; idx< (U8Data)MT2266_NUM_XFREQS; idx++)
    {
        if ( freqs[idx] >= f_in)
        {
            retVal = (U8Data)MT2266_NUM_XFREQS - idx;
            break;
        }
    }

    return retVal;
}


/****************************************************************************
**
**  Name: MT2266_ChangeFreq
**
**  Description:    Change the tuner's tuned frequency to f_in.
**
**  Parameters:     h           - Open handle to the tuner (from MT2266_Open).
**                  f_in        - RF input center frequency (in Hz).
**
**  Returns:        status:
**                      MT_OK            - No errors
**                      MT_INV_HANDLE    - Invalid tuner handle
**                      MT_DNC_UNLOCK    - Downconverter PLL unlocked
**                      MT_COMM_ERR      - Serial bus communications error
**                      MT_FIN_RANGE     - Input freq out of range
**                      MT_DNC_RANGE     - Downconverter freq out of range
**
**  Dependencies:   MUST CALL MT2266_Open BEFORE MT2266_ChangeFreq!
**
**                  MT2266_ReadSub       - Read byte(s) of data from the two-wire-bus
**                  MT2266_WriteSub      - Write byte(s) of data to the two-wire-bus
**                  MT_Sleep         - Delay execution for x milliseconds
**                  MT2266_GetLocked - Checks to see if the PLL is locked
**
**  Revision History:
**
**   SCR      Date      Author  Description
**  -------------------------------------------------------------------------
**   N/A   05-30-2006    DAD    Ver 1.0: Modified version of mt2260.c (Ver 1.01).
**   N/A   11-01-2006    RSK    Ver 1.02: Added usage of UFILT0 and UFILT1.
**   N/A   11-29-2006    DAD    Ver 1.03: Parenthesis clarification
**   118   05-09-2007    RSK    Ver 1.05: Refactored to call _Tune() API.
**
****************************************************************************/
UData_t MT2266_ChangeFreq(Handle_t h,
                          UData_t f_in)     /* RF input center frequency   */
{
    return (MT2266_Tune(h, f_in));
}


/****************************************************************************
**
**  Name: MT2266_Tune
**
**  Description:    Change the tuner's tuned frequency to f_in.
**
**  Parameters:     h           - Open handle to the tuner (from MT2266_Open).
**                  f_in        - RF input center frequency (in Hz).
**
**  Returns:        status:
**                      MT_OK            - No errors
**                      MT_INV_HANDLE    - Invalid tuner handle
**                      MT_DNC_UNLOCK    - Downconverter PLL unlocked
**                      MT_COMM_ERR      - Serial bus communications error
**                      MT_FIN_RANGE     - Input freq out of range
**                      MT_DNC_RANGE     - Downconverter freq out of range
**
**  Dependencies:   MUST CALL MT2266_Open BEFORE MT2266_Tune!
**
**                  MT_ReadSub       - Read byte(s) of data from the two-wire-bus
**                  MT_WriteSub      - Write byte(s) of data to the two-wire-bus
**                  MT_Sleep         - Delay execution for x milliseconds
**                  MT2266_GetLocked - Checks to see if the PLL is locked
**
**  Revision History:
**
**   SCR      Date      Author  Description
**  -------------------------------------------------------------------------
**   N/A   05-30-2006    DAD    Ver 1.0: Modified version of mt2260.c (Ver 1.01).
**   N/A   11-01-2006    RSK    Ver 1.02: Added usage of UFILT0 and UFILT1.
**   N/A   11-29-2006    DAD    Ver 1.03: Parenthesis clarification
**   118   05-09-2007    RSK    Ver 1.05: Adding Standard MTxxxx_Tune() API.
**
****************************************************************************/
UData_t MT2266_Tune(Handle_t h,
                    UData_t f_in)     /* RF input center frequency   */
{
    MT2266_Info_t* pInfo = (MT2266_Info_t*) h;

    UData_t status = MT_OK;       /*  status of operation             */
    UData_t LO;                   /*  LO register value               */
    UData_t Num;                  /*  Numerator for LO reg. value     */
    UData_t ofLO;                 /*  last time's LO frequency        */
    UData_t ofin;                 /*  last time's input frequency     */
    U8Data  LO_Band;              /*  LO Mode bits                    */
    UData_t s_fRef;               /*  Ref Freq scaled for LO Band     */
    UData_t this_band;            /*  Band for the requested freq     */
    UData_t SROx2;                /*  SRO times 2                     */

    /*  Verify that the handle passed points to a valid tuner         */
    if (IsValidHandle(pInfo) == 0)
        return (MT_INV_HANDLE);

    /*
    **  Save original input and LO value
    */
    ofLO = pInfo->f_LO;
    ofin = pInfo->f_in;

    /*
    **  Assign in the requested input value
    */
    pInfo->f_in = f_in;

    /*
    **  Get the SRO multiplier value
    */
    SROx2 = (2 - (pInfo->reg[MT2266_SRO_CTRL] & 0x01));

    /*  Request an LO that is on a step size boundary  */
    pInfo->f_LO = RoundToStep(f_in, pInfo->f_Step, pInfo->f_Ref);

    if (pInfo->f_LO < MIN_VHF_FREQ)
    {
        status |= MT_FIN_RANGE | MT_ARG_RANGE | MT_DNC_RANGE;
        return status;  /* Does not support frequencies below MIN_VHF_FREQ  */
    }
    else if (pInfo->f_LO <= MAX_VHF_FREQ)
    {
        /*  VHF Band  */
        s_fRef = pInfo->f_Ref * SROx2 / 4;
        LO_Band = 0;
        this_band = MT2266_VHF_BAND;
    }
    else if (pInfo->f_LO < MIN_UHF_FREQ)
    {
        status |= MT_FIN_RANGE | MT_ARG_RANGE | MT_DNC_RANGE;
        return status;  /* Does not support frequencies between MAX_VHF_FREQ & MIN_UHF_FREQ */
    }
    else if (pInfo->f_LO <= MAX_UHF_FREQ)
    {
        /*  UHF Band  */
        s_fRef = pInfo->f_Ref * SROx2 / 2;
        LO_Band = 1;
        this_band = MT2266_UHF_BAND;
    }
    else
    {
        status |= MT_FIN_RANGE | MT_ARG_RANGE | MT_DNC_RANGE;
        return status;  /* Does not support frequencies above MAX_UHF_FREQ */
    }

    /*
    ** Calculate the LO frequencies and the values to be placed
    ** in the tuning registers.
    */
    pInfo->f_LO = CalcLOMult(&LO, &Num, pInfo->f_LO, 8191, s_fRef);

    /*
    **  If we have the same LO frequencies and we're already locked,
    **  then just return without writing any registers.
    */
    if ((ofLO == pInfo->f_LO)
        && ((pInfo->reg[MT2266_STATUS_1] & 0x40) == 0x40))
    {
        return (status);
    }

    /*
    ** Reset defaults here if we're tuning into a new band
    */
    if (MT_NO_ERROR(status))
    {
        if (this_band != pInfo->band)
        {
            MT2266_DefaultsEntry *defaults = NULL;
            switch (this_band)
            {
                case MT2266_VHF_BAND:
                    defaults = &MT2266_VHF_defaults[0];
                    break;
                case MT2266_UHF_BAND:
                    defaults = &MT2266_UHF_defaults[0];
                    break;
                default:
                    status |= MT_ARG_RANGE;
            }
            if ( MT_NO_ERROR(status))
            {
                while (defaults->data && MT_NO_ERROR(status))
                {
                    status |= MT2266_WriteSub(pInfo->hUserData, pInfo->address, defaults->data[0], &defaults->data[1], defaults->cnt);
                    defaults++;
                }
                /* re-read the new registers into the cached values */
                status |= MT2266_ReadSub(pInfo->hUserData, pInfo->address, 0, &pInfo->reg[0], END_REGS);
                pInfo->band = this_band;
            }
        }
    }

    /*
    **  Place all of the calculated values into the local tuner
    **  register fields.
    */
    if (MT_NO_ERROR(status))
    {
        pInfo->reg[MT2266_LO_CTRL_1] = (U8Data)(Num >> 8);
        pInfo->reg[MT2266_LO_CTRL_2] = (U8Data)(Num & 0xFF);
        pInfo->reg[MT2266_LO_CTRL_3] = (U8Data)(LO & 0xFF);

        /*
        ** Now write out the computed register values
        */
        status |= MT2266_WriteSub(pInfo->hUserData, pInfo->address, MT2266_LO_CTRL_1, &pInfo->reg[MT2266_LO_CTRL_1], 3);

        if (pInfo->band == MT2266_UHF_BAND)
        {
            U8Data UFilt0 = 0;                        /*  def when f_in > all    */
            U8Data UFilt1 = 0;                        /*  def when f_in > all    */
            UData_t* XFreq0;
            UData_t* XFreq1;
            SData_t ClearTune_Fuse;
            SData_t f_offset;
            UData_t f_in_;

            PREFETCH(MT2266_BAND_CTRL, 2);  /* Fetch register(s) if __NO_CACHE__ defined */
            PREFETCH(MT2266_STATUS_5, 1);  /* Fetch register(s) if __NO_CACHE__ defined */

            XFreq0 = (pInfo->reg[MT2266_BAND_CTRL] & 0x10) ? pInfo->xfreqs.xfreq[ MT2266_UHF0_ATTEN ] : pInfo->xfreqs.xfreq[ MT2266_UHF0 ];
            XFreq1 = (pInfo->reg[MT2266_BAND_CTRL] & 0x10) ? pInfo->xfreqs.xfreq[ MT2266_UHF1_ATTEN ] : pInfo->xfreqs.xfreq[ MT2266_UHF1 ];

            ClearTune_Fuse = pInfo->reg[MT2266_STATUS_5] & 0x07;
            f_offset = (10000000) * ((ClearTune_Fuse > 3) ? (ClearTune_Fuse - 8) : ClearTune_Fuse);
            f_in_ = (f_in - f_offset) / 1000 / TUNE_STEP_SIZE;

            UFilt0 = GetCrossover( f_in_, XFreq0 );
            UFilt1 = GetCrossover( f_in_, XFreq1 );

            /*  If UFilt == 16, set UBANDen and set UFilt = 15  */
            if ( (UFilt0 == 16) || (UFilt1 == 16) )
            {
                pInfo->reg[MT2266_BAND_CTRL] |= 0x01;
                if( UFilt0 > 0 ) UFilt0--;
                if( UFilt1 > 0 ) UFilt1--;
            }
            else
                pInfo->reg[MT2266_BAND_CTRL] &= ~(0x01);

            pInfo->reg[MT2266_BAND_CTRL] =
                    (pInfo->reg[MT2266_BAND_CTRL] & 0x3F) | (LO_Band << 6);

            pInfo->reg[MT2266_CLEARTUNE] = (UFilt1 << 4) | UFilt0;
            /*  Write UBANDsel  [05] & ClearTune [06]  */
            status |= MT2266_WriteSub(pInfo->hUserData, pInfo->address, MT2266_BAND_CTRL, &pInfo->reg[MT2266_BAND_CTRL], 2);
        }
    }

    /*
    **  Check for LO lock
    */
    if (MT_NO_ERROR(status))
    {
        status |= MT2266_GetLocked(h);
    }

    return (status);
}