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)
543 return stb0899_write_regs(state, reg, &tmp, 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 = 0, div = 0;
555 div = stb0899_read_reg(state, STB0899_NCOARSE);
556 mclk = (div + 1) * state->config->xtal_freq / 6;
557 dprintk(state->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(state->verbose, FE_DEBUG, 1, "state->config=%p", state->config);
574 mdiv = ((6 * Mclk) / state->config->xtal_freq) - 1;
575 dprintk(state->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(state->verbose, FE_DEBUG, 1, "MasterCLOCK=%d", internal->master_clk);
583 static int stb0899_postproc(struct stb0899_state *state, u8 ctl, int enable)
585 struct stb0899_config *config = state->config;
586 const struct stb0899_postproc *postproc = config->postproc;
588 /* post process event */
591 if (postproc[ctl].level == STB0899_GPIOPULLUP)
592 stb0899_write_reg(state, postproc[ctl].gpio, 0x02);
594 stb0899_write_reg(state, postproc[ctl].gpio, 0x82);
596 if (postproc[ctl].level == STB0899_GPIOPULLUP)
597 stb0899_write_reg(state, postproc[ctl].gpio, 0x82);
599 stb0899_write_reg(state, postproc[ctl].gpio, 0x02);
605 static void stb0899_detach(struct dvb_frontend *fe)
607 struct stb0899_state *state = fe->demodulator_priv;
609 /* post process event */
610 stb0899_postproc(state, STB0899_POSTPROC_GPIO_POWER, 0);
613 static void stb0899_release(struct dvb_frontend *fe)
615 struct stb0899_state *state = fe->demodulator_priv;
617 dprintk(state->verbose, FE_DEBUG, 1, "Release Frontend");
625 static int stb0899_get_alpha(struct stb0899_state *state)
629 mode_coeff = stb0899_read_reg(state, STB0899_DEMOD);
631 if (STB0899_GETFIELD(MODECOEFF, mode_coeff) == 1)
640 static void stb0899_init_calc(struct stb0899_state *state)
642 struct stb0899_internal *internal = &state->internal;
647 /* Read registers (in burst mode) */
648 stb0899_read_regs(state, STB0899_AGC1REF, agc, 2); /* AGC1R and AGC2O */
650 /* Initial calculations */
651 master_clk = stb0899_get_mclk(state);
652 internal->t_agc1 = 0;
653 internal->t_agc2 = 0;
654 internal->master_clk = master_clk;
655 internal->mclk = master_clk / 65536L;
656 internal->rolloff = stb0899_get_alpha(state);
658 /* DVBS2 Initial calculations */
659 /* Set AGC value to the middle */
660 internal->agc_gain = 8154;
661 reg = STB0899_READ_S2REG(STB0899_S2DEMOD, IF_AGC_CNTRL);
662 STB0899_SETFIELD_VAL(IF_GAIN_INIT, reg, internal->agc_gain);
663 stb0899_write_s2reg(state, STB0899_S2DEMOD, STB0899_BASE_IF_AGC_CNTRL, STB0899_OFF0_IF_AGC_CNTRL, reg);
665 reg = STB0899_READ_S2REG(STB0899_S2DEMOD, RRC_ALPHA);
666 internal->rrc_alpha = STB0899_GETFIELD(RRC_ALPHA, reg);
668 internal->center_freq = 0;
669 internal->av_frame_coarse = 10;
670 internal->av_frame_fine = 20;
671 internal->step_size = 2;
673 if ((pParams->SpectralInv == FE_IQ_NORMAL) || (pParams->SpectralInv == FE_IQ_AUTO))
674 pParams->IQLocked = 0;
676 pParams->IQLocked = 1;
680 static int stb0899_wait_diseqc_fifo_empty(struct stb0899_state *state, int timeout)
683 unsigned long start = jiffies;
686 reg = stb0899_read_reg(state, STB0899_DISSTATUS);
687 if (!STB0899_GETFIELD(FIFOFULL, reg))
689 if (time_after(jiffies, start + timeout)) {
690 dprintk(state->verbose, FE_ERROR, 1, "timed out !!");
698 static int stb0899_send_diseqc_msg(struct dvb_frontend *fe, struct dvb_diseqc_master_cmd *cmd)
700 struct stb0899_state *state = fe->demodulator_priv;
703 if (cmd->msg_len > sizeof(cmd->msg))
706 /* enable FIFO precharge */
707 reg = stb0899_read_reg(state, STB0899_DISCNTRL1);
708 STB0899_SETFIELD_VAL(DISPRECHARGE, reg, 1);
709 stb0899_write_reg(state, STB0899_DISCNTRL1, reg);
710 for (i = 0; i < cmd->msg_len; i++) {
711 /* wait for FIFO empty */
712 if (stb0899_wait_diseqc_fifo_empty(state, 100) < 0)
715 stb0899_write_reg(state, STB0899_DISFIFO, cmd->msg[i]);
717 reg = stb0899_read_reg(state, STB0899_DISCNTRL1);
718 STB0899_SETFIELD_VAL(DISPRECHARGE, reg, 0);
719 stb0899_write_reg(state, STB0899_DISCNTRL1, reg);
724 static int stb0899_wait_diseqc_rxidle(struct stb0899_state *state, int timeout)
727 unsigned long start = jiffies;
729 while (!STB0899_GETFIELD(RXEND, reg)) {
730 reg = stb0899_read_reg(state, STB0899_DISRX_ST0);
731 if (time_after(jiffies, start + timeout)) {
732 dprintk(state->verbose, FE_ERROR, 1, "timed out!!");
741 static int stb0899_recv_slave_reply(struct dvb_frontend *fe, struct dvb_diseqc_slave_reply *reply)
743 struct stb0899_state *state = fe->demodulator_priv;
744 u8 reg, length = 0, i;
747 if (stb0899_wait_diseqc_rxidle(state, 100) < 0)
750 reg = stb0899_read_reg(state, STB0899_DISRX_ST0);
751 if (STB0899_GETFIELD(RXEND, reg)) {
753 reg = stb0899_read_reg(state, STB0899_DISRX_ST1);
754 length = STB0899_GETFIELD(FIFOBYTENBR, reg);
756 if (length > sizeof (reply->msg)) {
760 reply->msg_len = length;
763 for (i = 0; i < length; i++)
764 reply->msg[i] = stb0899_read_reg(state, STB0899_DISFIFO);
773 static int stb0899_wait_diseqc_txidle(struct stb0899_state *state, int timeout)
776 unsigned long start = jiffies;
778 while (!STB0899_GETFIELD(TXIDLE, reg)) {
779 reg = stb0899_read_reg(state, STB0899_DISSTATUS);
780 if (time_after(jiffies, start + timeout)) {
781 dprintk(state->verbose, FE_ERROR, 1, "timed out!!");
789 static int stb0899_send_diseqc_burst(struct dvb_frontend *fe,
790 enum fe_sec_mini_cmd burst)
792 struct stb0899_state *state = fe->demodulator_priv;
795 /* wait for diseqc idle */
796 if (stb0899_wait_diseqc_txidle(state, 100) < 0)
799 reg = stb0899_read_reg(state, STB0899_DISCNTRL1);
801 /* set to burst mode */
802 STB0899_SETFIELD_VAL(DISEQCMODE, reg, 0x03);
803 STB0899_SETFIELD_VAL(DISPRECHARGE, reg, 0x01);
804 stb0899_write_reg(state, STB0899_DISCNTRL1, reg);
808 stb0899_write_reg(state, STB0899_DISFIFO, 0x00);
812 stb0899_write_reg(state, STB0899_DISFIFO, 0xff);
815 reg = stb0899_read_reg(state, STB0899_DISCNTRL1);
816 STB0899_SETFIELD_VAL(DISPRECHARGE, reg, 0x00);
817 stb0899_write_reg(state, STB0899_DISCNTRL1, reg);
818 /* wait for diseqc idle */
819 if (stb0899_wait_diseqc_txidle(state, 100) < 0)
823 stb0899_write_reg(state, STB0899_DISCNTRL1, old_state);
828 static int stb0899_diseqc_init(struct stb0899_state *state)
831 struct dvb_diseqc_slave_reply rx_data;
835 u32 mclk, tx_freq = 22000;/* count = 0, i; */
836 reg = stb0899_read_reg(state, STB0899_DISCNTRL2);
837 STB0899_SETFIELD_VAL(ONECHIP_TRX, reg, 0);
838 stb0899_write_reg(state, STB0899_DISCNTRL2, reg);
841 reg = stb0899_read_reg(state, STB0899_DISCNTRL1);
842 STB0899_SETFIELD_VAL(DISEQCRESET, reg, 1);
843 stb0899_write_reg(state, STB0899_DISCNTRL1, reg);
845 reg = stb0899_read_reg(state, STB0899_DISCNTRL1);
846 STB0899_SETFIELD_VAL(DISEQCRESET, reg, 0);
847 stb0899_write_reg(state, STB0899_DISCNTRL1, reg);
849 mclk = stb0899_get_mclk(state);
850 f22_tx = mclk / (tx_freq * 32);
851 stb0899_write_reg(state, STB0899_DISF22, f22_tx); /* DiSEqC Tx freq */
852 state->rx_freq = 20000;
857 static int stb0899_sleep(struct dvb_frontend *fe)
859 struct stb0899_state *state = fe->demodulator_priv;
863 dprintk(state->verbose, FE_DEBUG, 1, "Going to Sleep .. (Really tired .. :-))");
864 /* post process event */
865 stb0899_postproc(state, STB0899_POSTPROC_GPIO_POWER, 0);
870 static int stb0899_wakeup(struct dvb_frontend *fe)
873 struct stb0899_state *state = fe->demodulator_priv;
875 if ((rc = stb0899_write_reg(state, STB0899_SYNTCTRL, STB0899_SELOSCI)))
877 /* Activate all clocks; DVB-S2 registers are inaccessible otherwise. */
878 if ((rc = stb0899_write_reg(state, STB0899_STOPCLK1, 0x00)))
880 if ((rc = stb0899_write_reg(state, STB0899_STOPCLK2, 0x00)))
883 /* post process event */
884 stb0899_postproc(state, STB0899_POSTPROC_GPIO_POWER, 1);
889 static int stb0899_init(struct dvb_frontend *fe)
892 struct stb0899_state *state = fe->demodulator_priv;
893 struct stb0899_config *config = state->config;
895 dprintk(state->verbose, FE_DEBUG, 1, "Initializing STB0899 ... ");
898 dprintk(state->verbose, FE_DEBUG, 1, "init device");
899 for (i = 0; config->init_dev[i].address != 0xffff; i++)
900 stb0899_write_reg(state, config->init_dev[i].address, config->init_dev[i].data);
902 dprintk(state->verbose, FE_DEBUG, 1, "init S2 demod");
904 for (i = 0; config->init_s2_demod[i].offset != 0xffff; i++)
905 stb0899_write_s2reg(state, STB0899_S2DEMOD,
906 config->init_s2_demod[i].base_address,
907 config->init_s2_demod[i].offset,
908 config->init_s2_demod[i].data);
910 dprintk(state->verbose, FE_DEBUG, 1, "init S1 demod");
912 for (i = 0; config->init_s1_demod[i].address != 0xffff; i++)
913 stb0899_write_reg(state, config->init_s1_demod[i].address, config->init_s1_demod[i].data);
915 dprintk(state->verbose, FE_DEBUG, 1, "init S2 FEC");
917 for (i = 0; config->init_s2_fec[i].offset != 0xffff; i++)
918 stb0899_write_s2reg(state, STB0899_S2FEC,
919 config->init_s2_fec[i].base_address,
920 config->init_s2_fec[i].offset,
921 config->init_s2_fec[i].data);
923 dprintk(state->verbose, FE_DEBUG, 1, "init TST");
925 for (i = 0; config->init_tst[i].address != 0xffff; i++)
926 stb0899_write_reg(state, config->init_tst[i].address, config->init_tst[i].data);
928 stb0899_init_calc(state);
929 stb0899_diseqc_init(state);
934 static int stb0899_table_lookup(const struct stb0899_tab *tab, int max, int val)
939 if (val < tab[min].read)
941 else if (val >= tab[max].read)
944 while ((max - min) > 1) {
945 med = (max + min) / 2;
946 if (val >= tab[min].read && val < tab[med].read)
951 res = ((val - tab[min].read) *
952 (tab[max].real - tab[min].real) /
953 (tab[max].read - tab[min].read)) +
960 static int stb0899_read_signal_strength(struct dvb_frontend *fe, u16 *strength)
962 struct stb0899_state *state = fe->demodulator_priv;
963 struct stb0899_internal *internal = &state->internal;
968 switch (state->delsys) {
971 if (internal->lock) {
972 reg = stb0899_read_reg(state, STB0899_VSTATUS);
973 if (STB0899_GETFIELD(VSTATUS_LOCKEDVIT, reg)) {
975 reg = stb0899_read_reg(state, STB0899_AGCIQIN);
976 val = (s32)(s8)STB0899_GETFIELD(AGCIQVALUE, reg);
978 *strength = stb0899_table_lookup(stb0899_dvbsrf_tab, ARRAY_SIZE(stb0899_dvbsrf_tab) - 1, val);
980 dprintk(state->verbose, FE_DEBUG, 1, "AGCIQVALUE = 0x%02x, C = %d * 0.1 dBm",
981 val & 0xff, *strength);
986 if (internal->lock) {
987 reg = STB0899_READ_S2REG(STB0899_S2DEMOD, IF_AGC_GAIN);
988 val = STB0899_GETFIELD(IF_AGC_GAIN, reg);
990 *strength = stb0899_table_lookup(stb0899_dvbs2rf_tab, ARRAY_SIZE(stb0899_dvbs2rf_tab) - 1, val);
992 dprintk(state->verbose, FE_DEBUG, 1, "IF_AGC_GAIN = 0x%04x, C = %d * 0.1 dBm",
993 val & 0x3fff, *strength);
997 dprintk(state->verbose, FE_DEBUG, 1, "Unsupported delivery system");
1004 static int stb0899_read_snr(struct dvb_frontend *fe, u16 *snr)
1006 struct stb0899_state *state = fe->demodulator_priv;
1007 struct stb0899_internal *internal = &state->internal;
1009 unsigned int val, quant, quantn = -1, est, estn = -1;
1014 reg = stb0899_read_reg(state, STB0899_VSTATUS);
1015 switch (state->delsys) {
1018 if (internal->lock) {
1019 if (STB0899_GETFIELD(VSTATUS_LOCKEDVIT, reg)) {
1021 stb0899_read_regs(state, STB0899_NIRM, buf, 2);
1022 val = MAKEWORD16(buf[0], buf[1]);
1024 *snr = stb0899_table_lookup(stb0899_cn_tab, ARRAY_SIZE(stb0899_cn_tab) - 1, val);
1025 dprintk(state->verbose, FE_DEBUG, 1, "NIR = 0x%02x%02x = %u, C/N = %d * 0.1 dBm\n",
1026 buf[0], buf[1], val, *snr);
1031 if (internal->lock) {
1032 reg = STB0899_READ_S2REG(STB0899_S2DEMOD, UWP_CNTRL1);
1033 quant = STB0899_GETFIELD(UWP_ESN0_QUANT, reg);
1034 reg = STB0899_READ_S2REG(STB0899_S2DEMOD, UWP_STAT2);
1035 est = STB0899_GETFIELD(ESN0_EST, reg);
1037 val = 301; /* C/N = 30.1 dB */
1039 val = 270; /* C/N = 27.0 dB */
1041 /* quantn = 100 * log(quant^2) */
1042 quantn = stb0899_table_lookup(stb0899_quant_tab, ARRAY_SIZE(stb0899_quant_tab) - 1, quant * 100);
1043 /* estn = 100 * log(est) */
1044 estn = stb0899_table_lookup(stb0899_est_tab, ARRAY_SIZE(stb0899_est_tab) - 1, est);
1045 /* snr(dBm/10) = -10*(log(est)-log(quant^2)) => snr(dBm/10) = (100*log(quant^2)-100*log(est))/10 */
1046 val = (quantn - estn) / 10;
1049 dprintk(state->verbose, FE_DEBUG, 1, "Es/N0 quant = %d (%d) estimate = %u (%d), C/N = %d * 0.1 dBm",
1050 quant, quantn, est, estn, val);
1054 dprintk(state->verbose, FE_DEBUG, 1, "Unsupported delivery system");
1061 static int stb0899_read_status(struct dvb_frontend *fe, enum fe_status *status)
1063 struct stb0899_state *state = fe->demodulator_priv;
1064 struct stb0899_internal *internal = &state->internal;
1068 switch (state->delsys) {
1071 dprintk(state->verbose, FE_DEBUG, 1, "Delivery system DVB-S/DSS");
1072 if (internal->lock) {
1073 reg = stb0899_read_reg(state, STB0899_VSTATUS);
1074 if (STB0899_GETFIELD(VSTATUS_LOCKEDVIT, reg)) {
1075 dprintk(state->verbose, FE_DEBUG, 1, "--------> FE_HAS_CARRIER | FE_HAS_LOCK");
1076 *status |= FE_HAS_SIGNAL | FE_HAS_CARRIER | FE_HAS_LOCK;
1078 reg = stb0899_read_reg(state, STB0899_PLPARM);
1079 if (STB0899_GETFIELD(VITCURPUN, reg)) {
1080 dprintk(state->verbose, FE_DEBUG, 1, "--------> FE_HAS_VITERBI | FE_HAS_SYNC");
1081 *status |= FE_HAS_VITERBI | FE_HAS_SYNC;
1082 /* post process event */
1083 stb0899_postproc(state, STB0899_POSTPROC_GPIO_LOCK, 1);
1089 dprintk(state->verbose, FE_DEBUG, 1, "Delivery system DVB-S2");
1090 if (internal->lock) {
1091 reg = STB0899_READ_S2REG(STB0899_S2DEMOD, DMD_STAT2);
1092 if (STB0899_GETFIELD(UWP_LOCK, reg) && STB0899_GETFIELD(CSM_LOCK, reg)) {
1093 *status |= FE_HAS_CARRIER;
1094 dprintk(state->verbose, FE_DEBUG, 1,
1095 "UWP & CSM Lock ! ---> DVB-S2 FE_HAS_CARRIER");
1097 reg = stb0899_read_reg(state, STB0899_CFGPDELSTATUS1);
1098 if (STB0899_GETFIELD(CFGPDELSTATUS_LOCK, reg)) {
1099 *status |= FE_HAS_LOCK;
1100 dprintk(state->verbose, FE_DEBUG, 1,
1101 "Packet Delineator Locked ! -----> DVB-S2 FE_HAS_LOCK");
1104 if (STB0899_GETFIELD(CONTINUOUS_STREAM, reg)) {
1105 *status |= FE_HAS_VITERBI;
1106 dprintk(state->verbose, FE_DEBUG, 1,
1107 "Packet Delineator found VITERBI ! -----> DVB-S2 FE_HAS_VITERBI");
1109 if (STB0899_GETFIELD(ACCEPTED_STREAM, reg)) {
1110 *status |= FE_HAS_SYNC;
1111 dprintk(state->verbose, FE_DEBUG, 1,
1112 "Packet Delineator found SYNC ! -----> DVB-S2 FE_HAS_SYNC");
1113 /* post process event */
1114 stb0899_postproc(state, STB0899_POSTPROC_GPIO_LOCK, 1);
1120 dprintk(state->verbose, FE_DEBUG, 1, "Unsupported delivery system");
1128 * viterbi error for DVB-S/DSS
1129 * packet error for DVB-S2
1130 * Bit Error Rate or Packet Error Rate * 10 ^ 7
1132 static int stb0899_read_ber(struct dvb_frontend *fe, u32 *ber)
1134 struct stb0899_state *state = fe->demodulator_priv;
1135 struct stb0899_internal *internal = &state->internal;
1141 switch (state->delsys) {
1144 if (internal->lock) {
1145 lsb = stb0899_read_reg(state, STB0899_ECNT1L);
1146 msb = stb0899_read_reg(state, STB0899_ECNT1M);
1147 *ber = MAKEWORD16(msb, lsb);
1149 if (STB0899_GETFIELD(VSTATUS_PRFVIT, internal->v_status)) {
1152 /* ber = ber * 10 ^ 7 */
1153 *ber /= (-1 + (1 << (2 * STB0899_GETFIELD(NOE, internal->err_ctrl))));
1159 if (internal->lock) {
1160 lsb = stb0899_read_reg(state, STB0899_ECNT1L);
1161 msb = stb0899_read_reg(state, STB0899_ECNT1M);
1162 *ber = MAKEWORD16(msb, lsb);
1163 /* ber = ber * 10 ^ 7 */
1165 *ber /= (-1 + (1 << (4 + 2 * STB0899_GETFIELD(NOE, internal->err_ctrl))));
1169 dprintk(state->verbose, FE_DEBUG, 1, "Unsupported delivery system");
1176 static int stb0899_set_voltage(struct dvb_frontend *fe,
1177 enum fe_sec_voltage voltage)
1179 struct stb0899_state *state = fe->demodulator_priv;
1182 case SEC_VOLTAGE_13:
1183 stb0899_write_reg(state, STB0899_GPIO00CFG, 0x82);
1184 stb0899_write_reg(state, STB0899_GPIO01CFG, 0x02);
1185 stb0899_write_reg(state, STB0899_GPIO02CFG, 0x00);
1187 case SEC_VOLTAGE_18:
1188 stb0899_write_reg(state, STB0899_GPIO00CFG, 0x02);
1189 stb0899_write_reg(state, STB0899_GPIO01CFG, 0x02);
1190 stb0899_write_reg(state, STB0899_GPIO02CFG, 0x82);
1192 case SEC_VOLTAGE_OFF:
1193 stb0899_write_reg(state, STB0899_GPIO00CFG, 0x82);
1194 stb0899_write_reg(state, STB0899_GPIO01CFG, 0x82);
1195 stb0899_write_reg(state, STB0899_GPIO02CFG, 0x82);
1204 static int stb0899_set_tone(struct dvb_frontend *fe, enum fe_sec_tone_mode tone)
1206 struct stb0899_state *state = fe->demodulator_priv;
1207 struct stb0899_internal *internal = &state->internal;
1211 /* wait for diseqc idle */
1212 if (stb0899_wait_diseqc_txidle(state, 100) < 0)
1217 div = (internal->master_clk / 100) / 5632;
1218 div = (div + 5) / 10;
1219 stb0899_write_reg(state, STB0899_DISEQCOCFG, 0x66);
1220 reg = stb0899_read_reg(state, STB0899_ACRPRESC);
1221 STB0899_SETFIELD_VAL(ACRPRESC, reg, 0x03);
1222 stb0899_write_reg(state, STB0899_ACRPRESC, reg);
1223 stb0899_write_reg(state, STB0899_ACRDIV1, div);
1226 stb0899_write_reg(state, STB0899_DISEQCOCFG, 0x20);
1234 int stb0899_i2c_gate_ctrl(struct dvb_frontend *fe, int enable)
1237 struct stb0899_state *state = fe->demodulator_priv;
1239 i2c_stat = stb0899_read_reg(state, STB0899_I2CRPT);
1244 dprintk(state->verbose, FE_DEBUG, 1, "Enabling I2C Repeater ...");
1245 i2c_stat |= STB0899_I2CTON;
1246 if (stb0899_write_reg(state, STB0899_I2CRPT, i2c_stat) < 0)
1249 dprintk(state->verbose, FE_DEBUG, 1, "Disabling I2C Repeater ...");
1250 i2c_stat &= ~STB0899_I2CTON;
1251 if (stb0899_write_reg(state, STB0899_I2CRPT, i2c_stat) < 0)
1256 dprintk(state->verbose, FE_ERROR, 1, "I2C Repeater control failed");
1261 static inline void CONVERT32(u32 x, char *str)
1263 *str++ = (x >> 24) & 0xff;
1264 *str++ = (x >> 16) & 0xff;
1265 *str++ = (x >> 8) & 0xff;
1266 *str++ = (x >> 0) & 0xff;
1270 static int stb0899_get_dev_id(struct stb0899_state *state)
1272 u8 chip_id, release;
1274 u32 demod_ver = 0, fec_ver = 0;
1275 char demod_str[5] = { 0 };
1276 char fec_str[5] = { 0 };
1278 id = stb0899_read_reg(state, STB0899_DEV_ID);
1279 dprintk(state->verbose, FE_DEBUG, 1, "ID reg=[0x%02x]", id);
1280 chip_id = STB0899_GETFIELD(CHIP_ID, id);
1281 release = STB0899_GETFIELD(CHIP_REL, id);
1283 dprintk(state->verbose, FE_ERROR, 1, "Device ID=[%d], Release=[%d]",
1286 CONVERT32(STB0899_READ_S2REG(STB0899_S2DEMOD, DMD_CORE_ID), (char *)&demod_str);
1288 demod_ver = STB0899_READ_S2REG(STB0899_S2DEMOD, DMD_VERSION_ID);
1289 dprintk(state->verbose, FE_ERROR, 1, "Demodulator Core ID=[%s], Version=[%d]", (char *) &demod_str, demod_ver);
1290 CONVERT32(STB0899_READ_S2REG(STB0899_S2FEC, FEC_CORE_ID_REG), (char *)&fec_str);
1291 fec_ver = STB0899_READ_S2REG(STB0899_S2FEC, FEC_VER_ID_REG);
1292 if (! (chip_id > 0)) {
1293 dprintk(state->verbose, FE_ERROR, 1, "couldn't find a STB 0899");
1297 dprintk(state->verbose, FE_ERROR, 1, "FEC Core ID=[%s], Version=[%d]", (char*) &fec_str, fec_ver);
1302 static void stb0899_set_delivery(struct stb0899_state *state)
1307 stop_clk[0] = stb0899_read_reg(state, STB0899_STOPCLK1);
1308 stop_clk[1] = stb0899_read_reg(state, STB0899_STOPCLK2);
1310 switch (state->delsys) {
1312 dprintk(state->verbose, FE_DEBUG, 1, "Delivery System -- DVB-S");
1313 /* FECM/Viterbi ON */
1314 reg = stb0899_read_reg(state, STB0899_FECM);
1315 STB0899_SETFIELD_VAL(FECM_RSVD0, reg, 0);
1316 STB0899_SETFIELD_VAL(FECM_VITERBI_ON, reg, 1);
1317 stb0899_write_reg(state, STB0899_FECM, reg);
1319 stb0899_write_reg(state, STB0899_RSULC, 0xb1);
1320 stb0899_write_reg(state, STB0899_TSULC, 0x40);
1321 stb0899_write_reg(state, STB0899_RSLLC, 0x42);
1322 stb0899_write_reg(state, STB0899_TSLPL, 0x12);
1324 reg = stb0899_read_reg(state, STB0899_TSTRES);
1325 STB0899_SETFIELD_VAL(FRESLDPC, reg, 1);
1326 stb0899_write_reg(state, STB0899_TSTRES, reg);
1328 STB0899_SETFIELD_VAL(STOP_CHK8PSK, stop_clk[0], 1);
1329 STB0899_SETFIELD_VAL(STOP_CKFEC108, stop_clk[0], 1);
1330 STB0899_SETFIELD_VAL(STOP_CKFEC216, stop_clk[0], 1);
1332 STB0899_SETFIELD_VAL(STOP_CKPKDLIN108, stop_clk[1], 1);
1333 STB0899_SETFIELD_VAL(STOP_CKPKDLIN216, stop_clk[1], 1);
1335 STB0899_SETFIELD_VAL(STOP_CKINTBUF216, stop_clk[0], 1);
1336 STB0899_SETFIELD_VAL(STOP_CKCORE216, stop_clk[0], 0);
1338 STB0899_SETFIELD_VAL(STOP_CKS2DMD108, stop_clk[1], 1);
1341 /* FECM/Viterbi OFF */
1342 reg = stb0899_read_reg(state, STB0899_FECM);
1343 STB0899_SETFIELD_VAL(FECM_RSVD0, reg, 0);
1344 STB0899_SETFIELD_VAL(FECM_VITERBI_ON, reg, 0);
1345 stb0899_write_reg(state, STB0899_FECM, reg);
1347 stb0899_write_reg(state, STB0899_RSULC, 0xb1);
1348 stb0899_write_reg(state, STB0899_TSULC, 0x42);
1349 stb0899_write_reg(state, STB0899_RSLLC, 0x40);
1350 stb0899_write_reg(state, STB0899_TSLPL, 0x02);
1352 reg = stb0899_read_reg(state, STB0899_TSTRES);
1353 STB0899_SETFIELD_VAL(FRESLDPC, reg, 0);
1354 stb0899_write_reg(state, STB0899_TSTRES, reg);
1356 STB0899_SETFIELD_VAL(STOP_CHK8PSK, stop_clk[0], 1);
1357 STB0899_SETFIELD_VAL(STOP_CKFEC108, stop_clk[0], 0);
1358 STB0899_SETFIELD_VAL(STOP_CKFEC216, stop_clk[0], 0);
1360 STB0899_SETFIELD_VAL(STOP_CKPKDLIN108, stop_clk[1], 0);
1361 STB0899_SETFIELD_VAL(STOP_CKPKDLIN216, stop_clk[1], 0);
1363 STB0899_SETFIELD_VAL(STOP_CKINTBUF216, stop_clk[0], 0);
1364 STB0899_SETFIELD_VAL(STOP_CKCORE216, stop_clk[0], 0);
1366 STB0899_SETFIELD_VAL(STOP_CKS2DMD108, stop_clk[1], 0);
1369 /* FECM/Viterbi ON */
1370 reg = stb0899_read_reg(state, STB0899_FECM);
1371 STB0899_SETFIELD_VAL(FECM_RSVD0, reg, 1);
1372 STB0899_SETFIELD_VAL(FECM_VITERBI_ON, reg, 1);
1373 stb0899_write_reg(state, STB0899_FECM, reg);
1375 stb0899_write_reg(state, STB0899_RSULC, 0xa1);
1376 stb0899_write_reg(state, STB0899_TSULC, 0x61);
1377 stb0899_write_reg(state, STB0899_RSLLC, 0x42);
1379 reg = stb0899_read_reg(state, STB0899_TSTRES);
1380 STB0899_SETFIELD_VAL(FRESLDPC, reg, 1);
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], 1);
1385 STB0899_SETFIELD_VAL(STOP_CKFEC216, stop_clk[0], 1);
1387 STB0899_SETFIELD_VAL(STOP_CKPKDLIN108, stop_clk[1], 1);
1388 STB0899_SETFIELD_VAL(STOP_CKPKDLIN216, stop_clk[1], 1);
1390 STB0899_SETFIELD_VAL(STOP_CKCORE216, stop_clk[0], 0);
1392 STB0899_SETFIELD_VAL(STOP_CKS2DMD108, stop_clk[1], 1);
1395 dprintk(state->verbose, FE_ERROR, 1, "Unsupported delivery system");
1398 STB0899_SETFIELD_VAL(STOP_CKADCI108, stop_clk[0], 0);
1399 stb0899_write_regs(state, STB0899_STOPCLK1, stop_clk, 2);
1403 * stb0899_set_iterations
1404 * set the LDPC iteration scale function
1406 static void stb0899_set_iterations(struct stb0899_state *state)
1408 struct stb0899_internal *internal = &state->internal;
1409 struct stb0899_config *config = state->config;
1414 iter_scale = 17 * (internal->master_clk / 1000);
1415 iter_scale += 410000;
1416 iter_scale /= (internal->srate / 1000000);
1419 if (iter_scale > config->ldpc_max_iter)
1420 iter_scale = config->ldpc_max_iter;
1422 reg = STB0899_READ_S2REG(STB0899_S2FEC, MAX_ITER);
1423 STB0899_SETFIELD_VAL(MAX_ITERATIONS, reg, iter_scale);
1424 stb0899_write_s2reg(state, STB0899_S2FEC, STB0899_BASE_MAX_ITER, STB0899_OFF0_MAX_ITER, reg);
1427 static enum dvbfe_search stb0899_search(struct dvb_frontend *fe)
1429 struct stb0899_state *state = fe->demodulator_priv;
1430 struct stb0899_params *i_params = &state->params;
1431 struct stb0899_internal *internal = &state->internal;
1432 struct stb0899_config *config = state->config;
1433 struct dtv_frontend_properties *props = &fe->dtv_property_cache;
1435 u32 SearchRange, gain;
1437 i_params->freq = props->frequency;
1438 i_params->srate = props->symbol_rate;
1439 state->delsys = props->delivery_system;
1440 dprintk(state->verbose, FE_DEBUG, 1, "delivery system=%d", state->delsys);
1442 SearchRange = 10000000;
1443 dprintk(state->verbose, FE_DEBUG, 1, "Frequency=%d, Srate=%d", i_params->freq, i_params->srate);
1444 /* checking Search Range is meaningless for a fixed 3 Mhz */
1445 if (INRANGE(i_params->srate, 1000000, 45000000)) {
1446 dprintk(state->verbose, FE_DEBUG, 1, "Parameters IN RANGE");
1447 stb0899_set_delivery(state);
1449 if (state->config->tuner_set_rfsiggain) {
1450 if (internal->srate > 15000000)
1451 gain = 8; /* 15Mb < srate < 45Mb, gain = 8dB */
1452 else if (internal->srate > 5000000)
1453 gain = 12; /* 5Mb < srate < 15Mb, gain = 12dB */
1455 gain = 14; /* 1Mb < srate < 5Mb, gain = 14db */
1456 state->config->tuner_set_rfsiggain(fe, gain);
1459 if (i_params->srate <= 5000000)
1460 stb0899_set_mclk(state, config->lo_clk);
1462 stb0899_set_mclk(state, config->hi_clk);
1464 switch (state->delsys) {
1467 dprintk(state->verbose, FE_DEBUG, 1, "DVB-S delivery system");
1468 internal->freq = i_params->freq;
1469 internal->srate = i_params->srate;
1471 * search = user search range +
1473 * 2 * Tuner_step_size +
1474 * 10% of the symbol rate
1476 internal->srch_range = SearchRange + 1500000 + (i_params->srate / 5);
1477 internal->derot_percent = 30;
1479 /* What to do for tuners having no bandwidth setup ? */
1480 /* enable tuner I/O */
1481 stb0899_i2c_gate_ctrl(&state->frontend, 1);
1483 if (state->config->tuner_set_bandwidth)
1484 state->config->tuner_set_bandwidth(fe, (13 * (stb0899_carr_width(state) + SearchRange)) / 10);
1485 if (state->config->tuner_get_bandwidth)
1486 state->config->tuner_get_bandwidth(fe, &internal->tuner_bw);
1488 /* disable tuner I/O */
1489 stb0899_i2c_gate_ctrl(&state->frontend, 0);
1491 /* Set DVB-S1 AGC */
1492 stb0899_write_reg(state, STB0899_AGCRFCFG, 0x11);
1494 /* Run the search algorithm */
1495 dprintk(state->verbose, FE_DEBUG, 1, "running DVB-S search algo ..");
1496 if (stb0899_dvbs_algo(state) == RANGEOK) {
1498 dprintk(state->verbose, FE_DEBUG, 1,
1499 "-------------------------------------> DVB-S LOCK !");
1501 // stb0899_write_reg(state, STB0899_ERRCTRL1, 0x3d); /* Viterbi Errors */
1502 // internal->v_status = stb0899_read_reg(state, STB0899_VSTATUS);
1503 // internal->err_ctrl = stb0899_read_reg(state, STB0899_ERRCTRL1);
1504 // dprintk(state->verbose, FE_DEBUG, 1, "VSTATUS=0x%02x", internal->v_status);
1505 // dprintk(state->verbose, FE_DEBUG, 1, "ERR_CTRL=0x%02x", internal->err_ctrl);
1507 return DVBFE_ALGO_SEARCH_SUCCESS;
1511 return DVBFE_ALGO_SEARCH_FAILED;
1515 internal->freq = i_params->freq;
1516 internal->srate = i_params->srate;
1517 internal->srch_range = SearchRange;
1519 /* enable tuner I/O */
1520 stb0899_i2c_gate_ctrl(&state->frontend, 1);
1522 if (state->config->tuner_set_bandwidth)
1523 state->config->tuner_set_bandwidth(fe, (stb0899_carr_width(state) + SearchRange));
1524 if (state->config->tuner_get_bandwidth)
1525 state->config->tuner_get_bandwidth(fe, &internal->tuner_bw);
1527 /* disable tuner I/O */
1528 stb0899_i2c_gate_ctrl(&state->frontend, 0);
1530 // pParams->SpectralInv = pSearch->IQ_Inversion;
1532 /* Set DVB-S2 AGC */
1533 stb0899_write_reg(state, STB0899_AGCRFCFG, 0x1c);
1535 /* Set IterScale =f(MCLK,SYMB) */
1536 stb0899_set_iterations(state);
1538 /* Run the search algorithm */
1539 dprintk(state->verbose, FE_DEBUG, 1, "running DVB-S2 search algo ..");
1540 if (stb0899_dvbs2_algo(state) == DVBS2_FEC_LOCK) {
1542 dprintk(state->verbose, FE_DEBUG, 1,
1543 "-------------------------------------> DVB-S2 LOCK !");
1545 // stb0899_write_reg(state, STB0899_ERRCTRL1, 0xb6); /* Packet Errors */
1546 // internal->v_status = stb0899_read_reg(state, STB0899_VSTATUS);
1547 // internal->err_ctrl = stb0899_read_reg(state, STB0899_ERRCTRL1);
1549 return DVBFE_ALGO_SEARCH_SUCCESS;
1553 return DVBFE_ALGO_SEARCH_FAILED;
1557 dprintk(state->verbose, FE_ERROR, 1, "Unsupported delivery system");
1558 return DVBFE_ALGO_SEARCH_INVALID;
1562 return DVBFE_ALGO_SEARCH_ERROR;
1565 static int stb0899_get_frontend(struct dvb_frontend *fe,
1566 struct dtv_frontend_properties *p)
1568 struct stb0899_state *state = fe->demodulator_priv;
1569 struct stb0899_internal *internal = &state->internal;
1571 dprintk(state->verbose, FE_DEBUG, 1, "Get params");
1572 p->symbol_rate = internal->srate;
1573 p->frequency = internal->freq;
1578 static enum dvbfe_algo stb0899_frontend_algo(struct dvb_frontend *fe)
1580 return DVBFE_ALGO_CUSTOM;
1583 static const struct dvb_frontend_ops stb0899_ops = {
1584 .delsys = { SYS_DVBS, SYS_DVBS2, SYS_DSS },
1586 .name = "STB0899 Multistandard",
1587 .frequency_min = 950000,
1588 .frequency_max = 2150000,
1589 .frequency_stepsize = 0,
1590 .frequency_tolerance = 0,
1591 .symbol_rate_min = 5000000,
1592 .symbol_rate_max = 45000000,
1594 .caps = FE_CAN_INVERSION_AUTO |
1596 FE_CAN_2G_MODULATION |
1600 .detach = stb0899_detach,
1601 .release = stb0899_release,
1602 .init = stb0899_init,
1603 .sleep = stb0899_sleep,
1604 // .wakeup = stb0899_wakeup,
1606 .i2c_gate_ctrl = stb0899_i2c_gate_ctrl,
1608 .get_frontend_algo = stb0899_frontend_algo,
1609 .search = stb0899_search,
1610 .get_frontend = stb0899_get_frontend,
1613 .read_status = stb0899_read_status,
1614 .read_snr = stb0899_read_snr,
1615 .read_signal_strength = stb0899_read_signal_strength,
1616 .read_ber = stb0899_read_ber,
1618 .set_voltage = stb0899_set_voltage,
1619 .set_tone = stb0899_set_tone,
1621 .diseqc_send_master_cmd = stb0899_send_diseqc_msg,
1622 .diseqc_recv_slave_reply = stb0899_recv_slave_reply,
1623 .diseqc_send_burst = stb0899_send_diseqc_burst,
1626 struct dvb_frontend *stb0899_attach(struct stb0899_config *config, struct i2c_adapter *i2c)
1628 struct stb0899_state *state = NULL;
1630 state = kzalloc(sizeof (struct stb0899_state), GFP_KERNEL);
1634 state->verbose = &verbose;
1635 state->config = config;
1637 state->frontend.ops = stb0899_ops;
1638 state->frontend.demodulator_priv = state;
1639 /* use configured inversion as default -- we'll later autodetect inversion */
1640 state->internal.inversion = config->inversion;
1642 stb0899_wakeup(&state->frontend);
1643 if (stb0899_get_dev_id(state) == -ENODEV) {
1644 printk("%s: Exiting .. !\n", __func__);
1648 printk("%s: Attaching STB0899 \n", __func__);
1649 return &state->frontend;
1655 EXPORT_SYMBOL(stb0899_attach);
1656 MODULE_PARM_DESC(verbose, "Set Verbosity level");
1657 MODULE_AUTHOR("Manu Abraham");
1658 MODULE_DESCRIPTION("STB0899 Multi-Std frontend");
1659 MODULE_LICENSE("GPL");