Merge from vendor branch READLINE:
[dragonfly.git] / sys / dev / video / bktr / bktr_tuner.c
1 /*
2  * 1. Redistributions of source code must retain the
3  * Copyright (c) 1997 Amancio Hasty, 1999 Roger Hardiman
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  * 3. All advertising materials mentioning features or use of this software
15  *    must display the following acknowledgement:
16  *      This product includes software developed by Amancio Hasty and
17  *      Roger Hardiman
18  * 4. The name of the author may not be used to endorse or promote products
19  *    derived from this software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
23  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24  * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
25  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
26  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
29  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
30  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31  * POSSIBILITY OF SUCH DAMAGE.
32  *
33  * $FreeBSD: src/sys/dev/bktr/bktr_tuner.c,v 1.12 2003/12/08 07:59:18 obrien Exp $
34  * $DragonFly: src/sys/dev/video/bktr/bktr_tuner.c,v 1.6 2004/05/15 17:54:13 joerg Exp $
35  */
36
37
38 /*
39  * This is part of the Driver for Video Capture Cards (Frame grabbers)
40  * and TV Tuner cards using the Brooktree Bt848, Bt848A, Bt849A, Bt878, Bt879
41  * chipset.
42  * Copyright Roger Hardiman and Amancio Hasty.
43  *
44  * bktr_tuner : This deals with controlling the tuner fitted to TV cards.
45  */
46
47 #include <sys/param.h>
48 #include <sys/systm.h>
49 #include <sys/kernel.h>
50 #include <sys/vnode.h>
51
52 #include <bus/pci/pcivar.h>
53
54 #include <machine/bus_memio.h>          /* for bus space */
55 #include <machine/bus.h>
56 #include <sys/bus.h>
57
58 #include <dev/video/meteor/ioctl_meteor.h>
59 #include <dev/video/bktr/ioctl_bt848.h> /* extensions to ioctl_meteor.h */
60 #include <dev/video/bktr/bktr_reg.h>
61 #include <dev/video/bktr/bktr_tuner.h>
62 #include <dev/video/bktr/bktr_card.h>
63 #include <dev/video/bktr/bktr_core.h>
64
65
66
67 #if defined( TUNER_AFC )
68 #define AFC_DELAY               10000   /* 10 millisend delay */
69 #define AFC_BITS                0x07
70 #define AFC_FREQ_MINUS_125      0x00
71 #define AFC_FREQ_MINUS_62       0x01
72 #define AFC_FREQ_CENTERED       0x02
73 #define AFC_FREQ_PLUS_62        0x03
74 #define AFC_FREQ_PLUS_125       0x04
75 #define AFC_MAX_STEP            (5 * FREQFACTOR) /* no more than 5 MHz */
76 #endif /* TUNER_AFC */
77
78   
79 #define TTYPE_XXX               0
80 #define TTYPE_NTSC              1
81 #define TTYPE_NTSC_J            2
82 #define TTYPE_PAL               3
83 #define TTYPE_PAL_M             4
84 #define TTYPE_PAL_N             5
85 #define TTYPE_SECAM             6
86   
87 #define TSA552x_CB_MSB          (0x80)
88 #define TSA552x_CB_CP           (1<<6)  /* set this for fast tuning */
89 #define TSA552x_CB_T2           (1<<5)  /* test mode - Normally set to 0 */
90 #define TSA552x_CB_T1           (1<<4)  /* test mode - Normally set to 0 */
91 #define TSA552x_CB_T0           (1<<3)  /* test mode - Normally set to 1 */
92 #define TSA552x_CB_RSA          (1<<2)  /* 0 for 31.25 khz, 1 for 62.5 kHz */
93 #define TSA552x_CB_RSB          (1<<1)  /* 0 for FM 50kHz steps, 1 = Use RSA*/
94 #define TSA552x_CB_OS           (1<<0)  /* Set to 0 for normal operation */
95
96 #define TSA552x_RADIO           (TSA552x_CB_MSB |       \
97                                  TSA552x_CB_T0)
98
99 /* raise the charge pump voltage for fast tuning */
100 #define TSA552x_FCONTROL        (TSA552x_CB_MSB |       \
101                                  TSA552x_CB_CP  |       \
102                                  TSA552x_CB_T0  |       \
103                                  TSA552x_CB_RSA |       \
104                                  TSA552x_CB_RSB)
105   
106 /* lower the charge pump voltage for better residual oscillator FM */
107 #define TSA552x_SCONTROL        (TSA552x_CB_MSB |       \
108                                  TSA552x_CB_T0  |       \
109                                  TSA552x_CB_RSA |       \
110                                  TSA552x_CB_RSB)
111   
112 /* The control value for the ALPS TSCH5 Tuner */
113 #define TSCH5_FCONTROL          0x82
114 #define TSCH5_RADIO             0x86
115   
116 /* The control value for the ALPS TSBH1 Tuner */
117 #define TSBH1_FCONTROL          0xce
118
119
120 static const struct TUNER tuners[] = {
121 /* XXX FIXME: fill in the band-switch crosspoints */
122         /* NO_TUNER */
123         { "<no>",                               /* the 'name' */
124            TTYPE_XXX,                           /* input type */
125            { 0x00,                              /* control byte for Tuner PLL */
126              0x00,
127              0x00,
128              0x00 },
129            { 0x00, 0x00 },                      /* band-switch crosspoints */
130            { 0x00, 0x00, 0x00,0x00} },          /* the band-switch values */
131
132         /* TEMIC_NTSC */
133         { "Temic NTSC",                         /* the 'name' */
134            TTYPE_NTSC,                          /* input type */
135            { TSA552x_SCONTROL,                  /* control byte for Tuner PLL */
136              TSA552x_SCONTROL,
137              TSA552x_SCONTROL,
138              0x00 },
139            { 0x00, 0x00},                       /* band-switch crosspoints */
140            { 0x02, 0x04, 0x01, 0x00 } },        /* the band-switch values */
141
142         /* TEMIC_PAL */
143         { "Temic PAL",                          /* the 'name' */
144            TTYPE_PAL,                           /* input type */
145            { TSA552x_SCONTROL,                  /* control byte for Tuner PLL */
146              TSA552x_SCONTROL,
147              TSA552x_SCONTROL,
148              0x00 },
149            { 0x00, 0x00 },                      /* band-switch crosspoints */
150            { 0x02, 0x04, 0x01, 0x00 } },        /* the band-switch values */
151
152         /* TEMIC_SECAM */
153         { "Temic SECAM",                        /* the 'name' */
154            TTYPE_SECAM,                         /* input type */
155            { TSA552x_SCONTROL,                  /* control byte for Tuner PLL */
156              TSA552x_SCONTROL,
157              TSA552x_SCONTROL,
158              0x00 },
159            { 0x00, 0x00 },                      /* band-switch crosspoints */
160            { 0x02, 0x04, 0x01,0x00 } },         /* the band-switch values */
161
162         /* PHILIPS_NTSC */
163         { "Philips NTSC",                       /* the 'name' */
164            TTYPE_NTSC,                          /* input type */
165            { TSA552x_SCONTROL,                  /* control byte for Tuner PLL */
166              TSA552x_SCONTROL,
167              TSA552x_SCONTROL,
168              0x00 },
169            { 0x00, 0x00 },                      /* band-switch crosspoints */
170            { 0xa0, 0x90, 0x30, 0x00 } },        /* the band-switch values */
171
172         /* PHILIPS_PAL */
173         { "Philips PAL",                        /* the 'name' */
174            TTYPE_PAL,                           /* input type */
175            { TSA552x_SCONTROL,                  /* control byte for Tuner PLL */
176              TSA552x_SCONTROL,
177              TSA552x_SCONTROL,
178              0x00 },
179            { 0x00, 0x00 },                      /* band-switch crosspoints */
180            { 0xa0, 0x90, 0x30, 0x00 } },        /* the band-switch values */
181
182         /* PHILIPS_SECAM */
183         { "Philips SECAM",                      /* the 'name' */
184            TTYPE_SECAM,                         /* input type */
185            { TSA552x_SCONTROL,                  /* control byte for Tuner PLL */
186              TSA552x_SCONTROL,
187              TSA552x_SCONTROL,
188              0x00 },
189            { 0x00, 0x00 },                      /* band-switch crosspoints */
190            { 0xa7, 0x97, 0x37, 0x00 } },        /* the band-switch values */
191
192         /* TEMIC_PAL I */
193         { "Temic PAL I",                        /* the 'name' */
194            TTYPE_PAL,                           /* input type */
195            { TSA552x_SCONTROL,                  /* control byte for Tuner PLL */
196              TSA552x_SCONTROL,
197              TSA552x_SCONTROL,
198              0x00 },
199            { 0x00, 0x00 },                      /* band-switch crosspoints */
200            { 0x02, 0x04, 0x01,0x00 } },         /* the band-switch values */
201
202         /* PHILIPS_PALI */
203         { "Philips PAL I",                      /* the 'name' */
204            TTYPE_PAL,                           /* input type */
205            { TSA552x_SCONTROL,                  /* control byte for Tuner PLL */
206              TSA552x_SCONTROL,
207              TSA552x_SCONTROL,
208              0x00 },
209           { 0x00, 0x00 },                      /* band-switch crosspoints */
210           { 0xa0, 0x90, 0x30,0x00 } },         /* the band-switch values */
211
212        /* PHILIPS_FR1236_NTSC */
213        { "Philips FR1236 NTSC FM",             /* the 'name' */
214           TTYPE_NTSC,                          /* input type */
215           { TSA552x_FCONTROL,                   /* control byte for Tuner PLL */
216             TSA552x_FCONTROL,
217             TSA552x_FCONTROL,
218             TSA552x_RADIO  },
219           { 0x00, 0x00 },                       /* band-switch crosspoints */
220           { 0xa0, 0x90, 0x30,0xa4 } },          /* the band-switch values */
221
222         /* PHILIPS_FR1216_PAL */
223         { "Philips FR1216 PAL FM" ,             /* the 'name' */
224            TTYPE_PAL,                           /* input type */
225            { TSA552x_FCONTROL,                  /* control byte for Tuner PLL */
226              TSA552x_FCONTROL,
227              TSA552x_FCONTROL,
228              TSA552x_RADIO },
229            { 0x00, 0x00 },                      /* band-switch crosspoints */
230            { 0xa0, 0x90, 0x30, 0xa4 } },        /* the band-switch values */
231
232         /* PHILIPS_FR1236_SECAM */
233         { "Philips FR1236 SECAM FM",            /* the 'name' */
234            TTYPE_SECAM,                         /* input type */
235            { TSA552x_FCONTROL,                  /* control byte for Tuner PLL */
236              TSA552x_FCONTROL,
237              TSA552x_FCONTROL,
238              TSA552x_RADIO },
239            { 0x00, 0x00 },                      /* band-switch crosspoints */
240            { 0xa7, 0x97, 0x37, 0xa4 } },        /* the band-switch values */
241
242         /* ALPS TSCH5 NTSC */
243         { "ALPS TSCH5 NTSC FM",                 /* the 'name' */
244            TTYPE_NTSC,                          /* input type */
245            { TSCH5_FCONTROL,                    /* control byte for Tuner PLL */
246              TSCH5_FCONTROL,
247              TSCH5_FCONTROL,
248              TSCH5_RADIO },
249            { 0x00, 0x00 },                      /* band-switch crosspoints */
250            { 0x14, 0x12, 0x11, 0x04 } },        /* the band-switch values */
251
252         /* ALPS TSBH1 NTSC */
253         { "ALPS TSBH1 NTSC",                    /* the 'name' */
254            TTYPE_NTSC,                          /* input type */
255            { TSBH1_FCONTROL,                    /* control byte for Tuner PLL */
256              TSBH1_FCONTROL,
257              TSBH1_FCONTROL,
258              0x00 },
259            { 0x00, 0x00 },                      /* band-switch crosspoints */
260            { 0x01, 0x02, 0x08, 0x00 } }         /* the band-switch values */
261 };
262
263
264 /* scaling factor for frequencies expressed as ints */
265 #define FREQFACTOR              16
266
267 /*
268  * Format:
269  *      entry 0:         MAX legal channel
270  *      entry 1:         IF frequency
271  *                       expressed as fi{mHz} * 16,
272  *                       eg 45.75mHz == 45.75 * 16 = 732
273  *      entry 2:         [place holder/future]
274  *      entry 3:         base of channel record 0
275  *      entry 3 + (x*3): base of channel record 'x'
276  *      entry LAST:      NULL channel entry marking end of records
277  *
278  * Record:
279  *      int 0:          base channel
280  *      int 1:          frequency of base channel,
281  *                       expressed as fb{mHz} * 16,
282  *      int 2:          offset frequency between channels,
283  *                       expressed as fo{mHz} * 16,
284  */
285
286 /*
287  * North American Broadcast Channels:
288  *
289  *  2:  55.25 mHz -  4:  67.25 mHz
290  *  5:  77.25 mHz -  6:  83.25 mHz
291  *  7: 175.25 mHz - 13: 211.25 mHz
292  * 14: 471.25 mHz - 83: 885.25 mHz
293  *
294  * IF freq: 45.75 mHz
295  */
296 #define OFFSET  6.00
297 static int nabcst[] = {
298         83,     (int)( 45.75 * FREQFACTOR),     0,
299         14,     (int)(471.25 * FREQFACTOR),     (int)(OFFSET * FREQFACTOR),
300          7,     (int)(175.25 * FREQFACTOR),     (int)(OFFSET * FREQFACTOR),
301          5,     (int)( 77.25 * FREQFACTOR),     (int)(OFFSET * FREQFACTOR),
302          2,     (int)( 55.25 * FREQFACTOR),     (int)(OFFSET * FREQFACTOR),
303          0
304 };
305 #undef OFFSET
306
307 /*
308  * North American Cable Channels, IRC:
309  *
310  *  2:  55.25 mHz -  4:  67.25 mHz
311  *  5:  77.25 mHz -  6:  83.25 mHz
312  *  7: 175.25 mHz - 13: 211.25 mHz
313  * 14: 121.25 mHz - 22: 169.25 mHz
314  * 23: 217.25 mHz - 94: 643.25 mHz
315  * 95:  91.25 mHz - 99: 115.25 mHz
316  *
317  * IF freq: 45.75 mHz
318  */
319 #define OFFSET  6.00
320 static int irccable[] = {
321         116,    (int)( 45.75 * FREQFACTOR),     0,
322         100,    (int)(649.25 * FREQFACTOR),     (int)(OFFSET * FREQFACTOR),
323         95,     (int)( 91.25 * FREQFACTOR),     (int)(OFFSET * FREQFACTOR),
324         23,     (int)(217.25 * FREQFACTOR),     (int)(OFFSET * FREQFACTOR),
325         14,     (int)(121.25 * FREQFACTOR),     (int)(OFFSET * FREQFACTOR),
326          7,     (int)(175.25 * FREQFACTOR),     (int)(OFFSET * FREQFACTOR),
327          5,     (int)( 77.25 * FREQFACTOR),     (int)(OFFSET * FREQFACTOR),
328          2,     (int)( 55.25 * FREQFACTOR),     (int)(OFFSET * FREQFACTOR),
329          0
330 };
331 #undef OFFSET
332
333 /*
334  * North American Cable Channels, HRC:
335  *
336  * 2:   54 mHz  - 4:    66 mHz
337  * 5:   78 mHz  - 6:    84 mHz
338  * 7:  174 mHz  - 13:  210 mHz
339  * 14: 120 mHz  - 22:  168 mHz
340  * 23: 216 mHz  - 94:  642 mHz
341  * 95:  90 mHz  - 99:  114 mHz
342  *
343  * IF freq: 45.75 mHz
344  */
345 #define OFFSET  6.00
346 static int hrccable[] = {
347         116,    (int)( 45.75 * FREQFACTOR),     0,
348         100,    (int)(648.00 * FREQFACTOR),     (int)(OFFSET * FREQFACTOR),
349         95,     (int)( 90.00 * FREQFACTOR),     (int)(OFFSET * FREQFACTOR),
350         23,     (int)(216.00 * FREQFACTOR),     (int)(OFFSET * FREQFACTOR),
351         14,     (int)(120.00 * FREQFACTOR),     (int)(OFFSET * FREQFACTOR),
352         7,      (int)(174.00 * FREQFACTOR),     (int)(OFFSET * FREQFACTOR),
353         5,      (int)( 78.00 * FREQFACTOR),     (int)(OFFSET * FREQFACTOR),
354         2,      (int)( 54.00 * FREQFACTOR),     (int)(OFFSET * FREQFACTOR),
355         0
356 };
357 #undef OFFSET
358
359 /*
360  * Western European broadcast channels:
361  *
362  * (there are others that appear to vary between countries - rmt)
363  *
364  * here's the table Philips provides:
365  * caution, some of the offsets don't compute...
366  *
367  *  1    4525   700     N21
368  * 
369  *  2    4825   700     E2
370  *  3    5525   700     E3
371  *  4    6225   700     E4
372  * 
373  *  5   17525   700     E5
374  *  6   18225   700     E6
375  *  7   18925   700     E7
376  *  8   19625   700     E8
377  *  9   20325   700     E9
378  * 10   21025   700     E10
379  * 11   21725   700     E11
380  * 12   22425   700     E12
381  * 
382  * 13    5375   700     ITA
383  * 14    6225   700     ITB
384  * 
385  * 15    8225   700     ITC
386  * 
387  * 16   17525   700     ITD
388  * 17   18325   700     ITE
389  * 
390  * 18   19225   700     ITF
391  * 19   20125   700     ITG
392  * 20   21025   700     ITH
393  * 
394  * 21   47125   800     E21
395  * 22   47925   800     E22
396  * 23   48725   800     E23
397  * 24   49525   800     E24
398  * 25   50325   800     E25
399  * 26   51125   800     E26
400  * 27   51925   800     E27
401  * 28   52725   800     E28
402  * 29   53525   800     E29
403  * 30   54325   800     E30
404  * 31   55125   800     E31
405  * 32   55925   800     E32
406  * 33   56725   800     E33
407  * 34   57525   800     E34
408  * 35   58325   800     E35
409  * 36   59125   800     E36
410  * 37   59925   800     E37
411  * 38   60725   800     E38
412  * 39   61525   800     E39
413  * 40   62325   800     E40
414  * 41   63125   800     E41
415  * 42   63925   800     E42
416  * 43   64725   800     E43
417  * 44   65525   800     E44
418  * 45   66325   800     E45
419  * 46   67125   800     E46
420  * 47   67925   800     E47
421  * 48   68725   800     E48
422  * 49   69525   800     E49
423  * 50   70325   800     E50
424  * 51   71125   800     E51
425  * 52   71925   800     E52
426  * 53   72725   800     E53
427  * 54   73525   800     E54
428  * 55   74325   800     E55
429  * 56   75125   800     E56
430  * 57   75925   800     E57
431  * 58   76725   800     E58
432  * 59   77525   800     E59
433  * 60   78325   800     E60
434  * 61   79125   800     E61
435  * 62   79925   800     E62
436  * 63   80725   800     E63
437  * 64   81525   800     E64
438  * 65   82325   800     E65
439  * 66   83125   800     E66
440  * 67   83925   800     E67
441  * 68   84725   800     E68
442  * 69   85525   800     E69
443  * 
444  * 70    4575   800     IA
445  * 71    5375   800     IB
446  * 72    6175   800     IC
447  * 
448  * 74    6925   700     S01
449  * 75    7625   700     S02
450  * 76    8325   700     S03
451  * 
452  * 80   10525   700     S1
453  * 81   11225   700     S2
454  * 82   11925   700     S3
455  * 83   12625   700     S4
456  * 84   13325   700     S5
457  * 85   14025   700     S6
458  * 86   14725   700     S7
459  * 87   15425   700     S8
460  * 88   16125   700     S9
461  * 89   16825   700     S10
462  * 90   23125   700     S11
463  * 91   23825   700     S12
464  * 92   24525   700     S13
465  * 93   25225   700     S14
466  * 94   25925   700     S15
467  * 95   26625   700     S16
468  * 96   27325   700     S17
469  * 97   28025   700     S18
470  * 98   28725   700     S19
471  * 99   29425   700     S20
472  *
473  *
474  * Channels S21 - S41 are taken from
475  * http://gemma.apple.com:80/dev/technotes/tn/tn1012.html
476  *
477  * 100  30325   800     S21
478  * 101  31125   800     S22
479  * 102  31925   800     S23
480  * 103  32725   800     S24
481  * 104  33525   800     S25
482  * 105  34325   800     S26         
483  * 106  35125   800     S27         
484  * 107  35925   800     S28         
485  * 108  36725   800     S29         
486  * 109  37525   800     S30         
487  * 110  38325   800     S31         
488  * 111  39125   800     S32         
489  * 112  39925   800     S33         
490  * 113  40725   800     S34         
491  * 114  41525   800     S35         
492  * 115  42325   800     S36         
493  * 116  43125   800     S37         
494  * 117  43925   800     S38         
495  * 118  44725   800     S39         
496  * 119  45525   800     S40         
497  * 120  46325   800     S41
498  * 
499  * 121   3890   000     IFFREQ
500  * 
501  */
502 static int weurope[] = {
503        121,     (int)( 38.90 * FREQFACTOR),     0, 
504        100,     (int)(303.25 * FREQFACTOR),     (int)(8.00 * FREQFACTOR), 
505         90,     (int)(231.25 * FREQFACTOR),     (int)(7.00 * FREQFACTOR),
506         80,     (int)(105.25 * FREQFACTOR),     (int)(7.00 * FREQFACTOR),  
507         74,     (int)( 69.25 * FREQFACTOR),     (int)(7.00 * FREQFACTOR),  
508         21,     (int)(471.25 * FREQFACTOR),     (int)(8.00 * FREQFACTOR),
509         17,     (int)(183.25 * FREQFACTOR),     (int)(9.00 * FREQFACTOR),
510         16,     (int)(175.25 * FREQFACTOR),     (int)(9.00 * FREQFACTOR),
511         15,     (int)(82.25 * FREQFACTOR),      (int)(8.50 * FREQFACTOR),
512         13,     (int)(53.75 * FREQFACTOR),      (int)(8.50 * FREQFACTOR),
513          5,     (int)(175.25 * FREQFACTOR),     (int)(7.00 * FREQFACTOR),
514          2,     (int)(48.25 * FREQFACTOR),      (int)(7.00 * FREQFACTOR),
515          0
516 };
517
518 /*
519  * Japanese Broadcast Channels:
520  *
521  *  1:  91.25MHz -  3: 103.25MHz
522  *  4: 171.25MHz -  7: 189.25MHz
523  *  8: 193.25MHz - 12: 217.25MHz  (VHF)
524  * 13: 471.25MHz - 62: 765.25MHz  (UHF)
525  *
526  * IF freq: 45.75 mHz
527  *  OR
528  * IF freq: 58.75 mHz
529  */
530 #define OFFSET  6.00
531 #define IF_FREQ 45.75
532 static int jpnbcst[] = {
533         62,     (int)(IF_FREQ * FREQFACTOR),    0,
534         13,     (int)(471.25 * FREQFACTOR),     (int)(OFFSET * FREQFACTOR),
535          8,     (int)(193.25 * FREQFACTOR),     (int)(OFFSET * FREQFACTOR),
536          4,     (int)(171.25 * FREQFACTOR),     (int)(OFFSET * FREQFACTOR),
537          1,     (int)( 91.25 * FREQFACTOR),     (int)(OFFSET * FREQFACTOR),
538          0
539 };
540 #undef IF_FREQ
541 #undef OFFSET
542
543 /*
544  * Japanese Cable Channels:
545  *
546  *  1:  91.25MHz -  3: 103.25MHz
547  *  4: 171.25MHz -  7: 189.25MHz
548  *  8: 193.25MHz - 12: 217.25MHz
549  * 13: 109.25MHz - 21: 157.25MHz
550  * 22: 165.25MHz
551  * 23: 223.25MHz - 63: 463.25MHz
552  *
553  * IF freq: 45.75 mHz
554  */
555 #define OFFSET  6.00
556 #define IF_FREQ 45.75
557 static int jpncable[] = {
558         63,     (int)(IF_FREQ * FREQFACTOR),    0,
559         23,     (int)(223.25 * FREQFACTOR),     (int)(OFFSET * FREQFACTOR),
560         22,     (int)(165.25 * FREQFACTOR),     (int)(OFFSET * FREQFACTOR),
561         13,     (int)(109.25 * FREQFACTOR),     (int)(OFFSET * FREQFACTOR),
562          8,     (int)(193.25 * FREQFACTOR),     (int)(OFFSET * FREQFACTOR),
563          4,     (int)(171.25 * FREQFACTOR),     (int)(OFFSET * FREQFACTOR),
564          1,     (int)( 91.25 * FREQFACTOR),     (int)(OFFSET * FREQFACTOR),
565          0
566 };
567 #undef IF_FREQ
568 #undef OFFSET
569
570 /*
571  * xUSSR Broadcast Channels:
572  *
573  *  1:  49.75MHz -  2:  59.25MHz
574  *  3:  77.25MHz -  5:  93.25MHz
575  *  6: 175.25MHz - 12: 223.25MHz
576  * 13-20 - not exist
577  * 21: 471.25MHz - 34: 575.25MHz
578  * 35: 583.25MHz - 69: 855.25MHz
579  *
580  * Cable channels
581  *
582  * 70: 111.25MHz - 77: 167.25MHz
583  * 78: 231.25MHz -107: 463.25MHz
584  *
585  * IF freq: 38.90 MHz
586  */
587 #define IF_FREQ 38.90
588 static int xussr[] = {
589       107,     (int)(IF_FREQ * FREQFACTOR),    0,
590        78,     (int)(231.25 * FREQFACTOR),     (int)(8.00 * FREQFACTOR), 
591        70,     (int)(111.25 * FREQFACTOR),     (int)(8.00 * FREQFACTOR),
592        35,     (int)(583.25 * FREQFACTOR),     (int)(8.00 * FREQFACTOR), 
593        21,     (int)(471.25 * FREQFACTOR),     (int)(8.00 * FREQFACTOR),
594         6,     (int)(175.25 * FREQFACTOR),     (int)(8.00 * FREQFACTOR),  
595         3,     (int)( 77.25 * FREQFACTOR),     (int)(8.00 * FREQFACTOR),  
596         1,     (int)( 49.75 * FREQFACTOR),     (int)(9.50 * FREQFACTOR),
597         0
598 };
599 #undef IF_FREQ
600
601 /*
602  * Australian broadcast channels
603  */
604 #define OFFSET  7.00
605 #define IF_FREQ 38.90 
606 static int australia[] = {
607        83,     (int)(IF_FREQ * FREQFACTOR),    0,
608        28,     (int)(527.25 * FREQFACTOR),     (int)(OFFSET * FREQFACTOR),
609        10,     (int)(209.25 * FREQFACTOR),     (int)(OFFSET * FREQFACTOR),
610         6,     (int)(175.25 * FREQFACTOR),     (int)(OFFSET * FREQFACTOR),
611         4,     (int)( 95.25 * FREQFACTOR),     (int)(OFFSET * FREQFACTOR),
612         3,     (int)( 86.25 * FREQFACTOR),     (int)(OFFSET * FREQFACTOR),
613         1,     (int)( 57.25 * FREQFACTOR),     (int)(OFFSET * FREQFACTOR),
614         0
615 };
616 #undef OFFSET
617 #undef IF_FREQ
618
619 /* 
620  * France broadcast channels
621  */
622 #define OFFSET 8.00
623 #define IF_FREQ 38.90
624 static int france[] = {
625         69,     (int)(IF_FREQ * FREQFACTOR),     0,
626         21,     (int)(471.25 * FREQFACTOR),     (int)(OFFSET * FREQFACTOR), /* 21 -> 69 */
627          5,     (int)(176.00 * FREQFACTOR),     (int)(OFFSET * FREQFACTOR), /* 5 -> 10 */
628          4,     (int)( 63.75 * FREQFACTOR),     (int)(OFFSET * FREQFACTOR), /* 4    */
629          3,     (int)( 60.50 * FREQFACTOR),     (int)(OFFSET * FREQFACTOR), /* 3    */
630          1,     (int)( 47.75 * FREQFACTOR),     (int)(OFFSET * FREQFACTOR), /* 1  2 */
631          0
632 }; 
633 #undef OFFSET
634 #undef IF_FREQ
635
636 static struct {
637         int     *ptr;
638         char    name[BT848_MAX_CHNLSET_NAME_LEN];
639 } freqTable[] = {
640         {NULL,          ""},
641         {nabcst,        "nabcst"},
642         {irccable,      "cableirc"},
643         {hrccable,      "cablehrc"},
644         {weurope,       "weurope"},
645         {jpnbcst,       "jpnbcst"},
646         {jpncable,      "jpncable"},
647         {xussr,         "xussr"},
648         {australia,     "australia"},
649         {france,        "france"},
650  
651 };
652
653 #define TBL_CHNL        freqTable[ bktr->tuner.chnlset ].ptr[ x ]
654 #define TBL_BASE_FREQ   freqTable[ bktr->tuner.chnlset ].ptr[ x + 1 ]
655 #define TBL_OFFSET      freqTable[ bktr->tuner.chnlset ].ptr[ x + 2 ]
656 static int
657 frequency_lookup( bktr_ptr_t bktr, int channel )
658 {
659         int     x;
660
661         /* check for "> MAX channel" */
662         x = 0;
663         if ( channel > TBL_CHNL )
664                 return( -1 );
665
666         /* search the table for data */
667         for ( x = 3; TBL_CHNL; x += 3 ) {
668                 if ( channel >= TBL_CHNL ) {
669                         return( TBL_BASE_FREQ +
670                                  ((channel - TBL_CHNL) * TBL_OFFSET) );
671                 }
672         }
673
674         /* not found, must be below the MIN channel */
675         return( -1 );
676 }
677 #undef TBL_OFFSET
678 #undef TBL_BASE_FREQ
679 #undef TBL_CHNL
680
681
682 #define TBL_IF  freqTable[ bktr->tuner.chnlset ].ptr[ 1 ]
683
684
685 /* Initialise the tuner structures in the bktr_softc */
686 /* This is needed as the tuner details are no longer globally declared */
687
688 void    select_tuner( bktr_ptr_t bktr, int tuner_type ) {
689         if (tuner_type < Bt848_MAX_TUNER) {
690                 bktr->card.tuner = &tuners[ tuner_type ];
691         } else {
692                 bktr->card.tuner = NULL;
693         }
694 }
695
696 /*
697  * Tuner Notes:
698  * Programming the tuner properly is quite complicated.
699  * Here are some notes, based on a FM1246 data sheet for a PAL-I tuner.
700  * The tuner (front end) covers 45.75 Mhz - 855.25 Mhz and an FM band of
701  * 87.5 Mhz to 108.0 Mhz.
702  *
703  * RF and IF.  RF = radio frequencies, it is the transmitted signal.
704  *             IF is the Intermediate Frequency (the offset from the base
705  *             signal where the video, color,  audio and NICAM signals are.
706  *
707  * Eg, Picture at 38.9 Mhz, Colour at 34.47 MHz, sound at 32.9 MHz
708  * NICAM at 32.348 Mhz.
709  * Strangely enough, there is an IF (intermediate frequency) for
710  * FM Radio which is 10.7 Mhz.
711  *
712  * The tuner also works in Bands. Philips bands are
713  * FM radio band 87.50 to 108.00 MHz
714  * Low band 45.75 to 170.00 MHz
715  * Mid band 170.00 to 450.00 MHz
716  * High band 450.00 to 855.25 MHz
717  *
718  *
719  * Now we need to set the PLL on the tuner to the required freuqncy.
720  * It has a programmable divisor.
721  * For TV we want
722  *  N = 16 (freq RF(pc) + freq IF(pc))  pc is picture carrier and RF and IF
723  *  are in MHz.
724
725  * For RADIO we want a different equation.
726  *  freq IF is 10.70 MHz (so the data sheet tells me)
727  * N = (freq RF + freq IF) / step size
728  * The step size must be set to 50 khz (so the data sheet tells me)
729  * (note this is 50 kHz, the other things are in MHz)
730  * so we end up with N = 20x(freq RF + 10.7)
731  *
732  */
733
734 #define LOW_BAND 0
735 #define MID_BAND 1
736 #define HIGH_BAND 2
737 #define FM_RADIO_BAND 3
738
739
740 /* Check if these are correct for other than Philips PAL */
741 #define STATUSBIT_COLD   0x80
742 #define STATUSBIT_LOCK   0x40
743 #define STATUSBIT_TV     0x20
744 #define STATUSBIT_STEREO 0x10 /* valid if FM (aka not TV) */
745 #define STATUSBIT_ADC    0x07
746
747 /*
748  * set the frequency of the tuner
749  * If 'type' is TV_FREQUENCY, the frequency is freq MHz*16
750  * If 'type' is FM_RADIO_FREQUENCY, the frequency is freq MHz * 100 
751  * (note *16 gives is 4 bits of fraction, eg steps of nnn.0625)
752  *
753  */
754 int
755 tv_freq( bktr_ptr_t bktr, int frequency, int type )
756 {
757         const struct TUNER*     tuner;
758         u_char                  addr;
759         u_char                  control;
760         u_char                  band;
761         int                     N;
762         int                     band_select = 0;
763 #if defined( TEST_TUNER_AFC )
764         int                     oldFrequency, afcDelta;
765 #endif
766
767         tuner = bktr->card.tuner;
768         if ( tuner == NULL )
769                 return( -1 );
770
771         if (type == TV_FREQUENCY) {
772                 /*
773                  * select the band based on frequency
774                  * XXX FIXME: get the cross-over points from the tuner struct
775                  */
776                 if ( frequency < (160 * FREQFACTOR  ) )
777                     band_select = LOW_BAND;
778                 else if ( frequency < (454 * FREQFACTOR ) )
779                     band_select = MID_BAND;
780                 else
781                     band_select = HIGH_BAND;
782
783 #if defined( TEST_TUNER_AFC )
784                 if ( bktr->tuner.afc )
785                         frequency -= 4;
786 #endif
787                 /*
788                  * N = 16 * { fRF(pc) + fIF(pc) }
789                  * or N = 16* fRF(pc) + 16*fIF(pc) }
790                  * where:
791                  *  pc is picture carrier, fRF & fIF are in MHz
792                  *
793                  * fortunatly, frequency is passed in as MHz * 16
794                  * and the TBL_IF frequency is also stored in MHz * 16
795                  */
796                 N = frequency + TBL_IF;
797
798                 /* set the address of the PLL */
799                 addr    = bktr->card.tuner_pllAddr;
800                 control = tuner->pllControl[ band_select ];
801                 band    = tuner->bandAddrs[ band_select ];
802
803                 if(!(band && control))          /* Don't try to set un- */
804                   return(-1);                   /* supported modes.     */
805
806                 if ( frequency > bktr->tuner.frequency ) {
807                         i2cWrite( bktr, addr, (N>>8) & 0x7f, N & 0xff );
808                         i2cWrite( bktr, addr, control, band );
809                 }
810                 else {
811                         i2cWrite( bktr, addr, control, band );
812                         i2cWrite( bktr, addr, (N>>8) & 0x7f, N & 0xff );
813                 }
814
815 #if defined( TUNER_AFC )
816                 if ( bktr->tuner.afc == TRUE ) {
817 #if defined( TEST_TUNER_AFC )
818                         oldFrequency = frequency;
819 #endif
820                         if ( (N = do_afc( bktr, addr, N )) < 0 ) {
821                             /* AFC failed, restore requested frequency */
822                             N = frequency + TBL_IF;
823 #if defined( TEST_TUNER_AFC )
824                             printf("%s: do_afc: failed to lock\n",
825                                    bktr_name(bktr));
826 #endif
827                             i2cWrite( bktr, addr, (N>>8) & 0x7f, N & 0xff );
828                         }
829                         else
830                             frequency = N - TBL_IF;
831 #if defined( TEST_TUNER_AFC )
832  printf("%s: do_afc: returned freq %d (%d %% %d)\n", bktr_name(bktr), frequency, frequency / 16, frequency % 16);
833                             afcDelta = frequency - oldFrequency;
834  printf("%s: changed by: %d clicks (%d mod %d)\n", bktr_name(bktr), afcDelta, afcDelta / 16, afcDelta % 16);
835 #endif
836                         }
837 #endif /* TUNER_AFC */
838
839                 bktr->tuner.frequency = frequency;
840         }
841
842         if ( type == FM_RADIO_FREQUENCY ) {
843                 band_select = FM_RADIO_BAND;
844
845                 /*
846                  * N = { fRF(pc) + fIF(pc) }/step_size
847                  * The step size is 50kHz for FM radio.
848                  * (eg after 102.35MHz comes 102.40 MHz)
849                  * fIF is 10.7 MHz (as detailed in the specs)
850                  *
851                  * frequency is passed in as MHz * 100
852                  *
853                  * So, we have N = (frequency/100 + 10.70)  /(50/1000)
854                  */
855                 N = (frequency + 1070)/5;
856
857                 /* set the address of the PLL */
858                 addr    = bktr->card.tuner_pllAddr;
859                 control = tuner->pllControl[ band_select ];
860                 band    = tuner->bandAddrs[ band_select ];
861
862                 if(!(band && control))          /* Don't try to set un- */
863                   return(-1);                   /* supported modes.     */
864           
865                 band |= bktr->tuner.radio_mode; /* tuner.radio_mode is set in
866                                                  * the ioctls RADIO_SETMODE
867                                                  * and RADIO_GETMODE */
868
869                 i2cWrite( bktr, addr, control, band );
870                 i2cWrite( bktr, addr, (N>>8) & 0x7f, N & 0xff );
871
872                 bktr->tuner.frequency = (N * 5) - 1070;
873
874
875         }
876  
877
878         return( 0 );
879 }
880
881
882
883 #if defined( TUNER_AFC )
884 /*
885  * 
886  */
887 int
888 do_afc( bktr_ptr_t bktr, int addr, int frequency )
889 {
890         int step;
891         int status;
892         int origFrequency;
893
894         origFrequency = frequency;
895
896         /* wait for first setting to take effect */
897         tsleep( BKTR_SLEEP, 0, "tuning", hz/8 );
898
899         if ( (status = i2cRead( bktr, addr + 1 )) < 0 )
900                 return( -1 );
901
902 #if defined( TEST_TUNER_AFC )
903  printf( "%s: Original freq: %d, status: 0x%02x\n", bktr_name(bktr), frequency, status );
904 #endif
905         for ( step = 0; step < AFC_MAX_STEP; ++step ) {
906                 if ( (status = i2cRead( bktr, addr + 1 )) < 0 )
907                         goto fubar;
908                 if ( !(status & 0x40) ) {
909 #if defined( TEST_TUNER_AFC )
910  printf( "%s: no lock!\n", bktr_name(bktr) );
911 #endif
912                         goto fubar;
913                 }
914
915                 switch( status & AFC_BITS ) {
916                 case AFC_FREQ_CENTERED:
917 #if defined( TEST_TUNER_AFC )
918  printf( "%s: Centered, freq: %d, status: 0x%02x\n", bktr_name(bktr), frequency, status );
919 #endif
920                         return( frequency );
921
922                 case AFC_FREQ_MINUS_125:
923                 case AFC_FREQ_MINUS_62:
924 #if defined( TEST_TUNER_AFC )
925  printf( "%s: Low, freq: %d, status: 0x%02x\n", bktr_name(bktr), frequency, status );
926 #endif
927                         --frequency;
928                         break;
929
930                 case AFC_FREQ_PLUS_62:
931                 case AFC_FREQ_PLUS_125:
932 #if defined( TEST_TUNER_AFC )
933  printf( "%s: Hi, freq: %d, status: 0x%02x\n", bktr_name(bktr), frequency, status );
934 #endif
935                         ++frequency;
936                         break;
937                 }
938
939                 i2cWrite( bktr, addr,
940                           (frequency>>8) & 0x7f, frequency & 0xff );
941                 DELAY( AFC_DELAY );
942         }
943
944  fubar:
945         i2cWrite( bktr, addr,
946                   (origFrequency>>8) & 0x7f, origFrequency & 0xff );
947
948         return( -1 );
949 }
950 #endif /* TUNER_AFC */
951 #undef TBL_IF
952
953
954 /*
955  * Get the Tuner status and signal strength
956  */
957 int     get_tuner_status( bktr_ptr_t bktr ) {
958         return i2cRead( bktr, bktr->card.tuner_pllAddr + 1 );
959 }
960
961 /*
962  * set the channel of the tuner
963  */
964 int
965 tv_channel( bktr_ptr_t bktr, int channel )
966 {
967         int frequency;
968
969         /* calculate the frequency according to tuner type */
970         if ( (frequency = frequency_lookup( bktr, channel )) < 0 )
971                 return( -1 );
972
973         /* set the new frequency */
974         if ( tv_freq( bktr, frequency, TV_FREQUENCY ) < 0 )
975                 return( -1 );
976
977         /* OK to update records */
978         return( (bktr->tuner.channel = channel) );
979 }
980
981 /*
982  * get channelset name
983  */
984 int
985 tuner_getchnlset(struct bktr_chnlset *chnlset)
986 {
987        if (( chnlset->index < CHNLSET_MIN ) ||
988                ( chnlset->index > CHNLSET_MAX ))
989                        return( EINVAL );
990
991        memcpy(&chnlset->name, &freqTable[chnlset->index].name,
992                BT848_MAX_CHNLSET_NAME_LEN);
993
994        chnlset->max_channel=freqTable[chnlset->index].ptr[0];
995        return( 0 );
996 }