ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / drivers / media / dvb / frontends / stv0299.c
1 /*
2     Universal driver for STV0299/TDA5059/SL1935 based
3     DVB QPSK frontends
4
5     Alps BSRU6, LG TDQB-S00x
6
7     Copyright (C) 2001-2002 Convergence Integrated Media GmbH
8         <ralph@convergence.de>,
9         <holger@convergence.de>,
10         <js@convergence.de>
11     
12
13     Philips SU1278/SH
14
15     Copyright (C) 2002 by Peter Schildmann <peter.schildmann@web.de>
16
17
18     LG TDQF-S001F
19
20     Copyright (C) 2002 Felix Domke <tmbinc@elitedvb.net>
21                      & Andreas Oberritter <obi@linuxtv.org>
22
23
24     Support for Samsung TBMU24112IMB used on Technisat SkyStar2 rev. 2.6B
25
26     Copyright (C) 2003 Vadim Catana <skystar@moldova.cc>:
27
28     Support for Philips SU1278 on Technotrend hardware
29
30     Copyright (C) 2004 Andrew de Quincey <adq_dvb@lidskialf.net>
31
32     This program is free software; you can redistribute it and/or modify
33     it under the terms of the GNU General Public License as published by
34     the Free Software Foundation; either version 2 of the License, or
35     (at your option) any later version.
36
37     This program is distributed in the hope that it will be useful,
38     but WITHOUT ANY WARRANTY; without even the implied warranty of
39     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
40     GNU General Public License for more details.
41
42     You should have received a copy of the GNU General Public License
43     along with this program; if not, write to the Free Software
44     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
45
46 */    
47
48 #include <linux/init.h>
49 #include <linux/kernel.h>
50 #include <linux/module.h>
51 #include <linux/string.h>
52 #include <linux/slab.h>
53 #include <asm/div64.h>
54
55 #include "dvb_frontend.h"
56 #include "dvb_functions.h"
57
58 #if 0
59 #define dprintk(x...) printk(x)
60 #else
61 #define dprintk(x...)
62 #endif
63
64 static int stv0299_status = 0;
65 static int disable_typhoon = 0;
66
67 #define STATUS_BER 0
68 #define STATUS_UCBLOCKS 1
69
70
71 /* frontend types */
72 #define UNKNOWN_FRONTEND  -1
73 #define PHILIPS_SU1278_TSA      0 // SU1278 with TSA5059 synth and datasheet recommended settings
74 #define ALPS_BSRU6         1
75 #define LG_TDQF_S001F      2
76 #define PHILIPS_SU1278_TUA      3 // SU1278 with TUA6100 synth
77 #define SAMSUNG_TBMU24112IMB    4
78 #define PHILIPS_SU1278_TSA_TT   5 // SU1278 with TSA5059 synth and TechnoTrend settings
79 #define PHILIPS_SU1278_TSA_TY   6 // SU1278 with TUA5059 synth and Typhoon wiring
80
81 /* Master Clock = 88 MHz */
82 #define M_CLK (88000000UL) 
83
84 /* Master Clock for TT cards = 64 MHz */
85 #define M_CLK_SU1278_TSA_TT (64000000UL)
86
87 static struct dvb_frontend_info uni0299_info = {
88         .name                   = "STV0299/TSA5059/SL1935 based",
89         .type                   = FE_QPSK,
90         .frequency_min          = 950000,
91         .frequency_max          = 2150000,
92         .frequency_stepsize     = 125,   /* kHz for QPSK frontends */
93         .frequency_tolerance    = M_CLK/2000,
94         .symbol_rate_min        = 1000000,
95         .symbol_rate_max        = 45000000,
96         .symbol_rate_tolerance  = 500,  /* ppm */
97         .notifier_delay         = 0,
98         .caps = FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 | FE_CAN_FEC_3_4 |
99               FE_CAN_FEC_5_6 | FE_CAN_FEC_7_8 |
100               FE_CAN_QPSK |
101               FE_CAN_FEC_AUTO
102 };
103
104
105 struct stv0299_state {
106         u8 tuner_type;
107         u8 initialised:1;
108         u32 tuner_frequency;
109         u32 symbol_rate;
110         fe_code_rate_t fec_inner;
111 };
112
113
114 static u8 init_tab [] = {
115         0x04, 0x7d,   /* F22FR = 0x7d                                 */
116                       /* F22 = f_VCO / 128 / 0x7d = 22 kHz            */
117
118         /* I2C bus repeater */
119         0x05, 0x35,   /* I2CT = 0, SCLT = 1, SDAT = 1                 */
120
121         /* general purpose DAC registers */
122         0x06, 0x40,   /* DAC not used, set to high impendance mode    */
123         0x07, 0x00,   /* DAC LSB                                      */
124
125         /* DiSEqC registers */
126         0x08, 0x40,   /* DiSEqC off, LNB power on OP2/LOCK pin on     */
127         0x09, 0x00,   /* FIFO                                         */
128
129         /* Input/Output configuration register */
130         0x0c, 0x51,   /* OP1 ctl = Normal, OP1 val = 1 (LNB Power ON) */
131                       /* OP0 ctl = Normal, OP0 val = 1 (18 V)         */
132                       /* Nyquist filter = 00, QPSK reverse = 0        */
133                    
134         /* AGC1 control register */
135         0x0d, 0x82,   /* DC offset compensation = ON, beta_agc1 = 2   */
136
137         /* Timing loop register */
138         0x0e, 0x23,   /* alpha_tmg = 2, beta_tmg = 3                  */
139
140         0x10, 0x3f,   // AGC2  0x3d
141
142         0x11, 0x84,
143         0x12, 0xb5,   // Lock detect: -64  Carrier freq detect:on
144
145         0x15, 0xc9,   // lock detector threshold
146
147         0x16, 0x00,
148         0x17, 0x00,
149         0x18, 0x00,
150         0x19, 0x00,
151         0x1a, 0x00,
152
153         0x1f, 0x50,
154
155         0x20, 0x00,
156         0x21, 0x00,
157         0x22, 0x00,
158         0x23, 0x00,
159
160         0x28, 0x00,  // out imp: normal  out type: parallel FEC mode:0
161
162         0x29, 0x1e,  // 1/2 threshold
163         0x2a, 0x14,  // 2/3 threshold
164         0x2b, 0x0f,  // 3/4 threshold
165         0x2c, 0x09,  // 5/6 threshold
166         0x2d, 0x05,  // 7/8 threshold
167         0x2e, 0x01,
168
169         0x31, 0x1f,  // test all FECs
170
171         0x32, 0x19,  // viterbi and synchro search
172         0x33, 0xfc,  // rs control
173         0x34, 0x93,  // error control
174 };
175
176
177 static u8 init_tab_samsung [] = {
178         0x01, 0x15,
179         0x02, 0x00,
180         0x03, 0x00,
181         0x04, 0x7D,
182         0x05, 0x35,
183         0x06, 0x02,
184         0x07, 0x00,
185         0x08, 0xC3,
186         0x0C, 0x00,
187         0x0D, 0x81,
188         0x0E, 0x23,
189         0x0F, 0x12,
190         0x10, 0x7E,
191         0x11, 0x84,
192         0x12, 0xB9,
193         0x13, 0x88,
194         0x14, 0x89,
195         0x15, 0xC9,
196         0x16, 0x00,
197         0x17, 0x5C,
198         0x18, 0x00,
199         0x19, 0x00,
200         0x1A, 0x00,
201         0x1C, 0x00,
202         0x1D, 0x00,
203         0x1E, 0x00,
204         0x1F, 0x3A,
205         0x20, 0x2E,
206         0x21, 0x80,
207         0x22, 0xFF,
208         0x23, 0xC1,
209         0x28, 0x00,
210         0x29, 0x1E,
211         0x2A, 0x14,
212         0x2B, 0x0F,
213         0x2C, 0x09,
214         0x2D, 0x05,
215         0x31, 0x1F,
216         0x32, 0x19,
217         0x33, 0xFE,
218         0x34, 0x93
219 };
220
221
222 static u8 init_tab_su1278_tsa_tt [] = {
223         0x01, 0x0f,
224         0x02, 0x30,
225         0x03, 0x00,
226         0x04, 0x5b,
227         0x05, 0x85,
228         0x06, 0x02,
229         0x07, 0x00,
230         0x08, 0x02,
231         0x09, 0x00,
232         0x0C, 0x01,
233         0x0D, 0x81,
234         0x0E, 0x44,
235         0x0f, 0x14,
236         0x10, 0x3c,
237         0x11, 0x84,
238         0x12, 0xda,
239         0x13, 0x97,
240         0x14, 0x95,
241         0x15, 0xc9,
242         0x16, 0x19,
243         0x17, 0x8c,
244         0x18, 0x59,
245         0x19, 0xf8,
246         0x1a, 0xfe,
247         0x1c, 0x7f,
248         0x1d, 0x00,
249         0x1e, 0x00,
250         0x1f, 0x50,
251         0x20, 0x00,
252         0x21, 0x00,
253         0x22, 0x00,
254         0x23, 0x00,
255         0x28, 0x00,
256         0x29, 0x28,
257         0x2a, 0x14,
258         0x2b, 0x0f,
259         0x2c, 0x09,
260         0x2d, 0x09,
261         0x31, 0x1f,
262         0x32, 0x19,
263         0x33, 0xfc,
264         0x34, 0x13
265 };
266
267 static int stv0299_set_FEC (struct dvb_i2c_bus *i2c, fe_code_rate_t fec);
268 static int stv0299_set_symbolrate (struct dvb_i2c_bus *i2c, u32 srate, int tuner_type);
269
270 static int stv0299_writereg (struct dvb_i2c_bus *i2c, u8 reg, u8 data)
271 {
272         int ret;
273         u8 buf [] = { reg, data };
274         struct i2c_msg msg = { .addr = 0x68, .flags = 0, .buf = buf, .len = 2 };
275
276         ret = i2c->xfer (i2c, &msg, 1);
277
278         if (ret != 1) 
279                 dprintk("%s: writereg error (reg == 0x%02x, val == 0x%02x, "
280                         "ret == %i)\n", __FUNCTION__, reg, data, ret);
281
282         return (ret != 1) ? -1 : 0;
283 }
284
285
286 static u8 stv0299_readreg (struct dvb_i2c_bus *i2c, u8 reg)
287 {
288         int ret;
289         u8 b0 [] = { reg };
290         u8 b1 [] = { 0 };
291         struct i2c_msg msg [] = { { .addr = 0x68, .flags = 0, .buf = b0, .len = 1 },
292                            { .addr = 0x68, .flags = I2C_M_RD, .buf = b1, .len = 1 } };
293
294         ret = i2c->xfer (i2c, msg, 2);
295         
296         if (ret != 2) 
297                 dprintk("%s: readreg error (reg == 0x%02x, ret == %i)\n",
298                                 __FUNCTION__, reg, ret);
299
300         return b1[0];
301 }
302
303
304 static int stv0299_readregs (struct dvb_i2c_bus *i2c, u8 reg1, u8 *b, u8 len)
305 {
306         int ret;
307         struct i2c_msg msg [] = { { .addr = 0x68, .flags = 0, .buf = &reg1, .len = 1 },
308                            { .addr = 0x68, .flags = I2C_M_RD, .buf = b, .len = len } };
309
310         ret = i2c->xfer (i2c, msg, 2);
311
312         if (ret != 2)
313                 dprintk("%s: readreg error (ret == %i)\n", __FUNCTION__, ret);
314
315         return ret == 2 ? 0 : ret;
316 }
317
318
319 static int pll_write (struct dvb_i2c_bus *i2c, u8 addr, u8 *data, int len)
320 {
321         int ret;
322         struct i2c_msg msg = { addr: addr, .flags = 0, .buf = data, .len = len };
323
324
325         stv0299_writereg(i2c, 0x05, 0xb5);      /*  enable i2c repeater on stv0299  */
326
327         ret =  i2c->xfer (i2c, &msg, 1);
328
329         stv0299_writereg(i2c, 0x05, 0x35);      /*  disable i2c repeater on stv0299  */
330
331         if (ret != 1)
332                 dprintk("%s: i/o error (ret == %i)\n", __FUNCTION__, ret);
333
334         return (ret != 1) ? -1 : 0;
335 }
336
337
338 static int sl1935_set_tv_freq (struct dvb_i2c_bus *i2c, u32 freq, int ftype)
339 {
340         u8 buf[4];
341         u32 div;
342
343         div = freq / 125;
344
345         dprintk("%s : freq = %i, div = %i\n", __FUNCTION__, freq, div);
346
347         buf[0] = (div >> 8) & 0x7f;
348         buf[1] = div & 0xff;
349         buf[2] = 0x84;  // 0xC4
350         buf[3] = 0x08;
351
352         if (freq < 1500000) buf[3] |= 0x10;
353
354         return pll_write (i2c, 0x61, buf, sizeof(buf));
355 }
356
357 /**
358  *   set up the downconverter frequency divisor for a 
359  *   reference clock comparision frequency of 125 kHz.
360  */
361 static int tsa5059_set_tv_freq  (struct dvb_i2c_bus *i2c, u32 freq, int ftype, int srate)
362 {
363         u8 addr;
364         u32 div;
365         u8 buf[4];
366         int divisor, regcode;
367
368         dprintk ("%s: freq %i, ftype %i\n", __FUNCTION__, freq, ftype);
369
370         if ((freq < 950000) || (freq > 2150000)) return -EINVAL;
371
372         if (ftype == PHILIPS_SU1278_TSA_TT) {
373         divisor = 500;
374         regcode = 2;
375         } else {
376                 divisor = 125;
377                 regcode = 4;
378         }
379
380         // setup frequency divisor
381         div = (freq + (divisor - 1)) / divisor; // round correctly
382         buf[0] = (div >> 8) & 0x7f;
383         buf[1] = div & 0xff;
384         buf[2] = 0x80 | ((div & 0x18000) >> 10) | regcode;
385         buf[3] = 0;
386
387         // tuner-specific settings
388         switch(ftype) {
389         case PHILIPS_SU1278_TSA:
390         case PHILIPS_SU1278_TSA_TT:
391         case PHILIPS_SU1278_TSA_TY:
392                 if (ftype == PHILIPS_SU1278_TSA_TY)
393                         addr = 0x61;
394                 else
395                 addr = 0x60;
396
397                 buf[3] |= 0x20;
398
399                 if (srate < 4000000) buf[3] |= 1;
400            
401                 if (freq < 1250000) buf[3] |= 0;
402                 else if (freq < 1550000) buf[3] |= 0x40;
403                 else if (freq < 2050000) buf[3] |= 0x80;
404                 else if (freq < 2150000) buf[3] |= 0xC0;
405                 break;
406
407         case ALPS_BSRU6:
408                 addr = 0x61;
409                 buf[3] = 0xC4;
410                 if (freq > 1530000) buf[3] = 0xc0;
411                 break;
412
413         default:
414                 return -EINVAL;
415         }
416
417         return pll_write (i2c, addr, buf, sizeof(buf));
418 }
419
420
421
422 #define ABS(x) ((x) < 0 ? -(x) : (x))
423 #define MIN2(a,b) ((a) < (b) ? (a) : (b))
424 #define MIN3(a,b,c) MIN2(MIN2(a,b),c)
425
426 static int tua6100_set_tv_freq  (struct dvb_i2c_bus *i2c, u32 freq,
427                          int ftype, int srate)
428 {
429         u8 reg0 [2] = { 0x00, 0x00 };
430         u8 reg1 [4] = { 0x01, 0x00, 0x00, 0x00 };
431         u8 reg2 [3] = { 0x02, 0x00, 0x00 };
432         int _fband;
433         int first_ZF;
434         int R, A, N, P, M;
435         int err;
436
437         first_ZF = (freq) / 1000;
438
439         if (ABS(MIN2(ABS(first_ZF-1190),ABS(first_ZF-1790))) <
440             ABS(MIN3(ABS(first_ZF-1202),ABS(first_ZF-1542),ABS(first_ZF-1890))))
441                 _fband = 2;
442         else
443                 _fband = 3;
444
445         if (_fband == 2) {
446                 if (((first_ZF >= 950) && (first_ZF < 1350)) ||
447                     ((first_ZF >= 1430) && (first_ZF < 1950)))
448                         reg0[1] = 0x07;
449                 else if (((first_ZF >= 1350) && (first_ZF < 1430)) ||
450                             ((first_ZF >= 1950) && (first_ZF < 2150)))
451                         reg0[1] = 0x0B;
452         }
453
454         if(_fband == 3) {
455                 if (((first_ZF >= 950) && (first_ZF < 1350)) ||
456                      ((first_ZF >= 1455) && (first_ZF < 1950)))
457                         reg0[1] = 0x07;
458                 else if (((first_ZF >= 1350) && (first_ZF < 1420)) ||
459                          ((first_ZF >= 1950) && (first_ZF < 2150)))
460                         reg0[1] = 0x0B;
461                 else if ((first_ZF >= 1420) && (first_ZF < 1455))
462                         reg0[1] = 0x0F;
463 }
464
465         if (first_ZF > 1525)
466                 reg1[1] |= 0x80;
467         else
468                 reg1[1] &= 0x7F;
469
470         if (_fband == 2) {
471                 if (first_ZF > 1430) { /* 1430MHZ */
472                         reg1[1] &= 0xCF; /* N2 */
473                         reg2[1] &= 0xCF; /* R2 */
474                         reg2[1] |= 0x10;
475                 } else {
476                         reg1[1] &= 0xCF; /* N2 */
477                         reg1[1] |= 0x20;
478                         reg2[1] &= 0xCF; /* R2 */
479                         reg2[1] |= 0x10;
480                 }
481 }
482
483         if (_fband == 3) {
484                 if ((first_ZF >= 1455) &&
485                     (first_ZF < 1630)) {
486                         reg1[1] &= 0xCF; /* N2 */
487                         reg1[1] |= 0x20;
488                         reg2[1] &= 0xCF; /* R2 */
489                 } else {
490                         if (first_ZF < 1455) {
491                                 reg1[1] &= 0xCF; /* N2 */
492                                 reg1[1] |= 0x20;
493                                 reg2[1] &= 0xCF; /* R2 */
494                                 reg2[1] |= 0x10;
495                         } else {
496                                 if (first_ZF >= 1630) {
497                                         reg1[1] &= 0xCF; /* N2 */
498                                         reg2[1] &= 0xCF; /* R2 */
499                                         reg2[1] |= 0x10;
500                                 }
501                         }
502                 }
503         }
504
505         /* set ports, enable P0 for symbol rates > 4Ms/s */
506         if (srate >= 4000000)
507                 reg1[1] |= 0x0c;
508         else
509                 reg1[1] |= 0x04;
510
511         reg2[1] |= 0x0c;
512
513         R = 64;
514         A = 64;
515         P = 64;  //32
516
517         M = (freq * R) / 4;             /* in Mhz */
518         N = (M - A * 1000) / (P * 1000);
519
520         reg1[1] |= (N >> 9) & 0x03;
521         reg1[2]  = (N >> 1) & 0xff;
522         reg1[3]  = (N << 7) & 0x80;
523
524         reg2[1] |= (R >> 8) & 0x03;
525         reg2[2]  = R & 0xFF;    /* R */
526
527         reg1[3] |= A & 0x7f;    /* A */
528
529         if (P == 64)
530                 reg1[1] |= 0x40; /* Prescaler 64/65 */
531
532         reg0[1] |= 0x03;
533
534         if ((err = pll_write(i2c, 0x60, reg0, sizeof(reg0))))
535                 return err;
536
537         if ((err = pll_write(i2c, 0x60, reg1, sizeof(reg1))))
538                 return err;
539
540         if ((err = pll_write(i2c, 0x60, reg2, sizeof(reg2))))
541                 return err;
542
543         return 0;
544 }
545
546
547 static int pll_set_tv_freq (struct dvb_i2c_bus *i2c, u32 freq, int ftype, int srate)
548 {
549         switch(ftype) {
550         case SAMSUNG_TBMU24112IMB:
551                 return sl1935_set_tv_freq(i2c, freq, ftype);
552
553         case LG_TDQF_S001F:
554                 return sl1935_set_tv_freq(i2c, freq, ftype);
555
556         case PHILIPS_SU1278_TUA:
557                 return tua6100_set_tv_freq(i2c, freq, ftype, srate);
558
559         default:
560                 return tsa5059_set_tv_freq(i2c, freq, ftype, srate);
561 }
562 }
563
564 #if 0
565 static int tsa5059_read_status  (struct dvb_i2c_bus *i2c)
566 {
567         int ret;
568         u8 rpt1 [] = { 0x05, 0xb5 };
569         u8 stat [] = { 0 };
570
571         struct i2c_msg msg [] = {{ .addr = 0x68, .flags = 0, .buf = rpt1, .len = 2 },
572                           { .addr = 0x60, .flags = I2C_M_RD, .buf = stat, .len = 1 }};
573
574         dprintk ("%s\n", __FUNCTION__);
575
576         ret = i2c->xfer (i2c, msg, 2);
577
578         if (ret != 2)
579                 dprintk("%s: readreg error (ret == %i)\n", __FUNCTION__, ret);
580
581         return stat[0];
582 }
583 #endif
584
585
586 static int stv0299_init (struct dvb_i2c_bus *i2c, int ftype)
587 {
588         int i;
589
590         dprintk("stv0299: init chip\n");
591
592         switch(ftype) {
593         case SAMSUNG_TBMU24112IMB:
594                 dprintk("%s: init stv0299 chip for Samsung TBMU24112IMB\n", __FUNCTION__);
595
596                 for (i=0; i<sizeof(init_tab_samsung); i+=2)
597                 {
598                         dprintk("%s: reg == 0x%02x, val == 0x%02x\n", __FUNCTION__, init_tab_samsung[i], init_tab_samsung[i+1]);
599
600                         stv0299_writereg (i2c, init_tab_samsung[i], init_tab_samsung[i+1]);
601                 }
602                 break;
603
604         case PHILIPS_SU1278_TSA_TT:
605                 for (i=0; i<sizeof(init_tab_su1278_tsa_tt); i+=2) {
606                         stv0299_writereg (i2c, init_tab_su1278_tsa_tt[i], init_tab_su1278_tsa_tt[i+1]);
607                 }
608                 break;
609
610         default:
611         stv0299_writereg (i2c, 0x01, 0x15);
612                 stv0299_writereg (i2c, 0x02, ftype == PHILIPS_SU1278_TUA ? 0x00 : 0x30);
613         stv0299_writereg (i2c, 0x03, 0x00);
614
615         for (i=0; i<sizeof(init_tab); i+=2)
616                 stv0299_writereg (i2c, init_tab[i], init_tab[i+1]);
617
618         /* AGC1 reference register setup */
619                 if (ftype == PHILIPS_SU1278_TSA || ftype == PHILIPS_SU1278_TSA_TY)
620                   stv0299_writereg (i2c, 0x0f, 0x92);  /* Iagc = Inverse, m1 = 18 */
621                 else if (ftype == PHILIPS_SU1278_TUA)
622                   stv0299_writereg (i2c, 0x0f, 0x94);  /* Iagc = Inverse, m1 = 20 */
623         else
624           stv0299_writereg (i2c, 0x0f, 0x52);  /* Iagc = Normal,  m1 = 18 */
625                 break;
626         }
627         
628         switch(stv0299_status) {
629         case STATUS_BER:
630                 stv0299_writereg(i2c, 0x34, 0x93);
631                 break;
632         
633         case STATUS_UCBLOCKS:
634                 stv0299_writereg(i2c, 0x34, 0xB3);
635                 break;
636         }
637
638         return 0;
639 }
640
641
642 static int stv0299_set_FEC (struct dvb_i2c_bus *i2c, fe_code_rate_t fec)
643 {
644         dprintk ("%s\n", __FUNCTION__);
645
646         switch (fec) {
647         case FEC_AUTO:
648         {
649                 dprintk ("%s : FEC_AUTO\n", __FUNCTION__);
650                 return stv0299_writereg (i2c, 0x31, 0x1f);
651         }
652         case FEC_1_2:
653         {
654                 dprintk ("%s : FEC_1_2\n", __FUNCTION__);
655                 return stv0299_writereg (i2c, 0x31, 0x01);
656         }
657         case FEC_2_3:
658         {
659                 dprintk ("%s : FEC_2_3\n", __FUNCTION__);
660                 return stv0299_writereg (i2c, 0x31, 0x02);
661         }
662         case FEC_3_4:
663         {
664                 dprintk ("%s : FEC_3_4\n", __FUNCTION__);
665                 return stv0299_writereg (i2c, 0x31, 0x04);
666         }
667         case FEC_5_6:
668         {
669                 dprintk ("%s : FEC_5_6\n", __FUNCTION__);
670                 return stv0299_writereg (i2c, 0x31, 0x08);
671         }
672         case FEC_7_8:
673         {
674                 dprintk ("%s : FEC_7_8\n", __FUNCTION__);
675                 return stv0299_writereg (i2c, 0x31, 0x10);
676         }
677         default:
678         {
679                 dprintk ("%s : FEC invalid\n", __FUNCTION__);
680                 return -EINVAL;
681         }
682 }
683 }
684
685
686 static fe_code_rate_t stv0299_get_fec (struct dvb_i2c_bus *i2c)
687 {
688         static fe_code_rate_t fec_tab [] = { FEC_2_3, FEC_3_4, FEC_5_6,
689                                              FEC_7_8, FEC_1_2 };
690         u8 index;
691
692         dprintk ("%s\n", __FUNCTION__);
693
694         index = stv0299_readreg (i2c, 0x1b);
695         index &= 0x7;
696
697         if (index > 4)
698                 return FEC_AUTO;
699
700         return fec_tab [index];
701 }
702
703
704 static int stv0299_wait_diseqc_fifo (struct dvb_i2c_bus *i2c, int timeout)
705 {
706         unsigned long start = jiffies;
707
708         dprintk ("%s\n", __FUNCTION__);
709
710         while (stv0299_readreg(i2c, 0x0a) & 1) {
711                 if (jiffies - start > timeout) {
712                         dprintk ("%s: timeout!!\n", __FUNCTION__);
713                         return -ETIMEDOUT;
714                 }
715                 dvb_delay(10);
716         };
717
718         return 0;
719 }
720
721
722 static int stv0299_wait_diseqc_idle (struct dvb_i2c_bus *i2c, int timeout)
723 {
724         unsigned long start = jiffies;
725
726         dprintk ("%s\n", __FUNCTION__);
727
728         while ((stv0299_readreg(i2c, 0x0a) & 3) != 2 ) {
729                 if (jiffies - start > timeout) {
730                         dprintk ("%s: timeout!!\n", __FUNCTION__);
731                         return -ETIMEDOUT;
732                 }
733                 dvb_delay(10);
734         };
735
736         return 0;
737 }
738
739
740 static int stv0299_send_diseqc_msg (struct dvb_i2c_bus *i2c,
741                              struct dvb_diseqc_master_cmd *m)
742 {
743         u8 val;
744         int i;
745
746         dprintk ("%s\n", __FUNCTION__);
747
748         if (stv0299_wait_diseqc_idle (i2c, 100) < 0)
749                 return -ETIMEDOUT;
750
751         val = stv0299_readreg (i2c, 0x08);
752
753         if (stv0299_writereg (i2c, 0x08, (val & ~0x7) | 0x6))  /* DiSEqC mode */
754                 return -EREMOTEIO;
755
756         for (i=0; i<m->msg_len; i++) {
757                 if (stv0299_wait_diseqc_fifo (i2c, 100) < 0)
758                         return -ETIMEDOUT;
759
760                 if (stv0299_writereg (i2c, 0x09, m->msg[i]))
761                         return -EREMOTEIO;
762         }
763
764         if (stv0299_wait_diseqc_idle (i2c, 100) < 0)
765                 return -ETIMEDOUT;
766
767         return 0;
768 }
769
770
771 static int stv0299_send_diseqc_burst (struct dvb_i2c_bus *i2c, fe_sec_mini_cmd_t burst)
772 {
773         u8 val;
774
775         dprintk ("%s\n", __FUNCTION__);
776
777         if (stv0299_wait_diseqc_idle (i2c, 100) < 0)
778                 return -ETIMEDOUT;
779
780         val = stv0299_readreg (i2c, 0x08);
781
782         if (stv0299_writereg (i2c, 0x08, (val & ~0x7) | 0x2))   /* burst mode */
783                 return -EREMOTEIO;
784
785         if (stv0299_writereg (i2c, 0x09, burst == SEC_MINI_A ? 0x00 : 0xff))
786                 return -EREMOTEIO;
787
788         if (stv0299_wait_diseqc_idle (i2c, 100) < 0)
789                 return -ETIMEDOUT;
790
791         if (stv0299_writereg (i2c, 0x08, val))
792                 return -EREMOTEIO;
793
794         return 0;
795 }
796
797
798 static int stv0299_set_tone (struct dvb_i2c_bus *i2c, fe_sec_tone_mode_t tone)
799 {
800         u8 val;
801
802         dprintk("%s: %s\n", __FUNCTION__,
803                 tone == SEC_TONE_ON ? "SEC_TONE_ON" : 
804                 tone == SEC_TONE_OFF ? "SEC_TONE_OFF" : "??");
805
806         if (stv0299_wait_diseqc_idle (i2c, 100) < 0)
807                 return -ETIMEDOUT;
808
809         val = stv0299_readreg (i2c, 0x08);
810
811         switch (tone) {
812         case SEC_TONE_ON:
813         {
814                 dprintk("%s: TONE_ON\n", __FUNCTION__);
815                 return stv0299_writereg (i2c, 0x08, val | 0x3);
816         }       
817         case SEC_TONE_OFF:
818         {
819                 dprintk("%s: TONE_OFF\n", __FUNCTION__);
820                 return stv0299_writereg (i2c, 0x08, (val & ~0x3) | 0x02);
821         }
822         default:
823         {
824                 dprintk("%s: TONE INVALID\n", __FUNCTION__);
825                 return -EINVAL;
826         }
827         };
828 }
829
830
831 static int stv0299_set_voltage (struct dvb_i2c_bus *i2c, fe_sec_voltage_t voltage,
832                                 int tuner_type)
833 {
834         u8 reg0x08;
835         u8 reg0x0c;
836
837         dprintk("%s: %s\n", __FUNCTION__,
838                 voltage == SEC_VOLTAGE_13 ? "SEC_VOLTAGE_13" : 
839                 voltage == SEC_VOLTAGE_18 ? "SEC_VOLTAGE_18" : "??");
840
841         reg0x08 = stv0299_readreg (i2c, 0x08);
842         reg0x0c = stv0299_readreg (i2c, 0x0c);
843
844         /**
845          *  H/V switching over OP0, OP1 and OP2 are LNB power enable bits
846          */
847         reg0x0c &= 0x0f;
848
849         if (voltage == SEC_VOLTAGE_OFF) {
850                 stv0299_writereg (i2c, 0x0c, 0x00); /*  LNB power off! */
851                 return stv0299_writereg (i2c, 0x08, 0x00); /*   LNB power off! */
852         }
853         
854                 stv0299_writereg (i2c, 0x08, reg0x08 | 0x40);
855
856         switch (voltage) {
857         case SEC_VOLTAGE_13:
858                 if (tuner_type == PHILIPS_SU1278_TSA_TY)
859                         return stv0299_writereg (i2c, 0x0c, reg0x0c | 0x10);
860                 else
861                         return stv0299_writereg (i2c, 0x0c, reg0x0c | 0x40);
862
863         case SEC_VOLTAGE_18:
864                 return stv0299_writereg (i2c, 0x0c, reg0x0c | 0x50);
865
866         default:
867                 return -EINVAL;
868         };
869 }
870
871
872 static int stv0299_set_symbolrate (struct dvb_i2c_bus *i2c, u32 srate, int tuner_type)
873 {
874         u64 big = srate;
875         u32 ratio;
876         u8 aclk = 0;
877         u8 bclk = 0;
878         u8 m1;
879         int Mclk = M_CLK;
880
881         // check rate is within limits
882         if ((srate < 1000000) || (srate > 45000000)) return -EINVAL;
883
884         // calculate value to program
885         if (tuner_type == PHILIPS_SU1278_TSA_TT) Mclk = M_CLK_SU1278_TSA_TT;
886         big = big << 20;
887         big += (Mclk-1); // round correctly
888         do_div(big, Mclk);
889         ratio = big << 4;
890
891         // program registers
892         switch(tuner_type) {
893         case PHILIPS_SU1278_TSA_TT:
894                 stv0299_writereg (i2c, 0x0e, 0x44);
895                 if (srate >= 10000000) {
896                         stv0299_writereg (i2c, 0x13, 0x97);
897                         stv0299_writereg (i2c, 0x14, 0x95);
898                         stv0299_writereg (i2c, 0x15, 0xc9);
899                         stv0299_writereg (i2c, 0x17, 0x8c);
900                         stv0299_writereg (i2c, 0x1a, 0xfe);
901                         stv0299_writereg (i2c, 0x1c, 0x7f);
902                         stv0299_writereg (i2c, 0x2d, 0x09);
903                 } else {
904                         stv0299_writereg (i2c, 0x13, 0x99);
905                         stv0299_writereg (i2c, 0x14, 0x8d);
906                         stv0299_writereg (i2c, 0x15, 0xce);
907                         stv0299_writereg (i2c, 0x17, 0x43);
908                         stv0299_writereg (i2c, 0x1a, 0x1d);
909                         stv0299_writereg (i2c, 0x1c, 0x12);
910                         stv0299_writereg (i2c, 0x2d, 0x05);
911                 }
912                 stv0299_writereg (i2c, 0x0e, 0x23);
913                 stv0299_writereg (i2c, 0x0f, 0x94);
914                 stv0299_writereg (i2c, 0x10, 0x39);
915                 stv0299_writereg (i2c, 0x15, 0xc9);
916
917                 stv0299_writereg (i2c, 0x1f, (ratio >> 16) & 0xff);
918                 stv0299_writereg (i2c, 0x20, (ratio >>  8) & 0xff);
919                 stv0299_writereg (i2c, 0x21, (ratio      ) & 0xf0);
920                 break;
921
922         case PHILIPS_SU1278_TSA_TY:
923         case PHILIPS_SU1278_TSA:
924                 aclk = 0xb5;
925                 if (srate < 2000000) bclk = 0x86;
926                 else if (srate < 5000000) bclk = 0x89;
927                 else if (srate < 15000000) bclk = 0x8f;
928                 else if (srate < 45000000) bclk = 0x95;
929
930                 m1 = 0x14;
931                 if (srate < 4000000) m1 = 0x10;
932
933                 stv0299_writereg (i2c, 0x13, aclk);
934                 stv0299_writereg (i2c, 0x14, bclk);
935                 stv0299_writereg (i2c, 0x1f, (ratio >> 16) & 0xff);
936                 stv0299_writereg (i2c, 0x20, (ratio >>  8) & 0xff);
937                 stv0299_writereg (i2c, 0x21, (ratio      ) & 0xf0);
938                 stv0299_writereg (i2c, 0x0f, (stv0299_readreg(i2c, 0x0f) & 0xc0) | m1);
939                 break;
940
941         case ALPS_BSRU6:
942         default:
943                 if (srate < 1500000) { aclk = 0xb7; bclk = 0x47; }
944                 else if (srate < 3000000) { aclk = 0xb7; bclk = 0x4b; }
945                 else if (srate < 7000000) { aclk = 0xb7; bclk = 0x4f; }
946                 else if (srate < 14000000) { aclk = 0xb7; bclk = 0x53; }
947                 else if (srate < 30000000) { aclk = 0xb6; bclk = 0x53; }
948                 else if (srate < 45000000) { aclk = 0xb4; bclk = 0x51; }
949   
950         stv0299_writereg (i2c, 0x13, aclk);
951         stv0299_writereg (i2c, 0x14, bclk);
952         stv0299_writereg (i2c, 0x1f, (ratio >> 16) & 0xff);
953         stv0299_writereg (i2c, 0x20, (ratio >>  8) & 0xff);
954         stv0299_writereg (i2c, 0x21, (ratio      ) & 0xf0);
955                 break;
956         }
957
958
959         return 0;
960 }
961
962
963 static int stv0299_get_symbolrate (struct dvb_i2c_bus *i2c, int tuner_type)
964 {
965         u32 Mclk = M_CLK / 4096L;
966         u32 srate;
967         s32 offset;
968         u8 sfr[3];
969         s8 rtf;
970
971         dprintk ("%s\n", __FUNCTION__);
972
973         if (tuner_type == PHILIPS_SU1278_TSA_TT) Mclk = M_CLK_SU1278_TSA_TT / 4096L;
974
975         stv0299_readregs (i2c, 0x1f, sfr, 3);
976         stv0299_readregs (i2c, 0x1a, &rtf, 1);
977
978         srate = (sfr[0] << 8) | sfr[1];
979         srate *= Mclk;
980         srate /= 16;
981         srate += (sfr[2] >> 4) * Mclk / 256;
982
983         offset = (s32) rtf * (srate / 4096L);
984         offset /= 128;
985
986         dprintk ("%s : srate = %i\n", __FUNCTION__, srate);
987         dprintk ("%s : ofset = %i\n", __FUNCTION__, offset);
988
989         srate += offset;
990
991         srate += 1000;
992         srate /= 2000;
993         srate *= 2000;
994
995         return srate;
996 }
997
998 static int uni0299_ioctl (struct dvb_frontend *fe, unsigned int cmd, void *arg)
999 {
1000         struct dvb_i2c_bus *i2c = fe->i2c;
1001         struct stv0299_state *state = (struct stv0299_state *) fe->data;
1002
1003         dprintk ("%s\n", __FUNCTION__);
1004
1005         switch (cmd) {
1006         case FE_GET_INFO:
1007         {
1008                 struct dvb_frontend_info* tmp = (struct dvb_frontend_info*) arg;
1009                 memcpy (arg, &uni0299_info, sizeof(struct dvb_frontend_info));
1010
1011                 if (state->tuner_type == PHILIPS_SU1278_TSA_TT) {
1012                         tmp->frequency_tolerance = M_CLK_SU1278_TSA_TT / 2000;
1013                 }
1014                 break;
1015         }
1016
1017         case FE_READ_STATUS:
1018         {
1019                 fe_status_t *status = (fe_status_t *) arg;
1020                 u8 signal = 0xff - stv0299_readreg (i2c, 0x18);
1021                 u8 sync = stv0299_readreg (i2c, 0x1b);
1022
1023                 dprintk ("%s : FE_READ_STATUS : VSTATUS: 0x%02x\n", __FUNCTION__, sync);
1024
1025                 *status = 0;
1026
1027                 if (signal > 10)
1028                         *status |= FE_HAS_SIGNAL;
1029
1030                 if (sync & 0x80)
1031                         *status |= FE_HAS_CARRIER;
1032
1033                 if (sync & 0x10)
1034                         *status |= FE_HAS_VITERBI;
1035
1036                 if (sync & 0x08)
1037                         *status |= FE_HAS_SYNC;
1038
1039                 if ((sync & 0x98) == 0x98)
1040                         *status |= FE_HAS_LOCK;
1041
1042                 break;
1043         }
1044
1045         case FE_READ_BER:
1046                 if (stv0299_status == STATUS_BER) {
1047                 *((u32*) arg) = (stv0299_readreg (i2c, 0x1d) << 8)
1048                                | stv0299_readreg (i2c, 0x1e);
1049                 } else {
1050                         *((u32*) arg) = 0;
1051                 }
1052                 break;
1053
1054         case FE_READ_SIGNAL_STRENGTH:
1055         {
1056                 s32 signal =  0xffff - ((stv0299_readreg (i2c, 0x18) << 8)
1057                                        | stv0299_readreg (i2c, 0x19));
1058
1059                 dprintk ("%s : FE_READ_SIGNAL_STRENGTH : AGC2I: 0x%02x%02x, signal=0x%04x\n", __FUNCTION__,
1060                          stv0299_readreg (i2c, 0x18),
1061                          stv0299_readreg (i2c, 0x19), (int) signal);
1062
1063                 signal = signal * 5 / 4;
1064                 *((u16*) arg) = (signal > 0xffff) ? 0xffff :
1065                                 (signal < 0) ? 0 : signal;
1066                 break;
1067         }
1068         case FE_READ_SNR:
1069         {
1070                 s32 snr = 0xffff - ((stv0299_readreg (i2c, 0x24) << 8)
1071                                    | stv0299_readreg (i2c, 0x25));
1072                 snr = 3 * (snr - 0xa100);
1073                 *((u16*) arg) = (snr > 0xffff) ? 0xffff :
1074                                 (snr < 0) ? 0 : snr;
1075                 break;
1076         }
1077         case FE_READ_UNCORRECTED_BLOCKS: 
1078                 if (stv0299_status == STATUS_UCBLOCKS) {
1079                         *((u32*) arg) = (stv0299_readreg (i2c, 0x1d) << 8)
1080                                        | stv0299_readreg (i2c, 0x1e);
1081                 } else {
1082                         *((u32*) arg) = 0;
1083                 }
1084                 break;
1085
1086         case FE_SET_FRONTEND:
1087         {
1088                 struct dvb_frontend_parameters *p = arg;
1089                 int invval = 0;
1090
1091                 dprintk ("%s : FE_SET_FRONTEND\n", __FUNCTION__);
1092
1093                 // set the inversion
1094                 if (p->inversion == INVERSION_OFF) invval = 0;
1095                 else if (p->inversion == INVERSION_ON) invval = 1;
1096                 else {
1097                         printk("stv0299 does not support auto-inversion\n");
1098                         return -EINVAL;
1099                 }
1100                 if (state->tuner_type == ALPS_BSRU6) invval = (~invval) & 1;
1101                 stv0299_writereg(i2c, 0x0c, (stv0299_readreg(i2c, 0x0c) & 0xfe) | invval);
1102
1103                 switch(state->tuner_type) {
1104                 case PHILIPS_SU1278_TSA_TT: 
1105                 {
1106                         /* check if we should do a finetune */
1107                         int frequency_delta = p->frequency - state->tuner_frequency;
1108                         int minmax = p->u.qpsk.symbol_rate / 2000;
1109                         if (minmax < 5000) minmax = 5000;
1110                    
1111                         if ((frequency_delta > -minmax) && (frequency_delta < minmax) && (frequency_delta != 0) &&
1112                             (state->fec_inner == p->u.qpsk.fec_inner) && 
1113                             (state->symbol_rate == p->u.qpsk.symbol_rate)) {
1114                                 int Drot_freq = (frequency_delta << 16) / (M_CLK_SU1278_TSA_TT / 1000);
1115
1116                                 // zap the derotator registers first
1117                                 stv0299_writereg (i2c, 0x22, 0x00); 
1118                                 stv0299_writereg (i2c, 0x23, 0x00);
1119
1120                                 // now set them as we want
1121                                 stv0299_writereg (i2c, 0x22, Drot_freq >> 8);
1122                                 stv0299_writereg (i2c, 0x23, Drot_freq);
1123                         } else {
1124                                 /* A "normal" tune is requested */
1125                                 pll_set_tv_freq (i2c, p->frequency, state->tuner_type, p->u.qpsk.symbol_rate);
1126                                 stv0299_writereg (i2c, 0x32, 0x80);
1127                                 stv0299_writereg (i2c, 0x22, 0x00);
1128                                 stv0299_writereg (i2c, 0x23, 0x00);
1129                                 stv0299_writereg (i2c, 0x32, 0x19);
1130                                 stv0299_set_symbolrate (i2c, p->u.qpsk.symbol_rate, state->tuner_type);
1131                 stv0299_set_FEC (i2c, p->u.qpsk.fec_inner);
1132                         }
1133                         break;
1134                 }
1135                     
1136                 default:
1137                         pll_set_tv_freq (i2c, p->frequency, state->tuner_type, p->u.qpsk.symbol_rate);
1138                         stv0299_set_FEC (i2c, p->u.qpsk.fec_inner);
1139                         stv0299_set_symbolrate (i2c, p->u.qpsk.symbol_rate, state->tuner_type);
1140                 stv0299_writereg (i2c, 0x22, 0x00);
1141                 stv0299_writereg (i2c, 0x23, 0x00);
1142                 stv0299_readreg (i2c, 0x23);
1143                 stv0299_writereg (i2c, 0x12, 0xb9);
1144                         break;
1145                 }
1146
1147                 state->tuner_frequency = p->frequency;
1148                 state->fec_inner = p->u.qpsk.fec_inner;
1149                 state->symbol_rate = p->u.qpsk.symbol_rate;
1150                 break;
1151         }
1152
1153         case FE_GET_FRONTEND:
1154         {
1155                 struct dvb_frontend_parameters *p = arg;
1156                 s32 derot_freq;
1157                 int Mclk = M_CLK;
1158                 int invval;
1159
1160                 if (state->tuner_type == PHILIPS_SU1278_TSA_TT) Mclk = M_CLK_SU1278_TSA_TT;
1161
1162                 derot_freq = (s32)(s16) ((stv0299_readreg (i2c, 0x22) << 8)
1163                                         | stv0299_readreg (i2c, 0x23));
1164
1165                 derot_freq *= (Mclk >> 16);
1166                 derot_freq += 500;
1167                 derot_freq /= 1000;
1168
1169                 p->frequency += derot_freq;
1170
1171                 invval = stv0299_readreg (i2c, 0x0c) & 1;
1172                 if (state->tuner_type == ALPS_BSRU6) invval = (~invval) & 1;
1173                 p->inversion = invval ? INVERSION_ON : INVERSION_OFF;
1174
1175                 p->u.qpsk.fec_inner = stv0299_get_fec (i2c);
1176                 p->u.qpsk.symbol_rate = stv0299_get_symbolrate (i2c, state->tuner_type);
1177                 break;
1178         }
1179
1180         case FE_SLEEP:
1181                 stv0299_writereg (i2c, 0x0c, 0x00);  /*  LNB power off! */
1182                 stv0299_writereg (i2c, 0x08, 0x00); /*  LNB power off! */
1183                 stv0299_writereg (i2c, 0x02, 0x80);
1184                 state->initialised = 0;
1185                 break;
1186
1187         case FE_INIT:
1188                 switch(state->tuner_type) {
1189                 case PHILIPS_SU1278_TSA_TT:
1190                         state->tuner_frequency = 0;
1191                         if (!state->initialised) {
1192                                 state->initialised = 1;
1193                                 return stv0299_init (i2c, state->tuner_type);
1194                         }
1195                         break;
1196
1197                 default:
1198                         return stv0299_init (i2c, state->tuner_type);
1199                 }
1200                 break;
1201
1202         case FE_DISEQC_SEND_MASTER_CMD:
1203                 return stv0299_send_diseqc_msg (i2c, arg);
1204
1205         case FE_DISEQC_SEND_BURST:
1206                 return stv0299_send_diseqc_burst (i2c, (fe_sec_mini_cmd_t) arg);
1207
1208         case FE_SET_TONE:
1209                 return stv0299_set_tone (i2c, (fe_sec_tone_mode_t) arg);
1210
1211         case FE_SET_VOLTAGE:
1212                 return stv0299_set_voltage (i2c, (fe_sec_voltage_t) arg,
1213                                             state->tuner_type);
1214
1215         case FE_GET_TUNE_SETTINGS:
1216         {
1217                 struct dvb_frontend_tune_settings* fesettings = (struct dvb_frontend_tune_settings*) arg;
1218             
1219                 switch(state->tuner_type) {
1220                 case PHILIPS_SU1278_TSA_TT:
1221                         fesettings->min_delay_ms = 50;
1222                         if (fesettings->parameters.u.qpsk.symbol_rate < 10000000) {
1223                                 fesettings->step_size = fesettings->parameters.u.qpsk.symbol_rate / 32000;
1224                                 fesettings->max_drift = 5000;
1225                         } else {
1226                                 fesettings->step_size = fesettings->parameters.u.qpsk.symbol_rate / 16000;
1227                                 fesettings->max_drift = fesettings->parameters.u.qpsk.symbol_rate / 2000;
1228                         }
1229                         break;
1230
1231                 default:
1232                         fesettings->min_delay_ms = 100;
1233                         if (fesettings->parameters.u.qpsk.symbol_rate < 10000000) {
1234                                 fesettings->step_size = fesettings->parameters.u.qpsk.symbol_rate / 32000;
1235                                 fesettings->max_drift = 5000;
1236                         } else {
1237                                 fesettings->step_size = fesettings->parameters.u.qpsk.symbol_rate / 16000;
1238                                 fesettings->max_drift = fesettings->parameters.u.qpsk.symbol_rate / 2000;
1239                         }
1240                         break;              
1241                 }
1242
1243                 return 0;
1244         }
1245
1246         default:
1247                 return -EOPNOTSUPP;
1248         };
1249
1250         return 0;
1251 }
1252
1253 static long probe_tuner (struct dvb_i2c_bus *i2c)
1254 {
1255         struct dvb_adapter * adapter = (struct dvb_adapter *) i2c->adapter;
1256
1257         /* read the status register of TSA5059 */
1258         u8 rpt[] = { 0x05, 0xb5 };
1259         u8 stat [] = { 0 };
1260         u8 tda6100_buf [] = { 0, 0 };
1261         int ret;
1262         struct i2c_msg msg1 [] = {{ .addr = 0x68, .flags = 0, .buf = rpt,  len: 2 },
1263                            { .addr = 0x60, .flags = I2C_M_RD, .buf = stat, .len = 1 }};
1264         struct i2c_msg msg2 [] = {{ .addr = 0x68, .flags = 0, .buf = rpt,  len: 2 },
1265                            { .addr = 0x61, .flags = I2C_M_RD, .buf = stat, .len = 1 }};
1266         struct i2c_msg msg3 [] = {{ .addr = 0x68, .flags = 0, .buf = rpt,  len: 2 },
1267                            { .addr = 0x60, .flags = 0, .buf = tda6100_buf, .len = 2 }};
1268
1269         stv0299_writereg (i2c, 0x01, 0x15);
1270         stv0299_writereg (i2c, 0x02, 0x30);
1271         stv0299_writereg (i2c, 0x03, 0x00);
1272
1273
1274         printk ("%s: try to attach to %s\n", __FUNCTION__, adapter->name);
1275
1276         if ( strcmp(adapter->name, "Technisat SkyStar2 driver") == 0 )
1277         {
1278             printk ("%s: setup for tuner Samsung TBMU24112IMB\n", __FILE__);
1279
1280             return SAMSUNG_TBMU24112IMB;
1281         }
1282
1283         if ((ret = i2c->xfer(i2c, msg1, 2)) == 2) {
1284                 if ( strcmp(adapter->name, "TT-Budget/WinTV-NOVA-CI PCI") == 0 ) {
1285                         // technotrend cards require non-datasheet settings
1286                         printk ("%s: setup for tuner SU1278 (TSA5059 synth) on"
1287                                 " TechnoTrend hardware\n", __FILE__);
1288                         return PHILIPS_SU1278_TSA_TT;
1289                 }  else {
1290                         // fall back to datasheet-recommended settings
1291                         printk ("%s: setup for tuner SU1278 (TSA5059 synth)\n",
1292                                 __FILE__);
1293                         return PHILIPS_SU1278_TSA;
1294                 }
1295                 }
1296
1297         if ((ret = i2c->xfer(i2c, msg2, 2)) == 2) {
1298                 if ( strcmp(adapter->name, "KNC1 DVB-S") == 0 &&
1299                      !disable_typhoon )
1300                 {
1301                         // Typhoon cards have unusual wiring.
1302                         printk ("%s: setup for tuner SU1278 (TSA5059 synth) on"
1303                                 " Typhoon hardware\n", __FILE__);
1304                         return PHILIPS_SU1278_TSA_TY;
1305                 }
1306                 //else if ((stat[0] & 0x3f) == 0) {
1307                 else if (0) {
1308                         printk ("%s: setup for tuner TDQF-S001F\n", __FILE__);
1309                         return LG_TDQF_S001F;
1310         } else {
1311                         printk ("%s: setup for tuner BSRU6, TDQB-S00x\n",
1312                         __FILE__);
1313                         return ALPS_BSRU6;
1314         }
1315         }
1316
1317         /**
1318          *  setup i2c timing for SU1278...
1319          */
1320         stv0299_writereg (i2c, 0x02, 0x00);
1321
1322         if ((ret = i2c->xfer(i2c, msg3, 2)) == 2) {
1323                 printk ("%s: setup for tuner Philips SU1278 (TUA6100 synth)\n",
1324                         __FILE__);
1325                 return PHILIPS_SU1278_TUA;
1326         }
1327
1328         printk ("%s: unknown PLL synthesizer (ret == %i), "
1329                 "please report to <linuxdvb@linuxtv.org>!!\n",
1330                 __FILE__, ret);
1331
1332         return UNKNOWN_FRONTEND;
1333 }
1334
1335
1336 static int uni0299_attach (struct dvb_i2c_bus *i2c, void **data)
1337 {
1338         struct stv0299_state* state;
1339         int tuner_type;
1340         u8 id;
1341  
1342         stv0299_writereg (i2c, 0x02, 0x34); /* standby off */
1343         dvb_delay(200);
1344         id = stv0299_readreg (i2c, 0x00);
1345
1346         dprintk ("%s: id == 0x%02x\n", __FUNCTION__, id);
1347
1348         /* register 0x00 contains 0xa1 for STV0299 and STV0299B */
1349         /* register 0x00 might contain 0x80 when returning from standby */
1350         if (id != 0xa1 && id != 0x80)
1351                 return -ENODEV;
1352
1353         if ((tuner_type = probe_tuner(i2c)) < 0)
1354                 return -ENODEV;
1355
1356         if ((state = kmalloc(sizeof(struct stv0299_state), GFP_KERNEL)) == NULL) {
1357                 return -ENOMEM;
1358         }
1359
1360         *data = state;
1361         state->tuner_type = tuner_type;
1362         state->tuner_frequency = 0;
1363         state->initialised = 0;
1364         return dvb_register_frontend (uni0299_ioctl, i2c, (void *) state,
1365                                &uni0299_info);
1366 }
1367
1368
1369 static void uni0299_detach (struct dvb_i2c_bus *i2c, void *data)
1370 {
1371         dprintk ("%s\n", __FUNCTION__);
1372         kfree(data);
1373         dvb_unregister_frontend (uni0299_ioctl, i2c);
1374 }
1375
1376
1377 static int __init init_uni0299 (void)
1378 {
1379         dprintk ("%s\n", __FUNCTION__);
1380         return dvb_register_i2c_device (NULL, uni0299_attach, uni0299_detach);
1381 }
1382
1383
1384 static void __exit exit_uni0299 (void)
1385 {
1386         dprintk ("%s\n", __FUNCTION__);
1387
1388         dvb_unregister_i2c_device (uni0299_attach);
1389 }
1390
1391 module_init (init_uni0299);
1392 module_exit (exit_uni0299);
1393
1394 MODULE_DESCRIPTION("Universal STV0299/TSA5059/SL1935 DVB Frontend driver");
1395 MODULE_AUTHOR("Ralph Metzler, Holger Waechtler, Peter Schildmann, Felix Domke, Andreas Oberritter, Andrew de Quincey");
1396 MODULE_LICENSE("GPL");
1397
1398 MODULE_PARM(stv0299_status, "i");
1399 MODULE_PARM_DESC(stv0299_status, "Which status value to support (0: BER, 1: UCBLOCKS)");
1400
1401 MODULE_PARM(disable_typhoon, "i");
1402 MODULE_PARM_DESC(disable_typhoon, "Disable support for Philips SU1278 on Typhoon hardware.");