summaryrefslogtreecommitdiff
path: root/hw/xfree86/i2c/fi1236.c
blob: 8337c900e038f9e1df8c9c5169c71dd8837e1aec (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605

#ifdef HAVE_XORG_CONFIG_H
#include <xorg-config.h>
#endif

#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>

#include "xf86.h"
#include "xf86i2c.h"
#include "fi1236.h"
#include "tda9885.h"
#include "i2c_def.h"

#define NUM_TUNERS    8

const FI1236_parameters tuner_parms[NUM_TUNERS] =
{
    /* 0 - FI1236 */
    { 733 ,884 ,12820 ,2516 ,7220 ,0xA2 ,0x94, 0x34, 0x8e },
    /* !!!based on documentation - it should be:
    {733 ,16*55.25 ,16*801.25 ,16*160 ,16*454 ,0xA0 ,0x90, 0x30, 0x8e},*/
    
    /* 1 - FI1216 */
    { 623 ,16*48.75 ,16*855.25 ,16*170 ,16*450 ,0xA0 ,0x90, 0x30, 0x8e },
    /* 2 - TEMIC FN5AL */
    { 623 ,16*45.75 ,16*855.25 ,16*169 ,16*454 ,0xA0 ,0x90, 0x30, 0x8e },
    /* 3 - MT2032.. */
    { 733 ,768 ,13760 , 0 , 0 , 0 , 0,  0, 0 },
    /* 4 - FI1246 */
    { 623 ,16*45.75 ,16*855.25 ,16*170 ,16*450 ,0xA0 ,0x90, 0x30, 0x8e },
    /* 5 - FI1256 */
    { 623 ,16*49.75 ,16*863.25 ,16*170 ,16*450 ,0xA0 ,0x90, 0x30, 0x8e },
    /* 6 - FI1236W */
    /*{ 733 ,884 ,12820 ,2516 ,7220 ,0x1 ,0x2, 0x4, 0x8e },*/
    { 732, 16*55.25, 16*801.25, 16*160, 16*442, 0x1, 0x2, 0x4, 0x8e },
	 /* 7 - FM1216ME */
    { 623 ,16*48.25 ,16*863.25 ,16*158.00 ,16*442.00 ,0x1 ,0x2, 0x4, 0x8e }
};


FI1236Ptr Detect_FI1236(I2CBusPtr b, I2CSlaveAddr addr)
{
   FI1236Ptr f;
   I2CByte a;

   f = calloc(1,sizeof(FI1236Rec));
   if(f == NULL) return NULL;
   f->d.DevName = strdup("FI12xx Tuner");
   f->d.SlaveAddr = addr;
   f->d.pI2CBus = b;
   f->d.NextDev = NULL;
   f->d.StartTimeout = b->StartTimeout;
   f->d.BitTimeout = b->BitTimeout;
   f->d.AcknTimeout = b->AcknTimeout;
   f->d.ByteTimeout = b->ByteTimeout;
   f->type=TUNER_TYPE_FI1236;
   f->afc_timer_installed=FALSE;
   f->last_afc_hint=TUNER_OFF;
   f->video_if=45.7812;
  
   if(!I2C_WriteRead(&(f->d), NULL, 0, &a, 1))
   {
   	free(f);
	return NULL;
    }
    FI1236_set_tuner_type(f, TUNER_TYPE_FI1236);
    if(!I2CDevInit(&(f->d)))
    {
       free(f);
       return NULL;
    }
    return f;
}

static void MT2032_dump_parameters(FI1236Ptr f, MT2032_parameters *m)
{
xf86DrvMsg(f->d.pI2CBus->scrnIndex, X_INFO, "MT2032: input f_rf=%g f_if1=%g f_if2=%g f_ref=%g f_ifbw=%g f_step=%g\n", 
	m->f_rf, m->f_if1, m->f_if2, m->f_ref, m->f_ifbw, m->f_step);

xf86DrvMsg(f->d.pI2CBus->scrnIndex, X_INFO, "MT2032: computed f_lo1=%g f_lo2=%g LO1I=%d LO2I=%d SEL=%d STEP=%d NUM=%d\n", 
	m->f_lo1, m->f_lo2, m->LO1I, m->LO2I, m->SEL, m->STEP, m->NUM);
}


static void MT2032_getid(FI1236Ptr f)
{
CARD8 out[4];
CARD8 in;

in=0x11;
I2C_WriteRead(&(f->d), (I2CByte *)&in, 1, out, 4);
xf86DrvMsg(f->d.pI2CBus->scrnIndex, X_INFO, "MT2032: Company code 0x%02x%02x, part code 0x%02x, revision code 0x%02x\n",
	out[0], out[1], out[2], out[3]);

}

/* might be buggy */
#if 0
static void MT2032_shutdown(FI1236Ptr f)
{
CARD8 data[10];

data[0]=0x00; /* start with register 0x00 */
data[1]=0x1A; 
data[2]=0x44;
data[3]=0x20;

I2C_WriteRead(&(f->d), (I2CByte *)data, 4, NULL, 0);

data[0]=0x05; /* now start with register 0x05 */
data[1]=0xD7;
data[2]=0x14;
data[3]=0x05;
I2C_WriteRead(&(f->d), (I2CByte *)data, 4, NULL, 0);

data[0]=0x0B; /* now start with register 0x05 */
data[1]=0x8F;
data[2]=0x07;
data[3]=0x43;
I2C_WriteRead(&(f->d), (I2CByte *)data, 4, NULL, 0);

usleep(15000);
}
#endif

static void MT2032_dump_status(FI1236Ptr f);

static void MT2032_init(FI1236Ptr f)
{
CARD8 data[10];
CARD8 value;
CARD8 xogc = 0x00;

MT2032_getid(f);

data[0]=0x02; /* start with register 0x02 */
data[1]=0xFF; 
data[2]=0x0F;
data[3]=0x1F;

I2C_WriteRead(&(f->d), (I2CByte *)data, 4, NULL, 0);

data[0]=0x06; /* now start with register 0x06 */
data[1]=0xE4;
data[2]=0x8F;
data[3]=0xC3;
data[4]=0x4E;
data[5]=0xEC;
I2C_WriteRead(&(f->d), (I2CByte *)data, 6, NULL, 0);

data[0]=0x0d; /* now start with register 0x0d */
data[1]=0x32;
I2C_WriteRead(&(f->d), (I2CByte *)data, 2, NULL, 0);

while(1) {
	usleep(15000); /* wait 15 milliseconds */

	data[0]=0x0e; /* register number 7, status */
	value=0xFF;
	if(!I2C_WriteRead(&(f->d), (I2CByte *)data, 1, &value, 1))
		xf86DrvMsg(f->d.pI2CBus->scrnIndex, X_INFO, "MT2032: failed to read XOK\n"); 
	xf86DrvMsg(f->d.pI2CBus->scrnIndex, X_INFO, "MT2032: XOK=%d\n", value & 0x01); 
	if(value & 1) break;
	
	data[0]=0x07;
	if(!I2C_WriteRead(&(f->d), (I2CByte *)data, 1, &value, 1))
		xf86DrvMsg(f->d.pI2CBus->scrnIndex, X_INFO, "MT2032: failed to read XOGC\n"); 
	 
	xogc=value & 0x7;
	if(xogc==4){
		break; /* XOGC has reached 4.. stop */	
		}
	xogc--;
	xf86DrvMsg(f->d.pI2CBus->scrnIndex, X_INFO, "MT2032: try XOGC=%d\n", xogc); 
	usleep(15000);
	data[0]=0x07; /* register number 7, control byte 2 */
	data[1]=0x08 | xogc;
	I2C_WriteRead(&(f->d), (I2CByte *)data, 2, NULL, 0);	
	}
f->xogc=xogc;
/* wait before continuing */
usleep(15000); /* wait 50 milliseconds */
MT2032_dump_status(f);
}

static int MT2032_no_spur_in_band(MT2032_parameters *m)
{
int n_max, n1, n2;
double f_test;
n_max=5;
n1=1;
while(1){
	n2=-n1;
	f_test=n1*(m->f_lo1-m->f_lo2);
	while(1){
		n2--;
		f_test=f_test-m->f_lo2;
		xf86DrvMsg(0, X_INFO, "testing f_test=%g n1=%d n2=%d f_lo1=%g f_lo2=%g f_if2=%g\n", f_test, n1, n2, m->f_lo1, m->f_lo2, m->f_if2);  
		xf86DrvMsg(0, X_INFO, "d_f=%g f_ifbw=%g\n",fabs(fabs(f_test)-m->f_if2), m->f_ifbw);  
		if((fabs(fabs(f_test)-m->f_if2)*2.0)<=m->f_ifbw)return 0;
		if(n2<=-n_max)break;
  		/* this line in the manual is bogus. I say it is faster
		and more correct to go over all harmonics.. */
		#if 0
		if(f_test<(m->f_lo2-m->f_if2-m->f_ifbw))break; 
		#endif
		}
	n1++;
	if(n1>=n_max)return 1;
	}

}

static void MT2032_calculate_register_settings(MT2032_parameters *m, double f_rf, double f_if1, double f_if2, double f_ref, double f_ifbw, double f_step)
{
int n;
m->f_rf=f_rf;
m->f_if1=f_if1;
m->f_if2=f_if2;
m->f_ref=f_ref;
m->f_ifbw=f_ifbw;
m->f_step=f_step;

m->f_lo1=f_rf+f_if1;
m->LO1I=lrint(m->f_lo1/f_ref);
m->f_lo1=f_ref*m->LO1I;

m->f_lo2=m->f_lo1-f_rf-f_if2;

/* check for spurs */
n=1;
while(n<3){
	if(MT2032_no_spur_in_band(m))break;
	if(m->f_lo1<(f_rf+f_if1)){
		m->LO1I+=n;
		} else {
		m->LO1I-=n;
		}
	m->f_lo1=m->LO1I*f_ref;
	m->f_lo2=m->f_lo1-f_rf-f_if2;
	n++;
	}
/* xf86DrvMsg(0, X_INFO, "MT2032: n=%d\n", n); */
/* select VCO */

/* m->f_lo1>1100.0 */
if(m->f_lo1<1370.0)m->SEL=4;
	else
if(m->f_lo1<1530.0)m->SEL=3;
	else
if(m->f_lo1<1720.0)m->SEL=2;
	else
if(m->f_lo1<1890.0)m->SEL=1;
	else  /* m->f_lo1 < 1958.0 */
	m->SEL=0;

/* calculate the rest of the registers */
m->LO2I=floor(m->f_lo2/f_ref);
m->STEP=floor(3780.0*f_step/f_ref);
m->NUM=floor(3780.0*(m->f_lo2/f_ref-m->LO2I));
m->NUM=m->STEP*lrint((1.0*m->NUM)/(1.0*m->STEP));
}

static int MT2032_wait_for_lock(FI1236Ptr f)
{
int n;
CARD8 data[10];
CARD8 value;

n=12;
while(1){
	data[0]=0x0e; /* register number 7, status */
	I2C_WriteRead(&(f->d), (I2CByte *)data, 1, &value, 1);
/*	xf86DrvMsg(f->d.pI2CBus->scrnIndex, X_INFO, "MT2032: LO1LK=%d LO2LK=%d\n", (value & 0x04)>>2, (value & 0x02)>>1); */
	if((value & 6)==6) break;
	usleep(1500);
	n--;
	if(n<0)break;
	}
if(n<0){
	xf86DrvMsg(f->d.pI2CBus->scrnIndex, X_INFO, "MT2032: failed to set frequency\n");
	return 0;
	}
return 1;
}

static void MT2032_implement_settings(FI1236Ptr f, MT2032_parameters *m)
{
CARD8 data[10];
CARD8 value;

data[0]=0x00;     /* start with register 0x00 */
data[1]=(m->LO1I>>3)-1;
data[2]=(m->SEL<<4)|(m->LO1I & 0x7);
data[3]=0x86;
I2C_WriteRead(&(f->d), (I2CByte *)data, 4, NULL, 0);

data[0]=0x05;     /* start with register 0x05 */
data[1]=((m->LO2I & 0x7)<<5)|((m->LO2I>>3)-1);
if(m->f_rf<400.0)data[2]=0xe4;
	else data[2]=0xf4;
I2C_WriteRead(&(f->d), (I2CByte *)data, 3, NULL, 0);

data[0]=0x07; /* register number 7, control byte 2 */
I2C_WriteRead(&(f->d), (I2CByte *)data, 1, &value, 1);
xf86DrvMsg(f->d.pI2CBus->scrnIndex, X_INFO, "MT2032: using XOGC=%d\n", (value & 0x07));
data[1]=8 | (value & 0x7);
I2C_WriteRead(&(f->d), (I2CByte *)data, 2, NULL, 0);	

data[0]=0x0b;   /* start with register 0x0b */
data[1]=m->NUM & 0xff;
data[2]=(1<<7)|((m->NUM >> 8) & 0x0f);
I2C_WriteRead(&(f->d), (I2CByte *)data, 3, NULL, 0);

MT2032_wait_for_lock(f);
}

static void MT2032_optimize_VCO(FI1236Ptr f, MT2032_parameters *m)
{
CARD8 data[10];
CARD8 value;
CARD8 TAD1;

data[0]=0x0f; /* register number 7, status */
I2C_WriteRead(&(f->d), (I2CByte *)data, 1, &value, 1);
TAD1=value & 0x07;
xf86DrvMsg(f->d.pI2CBus->scrnIndex, X_INFO, "MT2032: TAD1=%d SEL=%d\n", TAD1, m->SEL);
if(TAD1 < 2)return;
if(TAD1==2){
	if(m->SEL==0)return;
	m->SEL--;
	} else {
	if(m->SEL>=4)return;
	m->SEL++;
	}
data[0]=0x01;  /* start with register 1 */
data[1]=(m->SEL<<4)|(m->LO1I & 0x7);
I2C_WriteRead(&(f->d), (I2CByte *)data, 2, NULL, 0);

}

static int FI1236_get_afc_hint(FI1236Ptr f)
{
	CARD8 out;
	CARD8 AFC;

	if ((f->type == TUNER_TYPE_FM1216ME) || (f->type == TUNER_TYPE_FI1236W))
	{
		TDA9885Ptr t = (TDA9885Ptr)f->afc_source;
		if (t == NULL)
			return TUNER_OFF; 
		
		tda9885_getstatus(t);
		tda9885_dumpstatus(t);
		AFC = t->afc_status & 0x0f;

		xf86DrvMsg(f->d.pI2CBus->scrnIndex, X_INFO, "AFC: FI1236_get_afc_hint: %i\n", AFC);
		if (AFC == 0) return TUNER_TUNED;
		else if (AFC <= 0x07)return TUNER_JUST_BELOW;
		else if (AFC < 0x0f )return TUNER_JUST_ABOVE;
		else if (AFC == 0x0f)return TUNER_TUNED;
	}
	else
	{
		I2C_WriteRead(&(f->d), NULL, 0, &out, 1);
		AFC=out & 0x7;
		xf86DrvMsg(f->d.pI2CBus->scrnIndex, X_INFO, "AFC: FI1236_get_afc_hint: %i\n", AFC);
		if(AFC==2)return TUNER_TUNED;
		if(AFC==3)return TUNER_JUST_BELOW;
		if(AFC==1)return TUNER_JUST_ABOVE;
		return TUNER_OFF;
	}
	return TUNER_OFF;
}

static int MT2032_get_afc_hint(FI1236Ptr f)
{
CARD8 in;
CARD8 out[2];
CARD8 AFC;
in=0x0e;
I2C_WriteRead(&(f->d), (I2CByte *)&in, 1, out, 2);
AFC=(out[0]>>4) & 0x7;
#if 0
xf86DrvMsg(f->d.pI2CBus->scrnIndex, X_INFO, "AFC=%d TAD1=%d TAD2=%d\n", AFC, out[1] & 0x7, (out[1]>>4)& 0x07);
#endif
if(AFC==2)return TUNER_TUNED;
if(AFC==3)return TUNER_JUST_BELOW;
if(AFC==1)return TUNER_JUST_ABOVE;
return TUNER_OFF;
}

/* this function is for external use only */
int TUNER_get_afc_hint(FI1236Ptr f)
{
if(f->afc_timer_installed)return TUNER_STILL_TUNING;
return f->last_afc_hint;
}

static void MT2032_dump_status(FI1236Ptr f)
{
CARD8 in;
CARD8 out[2];
CARD8 AFC;
CARD8 LDONrb;
CARD8 LO1LK, LO2LK, XOK;
CARD8 TAD2, TAD1;

in=0x0e;
I2C_WriteRead(&(f->d), (I2CByte *)&in, 1, out, 2);
XOK=out[0] & 1;
LO1LK=(out[0]>>2) &1;
LO2LK=(out[0]>>1) &1;
LDONrb=(out[0]>>3) &1;

AFC=(out[0]>>4) & 0x7;

TAD1=(out[1] & 0x7);
TAD2=(out[1]>>4) & 0x7;

xf86DrvMsg(f->d.pI2CBus->scrnIndex, X_INFO, "MT2032: status: XOK=%d LO1LK=%d LO2LK=%d LDONrb=%d AFC=%d TAD1=%d TAD2=%d\n", 
	XOK, LO1LK, LO2LK, LDONrb, AFC, TAD1, TAD2);
xf86DrvMsg(f->d.pI2CBus->scrnIndex, X_INFO, "MT2032: status: OSCILLATOR:%s PLL1:%s PLL2:%s\n", 
	XOK ? "ok":"off", LO1LK ? "locked" : "off" , LO2LK ? "locked" : "off");

}

static void MT2032_tune(FI1236Ptr f, double freq, double step)
{
MT2032_parameters m;
CARD8 data[10];
int i;
/* NTSC IF is 44mhz.. but 733/16=45.8125 and all TDAXXXX docs mention
     45.75, 39, 58.75 and 30. */
#if 0
MT2032_calculate_register_settings(&m, freq, 1090.0, 45.125, 5.25, 6.0, step);
MT2032_calculate_register_settings(&m, freq, 1090.0, 45.74, 5.25, 6.0, step);
#endif
MT2032_calculate_register_settings(&m, freq, 1090.0, f->video_if, 5.25, 3.0, step);
MT2032_dump_parameters(f, &m);
MT2032_implement_settings(f, &m);
/* MT2032_dump_parameters(f, &m); */
for(i=0;i<3;i++){
	MT2032_optimize_VCO(f, &m);
	if(MT2032_wait_for_lock(f)){
		data[0]=0x02;  /* LO Gain control register 0x02 */
		data[1]=0x20;
		I2C_WriteRead(&(f->d), (I2CByte *)data, 2, NULL, 0);
		return;
		}
	data[0]=0x07;
	data[1]=0x88|f->xogc;
	I2C_WriteRead(&(f->d), (I2CByte *)data, 2, NULL, 0);
	usleep(15000);
	data[1]=0x08|f->xogc;
	I2C_WriteRead(&(f->d), (I2CByte *)data, 2, NULL, 0);	
	}
xf86DrvMsg(f->d.pI2CBus->scrnIndex, X_INFO, "MT2032: failed to set frequency\n");
}

void FI1236_set_tuner_type(FI1236Ptr f, int type)
{
f->type=type;
if(type>=NUM_TUNERS)type = NUM_TUNERS-1;
if(type<0)type = 0;
memcpy(&(f->parm), &(tuner_parms[type]), sizeof(FI1236_parameters));
f->original_frequency=f->parm.min_freq;
f->afc_delta=0;
if(type==TUNER_TYPE_MT2032){
	MT2032_init(f);
	return;
	}
}


static CARD32 AFC_TimerCallback(OsTimerPtr timer, CARD32 time, pointer data){
FI1236Ptr f=(FI1236Ptr)data;
if(FI1236_AFC(f))return 150;
	else {
	f->afc_timer_installed=FALSE;
	f->afc_count=0;
	return 0;
	}
}

void FI1236_tune(FI1236Ptr f, CARD32 frequency)
{
    CARD16 divider;
	 CARD8 data;

    if(frequency < f->parm.min_freq) frequency = f->parm.min_freq;
    if(frequency > f->parm.max_freq) frequency = f->parm.max_freq;

    divider = (f->parm.fcar+(CARD16)frequency) & 0x7fff;
    f->tuner_data.div1 = (CARD8)((divider>>8)&0x7f);
    f->tuner_data.div2 = (CARD8)(divider & 0xff);
    f->tuner_data.control = f->parm.control; 

    if(frequency < f->parm.threshold1)
    {
        f->tuner_data.band = f->parm.band_low;
    } 
    else if (frequency < f->parm.threshold2)
    {
        f->tuner_data.band = f->parm.band_mid;
    }
    else
    {
        f->tuner_data.band = f->parm.band_high;
    }

	 xf86DrvMsg(f->d.pI2CBus->scrnIndex, X_INFO, "Setting tuner band to %d\n", f->tuner_data.band);

    xf86DrvMsg(f->d.pI2CBus->scrnIndex, X_INFO, "Setting tuner frequency to %d\n", (int)frequency);

	 if ((f->type == TUNER_TYPE_FM1216ME) || (f->type == TUNER_TYPE_FI1236W))
	 {
				f->tuner_data.aux = 0x20;
				I2C_WriteRead(&(f->d), (I2CByte *)&(f->tuner_data), 5, NULL, 0);
				I2C_WriteRead(&(f->d), NULL, 0, &data, 1);
				xf86DrvMsg(f->d.pI2CBus->scrnIndex, X_INFO, "Tuner status %x\n", data);

	 }
	 else
				I2C_WriteRead(&(f->d), (I2CByte *)&(f->tuner_data), 4, NULL, 0);
}

void TUNER_set_frequency(FI1236Ptr f, CARD32 frequency)
{
    if(frequency < f->parm.min_freq) frequency = f->parm.min_freq;
    if(frequency > f->parm.max_freq) frequency = f->parm.max_freq;

    f->afc_delta=0;
    f->original_frequency=frequency;

    if(f->type==TUNER_TYPE_MT2032)
        {
    	MT2032_tune(f, (1.0*frequency)/16.0, 0.0625);
	} else 
	{
	FI1236_tune(f, frequency);
	}
    
    if(!f->afc_timer_installed)
        {
     	f->afc_timer_installed=TRUE;
/*     	RegisterBlockAndWakeupHandlers(FI1236_BlockHandler, AFCWakeup, f); */
	TimerSet(NULL, 0, 300, AFC_TimerCallback, f);
	}
	
}


int FI1236_AFC(FI1236Ptr f)
{
    #if 0
    xf86DrvMsg(f->d.pI2CBus->scrnIndex, X_INFO, "AFC: f=%p f->count=%d f->original_frequency=%d f->afc_delta=%d\n", f, f->afc_count, f->original_frequency, f->afc_delta);
    #endif
    f->afc_count++;
    if(f->type==TUNER_TYPE_MT2032)
        {
    	f->last_afc_hint=MT2032_get_afc_hint(f);
        xf86DrvMsg(f->d.pI2CBus->scrnIndex, X_INFO, "AFC: afc_hint=%d\n", f->last_afc_hint);
	if(f->last_afc_hint==TUNER_TUNED)return 0;
	if(f->afc_count>3)f->last_afc_hint=TUNER_OFF;
	if(f->last_afc_hint==TUNER_OFF)
	        {
		f->afc_delta=0;
		} else
		f->afc_delta+=f->last_afc_hint;
        xf86DrvMsg(f->d.pI2CBus->scrnIndex, X_INFO, "AFC: Setting tuner frequency to %g\n", (0.5*(2*f->original_frequency+f->afc_delta))/16.0);
    	MT2032_tune(f, (1.0*f->original_frequency+0.5*f->afc_delta)/16.0, 0.03125);
	if(f->last_afc_hint==TUNER_OFF)return 0;
	return 1; /* call me again */
	} else 
	{
    	f->last_afc_hint=FI1236_get_afc_hint(f);
	if(f->last_afc_hint==TUNER_TUNED)
	{
			  xf86DrvMsg(f->d.pI2CBus->scrnIndex, X_INFO, "AFC: TUNER_TUNNED\n");
			  return 0;
	}
	if(f->afc_count>3)f->last_afc_hint=TUNER_OFF;
	if(f->last_afc_hint==TUNER_OFF)
	        {
		f->afc_delta=0;
		} else
		f->afc_delta+=f->last_afc_hint;
        xf86DrvMsg(f->d.pI2CBus->scrnIndex, X_INFO, "AFC: Setting tuner frequency to %g\n", (0.5*(2*f->original_frequency+f->afc_delta))/16.0);
	FI1236_tune(f, f->original_frequency+f->afc_delta);
	if(f->last_afc_hint==TUNER_OFF)return 0;
	return 1; /* call me again */
	}
    return 0; /* done */
}

void fi1236_dump_status(FI1236Ptr f)
{
if(f->type==TUNER_TYPE_MT2032){
	MT2032_dump_status(f);
	}
}