2 STB0899 Multistandard Frontend driver
3 Copyright (C) Manu Abraham (abraham.manu@gmail.com)
5 Copyright (C) ST Microelectronics
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 #include <linux/init.h>
23 #include <linux/kernel.h>
24 #include <linux/module.h>
25 #include <linux/string.h>
27 #include <linux/dvb/frontend.h>
28 #include "dvb_frontend.h"
30 #include "stb0899_drv.h"
31 #include "stb0899_priv.h"
32 #include "stb0899_reg.h"
34 static unsigned int verbose = 5;
35 module_param(verbose, int, 0644);
37 /* C/N in dB/10, NIRM/NIRL */
38 static const struct stb0899_tab stb0899_cn_tab[] = {
61 /* DVB-S AGCIQ_VALUE vs. signal level in dBm/10.
62 * As measured, connected to a modulator.
63 * -8.0 to -50.0 dBm directly connected,
64 * -52.0 to -74.8 with extra attenuation.
65 * Cut-off to AGCIQ_VALUE = 0x80 below -74.8dBm.
66 * Crude linear extrapolation below -84.8dBm and above -8.0dBm.
68 static const struct stb0899_tab stb0899_dvbsrf_tab[] = {
102 /* DVB-S2 IF_AGC_GAIN vs. signal level in dBm/10.
103 * As measured, connected to a modulator.
104 * -8.0 to -50.1 dBm directly connected,
105 * -53.0 to -76.6 with extra attenuation.
106 * Cut-off to IF_AGC_GAIN = 0x3fff below -76.6dBm.
107 * Crude linear extrapolation below -76.6dBm and above -8.0dBm.
109 static const struct stb0899_tab stb0899_dvbs2rf_tab[] = {
136 /* DVB-S2 Es/N0 quant in dB/100 vs read value * 100*/
137 struct stb0899_tab stb0899_quant_tab[] = {
179 /* DVB-S2 Es/N0 estimate in dB/100 vs read value */
180 struct stb0899_tab stb0899_est_tab[] = {
220 int _stb0899_read_reg(struct stb0899_state *state, unsigned int reg)
224 u8 b0[] = { reg >> 8, reg & 0xff };
227 struct i2c_msg msg[] = {
229 .addr = state->config->demod_address,
234 .addr = state->config->demod_address,
241 ret = i2c_transfer(state->i2c, msg, 2);
243 if (ret != -ERESTARTSYS)
244 dprintk(verbose, FE_ERROR, 1,
245 "Read error, Reg=[0x%02x], Status=%d",
248 return ret < 0 ? ret : -EREMOTEIO;
250 if (unlikely(verbose >= FE_DEBUGREG))
251 dprintk(verbose, FE_ERROR, 1, "Reg=[0x%02x], data=%02x",
254 return (unsigned int)buf;
257 int stb0899_read_reg(struct stb0899_state *state, unsigned int reg)
261 result = _stb0899_read_reg(state, reg);
264 * access to 0xf2xx/0xf6xx
265 * must be followed by read from 0xf2ff/0xf6ff.
267 if ((reg != 0xf2ff) && (reg != 0xf6ff) &&
268 (((reg & 0xff00) == 0xf200) || ((reg & 0xff00) == 0xf600)))
269 _stb0899_read_reg(state, (reg | 0x00ff));
274 u32 _stb0899_read_s2reg(struct stb0899_state *state,
276 u32 stb0899_base_addr,
277 u16 stb0899_reg_offset)
285 GETBYTE(stb0899_i2cdev, BYTE1), /* 0xf3 S2 Base Address (MSB) */
286 GETBYTE(stb0899_i2cdev, BYTE0), /* 0xfc S2 Base Address (LSB) */
287 GETBYTE(stb0899_base_addr, BYTE0), /* 0x00 Base Address (LSB) */
288 GETBYTE(stb0899_base_addr, BYTE1), /* 0x04 Base Address (LSB) */
289 GETBYTE(stb0899_base_addr, BYTE2), /* 0x00 Base Address (MSB) */
290 GETBYTE(stb0899_base_addr, BYTE3), /* 0x00 Base Address (MSB) */
293 0x00, /* 0xf3 Reg Offset */
294 0x00, /* 0x44 Reg Offset */
297 struct i2c_msg msg_0 = {
298 .addr = state->config->demod_address,
304 struct i2c_msg msg_1 = {
305 .addr = state->config->demod_address,
311 struct i2c_msg msg_r = {
312 .addr = state->config->demod_address,
318 tmpaddr = stb0899_reg_offset & 0xff00;
319 if (!(stb0899_reg_offset & 0x8))
320 tmpaddr = stb0899_reg_offset | 0x20;
322 buf_1[0] = GETBYTE(tmpaddr, BYTE1);
323 buf_1[1] = GETBYTE(tmpaddr, BYTE0);
325 status = i2c_transfer(state->i2c, &msg_0, 1);
327 if (status != -ERESTARTSYS)
328 printk(KERN_ERR "%s ERR(1), Device=[0x%04x], Base address=[0x%08x], Offset=[0x%04x], Status=%d\n",
329 __func__, stb0899_i2cdev, stb0899_base_addr, stb0899_reg_offset, status);
335 status = i2c_transfer(state->i2c, &msg_1, 1);
339 status = i2c_transfer(state->i2c, &msg_r, 1);
343 buf_1[0] = GETBYTE(stb0899_reg_offset, BYTE1);
344 buf_1[1] = GETBYTE(stb0899_reg_offset, BYTE0);
347 status = i2c_transfer(state->i2c, &msg_1, 1);
349 if (status != -ERESTARTSYS)
350 printk(KERN_ERR "%s ERR(2), Device=[0x%04x], Base address=[0x%08x], Offset=[0x%04x], Status=%d\n",
351 __func__, stb0899_i2cdev, stb0899_base_addr, stb0899_reg_offset, status);
355 status = i2c_transfer(state->i2c, &msg_r, 1);
357 if (status != -ERESTARTSYS)
358 printk(KERN_ERR "%s ERR(3), Device=[0x%04x], Base address=[0x%08x], Offset=[0x%04x], Status=%d\n",
359 __func__, stb0899_i2cdev, stb0899_base_addr, stb0899_reg_offset, status);
360 return status < 0 ? status : -EREMOTEIO;
363 data = MAKEWORD32(buf[3], buf[2], buf[1], buf[0]);
364 if (unlikely(state->verbose >= FE_DEBUGREG))
365 printk(KERN_DEBUG "%s Device=[0x%04x], Base address=[0x%08x], Offset=[0x%04x], Data=[0x%08x]\n",
366 __func__, stb0899_i2cdev, stb0899_base_addr, stb0899_reg_offset, data);
371 return status < 0 ? status : -EREMOTEIO;
374 int stb0899_write_s2reg(struct stb0899_state *state,
376 u32 stb0899_base_addr,
377 u16 stb0899_reg_offset,
382 /* Base Address Setup */
384 GETBYTE(stb0899_i2cdev, BYTE1), /* 0xf3 S2 Base Address (MSB) */
385 GETBYTE(stb0899_i2cdev, BYTE0), /* 0xfc S2 Base Address (LSB) */
386 GETBYTE(stb0899_base_addr, BYTE0), /* 0x00 Base Address (LSB) */
387 GETBYTE(stb0899_base_addr, BYTE1), /* 0x04 Base Address (LSB) */
388 GETBYTE(stb0899_base_addr, BYTE2), /* 0x00 Base Address (MSB) */
389 GETBYTE(stb0899_base_addr, BYTE3), /* 0x00 Base Address (MSB) */
392 0x00, /* 0xf3 Reg Offset */
393 0x00, /* 0x44 Reg Offset */
400 struct i2c_msg msg_0 = {
401 .addr = state->config->demod_address,
407 struct i2c_msg msg_1 = {
408 .addr = state->config->demod_address,
414 buf_1[0] = GETBYTE(stb0899_reg_offset, BYTE1);
415 buf_1[1] = GETBYTE(stb0899_reg_offset, BYTE0);
416 buf_1[2] = GETBYTE(stb0899_data, BYTE0);
417 buf_1[3] = GETBYTE(stb0899_data, BYTE1);
418 buf_1[4] = GETBYTE(stb0899_data, BYTE2);
419 buf_1[5] = GETBYTE(stb0899_data, BYTE3);
421 if (unlikely(state->verbose >= FE_DEBUGREG))
422 printk(KERN_DEBUG "%s Device=[0x%04x], Base Address=[0x%08x], Offset=[0x%04x], Data=[0x%08x]\n",
423 __func__, stb0899_i2cdev, stb0899_base_addr, stb0899_reg_offset, stb0899_data);
425 status = i2c_transfer(state->i2c, &msg_0, 1);
426 if (unlikely(status < 1)) {
427 if (status != -ERESTARTSYS)
428 printk(KERN_ERR "%s ERR (1), 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);
432 status = i2c_transfer(state->i2c, &msg_1, 1);
433 if (unlikely(status < 1)) {
434 if (status != -ERESTARTSYS)
435 printk(KERN_ERR "%s ERR (2), Device=[0x%04x], Base Address=[0x%08x], Offset=[0x%04x], Data=[0x%08x], status=%d\n",
436 __func__, stb0899_i2cdev, stb0899_base_addr, stb0899_reg_offset, stb0899_data, status);
438 return status < 0 ? status : -EREMOTEIO;
444 return status < 0 ? status : -EREMOTEIO;
447 int stb0899_read_regs(struct stb0899_state *state, unsigned int reg, u8 *buf, size_t count)
451 u8 b0[] = { reg >> 8, reg & 0xff };
453 struct i2c_msg msg[] = {
455 .addr = state->config->demod_address,
460 .addr = state->config->demod_address,
467 status = i2c_transfer(state->i2c, msg, 2);
469 if (status != -ERESTARTSYS)
470 printk(KERN_ERR "%s Read error, Reg=[0x%04x], Count=%u, Status=%d\n",
471 __func__, reg, count, status);
476 * access to 0xf2xx/0xf6xx
477 * must be followed by read from 0xf2ff/0xf6ff.
479 if ((reg != 0xf2ff) && (reg != 0xf6ff) &&
480 (((reg & 0xff00) == 0xf200) || ((reg & 0xff00) == 0xf600)))
481 _stb0899_read_reg(state, (reg | 0x00ff));
483 if (unlikely(state->verbose >= FE_DEBUGREG)) {
486 printk(KERN_DEBUG "%s [0x%04x]:", __func__, reg);
487 for (i = 0; i < count; i++) {
488 printk(" %02x", buf[i]);
495 return status < 0 ? status : -EREMOTEIO;
498 int stb0899_write_regs(struct stb0899_state *state, unsigned int reg, u8 *data, size_t count)
502 struct i2c_msg i2c_msg = {
503 .addr = state->config->demod_address,
511 memcpy(&buf[2], data, count);
513 if (unlikely(state->verbose >= FE_DEBUGREG)) {
516 printk(KERN_DEBUG "%s [0x%04x]:", __func__, reg);
517 for (i = 0; i < count; i++)
518 printk(" %02x", data[i]);
521 ret = i2c_transfer(state->i2c, &i2c_msg, 1);
525 * access to 0xf2xx/0xf6xx
526 * must be followed by read from 0xf2ff/0xf6ff.
528 if ((((reg & 0xff00) == 0xf200) || ((reg & 0xff00) == 0xf600)))
529 stb0899_read_reg(state, (reg | 0x00ff));
532 if (ret != -ERESTARTSYS)
533 dprintk(verbose, FE_ERROR, 1, "Reg=[0x%04x], Data=[0x%02x ...], Count=%u, Status=%d",
534 reg, data[0], count, ret);
535 return ret < 0 ? ret : -EREMOTEIO;
541 int stb0899_write_reg(struct stb0899_state *state, unsigned int reg, u8 data)
543 return stb0899_write_regs(state, reg, &data, 1);
548 * Get STB0899 master clock frequency
549 * ExtClk: external clock frequency (Hz)
551 static u32 stb0899_get_mclk(struct stb0899_state *state)
553 u32 mclk = 90000000, div = 0;
555 div = stb0899_read_reg(state, STB0899_NCOARSE);
556 mclk = (div + 1) * state->config->xtal_freq / 6;
557 dprintk(verbose, FE_DEBUG, 1, "div=%d, mclk=%d", div, mclk);
564 * Set STB0899 master Clock frequency
565 * Mclk: demodulator master clock
566 * ExtClk: external clock frequency (Hz)
568 static void stb0899_set_mclk(struct stb0899_state *state, u32 Mclk)
570 struct stb0899_internal *internal = &state->internal;
573 dprintk(verbose, FE_DEBUG, 1, "state->config=%p", state->config);
574 mdiv = ((6 * Mclk) / state->config->xtal_freq) - 1;
575 dprintk(verbose, FE_DEBUG, 1, "mdiv=%d", mdiv);
577 stb0899_write_reg(state, STB0899_NCOARSE, mdiv);
578 internal->master_clk = stb0899_get_mclk(state);
580 dprintk(verbose, FE_DEBUG, 1, "MasterCLOCK=%d", internal->master_clk);
583 static void stb0899_release(struct dvb_frontend *fe)
585 struct stb0899_state *state = fe->demodulator_priv;
587 dprintk(verbose, FE_DEBUG, 1, "Release Frontend");
595 static int stb0899_get_alpha(struct stb0899_state *state)
599 mode_coeff = stb0899_read_reg(state, STB0899_DEMOD);
601 if (STB0899_GETFIELD(MODECOEFF, mode_coeff) == 1)
610 static void stb0899_init_calc(struct stb0899_state *state)
612 struct stb0899_internal *internal = &state->internal;
618 /* Read registers (in burst mode) */
619 agc1cn = stb0899_read_reg(state, STB0899_AGC1CN);
620 stb0899_read_regs(state, STB0899_AGC1REF, agc, 2); /* AGC1R and AGC2O */
622 /* Initial calculations */
623 master_clk = stb0899_get_mclk(state);
624 internal->t_agc1 = 0;
625 internal->t_agc2 = 0;
626 internal->master_clk = master_clk;
627 internal->mclk = master_clk / 65536L;
628 internal->rolloff = stb0899_get_alpha(state);
630 /* DVBS2 Initial calculations */
631 /* Set AGC value to the middle */
632 internal->agc_gain = 8154;
633 reg = STB0899_READ_S2REG(STB0899_S2DEMOD, IF_AGC_CNTRL);
634 STB0899_SETFIELD_VAL(IF_GAIN_INIT, reg, internal->agc_gain);
635 stb0899_write_s2reg(state, STB0899_S2DEMOD, STB0899_BASE_IF_AGC_CNTRL, STB0899_OFF0_IF_AGC_CNTRL, reg);
637 reg = STB0899_READ_S2REG(STB0899_S2DEMOD, RRC_ALPHA);
638 internal->rrc_alpha = STB0899_GETFIELD(RRC_ALPHA, reg);
640 internal->center_freq = 0;
641 internal->av_frame_coarse = 10;
642 internal->av_frame_fine = 20;
643 internal->step_size = 2;
645 if ((pParams->SpectralInv == FE_IQ_NORMAL) || (pParams->SpectralInv == FE_IQ_AUTO))
646 pParams->IQLocked = 0;
648 pParams->IQLocked = 1;
652 static int stb0899_wait_diseqc_fifo_empty(struct stb0899_state *state, int timeout)
655 unsigned long start = jiffies;
658 reg = stb0899_read_reg(state, STB0899_DISSTATUS);
659 if (!STB0899_GETFIELD(FIFOFULL, reg))
661 if ((jiffies - start) > timeout) {
662 dprintk(verbose, FE_ERROR, 1, "timed out !!");
670 static int stb0899_send_diseqc_msg(struct dvb_frontend *fe, struct dvb_diseqc_master_cmd *cmd)
672 struct stb0899_state *state = fe->demodulator_priv;
675 if (cmd->msg_len > 8)
678 /* enable FIFO precharge */
679 reg = stb0899_read_reg(state, STB0899_DISCNTRL1);
680 STB0899_SETFIELD_VAL(DISPRECHARGE, reg, 1);
681 stb0899_write_reg(state, STB0899_DISCNTRL1, reg);
682 for (i = 0; i < cmd->msg_len; i++) {
683 /* wait for FIFO empty */
684 if (stb0899_wait_diseqc_fifo_empty(state, 10) < 0)
687 stb0899_write_reg(state, STB0899_DISFIFO, cmd->msg[i]);
689 reg = stb0899_read_reg(state, STB0899_DISCNTRL1);
690 STB0899_SETFIELD_VAL(DISPRECHARGE, reg, 0);
691 stb0899_write_reg(state, STB0899_DISCNTRL1, reg);
696 static int stb0899_wait_diseqc_rxidle(struct stb0899_state *state, int timeout)
699 unsigned long start = jiffies;
701 while (!STB0899_GETFIELD(RXEND, reg)) {
702 reg = stb0899_read_reg(state, STB0899_DISRX_ST0);
703 if (jiffies - start > timeout) {
704 dprintk(verbose, FE_ERROR, 1, "timed out!!");
713 static int stb0899_recv_slave_reply(struct dvb_frontend *fe, struct dvb_diseqc_slave_reply *reply)
715 struct stb0899_state *state = fe->demodulator_priv;
716 u8 reg, length = 0, i;
719 if (stb0899_wait_diseqc_rxidle(state, 100) < 0)
722 reg = stb0899_read_reg(state, STB0899_DISRX_ST0);
723 if (STB0899_GETFIELD(RXEND, reg)) {
725 reg = stb0899_read_reg(state, STB0899_DISRX_ST1);
726 length = STB0899_GETFIELD(FIFOBYTENBR, reg);
728 if (length > sizeof (reply->msg)) {
732 reply->msg_len = length;
735 for (i = 0; i < length; i++)
736 reply->msg[i] = stb0899_read_reg(state, STB0899_DISFIFO);
745 static int stb0899_wait_diseqc_txidle(struct stb0899_state *state, int timeout)
748 unsigned long start = jiffies;
750 while (!STB0899_GETFIELD(TXIDLE, reg)) {
751 reg = stb0899_read_reg(state, STB0899_DISSTATUS);
752 if (jiffies - start > timeout) {
753 dprintk(verbose, FE_ERROR, 1, "timed out!!");
761 static int stb0899_send_diseqc_burst(struct dvb_frontend *fe, fe_sec_mini_cmd_t burst)
763 struct stb0899_state *state = fe->demodulator_priv;
766 /* wait for diseqc idle */
767 if (stb0899_wait_diseqc_txidle(state, 100) < 0)
770 reg = stb0899_read_reg(state, STB0899_DISCNTRL1);
772 /* set to burst mode */
773 STB0899_SETFIELD_VAL(DISEQCMODE, reg, 0x02);
774 STB0899_SETFIELD_VAL(DISPRECHARGE, reg, 0x01);
775 stb0899_write_reg(state, STB0899_DISCNTRL1, reg);
779 stb0899_write_reg(state, STB0899_DISFIFO, 0x00);
783 stb0899_write_reg(state, STB0899_DISFIFO, 0xff);
786 reg = stb0899_read_reg(state, STB0899_DISCNTRL1);
787 STB0899_SETFIELD_VAL(DISPRECHARGE, reg, 0x00);
788 stb0899_write_reg(state, STB0899_DISCNTRL1, reg);
789 /* wait for diseqc idle */
790 if (stb0899_wait_diseqc_txidle(state, 100) < 0)
794 stb0899_write_reg(state, STB0899_DISCNTRL1, old_state);
800 static int stb0899_sleep(struct dvb_frontend *fe)
802 struct stb0899_state *state = fe->demodulator_priv;
805 dprintk(verbose, FE_DEBUG, 1, "Going to Sleep .. (Really tired .. :-))");
809 static int stb0899_wakeup(struct dvb_frontend *fe)
812 struct stb0899_state *state = fe->demodulator_priv;
814 if ((rc = stb0899_write_reg(state, STB0899_SYNTCTRL, STB0899_SELOSCI)))
816 /* Activate all clocks; DVB-S2 registers are inaccessible otherwise. */
817 if ((rc = stb0899_write_reg(state, STB0899_STOPCLK1, 0x00)))
819 if ((rc = stb0899_write_reg(state, STB0899_STOPCLK2, 0x00)))
825 static int stb0899_init(struct dvb_frontend *fe)
828 struct stb0899_state *state = fe->demodulator_priv;
829 struct stb0899_config *config = state->config;
831 dprintk(verbose, FE_DEBUG, 1, "Initializing STB0899 ... ");
832 // mutex_init(&state->search_lock);
835 dprintk(verbose, FE_DEBUG, 1, "init device");
836 for (i = 0; config->init_dev[i].address != 0xffff; i++)
837 stb0899_write_reg(state, config->init_dev[i].address, config->init_dev[i].data);
839 dprintk(verbose, FE_DEBUG, 1, "init S2 demod");
841 for (i = 0; config->init_s2_demod[i].offset != 0xffff; i++)
842 stb0899_write_s2reg(state, STB0899_S2DEMOD,
843 config->init_s2_demod[i].base_address,
844 config->init_s2_demod[i].offset,
845 config->init_s2_demod[i].data);
847 dprintk(verbose, FE_DEBUG, 1, "init S1 demod");
849 for (i = 0; config->init_s1_demod[i].address != 0xffff; i++)
850 stb0899_write_reg(state, config->init_s1_demod[i].address, config->init_s1_demod[i].data);
852 dprintk(verbose, FE_DEBUG, 1, "init S2 FEC");
854 for (i = 0; config->init_s2_fec[i].offset != 0xffff; i++)
855 stb0899_write_s2reg(state, STB0899_S2FEC,
856 config->init_s2_fec[i].base_address,
857 config->init_s2_fec[i].offset,
858 config->init_s2_fec[i].data);
860 dprintk(verbose, FE_DEBUG, 1, "init TST");
862 for (i = 0; config->init_tst[i].address != 0xffff; i++)
863 stb0899_write_reg(state, config->init_tst[i].address, config->init_tst[i].data);
865 stb0899_init_calc(state);
866 // stb0899_diseqc_init(state);
871 static int stb0899_table_lookup(const struct stb0899_tab *tab, int max, int val)
876 if (val < tab[min].read)
878 else if (val >= tab[max].read)
881 while ((max - min) > 1) {
882 med = (max + min) / 2;
883 if (val >= tab[min].read && val < tab[med].read)
888 res = ((val - tab[min].read) *
889 (tab[max].real - tab[min].real) /
890 (tab[max].read - tab[min].read)) +
897 static int stb0899_read_signal_strength(struct dvb_frontend *fe, u16 *strength)
899 struct stb0899_state *state = fe->demodulator_priv;
900 struct stb0899_internal *internal = &state->internal;
904 switch (state->delsys) {
905 case DVBFE_DELSYS_DVBS:
906 case DVBFE_DELSYS_DSS:
907 if (internal->lock) {
908 reg = stb0899_read_reg(state, STB0899_VSTATUS);
909 if (STB0899_GETFIELD(VSTATUS_LOCKEDVIT, reg)) {
911 reg = stb0899_read_reg(state, STB0899_AGCIQIN);
912 val = (s32)(s8)STB0899_GETFIELD(AGCIQVALUE, reg);
914 *strength = stb0899_table_lookup(stb0899_dvbsrf_tab, ARRAY_SIZE(stb0899_dvbsrf_tab) - 1, val);
916 dprintk(verbose, FE_DEBUG, 1, "AGCIQVALUE = 0x%02x, C = %d * 0.1 dBm",
917 val & 0xff, *strength);
921 case DVBFE_DELSYS_DVBS2:
922 if (internal->lock) {
923 reg = STB0899_READ_S2REG(STB0899_DEMOD, IF_AGC_GAIN);
924 val = STB0899_GETFIELD(IF_AGC_GAIN, reg);
926 *strength = stb0899_table_lookup(stb0899_dvbs2rf_tab, ARRAY_SIZE(stb0899_dvbs2rf_tab) - 1, val);
928 dprintk(verbose, FE_DEBUG, 1, "IF_AGC_GAIN = 0x%04x, C = %d * 0.1 dBm",
929 val & 0x3fff, *strength);
933 dprintk(verbose, FE_DEBUG, 1, "Unsupported delivery system");
940 static int stb0899_read_snr(struct dvb_frontend *fe, u16 *snr)
942 struct stb0899_state *state = fe->demodulator_priv;
943 struct stb0899_internal *internal = &state->internal;
945 unsigned int val, quant, quantn = -1, est, estn = -1;
949 reg = stb0899_read_reg(state, STB0899_VSTATUS);
950 switch (state->delsys) {
951 case DVBFE_DELSYS_DVBS:
952 case DVBFE_DELSYS_DSS:
953 if (internal->lock) {
954 if (STB0899_GETFIELD(VSTATUS_LOCKEDVIT, reg)) {
956 stb0899_read_regs(state, STB0899_NIRM, buf, 2);
957 val = MAKEWORD16(buf[0], buf[1]);
959 *snr = stb0899_table_lookup(stb0899_cn_tab, ARRAY_SIZE(stb0899_cn_tab) - 1, val);
960 dprintk(verbose, FE_DEBUG, 1, "NIR = 0x%02x%02x = %u, C/N = %d * 0.1 dBm\n",
961 buf[0], buf[1], val, *snr);
965 case DVBFE_DELSYS_DVBS2:
966 if (internal->lock) {
967 reg = STB0899_READ_S2REG(STB0899_S2DEMOD, UWP_CNTRL1);
968 quant = STB0899_GETFIELD(UWP_ESN0_QUANT, reg);
969 reg = STB0899_READ_S2REG(STB0899_S2DEMOD, UWP_STAT2);
970 est = STB0899_GETFIELD(ESN0_EST, reg);
972 val = 301; /* C/N = 30.1 dB */
974 val = 270; /* C/N = 27.0 dB */
976 /* quantn = 100 * log(quant^2) */
977 quantn = stb0899_table_lookup(stb0899_quant_tab, ARRAY_SIZE(stb0899_quant_tab) - 1, quant * 100);
978 /* estn = 100 * log(est) */
979 estn = stb0899_table_lookup(stb0899_est_tab, ARRAY_SIZE(stb0899_est_tab) - 1, est);
980 /* snr(dBm/10) = -10*(log(est)-log(quant^2)) => snr(dBm/10) = (100*log(quant^2)-100*log(est))/10 */
981 val = (quantn - estn) / 10;
984 dprintk(verbose, FE_DEBUG, 1, "Es/N0 quant = %d (%d) estimate = %u (%d), C/N = %d * 0.1 dBm",
985 quant, quantn, est, estn, val);
989 dprintk(verbose, FE_DEBUG, 1, "Unsupported delivery system");
996 static int stb0899_read_status(struct dvb_frontend *fe, enum fe_status *status)
998 struct stb0899_state *state = fe->demodulator_priv;
999 struct stb0899_internal *internal = &state->internal;
1003 switch (state->delsys) {
1004 case DVBFE_DELSYS_DVBS:
1005 case DVBFE_DELSYS_DSS:
1006 dprintk(state->verbose, FE_DEBUG, 1, "Delivery system DVB-S/DSS");
1007 if (internal->lock) {
1008 reg = stb0899_read_reg(state, STB0899_VSTATUS);
1009 if (STB0899_GETFIELD(VSTATUS_LOCKEDVIT, reg)) {
1010 dprintk(state->verbose, FE_DEBUG, 1, "--------> FE_HAS_CARRIER | FE_HAS_LOCK");
1011 *status |= FE_HAS_CARRIER | FE_HAS_LOCK;
1013 reg = stb0899_read_reg(state, STB0899_PLPARM);
1014 if (STB0899_GETFIELD(VITCURPUN, reg)) {
1015 dprintk(state->verbose, FE_DEBUG, 1, "--------> FE_HAS_VITERBI | FE_HAS_SYNC");
1016 *status |= FE_HAS_VITERBI | FE_HAS_SYNC;
1021 case DVBFE_DELSYS_DVBS2:
1022 dprintk(state->verbose, FE_DEBUG, 1, "Delivery system DVB-S2");
1023 if (internal->lock) {
1024 reg = STB0899_READ_S2REG(STB0899_S2DEMOD, DMD_STAT2);
1025 if (STB0899_GETFIELD(UWP_LOCK, reg) && STB0899_GETFIELD(CSM_LOCK, reg)) {
1026 *status |= FE_HAS_CARRIER;
1027 dprintk(state->verbose, FE_DEBUG, 1,
1028 "UWP & CSM Lock ! ---> DVB-S2 FE_HAS_CARRIER");
1030 reg = stb0899_read_reg(state, STB0899_CFGPDELSTATUS1);
1031 if (STB0899_GETFIELD(CFGPDELSTATUS_LOCK, reg)) {
1032 *status |= FE_HAS_LOCK;
1033 dprintk(state->verbose, FE_DEBUG, 1,
1034 "Packet Delineator Locked ! -----> DVB-S2 FE_HAS_LOCK");
1037 if (STB0899_GETFIELD(CONTINUOUS_STREAM, reg)) {
1038 *status |= FE_HAS_VITERBI;
1039 dprintk(state->verbose, FE_DEBUG, 1,
1040 "Packet Delineator found VITERBI ! -----> DVB-S2 FE_HAS_VITERBI");
1042 if (STB0899_GETFIELD(ACCEPTED_STREAM, reg)) {
1043 *status |= FE_HAS_SYNC;
1044 dprintk(state->verbose, FE_DEBUG, 1,
1045 "Packet Delineator found SYNC ! -----> DVB-S2 FE_HAS_SYNC");
1051 dprintk(verbose, FE_DEBUG, 1, "Unsupported delivery system");
1059 * viterbi error for DVB-S/DSS
1060 * packet error for DVB-S2
1061 * Bit Error Rate or Packet Error Rate * 10 ^ 7
1063 static int stb0899_read_ber(struct dvb_frontend *fe, u32 *ber)
1065 struct stb0899_state *state = fe->demodulator_priv;
1066 struct stb0899_internal *internal = &state->internal;
1073 switch (state->delsys) {
1074 case DVBFE_DELSYS_DVBS:
1075 case DVBFE_DELSYS_DSS:
1076 if (internal->lock) {
1077 /* average 5 BER values */
1078 for (i = 0; i < 5; i++) {
1080 lsb = stb0899_read_reg(state, STB0899_ECNT1L);
1081 msb = stb0899_read_reg(state, STB0899_ECNT1M);
1082 *ber += MAKEWORD16(msb, lsb);
1086 if (STB0899_GETFIELD(VSTATUS_PRFVIT, internal->v_status)) {
1089 /* ber = ber * 10 ^ 7 */
1090 *ber /= (-1 + (1 << (2 * STB0899_GETFIELD(NOE, internal->err_ctrl))));
1095 case DVBFE_DELSYS_DVBS2:
1096 if (internal->lock) {
1097 /* Average 5 PER values */
1098 for (i = 0; i < 5; i++) {
1100 lsb = stb0899_read_reg(state, STB0899_ECNT1L);
1101 msb = stb0899_read_reg(state, STB0899_ECNT1M);
1102 *ber += MAKEWORD16(msb, lsb);
1104 /* ber = ber * 10 ^ 7 */
1106 *ber /= (-1 + (1 << (4 + 2 * STB0899_GETFIELD(NOE, internal->err_ctrl))));
1110 dprintk(verbose, FE_DEBUG, 1, "Unsupported delivery system");
1116 static int stb0899_set_voltage(struct dvb_frontend *fe, fe_sec_voltage_t voltage)
1118 struct stb0899_state *state = fe->demodulator_priv;
1121 case SEC_VOLTAGE_13:
1122 stb0899_write_reg(state, STB0899_GPIO00CFG, 0x82);
1123 stb0899_write_reg(state, STB0899_GPIO01CFG, 0x02);
1124 stb0899_write_reg(state, STB0899_GPIO02CFG, 0x00);
1126 case SEC_VOLTAGE_18:
1127 stb0899_write_reg(state, STB0899_GPIO00CFG, 0x02);
1128 stb0899_write_reg(state, STB0899_GPIO01CFG, 0x02);
1129 stb0899_write_reg(state, STB0899_GPIO02CFG, 0x82);
1131 case SEC_VOLTAGE_OFF:
1132 stb0899_write_reg(state, STB0899_GPIO00CFG, 0x82);
1133 stb0899_write_reg(state, STB0899_GPIO01CFG, 0x82);
1134 stb0899_write_reg(state, STB0899_GPIO02CFG, 0x82);
1143 static int stb0899_set_tone(struct dvb_frontend *fe, fe_sec_tone_mode_t tone)
1145 struct stb0899_state *state = fe->demodulator_priv;
1146 struct stb0899_internal *internal = &state->internal;
1150 /* wait for diseqc idle */
1151 if (stb0899_wait_diseqc_txidle(state, 100) < 0)
1156 div = (internal->master_clk / 100) / 5632;
1157 div = (div + 5) / 10;
1158 stb0899_write_reg(state, STB0899_DISEQCOCFG, 0x66);
1159 stb0899_write_reg(state, STB0899_ACRPRESC, 0x31);
1160 stb0899_write_reg(state, STB0899_ACRDIV1, div);
1163 stb0899_write_reg(state, STB0899_DISEQCOCFG, 0x20);
1171 static int stb0899_i2c_gate_ctrl(struct dvb_frontend *fe, int enable)
1174 struct stb0899_state *state = fe->demodulator_priv;
1176 i2c_stat = stb0899_read_reg(state, STB0899_I2CRPT);
1181 dprintk(state->verbose, FE_DEBUG, 1, "Enabling I2C Repeater ...");
1182 i2c_stat |= STB0899_I2CTON;
1183 if (stb0899_write_reg(state, STB0899_I2CRPT, i2c_stat) < 0)
1188 dprintk(state->verbose, FE_ERROR, 1, "I2C Repeater enable failed");
1193 static inline void CONVERT32(u32 x, char *str)
1195 *str++ = (x >> 24) & 0xff;
1196 *str++ = (x >> 16) & 0xff;
1197 *str++ = (x >> 8) & 0xff;
1198 *str++ = (x >> 0) & 0xff;
1202 int stb0899_get_dev_id(struct stb0899_state *state)
1204 u8 chip_id, release;
1206 u32 demod_ver = 0, fec_ver = 0;
1207 char demod_str[4] = { 0 };
1208 char fec_str[4] = { 0 };
1210 id = stb0899_read_reg(state, STB0899_DEV_ID);
1211 dprintk(state->verbose, FE_DEBUG, 1, "ID reg=[0x%02x]", id);
1212 chip_id = STB0899_GETFIELD(CHIP_ID, id);
1213 release = STB0899_GETFIELD(CHIP_REL, id);
1215 dprintk(state->verbose, FE_ERROR, 1, "Device ID=[%d], Release=[%d]",
1218 CONVERT32(STB0899_READ_S2REG(STB0899_S2DEMOD, DMD_CORE_ID), (char *)&demod_str);
1220 demod_ver = STB0899_READ_S2REG(STB0899_S2DEMOD, DMD_VERSION_ID);
1221 dprintk(state->verbose, FE_ERROR, 1, "Demodulator Core ID=[%s], Version=[%d]", (char *) &demod_str, demod_ver);
1222 CONVERT32(STB0899_READ_S2REG(STB0899_S2FEC, FEC_CORE_ID_REG), (char *)&fec_str);
1223 fec_ver = STB0899_READ_S2REG(STB0899_S2FEC, FEC_VER_ID_REG);
1224 if (! (chip_id > 0)) {
1225 dprintk(state->verbose, FE_ERROR, 1, "couldn't find a STB 0899");
1229 dprintk(state->verbose, FE_ERROR, 1, "FEC Core ID=[%s], Version=[%d]", (char*) &fec_str, fec_ver);
1234 static const struct dvbfe_info dvbs_info = {
1235 .name = "STB0899 DVB-S",
1236 .delivery = DVBFE_DELSYS_DVBS,
1238 .dvbs.modulation = DVBFE_MOD_QPSK,
1239 .dvbs.fec = DVBFE_FEC_1_2 | DVBFE_FEC_2_3 |
1240 DVBFE_FEC_3_4 | DVBFE_FEC_5_6 |
1244 .frequency_min = 950000,
1245 .frequency_max = 2150000,
1246 .frequency_step = 0,
1247 .symbol_rate_min = 1000000,
1248 .symbol_rate_max = 45000000,
1249 .symbol_rate_tolerance = 0
1252 static const struct dvbfe_info dss_info = {
1253 .name = "STB0899 DSS",
1254 .delivery = DVBFE_DELSYS_DSS,
1256 .dss.modulation = DVBFE_MOD_BPSK | DVBFE_MOD_QPSK,
1257 .dss.fec = DVBFE_FEC_1_2 | DVBFE_FEC_2_3 |
1258 DVBFE_FEC_3_4 | DVBFE_FEC_5_6 |
1262 .frequency_min = 950000,
1263 .frequency_max = 2150000,
1264 .frequency_step = 0,
1265 .symbol_rate_min = 1000000,
1266 .symbol_rate_max = 45000000,
1267 .symbol_rate_tolerance = 0
1270 static const struct dvbfe_info dvbs2_info = {
1271 .name = "STB0899 DVB-S2",
1272 .delivery = DVBFE_DELSYS_DVBS2,
1274 .dvbs2.modulation = DVBFE_MOD_QPSK | DVBFE_MOD_8PSK |
1275 DVBFE_MOD_16APSK | DVBFE_MOD_32APSK,
1277 .dvbs2.fec = DVBFE_FEC_1_4 | DVBFE_FEC_1_3 |
1278 DVBFE_FEC_2_5 | DVBFE_FEC_1_2 |
1279 DVBFE_FEC_3_5 | DVBFE_FEC_2_3 |
1280 DVBFE_FEC_3_4 | DVBFE_FEC_4_5 |
1281 DVBFE_FEC_5_6 | DVBFE_FEC_8_9 |
1285 .frequency_min = 950000,
1286 .frequency_max = 2150000,
1287 .frequency_step = 0,
1288 .symbol_rate_min = 1000000,
1289 .symbol_rate_max = 45000000,
1290 .symbol_rate_tolerance = 0
1293 static int stb0899_get_info(struct dvb_frontend *fe, struct dvbfe_info *fe_info)
1295 struct stb0899_state *state = fe->demodulator_priv;
1297 dprintk(verbose, FE_DEBUG, 1, "Get Info");
1299 state->delsys = fe_info->delivery;
1300 switch (state->delsys) {
1301 case DVBFE_DELSYS_DVBS:
1302 dprintk(verbose, FE_ERROR, 1, "Querying DVB-S info");
1303 memcpy(fe_info, &dvbs_info, sizeof (struct dvbfe_info));
1305 case DVBFE_DELSYS_DSS:
1306 dprintk(verbose, FE_ERROR, 1, "Querying DSS info");
1307 memcpy(fe_info, &dss_info, sizeof (struct dvbfe_info));
1309 case DVBFE_DELSYS_DVBS2:
1310 dprintk(verbose, FE_ERROR, 1, "Querying DVB-S2 info");
1311 memcpy(fe_info, &dvbs2_info, sizeof (struct dvbfe_info));
1314 dprintk(verbose, FE_ERROR, 1, "Unsupported delivery system");
1317 dprintk(verbose, FE_DEBUG, 1, "delivery system=%d", state->delsys);
1322 static int stb0899_get_delsys(struct dvb_frontend *fe, enum dvbfe_delsys *fe_delsys)
1324 *fe_delsys = DVBFE_DELSYS_DVBS | DVBFE_DELSYS_DSS | DVBFE_DELSYS_DVBS2;
1329 void stb0899_set_delsys(struct stb0899_state *state)
1334 stop_clk[0] = stb0899_read_reg(state, STB0899_STOPCLK1);
1335 stop_clk[1] = stb0899_read_reg(state, STB0899_STOPCLK2);
1337 switch (state->delsys) {
1338 case DVBFE_DELSYS_DVBS:
1339 dprintk(verbose, FE_DEBUG, 1, "Delivery System -- DVB-S");
1340 /* FECM/Viterbi ON */
1341 reg = stb0899_read_reg(state, STB0899_FECM);
1342 STB0899_SETFIELD_VAL(FECM_RSVD0, reg, 0);
1343 STB0899_SETFIELD_VAL(FECM_VITERBI_ON, reg, 1);
1344 stb0899_write_reg(state, STB0899_FECM, reg);
1346 stb0899_write_reg(state, STB0899_RSULC, 0xb1);
1347 stb0899_write_reg(state, STB0899_TSULC, 0x40);
1348 stb0899_write_reg(state, STB0899_RSLLC, 0x42);
1349 stb0899_write_reg(state, STB0899_TSLPL, 0x12);
1351 reg = stb0899_read_reg(state, STB0899_TSTRES);
1352 STB0899_SETFIELD_VAL(FRESLDPC, reg, 1);
1353 stb0899_write_reg(state, STB0899_TSTRES, reg);
1355 STB0899_SETFIELD_VAL(STOP_CHK8PSK, stop_clk[0], 1);
1356 STB0899_SETFIELD_VAL(STOP_CKFEC108, stop_clk[0], 1);
1357 STB0899_SETFIELD_VAL(STOP_CKFEC216, stop_clk[0], 1);
1359 STB0899_SETFIELD_VAL(STOP_CKPKDLIN108, stop_clk[1], 1);
1360 STB0899_SETFIELD_VAL(STOP_CKPKDLIN216, stop_clk[1], 1);
1362 STB0899_SETFIELD_VAL(STOP_CKINTBUF216, stop_clk[0], 1);
1363 STB0899_SETFIELD_VAL(STOP_CKCORE216, stop_clk[0], 1);
1365 STB0899_SETFIELD_VAL(STOP_CKS2DMD108, stop_clk[1], 1);
1367 case DVBFE_DELSYS_DVBS2:
1368 /* FECM/Viterbi OFF */
1369 reg = stb0899_read_reg(state, STB0899_FECM);
1370 STB0899_SETFIELD_VAL(FECM_RSVD0, reg, 0);
1371 STB0899_SETFIELD_VAL(FECM_VITERBI_ON, reg, 0);
1372 stb0899_write_reg(state, STB0899_FECM, reg);
1374 stb0899_write_reg(state, STB0899_RSULC, 0xb1);
1375 stb0899_write_reg(state, STB0899_TSULC, 0x42);
1376 stb0899_write_reg(state, STB0899_RSLLC, 0x40);
1377 stb0899_write_reg(state, STB0899_TSLPL, 0x02);
1379 reg = stb0899_read_reg(state, STB0899_TSTRES);
1380 STB0899_SETFIELD_VAL(FRESLDPC, reg, 0);
1381 stb0899_write_reg(state, STB0899_TSTRES, reg);
1383 STB0899_SETFIELD_VAL(STOP_CHK8PSK, stop_clk[0], 1);
1384 STB0899_SETFIELD_VAL(STOP_CKFEC108, stop_clk[0], 0);
1385 STB0899_SETFIELD_VAL(STOP_CKFEC216, stop_clk[0], 0);
1387 STB0899_SETFIELD_VAL(STOP_CKPKDLIN108, stop_clk[1], 0);
1388 STB0899_SETFIELD_VAL(STOP_CKPKDLIN216, stop_clk[1], 0);
1390 STB0899_SETFIELD_VAL(STOP_CKINTBUF216, stop_clk[0], 0);
1391 STB0899_SETFIELD_VAL(STOP_CKCORE216, stop_clk[0], 0);
1393 STB0899_SETFIELD_VAL(STOP_CKS2DMD108, stop_clk[1], 0);
1395 case DVBFE_DELSYS_DSS:
1396 /* FECM/Viterbi ON */
1397 reg = stb0899_read_reg(state, STB0899_FECM);
1398 STB0899_SETFIELD_VAL(FECM_RSVD0, reg, 1);
1399 STB0899_SETFIELD_VAL(FECM_VITERBI_ON, reg, 1);
1400 stb0899_write_reg(state, STB0899_FECM, reg);
1402 stb0899_write_reg(state, STB0899_RSULC, 0xa1);
1403 stb0899_write_reg(state, STB0899_TSULC, 0x61);
1404 stb0899_write_reg(state, STB0899_RSLLC, 0x42);
1406 reg = stb0899_read_reg(state, STB0899_TSTRES);
1407 STB0899_SETFIELD_VAL(FRESLDPC, reg, 1);
1408 stb0899_write_reg(state, STB0899_TSTRES, reg);
1410 STB0899_SETFIELD_VAL(STOP_CHK8PSK, stop_clk[0], 1);
1411 STB0899_SETFIELD_VAL(STOP_CKFEC108, stop_clk[0], 1);
1412 STB0899_SETFIELD_VAL(STOP_CKFEC216, stop_clk[0], 1);
1414 STB0899_SETFIELD_VAL(STOP_CKPKDLIN108, stop_clk[1], 1);
1415 STB0899_SETFIELD_VAL(STOP_CKPKDLIN216, stop_clk[1], 1);
1417 STB0899_SETFIELD_VAL(STOP_CKCORE216, stop_clk[0], 0);
1419 STB0899_SETFIELD_VAL(STOP_CKS2DMD108, stop_clk[1], 1);
1422 dprintk(verbose, FE_ERROR, 1, "Unsupported delivery system");
1425 STB0899_SETFIELD_VAL(STOP_CKADCI108, stop_clk[0], 0);
1426 stb0899_write_regs(state, STB0899_STOPCLK1, stop_clk, 2);
1430 * stb0899_set_iterations
1431 * set the LDPC iteration scale function
1433 static void stb0899_set_iterations(struct stb0899_state *state)
1435 struct stb0899_internal *internal = &state->internal;
1436 struct stb0899_config *config = state->config;
1441 iter_scale = 17 * (internal->master_clk / 1000);
1442 iter_scale += 410000;
1443 iter_scale /= (internal->srate / 1000000);
1446 if (iter_scale > config->ldpc_max_iter)
1447 iter_scale = config->ldpc_max_iter;
1449 reg = STB0899_READ_S2REG(STB0899_S2DEMOD, MAX_ITER);
1450 STB0899_SETFIELD_VAL(MAX_ITERATIONS, reg, iter_scale);
1451 stb0899_write_s2reg(state, STB0899_S2DEMOD, STB0899_BASE_MAX_ITER, STB0899_OFF0_MAX_ITER, reg);
1454 static enum dvbfe_search stb0899_search(struct dvb_frontend *fe, struct dvbfe_params *params)
1456 struct stb0899_state *state = fe->demodulator_priv;
1457 struct stb0899_params *i_params = &state->params;
1458 struct stb0899_internal *internal = &state->internal;
1460 u32 SearchRange, gain;
1462 switch (state->delsys) {
1463 case DVBFE_DELSYS_DVBS:
1464 dprintk(verbose, FE_ERROR, 1, "set DVB-S params");
1465 i_params->freq = params->frequency;
1466 i_params->srate = params->delsys.dvbs.symbol_rate;
1468 case DVBFE_DELSYS_DSS:
1469 dprintk(verbose, FE_ERROR, 1, "set DSS params");
1470 i_params->freq = params->frequency;
1471 i_params->srate = params->delsys.dss.symbol_rate;
1473 case DVBFE_DELSYS_DVBS2:
1474 dprintk(verbose, FE_ERROR, 1, "set DVB-S2 params");
1475 i_params->freq = params->frequency;
1476 i_params->srate = params->delsys.dvbs2.symbol_rate;
1479 dprintk(verbose, FE_ERROR, 1, "Unsupported delivery system");
1482 dprintk(verbose, FE_DEBUG, 1, "delivery system=%d", state->delsys);
1484 // SearchRange = 3000000; /* Search Bandwidth (3 Mhz, was initially 10 Mhz) */
1485 SearchRange = 10000000; /* Search Bandwidth (3 Mhz, was initially 10 Mhz) */
1486 dprintk(verbose, FE_DEBUG, 1, "Frequency=%d, Srate=%d", i_params->freq, i_params->srate);
1487 /* checking Search Range is meaningless for a fixed 3 Mhz */
1488 if (INRANGE(i_params->srate, 1000000, 45000000)) {
1489 dprintk(verbose, FE_DEBUG, 1, "Parameters IN RANGE");
1490 stb0899_set_delsys(state);
1492 if (state->config->tuner_set_rfsiggain) {
1493 if (internal->srate > 15000000)
1494 gain = 8; /* 15Mb < srate < 45Mb, gain = 8dB */
1495 else if (internal->srate > 5000000)
1496 gain = 12; /* 5Mb < srate < 15Mb, gain = 12dB */
1498 gain = 14; /* 1Mb < srate < 5Mb, gain = 14db */
1499 state->config->tuner_set_rfsiggain(fe, gain);
1502 if (i_params->srate <= 5000000)
1503 stb0899_set_mclk(state, 76500000);
1505 stb0899_set_mclk(state, 90000000);
1507 switch (state->delsys) {
1508 case DVBFE_DELSYS_DVBS:
1509 case DVBFE_DELSYS_DSS:
1510 dprintk(verbose, FE_DEBUG, 1, "DVB-S delivery system");
1511 internal->freq = i_params->freq;
1512 internal->srate = i_params->srate;
1514 * search = user search range +
1516 * 2 * Tuner_step_size +
1517 * 10% of the symbol rate
1519 internal->srch_range = SearchRange + 1500000 + (i_params->srate / 5);
1520 internal->derot_percent = 30;
1522 /* What to do for tuners having no bandwidth setup ? */
1523 if (state->config->tuner_set_bandwidth)
1524 state->config->tuner_set_bandwidth(fe, (13 * (stb0899_carr_width(state) + 10000000)) / 10);
1525 if (state->config->tuner_get_bandwidth)
1526 state->config->tuner_get_bandwidth(fe, &internal->tuner_bw);
1527 /* Set DVB-S1 AGC */
1528 stb0899_write_reg(state, STB0899_AGCRFCFG, 0x11);
1530 /* Run the search algorithm */
1531 dprintk(verbose, FE_DEBUG, 1, "running DVB-S search algo ..");
1532 if (stb0899_dvbs_algo(state) == RANGEOK) {
1534 dprintk(verbose, FE_DEBUG, 1,
1535 "-------------------------------------> DVB-S LOCK !");
1537 // stb0899_write_reg(state, STB0899_ERRCTRL1, 0x3d); /* Viterbi Errors */
1538 // internal->v_status = stb0899_read_reg(state, STB0899_VSTATUS);
1539 // internal->err_ctrl = stb0899_read_reg(state, STB0899_ERRCTRL1);
1540 // dprintk(verbose, FE_DEBUG, 1, "VSTATUS=0x%02x", internal->v_status);
1541 // dprintk(verbose, FE_DEBUG, 1, "ERR_CTRL=0x%02x", internal->err_ctrl);
1543 return DVBFE_ALGO_SEARCH_SUCCESS;
1547 return DVBFE_ALGO_SEARCH_FAILED;
1550 case DVBFE_DELSYS_DVBS2:
1551 internal->freq = i_params->freq;
1552 internal->srate = i_params->srate;
1553 internal->srch_range = SearchRange;
1555 if (state->config->tuner_set_bandwidth)
1556 state->config->tuner_set_bandwidth(fe, (stb0899_carr_width(state) + 10000000));
1557 if (state->config->tuner_get_bandwidth)
1558 state->config->tuner_get_bandwidth(fe, &internal->tuner_bw);
1560 // pParams->SpectralInv = pSearch->IQ_Inversion;
1562 /* Set DVB-S2 AGC */
1563 stb0899_write_reg(state, STB0899_AGCRFCFG, 0x1c);
1565 /* Set IterScale =f(MCLK,SYMB) */
1566 stb0899_set_iterations(state);
1568 /* Run the search algorithm */
1569 dprintk(verbose, FE_DEBUG, 1, "running DVB-S2 search algo ..");
1570 if (stb0899_dvbs2_algo(state) == DVBS2_FEC_LOCK) {
1572 dprintk(verbose, FE_DEBUG, 1,
1573 "-------------------------------------> DVB-S2 LOCK !");
1575 // stb0899_write_reg(state, STB0899_ERRCTRL1, 0xb6); /* Packet Errors */
1576 // internal->v_status = stb0899_read_reg(state, STB0899_VSTATUS);
1577 // internal->err_ctrl = stb0899_read_reg(state, STB0899_ERRCTRL1);
1579 return DVBFE_ALGO_SEARCH_SUCCESS;
1583 return DVBFE_ALGO_SEARCH_FAILED;
1587 dprintk(verbose, FE_ERROR, 1, "Unsupported delivery system");
1588 return DVBFE_ALGO_SEARCH_INVALID;
1592 return DVBFE_ALGO_SEARCH_ERROR;
1595 static enum stb0899_status stb0899_track_carrier(struct stb0899_state *state)
1599 reg = stb0899_read_reg(state, STB0899_DSTATUS);
1600 dprintk(verbose, FE_DEBUG, 1, "--------------------> STB0899_DSTATUS=[0x%02x]", reg);
1601 if (STB0899_GETFIELD(CARRIER_FOUND, reg)) {
1602 dprintk(verbose, FE_DEBUG, 1, "-------------> CARRIEROK !");
1605 dprintk(verbose, FE_DEBUG, 1, "-------------> NOCARRIER !");
1612 static enum stb0899_status stb0899_get_ifagc(struct stb0899_state *state)
1616 reg = STB0899_READ_S2REG(STB0899_S2DEMOD, DMD_STATUS);
1617 dprintk(verbose, FE_DEBUG, 1, "DMD_STATUS=[0x%02x]", reg);
1618 if (STB0899_GETFIELD(IF_AGC_LOCK, reg)) {
1619 dprintk(verbose, FE_DEBUG, 1, "------------->IF AGC LOCKED !");
1622 dprintk(verbose, FE_DEBUG, 1, "------------->IF AGC LOCK LOST !");
1629 static int stb0899_get_s1fec(struct stb0899_internal *internal, enum dvbfe_fec *fec)
1631 switch (internal->fecrate) {
1632 case STB0899_FEC_1_2:
1633 *fec = DVBFE_FEC_1_2;
1635 case STB0899_FEC_2_3:
1636 *fec = DVBFE_FEC_2_3;
1638 case STB0899_FEC_3_4:
1639 *fec = DVBFE_FEC_3_4;
1641 case STB0899_FEC_5_6:
1642 *fec = DVBFE_FEC_5_6;
1644 case STB0899_FEC_6_7:
1645 *fec = DVBFE_FEC_6_7;
1647 case STB0899_FEC_7_8:
1648 *fec = DVBFE_FEC_7_8;
1657 static int stb0899_get_modcod(struct stb0899_internal *internal, struct dvbs2_params *params)
1659 switch (internal->modcod) {
1660 case STB0899_DUMMY_PLF:
1661 params->modulation = DVBFE_MOD_NONE;
1662 params->fec = DVBFE_FEC_NONE;
1664 case STB0899_QPSK_14:
1665 params->modulation = DVBFE_MOD_QPSK;
1666 params->fec = DVBFE_FEC_1_4;
1668 case STB0899_QPSK_13:
1669 params->modulation = DVBFE_MOD_QPSK;
1670 params->fec = DVBFE_FEC_1_3;
1672 case STB0899_QPSK_25:
1673 params->modulation = DVBFE_MOD_QPSK;
1674 params->fec = DVBFE_FEC_2_5;
1676 case STB0899_QPSK_12:
1677 params->modulation = DVBFE_MOD_QPSK;
1678 params->fec = DVBFE_FEC_1_2;
1680 case STB0899_QPSK_35:
1681 params->modulation = DVBFE_MOD_QPSK;
1682 params->fec = DVBFE_FEC_3_5;
1684 case STB0899_QPSK_23:
1685 params->modulation = DVBFE_MOD_QPSK;
1686 params->fec = DVBFE_FEC_2_3;
1688 case STB0899_QPSK_34:
1689 params->modulation = DVBFE_MOD_QPSK;
1690 params->fec = DVBFE_FEC_3_4;
1692 case STB0899_QPSK_45:
1693 params->modulation = DVBFE_MOD_QPSK;
1694 params->fec = DVBFE_FEC_4_5;
1696 case STB0899_QPSK_56:
1697 params->modulation = DVBFE_MOD_QPSK;
1698 params->fec = DVBFE_FEC_5_6;
1700 case STB0899_QPSK_89:
1701 params->modulation = DVBFE_MOD_QPSK;
1702 params->fec = DVBFE_FEC_8_9;
1704 case STB0899_QPSK_910:
1705 params->modulation = DVBFE_MOD_QPSK;
1706 params->fec = DVBFE_FEC_9_10;
1708 case STB0899_8PSK_35:
1709 params->modulation = DVBFE_MOD_8PSK;
1710 params->fec = DVBFE_FEC_3_5;
1712 case STB0899_8PSK_23:
1713 params->modulation = DVBFE_MOD_8PSK;
1714 params->fec = DVBFE_FEC_2_3;
1716 case STB0899_8PSK_34:
1717 params->modulation = DVBFE_MOD_8PSK;
1718 params->fec = DVBFE_FEC_3_4;
1720 case STB0899_8PSK_56:
1721 params->modulation = DVBFE_MOD_8PSK;
1722 params->fec = DVBFE_FEC_5_6;
1724 case STB0899_8PSK_89:
1725 params->modulation = DVBFE_MOD_8PSK;
1726 params->fec = DVBFE_FEC_8_9;
1728 case STB0899_8PSK_910:
1729 params->modulation = DVBFE_MOD_8PSK;
1730 params->fec = DVBFE_FEC_9_10;
1732 case STB0899_16APSK_23:
1733 params->modulation = DVBFE_MOD_16APSK;
1734 params->fec = DVBFE_FEC_2_3;
1736 case STB0899_16APSK_34:
1737 params->modulation = DVBFE_MOD_16APSK;
1738 params->fec = DVBFE_FEC_3_4;
1740 case STB0899_16APSK_45:
1741 params->modulation = DVBFE_MOD_16APSK;
1742 params->fec = DVBFE_FEC_4_5;
1744 case STB0899_16APSK_56:
1745 params->modulation = DVBFE_MOD_16APSK;
1746 params->fec = DVBFE_FEC_5_6;
1748 case STB0899_16APSK_89:
1749 params->modulation = DVBFE_MOD_16APSK;
1750 params->fec = DVBFE_FEC_8_9;
1752 case STB0899_16APSK_910:
1753 params->modulation = DVBFE_MOD_16APSK;
1754 params->fec = DVBFE_FEC_9_10;
1756 case STB0899_32APSK_34:
1757 params->modulation = DVBFE_MOD_32APSK;
1758 params->fec = DVBFE_FEC_3_4;
1760 case STB0899_32APSK_45:
1761 params->modulation = DVBFE_MOD_32APSK;
1762 params->fec = DVBFE_FEC_4_5;
1764 case STB0899_32APSK_56:
1765 params->modulation = DVBFE_MOD_32APSK;
1766 params->fec = DVBFE_FEC_5_6;
1768 case STB0899_32APSK_89:
1769 params->modulation = DVBFE_MOD_32APSK;
1770 params->fec = DVBFE_FEC_8_9;
1772 case STB0899_32APSK_910:
1773 params->modulation = DVBFE_MOD_32APSK;
1774 params->fec = DVBFE_FEC_9_10;
1785 * periodically check the signal level against a specified
1786 * threshold level and perform derotator centering.
1787 * called once we have a lock from a succesful search
1790 * Will be called periodically called to maintain the
1793 * Will be used to get parameters as well as info from
1794 * the decoded baseband header
1796 * Once a new lock has established, the internal state
1797 * frequency (internal->freq) is updated
1799 static int stb0899_track(struct dvb_frontend *fe, struct dvbfe_params *params, int *delay)
1803 struct stb0899_state *state = fe->demodulator_priv;
1804 struct stb0899_internal *internal = &state->internal;
1806 lock_lost = STB0899_READ_S2REG(STB0899_S2DEMOD, LOCK_LOST);
1807 dprintk(verbose, FE_DEBUG, 1, "Lock Lost=[0x%02x]\n", lock_lost);
1808 if (STB0899_GETFIELD(LOCK_LOST, lock_lost))
1809 dprintk(verbose, FE_ERROR, 1, "Demodulator LOST LOCK !\n");
1816 static int stb0899_get_params(struct dvb_frontend *fe, struct dvbfe_params *params)
1818 struct stb0899_state *state = fe->demodulator_priv;
1819 struct stb0899_internal *internal = &state->internal;
1821 params->frequency = internal->freq;
1822 params->inversion = internal->inversion;
1823 params->delivery = state->delsys;
1824 switch (state->delsys) {
1825 case DVBFE_DELSYS_DVBS:
1826 dprintk(verbose, FE_DEBUG, 1, "Get DVB-S params");
1827 params->delsys.dvbs.symbol_rate = internal->srate;
1828 params->delsys.dvbs.modulation = DVBFE_MOD_QPSK;
1830 case DVBFE_DELSYS_DSS:
1831 dprintk(verbose, FE_DEBUG, 1, "Get DSS params");
1832 params->delsys.dss.symbol_rate = internal->srate;
1833 params->delsys.dss.modulation = DVBFE_MOD_QPSK;
1835 case DVBFE_DELSYS_DVBS2:
1836 dprintk(verbose, FE_DEBUG, 1, "Get DVB-S2 params");
1837 params->delsys.dvbs2.symbol_rate = internal->srate;
1840 dprintk(verbose, FE_ERROR, 1, "Unsupported delivery system");
1847 static enum dvbfe_algo stb0899_frontend_algo(struct dvb_frontend *fe)
1849 return DVBFE_ALGO_CUSTOM;
1852 static struct dvb_frontend_ops stb0899_ops = {
1855 .name = "STB0899 Multistandard",
1858 .release = stb0899_release,
1859 .init = stb0899_init,
1860 .sleep = stb0899_sleep,
1861 // .wakeup = stb0899_wakeup,
1863 .i2c_gate_ctrl = stb0899_i2c_gate_ctrl,
1864 .get_info = stb0899_get_info,
1865 .get_delsys = stb0899_get_delsys,
1867 .get_frontend_algo = stb0899_frontend_algo,
1868 .search = stb0899_search,
1869 .track = stb0899_track,
1870 .get_params = stb0899_get_params,
1872 .read_status = stb0899_read_status,
1873 .read_snr = stb0899_read_snr,
1874 .read_signal_strength = stb0899_read_signal_strength,
1875 .read_status = stb0899_read_status,
1876 .read_ber = stb0899_read_ber,
1878 .set_voltage = stb0899_set_voltage,
1879 .set_tone = stb0899_set_tone,
1881 .diseqc_send_master_cmd = stb0899_send_diseqc_msg,
1882 .diseqc_recv_slave_reply = stb0899_recv_slave_reply,
1883 .diseqc_send_burst = stb0899_send_diseqc_burst,
1886 struct dvb_frontend *stb0899_attach(struct stb0899_config *config, struct i2c_adapter *i2c)
1888 struct stb0899_state *state = NULL;
1890 state = kzalloc(sizeof (struct stb0899_state), GFP_KERNEL);
1894 state->verbose = verbose;
1895 state->config = config;
1897 state->frontend.ops = stb0899_ops;
1898 state->frontend.demodulator_priv = state;
1899 state->internal.inversion = IQ_SWAP_AUTO;
1901 stb0899_wakeup(&state->frontend);
1902 if (stb0899_get_dev_id(state) == -ENODEV) {
1903 printk("%s: Exiting .. !\n", __func__);
1907 printk("%s: Attaching STB0899 \n", __func__);
1908 return &state->frontend;
1914 EXPORT_SYMBOL(stb0899_attach);
1915 MODULE_PARM_DESC(verbose, "Set Verbosity level");
1916 MODULE_AUTHOR("Manu Abraham");
1917 MODULE_DESCRIPTION("STB0899 Multi-Std frontend");
1918 MODULE_LICENSE("GPL");