blob: 5b526732498ad4950e36d3a17e0dfce188be9b87 [file] [log] [blame]
Igor M. Liplianin17cce932011-01-25 17:02:00 -03001/*
2 * stv0367.c
3 *
4 * Driver for ST STV0367 DVB-T & DVB-C demodulator IC.
5 *
6 * Copyright (C) ST Microelectronics.
7 * Copyright (C) 2010,2011 NetUP Inc.
8 * Copyright (C) 2010,2011 Igor M. Liplianin <liplianin@netup.ru>
9 *
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.
14 *
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
18 *
19 * GNU General Public License for more details.
Igor M. Liplianin17cce932011-01-25 17:02:00 -030020 */
21
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>
27
28#include "stv0367.h"
Daniel Scheller8881ceb2017-03-29 13:43:04 -030029#include "stv0367_defs.h"
Igor M. Liplianin17cce932011-01-25 17:02:00 -030030#include "stv0367_regs.h"
31#include "stv0367_priv.h"
32
Mauro Carvalho Chehab9aca4fb2013-11-02 05:17:01 -030033/* Max transfer size done by I2C transfer functions */
34#define MAX_XFER_SIZE 64
35
Igor M. Liplianin17cce932011-01-25 17:02:00 -030036static int stvdebug;
37module_param_named(debug, stvdebug, int, 0644);
38
39static int i2cdebug;
40module_param_named(i2c_debug, i2cdebug, int, 0644);
41
42#define dprintk(args...) \
43 do { \
44 if (stvdebug) \
45 printk(KERN_DEBUG args); \
46 } while (0)
47 /* DVB-C */
48
49struct stv0367cab_state {
50 enum stv0367_cab_signal_type state;
51 u32 mclk;
52 u32 adc_clk;
53 s32 search_range;
54 s32 derot_offset;
55 /* results */
56 int locked; /* channel found */
57 u32 freq_khz; /* found frequency (in kHz) */
58 u32 symbol_rate; /* found symbol rate (in Bds) */
Mauro Carvalho Chehab0df289a2015-06-07 14:53:52 -030059 enum fe_spectral_inversion spect_inv; /* Spectrum Inversion */
Igor M. Liplianin17cce932011-01-25 17:02:00 -030060};
61
62struct stv0367ter_state {
63 /* DVB-T */
64 enum stv0367_ter_signal_type state;
65 enum stv0367_ter_if_iq_mode if_iq_mode;
66 enum stv0367_ter_mode mode;/* mode 2K or 8K */
Mauro Carvalho Chehab0df289a2015-06-07 14:53:52 -030067 enum fe_guard_interval guard;
Igor M. Liplianin17cce932011-01-25 17:02:00 -030068 enum stv0367_ter_hierarchy hierarchy;
69 u32 frequency;
Mauro Carvalho Chehab0df289a2015-06-07 14:53:52 -030070 enum fe_spectral_inversion sense; /* current search spectrum */
Igor M. Liplianin17cce932011-01-25 17:02:00 -030071 u8 force; /* force mode/guard */
72 u8 bw; /* channel width 6, 7 or 8 in MHz */
73 u8 pBW; /* channel width used during previous lock */
74 u32 pBER;
75 u32 pPER;
76 u32 ucblocks;
77 s8 echo_pos; /* echo position */
78 u8 first_lock;
79 u8 unlock_counter;
80 u32 agc_val;
81};
82
83struct stv0367_state {
84 struct dvb_frontend fe;
85 struct i2c_adapter *i2c;
86 /* config settings */
87 const struct stv0367_config *config;
88 u8 chip_id;
89 /* DVB-C */
90 struct stv0367cab_state *cab_state;
91 /* DVB-T */
92 struct stv0367ter_state *ter_state;
Daniel Schellerf61c2992017-03-29 13:43:01 -030093 /* flags for operation control */
94 u8 use_i2c_gatectrl;
Daniel Scheller8881ceb2017-03-29 13:43:04 -030095 u8 deftabs;
Igor M. Liplianin17cce932011-01-25 17:02:00 -030096};
97
98#define RF_LOOKUP_TABLE_SIZE 31
99#define RF_LOOKUP_TABLE2_SIZE 16
100/* RF Level (for RF AGC->AGC1) Lookup Table, depends on the board and tuner.*/
Hans Verkuil817d2fd2014-08-20 19:30:33 -0300101static const s32 stv0367cab_RF_LookUp1[RF_LOOKUP_TABLE_SIZE][RF_LOOKUP_TABLE_SIZE] = {
Igor M. Liplianin17cce932011-01-25 17:02:00 -0300102 {/*AGC1*/
103 48, 50, 51, 53, 54, 56, 57, 58, 60, 61, 62, 63,
104 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75,
105 76, 77, 78, 80, 83, 85, 88,
106 }, {/*RF(dbm)*/
107 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33,
108 34, 35, 36, 37, 38, 39, 41, 42, 43, 44, 46, 47,
109 49, 50, 52, 53, 54, 55, 56,
110 }
111};
112/* RF Level (for IF AGC->AGC2) Lookup Table, depends on the board and tuner.*/
Hans Verkuil817d2fd2014-08-20 19:30:33 -0300113static const s32 stv0367cab_RF_LookUp2[RF_LOOKUP_TABLE2_SIZE][RF_LOOKUP_TABLE2_SIZE] = {
Igor M. Liplianin17cce932011-01-25 17:02:00 -0300114 {/*AGC2*/
115 28, 29, 31, 32, 34, 35, 36, 37,
116 38, 39, 40, 41, 42, 43, 44, 45,
117 }, {/*RF(dbm)*/
118 57, 58, 59, 60, 61, 62, 63, 64,
119 65, 66, 67, 68, 69, 70, 71, 72,
120 }
121};
122
Igor M. Liplianin17cce932011-01-25 17:02:00 -0300123static
124int stv0367_writeregs(struct stv0367_state *state, u16 reg, u8 *data, int len)
125{
Mauro Carvalho Chehab9aca4fb2013-11-02 05:17:01 -0300126 u8 buf[MAX_XFER_SIZE];
Igor M. Liplianin17cce932011-01-25 17:02:00 -0300127 struct i2c_msg msg = {
128 .addr = state->config->demod_address,
129 .flags = 0,
130 .buf = buf,
131 .len = len + 2
132 };
133 int ret;
134
Mauro Carvalho Chehab9aca4fb2013-11-02 05:17:01 -0300135 if (2 + len > sizeof(buf)) {
136 printk(KERN_WARNING
137 "%s: i2c wr reg=%04x: len=%d is too big!\n",
138 KBUILD_MODNAME, reg, len);
139 return -EINVAL;
140 }
141
142
Igor M. Liplianin17cce932011-01-25 17:02:00 -0300143 buf[0] = MSB(reg);
144 buf[1] = LSB(reg);
145 memcpy(buf + 2, data, len);
146
147 if (i2cdebug)
Peter Griffin86a10282015-07-30 14:08:51 -0300148 printk(KERN_DEBUG "%s: [%02x] %02x: %02x\n", __func__,
149 state->config->demod_address, reg, buf[2]);
Igor M. Liplianin17cce932011-01-25 17:02:00 -0300150
151 ret = i2c_transfer(state->i2c, &msg, 1);
152 if (ret != 1)
Peter Griffin86a10282015-07-30 14:08:51 -0300153 printk(KERN_ERR "%s: i2c write error! ([%02x] %02x: %02x)\n",
154 __func__, state->config->demod_address, reg, buf[2]);
Igor M. Liplianin17cce932011-01-25 17:02:00 -0300155
156 return (ret != 1) ? -EREMOTEIO : 0;
157}
158
159static int stv0367_writereg(struct stv0367_state *state, u16 reg, u8 data)
160{
161 return stv0367_writeregs(state, reg, &data, 1);
162}
163
164static u8 stv0367_readreg(struct stv0367_state *state, u16 reg)
165{
166 u8 b0[] = { 0, 0 };
167 u8 b1[] = { 0 };
168 struct i2c_msg msg[] = {
169 {
170 .addr = state->config->demod_address,
171 .flags = 0,
172 .buf = b0,
173 .len = 2
174 }, {
175 .addr = state->config->demod_address,
176 .flags = I2C_M_RD,
177 .buf = b1,
178 .len = 1
179 }
180 };
181 int ret;
182
183 b0[0] = MSB(reg);
184 b0[1] = LSB(reg);
185
186 ret = i2c_transfer(state->i2c, msg, 2);
187 if (ret != 2)
Peter Griffin86a10282015-07-30 14:08:51 -0300188 printk(KERN_ERR "%s: i2c read error ([%02x] %02x: %02x)\n",
189 __func__, state->config->demod_address, reg, b1[0]);
Igor M. Liplianin17cce932011-01-25 17:02:00 -0300190
191 if (i2cdebug)
Peter Griffin86a10282015-07-30 14:08:51 -0300192 printk(KERN_DEBUG "%s: [%02x] %02x: %02x\n", __func__,
193 state->config->demod_address, reg, b1[0]);
Igor M. Liplianin17cce932011-01-25 17:02:00 -0300194
195 return b1[0];
196}
197
198static void extract_mask_pos(u32 label, u8 *mask, u8 *pos)
199{
200 u8 position = 0, i = 0;
201
202 (*mask) = label & 0xff;
203
204 while ((position == 0) && (i < 8)) {
205 position = ((*mask) >> i) & 0x01;
206 i++;
207 }
208
209 (*pos) = (i - 1);
210}
211
212static void stv0367_writebits(struct stv0367_state *state, u32 label, u8 val)
213{
214 u8 reg, mask, pos;
215
216 reg = stv0367_readreg(state, (label >> 16) & 0xffff);
217 extract_mask_pos(label, &mask, &pos);
218
219 val = mask & (val << pos);
220
221 reg = (reg & (~mask)) | val;
222 stv0367_writereg(state, (label >> 16) & 0xffff, reg);
223
224}
225
226static void stv0367_setbits(u8 *reg, u32 label, u8 val)
227{
228 u8 mask, pos;
229
230 extract_mask_pos(label, &mask, &pos);
231
232 val = mask & (val << pos);
233
234 (*reg) = ((*reg) & (~mask)) | val;
235}
236
237static u8 stv0367_readbits(struct stv0367_state *state, u32 label)
238{
239 u8 val = 0xff;
240 u8 mask, pos;
241
242 extract_mask_pos(label, &mask, &pos);
243
244 val = stv0367_readreg(state, label >> 16);
245 val = (val & mask) >> pos;
246
247 return val;
248}
249
Mauro Carvalho Chehab8c8ca1c2012-10-27 11:26:42 -0300250#if 0 /* Currently, unused */
251static u8 stv0367_getbits(u8 reg, u32 label)
Igor M. Liplianin17cce932011-01-25 17:02:00 -0300252{
253 u8 mask, pos;
254
255 extract_mask_pos(label, &mask, &pos);
256
257 return (reg & mask) >> pos;
258}
Mauro Carvalho Chehab8c8ca1c2012-10-27 11:26:42 -0300259#endif
Daniel Scheller41727cb2017-03-29 13:43:03 -0300260
261static void stv0367_write_table(struct stv0367_state *state,
262 const struct st_register *deftab)
263{
264 int i = 0;
265
266 while (1) {
267 if (!deftab[i].addr)
268 break;
269 stv0367_writereg(state, deftab[i].addr, deftab[i].value);
270 i++;
271 }
272}
273
Igor M. Liplianin17cce932011-01-25 17:02:00 -0300274static int stv0367ter_gate_ctrl(struct dvb_frontend *fe, int enable)
275{
276 struct stv0367_state *state = fe->demodulator_priv;
277 u8 tmp = stv0367_readreg(state, R367TER_I2CRPT);
278
279 dprintk("%s:\n", __func__);
280
281 if (enable) {
282 stv0367_setbits(&tmp, F367TER_STOP_ENABLE, 0);
283 stv0367_setbits(&tmp, F367TER_I2CT_ON, 1);
284 } else {
285 stv0367_setbits(&tmp, F367TER_STOP_ENABLE, 1);
286 stv0367_setbits(&tmp, F367TER_I2CT_ON, 0);
287 }
288
289 stv0367_writereg(state, R367TER_I2CRPT, tmp);
290
291 return 0;
292}
293
294static u32 stv0367_get_tuner_freq(struct dvb_frontend *fe)
295{
Emil Goode20721182014-06-24 18:42:27 -0300296 struct dvb_frontend_ops *frontend_ops = &fe->ops;
297 struct dvb_tuner_ops *tuner_ops = &frontend_ops->tuner_ops;
Igor M. Liplianin17cce932011-01-25 17:02:00 -0300298 u32 freq = 0;
Dan Carpenterbf512b22011-03-06 10:40:11 -0300299 int err = 0;
Igor M. Liplianin17cce932011-01-25 17:02:00 -0300300
301 dprintk("%s:\n", __func__);
302
Igor M. Liplianin17cce932011-01-25 17:02:00 -0300303 if (tuner_ops->get_frequency) {
304 err = tuner_ops->get_frequency(fe, &freq);
305 if (err < 0) {
306 printk(KERN_ERR "%s: Invalid parameter\n", __func__);
307 return err;
308 }
309
310 dprintk("%s: frequency=%d\n", __func__, freq);
311
312 } else
313 return -1;
314
315 return freq;
316}
317
318static u16 CellsCoeffs_8MHz_367cofdm[3][6][5] = {
319 {
320 {0x10EF, 0xE205, 0x10EF, 0xCE49, 0x6DA7}, /* CELL 1 COEFFS 27M*/
321 {0x2151, 0xc557, 0x2151, 0xc705, 0x6f93}, /* CELL 2 COEFFS */
322 {0x2503, 0xc000, 0x2503, 0xc375, 0x7194}, /* CELL 3 COEFFS */
323 {0x20E9, 0xca94, 0x20e9, 0xc153, 0x7194}, /* CELL 4 COEFFS */
324 {0x06EF, 0xF852, 0x06EF, 0xC057, 0x7207}, /* CELL 5 COEFFS */
325 {0x0000, 0x0ECC, 0x0ECC, 0x0000, 0x3647} /* CELL 6 COEFFS */
326 }, {
327 {0x10A0, 0xE2AF, 0x10A1, 0xCE76, 0x6D6D}, /* CELL 1 COEFFS 25M*/
328 {0x20DC, 0xC676, 0x20D9, 0xC80A, 0x6F29},
329 {0x2532, 0xC000, 0x251D, 0xC391, 0x706F},
330 {0x1F7A, 0xCD2B, 0x2032, 0xC15E, 0x711F},
331 {0x0698, 0xFA5E, 0x0568, 0xC059, 0x7193},
332 {0x0000, 0x0918, 0x149C, 0x0000, 0x3642} /* CELL 6 COEFFS */
333 }, {
334 {0x0000, 0x0000, 0x0000, 0x0000, 0x0000}, /* 30M */
335 {0x0000, 0x0000, 0x0000, 0x0000, 0x0000},
336 {0x0000, 0x0000, 0x0000, 0x0000, 0x0000},
337 {0x0000, 0x0000, 0x0000, 0x0000, 0x0000},
338 {0x0000, 0x0000, 0x0000, 0x0000, 0x0000},
339 {0x0000, 0x0000, 0x0000, 0x0000, 0x0000}
340 }
341};
342
343static u16 CellsCoeffs_7MHz_367cofdm[3][6][5] = {
344 {
345 {0x12CA, 0xDDAF, 0x12CA, 0xCCEB, 0x6FB1}, /* CELL 1 COEFFS 27M*/
346 {0x2329, 0xC000, 0x2329, 0xC6B0, 0x725F}, /* CELL 2 COEFFS */
347 {0x2394, 0xC000, 0x2394, 0xC2C7, 0x7410}, /* CELL 3 COEFFS */
348 {0x251C, 0xC000, 0x251C, 0xC103, 0x74D9}, /* CELL 4 COEFFS */
349 {0x0804, 0xF546, 0x0804, 0xC040, 0x7544}, /* CELL 5 COEFFS */
350 {0x0000, 0x0CD9, 0x0CD9, 0x0000, 0x370A} /* CELL 6 COEFFS */
351 }, {
352 {0x1285, 0xDE47, 0x1285, 0xCD17, 0x6F76}, /*25M*/
353 {0x234C, 0xC000, 0x2348, 0xC6DA, 0x7206},
354 {0x23B4, 0xC000, 0x23AC, 0xC2DB, 0x73B3},
355 {0x253D, 0xC000, 0x25B6, 0xC10B, 0x747F},
356 {0x0721, 0xF79C, 0x065F, 0xC041, 0x74EB},
357 {0x0000, 0x08FA, 0x1162, 0x0000, 0x36FF}
358 }, {
359 {0x0000, 0x0000, 0x0000, 0x0000, 0x0000}, /* 30M */
360 {0x0000, 0x0000, 0x0000, 0x0000, 0x0000},
361 {0x0000, 0x0000, 0x0000, 0x0000, 0x0000},
362 {0x0000, 0x0000, 0x0000, 0x0000, 0x0000},
363 {0x0000, 0x0000, 0x0000, 0x0000, 0x0000},
364 {0x0000, 0x0000, 0x0000, 0x0000, 0x0000}
365 }
366};
367
368static u16 CellsCoeffs_6MHz_367cofdm[3][6][5] = {
369 {
370 {0x1699, 0xD5B8, 0x1699, 0xCBC3, 0x713B}, /* CELL 1 COEFFS 27M*/
371 {0x2245, 0xC000, 0x2245, 0xC568, 0x74D5}, /* CELL 2 COEFFS */
372 {0x227F, 0xC000, 0x227F, 0xC1FC, 0x76C6}, /* CELL 3 COEFFS */
373 {0x235E, 0xC000, 0x235E, 0xC0A7, 0x778A}, /* CELL 4 COEFFS */
374 {0x0ECB, 0xEA0B, 0x0ECB, 0xC027, 0x77DD}, /* CELL 5 COEFFS */
375 {0x0000, 0x0B68, 0x0B68, 0x0000, 0xC89A}, /* CELL 6 COEFFS */
376 }, {
377 {0x1655, 0xD64E, 0x1658, 0xCBEF, 0x70FE}, /*25M*/
378 {0x225E, 0xC000, 0x2256, 0xC589, 0x7489},
379 {0x2293, 0xC000, 0x2295, 0xC209, 0x767E},
380 {0x2377, 0xC000, 0x23AA, 0xC0AB, 0x7746},
381 {0x0DC7, 0xEBC8, 0x0D07, 0xC027, 0x7799},
382 {0x0000, 0x0888, 0x0E9C, 0x0000, 0x3757}
383
384 }, {
385 {0x0000, 0x0000, 0x0000, 0x0000, 0x0000}, /* 30M */
386 {0x0000, 0x0000, 0x0000, 0x0000, 0x0000},
387 {0x0000, 0x0000, 0x0000, 0x0000, 0x0000},
388 {0x0000, 0x0000, 0x0000, 0x0000, 0x0000},
389 {0x0000, 0x0000, 0x0000, 0x0000, 0x0000},
390 {0x0000, 0x0000, 0x0000, 0x0000, 0x0000}
391 }
392};
393
394static u32 stv0367ter_get_mclk(struct stv0367_state *state, u32 ExtClk_Hz)
395{
396 u32 mclk_Hz = 0; /* master clock frequency (Hz) */
397 u32 m, n, p;
398
399 dprintk("%s:\n", __func__);
400
401 if (stv0367_readbits(state, F367TER_BYPASS_PLLXN) == 0) {
402 n = (u32)stv0367_readbits(state, F367TER_PLL_NDIV);
403 if (n == 0)
404 n = n + 1;
405
406 m = (u32)stv0367_readbits(state, F367TER_PLL_MDIV);
407 if (m == 0)
408 m = m + 1;
409
410 p = (u32)stv0367_readbits(state, F367TER_PLL_PDIV);
411 if (p > 5)
412 p = 5;
413
414 mclk_Hz = ((ExtClk_Hz / 2) * n) / (m * (1 << p));
415
416 dprintk("N=%d M=%d P=%d mclk_Hz=%d ExtClk_Hz=%d\n",
417 n, m, p, mclk_Hz, ExtClk_Hz);
418 } else
419 mclk_Hz = ExtClk_Hz;
420
421 dprintk("%s: mclk_Hz=%d\n", __func__, mclk_Hz);
422
423 return mclk_Hz;
424}
425
426static int stv0367ter_filt_coeff_init(struct stv0367_state *state,
Dan Carpenterbc4b18c2011-03-06 10:41:23 -0300427 u16 CellsCoeffs[3][6][5], u32 DemodXtal)
Igor M. Liplianin17cce932011-01-25 17:02:00 -0300428{
429 int i, j, k, freq;
430
431 dprintk("%s:\n", __func__);
432
433 freq = stv0367ter_get_mclk(state, DemodXtal);
434
435 if (freq == 53125000)
436 k = 1; /* equivalent to Xtal 25M on 362*/
437 else if (freq == 54000000)
438 k = 0; /* equivalent to Xtal 27M on 362*/
439 else if (freq == 52500000)
440 k = 2; /* equivalent to Xtal 30M on 362*/
441 else
442 return 0;
443
444 for (i = 1; i <= 6; i++) {
445 stv0367_writebits(state, F367TER_IIR_CELL_NB, i - 1);
446
447 for (j = 1; j <= 5; j++) {
448 stv0367_writereg(state,
449 (R367TER_IIRCX_COEFF1_MSB + 2 * (j - 1)),
450 MSB(CellsCoeffs[k][i-1][j-1]));
451 stv0367_writereg(state,
452 (R367TER_IIRCX_COEFF1_LSB + 2 * (j - 1)),
453 LSB(CellsCoeffs[k][i-1][j-1]));
454 }
455 }
456
457 return 1;
458
459}
460
461static void stv0367ter_agc_iir_lock_detect_set(struct stv0367_state *state)
462{
463 dprintk("%s:\n", __func__);
464
465 stv0367_writebits(state, F367TER_LOCK_DETECT_LSB, 0x00);
466
467 /* Lock detect 1 */
468 stv0367_writebits(state, F367TER_LOCK_DETECT_CHOICE, 0x00);
469 stv0367_writebits(state, F367TER_LOCK_DETECT_MSB, 0x06);
470 stv0367_writebits(state, F367TER_AUT_AGC_TARGET_LSB, 0x04);
471
472 /* Lock detect 2 */
473 stv0367_writebits(state, F367TER_LOCK_DETECT_CHOICE, 0x01);
474 stv0367_writebits(state, F367TER_LOCK_DETECT_MSB, 0x06);
475 stv0367_writebits(state, F367TER_AUT_AGC_TARGET_LSB, 0x04);
476
477 /* Lock detect 3 */
478 stv0367_writebits(state, F367TER_LOCK_DETECT_CHOICE, 0x02);
479 stv0367_writebits(state, F367TER_LOCK_DETECT_MSB, 0x01);
480 stv0367_writebits(state, F367TER_AUT_AGC_TARGET_LSB, 0x00);
481
482 /* Lock detect 4 */
483 stv0367_writebits(state, F367TER_LOCK_DETECT_CHOICE, 0x03);
484 stv0367_writebits(state, F367TER_LOCK_DETECT_MSB, 0x01);
485 stv0367_writebits(state, F367TER_AUT_AGC_TARGET_LSB, 0x00);
486
487}
488
489static int stv0367_iir_filt_init(struct stv0367_state *state, u8 Bandwidth,
490 u32 DemodXtalValue)
491{
492 dprintk("%s:\n", __func__);
493
494 stv0367_writebits(state, F367TER_NRST_IIR, 0);
495
496 switch (Bandwidth) {
497 case 6:
498 if (!stv0367ter_filt_coeff_init(state,
499 CellsCoeffs_6MHz_367cofdm,
500 DemodXtalValue))
501 return 0;
502 break;
503 case 7:
504 if (!stv0367ter_filt_coeff_init(state,
505 CellsCoeffs_7MHz_367cofdm,
506 DemodXtalValue))
507 return 0;
508 break;
509 case 8:
510 if (!stv0367ter_filt_coeff_init(state,
511 CellsCoeffs_8MHz_367cofdm,
512 DemodXtalValue))
513 return 0;
514 break;
515 default:
516 return 0;
517 }
518
519 stv0367_writebits(state, F367TER_NRST_IIR, 1);
520
521 return 1;
522}
523
524static void stv0367ter_agc_iir_rst(struct stv0367_state *state)
525{
526
527 u8 com_n;
528
529 dprintk("%s:\n", __func__);
530
531 com_n = stv0367_readbits(state, F367TER_COM_N);
532
533 stv0367_writebits(state, F367TER_COM_N, 0x07);
534
535 stv0367_writebits(state, F367TER_COM_SOFT_RSTN, 0x00);
536 stv0367_writebits(state, F367TER_COM_AGC_ON, 0x00);
537
538 stv0367_writebits(state, F367TER_COM_SOFT_RSTN, 0x01);
539 stv0367_writebits(state, F367TER_COM_AGC_ON, 0x01);
540
541 stv0367_writebits(state, F367TER_COM_N, com_n);
542
543}
544
545static int stv0367ter_duration(s32 mode, int tempo1, int tempo2, int tempo3)
546{
547 int local_tempo = 0;
548 switch (mode) {
549 case 0:
550 local_tempo = tempo1;
551 break;
552 case 1:
553 local_tempo = tempo2;
554 break ;
555
556 case 2:
557 local_tempo = tempo3;
558 break;
559
560 default:
561 break;
562 }
563 /* msleep(local_tempo); */
564 return local_tempo;
565}
566
567static enum
568stv0367_ter_signal_type stv0367ter_check_syr(struct stv0367_state *state)
569{
570 int wd = 100;
571 unsigned short int SYR_var;
572 s32 SYRStatus;
573
574 dprintk("%s:\n", __func__);
575
576 SYR_var = stv0367_readbits(state, F367TER_SYR_LOCK);
577
578 while ((!SYR_var) && (wd > 0)) {
579 usleep_range(2000, 3000);
580 wd -= 2;
581 SYR_var = stv0367_readbits(state, F367TER_SYR_LOCK);
582 }
583
584 if (!SYR_var)
585 SYRStatus = FE_TER_NOSYMBOL;
586 else
587 SYRStatus = FE_TER_SYMBOLOK;
588
589 dprintk("stv0367ter_check_syr SYRStatus %s\n",
590 SYR_var == 0 ? "No Symbol" : "OK");
591
592 return SYRStatus;
593}
594
595static enum
596stv0367_ter_signal_type stv0367ter_check_cpamp(struct stv0367_state *state,
597 s32 FFTmode)
598{
599
600 s32 CPAMPvalue = 0, CPAMPStatus, CPAMPMin;
601 int wd = 0;
602
603 dprintk("%s:\n", __func__);
604
605 switch (FFTmode) {
606 case 0: /*2k mode*/
607 CPAMPMin = 20;
608 wd = 10;
609 break;
610 case 1: /*8k mode*/
611 CPAMPMin = 80;
612 wd = 55;
613 break;
614 case 2: /*4k mode*/
615 CPAMPMin = 40;
616 wd = 30;
617 break;
618 default:
619 CPAMPMin = 0xffff; /*drives to NOCPAMP */
620 break;
621 }
622
623 dprintk("%s: CPAMPMin=%d wd=%d\n", __func__, CPAMPMin, wd);
624
625 CPAMPvalue = stv0367_readbits(state, F367TER_PPM_CPAMP_DIRECT);
626 while ((CPAMPvalue < CPAMPMin) && (wd > 0)) {
627 usleep_range(1000, 2000);
628 wd -= 1;
629 CPAMPvalue = stv0367_readbits(state, F367TER_PPM_CPAMP_DIRECT);
630 /*dprintk("CPAMPvalue= %d at wd=%d\n",CPAMPvalue,wd); */
631 }
632 dprintk("******last CPAMPvalue= %d at wd=%d\n", CPAMPvalue, wd);
633 if (CPAMPvalue < CPAMPMin) {
634 CPAMPStatus = FE_TER_NOCPAMP;
Daniel Schellerdf5a38e2017-03-29 13:43:02 -0300635 dprintk("%s: CPAMP failed\n", __func__);
Igor M. Liplianin17cce932011-01-25 17:02:00 -0300636 } else {
Daniel Schellerdf5a38e2017-03-29 13:43:02 -0300637 dprintk("%s: CPAMP OK !\n", __func__);
Igor M. Liplianin17cce932011-01-25 17:02:00 -0300638 CPAMPStatus = FE_TER_CPAMPOK;
639 }
640
641 return CPAMPStatus;
642}
643
Mauro Carvalho Chehab8c8ca1c2012-10-27 11:26:42 -0300644static enum stv0367_ter_signal_type
645stv0367ter_lock_algo(struct stv0367_state *state)
Igor M. Liplianin17cce932011-01-25 17:02:00 -0300646{
647 enum stv0367_ter_signal_type ret_flag;
648 short int wd, tempo;
649 u8 try, u_var1 = 0, u_var2 = 0, u_var3 = 0, u_var4 = 0, mode, guard;
650 u8 tmp, tmp2;
651
652 dprintk("%s:\n", __func__);
653
654 if (state == NULL)
655 return FE_TER_SWNOK;
656
657 try = 0;
658 do {
659 ret_flag = FE_TER_LOCKOK;
660
661 stv0367_writebits(state, F367TER_CORE_ACTIVE, 0);
662
663 if (state->config->if_iq_mode != 0)
664 stv0367_writebits(state, F367TER_COM_N, 0x07);
665
666 stv0367_writebits(state, F367TER_GUARD, 3);/* suggest 2k 1/4 */
667 stv0367_writebits(state, F367TER_MODE, 0);
668 stv0367_writebits(state, F367TER_SYR_TR_DIS, 0);
669 usleep_range(5000, 10000);
670
671 stv0367_writebits(state, F367TER_CORE_ACTIVE, 1);
672
673
674 if (stv0367ter_check_syr(state) == FE_TER_NOSYMBOL)
675 return FE_TER_NOSYMBOL;
676 else { /*
677 if chip locked on wrong mode first try,
678 it must lock correctly second try */
679 mode = stv0367_readbits(state, F367TER_SYR_MODE);
680 if (stv0367ter_check_cpamp(state, mode) ==
681 FE_TER_NOCPAMP) {
682 if (try == 0)
683 ret_flag = FE_TER_NOCPAMP;
684
685 }
686 }
687
688 try++;
689 } while ((try < 10) && (ret_flag != FE_TER_LOCKOK));
690
691 tmp = stv0367_readreg(state, R367TER_SYR_STAT);
692 tmp2 = stv0367_readreg(state, R367TER_STATUS);
Hans Verkuilfb661a72011-03-06 09:26:24 -0300693 dprintk("state=%p\n", state);
Igor M. Liplianin17cce932011-01-25 17:02:00 -0300694 dprintk("LOCK OK! mode=%d SYR_STAT=0x%x R367TER_STATUS=0x%x\n",
695 mode, tmp, tmp2);
696
697 tmp = stv0367_readreg(state, R367TER_PRVIT);
698 tmp2 = stv0367_readreg(state, R367TER_I2CRPT);
699 dprintk("PRVIT=0x%x I2CRPT=0x%x\n", tmp, tmp2);
700
701 tmp = stv0367_readreg(state, R367TER_GAIN_SRC1);
702 dprintk("GAIN_SRC1=0x%x\n", tmp);
703
704 if ((mode != 0) && (mode != 1) && (mode != 2))
705 return FE_TER_SWNOK;
706
707 /*guard=stv0367_readbits(state,F367TER_SYR_GUARD); */
708
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300709 /*suppress EPQ auto for SYR_GARD 1/16 or 1/32
Igor M. Liplianin17cce932011-01-25 17:02:00 -0300710 and set channel predictor in automatic */
711#if 0
712 switch (guard) {
713
714 case 0:
715 case 1:
716 stv0367_writebits(state, F367TER_AUTO_LE_EN, 0);
717 stv0367_writereg(state, R367TER_CHC_CTL, 0x01);
718 break;
719 case 2:
720 case 3:
721 stv0367_writebits(state, F367TER_AUTO_LE_EN, 1);
722 stv0367_writereg(state, R367TER_CHC_CTL, 0x11);
723 break;
724
725 default:
726 return FE_TER_SWNOK;
727 }
728#endif
729
730 /*reset fec an reedsolo FOR 367 only*/
731 stv0367_writebits(state, F367TER_RST_SFEC, 1);
732 stv0367_writebits(state, F367TER_RST_REEDSOLO, 1);
733 usleep_range(1000, 2000);
734 stv0367_writebits(state, F367TER_RST_SFEC, 0);
735 stv0367_writebits(state, F367TER_RST_REEDSOLO, 0);
736
737 u_var1 = stv0367_readbits(state, F367TER_LK);
738 u_var2 = stv0367_readbits(state, F367TER_PRF);
739 u_var3 = stv0367_readbits(state, F367TER_TPS_LOCK);
740 /* u_var4=stv0367_readbits(state,F367TER_TSFIFO_LINEOK); */
741
742 wd = stv0367ter_duration(mode, 125, 500, 250);
743 tempo = stv0367ter_duration(mode, 4, 16, 8);
744
745 /*while ( ((!u_var1)||(!u_var2)||(!u_var3)||(!u_var4)) && (wd>=0)) */
746 while (((!u_var1) || (!u_var2) || (!u_var3)) && (wd >= 0)) {
747 usleep_range(1000 * tempo, 1000 * (tempo + 1));
748 wd -= tempo;
749 u_var1 = stv0367_readbits(state, F367TER_LK);
750 u_var2 = stv0367_readbits(state, F367TER_PRF);
751 u_var3 = stv0367_readbits(state, F367TER_TPS_LOCK);
752 /*u_var4=stv0367_readbits(state, F367TER_TSFIFO_LINEOK); */
753 }
754
755 if (!u_var1)
756 return FE_TER_NOLOCK;
757
758
759 if (!u_var2)
760 return FE_TER_NOPRFOUND;
761
762 if (!u_var3)
763 return FE_TER_NOTPS;
764
765 guard = stv0367_readbits(state, F367TER_SYR_GUARD);
766 stv0367_writereg(state, R367TER_CHC_CTL, 0x11);
767 switch (guard) {
768 case 0:
769 case 1:
770 stv0367_writebits(state, F367TER_AUTO_LE_EN, 0);
771 /*stv0367_writereg(state,R367TER_CHC_CTL, 0x1);*/
772 stv0367_writebits(state, F367TER_SYR_FILTER, 0);
773 break;
774 case 2:
775 case 3:
776 stv0367_writebits(state, F367TER_AUTO_LE_EN, 1);
777 /*stv0367_writereg(state,R367TER_CHC_CTL, 0x11);*/
778 stv0367_writebits(state, F367TER_SYR_FILTER, 1);
779 break;
780
781 default:
782 return FE_TER_SWNOK;
783 }
784
785 /* apply Sfec workaround if 8K 64QAM CR!=1/2*/
786 if ((stv0367_readbits(state, F367TER_TPS_CONST) == 2) &&
787 (mode == 1) &&
788 (stv0367_readbits(state, F367TER_TPS_HPCODE) != 0)) {
789 stv0367_writereg(state, R367TER_SFDLYSETH, 0xc0);
790 stv0367_writereg(state, R367TER_SFDLYSETM, 0x60);
791 stv0367_writereg(state, R367TER_SFDLYSETL, 0x0);
792 } else
793 stv0367_writereg(state, R367TER_SFDLYSETH, 0x0);
794
795 wd = stv0367ter_duration(mode, 125, 500, 250);
796 u_var4 = stv0367_readbits(state, F367TER_TSFIFO_LINEOK);
797
798 while ((!u_var4) && (wd >= 0)) {
799 usleep_range(1000 * tempo, 1000 * (tempo + 1));
800 wd -= tempo;
801 u_var4 = stv0367_readbits(state, F367TER_TSFIFO_LINEOK);
802 }
803
804 if (!u_var4)
805 return FE_TER_NOLOCK;
806
807 /* for 367 leave COM_N at 0x7 for IQ_mode*/
808 /*if(ter_state->if_iq_mode!=FE_TER_NORMAL_IF_TUNER) {
809 tempo=0;
810 while ((stv0367_readbits(state,F367TER_COM_USEGAINTRK)!=1) &&
811 (stv0367_readbits(state,F367TER_COM_AGCLOCK)!=1)&&(tempo<100)) {
812 ChipWaitOrAbort(state,1);
813 tempo+=1;
814 }
815
816 stv0367_writebits(state,F367TER_COM_N,0x17);
817 } */
818
819 stv0367_writebits(state, F367TER_SYR_TR_DIS, 1);
820
821 dprintk("FE_TER_LOCKOK !!!\n");
822
823 return FE_TER_LOCKOK;
824
825}
826
827static void stv0367ter_set_ts_mode(struct stv0367_state *state,
828 enum stv0367_ts_mode PathTS)
829{
830
831 dprintk("%s:\n", __func__);
832
833 if (state == NULL)
834 return;
835
836 stv0367_writebits(state, F367TER_TS_DIS, 0);
837 switch (PathTS) {
838 default:
839 /*for removing warning :default we can assume in parallel mode*/
840 case STV0367_PARALLEL_PUNCT_CLOCK:
841 stv0367_writebits(state, F367TER_TSFIFO_SERIAL, 0);
842 stv0367_writebits(state, F367TER_TSFIFO_DVBCI, 0);
843 break;
844 case STV0367_SERIAL_PUNCT_CLOCK:
845 stv0367_writebits(state, F367TER_TSFIFO_SERIAL, 1);
846 stv0367_writebits(state, F367TER_TSFIFO_DVBCI, 1);
847 break;
848 }
849}
850
851static void stv0367ter_set_clk_pol(struct stv0367_state *state,
852 enum stv0367_clk_pol clock)
853{
854
855 dprintk("%s:\n", __func__);
856
857 if (state == NULL)
858 return;
859
860 switch (clock) {
861 case STV0367_RISINGEDGE_CLOCK:
862 stv0367_writebits(state, F367TER_TS_BYTE_CLK_INV, 1);
863 break;
864 case STV0367_FALLINGEDGE_CLOCK:
865 stv0367_writebits(state, F367TER_TS_BYTE_CLK_INV, 0);
866 break;
867 /*case FE_TER_CLOCK_POLARITY_DEFAULT:*/
868 default:
869 stv0367_writebits(state, F367TER_TS_BYTE_CLK_INV, 0);
870 break;
871 }
872}
873
874#if 0
875static void stv0367ter_core_sw(struct stv0367_state *state)
876{
877
878 dprintk("%s:\n", __func__);
879
880 stv0367_writebits(state, F367TER_CORE_ACTIVE, 0);
881 stv0367_writebits(state, F367TER_CORE_ACTIVE, 1);
882 msleep(350);
883}
884#endif
885static int stv0367ter_standby(struct dvb_frontend *fe, u8 standby_on)
886{
887 struct stv0367_state *state = fe->demodulator_priv;
888
889 dprintk("%s:\n", __func__);
890
891 if (standby_on) {
892 stv0367_writebits(state, F367TER_STDBY, 1);
893 stv0367_writebits(state, F367TER_STDBY_FEC, 1);
894 stv0367_writebits(state, F367TER_STDBY_CORE, 1);
895 } else {
896 stv0367_writebits(state, F367TER_STDBY, 0);
897 stv0367_writebits(state, F367TER_STDBY_FEC, 0);
898 stv0367_writebits(state, F367TER_STDBY_CORE, 0);
899 }
900
901 return 0;
902}
903
904static int stv0367ter_sleep(struct dvb_frontend *fe)
905{
906 return stv0367ter_standby(fe, 1);
907}
908
Mauro Carvalho Chehab8c8ca1c2012-10-27 11:26:42 -0300909static int stv0367ter_init(struct dvb_frontend *fe)
Igor M. Liplianin17cce932011-01-25 17:02:00 -0300910{
911 struct stv0367_state *state = fe->demodulator_priv;
912 struct stv0367ter_state *ter_state = state->ter_state;
Igor M. Liplianin17cce932011-01-25 17:02:00 -0300913
914 dprintk("%s:\n", __func__);
915
916 ter_state->pBER = 0;
917
Daniel Scheller8881ceb2017-03-29 13:43:04 -0300918 stv0367_write_table(state,
919 stv0367_deftabs[state->deftabs][STV0367_TAB_TER]);
Igor M. Liplianin17cce932011-01-25 17:02:00 -0300920
921 switch (state->config->xtal) {
922 /*set internal freq to 53.125MHz */
Peter Griffin6930f662015-07-30 14:08:52 -0300923 case 16000000:
924 stv0367_writereg(state, R367TER_PLLMDIV, 0x2);
925 stv0367_writereg(state, R367TER_PLLNDIV, 0x1b);
926 stv0367_writereg(state, R367TER_PLLSETUP, 0x18);
927 break;
Igor M. Liplianin17cce932011-01-25 17:02:00 -0300928 case 25000000:
929 stv0367_writereg(state, R367TER_PLLMDIV, 0xa);
930 stv0367_writereg(state, R367TER_PLLNDIV, 0x55);
931 stv0367_writereg(state, R367TER_PLLSETUP, 0x18);
932 break;
933 default:
934 case 27000000:
935 dprintk("FE_STV0367TER_SetCLKgen for 27Mhz\n");
936 stv0367_writereg(state, R367TER_PLLMDIV, 0x1);
937 stv0367_writereg(state, R367TER_PLLNDIV, 0x8);
938 stv0367_writereg(state, R367TER_PLLSETUP, 0x18);
939 break;
940 case 30000000:
941 stv0367_writereg(state, R367TER_PLLMDIV, 0xc);
942 stv0367_writereg(state, R367TER_PLLNDIV, 0x55);
943 stv0367_writereg(state, R367TER_PLLSETUP, 0x18);
944 break;
945 }
946
947 stv0367_writereg(state, R367TER_I2CRPT, 0xa0);
948 stv0367_writereg(state, R367TER_ANACTRL, 0x00);
949
950 /*Set TS1 and TS2 to serial or parallel mode */
951 stv0367ter_set_ts_mode(state, state->config->ts_mode);
952 stv0367ter_set_clk_pol(state, state->config->clk_pol);
953
954 state->chip_id = stv0367_readreg(state, R367TER_ID);
955 ter_state->first_lock = 0;
956 ter_state->unlock_counter = 2;
957
958 return 0;
959}
960
Mauro Carvalho Chehab285d55a2011-12-26 13:03:00 -0300961static int stv0367ter_algo(struct dvb_frontend *fe)
Igor M. Liplianin17cce932011-01-25 17:02:00 -0300962{
Mauro Carvalho Chehab285d55a2011-12-26 13:03:00 -0300963 struct dtv_frontend_properties *p = &fe->dtv_property_cache;
Igor M. Liplianin17cce932011-01-25 17:02:00 -0300964 struct stv0367_state *state = fe->demodulator_priv;
965 struct stv0367ter_state *ter_state = state->ter_state;
966 int offset = 0, tempo = 0;
967 u8 u_var;
Peter Senna Tschudindf1ec022012-06-14 13:58:14 -0300968 u8 /*constell,*/ counter;
Igor M. Liplianin17cce932011-01-25 17:02:00 -0300969 s8 step;
970 s32 timing_offset = 0;
971 u32 trl_nomrate = 0, InternalFreq = 0, temp = 0;
972
973 dprintk("%s:\n", __func__);
974
Mauro Carvalho Chehab285d55a2011-12-26 13:03:00 -0300975 ter_state->frequency = p->frequency;
Igor M. Liplianin17cce932011-01-25 17:02:00 -0300976 ter_state->force = FE_TER_FORCENONE
977 + stv0367_readbits(state, F367TER_FORCE) * 2;
978 ter_state->if_iq_mode = state->config->if_iq_mode;
979 switch (state->config->if_iq_mode) {
980 case FE_TER_NORMAL_IF_TUNER: /* Normal IF mode */
981 dprintk("ALGO: FE_TER_NORMAL_IF_TUNER selected\n");
982 stv0367_writebits(state, F367TER_TUNER_BB, 0);
983 stv0367_writebits(state, F367TER_LONGPATH_IF, 0);
984 stv0367_writebits(state, F367TER_DEMUX_SWAP, 0);
985 break;
986 case FE_TER_LONGPATH_IF_TUNER: /* Long IF mode */
987 dprintk("ALGO: FE_TER_LONGPATH_IF_TUNER selected\n");
988 stv0367_writebits(state, F367TER_TUNER_BB, 0);
989 stv0367_writebits(state, F367TER_LONGPATH_IF, 1);
990 stv0367_writebits(state, F367TER_DEMUX_SWAP, 1);
991 break;
992 case FE_TER_IQ_TUNER: /* IQ mode */
993 dprintk("ALGO: FE_TER_IQ_TUNER selected\n");
994 stv0367_writebits(state, F367TER_TUNER_BB, 1);
995 stv0367_writebits(state, F367TER_PPM_INVSEL, 0);
996 break;
997 default:
998 printk(KERN_ERR "ALGO: wrong TUNER type selected\n");
999 return -EINVAL;
1000 }
1001
1002 usleep_range(5000, 7000);
1003
Mauro Carvalho Chehab285d55a2011-12-26 13:03:00 -03001004 switch (p->inversion) {
Igor M. Liplianin17cce932011-01-25 17:02:00 -03001005 case INVERSION_AUTO:
1006 default:
1007 dprintk("%s: inversion AUTO\n", __func__);
1008 if (ter_state->if_iq_mode == FE_TER_IQ_TUNER)
1009 stv0367_writebits(state, F367TER_IQ_INVERT,
1010 ter_state->sense);
1011 else
1012 stv0367_writebits(state, F367TER_INV_SPECTR,
1013 ter_state->sense);
1014
1015 break;
1016 case INVERSION_ON:
1017 case INVERSION_OFF:
1018 if (ter_state->if_iq_mode == FE_TER_IQ_TUNER)
1019 stv0367_writebits(state, F367TER_IQ_INVERT,
Mauro Carvalho Chehab285d55a2011-12-26 13:03:00 -03001020 p->inversion);
Igor M. Liplianin17cce932011-01-25 17:02:00 -03001021 else
1022 stv0367_writebits(state, F367TER_INV_SPECTR,
Mauro Carvalho Chehab285d55a2011-12-26 13:03:00 -03001023 p->inversion);
Igor M. Liplianin17cce932011-01-25 17:02:00 -03001024
1025 break;
1026 }
1027
1028 if ((ter_state->if_iq_mode != FE_TER_NORMAL_IF_TUNER) &&
1029 (ter_state->pBW != ter_state->bw)) {
1030 stv0367ter_agc_iir_lock_detect_set(state);
1031
1032 /*set fine agc target to 180 for LPIF or IQ mode*/
1033 /* set Q_AGCTarget */
1034 stv0367_writebits(state, F367TER_SEL_IQNTAR, 1);
1035 stv0367_writebits(state, F367TER_AUT_AGC_TARGET_MSB, 0xB);
1036 /*stv0367_writebits(state,AUT_AGC_TARGET_LSB,0x04); */
1037
1038 /* set Q_AGCTarget */
1039 stv0367_writebits(state, F367TER_SEL_IQNTAR, 0);
1040 stv0367_writebits(state, F367TER_AUT_AGC_TARGET_MSB, 0xB);
1041 /*stv0367_writebits(state,AUT_AGC_TARGET_LSB,0x04); */
1042
1043 if (!stv0367_iir_filt_init(state, ter_state->bw,
1044 state->config->xtal))
1045 return -EINVAL;
1046 /*set IIR filter once for 6,7 or 8MHz BW*/
1047 ter_state->pBW = ter_state->bw;
1048
1049 stv0367ter_agc_iir_rst(state);
1050 }
1051
1052 if (ter_state->hierarchy == FE_TER_HIER_LOW_PRIO)
1053 stv0367_writebits(state, F367TER_BDI_LPSEL, 0x01);
1054 else
1055 stv0367_writebits(state, F367TER_BDI_LPSEL, 0x00);
1056
1057 InternalFreq = stv0367ter_get_mclk(state, state->config->xtal) / 1000;
1058 temp = (int)
1059 ((((ter_state->bw * 64 * (1 << 15) * 100)
1060 / (InternalFreq)) * 10) / 7);
1061
1062 stv0367_writebits(state, F367TER_TRL_NOMRATE_LSB, temp % 2);
1063 temp = temp / 2;
1064 stv0367_writebits(state, F367TER_TRL_NOMRATE_HI, temp / 256);
1065 stv0367_writebits(state, F367TER_TRL_NOMRATE_LO, temp % 256);
1066
1067 temp = stv0367_readbits(state, F367TER_TRL_NOMRATE_HI) * 512 +
1068 stv0367_readbits(state, F367TER_TRL_NOMRATE_LO) * 2 +
1069 stv0367_readbits(state, F367TER_TRL_NOMRATE_LSB);
1070 temp = (int)(((1 << 17) * ter_state->bw * 1000) / (7 * (InternalFreq)));
1071 stv0367_writebits(state, F367TER_GAIN_SRC_HI, temp / 256);
1072 stv0367_writebits(state, F367TER_GAIN_SRC_LO, temp % 256);
1073 temp = stv0367_readbits(state, F367TER_GAIN_SRC_HI) * 256 +
1074 stv0367_readbits(state, F367TER_GAIN_SRC_LO);
1075
1076 temp = (int)
1077 ((InternalFreq - state->config->if_khz) * (1 << 16)
1078 / (InternalFreq));
1079
1080 dprintk("DEROT temp=0x%x\n", temp);
1081 stv0367_writebits(state, F367TER_INC_DEROT_HI, temp / 256);
1082 stv0367_writebits(state, F367TER_INC_DEROT_LO, temp % 256);
1083
1084 ter_state->echo_pos = 0;
1085 ter_state->ucblocks = 0; /* liplianin */
1086 ter_state->pBER = 0; /* liplianin */
1087 stv0367_writebits(state, F367TER_LONG_ECHO, ter_state->echo_pos);
1088
1089 if (stv0367ter_lock_algo(state) != FE_TER_LOCKOK)
1090 return 0;
1091
1092 ter_state->state = FE_TER_LOCKOK;
Igor M. Liplianin17cce932011-01-25 17:02:00 -03001093
1094 ter_state->mode = stv0367_readbits(state, F367TER_SYR_MODE);
1095 ter_state->guard = stv0367_readbits(state, F367TER_SYR_GUARD);
1096
1097 ter_state->first_lock = 1; /* we know sense now :) */
1098
1099 ter_state->agc_val =
1100 (stv0367_readbits(state, F367TER_AGC1_VAL_LO) << 16) +
1101 (stv0367_readbits(state, F367TER_AGC1_VAL_HI) << 24) +
1102 stv0367_readbits(state, F367TER_AGC2_VAL_LO) +
1103 (stv0367_readbits(state, F367TER_AGC2_VAL_HI) << 8);
1104
1105 /* Carrier offset calculation */
1106 stv0367_writebits(state, F367TER_FREEZE, 1);
1107 offset = (stv0367_readbits(state, F367TER_CRL_FOFFSET_VHI) << 16) ;
1108 offset += (stv0367_readbits(state, F367TER_CRL_FOFFSET_HI) << 8);
1109 offset += (stv0367_readbits(state, F367TER_CRL_FOFFSET_LO));
1110 stv0367_writebits(state, F367TER_FREEZE, 0);
1111 if (offset > 8388607)
1112 offset -= 16777216;
1113
1114 offset = offset * 2 / 16384;
1115
1116 if (ter_state->mode == FE_TER_MODE_2K)
1117 offset = (offset * 4464) / 1000;/*** 1 FFT BIN=4.464khz***/
1118 else if (ter_state->mode == FE_TER_MODE_4K)
1119 offset = (offset * 223) / 100;/*** 1 FFT BIN=2.23khz***/
1120 else if (ter_state->mode == FE_TER_MODE_8K)
1121 offset = (offset * 111) / 100;/*** 1 FFT BIN=1.1khz***/
1122
1123 if (stv0367_readbits(state, F367TER_PPM_INVSEL) == 1) {
1124 if ((stv0367_readbits(state, F367TER_INV_SPECTR) ==
1125 (stv0367_readbits(state,
1126 F367TER_STATUS_INV_SPECRUM) == 1)))
1127 offset = offset * -1;
1128 }
1129
1130 if (ter_state->bw == 6)
1131 offset = (offset * 6) / 8;
1132 else if (ter_state->bw == 7)
1133 offset = (offset * 7) / 8;
1134
1135 ter_state->frequency += offset;
1136
1137 tempo = 10; /* exit even if timing_offset stays null */
1138 while ((timing_offset == 0) && (tempo > 0)) {
1139 usleep_range(10000, 20000); /*was 20ms */
1140 /* fine tuning of timing offset if required */
1141 timing_offset = stv0367_readbits(state, F367TER_TRL_TOFFSET_LO)
1142 + 256 * stv0367_readbits(state,
1143 F367TER_TRL_TOFFSET_HI);
1144 if (timing_offset >= 32768)
1145 timing_offset -= 65536;
1146 trl_nomrate = (512 * stv0367_readbits(state,
1147 F367TER_TRL_NOMRATE_HI)
1148 + stv0367_readbits(state, F367TER_TRL_NOMRATE_LO) * 2
1149 + stv0367_readbits(state, F367TER_TRL_NOMRATE_LSB));
1150
1151 timing_offset = ((signed)(1000000 / trl_nomrate) *
1152 timing_offset) / 2048;
1153 tempo--;
1154 }
1155
1156 if (timing_offset <= 0) {
1157 timing_offset = (timing_offset - 11) / 22;
1158 step = -1;
1159 } else {
1160 timing_offset = (timing_offset + 11) / 22;
1161 step = 1;
1162 }
1163
1164 for (counter = 0; counter < abs(timing_offset); counter++) {
1165 trl_nomrate += step;
1166 stv0367_writebits(state, F367TER_TRL_NOMRATE_LSB,
1167 trl_nomrate % 2);
1168 stv0367_writebits(state, F367TER_TRL_NOMRATE_LO,
1169 trl_nomrate / 2);
1170 usleep_range(1000, 2000);
1171 }
1172
1173 usleep_range(5000, 6000);
1174 /* unlocks could happen in case of trl centring big step,
1175 then a core off/on restarts demod */
1176 u_var = stv0367_readbits(state, F367TER_LK);
1177
1178 if (!u_var) {
1179 stv0367_writebits(state, F367TER_CORE_ACTIVE, 0);
1180 msleep(20);
1181 stv0367_writebits(state, F367TER_CORE_ACTIVE, 1);
1182 }
1183
1184 return 0;
1185}
1186
Mauro Carvalho Chehab285d55a2011-12-26 13:03:00 -03001187static int stv0367ter_set_frontend(struct dvb_frontend *fe)
Igor M. Liplianin17cce932011-01-25 17:02:00 -03001188{
Mauro Carvalho Chehab285d55a2011-12-26 13:03:00 -03001189 struct dtv_frontend_properties *p = &fe->dtv_property_cache;
Igor M. Liplianin17cce932011-01-25 17:02:00 -03001190 struct stv0367_state *state = fe->demodulator_priv;
1191 struct stv0367ter_state *ter_state = state->ter_state;
1192
1193 /*u8 trials[2]; */
1194 s8 num_trials, index;
1195 u8 SenseTrials[] = { INVERSION_ON, INVERSION_OFF };
1196
1197 stv0367ter_init(fe);
1198
1199 if (fe->ops.tuner_ops.set_params) {
Daniel Schellerf61c2992017-03-29 13:43:01 -03001200 if (state->use_i2c_gatectrl && fe->ops.i2c_gate_ctrl)
Igor M. Liplianin17cce932011-01-25 17:02:00 -03001201 fe->ops.i2c_gate_ctrl(fe, 1);
Mauro Carvalho Chehab14d24d12011-12-24 12:24:33 -03001202 fe->ops.tuner_ops.set_params(fe);
Daniel Schellerf61c2992017-03-29 13:43:01 -03001203 if (state->use_i2c_gatectrl && fe->ops.i2c_gate_ctrl)
Igor M. Liplianin17cce932011-01-25 17:02:00 -03001204 fe->ops.i2c_gate_ctrl(fe, 0);
1205 }
1206
Mauro Carvalho Chehab285d55a2011-12-26 13:03:00 -03001207 switch (p->transmission_mode) {
Igor M. Liplianin17cce932011-01-25 17:02:00 -03001208 default:
1209 case TRANSMISSION_MODE_AUTO:
1210 case TRANSMISSION_MODE_2K:
1211 ter_state->mode = FE_TER_MODE_2K;
1212 break;
1213/* case TRANSMISSION_MODE_4K:
1214 pLook.mode = FE_TER_MODE_4K;
1215 break;*/
1216 case TRANSMISSION_MODE_8K:
1217 ter_state->mode = FE_TER_MODE_8K;
1218 break;
1219 }
1220
Mauro Carvalho Chehab285d55a2011-12-26 13:03:00 -03001221 switch (p->guard_interval) {
Igor M. Liplianin17cce932011-01-25 17:02:00 -03001222 default:
1223 case GUARD_INTERVAL_1_32:
1224 case GUARD_INTERVAL_1_16:
1225 case GUARD_INTERVAL_1_8:
1226 case GUARD_INTERVAL_1_4:
Mauro Carvalho Chehab285d55a2011-12-26 13:03:00 -03001227 ter_state->guard = p->guard_interval;
Igor M. Liplianin17cce932011-01-25 17:02:00 -03001228 break;
1229 case GUARD_INTERVAL_AUTO:
1230 ter_state->guard = GUARD_INTERVAL_1_32;
1231 break;
1232 }
1233
Mauro Carvalho Chehab285d55a2011-12-26 13:03:00 -03001234 switch (p->bandwidth_hz) {
1235 case 6000000:
Igor M. Liplianin17cce932011-01-25 17:02:00 -03001236 ter_state->bw = FE_TER_CHAN_BW_6M;
1237 break;
Mauro Carvalho Chehab285d55a2011-12-26 13:03:00 -03001238 case 7000000:
Igor M. Liplianin17cce932011-01-25 17:02:00 -03001239 ter_state->bw = FE_TER_CHAN_BW_7M;
1240 break;
Mauro Carvalho Chehab285d55a2011-12-26 13:03:00 -03001241 case 8000000:
Igor M. Liplianin17cce932011-01-25 17:02:00 -03001242 default:
1243 ter_state->bw = FE_TER_CHAN_BW_8M;
1244 }
1245
1246 ter_state->hierarchy = FE_TER_HIER_NONE;
1247
Mauro Carvalho Chehab285d55a2011-12-26 13:03:00 -03001248 switch (p->inversion) {
Igor M. Liplianin17cce932011-01-25 17:02:00 -03001249 case INVERSION_OFF:
1250 case INVERSION_ON:
1251 num_trials = 1;
1252 break;
1253 default:
1254 num_trials = 2;
1255 if (ter_state->first_lock)
1256 num_trials = 1;
1257 break;
1258 }
1259
1260 ter_state->state = FE_TER_NOLOCK;
1261 index = 0;
1262
1263 while (((index) < num_trials) && (ter_state->state != FE_TER_LOCKOK)) {
1264 if (!ter_state->first_lock) {
Mauro Carvalho Chehab285d55a2011-12-26 13:03:00 -03001265 if (p->inversion == INVERSION_AUTO)
Igor M. Liplianin17cce932011-01-25 17:02:00 -03001266 ter_state->sense = SenseTrials[index];
1267
1268 }
Mauro Carvalho Chehab285d55a2011-12-26 13:03:00 -03001269 stv0367ter_algo(fe);
Igor M. Liplianin17cce932011-01-25 17:02:00 -03001270
1271 if ((ter_state->state == FE_TER_LOCKOK) &&
Mauro Carvalho Chehab285d55a2011-12-26 13:03:00 -03001272 (p->inversion == INVERSION_AUTO) &&
Igor M. Liplianin17cce932011-01-25 17:02:00 -03001273 (index == 1)) {
1274 /* invert spectrum sense */
1275 SenseTrials[index] = SenseTrials[0];
1276 SenseTrials[(index + 1) % 2] = (SenseTrials[1] + 1) % 2;
1277 }
1278
1279 index++;
1280 }
1281
1282 return 0;
1283}
1284
1285static int stv0367ter_read_ucblocks(struct dvb_frontend *fe, u32 *ucblocks)
1286{
1287 struct stv0367_state *state = fe->demodulator_priv;
1288 struct stv0367ter_state *ter_state = state->ter_state;
1289 u32 errs = 0;
1290
1291 /*wait for counting completion*/
1292 if (stv0367_readbits(state, F367TER_SFERRC_OLDVALUE) == 0) {
1293 errs =
1294 ((u32)stv0367_readbits(state, F367TER_ERR_CNT1)
1295 * (1 << 16))
1296 + ((u32)stv0367_readbits(state, F367TER_ERR_CNT1_HI)
1297 * (1 << 8))
1298 + ((u32)stv0367_readbits(state, F367TER_ERR_CNT1_LO));
1299 ter_state->ucblocks = errs;
1300 }
1301
1302 (*ucblocks) = ter_state->ucblocks;
1303
1304 return 0;
1305}
1306
Mauro Carvalho Chehab7e3e68b2016-02-04 12:58:30 -02001307static int stv0367ter_get_frontend(struct dvb_frontend *fe,
1308 struct dtv_frontend_properties *p)
Igor M. Liplianin17cce932011-01-25 17:02:00 -03001309{
1310 struct stv0367_state *state = fe->demodulator_priv;
1311 struct stv0367ter_state *ter_state = state->ter_state;
Igor M. Liplianin17cce932011-01-25 17:02:00 -03001312 enum stv0367_ter_mode mode;
1313 int constell = 0,/* snr = 0,*/ Data = 0;
1314
Mauro Carvalho Chehab285d55a2011-12-26 13:03:00 -03001315 p->frequency = stv0367_get_tuner_freq(fe);
1316 if ((int)p->frequency < 0)
1317 p->frequency = -p->frequency;
Igor M. Liplianin17cce932011-01-25 17:02:00 -03001318
1319 constell = stv0367_readbits(state, F367TER_TPS_CONST);
1320 if (constell == 0)
Mauro Carvalho Chehab285d55a2011-12-26 13:03:00 -03001321 p->modulation = QPSK;
Igor M. Liplianin17cce932011-01-25 17:02:00 -03001322 else if (constell == 1)
Mauro Carvalho Chehab285d55a2011-12-26 13:03:00 -03001323 p->modulation = QAM_16;
Igor M. Liplianin17cce932011-01-25 17:02:00 -03001324 else
Mauro Carvalho Chehab285d55a2011-12-26 13:03:00 -03001325 p->modulation = QAM_64;
Igor M. Liplianin17cce932011-01-25 17:02:00 -03001326
Mauro Carvalho Chehab285d55a2011-12-26 13:03:00 -03001327 p->inversion = stv0367_readbits(state, F367TER_INV_SPECTR);
Igor M. Liplianin17cce932011-01-25 17:02:00 -03001328
1329 /* Get the Hierarchical mode */
1330 Data = stv0367_readbits(state, F367TER_TPS_HIERMODE);
1331
1332 switch (Data) {
1333 case 0:
Mauro Carvalho Chehab285d55a2011-12-26 13:03:00 -03001334 p->hierarchy = HIERARCHY_NONE;
Igor M. Liplianin17cce932011-01-25 17:02:00 -03001335 break;
1336 case 1:
Mauro Carvalho Chehab285d55a2011-12-26 13:03:00 -03001337 p->hierarchy = HIERARCHY_1;
Igor M. Liplianin17cce932011-01-25 17:02:00 -03001338 break;
1339 case 2:
Mauro Carvalho Chehab285d55a2011-12-26 13:03:00 -03001340 p->hierarchy = HIERARCHY_2;
Igor M. Liplianin17cce932011-01-25 17:02:00 -03001341 break;
1342 case 3:
Mauro Carvalho Chehab285d55a2011-12-26 13:03:00 -03001343 p->hierarchy = HIERARCHY_4;
Igor M. Liplianin17cce932011-01-25 17:02:00 -03001344 break;
1345 default:
Mauro Carvalho Chehab285d55a2011-12-26 13:03:00 -03001346 p->hierarchy = HIERARCHY_AUTO;
Igor M. Liplianin17cce932011-01-25 17:02:00 -03001347 break; /* error */
1348 }
1349
1350 /* Get the FEC Rate */
1351 if (ter_state->hierarchy == FE_TER_HIER_LOW_PRIO)
1352 Data = stv0367_readbits(state, F367TER_TPS_LPCODE);
1353 else
1354 Data = stv0367_readbits(state, F367TER_TPS_HPCODE);
1355
1356 switch (Data) {
1357 case 0:
Mauro Carvalho Chehab285d55a2011-12-26 13:03:00 -03001358 p->code_rate_HP = FEC_1_2;
Igor M. Liplianin17cce932011-01-25 17:02:00 -03001359 break;
1360 case 1:
Mauro Carvalho Chehab285d55a2011-12-26 13:03:00 -03001361 p->code_rate_HP = FEC_2_3;
Igor M. Liplianin17cce932011-01-25 17:02:00 -03001362 break;
1363 case 2:
Mauro Carvalho Chehab285d55a2011-12-26 13:03:00 -03001364 p->code_rate_HP = FEC_3_4;
Igor M. Liplianin17cce932011-01-25 17:02:00 -03001365 break;
1366 case 3:
Mauro Carvalho Chehab285d55a2011-12-26 13:03:00 -03001367 p->code_rate_HP = FEC_5_6;
Igor M. Liplianin17cce932011-01-25 17:02:00 -03001368 break;
1369 case 4:
Mauro Carvalho Chehab285d55a2011-12-26 13:03:00 -03001370 p->code_rate_HP = FEC_7_8;
Igor M. Liplianin17cce932011-01-25 17:02:00 -03001371 break;
1372 default:
Mauro Carvalho Chehab285d55a2011-12-26 13:03:00 -03001373 p->code_rate_HP = FEC_AUTO;
Igor M. Liplianin17cce932011-01-25 17:02:00 -03001374 break; /* error */
1375 }
1376
1377 mode = stv0367_readbits(state, F367TER_SYR_MODE);
1378
1379 switch (mode) {
1380 case FE_TER_MODE_2K:
Mauro Carvalho Chehab285d55a2011-12-26 13:03:00 -03001381 p->transmission_mode = TRANSMISSION_MODE_2K;
Igor M. Liplianin17cce932011-01-25 17:02:00 -03001382 break;
1383/* case FE_TER_MODE_4K:
Mauro Carvalho Chehab285d55a2011-12-26 13:03:00 -03001384 p->transmission_mode = TRANSMISSION_MODE_4K;
Igor M. Liplianin17cce932011-01-25 17:02:00 -03001385 break;*/
1386 case FE_TER_MODE_8K:
Mauro Carvalho Chehab285d55a2011-12-26 13:03:00 -03001387 p->transmission_mode = TRANSMISSION_MODE_8K;
Igor M. Liplianin17cce932011-01-25 17:02:00 -03001388 break;
1389 default:
Mauro Carvalho Chehab285d55a2011-12-26 13:03:00 -03001390 p->transmission_mode = TRANSMISSION_MODE_AUTO;
Igor M. Liplianin17cce932011-01-25 17:02:00 -03001391 }
1392
Mauro Carvalho Chehab285d55a2011-12-26 13:03:00 -03001393 p->guard_interval = stv0367_readbits(state, F367TER_SYR_GUARD);
Igor M. Liplianin17cce932011-01-25 17:02:00 -03001394
Mauro Carvalho Chehab7c995072014-09-03 15:10:25 -03001395 return 0;
Igor M. Liplianin17cce932011-01-25 17:02:00 -03001396}
1397
1398static int stv0367ter_read_snr(struct dvb_frontend *fe, u16 *snr)
1399{
1400 struct stv0367_state *state = fe->demodulator_priv;
1401 u32 snru32 = 0;
1402 int cpt = 0;
1403 u8 cut = stv0367_readbits(state, F367TER_IDENTIFICATIONREG);
1404
1405 while (cpt < 10) {
1406 usleep_range(2000, 3000);
1407 if (cut == 0x50) /*cut 1.0 cut 1.1*/
1408 snru32 += stv0367_readbits(state, F367TER_CHCSNR) / 4;
1409 else /*cu2.0*/
1410 snru32 += 125 * stv0367_readbits(state, F367TER_CHCSNR);
1411
1412 cpt++;
1413 }
1414
1415 snru32 /= 10;/*average on 10 values*/
1416
1417 *snr = snru32 / 1000;
1418
1419 return 0;
1420}
1421
1422#if 0
1423static int stv0367ter_status(struct dvb_frontend *fe)
1424{
1425
1426 struct stv0367_state *state = fe->demodulator_priv;
1427 struct stv0367ter_state *ter_state = state->ter_state;
1428 int locked = FALSE;
1429
1430 locked = (stv0367_readbits(state, F367TER_LK));
1431 if (!locked)
1432 ter_state->unlock_counter += 1;
1433 else
1434 ter_state->unlock_counter = 0;
1435
1436 if (ter_state->unlock_counter > 2) {
1437 if (!stv0367_readbits(state, F367TER_TPS_LOCK) ||
1438 (!stv0367_readbits(state, F367TER_LK))) {
1439 stv0367_writebits(state, F367TER_CORE_ACTIVE, 0);
1440 usleep_range(2000, 3000);
1441 stv0367_writebits(state, F367TER_CORE_ACTIVE, 1);
1442 msleep(350);
1443 locked = (stv0367_readbits(state, F367TER_TPS_LOCK)) &&
1444 (stv0367_readbits(state, F367TER_LK));
1445 }
1446
1447 }
1448
1449 return locked;
1450}
1451#endif
Mauro Carvalho Chehab0df289a2015-06-07 14:53:52 -03001452static int stv0367ter_read_status(struct dvb_frontend *fe,
1453 enum fe_status *status)
Igor M. Liplianin17cce932011-01-25 17:02:00 -03001454{
1455 struct stv0367_state *state = fe->demodulator_priv;
1456
1457 dprintk("%s:\n", __func__);
1458
1459 *status = 0;
1460
1461 if (stv0367_readbits(state, F367TER_LK)) {
1462 *status |= FE_HAS_LOCK;
1463 dprintk("%s: stv0367 has locked\n", __func__);
1464 }
1465
1466 return 0;
1467}
1468
1469static int stv0367ter_read_ber(struct dvb_frontend *fe, u32 *ber)
1470{
1471 struct stv0367_state *state = fe->demodulator_priv;
1472 struct stv0367ter_state *ter_state = state->ter_state;
1473 u32 Errors = 0, tber = 0, temporary = 0;
1474 int abc = 0, def = 0;
1475
1476
1477 /*wait for counting completion*/
1478 if (stv0367_readbits(state, F367TER_SFERRC_OLDVALUE) == 0)
1479 Errors = ((u32)stv0367_readbits(state, F367TER_SFEC_ERR_CNT)
1480 * (1 << 16))
1481 + ((u32)stv0367_readbits(state, F367TER_SFEC_ERR_CNT_HI)
1482 * (1 << 8))
1483 + ((u32)stv0367_readbits(state,
1484 F367TER_SFEC_ERR_CNT_LO));
1485 /*measurement not completed, load previous value*/
1486 else {
1487 tber = ter_state->pBER;
1488 return 0;
1489 }
1490
1491 abc = stv0367_readbits(state, F367TER_SFEC_ERR_SOURCE);
1492 def = stv0367_readbits(state, F367TER_SFEC_NUM_EVENT);
1493
1494 if (Errors == 0) {
1495 tber = 0;
1496 } else if (abc == 0x7) {
1497 if (Errors <= 4) {
1498 temporary = (Errors * 1000000000) / (8 * (1 << 14));
1499 temporary = temporary;
1500 } else if (Errors <= 42) {
1501 temporary = (Errors * 100000000) / (8 * (1 << 14));
1502 temporary = temporary * 10;
1503 } else if (Errors <= 429) {
1504 temporary = (Errors * 10000000) / (8 * (1 << 14));
1505 temporary = temporary * 100;
1506 } else if (Errors <= 4294) {
1507 temporary = (Errors * 1000000) / (8 * (1 << 14));
1508 temporary = temporary * 1000;
1509 } else if (Errors <= 42949) {
1510 temporary = (Errors * 100000) / (8 * (1 << 14));
1511 temporary = temporary * 10000;
1512 } else if (Errors <= 429496) {
1513 temporary = (Errors * 10000) / (8 * (1 << 14));
1514 temporary = temporary * 100000;
1515 } else { /*if (Errors<4294967) 2^22 max error*/
1516 temporary = (Errors * 1000) / (8 * (1 << 14));
1517 temporary = temporary * 100000; /* still to *10 */
1518 }
1519
1520 /* Byte error*/
1521 if (def == 2)
1522 /*tber=Errors/(8*(1 <<14));*/
1523 tber = temporary;
1524 else if (def == 3)
1525 /*tber=Errors/(8*(1 <<16));*/
1526 tber = temporary / 4;
1527 else if (def == 4)
1528 /*tber=Errors/(8*(1 <<18));*/
1529 tber = temporary / 16;
1530 else if (def == 5)
1531 /*tber=Errors/(8*(1 <<20));*/
1532 tber = temporary / 64;
1533 else if (def == 6)
1534 /*tber=Errors/(8*(1 <<22));*/
1535 tber = temporary / 256;
1536 else
1537 /* should not pass here*/
1538 tber = 0;
1539
1540 if ((Errors < 4294967) && (Errors > 429496))
1541 tber *= 10;
1542
1543 }
1544
1545 /* save actual value */
1546 ter_state->pBER = tber;
1547
1548 (*ber) = tber;
1549
1550 return 0;
1551}
1552#if 0
1553static u32 stv0367ter_get_per(struct stv0367_state *state)
1554{
1555 struct stv0367ter_state *ter_state = state->ter_state;
1556 u32 Errors = 0, Per = 0, temporary = 0;
1557 int abc = 0, def = 0, cpt = 0;
1558
1559 while (((stv0367_readbits(state, F367TER_SFERRC_OLDVALUE) == 1) &&
1560 (cpt < 400)) || ((Errors == 0) && (cpt < 400))) {
1561 usleep_range(1000, 2000);
1562 Errors = ((u32)stv0367_readbits(state, F367TER_ERR_CNT1)
1563 * (1 << 16))
1564 + ((u32)stv0367_readbits(state, F367TER_ERR_CNT1_HI)
1565 * (1 << 8))
1566 + ((u32)stv0367_readbits(state, F367TER_ERR_CNT1_LO));
1567 cpt++;
1568 }
1569 abc = stv0367_readbits(state, F367TER_ERR_SRC1);
1570 def = stv0367_readbits(state, F367TER_NUM_EVT1);
1571
1572 if (Errors == 0)
1573 Per = 0;
1574 else if (abc == 0x9) {
1575 if (Errors <= 4) {
1576 temporary = (Errors * 1000000000) / (8 * (1 << 8));
1577 temporary = temporary;
1578 } else if (Errors <= 42) {
1579 temporary = (Errors * 100000000) / (8 * (1 << 8));
1580 temporary = temporary * 10;
1581 } else if (Errors <= 429) {
1582 temporary = (Errors * 10000000) / (8 * (1 << 8));
1583 temporary = temporary * 100;
1584 } else if (Errors <= 4294) {
1585 temporary = (Errors * 1000000) / (8 * (1 << 8));
1586 temporary = temporary * 1000;
1587 } else if (Errors <= 42949) {
1588 temporary = (Errors * 100000) / (8 * (1 << 8));
1589 temporary = temporary * 10000;
1590 } else { /*if(Errors<=429496) 2^16 errors max*/
1591 temporary = (Errors * 10000) / (8 * (1 << 8));
1592 temporary = temporary * 100000;
1593 }
1594
1595 /* pkt error*/
1596 if (def == 2)
1597 /*Per=Errors/(1 << 8);*/
1598 Per = temporary;
1599 else if (def == 3)
1600 /*Per=Errors/(1 << 10);*/
1601 Per = temporary / 4;
1602 else if (def == 4)
1603 /*Per=Errors/(1 << 12);*/
1604 Per = temporary / 16;
1605 else if (def == 5)
1606 /*Per=Errors/(1 << 14);*/
1607 Per = temporary / 64;
1608 else if (def == 6)
1609 /*Per=Errors/(1 << 16);*/
1610 Per = temporary / 256;
1611 else
1612 Per = 0;
1613
1614 }
1615 /* save actual value */
1616 ter_state->pPER = Per;
1617
1618 return Per;
1619}
1620#endif
1621static int stv0367_get_tune_settings(struct dvb_frontend *fe,
1622 struct dvb_frontend_tune_settings
1623 *fe_tune_settings)
1624{
1625 fe_tune_settings->min_delay_ms = 1000;
1626 fe_tune_settings->step_size = 0;
1627 fe_tune_settings->max_drift = 0;
1628
1629 return 0;
1630}
1631
1632static void stv0367_release(struct dvb_frontend *fe)
1633{
1634 struct stv0367_state *state = fe->demodulator_priv;
1635
1636 kfree(state->ter_state);
1637 kfree(state->cab_state);
1638 kfree(state);
1639}
1640
Max Kellermannbd336e62016-08-09 18:32:21 -03001641static const struct dvb_frontend_ops stv0367ter_ops = {
Mauro Carvalho Chehab285d55a2011-12-26 13:03:00 -03001642 .delsys = { SYS_DVBT },
Igor M. Liplianin17cce932011-01-25 17:02:00 -03001643 .info = {
1644 .name = "ST STV0367 DVB-T",
Igor M. Liplianin17cce932011-01-25 17:02:00 -03001645 .frequency_min = 47000000,
1646 .frequency_max = 862000000,
1647 .frequency_stepsize = 15625,
1648 .frequency_tolerance = 0,
1649 .caps = FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 |
1650 FE_CAN_FEC_3_4 | FE_CAN_FEC_5_6 | FE_CAN_FEC_7_8 |
1651 FE_CAN_FEC_AUTO |
1652 FE_CAN_QPSK | FE_CAN_QAM_16 | FE_CAN_QAM_64 |
1653 FE_CAN_QAM_128 | FE_CAN_QAM_256 | FE_CAN_QAM_AUTO |
1654 FE_CAN_TRANSMISSION_MODE_AUTO | FE_CAN_RECOVER |
1655 FE_CAN_INVERSION_AUTO |
1656 FE_CAN_MUTE_TS
1657 },
1658 .release = stv0367_release,
1659 .init = stv0367ter_init,
1660 .sleep = stv0367ter_sleep,
1661 .i2c_gate_ctrl = stv0367ter_gate_ctrl,
Mauro Carvalho Chehab285d55a2011-12-26 13:03:00 -03001662 .set_frontend = stv0367ter_set_frontend,
1663 .get_frontend = stv0367ter_get_frontend,
Igor M. Liplianin17cce932011-01-25 17:02:00 -03001664 .get_tune_settings = stv0367_get_tune_settings,
1665 .read_status = stv0367ter_read_status,
1666 .read_ber = stv0367ter_read_ber,/* too slow */
1667/* .read_signal_strength = stv0367_read_signal_strength,*/
1668 .read_snr = stv0367ter_read_snr,
1669 .read_ucblocks = stv0367ter_read_ucblocks,
1670};
1671
1672struct dvb_frontend *stv0367ter_attach(const struct stv0367_config *config,
1673 struct i2c_adapter *i2c)
1674{
1675 struct stv0367_state *state = NULL;
1676 struct stv0367ter_state *ter_state = NULL;
1677
1678 /* allocate memory for the internal state */
1679 state = kzalloc(sizeof(struct stv0367_state), GFP_KERNEL);
1680 if (state == NULL)
1681 goto error;
1682 ter_state = kzalloc(sizeof(struct stv0367ter_state), GFP_KERNEL);
1683 if (ter_state == NULL)
1684 goto error;
1685
1686 /* setup the state */
1687 state->i2c = i2c;
1688 state->config = config;
1689 state->ter_state = ter_state;
1690 state->fe.ops = stv0367ter_ops;
1691 state->fe.demodulator_priv = state;
1692 state->chip_id = stv0367_readreg(state, 0xf000);
1693
Daniel Schellerf61c2992017-03-29 13:43:01 -03001694 /* demod operation options */
1695 state->use_i2c_gatectrl = 1;
Daniel Scheller8881ceb2017-03-29 13:43:04 -03001696 state->deftabs = STV0367_DEFTAB_GENERIC;
Daniel Schellerf61c2992017-03-29 13:43:01 -03001697
Igor M. Liplianin17cce932011-01-25 17:02:00 -03001698 dprintk("%s: chip_id = 0x%x\n", __func__, state->chip_id);
1699
1700 /* check if the demod is there */
1701 if ((state->chip_id != 0x50) && (state->chip_id != 0x60))
1702 goto error;
1703
1704 return &state->fe;
1705
1706error:
1707 kfree(ter_state);
1708 kfree(state);
1709 return NULL;
1710}
1711EXPORT_SYMBOL(stv0367ter_attach);
1712
1713static int stv0367cab_gate_ctrl(struct dvb_frontend *fe, int enable)
1714{
1715 struct stv0367_state *state = fe->demodulator_priv;
1716
1717 dprintk("%s:\n", __func__);
1718
1719 stv0367_writebits(state, F367CAB_I2CT_ON, (enable > 0) ? 1 : 0);
1720
1721 return 0;
1722}
1723
1724static u32 stv0367cab_get_mclk(struct dvb_frontend *fe, u32 ExtClk_Hz)
1725{
1726 struct stv0367_state *state = fe->demodulator_priv;
1727 u32 mclk_Hz = 0;/* master clock frequency (Hz) */
1728 u32 M, N, P;
1729
1730
1731 if (stv0367_readbits(state, F367CAB_BYPASS_PLLXN) == 0) {
1732 N = (u32)stv0367_readbits(state, F367CAB_PLL_NDIV);
1733 if (N == 0)
1734 N = N + 1;
1735
1736 M = (u32)stv0367_readbits(state, F367CAB_PLL_MDIV);
1737 if (M == 0)
1738 M = M + 1;
1739
1740 P = (u32)stv0367_readbits(state, F367CAB_PLL_PDIV);
1741
1742 if (P > 5)
1743 P = 5;
1744
1745 mclk_Hz = ((ExtClk_Hz / 2) * N) / (M * (1 << P));
1746 dprintk("stv0367cab_get_mclk BYPASS_PLLXN mclk_Hz=%d\n",
1747 mclk_Hz);
1748 } else
1749 mclk_Hz = ExtClk_Hz;
1750
1751 dprintk("stv0367cab_get_mclk final mclk_Hz=%d\n", mclk_Hz);
1752
1753 return mclk_Hz;
1754}
1755
1756static u32 stv0367cab_get_adc_freq(struct dvb_frontend *fe, u32 ExtClk_Hz)
1757{
1758 u32 ADCClk_Hz = ExtClk_Hz;
1759
1760 ADCClk_Hz = stv0367cab_get_mclk(fe, ExtClk_Hz);
1761
1762 return ADCClk_Hz;
1763}
1764
Mauro Carvalho Chehab8c8ca1c2012-10-27 11:26:42 -03001765static enum stv0367cab_mod stv0367cab_SetQamSize(struct stv0367_state *state,
1766 u32 SymbolRate,
1767 enum stv0367cab_mod QAMSize)
Igor M. Liplianin17cce932011-01-25 17:02:00 -03001768{
1769 /* Set QAM size */
1770 stv0367_writebits(state, F367CAB_QAM_MODE, QAMSize);
1771
1772 /* Set Registers settings specific to the QAM size */
1773 switch (QAMSize) {
1774 case FE_CAB_MOD_QAM4:
1775 stv0367_writereg(state, R367CAB_IQDEM_ADJ_AGC_REF, 0x00);
1776 break;
1777 case FE_CAB_MOD_QAM16:
1778 stv0367_writereg(state, R367CAB_AGC_PWR_REF_L, 0x64);
1779 stv0367_writereg(state, R367CAB_IQDEM_ADJ_AGC_REF, 0x00);
1780 stv0367_writereg(state, R367CAB_FSM_STATE, 0x90);
1781 stv0367_writereg(state, R367CAB_EQU_CTR_LPF_GAIN, 0xc1);
1782 stv0367_writereg(state, R367CAB_EQU_CRL_LPF_GAIN, 0xa7);
1783 stv0367_writereg(state, R367CAB_EQU_CRL_LD_SEN, 0x95);
1784 stv0367_writereg(state, R367CAB_EQU_CRL_LIMITER, 0x40);
1785 stv0367_writereg(state, R367CAB_EQU_PNT_GAIN, 0x8a);
1786 break;
1787 case FE_CAB_MOD_QAM32:
1788 stv0367_writereg(state, R367CAB_IQDEM_ADJ_AGC_REF, 0x00);
1789 stv0367_writereg(state, R367CAB_AGC_PWR_REF_L, 0x6e);
1790 stv0367_writereg(state, R367CAB_FSM_STATE, 0xb0);
1791 stv0367_writereg(state, R367CAB_EQU_CTR_LPF_GAIN, 0xc1);
1792 stv0367_writereg(state, R367CAB_EQU_CRL_LPF_GAIN, 0xb7);
1793 stv0367_writereg(state, R367CAB_EQU_CRL_LD_SEN, 0x9d);
1794 stv0367_writereg(state, R367CAB_EQU_CRL_LIMITER, 0x7f);
1795 stv0367_writereg(state, R367CAB_EQU_PNT_GAIN, 0xa7);
1796 break;
1797 case FE_CAB_MOD_QAM64:
1798 stv0367_writereg(state, R367CAB_IQDEM_ADJ_AGC_REF, 0x82);
1799 stv0367_writereg(state, R367CAB_AGC_PWR_REF_L, 0x5a);
1800 if (SymbolRate > 45000000) {
1801 stv0367_writereg(state, R367CAB_FSM_STATE, 0xb0);
1802 stv0367_writereg(state, R367CAB_EQU_CTR_LPF_GAIN, 0xc1);
1803 stv0367_writereg(state, R367CAB_EQU_CRL_LPF_GAIN, 0xa5);
1804 } else if (SymbolRate > 25000000) {
1805 stv0367_writereg(state, R367CAB_FSM_STATE, 0xa0);
1806 stv0367_writereg(state, R367CAB_EQU_CTR_LPF_GAIN, 0xc1);
1807 stv0367_writereg(state, R367CAB_EQU_CRL_LPF_GAIN, 0xa6);
1808 } else {
1809 stv0367_writereg(state, R367CAB_FSM_STATE, 0xa0);
1810 stv0367_writereg(state, R367CAB_EQU_CTR_LPF_GAIN, 0xd1);
1811 stv0367_writereg(state, R367CAB_EQU_CRL_LPF_GAIN, 0xa7);
1812 }
1813 stv0367_writereg(state, R367CAB_EQU_CRL_LD_SEN, 0x95);
1814 stv0367_writereg(state, R367CAB_EQU_CRL_LIMITER, 0x40);
1815 stv0367_writereg(state, R367CAB_EQU_PNT_GAIN, 0x99);
1816 break;
1817 case FE_CAB_MOD_QAM128:
1818 stv0367_writereg(state, R367CAB_IQDEM_ADJ_AGC_REF, 0x00);
1819 stv0367_writereg(state, R367CAB_AGC_PWR_REF_L, 0x76);
1820 stv0367_writereg(state, R367CAB_FSM_STATE, 0x90);
1821 stv0367_writereg(state, R367CAB_EQU_CTR_LPF_GAIN, 0xb1);
1822 if (SymbolRate > 45000000)
1823 stv0367_writereg(state, R367CAB_EQU_CRL_LPF_GAIN, 0xa7);
1824 else if (SymbolRate > 25000000)
1825 stv0367_writereg(state, R367CAB_EQU_CRL_LPF_GAIN, 0xa6);
1826 else
1827 stv0367_writereg(state, R367CAB_EQU_CRL_LPF_GAIN, 0x97);
1828
1829 stv0367_writereg(state, R367CAB_EQU_CRL_LD_SEN, 0x8e);
1830 stv0367_writereg(state, R367CAB_EQU_CRL_LIMITER, 0x7f);
1831 stv0367_writereg(state, R367CAB_EQU_PNT_GAIN, 0xa7);
1832 break;
1833 case FE_CAB_MOD_QAM256:
1834 stv0367_writereg(state, R367CAB_IQDEM_ADJ_AGC_REF, 0x94);
1835 stv0367_writereg(state, R367CAB_AGC_PWR_REF_L, 0x5a);
1836 stv0367_writereg(state, R367CAB_FSM_STATE, 0xa0);
1837 if (SymbolRate > 45000000)
1838 stv0367_writereg(state, R367CAB_EQU_CTR_LPF_GAIN, 0xc1);
1839 else if (SymbolRate > 25000000)
1840 stv0367_writereg(state, R367CAB_EQU_CTR_LPF_GAIN, 0xc1);
1841 else
1842 stv0367_writereg(state, R367CAB_EQU_CTR_LPF_GAIN, 0xd1);
1843
1844 stv0367_writereg(state, R367CAB_EQU_CRL_LPF_GAIN, 0xa7);
1845 stv0367_writereg(state, R367CAB_EQU_CRL_LD_SEN, 0x85);
1846 stv0367_writereg(state, R367CAB_EQU_CRL_LIMITER, 0x40);
1847 stv0367_writereg(state, R367CAB_EQU_PNT_GAIN, 0xa7);
1848 break;
1849 case FE_CAB_MOD_QAM512:
1850 stv0367_writereg(state, R367CAB_IQDEM_ADJ_AGC_REF, 0x00);
1851 break;
1852 case FE_CAB_MOD_QAM1024:
1853 stv0367_writereg(state, R367CAB_IQDEM_ADJ_AGC_REF, 0x00);
1854 break;
1855 default:
1856 break;
1857 }
1858
1859 return QAMSize;
1860}
1861
1862static u32 stv0367cab_set_derot_freq(struct stv0367_state *state,
1863 u32 adc_hz, s32 derot_hz)
1864{
1865 u32 sampled_if = 0;
1866 u32 adc_khz;
1867
1868 adc_khz = adc_hz / 1000;
1869
1870 dprintk("%s: adc_hz=%d derot_hz=%d\n", __func__, adc_hz, derot_hz);
1871
1872 if (adc_khz != 0) {
1873 if (derot_hz < 1000000)
1874 derot_hz = adc_hz / 4; /* ZIF operation */
1875 if (derot_hz > adc_hz)
1876 derot_hz = derot_hz - adc_hz;
1877 sampled_if = (u32)derot_hz / 1000;
1878 sampled_if *= 32768;
1879 sampled_if /= adc_khz;
1880 sampled_if *= 256;
1881 }
1882
1883 if (sampled_if > 8388607)
1884 sampled_if = 8388607;
1885
1886 dprintk("%s: sampled_if=0x%x\n", __func__, sampled_if);
1887
1888 stv0367_writereg(state, R367CAB_MIX_NCO_LL, sampled_if);
1889 stv0367_writereg(state, R367CAB_MIX_NCO_HL, (sampled_if >> 8));
1890 stv0367_writebits(state, F367CAB_MIX_NCO_INC_HH, (sampled_if >> 16));
1891
1892 return derot_hz;
1893}
1894
1895static u32 stv0367cab_get_derot_freq(struct stv0367_state *state, u32 adc_hz)
1896{
1897 u32 sampled_if;
1898
1899 sampled_if = stv0367_readbits(state, F367CAB_MIX_NCO_INC_LL) +
1900 (stv0367_readbits(state, F367CAB_MIX_NCO_INC_HL) << 8) +
1901 (stv0367_readbits(state, F367CAB_MIX_NCO_INC_HH) << 16);
1902
1903 sampled_if /= 256;
1904 sampled_if *= (adc_hz / 1000);
1905 sampled_if += 1;
1906 sampled_if /= 32768;
1907
1908 return sampled_if;
1909}
1910
1911static u32 stv0367cab_set_srate(struct stv0367_state *state, u32 adc_hz,
1912 u32 mclk_hz, u32 SymbolRate,
1913 enum stv0367cab_mod QAMSize)
1914{
1915 u32 QamSizeCorr = 0;
1916 u32 u32_tmp = 0, u32_tmp1 = 0;
1917 u32 adp_khz;
1918
1919 dprintk("%s:\n", __func__);
1920
1921 /* Set Correction factor of SRC gain */
1922 switch (QAMSize) {
1923 case FE_CAB_MOD_QAM4:
1924 QamSizeCorr = 1110;
1925 break;
1926 case FE_CAB_MOD_QAM16:
1927 QamSizeCorr = 1032;
1928 break;
1929 case FE_CAB_MOD_QAM32:
1930 QamSizeCorr = 954;
1931 break;
1932 case FE_CAB_MOD_QAM64:
1933 QamSizeCorr = 983;
1934 break;
1935 case FE_CAB_MOD_QAM128:
1936 QamSizeCorr = 957;
1937 break;
1938 case FE_CAB_MOD_QAM256:
1939 QamSizeCorr = 948;
1940 break;
1941 case FE_CAB_MOD_QAM512:
1942 QamSizeCorr = 0;
1943 break;
1944 case FE_CAB_MOD_QAM1024:
1945 QamSizeCorr = 944;
1946 break;
1947 default:
1948 break;
1949 }
1950
1951 /* Transfer ratio calculation */
1952 if (adc_hz != 0) {
1953 u32_tmp = 256 * SymbolRate;
1954 u32_tmp = u32_tmp / adc_hz;
1955 }
1956 stv0367_writereg(state, R367CAB_EQU_CRL_TFR, (u8)u32_tmp);
1957
1958 /* Symbol rate and SRC gain calculation */
1959 adp_khz = (mclk_hz >> 1) / 1000;/* TRL works at half the system clock */
1960 if (adp_khz != 0) {
1961 u32_tmp = SymbolRate;
1962 u32_tmp1 = SymbolRate;
1963
1964 if (u32_tmp < 2097152) { /* 2097152 = 2^21 */
1965 /* Symbol rate calculation */
1966 u32_tmp *= 2048; /* 2048 = 2^11 */
1967 u32_tmp = u32_tmp / adp_khz;
1968 u32_tmp = u32_tmp * 16384; /* 16384 = 2^14 */
1969 u32_tmp /= 125 ; /* 125 = 1000/2^3 */
1970 u32_tmp = u32_tmp * 8; /* 8 = 2^3 */
1971
1972 /* SRC Gain Calculation */
1973 u32_tmp1 *= 2048; /* *2*2^10 */
1974 u32_tmp1 /= 439; /* *2/878 */
1975 u32_tmp1 *= 256; /* *2^8 */
1976 u32_tmp1 = u32_tmp1 / adp_khz; /* /(AdpClk in kHz) */
1977 u32_tmp1 *= QamSizeCorr * 9; /* *1000*corr factor */
1978 u32_tmp1 = u32_tmp1 / 10000000;
1979
1980 } else if (u32_tmp < 4194304) { /* 4194304 = 2**22 */
1981 /* Symbol rate calculation */
1982 u32_tmp *= 1024 ; /* 1024 = 2**10 */
1983 u32_tmp = u32_tmp / adp_khz;
1984 u32_tmp = u32_tmp * 16384; /* 16384 = 2**14 */
1985 u32_tmp /= 125 ; /* 125 = 1000/2**3 */
1986 u32_tmp = u32_tmp * 16; /* 16 = 2**4 */
1987
1988 /* SRC Gain Calculation */
1989 u32_tmp1 *= 1024; /* *2*2^9 */
1990 u32_tmp1 /= 439; /* *2/878 */
1991 u32_tmp1 *= 256; /* *2^8 */
1992 u32_tmp1 = u32_tmp1 / adp_khz; /* /(AdpClk in kHz)*/
1993 u32_tmp1 *= QamSizeCorr * 9; /* *1000*corr factor */
1994 u32_tmp1 = u32_tmp1 / 5000000;
1995 } else if (u32_tmp < 8388607) { /* 8388607 = 2**23 */
1996 /* Symbol rate calculation */
1997 u32_tmp *= 512 ; /* 512 = 2**9 */
1998 u32_tmp = u32_tmp / adp_khz;
1999 u32_tmp = u32_tmp * 16384; /* 16384 = 2**14 */
2000 u32_tmp /= 125 ; /* 125 = 1000/2**3 */
2001 u32_tmp = u32_tmp * 32; /* 32 = 2**5 */
2002
2003 /* SRC Gain Calculation */
2004 u32_tmp1 *= 512; /* *2*2^8 */
2005 u32_tmp1 /= 439; /* *2/878 */
2006 u32_tmp1 *= 256; /* *2^8 */
2007 u32_tmp1 = u32_tmp1 / adp_khz; /* /(AdpClk in kHz) */
2008 u32_tmp1 *= QamSizeCorr * 9; /* *1000*corr factor */
2009 u32_tmp1 = u32_tmp1 / 2500000;
2010 } else {
2011 /* Symbol rate calculation */
2012 u32_tmp *= 256 ; /* 256 = 2**8 */
2013 u32_tmp = u32_tmp / adp_khz;
2014 u32_tmp = u32_tmp * 16384; /* 16384 = 2**13 */
2015 u32_tmp /= 125 ; /* 125 = 1000/2**3 */
2016 u32_tmp = u32_tmp * 64; /* 64 = 2**6 */
2017
2018 /* SRC Gain Calculation */
2019 u32_tmp1 *= 256; /* 2*2^7 */
2020 u32_tmp1 /= 439; /* *2/878 */
2021 u32_tmp1 *= 256; /* *2^8 */
2022 u32_tmp1 = u32_tmp1 / adp_khz; /* /(AdpClk in kHz) */
2023 u32_tmp1 *= QamSizeCorr * 9; /* *1000*corr factor */
2024 u32_tmp1 = u32_tmp1 / 1250000;
2025 }
2026 }
2027#if 0
2028 /* Filters' coefficients are calculated and written
2029 into registers only if the filters are enabled */
2030 if (stv0367_readbits(state, F367CAB_ADJ_EN)) {
2031 stv0367cab_SetIirAdjacentcoefficient(state, mclk_hz,
2032 SymbolRate);
2033 /* AllPass filter must be enabled
2034 when the adjacents filter is used */
2035 stv0367_writebits(state, F367CAB_ALLPASSFILT_EN, 1);
2036 stv0367cab_SetAllPasscoefficient(state, mclk_hz, SymbolRate);
2037 } else
2038 /* AllPass filter must be disabled
2039 when the adjacents filter is not used */
2040#endif
2041 stv0367_writebits(state, F367CAB_ALLPASSFILT_EN, 0);
2042
2043 stv0367_writereg(state, R367CAB_SRC_NCO_LL, u32_tmp);
2044 stv0367_writereg(state, R367CAB_SRC_NCO_LH, (u32_tmp >> 8));
2045 stv0367_writereg(state, R367CAB_SRC_NCO_HL, (u32_tmp >> 16));
2046 stv0367_writereg(state, R367CAB_SRC_NCO_HH, (u32_tmp >> 24));
2047
2048 stv0367_writereg(state, R367CAB_IQDEM_GAIN_SRC_L, u32_tmp1 & 0x00ff);
2049 stv0367_writebits(state, F367CAB_GAIN_SRC_HI, (u32_tmp1 >> 8) & 0x00ff);
2050
2051 return SymbolRate ;
2052}
2053
2054static u32 stv0367cab_GetSymbolRate(struct stv0367_state *state, u32 mclk_hz)
2055{
2056 u32 regsym;
2057 u32 adp_khz;
2058
2059 regsym = stv0367_readreg(state, R367CAB_SRC_NCO_LL) +
2060 (stv0367_readreg(state, R367CAB_SRC_NCO_LH) << 8) +
2061 (stv0367_readreg(state, R367CAB_SRC_NCO_HL) << 16) +
2062 (stv0367_readreg(state, R367CAB_SRC_NCO_HH) << 24);
2063
2064 adp_khz = (mclk_hz >> 1) / 1000;/* TRL works at half the system clock */
2065
2066 if (regsym < 134217728) { /* 134217728L = 2**27*/
2067 regsym = regsym * 32; /* 32 = 2**5 */
2068 regsym = regsym / 32768; /* 32768L = 2**15 */
2069 regsym = adp_khz * regsym; /* AdpClk in kHz */
2070 regsym = regsym / 128; /* 128 = 2**7 */
2071 regsym *= 125 ; /* 125 = 1000/2**3 */
2072 regsym /= 2048 ; /* 2048 = 2**11 */
2073 } else if (regsym < 268435456) { /* 268435456L = 2**28 */
2074 regsym = regsym * 16; /* 16 = 2**4 */
2075 regsym = regsym / 32768; /* 32768L = 2**15 */
2076 regsym = adp_khz * regsym; /* AdpClk in kHz */
2077 regsym = regsym / 128; /* 128 = 2**7 */
2078 regsym *= 125 ; /* 125 = 1000/2**3*/
2079 regsym /= 1024 ; /* 256 = 2**10*/
2080 } else if (regsym < 536870912) { /* 536870912L = 2**29*/
2081 regsym = regsym * 8; /* 8 = 2**3 */
2082 regsym = regsym / 32768; /* 32768L = 2**15 */
2083 regsym = adp_khz * regsym; /* AdpClk in kHz */
2084 regsym = regsym / 128; /* 128 = 2**7 */
2085 regsym *= 125 ; /* 125 = 1000/2**3 */
2086 regsym /= 512 ; /* 128 = 2**9 */
2087 } else {
2088 regsym = regsym * 4; /* 4 = 2**2 */
2089 regsym = regsym / 32768; /* 32768L = 2**15 */
2090 regsym = adp_khz * regsym; /* AdpClk in kHz */
2091 regsym = regsym / 128; /* 128 = 2**7 */
2092 regsym *= 125 ; /* 125 = 1000/2**3 */
2093 regsym /= 256 ; /* 64 = 2**8 */
2094 }
2095
2096 return regsym;
2097}
2098
Mauro Carvalho Chehab0df289a2015-06-07 14:53:52 -03002099static int stv0367cab_read_status(struct dvb_frontend *fe,
2100 enum fe_status *status)
Igor M. Liplianin17cce932011-01-25 17:02:00 -03002101{
2102 struct stv0367_state *state = fe->demodulator_priv;
2103
2104 dprintk("%s:\n", __func__);
2105
2106 *status = 0;
2107
2108 if (stv0367_readbits(state, F367CAB_QAMFEC_LOCK)) {
2109 *status |= FE_HAS_LOCK;
2110 dprintk("%s: stv0367 has locked\n", __func__);
2111 }
2112
2113 return 0;
2114}
2115
2116static int stv0367cab_standby(struct dvb_frontend *fe, u8 standby_on)
2117{
2118 struct stv0367_state *state = fe->demodulator_priv;
2119
2120 dprintk("%s:\n", __func__);
2121
2122 if (standby_on) {
2123 stv0367_writebits(state, F367CAB_BYPASS_PLLXN, 0x03);
2124 stv0367_writebits(state, F367CAB_STDBY_PLLXN, 0x01);
2125 stv0367_writebits(state, F367CAB_STDBY, 1);
2126 stv0367_writebits(state, F367CAB_STDBY_CORE, 1);
2127 stv0367_writebits(state, F367CAB_EN_BUFFER_I, 0);
2128 stv0367_writebits(state, F367CAB_EN_BUFFER_Q, 0);
2129 stv0367_writebits(state, F367CAB_POFFQ, 1);
2130 stv0367_writebits(state, F367CAB_POFFI, 1);
2131 } else {
2132 stv0367_writebits(state, F367CAB_STDBY_PLLXN, 0x00);
2133 stv0367_writebits(state, F367CAB_BYPASS_PLLXN, 0x00);
2134 stv0367_writebits(state, F367CAB_STDBY, 0);
2135 stv0367_writebits(state, F367CAB_STDBY_CORE, 0);
2136 stv0367_writebits(state, F367CAB_EN_BUFFER_I, 1);
2137 stv0367_writebits(state, F367CAB_EN_BUFFER_Q, 1);
2138 stv0367_writebits(state, F367CAB_POFFQ, 0);
2139 stv0367_writebits(state, F367CAB_POFFI, 0);
2140 }
2141
2142 return 0;
2143}
2144
2145static int stv0367cab_sleep(struct dvb_frontend *fe)
2146{
2147 return stv0367cab_standby(fe, 1);
2148}
2149
Mauro Carvalho Chehab8c8ca1c2012-10-27 11:26:42 -03002150static int stv0367cab_init(struct dvb_frontend *fe)
Igor M. Liplianin17cce932011-01-25 17:02:00 -03002151{
2152 struct stv0367_state *state = fe->demodulator_priv;
2153 struct stv0367cab_state *cab_state = state->cab_state;
Igor M. Liplianin17cce932011-01-25 17:02:00 -03002154
2155 dprintk("%s:\n", __func__);
2156
Daniel Scheller8881ceb2017-03-29 13:43:04 -03002157 stv0367_write_table(state,
2158 stv0367_deftabs[state->deftabs][STV0367_TAB_CAB]);
Igor M. Liplianin17cce932011-01-25 17:02:00 -03002159
2160 switch (state->config->ts_mode) {
2161 case STV0367_DVBCI_CLOCK:
2162 dprintk("Setting TSMode = STV0367_DVBCI_CLOCK\n");
2163 stv0367_writebits(state, F367CAB_OUTFORMAT, 0x03);
2164 break;
2165 case STV0367_SERIAL_PUNCT_CLOCK:
2166 case STV0367_SERIAL_CONT_CLOCK:
2167 stv0367_writebits(state, F367CAB_OUTFORMAT, 0x01);
2168 break;
2169 case STV0367_PARALLEL_PUNCT_CLOCK:
2170 case STV0367_OUTPUTMODE_DEFAULT:
2171 stv0367_writebits(state, F367CAB_OUTFORMAT, 0x00);
2172 break;
2173 }
2174
2175 switch (state->config->clk_pol) {
2176 case STV0367_RISINGEDGE_CLOCK:
2177 stv0367_writebits(state, F367CAB_CLK_POLARITY, 0x00);
2178 break;
2179 case STV0367_FALLINGEDGE_CLOCK:
2180 case STV0367_CLOCKPOLARITY_DEFAULT:
2181 stv0367_writebits(state, F367CAB_CLK_POLARITY, 0x01);
2182 break;
2183 }
2184
2185 stv0367_writebits(state, F367CAB_SYNC_STRIP, 0x00);
2186
2187 stv0367_writebits(state, F367CAB_CT_NBST, 0x01);
2188
2189 stv0367_writebits(state, F367CAB_TS_SWAP, 0x01);
2190
2191 stv0367_writebits(state, F367CAB_FIFO_BYPASS, 0x00);
2192
2193 stv0367_writereg(state, R367CAB_ANACTRL, 0x00);/*PLL enabled and used */
2194
2195 cab_state->mclk = stv0367cab_get_mclk(fe, state->config->xtal);
2196 cab_state->adc_clk = stv0367cab_get_adc_freq(fe, state->config->xtal);
2197
2198 return 0;
2199}
2200static
2201enum stv0367_cab_signal_type stv0367cab_algo(struct stv0367_state *state,
Mauro Carvalho Chehab285d55a2011-12-26 13:03:00 -03002202 struct dtv_frontend_properties *p)
Igor M. Liplianin17cce932011-01-25 17:02:00 -03002203{
Igor M. Liplianin17cce932011-01-25 17:02:00 -03002204 struct stv0367cab_state *cab_state = state->cab_state;
2205 enum stv0367_cab_signal_type signalType = FE_CAB_NOAGC;
2206 u32 QAMFEC_Lock, QAM_Lock, u32_tmp,
2207 LockTime, TRLTimeOut, AGCTimeOut, CRLSymbols,
2208 CRLTimeOut, EQLTimeOut, DemodTimeOut, FECTimeOut;
2209 u8 TrackAGCAccum;
2210 s32 tmp;
2211
2212 dprintk("%s:\n", __func__);
2213
2214 /* Timeouts calculation */
2215 /* A max lock time of 25 ms is allowed for delayed AGC */
2216 AGCTimeOut = 25;
2217 /* 100000 symbols needed by the TRL as a maximum value */
Mauro Carvalho Chehab285d55a2011-12-26 13:03:00 -03002218 TRLTimeOut = 100000000 / p->symbol_rate;
Igor M. Liplianin17cce932011-01-25 17:02:00 -03002219 /* CRLSymbols is the needed number of symbols to achieve a lock
2220 within [-4%, +4%] of the symbol rate.
2221 CRL timeout is calculated
2222 for a lock within [-search_range, +search_range].
2223 EQL timeout can be changed depending on
2224 the micro-reflections we want to handle.
2225 A characterization must be performed
2226 with these echoes to get new timeout values.
2227 */
Mauro Carvalho Chehab285d55a2011-12-26 13:03:00 -03002228 switch (p->modulation) {
Igor M. Liplianin17cce932011-01-25 17:02:00 -03002229 case QAM_16:
2230 CRLSymbols = 150000;
2231 EQLTimeOut = 100;
2232 break;
2233 case QAM_32:
2234 CRLSymbols = 250000;
2235 EQLTimeOut = 100;
2236 break;
2237 case QAM_64:
2238 CRLSymbols = 200000;
2239 EQLTimeOut = 100;
2240 break;
2241 case QAM_128:
2242 CRLSymbols = 250000;
2243 EQLTimeOut = 100;
2244 break;
2245 case QAM_256:
2246 CRLSymbols = 250000;
2247 EQLTimeOut = 100;
2248 break;
2249 default:
2250 CRLSymbols = 200000;
2251 EQLTimeOut = 100;
2252 break;
2253 }
2254#if 0
2255 if (pIntParams->search_range < 0) {
2256 CRLTimeOut = (25 * CRLSymbols *
2257 (-pIntParams->search_range / 1000)) /
2258 (pIntParams->symbol_rate / 1000);
2259 } else
2260#endif
2261 CRLTimeOut = (25 * CRLSymbols * (cab_state->search_range / 1000)) /
Mauro Carvalho Chehab285d55a2011-12-26 13:03:00 -03002262 (p->symbol_rate / 1000);
Igor M. Liplianin17cce932011-01-25 17:02:00 -03002263
Mauro Carvalho Chehab285d55a2011-12-26 13:03:00 -03002264 CRLTimeOut = (1000 * CRLTimeOut) / p->symbol_rate;
Igor M. Liplianin17cce932011-01-25 17:02:00 -03002265 /* Timeouts below 50ms are coerced */
2266 if (CRLTimeOut < 50)
2267 CRLTimeOut = 50;
2268 /* A maximum of 100 TS packets is needed to get FEC lock even in case
2269 the spectrum inversion needs to be changed.
2270 This is equal to 20 ms in case of the lowest symbol rate of 0.87Msps
2271 */
2272 FECTimeOut = 20;
2273 DemodTimeOut = AGCTimeOut + TRLTimeOut + CRLTimeOut + EQLTimeOut;
2274
2275 dprintk("%s: DemodTimeOut=%d\n", __func__, DemodTimeOut);
2276
2277 /* Reset the TRL to ensure nothing starts until the
2278 AGC is stable which ensures a better lock time
2279 */
2280 stv0367_writereg(state, R367CAB_CTRL_1, 0x04);
2281 /* Set AGC accumulation time to minimum and lock threshold to maximum
2282 in order to speed up the AGC lock */
2283 TrackAGCAccum = stv0367_readbits(state, F367CAB_AGC_ACCUMRSTSEL);
2284 stv0367_writebits(state, F367CAB_AGC_ACCUMRSTSEL, 0x0);
2285 /* Modulus Mapper is disabled */
2286 stv0367_writebits(state, F367CAB_MODULUSMAP_EN, 0);
2287 /* Disable the sweep function */
2288 stv0367_writebits(state, F367CAB_SWEEP_EN, 0);
2289 /* The sweep function is never used, Sweep rate must be set to 0 */
2290 /* Set the derotator frequency in Hz */
2291 stv0367cab_set_derot_freq(state, cab_state->adc_clk,
2292 (1000 * (s32)state->config->if_khz + cab_state->derot_offset));
2293 /* Disable the Allpass Filter when the symbol rate is out of range */
Mauro Carvalho Chehab285d55a2011-12-26 13:03:00 -03002294 if ((p->symbol_rate > 10800000) | (p->symbol_rate < 1800000)) {
Igor M. Liplianin17cce932011-01-25 17:02:00 -03002295 stv0367_writebits(state, F367CAB_ADJ_EN, 0);
2296 stv0367_writebits(state, F367CAB_ALLPASSFILT_EN, 0);
2297 }
2298#if 0
2299 /* Check if the tuner is locked */
2300 tuner_lock = stv0367cab_tuner_get_status(fe);
2301 if (tuner_lock == 0)
2302 return FE_367CAB_NOTUNER;
2303#endif
Geert Uytterhoeven83a35e32013-06-28 11:27:31 +02002304 /* Release the TRL to start demodulator acquisition */
Igor M. Liplianin17cce932011-01-25 17:02:00 -03002305 /* Wait for QAM lock */
2306 LockTime = 0;
2307 stv0367_writereg(state, R367CAB_CTRL_1, 0x00);
2308 do {
2309 QAM_Lock = stv0367_readbits(state, F367CAB_FSM_STATUS);
2310 if ((LockTime >= (DemodTimeOut - EQLTimeOut)) &&
2311 (QAM_Lock == 0x04))
2312 /*
2313 * We don't wait longer, the frequency/phase offset
2314 * must be too big
2315 */
2316 LockTime = DemodTimeOut;
2317 else if ((LockTime >= (AGCTimeOut + TRLTimeOut)) &&
2318 (QAM_Lock == 0x02))
2319 /*
2320 * We don't wait longer, either there is no signal or
2321 * it is not the right symbol rate or it is an analog
2322 * carrier
2323 */
2324 {
2325 LockTime = DemodTimeOut;
2326 u32_tmp = stv0367_readbits(state,
2327 F367CAB_AGC_PWR_WORD_LO) +
2328 (stv0367_readbits(state,
2329 F367CAB_AGC_PWR_WORD_ME) << 8) +
2330 (stv0367_readbits(state,
2331 F367CAB_AGC_PWR_WORD_HI) << 16);
2332 if (u32_tmp >= 131072)
2333 u32_tmp = 262144 - u32_tmp;
2334 u32_tmp = u32_tmp / (1 << (11 - stv0367_readbits(state,
2335 F367CAB_AGC_IF_BWSEL)));
2336
2337 if (u32_tmp < stv0367_readbits(state,
2338 F367CAB_AGC_PWRREF_LO) +
2339 256 * stv0367_readbits(state,
2340 F367CAB_AGC_PWRREF_HI) - 10)
2341 QAM_Lock = 0x0f;
2342 } else {
2343 usleep_range(10000, 20000);
2344 LockTime += 10;
2345 }
2346 dprintk("QAM_Lock=0x%x LockTime=%d\n", QAM_Lock, LockTime);
2347 tmp = stv0367_readreg(state, R367CAB_IT_STATUS1);
2348
2349 dprintk("R367CAB_IT_STATUS1=0x%x\n", tmp);
2350
2351 } while (((QAM_Lock != 0x0c) && (QAM_Lock != 0x0b)) &&
2352 (LockTime < DemodTimeOut));
2353
2354 dprintk("QAM_Lock=0x%x\n", QAM_Lock);
2355
2356 tmp = stv0367_readreg(state, R367CAB_IT_STATUS1);
2357 dprintk("R367CAB_IT_STATUS1=0x%x\n", tmp);
2358 tmp = stv0367_readreg(state, R367CAB_IT_STATUS2);
2359 dprintk("R367CAB_IT_STATUS2=0x%x\n", tmp);
2360
2361 tmp = stv0367cab_get_derot_freq(state, cab_state->adc_clk);
2362 dprintk("stv0367cab_get_derot_freq=0x%x\n", tmp);
2363
2364 if ((QAM_Lock == 0x0c) || (QAM_Lock == 0x0b)) {
2365 /* Wait for FEC lock */
2366 LockTime = 0;
2367 do {
2368 usleep_range(5000, 7000);
2369 LockTime += 5;
2370 QAMFEC_Lock = stv0367_readbits(state,
2371 F367CAB_QAMFEC_LOCK);
2372 } while (!QAMFEC_Lock && (LockTime < FECTimeOut));
2373 } else
2374 QAMFEC_Lock = 0;
2375
2376 if (QAMFEC_Lock) {
2377 signalType = FE_CAB_DATAOK;
Igor M. Liplianin17cce932011-01-25 17:02:00 -03002378 cab_state->spect_inv = stv0367_readbits(state,
2379 F367CAB_QUAD_INV);
2380#if 0
2381/* not clear for me */
2382 if (state->config->if_khz != 0) {
2383 if (state->config->if_khz > cab_state->adc_clk / 1000) {
2384 cab_state->freq_khz =
2385 FE_Cab_TunerGetFrequency(pIntParams->hTuner)
2386 - stv0367cab_get_derot_freq(state, cab_state->adc_clk)
2387 - cab_state->adc_clk / 1000 + state->config->if_khz;
2388 } else {
2389 cab_state->freq_khz =
2390 FE_Cab_TunerGetFrequency(pIntParams->hTuner)
2391 - stv0367cab_get_derot_freq(state, cab_state->adc_clk)
2392 + state->config->if_khz;
2393 }
2394 } else {
2395 cab_state->freq_khz =
2396 FE_Cab_TunerGetFrequency(pIntParams->hTuner) +
2397 stv0367cab_get_derot_freq(state,
2398 cab_state->adc_clk) -
2399 cab_state->adc_clk / 4000;
2400 }
2401#endif
2402 cab_state->symbol_rate = stv0367cab_GetSymbolRate(state,
2403 cab_state->mclk);
2404 cab_state->locked = 1;
2405
2406 /* stv0367_setbits(state, F367CAB_AGC_ACCUMRSTSEL,7);*/
2407 } else {
2408 switch (QAM_Lock) {
2409 case 1:
2410 signalType = FE_CAB_NOAGC;
2411 break;
2412 case 2:
2413 signalType = FE_CAB_NOTIMING;
2414 break;
2415 case 3:
2416 signalType = FE_CAB_TIMINGOK;
2417 break;
2418 case 4:
2419 signalType = FE_CAB_NOCARRIER;
2420 break;
2421 case 5:
2422 signalType = FE_CAB_CARRIEROK;
2423 break;
2424 case 7:
2425 signalType = FE_CAB_NOBLIND;
2426 break;
2427 case 8:
2428 signalType = FE_CAB_BLINDOK;
2429 break;
2430 case 10:
2431 signalType = FE_CAB_NODEMOD;
2432 break;
2433 case 11:
2434 signalType = FE_CAB_DEMODOK;
2435 break;
2436 case 12:
2437 signalType = FE_CAB_DEMODOK;
2438 break;
2439 case 13:
2440 signalType = FE_CAB_NODEMOD;
2441 break;
2442 case 14:
2443 signalType = FE_CAB_NOBLIND;
2444 break;
2445 case 15:
2446 signalType = FE_CAB_NOSIGNAL;
2447 break;
2448 default:
2449 break;
2450 }
2451
2452 }
2453
2454 /* Set the AGC control values to tracking values */
2455 stv0367_writebits(state, F367CAB_AGC_ACCUMRSTSEL, TrackAGCAccum);
2456 return signalType;
2457}
2458
Mauro Carvalho Chehab285d55a2011-12-26 13:03:00 -03002459static int stv0367cab_set_frontend(struct dvb_frontend *fe)
Igor M. Liplianin17cce932011-01-25 17:02:00 -03002460{
Mauro Carvalho Chehab285d55a2011-12-26 13:03:00 -03002461 struct dtv_frontend_properties *p = &fe->dtv_property_cache;
Igor M. Liplianin17cce932011-01-25 17:02:00 -03002462 struct stv0367_state *state = fe->demodulator_priv;
2463 struct stv0367cab_state *cab_state = state->cab_state;
Igor M. Liplianin17cce932011-01-25 17:02:00 -03002464 enum stv0367cab_mod QAMSize = 0;
2465
2466 dprintk("%s: freq = %d, srate = %d\n", __func__,
Mauro Carvalho Chehab285d55a2011-12-26 13:03:00 -03002467 p->frequency, p->symbol_rate);
Igor M. Liplianin17cce932011-01-25 17:02:00 -03002468
2469 cab_state->derot_offset = 0;
2470
Mauro Carvalho Chehab285d55a2011-12-26 13:03:00 -03002471 switch (p->modulation) {
Igor M. Liplianin17cce932011-01-25 17:02:00 -03002472 case QAM_16:
2473 QAMSize = FE_CAB_MOD_QAM16;
2474 break;
2475 case QAM_32:
2476 QAMSize = FE_CAB_MOD_QAM32;
2477 break;
2478 case QAM_64:
2479 QAMSize = FE_CAB_MOD_QAM64;
2480 break;
2481 case QAM_128:
2482 QAMSize = FE_CAB_MOD_QAM128;
2483 break;
2484 case QAM_256:
2485 QAMSize = FE_CAB_MOD_QAM256;
2486 break;
2487 default:
2488 break;
2489 }
2490
2491 stv0367cab_init(fe);
2492
2493 /* Tuner Frequency Setting */
2494 if (fe->ops.tuner_ops.set_params) {
Daniel Schellerf61c2992017-03-29 13:43:01 -03002495 if (state->use_i2c_gatectrl && fe->ops.i2c_gate_ctrl)
Igor M. Liplianin17cce932011-01-25 17:02:00 -03002496 fe->ops.i2c_gate_ctrl(fe, 1);
Mauro Carvalho Chehab14d24d12011-12-24 12:24:33 -03002497 fe->ops.tuner_ops.set_params(fe);
Daniel Schellerf61c2992017-03-29 13:43:01 -03002498 if (state->use_i2c_gatectrl && fe->ops.i2c_gate_ctrl)
Igor M. Liplianin17cce932011-01-25 17:02:00 -03002499 fe->ops.i2c_gate_ctrl(fe, 0);
2500 }
2501
2502 stv0367cab_SetQamSize(
2503 state,
Mauro Carvalho Chehab285d55a2011-12-26 13:03:00 -03002504 p->symbol_rate,
Igor M. Liplianin17cce932011-01-25 17:02:00 -03002505 QAMSize);
2506
2507 stv0367cab_set_srate(state,
2508 cab_state->adc_clk,
2509 cab_state->mclk,
Mauro Carvalho Chehab285d55a2011-12-26 13:03:00 -03002510 p->symbol_rate,
Igor M. Liplianin17cce932011-01-25 17:02:00 -03002511 QAMSize);
2512 /* Search algorithm launch, [-1.1*RangeOffset, +1.1*RangeOffset] scan */
Mauro Carvalho Chehab285d55a2011-12-26 13:03:00 -03002513 cab_state->state = stv0367cab_algo(state, p);
Igor M. Liplianin17cce932011-01-25 17:02:00 -03002514 return 0;
2515}
2516
Mauro Carvalho Chehab7e3e68b2016-02-04 12:58:30 -02002517static int stv0367cab_get_frontend(struct dvb_frontend *fe,
2518 struct dtv_frontend_properties *p)
Igor M. Liplianin17cce932011-01-25 17:02:00 -03002519{
2520 struct stv0367_state *state = fe->demodulator_priv;
2521 struct stv0367cab_state *cab_state = state->cab_state;
Igor M. Liplianin17cce932011-01-25 17:02:00 -03002522
2523 enum stv0367cab_mod QAMSize;
2524
2525 dprintk("%s:\n", __func__);
2526
Mauro Carvalho Chehab285d55a2011-12-26 13:03:00 -03002527 p->symbol_rate = stv0367cab_GetSymbolRate(state, cab_state->mclk);
Igor M. Liplianin17cce932011-01-25 17:02:00 -03002528
2529 QAMSize = stv0367_readbits(state, F367CAB_QAM_MODE);
2530 switch (QAMSize) {
2531 case FE_CAB_MOD_QAM16:
Mauro Carvalho Chehab285d55a2011-12-26 13:03:00 -03002532 p->modulation = QAM_16;
Igor M. Liplianin17cce932011-01-25 17:02:00 -03002533 break;
2534 case FE_CAB_MOD_QAM32:
Mauro Carvalho Chehab285d55a2011-12-26 13:03:00 -03002535 p->modulation = QAM_32;
Igor M. Liplianin17cce932011-01-25 17:02:00 -03002536 break;
2537 case FE_CAB_MOD_QAM64:
Mauro Carvalho Chehab285d55a2011-12-26 13:03:00 -03002538 p->modulation = QAM_64;
Igor M. Liplianin17cce932011-01-25 17:02:00 -03002539 break;
2540 case FE_CAB_MOD_QAM128:
Mauro Carvalho Chehab285d55a2011-12-26 13:03:00 -03002541 p->modulation = QAM_128;
Igor M. Liplianin17cce932011-01-25 17:02:00 -03002542 break;
Maks Naumoveafeda92014-08-15 16:23:20 -03002543 case FE_CAB_MOD_QAM256:
Mauro Carvalho Chehab285d55a2011-12-26 13:03:00 -03002544 p->modulation = QAM_256;
Igor M. Liplianin17cce932011-01-25 17:02:00 -03002545 break;
2546 default:
2547 break;
2548 }
2549
Mauro Carvalho Chehab285d55a2011-12-26 13:03:00 -03002550 p->frequency = stv0367_get_tuner_freq(fe);
Igor M. Liplianin17cce932011-01-25 17:02:00 -03002551
Mauro Carvalho Chehab285d55a2011-12-26 13:03:00 -03002552 dprintk("%s: tuner frequency = %d\n", __func__, p->frequency);
Igor M. Liplianin17cce932011-01-25 17:02:00 -03002553
2554 if (state->config->if_khz == 0) {
Mauro Carvalho Chehab285d55a2011-12-26 13:03:00 -03002555 p->frequency +=
Igor M. Liplianin17cce932011-01-25 17:02:00 -03002556 (stv0367cab_get_derot_freq(state, cab_state->adc_clk) -
2557 cab_state->adc_clk / 4000);
2558 return 0;
2559 }
2560
2561 if (state->config->if_khz > cab_state->adc_clk / 1000)
Mauro Carvalho Chehab285d55a2011-12-26 13:03:00 -03002562 p->frequency += (state->config->if_khz
Igor M. Liplianin17cce932011-01-25 17:02:00 -03002563 - stv0367cab_get_derot_freq(state, cab_state->adc_clk)
2564 - cab_state->adc_clk / 1000);
2565 else
Mauro Carvalho Chehab285d55a2011-12-26 13:03:00 -03002566 p->frequency += (state->config->if_khz
Igor M. Liplianin17cce932011-01-25 17:02:00 -03002567 - stv0367cab_get_derot_freq(state, cab_state->adc_clk));
2568
2569 return 0;
2570}
2571
2572#if 0
2573void stv0367cab_GetErrorCount(state, enum stv0367cab_mod QAMSize,
2574 u32 symbol_rate, FE_367qam_Monitor *Monitor_results)
2575{
2576 stv0367cab_OptimiseNByteAndGetBER(state, QAMSize, symbol_rate, Monitor_results);
2577 stv0367cab_GetPacketsCount(state, Monitor_results);
2578
2579 return;
2580}
2581
2582static int stv0367cab_read_ber(struct dvb_frontend *fe, u32 *ber)
2583{
2584 struct stv0367_state *state = fe->demodulator_priv;
2585
2586 return 0;
2587}
2588#endif
2589static s32 stv0367cab_get_rf_lvl(struct stv0367_state *state)
2590{
2591 s32 rfLevel = 0;
2592 s32 RfAgcPwm = 0, IfAgcPwm = 0;
2593 u8 i;
2594
2595 stv0367_writebits(state, F367CAB_STDBY_ADCGP, 0x0);
2596
2597 RfAgcPwm =
2598 (stv0367_readbits(state, F367CAB_RF_AGC1_LEVEL_LO) & 0x03) +
2599 (stv0367_readbits(state, F367CAB_RF_AGC1_LEVEL_HI) << 2);
2600 RfAgcPwm = 100 * RfAgcPwm / 1023;
2601
2602 IfAgcPwm =
2603 stv0367_readbits(state, F367CAB_AGC_IF_PWMCMD_LO) +
2604 (stv0367_readbits(state, F367CAB_AGC_IF_PWMCMD_HI) << 8);
2605 if (IfAgcPwm >= 2048)
2606 IfAgcPwm -= 2048;
2607 else
2608 IfAgcPwm += 2048;
2609
2610 IfAgcPwm = 100 * IfAgcPwm / 4095;
2611
2612 /* For DTT75467 on NIM */
2613 if (RfAgcPwm < 90 && IfAgcPwm < 28) {
2614 for (i = 0; i < RF_LOOKUP_TABLE_SIZE; i++) {
2615 if (RfAgcPwm <= stv0367cab_RF_LookUp1[0][i]) {
2616 rfLevel = (-1) * stv0367cab_RF_LookUp1[1][i];
2617 break;
2618 }
2619 }
2620 if (i == RF_LOOKUP_TABLE_SIZE)
2621 rfLevel = -56;
2622 } else { /*if IF AGC>10*/
2623 for (i = 0; i < RF_LOOKUP_TABLE2_SIZE; i++) {
2624 if (IfAgcPwm <= stv0367cab_RF_LookUp2[0][i]) {
2625 rfLevel = (-1) * stv0367cab_RF_LookUp2[1][i];
2626 break;
2627 }
2628 }
2629 if (i == RF_LOOKUP_TABLE2_SIZE)
2630 rfLevel = -72;
2631 }
2632 return rfLevel;
2633}
2634
2635static int stv0367cab_read_strength(struct dvb_frontend *fe, u16 *strength)
2636{
2637 struct stv0367_state *state = fe->demodulator_priv;
2638
2639 s32 signal = stv0367cab_get_rf_lvl(state);
2640
2641 dprintk("%s: signal=%d dBm\n", __func__, signal);
2642
2643 if (signal <= -72)
2644 *strength = 65535;
2645 else
2646 *strength = (22 + signal) * (-1311);
2647
2648 dprintk("%s: strength=%d\n", __func__, (*strength));
2649
2650 return 0;
2651}
2652
2653static int stv0367cab_read_snr(struct dvb_frontend *fe, u16 *snr)
2654{
2655 struct stv0367_state *state = fe->demodulator_priv;
2656 u32 noisepercentage;
2657 enum stv0367cab_mod QAMSize;
2658 u32 regval = 0, temp = 0;
2659 int power, i;
2660
2661 QAMSize = stv0367_readbits(state, F367CAB_QAM_MODE);
2662 switch (QAMSize) {
2663 case FE_CAB_MOD_QAM4:
2664 power = 21904;
2665 break;
2666 case FE_CAB_MOD_QAM16:
2667 power = 20480;
2668 break;
2669 case FE_CAB_MOD_QAM32:
2670 power = 23040;
2671 break;
2672 case FE_CAB_MOD_QAM64:
2673 power = 21504;
2674 break;
2675 case FE_CAB_MOD_QAM128:
2676 power = 23616;
2677 break;
2678 case FE_CAB_MOD_QAM256:
2679 power = 21760;
2680 break;
2681 case FE_CAB_MOD_QAM512:
2682 power = 1;
2683 break;
2684 case FE_CAB_MOD_QAM1024:
2685 power = 21280;
2686 break;
2687 default:
2688 power = 1;
2689 break;
2690 }
2691
2692 for (i = 0; i < 10; i++) {
2693 regval += (stv0367_readbits(state, F367CAB_SNR_LO)
2694 + 256 * stv0367_readbits(state, F367CAB_SNR_HI));
2695 }
2696
2697 regval /= 10; /*for average over 10 times in for loop above*/
2698 if (regval != 0) {
2699 temp = power
2700 * (1 << (3 + stv0367_readbits(state, F367CAB_SNR_PER)));
2701 temp /= regval;
2702 }
2703
2704 /* table values, not needed to calculate logarithms */
2705 if (temp >= 5012)
2706 noisepercentage = 100;
2707 else if (temp >= 3981)
2708 noisepercentage = 93;
2709 else if (temp >= 3162)
2710 noisepercentage = 86;
2711 else if (temp >= 2512)
2712 noisepercentage = 79;
2713 else if (temp >= 1995)
2714 noisepercentage = 72;
2715 else if (temp >= 1585)
2716 noisepercentage = 65;
2717 else if (temp >= 1259)
2718 noisepercentage = 58;
2719 else if (temp >= 1000)
2720 noisepercentage = 50;
2721 else if (temp >= 794)
2722 noisepercentage = 43;
2723 else if (temp >= 501)
2724 noisepercentage = 36;
2725 else if (temp >= 316)
2726 noisepercentage = 29;
2727 else if (temp >= 200)
2728 noisepercentage = 22;
2729 else if (temp >= 158)
2730 noisepercentage = 14;
2731 else if (temp >= 126)
2732 noisepercentage = 7;
2733 else
2734 noisepercentage = 0;
2735
2736 dprintk("%s: noisepercentage=%d\n", __func__, noisepercentage);
2737
2738 *snr = (noisepercentage * 65535) / 100;
2739
2740 return 0;
2741}
2742
Abylay Ospan78db66e2011-01-02 09:12:00 -03002743static int stv0367cab_read_ucblcks(struct dvb_frontend *fe, u32 *ucblocks)
2744{
2745 struct stv0367_state *state = fe->demodulator_priv;
2746 int corrected, tscount;
2747
2748 *ucblocks = (stv0367_readreg(state, R367CAB_RS_COUNTER_5) << 8)
2749 | stv0367_readreg(state, R367CAB_RS_COUNTER_4);
2750 corrected = (stv0367_readreg(state, R367CAB_RS_COUNTER_3) << 8)
2751 | stv0367_readreg(state, R367CAB_RS_COUNTER_2);
2752 tscount = (stv0367_readreg(state, R367CAB_RS_COUNTER_2) << 8)
2753 | stv0367_readreg(state, R367CAB_RS_COUNTER_1);
2754
2755 dprintk("%s: uncorrected blocks=%d corrected blocks=%d tscount=%d\n",
2756 __func__, *ucblocks, corrected, tscount);
2757
2758 return 0;
2759};
2760
Max Kellermannbd336e62016-08-09 18:32:21 -03002761static const struct dvb_frontend_ops stv0367cab_ops = {
Mauro Carvalho Chehab285d55a2011-12-26 13:03:00 -03002762 .delsys = { SYS_DVBC_ANNEX_A },
Igor M. Liplianin17cce932011-01-25 17:02:00 -03002763 .info = {
2764 .name = "ST STV0367 DVB-C",
Igor M. Liplianin17cce932011-01-25 17:02:00 -03002765 .frequency_min = 47000000,
2766 .frequency_max = 862000000,
2767 .frequency_stepsize = 62500,
2768 .symbol_rate_min = 870000,
2769 .symbol_rate_max = 11700000,
2770 .caps = 0x400 |/* FE_CAN_QAM_4 */
2771 FE_CAN_QAM_16 | FE_CAN_QAM_32 |
2772 FE_CAN_QAM_64 | FE_CAN_QAM_128 |
2773 FE_CAN_QAM_256 | FE_CAN_FEC_AUTO
2774 },
2775 .release = stv0367_release,
2776 .init = stv0367cab_init,
2777 .sleep = stv0367cab_sleep,
2778 .i2c_gate_ctrl = stv0367cab_gate_ctrl,
Mauro Carvalho Chehab285d55a2011-12-26 13:03:00 -03002779 .set_frontend = stv0367cab_set_frontend,
2780 .get_frontend = stv0367cab_get_frontend,
Igor M. Liplianin17cce932011-01-25 17:02:00 -03002781 .read_status = stv0367cab_read_status,
2782/* .read_ber = stv0367cab_read_ber, */
2783 .read_signal_strength = stv0367cab_read_strength,
2784 .read_snr = stv0367cab_read_snr,
Abylay Ospan78db66e2011-01-02 09:12:00 -03002785 .read_ucblocks = stv0367cab_read_ucblcks,
Igor M. Liplianin17cce932011-01-25 17:02:00 -03002786 .get_tune_settings = stv0367_get_tune_settings,
2787};
2788
2789struct dvb_frontend *stv0367cab_attach(const struct stv0367_config *config,
2790 struct i2c_adapter *i2c)
2791{
2792 struct stv0367_state *state = NULL;
2793 struct stv0367cab_state *cab_state = NULL;
2794
2795 /* allocate memory for the internal state */
2796 state = kzalloc(sizeof(struct stv0367_state), GFP_KERNEL);
2797 if (state == NULL)
2798 goto error;
2799 cab_state = kzalloc(sizeof(struct stv0367cab_state), GFP_KERNEL);
2800 if (cab_state == NULL)
2801 goto error;
2802
2803 /* setup the state */
2804 state->i2c = i2c;
2805 state->config = config;
2806 cab_state->search_range = 280000;
2807 state->cab_state = cab_state;
2808 state->fe.ops = stv0367cab_ops;
2809 state->fe.demodulator_priv = state;
2810 state->chip_id = stv0367_readreg(state, 0xf000);
2811
Daniel Schellerf61c2992017-03-29 13:43:01 -03002812 /* demod operation options */
2813 state->use_i2c_gatectrl = 1;
Daniel Scheller8881ceb2017-03-29 13:43:04 -03002814 state->deftabs = STV0367_DEFTAB_GENERIC;
Daniel Schellerf61c2992017-03-29 13:43:01 -03002815
Igor M. Liplianin17cce932011-01-25 17:02:00 -03002816 dprintk("%s: chip_id = 0x%x\n", __func__, state->chip_id);
2817
2818 /* check if the demod is there */
2819 if ((state->chip_id != 0x50) && (state->chip_id != 0x60))
2820 goto error;
2821
2822 return &state->fe;
2823
2824error:
2825 kfree(cab_state);
2826 kfree(state);
2827 return NULL;
2828}
2829EXPORT_SYMBOL(stv0367cab_attach);
2830
2831MODULE_PARM_DESC(debug, "Set debug");
2832MODULE_PARM_DESC(i2c_debug, "Set i2c debug");
2833
2834MODULE_AUTHOR("Igor M. Liplianin");
2835MODULE_DESCRIPTION("ST STV0367 DVB-C/T demodulator driver");
2836MODULE_LICENSE("GPL");