4 * Driver for ST STV0367 DVB-T & DVB-C demodulator IC.
6 * Copyright (C) ST Microelectronics.
7 * Copyright (C) 2010,2011 NetUP Inc.
8 * Copyright (C) 2010,2011 Igor M. Liplianin <liplianin@netup.ru>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
22 #include <linux/kernel.h>
23 #include <linux/module.h>
24 #include <linux/string.h>
25 #include <linux/slab.h>
26 #include <linux/i2c.h>
31 #include "stv0367_defs.h"
32 #include "stv0367_regs.h"
33 #include "stv0367_priv.h"
35 /* Max transfer size done by I2C transfer functions */
36 #define MAX_XFER_SIZE 64
39 module_param_named(debug, stvdebug, int, 0644);
42 module_param_named(i2c_debug, i2cdebug, int, 0644);
44 #define dprintk(args...) \
47 printk(KERN_DEBUG args); \
51 enum active_demod_state { demod_none, demod_ter, demod_cab };
53 struct stv0367cab_state {
54 enum stv0367_cab_signal_type state;
60 int locked; /* channel found */
61 u32 freq_khz; /* found frequency (in kHz) */
62 u32 symbol_rate; /* found symbol rate (in Bds) */
63 enum fe_spectral_inversion spect_inv; /* Spectrum Inversion */
64 u32 qamfec_status_reg; /* status reg to poll for FEC Lock */
67 struct stv0367ter_state {
69 enum stv0367_ter_signal_type state;
70 enum stv0367_ter_if_iq_mode if_iq_mode;
71 enum stv0367_ter_mode mode;/* mode 2K or 8K */
72 enum fe_guard_interval guard;
73 enum stv0367_ter_hierarchy hierarchy;
75 enum fe_spectral_inversion sense; /* current search spectrum */
76 u8 force; /* force mode/guard */
77 u8 bw; /* channel width 6, 7 or 8 in MHz */
78 u8 pBW; /* channel width used during previous lock */
82 s8 echo_pos; /* echo position */
88 struct stv0367_state {
89 struct dvb_frontend fe;
90 struct i2c_adapter *i2c;
92 const struct stv0367_config *config;
95 struct stv0367cab_state *cab_state;
97 struct stv0367ter_state *ter_state;
98 /* flags for operation control */
101 u8 reinit_on_setfrontend;
103 enum active_demod_state activedemod;
106 #define RF_LOOKUP_TABLE_SIZE 31
107 #define RF_LOOKUP_TABLE2_SIZE 16
108 /* RF Level (for RF AGC->AGC1) Lookup Table, depends on the board and tuner.*/
109 static const s32 stv0367cab_RF_LookUp1[RF_LOOKUP_TABLE_SIZE][RF_LOOKUP_TABLE_SIZE] = {
111 48, 50, 51, 53, 54, 56, 57, 58, 60, 61, 62, 63,
112 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75,
113 76, 77, 78, 80, 83, 85, 88,
115 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33,
116 34, 35, 36, 37, 38, 39, 41, 42, 43, 44, 46, 47,
117 49, 50, 52, 53, 54, 55, 56,
120 /* RF Level (for IF AGC->AGC2) Lookup Table, depends on the board and tuner.*/
121 static const s32 stv0367cab_RF_LookUp2[RF_LOOKUP_TABLE2_SIZE][RF_LOOKUP_TABLE2_SIZE] = {
123 28, 29, 31, 32, 34, 35, 36, 37,
124 38, 39, 40, 41, 42, 43, 44, 45,
126 57, 58, 59, 60, 61, 62, 63, 64,
127 65, 66, 67, 68, 69, 70, 71, 72,
132 int stv0367_writeregs(struct stv0367_state *state, u16 reg, u8 *data, int len)
134 u8 buf[MAX_XFER_SIZE];
135 struct i2c_msg msg = {
136 .addr = state->config->demod_address,
143 if (2 + len > sizeof(buf)) {
145 "%s: i2c wr reg=%04x: len=%d is too big!\n",
146 KBUILD_MODNAME, reg, len);
153 memcpy(buf + 2, data, len);
156 printk(KERN_DEBUG "%s: [%02x] %02x: %02x\n", __func__,
157 state->config->demod_address, reg, buf[2]);
159 ret = i2c_transfer(state->i2c, &msg, 1);
161 printk(KERN_ERR "%s: i2c write error! ([%02x] %02x: %02x)\n",
162 __func__, state->config->demod_address, reg, buf[2]);
164 return (ret != 1) ? -EREMOTEIO : 0;
167 static int stv0367_writereg(struct stv0367_state *state, u16 reg, u8 data)
169 return stv0367_writeregs(state, reg, &data, 1);
172 static u8 stv0367_readreg(struct stv0367_state *state, u16 reg)
176 struct i2c_msg msg[] = {
178 .addr = state->config->demod_address,
183 .addr = state->config->demod_address,
194 ret = i2c_transfer(state->i2c, msg, 2);
196 printk(KERN_ERR "%s: i2c read error ([%02x] %02x: %02x)\n",
197 __func__, state->config->demod_address, reg, b1[0]);
200 printk(KERN_DEBUG "%s: [%02x] %02x: %02x\n", __func__,
201 state->config->demod_address, reg, b1[0]);
206 static void extract_mask_pos(u32 label, u8 *mask, u8 *pos)
208 u8 position = 0, i = 0;
210 (*mask) = label & 0xff;
212 while ((position == 0) && (i < 8)) {
213 position = ((*mask) >> i) & 0x01;
220 static void stv0367_writebits(struct stv0367_state *state, u32 label, u8 val)
224 reg = stv0367_readreg(state, (label >> 16) & 0xffff);
225 extract_mask_pos(label, &mask, &pos);
227 val = mask & (val << pos);
229 reg = (reg & (~mask)) | val;
230 stv0367_writereg(state, (label >> 16) & 0xffff, reg);
234 static void stv0367_setbits(u8 *reg, u32 label, u8 val)
238 extract_mask_pos(label, &mask, &pos);
240 val = mask & (val << pos);
242 (*reg) = ((*reg) & (~mask)) | val;
245 static u8 stv0367_readbits(struct stv0367_state *state, u32 label)
250 extract_mask_pos(label, &mask, &pos);
252 val = stv0367_readreg(state, label >> 16);
253 val = (val & mask) >> pos;
258 #if 0 /* Currently, unused */
259 static u8 stv0367_getbits(u8 reg, u32 label)
263 extract_mask_pos(label, &mask, &pos);
265 return (reg & mask) >> pos;
269 static void stv0367_write_table(struct stv0367_state *state,
270 const struct st_register *deftab)
277 stv0367_writereg(state, deftab[i].addr, deftab[i].value);
282 static void stv0367_pll_setup(struct stv0367_state *state,
283 u32 icspeed, u32 xtal)
285 /* note on regs: R367TER_* and R367CAB_* defines each point to
286 * 0xf0d8, so just use R367TER_ for both cases
290 case STV0367_ICSPEED_58000:
294 dprintk("STV0367 SetCLKgen for 58MHz IC and 27Mhz crystal\n");
295 /* PLLMDIV: 27, PLLNDIV: 232 */
296 stv0367_writereg(state, R367TER_PLLMDIV, 0x1b);
297 stv0367_writereg(state, R367TER_PLLNDIV, 0xe8);
302 case STV0367_ICSPEED_53125:
304 /* set internal freq to 53.125MHz */
306 stv0367_writereg(state, R367TER_PLLMDIV, 0x2);
307 stv0367_writereg(state, R367TER_PLLNDIV, 0x1b);
310 stv0367_writereg(state, R367TER_PLLMDIV, 0xa);
311 stv0367_writereg(state, R367TER_PLLNDIV, 0x55);
315 dprintk("FE_STV0367TER_SetCLKgen for 27Mhz\n");
316 stv0367_writereg(state, R367TER_PLLMDIV, 0x1);
317 stv0367_writereg(state, R367TER_PLLNDIV, 0x8);
320 stv0367_writereg(state, R367TER_PLLMDIV, 0xc);
321 stv0367_writereg(state, R367TER_PLLNDIV, 0x55);
326 stv0367_writereg(state, R367TER_PLLSETUP, 0x18);
329 static int stv0367_get_if_khz(struct stv0367_state *state, u32 *ifkhz)
331 if (state->auto_if_khz && state->fe.ops.tuner_ops.get_if_frequency) {
332 state->fe.ops.tuner_ops.get_if_frequency(&state->fe, ifkhz);
333 *ifkhz = *ifkhz / 1000; /* hz -> khz */
335 *ifkhz = state->config->if_khz;
340 static int stv0367ter_gate_ctrl(struct dvb_frontend *fe, int enable)
342 struct stv0367_state *state = fe->demodulator_priv;
343 u8 tmp = stv0367_readreg(state, R367TER_I2CRPT);
345 dprintk("%s:\n", __func__);
348 stv0367_setbits(&tmp, F367TER_STOP_ENABLE, 0);
349 stv0367_setbits(&tmp, F367TER_I2CT_ON, 1);
351 stv0367_setbits(&tmp, F367TER_STOP_ENABLE, 1);
352 stv0367_setbits(&tmp, F367TER_I2CT_ON, 0);
355 stv0367_writereg(state, R367TER_I2CRPT, tmp);
360 static u32 stv0367_get_tuner_freq(struct dvb_frontend *fe)
362 struct dvb_frontend_ops *frontend_ops = &fe->ops;
363 struct dvb_tuner_ops *tuner_ops = &frontend_ops->tuner_ops;
367 dprintk("%s:\n", __func__);
369 if (tuner_ops->get_frequency) {
370 err = tuner_ops->get_frequency(fe, &freq);
372 printk(KERN_ERR "%s: Invalid parameter\n", __func__);
376 dprintk("%s: frequency=%d\n", __func__, freq);
384 static u16 CellsCoeffs_8MHz_367cofdm[3][6][5] = {
386 {0x10EF, 0xE205, 0x10EF, 0xCE49, 0x6DA7}, /* CELL 1 COEFFS 27M*/
387 {0x2151, 0xc557, 0x2151, 0xc705, 0x6f93}, /* CELL 2 COEFFS */
388 {0x2503, 0xc000, 0x2503, 0xc375, 0x7194}, /* CELL 3 COEFFS */
389 {0x20E9, 0xca94, 0x20e9, 0xc153, 0x7194}, /* CELL 4 COEFFS */
390 {0x06EF, 0xF852, 0x06EF, 0xC057, 0x7207}, /* CELL 5 COEFFS */
391 {0x0000, 0x0ECC, 0x0ECC, 0x0000, 0x3647} /* CELL 6 COEFFS */
393 {0x10A0, 0xE2AF, 0x10A1, 0xCE76, 0x6D6D}, /* CELL 1 COEFFS 25M*/
394 {0x20DC, 0xC676, 0x20D9, 0xC80A, 0x6F29},
395 {0x2532, 0xC000, 0x251D, 0xC391, 0x706F},
396 {0x1F7A, 0xCD2B, 0x2032, 0xC15E, 0x711F},
397 {0x0698, 0xFA5E, 0x0568, 0xC059, 0x7193},
398 {0x0000, 0x0918, 0x149C, 0x0000, 0x3642} /* CELL 6 COEFFS */
400 {0x0000, 0x0000, 0x0000, 0x0000, 0x0000}, /* 30M */
401 {0x0000, 0x0000, 0x0000, 0x0000, 0x0000},
402 {0x0000, 0x0000, 0x0000, 0x0000, 0x0000},
403 {0x0000, 0x0000, 0x0000, 0x0000, 0x0000},
404 {0x0000, 0x0000, 0x0000, 0x0000, 0x0000},
405 {0x0000, 0x0000, 0x0000, 0x0000, 0x0000}
409 static u16 CellsCoeffs_7MHz_367cofdm[3][6][5] = {
411 {0x12CA, 0xDDAF, 0x12CA, 0xCCEB, 0x6FB1}, /* CELL 1 COEFFS 27M*/
412 {0x2329, 0xC000, 0x2329, 0xC6B0, 0x725F}, /* CELL 2 COEFFS */
413 {0x2394, 0xC000, 0x2394, 0xC2C7, 0x7410}, /* CELL 3 COEFFS */
414 {0x251C, 0xC000, 0x251C, 0xC103, 0x74D9}, /* CELL 4 COEFFS */
415 {0x0804, 0xF546, 0x0804, 0xC040, 0x7544}, /* CELL 5 COEFFS */
416 {0x0000, 0x0CD9, 0x0CD9, 0x0000, 0x370A} /* CELL 6 COEFFS */
418 {0x1285, 0xDE47, 0x1285, 0xCD17, 0x6F76}, /*25M*/
419 {0x234C, 0xC000, 0x2348, 0xC6DA, 0x7206},
420 {0x23B4, 0xC000, 0x23AC, 0xC2DB, 0x73B3},
421 {0x253D, 0xC000, 0x25B6, 0xC10B, 0x747F},
422 {0x0721, 0xF79C, 0x065F, 0xC041, 0x74EB},
423 {0x0000, 0x08FA, 0x1162, 0x0000, 0x36FF}
425 {0x0000, 0x0000, 0x0000, 0x0000, 0x0000}, /* 30M */
426 {0x0000, 0x0000, 0x0000, 0x0000, 0x0000},
427 {0x0000, 0x0000, 0x0000, 0x0000, 0x0000},
428 {0x0000, 0x0000, 0x0000, 0x0000, 0x0000},
429 {0x0000, 0x0000, 0x0000, 0x0000, 0x0000},
430 {0x0000, 0x0000, 0x0000, 0x0000, 0x0000}
434 static u16 CellsCoeffs_6MHz_367cofdm[3][6][5] = {
436 {0x1699, 0xD5B8, 0x1699, 0xCBC3, 0x713B}, /* CELL 1 COEFFS 27M*/
437 {0x2245, 0xC000, 0x2245, 0xC568, 0x74D5}, /* CELL 2 COEFFS */
438 {0x227F, 0xC000, 0x227F, 0xC1FC, 0x76C6}, /* CELL 3 COEFFS */
439 {0x235E, 0xC000, 0x235E, 0xC0A7, 0x778A}, /* CELL 4 COEFFS */
440 {0x0ECB, 0xEA0B, 0x0ECB, 0xC027, 0x77DD}, /* CELL 5 COEFFS */
441 {0x0000, 0x0B68, 0x0B68, 0x0000, 0xC89A}, /* CELL 6 COEFFS */
443 {0x1655, 0xD64E, 0x1658, 0xCBEF, 0x70FE}, /*25M*/
444 {0x225E, 0xC000, 0x2256, 0xC589, 0x7489},
445 {0x2293, 0xC000, 0x2295, 0xC209, 0x767E},
446 {0x2377, 0xC000, 0x23AA, 0xC0AB, 0x7746},
447 {0x0DC7, 0xEBC8, 0x0D07, 0xC027, 0x7799},
448 {0x0000, 0x0888, 0x0E9C, 0x0000, 0x3757}
451 {0x0000, 0x0000, 0x0000, 0x0000, 0x0000}, /* 30M */
452 {0x0000, 0x0000, 0x0000, 0x0000, 0x0000},
453 {0x0000, 0x0000, 0x0000, 0x0000, 0x0000},
454 {0x0000, 0x0000, 0x0000, 0x0000, 0x0000},
455 {0x0000, 0x0000, 0x0000, 0x0000, 0x0000},
456 {0x0000, 0x0000, 0x0000, 0x0000, 0x0000}
460 static u32 stv0367ter_get_mclk(struct stv0367_state *state, u32 ExtClk_Hz)
462 u32 mclk_Hz = 0; /* master clock frequency (Hz) */
465 dprintk("%s:\n", __func__);
467 if (stv0367_readbits(state, F367TER_BYPASS_PLLXN) == 0) {
468 n = (u32)stv0367_readbits(state, F367TER_PLL_NDIV);
472 m = (u32)stv0367_readbits(state, F367TER_PLL_MDIV);
476 p = (u32)stv0367_readbits(state, F367TER_PLL_PDIV);
480 mclk_Hz = ((ExtClk_Hz / 2) * n) / (m * (1 << p));
482 dprintk("N=%d M=%d P=%d mclk_Hz=%d ExtClk_Hz=%d\n",
483 n, m, p, mclk_Hz, ExtClk_Hz);
487 dprintk("%s: mclk_Hz=%d\n", __func__, mclk_Hz);
492 static int stv0367ter_filt_coeff_init(struct stv0367_state *state,
493 u16 CellsCoeffs[3][6][5], u32 DemodXtal)
497 dprintk("%s:\n", __func__);
499 freq = stv0367ter_get_mclk(state, DemodXtal);
501 if (freq == 53125000)
502 k = 1; /* equivalent to Xtal 25M on 362*/
503 else if (freq == 54000000)
504 k = 0; /* equivalent to Xtal 27M on 362*/
505 else if (freq == 52500000)
506 k = 2; /* equivalent to Xtal 30M on 362*/
510 for (i = 1; i <= 6; i++) {
511 stv0367_writebits(state, F367TER_IIR_CELL_NB, i - 1);
513 for (j = 1; j <= 5; j++) {
514 stv0367_writereg(state,
515 (R367TER_IIRCX_COEFF1_MSB + 2 * (j - 1)),
516 MSB(CellsCoeffs[k][i-1][j-1]));
517 stv0367_writereg(state,
518 (R367TER_IIRCX_COEFF1_LSB + 2 * (j - 1)),
519 LSB(CellsCoeffs[k][i-1][j-1]));
527 static void stv0367ter_agc_iir_lock_detect_set(struct stv0367_state *state)
529 dprintk("%s:\n", __func__);
531 stv0367_writebits(state, F367TER_LOCK_DETECT_LSB, 0x00);
534 stv0367_writebits(state, F367TER_LOCK_DETECT_CHOICE, 0x00);
535 stv0367_writebits(state, F367TER_LOCK_DETECT_MSB, 0x06);
536 stv0367_writebits(state, F367TER_AUT_AGC_TARGET_LSB, 0x04);
539 stv0367_writebits(state, F367TER_LOCK_DETECT_CHOICE, 0x01);
540 stv0367_writebits(state, F367TER_LOCK_DETECT_MSB, 0x06);
541 stv0367_writebits(state, F367TER_AUT_AGC_TARGET_LSB, 0x04);
544 stv0367_writebits(state, F367TER_LOCK_DETECT_CHOICE, 0x02);
545 stv0367_writebits(state, F367TER_LOCK_DETECT_MSB, 0x01);
546 stv0367_writebits(state, F367TER_AUT_AGC_TARGET_LSB, 0x00);
549 stv0367_writebits(state, F367TER_LOCK_DETECT_CHOICE, 0x03);
550 stv0367_writebits(state, F367TER_LOCK_DETECT_MSB, 0x01);
551 stv0367_writebits(state, F367TER_AUT_AGC_TARGET_LSB, 0x00);
555 static int stv0367_iir_filt_init(struct stv0367_state *state, u8 Bandwidth,
558 dprintk("%s:\n", __func__);
560 stv0367_writebits(state, F367TER_NRST_IIR, 0);
564 if (!stv0367ter_filt_coeff_init(state,
565 CellsCoeffs_6MHz_367cofdm,
570 if (!stv0367ter_filt_coeff_init(state,
571 CellsCoeffs_7MHz_367cofdm,
576 if (!stv0367ter_filt_coeff_init(state,
577 CellsCoeffs_8MHz_367cofdm,
585 stv0367_writebits(state, F367TER_NRST_IIR, 1);
590 static void stv0367ter_agc_iir_rst(struct stv0367_state *state)
595 dprintk("%s:\n", __func__);
597 com_n = stv0367_readbits(state, F367TER_COM_N);
599 stv0367_writebits(state, F367TER_COM_N, 0x07);
601 stv0367_writebits(state, F367TER_COM_SOFT_RSTN, 0x00);
602 stv0367_writebits(state, F367TER_COM_AGC_ON, 0x00);
604 stv0367_writebits(state, F367TER_COM_SOFT_RSTN, 0x01);
605 stv0367_writebits(state, F367TER_COM_AGC_ON, 0x01);
607 stv0367_writebits(state, F367TER_COM_N, com_n);
611 static int stv0367ter_duration(s32 mode, int tempo1, int tempo2, int tempo3)
616 local_tempo = tempo1;
619 local_tempo = tempo2;
623 local_tempo = tempo3;
629 /* msleep(local_tempo); */
634 stv0367_ter_signal_type stv0367ter_check_syr(struct stv0367_state *state)
637 unsigned short int SYR_var;
640 dprintk("%s:\n", __func__);
642 SYR_var = stv0367_readbits(state, F367TER_SYR_LOCK);
644 while ((!SYR_var) && (wd > 0)) {
645 usleep_range(2000, 3000);
647 SYR_var = stv0367_readbits(state, F367TER_SYR_LOCK);
651 SYRStatus = FE_TER_NOSYMBOL;
653 SYRStatus = FE_TER_SYMBOLOK;
655 dprintk("stv0367ter_check_syr SYRStatus %s\n",
656 SYR_var == 0 ? "No Symbol" : "OK");
662 stv0367_ter_signal_type stv0367ter_check_cpamp(struct stv0367_state *state,
666 s32 CPAMPvalue = 0, CPAMPStatus, CPAMPMin;
669 dprintk("%s:\n", __func__);
685 CPAMPMin = 0xffff; /*drives to NOCPAMP */
689 dprintk("%s: CPAMPMin=%d wd=%d\n", __func__, CPAMPMin, wd);
691 CPAMPvalue = stv0367_readbits(state, F367TER_PPM_CPAMP_DIRECT);
692 while ((CPAMPvalue < CPAMPMin) && (wd > 0)) {
693 usleep_range(1000, 2000);
695 CPAMPvalue = stv0367_readbits(state, F367TER_PPM_CPAMP_DIRECT);
696 /*dprintk("CPAMPvalue= %d at wd=%d\n",CPAMPvalue,wd); */
698 dprintk("******last CPAMPvalue= %d at wd=%d\n", CPAMPvalue, wd);
699 if (CPAMPvalue < CPAMPMin) {
700 CPAMPStatus = FE_TER_NOCPAMP;
701 dprintk("%s: CPAMP failed\n", __func__);
703 dprintk("%s: CPAMP OK !\n", __func__);
704 CPAMPStatus = FE_TER_CPAMPOK;
710 static enum stv0367_ter_signal_type
711 stv0367ter_lock_algo(struct stv0367_state *state)
713 enum stv0367_ter_signal_type ret_flag;
715 u8 try, u_var1 = 0, u_var2 = 0, u_var3 = 0, u_var4 = 0, mode, guard;
718 dprintk("%s:\n", __func__);
725 ret_flag = FE_TER_LOCKOK;
727 stv0367_writebits(state, F367TER_CORE_ACTIVE, 0);
729 if (state->config->if_iq_mode != 0)
730 stv0367_writebits(state, F367TER_COM_N, 0x07);
732 stv0367_writebits(state, F367TER_GUARD, 3);/* suggest 2k 1/4 */
733 stv0367_writebits(state, F367TER_MODE, 0);
734 stv0367_writebits(state, F367TER_SYR_TR_DIS, 0);
735 usleep_range(5000, 10000);
737 stv0367_writebits(state, F367TER_CORE_ACTIVE, 1);
740 if (stv0367ter_check_syr(state) == FE_TER_NOSYMBOL)
741 return FE_TER_NOSYMBOL;
743 if chip locked on wrong mode first try,
744 it must lock correctly second try */
745 mode = stv0367_readbits(state, F367TER_SYR_MODE);
746 if (stv0367ter_check_cpamp(state, mode) ==
749 ret_flag = FE_TER_NOCPAMP;
755 } while ((try < 10) && (ret_flag != FE_TER_LOCKOK));
757 tmp = stv0367_readreg(state, R367TER_SYR_STAT);
758 tmp2 = stv0367_readreg(state, R367TER_STATUS);
759 dprintk("state=%p\n", state);
760 dprintk("LOCK OK! mode=%d SYR_STAT=0x%x R367TER_STATUS=0x%x\n",
763 tmp = stv0367_readreg(state, R367TER_PRVIT);
764 tmp2 = stv0367_readreg(state, R367TER_I2CRPT);
765 dprintk("PRVIT=0x%x I2CRPT=0x%x\n", tmp, tmp2);
767 tmp = stv0367_readreg(state, R367TER_GAIN_SRC1);
768 dprintk("GAIN_SRC1=0x%x\n", tmp);
770 if ((mode != 0) && (mode != 1) && (mode != 2))
773 /*guard=stv0367_readbits(state,F367TER_SYR_GUARD); */
775 /*suppress EPQ auto for SYR_GARD 1/16 or 1/32
776 and set channel predictor in automatic */
782 stv0367_writebits(state, F367TER_AUTO_LE_EN, 0);
783 stv0367_writereg(state, R367TER_CHC_CTL, 0x01);
787 stv0367_writebits(state, F367TER_AUTO_LE_EN, 1);
788 stv0367_writereg(state, R367TER_CHC_CTL, 0x11);
796 /*reset fec an reedsolo FOR 367 only*/
797 stv0367_writebits(state, F367TER_RST_SFEC, 1);
798 stv0367_writebits(state, F367TER_RST_REEDSOLO, 1);
799 usleep_range(1000, 2000);
800 stv0367_writebits(state, F367TER_RST_SFEC, 0);
801 stv0367_writebits(state, F367TER_RST_REEDSOLO, 0);
803 u_var1 = stv0367_readbits(state, F367TER_LK);
804 u_var2 = stv0367_readbits(state, F367TER_PRF);
805 u_var3 = stv0367_readbits(state, F367TER_TPS_LOCK);
806 /* u_var4=stv0367_readbits(state,F367TER_TSFIFO_LINEOK); */
808 wd = stv0367ter_duration(mode, 125, 500, 250);
809 tempo = stv0367ter_duration(mode, 4, 16, 8);
811 /*while ( ((!u_var1)||(!u_var2)||(!u_var3)||(!u_var4)) && (wd>=0)) */
812 while (((!u_var1) || (!u_var2) || (!u_var3)) && (wd >= 0)) {
813 usleep_range(1000 * tempo, 1000 * (tempo + 1));
815 u_var1 = stv0367_readbits(state, F367TER_LK);
816 u_var2 = stv0367_readbits(state, F367TER_PRF);
817 u_var3 = stv0367_readbits(state, F367TER_TPS_LOCK);
818 /*u_var4=stv0367_readbits(state, F367TER_TSFIFO_LINEOK); */
822 return FE_TER_NOLOCK;
826 return FE_TER_NOPRFOUND;
831 guard = stv0367_readbits(state, F367TER_SYR_GUARD);
832 stv0367_writereg(state, R367TER_CHC_CTL, 0x11);
836 stv0367_writebits(state, F367TER_AUTO_LE_EN, 0);
837 /*stv0367_writereg(state,R367TER_CHC_CTL, 0x1);*/
838 stv0367_writebits(state, F367TER_SYR_FILTER, 0);
842 stv0367_writebits(state, F367TER_AUTO_LE_EN, 1);
843 /*stv0367_writereg(state,R367TER_CHC_CTL, 0x11);*/
844 stv0367_writebits(state, F367TER_SYR_FILTER, 1);
851 /* apply Sfec workaround if 8K 64QAM CR!=1/2*/
852 if ((stv0367_readbits(state, F367TER_TPS_CONST) == 2) &&
854 (stv0367_readbits(state, F367TER_TPS_HPCODE) != 0)) {
855 stv0367_writereg(state, R367TER_SFDLYSETH, 0xc0);
856 stv0367_writereg(state, R367TER_SFDLYSETM, 0x60);
857 stv0367_writereg(state, R367TER_SFDLYSETL, 0x0);
859 stv0367_writereg(state, R367TER_SFDLYSETH, 0x0);
861 wd = stv0367ter_duration(mode, 125, 500, 250);
862 u_var4 = stv0367_readbits(state, F367TER_TSFIFO_LINEOK);
864 while ((!u_var4) && (wd >= 0)) {
865 usleep_range(1000 * tempo, 1000 * (tempo + 1));
867 u_var4 = stv0367_readbits(state, F367TER_TSFIFO_LINEOK);
871 return FE_TER_NOLOCK;
873 /* for 367 leave COM_N at 0x7 for IQ_mode*/
874 /*if(ter_state->if_iq_mode!=FE_TER_NORMAL_IF_TUNER) {
876 while ((stv0367_readbits(state,F367TER_COM_USEGAINTRK)!=1) &&
877 (stv0367_readbits(state,F367TER_COM_AGCLOCK)!=1)&&(tempo<100)) {
878 ChipWaitOrAbort(state,1);
882 stv0367_writebits(state,F367TER_COM_N,0x17);
885 stv0367_writebits(state, F367TER_SYR_TR_DIS, 1);
887 dprintk("FE_TER_LOCKOK !!!\n");
889 return FE_TER_LOCKOK;
893 static void stv0367ter_set_ts_mode(struct stv0367_state *state,
894 enum stv0367_ts_mode PathTS)
897 dprintk("%s:\n", __func__);
902 stv0367_writebits(state, F367TER_TS_DIS, 0);
905 /*for removing warning :default we can assume in parallel mode*/
906 case STV0367_PARALLEL_PUNCT_CLOCK:
907 stv0367_writebits(state, F367TER_TSFIFO_SERIAL, 0);
908 stv0367_writebits(state, F367TER_TSFIFO_DVBCI, 0);
910 case STV0367_SERIAL_PUNCT_CLOCK:
911 stv0367_writebits(state, F367TER_TSFIFO_SERIAL, 1);
912 stv0367_writebits(state, F367TER_TSFIFO_DVBCI, 1);
917 static void stv0367ter_set_clk_pol(struct stv0367_state *state,
918 enum stv0367_clk_pol clock)
921 dprintk("%s:\n", __func__);
927 case STV0367_RISINGEDGE_CLOCK:
928 stv0367_writebits(state, F367TER_TS_BYTE_CLK_INV, 1);
930 case STV0367_FALLINGEDGE_CLOCK:
931 stv0367_writebits(state, F367TER_TS_BYTE_CLK_INV, 0);
933 /*case FE_TER_CLOCK_POLARITY_DEFAULT:*/
935 stv0367_writebits(state, F367TER_TS_BYTE_CLK_INV, 0);
941 static void stv0367ter_core_sw(struct stv0367_state *state)
944 dprintk("%s:\n", __func__);
946 stv0367_writebits(state, F367TER_CORE_ACTIVE, 0);
947 stv0367_writebits(state, F367TER_CORE_ACTIVE, 1);
951 static int stv0367ter_standby(struct dvb_frontend *fe, u8 standby_on)
953 struct stv0367_state *state = fe->demodulator_priv;
955 dprintk("%s:\n", __func__);
958 stv0367_writebits(state, F367TER_STDBY, 1);
959 stv0367_writebits(state, F367TER_STDBY_FEC, 1);
960 stv0367_writebits(state, F367TER_STDBY_CORE, 1);
962 stv0367_writebits(state, F367TER_STDBY, 0);
963 stv0367_writebits(state, F367TER_STDBY_FEC, 0);
964 stv0367_writebits(state, F367TER_STDBY_CORE, 0);
970 static int stv0367ter_sleep(struct dvb_frontend *fe)
972 return stv0367ter_standby(fe, 1);
975 static int stv0367ter_init(struct dvb_frontend *fe)
977 struct stv0367_state *state = fe->demodulator_priv;
978 struct stv0367ter_state *ter_state = state->ter_state;
980 dprintk("%s:\n", __func__);
984 stv0367_write_table(state,
985 stv0367_deftabs[state->deftabs][STV0367_TAB_TER]);
987 stv0367_pll_setup(state, STV0367_ICSPEED_53125, state->config->xtal);
989 stv0367_writereg(state, R367TER_I2CRPT, 0xa0);
990 stv0367_writereg(state, R367TER_ANACTRL, 0x00);
992 /*Set TS1 and TS2 to serial or parallel mode */
993 stv0367ter_set_ts_mode(state, state->config->ts_mode);
994 stv0367ter_set_clk_pol(state, state->config->clk_pol);
996 state->chip_id = stv0367_readreg(state, R367TER_ID);
997 ter_state->first_lock = 0;
998 ter_state->unlock_counter = 2;
1003 static int stv0367ter_algo(struct dvb_frontend *fe)
1005 struct dtv_frontend_properties *p = &fe->dtv_property_cache;
1006 struct stv0367_state *state = fe->demodulator_priv;
1007 struct stv0367ter_state *ter_state = state->ter_state;
1008 int offset = 0, tempo = 0;
1010 u8 /*constell,*/ counter;
1012 s32 timing_offset = 0;
1013 u32 trl_nomrate = 0, InternalFreq = 0, temp = 0, ifkhz = 0;
1015 dprintk("%s:\n", __func__);
1017 stv0367_get_if_khz(state, &ifkhz);
1019 ter_state->frequency = p->frequency;
1020 ter_state->force = FE_TER_FORCENONE
1021 + stv0367_readbits(state, F367TER_FORCE) * 2;
1022 ter_state->if_iq_mode = state->config->if_iq_mode;
1023 switch (state->config->if_iq_mode) {
1024 case FE_TER_NORMAL_IF_TUNER: /* Normal IF mode */
1025 dprintk("ALGO: FE_TER_NORMAL_IF_TUNER selected\n");
1026 stv0367_writebits(state, F367TER_TUNER_BB, 0);
1027 stv0367_writebits(state, F367TER_LONGPATH_IF, 0);
1028 stv0367_writebits(state, F367TER_DEMUX_SWAP, 0);
1030 case FE_TER_LONGPATH_IF_TUNER: /* Long IF mode */
1031 dprintk("ALGO: FE_TER_LONGPATH_IF_TUNER selected\n");
1032 stv0367_writebits(state, F367TER_TUNER_BB, 0);
1033 stv0367_writebits(state, F367TER_LONGPATH_IF, 1);
1034 stv0367_writebits(state, F367TER_DEMUX_SWAP, 1);
1036 case FE_TER_IQ_TUNER: /* IQ mode */
1037 dprintk("ALGO: FE_TER_IQ_TUNER selected\n");
1038 stv0367_writebits(state, F367TER_TUNER_BB, 1);
1039 stv0367_writebits(state, F367TER_PPM_INVSEL, 0);
1042 printk(KERN_ERR "ALGO: wrong TUNER type selected\n");
1046 usleep_range(5000, 7000);
1048 switch (p->inversion) {
1049 case INVERSION_AUTO:
1051 dprintk("%s: inversion AUTO\n", __func__);
1052 if (ter_state->if_iq_mode == FE_TER_IQ_TUNER)
1053 stv0367_writebits(state, F367TER_IQ_INVERT,
1056 stv0367_writebits(state, F367TER_INV_SPECTR,
1062 if (ter_state->if_iq_mode == FE_TER_IQ_TUNER)
1063 stv0367_writebits(state, F367TER_IQ_INVERT,
1066 stv0367_writebits(state, F367TER_INV_SPECTR,
1072 if ((ter_state->if_iq_mode != FE_TER_NORMAL_IF_TUNER) &&
1073 (ter_state->pBW != ter_state->bw)) {
1074 stv0367ter_agc_iir_lock_detect_set(state);
1076 /*set fine agc target to 180 for LPIF or IQ mode*/
1077 /* set Q_AGCTarget */
1078 stv0367_writebits(state, F367TER_SEL_IQNTAR, 1);
1079 stv0367_writebits(state, F367TER_AUT_AGC_TARGET_MSB, 0xB);
1080 /*stv0367_writebits(state,AUT_AGC_TARGET_LSB,0x04); */
1082 /* set Q_AGCTarget */
1083 stv0367_writebits(state, F367TER_SEL_IQNTAR, 0);
1084 stv0367_writebits(state, F367TER_AUT_AGC_TARGET_MSB, 0xB);
1085 /*stv0367_writebits(state,AUT_AGC_TARGET_LSB,0x04); */
1087 if (!stv0367_iir_filt_init(state, ter_state->bw,
1088 state->config->xtal))
1090 /*set IIR filter once for 6,7 or 8MHz BW*/
1091 ter_state->pBW = ter_state->bw;
1093 stv0367ter_agc_iir_rst(state);
1096 if (ter_state->hierarchy == FE_TER_HIER_LOW_PRIO)
1097 stv0367_writebits(state, F367TER_BDI_LPSEL, 0x01);
1099 stv0367_writebits(state, F367TER_BDI_LPSEL, 0x00);
1101 InternalFreq = stv0367ter_get_mclk(state, state->config->xtal) / 1000;
1103 ((((ter_state->bw * 64 * (1 << 15) * 100)
1104 / (InternalFreq)) * 10) / 7);
1106 stv0367_writebits(state, F367TER_TRL_NOMRATE_LSB, temp % 2);
1108 stv0367_writebits(state, F367TER_TRL_NOMRATE_HI, temp / 256);
1109 stv0367_writebits(state, F367TER_TRL_NOMRATE_LO, temp % 256);
1111 temp = stv0367_readbits(state, F367TER_TRL_NOMRATE_HI) * 512 +
1112 stv0367_readbits(state, F367TER_TRL_NOMRATE_LO) * 2 +
1113 stv0367_readbits(state, F367TER_TRL_NOMRATE_LSB);
1114 temp = (int)(((1 << 17) * ter_state->bw * 1000) / (7 * (InternalFreq)));
1115 stv0367_writebits(state, F367TER_GAIN_SRC_HI, temp / 256);
1116 stv0367_writebits(state, F367TER_GAIN_SRC_LO, temp % 256);
1117 temp = stv0367_readbits(state, F367TER_GAIN_SRC_HI) * 256 +
1118 stv0367_readbits(state, F367TER_GAIN_SRC_LO);
1121 ((InternalFreq - ifkhz) * (1 << 16) / (InternalFreq));
1123 dprintk("DEROT temp=0x%x\n", temp);
1124 stv0367_writebits(state, F367TER_INC_DEROT_HI, temp / 256);
1125 stv0367_writebits(state, F367TER_INC_DEROT_LO, temp % 256);
1127 ter_state->echo_pos = 0;
1128 ter_state->ucblocks = 0; /* liplianin */
1129 ter_state->pBER = 0; /* liplianin */
1130 stv0367_writebits(state, F367TER_LONG_ECHO, ter_state->echo_pos);
1132 if (stv0367ter_lock_algo(state) != FE_TER_LOCKOK)
1135 ter_state->state = FE_TER_LOCKOK;
1137 ter_state->mode = stv0367_readbits(state, F367TER_SYR_MODE);
1138 ter_state->guard = stv0367_readbits(state, F367TER_SYR_GUARD);
1140 ter_state->first_lock = 1; /* we know sense now :) */
1142 ter_state->agc_val =
1143 (stv0367_readbits(state, F367TER_AGC1_VAL_LO) << 16) +
1144 (stv0367_readbits(state, F367TER_AGC1_VAL_HI) << 24) +
1145 stv0367_readbits(state, F367TER_AGC2_VAL_LO) +
1146 (stv0367_readbits(state, F367TER_AGC2_VAL_HI) << 8);
1148 /* Carrier offset calculation */
1149 stv0367_writebits(state, F367TER_FREEZE, 1);
1150 offset = (stv0367_readbits(state, F367TER_CRL_FOFFSET_VHI) << 16) ;
1151 offset += (stv0367_readbits(state, F367TER_CRL_FOFFSET_HI) << 8);
1152 offset += (stv0367_readbits(state, F367TER_CRL_FOFFSET_LO));
1153 stv0367_writebits(state, F367TER_FREEZE, 0);
1154 if (offset > 8388607)
1157 offset = offset * 2 / 16384;
1159 if (ter_state->mode == FE_TER_MODE_2K)
1160 offset = (offset * 4464) / 1000;/*** 1 FFT BIN=4.464khz***/
1161 else if (ter_state->mode == FE_TER_MODE_4K)
1162 offset = (offset * 223) / 100;/*** 1 FFT BIN=2.23khz***/
1163 else if (ter_state->mode == FE_TER_MODE_8K)
1164 offset = (offset * 111) / 100;/*** 1 FFT BIN=1.1khz***/
1166 if (stv0367_readbits(state, F367TER_PPM_INVSEL) == 1) {
1167 if ((stv0367_readbits(state, F367TER_INV_SPECTR) ==
1168 (stv0367_readbits(state,
1169 F367TER_STATUS_INV_SPECRUM) == 1)))
1170 offset = offset * -1;
1173 if (ter_state->bw == 6)
1174 offset = (offset * 6) / 8;
1175 else if (ter_state->bw == 7)
1176 offset = (offset * 7) / 8;
1178 ter_state->frequency += offset;
1180 tempo = 10; /* exit even if timing_offset stays null */
1181 while ((timing_offset == 0) && (tempo > 0)) {
1182 usleep_range(10000, 20000); /*was 20ms */
1183 /* fine tuning of timing offset if required */
1184 timing_offset = stv0367_readbits(state, F367TER_TRL_TOFFSET_LO)
1185 + 256 * stv0367_readbits(state,
1186 F367TER_TRL_TOFFSET_HI);
1187 if (timing_offset >= 32768)
1188 timing_offset -= 65536;
1189 trl_nomrate = (512 * stv0367_readbits(state,
1190 F367TER_TRL_NOMRATE_HI)
1191 + stv0367_readbits(state, F367TER_TRL_NOMRATE_LO) * 2
1192 + stv0367_readbits(state, F367TER_TRL_NOMRATE_LSB));
1194 timing_offset = ((signed)(1000000 / trl_nomrate) *
1195 timing_offset) / 2048;
1199 if (timing_offset <= 0) {
1200 timing_offset = (timing_offset - 11) / 22;
1203 timing_offset = (timing_offset + 11) / 22;
1207 for (counter = 0; counter < abs(timing_offset); counter++) {
1208 trl_nomrate += step;
1209 stv0367_writebits(state, F367TER_TRL_NOMRATE_LSB,
1211 stv0367_writebits(state, F367TER_TRL_NOMRATE_LO,
1213 usleep_range(1000, 2000);
1216 usleep_range(5000, 6000);
1217 /* unlocks could happen in case of trl centring big step,
1218 then a core off/on restarts demod */
1219 u_var = stv0367_readbits(state, F367TER_LK);
1222 stv0367_writebits(state, F367TER_CORE_ACTIVE, 0);
1224 stv0367_writebits(state, F367TER_CORE_ACTIVE, 1);
1230 static int stv0367ter_set_frontend(struct dvb_frontend *fe)
1232 struct dtv_frontend_properties *p = &fe->dtv_property_cache;
1233 struct stv0367_state *state = fe->demodulator_priv;
1234 struct stv0367ter_state *ter_state = state->ter_state;
1237 s8 num_trials, index;
1238 u8 SenseTrials[] = { INVERSION_ON, INVERSION_OFF };
1240 if (state->reinit_on_setfrontend)
1241 stv0367ter_init(fe);
1243 if (fe->ops.tuner_ops.set_params) {
1244 if (state->use_i2c_gatectrl && fe->ops.i2c_gate_ctrl)
1245 fe->ops.i2c_gate_ctrl(fe, 1);
1246 fe->ops.tuner_ops.set_params(fe);
1247 if (state->use_i2c_gatectrl && fe->ops.i2c_gate_ctrl)
1248 fe->ops.i2c_gate_ctrl(fe, 0);
1251 switch (p->transmission_mode) {
1253 case TRANSMISSION_MODE_AUTO:
1254 case TRANSMISSION_MODE_2K:
1255 ter_state->mode = FE_TER_MODE_2K;
1257 /* case TRANSMISSION_MODE_4K:
1258 pLook.mode = FE_TER_MODE_4K;
1260 case TRANSMISSION_MODE_8K:
1261 ter_state->mode = FE_TER_MODE_8K;
1265 switch (p->guard_interval) {
1267 case GUARD_INTERVAL_1_32:
1268 case GUARD_INTERVAL_1_16:
1269 case GUARD_INTERVAL_1_8:
1270 case GUARD_INTERVAL_1_4:
1271 ter_state->guard = p->guard_interval;
1273 case GUARD_INTERVAL_AUTO:
1274 ter_state->guard = GUARD_INTERVAL_1_32;
1278 switch (p->bandwidth_hz) {
1280 ter_state->bw = FE_TER_CHAN_BW_6M;
1283 ter_state->bw = FE_TER_CHAN_BW_7M;
1287 ter_state->bw = FE_TER_CHAN_BW_8M;
1290 ter_state->hierarchy = FE_TER_HIER_NONE;
1292 switch (p->inversion) {
1299 if (ter_state->first_lock)
1304 ter_state->state = FE_TER_NOLOCK;
1307 while (((index) < num_trials) && (ter_state->state != FE_TER_LOCKOK)) {
1308 if (!ter_state->first_lock) {
1309 if (p->inversion == INVERSION_AUTO)
1310 ter_state->sense = SenseTrials[index];
1313 stv0367ter_algo(fe);
1315 if ((ter_state->state == FE_TER_LOCKOK) &&
1316 (p->inversion == INVERSION_AUTO) &&
1318 /* invert spectrum sense */
1319 SenseTrials[index] = SenseTrials[0];
1320 SenseTrials[(index + 1) % 2] = (SenseTrials[1] + 1) % 2;
1329 static int stv0367ter_read_ucblocks(struct dvb_frontend *fe, u32 *ucblocks)
1331 struct stv0367_state *state = fe->demodulator_priv;
1332 struct stv0367ter_state *ter_state = state->ter_state;
1335 /*wait for counting completion*/
1336 if (stv0367_readbits(state, F367TER_SFERRC_OLDVALUE) == 0) {
1338 ((u32)stv0367_readbits(state, F367TER_ERR_CNT1)
1340 + ((u32)stv0367_readbits(state, F367TER_ERR_CNT1_HI)
1342 + ((u32)stv0367_readbits(state, F367TER_ERR_CNT1_LO));
1343 ter_state->ucblocks = errs;
1346 (*ucblocks) = ter_state->ucblocks;
1351 static int stv0367ter_get_frontend(struct dvb_frontend *fe,
1352 struct dtv_frontend_properties *p)
1354 struct stv0367_state *state = fe->demodulator_priv;
1355 struct stv0367ter_state *ter_state = state->ter_state;
1356 enum stv0367_ter_mode mode;
1357 int constell = 0,/* snr = 0,*/ Data = 0;
1359 p->frequency = stv0367_get_tuner_freq(fe);
1360 if ((int)p->frequency < 0)
1361 p->frequency = -p->frequency;
1363 constell = stv0367_readbits(state, F367TER_TPS_CONST);
1365 p->modulation = QPSK;
1366 else if (constell == 1)
1367 p->modulation = QAM_16;
1369 p->modulation = QAM_64;
1371 p->inversion = stv0367_readbits(state, F367TER_INV_SPECTR);
1373 /* Get the Hierarchical mode */
1374 Data = stv0367_readbits(state, F367TER_TPS_HIERMODE);
1378 p->hierarchy = HIERARCHY_NONE;
1381 p->hierarchy = HIERARCHY_1;
1384 p->hierarchy = HIERARCHY_2;
1387 p->hierarchy = HIERARCHY_4;
1390 p->hierarchy = HIERARCHY_AUTO;
1394 /* Get the FEC Rate */
1395 if (ter_state->hierarchy == FE_TER_HIER_LOW_PRIO)
1396 Data = stv0367_readbits(state, F367TER_TPS_LPCODE);
1398 Data = stv0367_readbits(state, F367TER_TPS_HPCODE);
1402 p->code_rate_HP = FEC_1_2;
1405 p->code_rate_HP = FEC_2_3;
1408 p->code_rate_HP = FEC_3_4;
1411 p->code_rate_HP = FEC_5_6;
1414 p->code_rate_HP = FEC_7_8;
1417 p->code_rate_HP = FEC_AUTO;
1421 mode = stv0367_readbits(state, F367TER_SYR_MODE);
1424 case FE_TER_MODE_2K:
1425 p->transmission_mode = TRANSMISSION_MODE_2K;
1427 /* case FE_TER_MODE_4K:
1428 p->transmission_mode = TRANSMISSION_MODE_4K;
1430 case FE_TER_MODE_8K:
1431 p->transmission_mode = TRANSMISSION_MODE_8K;
1434 p->transmission_mode = TRANSMISSION_MODE_AUTO;
1437 p->guard_interval = stv0367_readbits(state, F367TER_SYR_GUARD);
1442 static u32 stv0367ter_snr_readreg(struct dvb_frontend *fe)
1444 struct stv0367_state *state = fe->demodulator_priv;
1447 u8 cut = stv0367_readbits(state, F367TER_IDENTIFICATIONREG);
1450 usleep_range(2000, 3000);
1451 if (cut == 0x50) /*cut 1.0 cut 1.1*/
1452 snru32 += stv0367_readbits(state, F367TER_CHCSNR) / 4;
1454 snru32 += 125 * stv0367_readbits(state, F367TER_CHCSNR);
1458 snru32 /= 10;/*average on 10 values*/
1463 static int stv0367ter_read_snr(struct dvb_frontend *fe, u16 *snr)
1465 u32 snrval = stv0367ter_snr_readreg(fe);
1467 *snr = snrval / 1000;
1473 static int stv0367ter_status(struct dvb_frontend *fe)
1476 struct stv0367_state *state = fe->demodulator_priv;
1477 struct stv0367ter_state *ter_state = state->ter_state;
1480 locked = (stv0367_readbits(state, F367TER_LK));
1482 ter_state->unlock_counter += 1;
1484 ter_state->unlock_counter = 0;
1486 if (ter_state->unlock_counter > 2) {
1487 if (!stv0367_readbits(state, F367TER_TPS_LOCK) ||
1488 (!stv0367_readbits(state, F367TER_LK))) {
1489 stv0367_writebits(state, F367TER_CORE_ACTIVE, 0);
1490 usleep_range(2000, 3000);
1491 stv0367_writebits(state, F367TER_CORE_ACTIVE, 1);
1493 locked = (stv0367_readbits(state, F367TER_TPS_LOCK)) &&
1494 (stv0367_readbits(state, F367TER_LK));
1502 static int stv0367ter_read_status(struct dvb_frontend *fe,
1503 enum fe_status *status)
1505 struct stv0367_state *state = fe->demodulator_priv;
1507 dprintk("%s:\n", __func__);
1511 if (stv0367_readbits(state, F367TER_LK)) {
1512 *status = FE_HAS_SIGNAL | FE_HAS_CARRIER | FE_HAS_VITERBI
1513 | FE_HAS_SYNC | FE_HAS_LOCK;
1514 dprintk("%s: stv0367 has locked\n", __func__);
1520 static int stv0367ter_read_ber(struct dvb_frontend *fe, u32 *ber)
1522 struct stv0367_state *state = fe->demodulator_priv;
1523 struct stv0367ter_state *ter_state = state->ter_state;
1524 u32 Errors = 0, tber = 0, temporary = 0;
1525 int abc = 0, def = 0;
1528 /*wait for counting completion*/
1529 if (stv0367_readbits(state, F367TER_SFERRC_OLDVALUE) == 0)
1530 Errors = ((u32)stv0367_readbits(state, F367TER_SFEC_ERR_CNT)
1532 + ((u32)stv0367_readbits(state, F367TER_SFEC_ERR_CNT_HI)
1534 + ((u32)stv0367_readbits(state,
1535 F367TER_SFEC_ERR_CNT_LO));
1536 /*measurement not completed, load previous value*/
1538 tber = ter_state->pBER;
1542 abc = stv0367_readbits(state, F367TER_SFEC_ERR_SOURCE);
1543 def = stv0367_readbits(state, F367TER_SFEC_NUM_EVENT);
1547 } else if (abc == 0x7) {
1549 temporary = (Errors * 1000000000) / (8 * (1 << 14));
1550 temporary = temporary;
1551 } else if (Errors <= 42) {
1552 temporary = (Errors * 100000000) / (8 * (1 << 14));
1553 temporary = temporary * 10;
1554 } else if (Errors <= 429) {
1555 temporary = (Errors * 10000000) / (8 * (1 << 14));
1556 temporary = temporary * 100;
1557 } else if (Errors <= 4294) {
1558 temporary = (Errors * 1000000) / (8 * (1 << 14));
1559 temporary = temporary * 1000;
1560 } else if (Errors <= 42949) {
1561 temporary = (Errors * 100000) / (8 * (1 << 14));
1562 temporary = temporary * 10000;
1563 } else if (Errors <= 429496) {
1564 temporary = (Errors * 10000) / (8 * (1 << 14));
1565 temporary = temporary * 100000;
1566 } else { /*if (Errors<4294967) 2^22 max error*/
1567 temporary = (Errors * 1000) / (8 * (1 << 14));
1568 temporary = temporary * 100000; /* still to *10 */
1573 /*tber=Errors/(8*(1 <<14));*/
1576 /*tber=Errors/(8*(1 <<16));*/
1577 tber = temporary / 4;
1579 /*tber=Errors/(8*(1 <<18));*/
1580 tber = temporary / 16;
1582 /*tber=Errors/(8*(1 <<20));*/
1583 tber = temporary / 64;
1585 /*tber=Errors/(8*(1 <<22));*/
1586 tber = temporary / 256;
1588 /* should not pass here*/
1591 if ((Errors < 4294967) && (Errors > 429496))
1596 /* save actual value */
1597 ter_state->pBER = tber;
1604 static u32 stv0367ter_get_per(struct stv0367_state *state)
1606 struct stv0367ter_state *ter_state = state->ter_state;
1607 u32 Errors = 0, Per = 0, temporary = 0;
1608 int abc = 0, def = 0, cpt = 0;
1610 while (((stv0367_readbits(state, F367TER_SFERRC_OLDVALUE) == 1) &&
1611 (cpt < 400)) || ((Errors == 0) && (cpt < 400))) {
1612 usleep_range(1000, 2000);
1613 Errors = ((u32)stv0367_readbits(state, F367TER_ERR_CNT1)
1615 + ((u32)stv0367_readbits(state, F367TER_ERR_CNT1_HI)
1617 + ((u32)stv0367_readbits(state, F367TER_ERR_CNT1_LO));
1620 abc = stv0367_readbits(state, F367TER_ERR_SRC1);
1621 def = stv0367_readbits(state, F367TER_NUM_EVT1);
1625 else if (abc == 0x9) {
1627 temporary = (Errors * 1000000000) / (8 * (1 << 8));
1628 temporary = temporary;
1629 } else if (Errors <= 42) {
1630 temporary = (Errors * 100000000) / (8 * (1 << 8));
1631 temporary = temporary * 10;
1632 } else if (Errors <= 429) {
1633 temporary = (Errors * 10000000) / (8 * (1 << 8));
1634 temporary = temporary * 100;
1635 } else if (Errors <= 4294) {
1636 temporary = (Errors * 1000000) / (8 * (1 << 8));
1637 temporary = temporary * 1000;
1638 } else if (Errors <= 42949) {
1639 temporary = (Errors * 100000) / (8 * (1 << 8));
1640 temporary = temporary * 10000;
1641 } else { /*if(Errors<=429496) 2^16 errors max*/
1642 temporary = (Errors * 10000) / (8 * (1 << 8));
1643 temporary = temporary * 100000;
1648 /*Per=Errors/(1 << 8);*/
1651 /*Per=Errors/(1 << 10);*/
1652 Per = temporary / 4;
1654 /*Per=Errors/(1 << 12);*/
1655 Per = temporary / 16;
1657 /*Per=Errors/(1 << 14);*/
1658 Per = temporary / 64;
1660 /*Per=Errors/(1 << 16);*/
1661 Per = temporary / 256;
1666 /* save actual value */
1667 ter_state->pPER = Per;
1672 static int stv0367_get_tune_settings(struct dvb_frontend *fe,
1673 struct dvb_frontend_tune_settings
1676 fe_tune_settings->min_delay_ms = 1000;
1677 fe_tune_settings->step_size = 0;
1678 fe_tune_settings->max_drift = 0;
1683 static void stv0367_release(struct dvb_frontend *fe)
1685 struct stv0367_state *state = fe->demodulator_priv;
1687 kfree(state->ter_state);
1688 kfree(state->cab_state);
1692 static const struct dvb_frontend_ops stv0367ter_ops = {
1693 .delsys = { SYS_DVBT },
1695 .name = "ST STV0367 DVB-T",
1696 .frequency_min = 47000000,
1697 .frequency_max = 862000000,
1698 .frequency_stepsize = 15625,
1699 .frequency_tolerance = 0,
1700 .caps = FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 |
1701 FE_CAN_FEC_3_4 | FE_CAN_FEC_5_6 | FE_CAN_FEC_7_8 |
1703 FE_CAN_QPSK | FE_CAN_QAM_16 | FE_CAN_QAM_64 |
1704 FE_CAN_QAM_128 | FE_CAN_QAM_256 | FE_CAN_QAM_AUTO |
1705 FE_CAN_TRANSMISSION_MODE_AUTO | FE_CAN_RECOVER |
1706 FE_CAN_INVERSION_AUTO |
1709 .release = stv0367_release,
1710 .init = stv0367ter_init,
1711 .sleep = stv0367ter_sleep,
1712 .i2c_gate_ctrl = stv0367ter_gate_ctrl,
1713 .set_frontend = stv0367ter_set_frontend,
1714 .get_frontend = stv0367ter_get_frontend,
1715 .get_tune_settings = stv0367_get_tune_settings,
1716 .read_status = stv0367ter_read_status,
1717 .read_ber = stv0367ter_read_ber,/* too slow */
1718 /* .read_signal_strength = stv0367_read_signal_strength,*/
1719 .read_snr = stv0367ter_read_snr,
1720 .read_ucblocks = stv0367ter_read_ucblocks,
1723 struct dvb_frontend *stv0367ter_attach(const struct stv0367_config *config,
1724 struct i2c_adapter *i2c)
1726 struct stv0367_state *state = NULL;
1727 struct stv0367ter_state *ter_state = NULL;
1729 /* allocate memory for the internal state */
1730 state = kzalloc(sizeof(struct stv0367_state), GFP_KERNEL);
1733 ter_state = kzalloc(sizeof(struct stv0367ter_state), GFP_KERNEL);
1734 if (ter_state == NULL)
1737 /* setup the state */
1739 state->config = config;
1740 state->ter_state = ter_state;
1741 state->fe.ops = stv0367ter_ops;
1742 state->fe.demodulator_priv = state;
1743 state->chip_id = stv0367_readreg(state, 0xf000);
1745 /* demod operation options */
1746 state->use_i2c_gatectrl = 1;
1747 state->deftabs = STV0367_DEFTAB_GENERIC;
1748 state->reinit_on_setfrontend = 1;
1749 state->auto_if_khz = 0;
1751 dprintk("%s: chip_id = 0x%x\n", __func__, state->chip_id);
1753 /* check if the demod is there */
1754 if ((state->chip_id != 0x50) && (state->chip_id != 0x60))
1764 EXPORT_SYMBOL(stv0367ter_attach);
1766 static int stv0367cab_gate_ctrl(struct dvb_frontend *fe, int enable)
1768 struct stv0367_state *state = fe->demodulator_priv;
1770 dprintk("%s:\n", __func__);
1772 stv0367_writebits(state, F367CAB_I2CT_ON, (enable > 0) ? 1 : 0);
1777 static u32 stv0367cab_get_mclk(struct dvb_frontend *fe, u32 ExtClk_Hz)
1779 struct stv0367_state *state = fe->demodulator_priv;
1780 u32 mclk_Hz = 0;/* master clock frequency (Hz) */
1784 if (stv0367_readbits(state, F367CAB_BYPASS_PLLXN) == 0) {
1785 N = (u32)stv0367_readbits(state, F367CAB_PLL_NDIV);
1789 M = (u32)stv0367_readbits(state, F367CAB_PLL_MDIV);
1793 P = (u32)stv0367_readbits(state, F367CAB_PLL_PDIV);
1798 mclk_Hz = ((ExtClk_Hz / 2) * N) / (M * (1 << P));
1799 dprintk("stv0367cab_get_mclk BYPASS_PLLXN mclk_Hz=%d\n",
1802 mclk_Hz = ExtClk_Hz;
1804 dprintk("stv0367cab_get_mclk final mclk_Hz=%d\n", mclk_Hz);
1809 static u32 stv0367cab_get_adc_freq(struct dvb_frontend *fe, u32 ExtClk_Hz)
1811 u32 ADCClk_Hz = ExtClk_Hz;
1813 ADCClk_Hz = stv0367cab_get_mclk(fe, ExtClk_Hz);
1818 static enum stv0367cab_mod stv0367cab_SetQamSize(struct stv0367_state *state,
1820 enum stv0367cab_mod QAMSize)
1823 stv0367_writebits(state, F367CAB_QAM_MODE, QAMSize);
1825 /* Set Registers settings specific to the QAM size */
1827 case FE_CAB_MOD_QAM4:
1828 stv0367_writereg(state, R367CAB_IQDEM_ADJ_AGC_REF, 0x00);
1830 case FE_CAB_MOD_QAM16:
1831 stv0367_writereg(state, R367CAB_AGC_PWR_REF_L, 0x64);
1832 stv0367_writereg(state, R367CAB_IQDEM_ADJ_AGC_REF, 0x00);
1833 stv0367_writereg(state, R367CAB_FSM_STATE, 0x90);
1834 stv0367_writereg(state, R367CAB_EQU_CTR_LPF_GAIN, 0xc1);
1835 stv0367_writereg(state, R367CAB_EQU_CRL_LPF_GAIN, 0xa7);
1836 stv0367_writereg(state, R367CAB_EQU_CRL_LD_SEN, 0x95);
1837 stv0367_writereg(state, R367CAB_EQU_CRL_LIMITER, 0x40);
1838 stv0367_writereg(state, R367CAB_EQU_PNT_GAIN, 0x8a);
1840 case FE_CAB_MOD_QAM32:
1841 stv0367_writereg(state, R367CAB_IQDEM_ADJ_AGC_REF, 0x00);
1842 stv0367_writereg(state, R367CAB_AGC_PWR_REF_L, 0x6e);
1843 stv0367_writereg(state, R367CAB_FSM_STATE, 0xb0);
1844 stv0367_writereg(state, R367CAB_EQU_CTR_LPF_GAIN, 0xc1);
1845 stv0367_writereg(state, R367CAB_EQU_CRL_LPF_GAIN, 0xb7);
1846 stv0367_writereg(state, R367CAB_EQU_CRL_LD_SEN, 0x9d);
1847 stv0367_writereg(state, R367CAB_EQU_CRL_LIMITER, 0x7f);
1848 stv0367_writereg(state, R367CAB_EQU_PNT_GAIN, 0xa7);
1850 case FE_CAB_MOD_QAM64:
1851 stv0367_writereg(state, R367CAB_IQDEM_ADJ_AGC_REF, 0x82);
1852 stv0367_writereg(state, R367CAB_AGC_PWR_REF_L, 0x5a);
1853 if (SymbolRate > 4500000) {
1854 stv0367_writereg(state, R367CAB_FSM_STATE, 0xb0);
1855 stv0367_writereg(state, R367CAB_EQU_CTR_LPF_GAIN, 0xc1);
1856 stv0367_writereg(state, R367CAB_EQU_CRL_LPF_GAIN, 0xa5);
1857 } else if (SymbolRate > 2500000) {
1858 stv0367_writereg(state, R367CAB_FSM_STATE, 0xa0);
1859 stv0367_writereg(state, R367CAB_EQU_CTR_LPF_GAIN, 0xc1);
1860 stv0367_writereg(state, R367CAB_EQU_CRL_LPF_GAIN, 0xa6);
1862 stv0367_writereg(state, R367CAB_FSM_STATE, 0xa0);
1863 stv0367_writereg(state, R367CAB_EQU_CTR_LPF_GAIN, 0xd1);
1864 stv0367_writereg(state, R367CAB_EQU_CRL_LPF_GAIN, 0xa7);
1866 stv0367_writereg(state, R367CAB_EQU_CRL_LD_SEN, 0x95);
1867 stv0367_writereg(state, R367CAB_EQU_CRL_LIMITER, 0x40);
1868 stv0367_writereg(state, R367CAB_EQU_PNT_GAIN, 0x99);
1870 case FE_CAB_MOD_QAM128:
1871 stv0367_writereg(state, R367CAB_IQDEM_ADJ_AGC_REF, 0x00);
1872 stv0367_writereg(state, R367CAB_AGC_PWR_REF_L, 0x76);
1873 stv0367_writereg(state, R367CAB_FSM_STATE, 0x90);
1874 stv0367_writereg(state, R367CAB_EQU_CTR_LPF_GAIN, 0xb1);
1875 if (SymbolRate > 4500000)
1876 stv0367_writereg(state, R367CAB_EQU_CRL_LPF_GAIN, 0xa7);
1877 else if (SymbolRate > 2500000)
1878 stv0367_writereg(state, R367CAB_EQU_CRL_LPF_GAIN, 0xa6);
1880 stv0367_writereg(state, R367CAB_EQU_CRL_LPF_GAIN, 0x97);
1882 stv0367_writereg(state, R367CAB_EQU_CRL_LD_SEN, 0x8e);
1883 stv0367_writereg(state, R367CAB_EQU_CRL_LIMITER, 0x7f);
1884 stv0367_writereg(state, R367CAB_EQU_PNT_GAIN, 0xa7);
1886 case FE_CAB_MOD_QAM256:
1887 stv0367_writereg(state, R367CAB_IQDEM_ADJ_AGC_REF, 0x94);
1888 stv0367_writereg(state, R367CAB_AGC_PWR_REF_L, 0x5a);
1889 stv0367_writereg(state, R367CAB_FSM_STATE, 0xa0);
1890 if (SymbolRate > 4500000)
1891 stv0367_writereg(state, R367CAB_EQU_CTR_LPF_GAIN, 0xc1);
1892 else if (SymbolRate > 2500000)
1893 stv0367_writereg(state, R367CAB_EQU_CTR_LPF_GAIN, 0xc1);
1895 stv0367_writereg(state, R367CAB_EQU_CTR_LPF_GAIN, 0xd1);
1897 stv0367_writereg(state, R367CAB_EQU_CRL_LPF_GAIN, 0xa7);
1898 stv0367_writereg(state, R367CAB_EQU_CRL_LD_SEN, 0x85);
1899 stv0367_writereg(state, R367CAB_EQU_CRL_LIMITER, 0x40);
1900 stv0367_writereg(state, R367CAB_EQU_PNT_GAIN, 0xa7);
1902 case FE_CAB_MOD_QAM512:
1903 stv0367_writereg(state, R367CAB_IQDEM_ADJ_AGC_REF, 0x00);
1905 case FE_CAB_MOD_QAM1024:
1906 stv0367_writereg(state, R367CAB_IQDEM_ADJ_AGC_REF, 0x00);
1915 static u32 stv0367cab_set_derot_freq(struct stv0367_state *state,
1916 u32 adc_hz, s32 derot_hz)
1921 adc_khz = adc_hz / 1000;
1923 dprintk("%s: adc_hz=%d derot_hz=%d\n", __func__, adc_hz, derot_hz);
1926 if (derot_hz < 1000000)
1927 derot_hz = adc_hz / 4; /* ZIF operation */
1928 if (derot_hz > adc_hz)
1929 derot_hz = derot_hz - adc_hz;
1930 sampled_if = (u32)derot_hz / 1000;
1931 sampled_if *= 32768;
1932 sampled_if /= adc_khz;
1936 if (sampled_if > 8388607)
1937 sampled_if = 8388607;
1939 dprintk("%s: sampled_if=0x%x\n", __func__, sampled_if);
1941 stv0367_writereg(state, R367CAB_MIX_NCO_LL, sampled_if);
1942 stv0367_writereg(state, R367CAB_MIX_NCO_HL, (sampled_if >> 8));
1943 stv0367_writebits(state, F367CAB_MIX_NCO_INC_HH, (sampled_if >> 16));
1948 static u32 stv0367cab_get_derot_freq(struct stv0367_state *state, u32 adc_hz)
1952 sampled_if = stv0367_readbits(state, F367CAB_MIX_NCO_INC_LL) +
1953 (stv0367_readbits(state, F367CAB_MIX_NCO_INC_HL) << 8) +
1954 (stv0367_readbits(state, F367CAB_MIX_NCO_INC_HH) << 16);
1957 sampled_if *= (adc_hz / 1000);
1959 sampled_if /= 32768;
1964 static u32 stv0367cab_set_srate(struct stv0367_state *state, u32 adc_hz,
1965 u32 mclk_hz, u32 SymbolRate,
1966 enum stv0367cab_mod QAMSize)
1968 u32 QamSizeCorr = 0;
1969 u32 u32_tmp = 0, u32_tmp1 = 0;
1972 dprintk("%s:\n", __func__);
1974 /* Set Correction factor of SRC gain */
1976 case FE_CAB_MOD_QAM4:
1979 case FE_CAB_MOD_QAM16:
1982 case FE_CAB_MOD_QAM32:
1985 case FE_CAB_MOD_QAM64:
1988 case FE_CAB_MOD_QAM128:
1991 case FE_CAB_MOD_QAM256:
1994 case FE_CAB_MOD_QAM512:
1997 case FE_CAB_MOD_QAM1024:
2004 /* Transfer ratio calculation */
2006 u32_tmp = 256 * SymbolRate;
2007 u32_tmp = u32_tmp / adc_hz;
2009 stv0367_writereg(state, R367CAB_EQU_CRL_TFR, (u8)u32_tmp);
2011 /* Symbol rate and SRC gain calculation */
2012 adp_khz = (mclk_hz >> 1) / 1000;/* TRL works at half the system clock */
2014 u32_tmp = SymbolRate;
2015 u32_tmp1 = SymbolRate;
2017 if (u32_tmp < 2097152) { /* 2097152 = 2^21 */
2018 /* Symbol rate calculation */
2019 u32_tmp *= 2048; /* 2048 = 2^11 */
2020 u32_tmp = u32_tmp / adp_khz;
2021 u32_tmp = u32_tmp * 16384; /* 16384 = 2^14 */
2022 u32_tmp /= 125 ; /* 125 = 1000/2^3 */
2023 u32_tmp = u32_tmp * 8; /* 8 = 2^3 */
2025 /* SRC Gain Calculation */
2026 u32_tmp1 *= 2048; /* *2*2^10 */
2027 u32_tmp1 /= 439; /* *2/878 */
2028 u32_tmp1 *= 256; /* *2^8 */
2029 u32_tmp1 = u32_tmp1 / adp_khz; /* /(AdpClk in kHz) */
2030 u32_tmp1 *= QamSizeCorr * 9; /* *1000*corr factor */
2031 u32_tmp1 = u32_tmp1 / 10000000;
2033 } else if (u32_tmp < 4194304) { /* 4194304 = 2**22 */
2034 /* Symbol rate calculation */
2035 u32_tmp *= 1024 ; /* 1024 = 2**10 */
2036 u32_tmp = u32_tmp / adp_khz;
2037 u32_tmp = u32_tmp * 16384; /* 16384 = 2**14 */
2038 u32_tmp /= 125 ; /* 125 = 1000/2**3 */
2039 u32_tmp = u32_tmp * 16; /* 16 = 2**4 */
2041 /* SRC Gain Calculation */
2042 u32_tmp1 *= 1024; /* *2*2^9 */
2043 u32_tmp1 /= 439; /* *2/878 */
2044 u32_tmp1 *= 256; /* *2^8 */
2045 u32_tmp1 = u32_tmp1 / adp_khz; /* /(AdpClk in kHz)*/
2046 u32_tmp1 *= QamSizeCorr * 9; /* *1000*corr factor */
2047 u32_tmp1 = u32_tmp1 / 5000000;
2048 } else if (u32_tmp < 8388607) { /* 8388607 = 2**23 */
2049 /* Symbol rate calculation */
2050 u32_tmp *= 512 ; /* 512 = 2**9 */
2051 u32_tmp = u32_tmp / adp_khz;
2052 u32_tmp = u32_tmp * 16384; /* 16384 = 2**14 */
2053 u32_tmp /= 125 ; /* 125 = 1000/2**3 */
2054 u32_tmp = u32_tmp * 32; /* 32 = 2**5 */
2056 /* SRC Gain Calculation */
2057 u32_tmp1 *= 512; /* *2*2^8 */
2058 u32_tmp1 /= 439; /* *2/878 */
2059 u32_tmp1 *= 256; /* *2^8 */
2060 u32_tmp1 = u32_tmp1 / adp_khz; /* /(AdpClk in kHz) */
2061 u32_tmp1 *= QamSizeCorr * 9; /* *1000*corr factor */
2062 u32_tmp1 = u32_tmp1 / 2500000;
2064 /* Symbol rate calculation */
2065 u32_tmp *= 256 ; /* 256 = 2**8 */
2066 u32_tmp = u32_tmp / adp_khz;
2067 u32_tmp = u32_tmp * 16384; /* 16384 = 2**13 */
2068 u32_tmp /= 125 ; /* 125 = 1000/2**3 */
2069 u32_tmp = u32_tmp * 64; /* 64 = 2**6 */
2071 /* SRC Gain Calculation */
2072 u32_tmp1 *= 256; /* 2*2^7 */
2073 u32_tmp1 /= 439; /* *2/878 */
2074 u32_tmp1 *= 256; /* *2^8 */
2075 u32_tmp1 = u32_tmp1 / adp_khz; /* /(AdpClk in kHz) */
2076 u32_tmp1 *= QamSizeCorr * 9; /* *1000*corr factor */
2077 u32_tmp1 = u32_tmp1 / 1250000;
2081 /* Filters' coefficients are calculated and written
2082 into registers only if the filters are enabled */
2083 if (stv0367_readbits(state, F367CAB_ADJ_EN)) {
2084 stv0367cab_SetIirAdjacentcoefficient(state, mclk_hz,
2086 /* AllPass filter must be enabled
2087 when the adjacents filter is used */
2088 stv0367_writebits(state, F367CAB_ALLPASSFILT_EN, 1);
2089 stv0367cab_SetAllPasscoefficient(state, mclk_hz, SymbolRate);
2091 /* AllPass filter must be disabled
2092 when the adjacents filter is not used */
2094 stv0367_writebits(state, F367CAB_ALLPASSFILT_EN, 0);
2096 stv0367_writereg(state, R367CAB_SRC_NCO_LL, u32_tmp);
2097 stv0367_writereg(state, R367CAB_SRC_NCO_LH, (u32_tmp >> 8));
2098 stv0367_writereg(state, R367CAB_SRC_NCO_HL, (u32_tmp >> 16));
2099 stv0367_writereg(state, R367CAB_SRC_NCO_HH, (u32_tmp >> 24));
2101 stv0367_writereg(state, R367CAB_IQDEM_GAIN_SRC_L, u32_tmp1 & 0x00ff);
2102 stv0367_writebits(state, F367CAB_GAIN_SRC_HI, (u32_tmp1 >> 8) & 0x00ff);
2107 static u32 stv0367cab_GetSymbolRate(struct stv0367_state *state, u32 mclk_hz)
2112 regsym = stv0367_readreg(state, R367CAB_SRC_NCO_LL) +
2113 (stv0367_readreg(state, R367CAB_SRC_NCO_LH) << 8) +
2114 (stv0367_readreg(state, R367CAB_SRC_NCO_HL) << 16) +
2115 (stv0367_readreg(state, R367CAB_SRC_NCO_HH) << 24);
2117 adp_khz = (mclk_hz >> 1) / 1000;/* TRL works at half the system clock */
2119 if (regsym < 134217728) { /* 134217728L = 2**27*/
2120 regsym = regsym * 32; /* 32 = 2**5 */
2121 regsym = regsym / 32768; /* 32768L = 2**15 */
2122 regsym = adp_khz * regsym; /* AdpClk in kHz */
2123 regsym = regsym / 128; /* 128 = 2**7 */
2124 regsym *= 125 ; /* 125 = 1000/2**3 */
2125 regsym /= 2048 ; /* 2048 = 2**11 */
2126 } else if (regsym < 268435456) { /* 268435456L = 2**28 */
2127 regsym = regsym * 16; /* 16 = 2**4 */
2128 regsym = regsym / 32768; /* 32768L = 2**15 */
2129 regsym = adp_khz * regsym; /* AdpClk in kHz */
2130 regsym = regsym / 128; /* 128 = 2**7 */
2131 regsym *= 125 ; /* 125 = 1000/2**3*/
2132 regsym /= 1024 ; /* 256 = 2**10*/
2133 } else if (regsym < 536870912) { /* 536870912L = 2**29*/
2134 regsym = regsym * 8; /* 8 = 2**3 */
2135 regsym = regsym / 32768; /* 32768L = 2**15 */
2136 regsym = adp_khz * regsym; /* AdpClk in kHz */
2137 regsym = regsym / 128; /* 128 = 2**7 */
2138 regsym *= 125 ; /* 125 = 1000/2**3 */
2139 regsym /= 512 ; /* 128 = 2**9 */
2141 regsym = regsym * 4; /* 4 = 2**2 */
2142 regsym = regsym / 32768; /* 32768L = 2**15 */
2143 regsym = adp_khz * regsym; /* AdpClk in kHz */
2144 regsym = regsym / 128; /* 128 = 2**7 */
2145 regsym *= 125 ; /* 125 = 1000/2**3 */
2146 regsym /= 256 ; /* 64 = 2**8 */
2152 static int stv0367cab_read_status(struct dvb_frontend *fe,
2153 enum fe_status *status)
2155 struct stv0367_state *state = fe->demodulator_priv;
2157 dprintk("%s:\n", __func__);
2161 if (state->cab_state->state > FE_CAB_NOSIGNAL)
2162 *status |= FE_HAS_SIGNAL;
2164 if (state->cab_state->state > FE_CAB_NOCARRIER)
2165 *status |= FE_HAS_CARRIER;
2167 if (state->cab_state->state >= FE_CAB_DEMODOK)
2168 *status |= FE_HAS_VITERBI;
2170 if (state->cab_state->state >= FE_CAB_DATAOK)
2171 *status |= FE_HAS_SYNC;
2173 if (stv0367_readbits(state, (state->cab_state->qamfec_status_reg ?
2174 state->cab_state->qamfec_status_reg : F367CAB_QAMFEC_LOCK))) {
2175 *status |= FE_HAS_LOCK;
2176 dprintk("%s: stv0367 has locked\n", __func__);
2182 static int stv0367cab_standby(struct dvb_frontend *fe, u8 standby_on)
2184 struct stv0367_state *state = fe->demodulator_priv;
2186 dprintk("%s:\n", __func__);
2189 stv0367_writebits(state, F367CAB_BYPASS_PLLXN, 0x03);
2190 stv0367_writebits(state, F367CAB_STDBY_PLLXN, 0x01);
2191 stv0367_writebits(state, F367CAB_STDBY, 1);
2192 stv0367_writebits(state, F367CAB_STDBY_CORE, 1);
2193 stv0367_writebits(state, F367CAB_EN_BUFFER_I, 0);
2194 stv0367_writebits(state, F367CAB_EN_BUFFER_Q, 0);
2195 stv0367_writebits(state, F367CAB_POFFQ, 1);
2196 stv0367_writebits(state, F367CAB_POFFI, 1);
2198 stv0367_writebits(state, F367CAB_STDBY_PLLXN, 0x00);
2199 stv0367_writebits(state, F367CAB_BYPASS_PLLXN, 0x00);
2200 stv0367_writebits(state, F367CAB_STDBY, 0);
2201 stv0367_writebits(state, F367CAB_STDBY_CORE, 0);
2202 stv0367_writebits(state, F367CAB_EN_BUFFER_I, 1);
2203 stv0367_writebits(state, F367CAB_EN_BUFFER_Q, 1);
2204 stv0367_writebits(state, F367CAB_POFFQ, 0);
2205 stv0367_writebits(state, F367CAB_POFFI, 0);
2211 static int stv0367cab_sleep(struct dvb_frontend *fe)
2213 return stv0367cab_standby(fe, 1);
2216 static int stv0367cab_init(struct dvb_frontend *fe)
2218 struct stv0367_state *state = fe->demodulator_priv;
2219 struct stv0367cab_state *cab_state = state->cab_state;
2221 dprintk("%s:\n", __func__);
2223 stv0367_write_table(state,
2224 stv0367_deftabs[state->deftabs][STV0367_TAB_CAB]);
2226 switch (state->config->ts_mode) {
2227 case STV0367_DVBCI_CLOCK:
2228 dprintk("Setting TSMode = STV0367_DVBCI_CLOCK\n");
2229 stv0367_writebits(state, F367CAB_OUTFORMAT, 0x03);
2231 case STV0367_SERIAL_PUNCT_CLOCK:
2232 case STV0367_SERIAL_CONT_CLOCK:
2233 stv0367_writebits(state, F367CAB_OUTFORMAT, 0x01);
2235 case STV0367_PARALLEL_PUNCT_CLOCK:
2236 case STV0367_OUTPUTMODE_DEFAULT:
2237 stv0367_writebits(state, F367CAB_OUTFORMAT, 0x00);
2241 switch (state->config->clk_pol) {
2242 case STV0367_RISINGEDGE_CLOCK:
2243 stv0367_writebits(state, F367CAB_CLK_POLARITY, 0x00);
2245 case STV0367_FALLINGEDGE_CLOCK:
2246 case STV0367_CLOCKPOLARITY_DEFAULT:
2247 stv0367_writebits(state, F367CAB_CLK_POLARITY, 0x01);
2251 stv0367_writebits(state, F367CAB_SYNC_STRIP, 0x00);
2253 stv0367_writebits(state, F367CAB_CT_NBST, 0x01);
2255 stv0367_writebits(state, F367CAB_TS_SWAP, 0x01);
2257 stv0367_writebits(state, F367CAB_FIFO_BYPASS, 0x00);
2259 stv0367_writereg(state, R367CAB_ANACTRL, 0x00);/*PLL enabled and used */
2261 cab_state->mclk = stv0367cab_get_mclk(fe, state->config->xtal);
2262 cab_state->adc_clk = stv0367cab_get_adc_freq(fe, state->config->xtal);
2267 enum stv0367_cab_signal_type stv0367cab_algo(struct stv0367_state *state,
2268 struct dtv_frontend_properties *p)
2270 struct stv0367cab_state *cab_state = state->cab_state;
2271 enum stv0367_cab_signal_type signalType = FE_CAB_NOAGC;
2272 u32 QAMFEC_Lock, QAM_Lock, u32_tmp, ifkhz,
2273 LockTime, TRLTimeOut, AGCTimeOut, CRLSymbols,
2274 CRLTimeOut, EQLTimeOut, DemodTimeOut, FECTimeOut;
2278 dprintk("%s:\n", __func__);
2280 stv0367_get_if_khz(state, &ifkhz);
2282 /* Timeouts calculation */
2283 /* A max lock time of 25 ms is allowed for delayed AGC */
2285 /* 100000 symbols needed by the TRL as a maximum value */
2286 TRLTimeOut = 100000000 / p->symbol_rate;
2287 /* CRLSymbols is the needed number of symbols to achieve a lock
2288 within [-4%, +4%] of the symbol rate.
2289 CRL timeout is calculated
2290 for a lock within [-search_range, +search_range].
2291 EQL timeout can be changed depending on
2292 the micro-reflections we want to handle.
2293 A characterization must be performed
2294 with these echoes to get new timeout values.
2296 switch (p->modulation) {
2298 CRLSymbols = 150000;
2302 CRLSymbols = 250000;
2306 CRLSymbols = 200000;
2310 CRLSymbols = 250000;
2314 CRLSymbols = 250000;
2318 CRLSymbols = 200000;
2323 if (pIntParams->search_range < 0) {
2324 CRLTimeOut = (25 * CRLSymbols *
2325 (-pIntParams->search_range / 1000)) /
2326 (pIntParams->symbol_rate / 1000);
2329 CRLTimeOut = (25 * CRLSymbols * (cab_state->search_range / 1000)) /
2330 (p->symbol_rate / 1000);
2332 CRLTimeOut = (1000 * CRLTimeOut) / p->symbol_rate;
2333 /* Timeouts below 50ms are coerced */
2334 if (CRLTimeOut < 50)
2336 /* A maximum of 100 TS packets is needed to get FEC lock even in case
2337 the spectrum inversion needs to be changed.
2338 This is equal to 20 ms in case of the lowest symbol rate of 0.87Msps
2341 DemodTimeOut = AGCTimeOut + TRLTimeOut + CRLTimeOut + EQLTimeOut;
2343 dprintk("%s: DemodTimeOut=%d\n", __func__, DemodTimeOut);
2345 /* Reset the TRL to ensure nothing starts until the
2346 AGC is stable which ensures a better lock time
2348 stv0367_writereg(state, R367CAB_CTRL_1, 0x04);
2349 /* Set AGC accumulation time to minimum and lock threshold to maximum
2350 in order to speed up the AGC lock */
2351 TrackAGCAccum = stv0367_readbits(state, F367CAB_AGC_ACCUMRSTSEL);
2352 stv0367_writebits(state, F367CAB_AGC_ACCUMRSTSEL, 0x0);
2353 /* Modulus Mapper is disabled */
2354 stv0367_writebits(state, F367CAB_MODULUSMAP_EN, 0);
2355 /* Disable the sweep function */
2356 stv0367_writebits(state, F367CAB_SWEEP_EN, 0);
2357 /* The sweep function is never used, Sweep rate must be set to 0 */
2358 /* Set the derotator frequency in Hz */
2359 stv0367cab_set_derot_freq(state, cab_state->adc_clk,
2360 (1000 * (s32)ifkhz + cab_state->derot_offset));
2361 /* Disable the Allpass Filter when the symbol rate is out of range */
2362 if ((p->symbol_rate > 10800000) | (p->symbol_rate < 1800000)) {
2363 stv0367_writebits(state, F367CAB_ADJ_EN, 0);
2364 stv0367_writebits(state, F367CAB_ALLPASSFILT_EN, 0);
2367 /* Check if the tuner is locked */
2368 tuner_lock = stv0367cab_tuner_get_status(fe);
2369 if (tuner_lock == 0)
2370 return FE_367CAB_NOTUNER;
2372 /* Release the TRL to start demodulator acquisition */
2373 /* Wait for QAM lock */
2375 stv0367_writereg(state, R367CAB_CTRL_1, 0x00);
2377 QAM_Lock = stv0367_readbits(state, F367CAB_FSM_STATUS);
2378 if ((LockTime >= (DemodTimeOut - EQLTimeOut)) &&
2381 * We don't wait longer, the frequency/phase offset
2384 LockTime = DemodTimeOut;
2385 else if ((LockTime >= (AGCTimeOut + TRLTimeOut)) &&
2388 * We don't wait longer, either there is no signal or
2389 * it is not the right symbol rate or it is an analog
2393 LockTime = DemodTimeOut;
2394 u32_tmp = stv0367_readbits(state,
2395 F367CAB_AGC_PWR_WORD_LO) +
2396 (stv0367_readbits(state,
2397 F367CAB_AGC_PWR_WORD_ME) << 8) +
2398 (stv0367_readbits(state,
2399 F367CAB_AGC_PWR_WORD_HI) << 16);
2400 if (u32_tmp >= 131072)
2401 u32_tmp = 262144 - u32_tmp;
2402 u32_tmp = u32_tmp / (1 << (11 - stv0367_readbits(state,
2403 F367CAB_AGC_IF_BWSEL)));
2405 if (u32_tmp < stv0367_readbits(state,
2406 F367CAB_AGC_PWRREF_LO) +
2407 256 * stv0367_readbits(state,
2408 F367CAB_AGC_PWRREF_HI) - 10)
2411 usleep_range(10000, 20000);
2414 dprintk("QAM_Lock=0x%x LockTime=%d\n", QAM_Lock, LockTime);
2415 tmp = stv0367_readreg(state, R367CAB_IT_STATUS1);
2417 dprintk("R367CAB_IT_STATUS1=0x%x\n", tmp);
2419 } while (((QAM_Lock != 0x0c) && (QAM_Lock != 0x0b)) &&
2420 (LockTime < DemodTimeOut));
2422 dprintk("QAM_Lock=0x%x\n", QAM_Lock);
2424 tmp = stv0367_readreg(state, R367CAB_IT_STATUS1);
2425 dprintk("R367CAB_IT_STATUS1=0x%x\n", tmp);
2426 tmp = stv0367_readreg(state, R367CAB_IT_STATUS2);
2427 dprintk("R367CAB_IT_STATUS2=0x%x\n", tmp);
2429 tmp = stv0367cab_get_derot_freq(state, cab_state->adc_clk);
2430 dprintk("stv0367cab_get_derot_freq=0x%x\n", tmp);
2432 if ((QAM_Lock == 0x0c) || (QAM_Lock == 0x0b)) {
2433 /* Wait for FEC lock */
2436 usleep_range(5000, 7000);
2438 QAMFEC_Lock = stv0367_readbits(state,
2439 (state->cab_state->qamfec_status_reg ?
2440 state->cab_state->qamfec_status_reg :
2441 F367CAB_QAMFEC_LOCK));
2442 } while (!QAMFEC_Lock && (LockTime < FECTimeOut));
2447 signalType = FE_CAB_DATAOK;
2448 cab_state->spect_inv = stv0367_readbits(state,
2451 /* not clear for me */
2453 if (ifkhz > cab_state->adc_clk / 1000) {
2454 cab_state->freq_khz =
2455 FE_Cab_TunerGetFrequency(pIntParams->hTuner)
2456 - stv0367cab_get_derot_freq(state, cab_state->adc_clk)
2457 - cab_state->adc_clk / 1000 + ifkhz;
2459 cab_state->freq_khz =
2460 FE_Cab_TunerGetFrequency(pIntParams->hTuner)
2461 - stv0367cab_get_derot_freq(state, cab_state->adc_clk)
2465 cab_state->freq_khz =
2466 FE_Cab_TunerGetFrequency(pIntParams->hTuner) +
2467 stv0367cab_get_derot_freq(state,
2468 cab_state->adc_clk) -
2469 cab_state->adc_clk / 4000;
2472 cab_state->symbol_rate = stv0367cab_GetSymbolRate(state,
2474 cab_state->locked = 1;
2476 /* stv0367_setbits(state, F367CAB_AGC_ACCUMRSTSEL,7);*/
2480 signalType = FE_CAB_NOAGC;
2483 signalType = FE_CAB_NOTIMING;
2486 signalType = FE_CAB_TIMINGOK;
2489 signalType = FE_CAB_NOCARRIER;
2492 signalType = FE_CAB_CARRIEROK;
2495 signalType = FE_CAB_NOBLIND;
2498 signalType = FE_CAB_BLINDOK;
2501 signalType = FE_CAB_NODEMOD;
2504 signalType = FE_CAB_DEMODOK;
2507 signalType = FE_CAB_DEMODOK;
2510 signalType = FE_CAB_NODEMOD;
2513 signalType = FE_CAB_NOBLIND;
2516 signalType = FE_CAB_NOSIGNAL;
2524 /* Set the AGC control values to tracking values */
2525 stv0367_writebits(state, F367CAB_AGC_ACCUMRSTSEL, TrackAGCAccum);
2529 static int stv0367cab_set_frontend(struct dvb_frontend *fe)
2531 struct dtv_frontend_properties *p = &fe->dtv_property_cache;
2532 struct stv0367_state *state = fe->demodulator_priv;
2533 struct stv0367cab_state *cab_state = state->cab_state;
2534 enum stv0367cab_mod QAMSize = 0;
2536 dprintk("%s: freq = %d, srate = %d\n", __func__,
2537 p->frequency, p->symbol_rate);
2539 cab_state->derot_offset = 0;
2541 switch (p->modulation) {
2543 QAMSize = FE_CAB_MOD_QAM16;
2546 QAMSize = FE_CAB_MOD_QAM32;
2549 QAMSize = FE_CAB_MOD_QAM64;
2552 QAMSize = FE_CAB_MOD_QAM128;
2555 QAMSize = FE_CAB_MOD_QAM256;
2561 if (state->reinit_on_setfrontend)
2562 stv0367cab_init(fe);
2564 /* Tuner Frequency Setting */
2565 if (fe->ops.tuner_ops.set_params) {
2566 if (state->use_i2c_gatectrl && fe->ops.i2c_gate_ctrl)
2567 fe->ops.i2c_gate_ctrl(fe, 1);
2568 fe->ops.tuner_ops.set_params(fe);
2569 if (state->use_i2c_gatectrl && fe->ops.i2c_gate_ctrl)
2570 fe->ops.i2c_gate_ctrl(fe, 0);
2573 stv0367cab_SetQamSize(
2578 stv0367cab_set_srate(state,
2583 /* Search algorithm launch, [-1.1*RangeOffset, +1.1*RangeOffset] scan */
2584 cab_state->state = stv0367cab_algo(state, p);
2588 static int stv0367cab_get_frontend(struct dvb_frontend *fe,
2589 struct dtv_frontend_properties *p)
2591 struct stv0367_state *state = fe->demodulator_priv;
2592 struct stv0367cab_state *cab_state = state->cab_state;
2595 enum stv0367cab_mod QAMSize;
2597 dprintk("%s:\n", __func__);
2599 stv0367_get_if_khz(state, &ifkhz);
2600 p->symbol_rate = stv0367cab_GetSymbolRate(state, cab_state->mclk);
2602 QAMSize = stv0367_readbits(state, F367CAB_QAM_MODE);
2604 case FE_CAB_MOD_QAM16:
2605 p->modulation = QAM_16;
2607 case FE_CAB_MOD_QAM32:
2608 p->modulation = QAM_32;
2610 case FE_CAB_MOD_QAM64:
2611 p->modulation = QAM_64;
2613 case FE_CAB_MOD_QAM128:
2614 p->modulation = QAM_128;
2616 case FE_CAB_MOD_QAM256:
2617 p->modulation = QAM_256;
2623 p->frequency = stv0367_get_tuner_freq(fe);
2625 dprintk("%s: tuner frequency = %d\n", __func__, p->frequency);
2629 (stv0367cab_get_derot_freq(state, cab_state->adc_clk) -
2630 cab_state->adc_clk / 4000);
2634 if (ifkhz > cab_state->adc_clk / 1000)
2635 p->frequency += (ifkhz
2636 - stv0367cab_get_derot_freq(state, cab_state->adc_clk)
2637 - cab_state->adc_clk / 1000);
2639 p->frequency += (ifkhz
2640 - stv0367cab_get_derot_freq(state, cab_state->adc_clk));
2646 void stv0367cab_GetErrorCount(state, enum stv0367cab_mod QAMSize,
2647 u32 symbol_rate, FE_367qam_Monitor *Monitor_results)
2649 stv0367cab_OptimiseNByteAndGetBER(state, QAMSize, symbol_rate, Monitor_results);
2650 stv0367cab_GetPacketsCount(state, Monitor_results);
2655 static int stv0367cab_read_ber(struct dvb_frontend *fe, u32 *ber)
2657 struct stv0367_state *state = fe->demodulator_priv;
2662 static s32 stv0367cab_get_rf_lvl(struct stv0367_state *state)
2665 s32 RfAgcPwm = 0, IfAgcPwm = 0;
2668 stv0367_writebits(state, F367CAB_STDBY_ADCGP, 0x0);
2671 (stv0367_readbits(state, F367CAB_RF_AGC1_LEVEL_LO) & 0x03) +
2672 (stv0367_readbits(state, F367CAB_RF_AGC1_LEVEL_HI) << 2);
2673 RfAgcPwm = 100 * RfAgcPwm / 1023;
2676 stv0367_readbits(state, F367CAB_AGC_IF_PWMCMD_LO) +
2677 (stv0367_readbits(state, F367CAB_AGC_IF_PWMCMD_HI) << 8);
2678 if (IfAgcPwm >= 2048)
2683 IfAgcPwm = 100 * IfAgcPwm / 4095;
2685 /* For DTT75467 on NIM */
2686 if (RfAgcPwm < 90 && IfAgcPwm < 28) {
2687 for (i = 0; i < RF_LOOKUP_TABLE_SIZE; i++) {
2688 if (RfAgcPwm <= stv0367cab_RF_LookUp1[0][i]) {
2689 rfLevel = (-1) * stv0367cab_RF_LookUp1[1][i];
2693 if (i == RF_LOOKUP_TABLE_SIZE)
2695 } else { /*if IF AGC>10*/
2696 for (i = 0; i < RF_LOOKUP_TABLE2_SIZE; i++) {
2697 if (IfAgcPwm <= stv0367cab_RF_LookUp2[0][i]) {
2698 rfLevel = (-1) * stv0367cab_RF_LookUp2[1][i];
2702 if (i == RF_LOOKUP_TABLE2_SIZE)
2708 static int stv0367cab_read_strength(struct dvb_frontend *fe, u16 *strength)
2710 struct stv0367_state *state = fe->demodulator_priv;
2712 s32 signal = stv0367cab_get_rf_lvl(state);
2714 dprintk("%s: signal=%d dBm\n", __func__, signal);
2719 *strength = (22 + signal) * (-1311);
2721 dprintk("%s: strength=%d\n", __func__, (*strength));
2726 static int stv0367cab_snr_power(struct dvb_frontend *fe)
2728 struct stv0367_state *state = fe->demodulator_priv;
2729 enum stv0367cab_mod QAMSize;
2731 QAMSize = stv0367_readbits(state, F367CAB_QAM_MODE);
2733 case FE_CAB_MOD_QAM4:
2735 case FE_CAB_MOD_QAM16:
2737 case FE_CAB_MOD_QAM32:
2739 case FE_CAB_MOD_QAM64:
2741 case FE_CAB_MOD_QAM128:
2743 case FE_CAB_MOD_QAM256:
2745 case FE_CAB_MOD_QAM1024:
2754 static int stv0367cab_snr_readreg(struct dvb_frontend *fe, int avgdiv)
2756 struct stv0367_state *state = fe->demodulator_priv;
2760 for (i = 0; i < 10; i++) {
2761 regval += (stv0367_readbits(state, F367CAB_SNR_LO)
2762 + 256 * stv0367_readbits(state, F367CAB_SNR_HI));
2771 static int stv0367cab_read_snr(struct dvb_frontend *fe, u16 *snr)
2773 struct stv0367_state *state = fe->demodulator_priv;
2774 u32 noisepercentage;
2775 u32 regval = 0, temp = 0;
2778 power = stv0367cab_snr_power(fe);
2779 regval = stv0367cab_snr_readreg(fe, 1);
2783 * (1 << (3 + stv0367_readbits(state, F367CAB_SNR_PER)));
2787 /* table values, not needed to calculate logarithms */
2789 noisepercentage = 100;
2790 else if (temp >= 3981)
2791 noisepercentage = 93;
2792 else if (temp >= 3162)
2793 noisepercentage = 86;
2794 else if (temp >= 2512)
2795 noisepercentage = 79;
2796 else if (temp >= 1995)
2797 noisepercentage = 72;
2798 else if (temp >= 1585)
2799 noisepercentage = 65;
2800 else if (temp >= 1259)
2801 noisepercentage = 58;
2802 else if (temp >= 1000)
2803 noisepercentage = 50;
2804 else if (temp >= 794)
2805 noisepercentage = 43;
2806 else if (temp >= 501)
2807 noisepercentage = 36;
2808 else if (temp >= 316)
2809 noisepercentage = 29;
2810 else if (temp >= 200)
2811 noisepercentage = 22;
2812 else if (temp >= 158)
2813 noisepercentage = 14;
2814 else if (temp >= 126)
2815 noisepercentage = 7;
2817 noisepercentage = 0;
2819 dprintk("%s: noisepercentage=%d\n", __func__, noisepercentage);
2821 *snr = (noisepercentage * 65535) / 100;
2826 static int stv0367cab_read_ucblcks(struct dvb_frontend *fe, u32 *ucblocks)
2828 struct stv0367_state *state = fe->demodulator_priv;
2829 int corrected, tscount;
2831 *ucblocks = (stv0367_readreg(state, R367CAB_RS_COUNTER_5) << 8)
2832 | stv0367_readreg(state, R367CAB_RS_COUNTER_4);
2833 corrected = (stv0367_readreg(state, R367CAB_RS_COUNTER_3) << 8)
2834 | stv0367_readreg(state, R367CAB_RS_COUNTER_2);
2835 tscount = (stv0367_readreg(state, R367CAB_RS_COUNTER_2) << 8)
2836 | stv0367_readreg(state, R367CAB_RS_COUNTER_1);
2838 dprintk("%s: uncorrected blocks=%d corrected blocks=%d tscount=%d\n",
2839 __func__, *ucblocks, corrected, tscount);
2844 static const struct dvb_frontend_ops stv0367cab_ops = {
2845 .delsys = { SYS_DVBC_ANNEX_A },
2847 .name = "ST STV0367 DVB-C",
2848 .frequency_min = 47000000,
2849 .frequency_max = 862000000,
2850 .frequency_stepsize = 62500,
2851 .symbol_rate_min = 870000,
2852 .symbol_rate_max = 11700000,
2853 .caps = 0x400 |/* FE_CAN_QAM_4 */
2854 FE_CAN_QAM_16 | FE_CAN_QAM_32 |
2855 FE_CAN_QAM_64 | FE_CAN_QAM_128 |
2856 FE_CAN_QAM_256 | FE_CAN_FEC_AUTO
2858 .release = stv0367_release,
2859 .init = stv0367cab_init,
2860 .sleep = stv0367cab_sleep,
2861 .i2c_gate_ctrl = stv0367cab_gate_ctrl,
2862 .set_frontend = stv0367cab_set_frontend,
2863 .get_frontend = stv0367cab_get_frontend,
2864 .read_status = stv0367cab_read_status,
2865 /* .read_ber = stv0367cab_read_ber, */
2866 .read_signal_strength = stv0367cab_read_strength,
2867 .read_snr = stv0367cab_read_snr,
2868 .read_ucblocks = stv0367cab_read_ucblcks,
2869 .get_tune_settings = stv0367_get_tune_settings,
2872 struct dvb_frontend *stv0367cab_attach(const struct stv0367_config *config,
2873 struct i2c_adapter *i2c)
2875 struct stv0367_state *state = NULL;
2876 struct stv0367cab_state *cab_state = NULL;
2878 /* allocate memory for the internal state */
2879 state = kzalloc(sizeof(struct stv0367_state), GFP_KERNEL);
2882 cab_state = kzalloc(sizeof(struct stv0367cab_state), GFP_KERNEL);
2883 if (cab_state == NULL)
2886 /* setup the state */
2888 state->config = config;
2889 cab_state->search_range = 280000;
2890 cab_state->qamfec_status_reg = F367CAB_QAMFEC_LOCK;
2891 state->cab_state = cab_state;
2892 state->fe.ops = stv0367cab_ops;
2893 state->fe.demodulator_priv = state;
2894 state->chip_id = stv0367_readreg(state, 0xf000);
2896 /* demod operation options */
2897 state->use_i2c_gatectrl = 1;
2898 state->deftabs = STV0367_DEFTAB_GENERIC;
2899 state->reinit_on_setfrontend = 1;
2900 state->auto_if_khz = 0;
2902 dprintk("%s: chip_id = 0x%x\n", __func__, state->chip_id);
2904 /* check if the demod is there */
2905 if ((state->chip_id != 0x50) && (state->chip_id != 0x60))
2915 EXPORT_SYMBOL(stv0367cab_attach);
2918 * Functions for operation on Digital Devices hardware
2921 static void stv0367ddb_setup_ter(struct stv0367_state *state)
2923 stv0367_writereg(state, R367TER_DEBUG_LT4, 0x00);
2924 stv0367_writereg(state, R367TER_DEBUG_LT5, 0x00);
2925 stv0367_writereg(state, R367TER_DEBUG_LT6, 0x00); /* R367CAB_CTRL_1 */
2926 stv0367_writereg(state, R367TER_DEBUG_LT7, 0x00); /* R367CAB_CTRL_2 */
2927 stv0367_writereg(state, R367TER_DEBUG_LT8, 0x00);
2928 stv0367_writereg(state, R367TER_DEBUG_LT9, 0x00);
2931 /* Buffer Q disabled, I Enabled, unsigned ADC */
2932 stv0367_writereg(state, R367TER_ANADIGCTRL, 0x89);
2933 stv0367_writereg(state, R367TER_DUAL_AD12, 0x04); /* ADCQ disabled */
2936 /* PLL bypassed and disabled */
2937 stv0367_writereg(state, R367TER_ANACTRL, 0x0D);
2938 stv0367_writereg(state, R367TER_TOPCTRL, 0x00); /* Set OFDM */
2940 /* IC runs at 54 MHz with a 27 MHz crystal */
2941 stv0367_pll_setup(state, STV0367_ICSPEED_53125, state->config->xtal);
2944 /* PLL enabled and used */
2945 stv0367_writereg(state, R367TER_ANACTRL, 0x00);
2947 state->activedemod = demod_ter;
2950 static void stv0367ddb_setup_cab(struct stv0367_state *state)
2952 stv0367_writereg(state, R367TER_DEBUG_LT4, 0x00);
2953 stv0367_writereg(state, R367TER_DEBUG_LT5, 0x01);
2954 stv0367_writereg(state, R367TER_DEBUG_LT6, 0x06); /* R367CAB_CTRL_1 */
2955 stv0367_writereg(state, R367TER_DEBUG_LT7, 0x03); /* R367CAB_CTRL_2 */
2956 stv0367_writereg(state, R367TER_DEBUG_LT8, 0x00);
2957 stv0367_writereg(state, R367TER_DEBUG_LT9, 0x00);
2960 /* Buffer Q disabled, I Enabled, signed ADC */
2961 stv0367_writereg(state, R367TER_ANADIGCTRL, 0x8B);
2963 stv0367_writereg(state, R367TER_DUAL_AD12, 0x04);
2966 /* PLL bypassed and disabled */
2967 stv0367_writereg(state, R367TER_ANACTRL, 0x0D);
2969 stv0367_writereg(state, R367TER_TOPCTRL, 0x10);
2971 /* IC runs at 58 MHz with a 27 MHz crystal */
2972 stv0367_pll_setup(state, STV0367_ICSPEED_58000, state->config->xtal);
2975 /* PLL enabled and used */
2976 stv0367_writereg(state, R367TER_ANACTRL, 0x00);
2978 state->cab_state->mclk = stv0367cab_get_mclk(&state->fe,
2979 state->config->xtal);
2980 state->cab_state->adc_clk = stv0367cab_get_adc_freq(&state->fe,
2981 state->config->xtal);
2983 state->activedemod = demod_cab;
2986 static int stv0367ddb_set_frontend(struct dvb_frontend *fe)
2988 struct stv0367_state *state = fe->demodulator_priv;
2990 switch (fe->dtv_property_cache.delivery_system) {
2992 if (state->activedemod != demod_ter)
2993 stv0367ddb_setup_ter(state);
2995 return stv0367ter_set_frontend(fe);
2996 case SYS_DVBC_ANNEX_A:
2997 if (state->activedemod != demod_cab)
2998 stv0367ddb_setup_cab(state);
3000 /* protect against division error oopses */
3001 if (fe->dtv_property_cache.symbol_rate == 0) {
3002 printk(KERN_ERR "Invalid symbol rate\n");
3006 return stv0367cab_set_frontend(fe);
3014 static void stv0367ddb_read_signal_strength(struct dvb_frontend *fe)
3016 struct stv0367_state *state = fe->demodulator_priv;
3017 struct dtv_frontend_properties *p = &fe->dtv_property_cache;
3020 switch (state->activedemod) {
3022 signalstrength = stv0367cab_get_rf_lvl(state) * 1000;
3025 p->strength.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
3029 p->strength.stat[0].scale = FE_SCALE_DECIBEL;
3030 p->strength.stat[0].uvalue = signalstrength;
3033 static void stv0367ddb_read_snr(struct dvb_frontend *fe)
3035 struct stv0367_state *state = fe->demodulator_priv;
3036 struct dtv_frontend_properties *p = &fe->dtv_property_cache;
3038 u32 regval, tmpval, snrval = 0;
3040 switch (state->activedemod) {
3042 snrval = stv0367ter_snr_readreg(fe);
3045 cab_pwr = stv0367cab_snr_power(fe);
3046 regval = stv0367cab_snr_readreg(fe, 0);
3048 /* prevent division by zero */
3054 tmpval = (cab_pwr * 320) / regval;
3055 snrval = ((tmpval != 0) ? (intlog2(tmpval) / 5581) : 0);
3058 p->cnr.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
3062 p->cnr.stat[0].scale = FE_SCALE_DECIBEL;
3063 p->cnr.stat[0].uvalue = snrval;
3066 static void stv0367ddb_read_ucblocks(struct dvb_frontend *fe)
3068 struct stv0367_state *state = fe->demodulator_priv;
3069 struct dtv_frontend_properties *p = &fe->dtv_property_cache;
3072 switch (state->activedemod) {
3074 stv0367ter_read_ucblocks(fe, &ucblocks);
3077 stv0367cab_read_ucblcks(fe, &ucblocks);
3080 p->block_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
3084 p->block_error.stat[0].scale = FE_SCALE_COUNTER;
3085 p->block_error.stat[0].uvalue = ucblocks;
3088 static int stv0367ddb_read_status(struct dvb_frontend *fe,
3089 enum fe_status *status)
3091 struct stv0367_state *state = fe->demodulator_priv;
3092 struct dtv_frontend_properties *p = &fe->dtv_property_cache;
3095 switch (state->activedemod) {
3097 ret = stv0367ter_read_status(fe, status);
3100 ret = stv0367cab_read_status(fe, status);
3106 /* stop and report on *_read_status failure */
3110 stv0367ddb_read_signal_strength(fe);
3112 /* read carrier/noise when a carrier is detected */
3113 if (*status & FE_HAS_CARRIER)
3114 stv0367ddb_read_snr(fe);
3116 p->cnr.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
3118 /* read uncorrected blocks on FE_HAS_LOCK */
3119 if (*status & FE_HAS_LOCK)
3120 stv0367ddb_read_ucblocks(fe);
3122 p->block_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
3127 static int stv0367ddb_get_frontend(struct dvb_frontend *fe,
3128 struct dtv_frontend_properties *p)
3130 struct stv0367_state *state = fe->demodulator_priv;
3132 switch (state->activedemod) {
3134 return stv0367ter_get_frontend(fe, p);
3136 return stv0367cab_get_frontend(fe, p);
3144 static int stv0367ddb_sleep(struct dvb_frontend *fe)
3146 struct stv0367_state *state = fe->demodulator_priv;
3148 switch (state->activedemod) {
3150 state->activedemod = demod_none;
3151 return stv0367ter_sleep(fe);
3153 state->activedemod = demod_none;
3154 return stv0367cab_sleep(fe);
3162 static int stv0367ddb_init(struct stv0367_state *state)
3164 struct stv0367ter_state *ter_state = state->ter_state;
3165 struct dtv_frontend_properties *p = &state->fe.dtv_property_cache;
3167 stv0367_writereg(state, R367TER_TOPCTRL, 0x10);
3169 if (stv0367_deftabs[state->deftabs][STV0367_TAB_BASE])
3170 stv0367_write_table(state,
3171 stv0367_deftabs[state->deftabs][STV0367_TAB_BASE]);
3173 stv0367_write_table(state,
3174 stv0367_deftabs[state->deftabs][STV0367_TAB_CAB]);
3176 stv0367_writereg(state, R367TER_TOPCTRL, 0x00);
3177 stv0367_write_table(state,
3178 stv0367_deftabs[state->deftabs][STV0367_TAB_TER]);
3180 stv0367_writereg(state, R367TER_GAIN_SRC1, 0x2A);
3181 stv0367_writereg(state, R367TER_GAIN_SRC2, 0xD6);
3182 stv0367_writereg(state, R367TER_INC_DEROT1, 0x55);
3183 stv0367_writereg(state, R367TER_INC_DEROT2, 0x55);
3184 stv0367_writereg(state, R367TER_TRL_CTL, 0x14);
3185 stv0367_writereg(state, R367TER_TRL_NOMRATE1, 0xAE);
3186 stv0367_writereg(state, R367TER_TRL_NOMRATE2, 0x56);
3187 stv0367_writereg(state, R367TER_FEPATH_CFG, 0x0);
3191 stv0367_writereg(state, R367TER_TSCFGH, 0x70);
3192 stv0367_writereg(state, R367TER_TSCFGM, 0xC0);
3193 stv0367_writereg(state, R367TER_TSCFGL, 0x20);
3194 stv0367_writereg(state, R367TER_TSSPEED, 0x40); /* Fixed at 54 MHz */
3196 stv0367_writereg(state, R367TER_TSCFGH, 0x71);
3197 stv0367_writereg(state, R367TER_TSCFGH, 0x70);
3199 stv0367_writereg(state, R367TER_TOPCTRL, 0x10);
3201 /* Also needed for QAM */
3202 stv0367_writereg(state, R367TER_AGC12C, 0x01); /* AGC Pin setup */
3204 stv0367_writereg(state, R367TER_AGCCTRL1, 0x8A);
3206 /* QAM TS setup, note exact format also depends on descrambler */
3208 /* Inverted Clock, Swap, serial */
3209 stv0367_writereg(state, R367CAB_OUTFORMAT_0, 0x85);
3211 /* Clock setup (PLL bypassed and disabled) */
3212 stv0367_writereg(state, R367TER_ANACTRL, 0x0D);
3214 /* IC runs at 58 MHz with a 27 MHz crystal */
3215 stv0367_pll_setup(state, STV0367_ICSPEED_58000, state->config->xtal);
3218 /* Buffer Q disabled, I Enabled, signed ADC */
3219 stv0367_writereg(state, R367TER_ANADIGCTRL, 0x8b);
3220 stv0367_writereg(state, R367TER_DUAL_AD12, 0x04); /* ADCQ disabled */
3222 /* Improves the C/N lock limit */
3223 stv0367_writereg(state, R367CAB_FSM_SNR2_HTH, 0x23);
3224 /* ZIF/IF Automatic mode */
3225 stv0367_writereg(state, R367CAB_IQ_QAM, 0x01);
3226 /* Improving burst noise performances */
3227 stv0367_writereg(state, R367CAB_EQU_FFE_LEAKAGE, 0x83);
3228 /* Improving ACI performances */
3229 stv0367_writereg(state, R367CAB_IQDEM_ADJ_EN, 0x05);
3231 /* PLL enabled and used */
3232 stv0367_writereg(state, R367TER_ANACTRL, 0x00);
3234 stv0367_writereg(state, R367TER_I2CRPT, (0x08 | ((5 & 0x07) << 4)));
3236 ter_state->pBER = 0;
3237 ter_state->first_lock = 0;
3238 ter_state->unlock_counter = 2;
3240 p->strength.len = 1;
3241 p->strength.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
3243 p->cnr.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
3244 p->block_error.len = 1;
3245 p->block_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
3250 static const struct dvb_frontend_ops stv0367ddb_ops = {
3251 .delsys = { SYS_DVBC_ANNEX_A, SYS_DVBT },
3253 .name = "ST STV0367 DDB DVB-C/T",
3254 .frequency_min = 47000000,
3255 .frequency_max = 865000000,
3256 .frequency_stepsize = 166667,
3257 .frequency_tolerance = 0,
3258 .symbol_rate_min = 870000,
3259 .symbol_rate_max = 11700000,
3261 0x400 |/* FE_CAN_QAM_4 */
3262 FE_CAN_QAM_16 | FE_CAN_QAM_32 |
3263 FE_CAN_QAM_64 | FE_CAN_QAM_128 |
3264 FE_CAN_QAM_256 | FE_CAN_QAM_AUTO |
3266 FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 | FE_CAN_FEC_3_4 |
3267 FE_CAN_FEC_5_6 | FE_CAN_FEC_7_8 | FE_CAN_FEC_AUTO |
3268 FE_CAN_QPSK | FE_CAN_TRANSMISSION_MODE_AUTO |
3269 FE_CAN_RECOVER | FE_CAN_INVERSION_AUTO |
3272 .release = stv0367_release,
3273 .sleep = stv0367ddb_sleep,
3274 .i2c_gate_ctrl = stv0367cab_gate_ctrl, /* valid for TER and CAB */
3275 .set_frontend = stv0367ddb_set_frontend,
3276 .get_frontend = stv0367ddb_get_frontend,
3277 .get_tune_settings = stv0367_get_tune_settings,
3278 .read_status = stv0367ddb_read_status,
3281 struct dvb_frontend *stv0367ddb_attach(const struct stv0367_config *config,
3282 struct i2c_adapter *i2c)
3284 struct stv0367_state *state = NULL;
3285 struct stv0367ter_state *ter_state = NULL;
3286 struct stv0367cab_state *cab_state = NULL;
3288 /* allocate memory for the internal state */
3289 state = kzalloc(sizeof(struct stv0367_state), GFP_KERNEL);
3292 ter_state = kzalloc(sizeof(struct stv0367ter_state), GFP_KERNEL);
3293 if (ter_state == NULL)
3295 cab_state = kzalloc(sizeof(struct stv0367cab_state), GFP_KERNEL);
3296 if (cab_state == NULL)
3299 /* setup the state */
3301 state->config = config;
3302 state->ter_state = ter_state;
3303 cab_state->search_range = 280000;
3304 cab_state->qamfec_status_reg = F367CAB_DESCR_SYNCSTATE;
3305 state->cab_state = cab_state;
3306 state->fe.ops = stv0367ddb_ops;
3307 state->fe.demodulator_priv = state;
3308 state->chip_id = stv0367_readreg(state, R367TER_ID);
3310 /* demod operation options */
3311 state->use_i2c_gatectrl = 0;
3312 state->deftabs = STV0367_DEFTAB_DDB;
3313 state->reinit_on_setfrontend = 0;
3314 state->auto_if_khz = 1;
3315 state->activedemod = demod_none;
3317 dprintk("%s: chip_id = 0x%x\n", __func__, state->chip_id);
3319 /* check if the demod is there */
3320 if ((state->chip_id != 0x50) && (state->chip_id != 0x60))
3323 dev_info(&i2c->dev, "Found %s with ChipID %02X at adr %02X\n",
3324 state->fe.ops.info.name, state->chip_id,
3325 config->demod_address);
3327 stv0367ddb_init(state);
3337 EXPORT_SYMBOL(stv0367ddb_attach);
3339 MODULE_PARM_DESC(debug, "Set debug");
3340 MODULE_PARM_DESC(i2c_debug, "Set i2c debug");
3342 MODULE_AUTHOR("Igor M. Liplianin");
3343 MODULE_DESCRIPTION("ST STV0367 DVB-C/T demodulator driver");
3344 MODULE_LICENSE("GPL");