blob: e7bed20e80f6b3202380e4e1188335ede098d9e0 [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
Daniel Schellerd394ad12017-06-25 08:26:44 -030028#include "dvb_math.h"
29
Igor M. Liplianin17cce932011-01-25 17:02:00 -030030#include "stv0367.h"
Daniel Scheller8881ceb2017-03-29 13:43:04 -030031#include "stv0367_defs.h"
Igor M. Liplianin17cce932011-01-25 17:02:00 -030032#include "stv0367_regs.h"
33#include "stv0367_priv.h"
34
Mauro Carvalho Chehab9aca4fb2013-11-02 05:17:01 -030035/* Max transfer size done by I2C transfer functions */
36#define MAX_XFER_SIZE 64
37
Igor M. Liplianin17cce932011-01-25 17:02:00 -030038static int stvdebug;
39module_param_named(debug, stvdebug, int, 0644);
40
41static int i2cdebug;
42module_param_named(i2c_debug, i2cdebug, int, 0644);
43
44#define dprintk(args...) \
45 do { \
46 if (stvdebug) \
47 printk(KERN_DEBUG args); \
48 } while (0)
49 /* DVB-C */
50
Daniel Schellerdbbac112017-03-29 13:43:11 -030051enum active_demod_state { demod_none, demod_ter, demod_cab };
52
Igor M. Liplianin17cce932011-01-25 17:02:00 -030053struct stv0367cab_state {
54 enum stv0367_cab_signal_type state;
55 u32 mclk;
56 u32 adc_clk;
57 s32 search_range;
58 s32 derot_offset;
59 /* results */
60 int locked; /* channel found */
61 u32 freq_khz; /* found frequency (in kHz) */
62 u32 symbol_rate; /* found symbol rate (in Bds) */
Mauro Carvalho Chehab0df289a2015-06-07 14:53:52 -030063 enum fe_spectral_inversion spect_inv; /* Spectrum Inversion */
Daniel Schellercae7a902017-03-29 13:43:08 -030064 u32 qamfec_status_reg; /* status reg to poll for FEC Lock */
Igor M. Liplianin17cce932011-01-25 17:02:00 -030065};
66
67struct stv0367ter_state {
68 /* DVB-T */
69 enum stv0367_ter_signal_type state;
70 enum stv0367_ter_if_iq_mode if_iq_mode;
71 enum stv0367_ter_mode mode;/* mode 2K or 8K */
Mauro Carvalho Chehab0df289a2015-06-07 14:53:52 -030072 enum fe_guard_interval guard;
Igor M. Liplianin17cce932011-01-25 17:02:00 -030073 enum stv0367_ter_hierarchy hierarchy;
74 u32 frequency;
Mauro Carvalho Chehab0df289a2015-06-07 14:53:52 -030075 enum fe_spectral_inversion sense; /* current search spectrum */
Igor M. Liplianin17cce932011-01-25 17:02:00 -030076 u8 force; /* force mode/guard */
77 u8 bw; /* channel width 6, 7 or 8 in MHz */
78 u8 pBW; /* channel width used during previous lock */
79 u32 pBER;
80 u32 pPER;
81 u32 ucblocks;
82 s8 echo_pos; /* echo position */
83 u8 first_lock;
84 u8 unlock_counter;
85 u32 agc_val;
86};
87
88struct stv0367_state {
89 struct dvb_frontend fe;
90 struct i2c_adapter *i2c;
91 /* config settings */
92 const struct stv0367_config *config;
93 u8 chip_id;
94 /* DVB-C */
95 struct stv0367cab_state *cab_state;
96 /* DVB-T */
97 struct stv0367ter_state *ter_state;
Daniel Schellerf61c2992017-03-29 13:43:01 -030098 /* flags for operation control */
99 u8 use_i2c_gatectrl;
Daniel Scheller8881ceb2017-03-29 13:43:04 -0300100 u8 deftabs;
Daniel Scheller7718dcf2017-03-29 13:43:06 -0300101 u8 reinit_on_setfrontend;
Daniel Schellerb16338e2017-03-29 13:43:07 -0300102 u8 auto_if_khz;
Daniel Schellerdbbac112017-03-29 13:43:11 -0300103 enum active_demod_state activedemod;
Igor M. Liplianin17cce932011-01-25 17:02:00 -0300104};
105
106#define RF_LOOKUP_TABLE_SIZE 31
107#define RF_LOOKUP_TABLE2_SIZE 16
108/* RF Level (for RF AGC->AGC1) Lookup Table, depends on the board and tuner.*/
Hans Verkuil817d2fd2014-08-20 19:30:33 -0300109static const s32 stv0367cab_RF_LookUp1[RF_LOOKUP_TABLE_SIZE][RF_LOOKUP_TABLE_SIZE] = {
Igor M. Liplianin17cce932011-01-25 17:02:00 -0300110 {/*AGC1*/
111 48, 50, 51, 53, 54, 56, 57, 58, 60, 61, 62, 63,
112 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75,
113 76, 77, 78, 80, 83, 85, 88,
114 }, {/*RF(dbm)*/
115 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33,
116 34, 35, 36, 37, 38, 39, 41, 42, 43, 44, 46, 47,
117 49, 50, 52, 53, 54, 55, 56,
118 }
119};
120/* RF Level (for IF AGC->AGC2) Lookup Table, depends on the board and tuner.*/
Hans Verkuil817d2fd2014-08-20 19:30:33 -0300121static const s32 stv0367cab_RF_LookUp2[RF_LOOKUP_TABLE2_SIZE][RF_LOOKUP_TABLE2_SIZE] = {
Igor M. Liplianin17cce932011-01-25 17:02:00 -0300122 {/*AGC2*/
123 28, 29, 31, 32, 34, 35, 36, 37,
124 38, 39, 40, 41, 42, 43, 44, 45,
125 }, {/*RF(dbm)*/
126 57, 58, 59, 60, 61, 62, 63, 64,
127 65, 66, 67, 68, 69, 70, 71, 72,
128 }
129};
130
Igor M. Liplianin17cce932011-01-25 17:02:00 -0300131static
132int stv0367_writeregs(struct stv0367_state *state, u16 reg, u8 *data, int len)
133{
Mauro Carvalho Chehab9aca4fb2013-11-02 05:17:01 -0300134 u8 buf[MAX_XFER_SIZE];
Igor M. Liplianin17cce932011-01-25 17:02:00 -0300135 struct i2c_msg msg = {
136 .addr = state->config->demod_address,
137 .flags = 0,
138 .buf = buf,
139 .len = len + 2
140 };
141 int ret;
142
Mauro Carvalho Chehab9aca4fb2013-11-02 05:17:01 -0300143 if (2 + len > sizeof(buf)) {
144 printk(KERN_WARNING
145 "%s: i2c wr reg=%04x: len=%d is too big!\n",
146 KBUILD_MODNAME, reg, len);
147 return -EINVAL;
148 }
149
150
Igor M. Liplianin17cce932011-01-25 17:02:00 -0300151 buf[0] = MSB(reg);
152 buf[1] = LSB(reg);
153 memcpy(buf + 2, data, len);
154
155 if (i2cdebug)
Peter Griffin86a10282015-07-30 14:08:51 -0300156 printk(KERN_DEBUG "%s: [%02x] %02x: %02x\n", __func__,
157 state->config->demod_address, reg, buf[2]);
Igor M. Liplianin17cce932011-01-25 17:02:00 -0300158
159 ret = i2c_transfer(state->i2c, &msg, 1);
160 if (ret != 1)
Peter Griffin86a10282015-07-30 14:08:51 -0300161 printk(KERN_ERR "%s: i2c write error! ([%02x] %02x: %02x)\n",
162 __func__, state->config->demod_address, reg, buf[2]);
Igor M. Liplianin17cce932011-01-25 17:02:00 -0300163
164 return (ret != 1) ? -EREMOTEIO : 0;
165}
166
167static int stv0367_writereg(struct stv0367_state *state, u16 reg, u8 data)
168{
169 return stv0367_writeregs(state, reg, &data, 1);
170}
171
172static u8 stv0367_readreg(struct stv0367_state *state, u16 reg)
173{
174 u8 b0[] = { 0, 0 };
175 u8 b1[] = { 0 };
176 struct i2c_msg msg[] = {
177 {
178 .addr = state->config->demod_address,
179 .flags = 0,
180 .buf = b0,
181 .len = 2
182 }, {
183 .addr = state->config->demod_address,
184 .flags = I2C_M_RD,
185 .buf = b1,
186 .len = 1
187 }
188 };
189 int ret;
190
191 b0[0] = MSB(reg);
192 b0[1] = LSB(reg);
193
194 ret = i2c_transfer(state->i2c, msg, 2);
195 if (ret != 2)
Peter Griffin86a10282015-07-30 14:08:51 -0300196 printk(KERN_ERR "%s: i2c read error ([%02x] %02x: %02x)\n",
197 __func__, state->config->demod_address, reg, b1[0]);
Igor M. Liplianin17cce932011-01-25 17:02:00 -0300198
199 if (i2cdebug)
Peter Griffin86a10282015-07-30 14:08:51 -0300200 printk(KERN_DEBUG "%s: [%02x] %02x: %02x\n", __func__,
201 state->config->demod_address, reg, b1[0]);
Igor M. Liplianin17cce932011-01-25 17:02:00 -0300202
203 return b1[0];
204}
205
206static void extract_mask_pos(u32 label, u8 *mask, u8 *pos)
207{
208 u8 position = 0, i = 0;
209
210 (*mask) = label & 0xff;
211
212 while ((position == 0) && (i < 8)) {
213 position = ((*mask) >> i) & 0x01;
214 i++;
215 }
216
217 (*pos) = (i - 1);
218}
219
220static void stv0367_writebits(struct stv0367_state *state, u32 label, u8 val)
221{
222 u8 reg, mask, pos;
223
224 reg = stv0367_readreg(state, (label >> 16) & 0xffff);
225 extract_mask_pos(label, &mask, &pos);
226
227 val = mask & (val << pos);
228
229 reg = (reg & (~mask)) | val;
230 stv0367_writereg(state, (label >> 16) & 0xffff, reg);
231
232}
233
234static void stv0367_setbits(u8 *reg, u32 label, u8 val)
235{
236 u8 mask, pos;
237
238 extract_mask_pos(label, &mask, &pos);
239
240 val = mask & (val << pos);
241
242 (*reg) = ((*reg) & (~mask)) | val;
243}
244
245static u8 stv0367_readbits(struct stv0367_state *state, u32 label)
246{
247 u8 val = 0xff;
248 u8 mask, pos;
249
250 extract_mask_pos(label, &mask, &pos);
251
252 val = stv0367_readreg(state, label >> 16);
253 val = (val & mask) >> pos;
254
255 return val;
256}
257
Mauro Carvalho Chehab8c8ca1c2012-10-27 11:26:42 -0300258#if 0 /* Currently, unused */
259static u8 stv0367_getbits(u8 reg, u32 label)
Igor M. Liplianin17cce932011-01-25 17:02:00 -0300260{
261 u8 mask, pos;
262
263 extract_mask_pos(label, &mask, &pos);
264
265 return (reg & mask) >> pos;
266}
Mauro Carvalho Chehab8c8ca1c2012-10-27 11:26:42 -0300267#endif
Daniel Scheller41727cb2017-03-29 13:43:03 -0300268
269static void stv0367_write_table(struct stv0367_state *state,
270 const struct st_register *deftab)
271{
272 int i = 0;
273
274 while (1) {
275 if (!deftab[i].addr)
276 break;
277 stv0367_writereg(state, deftab[i].addr, deftab[i].value);
278 i++;
279 }
280}
281
Daniel Scheller8a9c0732017-03-29 13:43:05 -0300282static void stv0367_pll_setup(struct stv0367_state *state,
283 u32 icspeed, u32 xtal)
284{
285 /* note on regs: R367TER_* and R367CAB_* defines each point to
286 * 0xf0d8, so just use R367TER_ for both cases
287 */
288
289 switch (icspeed) {
290 case STV0367_ICSPEED_58000:
291 switch (xtal) {
292 default:
293 case 27000000:
294 dprintk("STV0367 SetCLKgen for 58MHz IC and 27Mhz crystal\n");
295 /* PLLMDIV: 27, PLLNDIV: 232 */
296 stv0367_writereg(state, R367TER_PLLMDIV, 0x1b);
297 stv0367_writereg(state, R367TER_PLLNDIV, 0xe8);
298 break;
299 }
300 break;
301 default:
302 case STV0367_ICSPEED_53125:
303 switch (xtal) {
304 /* set internal freq to 53.125MHz */
305 case 16000000:
306 stv0367_writereg(state, R367TER_PLLMDIV, 0x2);
307 stv0367_writereg(state, R367TER_PLLNDIV, 0x1b);
308 break;
309 case 25000000:
310 stv0367_writereg(state, R367TER_PLLMDIV, 0xa);
311 stv0367_writereg(state, R367TER_PLLNDIV, 0x55);
312 break;
313 default:
314 case 27000000:
315 dprintk("FE_STV0367TER_SetCLKgen for 27Mhz\n");
316 stv0367_writereg(state, R367TER_PLLMDIV, 0x1);
317 stv0367_writereg(state, R367TER_PLLNDIV, 0x8);
318 break;
319 case 30000000:
320 stv0367_writereg(state, R367TER_PLLMDIV, 0xc);
321 stv0367_writereg(state, R367TER_PLLNDIV, 0x55);
322 break;
323 }
324 }
325
326 stv0367_writereg(state, R367TER_PLLSETUP, 0x18);
327}
328
Daniel Schellerb16338e2017-03-29 13:43:07 -0300329static int stv0367_get_if_khz(struct stv0367_state *state, u32 *ifkhz)
330{
331 if (state->auto_if_khz && state->fe.ops.tuner_ops.get_if_frequency) {
332 state->fe.ops.tuner_ops.get_if_frequency(&state->fe, ifkhz);
333 *ifkhz = *ifkhz / 1000; /* hz -> khz */
334 } else
335 *ifkhz = state->config->if_khz;
336
337 return 0;
338}
339
Igor M. Liplianin17cce932011-01-25 17:02:00 -0300340static int stv0367ter_gate_ctrl(struct dvb_frontend *fe, int enable)
341{
342 struct stv0367_state *state = fe->demodulator_priv;
343 u8 tmp = stv0367_readreg(state, R367TER_I2CRPT);
344
345 dprintk("%s:\n", __func__);
346
347 if (enable) {
348 stv0367_setbits(&tmp, F367TER_STOP_ENABLE, 0);
349 stv0367_setbits(&tmp, F367TER_I2CT_ON, 1);
350 } else {
351 stv0367_setbits(&tmp, F367TER_STOP_ENABLE, 1);
352 stv0367_setbits(&tmp, F367TER_I2CT_ON, 0);
353 }
354
355 stv0367_writereg(state, R367TER_I2CRPT, tmp);
356
357 return 0;
358}
359
360static u32 stv0367_get_tuner_freq(struct dvb_frontend *fe)
361{
Emil Goode20721182014-06-24 18:42:27 -0300362 struct dvb_frontend_ops *frontend_ops = &fe->ops;
363 struct dvb_tuner_ops *tuner_ops = &frontend_ops->tuner_ops;
Igor M. Liplianin17cce932011-01-25 17:02:00 -0300364 u32 freq = 0;
Dan Carpenterbf512b22011-03-06 10:40:11 -0300365 int err = 0;
Igor M. Liplianin17cce932011-01-25 17:02:00 -0300366
367 dprintk("%s:\n", __func__);
368
Igor M. Liplianin17cce932011-01-25 17:02:00 -0300369 if (tuner_ops->get_frequency) {
370 err = tuner_ops->get_frequency(fe, &freq);
371 if (err < 0) {
372 printk(KERN_ERR "%s: Invalid parameter\n", __func__);
373 return err;
374 }
375
376 dprintk("%s: frequency=%d\n", __func__, freq);
377
378 } else
379 return -1;
380
381 return freq;
382}
383
384static u16 CellsCoeffs_8MHz_367cofdm[3][6][5] = {
385 {
386 {0x10EF, 0xE205, 0x10EF, 0xCE49, 0x6DA7}, /* CELL 1 COEFFS 27M*/
387 {0x2151, 0xc557, 0x2151, 0xc705, 0x6f93}, /* CELL 2 COEFFS */
388 {0x2503, 0xc000, 0x2503, 0xc375, 0x7194}, /* CELL 3 COEFFS */
389 {0x20E9, 0xca94, 0x20e9, 0xc153, 0x7194}, /* CELL 4 COEFFS */
390 {0x06EF, 0xF852, 0x06EF, 0xC057, 0x7207}, /* CELL 5 COEFFS */
391 {0x0000, 0x0ECC, 0x0ECC, 0x0000, 0x3647} /* CELL 6 COEFFS */
392 }, {
393 {0x10A0, 0xE2AF, 0x10A1, 0xCE76, 0x6D6D}, /* CELL 1 COEFFS 25M*/
394 {0x20DC, 0xC676, 0x20D9, 0xC80A, 0x6F29},
395 {0x2532, 0xC000, 0x251D, 0xC391, 0x706F},
396 {0x1F7A, 0xCD2B, 0x2032, 0xC15E, 0x711F},
397 {0x0698, 0xFA5E, 0x0568, 0xC059, 0x7193},
398 {0x0000, 0x0918, 0x149C, 0x0000, 0x3642} /* CELL 6 COEFFS */
399 }, {
400 {0x0000, 0x0000, 0x0000, 0x0000, 0x0000}, /* 30M */
401 {0x0000, 0x0000, 0x0000, 0x0000, 0x0000},
402 {0x0000, 0x0000, 0x0000, 0x0000, 0x0000},
403 {0x0000, 0x0000, 0x0000, 0x0000, 0x0000},
404 {0x0000, 0x0000, 0x0000, 0x0000, 0x0000},
405 {0x0000, 0x0000, 0x0000, 0x0000, 0x0000}
406 }
407};
408
409static u16 CellsCoeffs_7MHz_367cofdm[3][6][5] = {
410 {
411 {0x12CA, 0xDDAF, 0x12CA, 0xCCEB, 0x6FB1}, /* CELL 1 COEFFS 27M*/
412 {0x2329, 0xC000, 0x2329, 0xC6B0, 0x725F}, /* CELL 2 COEFFS */
413 {0x2394, 0xC000, 0x2394, 0xC2C7, 0x7410}, /* CELL 3 COEFFS */
414 {0x251C, 0xC000, 0x251C, 0xC103, 0x74D9}, /* CELL 4 COEFFS */
415 {0x0804, 0xF546, 0x0804, 0xC040, 0x7544}, /* CELL 5 COEFFS */
416 {0x0000, 0x0CD9, 0x0CD9, 0x0000, 0x370A} /* CELL 6 COEFFS */
417 }, {
418 {0x1285, 0xDE47, 0x1285, 0xCD17, 0x6F76}, /*25M*/
419 {0x234C, 0xC000, 0x2348, 0xC6DA, 0x7206},
420 {0x23B4, 0xC000, 0x23AC, 0xC2DB, 0x73B3},
421 {0x253D, 0xC000, 0x25B6, 0xC10B, 0x747F},
422 {0x0721, 0xF79C, 0x065F, 0xC041, 0x74EB},
423 {0x0000, 0x08FA, 0x1162, 0x0000, 0x36FF}
424 }, {
425 {0x0000, 0x0000, 0x0000, 0x0000, 0x0000}, /* 30M */
426 {0x0000, 0x0000, 0x0000, 0x0000, 0x0000},
427 {0x0000, 0x0000, 0x0000, 0x0000, 0x0000},
428 {0x0000, 0x0000, 0x0000, 0x0000, 0x0000},
429 {0x0000, 0x0000, 0x0000, 0x0000, 0x0000},
430 {0x0000, 0x0000, 0x0000, 0x0000, 0x0000}
431 }
432};
433
434static u16 CellsCoeffs_6MHz_367cofdm[3][6][5] = {
435 {
436 {0x1699, 0xD5B8, 0x1699, 0xCBC3, 0x713B}, /* CELL 1 COEFFS 27M*/
437 {0x2245, 0xC000, 0x2245, 0xC568, 0x74D5}, /* CELL 2 COEFFS */
438 {0x227F, 0xC000, 0x227F, 0xC1FC, 0x76C6}, /* CELL 3 COEFFS */
439 {0x235E, 0xC000, 0x235E, 0xC0A7, 0x778A}, /* CELL 4 COEFFS */
440 {0x0ECB, 0xEA0B, 0x0ECB, 0xC027, 0x77DD}, /* CELL 5 COEFFS */
441 {0x0000, 0x0B68, 0x0B68, 0x0000, 0xC89A}, /* CELL 6 COEFFS */
442 }, {
443 {0x1655, 0xD64E, 0x1658, 0xCBEF, 0x70FE}, /*25M*/
444 {0x225E, 0xC000, 0x2256, 0xC589, 0x7489},
445 {0x2293, 0xC000, 0x2295, 0xC209, 0x767E},
446 {0x2377, 0xC000, 0x23AA, 0xC0AB, 0x7746},
447 {0x0DC7, 0xEBC8, 0x0D07, 0xC027, 0x7799},
448 {0x0000, 0x0888, 0x0E9C, 0x0000, 0x3757}
449
450 }, {
451 {0x0000, 0x0000, 0x0000, 0x0000, 0x0000}, /* 30M */
452 {0x0000, 0x0000, 0x0000, 0x0000, 0x0000},
453 {0x0000, 0x0000, 0x0000, 0x0000, 0x0000},
454 {0x0000, 0x0000, 0x0000, 0x0000, 0x0000},
455 {0x0000, 0x0000, 0x0000, 0x0000, 0x0000},
456 {0x0000, 0x0000, 0x0000, 0x0000, 0x0000}
457 }
458};
459
460static u32 stv0367ter_get_mclk(struct stv0367_state *state, u32 ExtClk_Hz)
461{
462 u32 mclk_Hz = 0; /* master clock frequency (Hz) */
463 u32 m, n, p;
464
465 dprintk("%s:\n", __func__);
466
467 if (stv0367_readbits(state, F367TER_BYPASS_PLLXN) == 0) {
468 n = (u32)stv0367_readbits(state, F367TER_PLL_NDIV);
469 if (n == 0)
470 n = n + 1;
471
472 m = (u32)stv0367_readbits(state, F367TER_PLL_MDIV);
473 if (m == 0)
474 m = m + 1;
475
476 p = (u32)stv0367_readbits(state, F367TER_PLL_PDIV);
477 if (p > 5)
478 p = 5;
479
480 mclk_Hz = ((ExtClk_Hz / 2) * n) / (m * (1 << p));
481
482 dprintk("N=%d M=%d P=%d mclk_Hz=%d ExtClk_Hz=%d\n",
483 n, m, p, mclk_Hz, ExtClk_Hz);
484 } else
485 mclk_Hz = ExtClk_Hz;
486
487 dprintk("%s: mclk_Hz=%d\n", __func__, mclk_Hz);
488
489 return mclk_Hz;
490}
491
492static int stv0367ter_filt_coeff_init(struct stv0367_state *state,
Dan Carpenterbc4b18c2011-03-06 10:41:23 -0300493 u16 CellsCoeffs[3][6][5], u32 DemodXtal)
Igor M. Liplianin17cce932011-01-25 17:02:00 -0300494{
495 int i, j, k, freq;
496
497 dprintk("%s:\n", __func__);
498
499 freq = stv0367ter_get_mclk(state, DemodXtal);
500
501 if (freq == 53125000)
502 k = 1; /* equivalent to Xtal 25M on 362*/
503 else if (freq == 54000000)
504 k = 0; /* equivalent to Xtal 27M on 362*/
505 else if (freq == 52500000)
506 k = 2; /* equivalent to Xtal 30M on 362*/
507 else
508 return 0;
509
510 for (i = 1; i <= 6; i++) {
511 stv0367_writebits(state, F367TER_IIR_CELL_NB, i - 1);
512
513 for (j = 1; j <= 5; j++) {
514 stv0367_writereg(state,
515 (R367TER_IIRCX_COEFF1_MSB + 2 * (j - 1)),
516 MSB(CellsCoeffs[k][i-1][j-1]));
517 stv0367_writereg(state,
518 (R367TER_IIRCX_COEFF1_LSB + 2 * (j - 1)),
519 LSB(CellsCoeffs[k][i-1][j-1]));
520 }
521 }
522
523 return 1;
524
525}
526
527static void stv0367ter_agc_iir_lock_detect_set(struct stv0367_state *state)
528{
529 dprintk("%s:\n", __func__);
530
531 stv0367_writebits(state, F367TER_LOCK_DETECT_LSB, 0x00);
532
533 /* Lock detect 1 */
534 stv0367_writebits(state, F367TER_LOCK_DETECT_CHOICE, 0x00);
535 stv0367_writebits(state, F367TER_LOCK_DETECT_MSB, 0x06);
536 stv0367_writebits(state, F367TER_AUT_AGC_TARGET_LSB, 0x04);
537
538 /* Lock detect 2 */
539 stv0367_writebits(state, F367TER_LOCK_DETECT_CHOICE, 0x01);
540 stv0367_writebits(state, F367TER_LOCK_DETECT_MSB, 0x06);
541 stv0367_writebits(state, F367TER_AUT_AGC_TARGET_LSB, 0x04);
542
543 /* Lock detect 3 */
544 stv0367_writebits(state, F367TER_LOCK_DETECT_CHOICE, 0x02);
545 stv0367_writebits(state, F367TER_LOCK_DETECT_MSB, 0x01);
546 stv0367_writebits(state, F367TER_AUT_AGC_TARGET_LSB, 0x00);
547
548 /* Lock detect 4 */
549 stv0367_writebits(state, F367TER_LOCK_DETECT_CHOICE, 0x03);
550 stv0367_writebits(state, F367TER_LOCK_DETECT_MSB, 0x01);
551 stv0367_writebits(state, F367TER_AUT_AGC_TARGET_LSB, 0x00);
552
553}
554
555static int stv0367_iir_filt_init(struct stv0367_state *state, u8 Bandwidth,
556 u32 DemodXtalValue)
557{
558 dprintk("%s:\n", __func__);
559
560 stv0367_writebits(state, F367TER_NRST_IIR, 0);
561
562 switch (Bandwidth) {
563 case 6:
564 if (!stv0367ter_filt_coeff_init(state,
565 CellsCoeffs_6MHz_367cofdm,
566 DemodXtalValue))
567 return 0;
568 break;
569 case 7:
570 if (!stv0367ter_filt_coeff_init(state,
571 CellsCoeffs_7MHz_367cofdm,
572 DemodXtalValue))
573 return 0;
574 break;
575 case 8:
576 if (!stv0367ter_filt_coeff_init(state,
577 CellsCoeffs_8MHz_367cofdm,
578 DemodXtalValue))
579 return 0;
580 break;
581 default:
582 return 0;
583 }
584
585 stv0367_writebits(state, F367TER_NRST_IIR, 1);
586
587 return 1;
588}
589
590static void stv0367ter_agc_iir_rst(struct stv0367_state *state)
591{
592
593 u8 com_n;
594
595 dprintk("%s:\n", __func__);
596
597 com_n = stv0367_readbits(state, F367TER_COM_N);
598
599 stv0367_writebits(state, F367TER_COM_N, 0x07);
600
601 stv0367_writebits(state, F367TER_COM_SOFT_RSTN, 0x00);
602 stv0367_writebits(state, F367TER_COM_AGC_ON, 0x00);
603
604 stv0367_writebits(state, F367TER_COM_SOFT_RSTN, 0x01);
605 stv0367_writebits(state, F367TER_COM_AGC_ON, 0x01);
606
607 stv0367_writebits(state, F367TER_COM_N, com_n);
608
609}
610
611static int stv0367ter_duration(s32 mode, int tempo1, int tempo2, int tempo3)
612{
613 int local_tempo = 0;
614 switch (mode) {
615 case 0:
616 local_tempo = tempo1;
617 break;
618 case 1:
619 local_tempo = tempo2;
620 break ;
621
622 case 2:
623 local_tempo = tempo3;
624 break;
625
626 default:
627 break;
628 }
629 /* msleep(local_tempo); */
630 return local_tempo;
631}
632
633static enum
634stv0367_ter_signal_type stv0367ter_check_syr(struct stv0367_state *state)
635{
636 int wd = 100;
637 unsigned short int SYR_var;
638 s32 SYRStatus;
639
640 dprintk("%s:\n", __func__);
641
642 SYR_var = stv0367_readbits(state, F367TER_SYR_LOCK);
643
644 while ((!SYR_var) && (wd > 0)) {
645 usleep_range(2000, 3000);
646 wd -= 2;
647 SYR_var = stv0367_readbits(state, F367TER_SYR_LOCK);
648 }
649
650 if (!SYR_var)
651 SYRStatus = FE_TER_NOSYMBOL;
652 else
653 SYRStatus = FE_TER_SYMBOLOK;
654
655 dprintk("stv0367ter_check_syr SYRStatus %s\n",
656 SYR_var == 0 ? "No Symbol" : "OK");
657
658 return SYRStatus;
659}
660
661static enum
662stv0367_ter_signal_type stv0367ter_check_cpamp(struct stv0367_state *state,
663 s32 FFTmode)
664{
665
666 s32 CPAMPvalue = 0, CPAMPStatus, CPAMPMin;
667 int wd = 0;
668
669 dprintk("%s:\n", __func__);
670
671 switch (FFTmode) {
672 case 0: /*2k mode*/
673 CPAMPMin = 20;
674 wd = 10;
675 break;
676 case 1: /*8k mode*/
677 CPAMPMin = 80;
678 wd = 55;
679 break;
680 case 2: /*4k mode*/
681 CPAMPMin = 40;
682 wd = 30;
683 break;
684 default:
685 CPAMPMin = 0xffff; /*drives to NOCPAMP */
686 break;
687 }
688
689 dprintk("%s: CPAMPMin=%d wd=%d\n", __func__, CPAMPMin, wd);
690
691 CPAMPvalue = stv0367_readbits(state, F367TER_PPM_CPAMP_DIRECT);
692 while ((CPAMPvalue < CPAMPMin) && (wd > 0)) {
693 usleep_range(1000, 2000);
694 wd -= 1;
695 CPAMPvalue = stv0367_readbits(state, F367TER_PPM_CPAMP_DIRECT);
696 /*dprintk("CPAMPvalue= %d at wd=%d\n",CPAMPvalue,wd); */
697 }
698 dprintk("******last CPAMPvalue= %d at wd=%d\n", CPAMPvalue, wd);
699 if (CPAMPvalue < CPAMPMin) {
700 CPAMPStatus = FE_TER_NOCPAMP;
Daniel Schellerdf5a38e2017-03-29 13:43:02 -0300701 dprintk("%s: CPAMP failed\n", __func__);
Igor M. Liplianin17cce932011-01-25 17:02:00 -0300702 } else {
Daniel Schellerdf5a38e2017-03-29 13:43:02 -0300703 dprintk("%s: CPAMP OK !\n", __func__);
Igor M. Liplianin17cce932011-01-25 17:02:00 -0300704 CPAMPStatus = FE_TER_CPAMPOK;
705 }
706
707 return CPAMPStatus;
708}
709
Mauro Carvalho Chehab8c8ca1c2012-10-27 11:26:42 -0300710static enum stv0367_ter_signal_type
711stv0367ter_lock_algo(struct stv0367_state *state)
Igor M. Liplianin17cce932011-01-25 17:02:00 -0300712{
713 enum stv0367_ter_signal_type ret_flag;
714 short int wd, tempo;
715 u8 try, u_var1 = 0, u_var2 = 0, u_var3 = 0, u_var4 = 0, mode, guard;
716 u8 tmp, tmp2;
717
718 dprintk("%s:\n", __func__);
719
720 if (state == NULL)
721 return FE_TER_SWNOK;
722
723 try = 0;
724 do {
725 ret_flag = FE_TER_LOCKOK;
726
727 stv0367_writebits(state, F367TER_CORE_ACTIVE, 0);
728
729 if (state->config->if_iq_mode != 0)
730 stv0367_writebits(state, F367TER_COM_N, 0x07);
731
732 stv0367_writebits(state, F367TER_GUARD, 3);/* suggest 2k 1/4 */
733 stv0367_writebits(state, F367TER_MODE, 0);
734 stv0367_writebits(state, F367TER_SYR_TR_DIS, 0);
735 usleep_range(5000, 10000);
736
737 stv0367_writebits(state, F367TER_CORE_ACTIVE, 1);
738
739
740 if (stv0367ter_check_syr(state) == FE_TER_NOSYMBOL)
741 return FE_TER_NOSYMBOL;
742 else { /*
743 if chip locked on wrong mode first try,
744 it must lock correctly second try */
745 mode = stv0367_readbits(state, F367TER_SYR_MODE);
746 if (stv0367ter_check_cpamp(state, mode) ==
747 FE_TER_NOCPAMP) {
748 if (try == 0)
749 ret_flag = FE_TER_NOCPAMP;
750
751 }
752 }
753
754 try++;
755 } while ((try < 10) && (ret_flag != FE_TER_LOCKOK));
756
757 tmp = stv0367_readreg(state, R367TER_SYR_STAT);
758 tmp2 = stv0367_readreg(state, R367TER_STATUS);
Hans Verkuilfb661a72011-03-06 09:26:24 -0300759 dprintk("state=%p\n", state);
Igor M. Liplianin17cce932011-01-25 17:02:00 -0300760 dprintk("LOCK OK! mode=%d SYR_STAT=0x%x R367TER_STATUS=0x%x\n",
761 mode, tmp, tmp2);
762
763 tmp = stv0367_readreg(state, R367TER_PRVIT);
764 tmp2 = stv0367_readreg(state, R367TER_I2CRPT);
765 dprintk("PRVIT=0x%x I2CRPT=0x%x\n", tmp, tmp2);
766
767 tmp = stv0367_readreg(state, R367TER_GAIN_SRC1);
768 dprintk("GAIN_SRC1=0x%x\n", tmp);
769
770 if ((mode != 0) && (mode != 1) && (mode != 2))
771 return FE_TER_SWNOK;
772
773 /*guard=stv0367_readbits(state,F367TER_SYR_GUARD); */
774
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300775 /*suppress EPQ auto for SYR_GARD 1/16 or 1/32
Igor M. Liplianin17cce932011-01-25 17:02:00 -0300776 and set channel predictor in automatic */
777#if 0
778 switch (guard) {
779
780 case 0:
781 case 1:
782 stv0367_writebits(state, F367TER_AUTO_LE_EN, 0);
783 stv0367_writereg(state, R367TER_CHC_CTL, 0x01);
784 break;
785 case 2:
786 case 3:
787 stv0367_writebits(state, F367TER_AUTO_LE_EN, 1);
788 stv0367_writereg(state, R367TER_CHC_CTL, 0x11);
789 break;
790
791 default:
792 return FE_TER_SWNOK;
793 }
794#endif
795
796 /*reset fec an reedsolo FOR 367 only*/
797 stv0367_writebits(state, F367TER_RST_SFEC, 1);
798 stv0367_writebits(state, F367TER_RST_REEDSOLO, 1);
799 usleep_range(1000, 2000);
800 stv0367_writebits(state, F367TER_RST_SFEC, 0);
801 stv0367_writebits(state, F367TER_RST_REEDSOLO, 0);
802
803 u_var1 = stv0367_readbits(state, F367TER_LK);
804 u_var2 = stv0367_readbits(state, F367TER_PRF);
805 u_var3 = stv0367_readbits(state, F367TER_TPS_LOCK);
806 /* u_var4=stv0367_readbits(state,F367TER_TSFIFO_LINEOK); */
807
808 wd = stv0367ter_duration(mode, 125, 500, 250);
809 tempo = stv0367ter_duration(mode, 4, 16, 8);
810
811 /*while ( ((!u_var1)||(!u_var2)||(!u_var3)||(!u_var4)) && (wd>=0)) */
812 while (((!u_var1) || (!u_var2) || (!u_var3)) && (wd >= 0)) {
813 usleep_range(1000 * tempo, 1000 * (tempo + 1));
814 wd -= tempo;
815 u_var1 = stv0367_readbits(state, F367TER_LK);
816 u_var2 = stv0367_readbits(state, F367TER_PRF);
817 u_var3 = stv0367_readbits(state, F367TER_TPS_LOCK);
818 /*u_var4=stv0367_readbits(state, F367TER_TSFIFO_LINEOK); */
819 }
820
821 if (!u_var1)
822 return FE_TER_NOLOCK;
823
824
825 if (!u_var2)
826 return FE_TER_NOPRFOUND;
827
828 if (!u_var3)
829 return FE_TER_NOTPS;
830
831 guard = stv0367_readbits(state, F367TER_SYR_GUARD);
832 stv0367_writereg(state, R367TER_CHC_CTL, 0x11);
833 switch (guard) {
834 case 0:
835 case 1:
836 stv0367_writebits(state, F367TER_AUTO_LE_EN, 0);
837 /*stv0367_writereg(state,R367TER_CHC_CTL, 0x1);*/
838 stv0367_writebits(state, F367TER_SYR_FILTER, 0);
839 break;
840 case 2:
841 case 3:
842 stv0367_writebits(state, F367TER_AUTO_LE_EN, 1);
843 /*stv0367_writereg(state,R367TER_CHC_CTL, 0x11);*/
844 stv0367_writebits(state, F367TER_SYR_FILTER, 1);
845 break;
846
847 default:
848 return FE_TER_SWNOK;
849 }
850
851 /* apply Sfec workaround if 8K 64QAM CR!=1/2*/
852 if ((stv0367_readbits(state, F367TER_TPS_CONST) == 2) &&
853 (mode == 1) &&
854 (stv0367_readbits(state, F367TER_TPS_HPCODE) != 0)) {
855 stv0367_writereg(state, R367TER_SFDLYSETH, 0xc0);
856 stv0367_writereg(state, R367TER_SFDLYSETM, 0x60);
857 stv0367_writereg(state, R367TER_SFDLYSETL, 0x0);
858 } else
859 stv0367_writereg(state, R367TER_SFDLYSETH, 0x0);
860
861 wd = stv0367ter_duration(mode, 125, 500, 250);
862 u_var4 = stv0367_readbits(state, F367TER_TSFIFO_LINEOK);
863
864 while ((!u_var4) && (wd >= 0)) {
865 usleep_range(1000 * tempo, 1000 * (tempo + 1));
866 wd -= tempo;
867 u_var4 = stv0367_readbits(state, F367TER_TSFIFO_LINEOK);
868 }
869
870 if (!u_var4)
871 return FE_TER_NOLOCK;
872
873 /* for 367 leave COM_N at 0x7 for IQ_mode*/
874 /*if(ter_state->if_iq_mode!=FE_TER_NORMAL_IF_TUNER) {
875 tempo=0;
876 while ((stv0367_readbits(state,F367TER_COM_USEGAINTRK)!=1) &&
877 (stv0367_readbits(state,F367TER_COM_AGCLOCK)!=1)&&(tempo<100)) {
878 ChipWaitOrAbort(state,1);
879 tempo+=1;
880 }
881
882 stv0367_writebits(state,F367TER_COM_N,0x17);
883 } */
884
885 stv0367_writebits(state, F367TER_SYR_TR_DIS, 1);
886
887 dprintk("FE_TER_LOCKOK !!!\n");
888
889 return FE_TER_LOCKOK;
890
891}
892
893static void stv0367ter_set_ts_mode(struct stv0367_state *state,
894 enum stv0367_ts_mode PathTS)
895{
896
897 dprintk("%s:\n", __func__);
898
899 if (state == NULL)
900 return;
901
902 stv0367_writebits(state, F367TER_TS_DIS, 0);
903 switch (PathTS) {
904 default:
905 /*for removing warning :default we can assume in parallel mode*/
906 case STV0367_PARALLEL_PUNCT_CLOCK:
907 stv0367_writebits(state, F367TER_TSFIFO_SERIAL, 0);
908 stv0367_writebits(state, F367TER_TSFIFO_DVBCI, 0);
909 break;
910 case STV0367_SERIAL_PUNCT_CLOCK:
911 stv0367_writebits(state, F367TER_TSFIFO_SERIAL, 1);
912 stv0367_writebits(state, F367TER_TSFIFO_DVBCI, 1);
913 break;
914 }
915}
916
917static void stv0367ter_set_clk_pol(struct stv0367_state *state,
918 enum stv0367_clk_pol clock)
919{
920
921 dprintk("%s:\n", __func__);
922
923 if (state == NULL)
924 return;
925
926 switch (clock) {
927 case STV0367_RISINGEDGE_CLOCK:
928 stv0367_writebits(state, F367TER_TS_BYTE_CLK_INV, 1);
929 break;
930 case STV0367_FALLINGEDGE_CLOCK:
931 stv0367_writebits(state, F367TER_TS_BYTE_CLK_INV, 0);
932 break;
933 /*case FE_TER_CLOCK_POLARITY_DEFAULT:*/
934 default:
935 stv0367_writebits(state, F367TER_TS_BYTE_CLK_INV, 0);
936 break;
937 }
938}
939
940#if 0
941static void stv0367ter_core_sw(struct stv0367_state *state)
942{
943
944 dprintk("%s:\n", __func__);
945
946 stv0367_writebits(state, F367TER_CORE_ACTIVE, 0);
947 stv0367_writebits(state, F367TER_CORE_ACTIVE, 1);
948 msleep(350);
949}
950#endif
951static int stv0367ter_standby(struct dvb_frontend *fe, u8 standby_on)
952{
953 struct stv0367_state *state = fe->demodulator_priv;
954
955 dprintk("%s:\n", __func__);
956
957 if (standby_on) {
958 stv0367_writebits(state, F367TER_STDBY, 1);
959 stv0367_writebits(state, F367TER_STDBY_FEC, 1);
960 stv0367_writebits(state, F367TER_STDBY_CORE, 1);
961 } else {
962 stv0367_writebits(state, F367TER_STDBY, 0);
963 stv0367_writebits(state, F367TER_STDBY_FEC, 0);
964 stv0367_writebits(state, F367TER_STDBY_CORE, 0);
965 }
966
967 return 0;
968}
969
970static int stv0367ter_sleep(struct dvb_frontend *fe)
971{
972 return stv0367ter_standby(fe, 1);
973}
974
Mauro Carvalho Chehab8c8ca1c2012-10-27 11:26:42 -0300975static int stv0367ter_init(struct dvb_frontend *fe)
Igor M. Liplianin17cce932011-01-25 17:02:00 -0300976{
977 struct stv0367_state *state = fe->demodulator_priv;
978 struct stv0367ter_state *ter_state = state->ter_state;
Igor M. Liplianin17cce932011-01-25 17:02:00 -0300979
980 dprintk("%s:\n", __func__);
981
982 ter_state->pBER = 0;
983
Daniel Scheller8881ceb2017-03-29 13:43:04 -0300984 stv0367_write_table(state,
985 stv0367_deftabs[state->deftabs][STV0367_TAB_TER]);
Igor M. Liplianin17cce932011-01-25 17:02:00 -0300986
Daniel Scheller8a9c0732017-03-29 13:43:05 -0300987 stv0367_pll_setup(state, STV0367_ICSPEED_53125, state->config->xtal);
Igor M. Liplianin17cce932011-01-25 17:02:00 -0300988
989 stv0367_writereg(state, R367TER_I2CRPT, 0xa0);
990 stv0367_writereg(state, R367TER_ANACTRL, 0x00);
991
992 /*Set TS1 and TS2 to serial or parallel mode */
993 stv0367ter_set_ts_mode(state, state->config->ts_mode);
994 stv0367ter_set_clk_pol(state, state->config->clk_pol);
995
996 state->chip_id = stv0367_readreg(state, R367TER_ID);
997 ter_state->first_lock = 0;
998 ter_state->unlock_counter = 2;
999
1000 return 0;
1001}
1002
Mauro Carvalho Chehab285d55a2011-12-26 13:03:00 -03001003static int stv0367ter_algo(struct dvb_frontend *fe)
Igor M. Liplianin17cce932011-01-25 17:02:00 -03001004{
Mauro Carvalho Chehab285d55a2011-12-26 13:03:00 -03001005 struct dtv_frontend_properties *p = &fe->dtv_property_cache;
Igor M. Liplianin17cce932011-01-25 17:02:00 -03001006 struct stv0367_state *state = fe->demodulator_priv;
1007 struct stv0367ter_state *ter_state = state->ter_state;
1008 int offset = 0, tempo = 0;
1009 u8 u_var;
Peter Senna Tschudindf1ec022012-06-14 13:58:14 -03001010 u8 /*constell,*/ counter;
Igor M. Liplianin17cce932011-01-25 17:02:00 -03001011 s8 step;
1012 s32 timing_offset = 0;
Daniel Schellerb16338e2017-03-29 13:43:07 -03001013 u32 trl_nomrate = 0, InternalFreq = 0, temp = 0, ifkhz = 0;
Igor M. Liplianin17cce932011-01-25 17:02:00 -03001014
1015 dprintk("%s:\n", __func__);
1016
Daniel Schellerb16338e2017-03-29 13:43:07 -03001017 stv0367_get_if_khz(state, &ifkhz);
1018
Mauro Carvalho Chehab285d55a2011-12-26 13:03:00 -03001019 ter_state->frequency = p->frequency;
Igor M. Liplianin17cce932011-01-25 17:02:00 -03001020 ter_state->force = FE_TER_FORCENONE
1021 + stv0367_readbits(state, F367TER_FORCE) * 2;
1022 ter_state->if_iq_mode = state->config->if_iq_mode;
1023 switch (state->config->if_iq_mode) {
1024 case FE_TER_NORMAL_IF_TUNER: /* Normal IF mode */
1025 dprintk("ALGO: FE_TER_NORMAL_IF_TUNER selected\n");
1026 stv0367_writebits(state, F367TER_TUNER_BB, 0);
1027 stv0367_writebits(state, F367TER_LONGPATH_IF, 0);
1028 stv0367_writebits(state, F367TER_DEMUX_SWAP, 0);
1029 break;
1030 case FE_TER_LONGPATH_IF_TUNER: /* Long IF mode */
1031 dprintk("ALGO: FE_TER_LONGPATH_IF_TUNER selected\n");
1032 stv0367_writebits(state, F367TER_TUNER_BB, 0);
1033 stv0367_writebits(state, F367TER_LONGPATH_IF, 1);
1034 stv0367_writebits(state, F367TER_DEMUX_SWAP, 1);
1035 break;
1036 case FE_TER_IQ_TUNER: /* IQ mode */
1037 dprintk("ALGO: FE_TER_IQ_TUNER selected\n");
1038 stv0367_writebits(state, F367TER_TUNER_BB, 1);
1039 stv0367_writebits(state, F367TER_PPM_INVSEL, 0);
1040 break;
1041 default:
1042 printk(KERN_ERR "ALGO: wrong TUNER type selected\n");
1043 return -EINVAL;
1044 }
1045
1046 usleep_range(5000, 7000);
1047
Mauro Carvalho Chehab285d55a2011-12-26 13:03:00 -03001048 switch (p->inversion) {
Igor M. Liplianin17cce932011-01-25 17:02:00 -03001049 case INVERSION_AUTO:
1050 default:
1051 dprintk("%s: inversion AUTO\n", __func__);
1052 if (ter_state->if_iq_mode == FE_TER_IQ_TUNER)
1053 stv0367_writebits(state, F367TER_IQ_INVERT,
1054 ter_state->sense);
1055 else
1056 stv0367_writebits(state, F367TER_INV_SPECTR,
1057 ter_state->sense);
1058
1059 break;
1060 case INVERSION_ON:
1061 case INVERSION_OFF:
1062 if (ter_state->if_iq_mode == FE_TER_IQ_TUNER)
1063 stv0367_writebits(state, F367TER_IQ_INVERT,
Mauro Carvalho Chehab285d55a2011-12-26 13:03:00 -03001064 p->inversion);
Igor M. Liplianin17cce932011-01-25 17:02:00 -03001065 else
1066 stv0367_writebits(state, F367TER_INV_SPECTR,
Mauro Carvalho Chehab285d55a2011-12-26 13:03:00 -03001067 p->inversion);
Igor M. Liplianin17cce932011-01-25 17:02:00 -03001068
1069 break;
1070 }
1071
1072 if ((ter_state->if_iq_mode != FE_TER_NORMAL_IF_TUNER) &&
1073 (ter_state->pBW != ter_state->bw)) {
1074 stv0367ter_agc_iir_lock_detect_set(state);
1075
1076 /*set fine agc target to 180 for LPIF or IQ mode*/
1077 /* set Q_AGCTarget */
1078 stv0367_writebits(state, F367TER_SEL_IQNTAR, 1);
1079 stv0367_writebits(state, F367TER_AUT_AGC_TARGET_MSB, 0xB);
1080 /*stv0367_writebits(state,AUT_AGC_TARGET_LSB,0x04); */
1081
1082 /* set Q_AGCTarget */
1083 stv0367_writebits(state, F367TER_SEL_IQNTAR, 0);
1084 stv0367_writebits(state, F367TER_AUT_AGC_TARGET_MSB, 0xB);
1085 /*stv0367_writebits(state,AUT_AGC_TARGET_LSB,0x04); */
1086
1087 if (!stv0367_iir_filt_init(state, ter_state->bw,
1088 state->config->xtal))
1089 return -EINVAL;
1090 /*set IIR filter once for 6,7 or 8MHz BW*/
1091 ter_state->pBW = ter_state->bw;
1092
1093 stv0367ter_agc_iir_rst(state);
1094 }
1095
1096 if (ter_state->hierarchy == FE_TER_HIER_LOW_PRIO)
1097 stv0367_writebits(state, F367TER_BDI_LPSEL, 0x01);
1098 else
1099 stv0367_writebits(state, F367TER_BDI_LPSEL, 0x00);
1100
1101 InternalFreq = stv0367ter_get_mclk(state, state->config->xtal) / 1000;
1102 temp = (int)
1103 ((((ter_state->bw * 64 * (1 << 15) * 100)
1104 / (InternalFreq)) * 10) / 7);
1105
1106 stv0367_writebits(state, F367TER_TRL_NOMRATE_LSB, temp % 2);
1107 temp = temp / 2;
1108 stv0367_writebits(state, F367TER_TRL_NOMRATE_HI, temp / 256);
1109 stv0367_writebits(state, F367TER_TRL_NOMRATE_LO, temp % 256);
1110
1111 temp = stv0367_readbits(state, F367TER_TRL_NOMRATE_HI) * 512 +
1112 stv0367_readbits(state, F367TER_TRL_NOMRATE_LO) * 2 +
1113 stv0367_readbits(state, F367TER_TRL_NOMRATE_LSB);
1114 temp = (int)(((1 << 17) * ter_state->bw * 1000) / (7 * (InternalFreq)));
1115 stv0367_writebits(state, F367TER_GAIN_SRC_HI, temp / 256);
1116 stv0367_writebits(state, F367TER_GAIN_SRC_LO, temp % 256);
1117 temp = stv0367_readbits(state, F367TER_GAIN_SRC_HI) * 256 +
1118 stv0367_readbits(state, F367TER_GAIN_SRC_LO);
1119
1120 temp = (int)
Daniel Schellerb16338e2017-03-29 13:43:07 -03001121 ((InternalFreq - ifkhz) * (1 << 16) / (InternalFreq));
Igor M. Liplianin17cce932011-01-25 17:02:00 -03001122
1123 dprintk("DEROT temp=0x%x\n", temp);
1124 stv0367_writebits(state, F367TER_INC_DEROT_HI, temp / 256);
1125 stv0367_writebits(state, F367TER_INC_DEROT_LO, temp % 256);
1126
1127 ter_state->echo_pos = 0;
1128 ter_state->ucblocks = 0; /* liplianin */
1129 ter_state->pBER = 0; /* liplianin */
1130 stv0367_writebits(state, F367TER_LONG_ECHO, ter_state->echo_pos);
1131
1132 if (stv0367ter_lock_algo(state) != FE_TER_LOCKOK)
1133 return 0;
1134
1135 ter_state->state = FE_TER_LOCKOK;
Igor M. Liplianin17cce932011-01-25 17:02:00 -03001136
1137 ter_state->mode = stv0367_readbits(state, F367TER_SYR_MODE);
1138 ter_state->guard = stv0367_readbits(state, F367TER_SYR_GUARD);
1139
1140 ter_state->first_lock = 1; /* we know sense now :) */
1141
1142 ter_state->agc_val =
1143 (stv0367_readbits(state, F367TER_AGC1_VAL_LO) << 16) +
1144 (stv0367_readbits(state, F367TER_AGC1_VAL_HI) << 24) +
1145 stv0367_readbits(state, F367TER_AGC2_VAL_LO) +
1146 (stv0367_readbits(state, F367TER_AGC2_VAL_HI) << 8);
1147
1148 /* Carrier offset calculation */
1149 stv0367_writebits(state, F367TER_FREEZE, 1);
1150 offset = (stv0367_readbits(state, F367TER_CRL_FOFFSET_VHI) << 16) ;
1151 offset += (stv0367_readbits(state, F367TER_CRL_FOFFSET_HI) << 8);
1152 offset += (stv0367_readbits(state, F367TER_CRL_FOFFSET_LO));
1153 stv0367_writebits(state, F367TER_FREEZE, 0);
1154 if (offset > 8388607)
1155 offset -= 16777216;
1156
1157 offset = offset * 2 / 16384;
1158
1159 if (ter_state->mode == FE_TER_MODE_2K)
1160 offset = (offset * 4464) / 1000;/*** 1 FFT BIN=4.464khz***/
1161 else if (ter_state->mode == FE_TER_MODE_4K)
1162 offset = (offset * 223) / 100;/*** 1 FFT BIN=2.23khz***/
1163 else if (ter_state->mode == FE_TER_MODE_8K)
1164 offset = (offset * 111) / 100;/*** 1 FFT BIN=1.1khz***/
1165
1166 if (stv0367_readbits(state, F367TER_PPM_INVSEL) == 1) {
1167 if ((stv0367_readbits(state, F367TER_INV_SPECTR) ==
1168 (stv0367_readbits(state,
1169 F367TER_STATUS_INV_SPECRUM) == 1)))
1170 offset = offset * -1;
1171 }
1172
1173 if (ter_state->bw == 6)
1174 offset = (offset * 6) / 8;
1175 else if (ter_state->bw == 7)
1176 offset = (offset * 7) / 8;
1177
1178 ter_state->frequency += offset;
1179
1180 tempo = 10; /* exit even if timing_offset stays null */
1181 while ((timing_offset == 0) && (tempo > 0)) {
1182 usleep_range(10000, 20000); /*was 20ms */
1183 /* fine tuning of timing offset if required */
1184 timing_offset = stv0367_readbits(state, F367TER_TRL_TOFFSET_LO)
1185 + 256 * stv0367_readbits(state,
1186 F367TER_TRL_TOFFSET_HI);
1187 if (timing_offset >= 32768)
1188 timing_offset -= 65536;
1189 trl_nomrate = (512 * stv0367_readbits(state,
1190 F367TER_TRL_NOMRATE_HI)
1191 + stv0367_readbits(state, F367TER_TRL_NOMRATE_LO) * 2
1192 + stv0367_readbits(state, F367TER_TRL_NOMRATE_LSB));
1193
1194 timing_offset = ((signed)(1000000 / trl_nomrate) *
1195 timing_offset) / 2048;
1196 tempo--;
1197 }
1198
1199 if (timing_offset <= 0) {
1200 timing_offset = (timing_offset - 11) / 22;
1201 step = -1;
1202 } else {
1203 timing_offset = (timing_offset + 11) / 22;
1204 step = 1;
1205 }
1206
1207 for (counter = 0; counter < abs(timing_offset); counter++) {
1208 trl_nomrate += step;
1209 stv0367_writebits(state, F367TER_TRL_NOMRATE_LSB,
1210 trl_nomrate % 2);
1211 stv0367_writebits(state, F367TER_TRL_NOMRATE_LO,
1212 trl_nomrate / 2);
1213 usleep_range(1000, 2000);
1214 }
1215
1216 usleep_range(5000, 6000);
1217 /* unlocks could happen in case of trl centring big step,
1218 then a core off/on restarts demod */
1219 u_var = stv0367_readbits(state, F367TER_LK);
1220
1221 if (!u_var) {
1222 stv0367_writebits(state, F367TER_CORE_ACTIVE, 0);
1223 msleep(20);
1224 stv0367_writebits(state, F367TER_CORE_ACTIVE, 1);
1225 }
1226
1227 return 0;
1228}
1229
Mauro Carvalho Chehab285d55a2011-12-26 13:03:00 -03001230static int stv0367ter_set_frontend(struct dvb_frontend *fe)
Igor M. Liplianin17cce932011-01-25 17:02:00 -03001231{
Mauro Carvalho Chehab285d55a2011-12-26 13:03:00 -03001232 struct dtv_frontend_properties *p = &fe->dtv_property_cache;
Igor M. Liplianin17cce932011-01-25 17:02:00 -03001233 struct stv0367_state *state = fe->demodulator_priv;
1234 struct stv0367ter_state *ter_state = state->ter_state;
1235
1236 /*u8 trials[2]; */
1237 s8 num_trials, index;
1238 u8 SenseTrials[] = { INVERSION_ON, INVERSION_OFF };
1239
Daniel Scheller7718dcf2017-03-29 13:43:06 -03001240 if (state->reinit_on_setfrontend)
1241 stv0367ter_init(fe);
Igor M. Liplianin17cce932011-01-25 17:02:00 -03001242
1243 if (fe->ops.tuner_ops.set_params) {
Daniel Schellerf61c2992017-03-29 13:43:01 -03001244 if (state->use_i2c_gatectrl && fe->ops.i2c_gate_ctrl)
Igor M. Liplianin17cce932011-01-25 17:02:00 -03001245 fe->ops.i2c_gate_ctrl(fe, 1);
Mauro Carvalho Chehab14d24d12011-12-24 12:24:33 -03001246 fe->ops.tuner_ops.set_params(fe);
Daniel Schellerf61c2992017-03-29 13:43:01 -03001247 if (state->use_i2c_gatectrl && fe->ops.i2c_gate_ctrl)
Igor M. Liplianin17cce932011-01-25 17:02:00 -03001248 fe->ops.i2c_gate_ctrl(fe, 0);
1249 }
1250
Mauro Carvalho Chehab285d55a2011-12-26 13:03:00 -03001251 switch (p->transmission_mode) {
Igor M. Liplianin17cce932011-01-25 17:02:00 -03001252 default:
1253 case TRANSMISSION_MODE_AUTO:
1254 case TRANSMISSION_MODE_2K:
1255 ter_state->mode = FE_TER_MODE_2K;
1256 break;
1257/* case TRANSMISSION_MODE_4K:
1258 pLook.mode = FE_TER_MODE_4K;
1259 break;*/
1260 case TRANSMISSION_MODE_8K:
1261 ter_state->mode = FE_TER_MODE_8K;
1262 break;
1263 }
1264
Mauro Carvalho Chehab285d55a2011-12-26 13:03:00 -03001265 switch (p->guard_interval) {
Igor M. Liplianin17cce932011-01-25 17:02:00 -03001266 default:
1267 case GUARD_INTERVAL_1_32:
1268 case GUARD_INTERVAL_1_16:
1269 case GUARD_INTERVAL_1_8:
1270 case GUARD_INTERVAL_1_4:
Mauro Carvalho Chehab285d55a2011-12-26 13:03:00 -03001271 ter_state->guard = p->guard_interval;
Igor M. Liplianin17cce932011-01-25 17:02:00 -03001272 break;
1273 case GUARD_INTERVAL_AUTO:
1274 ter_state->guard = GUARD_INTERVAL_1_32;
1275 break;
1276 }
1277
Mauro Carvalho Chehab285d55a2011-12-26 13:03:00 -03001278 switch (p->bandwidth_hz) {
1279 case 6000000:
Igor M. Liplianin17cce932011-01-25 17:02:00 -03001280 ter_state->bw = FE_TER_CHAN_BW_6M;
1281 break;
Mauro Carvalho Chehab285d55a2011-12-26 13:03:00 -03001282 case 7000000:
Igor M. Liplianin17cce932011-01-25 17:02:00 -03001283 ter_state->bw = FE_TER_CHAN_BW_7M;
1284 break;
Mauro Carvalho Chehab285d55a2011-12-26 13:03:00 -03001285 case 8000000:
Igor M. Liplianin17cce932011-01-25 17:02:00 -03001286 default:
1287 ter_state->bw = FE_TER_CHAN_BW_8M;
1288 }
1289
1290 ter_state->hierarchy = FE_TER_HIER_NONE;
1291
Mauro Carvalho Chehab285d55a2011-12-26 13:03:00 -03001292 switch (p->inversion) {
Igor M. Liplianin17cce932011-01-25 17:02:00 -03001293 case INVERSION_OFF:
1294 case INVERSION_ON:
1295 num_trials = 1;
1296 break;
1297 default:
1298 num_trials = 2;
1299 if (ter_state->first_lock)
1300 num_trials = 1;
1301 break;
1302 }
1303
1304 ter_state->state = FE_TER_NOLOCK;
1305 index = 0;
1306
1307 while (((index) < num_trials) && (ter_state->state != FE_TER_LOCKOK)) {
1308 if (!ter_state->first_lock) {
Mauro Carvalho Chehab285d55a2011-12-26 13:03:00 -03001309 if (p->inversion == INVERSION_AUTO)
Igor M. Liplianin17cce932011-01-25 17:02:00 -03001310 ter_state->sense = SenseTrials[index];
1311
1312 }
Mauro Carvalho Chehab285d55a2011-12-26 13:03:00 -03001313 stv0367ter_algo(fe);
Igor M. Liplianin17cce932011-01-25 17:02:00 -03001314
1315 if ((ter_state->state == FE_TER_LOCKOK) &&
Mauro Carvalho Chehab285d55a2011-12-26 13:03:00 -03001316 (p->inversion == INVERSION_AUTO) &&
Igor M. Liplianin17cce932011-01-25 17:02:00 -03001317 (index == 1)) {
1318 /* invert spectrum sense */
1319 SenseTrials[index] = SenseTrials[0];
1320 SenseTrials[(index + 1) % 2] = (SenseTrials[1] + 1) % 2;
1321 }
1322
1323 index++;
1324 }
1325
1326 return 0;
1327}
1328
1329static int stv0367ter_read_ucblocks(struct dvb_frontend *fe, u32 *ucblocks)
1330{
1331 struct stv0367_state *state = fe->demodulator_priv;
1332 struct stv0367ter_state *ter_state = state->ter_state;
1333 u32 errs = 0;
1334
1335 /*wait for counting completion*/
1336 if (stv0367_readbits(state, F367TER_SFERRC_OLDVALUE) == 0) {
1337 errs =
1338 ((u32)stv0367_readbits(state, F367TER_ERR_CNT1)
1339 * (1 << 16))
1340 + ((u32)stv0367_readbits(state, F367TER_ERR_CNT1_HI)
1341 * (1 << 8))
1342 + ((u32)stv0367_readbits(state, F367TER_ERR_CNT1_LO));
1343 ter_state->ucblocks = errs;
1344 }
1345
1346 (*ucblocks) = ter_state->ucblocks;
1347
1348 return 0;
1349}
1350
Mauro Carvalho Chehab7e3e68b2016-02-04 12:58:30 -02001351static int stv0367ter_get_frontend(struct dvb_frontend *fe,
1352 struct dtv_frontend_properties *p)
Igor M. Liplianin17cce932011-01-25 17:02:00 -03001353{
1354 struct stv0367_state *state = fe->demodulator_priv;
1355 struct stv0367ter_state *ter_state = state->ter_state;
Igor M. Liplianin17cce932011-01-25 17:02:00 -03001356 enum stv0367_ter_mode mode;
1357 int constell = 0,/* snr = 0,*/ Data = 0;
1358
Mauro Carvalho Chehab285d55a2011-12-26 13:03:00 -03001359 p->frequency = stv0367_get_tuner_freq(fe);
1360 if ((int)p->frequency < 0)
1361 p->frequency = -p->frequency;
Igor M. Liplianin17cce932011-01-25 17:02:00 -03001362
1363 constell = stv0367_readbits(state, F367TER_TPS_CONST);
1364 if (constell == 0)
Mauro Carvalho Chehab285d55a2011-12-26 13:03:00 -03001365 p->modulation = QPSK;
Igor M. Liplianin17cce932011-01-25 17:02:00 -03001366 else if (constell == 1)
Mauro Carvalho Chehab285d55a2011-12-26 13:03:00 -03001367 p->modulation = QAM_16;
Igor M. Liplianin17cce932011-01-25 17:02:00 -03001368 else
Mauro Carvalho Chehab285d55a2011-12-26 13:03:00 -03001369 p->modulation = QAM_64;
Igor M. Liplianin17cce932011-01-25 17:02:00 -03001370
Mauro Carvalho Chehab285d55a2011-12-26 13:03:00 -03001371 p->inversion = stv0367_readbits(state, F367TER_INV_SPECTR);
Igor M. Liplianin17cce932011-01-25 17:02:00 -03001372
1373 /* Get the Hierarchical mode */
1374 Data = stv0367_readbits(state, F367TER_TPS_HIERMODE);
1375
1376 switch (Data) {
1377 case 0:
Mauro Carvalho Chehab285d55a2011-12-26 13:03:00 -03001378 p->hierarchy = HIERARCHY_NONE;
Igor M. Liplianin17cce932011-01-25 17:02:00 -03001379 break;
1380 case 1:
Mauro Carvalho Chehab285d55a2011-12-26 13:03:00 -03001381 p->hierarchy = HIERARCHY_1;
Igor M. Liplianin17cce932011-01-25 17:02:00 -03001382 break;
1383 case 2:
Mauro Carvalho Chehab285d55a2011-12-26 13:03:00 -03001384 p->hierarchy = HIERARCHY_2;
Igor M. Liplianin17cce932011-01-25 17:02:00 -03001385 break;
1386 case 3:
Mauro Carvalho Chehab285d55a2011-12-26 13:03:00 -03001387 p->hierarchy = HIERARCHY_4;
Igor M. Liplianin17cce932011-01-25 17:02:00 -03001388 break;
1389 default:
Mauro Carvalho Chehab285d55a2011-12-26 13:03:00 -03001390 p->hierarchy = HIERARCHY_AUTO;
Igor M. Liplianin17cce932011-01-25 17:02:00 -03001391 break; /* error */
1392 }
1393
1394 /* Get the FEC Rate */
1395 if (ter_state->hierarchy == FE_TER_HIER_LOW_PRIO)
1396 Data = stv0367_readbits(state, F367TER_TPS_LPCODE);
1397 else
1398 Data = stv0367_readbits(state, F367TER_TPS_HPCODE);
1399
1400 switch (Data) {
1401 case 0:
Mauro Carvalho Chehab285d55a2011-12-26 13:03:00 -03001402 p->code_rate_HP = FEC_1_2;
Igor M. Liplianin17cce932011-01-25 17:02:00 -03001403 break;
1404 case 1:
Mauro Carvalho Chehab285d55a2011-12-26 13:03:00 -03001405 p->code_rate_HP = FEC_2_3;
Igor M. Liplianin17cce932011-01-25 17:02:00 -03001406 break;
1407 case 2:
Mauro Carvalho Chehab285d55a2011-12-26 13:03:00 -03001408 p->code_rate_HP = FEC_3_4;
Igor M. Liplianin17cce932011-01-25 17:02:00 -03001409 break;
1410 case 3:
Mauro Carvalho Chehab285d55a2011-12-26 13:03:00 -03001411 p->code_rate_HP = FEC_5_6;
Igor M. Liplianin17cce932011-01-25 17:02:00 -03001412 break;
1413 case 4:
Mauro Carvalho Chehab285d55a2011-12-26 13:03:00 -03001414 p->code_rate_HP = FEC_7_8;
Igor M. Liplianin17cce932011-01-25 17:02:00 -03001415 break;
1416 default:
Mauro Carvalho Chehab285d55a2011-12-26 13:03:00 -03001417 p->code_rate_HP = FEC_AUTO;
Igor M. Liplianin17cce932011-01-25 17:02:00 -03001418 break; /* error */
1419 }
1420
1421 mode = stv0367_readbits(state, F367TER_SYR_MODE);
1422
1423 switch (mode) {
1424 case FE_TER_MODE_2K:
Mauro Carvalho Chehab285d55a2011-12-26 13:03:00 -03001425 p->transmission_mode = TRANSMISSION_MODE_2K;
Igor M. Liplianin17cce932011-01-25 17:02:00 -03001426 break;
1427/* case FE_TER_MODE_4K:
Mauro Carvalho Chehab285d55a2011-12-26 13:03:00 -03001428 p->transmission_mode = TRANSMISSION_MODE_4K;
Igor M. Liplianin17cce932011-01-25 17:02:00 -03001429 break;*/
1430 case FE_TER_MODE_8K:
Mauro Carvalho Chehab285d55a2011-12-26 13:03:00 -03001431 p->transmission_mode = TRANSMISSION_MODE_8K;
Igor M. Liplianin17cce932011-01-25 17:02:00 -03001432 break;
1433 default:
Mauro Carvalho Chehab285d55a2011-12-26 13:03:00 -03001434 p->transmission_mode = TRANSMISSION_MODE_AUTO;
Igor M. Liplianin17cce932011-01-25 17:02:00 -03001435 }
1436
Mauro Carvalho Chehab285d55a2011-12-26 13:03:00 -03001437 p->guard_interval = stv0367_readbits(state, F367TER_SYR_GUARD);
Igor M. Liplianin17cce932011-01-25 17:02:00 -03001438
Mauro Carvalho Chehab7c995072014-09-03 15:10:25 -03001439 return 0;
Igor M. Liplianin17cce932011-01-25 17:02:00 -03001440}
1441
Daniel Schellerb138afd2017-06-21 16:45:42 -03001442static u32 stv0367ter_snr_readreg(struct dvb_frontend *fe)
Igor M. Liplianin17cce932011-01-25 17:02:00 -03001443{
1444 struct stv0367_state *state = fe->demodulator_priv;
1445 u32 snru32 = 0;
1446 int cpt = 0;
1447 u8 cut = stv0367_readbits(state, F367TER_IDENTIFICATIONREG);
1448
1449 while (cpt < 10) {
1450 usleep_range(2000, 3000);
1451 if (cut == 0x50) /*cut 1.0 cut 1.1*/
1452 snru32 += stv0367_readbits(state, F367TER_CHCSNR) / 4;
1453 else /*cu2.0*/
1454 snru32 += 125 * stv0367_readbits(state, F367TER_CHCSNR);
1455
1456 cpt++;
1457 }
Igor M. Liplianin17cce932011-01-25 17:02:00 -03001458 snru32 /= 10;/*average on 10 values*/
1459
Daniel Schellerb138afd2017-06-21 16:45:42 -03001460 return snru32;
1461}
1462
1463static int stv0367ter_read_snr(struct dvb_frontend *fe, u16 *snr)
1464{
1465 u32 snrval = stv0367ter_snr_readreg(fe);
1466
1467 *snr = snrval / 1000;
Igor M. Liplianin17cce932011-01-25 17:02:00 -03001468
1469 return 0;
1470}
1471
1472#if 0
1473static int stv0367ter_status(struct dvb_frontend *fe)
1474{
1475
1476 struct stv0367_state *state = fe->demodulator_priv;
1477 struct stv0367ter_state *ter_state = state->ter_state;
1478 int locked = FALSE;
1479
1480 locked = (stv0367_readbits(state, F367TER_LK));
1481 if (!locked)
1482 ter_state->unlock_counter += 1;
1483 else
1484 ter_state->unlock_counter = 0;
1485
1486 if (ter_state->unlock_counter > 2) {
1487 if (!stv0367_readbits(state, F367TER_TPS_LOCK) ||
1488 (!stv0367_readbits(state, F367TER_LK))) {
1489 stv0367_writebits(state, F367TER_CORE_ACTIVE, 0);
1490 usleep_range(2000, 3000);
1491 stv0367_writebits(state, F367TER_CORE_ACTIVE, 1);
1492 msleep(350);
1493 locked = (stv0367_readbits(state, F367TER_TPS_LOCK)) &&
1494 (stv0367_readbits(state, F367TER_LK));
1495 }
1496
1497 }
1498
1499 return locked;
1500}
1501#endif
Mauro Carvalho Chehab0df289a2015-06-07 14:53:52 -03001502static int stv0367ter_read_status(struct dvb_frontend *fe,
1503 enum fe_status *status)
Igor M. Liplianin17cce932011-01-25 17:02:00 -03001504{
1505 struct stv0367_state *state = fe->demodulator_priv;
1506
1507 dprintk("%s:\n", __func__);
1508
1509 *status = 0;
1510
1511 if (stv0367_readbits(state, F367TER_LK)) {
1512 *status |= FE_HAS_LOCK;
1513 dprintk("%s: stv0367 has locked\n", __func__);
1514 }
1515
1516 return 0;
1517}
1518
1519static int stv0367ter_read_ber(struct dvb_frontend *fe, u32 *ber)
1520{
1521 struct stv0367_state *state = fe->demodulator_priv;
1522 struct stv0367ter_state *ter_state = state->ter_state;
1523 u32 Errors = 0, tber = 0, temporary = 0;
1524 int abc = 0, def = 0;
1525
1526
1527 /*wait for counting completion*/
1528 if (stv0367_readbits(state, F367TER_SFERRC_OLDVALUE) == 0)
1529 Errors = ((u32)stv0367_readbits(state, F367TER_SFEC_ERR_CNT)
1530 * (1 << 16))
1531 + ((u32)stv0367_readbits(state, F367TER_SFEC_ERR_CNT_HI)
1532 * (1 << 8))
1533 + ((u32)stv0367_readbits(state,
1534 F367TER_SFEC_ERR_CNT_LO));
1535 /*measurement not completed, load previous value*/
1536 else {
1537 tber = ter_state->pBER;
1538 return 0;
1539 }
1540
1541 abc = stv0367_readbits(state, F367TER_SFEC_ERR_SOURCE);
1542 def = stv0367_readbits(state, F367TER_SFEC_NUM_EVENT);
1543
1544 if (Errors == 0) {
1545 tber = 0;
1546 } else if (abc == 0x7) {
1547 if (Errors <= 4) {
1548 temporary = (Errors * 1000000000) / (8 * (1 << 14));
1549 temporary = temporary;
1550 } else if (Errors <= 42) {
1551 temporary = (Errors * 100000000) / (8 * (1 << 14));
1552 temporary = temporary * 10;
1553 } else if (Errors <= 429) {
1554 temporary = (Errors * 10000000) / (8 * (1 << 14));
1555 temporary = temporary * 100;
1556 } else if (Errors <= 4294) {
1557 temporary = (Errors * 1000000) / (8 * (1 << 14));
1558 temporary = temporary * 1000;
1559 } else if (Errors <= 42949) {
1560 temporary = (Errors * 100000) / (8 * (1 << 14));
1561 temporary = temporary * 10000;
1562 } else if (Errors <= 429496) {
1563 temporary = (Errors * 10000) / (8 * (1 << 14));
1564 temporary = temporary * 100000;
1565 } else { /*if (Errors<4294967) 2^22 max error*/
1566 temporary = (Errors * 1000) / (8 * (1 << 14));
1567 temporary = temporary * 100000; /* still to *10 */
1568 }
1569
1570 /* Byte error*/
1571 if (def == 2)
1572 /*tber=Errors/(8*(1 <<14));*/
1573 tber = temporary;
1574 else if (def == 3)
1575 /*tber=Errors/(8*(1 <<16));*/
1576 tber = temporary / 4;
1577 else if (def == 4)
1578 /*tber=Errors/(8*(1 <<18));*/
1579 tber = temporary / 16;
1580 else if (def == 5)
1581 /*tber=Errors/(8*(1 <<20));*/
1582 tber = temporary / 64;
1583 else if (def == 6)
1584 /*tber=Errors/(8*(1 <<22));*/
1585 tber = temporary / 256;
1586 else
1587 /* should not pass here*/
1588 tber = 0;
1589
1590 if ((Errors < 4294967) && (Errors > 429496))
1591 tber *= 10;
1592
1593 }
1594
1595 /* save actual value */
1596 ter_state->pBER = tber;
1597
1598 (*ber) = tber;
1599
1600 return 0;
1601}
1602#if 0
1603static u32 stv0367ter_get_per(struct stv0367_state *state)
1604{
1605 struct stv0367ter_state *ter_state = state->ter_state;
1606 u32 Errors = 0, Per = 0, temporary = 0;
1607 int abc = 0, def = 0, cpt = 0;
1608
1609 while (((stv0367_readbits(state, F367TER_SFERRC_OLDVALUE) == 1) &&
1610 (cpt < 400)) || ((Errors == 0) && (cpt < 400))) {
1611 usleep_range(1000, 2000);
1612 Errors = ((u32)stv0367_readbits(state, F367TER_ERR_CNT1)
1613 * (1 << 16))
1614 + ((u32)stv0367_readbits(state, F367TER_ERR_CNT1_HI)
1615 * (1 << 8))
1616 + ((u32)stv0367_readbits(state, F367TER_ERR_CNT1_LO));
1617 cpt++;
1618 }
1619 abc = stv0367_readbits(state, F367TER_ERR_SRC1);
1620 def = stv0367_readbits(state, F367TER_NUM_EVT1);
1621
1622 if (Errors == 0)
1623 Per = 0;
1624 else if (abc == 0x9) {
1625 if (Errors <= 4) {
1626 temporary = (Errors * 1000000000) / (8 * (1 << 8));
1627 temporary = temporary;
1628 } else if (Errors <= 42) {
1629 temporary = (Errors * 100000000) / (8 * (1 << 8));
1630 temporary = temporary * 10;
1631 } else if (Errors <= 429) {
1632 temporary = (Errors * 10000000) / (8 * (1 << 8));
1633 temporary = temporary * 100;
1634 } else if (Errors <= 4294) {
1635 temporary = (Errors * 1000000) / (8 * (1 << 8));
1636 temporary = temporary * 1000;
1637 } else if (Errors <= 42949) {
1638 temporary = (Errors * 100000) / (8 * (1 << 8));
1639 temporary = temporary * 10000;
1640 } else { /*if(Errors<=429496) 2^16 errors max*/
1641 temporary = (Errors * 10000) / (8 * (1 << 8));
1642 temporary = temporary * 100000;
1643 }
1644
1645 /* pkt error*/
1646 if (def == 2)
1647 /*Per=Errors/(1 << 8);*/
1648 Per = temporary;
1649 else if (def == 3)
1650 /*Per=Errors/(1 << 10);*/
1651 Per = temporary / 4;
1652 else if (def == 4)
1653 /*Per=Errors/(1 << 12);*/
1654 Per = temporary / 16;
1655 else if (def == 5)
1656 /*Per=Errors/(1 << 14);*/
1657 Per = temporary / 64;
1658 else if (def == 6)
1659 /*Per=Errors/(1 << 16);*/
1660 Per = temporary / 256;
1661 else
1662 Per = 0;
1663
1664 }
1665 /* save actual value */
1666 ter_state->pPER = Per;
1667
1668 return Per;
1669}
1670#endif
1671static int stv0367_get_tune_settings(struct dvb_frontend *fe,
1672 struct dvb_frontend_tune_settings
1673 *fe_tune_settings)
1674{
1675 fe_tune_settings->min_delay_ms = 1000;
1676 fe_tune_settings->step_size = 0;
1677 fe_tune_settings->max_drift = 0;
1678
1679 return 0;
1680}
1681
1682static void stv0367_release(struct dvb_frontend *fe)
1683{
1684 struct stv0367_state *state = fe->demodulator_priv;
1685
1686 kfree(state->ter_state);
1687 kfree(state->cab_state);
1688 kfree(state);
1689}
1690
Max Kellermannbd336e62016-08-09 18:32:21 -03001691static const struct dvb_frontend_ops stv0367ter_ops = {
Mauro Carvalho Chehab285d55a2011-12-26 13:03:00 -03001692 .delsys = { SYS_DVBT },
Igor M. Liplianin17cce932011-01-25 17:02:00 -03001693 .info = {
1694 .name = "ST STV0367 DVB-T",
Igor M. Liplianin17cce932011-01-25 17:02:00 -03001695 .frequency_min = 47000000,
1696 .frequency_max = 862000000,
1697 .frequency_stepsize = 15625,
1698 .frequency_tolerance = 0,
1699 .caps = FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 |
1700 FE_CAN_FEC_3_4 | FE_CAN_FEC_5_6 | FE_CAN_FEC_7_8 |
1701 FE_CAN_FEC_AUTO |
1702 FE_CAN_QPSK | FE_CAN_QAM_16 | FE_CAN_QAM_64 |
1703 FE_CAN_QAM_128 | FE_CAN_QAM_256 | FE_CAN_QAM_AUTO |
1704 FE_CAN_TRANSMISSION_MODE_AUTO | FE_CAN_RECOVER |
1705 FE_CAN_INVERSION_AUTO |
1706 FE_CAN_MUTE_TS
1707 },
1708 .release = stv0367_release,
1709 .init = stv0367ter_init,
1710 .sleep = stv0367ter_sleep,
1711 .i2c_gate_ctrl = stv0367ter_gate_ctrl,
Mauro Carvalho Chehab285d55a2011-12-26 13:03:00 -03001712 .set_frontend = stv0367ter_set_frontend,
1713 .get_frontend = stv0367ter_get_frontend,
Igor M. Liplianin17cce932011-01-25 17:02:00 -03001714 .get_tune_settings = stv0367_get_tune_settings,
1715 .read_status = stv0367ter_read_status,
1716 .read_ber = stv0367ter_read_ber,/* too slow */
1717/* .read_signal_strength = stv0367_read_signal_strength,*/
1718 .read_snr = stv0367ter_read_snr,
1719 .read_ucblocks = stv0367ter_read_ucblocks,
1720};
1721
1722struct dvb_frontend *stv0367ter_attach(const struct stv0367_config *config,
1723 struct i2c_adapter *i2c)
1724{
1725 struct stv0367_state *state = NULL;
1726 struct stv0367ter_state *ter_state = NULL;
1727
1728 /* allocate memory for the internal state */
1729 state = kzalloc(sizeof(struct stv0367_state), GFP_KERNEL);
1730 if (state == NULL)
1731 goto error;
1732 ter_state = kzalloc(sizeof(struct stv0367ter_state), GFP_KERNEL);
1733 if (ter_state == NULL)
1734 goto error;
1735
1736 /* setup the state */
1737 state->i2c = i2c;
1738 state->config = config;
1739 state->ter_state = ter_state;
1740 state->fe.ops = stv0367ter_ops;
1741 state->fe.demodulator_priv = state;
1742 state->chip_id = stv0367_readreg(state, 0xf000);
1743
Daniel Schellerf61c2992017-03-29 13:43:01 -03001744 /* demod operation options */
1745 state->use_i2c_gatectrl = 1;
Daniel Scheller8881ceb2017-03-29 13:43:04 -03001746 state->deftabs = STV0367_DEFTAB_GENERIC;
Daniel Scheller7718dcf2017-03-29 13:43:06 -03001747 state->reinit_on_setfrontend = 1;
Daniel Schellerb16338e2017-03-29 13:43:07 -03001748 state->auto_if_khz = 0;
Daniel Schellerf61c2992017-03-29 13:43:01 -03001749
Igor M. Liplianin17cce932011-01-25 17:02:00 -03001750 dprintk("%s: chip_id = 0x%x\n", __func__, state->chip_id);
1751
1752 /* check if the demod is there */
1753 if ((state->chip_id != 0x50) && (state->chip_id != 0x60))
1754 goto error;
1755
1756 return &state->fe;
1757
1758error:
1759 kfree(ter_state);
1760 kfree(state);
1761 return NULL;
1762}
1763EXPORT_SYMBOL(stv0367ter_attach);
1764
1765static int stv0367cab_gate_ctrl(struct dvb_frontend *fe, int enable)
1766{
1767 struct stv0367_state *state = fe->demodulator_priv;
1768
1769 dprintk("%s:\n", __func__);
1770
1771 stv0367_writebits(state, F367CAB_I2CT_ON, (enable > 0) ? 1 : 0);
1772
1773 return 0;
1774}
1775
1776static u32 stv0367cab_get_mclk(struct dvb_frontend *fe, u32 ExtClk_Hz)
1777{
1778 struct stv0367_state *state = fe->demodulator_priv;
1779 u32 mclk_Hz = 0;/* master clock frequency (Hz) */
1780 u32 M, N, P;
1781
1782
1783 if (stv0367_readbits(state, F367CAB_BYPASS_PLLXN) == 0) {
1784 N = (u32)stv0367_readbits(state, F367CAB_PLL_NDIV);
1785 if (N == 0)
1786 N = N + 1;
1787
1788 M = (u32)stv0367_readbits(state, F367CAB_PLL_MDIV);
1789 if (M == 0)
1790 M = M + 1;
1791
1792 P = (u32)stv0367_readbits(state, F367CAB_PLL_PDIV);
1793
1794 if (P > 5)
1795 P = 5;
1796
1797 mclk_Hz = ((ExtClk_Hz / 2) * N) / (M * (1 << P));
1798 dprintk("stv0367cab_get_mclk BYPASS_PLLXN mclk_Hz=%d\n",
1799 mclk_Hz);
1800 } else
1801 mclk_Hz = ExtClk_Hz;
1802
1803 dprintk("stv0367cab_get_mclk final mclk_Hz=%d\n", mclk_Hz);
1804
1805 return mclk_Hz;
1806}
1807
1808static u32 stv0367cab_get_adc_freq(struct dvb_frontend *fe, u32 ExtClk_Hz)
1809{
1810 u32 ADCClk_Hz = ExtClk_Hz;
1811
1812 ADCClk_Hz = stv0367cab_get_mclk(fe, ExtClk_Hz);
1813
1814 return ADCClk_Hz;
1815}
1816
Mauro Carvalho Chehab8c8ca1c2012-10-27 11:26:42 -03001817static enum stv0367cab_mod stv0367cab_SetQamSize(struct stv0367_state *state,
1818 u32 SymbolRate,
1819 enum stv0367cab_mod QAMSize)
Igor M. Liplianin17cce932011-01-25 17:02:00 -03001820{
1821 /* Set QAM size */
1822 stv0367_writebits(state, F367CAB_QAM_MODE, QAMSize);
1823
1824 /* Set Registers settings specific to the QAM size */
1825 switch (QAMSize) {
1826 case FE_CAB_MOD_QAM4:
1827 stv0367_writereg(state, R367CAB_IQDEM_ADJ_AGC_REF, 0x00);
1828 break;
1829 case FE_CAB_MOD_QAM16:
1830 stv0367_writereg(state, R367CAB_AGC_PWR_REF_L, 0x64);
1831 stv0367_writereg(state, R367CAB_IQDEM_ADJ_AGC_REF, 0x00);
1832 stv0367_writereg(state, R367CAB_FSM_STATE, 0x90);
1833 stv0367_writereg(state, R367CAB_EQU_CTR_LPF_GAIN, 0xc1);
1834 stv0367_writereg(state, R367CAB_EQU_CRL_LPF_GAIN, 0xa7);
1835 stv0367_writereg(state, R367CAB_EQU_CRL_LD_SEN, 0x95);
1836 stv0367_writereg(state, R367CAB_EQU_CRL_LIMITER, 0x40);
1837 stv0367_writereg(state, R367CAB_EQU_PNT_GAIN, 0x8a);
1838 break;
1839 case FE_CAB_MOD_QAM32:
1840 stv0367_writereg(state, R367CAB_IQDEM_ADJ_AGC_REF, 0x00);
1841 stv0367_writereg(state, R367CAB_AGC_PWR_REF_L, 0x6e);
1842 stv0367_writereg(state, R367CAB_FSM_STATE, 0xb0);
1843 stv0367_writereg(state, R367CAB_EQU_CTR_LPF_GAIN, 0xc1);
1844 stv0367_writereg(state, R367CAB_EQU_CRL_LPF_GAIN, 0xb7);
1845 stv0367_writereg(state, R367CAB_EQU_CRL_LD_SEN, 0x9d);
1846 stv0367_writereg(state, R367CAB_EQU_CRL_LIMITER, 0x7f);
1847 stv0367_writereg(state, R367CAB_EQU_PNT_GAIN, 0xa7);
1848 break;
1849 case FE_CAB_MOD_QAM64:
1850 stv0367_writereg(state, R367CAB_IQDEM_ADJ_AGC_REF, 0x82);
1851 stv0367_writereg(state, R367CAB_AGC_PWR_REF_L, 0x5a);
Daniel Scheller8b39f072017-03-29 13:43:09 -03001852 if (SymbolRate > 4500000) {
Igor M. Liplianin17cce932011-01-25 17:02:00 -03001853 stv0367_writereg(state, R367CAB_FSM_STATE, 0xb0);
1854 stv0367_writereg(state, R367CAB_EQU_CTR_LPF_GAIN, 0xc1);
1855 stv0367_writereg(state, R367CAB_EQU_CRL_LPF_GAIN, 0xa5);
Daniel Scheller8b39f072017-03-29 13:43:09 -03001856 } else if (SymbolRate > 2500000) {
Igor M. Liplianin17cce932011-01-25 17:02:00 -03001857 stv0367_writereg(state, R367CAB_FSM_STATE, 0xa0);
1858 stv0367_writereg(state, R367CAB_EQU_CTR_LPF_GAIN, 0xc1);
1859 stv0367_writereg(state, R367CAB_EQU_CRL_LPF_GAIN, 0xa6);
1860 } else {
1861 stv0367_writereg(state, R367CAB_FSM_STATE, 0xa0);
1862 stv0367_writereg(state, R367CAB_EQU_CTR_LPF_GAIN, 0xd1);
1863 stv0367_writereg(state, R367CAB_EQU_CRL_LPF_GAIN, 0xa7);
1864 }
1865 stv0367_writereg(state, R367CAB_EQU_CRL_LD_SEN, 0x95);
1866 stv0367_writereg(state, R367CAB_EQU_CRL_LIMITER, 0x40);
1867 stv0367_writereg(state, R367CAB_EQU_PNT_GAIN, 0x99);
1868 break;
1869 case FE_CAB_MOD_QAM128:
1870 stv0367_writereg(state, R367CAB_IQDEM_ADJ_AGC_REF, 0x00);
1871 stv0367_writereg(state, R367CAB_AGC_PWR_REF_L, 0x76);
1872 stv0367_writereg(state, R367CAB_FSM_STATE, 0x90);
1873 stv0367_writereg(state, R367CAB_EQU_CTR_LPF_GAIN, 0xb1);
Daniel Scheller8b39f072017-03-29 13:43:09 -03001874 if (SymbolRate > 4500000)
Igor M. Liplianin17cce932011-01-25 17:02:00 -03001875 stv0367_writereg(state, R367CAB_EQU_CRL_LPF_GAIN, 0xa7);
Daniel Scheller8b39f072017-03-29 13:43:09 -03001876 else if (SymbolRate > 2500000)
Igor M. Liplianin17cce932011-01-25 17:02:00 -03001877 stv0367_writereg(state, R367CAB_EQU_CRL_LPF_GAIN, 0xa6);
1878 else
1879 stv0367_writereg(state, R367CAB_EQU_CRL_LPF_GAIN, 0x97);
1880
1881 stv0367_writereg(state, R367CAB_EQU_CRL_LD_SEN, 0x8e);
1882 stv0367_writereg(state, R367CAB_EQU_CRL_LIMITER, 0x7f);
1883 stv0367_writereg(state, R367CAB_EQU_PNT_GAIN, 0xa7);
1884 break;
1885 case FE_CAB_MOD_QAM256:
1886 stv0367_writereg(state, R367CAB_IQDEM_ADJ_AGC_REF, 0x94);
1887 stv0367_writereg(state, R367CAB_AGC_PWR_REF_L, 0x5a);
1888 stv0367_writereg(state, R367CAB_FSM_STATE, 0xa0);
Daniel Scheller8b39f072017-03-29 13:43:09 -03001889 if (SymbolRate > 4500000)
Igor M. Liplianin17cce932011-01-25 17:02:00 -03001890 stv0367_writereg(state, R367CAB_EQU_CTR_LPF_GAIN, 0xc1);
Daniel Scheller8b39f072017-03-29 13:43:09 -03001891 else if (SymbolRate > 2500000)
Igor M. Liplianin17cce932011-01-25 17:02:00 -03001892 stv0367_writereg(state, R367CAB_EQU_CTR_LPF_GAIN, 0xc1);
1893 else
1894 stv0367_writereg(state, R367CAB_EQU_CTR_LPF_GAIN, 0xd1);
1895
1896 stv0367_writereg(state, R367CAB_EQU_CRL_LPF_GAIN, 0xa7);
1897 stv0367_writereg(state, R367CAB_EQU_CRL_LD_SEN, 0x85);
1898 stv0367_writereg(state, R367CAB_EQU_CRL_LIMITER, 0x40);
1899 stv0367_writereg(state, R367CAB_EQU_PNT_GAIN, 0xa7);
1900 break;
1901 case FE_CAB_MOD_QAM512:
1902 stv0367_writereg(state, R367CAB_IQDEM_ADJ_AGC_REF, 0x00);
1903 break;
1904 case FE_CAB_MOD_QAM1024:
1905 stv0367_writereg(state, R367CAB_IQDEM_ADJ_AGC_REF, 0x00);
1906 break;
1907 default:
1908 break;
1909 }
1910
1911 return QAMSize;
1912}
1913
1914static u32 stv0367cab_set_derot_freq(struct stv0367_state *state,
1915 u32 adc_hz, s32 derot_hz)
1916{
1917 u32 sampled_if = 0;
1918 u32 adc_khz;
1919
1920 adc_khz = adc_hz / 1000;
1921
1922 dprintk("%s: adc_hz=%d derot_hz=%d\n", __func__, adc_hz, derot_hz);
1923
1924 if (adc_khz != 0) {
1925 if (derot_hz < 1000000)
1926 derot_hz = adc_hz / 4; /* ZIF operation */
1927 if (derot_hz > adc_hz)
1928 derot_hz = derot_hz - adc_hz;
1929 sampled_if = (u32)derot_hz / 1000;
1930 sampled_if *= 32768;
1931 sampled_if /= adc_khz;
1932 sampled_if *= 256;
1933 }
1934
1935 if (sampled_if > 8388607)
1936 sampled_if = 8388607;
1937
1938 dprintk("%s: sampled_if=0x%x\n", __func__, sampled_if);
1939
1940 stv0367_writereg(state, R367CAB_MIX_NCO_LL, sampled_if);
1941 stv0367_writereg(state, R367CAB_MIX_NCO_HL, (sampled_if >> 8));
1942 stv0367_writebits(state, F367CAB_MIX_NCO_INC_HH, (sampled_if >> 16));
1943
1944 return derot_hz;
1945}
1946
1947static u32 stv0367cab_get_derot_freq(struct stv0367_state *state, u32 adc_hz)
1948{
1949 u32 sampled_if;
1950
1951 sampled_if = stv0367_readbits(state, F367CAB_MIX_NCO_INC_LL) +
1952 (stv0367_readbits(state, F367CAB_MIX_NCO_INC_HL) << 8) +
1953 (stv0367_readbits(state, F367CAB_MIX_NCO_INC_HH) << 16);
1954
1955 sampled_if /= 256;
1956 sampled_if *= (adc_hz / 1000);
1957 sampled_if += 1;
1958 sampled_if /= 32768;
1959
1960 return sampled_if;
1961}
1962
1963static u32 stv0367cab_set_srate(struct stv0367_state *state, u32 adc_hz,
1964 u32 mclk_hz, u32 SymbolRate,
1965 enum stv0367cab_mod QAMSize)
1966{
1967 u32 QamSizeCorr = 0;
1968 u32 u32_tmp = 0, u32_tmp1 = 0;
1969 u32 adp_khz;
1970
1971 dprintk("%s:\n", __func__);
1972
1973 /* Set Correction factor of SRC gain */
1974 switch (QAMSize) {
1975 case FE_CAB_MOD_QAM4:
1976 QamSizeCorr = 1110;
1977 break;
1978 case FE_CAB_MOD_QAM16:
1979 QamSizeCorr = 1032;
1980 break;
1981 case FE_CAB_MOD_QAM32:
1982 QamSizeCorr = 954;
1983 break;
1984 case FE_CAB_MOD_QAM64:
1985 QamSizeCorr = 983;
1986 break;
1987 case FE_CAB_MOD_QAM128:
1988 QamSizeCorr = 957;
1989 break;
1990 case FE_CAB_MOD_QAM256:
1991 QamSizeCorr = 948;
1992 break;
1993 case FE_CAB_MOD_QAM512:
1994 QamSizeCorr = 0;
1995 break;
1996 case FE_CAB_MOD_QAM1024:
1997 QamSizeCorr = 944;
1998 break;
1999 default:
2000 break;
2001 }
2002
2003 /* Transfer ratio calculation */
2004 if (adc_hz != 0) {
2005 u32_tmp = 256 * SymbolRate;
2006 u32_tmp = u32_tmp / adc_hz;
2007 }
2008 stv0367_writereg(state, R367CAB_EQU_CRL_TFR, (u8)u32_tmp);
2009
2010 /* Symbol rate and SRC gain calculation */
2011 adp_khz = (mclk_hz >> 1) / 1000;/* TRL works at half the system clock */
2012 if (adp_khz != 0) {
2013 u32_tmp = SymbolRate;
2014 u32_tmp1 = SymbolRate;
2015
2016 if (u32_tmp < 2097152) { /* 2097152 = 2^21 */
2017 /* Symbol rate calculation */
2018 u32_tmp *= 2048; /* 2048 = 2^11 */
2019 u32_tmp = u32_tmp / adp_khz;
2020 u32_tmp = u32_tmp * 16384; /* 16384 = 2^14 */
2021 u32_tmp /= 125 ; /* 125 = 1000/2^3 */
2022 u32_tmp = u32_tmp * 8; /* 8 = 2^3 */
2023
2024 /* SRC Gain Calculation */
2025 u32_tmp1 *= 2048; /* *2*2^10 */
2026 u32_tmp1 /= 439; /* *2/878 */
2027 u32_tmp1 *= 256; /* *2^8 */
2028 u32_tmp1 = u32_tmp1 / adp_khz; /* /(AdpClk in kHz) */
2029 u32_tmp1 *= QamSizeCorr * 9; /* *1000*corr factor */
2030 u32_tmp1 = u32_tmp1 / 10000000;
2031
2032 } else if (u32_tmp < 4194304) { /* 4194304 = 2**22 */
2033 /* Symbol rate calculation */
2034 u32_tmp *= 1024 ; /* 1024 = 2**10 */
2035 u32_tmp = u32_tmp / adp_khz;
2036 u32_tmp = u32_tmp * 16384; /* 16384 = 2**14 */
2037 u32_tmp /= 125 ; /* 125 = 1000/2**3 */
2038 u32_tmp = u32_tmp * 16; /* 16 = 2**4 */
2039
2040 /* SRC Gain Calculation */
2041 u32_tmp1 *= 1024; /* *2*2^9 */
2042 u32_tmp1 /= 439; /* *2/878 */
2043 u32_tmp1 *= 256; /* *2^8 */
2044 u32_tmp1 = u32_tmp1 / adp_khz; /* /(AdpClk in kHz)*/
2045 u32_tmp1 *= QamSizeCorr * 9; /* *1000*corr factor */
2046 u32_tmp1 = u32_tmp1 / 5000000;
2047 } else if (u32_tmp < 8388607) { /* 8388607 = 2**23 */
2048 /* Symbol rate calculation */
2049 u32_tmp *= 512 ; /* 512 = 2**9 */
2050 u32_tmp = u32_tmp / adp_khz;
2051 u32_tmp = u32_tmp * 16384; /* 16384 = 2**14 */
2052 u32_tmp /= 125 ; /* 125 = 1000/2**3 */
2053 u32_tmp = u32_tmp * 32; /* 32 = 2**5 */
2054
2055 /* SRC Gain Calculation */
2056 u32_tmp1 *= 512; /* *2*2^8 */
2057 u32_tmp1 /= 439; /* *2/878 */
2058 u32_tmp1 *= 256; /* *2^8 */
2059 u32_tmp1 = u32_tmp1 / adp_khz; /* /(AdpClk in kHz) */
2060 u32_tmp1 *= QamSizeCorr * 9; /* *1000*corr factor */
2061 u32_tmp1 = u32_tmp1 / 2500000;
2062 } else {
2063 /* Symbol rate calculation */
2064 u32_tmp *= 256 ; /* 256 = 2**8 */
2065 u32_tmp = u32_tmp / adp_khz;
2066 u32_tmp = u32_tmp * 16384; /* 16384 = 2**13 */
2067 u32_tmp /= 125 ; /* 125 = 1000/2**3 */
2068 u32_tmp = u32_tmp * 64; /* 64 = 2**6 */
2069
2070 /* SRC Gain Calculation */
2071 u32_tmp1 *= 256; /* 2*2^7 */
2072 u32_tmp1 /= 439; /* *2/878 */
2073 u32_tmp1 *= 256; /* *2^8 */
2074 u32_tmp1 = u32_tmp1 / adp_khz; /* /(AdpClk in kHz) */
2075 u32_tmp1 *= QamSizeCorr * 9; /* *1000*corr factor */
2076 u32_tmp1 = u32_tmp1 / 1250000;
2077 }
2078 }
2079#if 0
2080 /* Filters' coefficients are calculated and written
2081 into registers only if the filters are enabled */
2082 if (stv0367_readbits(state, F367CAB_ADJ_EN)) {
2083 stv0367cab_SetIirAdjacentcoefficient(state, mclk_hz,
2084 SymbolRate);
2085 /* AllPass filter must be enabled
2086 when the adjacents filter is used */
2087 stv0367_writebits(state, F367CAB_ALLPASSFILT_EN, 1);
2088 stv0367cab_SetAllPasscoefficient(state, mclk_hz, SymbolRate);
2089 } else
2090 /* AllPass filter must be disabled
2091 when the adjacents filter is not used */
2092#endif
2093 stv0367_writebits(state, F367CAB_ALLPASSFILT_EN, 0);
2094
2095 stv0367_writereg(state, R367CAB_SRC_NCO_LL, u32_tmp);
2096 stv0367_writereg(state, R367CAB_SRC_NCO_LH, (u32_tmp >> 8));
2097 stv0367_writereg(state, R367CAB_SRC_NCO_HL, (u32_tmp >> 16));
2098 stv0367_writereg(state, R367CAB_SRC_NCO_HH, (u32_tmp >> 24));
2099
2100 stv0367_writereg(state, R367CAB_IQDEM_GAIN_SRC_L, u32_tmp1 & 0x00ff);
2101 stv0367_writebits(state, F367CAB_GAIN_SRC_HI, (u32_tmp1 >> 8) & 0x00ff);
2102
2103 return SymbolRate ;
2104}
2105
2106static u32 stv0367cab_GetSymbolRate(struct stv0367_state *state, u32 mclk_hz)
2107{
2108 u32 regsym;
2109 u32 adp_khz;
2110
2111 regsym = stv0367_readreg(state, R367CAB_SRC_NCO_LL) +
2112 (stv0367_readreg(state, R367CAB_SRC_NCO_LH) << 8) +
2113 (stv0367_readreg(state, R367CAB_SRC_NCO_HL) << 16) +
2114 (stv0367_readreg(state, R367CAB_SRC_NCO_HH) << 24);
2115
2116 adp_khz = (mclk_hz >> 1) / 1000;/* TRL works at half the system clock */
2117
2118 if (regsym < 134217728) { /* 134217728L = 2**27*/
2119 regsym = regsym * 32; /* 32 = 2**5 */
2120 regsym = regsym / 32768; /* 32768L = 2**15 */
2121 regsym = adp_khz * regsym; /* AdpClk in kHz */
2122 regsym = regsym / 128; /* 128 = 2**7 */
2123 regsym *= 125 ; /* 125 = 1000/2**3 */
2124 regsym /= 2048 ; /* 2048 = 2**11 */
2125 } else if (regsym < 268435456) { /* 268435456L = 2**28 */
2126 regsym = regsym * 16; /* 16 = 2**4 */
2127 regsym = regsym / 32768; /* 32768L = 2**15 */
2128 regsym = adp_khz * regsym; /* AdpClk in kHz */
2129 regsym = regsym / 128; /* 128 = 2**7 */
2130 regsym *= 125 ; /* 125 = 1000/2**3*/
2131 regsym /= 1024 ; /* 256 = 2**10*/
2132 } else if (regsym < 536870912) { /* 536870912L = 2**29*/
2133 regsym = regsym * 8; /* 8 = 2**3 */
2134 regsym = regsym / 32768; /* 32768L = 2**15 */
2135 regsym = adp_khz * regsym; /* AdpClk in kHz */
2136 regsym = regsym / 128; /* 128 = 2**7 */
2137 regsym *= 125 ; /* 125 = 1000/2**3 */
2138 regsym /= 512 ; /* 128 = 2**9 */
2139 } else {
2140 regsym = regsym * 4; /* 4 = 2**2 */
2141 regsym = regsym / 32768; /* 32768L = 2**15 */
2142 regsym = adp_khz * regsym; /* AdpClk in kHz */
2143 regsym = regsym / 128; /* 128 = 2**7 */
2144 regsym *= 125 ; /* 125 = 1000/2**3 */
2145 regsym /= 256 ; /* 64 = 2**8 */
2146 }
2147
2148 return regsym;
2149}
2150
Mauro Carvalho Chehab0df289a2015-06-07 14:53:52 -03002151static int stv0367cab_read_status(struct dvb_frontend *fe,
2152 enum fe_status *status)
Igor M. Liplianin17cce932011-01-25 17:02:00 -03002153{
2154 struct stv0367_state *state = fe->demodulator_priv;
2155
2156 dprintk("%s:\n", __func__);
2157
2158 *status = 0;
2159
Daniel Schellercae7a902017-03-29 13:43:08 -03002160 if (stv0367_readbits(state, (state->cab_state->qamfec_status_reg ?
2161 state->cab_state->qamfec_status_reg : F367CAB_QAMFEC_LOCK))) {
Igor M. Liplianin17cce932011-01-25 17:02:00 -03002162 *status |= FE_HAS_LOCK;
2163 dprintk("%s: stv0367 has locked\n", __func__);
2164 }
2165
2166 return 0;
2167}
2168
2169static int stv0367cab_standby(struct dvb_frontend *fe, u8 standby_on)
2170{
2171 struct stv0367_state *state = fe->demodulator_priv;
2172
2173 dprintk("%s:\n", __func__);
2174
2175 if (standby_on) {
2176 stv0367_writebits(state, F367CAB_BYPASS_PLLXN, 0x03);
2177 stv0367_writebits(state, F367CAB_STDBY_PLLXN, 0x01);
2178 stv0367_writebits(state, F367CAB_STDBY, 1);
2179 stv0367_writebits(state, F367CAB_STDBY_CORE, 1);
2180 stv0367_writebits(state, F367CAB_EN_BUFFER_I, 0);
2181 stv0367_writebits(state, F367CAB_EN_BUFFER_Q, 0);
2182 stv0367_writebits(state, F367CAB_POFFQ, 1);
2183 stv0367_writebits(state, F367CAB_POFFI, 1);
2184 } else {
2185 stv0367_writebits(state, F367CAB_STDBY_PLLXN, 0x00);
2186 stv0367_writebits(state, F367CAB_BYPASS_PLLXN, 0x00);
2187 stv0367_writebits(state, F367CAB_STDBY, 0);
2188 stv0367_writebits(state, F367CAB_STDBY_CORE, 0);
2189 stv0367_writebits(state, F367CAB_EN_BUFFER_I, 1);
2190 stv0367_writebits(state, F367CAB_EN_BUFFER_Q, 1);
2191 stv0367_writebits(state, F367CAB_POFFQ, 0);
2192 stv0367_writebits(state, F367CAB_POFFI, 0);
2193 }
2194
2195 return 0;
2196}
2197
2198static int stv0367cab_sleep(struct dvb_frontend *fe)
2199{
2200 return stv0367cab_standby(fe, 1);
2201}
2202
Mauro Carvalho Chehab8c8ca1c2012-10-27 11:26:42 -03002203static int stv0367cab_init(struct dvb_frontend *fe)
Igor M. Liplianin17cce932011-01-25 17:02:00 -03002204{
2205 struct stv0367_state *state = fe->demodulator_priv;
2206 struct stv0367cab_state *cab_state = state->cab_state;
Igor M. Liplianin17cce932011-01-25 17:02:00 -03002207
2208 dprintk("%s:\n", __func__);
2209
Daniel Scheller8881ceb2017-03-29 13:43:04 -03002210 stv0367_write_table(state,
2211 stv0367_deftabs[state->deftabs][STV0367_TAB_CAB]);
Igor M. Liplianin17cce932011-01-25 17:02:00 -03002212
2213 switch (state->config->ts_mode) {
2214 case STV0367_DVBCI_CLOCK:
2215 dprintk("Setting TSMode = STV0367_DVBCI_CLOCK\n");
2216 stv0367_writebits(state, F367CAB_OUTFORMAT, 0x03);
2217 break;
2218 case STV0367_SERIAL_PUNCT_CLOCK:
2219 case STV0367_SERIAL_CONT_CLOCK:
2220 stv0367_writebits(state, F367CAB_OUTFORMAT, 0x01);
2221 break;
2222 case STV0367_PARALLEL_PUNCT_CLOCK:
2223 case STV0367_OUTPUTMODE_DEFAULT:
2224 stv0367_writebits(state, F367CAB_OUTFORMAT, 0x00);
2225 break;
2226 }
2227
2228 switch (state->config->clk_pol) {
2229 case STV0367_RISINGEDGE_CLOCK:
2230 stv0367_writebits(state, F367CAB_CLK_POLARITY, 0x00);
2231 break;
2232 case STV0367_FALLINGEDGE_CLOCK:
2233 case STV0367_CLOCKPOLARITY_DEFAULT:
2234 stv0367_writebits(state, F367CAB_CLK_POLARITY, 0x01);
2235 break;
2236 }
2237
2238 stv0367_writebits(state, F367CAB_SYNC_STRIP, 0x00);
2239
2240 stv0367_writebits(state, F367CAB_CT_NBST, 0x01);
2241
2242 stv0367_writebits(state, F367CAB_TS_SWAP, 0x01);
2243
2244 stv0367_writebits(state, F367CAB_FIFO_BYPASS, 0x00);
2245
2246 stv0367_writereg(state, R367CAB_ANACTRL, 0x00);/*PLL enabled and used */
2247
2248 cab_state->mclk = stv0367cab_get_mclk(fe, state->config->xtal);
2249 cab_state->adc_clk = stv0367cab_get_adc_freq(fe, state->config->xtal);
2250
2251 return 0;
2252}
2253static
2254enum stv0367_cab_signal_type stv0367cab_algo(struct stv0367_state *state,
Mauro Carvalho Chehab285d55a2011-12-26 13:03:00 -03002255 struct dtv_frontend_properties *p)
Igor M. Liplianin17cce932011-01-25 17:02:00 -03002256{
Igor M. Liplianin17cce932011-01-25 17:02:00 -03002257 struct stv0367cab_state *cab_state = state->cab_state;
2258 enum stv0367_cab_signal_type signalType = FE_CAB_NOAGC;
Daniel Schellerb16338e2017-03-29 13:43:07 -03002259 u32 QAMFEC_Lock, QAM_Lock, u32_tmp, ifkhz,
Igor M. Liplianin17cce932011-01-25 17:02:00 -03002260 LockTime, TRLTimeOut, AGCTimeOut, CRLSymbols,
2261 CRLTimeOut, EQLTimeOut, DemodTimeOut, FECTimeOut;
2262 u8 TrackAGCAccum;
2263 s32 tmp;
2264
2265 dprintk("%s:\n", __func__);
2266
Daniel Schellerb16338e2017-03-29 13:43:07 -03002267 stv0367_get_if_khz(state, &ifkhz);
2268
Igor M. Liplianin17cce932011-01-25 17:02:00 -03002269 /* Timeouts calculation */
2270 /* A max lock time of 25 ms is allowed for delayed AGC */
2271 AGCTimeOut = 25;
2272 /* 100000 symbols needed by the TRL as a maximum value */
Mauro Carvalho Chehab285d55a2011-12-26 13:03:00 -03002273 TRLTimeOut = 100000000 / p->symbol_rate;
Igor M. Liplianin17cce932011-01-25 17:02:00 -03002274 /* CRLSymbols is the needed number of symbols to achieve a lock
2275 within [-4%, +4%] of the symbol rate.
2276 CRL timeout is calculated
2277 for a lock within [-search_range, +search_range].
2278 EQL timeout can be changed depending on
2279 the micro-reflections we want to handle.
2280 A characterization must be performed
2281 with these echoes to get new timeout values.
2282 */
Mauro Carvalho Chehab285d55a2011-12-26 13:03:00 -03002283 switch (p->modulation) {
Igor M. Liplianin17cce932011-01-25 17:02:00 -03002284 case QAM_16:
2285 CRLSymbols = 150000;
2286 EQLTimeOut = 100;
2287 break;
2288 case QAM_32:
2289 CRLSymbols = 250000;
2290 EQLTimeOut = 100;
2291 break;
2292 case QAM_64:
2293 CRLSymbols = 200000;
2294 EQLTimeOut = 100;
2295 break;
2296 case QAM_128:
2297 CRLSymbols = 250000;
2298 EQLTimeOut = 100;
2299 break;
2300 case QAM_256:
2301 CRLSymbols = 250000;
2302 EQLTimeOut = 100;
2303 break;
2304 default:
2305 CRLSymbols = 200000;
2306 EQLTimeOut = 100;
2307 break;
2308 }
2309#if 0
2310 if (pIntParams->search_range < 0) {
2311 CRLTimeOut = (25 * CRLSymbols *
2312 (-pIntParams->search_range / 1000)) /
2313 (pIntParams->symbol_rate / 1000);
2314 } else
2315#endif
2316 CRLTimeOut = (25 * CRLSymbols * (cab_state->search_range / 1000)) /
Mauro Carvalho Chehab285d55a2011-12-26 13:03:00 -03002317 (p->symbol_rate / 1000);
Igor M. Liplianin17cce932011-01-25 17:02:00 -03002318
Mauro Carvalho Chehab285d55a2011-12-26 13:03:00 -03002319 CRLTimeOut = (1000 * CRLTimeOut) / p->symbol_rate;
Igor M. Liplianin17cce932011-01-25 17:02:00 -03002320 /* Timeouts below 50ms are coerced */
2321 if (CRLTimeOut < 50)
2322 CRLTimeOut = 50;
2323 /* A maximum of 100 TS packets is needed to get FEC lock even in case
2324 the spectrum inversion needs to be changed.
2325 This is equal to 20 ms in case of the lowest symbol rate of 0.87Msps
2326 */
2327 FECTimeOut = 20;
2328 DemodTimeOut = AGCTimeOut + TRLTimeOut + CRLTimeOut + EQLTimeOut;
2329
2330 dprintk("%s: DemodTimeOut=%d\n", __func__, DemodTimeOut);
2331
2332 /* Reset the TRL to ensure nothing starts until the
2333 AGC is stable which ensures a better lock time
2334 */
2335 stv0367_writereg(state, R367CAB_CTRL_1, 0x04);
2336 /* Set AGC accumulation time to minimum and lock threshold to maximum
2337 in order to speed up the AGC lock */
2338 TrackAGCAccum = stv0367_readbits(state, F367CAB_AGC_ACCUMRSTSEL);
2339 stv0367_writebits(state, F367CAB_AGC_ACCUMRSTSEL, 0x0);
2340 /* Modulus Mapper is disabled */
2341 stv0367_writebits(state, F367CAB_MODULUSMAP_EN, 0);
2342 /* Disable the sweep function */
2343 stv0367_writebits(state, F367CAB_SWEEP_EN, 0);
2344 /* The sweep function is never used, Sweep rate must be set to 0 */
2345 /* Set the derotator frequency in Hz */
2346 stv0367cab_set_derot_freq(state, cab_state->adc_clk,
Daniel Schellerb16338e2017-03-29 13:43:07 -03002347 (1000 * (s32)ifkhz + cab_state->derot_offset));
Igor M. Liplianin17cce932011-01-25 17:02:00 -03002348 /* Disable the Allpass Filter when the symbol rate is out of range */
Mauro Carvalho Chehab285d55a2011-12-26 13:03:00 -03002349 if ((p->symbol_rate > 10800000) | (p->symbol_rate < 1800000)) {
Igor M. Liplianin17cce932011-01-25 17:02:00 -03002350 stv0367_writebits(state, F367CAB_ADJ_EN, 0);
2351 stv0367_writebits(state, F367CAB_ALLPASSFILT_EN, 0);
2352 }
2353#if 0
2354 /* Check if the tuner is locked */
2355 tuner_lock = stv0367cab_tuner_get_status(fe);
2356 if (tuner_lock == 0)
2357 return FE_367CAB_NOTUNER;
2358#endif
Geert Uytterhoeven83a35e32013-06-28 11:27:31 +02002359 /* Release the TRL to start demodulator acquisition */
Igor M. Liplianin17cce932011-01-25 17:02:00 -03002360 /* Wait for QAM lock */
2361 LockTime = 0;
2362 stv0367_writereg(state, R367CAB_CTRL_1, 0x00);
2363 do {
2364 QAM_Lock = stv0367_readbits(state, F367CAB_FSM_STATUS);
2365 if ((LockTime >= (DemodTimeOut - EQLTimeOut)) &&
2366 (QAM_Lock == 0x04))
2367 /*
2368 * We don't wait longer, the frequency/phase offset
2369 * must be too big
2370 */
2371 LockTime = DemodTimeOut;
2372 else if ((LockTime >= (AGCTimeOut + TRLTimeOut)) &&
2373 (QAM_Lock == 0x02))
2374 /*
2375 * We don't wait longer, either there is no signal or
2376 * it is not the right symbol rate or it is an analog
2377 * carrier
2378 */
2379 {
2380 LockTime = DemodTimeOut;
2381 u32_tmp = stv0367_readbits(state,
2382 F367CAB_AGC_PWR_WORD_LO) +
2383 (stv0367_readbits(state,
2384 F367CAB_AGC_PWR_WORD_ME) << 8) +
2385 (stv0367_readbits(state,
2386 F367CAB_AGC_PWR_WORD_HI) << 16);
2387 if (u32_tmp >= 131072)
2388 u32_tmp = 262144 - u32_tmp;
2389 u32_tmp = u32_tmp / (1 << (11 - stv0367_readbits(state,
2390 F367CAB_AGC_IF_BWSEL)));
2391
2392 if (u32_tmp < stv0367_readbits(state,
2393 F367CAB_AGC_PWRREF_LO) +
2394 256 * stv0367_readbits(state,
2395 F367CAB_AGC_PWRREF_HI) - 10)
2396 QAM_Lock = 0x0f;
2397 } else {
2398 usleep_range(10000, 20000);
2399 LockTime += 10;
2400 }
2401 dprintk("QAM_Lock=0x%x LockTime=%d\n", QAM_Lock, LockTime);
2402 tmp = stv0367_readreg(state, R367CAB_IT_STATUS1);
2403
2404 dprintk("R367CAB_IT_STATUS1=0x%x\n", tmp);
2405
2406 } while (((QAM_Lock != 0x0c) && (QAM_Lock != 0x0b)) &&
2407 (LockTime < DemodTimeOut));
2408
2409 dprintk("QAM_Lock=0x%x\n", QAM_Lock);
2410
2411 tmp = stv0367_readreg(state, R367CAB_IT_STATUS1);
2412 dprintk("R367CAB_IT_STATUS1=0x%x\n", tmp);
2413 tmp = stv0367_readreg(state, R367CAB_IT_STATUS2);
2414 dprintk("R367CAB_IT_STATUS2=0x%x\n", tmp);
2415
2416 tmp = stv0367cab_get_derot_freq(state, cab_state->adc_clk);
2417 dprintk("stv0367cab_get_derot_freq=0x%x\n", tmp);
2418
2419 if ((QAM_Lock == 0x0c) || (QAM_Lock == 0x0b)) {
2420 /* Wait for FEC lock */
2421 LockTime = 0;
2422 do {
2423 usleep_range(5000, 7000);
2424 LockTime += 5;
2425 QAMFEC_Lock = stv0367_readbits(state,
Daniel Schellercae7a902017-03-29 13:43:08 -03002426 (state->cab_state->qamfec_status_reg ?
2427 state->cab_state->qamfec_status_reg :
2428 F367CAB_QAMFEC_LOCK));
Igor M. Liplianin17cce932011-01-25 17:02:00 -03002429 } while (!QAMFEC_Lock && (LockTime < FECTimeOut));
2430 } else
2431 QAMFEC_Lock = 0;
2432
2433 if (QAMFEC_Lock) {
2434 signalType = FE_CAB_DATAOK;
Igor M. Liplianin17cce932011-01-25 17:02:00 -03002435 cab_state->spect_inv = stv0367_readbits(state,
2436 F367CAB_QUAD_INV);
2437#if 0
2438/* not clear for me */
Daniel Schellerb16338e2017-03-29 13:43:07 -03002439 if (ifkhz != 0) {
2440 if (ifkhz > cab_state->adc_clk / 1000) {
Igor M. Liplianin17cce932011-01-25 17:02:00 -03002441 cab_state->freq_khz =
2442 FE_Cab_TunerGetFrequency(pIntParams->hTuner)
2443 - stv0367cab_get_derot_freq(state, cab_state->adc_clk)
Daniel Schellerb16338e2017-03-29 13:43:07 -03002444 - cab_state->adc_clk / 1000 + ifkhz;
Igor M. Liplianin17cce932011-01-25 17:02:00 -03002445 } else {
2446 cab_state->freq_khz =
2447 FE_Cab_TunerGetFrequency(pIntParams->hTuner)
2448 - stv0367cab_get_derot_freq(state, cab_state->adc_clk)
Daniel Schellerb16338e2017-03-29 13:43:07 -03002449 + ifkhz;
Igor M. Liplianin17cce932011-01-25 17:02:00 -03002450 }
2451 } else {
2452 cab_state->freq_khz =
2453 FE_Cab_TunerGetFrequency(pIntParams->hTuner) +
2454 stv0367cab_get_derot_freq(state,
2455 cab_state->adc_clk) -
2456 cab_state->adc_clk / 4000;
2457 }
2458#endif
2459 cab_state->symbol_rate = stv0367cab_GetSymbolRate(state,
2460 cab_state->mclk);
2461 cab_state->locked = 1;
2462
2463 /* stv0367_setbits(state, F367CAB_AGC_ACCUMRSTSEL,7);*/
2464 } else {
2465 switch (QAM_Lock) {
2466 case 1:
2467 signalType = FE_CAB_NOAGC;
2468 break;
2469 case 2:
2470 signalType = FE_CAB_NOTIMING;
2471 break;
2472 case 3:
2473 signalType = FE_CAB_TIMINGOK;
2474 break;
2475 case 4:
2476 signalType = FE_CAB_NOCARRIER;
2477 break;
2478 case 5:
2479 signalType = FE_CAB_CARRIEROK;
2480 break;
2481 case 7:
2482 signalType = FE_CAB_NOBLIND;
2483 break;
2484 case 8:
2485 signalType = FE_CAB_BLINDOK;
2486 break;
2487 case 10:
2488 signalType = FE_CAB_NODEMOD;
2489 break;
2490 case 11:
2491 signalType = FE_CAB_DEMODOK;
2492 break;
2493 case 12:
2494 signalType = FE_CAB_DEMODOK;
2495 break;
2496 case 13:
2497 signalType = FE_CAB_NODEMOD;
2498 break;
2499 case 14:
2500 signalType = FE_CAB_NOBLIND;
2501 break;
2502 case 15:
2503 signalType = FE_CAB_NOSIGNAL;
2504 break;
2505 default:
2506 break;
2507 }
2508
2509 }
2510
2511 /* Set the AGC control values to tracking values */
2512 stv0367_writebits(state, F367CAB_AGC_ACCUMRSTSEL, TrackAGCAccum);
2513 return signalType;
2514}
2515
Mauro Carvalho Chehab285d55a2011-12-26 13:03:00 -03002516static int stv0367cab_set_frontend(struct dvb_frontend *fe)
Igor M. Liplianin17cce932011-01-25 17:02:00 -03002517{
Mauro Carvalho Chehab285d55a2011-12-26 13:03:00 -03002518 struct dtv_frontend_properties *p = &fe->dtv_property_cache;
Igor M. Liplianin17cce932011-01-25 17:02:00 -03002519 struct stv0367_state *state = fe->demodulator_priv;
2520 struct stv0367cab_state *cab_state = state->cab_state;
Igor M. Liplianin17cce932011-01-25 17:02:00 -03002521 enum stv0367cab_mod QAMSize = 0;
2522
2523 dprintk("%s: freq = %d, srate = %d\n", __func__,
Mauro Carvalho Chehab285d55a2011-12-26 13:03:00 -03002524 p->frequency, p->symbol_rate);
Igor M. Liplianin17cce932011-01-25 17:02:00 -03002525
2526 cab_state->derot_offset = 0;
2527
Mauro Carvalho Chehab285d55a2011-12-26 13:03:00 -03002528 switch (p->modulation) {
Igor M. Liplianin17cce932011-01-25 17:02:00 -03002529 case QAM_16:
2530 QAMSize = FE_CAB_MOD_QAM16;
2531 break;
2532 case QAM_32:
2533 QAMSize = FE_CAB_MOD_QAM32;
2534 break;
2535 case QAM_64:
2536 QAMSize = FE_CAB_MOD_QAM64;
2537 break;
2538 case QAM_128:
2539 QAMSize = FE_CAB_MOD_QAM128;
2540 break;
2541 case QAM_256:
2542 QAMSize = FE_CAB_MOD_QAM256;
2543 break;
2544 default:
2545 break;
2546 }
2547
Daniel Scheller7718dcf2017-03-29 13:43:06 -03002548 if (state->reinit_on_setfrontend)
2549 stv0367cab_init(fe);
Igor M. Liplianin17cce932011-01-25 17:02:00 -03002550
2551 /* Tuner Frequency Setting */
2552 if (fe->ops.tuner_ops.set_params) {
Daniel Schellerf61c2992017-03-29 13:43:01 -03002553 if (state->use_i2c_gatectrl && fe->ops.i2c_gate_ctrl)
Igor M. Liplianin17cce932011-01-25 17:02:00 -03002554 fe->ops.i2c_gate_ctrl(fe, 1);
Mauro Carvalho Chehab14d24d12011-12-24 12:24:33 -03002555 fe->ops.tuner_ops.set_params(fe);
Daniel Schellerf61c2992017-03-29 13:43:01 -03002556 if (state->use_i2c_gatectrl && fe->ops.i2c_gate_ctrl)
Igor M. Liplianin17cce932011-01-25 17:02:00 -03002557 fe->ops.i2c_gate_ctrl(fe, 0);
2558 }
2559
2560 stv0367cab_SetQamSize(
2561 state,
Mauro Carvalho Chehab285d55a2011-12-26 13:03:00 -03002562 p->symbol_rate,
Igor M. Liplianin17cce932011-01-25 17:02:00 -03002563 QAMSize);
2564
2565 stv0367cab_set_srate(state,
2566 cab_state->adc_clk,
2567 cab_state->mclk,
Mauro Carvalho Chehab285d55a2011-12-26 13:03:00 -03002568 p->symbol_rate,
Igor M. Liplianin17cce932011-01-25 17:02:00 -03002569 QAMSize);
2570 /* Search algorithm launch, [-1.1*RangeOffset, +1.1*RangeOffset] scan */
Mauro Carvalho Chehab285d55a2011-12-26 13:03:00 -03002571 cab_state->state = stv0367cab_algo(state, p);
Igor M. Liplianin17cce932011-01-25 17:02:00 -03002572 return 0;
2573}
2574
Mauro Carvalho Chehab7e3e68b2016-02-04 12:58:30 -02002575static int stv0367cab_get_frontend(struct dvb_frontend *fe,
2576 struct dtv_frontend_properties *p)
Igor M. Liplianin17cce932011-01-25 17:02:00 -03002577{
2578 struct stv0367_state *state = fe->demodulator_priv;
2579 struct stv0367cab_state *cab_state = state->cab_state;
Daniel Schellerb16338e2017-03-29 13:43:07 -03002580 u32 ifkhz = 0;
Igor M. Liplianin17cce932011-01-25 17:02:00 -03002581
2582 enum stv0367cab_mod QAMSize;
2583
2584 dprintk("%s:\n", __func__);
2585
Daniel Schellerb16338e2017-03-29 13:43:07 -03002586 stv0367_get_if_khz(state, &ifkhz);
Mauro Carvalho Chehab285d55a2011-12-26 13:03:00 -03002587 p->symbol_rate = stv0367cab_GetSymbolRate(state, cab_state->mclk);
Igor M. Liplianin17cce932011-01-25 17:02:00 -03002588
2589 QAMSize = stv0367_readbits(state, F367CAB_QAM_MODE);
2590 switch (QAMSize) {
2591 case FE_CAB_MOD_QAM16:
Mauro Carvalho Chehab285d55a2011-12-26 13:03:00 -03002592 p->modulation = QAM_16;
Igor M. Liplianin17cce932011-01-25 17:02:00 -03002593 break;
2594 case FE_CAB_MOD_QAM32:
Mauro Carvalho Chehab285d55a2011-12-26 13:03:00 -03002595 p->modulation = QAM_32;
Igor M. Liplianin17cce932011-01-25 17:02:00 -03002596 break;
2597 case FE_CAB_MOD_QAM64:
Mauro Carvalho Chehab285d55a2011-12-26 13:03:00 -03002598 p->modulation = QAM_64;
Igor M. Liplianin17cce932011-01-25 17:02:00 -03002599 break;
2600 case FE_CAB_MOD_QAM128:
Mauro Carvalho Chehab285d55a2011-12-26 13:03:00 -03002601 p->modulation = QAM_128;
Igor M. Liplianin17cce932011-01-25 17:02:00 -03002602 break;
Maks Naumoveafeda92014-08-15 16:23:20 -03002603 case FE_CAB_MOD_QAM256:
Mauro Carvalho Chehab285d55a2011-12-26 13:03:00 -03002604 p->modulation = QAM_256;
Igor M. Liplianin17cce932011-01-25 17:02:00 -03002605 break;
2606 default:
2607 break;
2608 }
2609
Mauro Carvalho Chehab285d55a2011-12-26 13:03:00 -03002610 p->frequency = stv0367_get_tuner_freq(fe);
Igor M. Liplianin17cce932011-01-25 17:02:00 -03002611
Mauro Carvalho Chehab285d55a2011-12-26 13:03:00 -03002612 dprintk("%s: tuner frequency = %d\n", __func__, p->frequency);
Igor M. Liplianin17cce932011-01-25 17:02:00 -03002613
Daniel Schellerb16338e2017-03-29 13:43:07 -03002614 if (ifkhz == 0) {
Mauro Carvalho Chehab285d55a2011-12-26 13:03:00 -03002615 p->frequency +=
Igor M. Liplianin17cce932011-01-25 17:02:00 -03002616 (stv0367cab_get_derot_freq(state, cab_state->adc_clk) -
2617 cab_state->adc_clk / 4000);
2618 return 0;
2619 }
2620
Daniel Schellerb16338e2017-03-29 13:43:07 -03002621 if (ifkhz > cab_state->adc_clk / 1000)
2622 p->frequency += (ifkhz
Igor M. Liplianin17cce932011-01-25 17:02:00 -03002623 - stv0367cab_get_derot_freq(state, cab_state->adc_clk)
2624 - cab_state->adc_clk / 1000);
2625 else
Daniel Schellerb16338e2017-03-29 13:43:07 -03002626 p->frequency += (ifkhz
Igor M. Liplianin17cce932011-01-25 17:02:00 -03002627 - stv0367cab_get_derot_freq(state, cab_state->adc_clk));
2628
2629 return 0;
2630}
2631
2632#if 0
2633void stv0367cab_GetErrorCount(state, enum stv0367cab_mod QAMSize,
2634 u32 symbol_rate, FE_367qam_Monitor *Monitor_results)
2635{
2636 stv0367cab_OptimiseNByteAndGetBER(state, QAMSize, symbol_rate, Monitor_results);
2637 stv0367cab_GetPacketsCount(state, Monitor_results);
2638
2639 return;
2640}
2641
2642static int stv0367cab_read_ber(struct dvb_frontend *fe, u32 *ber)
2643{
2644 struct stv0367_state *state = fe->demodulator_priv;
2645
2646 return 0;
2647}
2648#endif
2649static s32 stv0367cab_get_rf_lvl(struct stv0367_state *state)
2650{
2651 s32 rfLevel = 0;
2652 s32 RfAgcPwm = 0, IfAgcPwm = 0;
2653 u8 i;
2654
2655 stv0367_writebits(state, F367CAB_STDBY_ADCGP, 0x0);
2656
2657 RfAgcPwm =
2658 (stv0367_readbits(state, F367CAB_RF_AGC1_LEVEL_LO) & 0x03) +
2659 (stv0367_readbits(state, F367CAB_RF_AGC1_LEVEL_HI) << 2);
2660 RfAgcPwm = 100 * RfAgcPwm / 1023;
2661
2662 IfAgcPwm =
2663 stv0367_readbits(state, F367CAB_AGC_IF_PWMCMD_LO) +
2664 (stv0367_readbits(state, F367CAB_AGC_IF_PWMCMD_HI) << 8);
2665 if (IfAgcPwm >= 2048)
2666 IfAgcPwm -= 2048;
2667 else
2668 IfAgcPwm += 2048;
2669
2670 IfAgcPwm = 100 * IfAgcPwm / 4095;
2671
2672 /* For DTT75467 on NIM */
2673 if (RfAgcPwm < 90 && IfAgcPwm < 28) {
2674 for (i = 0; i < RF_LOOKUP_TABLE_SIZE; i++) {
2675 if (RfAgcPwm <= stv0367cab_RF_LookUp1[0][i]) {
2676 rfLevel = (-1) * stv0367cab_RF_LookUp1[1][i];
2677 break;
2678 }
2679 }
2680 if (i == RF_LOOKUP_TABLE_SIZE)
2681 rfLevel = -56;
2682 } else { /*if IF AGC>10*/
2683 for (i = 0; i < RF_LOOKUP_TABLE2_SIZE; i++) {
2684 if (IfAgcPwm <= stv0367cab_RF_LookUp2[0][i]) {
2685 rfLevel = (-1) * stv0367cab_RF_LookUp2[1][i];
2686 break;
2687 }
2688 }
2689 if (i == RF_LOOKUP_TABLE2_SIZE)
2690 rfLevel = -72;
2691 }
2692 return rfLevel;
2693}
2694
2695static int stv0367cab_read_strength(struct dvb_frontend *fe, u16 *strength)
2696{
2697 struct stv0367_state *state = fe->demodulator_priv;
2698
2699 s32 signal = stv0367cab_get_rf_lvl(state);
2700
2701 dprintk("%s: signal=%d dBm\n", __func__, signal);
2702
2703 if (signal <= -72)
2704 *strength = 65535;
2705 else
2706 *strength = (22 + signal) * (-1311);
2707
2708 dprintk("%s: strength=%d\n", __func__, (*strength));
2709
2710 return 0;
2711}
2712
Daniel Schellerb138afd2017-06-21 16:45:42 -03002713static int stv0367cab_snr_power(struct dvb_frontend *fe)
Igor M. Liplianin17cce932011-01-25 17:02:00 -03002714{
2715 struct stv0367_state *state = fe->demodulator_priv;
Igor M. Liplianin17cce932011-01-25 17:02:00 -03002716 enum stv0367cab_mod QAMSize;
Igor M. Liplianin17cce932011-01-25 17:02:00 -03002717
2718 QAMSize = stv0367_readbits(state, F367CAB_QAM_MODE);
2719 switch (QAMSize) {
2720 case FE_CAB_MOD_QAM4:
Daniel Schellerb138afd2017-06-21 16:45:42 -03002721 return 21904;
Igor M. Liplianin17cce932011-01-25 17:02:00 -03002722 case FE_CAB_MOD_QAM16:
Daniel Schellerb138afd2017-06-21 16:45:42 -03002723 return 20480;
Igor M. Liplianin17cce932011-01-25 17:02:00 -03002724 case FE_CAB_MOD_QAM32:
Daniel Schellerb138afd2017-06-21 16:45:42 -03002725 return 23040;
Igor M. Liplianin17cce932011-01-25 17:02:00 -03002726 case FE_CAB_MOD_QAM64:
Daniel Schellerb138afd2017-06-21 16:45:42 -03002727 return 21504;
Igor M. Liplianin17cce932011-01-25 17:02:00 -03002728 case FE_CAB_MOD_QAM128:
Daniel Schellerb138afd2017-06-21 16:45:42 -03002729 return 23616;
Igor M. Liplianin17cce932011-01-25 17:02:00 -03002730 case FE_CAB_MOD_QAM256:
Daniel Schellerb138afd2017-06-21 16:45:42 -03002731 return 21760;
Igor M. Liplianin17cce932011-01-25 17:02:00 -03002732 case FE_CAB_MOD_QAM1024:
Daniel Schellerb138afd2017-06-21 16:45:42 -03002733 return 21280;
Igor M. Liplianin17cce932011-01-25 17:02:00 -03002734 default:
Igor M. Liplianin17cce932011-01-25 17:02:00 -03002735 break;
2736 }
2737
Daniel Schellerb138afd2017-06-21 16:45:42 -03002738 return 1;
2739}
2740
2741static int stv0367cab_snr_readreg(struct dvb_frontend *fe, int avgdiv)
2742{
2743 struct stv0367_state *state = fe->demodulator_priv;
2744 u32 regval = 0;
2745 int i;
2746
Igor M. Liplianin17cce932011-01-25 17:02:00 -03002747 for (i = 0; i < 10; i++) {
2748 regval += (stv0367_readbits(state, F367CAB_SNR_LO)
2749 + 256 * stv0367_readbits(state, F367CAB_SNR_HI));
2750 }
2751
Daniel Schellerb138afd2017-06-21 16:45:42 -03002752 if (avgdiv)
2753 regval /= 10;
2754
2755 return regval;
2756}
2757
2758static int stv0367cab_read_snr(struct dvb_frontend *fe, u16 *snr)
2759{
2760 struct stv0367_state *state = fe->demodulator_priv;
2761 u32 noisepercentage;
2762 u32 regval = 0, temp = 0;
2763 int power;
2764
2765 power = stv0367cab_snr_power(fe);
2766 regval = stv0367cab_snr_readreg(fe, 1);
2767
Igor M. Liplianin17cce932011-01-25 17:02:00 -03002768 if (regval != 0) {
2769 temp = power
2770 * (1 << (3 + stv0367_readbits(state, F367CAB_SNR_PER)));
2771 temp /= regval;
2772 }
2773
2774 /* table values, not needed to calculate logarithms */
2775 if (temp >= 5012)
2776 noisepercentage = 100;
2777 else if (temp >= 3981)
2778 noisepercentage = 93;
2779 else if (temp >= 3162)
2780 noisepercentage = 86;
2781 else if (temp >= 2512)
2782 noisepercentage = 79;
2783 else if (temp >= 1995)
2784 noisepercentage = 72;
2785 else if (temp >= 1585)
2786 noisepercentage = 65;
2787 else if (temp >= 1259)
2788 noisepercentage = 58;
2789 else if (temp >= 1000)
2790 noisepercentage = 50;
2791 else if (temp >= 794)
2792 noisepercentage = 43;
2793 else if (temp >= 501)
2794 noisepercentage = 36;
2795 else if (temp >= 316)
2796 noisepercentage = 29;
2797 else if (temp >= 200)
2798 noisepercentage = 22;
2799 else if (temp >= 158)
2800 noisepercentage = 14;
2801 else if (temp >= 126)
2802 noisepercentage = 7;
2803 else
2804 noisepercentage = 0;
2805
2806 dprintk("%s: noisepercentage=%d\n", __func__, noisepercentage);
2807
2808 *snr = (noisepercentage * 65535) / 100;
2809
2810 return 0;
2811}
2812
Abylay Ospan78db66e2011-01-02 09:12:00 -03002813static int stv0367cab_read_ucblcks(struct dvb_frontend *fe, u32 *ucblocks)
2814{
2815 struct stv0367_state *state = fe->demodulator_priv;
2816 int corrected, tscount;
2817
2818 *ucblocks = (stv0367_readreg(state, R367CAB_RS_COUNTER_5) << 8)
2819 | stv0367_readreg(state, R367CAB_RS_COUNTER_4);
2820 corrected = (stv0367_readreg(state, R367CAB_RS_COUNTER_3) << 8)
2821 | stv0367_readreg(state, R367CAB_RS_COUNTER_2);
2822 tscount = (stv0367_readreg(state, R367CAB_RS_COUNTER_2) << 8)
2823 | stv0367_readreg(state, R367CAB_RS_COUNTER_1);
2824
2825 dprintk("%s: uncorrected blocks=%d corrected blocks=%d tscount=%d\n",
2826 __func__, *ucblocks, corrected, tscount);
2827
2828 return 0;
2829};
2830
Max Kellermannbd336e62016-08-09 18:32:21 -03002831static const struct dvb_frontend_ops stv0367cab_ops = {
Mauro Carvalho Chehab285d55a2011-12-26 13:03:00 -03002832 .delsys = { SYS_DVBC_ANNEX_A },
Igor M. Liplianin17cce932011-01-25 17:02:00 -03002833 .info = {
2834 .name = "ST STV0367 DVB-C",
Igor M. Liplianin17cce932011-01-25 17:02:00 -03002835 .frequency_min = 47000000,
2836 .frequency_max = 862000000,
2837 .frequency_stepsize = 62500,
2838 .symbol_rate_min = 870000,
2839 .symbol_rate_max = 11700000,
2840 .caps = 0x400 |/* FE_CAN_QAM_4 */
2841 FE_CAN_QAM_16 | FE_CAN_QAM_32 |
2842 FE_CAN_QAM_64 | FE_CAN_QAM_128 |
2843 FE_CAN_QAM_256 | FE_CAN_FEC_AUTO
2844 },
2845 .release = stv0367_release,
2846 .init = stv0367cab_init,
2847 .sleep = stv0367cab_sleep,
2848 .i2c_gate_ctrl = stv0367cab_gate_ctrl,
Mauro Carvalho Chehab285d55a2011-12-26 13:03:00 -03002849 .set_frontend = stv0367cab_set_frontend,
2850 .get_frontend = stv0367cab_get_frontend,
Igor M. Liplianin17cce932011-01-25 17:02:00 -03002851 .read_status = stv0367cab_read_status,
2852/* .read_ber = stv0367cab_read_ber, */
2853 .read_signal_strength = stv0367cab_read_strength,
2854 .read_snr = stv0367cab_read_snr,
Abylay Ospan78db66e2011-01-02 09:12:00 -03002855 .read_ucblocks = stv0367cab_read_ucblcks,
Igor M. Liplianin17cce932011-01-25 17:02:00 -03002856 .get_tune_settings = stv0367_get_tune_settings,
2857};
2858
2859struct dvb_frontend *stv0367cab_attach(const struct stv0367_config *config,
2860 struct i2c_adapter *i2c)
2861{
2862 struct stv0367_state *state = NULL;
2863 struct stv0367cab_state *cab_state = NULL;
2864
2865 /* allocate memory for the internal state */
2866 state = kzalloc(sizeof(struct stv0367_state), GFP_KERNEL);
2867 if (state == NULL)
2868 goto error;
2869 cab_state = kzalloc(sizeof(struct stv0367cab_state), GFP_KERNEL);
2870 if (cab_state == NULL)
2871 goto error;
2872
2873 /* setup the state */
2874 state->i2c = i2c;
2875 state->config = config;
2876 cab_state->search_range = 280000;
Daniel Schellercae7a902017-03-29 13:43:08 -03002877 cab_state->qamfec_status_reg = F367CAB_QAMFEC_LOCK;
Igor M. Liplianin17cce932011-01-25 17:02:00 -03002878 state->cab_state = cab_state;
2879 state->fe.ops = stv0367cab_ops;
2880 state->fe.demodulator_priv = state;
2881 state->chip_id = stv0367_readreg(state, 0xf000);
2882
Daniel Schellerf61c2992017-03-29 13:43:01 -03002883 /* demod operation options */
2884 state->use_i2c_gatectrl = 1;
Daniel Scheller8881ceb2017-03-29 13:43:04 -03002885 state->deftabs = STV0367_DEFTAB_GENERIC;
Daniel Scheller7718dcf2017-03-29 13:43:06 -03002886 state->reinit_on_setfrontend = 1;
Daniel Schellerb16338e2017-03-29 13:43:07 -03002887 state->auto_if_khz = 0;
Daniel Schellerf61c2992017-03-29 13:43:01 -03002888
Igor M. Liplianin17cce932011-01-25 17:02:00 -03002889 dprintk("%s: chip_id = 0x%x\n", __func__, state->chip_id);
2890
2891 /* check if the demod is there */
2892 if ((state->chip_id != 0x50) && (state->chip_id != 0x60))
2893 goto error;
2894
2895 return &state->fe;
2896
2897error:
2898 kfree(cab_state);
2899 kfree(state);
2900 return NULL;
2901}
2902EXPORT_SYMBOL(stv0367cab_attach);
2903
Daniel Schellerdbbac112017-03-29 13:43:11 -03002904/*
2905 * Functions for operation on Digital Devices hardware
2906 */
2907
2908static void stv0367ddb_setup_ter(struct stv0367_state *state)
2909{
2910 stv0367_writereg(state, R367TER_DEBUG_LT4, 0x00);
2911 stv0367_writereg(state, R367TER_DEBUG_LT5, 0x00);
2912 stv0367_writereg(state, R367TER_DEBUG_LT6, 0x00); /* R367CAB_CTRL_1 */
2913 stv0367_writereg(state, R367TER_DEBUG_LT7, 0x00); /* R367CAB_CTRL_2 */
2914 stv0367_writereg(state, R367TER_DEBUG_LT8, 0x00);
2915 stv0367_writereg(state, R367TER_DEBUG_LT9, 0x00);
2916
2917 /* Tuner Setup */
2918 /* Buffer Q disabled, I Enabled, unsigned ADC */
2919 stv0367_writereg(state, R367TER_ANADIGCTRL, 0x89);
2920 stv0367_writereg(state, R367TER_DUAL_AD12, 0x04); /* ADCQ disabled */
2921
2922 /* Clock setup */
2923 /* PLL bypassed and disabled */
2924 stv0367_writereg(state, R367TER_ANACTRL, 0x0D);
2925 stv0367_writereg(state, R367TER_TOPCTRL, 0x00); /* Set OFDM */
2926
2927 /* IC runs at 54 MHz with a 27 MHz crystal */
2928 stv0367_pll_setup(state, STV0367_ICSPEED_53125, state->config->xtal);
2929
2930 msleep(50);
2931 /* PLL enabled and used */
2932 stv0367_writereg(state, R367TER_ANACTRL, 0x00);
2933
2934 state->activedemod = demod_ter;
2935}
2936
2937static void stv0367ddb_setup_cab(struct stv0367_state *state)
2938{
2939 stv0367_writereg(state, R367TER_DEBUG_LT4, 0x00);
2940 stv0367_writereg(state, R367TER_DEBUG_LT5, 0x01);
2941 stv0367_writereg(state, R367TER_DEBUG_LT6, 0x06); /* R367CAB_CTRL_1 */
2942 stv0367_writereg(state, R367TER_DEBUG_LT7, 0x03); /* R367CAB_CTRL_2 */
2943 stv0367_writereg(state, R367TER_DEBUG_LT8, 0x00);
2944 stv0367_writereg(state, R367TER_DEBUG_LT9, 0x00);
2945
2946 /* Tuner Setup */
2947 /* Buffer Q disabled, I Enabled, signed ADC */
2948 stv0367_writereg(state, R367TER_ANADIGCTRL, 0x8B);
2949 /* ADCQ disabled */
2950 stv0367_writereg(state, R367TER_DUAL_AD12, 0x04);
2951
2952 /* Clock setup */
2953 /* PLL bypassed and disabled */
2954 stv0367_writereg(state, R367TER_ANACTRL, 0x0D);
2955 /* Set QAM */
2956 stv0367_writereg(state, R367TER_TOPCTRL, 0x10);
2957
2958 /* IC runs at 58 MHz with a 27 MHz crystal */
2959 stv0367_pll_setup(state, STV0367_ICSPEED_58000, state->config->xtal);
2960
2961 msleep(50);
2962 /* PLL enabled and used */
2963 stv0367_writereg(state, R367TER_ANACTRL, 0x00);
2964
2965 state->cab_state->mclk = stv0367cab_get_mclk(&state->fe,
2966 state->config->xtal);
2967 state->cab_state->adc_clk = stv0367cab_get_adc_freq(&state->fe,
2968 state->config->xtal);
2969
2970 state->activedemod = demod_cab;
2971}
2972
2973static int stv0367ddb_set_frontend(struct dvb_frontend *fe)
2974{
2975 struct stv0367_state *state = fe->demodulator_priv;
2976
2977 switch (fe->dtv_property_cache.delivery_system) {
2978 case SYS_DVBT:
2979 if (state->activedemod != demod_ter)
2980 stv0367ddb_setup_ter(state);
2981
2982 return stv0367ter_set_frontend(fe);
2983 case SYS_DVBC_ANNEX_A:
2984 if (state->activedemod != demod_cab)
2985 stv0367ddb_setup_cab(state);
2986
2987 /* protect against division error oopses */
2988 if (fe->dtv_property_cache.symbol_rate == 0) {
2989 printk(KERN_ERR "Invalid symbol rate\n");
2990 return -EINVAL;
2991 }
2992
2993 return stv0367cab_set_frontend(fe);
2994 default:
2995 break;
2996 }
2997
2998 return -EINVAL;
2999}
3000
Daniel Schellerd394ad12017-06-25 08:26:44 -03003001static void stv0367ddb_read_snr(struct dvb_frontend *fe)
3002{
3003 struct stv0367_state *state = fe->demodulator_priv;
3004 struct dtv_frontend_properties *p = &fe->dtv_property_cache;
3005 int cab_pwr;
3006 u32 regval, tmpval, snrval = 0;
3007
3008 switch (state->activedemod) {
3009 case demod_ter:
3010 snrval = stv0367ter_snr_readreg(fe);
3011 break;
3012 case demod_cab:
3013 cab_pwr = stv0367cab_snr_power(fe);
3014 regval = stv0367cab_snr_readreg(fe, 0);
3015
3016 /* prevent division by zero */
Mauro Carvalho Chehabf5a1ba82017-06-25 09:07:45 -03003017 if (!regval) {
Daniel Schellerd394ad12017-06-25 08:26:44 -03003018 snrval = 0;
Mauro Carvalho Chehabf5a1ba82017-06-25 09:07:45 -03003019 break;
3020 }
Daniel Schellerd394ad12017-06-25 08:26:44 -03003021
3022 tmpval = (cab_pwr * 320) / regval;
3023 snrval = ((tmpval != 0) ? (intlog2(tmpval) / 5581) : 0);
3024 break;
3025 default:
3026 p->cnr.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
3027 return;
3028 }
3029
3030 p->cnr.stat[0].scale = FE_SCALE_DECIBEL;
3031 p->cnr.stat[0].uvalue = snrval;
3032}
3033
Daniel Scheller4043eb22017-06-21 16:45:41 -03003034static void stv0367ddb_read_ucblocks(struct dvb_frontend *fe)
3035{
3036 struct stv0367_state *state = fe->demodulator_priv;
3037 struct dtv_frontend_properties *p = &fe->dtv_property_cache;
3038 u32 ucblocks = 0;
3039
3040 switch (state->activedemod) {
3041 case demod_ter:
3042 stv0367ter_read_ucblocks(fe, &ucblocks);
3043 break;
3044 case demod_cab:
3045 stv0367cab_read_ucblcks(fe, &ucblocks);
3046 break;
3047 default:
3048 p->block_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
3049 return;
3050 }
3051
3052 p->block_error.stat[0].scale = FE_SCALE_COUNTER;
3053 p->block_error.stat[0].uvalue = ucblocks;
3054}
3055
Daniel Schellerdbbac112017-03-29 13:43:11 -03003056static int stv0367ddb_read_status(struct dvb_frontend *fe,
3057 enum fe_status *status)
3058{
3059 struct stv0367_state *state = fe->demodulator_priv;
Daniel Scheller4043eb22017-06-21 16:45:41 -03003060 struct dtv_frontend_properties *p = &fe->dtv_property_cache;
3061 int ret;
Daniel Schellerdbbac112017-03-29 13:43:11 -03003062
3063 switch (state->activedemod) {
3064 case demod_ter:
Daniel Scheller4043eb22017-06-21 16:45:41 -03003065 ret = stv0367ter_read_status(fe, status);
Daniel Schellerdbbac112017-03-29 13:43:11 -03003066 break;
Daniel Scheller4043eb22017-06-21 16:45:41 -03003067 case demod_cab:
3068 ret = stv0367cab_read_status(fe, status);
3069 break;
3070 default:
3071 return 0;
Daniel Schellerdbbac112017-03-29 13:43:11 -03003072 }
3073
Daniel Scheller4043eb22017-06-21 16:45:41 -03003074 /* stop and report on *_read_status failure */
3075 if (ret)
3076 return ret;
3077
Daniel Schellerd394ad12017-06-25 08:26:44 -03003078 /* read carrier/noise when a carrier is detected */
3079 if (*status & FE_HAS_CARRIER)
3080 stv0367ddb_read_snr(fe);
3081 else
3082 p->cnr.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
3083
Daniel Scheller4043eb22017-06-21 16:45:41 -03003084 /* stop if demod isn't locked */
3085 if (!(*status & FE_HAS_LOCK)) {
3086 p->block_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
3087 return ret;
3088 }
3089
3090 stv0367ddb_read_ucblocks(fe);
3091
3092 return 0;
Daniel Schellerdbbac112017-03-29 13:43:11 -03003093}
3094
3095static int stv0367ddb_get_frontend(struct dvb_frontend *fe,
3096 struct dtv_frontend_properties *p)
3097{
3098 struct stv0367_state *state = fe->demodulator_priv;
3099
3100 switch (state->activedemod) {
3101 case demod_ter:
3102 return stv0367ter_get_frontend(fe, p);
3103 case demod_cab:
3104 return stv0367cab_get_frontend(fe, p);
3105 default:
3106 break;
3107 }
3108
3109 return -EINVAL;
3110}
3111
3112static int stv0367ddb_sleep(struct dvb_frontend *fe)
3113{
3114 struct stv0367_state *state = fe->demodulator_priv;
3115
3116 switch (state->activedemod) {
3117 case demod_ter:
3118 state->activedemod = demod_none;
3119 return stv0367ter_sleep(fe);
3120 case demod_cab:
3121 state->activedemod = demod_none;
3122 return stv0367cab_sleep(fe);
3123 default:
3124 break;
3125 }
3126
3127 return -EINVAL;
3128}
3129
3130static int stv0367ddb_init(struct stv0367_state *state)
3131{
3132 struct stv0367ter_state *ter_state = state->ter_state;
Daniel Scheller4043eb22017-06-21 16:45:41 -03003133 struct dtv_frontend_properties *p = &state->fe.dtv_property_cache;
Daniel Schellerdbbac112017-03-29 13:43:11 -03003134
3135 stv0367_writereg(state, R367TER_TOPCTRL, 0x10);
3136
3137 if (stv0367_deftabs[state->deftabs][STV0367_TAB_BASE])
3138 stv0367_write_table(state,
3139 stv0367_deftabs[state->deftabs][STV0367_TAB_BASE]);
3140
3141 stv0367_write_table(state,
3142 stv0367_deftabs[state->deftabs][STV0367_TAB_CAB]);
3143
3144 stv0367_writereg(state, R367TER_TOPCTRL, 0x00);
3145 stv0367_write_table(state,
3146 stv0367_deftabs[state->deftabs][STV0367_TAB_TER]);
3147
3148 stv0367_writereg(state, R367TER_GAIN_SRC1, 0x2A);
3149 stv0367_writereg(state, R367TER_GAIN_SRC2, 0xD6);
3150 stv0367_writereg(state, R367TER_INC_DEROT1, 0x55);
3151 stv0367_writereg(state, R367TER_INC_DEROT2, 0x55);
3152 stv0367_writereg(state, R367TER_TRL_CTL, 0x14);
3153 stv0367_writereg(state, R367TER_TRL_NOMRATE1, 0xAE);
3154 stv0367_writereg(state, R367TER_TRL_NOMRATE2, 0x56);
3155 stv0367_writereg(state, R367TER_FEPATH_CFG, 0x0);
3156
3157 /* OFDM TS Setup */
3158
3159 stv0367_writereg(state, R367TER_TSCFGH, 0x70);
3160 stv0367_writereg(state, R367TER_TSCFGM, 0xC0);
3161 stv0367_writereg(state, R367TER_TSCFGL, 0x20);
3162 stv0367_writereg(state, R367TER_TSSPEED, 0x40); /* Fixed at 54 MHz */
3163
3164 stv0367_writereg(state, R367TER_TSCFGH, 0x71);
3165 stv0367_writereg(state, R367TER_TSCFGH, 0x70);
3166
3167 stv0367_writereg(state, R367TER_TOPCTRL, 0x10);
3168
3169 /* Also needed for QAM */
3170 stv0367_writereg(state, R367TER_AGC12C, 0x01); /* AGC Pin setup */
3171
3172 stv0367_writereg(state, R367TER_AGCCTRL1, 0x8A);
3173
3174 /* QAM TS setup, note exact format also depends on descrambler */
3175 /* settings */
3176 /* Inverted Clock, Swap, serial */
3177 stv0367_writereg(state, R367CAB_OUTFORMAT_0, 0x85);
3178
3179 /* Clock setup (PLL bypassed and disabled) */
3180 stv0367_writereg(state, R367TER_ANACTRL, 0x0D);
3181
3182 /* IC runs at 58 MHz with a 27 MHz crystal */
3183 stv0367_pll_setup(state, STV0367_ICSPEED_58000, state->config->xtal);
3184
3185 /* Tuner setup */
3186 /* Buffer Q disabled, I Enabled, signed ADC */
3187 stv0367_writereg(state, R367TER_ANADIGCTRL, 0x8b);
3188 stv0367_writereg(state, R367TER_DUAL_AD12, 0x04); /* ADCQ disabled */
3189
3190 /* Improves the C/N lock limit */
3191 stv0367_writereg(state, R367CAB_FSM_SNR2_HTH, 0x23);
3192 /* ZIF/IF Automatic mode */
3193 stv0367_writereg(state, R367CAB_IQ_QAM, 0x01);
3194 /* Improving burst noise performances */
3195 stv0367_writereg(state, R367CAB_EQU_FFE_LEAKAGE, 0x83);
3196 /* Improving ACI performances */
3197 stv0367_writereg(state, R367CAB_IQDEM_ADJ_EN, 0x05);
3198
3199 /* PLL enabled and used */
3200 stv0367_writereg(state, R367TER_ANACTRL, 0x00);
3201
3202 stv0367_writereg(state, R367TER_I2CRPT, (0x08 | ((5 & 0x07) << 4)));
3203
3204 ter_state->pBER = 0;
3205 ter_state->first_lock = 0;
3206 ter_state->unlock_counter = 2;
3207
Daniel Scheller4043eb22017-06-21 16:45:41 -03003208 p->strength.len = 1;
3209 p->strength.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
3210 p->cnr.len = 1;
3211 p->cnr.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
3212 p->block_error.len = 1;
3213 p->block_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
3214
Daniel Schellerdbbac112017-03-29 13:43:11 -03003215 return 0;
3216}
3217
3218static const struct dvb_frontend_ops stv0367ddb_ops = {
3219 .delsys = { SYS_DVBC_ANNEX_A, SYS_DVBT },
3220 .info = {
3221 .name = "ST STV0367 DDB DVB-C/T",
3222 .frequency_min = 47000000,
3223 .frequency_max = 865000000,
3224 .frequency_stepsize = 166667,
3225 .frequency_tolerance = 0,
3226 .symbol_rate_min = 870000,
3227 .symbol_rate_max = 11700000,
3228 .caps = /* DVB-C */
3229 0x400 |/* FE_CAN_QAM_4 */
3230 FE_CAN_QAM_16 | FE_CAN_QAM_32 |
3231 FE_CAN_QAM_64 | FE_CAN_QAM_128 |
Daniel Schellerf9c1e252017-06-21 17:10:54 -03003232 FE_CAN_QAM_256 | FE_CAN_QAM_AUTO |
Daniel Schellerdbbac112017-03-29 13:43:11 -03003233 /* DVB-T */
Daniel Schellerf9c1e252017-06-21 17:10:54 -03003234 FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 | FE_CAN_FEC_3_4 |
3235 FE_CAN_FEC_5_6 | FE_CAN_FEC_7_8 | FE_CAN_FEC_AUTO |
3236 FE_CAN_QPSK | FE_CAN_TRANSMISSION_MODE_AUTO |
3237 FE_CAN_RECOVER | FE_CAN_INVERSION_AUTO |
Daniel Schellerdbbac112017-03-29 13:43:11 -03003238 FE_CAN_MUTE_TS
3239 },
3240 .release = stv0367_release,
3241 .sleep = stv0367ddb_sleep,
3242 .i2c_gate_ctrl = stv0367cab_gate_ctrl, /* valid for TER and CAB */
3243 .set_frontend = stv0367ddb_set_frontend,
3244 .get_frontend = stv0367ddb_get_frontend,
3245 .get_tune_settings = stv0367_get_tune_settings,
3246 .read_status = stv0367ddb_read_status,
3247};
3248
3249struct dvb_frontend *stv0367ddb_attach(const struct stv0367_config *config,
3250 struct i2c_adapter *i2c)
3251{
3252 struct stv0367_state *state = NULL;
3253 struct stv0367ter_state *ter_state = NULL;
3254 struct stv0367cab_state *cab_state = NULL;
3255
3256 /* allocate memory for the internal state */
3257 state = kzalloc(sizeof(struct stv0367_state), GFP_KERNEL);
3258 if (state == NULL)
3259 goto error;
3260 ter_state = kzalloc(sizeof(struct stv0367ter_state), GFP_KERNEL);
3261 if (ter_state == NULL)
3262 goto error;
3263 cab_state = kzalloc(sizeof(struct stv0367cab_state), GFP_KERNEL);
3264 if (cab_state == NULL)
3265 goto error;
3266
3267 /* setup the state */
3268 state->i2c = i2c;
3269 state->config = config;
3270 state->ter_state = ter_state;
3271 cab_state->search_range = 280000;
3272 cab_state->qamfec_status_reg = F367CAB_DESCR_SYNCSTATE;
3273 state->cab_state = cab_state;
3274 state->fe.ops = stv0367ddb_ops;
3275 state->fe.demodulator_priv = state;
3276 state->chip_id = stv0367_readreg(state, R367TER_ID);
3277
3278 /* demod operation options */
3279 state->use_i2c_gatectrl = 0;
3280 state->deftabs = STV0367_DEFTAB_DDB;
3281 state->reinit_on_setfrontend = 0;
3282 state->auto_if_khz = 1;
3283 state->activedemod = demod_none;
3284
3285 dprintk("%s: chip_id = 0x%x\n", __func__, state->chip_id);
3286
3287 /* check if the demod is there */
3288 if ((state->chip_id != 0x50) && (state->chip_id != 0x60))
3289 goto error;
3290
3291 dev_info(&i2c->dev, "Found %s with ChipID %02X at adr %02X\n",
3292 state->fe.ops.info.name, state->chip_id,
3293 config->demod_address);
3294
3295 stv0367ddb_init(state);
3296
3297 return &state->fe;
3298
3299error:
3300 kfree(cab_state);
3301 kfree(ter_state);
3302 kfree(state);
3303 return NULL;
3304}
3305EXPORT_SYMBOL(stv0367ddb_attach);
3306
Igor M. Liplianin17cce932011-01-25 17:02:00 -03003307MODULE_PARM_DESC(debug, "Set debug");
3308MODULE_PARM_DESC(i2c_debug, "Set i2c debug");
3309
3310MODULE_AUTHOR("Igor M. Liplianin");
3311MODULE_DESCRIPTION("ST STV0367 DVB-C/T demodulator driver");
3312MODULE_LICENSE("GPL");