blob: 3451e77a277194bca4bd39706239d34cf4bbb232 [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.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 *
22 */
23
24#include <asm/div64.h>
25#include <linux/dvb/frontend.h>
26#include "dvb_math.h"
27#include "lgdt3306a.h"
28
29
30static int debug;
31module_param(debug, int, 0644);
32MODULE_PARM_DESC(debug, "set debug level (info=1, reg=2 (or-able))");
33
34#define DBG_INFO 1
35#define DBG_REG 2
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -030036#define DBG_DUMP 4 /* FGR - comment out to remove dump code */
Fred Richterb63b36f2014-03-24 19:56:00 -030037
38#define lg_printk(kern, fmt, arg...) \
39 printk(kern "%s(): " fmt, __func__, ##arg)
40
41#define lg_info(fmt, arg...) printk(KERN_INFO "lgdt3306a: " fmt, ##arg)
42#define lg_warn(fmt, arg...) lg_printk(KERN_WARNING, fmt, ##arg)
43#define lg_err(fmt, arg...) lg_printk(KERN_ERR, fmt, ##arg)
44#define lg_dbg(fmt, arg...) if (debug & DBG_INFO) \
45 lg_printk(KERN_DEBUG, fmt, ##arg)
46#define lg_reg(fmt, arg...) if (debug & DBG_REG) \
47 lg_printk(KERN_DEBUG, fmt, ##arg)
48
49#define lg_chkerr(ret) \
50({ \
51 int __ret; \
52 __ret = (ret < 0); \
53 if (__ret) \
54 lg_err("error %d on line %d\n", ret, __LINE__); \
55 __ret; \
56})
57
58struct lgdt3306a_state {
59 struct i2c_adapter *i2c_adap;
60 const struct lgdt3306a_config *cfg;
61
62 struct dvb_frontend frontend;
63
64 fe_modulation_t current_modulation;
65 u32 current_frequency;
66 u32 snr;
67};
68
69/* -----------------------------------------------
70 LG3306A Register Usage
71 (LG does not really name the registers, so this code does not either)
72 0000 -> 00FF Common control and status
73 1000 -> 10FF Synchronizer control and status
74 1F00 -> 1FFF Smart Antenna control and status
75 2100 -> 21FF VSB Equalizer control and status
76 2800 -> 28FF QAM Equalizer control and status
77 3000 -> 30FF FEC control and status
78 ---------------------------------------------- */
79
80typedef enum{
81 LG3306_UNLOCK = 0x00,
82 LG3306_LOCK = 0x01,
83 LG3306_UNKNOWN_LOCK = 0xFF
84}LG3306_LOCK_STATUS;
85
86typedef enum{
87 LG3306_NL_INIT = 0x00,
88 LG3306_NL_PROCESS = 0x01,
89 LG3306_NL_LOCK = 0x02,
90 LG3306_NL_FAIL = 0x03,
91 LG3306_NL_UNKNOWN = 0xFF
92}LG3306_NEVERLOCK_STATUS;
93
94typedef enum{
95 LG3306_VSB = 0x00,
96 LG3306_QAM64 = 0x01,
97 LG3306_QAM256 = 0x02,
98 LG3306_UNKNOWN_MODE = 0xFF
99}LG3306_MODULATION;
100
101typedef enum
102{
103 LG3306_SYNC_LOCK,
104 LG3306_FEC_LOCK,
105 LG3306_TR_LOCK,
106 LG3306_AGC_LOCK,
107} LG3306_LOCK_CHECK;
108
109
110#ifdef DBG_DUMP
111static void lgdt3306a_DumpAllRegs(struct lgdt3306a_state *state);
112static void lgdt3306a_DumpRegs(struct lgdt3306a_state *state);
113#endif
114
115
116static int lgdt3306a_write_reg(struct lgdt3306a_state *state, u16 reg, u8 val)
117{
118 int ret;
119 u8 buf[] = { reg >> 8, reg & 0xff, val };
120 struct i2c_msg msg = {
121 .addr = state->cfg->i2c_addr, .flags = 0,
122 .buf = buf, .len = 3,
123 };
124
125 lg_reg("reg: 0x%04x, val: 0x%02x\n", reg, val);
126
127 ret = i2c_transfer(state->i2c_adap, &msg, 1);
128
129 if (ret != 1) {
130 lg_err("error (addr %02x %02x <- %02x, err = %i)\n",
131 msg.buf[0], msg.buf[1], msg.buf[2], ret);
132 if (ret < 0)
133 return ret;
134 else
135 return -EREMOTEIO;
136 }
137 return 0;
138}
139
140static int lgdt3306a_read_reg(struct lgdt3306a_state *state, u16 reg, u8 *val)
141{
142 int ret;
143 u8 reg_buf[] = { reg >> 8, reg & 0xff };
144 struct i2c_msg msg[] = {
145 { .addr = state->cfg->i2c_addr,
146 .flags = 0, .buf = reg_buf, .len = 2 },
147 { .addr = state->cfg->i2c_addr,
148 .flags = I2C_M_RD, .buf = val, .len = 1 },
149 };
150
151 ret = i2c_transfer(state->i2c_adap, msg, 2);
152
153 if (ret != 2) {
154 lg_err("error (addr %02x reg %04x error (ret == %i)\n",
155 state->cfg->i2c_addr, reg, ret);
156 if (ret < 0)
157 return ret;
158 else
159 return -EREMOTEIO;
160 }
161 lg_reg("reg: 0x%04x, val: 0x%02x\n", reg, *val);
162
163 return 0;
164}
165
166#define read_reg(state, reg) \
167({ \
168 u8 __val; \
169 int ret = lgdt3306a_read_reg(state, reg, &__val); \
170 if (lg_chkerr(ret)) \
171 __val = 0; \
172 __val; \
173})
174
175static int lgdt3306a_set_reg_bit(struct lgdt3306a_state *state,
176 u16 reg, int bit, int onoff)
177{
178 u8 val;
179 int ret;
180
181 lg_reg("reg: 0x%04x, bit: %d, level: %d\n", reg, bit, onoff);
182
183 ret = lgdt3306a_read_reg(state, reg, &val);
184 if (lg_chkerr(ret))
185 goto fail;
186
187 val &= ~(1 << bit);
188 val |= (onoff & 1) << bit;
189
190 ret = lgdt3306a_write_reg(state, reg, val);
191 lg_chkerr(ret);
192fail:
193 return ret;
194}
195
196/* ------------------------------------------------------------------------ */
197
198static int lgdt3306a_soft_reset(struct lgdt3306a_state *state)
199{
200 int ret;
201
202 lg_dbg("\n");
203
204 ret = lgdt3306a_set_reg_bit(state, 0x0000, 7, 0);
205 if (lg_chkerr(ret))
206 goto fail;
207
208 msleep(20);
209 ret = lgdt3306a_set_reg_bit(state, 0x0000, 7, 1);
210 lg_chkerr(ret);
211
212fail:
213 return ret;
214}
215
216static int lgdt3306a_mpeg_mode(struct lgdt3306a_state *state,
217 enum lgdt3306a_mpeg_mode mode)
218{
219 u8 val;
220 int ret;
221
222 lg_dbg("(%d)\n", mode);
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -0300223 /* transport packet format */
224 ret = lgdt3306a_set_reg_bit(state, 0x0071, 7, mode == LGDT3306A_MPEG_PARALLEL?1:0); /* TPSENB=0x80 */
Fred Richterb63b36f2014-03-24 19:56:00 -0300225 if (lg_chkerr(ret))
226 goto fail;
227
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -0300228 /* start of packet signal duration */
229 ret = lgdt3306a_set_reg_bit(state, 0x0071, 6, 0); /* TPSSOPBITEN=0x40; 0=byte duration, 1=bit duration */
Fred Richterb63b36f2014-03-24 19:56:00 -0300230 if (lg_chkerr(ret))
231 goto fail;
232
233 ret = lgdt3306a_read_reg(state, 0x0070, &val);
234 if (lg_chkerr(ret))
235 goto fail;
236
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -0300237 val |= 0x10; /* TPCLKSUPB=0x10 */
Fred Richterb63b36f2014-03-24 19:56:00 -0300238
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -0300239 if (mode == LGDT3306A_MPEG_PARALLEL)
Fred Richterb63b36f2014-03-24 19:56:00 -0300240 val &= ~0x10;
241
242 ret = lgdt3306a_write_reg(state, 0x0070, val);
243 lg_chkerr(ret);
244
245fail:
246 return ret;
247}
248
249static int lgdt3306a_mpeg_mode_polarity(struct lgdt3306a_state *state,
250 enum lgdt3306a_tp_clock_edge edge,
251 enum lgdt3306a_tp_valid_polarity valid)
252{
253 u8 val;
254 int ret;
255
256 lg_dbg("edge=%d, valid=%d\n", edge, valid);
257
258 ret = lgdt3306a_read_reg(state, 0x0070, &val);
259 if (lg_chkerr(ret))
260 goto fail;
261
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -0300262 val &= ~0x06; /* TPCLKPOL=0x04, TPVALPOL=0x02 */
Fred Richterb63b36f2014-03-24 19:56:00 -0300263
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -0300264 if (edge == LGDT3306A_TPCLK_RISING_EDGE)
Fred Richterb63b36f2014-03-24 19:56:00 -0300265 val |= 0x04;
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -0300266 if (valid == LGDT3306A_TP_VALID_HIGH)
Fred Richterb63b36f2014-03-24 19:56:00 -0300267 val |= 0x02;
268
269 ret = lgdt3306a_write_reg(state, 0x0070, val);
270 lg_chkerr(ret);
271
272fail:
273 return ret;
274}
275
276static int lgdt3306a_mpeg_tristate(struct lgdt3306a_state *state,
277 int mode)
278{
279 u8 val;
280 int ret;
281
282 lg_dbg("(%d)\n", mode);
283
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -0300284 if (mode) {
Fred Richterb63b36f2014-03-24 19:56:00 -0300285 ret = lgdt3306a_read_reg(state, 0x0070, &val);
286 if (lg_chkerr(ret))
287 goto fail;
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -0300288 val &= ~0xA8; /* Tristate bus; TPOUTEN=0x80, TPCLKOUTEN=0x20, TPDATAOUTEN=0x08 */
Fred Richterb63b36f2014-03-24 19:56:00 -0300289 ret = lgdt3306a_write_reg(state, 0x0070, val);
290 if (lg_chkerr(ret))
291 goto fail;
292
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -0300293 ret = lgdt3306a_set_reg_bit(state, 0x0003, 6, 1); /* AGCIFOUTENB=0x40; 1=Disable IFAGC pin */
Fred Richterb63b36f2014-03-24 19:56:00 -0300294 if (lg_chkerr(ret))
295 goto fail;
296
297 } else {
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -0300298 ret = lgdt3306a_set_reg_bit(state, 0x0003, 6, 0); /* enable IFAGC pin */
Fred Richterb63b36f2014-03-24 19:56:00 -0300299 if (lg_chkerr(ret))
300 goto fail;
301
302 ret = lgdt3306a_read_reg(state, 0x0070, &val);
303 if (lg_chkerr(ret))
304 goto fail;
305
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -0300306 val |= 0xA8; /* enable bus */
Fred Richterb63b36f2014-03-24 19:56:00 -0300307 ret = lgdt3306a_write_reg(state, 0x0070, val);
308 if (lg_chkerr(ret))
309 goto fail;
310 }
311
312fail:
313 return ret;
314}
315
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -0300316static int lgdt3306a_ts_bus_ctrl(struct dvb_frontend *fe, int acquire)
Fred Richterb63b36f2014-03-24 19:56:00 -0300317{
318 struct lgdt3306a_state *state = fe->demodulator_priv;
319
320 lg_dbg("acquire=%d\n", acquire);
321
322 return lgdt3306a_mpeg_tristate(state, acquire ? 0 : 1);
323
324}
325
326static int lgdt3306a_power(struct lgdt3306a_state *state,
327 int mode)
328{
329 int ret;
330
331 lg_dbg("(%d)\n", mode);
332
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -0300333 if (mode == 0) {
334 ret = lgdt3306a_set_reg_bit(state, 0x0000, 7, 0); /* into reset */
Fred Richterb63b36f2014-03-24 19:56:00 -0300335 if (lg_chkerr(ret))
336 goto fail;
337
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -0300338 ret = lgdt3306a_set_reg_bit(state, 0x0000, 0, 0); /* power down */
Fred Richterb63b36f2014-03-24 19:56:00 -0300339 if (lg_chkerr(ret))
340 goto fail;
341
342 } else {
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -0300343 ret = lgdt3306a_set_reg_bit(state, 0x0000, 7, 1); /* out of reset */
Fred Richterb63b36f2014-03-24 19:56:00 -0300344 if (lg_chkerr(ret))
345 goto fail;
346
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -0300347 ret = lgdt3306a_set_reg_bit(state, 0x0000, 0, 1); /* power up */
Fred Richterb63b36f2014-03-24 19:56:00 -0300348 if (lg_chkerr(ret))
349 goto fail;
350 }
351
352#ifdef DBG_DUMP
353 lgdt3306a_DumpAllRegs(state);
354#endif
355fail:
356 return ret;
357}
358
359
360static int lgdt3306a_set_vsb(struct lgdt3306a_state *state)
361{
362 u8 val;
363 int ret;
364
365 lg_dbg("\n");
366
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -0300367 /* 0. Spectrum inversion detection manual; spectrum inverted */
Fred Richterb63b36f2014-03-24 19:56:00 -0300368 ret = lgdt3306a_read_reg(state, 0x0002, &val);
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -0300369 val &= 0xF7; /* SPECINVAUTO Off */
370 val |= 0x04; /* SPECINV On */
Fred Richterb63b36f2014-03-24 19:56:00 -0300371 ret = lgdt3306a_write_reg(state, 0x0002, val);
372 if (lg_chkerr(ret))
373 goto fail;
374
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -0300375 /* 1. Selection of standard mode(0x08=QAM, 0x80=VSB) */
Fred Richterb63b36f2014-03-24 19:56:00 -0300376 ret = lgdt3306a_write_reg(state, 0x0008, 0x80);
377 if (lg_chkerr(ret))
378 goto fail;
379
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -0300380 /* 2. Bandwidth mode for VSB(6MHz) */
Fred Richterb63b36f2014-03-24 19:56:00 -0300381 ret = lgdt3306a_read_reg(state, 0x0009, &val);
382 val &= 0xE3;
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -0300383 val |= 0x0C; /* STDOPDETTMODE[2:0]=3 */
Fred Richterb63b36f2014-03-24 19:56:00 -0300384 ret = lgdt3306a_write_reg(state, 0x0009, val);
385 if (lg_chkerr(ret))
386 goto fail;
387
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -0300388 /* 3. QAM mode detection mode(None) */
Fred Richterb63b36f2014-03-24 19:56:00 -0300389 ret = lgdt3306a_read_reg(state, 0x0009, &val);
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -0300390 val &= 0xFC; /* STDOPDETCMODE[1:0]=0 */
Fred Richterb63b36f2014-03-24 19:56:00 -0300391 ret = lgdt3306a_write_reg(state, 0x0009, val);
392 if (lg_chkerr(ret))
393 goto fail;
394
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -0300395 /* 4. ADC sampling frequency rate(2x sampling) */
Fred Richterb63b36f2014-03-24 19:56:00 -0300396 ret = lgdt3306a_read_reg(state, 0x000D, &val);
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -0300397 val &= 0xBF; /* SAMPLING4XFEN=0 */
Fred Richterb63b36f2014-03-24 19:56:00 -0300398 ret = lgdt3306a_write_reg(state, 0x000D, val);
399 if (lg_chkerr(ret))
400 goto fail;
401
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -0300402#if 0
403 /* FGR - disable any AICC filtering, testing only */
404
Fred Richterb63b36f2014-03-24 19:56:00 -0300405 ret = lgdt3306a_write_reg(state, 0x0024, 0x00);
406 if (lg_chkerr(ret))
407 goto fail;
408
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -0300409 /* AICCFIXFREQ0 NT N-1(Video rejection) */
Fred Richterb63b36f2014-03-24 19:56:00 -0300410 ret = lgdt3306a_write_reg(state, 0x002E, 0x00);
411 ret = lgdt3306a_write_reg(state, 0x002F, 0x00);
412 ret = lgdt3306a_write_reg(state, 0x0030, 0x00);
413
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -0300414 /* AICCFIXFREQ1 NT N-1(Audio rejection) */
Fred Richterb63b36f2014-03-24 19:56:00 -0300415 ret = lgdt3306a_write_reg(state, 0x002B, 0x00);
416 ret = lgdt3306a_write_reg(state, 0x002C, 0x00);
417 ret = lgdt3306a_write_reg(state, 0x002D, 0x00);
418
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -0300419 /* AICCFIXFREQ2 NT Co-Channel(Video rejection) */
Fred Richterb63b36f2014-03-24 19:56:00 -0300420 ret = lgdt3306a_write_reg(state, 0x0028, 0x00);
421 ret = lgdt3306a_write_reg(state, 0x0029, 0x00);
422 ret = lgdt3306a_write_reg(state, 0x002A, 0x00);
423
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -0300424 /* AICCFIXFREQ3 NT Co-Channel(Audio rejection) */
Fred Richterb63b36f2014-03-24 19:56:00 -0300425 ret = lgdt3306a_write_reg(state, 0x0025, 0x00);
426 ret = lgdt3306a_write_reg(state, 0x0026, 0x00);
427 ret = lgdt3306a_write_reg(state, 0x0027, 0x00);
428
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -0300429#else
430 /* FGR - this works well for HVR-1955,1975 */
431
432 /* 5. AICCOPMODE NT N-1 Adj. */
Fred Richterb63b36f2014-03-24 19:56:00 -0300433 ret = lgdt3306a_write_reg(state, 0x0024, 0x5A);
434 if (lg_chkerr(ret))
435 goto fail;
436
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -0300437 /* AICCFIXFREQ0 NT N-1(Video rejection) */
Fred Richterb63b36f2014-03-24 19:56:00 -0300438 ret = lgdt3306a_write_reg(state, 0x002E, 0x5A);
439 ret = lgdt3306a_write_reg(state, 0x002F, 0x00);
440 ret = lgdt3306a_write_reg(state, 0x0030, 0x00);
441
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -0300442 /* AICCFIXFREQ1 NT N-1(Audio rejection) */
Fred Richterb63b36f2014-03-24 19:56:00 -0300443 ret = lgdt3306a_write_reg(state, 0x002B, 0x36);
444 ret = lgdt3306a_write_reg(state, 0x002C, 0x00);
445 ret = lgdt3306a_write_reg(state, 0x002D, 0x00);
446
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -0300447 /* AICCFIXFREQ2 NT Co-Channel(Video rejection) */
Fred Richterb63b36f2014-03-24 19:56:00 -0300448 ret = lgdt3306a_write_reg(state, 0x0028, 0x2A);
449 ret = lgdt3306a_write_reg(state, 0x0029, 0x00);
450 ret = lgdt3306a_write_reg(state, 0x002A, 0x00);
451
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -0300452 /* AICCFIXFREQ3 NT Co-Channel(Audio rejection) */
Fred Richterb63b36f2014-03-24 19:56:00 -0300453 ret = lgdt3306a_write_reg(state, 0x0025, 0x06);
454 ret = lgdt3306a_write_reg(state, 0x0026, 0x00);
455 ret = lgdt3306a_write_reg(state, 0x0027, 0x00);
456#endif
457
458 ret = lgdt3306a_read_reg(state, 0x001E, &val);
459 val &= 0x0F;
460 val |= 0xA0;
461 ret = lgdt3306a_write_reg(state, 0x001E, val);
462
463 ret = lgdt3306a_write_reg(state, 0x0022, 0x08);
464
465 ret = lgdt3306a_write_reg(state, 0x0023, 0xFF);
466
467 ret = lgdt3306a_read_reg(state, 0x211F, &val);
468 val &= 0xEF;
469 ret = lgdt3306a_write_reg(state, 0x211F, val);
470
471 ret = lgdt3306a_write_reg(state, 0x2173, 0x01);
472
473 ret = lgdt3306a_read_reg(state, 0x1061, &val);
474 val &= 0xF8;
475 val |= 0x04;
476 ret = lgdt3306a_write_reg(state, 0x1061, val);
477
478 ret = lgdt3306a_read_reg(state, 0x103D, &val);
479 val &= 0xCF;
480 ret = lgdt3306a_write_reg(state, 0x103D, val);
481
482 ret = lgdt3306a_write_reg(state, 0x2122, 0x40);
483
484 ret = lgdt3306a_read_reg(state, 0x2141, &val);
485 val &= 0x3F;
486 ret = lgdt3306a_write_reg(state, 0x2141, val);
487
488 ret = lgdt3306a_read_reg(state, 0x2135, &val);
489 val &= 0x0F;
490 val |= 0x70;
491 ret = lgdt3306a_write_reg(state, 0x2135, val);
492
493 ret = lgdt3306a_read_reg(state, 0x0003, &val);
494 val &= 0xF7;
495 ret = lgdt3306a_write_reg(state, 0x0003, val);
496
497 ret = lgdt3306a_read_reg(state, 0x001C, &val);
498 val &= 0x7F;
499 ret = lgdt3306a_write_reg(state, 0x001C, val);
500
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -0300501 /* 6. EQ step size */
Fred Richterb63b36f2014-03-24 19:56:00 -0300502 ret = lgdt3306a_read_reg(state, 0x2179, &val);
503 val &= 0xF8;
504 ret = lgdt3306a_write_reg(state, 0x2179, val);
505
506 ret = lgdt3306a_read_reg(state, 0x217A, &val);
507 val &= 0xF8;
508 ret = lgdt3306a_write_reg(state, 0x217A, val);
509
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -0300510 /* 7. Reset */
Fred Richterb63b36f2014-03-24 19:56:00 -0300511 ret = lgdt3306a_soft_reset(state);
512 if (lg_chkerr(ret))
513 goto fail;
514
515 lg_dbg("complete\n");
516fail:
517 return ret;
518}
519
520static int lgdt3306a_set_qam(struct lgdt3306a_state *state, int modulation)
521{
522 u8 val;
523 int ret;
524
525 lg_dbg("modulation=%d\n", modulation);
526
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -0300527 /* 1. Selection of standard mode(0x08=QAM, 0x80=VSB) */
Fred Richterb63b36f2014-03-24 19:56:00 -0300528 ret = lgdt3306a_write_reg(state, 0x0008, 0x08);
529 if (lg_chkerr(ret))
530 goto fail;
531
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -0300532 /* 1a. Spectrum inversion detection to Auto */
Fred Richterb63b36f2014-03-24 19:56:00 -0300533 ret = lgdt3306a_read_reg(state, 0x0002, &val);
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -0300534 val &= 0xFB; /* SPECINV Off */
535 val |= 0x08; /* SPECINVAUTO On */
Fred Richterb63b36f2014-03-24 19:56:00 -0300536 ret = lgdt3306a_write_reg(state, 0x0002, val);
537 if (lg_chkerr(ret))
538 goto fail;
539
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -0300540 /* 2. Bandwidth mode for QAM */
Fred Richterb63b36f2014-03-24 19:56:00 -0300541 ret = lgdt3306a_read_reg(state, 0x0009, &val);
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -0300542 val &= 0xE3; /* STDOPDETTMODE[2:0]=0 VSB Off */
Fred Richterb63b36f2014-03-24 19:56:00 -0300543 ret = lgdt3306a_write_reg(state, 0x0009, val);
544 if (lg_chkerr(ret))
545 goto fail;
546
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -0300547 /* 3. : 64QAM/256QAM detection(manual, auto) */
Fred Richterb63b36f2014-03-24 19:56:00 -0300548 ret = lgdt3306a_read_reg(state, 0x0009, &val);
549 val &= 0xFC;
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -0300550 val |= 0x02; /* STDOPDETCMODE[1:0]=1=Manual 2=Auto */
Fred Richterb63b36f2014-03-24 19:56:00 -0300551 ret = lgdt3306a_write_reg(state, 0x0009, val);
552 if (lg_chkerr(ret))
553 goto fail;
554
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -0300555 /* 3a. : 64QAM/256QAM selection for manual */
Fred Richterb63b36f2014-03-24 19:56:00 -0300556 ret = lgdt3306a_read_reg(state, 0x101a, &val);
557 val &= 0xF8;
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -0300558 if (modulation == QAM_64)
559 val |= 0x02; /* QMDQMODE[2:0]=2=QAM64 */
560 else
561 val |= 0x04; /* QMDQMODE[2:0]=4=QAM256 */
562
Fred Richterb63b36f2014-03-24 19:56:00 -0300563 ret = lgdt3306a_write_reg(state, 0x101a, val);
564 if (lg_chkerr(ret))
565 goto fail;
566
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -0300567 /* 4. ADC sampling frequency rate(4x sampling) */
Fred Richterb63b36f2014-03-24 19:56:00 -0300568 ret = lgdt3306a_read_reg(state, 0x000D, &val);
569 val &= 0xBF;
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -0300570 val |= 0x40; /* SAMPLING4XFEN=1 */
Fred Richterb63b36f2014-03-24 19:56:00 -0300571 ret = lgdt3306a_write_reg(state, 0x000D, val);
572 if (lg_chkerr(ret))
573 goto fail;
574
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -0300575 /* 5. No AICC operation in QAM mode */
Fred Richterb63b36f2014-03-24 19:56:00 -0300576 ret = lgdt3306a_read_reg(state, 0x0024, &val);
577 val &= 0x00;
578 ret = lgdt3306a_write_reg(state, 0x0024, val);
579 if (lg_chkerr(ret))
580 goto fail;
581
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -0300582 /* 6. Reset */
Fred Richterb63b36f2014-03-24 19:56:00 -0300583 ret = lgdt3306a_soft_reset(state);
584 if (lg_chkerr(ret))
585 goto fail;
586
587 lg_dbg("complete\n");
588fail:
589 return ret;
590}
591
592static int lgdt3306a_set_modulation(struct lgdt3306a_state *state,
593 struct dtv_frontend_properties *p)
594{
595 int ret;
596
597 lg_dbg("\n");
598
599 switch (p->modulation) {
600 case VSB_8:
601 ret = lgdt3306a_set_vsb(state);
602 break;
603 case QAM_64:
604 ret = lgdt3306a_set_qam(state, QAM_64);
605 break;
606 case QAM_256:
607 ret = lgdt3306a_set_qam(state, QAM_256);
608 break;
609 default:
610 return -EINVAL;
611 }
612 if (lg_chkerr(ret))
613 goto fail;
614
615 state->current_modulation = p->modulation;
616
617fail:
618 return ret;
619}
620
621/* ------------------------------------------------------------------------ */
622
623static int lgdt3306a_agc_setup(struct lgdt3306a_state *state,
624 struct dtv_frontend_properties *p)
625{
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -0300626 /* TODO: anything we want to do here??? */
Fred Richterb63b36f2014-03-24 19:56:00 -0300627 lg_dbg("\n");
628
629 switch (p->modulation) {
630 case VSB_8:
631 break;
632 case QAM_64:
633 case QAM_256:
634 break;
635 default:
636 return -EINVAL;
637 }
638 return 0;
639}
640
641/* ------------------------------------------------------------------------ */
642
643static int lgdt3306a_set_inversion(struct lgdt3306a_state *state,
644 int inversion)
645{
646 int ret;
647
648 lg_dbg("(%d)\n", inversion);
649
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -0300650 ret = lgdt3306a_set_reg_bit(state, 0x0002, 2, inversion ? 1 : 0);
Fred Richterb63b36f2014-03-24 19:56:00 -0300651 return ret;
652}
653
654static int lgdt3306a_set_inversion_auto(struct lgdt3306a_state *state,
655 int enabled)
656{
657 int ret;
658
659 lg_dbg("(%d)\n", enabled);
660
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -0300661 /* 0=Manual 1=Auto(QAM only) */
662 ret = lgdt3306a_set_reg_bit(state, 0x0002, 3, enabled);/* SPECINVAUTO=0x04 */
Fred Richterb63b36f2014-03-24 19:56:00 -0300663 return ret;
664}
665
666static int lgdt3306a_spectral_inversion(struct lgdt3306a_state *state,
667 struct dtv_frontend_properties *p,
668 int inversion)
669{
670 int ret = 0;
671
672 lg_dbg("(%d)\n", inversion);
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -0300673#if 0
674/* FGR - spectral_inversion defaults already set for VSB and QAM; can enable later if desired */
Fred Richterb63b36f2014-03-24 19:56:00 -0300675
676 ret = lgdt3306a_set_inversion(state, inversion);
677
678 switch (p->modulation) {
679 case VSB_8:
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -0300680 ret = lgdt3306a_set_inversion_auto(state, 0); /* Manual only for VSB */
Fred Richterb63b36f2014-03-24 19:56:00 -0300681 break;
682 case QAM_64:
683 case QAM_256:
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -0300684 ret = lgdt3306a_set_inversion_auto(state, 1); /* Auto ok for QAM */
Fred Richterb63b36f2014-03-24 19:56:00 -0300685 break;
686 default:
687 ret = -EINVAL;
688 }
689#endif
690 return ret;
691}
692
693static int lgdt3306a_set_if(struct lgdt3306a_state *state,
694 struct dtv_frontend_properties *p)
695{
696 int ret;
697 u16 if_freq_khz;
698 u8 nco1, nco2;
699
700 switch (p->modulation) {
701 case VSB_8:
702 if_freq_khz = state->cfg->vsb_if_khz;
703 break;
704 case QAM_64:
705 case QAM_256:
706 if_freq_khz = state->cfg->qam_if_khz;
707 break;
708 default:
709 return -EINVAL;
710 }
711
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -0300712 switch (if_freq_khz) {
Fred Richterb63b36f2014-03-24 19:56:00 -0300713 default:
714 lg_warn("IF=%d KHz is not supportted, 3250 assumed\n", if_freq_khz);
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -0300715 /* fallthrough */
716 case 3250: /* 3.25Mhz */
Fred Richterb63b36f2014-03-24 19:56:00 -0300717 nco1 = 0x34;
718 nco2 = 0x00;
719 break;
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -0300720 case 3500: /* 3.50Mhz */
Fred Richterb63b36f2014-03-24 19:56:00 -0300721 nco1 = 0x38;
722 nco2 = 0x00;
723 break;
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -0300724 case 4000: /* 4.00Mhz */
Fred Richterb63b36f2014-03-24 19:56:00 -0300725 nco1 = 0x40;
726 nco2 = 0x00;
727 break;
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -0300728 case 5000: /* 5.00Mhz */
Fred Richterb63b36f2014-03-24 19:56:00 -0300729 nco1 = 0x50;
730 nco2 = 0x00;
731 break;
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -0300732 case 5380: /* 5.38Mhz */
Fred Richterb63b36f2014-03-24 19:56:00 -0300733 nco1 = 0x56;
734 nco2 = 0x14;
735 break;
736 }
737 ret = lgdt3306a_write_reg(state, 0x0010, nco1);
738 ret = lgdt3306a_write_reg(state, 0x0011, nco2);
739
740 lg_dbg("if_freq=%d KHz->[%04x]\n", if_freq_khz, nco1<<8 | nco2);
741
742 return 0;
743}
744
745/* ------------------------------------------------------------------------ */
746
747static int lgdt3306a_i2c_gate_ctrl(struct dvb_frontend *fe, int enable)
748{
749 struct lgdt3306a_state *state = fe->demodulator_priv;
750
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -0300751 if (state->cfg->deny_i2c_rptr) {
Fred Richterb63b36f2014-03-24 19:56:00 -0300752 lg_dbg("deny_i2c_rptr=%d\n", state->cfg->deny_i2c_rptr);
753 return 0;
754 }
755 lg_dbg("(%d)\n", enable);
756
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -0300757 return lgdt3306a_set_reg_bit(state, 0x0002, 7, enable ? 0 : 1); /* NI2CRPTEN=0x80 */
Fred Richterb63b36f2014-03-24 19:56:00 -0300758}
759
760static int lgdt3306a_sleep(struct lgdt3306a_state *state)
761{
762 int ret;
763
764 lg_dbg("\n");
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -0300765 state->current_frequency = -1; /* force re-tune, when we wake */
Fred Richterb63b36f2014-03-24 19:56:00 -0300766
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -0300767 ret = lgdt3306a_mpeg_tristate(state, 1); /* disable data bus */
Fred Richterb63b36f2014-03-24 19:56:00 -0300768 if (lg_chkerr(ret))
769 goto fail;
770
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -0300771 ret = lgdt3306a_power(state, 0); /* power down */
Fred Richterb63b36f2014-03-24 19:56:00 -0300772 lg_chkerr(ret);
773
774fail:
775 return 0;
776}
777
778static int lgdt3306a_fe_sleep(struct dvb_frontend *fe)
779{
780 struct lgdt3306a_state *state = fe->demodulator_priv;
781
782 return lgdt3306a_sleep(state);
783}
784
785static int lgdt3306a_init(struct dvb_frontend *fe)
786{
787 struct lgdt3306a_state *state = fe->demodulator_priv;
788 u8 val;
789 int ret;
790
791 lg_dbg("\n");
792
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -0300793 /* 1. Normal operation mode */
794 ret = lgdt3306a_set_reg_bit(state, 0x0001, 0, 1); /* SIMFASTENB=0x01 */
Fred Richterb63b36f2014-03-24 19:56:00 -0300795 if (lg_chkerr(ret))
796 goto fail;
797
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -0300798 /* 2. Spectrum inversion auto detection (Not valid for VSB) */
Fred Richterb63b36f2014-03-24 19:56:00 -0300799 ret = lgdt3306a_set_inversion_auto(state, 0);
800 if (lg_chkerr(ret))
801 goto fail;
802
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -0300803 /* 3. Spectrum inversion(According to the tuner configuration) */
Fred Richterb63b36f2014-03-24 19:56:00 -0300804 ret = lgdt3306a_set_inversion(state, 1);
805 if (lg_chkerr(ret))
806 goto fail;
807
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -0300808 /* 4. Peak-to-peak voltage of ADC input signal */
809 ret = lgdt3306a_set_reg_bit(state, 0x0004, 7, 1); /* ADCSEL1V=0x80=1Vpp; 0x00=2Vpp */
Fred Richterb63b36f2014-03-24 19:56:00 -0300810 if (lg_chkerr(ret))
811 goto fail;
812
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -0300813 /* 5. ADC output data capture clock phase */
814 ret = lgdt3306a_set_reg_bit(state, 0x0004, 2, 0); /* 0=same phase as ADC clock */
Fred Richterb63b36f2014-03-24 19:56:00 -0300815 if (lg_chkerr(ret))
816 goto fail;
817
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -0300818 /* 5a. ADC sampling clock source */
819 ret = lgdt3306a_set_reg_bit(state, 0x0004, 3, 0); /* ADCCLKPLLSEL=0x08; 0=use ext clock, not PLL */
Fred Richterb63b36f2014-03-24 19:56:00 -0300820 if (lg_chkerr(ret))
821 goto fail;
822
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -0300823 /* 6. Automatic PLL set */
824 ret = lgdt3306a_set_reg_bit(state, 0x0005, 6, 0); /* PLLSETAUTO=0x40; 0=off */
Fred Richterb63b36f2014-03-24 19:56:00 -0300825 if (lg_chkerr(ret))
826 goto fail;
827
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -0300828 if (state->cfg->xtalMHz == 24) { /* 24MHz */
829 /* 7. Frequency for PLL output(0x2564 for 192MHz for 24MHz) */
Fred Richterb63b36f2014-03-24 19:56:00 -0300830 ret = lgdt3306a_read_reg(state, 0x0005, &val);
831 if (lg_chkerr(ret))
832 goto fail;
833 val &= 0xC0;
834 val |= 0x25;
835 ret = lgdt3306a_write_reg(state, 0x0005, val);
836 if (lg_chkerr(ret))
837 goto fail;
838 ret = lgdt3306a_write_reg(state, 0x0006, 0x64);
839 if (lg_chkerr(ret))
840 goto fail;
841
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -0300842 /* 8. ADC sampling frequency(0x180000 for 24MHz sampling) */
Fred Richterb63b36f2014-03-24 19:56:00 -0300843 ret = lgdt3306a_read_reg(state, 0x000D, &val);
844 if (lg_chkerr(ret))
845 goto fail;
846 val &= 0xC0;
847 val |= 0x18;
848 ret = lgdt3306a_write_reg(state, 0x000D, val);
849 if (lg_chkerr(ret))
850 goto fail;
851
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -0300852 } else if (state->cfg->xtalMHz == 25) { /* 25MHz */
853 /* 7. Frequency for PLL output */
Fred Richterb63b36f2014-03-24 19:56:00 -0300854 ret = lgdt3306a_read_reg(state, 0x0005, &val);
855 if (lg_chkerr(ret))
856 goto fail;
857 val &= 0xC0;
858 val |= 0x25;
859 ret = lgdt3306a_write_reg(state, 0x0005, val);
860 if (lg_chkerr(ret))
861 goto fail;
862 ret = lgdt3306a_write_reg(state, 0x0006, 0x64);
863 if (lg_chkerr(ret))
864 goto fail;
865
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -0300866 /* 8. ADC sampling frequency(0x190000 for 25MHz sampling) */
Fred Richterb63b36f2014-03-24 19:56:00 -0300867 ret = lgdt3306a_read_reg(state, 0x000D, &val);
868 if (lg_chkerr(ret))
869 goto fail;
870 val &= 0xC0;
871 val |= 0x19;
872 ret = lgdt3306a_write_reg(state, 0x000D, val);
873 if (lg_chkerr(ret))
874 goto fail;
875 } else {
876 lg_err("Bad xtalMHz=%d\n", state->cfg->xtalMHz);
877 }
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -0300878#if 0
879 ret = lgdt3306a_write_reg(state, 0x000E, 0x00);
880 ret = lgdt3306a_write_reg(state, 0x000F, 0x00);
881#endif
Fred Richterb63b36f2014-03-24 19:56:00 -0300882
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -0300883 /* 9. Center frequency of input signal of ADC */
884 ret = lgdt3306a_write_reg(state, 0x0010, 0x34); /* 3.25MHz */
885 ret = lgdt3306a_write_reg(state, 0x0011, 0x00);
Fred Richterb63b36f2014-03-24 19:56:00 -0300886
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -0300887 /* 10. Fixed gain error value */
888 ret = lgdt3306a_write_reg(state, 0x0014, 0); /* gain error=0 */
Fred Richterb63b36f2014-03-24 19:56:00 -0300889
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -0300890 /* 10a. VSB TR BW gear shift initial step */
Fred Richterb63b36f2014-03-24 19:56:00 -0300891 ret = lgdt3306a_read_reg(state, 0x103C, &val);
892 val &= 0x0F;
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -0300893 val |= 0x20; /* SAMGSAUTOSTL_V[3:0] = 2 */
Fred Richterb63b36f2014-03-24 19:56:00 -0300894 ret = lgdt3306a_write_reg(state, 0x103C, val);
895
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -0300896 /* 10b. Timing offset calibration in low temperature for VSB */
Fred Richterb63b36f2014-03-24 19:56:00 -0300897 ret = lgdt3306a_read_reg(state, 0x103D, &val);
898 val &= 0xFC;
899 val |= 0x03;
900 ret = lgdt3306a_write_reg(state, 0x103D, val);
901
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -0300902 /* 10c. Timing offset calibration in low temperature for QAM */
Fred Richterb63b36f2014-03-24 19:56:00 -0300903 ret = lgdt3306a_read_reg(state, 0x1036, &val);
904 val &= 0xF0;
905 val |= 0x0C;
906 ret = lgdt3306a_write_reg(state, 0x1036, val);
907
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -0300908 /* 11. Using the imaginary part of CIR in CIR loading */
Fred Richterb63b36f2014-03-24 19:56:00 -0300909 ret = lgdt3306a_read_reg(state, 0x211F, &val);
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -0300910 val &= 0xEF; /* do not use imaginary of CIR */
Fred Richterb63b36f2014-03-24 19:56:00 -0300911 ret = lgdt3306a_write_reg(state, 0x211F, val);
912
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -0300913 /* 12. Control of no signal detector function */
Fred Richterb63b36f2014-03-24 19:56:00 -0300914 ret = lgdt3306a_read_reg(state, 0x2849, &val);
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -0300915 val &= 0xEF; /* NOUSENOSIGDET=0, enable no signal detector */
Fred Richterb63b36f2014-03-24 19:56:00 -0300916 ret = lgdt3306a_write_reg(state, 0x2849, val);
917
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -0300918 /* FGR - put demod in some known mode */
Fred Richterb63b36f2014-03-24 19:56:00 -0300919 ret = lgdt3306a_set_vsb(state);
920
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -0300921 /* 13. TP stream format */
Fred Richterb63b36f2014-03-24 19:56:00 -0300922 ret = lgdt3306a_mpeg_mode(state, state->cfg->mpeg_mode);
923
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -0300924 /* 14. disable output buses */
Fred Richterb63b36f2014-03-24 19:56:00 -0300925 ret = lgdt3306a_mpeg_tristate(state, 1);
926
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -0300927 /* 15. Sleep (in reset) */
Fred Richterb63b36f2014-03-24 19:56:00 -0300928 ret = lgdt3306a_sleep(state);
929 lg_chkerr(ret);
930
931fail:
932 return ret;
933}
934
935static int lgdt3306a_set_parameters(struct dvb_frontend *fe)
936{
937 struct dtv_frontend_properties *p = &fe->dtv_property_cache;
938 struct lgdt3306a_state *state = fe->demodulator_priv;
939 int ret;
940
941 lg_dbg("(%d, %d)\n", p->frequency, p->modulation);
942
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -0300943 if (state->current_frequency == p->frequency &&
944 state->current_modulation == p->modulation) {
Fred Richterb63b36f2014-03-24 19:56:00 -0300945 lg_dbg(" (already set, skipping ...)\n");
946 return 0;
947 }
948 state->current_frequency = -1;
949 state->current_modulation = -1;
950
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -0300951 ret = lgdt3306a_power(state, 1); /* power up */
Fred Richterb63b36f2014-03-24 19:56:00 -0300952 if (lg_chkerr(ret))
953 goto fail;
954
955 if (fe->ops.tuner_ops.set_params) {
956 ret = fe->ops.tuner_ops.set_params(fe);
957 if (fe->ops.i2c_gate_ctrl)
958 fe->ops.i2c_gate_ctrl(fe, 0);
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -0300959#if 0
960 if (lg_chkerr(ret))
961 goto fail;
962 state->current_frequency = p->frequency;
963#endif
Fred Richterb63b36f2014-03-24 19:56:00 -0300964 }
965
966 ret = lgdt3306a_set_modulation(state, p);
967 if (lg_chkerr(ret))
968 goto fail;
969
970 ret = lgdt3306a_agc_setup(state, p);
971 if (lg_chkerr(ret))
972 goto fail;
973
974 ret = lgdt3306a_set_if(state, p);
975 if (lg_chkerr(ret))
976 goto fail;
977
978 ret = lgdt3306a_spectral_inversion(state, p,
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -0300979 state->cfg->spectral_inversion ? 1 : 0);
Fred Richterb63b36f2014-03-24 19:56:00 -0300980 if (lg_chkerr(ret))
981 goto fail;
982
983 ret = lgdt3306a_mpeg_mode(state, state->cfg->mpeg_mode);
984 if (lg_chkerr(ret))
985 goto fail;
986
987 ret = lgdt3306a_mpeg_mode_polarity(state,
988 state->cfg->tpclk_edge,
989 state->cfg->tpvalid_polarity);
990 if (lg_chkerr(ret))
991 goto fail;
992
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -0300993 ret = lgdt3306a_mpeg_tristate(state, 0); /* enable data bus */
Fred Richterb63b36f2014-03-24 19:56:00 -0300994 if (lg_chkerr(ret))
995 goto fail;
996
997 ret = lgdt3306a_soft_reset(state);
998 if (lg_chkerr(ret))
999 goto fail;
1000
1001#ifdef DBG_DUMP
1002 lgdt3306a_DumpAllRegs(state);
1003#endif
1004 state->current_frequency = p->frequency;
1005fail:
1006 return ret;
1007}
1008
1009static int lgdt3306a_get_frontend(struct dvb_frontend *fe)
1010{
1011 struct lgdt3306a_state *state = fe->demodulator_priv;
1012 struct dtv_frontend_properties *p = &fe->dtv_property_cache;
1013
1014 lg_dbg("(%u, %d)\n", state->current_frequency, state->current_modulation);
1015
1016 p->modulation = state->current_modulation;
1017 p->frequency = state->current_frequency;
1018 return 0;
1019}
1020
1021static enum dvbfe_algo lgdt3306a_get_frontend_algo(struct dvb_frontend *fe)
1022{
1023#if 1
1024 return DVBFE_ALGO_CUSTOM;
1025#else
1026 return DVBFE_ALGO_HW;
1027#endif
1028}
1029
1030/* ------------------------------------------------------------------------ */
1031static void lgdt3306a_monitor_vsb(struct lgdt3306a_state *state)
1032{
1033 u8 val;
1034 int ret;
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -03001035 u8 snrRef, maxPowerMan, nCombDet;
1036 u16 fbDlyCir;
Fred Richterb63b36f2014-03-24 19:56:00 -03001037
1038 ret = lgdt3306a_read_reg(state, 0x21A1, &val);
1039 snrRef = val & 0x3F;
1040
1041 ret = lgdt3306a_read_reg(state, 0x2185, &maxPowerMan);
1042
1043 ret = lgdt3306a_read_reg(state, 0x2191, &val);
1044 nCombDet = (val & 0x80) >> 7;
1045
1046 ret = lgdt3306a_read_reg(state, 0x2180, &val);
1047 fbDlyCir = (val & 0x03) << 8;
1048 ret = lgdt3306a_read_reg(state, 0x2181, &val);
1049 fbDlyCir |= val;
1050
1051 lg_dbg("snrRef=%d maxPowerMan=0x%x nCombDet=%d fbDlyCir=0x%x\n",
1052 snrRef, maxPowerMan, nCombDet, fbDlyCir);
1053
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -03001054 /* Carrier offset sub loop bandwidth */
Fred Richterb63b36f2014-03-24 19:56:00 -03001055 ret = lgdt3306a_read_reg(state, 0x1061, &val);
1056 val &= 0xF8;
1057 if ((snrRef > 18) && (maxPowerMan > 0x68) && (nCombDet == 0x01) && ((fbDlyCir == 0x03FF) || (fbDlyCir < 0x6C))) {
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -03001058 /* SNR is over 18dB and no ghosting */
1059 val |= 0x00; /* final bandwidth = 0 */
Fred Richterb63b36f2014-03-24 19:56:00 -03001060 } else {
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -03001061 val |= 0x04; /* final bandwidth = 4 */
Fred Richterb63b36f2014-03-24 19:56:00 -03001062 }
1063 ret = lgdt3306a_write_reg(state, 0x1061, val);
1064
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -03001065 /* Adjust Notch Filter */
Fred Richterb63b36f2014-03-24 19:56:00 -03001066 ret = lgdt3306a_read_reg(state, 0x0024, &val);
1067 val &= 0x0F;
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -03001068 if (nCombDet == 0) { /* Turn on the Notch Filter */
Fred Richterb63b36f2014-03-24 19:56:00 -03001069 val |= 0x50;
1070 }
1071 ret = lgdt3306a_write_reg(state, 0x0024, val);
1072
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -03001073 /* VSB Timing Recovery output normalization */
Fred Richterb63b36f2014-03-24 19:56:00 -03001074 ret = lgdt3306a_read_reg(state, 0x103D, &val);
1075 val &= 0xCF;
1076 val |= 0x20;
1077 ret = lgdt3306a_write_reg(state, 0x103D, val);
1078}
1079
1080static LG3306_MODULATION lgdt3306a_check_oper_mode(struct lgdt3306a_state *state)
1081{
1082 u8 val = 0;
1083 int ret;
1084
1085 ret = lgdt3306a_read_reg(state, 0x0081, &val);
1086
1087 if (val & 0x80) {
1088 lg_dbg("VSB\n");
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -03001089 return LG3306_VSB;
Fred Richterb63b36f2014-03-24 19:56:00 -03001090 }
Michael Ira Krufkyc714efe2014-08-03 14:51:49 -03001091 if (val & 0x08) {
Fred Richterb63b36f2014-03-24 19:56:00 -03001092 ret = lgdt3306a_read_reg(state, 0x00A6, &val);
1093 val = val >> 2;
1094 if (val & 0x01) {
1095 lg_dbg("QAM256\n");
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -03001096 return LG3306_QAM256;
Fred Richterb63b36f2014-03-24 19:56:00 -03001097 } else {
1098 lg_dbg("QAM64\n");
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -03001099 return LG3306_QAM64;
Fred Richterb63b36f2014-03-24 19:56:00 -03001100 }
1101 }
1102 lg_warn("UNKNOWN\n");
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -03001103 return LG3306_UNKNOWN_MODE;
Fred Richterb63b36f2014-03-24 19:56:00 -03001104}
1105
1106static LG3306_LOCK_STATUS lgdt3306a_check_lock_status(struct lgdt3306a_state *state,
1107 LG3306_LOCK_CHECK whatLock)
1108{
1109 u8 val = 0;
1110 int ret;
1111 LG3306_MODULATION modeOper;
1112 LG3306_LOCK_STATUS lockStatus;
1113
1114 modeOper = LG3306_UNKNOWN_MODE;
1115
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -03001116 switch (whatLock) {
1117 case LG3306_SYNC_LOCK:
Fred Richterb63b36f2014-03-24 19:56:00 -03001118 {
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -03001119 ret = lgdt3306a_read_reg(state, 0x00A6, &val);
1120
1121 if ((val & 0x80) == 0x80)
1122 lockStatus = LG3306_LOCK;
1123 else
1124 lockStatus = LG3306_UNLOCK;
1125
1126 lg_dbg("SYNC_LOCK=%x\n", lockStatus);
1127 break;
1128 }
1129 case LG3306_AGC_LOCK:
1130 {
1131 ret = lgdt3306a_read_reg(state, 0x0080, &val);
1132
1133 if ((val & 0x40) == 0x40)
1134 lockStatus = LG3306_LOCK;
1135 else
1136 lockStatus = LG3306_UNLOCK;
1137
1138 lg_dbg("AGC_LOCK=%x\n", lockStatus);
1139 break;
1140 }
1141 case LG3306_TR_LOCK:
1142 {
1143 modeOper = lgdt3306a_check_oper_mode(state);
1144 if ((modeOper == LG3306_QAM64) || (modeOper == LG3306_QAM256)) {
1145 ret = lgdt3306a_read_reg(state, 0x1094, &val);
Fred Richterb63b36f2014-03-24 19:56:00 -03001146
1147 if ((val & 0x80) == 0x80)
1148 lockStatus = LG3306_LOCK;
1149 else
1150 lockStatus = LG3306_UNLOCK;
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -03001151 } else
1152 lockStatus = LG3306_UNKNOWN_LOCK;
Fred Richterb63b36f2014-03-24 19:56:00 -03001153
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -03001154 lg_dbg("TR_LOCK=%x\n", lockStatus);
1155 break;
1156 }
1157 case LG3306_FEC_LOCK:
1158 {
1159 modeOper = lgdt3306a_check_oper_mode(state);
1160 if ((modeOper == LG3306_QAM64) || (modeOper == LG3306_QAM256)) {
Fred Richterb63b36f2014-03-24 19:56:00 -03001161 ret = lgdt3306a_read_reg(state, 0x0080, &val);
1162
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -03001163 if ((val & 0x10) == 0x10)
Fred Richterb63b36f2014-03-24 19:56:00 -03001164 lockStatus = LG3306_LOCK;
1165 else
1166 lockStatus = LG3306_UNLOCK;
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -03001167 } else
Fred Richterb63b36f2014-03-24 19:56:00 -03001168 lockStatus = LG3306_UNKNOWN_LOCK;
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -03001169
1170 lg_dbg("FEC_LOCK=%x\n", lockStatus);
1171 break;
Fred Richterb63b36f2014-03-24 19:56:00 -03001172 }
1173
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -03001174 default:
1175 lockStatus = LG3306_UNKNOWN_LOCK;
1176 lg_warn("UNKNOWN whatLock=%d\n", whatLock);
1177 break;
1178 }
1179
1180 return lockStatus;
Fred Richterb63b36f2014-03-24 19:56:00 -03001181}
1182
1183static LG3306_NEVERLOCK_STATUS lgdt3306a_check_neverlock_status(struct lgdt3306a_state *state)
1184{
1185 u8 val = 0;
1186 int ret;
1187 LG3306_NEVERLOCK_STATUS lockStatus;
1188
1189 ret = lgdt3306a_read_reg(state, 0x0080, &val);
1190 lockStatus = (LG3306_NEVERLOCK_STATUS)(val & 0x03);
1191
1192 lg_dbg("NeverLock=%d", lockStatus);
1193
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -03001194 return lockStatus;
Fred Richterb63b36f2014-03-24 19:56:00 -03001195}
1196
1197static void lgdt3306a_pre_monitoring(struct lgdt3306a_state *state)
1198{
1199 u8 val = 0;
1200 int ret;
1201 u8 currChDiffACQ, snrRef, mainStrong, aiccrejStatus;
1202
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -03001203 /* Channel variation */
Fred Richterb63b36f2014-03-24 19:56:00 -03001204 ret = lgdt3306a_read_reg(state, 0x21BC, &currChDiffACQ);
1205
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -03001206 /* SNR of Frame sync */
Fred Richterb63b36f2014-03-24 19:56:00 -03001207 ret = lgdt3306a_read_reg(state, 0x21A1, &val);
1208 snrRef = val & 0x3F;
1209
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -03001210 /* Strong Main CIR */
Fred Richterb63b36f2014-03-24 19:56:00 -03001211 ret = lgdt3306a_read_reg(state, 0x2199, &val);
1212 mainStrong = (val & 0x40) >> 6;
1213
1214 ret = lgdt3306a_read_reg(state, 0x0090, &val);
1215 aiccrejStatus = (val & 0xF0) >> 4;
1216
1217 lg_dbg("snrRef=%d mainStrong=%d aiccrejStatus=%d currChDiffACQ=0x%x\n",
1218 snrRef, mainStrong, aiccrejStatus, currChDiffACQ);
1219
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -03001220#if 0
1221 if ((mainStrong == 0) && (currChDiffACQ > 0x70)) /* Dynamic ghost exists */
1222#endif
1223 if (mainStrong == 0) {
Fred Richterb63b36f2014-03-24 19:56:00 -03001224 ret = lgdt3306a_read_reg(state, 0x2135, &val);
1225 val &= 0x0F;
1226 val |= 0xA0;
1227 ret = lgdt3306a_write_reg(state, 0x2135, val);
1228
1229 ret = lgdt3306a_read_reg(state, 0x2141, &val);
1230 val &= 0x3F;
1231 val |= 0x80;
1232 ret = lgdt3306a_write_reg(state, 0x2141, val);
1233
1234 ret = lgdt3306a_write_reg(state, 0x2122, 0x70);
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -03001235 } else { /* Weak ghost or static channel */
Fred Richterb63b36f2014-03-24 19:56:00 -03001236 ret = lgdt3306a_read_reg(state, 0x2135, &val);
1237 val &= 0x0F;
1238 val |= 0x70;
1239 ret = lgdt3306a_write_reg(state, 0x2135, val);
1240
1241 ret = lgdt3306a_read_reg(state, 0x2141, &val);
1242 val &= 0x3F;
1243 val |= 0x40;
1244 ret = lgdt3306a_write_reg(state, 0x2141, val);
1245
1246 ret = lgdt3306a_write_reg(state, 0x2122, 0x40);
1247 }
1248
1249}
1250
1251static LG3306_LOCK_STATUS lgdt3306a_sync_lock_poll(struct lgdt3306a_state *state)
1252{
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -03001253 LG3306_LOCK_STATUS syncLockStatus = LG3306_UNLOCK;
Fred Richterb63b36f2014-03-24 19:56:00 -03001254 int i;
1255
1256 for (i = 0; i < 2; i++) {
1257 msleep(30);
1258
1259 syncLockStatus = lgdt3306a_check_lock_status(state, LG3306_SYNC_LOCK);
1260
1261 if (syncLockStatus == LG3306_LOCK) {
1262 lg_dbg("locked(%d)\n", i);
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -03001263 return LG3306_LOCK;
Fred Richterb63b36f2014-03-24 19:56:00 -03001264 }
1265 }
1266 lg_dbg("not locked\n");
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -03001267 return LG3306_UNLOCK;
Fred Richterb63b36f2014-03-24 19:56:00 -03001268}
1269
1270static LG3306_LOCK_STATUS lgdt3306a_fec_lock_poll(struct lgdt3306a_state *state)
1271{
1272 LG3306_LOCK_STATUS FECLockStatus = LG3306_UNLOCK;
1273 int i;
1274
1275 for (i = 0; i < 2; i++) {
1276 msleep(30);
1277
1278 FECLockStatus = lgdt3306a_check_lock_status(state, LG3306_FEC_LOCK);
1279
1280 if (FECLockStatus == LG3306_LOCK) {
1281 lg_dbg("locked(%d)\n", i);
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -03001282 return FECLockStatus;
Fred Richterb63b36f2014-03-24 19:56:00 -03001283 }
1284 }
1285 lg_dbg("not locked\n");
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -03001286 return FECLockStatus;
Fred Richterb63b36f2014-03-24 19:56:00 -03001287}
1288
1289static LG3306_NEVERLOCK_STATUS lgdt3306a_neverlock_poll(struct lgdt3306a_state *state)
1290{
1291 LG3306_NEVERLOCK_STATUS NLLockStatus = LG3306_NL_FAIL;
1292 int i;
1293
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -03001294 for (i = 0; i < 5; i++) {
Fred Richterb63b36f2014-03-24 19:56:00 -03001295 msleep(30);
1296
1297 NLLockStatus = lgdt3306a_check_neverlock_status(state);
1298
1299 if (NLLockStatus == LG3306_NL_LOCK) {
1300 lg_dbg("NL_LOCK(%d)\n", i);
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -03001301 return NLLockStatus;
Fred Richterb63b36f2014-03-24 19:56:00 -03001302 }
1303 }
1304 lg_dbg("NLLockStatus=%d\n", NLLockStatus);
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -03001305 return NLLockStatus;
Fred Richterb63b36f2014-03-24 19:56:00 -03001306}
1307
1308static u8 lgdt3306a_get_packet_error(struct lgdt3306a_state *state)
1309{
1310 u8 val;
1311 int ret;
1312
1313 ret = lgdt3306a_read_reg(state, 0x00FA, &val);
1314
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -03001315 return val;
Fred Richterb63b36f2014-03-24 19:56:00 -03001316}
1317
1318static u32 log10_x1000(u32 x)
1319{
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -03001320 static u32 valx_x10[] = { 10, 11, 13, 15, 17, 20, 25, 33, 41, 50, 59, 73, 87, 100 };
1321 static u32 log10x_x1000[] = { 0, 41, 114, 176, 230, 301, 398, 518, 613, 699, 771, 863, 939, 1000 };
1322 static u32 nelems = sizeof(valx_x10)/sizeof(valx_x10[0]);
Fred Richterb63b36f2014-03-24 19:56:00 -03001323 u32 log_val = 0;
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -03001324 u32 i;
Fred Richterb63b36f2014-03-24 19:56:00 -03001325
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -03001326 if (x <= 0)
1327 return -1000000; /* signal error */
Fred Richterb63b36f2014-03-24 19:56:00 -03001328
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -03001329 if (x < 10) {
1330 while (x < 10) {
1331 x = x * 10;
Fred Richterb63b36f2014-03-24 19:56:00 -03001332 log_val--;
1333 }
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -03001334 } else if (x == 10) {
1335 return 0; /* log(1)=0 */
Fred Richterb63b36f2014-03-24 19:56:00 -03001336 } else {
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -03001337 while (x >= 100) {
1338 x = x / 10;
Fred Richterb63b36f2014-03-24 19:56:00 -03001339 log_val++;
1340 }
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -03001341 }
Fred Richterb63b36f2014-03-24 19:56:00 -03001342 log_val *= 1000;
1343
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -03001344 if (x == 10) /* was our input an exact multiple of 10 */
1345 return log_val; /* don't need to interpolate */
Fred Richterb63b36f2014-03-24 19:56:00 -03001346
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -03001347 /* find our place on the log curve */
1348 for (i = 1; i < nelems; i++) {
1349 if (valx_x10[i] >= x)
1350 break;
Fred Richterb63b36f2014-03-24 19:56:00 -03001351 }
1352
1353 {
1354 u32 diff_val = x - valx_x10[i-1];
1355 u32 step_val = valx_x10[i] - valx_x10[i-1];
1356 u32 step_log10 = log10x_x1000[i] - log10x_x1000[i-1];
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -03001357 /* do a linear interpolation to get in-between values */
1358 return log_val + log10x_x1000[i-1] +
1359 ((diff_val*step_log10) / step_val);
Fred Richterb63b36f2014-03-24 19:56:00 -03001360 }
1361}
1362
1363static u32 lgdt3306a_calculate_snr_x100(struct lgdt3306a_state *state)
1364{
1365 u32 mse; /* Mean-Square Error */
1366 u32 pwr; /* Constelation power */
1367 u32 snr_x100;
1368
1369 mse = (read_reg(state, 0x00EC) << 8) |
1370 (read_reg(state, 0x00ED));
1371 pwr = (read_reg(state, 0x00E8) << 8) |
1372 (read_reg(state, 0x00E9));
1373
1374 if (mse == 0) /* no signal */
1375 return 0;
1376
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -03001377 snr_x100 = log10_x1000((pwr * 10000) / mse) - 3000;
Fred Richterb63b36f2014-03-24 19:56:00 -03001378 lg_dbg("mse=%u, pwr=%u, snr_x100=%d\n", mse, pwr, snr_x100);
1379
1380 return snr_x100;
1381}
1382
1383static LG3306_LOCK_STATUS lgdt3306a_vsb_lock_poll(struct lgdt3306a_state *state)
1384{
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -03001385 u8 cnt = 0;
1386 u8 packet_error;
1387 u32 snr;
Fred Richterb63b36f2014-03-24 19:56:00 -03001388
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -03001389 while (1) {
Fred Richterb63b36f2014-03-24 19:56:00 -03001390 if (lgdt3306a_sync_lock_poll(state) == LG3306_UNLOCK) {
1391 lg_dbg("no sync lock!\n");
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -03001392 return LG3306_UNLOCK;
Fred Richterb63b36f2014-03-24 19:56:00 -03001393 } else {
1394 msleep(20);
1395 lgdt3306a_pre_monitoring(state);
1396
1397 packet_error = lgdt3306a_get_packet_error(state);
1398 snr = lgdt3306a_calculate_snr_x100(state);
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -03001399 lg_dbg("cnt=%d errors=%d snr=%d\n",
1400 cnt, packet_error, snr);
Fred Richterb63b36f2014-03-24 19:56:00 -03001401
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -03001402 if ((snr < 1500) || (packet_error >= 0xff))
Fred Richterb63b36f2014-03-24 19:56:00 -03001403 cnt++;
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -03001404 else
1405 return LG3306_LOCK;
Fred Richterb63b36f2014-03-24 19:56:00 -03001406
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -03001407 if (cnt >= 10) {
Fred Richterb63b36f2014-03-24 19:56:00 -03001408 lg_dbg("not locked!\n");
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -03001409 return LG3306_UNLOCK;
Fred Richterb63b36f2014-03-24 19:56:00 -03001410 }
1411 }
1412 }
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -03001413 return LG3306_UNLOCK;
Fred Richterb63b36f2014-03-24 19:56:00 -03001414}
1415
1416static LG3306_LOCK_STATUS lgdt3306a_qam_lock_poll(struct lgdt3306a_state *state)
1417{
1418 u8 cnt = 0;
1419 u8 packet_error;
1420 u32 snr;
1421
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -03001422 while (1) {
1423 if (lgdt3306a_fec_lock_poll(state) == LG3306_UNLOCK) {
Fred Richterb63b36f2014-03-24 19:56:00 -03001424 lg_dbg("no fec lock!\n");
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -03001425 return LG3306_UNLOCK;
Fred Richterb63b36f2014-03-24 19:56:00 -03001426 } else {
1427 msleep(20);
1428
1429 packet_error = lgdt3306a_get_packet_error(state);
1430 snr = lgdt3306a_calculate_snr_x100(state);
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -03001431 lg_dbg("cnt=%d errors=%d snr=%d\n",
1432 cnt, packet_error, snr);
Fred Richterb63b36f2014-03-24 19:56:00 -03001433
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -03001434 if ((snr < 1500) || (packet_error >= 0xff))
Fred Richterb63b36f2014-03-24 19:56:00 -03001435 cnt++;
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -03001436 else
1437 return LG3306_LOCK;
Fred Richterb63b36f2014-03-24 19:56:00 -03001438
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -03001439 if (cnt >= 10) {
Fred Richterb63b36f2014-03-24 19:56:00 -03001440 lg_dbg("not locked!\n");
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -03001441 return LG3306_UNLOCK;
Fred Richterb63b36f2014-03-24 19:56:00 -03001442 }
1443 }
1444 }
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -03001445 return LG3306_UNLOCK;
Fred Richterb63b36f2014-03-24 19:56:00 -03001446}
1447
1448static int lgdt3306a_read_status(struct dvb_frontend *fe, fe_status_t *status)
1449{
Fred Richterb63b36f2014-03-24 19:56:00 -03001450 struct lgdt3306a_state *state = fe->demodulator_priv;
Fred Richterb63b36f2014-03-24 19:56:00 -03001451 u16 strength = 0;
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -03001452 int ret = 0;
1453
Fred Richterb63b36f2014-03-24 19:56:00 -03001454 if (fe->ops.tuner_ops.get_rf_strength) {
1455 ret = fe->ops.tuner_ops.get_rf_strength(fe, &strength);
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -03001456 if (ret == 0) {
Fred Richterb63b36f2014-03-24 19:56:00 -03001457 lg_dbg("strength=%d\n", strength);
1458 } else {
1459 lg_dbg("fe->ops.tuner_ops.get_rf_strength() failed\n");
1460 }
1461 }
1462
1463 *status = 0;
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -03001464 if (lgdt3306a_neverlock_poll(state) == LG3306_NL_LOCK) {
Fred Richterb63b36f2014-03-24 19:56:00 -03001465 *status |= FE_HAS_SIGNAL;
1466 *status |= FE_HAS_CARRIER;
1467
1468 switch (state->current_modulation) {
1469 case QAM_256:
1470 case QAM_64:
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -03001471 if (lgdt3306a_qam_lock_poll(state) == LG3306_LOCK) {
Fred Richterb63b36f2014-03-24 19:56:00 -03001472 *status |= FE_HAS_VITERBI;
1473 *status |= FE_HAS_SYNC;
1474
1475 *status |= FE_HAS_LOCK;
1476 }
1477 break;
1478 case VSB_8:
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -03001479 if (lgdt3306a_vsb_lock_poll(state) == LG3306_LOCK) {
Fred Richterb63b36f2014-03-24 19:56:00 -03001480 *status |= FE_HAS_VITERBI;
1481 *status |= FE_HAS_SYNC;
1482
1483 *status |= FE_HAS_LOCK;
1484
1485 lgdt3306a_monitor_vsb(state);
1486 }
1487 break;
1488 default:
1489 ret = -EINVAL;
1490 }
1491 }
1492 return ret;
1493}
1494
1495
1496static int lgdt3306a_read_snr(struct dvb_frontend *fe, u16 *snr)
1497{
1498 struct lgdt3306a_state *state = fe->demodulator_priv;
1499
1500 state->snr = lgdt3306a_calculate_snr_x100(state);
1501 /* report SNR in dB * 10 */
1502 *snr = state->snr/10;
1503
1504 return 0;
1505}
1506
1507static int lgdt3306a_read_signal_strength(struct dvb_frontend *fe,
1508 u16 *strength)
1509{
1510 /*
1511 * Calculate some sort of "strength" from SNR
1512 */
1513 struct lgdt3306a_state *state = fe->demodulator_priv;
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -03001514 u16 snr; /* snr_x10 */
Fred Richterb63b36f2014-03-24 19:56:00 -03001515 int ret;
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -03001516 u32 ref_snr; /* snr*100 */
Fred Richterb63b36f2014-03-24 19:56:00 -03001517 u32 str;
1518
1519 *strength = 0;
1520
1521 switch (state->current_modulation) {
1522 case VSB_8:
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -03001523 ref_snr = 1600; /* 16dB */
Fred Richterb63b36f2014-03-24 19:56:00 -03001524 break;
1525 case QAM_64:
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -03001526 ref_snr = 2200; /* 22dB */
Fred Richterb63b36f2014-03-24 19:56:00 -03001527 break;
1528 case QAM_256:
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -03001529 ref_snr = 2800; /* 28dB */
Fred Richterb63b36f2014-03-24 19:56:00 -03001530 break;
1531 default:
1532 return -EINVAL;
1533 }
1534
1535 ret = fe->ops.read_snr(fe, &snr);
1536 if (lg_chkerr(ret))
1537 goto fail;
1538
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -03001539 if (state->snr <= (ref_snr - 100))
Fred Richterb63b36f2014-03-24 19:56:00 -03001540 str = 0;
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -03001541 else if (state->snr <= ref_snr)
1542 str = (0xffff * 65) / 100; /* 65% */
Fred Richterb63b36f2014-03-24 19:56:00 -03001543 else {
1544 str = state->snr - ref_snr;
1545 str /= 50;
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -03001546 str += 78; /* 78%-100% */
1547 if (str > 100)
Fred Richterb63b36f2014-03-24 19:56:00 -03001548 str = 100;
1549 str = (0xffff * str) / 100;
1550 }
1551 *strength = (u16)str;
1552 lg_dbg("strength=%u\n", *strength);
1553
1554fail:
1555 return ret;
1556}
1557
1558/* ------------------------------------------------------------------------ */
1559
1560static int lgdt3306a_read_ber(struct dvb_frontend *fe, u32 *ber)
1561{
1562 struct lgdt3306a_state *state = fe->demodulator_priv;
1563 u32 tmp;
1564
1565 *ber = 0;
1566#if 1
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -03001567 /* FGR - BUGBUG - I don't know what value is expected by dvb_core
1568 * what is the scale of the value?? */
1569 tmp = read_reg(state, 0x00FC); /* NBERVALUE[24-31] */
1570 tmp = (tmp << 8) | read_reg(state, 0x00FD); /* NBERVALUE[16-23] */
1571 tmp = (tmp << 8) | read_reg(state, 0x00FE); /* NBERVALUE[8-15] */
1572 tmp = (tmp << 8) | read_reg(state, 0x00FF); /* NBERVALUE[0-7] */
Fred Richterb63b36f2014-03-24 19:56:00 -03001573 *ber = tmp;
1574 lg_dbg("ber=%u\n", tmp);
1575#endif
1576 return 0;
1577}
1578
1579static int lgdt3306a_read_ucblocks(struct dvb_frontend *fe, u32 *ucblocks)
1580{
1581 struct lgdt3306a_state *state = fe->demodulator_priv;
1582
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -03001583 *ucblocks = 0;
Fred Richterb63b36f2014-03-24 19:56:00 -03001584#if 1
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -03001585 /* FGR - BUGBUG - I don't know what value is expected by dvb_core
1586 * what happens when value wraps? */
1587 *ucblocks = read_reg(state, 0x00F4); /* TPIFTPERRCNT[0-7] */
Fred Richterb63b36f2014-03-24 19:56:00 -03001588 lg_dbg("ucblocks=%u\n", *ucblocks);
1589#endif
1590
1591 return 0;
1592}
1593
1594static int lgdt3306a_tune(struct dvb_frontend *fe, bool re_tune, unsigned int mode_flags, unsigned int *delay, fe_status_t *status)
1595{
1596 int ret = 0;
1597 struct lgdt3306a_state *state = fe->demodulator_priv;
1598
1599 lg_dbg("re_tune=%u\n", re_tune);
1600
1601 if (re_tune) {
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -03001602 state->current_frequency = -1; /* force re-tune */
1603 if ((ret = lgdt3306a_set_parameters(fe)) != 0)
Fred Richterb63b36f2014-03-24 19:56:00 -03001604 return ret;
Fred Richterb63b36f2014-03-24 19:56:00 -03001605 }
1606 *delay = 125;
1607 ret = lgdt3306a_read_status(fe, status);
1608
1609 return ret;
1610}
1611
1612static int lgdt3306a_get_tune_settings(struct dvb_frontend *fe,
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -03001613 struct dvb_frontend_tune_settings
1614 *fe_tune_settings)
Fred Richterb63b36f2014-03-24 19:56:00 -03001615{
1616 fe_tune_settings->min_delay_ms = 100;
1617 lg_dbg("\n");
1618 return 0;
1619}
1620
1621static int lgdt3306a_search(struct dvb_frontend *fe)
1622{
1623 fe_status_t status = 0;
1624 int i, ret;
1625
1626 /* set frontend */
1627 ret = lgdt3306a_set_parameters(fe);
1628 if (ret)
1629 goto error;
1630
1631 /* wait frontend lock */
1632 for (i = 20; i > 0; i--) {
1633 lg_dbg(": loop=%d\n", i);
1634 msleep(50);
1635 ret = lgdt3306a_read_status(fe, &status);
1636 if (ret)
1637 goto error;
1638
1639 if (status & FE_HAS_LOCK)
1640 break;
1641 }
1642
1643 /* check if we have a valid signal */
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -03001644 if (status & FE_HAS_LOCK)
Fred Richterb63b36f2014-03-24 19:56:00 -03001645 return DVBFE_ALGO_SEARCH_SUCCESS;
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -03001646 else
Fred Richterb63b36f2014-03-24 19:56:00 -03001647 return DVBFE_ALGO_SEARCH_AGAIN;
Fred Richterb63b36f2014-03-24 19:56:00 -03001648
1649error:
1650 lg_dbg("failed (%d)\n", ret);
1651 return DVBFE_ALGO_SEARCH_ERROR;
1652}
1653
1654static void lgdt3306a_release(struct dvb_frontend *fe)
1655{
1656 struct lgdt3306a_state *state = fe->demodulator_priv;
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -03001657
Fred Richterb63b36f2014-03-24 19:56:00 -03001658 lg_dbg("\n");
1659 kfree(state);
1660}
1661
1662static struct dvb_frontend_ops lgdt3306a_ops;
1663
1664struct dvb_frontend *lgdt3306a_attach(const struct lgdt3306a_config *config,
1665 struct i2c_adapter *i2c_adap)
1666{
1667 struct lgdt3306a_state *state = NULL;
1668 int ret;
1669 u8 val;
1670
1671 lg_dbg("(%d-%04x)\n",
1672 i2c_adap ? i2c_adapter_id(i2c_adap) : 0,
1673 config ? config->i2c_addr : 0);
1674
1675 state = kzalloc(sizeof(struct lgdt3306a_state), GFP_KERNEL);
1676 if (state == NULL)
1677 goto fail;
1678
1679 state->cfg = config;
1680 state->i2c_adap = i2c_adap;
1681
1682 memcpy(&state->frontend.ops, &lgdt3306a_ops,
1683 sizeof(struct dvb_frontend_ops));
1684 state->frontend.demodulator_priv = state;
1685
1686 /* verify that we're talking to a lg3306a */
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -03001687 /* FGR - NOTE - there is no obvious ChipId to check; we check
1688 * some "known" bits after reset, but it's still just a guess */
Fred Richterb63b36f2014-03-24 19:56:00 -03001689 ret = lgdt3306a_read_reg(state, 0x0000, &val);
1690 if (lg_chkerr(ret))
1691 goto fail;
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -03001692 if ((val & 0x74) != 0x74) {
Fred Richterb63b36f2014-03-24 19:56:00 -03001693 lg_warn("expected 0x74, got 0x%x\n", (val & 0x74));
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -03001694#if 0
1695 goto fail; /* BUGBUG - re-enable when we know this is right */
1696#endif
Fred Richterb63b36f2014-03-24 19:56:00 -03001697 }
1698 ret = lgdt3306a_read_reg(state, 0x0001, &val);
1699 if (lg_chkerr(ret))
1700 goto fail;
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -03001701 if ((val & 0xF6) != 0xC6) {
Fred Richterb63b36f2014-03-24 19:56:00 -03001702 lg_warn("expected 0xC6, got 0x%x\n", (val & 0xF6));
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -03001703#if 0
1704 goto fail; /* BUGBUG - re-enable when we know this is right */
1705#endif
Fred Richterb63b36f2014-03-24 19:56:00 -03001706 }
1707 ret = lgdt3306a_read_reg(state, 0x0002, &val);
1708 if (lg_chkerr(ret))
1709 goto fail;
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -03001710 if ((val & 0x73) != 0x03) {
Fred Richterb63b36f2014-03-24 19:56:00 -03001711 lg_warn("expected 0x03, got 0x%x\n", (val & 0x73));
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -03001712#if 0
1713 goto fail; /* BUGBUG - re-enable when we know this is right */
1714#endif
Fred Richterb63b36f2014-03-24 19:56:00 -03001715 }
1716
1717 state->current_frequency = -1;
1718 state->current_modulation = -1;
1719
1720 lgdt3306a_sleep(state);
1721
1722 return &state->frontend;
1723
1724fail:
1725 lg_warn("unable to detect LGDT3306A hardware\n");
1726 kfree(state);
1727 return NULL;
1728}
Michael Ira Krufkyebd91752014-08-03 15:05:59 -03001729EXPORT_SYMBOL(lgdt3306a_attach);
Fred Richterb63b36f2014-03-24 19:56:00 -03001730
1731#ifdef DBG_DUMP
1732
1733static const short regtab[] = {
1734 0x0000, //SOFTRSTB 1'b1 1'b1 1'b1 ADCPDB 1'b1 PLLPDB GBBPDB 11111111
1735 0x0001, //1'b1 1'b1 1'b0 1'b0 AUTORPTRS
1736 0x0002, //NI2CRPTEN 1'b0 1'b0 1'b0 SPECINVAUT
1737 0x0003, //AGCRFOUT
1738 0x0004, //ADCSEL1V ADCCNT ADCCNF ADCCNS ADCCLKPLL
1739 0x0005, //PLLINDIVSE
1740 0x0006, //PLLCTRL[7:0] 11100001
1741 0x0007, //SYSINITWAITTIME[7:0] (msec) 00001000
1742 0x0008, //STDOPMODE[7:0] 10000000
1743 0x0009, //1'b0 1'b0 1'b0 STDOPDETTMODE[2:0] STDOPDETCMODE[1:0] 00011110
1744 0x000A, //DAFTEN 1'b1 x x SCSYSLOCK
1745 0x000B, //SCSYSLOCKCHKTIME[7:0] (10msec) 01100100
1746 0x000D, //x SAMPLING4
1747 0x000E, //SAMFREQ[15:8] 00000000
1748 0x000F, //SAMFREQ[7:0] 00000000
1749 0x0010, //IFFREQ[15:8] 01100000
1750 0x0011, //IFFREQ[7:0] 00000000
1751 0x0012, //AGCEN AGCREFMO
1752 0x0013, //AGCRFFIXB AGCIFFIXB AGCLOCKDETRNGSEL[1:0] 1'b1 1'b0 1'b0 1'b0 11101000
1753 0x0014, //AGCFIXVALUE[7:0] 01111111
1754 0x0015, //AGCREF[15:8] 00001010
1755 0x0016, //AGCREF[7:0] 11100100
1756 0x0017, //AGCDELAY[7:0] 00100000
1757 0x0018, //AGCRFBW[3:0] AGCIFBW[3:0] 10001000
1758 0x0019, //AGCUDOUTMODE[1:0] AGCUDCTRLLEN[1:0] AGCUDCTRL
1759 0x001C, //1'b1 PFEN MFEN AICCVSYNC
1760 0x001D, //1'b0 1'b1 1'b0 1'b1 AICCVSYNC
1761 0x001E, //AICCALPHA[3:0] 1'b1 1'b0 1'b1 1'b0 01111010
1762 0x001F, //AICCDETTH[19:16] AICCOFFTH[19:16] 00000000
1763 0x0020, //AICCDETTH[15:8] 01111100
1764 0x0021, //AICCDETTH[7:0] 00000000
1765 0x0022, //AICCOFFTH[15:8] 00000101
1766 0x0023, //AICCOFFTH[7:0] 11100000
1767 0x0024, //AICCOPMODE3[1:0] AICCOPMODE2[1:0] AICCOPMODE1[1:0] AICCOPMODE0[1:0] 00000000
1768 0x0025, //AICCFIXFREQ3[23:16] 00000000
1769 0x0026, //AICCFIXFREQ3[15:8] 00000000
1770 0x0027, //AICCFIXFREQ3[7:0] 00000000
1771 0x0028, //AICCFIXFREQ2[23:16] 00000000
1772 0x0029, //AICCFIXFREQ2[15:8] 00000000
1773 0x002A, //AICCFIXFREQ2[7:0] 00000000
1774 0x002B, //AICCFIXFREQ1[23:16] 00000000
1775 0x002C, //AICCFIXFREQ1[15:8] 00000000
1776 0x002D, //AICCFIXFREQ1[7:0] 00000000
1777 0x002E, //AICCFIXFREQ0[23:16] 00000000
1778 0x002F, //AICCFIXFREQ0[15:8] 00000000
1779 0x0030, //AICCFIXFREQ0[7:0] 00000000
1780 0x0031, //1'b0 1'b1 1'b0 1'b0 x DAGC1STER
1781 0x0032, //DAGC1STEN DAGC1STER
1782 0x0033, //DAGC1STREF[15:8] 00001010
1783 0x0034, //DAGC1STREF[7:0] 11100100
1784 0x0035, //DAGC2NDE
1785 0x0036, //DAGC2NDREF[15:8] 00001010
1786 0x0037, //DAGC2NDREF[7:0] 10000000
1787 0x0038, //DAGC2NDLOCKDETRNGSEL[1:0]
1788 0x003D, //1'b1 SAMGEARS
1789 0x0040, //SAMLFGMA
1790 0x0041, //SAMLFBWM
1791 0x0044, //1'b1 CRGEARSHE
1792 0x0045, //CRLFGMAN
1793 0x0046, //CFLFBWMA
1794 0x0047, //CRLFGMAN
1795 0x0048, //x x x x CRLFGSTEP_VS[3:0] xxxx1001
1796 0x0049, //CRLFBWMA
1797 0x004A, //CRLFBWMA
1798 0x0050, //1'b0 1'b1 1'b1 1'b0 MSECALCDA
1799 0x0070, //TPOUTEN TPIFEN TPCLKOUTE
1800 0x0071, //TPSENB TPSSOPBITE
1801 0x0073, //TP47HINS x x CHBERINT PERMODE[1:0] PERINT[1:0] 1xx11100
1802 0x0075, //x x x x x IQSWAPCTRL[2:0] xxxxx000
1803 0x0076, //NBERCON NBERST NBERPOL NBERWSYN
1804 0x0077, //x NBERLOSTTH[2:0] NBERACQTH[3:0] x0000000
1805 0x0078, //NBERPOLY[31:24] 00000000
1806 0x0079, //NBERPOLY[23:16] 00000000
1807 0x007A, //NBERPOLY[15:8] 00000000
1808 0x007B, //NBERPOLY[7:0] 00000000
1809 0x007C, //NBERPED[31:24] 00000000
1810 0x007D, //NBERPED[23:16] 00000000
1811 0x007E, //NBERPED[15:8] 00000000
1812 0x007F, //NBERPED[7:0] 00000000
1813 0x0080, //x AGCLOCK DAGCLOCK SYSLOCK x x NEVERLOCK[1:0]
1814 0x0085, //SPECINVST
1815 0x0088, //SYSLOCKTIME[15:8]
1816 0x0089, //SYSLOCKTIME[7:0]
1817 0x008C, //FECLOCKTIME[15:8]
1818 0x008D, //FECLOCKTIME[7:0]
1819 0x008E, //AGCACCOUT[15:8]
1820 0x008F, //AGCACCOUT[7:0]
1821 0x0090, //AICCREJSTATUS[3:0] AICCREJBUSY[3:0]
1822 0x0091, //AICCVSYNC
1823 0x009C, //CARRFREQOFFSET[15:8]
1824 0x009D, //CARRFREQOFFSET[7:0]
1825 0x00A1, //SAMFREQOFFSET[23:16]
1826 0x00A2, //SAMFREQOFFSET[15:8]
1827 0x00A3, //SAMFREQOFFSET[7:0]
1828 0x00A6, //SYNCLOCK SYNCLOCKH
1829#if 0//covered elsewhere
1830 0x00E8, //CONSTPWR[15:8]
1831 0x00E9, //CONSTPWR[7:0]
1832 0x00EA, //BMSE[15:8]
1833 0x00EB, //BMSE[7:0]
1834 0x00EC, //MSE[15:8]
1835 0x00ED, //MSE[7:0]
1836 0x00EE, //CONSTI[7:0]
1837 0x00EF, //CONSTQ[7:0]
1838#endif
1839 0x00F4, //TPIFTPERRCNT[7:0]
1840 0x00F5, //TPCORREC
1841 0x00F6, //VBBER[15:8]
1842 0x00F7, //VBBER[7:0]
1843 0x00F8, //VABER[15:8]
1844 0x00F9, //VABER[7:0]
1845 0x00FA, //TPERRCNT[7:0]
1846 0x00FB, //NBERLOCK x x x x x x x
1847 0x00FC, //NBERVALUE[31:24]
1848 0x00FD, //NBERVALUE[23:16]
1849 0x00FE, //NBERVALUE[15:8]
1850 0x00FF, //NBERVALUE[7:0]
1851 0x1000, //1'b0 WODAGCOU
1852 0x1005, //x x 1'b1 1'b1 x SRD_Q_QM
1853 0x1009, //SRDWAITTIME[7:0] (10msec) 00100011
1854 0x100A, //SRDWAITTIME_CQS[7:0] (msec) 01100100
1855 0x101A, //x 1'b1 1'b0 1'b0 x QMDQAMMODE[2:0] x100x010
1856 0x1036, //1'b0 1'b1 1'b0 1'b0 SAMGSEND_CQS[3:0] 01001110
1857 0x103C, //SAMGSAUTOSTL_V[3:0] SAMGSAUTOEDL_V[3:0] 01000110
1858 0x103D, //1'b1 1'b1 SAMCNORMBP_V[1:0] 1'b0 1'b0 SAMMODESEL_V[1:0] 11100001
1859 0x103F, //SAMZTEDSE
1860 0x105D, //EQSTATUSE
1861 0x105F, //x PMAPG2_V[2:0] x DMAPG2_V[2:0] x001x011
1862 0x1060, //1'b1 EQSTATUSE
1863 0x1061, //CRMAPBWSTL_V[3:0] CRMAPBWEDL_V[3:0] 00000100
1864 0x1065, //1'b0 x CRMODE_V[1:0] 1'b1 x 1'b1 x 0x111x1x
1865 0x1066, //1'b0 1'b0 1'b1 1'b0 1'b1 PNBOOSTSE
1866 0x1068, //CREPHNGAIN2_V[3:0] CREPHNPBW_V[3:0] 10010001
1867 0x106E, //x x x x x CREPHNEN_
1868 0x106F, //CREPHNTH_V[7:0] 00010101
1869 0x1072, //CRSWEEPN
1870 0x1073, //CRPGAIN_V[3:0] x x 1'b1 1'b1 1001xx11
1871 0x1074, //CRPBW_V[3:0] x x 1'b1 1'b1 0001xx11
1872 0x1080, //DAFTSTATUS[1:0] x x x x x x
1873 0x1081, //SRDSTATUS[1:0] x x x x x SRDLOCK
1874 0x10A9, //EQSTATUS_CQS[1:0] x x x x x x
1875 0x10B7, //EQSTATUS_V[1:0] x x x x x x
1876#if 0//SMART_ANT
1877 0x1F00, //MODEDETE
1878 0x1F01, //x x x x x x x SFNRST xxxxxxx0
1879 0x1F03, //NUMOFANT[7:0] 10000000
1880 0x1F04, //x SELMASK[6:0] x0000000
1881 0x1F05, //x SETMASK[6:0] x0000000
1882 0x1F06, //x TXDATA[6:0] x0000000
1883 0x1F07, //x CHNUMBER[6:0] x0000000
1884 0x1F09, //AGCTIME[23:16] 10011000
1885 0x1F0A, //AGCTIME[15:8] 10010110
1886 0x1F0B, //AGCTIME[7:0] 10000000
1887 0x1F0C, //ANTTIME[31:24] 00000000
1888 0x1F0D, //ANTTIME[23:16] 00000011
1889 0x1F0E, //ANTTIME[15:8] 10010000
1890 0x1F0F, //ANTTIME[7:0] 10010000
1891 0x1F11, //SYNCTIME[23:16] 10011000
1892 0x1F12, //SYNCTIME[15:8] 10010110
1893 0x1F13, //SYNCTIME[7:0] 10000000
1894 0x1F14, //SNRTIME[31:24] 00000001
1895 0x1F15, //SNRTIME[23:16] 01111101
1896 0x1F16, //SNRTIME[15:8] 01111000
1897 0x1F17, //SNRTIME[7:0] 01000000
1898 0x1F19, //FECTIME[23:16] 00000000
1899 0x1F1A, //FECTIME[15:8] 01110010
1900 0x1F1B, //FECTIME[7:0] 01110000
1901 0x1F1D, //FECTHD[7:0] 00000011
1902 0x1F1F, //SNRTHD[23:16] 00001000
1903 0x1F20, //SNRTHD[15:8] 01111111
1904 0x1F21, //SNRTHD[7:0] 10000101
1905 0x1F80, //IRQFLG x x SFSDRFLG MODEBFLG SAVEFLG SCANFLG TRACKFLG
1906 0x1F81, //x SYNCCON SNRCON FECCON x STDBUSY SYNCRST AGCFZCO
1907 0x1F82, //x x x SCANOPCD[4:0]
1908 0x1F83, //x x x x MAINOPCD[3:0]
1909 0x1F84, //x x RXDATA[13:8]
1910 0x1F85, //RXDATA[7:0]
1911 0x1F86, //x x SDTDATA[13:8]
1912 0x1F87, //SDTDATA[7:0]
1913 0x1F89, //ANTSNR[23:16]
1914 0x1F8A, //ANTSNR[15:8]
1915 0x1F8B, //ANTSNR[7:0]
1916 0x1F8C, //x x x x ANTFEC[13:8]
1917 0x1F8D, //ANTFEC[7:0]
1918 0x1F8E, //MAXCNT[7:0]
1919 0x1F8F, //SCANCNT[7:0]
1920 0x1F91, //MAXPW[23:16]
1921 0x1F92, //MAXPW[15:8]
1922 0x1F93, //MAXPW[7:0]
1923 0x1F95, //CURPWMSE[23:16]
1924 0x1F96, //CURPWMSE[15:8]
1925 0x1F97, //CURPWMSE[7:0]
1926#endif//SMART_ANT
1927 0x211F, //1'b1 1'b1 1'b1 CIRQEN x x 1'b0 1'b0 1111xx00
1928 0x212A, //EQAUTOST
1929 0x2122, //CHFAST[7:0] 01100000
1930 0x212B, //FFFSTEP_V[3:0] x FBFSTEP_V[2:0] 0001x001
1931 0x212C, //PHDEROTBWSEL[3:0] 1'b1 1'b1 1'b1 1'b0 10001110
1932 0x212D, //1'b1 1'b1 1'b1 1'b1 x x TPIFLOCKS
1933 0x2135, //DYNTRACKFDEQ[3:0] x 1'b0 1'b0 1'b0 1010x000
1934 0x2141, //TRMODE[1:0] 1'b1 1'b1 1'b0 1'b1 1'b1 1'b1 01110111
1935 0x2162, //AICCCTRLE
1936 0x2173, //PHNCNFCNT[7:0] 00000100
1937 0x2179, //1'b0 1'b0 1'b0 1'b1 x BADSINGLEDYNTRACKFBF[2:0] 0001x001
1938 0x217A, //1'b0 1'b0 1'b0 1'b1 x BADSLOWSINGLEDYNTRACKFBF[2:0] 0001x001
1939 0x217E, //CNFCNTTPIF[7:0] 00001000
1940 0x217F, //TPERRCNTTPIF[7:0] 00000001
1941 0x2180, //x x x x x x FBDLYCIR[9:8]
1942 0x2181, //FBDLYCIR[7:0]
1943 0x2185, //MAXPWRMAIN[7:0]
1944 0x2191, //NCOMBDET x x x x x x x
1945 0x2199, //x MAINSTRON
1946 0x219A, //FFFEQSTEPOUT_V[3:0] FBFSTEPOUT_V[2:0]
1947 0x21A1, //x x SNRREF[5:0]
1948 0x2845, //1'b0 1'b1 x x FFFSTEP_CQS[1:0] FFFCENTERTAP[1:0] 01xx1110
1949 0x2846, //1'b0 x 1'b0 1'b1 FBFSTEP_CQS[1:0] 1'b1 1'b0 0x011110
1950 0x2847, //ENNOSIGDE
1951 0x2849, //1'b1 1'b1 NOUSENOSI
1952 0x284A, //EQINITWAITTIME[7:0] 01100100
1953 0x3000, //1'b1 1'b1 1'b1 x x x 1'b0 RPTRSTM
1954 0x3001, //RPTRSTWAITTIME[7:0] (100msec) 00110010
1955 0x3031, //FRAMELOC
1956 0x3032, //1'b1 1'b0 1'b0 1'b0 x x FRAMELOCKMODE_CQS[1:0] 1000xx11
1957 0x30A9, //VDLOCK_Q FRAMELOCK
1958 0x30AA, //MPEGLOCK
1959};
1960
1961#define numDumpRegs (sizeof(regtab)/sizeof(regtab[0]))
1962static u8 regval1[numDumpRegs] = {0, };
1963static u8 regval2[numDumpRegs] = {0, };
1964
1965static void lgdt3306a_DumpAllRegs(struct lgdt3306a_state *state)
1966{
1967 memset(regval2, 0xff, sizeof(regval2));
1968 lgdt3306a_DumpRegs(state);
1969}
1970
1971static void lgdt3306a_DumpRegs(struct lgdt3306a_state *state)
1972{
1973 int i;
1974 int sav_debug = debug;
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -03001975
Fred Richterb63b36f2014-03-24 19:56:00 -03001976 if ((debug & DBG_DUMP) == 0)
1977 return;
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -03001978 debug &= ~DBG_REG; /* supress DBG_REG during reg dump */
Fred Richterb63b36f2014-03-24 19:56:00 -03001979
1980 lg_info("\n");
1981
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -03001982 for (i = 0; i < numDumpRegs; i++) {
Fred Richterb63b36f2014-03-24 19:56:00 -03001983 lgdt3306a_read_reg(state, regtab[i], &regval1[i]);
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -03001984 if (regval1[i] != regval2[i]) {
1985 lg_info(" %04X = %02X\n", regtab[i], regval1[i]);
1986 regval2[i] = regval1[i];
Fred Richterb63b36f2014-03-24 19:56:00 -03001987 }
1988 }
1989 debug = sav_debug;
1990}
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -03001991#endif /* DBG_DUMP */
Fred Richterb63b36f2014-03-24 19:56:00 -03001992
1993
1994
Fred Richterb63b36f2014-03-24 19:56:00 -03001995static struct dvb_frontend_ops lgdt3306a_ops = {
1996 .delsys = { SYS_ATSC, SYS_DVBC_ANNEX_B },
1997 .info = {
1998 .name = "LG Electronics LGDT3306A VSB/QAM Frontend",
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -03001999#if 0
2000 .type = FE_ATSC,
2001#endif
Fred Richterb63b36f2014-03-24 19:56:00 -03002002 .frequency_min = 54000000,
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -03002003 .frequency_max = 858000000,
Fred Richterb63b36f2014-03-24 19:56:00 -03002004 .frequency_stepsize = 62500,
2005 .caps = FE_CAN_QAM_64 | FE_CAN_QAM_256 | FE_CAN_8VSB
2006 },
2007 .i2c_gate_ctrl = lgdt3306a_i2c_gate_ctrl,
2008 .init = lgdt3306a_init,
2009 .sleep = lgdt3306a_fe_sleep,
2010 /* if this is set, it overrides the default swzigzag */
2011 .tune = lgdt3306a_tune,
2012 .set_frontend = lgdt3306a_set_parameters,
2013 .get_frontend = lgdt3306a_get_frontend,
2014 .get_frontend_algo = lgdt3306a_get_frontend_algo,
2015 .get_tune_settings = lgdt3306a_get_tune_settings,
2016 .read_status = lgdt3306a_read_status,
2017 .read_ber = lgdt3306a_read_ber,
2018 .read_signal_strength = lgdt3306a_read_signal_strength,
2019 .read_snr = lgdt3306a_read_snr,
2020 .read_ucblocks = lgdt3306a_read_ucblocks,
2021 .release = lgdt3306a_release,
2022 .ts_bus_ctrl = lgdt3306a_ts_bus_ctrl,
2023 .search = lgdt3306a_search,
2024};
2025
2026MODULE_DESCRIPTION("LG Electronics LGDT3306A ATSC/QAM-B Demodulator Driver");
2027MODULE_AUTHOR("Fred Richter <frichter@hauppauge.com>");
2028MODULE_LICENSE("GPL");
2029MODULE_VERSION("0.2");
2030
2031/*
2032 * Local variables:
2033 * c-basic-offset: 8
2034 * End:
2035 */