1 // SPDX-License-Identifier: GPL-2.0-or-later
3 STB0899 Multistandard Frontend driver
4 Copyright (C) Manu Abraham (abraham.manu@gmail.com)
6 Copyright (C) ST Microelectronics
10 #include <linux/init.h>
11 #include <linux/jiffies.h>
12 #include <linux/kernel.h>
13 #include <linux/module.h>
14 #include <linux/slab.h>
15 #include <linux/string.h>
17 #include <linux/dvb/frontend.h>
18 #include <media/dvb_frontend.h>
20 #include "stb0899_drv.h"
21 #include "stb0899_priv.h"
22 #include "stb0899_reg.h"
24 /* Max transfer size done by I2C transfer functions */
25 #define MAX_XFER_SIZE 64
27 static unsigned int verbose
= 0;//1;
28 module_param(verbose
, int, 0644);
30 /* C/N in dB/10, NIRM/NIRL */
31 static const struct stb0899_tab stb0899_cn_tab
[] = {
54 /* DVB-S AGCIQ_VALUE vs. signal level in dBm/10.
55 * As measured, connected to a modulator.
56 * -8.0 to -50.0 dBm directly connected,
57 * -52.0 to -74.8 with extra attenuation.
58 * Cut-off to AGCIQ_VALUE = 0x80 below -74.8dBm.
59 * Crude linear extrapolation below -84.8dBm and above -8.0dBm.
61 static const struct stb0899_tab stb0899_dvbsrf_tab
[] = {
95 /* DVB-S2 IF_AGC_GAIN vs. signal level in dBm/10.
96 * As measured, connected to a modulator.
97 * -8.0 to -50.1 dBm directly connected,
98 * -53.0 to -76.6 with extra attenuation.
99 * Cut-off to IF_AGC_GAIN = 0x3fff below -76.6dBm.
100 * Crude linear extrapolation below -76.6dBm and above -8.0dBm.
102 static const struct stb0899_tab stb0899_dvbs2rf_tab
[] = {
129 /* DVB-S2 Es/N0 quant in dB/100 vs read value * 100*/
130 static struct stb0899_tab stb0899_quant_tab
[] = {
172 /* DVB-S2 Es/N0 estimate in dB/100 vs read value */
173 static struct stb0899_tab stb0899_est_tab
[] = {
213 static int _stb0899_read_reg(struct stb0899_state
*state
, unsigned int reg
)
217 u8 b0
[] = { reg
>> 8, reg
& 0xff };
220 struct i2c_msg msg
[] = {
222 .addr
= state
->config
->demod_address
,
227 .addr
= state
->config
->demod_address
,
234 ret
= i2c_transfer(state
->i2c
, msg
, 2);
236 if (ret
!= -ERESTARTSYS
)
237 dprintk(state
->verbose
, FE_ERROR
, 1,
238 "Read error, Reg=[0x%02x], Status=%d",
241 return ret
< 0 ? ret
: -EREMOTEIO
;
243 if (unlikely(*state
->verbose
>= FE_DEBUGREG
))
244 dprintk(state
->verbose
, FE_ERROR
, 1, "Reg=[0x%02x], data=%02x",
247 return (unsigned int)buf
;
250 int stb0899_read_reg(struct stb0899_state
*state
, unsigned int reg
)
254 result
= _stb0899_read_reg(state
, reg
);
257 * access to 0xf2xx/0xf6xx
258 * must be followed by read from 0xf2ff/0xf6ff.
260 if ((reg
!= 0xf2ff) && (reg
!= 0xf6ff) &&
261 (((reg
& 0xff00) == 0xf200) || ((reg
& 0xff00) == 0xf600)))
262 _stb0899_read_reg(state
, (reg
| 0x00ff));
267 u32
_stb0899_read_s2reg(struct stb0899_state
*state
,
269 u32 stb0899_base_addr
,
270 u16 stb0899_reg_offset
)
278 GETBYTE(stb0899_i2cdev
, BYTE1
), /* 0xf3 S2 Base Address (MSB) */
279 GETBYTE(stb0899_i2cdev
, BYTE0
), /* 0xfc S2 Base Address (LSB) */
280 GETBYTE(stb0899_base_addr
, BYTE0
), /* 0x00 Base Address (LSB) */
281 GETBYTE(stb0899_base_addr
, BYTE1
), /* 0x04 Base Address (LSB) */
282 GETBYTE(stb0899_base_addr
, BYTE2
), /* 0x00 Base Address (MSB) */
283 GETBYTE(stb0899_base_addr
, BYTE3
), /* 0x00 Base Address (MSB) */
286 0x00, /* 0xf3 Reg Offset */
287 0x00, /* 0x44 Reg Offset */
290 struct i2c_msg msg_0
= {
291 .addr
= state
->config
->demod_address
,
297 struct i2c_msg msg_1
= {
298 .addr
= state
->config
->demod_address
,
304 struct i2c_msg msg_r
= {
305 .addr
= state
->config
->demod_address
,
311 tmpaddr
= stb0899_reg_offset
& 0xff00;
312 if (!(stb0899_reg_offset
& 0x8))
313 tmpaddr
= stb0899_reg_offset
| 0x20;
315 buf_1
[0] = GETBYTE(tmpaddr
, BYTE1
);
316 buf_1
[1] = GETBYTE(tmpaddr
, BYTE0
);
318 status
= i2c_transfer(state
->i2c
, &msg_0
, 1);
320 if (status
!= -ERESTARTSYS
)
321 printk(KERN_ERR
"%s ERR(1), Device=[0x%04x], Base address=[0x%08x], Offset=[0x%04x], Status=%d\n",
322 __func__
, stb0899_i2cdev
, stb0899_base_addr
, stb0899_reg_offset
, status
);
328 status
= i2c_transfer(state
->i2c
, &msg_1
, 1);
332 status
= i2c_transfer(state
->i2c
, &msg_r
, 1);
336 buf_1
[0] = GETBYTE(stb0899_reg_offset
, BYTE1
);
337 buf_1
[1] = GETBYTE(stb0899_reg_offset
, BYTE0
);
340 status
= i2c_transfer(state
->i2c
, &msg_1
, 1);
342 if (status
!= -ERESTARTSYS
)
343 printk(KERN_ERR
"%s ERR(2), Device=[0x%04x], Base address=[0x%08x], Offset=[0x%04x], Status=%d\n",
344 __func__
, stb0899_i2cdev
, stb0899_base_addr
, stb0899_reg_offset
, status
);
348 status
= i2c_transfer(state
->i2c
, &msg_r
, 1);
350 if (status
!= -ERESTARTSYS
)
351 printk(KERN_ERR
"%s ERR(3), Device=[0x%04x], Base address=[0x%08x], Offset=[0x%04x], Status=%d\n",
352 __func__
, stb0899_i2cdev
, stb0899_base_addr
, stb0899_reg_offset
, status
);
353 return status
< 0 ? status
: -EREMOTEIO
;
356 data
= MAKEWORD32(buf
[3], buf
[2], buf
[1], buf
[0]);
357 if (unlikely(*state
->verbose
>= FE_DEBUGREG
))
358 printk(KERN_DEBUG
"%s Device=[0x%04x], Base address=[0x%08x], Offset=[0x%04x], Data=[0x%08x]\n",
359 __func__
, stb0899_i2cdev
, stb0899_base_addr
, stb0899_reg_offset
, data
);
364 return status
< 0 ? status
: -EREMOTEIO
;
367 int stb0899_write_s2reg(struct stb0899_state
*state
,
369 u32 stb0899_base_addr
,
370 u16 stb0899_reg_offset
,
375 /* Base Address Setup */
377 GETBYTE(stb0899_i2cdev
, BYTE1
), /* 0xf3 S2 Base Address (MSB) */
378 GETBYTE(stb0899_i2cdev
, BYTE0
), /* 0xfc S2 Base Address (LSB) */
379 GETBYTE(stb0899_base_addr
, BYTE0
), /* 0x00 Base Address (LSB) */
380 GETBYTE(stb0899_base_addr
, BYTE1
), /* 0x04 Base Address (LSB) */
381 GETBYTE(stb0899_base_addr
, BYTE2
), /* 0x00 Base Address (MSB) */
382 GETBYTE(stb0899_base_addr
, BYTE3
), /* 0x00 Base Address (MSB) */
385 0x00, /* 0xf3 Reg Offset */
386 0x00, /* 0x44 Reg Offset */
393 struct i2c_msg msg_0
= {
394 .addr
= state
->config
->demod_address
,
400 struct i2c_msg msg_1
= {
401 .addr
= state
->config
->demod_address
,
407 buf_1
[0] = GETBYTE(stb0899_reg_offset
, BYTE1
);
408 buf_1
[1] = GETBYTE(stb0899_reg_offset
, BYTE0
);
409 buf_1
[2] = GETBYTE(stb0899_data
, BYTE0
);
410 buf_1
[3] = GETBYTE(stb0899_data
, BYTE1
);
411 buf_1
[4] = GETBYTE(stb0899_data
, BYTE2
);
412 buf_1
[5] = GETBYTE(stb0899_data
, BYTE3
);
414 if (unlikely(*state
->verbose
>= FE_DEBUGREG
))
415 printk(KERN_DEBUG
"%s Device=[0x%04x], Base Address=[0x%08x], Offset=[0x%04x], Data=[0x%08x]\n",
416 __func__
, stb0899_i2cdev
, stb0899_base_addr
, stb0899_reg_offset
, stb0899_data
);
418 status
= i2c_transfer(state
->i2c
, &msg_0
, 1);
419 if (unlikely(status
< 1)) {
420 if (status
!= -ERESTARTSYS
)
421 printk(KERN_ERR
"%s ERR (1), Device=[0x%04x], Base Address=[0x%08x], Offset=[0x%04x], Data=[0x%08x], status=%d\n",
422 __func__
, stb0899_i2cdev
, stb0899_base_addr
, stb0899_reg_offset
, stb0899_data
, status
);
425 status
= i2c_transfer(state
->i2c
, &msg_1
, 1);
426 if (unlikely(status
< 1)) {
427 if (status
!= -ERESTARTSYS
)
428 printk(KERN_ERR
"%s ERR (2), Device=[0x%04x], Base Address=[0x%08x], Offset=[0x%04x], Data=[0x%08x], status=%d\n",
429 __func__
, stb0899_i2cdev
, stb0899_base_addr
, stb0899_reg_offset
, stb0899_data
, status
);
431 return status
< 0 ? status
: -EREMOTEIO
;
437 return status
< 0 ? status
: -EREMOTEIO
;
440 int stb0899_read_regs(struct stb0899_state
*state
, unsigned int reg
, u8
*buf
, u32 count
)
444 u8 b0
[] = { reg
>> 8, reg
& 0xff };
446 struct i2c_msg msg
[] = {
448 .addr
= state
->config
->demod_address
,
453 .addr
= state
->config
->demod_address
,
460 status
= i2c_transfer(state
->i2c
, msg
, 2);
462 if (status
!= -ERESTARTSYS
)
463 printk(KERN_ERR
"%s Read error, Reg=[0x%04x], Count=%u, Status=%d\n",
464 __func__
, reg
, count
, status
);
469 * access to 0xf2xx/0xf6xx
470 * must be followed by read from 0xf2ff/0xf6ff.
472 if ((reg
!= 0xf2ff) && (reg
!= 0xf6ff) &&
473 (((reg
& 0xff00) == 0xf200) || ((reg
& 0xff00) == 0xf600)))
474 _stb0899_read_reg(state
, (reg
| 0x00ff));
476 dprintk(state
->verbose
, FE_DEBUGREG
, 1,
477 "%s [0x%04x]: %*ph", __func__
, reg
, count
, buf
);
481 return status
< 0 ? status
: -EREMOTEIO
;
484 int stb0899_write_regs(struct stb0899_state
*state
, unsigned int reg
, u8
*data
, u32 count
)
487 u8 buf
[MAX_XFER_SIZE
];
488 struct i2c_msg i2c_msg
= {
489 .addr
= state
->config
->demod_address
,
495 if (2 + count
> sizeof(buf
)) {
497 "%s: i2c wr reg=%04x: len=%d is too big!\n",
498 KBUILD_MODNAME
, reg
, count
);
504 memcpy(&buf
[2], data
, count
);
506 dprintk(state
->verbose
, FE_DEBUGREG
, 1,
507 "%s [0x%04x]: %*ph", __func__
, reg
, count
, data
);
508 ret
= i2c_transfer(state
->i2c
, &i2c_msg
, 1);
512 * access to 0xf2xx/0xf6xx
513 * must be followed by read from 0xf2ff/0xf6ff.
515 if ((((reg
& 0xff00) == 0xf200) || ((reg
& 0xff00) == 0xf600)))
516 stb0899_read_reg(state
, (reg
| 0x00ff));
519 if (ret
!= -ERESTARTSYS
)
520 dprintk(state
->verbose
, FE_ERROR
, 1, "Reg=[0x%04x], Data=[0x%02x ...], Count=%u, Status=%d",
521 reg
, data
[0], count
, ret
);
522 return ret
< 0 ? ret
: -EREMOTEIO
;
528 int stb0899_write_reg(struct stb0899_state
*state
, unsigned int reg
, u8 data
)
531 return stb0899_write_regs(state
, reg
, &tmp
, 1);
536 * Get STB0899 master clock frequency
537 * ExtClk: external clock frequency (Hz)
539 static u32
stb0899_get_mclk(struct stb0899_state
*state
)
541 u32 mclk
= 0, div
= 0;
543 div
= stb0899_read_reg(state
, STB0899_NCOARSE
);
544 mclk
= (div
+ 1) * state
->config
->xtal_freq
/ 6;
545 dprintk(state
->verbose
, FE_DEBUG
, 1, "div=%d, mclk=%d", div
, mclk
);
552 * Set STB0899 master Clock frequency
553 * Mclk: demodulator master clock
554 * ExtClk: external clock frequency (Hz)
556 static void stb0899_set_mclk(struct stb0899_state
*state
, u32 Mclk
)
558 struct stb0899_internal
*internal
= &state
->internal
;
561 dprintk(state
->verbose
, FE_DEBUG
, 1, "state->config=%p", state
->config
);
562 mdiv
= ((6 * Mclk
) / state
->config
->xtal_freq
) - 1;
563 dprintk(state
->verbose
, FE_DEBUG
, 1, "mdiv=%d", mdiv
);
565 stb0899_write_reg(state
, STB0899_NCOARSE
, mdiv
);
566 internal
->master_clk
= stb0899_get_mclk(state
);
568 dprintk(state
->verbose
, FE_DEBUG
, 1, "MasterCLOCK=%d", internal
->master_clk
);
571 static int stb0899_postproc(struct stb0899_state
*state
, u8 ctl
, int enable
)
573 struct stb0899_config
*config
= state
->config
;
574 const struct stb0899_postproc
*postproc
= config
->postproc
;
576 /* post process event */
579 if (postproc
[ctl
].level
== STB0899_GPIOPULLUP
)
580 stb0899_write_reg(state
, postproc
[ctl
].gpio
, 0x02);
582 stb0899_write_reg(state
, postproc
[ctl
].gpio
, 0x82);
584 if (postproc
[ctl
].level
== STB0899_GPIOPULLUP
)
585 stb0899_write_reg(state
, postproc
[ctl
].gpio
, 0x82);
587 stb0899_write_reg(state
, postproc
[ctl
].gpio
, 0x02);
593 static void stb0899_detach(struct dvb_frontend
*fe
)
595 struct stb0899_state
*state
= fe
->demodulator_priv
;
597 /* post process event */
598 stb0899_postproc(state
, STB0899_POSTPROC_GPIO_POWER
, 0);
601 static void stb0899_release(struct dvb_frontend
*fe
)
603 struct stb0899_state
*state
= fe
->demodulator_priv
;
605 dprintk(state
->verbose
, FE_DEBUG
, 1, "Release Frontend");
613 static int stb0899_get_alpha(struct stb0899_state
*state
)
617 mode_coeff
= stb0899_read_reg(state
, STB0899_DEMOD
);
619 if (STB0899_GETFIELD(MODECOEFF
, mode_coeff
) == 1)
628 static void stb0899_init_calc(struct stb0899_state
*state
)
630 struct stb0899_internal
*internal
= &state
->internal
;
635 /* Read registers (in burst mode) */
636 stb0899_read_regs(state
, STB0899_AGC1REF
, agc
, 2); /* AGC1R and AGC2O */
638 /* Initial calculations */
639 master_clk
= stb0899_get_mclk(state
);
640 internal
->t_agc1
= 0;
641 internal
->t_agc2
= 0;
642 internal
->master_clk
= master_clk
;
643 internal
->mclk
= master_clk
/ 65536L;
644 internal
->rolloff
= stb0899_get_alpha(state
);
646 /* DVBS2 Initial calculations */
647 /* Set AGC value to the middle */
648 internal
->agc_gain
= 8154;
649 reg
= STB0899_READ_S2REG(STB0899_S2DEMOD
, IF_AGC_CNTRL
);
650 STB0899_SETFIELD_VAL(IF_GAIN_INIT
, reg
, internal
->agc_gain
);
651 stb0899_write_s2reg(state
, STB0899_S2DEMOD
, STB0899_BASE_IF_AGC_CNTRL
, STB0899_OFF0_IF_AGC_CNTRL
, reg
);
653 reg
= STB0899_READ_S2REG(STB0899_S2DEMOD
, RRC_ALPHA
);
654 internal
->rrc_alpha
= STB0899_GETFIELD(RRC_ALPHA
, reg
);
656 internal
->center_freq
= 0;
657 internal
->av_frame_coarse
= 10;
658 internal
->av_frame_fine
= 20;
659 internal
->step_size
= 2;
661 if ((pParams->SpectralInv == FE_IQ_NORMAL) || (pParams->SpectralInv == FE_IQ_AUTO))
662 pParams->IQLocked = 0;
664 pParams->IQLocked = 1;
668 static int stb0899_wait_diseqc_fifo_empty(struct stb0899_state
*state
, int timeout
)
671 unsigned long start
= jiffies
;
674 reg
= stb0899_read_reg(state
, STB0899_DISSTATUS
);
675 if (!STB0899_GETFIELD(FIFOFULL
, reg
))
677 if (time_after(jiffies
, start
+ timeout
)) {
678 dprintk(state
->verbose
, FE_ERROR
, 1, "timed out !!");
686 static int stb0899_send_diseqc_msg(struct dvb_frontend
*fe
, struct dvb_diseqc_master_cmd
*cmd
)
688 struct stb0899_state
*state
= fe
->demodulator_priv
;
691 if (cmd
->msg_len
> sizeof(cmd
->msg
))
694 /* enable FIFO precharge */
695 reg
= stb0899_read_reg(state
, STB0899_DISCNTRL1
);
696 STB0899_SETFIELD_VAL(DISPRECHARGE
, reg
, 1);
697 stb0899_write_reg(state
, STB0899_DISCNTRL1
, reg
);
698 for (i
= 0; i
< cmd
->msg_len
; i
++) {
699 /* wait for FIFO empty */
700 if (stb0899_wait_diseqc_fifo_empty(state
, 100) < 0)
703 stb0899_write_reg(state
, STB0899_DISFIFO
, cmd
->msg
[i
]);
705 reg
= stb0899_read_reg(state
, STB0899_DISCNTRL1
);
706 STB0899_SETFIELD_VAL(DISPRECHARGE
, reg
, 0);
707 stb0899_write_reg(state
, STB0899_DISCNTRL1
, reg
);
712 static int stb0899_wait_diseqc_rxidle(struct stb0899_state
*state
, int timeout
)
715 unsigned long start
= jiffies
;
717 while (!STB0899_GETFIELD(RXEND
, reg
)) {
718 reg
= stb0899_read_reg(state
, STB0899_DISRX_ST0
);
719 if (time_after(jiffies
, start
+ timeout
)) {
720 dprintk(state
->verbose
, FE_ERROR
, 1, "timed out!!");
729 static int stb0899_recv_slave_reply(struct dvb_frontend
*fe
, struct dvb_diseqc_slave_reply
*reply
)
731 struct stb0899_state
*state
= fe
->demodulator_priv
;
732 u8 reg
, length
= 0, i
;
735 if (stb0899_wait_diseqc_rxidle(state
, 100) < 0)
738 reg
= stb0899_read_reg(state
, STB0899_DISRX_ST0
);
739 if (STB0899_GETFIELD(RXEND
, reg
)) {
741 reg
= stb0899_read_reg(state
, STB0899_DISRX_ST1
);
742 length
= STB0899_GETFIELD(FIFOBYTENBR
, reg
);
744 if (length
> sizeof (reply
->msg
)) {
748 reply
->msg_len
= length
;
751 for (i
= 0; i
< length
; i
++)
752 reply
->msg
[i
] = stb0899_read_reg(state
, STB0899_DISFIFO
);
761 static int stb0899_wait_diseqc_txidle(struct stb0899_state
*state
, int timeout
)
764 unsigned long start
= jiffies
;
766 while (!STB0899_GETFIELD(TXIDLE
, reg
)) {
767 reg
= stb0899_read_reg(state
, STB0899_DISSTATUS
);
768 if (time_after(jiffies
, start
+ timeout
)) {
769 dprintk(state
->verbose
, FE_ERROR
, 1, "timed out!!");
777 static int stb0899_send_diseqc_burst(struct dvb_frontend
*fe
,
778 enum fe_sec_mini_cmd burst
)
780 struct stb0899_state
*state
= fe
->demodulator_priv
;
783 /* wait for diseqc idle */
784 if (stb0899_wait_diseqc_txidle(state
, 100) < 0)
787 reg
= stb0899_read_reg(state
, STB0899_DISCNTRL1
);
789 /* set to burst mode */
790 STB0899_SETFIELD_VAL(DISEQCMODE
, reg
, 0x03);
791 STB0899_SETFIELD_VAL(DISPRECHARGE
, reg
, 0x01);
792 stb0899_write_reg(state
, STB0899_DISCNTRL1
, reg
);
796 stb0899_write_reg(state
, STB0899_DISFIFO
, 0x00);
800 stb0899_write_reg(state
, STB0899_DISFIFO
, 0xff);
803 reg
= stb0899_read_reg(state
, STB0899_DISCNTRL1
);
804 STB0899_SETFIELD_VAL(DISPRECHARGE
, reg
, 0x00);
805 stb0899_write_reg(state
, STB0899_DISCNTRL1
, reg
);
806 /* wait for diseqc idle */
807 if (stb0899_wait_diseqc_txidle(state
, 100) < 0)
811 stb0899_write_reg(state
, STB0899_DISCNTRL1
, old_state
);
816 static int stb0899_diseqc_init(struct stb0899_state
*state
)
819 struct dvb_diseqc_slave_reply rx_data;
823 u32 mclk
, tx_freq
= 22000;/* count = 0, i; */
824 reg
= stb0899_read_reg(state
, STB0899_DISCNTRL2
);
825 STB0899_SETFIELD_VAL(ONECHIP_TRX
, reg
, 0);
826 stb0899_write_reg(state
, STB0899_DISCNTRL2
, reg
);
829 reg
= stb0899_read_reg(state
, STB0899_DISCNTRL1
);
830 STB0899_SETFIELD_VAL(DISEQCRESET
, reg
, 1);
831 stb0899_write_reg(state
, STB0899_DISCNTRL1
, reg
);
833 reg
= stb0899_read_reg(state
, STB0899_DISCNTRL1
);
834 STB0899_SETFIELD_VAL(DISEQCRESET
, reg
, 0);
835 stb0899_write_reg(state
, STB0899_DISCNTRL1
, reg
);
837 mclk
= stb0899_get_mclk(state
);
838 f22_tx
= mclk
/ (tx_freq
* 32);
839 stb0899_write_reg(state
, STB0899_DISF22
, f22_tx
); /* DiSEqC Tx freq */
840 state
->rx_freq
= 20000;
845 static int stb0899_sleep(struct dvb_frontend
*fe
)
847 struct stb0899_state
*state
= fe
->demodulator_priv
;
851 dprintk(state
->verbose
, FE_DEBUG
, 1, "Going to Sleep .. (Really tired .. :-))");
852 /* post process event */
853 stb0899_postproc(state
, STB0899_POSTPROC_GPIO_POWER
, 0);
858 static int stb0899_wakeup(struct dvb_frontend
*fe
)
861 struct stb0899_state
*state
= fe
->demodulator_priv
;
863 if ((rc
= stb0899_write_reg(state
, STB0899_SYNTCTRL
, STB0899_SELOSCI
)))
865 /* Activate all clocks; DVB-S2 registers are inaccessible otherwise. */
866 if ((rc
= stb0899_write_reg(state
, STB0899_STOPCLK1
, 0x00)))
868 if ((rc
= stb0899_write_reg(state
, STB0899_STOPCLK2
, 0x00)))
871 /* post process event */
872 stb0899_postproc(state
, STB0899_POSTPROC_GPIO_POWER
, 1);
877 static int stb0899_init(struct dvb_frontend
*fe
)
880 struct stb0899_state
*state
= fe
->demodulator_priv
;
881 struct stb0899_config
*config
= state
->config
;
883 dprintk(state
->verbose
, FE_DEBUG
, 1, "Initializing STB0899 ... ");
886 dprintk(state
->verbose
, FE_DEBUG
, 1, "init device");
887 for (i
= 0; config
->init_dev
[i
].address
!= 0xffff; i
++)
888 stb0899_write_reg(state
, config
->init_dev
[i
].address
, config
->init_dev
[i
].data
);
890 dprintk(state
->verbose
, FE_DEBUG
, 1, "init S2 demod");
892 for (i
= 0; config
->init_s2_demod
[i
].offset
!= 0xffff; i
++)
893 stb0899_write_s2reg(state
, STB0899_S2DEMOD
,
894 config
->init_s2_demod
[i
].base_address
,
895 config
->init_s2_demod
[i
].offset
,
896 config
->init_s2_demod
[i
].data
);
898 dprintk(state
->verbose
, FE_DEBUG
, 1, "init S1 demod");
900 for (i
= 0; config
->init_s1_demod
[i
].address
!= 0xffff; i
++)
901 stb0899_write_reg(state
, config
->init_s1_demod
[i
].address
, config
->init_s1_demod
[i
].data
);
903 dprintk(state
->verbose
, FE_DEBUG
, 1, "init S2 FEC");
905 for (i
= 0; config
->init_s2_fec
[i
].offset
!= 0xffff; i
++)
906 stb0899_write_s2reg(state
, STB0899_S2FEC
,
907 config
->init_s2_fec
[i
].base_address
,
908 config
->init_s2_fec
[i
].offset
,
909 config
->init_s2_fec
[i
].data
);
911 dprintk(state
->verbose
, FE_DEBUG
, 1, "init TST");
913 for (i
= 0; config
->init_tst
[i
].address
!= 0xffff; i
++)
914 stb0899_write_reg(state
, config
->init_tst
[i
].address
, config
->init_tst
[i
].data
);
916 stb0899_init_calc(state
);
917 stb0899_diseqc_init(state
);
922 static int stb0899_table_lookup(const struct stb0899_tab
*tab
, int max
, int val
)
927 if (val
< tab
[min
].read
)
929 else if (val
>= tab
[max
].read
)
932 while ((max
- min
) > 1) {
933 med
= (max
+ min
) / 2;
934 if (val
>= tab
[min
].read
&& val
< tab
[med
].read
)
939 res
= ((val
- tab
[min
].read
) *
940 (tab
[max
].real
- tab
[min
].real
) /
941 (tab
[max
].read
- tab
[min
].read
)) +
948 static int stb0899_read_signal_strength(struct dvb_frontend
*fe
, u16
*strength
)
950 struct stb0899_state
*state
= fe
->demodulator_priv
;
951 struct stb0899_internal
*internal
= &state
->internal
;
956 switch (state
->delsys
) {
959 if (internal
->lock
) {
960 reg
= stb0899_read_reg(state
, STB0899_VSTATUS
);
961 if (STB0899_GETFIELD(VSTATUS_LOCKEDVIT
, reg
)) {
963 reg
= stb0899_read_reg(state
, STB0899_AGCIQIN
);
964 val
= (s32
)(s8
)STB0899_GETFIELD(AGCIQVALUE
, reg
);
966 *strength
= stb0899_table_lookup(stb0899_dvbsrf_tab
, ARRAY_SIZE(stb0899_dvbsrf_tab
) - 1, val
);
968 dprintk(state
->verbose
, FE_DEBUG
, 1, "AGCIQVALUE = 0x%02x, C = %d * 0.1 dBm",
969 val
& 0xff, *strength
);
974 if (internal
->lock
) {
975 reg
= STB0899_READ_S2REG(STB0899_S2DEMOD
, IF_AGC_GAIN
);
976 val
= STB0899_GETFIELD(IF_AGC_GAIN
, reg
);
978 *strength
= stb0899_table_lookup(stb0899_dvbs2rf_tab
, ARRAY_SIZE(stb0899_dvbs2rf_tab
) - 1, val
);
980 dprintk(state
->verbose
, FE_DEBUG
, 1, "IF_AGC_GAIN = 0x%04x, C = %d * 0.1 dBm",
981 val
& 0x3fff, *strength
);
985 dprintk(state
->verbose
, FE_DEBUG
, 1, "Unsupported delivery system");
992 static int stb0899_read_snr(struct dvb_frontend
*fe
, u16
*snr
)
994 struct stb0899_state
*state
= fe
->demodulator_priv
;
995 struct stb0899_internal
*internal
= &state
->internal
;
997 unsigned int val
, quant
, quantn
= -1, est
, estn
= -1;
1002 reg
= stb0899_read_reg(state
, STB0899_VSTATUS
);
1003 switch (state
->delsys
) {
1006 if (internal
->lock
) {
1007 if (STB0899_GETFIELD(VSTATUS_LOCKEDVIT
, reg
)) {
1009 stb0899_read_regs(state
, STB0899_NIRM
, buf
, 2);
1010 val
= MAKEWORD16(buf
[0], buf
[1]);
1012 *snr
= stb0899_table_lookup(stb0899_cn_tab
, ARRAY_SIZE(stb0899_cn_tab
) - 1, val
);
1013 dprintk(state
->verbose
, FE_DEBUG
, 1, "NIR = 0x%02x%02x = %u, C/N = %d * 0.1 dBm\n",
1014 buf
[0], buf
[1], val
, *snr
);
1019 if (internal
->lock
) {
1020 reg
= STB0899_READ_S2REG(STB0899_S2DEMOD
, UWP_CNTRL1
);
1021 quant
= STB0899_GETFIELD(UWP_ESN0_QUANT
, reg
);
1022 reg
= STB0899_READ_S2REG(STB0899_S2DEMOD
, UWP_STAT2
);
1023 est
= STB0899_GETFIELD(ESN0_EST
, reg
);
1025 val
= 301; /* C/N = 30.1 dB */
1027 val
= 270; /* C/N = 27.0 dB */
1029 /* quantn = 100 * log(quant^2) */
1030 quantn
= stb0899_table_lookup(stb0899_quant_tab
, ARRAY_SIZE(stb0899_quant_tab
) - 1, quant
* 100);
1031 /* estn = 100 * log(est) */
1032 estn
= stb0899_table_lookup(stb0899_est_tab
, ARRAY_SIZE(stb0899_est_tab
) - 1, est
);
1033 /* snr(dBm/10) = -10*(log(est)-log(quant^2)) => snr(dBm/10) = (100*log(quant^2)-100*log(est))/10 */
1034 val
= (quantn
- estn
) / 10;
1037 dprintk(state
->verbose
, FE_DEBUG
, 1, "Es/N0 quant = %d (%d) estimate = %u (%d), C/N = %d * 0.1 dBm",
1038 quant
, quantn
, est
, estn
, val
);
1042 dprintk(state
->verbose
, FE_DEBUG
, 1, "Unsupported delivery system");
1049 static int stb0899_read_status(struct dvb_frontend
*fe
, enum fe_status
*status
)
1051 struct stb0899_state
*state
= fe
->demodulator_priv
;
1052 struct stb0899_internal
*internal
= &state
->internal
;
1056 switch (state
->delsys
) {
1059 dprintk(state
->verbose
, FE_DEBUG
, 1, "Delivery system DVB-S/DSS");
1060 if (internal
->lock
) {
1061 reg
= stb0899_read_reg(state
, STB0899_VSTATUS
);
1062 if (STB0899_GETFIELD(VSTATUS_LOCKEDVIT
, reg
)) {
1063 dprintk(state
->verbose
, FE_DEBUG
, 1, "--------> FE_HAS_CARRIER | FE_HAS_LOCK");
1064 *status
|= FE_HAS_SIGNAL
| FE_HAS_CARRIER
| FE_HAS_LOCK
;
1066 reg
= stb0899_read_reg(state
, STB0899_PLPARM
);
1067 if (STB0899_GETFIELD(VITCURPUN
, reg
)) {
1068 dprintk(state
->verbose
, FE_DEBUG
, 1, "--------> FE_HAS_VITERBI | FE_HAS_SYNC");
1069 *status
|= FE_HAS_VITERBI
| FE_HAS_SYNC
;
1070 /* post process event */
1071 stb0899_postproc(state
, STB0899_POSTPROC_GPIO_LOCK
, 1);
1077 dprintk(state
->verbose
, FE_DEBUG
, 1, "Delivery system DVB-S2");
1078 if (internal
->lock
) {
1079 reg
= STB0899_READ_S2REG(STB0899_S2DEMOD
, DMD_STAT2
);
1080 if (STB0899_GETFIELD(UWP_LOCK
, reg
) && STB0899_GETFIELD(CSM_LOCK
, reg
)) {
1081 *status
|= FE_HAS_CARRIER
;
1082 dprintk(state
->verbose
, FE_DEBUG
, 1,
1083 "UWP & CSM Lock ! ---> DVB-S2 FE_HAS_CARRIER");
1085 reg
= stb0899_read_reg(state
, STB0899_CFGPDELSTATUS1
);
1086 if (STB0899_GETFIELD(CFGPDELSTATUS_LOCK
, reg
)) {
1087 *status
|= FE_HAS_LOCK
;
1088 dprintk(state
->verbose
, FE_DEBUG
, 1,
1089 "Packet Delineator Locked ! -----> DVB-S2 FE_HAS_LOCK");
1092 if (STB0899_GETFIELD(CONTINUOUS_STREAM
, reg
)) {
1093 *status
|= FE_HAS_VITERBI
;
1094 dprintk(state
->verbose
, FE_DEBUG
, 1,
1095 "Packet Delineator found VITERBI ! -----> DVB-S2 FE_HAS_VITERBI");
1097 if (STB0899_GETFIELD(ACCEPTED_STREAM
, reg
)) {
1098 *status
|= FE_HAS_SYNC
;
1099 dprintk(state
->verbose
, FE_DEBUG
, 1,
1100 "Packet Delineator found SYNC ! -----> DVB-S2 FE_HAS_SYNC");
1101 /* post process event */
1102 stb0899_postproc(state
, STB0899_POSTPROC_GPIO_LOCK
, 1);
1108 dprintk(state
->verbose
, FE_DEBUG
, 1, "Unsupported delivery system");
1116 * viterbi error for DVB-S/DSS
1117 * packet error for DVB-S2
1118 * Bit Error Rate or Packet Error Rate * 10 ^ 7
1120 static int stb0899_read_ber(struct dvb_frontend
*fe
, u32
*ber
)
1122 struct stb0899_state
*state
= fe
->demodulator_priv
;
1123 struct stb0899_internal
*internal
= &state
->internal
;
1129 switch (state
->delsys
) {
1132 if (internal
->lock
) {
1133 lsb
= stb0899_read_reg(state
, STB0899_ECNT1L
);
1134 msb
= stb0899_read_reg(state
, STB0899_ECNT1M
);
1135 *ber
= MAKEWORD16(msb
, lsb
);
1137 if (STB0899_GETFIELD(VSTATUS_PRFVIT
, internal
->v_status
)) {
1140 /* ber = ber * 10 ^ 7 */
1141 *ber
/= (-1 + (1 << (2 * STB0899_GETFIELD(NOE
, internal
->err_ctrl
))));
1147 if (internal
->lock
) {
1148 lsb
= stb0899_read_reg(state
, STB0899_ECNT1L
);
1149 msb
= stb0899_read_reg(state
, STB0899_ECNT1M
);
1150 *ber
= MAKEWORD16(msb
, lsb
);
1151 /* ber = ber * 10 ^ 7 */
1153 *ber
/= (-1 + (1 << (4 + 2 * STB0899_GETFIELD(NOE
, internal
->err_ctrl
))));
1157 dprintk(state
->verbose
, FE_DEBUG
, 1, "Unsupported delivery system");
1164 static int stb0899_set_voltage(struct dvb_frontend
*fe
,
1165 enum fe_sec_voltage voltage
)
1167 struct stb0899_state
*state
= fe
->demodulator_priv
;
1170 case SEC_VOLTAGE_13
:
1171 stb0899_write_reg(state
, STB0899_GPIO00CFG
, 0x82);
1172 stb0899_write_reg(state
, STB0899_GPIO01CFG
, 0x02);
1173 stb0899_write_reg(state
, STB0899_GPIO02CFG
, 0x00);
1175 case SEC_VOLTAGE_18
:
1176 stb0899_write_reg(state
, STB0899_GPIO00CFG
, 0x02);
1177 stb0899_write_reg(state
, STB0899_GPIO01CFG
, 0x02);
1178 stb0899_write_reg(state
, STB0899_GPIO02CFG
, 0x82);
1180 case SEC_VOLTAGE_OFF
:
1181 stb0899_write_reg(state
, STB0899_GPIO00CFG
, 0x82);
1182 stb0899_write_reg(state
, STB0899_GPIO01CFG
, 0x82);
1183 stb0899_write_reg(state
, STB0899_GPIO02CFG
, 0x82);
1192 static int stb0899_set_tone(struct dvb_frontend
*fe
, enum fe_sec_tone_mode tone
)
1194 struct stb0899_state
*state
= fe
->demodulator_priv
;
1195 struct stb0899_internal
*internal
= &state
->internal
;
1199 /* wait for diseqc idle */
1200 if (stb0899_wait_diseqc_txidle(state
, 100) < 0)
1205 div
= (internal
->master_clk
/ 100) / 5632;
1206 div
= (div
+ 5) / 10;
1207 stb0899_write_reg(state
, STB0899_DISEQCOCFG
, 0x66);
1208 reg
= stb0899_read_reg(state
, STB0899_ACRPRESC
);
1209 STB0899_SETFIELD_VAL(ACRPRESC
, reg
, 0x03);
1210 stb0899_write_reg(state
, STB0899_ACRPRESC
, reg
);
1211 stb0899_write_reg(state
, STB0899_ACRDIV1
, div
);
1214 stb0899_write_reg(state
, STB0899_DISEQCOCFG
, 0x20);
1222 int stb0899_i2c_gate_ctrl(struct dvb_frontend
*fe
, int enable
)
1225 struct stb0899_state
*state
= fe
->demodulator_priv
;
1227 i2c_stat
= stb0899_read_reg(state
, STB0899_I2CRPT
);
1232 dprintk(state
->verbose
, FE_DEBUG
, 1, "Enabling I2C Repeater ...");
1233 i2c_stat
|= STB0899_I2CTON
;
1234 if (stb0899_write_reg(state
, STB0899_I2CRPT
, i2c_stat
) < 0)
1237 dprintk(state
->verbose
, FE_DEBUG
, 1, "Disabling I2C Repeater ...");
1238 i2c_stat
&= ~STB0899_I2CTON
;
1239 if (stb0899_write_reg(state
, STB0899_I2CRPT
, i2c_stat
) < 0)
1244 dprintk(state
->verbose
, FE_ERROR
, 1, "I2C Repeater control failed");
1249 static inline void CONVERT32(u32 x
, char *str
)
1251 *str
++ = (x
>> 24) & 0xff;
1252 *str
++ = (x
>> 16) & 0xff;
1253 *str
++ = (x
>> 8) & 0xff;
1254 *str
++ = (x
>> 0) & 0xff;
1258 static int stb0899_get_dev_id(struct stb0899_state
*state
)
1260 u8 chip_id
, release
;
1262 u32 demod_ver
= 0, fec_ver
= 0;
1263 char demod_str
[5] = { 0 };
1264 char fec_str
[5] = { 0 };
1266 id
= stb0899_read_reg(state
, STB0899_DEV_ID
);
1267 dprintk(state
->verbose
, FE_DEBUG
, 1, "ID reg=[0x%02x]", id
);
1268 chip_id
= STB0899_GETFIELD(CHIP_ID
, id
);
1269 release
= STB0899_GETFIELD(CHIP_REL
, id
);
1271 dprintk(state
->verbose
, FE_ERROR
, 1, "Device ID=[%d], Release=[%d]",
1274 CONVERT32(STB0899_READ_S2REG(STB0899_S2DEMOD
, DMD_CORE_ID
), (char *)&demod_str
);
1276 demod_ver
= STB0899_READ_S2REG(STB0899_S2DEMOD
, DMD_VERSION_ID
);
1277 dprintk(state
->verbose
, FE_ERROR
, 1, "Demodulator Core ID=[%s], Version=[%d]", (char *) &demod_str
, demod_ver
);
1278 CONVERT32(STB0899_READ_S2REG(STB0899_S2FEC
, FEC_CORE_ID_REG
), (char *)&fec_str
);
1279 fec_ver
= STB0899_READ_S2REG(STB0899_S2FEC
, FEC_VER_ID_REG
);
1280 if (! (chip_id
> 0)) {
1281 dprintk(state
->verbose
, FE_ERROR
, 1, "couldn't find a STB 0899");
1285 dprintk(state
->verbose
, FE_ERROR
, 1, "FEC Core ID=[%s], Version=[%d]", (char*) &fec_str
, fec_ver
);
1290 static void stb0899_set_delivery(struct stb0899_state
*state
)
1295 stop_clk
[0] = stb0899_read_reg(state
, STB0899_STOPCLK1
);
1296 stop_clk
[1] = stb0899_read_reg(state
, STB0899_STOPCLK2
);
1298 switch (state
->delsys
) {
1300 dprintk(state
->verbose
, FE_DEBUG
, 1, "Delivery System -- DVB-S");
1301 /* FECM/Viterbi ON */
1302 reg
= stb0899_read_reg(state
, STB0899_FECM
);
1303 STB0899_SETFIELD_VAL(FECM_RSVD0
, reg
, 0);
1304 STB0899_SETFIELD_VAL(FECM_VITERBI_ON
, reg
, 1);
1305 stb0899_write_reg(state
, STB0899_FECM
, reg
);
1307 stb0899_write_reg(state
, STB0899_RSULC
, 0xb1);
1308 stb0899_write_reg(state
, STB0899_TSULC
, 0x40);
1309 stb0899_write_reg(state
, STB0899_RSLLC
, 0x42);
1310 stb0899_write_reg(state
, STB0899_TSLPL
, 0x12);
1312 reg
= stb0899_read_reg(state
, STB0899_TSTRES
);
1313 STB0899_SETFIELD_VAL(FRESLDPC
, reg
, 1);
1314 stb0899_write_reg(state
, STB0899_TSTRES
, reg
);
1316 STB0899_SETFIELD_VAL(STOP_CHK8PSK
, stop_clk
[0], 1);
1317 STB0899_SETFIELD_VAL(STOP_CKFEC108
, stop_clk
[0], 1);
1318 STB0899_SETFIELD_VAL(STOP_CKFEC216
, stop_clk
[0], 1);
1320 STB0899_SETFIELD_VAL(STOP_CKPKDLIN108
, stop_clk
[1], 1);
1321 STB0899_SETFIELD_VAL(STOP_CKPKDLIN216
, stop_clk
[1], 1);
1323 STB0899_SETFIELD_VAL(STOP_CKINTBUF216
, stop_clk
[0], 1);
1324 STB0899_SETFIELD_VAL(STOP_CKCORE216
, stop_clk
[0], 0);
1326 STB0899_SETFIELD_VAL(STOP_CKS2DMD108
, stop_clk
[1], 1);
1329 /* FECM/Viterbi OFF */
1330 reg
= stb0899_read_reg(state
, STB0899_FECM
);
1331 STB0899_SETFIELD_VAL(FECM_RSVD0
, reg
, 0);
1332 STB0899_SETFIELD_VAL(FECM_VITERBI_ON
, reg
, 0);
1333 stb0899_write_reg(state
, STB0899_FECM
, reg
);
1335 stb0899_write_reg(state
, STB0899_RSULC
, 0xb1);
1336 stb0899_write_reg(state
, STB0899_TSULC
, 0x42);
1337 stb0899_write_reg(state
, STB0899_RSLLC
, 0x40);
1338 stb0899_write_reg(state
, STB0899_TSLPL
, 0x02);
1340 reg
= stb0899_read_reg(state
, STB0899_TSTRES
);
1341 STB0899_SETFIELD_VAL(FRESLDPC
, reg
, 0);
1342 stb0899_write_reg(state
, STB0899_TSTRES
, reg
);
1344 STB0899_SETFIELD_VAL(STOP_CHK8PSK
, stop_clk
[0], 1);
1345 STB0899_SETFIELD_VAL(STOP_CKFEC108
, stop_clk
[0], 0);
1346 STB0899_SETFIELD_VAL(STOP_CKFEC216
, stop_clk
[0], 0);
1348 STB0899_SETFIELD_VAL(STOP_CKPKDLIN108
, stop_clk
[1], 0);
1349 STB0899_SETFIELD_VAL(STOP_CKPKDLIN216
, stop_clk
[1], 0);
1351 STB0899_SETFIELD_VAL(STOP_CKINTBUF216
, stop_clk
[0], 0);
1352 STB0899_SETFIELD_VAL(STOP_CKCORE216
, stop_clk
[0], 0);
1354 STB0899_SETFIELD_VAL(STOP_CKS2DMD108
, stop_clk
[1], 0);
1357 /* FECM/Viterbi ON */
1358 reg
= stb0899_read_reg(state
, STB0899_FECM
);
1359 STB0899_SETFIELD_VAL(FECM_RSVD0
, reg
, 1);
1360 STB0899_SETFIELD_VAL(FECM_VITERBI_ON
, reg
, 1);
1361 stb0899_write_reg(state
, STB0899_FECM
, reg
);
1363 stb0899_write_reg(state
, STB0899_RSULC
, 0xa1);
1364 stb0899_write_reg(state
, STB0899_TSULC
, 0x61);
1365 stb0899_write_reg(state
, STB0899_RSLLC
, 0x42);
1367 reg
= stb0899_read_reg(state
, STB0899_TSTRES
);
1368 STB0899_SETFIELD_VAL(FRESLDPC
, reg
, 1);
1369 stb0899_write_reg(state
, STB0899_TSTRES
, reg
);
1371 STB0899_SETFIELD_VAL(STOP_CHK8PSK
, stop_clk
[0], 1);
1372 STB0899_SETFIELD_VAL(STOP_CKFEC108
, stop_clk
[0], 1);
1373 STB0899_SETFIELD_VAL(STOP_CKFEC216
, stop_clk
[0], 1);
1375 STB0899_SETFIELD_VAL(STOP_CKPKDLIN108
, stop_clk
[1], 1);
1376 STB0899_SETFIELD_VAL(STOP_CKPKDLIN216
, stop_clk
[1], 1);
1378 STB0899_SETFIELD_VAL(STOP_CKCORE216
, stop_clk
[0], 0);
1380 STB0899_SETFIELD_VAL(STOP_CKS2DMD108
, stop_clk
[1], 1);
1383 dprintk(state
->verbose
, FE_ERROR
, 1, "Unsupported delivery system");
1386 STB0899_SETFIELD_VAL(STOP_CKADCI108
, stop_clk
[0], 0);
1387 stb0899_write_regs(state
, STB0899_STOPCLK1
, stop_clk
, 2);
1391 * stb0899_set_iterations
1392 * set the LDPC iteration scale function
1394 static void stb0899_set_iterations(struct stb0899_state
*state
)
1396 struct stb0899_internal
*internal
= &state
->internal
;
1397 struct stb0899_config
*config
= state
->config
;
1402 iter_scale
= 17 * (internal
->master_clk
/ 1000);
1403 iter_scale
+= 410000;
1404 iter_scale
/= (internal
->srate
/ 1000000);
1407 if (iter_scale
> config
->ldpc_max_iter
)
1408 iter_scale
= config
->ldpc_max_iter
;
1410 reg
= STB0899_READ_S2REG(STB0899_S2FEC
, MAX_ITER
);
1411 STB0899_SETFIELD_VAL(MAX_ITERATIONS
, reg
, iter_scale
);
1412 stb0899_write_s2reg(state
, STB0899_S2FEC
, STB0899_BASE_MAX_ITER
, STB0899_OFF0_MAX_ITER
, reg
);
1415 static enum dvbfe_search
stb0899_search(struct dvb_frontend
*fe
)
1417 struct stb0899_state
*state
= fe
->demodulator_priv
;
1418 struct stb0899_params
*i_params
= &state
->params
;
1419 struct stb0899_internal
*internal
= &state
->internal
;
1420 struct stb0899_config
*config
= state
->config
;
1421 struct dtv_frontend_properties
*props
= &fe
->dtv_property_cache
;
1423 u32 SearchRange
, gain
;
1425 i_params
->freq
= props
->frequency
;
1426 i_params
->srate
= props
->symbol_rate
;
1427 state
->delsys
= props
->delivery_system
;
1428 dprintk(state
->verbose
, FE_DEBUG
, 1, "delivery system=%d", state
->delsys
);
1430 SearchRange
= 10000000;
1431 dprintk(state
->verbose
, FE_DEBUG
, 1, "Frequency=%d, Srate=%d", i_params
->freq
, i_params
->srate
);
1432 /* checking Search Range is meaningless for a fixed 3 Mhz */
1433 if (INRANGE(i_params
->srate
, 1000000, 45000000)) {
1434 dprintk(state
->verbose
, FE_DEBUG
, 1, "Parameters IN RANGE");
1435 stb0899_set_delivery(state
);
1437 if (state
->config
->tuner_set_rfsiggain
) {
1438 if (internal
->srate
> 15000000)
1439 gain
= 8; /* 15Mb < srate < 45Mb, gain = 8dB */
1440 else if (internal
->srate
> 5000000)
1441 gain
= 12; /* 5Mb < srate < 15Mb, gain = 12dB */
1443 gain
= 14; /* 1Mb < srate < 5Mb, gain = 14db */
1444 state
->config
->tuner_set_rfsiggain(fe
, gain
);
1447 if (i_params
->srate
<= 5000000)
1448 stb0899_set_mclk(state
, config
->lo_clk
);
1450 stb0899_set_mclk(state
, config
->hi_clk
);
1452 switch (state
->delsys
) {
1455 dprintk(state
->verbose
, FE_DEBUG
, 1, "DVB-S delivery system");
1456 internal
->freq
= i_params
->freq
;
1457 internal
->srate
= i_params
->srate
;
1459 * search = user search range +
1461 * 2 * Tuner_step_size +
1462 * 10% of the symbol rate
1464 internal
->srch_range
= SearchRange
+ 1500000 + (i_params
->srate
/ 5);
1465 internal
->derot_percent
= 30;
1467 /* What to do for tuners having no bandwidth setup ? */
1468 /* enable tuner I/O */
1469 stb0899_i2c_gate_ctrl(&state
->frontend
, 1);
1471 if (state
->config
->tuner_set_bandwidth
)
1472 state
->config
->tuner_set_bandwidth(fe
, (13 * (stb0899_carr_width(state
) + SearchRange
)) / 10);
1473 if (state
->config
->tuner_get_bandwidth
)
1474 state
->config
->tuner_get_bandwidth(fe
, &internal
->tuner_bw
);
1476 /* disable tuner I/O */
1477 stb0899_i2c_gate_ctrl(&state
->frontend
, 0);
1479 /* Set DVB-S1 AGC */
1480 stb0899_write_reg(state
, STB0899_AGCRFCFG
, 0x11);
1482 /* Run the search algorithm */
1483 dprintk(state
->verbose
, FE_DEBUG
, 1, "running DVB-S search algo ..");
1484 if (stb0899_dvbs_algo(state
) == RANGEOK
) {
1486 dprintk(state
->verbose
, FE_DEBUG
, 1,
1487 "-------------------------------------> DVB-S LOCK !");
1489 // stb0899_write_reg(state, STB0899_ERRCTRL1, 0x3d); /* Viterbi Errors */
1490 // internal->v_status = stb0899_read_reg(state, STB0899_VSTATUS);
1491 // internal->err_ctrl = stb0899_read_reg(state, STB0899_ERRCTRL1);
1492 // dprintk(state->verbose, FE_DEBUG, 1, "VSTATUS=0x%02x", internal->v_status);
1493 // dprintk(state->verbose, FE_DEBUG, 1, "ERR_CTRL=0x%02x", internal->err_ctrl);
1495 return DVBFE_ALGO_SEARCH_SUCCESS
;
1499 return DVBFE_ALGO_SEARCH_FAILED
;
1503 internal
->freq
= i_params
->freq
;
1504 internal
->srate
= i_params
->srate
;
1505 internal
->srch_range
= SearchRange
;
1507 /* enable tuner I/O */
1508 stb0899_i2c_gate_ctrl(&state
->frontend
, 1);
1510 if (state
->config
->tuner_set_bandwidth
)
1511 state
->config
->tuner_set_bandwidth(fe
, (stb0899_carr_width(state
) + SearchRange
));
1512 if (state
->config
->tuner_get_bandwidth
)
1513 state
->config
->tuner_get_bandwidth(fe
, &internal
->tuner_bw
);
1515 /* disable tuner I/O */
1516 stb0899_i2c_gate_ctrl(&state
->frontend
, 0);
1518 // pParams->SpectralInv = pSearch->IQ_Inversion;
1520 /* Set DVB-S2 AGC */
1521 stb0899_write_reg(state
, STB0899_AGCRFCFG
, 0x1c);
1523 /* Set IterScale =f(MCLK,SYMB) */
1524 stb0899_set_iterations(state
);
1526 /* Run the search algorithm */
1527 dprintk(state
->verbose
, FE_DEBUG
, 1, "running DVB-S2 search algo ..");
1528 if (stb0899_dvbs2_algo(state
) == DVBS2_FEC_LOCK
) {
1530 dprintk(state
->verbose
, FE_DEBUG
, 1,
1531 "-------------------------------------> DVB-S2 LOCK !");
1533 // stb0899_write_reg(state, STB0899_ERRCTRL1, 0xb6); /* Packet Errors */
1534 // internal->v_status = stb0899_read_reg(state, STB0899_VSTATUS);
1535 // internal->err_ctrl = stb0899_read_reg(state, STB0899_ERRCTRL1);
1537 return DVBFE_ALGO_SEARCH_SUCCESS
;
1541 return DVBFE_ALGO_SEARCH_FAILED
;
1545 dprintk(state
->verbose
, FE_ERROR
, 1, "Unsupported delivery system");
1546 return DVBFE_ALGO_SEARCH_INVALID
;
1550 return DVBFE_ALGO_SEARCH_ERROR
;
1553 static int stb0899_get_frontend(struct dvb_frontend
*fe
,
1554 struct dtv_frontend_properties
*p
)
1556 struct stb0899_state
*state
= fe
->demodulator_priv
;
1557 struct stb0899_internal
*internal
= &state
->internal
;
1559 dprintk(state
->verbose
, FE_DEBUG
, 1, "Get params");
1560 p
->symbol_rate
= internal
->srate
;
1561 p
->frequency
= internal
->freq
;
1566 static enum dvbfe_algo
stb0899_frontend_algo(struct dvb_frontend
*fe
)
1568 return DVBFE_ALGO_CUSTOM
;
1571 static const struct dvb_frontend_ops stb0899_ops
= {
1572 .delsys
= { SYS_DVBS
, SYS_DVBS2
, SYS_DSS
},
1574 .name
= "STB0899 Multistandard",
1575 .frequency_min_hz
= 950 * MHz
,
1576 .frequency_max_hz
= 2150 * MHz
,
1577 .symbol_rate_min
= 5000000,
1578 .symbol_rate_max
= 45000000,
1580 .caps
= FE_CAN_INVERSION_AUTO
|
1582 FE_CAN_2G_MODULATION
|
1586 .detach
= stb0899_detach
,
1587 .release
= stb0899_release
,
1588 .init
= stb0899_init
,
1589 .sleep
= stb0899_sleep
,
1590 // .wakeup = stb0899_wakeup,
1592 .i2c_gate_ctrl
= stb0899_i2c_gate_ctrl
,
1594 .get_frontend_algo
= stb0899_frontend_algo
,
1595 .search
= stb0899_search
,
1596 .get_frontend
= stb0899_get_frontend
,
1599 .read_status
= stb0899_read_status
,
1600 .read_snr
= stb0899_read_snr
,
1601 .read_signal_strength
= stb0899_read_signal_strength
,
1602 .read_ber
= stb0899_read_ber
,
1604 .set_voltage
= stb0899_set_voltage
,
1605 .set_tone
= stb0899_set_tone
,
1607 .diseqc_send_master_cmd
= stb0899_send_diseqc_msg
,
1608 .diseqc_recv_slave_reply
= stb0899_recv_slave_reply
,
1609 .diseqc_send_burst
= stb0899_send_diseqc_burst
,
1612 struct dvb_frontend
*stb0899_attach(struct stb0899_config
*config
, struct i2c_adapter
*i2c
)
1614 struct stb0899_state
*state
= NULL
;
1616 state
= kzalloc(sizeof (struct stb0899_state
), GFP_KERNEL
);
1620 state
->verbose
= &verbose
;
1621 state
->config
= config
;
1623 state
->frontend
.ops
= stb0899_ops
;
1624 state
->frontend
.demodulator_priv
= state
;
1625 /* use configured inversion as default -- we'll later autodetect inversion */
1626 state
->internal
.inversion
= config
->inversion
;
1628 stb0899_wakeup(&state
->frontend
);
1629 if (stb0899_get_dev_id(state
) == -ENODEV
) {
1630 printk("%s: Exiting .. !\n", __func__
);
1634 printk("%s: Attaching STB0899 \n", __func__
);
1635 return &state
->frontend
;
1641 EXPORT_SYMBOL(stb0899_attach
);
1642 MODULE_PARM_DESC(verbose
, "Set Verbosity level");
1643 MODULE_AUTHOR("Manu Abraham");
1644 MODULE_DESCRIPTION("STB0899 Multi-Std frontend");
1645 MODULE_LICENSE("GPL");