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/jiffies.h>
24 #include <linux/kernel.h>
25 #include <linux/module.h>
26 #include <linux/slab.h>
27 #include <linux/string.h>
29 #include <linux/dvb/frontend.h>
30 #include "dvb_frontend.h"
32 #include "stb0899_drv.h"
33 #include "stb0899_priv.h"
34 #include "stb0899_reg.h"
36 /* Max transfer size done by I2C transfer functions */
37 #define MAX_XFER_SIZE 64
39 static unsigned int verbose = 0;//1;
40 module_param(verbose, int, 0644);
42 /* C/N in dB/10, NIRM/NIRL */
43 static const struct stb0899_tab stb0899_cn_tab[] = {
66 /* DVB-S AGCIQ_VALUE vs. signal level in dBm/10.
67 * As measured, connected to a modulator.
68 * -8.0 to -50.0 dBm directly connected,
69 * -52.0 to -74.8 with extra attenuation.
70 * Cut-off to AGCIQ_VALUE = 0x80 below -74.8dBm.
71 * Crude linear extrapolation below -84.8dBm and above -8.0dBm.
73 static const struct stb0899_tab stb0899_dvbsrf_tab[] = {
107 /* DVB-S2 IF_AGC_GAIN vs. signal level in dBm/10.
108 * As measured, connected to a modulator.
109 * -8.0 to -50.1 dBm directly connected,
110 * -53.0 to -76.6 with extra attenuation.
111 * Cut-off to IF_AGC_GAIN = 0x3fff below -76.6dBm.
112 * Crude linear extrapolation below -76.6dBm and above -8.0dBm.
114 static const struct stb0899_tab stb0899_dvbs2rf_tab[] = {
141 /* DVB-S2 Es/N0 quant in dB/100 vs read value * 100*/
142 static struct stb0899_tab stb0899_quant_tab[] = {
184 /* DVB-S2 Es/N0 estimate in dB/100 vs read value */
185 static struct stb0899_tab stb0899_est_tab[] = {
225 static int _stb0899_read_reg(struct stb0899_state *state, unsigned int reg)
229 u8 b0[] = { reg >> 8, reg & 0xff };
232 struct i2c_msg msg[] = {
234 .addr = state->config->demod_address,
239 .addr = state->config->demod_address,
246 ret = i2c_transfer(state->i2c, msg, 2);
248 if (ret != -ERESTARTSYS)
249 dprintk(state->verbose, FE_ERROR, 1,
250 "Read error, Reg=[0x%02x], Status=%d",
253 return ret < 0 ? ret : -EREMOTEIO;
255 if (unlikely(*state->verbose >= FE_DEBUGREG))
256 dprintk(state->verbose, FE_ERROR, 1, "Reg=[0x%02x], data=%02x",
259 return (unsigned int)buf;
262 int stb0899_read_reg(struct stb0899_state *state, unsigned int reg)
266 result = _stb0899_read_reg(state, reg);
269 * access to 0xf2xx/0xf6xx
270 * must be followed by read from 0xf2ff/0xf6ff.
272 if ((reg != 0xf2ff) && (reg != 0xf6ff) &&
273 (((reg & 0xff00) == 0xf200) || ((reg & 0xff00) == 0xf600)))
274 _stb0899_read_reg(state, (reg | 0x00ff));
279 u32 _stb0899_read_s2reg(struct stb0899_state *state,
281 u32 stb0899_base_addr,
282 u16 stb0899_reg_offset)
290 GETBYTE(stb0899_i2cdev, BYTE1), /* 0xf3 S2 Base Address (MSB) */
291 GETBYTE(stb0899_i2cdev, BYTE0), /* 0xfc S2 Base Address (LSB) */
292 GETBYTE(stb0899_base_addr, BYTE0), /* 0x00 Base Address (LSB) */
293 GETBYTE(stb0899_base_addr, BYTE1), /* 0x04 Base Address (LSB) */
294 GETBYTE(stb0899_base_addr, BYTE2), /* 0x00 Base Address (MSB) */
295 GETBYTE(stb0899_base_addr, BYTE3), /* 0x00 Base Address (MSB) */
298 0x00, /* 0xf3 Reg Offset */
299 0x00, /* 0x44 Reg Offset */
302 struct i2c_msg msg_0 = {
303 .addr = state->config->demod_address,
309 struct i2c_msg msg_1 = {
310 .addr = state->config->demod_address,
316 struct i2c_msg msg_r = {
317 .addr = state->config->demod_address,
323 tmpaddr = stb0899_reg_offset & 0xff00;
324 if (!(stb0899_reg_offset & 0x8))
325 tmpaddr = stb0899_reg_offset | 0x20;
327 buf_1[0] = GETBYTE(tmpaddr, BYTE1);
328 buf_1[1] = GETBYTE(tmpaddr, BYTE0);
330 status = i2c_transfer(state->i2c, &msg_0, 1);
332 if (status != -ERESTARTSYS)
333 printk(KERN_ERR "%s ERR(1), Device=[0x%04x], Base address=[0x%08x], Offset=[0x%04x], Status=%d\n",
334 __func__, stb0899_i2cdev, stb0899_base_addr, stb0899_reg_offset, status);
340 status = i2c_transfer(state->i2c, &msg_1, 1);
344 status = i2c_transfer(state->i2c, &msg_r, 1);
348 buf_1[0] = GETBYTE(stb0899_reg_offset, BYTE1);
349 buf_1[1] = GETBYTE(stb0899_reg_offset, BYTE0);
352 status = i2c_transfer(state->i2c, &msg_1, 1);
354 if (status != -ERESTARTSYS)
355 printk(KERN_ERR "%s ERR(2), Device=[0x%04x], Base address=[0x%08x], Offset=[0x%04x], Status=%d\n",
356 __func__, stb0899_i2cdev, stb0899_base_addr, stb0899_reg_offset, status);
360 status = i2c_transfer(state->i2c, &msg_r, 1);
362 if (status != -ERESTARTSYS)
363 printk(KERN_ERR "%s ERR(3), Device=[0x%04x], Base address=[0x%08x], Offset=[0x%04x], Status=%d\n",
364 __func__, stb0899_i2cdev, stb0899_base_addr, stb0899_reg_offset, status);
365 return status < 0 ? status : -EREMOTEIO;
368 data = MAKEWORD32(buf[3], buf[2], buf[1], buf[0]);
369 if (unlikely(*state->verbose >= FE_DEBUGREG))
370 printk(KERN_DEBUG "%s Device=[0x%04x], Base address=[0x%08x], Offset=[0x%04x], Data=[0x%08x]\n",
371 __func__, stb0899_i2cdev, stb0899_base_addr, stb0899_reg_offset, data);
376 return status < 0 ? status : -EREMOTEIO;
379 int stb0899_write_s2reg(struct stb0899_state *state,
381 u32 stb0899_base_addr,
382 u16 stb0899_reg_offset,
387 /* Base Address Setup */
389 GETBYTE(stb0899_i2cdev, BYTE1), /* 0xf3 S2 Base Address (MSB) */
390 GETBYTE(stb0899_i2cdev, BYTE0), /* 0xfc S2 Base Address (LSB) */
391 GETBYTE(stb0899_base_addr, BYTE0), /* 0x00 Base Address (LSB) */
392 GETBYTE(stb0899_base_addr, BYTE1), /* 0x04 Base Address (LSB) */
393 GETBYTE(stb0899_base_addr, BYTE2), /* 0x00 Base Address (MSB) */
394 GETBYTE(stb0899_base_addr, BYTE3), /* 0x00 Base Address (MSB) */
397 0x00, /* 0xf3 Reg Offset */
398 0x00, /* 0x44 Reg Offset */
405 struct i2c_msg msg_0 = {
406 .addr = state->config->demod_address,
412 struct i2c_msg msg_1 = {
413 .addr = state->config->demod_address,
419 buf_1[0] = GETBYTE(stb0899_reg_offset, BYTE1);
420 buf_1[1] = GETBYTE(stb0899_reg_offset, BYTE0);
421 buf_1[2] = GETBYTE(stb0899_data, BYTE0);
422 buf_1[3] = GETBYTE(stb0899_data, BYTE1);
423 buf_1[4] = GETBYTE(stb0899_data, BYTE2);
424 buf_1[5] = GETBYTE(stb0899_data, BYTE3);
426 if (unlikely(*state->verbose >= FE_DEBUGREG))
427 printk(KERN_DEBUG "%s Device=[0x%04x], Base Address=[0x%08x], Offset=[0x%04x], Data=[0x%08x]\n",
428 __func__, stb0899_i2cdev, stb0899_base_addr, stb0899_reg_offset, stb0899_data);
430 status = i2c_transfer(state->i2c, &msg_0, 1);
431 if (unlikely(status < 1)) {
432 if (status != -ERESTARTSYS)
433 printk(KERN_ERR "%s ERR (1), Device=[0x%04x], Base Address=[0x%08x], Offset=[0x%04x], Data=[0x%08x], status=%d\n",
434 __func__, stb0899_i2cdev, stb0899_base_addr, stb0899_reg_offset, stb0899_data, status);
437 status = i2c_transfer(state->i2c, &msg_1, 1);
438 if (unlikely(status < 1)) {
439 if (status != -ERESTARTSYS)
440 printk(KERN_ERR "%s ERR (2), Device=[0x%04x], Base Address=[0x%08x], Offset=[0x%04x], Data=[0x%08x], status=%d\n",
441 __func__, stb0899_i2cdev, stb0899_base_addr, stb0899_reg_offset, stb0899_data, status);
443 return status < 0 ? status : -EREMOTEIO;
449 return status < 0 ? status : -EREMOTEIO;
452 int stb0899_read_regs(struct stb0899_state *state, unsigned int reg, u8 *buf, u32 count)
456 u8 b0[] = { reg >> 8, reg & 0xff };
458 struct i2c_msg msg[] = {
460 .addr = state->config->demod_address,
465 .addr = state->config->demod_address,
472 status = i2c_transfer(state->i2c, msg, 2);
474 if (status != -ERESTARTSYS)
475 printk(KERN_ERR "%s Read error, Reg=[0x%04x], Count=%u, Status=%d\n",
476 __func__, reg, count, status);
481 * access to 0xf2xx/0xf6xx
482 * must be followed by read from 0xf2ff/0xf6ff.
484 if ((reg != 0xf2ff) && (reg != 0xf6ff) &&
485 (((reg & 0xff00) == 0xf200) || ((reg & 0xff00) == 0xf600)))
486 _stb0899_read_reg(state, (reg | 0x00ff));
488 dprintk(state->verbose, FE_DEBUGREG, 1,
489 "%s [0x%04x]: %*ph", __func__, reg, count, buf);
493 return status < 0 ? status : -EREMOTEIO;
496 int stb0899_write_regs(struct stb0899_state *state, unsigned int reg, u8 *data, u32 count)
499 u8 buf[MAX_XFER_SIZE];
500 struct i2c_msg i2c_msg = {
501 .addr = state->config->demod_address,
507 if (2 + count > sizeof(buf)) {
509 "%s: i2c wr reg=%04x: len=%d is too big!\n",
510 KBUILD_MODNAME, reg, count);
516 memcpy(&buf[2], data, count);
518 dprintk(state->verbose, FE_DEBUGREG, 1,
519 "%s [0x%04x]: %*ph", __func__, reg, count, data);
520 ret = i2c_transfer(state->i2c, &i2c_msg, 1);
524 * access to 0xf2xx/0xf6xx
525 * must be followed by read from 0xf2ff/0xf6ff.
527 if ((((reg & 0xff00) == 0xf200) || ((reg & 0xff00) == 0xf600)))
528 stb0899_read_reg(state, (reg | 0x00ff));
531 if (ret != -ERESTARTSYS)
532 dprintk(state->verbose, FE_ERROR, 1, "Reg=[0x%04x], Data=[0x%02x ...], Count=%u, Status=%d",
533 reg, data[0], count, ret);
534 return ret < 0 ? ret : -EREMOTEIO;
540 int stb0899_write_reg(struct stb0899_state *state, unsigned int reg, u8 data)
542 return stb0899_write_regs(state, reg, &data, 1);
547 * Get STB0899 master clock frequency
548 * ExtClk: external clock frequency (Hz)
550 static u32 stb0899_get_mclk(struct stb0899_state *state)
552 u32 mclk = 0, div = 0;
554 div = stb0899_read_reg(state, STB0899_NCOARSE);
555 mclk = (div + 1) * state->config->xtal_freq / 6;
556 dprintk(state->verbose, FE_DEBUG, 1, "div=%d, mclk=%d", div, mclk);
563 * Set STB0899 master Clock frequency
564 * Mclk: demodulator master clock
565 * ExtClk: external clock frequency (Hz)
567 static void stb0899_set_mclk(struct stb0899_state *state, u32 Mclk)
569 struct stb0899_internal *internal = &state->internal;
572 dprintk(state->verbose, FE_DEBUG, 1, "state->config=%p", state->config);
573 mdiv = ((6 * Mclk) / state->config->xtal_freq) - 1;
574 dprintk(state->verbose, FE_DEBUG, 1, "mdiv=%d", mdiv);
576 stb0899_write_reg(state, STB0899_NCOARSE, mdiv);
577 internal->master_clk = stb0899_get_mclk(state);
579 dprintk(state->verbose, FE_DEBUG, 1, "MasterCLOCK=%d", internal->master_clk);
582 static int stb0899_postproc(struct stb0899_state *state, u8 ctl, int enable)
584 struct stb0899_config *config = state->config;
585 const struct stb0899_postproc *postproc = config->postproc;
587 /* post process event */
590 if (postproc[ctl].level == STB0899_GPIOPULLUP)
591 stb0899_write_reg(state, postproc[ctl].gpio, 0x02);
593 stb0899_write_reg(state, postproc[ctl].gpio, 0x82);
595 if (postproc[ctl].level == STB0899_GPIOPULLUP)
596 stb0899_write_reg(state, postproc[ctl].gpio, 0x82);
598 stb0899_write_reg(state, postproc[ctl].gpio, 0x02);
604 static void stb0899_detach(struct dvb_frontend *fe)
606 struct stb0899_state *state = fe->demodulator_priv;
608 /* post process event */
609 stb0899_postproc(state, STB0899_POSTPROC_GPIO_POWER, 0);
612 static void stb0899_release(struct dvb_frontend *fe)
614 struct stb0899_state *state = fe->demodulator_priv;
616 dprintk(state->verbose, FE_DEBUG, 1, "Release Frontend");
624 static int stb0899_get_alpha(struct stb0899_state *state)
628 mode_coeff = stb0899_read_reg(state, STB0899_DEMOD);
630 if (STB0899_GETFIELD(MODECOEFF, mode_coeff) == 1)
639 static void stb0899_init_calc(struct stb0899_state *state)
641 struct stb0899_internal *internal = &state->internal;
646 /* Read registers (in burst mode) */
647 stb0899_read_regs(state, STB0899_AGC1REF, agc, 2); /* AGC1R and AGC2O */
649 /* Initial calculations */
650 master_clk = stb0899_get_mclk(state);
651 internal->t_agc1 = 0;
652 internal->t_agc2 = 0;
653 internal->master_clk = master_clk;
654 internal->mclk = master_clk / 65536L;
655 internal->rolloff = stb0899_get_alpha(state);
657 /* DVBS2 Initial calculations */
658 /* Set AGC value to the middle */
659 internal->agc_gain = 8154;
660 reg = STB0899_READ_S2REG(STB0899_S2DEMOD, IF_AGC_CNTRL);
661 STB0899_SETFIELD_VAL(IF_GAIN_INIT, reg, internal->agc_gain);
662 stb0899_write_s2reg(state, STB0899_S2DEMOD, STB0899_BASE_IF_AGC_CNTRL, STB0899_OFF0_IF_AGC_CNTRL, reg);
664 reg = STB0899_READ_S2REG(STB0899_S2DEMOD, RRC_ALPHA);
665 internal->rrc_alpha = STB0899_GETFIELD(RRC_ALPHA, reg);
667 internal->center_freq = 0;
668 internal->av_frame_coarse = 10;
669 internal->av_frame_fine = 20;
670 internal->step_size = 2;
672 if ((pParams->SpectralInv == FE_IQ_NORMAL) || (pParams->SpectralInv == FE_IQ_AUTO))
673 pParams->IQLocked = 0;
675 pParams->IQLocked = 1;
679 static int stb0899_wait_diseqc_fifo_empty(struct stb0899_state *state, int timeout)
682 unsigned long start = jiffies;
685 reg = stb0899_read_reg(state, STB0899_DISSTATUS);
686 if (!STB0899_GETFIELD(FIFOFULL, reg))
688 if (time_after(jiffies, start + timeout)) {
689 dprintk(state->verbose, FE_ERROR, 1, "timed out !!");
697 static int stb0899_send_diseqc_msg(struct dvb_frontend *fe, struct dvb_diseqc_master_cmd *cmd)
699 struct stb0899_state *state = fe->demodulator_priv;
702 if (cmd->msg_len > sizeof(cmd->msg))
705 /* enable FIFO precharge */
706 reg = stb0899_read_reg(state, STB0899_DISCNTRL1);
707 STB0899_SETFIELD_VAL(DISPRECHARGE, reg, 1);
708 stb0899_write_reg(state, STB0899_DISCNTRL1, reg);
709 for (i = 0; i < cmd->msg_len; i++) {
710 /* wait for FIFO empty */
711 if (stb0899_wait_diseqc_fifo_empty(state, 100) < 0)
714 stb0899_write_reg(state, STB0899_DISFIFO, cmd->msg[i]);
716 reg = stb0899_read_reg(state, STB0899_DISCNTRL1);
717 STB0899_SETFIELD_VAL(DISPRECHARGE, reg, 0);
718 stb0899_write_reg(state, STB0899_DISCNTRL1, reg);
723 static int stb0899_wait_diseqc_rxidle(struct stb0899_state *state, int timeout)
726 unsigned long start = jiffies;
728 while (!STB0899_GETFIELD(RXEND, reg)) {
729 reg = stb0899_read_reg(state, STB0899_DISRX_ST0);
730 if (time_after(jiffies, start + timeout)) {
731 dprintk(state->verbose, FE_ERROR, 1, "timed out!!");
740 static int stb0899_recv_slave_reply(struct dvb_frontend *fe, struct dvb_diseqc_slave_reply *reply)
742 struct stb0899_state *state = fe->demodulator_priv;
743 u8 reg, length = 0, i;
746 if (stb0899_wait_diseqc_rxidle(state, 100) < 0)
749 reg = stb0899_read_reg(state, STB0899_DISRX_ST0);
750 if (STB0899_GETFIELD(RXEND, reg)) {
752 reg = stb0899_read_reg(state, STB0899_DISRX_ST1);
753 length = STB0899_GETFIELD(FIFOBYTENBR, reg);
755 if (length > sizeof (reply->msg)) {
759 reply->msg_len = length;
762 for (i = 0; i < length; i++)
763 reply->msg[i] = stb0899_read_reg(state, STB0899_DISFIFO);
772 static int stb0899_wait_diseqc_txidle(struct stb0899_state *state, int timeout)
775 unsigned long start = jiffies;
777 while (!STB0899_GETFIELD(TXIDLE, reg)) {
778 reg = stb0899_read_reg(state, STB0899_DISSTATUS);
779 if (time_after(jiffies, start + timeout)) {
780 dprintk(state->verbose, FE_ERROR, 1, "timed out!!");
788 static int stb0899_send_diseqc_burst(struct dvb_frontend *fe,
789 enum fe_sec_mini_cmd burst)
791 struct stb0899_state *state = fe->demodulator_priv;
794 /* wait for diseqc idle */
795 if (stb0899_wait_diseqc_txidle(state, 100) < 0)
798 reg = stb0899_read_reg(state, STB0899_DISCNTRL1);
800 /* set to burst mode */
801 STB0899_SETFIELD_VAL(DISEQCMODE, reg, 0x03);
802 STB0899_SETFIELD_VAL(DISPRECHARGE, reg, 0x01);
803 stb0899_write_reg(state, STB0899_DISCNTRL1, reg);
807 stb0899_write_reg(state, STB0899_DISFIFO, 0x00);
811 stb0899_write_reg(state, STB0899_DISFIFO, 0xff);
814 reg = stb0899_read_reg(state, STB0899_DISCNTRL1);
815 STB0899_SETFIELD_VAL(DISPRECHARGE, reg, 0x00);
816 stb0899_write_reg(state, STB0899_DISCNTRL1, reg);
817 /* wait for diseqc idle */
818 if (stb0899_wait_diseqc_txidle(state, 100) < 0)
822 stb0899_write_reg(state, STB0899_DISCNTRL1, old_state);
827 static int stb0899_diseqc_init(struct stb0899_state *state)
830 struct dvb_diseqc_slave_reply rx_data;
834 u32 mclk, tx_freq = 22000;/* count = 0, i; */
835 reg = stb0899_read_reg(state, STB0899_DISCNTRL2);
836 STB0899_SETFIELD_VAL(ONECHIP_TRX, reg, 0);
837 stb0899_write_reg(state, STB0899_DISCNTRL2, reg);
840 reg = stb0899_read_reg(state, STB0899_DISCNTRL1);
841 STB0899_SETFIELD_VAL(DISEQCRESET, reg, 1);
842 stb0899_write_reg(state, STB0899_DISCNTRL1, reg);
844 reg = stb0899_read_reg(state, STB0899_DISCNTRL1);
845 STB0899_SETFIELD_VAL(DISEQCRESET, reg, 0);
846 stb0899_write_reg(state, STB0899_DISCNTRL1, reg);
848 mclk = stb0899_get_mclk(state);
849 f22_tx = mclk / (tx_freq * 32);
850 stb0899_write_reg(state, STB0899_DISF22, f22_tx); /* DiSEqC Tx freq */
851 state->rx_freq = 20000;
856 static int stb0899_sleep(struct dvb_frontend *fe)
858 struct stb0899_state *state = fe->demodulator_priv;
862 dprintk(state->verbose, FE_DEBUG, 1, "Going to Sleep .. (Really tired .. :-))");
863 /* post process event */
864 stb0899_postproc(state, STB0899_POSTPROC_GPIO_POWER, 0);
869 static int stb0899_wakeup(struct dvb_frontend *fe)
872 struct stb0899_state *state = fe->demodulator_priv;
874 if ((rc = stb0899_write_reg(state, STB0899_SYNTCTRL, STB0899_SELOSCI)))
876 /* Activate all clocks; DVB-S2 registers are inaccessible otherwise. */
877 if ((rc = stb0899_write_reg(state, STB0899_STOPCLK1, 0x00)))
879 if ((rc = stb0899_write_reg(state, STB0899_STOPCLK2, 0x00)))
882 /* post process event */
883 stb0899_postproc(state, STB0899_POSTPROC_GPIO_POWER, 1);
888 static int stb0899_init(struct dvb_frontend *fe)
891 struct stb0899_state *state = fe->demodulator_priv;
892 struct stb0899_config *config = state->config;
894 dprintk(state->verbose, FE_DEBUG, 1, "Initializing STB0899 ... ");
897 dprintk(state->verbose, FE_DEBUG, 1, "init device");
898 for (i = 0; config->init_dev[i].address != 0xffff; i++)
899 stb0899_write_reg(state, config->init_dev[i].address, config->init_dev[i].data);
901 dprintk(state->verbose, FE_DEBUG, 1, "init S2 demod");
903 for (i = 0; config->init_s2_demod[i].offset != 0xffff; i++)
904 stb0899_write_s2reg(state, STB0899_S2DEMOD,
905 config->init_s2_demod[i].base_address,
906 config->init_s2_demod[i].offset,
907 config->init_s2_demod[i].data);
909 dprintk(state->verbose, FE_DEBUG, 1, "init S1 demod");
911 for (i = 0; config->init_s1_demod[i].address != 0xffff; i++)
912 stb0899_write_reg(state, config->init_s1_demod[i].address, config->init_s1_demod[i].data);
914 dprintk(state->verbose, FE_DEBUG, 1, "init S2 FEC");
916 for (i = 0; config->init_s2_fec[i].offset != 0xffff; i++)
917 stb0899_write_s2reg(state, STB0899_S2FEC,
918 config->init_s2_fec[i].base_address,
919 config->init_s2_fec[i].offset,
920 config->init_s2_fec[i].data);
922 dprintk(state->verbose, FE_DEBUG, 1, "init TST");
924 for (i = 0; config->init_tst[i].address != 0xffff; i++)
925 stb0899_write_reg(state, config->init_tst[i].address, config->init_tst[i].data);
927 stb0899_init_calc(state);
928 stb0899_diseqc_init(state);
933 static int stb0899_table_lookup(const struct stb0899_tab *tab, int max, int val)
938 if (val < tab[min].read)
940 else if (val >= tab[max].read)
943 while ((max - min) > 1) {
944 med = (max + min) / 2;
945 if (val >= tab[min].read && val < tab[med].read)
950 res = ((val - tab[min].read) *
951 (tab[max].real - tab[min].real) /
952 (tab[max].read - tab[min].read)) +
959 static int stb0899_read_signal_strength(struct dvb_frontend *fe, u16 *strength)
961 struct stb0899_state *state = fe->demodulator_priv;
962 struct stb0899_internal *internal = &state->internal;
967 switch (state->delsys) {
970 if (internal->lock) {
971 reg = stb0899_read_reg(state, STB0899_VSTATUS);
972 if (STB0899_GETFIELD(VSTATUS_LOCKEDVIT, reg)) {
974 reg = stb0899_read_reg(state, STB0899_AGCIQIN);
975 val = (s32)(s8)STB0899_GETFIELD(AGCIQVALUE, reg);
977 *strength = stb0899_table_lookup(stb0899_dvbsrf_tab, ARRAY_SIZE(stb0899_dvbsrf_tab) - 1, val);
979 dprintk(state->verbose, FE_DEBUG, 1, "AGCIQVALUE = 0x%02x, C = %d * 0.1 dBm",
980 val & 0xff, *strength);
985 if (internal->lock) {
986 reg = STB0899_READ_S2REG(STB0899_S2DEMOD, IF_AGC_GAIN);
987 val = STB0899_GETFIELD(IF_AGC_GAIN, reg);
989 *strength = stb0899_table_lookup(stb0899_dvbs2rf_tab, ARRAY_SIZE(stb0899_dvbs2rf_tab) - 1, val);
991 dprintk(state->verbose, FE_DEBUG, 1, "IF_AGC_GAIN = 0x%04x, C = %d * 0.1 dBm",
992 val & 0x3fff, *strength);
996 dprintk(state->verbose, FE_DEBUG, 1, "Unsupported delivery system");
1003 static int stb0899_read_snr(struct dvb_frontend *fe, u16 *snr)
1005 struct stb0899_state *state = fe->demodulator_priv;
1006 struct stb0899_internal *internal = &state->internal;
1008 unsigned int val, quant, quantn = -1, est, estn = -1;
1013 reg = stb0899_read_reg(state, STB0899_VSTATUS);
1014 switch (state->delsys) {
1017 if (internal->lock) {
1018 if (STB0899_GETFIELD(VSTATUS_LOCKEDVIT, reg)) {
1020 stb0899_read_regs(state, STB0899_NIRM, buf, 2);
1021 val = MAKEWORD16(buf[0], buf[1]);
1023 *snr = stb0899_table_lookup(stb0899_cn_tab, ARRAY_SIZE(stb0899_cn_tab) - 1, val);
1024 dprintk(state->verbose, FE_DEBUG, 1, "NIR = 0x%02x%02x = %u, C/N = %d * 0.1 dBm\n",
1025 buf[0], buf[1], val, *snr);
1030 if (internal->lock) {
1031 reg = STB0899_READ_S2REG(STB0899_S2DEMOD, UWP_CNTRL1);
1032 quant = STB0899_GETFIELD(UWP_ESN0_QUANT, reg);
1033 reg = STB0899_READ_S2REG(STB0899_S2DEMOD, UWP_STAT2);
1034 est = STB0899_GETFIELD(ESN0_EST, reg);
1036 val = 301; /* C/N = 30.1 dB */
1038 val = 270; /* C/N = 27.0 dB */
1040 /* quantn = 100 * log(quant^2) */
1041 quantn = stb0899_table_lookup(stb0899_quant_tab, ARRAY_SIZE(stb0899_quant_tab) - 1, quant * 100);
1042 /* estn = 100 * log(est) */
1043 estn = stb0899_table_lookup(stb0899_est_tab, ARRAY_SIZE(stb0899_est_tab) - 1, est);
1044 /* snr(dBm/10) = -10*(log(est)-log(quant^2)) => snr(dBm/10) = (100*log(quant^2)-100*log(est))/10 */
1045 val = (quantn - estn) / 10;
1048 dprintk(state->verbose, FE_DEBUG, 1, "Es/N0 quant = %d (%d) estimate = %u (%d), C/N = %d * 0.1 dBm",
1049 quant, quantn, est, estn, val);
1053 dprintk(state->verbose, FE_DEBUG, 1, "Unsupported delivery system");
1060 static int stb0899_read_status(struct dvb_frontend *fe, enum fe_status *status)
1062 struct stb0899_state *state = fe->demodulator_priv;
1063 struct stb0899_internal *internal = &state->internal;
1067 switch (state->delsys) {
1070 dprintk(state->verbose, FE_DEBUG, 1, "Delivery system DVB-S/DSS");
1071 if (internal->lock) {
1072 reg = stb0899_read_reg(state, STB0899_VSTATUS);
1073 if (STB0899_GETFIELD(VSTATUS_LOCKEDVIT, reg)) {
1074 dprintk(state->verbose, FE_DEBUG, 1, "--------> FE_HAS_CARRIER | FE_HAS_LOCK");
1075 *status |= FE_HAS_SIGNAL | FE_HAS_CARRIER | FE_HAS_LOCK;
1077 reg = stb0899_read_reg(state, STB0899_PLPARM);
1078 if (STB0899_GETFIELD(VITCURPUN, reg)) {
1079 dprintk(state->verbose, FE_DEBUG, 1, "--------> FE_HAS_VITERBI | FE_HAS_SYNC");
1080 *status |= FE_HAS_VITERBI | FE_HAS_SYNC;
1081 /* post process event */
1082 stb0899_postproc(state, STB0899_POSTPROC_GPIO_LOCK, 1);
1088 dprintk(state->verbose, FE_DEBUG, 1, "Delivery system DVB-S2");
1089 if (internal->lock) {
1090 reg = STB0899_READ_S2REG(STB0899_S2DEMOD, DMD_STAT2);
1091 if (STB0899_GETFIELD(UWP_LOCK, reg) && STB0899_GETFIELD(CSM_LOCK, reg)) {
1092 *status |= FE_HAS_CARRIER;
1093 dprintk(state->verbose, FE_DEBUG, 1,
1094 "UWP & CSM Lock ! ---> DVB-S2 FE_HAS_CARRIER");
1096 reg = stb0899_read_reg(state, STB0899_CFGPDELSTATUS1);
1097 if (STB0899_GETFIELD(CFGPDELSTATUS_LOCK, reg)) {
1098 *status |= FE_HAS_LOCK;
1099 dprintk(state->verbose, FE_DEBUG, 1,
1100 "Packet Delineator Locked ! -----> DVB-S2 FE_HAS_LOCK");
1103 if (STB0899_GETFIELD(CONTINUOUS_STREAM, reg)) {
1104 *status |= FE_HAS_VITERBI;
1105 dprintk(state->verbose, FE_DEBUG, 1,
1106 "Packet Delineator found VITERBI ! -----> DVB-S2 FE_HAS_VITERBI");
1108 if (STB0899_GETFIELD(ACCEPTED_STREAM, reg)) {
1109 *status |= FE_HAS_SYNC;
1110 dprintk(state->verbose, FE_DEBUG, 1,
1111 "Packet Delineator found SYNC ! -----> DVB-S2 FE_HAS_SYNC");
1112 /* post process event */
1113 stb0899_postproc(state, STB0899_POSTPROC_GPIO_LOCK, 1);
1119 dprintk(state->verbose, FE_DEBUG, 1, "Unsupported delivery system");
1127 * viterbi error for DVB-S/DSS
1128 * packet error for DVB-S2
1129 * Bit Error Rate or Packet Error Rate * 10 ^ 7
1131 static int stb0899_read_ber(struct dvb_frontend *fe, u32 *ber)
1133 struct stb0899_state *state = fe->demodulator_priv;
1134 struct stb0899_internal *internal = &state->internal;
1140 switch (state->delsys) {
1143 if (internal->lock) {
1144 lsb = stb0899_read_reg(state, STB0899_ECNT1L);
1145 msb = stb0899_read_reg(state, STB0899_ECNT1M);
1146 *ber = MAKEWORD16(msb, lsb);
1148 if (STB0899_GETFIELD(VSTATUS_PRFVIT, internal->v_status)) {
1151 /* ber = ber * 10 ^ 7 */
1152 *ber /= (-1 + (1 << (2 * STB0899_GETFIELD(NOE, internal->err_ctrl))));
1158 if (internal->lock) {
1159 lsb = stb0899_read_reg(state, STB0899_ECNT1L);
1160 msb = stb0899_read_reg(state, STB0899_ECNT1M);
1161 *ber = MAKEWORD16(msb, lsb);
1162 /* ber = ber * 10 ^ 7 */
1164 *ber /= (-1 + (1 << (4 + 2 * STB0899_GETFIELD(NOE, internal->err_ctrl))));
1168 dprintk(state->verbose, FE_DEBUG, 1, "Unsupported delivery system");
1175 static int stb0899_set_voltage(struct dvb_frontend *fe,
1176 enum fe_sec_voltage voltage)
1178 struct stb0899_state *state = fe->demodulator_priv;
1181 case SEC_VOLTAGE_13:
1182 stb0899_write_reg(state, STB0899_GPIO00CFG, 0x82);
1183 stb0899_write_reg(state, STB0899_GPIO01CFG, 0x02);
1184 stb0899_write_reg(state, STB0899_GPIO02CFG, 0x00);
1186 case SEC_VOLTAGE_18:
1187 stb0899_write_reg(state, STB0899_GPIO00CFG, 0x02);
1188 stb0899_write_reg(state, STB0899_GPIO01CFG, 0x02);
1189 stb0899_write_reg(state, STB0899_GPIO02CFG, 0x82);
1191 case SEC_VOLTAGE_OFF:
1192 stb0899_write_reg(state, STB0899_GPIO00CFG, 0x82);
1193 stb0899_write_reg(state, STB0899_GPIO01CFG, 0x82);
1194 stb0899_write_reg(state, STB0899_GPIO02CFG, 0x82);
1203 static int stb0899_set_tone(struct dvb_frontend *fe, enum fe_sec_tone_mode tone)
1205 struct stb0899_state *state = fe->demodulator_priv;
1206 struct stb0899_internal *internal = &state->internal;
1210 /* wait for diseqc idle */
1211 if (stb0899_wait_diseqc_txidle(state, 100) < 0)
1216 div = (internal->master_clk / 100) / 5632;
1217 div = (div + 5) / 10;
1218 stb0899_write_reg(state, STB0899_DISEQCOCFG, 0x66);
1219 reg = stb0899_read_reg(state, STB0899_ACRPRESC);
1220 STB0899_SETFIELD_VAL(ACRPRESC, reg, 0x03);
1221 stb0899_write_reg(state, STB0899_ACRPRESC, reg);
1222 stb0899_write_reg(state, STB0899_ACRDIV1, div);
1225 stb0899_write_reg(state, STB0899_DISEQCOCFG, 0x20);
1233 int stb0899_i2c_gate_ctrl(struct dvb_frontend *fe, int enable)
1236 struct stb0899_state *state = fe->demodulator_priv;
1238 i2c_stat = stb0899_read_reg(state, STB0899_I2CRPT);
1243 dprintk(state->verbose, FE_DEBUG, 1, "Enabling I2C Repeater ...");
1244 i2c_stat |= STB0899_I2CTON;
1245 if (stb0899_write_reg(state, STB0899_I2CRPT, i2c_stat) < 0)
1248 dprintk(state->verbose, FE_DEBUG, 1, "Disabling I2C Repeater ...");
1249 i2c_stat &= ~STB0899_I2CTON;
1250 if (stb0899_write_reg(state, STB0899_I2CRPT, i2c_stat) < 0)
1255 dprintk(state->verbose, FE_ERROR, 1, "I2C Repeater control failed");
1260 static inline void CONVERT32(u32 x, char *str)
1262 *str++ = (x >> 24) & 0xff;
1263 *str++ = (x >> 16) & 0xff;
1264 *str++ = (x >> 8) & 0xff;
1265 *str++ = (x >> 0) & 0xff;
1269 static int stb0899_get_dev_id(struct stb0899_state *state)
1271 u8 chip_id, release;
1273 u32 demod_ver = 0, fec_ver = 0;
1274 char demod_str[5] = { 0 };
1275 char fec_str[5] = { 0 };
1277 id = stb0899_read_reg(state, STB0899_DEV_ID);
1278 dprintk(state->verbose, FE_DEBUG, 1, "ID reg=[0x%02x]", id);
1279 chip_id = STB0899_GETFIELD(CHIP_ID, id);
1280 release = STB0899_GETFIELD(CHIP_REL, id);
1282 dprintk(state->verbose, FE_ERROR, 1, "Device ID=[%d], Release=[%d]",
1285 CONVERT32(STB0899_READ_S2REG(STB0899_S2DEMOD, DMD_CORE_ID), (char *)&demod_str);
1287 demod_ver = STB0899_READ_S2REG(STB0899_S2DEMOD, DMD_VERSION_ID);
1288 dprintk(state->verbose, FE_ERROR, 1, "Demodulator Core ID=[%s], Version=[%d]", (char *) &demod_str, demod_ver);
1289 CONVERT32(STB0899_READ_S2REG(STB0899_S2FEC, FEC_CORE_ID_REG), (char *)&fec_str);
1290 fec_ver = STB0899_READ_S2REG(STB0899_S2FEC, FEC_VER_ID_REG);
1291 if (! (chip_id > 0)) {
1292 dprintk(state->verbose, FE_ERROR, 1, "couldn't find a STB 0899");
1296 dprintk(state->verbose, FE_ERROR, 1, "FEC Core ID=[%s], Version=[%d]", (char*) &fec_str, fec_ver);
1301 static void stb0899_set_delivery(struct stb0899_state *state)
1306 stop_clk[0] = stb0899_read_reg(state, STB0899_STOPCLK1);
1307 stop_clk[1] = stb0899_read_reg(state, STB0899_STOPCLK2);
1309 switch (state->delsys) {
1311 dprintk(state->verbose, FE_DEBUG, 1, "Delivery System -- DVB-S");
1312 /* FECM/Viterbi ON */
1313 reg = stb0899_read_reg(state, STB0899_FECM);
1314 STB0899_SETFIELD_VAL(FECM_RSVD0, reg, 0);
1315 STB0899_SETFIELD_VAL(FECM_VITERBI_ON, reg, 1);
1316 stb0899_write_reg(state, STB0899_FECM, reg);
1318 stb0899_write_reg(state, STB0899_RSULC, 0xb1);
1319 stb0899_write_reg(state, STB0899_TSULC, 0x40);
1320 stb0899_write_reg(state, STB0899_RSLLC, 0x42);
1321 stb0899_write_reg(state, STB0899_TSLPL, 0x12);
1323 reg = stb0899_read_reg(state, STB0899_TSTRES);
1324 STB0899_SETFIELD_VAL(FRESLDPC, reg, 1);
1325 stb0899_write_reg(state, STB0899_TSTRES, reg);
1327 STB0899_SETFIELD_VAL(STOP_CHK8PSK, stop_clk[0], 1);
1328 STB0899_SETFIELD_VAL(STOP_CKFEC108, stop_clk[0], 1);
1329 STB0899_SETFIELD_VAL(STOP_CKFEC216, stop_clk[0], 1);
1331 STB0899_SETFIELD_VAL(STOP_CKPKDLIN108, stop_clk[1], 1);
1332 STB0899_SETFIELD_VAL(STOP_CKPKDLIN216, stop_clk[1], 1);
1334 STB0899_SETFIELD_VAL(STOP_CKINTBUF216, stop_clk[0], 1);
1335 STB0899_SETFIELD_VAL(STOP_CKCORE216, stop_clk[0], 0);
1337 STB0899_SETFIELD_VAL(STOP_CKS2DMD108, stop_clk[1], 1);
1340 /* FECM/Viterbi OFF */
1341 reg = stb0899_read_reg(state, STB0899_FECM);
1342 STB0899_SETFIELD_VAL(FECM_RSVD0, reg, 0);
1343 STB0899_SETFIELD_VAL(FECM_VITERBI_ON, reg, 0);
1344 stb0899_write_reg(state, STB0899_FECM, reg);
1346 stb0899_write_reg(state, STB0899_RSULC, 0xb1);
1347 stb0899_write_reg(state, STB0899_TSULC, 0x42);
1348 stb0899_write_reg(state, STB0899_RSLLC, 0x40);
1349 stb0899_write_reg(state, STB0899_TSLPL, 0x02);
1351 reg = stb0899_read_reg(state, STB0899_TSTRES);
1352 STB0899_SETFIELD_VAL(FRESLDPC, reg, 0);
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], 0);
1357 STB0899_SETFIELD_VAL(STOP_CKFEC216, stop_clk[0], 0);
1359 STB0899_SETFIELD_VAL(STOP_CKPKDLIN108, stop_clk[1], 0);
1360 STB0899_SETFIELD_VAL(STOP_CKPKDLIN216, stop_clk[1], 0);
1362 STB0899_SETFIELD_VAL(STOP_CKINTBUF216, stop_clk[0], 0);
1363 STB0899_SETFIELD_VAL(STOP_CKCORE216, stop_clk[0], 0);
1365 STB0899_SETFIELD_VAL(STOP_CKS2DMD108, stop_clk[1], 0);
1368 /* FECM/Viterbi ON */
1369 reg = stb0899_read_reg(state, STB0899_FECM);
1370 STB0899_SETFIELD_VAL(FECM_RSVD0, reg, 1);
1371 STB0899_SETFIELD_VAL(FECM_VITERBI_ON, reg, 1);
1372 stb0899_write_reg(state, STB0899_FECM, reg);
1374 stb0899_write_reg(state, STB0899_RSULC, 0xa1);
1375 stb0899_write_reg(state, STB0899_TSULC, 0x61);
1376 stb0899_write_reg(state, STB0899_RSLLC, 0x42);
1378 reg = stb0899_read_reg(state, STB0899_TSTRES);
1379 STB0899_SETFIELD_VAL(FRESLDPC, reg, 1);
1380 stb0899_write_reg(state, STB0899_TSTRES, reg);
1382 STB0899_SETFIELD_VAL(STOP_CHK8PSK, stop_clk[0], 1);
1383 STB0899_SETFIELD_VAL(STOP_CKFEC108, stop_clk[0], 1);
1384 STB0899_SETFIELD_VAL(STOP_CKFEC216, stop_clk[0], 1);
1386 STB0899_SETFIELD_VAL(STOP_CKPKDLIN108, stop_clk[1], 1);
1387 STB0899_SETFIELD_VAL(STOP_CKPKDLIN216, stop_clk[1], 1);
1389 STB0899_SETFIELD_VAL(STOP_CKCORE216, stop_clk[0], 0);
1391 STB0899_SETFIELD_VAL(STOP_CKS2DMD108, stop_clk[1], 1);
1394 dprintk(state->verbose, FE_ERROR, 1, "Unsupported delivery system");
1397 STB0899_SETFIELD_VAL(STOP_CKADCI108, stop_clk[0], 0);
1398 stb0899_write_regs(state, STB0899_STOPCLK1, stop_clk, 2);
1402 * stb0899_set_iterations
1403 * set the LDPC iteration scale function
1405 static void stb0899_set_iterations(struct stb0899_state *state)
1407 struct stb0899_internal *internal = &state->internal;
1408 struct stb0899_config *config = state->config;
1413 iter_scale = 17 * (internal->master_clk / 1000);
1414 iter_scale += 410000;
1415 iter_scale /= (internal->srate / 1000000);
1418 if (iter_scale > config->ldpc_max_iter)
1419 iter_scale = config->ldpc_max_iter;
1421 reg = STB0899_READ_S2REG(STB0899_S2FEC, MAX_ITER);
1422 STB0899_SETFIELD_VAL(MAX_ITERATIONS, reg, iter_scale);
1423 stb0899_write_s2reg(state, STB0899_S2FEC, STB0899_BASE_MAX_ITER, STB0899_OFF0_MAX_ITER, reg);
1426 static enum dvbfe_search stb0899_search(struct dvb_frontend *fe)
1428 struct stb0899_state *state = fe->demodulator_priv;
1429 struct stb0899_params *i_params = &state->params;
1430 struct stb0899_internal *internal = &state->internal;
1431 struct stb0899_config *config = state->config;
1432 struct dtv_frontend_properties *props = &fe->dtv_property_cache;
1434 u32 SearchRange, gain;
1436 i_params->freq = props->frequency;
1437 i_params->srate = props->symbol_rate;
1438 state->delsys = props->delivery_system;
1439 dprintk(state->verbose, FE_DEBUG, 1, "delivery system=%d", state->delsys);
1441 SearchRange = 10000000;
1442 dprintk(state->verbose, FE_DEBUG, 1, "Frequency=%d, Srate=%d", i_params->freq, i_params->srate);
1443 /* checking Search Range is meaningless for a fixed 3 Mhz */
1444 if (INRANGE(i_params->srate, 1000000, 45000000)) {
1445 dprintk(state->verbose, FE_DEBUG, 1, "Parameters IN RANGE");
1446 stb0899_set_delivery(state);
1448 if (state->config->tuner_set_rfsiggain) {
1449 if (internal->srate > 15000000)
1450 gain = 8; /* 15Mb < srate < 45Mb, gain = 8dB */
1451 else if (internal->srate > 5000000)
1452 gain = 12; /* 5Mb < srate < 15Mb, gain = 12dB */
1454 gain = 14; /* 1Mb < srate < 5Mb, gain = 14db */
1455 state->config->tuner_set_rfsiggain(fe, gain);
1458 if (i_params->srate <= 5000000)
1459 stb0899_set_mclk(state, config->lo_clk);
1461 stb0899_set_mclk(state, config->hi_clk);
1463 switch (state->delsys) {
1466 dprintk(state->verbose, FE_DEBUG, 1, "DVB-S delivery system");
1467 internal->freq = i_params->freq;
1468 internal->srate = i_params->srate;
1470 * search = user search range +
1472 * 2 * Tuner_step_size +
1473 * 10% of the symbol rate
1475 internal->srch_range = SearchRange + 1500000 + (i_params->srate / 5);
1476 internal->derot_percent = 30;
1478 /* What to do for tuners having no bandwidth setup ? */
1479 /* enable tuner I/O */
1480 stb0899_i2c_gate_ctrl(&state->frontend, 1);
1482 if (state->config->tuner_set_bandwidth)
1483 state->config->tuner_set_bandwidth(fe, (13 * (stb0899_carr_width(state) + SearchRange)) / 10);
1484 if (state->config->tuner_get_bandwidth)
1485 state->config->tuner_get_bandwidth(fe, &internal->tuner_bw);
1487 /* disable tuner I/O */
1488 stb0899_i2c_gate_ctrl(&state->frontend, 0);
1490 /* Set DVB-S1 AGC */
1491 stb0899_write_reg(state, STB0899_AGCRFCFG, 0x11);
1493 /* Run the search algorithm */
1494 dprintk(state->verbose, FE_DEBUG, 1, "running DVB-S search algo ..");
1495 if (stb0899_dvbs_algo(state) == RANGEOK) {
1497 dprintk(state->verbose, FE_DEBUG, 1,
1498 "-------------------------------------> DVB-S LOCK !");
1500 // stb0899_write_reg(state, STB0899_ERRCTRL1, 0x3d); /* Viterbi Errors */
1501 // internal->v_status = stb0899_read_reg(state, STB0899_VSTATUS);
1502 // internal->err_ctrl = stb0899_read_reg(state, STB0899_ERRCTRL1);
1503 // dprintk(state->verbose, FE_DEBUG, 1, "VSTATUS=0x%02x", internal->v_status);
1504 // dprintk(state->verbose, FE_DEBUG, 1, "ERR_CTRL=0x%02x", internal->err_ctrl);
1506 return DVBFE_ALGO_SEARCH_SUCCESS;
1510 return DVBFE_ALGO_SEARCH_FAILED;
1514 internal->freq = i_params->freq;
1515 internal->srate = i_params->srate;
1516 internal->srch_range = SearchRange;
1518 /* enable tuner I/O */
1519 stb0899_i2c_gate_ctrl(&state->frontend, 1);
1521 if (state->config->tuner_set_bandwidth)
1522 state->config->tuner_set_bandwidth(fe, (stb0899_carr_width(state) + SearchRange));
1523 if (state->config->tuner_get_bandwidth)
1524 state->config->tuner_get_bandwidth(fe, &internal->tuner_bw);
1526 /* disable tuner I/O */
1527 stb0899_i2c_gate_ctrl(&state->frontend, 0);
1529 // pParams->SpectralInv = pSearch->IQ_Inversion;
1531 /* Set DVB-S2 AGC */
1532 stb0899_write_reg(state, STB0899_AGCRFCFG, 0x1c);
1534 /* Set IterScale =f(MCLK,SYMB) */
1535 stb0899_set_iterations(state);
1537 /* Run the search algorithm */
1538 dprintk(state->verbose, FE_DEBUG, 1, "running DVB-S2 search algo ..");
1539 if (stb0899_dvbs2_algo(state) == DVBS2_FEC_LOCK) {
1541 dprintk(state->verbose, FE_DEBUG, 1,
1542 "-------------------------------------> DVB-S2 LOCK !");
1544 // stb0899_write_reg(state, STB0899_ERRCTRL1, 0xb6); /* Packet Errors */
1545 // internal->v_status = stb0899_read_reg(state, STB0899_VSTATUS);
1546 // internal->err_ctrl = stb0899_read_reg(state, STB0899_ERRCTRL1);
1548 return DVBFE_ALGO_SEARCH_SUCCESS;
1552 return DVBFE_ALGO_SEARCH_FAILED;
1556 dprintk(state->verbose, FE_ERROR, 1, "Unsupported delivery system");
1557 return DVBFE_ALGO_SEARCH_INVALID;
1561 return DVBFE_ALGO_SEARCH_ERROR;
1564 static int stb0899_get_frontend(struct dvb_frontend *fe,
1565 struct dtv_frontend_properties *p)
1567 struct stb0899_state *state = fe->demodulator_priv;
1568 struct stb0899_internal *internal = &state->internal;
1570 dprintk(state->verbose, FE_DEBUG, 1, "Get params");
1571 p->symbol_rate = internal->srate;
1572 p->frequency = internal->freq;
1577 static enum dvbfe_algo stb0899_frontend_algo(struct dvb_frontend *fe)
1579 return DVBFE_ALGO_CUSTOM;
1582 static const struct dvb_frontend_ops stb0899_ops = {
1583 .delsys = { SYS_DVBS, SYS_DVBS2, SYS_DSS },
1585 .name = "STB0899 Multistandard",
1586 .frequency_min = 950000,
1587 .frequency_max = 2150000,
1588 .frequency_stepsize = 0,
1589 .frequency_tolerance = 0,
1590 .symbol_rate_min = 5000000,
1591 .symbol_rate_max = 45000000,
1593 .caps = FE_CAN_INVERSION_AUTO |
1595 FE_CAN_2G_MODULATION |
1599 .detach = stb0899_detach,
1600 .release = stb0899_release,
1601 .init = stb0899_init,
1602 .sleep = stb0899_sleep,
1603 // .wakeup = stb0899_wakeup,
1605 .i2c_gate_ctrl = stb0899_i2c_gate_ctrl,
1607 .get_frontend_algo = stb0899_frontend_algo,
1608 .search = stb0899_search,
1609 .get_frontend = stb0899_get_frontend,
1612 .read_status = stb0899_read_status,
1613 .read_snr = stb0899_read_snr,
1614 .read_signal_strength = stb0899_read_signal_strength,
1615 .read_ber = stb0899_read_ber,
1617 .set_voltage = stb0899_set_voltage,
1618 .set_tone = stb0899_set_tone,
1620 .diseqc_send_master_cmd = stb0899_send_diseqc_msg,
1621 .diseqc_recv_slave_reply = stb0899_recv_slave_reply,
1622 .diseqc_send_burst = stb0899_send_diseqc_burst,
1625 struct dvb_frontend *stb0899_attach(struct stb0899_config *config, struct i2c_adapter *i2c)
1627 struct stb0899_state *state = NULL;
1629 state = kzalloc(sizeof (struct stb0899_state), GFP_KERNEL);
1633 state->verbose = &verbose;
1634 state->config = config;
1636 state->frontend.ops = stb0899_ops;
1637 state->frontend.demodulator_priv = state;
1638 /* use configured inversion as default -- we'll later autodetect inversion */
1639 state->internal.inversion = config->inversion;
1641 stb0899_wakeup(&state->frontend);
1642 if (stb0899_get_dev_id(state) == -ENODEV) {
1643 printk("%s: Exiting .. !\n", __func__);
1647 printk("%s: Attaching STB0899 \n", __func__);
1648 return &state->frontend;
1654 EXPORT_SYMBOL(stb0899_attach);
1655 MODULE_PARM_DESC(verbose, "Set Verbosity level");
1656 MODULE_AUTHOR("Manu Abraham");
1657 MODULE_DESCRIPTION("STB0899 Multi-Std frontend");
1658 MODULE_LICENSE("GPL");