blob: 721fbc07e9eee5bca6ea883c98af5603dd41cbf1 [file] [log] [blame]
Fred Richterb63b36f2014-03-24 19:56:00 -03001/*
2 * Support for LGDT3306A - 8VSB/QAM-B
3 *
4 * Copyright (C) 2013 Fred Richter <frichter@hauppauge.com>
5 * - driver structure based on lgdt3305.[ch] by Michael Krufky
6 * - code based on LG3306_V0.35 API by LG Electronics Inc.
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
Fred Richterb63b36f2014-03-24 19:56:00 -030017 */
18
Mauro Carvalho Chehab097117c2014-10-28 11:35:16 -020019#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
20
Fred Richterb63b36f2014-03-24 19:56:00 -030021#include <asm/div64.h>
22#include <linux/dvb/frontend.h>
23#include "dvb_math.h"
24#include "lgdt3306a.h"
25
26
27static int debug;
28module_param(debug, int, 0644);
29MODULE_PARM_DESC(debug, "set debug level (info=1, reg=2 (or-able))");
30
31#define DBG_INFO 1
32#define DBG_REG 2
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -030033#define DBG_DUMP 4 /* FGR - comment out to remove dump code */
Fred Richterb63b36f2014-03-24 19:56:00 -030034
Mauro Carvalho Chehab097117c2014-10-28 11:35:16 -020035#define lg_debug(fmt, arg...) \
36 printk(KERN_DEBUG pr_fmt(fmt), ## arg)
Fred Richterb63b36f2014-03-24 19:56:00 -030037
Mauro Carvalho Chehab097117c2014-10-28 11:35:16 -020038#define dbg_info(fmt, arg...) \
39 do { \
40 if (debug & DBG_INFO) \
41 lg_debug(fmt, ## arg); \
42 } while (0)
43
44#define dbg_reg(fmt, arg...) \
45 do { \
46 if (debug & DBG_REG) \
47 lg_debug(fmt, ## arg); \
48 } while (0)
Fred Richterb63b36f2014-03-24 19:56:00 -030049
50#define lg_chkerr(ret) \
51({ \
52 int __ret; \
53 __ret = (ret < 0); \
54 if (__ret) \
Mauro Carvalho Chehab097117c2014-10-28 11:35:16 -020055 pr_err("error %d on line %d\n", ret, __LINE__); \
Fred Richterb63b36f2014-03-24 19:56:00 -030056 __ret; \
57})
58
59struct lgdt3306a_state {
60 struct i2c_adapter *i2c_adap;
61 const struct lgdt3306a_config *cfg;
62
63 struct dvb_frontend frontend;
64
Mauro Carvalho Chehab0df289a2015-06-07 14:53:52 -030065 enum fe_modulation current_modulation;
Fred Richterb63b36f2014-03-24 19:56:00 -030066 u32 current_frequency;
67 u32 snr;
68};
69
Mauro Carvalho Chehab95f22c52014-10-28 12:40:20 -020070/*
71 * LG3306A Register Usage
72 * (LG does not really name the registers, so this code does not either)
73 *
74 * 0000 -> 00FF Common control and status
75 * 1000 -> 10FF Synchronizer control and status
76 * 1F00 -> 1FFF Smart Antenna control and status
77 * 2100 -> 21FF VSB Equalizer control and status
78 * 2800 -> 28FF QAM Equalizer control and status
79 * 3000 -> 30FF FEC control and status
80 */
Fred Richterb63b36f2014-03-24 19:56:00 -030081
Michael Ira Krufkyf883d602014-08-03 15:29:04 -030082enum lgdt3306a_lock_status {
83 LG3306_UNLOCK = 0x00,
84 LG3306_LOCK = 0x01,
Mauro Carvalho Chehab4937ba92014-10-28 10:50:36 -020085 LG3306_UNKNOWN_LOCK = 0xff
Michael Ira Krufkyf883d602014-08-03 15:29:04 -030086};
Fred Richterb63b36f2014-03-24 19:56:00 -030087
Michael Ira Krufkyf883d602014-08-03 15:29:04 -030088enum lgdt3306a_neverlock_status {
Fred Richterb63b36f2014-03-24 19:56:00 -030089 LG3306_NL_INIT = 0x00,
90 LG3306_NL_PROCESS = 0x01,
91 LG3306_NL_LOCK = 0x02,
92 LG3306_NL_FAIL = 0x03,
Mauro Carvalho Chehab4937ba92014-10-28 10:50:36 -020093 LG3306_NL_UNKNOWN = 0xff
Michael Ira Krufkyf883d602014-08-03 15:29:04 -030094};
Fred Richterb63b36f2014-03-24 19:56:00 -030095
Michael Ira Krufkyf883d602014-08-03 15:29:04 -030096enum lgdt3306a_modulation {
97 LG3306_VSB = 0x00,
98 LG3306_QAM64 = 0x01,
99 LG3306_QAM256 = 0x02,
Mauro Carvalho Chehab4937ba92014-10-28 10:50:36 -0200100 LG3306_UNKNOWN_MODE = 0xff
Michael Ira Krufkyf883d602014-08-03 15:29:04 -0300101};
Fred Richterb63b36f2014-03-24 19:56:00 -0300102
Michael Ira Krufkyf883d602014-08-03 15:29:04 -0300103enum lgdt3306a_lock_check {
Fred Richterb63b36f2014-03-24 19:56:00 -0300104 LG3306_SYNC_LOCK,
105 LG3306_FEC_LOCK,
106 LG3306_TR_LOCK,
107 LG3306_AGC_LOCK,
Michael Ira Krufkyf883d602014-08-03 15:29:04 -0300108};
Fred Richterb63b36f2014-03-24 19:56:00 -0300109
110
111#ifdef DBG_DUMP
112static void lgdt3306a_DumpAllRegs(struct lgdt3306a_state *state);
113static void lgdt3306a_DumpRegs(struct lgdt3306a_state *state);
114#endif
115
116
117static int lgdt3306a_write_reg(struct lgdt3306a_state *state, u16 reg, u8 val)
118{
119 int ret;
120 u8 buf[] = { reg >> 8, reg & 0xff, val };
121 struct i2c_msg msg = {
122 .addr = state->cfg->i2c_addr, .flags = 0,
123 .buf = buf, .len = 3,
124 };
125
Mauro Carvalho Chehab097117c2014-10-28 11:35:16 -0200126 dbg_reg("reg: 0x%04x, val: 0x%02x\n", reg, val);
Fred Richterb63b36f2014-03-24 19:56:00 -0300127
128 ret = i2c_transfer(state->i2c_adap, &msg, 1);
129
130 if (ret != 1) {
Mauro Carvalho Chehab097117c2014-10-28 11:35:16 -0200131 pr_err("error (addr %02x %02x <- %02x, err = %i)\n",
Fred Richterb63b36f2014-03-24 19:56:00 -0300132 msg.buf[0], msg.buf[1], msg.buf[2], ret);
133 if (ret < 0)
134 return ret;
135 else
136 return -EREMOTEIO;
137 }
138 return 0;
139}
140
141static int lgdt3306a_read_reg(struct lgdt3306a_state *state, u16 reg, u8 *val)
142{
143 int ret;
144 u8 reg_buf[] = { reg >> 8, reg & 0xff };
145 struct i2c_msg msg[] = {
146 { .addr = state->cfg->i2c_addr,
147 .flags = 0, .buf = reg_buf, .len = 2 },
148 { .addr = state->cfg->i2c_addr,
149 .flags = I2C_M_RD, .buf = val, .len = 1 },
150 };
151
152 ret = i2c_transfer(state->i2c_adap, msg, 2);
153
154 if (ret != 2) {
Mauro Carvalho Chehab097117c2014-10-28 11:35:16 -0200155 pr_err("error (addr %02x reg %04x error (ret == %i)\n",
Fred Richterb63b36f2014-03-24 19:56:00 -0300156 state->cfg->i2c_addr, reg, ret);
157 if (ret < 0)
158 return ret;
159 else
160 return -EREMOTEIO;
161 }
Mauro Carvalho Chehab097117c2014-10-28 11:35:16 -0200162 dbg_reg("reg: 0x%04x, val: 0x%02x\n", reg, *val);
Fred Richterb63b36f2014-03-24 19:56:00 -0300163
164 return 0;
165}
166
167#define read_reg(state, reg) \
168({ \
169 u8 __val; \
170 int ret = lgdt3306a_read_reg(state, reg, &__val); \
171 if (lg_chkerr(ret)) \
172 __val = 0; \
173 __val; \
174})
175
176static int lgdt3306a_set_reg_bit(struct lgdt3306a_state *state,
177 u16 reg, int bit, int onoff)
178{
179 u8 val;
180 int ret;
181
Mauro Carvalho Chehab097117c2014-10-28 11:35:16 -0200182 dbg_reg("reg: 0x%04x, bit: %d, level: %d\n", reg, bit, onoff);
Fred Richterb63b36f2014-03-24 19:56:00 -0300183
184 ret = lgdt3306a_read_reg(state, reg, &val);
185 if (lg_chkerr(ret))
186 goto fail;
187
188 val &= ~(1 << bit);
189 val |= (onoff & 1) << bit;
190
191 ret = lgdt3306a_write_reg(state, reg, val);
192 lg_chkerr(ret);
193fail:
194 return ret;
195}
196
197/* ------------------------------------------------------------------------ */
198
199static int lgdt3306a_soft_reset(struct lgdt3306a_state *state)
200{
201 int ret;
202
Mauro Carvalho Chehab097117c2014-10-28 11:35:16 -0200203 dbg_info("\n");
Fred Richterb63b36f2014-03-24 19:56:00 -0300204
205 ret = lgdt3306a_set_reg_bit(state, 0x0000, 7, 0);
206 if (lg_chkerr(ret))
207 goto fail;
208
209 msleep(20);
210 ret = lgdt3306a_set_reg_bit(state, 0x0000, 7, 1);
211 lg_chkerr(ret);
212
213fail:
214 return ret;
215}
216
217static int lgdt3306a_mpeg_mode(struct lgdt3306a_state *state,
218 enum lgdt3306a_mpeg_mode mode)
219{
220 u8 val;
221 int ret;
222
Mauro Carvalho Chehab097117c2014-10-28 11:35:16 -0200223 dbg_info("(%d)\n", mode);
Mauro Carvalho Chehab534f4362014-10-28 12:35:18 -0200224 /* transport packet format - TPSENB=0x80 */
225 ret = lgdt3306a_set_reg_bit(state, 0x0071, 7,
226 mode == LGDT3306A_MPEG_PARALLEL ? 1 : 0);
Fred Richterb63b36f2014-03-24 19:56:00 -0300227 if (lg_chkerr(ret))
228 goto fail;
229
Mauro Carvalho Chehab534f4362014-10-28 12:35:18 -0200230 /*
231 * start of packet signal duration
232 * TPSSOPBITEN=0x40; 0=byte duration, 1=bit duration
233 */
234 ret = lgdt3306a_set_reg_bit(state, 0x0071, 6, 0);
Fred Richterb63b36f2014-03-24 19:56:00 -0300235 if (lg_chkerr(ret))
236 goto fail;
237
238 ret = lgdt3306a_read_reg(state, 0x0070, &val);
239 if (lg_chkerr(ret))
240 goto fail;
241
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -0300242 val |= 0x10; /* TPCLKSUPB=0x10 */
Fred Richterb63b36f2014-03-24 19:56:00 -0300243
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -0300244 if (mode == LGDT3306A_MPEG_PARALLEL)
Fred Richterb63b36f2014-03-24 19:56:00 -0300245 val &= ~0x10;
246
247 ret = lgdt3306a_write_reg(state, 0x0070, val);
248 lg_chkerr(ret);
249
250fail:
251 return ret;
252}
253
254static int lgdt3306a_mpeg_mode_polarity(struct lgdt3306a_state *state,
255 enum lgdt3306a_tp_clock_edge edge,
256 enum lgdt3306a_tp_valid_polarity valid)
257{
258 u8 val;
259 int ret;
260
Mauro Carvalho Chehab097117c2014-10-28 11:35:16 -0200261 dbg_info("edge=%d, valid=%d\n", edge, valid);
Fred Richterb63b36f2014-03-24 19:56:00 -0300262
263 ret = lgdt3306a_read_reg(state, 0x0070, &val);
264 if (lg_chkerr(ret))
265 goto fail;
266
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -0300267 val &= ~0x06; /* TPCLKPOL=0x04, TPVALPOL=0x02 */
Fred Richterb63b36f2014-03-24 19:56:00 -0300268
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -0300269 if (edge == LGDT3306A_TPCLK_RISING_EDGE)
Fred Richterb63b36f2014-03-24 19:56:00 -0300270 val |= 0x04;
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -0300271 if (valid == LGDT3306A_TP_VALID_HIGH)
Fred Richterb63b36f2014-03-24 19:56:00 -0300272 val |= 0x02;
273
274 ret = lgdt3306a_write_reg(state, 0x0070, val);
275 lg_chkerr(ret);
276
277fail:
278 return ret;
279}
280
281static int lgdt3306a_mpeg_tristate(struct lgdt3306a_state *state,
282 int mode)
283{
284 u8 val;
285 int ret;
286
Mauro Carvalho Chehab097117c2014-10-28 11:35:16 -0200287 dbg_info("(%d)\n", mode);
Fred Richterb63b36f2014-03-24 19:56:00 -0300288
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -0300289 if (mode) {
Fred Richterb63b36f2014-03-24 19:56:00 -0300290 ret = lgdt3306a_read_reg(state, 0x0070, &val);
291 if (lg_chkerr(ret))
292 goto fail;
Mauro Carvalho Chehab534f4362014-10-28 12:35:18 -0200293 /*
294 * Tristate bus; TPOUTEN=0x80, TPCLKOUTEN=0x20,
295 * TPDATAOUTEN=0x08
296 */
297 val &= ~0xa8;
Fred Richterb63b36f2014-03-24 19:56:00 -0300298 ret = lgdt3306a_write_reg(state, 0x0070, val);
299 if (lg_chkerr(ret))
300 goto fail;
301
Mauro Carvalho Chehab534f4362014-10-28 12:35:18 -0200302 /* AGCIFOUTENB=0x40; 1=Disable IFAGC pin */
303 ret = lgdt3306a_set_reg_bit(state, 0x0003, 6, 1);
Fred Richterb63b36f2014-03-24 19:56:00 -0300304 if (lg_chkerr(ret))
305 goto fail;
306
307 } else {
Mauro Carvalho Chehab534f4362014-10-28 12:35:18 -0200308 /* enable IFAGC pin */
309 ret = lgdt3306a_set_reg_bit(state, 0x0003, 6, 0);
Fred Richterb63b36f2014-03-24 19:56:00 -0300310 if (lg_chkerr(ret))
311 goto fail;
312
313 ret = lgdt3306a_read_reg(state, 0x0070, &val);
314 if (lg_chkerr(ret))
315 goto fail;
316
Mauro Carvalho Chehab4937ba92014-10-28 10:50:36 -0200317 val |= 0xa8; /* enable bus */
Fred Richterb63b36f2014-03-24 19:56:00 -0300318 ret = lgdt3306a_write_reg(state, 0x0070, val);
319 if (lg_chkerr(ret))
320 goto fail;
321 }
322
323fail:
324 return ret;
325}
326
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -0300327static int lgdt3306a_ts_bus_ctrl(struct dvb_frontend *fe, int acquire)
Fred Richterb63b36f2014-03-24 19:56:00 -0300328{
329 struct lgdt3306a_state *state = fe->demodulator_priv;
330
Mauro Carvalho Chehab097117c2014-10-28 11:35:16 -0200331 dbg_info("acquire=%d\n", acquire);
Fred Richterb63b36f2014-03-24 19:56:00 -0300332
333 return lgdt3306a_mpeg_tristate(state, acquire ? 0 : 1);
334
335}
336
337static int lgdt3306a_power(struct lgdt3306a_state *state,
338 int mode)
339{
340 int ret;
341
Mauro Carvalho Chehab097117c2014-10-28 11:35:16 -0200342 dbg_info("(%d)\n", mode);
Fred Richterb63b36f2014-03-24 19:56:00 -0300343
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -0300344 if (mode == 0) {
Mauro Carvalho Chehab534f4362014-10-28 12:35:18 -0200345 /* into reset */
346 ret = lgdt3306a_set_reg_bit(state, 0x0000, 7, 0);
Fred Richterb63b36f2014-03-24 19:56:00 -0300347 if (lg_chkerr(ret))
348 goto fail;
349
Mauro Carvalho Chehab534f4362014-10-28 12:35:18 -0200350 /* power down */
351 ret = lgdt3306a_set_reg_bit(state, 0x0000, 0, 0);
Fred Richterb63b36f2014-03-24 19:56:00 -0300352 if (lg_chkerr(ret))
353 goto fail;
354
355 } else {
Mauro Carvalho Chehab534f4362014-10-28 12:35:18 -0200356 /* out of reset */
357 ret = lgdt3306a_set_reg_bit(state, 0x0000, 7, 1);
Fred Richterb63b36f2014-03-24 19:56:00 -0300358 if (lg_chkerr(ret))
359 goto fail;
360
Mauro Carvalho Chehab534f4362014-10-28 12:35:18 -0200361 /* power up */
362 ret = lgdt3306a_set_reg_bit(state, 0x0000, 0, 1);
Fred Richterb63b36f2014-03-24 19:56:00 -0300363 if (lg_chkerr(ret))
364 goto fail;
365 }
366
367#ifdef DBG_DUMP
368 lgdt3306a_DumpAllRegs(state);
369#endif
370fail:
371 return ret;
372}
373
374
375static int lgdt3306a_set_vsb(struct lgdt3306a_state *state)
376{
377 u8 val;
378 int ret;
379
Mauro Carvalho Chehab097117c2014-10-28 11:35:16 -0200380 dbg_info("\n");
Fred Richterb63b36f2014-03-24 19:56:00 -0300381
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -0300382 /* 0. Spectrum inversion detection manual; spectrum inverted */
Fred Richterb63b36f2014-03-24 19:56:00 -0300383 ret = lgdt3306a_read_reg(state, 0x0002, &val);
Mauro Carvalho Chehab4937ba92014-10-28 10:50:36 -0200384 val &= 0xf7; /* SPECINVAUTO Off */
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -0300385 val |= 0x04; /* SPECINV On */
Fred Richterb63b36f2014-03-24 19:56:00 -0300386 ret = lgdt3306a_write_reg(state, 0x0002, val);
387 if (lg_chkerr(ret))
388 goto fail;
389
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -0300390 /* 1. Selection of standard mode(0x08=QAM, 0x80=VSB) */
Fred Richterb63b36f2014-03-24 19:56:00 -0300391 ret = lgdt3306a_write_reg(state, 0x0008, 0x80);
392 if (lg_chkerr(ret))
393 goto fail;
394
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -0300395 /* 2. Bandwidth mode for VSB(6MHz) */
Fred Richterb63b36f2014-03-24 19:56:00 -0300396 ret = lgdt3306a_read_reg(state, 0x0009, &val);
Mauro Carvalho Chehab4937ba92014-10-28 10:50:36 -0200397 val &= 0xe3;
398 val |= 0x0c; /* STDOPDETTMODE[2:0]=3 */
Fred Richterb63b36f2014-03-24 19:56:00 -0300399 ret = lgdt3306a_write_reg(state, 0x0009, val);
400 if (lg_chkerr(ret))
401 goto fail;
402
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -0300403 /* 3. QAM mode detection mode(None) */
Fred Richterb63b36f2014-03-24 19:56:00 -0300404 ret = lgdt3306a_read_reg(state, 0x0009, &val);
Mauro Carvalho Chehab4937ba92014-10-28 10:50:36 -0200405 val &= 0xfc; /* STDOPDETCMODE[1:0]=0 */
Fred Richterb63b36f2014-03-24 19:56:00 -0300406 ret = lgdt3306a_write_reg(state, 0x0009, val);
407 if (lg_chkerr(ret))
408 goto fail;
409
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -0300410 /* 4. ADC sampling frequency rate(2x sampling) */
Mauro Carvalho Chehab4937ba92014-10-28 10:50:36 -0200411 ret = lgdt3306a_read_reg(state, 0x000d, &val);
412 val &= 0xbf; /* SAMPLING4XFEN=0 */
413 ret = lgdt3306a_write_reg(state, 0x000d, val);
Fred Richterb63b36f2014-03-24 19:56:00 -0300414 if (lg_chkerr(ret))
415 goto fail;
416
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -0300417#if 0
418 /* FGR - disable any AICC filtering, testing only */
419
Fred Richterb63b36f2014-03-24 19:56:00 -0300420 ret = lgdt3306a_write_reg(state, 0x0024, 0x00);
421 if (lg_chkerr(ret))
422 goto fail;
423
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -0300424 /* AICCFIXFREQ0 NT N-1(Video rejection) */
Mauro Carvalho Chehab4937ba92014-10-28 10:50:36 -0200425 ret = lgdt3306a_write_reg(state, 0x002e, 0x00);
426 ret = lgdt3306a_write_reg(state, 0x002f, 0x00);
Fred Richterb63b36f2014-03-24 19:56:00 -0300427 ret = lgdt3306a_write_reg(state, 0x0030, 0x00);
428
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -0300429 /* AICCFIXFREQ1 NT N-1(Audio rejection) */
Mauro Carvalho Chehab4937ba92014-10-28 10:50:36 -0200430 ret = lgdt3306a_write_reg(state, 0x002b, 0x00);
431 ret = lgdt3306a_write_reg(state, 0x002c, 0x00);
432 ret = lgdt3306a_write_reg(state, 0x002d, 0x00);
Fred Richterb63b36f2014-03-24 19:56:00 -0300433
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -0300434 /* AICCFIXFREQ2 NT Co-Channel(Video rejection) */
Fred Richterb63b36f2014-03-24 19:56:00 -0300435 ret = lgdt3306a_write_reg(state, 0x0028, 0x00);
436 ret = lgdt3306a_write_reg(state, 0x0029, 0x00);
Mauro Carvalho Chehab4937ba92014-10-28 10:50:36 -0200437 ret = lgdt3306a_write_reg(state, 0x002a, 0x00);
Fred Richterb63b36f2014-03-24 19:56:00 -0300438
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -0300439 /* AICCFIXFREQ3 NT Co-Channel(Audio rejection) */
Fred Richterb63b36f2014-03-24 19:56:00 -0300440 ret = lgdt3306a_write_reg(state, 0x0025, 0x00);
441 ret = lgdt3306a_write_reg(state, 0x0026, 0x00);
442 ret = lgdt3306a_write_reg(state, 0x0027, 0x00);
443
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -0300444#else
445 /* FGR - this works well for HVR-1955,1975 */
446
447 /* 5. AICCOPMODE NT N-1 Adj. */
Fred Richterb63b36f2014-03-24 19:56:00 -0300448 ret = lgdt3306a_write_reg(state, 0x0024, 0x5A);
449 if (lg_chkerr(ret))
450 goto fail;
451
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -0300452 /* AICCFIXFREQ0 NT N-1(Video rejection) */
Mauro Carvalho Chehab4937ba92014-10-28 10:50:36 -0200453 ret = lgdt3306a_write_reg(state, 0x002e, 0x5A);
454 ret = lgdt3306a_write_reg(state, 0x002f, 0x00);
Fred Richterb63b36f2014-03-24 19:56:00 -0300455 ret = lgdt3306a_write_reg(state, 0x0030, 0x00);
456
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -0300457 /* AICCFIXFREQ1 NT N-1(Audio rejection) */
Mauro Carvalho Chehab4937ba92014-10-28 10:50:36 -0200458 ret = lgdt3306a_write_reg(state, 0x002b, 0x36);
459 ret = lgdt3306a_write_reg(state, 0x002c, 0x00);
460 ret = lgdt3306a_write_reg(state, 0x002d, 0x00);
Fred Richterb63b36f2014-03-24 19:56:00 -0300461
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -0300462 /* AICCFIXFREQ2 NT Co-Channel(Video rejection) */
Fred Richterb63b36f2014-03-24 19:56:00 -0300463 ret = lgdt3306a_write_reg(state, 0x0028, 0x2A);
464 ret = lgdt3306a_write_reg(state, 0x0029, 0x00);
Mauro Carvalho Chehab4937ba92014-10-28 10:50:36 -0200465 ret = lgdt3306a_write_reg(state, 0x002a, 0x00);
Fred Richterb63b36f2014-03-24 19:56:00 -0300466
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -0300467 /* AICCFIXFREQ3 NT Co-Channel(Audio rejection) */
Fred Richterb63b36f2014-03-24 19:56:00 -0300468 ret = lgdt3306a_write_reg(state, 0x0025, 0x06);
469 ret = lgdt3306a_write_reg(state, 0x0026, 0x00);
470 ret = lgdt3306a_write_reg(state, 0x0027, 0x00);
471#endif
472
Mauro Carvalho Chehab4937ba92014-10-28 10:50:36 -0200473 ret = lgdt3306a_read_reg(state, 0x001e, &val);
474 val &= 0x0f;
475 val |= 0xa0;
476 ret = lgdt3306a_write_reg(state, 0x001e, val);
Fred Richterb63b36f2014-03-24 19:56:00 -0300477
478 ret = lgdt3306a_write_reg(state, 0x0022, 0x08);
479
480 ret = lgdt3306a_write_reg(state, 0x0023, 0xFF);
481
Mauro Carvalho Chehab4937ba92014-10-28 10:50:36 -0200482 ret = lgdt3306a_read_reg(state, 0x211f, &val);
483 val &= 0xef;
484 ret = lgdt3306a_write_reg(state, 0x211f, val);
Fred Richterb63b36f2014-03-24 19:56:00 -0300485
486 ret = lgdt3306a_write_reg(state, 0x2173, 0x01);
487
488 ret = lgdt3306a_read_reg(state, 0x1061, &val);
Mauro Carvalho Chehab4937ba92014-10-28 10:50:36 -0200489 val &= 0xf8;
Fred Richterb63b36f2014-03-24 19:56:00 -0300490 val |= 0x04;
491 ret = lgdt3306a_write_reg(state, 0x1061, val);
492
Mauro Carvalho Chehab4937ba92014-10-28 10:50:36 -0200493 ret = lgdt3306a_read_reg(state, 0x103d, &val);
494 val &= 0xcf;
495 ret = lgdt3306a_write_reg(state, 0x103d, val);
Fred Richterb63b36f2014-03-24 19:56:00 -0300496
497 ret = lgdt3306a_write_reg(state, 0x2122, 0x40);
498
499 ret = lgdt3306a_read_reg(state, 0x2141, &val);
Mauro Carvalho Chehab4937ba92014-10-28 10:50:36 -0200500 val &= 0x3f;
Fred Richterb63b36f2014-03-24 19:56:00 -0300501 ret = lgdt3306a_write_reg(state, 0x2141, val);
502
503 ret = lgdt3306a_read_reg(state, 0x2135, &val);
Mauro Carvalho Chehab4937ba92014-10-28 10:50:36 -0200504 val &= 0x0f;
Fred Richterb63b36f2014-03-24 19:56:00 -0300505 val |= 0x70;
506 ret = lgdt3306a_write_reg(state, 0x2135, val);
507
508 ret = lgdt3306a_read_reg(state, 0x0003, &val);
Mauro Carvalho Chehab4937ba92014-10-28 10:50:36 -0200509 val &= 0xf7;
Fred Richterb63b36f2014-03-24 19:56:00 -0300510 ret = lgdt3306a_write_reg(state, 0x0003, val);
511
Mauro Carvalho Chehab4937ba92014-10-28 10:50:36 -0200512 ret = lgdt3306a_read_reg(state, 0x001c, &val);
513 val &= 0x7f;
514 ret = lgdt3306a_write_reg(state, 0x001c, val);
Fred Richterb63b36f2014-03-24 19:56:00 -0300515
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -0300516 /* 6. EQ step size */
Fred Richterb63b36f2014-03-24 19:56:00 -0300517 ret = lgdt3306a_read_reg(state, 0x2179, &val);
Mauro Carvalho Chehab4937ba92014-10-28 10:50:36 -0200518 val &= 0xf8;
Fred Richterb63b36f2014-03-24 19:56:00 -0300519 ret = lgdt3306a_write_reg(state, 0x2179, val);
520
Mauro Carvalho Chehab4937ba92014-10-28 10:50:36 -0200521 ret = lgdt3306a_read_reg(state, 0x217a, &val);
522 val &= 0xf8;
523 ret = lgdt3306a_write_reg(state, 0x217a, val);
Fred Richterb63b36f2014-03-24 19:56:00 -0300524
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -0300525 /* 7. Reset */
Fred Richterb63b36f2014-03-24 19:56:00 -0300526 ret = lgdt3306a_soft_reset(state);
527 if (lg_chkerr(ret))
528 goto fail;
529
Mauro Carvalho Chehab097117c2014-10-28 11:35:16 -0200530 dbg_info("complete\n");
Fred Richterb63b36f2014-03-24 19:56:00 -0300531fail:
532 return ret;
533}
534
535static int lgdt3306a_set_qam(struct lgdt3306a_state *state, int modulation)
536{
537 u8 val;
538 int ret;
539
Mauro Carvalho Chehab097117c2014-10-28 11:35:16 -0200540 dbg_info("modulation=%d\n", modulation);
Fred Richterb63b36f2014-03-24 19:56:00 -0300541
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -0300542 /* 1. Selection of standard mode(0x08=QAM, 0x80=VSB) */
Fred Richterb63b36f2014-03-24 19:56:00 -0300543 ret = lgdt3306a_write_reg(state, 0x0008, 0x08);
544 if (lg_chkerr(ret))
545 goto fail;
546
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -0300547 /* 1a. Spectrum inversion detection to Auto */
Fred Richterb63b36f2014-03-24 19:56:00 -0300548 ret = lgdt3306a_read_reg(state, 0x0002, &val);
Mauro Carvalho Chehab4937ba92014-10-28 10:50:36 -0200549 val &= 0xfb; /* SPECINV Off */
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -0300550 val |= 0x08; /* SPECINVAUTO On */
Fred Richterb63b36f2014-03-24 19:56:00 -0300551 ret = lgdt3306a_write_reg(state, 0x0002, val);
552 if (lg_chkerr(ret))
553 goto fail;
554
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -0300555 /* 2. Bandwidth mode for QAM */
Fred Richterb63b36f2014-03-24 19:56:00 -0300556 ret = lgdt3306a_read_reg(state, 0x0009, &val);
Mauro Carvalho Chehab4937ba92014-10-28 10:50:36 -0200557 val &= 0xe3; /* STDOPDETTMODE[2:0]=0 VSB Off */
Fred Richterb63b36f2014-03-24 19:56:00 -0300558 ret = lgdt3306a_write_reg(state, 0x0009, val);
559 if (lg_chkerr(ret))
560 goto fail;
561
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -0300562 /* 3. : 64QAM/256QAM detection(manual, auto) */
Fred Richterb63b36f2014-03-24 19:56:00 -0300563 ret = lgdt3306a_read_reg(state, 0x0009, &val);
Mauro Carvalho Chehab4937ba92014-10-28 10:50:36 -0200564 val &= 0xfc;
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -0300565 val |= 0x02; /* STDOPDETCMODE[1:0]=1=Manual 2=Auto */
Fred Richterb63b36f2014-03-24 19:56:00 -0300566 ret = lgdt3306a_write_reg(state, 0x0009, val);
567 if (lg_chkerr(ret))
568 goto fail;
569
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -0300570 /* 3a. : 64QAM/256QAM selection for manual */
Fred Richterb63b36f2014-03-24 19:56:00 -0300571 ret = lgdt3306a_read_reg(state, 0x101a, &val);
Mauro Carvalho Chehab4937ba92014-10-28 10:50:36 -0200572 val &= 0xf8;
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -0300573 if (modulation == QAM_64)
574 val |= 0x02; /* QMDQMODE[2:0]=2=QAM64 */
575 else
576 val |= 0x04; /* QMDQMODE[2:0]=4=QAM256 */
577
Fred Richterb63b36f2014-03-24 19:56:00 -0300578 ret = lgdt3306a_write_reg(state, 0x101a, val);
579 if (lg_chkerr(ret))
580 goto fail;
581
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -0300582 /* 4. ADC sampling frequency rate(4x sampling) */
Mauro Carvalho Chehab4937ba92014-10-28 10:50:36 -0200583 ret = lgdt3306a_read_reg(state, 0x000d, &val);
584 val &= 0xbf;
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -0300585 val |= 0x40; /* SAMPLING4XFEN=1 */
Mauro Carvalho Chehab4937ba92014-10-28 10:50:36 -0200586 ret = lgdt3306a_write_reg(state, 0x000d, val);
Fred Richterb63b36f2014-03-24 19:56:00 -0300587 if (lg_chkerr(ret))
588 goto fail;
589
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -0300590 /* 5. No AICC operation in QAM mode */
Fred Richterb63b36f2014-03-24 19:56:00 -0300591 ret = lgdt3306a_read_reg(state, 0x0024, &val);
592 val &= 0x00;
593 ret = lgdt3306a_write_reg(state, 0x0024, val);
594 if (lg_chkerr(ret))
595 goto fail;
596
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -0300597 /* 6. Reset */
Fred Richterb63b36f2014-03-24 19:56:00 -0300598 ret = lgdt3306a_soft_reset(state);
599 if (lg_chkerr(ret))
600 goto fail;
601
Mauro Carvalho Chehab097117c2014-10-28 11:35:16 -0200602 dbg_info("complete\n");
Fred Richterb63b36f2014-03-24 19:56:00 -0300603fail:
604 return ret;
605}
606
607static int lgdt3306a_set_modulation(struct lgdt3306a_state *state,
608 struct dtv_frontend_properties *p)
609{
610 int ret;
611
Mauro Carvalho Chehab097117c2014-10-28 11:35:16 -0200612 dbg_info("\n");
Fred Richterb63b36f2014-03-24 19:56:00 -0300613
614 switch (p->modulation) {
615 case VSB_8:
616 ret = lgdt3306a_set_vsb(state);
617 break;
618 case QAM_64:
619 ret = lgdt3306a_set_qam(state, QAM_64);
620 break;
621 case QAM_256:
622 ret = lgdt3306a_set_qam(state, QAM_256);
623 break;
624 default:
625 return -EINVAL;
626 }
627 if (lg_chkerr(ret))
628 goto fail;
629
630 state->current_modulation = p->modulation;
631
632fail:
633 return ret;
634}
635
636/* ------------------------------------------------------------------------ */
637
638static int lgdt3306a_agc_setup(struct lgdt3306a_state *state,
639 struct dtv_frontend_properties *p)
640{
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -0300641 /* TODO: anything we want to do here??? */
Mauro Carvalho Chehab097117c2014-10-28 11:35:16 -0200642 dbg_info("\n");
Fred Richterb63b36f2014-03-24 19:56:00 -0300643
644 switch (p->modulation) {
645 case VSB_8:
646 break;
647 case QAM_64:
648 case QAM_256:
649 break;
650 default:
651 return -EINVAL;
652 }
653 return 0;
654}
655
656/* ------------------------------------------------------------------------ */
657
658static int lgdt3306a_set_inversion(struct lgdt3306a_state *state,
659 int inversion)
660{
661 int ret;
662
Mauro Carvalho Chehab097117c2014-10-28 11:35:16 -0200663 dbg_info("(%d)\n", inversion);
Fred Richterb63b36f2014-03-24 19:56:00 -0300664
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -0300665 ret = lgdt3306a_set_reg_bit(state, 0x0002, 2, inversion ? 1 : 0);
Fred Richterb63b36f2014-03-24 19:56:00 -0300666 return ret;
667}
668
669static int lgdt3306a_set_inversion_auto(struct lgdt3306a_state *state,
670 int enabled)
671{
672 int ret;
673
Mauro Carvalho Chehab097117c2014-10-28 11:35:16 -0200674 dbg_info("(%d)\n", enabled);
Fred Richterb63b36f2014-03-24 19:56:00 -0300675
Mauro Carvalho Chehab534f4362014-10-28 12:35:18 -0200676 /* 0=Manual 1=Auto(QAM only) - SPECINVAUTO=0x04 */
677 ret = lgdt3306a_set_reg_bit(state, 0x0002, 3, enabled);
Fred Richterb63b36f2014-03-24 19:56:00 -0300678 return ret;
679}
680
681static int lgdt3306a_spectral_inversion(struct lgdt3306a_state *state,
682 struct dtv_frontend_properties *p,
683 int inversion)
684{
685 int ret = 0;
686
Mauro Carvalho Chehab097117c2014-10-28 11:35:16 -0200687 dbg_info("(%d)\n", inversion);
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -0300688#if 0
Mauro Carvalho Chehab534f4362014-10-28 12:35:18 -0200689 /*
690 * FGR - spectral_inversion defaults already set for VSB and QAM;
691 * can enable later if desired
692 */
Fred Richterb63b36f2014-03-24 19:56:00 -0300693
694 ret = lgdt3306a_set_inversion(state, inversion);
695
696 switch (p->modulation) {
697 case VSB_8:
Mauro Carvalho Chehab534f4362014-10-28 12:35:18 -0200698 /* Manual only for VSB */
699 ret = lgdt3306a_set_inversion_auto(state, 0);
Fred Richterb63b36f2014-03-24 19:56:00 -0300700 break;
701 case QAM_64:
702 case QAM_256:
Mauro Carvalho Chehab534f4362014-10-28 12:35:18 -0200703 /* Auto ok for QAM */
704 ret = lgdt3306a_set_inversion_auto(state, 1);
Fred Richterb63b36f2014-03-24 19:56:00 -0300705 break;
706 default:
707 ret = -EINVAL;
708 }
709#endif
710 return ret;
711}
712
713static int lgdt3306a_set_if(struct lgdt3306a_state *state,
714 struct dtv_frontend_properties *p)
715{
716 int ret;
717 u16 if_freq_khz;
718 u8 nco1, nco2;
719
720 switch (p->modulation) {
721 case VSB_8:
722 if_freq_khz = state->cfg->vsb_if_khz;
723 break;
724 case QAM_64:
725 case QAM_256:
726 if_freq_khz = state->cfg->qam_if_khz;
727 break;
728 default:
729 return -EINVAL;
730 }
731
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -0300732 switch (if_freq_khz) {
Fred Richterb63b36f2014-03-24 19:56:00 -0300733 default:
Mauro Carvalho Chehab534f4362014-10-28 12:35:18 -0200734 pr_warn("IF=%d KHz is not supportted, 3250 assumed\n",
735 if_freq_khz);
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -0300736 /* fallthrough */
Michael Ira Krufky34a5a2f2014-10-25 11:26:15 -0300737 case 3250: /* 3.25Mhz */
Fred Richterb63b36f2014-03-24 19:56:00 -0300738 nco1 = 0x34;
739 nco2 = 0x00;
740 break;
Michael Ira Krufky34a5a2f2014-10-25 11:26:15 -0300741 case 3500: /* 3.50Mhz */
Fred Richterb63b36f2014-03-24 19:56:00 -0300742 nco1 = 0x38;
743 nco2 = 0x00;
744 break;
Michael Ira Krufky34a5a2f2014-10-25 11:26:15 -0300745 case 4000: /* 4.00Mhz */
Fred Richterb63b36f2014-03-24 19:56:00 -0300746 nco1 = 0x40;
747 nco2 = 0x00;
748 break;
Michael Ira Krufky34a5a2f2014-10-25 11:26:15 -0300749 case 5000: /* 5.00Mhz */
Fred Richterb63b36f2014-03-24 19:56:00 -0300750 nco1 = 0x50;
751 nco2 = 0x00;
752 break;
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -0300753 case 5380: /* 5.38Mhz */
Fred Richterb63b36f2014-03-24 19:56:00 -0300754 nco1 = 0x56;
755 nco2 = 0x14;
756 break;
757 }
758 ret = lgdt3306a_write_reg(state, 0x0010, nco1);
Mauro Carvalho Chehabee0133e2014-10-28 11:21:48 -0200759 if (ret)
760 return ret;
Fred Richterb63b36f2014-03-24 19:56:00 -0300761 ret = lgdt3306a_write_reg(state, 0x0011, nco2);
Mauro Carvalho Chehabee0133e2014-10-28 11:21:48 -0200762 if (ret)
763 return ret;
Fred Richterb63b36f2014-03-24 19:56:00 -0300764
Mauro Carvalho Chehab097117c2014-10-28 11:35:16 -0200765 dbg_info("if_freq=%d KHz->[%04x]\n", if_freq_khz, nco1<<8 | nco2);
Fred Richterb63b36f2014-03-24 19:56:00 -0300766
767 return 0;
768}
769
770/* ------------------------------------------------------------------------ */
771
772static int lgdt3306a_i2c_gate_ctrl(struct dvb_frontend *fe, int enable)
773{
774 struct lgdt3306a_state *state = fe->demodulator_priv;
775
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -0300776 if (state->cfg->deny_i2c_rptr) {
Mauro Carvalho Chehab097117c2014-10-28 11:35:16 -0200777 dbg_info("deny_i2c_rptr=%d\n", state->cfg->deny_i2c_rptr);
Fred Richterb63b36f2014-03-24 19:56:00 -0300778 return 0;
779 }
Mauro Carvalho Chehab097117c2014-10-28 11:35:16 -0200780 dbg_info("(%d)\n", enable);
Fred Richterb63b36f2014-03-24 19:56:00 -0300781
Mauro Carvalho Chehab534f4362014-10-28 12:35:18 -0200782 /* NI2CRPTEN=0x80 */
783 return lgdt3306a_set_reg_bit(state, 0x0002, 7, enable ? 0 : 1);
Fred Richterb63b36f2014-03-24 19:56:00 -0300784}
785
786static int lgdt3306a_sleep(struct lgdt3306a_state *state)
787{
788 int ret;
789
Mauro Carvalho Chehab097117c2014-10-28 11:35:16 -0200790 dbg_info("\n");
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -0300791 state->current_frequency = -1; /* force re-tune, when we wake */
Fred Richterb63b36f2014-03-24 19:56:00 -0300792
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -0300793 ret = lgdt3306a_mpeg_tristate(state, 1); /* disable data bus */
Fred Richterb63b36f2014-03-24 19:56:00 -0300794 if (lg_chkerr(ret))
795 goto fail;
796
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -0300797 ret = lgdt3306a_power(state, 0); /* power down */
Fred Richterb63b36f2014-03-24 19:56:00 -0300798 lg_chkerr(ret);
799
800fail:
801 return 0;
802}
803
804static int lgdt3306a_fe_sleep(struct dvb_frontend *fe)
805{
806 struct lgdt3306a_state *state = fe->demodulator_priv;
807
808 return lgdt3306a_sleep(state);
809}
810
811static int lgdt3306a_init(struct dvb_frontend *fe)
812{
813 struct lgdt3306a_state *state = fe->demodulator_priv;
814 u8 val;
815 int ret;
816
Mauro Carvalho Chehab097117c2014-10-28 11:35:16 -0200817 dbg_info("\n");
Fred Richterb63b36f2014-03-24 19:56:00 -0300818
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -0300819 /* 1. Normal operation mode */
820 ret = lgdt3306a_set_reg_bit(state, 0x0001, 0, 1); /* SIMFASTENB=0x01 */
Fred Richterb63b36f2014-03-24 19:56:00 -0300821 if (lg_chkerr(ret))
822 goto fail;
823
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -0300824 /* 2. Spectrum inversion auto detection (Not valid for VSB) */
Fred Richterb63b36f2014-03-24 19:56:00 -0300825 ret = lgdt3306a_set_inversion_auto(state, 0);
826 if (lg_chkerr(ret))
827 goto fail;
828
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -0300829 /* 3. Spectrum inversion(According to the tuner configuration) */
Fred Richterb63b36f2014-03-24 19:56:00 -0300830 ret = lgdt3306a_set_inversion(state, 1);
831 if (lg_chkerr(ret))
832 goto fail;
833
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -0300834 /* 4. Peak-to-peak voltage of ADC input signal */
Mauro Carvalho Chehab534f4362014-10-28 12:35:18 -0200835
836 /* ADCSEL1V=0x80=1Vpp; 0x00=2Vpp */
837 ret = lgdt3306a_set_reg_bit(state, 0x0004, 7, 1);
Fred Richterb63b36f2014-03-24 19:56:00 -0300838 if (lg_chkerr(ret))
839 goto fail;
840
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -0300841 /* 5. ADC output data capture clock phase */
Mauro Carvalho Chehab534f4362014-10-28 12:35:18 -0200842
843 /* 0=same phase as ADC clock */
844 ret = lgdt3306a_set_reg_bit(state, 0x0004, 2, 0);
Fred Richterb63b36f2014-03-24 19:56:00 -0300845 if (lg_chkerr(ret))
846 goto fail;
847
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -0300848 /* 5a. ADC sampling clock source */
Mauro Carvalho Chehab534f4362014-10-28 12:35:18 -0200849
850 /* ADCCLKPLLSEL=0x08; 0=use ext clock, not PLL */
851 ret = lgdt3306a_set_reg_bit(state, 0x0004, 3, 0);
Fred Richterb63b36f2014-03-24 19:56:00 -0300852 if (lg_chkerr(ret))
853 goto fail;
854
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -0300855 /* 6. Automatic PLL set */
Mauro Carvalho Chehab534f4362014-10-28 12:35:18 -0200856
857 /* PLLSETAUTO=0x40; 0=off */
858 ret = lgdt3306a_set_reg_bit(state, 0x0005, 6, 0);
Fred Richterb63b36f2014-03-24 19:56:00 -0300859 if (lg_chkerr(ret))
860 goto fail;
861
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -0300862 if (state->cfg->xtalMHz == 24) { /* 24MHz */
863 /* 7. Frequency for PLL output(0x2564 for 192MHz for 24MHz) */
Fred Richterb63b36f2014-03-24 19:56:00 -0300864 ret = lgdt3306a_read_reg(state, 0x0005, &val);
865 if (lg_chkerr(ret))
866 goto fail;
Mauro Carvalho Chehab4937ba92014-10-28 10:50:36 -0200867 val &= 0xc0;
Fred Richterb63b36f2014-03-24 19:56:00 -0300868 val |= 0x25;
869 ret = lgdt3306a_write_reg(state, 0x0005, val);
870 if (lg_chkerr(ret))
871 goto fail;
872 ret = lgdt3306a_write_reg(state, 0x0006, 0x64);
873 if (lg_chkerr(ret))
874 goto fail;
875
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -0300876 /* 8. ADC sampling frequency(0x180000 for 24MHz sampling) */
Mauro Carvalho Chehab4937ba92014-10-28 10:50:36 -0200877 ret = lgdt3306a_read_reg(state, 0x000d, &val);
Fred Richterb63b36f2014-03-24 19:56:00 -0300878 if (lg_chkerr(ret))
879 goto fail;
Mauro Carvalho Chehab4937ba92014-10-28 10:50:36 -0200880 val &= 0xc0;
Fred Richterb63b36f2014-03-24 19:56:00 -0300881 val |= 0x18;
Mauro Carvalho Chehab4937ba92014-10-28 10:50:36 -0200882 ret = lgdt3306a_write_reg(state, 0x000d, val);
Fred Richterb63b36f2014-03-24 19:56:00 -0300883 if (lg_chkerr(ret))
884 goto fail;
885
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -0300886 } else if (state->cfg->xtalMHz == 25) { /* 25MHz */
887 /* 7. Frequency for PLL output */
Fred Richterb63b36f2014-03-24 19:56:00 -0300888 ret = lgdt3306a_read_reg(state, 0x0005, &val);
889 if (lg_chkerr(ret))
890 goto fail;
Mauro Carvalho Chehab4937ba92014-10-28 10:50:36 -0200891 val &= 0xc0;
Fred Richterb63b36f2014-03-24 19:56:00 -0300892 val |= 0x25;
893 ret = lgdt3306a_write_reg(state, 0x0005, val);
894 if (lg_chkerr(ret))
895 goto fail;
896 ret = lgdt3306a_write_reg(state, 0x0006, 0x64);
897 if (lg_chkerr(ret))
898 goto fail;
899
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -0300900 /* 8. ADC sampling frequency(0x190000 for 25MHz sampling) */
Mauro Carvalho Chehab4937ba92014-10-28 10:50:36 -0200901 ret = lgdt3306a_read_reg(state, 0x000d, &val);
Fred Richterb63b36f2014-03-24 19:56:00 -0300902 if (lg_chkerr(ret))
903 goto fail;
Mauro Carvalho Chehab4937ba92014-10-28 10:50:36 -0200904 val &= 0xc0;
Fred Richterb63b36f2014-03-24 19:56:00 -0300905 val |= 0x19;
Mauro Carvalho Chehab4937ba92014-10-28 10:50:36 -0200906 ret = lgdt3306a_write_reg(state, 0x000d, val);
Fred Richterb63b36f2014-03-24 19:56:00 -0300907 if (lg_chkerr(ret))
908 goto fail;
909 } else {
Mauro Carvalho Chehab097117c2014-10-28 11:35:16 -0200910 pr_err("Bad xtalMHz=%d\n", state->cfg->xtalMHz);
Fred Richterb63b36f2014-03-24 19:56:00 -0300911 }
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -0300912#if 0
Mauro Carvalho Chehab4937ba92014-10-28 10:50:36 -0200913 ret = lgdt3306a_write_reg(state, 0x000e, 0x00);
914 ret = lgdt3306a_write_reg(state, 0x000f, 0x00);
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -0300915#endif
Fred Richterb63b36f2014-03-24 19:56:00 -0300916
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -0300917 /* 9. Center frequency of input signal of ADC */
918 ret = lgdt3306a_write_reg(state, 0x0010, 0x34); /* 3.25MHz */
919 ret = lgdt3306a_write_reg(state, 0x0011, 0x00);
Fred Richterb63b36f2014-03-24 19:56:00 -0300920
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -0300921 /* 10. Fixed gain error value */
922 ret = lgdt3306a_write_reg(state, 0x0014, 0); /* gain error=0 */
Fred Richterb63b36f2014-03-24 19:56:00 -0300923
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -0300924 /* 10a. VSB TR BW gear shift initial step */
Mauro Carvalho Chehab4937ba92014-10-28 10:50:36 -0200925 ret = lgdt3306a_read_reg(state, 0x103c, &val);
926 val &= 0x0f;
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -0300927 val |= 0x20; /* SAMGSAUTOSTL_V[3:0] = 2 */
Mauro Carvalho Chehab4937ba92014-10-28 10:50:36 -0200928 ret = lgdt3306a_write_reg(state, 0x103c, val);
Fred Richterb63b36f2014-03-24 19:56:00 -0300929
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -0300930 /* 10b. Timing offset calibration in low temperature for VSB */
Mauro Carvalho Chehab4937ba92014-10-28 10:50:36 -0200931 ret = lgdt3306a_read_reg(state, 0x103d, &val);
932 val &= 0xfc;
Fred Richterb63b36f2014-03-24 19:56:00 -0300933 val |= 0x03;
Mauro Carvalho Chehab4937ba92014-10-28 10:50:36 -0200934 ret = lgdt3306a_write_reg(state, 0x103d, val);
Fred Richterb63b36f2014-03-24 19:56:00 -0300935
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -0300936 /* 10c. Timing offset calibration in low temperature for QAM */
Fred Richterb63b36f2014-03-24 19:56:00 -0300937 ret = lgdt3306a_read_reg(state, 0x1036, &val);
Mauro Carvalho Chehab4937ba92014-10-28 10:50:36 -0200938 val &= 0xf0;
939 val |= 0x0c;
Fred Richterb63b36f2014-03-24 19:56:00 -0300940 ret = lgdt3306a_write_reg(state, 0x1036, val);
941
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -0300942 /* 11. Using the imaginary part of CIR in CIR loading */
Mauro Carvalho Chehab4937ba92014-10-28 10:50:36 -0200943 ret = lgdt3306a_read_reg(state, 0x211f, &val);
944 val &= 0xef; /* do not use imaginary of CIR */
945 ret = lgdt3306a_write_reg(state, 0x211f, val);
Fred Richterb63b36f2014-03-24 19:56:00 -0300946
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -0300947 /* 12. Control of no signal detector function */
Fred Richterb63b36f2014-03-24 19:56:00 -0300948 ret = lgdt3306a_read_reg(state, 0x2849, &val);
Mauro Carvalho Chehab4937ba92014-10-28 10:50:36 -0200949 val &= 0xef; /* NOUSENOSIGDET=0, enable no signal detector */
Fred Richterb63b36f2014-03-24 19:56:00 -0300950 ret = lgdt3306a_write_reg(state, 0x2849, val);
951
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -0300952 /* FGR - put demod in some known mode */
Fred Richterb63b36f2014-03-24 19:56:00 -0300953 ret = lgdt3306a_set_vsb(state);
954
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -0300955 /* 13. TP stream format */
Fred Richterb63b36f2014-03-24 19:56:00 -0300956 ret = lgdt3306a_mpeg_mode(state, state->cfg->mpeg_mode);
957
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -0300958 /* 14. disable output buses */
Fred Richterb63b36f2014-03-24 19:56:00 -0300959 ret = lgdt3306a_mpeg_tristate(state, 1);
960
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -0300961 /* 15. Sleep (in reset) */
Fred Richterb63b36f2014-03-24 19:56:00 -0300962 ret = lgdt3306a_sleep(state);
963 lg_chkerr(ret);
964
965fail:
966 return ret;
967}
968
969static int lgdt3306a_set_parameters(struct dvb_frontend *fe)
970{
971 struct dtv_frontend_properties *p = &fe->dtv_property_cache;
972 struct lgdt3306a_state *state = fe->demodulator_priv;
973 int ret;
974
Mauro Carvalho Chehab097117c2014-10-28 11:35:16 -0200975 dbg_info("(%d, %d)\n", p->frequency, p->modulation);
Fred Richterb63b36f2014-03-24 19:56:00 -0300976
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -0300977 if (state->current_frequency == p->frequency &&
978 state->current_modulation == p->modulation) {
Mauro Carvalho Chehab097117c2014-10-28 11:35:16 -0200979 dbg_info(" (already set, skipping ...)\n");
Fred Richterb63b36f2014-03-24 19:56:00 -0300980 return 0;
981 }
982 state->current_frequency = -1;
983 state->current_modulation = -1;
984
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -0300985 ret = lgdt3306a_power(state, 1); /* power up */
Fred Richterb63b36f2014-03-24 19:56:00 -0300986 if (lg_chkerr(ret))
987 goto fail;
988
989 if (fe->ops.tuner_ops.set_params) {
990 ret = fe->ops.tuner_ops.set_params(fe);
991 if (fe->ops.i2c_gate_ctrl)
992 fe->ops.i2c_gate_ctrl(fe, 0);
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -0300993#if 0
994 if (lg_chkerr(ret))
995 goto fail;
996 state->current_frequency = p->frequency;
997#endif
Fred Richterb63b36f2014-03-24 19:56:00 -0300998 }
999
1000 ret = lgdt3306a_set_modulation(state, p);
1001 if (lg_chkerr(ret))
1002 goto fail;
1003
1004 ret = lgdt3306a_agc_setup(state, p);
1005 if (lg_chkerr(ret))
1006 goto fail;
1007
1008 ret = lgdt3306a_set_if(state, p);
1009 if (lg_chkerr(ret))
1010 goto fail;
1011
1012 ret = lgdt3306a_spectral_inversion(state, p,
Mauro Carvalho Chehab534f4362014-10-28 12:35:18 -02001013 state->cfg->spectral_inversion ? 1 : 0);
Fred Richterb63b36f2014-03-24 19:56:00 -03001014 if (lg_chkerr(ret))
1015 goto fail;
1016
1017 ret = lgdt3306a_mpeg_mode(state, state->cfg->mpeg_mode);
1018 if (lg_chkerr(ret))
1019 goto fail;
1020
1021 ret = lgdt3306a_mpeg_mode_polarity(state,
1022 state->cfg->tpclk_edge,
1023 state->cfg->tpvalid_polarity);
1024 if (lg_chkerr(ret))
1025 goto fail;
1026
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -03001027 ret = lgdt3306a_mpeg_tristate(state, 0); /* enable data bus */
Fred Richterb63b36f2014-03-24 19:56:00 -03001028 if (lg_chkerr(ret))
1029 goto fail;
1030
1031 ret = lgdt3306a_soft_reset(state);
1032 if (lg_chkerr(ret))
1033 goto fail;
1034
1035#ifdef DBG_DUMP
1036 lgdt3306a_DumpAllRegs(state);
1037#endif
1038 state->current_frequency = p->frequency;
1039fail:
1040 return ret;
1041}
1042
1043static int lgdt3306a_get_frontend(struct dvb_frontend *fe)
1044{
1045 struct lgdt3306a_state *state = fe->demodulator_priv;
1046 struct dtv_frontend_properties *p = &fe->dtv_property_cache;
1047
Mauro Carvalho Chehab534f4362014-10-28 12:35:18 -02001048 dbg_info("(%u, %d)\n",
1049 state->current_frequency, state->current_modulation);
Fred Richterb63b36f2014-03-24 19:56:00 -03001050
1051 p->modulation = state->current_modulation;
1052 p->frequency = state->current_frequency;
1053 return 0;
1054}
1055
1056static enum dvbfe_algo lgdt3306a_get_frontend_algo(struct dvb_frontend *fe)
1057{
1058#if 1
1059 return DVBFE_ALGO_CUSTOM;
1060#else
1061 return DVBFE_ALGO_HW;
1062#endif
1063}
1064
1065/* ------------------------------------------------------------------------ */
Mauro Carvalho Chehabee0133e2014-10-28 11:21:48 -02001066static int lgdt3306a_monitor_vsb(struct lgdt3306a_state *state)
Fred Richterb63b36f2014-03-24 19:56:00 -03001067{
1068 u8 val;
1069 int ret;
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -03001070 u8 snrRef, maxPowerMan, nCombDet;
1071 u16 fbDlyCir;
Fred Richterb63b36f2014-03-24 19:56:00 -03001072
Mauro Carvalho Chehab4937ba92014-10-28 10:50:36 -02001073 ret = lgdt3306a_read_reg(state, 0x21a1, &val);
Mauro Carvalho Chehabee0133e2014-10-28 11:21:48 -02001074 if (ret)
1075 return ret;
Mauro Carvalho Chehab4937ba92014-10-28 10:50:36 -02001076 snrRef = val & 0x3f;
Fred Richterb63b36f2014-03-24 19:56:00 -03001077
1078 ret = lgdt3306a_read_reg(state, 0x2185, &maxPowerMan);
Mauro Carvalho Chehabee0133e2014-10-28 11:21:48 -02001079 if (ret)
1080 return ret;
Fred Richterb63b36f2014-03-24 19:56:00 -03001081
1082 ret = lgdt3306a_read_reg(state, 0x2191, &val);
Mauro Carvalho Chehabee0133e2014-10-28 11:21:48 -02001083 if (ret)
1084 return ret;
Fred Richterb63b36f2014-03-24 19:56:00 -03001085 nCombDet = (val & 0x80) >> 7;
1086
1087 ret = lgdt3306a_read_reg(state, 0x2180, &val);
Mauro Carvalho Chehabee0133e2014-10-28 11:21:48 -02001088 if (ret)
1089 return ret;
Fred Richterb63b36f2014-03-24 19:56:00 -03001090 fbDlyCir = (val & 0x03) << 8;
Mauro Carvalho Chehabee0133e2014-10-28 11:21:48 -02001091
Fred Richterb63b36f2014-03-24 19:56:00 -03001092 ret = lgdt3306a_read_reg(state, 0x2181, &val);
Mauro Carvalho Chehabee0133e2014-10-28 11:21:48 -02001093 if (ret)
1094 return ret;
Fred Richterb63b36f2014-03-24 19:56:00 -03001095 fbDlyCir |= val;
1096
Mauro Carvalho Chehab097117c2014-10-28 11:35:16 -02001097 dbg_info("snrRef=%d maxPowerMan=0x%x nCombDet=%d fbDlyCir=0x%x\n",
Fred Richterb63b36f2014-03-24 19:56:00 -03001098 snrRef, maxPowerMan, nCombDet, fbDlyCir);
1099
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -03001100 /* Carrier offset sub loop bandwidth */
Fred Richterb63b36f2014-03-24 19:56:00 -03001101 ret = lgdt3306a_read_reg(state, 0x1061, &val);
Mauro Carvalho Chehabee0133e2014-10-28 11:21:48 -02001102 if (ret)
1103 return ret;
Mauro Carvalho Chehab4937ba92014-10-28 10:50:36 -02001104 val &= 0xf8;
Mauro Carvalho Chehab534f4362014-10-28 12:35:18 -02001105 if ((snrRef > 18) && (maxPowerMan > 0x68)
1106 && (nCombDet == 0x01)
1107 && ((fbDlyCir == 0x03FF) || (fbDlyCir < 0x6C))) {
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -03001108 /* SNR is over 18dB and no ghosting */
1109 val |= 0x00; /* final bandwidth = 0 */
Fred Richterb63b36f2014-03-24 19:56:00 -03001110 } else {
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -03001111 val |= 0x04; /* final bandwidth = 4 */
Fred Richterb63b36f2014-03-24 19:56:00 -03001112 }
1113 ret = lgdt3306a_write_reg(state, 0x1061, val);
Mauro Carvalho Chehabee0133e2014-10-28 11:21:48 -02001114 if (ret)
1115 return ret;
Fred Richterb63b36f2014-03-24 19:56:00 -03001116
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -03001117 /* Adjust Notch Filter */
Fred Richterb63b36f2014-03-24 19:56:00 -03001118 ret = lgdt3306a_read_reg(state, 0x0024, &val);
Mauro Carvalho Chehabee0133e2014-10-28 11:21:48 -02001119 if (ret)
1120 return ret;
Mauro Carvalho Chehab4937ba92014-10-28 10:50:36 -02001121 val &= 0x0f;
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -03001122 if (nCombDet == 0) { /* Turn on the Notch Filter */
Fred Richterb63b36f2014-03-24 19:56:00 -03001123 val |= 0x50;
1124 }
1125 ret = lgdt3306a_write_reg(state, 0x0024, val);
Mauro Carvalho Chehabee0133e2014-10-28 11:21:48 -02001126 if (ret)
1127 return ret;
Fred Richterb63b36f2014-03-24 19:56:00 -03001128
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -03001129 /* VSB Timing Recovery output normalization */
Mauro Carvalho Chehab4937ba92014-10-28 10:50:36 -02001130 ret = lgdt3306a_read_reg(state, 0x103d, &val);
Mauro Carvalho Chehabee0133e2014-10-28 11:21:48 -02001131 if (ret)
1132 return ret;
Mauro Carvalho Chehab4937ba92014-10-28 10:50:36 -02001133 val &= 0xcf;
Fred Richterb63b36f2014-03-24 19:56:00 -03001134 val |= 0x20;
Mauro Carvalho Chehab4937ba92014-10-28 10:50:36 -02001135 ret = lgdt3306a_write_reg(state, 0x103d, val);
Mauro Carvalho Chehabee0133e2014-10-28 11:21:48 -02001136
1137 return ret;
Fred Richterb63b36f2014-03-24 19:56:00 -03001138}
1139
Mauro Carvalho Chehab534f4362014-10-28 12:35:18 -02001140static enum lgdt3306a_modulation
1141lgdt3306a_check_oper_mode(struct lgdt3306a_state *state)
Fred Richterb63b36f2014-03-24 19:56:00 -03001142{
1143 u8 val = 0;
1144 int ret;
1145
1146 ret = lgdt3306a_read_reg(state, 0x0081, &val);
Mauro Carvalho Chehabee0133e2014-10-28 11:21:48 -02001147 if (ret)
1148 goto err;
Fred Richterb63b36f2014-03-24 19:56:00 -03001149
1150 if (val & 0x80) {
Mauro Carvalho Chehab097117c2014-10-28 11:35:16 -02001151 dbg_info("VSB\n");
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -03001152 return LG3306_VSB;
Fred Richterb63b36f2014-03-24 19:56:00 -03001153 }
Michael Ira Krufkyc714efe2014-08-03 14:51:49 -03001154 if (val & 0x08) {
Mauro Carvalho Chehab4937ba92014-10-28 10:50:36 -02001155 ret = lgdt3306a_read_reg(state, 0x00a6, &val);
Mauro Carvalho Chehabee0133e2014-10-28 11:21:48 -02001156 if (ret)
1157 goto err;
Fred Richterb63b36f2014-03-24 19:56:00 -03001158 val = val >> 2;
1159 if (val & 0x01) {
Mauro Carvalho Chehab097117c2014-10-28 11:35:16 -02001160 dbg_info("QAM256\n");
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -03001161 return LG3306_QAM256;
Fred Richterb63b36f2014-03-24 19:56:00 -03001162 }
Mauro Carvalho Chehabb4e43e92014-10-28 12:05:35 -02001163 dbg_info("QAM64\n");
1164 return LG3306_QAM64;
Fred Richterb63b36f2014-03-24 19:56:00 -03001165 }
Mauro Carvalho Chehabee0133e2014-10-28 11:21:48 -02001166err:
Mauro Carvalho Chehab097117c2014-10-28 11:35:16 -02001167 pr_warn("UNKNOWN\n");
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -03001168 return LG3306_UNKNOWN_MODE;
Fred Richterb63b36f2014-03-24 19:56:00 -03001169}
1170
Mauro Carvalho Chehab534f4362014-10-28 12:35:18 -02001171static enum lgdt3306a_lock_status
1172lgdt3306a_check_lock_status(struct lgdt3306a_state *state,
1173 enum lgdt3306a_lock_check whatLock)
Fred Richterb63b36f2014-03-24 19:56:00 -03001174{
1175 u8 val = 0;
1176 int ret;
Michael Ira Krufkyf883d602014-08-03 15:29:04 -03001177 enum lgdt3306a_modulation modeOper;
1178 enum lgdt3306a_lock_status lockStatus;
Fred Richterb63b36f2014-03-24 19:56:00 -03001179
1180 modeOper = LG3306_UNKNOWN_MODE;
1181
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -03001182 switch (whatLock) {
1183 case LG3306_SYNC_LOCK:
Fred Richterb63b36f2014-03-24 19:56:00 -03001184 {
Mauro Carvalho Chehab4937ba92014-10-28 10:50:36 -02001185 ret = lgdt3306a_read_reg(state, 0x00a6, &val);
Mauro Carvalho Chehabee0133e2014-10-28 11:21:48 -02001186 if (ret)
1187 return ret;
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -03001188
1189 if ((val & 0x80) == 0x80)
1190 lockStatus = LG3306_LOCK;
1191 else
1192 lockStatus = LG3306_UNLOCK;
1193
Mauro Carvalho Chehab097117c2014-10-28 11:35:16 -02001194 dbg_info("SYNC_LOCK=%x\n", lockStatus);
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -03001195 break;
1196 }
1197 case LG3306_AGC_LOCK:
1198 {
1199 ret = lgdt3306a_read_reg(state, 0x0080, &val);
Mauro Carvalho Chehabee0133e2014-10-28 11:21:48 -02001200 if (ret)
1201 return ret;
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -03001202
1203 if ((val & 0x40) == 0x40)
1204 lockStatus = LG3306_LOCK;
1205 else
1206 lockStatus = LG3306_UNLOCK;
1207
Mauro Carvalho Chehab097117c2014-10-28 11:35:16 -02001208 dbg_info("AGC_LOCK=%x\n", lockStatus);
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -03001209 break;
1210 }
1211 case LG3306_TR_LOCK:
1212 {
1213 modeOper = lgdt3306a_check_oper_mode(state);
1214 if ((modeOper == LG3306_QAM64) || (modeOper == LG3306_QAM256)) {
1215 ret = lgdt3306a_read_reg(state, 0x1094, &val);
Mauro Carvalho Chehabee0133e2014-10-28 11:21:48 -02001216 if (ret)
1217 return ret;
Fred Richterb63b36f2014-03-24 19:56:00 -03001218
1219 if ((val & 0x80) == 0x80)
1220 lockStatus = LG3306_LOCK;
1221 else
1222 lockStatus = LG3306_UNLOCK;
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -03001223 } else
1224 lockStatus = LG3306_UNKNOWN_LOCK;
Fred Richterb63b36f2014-03-24 19:56:00 -03001225
Mauro Carvalho Chehab097117c2014-10-28 11:35:16 -02001226 dbg_info("TR_LOCK=%x\n", lockStatus);
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -03001227 break;
1228 }
1229 case LG3306_FEC_LOCK:
1230 {
1231 modeOper = lgdt3306a_check_oper_mode(state);
1232 if ((modeOper == LG3306_QAM64) || (modeOper == LG3306_QAM256)) {
Fred Richterb63b36f2014-03-24 19:56:00 -03001233 ret = lgdt3306a_read_reg(state, 0x0080, &val);
Mauro Carvalho Chehabee0133e2014-10-28 11:21:48 -02001234 if (ret)
1235 return ret;
Fred Richterb63b36f2014-03-24 19:56:00 -03001236
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -03001237 if ((val & 0x10) == 0x10)
Fred Richterb63b36f2014-03-24 19:56:00 -03001238 lockStatus = LG3306_LOCK;
1239 else
1240 lockStatus = LG3306_UNLOCK;
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -03001241 } else
Fred Richterb63b36f2014-03-24 19:56:00 -03001242 lockStatus = LG3306_UNKNOWN_LOCK;
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -03001243
Mauro Carvalho Chehab097117c2014-10-28 11:35:16 -02001244 dbg_info("FEC_LOCK=%x\n", lockStatus);
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -03001245 break;
Fred Richterb63b36f2014-03-24 19:56:00 -03001246 }
1247
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -03001248 default:
1249 lockStatus = LG3306_UNKNOWN_LOCK;
Mauro Carvalho Chehab097117c2014-10-28 11:35:16 -02001250 pr_warn("UNKNOWN whatLock=%d\n", whatLock);
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -03001251 break;
1252 }
1253
1254 return lockStatus;
Fred Richterb63b36f2014-03-24 19:56:00 -03001255}
1256
Mauro Carvalho Chehab534f4362014-10-28 12:35:18 -02001257static enum lgdt3306a_neverlock_status
1258lgdt3306a_check_neverlock_status(struct lgdt3306a_state *state)
Fred Richterb63b36f2014-03-24 19:56:00 -03001259{
1260 u8 val = 0;
1261 int ret;
Michael Ira Krufkyf883d602014-08-03 15:29:04 -03001262 enum lgdt3306a_neverlock_status lockStatus;
Fred Richterb63b36f2014-03-24 19:56:00 -03001263
1264 ret = lgdt3306a_read_reg(state, 0x0080, &val);
Mauro Carvalho Chehabee0133e2014-10-28 11:21:48 -02001265 if (ret)
1266 return ret;
Michael Ira Krufkyf883d602014-08-03 15:29:04 -03001267 lockStatus = (enum lgdt3306a_neverlock_status)(val & 0x03);
Fred Richterb63b36f2014-03-24 19:56:00 -03001268
Mauro Carvalho Chehab097117c2014-10-28 11:35:16 -02001269 dbg_info("NeverLock=%d", lockStatus);
Fred Richterb63b36f2014-03-24 19:56:00 -03001270
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -03001271 return lockStatus;
Fred Richterb63b36f2014-03-24 19:56:00 -03001272}
1273
Mauro Carvalho Chehabee0133e2014-10-28 11:21:48 -02001274static int lgdt3306a_pre_monitoring(struct lgdt3306a_state *state)
Fred Richterb63b36f2014-03-24 19:56:00 -03001275{
1276 u8 val = 0;
1277 int ret;
1278 u8 currChDiffACQ, snrRef, mainStrong, aiccrejStatus;
1279
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -03001280 /* Channel variation */
Mauro Carvalho Chehab4937ba92014-10-28 10:50:36 -02001281 ret = lgdt3306a_read_reg(state, 0x21bc, &currChDiffACQ);
Mauro Carvalho Chehabee0133e2014-10-28 11:21:48 -02001282 if (ret)
1283 return ret;
Fred Richterb63b36f2014-03-24 19:56:00 -03001284
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -03001285 /* SNR of Frame sync */
Mauro Carvalho Chehab4937ba92014-10-28 10:50:36 -02001286 ret = lgdt3306a_read_reg(state, 0x21a1, &val);
Mauro Carvalho Chehabee0133e2014-10-28 11:21:48 -02001287 if (ret)
1288 return ret;
Mauro Carvalho Chehab4937ba92014-10-28 10:50:36 -02001289 snrRef = val & 0x3f;
Fred Richterb63b36f2014-03-24 19:56:00 -03001290
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -03001291 /* Strong Main CIR */
Fred Richterb63b36f2014-03-24 19:56:00 -03001292 ret = lgdt3306a_read_reg(state, 0x2199, &val);
Mauro Carvalho Chehabee0133e2014-10-28 11:21:48 -02001293 if (ret)
1294 return ret;
Fred Richterb63b36f2014-03-24 19:56:00 -03001295 mainStrong = (val & 0x40) >> 6;
1296
1297 ret = lgdt3306a_read_reg(state, 0x0090, &val);
Mauro Carvalho Chehabee0133e2014-10-28 11:21:48 -02001298 if (ret)
1299 return ret;
Mauro Carvalho Chehab4937ba92014-10-28 10:50:36 -02001300 aiccrejStatus = (val & 0xf0) >> 4;
Fred Richterb63b36f2014-03-24 19:56:00 -03001301
Mauro Carvalho Chehab097117c2014-10-28 11:35:16 -02001302 dbg_info("snrRef=%d mainStrong=%d aiccrejStatus=%d currChDiffACQ=0x%x\n",
Fred Richterb63b36f2014-03-24 19:56:00 -03001303 snrRef, mainStrong, aiccrejStatus, currChDiffACQ);
1304
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -03001305#if 0
Mauro Carvalho Chehab534f4362014-10-28 12:35:18 -02001306 /* Dynamic ghost exists */
1307 if ((mainStrong == 0) && (currChDiffACQ > 0x70))
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -03001308#endif
1309 if (mainStrong == 0) {
Fred Richterb63b36f2014-03-24 19:56:00 -03001310 ret = lgdt3306a_read_reg(state, 0x2135, &val);
Mauro Carvalho Chehabee0133e2014-10-28 11:21:48 -02001311 if (ret)
1312 return ret;
Mauro Carvalho Chehab4937ba92014-10-28 10:50:36 -02001313 val &= 0x0f;
1314 val |= 0xa0;
Fred Richterb63b36f2014-03-24 19:56:00 -03001315 ret = lgdt3306a_write_reg(state, 0x2135, val);
Mauro Carvalho Chehabee0133e2014-10-28 11:21:48 -02001316 if (ret)
1317 return ret;
Fred Richterb63b36f2014-03-24 19:56:00 -03001318
1319 ret = lgdt3306a_read_reg(state, 0x2141, &val);
Mauro Carvalho Chehabee0133e2014-10-28 11:21:48 -02001320 if (ret)
1321 return ret;
Mauro Carvalho Chehab4937ba92014-10-28 10:50:36 -02001322 val &= 0x3f;
Fred Richterb63b36f2014-03-24 19:56:00 -03001323 val |= 0x80;
1324 ret = lgdt3306a_write_reg(state, 0x2141, val);
Mauro Carvalho Chehabee0133e2014-10-28 11:21:48 -02001325 if (ret)
1326 return ret;
Fred Richterb63b36f2014-03-24 19:56:00 -03001327
1328 ret = lgdt3306a_write_reg(state, 0x2122, 0x70);
Mauro Carvalho Chehabee0133e2014-10-28 11:21:48 -02001329 if (ret)
1330 return ret;
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -03001331 } else { /* Weak ghost or static channel */
Fred Richterb63b36f2014-03-24 19:56:00 -03001332 ret = lgdt3306a_read_reg(state, 0x2135, &val);
Mauro Carvalho Chehabee0133e2014-10-28 11:21:48 -02001333 if (ret)
1334 return ret;
Mauro Carvalho Chehab4937ba92014-10-28 10:50:36 -02001335 val &= 0x0f;
Fred Richterb63b36f2014-03-24 19:56:00 -03001336 val |= 0x70;
1337 ret = lgdt3306a_write_reg(state, 0x2135, val);
Mauro Carvalho Chehabee0133e2014-10-28 11:21:48 -02001338 if (ret)
1339 return ret;
Fred Richterb63b36f2014-03-24 19:56:00 -03001340
1341 ret = lgdt3306a_read_reg(state, 0x2141, &val);
Mauro Carvalho Chehabee0133e2014-10-28 11:21:48 -02001342 if (ret)
1343 return ret;
Mauro Carvalho Chehab4937ba92014-10-28 10:50:36 -02001344 val &= 0x3f;
Fred Richterb63b36f2014-03-24 19:56:00 -03001345 val |= 0x40;
1346 ret = lgdt3306a_write_reg(state, 0x2141, val);
Mauro Carvalho Chehabee0133e2014-10-28 11:21:48 -02001347 if (ret)
1348 return ret;
Fred Richterb63b36f2014-03-24 19:56:00 -03001349
1350 ret = lgdt3306a_write_reg(state, 0x2122, 0x40);
Mauro Carvalho Chehabee0133e2014-10-28 11:21:48 -02001351 if (ret)
1352 return ret;
Fred Richterb63b36f2014-03-24 19:56:00 -03001353 }
Mauro Carvalho Chehabee0133e2014-10-28 11:21:48 -02001354 return 0;
Fred Richterb63b36f2014-03-24 19:56:00 -03001355}
1356
Mauro Carvalho Chehab534f4362014-10-28 12:35:18 -02001357static enum lgdt3306a_lock_status
1358lgdt3306a_sync_lock_poll(struct lgdt3306a_state *state)
Fred Richterb63b36f2014-03-24 19:56:00 -03001359{
Michael Ira Krufkyf883d602014-08-03 15:29:04 -03001360 enum lgdt3306a_lock_status syncLockStatus = LG3306_UNLOCK;
Fred Richterb63b36f2014-03-24 19:56:00 -03001361 int i;
1362
1363 for (i = 0; i < 2; i++) {
1364 msleep(30);
1365
Mauro Carvalho Chehab534f4362014-10-28 12:35:18 -02001366 syncLockStatus = lgdt3306a_check_lock_status(state,
1367 LG3306_SYNC_LOCK);
Fred Richterb63b36f2014-03-24 19:56:00 -03001368
1369 if (syncLockStatus == LG3306_LOCK) {
Mauro Carvalho Chehab097117c2014-10-28 11:35:16 -02001370 dbg_info("locked(%d)\n", i);
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -03001371 return LG3306_LOCK;
Fred Richterb63b36f2014-03-24 19:56:00 -03001372 }
1373 }
Mauro Carvalho Chehab097117c2014-10-28 11:35:16 -02001374 dbg_info("not locked\n");
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -03001375 return LG3306_UNLOCK;
Fred Richterb63b36f2014-03-24 19:56:00 -03001376}
1377
Mauro Carvalho Chehab534f4362014-10-28 12:35:18 -02001378static enum lgdt3306a_lock_status
1379lgdt3306a_fec_lock_poll(struct lgdt3306a_state *state)
Fred Richterb63b36f2014-03-24 19:56:00 -03001380{
Michael Ira Krufkyf883d602014-08-03 15:29:04 -03001381 enum lgdt3306a_lock_status FECLockStatus = LG3306_UNLOCK;
Fred Richterb63b36f2014-03-24 19:56:00 -03001382 int i;
1383
1384 for (i = 0; i < 2; i++) {
1385 msleep(30);
1386
Mauro Carvalho Chehab534f4362014-10-28 12:35:18 -02001387 FECLockStatus = lgdt3306a_check_lock_status(state,
1388 LG3306_FEC_LOCK);
Fred Richterb63b36f2014-03-24 19:56:00 -03001389
1390 if (FECLockStatus == LG3306_LOCK) {
Mauro Carvalho Chehab097117c2014-10-28 11:35:16 -02001391 dbg_info("locked(%d)\n", i);
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -03001392 return FECLockStatus;
Fred Richterb63b36f2014-03-24 19:56:00 -03001393 }
1394 }
Mauro Carvalho Chehab097117c2014-10-28 11:35:16 -02001395 dbg_info("not locked\n");
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -03001396 return FECLockStatus;
Fred Richterb63b36f2014-03-24 19:56:00 -03001397}
1398
Mauro Carvalho Chehab534f4362014-10-28 12:35:18 -02001399static enum lgdt3306a_neverlock_status
1400lgdt3306a_neverlock_poll(struct lgdt3306a_state *state)
Fred Richterb63b36f2014-03-24 19:56:00 -03001401{
Michael Ira Krufkyf883d602014-08-03 15:29:04 -03001402 enum lgdt3306a_neverlock_status NLLockStatus = LG3306_NL_FAIL;
Fred Richterb63b36f2014-03-24 19:56:00 -03001403 int i;
1404
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -03001405 for (i = 0; i < 5; i++) {
Fred Richterb63b36f2014-03-24 19:56:00 -03001406 msleep(30);
1407
1408 NLLockStatus = lgdt3306a_check_neverlock_status(state);
1409
1410 if (NLLockStatus == LG3306_NL_LOCK) {
Mauro Carvalho Chehab097117c2014-10-28 11:35:16 -02001411 dbg_info("NL_LOCK(%d)\n", i);
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -03001412 return NLLockStatus;
Fred Richterb63b36f2014-03-24 19:56:00 -03001413 }
1414 }
Mauro Carvalho Chehab097117c2014-10-28 11:35:16 -02001415 dbg_info("NLLockStatus=%d\n", NLLockStatus);
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -03001416 return NLLockStatus;
Fred Richterb63b36f2014-03-24 19:56:00 -03001417}
1418
1419static u8 lgdt3306a_get_packet_error(struct lgdt3306a_state *state)
1420{
1421 u8 val;
1422 int ret;
1423
Mauro Carvalho Chehab4937ba92014-10-28 10:50:36 -02001424 ret = lgdt3306a_read_reg(state, 0x00fa, &val);
Mauro Carvalho Chehabee0133e2014-10-28 11:21:48 -02001425 if (ret)
1426 return ret;
Fred Richterb63b36f2014-03-24 19:56:00 -03001427
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -03001428 return val;
Fred Richterb63b36f2014-03-24 19:56:00 -03001429}
1430
Mauro Carvalho Chehab9369fe02014-10-28 12:30:44 -02001431static const u32 valx_x10[] = {
1432 10, 11, 13, 15, 17, 20, 25, 33, 41, 50, 59, 73, 87, 100
1433};
1434static const u32 log10x_x1000[] = {
Mauro Carvalho Chehab95f22c52014-10-28 12:40:20 -02001435 0, 41, 114, 176, 230, 301, 398, 518, 613, 699, 771, 863, 939, 1000
Mauro Carvalho Chehab9369fe02014-10-28 12:30:44 -02001436};
1437
Fred Richterb63b36f2014-03-24 19:56:00 -03001438static u32 log10_x1000(u32 x)
1439{
Mauro Carvalho Chehaba132fef2014-10-28 11:07:03 -02001440 u32 diff_val, step_val, step_log10;
Fred Richterb63b36f2014-03-24 19:56:00 -03001441 u32 log_val = 0;
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -03001442 u32 i;
Fred Richterb63b36f2014-03-24 19:56:00 -03001443
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -03001444 if (x <= 0)
1445 return -1000000; /* signal error */
Fred Richterb63b36f2014-03-24 19:56:00 -03001446
Mauro Carvalho Chehabb4e43e92014-10-28 12:05:35 -02001447 if (x == 10)
1448 return 0; /* log(1)=0 */
1449
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -03001450 if (x < 10) {
1451 while (x < 10) {
1452 x = x * 10;
Fred Richterb63b36f2014-03-24 19:56:00 -03001453 log_val--;
1454 }
Mauro Carvalho Chehabb4e43e92014-10-28 12:05:35 -02001455 } else { /* x > 10 */
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -03001456 while (x >= 100) {
1457 x = x / 10;
Fred Richterb63b36f2014-03-24 19:56:00 -03001458 log_val++;
1459 }
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -03001460 }
Fred Richterb63b36f2014-03-24 19:56:00 -03001461 log_val *= 1000;
1462
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -03001463 if (x == 10) /* was our input an exact multiple of 10 */
1464 return log_val; /* don't need to interpolate */
Fred Richterb63b36f2014-03-24 19:56:00 -03001465
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -03001466 /* find our place on the log curve */
Mauro Carvalho Chehab9369fe02014-10-28 12:30:44 -02001467 for (i = 1; i < ARRAY_SIZE(valx_x10); i++) {
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -03001468 if (valx_x10[i] >= x)
1469 break;
Fred Richterb63b36f2014-03-24 19:56:00 -03001470 }
Mauro Carvalho Chehab9369fe02014-10-28 12:30:44 -02001471 if (i == ARRAY_SIZE(valx_x10))
Mauro Carvalho Chehaba132fef2014-10-28 11:07:03 -02001472 return log_val + log10x_x1000[i - 1];
Fred Richterb63b36f2014-03-24 19:56:00 -03001473
Mauro Carvalho Chehaba132fef2014-10-28 11:07:03 -02001474 diff_val = x - valx_x10[i-1];
1475 step_val = valx_x10[i] - valx_x10[i - 1];
1476 step_log10 = log10x_x1000[i] - log10x_x1000[i - 1];
1477
1478 /* do a linear interpolation to get in-between values */
1479 return log_val + log10x_x1000[i - 1] +
1480 ((diff_val*step_log10) / step_val);
Fred Richterb63b36f2014-03-24 19:56:00 -03001481}
1482
1483static u32 lgdt3306a_calculate_snr_x100(struct lgdt3306a_state *state)
1484{
Michael Ira Krufky34a5a2f2014-10-25 11:26:15 -03001485 u32 mse; /* Mean-Square Error */
1486 u32 pwr; /* Constelation power */
Fred Richterb63b36f2014-03-24 19:56:00 -03001487 u32 snr_x100;
1488
Mauro Carvalho Chehab4937ba92014-10-28 10:50:36 -02001489 mse = (read_reg(state, 0x00ec) << 8) |
1490 (read_reg(state, 0x00ed));
1491 pwr = (read_reg(state, 0x00e8) << 8) |
1492 (read_reg(state, 0x00e9));
Fred Richterb63b36f2014-03-24 19:56:00 -03001493
1494 if (mse == 0) /* no signal */
1495 return 0;
1496
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -03001497 snr_x100 = log10_x1000((pwr * 10000) / mse) - 3000;
Mauro Carvalho Chehab097117c2014-10-28 11:35:16 -02001498 dbg_info("mse=%u, pwr=%u, snr_x100=%d\n", mse, pwr, snr_x100);
Fred Richterb63b36f2014-03-24 19:56:00 -03001499
1500 return snr_x100;
1501}
1502
Mauro Carvalho Chehab534f4362014-10-28 12:35:18 -02001503static enum lgdt3306a_lock_status
1504lgdt3306a_vsb_lock_poll(struct lgdt3306a_state *state)
Fred Richterb63b36f2014-03-24 19:56:00 -03001505{
Mauro Carvalho Chehabe2c47fa2014-10-28 11:27:34 -02001506 int ret;
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -03001507 u8 cnt = 0;
1508 u8 packet_error;
1509 u32 snr;
Fred Richterb63b36f2014-03-24 19:56:00 -03001510
Mauro Carvalho Chehabb1a88c72014-10-28 12:00:48 -02001511 for (cnt = 0; cnt < 10; cnt++) {
Fred Richterb63b36f2014-03-24 19:56:00 -03001512 if (lgdt3306a_sync_lock_poll(state) == LG3306_UNLOCK) {
Mauro Carvalho Chehab097117c2014-10-28 11:35:16 -02001513 dbg_info("no sync lock!\n");
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -03001514 return LG3306_UNLOCK;
Fred Richterb63b36f2014-03-24 19:56:00 -03001515 }
Mauro Carvalho Chehabb1a88c72014-10-28 12:00:48 -02001516
1517 msleep(20);
1518 ret = lgdt3306a_pre_monitoring(state);
1519 if (ret)
1520 break;
1521
1522 packet_error = lgdt3306a_get_packet_error(state);
1523 snr = lgdt3306a_calculate_snr_x100(state);
1524 dbg_info("cnt=%d errors=%d snr=%d\n", cnt, packet_error, snr);
1525
1526 if ((snr >= 1500) && (packet_error < 0xff))
1527 return LG3306_LOCK;
Fred Richterb63b36f2014-03-24 19:56:00 -03001528 }
Mauro Carvalho Chehabb1a88c72014-10-28 12:00:48 -02001529
1530 dbg_info("not locked!\n");
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -03001531 return LG3306_UNLOCK;
Fred Richterb63b36f2014-03-24 19:56:00 -03001532}
1533
Mauro Carvalho Chehab534f4362014-10-28 12:35:18 -02001534static enum lgdt3306a_lock_status
1535lgdt3306a_qam_lock_poll(struct lgdt3306a_state *state)
Fred Richterb63b36f2014-03-24 19:56:00 -03001536{
Mauro Carvalho Chehabb1a88c72014-10-28 12:00:48 -02001537 u8 cnt;
Fred Richterb63b36f2014-03-24 19:56:00 -03001538 u8 packet_error;
1539 u32 snr;
1540
Mauro Carvalho Chehabb1a88c72014-10-28 12:00:48 -02001541 for (cnt = 0; cnt < 10; cnt++) {
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -03001542 if (lgdt3306a_fec_lock_poll(state) == LG3306_UNLOCK) {
Mauro Carvalho Chehab097117c2014-10-28 11:35:16 -02001543 dbg_info("no fec lock!\n");
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -03001544 return LG3306_UNLOCK;
Fred Richterb63b36f2014-03-24 19:56:00 -03001545 }
Mauro Carvalho Chehabb1a88c72014-10-28 12:00:48 -02001546
1547 msleep(20);
1548
1549 packet_error = lgdt3306a_get_packet_error(state);
1550 snr = lgdt3306a_calculate_snr_x100(state);
1551 dbg_info("cnt=%d errors=%d snr=%d\n", cnt, packet_error, snr);
1552
1553 if ((snr >= 1500) && (packet_error < 0xff))
1554 return LG3306_LOCK;
Fred Richterb63b36f2014-03-24 19:56:00 -03001555 }
Mauro Carvalho Chehabb1a88c72014-10-28 12:00:48 -02001556
1557 dbg_info("not locked!\n");
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -03001558 return LG3306_UNLOCK;
Fred Richterb63b36f2014-03-24 19:56:00 -03001559}
1560
Mauro Carvalho Chehab0df289a2015-06-07 14:53:52 -03001561static int lgdt3306a_read_status(struct dvb_frontend *fe,
1562 enum fe_status *status)
Fred Richterb63b36f2014-03-24 19:56:00 -03001563{
Fred Richterb63b36f2014-03-24 19:56:00 -03001564 struct lgdt3306a_state *state = fe->demodulator_priv;
Fred Richterb63b36f2014-03-24 19:56:00 -03001565 u16 strength = 0;
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -03001566 int ret = 0;
1567
Fred Richterb63b36f2014-03-24 19:56:00 -03001568 if (fe->ops.tuner_ops.get_rf_strength) {
1569 ret = fe->ops.tuner_ops.get_rf_strength(fe, &strength);
Mauro Carvalho Chehabc9897642014-10-28 12:07:52 -02001570 if (ret == 0)
Mauro Carvalho Chehab097117c2014-10-28 11:35:16 -02001571 dbg_info("strength=%d\n", strength);
Mauro Carvalho Chehabc9897642014-10-28 12:07:52 -02001572 else
Mauro Carvalho Chehab097117c2014-10-28 11:35:16 -02001573 dbg_info("fe->ops.tuner_ops.get_rf_strength() failed\n");
Fred Richterb63b36f2014-03-24 19:56:00 -03001574 }
1575
1576 *status = 0;
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -03001577 if (lgdt3306a_neverlock_poll(state) == LG3306_NL_LOCK) {
Fred Richterb63b36f2014-03-24 19:56:00 -03001578 *status |= FE_HAS_SIGNAL;
1579 *status |= FE_HAS_CARRIER;
1580
1581 switch (state->current_modulation) {
1582 case QAM_256:
1583 case QAM_64:
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -03001584 if (lgdt3306a_qam_lock_poll(state) == LG3306_LOCK) {
Fred Richterb63b36f2014-03-24 19:56:00 -03001585 *status |= FE_HAS_VITERBI;
1586 *status |= FE_HAS_SYNC;
1587
1588 *status |= FE_HAS_LOCK;
1589 }
1590 break;
1591 case VSB_8:
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -03001592 if (lgdt3306a_vsb_lock_poll(state) == LG3306_LOCK) {
Fred Richterb63b36f2014-03-24 19:56:00 -03001593 *status |= FE_HAS_VITERBI;
1594 *status |= FE_HAS_SYNC;
1595
1596 *status |= FE_HAS_LOCK;
1597
Mauro Carvalho Chehabee0133e2014-10-28 11:21:48 -02001598 ret = lgdt3306a_monitor_vsb(state);
Fred Richterb63b36f2014-03-24 19:56:00 -03001599 }
1600 break;
1601 default:
1602 ret = -EINVAL;
1603 }
1604 }
1605 return ret;
1606}
1607
1608
1609static int lgdt3306a_read_snr(struct dvb_frontend *fe, u16 *snr)
1610{
1611 struct lgdt3306a_state *state = fe->demodulator_priv;
1612
1613 state->snr = lgdt3306a_calculate_snr_x100(state);
1614 /* report SNR in dB * 10 */
1615 *snr = state->snr/10;
1616
1617 return 0;
1618}
1619
1620static int lgdt3306a_read_signal_strength(struct dvb_frontend *fe,
1621 u16 *strength)
1622{
1623 /*
1624 * Calculate some sort of "strength" from SNR
1625 */
1626 struct lgdt3306a_state *state = fe->demodulator_priv;
Michael Ira Krufky34a5a2f2014-10-25 11:26:15 -03001627 u16 snr; /* snr_x10 */
Fred Richterb63b36f2014-03-24 19:56:00 -03001628 int ret;
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -03001629 u32 ref_snr; /* snr*100 */
Fred Richterb63b36f2014-03-24 19:56:00 -03001630 u32 str;
1631
1632 *strength = 0;
1633
1634 switch (state->current_modulation) {
1635 case VSB_8:
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -03001636 ref_snr = 1600; /* 16dB */
Fred Richterb63b36f2014-03-24 19:56:00 -03001637 break;
1638 case QAM_64:
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -03001639 ref_snr = 2200; /* 22dB */
Fred Richterb63b36f2014-03-24 19:56:00 -03001640 break;
1641 case QAM_256:
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -03001642 ref_snr = 2800; /* 28dB */
Fred Richterb63b36f2014-03-24 19:56:00 -03001643 break;
1644 default:
1645 return -EINVAL;
1646 }
1647
1648 ret = fe->ops.read_snr(fe, &snr);
1649 if (lg_chkerr(ret))
1650 goto fail;
1651
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -03001652 if (state->snr <= (ref_snr - 100))
Fred Richterb63b36f2014-03-24 19:56:00 -03001653 str = 0;
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -03001654 else if (state->snr <= ref_snr)
1655 str = (0xffff * 65) / 100; /* 65% */
Fred Richterb63b36f2014-03-24 19:56:00 -03001656 else {
1657 str = state->snr - ref_snr;
1658 str /= 50;
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -03001659 str += 78; /* 78%-100% */
1660 if (str > 100)
Fred Richterb63b36f2014-03-24 19:56:00 -03001661 str = 100;
1662 str = (0xffff * str) / 100;
1663 }
1664 *strength = (u16)str;
Mauro Carvalho Chehab097117c2014-10-28 11:35:16 -02001665 dbg_info("strength=%u\n", *strength);
Fred Richterb63b36f2014-03-24 19:56:00 -03001666
1667fail:
1668 return ret;
1669}
1670
1671/* ------------------------------------------------------------------------ */
1672
1673static int lgdt3306a_read_ber(struct dvb_frontend *fe, u32 *ber)
1674{
1675 struct lgdt3306a_state *state = fe->demodulator_priv;
1676 u32 tmp;
1677
1678 *ber = 0;
1679#if 1
Mauro Carvalho Chehab534f4362014-10-28 12:35:18 -02001680 /* FGR - FIXME - I don't know what value is expected by dvb_core
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -03001681 * what is the scale of the value?? */
Mauro Carvalho Chehab4937ba92014-10-28 10:50:36 -02001682 tmp = read_reg(state, 0x00fc); /* NBERVALUE[24-31] */
1683 tmp = (tmp << 8) | read_reg(state, 0x00fd); /* NBERVALUE[16-23] */
1684 tmp = (tmp << 8) | read_reg(state, 0x00fe); /* NBERVALUE[8-15] */
1685 tmp = (tmp << 8) | read_reg(state, 0x00ff); /* NBERVALUE[0-7] */
Fred Richterb63b36f2014-03-24 19:56:00 -03001686 *ber = tmp;
Mauro Carvalho Chehab097117c2014-10-28 11:35:16 -02001687 dbg_info("ber=%u\n", tmp);
Fred Richterb63b36f2014-03-24 19:56:00 -03001688#endif
1689 return 0;
1690}
1691
1692static int lgdt3306a_read_ucblocks(struct dvb_frontend *fe, u32 *ucblocks)
1693{
1694 struct lgdt3306a_state *state = fe->demodulator_priv;
1695
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -03001696 *ucblocks = 0;
Fred Richterb63b36f2014-03-24 19:56:00 -03001697#if 1
Mauro Carvalho Chehab534f4362014-10-28 12:35:18 -02001698 /* FGR - FIXME - I don't know what value is expected by dvb_core
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -03001699 * what happens when value wraps? */
Mauro Carvalho Chehab4937ba92014-10-28 10:50:36 -02001700 *ucblocks = read_reg(state, 0x00f4); /* TPIFTPERRCNT[0-7] */
Mauro Carvalho Chehab097117c2014-10-28 11:35:16 -02001701 dbg_info("ucblocks=%u\n", *ucblocks);
Fred Richterb63b36f2014-03-24 19:56:00 -03001702#endif
1703
1704 return 0;
1705}
1706
Mauro Carvalho Chehab534f4362014-10-28 12:35:18 -02001707static int lgdt3306a_tune(struct dvb_frontend *fe, bool re_tune,
1708 unsigned int mode_flags, unsigned int *delay,
Mauro Carvalho Chehab0df289a2015-06-07 14:53:52 -03001709 enum fe_status *status)
Fred Richterb63b36f2014-03-24 19:56:00 -03001710{
1711 int ret = 0;
1712 struct lgdt3306a_state *state = fe->demodulator_priv;
1713
Mauro Carvalho Chehab097117c2014-10-28 11:35:16 -02001714 dbg_info("re_tune=%u\n", re_tune);
Fred Richterb63b36f2014-03-24 19:56:00 -03001715
1716 if (re_tune) {
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -03001717 state->current_frequency = -1; /* force re-tune */
Michael Ira Krufkyae21e442014-08-03 15:18:23 -03001718 ret = lgdt3306a_set_parameters(fe);
1719 if (ret != 0)
Fred Richterb63b36f2014-03-24 19:56:00 -03001720 return ret;
Fred Richterb63b36f2014-03-24 19:56:00 -03001721 }
1722 *delay = 125;
1723 ret = lgdt3306a_read_status(fe, status);
1724
1725 return ret;
1726}
1727
1728static int lgdt3306a_get_tune_settings(struct dvb_frontend *fe,
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -03001729 struct dvb_frontend_tune_settings
1730 *fe_tune_settings)
Fred Richterb63b36f2014-03-24 19:56:00 -03001731{
1732 fe_tune_settings->min_delay_ms = 100;
Mauro Carvalho Chehab097117c2014-10-28 11:35:16 -02001733 dbg_info("\n");
Fred Richterb63b36f2014-03-24 19:56:00 -03001734 return 0;
1735}
1736
1737static int lgdt3306a_search(struct dvb_frontend *fe)
1738{
Mauro Carvalho Chehab0df289a2015-06-07 14:53:52 -03001739 enum fe_status status = 0;
Fred Richterb63b36f2014-03-24 19:56:00 -03001740 int i, ret;
1741
1742 /* set frontend */
1743 ret = lgdt3306a_set_parameters(fe);
1744 if (ret)
1745 goto error;
1746
1747 /* wait frontend lock */
1748 for (i = 20; i > 0; i--) {
Mauro Carvalho Chehab097117c2014-10-28 11:35:16 -02001749 dbg_info(": loop=%d\n", i);
Fred Richterb63b36f2014-03-24 19:56:00 -03001750 msleep(50);
1751 ret = lgdt3306a_read_status(fe, &status);
1752 if (ret)
1753 goto error;
1754
1755 if (status & FE_HAS_LOCK)
1756 break;
1757 }
1758
1759 /* check if we have a valid signal */
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -03001760 if (status & FE_HAS_LOCK)
Fred Richterb63b36f2014-03-24 19:56:00 -03001761 return DVBFE_ALGO_SEARCH_SUCCESS;
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -03001762 else
Fred Richterb63b36f2014-03-24 19:56:00 -03001763 return DVBFE_ALGO_SEARCH_AGAIN;
Fred Richterb63b36f2014-03-24 19:56:00 -03001764
1765error:
Mauro Carvalho Chehab097117c2014-10-28 11:35:16 -02001766 dbg_info("failed (%d)\n", ret);
Fred Richterb63b36f2014-03-24 19:56:00 -03001767 return DVBFE_ALGO_SEARCH_ERROR;
1768}
1769
1770static void lgdt3306a_release(struct dvb_frontend *fe)
1771{
1772 struct lgdt3306a_state *state = fe->demodulator_priv;
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -03001773
Mauro Carvalho Chehab097117c2014-10-28 11:35:16 -02001774 dbg_info("\n");
Fred Richterb63b36f2014-03-24 19:56:00 -03001775 kfree(state);
1776}
1777
1778static struct dvb_frontend_ops lgdt3306a_ops;
1779
1780struct dvb_frontend *lgdt3306a_attach(const struct lgdt3306a_config *config,
Mauro Carvalho Chehabc43e6512014-10-28 10:56:10 -02001781 struct i2c_adapter *i2c_adap)
Fred Richterb63b36f2014-03-24 19:56:00 -03001782{
1783 struct lgdt3306a_state *state = NULL;
1784 int ret;
1785 u8 val;
1786
Mauro Carvalho Chehab097117c2014-10-28 11:35:16 -02001787 dbg_info("(%d-%04x)\n",
Fred Richterb63b36f2014-03-24 19:56:00 -03001788 i2c_adap ? i2c_adapter_id(i2c_adap) : 0,
1789 config ? config->i2c_addr : 0);
1790
1791 state = kzalloc(sizeof(struct lgdt3306a_state), GFP_KERNEL);
1792 if (state == NULL)
1793 goto fail;
1794
1795 state->cfg = config;
1796 state->i2c_adap = i2c_adap;
1797
1798 memcpy(&state->frontend.ops, &lgdt3306a_ops,
1799 sizeof(struct dvb_frontend_ops));
1800 state->frontend.demodulator_priv = state;
1801
1802 /* verify that we're talking to a lg3306a */
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -03001803 /* FGR - NOTE - there is no obvious ChipId to check; we check
1804 * some "known" bits after reset, but it's still just a guess */
Fred Richterb63b36f2014-03-24 19:56:00 -03001805 ret = lgdt3306a_read_reg(state, 0x0000, &val);
1806 if (lg_chkerr(ret))
1807 goto fail;
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -03001808 if ((val & 0x74) != 0x74) {
Mauro Carvalho Chehab097117c2014-10-28 11:35:16 -02001809 pr_warn("expected 0x74, got 0x%x\n", (val & 0x74));
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -03001810#if 0
Mauro Carvalho Chehab534f4362014-10-28 12:35:18 -02001811 /* FIXME - re-enable when we know this is right */
1812 goto fail;
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -03001813#endif
Fred Richterb63b36f2014-03-24 19:56:00 -03001814 }
1815 ret = lgdt3306a_read_reg(state, 0x0001, &val);
1816 if (lg_chkerr(ret))
1817 goto fail;
Mauro Carvalho Chehab4937ba92014-10-28 10:50:36 -02001818 if ((val & 0xf6) != 0xc6) {
Mauro Carvalho Chehab097117c2014-10-28 11:35:16 -02001819 pr_warn("expected 0xc6, got 0x%x\n", (val & 0xf6));
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -03001820#if 0
Mauro Carvalho Chehab534f4362014-10-28 12:35:18 -02001821 /* FIXME - re-enable when we know this is right */
1822 goto fail;
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -03001823#endif
Fred Richterb63b36f2014-03-24 19:56:00 -03001824 }
1825 ret = lgdt3306a_read_reg(state, 0x0002, &val);
1826 if (lg_chkerr(ret))
1827 goto fail;
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -03001828 if ((val & 0x73) != 0x03) {
Mauro Carvalho Chehab097117c2014-10-28 11:35:16 -02001829 pr_warn("expected 0x03, got 0x%x\n", (val & 0x73));
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -03001830#if 0
Mauro Carvalho Chehab534f4362014-10-28 12:35:18 -02001831 /* FIXME - re-enable when we know this is right */
1832 goto fail;
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -03001833#endif
Fred Richterb63b36f2014-03-24 19:56:00 -03001834 }
1835
1836 state->current_frequency = -1;
1837 state->current_modulation = -1;
1838
1839 lgdt3306a_sleep(state);
1840
1841 return &state->frontend;
1842
1843fail:
Mauro Carvalho Chehab097117c2014-10-28 11:35:16 -02001844 pr_warn("unable to detect LGDT3306A hardware\n");
Fred Richterb63b36f2014-03-24 19:56:00 -03001845 kfree(state);
1846 return NULL;
1847}
Michael Ira Krufkyebd91752014-08-03 15:05:59 -03001848EXPORT_SYMBOL(lgdt3306a_attach);
Fred Richterb63b36f2014-03-24 19:56:00 -03001849
1850#ifdef DBG_DUMP
1851
1852static const short regtab[] = {
Michael Ira Krufkycb4671c2014-10-25 11:12:25 -03001853 0x0000, /* SOFTRSTB 1'b1 1'b1 1'b1 ADCPDB 1'b1 PLLPDB GBBPDB 11111111 */
1854 0x0001, /* 1'b1 1'b1 1'b0 1'b0 AUTORPTRS */
1855 0x0002, /* NI2CRPTEN 1'b0 1'b0 1'b0 SPECINVAUT */
1856 0x0003, /* AGCRFOUT */
1857 0x0004, /* ADCSEL1V ADCCNT ADCCNF ADCCNS ADCCLKPLL */
1858 0x0005, /* PLLINDIVSE */
1859 0x0006, /* PLLCTRL[7:0] 11100001 */
1860 0x0007, /* SYSINITWAITTIME[7:0] (msec) 00001000 */
1861 0x0008, /* STDOPMODE[7:0] 10000000 */
1862 0x0009, /* 1'b0 1'b0 1'b0 STDOPDETTMODE[2:0] STDOPDETCMODE[1:0] 00011110 */
Mauro Carvalho Chehab4937ba92014-10-28 10:50:36 -02001863 0x000a, /* DAFTEN 1'b1 x x SCSYSLOCK */
1864 0x000b, /* SCSYSLOCKCHKTIME[7:0] (10msec) 01100100 */
1865 0x000d, /* x SAMPLING4 */
1866 0x000e, /* SAMFREQ[15:8] 00000000 */
1867 0x000f, /* SAMFREQ[7:0] 00000000 */
Michael Ira Krufkycb4671c2014-10-25 11:12:25 -03001868 0x0010, /* IFFREQ[15:8] 01100000 */
1869 0x0011, /* IFFREQ[7:0] 00000000 */
1870 0x0012, /* AGCEN AGCREFMO */
1871 0x0013, /* AGCRFFIXB AGCIFFIXB AGCLOCKDETRNGSEL[1:0] 1'b1 1'b0 1'b0 1'b0 11101000 */
1872 0x0014, /* AGCFIXVALUE[7:0] 01111111 */
1873 0x0015, /* AGCREF[15:8] 00001010 */
1874 0x0016, /* AGCREF[7:0] 11100100 */
1875 0x0017, /* AGCDELAY[7:0] 00100000 */
1876 0x0018, /* AGCRFBW[3:0] AGCIFBW[3:0] 10001000 */
1877 0x0019, /* AGCUDOUTMODE[1:0] AGCUDCTRLLEN[1:0] AGCUDCTRL */
Mauro Carvalho Chehab4937ba92014-10-28 10:50:36 -02001878 0x001c, /* 1'b1 PFEN MFEN AICCVSYNC */
1879 0x001d, /* 1'b0 1'b1 1'b0 1'b1 AICCVSYNC */
1880 0x001e, /* AICCALPHA[3:0] 1'b1 1'b0 1'b1 1'b0 01111010 */
1881 0x001f, /* AICCDETTH[19:16] AICCOFFTH[19:16] 00000000 */
Michael Ira Krufkycb4671c2014-10-25 11:12:25 -03001882 0x0020, /* AICCDETTH[15:8] 01111100 */
1883 0x0021, /* AICCDETTH[7:0] 00000000 */
1884 0x0022, /* AICCOFFTH[15:8] 00000101 */
1885 0x0023, /* AICCOFFTH[7:0] 11100000 */
1886 0x0024, /* AICCOPMODE3[1:0] AICCOPMODE2[1:0] AICCOPMODE1[1:0] AICCOPMODE0[1:0] 00000000 */
1887 0x0025, /* AICCFIXFREQ3[23:16] 00000000 */
1888 0x0026, /* AICCFIXFREQ3[15:8] 00000000 */
1889 0x0027, /* AICCFIXFREQ3[7:0] 00000000 */
1890 0x0028, /* AICCFIXFREQ2[23:16] 00000000 */
1891 0x0029, /* AICCFIXFREQ2[15:8] 00000000 */
Mauro Carvalho Chehab4937ba92014-10-28 10:50:36 -02001892 0x002a, /* AICCFIXFREQ2[7:0] 00000000 */
1893 0x002b, /* AICCFIXFREQ1[23:16] 00000000 */
1894 0x002c, /* AICCFIXFREQ1[15:8] 00000000 */
1895 0x002d, /* AICCFIXFREQ1[7:0] 00000000 */
1896 0x002e, /* AICCFIXFREQ0[23:16] 00000000 */
1897 0x002f, /* AICCFIXFREQ0[15:8] 00000000 */
Michael Ira Krufkycb4671c2014-10-25 11:12:25 -03001898 0x0030, /* AICCFIXFREQ0[7:0] 00000000 */
1899 0x0031, /* 1'b0 1'b1 1'b0 1'b0 x DAGC1STER */
1900 0x0032, /* DAGC1STEN DAGC1STER */
1901 0x0033, /* DAGC1STREF[15:8] 00001010 */
1902 0x0034, /* DAGC1STREF[7:0] 11100100 */
1903 0x0035, /* DAGC2NDE */
1904 0x0036, /* DAGC2NDREF[15:8] 00001010 */
1905 0x0037, /* DAGC2NDREF[7:0] 10000000 */
1906 0x0038, /* DAGC2NDLOCKDETRNGSEL[1:0] */
Mauro Carvalho Chehab4937ba92014-10-28 10:50:36 -02001907 0x003d, /* 1'b1 SAMGEARS */
Michael Ira Krufkycb4671c2014-10-25 11:12:25 -03001908 0x0040, /* SAMLFGMA */
1909 0x0041, /* SAMLFBWM */
1910 0x0044, /* 1'b1 CRGEARSHE */
1911 0x0045, /* CRLFGMAN */
1912 0x0046, /* CFLFBWMA */
1913 0x0047, /* CRLFGMAN */
1914 0x0048, /* x x x x CRLFGSTEP_VS[3:0] xxxx1001 */
1915 0x0049, /* CRLFBWMA */
Mauro Carvalho Chehab4937ba92014-10-28 10:50:36 -02001916 0x004a, /* CRLFBWMA */
Michael Ira Krufkycb4671c2014-10-25 11:12:25 -03001917 0x0050, /* 1'b0 1'b1 1'b1 1'b0 MSECALCDA */
1918 0x0070, /* TPOUTEN TPIFEN TPCLKOUTE */
1919 0x0071, /* TPSENB TPSSOPBITE */
1920 0x0073, /* TP47HINS x x CHBERINT PERMODE[1:0] PERINT[1:0] 1xx11100 */
1921 0x0075, /* x x x x x IQSWAPCTRL[2:0] xxxxx000 */
1922 0x0076, /* NBERCON NBERST NBERPOL NBERWSYN */
1923 0x0077, /* x NBERLOSTTH[2:0] NBERACQTH[3:0] x0000000 */
1924 0x0078, /* NBERPOLY[31:24] 00000000 */
1925 0x0079, /* NBERPOLY[23:16] 00000000 */
Mauro Carvalho Chehab4937ba92014-10-28 10:50:36 -02001926 0x007a, /* NBERPOLY[15:8] 00000000 */
1927 0x007b, /* NBERPOLY[7:0] 00000000 */
1928 0x007c, /* NBERPED[31:24] 00000000 */
1929 0x007d, /* NBERPED[23:16] 00000000 */
1930 0x007e, /* NBERPED[15:8] 00000000 */
1931 0x007f, /* NBERPED[7:0] 00000000 */
Michael Ira Krufkycb4671c2014-10-25 11:12:25 -03001932 0x0080, /* x AGCLOCK DAGCLOCK SYSLOCK x x NEVERLOCK[1:0] */
1933 0x0085, /* SPECINVST */
1934 0x0088, /* SYSLOCKTIME[15:8] */
1935 0x0089, /* SYSLOCKTIME[7:0] */
Mauro Carvalho Chehab4937ba92014-10-28 10:50:36 -02001936 0x008c, /* FECLOCKTIME[15:8] */
1937 0x008d, /* FECLOCKTIME[7:0] */
1938 0x008e, /* AGCACCOUT[15:8] */
1939 0x008f, /* AGCACCOUT[7:0] */
Michael Ira Krufkycb4671c2014-10-25 11:12:25 -03001940 0x0090, /* AICCREJSTATUS[3:0] AICCREJBUSY[3:0] */
1941 0x0091, /* AICCVSYNC */
Mauro Carvalho Chehab4937ba92014-10-28 10:50:36 -02001942 0x009c, /* CARRFREQOFFSET[15:8] */
1943 0x009d, /* CARRFREQOFFSET[7:0] */
1944 0x00a1, /* SAMFREQOFFSET[23:16] */
1945 0x00a2, /* SAMFREQOFFSET[15:8] */
1946 0x00a3, /* SAMFREQOFFSET[7:0] */
1947 0x00a6, /* SYNCLOCK SYNCLOCKH */
Michael Ira Krufky6da7ac92014-10-25 11:05:05 -03001948#if 0 /* covered elsewhere */
Mauro Carvalho Chehab4937ba92014-10-28 10:50:36 -02001949 0x00e8, /* CONSTPWR[15:8] */
1950 0x00e9, /* CONSTPWR[7:0] */
1951 0x00ea, /* BMSE[15:8] */
1952 0x00eb, /* BMSE[7:0] */
1953 0x00ec, /* MSE[15:8] */
1954 0x00ed, /* MSE[7:0] */
1955 0x00ee, /* CONSTI[7:0] */
1956 0x00ef, /* CONSTQ[7:0] */
Fred Richterb63b36f2014-03-24 19:56:00 -03001957#endif
Mauro Carvalho Chehab4937ba92014-10-28 10:50:36 -02001958 0x00f4, /* TPIFTPERRCNT[7:0] */
1959 0x00f5, /* TPCORREC */
1960 0x00f6, /* VBBER[15:8] */
1961 0x00f7, /* VBBER[7:0] */
1962 0x00f8, /* VABER[15:8] */
1963 0x00f9, /* VABER[7:0] */
1964 0x00fa, /* TPERRCNT[7:0] */
1965 0x00fb, /* NBERLOCK x x x x x x x */
1966 0x00fc, /* NBERVALUE[31:24] */
1967 0x00fd, /* NBERVALUE[23:16] */
1968 0x00fe, /* NBERVALUE[15:8] */
1969 0x00ff, /* NBERVALUE[7:0] */
Michael Ira Krufkycb4671c2014-10-25 11:12:25 -03001970 0x1000, /* 1'b0 WODAGCOU */
1971 0x1005, /* x x 1'b1 1'b1 x SRD_Q_QM */
1972 0x1009, /* SRDWAITTIME[7:0] (10msec) 00100011 */
Mauro Carvalho Chehab4937ba92014-10-28 10:50:36 -02001973 0x100a, /* SRDWAITTIME_CQS[7:0] (msec) 01100100 */
1974 0x101a, /* x 1'b1 1'b0 1'b0 x QMDQAMMODE[2:0] x100x010 */
Michael Ira Krufkycb4671c2014-10-25 11:12:25 -03001975 0x1036, /* 1'b0 1'b1 1'b0 1'b0 SAMGSEND_CQS[3:0] 01001110 */
Mauro Carvalho Chehab4937ba92014-10-28 10:50:36 -02001976 0x103c, /* SAMGSAUTOSTL_V[3:0] SAMGSAUTOEDL_V[3:0] 01000110 */
1977 0x103d, /* 1'b1 1'b1 SAMCNORMBP_V[1:0] 1'b0 1'b0 SAMMODESEL_V[1:0] 11100001 */
1978 0x103f, /* SAMZTEDSE */
1979 0x105d, /* EQSTATUSE */
1980 0x105f, /* x PMAPG2_V[2:0] x DMAPG2_V[2:0] x001x011 */
Michael Ira Krufkycb4671c2014-10-25 11:12:25 -03001981 0x1060, /* 1'b1 EQSTATUSE */
1982 0x1061, /* CRMAPBWSTL_V[3:0] CRMAPBWEDL_V[3:0] 00000100 */
1983 0x1065, /* 1'b0 x CRMODE_V[1:0] 1'b1 x 1'b1 x 0x111x1x */
1984 0x1066, /* 1'b0 1'b0 1'b1 1'b0 1'b1 PNBOOSTSE */
1985 0x1068, /* CREPHNGAIN2_V[3:0] CREPHNPBW_V[3:0] 10010001 */
Mauro Carvalho Chehab4937ba92014-10-28 10:50:36 -02001986 0x106e, /* x x x x x CREPHNEN_ */
1987 0x106f, /* CREPHNTH_V[7:0] 00010101 */
Michael Ira Krufkycb4671c2014-10-25 11:12:25 -03001988 0x1072, /* CRSWEEPN */
1989 0x1073, /* CRPGAIN_V[3:0] x x 1'b1 1'b1 1001xx11 */
1990 0x1074, /* CRPBW_V[3:0] x x 1'b1 1'b1 0001xx11 */
1991 0x1080, /* DAFTSTATUS[1:0] x x x x x x */
1992 0x1081, /* SRDSTATUS[1:0] x x x x x SRDLOCK */
Mauro Carvalho Chehab4937ba92014-10-28 10:50:36 -02001993 0x10a9, /* EQSTATUS_CQS[1:0] x x x x x x */
1994 0x10b7, /* EQSTATUS_V[1:0] x x x x x x */
Michael Ira Krufky6da7ac92014-10-25 11:05:05 -03001995#if 0 /* SMART_ANT */
Mauro Carvalho Chehab4937ba92014-10-28 10:50:36 -02001996 0x1f00, /* MODEDETE */
1997 0x1f01, /* x x x x x x x SFNRST xxxxxxx0 */
1998 0x1f03, /* NUMOFANT[7:0] 10000000 */
1999 0x1f04, /* x SELMASK[6:0] x0000000 */
2000 0x1f05, /* x SETMASK[6:0] x0000000 */
2001 0x1f06, /* x TXDATA[6:0] x0000000 */
2002 0x1f07, /* x CHNUMBER[6:0] x0000000 */
2003 0x1f09, /* AGCTIME[23:16] 10011000 */
2004 0x1f0a, /* AGCTIME[15:8] 10010110 */
2005 0x1f0b, /* AGCTIME[7:0] 10000000 */
2006 0x1f0c, /* ANTTIME[31:24] 00000000 */
2007 0x1f0d, /* ANTTIME[23:16] 00000011 */
2008 0x1f0e, /* ANTTIME[15:8] 10010000 */
2009 0x1f0f, /* ANTTIME[7:0] 10010000 */
2010 0x1f11, /* SYNCTIME[23:16] 10011000 */
2011 0x1f12, /* SYNCTIME[15:8] 10010110 */
2012 0x1f13, /* SYNCTIME[7:0] 10000000 */
2013 0x1f14, /* SNRTIME[31:24] 00000001 */
2014 0x1f15, /* SNRTIME[23:16] 01111101 */
2015 0x1f16, /* SNRTIME[15:8] 01111000 */
2016 0x1f17, /* SNRTIME[7:0] 01000000 */
2017 0x1f19, /* FECTIME[23:16] 00000000 */
2018 0x1f1a, /* FECTIME[15:8] 01110010 */
2019 0x1f1b, /* FECTIME[7:0] 01110000 */
2020 0x1f1d, /* FECTHD[7:0] 00000011 */
2021 0x1f1f, /* SNRTHD[23:16] 00001000 */
2022 0x1f20, /* SNRTHD[15:8] 01111111 */
2023 0x1f21, /* SNRTHD[7:0] 10000101 */
2024 0x1f80, /* IRQFLG x x SFSDRFLG MODEBFLG SAVEFLG SCANFLG TRACKFLG */
2025 0x1f81, /* x SYNCCON SNRCON FECCON x STDBUSY SYNCRST AGCFZCO */
2026 0x1f82, /* x x x SCANOPCD[4:0] */
2027 0x1f83, /* x x x x MAINOPCD[3:0] */
2028 0x1f84, /* x x RXDATA[13:8] */
2029 0x1f85, /* RXDATA[7:0] */
2030 0x1f86, /* x x SDTDATA[13:8] */
2031 0x1f87, /* SDTDATA[7:0] */
2032 0x1f89, /* ANTSNR[23:16] */
2033 0x1f8a, /* ANTSNR[15:8] */
2034 0x1f8b, /* ANTSNR[7:0] */
2035 0x1f8c, /* x x x x ANTFEC[13:8] */
2036 0x1f8d, /* ANTFEC[7:0] */
2037 0x1f8e, /* MAXCNT[7:0] */
2038 0x1f8f, /* SCANCNT[7:0] */
2039 0x1f91, /* MAXPW[23:16] */
2040 0x1f92, /* MAXPW[15:8] */
2041 0x1f93, /* MAXPW[7:0] */
2042 0x1f95, /* CURPWMSE[23:16] */
2043 0x1f96, /* CURPWMSE[15:8] */
2044 0x1f97, /* CURPWMSE[7:0] */
Michael Ira Krufky6da7ac92014-10-25 11:05:05 -03002045#endif /* SMART_ANT */
Mauro Carvalho Chehab4937ba92014-10-28 10:50:36 -02002046 0x211f, /* 1'b1 1'b1 1'b1 CIRQEN x x 1'b0 1'b0 1111xx00 */
2047 0x212a, /* EQAUTOST */
Michael Ira Krufkycb4671c2014-10-25 11:12:25 -03002048 0x2122, /* CHFAST[7:0] 01100000 */
Mauro Carvalho Chehab4937ba92014-10-28 10:50:36 -02002049 0x212b, /* FFFSTEP_V[3:0] x FBFSTEP_V[2:0] 0001x001 */
2050 0x212c, /* PHDEROTBWSEL[3:0] 1'b1 1'b1 1'b1 1'b0 10001110 */
2051 0x212d, /* 1'b1 1'b1 1'b1 1'b1 x x TPIFLOCKS */
Michael Ira Krufkycb4671c2014-10-25 11:12:25 -03002052 0x2135, /* DYNTRACKFDEQ[3:0] x 1'b0 1'b0 1'b0 1010x000 */
2053 0x2141, /* TRMODE[1:0] 1'b1 1'b1 1'b0 1'b1 1'b1 1'b1 01110111 */
2054 0x2162, /* AICCCTRLE */
2055 0x2173, /* PHNCNFCNT[7:0] 00000100 */
2056 0x2179, /* 1'b0 1'b0 1'b0 1'b1 x BADSINGLEDYNTRACKFBF[2:0] 0001x001 */
Mauro Carvalho Chehab4937ba92014-10-28 10:50:36 -02002057 0x217a, /* 1'b0 1'b0 1'b0 1'b1 x BADSLOWSINGLEDYNTRACKFBF[2:0] 0001x001 */
2058 0x217e, /* CNFCNTTPIF[7:0] 00001000 */
2059 0x217f, /* TPERRCNTTPIF[7:0] 00000001 */
Michael Ira Krufkycb4671c2014-10-25 11:12:25 -03002060 0x2180, /* x x x x x x FBDLYCIR[9:8] */
2061 0x2181, /* FBDLYCIR[7:0] */
2062 0x2185, /* MAXPWRMAIN[7:0] */
2063 0x2191, /* NCOMBDET x x x x x x x */
2064 0x2199, /* x MAINSTRON */
Mauro Carvalho Chehab4937ba92014-10-28 10:50:36 -02002065 0x219a, /* FFFEQSTEPOUT_V[3:0] FBFSTEPOUT_V[2:0] */
2066 0x21a1, /* x x SNRREF[5:0] */
Michael Ira Krufkycb4671c2014-10-25 11:12:25 -03002067 0x2845, /* 1'b0 1'b1 x x FFFSTEP_CQS[1:0] FFFCENTERTAP[1:0] 01xx1110 */
2068 0x2846, /* 1'b0 x 1'b0 1'b1 FBFSTEP_CQS[1:0] 1'b1 1'b0 0x011110 */
2069 0x2847, /* ENNOSIGDE */
2070 0x2849, /* 1'b1 1'b1 NOUSENOSI */
Mauro Carvalho Chehab4937ba92014-10-28 10:50:36 -02002071 0x284a, /* EQINITWAITTIME[7:0] 01100100 */
Michael Ira Krufkycb4671c2014-10-25 11:12:25 -03002072 0x3000, /* 1'b1 1'b1 1'b1 x x x 1'b0 RPTRSTM */
2073 0x3001, /* RPTRSTWAITTIME[7:0] (100msec) 00110010 */
2074 0x3031, /* FRAMELOC */
2075 0x3032, /* 1'b1 1'b0 1'b0 1'b0 x x FRAMELOCKMODE_CQS[1:0] 1000xx11 */
Mauro Carvalho Chehab4937ba92014-10-28 10:50:36 -02002076 0x30a9, /* VDLOCK_Q FRAMELOCK */
2077 0x30aa, /* MPEGLOCK */
Fred Richterb63b36f2014-03-24 19:56:00 -03002078};
2079
Michael Ira Krufky34a5a2f2014-10-25 11:26:15 -03002080#define numDumpRegs (sizeof(regtab)/sizeof(regtab[0]))
Fred Richterb63b36f2014-03-24 19:56:00 -03002081static u8 regval1[numDumpRegs] = {0, };
2082static u8 regval2[numDumpRegs] = {0, };
2083
2084static void lgdt3306a_DumpAllRegs(struct lgdt3306a_state *state)
2085{
2086 memset(regval2, 0xff, sizeof(regval2));
2087 lgdt3306a_DumpRegs(state);
2088}
2089
2090static void lgdt3306a_DumpRegs(struct lgdt3306a_state *state)
2091{
2092 int i;
2093 int sav_debug = debug;
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -03002094
Fred Richterb63b36f2014-03-24 19:56:00 -03002095 if ((debug & DBG_DUMP) == 0)
2096 return;
Michael Ira Krufky831a9112014-10-25 11:20:57 -03002097 debug &= ~DBG_REG; /* suppress DBG_REG during reg dump */
Fred Richterb63b36f2014-03-24 19:56:00 -03002098
Mauro Carvalho Chehab097117c2014-10-28 11:35:16 -02002099 lg_debug("\n");
Fred Richterb63b36f2014-03-24 19:56:00 -03002100
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -03002101 for (i = 0; i < numDumpRegs; i++) {
Fred Richterb63b36f2014-03-24 19:56:00 -03002102 lgdt3306a_read_reg(state, regtab[i], &regval1[i]);
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -03002103 if (regval1[i] != regval2[i]) {
Mauro Carvalho Chehab097117c2014-10-28 11:35:16 -02002104 lg_debug(" %04X = %02X\n", regtab[i], regval1[i]);
Mauro Carvalho Chehab16afc672015-04-28 18:31:21 -03002105 regval2[i] = regval1[i];
Fred Richterb63b36f2014-03-24 19:56:00 -03002106 }
2107 }
2108 debug = sav_debug;
2109}
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -03002110#endif /* DBG_DUMP */
Fred Richterb63b36f2014-03-24 19:56:00 -03002111
2112
2113
Fred Richterb63b36f2014-03-24 19:56:00 -03002114static struct dvb_frontend_ops lgdt3306a_ops = {
2115 .delsys = { SYS_ATSC, SYS_DVBC_ANNEX_B },
2116 .info = {
2117 .name = "LG Electronics LGDT3306A VSB/QAM Frontend",
Fred Richterb63b36f2014-03-24 19:56:00 -03002118 .frequency_min = 54000000,
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -03002119 .frequency_max = 858000000,
Fred Richterb63b36f2014-03-24 19:56:00 -03002120 .frequency_stepsize = 62500,
2121 .caps = FE_CAN_QAM_64 | FE_CAN_QAM_256 | FE_CAN_8VSB
2122 },
2123 .i2c_gate_ctrl = lgdt3306a_i2c_gate_ctrl,
2124 .init = lgdt3306a_init,
2125 .sleep = lgdt3306a_fe_sleep,
2126 /* if this is set, it overrides the default swzigzag */
2127 .tune = lgdt3306a_tune,
2128 .set_frontend = lgdt3306a_set_parameters,
2129 .get_frontend = lgdt3306a_get_frontend,
2130 .get_frontend_algo = lgdt3306a_get_frontend_algo,
2131 .get_tune_settings = lgdt3306a_get_tune_settings,
2132 .read_status = lgdt3306a_read_status,
2133 .read_ber = lgdt3306a_read_ber,
2134 .read_signal_strength = lgdt3306a_read_signal_strength,
2135 .read_snr = lgdt3306a_read_snr,
2136 .read_ucblocks = lgdt3306a_read_ucblocks,
2137 .release = lgdt3306a_release,
2138 .ts_bus_ctrl = lgdt3306a_ts_bus_ctrl,
2139 .search = lgdt3306a_search,
2140};
2141
2142MODULE_DESCRIPTION("LG Electronics LGDT3306A ATSC/QAM-B Demodulator Driver");
2143MODULE_AUTHOR("Fred Richter <frichter@hauppauge.com>");
2144MODULE_LICENSE("GPL");
2145MODULE_VERSION("0.2");