blob: 9a49db1ae1abc7b434fa7def16ae22fe1dba32df [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.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 */
25
26#include <linux/kernel.h>
27#include <linux/module.h>
28#include <linux/string.h>
29#include <linux/slab.h>
30#include <linux/i2c.h>
31
32#include "stv0367.h"
33#include "stv0367_regs.h"
34#include "stv0367_priv.h"
35
Mauro Carvalho Chehab9aca4fb2013-11-02 05:17:01 -030036/* Max transfer size done by I2C transfer functions */
37#define MAX_XFER_SIZE 64
38
Igor M. Liplianin17cce932011-01-25 17:02:00 -030039static int stvdebug;
40module_param_named(debug, stvdebug, int, 0644);
41
42static int i2cdebug;
43module_param_named(i2c_debug, i2cdebug, int, 0644);
44
45#define dprintk(args...) \
46 do { \
47 if (stvdebug) \
48 printk(KERN_DEBUG args); \
49 } while (0)
50 /* DVB-C */
51
52struct stv0367cab_state {
53 enum stv0367_cab_signal_type state;
54 u32 mclk;
55 u32 adc_clk;
56 s32 search_range;
57 s32 derot_offset;
58 /* results */
59 int locked; /* channel found */
60 u32 freq_khz; /* found frequency (in kHz) */
61 u32 symbol_rate; /* found symbol rate (in Bds) */
Mauro Carvalho Chehab0df289a2015-06-07 14:53:52 -030062 enum fe_spectral_inversion spect_inv; /* Spectrum Inversion */
Igor M. Liplianin17cce932011-01-25 17:02:00 -030063};
64
65struct stv0367ter_state {
66 /* DVB-T */
67 enum stv0367_ter_signal_type state;
68 enum stv0367_ter_if_iq_mode if_iq_mode;
69 enum stv0367_ter_mode mode;/* mode 2K or 8K */
Mauro Carvalho Chehab0df289a2015-06-07 14:53:52 -030070 enum fe_guard_interval guard;
Igor M. Liplianin17cce932011-01-25 17:02:00 -030071 enum stv0367_ter_hierarchy hierarchy;
72 u32 frequency;
Mauro Carvalho Chehab0df289a2015-06-07 14:53:52 -030073 enum fe_spectral_inversion sense; /* current search spectrum */
Igor M. Liplianin17cce932011-01-25 17:02:00 -030074 u8 force; /* force mode/guard */
75 u8 bw; /* channel width 6, 7 or 8 in MHz */
76 u8 pBW; /* channel width used during previous lock */
77 u32 pBER;
78 u32 pPER;
79 u32 ucblocks;
80 s8 echo_pos; /* echo position */
81 u8 first_lock;
82 u8 unlock_counter;
83 u32 agc_val;
84};
85
86struct stv0367_state {
87 struct dvb_frontend fe;
88 struct i2c_adapter *i2c;
89 /* config settings */
90 const struct stv0367_config *config;
91 u8 chip_id;
92 /* DVB-C */
93 struct stv0367cab_state *cab_state;
94 /* DVB-T */
95 struct stv0367ter_state *ter_state;
96};
97
98struct st_register {
99 u16 addr;
100 u8 value;
101};
102
103/* values for STV4100 XTAL=30M int clk=53.125M*/
104static struct st_register def0367ter[STV0367TER_NBREGS] = {
105 {R367TER_ID, 0x60},
106 {R367TER_I2CRPT, 0xa0},
107 /* {R367TER_I2CRPT, 0x22},*/
108 {R367TER_TOPCTRL, 0x00},/* for xc5000; was 0x02 */
109 {R367TER_IOCFG0, 0x40},
110 {R367TER_DAC0R, 0x00},
111 {R367TER_IOCFG1, 0x00},
112 {R367TER_DAC1R, 0x00},
113 {R367TER_IOCFG2, 0x62},
114 {R367TER_SDFR, 0x00},
115 {R367TER_STATUS, 0xf8},
116 {R367TER_AUX_CLK, 0x0a},
117 {R367TER_FREESYS1, 0x00},
118 {R367TER_FREESYS2, 0x00},
119 {R367TER_FREESYS3, 0x00},
120 {R367TER_GPIO_CFG, 0x55},
121 {R367TER_GPIO_CMD, 0x00},
122 {R367TER_AGC2MAX, 0xff},
123 {R367TER_AGC2MIN, 0x00},
124 {R367TER_AGC1MAX, 0xff},
125 {R367TER_AGC1MIN, 0x00},
126 {R367TER_AGCR, 0xbc},
127 {R367TER_AGC2TH, 0x00},
128 {R367TER_AGC12C, 0x00},
129 {R367TER_AGCCTRL1, 0x85},
130 {R367TER_AGCCTRL2, 0x1f},
131 {R367TER_AGC1VAL1, 0x00},
132 {R367TER_AGC1VAL2, 0x00},
133 {R367TER_AGC2VAL1, 0x6f},
134 {R367TER_AGC2VAL2, 0x05},
135 {R367TER_AGC2PGA, 0x00},
136 {R367TER_OVF_RATE1, 0x00},
137 {R367TER_OVF_RATE2, 0x00},
138 {R367TER_GAIN_SRC1, 0xaa},/* for xc5000; was 0x2b */
139 {R367TER_GAIN_SRC2, 0xd6},/* for xc5000; was 0x04 */
140 {R367TER_INC_DEROT1, 0x55},
141 {R367TER_INC_DEROT2, 0x55},
142 {R367TER_PPM_CPAMP_DIR, 0x2c},
143 {R367TER_PPM_CPAMP_INV, 0x00},
144 {R367TER_FREESTFE_1, 0x00},
145 {R367TER_FREESTFE_2, 0x1c},
146 {R367TER_DCOFFSET, 0x00},
147 {R367TER_EN_PROCESS, 0x05},
148 {R367TER_SDI_SMOOTHER, 0x80},
149 {R367TER_FE_LOOP_OPEN, 0x1c},
150 {R367TER_FREQOFF1, 0x00},
151 {R367TER_FREQOFF2, 0x00},
152 {R367TER_FREQOFF3, 0x00},
153 {R367TER_TIMOFF1, 0x00},
154 {R367TER_TIMOFF2, 0x00},
155 {R367TER_EPQ, 0x02},
156 {R367TER_EPQAUTO, 0x01},
157 {R367TER_SYR_UPDATE, 0xf5},
158 {R367TER_CHPFREE, 0x00},
159 {R367TER_PPM_STATE_MAC, 0x23},
160 {R367TER_INR_THRESHOLD, 0xff},
161 {R367TER_EPQ_TPS_ID_CELL, 0xf9},
162 {R367TER_EPQ_CFG, 0x00},
163 {R367TER_EPQ_STATUS, 0x01},
164 {R367TER_AUTORELOCK, 0x81},
165 {R367TER_BER_THR_VMSB, 0x00},
166 {R367TER_BER_THR_MSB, 0x00},
167 {R367TER_BER_THR_LSB, 0x00},
168 {R367TER_CCD, 0x83},
169 {R367TER_SPECTR_CFG, 0x00},
170 {R367TER_CHC_DUMMY, 0x18},
171 {R367TER_INC_CTL, 0x88},
172 {R367TER_INCTHRES_COR1, 0xb4},
173 {R367TER_INCTHRES_COR2, 0x96},
174 {R367TER_INCTHRES_DET1, 0x0e},
175 {R367TER_INCTHRES_DET2, 0x11},
176 {R367TER_IIR_CELLNB, 0x8d},
177 {R367TER_IIRCX_COEFF1_MSB, 0x00},
178 {R367TER_IIRCX_COEFF1_LSB, 0x00},
179 {R367TER_IIRCX_COEFF2_MSB, 0x09},
180 {R367TER_IIRCX_COEFF2_LSB, 0x18},
181 {R367TER_IIRCX_COEFF3_MSB, 0x14},
182 {R367TER_IIRCX_COEFF3_LSB, 0x9c},
183 {R367TER_IIRCX_COEFF4_MSB, 0x00},
184 {R367TER_IIRCX_COEFF4_LSB, 0x00},
185 {R367TER_IIRCX_COEFF5_MSB, 0x36},
186 {R367TER_IIRCX_COEFF5_LSB, 0x42},
187 {R367TER_FEPATH_CFG, 0x00},
188 {R367TER_PMC1_FUNC, 0x65},
189 {R367TER_PMC1_FOR, 0x00},
190 {R367TER_PMC2_FUNC, 0x00},
191 {R367TER_STATUS_ERR_DA, 0xe0},
192 {R367TER_DIG_AGC_R, 0xfe},
193 {R367TER_COMAGC_TARMSB, 0x0b},
194 {R367TER_COM_AGC_TAR_ENMODE, 0x41},
195 {R367TER_COM_AGC_CFG, 0x3e},
196 {R367TER_COM_AGC_GAIN1, 0x39},
197 {R367TER_AUT_AGC_TARGETMSB, 0x0b},
198 {R367TER_LOCK_DET_MSB, 0x01},
199 {R367TER_AGCTAR_LOCK_LSBS, 0x40},
200 {R367TER_AUT_GAIN_EN, 0xf4},
201 {R367TER_AUT_CFG, 0xf0},
202 {R367TER_LOCKN, 0x23},
203 {R367TER_INT_X_3, 0x00},
204 {R367TER_INT_X_2, 0x03},
205 {R367TER_INT_X_1, 0x8d},
206 {R367TER_INT_X_0, 0xa0},
207 {R367TER_MIN_ERRX_MSB, 0x00},
208 {R367TER_COR_CTL, 0x23},
209 {R367TER_COR_STAT, 0xf6},
210 {R367TER_COR_INTEN, 0x00},
211 {R367TER_COR_INTSTAT, 0x3f},
212 {R367TER_COR_MODEGUARD, 0x03},
213 {R367TER_AGC_CTL, 0x08},
214 {R367TER_AGC_MANUAL1, 0x00},
215 {R367TER_AGC_MANUAL2, 0x00},
216 {R367TER_AGC_TARG, 0x16},
217 {R367TER_AGC_GAIN1, 0x53},
218 {R367TER_AGC_GAIN2, 0x1d},
219 {R367TER_RESERVED_1, 0x00},
220 {R367TER_RESERVED_2, 0x00},
221 {R367TER_RESERVED_3, 0x00},
222 {R367TER_CAS_CTL, 0x44},
223 {R367TER_CAS_FREQ, 0xb3},
224 {R367TER_CAS_DAGCGAIN, 0x12},
225 {R367TER_SYR_CTL, 0x04},
226 {R367TER_SYR_STAT, 0x10},
227 {R367TER_SYR_NCO1, 0x00},
228 {R367TER_SYR_NCO2, 0x00},
229 {R367TER_SYR_OFFSET1, 0x00},
230 {R367TER_SYR_OFFSET2, 0x00},
231 {R367TER_FFT_CTL, 0x00},
232 {R367TER_SCR_CTL, 0x70},
233 {R367TER_PPM_CTL1, 0xf8},
234 {R367TER_TRL_CTL, 0x14},/* for xc5000; was 0xac */
235 {R367TER_TRL_NOMRATE1, 0xae},/* for xc5000; was 0x1e */
236 {R367TER_TRL_NOMRATE2, 0x56},/* for xc5000; was 0x58 */
237 {R367TER_TRL_TIME1, 0x1d},
238 {R367TER_TRL_TIME2, 0xfc},
239 {R367TER_CRL_CTL, 0x24},
240 {R367TER_CRL_FREQ1, 0xad},
241 {R367TER_CRL_FREQ2, 0x9d},
242 {R367TER_CRL_FREQ3, 0xff},
243 {R367TER_CHC_CTL, 0x01},
244 {R367TER_CHC_SNR, 0xf0},
245 {R367TER_BDI_CTL, 0x00},
246 {R367TER_DMP_CTL, 0x00},
247 {R367TER_TPS_RCVD1, 0x30},
248 {R367TER_TPS_RCVD2, 0x02},
249 {R367TER_TPS_RCVD3, 0x01},
250 {R367TER_TPS_RCVD4, 0x00},
251 {R367TER_TPS_ID_CELL1, 0x00},
252 {R367TER_TPS_ID_CELL2, 0x00},
253 {R367TER_TPS_RCVD5_SET1, 0x02},
254 {R367TER_TPS_SET2, 0x02},
255 {R367TER_TPS_SET3, 0x01},
256 {R367TER_TPS_CTL, 0x00},
257 {R367TER_CTL_FFTOSNUM, 0x34},
258 {R367TER_TESTSELECT, 0x09},
259 {R367TER_MSC_REV, 0x0a},
260 {R367TER_PIR_CTL, 0x00},
261 {R367TER_SNR_CARRIER1, 0xa1},
262 {R367TER_SNR_CARRIER2, 0x9a},
263 {R367TER_PPM_CPAMP, 0x2c},
264 {R367TER_TSM_AP0, 0x00},
265 {R367TER_TSM_AP1, 0x00},
266 {R367TER_TSM_AP2 , 0x00},
267 {R367TER_TSM_AP3, 0x00},
268 {R367TER_TSM_AP4, 0x00},
269 {R367TER_TSM_AP5, 0x00},
270 {R367TER_TSM_AP6, 0x00},
271 {R367TER_TSM_AP7, 0x00},
272 {R367TER_TSTRES, 0x00},
273 {R367TER_ANACTRL, 0x0D},/* PLL stoped, restart at init!!! */
274 {R367TER_TSTBUS, 0x00},
275 {R367TER_TSTRATE, 0x00},
276 {R367TER_CONSTMODE, 0x01},
277 {R367TER_CONSTCARR1, 0x00},
278 {R367TER_CONSTCARR2, 0x00},
279 {R367TER_ICONSTEL, 0x0a},
280 {R367TER_QCONSTEL, 0x15},
281 {R367TER_TSTBISTRES0, 0x00},
282 {R367TER_TSTBISTRES1, 0x00},
283 {R367TER_TSTBISTRES2, 0x28},
284 {R367TER_TSTBISTRES3, 0x00},
285 {R367TER_RF_AGC1, 0xff},
286 {R367TER_RF_AGC2, 0x83},
287 {R367TER_ANADIGCTRL, 0x19},
288 {R367TER_PLLMDIV, 0x01},/* for xc5000; was 0x0c */
289 {R367TER_PLLNDIV, 0x06},/* for xc5000; was 0x55 */
290 {R367TER_PLLSETUP, 0x18},
Abylay Ospan01a475d2011-01-02 09:11:00 -0300291 {R367TER_DUAL_AD12, 0x0C},/* for xc5000 AGC voltage 1.6V */
Igor M. Liplianin17cce932011-01-25 17:02:00 -0300292 {R367TER_TSTBIST, 0x00},
293 {R367TER_PAD_COMP_CTRL, 0x00},
294 {R367TER_PAD_COMP_WR, 0x00},
295 {R367TER_PAD_COMP_RD, 0xe0},
296 {R367TER_SYR_TARGET_FFTADJT_MSB, 0x00},
297 {R367TER_SYR_TARGET_FFTADJT_LSB, 0x00},
298 {R367TER_SYR_TARGET_CHCADJT_MSB, 0x00},
299 {R367TER_SYR_TARGET_CHCADJT_LSB, 0x00},
300 {R367TER_SYR_FLAG, 0x00},
301 {R367TER_CRL_TARGET1, 0x00},
302 {R367TER_CRL_TARGET2, 0x00},
303 {R367TER_CRL_TARGET3, 0x00},
304 {R367TER_CRL_TARGET4, 0x00},
305 {R367TER_CRL_FLAG, 0x00},
306 {R367TER_TRL_TARGET1, 0x00},
307 {R367TER_TRL_TARGET2, 0x00},
308 {R367TER_TRL_CHC, 0x00},
309 {R367TER_CHC_SNR_TARG, 0x00},
310 {R367TER_TOP_TRACK, 0x00},
311 {R367TER_TRACKER_FREE1, 0x00},
312 {R367TER_ERROR_CRL1, 0x00},
313 {R367TER_ERROR_CRL2, 0x00},
314 {R367TER_ERROR_CRL3, 0x00},
315 {R367TER_ERROR_CRL4, 0x00},
316 {R367TER_DEC_NCO1, 0x2c},
317 {R367TER_DEC_NCO2, 0x0f},
318 {R367TER_DEC_NCO3, 0x20},
319 {R367TER_SNR, 0xf1},
320 {R367TER_SYR_FFTADJ1, 0x00},
321 {R367TER_SYR_FFTADJ2, 0x00},
322 {R367TER_SYR_CHCADJ1, 0x00},
323 {R367TER_SYR_CHCADJ2, 0x00},
324 {R367TER_SYR_OFF, 0x00},
325 {R367TER_PPM_OFFSET1, 0x00},
326 {R367TER_PPM_OFFSET2, 0x03},
327 {R367TER_TRACKER_FREE2, 0x00},
328 {R367TER_DEBG_LT10, 0x00},
329 {R367TER_DEBG_LT11, 0x00},
330 {R367TER_DEBG_LT12, 0x00},
331 {R367TER_DEBG_LT13, 0x00},
332 {R367TER_DEBG_LT14, 0x00},
333 {R367TER_DEBG_LT15, 0x00},
334 {R367TER_DEBG_LT16, 0x00},
335 {R367TER_DEBG_LT17, 0x00},
336 {R367TER_DEBG_LT18, 0x00},
337 {R367TER_DEBG_LT19, 0x00},
338 {R367TER_DEBG_LT1A, 0x00},
339 {R367TER_DEBG_LT1B, 0x00},
340 {R367TER_DEBG_LT1C, 0x00},
341 {R367TER_DEBG_LT1D, 0x00},
342 {R367TER_DEBG_LT1E, 0x00},
343 {R367TER_DEBG_LT1F, 0x00},
344 {R367TER_RCCFGH, 0x00},
345 {R367TER_RCCFGM, 0x00},
346 {R367TER_RCCFGL, 0x00},
347 {R367TER_RCINSDELH, 0x00},
348 {R367TER_RCINSDELM, 0x00},
349 {R367TER_RCINSDELL, 0x00},
350 {R367TER_RCSTATUS, 0x00},
351 {R367TER_RCSPEED, 0x6f},
352 {R367TER_RCDEBUGM, 0xe7},
353 {R367TER_RCDEBUGL, 0x9b},
354 {R367TER_RCOBSCFG, 0x00},
355 {R367TER_RCOBSM, 0x00},
356 {R367TER_RCOBSL, 0x00},
357 {R367TER_RCFECSPY, 0x00},
358 {R367TER_RCFSPYCFG, 0x00},
359 {R367TER_RCFSPYDATA, 0x00},
360 {R367TER_RCFSPYOUT, 0x00},
361 {R367TER_RCFSTATUS, 0x00},
362 {R367TER_RCFGOODPACK, 0x00},
363 {R367TER_RCFPACKCNT, 0x00},
364 {R367TER_RCFSPYMISC, 0x00},
365 {R367TER_RCFBERCPT4, 0x00},
366 {R367TER_RCFBERCPT3, 0x00},
367 {R367TER_RCFBERCPT2, 0x00},
368 {R367TER_RCFBERCPT1, 0x00},
369 {R367TER_RCFBERCPT0, 0x00},
370 {R367TER_RCFBERERR2, 0x00},
371 {R367TER_RCFBERERR1, 0x00},
372 {R367TER_RCFBERERR0, 0x00},
373 {R367TER_RCFSTATESM, 0x00},
374 {R367TER_RCFSTATESL, 0x00},
375 {R367TER_RCFSPYBER, 0x00},
376 {R367TER_RCFSPYDISTM, 0x00},
377 {R367TER_RCFSPYDISTL, 0x00},
378 {R367TER_RCFSPYOBS7, 0x00},
379 {R367TER_RCFSPYOBS6, 0x00},
380 {R367TER_RCFSPYOBS5, 0x00},
381 {R367TER_RCFSPYOBS4, 0x00},
382 {R367TER_RCFSPYOBS3, 0x00},
383 {R367TER_RCFSPYOBS2, 0x00},
384 {R367TER_RCFSPYOBS1, 0x00},
385 {R367TER_RCFSPYOBS0, 0x00},
386 {R367TER_TSGENERAL, 0x00},
387 {R367TER_RC1SPEED, 0x6f},
388 {R367TER_TSGSTATUS, 0x18},
389 {R367TER_FECM, 0x01},
390 {R367TER_VTH12, 0xff},
391 {R367TER_VTH23, 0xa1},
392 {R367TER_VTH34, 0x64},
393 {R367TER_VTH56, 0x40},
394 {R367TER_VTH67, 0x00},
395 {R367TER_VTH78, 0x2c},
396 {R367TER_VITCURPUN, 0x12},
397 {R367TER_VERROR, 0x01},
398 {R367TER_PRVIT, 0x3f},
399 {R367TER_VAVSRVIT, 0x00},
400 {R367TER_VSTATUSVIT, 0xbd},
401 {R367TER_VTHINUSE, 0xa1},
402 {R367TER_KDIV12, 0x20},
403 {R367TER_KDIV23, 0x40},
404 {R367TER_KDIV34, 0x20},
405 {R367TER_KDIV56, 0x30},
406 {R367TER_KDIV67, 0x00},
407 {R367TER_KDIV78, 0x30},
408 {R367TER_SIGPOWER, 0x54},
409 {R367TER_DEMAPVIT, 0x40},
410 {R367TER_VITSCALE, 0x00},
411 {R367TER_FFEC1PRG, 0x00},
412 {R367TER_FVITCURPUN, 0x12},
413 {R367TER_FVERROR, 0x01},
414 {R367TER_FVSTATUSVIT, 0xbd},
415 {R367TER_DEBUG_LT1, 0x00},
416 {R367TER_DEBUG_LT2, 0x00},
417 {R367TER_DEBUG_LT3, 0x00},
418 {R367TER_TSTSFMET, 0x00},
419 {R367TER_SELOUT, 0x00},
420 {R367TER_TSYNC, 0x00},
421 {R367TER_TSTERR, 0x00},
422 {R367TER_TSFSYNC, 0x00},
423 {R367TER_TSTSFERR, 0x00},
424 {R367TER_TSTTSSF1, 0x01},
425 {R367TER_TSTTSSF2, 0x1f},
426 {R367TER_TSTTSSF3, 0x00},
427 {R367TER_TSTTS1, 0x00},
428 {R367TER_TSTTS2, 0x1f},
429 {R367TER_TSTTS3, 0x01},
430 {R367TER_TSTTS4, 0x00},
431 {R367TER_TSTTSRC, 0x00},
432 {R367TER_TSTTSRS, 0x00},
433 {R367TER_TSSTATEM, 0xb0},
434 {R367TER_TSSTATEL, 0x40},
435 {R367TER_TSCFGH, 0xC0},
436 {R367TER_TSCFGM, 0xc0},/* for xc5000; was 0x00 */
437 {R367TER_TSCFGL, 0x20},
438 {R367TER_TSSYNC, 0x00},
439 {R367TER_TSINSDELH, 0x00},
440 {R367TER_TSINSDELM, 0x00},
441 {R367TER_TSINSDELL, 0x00},
442 {R367TER_TSDIVN, 0x03},
443 {R367TER_TSDIVPM, 0x00},
444 {R367TER_TSDIVPL, 0x00},
445 {R367TER_TSDIVQM, 0x00},
446 {R367TER_TSDIVQL, 0x00},
447 {R367TER_TSDILSTKM, 0x00},
448 {R367TER_TSDILSTKL, 0x00},
449 {R367TER_TSSPEED, 0x40},/* for xc5000; was 0x6f */
450 {R367TER_TSSTATUS, 0x81},
451 {R367TER_TSSTATUS2, 0x6a},
452 {R367TER_TSBITRATEM, 0x0f},
453 {R367TER_TSBITRATEL, 0xc6},
454 {R367TER_TSPACKLENM, 0x00},
455 {R367TER_TSPACKLENL, 0xfc},
456 {R367TER_TSBLOCLENM, 0x0a},
457 {R367TER_TSBLOCLENL, 0x80},
458 {R367TER_TSDLYH, 0x90},
459 {R367TER_TSDLYM, 0x68},
460 {R367TER_TSDLYL, 0x01},
461 {R367TER_TSNPDAV, 0x00},
462 {R367TER_TSBUFSTATH, 0x00},
463 {R367TER_TSBUFSTATM, 0x00},
464 {R367TER_TSBUFSTATL, 0x00},
465 {R367TER_TSDEBUGM, 0xcf},
466 {R367TER_TSDEBUGL, 0x1e},
467 {R367TER_TSDLYSETH, 0x00},
468 {R367TER_TSDLYSETM, 0x68},
469 {R367TER_TSDLYSETL, 0x00},
470 {R367TER_TSOBSCFG, 0x00},
471 {R367TER_TSOBSM, 0x47},
472 {R367TER_TSOBSL, 0x1f},
473 {R367TER_ERRCTRL1, 0x95},
474 {R367TER_ERRCNT1H, 0x80},
475 {R367TER_ERRCNT1M, 0x00},
476 {R367TER_ERRCNT1L, 0x00},
477 {R367TER_ERRCTRL2, 0x95},
478 {R367TER_ERRCNT2H, 0x00},
479 {R367TER_ERRCNT2M, 0x00},
480 {R367TER_ERRCNT2L, 0x00},
481 {R367TER_FECSPY, 0x88},
482 {R367TER_FSPYCFG, 0x2c},
483 {R367TER_FSPYDATA, 0x3a},
484 {R367TER_FSPYOUT, 0x06},
485 {R367TER_FSTATUS, 0x61},
486 {R367TER_FGOODPACK, 0xff},
487 {R367TER_FPACKCNT, 0xff},
488 {R367TER_FSPYMISC, 0x66},
489 {R367TER_FBERCPT4, 0x00},
490 {R367TER_FBERCPT3, 0x00},
491 {R367TER_FBERCPT2, 0x36},
492 {R367TER_FBERCPT1, 0x36},
493 {R367TER_FBERCPT0, 0x14},
494 {R367TER_FBERERR2, 0x00},
495 {R367TER_FBERERR1, 0x03},
496 {R367TER_FBERERR0, 0x28},
497 {R367TER_FSTATESM, 0x00},
498 {R367TER_FSTATESL, 0x02},
499 {R367TER_FSPYBER, 0x00},
500 {R367TER_FSPYDISTM, 0x01},
501 {R367TER_FSPYDISTL, 0x9f},
502 {R367TER_FSPYOBS7, 0xc9},
503 {R367TER_FSPYOBS6, 0x99},
504 {R367TER_FSPYOBS5, 0x08},
505 {R367TER_FSPYOBS4, 0xec},
506 {R367TER_FSPYOBS3, 0x01},
507 {R367TER_FSPYOBS2, 0x0f},
508 {R367TER_FSPYOBS1, 0xf5},
509 {R367TER_FSPYOBS0, 0x08},
510 {R367TER_SFDEMAP, 0x40},
511 {R367TER_SFERROR, 0x00},
512 {R367TER_SFAVSR, 0x30},
513 {R367TER_SFECSTATUS, 0xcc},
514 {R367TER_SFKDIV12, 0x20},
515 {R367TER_SFKDIV23, 0x40},
516 {R367TER_SFKDIV34, 0x20},
517 {R367TER_SFKDIV56, 0x20},
518 {R367TER_SFKDIV67, 0x00},
519 {R367TER_SFKDIV78, 0x20},
520 {R367TER_SFDILSTKM, 0x00},
521 {R367TER_SFDILSTKL, 0x00},
522 {R367TER_SFSTATUS, 0xb5},
523 {R367TER_SFDLYH, 0x90},
524 {R367TER_SFDLYM, 0x60},
525 {R367TER_SFDLYL, 0x01},
526 {R367TER_SFDLYSETH, 0xc0},
527 {R367TER_SFDLYSETM, 0x60},
528 {R367TER_SFDLYSETL, 0x00},
529 {R367TER_SFOBSCFG, 0x00},
530 {R367TER_SFOBSM, 0x47},
531 {R367TER_SFOBSL, 0x05},
532 {R367TER_SFECINFO, 0x40},
533 {R367TER_SFERRCTRL, 0x74},
534 {R367TER_SFERRCNTH, 0x80},
535 {R367TER_SFERRCNTM , 0x00},
536 {R367TER_SFERRCNTL, 0x00},
537 {R367TER_SYMBRATEM, 0x2f},
538 {R367TER_SYMBRATEL, 0x50},
539 {R367TER_SYMBSTATUS, 0x7f},
540 {R367TER_SYMBCFG, 0x00},
541 {R367TER_SYMBFIFOM, 0xf4},
542 {R367TER_SYMBFIFOL, 0x0d},
543 {R367TER_SYMBOFFSM, 0xf0},
544 {R367TER_SYMBOFFSL, 0x2d},
545 {R367TER_DEBUG_LT4, 0x00},
546 {R367TER_DEBUG_LT5, 0x00},
547 {R367TER_DEBUG_LT6, 0x00},
548 {R367TER_DEBUG_LT7, 0x00},
549 {R367TER_DEBUG_LT8, 0x00},
550 {R367TER_DEBUG_LT9, 0x00},
551};
552
553#define RF_LOOKUP_TABLE_SIZE 31
554#define RF_LOOKUP_TABLE2_SIZE 16
555/* RF Level (for RF AGC->AGC1) Lookup Table, depends on the board and tuner.*/
Hans Verkuil817d2fd2014-08-20 19:30:33 -0300556static const s32 stv0367cab_RF_LookUp1[RF_LOOKUP_TABLE_SIZE][RF_LOOKUP_TABLE_SIZE] = {
Igor M. Liplianin17cce932011-01-25 17:02:00 -0300557 {/*AGC1*/
558 48, 50, 51, 53, 54, 56, 57, 58, 60, 61, 62, 63,
559 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75,
560 76, 77, 78, 80, 83, 85, 88,
561 }, {/*RF(dbm)*/
562 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33,
563 34, 35, 36, 37, 38, 39, 41, 42, 43, 44, 46, 47,
564 49, 50, 52, 53, 54, 55, 56,
565 }
566};
567/* RF Level (for IF AGC->AGC2) Lookup Table, depends on the board and tuner.*/
Hans Verkuil817d2fd2014-08-20 19:30:33 -0300568static const s32 stv0367cab_RF_LookUp2[RF_LOOKUP_TABLE2_SIZE][RF_LOOKUP_TABLE2_SIZE] = {
Igor M. Liplianin17cce932011-01-25 17:02:00 -0300569 {/*AGC2*/
570 28, 29, 31, 32, 34, 35, 36, 37,
571 38, 39, 40, 41, 42, 43, 44, 45,
572 }, {/*RF(dbm)*/
573 57, 58, 59, 60, 61, 62, 63, 64,
574 65, 66, 67, 68, 69, 70, 71, 72,
575 }
576};
577
578static struct st_register def0367cab[STV0367CAB_NBREGS] = {
579 {R367CAB_ID, 0x60},
580 {R367CAB_I2CRPT, 0xa0},
581 /*{R367CAB_I2CRPT, 0x22},*/
582 {R367CAB_TOPCTRL, 0x10},
583 {R367CAB_IOCFG0, 0x80},
584 {R367CAB_DAC0R, 0x00},
585 {R367CAB_IOCFG1, 0x00},
586 {R367CAB_DAC1R, 0x00},
587 {R367CAB_IOCFG2, 0x00},
588 {R367CAB_SDFR, 0x00},
589 {R367CAB_AUX_CLK, 0x00},
590 {R367CAB_FREESYS1, 0x00},
591 {R367CAB_FREESYS2, 0x00},
592 {R367CAB_FREESYS3, 0x00},
593 {R367CAB_GPIO_CFG, 0x55},
594 {R367CAB_GPIO_CMD, 0x01},
595 {R367CAB_TSTRES, 0x00},
596 {R367CAB_ANACTRL, 0x0d},/* was 0x00 need to check - I.M.L.*/
597 {R367CAB_TSTBUS, 0x00},
598 {R367CAB_RF_AGC1, 0xea},
599 {R367CAB_RF_AGC2, 0x82},
600 {R367CAB_ANADIGCTRL, 0x0b},
601 {R367CAB_PLLMDIV, 0x01},
602 {R367CAB_PLLNDIV, 0x08},
603 {R367CAB_PLLSETUP, 0x18},
Abylay Ospan01a475d2011-01-02 09:11:00 -0300604 {R367CAB_DUAL_AD12, 0x0C}, /* for xc5000 AGC voltage 1.6V */
Igor M. Liplianin17cce932011-01-25 17:02:00 -0300605 {R367CAB_TSTBIST, 0x00},
606 {R367CAB_CTRL_1, 0x00},
607 {R367CAB_CTRL_2, 0x03},
608 {R367CAB_IT_STATUS1, 0x2b},
609 {R367CAB_IT_STATUS2, 0x08},
610 {R367CAB_IT_EN1, 0x00},
611 {R367CAB_IT_EN2, 0x00},
612 {R367CAB_CTRL_STATUS, 0x04},
613 {R367CAB_TEST_CTL, 0x00},
614 {R367CAB_AGC_CTL, 0x73},
615 {R367CAB_AGC_IF_CFG, 0x50},
616 {R367CAB_AGC_RF_CFG, 0x00},
617 {R367CAB_AGC_PWM_CFG, 0x03},
618 {R367CAB_AGC_PWR_REF_L, 0x5a},
619 {R367CAB_AGC_PWR_REF_H, 0x00},
620 {R367CAB_AGC_RF_TH_L, 0xff},
621 {R367CAB_AGC_RF_TH_H, 0x07},
622 {R367CAB_AGC_IF_LTH_L, 0x00},
623 {R367CAB_AGC_IF_LTH_H, 0x08},
624 {R367CAB_AGC_IF_HTH_L, 0xff},
625 {R367CAB_AGC_IF_HTH_H, 0x07},
626 {R367CAB_AGC_PWR_RD_L, 0xa0},
627 {R367CAB_AGC_PWR_RD_M, 0xe9},
628 {R367CAB_AGC_PWR_RD_H, 0x03},
629 {R367CAB_AGC_PWM_IFCMD_L, 0xe4},
630 {R367CAB_AGC_PWM_IFCMD_H, 0x00},
631 {R367CAB_AGC_PWM_RFCMD_L, 0xff},
632 {R367CAB_AGC_PWM_RFCMD_H, 0x07},
633 {R367CAB_IQDEM_CFG, 0x01},
634 {R367CAB_MIX_NCO_LL, 0x22},
635 {R367CAB_MIX_NCO_HL, 0x96},
636 {R367CAB_MIX_NCO_HH, 0x55},
637 {R367CAB_SRC_NCO_LL, 0xff},
638 {R367CAB_SRC_NCO_LH, 0x0c},
639 {R367CAB_SRC_NCO_HL, 0xf5},
640 {R367CAB_SRC_NCO_HH, 0x20},
641 {R367CAB_IQDEM_GAIN_SRC_L, 0x06},
642 {R367CAB_IQDEM_GAIN_SRC_H, 0x01},
643 {R367CAB_IQDEM_DCRM_CFG_LL, 0xfe},
644 {R367CAB_IQDEM_DCRM_CFG_LH, 0xff},
645 {R367CAB_IQDEM_DCRM_CFG_HL, 0x0f},
646 {R367CAB_IQDEM_DCRM_CFG_HH, 0x00},
647 {R367CAB_IQDEM_ADJ_COEFF0, 0x34},
648 {R367CAB_IQDEM_ADJ_COEFF1, 0xae},
649 {R367CAB_IQDEM_ADJ_COEFF2, 0x46},
650 {R367CAB_IQDEM_ADJ_COEFF3, 0x77},
651 {R367CAB_IQDEM_ADJ_COEFF4, 0x96},
652 {R367CAB_IQDEM_ADJ_COEFF5, 0x69},
653 {R367CAB_IQDEM_ADJ_COEFF6, 0xc7},
654 {R367CAB_IQDEM_ADJ_COEFF7, 0x01},
655 {R367CAB_IQDEM_ADJ_EN, 0x04},
656 {R367CAB_IQDEM_ADJ_AGC_REF, 0x94},
657 {R367CAB_ALLPASSFILT1, 0xc9},
658 {R367CAB_ALLPASSFILT2, 0x2d},
659 {R367CAB_ALLPASSFILT3, 0xa3},
660 {R367CAB_ALLPASSFILT4, 0xfb},
661 {R367CAB_ALLPASSFILT5, 0xf6},
662 {R367CAB_ALLPASSFILT6, 0x45},
663 {R367CAB_ALLPASSFILT7, 0x6f},
664 {R367CAB_ALLPASSFILT8, 0x7e},
665 {R367CAB_ALLPASSFILT9, 0x05},
666 {R367CAB_ALLPASSFILT10, 0x0a},
667 {R367CAB_ALLPASSFILT11, 0x51},
668 {R367CAB_TRL_AGC_CFG, 0x20},
669 {R367CAB_TRL_LPF_CFG, 0x28},
670 {R367CAB_TRL_LPF_ACQ_GAIN, 0x44},
671 {R367CAB_TRL_LPF_TRK_GAIN, 0x22},
672 {R367CAB_TRL_LPF_OUT_GAIN, 0x03},
673 {R367CAB_TRL_LOCKDET_LTH, 0x04},
674 {R367CAB_TRL_LOCKDET_HTH, 0x11},
675 {R367CAB_TRL_LOCKDET_TRGVAL, 0x20},
676 {R367CAB_IQ_QAM, 0x01},
677 {R367CAB_FSM_STATE, 0xa0},
678 {R367CAB_FSM_CTL, 0x08},
679 {R367CAB_FSM_STS, 0x0c},
680 {R367CAB_FSM_SNR0_HTH, 0x00},
681 {R367CAB_FSM_SNR1_HTH, 0x00},
682 {R367CAB_FSM_SNR2_HTH, 0x23},/* 0x00 */
683 {R367CAB_FSM_SNR0_LTH, 0x00},
684 {R367CAB_FSM_SNR1_LTH, 0x00},
685 {R367CAB_FSM_EQA1_HTH, 0x00},
686 {R367CAB_FSM_TEMPO, 0x32},
687 {R367CAB_FSM_CONFIG, 0x03},
688 {R367CAB_EQU_I_TESTTAP_L, 0x11},
689 {R367CAB_EQU_I_TESTTAP_M, 0x00},
690 {R367CAB_EQU_I_TESTTAP_H, 0x00},
691 {R367CAB_EQU_TESTAP_CFG, 0x00},
692 {R367CAB_EQU_Q_TESTTAP_L, 0xff},
693 {R367CAB_EQU_Q_TESTTAP_M, 0x00},
694 {R367CAB_EQU_Q_TESTTAP_H, 0x00},
695 {R367CAB_EQU_TAP_CTRL, 0x00},
696 {R367CAB_EQU_CTR_CRL_CONTROL_L, 0x11},
697 {R367CAB_EQU_CTR_CRL_CONTROL_H, 0x05},
698 {R367CAB_EQU_CTR_HIPOW_L, 0x00},
699 {R367CAB_EQU_CTR_HIPOW_H, 0x00},
700 {R367CAB_EQU_I_EQU_LO, 0xef},
701 {R367CAB_EQU_I_EQU_HI, 0x00},
702 {R367CAB_EQU_Q_EQU_LO, 0xee},
703 {R367CAB_EQU_Q_EQU_HI, 0x00},
704 {R367CAB_EQU_MAPPER, 0xc5},
705 {R367CAB_EQU_SWEEP_RATE, 0x80},
706 {R367CAB_EQU_SNR_LO, 0x64},
707 {R367CAB_EQU_SNR_HI, 0x03},
708 {R367CAB_EQU_GAMMA_LO, 0x00},
709 {R367CAB_EQU_GAMMA_HI, 0x00},
710 {R367CAB_EQU_ERR_GAIN, 0x36},
711 {R367CAB_EQU_RADIUS, 0xaa},
712 {R367CAB_EQU_FFE_MAINTAP, 0x00},
713 {R367CAB_EQU_FFE_LEAKAGE, 0x63},
714 {R367CAB_EQU_FFE_MAINTAP_POS, 0xdf},
715 {R367CAB_EQU_GAIN_WIDE, 0x88},
716 {R367CAB_EQU_GAIN_NARROW, 0x41},
717 {R367CAB_EQU_CTR_LPF_GAIN, 0xd1},
718 {R367CAB_EQU_CRL_LPF_GAIN, 0xa7},
719 {R367CAB_EQU_GLOBAL_GAIN, 0x06},
720 {R367CAB_EQU_CRL_LD_SEN, 0x85},
721 {R367CAB_EQU_CRL_LD_VAL, 0xe2},
722 {R367CAB_EQU_CRL_TFR, 0x20},
723 {R367CAB_EQU_CRL_BISTH_LO, 0x00},
724 {R367CAB_EQU_CRL_BISTH_HI, 0x00},
725 {R367CAB_EQU_SWEEP_RANGE_LO, 0x00},
726 {R367CAB_EQU_SWEEP_RANGE_HI, 0x00},
727 {R367CAB_EQU_CRL_LIMITER, 0x40},
728 {R367CAB_EQU_MODULUS_MAP, 0x90},
729 {R367CAB_EQU_PNT_GAIN, 0xa7},
730 {R367CAB_FEC_AC_CTR_0, 0x16},
731 {R367CAB_FEC_AC_CTR_1, 0x0b},
732 {R367CAB_FEC_AC_CTR_2, 0x88},
733 {R367CAB_FEC_AC_CTR_3, 0x02},
734 {R367CAB_FEC_STATUS, 0x12},
735 {R367CAB_RS_COUNTER_0, 0x7d},
736 {R367CAB_RS_COUNTER_1, 0xd0},
737 {R367CAB_RS_COUNTER_2, 0x19},
738 {R367CAB_RS_COUNTER_3, 0x0b},
739 {R367CAB_RS_COUNTER_4, 0xa3},
740 {R367CAB_RS_COUNTER_5, 0x00},
741 {R367CAB_BERT_0, 0x01},
742 {R367CAB_BERT_1, 0x25},
743 {R367CAB_BERT_2, 0x41},
744 {R367CAB_BERT_3, 0x39},
745 {R367CAB_OUTFORMAT_0, 0xc2},
746 {R367CAB_OUTFORMAT_1, 0x22},
747 {R367CAB_SMOOTHER_2, 0x28},
748 {R367CAB_TSMF_CTRL_0, 0x01},
749 {R367CAB_TSMF_CTRL_1, 0xc6},
750 {R367CAB_TSMF_CTRL_3, 0x43},
751 {R367CAB_TS_ON_ID_0, 0x00},
752 {R367CAB_TS_ON_ID_1, 0x00},
753 {R367CAB_TS_ON_ID_2, 0x00},
754 {R367CAB_TS_ON_ID_3, 0x00},
755 {R367CAB_RE_STATUS_0, 0x00},
756 {R367CAB_RE_STATUS_1, 0x00},
757 {R367CAB_RE_STATUS_2, 0x00},
758 {R367CAB_RE_STATUS_3, 0x00},
759 {R367CAB_TS_STATUS_0, 0x00},
760 {R367CAB_TS_STATUS_1, 0x00},
761 {R367CAB_TS_STATUS_2, 0xa0},
762 {R367CAB_TS_STATUS_3, 0x00},
763 {R367CAB_T_O_ID_0, 0x00},
764 {R367CAB_T_O_ID_1, 0x00},
765 {R367CAB_T_O_ID_2, 0x00},
766 {R367CAB_T_O_ID_3, 0x00},
767};
768
769static
770int stv0367_writeregs(struct stv0367_state *state, u16 reg, u8 *data, int len)
771{
Mauro Carvalho Chehab9aca4fb2013-11-02 05:17:01 -0300772 u8 buf[MAX_XFER_SIZE];
Igor M. Liplianin17cce932011-01-25 17:02:00 -0300773 struct i2c_msg msg = {
774 .addr = state->config->demod_address,
775 .flags = 0,
776 .buf = buf,
777 .len = len + 2
778 };
779 int ret;
780
Mauro Carvalho Chehab9aca4fb2013-11-02 05:17:01 -0300781 if (2 + len > sizeof(buf)) {
782 printk(KERN_WARNING
783 "%s: i2c wr reg=%04x: len=%d is too big!\n",
784 KBUILD_MODNAME, reg, len);
785 return -EINVAL;
786 }
787
788
Igor M. Liplianin17cce932011-01-25 17:02:00 -0300789 buf[0] = MSB(reg);
790 buf[1] = LSB(reg);
791 memcpy(buf + 2, data, len);
792
793 if (i2cdebug)
Peter Griffin86a10282015-07-30 14:08:51 -0300794 printk(KERN_DEBUG "%s: [%02x] %02x: %02x\n", __func__,
795 state->config->demod_address, reg, buf[2]);
Igor M. Liplianin17cce932011-01-25 17:02:00 -0300796
797 ret = i2c_transfer(state->i2c, &msg, 1);
798 if (ret != 1)
Peter Griffin86a10282015-07-30 14:08:51 -0300799 printk(KERN_ERR "%s: i2c write error! ([%02x] %02x: %02x)\n",
800 __func__, state->config->demod_address, reg, buf[2]);
Igor M. Liplianin17cce932011-01-25 17:02:00 -0300801
802 return (ret != 1) ? -EREMOTEIO : 0;
803}
804
805static int stv0367_writereg(struct stv0367_state *state, u16 reg, u8 data)
806{
807 return stv0367_writeregs(state, reg, &data, 1);
808}
809
810static u8 stv0367_readreg(struct stv0367_state *state, u16 reg)
811{
812 u8 b0[] = { 0, 0 };
813 u8 b1[] = { 0 };
814 struct i2c_msg msg[] = {
815 {
816 .addr = state->config->demod_address,
817 .flags = 0,
818 .buf = b0,
819 .len = 2
820 }, {
821 .addr = state->config->demod_address,
822 .flags = I2C_M_RD,
823 .buf = b1,
824 .len = 1
825 }
826 };
827 int ret;
828
829 b0[0] = MSB(reg);
830 b0[1] = LSB(reg);
831
832 ret = i2c_transfer(state->i2c, msg, 2);
833 if (ret != 2)
Peter Griffin86a10282015-07-30 14:08:51 -0300834 printk(KERN_ERR "%s: i2c read error ([%02x] %02x: %02x)\n",
835 __func__, state->config->demod_address, reg, b1[0]);
Igor M. Liplianin17cce932011-01-25 17:02:00 -0300836
837 if (i2cdebug)
Peter Griffin86a10282015-07-30 14:08:51 -0300838 printk(KERN_DEBUG "%s: [%02x] %02x: %02x\n", __func__,
839 state->config->demod_address, reg, b1[0]);
Igor M. Liplianin17cce932011-01-25 17:02:00 -0300840
841 return b1[0];
842}
843
844static void extract_mask_pos(u32 label, u8 *mask, u8 *pos)
845{
846 u8 position = 0, i = 0;
847
848 (*mask) = label & 0xff;
849
850 while ((position == 0) && (i < 8)) {
851 position = ((*mask) >> i) & 0x01;
852 i++;
853 }
854
855 (*pos) = (i - 1);
856}
857
858static void stv0367_writebits(struct stv0367_state *state, u32 label, u8 val)
859{
860 u8 reg, mask, pos;
861
862 reg = stv0367_readreg(state, (label >> 16) & 0xffff);
863 extract_mask_pos(label, &mask, &pos);
864
865 val = mask & (val << pos);
866
867 reg = (reg & (~mask)) | val;
868 stv0367_writereg(state, (label >> 16) & 0xffff, reg);
869
870}
871
872static void stv0367_setbits(u8 *reg, u32 label, u8 val)
873{
874 u8 mask, pos;
875
876 extract_mask_pos(label, &mask, &pos);
877
878 val = mask & (val << pos);
879
880 (*reg) = ((*reg) & (~mask)) | val;
881}
882
883static u8 stv0367_readbits(struct stv0367_state *state, u32 label)
884{
885 u8 val = 0xff;
886 u8 mask, pos;
887
888 extract_mask_pos(label, &mask, &pos);
889
890 val = stv0367_readreg(state, label >> 16);
891 val = (val & mask) >> pos;
892
893 return val;
894}
895
Mauro Carvalho Chehab8c8ca1c2012-10-27 11:26:42 -0300896#if 0 /* Currently, unused */
897static u8 stv0367_getbits(u8 reg, u32 label)
Igor M. Liplianin17cce932011-01-25 17:02:00 -0300898{
899 u8 mask, pos;
900
901 extract_mask_pos(label, &mask, &pos);
902
903 return (reg & mask) >> pos;
904}
Mauro Carvalho Chehab8c8ca1c2012-10-27 11:26:42 -0300905#endif
Igor M. Liplianin17cce932011-01-25 17:02:00 -0300906static int stv0367ter_gate_ctrl(struct dvb_frontend *fe, int enable)
907{
908 struct stv0367_state *state = fe->demodulator_priv;
909 u8 tmp = stv0367_readreg(state, R367TER_I2CRPT);
910
911 dprintk("%s:\n", __func__);
912
913 if (enable) {
914 stv0367_setbits(&tmp, F367TER_STOP_ENABLE, 0);
915 stv0367_setbits(&tmp, F367TER_I2CT_ON, 1);
916 } else {
917 stv0367_setbits(&tmp, F367TER_STOP_ENABLE, 1);
918 stv0367_setbits(&tmp, F367TER_I2CT_ON, 0);
919 }
920
921 stv0367_writereg(state, R367TER_I2CRPT, tmp);
922
923 return 0;
924}
925
926static u32 stv0367_get_tuner_freq(struct dvb_frontend *fe)
927{
Emil Goode20721182014-06-24 18:42:27 -0300928 struct dvb_frontend_ops *frontend_ops = &fe->ops;
929 struct dvb_tuner_ops *tuner_ops = &frontend_ops->tuner_ops;
Igor M. Liplianin17cce932011-01-25 17:02:00 -0300930 u32 freq = 0;
Dan Carpenterbf512b22011-03-06 10:40:11 -0300931 int err = 0;
Igor M. Liplianin17cce932011-01-25 17:02:00 -0300932
933 dprintk("%s:\n", __func__);
934
Igor M. Liplianin17cce932011-01-25 17:02:00 -0300935 if (tuner_ops->get_frequency) {
936 err = tuner_ops->get_frequency(fe, &freq);
937 if (err < 0) {
938 printk(KERN_ERR "%s: Invalid parameter\n", __func__);
939 return err;
940 }
941
942 dprintk("%s: frequency=%d\n", __func__, freq);
943
944 } else
945 return -1;
946
947 return freq;
948}
949
950static u16 CellsCoeffs_8MHz_367cofdm[3][6][5] = {
951 {
952 {0x10EF, 0xE205, 0x10EF, 0xCE49, 0x6DA7}, /* CELL 1 COEFFS 27M*/
953 {0x2151, 0xc557, 0x2151, 0xc705, 0x6f93}, /* CELL 2 COEFFS */
954 {0x2503, 0xc000, 0x2503, 0xc375, 0x7194}, /* CELL 3 COEFFS */
955 {0x20E9, 0xca94, 0x20e9, 0xc153, 0x7194}, /* CELL 4 COEFFS */
956 {0x06EF, 0xF852, 0x06EF, 0xC057, 0x7207}, /* CELL 5 COEFFS */
957 {0x0000, 0x0ECC, 0x0ECC, 0x0000, 0x3647} /* CELL 6 COEFFS */
958 }, {
959 {0x10A0, 0xE2AF, 0x10A1, 0xCE76, 0x6D6D}, /* CELL 1 COEFFS 25M*/
960 {0x20DC, 0xC676, 0x20D9, 0xC80A, 0x6F29},
961 {0x2532, 0xC000, 0x251D, 0xC391, 0x706F},
962 {0x1F7A, 0xCD2B, 0x2032, 0xC15E, 0x711F},
963 {0x0698, 0xFA5E, 0x0568, 0xC059, 0x7193},
964 {0x0000, 0x0918, 0x149C, 0x0000, 0x3642} /* CELL 6 COEFFS */
965 }, {
966 {0x0000, 0x0000, 0x0000, 0x0000, 0x0000}, /* 30M */
967 {0x0000, 0x0000, 0x0000, 0x0000, 0x0000},
968 {0x0000, 0x0000, 0x0000, 0x0000, 0x0000},
969 {0x0000, 0x0000, 0x0000, 0x0000, 0x0000},
970 {0x0000, 0x0000, 0x0000, 0x0000, 0x0000},
971 {0x0000, 0x0000, 0x0000, 0x0000, 0x0000}
972 }
973};
974
975static u16 CellsCoeffs_7MHz_367cofdm[3][6][5] = {
976 {
977 {0x12CA, 0xDDAF, 0x12CA, 0xCCEB, 0x6FB1}, /* CELL 1 COEFFS 27M*/
978 {0x2329, 0xC000, 0x2329, 0xC6B0, 0x725F}, /* CELL 2 COEFFS */
979 {0x2394, 0xC000, 0x2394, 0xC2C7, 0x7410}, /* CELL 3 COEFFS */
980 {0x251C, 0xC000, 0x251C, 0xC103, 0x74D9}, /* CELL 4 COEFFS */
981 {0x0804, 0xF546, 0x0804, 0xC040, 0x7544}, /* CELL 5 COEFFS */
982 {0x0000, 0x0CD9, 0x0CD9, 0x0000, 0x370A} /* CELL 6 COEFFS */
983 }, {
984 {0x1285, 0xDE47, 0x1285, 0xCD17, 0x6F76}, /*25M*/
985 {0x234C, 0xC000, 0x2348, 0xC6DA, 0x7206},
986 {0x23B4, 0xC000, 0x23AC, 0xC2DB, 0x73B3},
987 {0x253D, 0xC000, 0x25B6, 0xC10B, 0x747F},
988 {0x0721, 0xF79C, 0x065F, 0xC041, 0x74EB},
989 {0x0000, 0x08FA, 0x1162, 0x0000, 0x36FF}
990 }, {
991 {0x0000, 0x0000, 0x0000, 0x0000, 0x0000}, /* 30M */
992 {0x0000, 0x0000, 0x0000, 0x0000, 0x0000},
993 {0x0000, 0x0000, 0x0000, 0x0000, 0x0000},
994 {0x0000, 0x0000, 0x0000, 0x0000, 0x0000},
995 {0x0000, 0x0000, 0x0000, 0x0000, 0x0000},
996 {0x0000, 0x0000, 0x0000, 0x0000, 0x0000}
997 }
998};
999
1000static u16 CellsCoeffs_6MHz_367cofdm[3][6][5] = {
1001 {
1002 {0x1699, 0xD5B8, 0x1699, 0xCBC3, 0x713B}, /* CELL 1 COEFFS 27M*/
1003 {0x2245, 0xC000, 0x2245, 0xC568, 0x74D5}, /* CELL 2 COEFFS */
1004 {0x227F, 0xC000, 0x227F, 0xC1FC, 0x76C6}, /* CELL 3 COEFFS */
1005 {0x235E, 0xC000, 0x235E, 0xC0A7, 0x778A}, /* CELL 4 COEFFS */
1006 {0x0ECB, 0xEA0B, 0x0ECB, 0xC027, 0x77DD}, /* CELL 5 COEFFS */
1007 {0x0000, 0x0B68, 0x0B68, 0x0000, 0xC89A}, /* CELL 6 COEFFS */
1008 }, {
1009 {0x1655, 0xD64E, 0x1658, 0xCBEF, 0x70FE}, /*25M*/
1010 {0x225E, 0xC000, 0x2256, 0xC589, 0x7489},
1011 {0x2293, 0xC000, 0x2295, 0xC209, 0x767E},
1012 {0x2377, 0xC000, 0x23AA, 0xC0AB, 0x7746},
1013 {0x0DC7, 0xEBC8, 0x0D07, 0xC027, 0x7799},
1014 {0x0000, 0x0888, 0x0E9C, 0x0000, 0x3757}
1015
1016 }, {
1017 {0x0000, 0x0000, 0x0000, 0x0000, 0x0000}, /* 30M */
1018 {0x0000, 0x0000, 0x0000, 0x0000, 0x0000},
1019 {0x0000, 0x0000, 0x0000, 0x0000, 0x0000},
1020 {0x0000, 0x0000, 0x0000, 0x0000, 0x0000},
1021 {0x0000, 0x0000, 0x0000, 0x0000, 0x0000},
1022 {0x0000, 0x0000, 0x0000, 0x0000, 0x0000}
1023 }
1024};
1025
1026static u32 stv0367ter_get_mclk(struct stv0367_state *state, u32 ExtClk_Hz)
1027{
1028 u32 mclk_Hz = 0; /* master clock frequency (Hz) */
1029 u32 m, n, p;
1030
1031 dprintk("%s:\n", __func__);
1032
1033 if (stv0367_readbits(state, F367TER_BYPASS_PLLXN) == 0) {
1034 n = (u32)stv0367_readbits(state, F367TER_PLL_NDIV);
1035 if (n == 0)
1036 n = n + 1;
1037
1038 m = (u32)stv0367_readbits(state, F367TER_PLL_MDIV);
1039 if (m == 0)
1040 m = m + 1;
1041
1042 p = (u32)stv0367_readbits(state, F367TER_PLL_PDIV);
1043 if (p > 5)
1044 p = 5;
1045
1046 mclk_Hz = ((ExtClk_Hz / 2) * n) / (m * (1 << p));
1047
1048 dprintk("N=%d M=%d P=%d mclk_Hz=%d ExtClk_Hz=%d\n",
1049 n, m, p, mclk_Hz, ExtClk_Hz);
1050 } else
1051 mclk_Hz = ExtClk_Hz;
1052
1053 dprintk("%s: mclk_Hz=%d\n", __func__, mclk_Hz);
1054
1055 return mclk_Hz;
1056}
1057
1058static int stv0367ter_filt_coeff_init(struct stv0367_state *state,
Dan Carpenterbc4b18c2011-03-06 10:41:23 -03001059 u16 CellsCoeffs[3][6][5], u32 DemodXtal)
Igor M. Liplianin17cce932011-01-25 17:02:00 -03001060{
1061 int i, j, k, freq;
1062
1063 dprintk("%s:\n", __func__);
1064
1065 freq = stv0367ter_get_mclk(state, DemodXtal);
1066
1067 if (freq == 53125000)
1068 k = 1; /* equivalent to Xtal 25M on 362*/
1069 else if (freq == 54000000)
1070 k = 0; /* equivalent to Xtal 27M on 362*/
1071 else if (freq == 52500000)
1072 k = 2; /* equivalent to Xtal 30M on 362*/
1073 else
1074 return 0;
1075
1076 for (i = 1; i <= 6; i++) {
1077 stv0367_writebits(state, F367TER_IIR_CELL_NB, i - 1);
1078
1079 for (j = 1; j <= 5; j++) {
1080 stv0367_writereg(state,
1081 (R367TER_IIRCX_COEFF1_MSB + 2 * (j - 1)),
1082 MSB(CellsCoeffs[k][i-1][j-1]));
1083 stv0367_writereg(state,
1084 (R367TER_IIRCX_COEFF1_LSB + 2 * (j - 1)),
1085 LSB(CellsCoeffs[k][i-1][j-1]));
1086 }
1087 }
1088
1089 return 1;
1090
1091}
1092
1093static void stv0367ter_agc_iir_lock_detect_set(struct stv0367_state *state)
1094{
1095 dprintk("%s:\n", __func__);
1096
1097 stv0367_writebits(state, F367TER_LOCK_DETECT_LSB, 0x00);
1098
1099 /* Lock detect 1 */
1100 stv0367_writebits(state, F367TER_LOCK_DETECT_CHOICE, 0x00);
1101 stv0367_writebits(state, F367TER_LOCK_DETECT_MSB, 0x06);
1102 stv0367_writebits(state, F367TER_AUT_AGC_TARGET_LSB, 0x04);
1103
1104 /* Lock detect 2 */
1105 stv0367_writebits(state, F367TER_LOCK_DETECT_CHOICE, 0x01);
1106 stv0367_writebits(state, F367TER_LOCK_DETECT_MSB, 0x06);
1107 stv0367_writebits(state, F367TER_AUT_AGC_TARGET_LSB, 0x04);
1108
1109 /* Lock detect 3 */
1110 stv0367_writebits(state, F367TER_LOCK_DETECT_CHOICE, 0x02);
1111 stv0367_writebits(state, F367TER_LOCK_DETECT_MSB, 0x01);
1112 stv0367_writebits(state, F367TER_AUT_AGC_TARGET_LSB, 0x00);
1113
1114 /* Lock detect 4 */
1115 stv0367_writebits(state, F367TER_LOCK_DETECT_CHOICE, 0x03);
1116 stv0367_writebits(state, F367TER_LOCK_DETECT_MSB, 0x01);
1117 stv0367_writebits(state, F367TER_AUT_AGC_TARGET_LSB, 0x00);
1118
1119}
1120
1121static int stv0367_iir_filt_init(struct stv0367_state *state, u8 Bandwidth,
1122 u32 DemodXtalValue)
1123{
1124 dprintk("%s:\n", __func__);
1125
1126 stv0367_writebits(state, F367TER_NRST_IIR, 0);
1127
1128 switch (Bandwidth) {
1129 case 6:
1130 if (!stv0367ter_filt_coeff_init(state,
1131 CellsCoeffs_6MHz_367cofdm,
1132 DemodXtalValue))
1133 return 0;
1134 break;
1135 case 7:
1136 if (!stv0367ter_filt_coeff_init(state,
1137 CellsCoeffs_7MHz_367cofdm,
1138 DemodXtalValue))
1139 return 0;
1140 break;
1141 case 8:
1142 if (!stv0367ter_filt_coeff_init(state,
1143 CellsCoeffs_8MHz_367cofdm,
1144 DemodXtalValue))
1145 return 0;
1146 break;
1147 default:
1148 return 0;
1149 }
1150
1151 stv0367_writebits(state, F367TER_NRST_IIR, 1);
1152
1153 return 1;
1154}
1155
1156static void stv0367ter_agc_iir_rst(struct stv0367_state *state)
1157{
1158
1159 u8 com_n;
1160
1161 dprintk("%s:\n", __func__);
1162
1163 com_n = stv0367_readbits(state, F367TER_COM_N);
1164
1165 stv0367_writebits(state, F367TER_COM_N, 0x07);
1166
1167 stv0367_writebits(state, F367TER_COM_SOFT_RSTN, 0x00);
1168 stv0367_writebits(state, F367TER_COM_AGC_ON, 0x00);
1169
1170 stv0367_writebits(state, F367TER_COM_SOFT_RSTN, 0x01);
1171 stv0367_writebits(state, F367TER_COM_AGC_ON, 0x01);
1172
1173 stv0367_writebits(state, F367TER_COM_N, com_n);
1174
1175}
1176
1177static int stv0367ter_duration(s32 mode, int tempo1, int tempo2, int tempo3)
1178{
1179 int local_tempo = 0;
1180 switch (mode) {
1181 case 0:
1182 local_tempo = tempo1;
1183 break;
1184 case 1:
1185 local_tempo = tempo2;
1186 break ;
1187
1188 case 2:
1189 local_tempo = tempo3;
1190 break;
1191
1192 default:
1193 break;
1194 }
1195 /* msleep(local_tempo); */
1196 return local_tempo;
1197}
1198
1199static enum
1200stv0367_ter_signal_type stv0367ter_check_syr(struct stv0367_state *state)
1201{
1202 int wd = 100;
1203 unsigned short int SYR_var;
1204 s32 SYRStatus;
1205
1206 dprintk("%s:\n", __func__);
1207
1208 SYR_var = stv0367_readbits(state, F367TER_SYR_LOCK);
1209
1210 while ((!SYR_var) && (wd > 0)) {
1211 usleep_range(2000, 3000);
1212 wd -= 2;
1213 SYR_var = stv0367_readbits(state, F367TER_SYR_LOCK);
1214 }
1215
1216 if (!SYR_var)
1217 SYRStatus = FE_TER_NOSYMBOL;
1218 else
1219 SYRStatus = FE_TER_SYMBOLOK;
1220
1221 dprintk("stv0367ter_check_syr SYRStatus %s\n",
1222 SYR_var == 0 ? "No Symbol" : "OK");
1223
1224 return SYRStatus;
1225}
1226
1227static enum
1228stv0367_ter_signal_type stv0367ter_check_cpamp(struct stv0367_state *state,
1229 s32 FFTmode)
1230{
1231
1232 s32 CPAMPvalue = 0, CPAMPStatus, CPAMPMin;
1233 int wd = 0;
1234
1235 dprintk("%s:\n", __func__);
1236
1237 switch (FFTmode) {
1238 case 0: /*2k mode*/
1239 CPAMPMin = 20;
1240 wd = 10;
1241 break;
1242 case 1: /*8k mode*/
1243 CPAMPMin = 80;
1244 wd = 55;
1245 break;
1246 case 2: /*4k mode*/
1247 CPAMPMin = 40;
1248 wd = 30;
1249 break;
1250 default:
1251 CPAMPMin = 0xffff; /*drives to NOCPAMP */
1252 break;
1253 }
1254
1255 dprintk("%s: CPAMPMin=%d wd=%d\n", __func__, CPAMPMin, wd);
1256
1257 CPAMPvalue = stv0367_readbits(state, F367TER_PPM_CPAMP_DIRECT);
1258 while ((CPAMPvalue < CPAMPMin) && (wd > 0)) {
1259 usleep_range(1000, 2000);
1260 wd -= 1;
1261 CPAMPvalue = stv0367_readbits(state, F367TER_PPM_CPAMP_DIRECT);
1262 /*dprintk("CPAMPvalue= %d at wd=%d\n",CPAMPvalue,wd); */
1263 }
1264 dprintk("******last CPAMPvalue= %d at wd=%d\n", CPAMPvalue, wd);
1265 if (CPAMPvalue < CPAMPMin) {
1266 CPAMPStatus = FE_TER_NOCPAMP;
1267 printk(KERN_ERR "CPAMP failed\n");
1268 } else {
1269 printk(KERN_ERR "CPAMP OK !\n");
1270 CPAMPStatus = FE_TER_CPAMPOK;
1271 }
1272
1273 return CPAMPStatus;
1274}
1275
Mauro Carvalho Chehab8c8ca1c2012-10-27 11:26:42 -03001276static enum stv0367_ter_signal_type
1277stv0367ter_lock_algo(struct stv0367_state *state)
Igor M. Liplianin17cce932011-01-25 17:02:00 -03001278{
1279 enum stv0367_ter_signal_type ret_flag;
1280 short int wd, tempo;
1281 u8 try, u_var1 = 0, u_var2 = 0, u_var3 = 0, u_var4 = 0, mode, guard;
1282 u8 tmp, tmp2;
1283
1284 dprintk("%s:\n", __func__);
1285
1286 if (state == NULL)
1287 return FE_TER_SWNOK;
1288
1289 try = 0;
1290 do {
1291 ret_flag = FE_TER_LOCKOK;
1292
1293 stv0367_writebits(state, F367TER_CORE_ACTIVE, 0);
1294
1295 if (state->config->if_iq_mode != 0)
1296 stv0367_writebits(state, F367TER_COM_N, 0x07);
1297
1298 stv0367_writebits(state, F367TER_GUARD, 3);/* suggest 2k 1/4 */
1299 stv0367_writebits(state, F367TER_MODE, 0);
1300 stv0367_writebits(state, F367TER_SYR_TR_DIS, 0);
1301 usleep_range(5000, 10000);
1302
1303 stv0367_writebits(state, F367TER_CORE_ACTIVE, 1);
1304
1305
1306 if (stv0367ter_check_syr(state) == FE_TER_NOSYMBOL)
1307 return FE_TER_NOSYMBOL;
1308 else { /*
1309 if chip locked on wrong mode first try,
1310 it must lock correctly second try */
1311 mode = stv0367_readbits(state, F367TER_SYR_MODE);
1312 if (stv0367ter_check_cpamp(state, mode) ==
1313 FE_TER_NOCPAMP) {
1314 if (try == 0)
1315 ret_flag = FE_TER_NOCPAMP;
1316
1317 }
1318 }
1319
1320 try++;
1321 } while ((try < 10) && (ret_flag != FE_TER_LOCKOK));
1322
1323 tmp = stv0367_readreg(state, R367TER_SYR_STAT);
1324 tmp2 = stv0367_readreg(state, R367TER_STATUS);
Hans Verkuilfb661a72011-03-06 09:26:24 -03001325 dprintk("state=%p\n", state);
Igor M. Liplianin17cce932011-01-25 17:02:00 -03001326 dprintk("LOCK OK! mode=%d SYR_STAT=0x%x R367TER_STATUS=0x%x\n",
1327 mode, tmp, tmp2);
1328
1329 tmp = stv0367_readreg(state, R367TER_PRVIT);
1330 tmp2 = stv0367_readreg(state, R367TER_I2CRPT);
1331 dprintk("PRVIT=0x%x I2CRPT=0x%x\n", tmp, tmp2);
1332
1333 tmp = stv0367_readreg(state, R367TER_GAIN_SRC1);
1334 dprintk("GAIN_SRC1=0x%x\n", tmp);
1335
1336 if ((mode != 0) && (mode != 1) && (mode != 2))
1337 return FE_TER_SWNOK;
1338
1339 /*guard=stv0367_readbits(state,F367TER_SYR_GUARD); */
1340
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001341 /*suppress EPQ auto for SYR_GARD 1/16 or 1/32
Igor M. Liplianin17cce932011-01-25 17:02:00 -03001342 and set channel predictor in automatic */
1343#if 0
1344 switch (guard) {
1345
1346 case 0:
1347 case 1:
1348 stv0367_writebits(state, F367TER_AUTO_LE_EN, 0);
1349 stv0367_writereg(state, R367TER_CHC_CTL, 0x01);
1350 break;
1351 case 2:
1352 case 3:
1353 stv0367_writebits(state, F367TER_AUTO_LE_EN, 1);
1354 stv0367_writereg(state, R367TER_CHC_CTL, 0x11);
1355 break;
1356
1357 default:
1358 return FE_TER_SWNOK;
1359 }
1360#endif
1361
1362 /*reset fec an reedsolo FOR 367 only*/
1363 stv0367_writebits(state, F367TER_RST_SFEC, 1);
1364 stv0367_writebits(state, F367TER_RST_REEDSOLO, 1);
1365 usleep_range(1000, 2000);
1366 stv0367_writebits(state, F367TER_RST_SFEC, 0);
1367 stv0367_writebits(state, F367TER_RST_REEDSOLO, 0);
1368
1369 u_var1 = stv0367_readbits(state, F367TER_LK);
1370 u_var2 = stv0367_readbits(state, F367TER_PRF);
1371 u_var3 = stv0367_readbits(state, F367TER_TPS_LOCK);
1372 /* u_var4=stv0367_readbits(state,F367TER_TSFIFO_LINEOK); */
1373
1374 wd = stv0367ter_duration(mode, 125, 500, 250);
1375 tempo = stv0367ter_duration(mode, 4, 16, 8);
1376
1377 /*while ( ((!u_var1)||(!u_var2)||(!u_var3)||(!u_var4)) && (wd>=0)) */
1378 while (((!u_var1) || (!u_var2) || (!u_var3)) && (wd >= 0)) {
1379 usleep_range(1000 * tempo, 1000 * (tempo + 1));
1380 wd -= tempo;
1381 u_var1 = stv0367_readbits(state, F367TER_LK);
1382 u_var2 = stv0367_readbits(state, F367TER_PRF);
1383 u_var3 = stv0367_readbits(state, F367TER_TPS_LOCK);
1384 /*u_var4=stv0367_readbits(state, F367TER_TSFIFO_LINEOK); */
1385 }
1386
1387 if (!u_var1)
1388 return FE_TER_NOLOCK;
1389
1390
1391 if (!u_var2)
1392 return FE_TER_NOPRFOUND;
1393
1394 if (!u_var3)
1395 return FE_TER_NOTPS;
1396
1397 guard = stv0367_readbits(state, F367TER_SYR_GUARD);
1398 stv0367_writereg(state, R367TER_CHC_CTL, 0x11);
1399 switch (guard) {
1400 case 0:
1401 case 1:
1402 stv0367_writebits(state, F367TER_AUTO_LE_EN, 0);
1403 /*stv0367_writereg(state,R367TER_CHC_CTL, 0x1);*/
1404 stv0367_writebits(state, F367TER_SYR_FILTER, 0);
1405 break;
1406 case 2:
1407 case 3:
1408 stv0367_writebits(state, F367TER_AUTO_LE_EN, 1);
1409 /*stv0367_writereg(state,R367TER_CHC_CTL, 0x11);*/
1410 stv0367_writebits(state, F367TER_SYR_FILTER, 1);
1411 break;
1412
1413 default:
1414 return FE_TER_SWNOK;
1415 }
1416
1417 /* apply Sfec workaround if 8K 64QAM CR!=1/2*/
1418 if ((stv0367_readbits(state, F367TER_TPS_CONST) == 2) &&
1419 (mode == 1) &&
1420 (stv0367_readbits(state, F367TER_TPS_HPCODE) != 0)) {
1421 stv0367_writereg(state, R367TER_SFDLYSETH, 0xc0);
1422 stv0367_writereg(state, R367TER_SFDLYSETM, 0x60);
1423 stv0367_writereg(state, R367TER_SFDLYSETL, 0x0);
1424 } else
1425 stv0367_writereg(state, R367TER_SFDLYSETH, 0x0);
1426
1427 wd = stv0367ter_duration(mode, 125, 500, 250);
1428 u_var4 = stv0367_readbits(state, F367TER_TSFIFO_LINEOK);
1429
1430 while ((!u_var4) && (wd >= 0)) {
1431 usleep_range(1000 * tempo, 1000 * (tempo + 1));
1432 wd -= tempo;
1433 u_var4 = stv0367_readbits(state, F367TER_TSFIFO_LINEOK);
1434 }
1435
1436 if (!u_var4)
1437 return FE_TER_NOLOCK;
1438
1439 /* for 367 leave COM_N at 0x7 for IQ_mode*/
1440 /*if(ter_state->if_iq_mode!=FE_TER_NORMAL_IF_TUNER) {
1441 tempo=0;
1442 while ((stv0367_readbits(state,F367TER_COM_USEGAINTRK)!=1) &&
1443 (stv0367_readbits(state,F367TER_COM_AGCLOCK)!=1)&&(tempo<100)) {
1444 ChipWaitOrAbort(state,1);
1445 tempo+=1;
1446 }
1447
1448 stv0367_writebits(state,F367TER_COM_N,0x17);
1449 } */
1450
1451 stv0367_writebits(state, F367TER_SYR_TR_DIS, 1);
1452
1453 dprintk("FE_TER_LOCKOK !!!\n");
1454
1455 return FE_TER_LOCKOK;
1456
1457}
1458
1459static void stv0367ter_set_ts_mode(struct stv0367_state *state,
1460 enum stv0367_ts_mode PathTS)
1461{
1462
1463 dprintk("%s:\n", __func__);
1464
1465 if (state == NULL)
1466 return;
1467
1468 stv0367_writebits(state, F367TER_TS_DIS, 0);
1469 switch (PathTS) {
1470 default:
1471 /*for removing warning :default we can assume in parallel mode*/
1472 case STV0367_PARALLEL_PUNCT_CLOCK:
1473 stv0367_writebits(state, F367TER_TSFIFO_SERIAL, 0);
1474 stv0367_writebits(state, F367TER_TSFIFO_DVBCI, 0);
1475 break;
1476 case STV0367_SERIAL_PUNCT_CLOCK:
1477 stv0367_writebits(state, F367TER_TSFIFO_SERIAL, 1);
1478 stv0367_writebits(state, F367TER_TSFIFO_DVBCI, 1);
1479 break;
1480 }
1481}
1482
1483static void stv0367ter_set_clk_pol(struct stv0367_state *state,
1484 enum stv0367_clk_pol clock)
1485{
1486
1487 dprintk("%s:\n", __func__);
1488
1489 if (state == NULL)
1490 return;
1491
1492 switch (clock) {
1493 case STV0367_RISINGEDGE_CLOCK:
1494 stv0367_writebits(state, F367TER_TS_BYTE_CLK_INV, 1);
1495 break;
1496 case STV0367_FALLINGEDGE_CLOCK:
1497 stv0367_writebits(state, F367TER_TS_BYTE_CLK_INV, 0);
1498 break;
1499 /*case FE_TER_CLOCK_POLARITY_DEFAULT:*/
1500 default:
1501 stv0367_writebits(state, F367TER_TS_BYTE_CLK_INV, 0);
1502 break;
1503 }
1504}
1505
1506#if 0
1507static void stv0367ter_core_sw(struct stv0367_state *state)
1508{
1509
1510 dprintk("%s:\n", __func__);
1511
1512 stv0367_writebits(state, F367TER_CORE_ACTIVE, 0);
1513 stv0367_writebits(state, F367TER_CORE_ACTIVE, 1);
1514 msleep(350);
1515}
1516#endif
1517static int stv0367ter_standby(struct dvb_frontend *fe, u8 standby_on)
1518{
1519 struct stv0367_state *state = fe->demodulator_priv;
1520
1521 dprintk("%s:\n", __func__);
1522
1523 if (standby_on) {
1524 stv0367_writebits(state, F367TER_STDBY, 1);
1525 stv0367_writebits(state, F367TER_STDBY_FEC, 1);
1526 stv0367_writebits(state, F367TER_STDBY_CORE, 1);
1527 } else {
1528 stv0367_writebits(state, F367TER_STDBY, 0);
1529 stv0367_writebits(state, F367TER_STDBY_FEC, 0);
1530 stv0367_writebits(state, F367TER_STDBY_CORE, 0);
1531 }
1532
1533 return 0;
1534}
1535
1536static int stv0367ter_sleep(struct dvb_frontend *fe)
1537{
1538 return stv0367ter_standby(fe, 1);
1539}
1540
Mauro Carvalho Chehab8c8ca1c2012-10-27 11:26:42 -03001541static int stv0367ter_init(struct dvb_frontend *fe)
Igor M. Liplianin17cce932011-01-25 17:02:00 -03001542{
1543 struct stv0367_state *state = fe->demodulator_priv;
1544 struct stv0367ter_state *ter_state = state->ter_state;
1545 int i;
1546
1547 dprintk("%s:\n", __func__);
1548
1549 ter_state->pBER = 0;
1550
1551 for (i = 0; i < STV0367TER_NBREGS; i++)
1552 stv0367_writereg(state, def0367ter[i].addr,
1553 def0367ter[i].value);
1554
1555 switch (state->config->xtal) {
1556 /*set internal freq to 53.125MHz */
1557 case 25000000:
1558 stv0367_writereg(state, R367TER_PLLMDIV, 0xa);
1559 stv0367_writereg(state, R367TER_PLLNDIV, 0x55);
1560 stv0367_writereg(state, R367TER_PLLSETUP, 0x18);
1561 break;
1562 default:
1563 case 27000000:
1564 dprintk("FE_STV0367TER_SetCLKgen for 27Mhz\n");
1565 stv0367_writereg(state, R367TER_PLLMDIV, 0x1);
1566 stv0367_writereg(state, R367TER_PLLNDIV, 0x8);
1567 stv0367_writereg(state, R367TER_PLLSETUP, 0x18);
1568 break;
1569 case 30000000:
1570 stv0367_writereg(state, R367TER_PLLMDIV, 0xc);
1571 stv0367_writereg(state, R367TER_PLLNDIV, 0x55);
1572 stv0367_writereg(state, R367TER_PLLSETUP, 0x18);
1573 break;
1574 }
1575
1576 stv0367_writereg(state, R367TER_I2CRPT, 0xa0);
1577 stv0367_writereg(state, R367TER_ANACTRL, 0x00);
1578
1579 /*Set TS1 and TS2 to serial or parallel mode */
1580 stv0367ter_set_ts_mode(state, state->config->ts_mode);
1581 stv0367ter_set_clk_pol(state, state->config->clk_pol);
1582
1583 state->chip_id = stv0367_readreg(state, R367TER_ID);
1584 ter_state->first_lock = 0;
1585 ter_state->unlock_counter = 2;
1586
1587 return 0;
1588}
1589
Mauro Carvalho Chehab285d55a2011-12-26 13:03:00 -03001590static int stv0367ter_algo(struct dvb_frontend *fe)
Igor M. Liplianin17cce932011-01-25 17:02:00 -03001591{
Mauro Carvalho Chehab285d55a2011-12-26 13:03:00 -03001592 struct dtv_frontend_properties *p = &fe->dtv_property_cache;
Igor M. Liplianin17cce932011-01-25 17:02:00 -03001593 struct stv0367_state *state = fe->demodulator_priv;
1594 struct stv0367ter_state *ter_state = state->ter_state;
1595 int offset = 0, tempo = 0;
1596 u8 u_var;
Peter Senna Tschudindf1ec022012-06-14 13:58:14 -03001597 u8 /*constell,*/ counter;
Igor M. Liplianin17cce932011-01-25 17:02:00 -03001598 s8 step;
1599 s32 timing_offset = 0;
1600 u32 trl_nomrate = 0, InternalFreq = 0, temp = 0;
1601
1602 dprintk("%s:\n", __func__);
1603
Mauro Carvalho Chehab285d55a2011-12-26 13:03:00 -03001604 ter_state->frequency = p->frequency;
Igor M. Liplianin17cce932011-01-25 17:02:00 -03001605 ter_state->force = FE_TER_FORCENONE
1606 + stv0367_readbits(state, F367TER_FORCE) * 2;
1607 ter_state->if_iq_mode = state->config->if_iq_mode;
1608 switch (state->config->if_iq_mode) {
1609 case FE_TER_NORMAL_IF_TUNER: /* Normal IF mode */
1610 dprintk("ALGO: FE_TER_NORMAL_IF_TUNER selected\n");
1611 stv0367_writebits(state, F367TER_TUNER_BB, 0);
1612 stv0367_writebits(state, F367TER_LONGPATH_IF, 0);
1613 stv0367_writebits(state, F367TER_DEMUX_SWAP, 0);
1614 break;
1615 case FE_TER_LONGPATH_IF_TUNER: /* Long IF mode */
1616 dprintk("ALGO: FE_TER_LONGPATH_IF_TUNER selected\n");
1617 stv0367_writebits(state, F367TER_TUNER_BB, 0);
1618 stv0367_writebits(state, F367TER_LONGPATH_IF, 1);
1619 stv0367_writebits(state, F367TER_DEMUX_SWAP, 1);
1620 break;
1621 case FE_TER_IQ_TUNER: /* IQ mode */
1622 dprintk("ALGO: FE_TER_IQ_TUNER selected\n");
1623 stv0367_writebits(state, F367TER_TUNER_BB, 1);
1624 stv0367_writebits(state, F367TER_PPM_INVSEL, 0);
1625 break;
1626 default:
1627 printk(KERN_ERR "ALGO: wrong TUNER type selected\n");
1628 return -EINVAL;
1629 }
1630
1631 usleep_range(5000, 7000);
1632
Mauro Carvalho Chehab285d55a2011-12-26 13:03:00 -03001633 switch (p->inversion) {
Igor M. Liplianin17cce932011-01-25 17:02:00 -03001634 case INVERSION_AUTO:
1635 default:
1636 dprintk("%s: inversion AUTO\n", __func__);
1637 if (ter_state->if_iq_mode == FE_TER_IQ_TUNER)
1638 stv0367_writebits(state, F367TER_IQ_INVERT,
1639 ter_state->sense);
1640 else
1641 stv0367_writebits(state, F367TER_INV_SPECTR,
1642 ter_state->sense);
1643
1644 break;
1645 case INVERSION_ON:
1646 case INVERSION_OFF:
1647 if (ter_state->if_iq_mode == FE_TER_IQ_TUNER)
1648 stv0367_writebits(state, F367TER_IQ_INVERT,
Mauro Carvalho Chehab285d55a2011-12-26 13:03:00 -03001649 p->inversion);
Igor M. Liplianin17cce932011-01-25 17:02:00 -03001650 else
1651 stv0367_writebits(state, F367TER_INV_SPECTR,
Mauro Carvalho Chehab285d55a2011-12-26 13:03:00 -03001652 p->inversion);
Igor M. Liplianin17cce932011-01-25 17:02:00 -03001653
1654 break;
1655 }
1656
1657 if ((ter_state->if_iq_mode != FE_TER_NORMAL_IF_TUNER) &&
1658 (ter_state->pBW != ter_state->bw)) {
1659 stv0367ter_agc_iir_lock_detect_set(state);
1660
1661 /*set fine agc target to 180 for LPIF or IQ mode*/
1662 /* set Q_AGCTarget */
1663 stv0367_writebits(state, F367TER_SEL_IQNTAR, 1);
1664 stv0367_writebits(state, F367TER_AUT_AGC_TARGET_MSB, 0xB);
1665 /*stv0367_writebits(state,AUT_AGC_TARGET_LSB,0x04); */
1666
1667 /* set Q_AGCTarget */
1668 stv0367_writebits(state, F367TER_SEL_IQNTAR, 0);
1669 stv0367_writebits(state, F367TER_AUT_AGC_TARGET_MSB, 0xB);
1670 /*stv0367_writebits(state,AUT_AGC_TARGET_LSB,0x04); */
1671
1672 if (!stv0367_iir_filt_init(state, ter_state->bw,
1673 state->config->xtal))
1674 return -EINVAL;
1675 /*set IIR filter once for 6,7 or 8MHz BW*/
1676 ter_state->pBW = ter_state->bw;
1677
1678 stv0367ter_agc_iir_rst(state);
1679 }
1680
1681 if (ter_state->hierarchy == FE_TER_HIER_LOW_PRIO)
1682 stv0367_writebits(state, F367TER_BDI_LPSEL, 0x01);
1683 else
1684 stv0367_writebits(state, F367TER_BDI_LPSEL, 0x00);
1685
1686 InternalFreq = stv0367ter_get_mclk(state, state->config->xtal) / 1000;
1687 temp = (int)
1688 ((((ter_state->bw * 64 * (1 << 15) * 100)
1689 / (InternalFreq)) * 10) / 7);
1690
1691 stv0367_writebits(state, F367TER_TRL_NOMRATE_LSB, temp % 2);
1692 temp = temp / 2;
1693 stv0367_writebits(state, F367TER_TRL_NOMRATE_HI, temp / 256);
1694 stv0367_writebits(state, F367TER_TRL_NOMRATE_LO, temp % 256);
1695
1696 temp = stv0367_readbits(state, F367TER_TRL_NOMRATE_HI) * 512 +
1697 stv0367_readbits(state, F367TER_TRL_NOMRATE_LO) * 2 +
1698 stv0367_readbits(state, F367TER_TRL_NOMRATE_LSB);
1699 temp = (int)(((1 << 17) * ter_state->bw * 1000) / (7 * (InternalFreq)));
1700 stv0367_writebits(state, F367TER_GAIN_SRC_HI, temp / 256);
1701 stv0367_writebits(state, F367TER_GAIN_SRC_LO, temp % 256);
1702 temp = stv0367_readbits(state, F367TER_GAIN_SRC_HI) * 256 +
1703 stv0367_readbits(state, F367TER_GAIN_SRC_LO);
1704
1705 temp = (int)
1706 ((InternalFreq - state->config->if_khz) * (1 << 16)
1707 / (InternalFreq));
1708
1709 dprintk("DEROT temp=0x%x\n", temp);
1710 stv0367_writebits(state, F367TER_INC_DEROT_HI, temp / 256);
1711 stv0367_writebits(state, F367TER_INC_DEROT_LO, temp % 256);
1712
1713 ter_state->echo_pos = 0;
1714 ter_state->ucblocks = 0; /* liplianin */
1715 ter_state->pBER = 0; /* liplianin */
1716 stv0367_writebits(state, F367TER_LONG_ECHO, ter_state->echo_pos);
1717
1718 if (stv0367ter_lock_algo(state) != FE_TER_LOCKOK)
1719 return 0;
1720
1721 ter_state->state = FE_TER_LOCKOK;
Igor M. Liplianin17cce932011-01-25 17:02:00 -03001722
1723 ter_state->mode = stv0367_readbits(state, F367TER_SYR_MODE);
1724 ter_state->guard = stv0367_readbits(state, F367TER_SYR_GUARD);
1725
1726 ter_state->first_lock = 1; /* we know sense now :) */
1727
1728 ter_state->agc_val =
1729 (stv0367_readbits(state, F367TER_AGC1_VAL_LO) << 16) +
1730 (stv0367_readbits(state, F367TER_AGC1_VAL_HI) << 24) +
1731 stv0367_readbits(state, F367TER_AGC2_VAL_LO) +
1732 (stv0367_readbits(state, F367TER_AGC2_VAL_HI) << 8);
1733
1734 /* Carrier offset calculation */
1735 stv0367_writebits(state, F367TER_FREEZE, 1);
1736 offset = (stv0367_readbits(state, F367TER_CRL_FOFFSET_VHI) << 16) ;
1737 offset += (stv0367_readbits(state, F367TER_CRL_FOFFSET_HI) << 8);
1738 offset += (stv0367_readbits(state, F367TER_CRL_FOFFSET_LO));
1739 stv0367_writebits(state, F367TER_FREEZE, 0);
1740 if (offset > 8388607)
1741 offset -= 16777216;
1742
1743 offset = offset * 2 / 16384;
1744
1745 if (ter_state->mode == FE_TER_MODE_2K)
1746 offset = (offset * 4464) / 1000;/*** 1 FFT BIN=4.464khz***/
1747 else if (ter_state->mode == FE_TER_MODE_4K)
1748 offset = (offset * 223) / 100;/*** 1 FFT BIN=2.23khz***/
1749 else if (ter_state->mode == FE_TER_MODE_8K)
1750 offset = (offset * 111) / 100;/*** 1 FFT BIN=1.1khz***/
1751
1752 if (stv0367_readbits(state, F367TER_PPM_INVSEL) == 1) {
1753 if ((stv0367_readbits(state, F367TER_INV_SPECTR) ==
1754 (stv0367_readbits(state,
1755 F367TER_STATUS_INV_SPECRUM) == 1)))
1756 offset = offset * -1;
1757 }
1758
1759 if (ter_state->bw == 6)
1760 offset = (offset * 6) / 8;
1761 else if (ter_state->bw == 7)
1762 offset = (offset * 7) / 8;
1763
1764 ter_state->frequency += offset;
1765
1766 tempo = 10; /* exit even if timing_offset stays null */
1767 while ((timing_offset == 0) && (tempo > 0)) {
1768 usleep_range(10000, 20000); /*was 20ms */
1769 /* fine tuning of timing offset if required */
1770 timing_offset = stv0367_readbits(state, F367TER_TRL_TOFFSET_LO)
1771 + 256 * stv0367_readbits(state,
1772 F367TER_TRL_TOFFSET_HI);
1773 if (timing_offset >= 32768)
1774 timing_offset -= 65536;
1775 trl_nomrate = (512 * stv0367_readbits(state,
1776 F367TER_TRL_NOMRATE_HI)
1777 + stv0367_readbits(state, F367TER_TRL_NOMRATE_LO) * 2
1778 + stv0367_readbits(state, F367TER_TRL_NOMRATE_LSB));
1779
1780 timing_offset = ((signed)(1000000 / trl_nomrate) *
1781 timing_offset) / 2048;
1782 tempo--;
1783 }
1784
1785 if (timing_offset <= 0) {
1786 timing_offset = (timing_offset - 11) / 22;
1787 step = -1;
1788 } else {
1789 timing_offset = (timing_offset + 11) / 22;
1790 step = 1;
1791 }
1792
1793 for (counter = 0; counter < abs(timing_offset); counter++) {
1794 trl_nomrate += step;
1795 stv0367_writebits(state, F367TER_TRL_NOMRATE_LSB,
1796 trl_nomrate % 2);
1797 stv0367_writebits(state, F367TER_TRL_NOMRATE_LO,
1798 trl_nomrate / 2);
1799 usleep_range(1000, 2000);
1800 }
1801
1802 usleep_range(5000, 6000);
1803 /* unlocks could happen in case of trl centring big step,
1804 then a core off/on restarts demod */
1805 u_var = stv0367_readbits(state, F367TER_LK);
1806
1807 if (!u_var) {
1808 stv0367_writebits(state, F367TER_CORE_ACTIVE, 0);
1809 msleep(20);
1810 stv0367_writebits(state, F367TER_CORE_ACTIVE, 1);
1811 }
1812
1813 return 0;
1814}
1815
Mauro Carvalho Chehab285d55a2011-12-26 13:03:00 -03001816static int stv0367ter_set_frontend(struct dvb_frontend *fe)
Igor M. Liplianin17cce932011-01-25 17:02:00 -03001817{
Mauro Carvalho Chehab285d55a2011-12-26 13:03:00 -03001818 struct dtv_frontend_properties *p = &fe->dtv_property_cache;
Igor M. Liplianin17cce932011-01-25 17:02:00 -03001819 struct stv0367_state *state = fe->demodulator_priv;
1820 struct stv0367ter_state *ter_state = state->ter_state;
1821
1822 /*u8 trials[2]; */
1823 s8 num_trials, index;
1824 u8 SenseTrials[] = { INVERSION_ON, INVERSION_OFF };
1825
1826 stv0367ter_init(fe);
1827
1828 if (fe->ops.tuner_ops.set_params) {
1829 if (fe->ops.i2c_gate_ctrl)
1830 fe->ops.i2c_gate_ctrl(fe, 1);
Mauro Carvalho Chehab14d24d12011-12-24 12:24:33 -03001831 fe->ops.tuner_ops.set_params(fe);
Igor M. Liplianin17cce932011-01-25 17:02:00 -03001832 if (fe->ops.i2c_gate_ctrl)
1833 fe->ops.i2c_gate_ctrl(fe, 0);
1834 }
1835
Mauro Carvalho Chehab285d55a2011-12-26 13:03:00 -03001836 switch (p->transmission_mode) {
Igor M. Liplianin17cce932011-01-25 17:02:00 -03001837 default:
1838 case TRANSMISSION_MODE_AUTO:
1839 case TRANSMISSION_MODE_2K:
1840 ter_state->mode = FE_TER_MODE_2K;
1841 break;
1842/* case TRANSMISSION_MODE_4K:
1843 pLook.mode = FE_TER_MODE_4K;
1844 break;*/
1845 case TRANSMISSION_MODE_8K:
1846 ter_state->mode = FE_TER_MODE_8K;
1847 break;
1848 }
1849
Mauro Carvalho Chehab285d55a2011-12-26 13:03:00 -03001850 switch (p->guard_interval) {
Igor M. Liplianin17cce932011-01-25 17:02:00 -03001851 default:
1852 case GUARD_INTERVAL_1_32:
1853 case GUARD_INTERVAL_1_16:
1854 case GUARD_INTERVAL_1_8:
1855 case GUARD_INTERVAL_1_4:
Mauro Carvalho Chehab285d55a2011-12-26 13:03:00 -03001856 ter_state->guard = p->guard_interval;
Igor M. Liplianin17cce932011-01-25 17:02:00 -03001857 break;
1858 case GUARD_INTERVAL_AUTO:
1859 ter_state->guard = GUARD_INTERVAL_1_32;
1860 break;
1861 }
1862
Mauro Carvalho Chehab285d55a2011-12-26 13:03:00 -03001863 switch (p->bandwidth_hz) {
1864 case 6000000:
Igor M. Liplianin17cce932011-01-25 17:02:00 -03001865 ter_state->bw = FE_TER_CHAN_BW_6M;
1866 break;
Mauro Carvalho Chehab285d55a2011-12-26 13:03:00 -03001867 case 7000000:
Igor M. Liplianin17cce932011-01-25 17:02:00 -03001868 ter_state->bw = FE_TER_CHAN_BW_7M;
1869 break;
Mauro Carvalho Chehab285d55a2011-12-26 13:03:00 -03001870 case 8000000:
Igor M. Liplianin17cce932011-01-25 17:02:00 -03001871 default:
1872 ter_state->bw = FE_TER_CHAN_BW_8M;
1873 }
1874
1875 ter_state->hierarchy = FE_TER_HIER_NONE;
1876
Mauro Carvalho Chehab285d55a2011-12-26 13:03:00 -03001877 switch (p->inversion) {
Igor M. Liplianin17cce932011-01-25 17:02:00 -03001878 case INVERSION_OFF:
1879 case INVERSION_ON:
1880 num_trials = 1;
1881 break;
1882 default:
1883 num_trials = 2;
1884 if (ter_state->first_lock)
1885 num_trials = 1;
1886 break;
1887 }
1888
1889 ter_state->state = FE_TER_NOLOCK;
1890 index = 0;
1891
1892 while (((index) < num_trials) && (ter_state->state != FE_TER_LOCKOK)) {
1893 if (!ter_state->first_lock) {
Mauro Carvalho Chehab285d55a2011-12-26 13:03:00 -03001894 if (p->inversion == INVERSION_AUTO)
Igor M. Liplianin17cce932011-01-25 17:02:00 -03001895 ter_state->sense = SenseTrials[index];
1896
1897 }
Mauro Carvalho Chehab285d55a2011-12-26 13:03:00 -03001898 stv0367ter_algo(fe);
Igor M. Liplianin17cce932011-01-25 17:02:00 -03001899
1900 if ((ter_state->state == FE_TER_LOCKOK) &&
Mauro Carvalho Chehab285d55a2011-12-26 13:03:00 -03001901 (p->inversion == INVERSION_AUTO) &&
Igor M. Liplianin17cce932011-01-25 17:02:00 -03001902 (index == 1)) {
1903 /* invert spectrum sense */
1904 SenseTrials[index] = SenseTrials[0];
1905 SenseTrials[(index + 1) % 2] = (SenseTrials[1] + 1) % 2;
1906 }
1907
1908 index++;
1909 }
1910
1911 return 0;
1912}
1913
1914static int stv0367ter_read_ucblocks(struct dvb_frontend *fe, u32 *ucblocks)
1915{
1916 struct stv0367_state *state = fe->demodulator_priv;
1917 struct stv0367ter_state *ter_state = state->ter_state;
1918 u32 errs = 0;
1919
1920 /*wait for counting completion*/
1921 if (stv0367_readbits(state, F367TER_SFERRC_OLDVALUE) == 0) {
1922 errs =
1923 ((u32)stv0367_readbits(state, F367TER_ERR_CNT1)
1924 * (1 << 16))
1925 + ((u32)stv0367_readbits(state, F367TER_ERR_CNT1_HI)
1926 * (1 << 8))
1927 + ((u32)stv0367_readbits(state, F367TER_ERR_CNT1_LO));
1928 ter_state->ucblocks = errs;
1929 }
1930
1931 (*ucblocks) = ter_state->ucblocks;
1932
1933 return 0;
1934}
1935
Mauro Carvalho Chehab7c61d802011-12-30 11:30:21 -03001936static int stv0367ter_get_frontend(struct dvb_frontend *fe)
Igor M. Liplianin17cce932011-01-25 17:02:00 -03001937{
Mauro Carvalho Chehab7c61d802011-12-30 11:30:21 -03001938 struct dtv_frontend_properties *p = &fe->dtv_property_cache;
Igor M. Liplianin17cce932011-01-25 17:02:00 -03001939 struct stv0367_state *state = fe->demodulator_priv;
1940 struct stv0367ter_state *ter_state = state->ter_state;
Igor M. Liplianin17cce932011-01-25 17:02:00 -03001941 enum stv0367_ter_mode mode;
1942 int constell = 0,/* snr = 0,*/ Data = 0;
1943
Mauro Carvalho Chehab285d55a2011-12-26 13:03:00 -03001944 p->frequency = stv0367_get_tuner_freq(fe);
1945 if ((int)p->frequency < 0)
1946 p->frequency = -p->frequency;
Igor M. Liplianin17cce932011-01-25 17:02:00 -03001947
1948 constell = stv0367_readbits(state, F367TER_TPS_CONST);
1949 if (constell == 0)
Mauro Carvalho Chehab285d55a2011-12-26 13:03:00 -03001950 p->modulation = QPSK;
Igor M. Liplianin17cce932011-01-25 17:02:00 -03001951 else if (constell == 1)
Mauro Carvalho Chehab285d55a2011-12-26 13:03:00 -03001952 p->modulation = QAM_16;
Igor M. Liplianin17cce932011-01-25 17:02:00 -03001953 else
Mauro Carvalho Chehab285d55a2011-12-26 13:03:00 -03001954 p->modulation = QAM_64;
Igor M. Liplianin17cce932011-01-25 17:02:00 -03001955
Mauro Carvalho Chehab285d55a2011-12-26 13:03:00 -03001956 p->inversion = stv0367_readbits(state, F367TER_INV_SPECTR);
Igor M. Liplianin17cce932011-01-25 17:02:00 -03001957
1958 /* Get the Hierarchical mode */
1959 Data = stv0367_readbits(state, F367TER_TPS_HIERMODE);
1960
1961 switch (Data) {
1962 case 0:
Mauro Carvalho Chehab285d55a2011-12-26 13:03:00 -03001963 p->hierarchy = HIERARCHY_NONE;
Igor M. Liplianin17cce932011-01-25 17:02:00 -03001964 break;
1965 case 1:
Mauro Carvalho Chehab285d55a2011-12-26 13:03:00 -03001966 p->hierarchy = HIERARCHY_1;
Igor M. Liplianin17cce932011-01-25 17:02:00 -03001967 break;
1968 case 2:
Mauro Carvalho Chehab285d55a2011-12-26 13:03:00 -03001969 p->hierarchy = HIERARCHY_2;
Igor M. Liplianin17cce932011-01-25 17:02:00 -03001970 break;
1971 case 3:
Mauro Carvalho Chehab285d55a2011-12-26 13:03:00 -03001972 p->hierarchy = HIERARCHY_4;
Igor M. Liplianin17cce932011-01-25 17:02:00 -03001973 break;
1974 default:
Mauro Carvalho Chehab285d55a2011-12-26 13:03:00 -03001975 p->hierarchy = HIERARCHY_AUTO;
Igor M. Liplianin17cce932011-01-25 17:02:00 -03001976 break; /* error */
1977 }
1978
1979 /* Get the FEC Rate */
1980 if (ter_state->hierarchy == FE_TER_HIER_LOW_PRIO)
1981 Data = stv0367_readbits(state, F367TER_TPS_LPCODE);
1982 else
1983 Data = stv0367_readbits(state, F367TER_TPS_HPCODE);
1984
1985 switch (Data) {
1986 case 0:
Mauro Carvalho Chehab285d55a2011-12-26 13:03:00 -03001987 p->code_rate_HP = FEC_1_2;
Igor M. Liplianin17cce932011-01-25 17:02:00 -03001988 break;
1989 case 1:
Mauro Carvalho Chehab285d55a2011-12-26 13:03:00 -03001990 p->code_rate_HP = FEC_2_3;
Igor M. Liplianin17cce932011-01-25 17:02:00 -03001991 break;
1992 case 2:
Mauro Carvalho Chehab285d55a2011-12-26 13:03:00 -03001993 p->code_rate_HP = FEC_3_4;
Igor M. Liplianin17cce932011-01-25 17:02:00 -03001994 break;
1995 case 3:
Mauro Carvalho Chehab285d55a2011-12-26 13:03:00 -03001996 p->code_rate_HP = FEC_5_6;
Igor M. Liplianin17cce932011-01-25 17:02:00 -03001997 break;
1998 case 4:
Mauro Carvalho Chehab285d55a2011-12-26 13:03:00 -03001999 p->code_rate_HP = FEC_7_8;
Igor M. Liplianin17cce932011-01-25 17:02:00 -03002000 break;
2001 default:
Mauro Carvalho Chehab285d55a2011-12-26 13:03:00 -03002002 p->code_rate_HP = FEC_AUTO;
Igor M. Liplianin17cce932011-01-25 17:02:00 -03002003 break; /* error */
2004 }
2005
2006 mode = stv0367_readbits(state, F367TER_SYR_MODE);
2007
2008 switch (mode) {
2009 case FE_TER_MODE_2K:
Mauro Carvalho Chehab285d55a2011-12-26 13:03:00 -03002010 p->transmission_mode = TRANSMISSION_MODE_2K;
Igor M. Liplianin17cce932011-01-25 17:02:00 -03002011 break;
2012/* case FE_TER_MODE_4K:
Mauro Carvalho Chehab285d55a2011-12-26 13:03:00 -03002013 p->transmission_mode = TRANSMISSION_MODE_4K;
Igor M. Liplianin17cce932011-01-25 17:02:00 -03002014 break;*/
2015 case FE_TER_MODE_8K:
Mauro Carvalho Chehab285d55a2011-12-26 13:03:00 -03002016 p->transmission_mode = TRANSMISSION_MODE_8K;
Igor M. Liplianin17cce932011-01-25 17:02:00 -03002017 break;
2018 default:
Mauro Carvalho Chehab285d55a2011-12-26 13:03:00 -03002019 p->transmission_mode = TRANSMISSION_MODE_AUTO;
Igor M. Liplianin17cce932011-01-25 17:02:00 -03002020 }
2021
Mauro Carvalho Chehab285d55a2011-12-26 13:03:00 -03002022 p->guard_interval = stv0367_readbits(state, F367TER_SYR_GUARD);
Igor M. Liplianin17cce932011-01-25 17:02:00 -03002023
Mauro Carvalho Chehab7c995072014-09-03 15:10:25 -03002024 return 0;
Igor M. Liplianin17cce932011-01-25 17:02:00 -03002025}
2026
2027static int stv0367ter_read_snr(struct dvb_frontend *fe, u16 *snr)
2028{
2029 struct stv0367_state *state = fe->demodulator_priv;
2030 u32 snru32 = 0;
2031 int cpt = 0;
2032 u8 cut = stv0367_readbits(state, F367TER_IDENTIFICATIONREG);
2033
2034 while (cpt < 10) {
2035 usleep_range(2000, 3000);
2036 if (cut == 0x50) /*cut 1.0 cut 1.1*/
2037 snru32 += stv0367_readbits(state, F367TER_CHCSNR) / 4;
2038 else /*cu2.0*/
2039 snru32 += 125 * stv0367_readbits(state, F367TER_CHCSNR);
2040
2041 cpt++;
2042 }
2043
2044 snru32 /= 10;/*average on 10 values*/
2045
2046 *snr = snru32 / 1000;
2047
2048 return 0;
2049}
2050
2051#if 0
2052static int stv0367ter_status(struct dvb_frontend *fe)
2053{
2054
2055 struct stv0367_state *state = fe->demodulator_priv;
2056 struct stv0367ter_state *ter_state = state->ter_state;
2057 int locked = FALSE;
2058
2059 locked = (stv0367_readbits(state, F367TER_LK));
2060 if (!locked)
2061 ter_state->unlock_counter += 1;
2062 else
2063 ter_state->unlock_counter = 0;
2064
2065 if (ter_state->unlock_counter > 2) {
2066 if (!stv0367_readbits(state, F367TER_TPS_LOCK) ||
2067 (!stv0367_readbits(state, F367TER_LK))) {
2068 stv0367_writebits(state, F367TER_CORE_ACTIVE, 0);
2069 usleep_range(2000, 3000);
2070 stv0367_writebits(state, F367TER_CORE_ACTIVE, 1);
2071 msleep(350);
2072 locked = (stv0367_readbits(state, F367TER_TPS_LOCK)) &&
2073 (stv0367_readbits(state, F367TER_LK));
2074 }
2075
2076 }
2077
2078 return locked;
2079}
2080#endif
Mauro Carvalho Chehab0df289a2015-06-07 14:53:52 -03002081static int stv0367ter_read_status(struct dvb_frontend *fe,
2082 enum fe_status *status)
Igor M. Liplianin17cce932011-01-25 17:02:00 -03002083{
2084 struct stv0367_state *state = fe->demodulator_priv;
2085
2086 dprintk("%s:\n", __func__);
2087
2088 *status = 0;
2089
2090 if (stv0367_readbits(state, F367TER_LK)) {
2091 *status |= FE_HAS_LOCK;
2092 dprintk("%s: stv0367 has locked\n", __func__);
2093 }
2094
2095 return 0;
2096}
2097
2098static int stv0367ter_read_ber(struct dvb_frontend *fe, u32 *ber)
2099{
2100 struct stv0367_state *state = fe->demodulator_priv;
2101 struct stv0367ter_state *ter_state = state->ter_state;
2102 u32 Errors = 0, tber = 0, temporary = 0;
2103 int abc = 0, def = 0;
2104
2105
2106 /*wait for counting completion*/
2107 if (stv0367_readbits(state, F367TER_SFERRC_OLDVALUE) == 0)
2108 Errors = ((u32)stv0367_readbits(state, F367TER_SFEC_ERR_CNT)
2109 * (1 << 16))
2110 + ((u32)stv0367_readbits(state, F367TER_SFEC_ERR_CNT_HI)
2111 * (1 << 8))
2112 + ((u32)stv0367_readbits(state,
2113 F367TER_SFEC_ERR_CNT_LO));
2114 /*measurement not completed, load previous value*/
2115 else {
2116 tber = ter_state->pBER;
2117 return 0;
2118 }
2119
2120 abc = stv0367_readbits(state, F367TER_SFEC_ERR_SOURCE);
2121 def = stv0367_readbits(state, F367TER_SFEC_NUM_EVENT);
2122
2123 if (Errors == 0) {
2124 tber = 0;
2125 } else if (abc == 0x7) {
2126 if (Errors <= 4) {
2127 temporary = (Errors * 1000000000) / (8 * (1 << 14));
2128 temporary = temporary;
2129 } else if (Errors <= 42) {
2130 temporary = (Errors * 100000000) / (8 * (1 << 14));
2131 temporary = temporary * 10;
2132 } else if (Errors <= 429) {
2133 temporary = (Errors * 10000000) / (8 * (1 << 14));
2134 temporary = temporary * 100;
2135 } else if (Errors <= 4294) {
2136 temporary = (Errors * 1000000) / (8 * (1 << 14));
2137 temporary = temporary * 1000;
2138 } else if (Errors <= 42949) {
2139 temporary = (Errors * 100000) / (8 * (1 << 14));
2140 temporary = temporary * 10000;
2141 } else if (Errors <= 429496) {
2142 temporary = (Errors * 10000) / (8 * (1 << 14));
2143 temporary = temporary * 100000;
2144 } else { /*if (Errors<4294967) 2^22 max error*/
2145 temporary = (Errors * 1000) / (8 * (1 << 14));
2146 temporary = temporary * 100000; /* still to *10 */
2147 }
2148
2149 /* Byte error*/
2150 if (def == 2)
2151 /*tber=Errors/(8*(1 <<14));*/
2152 tber = temporary;
2153 else if (def == 3)
2154 /*tber=Errors/(8*(1 <<16));*/
2155 tber = temporary / 4;
2156 else if (def == 4)
2157 /*tber=Errors/(8*(1 <<18));*/
2158 tber = temporary / 16;
2159 else if (def == 5)
2160 /*tber=Errors/(8*(1 <<20));*/
2161 tber = temporary / 64;
2162 else if (def == 6)
2163 /*tber=Errors/(8*(1 <<22));*/
2164 tber = temporary / 256;
2165 else
2166 /* should not pass here*/
2167 tber = 0;
2168
2169 if ((Errors < 4294967) && (Errors > 429496))
2170 tber *= 10;
2171
2172 }
2173
2174 /* save actual value */
2175 ter_state->pBER = tber;
2176
2177 (*ber) = tber;
2178
2179 return 0;
2180}
2181#if 0
2182static u32 stv0367ter_get_per(struct stv0367_state *state)
2183{
2184 struct stv0367ter_state *ter_state = state->ter_state;
2185 u32 Errors = 0, Per = 0, temporary = 0;
2186 int abc = 0, def = 0, cpt = 0;
2187
2188 while (((stv0367_readbits(state, F367TER_SFERRC_OLDVALUE) == 1) &&
2189 (cpt < 400)) || ((Errors == 0) && (cpt < 400))) {
2190 usleep_range(1000, 2000);
2191 Errors = ((u32)stv0367_readbits(state, F367TER_ERR_CNT1)
2192 * (1 << 16))
2193 + ((u32)stv0367_readbits(state, F367TER_ERR_CNT1_HI)
2194 * (1 << 8))
2195 + ((u32)stv0367_readbits(state, F367TER_ERR_CNT1_LO));
2196 cpt++;
2197 }
2198 abc = stv0367_readbits(state, F367TER_ERR_SRC1);
2199 def = stv0367_readbits(state, F367TER_NUM_EVT1);
2200
2201 if (Errors == 0)
2202 Per = 0;
2203 else if (abc == 0x9) {
2204 if (Errors <= 4) {
2205 temporary = (Errors * 1000000000) / (8 * (1 << 8));
2206 temporary = temporary;
2207 } else if (Errors <= 42) {
2208 temporary = (Errors * 100000000) / (8 * (1 << 8));
2209 temporary = temporary * 10;
2210 } else if (Errors <= 429) {
2211 temporary = (Errors * 10000000) / (8 * (1 << 8));
2212 temporary = temporary * 100;
2213 } else if (Errors <= 4294) {
2214 temporary = (Errors * 1000000) / (8 * (1 << 8));
2215 temporary = temporary * 1000;
2216 } else if (Errors <= 42949) {
2217 temporary = (Errors * 100000) / (8 * (1 << 8));
2218 temporary = temporary * 10000;
2219 } else { /*if(Errors<=429496) 2^16 errors max*/
2220 temporary = (Errors * 10000) / (8 * (1 << 8));
2221 temporary = temporary * 100000;
2222 }
2223
2224 /* pkt error*/
2225 if (def == 2)
2226 /*Per=Errors/(1 << 8);*/
2227 Per = temporary;
2228 else if (def == 3)
2229 /*Per=Errors/(1 << 10);*/
2230 Per = temporary / 4;
2231 else if (def == 4)
2232 /*Per=Errors/(1 << 12);*/
2233 Per = temporary / 16;
2234 else if (def == 5)
2235 /*Per=Errors/(1 << 14);*/
2236 Per = temporary / 64;
2237 else if (def == 6)
2238 /*Per=Errors/(1 << 16);*/
2239 Per = temporary / 256;
2240 else
2241 Per = 0;
2242
2243 }
2244 /* save actual value */
2245 ter_state->pPER = Per;
2246
2247 return Per;
2248}
2249#endif
2250static int stv0367_get_tune_settings(struct dvb_frontend *fe,
2251 struct dvb_frontend_tune_settings
2252 *fe_tune_settings)
2253{
2254 fe_tune_settings->min_delay_ms = 1000;
2255 fe_tune_settings->step_size = 0;
2256 fe_tune_settings->max_drift = 0;
2257
2258 return 0;
2259}
2260
2261static void stv0367_release(struct dvb_frontend *fe)
2262{
2263 struct stv0367_state *state = fe->demodulator_priv;
2264
2265 kfree(state->ter_state);
2266 kfree(state->cab_state);
2267 kfree(state);
2268}
2269
2270static struct dvb_frontend_ops stv0367ter_ops = {
Mauro Carvalho Chehab285d55a2011-12-26 13:03:00 -03002271 .delsys = { SYS_DVBT },
Igor M. Liplianin17cce932011-01-25 17:02:00 -03002272 .info = {
2273 .name = "ST STV0367 DVB-T",
Igor M. Liplianin17cce932011-01-25 17:02:00 -03002274 .frequency_min = 47000000,
2275 .frequency_max = 862000000,
2276 .frequency_stepsize = 15625,
2277 .frequency_tolerance = 0,
2278 .caps = FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 |
2279 FE_CAN_FEC_3_4 | FE_CAN_FEC_5_6 | FE_CAN_FEC_7_8 |
2280 FE_CAN_FEC_AUTO |
2281 FE_CAN_QPSK | FE_CAN_QAM_16 | FE_CAN_QAM_64 |
2282 FE_CAN_QAM_128 | FE_CAN_QAM_256 | FE_CAN_QAM_AUTO |
2283 FE_CAN_TRANSMISSION_MODE_AUTO | FE_CAN_RECOVER |
2284 FE_CAN_INVERSION_AUTO |
2285 FE_CAN_MUTE_TS
2286 },
2287 .release = stv0367_release,
2288 .init = stv0367ter_init,
2289 .sleep = stv0367ter_sleep,
2290 .i2c_gate_ctrl = stv0367ter_gate_ctrl,
Mauro Carvalho Chehab285d55a2011-12-26 13:03:00 -03002291 .set_frontend = stv0367ter_set_frontend,
2292 .get_frontend = stv0367ter_get_frontend,
Igor M. Liplianin17cce932011-01-25 17:02:00 -03002293 .get_tune_settings = stv0367_get_tune_settings,
2294 .read_status = stv0367ter_read_status,
2295 .read_ber = stv0367ter_read_ber,/* too slow */
2296/* .read_signal_strength = stv0367_read_signal_strength,*/
2297 .read_snr = stv0367ter_read_snr,
2298 .read_ucblocks = stv0367ter_read_ucblocks,
2299};
2300
2301struct dvb_frontend *stv0367ter_attach(const struct stv0367_config *config,
2302 struct i2c_adapter *i2c)
2303{
2304 struct stv0367_state *state = NULL;
2305 struct stv0367ter_state *ter_state = NULL;
2306
2307 /* allocate memory for the internal state */
2308 state = kzalloc(sizeof(struct stv0367_state), GFP_KERNEL);
2309 if (state == NULL)
2310 goto error;
2311 ter_state = kzalloc(sizeof(struct stv0367ter_state), GFP_KERNEL);
2312 if (ter_state == NULL)
2313 goto error;
2314
2315 /* setup the state */
2316 state->i2c = i2c;
2317 state->config = config;
2318 state->ter_state = ter_state;
2319 state->fe.ops = stv0367ter_ops;
2320 state->fe.demodulator_priv = state;
2321 state->chip_id = stv0367_readreg(state, 0xf000);
2322
2323 dprintk("%s: chip_id = 0x%x\n", __func__, state->chip_id);
2324
2325 /* check if the demod is there */
2326 if ((state->chip_id != 0x50) && (state->chip_id != 0x60))
2327 goto error;
2328
2329 return &state->fe;
2330
2331error:
2332 kfree(ter_state);
2333 kfree(state);
2334 return NULL;
2335}
2336EXPORT_SYMBOL(stv0367ter_attach);
2337
2338static int stv0367cab_gate_ctrl(struct dvb_frontend *fe, int enable)
2339{
2340 struct stv0367_state *state = fe->demodulator_priv;
2341
2342 dprintk("%s:\n", __func__);
2343
2344 stv0367_writebits(state, F367CAB_I2CT_ON, (enable > 0) ? 1 : 0);
2345
2346 return 0;
2347}
2348
2349static u32 stv0367cab_get_mclk(struct dvb_frontend *fe, u32 ExtClk_Hz)
2350{
2351 struct stv0367_state *state = fe->demodulator_priv;
2352 u32 mclk_Hz = 0;/* master clock frequency (Hz) */
2353 u32 M, N, P;
2354
2355
2356 if (stv0367_readbits(state, F367CAB_BYPASS_PLLXN) == 0) {
2357 N = (u32)stv0367_readbits(state, F367CAB_PLL_NDIV);
2358 if (N == 0)
2359 N = N + 1;
2360
2361 M = (u32)stv0367_readbits(state, F367CAB_PLL_MDIV);
2362 if (M == 0)
2363 M = M + 1;
2364
2365 P = (u32)stv0367_readbits(state, F367CAB_PLL_PDIV);
2366
2367 if (P > 5)
2368 P = 5;
2369
2370 mclk_Hz = ((ExtClk_Hz / 2) * N) / (M * (1 << P));
2371 dprintk("stv0367cab_get_mclk BYPASS_PLLXN mclk_Hz=%d\n",
2372 mclk_Hz);
2373 } else
2374 mclk_Hz = ExtClk_Hz;
2375
2376 dprintk("stv0367cab_get_mclk final mclk_Hz=%d\n", mclk_Hz);
2377
2378 return mclk_Hz;
2379}
2380
2381static u32 stv0367cab_get_adc_freq(struct dvb_frontend *fe, u32 ExtClk_Hz)
2382{
2383 u32 ADCClk_Hz = ExtClk_Hz;
2384
2385 ADCClk_Hz = stv0367cab_get_mclk(fe, ExtClk_Hz);
2386
2387 return ADCClk_Hz;
2388}
2389
Mauro Carvalho Chehab8c8ca1c2012-10-27 11:26:42 -03002390static enum stv0367cab_mod stv0367cab_SetQamSize(struct stv0367_state *state,
2391 u32 SymbolRate,
2392 enum stv0367cab_mod QAMSize)
Igor M. Liplianin17cce932011-01-25 17:02:00 -03002393{
2394 /* Set QAM size */
2395 stv0367_writebits(state, F367CAB_QAM_MODE, QAMSize);
2396
2397 /* Set Registers settings specific to the QAM size */
2398 switch (QAMSize) {
2399 case FE_CAB_MOD_QAM4:
2400 stv0367_writereg(state, R367CAB_IQDEM_ADJ_AGC_REF, 0x00);
2401 break;
2402 case FE_CAB_MOD_QAM16:
2403 stv0367_writereg(state, R367CAB_AGC_PWR_REF_L, 0x64);
2404 stv0367_writereg(state, R367CAB_IQDEM_ADJ_AGC_REF, 0x00);
2405 stv0367_writereg(state, R367CAB_FSM_STATE, 0x90);
2406 stv0367_writereg(state, R367CAB_EQU_CTR_LPF_GAIN, 0xc1);
2407 stv0367_writereg(state, R367CAB_EQU_CRL_LPF_GAIN, 0xa7);
2408 stv0367_writereg(state, R367CAB_EQU_CRL_LD_SEN, 0x95);
2409 stv0367_writereg(state, R367CAB_EQU_CRL_LIMITER, 0x40);
2410 stv0367_writereg(state, R367CAB_EQU_PNT_GAIN, 0x8a);
2411 break;
2412 case FE_CAB_MOD_QAM32:
2413 stv0367_writereg(state, R367CAB_IQDEM_ADJ_AGC_REF, 0x00);
2414 stv0367_writereg(state, R367CAB_AGC_PWR_REF_L, 0x6e);
2415 stv0367_writereg(state, R367CAB_FSM_STATE, 0xb0);
2416 stv0367_writereg(state, R367CAB_EQU_CTR_LPF_GAIN, 0xc1);
2417 stv0367_writereg(state, R367CAB_EQU_CRL_LPF_GAIN, 0xb7);
2418 stv0367_writereg(state, R367CAB_EQU_CRL_LD_SEN, 0x9d);
2419 stv0367_writereg(state, R367CAB_EQU_CRL_LIMITER, 0x7f);
2420 stv0367_writereg(state, R367CAB_EQU_PNT_GAIN, 0xa7);
2421 break;
2422 case FE_CAB_MOD_QAM64:
2423 stv0367_writereg(state, R367CAB_IQDEM_ADJ_AGC_REF, 0x82);
2424 stv0367_writereg(state, R367CAB_AGC_PWR_REF_L, 0x5a);
2425 if (SymbolRate > 45000000) {
2426 stv0367_writereg(state, R367CAB_FSM_STATE, 0xb0);
2427 stv0367_writereg(state, R367CAB_EQU_CTR_LPF_GAIN, 0xc1);
2428 stv0367_writereg(state, R367CAB_EQU_CRL_LPF_GAIN, 0xa5);
2429 } else if (SymbolRate > 25000000) {
2430 stv0367_writereg(state, R367CAB_FSM_STATE, 0xa0);
2431 stv0367_writereg(state, R367CAB_EQU_CTR_LPF_GAIN, 0xc1);
2432 stv0367_writereg(state, R367CAB_EQU_CRL_LPF_GAIN, 0xa6);
2433 } else {
2434 stv0367_writereg(state, R367CAB_FSM_STATE, 0xa0);
2435 stv0367_writereg(state, R367CAB_EQU_CTR_LPF_GAIN, 0xd1);
2436 stv0367_writereg(state, R367CAB_EQU_CRL_LPF_GAIN, 0xa7);
2437 }
2438 stv0367_writereg(state, R367CAB_EQU_CRL_LD_SEN, 0x95);
2439 stv0367_writereg(state, R367CAB_EQU_CRL_LIMITER, 0x40);
2440 stv0367_writereg(state, R367CAB_EQU_PNT_GAIN, 0x99);
2441 break;
2442 case FE_CAB_MOD_QAM128:
2443 stv0367_writereg(state, R367CAB_IQDEM_ADJ_AGC_REF, 0x00);
2444 stv0367_writereg(state, R367CAB_AGC_PWR_REF_L, 0x76);
2445 stv0367_writereg(state, R367CAB_FSM_STATE, 0x90);
2446 stv0367_writereg(state, R367CAB_EQU_CTR_LPF_GAIN, 0xb1);
2447 if (SymbolRate > 45000000)
2448 stv0367_writereg(state, R367CAB_EQU_CRL_LPF_GAIN, 0xa7);
2449 else if (SymbolRate > 25000000)
2450 stv0367_writereg(state, R367CAB_EQU_CRL_LPF_GAIN, 0xa6);
2451 else
2452 stv0367_writereg(state, R367CAB_EQU_CRL_LPF_GAIN, 0x97);
2453
2454 stv0367_writereg(state, R367CAB_EQU_CRL_LD_SEN, 0x8e);
2455 stv0367_writereg(state, R367CAB_EQU_CRL_LIMITER, 0x7f);
2456 stv0367_writereg(state, R367CAB_EQU_PNT_GAIN, 0xa7);
2457 break;
2458 case FE_CAB_MOD_QAM256:
2459 stv0367_writereg(state, R367CAB_IQDEM_ADJ_AGC_REF, 0x94);
2460 stv0367_writereg(state, R367CAB_AGC_PWR_REF_L, 0x5a);
2461 stv0367_writereg(state, R367CAB_FSM_STATE, 0xa0);
2462 if (SymbolRate > 45000000)
2463 stv0367_writereg(state, R367CAB_EQU_CTR_LPF_GAIN, 0xc1);
2464 else if (SymbolRate > 25000000)
2465 stv0367_writereg(state, R367CAB_EQU_CTR_LPF_GAIN, 0xc1);
2466 else
2467 stv0367_writereg(state, R367CAB_EQU_CTR_LPF_GAIN, 0xd1);
2468
2469 stv0367_writereg(state, R367CAB_EQU_CRL_LPF_GAIN, 0xa7);
2470 stv0367_writereg(state, R367CAB_EQU_CRL_LD_SEN, 0x85);
2471 stv0367_writereg(state, R367CAB_EQU_CRL_LIMITER, 0x40);
2472 stv0367_writereg(state, R367CAB_EQU_PNT_GAIN, 0xa7);
2473 break;
2474 case FE_CAB_MOD_QAM512:
2475 stv0367_writereg(state, R367CAB_IQDEM_ADJ_AGC_REF, 0x00);
2476 break;
2477 case FE_CAB_MOD_QAM1024:
2478 stv0367_writereg(state, R367CAB_IQDEM_ADJ_AGC_REF, 0x00);
2479 break;
2480 default:
2481 break;
2482 }
2483
2484 return QAMSize;
2485}
2486
2487static u32 stv0367cab_set_derot_freq(struct stv0367_state *state,
2488 u32 adc_hz, s32 derot_hz)
2489{
2490 u32 sampled_if = 0;
2491 u32 adc_khz;
2492
2493 adc_khz = adc_hz / 1000;
2494
2495 dprintk("%s: adc_hz=%d derot_hz=%d\n", __func__, adc_hz, derot_hz);
2496
2497 if (adc_khz != 0) {
2498 if (derot_hz < 1000000)
2499 derot_hz = adc_hz / 4; /* ZIF operation */
2500 if (derot_hz > adc_hz)
2501 derot_hz = derot_hz - adc_hz;
2502 sampled_if = (u32)derot_hz / 1000;
2503 sampled_if *= 32768;
2504 sampled_if /= adc_khz;
2505 sampled_if *= 256;
2506 }
2507
2508 if (sampled_if > 8388607)
2509 sampled_if = 8388607;
2510
2511 dprintk("%s: sampled_if=0x%x\n", __func__, sampled_if);
2512
2513 stv0367_writereg(state, R367CAB_MIX_NCO_LL, sampled_if);
2514 stv0367_writereg(state, R367CAB_MIX_NCO_HL, (sampled_if >> 8));
2515 stv0367_writebits(state, F367CAB_MIX_NCO_INC_HH, (sampled_if >> 16));
2516
2517 return derot_hz;
2518}
2519
2520static u32 stv0367cab_get_derot_freq(struct stv0367_state *state, u32 adc_hz)
2521{
2522 u32 sampled_if;
2523
2524 sampled_if = stv0367_readbits(state, F367CAB_MIX_NCO_INC_LL) +
2525 (stv0367_readbits(state, F367CAB_MIX_NCO_INC_HL) << 8) +
2526 (stv0367_readbits(state, F367CAB_MIX_NCO_INC_HH) << 16);
2527
2528 sampled_if /= 256;
2529 sampled_if *= (adc_hz / 1000);
2530 sampled_if += 1;
2531 sampled_if /= 32768;
2532
2533 return sampled_if;
2534}
2535
2536static u32 stv0367cab_set_srate(struct stv0367_state *state, u32 adc_hz,
2537 u32 mclk_hz, u32 SymbolRate,
2538 enum stv0367cab_mod QAMSize)
2539{
2540 u32 QamSizeCorr = 0;
2541 u32 u32_tmp = 0, u32_tmp1 = 0;
2542 u32 adp_khz;
2543
2544 dprintk("%s:\n", __func__);
2545
2546 /* Set Correction factor of SRC gain */
2547 switch (QAMSize) {
2548 case FE_CAB_MOD_QAM4:
2549 QamSizeCorr = 1110;
2550 break;
2551 case FE_CAB_MOD_QAM16:
2552 QamSizeCorr = 1032;
2553 break;
2554 case FE_CAB_MOD_QAM32:
2555 QamSizeCorr = 954;
2556 break;
2557 case FE_CAB_MOD_QAM64:
2558 QamSizeCorr = 983;
2559 break;
2560 case FE_CAB_MOD_QAM128:
2561 QamSizeCorr = 957;
2562 break;
2563 case FE_CAB_MOD_QAM256:
2564 QamSizeCorr = 948;
2565 break;
2566 case FE_CAB_MOD_QAM512:
2567 QamSizeCorr = 0;
2568 break;
2569 case FE_CAB_MOD_QAM1024:
2570 QamSizeCorr = 944;
2571 break;
2572 default:
2573 break;
2574 }
2575
2576 /* Transfer ratio calculation */
2577 if (adc_hz != 0) {
2578 u32_tmp = 256 * SymbolRate;
2579 u32_tmp = u32_tmp / adc_hz;
2580 }
2581 stv0367_writereg(state, R367CAB_EQU_CRL_TFR, (u8)u32_tmp);
2582
2583 /* Symbol rate and SRC gain calculation */
2584 adp_khz = (mclk_hz >> 1) / 1000;/* TRL works at half the system clock */
2585 if (adp_khz != 0) {
2586 u32_tmp = SymbolRate;
2587 u32_tmp1 = SymbolRate;
2588
2589 if (u32_tmp < 2097152) { /* 2097152 = 2^21 */
2590 /* Symbol rate calculation */
2591 u32_tmp *= 2048; /* 2048 = 2^11 */
2592 u32_tmp = u32_tmp / adp_khz;
2593 u32_tmp = u32_tmp * 16384; /* 16384 = 2^14 */
2594 u32_tmp /= 125 ; /* 125 = 1000/2^3 */
2595 u32_tmp = u32_tmp * 8; /* 8 = 2^3 */
2596
2597 /* SRC Gain Calculation */
2598 u32_tmp1 *= 2048; /* *2*2^10 */
2599 u32_tmp1 /= 439; /* *2/878 */
2600 u32_tmp1 *= 256; /* *2^8 */
2601 u32_tmp1 = u32_tmp1 / adp_khz; /* /(AdpClk in kHz) */
2602 u32_tmp1 *= QamSizeCorr * 9; /* *1000*corr factor */
2603 u32_tmp1 = u32_tmp1 / 10000000;
2604
2605 } else if (u32_tmp < 4194304) { /* 4194304 = 2**22 */
2606 /* Symbol rate calculation */
2607 u32_tmp *= 1024 ; /* 1024 = 2**10 */
2608 u32_tmp = u32_tmp / adp_khz;
2609 u32_tmp = u32_tmp * 16384; /* 16384 = 2**14 */
2610 u32_tmp /= 125 ; /* 125 = 1000/2**3 */
2611 u32_tmp = u32_tmp * 16; /* 16 = 2**4 */
2612
2613 /* SRC Gain Calculation */
2614 u32_tmp1 *= 1024; /* *2*2^9 */
2615 u32_tmp1 /= 439; /* *2/878 */
2616 u32_tmp1 *= 256; /* *2^8 */
2617 u32_tmp1 = u32_tmp1 / adp_khz; /* /(AdpClk in kHz)*/
2618 u32_tmp1 *= QamSizeCorr * 9; /* *1000*corr factor */
2619 u32_tmp1 = u32_tmp1 / 5000000;
2620 } else if (u32_tmp < 8388607) { /* 8388607 = 2**23 */
2621 /* Symbol rate calculation */
2622 u32_tmp *= 512 ; /* 512 = 2**9 */
2623 u32_tmp = u32_tmp / adp_khz;
2624 u32_tmp = u32_tmp * 16384; /* 16384 = 2**14 */
2625 u32_tmp /= 125 ; /* 125 = 1000/2**3 */
2626 u32_tmp = u32_tmp * 32; /* 32 = 2**5 */
2627
2628 /* SRC Gain Calculation */
2629 u32_tmp1 *= 512; /* *2*2^8 */
2630 u32_tmp1 /= 439; /* *2/878 */
2631 u32_tmp1 *= 256; /* *2^8 */
2632 u32_tmp1 = u32_tmp1 / adp_khz; /* /(AdpClk in kHz) */
2633 u32_tmp1 *= QamSizeCorr * 9; /* *1000*corr factor */
2634 u32_tmp1 = u32_tmp1 / 2500000;
2635 } else {
2636 /* Symbol rate calculation */
2637 u32_tmp *= 256 ; /* 256 = 2**8 */
2638 u32_tmp = u32_tmp / adp_khz;
2639 u32_tmp = u32_tmp * 16384; /* 16384 = 2**13 */
2640 u32_tmp /= 125 ; /* 125 = 1000/2**3 */
2641 u32_tmp = u32_tmp * 64; /* 64 = 2**6 */
2642
2643 /* SRC Gain Calculation */
2644 u32_tmp1 *= 256; /* 2*2^7 */
2645 u32_tmp1 /= 439; /* *2/878 */
2646 u32_tmp1 *= 256; /* *2^8 */
2647 u32_tmp1 = u32_tmp1 / adp_khz; /* /(AdpClk in kHz) */
2648 u32_tmp1 *= QamSizeCorr * 9; /* *1000*corr factor */
2649 u32_tmp1 = u32_tmp1 / 1250000;
2650 }
2651 }
2652#if 0
2653 /* Filters' coefficients are calculated and written
2654 into registers only if the filters are enabled */
2655 if (stv0367_readbits(state, F367CAB_ADJ_EN)) {
2656 stv0367cab_SetIirAdjacentcoefficient(state, mclk_hz,
2657 SymbolRate);
2658 /* AllPass filter must be enabled
2659 when the adjacents filter is used */
2660 stv0367_writebits(state, F367CAB_ALLPASSFILT_EN, 1);
2661 stv0367cab_SetAllPasscoefficient(state, mclk_hz, SymbolRate);
2662 } else
2663 /* AllPass filter must be disabled
2664 when the adjacents filter is not used */
2665#endif
2666 stv0367_writebits(state, F367CAB_ALLPASSFILT_EN, 0);
2667
2668 stv0367_writereg(state, R367CAB_SRC_NCO_LL, u32_tmp);
2669 stv0367_writereg(state, R367CAB_SRC_NCO_LH, (u32_tmp >> 8));
2670 stv0367_writereg(state, R367CAB_SRC_NCO_HL, (u32_tmp >> 16));
2671 stv0367_writereg(state, R367CAB_SRC_NCO_HH, (u32_tmp >> 24));
2672
2673 stv0367_writereg(state, R367CAB_IQDEM_GAIN_SRC_L, u32_tmp1 & 0x00ff);
2674 stv0367_writebits(state, F367CAB_GAIN_SRC_HI, (u32_tmp1 >> 8) & 0x00ff);
2675
2676 return SymbolRate ;
2677}
2678
2679static u32 stv0367cab_GetSymbolRate(struct stv0367_state *state, u32 mclk_hz)
2680{
2681 u32 regsym;
2682 u32 adp_khz;
2683
2684 regsym = stv0367_readreg(state, R367CAB_SRC_NCO_LL) +
2685 (stv0367_readreg(state, R367CAB_SRC_NCO_LH) << 8) +
2686 (stv0367_readreg(state, R367CAB_SRC_NCO_HL) << 16) +
2687 (stv0367_readreg(state, R367CAB_SRC_NCO_HH) << 24);
2688
2689 adp_khz = (mclk_hz >> 1) / 1000;/* TRL works at half the system clock */
2690
2691 if (regsym < 134217728) { /* 134217728L = 2**27*/
2692 regsym = regsym * 32; /* 32 = 2**5 */
2693 regsym = regsym / 32768; /* 32768L = 2**15 */
2694 regsym = adp_khz * regsym; /* AdpClk in kHz */
2695 regsym = regsym / 128; /* 128 = 2**7 */
2696 regsym *= 125 ; /* 125 = 1000/2**3 */
2697 regsym /= 2048 ; /* 2048 = 2**11 */
2698 } else if (regsym < 268435456) { /* 268435456L = 2**28 */
2699 regsym = regsym * 16; /* 16 = 2**4 */
2700 regsym = regsym / 32768; /* 32768L = 2**15 */
2701 regsym = adp_khz * regsym; /* AdpClk in kHz */
2702 regsym = regsym / 128; /* 128 = 2**7 */
2703 regsym *= 125 ; /* 125 = 1000/2**3*/
2704 regsym /= 1024 ; /* 256 = 2**10*/
2705 } else if (regsym < 536870912) { /* 536870912L = 2**29*/
2706 regsym = regsym * 8; /* 8 = 2**3 */
2707 regsym = regsym / 32768; /* 32768L = 2**15 */
2708 regsym = adp_khz * regsym; /* AdpClk in kHz */
2709 regsym = regsym / 128; /* 128 = 2**7 */
2710 regsym *= 125 ; /* 125 = 1000/2**3 */
2711 regsym /= 512 ; /* 128 = 2**9 */
2712 } else {
2713 regsym = regsym * 4; /* 4 = 2**2 */
2714 regsym = regsym / 32768; /* 32768L = 2**15 */
2715 regsym = adp_khz * regsym; /* AdpClk in kHz */
2716 regsym = regsym / 128; /* 128 = 2**7 */
2717 regsym *= 125 ; /* 125 = 1000/2**3 */
2718 regsym /= 256 ; /* 64 = 2**8 */
2719 }
2720
2721 return regsym;
2722}
2723
Mauro Carvalho Chehab0df289a2015-06-07 14:53:52 -03002724static int stv0367cab_read_status(struct dvb_frontend *fe,
2725 enum fe_status *status)
Igor M. Liplianin17cce932011-01-25 17:02:00 -03002726{
2727 struct stv0367_state *state = fe->demodulator_priv;
2728
2729 dprintk("%s:\n", __func__);
2730
2731 *status = 0;
2732
2733 if (stv0367_readbits(state, F367CAB_QAMFEC_LOCK)) {
2734 *status |= FE_HAS_LOCK;
2735 dprintk("%s: stv0367 has locked\n", __func__);
2736 }
2737
2738 return 0;
2739}
2740
2741static int stv0367cab_standby(struct dvb_frontend *fe, u8 standby_on)
2742{
2743 struct stv0367_state *state = fe->demodulator_priv;
2744
2745 dprintk("%s:\n", __func__);
2746
2747 if (standby_on) {
2748 stv0367_writebits(state, F367CAB_BYPASS_PLLXN, 0x03);
2749 stv0367_writebits(state, F367CAB_STDBY_PLLXN, 0x01);
2750 stv0367_writebits(state, F367CAB_STDBY, 1);
2751 stv0367_writebits(state, F367CAB_STDBY_CORE, 1);
2752 stv0367_writebits(state, F367CAB_EN_BUFFER_I, 0);
2753 stv0367_writebits(state, F367CAB_EN_BUFFER_Q, 0);
2754 stv0367_writebits(state, F367CAB_POFFQ, 1);
2755 stv0367_writebits(state, F367CAB_POFFI, 1);
2756 } else {
2757 stv0367_writebits(state, F367CAB_STDBY_PLLXN, 0x00);
2758 stv0367_writebits(state, F367CAB_BYPASS_PLLXN, 0x00);
2759 stv0367_writebits(state, F367CAB_STDBY, 0);
2760 stv0367_writebits(state, F367CAB_STDBY_CORE, 0);
2761 stv0367_writebits(state, F367CAB_EN_BUFFER_I, 1);
2762 stv0367_writebits(state, F367CAB_EN_BUFFER_Q, 1);
2763 stv0367_writebits(state, F367CAB_POFFQ, 0);
2764 stv0367_writebits(state, F367CAB_POFFI, 0);
2765 }
2766
2767 return 0;
2768}
2769
2770static int stv0367cab_sleep(struct dvb_frontend *fe)
2771{
2772 return stv0367cab_standby(fe, 1);
2773}
2774
Mauro Carvalho Chehab8c8ca1c2012-10-27 11:26:42 -03002775static int stv0367cab_init(struct dvb_frontend *fe)
Igor M. Liplianin17cce932011-01-25 17:02:00 -03002776{
2777 struct stv0367_state *state = fe->demodulator_priv;
2778 struct stv0367cab_state *cab_state = state->cab_state;
2779 int i;
2780
2781 dprintk("%s:\n", __func__);
2782
2783 for (i = 0; i < STV0367CAB_NBREGS; i++)
2784 stv0367_writereg(state, def0367cab[i].addr,
2785 def0367cab[i].value);
2786
2787 switch (state->config->ts_mode) {
2788 case STV0367_DVBCI_CLOCK:
2789 dprintk("Setting TSMode = STV0367_DVBCI_CLOCK\n");
2790 stv0367_writebits(state, F367CAB_OUTFORMAT, 0x03);
2791 break;
2792 case STV0367_SERIAL_PUNCT_CLOCK:
2793 case STV0367_SERIAL_CONT_CLOCK:
2794 stv0367_writebits(state, F367CAB_OUTFORMAT, 0x01);
2795 break;
2796 case STV0367_PARALLEL_PUNCT_CLOCK:
2797 case STV0367_OUTPUTMODE_DEFAULT:
2798 stv0367_writebits(state, F367CAB_OUTFORMAT, 0x00);
2799 break;
2800 }
2801
2802 switch (state->config->clk_pol) {
2803 case STV0367_RISINGEDGE_CLOCK:
2804 stv0367_writebits(state, F367CAB_CLK_POLARITY, 0x00);
2805 break;
2806 case STV0367_FALLINGEDGE_CLOCK:
2807 case STV0367_CLOCKPOLARITY_DEFAULT:
2808 stv0367_writebits(state, F367CAB_CLK_POLARITY, 0x01);
2809 break;
2810 }
2811
2812 stv0367_writebits(state, F367CAB_SYNC_STRIP, 0x00);
2813
2814 stv0367_writebits(state, F367CAB_CT_NBST, 0x01);
2815
2816 stv0367_writebits(state, F367CAB_TS_SWAP, 0x01);
2817
2818 stv0367_writebits(state, F367CAB_FIFO_BYPASS, 0x00);
2819
2820 stv0367_writereg(state, R367CAB_ANACTRL, 0x00);/*PLL enabled and used */
2821
2822 cab_state->mclk = stv0367cab_get_mclk(fe, state->config->xtal);
2823 cab_state->adc_clk = stv0367cab_get_adc_freq(fe, state->config->xtal);
2824
2825 return 0;
2826}
2827static
2828enum stv0367_cab_signal_type stv0367cab_algo(struct stv0367_state *state,
Mauro Carvalho Chehab285d55a2011-12-26 13:03:00 -03002829 struct dtv_frontend_properties *p)
Igor M. Liplianin17cce932011-01-25 17:02:00 -03002830{
Igor M. Liplianin17cce932011-01-25 17:02:00 -03002831 struct stv0367cab_state *cab_state = state->cab_state;
2832 enum stv0367_cab_signal_type signalType = FE_CAB_NOAGC;
2833 u32 QAMFEC_Lock, QAM_Lock, u32_tmp,
2834 LockTime, TRLTimeOut, AGCTimeOut, CRLSymbols,
2835 CRLTimeOut, EQLTimeOut, DemodTimeOut, FECTimeOut;
2836 u8 TrackAGCAccum;
2837 s32 tmp;
2838
2839 dprintk("%s:\n", __func__);
2840
2841 /* Timeouts calculation */
2842 /* A max lock time of 25 ms is allowed for delayed AGC */
2843 AGCTimeOut = 25;
2844 /* 100000 symbols needed by the TRL as a maximum value */
Mauro Carvalho Chehab285d55a2011-12-26 13:03:00 -03002845 TRLTimeOut = 100000000 / p->symbol_rate;
Igor M. Liplianin17cce932011-01-25 17:02:00 -03002846 /* CRLSymbols is the needed number of symbols to achieve a lock
2847 within [-4%, +4%] of the symbol rate.
2848 CRL timeout is calculated
2849 for a lock within [-search_range, +search_range].
2850 EQL timeout can be changed depending on
2851 the micro-reflections we want to handle.
2852 A characterization must be performed
2853 with these echoes to get new timeout values.
2854 */
Mauro Carvalho Chehab285d55a2011-12-26 13:03:00 -03002855 switch (p->modulation) {
Igor M. Liplianin17cce932011-01-25 17:02:00 -03002856 case QAM_16:
2857 CRLSymbols = 150000;
2858 EQLTimeOut = 100;
2859 break;
2860 case QAM_32:
2861 CRLSymbols = 250000;
2862 EQLTimeOut = 100;
2863 break;
2864 case QAM_64:
2865 CRLSymbols = 200000;
2866 EQLTimeOut = 100;
2867 break;
2868 case QAM_128:
2869 CRLSymbols = 250000;
2870 EQLTimeOut = 100;
2871 break;
2872 case QAM_256:
2873 CRLSymbols = 250000;
2874 EQLTimeOut = 100;
2875 break;
2876 default:
2877 CRLSymbols = 200000;
2878 EQLTimeOut = 100;
2879 break;
2880 }
2881#if 0
2882 if (pIntParams->search_range < 0) {
2883 CRLTimeOut = (25 * CRLSymbols *
2884 (-pIntParams->search_range / 1000)) /
2885 (pIntParams->symbol_rate / 1000);
2886 } else
2887#endif
2888 CRLTimeOut = (25 * CRLSymbols * (cab_state->search_range / 1000)) /
Mauro Carvalho Chehab285d55a2011-12-26 13:03:00 -03002889 (p->symbol_rate / 1000);
Igor M. Liplianin17cce932011-01-25 17:02:00 -03002890
Mauro Carvalho Chehab285d55a2011-12-26 13:03:00 -03002891 CRLTimeOut = (1000 * CRLTimeOut) / p->symbol_rate;
Igor M. Liplianin17cce932011-01-25 17:02:00 -03002892 /* Timeouts below 50ms are coerced */
2893 if (CRLTimeOut < 50)
2894 CRLTimeOut = 50;
2895 /* A maximum of 100 TS packets is needed to get FEC lock even in case
2896 the spectrum inversion needs to be changed.
2897 This is equal to 20 ms in case of the lowest symbol rate of 0.87Msps
2898 */
2899 FECTimeOut = 20;
2900 DemodTimeOut = AGCTimeOut + TRLTimeOut + CRLTimeOut + EQLTimeOut;
2901
2902 dprintk("%s: DemodTimeOut=%d\n", __func__, DemodTimeOut);
2903
2904 /* Reset the TRL to ensure nothing starts until the
2905 AGC is stable which ensures a better lock time
2906 */
2907 stv0367_writereg(state, R367CAB_CTRL_1, 0x04);
2908 /* Set AGC accumulation time to minimum and lock threshold to maximum
2909 in order to speed up the AGC lock */
2910 TrackAGCAccum = stv0367_readbits(state, F367CAB_AGC_ACCUMRSTSEL);
2911 stv0367_writebits(state, F367CAB_AGC_ACCUMRSTSEL, 0x0);
2912 /* Modulus Mapper is disabled */
2913 stv0367_writebits(state, F367CAB_MODULUSMAP_EN, 0);
2914 /* Disable the sweep function */
2915 stv0367_writebits(state, F367CAB_SWEEP_EN, 0);
2916 /* The sweep function is never used, Sweep rate must be set to 0 */
2917 /* Set the derotator frequency in Hz */
2918 stv0367cab_set_derot_freq(state, cab_state->adc_clk,
2919 (1000 * (s32)state->config->if_khz + cab_state->derot_offset));
2920 /* Disable the Allpass Filter when the symbol rate is out of range */
Mauro Carvalho Chehab285d55a2011-12-26 13:03:00 -03002921 if ((p->symbol_rate > 10800000) | (p->symbol_rate < 1800000)) {
Igor M. Liplianin17cce932011-01-25 17:02:00 -03002922 stv0367_writebits(state, F367CAB_ADJ_EN, 0);
2923 stv0367_writebits(state, F367CAB_ALLPASSFILT_EN, 0);
2924 }
2925#if 0
2926 /* Check if the tuner is locked */
2927 tuner_lock = stv0367cab_tuner_get_status(fe);
2928 if (tuner_lock == 0)
2929 return FE_367CAB_NOTUNER;
2930#endif
Geert Uytterhoeven83a35e32013-06-28 11:27:31 +02002931 /* Release the TRL to start demodulator acquisition */
Igor M. Liplianin17cce932011-01-25 17:02:00 -03002932 /* Wait for QAM lock */
2933 LockTime = 0;
2934 stv0367_writereg(state, R367CAB_CTRL_1, 0x00);
2935 do {
2936 QAM_Lock = stv0367_readbits(state, F367CAB_FSM_STATUS);
2937 if ((LockTime >= (DemodTimeOut - EQLTimeOut)) &&
2938 (QAM_Lock == 0x04))
2939 /*
2940 * We don't wait longer, the frequency/phase offset
2941 * must be too big
2942 */
2943 LockTime = DemodTimeOut;
2944 else if ((LockTime >= (AGCTimeOut + TRLTimeOut)) &&
2945 (QAM_Lock == 0x02))
2946 /*
2947 * We don't wait longer, either there is no signal or
2948 * it is not the right symbol rate or it is an analog
2949 * carrier
2950 */
2951 {
2952 LockTime = DemodTimeOut;
2953 u32_tmp = stv0367_readbits(state,
2954 F367CAB_AGC_PWR_WORD_LO) +
2955 (stv0367_readbits(state,
2956 F367CAB_AGC_PWR_WORD_ME) << 8) +
2957 (stv0367_readbits(state,
2958 F367CAB_AGC_PWR_WORD_HI) << 16);
2959 if (u32_tmp >= 131072)
2960 u32_tmp = 262144 - u32_tmp;
2961 u32_tmp = u32_tmp / (1 << (11 - stv0367_readbits(state,
2962 F367CAB_AGC_IF_BWSEL)));
2963
2964 if (u32_tmp < stv0367_readbits(state,
2965 F367CAB_AGC_PWRREF_LO) +
2966 256 * stv0367_readbits(state,
2967 F367CAB_AGC_PWRREF_HI) - 10)
2968 QAM_Lock = 0x0f;
2969 } else {
2970 usleep_range(10000, 20000);
2971 LockTime += 10;
2972 }
2973 dprintk("QAM_Lock=0x%x LockTime=%d\n", QAM_Lock, LockTime);
2974 tmp = stv0367_readreg(state, R367CAB_IT_STATUS1);
2975
2976 dprintk("R367CAB_IT_STATUS1=0x%x\n", tmp);
2977
2978 } while (((QAM_Lock != 0x0c) && (QAM_Lock != 0x0b)) &&
2979 (LockTime < DemodTimeOut));
2980
2981 dprintk("QAM_Lock=0x%x\n", QAM_Lock);
2982
2983 tmp = stv0367_readreg(state, R367CAB_IT_STATUS1);
2984 dprintk("R367CAB_IT_STATUS1=0x%x\n", tmp);
2985 tmp = stv0367_readreg(state, R367CAB_IT_STATUS2);
2986 dprintk("R367CAB_IT_STATUS2=0x%x\n", tmp);
2987
2988 tmp = stv0367cab_get_derot_freq(state, cab_state->adc_clk);
2989 dprintk("stv0367cab_get_derot_freq=0x%x\n", tmp);
2990
2991 if ((QAM_Lock == 0x0c) || (QAM_Lock == 0x0b)) {
2992 /* Wait for FEC lock */
2993 LockTime = 0;
2994 do {
2995 usleep_range(5000, 7000);
2996 LockTime += 5;
2997 QAMFEC_Lock = stv0367_readbits(state,
2998 F367CAB_QAMFEC_LOCK);
2999 } while (!QAMFEC_Lock && (LockTime < FECTimeOut));
3000 } else
3001 QAMFEC_Lock = 0;
3002
3003 if (QAMFEC_Lock) {
3004 signalType = FE_CAB_DATAOK;
Igor M. Liplianin17cce932011-01-25 17:02:00 -03003005 cab_state->spect_inv = stv0367_readbits(state,
3006 F367CAB_QUAD_INV);
3007#if 0
3008/* not clear for me */
3009 if (state->config->if_khz != 0) {
3010 if (state->config->if_khz > cab_state->adc_clk / 1000) {
3011 cab_state->freq_khz =
3012 FE_Cab_TunerGetFrequency(pIntParams->hTuner)
3013 - stv0367cab_get_derot_freq(state, cab_state->adc_clk)
3014 - cab_state->adc_clk / 1000 + state->config->if_khz;
3015 } else {
3016 cab_state->freq_khz =
3017 FE_Cab_TunerGetFrequency(pIntParams->hTuner)
3018 - stv0367cab_get_derot_freq(state, cab_state->adc_clk)
3019 + state->config->if_khz;
3020 }
3021 } else {
3022 cab_state->freq_khz =
3023 FE_Cab_TunerGetFrequency(pIntParams->hTuner) +
3024 stv0367cab_get_derot_freq(state,
3025 cab_state->adc_clk) -
3026 cab_state->adc_clk / 4000;
3027 }
3028#endif
3029 cab_state->symbol_rate = stv0367cab_GetSymbolRate(state,
3030 cab_state->mclk);
3031 cab_state->locked = 1;
3032
3033 /* stv0367_setbits(state, F367CAB_AGC_ACCUMRSTSEL,7);*/
3034 } else {
3035 switch (QAM_Lock) {
3036 case 1:
3037 signalType = FE_CAB_NOAGC;
3038 break;
3039 case 2:
3040 signalType = FE_CAB_NOTIMING;
3041 break;
3042 case 3:
3043 signalType = FE_CAB_TIMINGOK;
3044 break;
3045 case 4:
3046 signalType = FE_CAB_NOCARRIER;
3047 break;
3048 case 5:
3049 signalType = FE_CAB_CARRIEROK;
3050 break;
3051 case 7:
3052 signalType = FE_CAB_NOBLIND;
3053 break;
3054 case 8:
3055 signalType = FE_CAB_BLINDOK;
3056 break;
3057 case 10:
3058 signalType = FE_CAB_NODEMOD;
3059 break;
3060 case 11:
3061 signalType = FE_CAB_DEMODOK;
3062 break;
3063 case 12:
3064 signalType = FE_CAB_DEMODOK;
3065 break;
3066 case 13:
3067 signalType = FE_CAB_NODEMOD;
3068 break;
3069 case 14:
3070 signalType = FE_CAB_NOBLIND;
3071 break;
3072 case 15:
3073 signalType = FE_CAB_NOSIGNAL;
3074 break;
3075 default:
3076 break;
3077 }
3078
3079 }
3080
3081 /* Set the AGC control values to tracking values */
3082 stv0367_writebits(state, F367CAB_AGC_ACCUMRSTSEL, TrackAGCAccum);
3083 return signalType;
3084}
3085
Mauro Carvalho Chehab285d55a2011-12-26 13:03:00 -03003086static int stv0367cab_set_frontend(struct dvb_frontend *fe)
Igor M. Liplianin17cce932011-01-25 17:02:00 -03003087{
Mauro Carvalho Chehab285d55a2011-12-26 13:03:00 -03003088 struct dtv_frontend_properties *p = &fe->dtv_property_cache;
Igor M. Liplianin17cce932011-01-25 17:02:00 -03003089 struct stv0367_state *state = fe->demodulator_priv;
3090 struct stv0367cab_state *cab_state = state->cab_state;
Igor M. Liplianin17cce932011-01-25 17:02:00 -03003091 enum stv0367cab_mod QAMSize = 0;
3092
3093 dprintk("%s: freq = %d, srate = %d\n", __func__,
Mauro Carvalho Chehab285d55a2011-12-26 13:03:00 -03003094 p->frequency, p->symbol_rate);
Igor M. Liplianin17cce932011-01-25 17:02:00 -03003095
3096 cab_state->derot_offset = 0;
3097
Mauro Carvalho Chehab285d55a2011-12-26 13:03:00 -03003098 switch (p->modulation) {
Igor M. Liplianin17cce932011-01-25 17:02:00 -03003099 case QAM_16:
3100 QAMSize = FE_CAB_MOD_QAM16;
3101 break;
3102 case QAM_32:
3103 QAMSize = FE_CAB_MOD_QAM32;
3104 break;
3105 case QAM_64:
3106 QAMSize = FE_CAB_MOD_QAM64;
3107 break;
3108 case QAM_128:
3109 QAMSize = FE_CAB_MOD_QAM128;
3110 break;
3111 case QAM_256:
3112 QAMSize = FE_CAB_MOD_QAM256;
3113 break;
3114 default:
3115 break;
3116 }
3117
3118 stv0367cab_init(fe);
3119
3120 /* Tuner Frequency Setting */
3121 if (fe->ops.tuner_ops.set_params) {
3122 if (fe->ops.i2c_gate_ctrl)
3123 fe->ops.i2c_gate_ctrl(fe, 1);
Mauro Carvalho Chehab14d24d12011-12-24 12:24:33 -03003124 fe->ops.tuner_ops.set_params(fe);
Igor M. Liplianin17cce932011-01-25 17:02:00 -03003125 if (fe->ops.i2c_gate_ctrl)
3126 fe->ops.i2c_gate_ctrl(fe, 0);
3127 }
3128
3129 stv0367cab_SetQamSize(
3130 state,
Mauro Carvalho Chehab285d55a2011-12-26 13:03:00 -03003131 p->symbol_rate,
Igor M. Liplianin17cce932011-01-25 17:02:00 -03003132 QAMSize);
3133
3134 stv0367cab_set_srate(state,
3135 cab_state->adc_clk,
3136 cab_state->mclk,
Mauro Carvalho Chehab285d55a2011-12-26 13:03:00 -03003137 p->symbol_rate,
Igor M. Liplianin17cce932011-01-25 17:02:00 -03003138 QAMSize);
3139 /* Search algorithm launch, [-1.1*RangeOffset, +1.1*RangeOffset] scan */
Mauro Carvalho Chehab285d55a2011-12-26 13:03:00 -03003140 cab_state->state = stv0367cab_algo(state, p);
Igor M. Liplianin17cce932011-01-25 17:02:00 -03003141 return 0;
3142}
3143
Mauro Carvalho Chehab7c61d802011-12-30 11:30:21 -03003144static int stv0367cab_get_frontend(struct dvb_frontend *fe)
Igor M. Liplianin17cce932011-01-25 17:02:00 -03003145{
Mauro Carvalho Chehab7c61d802011-12-30 11:30:21 -03003146 struct dtv_frontend_properties *p = &fe->dtv_property_cache;
Igor M. Liplianin17cce932011-01-25 17:02:00 -03003147 struct stv0367_state *state = fe->demodulator_priv;
3148 struct stv0367cab_state *cab_state = state->cab_state;
Igor M. Liplianin17cce932011-01-25 17:02:00 -03003149
3150 enum stv0367cab_mod QAMSize;
3151
3152 dprintk("%s:\n", __func__);
3153
Mauro Carvalho Chehab285d55a2011-12-26 13:03:00 -03003154 p->symbol_rate = stv0367cab_GetSymbolRate(state, cab_state->mclk);
Igor M. Liplianin17cce932011-01-25 17:02:00 -03003155
3156 QAMSize = stv0367_readbits(state, F367CAB_QAM_MODE);
3157 switch (QAMSize) {
3158 case FE_CAB_MOD_QAM16:
Mauro Carvalho Chehab285d55a2011-12-26 13:03:00 -03003159 p->modulation = QAM_16;
Igor M. Liplianin17cce932011-01-25 17:02:00 -03003160 break;
3161 case FE_CAB_MOD_QAM32:
Mauro Carvalho Chehab285d55a2011-12-26 13:03:00 -03003162 p->modulation = QAM_32;
Igor M. Liplianin17cce932011-01-25 17:02:00 -03003163 break;
3164 case FE_CAB_MOD_QAM64:
Mauro Carvalho Chehab285d55a2011-12-26 13:03:00 -03003165 p->modulation = QAM_64;
Igor M. Liplianin17cce932011-01-25 17:02:00 -03003166 break;
3167 case FE_CAB_MOD_QAM128:
Mauro Carvalho Chehab285d55a2011-12-26 13:03:00 -03003168 p->modulation = QAM_128;
Igor M. Liplianin17cce932011-01-25 17:02:00 -03003169 break;
Maks Naumoveafeda92014-08-15 16:23:20 -03003170 case FE_CAB_MOD_QAM256:
Mauro Carvalho Chehab285d55a2011-12-26 13:03:00 -03003171 p->modulation = QAM_256;
Igor M. Liplianin17cce932011-01-25 17:02:00 -03003172 break;
3173 default:
3174 break;
3175 }
3176
Mauro Carvalho Chehab285d55a2011-12-26 13:03:00 -03003177 p->frequency = stv0367_get_tuner_freq(fe);
Igor M. Liplianin17cce932011-01-25 17:02:00 -03003178
Mauro Carvalho Chehab285d55a2011-12-26 13:03:00 -03003179 dprintk("%s: tuner frequency = %d\n", __func__, p->frequency);
Igor M. Liplianin17cce932011-01-25 17:02:00 -03003180
3181 if (state->config->if_khz == 0) {
Mauro Carvalho Chehab285d55a2011-12-26 13:03:00 -03003182 p->frequency +=
Igor M. Liplianin17cce932011-01-25 17:02:00 -03003183 (stv0367cab_get_derot_freq(state, cab_state->adc_clk) -
3184 cab_state->adc_clk / 4000);
3185 return 0;
3186 }
3187
3188 if (state->config->if_khz > cab_state->adc_clk / 1000)
Mauro Carvalho Chehab285d55a2011-12-26 13:03:00 -03003189 p->frequency += (state->config->if_khz
Igor M. Liplianin17cce932011-01-25 17:02:00 -03003190 - stv0367cab_get_derot_freq(state, cab_state->adc_clk)
3191 - cab_state->adc_clk / 1000);
3192 else
Mauro Carvalho Chehab285d55a2011-12-26 13:03:00 -03003193 p->frequency += (state->config->if_khz
Igor M. Liplianin17cce932011-01-25 17:02:00 -03003194 - stv0367cab_get_derot_freq(state, cab_state->adc_clk));
3195
3196 return 0;
3197}
3198
3199#if 0
3200void stv0367cab_GetErrorCount(state, enum stv0367cab_mod QAMSize,
3201 u32 symbol_rate, FE_367qam_Monitor *Monitor_results)
3202{
3203 stv0367cab_OptimiseNByteAndGetBER(state, QAMSize, symbol_rate, Monitor_results);
3204 stv0367cab_GetPacketsCount(state, Monitor_results);
3205
3206 return;
3207}
3208
3209static int stv0367cab_read_ber(struct dvb_frontend *fe, u32 *ber)
3210{
3211 struct stv0367_state *state = fe->demodulator_priv;
3212
3213 return 0;
3214}
3215#endif
3216static s32 stv0367cab_get_rf_lvl(struct stv0367_state *state)
3217{
3218 s32 rfLevel = 0;
3219 s32 RfAgcPwm = 0, IfAgcPwm = 0;
3220 u8 i;
3221
3222 stv0367_writebits(state, F367CAB_STDBY_ADCGP, 0x0);
3223
3224 RfAgcPwm =
3225 (stv0367_readbits(state, F367CAB_RF_AGC1_LEVEL_LO) & 0x03) +
3226 (stv0367_readbits(state, F367CAB_RF_AGC1_LEVEL_HI) << 2);
3227 RfAgcPwm = 100 * RfAgcPwm / 1023;
3228
3229 IfAgcPwm =
3230 stv0367_readbits(state, F367CAB_AGC_IF_PWMCMD_LO) +
3231 (stv0367_readbits(state, F367CAB_AGC_IF_PWMCMD_HI) << 8);
3232 if (IfAgcPwm >= 2048)
3233 IfAgcPwm -= 2048;
3234 else
3235 IfAgcPwm += 2048;
3236
3237 IfAgcPwm = 100 * IfAgcPwm / 4095;
3238
3239 /* For DTT75467 on NIM */
3240 if (RfAgcPwm < 90 && IfAgcPwm < 28) {
3241 for (i = 0; i < RF_LOOKUP_TABLE_SIZE; i++) {
3242 if (RfAgcPwm <= stv0367cab_RF_LookUp1[0][i]) {
3243 rfLevel = (-1) * stv0367cab_RF_LookUp1[1][i];
3244 break;
3245 }
3246 }
3247 if (i == RF_LOOKUP_TABLE_SIZE)
3248 rfLevel = -56;
3249 } else { /*if IF AGC>10*/
3250 for (i = 0; i < RF_LOOKUP_TABLE2_SIZE; i++) {
3251 if (IfAgcPwm <= stv0367cab_RF_LookUp2[0][i]) {
3252 rfLevel = (-1) * stv0367cab_RF_LookUp2[1][i];
3253 break;
3254 }
3255 }
3256 if (i == RF_LOOKUP_TABLE2_SIZE)
3257 rfLevel = -72;
3258 }
3259 return rfLevel;
3260}
3261
3262static int stv0367cab_read_strength(struct dvb_frontend *fe, u16 *strength)
3263{
3264 struct stv0367_state *state = fe->demodulator_priv;
3265
3266 s32 signal = stv0367cab_get_rf_lvl(state);
3267
3268 dprintk("%s: signal=%d dBm\n", __func__, signal);
3269
3270 if (signal <= -72)
3271 *strength = 65535;
3272 else
3273 *strength = (22 + signal) * (-1311);
3274
3275 dprintk("%s: strength=%d\n", __func__, (*strength));
3276
3277 return 0;
3278}
3279
3280static int stv0367cab_read_snr(struct dvb_frontend *fe, u16 *snr)
3281{
3282 struct stv0367_state *state = fe->demodulator_priv;
3283 u32 noisepercentage;
3284 enum stv0367cab_mod QAMSize;
3285 u32 regval = 0, temp = 0;
3286 int power, i;
3287
3288 QAMSize = stv0367_readbits(state, F367CAB_QAM_MODE);
3289 switch (QAMSize) {
3290 case FE_CAB_MOD_QAM4:
3291 power = 21904;
3292 break;
3293 case FE_CAB_MOD_QAM16:
3294 power = 20480;
3295 break;
3296 case FE_CAB_MOD_QAM32:
3297 power = 23040;
3298 break;
3299 case FE_CAB_MOD_QAM64:
3300 power = 21504;
3301 break;
3302 case FE_CAB_MOD_QAM128:
3303 power = 23616;
3304 break;
3305 case FE_CAB_MOD_QAM256:
3306 power = 21760;
3307 break;
3308 case FE_CAB_MOD_QAM512:
3309 power = 1;
3310 break;
3311 case FE_CAB_MOD_QAM1024:
3312 power = 21280;
3313 break;
3314 default:
3315 power = 1;
3316 break;
3317 }
3318
3319 for (i = 0; i < 10; i++) {
3320 regval += (stv0367_readbits(state, F367CAB_SNR_LO)
3321 + 256 * stv0367_readbits(state, F367CAB_SNR_HI));
3322 }
3323
3324 regval /= 10; /*for average over 10 times in for loop above*/
3325 if (regval != 0) {
3326 temp = power
3327 * (1 << (3 + stv0367_readbits(state, F367CAB_SNR_PER)));
3328 temp /= regval;
3329 }
3330
3331 /* table values, not needed to calculate logarithms */
3332 if (temp >= 5012)
3333 noisepercentage = 100;
3334 else if (temp >= 3981)
3335 noisepercentage = 93;
3336 else if (temp >= 3162)
3337 noisepercentage = 86;
3338 else if (temp >= 2512)
3339 noisepercentage = 79;
3340 else if (temp >= 1995)
3341 noisepercentage = 72;
3342 else if (temp >= 1585)
3343 noisepercentage = 65;
3344 else if (temp >= 1259)
3345 noisepercentage = 58;
3346 else if (temp >= 1000)
3347 noisepercentage = 50;
3348 else if (temp >= 794)
3349 noisepercentage = 43;
3350 else if (temp >= 501)
3351 noisepercentage = 36;
3352 else if (temp >= 316)
3353 noisepercentage = 29;
3354 else if (temp >= 200)
3355 noisepercentage = 22;
3356 else if (temp >= 158)
3357 noisepercentage = 14;
3358 else if (temp >= 126)
3359 noisepercentage = 7;
3360 else
3361 noisepercentage = 0;
3362
3363 dprintk("%s: noisepercentage=%d\n", __func__, noisepercentage);
3364
3365 *snr = (noisepercentage * 65535) / 100;
3366
3367 return 0;
3368}
3369
Abylay Ospan78db66e2011-01-02 09:12:00 -03003370static int stv0367cab_read_ucblcks(struct dvb_frontend *fe, u32 *ucblocks)
3371{
3372 struct stv0367_state *state = fe->demodulator_priv;
3373 int corrected, tscount;
3374
3375 *ucblocks = (stv0367_readreg(state, R367CAB_RS_COUNTER_5) << 8)
3376 | stv0367_readreg(state, R367CAB_RS_COUNTER_4);
3377 corrected = (stv0367_readreg(state, R367CAB_RS_COUNTER_3) << 8)
3378 | stv0367_readreg(state, R367CAB_RS_COUNTER_2);
3379 tscount = (stv0367_readreg(state, R367CAB_RS_COUNTER_2) << 8)
3380 | stv0367_readreg(state, R367CAB_RS_COUNTER_1);
3381
3382 dprintk("%s: uncorrected blocks=%d corrected blocks=%d tscount=%d\n",
3383 __func__, *ucblocks, corrected, tscount);
3384
3385 return 0;
3386};
3387
Igor M. Liplianin17cce932011-01-25 17:02:00 -03003388static struct dvb_frontend_ops stv0367cab_ops = {
Mauro Carvalho Chehab285d55a2011-12-26 13:03:00 -03003389 .delsys = { SYS_DVBC_ANNEX_A },
Igor M. Liplianin17cce932011-01-25 17:02:00 -03003390 .info = {
3391 .name = "ST STV0367 DVB-C",
Igor M. Liplianin17cce932011-01-25 17:02:00 -03003392 .frequency_min = 47000000,
3393 .frequency_max = 862000000,
3394 .frequency_stepsize = 62500,
3395 .symbol_rate_min = 870000,
3396 .symbol_rate_max = 11700000,
3397 .caps = 0x400 |/* FE_CAN_QAM_4 */
3398 FE_CAN_QAM_16 | FE_CAN_QAM_32 |
3399 FE_CAN_QAM_64 | FE_CAN_QAM_128 |
3400 FE_CAN_QAM_256 | FE_CAN_FEC_AUTO
3401 },
3402 .release = stv0367_release,
3403 .init = stv0367cab_init,
3404 .sleep = stv0367cab_sleep,
3405 .i2c_gate_ctrl = stv0367cab_gate_ctrl,
Mauro Carvalho Chehab285d55a2011-12-26 13:03:00 -03003406 .set_frontend = stv0367cab_set_frontend,
3407 .get_frontend = stv0367cab_get_frontend,
Igor M. Liplianin17cce932011-01-25 17:02:00 -03003408 .read_status = stv0367cab_read_status,
3409/* .read_ber = stv0367cab_read_ber, */
3410 .read_signal_strength = stv0367cab_read_strength,
3411 .read_snr = stv0367cab_read_snr,
Abylay Ospan78db66e2011-01-02 09:12:00 -03003412 .read_ucblocks = stv0367cab_read_ucblcks,
Igor M. Liplianin17cce932011-01-25 17:02:00 -03003413 .get_tune_settings = stv0367_get_tune_settings,
3414};
3415
3416struct dvb_frontend *stv0367cab_attach(const struct stv0367_config *config,
3417 struct i2c_adapter *i2c)
3418{
3419 struct stv0367_state *state = NULL;
3420 struct stv0367cab_state *cab_state = NULL;
3421
3422 /* allocate memory for the internal state */
3423 state = kzalloc(sizeof(struct stv0367_state), GFP_KERNEL);
3424 if (state == NULL)
3425 goto error;
3426 cab_state = kzalloc(sizeof(struct stv0367cab_state), GFP_KERNEL);
3427 if (cab_state == NULL)
3428 goto error;
3429
3430 /* setup the state */
3431 state->i2c = i2c;
3432 state->config = config;
3433 cab_state->search_range = 280000;
3434 state->cab_state = cab_state;
3435 state->fe.ops = stv0367cab_ops;
3436 state->fe.demodulator_priv = state;
3437 state->chip_id = stv0367_readreg(state, 0xf000);
3438
3439 dprintk("%s: chip_id = 0x%x\n", __func__, state->chip_id);
3440
3441 /* check if the demod is there */
3442 if ((state->chip_id != 0x50) && (state->chip_id != 0x60))
3443 goto error;
3444
3445 return &state->fe;
3446
3447error:
3448 kfree(cab_state);
3449 kfree(state);
3450 return NULL;
3451}
3452EXPORT_SYMBOL(stv0367cab_attach);
3453
3454MODULE_PARM_DESC(debug, "Set debug");
3455MODULE_PARM_DESC(i2c_debug, "Set i2c debug");
3456
3457MODULE_AUTHOR("Igor M. Liplianin");
3458MODULE_DESCRIPTION("ST STV0367 DVB-C/T demodulator driver");
3459MODULE_LICENSE("GPL");