2 Driver for VES1893 and VES1993 QPSK Frontends
4 Copyright (C) 1999 Convergence Integrated Media GmbH <ralph@convergence.de>
5 Copyright (C) 2001 Ronny Strutz <3des@elitedvb.de>
6 Copyright (C) 2002 Dennis Noermann <dennis.noermann@noernet.de>
7 Copyright (C) 2002-2003 Andreas Oberritter <obi@linuxtv.org>
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 #include <linux/kernel.h>
27 #include <linux/module.h>
28 #include <linux/init.h>
29 #include <linux/string.h>
30 #include <linux/slab.h>
31 #include <linux/delay.h>
33 #include "dvb_frontend.h"
36 #define dprintk if (debug) printk
38 static int board_type
= 0;
39 #define BOARD_SIEMENS_PCI 0
40 #define BOARD_NOKIA_DBOX2 1
41 #define BOARD_SAGEM_DBOX2 2
43 static int demod_type
= 0;
44 #define DEMOD_VES1893 0
45 #define DEMOD_VES1993 1
47 static struct dvb_frontend_info ves1x93_info
= {
50 .frequency_min
= 950000,
51 .frequency_max
= 2150000,
52 .frequency_stepsize
= 125, /* kHz for QPSK frontends */
53 .frequency_tolerance
= 29500,
54 .symbol_rate_min
= 1000000,
55 .symbol_rate_max
= 45000000,
56 /* .symbol_rate_tolerance = ???,*/
57 .notifier_delay
= 50, /* 1/20 s */
58 .caps
= FE_CAN_INVERSION_AUTO
|
59 FE_CAN_FEC_1_2
| FE_CAN_FEC_2_3
| FE_CAN_FEC_3_4
|
60 FE_CAN_FEC_5_6
| FE_CAN_FEC_7_8
| FE_CAN_FEC_AUTO
|
66 * nokia dbox2 (ves1893) and sagem dbox2 (ves1993)
67 * need bit AGCR[PWMS] set to 1
70 static u8 init_1893_tab
[] = {
71 0x01, 0xa4, 0x35, 0x80, 0x2a, 0x0b, 0x55, 0xc4,
72 0x09, 0x69, 0x00, 0x86, 0x4c, 0x28, 0x7f, 0x00,
73 0x00, 0x81, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
74 0x80, 0x00, 0x21, 0xb0, 0x14, 0x00, 0xdc, 0x00,
75 0x81, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
76 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
77 0x00, 0x55, 0x00, 0x00, 0x7f, 0x00
81 static u8 init_1993_tab
[] = {
82 0x00, 0x9c, 0x35, 0x80, 0x6a, 0x09, 0x72, 0x8c,
83 0x09, 0x6b, 0x00, 0x00, 0x4c, 0x08, 0x00, 0x00,
84 0x00, 0x81, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
85 0x80, 0x40, 0x21, 0xb0, 0x00, 0x00, 0x00, 0x10,
86 0x81, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
87 0x00, 0x00, 0x80, 0x80, 0x00, 0x00, 0x00, 0x00,
88 0x00, 0x55, 0x03, 0x00, 0x00, 0x00, 0x00, 0x03,
89 0x00, 0x00, 0x0e, 0x80, 0x00
93 static u8
* init_1x93_tab
;
96 static u8 init_1893_wtab
[] =
98 1,1,1,1,1,1,1,1, 1,1,0,0,1,1,0,0,
99 0,1,0,0,0,0,0,0, 1,0,1,1,0,0,0,1,
100 1,1,1,0,0,0,0,0, 0,0,1,1,0,0,0,0,
105 static u8 init_1993_wtab
[] =
107 1,1,1,1,1,1,1,1, 1,1,0,0,1,1,0,0,
108 0,1,0,0,0,0,0,0, 1,1,1,1,0,0,0,1,
109 1,1,1,0,0,0,0,0, 0,0,1,1,0,0,0,0,
110 1,1,1,0,1,1,1,1, 1,1,1,1,1
113 struct ves1x93_state
{
114 fe_spectral_inversion_t inversion
;
115 struct i2c_adapter
*i2c
;
116 struct dvb_adapter
*dvb
;
121 static int ves1x93_writereg (struct i2c_adapter
*i2c
, u8 reg
, u8 data
)
123 u8 buf
[] = { 0x00, reg
, data
};
124 struct i2c_msg msg
= { .addr
= 0x08, .flags
= 0, .buf
= buf
, .len
= 3 };
127 if ((err
= i2c_transfer (i2c
, &msg
, 1)) != 1) {
128 dprintk ("%s: writereg error (err == %i, reg == 0x%02x, data == 0x%02x)\n", __FUNCTION__
, err
, reg
, data
);
136 static u8
ves1x93_readreg (struct i2c_adapter
*i2c
, u8 reg
)
139 u8 b0
[] = { 0x00, reg
};
141 struct i2c_msg msg
[] = { { .addr
= 0x08, .flags
= 0, .buf
= b0
, .len
= 2 },
142 { .addr
= 0x08, .flags
= I2C_M_RD
, .buf
= b1
, .len
= 1 } };
144 ret
= i2c_transfer (i2c
, msg
, 2);
147 dprintk("%s: readreg error (ret == %i)\n", __FUNCTION__
, ret
);
153 static int tuner_write (struct i2c_adapter
*i2c
, u8
*data
, u8 len
)
156 struct i2c_msg msg
= { .addr
= 0x61, .flags
= 0, .buf
= data
, .len
= len
};
158 ves1x93_writereg(i2c
, 0x00, 0x11);
159 ret
= i2c_transfer (i2c
, &msg
, 1);
160 ves1x93_writereg(i2c
, 0x00, 0x01);
163 printk("%s: i/o error (ret == %i)\n", __FUNCTION__
, ret
);
165 return (ret
!= 1) ? -1 : 0;
171 * set up the downconverter frequency divisor for a
172 * reference clock comparision frequency of 125 kHz.
174 static int sp5659_set_tv_freq (struct i2c_adapter
*i2c
, u32 freq
)
178 u32 div
= (freq
+ 479500) / 125;
180 if (freq
> 2000000) pwr
= 3;
181 else if (freq
> 1800000) pwr
= 2;
182 else if (freq
> 1600000) pwr
= 1;
183 else if (freq
> 1200000) pwr
= 0;
184 else if (freq
>= 1100000) pwr
= 1;
187 buf
[0] = (div
>> 8) & 0x7f;
189 buf
[2] = ((div
& 0x18000) >> 10) | 0x95;
190 buf
[3] = (pwr
<< 6) | 0x30;
192 // NOTE: since we're using a prescaler of 2, we set the
193 // divisor frequency to 62.5kHz and divide by 125 above
195 return tuner_write (i2c
, buf
, sizeof(buf
));
199 static int tsa5059_set_tv_freq (struct i2c_adapter
*i2c
, u32 freq
)
206 buf
[0] = (freq
>> 8) & 0x7F;
207 buf
[1] = freq
& 0xFF;
209 ret
= tuner_write(i2c
, buf
, sizeof(buf
));
215 static int tuner_set_tv_freq (struct i2c_adapter
*i2c
, u32 freq
)
217 if ((demod_type
== DEMOD_VES1893
) && (board_type
== BOARD_SIEMENS_PCI
))
218 return sp5659_set_tv_freq (i2c
, freq
);
219 else if (demod_type
== DEMOD_VES1993
)
220 return tsa5059_set_tv_freq (i2c
, freq
);
226 static int ves1x93_init (struct i2c_adapter
*i2c
)
232 dprintk("%s: init chip\n", __FUNCTION__
);
234 switch (demod_type
) {
236 init_1x93_tab
= init_1893_tab
;
237 init_1x93_wtab
= init_1893_wtab
;
238 size
= sizeof(init_1893_tab
);
239 if (board_type
== BOARD_NOKIA_DBOX2
)
240 init_1x93_tab
[0x05] |= 0x20; /* invert PWM */
244 init_1x93_tab
= init_1993_tab
;
245 init_1x93_wtab
= init_1993_wtab
;
246 size
= sizeof(init_1993_tab
);
247 if (board_type
== BOARD_SAGEM_DBOX2
)
248 init_1x93_tab
[0x05] |= 0x20; /* invert PWM */
255 for (i
= 0; i
< size
; i
++)
256 if (init_1x93_wtab
[i
])
257 ves1x93_writereg (i2c
, i
, init_1x93_tab
[i
]);
259 if (demod_type
== DEMOD_VES1993
) {
260 if (board_type
== BOARD_NOKIA_DBOX2
)
261 tuner_write(i2c
, "\x06\x5c\x83\x60", 4);
262 else if (board_type
== BOARD_SAGEM_DBOX2
)
263 tuner_write(i2c
, "\x25\x70\x92\x40", 4);
270 static int ves1x93_clr_bit (struct i2c_adapter
*i2c
)
273 ves1x93_writereg (i2c
, 0, init_1x93_tab
[0] & 0xfe);
274 ves1x93_writereg (i2c
, 0, init_1x93_tab
[0]);
280 static int ves1x93_set_inversion (struct i2c_adapter
*i2c
, fe_spectral_inversion_t inversion
)
285 * inversion on/off are interchanged because i and q seem to
286 * be swapped on the hardware
303 return ves1x93_writereg (i2c
, 0x0c, (init_1x93_tab
[0x0c] & 0x3f) | val
);
307 static int ves1x93_set_fec (struct i2c_adapter
*i2c
, fe_code_rate_t fec
)
310 return ves1x93_writereg (i2c
, 0x0d, 0x08);
311 else if (fec
< FEC_1_2
|| fec
> FEC_8_9
)
314 return ves1x93_writereg (i2c
, 0x0d, fec
- FEC_1_2
);
318 static fe_code_rate_t
ves1x93_get_fec (struct i2c_adapter
*i2c
)
320 return FEC_1_2
+ ((ves1x93_readreg (i2c
, 0x0d) >> 4) & 0x7);
324 static int ves1x93_set_symbolrate (struct i2c_adapter
*i2c
, u32 srate
)
328 u8 ADCONF
, FCONF
, FNR
;
333 dprintk("%s: srate == %d\n", __FUNCTION__
, (unsigned int) srate
);
335 switch (board_type
) {
336 case BOARD_SIEMENS_PCI
:
339 case BOARD_NOKIA_DBOX2
:
340 if (demod_type
== DEMOD_VES1893
)
342 else if (demod_type
== DEMOD_VES1993
)
347 case BOARD_SAGEM_DBOX2
:
360 #define MUL (1UL<<26)
362 FIN
= (XIN
+ 6000) >> 4;
367 tmp
= (tmp
% FIN
) << 8;
368 ratio
= (ratio
<< 8) + tmp
/ FIN
;
370 tmp
= (tmp
% FIN
) << 8;
371 ratio
= (ratio
<< 8) + tmp
/ FIN
;
375 if (ratio
< MUL
/3) FNR
= 0;
376 if (ratio
< (MUL
*11)/50) FNR
= 1;
377 if (ratio
< MUL
/6) FNR
= 2;
378 if (ratio
< MUL
/9) FNR
= 3;
379 if (ratio
< MUL
/12) FNR
= 4;
380 if (ratio
< (MUL
*11)/200) FNR
= 5;
381 if (ratio
< MUL
/24) FNR
= 6;
382 if (ratio
< (MUL
*27)/1000) FNR
= 7;
383 if (ratio
< MUL
/48) FNR
= 8;
384 if (ratio
< (MUL
*137)/10000) FNR
= 9;
392 FCONF
= 0x88 | (FNR
>> 1) | ((FNR
& 0x01) << 5);
393 /*FCONF = 0x80 | ((FNR & 0x01) << 5) | (((FNR > 1) & 0x03) << 3) | ((FNR >> 1) & 0x07);*/
396 BDR
= (( (ratio
<< (FNR
>> 1)) >> 4) + 1) >> 1;
397 BDRI
= ( ((FIN
<< 8) / ((srate
<< (FNR
>> 1)) >> 2)) + 1) >> 1;
399 dprintk("FNR= %d\n", FNR
);
400 dprintk("ratio= %08x\n", (unsigned int) ratio
);
401 dprintk("BDR= %08x\n", (unsigned int) BDR
);
402 dprintk("BDRI= %02x\n", (unsigned int) BDRI
);
407 ves1x93_writereg (i2c
, 0x06, 0xff & BDR
);
408 ves1x93_writereg (i2c
, 0x07, 0xff & (BDR
>> 8));
409 ves1x93_writereg (i2c
, 0x08, 0x0f & (BDR
>> 16));
411 ves1x93_writereg (i2c
, 0x09, BDRI
);
412 ves1x93_writereg (i2c
, 0x20, ADCONF
);
413 ves1x93_writereg (i2c
, 0x21, FCONF
);
416 ves1x93_writereg (i2c
, 0x05, init_1x93_tab
[0x05] | 0x80);
418 ves1x93_writereg (i2c
, 0x05, init_1x93_tab
[0x05] & 0x7f);
420 /* ves1993 hates this, will lose lock */
421 if (demod_type
!= DEMOD_VES1993
)
422 ves1x93_clr_bit (i2c
);
427 static int ves1x93_set_voltage (struct i2c_adapter
*i2c
, fe_sec_voltage_t voltage
)
431 return ves1x93_writereg (i2c
, 0x1f, 0x20);
433 return ves1x93_writereg (i2c
, 0x1f, 0x30);
434 case SEC_VOLTAGE_OFF
:
435 return ves1x93_writereg (i2c
, 0x1f, 0x00);
442 static int ves1x93_ioctl (struct dvb_frontend
*fe
, unsigned int cmd
, void *arg
)
444 struct ves1x93_state
*state
= (struct ves1x93_state
*) fe
->data
;
445 struct i2c_adapter
*i2c
= state
->i2c
;
449 memcpy (arg
, &ves1x93_info
, sizeof(struct dvb_frontend_info
));
454 fe_status_t
*status
= arg
;
455 u8 sync
= ves1x93_readreg (i2c
, 0x0e);
458 * The ves1893 sometimes returns sync values that make no sense,
459 * because, e.g., the SIGNAL bit is 0, while some of the higher
460 * bits are 1 (and how can there be a CARRIER w/o a SIGNAL?).
461 * Tests showed that the the VITERBI and SYNC bits are returned
462 * reliably, while the SIGNAL and CARRIER bits ar sometimes wrong.
463 * If such a case occurs, we read the value again, until we get a
466 int maxtry
= 10; /* just for safety - let's not get stuck here */
467 while ((sync
& 0x03) != 0x03 && (sync
& 0x0c) && maxtry
--) {
469 sync
= ves1x93_readreg (i2c
, 0x0e);
475 *status
|= FE_HAS_SIGNAL
;
478 *status
|= FE_HAS_CARRIER
;
481 *status
|= FE_HAS_VITERBI
;
484 *status
|= FE_HAS_SYNC
;
486 if ((sync
& 0x1f) == 0x1f)
487 *status
|= FE_HAS_LOCK
;
494 u32
*ber
= (u32
*) arg
;
496 *ber
= ves1x93_readreg (i2c
, 0x15);
497 *ber
|= (ves1x93_readreg (i2c
, 0x16) << 8);
498 *ber
|= ((ves1x93_readreg (i2c
, 0x17) & 0x0F) << 16);
503 case FE_READ_SIGNAL_STRENGTH
:
505 u8 signal
= ~ves1x93_readreg (i2c
, 0x0b);
506 *((u16
*) arg
) = (signal
<< 8) | signal
;
512 u8 snr
= ~ves1x93_readreg (i2c
, 0x1c);
513 *(u16
*) arg
= (snr
<< 8) | snr
;
517 case FE_READ_UNCORRECTED_BLOCKS
:
519 *(u32
*) arg
= ves1x93_readreg (i2c
, 0x18) & 0x7f;
521 if (*(u32
*) arg
== 0x7f)
522 *(u32
*) arg
= 0xffffffff; /* counter overflow... */
524 ves1x93_writereg (i2c
, 0x18, 0x00); /* reset the counter */
525 ves1x93_writereg (i2c
, 0x18, 0x80); /* dto. */
529 case FE_SET_FRONTEND
:
531 struct dvb_frontend_parameters
*p
= arg
;
533 tuner_set_tv_freq (i2c
, p
->frequency
);
534 ves1x93_set_inversion (i2c
, p
->inversion
);
535 ves1x93_set_fec (i2c
, p
->u
.qpsk
.fec_inner
);
536 ves1x93_set_symbolrate (i2c
, p
->u
.qpsk
.symbol_rate
);
537 state
->inversion
= p
->inversion
;
541 case FE_GET_FRONTEND
:
543 struct dvb_frontend_parameters
*p
= arg
;
546 afc
= ((int)((char)(ves1x93_readreg (i2c
, 0x0a) << 1)))/2;
547 afc
= (afc
* (int)(p
->u
.qpsk
.symbol_rate
/1000/8))/16;
552 * inversion indicator is only valid
553 * if auto inversion was used
555 if (state
->inversion
== INVERSION_AUTO
)
556 p
->inversion
= (ves1x93_readreg (i2c
, 0x0f) & 2) ?
557 INVERSION_OFF
: INVERSION_ON
;
558 p
->u
.qpsk
.fec_inner
= ves1x93_get_fec (i2c
);
559 /* XXX FIXME: timing offset !! */
564 if (board_type
== BOARD_SIEMENS_PCI
)
565 ves1x93_writereg (i2c
, 0x1f, 0x00); /* LNB power off */
566 return ves1x93_writereg (i2c
, 0x00, 0x08);
569 return ves1x93_init (i2c
);
572 return -EOPNOTSUPP
; /* the ves1893 can generate the 22k */
573 /* let's implement this when we have */
574 /* a box that uses the 22K_0 pin... */
577 return ves1x93_set_voltage (i2c
, (fe_sec_voltage_t
) arg
);
586 static struct i2c_client client_template
;
588 static int attach_adapter(struct i2c_adapter
*adapter
)
590 struct i2c_client
*client
;
591 struct ves1x93_state
* state
;
592 u8 identity
= ves1x93_readreg(adapter
, 0x1e);
596 case 0xdc: /* VES1893A rev1 */
597 printk("ves1x93: Detected ves1893a rev1\n");
598 demod_type
= DEMOD_VES1893
;
599 ves1x93_info
.name
[4] = '8';
601 case 0xdd: /* VES1893A rev2 */
602 printk("ves1x93: Detected ves1893a rev2\n");
603 demod_type
= DEMOD_VES1893
;
604 ves1x93_info
.name
[4] = '8';
606 case 0xde: /* VES1993 */
607 printk("ves1x93: Detected ves1993\n");
608 demod_type
= DEMOD_VES1993
;
609 ves1x93_info
.name
[4] = '9';
612 dprintk("VES1x93 not found (identity %02x)\n", identity
);
616 if ((state
= kmalloc(sizeof(struct ves1x93_state
), GFP_KERNEL
)) == NULL
) {
620 if (NULL
== (client
= kmalloc(sizeof(struct i2c_client
), GFP_KERNEL
))) {
625 state
->inversion
= INVERSION_OFF
;
626 state
->i2c
= adapter
;
628 memcpy(client
, &client_template
, sizeof(struct i2c_client
));
629 client
->adapter
= adapter
;
630 client
->addr
= (0x08>>1);
631 i2c_set_clientdata(client
, (void*)state
);
633 ret
= i2c_attach_client(client
);
642 ret
= dvb_register_frontend(ves1x93_ioctl
, state
->dvb
, state
,
643 &ves1x93_info
, THIS_MODULE
);
645 i2c_detach_client(client
);
654 static int detach_client(struct i2c_client
*client
)
656 struct ves1x93_state
*state
= (struct ves1x93_state
*)i2c_get_clientdata(client
);
657 dvb_unregister_frontend(ves1x93_ioctl
, state
->dvb
);
658 i2c_detach_client(client
);
665 static int command (struct i2c_client
*client
, unsigned int cmd
, void *arg
)
667 struct ves1x93_state
*state
= (struct ves1x93_state
*)i2c_get_clientdata(client
);
669 dprintk ("%s\n", __FUNCTION__
);
673 state
->dvb
= (struct dvb_adapter
*)arg
;
676 case FE_UNREGISTER
: {
686 static struct i2c_driver driver
= {
687 .owner
= THIS_MODULE
,
689 .id
= I2C_DRIVERID_DVBFE_VES1X93
,
690 .flags
= I2C_DF_NOTIFY
,
691 .attach_adapter
= attach_adapter
,
692 .detach_client
= detach_client
,
696 static struct i2c_client client_template
= {
697 I2C_DEVNAME("ves1x93"),
698 .flags
= I2C_CLIENT_ALLOW_USE
,
702 static int __init
init_ves1x93 (void)
704 switch (board_type
) {
705 case BOARD_NOKIA_DBOX2
:
706 dprintk("%s: NOKIA_DBOX2\n", __FILE__
);
708 case BOARD_SAGEM_DBOX2
:
709 dprintk("%s: SAGEM_DBOX2\n", __FILE__
);
711 case BOARD_SIEMENS_PCI
:
712 dprintk("%s: SIEMENS_PCI\n", __FILE__
);
718 return i2c_add_driver(&driver
);
722 static void __exit
exit_ves1x93 (void)
724 if (i2c_del_driver(&driver
))
725 printk("vex1x93: driver deregistration failed\n");
728 module_init(init_ves1x93
);
729 module_exit(exit_ves1x93
);
732 MODULE_DESCRIPTION("VES1x93 DVB-S Frontend");
733 MODULE_AUTHOR("Ralph Metzler");
734 MODULE_LICENSE("GPL");
735 MODULE_PARM(debug
,"i");
736 MODULE_PARM(board_type
,"i");