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