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