lgdt3306a.c 52.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
/*
 *    Support for LGDT3306A - 8VSB/QAM-B
 *
 *    Copyright (C) 2013 Fred Richter <frichter@hauppauge.com>
 *    - driver structure based on lgdt3305.[ch] by Michael Krufky
 *    - code based on LG3306_V0.35 API by LG Electronics Inc.
 *
 *    This program is free software; you can redistribute it and/or modify
 *    it under the terms of the GNU General Public License as published by
 *    the Free Software Foundation; either version 2 of the License, or
 *    (at your option) any later version.
 *
 *    This program is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU General Public License for more details.
 *
 *    You should have received a copy of the GNU General Public License
 *    along with this program; if not, write to the Free Software
 *    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 */

#include <asm/div64.h>
#include <linux/dvb/frontend.h>
#include "dvb_math.h"
#include "lgdt3306a.h"


static int debug;
module_param(debug, int, 0644);
MODULE_PARM_DESC(debug, "set debug level (info=1, reg=2 (or-able))");

#define DBG_INFO 1
#define DBG_REG  2
36
#define DBG_DUMP 4 /* FGR - comment out to remove dump code */
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79

#define lg_printk(kern, fmt, arg...)					\
	printk(kern "%s(): " fmt, __func__, ##arg)

#define lg_info(fmt, arg...)	printk(KERN_INFO "lgdt3306a: " fmt, ##arg)
#define lg_warn(fmt, arg...)	lg_printk(KERN_WARNING,       fmt, ##arg)
#define lg_err(fmt, arg...)	lg_printk(KERN_ERR,           fmt, ##arg)
#define lg_dbg(fmt, arg...) if (debug & DBG_INFO)			\
				lg_printk(KERN_DEBUG,         fmt, ##arg)
#define lg_reg(fmt, arg...) if (debug & DBG_REG)			\
				lg_printk(KERN_DEBUG,         fmt, ##arg)

#define lg_chkerr(ret)							\
({									\
	int __ret;							\
	__ret = (ret < 0);						\
	if (__ret)							\
		lg_err("error %d on line %d\n",	ret, __LINE__);		\
	__ret;								\
})

struct lgdt3306a_state {
	struct i2c_adapter *i2c_adap;
	const struct lgdt3306a_config *cfg;

	struct dvb_frontend frontend;

	fe_modulation_t current_modulation;
	u32 current_frequency;
	u32 snr;
};

/* -----------------------------------------------
 LG3306A Register Usage
   (LG does not really name the registers, so this code does not either)
 0000 -> 00FF Common control and status
 1000 -> 10FF Synchronizer control and status
 1F00 -> 1FFF Smart Antenna control and status
 2100 -> 21FF VSB Equalizer control and status
 2800 -> 28FF QAM Equalizer control and status
 3000 -> 30FF FEC control and status
 ---------------------------------------------- */

80 81 82
enum lgdt3306a_lock_status {
	LG3306_UNLOCK       = 0x00,
	LG3306_LOCK         = 0x01,
83
	LG3306_UNKNOWN_LOCK = 0xff
84
};
85

86
enum lgdt3306a_neverlock_status {
87 88 89 90
	LG3306_NL_INIT    = 0x00,
	LG3306_NL_PROCESS = 0x01,
	LG3306_NL_LOCK    = 0x02,
	LG3306_NL_FAIL    = 0x03,
91
	LG3306_NL_UNKNOWN = 0xff
92
};
93

94 95 96 97
enum lgdt3306a_modulation {
	LG3306_VSB          = 0x00,
	LG3306_QAM64        = 0x01,
	LG3306_QAM256       = 0x02,
98
	LG3306_UNKNOWN_MODE = 0xff
99
};
100

101
enum lgdt3306a_lock_check {
102 103 104 105
	LG3306_SYNC_LOCK,
	LG3306_FEC_LOCK,
	LG3306_TR_LOCK,
	LG3306_AGC_LOCK,
106
};
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221


#ifdef DBG_DUMP
static void lgdt3306a_DumpAllRegs(struct lgdt3306a_state *state);
static void lgdt3306a_DumpRegs(struct lgdt3306a_state *state);
#endif


static int lgdt3306a_write_reg(struct lgdt3306a_state *state, u16 reg, u8 val)
{
	int ret;
	u8 buf[] = { reg >> 8, reg & 0xff, val };
	struct i2c_msg msg = {
		.addr = state->cfg->i2c_addr, .flags = 0,
		.buf = buf, .len = 3,
	};

	lg_reg("reg: 0x%04x, val: 0x%02x\n", reg, val);

	ret = i2c_transfer(state->i2c_adap, &msg, 1);

	if (ret != 1) {
		lg_err("error (addr %02x %02x <- %02x, err = %i)\n",
		       msg.buf[0], msg.buf[1], msg.buf[2], ret);
		if (ret < 0)
			return ret;
		else
			return -EREMOTEIO;
	}
	return 0;
}

static int lgdt3306a_read_reg(struct lgdt3306a_state *state, u16 reg, u8 *val)
{
	int ret;
	u8 reg_buf[] = { reg >> 8, reg & 0xff };
	struct i2c_msg msg[] = {
		{ .addr = state->cfg->i2c_addr,
		  .flags = 0, .buf = reg_buf, .len = 2 },
		{ .addr = state->cfg->i2c_addr,
		  .flags = I2C_M_RD, .buf = val, .len = 1 },
	};

	ret = i2c_transfer(state->i2c_adap, msg, 2);

	if (ret != 2) {
		lg_err("error (addr %02x reg %04x error (ret == %i)\n",
		       state->cfg->i2c_addr, reg, ret);
		if (ret < 0)
			return ret;
		else
			return -EREMOTEIO;
	}
	lg_reg("reg: 0x%04x, val: 0x%02x\n", reg, *val);

	return 0;
}

#define read_reg(state, reg)						\
({									\
	u8 __val;							\
	int ret = lgdt3306a_read_reg(state, reg, &__val);		\
	if (lg_chkerr(ret))						\
		__val = 0;						\
	__val;								\
})

static int lgdt3306a_set_reg_bit(struct lgdt3306a_state *state,
				u16 reg, int bit, int onoff)
{
	u8 val;
	int ret;

	lg_reg("reg: 0x%04x, bit: %d, level: %d\n", reg, bit, onoff);

	ret = lgdt3306a_read_reg(state, reg, &val);
	if (lg_chkerr(ret))
		goto fail;

	val &= ~(1 << bit);
	val |= (onoff & 1) << bit;

	ret = lgdt3306a_write_reg(state, reg, val);
	lg_chkerr(ret);
fail:
	return ret;
}

/* ------------------------------------------------------------------------ */

static int lgdt3306a_soft_reset(struct lgdt3306a_state *state)
{
	int ret;

	lg_dbg("\n");

	ret = lgdt3306a_set_reg_bit(state, 0x0000, 7, 0);
	if (lg_chkerr(ret))
		goto fail;

	msleep(20);
	ret = lgdt3306a_set_reg_bit(state, 0x0000, 7, 1);
	lg_chkerr(ret);

fail:
	return ret;
}

static int lgdt3306a_mpeg_mode(struct lgdt3306a_state *state,
				     enum lgdt3306a_mpeg_mode mode)
{
	u8 val;
	int ret;

	lg_dbg("(%d)\n", mode);
222 223
	/* transport packet format */
	ret = lgdt3306a_set_reg_bit(state, 0x0071, 7, mode == LGDT3306A_MPEG_PARALLEL?1:0); /* TPSENB=0x80 */
224 225 226
	if (lg_chkerr(ret))
		goto fail;

227 228
	/* start of packet signal duration */
	ret = lgdt3306a_set_reg_bit(state, 0x0071, 6, 0); /* TPSSOPBITEN=0x40; 0=byte duration, 1=bit duration */
229 230 231 232 233 234 235
	if (lg_chkerr(ret))
		goto fail;

	ret = lgdt3306a_read_reg(state, 0x0070, &val);
	if (lg_chkerr(ret))
		goto fail;

236
	val |= 0x10; /* TPCLKSUPB=0x10 */
237

238
	if (mode == LGDT3306A_MPEG_PARALLEL)
239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260
		val &= ~0x10;

	ret = lgdt3306a_write_reg(state, 0x0070, val);
	lg_chkerr(ret);

fail:
	return ret;
}

static int lgdt3306a_mpeg_mode_polarity(struct lgdt3306a_state *state,
				       enum lgdt3306a_tp_clock_edge edge,
				       enum lgdt3306a_tp_valid_polarity valid)
{
	u8 val;
	int ret;

	lg_dbg("edge=%d, valid=%d\n", edge, valid);

	ret = lgdt3306a_read_reg(state, 0x0070, &val);
	if (lg_chkerr(ret))
		goto fail;

261
	val &= ~0x06; /* TPCLKPOL=0x04, TPVALPOL=0x02 */
262

263
	if (edge == LGDT3306A_TPCLK_RISING_EDGE)
264
		val |= 0x04;
265
	if (valid == LGDT3306A_TP_VALID_HIGH)
266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282
		val |= 0x02;

	ret = lgdt3306a_write_reg(state, 0x0070, val);
	lg_chkerr(ret);

fail:
	return ret;
}

static int lgdt3306a_mpeg_tristate(struct lgdt3306a_state *state,
				     int mode)
{
	u8 val;
	int ret;

	lg_dbg("(%d)\n", mode);

283
	if (mode) {
284 285 286
		ret = lgdt3306a_read_reg(state, 0x0070, &val);
		if (lg_chkerr(ret))
			goto fail;
287
		val &= ~0xa8; /* Tristate bus; TPOUTEN=0x80, TPCLKOUTEN=0x20, TPDATAOUTEN=0x08 */
288 289 290 291
		ret = lgdt3306a_write_reg(state, 0x0070, val);
		if (lg_chkerr(ret))
			goto fail;

292
		ret = lgdt3306a_set_reg_bit(state, 0x0003, 6, 1); /* AGCIFOUTENB=0x40; 1=Disable IFAGC pin */
293 294 295 296
		if (lg_chkerr(ret))
			goto fail;

	} else {
297
		ret = lgdt3306a_set_reg_bit(state, 0x0003, 6, 0); /* enable IFAGC pin */
298 299 300 301 302 303 304
		if (lg_chkerr(ret))
			goto fail;

		ret = lgdt3306a_read_reg(state, 0x0070, &val);
		if (lg_chkerr(ret))
			goto fail;

305
		val |= 0xa8; /* enable bus */
306 307 308 309 310 311 312 313 314
		ret = lgdt3306a_write_reg(state, 0x0070, val);
		if (lg_chkerr(ret))
			goto fail;
	}

fail:
	return ret;
}

315
static int lgdt3306a_ts_bus_ctrl(struct dvb_frontend *fe, int acquire)
316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331
{
	struct lgdt3306a_state *state = fe->demodulator_priv;

	lg_dbg("acquire=%d\n", acquire);

	return lgdt3306a_mpeg_tristate(state, acquire ? 0 : 1);

}

static int lgdt3306a_power(struct lgdt3306a_state *state,
				     int mode)
{
	int ret;

	lg_dbg("(%d)\n", mode);

332 333
	if (mode == 0) {
		ret = lgdt3306a_set_reg_bit(state, 0x0000, 7, 0); /* into reset */
334 335 336
		if (lg_chkerr(ret))
			goto fail;

337
		ret = lgdt3306a_set_reg_bit(state, 0x0000, 0, 0); /* power down */
338 339 340 341
		if (lg_chkerr(ret))
			goto fail;

	} else {
342
		ret = lgdt3306a_set_reg_bit(state, 0x0000, 7, 1); /* out of reset */
343 344 345
		if (lg_chkerr(ret))
			goto fail;

346
		ret = lgdt3306a_set_reg_bit(state, 0x0000, 0, 1); /* power up */
347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365
		if (lg_chkerr(ret))
			goto fail;
	}

#ifdef DBG_DUMP
	lgdt3306a_DumpAllRegs(state);
#endif
fail:
	return ret;
}


static int lgdt3306a_set_vsb(struct lgdt3306a_state *state)
{
	u8 val;
	int ret;

	lg_dbg("\n");

366
	/* 0. Spectrum inversion detection manual; spectrum inverted */
367
	ret = lgdt3306a_read_reg(state, 0x0002, &val);
368
	val &= 0xf7; /* SPECINVAUTO Off */
369
	val |= 0x04; /* SPECINV On */
370 371 372 373
	ret = lgdt3306a_write_reg(state, 0x0002, val);
	if (lg_chkerr(ret))
		goto fail;

374
	/* 1. Selection of standard mode(0x08=QAM, 0x80=VSB) */
375 376 377 378
	ret = lgdt3306a_write_reg(state, 0x0008, 0x80);
	if (lg_chkerr(ret))
		goto fail;

379
	/* 2. Bandwidth mode for VSB(6MHz) */
380
	ret = lgdt3306a_read_reg(state, 0x0009, &val);
381 382
	val &= 0xe3;
	val |= 0x0c; /* STDOPDETTMODE[2:0]=3 */
383 384 385 386
	ret = lgdt3306a_write_reg(state, 0x0009, val);
	if (lg_chkerr(ret))
		goto fail;

387
	/* 3. QAM mode detection mode(None) */
388
	ret = lgdt3306a_read_reg(state, 0x0009, &val);
389
	val &= 0xfc; /* STDOPDETCMODE[1:0]=0 */
390 391 392 393
	ret = lgdt3306a_write_reg(state, 0x0009, val);
	if (lg_chkerr(ret))
		goto fail;

394
	/* 4. ADC sampling frequency rate(2x sampling) */
395 396 397
	ret = lgdt3306a_read_reg(state, 0x000d, &val);
	val &= 0xbf; /* SAMPLING4XFEN=0 */
	ret = lgdt3306a_write_reg(state, 0x000d, val);
398 399 400
	if (lg_chkerr(ret))
		goto fail;

401 402 403
#if 0
	/* FGR - disable any AICC filtering, testing only */

404 405 406 407
	ret = lgdt3306a_write_reg(state, 0x0024, 0x00);
	if (lg_chkerr(ret))
		goto fail;

408
	/* AICCFIXFREQ0 NT N-1(Video rejection) */
409 410
	ret = lgdt3306a_write_reg(state, 0x002e, 0x00);
	ret = lgdt3306a_write_reg(state, 0x002f, 0x00);
411 412
	ret = lgdt3306a_write_reg(state, 0x0030, 0x00);

413
	/* AICCFIXFREQ1 NT N-1(Audio rejection) */
414 415 416
	ret = lgdt3306a_write_reg(state, 0x002b, 0x00);
	ret = lgdt3306a_write_reg(state, 0x002c, 0x00);
	ret = lgdt3306a_write_reg(state, 0x002d, 0x00);
417

418
	/* AICCFIXFREQ2 NT Co-Channel(Video rejection) */
419 420
	ret = lgdt3306a_write_reg(state, 0x0028, 0x00);
	ret = lgdt3306a_write_reg(state, 0x0029, 0x00);
421
	ret = lgdt3306a_write_reg(state, 0x002a, 0x00);
422

423
	/* AICCFIXFREQ3 NT Co-Channel(Audio rejection) */
424 425 426 427
	ret = lgdt3306a_write_reg(state, 0x0025, 0x00);
	ret = lgdt3306a_write_reg(state, 0x0026, 0x00);
	ret = lgdt3306a_write_reg(state, 0x0027, 0x00);

428 429 430 431
#else
	/* FGR - this works well for HVR-1955,1975 */

	/* 5. AICCOPMODE  NT N-1 Adj. */
432 433 434 435
	ret = lgdt3306a_write_reg(state, 0x0024, 0x5A);
	if (lg_chkerr(ret))
		goto fail;

436
	/* AICCFIXFREQ0 NT N-1(Video rejection) */
437 438
	ret = lgdt3306a_write_reg(state, 0x002e, 0x5A);
	ret = lgdt3306a_write_reg(state, 0x002f, 0x00);
439 440
	ret = lgdt3306a_write_reg(state, 0x0030, 0x00);

441
	/* AICCFIXFREQ1 NT N-1(Audio rejection) */
442 443 444
	ret = lgdt3306a_write_reg(state, 0x002b, 0x36);
	ret = lgdt3306a_write_reg(state, 0x002c, 0x00);
	ret = lgdt3306a_write_reg(state, 0x002d, 0x00);
445

446
	/* AICCFIXFREQ2 NT Co-Channel(Video rejection) */
447 448
	ret = lgdt3306a_write_reg(state, 0x0028, 0x2A);
	ret = lgdt3306a_write_reg(state, 0x0029, 0x00);
449
	ret = lgdt3306a_write_reg(state, 0x002a, 0x00);
450

451
	/* AICCFIXFREQ3 NT Co-Channel(Audio rejection) */
452 453 454 455 456
	ret = lgdt3306a_write_reg(state, 0x0025, 0x06);
	ret = lgdt3306a_write_reg(state, 0x0026, 0x00);
	ret = lgdt3306a_write_reg(state, 0x0027, 0x00);
#endif

457 458 459 460
	ret = lgdt3306a_read_reg(state, 0x001e, &val);
	val &= 0x0f;
	val |= 0xa0;
	ret = lgdt3306a_write_reg(state, 0x001e, val);
461 462 463 464 465

	ret = lgdt3306a_write_reg(state, 0x0022, 0x08);

	ret = lgdt3306a_write_reg(state, 0x0023, 0xFF);

466 467 468
	ret = lgdt3306a_read_reg(state, 0x211f, &val);
	val &= 0xef;
	ret = lgdt3306a_write_reg(state, 0x211f, val);
469 470 471 472

	ret = lgdt3306a_write_reg(state, 0x2173, 0x01);

	ret = lgdt3306a_read_reg(state, 0x1061, &val);
473
	val &= 0xf8;
474 475 476
	val |= 0x04;
	ret = lgdt3306a_write_reg(state, 0x1061, val);

477 478 479
	ret = lgdt3306a_read_reg(state, 0x103d, &val);
	val &= 0xcf;
	ret = lgdt3306a_write_reg(state, 0x103d, val);
480 481 482 483

	ret = lgdt3306a_write_reg(state, 0x2122, 0x40);

	ret = lgdt3306a_read_reg(state, 0x2141, &val);
484
	val &= 0x3f;
485 486 487
	ret = lgdt3306a_write_reg(state, 0x2141, val);

	ret = lgdt3306a_read_reg(state, 0x2135, &val);
488
	val &= 0x0f;
489 490 491 492
	val |= 0x70;
	ret = lgdt3306a_write_reg(state, 0x2135, val);

	ret = lgdt3306a_read_reg(state, 0x0003, &val);
493
	val &= 0xf7;
494 495
	ret = lgdt3306a_write_reg(state, 0x0003, val);

496 497 498
	ret = lgdt3306a_read_reg(state, 0x001c, &val);
	val &= 0x7f;
	ret = lgdt3306a_write_reg(state, 0x001c, val);
499

500
	/* 6. EQ step size */
501
	ret = lgdt3306a_read_reg(state, 0x2179, &val);
502
	val &= 0xf8;
503 504
	ret = lgdt3306a_write_reg(state, 0x2179, val);

505 506 507
	ret = lgdt3306a_read_reg(state, 0x217a, &val);
	val &= 0xf8;
	ret = lgdt3306a_write_reg(state, 0x217a, val);
508

509
	/* 7. Reset */
510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525
	ret = lgdt3306a_soft_reset(state);
	if (lg_chkerr(ret))
		goto fail;

	lg_dbg("complete\n");
fail:
	return ret;
}

static int lgdt3306a_set_qam(struct lgdt3306a_state *state, int modulation)
{
	u8 val;
	int ret;

	lg_dbg("modulation=%d\n", modulation);

526
	/* 1. Selection of standard mode(0x08=QAM, 0x80=VSB) */
527 528 529 530
	ret = lgdt3306a_write_reg(state, 0x0008, 0x08);
	if (lg_chkerr(ret))
		goto fail;

531
	/* 1a. Spectrum inversion detection to Auto */
532
	ret = lgdt3306a_read_reg(state, 0x0002, &val);
533
	val &= 0xfb; /* SPECINV Off */
534
	val |= 0x08; /* SPECINVAUTO On */
535 536 537 538
	ret = lgdt3306a_write_reg(state, 0x0002, val);
	if (lg_chkerr(ret))
		goto fail;

539
	/* 2. Bandwidth mode for QAM */
540
	ret = lgdt3306a_read_reg(state, 0x0009, &val);
541
	val &= 0xe3; /* STDOPDETTMODE[2:0]=0 VSB Off */
542 543 544 545
	ret = lgdt3306a_write_reg(state, 0x0009, val);
	if (lg_chkerr(ret))
		goto fail;

546
	/* 3. : 64QAM/256QAM detection(manual, auto) */
547
	ret = lgdt3306a_read_reg(state, 0x0009, &val);
548
	val &= 0xfc;
549
	val |= 0x02; /* STDOPDETCMODE[1:0]=1=Manual 2=Auto */
550 551 552 553
	ret = lgdt3306a_write_reg(state, 0x0009, val);
	if (lg_chkerr(ret))
		goto fail;

554
	/* 3a. : 64QAM/256QAM selection for manual */
555
	ret = lgdt3306a_read_reg(state, 0x101a, &val);
556
	val &= 0xf8;
557 558 559 560 561
	if (modulation == QAM_64)
		val |= 0x02; /* QMDQMODE[2:0]=2=QAM64 */
	else
		val |= 0x04; /* QMDQMODE[2:0]=4=QAM256 */

562 563 564 565
	ret = lgdt3306a_write_reg(state, 0x101a, val);
	if (lg_chkerr(ret))
		goto fail;

566
	/* 4. ADC sampling frequency rate(4x sampling) */
567 568
	ret = lgdt3306a_read_reg(state, 0x000d, &val);
	val &= 0xbf;
569
	val |= 0x40; /* SAMPLING4XFEN=1 */
570
	ret = lgdt3306a_write_reg(state, 0x000d, val);
571 572 573
	if (lg_chkerr(ret))
		goto fail;

574
	/* 5. No AICC operation in QAM mode */
575 576 577 578 579 580
	ret = lgdt3306a_read_reg(state, 0x0024, &val);
	val &= 0x00;
	ret = lgdt3306a_write_reg(state, 0x0024, val);
	if (lg_chkerr(ret))
		goto fail;

581
	/* 6. Reset */
582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624
	ret = lgdt3306a_soft_reset(state);
	if (lg_chkerr(ret))
		goto fail;

	lg_dbg("complete\n");
fail:
	return ret;
}

static int lgdt3306a_set_modulation(struct lgdt3306a_state *state,
				   struct dtv_frontend_properties *p)
{
	int ret;

	lg_dbg("\n");

	switch (p->modulation) {
	case VSB_8:
		ret = lgdt3306a_set_vsb(state);
		break;
	case QAM_64:
		ret = lgdt3306a_set_qam(state, QAM_64);
		break;
	case QAM_256:
		ret = lgdt3306a_set_qam(state, QAM_256);
		break;
	default:
		return -EINVAL;
	}
	if (lg_chkerr(ret))
		goto fail;

	state->current_modulation = p->modulation;

fail:
	return ret;
}

/* ------------------------------------------------------------------------ */

static int lgdt3306a_agc_setup(struct lgdt3306a_state *state,
			      struct dtv_frontend_properties *p)
{
625
	/* TODO: anything we want to do here??? */
626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648
	lg_dbg("\n");

	switch (p->modulation) {
	case VSB_8:
		break;
	case QAM_64:
	case QAM_256:
		break;
	default:
		return -EINVAL;
	}
	return 0;
}

/* ------------------------------------------------------------------------ */

static int lgdt3306a_set_inversion(struct lgdt3306a_state *state,
				       int inversion)
{
	int ret;

	lg_dbg("(%d)\n", inversion);

649
	ret = lgdt3306a_set_reg_bit(state, 0x0002, 2, inversion ? 1 : 0);
650 651 652 653 654 655 656 657 658 659
	return ret;
}

static int lgdt3306a_set_inversion_auto(struct lgdt3306a_state *state,
				       int enabled)
{
	int ret;

	lg_dbg("(%d)\n", enabled);

660 661
	/* 0=Manual 1=Auto(QAM only) */
	ret = lgdt3306a_set_reg_bit(state, 0x0002, 3, enabled);/* SPECINVAUTO=0x04 */
662 663 664 665 666 667 668 669 670 671
	return ret;
}

static int lgdt3306a_spectral_inversion(struct lgdt3306a_state *state,
				       struct dtv_frontend_properties *p,
				       int inversion)
{
	int ret = 0;

	lg_dbg("(%d)\n", inversion);
672 673
#if 0
/* FGR - spectral_inversion defaults already set for VSB and QAM; can enable later if desired */
674 675 676 677 678

	ret = lgdt3306a_set_inversion(state, inversion);

	switch (p->modulation) {
	case VSB_8:
679
		ret = lgdt3306a_set_inversion_auto(state, 0); /* Manual only for VSB */
680 681 682
		break;
	case QAM_64:
	case QAM_256:
683
		ret = lgdt3306a_set_inversion_auto(state, 1); /* Auto ok for QAM */
684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710
		break;
	default:
		ret = -EINVAL;
	}
#endif
	return ret;
}

static int lgdt3306a_set_if(struct lgdt3306a_state *state,
			   struct dtv_frontend_properties *p)
{
	int ret;
	u16 if_freq_khz;
	u8 nco1, nco2;

	switch (p->modulation) {
	case VSB_8:
		if_freq_khz = state->cfg->vsb_if_khz;
		break;
	case QAM_64:
	case QAM_256:
		if_freq_khz = state->cfg->qam_if_khz;
		break;
	default:
		return -EINVAL;
	}

711
	switch (if_freq_khz) {
712 713
	default:
	    lg_warn("IF=%d KHz is not supportted, 3250 assumed\n", if_freq_khz);
714
		/* fallthrough */
715
	case 3250: /* 3.25Mhz */
716 717 718
		nco1 = 0x34;
		nco2 = 0x00;
		break;
719
	case 3500: /* 3.50Mhz */
720 721 722
		nco1 = 0x38;
		nco2 = 0x00;
		break;
723
	case 4000: /* 4.00Mhz */
724 725 726
		nco1 = 0x40;
		nco2 = 0x00;
		break;
727
	case 5000: /* 5.00Mhz */
728 729 730
		nco1 = 0x50;
		nco2 = 0x00;
		break;
731
	case 5380: /* 5.38Mhz */
732 733 734 735 736
		nco1 = 0x56;
		nco2 = 0x14;
		break;
	}
	ret = lgdt3306a_write_reg(state, 0x0010, nco1);
737 738
	if (ret)
		return ret;
739
	ret = lgdt3306a_write_reg(state, 0x0011, nco2);
740 741
	if (ret)
		return ret;
742 743 744 745 746 747 748 749 750 751 752 753

	lg_dbg("if_freq=%d KHz->[%04x]\n", if_freq_khz, nco1<<8 | nco2);

	return 0;
}

/* ------------------------------------------------------------------------ */

static int lgdt3306a_i2c_gate_ctrl(struct dvb_frontend *fe, int enable)
{
	struct lgdt3306a_state *state = fe->demodulator_priv;

754
	if (state->cfg->deny_i2c_rptr) {
755 756 757 758 759
		lg_dbg("deny_i2c_rptr=%d\n", state->cfg->deny_i2c_rptr);
		return 0;
	}
	lg_dbg("(%d)\n", enable);

760
	return lgdt3306a_set_reg_bit(state, 0x0002, 7, enable ? 0 : 1); /* NI2CRPTEN=0x80 */
761 762 763 764 765 766 767
}

static int lgdt3306a_sleep(struct lgdt3306a_state *state)
{
	int ret;

	lg_dbg("\n");
768
	state->current_frequency = -1; /* force re-tune, when we wake */
769

770
	ret = lgdt3306a_mpeg_tristate(state, 1); /* disable data bus */
771 772 773
	if (lg_chkerr(ret))
		goto fail;

774
	ret = lgdt3306a_power(state, 0); /* power down */
775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795
	lg_chkerr(ret);

fail:
	return 0;
}

static int lgdt3306a_fe_sleep(struct dvb_frontend *fe)
{
	struct lgdt3306a_state *state = fe->demodulator_priv;

	return lgdt3306a_sleep(state);
}

static int lgdt3306a_init(struct dvb_frontend *fe)
{
	struct lgdt3306a_state *state = fe->demodulator_priv;
	u8 val;
	int ret;

	lg_dbg("\n");

796 797
	/* 1. Normal operation mode */
	ret = lgdt3306a_set_reg_bit(state, 0x0001, 0, 1); /* SIMFASTENB=0x01 */
798 799 800
	if (lg_chkerr(ret))
		goto fail;

801
	/* 2. Spectrum inversion auto detection (Not valid for VSB) */
802 803 804 805
	ret = lgdt3306a_set_inversion_auto(state, 0);
	if (lg_chkerr(ret))
		goto fail;

806
	/* 3. Spectrum inversion(According to the tuner configuration) */
807 808 809 810
	ret = lgdt3306a_set_inversion(state, 1);
	if (lg_chkerr(ret))
		goto fail;

811 812
	/* 4. Peak-to-peak voltage of ADC input signal */
	ret = lgdt3306a_set_reg_bit(state, 0x0004, 7, 1); /* ADCSEL1V=0x80=1Vpp; 0x00=2Vpp */
813 814 815
	if (lg_chkerr(ret))
		goto fail;

816 817
	/* 5. ADC output data capture clock phase */
	ret = lgdt3306a_set_reg_bit(state, 0x0004, 2, 0); /* 0=same phase as ADC clock */
818 819 820
	if (lg_chkerr(ret))
		goto fail;

821 822
	/* 5a. ADC sampling clock source */
	ret = lgdt3306a_set_reg_bit(state, 0x0004, 3, 0); /* ADCCLKPLLSEL=0x08; 0=use ext clock, not PLL */
823 824 825
	if (lg_chkerr(ret))
		goto fail;

826 827
	/* 6. Automatic PLL set */
	ret = lgdt3306a_set_reg_bit(state, 0x0005, 6, 0); /* PLLSETAUTO=0x40; 0=off */
828 829 830
	if (lg_chkerr(ret))
		goto fail;

831 832
	if (state->cfg->xtalMHz == 24) {	/* 24MHz */
		/* 7. Frequency for PLL output(0x2564 for 192MHz for 24MHz) */
833 834 835
		ret = lgdt3306a_read_reg(state, 0x0005, &val);
		if (lg_chkerr(ret))
			goto fail;
836
		val &= 0xc0;
837 838 839 840 841 842 843 844
		val |= 0x25;
		ret = lgdt3306a_write_reg(state, 0x0005, val);
		if (lg_chkerr(ret))
			goto fail;
		ret = lgdt3306a_write_reg(state, 0x0006, 0x64);
		if (lg_chkerr(ret))
			goto fail;

845
		/* 8. ADC sampling frequency(0x180000 for 24MHz sampling) */
846
		ret = lgdt3306a_read_reg(state, 0x000d, &val);
847 848
		if (lg_chkerr(ret))
			goto fail;
849
		val &= 0xc0;
850
		val |= 0x18;
851
		ret = lgdt3306a_write_reg(state, 0x000d, val);
852 853 854
		if (lg_chkerr(ret))
			goto fail;

855 856
	} else if (state->cfg->xtalMHz == 25) { /* 25MHz */
		/* 7. Frequency for PLL output */
857 858 859
		ret = lgdt3306a_read_reg(state, 0x0005, &val);
		if (lg_chkerr(ret))
			goto fail;
860
		val &= 0xc0;
861 862 863 864 865 866 867 868
		val |= 0x25;
		ret = lgdt3306a_write_reg(state, 0x0005, val);
		if (lg_chkerr(ret))
			goto fail;
		ret = lgdt3306a_write_reg(state, 0x0006, 0x64);
		if (lg_chkerr(ret))
			goto fail;

869
		/* 8. ADC sampling frequency(0x190000 for 25MHz sampling) */
870
		ret = lgdt3306a_read_reg(state, 0x000d, &val);
871 872
		if (lg_chkerr(ret))
			goto fail;
873
		val &= 0xc0;
874
		val |= 0x19;
875
		ret = lgdt3306a_write_reg(state, 0x000d, val);
876 877 878 879 880
		if (lg_chkerr(ret))
			goto fail;
	} else {
		lg_err("Bad xtalMHz=%d\n", state->cfg->xtalMHz);
	}
881
#if 0
882 883
	ret = lgdt3306a_write_reg(state, 0x000e, 0x00);
	ret = lgdt3306a_write_reg(state, 0x000f, 0x00);
884
#endif
885

886 887 888
	/* 9. Center frequency of input signal of ADC */
	ret = lgdt3306a_write_reg(state, 0x0010, 0x34); /* 3.25MHz */
	ret = lgdt3306a_write_reg(state, 0x0011, 0x00);
889

890 891
	/* 10. Fixed gain error value */
	ret = lgdt3306a_write_reg(state, 0x0014, 0); /* gain error=0 */
892

893
	/* 10a. VSB TR BW gear shift initial step */
894 895
	ret = lgdt3306a_read_reg(state, 0x103c, &val);
	val &= 0x0f;
896
	val |= 0x20; /* SAMGSAUTOSTL_V[3:0] = 2 */
897
	ret = lgdt3306a_write_reg(state, 0x103c, val);
898

899
	/* 10b. Timing offset calibration in low temperature for VSB */
900 901
	ret = lgdt3306a_read_reg(state, 0x103d, &val);
	val &= 0xfc;
902
	val |= 0x03;
903
	ret = lgdt3306a_write_reg(state, 0x103d, val);
904

905
	/* 10c. Timing offset calibration in low temperature for QAM */
906
	ret = lgdt3306a_read_reg(state, 0x1036, &val);
907 908
	val &= 0xf0;
	val |= 0x0c;
909 910
	ret = lgdt3306a_write_reg(state, 0x1036, val);

911
	/* 11. Using the imaginary part of CIR in CIR loading */
912 913 914
	ret = lgdt3306a_read_reg(state, 0x211f, &val);
	val &= 0xef; /* do not use imaginary of CIR */
	ret = lgdt3306a_write_reg(state, 0x211f, val);
915

916
	/* 12. Control of no signal detector function */
917
	ret = lgdt3306a_read_reg(state, 0x2849, &val);
918
	val &= 0xef; /* NOUSENOSIGDET=0, enable no signal detector */
919 920
	ret = lgdt3306a_write_reg(state, 0x2849, val);

921
	/* FGR - put demod in some known mode */
922 923
	ret = lgdt3306a_set_vsb(state);

924
	/* 13. TP stream format */
925 926
	ret = lgdt3306a_mpeg_mode(state, state->cfg->mpeg_mode);

927
	/* 14. disable output buses */
928 929
	ret = lgdt3306a_mpeg_tristate(state, 1);

930
	/* 15. Sleep (in reset) */
931 932 933 934 935 936 937 938 939 940 941 942 943 944 945
	ret = lgdt3306a_sleep(state);
	lg_chkerr(ret);

fail:
	return ret;
}

static int lgdt3306a_set_parameters(struct dvb_frontend *fe)
{
	struct dtv_frontend_properties *p = &fe->dtv_property_cache;
	struct lgdt3306a_state *state = fe->demodulator_priv;
	int ret;

	lg_dbg("(%d, %d)\n", p->frequency, p->modulation);

946 947
	if (state->current_frequency  == p->frequency &&
	   state->current_modulation == p->modulation) {
948 949 950 951 952 953
		lg_dbg(" (already set, skipping ...)\n");
		return 0;
	}
	state->current_frequency = -1;
	state->current_modulation = -1;

954
	ret = lgdt3306a_power(state, 1); /* power up */
955 956 957 958 959 960 961
	if (lg_chkerr(ret))
		goto fail;

	if (fe->ops.tuner_ops.set_params) {
		ret = fe->ops.tuner_ops.set_params(fe);
		if (fe->ops.i2c_gate_ctrl)
			fe->ops.i2c_gate_ctrl(fe, 0);
962 963 964 965 966
#if 0
		if (lg_chkerr(ret))
			goto fail;
		state->current_frequency = p->frequency;
#endif
967 968 969 970 971 972 973 974 975 976 977 978 979 980 981
	}

	ret = lgdt3306a_set_modulation(state, p);
	if (lg_chkerr(ret))
		goto fail;

	ret = lgdt3306a_agc_setup(state, p);
	if (lg_chkerr(ret))
		goto fail;

	ret = lgdt3306a_set_if(state, p);
	if (lg_chkerr(ret))
		goto fail;

	ret = lgdt3306a_spectral_inversion(state, p,
982
					  state->cfg->spectral_inversion ? 1 : 0);
983 984 985 986 987 988 989 990 991 992 993 994 995
	if (lg_chkerr(ret))
		goto fail;

	ret = lgdt3306a_mpeg_mode(state, state->cfg->mpeg_mode);
	if (lg_chkerr(ret))
		goto fail;

	ret = lgdt3306a_mpeg_mode_polarity(state,
					  state->cfg->tpclk_edge,
					  state->cfg->tpvalid_polarity);
	if (lg_chkerr(ret))
		goto fail;

996
	ret = lgdt3306a_mpeg_tristate(state, 0); /* enable data bus */
997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033
	if (lg_chkerr(ret))
		goto fail;

	ret = lgdt3306a_soft_reset(state);
	if (lg_chkerr(ret))
		goto fail;

#ifdef DBG_DUMP
	lgdt3306a_DumpAllRegs(state);
#endif
	state->current_frequency = p->frequency;
fail:
	return ret;
}

static int lgdt3306a_get_frontend(struct dvb_frontend *fe)
{
	struct lgdt3306a_state *state = fe->demodulator_priv;
	struct dtv_frontend_properties *p = &fe->dtv_property_cache;

	lg_dbg("(%u, %d)\n", state->current_frequency, state->current_modulation);

	p->modulation = state->current_modulation;
	p->frequency = state->current_frequency;
	return 0;
}

static enum dvbfe_algo lgdt3306a_get_frontend_algo(struct dvb_frontend *fe)
{
#if 1
	return DVBFE_ALGO_CUSTOM;
#else
	return DVBFE_ALGO_HW;
#endif
}

/* ------------------------------------------------------------------------ */
1034
static int lgdt3306a_monitor_vsb(struct lgdt3306a_state *state)
1035 1036 1037
{
	u8 val;
	int ret;
1038 1039
	u8 snrRef, maxPowerMan, nCombDet;
	u16 fbDlyCir;
1040

1041
	ret = lgdt3306a_read_reg(state, 0x21a1, &val);
1042 1043
	if (ret)
		return ret;
1044
	snrRef = val & 0x3f;
1045 1046

	ret = lgdt3306a_read_reg(state, 0x2185, &maxPowerMan);
1047 1048
	if (ret)
		return ret;
1049 1050

	ret = lgdt3306a_read_reg(state, 0x2191, &val);
1051 1052
	if (ret)
		return ret;
1053 1054 1055
	nCombDet = (val & 0x80) >> 7;

	ret = lgdt3306a_read_reg(state, 0x2180, &val);
1056 1057
	if (ret)
		return ret;
1058
	fbDlyCir = (val & 0x03) << 8;
1059

1060
	ret = lgdt3306a_read_reg(state, 0x2181, &val);
1061 1062
	if (ret)
		return ret;
1063 1064 1065 1066 1067
	fbDlyCir |= val;

	lg_dbg("snrRef=%d maxPowerMan=0x%x nCombDet=%d fbDlyCir=0x%x\n",
		snrRef, maxPowerMan, nCombDet, fbDlyCir);

1068
	/* Carrier offset sub loop bandwidth */
1069
	ret = lgdt3306a_read_reg(state, 0x1061, &val);
1070 1071
	if (ret)
		return ret;
1072
	val &= 0xf8;
1073
	if ((snrRef > 18) && (maxPowerMan > 0x68) && (nCombDet == 0x01) && ((fbDlyCir == 0x03FF) || (fbDlyCir < 0x6C)))	{
1074 1075
		/* SNR is over 18dB and no ghosting */
		val |= 0x00; /* final bandwidth = 0 */
1076
	} else {
1077
		val |= 0x04; /* final bandwidth = 4 */
1078 1079
	}
	ret = lgdt3306a_write_reg(state, 0x1061, val);
1080 1081
	if (ret)
		return ret;
1082

1083
	/* Adjust Notch Filter */
1084
	ret = lgdt3306a_read_reg(state, 0x0024, &val);
1085 1086
	if (ret)
		return ret;
1087
	val &= 0x0f;
1088
	if (nCombDet == 0) { /* Turn on the Notch Filter */
1089 1090 1091
		val |= 0x50;
	}
	ret = lgdt3306a_write_reg(state, 0x0024, val);
1092 1093
	if (ret)
		return ret;
1094

1095
	/* VSB Timing Recovery output normalization */
1096
	ret = lgdt3306a_read_reg(state, 0x103d, &val);
1097 1098
	if (ret)
		return ret;
1099
	val &= 0xcf;
1100
	val |= 0x20;
1101
	ret = lgdt3306a_write_reg(state, 0x103d, val);
1102 1103

	return ret;
1104 1105
}

1106
static enum lgdt3306a_modulation lgdt3306a_check_oper_mode(struct lgdt3306a_state *state)
1107 1108 1109 1110 1111
{
	u8 val = 0;
	int ret;

	ret = lgdt3306a_read_reg(state, 0x0081, &val);
1112 1113
	if (ret)
		goto err;
1114 1115 1116

	if (val & 0x80)	{
		lg_dbg("VSB\n");
1117
		return LG3306_VSB;
1118
	}
1119
	if (val & 0x08) {
1120
		ret = lgdt3306a_read_reg(state, 0x00a6, &val);
1121 1122
		if (ret)
			goto err;
1123 1124 1125
		val = val >> 2;
		if (val & 0x01) {
			lg_dbg("QAM256\n");
1126
			return LG3306_QAM256;
1127 1128
		} else {
			lg_dbg("QAM64\n");
1129
			return LG3306_QAM64;
1130 1131
		}
	}
1132
err:
1133
	lg_warn("UNKNOWN\n");
1134
	return LG3306_UNKNOWN_MODE;
1135 1136
}

1137 1138
static enum lgdt3306a_lock_status lgdt3306a_check_lock_status(struct lgdt3306a_state *state,
			enum lgdt3306a_lock_check whatLock)
1139 1140 1141
{
	u8 val = 0;
	int ret;
1142 1143
	enum lgdt3306a_modulation	modeOper;
	enum lgdt3306a_lock_status lockStatus;
1144 1145 1146

	modeOper = LG3306_UNKNOWN_MODE;

1147 1148 1149
	switch (whatLock) {
	case LG3306_SYNC_LOCK:
	{
1150
		ret = lgdt3306a_read_reg(state, 0x00a6, &val);
1151 1152
		if (ret)
			return ret;
1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164

		if ((val & 0x80) == 0x80)
			lockStatus = LG3306_LOCK;
		else
			lockStatus = LG3306_UNLOCK;

		lg_dbg("SYNC_LOCK=%x\n", lockStatus);
		break;
	}
	case LG3306_AGC_LOCK:
	{
		ret = lgdt3306a_read_reg(state, 0x0080, &val);
1165 1166
		if (ret)
			return ret;
1167 1168 1169 1170 1171 1172 1173 1174 1175 1176

		if ((val & 0x40) == 0x40)
			lockStatus = LG3306_LOCK;
		else
			lockStatus = LG3306_UNLOCK;

		lg_dbg("AGC_LOCK=%x\n", lockStatus);
		break;
	}
	case LG3306_TR_LOCK:
1177
	{
1178 1179 1180
		modeOper = lgdt3306a_check_oper_mode(state);
		if ((modeOper == LG3306_QAM64) || (modeOper == LG3306_QAM256)) {
			ret = lgdt3306a_read_reg(state, 0x1094, &val);
1181 1182
			if (ret)
				return ret;
1183 1184 1185 1186 1187

			if ((val & 0x80) == 0x80)
				lockStatus = LG3306_LOCK;
			else
				lockStatus = LG3306_UNLOCK;
1188 1189
		} else
			lockStatus = LG3306_UNKNOWN_LOCK;
1190

1191 1192 1193 1194 1195 1196 1197
		lg_dbg("TR_LOCK=%x\n", lockStatus);
		break;
	}
	case LG3306_FEC_LOCK:
	{
		modeOper = lgdt3306a_check_oper_mode(state);
		if ((modeOper == LG3306_QAM64) || (modeOper == LG3306_QAM256)) {
1198
			ret = lgdt3306a_read_reg(state, 0x0080, &val);
1199 1200
			if (ret)
				return ret;
1201

1202
			if ((val & 0x10) == 0x10)
1203 1204 1205
				lockStatus = LG3306_LOCK;
			else
				lockStatus = LG3306_UNLOCK;
1206 1207
		} else
			lockStatus = LG3306_UNKNOWN_LOCK;
1208

1209 1210 1211
		lg_dbg("FEC_LOCK=%x\n", lockStatus);
		break;
	}
1212

1213 1214 1215 1216
	default:
		lockStatus = LG3306_UNKNOWN_LOCK;
		lg_warn("UNKNOWN whatLock=%d\n", whatLock);
		break;
1217 1218
	}

1219
	return lockStatus;
1220 1221
}

1222
static enum lgdt3306a_neverlock_status lgdt3306a_check_neverlock_status(struct lgdt3306a_state *state)
1223 1224 1225
{
	u8 val = 0;
	int ret;
1226
	enum lgdt3306a_neverlock_status lockStatus;
1227 1228

	ret = lgdt3306a_read_reg(state, 0x0080, &val);
1229 1230
	if (ret)
		return ret;
1231
	lockStatus = (enum lgdt3306a_neverlock_status)(val & 0x03);
1232 1233 1234

	lg_dbg("NeverLock=%d", lockStatus);

1235
	return lockStatus;
1236 1237
}

1238
static int lgdt3306a_pre_monitoring(struct lgdt3306a_state *state)
1239 1240 1241 1242 1243
{
	u8 val = 0;
	int ret;
	u8 currChDiffACQ, snrRef, mainStrong, aiccrejStatus;

1244
	/* Channel variation */
1245
	ret = lgdt3306a_read_reg(state, 0x21bc, &currChDiffACQ);
1246 1247
	if (ret)
		return ret;
1248

1249
	/* SNR of Frame sync */
1250
	ret = lgdt3306a_read_reg(state, 0x21a1, &val);
1251 1252
	if (ret)
		return ret;
1253
	snrRef = val & 0x3f;
1254

1255
	/* Strong Main CIR */
1256
	ret = lgdt3306a_read_reg(state, 0x2199, &val);
1257 1258
	if (ret)
		return ret;
1259 1260 1261
	mainStrong = (val & 0x40) >> 6;

	ret = lgdt3306a_read_reg(state, 0x0090, &val);
1262 1263
	if (ret)
		return ret;
1264
	aiccrejStatus = (val & 0xf0) >> 4;
1265 1266 1267 1268

	lg_dbg("snrRef=%d mainStrong=%d aiccrejStatus=%d currChDiffACQ=0x%x\n",
		snrRef, mainStrong, aiccrejStatus, currChDiffACQ);

1269 1270 1271 1272
#if 0
	if ((mainStrong == 0) && (currChDiffACQ > 0x70)) /* Dynamic ghost exists */
#endif
	if (mainStrong == 0) {
1273
		ret = lgdt3306a_read_reg(state, 0x2135, &val);
1274 1275
		if (ret)
			return ret;
1276 1277
		val &= 0x0f;
		val |= 0xa0;
1278
		ret = lgdt3306a_write_reg(state, 0x2135, val);
1279 1280
		if (ret)
			return ret;
1281 1282

		ret = lgdt3306a_read_reg(state, 0x2141, &val);
1283 1284
		if (ret)
			return ret;
1285
		val &= 0x3f;
1286 1287
		val |= 0x80;
		ret = lgdt3306a_write_reg(state, 0x2141, val);
1288 1289
		if (ret)
			return ret;
1290 1291

		ret = lgdt3306a_write_reg(state, 0x2122, 0x70);
1292 1293
		if (ret)
			return ret;
1294
	} else { /* Weak ghost or static channel */
1295
		ret = lgdt3306a_read_reg(state, 0x2135, &val);
1296 1297
		if (ret)
			return ret;
1298
		val &= 0x0f;
1299 1300
		val |= 0x70;
		ret = lgdt3306a_write_reg(state, 0x2135, val);
1301 1302
		if (ret)
			return ret;
1303 1304

		ret = lgdt3306a_read_reg(state, 0x2141, &val);
1305 1306
		if (ret)
			return ret;
1307
		val &= 0x3f;
1308 1309
		val |= 0x40;
		ret = lgdt3306a_write_reg(state, 0x2141, val);
1310 1311
		if (ret)
			return ret;
1312 1313

		ret = lgdt3306a_write_reg(state, 0x2122, 0x40);
1314 1315
		if (ret)
			return ret;
1316
	}
1317
	return 0;
1318 1319
}

1320
static enum lgdt3306a_lock_status lgdt3306a_sync_lock_poll(struct lgdt3306a_state *state)
1321
{
1322
	enum lgdt3306a_lock_status syncLockStatus = LG3306_UNLOCK;
1323 1324 1325 1326 1327 1328 1329 1330 1331
	int	i;

	for (i = 0; i < 2; i++)	{
		msleep(30);

		syncLockStatus = lgdt3306a_check_lock_status(state, LG3306_SYNC_LOCK);

		if (syncLockStatus == LG3306_LOCK) {
			lg_dbg("locked(%d)\n", i);
1332
			return LG3306_LOCK;
1333 1334 1335
		}
	}
	lg_dbg("not locked\n");
1336
	return LG3306_UNLOCK;
1337 1338
}

1339
static enum lgdt3306a_lock_status lgdt3306a_fec_lock_poll(struct lgdt3306a_state *state)
1340
{
1341
	enum lgdt3306a_lock_status FECLockStatus = LG3306_UNLOCK;
1342 1343 1344 1345 1346 1347 1348 1349 1350
	int	i;

	for (i = 0; i < 2; i++)	{
		msleep(30);

		FECLockStatus = lgdt3306a_check_lock_status(state, LG3306_FEC_LOCK);

		if (FECLockStatus == LG3306_LOCK) {
			lg_dbg("locked(%d)\n", i);
1351
			return FECLockStatus;
1352 1353 1354
		}
	}
	lg_dbg("not locked\n");
1355
	return FECLockStatus;
1356 1357
}

1358
static enum lgdt3306a_neverlock_status lgdt3306a_neverlock_poll(struct lgdt3306a_state *state)
1359
{
1360
	enum lgdt3306a_neverlock_status NLLockStatus = LG3306_NL_FAIL;
1361 1362
	int	i;

1363
	for (i = 0; i < 5; i++) {
1364 1365 1366 1367 1368 1369
		msleep(30);

		NLLockStatus = lgdt3306a_check_neverlock_status(state);

		if (NLLockStatus == LG3306_NL_LOCK) {
			lg_dbg("NL_LOCK(%d)\n", i);
1370
			return NLLockStatus;
1371 1372 1373
		}
	}
	lg_dbg("NLLockStatus=%d\n", NLLockStatus);
1374
	return NLLockStatus;
1375 1376 1377 1378 1379 1380 1381
}

static u8 lgdt3306a_get_packet_error(struct lgdt3306a_state *state)
{
	u8 val;
	int ret;

1382
	ret = lgdt3306a_read_reg(state, 0x00fa, &val);
1383 1384
	if (ret)
		return ret;
1385

1386
	return val;
1387 1388 1389 1390
}

static u32 log10_x1000(u32 x)
{
1391 1392 1393
	static u32 valx_x10[]     = {  10,  11,  13,  15,  17,  20,  25,  33,  41,  50,  59,  73,  87,  100 };
	static u32 log10x_x1000[] = {   0,  41, 114, 176, 230, 301, 398, 518, 613, 699, 771, 863, 939, 1000 };
	static u32 nelems = sizeof(valx_x10)/sizeof(valx_x10[0]);
1394
	u32 diff_val, step_val, step_log10;
1395
	u32 log_val = 0;
1396
	u32 i;
1397

1398 1399
	if (x <= 0)
		return -1000000; /* signal error */
1400

1401 1402 1403
	if (x < 10) {
		while (x < 10) {
			x = x * 10;
1404 1405
			log_val--;
		}
1406 1407
	} else if (x == 10) {
		return 0; /* log(1)=0 */
1408
	} else {
1409 1410
		while (x >= 100) {
			x = x / 10;
1411 1412
			log_val++;
		}
1413
	}
1414 1415
	log_val *= 1000;

1416 1417
	if (x == 10) /* was our input an exact multiple of 10 */
		return log_val;	/* don't need to interpolate */
1418

1419 1420 1421 1422
	/* find our place on the log curve */
	for (i = 1; i < nelems; i++) {
		if (valx_x10[i] >= x)
			break;
1423
	}
1424 1425
	if (i == nelems)
		return log_val + log10x_x1000[i - 1];
1426

1427 1428 1429 1430 1431 1432 1433
	diff_val   = x - valx_x10[i-1];
	step_val   = valx_x10[i] - valx_x10[i - 1];
	step_log10 = log10x_x1000[i] - log10x_x1000[i - 1];

	/* do a linear interpolation to get in-between values */
	return log_val + log10x_x1000[i - 1] +
		((diff_val*step_log10) / step_val);
1434 1435 1436 1437
}

static u32 lgdt3306a_calculate_snr_x100(struct lgdt3306a_state *state)
{
1438 1439
	u32 mse; /* Mean-Square Error */
	u32 pwr; /* Constelation power */
1440 1441
	u32 snr_x100;

1442 1443 1444 1445
	mse = (read_reg(state, 0x00ec) << 8) |
	      (read_reg(state, 0x00ed));
	pwr = (read_reg(state, 0x00e8) << 8) |
	      (read_reg(state, 0x00e9));
1446 1447 1448 1449

	if (mse == 0) /* no signal */
		return 0;

1450
	snr_x100 = log10_x1000((pwr * 10000) / mse) - 3000;
1451 1452 1453 1454 1455
	lg_dbg("mse=%u, pwr=%u, snr_x100=%d\n", mse, pwr, snr_x100);

	return snr_x100;
}

1456
static enum lgdt3306a_lock_status lgdt3306a_vsb_lock_poll(struct lgdt3306a_state *state)
1457
{
1458 1459 1460
	u8 cnt = 0;
	u8 packet_error;
	u32 snr;
1461

1462
	while (1) {
1463 1464
		if (lgdt3306a_sync_lock_poll(state) == LG3306_UNLOCK) {
			lg_dbg("no sync lock!\n");
1465
			return LG3306_UNLOCK;
1466 1467
		} else {
			msleep(20);
1468 1469 1470
			ret = lgdt3306a_pre_monitoring(state);
			if (ret)
				return LG3306_UNLOCK;
1471 1472 1473

			packet_error = lgdt3306a_get_packet_error(state);
			snr = lgdt3306a_calculate_snr_x100(state);
1474 1475
			lg_dbg("cnt=%d errors=%d snr=%d\n",
			       cnt, packet_error, snr);
1476

1477
			if ((snr < 1500) || (packet_error >= 0xff))
1478
				cnt++;
1479 1480
			else
				return LG3306_LOCK;
1481

1482
			if (cnt >= 10) {
1483
				lg_dbg("not locked!\n");
1484
				return LG3306_UNLOCK;
1485 1486 1487
			}
		}
	}
1488
	return LG3306_UNLOCK;
1489 1490
}

1491
static enum lgdt3306a_lock_status lgdt3306a_qam_lock_poll(struct lgdt3306a_state *state)
1492 1493 1494 1495 1496
{
	u8 cnt = 0;
	u8 packet_error;
	u32	snr;

1497 1498
	while (1) {
		if (lgdt3306a_fec_lock_poll(state) == LG3306_UNLOCK) {
1499
			lg_dbg("no fec lock!\n");
1500
			return LG3306_UNLOCK;
1501 1502 1503 1504 1505
		} else {
			msleep(20);

			packet_error = lgdt3306a_get_packet_error(state);
			snr = lgdt3306a_calculate_snr_x100(state);
1506 1507
			lg_dbg("cnt=%d errors=%d snr=%d\n",
			       cnt, packet_error, snr);
1508

1509
			if ((snr < 1500) || (packet_error >= 0xff))
1510
				cnt++;
1511 1512
			else
				return LG3306_LOCK;
1513

1514
			if (cnt >= 10) {
1515
				lg_dbg("not locked!\n");
1516
				return LG3306_UNLOCK;
1517 1518 1519
			}
		}
	}
1520
	return LG3306_UNLOCK;
1521 1522 1523 1524 1525 1526
}

static int lgdt3306a_read_status(struct dvb_frontend *fe, fe_status_t *status)
{
	struct lgdt3306a_state *state = fe->demodulator_priv;
	u16 strength = 0;
1527 1528
	int ret = 0;

1529 1530
	if (fe->ops.tuner_ops.get_rf_strength) {
		ret = fe->ops.tuner_ops.get_rf_strength(fe, &strength);
1531
		if (ret == 0) {
1532 1533 1534 1535 1536 1537 1538
			lg_dbg("strength=%d\n", strength);
		} else {
			lg_dbg("fe->ops.tuner_ops.get_rf_strength() failed\n");
		}
	}

	*status = 0;
1539
	if (lgdt3306a_neverlock_poll(state) == LG3306_NL_LOCK) {
1540 1541 1542 1543 1544 1545
		*status |= FE_HAS_SIGNAL;
		*status |= FE_HAS_CARRIER;

		switch (state->current_modulation) {
		case QAM_256:
		case QAM_64:
1546
			if (lgdt3306a_qam_lock_poll(state) == LG3306_LOCK) {
1547 1548 1549 1550 1551 1552 1553
				*status |= FE_HAS_VITERBI;
				*status |= FE_HAS_SYNC;

				*status |= FE_HAS_LOCK;
			}
			break;
		case VSB_8:
1554
			if (lgdt3306a_vsb_lock_poll(state) == LG3306_LOCK) {
1555 1556 1557 1558 1559
				*status |= FE_HAS_VITERBI;
				*status |= FE_HAS_SYNC;

				*status |= FE_HAS_LOCK;

1560
				ret = lgdt3306a_monitor_vsb(state);
1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588
			}
			break;
		default:
			ret = -EINVAL;
		}
	}
	return ret;
}


static int lgdt3306a_read_snr(struct dvb_frontend *fe, u16 *snr)
{
	struct lgdt3306a_state *state = fe->demodulator_priv;

	state->snr = lgdt3306a_calculate_snr_x100(state);
	/* report SNR in dB * 10 */
	*snr = state->snr/10;

	return 0;
}

static int lgdt3306a_read_signal_strength(struct dvb_frontend *fe,
					 u16 *strength)
{
	/*
	 * Calculate some sort of "strength" from SNR
	 */
	struct lgdt3306a_state *state = fe->demodulator_priv;
1589
	u16 snr; /* snr_x10 */
1590
	int ret;
1591
	u32 ref_snr; /* snr*100 */
1592 1593 1594 1595 1596 1597
	u32 str;

	*strength = 0;

	switch (state->current_modulation) {
	case VSB_8:
1598
		 ref_snr = 1600; /* 16dB */
1599 1600
		 break;
	case QAM_64:
1601
		 ref_snr = 2200; /* 22dB */
1602 1603
		 break;
	case QAM_256:
1604
		 ref_snr = 2800; /* 28dB */
1605 1606 1607 1608 1609 1610 1611 1612 1613
		 break;
	default:
		return -EINVAL;
	}

	ret = fe->ops.read_snr(fe, &snr);
	if (lg_chkerr(ret))
		goto fail;

1614
	if (state->snr <= (ref_snr - 100))
1615
		str = 0;
1616 1617
	else if (state->snr <= ref_snr)
		str = (0xffff * 65) / 100; /* 65% */
1618 1619 1620
	else {
		str = state->snr - ref_snr;
		str /= 50;
1621 1622
		str += 78; /* 78%-100% */
		if (str > 100)
1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641
			str = 100;
		str = (0xffff * str) / 100;
	}
	*strength = (u16)str;
	lg_dbg("strength=%u\n", *strength);

fail:
	return ret;
}

/* ------------------------------------------------------------------------ */

static int lgdt3306a_read_ber(struct dvb_frontend *fe, u32 *ber)
{
	struct lgdt3306a_state *state = fe->demodulator_priv;
	u32 tmp;

	*ber = 0;
#if 1
1642 1643
	/* FGR - BUGBUG - I don't know what value is expected by dvb_core
	 * what is the scale of the value?? */
1644 1645 1646 1647
	tmp =              read_reg(state, 0x00fc); /* NBERVALUE[24-31] */
	tmp = (tmp << 8) | read_reg(state, 0x00fd); /* NBERVALUE[16-23] */
	tmp = (tmp << 8) | read_reg(state, 0x00fe); /* NBERVALUE[8-15] */
	tmp = (tmp << 8) | read_reg(state, 0x00ff); /* NBERVALUE[0-7] */
1648 1649 1650 1651 1652 1653 1654 1655 1656 1657
	*ber = tmp;
	lg_dbg("ber=%u\n", tmp);
#endif
	return 0;
}

static int lgdt3306a_read_ucblocks(struct dvb_frontend *fe, u32 *ucblocks)
{
	struct lgdt3306a_state *state = fe->demodulator_priv;

1658
	*ucblocks = 0;
1659
#if 1
1660 1661
	/* FGR - BUGBUG - I don't know what value is expected by dvb_core
	 * what happens when value wraps? */
1662
	*ucblocks = read_reg(state, 0x00f4); /* TPIFTPERRCNT[0-7] */
1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676
	lg_dbg("ucblocks=%u\n", *ucblocks);
#endif

	return 0;
}

static int lgdt3306a_tune(struct dvb_frontend *fe, bool re_tune, unsigned int mode_flags, unsigned int *delay, fe_status_t *status)
{
	int ret = 0;
	struct lgdt3306a_state *state = fe->demodulator_priv;

	lg_dbg("re_tune=%u\n", re_tune);

	if (re_tune) {
1677
		state->current_frequency = -1; /* force re-tune */
1678 1679
		ret = lgdt3306a_set_parameters(fe);
		if (ret != 0)
1680 1681 1682 1683 1684 1685 1686 1687 1688
			return ret;
	}
	*delay = 125;
	ret = lgdt3306a_read_status(fe, status);

	return ret;
}

static int lgdt3306a_get_tune_settings(struct dvb_frontend *fe,
1689 1690
				       struct dvb_frontend_tune_settings
				       *fe_tune_settings)
1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719
{
	fe_tune_settings->min_delay_ms = 100;
	lg_dbg("\n");
	return 0;
}

static int lgdt3306a_search(struct dvb_frontend *fe)
{
	fe_status_t status = 0;
	int i, ret;

	/* set frontend */
	ret = lgdt3306a_set_parameters(fe);
	if (ret)
		goto error;

	/* wait frontend lock */
	for (i = 20; i > 0; i--) {
		lg_dbg(": loop=%d\n", i);
		msleep(50);
		ret = lgdt3306a_read_status(fe, &status);
		if (ret)
			goto error;

		if (status & FE_HAS_LOCK)
			break;
	}

	/* check if we have a valid signal */
1720
	if (status & FE_HAS_LOCK)
1721
		return DVBFE_ALGO_SEARCH_SUCCESS;
1722
	else
1723 1724 1725 1726 1727 1728 1729 1730 1731 1732
		return DVBFE_ALGO_SEARCH_AGAIN;

error:
	lg_dbg("failed (%d)\n", ret);
	return DVBFE_ALGO_SEARCH_ERROR;
}

static void lgdt3306a_release(struct dvb_frontend *fe)
{
	struct lgdt3306a_state *state = fe->demodulator_priv;
1733

1734 1735 1736 1737 1738 1739 1740
	lg_dbg("\n");
	kfree(state);
}

static struct dvb_frontend_ops lgdt3306a_ops;

struct dvb_frontend *lgdt3306a_attach(const struct lgdt3306a_config *config,
1741
				      struct i2c_adapter *i2c_adap)
1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762
{
	struct lgdt3306a_state *state = NULL;
	int ret;
	u8 val;

	lg_dbg("(%d-%04x)\n",
	       i2c_adap ? i2c_adapter_id(i2c_adap) : 0,
	       config ? config->i2c_addr : 0);

	state = kzalloc(sizeof(struct lgdt3306a_state), GFP_KERNEL);
	if (state == NULL)
		goto fail;

	state->cfg = config;
	state->i2c_adap = i2c_adap;

	memcpy(&state->frontend.ops, &lgdt3306a_ops,
	       sizeof(struct dvb_frontend_ops));
	state->frontend.demodulator_priv = state;

	/* verify that we're talking to a lg3306a */
1763 1764
	/* FGR - NOTE - there is no obvious ChipId to check; we check
	 * some "known" bits after reset, but it's still just a guess */
1765 1766 1767
	ret = lgdt3306a_read_reg(state, 0x0000, &val);
	if (lg_chkerr(ret))
		goto fail;
1768
	if ((val & 0x74) != 0x74) {
1769
		lg_warn("expected 0x74, got 0x%x\n", (val & 0x74));
1770 1771 1772
#if 0
		goto fail;	/* BUGBUG - re-enable when we know this is right */
#endif
1773 1774 1775 1776
	}
	ret = lgdt3306a_read_reg(state, 0x0001, &val);
	if (lg_chkerr(ret))
		goto fail;
1777 1778
	if ((val & 0xf6) != 0xc6) {
		lg_warn("expected 0xc6, got 0x%x\n", (val & 0xf6));
1779 1780 1781
#if 0
		goto fail;	/* BUGBUG - re-enable when we know this is right */
#endif
1782 1783 1784 1785
	}
	ret = lgdt3306a_read_reg(state, 0x0002, &val);
	if (lg_chkerr(ret))
		goto fail;
1786
	if ((val & 0x73) != 0x03) {
1787
		lg_warn("expected 0x03, got 0x%x\n", (val & 0x73));
1788 1789 1790
#if 0
		goto fail;	/* BUGBUG - re-enable when we know this is right */
#endif
1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804
	}

	state->current_frequency = -1;
	state->current_modulation = -1;

	lgdt3306a_sleep(state);

	return &state->frontend;

fail:
	lg_warn("unable to detect LGDT3306A hardware\n");
	kfree(state);
	return NULL;
}
1805
EXPORT_SYMBOL(lgdt3306a_attach);
1806 1807 1808 1809

#ifdef DBG_DUMP

static const short regtab[] = {
1810 1811 1812 1813 1814 1815 1816 1817 1818 1819
	0x0000, /* SOFTRSTB 1'b1 1'b1 1'b1 ADCPDB 1'b1 PLLPDB GBBPDB 11111111 */
	0x0001, /* 1'b1 1'b1 1'b0 1'b0 AUTORPTRS */
	0x0002, /* NI2CRPTEN 1'b0 1'b0 1'b0 SPECINVAUT */
	0x0003, /* AGCRFOUT */
	0x0004, /* ADCSEL1V ADCCNT ADCCNF ADCCNS ADCCLKPLL */
	0x0005, /* PLLINDIVSE */
	0x0006, /* PLLCTRL[7:0] 11100001 */
	0x0007, /* SYSINITWAITTIME[7:0] (msec) 00001000 */
	0x0008, /* STDOPMODE[7:0] 10000000 */
	0x0009, /* 1'b0 1'b0 1'b0 STDOPDETTMODE[2:0] STDOPDETCMODE[1:0] 00011110 */
1820 1821 1822 1823 1824
	0x000a, /* DAFTEN 1'b1 x x SCSYSLOCK */
	0x000b, /* SCSYSLOCKCHKTIME[7:0] (10msec) 01100100 */
	0x000d, /* x SAMPLING4 */
	0x000e, /* SAMFREQ[15:8] 00000000 */
	0x000f, /* SAMFREQ[7:0] 00000000 */
1825 1826 1827 1828 1829 1830 1831 1832 1833 1834
	0x0010, /* IFFREQ[15:8] 01100000 */
	0x0011, /* IFFREQ[7:0] 00000000 */
	0x0012, /* AGCEN AGCREFMO */
	0x0013, /* AGCRFFIXB AGCIFFIXB AGCLOCKDETRNGSEL[1:0] 1'b1 1'b0 1'b0 1'b0 11101000 */
	0x0014, /* AGCFIXVALUE[7:0] 01111111 */
	0x0015, /* AGCREF[15:8] 00001010 */
	0x0016, /* AGCREF[7:0] 11100100 */
	0x0017, /* AGCDELAY[7:0] 00100000 */
	0x0018, /* AGCRFBW[3:0] AGCIFBW[3:0] 10001000 */
	0x0019, /* AGCUDOUTMODE[1:0] AGCUDCTRLLEN[1:0] AGCUDCTRL */
1835 1836 1837 1838
	0x001c, /* 1'b1 PFEN MFEN AICCVSYNC */
	0x001d, /* 1'b0 1'b1 1'b0 1'b1 AICCVSYNC */
	0x001e, /* AICCALPHA[3:0] 1'b1 1'b0 1'b1 1'b0 01111010 */
	0x001f, /* AICCDETTH[19:16] AICCOFFTH[19:16] 00000000 */
1839 1840 1841 1842 1843 1844 1845 1846 1847 1848
	0x0020, /* AICCDETTH[15:8] 01111100 */
	0x0021, /* AICCDETTH[7:0] 00000000 */
	0x0022, /* AICCOFFTH[15:8] 00000101 */
	0x0023, /* AICCOFFTH[7:0] 11100000 */
	0x0024, /* AICCOPMODE3[1:0] AICCOPMODE2[1:0] AICCOPMODE1[1:0] AICCOPMODE0[1:0] 00000000 */
	0x0025, /* AICCFIXFREQ3[23:16] 00000000 */
	0x0026, /* AICCFIXFREQ3[15:8] 00000000 */
	0x0027, /* AICCFIXFREQ3[7:0] 00000000 */
	0x0028, /* AICCFIXFREQ2[23:16] 00000000 */
	0x0029, /* AICCFIXFREQ2[15:8] 00000000 */
1849 1850 1851 1852 1853 1854
	0x002a, /* AICCFIXFREQ2[7:0] 00000000 */
	0x002b, /* AICCFIXFREQ1[23:16] 00000000 */
	0x002c, /* AICCFIXFREQ1[15:8] 00000000 */
	0x002d, /* AICCFIXFREQ1[7:0] 00000000 */
	0x002e, /* AICCFIXFREQ0[23:16] 00000000 */
	0x002f, /* AICCFIXFREQ0[15:8] 00000000 */
1855 1856 1857 1858 1859 1860 1861 1862 1863
	0x0030, /* AICCFIXFREQ0[7:0] 00000000 */
	0x0031, /* 1'b0 1'b1 1'b0 1'b0 x DAGC1STER */
	0x0032, /* DAGC1STEN DAGC1STER */
	0x0033, /* DAGC1STREF[15:8] 00001010 */
	0x0034, /* DAGC1STREF[7:0] 11100100 */
	0x0035, /* DAGC2NDE */
	0x0036, /* DAGC2NDREF[15:8] 00001010 */
	0x0037, /* DAGC2NDREF[7:0] 10000000 */
	0x0038, /* DAGC2NDLOCKDETRNGSEL[1:0] */
1864
	0x003d, /* 1'b1 SAMGEARS */
1865 1866 1867 1868 1869 1870 1871 1872
	0x0040, /* SAMLFGMA */
	0x0041, /* SAMLFBWM */
	0x0044, /* 1'b1 CRGEARSHE */
	0x0045, /* CRLFGMAN */
	0x0046, /* CFLFBWMA */
	0x0047, /* CRLFGMAN */
	0x0048, /* x x x x CRLFGSTEP_VS[3:0] xxxx1001 */
	0x0049, /* CRLFBWMA */
1873
	0x004a, /* CRLFBWMA */
1874 1875 1876 1877 1878 1879 1880 1881 1882
	0x0050, /* 1'b0 1'b1 1'b1 1'b0 MSECALCDA */
	0x0070, /* TPOUTEN TPIFEN TPCLKOUTE */
	0x0071, /* TPSENB TPSSOPBITE */
	0x0073, /* TP47HINS x x CHBERINT PERMODE[1:0] PERINT[1:0] 1xx11100 */
	0x0075, /* x x x x x IQSWAPCTRL[2:0] xxxxx000 */
	0x0076, /* NBERCON NBERST NBERPOL NBERWSYN */
	0x0077, /* x NBERLOSTTH[2:0] NBERACQTH[3:0] x0000000 */
	0x0078, /* NBERPOLY[31:24] 00000000 */
	0x0079, /* NBERPOLY[23:16] 00000000 */
1883 1884 1885 1886 1887 1888
	0x007a, /* NBERPOLY[15:8] 00000000 */
	0x007b, /* NBERPOLY[7:0] 00000000 */
	0x007c, /* NBERPED[31:24] 00000000 */
	0x007d, /* NBERPED[23:16] 00000000 */
	0x007e, /* NBERPED[15:8] 00000000 */
	0x007f, /* NBERPED[7:0] 00000000 */
1889 1890 1891 1892
	0x0080, /* x AGCLOCK DAGCLOCK SYSLOCK x x NEVERLOCK[1:0] */
	0x0085, /* SPECINVST */
	0x0088, /* SYSLOCKTIME[15:8] */
	0x0089, /* SYSLOCKTIME[7:0] */
1893 1894 1895 1896
	0x008c, /* FECLOCKTIME[15:8] */
	0x008d, /* FECLOCKTIME[7:0] */
	0x008e, /* AGCACCOUT[15:8] */
	0x008f, /* AGCACCOUT[7:0] */
1897 1898
	0x0090, /* AICCREJSTATUS[3:0] AICCREJBUSY[3:0] */
	0x0091, /* AICCVSYNC */
1899 1900 1901 1902 1903 1904
	0x009c, /* CARRFREQOFFSET[15:8] */
	0x009d, /* CARRFREQOFFSET[7:0] */
	0x00a1, /* SAMFREQOFFSET[23:16] */
	0x00a2, /* SAMFREQOFFSET[15:8] */
	0x00a3, /* SAMFREQOFFSET[7:0] */
	0x00a6, /* SYNCLOCK SYNCLOCKH */
1905
#if 0 /* covered elsewhere */
1906 1907 1908 1909 1910 1911 1912 1913
	0x00e8, /* CONSTPWR[15:8] */
	0x00e9, /* CONSTPWR[7:0] */
	0x00ea, /* BMSE[15:8] */
	0x00eb, /* BMSE[7:0] */
	0x00ec, /* MSE[15:8] */
	0x00ed, /* MSE[7:0] */
	0x00ee, /* CONSTI[7:0] */
	0x00ef, /* CONSTQ[7:0] */
1914
#endif
1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926
	0x00f4, /* TPIFTPERRCNT[7:0] */
	0x00f5, /* TPCORREC */
	0x00f6, /* VBBER[15:8] */
	0x00f7, /* VBBER[7:0] */
	0x00f8, /* VABER[15:8] */
	0x00f9, /* VABER[7:0] */
	0x00fa, /* TPERRCNT[7:0] */
	0x00fb, /* NBERLOCK x x x x x x x */
	0x00fc, /* NBERVALUE[31:24] */
	0x00fd, /* NBERVALUE[23:16] */
	0x00fe, /* NBERVALUE[15:8] */
	0x00ff, /* NBERVALUE[7:0] */
1927 1928 1929
	0x1000, /* 1'b0 WODAGCOU */
	0x1005, /* x x 1'b1 1'b1 x SRD_Q_QM */
	0x1009, /* SRDWAITTIME[7:0] (10msec) 00100011 */
1930 1931
	0x100a, /* SRDWAITTIME_CQS[7:0] (msec) 01100100 */
	0x101a, /* x 1'b1 1'b0 1'b0 x QMDQAMMODE[2:0] x100x010 */
1932
	0x1036, /* 1'b0 1'b1 1'b0 1'b0 SAMGSEND_CQS[3:0] 01001110 */
1933 1934 1935 1936 1937
	0x103c, /* SAMGSAUTOSTL_V[3:0] SAMGSAUTOEDL_V[3:0] 01000110 */
	0x103d, /* 1'b1 1'b1 SAMCNORMBP_V[1:0] 1'b0 1'b0 SAMMODESEL_V[1:0] 11100001 */
	0x103f, /* SAMZTEDSE */
	0x105d, /* EQSTATUSE */
	0x105f, /* x PMAPG2_V[2:0] x DMAPG2_V[2:0] x001x011 */
1938 1939 1940 1941 1942
	0x1060, /* 1'b1 EQSTATUSE */
	0x1061, /* CRMAPBWSTL_V[3:0] CRMAPBWEDL_V[3:0] 00000100 */
	0x1065, /* 1'b0 x CRMODE_V[1:0] 1'b1 x 1'b1 x 0x111x1x */
	0x1066, /* 1'b0 1'b0 1'b1 1'b0 1'b1 PNBOOSTSE */
	0x1068, /* CREPHNGAIN2_V[3:0] CREPHNPBW_V[3:0] 10010001 */
1943 1944
	0x106e, /* x x x x x CREPHNEN_ */
	0x106f, /* CREPHNTH_V[7:0] 00010101 */
1945 1946 1947 1948 1949
	0x1072, /* CRSWEEPN */
	0x1073, /* CRPGAIN_V[3:0] x x 1'b1 1'b1 1001xx11 */
	0x1074, /* CRPBW_V[3:0] x x 1'b1 1'b1 0001xx11 */
	0x1080, /* DAFTSTATUS[1:0] x x x x x x */
	0x1081, /* SRDSTATUS[1:0] x x x x x SRDLOCK */
1950 1951
	0x10a9, /* EQSTATUS_CQS[1:0] x x x x x x */
	0x10b7, /* EQSTATUS_V[1:0] x x x x x x */
1952
#if 0 /* SMART_ANT */
1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001
	0x1f00, /* MODEDETE */
	0x1f01, /* x x x x x x x SFNRST xxxxxxx0 */
	0x1f03, /* NUMOFANT[7:0] 10000000 */
	0x1f04, /* x SELMASK[6:0] x0000000 */
	0x1f05, /* x SETMASK[6:0] x0000000 */
	0x1f06, /* x TXDATA[6:0] x0000000 */
	0x1f07, /* x CHNUMBER[6:0] x0000000 */
	0x1f09, /* AGCTIME[23:16] 10011000 */
	0x1f0a, /* AGCTIME[15:8] 10010110 */
	0x1f0b, /* AGCTIME[7:0] 10000000 */
	0x1f0c, /* ANTTIME[31:24] 00000000 */
	0x1f0d, /* ANTTIME[23:16] 00000011 */
	0x1f0e, /* ANTTIME[15:8] 10010000 */
	0x1f0f, /* ANTTIME[7:0] 10010000 */
	0x1f11, /* SYNCTIME[23:16] 10011000 */
	0x1f12, /* SYNCTIME[15:8] 10010110 */
	0x1f13, /* SYNCTIME[7:0] 10000000 */
	0x1f14, /* SNRTIME[31:24] 00000001 */
	0x1f15, /* SNRTIME[23:16] 01111101 */
	0x1f16, /* SNRTIME[15:8] 01111000 */
	0x1f17, /* SNRTIME[7:0] 01000000 */
	0x1f19, /* FECTIME[23:16] 00000000 */
	0x1f1a, /* FECTIME[15:8] 01110010 */
	0x1f1b, /* FECTIME[7:0] 01110000 */
	0x1f1d, /* FECTHD[7:0] 00000011 */
	0x1f1f, /* SNRTHD[23:16] 00001000 */
	0x1f20, /* SNRTHD[15:8] 01111111 */
	0x1f21, /* SNRTHD[7:0] 10000101 */
	0x1f80, /* IRQFLG x x SFSDRFLG MODEBFLG SAVEFLG SCANFLG TRACKFLG */
	0x1f81, /* x SYNCCON SNRCON FECCON x STDBUSY SYNCRST AGCFZCO */
	0x1f82, /* x x x SCANOPCD[4:0] */
	0x1f83, /* x x x x MAINOPCD[3:0] */
	0x1f84, /* x x RXDATA[13:8] */
	0x1f85, /* RXDATA[7:0] */
	0x1f86, /* x x SDTDATA[13:8] */
	0x1f87, /* SDTDATA[7:0] */
	0x1f89, /* ANTSNR[23:16] */
	0x1f8a, /* ANTSNR[15:8] */
	0x1f8b, /* ANTSNR[7:0] */
	0x1f8c, /* x x x x ANTFEC[13:8] */
	0x1f8d, /* ANTFEC[7:0] */
	0x1f8e, /* MAXCNT[7:0] */
	0x1f8f, /* SCANCNT[7:0] */
	0x1f91, /* MAXPW[23:16] */
	0x1f92, /* MAXPW[15:8] */
	0x1f93, /* MAXPW[7:0] */
	0x1f95, /* CURPWMSE[23:16] */
	0x1f96, /* CURPWMSE[15:8] */
	0x1f97, /* CURPWMSE[7:0] */
2002
#endif /* SMART_ANT */
2003 2004
	0x211f, /* 1'b1 1'b1 1'b1 CIRQEN x x 1'b0 1'b0 1111xx00 */
	0x212a, /* EQAUTOST */
2005
	0x2122, /* CHFAST[7:0] 01100000 */
2006 2007 2008
	0x212b, /* FFFSTEP_V[3:0] x FBFSTEP_V[2:0] 0001x001 */
	0x212c, /* PHDEROTBWSEL[3:0] 1'b1 1'b1 1'b1 1'b0 10001110 */
	0x212d, /* 1'b1 1'b1 1'b1 1'b1 x x TPIFLOCKS */
2009 2010 2011 2012 2013
	0x2135, /* DYNTRACKFDEQ[3:0] x 1'b0 1'b0 1'b0 1010x000 */
	0x2141, /* TRMODE[1:0] 1'b1 1'b1 1'b0 1'b1 1'b1 1'b1 01110111 */
	0x2162, /* AICCCTRLE */
	0x2173, /* PHNCNFCNT[7:0] 00000100 */
	0x2179, /* 1'b0 1'b0 1'b0 1'b1 x BADSINGLEDYNTRACKFBF[2:0] 0001x001 */
2014 2015 2016
	0x217a, /* 1'b0 1'b0 1'b0 1'b1 x BADSLOWSINGLEDYNTRACKFBF[2:0] 0001x001 */
	0x217e, /* CNFCNTTPIF[7:0] 00001000 */
	0x217f, /* TPERRCNTTPIF[7:0] 00000001 */
2017 2018 2019 2020 2021
	0x2180, /* x x x x x x FBDLYCIR[9:8] */
	0x2181, /* FBDLYCIR[7:0] */
	0x2185, /* MAXPWRMAIN[7:0] */
	0x2191, /* NCOMBDET x x x x x x x */
	0x2199, /* x MAINSTRON */
2022 2023
	0x219a, /* FFFEQSTEPOUT_V[3:0] FBFSTEPOUT_V[2:0] */
	0x21a1, /* x x SNRREF[5:0] */
2024 2025 2026 2027
	0x2845, /* 1'b0 1'b1 x x FFFSTEP_CQS[1:0] FFFCENTERTAP[1:0] 01xx1110 */
	0x2846, /* 1'b0 x 1'b0 1'b1 FBFSTEP_CQS[1:0] 1'b1 1'b0 0x011110 */
	0x2847, /* ENNOSIGDE */
	0x2849, /* 1'b1 1'b1 NOUSENOSI */
2028
	0x284a, /* EQINITWAITTIME[7:0] 01100100 */
2029 2030 2031 2032
	0x3000, /* 1'b1 1'b1 1'b1 x x x 1'b0 RPTRSTM */
	0x3001, /* RPTRSTWAITTIME[7:0] (100msec) 00110010 */
	0x3031, /* FRAMELOC */
	0x3032, /* 1'b1 1'b0 1'b0 1'b0 x x FRAMELOCKMODE_CQS[1:0] 1000xx11 */
2033 2034
	0x30a9, /* VDLOCK_Q FRAMELOCK */
	0x30aa, /* MPEGLOCK */
2035 2036
};

2037
#define numDumpRegs (sizeof(regtab)/sizeof(regtab[0]))
2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050
static u8 regval1[numDumpRegs] = {0, };
static u8 regval2[numDumpRegs] = {0, };

static void lgdt3306a_DumpAllRegs(struct lgdt3306a_state *state)
{
		memset(regval2, 0xff, sizeof(regval2));
		lgdt3306a_DumpRegs(state);
}

static void lgdt3306a_DumpRegs(struct lgdt3306a_state *state)
{
	int i;
	int sav_debug = debug;
2051

2052 2053
	if ((debug & DBG_DUMP) == 0)
		return;
M
Michael Ira Krufky 已提交
2054
	debug &= ~DBG_REG; /* suppress DBG_REG during reg dump */
2055 2056 2057

	lg_info("\n");

2058
	for (i = 0; i < numDumpRegs; i++) {
2059
		lgdt3306a_read_reg(state, regtab[i], &regval1[i]);
2060 2061 2062
		if (regval1[i] != regval2[i]) {
			lg_info(" %04X = %02X\n", regtab[i], regval1[i]);
				regval2[i] = regval1[i];
2063 2064 2065 2066
		}
	}
	debug = sav_debug;
}
2067
#endif /* DBG_DUMP */
2068 2069 2070 2071 2072 2073 2074



static struct dvb_frontend_ops lgdt3306a_ops = {
	.delsys = { SYS_ATSC, SYS_DVBC_ANNEX_B },
	.info = {
		.name = "LG Electronics LGDT3306A VSB/QAM Frontend",
2075 2076 2077
#if 0
		.type               = FE_ATSC,
#endif
2078
		.frequency_min      = 54000000,
2079
		.frequency_max      = 858000000,
2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111
		.frequency_stepsize = 62500,
		.caps = FE_CAN_QAM_64 | FE_CAN_QAM_256 | FE_CAN_8VSB
	},
	.i2c_gate_ctrl        = lgdt3306a_i2c_gate_ctrl,
	.init                 = lgdt3306a_init,
	.sleep                = lgdt3306a_fe_sleep,
	/* if this is set, it overrides the default swzigzag */
	.tune                 = lgdt3306a_tune,
	.set_frontend         = lgdt3306a_set_parameters,
	.get_frontend         = lgdt3306a_get_frontend,
	.get_frontend_algo    = lgdt3306a_get_frontend_algo,
	.get_tune_settings    = lgdt3306a_get_tune_settings,
	.read_status          = lgdt3306a_read_status,
	.read_ber             = lgdt3306a_read_ber,
	.read_signal_strength = lgdt3306a_read_signal_strength,
	.read_snr             = lgdt3306a_read_snr,
	.read_ucblocks        = lgdt3306a_read_ucblocks,
	.release              = lgdt3306a_release,
	.ts_bus_ctrl          = lgdt3306a_ts_bus_ctrl,
	.search               = lgdt3306a_search,
};

MODULE_DESCRIPTION("LG Electronics LGDT3306A ATSC/QAM-B Demodulator Driver");
MODULE_AUTHOR("Fred Richter <frichter@hauppauge.com>");
MODULE_LICENSE("GPL");
MODULE_VERSION("0.2");

/*
 * Local variables:
 * c-basic-offset: 8
 * End:
 */