]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/dev/bktr/bktr_tuner.c
allwinner: Add a new clock aw_clk_m
[FreeBSD/FreeBSD.git] / sys / dev / bktr / bktr_tuner.c
1 /*-
2  * SPDX-License-Identifier: BSD-4-Clause
3  *
4  * 1. Redistributions of source code must retain the
5  * Copyright (c) 1997 Amancio Hasty, 1999 Roger Hardiman
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. All advertising materials mentioning features or use of this software
17  *    must display the following acknowledgement:
18  *      This product includes software developed by Amancio Hasty and
19  *      Roger Hardiman
20  * 4. The name of the author may not be used to endorse or promote products
21  *    derived from this software without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
24  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
25  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
26  * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
27  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
28  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
29  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
31  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
32  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33  * POSSIBILITY OF SUCH DAMAGE.
34  */
35
36 #include <sys/cdefs.h>
37 __FBSDID("$FreeBSD$");
38
39 /*
40  * This is part of the Driver for Video Capture Cards (Frame grabbers)
41  * and TV Tuner cards using the Brooktree Bt848, Bt848A, Bt849A, Bt878, Bt879
42  * chipset.
43  * Copyright Roger Hardiman and Amancio Hasty.
44  *
45  * bktr_tuner : This deals with controlling the tuner fitted to TV cards.
46  */
47
48 #include <sys/param.h>
49 #include <sys/systm.h>
50 #include <sys/kernel.h>
51 #ifdef __NetBSD__
52 #include <sys/proc.h>
53 #endif
54
55 #ifdef __FreeBSD__
56 #if (__FreeBSD_version < 500000)
57 #include <machine/clock.h>              /* for DELAY */
58 #include <pci/pcivar.h>
59 #else
60 #include <sys/lock.h>
61 #include <sys/mutex.h>
62 #include <sys/selinfo.h>
63 #include <dev/pci/pcivar.h>
64 #endif
65
66 #include <machine/bus.h>
67 #include <sys/bus.h>
68 #endif
69
70 #ifdef __NetBSD__
71 #include <dev/ic/bt8xx.h>       /* NetBSD .h file location */
72 #include <dev/pci/bktr/bktr_reg.h>
73 #include <dev/pci/bktr/bktr_tuner.h>
74 #include <dev/pci/bktr/bktr_card.h>
75 #include <dev/pci/bktr/bktr_core.h>
76 #else
77 #include <dev/bktr/ioctl_meteor.h>
78 #include <dev/bktr/ioctl_bt848.h>       /* extensions to ioctl_meteor.h */
79 #include <dev/bktr/bktr_reg.h>
80 #include <dev/bktr/bktr_tuner.h>
81 #include <dev/bktr/bktr_card.h>
82 #include <dev/bktr/bktr_core.h>
83 #endif
84
85
86
87 #if defined( TUNER_AFC )
88 #define AFC_DELAY               10000   /* 10 millisend delay */
89 #define AFC_BITS                0x07
90 #define AFC_FREQ_MINUS_125      0x00
91 #define AFC_FREQ_MINUS_62       0x01
92 #define AFC_FREQ_CENTERED       0x02
93 #define AFC_FREQ_PLUS_62        0x03
94 #define AFC_FREQ_PLUS_125       0x04
95 #define AFC_MAX_STEP            (5 * FREQFACTOR) /* no more than 5 MHz */
96 #endif /* TUNER_AFC */
97
98   
99 #define TTYPE_XXX               0
100 #define TTYPE_NTSC              1
101 #define TTYPE_NTSC_J            2
102 #define TTYPE_PAL               3
103 #define TTYPE_PAL_M             4
104 #define TTYPE_PAL_N             5
105 #define TTYPE_SECAM             6
106   
107 #define TSA552x_CB_MSB          (0x80)
108 #define TSA552x_CB_CP           (1<<6)  /* set this for fast tuning */
109 #define TSA552x_CB_T2           (1<<5)  /* test mode - Normally set to 0 */
110 #define TSA552x_CB_T1           (1<<4)  /* test mode - Normally set to 0 */
111 #define TSA552x_CB_T0           (1<<3)  /* test mode - Normally set to 1 */
112 #define TSA552x_CB_RSA          (1<<2)  /* 0 for 31.25 khz, 1 for 62.5 kHz */
113 #define TSA552x_CB_RSB          (1<<1)  /* 0 for FM 50kHz steps, 1 = Use RSA*/
114 #define TSA552x_CB_OS           (1<<0)  /* Set to 0 for normal operation */
115
116 #define TSA552x_RADIO           (TSA552x_CB_MSB |       \
117                                  TSA552x_CB_T0)
118
119 /* raise the charge pump voltage for fast tuning */
120 #define TSA552x_FCONTROL        (TSA552x_CB_MSB |       \
121                                  TSA552x_CB_CP  |       \
122                                  TSA552x_CB_T0  |       \
123                                  TSA552x_CB_RSA |       \
124                                  TSA552x_CB_RSB)
125   
126 /* lower the charge pump voltage for better residual oscillator FM */
127 #define TSA552x_SCONTROL        (TSA552x_CB_MSB |       \
128                                  TSA552x_CB_T0  |       \
129                                  TSA552x_CB_RSA |       \
130                                  TSA552x_CB_RSB)
131   
132 /* The control value for the ALPS TSCH5 Tuner */
133 #define TSCH5_FCONTROL          0x82
134 #define TSCH5_RADIO             0x86
135   
136 /* The control value for the ALPS TSBH1 Tuner */
137 #define TSBH1_FCONTROL          0xce
138
139
140 static void mt2032_set_tv_freq(bktr_ptr_t bktr, unsigned int freq);
141
142
143 static const struct TUNER tuners[] = {
144 /* XXX FIXME: fill in the band-switch crosspoints */
145         /* NO_TUNER */
146         { "<no>",                               /* the 'name' */
147            TTYPE_XXX,                           /* input type */
148            { 0x00,                              /* control byte for Tuner PLL */
149              0x00,
150              0x00,
151              0x00 },
152            { 0x00, 0x00 },                      /* band-switch crosspoints */
153            { 0x00, 0x00, 0x00,0x00} },          /* the band-switch values */
154
155         /* TEMIC_NTSC */
156         { "Temic NTSC",                         /* the 'name' */
157            TTYPE_NTSC,                          /* input type */
158            { TSA552x_SCONTROL,                  /* control byte for Tuner PLL */
159              TSA552x_SCONTROL,
160              TSA552x_SCONTROL,
161              0x00 },
162            { 0x00, 0x00},                       /* band-switch crosspoints */
163            { 0x02, 0x04, 0x01, 0x00 } },        /* the band-switch values */
164
165         /* TEMIC_PAL */
166         { "Temic PAL",                          /* the 'name' */
167            TTYPE_PAL,                           /* input type */
168            { TSA552x_SCONTROL,                  /* control byte for Tuner PLL */
169              TSA552x_SCONTROL,
170              TSA552x_SCONTROL,
171              0x00 },
172            { 0x00, 0x00 },                      /* band-switch crosspoints */
173            { 0x02, 0x04, 0x01, 0x00 } },        /* the band-switch values */
174
175         /* TEMIC_SECAM */
176         { "Temic SECAM",                        /* the 'name' */
177            TTYPE_SECAM,                         /* input type */
178            { TSA552x_SCONTROL,                  /* control byte for Tuner PLL */
179              TSA552x_SCONTROL,
180              TSA552x_SCONTROL,
181              0x00 },
182            { 0x00, 0x00 },                      /* band-switch crosspoints */
183            { 0x02, 0x04, 0x01,0x00 } },         /* the band-switch values */
184
185         /* PHILIPS_NTSC */
186         { "Philips NTSC",                       /* the 'name' */
187            TTYPE_NTSC,                          /* input type */
188            { TSA552x_SCONTROL,                  /* control byte for Tuner PLL */
189              TSA552x_SCONTROL,
190              TSA552x_SCONTROL,
191              0x00 },
192            { 0x00, 0x00 },                      /* band-switch crosspoints */
193            { 0xa0, 0x90, 0x30, 0x00 } },        /* the band-switch values */
194
195         /* PHILIPS_PAL */
196         { "Philips PAL",                        /* the 'name' */
197            TTYPE_PAL,                           /* input type */
198            { TSA552x_SCONTROL,                  /* control byte for Tuner PLL */
199              TSA552x_SCONTROL,
200              TSA552x_SCONTROL,
201              0x00 },
202            { 0x00, 0x00 },                      /* band-switch crosspoints */
203            { 0xa0, 0x90, 0x30, 0x00 } },        /* the band-switch values */
204
205         /* PHILIPS_SECAM */
206         { "Philips SECAM",                      /* the 'name' */
207            TTYPE_SECAM,                         /* input type */
208            { TSA552x_SCONTROL,                  /* control byte for Tuner PLL */
209              TSA552x_SCONTROL,
210              TSA552x_SCONTROL,
211              0x00 },
212            { 0x00, 0x00 },                      /* band-switch crosspoints */
213            { 0xa7, 0x97, 0x37, 0x00 } },        /* the band-switch values */
214
215         /* TEMIC_PAL I */
216         { "Temic PAL I",                        /* the 'name' */
217            TTYPE_PAL,                           /* input type */
218            { TSA552x_SCONTROL,                  /* control byte for Tuner PLL */
219              TSA552x_SCONTROL,
220              TSA552x_SCONTROL,
221              0x00 },
222            { 0x00, 0x00 },                      /* band-switch crosspoints */
223            { 0x02, 0x04, 0x01,0x00 } },         /* the band-switch values */
224
225         /* PHILIPS_PALI */
226         { "Philips PAL I",                      /* the 'name' */
227            TTYPE_PAL,                           /* input type */
228            { TSA552x_SCONTROL,                  /* control byte for Tuner PLL */
229              TSA552x_SCONTROL,
230              TSA552x_SCONTROL,
231              0x00 },
232           { 0x00, 0x00 },                      /* band-switch crosspoints */
233           { 0xa0, 0x90, 0x30,0x00 } },         /* the band-switch values */
234
235        /* PHILIPS_FR1236_NTSC */
236        { "Philips FR1236 NTSC FM",             /* the 'name' */
237           TTYPE_NTSC,                          /* input type */
238           { TSA552x_FCONTROL,                   /* control byte for Tuner PLL */
239             TSA552x_FCONTROL,
240             TSA552x_FCONTROL,
241             TSA552x_RADIO  },
242           { 0x00, 0x00 },                       /* band-switch crosspoints */
243           { 0xa0, 0x90, 0x30,0xa4 } },          /* the band-switch values */
244
245         /* PHILIPS_FR1216_PAL */
246         { "Philips FR1216 PAL FM" ,             /* the 'name' */
247            TTYPE_PAL,                           /* input type */
248            { TSA552x_FCONTROL,                  /* control byte for Tuner PLL */
249              TSA552x_FCONTROL,
250              TSA552x_FCONTROL,
251              TSA552x_RADIO },
252            { 0x00, 0x00 },                      /* band-switch crosspoints */
253            { 0xa0, 0x90, 0x30, 0xa4 } },        /* the band-switch values */
254
255         /* PHILIPS_FR1236_SECAM */
256         { "Philips FR1236 SECAM FM",            /* the 'name' */
257            TTYPE_SECAM,                         /* input type */
258            { TSA552x_FCONTROL,                  /* control byte for Tuner PLL */
259              TSA552x_FCONTROL,
260              TSA552x_FCONTROL,
261              TSA552x_RADIO },
262            { 0x00, 0x00 },                      /* band-switch crosspoints */
263            { 0xa7, 0x97, 0x37, 0xa4 } },        /* the band-switch values */
264
265         /* ALPS TSCH5 NTSC */
266         { "ALPS TSCH5 NTSC FM",                 /* the 'name' */
267            TTYPE_NTSC,                          /* input type */
268            { TSCH5_FCONTROL,                    /* control byte for Tuner PLL */
269              TSCH5_FCONTROL,
270              TSCH5_FCONTROL,
271              TSCH5_RADIO },
272            { 0x00, 0x00 },                      /* band-switch crosspoints */
273            { 0x14, 0x12, 0x11, 0x04 } },        /* the band-switch values */
274
275         /* ALPS TSBH1 NTSC */
276         { "ALPS TSBH1 NTSC",                    /* the 'name' */
277            TTYPE_NTSC,                          /* input type */
278            { TSBH1_FCONTROL,                    /* control byte for Tuner PLL */
279              TSBH1_FCONTROL,
280              TSBH1_FCONTROL,
281              0x00 },
282            { 0x00, 0x00 },                      /* band-switch crosspoints */
283            { 0x01, 0x02, 0x08, 0x00 } },        /* the band-switch values */
284
285         /* MT2032 Microtune */
286         { "MT2032",                             /* the 'name' */
287            TTYPE_PAL,                           /* input type */
288            { TSA552x_SCONTROL,                  /* control byte for Tuner PLL */
289              TSA552x_SCONTROL,
290              TSA552x_SCONTROL,
291              0x00 },
292            { 0x00, 0x00 },                      /* band-switch crosspoints */
293            { 0xa0, 0x90, 0x30, 0x00 } },        /* the band-switch values */
294
295          /* LG TPI8PSB12P PAL */
296          { "LG TPI8PSB12P PAL",                 /* the 'name' */
297            TTYPE_PAL,                           /* input type */
298            { TSA552x_SCONTROL,                  /* control byte for Tuner PLL */
299              TSA552x_SCONTROL,
300              TSA552x_SCONTROL,
301              0x00 },
302            { 0x00, 0x00 },                      /* band-switch crosspoints */
303            { 0xa0, 0x90, 0x30, 0x8e } },        /* the band-switch values */
304
305          /* PHILIPS FI1216 */
306          { "PHILIPS_FI1216",                    /* the 'name' */
307            TTYPE_PAL,                           /* input type */
308            { TSBH1_FCONTROL,                    /* control byte for Tuner PLL */
309              TSBH1_FCONTROL,
310              TSBH1_FCONTROL,
311              0x00 },
312            { 0x00, 0x00 },                      /* band-switch crosspoints */
313            { 0x01, 0x02, 0x04, 0x00 } },        /* the band-switch values */
314 };
315
316
317 /* scaling factor for frequencies expressed as ints */
318 #define FREQFACTOR              16
319
320 /*
321  * Format:
322  *      entry 0:         MAX legal channel
323  *      entry 1:         IF frequency
324  *                       expressed as fi{mHz} * 16,
325  *                       eg 45.75mHz == 45.75 * 16 = 732
326  *      entry 2:         [place holder/future]
327  *      entry 3:         base of channel record 0
328  *      entry 3 + (x*3): base of channel record 'x'
329  *      entry LAST:      NULL channel entry marking end of records
330  *
331  * Record:
332  *      int 0:          base channel
333  *      int 1:          frequency of base channel,
334  *                       expressed as fb{mHz} * 16,
335  *      int 2:          offset frequency between channels,
336  *                       expressed as fo{mHz} * 16,
337  */
338
339 /*
340  * North American Broadcast Channels:
341  *
342  *  2:  55.25 mHz -  4:  67.25 mHz
343  *  5:  77.25 mHz -  6:  83.25 mHz
344  *  7: 175.25 mHz - 13: 211.25 mHz
345  * 14: 471.25 mHz - 83: 885.25 mHz
346  *
347  * IF freq: 45.75 mHz
348  */
349 #define OFFSET  6.00
350 static int nabcst[] = {
351         83,     (int)( 45.75 * FREQFACTOR),     0,
352         14,     (int)(471.25 * FREQFACTOR),     (int)(OFFSET * FREQFACTOR),
353          7,     (int)(175.25 * FREQFACTOR),     (int)(OFFSET * FREQFACTOR),
354          5,     (int)( 77.25 * FREQFACTOR),     (int)(OFFSET * FREQFACTOR),
355          2,     (int)( 55.25 * FREQFACTOR),     (int)(OFFSET * FREQFACTOR),
356          0
357 };
358 #undef OFFSET
359
360 /*
361  * North American Cable Channels, IRC:
362  *
363  *  2:  55.25 mHz -  4:  67.25 mHz
364  *  5:  77.25 mHz -  6:  83.25 mHz
365  *  7: 175.25 mHz - 13: 211.25 mHz
366  * 14: 121.25 mHz - 22: 169.25 mHz
367  * 23: 217.25 mHz - 94: 643.25 mHz
368  * 95:  91.25 mHz - 99: 115.25 mHz
369  *
370  * IF freq: 45.75 mHz
371  */
372 #define OFFSET  6.00
373 static int irccable[] = {
374         116,    (int)( 45.75 * FREQFACTOR),     0,
375         100,    (int)(649.25 * FREQFACTOR),     (int)(OFFSET * FREQFACTOR),
376         95,     (int)( 91.25 * FREQFACTOR),     (int)(OFFSET * FREQFACTOR),
377         23,     (int)(217.25 * FREQFACTOR),     (int)(OFFSET * FREQFACTOR),
378         14,     (int)(121.25 * FREQFACTOR),     (int)(OFFSET * FREQFACTOR),
379          7,     (int)(175.25 * FREQFACTOR),     (int)(OFFSET * FREQFACTOR),
380          5,     (int)( 77.25 * FREQFACTOR),     (int)(OFFSET * FREQFACTOR),
381          2,     (int)( 55.25 * FREQFACTOR),     (int)(OFFSET * FREQFACTOR),
382          0
383 };
384 #undef OFFSET
385
386 /*
387  * North American Cable Channels, HRC:
388  *
389  * 2:   54 mHz  - 4:    66 mHz
390  * 5:   78 mHz  - 6:    84 mHz
391  * 7:  174 mHz  - 13:  210 mHz
392  * 14: 120 mHz  - 22:  168 mHz
393  * 23: 216 mHz  - 94:  642 mHz
394  * 95:  90 mHz  - 99:  114 mHz
395  *
396  * IF freq: 45.75 mHz
397  */
398 #define OFFSET  6.00
399 static int hrccable[] = {
400         116,    (int)( 45.75 * FREQFACTOR),     0,
401         100,    (int)(648.00 * FREQFACTOR),     (int)(OFFSET * FREQFACTOR),
402         95,     (int)( 90.00 * FREQFACTOR),     (int)(OFFSET * FREQFACTOR),
403         23,     (int)(216.00 * FREQFACTOR),     (int)(OFFSET * FREQFACTOR),
404         14,     (int)(120.00 * FREQFACTOR),     (int)(OFFSET * FREQFACTOR),
405         7,      (int)(174.00 * FREQFACTOR),     (int)(OFFSET * FREQFACTOR),
406         5,      (int)( 78.00 * FREQFACTOR),     (int)(OFFSET * FREQFACTOR),
407         2,      (int)( 54.00 * FREQFACTOR),     (int)(OFFSET * FREQFACTOR),
408         0
409 };
410 #undef OFFSET
411
412 /*
413  * Western European broadcast channels:
414  *
415  * (there are others that appear to vary between countries - rmt)
416  *
417  * here's the table Philips provides:
418  * caution, some of the offsets don't compute...
419  *
420  *  1    4525   700     N21
421  * 
422  *  2    4825   700     E2
423  *  3    5525   700     E3
424  *  4    6225   700     E4
425  * 
426  *  5   17525   700     E5
427  *  6   18225   700     E6
428  *  7   18925   700     E7
429  *  8   19625   700     E8
430  *  9   20325   700     E9
431  * 10   21025   700     E10
432  * 11   21725   700     E11
433  * 12   22425   700     E12
434  * 
435  * 13    5375   700     ITA
436  * 14    6225   700     ITB
437  * 
438  * 15    8225   700     ITC
439  * 
440  * 16   17525   700     ITD
441  * 17   18325   700     ITE
442  * 
443  * 18   19225   700     ITF
444  * 19   20125   700     ITG
445  * 20   21025   700     ITH
446  * 
447  * 21   47125   800     E21
448  * 22   47925   800     E22
449  * 23   48725   800     E23
450  * 24   49525   800     E24
451  * 25   50325   800     E25
452  * 26   51125   800     E26
453  * 27   51925   800     E27
454  * 28   52725   800     E28
455  * 29   53525   800     E29
456  * 30   54325   800     E30
457  * 31   55125   800     E31
458  * 32   55925   800     E32
459  * 33   56725   800     E33
460  * 34   57525   800     E34
461  * 35   58325   800     E35
462  * 36   59125   800     E36
463  * 37   59925   800     E37
464  * 38   60725   800     E38
465  * 39   61525   800     E39
466  * 40   62325   800     E40
467  * 41   63125   800     E41
468  * 42   63925   800     E42
469  * 43   64725   800     E43
470  * 44   65525   800     E44
471  * 45   66325   800     E45
472  * 46   67125   800     E46
473  * 47   67925   800     E47
474  * 48   68725   800     E48
475  * 49   69525   800     E49
476  * 50   70325   800     E50
477  * 51   71125   800     E51
478  * 52   71925   800     E52
479  * 53   72725   800     E53
480  * 54   73525   800     E54
481  * 55   74325   800     E55
482  * 56   75125   800     E56
483  * 57   75925   800     E57
484  * 58   76725   800     E58
485  * 59   77525   800     E59
486  * 60   78325   800     E60
487  * 61   79125   800     E61
488  * 62   79925   800     E62
489  * 63   80725   800     E63
490  * 64   81525   800     E64
491  * 65   82325   800     E65
492  * 66   83125   800     E66
493  * 67   83925   800     E67
494  * 68   84725   800     E68
495  * 69   85525   800     E69
496  * 
497  * 70    4575   800     IA
498  * 71    5375   800     IB
499  * 72    6175   800     IC
500  * 
501  * 74    6925   700     S01
502  * 75    7625   700     S02
503  * 76    8325   700     S03
504  * 
505  * 80   10525   700     S1
506  * 81   11225   700     S2
507  * 82   11925   700     S3
508  * 83   12625   700     S4
509  * 84   13325   700     S5
510  * 85   14025   700     S6
511  * 86   14725   700     S7
512  * 87   15425   700     S8
513  * 88   16125   700     S9
514  * 89   16825   700     S10
515  * 90   23125   700     S11
516  * 91   23825   700     S12
517  * 92   24525   700     S13
518  * 93   25225   700     S14
519  * 94   25925   700     S15
520  * 95   26625   700     S16
521  * 96   27325   700     S17
522  * 97   28025   700     S18
523  * 98   28725   700     S19
524  * 99   29425   700     S20
525  *
526  *
527  * Channels S21 - S41 are taken from
528  * http://gemma.apple.com:80/dev/technotes/tn/tn1012.html
529  *
530  * 100  30325   800     S21
531  * 101  31125   800     S22
532  * 102  31925   800     S23
533  * 103  32725   800     S24
534  * 104  33525   800     S25
535  * 105  34325   800     S26         
536  * 106  35125   800     S27         
537  * 107  35925   800     S28         
538  * 108  36725   800     S29         
539  * 109  37525   800     S30         
540  * 110  38325   800     S31         
541  * 111  39125   800     S32         
542  * 112  39925   800     S33         
543  * 113  40725   800     S34         
544  * 114  41525   800     S35         
545  * 115  42325   800     S36         
546  * 116  43125   800     S37         
547  * 117  43925   800     S38         
548  * 118  44725   800     S39         
549  * 119  45525   800     S40         
550  * 120  46325   800     S41
551  * 
552  * 121   3890   000     IFFREQ
553  * 
554  */
555 static int weurope[] = {
556        121,     (int)( 38.90 * FREQFACTOR),     0, 
557        100,     (int)(303.25 * FREQFACTOR),     (int)(8.00 * FREQFACTOR), 
558         90,     (int)(231.25 * FREQFACTOR),     (int)(7.00 * FREQFACTOR),
559         80,     (int)(105.25 * FREQFACTOR),     (int)(7.00 * FREQFACTOR),  
560         74,     (int)( 69.25 * FREQFACTOR),     (int)(7.00 * FREQFACTOR),  
561         21,     (int)(471.25 * FREQFACTOR),     (int)(8.00 * FREQFACTOR),
562         17,     (int)(183.25 * FREQFACTOR),     (int)(9.00 * FREQFACTOR),
563         16,     (int)(175.25 * FREQFACTOR),     (int)(9.00 * FREQFACTOR),
564         15,     (int)(82.25 * FREQFACTOR),      (int)(8.50 * FREQFACTOR),
565         13,     (int)(53.75 * FREQFACTOR),      (int)(8.50 * FREQFACTOR),
566          5,     (int)(175.25 * FREQFACTOR),     (int)(7.00 * FREQFACTOR),
567          2,     (int)(48.25 * FREQFACTOR),      (int)(7.00 * FREQFACTOR),
568          0
569 };
570
571 /*
572  * Japanese Broadcast Channels:
573  *
574  *  1:  91.25MHz -  3: 103.25MHz
575  *  4: 171.25MHz -  7: 189.25MHz
576  *  8: 193.25MHz - 12: 217.25MHz  (VHF)
577  * 13: 471.25MHz - 62: 765.25MHz  (UHF)
578  *
579  * IF freq: 58.75 mHz
580  */
581 #define OFFSET  6.00
582 #define IF_FREQ 58.75
583 static int jpnbcst[] = {
584         62,     (int)(IF_FREQ * FREQFACTOR),    0,
585         13,     (int)(471.25 * FREQFACTOR),     (int)(OFFSET * FREQFACTOR),
586          8,     (int)(193.25 * FREQFACTOR),     (int)(OFFSET * FREQFACTOR),
587          4,     (int)(171.25 * FREQFACTOR),     (int)(OFFSET * FREQFACTOR),
588          1,     (int)( 91.25 * FREQFACTOR),     (int)(OFFSET * FREQFACTOR),
589          0
590 };
591 #undef IF_FREQ
592 #undef OFFSET
593
594 /*
595  * Japanese Cable Channels:
596  *
597  *  1:  91.25MHz -  3: 103.25MHz
598  *  4: 171.25MHz -  7: 189.25MHz
599  *  8: 193.25MHz - 12: 217.25MHz
600  * 13: 109.25MHz - 21: 157.25MHz
601  * 22: 165.25MHz
602  * 23: 223.25MHz - 63: 463.25MHz
603  *
604  * IF freq: 58.75 mHz
605  */
606 #define OFFSET  6.00
607 #define IF_FREQ 58.75
608 static int jpncable[] = {
609         63,     (int)(IF_FREQ * FREQFACTOR),    0,
610         23,     (int)(223.25 * FREQFACTOR),     (int)(OFFSET * FREQFACTOR),
611         22,     (int)(165.25 * FREQFACTOR),     (int)(OFFSET * FREQFACTOR),
612         13,     (int)(109.25 * FREQFACTOR),     (int)(OFFSET * FREQFACTOR),
613          8,     (int)(193.25 * FREQFACTOR),     (int)(OFFSET * FREQFACTOR),
614          4,     (int)(171.25 * FREQFACTOR),     (int)(OFFSET * FREQFACTOR),
615          1,     (int)( 91.25 * FREQFACTOR),     (int)(OFFSET * FREQFACTOR),
616          0
617 };
618 #undef IF_FREQ
619 #undef OFFSET
620
621 /*
622  * xUSSR Broadcast Channels:
623  *
624  *  1:  49.75MHz -  2:  59.25MHz
625  *  3:  77.25MHz -  5:  93.25MHz
626  *  6: 175.25MHz - 12: 223.25MHz
627  * 13-20 - not exist
628  * 21: 471.25MHz - 34: 575.25MHz
629  * 35: 583.25MHz - 69: 855.25MHz
630  *
631  * Cable channels
632  *
633  * 70: 111.25MHz - 77: 167.25MHz
634  * 78: 231.25MHz -107: 463.25MHz
635  *
636  * IF freq: 38.90 MHz
637  */
638 #define IF_FREQ 38.90
639 static int xussr[] = {
640       107,     (int)(IF_FREQ * FREQFACTOR),    0,
641        78,     (int)(231.25 * FREQFACTOR),     (int)(8.00 * FREQFACTOR), 
642        70,     (int)(111.25 * FREQFACTOR),     (int)(8.00 * FREQFACTOR),
643        35,     (int)(583.25 * FREQFACTOR),     (int)(8.00 * FREQFACTOR), 
644        21,     (int)(471.25 * FREQFACTOR),     (int)(8.00 * FREQFACTOR),
645         6,     (int)(175.25 * FREQFACTOR),     (int)(8.00 * FREQFACTOR),  
646         3,     (int)( 77.25 * FREQFACTOR),     (int)(8.00 * FREQFACTOR),  
647         1,     (int)( 49.75 * FREQFACTOR),     (int)(9.50 * FREQFACTOR),
648         0
649 };
650 #undef IF_FREQ
651
652 /*
653  * Australian broadcast channels
654  */
655 #define OFFSET  7.00
656 #define IF_FREQ 38.90 
657 static int australia[] = {
658        83,     (int)(IF_FREQ * FREQFACTOR),    0,
659        28,     (int)(527.25 * FREQFACTOR),     (int)(OFFSET * FREQFACTOR),
660        10,     (int)(209.25 * FREQFACTOR),     (int)(OFFSET * FREQFACTOR),
661         6,     (int)(175.25 * FREQFACTOR),     (int)(OFFSET * FREQFACTOR),
662         4,     (int)( 95.25 * FREQFACTOR),     (int)(OFFSET * FREQFACTOR),
663         3,     (int)( 86.25 * FREQFACTOR),     (int)(OFFSET * FREQFACTOR),
664         1,     (int)( 57.25 * FREQFACTOR),     (int)(OFFSET * FREQFACTOR),
665         0
666 };
667 #undef OFFSET
668 #undef IF_FREQ
669
670 /* 
671  * France broadcast channels
672  */
673 #define OFFSET 8.00
674 #define IF_FREQ 38.90
675 static int france[] = {
676         69,     (int)(IF_FREQ * FREQFACTOR),     0,
677         21,     (int)(471.25 * FREQFACTOR),     (int)(OFFSET * FREQFACTOR), /* 21 -> 69 */
678          5,     (int)(176.00 * FREQFACTOR),     (int)(OFFSET * FREQFACTOR), /* 5 -> 10 */
679          4,     (int)( 63.75 * FREQFACTOR),     (int)(OFFSET * FREQFACTOR), /* 4    */
680          3,     (int)( 60.50 * FREQFACTOR),     (int)(OFFSET * FREQFACTOR), /* 3    */
681          1,     (int)( 47.75 * FREQFACTOR),     (int)(OFFSET * FREQFACTOR), /* 1  2 */
682          0
683 }; 
684 #undef OFFSET
685 #undef IF_FREQ
686
687 static struct {
688         int     *ptr;
689         char    name[BT848_MAX_CHNLSET_NAME_LEN];
690 } freqTable[] = {
691         {NULL,          ""},
692         {nabcst,        "nabcst"},
693         {irccable,      "cableirc"},
694         {hrccable,      "cablehrc"},
695         {weurope,       "weurope"},
696         {jpnbcst,       "jpnbcst"},
697         {jpncable,      "jpncable"},
698         {xussr,         "xussr"},
699         {australia,     "australia"},
700         {france,        "france"},
701  
702 };
703
704 #define TBL_CHNL        freqTable[ bktr->tuner.chnlset ].ptr[ x ]
705 #define TBL_BASE_FREQ   freqTable[ bktr->tuner.chnlset ].ptr[ x + 1 ]
706 #define TBL_OFFSET      freqTable[ bktr->tuner.chnlset ].ptr[ x + 2 ]
707 static int
708 frequency_lookup( bktr_ptr_t bktr, int channel )
709 {
710         int     x;
711
712         /* check for "> MAX channel" */
713         x = 0;
714         if ( channel > TBL_CHNL )
715                 return( -1 );
716
717         /* search the table for data */
718         for ( x = 3; TBL_CHNL; x += 3 ) {
719                 if ( channel >= TBL_CHNL ) {
720                         return( TBL_BASE_FREQ +
721                                  ((channel - TBL_CHNL) * TBL_OFFSET) );
722                 }
723         }
724
725         /* not found, must be below the MIN channel */
726         return( -1 );
727 }
728 #undef TBL_OFFSET
729 #undef TBL_BASE_FREQ
730 #undef TBL_CHNL
731
732
733 #define TBL_IF  (bktr->format_params == BT848_IFORM_F_NTSCJ || \
734                  bktr->format_params == BT848_IFORM_F_NTSCM ? \
735                  nabcst[1] : weurope[1])
736
737
738 /* Initialise the tuner structures in the bktr_softc */
739 /* This is needed as the tuner details are no longer globally declared */
740
741 void    select_tuner( bktr_ptr_t bktr, int tuner_type ) {
742         if (tuner_type < Bt848_MAX_TUNER) {
743                 bktr->card.tuner = &tuners[ tuner_type ];
744         } else {
745                 bktr->card.tuner = NULL;
746         }
747 }
748
749 /*
750  * Tuner Notes:
751  * Programming the tuner properly is quite complicated.
752  * Here are some notes, based on a FM1246 data sheet for a PAL-I tuner.
753  * The tuner (front end) covers 45.75 Mhz - 855.25 Mhz and an FM band of
754  * 87.5 Mhz to 108.0 Mhz.
755  *
756  * RF and IF.  RF = radio frequencies, it is the transmitted signal.
757  *             IF is the Intermediate Frequency (the offset from the base
758  *             signal where the video, color,  audio and NICAM signals are.
759  *
760  * Eg, Picture at 38.9 Mhz, Colour at 34.47 MHz, sound at 32.9 MHz
761  * NICAM at 32.348 Mhz.
762  * Strangely enough, there is an IF (intermediate frequency) for
763  * FM Radio which is 10.7 Mhz.
764  *
765  * The tuner also works in Bands. Philips bands are
766  * FM radio band 87.50 to 108.00 MHz
767  * Low band 45.75 to 170.00 MHz
768  * Mid band 170.00 to 450.00 MHz
769  * High band 450.00 to 855.25 MHz
770  *
771  *
772  * Now we need to set the PLL on the tuner to the required freuqncy.
773  * It has a programmable divisor.
774  * For TV we want
775  *  N = 16 (freq RF(pc) + freq IF(pc))  pc is picture carrier and RF and IF
776  *  are in MHz.
777
778  * For RADIO we want a different equation.
779  *  freq IF is 10.70 MHz (so the data sheet tells me)
780  * N = (freq RF + freq IF) / step size
781  * The step size must be set to 50 khz (so the data sheet tells me)
782  * (note this is 50 kHz, the other things are in MHz)
783  * so we end up with N = 20x(freq RF + 10.7)
784  *
785  */
786
787 #define LOW_BAND 0
788 #define MID_BAND 1
789 #define HIGH_BAND 2
790 #define FM_RADIO_BAND 3
791
792
793 /* Check if these are correct for other than Philips PAL */
794 #define STATUSBIT_COLD   0x80
795 #define STATUSBIT_LOCK   0x40
796 #define STATUSBIT_TV     0x20
797 #define STATUSBIT_STEREO 0x10 /* valid if FM (aka not TV) */
798 #define STATUSBIT_ADC    0x07
799
800 /*
801  * set the frequency of the tuner
802  * If 'type' is TV_FREQUENCY, the frequency is freq MHz*16
803  * If 'type' is FM_RADIO_FREQUENCY, the frequency is freq MHz * 100 
804  * (note *16 gives is 4 bits of fraction, eg steps of nnn.0625)
805  *
806  */
807 int
808 tv_freq( bktr_ptr_t bktr, int frequency, int type )
809 {
810         const struct TUNER*     tuner;
811         u_char                  addr;
812         u_char                  control;
813         u_char                  band;
814         int                     N;
815         int                     band_select = 0;
816 #if defined( TEST_TUNER_AFC )
817         int                     oldFrequency, afcDelta;
818 #endif
819
820         tuner = bktr->card.tuner;
821         if ( tuner == NULL )
822                 return( -1 );
823
824         if (tuner == &tuners[TUNER_MT2032]) {
825                 mt2032_set_tv_freq(bktr, frequency);
826                 return 0;
827         }
828         if (type == TV_FREQUENCY) {
829                 /*
830                  * select the band based on frequency
831                  * XXX FIXME: get the cross-over points from the tuner struct
832                  */
833                 if ( frequency < (160 * FREQFACTOR  ) )
834                     band_select = LOW_BAND;
835                 else if ( frequency < (454 * FREQFACTOR ) )
836                     band_select = MID_BAND;
837                 else
838                     band_select = HIGH_BAND;
839
840 #if defined( TEST_TUNER_AFC )
841                 if ( bktr->tuner.afc )
842                         frequency -= 4;
843 #endif
844                 /*
845                  * N = 16 * { fRF(pc) + fIF(pc) }
846                  * or N = 16* fRF(pc) + 16*fIF(pc) }
847                  * where:
848                  *  pc is picture carrier, fRF & fIF are in MHz
849                  *
850                  * fortunatly, frequency is passed in as MHz * 16
851                  * and the TBL_IF frequency is also stored in MHz * 16
852                  */
853                 N = frequency + TBL_IF;
854
855                 /* set the address of the PLL */
856                 addr    = bktr->card.tuner_pllAddr;
857                 control = tuner->pllControl[ band_select ];
858                 band    = tuner->bandAddrs[ band_select ];
859
860                 if(!(band && control))          /* Don't try to set un- */
861                   return(-1);                   /* supported modes.     */
862
863                 if ( frequency > bktr->tuner.frequency ) {
864                         i2cWrite( bktr, addr, (N>>8) & 0x7f, N & 0xff );
865                         i2cWrite( bktr, addr, control, band );
866                 }
867                 else {
868                         i2cWrite( bktr, addr, control, band );
869                         i2cWrite( bktr, addr, (N>>8) & 0x7f, N & 0xff );
870                 }
871
872 #if defined( TUNER_AFC )
873                 if ( bktr->tuner.afc == TRUE ) {
874 #if defined( TEST_TUNER_AFC )
875                         oldFrequency = frequency;
876 #endif
877                         if ( (N = do_afc( bktr, addr, N )) < 0 ) {
878                             /* AFC failed, restore requested frequency */
879                             N = frequency + TBL_IF;
880 #if defined( TEST_TUNER_AFC )
881                             printf("%s: do_afc: failed to lock\n",
882                                    bktr_name(bktr));
883 #endif
884                             i2cWrite( bktr, addr, (N>>8) & 0x7f, N & 0xff );
885                         }
886                         else
887                             frequency = N - TBL_IF;
888 #if defined( TEST_TUNER_AFC )
889  printf("%s: do_afc: returned freq %d (%d %% %d)\n", bktr_name(bktr), frequency, frequency / 16, frequency % 16);
890                             afcDelta = frequency - oldFrequency;
891  printf("%s: changed by: %d clicks (%d mod %d)\n", bktr_name(bktr), afcDelta, afcDelta / 16, afcDelta % 16);
892 #endif
893                         }
894 #endif /* TUNER_AFC */
895
896                 bktr->tuner.frequency = frequency;
897         }
898
899         if ( type == FM_RADIO_FREQUENCY ) {
900                 band_select = FM_RADIO_BAND;
901
902                 /*
903                  * N = { fRF(pc) + fIF(pc) }/step_size
904                  * The step size is 50kHz for FM radio.
905                  * (eg after 102.35MHz comes 102.40 MHz)
906                  * fIF is 10.7 MHz (as detailed in the specs)
907                  *
908                  * frequency is passed in as MHz * 100
909                  *
910                  * So, we have N = (frequency/100 + 10.70)  /(50/1000)
911                  */
912                 N = (frequency + 1070)/5;
913
914                 /* set the address of the PLL */
915                 addr    = bktr->card.tuner_pllAddr;
916                 control = tuner->pllControl[ band_select ];
917                 band    = tuner->bandAddrs[ band_select ];
918
919                 if(!(band && control))          /* Don't try to set un- */
920                   return(-1);                   /* supported modes.     */
921           
922                 band |= bktr->tuner.radio_mode; /* tuner.radio_mode is set in
923                                                  * the ioctls RADIO_SETMODE
924                                                  * and RADIO_GETMODE */
925
926                 i2cWrite( bktr, addr, control, band );
927                 i2cWrite( bktr, addr, (N>>8) & 0x7f, N & 0xff );
928
929                 bktr->tuner.frequency = (N * 5) - 1070;
930
931
932         }
933  
934
935         return( 0 );
936 }
937
938
939
940 #if defined( TUNER_AFC )
941 /*
942  * 
943  */
944 int
945 do_afc( bktr_ptr_t bktr, int addr, int frequency )
946 {
947         int step;
948         int status;
949         int origFrequency;
950
951         origFrequency = frequency;
952
953         /* wait for first setting to take effect */
954         tsleep( BKTR_SLEEP, PZERO, "tuning", hz/8 );
955
956         if ( (status = i2cRead( bktr, addr + 1 )) < 0 )
957                 return( -1 );
958
959 #if defined( TEST_TUNER_AFC )
960  printf( "%s: Original freq: %d, status: 0x%02x\n", bktr_name(bktr), frequency, status );
961 #endif
962         for ( step = 0; step < AFC_MAX_STEP; ++step ) {
963                 if ( (status = i2cRead( bktr, addr + 1 )) < 0 )
964                         goto fubar;
965                 if ( !(status & 0x40) ) {
966 #if defined( TEST_TUNER_AFC )
967  printf( "%s: no lock!\n", bktr_name(bktr) );
968 #endif
969                         goto fubar;
970                 }
971
972                 switch( status & AFC_BITS ) {
973                 case AFC_FREQ_CENTERED:
974 #if defined( TEST_TUNER_AFC )
975  printf( "%s: Centered, freq: %d, status: 0x%02x\n", bktr_name(bktr), frequency, status );
976 #endif
977                         return( frequency );
978
979                 case AFC_FREQ_MINUS_125:
980                 case AFC_FREQ_MINUS_62:
981 #if defined( TEST_TUNER_AFC )
982  printf( "%s: Low, freq: %d, status: 0x%02x\n", bktr_name(bktr), frequency, status );
983 #endif
984                         --frequency;
985                         break;
986
987                 case AFC_FREQ_PLUS_62:
988                 case AFC_FREQ_PLUS_125:
989 #if defined( TEST_TUNER_AFC )
990  printf( "%s: Hi, freq: %d, status: 0x%02x\n", bktr_name(bktr), frequency, status );
991 #endif
992                         ++frequency;
993                         break;
994                 }
995
996                 i2cWrite( bktr, addr,
997                           (frequency>>8) & 0x7f, frequency & 0xff );
998                 DELAY( AFC_DELAY );
999         }
1000
1001  fubar:
1002         i2cWrite( bktr, addr,
1003                   (origFrequency>>8) & 0x7f, origFrequency & 0xff );
1004
1005         return( -1 );
1006 }
1007 #endif /* TUNER_AFC */
1008 #undef TBL_IF
1009
1010
1011 /*
1012  * Get the Tuner status and signal strength
1013  */
1014 int     get_tuner_status( bktr_ptr_t bktr ) {
1015         if (bktr->card.tuner == &tuners[TUNER_MT2032])
1016                 return 0;
1017         return i2cRead( bktr, bktr->card.tuner_pllAddr + 1 );
1018 }
1019
1020 /*
1021  * set the channel of the tuner
1022  */
1023 int
1024 tv_channel( bktr_ptr_t bktr, int channel )
1025 {
1026         int frequency;
1027
1028         /* calculate the frequency according to tuner type */
1029         if ( (frequency = frequency_lookup( bktr, channel )) < 0 )
1030                 return( -1 );
1031
1032         /* set the new frequency */
1033         if ( tv_freq( bktr, frequency, TV_FREQUENCY ) < 0 )
1034                 return( -1 );
1035
1036         /* OK to update records */
1037         return( (bktr->tuner.channel = channel) );
1038 }
1039
1040 /*
1041  * get channelset name
1042  */
1043 int
1044 tuner_getchnlset(struct bktr_chnlset *chnlset)
1045 {
1046        if (( chnlset->index < CHNLSET_MIN ) ||
1047                ( chnlset->index > CHNLSET_MAX ))
1048                        return( EINVAL );
1049
1050        memcpy(&chnlset->name, &freqTable[chnlset->index].name,
1051                BT848_MAX_CHNLSET_NAME_LEN);
1052
1053        chnlset->max_channel=freqTable[chnlset->index].ptr[0];
1054        return( 0 );
1055 }
1056
1057
1058
1059
1060 #define TDA9887_ADDR    0x86
1061
1062 static int
1063 TDA9887_init(bktr_ptr_t bktr, int output2_enable)
1064 {
1065         u_char addr = TDA9887_ADDR;
1066
1067         i2cWrite(bktr, addr, 0, output2_enable ? 0x50 : 0xd0);
1068         i2cWrite(bktr, addr, 1, 0x6e); /* takeover point / de-emphasis */
1069
1070         /* PAL BG: 0x09  PAL I: 0x0a  NTSC: 0x04 */
1071 #ifdef MT2032_NTSC
1072         i2cWrite(bktr, addr, 2, 0x04);
1073 #else
1074         i2cWrite(bktr, addr, 2, 0x09);
1075 #endif
1076         return 0;
1077 }
1078
1079
1080
1081 #define MT2032_OPTIMIZE_VCO      1
1082
1083 /* holds the value of XOGC register after init */
1084 static int      MT2032_XOGC = 4;
1085
1086 /* card.tuner_pllAddr not set during init */
1087 #define MT2032_ADDR             0xc0
1088
1089 #ifndef MT2032_ADDR
1090 #define MT2032_ADDR             (bktr->card.tuner_pllAddr)
1091 #endif
1092
1093 static int 
1094 _MT2032_GetRegister(bktr_ptr_t bktr, u_char regNum)
1095 {
1096         int             ch;
1097
1098         if (i2cWrite(bktr, MT2032_ADDR, regNum, -1) == -1) {
1099                 if (bootverbose)
1100                         printf("%s: MT2032 write failed (i2c addr %#x)\n",
1101                                 bktr_name(bktr), MT2032_ADDR);
1102                 return -1;
1103         }
1104         if ((ch = i2cRead(bktr, MT2032_ADDR + 1)) == -1) {
1105                 if (bootverbose)
1106                         printf("%s: MT2032 get register %d failed\n",
1107                                 bktr_name(bktr), regNum);
1108                 return -1;
1109         }
1110         return ch;
1111 }
1112
1113 static void 
1114 _MT2032_SetRegister(bktr_ptr_t bktr, u_char regNum, u_char data)
1115 {
1116         i2cWrite(bktr, MT2032_ADDR, regNum, data);
1117 }
1118
1119 #define MT2032_GetRegister(r)           _MT2032_GetRegister(bktr,r)
1120 #define MT2032_SetRegister(r,d)         _MT2032_SetRegister(bktr,r,d)
1121
1122
1123 int 
1124 mt2032_init(bktr_ptr_t bktr)
1125 {
1126         u_char            rdbuf[22];
1127         int             xogc, xok = 0;
1128         int             i;
1129         int             x;
1130
1131         TDA9887_init(bktr, 0);
1132
1133         for (i = 0; i < 21; i++) {
1134                 if ((x = MT2032_GetRegister(i)) == -1)
1135                         break;
1136                 rdbuf[i] = x;
1137         }
1138         if (i < 21)
1139                 return -1;
1140
1141         printf("%s: MT2032: Companycode=%02x%02x Part=%02x Revision=%02x\n",
1142                 bktr_name(bktr),
1143                 rdbuf[0x11], rdbuf[0x12], rdbuf[0x13], rdbuf[0x14]);
1144         if (rdbuf[0x13] != 4) {
1145                 printf("%s: MT2032 not found or unknown type\n", bktr_name(bktr));
1146                 return -1;
1147         }
1148
1149         /* Initialize Registers per spec. */
1150         MT2032_SetRegister(2, 0xff);
1151         MT2032_SetRegister(3, 0x0f);
1152         MT2032_SetRegister(4, 0x1f);
1153         MT2032_SetRegister(6, 0xe4);
1154         MT2032_SetRegister(7, 0x8f);
1155         MT2032_SetRegister(8, 0xc3);
1156         MT2032_SetRegister(9, 0x4e);
1157         MT2032_SetRegister(10, 0xec);
1158         MT2032_SetRegister(13, 0x32);
1159
1160         /* Adjust XOGC (register 7), wait for XOK */
1161         xogc = 7;
1162         do {
1163                 DELAY(10000);
1164                 xok = MT2032_GetRegister(0x0e) & 0x01;
1165                 if (xok == 1) {
1166                         break;
1167                 }
1168                 xogc--;
1169                 if (xogc == 3) {
1170                         xogc = 4;       /* min. 4 per spec */
1171                         break;
1172                 }
1173                 MT2032_SetRegister(7, 0x88 + xogc);
1174         } while (xok != 1);
1175
1176         TDA9887_init(bktr, 1);
1177
1178         MT2032_XOGC = xogc;
1179
1180         return 0;
1181 }
1182
1183 static int 
1184 MT2032_SpurCheck(int f1, int f2, int spectrum_from, int spectrum_to)
1185 {
1186         int             n1 = 1, n2, f;
1187
1188         f1 = f1 / 1000;         /* scale to kHz to avoid 32bit overflows */
1189         f2 = f2 / 1000;
1190         spectrum_from /= 1000;
1191         spectrum_to /= 1000;
1192
1193         do {
1194                 n2 = -n1;
1195                 f = n1 * (f1 - f2);
1196                 do {
1197                         n2--;
1198                         f = f - f2;
1199                         if ((f > spectrum_from) && (f < spectrum_to)) {
1200                                 return 1;
1201                         }
1202                 } while ((f > (f2 - spectrum_to)) || (n2 > -5));
1203                 n1++;
1204         } while (n1 < 5);
1205
1206         return 0;
1207 }
1208
1209 static int
1210 MT2032_ComputeFreq(
1211                    int rfin,
1212                    int if1,
1213                    int if2,
1214                    int spectrum_from,
1215                    int spectrum_to,
1216                    unsigned char *buf,
1217                    int *ret_sel,
1218                    int xogc
1219 )
1220 {                               /* all in Hz */
1221         int             fref, lo1, lo1n, lo1a, s, sel;
1222         int             lo1freq, desired_lo1, desired_lo2, lo2, lo2n, lo2a,
1223                         lo2num, lo2freq;
1224         int             nLO1adjust;
1225
1226         fref = 5250 * 1000;     /* 5.25MHz */
1227
1228         /* per spec 2.3.1 */
1229         desired_lo1 = rfin + if1;
1230         lo1 = (2 * (desired_lo1 / 1000) + (fref / 1000)) / (2 * fref / 1000);
1231         lo1freq = lo1 * fref;
1232         desired_lo2 = lo1freq - rfin - if2;
1233
1234         /* per spec 2.3.2 */
1235         for (nLO1adjust = 1; nLO1adjust < 3; nLO1adjust++) {
1236                 if (!MT2032_SpurCheck(lo1freq, desired_lo2, spectrum_from, spectrum_to)) {
1237                         break;
1238                 }
1239                 if (lo1freq < desired_lo1) {
1240                         lo1 += nLO1adjust;
1241                 } else {
1242                         lo1 -= nLO1adjust;
1243                 }
1244
1245                 lo1freq = lo1 * fref;
1246                 desired_lo2 = lo1freq - rfin - if2;
1247         }
1248
1249         /* per spec 2.3.3 */
1250         s = lo1freq / 1000 / 1000;
1251
1252         if (MT2032_OPTIMIZE_VCO) {
1253                 if (s > 1890) {
1254                         sel = 0;
1255                 } else if (s > 1720) {
1256                         sel = 1;
1257                 } else if (s > 1530) {
1258                         sel = 2;
1259                 } else if (s > 1370) {
1260                         sel = 3;
1261                 } else {
1262                         sel = 4;/* >1090 */
1263                 }
1264         } else {
1265                 if (s > 1790) {
1266                         sel = 0;/* <1958 */
1267                 } else if (s > 1617) {
1268                         sel = 1;
1269                 } else if (s > 1449) {
1270                         sel = 2;
1271                 } else if (s > 1291) {
1272                         sel = 3;
1273                 } else {
1274                         sel = 4;/* >1090 */
1275                 }
1276         }
1277
1278         *ret_sel = sel;
1279
1280         /* per spec 2.3.4 */
1281         lo1n = lo1 / 8;
1282         lo1a = lo1 - (lo1n * 8);
1283         lo2 = desired_lo2 / fref;
1284         lo2n = lo2 / 8;
1285         lo2a = lo2 - (lo2n * 8);
1286         /* scale to fit in 32bit arith */
1287         lo2num = ((desired_lo2 / 1000) % (fref / 1000)) * 3780 / (fref / 1000);
1288         lo2freq = (lo2a + 8 * lo2n) * fref + lo2num * (fref / 1000) / 3780 * 1000;
1289
1290         if (lo1a < 0 || lo1a > 7 || lo1n < 17 || lo1n > 48 || lo2a < 0 ||
1291             lo2a > 7 || lo2n < 17 || lo2n > 30) {
1292                 printf("MT2032: parameter out of range\n");
1293                 return -1;
1294         }
1295         /* set up MT2032 register map for transfer over i2c */
1296         buf[0] = lo1n - 1;
1297         buf[1] = lo1a | (sel << 4);
1298         buf[2] = 0x86;          /* LOGC */
1299         buf[3] = 0x0f;          /* reserved */
1300         buf[4] = 0x1f;
1301         buf[5] = (lo2n - 1) | (lo2a << 5);
1302         if (rfin < 400 * 1000 * 1000) {
1303                 buf[6] = 0xe4;
1304         } else {
1305                 buf[6] = 0xf4;  /* set PKEN per rev 1.2 */
1306         }
1307
1308         buf[7] = 8 + xogc;
1309         buf[8] = 0xc3;          /* reserved */
1310         buf[9] = 0x4e;          /* reserved */
1311         buf[10] = 0xec;         /* reserved */
1312         buf[11] = (lo2num & 0xff);
1313         buf[12] = (lo2num >> 8) | 0x80; /* Lo2RST */
1314
1315         return 0;
1316 }
1317
1318 static int 
1319 MT2032_CheckLOLock(bktr_ptr_t bktr)
1320 {
1321         int             t, lock = 0;
1322         for (t = 0; t < 10; t++) {
1323                 lock = MT2032_GetRegister(0x0e) & 0x06;
1324                 if (lock == 6) {
1325                         break;
1326                 }
1327                 DELAY(1000);
1328         }
1329         return lock;
1330 }
1331
1332 static int 
1333 MT2032_OptimizeVCO(bktr_ptr_t bktr, int sel, int lock)
1334 {
1335         int             tad1, lo1a;
1336
1337         tad1 = MT2032_GetRegister(0x0f) & 0x07;
1338
1339         if (tad1 == 0) {
1340                 return lock;
1341         }
1342         if (tad1 == 1) {
1343                 return lock;
1344         }
1345         if (tad1 == 2) {
1346                 if (sel == 0) {
1347                         return lock;
1348                 } else {
1349                         sel--;
1350                 }
1351         } else {
1352                 if (sel < 4) {
1353                         sel++;
1354                 } else {
1355                         return lock;
1356                 }
1357         }
1358         lo1a = MT2032_GetRegister(0x01) & 0x07;
1359         MT2032_SetRegister(0x01, lo1a | (sel << 4));
1360         lock = MT2032_CheckLOLock(bktr);
1361         return lock;
1362 }
1363
1364 static int
1365 MT2032_SetIFFreq(bktr_ptr_t bktr, int rfin, int if1, int if2, int from, int to)
1366 {
1367         u_char          buf[21];
1368         int             lint_try, sel, lock = 0;
1369
1370         if (MT2032_ComputeFreq(rfin, if1, if2, from, to, &buf[0], &sel, MT2032_XOGC) == -1)
1371                 return -1;
1372
1373         TDA9887_init(bktr, 0);
1374
1375         /* send only the relevant registers per Rev. 1.2 */
1376         MT2032_SetRegister(0, buf[0x00]);
1377         MT2032_SetRegister(1, buf[0x01]);
1378         MT2032_SetRegister(2, buf[0x02]);
1379
1380         MT2032_SetRegister(5, buf[0x05]);
1381         MT2032_SetRegister(6, buf[0x06]);
1382         MT2032_SetRegister(7, buf[0x07]);
1383
1384         MT2032_SetRegister(11, buf[0x0B]);
1385         MT2032_SetRegister(12, buf[0x0C]);
1386
1387         /* wait for PLLs to lock (per manual), retry LINT if not. */
1388         for (lint_try = 0; lint_try < 2; lint_try++) {
1389                 lock = MT2032_CheckLOLock(bktr);
1390
1391                 if (MT2032_OPTIMIZE_VCO) {
1392                         lock = MT2032_OptimizeVCO(bktr, sel, lock);
1393                 }
1394                 if (lock == 6) {
1395                         break;
1396                 }
1397                 /* set LINT to re-init PLLs */
1398                 MT2032_SetRegister(7, 0x80 + 8 + MT2032_XOGC);
1399                 DELAY(10000);
1400                 MT2032_SetRegister(7, 8 + MT2032_XOGC);
1401         }
1402         if (lock != 6)
1403                 printf("%s: PLL didn't lock\n", bktr_name(bktr));
1404
1405         MT2032_SetRegister(2, 0x20);
1406
1407         TDA9887_init(bktr, 1);
1408         return 0;
1409 }
1410
1411 static void
1412 mt2032_set_tv_freq(bktr_ptr_t bktr, unsigned int freq)
1413 {
1414         int if2,from,to;
1415         int stat, tad;
1416
1417 #ifdef MT2032_NTSC
1418         from=40750*1000;
1419         to=46750*1000;
1420         if2=45750*1000;
1421 #else
1422         from=32900*1000;
1423         to=39900*1000;
1424         if2=38900*1000;
1425 #endif
1426
1427         if (MT2032_SetIFFreq(bktr, freq*62500 /* freq*1000*1000/16 */,
1428                         1090*1000*1000, if2, from, to) == 0) {
1429                 bktr->tuner.frequency = freq;
1430                 stat = MT2032_GetRegister(0x0e);
1431                 tad = MT2032_GetRegister(0x0f);
1432                 if (bootverbose)
1433                         printf("%s: frequency set to %d, st = %#x, tad = %#x\n",
1434                                 bktr_name(bktr), freq*62500, stat, tad);
1435         }
1436 }