blob: d2477edae197b9666100d9e3129e08a32426db74 [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 Chehab097117ca2014-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>
Thomas Meyer1f679ff2017-09-03 08:19:31 -040022#include <linux/kernel.h>
Fred Richterb63b36f2014-03-24 19:56:00 -030023#include <linux/dvb/frontend.h>
Mauro Carvalho Chehabfada1932017-12-28 13:03:51 -050024#include <media/dvb_math.h>
Fred Richterb63b36f2014-03-24 19:56:00 -030025#include "lgdt3306a.h"
Kevin Cheng4f751892017-01-10 01:14:18 -020026#include <linux/i2c-mux.h>
Fred Richterb63b36f2014-03-24 19:56:00 -030027
28
29static int debug;
30module_param(debug, int, 0644);
31MODULE_PARM_DESC(debug, "set debug level (info=1, reg=2 (or-able))");
32
33#define DBG_INFO 1
34#define DBG_REG 2
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -030035#define DBG_DUMP 4 /* FGR - comment out to remove dump code */
Fred Richterb63b36f2014-03-24 19:56:00 -030036
Mauro Carvalho Chehab097117ca2014-10-28 11:35:16 -020037#define lg_debug(fmt, arg...) \
38 printk(KERN_DEBUG pr_fmt(fmt), ## arg)
Fred Richterb63b36f2014-03-24 19:56:00 -030039
Mauro Carvalho Chehab097117ca2014-10-28 11:35:16 -020040#define dbg_info(fmt, arg...) \
41 do { \
42 if (debug & DBG_INFO) \
43 lg_debug(fmt, ## arg); \
44 } while (0)
45
46#define dbg_reg(fmt, arg...) \
47 do { \
48 if (debug & DBG_REG) \
49 lg_debug(fmt, ## arg); \
50 } while (0)
Fred Richterb63b36f2014-03-24 19:56:00 -030051
52#define lg_chkerr(ret) \
53({ \
54 int __ret; \
55 __ret = (ret < 0); \
56 if (__ret) \
Mauro Carvalho Chehab097117ca2014-10-28 11:35:16 -020057 pr_err("error %d on line %d\n", ret, __LINE__); \
Fred Richterb63b36f2014-03-24 19:56:00 -030058 __ret; \
59})
60
61struct lgdt3306a_state {
62 struct i2c_adapter *i2c_adap;
63 const struct lgdt3306a_config *cfg;
64
65 struct dvb_frontend frontend;
66
Mauro Carvalho Chehab0df289a2015-06-07 14:53:52 -030067 enum fe_modulation current_modulation;
Fred Richterb63b36f2014-03-24 19:56:00 -030068 u32 current_frequency;
69 u32 snr;
Kevin Cheng4f751892017-01-10 01:14:18 -020070
71 struct i2c_mux_core *muxc;
Fred Richterb63b36f2014-03-24 19:56:00 -030072};
73
Mauro Carvalho Chehab95f22c52014-10-28 12:40:20 -020074/*
75 * LG3306A Register Usage
76 * (LG does not really name the registers, so this code does not either)
77 *
78 * 0000 -> 00FF Common control and status
79 * 1000 -> 10FF Synchronizer control and status
80 * 1F00 -> 1FFF Smart Antenna control and status
81 * 2100 -> 21FF VSB Equalizer control and status
82 * 2800 -> 28FF QAM Equalizer control and status
83 * 3000 -> 30FF FEC control and status
84 */
Fred Richterb63b36f2014-03-24 19:56:00 -030085
Michael Ira Krufkyf883d602014-08-03 15:29:04 -030086enum lgdt3306a_lock_status {
87 LG3306_UNLOCK = 0x00,
88 LG3306_LOCK = 0x01,
Mauro Carvalho Chehab4937ba92014-10-28 10:50:36 -020089 LG3306_UNKNOWN_LOCK = 0xff
Michael Ira Krufkyf883d602014-08-03 15:29:04 -030090};
Fred Richterb63b36f2014-03-24 19:56:00 -030091
Michael Ira Krufkyf883d602014-08-03 15:29:04 -030092enum lgdt3306a_neverlock_status {
Fred Richterb63b36f2014-03-24 19:56:00 -030093 LG3306_NL_INIT = 0x00,
94 LG3306_NL_PROCESS = 0x01,
95 LG3306_NL_LOCK = 0x02,
96 LG3306_NL_FAIL = 0x03,
Mauro Carvalho Chehab4937ba92014-10-28 10:50:36 -020097 LG3306_NL_UNKNOWN = 0xff
Michael Ira Krufkyf883d602014-08-03 15:29:04 -030098};
Fred Richterb63b36f2014-03-24 19:56:00 -030099
Michael Ira Krufkyf883d602014-08-03 15:29:04 -0300100enum lgdt3306a_modulation {
101 LG3306_VSB = 0x00,
102 LG3306_QAM64 = 0x01,
103 LG3306_QAM256 = 0x02,
Mauro Carvalho Chehab4937ba92014-10-28 10:50:36 -0200104 LG3306_UNKNOWN_MODE = 0xff
Michael Ira Krufkyf883d602014-08-03 15:29:04 -0300105};
Fred Richterb63b36f2014-03-24 19:56:00 -0300106
Michael Ira Krufkyf883d602014-08-03 15:29:04 -0300107enum lgdt3306a_lock_check {
Fred Richterb63b36f2014-03-24 19:56:00 -0300108 LG3306_SYNC_LOCK,
109 LG3306_FEC_LOCK,
110 LG3306_TR_LOCK,
111 LG3306_AGC_LOCK,
Michael Ira Krufkyf883d602014-08-03 15:29:04 -0300112};
Fred Richterb63b36f2014-03-24 19:56:00 -0300113
114
115#ifdef DBG_DUMP
116static void lgdt3306a_DumpAllRegs(struct lgdt3306a_state *state);
117static void lgdt3306a_DumpRegs(struct lgdt3306a_state *state);
118#endif
119
120
121static int lgdt3306a_write_reg(struct lgdt3306a_state *state, u16 reg, u8 val)
122{
123 int ret;
124 u8 buf[] = { reg >> 8, reg & 0xff, val };
125 struct i2c_msg msg = {
126 .addr = state->cfg->i2c_addr, .flags = 0,
127 .buf = buf, .len = 3,
128 };
129
Mauro Carvalho Chehab097117ca2014-10-28 11:35:16 -0200130 dbg_reg("reg: 0x%04x, val: 0x%02x\n", reg, val);
Fred Richterb63b36f2014-03-24 19:56:00 -0300131
132 ret = i2c_transfer(state->i2c_adap, &msg, 1);
133
134 if (ret != 1) {
Mauro Carvalho Chehab097117ca2014-10-28 11:35:16 -0200135 pr_err("error (addr %02x %02x <- %02x, err = %i)\n",
Fred Richterb63b36f2014-03-24 19:56:00 -0300136 msg.buf[0], msg.buf[1], msg.buf[2], ret);
137 if (ret < 0)
138 return ret;
139 else
140 return -EREMOTEIO;
141 }
142 return 0;
143}
144
145static int lgdt3306a_read_reg(struct lgdt3306a_state *state, u16 reg, u8 *val)
146{
147 int ret;
148 u8 reg_buf[] = { reg >> 8, reg & 0xff };
149 struct i2c_msg msg[] = {
150 { .addr = state->cfg->i2c_addr,
151 .flags = 0, .buf = reg_buf, .len = 2 },
152 { .addr = state->cfg->i2c_addr,
153 .flags = I2C_M_RD, .buf = val, .len = 1 },
154 };
155
156 ret = i2c_transfer(state->i2c_adap, msg, 2);
157
158 if (ret != 2) {
Mauro Carvalho Chehab097117ca2014-10-28 11:35:16 -0200159 pr_err("error (addr %02x reg %04x error (ret == %i)\n",
Fred Richterb63b36f2014-03-24 19:56:00 -0300160 state->cfg->i2c_addr, reg, ret);
161 if (ret < 0)
162 return ret;
163 else
164 return -EREMOTEIO;
165 }
Mauro Carvalho Chehab097117ca2014-10-28 11:35:16 -0200166 dbg_reg("reg: 0x%04x, val: 0x%02x\n", reg, *val);
Fred Richterb63b36f2014-03-24 19:56:00 -0300167
168 return 0;
169}
170
171#define read_reg(state, reg) \
172({ \
173 u8 __val; \
174 int ret = lgdt3306a_read_reg(state, reg, &__val); \
175 if (lg_chkerr(ret)) \
176 __val = 0; \
177 __val; \
178})
179
180static int lgdt3306a_set_reg_bit(struct lgdt3306a_state *state,
181 u16 reg, int bit, int onoff)
182{
183 u8 val;
184 int ret;
185
Mauro Carvalho Chehab097117ca2014-10-28 11:35:16 -0200186 dbg_reg("reg: 0x%04x, bit: %d, level: %d\n", reg, bit, onoff);
Fred Richterb63b36f2014-03-24 19:56:00 -0300187
188 ret = lgdt3306a_read_reg(state, reg, &val);
189 if (lg_chkerr(ret))
190 goto fail;
191
192 val &= ~(1 << bit);
193 val |= (onoff & 1) << bit;
194
195 ret = lgdt3306a_write_reg(state, reg, val);
196 lg_chkerr(ret);
197fail:
198 return ret;
199}
200
201/* ------------------------------------------------------------------------ */
202
203static int lgdt3306a_soft_reset(struct lgdt3306a_state *state)
204{
205 int ret;
206
Mauro Carvalho Chehab097117ca2014-10-28 11:35:16 -0200207 dbg_info("\n");
Fred Richterb63b36f2014-03-24 19:56:00 -0300208
209 ret = lgdt3306a_set_reg_bit(state, 0x0000, 7, 0);
210 if (lg_chkerr(ret))
211 goto fail;
212
213 msleep(20);
214 ret = lgdt3306a_set_reg_bit(state, 0x0000, 7, 1);
215 lg_chkerr(ret);
216
217fail:
218 return ret;
219}
220
221static int lgdt3306a_mpeg_mode(struct lgdt3306a_state *state,
222 enum lgdt3306a_mpeg_mode mode)
223{
224 u8 val;
225 int ret;
226
Mauro Carvalho Chehab097117ca2014-10-28 11:35:16 -0200227 dbg_info("(%d)\n", mode);
Mauro Carvalho Chehab534f4362014-10-28 12:35:18 -0200228 /* transport packet format - TPSENB=0x80 */
229 ret = lgdt3306a_set_reg_bit(state, 0x0071, 7,
230 mode == LGDT3306A_MPEG_PARALLEL ? 1 : 0);
Fred Richterb63b36f2014-03-24 19:56:00 -0300231 if (lg_chkerr(ret))
232 goto fail;
233
Mauro Carvalho Chehab534f4362014-10-28 12:35:18 -0200234 /*
235 * start of packet signal duration
236 * TPSSOPBITEN=0x40; 0=byte duration, 1=bit duration
237 */
238 ret = lgdt3306a_set_reg_bit(state, 0x0071, 6, 0);
Fred Richterb63b36f2014-03-24 19:56:00 -0300239 if (lg_chkerr(ret))
240 goto fail;
241
242 ret = lgdt3306a_read_reg(state, 0x0070, &val);
243 if (lg_chkerr(ret))
244 goto fail;
245
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -0300246 val |= 0x10; /* TPCLKSUPB=0x10 */
Fred Richterb63b36f2014-03-24 19:56:00 -0300247
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -0300248 if (mode == LGDT3306A_MPEG_PARALLEL)
Fred Richterb63b36f2014-03-24 19:56:00 -0300249 val &= ~0x10;
250
251 ret = lgdt3306a_write_reg(state, 0x0070, val);
252 lg_chkerr(ret);
253
254fail:
255 return ret;
256}
257
258static int lgdt3306a_mpeg_mode_polarity(struct lgdt3306a_state *state,
259 enum lgdt3306a_tp_clock_edge edge,
260 enum lgdt3306a_tp_valid_polarity valid)
261{
262 u8 val;
263 int ret;
264
Mauro Carvalho Chehab097117ca2014-10-28 11:35:16 -0200265 dbg_info("edge=%d, valid=%d\n", edge, valid);
Fred Richterb63b36f2014-03-24 19:56:00 -0300266
267 ret = lgdt3306a_read_reg(state, 0x0070, &val);
268 if (lg_chkerr(ret))
269 goto fail;
270
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -0300271 val &= ~0x06; /* TPCLKPOL=0x04, TPVALPOL=0x02 */
Fred Richterb63b36f2014-03-24 19:56:00 -0300272
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -0300273 if (edge == LGDT3306A_TPCLK_RISING_EDGE)
Fred Richterb63b36f2014-03-24 19:56:00 -0300274 val |= 0x04;
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -0300275 if (valid == LGDT3306A_TP_VALID_HIGH)
Fred Richterb63b36f2014-03-24 19:56:00 -0300276 val |= 0x02;
277
278 ret = lgdt3306a_write_reg(state, 0x0070, val);
279 lg_chkerr(ret);
280
281fail:
282 return ret;
283}
284
285static int lgdt3306a_mpeg_tristate(struct lgdt3306a_state *state,
286 int mode)
287{
288 u8 val;
289 int ret;
290
Mauro Carvalho Chehab097117ca2014-10-28 11:35:16 -0200291 dbg_info("(%d)\n", mode);
Fred Richterb63b36f2014-03-24 19:56:00 -0300292
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -0300293 if (mode) {
Fred Richterb63b36f2014-03-24 19:56:00 -0300294 ret = lgdt3306a_read_reg(state, 0x0070, &val);
295 if (lg_chkerr(ret))
296 goto fail;
Mauro Carvalho Chehab534f4362014-10-28 12:35:18 -0200297 /*
298 * Tristate bus; TPOUTEN=0x80, TPCLKOUTEN=0x20,
299 * TPDATAOUTEN=0x08
300 */
301 val &= ~0xa8;
Fred Richterb63b36f2014-03-24 19:56:00 -0300302 ret = lgdt3306a_write_reg(state, 0x0070, val);
303 if (lg_chkerr(ret))
304 goto fail;
305
Mauro Carvalho Chehab534f4362014-10-28 12:35:18 -0200306 /* AGCIFOUTENB=0x40; 1=Disable IFAGC pin */
307 ret = lgdt3306a_set_reg_bit(state, 0x0003, 6, 1);
Fred Richterb63b36f2014-03-24 19:56:00 -0300308 if (lg_chkerr(ret))
309 goto fail;
310
311 } else {
Mauro Carvalho Chehab534f4362014-10-28 12:35:18 -0200312 /* enable IFAGC pin */
313 ret = lgdt3306a_set_reg_bit(state, 0x0003, 6, 0);
Fred Richterb63b36f2014-03-24 19:56:00 -0300314 if (lg_chkerr(ret))
315 goto fail;
316
317 ret = lgdt3306a_read_reg(state, 0x0070, &val);
318 if (lg_chkerr(ret))
319 goto fail;
320
Mauro Carvalho Chehab4937ba92014-10-28 10:50:36 -0200321 val |= 0xa8; /* enable bus */
Fred Richterb63b36f2014-03-24 19:56:00 -0300322 ret = lgdt3306a_write_reg(state, 0x0070, val);
323 if (lg_chkerr(ret))
324 goto fail;
325 }
326
327fail:
328 return ret;
329}
330
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -0300331static int lgdt3306a_ts_bus_ctrl(struct dvb_frontend *fe, int acquire)
Fred Richterb63b36f2014-03-24 19:56:00 -0300332{
333 struct lgdt3306a_state *state = fe->demodulator_priv;
334
Mauro Carvalho Chehab097117ca2014-10-28 11:35:16 -0200335 dbg_info("acquire=%d\n", acquire);
Fred Richterb63b36f2014-03-24 19:56:00 -0300336
337 return lgdt3306a_mpeg_tristate(state, acquire ? 0 : 1);
338
339}
340
341static int lgdt3306a_power(struct lgdt3306a_state *state,
342 int mode)
343{
344 int ret;
345
Mauro Carvalho Chehab097117ca2014-10-28 11:35:16 -0200346 dbg_info("(%d)\n", mode);
Fred Richterb63b36f2014-03-24 19:56:00 -0300347
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -0300348 if (mode == 0) {
Mauro Carvalho Chehab534f4362014-10-28 12:35:18 -0200349 /* into reset */
350 ret = lgdt3306a_set_reg_bit(state, 0x0000, 7, 0);
Fred Richterb63b36f2014-03-24 19:56:00 -0300351 if (lg_chkerr(ret))
352 goto fail;
353
Mauro Carvalho Chehab534f4362014-10-28 12:35:18 -0200354 /* power down */
355 ret = lgdt3306a_set_reg_bit(state, 0x0000, 0, 0);
Fred Richterb63b36f2014-03-24 19:56:00 -0300356 if (lg_chkerr(ret))
357 goto fail;
358
359 } else {
Mauro Carvalho Chehab534f4362014-10-28 12:35:18 -0200360 /* out of reset */
361 ret = lgdt3306a_set_reg_bit(state, 0x0000, 7, 1);
Fred Richterb63b36f2014-03-24 19:56:00 -0300362 if (lg_chkerr(ret))
363 goto fail;
364
Mauro Carvalho Chehab534f4362014-10-28 12:35:18 -0200365 /* power up */
366 ret = lgdt3306a_set_reg_bit(state, 0x0000, 0, 1);
Fred Richterb63b36f2014-03-24 19:56:00 -0300367 if (lg_chkerr(ret))
368 goto fail;
369 }
370
371#ifdef DBG_DUMP
372 lgdt3306a_DumpAllRegs(state);
373#endif
374fail:
375 return ret;
376}
377
378
379static int lgdt3306a_set_vsb(struct lgdt3306a_state *state)
380{
381 u8 val;
382 int ret;
383
Mauro Carvalho Chehab097117ca2014-10-28 11:35:16 -0200384 dbg_info("\n");
Fred Richterb63b36f2014-03-24 19:56:00 -0300385
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -0300386 /* 0. Spectrum inversion detection manual; spectrum inverted */
Fred Richterb63b36f2014-03-24 19:56:00 -0300387 ret = lgdt3306a_read_reg(state, 0x0002, &val);
Mauro Carvalho Chehab4937ba92014-10-28 10:50:36 -0200388 val &= 0xf7; /* SPECINVAUTO Off */
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -0300389 val |= 0x04; /* SPECINV On */
Fred Richterb63b36f2014-03-24 19:56:00 -0300390 ret = lgdt3306a_write_reg(state, 0x0002, val);
391 if (lg_chkerr(ret))
392 goto fail;
393
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -0300394 /* 1. Selection of standard mode(0x08=QAM, 0x80=VSB) */
Fred Richterb63b36f2014-03-24 19:56:00 -0300395 ret = lgdt3306a_write_reg(state, 0x0008, 0x80);
396 if (lg_chkerr(ret))
397 goto fail;
398
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -0300399 /* 2. Bandwidth mode for VSB(6MHz) */
Fred Richterb63b36f2014-03-24 19:56:00 -0300400 ret = lgdt3306a_read_reg(state, 0x0009, &val);
Mauro Carvalho Chehab4937ba92014-10-28 10:50:36 -0200401 val &= 0xe3;
402 val |= 0x0c; /* STDOPDETTMODE[2:0]=3 */
Fred Richterb63b36f2014-03-24 19:56:00 -0300403 ret = lgdt3306a_write_reg(state, 0x0009, val);
404 if (lg_chkerr(ret))
405 goto fail;
406
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -0300407 /* 3. QAM mode detection mode(None) */
Fred Richterb63b36f2014-03-24 19:56:00 -0300408 ret = lgdt3306a_read_reg(state, 0x0009, &val);
Mauro Carvalho Chehab4937ba92014-10-28 10:50:36 -0200409 val &= 0xfc; /* STDOPDETCMODE[1:0]=0 */
Fred Richterb63b36f2014-03-24 19:56:00 -0300410 ret = lgdt3306a_write_reg(state, 0x0009, val);
411 if (lg_chkerr(ret))
412 goto fail;
413
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -0300414 /* 4. ADC sampling frequency rate(2x sampling) */
Mauro Carvalho Chehab4937ba92014-10-28 10:50:36 -0200415 ret = lgdt3306a_read_reg(state, 0x000d, &val);
416 val &= 0xbf; /* SAMPLING4XFEN=0 */
417 ret = lgdt3306a_write_reg(state, 0x000d, val);
Fred Richterb63b36f2014-03-24 19:56:00 -0300418 if (lg_chkerr(ret))
419 goto fail;
420
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -0300421#if 0
422 /* FGR - disable any AICC filtering, testing only */
423
Fred Richterb63b36f2014-03-24 19:56:00 -0300424 ret = lgdt3306a_write_reg(state, 0x0024, 0x00);
425 if (lg_chkerr(ret))
426 goto fail;
427
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -0300428 /* AICCFIXFREQ0 NT N-1(Video rejection) */
Mauro Carvalho Chehab4937ba92014-10-28 10:50:36 -0200429 ret = lgdt3306a_write_reg(state, 0x002e, 0x00);
430 ret = lgdt3306a_write_reg(state, 0x002f, 0x00);
Fred Richterb63b36f2014-03-24 19:56:00 -0300431 ret = lgdt3306a_write_reg(state, 0x0030, 0x00);
432
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -0300433 /* AICCFIXFREQ1 NT N-1(Audio rejection) */
Mauro Carvalho Chehab4937ba92014-10-28 10:50:36 -0200434 ret = lgdt3306a_write_reg(state, 0x002b, 0x00);
435 ret = lgdt3306a_write_reg(state, 0x002c, 0x00);
436 ret = lgdt3306a_write_reg(state, 0x002d, 0x00);
Fred Richterb63b36f2014-03-24 19:56:00 -0300437
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -0300438 /* AICCFIXFREQ2 NT Co-Channel(Video rejection) */
Fred Richterb63b36f2014-03-24 19:56:00 -0300439 ret = lgdt3306a_write_reg(state, 0x0028, 0x00);
440 ret = lgdt3306a_write_reg(state, 0x0029, 0x00);
Mauro Carvalho Chehab4937ba92014-10-28 10:50:36 -0200441 ret = lgdt3306a_write_reg(state, 0x002a, 0x00);
Fred Richterb63b36f2014-03-24 19:56:00 -0300442
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -0300443 /* AICCFIXFREQ3 NT Co-Channel(Audio rejection) */
Fred Richterb63b36f2014-03-24 19:56:00 -0300444 ret = lgdt3306a_write_reg(state, 0x0025, 0x00);
445 ret = lgdt3306a_write_reg(state, 0x0026, 0x00);
446 ret = lgdt3306a_write_reg(state, 0x0027, 0x00);
447
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -0300448#else
449 /* FGR - this works well for HVR-1955,1975 */
450
451 /* 5. AICCOPMODE NT N-1 Adj. */
Fred Richterb63b36f2014-03-24 19:56:00 -0300452 ret = lgdt3306a_write_reg(state, 0x0024, 0x5A);
453 if (lg_chkerr(ret))
454 goto fail;
455
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -0300456 /* AICCFIXFREQ0 NT N-1(Video rejection) */
Mauro Carvalho Chehab4937ba92014-10-28 10:50:36 -0200457 ret = lgdt3306a_write_reg(state, 0x002e, 0x5A);
458 ret = lgdt3306a_write_reg(state, 0x002f, 0x00);
Fred Richterb63b36f2014-03-24 19:56:00 -0300459 ret = lgdt3306a_write_reg(state, 0x0030, 0x00);
460
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -0300461 /* AICCFIXFREQ1 NT N-1(Audio rejection) */
Mauro Carvalho Chehab4937ba92014-10-28 10:50:36 -0200462 ret = lgdt3306a_write_reg(state, 0x002b, 0x36);
463 ret = lgdt3306a_write_reg(state, 0x002c, 0x00);
464 ret = lgdt3306a_write_reg(state, 0x002d, 0x00);
Fred Richterb63b36f2014-03-24 19:56:00 -0300465
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -0300466 /* AICCFIXFREQ2 NT Co-Channel(Video rejection) */
Fred Richterb63b36f2014-03-24 19:56:00 -0300467 ret = lgdt3306a_write_reg(state, 0x0028, 0x2A);
468 ret = lgdt3306a_write_reg(state, 0x0029, 0x00);
Mauro Carvalho Chehab4937ba92014-10-28 10:50:36 -0200469 ret = lgdt3306a_write_reg(state, 0x002a, 0x00);
Fred Richterb63b36f2014-03-24 19:56:00 -0300470
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -0300471 /* AICCFIXFREQ3 NT Co-Channel(Audio rejection) */
Fred Richterb63b36f2014-03-24 19:56:00 -0300472 ret = lgdt3306a_write_reg(state, 0x0025, 0x06);
473 ret = lgdt3306a_write_reg(state, 0x0026, 0x00);
474 ret = lgdt3306a_write_reg(state, 0x0027, 0x00);
475#endif
476
Mauro Carvalho Chehab4937ba92014-10-28 10:50:36 -0200477 ret = lgdt3306a_read_reg(state, 0x001e, &val);
478 val &= 0x0f;
479 val |= 0xa0;
480 ret = lgdt3306a_write_reg(state, 0x001e, val);
Fred Richterb63b36f2014-03-24 19:56:00 -0300481
482 ret = lgdt3306a_write_reg(state, 0x0022, 0x08);
483
484 ret = lgdt3306a_write_reg(state, 0x0023, 0xFF);
485
Mauro Carvalho Chehab4937ba92014-10-28 10:50:36 -0200486 ret = lgdt3306a_read_reg(state, 0x211f, &val);
487 val &= 0xef;
488 ret = lgdt3306a_write_reg(state, 0x211f, val);
Fred Richterb63b36f2014-03-24 19:56:00 -0300489
490 ret = lgdt3306a_write_reg(state, 0x2173, 0x01);
491
492 ret = lgdt3306a_read_reg(state, 0x1061, &val);
Mauro Carvalho Chehab4937ba92014-10-28 10:50:36 -0200493 val &= 0xf8;
Fred Richterb63b36f2014-03-24 19:56:00 -0300494 val |= 0x04;
495 ret = lgdt3306a_write_reg(state, 0x1061, val);
496
Mauro Carvalho Chehab4937ba92014-10-28 10:50:36 -0200497 ret = lgdt3306a_read_reg(state, 0x103d, &val);
498 val &= 0xcf;
499 ret = lgdt3306a_write_reg(state, 0x103d, val);
Fred Richterb63b36f2014-03-24 19:56:00 -0300500
501 ret = lgdt3306a_write_reg(state, 0x2122, 0x40);
502
503 ret = lgdt3306a_read_reg(state, 0x2141, &val);
Mauro Carvalho Chehab4937ba92014-10-28 10:50:36 -0200504 val &= 0x3f;
Fred Richterb63b36f2014-03-24 19:56:00 -0300505 ret = lgdt3306a_write_reg(state, 0x2141, val);
506
507 ret = lgdt3306a_read_reg(state, 0x2135, &val);
Mauro Carvalho Chehab4937ba92014-10-28 10:50:36 -0200508 val &= 0x0f;
Fred Richterb63b36f2014-03-24 19:56:00 -0300509 val |= 0x70;
510 ret = lgdt3306a_write_reg(state, 0x2135, val);
511
512 ret = lgdt3306a_read_reg(state, 0x0003, &val);
Mauro Carvalho Chehab4937ba92014-10-28 10:50:36 -0200513 val &= 0xf7;
Fred Richterb63b36f2014-03-24 19:56:00 -0300514 ret = lgdt3306a_write_reg(state, 0x0003, val);
515
Mauro Carvalho Chehab4937ba92014-10-28 10:50:36 -0200516 ret = lgdt3306a_read_reg(state, 0x001c, &val);
517 val &= 0x7f;
518 ret = lgdt3306a_write_reg(state, 0x001c, val);
Fred Richterb63b36f2014-03-24 19:56:00 -0300519
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -0300520 /* 6. EQ step size */
Fred Richterb63b36f2014-03-24 19:56:00 -0300521 ret = lgdt3306a_read_reg(state, 0x2179, &val);
Mauro Carvalho Chehab4937ba92014-10-28 10:50:36 -0200522 val &= 0xf8;
Fred Richterb63b36f2014-03-24 19:56:00 -0300523 ret = lgdt3306a_write_reg(state, 0x2179, val);
524
Mauro Carvalho Chehab4937ba92014-10-28 10:50:36 -0200525 ret = lgdt3306a_read_reg(state, 0x217a, &val);
526 val &= 0xf8;
527 ret = lgdt3306a_write_reg(state, 0x217a, val);
Fred Richterb63b36f2014-03-24 19:56:00 -0300528
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -0300529 /* 7. Reset */
Fred Richterb63b36f2014-03-24 19:56:00 -0300530 ret = lgdt3306a_soft_reset(state);
531 if (lg_chkerr(ret))
532 goto fail;
533
Mauro Carvalho Chehab097117ca2014-10-28 11:35:16 -0200534 dbg_info("complete\n");
Fred Richterb63b36f2014-03-24 19:56:00 -0300535fail:
536 return ret;
537}
538
539static int lgdt3306a_set_qam(struct lgdt3306a_state *state, int modulation)
540{
541 u8 val;
542 int ret;
543
Mauro Carvalho Chehab097117ca2014-10-28 11:35:16 -0200544 dbg_info("modulation=%d\n", modulation);
Fred Richterb63b36f2014-03-24 19:56:00 -0300545
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -0300546 /* 1. Selection of standard mode(0x08=QAM, 0x80=VSB) */
Fred Richterb63b36f2014-03-24 19:56:00 -0300547 ret = lgdt3306a_write_reg(state, 0x0008, 0x08);
548 if (lg_chkerr(ret))
549 goto fail;
550
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -0300551 /* 1a. Spectrum inversion detection to Auto */
Fred Richterb63b36f2014-03-24 19:56:00 -0300552 ret = lgdt3306a_read_reg(state, 0x0002, &val);
Mauro Carvalho Chehab4937ba92014-10-28 10:50:36 -0200553 val &= 0xfb; /* SPECINV Off */
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -0300554 val |= 0x08; /* SPECINVAUTO On */
Fred Richterb63b36f2014-03-24 19:56:00 -0300555 ret = lgdt3306a_write_reg(state, 0x0002, val);
556 if (lg_chkerr(ret))
557 goto fail;
558
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -0300559 /* 2. Bandwidth mode for QAM */
Fred Richterb63b36f2014-03-24 19:56:00 -0300560 ret = lgdt3306a_read_reg(state, 0x0009, &val);
Mauro Carvalho Chehab4937ba92014-10-28 10:50:36 -0200561 val &= 0xe3; /* STDOPDETTMODE[2:0]=0 VSB Off */
Fred Richterb63b36f2014-03-24 19:56:00 -0300562 ret = lgdt3306a_write_reg(state, 0x0009, val);
563 if (lg_chkerr(ret))
564 goto fail;
565
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -0300566 /* 3. : 64QAM/256QAM detection(manual, auto) */
Fred Richterb63b36f2014-03-24 19:56:00 -0300567 ret = lgdt3306a_read_reg(state, 0x0009, &val);
Mauro Carvalho Chehab4937ba92014-10-28 10:50:36 -0200568 val &= 0xfc;
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -0300569 val |= 0x02; /* STDOPDETCMODE[1:0]=1=Manual 2=Auto */
Fred Richterb63b36f2014-03-24 19:56:00 -0300570 ret = lgdt3306a_write_reg(state, 0x0009, val);
571 if (lg_chkerr(ret))
572 goto fail;
573
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -0300574 /* 3a. : 64QAM/256QAM selection for manual */
Fred Richterb63b36f2014-03-24 19:56:00 -0300575 ret = lgdt3306a_read_reg(state, 0x101a, &val);
Mauro Carvalho Chehab4937ba92014-10-28 10:50:36 -0200576 val &= 0xf8;
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -0300577 if (modulation == QAM_64)
578 val |= 0x02; /* QMDQMODE[2:0]=2=QAM64 */
579 else
580 val |= 0x04; /* QMDQMODE[2:0]=4=QAM256 */
581
Fred Richterb63b36f2014-03-24 19:56:00 -0300582 ret = lgdt3306a_write_reg(state, 0x101a, val);
583 if (lg_chkerr(ret))
584 goto fail;
585
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -0300586 /* 4. ADC sampling frequency rate(4x sampling) */
Mauro Carvalho Chehab4937ba92014-10-28 10:50:36 -0200587 ret = lgdt3306a_read_reg(state, 0x000d, &val);
588 val &= 0xbf;
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -0300589 val |= 0x40; /* SAMPLING4XFEN=1 */
Mauro Carvalho Chehab4937ba92014-10-28 10:50:36 -0200590 ret = lgdt3306a_write_reg(state, 0x000d, val);
Fred Richterb63b36f2014-03-24 19:56:00 -0300591 if (lg_chkerr(ret))
592 goto fail;
593
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -0300594 /* 5. No AICC operation in QAM mode */
Fred Richterb63b36f2014-03-24 19:56:00 -0300595 ret = lgdt3306a_read_reg(state, 0x0024, &val);
596 val &= 0x00;
597 ret = lgdt3306a_write_reg(state, 0x0024, val);
598 if (lg_chkerr(ret))
599 goto fail;
600
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -0300601 /* 6. Reset */
Fred Richterb63b36f2014-03-24 19:56:00 -0300602 ret = lgdt3306a_soft_reset(state);
603 if (lg_chkerr(ret))
604 goto fail;
605
Mauro Carvalho Chehab097117ca2014-10-28 11:35:16 -0200606 dbg_info("complete\n");
Fred Richterb63b36f2014-03-24 19:56:00 -0300607fail:
608 return ret;
609}
610
611static int lgdt3306a_set_modulation(struct lgdt3306a_state *state,
612 struct dtv_frontend_properties *p)
613{
614 int ret;
615
Mauro Carvalho Chehab097117ca2014-10-28 11:35:16 -0200616 dbg_info("\n");
Fred Richterb63b36f2014-03-24 19:56:00 -0300617
618 switch (p->modulation) {
619 case VSB_8:
620 ret = lgdt3306a_set_vsb(state);
621 break;
622 case QAM_64:
623 ret = lgdt3306a_set_qam(state, QAM_64);
624 break;
625 case QAM_256:
626 ret = lgdt3306a_set_qam(state, QAM_256);
627 break;
628 default:
629 return -EINVAL;
630 }
631 if (lg_chkerr(ret))
632 goto fail;
633
634 state->current_modulation = p->modulation;
635
636fail:
637 return ret;
638}
639
640/* ------------------------------------------------------------------------ */
641
642static int lgdt3306a_agc_setup(struct lgdt3306a_state *state,
643 struct dtv_frontend_properties *p)
644{
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -0300645 /* TODO: anything we want to do here??? */
Mauro Carvalho Chehab097117ca2014-10-28 11:35:16 -0200646 dbg_info("\n");
Fred Richterb63b36f2014-03-24 19:56:00 -0300647
648 switch (p->modulation) {
649 case VSB_8:
650 break;
651 case QAM_64:
652 case QAM_256:
653 break;
654 default:
655 return -EINVAL;
656 }
657 return 0;
658}
659
660/* ------------------------------------------------------------------------ */
661
662static int lgdt3306a_set_inversion(struct lgdt3306a_state *state,
663 int inversion)
664{
665 int ret;
666
Mauro Carvalho Chehab097117ca2014-10-28 11:35:16 -0200667 dbg_info("(%d)\n", inversion);
Fred Richterb63b36f2014-03-24 19:56:00 -0300668
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -0300669 ret = lgdt3306a_set_reg_bit(state, 0x0002, 2, inversion ? 1 : 0);
Fred Richterb63b36f2014-03-24 19:56:00 -0300670 return ret;
671}
672
673static int lgdt3306a_set_inversion_auto(struct lgdt3306a_state *state,
674 int enabled)
675{
676 int ret;
677
Mauro Carvalho Chehab097117ca2014-10-28 11:35:16 -0200678 dbg_info("(%d)\n", enabled);
Fred Richterb63b36f2014-03-24 19:56:00 -0300679
Mauro Carvalho Chehab534f4362014-10-28 12:35:18 -0200680 /* 0=Manual 1=Auto(QAM only) - SPECINVAUTO=0x04 */
681 ret = lgdt3306a_set_reg_bit(state, 0x0002, 3, enabled);
Fred Richterb63b36f2014-03-24 19:56:00 -0300682 return ret;
683}
684
685static int lgdt3306a_spectral_inversion(struct lgdt3306a_state *state,
686 struct dtv_frontend_properties *p,
687 int inversion)
688{
689 int ret = 0;
690
Mauro Carvalho Chehab097117ca2014-10-28 11:35:16 -0200691 dbg_info("(%d)\n", inversion);
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -0300692#if 0
Mauro Carvalho Chehab534f4362014-10-28 12:35:18 -0200693 /*
694 * FGR - spectral_inversion defaults already set for VSB and QAM;
695 * can enable later if desired
696 */
Fred Richterb63b36f2014-03-24 19:56:00 -0300697
698 ret = lgdt3306a_set_inversion(state, inversion);
699
700 switch (p->modulation) {
701 case VSB_8:
Mauro Carvalho Chehab534f4362014-10-28 12:35:18 -0200702 /* Manual only for VSB */
703 ret = lgdt3306a_set_inversion_auto(state, 0);
Fred Richterb63b36f2014-03-24 19:56:00 -0300704 break;
705 case QAM_64:
706 case QAM_256:
Mauro Carvalho Chehab534f4362014-10-28 12:35:18 -0200707 /* Auto ok for QAM */
708 ret = lgdt3306a_set_inversion_auto(state, 1);
Fred Richterb63b36f2014-03-24 19:56:00 -0300709 break;
710 default:
711 ret = -EINVAL;
712 }
713#endif
714 return ret;
715}
716
717static int lgdt3306a_set_if(struct lgdt3306a_state *state,
718 struct dtv_frontend_properties *p)
719{
720 int ret;
721 u16 if_freq_khz;
722 u8 nco1, nco2;
723
724 switch (p->modulation) {
725 case VSB_8:
726 if_freq_khz = state->cfg->vsb_if_khz;
727 break;
728 case QAM_64:
729 case QAM_256:
730 if_freq_khz = state->cfg->qam_if_khz;
731 break;
732 default:
733 return -EINVAL;
734 }
735
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -0300736 switch (if_freq_khz) {
Fred Richterb63b36f2014-03-24 19:56:00 -0300737 default:
Colin Ian Kingf86548c2016-09-01 08:09:41 -0300738 pr_warn("IF=%d KHz is not supported, 3250 assumed\n",
Mauro Carvalho Chehab534f4362014-10-28 12:35:18 -0200739 if_freq_khz);
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -0300740 /* fallthrough */
Michael Ira Krufky34a5a2f2014-10-25 11:26:15 -0300741 case 3250: /* 3.25Mhz */
Fred Richterb63b36f2014-03-24 19:56:00 -0300742 nco1 = 0x34;
743 nco2 = 0x00;
744 break;
Michael Ira Krufky34a5a2f2014-10-25 11:26:15 -0300745 case 3500: /* 3.50Mhz */
Fred Richterb63b36f2014-03-24 19:56:00 -0300746 nco1 = 0x38;
747 nco2 = 0x00;
748 break;
Michael Ira Krufky34a5a2f2014-10-25 11:26:15 -0300749 case 4000: /* 4.00Mhz */
Fred Richterb63b36f2014-03-24 19:56:00 -0300750 nco1 = 0x40;
751 nco2 = 0x00;
752 break;
Michael Ira Krufky34a5a2f2014-10-25 11:26:15 -0300753 case 5000: /* 5.00Mhz */
Fred Richterb63b36f2014-03-24 19:56:00 -0300754 nco1 = 0x50;
755 nco2 = 0x00;
756 break;
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -0300757 case 5380: /* 5.38Mhz */
Fred Richterb63b36f2014-03-24 19:56:00 -0300758 nco1 = 0x56;
759 nco2 = 0x14;
760 break;
761 }
762 ret = lgdt3306a_write_reg(state, 0x0010, nco1);
Mauro Carvalho Chehabee0133e2014-10-28 11:21:48 -0200763 if (ret)
764 return ret;
Fred Richterb63b36f2014-03-24 19:56:00 -0300765 ret = lgdt3306a_write_reg(state, 0x0011, nco2);
Mauro Carvalho Chehabee0133e2014-10-28 11:21:48 -0200766 if (ret)
767 return ret;
Fred Richterb63b36f2014-03-24 19:56:00 -0300768
Mauro Carvalho Chehab097117ca2014-10-28 11:35:16 -0200769 dbg_info("if_freq=%d KHz->[%04x]\n", if_freq_khz, nco1<<8 | nco2);
Fred Richterb63b36f2014-03-24 19:56:00 -0300770
771 return 0;
772}
773
774/* ------------------------------------------------------------------------ */
775
776static int lgdt3306a_i2c_gate_ctrl(struct dvb_frontend *fe, int enable)
777{
778 struct lgdt3306a_state *state = fe->demodulator_priv;
779
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -0300780 if (state->cfg->deny_i2c_rptr) {
Mauro Carvalho Chehab097117ca2014-10-28 11:35:16 -0200781 dbg_info("deny_i2c_rptr=%d\n", state->cfg->deny_i2c_rptr);
Fred Richterb63b36f2014-03-24 19:56:00 -0300782 return 0;
783 }
Mauro Carvalho Chehab097117ca2014-10-28 11:35:16 -0200784 dbg_info("(%d)\n", enable);
Fred Richterb63b36f2014-03-24 19:56:00 -0300785
Mauro Carvalho Chehab534f4362014-10-28 12:35:18 -0200786 /* NI2CRPTEN=0x80 */
787 return lgdt3306a_set_reg_bit(state, 0x0002, 7, enable ? 0 : 1);
Fred Richterb63b36f2014-03-24 19:56:00 -0300788}
789
790static int lgdt3306a_sleep(struct lgdt3306a_state *state)
791{
792 int ret;
793
Mauro Carvalho Chehab097117ca2014-10-28 11:35:16 -0200794 dbg_info("\n");
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -0300795 state->current_frequency = -1; /* force re-tune, when we wake */
Fred Richterb63b36f2014-03-24 19:56:00 -0300796
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -0300797 ret = lgdt3306a_mpeg_tristate(state, 1); /* disable data bus */
Fred Richterb63b36f2014-03-24 19:56:00 -0300798 if (lg_chkerr(ret))
799 goto fail;
800
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -0300801 ret = lgdt3306a_power(state, 0); /* power down */
Fred Richterb63b36f2014-03-24 19:56:00 -0300802 lg_chkerr(ret);
803
804fail:
805 return 0;
806}
807
808static int lgdt3306a_fe_sleep(struct dvb_frontend *fe)
809{
810 struct lgdt3306a_state *state = fe->demodulator_priv;
811
812 return lgdt3306a_sleep(state);
813}
814
815static int lgdt3306a_init(struct dvb_frontend *fe)
816{
817 struct lgdt3306a_state *state = fe->demodulator_priv;
818 u8 val;
819 int ret;
820
Mauro Carvalho Chehab097117ca2014-10-28 11:35:16 -0200821 dbg_info("\n");
Fred Richterb63b36f2014-03-24 19:56:00 -0300822
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -0300823 /* 1. Normal operation mode */
824 ret = lgdt3306a_set_reg_bit(state, 0x0001, 0, 1); /* SIMFASTENB=0x01 */
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 /* 2. Spectrum inversion auto detection (Not valid for VSB) */
Fred Richterb63b36f2014-03-24 19:56:00 -0300829 ret = lgdt3306a_set_inversion_auto(state, 0);
830 if (lg_chkerr(ret))
831 goto fail;
832
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -0300833 /* 3. Spectrum inversion(According to the tuner configuration) */
Fred Richterb63b36f2014-03-24 19:56:00 -0300834 ret = lgdt3306a_set_inversion(state, 1);
835 if (lg_chkerr(ret))
836 goto fail;
837
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -0300838 /* 4. Peak-to-peak voltage of ADC input signal */
Mauro Carvalho Chehab534f4362014-10-28 12:35:18 -0200839
840 /* ADCSEL1V=0x80=1Vpp; 0x00=2Vpp */
841 ret = lgdt3306a_set_reg_bit(state, 0x0004, 7, 1);
Fred Richterb63b36f2014-03-24 19:56:00 -0300842 if (lg_chkerr(ret))
843 goto fail;
844
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -0300845 /* 5. ADC output data capture clock phase */
Mauro Carvalho Chehab534f4362014-10-28 12:35:18 -0200846
847 /* 0=same phase as ADC clock */
848 ret = lgdt3306a_set_reg_bit(state, 0x0004, 2, 0);
Fred Richterb63b36f2014-03-24 19:56:00 -0300849 if (lg_chkerr(ret))
850 goto fail;
851
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -0300852 /* 5a. ADC sampling clock source */
Mauro Carvalho Chehab534f4362014-10-28 12:35:18 -0200853
854 /* ADCCLKPLLSEL=0x08; 0=use ext clock, not PLL */
855 ret = lgdt3306a_set_reg_bit(state, 0x0004, 3, 0);
Fred Richterb63b36f2014-03-24 19:56:00 -0300856 if (lg_chkerr(ret))
857 goto fail;
858
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -0300859 /* 6. Automatic PLL set */
Mauro Carvalho Chehab534f4362014-10-28 12:35:18 -0200860
861 /* PLLSETAUTO=0x40; 0=off */
862 ret = lgdt3306a_set_reg_bit(state, 0x0005, 6, 0);
Fred Richterb63b36f2014-03-24 19:56:00 -0300863 if (lg_chkerr(ret))
864 goto fail;
865
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -0300866 if (state->cfg->xtalMHz == 24) { /* 24MHz */
867 /* 7. Frequency for PLL output(0x2564 for 192MHz for 24MHz) */
Fred Richterb63b36f2014-03-24 19:56:00 -0300868 ret = lgdt3306a_read_reg(state, 0x0005, &val);
869 if (lg_chkerr(ret))
870 goto fail;
Mauro Carvalho Chehab4937ba92014-10-28 10:50:36 -0200871 val &= 0xc0;
Fred Richterb63b36f2014-03-24 19:56:00 -0300872 val |= 0x25;
873 ret = lgdt3306a_write_reg(state, 0x0005, val);
874 if (lg_chkerr(ret))
875 goto fail;
876 ret = lgdt3306a_write_reg(state, 0x0006, 0x64);
877 if (lg_chkerr(ret))
878 goto fail;
879
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -0300880 /* 8. ADC sampling frequency(0x180000 for 24MHz sampling) */
Mauro Carvalho Chehab4937ba92014-10-28 10:50:36 -0200881 ret = lgdt3306a_read_reg(state, 0x000d, &val);
Fred Richterb63b36f2014-03-24 19:56:00 -0300882 if (lg_chkerr(ret))
883 goto fail;
Mauro Carvalho Chehab4937ba92014-10-28 10:50:36 -0200884 val &= 0xc0;
Fred Richterb63b36f2014-03-24 19:56:00 -0300885 val |= 0x18;
Mauro Carvalho Chehab4937ba92014-10-28 10:50:36 -0200886 ret = lgdt3306a_write_reg(state, 0x000d, val);
Fred Richterb63b36f2014-03-24 19:56:00 -0300887 if (lg_chkerr(ret))
888 goto fail;
889
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -0300890 } else if (state->cfg->xtalMHz == 25) { /* 25MHz */
891 /* 7. Frequency for PLL output */
Fred Richterb63b36f2014-03-24 19:56:00 -0300892 ret = lgdt3306a_read_reg(state, 0x0005, &val);
893 if (lg_chkerr(ret))
894 goto fail;
Mauro Carvalho Chehab4937ba92014-10-28 10:50:36 -0200895 val &= 0xc0;
Fred Richterb63b36f2014-03-24 19:56:00 -0300896 val |= 0x25;
897 ret = lgdt3306a_write_reg(state, 0x0005, val);
898 if (lg_chkerr(ret))
899 goto fail;
900 ret = lgdt3306a_write_reg(state, 0x0006, 0x64);
901 if (lg_chkerr(ret))
902 goto fail;
903
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -0300904 /* 8. ADC sampling frequency(0x190000 for 25MHz sampling) */
Mauro Carvalho Chehab4937ba92014-10-28 10:50:36 -0200905 ret = lgdt3306a_read_reg(state, 0x000d, &val);
Fred Richterb63b36f2014-03-24 19:56:00 -0300906 if (lg_chkerr(ret))
907 goto fail;
Mauro Carvalho Chehab4937ba92014-10-28 10:50:36 -0200908 val &= 0xc0;
Fred Richterb63b36f2014-03-24 19:56:00 -0300909 val |= 0x19;
Mauro Carvalho Chehab4937ba92014-10-28 10:50:36 -0200910 ret = lgdt3306a_write_reg(state, 0x000d, val);
Fred Richterb63b36f2014-03-24 19:56:00 -0300911 if (lg_chkerr(ret))
912 goto fail;
913 } else {
Mauro Carvalho Chehab097117ca2014-10-28 11:35:16 -0200914 pr_err("Bad xtalMHz=%d\n", state->cfg->xtalMHz);
Fred Richterb63b36f2014-03-24 19:56:00 -0300915 }
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -0300916#if 0
Mauro Carvalho Chehab4937ba92014-10-28 10:50:36 -0200917 ret = lgdt3306a_write_reg(state, 0x000e, 0x00);
918 ret = lgdt3306a_write_reg(state, 0x000f, 0x00);
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -0300919#endif
Fred Richterb63b36f2014-03-24 19:56:00 -0300920
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -0300921 /* 9. Center frequency of input signal of ADC */
922 ret = lgdt3306a_write_reg(state, 0x0010, 0x34); /* 3.25MHz */
923 ret = lgdt3306a_write_reg(state, 0x0011, 0x00);
Fred Richterb63b36f2014-03-24 19:56:00 -0300924
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -0300925 /* 10. Fixed gain error value */
926 ret = lgdt3306a_write_reg(state, 0x0014, 0); /* gain error=0 */
Fred Richterb63b36f2014-03-24 19:56:00 -0300927
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -0300928 /* 10a. VSB TR BW gear shift initial step */
Mauro Carvalho Chehab4937ba92014-10-28 10:50:36 -0200929 ret = lgdt3306a_read_reg(state, 0x103c, &val);
930 val &= 0x0f;
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -0300931 val |= 0x20; /* SAMGSAUTOSTL_V[3:0] = 2 */
Mauro Carvalho Chehab4937ba92014-10-28 10:50:36 -0200932 ret = lgdt3306a_write_reg(state, 0x103c, val);
Fred Richterb63b36f2014-03-24 19:56:00 -0300933
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -0300934 /* 10b. Timing offset calibration in low temperature for VSB */
Mauro Carvalho Chehab4937ba92014-10-28 10:50:36 -0200935 ret = lgdt3306a_read_reg(state, 0x103d, &val);
936 val &= 0xfc;
Fred Richterb63b36f2014-03-24 19:56:00 -0300937 val |= 0x03;
Mauro Carvalho Chehab4937ba92014-10-28 10:50:36 -0200938 ret = lgdt3306a_write_reg(state, 0x103d, val);
Fred Richterb63b36f2014-03-24 19:56:00 -0300939
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -0300940 /* 10c. Timing offset calibration in low temperature for QAM */
Fred Richterb63b36f2014-03-24 19:56:00 -0300941 ret = lgdt3306a_read_reg(state, 0x1036, &val);
Mauro Carvalho Chehab4937ba92014-10-28 10:50:36 -0200942 val &= 0xf0;
943 val |= 0x0c;
Fred Richterb63b36f2014-03-24 19:56:00 -0300944 ret = lgdt3306a_write_reg(state, 0x1036, val);
945
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -0300946 /* 11. Using the imaginary part of CIR in CIR loading */
Mauro Carvalho Chehab4937ba92014-10-28 10:50:36 -0200947 ret = lgdt3306a_read_reg(state, 0x211f, &val);
948 val &= 0xef; /* do not use imaginary of CIR */
949 ret = lgdt3306a_write_reg(state, 0x211f, val);
Fred Richterb63b36f2014-03-24 19:56:00 -0300950
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -0300951 /* 12. Control of no signal detector function */
Fred Richterb63b36f2014-03-24 19:56:00 -0300952 ret = lgdt3306a_read_reg(state, 0x2849, &val);
Mauro Carvalho Chehab4937ba92014-10-28 10:50:36 -0200953 val &= 0xef; /* NOUSENOSIGDET=0, enable no signal detector */
Fred Richterb63b36f2014-03-24 19:56:00 -0300954 ret = lgdt3306a_write_reg(state, 0x2849, val);
955
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -0300956 /* FGR - put demod in some known mode */
Fred Richterb63b36f2014-03-24 19:56:00 -0300957 ret = lgdt3306a_set_vsb(state);
958
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -0300959 /* 13. TP stream format */
Fred Richterb63b36f2014-03-24 19:56:00 -0300960 ret = lgdt3306a_mpeg_mode(state, state->cfg->mpeg_mode);
961
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -0300962 /* 14. disable output buses */
Fred Richterb63b36f2014-03-24 19:56:00 -0300963 ret = lgdt3306a_mpeg_tristate(state, 1);
964
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -0300965 /* 15. Sleep (in reset) */
Fred Richterb63b36f2014-03-24 19:56:00 -0300966 ret = lgdt3306a_sleep(state);
967 lg_chkerr(ret);
968
969fail:
970 return ret;
971}
972
973static int lgdt3306a_set_parameters(struct dvb_frontend *fe)
974{
975 struct dtv_frontend_properties *p = &fe->dtv_property_cache;
976 struct lgdt3306a_state *state = fe->demodulator_priv;
977 int ret;
978
Mauro Carvalho Chehab097117ca2014-10-28 11:35:16 -0200979 dbg_info("(%d, %d)\n", p->frequency, p->modulation);
Fred Richterb63b36f2014-03-24 19:56:00 -0300980
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -0300981 if (state->current_frequency == p->frequency &&
982 state->current_modulation == p->modulation) {
Mauro Carvalho Chehab097117ca2014-10-28 11:35:16 -0200983 dbg_info(" (already set, skipping ...)\n");
Fred Richterb63b36f2014-03-24 19:56:00 -0300984 return 0;
985 }
986 state->current_frequency = -1;
987 state->current_modulation = -1;
988
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -0300989 ret = lgdt3306a_power(state, 1); /* power up */
Fred Richterb63b36f2014-03-24 19:56:00 -0300990 if (lg_chkerr(ret))
991 goto fail;
992
993 if (fe->ops.tuner_ops.set_params) {
994 ret = fe->ops.tuner_ops.set_params(fe);
995 if (fe->ops.i2c_gate_ctrl)
996 fe->ops.i2c_gate_ctrl(fe, 0);
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -0300997#if 0
998 if (lg_chkerr(ret))
999 goto fail;
1000 state->current_frequency = p->frequency;
1001#endif
Fred Richterb63b36f2014-03-24 19:56:00 -03001002 }
1003
1004 ret = lgdt3306a_set_modulation(state, p);
1005 if (lg_chkerr(ret))
1006 goto fail;
1007
1008 ret = lgdt3306a_agc_setup(state, p);
1009 if (lg_chkerr(ret))
1010 goto fail;
1011
1012 ret = lgdt3306a_set_if(state, p);
1013 if (lg_chkerr(ret))
1014 goto fail;
1015
1016 ret = lgdt3306a_spectral_inversion(state, p,
Mauro Carvalho Chehab534f4362014-10-28 12:35:18 -02001017 state->cfg->spectral_inversion ? 1 : 0);
Fred Richterb63b36f2014-03-24 19:56:00 -03001018 if (lg_chkerr(ret))
1019 goto fail;
1020
1021 ret = lgdt3306a_mpeg_mode(state, state->cfg->mpeg_mode);
1022 if (lg_chkerr(ret))
1023 goto fail;
1024
1025 ret = lgdt3306a_mpeg_mode_polarity(state,
1026 state->cfg->tpclk_edge,
1027 state->cfg->tpvalid_polarity);
1028 if (lg_chkerr(ret))
1029 goto fail;
1030
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -03001031 ret = lgdt3306a_mpeg_tristate(state, 0); /* enable data bus */
Fred Richterb63b36f2014-03-24 19:56:00 -03001032 if (lg_chkerr(ret))
1033 goto fail;
1034
1035 ret = lgdt3306a_soft_reset(state);
1036 if (lg_chkerr(ret))
1037 goto fail;
1038
1039#ifdef DBG_DUMP
1040 lgdt3306a_DumpAllRegs(state);
1041#endif
1042 state->current_frequency = p->frequency;
1043fail:
1044 return ret;
1045}
1046
Mauro Carvalho Chehab7e3e68b2016-02-04 12:58:30 -02001047static int lgdt3306a_get_frontend(struct dvb_frontend *fe,
1048 struct dtv_frontend_properties *p)
Fred Richterb63b36f2014-03-24 19:56:00 -03001049{
1050 struct lgdt3306a_state *state = fe->demodulator_priv;
Fred Richterb63b36f2014-03-24 19:56:00 -03001051
Mauro Carvalho Chehab534f4362014-10-28 12:35:18 -02001052 dbg_info("(%u, %d)\n",
1053 state->current_frequency, state->current_modulation);
Fred Richterb63b36f2014-03-24 19:56:00 -03001054
1055 p->modulation = state->current_modulation;
1056 p->frequency = state->current_frequency;
1057 return 0;
1058}
1059
1060static enum dvbfe_algo lgdt3306a_get_frontend_algo(struct dvb_frontend *fe)
1061{
1062#if 1
1063 return DVBFE_ALGO_CUSTOM;
1064#else
1065 return DVBFE_ALGO_HW;
1066#endif
1067}
1068
1069/* ------------------------------------------------------------------------ */
Mauro Carvalho Chehabee0133e2014-10-28 11:21:48 -02001070static int lgdt3306a_monitor_vsb(struct lgdt3306a_state *state)
Fred Richterb63b36f2014-03-24 19:56:00 -03001071{
1072 u8 val;
1073 int ret;
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -03001074 u8 snrRef, maxPowerMan, nCombDet;
1075 u16 fbDlyCir;
Fred Richterb63b36f2014-03-24 19:56:00 -03001076
Mauro Carvalho Chehab4937ba92014-10-28 10:50:36 -02001077 ret = lgdt3306a_read_reg(state, 0x21a1, &val);
Mauro Carvalho Chehabee0133e2014-10-28 11:21:48 -02001078 if (ret)
1079 return ret;
Mauro Carvalho Chehab4937ba92014-10-28 10:50:36 -02001080 snrRef = val & 0x3f;
Fred Richterb63b36f2014-03-24 19:56:00 -03001081
1082 ret = lgdt3306a_read_reg(state, 0x2185, &maxPowerMan);
Mauro Carvalho Chehabee0133e2014-10-28 11:21:48 -02001083 if (ret)
1084 return ret;
Fred Richterb63b36f2014-03-24 19:56:00 -03001085
1086 ret = lgdt3306a_read_reg(state, 0x2191, &val);
Mauro Carvalho Chehabee0133e2014-10-28 11:21:48 -02001087 if (ret)
1088 return ret;
Fred Richterb63b36f2014-03-24 19:56:00 -03001089 nCombDet = (val & 0x80) >> 7;
1090
1091 ret = lgdt3306a_read_reg(state, 0x2180, &val);
Mauro Carvalho Chehabee0133e2014-10-28 11:21:48 -02001092 if (ret)
1093 return ret;
Fred Richterb63b36f2014-03-24 19:56:00 -03001094 fbDlyCir = (val & 0x03) << 8;
Mauro Carvalho Chehabee0133e2014-10-28 11:21:48 -02001095
Fred Richterb63b36f2014-03-24 19:56:00 -03001096 ret = lgdt3306a_read_reg(state, 0x2181, &val);
Mauro Carvalho Chehabee0133e2014-10-28 11:21:48 -02001097 if (ret)
1098 return ret;
Fred Richterb63b36f2014-03-24 19:56:00 -03001099 fbDlyCir |= val;
1100
Mauro Carvalho Chehab097117ca2014-10-28 11:35:16 -02001101 dbg_info("snrRef=%d maxPowerMan=0x%x nCombDet=%d fbDlyCir=0x%x\n",
Fred Richterb63b36f2014-03-24 19:56:00 -03001102 snrRef, maxPowerMan, nCombDet, fbDlyCir);
1103
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -03001104 /* Carrier offset sub loop bandwidth */
Fred Richterb63b36f2014-03-24 19:56:00 -03001105 ret = lgdt3306a_read_reg(state, 0x1061, &val);
Mauro Carvalho Chehabee0133e2014-10-28 11:21:48 -02001106 if (ret)
1107 return ret;
Mauro Carvalho Chehab4937ba92014-10-28 10:50:36 -02001108 val &= 0xf8;
Mauro Carvalho Chehab534f4362014-10-28 12:35:18 -02001109 if ((snrRef > 18) && (maxPowerMan > 0x68)
1110 && (nCombDet == 0x01)
1111 && ((fbDlyCir == 0x03FF) || (fbDlyCir < 0x6C))) {
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -03001112 /* SNR is over 18dB and no ghosting */
1113 val |= 0x00; /* final bandwidth = 0 */
Fred Richterb63b36f2014-03-24 19:56:00 -03001114 } else {
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -03001115 val |= 0x04; /* final bandwidth = 4 */
Fred Richterb63b36f2014-03-24 19:56:00 -03001116 }
1117 ret = lgdt3306a_write_reg(state, 0x1061, val);
Mauro Carvalho Chehabee0133e2014-10-28 11:21:48 -02001118 if (ret)
1119 return ret;
Fred Richterb63b36f2014-03-24 19:56:00 -03001120
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -03001121 /* Adjust Notch Filter */
Fred Richterb63b36f2014-03-24 19:56:00 -03001122 ret = lgdt3306a_read_reg(state, 0x0024, &val);
Mauro Carvalho Chehabee0133e2014-10-28 11:21:48 -02001123 if (ret)
1124 return ret;
Mauro Carvalho Chehab4937ba92014-10-28 10:50:36 -02001125 val &= 0x0f;
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -03001126 if (nCombDet == 0) { /* Turn on the Notch Filter */
Fred Richterb63b36f2014-03-24 19:56:00 -03001127 val |= 0x50;
1128 }
1129 ret = lgdt3306a_write_reg(state, 0x0024, val);
Mauro Carvalho Chehabee0133e2014-10-28 11:21:48 -02001130 if (ret)
1131 return ret;
Fred Richterb63b36f2014-03-24 19:56:00 -03001132
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -03001133 /* VSB Timing Recovery output normalization */
Mauro Carvalho Chehab4937ba92014-10-28 10:50:36 -02001134 ret = lgdt3306a_read_reg(state, 0x103d, &val);
Mauro Carvalho Chehabee0133e2014-10-28 11:21:48 -02001135 if (ret)
1136 return ret;
Mauro Carvalho Chehab4937ba92014-10-28 10:50:36 -02001137 val &= 0xcf;
Fred Richterb63b36f2014-03-24 19:56:00 -03001138 val |= 0x20;
Mauro Carvalho Chehab4937ba92014-10-28 10:50:36 -02001139 ret = lgdt3306a_write_reg(state, 0x103d, val);
Mauro Carvalho Chehabee0133e2014-10-28 11:21:48 -02001140
1141 return ret;
Fred Richterb63b36f2014-03-24 19:56:00 -03001142}
1143
Mauro Carvalho Chehab534f4362014-10-28 12:35:18 -02001144static enum lgdt3306a_modulation
1145lgdt3306a_check_oper_mode(struct lgdt3306a_state *state)
Fred Richterb63b36f2014-03-24 19:56:00 -03001146{
1147 u8 val = 0;
1148 int ret;
1149
1150 ret = lgdt3306a_read_reg(state, 0x0081, &val);
Mauro Carvalho Chehabee0133e2014-10-28 11:21:48 -02001151 if (ret)
1152 goto err;
Fred Richterb63b36f2014-03-24 19:56:00 -03001153
1154 if (val & 0x80) {
Mauro Carvalho Chehab097117ca2014-10-28 11:35:16 -02001155 dbg_info("VSB\n");
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -03001156 return LG3306_VSB;
Fred Richterb63b36f2014-03-24 19:56:00 -03001157 }
Michael Ira Krufkyc714efe2014-08-03 14:51:49 -03001158 if (val & 0x08) {
Mauro Carvalho Chehab4937ba92014-10-28 10:50:36 -02001159 ret = lgdt3306a_read_reg(state, 0x00a6, &val);
Mauro Carvalho Chehabee0133e2014-10-28 11:21:48 -02001160 if (ret)
1161 goto err;
Fred Richterb63b36f2014-03-24 19:56:00 -03001162 val = val >> 2;
1163 if (val & 0x01) {
Mauro Carvalho Chehab097117ca2014-10-28 11:35:16 -02001164 dbg_info("QAM256\n");
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -03001165 return LG3306_QAM256;
Fred Richterb63b36f2014-03-24 19:56:00 -03001166 }
Mauro Carvalho Chehabb4e43e92014-10-28 12:05:35 -02001167 dbg_info("QAM64\n");
1168 return LG3306_QAM64;
Fred Richterb63b36f2014-03-24 19:56:00 -03001169 }
Mauro Carvalho Chehabee0133e2014-10-28 11:21:48 -02001170err:
Mauro Carvalho Chehab097117ca2014-10-28 11:35:16 -02001171 pr_warn("UNKNOWN\n");
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -03001172 return LG3306_UNKNOWN_MODE;
Fred Richterb63b36f2014-03-24 19:56:00 -03001173}
1174
Mauro Carvalho Chehab534f4362014-10-28 12:35:18 -02001175static enum lgdt3306a_lock_status
1176lgdt3306a_check_lock_status(struct lgdt3306a_state *state,
1177 enum lgdt3306a_lock_check whatLock)
Fred Richterb63b36f2014-03-24 19:56:00 -03001178{
1179 u8 val = 0;
1180 int ret;
Michael Ira Krufkyf883d602014-08-03 15:29:04 -03001181 enum lgdt3306a_modulation modeOper;
1182 enum lgdt3306a_lock_status lockStatus;
Fred Richterb63b36f2014-03-24 19:56:00 -03001183
1184 modeOper = LG3306_UNKNOWN_MODE;
1185
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -03001186 switch (whatLock) {
1187 case LG3306_SYNC_LOCK:
Fred Richterb63b36f2014-03-24 19:56:00 -03001188 {
Mauro Carvalho Chehab4937ba92014-10-28 10:50:36 -02001189 ret = lgdt3306a_read_reg(state, 0x00a6, &val);
Mauro Carvalho Chehabee0133e2014-10-28 11:21:48 -02001190 if (ret)
1191 return ret;
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -03001192
1193 if ((val & 0x80) == 0x80)
1194 lockStatus = LG3306_LOCK;
1195 else
1196 lockStatus = LG3306_UNLOCK;
1197
Mauro Carvalho Chehab097117ca2014-10-28 11:35:16 -02001198 dbg_info("SYNC_LOCK=%x\n", lockStatus);
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -03001199 break;
1200 }
1201 case LG3306_AGC_LOCK:
1202 {
1203 ret = lgdt3306a_read_reg(state, 0x0080, &val);
Mauro Carvalho Chehabee0133e2014-10-28 11:21:48 -02001204 if (ret)
1205 return ret;
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -03001206
1207 if ((val & 0x40) == 0x40)
1208 lockStatus = LG3306_LOCK;
1209 else
1210 lockStatus = LG3306_UNLOCK;
1211
Mauro Carvalho Chehab097117ca2014-10-28 11:35:16 -02001212 dbg_info("AGC_LOCK=%x\n", lockStatus);
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -03001213 break;
1214 }
1215 case LG3306_TR_LOCK:
1216 {
1217 modeOper = lgdt3306a_check_oper_mode(state);
1218 if ((modeOper == LG3306_QAM64) || (modeOper == LG3306_QAM256)) {
1219 ret = lgdt3306a_read_reg(state, 0x1094, &val);
Mauro Carvalho Chehabee0133e2014-10-28 11:21:48 -02001220 if (ret)
1221 return ret;
Fred Richterb63b36f2014-03-24 19:56:00 -03001222
1223 if ((val & 0x80) == 0x80)
1224 lockStatus = LG3306_LOCK;
1225 else
1226 lockStatus = LG3306_UNLOCK;
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -03001227 } else
1228 lockStatus = LG3306_UNKNOWN_LOCK;
Fred Richterb63b36f2014-03-24 19:56:00 -03001229
Mauro Carvalho Chehab097117ca2014-10-28 11:35:16 -02001230 dbg_info("TR_LOCK=%x\n", lockStatus);
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -03001231 break;
1232 }
1233 case LG3306_FEC_LOCK:
1234 {
1235 modeOper = lgdt3306a_check_oper_mode(state);
1236 if ((modeOper == LG3306_QAM64) || (modeOper == LG3306_QAM256)) {
Fred Richterb63b36f2014-03-24 19:56:00 -03001237 ret = lgdt3306a_read_reg(state, 0x0080, &val);
Mauro Carvalho Chehabee0133e2014-10-28 11:21:48 -02001238 if (ret)
1239 return ret;
Fred Richterb63b36f2014-03-24 19:56:00 -03001240
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -03001241 if ((val & 0x10) == 0x10)
Fred Richterb63b36f2014-03-24 19:56:00 -03001242 lockStatus = LG3306_LOCK;
1243 else
1244 lockStatus = LG3306_UNLOCK;
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -03001245 } else
Fred Richterb63b36f2014-03-24 19:56:00 -03001246 lockStatus = LG3306_UNKNOWN_LOCK;
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -03001247
Mauro Carvalho Chehab097117ca2014-10-28 11:35:16 -02001248 dbg_info("FEC_LOCK=%x\n", lockStatus);
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -03001249 break;
Fred Richterb63b36f2014-03-24 19:56:00 -03001250 }
1251
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -03001252 default:
1253 lockStatus = LG3306_UNKNOWN_LOCK;
Mauro Carvalho Chehab097117ca2014-10-28 11:35:16 -02001254 pr_warn("UNKNOWN whatLock=%d\n", whatLock);
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -03001255 break;
1256 }
1257
1258 return lockStatus;
Fred Richterb63b36f2014-03-24 19:56:00 -03001259}
1260
Mauro Carvalho Chehab534f4362014-10-28 12:35:18 -02001261static enum lgdt3306a_neverlock_status
1262lgdt3306a_check_neverlock_status(struct lgdt3306a_state *state)
Fred Richterb63b36f2014-03-24 19:56:00 -03001263{
1264 u8 val = 0;
1265 int ret;
Michael Ira Krufkyf883d602014-08-03 15:29:04 -03001266 enum lgdt3306a_neverlock_status lockStatus;
Fred Richterb63b36f2014-03-24 19:56:00 -03001267
1268 ret = lgdt3306a_read_reg(state, 0x0080, &val);
Mauro Carvalho Chehabee0133e2014-10-28 11:21:48 -02001269 if (ret)
1270 return ret;
Michael Ira Krufkyf883d602014-08-03 15:29:04 -03001271 lockStatus = (enum lgdt3306a_neverlock_status)(val & 0x03);
Fred Richterb63b36f2014-03-24 19:56:00 -03001272
Mauro Carvalho Chehab097117ca2014-10-28 11:35:16 -02001273 dbg_info("NeverLock=%d", lockStatus);
Fred Richterb63b36f2014-03-24 19:56:00 -03001274
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -03001275 return lockStatus;
Fred Richterb63b36f2014-03-24 19:56:00 -03001276}
1277
Mauro Carvalho Chehabee0133e2014-10-28 11:21:48 -02001278static int lgdt3306a_pre_monitoring(struct lgdt3306a_state *state)
Fred Richterb63b36f2014-03-24 19:56:00 -03001279{
1280 u8 val = 0;
1281 int ret;
1282 u8 currChDiffACQ, snrRef, mainStrong, aiccrejStatus;
1283
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -03001284 /* Channel variation */
Mauro Carvalho Chehab4937ba92014-10-28 10:50:36 -02001285 ret = lgdt3306a_read_reg(state, 0x21bc, &currChDiffACQ);
Mauro Carvalho Chehabee0133e2014-10-28 11:21:48 -02001286 if (ret)
1287 return ret;
Fred Richterb63b36f2014-03-24 19:56:00 -03001288
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -03001289 /* SNR of Frame sync */
Mauro Carvalho Chehab4937ba92014-10-28 10:50:36 -02001290 ret = lgdt3306a_read_reg(state, 0x21a1, &val);
Mauro Carvalho Chehabee0133e2014-10-28 11:21:48 -02001291 if (ret)
1292 return ret;
Mauro Carvalho Chehab4937ba92014-10-28 10:50:36 -02001293 snrRef = val & 0x3f;
Fred Richterb63b36f2014-03-24 19:56:00 -03001294
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -03001295 /* Strong Main CIR */
Fred Richterb63b36f2014-03-24 19:56:00 -03001296 ret = lgdt3306a_read_reg(state, 0x2199, &val);
Mauro Carvalho Chehabee0133e2014-10-28 11:21:48 -02001297 if (ret)
1298 return ret;
Fred Richterb63b36f2014-03-24 19:56:00 -03001299 mainStrong = (val & 0x40) >> 6;
1300
1301 ret = lgdt3306a_read_reg(state, 0x0090, &val);
Mauro Carvalho Chehabee0133e2014-10-28 11:21:48 -02001302 if (ret)
1303 return ret;
Mauro Carvalho Chehab4937ba92014-10-28 10:50:36 -02001304 aiccrejStatus = (val & 0xf0) >> 4;
Fred Richterb63b36f2014-03-24 19:56:00 -03001305
Mauro Carvalho Chehab097117ca2014-10-28 11:35:16 -02001306 dbg_info("snrRef=%d mainStrong=%d aiccrejStatus=%d currChDiffACQ=0x%x\n",
Fred Richterb63b36f2014-03-24 19:56:00 -03001307 snrRef, mainStrong, aiccrejStatus, currChDiffACQ);
1308
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -03001309#if 0
Mauro Carvalho Chehab534f4362014-10-28 12:35:18 -02001310 /* Dynamic ghost exists */
1311 if ((mainStrong == 0) && (currChDiffACQ > 0x70))
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -03001312#endif
1313 if (mainStrong == 0) {
Fred Richterb63b36f2014-03-24 19:56:00 -03001314 ret = lgdt3306a_read_reg(state, 0x2135, &val);
Mauro Carvalho Chehabee0133e2014-10-28 11:21:48 -02001315 if (ret)
1316 return ret;
Mauro Carvalho Chehab4937ba92014-10-28 10:50:36 -02001317 val &= 0x0f;
1318 val |= 0xa0;
Fred Richterb63b36f2014-03-24 19:56:00 -03001319 ret = lgdt3306a_write_reg(state, 0x2135, val);
Mauro Carvalho Chehabee0133e2014-10-28 11:21:48 -02001320 if (ret)
1321 return ret;
Fred Richterb63b36f2014-03-24 19:56:00 -03001322
1323 ret = lgdt3306a_read_reg(state, 0x2141, &val);
Mauro Carvalho Chehabee0133e2014-10-28 11:21:48 -02001324 if (ret)
1325 return ret;
Mauro Carvalho Chehab4937ba92014-10-28 10:50:36 -02001326 val &= 0x3f;
Fred Richterb63b36f2014-03-24 19:56:00 -03001327 val |= 0x80;
1328 ret = lgdt3306a_write_reg(state, 0x2141, val);
Mauro Carvalho Chehabee0133e2014-10-28 11:21:48 -02001329 if (ret)
1330 return ret;
Fred Richterb63b36f2014-03-24 19:56:00 -03001331
1332 ret = lgdt3306a_write_reg(state, 0x2122, 0x70);
Mauro Carvalho Chehabee0133e2014-10-28 11:21:48 -02001333 if (ret)
1334 return ret;
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -03001335 } else { /* Weak ghost or static channel */
Fred Richterb63b36f2014-03-24 19:56:00 -03001336 ret = lgdt3306a_read_reg(state, 0x2135, &val);
Mauro Carvalho Chehabee0133e2014-10-28 11:21:48 -02001337 if (ret)
1338 return ret;
Mauro Carvalho Chehab4937ba92014-10-28 10:50:36 -02001339 val &= 0x0f;
Fred Richterb63b36f2014-03-24 19:56:00 -03001340 val |= 0x70;
1341 ret = lgdt3306a_write_reg(state, 0x2135, val);
Mauro Carvalho Chehabee0133e2014-10-28 11:21:48 -02001342 if (ret)
1343 return ret;
Fred Richterb63b36f2014-03-24 19:56:00 -03001344
1345 ret = lgdt3306a_read_reg(state, 0x2141, &val);
Mauro Carvalho Chehabee0133e2014-10-28 11:21:48 -02001346 if (ret)
1347 return ret;
Mauro Carvalho Chehab4937ba92014-10-28 10:50:36 -02001348 val &= 0x3f;
Fred Richterb63b36f2014-03-24 19:56:00 -03001349 val |= 0x40;
1350 ret = lgdt3306a_write_reg(state, 0x2141, val);
Mauro Carvalho Chehabee0133e2014-10-28 11:21:48 -02001351 if (ret)
1352 return ret;
Fred Richterb63b36f2014-03-24 19:56:00 -03001353
1354 ret = lgdt3306a_write_reg(state, 0x2122, 0x40);
Mauro Carvalho Chehabee0133e2014-10-28 11:21:48 -02001355 if (ret)
1356 return ret;
Fred Richterb63b36f2014-03-24 19:56:00 -03001357 }
Mauro Carvalho Chehabee0133e2014-10-28 11:21:48 -02001358 return 0;
Fred Richterb63b36f2014-03-24 19:56:00 -03001359}
1360
Mauro Carvalho Chehab534f4362014-10-28 12:35:18 -02001361static enum lgdt3306a_lock_status
1362lgdt3306a_sync_lock_poll(struct lgdt3306a_state *state)
Fred Richterb63b36f2014-03-24 19:56:00 -03001363{
Michael Ira Krufkyf883d602014-08-03 15:29:04 -03001364 enum lgdt3306a_lock_status syncLockStatus = LG3306_UNLOCK;
Fred Richterb63b36f2014-03-24 19:56:00 -03001365 int i;
1366
1367 for (i = 0; i < 2; i++) {
1368 msleep(30);
1369
Mauro Carvalho Chehab534f4362014-10-28 12:35:18 -02001370 syncLockStatus = lgdt3306a_check_lock_status(state,
1371 LG3306_SYNC_LOCK);
Fred Richterb63b36f2014-03-24 19:56:00 -03001372
1373 if (syncLockStatus == LG3306_LOCK) {
Mauro Carvalho Chehab097117ca2014-10-28 11:35:16 -02001374 dbg_info("locked(%d)\n", i);
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -03001375 return LG3306_LOCK;
Fred Richterb63b36f2014-03-24 19:56:00 -03001376 }
1377 }
Mauro Carvalho Chehab097117ca2014-10-28 11:35:16 -02001378 dbg_info("not locked\n");
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -03001379 return LG3306_UNLOCK;
Fred Richterb63b36f2014-03-24 19:56:00 -03001380}
1381
Mauro Carvalho Chehab534f4362014-10-28 12:35:18 -02001382static enum lgdt3306a_lock_status
1383lgdt3306a_fec_lock_poll(struct lgdt3306a_state *state)
Fred Richterb63b36f2014-03-24 19:56:00 -03001384{
Michael Ira Krufkyf883d602014-08-03 15:29:04 -03001385 enum lgdt3306a_lock_status FECLockStatus = LG3306_UNLOCK;
Fred Richterb63b36f2014-03-24 19:56:00 -03001386 int i;
1387
1388 for (i = 0; i < 2; i++) {
1389 msleep(30);
1390
Mauro Carvalho Chehab534f4362014-10-28 12:35:18 -02001391 FECLockStatus = lgdt3306a_check_lock_status(state,
1392 LG3306_FEC_LOCK);
Fred Richterb63b36f2014-03-24 19:56:00 -03001393
1394 if (FECLockStatus == LG3306_LOCK) {
Mauro Carvalho Chehab097117ca2014-10-28 11:35:16 -02001395 dbg_info("locked(%d)\n", i);
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -03001396 return FECLockStatus;
Fred Richterb63b36f2014-03-24 19:56:00 -03001397 }
1398 }
Mauro Carvalho Chehab097117ca2014-10-28 11:35:16 -02001399 dbg_info("not locked\n");
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -03001400 return FECLockStatus;
Fred Richterb63b36f2014-03-24 19:56:00 -03001401}
1402
Mauro Carvalho Chehab534f4362014-10-28 12:35:18 -02001403static enum lgdt3306a_neverlock_status
1404lgdt3306a_neverlock_poll(struct lgdt3306a_state *state)
Fred Richterb63b36f2014-03-24 19:56:00 -03001405{
Michael Ira Krufkyf883d602014-08-03 15:29:04 -03001406 enum lgdt3306a_neverlock_status NLLockStatus = LG3306_NL_FAIL;
Fred Richterb63b36f2014-03-24 19:56:00 -03001407 int i;
1408
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -03001409 for (i = 0; i < 5; i++) {
Fred Richterb63b36f2014-03-24 19:56:00 -03001410 msleep(30);
1411
1412 NLLockStatus = lgdt3306a_check_neverlock_status(state);
1413
1414 if (NLLockStatus == LG3306_NL_LOCK) {
Mauro Carvalho Chehab097117ca2014-10-28 11:35:16 -02001415 dbg_info("NL_LOCK(%d)\n", i);
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -03001416 return NLLockStatus;
Fred Richterb63b36f2014-03-24 19:56:00 -03001417 }
1418 }
Mauro Carvalho Chehab097117ca2014-10-28 11:35:16 -02001419 dbg_info("NLLockStatus=%d\n", NLLockStatus);
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -03001420 return NLLockStatus;
Fred Richterb63b36f2014-03-24 19:56:00 -03001421}
1422
1423static u8 lgdt3306a_get_packet_error(struct lgdt3306a_state *state)
1424{
1425 u8 val;
1426 int ret;
1427
Mauro Carvalho Chehab4937ba92014-10-28 10:50:36 -02001428 ret = lgdt3306a_read_reg(state, 0x00fa, &val);
Mauro Carvalho Chehabee0133e2014-10-28 11:21:48 -02001429 if (ret)
1430 return ret;
Fred Richterb63b36f2014-03-24 19:56:00 -03001431
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -03001432 return val;
Fred Richterb63b36f2014-03-24 19:56:00 -03001433}
1434
Mauro Carvalho Chehab9369fe02014-10-28 12:30:44 -02001435static const u32 valx_x10[] = {
1436 10, 11, 13, 15, 17, 20, 25, 33, 41, 50, 59, 73, 87, 100
1437};
1438static const u32 log10x_x1000[] = {
Mauro Carvalho Chehab95f22c52014-10-28 12:40:20 -02001439 0, 41, 114, 176, 230, 301, 398, 518, 613, 699, 771, 863, 939, 1000
Mauro Carvalho Chehab9369fe02014-10-28 12:30:44 -02001440};
1441
Fred Richterb63b36f2014-03-24 19:56:00 -03001442static u32 log10_x1000(u32 x)
1443{
Mauro Carvalho Chehaba132fef2014-10-28 11:07:03 -02001444 u32 diff_val, step_val, step_log10;
Fred Richterb63b36f2014-03-24 19:56:00 -03001445 u32 log_val = 0;
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -03001446 u32 i;
Fred Richterb63b36f2014-03-24 19:56:00 -03001447
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -03001448 if (x <= 0)
1449 return -1000000; /* signal error */
Fred Richterb63b36f2014-03-24 19:56:00 -03001450
Mauro Carvalho Chehabb4e43e92014-10-28 12:05:35 -02001451 if (x == 10)
1452 return 0; /* log(1)=0 */
1453
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -03001454 if (x < 10) {
1455 while (x < 10) {
1456 x = x * 10;
Fred Richterb63b36f2014-03-24 19:56:00 -03001457 log_val--;
1458 }
Mauro Carvalho Chehabb4e43e92014-10-28 12:05:35 -02001459 } else { /* x > 10 */
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -03001460 while (x >= 100) {
1461 x = x / 10;
Fred Richterb63b36f2014-03-24 19:56:00 -03001462 log_val++;
1463 }
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -03001464 }
Fred Richterb63b36f2014-03-24 19:56:00 -03001465 log_val *= 1000;
1466
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -03001467 if (x == 10) /* was our input an exact multiple of 10 */
1468 return log_val; /* don't need to interpolate */
Fred Richterb63b36f2014-03-24 19:56:00 -03001469
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -03001470 /* find our place on the log curve */
Mauro Carvalho Chehab9369fe02014-10-28 12:30:44 -02001471 for (i = 1; i < ARRAY_SIZE(valx_x10); i++) {
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -03001472 if (valx_x10[i] >= x)
1473 break;
Fred Richterb63b36f2014-03-24 19:56:00 -03001474 }
Mauro Carvalho Chehab9369fe02014-10-28 12:30:44 -02001475 if (i == ARRAY_SIZE(valx_x10))
Mauro Carvalho Chehaba132fef2014-10-28 11:07:03 -02001476 return log_val + log10x_x1000[i - 1];
Fred Richterb63b36f2014-03-24 19:56:00 -03001477
Mauro Carvalho Chehaba132fef2014-10-28 11:07:03 -02001478 diff_val = x - valx_x10[i-1];
1479 step_val = valx_x10[i] - valx_x10[i - 1];
1480 step_log10 = log10x_x1000[i] - log10x_x1000[i - 1];
1481
1482 /* do a linear interpolation to get in-between values */
1483 return log_val + log10x_x1000[i - 1] +
1484 ((diff_val*step_log10) / step_val);
Fred Richterb63b36f2014-03-24 19:56:00 -03001485}
1486
1487static u32 lgdt3306a_calculate_snr_x100(struct lgdt3306a_state *state)
1488{
Michael Ira Krufky34a5a2f2014-10-25 11:26:15 -03001489 u32 mse; /* Mean-Square Error */
1490 u32 pwr; /* Constelation power */
Fred Richterb63b36f2014-03-24 19:56:00 -03001491 u32 snr_x100;
1492
Mauro Carvalho Chehab4937ba92014-10-28 10:50:36 -02001493 mse = (read_reg(state, 0x00ec) << 8) |
1494 (read_reg(state, 0x00ed));
1495 pwr = (read_reg(state, 0x00e8) << 8) |
1496 (read_reg(state, 0x00e9));
Fred Richterb63b36f2014-03-24 19:56:00 -03001497
1498 if (mse == 0) /* no signal */
1499 return 0;
1500
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -03001501 snr_x100 = log10_x1000((pwr * 10000) / mse) - 3000;
Mauro Carvalho Chehab097117ca2014-10-28 11:35:16 -02001502 dbg_info("mse=%u, pwr=%u, snr_x100=%d\n", mse, pwr, snr_x100);
Fred Richterb63b36f2014-03-24 19:56:00 -03001503
1504 return snr_x100;
1505}
1506
Mauro Carvalho Chehab534f4362014-10-28 12:35:18 -02001507static enum lgdt3306a_lock_status
1508lgdt3306a_vsb_lock_poll(struct lgdt3306a_state *state)
Fred Richterb63b36f2014-03-24 19:56:00 -03001509{
Mauro Carvalho Chehabe2c47fa2014-10-28 11:27:34 -02001510 int ret;
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -03001511 u8 cnt = 0;
1512 u8 packet_error;
1513 u32 snr;
Fred Richterb63b36f2014-03-24 19:56:00 -03001514
Mauro Carvalho Chehabb1a88c72014-10-28 12:00:48 -02001515 for (cnt = 0; cnt < 10; cnt++) {
Fred Richterb63b36f2014-03-24 19:56:00 -03001516 if (lgdt3306a_sync_lock_poll(state) == LG3306_UNLOCK) {
Mauro Carvalho Chehab097117ca2014-10-28 11:35:16 -02001517 dbg_info("no sync lock!\n");
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -03001518 return LG3306_UNLOCK;
Fred Richterb63b36f2014-03-24 19:56:00 -03001519 }
Mauro Carvalho Chehabb1a88c72014-10-28 12:00:48 -02001520
1521 msleep(20);
1522 ret = lgdt3306a_pre_monitoring(state);
1523 if (ret)
1524 break;
1525
1526 packet_error = lgdt3306a_get_packet_error(state);
1527 snr = lgdt3306a_calculate_snr_x100(state);
1528 dbg_info("cnt=%d errors=%d snr=%d\n", cnt, packet_error, snr);
1529
1530 if ((snr >= 1500) && (packet_error < 0xff))
1531 return LG3306_LOCK;
Fred Richterb63b36f2014-03-24 19:56:00 -03001532 }
Mauro Carvalho Chehabb1a88c72014-10-28 12:00:48 -02001533
1534 dbg_info("not locked!\n");
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -03001535 return LG3306_UNLOCK;
Fred Richterb63b36f2014-03-24 19:56:00 -03001536}
1537
Mauro Carvalho Chehab534f4362014-10-28 12:35:18 -02001538static enum lgdt3306a_lock_status
1539lgdt3306a_qam_lock_poll(struct lgdt3306a_state *state)
Fred Richterb63b36f2014-03-24 19:56:00 -03001540{
Mauro Carvalho Chehabb1a88c72014-10-28 12:00:48 -02001541 u8 cnt;
Fred Richterb63b36f2014-03-24 19:56:00 -03001542 u8 packet_error;
1543 u32 snr;
1544
Mauro Carvalho Chehabb1a88c72014-10-28 12:00:48 -02001545 for (cnt = 0; cnt < 10; cnt++) {
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -03001546 if (lgdt3306a_fec_lock_poll(state) == LG3306_UNLOCK) {
Mauro Carvalho Chehab097117ca2014-10-28 11:35:16 -02001547 dbg_info("no fec lock!\n");
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -03001548 return LG3306_UNLOCK;
Fred Richterb63b36f2014-03-24 19:56:00 -03001549 }
Mauro Carvalho Chehabb1a88c72014-10-28 12:00:48 -02001550
1551 msleep(20);
1552
1553 packet_error = lgdt3306a_get_packet_error(state);
1554 snr = lgdt3306a_calculate_snr_x100(state);
1555 dbg_info("cnt=%d errors=%d snr=%d\n", cnt, packet_error, snr);
1556
1557 if ((snr >= 1500) && (packet_error < 0xff))
1558 return LG3306_LOCK;
Fred Richterb63b36f2014-03-24 19:56:00 -03001559 }
Mauro Carvalho Chehabb1a88c72014-10-28 12:00:48 -02001560
1561 dbg_info("not locked!\n");
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -03001562 return LG3306_UNLOCK;
Fred Richterb63b36f2014-03-24 19:56:00 -03001563}
1564
Mauro Carvalho Chehab0df289a2015-06-07 14:53:52 -03001565static int lgdt3306a_read_status(struct dvb_frontend *fe,
1566 enum fe_status *status)
Fred Richterb63b36f2014-03-24 19:56:00 -03001567{
Fred Richterb63b36f2014-03-24 19:56:00 -03001568 struct lgdt3306a_state *state = fe->demodulator_priv;
Fred Richterb63b36f2014-03-24 19:56:00 -03001569 u16 strength = 0;
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -03001570 int ret = 0;
1571
Fred Richterb63b36f2014-03-24 19:56:00 -03001572 if (fe->ops.tuner_ops.get_rf_strength) {
1573 ret = fe->ops.tuner_ops.get_rf_strength(fe, &strength);
Mauro Carvalho Chehabc9897642014-10-28 12:07:52 -02001574 if (ret == 0)
Mauro Carvalho Chehab097117ca2014-10-28 11:35:16 -02001575 dbg_info("strength=%d\n", strength);
Mauro Carvalho Chehabc9897642014-10-28 12:07:52 -02001576 else
Mauro Carvalho Chehab097117ca2014-10-28 11:35:16 -02001577 dbg_info("fe->ops.tuner_ops.get_rf_strength() failed\n");
Fred Richterb63b36f2014-03-24 19:56:00 -03001578 }
1579
1580 *status = 0;
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -03001581 if (lgdt3306a_neverlock_poll(state) == LG3306_NL_LOCK) {
Fred Richterb63b36f2014-03-24 19:56:00 -03001582 *status |= FE_HAS_SIGNAL;
1583 *status |= FE_HAS_CARRIER;
1584
1585 switch (state->current_modulation) {
1586 case QAM_256:
1587 case QAM_64:
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -03001588 if (lgdt3306a_qam_lock_poll(state) == LG3306_LOCK) {
Fred Richterb63b36f2014-03-24 19:56:00 -03001589 *status |= FE_HAS_VITERBI;
1590 *status |= FE_HAS_SYNC;
1591
1592 *status |= FE_HAS_LOCK;
1593 }
1594 break;
1595 case VSB_8:
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -03001596 if (lgdt3306a_vsb_lock_poll(state) == LG3306_LOCK) {
Fred Richterb63b36f2014-03-24 19:56:00 -03001597 *status |= FE_HAS_VITERBI;
1598 *status |= FE_HAS_SYNC;
1599
1600 *status |= FE_HAS_LOCK;
1601
Mauro Carvalho Chehabee0133e2014-10-28 11:21:48 -02001602 ret = lgdt3306a_monitor_vsb(state);
Fred Richterb63b36f2014-03-24 19:56:00 -03001603 }
1604 break;
1605 default:
1606 ret = -EINVAL;
1607 }
1608 }
1609 return ret;
1610}
1611
1612
1613static int lgdt3306a_read_snr(struct dvb_frontend *fe, u16 *snr)
1614{
1615 struct lgdt3306a_state *state = fe->demodulator_priv;
1616
1617 state->snr = lgdt3306a_calculate_snr_x100(state);
1618 /* report SNR in dB * 10 */
1619 *snr = state->snr/10;
1620
1621 return 0;
1622}
1623
1624static int lgdt3306a_read_signal_strength(struct dvb_frontend *fe,
1625 u16 *strength)
1626{
1627 /*
1628 * Calculate some sort of "strength" from SNR
1629 */
1630 struct lgdt3306a_state *state = fe->demodulator_priv;
Michael Ira Krufky34a5a2f2014-10-25 11:26:15 -03001631 u16 snr; /* snr_x10 */
Fred Richterb63b36f2014-03-24 19:56:00 -03001632 int ret;
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -03001633 u32 ref_snr; /* snr*100 */
Fred Richterb63b36f2014-03-24 19:56:00 -03001634 u32 str;
1635
1636 *strength = 0;
1637
1638 switch (state->current_modulation) {
1639 case VSB_8:
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -03001640 ref_snr = 1600; /* 16dB */
Fred Richterb63b36f2014-03-24 19:56:00 -03001641 break;
1642 case QAM_64:
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -03001643 ref_snr = 2200; /* 22dB */
Fred Richterb63b36f2014-03-24 19:56:00 -03001644 break;
1645 case QAM_256:
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -03001646 ref_snr = 2800; /* 28dB */
Fred Richterb63b36f2014-03-24 19:56:00 -03001647 break;
1648 default:
1649 return -EINVAL;
1650 }
1651
1652 ret = fe->ops.read_snr(fe, &snr);
1653 if (lg_chkerr(ret))
1654 goto fail;
1655
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -03001656 if (state->snr <= (ref_snr - 100))
Fred Richterb63b36f2014-03-24 19:56:00 -03001657 str = 0;
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -03001658 else if (state->snr <= ref_snr)
1659 str = (0xffff * 65) / 100; /* 65% */
Fred Richterb63b36f2014-03-24 19:56:00 -03001660 else {
1661 str = state->snr - ref_snr;
1662 str /= 50;
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -03001663 str += 78; /* 78%-100% */
1664 if (str > 100)
Fred Richterb63b36f2014-03-24 19:56:00 -03001665 str = 100;
1666 str = (0xffff * str) / 100;
1667 }
1668 *strength = (u16)str;
Mauro Carvalho Chehab097117ca2014-10-28 11:35:16 -02001669 dbg_info("strength=%u\n", *strength);
Fred Richterb63b36f2014-03-24 19:56:00 -03001670
1671fail:
1672 return ret;
1673}
1674
1675/* ------------------------------------------------------------------------ */
1676
1677static int lgdt3306a_read_ber(struct dvb_frontend *fe, u32 *ber)
1678{
1679 struct lgdt3306a_state *state = fe->demodulator_priv;
1680 u32 tmp;
1681
1682 *ber = 0;
1683#if 1
Mauro Carvalho Chehab534f4362014-10-28 12:35:18 -02001684 /* FGR - FIXME - I don't know what value is expected by dvb_core
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -03001685 * what is the scale of the value?? */
Mauro Carvalho Chehab4937ba92014-10-28 10:50:36 -02001686 tmp = read_reg(state, 0x00fc); /* NBERVALUE[24-31] */
1687 tmp = (tmp << 8) | read_reg(state, 0x00fd); /* NBERVALUE[16-23] */
1688 tmp = (tmp << 8) | read_reg(state, 0x00fe); /* NBERVALUE[8-15] */
1689 tmp = (tmp << 8) | read_reg(state, 0x00ff); /* NBERVALUE[0-7] */
Fred Richterb63b36f2014-03-24 19:56:00 -03001690 *ber = tmp;
Mauro Carvalho Chehab097117ca2014-10-28 11:35:16 -02001691 dbg_info("ber=%u\n", tmp);
Fred Richterb63b36f2014-03-24 19:56:00 -03001692#endif
1693 return 0;
1694}
1695
1696static int lgdt3306a_read_ucblocks(struct dvb_frontend *fe, u32 *ucblocks)
1697{
1698 struct lgdt3306a_state *state = fe->demodulator_priv;
1699
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -03001700 *ucblocks = 0;
Fred Richterb63b36f2014-03-24 19:56:00 -03001701#if 1
Mauro Carvalho Chehab534f4362014-10-28 12:35:18 -02001702 /* FGR - FIXME - I don't know what value is expected by dvb_core
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -03001703 * what happens when value wraps? */
Mauro Carvalho Chehab4937ba92014-10-28 10:50:36 -02001704 *ucblocks = read_reg(state, 0x00f4); /* TPIFTPERRCNT[0-7] */
Mauro Carvalho Chehab097117ca2014-10-28 11:35:16 -02001705 dbg_info("ucblocks=%u\n", *ucblocks);
Fred Richterb63b36f2014-03-24 19:56:00 -03001706#endif
1707
1708 return 0;
1709}
1710
Mauro Carvalho Chehab534f4362014-10-28 12:35:18 -02001711static int lgdt3306a_tune(struct dvb_frontend *fe, bool re_tune,
1712 unsigned int mode_flags, unsigned int *delay,
Mauro Carvalho Chehab0df289a2015-06-07 14:53:52 -03001713 enum fe_status *status)
Fred Richterb63b36f2014-03-24 19:56:00 -03001714{
1715 int ret = 0;
1716 struct lgdt3306a_state *state = fe->demodulator_priv;
1717
Mauro Carvalho Chehab097117ca2014-10-28 11:35:16 -02001718 dbg_info("re_tune=%u\n", re_tune);
Fred Richterb63b36f2014-03-24 19:56:00 -03001719
1720 if (re_tune) {
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -03001721 state->current_frequency = -1; /* force re-tune */
Michael Ira Krufkyae21e442014-08-03 15:18:23 -03001722 ret = lgdt3306a_set_parameters(fe);
1723 if (ret != 0)
Fred Richterb63b36f2014-03-24 19:56:00 -03001724 return ret;
Fred Richterb63b36f2014-03-24 19:56:00 -03001725 }
1726 *delay = 125;
1727 ret = lgdt3306a_read_status(fe, status);
1728
1729 return ret;
1730}
1731
1732static int lgdt3306a_get_tune_settings(struct dvb_frontend *fe,
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -03001733 struct dvb_frontend_tune_settings
1734 *fe_tune_settings)
Fred Richterb63b36f2014-03-24 19:56:00 -03001735{
1736 fe_tune_settings->min_delay_ms = 100;
Mauro Carvalho Chehab097117ca2014-10-28 11:35:16 -02001737 dbg_info("\n");
Fred Richterb63b36f2014-03-24 19:56:00 -03001738 return 0;
1739}
1740
1741static int lgdt3306a_search(struct dvb_frontend *fe)
1742{
Mauro Carvalho Chehab0df289a2015-06-07 14:53:52 -03001743 enum fe_status status = 0;
Abylay Ospandd145232016-07-25 15:38:59 -03001744 int ret;
Fred Richterb63b36f2014-03-24 19:56:00 -03001745
1746 /* set frontend */
1747 ret = lgdt3306a_set_parameters(fe);
1748 if (ret)
1749 goto error;
1750
Abylay Ospandd145232016-07-25 15:38:59 -03001751 ret = lgdt3306a_read_status(fe, &status);
1752 if (ret)
1753 goto error;
Fred Richterb63b36f2014-03-24 19:56:00 -03001754
1755 /* check if we have a valid signal */
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -03001756 if (status & FE_HAS_LOCK)
Fred Richterb63b36f2014-03-24 19:56:00 -03001757 return DVBFE_ALGO_SEARCH_SUCCESS;
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -03001758 else
Fred Richterb63b36f2014-03-24 19:56:00 -03001759 return DVBFE_ALGO_SEARCH_AGAIN;
Fred Richterb63b36f2014-03-24 19:56:00 -03001760
1761error:
Mauro Carvalho Chehab097117ca2014-10-28 11:35:16 -02001762 dbg_info("failed (%d)\n", ret);
Fred Richterb63b36f2014-03-24 19:56:00 -03001763 return DVBFE_ALGO_SEARCH_ERROR;
1764}
1765
1766static void lgdt3306a_release(struct dvb_frontend *fe)
1767{
1768 struct lgdt3306a_state *state = fe->demodulator_priv;
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -03001769
Mauro Carvalho Chehab097117ca2014-10-28 11:35:16 -02001770 dbg_info("\n");
Fred Richterb63b36f2014-03-24 19:56:00 -03001771 kfree(state);
1772}
1773
Max Kellermannbd336e62016-08-09 18:32:21 -03001774static const struct dvb_frontend_ops lgdt3306a_ops;
Fred Richterb63b36f2014-03-24 19:56:00 -03001775
1776struct dvb_frontend *lgdt3306a_attach(const struct lgdt3306a_config *config,
Mauro Carvalho Chehabc43e6512014-10-28 10:56:10 -02001777 struct i2c_adapter *i2c_adap)
Fred Richterb63b36f2014-03-24 19:56:00 -03001778{
1779 struct lgdt3306a_state *state = NULL;
1780 int ret;
1781 u8 val;
1782
Mauro Carvalho Chehab097117ca2014-10-28 11:35:16 -02001783 dbg_info("(%d-%04x)\n",
Fred Richterb63b36f2014-03-24 19:56:00 -03001784 i2c_adap ? i2c_adapter_id(i2c_adap) : 0,
1785 config ? config->i2c_addr : 0);
1786
1787 state = kzalloc(sizeof(struct lgdt3306a_state), GFP_KERNEL);
1788 if (state == NULL)
1789 goto fail;
1790
1791 state->cfg = config;
1792 state->i2c_adap = i2c_adap;
1793
1794 memcpy(&state->frontend.ops, &lgdt3306a_ops,
1795 sizeof(struct dvb_frontend_ops));
1796 state->frontend.demodulator_priv = state;
1797
1798 /* verify that we're talking to a lg3306a */
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -03001799 /* FGR - NOTE - there is no obvious ChipId to check; we check
1800 * some "known" bits after reset, but it's still just a guess */
Fred Richterb63b36f2014-03-24 19:56:00 -03001801 ret = lgdt3306a_read_reg(state, 0x0000, &val);
1802 if (lg_chkerr(ret))
1803 goto fail;
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -03001804 if ((val & 0x74) != 0x74) {
Mauro Carvalho Chehab097117ca2014-10-28 11:35:16 -02001805 pr_warn("expected 0x74, got 0x%x\n", (val & 0x74));
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -03001806#if 0
Mauro Carvalho Chehab534f4362014-10-28 12:35:18 -02001807 /* FIXME - re-enable when we know this is right */
1808 goto fail;
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -03001809#endif
Fred Richterb63b36f2014-03-24 19:56:00 -03001810 }
1811 ret = lgdt3306a_read_reg(state, 0x0001, &val);
1812 if (lg_chkerr(ret))
1813 goto fail;
Mauro Carvalho Chehab4937ba92014-10-28 10:50:36 -02001814 if ((val & 0xf6) != 0xc6) {
Mauro Carvalho Chehab097117ca2014-10-28 11:35:16 -02001815 pr_warn("expected 0xc6, got 0x%x\n", (val & 0xf6));
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -03001816#if 0
Mauro Carvalho Chehab534f4362014-10-28 12:35:18 -02001817 /* FIXME - re-enable when we know this is right */
1818 goto fail;
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -03001819#endif
Fred Richterb63b36f2014-03-24 19:56:00 -03001820 }
1821 ret = lgdt3306a_read_reg(state, 0x0002, &val);
1822 if (lg_chkerr(ret))
1823 goto fail;
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -03001824 if ((val & 0x73) != 0x03) {
Mauro Carvalho Chehab097117ca2014-10-28 11:35:16 -02001825 pr_warn("expected 0x03, got 0x%x\n", (val & 0x73));
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -03001826#if 0
Mauro Carvalho Chehab534f4362014-10-28 12:35:18 -02001827 /* FIXME - re-enable when we know this is right */
1828 goto fail;
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -03001829#endif
Fred Richterb63b36f2014-03-24 19:56:00 -03001830 }
1831
1832 state->current_frequency = -1;
1833 state->current_modulation = -1;
1834
1835 lgdt3306a_sleep(state);
1836
1837 return &state->frontend;
1838
1839fail:
Mauro Carvalho Chehab097117ca2014-10-28 11:35:16 -02001840 pr_warn("unable to detect LGDT3306A hardware\n");
Fred Richterb63b36f2014-03-24 19:56:00 -03001841 kfree(state);
1842 return NULL;
1843}
Michael Ira Krufkyebd91752014-08-03 15:05:59 -03001844EXPORT_SYMBOL(lgdt3306a_attach);
Fred Richterb63b36f2014-03-24 19:56:00 -03001845
1846#ifdef DBG_DUMP
1847
1848static const short regtab[] = {
Michael Ira Krufkycb4671c2014-10-25 11:12:25 -03001849 0x0000, /* SOFTRSTB 1'b1 1'b1 1'b1 ADCPDB 1'b1 PLLPDB GBBPDB 11111111 */
1850 0x0001, /* 1'b1 1'b1 1'b0 1'b0 AUTORPTRS */
1851 0x0002, /* NI2CRPTEN 1'b0 1'b0 1'b0 SPECINVAUT */
1852 0x0003, /* AGCRFOUT */
1853 0x0004, /* ADCSEL1V ADCCNT ADCCNF ADCCNS ADCCLKPLL */
1854 0x0005, /* PLLINDIVSE */
1855 0x0006, /* PLLCTRL[7:0] 11100001 */
1856 0x0007, /* SYSINITWAITTIME[7:0] (msec) 00001000 */
1857 0x0008, /* STDOPMODE[7:0] 10000000 */
1858 0x0009, /* 1'b0 1'b0 1'b0 STDOPDETTMODE[2:0] STDOPDETCMODE[1:0] 00011110 */
Mauro Carvalho Chehab4937ba92014-10-28 10:50:36 -02001859 0x000a, /* DAFTEN 1'b1 x x SCSYSLOCK */
1860 0x000b, /* SCSYSLOCKCHKTIME[7:0] (10msec) 01100100 */
1861 0x000d, /* x SAMPLING4 */
1862 0x000e, /* SAMFREQ[15:8] 00000000 */
1863 0x000f, /* SAMFREQ[7:0] 00000000 */
Michael Ira Krufkycb4671c2014-10-25 11:12:25 -03001864 0x0010, /* IFFREQ[15:8] 01100000 */
1865 0x0011, /* IFFREQ[7:0] 00000000 */
1866 0x0012, /* AGCEN AGCREFMO */
1867 0x0013, /* AGCRFFIXB AGCIFFIXB AGCLOCKDETRNGSEL[1:0] 1'b1 1'b0 1'b0 1'b0 11101000 */
1868 0x0014, /* AGCFIXVALUE[7:0] 01111111 */
1869 0x0015, /* AGCREF[15:8] 00001010 */
1870 0x0016, /* AGCREF[7:0] 11100100 */
1871 0x0017, /* AGCDELAY[7:0] 00100000 */
1872 0x0018, /* AGCRFBW[3:0] AGCIFBW[3:0] 10001000 */
1873 0x0019, /* AGCUDOUTMODE[1:0] AGCUDCTRLLEN[1:0] AGCUDCTRL */
Mauro Carvalho Chehab4937ba92014-10-28 10:50:36 -02001874 0x001c, /* 1'b1 PFEN MFEN AICCVSYNC */
1875 0x001d, /* 1'b0 1'b1 1'b0 1'b1 AICCVSYNC */
1876 0x001e, /* AICCALPHA[3:0] 1'b1 1'b0 1'b1 1'b0 01111010 */
1877 0x001f, /* AICCDETTH[19:16] AICCOFFTH[19:16] 00000000 */
Michael Ira Krufkycb4671c2014-10-25 11:12:25 -03001878 0x0020, /* AICCDETTH[15:8] 01111100 */
1879 0x0021, /* AICCDETTH[7:0] 00000000 */
1880 0x0022, /* AICCOFFTH[15:8] 00000101 */
1881 0x0023, /* AICCOFFTH[7:0] 11100000 */
1882 0x0024, /* AICCOPMODE3[1:0] AICCOPMODE2[1:0] AICCOPMODE1[1:0] AICCOPMODE0[1:0] 00000000 */
1883 0x0025, /* AICCFIXFREQ3[23:16] 00000000 */
1884 0x0026, /* AICCFIXFREQ3[15:8] 00000000 */
1885 0x0027, /* AICCFIXFREQ3[7:0] 00000000 */
1886 0x0028, /* AICCFIXFREQ2[23:16] 00000000 */
1887 0x0029, /* AICCFIXFREQ2[15:8] 00000000 */
Mauro Carvalho Chehab4937ba92014-10-28 10:50:36 -02001888 0x002a, /* AICCFIXFREQ2[7:0] 00000000 */
1889 0x002b, /* AICCFIXFREQ1[23:16] 00000000 */
1890 0x002c, /* AICCFIXFREQ1[15:8] 00000000 */
1891 0x002d, /* AICCFIXFREQ1[7:0] 00000000 */
1892 0x002e, /* AICCFIXFREQ0[23:16] 00000000 */
1893 0x002f, /* AICCFIXFREQ0[15:8] 00000000 */
Michael Ira Krufkycb4671c2014-10-25 11:12:25 -03001894 0x0030, /* AICCFIXFREQ0[7:0] 00000000 */
1895 0x0031, /* 1'b0 1'b1 1'b0 1'b0 x DAGC1STER */
1896 0x0032, /* DAGC1STEN DAGC1STER */
1897 0x0033, /* DAGC1STREF[15:8] 00001010 */
1898 0x0034, /* DAGC1STREF[7:0] 11100100 */
1899 0x0035, /* DAGC2NDE */
1900 0x0036, /* DAGC2NDREF[15:8] 00001010 */
1901 0x0037, /* DAGC2NDREF[7:0] 10000000 */
1902 0x0038, /* DAGC2NDLOCKDETRNGSEL[1:0] */
Mauro Carvalho Chehab4937ba92014-10-28 10:50:36 -02001903 0x003d, /* 1'b1 SAMGEARS */
Michael Ira Krufkycb4671c2014-10-25 11:12:25 -03001904 0x0040, /* SAMLFGMA */
1905 0x0041, /* SAMLFBWM */
1906 0x0044, /* 1'b1 CRGEARSHE */
1907 0x0045, /* CRLFGMAN */
1908 0x0046, /* CFLFBWMA */
1909 0x0047, /* CRLFGMAN */
1910 0x0048, /* x x x x CRLFGSTEP_VS[3:0] xxxx1001 */
1911 0x0049, /* CRLFBWMA */
Mauro Carvalho Chehab4937ba92014-10-28 10:50:36 -02001912 0x004a, /* CRLFBWMA */
Michael Ira Krufkycb4671c2014-10-25 11:12:25 -03001913 0x0050, /* 1'b0 1'b1 1'b1 1'b0 MSECALCDA */
1914 0x0070, /* TPOUTEN TPIFEN TPCLKOUTE */
1915 0x0071, /* TPSENB TPSSOPBITE */
1916 0x0073, /* TP47HINS x x CHBERINT PERMODE[1:0] PERINT[1:0] 1xx11100 */
1917 0x0075, /* x x x x x IQSWAPCTRL[2:0] xxxxx000 */
1918 0x0076, /* NBERCON NBERST NBERPOL NBERWSYN */
1919 0x0077, /* x NBERLOSTTH[2:0] NBERACQTH[3:0] x0000000 */
1920 0x0078, /* NBERPOLY[31:24] 00000000 */
1921 0x0079, /* NBERPOLY[23:16] 00000000 */
Mauro Carvalho Chehab4937ba92014-10-28 10:50:36 -02001922 0x007a, /* NBERPOLY[15:8] 00000000 */
1923 0x007b, /* NBERPOLY[7:0] 00000000 */
1924 0x007c, /* NBERPED[31:24] 00000000 */
1925 0x007d, /* NBERPED[23:16] 00000000 */
1926 0x007e, /* NBERPED[15:8] 00000000 */
1927 0x007f, /* NBERPED[7:0] 00000000 */
Michael Ira Krufkycb4671c2014-10-25 11:12:25 -03001928 0x0080, /* x AGCLOCK DAGCLOCK SYSLOCK x x NEVERLOCK[1:0] */
1929 0x0085, /* SPECINVST */
1930 0x0088, /* SYSLOCKTIME[15:8] */
1931 0x0089, /* SYSLOCKTIME[7:0] */
Mauro Carvalho Chehab4937ba92014-10-28 10:50:36 -02001932 0x008c, /* FECLOCKTIME[15:8] */
1933 0x008d, /* FECLOCKTIME[7:0] */
1934 0x008e, /* AGCACCOUT[15:8] */
1935 0x008f, /* AGCACCOUT[7:0] */
Michael Ira Krufkycb4671c2014-10-25 11:12:25 -03001936 0x0090, /* AICCREJSTATUS[3:0] AICCREJBUSY[3:0] */
1937 0x0091, /* AICCVSYNC */
Mauro Carvalho Chehab4937ba92014-10-28 10:50:36 -02001938 0x009c, /* CARRFREQOFFSET[15:8] */
1939 0x009d, /* CARRFREQOFFSET[7:0] */
1940 0x00a1, /* SAMFREQOFFSET[23:16] */
1941 0x00a2, /* SAMFREQOFFSET[15:8] */
1942 0x00a3, /* SAMFREQOFFSET[7:0] */
1943 0x00a6, /* SYNCLOCK SYNCLOCKH */
Michael Ira Krufky6da7ac92014-10-25 11:05:05 -03001944#if 0 /* covered elsewhere */
Mauro Carvalho Chehab4937ba92014-10-28 10:50:36 -02001945 0x00e8, /* CONSTPWR[15:8] */
1946 0x00e9, /* CONSTPWR[7:0] */
1947 0x00ea, /* BMSE[15:8] */
1948 0x00eb, /* BMSE[7:0] */
1949 0x00ec, /* MSE[15:8] */
1950 0x00ed, /* MSE[7:0] */
1951 0x00ee, /* CONSTI[7:0] */
1952 0x00ef, /* CONSTQ[7:0] */
Fred Richterb63b36f2014-03-24 19:56:00 -03001953#endif
Mauro Carvalho Chehab4937ba92014-10-28 10:50:36 -02001954 0x00f4, /* TPIFTPERRCNT[7:0] */
1955 0x00f5, /* TPCORREC */
1956 0x00f6, /* VBBER[15:8] */
1957 0x00f7, /* VBBER[7:0] */
1958 0x00f8, /* VABER[15:8] */
1959 0x00f9, /* VABER[7:0] */
1960 0x00fa, /* TPERRCNT[7:0] */
1961 0x00fb, /* NBERLOCK x x x x x x x */
1962 0x00fc, /* NBERVALUE[31:24] */
1963 0x00fd, /* NBERVALUE[23:16] */
1964 0x00fe, /* NBERVALUE[15:8] */
1965 0x00ff, /* NBERVALUE[7:0] */
Michael Ira Krufkycb4671c2014-10-25 11:12:25 -03001966 0x1000, /* 1'b0 WODAGCOU */
1967 0x1005, /* x x 1'b1 1'b1 x SRD_Q_QM */
1968 0x1009, /* SRDWAITTIME[7:0] (10msec) 00100011 */
Mauro Carvalho Chehab4937ba92014-10-28 10:50:36 -02001969 0x100a, /* SRDWAITTIME_CQS[7:0] (msec) 01100100 */
1970 0x101a, /* x 1'b1 1'b0 1'b0 x QMDQAMMODE[2:0] x100x010 */
Michael Ira Krufkycb4671c2014-10-25 11:12:25 -03001971 0x1036, /* 1'b0 1'b1 1'b0 1'b0 SAMGSEND_CQS[3:0] 01001110 */
Mauro Carvalho Chehab4937ba92014-10-28 10:50:36 -02001972 0x103c, /* SAMGSAUTOSTL_V[3:0] SAMGSAUTOEDL_V[3:0] 01000110 */
1973 0x103d, /* 1'b1 1'b1 SAMCNORMBP_V[1:0] 1'b0 1'b0 SAMMODESEL_V[1:0] 11100001 */
1974 0x103f, /* SAMZTEDSE */
1975 0x105d, /* EQSTATUSE */
1976 0x105f, /* x PMAPG2_V[2:0] x DMAPG2_V[2:0] x001x011 */
Michael Ira Krufkycb4671c2014-10-25 11:12:25 -03001977 0x1060, /* 1'b1 EQSTATUSE */
1978 0x1061, /* CRMAPBWSTL_V[3:0] CRMAPBWEDL_V[3:0] 00000100 */
1979 0x1065, /* 1'b0 x CRMODE_V[1:0] 1'b1 x 1'b1 x 0x111x1x */
1980 0x1066, /* 1'b0 1'b0 1'b1 1'b0 1'b1 PNBOOSTSE */
1981 0x1068, /* CREPHNGAIN2_V[3:0] CREPHNPBW_V[3:0] 10010001 */
Mauro Carvalho Chehab4937ba92014-10-28 10:50:36 -02001982 0x106e, /* x x x x x CREPHNEN_ */
1983 0x106f, /* CREPHNTH_V[7:0] 00010101 */
Michael Ira Krufkycb4671c2014-10-25 11:12:25 -03001984 0x1072, /* CRSWEEPN */
1985 0x1073, /* CRPGAIN_V[3:0] x x 1'b1 1'b1 1001xx11 */
1986 0x1074, /* CRPBW_V[3:0] x x 1'b1 1'b1 0001xx11 */
1987 0x1080, /* DAFTSTATUS[1:0] x x x x x x */
1988 0x1081, /* SRDSTATUS[1:0] x x x x x SRDLOCK */
Mauro Carvalho Chehab4937ba92014-10-28 10:50:36 -02001989 0x10a9, /* EQSTATUS_CQS[1:0] x x x x x x */
1990 0x10b7, /* EQSTATUS_V[1:0] x x x x x x */
Michael Ira Krufky6da7ac92014-10-25 11:05:05 -03001991#if 0 /* SMART_ANT */
Mauro Carvalho Chehab4937ba92014-10-28 10:50:36 -02001992 0x1f00, /* MODEDETE */
1993 0x1f01, /* x x x x x x x SFNRST xxxxxxx0 */
1994 0x1f03, /* NUMOFANT[7:0] 10000000 */
1995 0x1f04, /* x SELMASK[6:0] x0000000 */
1996 0x1f05, /* x SETMASK[6:0] x0000000 */
1997 0x1f06, /* x TXDATA[6:0] x0000000 */
1998 0x1f07, /* x CHNUMBER[6:0] x0000000 */
1999 0x1f09, /* AGCTIME[23:16] 10011000 */
2000 0x1f0a, /* AGCTIME[15:8] 10010110 */
2001 0x1f0b, /* AGCTIME[7:0] 10000000 */
2002 0x1f0c, /* ANTTIME[31:24] 00000000 */
2003 0x1f0d, /* ANTTIME[23:16] 00000011 */
2004 0x1f0e, /* ANTTIME[15:8] 10010000 */
2005 0x1f0f, /* ANTTIME[7:0] 10010000 */
2006 0x1f11, /* SYNCTIME[23:16] 10011000 */
2007 0x1f12, /* SYNCTIME[15:8] 10010110 */
2008 0x1f13, /* SYNCTIME[7:0] 10000000 */
2009 0x1f14, /* SNRTIME[31:24] 00000001 */
2010 0x1f15, /* SNRTIME[23:16] 01111101 */
2011 0x1f16, /* SNRTIME[15:8] 01111000 */
2012 0x1f17, /* SNRTIME[7:0] 01000000 */
2013 0x1f19, /* FECTIME[23:16] 00000000 */
2014 0x1f1a, /* FECTIME[15:8] 01110010 */
2015 0x1f1b, /* FECTIME[7:0] 01110000 */
2016 0x1f1d, /* FECTHD[7:0] 00000011 */
2017 0x1f1f, /* SNRTHD[23:16] 00001000 */
2018 0x1f20, /* SNRTHD[15:8] 01111111 */
2019 0x1f21, /* SNRTHD[7:0] 10000101 */
2020 0x1f80, /* IRQFLG x x SFSDRFLG MODEBFLG SAVEFLG SCANFLG TRACKFLG */
2021 0x1f81, /* x SYNCCON SNRCON FECCON x STDBUSY SYNCRST AGCFZCO */
2022 0x1f82, /* x x x SCANOPCD[4:0] */
2023 0x1f83, /* x x x x MAINOPCD[3:0] */
2024 0x1f84, /* x x RXDATA[13:8] */
2025 0x1f85, /* RXDATA[7:0] */
2026 0x1f86, /* x x SDTDATA[13:8] */
2027 0x1f87, /* SDTDATA[7:0] */
2028 0x1f89, /* ANTSNR[23:16] */
2029 0x1f8a, /* ANTSNR[15:8] */
2030 0x1f8b, /* ANTSNR[7:0] */
2031 0x1f8c, /* x x x x ANTFEC[13:8] */
2032 0x1f8d, /* ANTFEC[7:0] */
2033 0x1f8e, /* MAXCNT[7:0] */
2034 0x1f8f, /* SCANCNT[7:0] */
2035 0x1f91, /* MAXPW[23:16] */
2036 0x1f92, /* MAXPW[15:8] */
2037 0x1f93, /* MAXPW[7:0] */
2038 0x1f95, /* CURPWMSE[23:16] */
2039 0x1f96, /* CURPWMSE[15:8] */
2040 0x1f97, /* CURPWMSE[7:0] */
Michael Ira Krufky6da7ac92014-10-25 11:05:05 -03002041#endif /* SMART_ANT */
Mauro Carvalho Chehab4937ba92014-10-28 10:50:36 -02002042 0x211f, /* 1'b1 1'b1 1'b1 CIRQEN x x 1'b0 1'b0 1111xx00 */
2043 0x212a, /* EQAUTOST */
Michael Ira Krufkycb4671c2014-10-25 11:12:25 -03002044 0x2122, /* CHFAST[7:0] 01100000 */
Mauro Carvalho Chehab4937ba92014-10-28 10:50:36 -02002045 0x212b, /* FFFSTEP_V[3:0] x FBFSTEP_V[2:0] 0001x001 */
2046 0x212c, /* PHDEROTBWSEL[3:0] 1'b1 1'b1 1'b1 1'b0 10001110 */
2047 0x212d, /* 1'b1 1'b1 1'b1 1'b1 x x TPIFLOCKS */
Michael Ira Krufkycb4671c2014-10-25 11:12:25 -03002048 0x2135, /* DYNTRACKFDEQ[3:0] x 1'b0 1'b0 1'b0 1010x000 */
2049 0x2141, /* TRMODE[1:0] 1'b1 1'b1 1'b0 1'b1 1'b1 1'b1 01110111 */
2050 0x2162, /* AICCCTRLE */
2051 0x2173, /* PHNCNFCNT[7:0] 00000100 */
2052 0x2179, /* 1'b0 1'b0 1'b0 1'b1 x BADSINGLEDYNTRACKFBF[2:0] 0001x001 */
Mauro Carvalho Chehab4937ba92014-10-28 10:50:36 -02002053 0x217a, /* 1'b0 1'b0 1'b0 1'b1 x BADSLOWSINGLEDYNTRACKFBF[2:0] 0001x001 */
2054 0x217e, /* CNFCNTTPIF[7:0] 00001000 */
2055 0x217f, /* TPERRCNTTPIF[7:0] 00000001 */
Michael Ira Krufkycb4671c2014-10-25 11:12:25 -03002056 0x2180, /* x x x x x x FBDLYCIR[9:8] */
2057 0x2181, /* FBDLYCIR[7:0] */
2058 0x2185, /* MAXPWRMAIN[7:0] */
2059 0x2191, /* NCOMBDET x x x x x x x */
2060 0x2199, /* x MAINSTRON */
Mauro Carvalho Chehab4937ba92014-10-28 10:50:36 -02002061 0x219a, /* FFFEQSTEPOUT_V[3:0] FBFSTEPOUT_V[2:0] */
2062 0x21a1, /* x x SNRREF[5:0] */
Michael Ira Krufkycb4671c2014-10-25 11:12:25 -03002063 0x2845, /* 1'b0 1'b1 x x FFFSTEP_CQS[1:0] FFFCENTERTAP[1:0] 01xx1110 */
2064 0x2846, /* 1'b0 x 1'b0 1'b1 FBFSTEP_CQS[1:0] 1'b1 1'b0 0x011110 */
2065 0x2847, /* ENNOSIGDE */
2066 0x2849, /* 1'b1 1'b1 NOUSENOSI */
Mauro Carvalho Chehab4937ba92014-10-28 10:50:36 -02002067 0x284a, /* EQINITWAITTIME[7:0] 01100100 */
Michael Ira Krufkycb4671c2014-10-25 11:12:25 -03002068 0x3000, /* 1'b1 1'b1 1'b1 x x x 1'b0 RPTRSTM */
2069 0x3001, /* RPTRSTWAITTIME[7:0] (100msec) 00110010 */
2070 0x3031, /* FRAMELOC */
2071 0x3032, /* 1'b1 1'b0 1'b0 1'b0 x x FRAMELOCKMODE_CQS[1:0] 1000xx11 */
Mauro Carvalho Chehab4937ba92014-10-28 10:50:36 -02002072 0x30a9, /* VDLOCK_Q FRAMELOCK */
2073 0x30aa, /* MPEGLOCK */
Fred Richterb63b36f2014-03-24 19:56:00 -03002074};
2075
Thomas Meyer1f679ff2017-09-03 08:19:31 -04002076#define numDumpRegs (ARRAY_SIZE(regtab))
Fred Richterb63b36f2014-03-24 19:56:00 -03002077static u8 regval1[numDumpRegs] = {0, };
2078static u8 regval2[numDumpRegs] = {0, };
2079
2080static void lgdt3306a_DumpAllRegs(struct lgdt3306a_state *state)
2081{
2082 memset(regval2, 0xff, sizeof(regval2));
2083 lgdt3306a_DumpRegs(state);
2084}
2085
2086static void lgdt3306a_DumpRegs(struct lgdt3306a_state *state)
2087{
2088 int i;
2089 int sav_debug = debug;
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -03002090
Fred Richterb63b36f2014-03-24 19:56:00 -03002091 if ((debug & DBG_DUMP) == 0)
2092 return;
Michael Ira Krufky831a9112014-10-25 11:20:57 -03002093 debug &= ~DBG_REG; /* suppress DBG_REG during reg dump */
Fred Richterb63b36f2014-03-24 19:56:00 -03002094
Mauro Carvalho Chehab097117ca2014-10-28 11:35:16 -02002095 lg_debug("\n");
Fred Richterb63b36f2014-03-24 19:56:00 -03002096
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -03002097 for (i = 0; i < numDumpRegs; i++) {
Fred Richterb63b36f2014-03-24 19:56:00 -03002098 lgdt3306a_read_reg(state, regtab[i], &regval1[i]);
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -03002099 if (regval1[i] != regval2[i]) {
Mauro Carvalho Chehab097117ca2014-10-28 11:35:16 -02002100 lg_debug(" %04X = %02X\n", regtab[i], regval1[i]);
Mauro Carvalho Chehab16afc672015-04-28 18:31:21 -03002101 regval2[i] = regval1[i];
Fred Richterb63b36f2014-03-24 19:56:00 -03002102 }
2103 }
2104 debug = sav_debug;
2105}
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -03002106#endif /* DBG_DUMP */
Fred Richterb63b36f2014-03-24 19:56:00 -03002107
2108
2109
Max Kellermannbd336e62016-08-09 18:32:21 -03002110static const struct dvb_frontend_ops lgdt3306a_ops = {
Fred Richterb63b36f2014-03-24 19:56:00 -03002111 .delsys = { SYS_ATSC, SYS_DVBC_ANNEX_B },
2112 .info = {
2113 .name = "LG Electronics LGDT3306A VSB/QAM Frontend",
Fred Richterb63b36f2014-03-24 19:56:00 -03002114 .frequency_min = 54000000,
Michael Ira Krufky8e8cd342014-07-27 19:24:24 -03002115 .frequency_max = 858000000,
Fred Richterb63b36f2014-03-24 19:56:00 -03002116 .frequency_stepsize = 62500,
2117 .caps = FE_CAN_QAM_64 | FE_CAN_QAM_256 | FE_CAN_8VSB
2118 },
2119 .i2c_gate_ctrl = lgdt3306a_i2c_gate_ctrl,
2120 .init = lgdt3306a_init,
2121 .sleep = lgdt3306a_fe_sleep,
2122 /* if this is set, it overrides the default swzigzag */
2123 .tune = lgdt3306a_tune,
2124 .set_frontend = lgdt3306a_set_parameters,
2125 .get_frontend = lgdt3306a_get_frontend,
2126 .get_frontend_algo = lgdt3306a_get_frontend_algo,
2127 .get_tune_settings = lgdt3306a_get_tune_settings,
2128 .read_status = lgdt3306a_read_status,
2129 .read_ber = lgdt3306a_read_ber,
2130 .read_signal_strength = lgdt3306a_read_signal_strength,
2131 .read_snr = lgdt3306a_read_snr,
2132 .read_ucblocks = lgdt3306a_read_ucblocks,
2133 .release = lgdt3306a_release,
2134 .ts_bus_ctrl = lgdt3306a_ts_bus_ctrl,
2135 .search = lgdt3306a_search,
2136};
2137
Kevin Cheng4f751892017-01-10 01:14:18 -02002138static int lgdt3306a_select(struct i2c_mux_core *muxc, u32 chan)
2139{
2140 struct i2c_client *client = i2c_mux_priv(muxc);
2141 struct lgdt3306a_state *state = i2c_get_clientdata(client);
2142
2143 return lgdt3306a_i2c_gate_ctrl(&state->frontend, 1);
2144}
2145
2146static int lgdt3306a_deselect(struct i2c_mux_core *muxc, u32 chan)
2147{
2148 struct i2c_client *client = i2c_mux_priv(muxc);
2149 struct lgdt3306a_state *state = i2c_get_clientdata(client);
2150
2151 return lgdt3306a_i2c_gate_ctrl(&state->frontend, 0);
2152}
2153
2154static int lgdt3306a_probe(struct i2c_client *client,
2155 const struct i2c_device_id *id)
2156{
2157 struct lgdt3306a_config *config;
2158 struct lgdt3306a_state *state;
2159 struct dvb_frontend *fe;
2160 int ret;
2161
2162 config = kzalloc(sizeof(struct lgdt3306a_config), GFP_KERNEL);
2163 if (config == NULL) {
2164 ret = -ENOMEM;
2165 goto fail;
2166 }
2167
2168 memcpy(config, client->dev.platform_data,
2169 sizeof(struct lgdt3306a_config));
2170
2171 config->i2c_addr = client->addr;
2172 fe = lgdt3306a_attach(config, client->adapter);
2173 if (fe == NULL) {
2174 ret = -ENODEV;
2175 goto err_fe;
2176 }
2177
2178 i2c_set_clientdata(client, fe->demodulator_priv);
2179 state = fe->demodulator_priv;
Brad Love5b3a8e92018-01-04 19:04:17 -05002180 state->frontend.ops.release = NULL;
Kevin Cheng4f751892017-01-10 01:14:18 -02002181
2182 /* create mux i2c adapter for tuner */
2183 state->muxc = i2c_mux_alloc(client->adapter, &client->dev,
2184 1, 0, I2C_MUX_LOCKED,
2185 lgdt3306a_select, lgdt3306a_deselect);
2186 if (!state->muxc) {
2187 ret = -ENOMEM;
2188 goto err_kfree;
2189 }
2190 state->muxc->priv = client;
2191 ret = i2c_mux_add_adapter(state->muxc, 0, 0, 0);
2192 if (ret)
2193 goto err_kfree;
2194
2195 /* create dvb_frontend */
2196 fe->ops.i2c_gate_ctrl = NULL;
2197 *config->i2c_adapter = state->muxc->adapter[0];
2198 *config->fe = fe;
2199
2200 return 0;
2201
2202err_kfree:
2203 kfree(state);
2204err_fe:
2205 kfree(config);
2206fail:
2207 dev_dbg(&client->dev, "failed=%d\n", ret);
2208 return ret;
2209}
2210
2211static int lgdt3306a_remove(struct i2c_client *client)
2212{
2213 struct lgdt3306a_state *state = i2c_get_clientdata(client);
2214
2215 i2c_mux_del_adapters(state->muxc);
2216
2217 state->frontend.ops.release = NULL;
2218 state->frontend.demodulator_priv = NULL;
2219
2220 kfree(state->cfg);
2221 kfree(state);
2222
2223 return 0;
2224}
2225
2226static const struct i2c_device_id lgdt3306a_id_table[] = {
2227 {"lgdt3306a", 0},
2228 {}
2229};
2230MODULE_DEVICE_TABLE(i2c, lgdt3306a_id_table);
2231
2232static struct i2c_driver lgdt3306a_driver = {
2233 .driver = {
2234 .name = "lgdt3306a",
2235 .suppress_bind_attrs = true,
2236 },
2237 .probe = lgdt3306a_probe,
2238 .remove = lgdt3306a_remove,
2239 .id_table = lgdt3306a_id_table,
2240};
2241
2242module_i2c_driver(lgdt3306a_driver);
2243
Fred Richterb63b36f2014-03-24 19:56:00 -03002244MODULE_DESCRIPTION("LG Electronics LGDT3306A ATSC/QAM-B Demodulator Driver");
2245MODULE_AUTHOR("Fred Richter <frichter@hauppauge.com>");
2246MODULE_LICENSE("GPL");
2247MODULE_VERSION("0.2");