drxk_hard.c 161.5 KB
Newer Older
R
Ralph Metzler 已提交
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 36 37 38 39
/*
 * drxk_hard: DRX-K DVB-C/T demodulator driver
 *
 * Copyright (C) 2010-2011 Digital Devices GmbH
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * version 2 only, as published by the Free Software Foundation.
 *
 *
 * 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., 51 Franklin Street, Fifth Floor, Boston, MA
 * 02110-1301, USA
 * Or, point your browser to http://www.gnu.org/copyleft/gpl.html
 */

#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/moduleparam.h>
#include <linux/init.h>
#include <linux/delay.h>
#include <linux/firmware.h>
#include <linux/i2c.h>
#include <linux/version.h>
#include <asm/div64.h>

#include "dvb_frontend.h"
#include "drxk.h"
#include "drxk_hard.h"

static int PowerDownDVBT(struct drxk_state *state, bool setPowerMode);
static int PowerDownQAM(struct drxk_state *state);
40 41 42 43 44
static int SetDVBTStandard(struct drxk_state *state,
			   enum OperationMode oMode);
static int SetQAMStandard(struct drxk_state *state,
			  enum OperationMode oMode);
static int SetQAM(struct drxk_state *state, u16 IntermediateFreqkHz,
R
Ralph Metzler 已提交
45
		  s32 tunerFreqOffset);
46 47
static int SetDVBTStandard(struct drxk_state *state,
			   enum OperationMode oMode);
R
Ralph Metzler 已提交
48
static int DVBTStart(struct drxk_state *state);
49 50
static int SetDVBT(struct drxk_state *state, u16 IntermediateFreqkHz,
		   s32 tunerFreqOffset);
R
Ralph Metzler 已提交
51 52 53 54 55 56 57 58 59 60 61 62 63
static int GetQAMLockStatus(struct drxk_state *state, u32 *pLockStatus);
static int GetDVBTLockStatus(struct drxk_state *state, u32 *pLockStatus);
static int SwitchAntennaToQAM(struct drxk_state *state);
static int SwitchAntennaToDVBT(struct drxk_state *state);

static bool IsDVBT(struct drxk_state *state)
{
	return state->m_OperationMode == OM_DVBT;
}

static bool IsQAM(struct drxk_state *state)
{
	return state->m_OperationMode == OM_QAM_ITU_A ||
64 65
	    state->m_OperationMode == OM_QAM_ITU_B ||
	    state->m_OperationMode == OM_QAM_ITU_C;
R
Ralph Metzler 已提交
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 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
}

bool IsA1WithPatchCode(struct drxk_state *state)
{
	return state->m_DRXK_A1_PATCH_CODE;
}

bool IsA1WithRomCode(struct drxk_state *state)
{
	return state->m_DRXK_A1_ROM_CODE;
}

#define NOA1ROM 0

#define DRXDAP_FASI_SHORT_FORMAT(addr) (((addr) & 0xFC30FF80) == 0)
#define DRXDAP_FASI_LONG_FORMAT(addr)  (((addr) & 0xFC30FF80) != 0)

#define DEFAULT_MER_83  165
#define DEFAULT_MER_93  250

#ifndef DRXK_MPEG_SERIAL_OUTPUT_PIN_DRIVE_STRENGTH
#define DRXK_MPEG_SERIAL_OUTPUT_PIN_DRIVE_STRENGTH (0x02)
#endif

#ifndef DRXK_MPEG_PARALLEL_OUTPUT_PIN_DRIVE_STRENGTH
#define DRXK_MPEG_PARALLEL_OUTPUT_PIN_DRIVE_STRENGTH (0x03)
#endif

#ifndef DRXK_MPEG_OUTPUT_CLK_DRIVE_STRENGTH
#define DRXK_MPEG_OUTPUT_CLK_DRIVE_STRENGTH (0x06)
#endif

#define DEFAULT_DRXK_MPEG_LOCK_TIMEOUT 700
#define DEFAULT_DRXK_DEMOD_LOCK_TIMEOUT 500

#ifndef DRXK_KI_RAGC_ATV
#define DRXK_KI_RAGC_ATV   4
#endif
#ifndef DRXK_KI_IAGC_ATV
#define DRXK_KI_IAGC_ATV   6
#endif
#ifndef DRXK_KI_DAGC_ATV
#define DRXK_KI_DAGC_ATV   7
#endif

#ifndef DRXK_KI_RAGC_QAM
#define DRXK_KI_RAGC_QAM   3
#endif
#ifndef DRXK_KI_IAGC_QAM
#define DRXK_KI_IAGC_QAM   4
#endif
#ifndef DRXK_KI_DAGC_QAM
#define DRXK_KI_DAGC_QAM   7
#endif
#ifndef DRXK_KI_RAGC_DVBT
#define DRXK_KI_RAGC_DVBT  (IsA1WithPatchCode(state) ? 3 : 2)
#endif
#ifndef DRXK_KI_IAGC_DVBT
#define DRXK_KI_IAGC_DVBT  (IsA1WithPatchCode(state) ? 4 : 2)
#endif
#ifndef DRXK_KI_DAGC_DVBT
#define DRXK_KI_DAGC_DVBT  (IsA1WithPatchCode(state) ? 10 : 7)
#endif

#ifndef DRXK_AGC_DAC_OFFSET
#define DRXK_AGC_DAC_OFFSET (0x800)
#endif

#ifndef DRXK_BANDWIDTH_8MHZ_IN_HZ
#define DRXK_BANDWIDTH_8MHZ_IN_HZ  (0x8B8249L)
#endif

#ifndef DRXK_BANDWIDTH_7MHZ_IN_HZ
#define DRXK_BANDWIDTH_7MHZ_IN_HZ  (0x7A1200L)
#endif

#ifndef DRXK_BANDWIDTH_6MHZ_IN_HZ
#define DRXK_BANDWIDTH_6MHZ_IN_HZ  (0x68A1B6L)
#endif

#ifndef DRXK_QAM_SYMBOLRATE_MAX
#define DRXK_QAM_SYMBOLRATE_MAX         (7233000)
#endif

#define DRXK_BL_ROM_OFFSET_TAPS_DVBT    56
#define DRXK_BL_ROM_OFFSET_TAPS_ITU_A   64
#define DRXK_BL_ROM_OFFSET_TAPS_ITU_C   0x5FE0
#define DRXK_BL_ROM_OFFSET_TAPS_BG      24
#define DRXK_BL_ROM_OFFSET_TAPS_DKILLP  32
#define DRXK_BL_ROM_OFFSET_TAPS_NTSC    40
#define DRXK_BL_ROM_OFFSET_TAPS_FM      48
#define DRXK_BL_ROM_OFFSET_UCODE        0

#define DRXK_BLC_TIMEOUT                100

#define DRXK_BLCC_NR_ELEMENTS_TAPS      2
#define DRXK_BLCC_NR_ELEMENTS_UCODE     6

#define DRXK_BLDC_NR_ELEMENTS_TAPS      28

#ifndef DRXK_OFDM_NE_NOTCH_WIDTH
#define DRXK_OFDM_NE_NOTCH_WIDTH             (4)
#endif

#define DRXK_QAM_SL_SIG_POWER_QAM16       (40960)
#define DRXK_QAM_SL_SIG_POWER_QAM32       (20480)
#define DRXK_QAM_SL_SIG_POWER_QAM64       (43008)
#define DRXK_QAM_SL_SIG_POWER_QAM128      (20992)
#define DRXK_QAM_SL_SIG_POWER_QAM256      (43520)

176 177 178 179 180 181 182 183 184 185
static unsigned int debug;
module_param(debug, int, 0644);
MODULE_PARM_DESC(debug, "enable debug messages");

#define dprintk(level, fmt, arg...) do {			\
if (debug >= level)						\
	printk(KERN_DEBUG "drxk: %s" fmt, __func__, ## arg);	\
} while (0)


186
static inline u32 MulDiv32(u32 a, u32 b, u32 c)
R
Ralph Metzler 已提交
187 188 189
{
	u64 tmp64;

190
	tmp64 = (u64) a * (u64) b;
R
Ralph Metzler 已提交
191 192 193 194 195 196 197 198 199 200 201
	do_div(tmp64, c);

	return (u32) tmp64;
}

inline u32 Frac28a(u32 a, u32 c)
{
	int i = 0;
	u32 Q1 = 0;
	u32 R0 = 0;

202 203 204
	R0 = (a % c) << 4;	/* 32-28 == 4 shifts possible at max */
	Q1 = a / c;		/* integer part, only the 4 least significant bits
				   will be visible in the result */
R
Ralph Metzler 已提交
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221

	/* division using radix 16, 7 nibbles in the result */
	for (i = 0; i < 7; i++) {
		Q1 = (Q1 << 4) | (R0 / c);
		R0 = (R0 % c) << 4;
	}
	/* rounding */
	if ((R0 >> 3) >= c)
		Q1++;

	return Q1;
}

static u32 Log10Times100(u32 x)
{
	static const u8 scale = 15;
	static const u8 indexWidth = 5;
222
	u8 i = 0;
R
Ralph Metzler 已提交
223 224 225 226 227
	u32 y = 0;
	u32 d = 0;
	u32 k = 0;
	u32 r = 0;
	/*
228 229 230
	   log2lut[n] = (1<<scale) * 200 * log2(1.0 + ((1.0/(1<<INDEXWIDTH)) * n))
	   0 <= n < ((1<<INDEXWIDTH)+1)
	 */
R
Ralph Metzler 已提交
231 232

	static const u32 log2lut[] = {
233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265
		0,		/* 0.000000 */
		290941,		/* 290941.300628 */
		573196,		/* 573196.476418 */
		847269,		/* 847269.179851 */
		1113620,	/* 1113620.489452 */
		1372674,	/* 1372673.576986 */
		1624818,	/* 1624817.752104 */
		1870412,	/* 1870411.981536 */
		2109788,	/* 2109787.962654 */
		2343253,	/* 2343252.817465 */
		2571091,	/* 2571091.461923 */
		2793569,	/* 2793568.696416 */
		3010931,	/* 3010931.055901 */
		3223408,	/* 3223408.452106 */
		3431216,	/* 3431215.635215 */
		3634553,	/* 3634553.498355 */
		3833610,	/* 3833610.244726 */
		4028562,	/* 4028562.434393 */
		4219576,	/* 4219575.925308 */
		4406807,	/* 4406806.721144 */
		4590402,	/* 4590401.736809 */
		4770499,	/* 4770499.491025 */
		4947231,	/* 4947230.734179 */
		5120719,	/* 5120719.018555 */
		5291081,	/* 5291081.217197 */
		5458428,	/* 5458427.996830 */
		5622864,	/* 5622864.249668 */
		5784489,	/* 5784489.488298 */
		5943398,	/* 5943398.207380 */
		6099680,	/* 6099680.215452 */
		6253421,	/* 6253420.939751 */
		6404702,	/* 6404701.706649 */
		6553600,	/* 6553600.000000 */
R
Ralph Metzler 已提交
266 267 268 269
	};


	if (x == 0)
270
		return 0;
R
Ralph Metzler 已提交
271 272 273 274 275

	/* Scale x (normalize) */
	/* computing y in log(x/y) = log(x) - log(y) */
	if ((x & ((0xffffffff) << (scale + 1))) == 0) {
		for (k = scale; k > 0; k--) {
276
			if (x & (((u32) 1) << scale))
R
Ralph Metzler 已提交
277 278 279 280
				break;
			x <<= 1;
		}
	} else {
281 282
		for (k = scale; k < 31; k++) {
			if ((x & (((u32) (-1)) << (scale + 1))) == 0)
R
Ralph Metzler 已提交
283 284
				break;
			x >>= 1;
285
		}
R
Ralph Metzler 已提交
286 287
	}
	/*
288 289
	   Now x has binary point between bit[scale] and bit[scale-1]
	   and 1.0 <= x < 2.0 */
R
Ralph Metzler 已提交
290 291

	/* correction for divison: log(x) = log(x/y)+log(y) */
292
	y = k * ((((u32) 1) << scale) * 200);
R
Ralph Metzler 已提交
293 294

	/* remove integer part */
295
	x &= ((((u32) 1) << scale) - 1);
R
Ralph Metzler 已提交
296 297 298
	/* get index */
	i = (u8) (x >> (scale - indexWidth));
	/* compute delta (x - a) */
299
	d = x & ((((u32) 1) << (scale - indexWidth)) - 1);
R
Ralph Metzler 已提交
300 301
	/* compute log, multiplication (d* (..)) must be within range ! */
	y += log2lut[i] +
302
	    ((d * (log2lut[i + 1] - log2lut[i])) >> (scale - indexWidth));
R
Ralph Metzler 已提交
303
	/* Conver to log10() */
304
	y /= 108853;		/* (log2(10) << scale) */
R
Ralph Metzler 已提交
305 306
	r = (y >> 1);
	/* rounding */
307
	if (y & ((u32) 1))
R
Ralph Metzler 已提交
308
		r++;
309
	return r;
R
Ralph Metzler 已提交
310 311 312 313 314 315 316 317
}

/****************************************************************************/
/* I2C **********************************************************************/
/****************************************************************************/

static int i2c_read1(struct i2c_adapter *adapter, u8 adr, u8 *val)
{
318 319 320
	struct i2c_msg msgs[1] = { {.addr = adr, .flags = I2C_M_RD,
				    .buf = val, .len = 1}
	};
R
Ralph Metzler 已提交
321 322 323 324 325
	return (i2c_transfer(adapter, msgs, 1) == 1) ? 0 : -1;
}

static int i2c_write(struct i2c_adapter *adap, u8 adr, u8 *data, int len)
{
326 327
	struct i2c_msg msg = {
	    .addr = adr, .flags = 0, .buf = data, .len = len };
R
Ralph Metzler 已提交
328

329 330 331 332 333 334 335
	dprintk(3, ":");
	if (debug > 2) {
		int i;
		for (i = 0; i < len; i++)
			printk(KERN_CONT " %02x", data[i]);
		printk(KERN_CONT "\n");
	}
R
Ralph Metzler 已提交
336
	if (i2c_transfer(adap, &msg, 1) != 1) {
337
		printk(KERN_ERR "drxk: i2c write error at addr 0x%02x\n", adr);
R
Ralph Metzler 已提交
338 339 340 341 342 343 344 345
		return -1;
	}
	return 0;
}

static int i2c_read(struct i2c_adapter *adap,
		    u8 adr, u8 *msg, int len, u8 *answ, int alen)
{
346 347 348 349 350
	struct i2c_msg msgs[2] = { {.addr = adr, .flags = 0,
				    .buf = msg, .len = len},
	{.addr = adr, .flags = I2C_M_RD,
	 .buf = answ, .len = alen}
	};
351 352 353 354 355 356 357
	dprintk(3, ":");
	if (debug > 2) {
		int i;
		for (i = 0; i < len; i++)
			printk(KERN_CONT " %02x", msg[i]);
		printk(KERN_CONT "\n");
	}
R
Ralph Metzler 已提交
358
	if (i2c_transfer(adap, msgs, 2) != 2) {
359 360 361
		if (debug > 2)
			printk(KERN_CONT ": ERROR!\n");

362
		printk(KERN_ERR "drxk: i2c read error at addr 0x%02x\n", adr);
R
Ralph Metzler 已提交
363 364
		return -1;
	}
365 366 367 368 369 370 371
	if (debug > 2) {
		int i;
		printk(KERN_CONT ": Read ");
		for (i = 0; i < len; i++)
			printk(KERN_CONT " %02x", msg[i]);
		printk(KERN_CONT "\n");
	}
R
Ralph Metzler 已提交
372 373 374
	return 0;
}

375
static int read16_flags(struct drxk_state *state, u32 reg, u16 *data, u8 flags)
R
Ralph Metzler 已提交
376
{
377
	u8 adr = state->demod_address, mm1[4], mm2[2], len;
R
Ralph Metzler 已提交
378 379 380 381 382 383 384 385 386 387 388 389 390 391
#ifdef I2C_LONG_ADR
	flags |= 0xC0;
#endif
	if (DRXDAP_FASI_LONG_FORMAT(reg) || (flags != 0)) {
		mm1[0] = (((reg << 1) & 0xFF) | 0x01);
		mm1[1] = ((reg >> 16) & 0xFF);
		mm1[2] = ((reg >> 24) & 0xFF) | flags;
		mm1[3] = ((reg >> 7) & 0xFF);
		len = 4;
	} else {
		mm1[0] = ((reg << 1) & 0xFF);
		mm1[1] = (((reg >> 16) & 0x0F) | ((reg >> 18) & 0xF0));
		len = 2;
	}
392
	dprintk(2, "(0x%08x, 0x%02x)\n", reg, flags);
R
Ralph Metzler 已提交
393 394 395 396
	if (i2c_read(state->i2c, adr, mm1, len, mm2, 2) < 0)
		return -1;
	if (data)
		*data = mm2[0] | (mm2[1] << 8);
397

R
Ralph Metzler 已提交
398 399 400
	return 0;
}

401
static int read16(struct drxk_state *state, u32 reg, u16 *data)
R
Ralph Metzler 已提交
402
{
403
	return read16_flags(state, reg, data, 0);
R
Ralph Metzler 已提交
404 405
}

406
static int read32_flags(struct drxk_state *state, u32 reg, u32 *data, u8 flags)
R
Ralph Metzler 已提交
407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422
{
	u8 adr = state->demod_address, mm1[4], mm2[4], len;
#ifdef I2C_LONG_ADR
	flags |= 0xC0;
#endif
	if (DRXDAP_FASI_LONG_FORMAT(reg) || (flags != 0)) {
		mm1[0] = (((reg << 1) & 0xFF) | 0x01);
		mm1[1] = ((reg >> 16) & 0xFF);
		mm1[2] = ((reg >> 24) & 0xFF) | flags;
		mm1[3] = ((reg >> 7) & 0xFF);
		len = 4;
	} else {
		mm1[0] = ((reg << 1) & 0xFF);
		mm1[1] = (((reg >> 16) & 0x0F) | ((reg >> 18) & 0xF0));
		len = 2;
	}
423
	dprintk(2, "(0x%08x, 0x%02x)\n", reg, flags);
R
Ralph Metzler 已提交
424 425 426 427
	if (i2c_read(state->i2c, adr, mm1, len, mm2, 4) < 0)
		return -1;
	if (data)
		*data = mm2[0] | (mm2[1] << 8) |
428
		    (mm2[2] << 16) | (mm2[3] << 24);
429

R
Ralph Metzler 已提交
430 431 432
	return 0;
}

433 434 435 436 437 438
static int read32(struct drxk_state *state, u32 reg, u32 *data)
{
	return read32_flags(state, reg, data, 0);
}

static int write16_flags(struct drxk_state *state, u32 reg, u16 data, u8 flags)
R
Ralph Metzler 已提交
439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455
{
	u8 adr = state->demod_address, mm[6], len;
#ifdef I2C_LONG_ADR
	flags |= 0xC0;
#endif
	if (DRXDAP_FASI_LONG_FORMAT(reg) || (flags != 0)) {
		mm[0] = (((reg << 1) & 0xFF) | 0x01);
		mm[1] = ((reg >> 16) & 0xFF);
		mm[2] = ((reg >> 24) & 0xFF) | flags;
		mm[3] = ((reg >> 7) & 0xFF);
		len = 4;
	} else {
		mm[0] = ((reg << 1) & 0xFF);
		mm[1] = (((reg >> 16) & 0x0F) | ((reg >> 18) & 0xF0));
		len = 2;
	}
	mm[len] = data & 0xff;
456
	mm[len + 1] = (data >> 8) & 0xff;
457 458

	dprintk(2, "(0x%08x, 0x%04x, 0x%02x)\n", reg, data, flags);
R
Ralph Metzler 已提交
459 460 461 462 463
	if (i2c_write(state->i2c, adr, mm, len + 2) < 0)
		return -1;
	return 0;
}

464
static int write16(struct drxk_state *state, u32 reg, u16 data)
R
Ralph Metzler 已提交
465
{
466
	return write16_flags(state, reg, data, 0);
R
Ralph Metzler 已提交
467 468
}

469
static int write32_flags(struct drxk_state *state, u32 reg, u32 data, u8 flags)
R
Ralph Metzler 已提交
470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486
{
	u8 adr = state->demod_address, mm[8], len;
#ifdef I2C_LONG_ADR
	flags |= 0xC0;
#endif
	if (DRXDAP_FASI_LONG_FORMAT(reg) || (flags != 0)) {
		mm[0] = (((reg << 1) & 0xFF) | 0x01);
		mm[1] = ((reg >> 16) & 0xFF);
		mm[2] = ((reg >> 24) & 0xFF) | flags;
		mm[3] = ((reg >> 7) & 0xFF);
		len = 4;
	} else {
		mm[0] = ((reg << 1) & 0xFF);
		mm[1] = (((reg >> 16) & 0x0F) | ((reg >> 18) & 0xF0));
		len = 2;
	}
	mm[len] = data & 0xff;
487 488 489
	mm[len + 1] = (data >> 8) & 0xff;
	mm[len + 2] = (data >> 16) & 0xff;
	mm[len + 3] = (data >> 24) & 0xff;
490
	dprintk(2, "(0x%08x, 0x%08x, 0x%02x)\n", reg, data, flags);
491
	if (i2c_write(state->i2c, adr, mm, len + 4) < 0)
R
Ralph Metzler 已提交
492 493 494 495
		return -1;
	return 0;
}

496 497 498 499 500 501 502
static int write32(struct drxk_state *state, u32 reg, u32 data)
{
	return write32_flags(state, reg, data, 0);
}

static int write_block(struct drxk_state *state, u32 Address,
		      const int BlockSize, const u8 pBlock[])
R
Ralph Metzler 已提交
503 504
{
	int status = 0, BlkSize = BlockSize;
505
	u8 Flags = 0;
R
Ralph Metzler 已提交
506 507 508
#ifdef I2C_LONG_ADR
	Flags |= 0xC0;
#endif
509
	while (BlkSize > 0) {
R
Ralph Metzler 已提交
510
		int Chunk = BlkSize > state->m_ChunkSize ?
511
		    state->m_ChunkSize : BlkSize;
R
Ralph Metzler 已提交
512 513 514
		u8 *AdrBuf = &state->Chunk[0];
		u32 AdrLength = 0;

515 516 517 518 519
		if (DRXDAP_FASI_LONG_FORMAT(Address) || (Flags != 0)) {
			AdrBuf[0] = (((Address << 1) & 0xFF) | 0x01);
			AdrBuf[1] = ((Address >> 16) & 0xFF);
			AdrBuf[2] = ((Address >> 24) & 0xFF);
			AdrBuf[3] = ((Address >> 7) & 0xFF);
R
Ralph Metzler 已提交
520 521 522 523
			AdrBuf[2] |= Flags;
			AdrLength = 4;
			if (Chunk == state->m_ChunkSize)
				Chunk -= 2;
524
		} else {
R
Ralph Metzler 已提交
525 526 527 528 529 530
			AdrBuf[0] = ((Address << 1) & 0xFF);
			AdrBuf[1] = (((Address >> 16) & 0x0F) |
				     ((Address >> 18) & 0xF0));
			AdrLength = 2;
		}
		memcpy(&state->Chunk[AdrLength], pBlock, Chunk);
531 532 533 534 535 536 537 538
		dprintk(2, "(0x%08x, 0x%02x)\n", Address, Flags);
		if (debug > 1) {
			int i;
			if (pBlock)
				for (i = 0; i < Chunk; i++)
					printk(KERN_CONT " %02x", pBlock[i]);
			printk(KERN_CONT "\n");
		}
R
Ralph Metzler 已提交
539
		status = i2c_write(state->i2c, state->demod_address,
540 541
				   &state->Chunk[0], Chunk + AdrLength);
		if (status < 0) {
542 543
			printk(KERN_ERR "drxk: %s: i2c write error at addr 0x%02x\n",
			       __func__, Address);
R
Ralph Metzler 已提交
544 545 546 547 548 549
			break;
		}
		pBlock += Chunk;
		Address += (Chunk >> 1);
		BlkSize -= Chunk;
	}
550
	return status;
R
Ralph Metzler 已提交
551 552 553 554 555 556 557 558 559 560 561 562
}

#ifndef DRXK_MAX_RETRIES_POWERUP
#define DRXK_MAX_RETRIES_POWERUP 20
#endif

int PowerUpDevice(struct drxk_state *state)
{
	int status;
	u8 data = 0;
	u16 retryCount = 0;

563 564
	dprintk(1, "\n");

R
Ralph Metzler 已提交
565
	status = i2c_read1(state->i2c, state->demod_address, &data);
566
	if (status < 0)
R
Ralph Metzler 已提交
567 568 569 570
		do {
			data = 0;
			if (i2c_write(state->i2c,
				      state->demod_address, &data, 1) < 0)
571
				printk(KERN_ERR "drxk: powerup failed\n");
R
Ralph Metzler 已提交
572
			msleep(10);
573
			retryCount++;
R
Ralph Metzler 已提交
574 575 576 577 578 579 580
		} while (i2c_read1(state->i2c,
				   state->demod_address, &data) < 0 &&
			 (retryCount < DRXK_MAX_RETRIES_POWERUP));
	if (retryCount >= DRXK_MAX_RETRIES_POWERUP)
		return -1;
	do {
		/* Make sure all clk domains are active */
581
		status = write16(state, SIO_CC_PWD_MODE__A, SIO_CC_PWD_MODE_LEVEL_NONE);
582 583
		if (status < 0)
			break;
584
		status = write16(state, SIO_CC_UPDATE__A, SIO_CC_UPDATE_KEY);
585 586
		if (status < 0)
			break;
R
Ralph Metzler 已提交
587
		/* Enable pll lock tests */
588
		status = write16(state, SIO_CC_PLL_LOCK__A, 1);
589 590
		if (status < 0)
			break;
R
Ralph Metzler 已提交
591 592 593 594 595 596 597 598
		state->m_currentPowerMode = DRX_POWER_UP;
	} while (0);
	return status;
}


static int init_state(struct drxk_state *state)
{
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 625
	u32 ulVSBIfAgcMode = DRXK_AGC_CTRL_AUTO;
	u32 ulVSBIfAgcOutputLevel = 0;
	u32 ulVSBIfAgcMinLevel = 0;
	u32 ulVSBIfAgcMaxLevel = 0x7FFF;
	u32 ulVSBIfAgcSpeed = 3;

	u32 ulVSBRfAgcMode = DRXK_AGC_CTRL_AUTO;
	u32 ulVSBRfAgcOutputLevel = 0;
	u32 ulVSBRfAgcMinLevel = 0;
	u32 ulVSBRfAgcMaxLevel = 0x7FFF;
	u32 ulVSBRfAgcSpeed = 3;
	u32 ulVSBRfAgcTop = 9500;
	u32 ulVSBRfAgcCutOffCurrent = 4000;

	u32 ulATVIfAgcMode = DRXK_AGC_CTRL_AUTO;
	u32 ulATVIfAgcOutputLevel = 0;
	u32 ulATVIfAgcMinLevel = 0;
	u32 ulATVIfAgcMaxLevel = 0;
	u32 ulATVIfAgcSpeed = 3;

	u32 ulATVRfAgcMode = DRXK_AGC_CTRL_OFF;
	u32 ulATVRfAgcOutputLevel = 0;
	u32 ulATVRfAgcMinLevel = 0;
	u32 ulATVRfAgcMaxLevel = 0;
	u32 ulATVRfAgcTop = 9500;
	u32 ulATVRfAgcCutOffCurrent = 4000;
	u32 ulATVRfAgcSpeed = 3;
R
Ralph Metzler 已提交
626 627 628 629 630 631 632 633 634 635 636 637 638 639

	u32 ulQual83 = DEFAULT_MER_83;
	u32 ulQual93 = DEFAULT_MER_93;

	u32 ulDVBTStaticTSClock = 1;
	u32 ulDVBCStaticTSClock = 1;

	u32 ulMpegLockTimeOut = DEFAULT_DRXK_MPEG_LOCK_TIMEOUT;
	u32 ulDemodLockTimeOut = DEFAULT_DRXK_DEMOD_LOCK_TIMEOUT;

	/* io_pad_cfg register (8 bit reg.) MSB bit is 1 (default value) */
	/* io_pad_cfg_mode output mode is drive always */
	/* io_pad_cfg_drive is set to power 2 (23 mA) */
	u32 ulGPIOCfg = 0x0113;
640
	u32 ulGPIO = 0;
R
Ralph Metzler 已提交
641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656
	u32 ulSerialMode = 1;
	u32 ulInvertTSClock = 0;
	u32 ulTSDataStrength = DRXK_MPEG_SERIAL_OUTPUT_PIN_DRIVE_STRENGTH;
	u32 ulTSClockkStrength = DRXK_MPEG_OUTPUT_CLK_DRIVE_STRENGTH;
	u32 ulDVBTBitrate = 50000000;
	u32 ulDVBCBitrate = DRXK_QAM_SYMBOLRATE_MAX * 8;

	u32 ulInsertRSByte = 0;

	u32 ulRfMirror = 1;
	u32 ulPowerDown = 0;

	u32 ulAntennaDVBT = 1;
	u32 ulAntennaDVBC = 0;
	u32 ulAntennaSwitchDVBTDVBC = 0;

657 658
	dprintk(1, "\n");

R
Ralph Metzler 已提交
659
	state->m_hasLNA = false;
660 661 662
	state->m_hasDVBT = false;
	state->m_hasDVBC = false;
	state->m_hasATV = false;
R
Ralph Metzler 已提交
663 664 665 666 667 668 669 670 671 672
	state->m_hasOOB = false;
	state->m_hasAudio = false;

	state->m_ChunkSize = 124;

	state->m_oscClockFreq = 0;
	state->m_smartAntInverted = false;
	state->m_bPDownOpenBridge = false;

	/* real system clock frequency in kHz */
673
	state->m_sysClockFreq = 151875;
R
Ralph Metzler 已提交
674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695
	/* Timing div, 250ns/Psys */
	/* Timing div, = (delay (nano seconds) * sysclk (kHz))/ 1000 */
	state->m_HICfgTimingDiv = ((state->m_sysClockFreq / 1000) *
				   HI_I2C_DELAY) / 1000;
	/* Clipping */
	if (state->m_HICfgTimingDiv > SIO_HI_RA_RAM_PAR_2_CFG_DIV__M)
		state->m_HICfgTimingDiv = SIO_HI_RA_RAM_PAR_2_CFG_DIV__M;
	state->m_HICfgWakeUpKey = (state->demod_address << 1);
	/* port/bridge/power down ctrl */
	state->m_HICfgCtrl = SIO_HI_RA_RAM_PAR_5_CFG_SLV0_SLAVE;

	state->m_bPowerDown = (ulPowerDown != 0);

	state->m_DRXK_A1_PATCH_CODE = false;
	state->m_DRXK_A1_ROM_CODE = false;
	state->m_DRXK_A2_ROM_CODE = false;
	state->m_DRXK_A3_ROM_CODE = false;
	state->m_DRXK_A2_PATCH_CODE = false;
	state->m_DRXK_A3_PATCH_CODE = false;

	/* Init AGC and PGA parameters */
	/* VSB IF */
696 697 698 699 700
	state->m_vsbIfAgcCfg.ctrlMode = (ulVSBIfAgcMode);
	state->m_vsbIfAgcCfg.outputLevel = (ulVSBIfAgcOutputLevel);
	state->m_vsbIfAgcCfg.minOutputLevel = (ulVSBIfAgcMinLevel);
	state->m_vsbIfAgcCfg.maxOutputLevel = (ulVSBIfAgcMaxLevel);
	state->m_vsbIfAgcCfg.speed = (ulVSBIfAgcSpeed);
R
Ralph Metzler 已提交
701 702 703
	state->m_vsbPgaCfg = 140;

	/* VSB RF */
704 705 706 707 708 709 710 711 712
	state->m_vsbRfAgcCfg.ctrlMode = (ulVSBRfAgcMode);
	state->m_vsbRfAgcCfg.outputLevel = (ulVSBRfAgcOutputLevel);
	state->m_vsbRfAgcCfg.minOutputLevel = (ulVSBRfAgcMinLevel);
	state->m_vsbRfAgcCfg.maxOutputLevel = (ulVSBRfAgcMaxLevel);
	state->m_vsbRfAgcCfg.speed = (ulVSBRfAgcSpeed);
	state->m_vsbRfAgcCfg.top = (ulVSBRfAgcTop);
	state->m_vsbRfAgcCfg.cutOffCurrent = (ulVSBRfAgcCutOffCurrent);
	state->m_vsbPreSawCfg.reference = 0x07;
	state->m_vsbPreSawCfg.usePreSaw = true;
R
Ralph Metzler 已提交
713 714 715 716 717 718 719 720 721

	state->m_Quality83percent = DEFAULT_MER_83;
	state->m_Quality93percent = DEFAULT_MER_93;
	if (ulQual93 <= 500 && ulQual83 < ulQual93) {
		state->m_Quality83percent = ulQual83;
		state->m_Quality93percent = ulQual93;
	}

	/* ATV IF */
722 723 724 725 726
	state->m_atvIfAgcCfg.ctrlMode = (ulATVIfAgcMode);
	state->m_atvIfAgcCfg.outputLevel = (ulATVIfAgcOutputLevel);
	state->m_atvIfAgcCfg.minOutputLevel = (ulATVIfAgcMinLevel);
	state->m_atvIfAgcCfg.maxOutputLevel = (ulATVIfAgcMaxLevel);
	state->m_atvIfAgcCfg.speed = (ulATVIfAgcSpeed);
R
Ralph Metzler 已提交
727 728

	/* ATV RF */
729 730 731 732 733 734 735 736 737
	state->m_atvRfAgcCfg.ctrlMode = (ulATVRfAgcMode);
	state->m_atvRfAgcCfg.outputLevel = (ulATVRfAgcOutputLevel);
	state->m_atvRfAgcCfg.minOutputLevel = (ulATVRfAgcMinLevel);
	state->m_atvRfAgcCfg.maxOutputLevel = (ulATVRfAgcMaxLevel);
	state->m_atvRfAgcCfg.speed = (ulATVRfAgcSpeed);
	state->m_atvRfAgcCfg.top = (ulATVRfAgcTop);
	state->m_atvRfAgcCfg.cutOffCurrent = (ulATVRfAgcCutOffCurrent);
	state->m_atvPreSawCfg.reference = 0x04;
	state->m_atvPreSawCfg.usePreSaw = true;
R
Ralph Metzler 已提交
738 739 740


	/* DVBT RF */
741 742 743 744 745 746 747
	state->m_dvbtRfAgcCfg.ctrlMode = DRXK_AGC_CTRL_OFF;
	state->m_dvbtRfAgcCfg.outputLevel = 0;
	state->m_dvbtRfAgcCfg.minOutputLevel = 0;
	state->m_dvbtRfAgcCfg.maxOutputLevel = 0xFFFF;
	state->m_dvbtRfAgcCfg.top = 0x2100;
	state->m_dvbtRfAgcCfg.cutOffCurrent = 4000;
	state->m_dvbtRfAgcCfg.speed = 1;
R
Ralph Metzler 已提交
748 749 750


	/* DVBT IF */
751 752 753 754 755 756 757
	state->m_dvbtIfAgcCfg.ctrlMode = DRXK_AGC_CTRL_AUTO;
	state->m_dvbtIfAgcCfg.outputLevel = 0;
	state->m_dvbtIfAgcCfg.minOutputLevel = 0;
	state->m_dvbtIfAgcCfg.maxOutputLevel = 9000;
	state->m_dvbtIfAgcCfg.top = 13424;
	state->m_dvbtIfAgcCfg.cutOffCurrent = 0;
	state->m_dvbtIfAgcCfg.speed = 3;
R
Ralph Metzler 已提交
758
	state->m_dvbtIfAgcCfg.FastClipCtrlDelay = 30;
759 760
	state->m_dvbtIfAgcCfg.IngainTgtMax = 30000;
	/* state->m_dvbtPgaCfg = 140; */
R
Ralph Metzler 已提交
761

762 763
	state->m_dvbtPreSawCfg.reference = 4;
	state->m_dvbtPreSawCfg.usePreSaw = false;
R
Ralph Metzler 已提交
764 765

	/* QAM RF */
766 767 768 769 770 771 772
	state->m_qamRfAgcCfg.ctrlMode = DRXK_AGC_CTRL_OFF;
	state->m_qamRfAgcCfg.outputLevel = 0;
	state->m_qamRfAgcCfg.minOutputLevel = 6023;
	state->m_qamRfAgcCfg.maxOutputLevel = 27000;
	state->m_qamRfAgcCfg.top = 0x2380;
	state->m_qamRfAgcCfg.cutOffCurrent = 4000;
	state->m_qamRfAgcCfg.speed = 3;
R
Ralph Metzler 已提交
773 774

	/* QAM IF */
775 776 777 778 779 780 781 782
	state->m_qamIfAgcCfg.ctrlMode = DRXK_AGC_CTRL_AUTO;
	state->m_qamIfAgcCfg.outputLevel = 0;
	state->m_qamIfAgcCfg.minOutputLevel = 0;
	state->m_qamIfAgcCfg.maxOutputLevel = 9000;
	state->m_qamIfAgcCfg.top = 0x0511;
	state->m_qamIfAgcCfg.cutOffCurrent = 0;
	state->m_qamIfAgcCfg.speed = 3;
	state->m_qamIfAgcCfg.IngainTgtMax = 5119;
R
Ralph Metzler 已提交
783 784
	state->m_qamIfAgcCfg.FastClipCtrlDelay = 50;

785 786 787
	state->m_qamPgaCfg = 140;
	state->m_qamPreSawCfg.reference = 4;
	state->m_qamPreSawCfg.usePreSaw = false;
R
Ralph Metzler 已提交
788 789 790 791 792

	state->m_OperationMode = OM_NONE;
	state->m_DrxkState = DRXK_UNINITIALIZED;

	/* MPEG output configuration */
793 794 795 796 797 798 799 800 801
	state->m_enableMPEGOutput = true;	/* If TRUE; enable MPEG ouput */
	state->m_insertRSByte = false;	/* If TRUE; insert RS byte */
	state->m_enableParallel = true;	/* If TRUE;
					   parallel out otherwise serial */
	state->m_invertDATA = false;	/* If TRUE; invert DATA signals */
	state->m_invertERR = false;	/* If TRUE; invert ERR signal */
	state->m_invertSTR = false;	/* If TRUE; invert STR signals */
	state->m_invertVAL = false;	/* If TRUE; invert VAL signals */
	state->m_invertCLK = (ulInvertTSClock != 0);	/* If TRUE; invert CLK signals */
R
Ralph Metzler 已提交
802
	state->m_DVBTStaticCLK = (ulDVBTStaticTSClock != 0);
803
	state->m_DVBCStaticCLK = (ulDVBCStaticTSClock != 0);
R
Ralph Metzler 已提交
804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826
	/* If TRUE; static MPEG clockrate will be used;
	   otherwise clockrate will adapt to the bitrate of the TS */

	state->m_DVBTBitrate = ulDVBTBitrate;
	state->m_DVBCBitrate = ulDVBCBitrate;

	state->m_TSDataStrength = (ulTSDataStrength & 0x07);
	state->m_TSClockkStrength = (ulTSClockkStrength & 0x07);

	/* Maximum bitrate in b/s in case static clockrate is selected */
	state->m_mpegTsStaticBitrate = 19392658;
	state->m_disableTEIhandling = false;

	if (ulInsertRSByte)
		state->m_insertRSByte = true;

	state->m_MpegLockTimeOut = DEFAULT_DRXK_MPEG_LOCK_TIMEOUT;
	if (ulMpegLockTimeOut < 10000)
		state->m_MpegLockTimeOut = ulMpegLockTimeOut;
	state->m_DemodLockTimeOut = DEFAULT_DRXK_DEMOD_LOCK_TIMEOUT;
	if (ulDemodLockTimeOut < 10000)
		state->m_DemodLockTimeOut = ulDemodLockTimeOut;

827 828
	/* QAM defaults */
	state->m_Constellation = DRX_CONSTELLATION_AUTO;
R
Ralph Metzler 已提交
829
	state->m_qamInterleaveMode = DRXK_QAM_I12_J17;
830 831
	state->m_fecRsPlen = 204 * 8;	/* fecRsPlen  annex A */
	state->m_fecRsPrescale = 1;
R
Ralph Metzler 已提交
832 833 834 835 836

	state->m_sqiSpeed = DRXK_DVBT_SQI_SPEED_MEDIUM;
	state->m_agcFastClipCtrlDelay = 0;

	state->m_GPIOCfg = (ulGPIOCfg);
837
	state->m_GPIO = (ulGPIO == 0 ? 0 : 1);
R
Ralph Metzler 已提交
838 839 840 841

	state->m_AntennaDVBT = (ulAntennaDVBT == 0 ? 0 : 1);
	state->m_AntennaDVBC = (ulAntennaDVBC == 0 ? 0 : 1);
	state->m_AntennaSwitchDVBTDVBC =
842
	    (ulAntennaSwitchDVBTDVBC == 0 ? 0 : 1);
R
Ralph Metzler 已提交
843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860

	state->m_bPowerDown = false;
	state->m_currentPowerMode = DRX_POWER_DOWN;

	state->m_enableParallel = (ulSerialMode == 0);

	state->m_rfmirror = (ulRfMirror == 0);
	state->m_IfAgcPol = false;
	return 0;
}

static int DRXX_Open(struct drxk_state *state)
{
	int status = 0;
	u32 jtag = 0;
	u16 bid = 0;
	u16 key = 0;

861
	dprintk(1, "\n");
R
Ralph Metzler 已提交
862 863
	do {
		/* stop lock indicator process */
864
		status = write16(state, SCU_RAM_GPIO__A, SCU_RAM_GPIO_HW_LOCK_IND_DISABLE);
865 866
		if (status < 0)
			break;
R
Ralph Metzler 已提交
867
		/* Check device id */
868
		status = read16(state, SIO_TOP_COMM_KEY__A, &key);
869 870
		if (status < 0)
			break;
871
		status = write16(state, SIO_TOP_COMM_KEY__A, SIO_TOP_COMM_KEY_KEY);
872 873
		if (status < 0)
			break;
874
		status = read32(state, SIO_TOP_JTAGID_LO__A, &jtag);
875 876
		if (status < 0)
			break;
877
		status = read16(state, SIO_PDR_UIO_IN_HI__A, &bid);
878 879
		if (status < 0)
			break;
880
		status = write16(state, SIO_TOP_COMM_KEY__A, key);
881 882
		if (status < 0)
			break;
883
	} while (0);
R
Ralph Metzler 已提交
884 885 886 887 888
	return status;
}

static int GetDeviceCapabilities(struct drxk_state *state)
{
889
	u16 sioPdrOhwCfg = 0;
R
Ralph Metzler 已提交
890 891 892
	u32 sioTopJtagidLo = 0;
	int status;

893
	dprintk(1, "\n");
R
Ralph Metzler 已提交
894 895 896
	do {
		/* driver 0.9.0 */
		/* stop lock indicator process */
897
		status = write16(state, SCU_RAM_GPIO__A, SCU_RAM_GPIO_HW_LOCK_IND_DISABLE);
898 899
		if (status < 0)
			break;
R
Ralph Metzler 已提交
900

901
		status = write16(state, SIO_TOP_COMM_KEY__A, 0xFABA);
902 903
		if (status < 0)
			break;
904
		status = read16(state, SIO_PDR_OHW_CFG__A, &sioPdrOhwCfg);
905 906
		if (status < 0)
			break;
907
		status = write16(state, SIO_TOP_COMM_KEY__A, 0x0000);
908 909
		if (status < 0)
			break;
R
Ralph Metzler 已提交
910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930

		switch ((sioPdrOhwCfg & SIO_PDR_OHW_CFG_FREF_SEL__M)) {
		case 0:
			/* ignore (bypass ?) */
			break;
		case 1:
			/* 27 MHz */
			state->m_oscClockFreq = 27000;
			break;
		case 2:
			/* 20.25 MHz */
			state->m_oscClockFreq = 20250;
			break;
		case 3:
			/* 4 MHz */
			state->m_oscClockFreq = 20250;
			break;
		default:
			return -1;
		}
		/*
931 932 933
		   Determine device capabilities
		   Based on pinning v14
		 */
934
		status = read32(state, SIO_TOP_JTAGID_LO__A, &sioTopJtagidLo);
935 936
		if (status < 0)
			break;
R
Ralph Metzler 已提交
937
		/* driver 0.9.0 */
938
		switch ((sioTopJtagidLo >> 29) & 0xF) {
R
Ralph Metzler 已提交
939 940 941 942 943 944 945 946 947 948 949 950 951 952
		case 0:
			state->m_deviceSpin = DRXK_SPIN_A1;
			break;
		case 2:
			state->m_deviceSpin = DRXK_SPIN_A2;
			break;
		case 3:
			state->m_deviceSpin = DRXK_SPIN_A3;
			break;
		default:
			state->m_deviceSpin = DRXK_SPIN_UNKNOWN;
			status = -1;
			break;
		}
953
		switch ((sioTopJtagidLo >> 12) & 0xFF) {
R
Ralph Metzler 已提交
954 955
		case 0x13:
			/* typeId = DRX3913K_TYPE_ID */
956 957 958
			state->m_hasLNA = false;
			state->m_hasOOB = false;
			state->m_hasATV = false;
R
Ralph Metzler 已提交
959
			state->m_hasAudio = false;
960 961
			state->m_hasDVBT = true;
			state->m_hasDVBC = true;
R
Ralph Metzler 已提交
962 963 964
			state->m_hasSAWSW = true;
			state->m_hasGPIO2 = false;
			state->m_hasGPIO1 = false;
965
			state->m_hasIRQN = false;
R
Ralph Metzler 已提交
966 967 968
			break;
		case 0x15:
			/* typeId = DRX3915K_TYPE_ID */
969 970 971
			state->m_hasLNA = false;
			state->m_hasOOB = false;
			state->m_hasATV = true;
R
Ralph Metzler 已提交
972
			state->m_hasAudio = false;
973 974
			state->m_hasDVBT = true;
			state->m_hasDVBC = false;
R
Ralph Metzler 已提交
975 976 977
			state->m_hasSAWSW = true;
			state->m_hasGPIO2 = true;
			state->m_hasGPIO1 = true;
978
			state->m_hasIRQN = false;
R
Ralph Metzler 已提交
979 980 981
			break;
		case 0x16:
			/* typeId = DRX3916K_TYPE_ID */
982 983 984
			state->m_hasLNA = false;
			state->m_hasOOB = false;
			state->m_hasATV = true;
R
Ralph Metzler 已提交
985
			state->m_hasAudio = false;
986 987
			state->m_hasDVBT = true;
			state->m_hasDVBC = false;
R
Ralph Metzler 已提交
988 989 990
			state->m_hasSAWSW = true;
			state->m_hasGPIO2 = true;
			state->m_hasGPIO1 = true;
991
			state->m_hasIRQN = false;
R
Ralph Metzler 已提交
992 993 994
			break;
		case 0x18:
			/* typeId = DRX3918K_TYPE_ID */
995 996 997
			state->m_hasLNA = false;
			state->m_hasOOB = false;
			state->m_hasATV = true;
R
Ralph Metzler 已提交
998
			state->m_hasAudio = true;
999 1000
			state->m_hasDVBT = true;
			state->m_hasDVBC = false;
R
Ralph Metzler 已提交
1001 1002 1003
			state->m_hasSAWSW = true;
			state->m_hasGPIO2 = true;
			state->m_hasGPIO1 = true;
1004
			state->m_hasIRQN = false;
R
Ralph Metzler 已提交
1005 1006 1007
			break;
		case 0x21:
			/* typeId = DRX3921K_TYPE_ID */
1008 1009 1010
			state->m_hasLNA = false;
			state->m_hasOOB = false;
			state->m_hasATV = true;
R
Ralph Metzler 已提交
1011
			state->m_hasAudio = true;
1012 1013
			state->m_hasDVBT = true;
			state->m_hasDVBC = true;
R
Ralph Metzler 已提交
1014 1015 1016
			state->m_hasSAWSW = true;
			state->m_hasGPIO2 = true;
			state->m_hasGPIO1 = true;
1017
			state->m_hasIRQN = false;
R
Ralph Metzler 已提交
1018 1019 1020
			break;
		case 0x23:
			/* typeId = DRX3923K_TYPE_ID */
1021 1022 1023
			state->m_hasLNA = false;
			state->m_hasOOB = false;
			state->m_hasATV = true;
R
Ralph Metzler 已提交
1024
			state->m_hasAudio = true;
1025 1026
			state->m_hasDVBT = true;
			state->m_hasDVBC = true;
R
Ralph Metzler 已提交
1027 1028 1029
			state->m_hasSAWSW = true;
			state->m_hasGPIO2 = true;
			state->m_hasGPIO1 = true;
1030
			state->m_hasIRQN = false;
R
Ralph Metzler 已提交
1031 1032 1033
			break;
		case 0x25:
			/* typeId = DRX3925K_TYPE_ID */
1034 1035 1036
			state->m_hasLNA = false;
			state->m_hasOOB = false;
			state->m_hasATV = true;
R
Ralph Metzler 已提交
1037
			state->m_hasAudio = true;
1038 1039
			state->m_hasDVBT = true;
			state->m_hasDVBC = true;
R
Ralph Metzler 已提交
1040 1041 1042
			state->m_hasSAWSW = true;
			state->m_hasGPIO2 = true;
			state->m_hasGPIO1 = true;
1043
			state->m_hasIRQN = false;
R
Ralph Metzler 已提交
1044 1045 1046
			break;
		case 0x26:
			/* typeId = DRX3926K_TYPE_ID */
1047 1048 1049
			state->m_hasLNA = false;
			state->m_hasOOB = false;
			state->m_hasATV = true;
R
Ralph Metzler 已提交
1050
			state->m_hasAudio = false;
1051 1052
			state->m_hasDVBT = true;
			state->m_hasDVBC = true;
R
Ralph Metzler 已提交
1053 1054 1055
			state->m_hasSAWSW = true;
			state->m_hasGPIO2 = true;
			state->m_hasGPIO1 = true;
1056
			state->m_hasIRQN = false;
R
Ralph Metzler 已提交
1057 1058
			break;
		default:
1059
			printk(KERN_ERR "drxk: DeviceID not supported = %02x\n",
1060
			       ((sioTopJtagidLo >> 12) & 0xFF));
R
Ralph Metzler 已提交
1061 1062 1063
			status = -1;
			break;
		}
1064
	} while (0);
R
Ralph Metzler 已提交
1065 1066 1067 1068 1069 1070 1071 1072
	return status;
}

static int HI_Command(struct drxk_state *state, u16 cmd, u16 *pResult)
{
	int status;
	bool powerdown_cmd;

1073 1074
	dprintk(1, "\n");

R
Ralph Metzler 已提交
1075
	/* Write command */
1076
	status = write16(state, SIO_HI_RA_RAM_CMD__A, cmd);
R
Ralph Metzler 已提交
1077 1078 1079 1080 1081 1082
	if (status < 0)
		return status;
	if (cmd == SIO_HI_RA_RAM_CMD_RESET)
		msleep(1);

	powerdown_cmd =
1083 1084 1085 1086
	    (bool) ((cmd == SIO_HI_RA_RAM_CMD_CONFIG) &&
		    ((state->m_HICfgCtrl) &
		     SIO_HI_RA_RAM_PAR_5_CFG_SLEEP__M) ==
		    SIO_HI_RA_RAM_PAR_5_CFG_SLEEP_ZZZ);
R
Ralph Metzler 已提交
1087 1088 1089 1090 1091 1092 1093 1094
	if (powerdown_cmd == false) {
		/* Wait until command rdy */
		u32 retryCount = 0;
		u16 waitCmd;

		do {
			msleep(1);
			retryCount += 1;
1095 1096
			status = read16(state, SIO_HI_RA_RAM_CMD__A,
					  &waitCmd);
1097 1098
		} while ((status < 0) && (retryCount < DRXK_MAX_RETRIES)
			 && (waitCmd != 0));
R
Ralph Metzler 已提交
1099 1100

		if (status == 0)
1101 1102
			status = read16(state, SIO_HI_RA_RAM_RES__A,
					pResult);
R
Ralph Metzler 已提交
1103 1104 1105 1106 1107 1108 1109 1110
	}
	return status;
}

static int HI_CfgCommand(struct drxk_state *state)
{
	int status;

1111 1112
	dprintk(1, "\n");

R
Ralph Metzler 已提交
1113 1114
	mutex_lock(&state->mutex);
	do {
1115
		status = write16(state, SIO_HI_RA_RAM_PAR_6__A, state->m_HICfgTimeout);
1116 1117
		if (status < 0)
			break;
1118
		status = write16(state, SIO_HI_RA_RAM_PAR_5__A, state->m_HICfgCtrl);
1119 1120
		if (status < 0)
			break;
1121
		status = write16(state, SIO_HI_RA_RAM_PAR_4__A, state->m_HICfgWakeUpKey);
1122 1123
		if (status < 0)
			break;
1124
		status = write16(state, SIO_HI_RA_RAM_PAR_3__A, state->m_HICfgBridgeDelay);
1125 1126
		if (status < 0)
			break;
1127
		status = write16(state, SIO_HI_RA_RAM_PAR_2__A, state->m_HICfgTimingDiv);
1128 1129
		if (status < 0)
			break;
1130
		status = write16(state, SIO_HI_RA_RAM_PAR_1__A, SIO_HI_RA_RAM_PAR_1_PAR1_SEC_KEY);
1131 1132 1133 1134 1135
		if (status < 0)
			break;
		status = HI_Command(state, SIO_HI_RA_RAM_CMD_CONFIG, 0);
		if (status < 0)
			break;
R
Ralph Metzler 已提交
1136 1137

		state->m_HICfgCtrl &= ~SIO_HI_RA_RAM_PAR_5_CFG_SLEEP_ZZZ;
1138
	} while (0);
R
Ralph Metzler 已提交
1139 1140 1141 1142 1143 1144
	mutex_unlock(&state->mutex);
	return status;
}

static int InitHI(struct drxk_state *state)
{
1145 1146
	dprintk(1, "\n");

1147
	state->m_HICfgWakeUpKey = (state->demod_address << 1);
R
Ralph Metzler 已提交
1148 1149 1150
	state->m_HICfgTimeout = 0x96FF;
	/* port/bridge/power down ctrl */
	state->m_HICfgCtrl = SIO_HI_RA_RAM_PAR_5_CFG_SLV0_SLAVE;
1151
	return HI_CfgCommand(state);
R
Ralph Metzler 已提交
1152 1153 1154 1155 1156
}

static int MPEGTSConfigurePins(struct drxk_state *state, bool mpegEnable)
{
	int status = -1;
1157 1158
	u16 sioPdrMclkCfg = 0;
	u16 sioPdrMdxCfg = 0;
R
Ralph Metzler 已提交
1159

1160
	dprintk(1, "\n");
R
Ralph Metzler 已提交
1161 1162
	do {
		/* stop lock indicator process */
1163
		status = write16(state, SCU_RAM_GPIO__A, SCU_RAM_GPIO_HW_LOCK_IND_DISABLE);
1164 1165
		if (status < 0)
			break;
R
Ralph Metzler 已提交
1166 1167

		/*  MPEG TS pad configuration */
1168
		status = write16(state, SIO_TOP_COMM_KEY__A, 0xFABA);
1169 1170
		if (status < 0)
			break;
R
Ralph Metzler 已提交
1171 1172 1173

		if (mpegEnable == false) {
			/*  Set MPEG TS pads to inputmode */
1174
			status = write16(state, SIO_PDR_MSTRT_CFG__A, 0x0000);
1175 1176
			if (status < 0)
				break;
1177
			status = write16(state, SIO_PDR_MERR_CFG__A, 0x0000);
1178 1179
			if (status < 0)
				break;
1180
			status = write16(state, SIO_PDR_MCLK_CFG__A, 0x0000);
1181 1182
			if (status < 0)
				break;
1183
			status = write16(state, SIO_PDR_MVAL_CFG__A, 0x0000);
1184 1185
			if (status < 0)
				break;
1186
			status = write16(state, SIO_PDR_MD0_CFG__A, 0x0000);
1187 1188
			if (status < 0)
				break;
1189
			status = write16(state, SIO_PDR_MD1_CFG__A, 0x0000);
1190 1191
			if (status < 0)
				break;
1192
			status = write16(state, SIO_PDR_MD2_CFG__A, 0x0000);
1193 1194
			if (status < 0)
				break;
1195
			status = write16(state, SIO_PDR_MD3_CFG__A, 0x0000);
1196 1197
			if (status < 0)
				break;
1198
			status = write16(state, SIO_PDR_MD4_CFG__A, 0x0000);
1199 1200
			if (status < 0)
				break;
1201
			status = write16(state, SIO_PDR_MD5_CFG__A, 0x0000);
1202 1203
			if (status < 0)
				break;
1204
			status = write16(state, SIO_PDR_MD6_CFG__A, 0x0000);
1205 1206
			if (status < 0)
				break;
1207
			status = write16(state, SIO_PDR_MD7_CFG__A, 0x0000);
1208 1209
			if (status < 0)
				break;
R
Ralph Metzler 已提交
1210 1211 1212
		} else {
			/* Enable MPEG output */
			sioPdrMdxCfg =
1213 1214
			    ((state->m_TSDataStrength <<
			      SIO_PDR_MD0_CFG_DRIVE__B) | 0x0003);
R
Ralph Metzler 已提交
1215
			sioPdrMclkCfg = ((state->m_TSClockkStrength <<
1216 1217
					  SIO_PDR_MCLK_CFG_DRIVE__B) |
					 0x0003);
R
Ralph Metzler 已提交
1218

1219
			status = write16(state, SIO_PDR_MSTRT_CFG__A, sioPdrMdxCfg);
1220 1221
			if (status < 0)
				break;
1222
			status = write16(state, SIO_PDR_MERR_CFG__A, 0x0000);	/* Disable */
1223 1224
			if (status < 0)
				break;
1225
			status = write16(state, SIO_PDR_MVAL_CFG__A, 0x0000);	/* Disable */
1226 1227
			if (status < 0)
				break;
R
Ralph Metzler 已提交
1228 1229
			if (state->m_enableParallel == true) {
				/* paralel -> enable MD1 to MD7 */
1230
				status = write16(state, SIO_PDR_MD1_CFG__A, sioPdrMdxCfg);
1231 1232
				if (status < 0)
					break;
1233
				status = write16(state, SIO_PDR_MD2_CFG__A, sioPdrMdxCfg);
1234 1235
				if (status < 0)
					break;
1236
				status = write16(state, SIO_PDR_MD3_CFG__A, sioPdrMdxCfg);
1237 1238
				if (status < 0)
					break;
1239
				status = write16(state, SIO_PDR_MD4_CFG__A, sioPdrMdxCfg);
1240 1241
				if (status < 0)
					break;
1242
				status = write16(state, SIO_PDR_MD5_CFG__A, sioPdrMdxCfg);
1243 1244
				if (status < 0)
					break;
1245
				status = write16(state, SIO_PDR_MD6_CFG__A, sioPdrMdxCfg);
1246 1247
				if (status < 0)
					break;
1248
				status = write16(state, SIO_PDR_MD7_CFG__A, sioPdrMdxCfg);
1249 1250
				if (status < 0)
					break;
R
Ralph Metzler 已提交
1251
			} else {
1252 1253 1254
				sioPdrMdxCfg = ((state->m_TSDataStrength <<
						 SIO_PDR_MD0_CFG_DRIVE__B)
						| 0x0003);
R
Ralph Metzler 已提交
1255
				/* serial -> disable MD1 to MD7 */
1256
				status = write16(state, SIO_PDR_MD1_CFG__A, 0x0000);
1257 1258
				if (status < 0)
					break;
1259
				status = write16(state, SIO_PDR_MD2_CFG__A, 0x0000);
1260 1261
				if (status < 0)
					break;
1262
				status = write16(state, SIO_PDR_MD3_CFG__A, 0x0000);
1263 1264
				if (status < 0)
					break;
1265
				status = write16(state, SIO_PDR_MD4_CFG__A, 0x0000);
1266 1267
				if (status < 0)
					break;
1268
				status = write16(state, SIO_PDR_MD5_CFG__A, 0x0000);
1269 1270
				if (status < 0)
					break;
1271
				status = write16(state, SIO_PDR_MD6_CFG__A, 0x0000);
1272 1273
				if (status < 0)
					break;
1274
				status = write16(state, SIO_PDR_MD7_CFG__A, 0x0000);
1275 1276
				if (status < 0)
					break;
R
Ralph Metzler 已提交
1277
			}
1278
			status = write16(state, SIO_PDR_MCLK_CFG__A, sioPdrMclkCfg);
1279 1280
			if (status < 0)
				break;
1281
			status = write16(state, SIO_PDR_MD0_CFG__A, sioPdrMdxCfg);
1282 1283
			if (status < 0)
				break;
R
Ralph Metzler 已提交
1284 1285
		}
		/*  Enable MB output over MPEG pads and ctl input */
1286
		status = write16(state, SIO_PDR_MON_CFG__A, 0x0000);
1287 1288
		if (status < 0)
			break;
R
Ralph Metzler 已提交
1289
		/*  Write nomagic word to enable pdr reg write */
1290
		status = write16(state, SIO_TOP_COMM_KEY__A, 0x0000);
1291 1292
		if (status < 0)
			break;
1293
	} while (0);
R
Ralph Metzler 已提交
1294 1295 1296 1297 1298
	return status;
}

static int MPEGTSDisable(struct drxk_state *state)
{
1299 1300
	dprintk(1, "\n");

R
Ralph Metzler 已提交
1301 1302 1303 1304 1305 1306 1307 1308 1309 1310
	return MPEGTSConfigurePins(state, false);
}

static int BLChainCmd(struct drxk_state *state,
		      u16 romOffset, u16 nrOfElements, u32 timeOut)
{
	u16 blStatus = 0;
	int status;
	unsigned long end;

1311 1312
	dprintk(1, "\n");

R
Ralph Metzler 已提交
1313 1314
	mutex_lock(&state->mutex);
	do {
1315
		status = write16(state, SIO_BL_MODE__A, SIO_BL_MODE_CHAIN);
1316 1317
		if (status < 0)
			break;
1318
		status = write16(state, SIO_BL_CHAIN_ADDR__A, romOffset);
1319 1320
		if (status < 0)
			break;
1321
		status = write16(state, SIO_BL_CHAIN_LEN__A, nrOfElements);
1322 1323
		if (status < 0)
			break;
1324
		status = write16(state, SIO_BL_ENABLE__A, SIO_BL_ENABLE_ON);
1325 1326
		if (status < 0)
			break;
1327
		end = jiffies + msecs_to_jiffies(timeOut);
R
Ralph Metzler 已提交
1328 1329 1330

		do {
			msleep(1);
1331
			status = read16(state, SIO_BL_STATUS__A, &blStatus);
1332 1333
			if (status < 0)
				break;
R
Ralph Metzler 已提交
1334 1335 1336
		} while ((blStatus == 0x1) &&
			 ((time_is_after_jiffies(end))));
		if (blStatus == 0x1) {
1337
			printk(KERN_ERR "drxk: SIO not ready\n");
R
Ralph Metzler 已提交
1338 1339 1340
			mutex_unlock(&state->mutex);
			return -1;
		}
1341
	} while (0);
R
Ralph Metzler 已提交
1342 1343 1344 1345 1346 1347
	mutex_unlock(&state->mutex);
	return status;
}


static int DownloadMicrocode(struct drxk_state *state,
1348
			     const u8 pMCImage[], u32 Length)
R
Ralph Metzler 已提交
1349 1350 1351 1352 1353 1354 1355 1356 1357 1358
{
	const u8 *pSrc = pMCImage;
	u16 Flags;
	u16 Drain;
	u32 Address;
	u16 nBlocks;
	u16 BlockSize;
	u16 BlockCRC;
	u32 offset = 0;
	u32 i;
1359
	int status = 0;
R
Ralph Metzler 已提交
1360

1361 1362
	dprintk(1, "\n");

R
Ralph Metzler 已提交
1363 1364
	/* down the drain (we don care about MAGIC_WORD) */
	Drain = (pSrc[0] << 8) | pSrc[1];
1365 1366
	pSrc += sizeof(u16);
	offset += sizeof(u16);
R
Ralph Metzler 已提交
1367
	nBlocks = (pSrc[0] << 8) | pSrc[1];
1368 1369
	pSrc += sizeof(u16);
	offset += sizeof(u16);
R
Ralph Metzler 已提交
1370 1371 1372

	for (i = 0; i < nBlocks; i += 1) {
		Address = (pSrc[0] << 24) | (pSrc[1] << 16) |
1373 1374 1375
		    (pSrc[2] << 8) | pSrc[3];
		pSrc += sizeof(u32);
		offset += sizeof(u32);
R
Ralph Metzler 已提交
1376 1377

		BlockSize = ((pSrc[0] << 8) | pSrc[1]) * sizeof(u16);
1378 1379
		pSrc += sizeof(u16);
		offset += sizeof(u16);
R
Ralph Metzler 已提交
1380 1381

		Flags = (pSrc[0] << 8) | pSrc[1];
1382 1383
		pSrc += sizeof(u16);
		offset += sizeof(u16);
R
Ralph Metzler 已提交
1384 1385

		BlockCRC = (pSrc[0] << 8) | pSrc[1];
1386 1387
		pSrc += sizeof(u16);
		offset += sizeof(u16);
1388
		status = write_block(state, Address, BlockSize, pSrc);
1389
		if (status < 0)
R
Ralph Metzler 已提交
1390 1391 1392 1393 1394 1395 1396 1397 1398 1399
			break;
		pSrc += BlockSize;
		offset += BlockSize;
	}
	return status;
}

static int DVBTEnableOFDMTokenRing(struct drxk_state *state, bool enable)
{
	int status;
1400 1401
	u16 data = 0;
	u16 desiredCtrl = SIO_OFDM_SH_OFDM_RING_ENABLE_ON;
R
Ralph Metzler 已提交
1402 1403 1404
	u16 desiredStatus = SIO_OFDM_SH_OFDM_RING_STATUS_ENABLED;
	unsigned long end;

1405 1406
	dprintk(1, "\n");

R
Ralph Metzler 已提交
1407
	if (enable == false) {
1408
		desiredCtrl = SIO_OFDM_SH_OFDM_RING_ENABLE_OFF;
R
Ralph Metzler 已提交
1409 1410 1411
		desiredStatus = SIO_OFDM_SH_OFDM_RING_STATUS_DOWN;
	}

1412
	status = (read16(state, SIO_OFDM_SH_OFDM_RING_STATUS__A, &data));
R
Ralph Metzler 已提交
1413 1414 1415 1416 1417 1418

	if (data == desiredStatus) {
		/* tokenring already has correct status */
		return status;
	}
	/* Disable/enable dvbt tokenring bridge   */
1419
	status =
1420
	    write16(state, SIO_OFDM_SH_OFDM_RING_ENABLE__A, desiredCtrl);
R
Ralph Metzler 已提交
1421

1422
	end = jiffies + msecs_to_jiffies(DRXK_OFDM_TR_SHUTDOWN_TIMEOUT);
1423
	do {
1424
		status = read16(state, SIO_OFDM_SH_OFDM_RING_STATUS__A, &data);
1425 1426 1427
		if (status < 0)
			break;
	} while ((data != desiredStatus) && ((time_is_after_jiffies(end))));
R
Ralph Metzler 已提交
1428
	if (data != desiredStatus) {
1429
		printk(KERN_ERR "drxk: SIO not ready\n");
R
Ralph Metzler 已提交
1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440
		return -1;
	}
	return status;
}

static int MPEGTSStop(struct drxk_state *state)
{
	int status = 0;
	u16 fecOcSncMode = 0;
	u16 fecOcIprMode = 0;

1441 1442
	dprintk(1, "\n");

R
Ralph Metzler 已提交
1443 1444
	do {
		/* Gracefull shutdown (byte boundaries) */
1445
		status = read16(state, FEC_OC_SNC_MODE__A, &fecOcSncMode);
1446 1447
		if (status < 0)
			break;
R
Ralph Metzler 已提交
1448
		fecOcSncMode |= FEC_OC_SNC_MODE_SHUTDOWN__M;
1449
		status = write16(state, FEC_OC_SNC_MODE__A, fecOcSncMode);
1450 1451
		if (status < 0)
			break;
R
Ralph Metzler 已提交
1452 1453

		/* Suppress MCLK during absence of data */
1454
		status = read16(state, FEC_OC_IPR_MODE__A, &fecOcIprMode);
1455 1456
		if (status < 0)
			break;
R
Ralph Metzler 已提交
1457
		fecOcIprMode |= FEC_OC_IPR_MODE_MCLK_DIS_DAT_ABS__M;
1458
		status = write16(state, FEC_OC_IPR_MODE__A, fecOcIprMode);
1459 1460
		if (status < 0)
			break;
R
Ralph Metzler 已提交
1461 1462 1463 1464 1465 1466
	} while (0);
	return status;
}

static int scu_command(struct drxk_state *state,
		       u16 cmd, u8 parameterLen,
1467
		       u16 *parameter, u8 resultLen, u16 *result)
R
Ralph Metzler 已提交
1468 1469 1470 1471 1472 1473 1474 1475
{
#if (SCU_RAM_PARAM_0__A - SCU_RAM_PARAM_15__A) != 15
#error DRXK register mapping no longer compatible with this routine!
#endif
	u16 curCmd = 0;
	int status;
	unsigned long end;

1476 1477
	dprintk(1, "\n");

R
Ralph Metzler 已提交
1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488
	if ((cmd == 0) || ((parameterLen > 0) && (parameter == NULL)) ||
	    ((resultLen > 0) && (result == NULL)))
		return -1;

	mutex_lock(&state->mutex);
	do {
		/* assume that the command register is ready
		   since it is checked afterwards */
		u8 buffer[34];
		int cnt = 0, ii;

1489
		for (ii = parameterLen - 1; ii >= 0; ii -= 1) {
R
Ralph Metzler 已提交
1490 1491 1492 1493 1494 1495
			buffer[cnt++] = (parameter[ii] & 0xFF);
			buffer[cnt++] = ((parameter[ii] >> 8) & 0xFF);
		}
		buffer[cnt++] = (cmd & 0xFF);
		buffer[cnt++] = ((cmd >> 8) & 0xFF);

1496 1497
		write_block(state, SCU_RAM_PARAM_0__A -
			   (parameterLen - 1), cnt, buffer);
R
Ralph Metzler 已提交
1498
		/* Wait until SCU has processed command */
1499
		end = jiffies + msecs_to_jiffies(DRXK_MAX_WAITTIME);
R
Ralph Metzler 已提交
1500 1501
		do {
			msleep(1);
1502
			status = read16(state, SCU_RAM_COMMAND__A, &curCmd);
1503 1504
			if (status < 0)
				break;
1505 1506
		} while (!(curCmd == DRX_SCU_READY)
			 && (time_is_after_jiffies(end)));
R
Ralph Metzler 已提交
1507
		if (curCmd != DRX_SCU_READY) {
1508
			printk(KERN_ERR "drxk: SCU not ready\n");
R
Ralph Metzler 已提交
1509 1510 1511 1512 1513 1514 1515 1516
			mutex_unlock(&state->mutex);
			return -1;
		}
		/* read results */
		if ((resultLen > 0) && (result != NULL)) {
			s16 err;
			int ii;

1517
			for (ii = resultLen - 1; ii >= 0; ii -= 1) {
1518
				status = read16(state, SCU_RAM_PARAM_0__A - ii, &result[ii]);
1519 1520
				if (status < 0)
					break;
R
Ralph Metzler 已提交
1521 1522 1523
			}

			/* Check if an error was reported by SCU */
1524
			err = (s16) result[0];
R
Ralph Metzler 已提交
1525 1526 1527

			/* check a few fixed error codes */
			if (err == SCU_RESULT_UNKSTD) {
1528
				printk(KERN_ERR "drxk: SCU_RESULT_UNKSTD\n");
R
Ralph Metzler 已提交
1529 1530 1531
				mutex_unlock(&state->mutex);
				return -1;
			} else if (err == SCU_RESULT_UNKCMD) {
1532
				printk(KERN_ERR "drxk: SCU_RESULT_UNKCMD\n");
R
Ralph Metzler 已提交
1533 1534 1535 1536 1537 1538
				mutex_unlock(&state->mutex);
				return -1;
			}
			/* here it is assumed that negative means error,
			   and positive no error */
			else if (err < 0) {
1539
				printk(KERN_ERR "drxk: %s ERROR\n", __func__);
R
Ralph Metzler 已提交
1540 1541 1542 1543
				mutex_unlock(&state->mutex);
				return -1;
			}
		}
1544
	} while (0);
R
Ralph Metzler 已提交
1545
	mutex_unlock(&state->mutex);
1546
	if (status < 0)
1547
		printk(KERN_ERR "drxk: %s: status = %d\n", __func__, status);
R
Ralph Metzler 已提交
1548 1549 1550 1551 1552 1553 1554 1555 1556

	return status;
}

static int SetIqmAf(struct drxk_state *state, bool active)
{
	u16 data = 0;
	int status;

1557 1558
	dprintk(1, "\n");

1559
	do {
R
Ralph Metzler 已提交
1560
		/* Configure IQM */
1561
		status = read16(state, IQM_AF_STDBY__A, &data);
1562 1563
		if (status < 0)
			break;
R
Ralph Metzler 已提交
1564 1565 1566 1567 1568
		if (!active) {
			data |= (IQM_AF_STDBY_STDBY_ADC_STANDBY
				 | IQM_AF_STDBY_STDBY_AMP_STANDBY
				 | IQM_AF_STDBY_STDBY_PD_STANDBY
				 | IQM_AF_STDBY_STDBY_TAGC_IF_STANDBY
1569 1570 1571
				 | IQM_AF_STDBY_STDBY_TAGC_RF_STANDBY);
		} else {	/* active */

R
Ralph Metzler 已提交
1572 1573 1574 1575 1576
			data &= ((~IQM_AF_STDBY_STDBY_ADC_STANDBY)
				 & (~IQM_AF_STDBY_STDBY_AMP_STANDBY)
				 & (~IQM_AF_STDBY_STDBY_PD_STANDBY)
				 & (~IQM_AF_STDBY_STDBY_TAGC_IF_STANDBY)
				 & (~IQM_AF_STDBY_STDBY_TAGC_RF_STANDBY)
1577
			    );
R
Ralph Metzler 已提交
1578
		}
1579
		status = write16(state, IQM_AF_STDBY__A, data);
1580 1581
		if (status < 0)
			break;
1582
	} while (0);
R
Ralph Metzler 已提交
1583 1584 1585
	return status;
}

1586
static int CtrlPowerMode(struct drxk_state *state, enum DRXPowerMode *mode)
R
Ralph Metzler 已提交
1587 1588
{
	int status = 0;
1589
	u16 sioCcPwdMode = 0;
R
Ralph Metzler 已提交
1590

1591 1592
	dprintk(1, "\n");

R
Ralph Metzler 已提交
1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623
	/* Check arguments */
	if (mode == NULL)
		return -1;

	switch (*mode) {
	case DRX_POWER_UP:
		sioCcPwdMode = SIO_CC_PWD_MODE_LEVEL_NONE;
		break;
	case DRXK_POWER_DOWN_OFDM:
		sioCcPwdMode = SIO_CC_PWD_MODE_LEVEL_OFDM;
		break;
	case DRXK_POWER_DOWN_CORE:
		sioCcPwdMode = SIO_CC_PWD_MODE_LEVEL_CLOCK;
		break;
	case DRXK_POWER_DOWN_PLL:
		sioCcPwdMode = SIO_CC_PWD_MODE_LEVEL_PLL;
		break;
	case DRX_POWER_DOWN:
		sioCcPwdMode = SIO_CC_PWD_MODE_LEVEL_OSC;
		break;
	default:
		/* Unknow sleep mode */
		return -1;
		break;
	}

	/* If already in requested power mode, do nothing */
	if (state->m_currentPowerMode == *mode)
		return 0;

	/* For next steps make sure to start from DRX_POWER_UP mode */
1624
	if (state->m_currentPowerMode != DRX_POWER_UP) {
R
Ralph Metzler 已提交
1625
		do {
1626 1627 1628 1629 1630 1631
			status = PowerUpDevice(state);
			if (status < 0)
				break;
			status = DVBTEnableOFDMTokenRing(state, true);
			if (status < 0)
				break;
1632
		} while (0);
R
Ralph Metzler 已提交
1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647
	}

	if (*mode == DRX_POWER_UP) {
		/* Restore analog & pin configuartion */
	} else {
		/* Power down to requested mode */
		/* Backup some register settings */
		/* Set pins with possible pull-ups connected
		   to them in input mode */
		/* Analog power down */
		/* ADC power down */
		/* Power down device */
		/* stop all comm_exec */
		/* Stop and power down previous standard */
		do {
1648
			switch (state->m_OperationMode) {
R
Ralph Metzler 已提交
1649
			case OM_DVBT:
1650 1651 1652 1653 1654 1655
				status = MPEGTSStop(state);
				if (status < 0)
					break;
				status = PowerDownDVBT(state, false);
				if (status < 0)
					break;
R
Ralph Metzler 已提交
1656 1657 1658
				break;
			case OM_QAM_ITU_A:
			case OM_QAM_ITU_C:
1659 1660 1661 1662 1663 1664
				status = MPEGTSStop(state);
				if (status < 0)
					break;
				status = PowerDownQAM(state);
				if (status < 0)
					break;
R
Ralph Metzler 已提交
1665 1666 1667 1668
				break;
			default:
				break;
			}
1669 1670 1671
			status = DVBTEnableOFDMTokenRing(state, false);
			if (status < 0)
				break;
1672
			status = write16(state, SIO_CC_PWD_MODE__A, sioCcPwdMode);
1673 1674
			if (status < 0)
				break;
1675
			status = write16(state, SIO_CC_UPDATE__A, SIO_CC_UPDATE_KEY);
1676 1677
			if (status < 0)
				break;
R
Ralph Metzler 已提交
1678

1679
			if (*mode != DRXK_POWER_DOWN_OFDM) {
R
Ralph Metzler 已提交
1680
				state->m_HICfgCtrl |=
1681
				    SIO_HI_RA_RAM_PAR_5_CFG_SLEEP_ZZZ;
1682 1683 1684
				status = HI_CfgCommand(state);
				if (status < 0)
					break;
R
Ralph Metzler 已提交
1685
			}
1686
		} while (0);
R
Ralph Metzler 已提交
1687 1688
	}
	state->m_currentPowerMode = *mode;
1689
	return status;
R
Ralph Metzler 已提交
1690 1691 1692 1693
}

static int PowerDownDVBT(struct drxk_state *state, bool setPowerMode)
{
1694
	enum DRXPowerMode powerMode = DRXK_POWER_DOWN_OFDM;
R
Ralph Metzler 已提交
1695 1696 1697 1698
	u16 cmdResult = 0;
	u16 data = 0;
	int status;

1699 1700
	dprintk(1, "\n");

R
Ralph Metzler 已提交
1701
	do {
1702
		status = read16(state, SCU_COMM_EXEC__A, &data);
1703 1704
		if (status < 0)
			break;
R
Ralph Metzler 已提交
1705 1706
		if (data == SCU_COMM_EXEC_ACTIVE) {
			/* Send OFDM stop command */
1707 1708 1709
			status = scu_command(state, SCU_RAM_COMMAND_STANDARD_OFDM | SCU_RAM_COMMAND_CMD_DEMOD_STOP, 0, NULL, 1, &cmdResult);
			if (status < 0)
				break;
R
Ralph Metzler 已提交
1710
			/* Send OFDM reset command */
1711 1712 1713
			status = scu_command(state, SCU_RAM_COMMAND_STANDARD_OFDM | SCU_RAM_COMMAND_CMD_DEMOD_RESET, 0, NULL, 1, &cmdResult);
			if (status < 0)
				break;
R
Ralph Metzler 已提交
1714 1715 1716
		}

		/* Reset datapath for OFDM, processors first */
1717
		status = write16(state, OFDM_SC_COMM_EXEC__A, OFDM_SC_COMM_EXEC_STOP);
1718 1719
		if (status < 0)
			break;
1720
		status = write16(state, OFDM_LC_COMM_EXEC__A, OFDM_LC_COMM_EXEC_STOP);
1721 1722
		if (status < 0)
			break;
1723
		status = write16(state, IQM_COMM_EXEC__A, IQM_COMM_EXEC_B_STOP);
1724 1725
		if (status < 0)
			break;
R
Ralph Metzler 已提交
1726 1727

		/* powerdown AFE                   */
1728 1729 1730
		status = SetIqmAf(state, false);
		if (status < 0)
			break;
R
Ralph Metzler 已提交
1731 1732 1733

		/* powerdown to OFDM mode          */
		if (setPowerMode) {
1734 1735 1736
			status = CtrlPowerMode(state, &powerMode);
			if (status < 0)
				break;
R
Ralph Metzler 已提交
1737
		}
1738
	} while (0);
R
Ralph Metzler 已提交
1739 1740 1741
	return status;
}

1742 1743
static int SetOperationMode(struct drxk_state *state,
			    enum OperationMode oMode)
R
Ralph Metzler 已提交
1744 1745 1746
{
	int status = 0;

1747
	dprintk(1, "\n");
R
Ralph Metzler 已提交
1748
	/*
1749 1750 1751 1752
	   Stop and power down previous standard
	   TODO investigate total power down instead of partial
	   power down depending on "previous" standard.
	 */
R
Ralph Metzler 已提交
1753 1754
	do {
		/* disable HW lock indicator */
1755
		status = write16(state, SCU_RAM_GPIO__A, SCU_RAM_GPIO_HW_LOCK_IND_DISABLE);
1756 1757
		if (status < 0)
			break;
R
Ralph Metzler 已提交
1758 1759

		if (state->m_OperationMode != oMode) {
1760 1761
			switch (state->m_OperationMode) {
				/* OM_NONE was added for start up */
R
Ralph Metzler 已提交
1762 1763 1764
			case OM_NONE:
				break;
			case OM_DVBT:
1765 1766 1767 1768 1769 1770
				status = MPEGTSStop(state);
				if (status < 0)
					break;
				status = PowerDownDVBT(state, true);
				if (status < 0)
					break;
R
Ralph Metzler 已提交
1771 1772 1773 1774 1775
				state->m_OperationMode = OM_NONE;
				break;
			case OM_QAM_ITU_B:
				status = -1;
				break;
1776
			case OM_QAM_ITU_A:	/* fallthrough */
R
Ralph Metzler 已提交
1777
			case OM_QAM_ITU_C:
1778 1779 1780 1781 1782 1783
				status = MPEGTSStop(state);
				if (status < 0)
					break;
				status = PowerDownQAM(state);
				if (status < 0)
					break;
R
Ralph Metzler 已提交
1784 1785 1786 1787 1788
				state->m_OperationMode = OM_NONE;
				break;
			default:
				status = -1;
			}
1789 1790 1791
			status = status;
			if (status < 0)
				break;
R
Ralph Metzler 已提交
1792 1793

			/*
1794 1795 1796
			   Power up new standard
			 */
			switch (oMode) {
R
Ralph Metzler 已提交
1797 1798
			case OM_DVBT:
				state->m_OperationMode = oMode;
1799 1800 1801
				status = SetDVBTStandard(state, oMode);
				if (status < 0)
					break;
R
Ralph Metzler 已提交
1802 1803 1804 1805
				break;
			case OM_QAM_ITU_B:
				status = -1;
				break;
1806
			case OM_QAM_ITU_A:	/* fallthrough */
R
Ralph Metzler 已提交
1807 1808
			case OM_QAM_ITU_C:
				state->m_OperationMode = oMode;
1809 1810 1811
				status = SetQAMStandard(state, oMode);
				if (status < 0)
					break;
R
Ralph Metzler 已提交
1812 1813 1814 1815 1816
				break;
			default:
				status = -1;
			}
		}
1817 1818 1819
		status = status;
		if (status < 0)
			break;
1820
	} while (0);
R
Ralph Metzler 已提交
1821 1822 1823 1824 1825 1826
	return 0;
}

static int Start(struct drxk_state *state, s32 offsetFreq,
		 s32 IntermediateFrequency)
{
1827
	int status = 0;
R
Ralph Metzler 已提交
1828

1829
	dprintk(1, "\n");
R
Ralph Metzler 已提交
1830 1831
	do {
		u16 IFreqkHz;
1832
		s32 OffsetkHz = offsetFreq / 1000;
R
Ralph Metzler 已提交
1833 1834 1835 1836 1837 1838 1839

		if (state->m_DrxkState != DRXK_STOPPED &&
		    state->m_DrxkState != DRXK_DTV_STARTED) {
			status = -1;
			break;
		}
		state->m_bMirrorFreqSpect =
1840
		    (state->param.inversion == INVERSION_ON);
R
Ralph Metzler 已提交
1841 1842

		if (IntermediateFrequency < 0) {
1843 1844
			state->m_bMirrorFreqSpect =
			    !state->m_bMirrorFreqSpect;
R
Ralph Metzler 已提交
1845 1846 1847
			IntermediateFrequency = -IntermediateFrequency;
		}

1848
		switch (state->m_OperationMode) {
R
Ralph Metzler 已提交
1849 1850 1851
		case OM_QAM_ITU_A:
		case OM_QAM_ITU_C:
			IFreqkHz = (IntermediateFrequency / 1000);
1852 1853 1854
			status = SetQAM(state, IFreqkHz, OffsetkHz);
			if (status < 0)
				break;
R
Ralph Metzler 已提交
1855 1856 1857 1858
			state->m_DrxkState = DRXK_DTV_STARTED;
			break;
		case OM_DVBT:
			IFreqkHz = (IntermediateFrequency / 1000);
1859 1860 1861 1862 1863 1864 1865 1866 1867
			status = MPEGTSStop(state);
			if (status < 0)
				break;
			status = SetDVBT(state, IFreqkHz, OffsetkHz);
			if (status < 0)
				break;
			status = DVBTStart(state);
			if (status < 0)
				break;
R
Ralph Metzler 已提交
1868 1869 1870 1871 1872
			state->m_DrxkState = DRXK_DTV_STARTED;
			break;
		default:
			break;
		}
1873
	} while (0);
R
Ralph Metzler 已提交
1874 1875 1876 1877 1878
	return status;
}

static int ShutDown(struct drxk_state *state)
{
1879 1880
	dprintk(1, "\n");

R
Ralph Metzler 已提交
1881 1882 1883 1884
	MPEGTSStop(state);
	return 0;
}

1885 1886
static int GetLockStatus(struct drxk_state *state, u32 *pLockStatus,
			 u32 Time)
R
Ralph Metzler 已提交
1887
{
1888
	int status = 0;
R
Ralph Metzler 已提交
1889

1890 1891
	dprintk(1, "\n");

R
Ralph Metzler 已提交
1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920
	if (pLockStatus == NULL)
		return -1;

	*pLockStatus = NOT_LOCKED;

	/* define the SCU command code */
	switch (state->m_OperationMode) {
	case OM_QAM_ITU_A:
	case OM_QAM_ITU_B:
	case OM_QAM_ITU_C:
		status = GetQAMLockStatus(state, pLockStatus);
		break;
	case OM_DVBT:
		status = GetDVBTLockStatus(state, pLockStatus);
		break;
	default:
		break;
	}
	return status;
}

static int MPEGTSStart(struct drxk_state *state)
{
	int status = 0;

	u16 fecOcSncMode = 0;

	do {
		/* Allow OC to sync again */
1921
		status = read16(state, FEC_OC_SNC_MODE__A, &fecOcSncMode);
1922 1923
		if (status < 0)
			break;
R
Ralph Metzler 已提交
1924
		fecOcSncMode &= ~FEC_OC_SNC_MODE_SHUTDOWN__M;
1925
		status = write16(state, FEC_OC_SNC_MODE__A, fecOcSncMode);
1926 1927
		if (status < 0)
			break;
1928
		status = write16(state, FEC_OC_SNC_UNLOCK__A, 1);
1929 1930
		if (status < 0)
			break;
R
Ralph Metzler 已提交
1931 1932 1933 1934 1935 1936 1937 1938
	} while (0);
	return status;
}

static int MPEGTSDtoInit(struct drxk_state *state)
{
	int status = -1;

1939 1940
	dprintk(1, "\n");

R
Ralph Metzler 已提交
1941 1942
	do {
		/* Rate integration settings */
1943
		status = write16(state, FEC_OC_RCN_CTL_STEP_LO__A, 0x0000);
1944 1945
		if (status < 0)
			break;
1946
		status = write16(state, FEC_OC_RCN_CTL_STEP_HI__A, 0x000C);
1947 1948
		if (status < 0)
			break;
1949
		status = write16(state, FEC_OC_RCN_GAIN__A, 0x000A);
1950 1951
		if (status < 0)
			break;
1952
		status = write16(state, FEC_OC_AVR_PARM_A__A, 0x0008);
1953 1954
		if (status < 0)
			break;
1955
		status = write16(state, FEC_OC_AVR_PARM_B__A, 0x0006);
1956 1957
		if (status < 0)
			break;
1958
		status = write16(state, FEC_OC_TMD_HI_MARGIN__A, 0x0680);
1959 1960
		if (status < 0)
			break;
1961
		status = write16(state, FEC_OC_TMD_LO_MARGIN__A, 0x0080);
1962 1963
		if (status < 0)
			break;
1964
		status = write16(state, FEC_OC_TMD_COUNT__A, 0x03F4);
1965 1966
		if (status < 0)
			break;
R
Ralph Metzler 已提交
1967 1968

		/* Additional configuration */
1969
		status = write16(state, FEC_OC_OCR_INVERT__A, 0);
1970 1971
		if (status < 0)
			break;
1972
		status = write16(state, FEC_OC_SNC_LWM__A, 2);
1973 1974
		if (status < 0)
			break;
1975
		status = write16(state, FEC_OC_SNC_HWM__A, 12);
1976 1977
		if (status < 0)
			break;
R
Ralph Metzler 已提交
1978 1979 1980 1981
	} while (0);
	return status;
}

1982 1983
static int MPEGTSDtoSetup(struct drxk_state *state,
			  enum OperationMode oMode)
R
Ralph Metzler 已提交
1984 1985 1986
{
	int status = -1;

1987 1988 1989 1990 1991 1992 1993
	u16 fecOcRegMode = 0;	/* FEC_OC_MODE       register value */
	u16 fecOcRegIprMode = 0;	/* FEC_OC_IPR_MODE   register value */
	u16 fecOcDtoMode = 0;	/* FEC_OC_IPR_INVERT register value */
	u16 fecOcFctMode = 0;	/* FEC_OC_IPR_INVERT register value */
	u16 fecOcDtoPeriod = 2;	/* FEC_OC_IPR_INVERT register value */
	u16 fecOcDtoBurstLen = 188;	/* FEC_OC_IPR_INVERT register value */
	u32 fecOcRcnCtlRate = 0;	/* FEC_OC_IPR_INVERT register value */
R
Ralph Metzler 已提交
1994 1995
	u16 fecOcTmdMode = 0;
	u16 fecOcTmdIntUpdRate = 0;
1996
	u32 maxBitRate = 0;
R
Ralph Metzler 已提交
1997 1998
	bool staticCLK = false;

1999 2000
	dprintk(1, "\n");

R
Ralph Metzler 已提交
2001 2002
	do {
		/* Check insertion of the Reed-Solomon parity bytes */
2003
		status = read16(state, FEC_OC_MODE__A, &fecOcRegMode);
2004 2005
		if (status < 0)
			break;
2006
		status = read16(state, FEC_OC_IPR_MODE__A, &fecOcRegIprMode);
2007 2008
		if (status < 0)
			break;
2009
		fecOcRegMode &= (~FEC_OC_MODE_PARITY__M);
R
Ralph Metzler 已提交
2010 2011 2012
		fecOcRegIprMode &= (~FEC_OC_IPR_MODE_MVAL_DIS_PAR__M);
		if (state->m_insertRSByte == true) {
			/* enable parity symbol forward */
2013
			fecOcRegMode |= FEC_OC_MODE_PARITY__M;
R
Ralph Metzler 已提交
2014 2015 2016
			/* MVAL disable during parity bytes */
			fecOcRegIprMode |= FEC_OC_IPR_MODE_MVAL_DIS_PAR__M;
			/* TS burst length to 204 */
2017
			fecOcDtoBurstLen = 204;
R
Ralph Metzler 已提交
2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033
		}

		/* Check serial or parrallel output */
		fecOcRegIprMode &= (~(FEC_OC_IPR_MODE_SERIAL__M));
		if (state->m_enableParallel == false) {
			/* MPEG data output is serial -> set ipr_mode[0] */
			fecOcRegIprMode |= FEC_OC_IPR_MODE_SERIAL__M;
		}

		switch (oMode) {
		case OM_DVBT:
			maxBitRate = state->m_DVBTBitrate;
			fecOcTmdMode = 3;
			fecOcRcnCtlRate = 0xC00000;
			staticCLK = state->m_DVBTStaticCLK;
			break;
2034
		case OM_QAM_ITU_A:	/* fallthrough */
R
Ralph Metzler 已提交
2035 2036
		case OM_QAM_ITU_C:
			fecOcTmdMode = 0x0004;
2037
			fecOcRcnCtlRate = 0xD2B4EE;	/* good for >63 Mb/s */
R
Ralph Metzler 已提交
2038 2039 2040 2041 2042
			maxBitRate = state->m_DVBCBitrate;
			staticCLK = state->m_DVBCStaticCLK;
			break;
		default:
			status = -1;
2043
		}		/* switch (standard) */
2044 2045 2046
		status = status;
		if (status < 0)
			break;
R
Ralph Metzler 已提交
2047 2048

		/* Configure DTO's */
2049
		if (staticCLK) {
R
Ralph Metzler 已提交
2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062
			u32 bitRate = 0;

			/* Rational DTO for MCLK source (static MCLK rate),
			   Dynamic DTO for optimal grouping
			   (avoid intra-packet gaps),
			   DTO offset enable to sync TS burst with MSTRT */
			fecOcDtoMode = (FEC_OC_DTO_MODE_DYNAMIC__M |
					FEC_OC_DTO_MODE_OFFSET_ENABLE__M);
			fecOcFctMode = (FEC_OC_FCT_MODE_RAT_ENA__M |
					FEC_OC_FCT_MODE_VIRT_ENA__M);

			/* Check user defined bitrate */
			bitRate = maxBitRate;
2063
			if (bitRate > 75900000UL) {	/* max is 75.9 Mb/s */
R
Ralph Metzler 已提交
2064 2065 2066 2067 2068 2069 2070
				bitRate = 75900000UL;
			}
			/* Rational DTO period:
			   dto_period = (Fsys / bitrate) - 2

			   Result should be floored,
			   to make sure >= requested bitrate
2071
			 */
R
Ralph Metzler 已提交
2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086
			fecOcDtoPeriod = (u16) (((state->m_sysClockFreq)
						 * 1000) / bitRate);
			if (fecOcDtoPeriod <= 2)
				fecOcDtoPeriod = 0;
			else
				fecOcDtoPeriod -= 2;
			fecOcTmdIntUpdRate = 8;
		} else {
			/* (commonAttr->staticCLK == false) => dynamic mode */
			fecOcDtoMode = FEC_OC_DTO_MODE_DYNAMIC__M;
			fecOcFctMode = FEC_OC_FCT_MODE__PRE;
			fecOcTmdIntUpdRate = 5;
		}

		/* Write appropriate registers with requested configuration */
2087
		status = write16(state, FEC_OC_DTO_BURST_LEN__A, fecOcDtoBurstLen);
2088 2089
		if (status < 0)
			break;
2090
		status = write16(state, FEC_OC_DTO_PERIOD__A, fecOcDtoPeriod);
2091 2092
		if (status < 0)
			break;
2093
		status = write16(state, FEC_OC_DTO_MODE__A, fecOcDtoMode);
2094 2095
		if (status < 0)
			break;
2096
		status = write16(state, FEC_OC_FCT_MODE__A, fecOcFctMode);
2097 2098
		if (status < 0)
			break;
2099
		status = write16(state, FEC_OC_MODE__A, fecOcRegMode);
2100 2101
		if (status < 0)
			break;
2102
		status = write16(state, FEC_OC_IPR_MODE__A, fecOcRegIprMode);
2103 2104
		if (status < 0)
			break;
R
Ralph Metzler 已提交
2105 2106

		/* Rate integration settings */
2107
		status = write32(state, FEC_OC_RCN_CTL_RATE_LO__A, fecOcRcnCtlRate);
2108 2109
		if (status < 0)
			break;
2110
		status = write16(state, FEC_OC_TMD_INT_UPD_RATE__A, fecOcTmdIntUpdRate);
2111 2112
		if (status < 0)
			break;
2113
		status = write16(state, FEC_OC_TMD_MODE__A, fecOcTmdMode);
2114 2115
		if (status < 0)
			break;
R
Ralph Metzler 已提交
2116 2117 2118 2119 2120 2121 2122
	} while (0);
	return status;
}

static int MPEGTSConfigurePolarity(struct drxk_state *state)
{
	int status;
2123
	u16 fecOcRegIprInvert = 0;
R
Ralph Metzler 已提交
2124

2125 2126
	dprintk(1, "\n");

R
Ralph Metzler 已提交
2127 2128
	/* Data mask for the output data byte */
	u16 InvertDataMask =
2129 2130 2131 2132
	    FEC_OC_IPR_INVERT_MD7__M | FEC_OC_IPR_INVERT_MD6__M |
	    FEC_OC_IPR_INVERT_MD5__M | FEC_OC_IPR_INVERT_MD4__M |
	    FEC_OC_IPR_INVERT_MD3__M | FEC_OC_IPR_INVERT_MD2__M |
	    FEC_OC_IPR_INVERT_MD1__M | FEC_OC_IPR_INVERT_MD0__M;
R
Ralph Metzler 已提交
2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149

	/* Control selective inversion of output bits */
	fecOcRegIprInvert &= (~(InvertDataMask));
	if (state->m_invertDATA == true)
		fecOcRegIprInvert |= InvertDataMask;
	fecOcRegIprInvert &= (~(FEC_OC_IPR_INVERT_MERR__M));
	if (state->m_invertERR == true)
		fecOcRegIprInvert |= FEC_OC_IPR_INVERT_MERR__M;
	fecOcRegIprInvert &= (~(FEC_OC_IPR_INVERT_MSTRT__M));
	if (state->m_invertSTR == true)
		fecOcRegIprInvert |= FEC_OC_IPR_INVERT_MSTRT__M;
	fecOcRegIprInvert &= (~(FEC_OC_IPR_INVERT_MVAL__M));
	if (state->m_invertVAL == true)
		fecOcRegIprInvert |= FEC_OC_IPR_INVERT_MVAL__M;
	fecOcRegIprInvert &= (~(FEC_OC_IPR_INVERT_MCLK__M));
	if (state->m_invertCLK == true)
		fecOcRegIprInvert |= FEC_OC_IPR_INVERT_MCLK__M;
2150
	status = write16(state, FEC_OC_IPR_INVERT__A, fecOcRegIprInvert);
R
Ralph Metzler 已提交
2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161
	return status;
}

#define   SCU_RAM_AGC_KI_INV_RF_POL__M 0x4000

static int SetAgcRf(struct drxk_state *state,
		    struct SCfgAgc *pAgcCfg, bool isDTV)
{
	int status = 0;
	struct SCfgAgc *pIfAgcSettings;

2162 2163
	dprintk(1, "\n");

R
Ralph Metzler 已提交
2164 2165 2166 2167 2168 2169 2170
	if (pAgcCfg == NULL)
		return -1;

	do {
		u16 data = 0;

		switch (pAgcCfg->ctrlMode) {
2171
		case DRXK_AGC_CTRL_AUTO:
R
Ralph Metzler 已提交
2172 2173

			/* Enable RF AGC DAC */
2174
			status = read16(state, IQM_AF_STDBY__A, &data);
2175 2176
			if (status < 0)
				break;
R
Ralph Metzler 已提交
2177
			data &= ~IQM_AF_STDBY_STDBY_TAGC_RF_STANDBY;
2178
			status = write16(state, IQM_AF_STDBY__A, data);
2179 2180
			if (status < 0)
				break;
R
Ralph Metzler 已提交
2181

2182
			status = read16(state, SCU_RAM_AGC_CONFIG__A, &data);
2183 2184
			if (status < 0)
				break;
R
Ralph Metzler 已提交
2185 2186 2187 2188 2189 2190 2191 2192 2193

			/* Enable SCU RF AGC loop */
			data &= ~SCU_RAM_AGC_CONFIG_DISABLE_RF_AGC__M;

			/* Polarity */
			if (state->m_RfAgcPol)
				data |= SCU_RAM_AGC_CONFIG_INV_RF_POL__M;
			else
				data &= ~SCU_RAM_AGC_CONFIG_INV_RF_POL__M;
2194
			status = write16(state, SCU_RAM_AGC_CONFIG__A, data);
2195 2196
			if (status < 0)
				break;
R
Ralph Metzler 已提交
2197 2198

			/* Set speed (using complementary reduction value) */
2199
			status = read16(state, SCU_RAM_AGC_KI_RED__A, &data);
2200 2201
			if (status < 0)
				break;
R
Ralph Metzler 已提交
2202 2203 2204 2205 2206 2207

			data &= ~SCU_RAM_AGC_KI_RED_RAGC_RED__M;
			data |= (~(pAgcCfg->speed <<
				   SCU_RAM_AGC_KI_RED_RAGC_RED__B)
				 & SCU_RAM_AGC_KI_RED_RAGC_RED__M);

2208
			status = write16(state, SCU_RAM_AGC_KI_RED__A, data);
2209 2210
			if (status < 0)
				break;
R
Ralph Metzler 已提交
2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222

			if (IsDVBT(state))
				pIfAgcSettings = &state->m_dvbtIfAgcCfg;
			else if (IsQAM(state))
				pIfAgcSettings = &state->m_qamIfAgcCfg;
			else
				pIfAgcSettings = &state->m_atvIfAgcCfg;
			if (pIfAgcSettings == NULL)
				return -1;

			/* Set TOP, only if IF-AGC is in AUTO mode */
			if (pIfAgcSettings->ctrlMode == DRXK_AGC_CTRL_AUTO)
2223
				status = write16(state, SCU_RAM_AGC_IF_IACCU_HI_TGT_MAX__A, pAgcCfg->top);
2224 2225
				if (status < 0)
					break;
R
Ralph Metzler 已提交
2226 2227

			/* Cut-Off current */
2228
			status = write16(state, SCU_RAM_AGC_RF_IACCU_HI_CO__A, pAgcCfg->cutOffCurrent);
2229 2230
			if (status < 0)
				break;
R
Ralph Metzler 已提交
2231 2232

			/* Max. output level */
2233
			status = write16(state, SCU_RAM_AGC_RF_MAX__A, pAgcCfg->maxOutputLevel);
2234 2235
			if (status < 0)
				break;
R
Ralph Metzler 已提交
2236 2237 2238 2239 2240

			break;

		case DRXK_AGC_CTRL_USER:
			/* Enable RF AGC DAC */
2241
			status = read16(state, IQM_AF_STDBY__A, &data);
2242 2243
			if (status < 0)
				break;
R
Ralph Metzler 已提交
2244
			data &= ~IQM_AF_STDBY_STDBY_TAGC_RF_STANDBY;
2245
			status = write16(state, IQM_AF_STDBY__A, data);
2246 2247
			if (status < 0)
				break;
R
Ralph Metzler 已提交
2248 2249

			/* Disable SCU RF AGC loop */
2250
			status = read16(state, SCU_RAM_AGC_CONFIG__A, &data);
2251 2252
			if (status < 0)
				break;
R
Ralph Metzler 已提交
2253 2254 2255 2256 2257
			data |= SCU_RAM_AGC_CONFIG_DISABLE_RF_AGC__M;
			if (state->m_RfAgcPol)
				data |= SCU_RAM_AGC_CONFIG_INV_RF_POL__M;
			else
				data &= ~SCU_RAM_AGC_CONFIG_INV_RF_POL__M;
2258
			status = write16(state, SCU_RAM_AGC_CONFIG__A, data);
2259 2260
			if (status < 0)
				break;
R
Ralph Metzler 已提交
2261 2262

			/* SCU c.o.c. to 0, enabling full control range */
2263
			status = write16(state, SCU_RAM_AGC_RF_IACCU_HI_CO__A, 0);
2264 2265
			if (status < 0)
				break;
R
Ralph Metzler 已提交
2266 2267

			/* Write value to output pin */
2268
			status = write16(state, SCU_RAM_AGC_RF_IACCU_HI__A, pAgcCfg->outputLevel);
2269 2270
			if (status < 0)
				break;
R
Ralph Metzler 已提交
2271 2272
			break;

2273
		case DRXK_AGC_CTRL_OFF:
R
Ralph Metzler 已提交
2274
			/* Disable RF AGC DAC */
2275
			status = read16(state, IQM_AF_STDBY__A, &data);
2276 2277
			if (status < 0)
				break;
R
Ralph Metzler 已提交
2278
			data |= IQM_AF_STDBY_STDBY_TAGC_RF_STANDBY;
2279
			status = write16(state, IQM_AF_STDBY__A, data);
2280 2281
			if (status < 0)
				break;
R
Ralph Metzler 已提交
2282 2283

			/* Disable SCU RF AGC loop */
2284
			status = read16(state, SCU_RAM_AGC_CONFIG__A, &data);
2285 2286
			if (status < 0)
				break;
R
Ralph Metzler 已提交
2287
			data |= SCU_RAM_AGC_CONFIG_DISABLE_RF_AGC__M;
2288
			status = write16(state, SCU_RAM_AGC_CONFIG__A, data);
2289 2290
			if (status < 0)
				break;
R
Ralph Metzler 已提交
2291 2292 2293 2294 2295
			break;

		default:
			return -1;

2296 2297
		}		/* switch (agcsettings->ctrlMode) */
	} while (0);
R
Ralph Metzler 已提交
2298 2299 2300 2301 2302
	return status;
}

#define SCU_RAM_AGC_KI_INV_IF_POL__M 0x2000

2303 2304
static int SetAgcIf(struct drxk_state *state,
		    struct SCfgAgc *pAgcCfg, bool isDTV)
R
Ralph Metzler 已提交
2305 2306 2307 2308 2309
{
	u16 data = 0;
	int status = 0;
	struct SCfgAgc *pRfAgcSettings;

2310 2311
	dprintk(1, "\n");

R
Ralph Metzler 已提交
2312 2313
	do {
		switch (pAgcCfg->ctrlMode) {
2314
		case DRXK_AGC_CTRL_AUTO:
R
Ralph Metzler 已提交
2315 2316

			/* Enable IF AGC DAC */
2317
			status = read16(state, IQM_AF_STDBY__A, &data);
2318 2319
			if (status < 0)
				break;
R
Ralph Metzler 已提交
2320
			data &= ~IQM_AF_STDBY_STDBY_TAGC_IF_STANDBY;
2321
			status = write16(state, IQM_AF_STDBY__A, data);
2322 2323
			if (status < 0)
				break;
R
Ralph Metzler 已提交
2324

2325
			status = read16(state, SCU_RAM_AGC_CONFIG__A, &data);
2326 2327
			if (status < 0)
				break;
R
Ralph Metzler 已提交
2328 2329 2330 2331 2332 2333 2334 2335 2336

			/* Enable SCU IF AGC loop */
			data &= ~SCU_RAM_AGC_CONFIG_DISABLE_IF_AGC__M;

			/* Polarity */
			if (state->m_IfAgcPol)
				data |= SCU_RAM_AGC_CONFIG_INV_IF_POL__M;
			else
				data &= ~SCU_RAM_AGC_CONFIG_INV_IF_POL__M;
2337
			status = write16(state, SCU_RAM_AGC_CONFIG__A, data);
2338 2339
			if (status < 0)
				break;
R
Ralph Metzler 已提交
2340 2341

			/* Set speed (using complementary reduction value) */
2342
			status = read16(state, SCU_RAM_AGC_KI_RED__A, &data);
2343 2344
			if (status < 0)
				break;
R
Ralph Metzler 已提交
2345 2346 2347 2348 2349
			data &= ~SCU_RAM_AGC_KI_RED_IAGC_RED__M;
			data |= (~(pAgcCfg->speed <<
				   SCU_RAM_AGC_KI_RED_IAGC_RED__B)
				 & SCU_RAM_AGC_KI_RED_IAGC_RED__M);

2350
			status = write16(state, SCU_RAM_AGC_KI_RED__A, data);
2351 2352
			if (status < 0)
				break;
R
Ralph Metzler 已提交
2353 2354 2355 2356 2357 2358 2359 2360

			if (IsQAM(state))
				pRfAgcSettings = &state->m_qamRfAgcCfg;
			else
				pRfAgcSettings = &state->m_atvRfAgcCfg;
			if (pRfAgcSettings == NULL)
				return -1;
			/* Restore TOP */
2361
			status = write16(state, SCU_RAM_AGC_IF_IACCU_HI_TGT_MAX__A, pRfAgcSettings->top);
2362 2363
			if (status < 0)
				break;
R
Ralph Metzler 已提交
2364 2365
			break;

2366
		case DRXK_AGC_CTRL_USER:
R
Ralph Metzler 已提交
2367 2368

			/* Enable IF AGC DAC */
2369
			status = read16(state, IQM_AF_STDBY__A, &data);
2370 2371
			if (status < 0)
				break;
R
Ralph Metzler 已提交
2372
			data &= ~IQM_AF_STDBY_STDBY_TAGC_IF_STANDBY;
2373
			status = write16(state, IQM_AF_STDBY__A, data);
2374 2375
			if (status < 0)
				break;
R
Ralph Metzler 已提交
2376

2377
			status = read16(state, SCU_RAM_AGC_CONFIG__A, &data);
2378 2379
			if (status < 0)
				break;
R
Ralph Metzler 已提交
2380 2381 2382 2383 2384 2385 2386 2387 2388

			/* Disable SCU IF AGC loop */
			data |= SCU_RAM_AGC_CONFIG_DISABLE_IF_AGC__M;

			/* Polarity */
			if (state->m_IfAgcPol)
				data |= SCU_RAM_AGC_CONFIG_INV_IF_POL__M;
			else
				data &= ~SCU_RAM_AGC_CONFIG_INV_IF_POL__M;
2389
			status = write16(state, SCU_RAM_AGC_CONFIG__A, data);
2390 2391
			if (status < 0)
				break;
R
Ralph Metzler 已提交
2392 2393

			/* Write value to output pin */
2394
			status = write16(state, SCU_RAM_AGC_IF_IACCU_HI_TGT_MAX__A, pAgcCfg->outputLevel);
2395 2396
			if (status < 0)
				break;
R
Ralph Metzler 已提交
2397 2398
			break;

2399
		case DRXK_AGC_CTRL_OFF:
R
Ralph Metzler 已提交
2400 2401

			/* Disable If AGC DAC */
2402
			status = read16(state, IQM_AF_STDBY__A, &data);
2403 2404
			if (status < 0)
				break;
R
Ralph Metzler 已提交
2405
			data |= IQM_AF_STDBY_STDBY_TAGC_IF_STANDBY;
2406
			status = write16(state, IQM_AF_STDBY__A, data);
2407 2408
			if (status < 0)
				break;
R
Ralph Metzler 已提交
2409 2410

			/* Disable SCU IF AGC loop */
2411
			status = read16(state, SCU_RAM_AGC_CONFIG__A, &data);
2412 2413
			if (status < 0)
				break;
R
Ralph Metzler 已提交
2414
			data |= SCU_RAM_AGC_CONFIG_DISABLE_IF_AGC__M;
2415
			status = write16(state, SCU_RAM_AGC_CONFIG__A, data);
2416 2417
			if (status < 0)
				break;
R
Ralph Metzler 已提交
2418
			break;
2419
		}		/* switch (agcSettingsIf->ctrlMode) */
R
Ralph Metzler 已提交
2420 2421 2422

		/* always set the top to support
		   configurations without if-loop */
2423
		status = write16(state, SCU_RAM_AGC_INGAIN_TGT_MIN__A, pAgcCfg->top);
2424 2425
		if (status < 0)
			break;
R
Ralph Metzler 已提交
2426 2427


2428
	} while (0);
R
Ralph Metzler 已提交
2429 2430 2431 2432 2433 2434
	return status;
}

static int ReadIFAgc(struct drxk_state *state, u32 *pValue)
{
	u16 agcDacLvl;
2435
	int status = read16(state, IQM_AF_AGC_IF__A, &agcDacLvl);
R
Ralph Metzler 已提交
2436

2437 2438
	dprintk(1, "\n");

R
Ralph Metzler 已提交
2439 2440
	*pValue = 0;

2441
	if (status == 0) {
R
Ralph Metzler 已提交
2442 2443 2444 2445
		u16 Level = 0;
		if (agcDacLvl > DRXK_AGC_DAC_OFFSET)
			Level = agcDacLvl - DRXK_AGC_DAC_OFFSET;
		if (Level < 14000)
2446
			*pValue = (14000 - Level) / 4;
R
Ralph Metzler 已提交
2447 2448 2449 2450 2451 2452
		else
			*pValue = 0;
	}
	return status;
}

2453 2454
static int GetQAMSignalToNoise(struct drxk_state *state,
			       s32 *pSignalToNoise)
R
Ralph Metzler 已提交
2455 2456 2457
{
	int status = 0;

2458 2459
	dprintk(1, "\n");

R
Ralph Metzler 已提交
2460 2461
	do {
		/* MER calculation */
2462
		u16 qamSlErrPower = 0;	/* accum. error between
R
Ralph Metzler 已提交
2463
					   raw and sliced symbols */
2464
		u32 qamSlSigPower = 0;	/* used for MER, depends of
R
Ralph Metzler 已提交
2465
					   QAM constellation */
2466
		u32 qamSlMer = 0;	/* QAM MER */
R
Ralph Metzler 已提交
2467 2468

		/* get the register value needed for MER */
2469
		status = read16(state, QAM_SL_ERR_POWER__A, &qamSlErrPower);
2470 2471
		if (status < 0)
			break;
R
Ralph Metzler 已提交
2472

2473
		switch (state->param.u.qam.modulation) {
R
Ralph Metzler 已提交
2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492
		case QAM_16:
			qamSlSigPower = DRXK_QAM_SL_SIG_POWER_QAM16 << 2;
			break;
		case QAM_32:
			qamSlSigPower = DRXK_QAM_SL_SIG_POWER_QAM32 << 2;
			break;
		case QAM_64:
			qamSlSigPower = DRXK_QAM_SL_SIG_POWER_QAM64 << 2;
			break;
		case QAM_128:
			qamSlSigPower = DRXK_QAM_SL_SIG_POWER_QAM128 << 2;
			break;
		default:
		case QAM_256:
			qamSlSigPower = DRXK_QAM_SL_SIG_POWER_QAM256 << 2;
			break;
		}

		if (qamSlErrPower > 0) {
2493 2494
			qamSlMer = Log10Times100(qamSlSigPower) -
			    Log10Times100((u32) qamSlErrPower);
R
Ralph Metzler 已提交
2495 2496
		}
		*pSignalToNoise = qamSlMer;
2497
	} while (0);
R
Ralph Metzler 已提交
2498 2499 2500
	return status;
}

2501 2502
static int GetDVBTSignalToNoise(struct drxk_state *state,
				s32 *pSignalToNoise)
R
Ralph Metzler 已提交
2503 2504 2505
{
	int status = 0;

2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517
	u16 regData = 0;
	u32 EqRegTdSqrErrI = 0;
	u32 EqRegTdSqrErrQ = 0;
	u16 EqRegTdSqrErrExp = 0;
	u16 EqRegTdTpsPwrOfs = 0;
	u16 EqRegTdReqSmbCnt = 0;
	u32 tpsCnt = 0;
	u32 SqrErrIQ = 0;
	u32 a = 0;
	u32 b = 0;
	u32 c = 0;
	u32 iMER = 0;
R
Ralph Metzler 已提交
2518 2519
	u16 transmissionParams = 0;

2520
	dprintk(1, "\n");
R
Ralph Metzler 已提交
2521
	do {
2522
		status = read16(state, OFDM_EQ_TOP_TD_TPS_PWR_OFS__A, &EqRegTdTpsPwrOfs);
2523 2524
		if (status < 0)
			break;
2525
		status = read16(state, OFDM_EQ_TOP_TD_REQ_SMB_CNT__A, &EqRegTdReqSmbCnt);
2526 2527
		if (status < 0)
			break;
2528
		status = read16(state, OFDM_EQ_TOP_TD_SQR_ERR_EXP__A, &EqRegTdSqrErrExp);
2529 2530
		if (status < 0)
			break;
2531
		status = read16(state, OFDM_EQ_TOP_TD_SQR_ERR_I__A, &regData);
2532 2533
		if (status < 0)
			break;
R
Ralph Metzler 已提交
2534
		/* Extend SQR_ERR_I operational range */
2535
		EqRegTdSqrErrI = (u32) regData;
R
Ralph Metzler 已提交
2536 2537 2538 2539
		if ((EqRegTdSqrErrExp > 11) &&
		    (EqRegTdSqrErrI < 0x00000FFFUL)) {
			EqRegTdSqrErrI += 0x00010000UL;
		}
2540
		status = read16(state, OFDM_EQ_TOP_TD_SQR_ERR_Q__A, &regData);
2541 2542
		if (status < 0)
			break;
R
Ralph Metzler 已提交
2543
		/* Extend SQR_ERR_Q operational range */
2544
		EqRegTdSqrErrQ = (u32) regData;
R
Ralph Metzler 已提交
2545 2546 2547 2548
		if ((EqRegTdSqrErrExp > 11) &&
		    (EqRegTdSqrErrQ < 0x00000FFFUL))
			EqRegTdSqrErrQ += 0x00010000UL;

2549
		status = read16(state, OFDM_SC_RA_RAM_OP_PARAM__A, &transmissionParams);
2550 2551
		if (status < 0)
			break;
R
Ralph Metzler 已提交
2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564

		/* Check input data for MER */

		/* MER calculation (in 0.1 dB) without math.h */
		if ((EqRegTdTpsPwrOfs == 0) || (EqRegTdReqSmbCnt == 0))
			iMER = 0;
		else if ((EqRegTdSqrErrI + EqRegTdSqrErrQ) == 0) {
			/* No error at all, this must be the HW reset value
			 * Apparently no first measurement yet
			 * Set MER to 0.0 */
			iMER = 0;
		} else {
			SqrErrIQ = (EqRegTdSqrErrI + EqRegTdSqrErrQ) <<
2565
			    EqRegTdSqrErrExp;
R
Ralph Metzler 已提交
2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580
			if ((transmissionParams &
			     OFDM_SC_RA_RAM_OP_PARAM_MODE__M)
			    == OFDM_SC_RA_RAM_OP_PARAM_MODE_2K)
				tpsCnt = 17;
			else
				tpsCnt = 68;

			/* IMER = 100 * log10 (x)
			   where x = (EqRegTdTpsPwrOfs^2 *
			   EqRegTdReqSmbCnt * tpsCnt)/SqrErrIQ

			   => IMER = a + b -c
			   where a = 100 * log10 (EqRegTdTpsPwrOfs^2)
			   b = 100 * log10 (EqRegTdReqSmbCnt * tpsCnt)
			   c = 100 * log10 (SqrErrIQ)
2581
			 */
R
Ralph Metzler 已提交
2582 2583

			/* log(x) x = 9bits * 9bits->18 bits  */
2584 2585
			a = Log10Times100(EqRegTdTpsPwrOfs *
					  EqRegTdTpsPwrOfs);
R
Ralph Metzler 已提交
2586
			/* log(x) x = 16bits * 7bits->23 bits  */
2587
			b = Log10Times100(EqRegTdReqSmbCnt * tpsCnt);
R
Ralph Metzler 已提交
2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598
			/* log(x) x = (16bits + 16bits) << 15 ->32 bits  */
			c = Log10Times100(SqrErrIQ);

			iMER = a + b;
			/* No negative MER, clip to zero */
			if (iMER > c)
				iMER -= c;
			else
				iMER = 0;
		}
		*pSignalToNoise = iMER;
2599
	} while (0);
R
Ralph Metzler 已提交
2600 2601 2602 2603 2604 2605

	return status;
}

static int GetSignalToNoise(struct drxk_state *state, s32 *pSignalToNoise)
{
2606 2607
	dprintk(1, "\n");

R
Ralph Metzler 已提交
2608
	*pSignalToNoise = 0;
2609
	switch (state->m_OperationMode) {
R
Ralph Metzler 已提交
2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626
	case OM_DVBT:
		return GetDVBTSignalToNoise(state, pSignalToNoise);
	case OM_QAM_ITU_A:
	case OM_QAM_ITU_C:
		return GetQAMSignalToNoise(state, pSignalToNoise);
	default:
		break;
	}
	return 0;
}

#if 0
static int GetDVBTQuality(struct drxk_state *state, s32 *pQuality)
{
	/* SNR Values for quasi errorfree reception rom Nordig 2.2 */
	int status = 0;

2627 2628
	dprintk(1, "\n");

2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645
	static s32 QE_SN[] = {
		51,		/* QPSK 1/2 */
		69,		/* QPSK 2/3 */
		79,		/* QPSK 3/4 */
		89,		/* QPSK 5/6 */
		97,		/* QPSK 7/8 */
		108,		/* 16-QAM 1/2 */
		131,		/* 16-QAM 2/3 */
		146,		/* 16-QAM 3/4 */
		156,		/* 16-QAM 5/6 */
		160,		/* 16-QAM 7/8 */
		165,		/* 64-QAM 1/2 */
		187,		/* 64-QAM 2/3 */
		202,		/* 64-QAM 3/4 */
		216,		/* 64-QAM 5/6 */
		225,		/* 64-QAM 7/8 */
	};
R
Ralph Metzler 已提交
2646 2647 2648 2649 2650 2651 2652 2653 2654 2655

	*pQuality = 0;

	do {
		s32 SignalToNoise = 0;
		u16 Constellation = 0;
		u16 CodeRate = 0;
		u32 SignalToNoiseRel;
		u32 BERQuality;

2656 2657 2658
		status = GetDVBTSignalToNoise(state, &SignalToNoise);
		if (status < 0)
			break;
2659
		status = read16(state, OFDM_EQ_TOP_TD_TPS_CONST__A, &Constellation);
2660 2661
		if (status < 0)
			break;
R
Ralph Metzler 已提交
2662 2663
		Constellation &= OFDM_EQ_TOP_TD_TPS_CONST__M;

2664
		status = read16(state, OFDM_EQ_TOP_TD_TPS_CODE_HP__A, &CodeRate);
2665 2666
		if (status < 0)
			break;
R
Ralph Metzler 已提交
2667 2668 2669 2670 2671 2672
		CodeRate &= OFDM_EQ_TOP_TD_TPS_CODE_HP__M;

		if (Constellation > OFDM_EQ_TOP_TD_TPS_CONST_64QAM ||
		    CodeRate > OFDM_EQ_TOP_TD_TPS_CODE_LP_7_8)
			break;
		SignalToNoiseRel = SignalToNoise -
2673
		    QE_SN[Constellation * 5 + CodeRate];
R
Ralph Metzler 已提交
2674 2675
		BERQuality = 100;

2676 2677
		if (SignalToNoiseRel < -70)
			*pQuality = 0;
R
Ralph Metzler 已提交
2678 2679 2680 2681 2682
		else if (SignalToNoiseRel < 30)
			*pQuality = ((SignalToNoiseRel + 70) *
				     BERQuality) / 100;
		else
			*pQuality = BERQuality;
2683
	} while (0);
R
Ralph Metzler 已提交
2684 2685 2686
	return 0;
};

2687
static int GetDVBCQuality(struct drxk_state *state, s32 *pQuality)
R
Ralph Metzler 已提交
2688 2689 2690 2691
{
	int status = 0;
	*pQuality = 0;

2692 2693
	dprintk(1, "\n");

R
Ralph Metzler 已提交
2694 2695 2696 2697 2698
	do {
		u32 SignalToNoise = 0;
		u32 BERQuality = 100;
		u32 SignalToNoiseRel = 0;

2699 2700 2701
		status = GetQAMSignalToNoise(state, &SignalToNoise);
		if (status < 0)
			break;
R
Ralph Metzler 已提交
2702

2703
		switch (state->param.u.qam.modulation) {
R
Ralph Metzler 已提交
2704 2705 2706 2707 2708
		case QAM_16:
			SignalToNoiseRel = SignalToNoise - 200;
			break;
		case QAM_32:
			SignalToNoiseRel = SignalToNoise - 230;
2709
			break;	/* Not in NorDig */
R
Ralph Metzler 已提交
2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728
		case QAM_64:
			SignalToNoiseRel = SignalToNoise - 260;
			break;
		case QAM_128:
			SignalToNoiseRel = SignalToNoise - 290;
			break;
		default:
		case QAM_256:
			SignalToNoiseRel = SignalToNoise - 320;
			break;
		}

		if (SignalToNoiseRel < -70)
			*pQuality = 0;
		else if (SignalToNoiseRel < 30)
			*pQuality = ((SignalToNoiseRel + 70) *
				     BERQuality) / 100;
		else
			*pQuality = BERQuality;
2729
	} while (0);
R
Ralph Metzler 已提交
2730 2731 2732 2733 2734 2735

	return status;
}

static int GetQuality(struct drxk_state *state, s32 *pQuality)
{
2736 2737
	dprintk(1, "\n");

2738 2739
	switch (state->m_OperationMode) {
	case OM_DVBT:
R
Ralph Metzler 已提交
2740
		return GetDVBTQuality(state, pQuality);
2741
	case OM_QAM_ITU_A:
R
Ralph Metzler 已提交
2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767
		return GetDVBCQuality(state, pQuality);
	default:
		break;
	}

	return 0;
}
#endif

/* Free data ram in SIO HI */
#define SIO_HI_RA_RAM_USR_BEGIN__A 0x420040
#define SIO_HI_RA_RAM_USR_END__A   0x420060

#define DRXK_HI_ATOMIC_BUF_START (SIO_HI_RA_RAM_USR_BEGIN__A)
#define DRXK_HI_ATOMIC_BUF_END   (SIO_HI_RA_RAM_USR_BEGIN__A + 7)
#define DRXK_HI_ATOMIC_READ      SIO_HI_RA_RAM_PAR_3_ACP_RW_READ
#define DRXK_HI_ATOMIC_WRITE     SIO_HI_RA_RAM_PAR_3_ACP_RW_WRITE

#define DRXDAP_FASI_ADDR2BLOCK(addr)  (((addr) >> 22) & 0x3F)
#define DRXDAP_FASI_ADDR2BANK(addr)   (((addr) >> 16) & 0x3F)
#define DRXDAP_FASI_ADDR2OFFSET(addr) ((addr) & 0x7FFF)

static int ConfigureI2CBridge(struct drxk_state *state, bool bEnableBridge)
{
	int status;

2768 2769
	dprintk(1, "\n");

R
Ralph Metzler 已提交
2770 2771 2772 2773 2774 2775
	if (state->m_DrxkState == DRXK_UNINITIALIZED)
		return -1;
	if (state->m_DrxkState == DRXK_POWERED_DOWN)
		return -1;

	do {
2776
		status = write16(state, SIO_HI_RA_RAM_PAR_1__A, SIO_HI_RA_RAM_PAR_1_PAR1_SEC_KEY);
2777 2778
		if (status < 0)
			break;
R
Ralph Metzler 已提交
2779
		if (bEnableBridge) {
2780
			status = write16(state, SIO_HI_RA_RAM_PAR_2__A, SIO_HI_RA_RAM_PAR_2_BRD_CFG_CLOSED);
2781 2782
			if (status < 0)
				break;
R
Ralph Metzler 已提交
2783
		} else {
2784
			status = write16(state, SIO_HI_RA_RAM_PAR_2__A, SIO_HI_RA_RAM_PAR_2_BRD_CFG_OPEN);
2785 2786
			if (status < 0)
				break;
R
Ralph Metzler 已提交
2787 2788
		}

2789 2790 2791
		status = HI_Command(state, SIO_HI_RA_RAM_CMD_BRDCTRL, 0);
		if (status < 0)
			break;
2792
	} while (0);
R
Ralph Metzler 已提交
2793 2794 2795
	return status;
}

2796 2797
static int SetPreSaw(struct drxk_state *state,
		     struct SCfgPreSaw *pPreSawCfg)
R
Ralph Metzler 已提交
2798 2799 2800
{
	int status;

2801 2802
	dprintk(1, "\n");

2803 2804
	if ((pPreSawCfg == NULL)
	    || (pPreSawCfg->reference > IQM_AF_PDREF__M))
R
Ralph Metzler 已提交
2805 2806
		return -1;

2807
	status = write16(state, IQM_AF_PDREF__A, pPreSawCfg->reference);
R
Ralph Metzler 已提交
2808 2809 2810 2811
	return status;
}

static int BLDirectCmd(struct drxk_state *state, u32 targetAddr,
2812
		       u16 romOffset, u16 nrOfElements, u32 timeOut)
R
Ralph Metzler 已提交
2813
{
2814 2815 2816 2817
	u16 blStatus = 0;
	u16 offset = (u16) ((targetAddr >> 0) & 0x00FFFF);
	u16 blockbank = (u16) ((targetAddr >> 16) & 0x000FFF);
	int status;
R
Ralph Metzler 已提交
2818 2819
	unsigned long end;

2820 2821
	dprintk(1, "\n");

R
Ralph Metzler 已提交
2822 2823
	mutex_lock(&state->mutex);
	do {
2824
		status = write16(state, SIO_BL_MODE__A, SIO_BL_MODE_DIRECT);
2825 2826
		if (status < 0)
			break;
2827
		status = write16(state, SIO_BL_TGT_HDR__A, blockbank);
2828 2829
		if (status < 0)
			break;
2830
		status = write16(state, SIO_BL_TGT_ADDR__A, offset);
2831 2832
		if (status < 0)
			break;
2833
		status = write16(state, SIO_BL_SRC_ADDR__A, romOffset);
2834 2835
		if (status < 0)
			break;
2836
		status = write16(state, SIO_BL_SRC_LEN__A, nrOfElements);
2837 2838
		if (status < 0)
			break;
2839
		status = write16(state, SIO_BL_ENABLE__A, SIO_BL_ENABLE_ON);
2840 2841
		if (status < 0)
			break;
R
Ralph Metzler 已提交
2842

2843
		end = jiffies + msecs_to_jiffies(timeOut);
R
Ralph Metzler 已提交
2844
		do {
2845
			status = read16(state, SIO_BL_STATUS__A, &blStatus);
2846 2847
			if (status < 0)
				break;
2848
		} while ((blStatus == 0x1) && time_is_after_jiffies(end));
R
Ralph Metzler 已提交
2849
		if (blStatus == 0x1) {
2850
			printk(KERN_ERR "drxk: SIO not ready\n");
R
Ralph Metzler 已提交
2851 2852 2853
			mutex_unlock(&state->mutex);
			return -1;
		}
2854
	} while (0);
R
Ralph Metzler 已提交
2855 2856 2857 2858 2859
	mutex_unlock(&state->mutex);
	return status;

}

2860
static int ADCSyncMeasurement(struct drxk_state *state, u16 *count)
R
Ralph Metzler 已提交
2861 2862 2863 2864
{
	u16 data = 0;
	int status;

2865 2866
	dprintk(1, "\n");

R
Ralph Metzler 已提交
2867 2868
	do {
		/* Start measurement */
2869
		status = write16(state, IQM_AF_COMM_EXEC__A, IQM_AF_COMM_EXEC_ACTIVE);
2870 2871
		if (status < 0)
			break;
2872
		status = write16(state, IQM_AF_START_LOCK__A, 1);
2873 2874
		if (status < 0)
			break;
R
Ralph Metzler 已提交
2875 2876

		*count = 0;
2877
		status = read16(state, IQM_AF_PHASE0__A, &data);
2878 2879
		if (status < 0)
			break;
R
Ralph Metzler 已提交
2880
		if (data == 127)
2881
			*count = *count + 1;
2882
		status = read16(state, IQM_AF_PHASE1__A, &data);
2883 2884
		if (status < 0)
			break;
R
Ralph Metzler 已提交
2885
		if (data == 127)
2886
			*count = *count + 1;
2887
		status = read16(state, IQM_AF_PHASE2__A, &data);
2888 2889
		if (status < 0)
			break;
R
Ralph Metzler 已提交
2890
		if (data == 127)
2891 2892
			*count = *count + 1;
	} while (0);
R
Ralph Metzler 已提交
2893 2894 2895 2896 2897 2898 2899 2900
	return status;
}

static int ADCSynchronization(struct drxk_state *state)
{
	u16 count = 0;
	int status;

2901 2902
	dprintk(1, "\n");

R
Ralph Metzler 已提交
2903
	do {
2904 2905 2906
		status = ADCSyncMeasurement(state, &count);
		if (status < 0)
			break;
R
Ralph Metzler 已提交
2907

2908
		if (count == 1) {
R
Ralph Metzler 已提交
2909 2910 2911
			/* Try sampling on a diffrent edge */
			u16 clkNeg = 0;

2912
			status = read16(state, IQM_AF_CLKNEG__A, &clkNeg);
2913 2914
			if (status < 0)
				break;
2915
			if ((clkNeg | IQM_AF_CLKNEG_CLKNEGDATA__M) ==
R
Ralph Metzler 已提交
2916 2917 2918
			    IQM_AF_CLKNEG_CLKNEGDATA_CLK_ADC_DATA_POS) {
				clkNeg &= (~(IQM_AF_CLKNEG_CLKNEGDATA__M));
				clkNeg |=
2919
				    IQM_AF_CLKNEG_CLKNEGDATA_CLK_ADC_DATA_NEG;
R
Ralph Metzler 已提交
2920 2921 2922
			} else {
				clkNeg &= (~(IQM_AF_CLKNEG_CLKNEGDATA__M));
				clkNeg |=
2923
				    IQM_AF_CLKNEG_CLKNEGDATA_CLK_ADC_DATA_POS;
R
Ralph Metzler 已提交
2924
			}
2925
			status = write16(state, IQM_AF_CLKNEG__A, clkNeg);
2926 2927 2928 2929 2930
			if (status < 0)
				break;
			status = ADCSyncMeasurement(state, &count);
			if (status < 0)
				break;
R
Ralph Metzler 已提交
2931 2932 2933 2934 2935 2936 2937 2938 2939 2940
		}

		if (count < 2)
			status = -1;
	} while (0);
	return status;
}

static int SetFrequencyShifter(struct drxk_state *state,
			       u16 intermediateFreqkHz,
2941
			       s32 tunerFreqOffset, bool isDTV)
R
Ralph Metzler 已提交
2942 2943
{
	bool selectPosImage = false;
2944
	u32 rfFreqResidual = tunerFreqOffset;
R
Ralph Metzler 已提交
2945 2946 2947 2948 2949 2950
	u32 fmFrequencyShift = 0;
	bool tunerMirror = !state->m_bMirrorFreqSpect;
	u32 adcFreq;
	bool adcFlip;
	int status;
	u32 ifFreqActual;
2951
	u32 samplingFrequency = (u32) (state->m_sysClockFreq / 3);
R
Ralph Metzler 已提交
2952 2953 2954
	u32 frequencyShift;
	bool imageToSelect;

2955 2956
	dprintk(1, "\n");

R
Ralph Metzler 已提交
2957
	/*
2958 2959 2960
	   Program frequency shifter
	   No need to account for mirroring on RF
	 */
R
Ralph Metzler 已提交
2961 2962 2963 2964
	if (isDTV) {
		if ((state->m_OperationMode == OM_QAM_ITU_A) ||
		    (state->m_OperationMode == OM_QAM_ITU_C) ||
		    (state->m_OperationMode == OM_DVBT))
2965 2966 2967
			selectPosImage = true;
		else
			selectPosImage = false;
R
Ralph Metzler 已提交
2968 2969 2970 2971
	}
	if (tunerMirror)
		/* tuner doesn't mirror */
		ifFreqActual = intermediateFreqkHz +
2972
		    rfFreqResidual + fmFrequencyShift;
R
Ralph Metzler 已提交
2973 2974 2975
	else
		/* tuner mirrors */
		ifFreqActual = intermediateFreqkHz -
2976
		    rfFreqResidual - fmFrequencyShift;
R
Ralph Metzler 已提交
2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988
	if (ifFreqActual > samplingFrequency / 2) {
		/* adc mirrors */
		adcFreq = samplingFrequency - ifFreqActual;
		adcFlip = true;
	} else {
		/* adc doesn't mirror */
		adcFreq = ifFreqActual;
		adcFlip = false;
	}

	frequencyShift = adcFreq;
	imageToSelect = state->m_rfmirror ^ tunerMirror ^
2989 2990 2991
	    adcFlip ^ selectPosImage;
	state->m_IqmFsRateOfs =
	    Frac28a((frequencyShift), samplingFrequency);
R
Ralph Metzler 已提交
2992 2993 2994 2995 2996 2997

	if (imageToSelect)
		state->m_IqmFsRateOfs = ~state->m_IqmFsRateOfs + 1;

	/* Program frequency shifter with tuner offset compensation */
	/* frequencyShift += tunerFreqOffset; TODO */
2998 2999
	status = write32(state, IQM_FS_RATE_OFS_LO__A,
			 state->m_IqmFsRateOfs);
R
Ralph Metzler 已提交
3000 3001 3002 3003 3004
	return status;
}

static int InitAGC(struct drxk_state *state, bool isDTV)
{
3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016
	u16 ingainTgt = 0;
	u16 ingainTgtMin = 0;
	u16 ingainTgtMax = 0;
	u16 clpCyclen = 0;
	u16 clpSumMin = 0;
	u16 clpDirTo = 0;
	u16 snsSumMin = 0;
	u16 snsSumMax = 0;
	u16 clpSumMax = 0;
	u16 snsDirTo = 0;
	u16 kiInnergainMin = 0;
	u16 ifIaccuHiTgt = 0;
R
Ralph Metzler 已提交
3017 3018
	u16 ifIaccuHiTgtMin = 0;
	u16 ifIaccuHiTgtMax = 0;
3019 3020 3021
	u16 data = 0;
	u16 fastClpCtrlDelay = 0;
	u16 clpCtrlMode = 0;
R
Ralph Metzler 已提交
3022 3023
	int status = 0;

3024 3025
	dprintk(1, "\n");

R
Ralph Metzler 已提交
3026 3027
	do {
		/* Common settings */
3028
		snsSumMax = 1023;
R
Ralph Metzler 已提交
3029
		ifIaccuHiTgtMin = 2047;
3030 3031
		clpCyclen = 500;
		clpSumMax = 1023;
R
Ralph Metzler 已提交
3032 3033 3034

		if (IsQAM(state)) {
			/* Standard specific settings */
3035 3036 3037 3038 3039 3040
			clpSumMin = 8;
			clpDirTo = (u16) -9;
			clpCtrlMode = 0;
			snsSumMin = 8;
			snsDirTo = (u16) -9;
			kiInnergainMin = (u16) -1030;
R
Ralph Metzler 已提交
3041 3042
		} else
			status = -1;
3043 3044 3045
		status = (status);
		if (status < 0)
			break;
R
Ralph Metzler 已提交
3046
		if (IsQAM(state)) {
3047 3048 3049 3050 3051
			ifIaccuHiTgtMax = 0x2380;
			ifIaccuHiTgt = 0x2380;
			ingainTgtMin = 0x0511;
			ingainTgt = 0x0511;
			ingainTgtMax = 5119;
R
Ralph Metzler 已提交
3052
			fastClpCtrlDelay =
3053
			    state->m_qamIfAgcCfg.FastClipCtrlDelay;
R
Ralph Metzler 已提交
3054
		} else {
3055 3056 3057 3058 3059
			ifIaccuHiTgtMax = 0x1200;
			ifIaccuHiTgt = 0x1200;
			ingainTgtMin = 13424;
			ingainTgt = 13424;
			ingainTgtMax = 30000;
R
Ralph Metzler 已提交
3060
			fastClpCtrlDelay =
3061
			    state->m_dvbtIfAgcCfg.FastClipCtrlDelay;
R
Ralph Metzler 已提交
3062
		}
3063
		status = write16(state, SCU_RAM_AGC_FAST_CLP_CTRL_DELAY__A, fastClpCtrlDelay);
3064 3065 3066
		if (status < 0)
			break;

3067
		status = write16(state, SCU_RAM_AGC_CLP_CTRL_MODE__A, clpCtrlMode);
3068 3069
		if (status < 0)
			break;
3070
		status = write16(state, SCU_RAM_AGC_INGAIN_TGT__A, ingainTgt);
3071 3072
		if (status < 0)
			break;
3073
		status = write16(state, SCU_RAM_AGC_INGAIN_TGT_MIN__A, ingainTgtMin);
3074 3075
		if (status < 0)
			break;
3076
		status = write16(state, SCU_RAM_AGC_INGAIN_TGT_MAX__A, ingainTgtMax);
3077 3078
		if (status < 0)
			break;
3079
		status = write16(state, SCU_RAM_AGC_IF_IACCU_HI_TGT_MIN__A, ifIaccuHiTgtMin);
3080 3081
		if (status < 0)
			break;
3082
		status = write16(state, SCU_RAM_AGC_IF_IACCU_HI_TGT_MAX__A, ifIaccuHiTgtMax);
3083 3084
		if (status < 0)
			break;
3085
		status = write16(state, SCU_RAM_AGC_IF_IACCU_HI__A, 0);
3086 3087
		if (status < 0)
			break;
3088
		status = write16(state, SCU_RAM_AGC_IF_IACCU_LO__A, 0);
3089 3090
		if (status < 0)
			break;
3091
		status = write16(state, SCU_RAM_AGC_RF_IACCU_HI__A, 0);
3092 3093
		if (status < 0)
			break;
3094
		status = write16(state, SCU_RAM_AGC_RF_IACCU_LO__A, 0);
3095 3096
		if (status < 0)
			break;
3097
		status = write16(state, SCU_RAM_AGC_CLP_SUM_MAX__A, clpSumMax);
3098 3099
		if (status < 0)
			break;
3100
		status = write16(state, SCU_RAM_AGC_SNS_SUM_MAX__A, snsSumMax);
3101 3102 3103
		if (status < 0)
			break;

3104
		status = write16(state, SCU_RAM_AGC_KI_INNERGAIN_MIN__A, kiInnergainMin);
3105 3106
		if (status < 0)
			break;
3107
		status = write16(state, SCU_RAM_AGC_IF_IACCU_HI_TGT__A, ifIaccuHiTgt);
3108 3109
		if (status < 0)
			break;
3110
		status = write16(state, SCU_RAM_AGC_CLP_CYCLEN__A, clpCyclen);
3111 3112 3113
		if (status < 0)
			break;

3114
		status = write16(state, SCU_RAM_AGC_RF_SNS_DEV_MAX__A, 1023);
3115 3116
		if (status < 0)
			break;
3117
		status = write16(state, SCU_RAM_AGC_RF_SNS_DEV_MIN__A, (u16) -1023);
3118 3119
		if (status < 0)
			break;
3120
		status = write16(state, SCU_RAM_AGC_FAST_SNS_CTRL_DELAY__A, 50);
3121 3122 3123
		if (status < 0)
			break;

3124
		status = write16(state, SCU_RAM_AGC_KI_MAXMINGAIN_TH__A, 20);
3125 3126
		if (status < 0)
			break;
3127
		status = write16(state, SCU_RAM_AGC_CLP_SUM_MIN__A, clpSumMin);
3128 3129
		if (status < 0)
			break;
3130
		status = write16(state, SCU_RAM_AGC_SNS_SUM_MIN__A, snsSumMin);
3131 3132
		if (status < 0)
			break;
3133
		status = write16(state, SCU_RAM_AGC_CLP_DIR_TO__A, clpDirTo);
3134 3135
		if (status < 0)
			break;
3136
		status = write16(state, SCU_RAM_AGC_SNS_DIR_TO__A, snsDirTo);
3137 3138
		if (status < 0)
			break;
3139
		status = write16(state, SCU_RAM_AGC_KI_MINGAIN__A, 0x7fff);
3140 3141
		if (status < 0)
			break;
3142
		status = write16(state, SCU_RAM_AGC_KI_MAXGAIN__A, 0x0);
3143 3144
		if (status < 0)
			break;
3145
		status = write16(state, SCU_RAM_AGC_KI_MIN__A, 0x0117);
3146 3147
		if (status < 0)
			break;
3148
		status = write16(state, SCU_RAM_AGC_KI_MAX__A, 0x0657);
3149 3150
		if (status < 0)
			break;
3151
		status = write16(state, SCU_RAM_AGC_CLP_SUM__A, 0);
3152 3153
		if (status < 0)
			break;
3154
		status = write16(state, SCU_RAM_AGC_CLP_CYCCNT__A, 0);
3155 3156
		if (status < 0)
			break;
3157
		status = write16(state, SCU_RAM_AGC_CLP_DIR_WD__A, 0);
3158 3159
		if (status < 0)
			break;
3160
		status = write16(state, SCU_RAM_AGC_CLP_DIR_STP__A, 1);
3161 3162
		if (status < 0)
			break;
3163
		status = write16(state, SCU_RAM_AGC_SNS_SUM__A, 0);
3164 3165
		if (status < 0)
			break;
3166
		status = write16(state, SCU_RAM_AGC_SNS_CYCCNT__A, 0);
3167 3168
		if (status < 0)
			break;
3169
		status = write16(state, SCU_RAM_AGC_SNS_DIR_WD__A, 0);
3170 3171
		if (status < 0)
			break;
3172
		status = write16(state, SCU_RAM_AGC_SNS_DIR_STP__A, 1);
3173 3174
		if (status < 0)
			break;
3175
		status = write16(state, SCU_RAM_AGC_SNS_CYCLEN__A, 500);
3176 3177
		if (status < 0)
			break;
3178
		status = write16(state, SCU_RAM_AGC_KI_CYCLEN__A, 500);
3179 3180
		if (status < 0)
			break;
R
Ralph Metzler 已提交
3181 3182

		/* Initialize inner-loop KI gain factors */
3183
		status = read16(state, SCU_RAM_AGC_KI__A, &data);
3184 3185
		if (status < 0)
			break;
R
Ralph Metzler 已提交
3186 3187 3188 3189 3190 3191 3192
		if (IsQAM(state)) {
			data = 0x0657;
			data &= ~SCU_RAM_AGC_KI_RF__M;
			data |= (DRXK_KI_RAGC_QAM << SCU_RAM_AGC_KI_RF__B);
			data &= ~SCU_RAM_AGC_KI_IF__M;
			data |= (DRXK_KI_IAGC_QAM << SCU_RAM_AGC_KI_IF__B);
		}
3193
		status = write16(state, SCU_RAM_AGC_KI__A, data);
3194 3195
		if (status < 0)
			break;
3196
	} while (0);
R
Ralph Metzler 已提交
3197 3198 3199
	return status;
}

3200
static int DVBTQAMGetAccPktErr(struct drxk_state *state, u16 *packetErr)
R
Ralph Metzler 已提交
3201 3202 3203
{
	int status;

3204
	dprintk(1, "\n");
R
Ralph Metzler 已提交
3205 3206
	do {
		if (packetErr == NULL) {
3207
			status = write16(state, SCU_RAM_FEC_ACCUM_PKT_FAILURES__A, 0);
3208 3209
			if (status < 0)
				break;
R
Ralph Metzler 已提交
3210
		} else {
3211
			status = read16(state, SCU_RAM_FEC_ACCUM_PKT_FAILURES__A, packetErr);
3212 3213
			if (status < 0)
				break;
R
Ralph Metzler 已提交
3214 3215 3216 3217 3218 3219 3220 3221 3222 3223
		}
	} while (0);
	return status;
}

static int DVBTScCommand(struct drxk_state *state,
			 u16 cmd, u16 subcmd,
			 u16 param0, u16 param1, u16 param2,
			 u16 param3, u16 param4)
{
3224 3225
	u16 curCmd = 0;
	u16 errCode = 0;
R
Ralph Metzler 已提交
3226
	u16 retryCnt = 0;
3227 3228
	u16 scExec = 0;
	int status;
R
Ralph Metzler 已提交
3229

3230
	dprintk(1, "\n");
3231
	status = read16(state, OFDM_SC_COMM_EXEC__A, &scExec);
R
Ralph Metzler 已提交
3232 3233 3234 3235 3236 3237
	if (scExec != 1) {
		/* SC is not running */
		return -1;
	}

	/* Wait until sc is ready to receive command */
3238
	retryCnt = 0;
R
Ralph Metzler 已提交
3239 3240
	do {
		msleep(1);
3241
		status = read16(state, OFDM_SC_RA_RAM_CMD__A, &curCmd);
R
Ralph Metzler 已提交
3242 3243 3244 3245 3246 3247 3248 3249 3250 3251
		retryCnt++;
	} while ((curCmd != 0) && (retryCnt < DRXK_MAX_RETRIES));
	if (retryCnt >= DRXK_MAX_RETRIES)
		return -1;
	/* Write sub-command */
	switch (cmd) {
		/* All commands using sub-cmd */
	case OFDM_SC_RA_RAM_CMD_PROC_START:
	case OFDM_SC_RA_RAM_CMD_SET_PREF_PARAM:
	case OFDM_SC_RA_RAM_CMD_PROGRAM_PARAM:
3252
		status =
3253
		    write16(state, OFDM_SC_RA_RAM_CMD_ADDR__A, subcmd);
R
Ralph Metzler 已提交
3254 3255 3256 3257
		break;
	default:
		/* Do nothing */
		break;
3258
	}			/* switch (cmd->cmd) */
R
Ralph Metzler 已提交
3259 3260 3261 3262 3263 3264 3265 3266 3267 3268

	/* Write needed parameters and the command */
	switch (cmd) {
		/* All commands using 5 parameters */
		/* All commands using 4 parameters */
		/* All commands using 3 parameters */
		/* All commands using 2 parameters */
	case OFDM_SC_RA_RAM_CMD_PROC_START:
	case OFDM_SC_RA_RAM_CMD_SET_PREF_PARAM:
	case OFDM_SC_RA_RAM_CMD_PROGRAM_PARAM:
3269
		status =
3270
		    write16(state, OFDM_SC_RA_RAM_PARAM1__A, param1);
R
Ralph Metzler 已提交
3271 3272 3273
		/* All commands using 1 parameters */
	case OFDM_SC_RA_RAM_CMD_SET_ECHO_TIMING:
	case OFDM_SC_RA_RAM_CMD_USER_IO:
3274
		status =
3275
		    write16(state, OFDM_SC_RA_RAM_PARAM0__A, param0);
R
Ralph Metzler 已提交
3276 3277 3278 3279
		/* All commands using 0 parameters */
	case OFDM_SC_RA_RAM_CMD_GET_OP_PARAM:
	case OFDM_SC_RA_RAM_CMD_NULL:
		/* Write command */
3280
		status = write16(state, OFDM_SC_RA_RAM_CMD__A, cmd);
R
Ralph Metzler 已提交
3281 3282 3283 3284
		break;
	default:
		/* Unknown command */
		return -EINVAL;
3285
	}			/* switch (cmd->cmd) */
R
Ralph Metzler 已提交
3286 3287 3288

	/* Wait until sc is ready processing command */
	retryCnt = 0;
3289
	do {
R
Ralph Metzler 已提交
3290
		msleep(1);
3291
		status = read16(state, OFDM_SC_RA_RAM_CMD__A, &curCmd);
R
Ralph Metzler 已提交
3292
		retryCnt++;
3293
	} while ((curCmd != 0) && (retryCnt < DRXK_MAX_RETRIES));
R
Ralph Metzler 已提交
3294 3295 3296 3297
	if (retryCnt >= DRXK_MAX_RETRIES)
		return -1;

	/* Check for illegal cmd */
3298
	status = read16(state, OFDM_SC_RA_RAM_CMD_ADDR__A, &errCode);
3299
	if (errCode == 0xFFFF) {
R
Ralph Metzler 已提交
3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312
		/* illegal command */
		return -EINVAL;
	}

	/* Retreive results parameters from SC */
	switch (cmd) {
		/* All commands yielding 5 results */
		/* All commands yielding 4 results */
		/* All commands yielding 3 results */
		/* All commands yielding 2 results */
		/* All commands yielding 1 result */
	case OFDM_SC_RA_RAM_CMD_USER_IO:
	case OFDM_SC_RA_RAM_CMD_GET_OP_PARAM:
3313
		status =
3314
		    read16(state, OFDM_SC_RA_RAM_PARAM0__A, &(param0));
R
Ralph Metzler 已提交
3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326
		/* All commands yielding 0 results */
	case OFDM_SC_RA_RAM_CMD_SET_ECHO_TIMING:
	case OFDM_SC_RA_RAM_CMD_SET_TIMER:
	case OFDM_SC_RA_RAM_CMD_PROC_START:
	case OFDM_SC_RA_RAM_CMD_SET_PREF_PARAM:
	case OFDM_SC_RA_RAM_CMD_PROGRAM_PARAM:
	case OFDM_SC_RA_RAM_CMD_NULL:
		break;
	default:
		/* Unknown command */
		return -EINVAL;
		break;
3327
	}			/* switch (cmd->cmd) */
R
Ralph Metzler 已提交
3328 3329 3330
	return status;
}

3331
static int PowerUpDVBT(struct drxk_state *state)
R
Ralph Metzler 已提交
3332
{
3333
	enum DRXPowerMode powerMode = DRX_POWER_UP;
R
Ralph Metzler 已提交
3334 3335
	int status;

3336
	dprintk(1, "\n");
R
Ralph Metzler 已提交
3337
	do {
3338 3339 3340
		status = CtrlPowerMode(state, &powerMode);
		if (status < 0)
			break;
R
Ralph Metzler 已提交
3341 3342 3343 3344
	} while (0);
	return status;
}

3345
static int DVBTCtrlSetIncEnable(struct drxk_state *state, bool *enabled)
R
Ralph Metzler 已提交
3346
{
3347 3348
	int status;

3349
	dprintk(1, "\n");
3350
	if (*enabled == true)
3351
		status = write16(state, IQM_CF_BYPASSDET__A, 0);
3352
	else
3353
		status = write16(state, IQM_CF_BYPASSDET__A, 1);
3354 3355

	return status;
R
Ralph Metzler 已提交
3356
}
3357 3358 3359

#define DEFAULT_FR_THRES_8K     4000
static int DVBTCtrlSetFrEnable(struct drxk_state *state, bool *enabled)
R
Ralph Metzler 已提交
3360 3361
{

3362 3363
	int status;

3364
	dprintk(1, "\n");
3365 3366
	if (*enabled == true) {
		/* write mask to 1 */
3367
		status = write16(state, OFDM_SC_RA_RAM_FR_THRES_8K__A,
3368 3369 3370
				   DEFAULT_FR_THRES_8K);
	} else {
		/* write mask to 0 */
3371
		status = write16(state, OFDM_SC_RA_RAM_FR_THRES_8K__A, 0);
3372 3373 3374
	}

	return status;
R
Ralph Metzler 已提交
3375 3376
}

3377 3378
static int DVBTCtrlSetEchoThreshold(struct drxk_state *state,
				    struct DRXKCfgDvbtEchoThres_t *echoThres)
R
Ralph Metzler 已提交
3379
{
3380
	u16 data = 0;
R
Ralph Metzler 已提交
3381 3382
	int status;

3383
	dprintk(1, "\n");
R
Ralph Metzler 已提交
3384
	do {
3385
		status = read16(state, OFDM_SC_RA_RAM_ECHO_THRES__A, &data);
3386 3387
		if (status < 0)
			break;
3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408

		switch (echoThres->fftMode) {
		case DRX_FFTMODE_2K:
			data &= ~OFDM_SC_RA_RAM_ECHO_THRES_2K__M;
			data |=
			    ((echoThres->threshold <<
			      OFDM_SC_RA_RAM_ECHO_THRES_2K__B)
			     & (OFDM_SC_RA_RAM_ECHO_THRES_2K__M));
			break;
		case DRX_FFTMODE_8K:
			data &= ~OFDM_SC_RA_RAM_ECHO_THRES_8K__M;
			data |=
			    ((echoThres->threshold <<
			      OFDM_SC_RA_RAM_ECHO_THRES_8K__B)
			     & (OFDM_SC_RA_RAM_ECHO_THRES_8K__M));
			break;
		default:
			return -1;
			break;
		}

3409
		status = write16(state, OFDM_SC_RA_RAM_ECHO_THRES__A, data);
3410 3411
		if (status < 0)
			break;
3412 3413 3414
	} while (0);

	return status;
R
Ralph Metzler 已提交
3415 3416 3417
}

static int DVBTCtrlSetSqiSpeed(struct drxk_state *state,
3418
			       enum DRXKCfgDvbtSqiSpeed *speed)
R
Ralph Metzler 已提交
3419 3420 3421
{
	int status;

3422 3423
	dprintk(1, "\n");

R
Ralph Metzler 已提交
3424 3425 3426 3427 3428 3429 3430 3431
	switch (*speed) {
	case DRXK_DVBT_SQI_SPEED_FAST:
	case DRXK_DVBT_SQI_SPEED_MEDIUM:
	case DRXK_DVBT_SQI_SPEED_SLOW:
		break;
	default:
		return -EINVAL;
	}
3432
	status = write16(state, SCU_RAM_FEC_PRE_RS_BER_FILTER_SH__A,
3433
			   (u16) *speed);
R
Ralph Metzler 已提交
3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446
	return status;
}

/*============================================================================*/

/**
* \brief Activate DVBT specific presets
* \param demod instance of demodulator.
* \return DRXStatus_t.
*
* Called in DVBTSetStandard
*
*/
3447
static int DVBTActivatePresets(struct drxk_state *state)
R
Ralph Metzler 已提交
3448
{
3449 3450 3451 3452 3453
	int status;

	struct DRXKCfgDvbtEchoThres_t echoThres2k = { 0, DRX_FFTMODE_2K };
	struct DRXKCfgDvbtEchoThres_t echoThres8k = { 0, DRX_FFTMODE_8K };

3454
	dprintk(1, "\n");
3455 3456 3457
	do {
		bool setincenable = false;
		bool setfrenable = true;
3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469
		status = DVBTCtrlSetIncEnable(state, &setincenable);
		if (status < 0)
			break;
		status = DVBTCtrlSetFrEnable(state, &setfrenable);
		if (status < 0)
			break;
		status = DVBTCtrlSetEchoThreshold(state, &echoThres2k);
		if (status < 0)
			break;
		status = DVBTCtrlSetEchoThreshold(state, &echoThres8k);
		if (status < 0)
			break;
3470
		status = write16(state, SCU_RAM_AGC_INGAIN_TGT_MAX__A, state->m_dvbtIfAgcCfg.IngainTgtMax);
3471 3472
		if (status < 0)
			break;
3473 3474 3475
	} while (0);

	return status;
R
Ralph Metzler 已提交
3476
}
3477

R
Ralph Metzler 已提交
3478 3479 3480 3481 3482 3483 3484 3485 3486 3487
/*============================================================================*/

/**
* \brief Initialize channelswitch-independent settings for DVBT.
* \param demod instance of demodulator.
* \return DRXStatus_t.
*
* For ROM code channel filter taps are loaded from the bootloader. For microcode
* the DVB-T taps from the drxk_filters.h are used.
*/
3488 3489
static int SetDVBTStandard(struct drxk_state *state,
			   enum OperationMode oMode)
R
Ralph Metzler 已提交
3490
{
3491 3492 3493
	u16 cmdResult = 0;
	u16 data = 0;
	int status;
R
Ralph Metzler 已提交
3494

3495
	dprintk(1, "\n");
R
Ralph Metzler 已提交
3496

3497
	PowerUpDVBT(state);
R
Ralph Metzler 已提交
3498 3499 3500 3501
	do {
		/* added antenna switch */
		SwitchAntennaToDVBT(state);
		/* send OFDM reset command */
3502 3503 3504
		status = scu_command(state, SCU_RAM_COMMAND_STANDARD_OFDM | SCU_RAM_COMMAND_CMD_DEMOD_RESET, 0, NULL, 1, &cmdResult);
		if (status < 0)
			break;
R
Ralph Metzler 已提交
3505 3506

		/* send OFDM setenv command */
3507 3508 3509
		status = scu_command(state, SCU_RAM_COMMAND_STANDARD_OFDM | SCU_RAM_COMMAND_CMD_DEMOD_SET_ENV, 0, NULL, 1, &cmdResult);
		if (status < 0)
			break;
R
Ralph Metzler 已提交
3510 3511

		/* reset datapath for OFDM, processors first */
3512
		status = write16(state, OFDM_SC_COMM_EXEC__A, OFDM_SC_COMM_EXEC_STOP);
3513 3514
		if (status < 0)
			break;
3515
		status = write16(state, OFDM_LC_COMM_EXEC__A, OFDM_LC_COMM_EXEC_STOP);
3516 3517
		if (status < 0)
			break;
3518
		status = write16(state, IQM_COMM_EXEC__A, IQM_COMM_EXEC_B_STOP);
3519 3520
		if (status < 0)
			break;
R
Ralph Metzler 已提交
3521 3522 3523

		/* IQM setup */
		/* synchronize on ofdstate->m_festart */
3524
		status = write16(state, IQM_AF_UPD_SEL__A, 1);
3525 3526
		if (status < 0)
			break;
R
Ralph Metzler 已提交
3527
		/* window size for clipping ADC detection */
3528
		status = write16(state, IQM_AF_CLP_LEN__A, 0);
3529 3530
		if (status < 0)
			break;
R
Ralph Metzler 已提交
3531
		/* window size for for sense pre-SAW detection */
3532
		status = write16(state, IQM_AF_SNS_LEN__A, 0);
3533 3534
		if (status < 0)
			break;
R
Ralph Metzler 已提交
3535
		/* sense threshold for sense pre-SAW detection */
3536
		status = write16(state, IQM_AF_AMUX__A, IQM_AF_AMUX_SIGNAL2ADC);
3537 3538 3539 3540 3541
		if (status < 0)
			break;
		status = SetIqmAf(state, true);
		if (status < 0)
			break;
R
Ralph Metzler 已提交
3542

3543
		status = write16(state, IQM_AF_AGC_RF__A, 0);
3544 3545
		if (status < 0)
			break;
R
Ralph Metzler 已提交
3546 3547

		/* Impulse noise cruncher setup */
3548
		status = write16(state, IQM_AF_INC_LCT__A, 0);	/* crunch in IQM_CF */
3549 3550
		if (status < 0)
			break;
3551
		status = write16(state, IQM_CF_DET_LCT__A, 0);	/* detect in IQM_CF */
3552 3553
		if (status < 0)
			break;
3554
		status = write16(state, IQM_CF_WND_LEN__A, 3);	/* peak detector window length */
3555 3556
		if (status < 0)
			break;
R
Ralph Metzler 已提交
3557

3558
		status = write16(state, IQM_RC_STRETCH__A, 16);
3559 3560
		if (status < 0)
			break;
3561
		status = write16(state, IQM_CF_OUT_ENA__A, 0x4);	/* enable output 2 */
3562 3563
		if (status < 0)
			break;
3564
		status = write16(state, IQM_CF_DS_ENA__A, 0x4);	/* decimate output 2 */
3565 3566
		if (status < 0)
			break;
3567
		status = write16(state, IQM_CF_SCALE__A, 1600);
3568 3569
		if (status < 0)
			break;
3570
		status = write16(state, IQM_CF_SCALE_SH__A, 0);
3571 3572
		if (status < 0)
			break;
R
Ralph Metzler 已提交
3573 3574

		/* virtual clipping threshold for clipping ADC detection */
3575
		status = write16(state, IQM_AF_CLP_TH__A, 448);
3576 3577
		if (status < 0)
			break;
3578
		status = write16(state, IQM_CF_DATATH__A, 495);	/* crunching threshold */
3579 3580
		if (status < 0)
			break;
R
Ralph Metzler 已提交
3581

3582 3583 3584
		status = BLChainCmd(state, DRXK_BL_ROM_OFFSET_TAPS_DVBT, DRXK_BLCC_NR_ELEMENTS_TAPS, DRXK_BLC_TIMEOUT);
		if (status < 0)
			break;
R
Ralph Metzler 已提交
3585

3586
		status = write16(state, IQM_CF_PKDTH__A, 2);	/* peak detector threshold */
3587 3588
		if (status < 0)
			break;
3589
		status = write16(state, IQM_CF_POW_MEAS_LEN__A, 2);
3590 3591
		if (status < 0)
			break;
R
Ralph Metzler 已提交
3592
		/* enable power measurement interrupt */
3593
		status = write16(state, IQM_CF_COMM_INT_MSK__A, 1);
3594 3595
		if (status < 0)
			break;
3596
		status = write16(state, IQM_COMM_EXEC__A, IQM_COMM_EXEC_B_ACTIVE);
3597 3598
		if (status < 0)
			break;
R
Ralph Metzler 已提交
3599 3600

		/* IQM will not be reset from here, sync ADC and update/init AGC */
3601 3602 3603 3604 3605 3606
		status = ADCSynchronization(state);
		if (status < 0)
			break;
		status = SetPreSaw(state, &state->m_dvbtPreSawCfg);
		if (status < 0)
			break;
R
Ralph Metzler 已提交
3607 3608

		/* Halt SCU to enable safe non-atomic accesses */
3609
		status = write16(state, SCU_COMM_EXEC__A, SCU_COMM_EXEC_HOLD);
3610 3611
		if (status < 0)
			break;
R
Ralph Metzler 已提交
3612

3613 3614 3615 3616 3617 3618
		status = SetAgcRf(state, &state->m_dvbtRfAgcCfg, true);
		if (status < 0)
			break;
		status = SetAgcIf(state, &state->m_dvbtIfAgcCfg, true);
		if (status < 0)
			break;
R
Ralph Metzler 已提交
3619 3620

		/* Set Noise Estimation notch width and enable DC fix */
3621
		status = read16(state, OFDM_SC_RA_RAM_CONFIG__A, &data);
3622 3623
		if (status < 0)
			break;
R
Ralph Metzler 已提交
3624
		data |= OFDM_SC_RA_RAM_CONFIG_NE_FIX_ENABLE__M;
3625
		status = write16(state, OFDM_SC_RA_RAM_CONFIG__A, data);
3626 3627
		if (status < 0)
			break;
R
Ralph Metzler 已提交
3628 3629

		/* Activate SCU to enable SCU commands */
3630
		status = write16(state, SCU_COMM_EXEC__A, SCU_COMM_EXEC_ACTIVE);
3631 3632
		if (status < 0)
			break;
R
Ralph Metzler 已提交
3633

3634
		if (!state->m_DRXK_A3_ROM_CODE) {
R
Ralph Metzler 已提交
3635
			/* AGCInit() is not done for DVBT, so set agcFastClipCtrlDelay  */
3636
			status = write16(state, SCU_RAM_AGC_FAST_CLP_CTRL_DELAY__A, state->m_dvbtIfAgcCfg.FastClipCtrlDelay);
3637 3638
			if (status < 0)
				break;
R
Ralph Metzler 已提交
3639 3640 3641 3642
		}

		/* OFDM_SC setup */
#ifdef COMPILE_FOR_NONRT
3643
		status = write16(state, OFDM_SC_RA_RAM_BE_OPT_DELAY__A, 1);
3644 3645
		if (status < 0)
			break;
3646
		status = write16(state, OFDM_SC_RA_RAM_BE_OPT_INIT_DELAY__A, 2);
3647 3648
		if (status < 0)
			break;
R
Ralph Metzler 已提交
3649 3650 3651
#endif

		/* FEC setup */
3652
		status = write16(state, FEC_DI_INPUT_CTL__A, 1);	/* OFDM input */
3653 3654
		if (status < 0)
			break;
R
Ralph Metzler 已提交
3655 3656 3657


#ifdef COMPILE_FOR_NONRT
3658
		status = write16(state, FEC_RS_MEASUREMENT_PERIOD__A, 0x400);
3659 3660
		if (status < 0)
			break;
R
Ralph Metzler 已提交
3661
#else
3662
		status = write16(state, FEC_RS_MEASUREMENT_PERIOD__A, 0x1000);
3663 3664
		if (status < 0)
			break;
R
Ralph Metzler 已提交
3665
#endif
3666
		status = write16(state, FEC_RS_MEASUREMENT_PRESCALE__A, 0x0001);
3667 3668
		if (status < 0)
			break;
R
Ralph Metzler 已提交
3669 3670

		/* Setup MPEG bus */
3671 3672 3673
		status = MPEGTSDtoSetup(state, OM_DVBT);
		if (status < 0)
			break;
R
Ralph Metzler 已提交
3674
		/* Set DVBT Presets */
3675 3676 3677
		status = DVBTActivatePresets(state);
		if (status < 0)
			break;
R
Ralph Metzler 已提交
3678 3679 3680

	} while (0);

3681
	if (status < 0)
3682
		printk(KERN_ERR "drxk: %s status - %08x\n", __func__, status);
R
Ralph Metzler 已提交
3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694

	return status;
}

/*============================================================================*/
/**
* \brief Start dvbt demodulating for channel.
* \param demod instance of demodulator.
* \return DRXStatus_t.
*/
static int DVBTStart(struct drxk_state *state)
{
3695 3696 3697 3698
	u16 param1;
	int status;
	/* DRXKOfdmScCmd_t scCmd; */

3699
	dprintk(1, "\n");
3700 3701 3702 3703
	/* Start correct processes to get in lock */
	/* DRXK: OFDM_SC_RA_RAM_PROC_LOCKTRACK is no longer in mapfile! */
	do {
		param1 = OFDM_SC_RA_RAM_LOCKTRACK_MIN;
3704 3705 3706
		status = DVBTScCommand(state, OFDM_SC_RA_RAM_CMD_PROC_START, 0, OFDM_SC_RA_RAM_SW_EVENT_RUN_NMASK__M, param1, 0, 0, 0);
		if (status < 0)
			break;
3707
		/* Start FEC OC */
3708 3709 3710
		status = MPEGTSStart(state);
		if (status < 0)
			break;
3711
		status = write16(state, FEC_COMM_EXEC__A, FEC_COMM_EXEC_ACTIVE);
3712 3713
		if (status < 0)
			break;
3714 3715
	} while (0);
	return status;
R
Ralph Metzler 已提交
3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726
}


/*============================================================================*/

/**
* \brief Set up dvbt demodulator for channel.
* \param demod instance of demodulator.
* \return DRXStatus_t.
* // original DVBTSetChannel()
*/
3727 3728
static int SetDVBT(struct drxk_state *state, u16 IntermediateFreqkHz,
		   s32 tunerFreqOffset)
R
Ralph Metzler 已提交
3729
{
3730 3731 3732 3733 3734 3735
	u16 cmdResult = 0;
	u16 transmissionParams = 0;
	u16 operationMode = 0;
	u32 iqmRcRateOfs = 0;
	u32 bandwidth = 0;
	u16 param1;
R
Ralph Metzler 已提交
3736 3737
	int status;

3738
	dprintk(1, "\n");
3739
	/* printk(KERN_DEBUG "drxk: %s IF =%d, TFO = %d\n", __func__, IntermediateFreqkHz, tunerFreqOffset); */
R
Ralph Metzler 已提交
3740
	do {
3741 3742 3743
		status = scu_command(state, SCU_RAM_COMMAND_STANDARD_OFDM | SCU_RAM_COMMAND_CMD_DEMOD_STOP, 0, NULL, 1, &cmdResult);
		if (status < 0)
			break;
R
Ralph Metzler 已提交
3744 3745

		/* Halt SCU to enable safe non-atomic accesses */
3746
		status = write16(state, SCU_COMM_EXEC__A, SCU_COMM_EXEC_HOLD);
3747 3748
		if (status < 0)
			break;
R
Ralph Metzler 已提交
3749 3750

		/* Stop processors */
3751
		status = write16(state, OFDM_SC_COMM_EXEC__A, OFDM_SC_COMM_EXEC_STOP);
3752 3753
		if (status < 0)
			break;
3754
		status = write16(state, OFDM_LC_COMM_EXEC__A, OFDM_LC_COMM_EXEC_STOP);
3755 3756
		if (status < 0)
			break;
R
Ralph Metzler 已提交
3757 3758 3759

		/* Mandatory fix, always stop CP, required to set spl offset back to
		   hardware default (is set to 0 by ucode during pilot detection */
3760
		status = write16(state, OFDM_CP_COMM_EXEC__A, OFDM_CP_COMM_EXEC_STOP);
3761 3762
		if (status < 0)
			break;
R
Ralph Metzler 已提交
3763 3764 3765 3766

		/*== Write channel settings to device =====================================*/

		/* mode */
3767
		switch (state->param.u.ofdm.transmission_mode) {
R
Ralph Metzler 已提交
3768 3769 3770 3771 3772
		case TRANSMISSION_MODE_AUTO:
		default:
			operationMode |= OFDM_SC_RA_RAM_OP_AUTO_MODE__M;
			/* fall through , try first guess DRX_FFTMODE_8K */
		case TRANSMISSION_MODE_8K:
3773 3774
			transmissionParams |=
			    OFDM_SC_RA_RAM_OP_PARAM_MODE_8K;
R
Ralph Metzler 已提交
3775 3776
			break;
		case TRANSMISSION_MODE_2K:
3777 3778
			transmissionParams |=
			    OFDM_SC_RA_RAM_OP_PARAM_MODE_2K;
R
Ralph Metzler 已提交
3779 3780 3781 3782
			break;
		}

		/* guard */
3783
		switch (state->param.u.ofdm.guard_interval) {
R
Ralph Metzler 已提交
3784 3785 3786 3787 3788
		default:
		case GUARD_INTERVAL_AUTO:
			operationMode |= OFDM_SC_RA_RAM_OP_AUTO_GUARD__M;
			/* fall through , try first guess DRX_GUARD_1DIV4 */
		case GUARD_INTERVAL_1_4:
3789 3790
			transmissionParams |=
			    OFDM_SC_RA_RAM_OP_PARAM_GUARD_4;
R
Ralph Metzler 已提交
3791 3792
			break;
		case GUARD_INTERVAL_1_32:
3793 3794
			transmissionParams |=
			    OFDM_SC_RA_RAM_OP_PARAM_GUARD_32;
R
Ralph Metzler 已提交
3795 3796
			break;
		case GUARD_INTERVAL_1_16:
3797 3798
			transmissionParams |=
			    OFDM_SC_RA_RAM_OP_PARAM_GUARD_16;
R
Ralph Metzler 已提交
3799 3800
			break;
		case GUARD_INTERVAL_1_8:
3801 3802
			transmissionParams |=
			    OFDM_SC_RA_RAM_OP_PARAM_GUARD_8;
R
Ralph Metzler 已提交
3803 3804 3805 3806
			break;
		}

		/* hierarchy */
3807
		switch (state->param.u.ofdm.hierarchy_information) {
R
Ralph Metzler 已提交
3808
		case HIERARCHY_AUTO:
3809
		case HIERARCHY_NONE:
R
Ralph Metzler 已提交
3810 3811 3812
		default:
			operationMode |= OFDM_SC_RA_RAM_OP_AUTO_HIER__M;
			/* fall through , try first guess SC_RA_RAM_OP_PARAM_HIER_NO */
3813 3814 3815 3816 3817
			/* transmissionParams |= OFDM_SC_RA_RAM_OP_PARAM_HIER_NO; */
			/* break; */
		case HIERARCHY_1:
			transmissionParams |=
			    OFDM_SC_RA_RAM_OP_PARAM_HIER_A1;
R
Ralph Metzler 已提交
3818
			break;
3819 3820 3821
		case HIERARCHY_2:
			transmissionParams |=
			    OFDM_SC_RA_RAM_OP_PARAM_HIER_A2;
R
Ralph Metzler 已提交
3822
			break;
3823 3824 3825
		case HIERARCHY_4:
			transmissionParams |=
			    OFDM_SC_RA_RAM_OP_PARAM_HIER_A4;
R
Ralph Metzler 已提交
3826 3827 3828 3829 3830
			break;
		}


		/* constellation */
3831
		switch (state->param.u.ofdm.constellation) {
R
Ralph Metzler 已提交
3832 3833 3834 3835 3836
		case QAM_AUTO:
		default:
			operationMode |= OFDM_SC_RA_RAM_OP_AUTO_CONST__M;
			/* fall through , try first guess DRX_CONSTELLATION_QAM64 */
		case QAM_64:
3837 3838
			transmissionParams |=
			    OFDM_SC_RA_RAM_OP_PARAM_CONST_QAM64;
R
Ralph Metzler 已提交
3839 3840
			break;
		case QPSK:
3841 3842
			transmissionParams |=
			    OFDM_SC_RA_RAM_OP_PARAM_CONST_QPSK;
R
Ralph Metzler 已提交
3843 3844
			break;
		case QAM_16:
3845 3846
			transmissionParams |=
			    OFDM_SC_RA_RAM_OP_PARAM_CONST_QAM16;
R
Ralph Metzler 已提交
3847 3848 3849
			break;
		}
#if 0
3850 3851 3852 3853 3854 3855 3856 3857 3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868 3869
		/* No hierachical channels support in BDA */
		/* Priority (only for hierarchical channels) */
		switch (channel->priority) {
		case DRX_PRIORITY_LOW:
			transmissionParams |=
			    OFDM_SC_RA_RAM_OP_PARAM_PRIO_LO;
			WR16(devAddr, OFDM_EC_SB_PRIOR__A,
			     OFDM_EC_SB_PRIOR_LO);
			break;
		case DRX_PRIORITY_HIGH:
			transmissionParams |=
			    OFDM_SC_RA_RAM_OP_PARAM_PRIO_HI;
			WR16(devAddr, OFDM_EC_SB_PRIOR__A,
			     OFDM_EC_SB_PRIOR_HI));
			break;
		case DRX_PRIORITY_UNKNOWN:	/* fall through */
		default:
			return DRX_STS_INVALID_ARG;
			break;
		}
R
Ralph Metzler 已提交
3870
#else
3871
		/* Set Priorty high */
R
Ralph Metzler 已提交
3872
		transmissionParams |= OFDM_SC_RA_RAM_OP_PARAM_PRIO_HI;
3873
		status = write16(state, OFDM_EC_SB_PRIOR__A, OFDM_EC_SB_PRIOR_HI);
3874 3875
		if (status < 0)
			break;
R
Ralph Metzler 已提交
3876 3877 3878
#endif

		/* coderate */
3879
		switch (state->param.u.ofdm.code_rate_HP) {
R
Ralph Metzler 已提交
3880 3881 3882 3883
		case FEC_AUTO:
		default:
			operationMode |= OFDM_SC_RA_RAM_OP_AUTO_RATE__M;
			/* fall through , try first guess DRX_CODERATE_2DIV3 */
3884 3885 3886
		case FEC_2_3:
			transmissionParams |=
			    OFDM_SC_RA_RAM_OP_PARAM_RATE_2_3;
R
Ralph Metzler 已提交
3887
			break;
3888 3889 3890
		case FEC_1_2:
			transmissionParams |=
			    OFDM_SC_RA_RAM_OP_PARAM_RATE_1_2;
R
Ralph Metzler 已提交
3891
			break;
3892 3893 3894
		case FEC_3_4:
			transmissionParams |=
			    OFDM_SC_RA_RAM_OP_PARAM_RATE_3_4;
R
Ralph Metzler 已提交
3895
			break;
3896 3897 3898
		case FEC_5_6:
			transmissionParams |=
			    OFDM_SC_RA_RAM_OP_PARAM_RATE_5_6;
R
Ralph Metzler 已提交
3899
			break;
3900 3901 3902
		case FEC_7_8:
			transmissionParams |=
			    OFDM_SC_RA_RAM_OP_PARAM_RATE_7_8;
R
Ralph Metzler 已提交
3903 3904 3905 3906 3907 3908 3909 3910 3911 3912
			break;
		}

		/* SAW filter selection: normaly not necesarry, but if wanted
		   the application can select a SAW filter via the driver by using UIOs */
		/* First determine real bandwidth (Hz) */
		/* Also set delay for impulse noise cruncher */
		/* Also set parameters for EC_OC fix, note EC_OC_REG_TMD_HIL_MAR is changed
		   by SC for fix for some 8K,1/8 guard but is restored by InitEC and ResetEC
		   functions */
3913
		switch (state->param.u.ofdm.bandwidth) {
R
Ralph Metzler 已提交
3914 3915 3916
		case BANDWIDTH_AUTO:
		case BANDWIDTH_8_MHZ:
			bandwidth = DRXK_BANDWIDTH_8MHZ_IN_HZ;
3917
			status = write16(state, OFDM_SC_RA_RAM_SRMM_FIX_FACT_8K__A, 3052);
3918 3919
			if (status < 0)
				break;
R
Ralph Metzler 已提交
3920
			/* cochannel protection for PAL 8 MHz */
3921
			status = write16(state, OFDM_SC_RA_RAM_NI_INIT_8K_PER_LEFT__A, 7);
3922 3923
			if (status < 0)
				break;
3924
			status = write16(state, OFDM_SC_RA_RAM_NI_INIT_8K_PER_RIGHT__A, 7);
3925 3926
			if (status < 0)
				break;
3927
			status = write16(state, OFDM_SC_RA_RAM_NI_INIT_2K_PER_LEFT__A, 7);
3928 3929
			if (status < 0)
				break;
3930
			status = write16(state, OFDM_SC_RA_RAM_NI_INIT_2K_PER_RIGHT__A, 1);
3931 3932
			if (status < 0)
				break;
R
Ralph Metzler 已提交
3933 3934 3935
			break;
		case BANDWIDTH_7_MHZ:
			bandwidth = DRXK_BANDWIDTH_7MHZ_IN_HZ;
3936
			status = write16(state, OFDM_SC_RA_RAM_SRMM_FIX_FACT_8K__A, 3491);
3937 3938
			if (status < 0)
				break;
R
Ralph Metzler 已提交
3939
			/* cochannel protection for PAL 7 MHz */
3940
			status = write16(state, OFDM_SC_RA_RAM_NI_INIT_8K_PER_LEFT__A, 8);
3941 3942
			if (status < 0)
				break;
3943
			status = write16(state, OFDM_SC_RA_RAM_NI_INIT_8K_PER_RIGHT__A, 8);
3944 3945
			if (status < 0)
				break;
3946
			status = write16(state, OFDM_SC_RA_RAM_NI_INIT_2K_PER_LEFT__A, 4);
3947 3948
			if (status < 0)
				break;
3949
			status = write16(state, OFDM_SC_RA_RAM_NI_INIT_2K_PER_RIGHT__A, 1);
3950 3951
			if (status < 0)
				break;
R
Ralph Metzler 已提交
3952 3953 3954
			break;
		case BANDWIDTH_6_MHZ:
			bandwidth = DRXK_BANDWIDTH_6MHZ_IN_HZ;
3955
			status = write16(state, OFDM_SC_RA_RAM_SRMM_FIX_FACT_8K__A, 4073);
3956 3957
			if (status < 0)
				break;
R
Ralph Metzler 已提交
3958
			/* cochannel protection for NTSC 6 MHz */
3959
			status = write16(state, OFDM_SC_RA_RAM_NI_INIT_8K_PER_LEFT__A, 19);
3960 3961
			if (status < 0)
				break;
3962
			status = write16(state, OFDM_SC_RA_RAM_NI_INIT_8K_PER_RIGHT__A, 19);
3963 3964
			if (status < 0)
				break;
3965
			status = write16(state, OFDM_SC_RA_RAM_NI_INIT_2K_PER_LEFT__A, 14);
3966 3967
			if (status < 0)
				break;
3968
			status = write16(state, OFDM_SC_RA_RAM_NI_INIT_2K_PER_RIGHT__A, 1);
3969 3970
			if (status < 0)
				break;
R
Ralph Metzler 已提交
3971
			break;
3972 3973
		default:
			return -EINVAL;
R
Ralph Metzler 已提交
3974 3975
		}

3976
		if (iqmRcRateOfs == 0) {
R
Ralph Metzler 已提交
3977 3978 3979 3980
			/* Now compute IQM_RC_RATE_OFS
			   (((SysFreq/BandWidth)/2)/2) -1) * 2^23)
			   =>
			   ((SysFreq / BandWidth) * (2^21)) - (2^23)
3981
			 */
R
Ralph Metzler 已提交
3982 3983 3984 3985
			/* (SysFreq / BandWidth) * (2^28)  */
			/* assert (MAX(sysClk)/MIN(bandwidth) < 16)
			   => assert(MAX(sysClk) < 16*MIN(bandwidth))
			   => assert(109714272 > 48000000) = true so Frac 28 can be used  */
3986 3987 3988
			iqmRcRateOfs = Frac28a((u32)
					       ((state->m_sysClockFreq *
						 1000) / 3), bandwidth);
R
Ralph Metzler 已提交
3989 3990 3991
			/* (SysFreq / BandWidth) * (2^21), rounding before truncating  */
			if ((iqmRcRateOfs & 0x7fL) >= 0x40)
				iqmRcRateOfs += 0x80L;
3992
			iqmRcRateOfs = iqmRcRateOfs >> 7;
R
Ralph Metzler 已提交
3993
			/* ((SysFreq / BandWidth) * (2^21)) - (2^23)  */
3994
			iqmRcRateOfs = iqmRcRateOfs - (1 << 23);
R
Ralph Metzler 已提交
3995 3996
		}

3997 3998 3999
		iqmRcRateOfs &=
		    ((((u32) IQM_RC_RATE_OFS_HI__M) <<
		      IQM_RC_RATE_OFS_LO__W) | IQM_RC_RATE_OFS_LO__M);
4000
		status = write32(state, IQM_RC_RATE_OFS_LO__A, iqmRcRateOfs);
4001 4002
		if (status < 0)
			break;
R
Ralph Metzler 已提交
4003 4004 4005

		/* Bandwidth setting done */

4006 4007 4008 4009 4010 4011 4012 4013
#if 0
		status = DVBTSetFrequencyShift(demod, channel, tunerOffset);
		if (status < 0)
			break;
#endif
		status = SetFrequencyShifter(state, IntermediateFreqkHz, tunerFreqOffset, true);
		if (status < 0)
			break;
R
Ralph Metzler 已提交
4014 4015 4016 4017

		/*== Start SC, write channel settings to SC ===============================*/

		/* Activate SCU to enable SCU commands */
4018
		status = write16(state, SCU_COMM_EXEC__A, SCU_COMM_EXEC_ACTIVE);
4019 4020
		if (status < 0)
			break;
R
Ralph Metzler 已提交
4021 4022

		/* Enable SC after setting all other parameters */
4023
		status = write16(state, OFDM_SC_COMM_STATE__A, 0);
4024 4025
		if (status < 0)
			break;
4026
		status = write16(state, OFDM_SC_COMM_EXEC__A, 1);
4027 4028
		if (status < 0)
			break;
R
Ralph Metzler 已提交
4029 4030


4031 4032 4033
		status = scu_command(state, SCU_RAM_COMMAND_STANDARD_OFDM | SCU_RAM_COMMAND_CMD_DEMOD_START, 0, NULL, 1, &cmdResult);
		if (status < 0)
			break;
R
Ralph Metzler 已提交
4034 4035

		/* Write SC parameter registers, set all AUTO flags in operation mode */
4036 4037 4038 4039 4040 4041 4042 4043
		param1 = (OFDM_SC_RA_RAM_OP_AUTO_MODE__M |
			  OFDM_SC_RA_RAM_OP_AUTO_GUARD__M |
			  OFDM_SC_RA_RAM_OP_AUTO_CONST__M |
			  OFDM_SC_RA_RAM_OP_AUTO_HIER__M |
			  OFDM_SC_RA_RAM_OP_AUTO_RATE__M);
		status =
		    DVBTScCommand(state, OFDM_SC_RA_RAM_CMD_SET_PREF_PARAM,
				  0, transmissionParams, param1, 0, 0, 0);
R
Ralph Metzler 已提交
4044
		if (!state->m_DRXK_A3_ROM_CODE)
4045 4046 4047
			status = DVBTCtrlSetSqiSpeed(state, &state->m_sqiSpeed);
			if (status < 0)
				break;
R
Ralph Metzler 已提交
4048

4049
	} while (0);
R
Ralph Metzler 已提交
4050 4051 4052 4053 4054 4055 4056 4057 4058 4059 4060 4061 4062 4063 4064 4065

	return status;
}


/*============================================================================*/

/**
* \brief Retreive lock status .
* \param demod    Pointer to demodulator instance.
* \param lockStat Pointer to lock status structure.
* \return DRXStatus_t.
*
*/
static int GetDVBTLockStatus(struct drxk_state *state, u32 *pLockStatus)
{
4066 4067 4068 4069 4070 4071 4072 4073 4074
	int status;
	const u16 mpeg_lock_mask = (OFDM_SC_RA_RAM_LOCK_MPEG__M |
				    OFDM_SC_RA_RAM_LOCK_FEC__M);
	const u16 fec_lock_mask = (OFDM_SC_RA_RAM_LOCK_FEC__M);
	const u16 demod_lock_mask = OFDM_SC_RA_RAM_LOCK_DEMOD__M;

	u16 ScRaRamLock = 0;
	u16 ScCommExec = 0;

4075 4076
	dprintk(1, "\n");

4077 4078
	/* driver 0.9.0 */
	/* Check if SC is running */
4079
	status = read16(state, OFDM_SC_COMM_EXEC__A, &ScCommExec);
4080 4081 4082 4083 4084 4085
	if (ScCommExec == OFDM_SC_COMM_EXEC_STOP) {
		/* SC not active; return DRX_NOT_LOCKED */
		*pLockStatus = NOT_LOCKED;
		return status;
	}

4086
	status = read16(state, OFDM_SC_RA_RAM_LOCK__A, &ScRaRamLock);
4087 4088 4089 4090 4091 4092 4093 4094 4095 4096 4097 4098 4099

	if ((ScRaRamLock & mpeg_lock_mask) == mpeg_lock_mask)
		*pLockStatus = MPEG_LOCK;
	else if ((ScRaRamLock & fec_lock_mask) == fec_lock_mask)
		*pLockStatus = FEC_LOCK;
	else if ((ScRaRamLock & demod_lock_mask) == demod_lock_mask)
		*pLockStatus = DEMOD_LOCK;
	else if (ScRaRamLock & OFDM_SC_RA_RAM_LOCK_NODVBT__M)
		*pLockStatus = NEVER_LOCK;
	else
		*pLockStatus = NOT_LOCKED;

	return status;
R
Ralph Metzler 已提交
4100 4101
}

4102
static int PowerUpQAM(struct drxk_state *state)
R
Ralph Metzler 已提交
4103
{
4104 4105
	enum DRXPowerMode powerMode = DRXK_POWER_DOWN_OFDM;
	int status = 0;
R
Ralph Metzler 已提交
4106

4107
	dprintk(1, "\n");
4108
	do {
4109 4110 4111
		status = CtrlPowerMode(state, &powerMode);
		if (status < 0)
			break;
R
Ralph Metzler 已提交
4112

4113
	} while (0);
R
Ralph Metzler 已提交
4114

4115
	return status;
R
Ralph Metzler 已提交
4116 4117 4118
}


4119
/** Power Down QAM */
R
Ralph Metzler 已提交
4120 4121
static int PowerDownQAM(struct drxk_state *state)
{
4122 4123 4124 4125
	u16 data = 0;
	u16 cmdResult;
	int status = 0;

4126
	dprintk(1, "\n");
4127
	do {
4128
		status = read16(state, SCU_COMM_EXEC__A, &data);
4129 4130
		if (status < 0)
			break;
4131 4132 4133 4134 4135 4136
		if (data == SCU_COMM_EXEC_ACTIVE) {
			/*
			   STOP demodulator
			   QAM and HW blocks
			 */
			/* stop all comstate->m_exec */
4137
			status = write16(state, QAM_COMM_EXEC__A, QAM_COMM_EXEC_STOP);
4138 4139 4140 4141 4142
			if (status < 0)
				break;
			status = scu_command(state, SCU_RAM_COMMAND_STANDARD_QAM | SCU_RAM_COMMAND_CMD_DEMOD_STOP, 0, NULL, 1, &cmdResult);
			if (status < 0)
				break;
4143 4144
		}
		/* powerdown AFE                   */
4145 4146 4147
		status = SetIqmAf(state, false);
		if (status < 0)
			break;
4148 4149 4150
	} while (0);

	return status;
R
Ralph Metzler 已提交
4151
}
4152

R
Ralph Metzler 已提交
4153 4154 4155 4156 4157 4158 4159 4160 4161 4162 4163 4164 4165 4166 4167 4168 4169
/*============================================================================*/

/**
* \brief Setup of the QAM Measurement intervals for signal quality
* \param demod instance of demod.
* \param constellation current constellation.
* \return DRXStatus_t.
*
*  NOTE:
*  Take into account that for certain settings the errorcounters can overflow.
*  The implementation does not check this.
*
*/
static int SetQAMMeasurement(struct drxk_state *state,
			     enum EDrxkConstellation constellation,
			     u32 symbolRate)
{
4170 4171 4172 4173
	u32 fecBitsDesired = 0;	/* BER accounting period */
	u32 fecRsPeriodTotal = 0;	/* Total period */
	u16 fecRsPrescale = 0;	/* ReedSolomon Measurement Prescale */
	u16 fecRsPeriod = 0;	/* Value for corresponding I2C register */
R
Ralph Metzler 已提交
4174 4175
	int status = 0;

4176
	dprintk(1, "\n");
R
Ralph Metzler 已提交
4177

4178
	fecRsPrescale = 1;
R
Ralph Metzler 已提交
4179 4180 4181 4182 4183 4184 4185
	do {

		/* fecBitsDesired = symbolRate [kHz] *
		   FrameLenght [ms] *
		   (constellation + 1) *
		   SyncLoss (== 1) *
		   ViterbiLoss (==1)
4186 4187
		 */
		switch (constellation) {
R
Ralph Metzler 已提交
4188 4189 4190 4191 4192 4193 4194 4195 4196 4197 4198 4199 4200 4201 4202 4203 4204 4205
		case DRX_CONSTELLATION_QAM16:
			fecBitsDesired = 4 * symbolRate;
			break;
		case DRX_CONSTELLATION_QAM32:
			fecBitsDesired = 5 * symbolRate;
			break;
		case DRX_CONSTELLATION_QAM64:
			fecBitsDesired = 6 * symbolRate;
			break;
		case DRX_CONSTELLATION_QAM128:
			fecBitsDesired = 7 * symbolRate;
			break;
		case DRX_CONSTELLATION_QAM256:
			fecBitsDesired = 8 * symbolRate;
			break;
		default:
			status = -EINVAL;
		}
4206 4207 4208
		status = status;
		if (status < 0)
			break;
R
Ralph Metzler 已提交
4209

4210 4211
		fecBitsDesired /= 1000;	/* symbolRate [Hz] -> symbolRate [kHz]  */
		fecBitsDesired *= 500;	/* meas. period [ms] */
R
Ralph Metzler 已提交
4212 4213 4214

		/* Annex A/C: bits/RsPeriod = 204 * 8 = 1632 */
		/* fecRsPeriodTotal = fecBitsDesired / 1632 */
4215
		fecRsPeriodTotal = (fecBitsDesired / 1632UL) + 1;	/* roughly ceil */
R
Ralph Metzler 已提交
4216 4217 4218 4219 4220 4221 4222

		/* fecRsPeriodTotal =  fecRsPrescale * fecRsPeriod  */
		fecRsPrescale = 1 + (u16) (fecRsPeriodTotal >> 16);
		if (fecRsPrescale == 0) {
			/* Divide by zero (though impossible) */
			status = -1;
		}
4223 4224 4225
		status = status;
		if (status < 0)
			break;
4226 4227 4228
		fecRsPeriod =
		    ((u16) fecRsPeriodTotal +
		     (fecRsPrescale >> 1)) / fecRsPrescale;
R
Ralph Metzler 已提交
4229 4230

		/* write corresponding registers */
4231
		status = write16(state, FEC_RS_MEASUREMENT_PERIOD__A, fecRsPeriod);
4232 4233
		if (status < 0)
			break;
4234
		status = write16(state, FEC_RS_MEASUREMENT_PRESCALE__A, fecRsPrescale);
4235 4236
		if (status < 0)
			break;
4237
		status = write16(state, FEC_OC_SNC_FAIL_PERIOD__A, fecRsPeriod);
4238 4239
		if (status < 0)
			break;
R
Ralph Metzler 已提交
4240 4241 4242

	} while (0);

4243
	if (status < 0)
4244
		printk(KERN_ERR "drxk: %s: status - %08x\n", __func__, status);
4245

R
Ralph Metzler 已提交
4246 4247 4248
	return status;
}

4249
static int SetQAM16(struct drxk_state *state)
R
Ralph Metzler 已提交
4250
{
4251 4252
	int status = 0;

4253
	dprintk(1, "\n");
4254 4255 4256
	do {
		/* QAM Equalizer Setup */
		/* Equalizer */
4257
		status = write16(state, SCU_RAM_QAM_EQ_CMA_RAD0__A, 13517);
4258 4259
		if (status < 0)
			break;
4260
		status = write16(state, SCU_RAM_QAM_EQ_CMA_RAD1__A, 13517);
4261 4262
		if (status < 0)
			break;
4263
		status = write16(state, SCU_RAM_QAM_EQ_CMA_RAD2__A, 13517);
4264 4265
		if (status < 0)
			break;
4266
		status = write16(state, SCU_RAM_QAM_EQ_CMA_RAD3__A, 13517);
4267 4268
		if (status < 0)
			break;
4269
		status = write16(state, SCU_RAM_QAM_EQ_CMA_RAD4__A, 13517);
4270 4271
		if (status < 0)
			break;
4272
		status = write16(state, SCU_RAM_QAM_EQ_CMA_RAD5__A, 13517);
4273 4274
		if (status < 0)
			break;
4275
		/* Decision Feedback Equalizer */
4276
		status = write16(state, QAM_DQ_QUAL_FUN0__A, 2);
4277 4278
		if (status < 0)
			break;
4279
		status = write16(state, QAM_DQ_QUAL_FUN1__A, 2);
4280 4281
		if (status < 0)
			break;
4282
		status = write16(state, QAM_DQ_QUAL_FUN2__A, 2);
4283 4284
		if (status < 0)
			break;
4285
		status = write16(state, QAM_DQ_QUAL_FUN3__A, 2);
4286 4287
		if (status < 0)
			break;
4288
		status = write16(state, QAM_DQ_QUAL_FUN4__A, 2);
4289 4290
		if (status < 0)
			break;
4291
		status = write16(state, QAM_DQ_QUAL_FUN5__A, 0);
4292 4293
		if (status < 0)
			break;
4294

4295
		status = write16(state, QAM_SY_SYNC_HWM__A, 5);
4296 4297
		if (status < 0)
			break;
4298
		status = write16(state, QAM_SY_SYNC_AWM__A, 4);
4299 4300
		if (status < 0)
			break;
4301
		status = write16(state, QAM_SY_SYNC_LWM__A, 3);
4302 4303
		if (status < 0)
			break;
4304 4305

		/* QAM Slicer Settings */
4306
		status = write16(state, SCU_RAM_QAM_SL_SIG_POWER__A, DRXK_QAM_SL_SIG_POWER_QAM16);
4307 4308
		if (status < 0)
			break;
4309 4310

		/* QAM Loop Controller Coeficients */
4311
		status = write16(state, SCU_RAM_QAM_LC_CA_FINE__A, 15);
4312 4313
		if (status < 0)
			break;
4314
		status = write16(state, SCU_RAM_QAM_LC_CA_COARSE__A, 40);
4315 4316
		if (status < 0)
			break;
4317
		status = write16(state, SCU_RAM_QAM_LC_EP_FINE__A, 12);
4318 4319
		if (status < 0)
			break;
4320
		status = write16(state, SCU_RAM_QAM_LC_EP_MEDIUM__A, 24);
4321 4322
		if (status < 0)
			break;
4323
		status = write16(state, SCU_RAM_QAM_LC_EP_COARSE__A, 24);
4324 4325
		if (status < 0)
			break;
4326
		status = write16(state, SCU_RAM_QAM_LC_EI_FINE__A, 12);
4327 4328
		if (status < 0)
			break;
4329
		status = write16(state, SCU_RAM_QAM_LC_EI_MEDIUM__A, 16);
4330 4331
		if (status < 0)
			break;
4332
		status = write16(state, SCU_RAM_QAM_LC_EI_COARSE__A, 16);
4333 4334 4335
		if (status < 0)
			break;

4336
		status = write16(state, SCU_RAM_QAM_LC_CP_FINE__A, 5);
4337 4338
		if (status < 0)
			break;
4339
		status = write16(state, SCU_RAM_QAM_LC_CP_MEDIUM__A, 20);
4340 4341
		if (status < 0)
			break;
4342
		status = write16(state, SCU_RAM_QAM_LC_CP_COARSE__A, 80);
4343 4344
		if (status < 0)
			break;
4345
		status = write16(state, SCU_RAM_QAM_LC_CI_FINE__A, 5);
4346 4347
		if (status < 0)
			break;
4348
		status = write16(state, SCU_RAM_QAM_LC_CI_MEDIUM__A, 20);
4349 4350
		if (status < 0)
			break;
4351
		status = write16(state, SCU_RAM_QAM_LC_CI_COARSE__A, 50);
4352 4353
		if (status < 0)
			break;
4354
		status = write16(state, SCU_RAM_QAM_LC_CF_FINE__A, 16);
4355 4356
		if (status < 0)
			break;
4357
		status = write16(state, SCU_RAM_QAM_LC_CF_MEDIUM__A, 16);
4358 4359
		if (status < 0)
			break;
4360
		status = write16(state, SCU_RAM_QAM_LC_CF_COARSE__A, 32);
4361 4362
		if (status < 0)
			break;
4363
		status = write16(state, SCU_RAM_QAM_LC_CF1_FINE__A, 5);
4364 4365
		if (status < 0)
			break;
4366
		status = write16(state, SCU_RAM_QAM_LC_CF1_MEDIUM__A, 10);
4367 4368
		if (status < 0)
			break;
4369
		status = write16(state, SCU_RAM_QAM_LC_CF1_COARSE__A, 10);
4370 4371
		if (status < 0)
			break;
4372 4373 4374 4375


		/* QAM State Machine (FSM) Thresholds */

4376
		status = write16(state, SCU_RAM_QAM_FSM_RTH__A, 140);
4377 4378
		if (status < 0)
			break;
4379
		status = write16(state, SCU_RAM_QAM_FSM_FTH__A, 50);
4380 4381
		if (status < 0)
			break;
4382
		status = write16(state, SCU_RAM_QAM_FSM_CTH__A, 95);
4383 4384
		if (status < 0)
			break;
4385
		status = write16(state, SCU_RAM_QAM_FSM_PTH__A, 120);
4386 4387
		if (status < 0)
			break;
4388
		status = write16(state, SCU_RAM_QAM_FSM_QTH__A, 230);
4389 4390
		if (status < 0)
			break;
4391
		status = write16(state, SCU_RAM_QAM_FSM_MTH__A, 105);
4392 4393
		if (status < 0)
			break;
4394

4395
		status = write16(state, SCU_RAM_QAM_FSM_RATE_LIM__A, 40);
4396 4397
		if (status < 0)
			break;
4398
		status = write16(state, SCU_RAM_QAM_FSM_COUNT_LIM__A, 4);
4399 4400
		if (status < 0)
			break;
4401
		status = write16(state, SCU_RAM_QAM_FSM_FREQ_LIM__A, 24);
4402 4403
		if (status < 0)
			break;
4404 4405 4406 4407


		/* QAM FSM Tracking Parameters */

4408
		status = write16(state, SCU_RAM_QAM_FSM_MEDIAN_AV_MULT__A, (u16) 16);
4409 4410
		if (status < 0)
			break;
4411
		status = write16(state, SCU_RAM_QAM_FSM_RADIUS_AV_LIMIT__A, (u16) 220);
4412 4413
		if (status < 0)
			break;
4414
		status = write16(state, SCU_RAM_QAM_FSM_LCAVG_OFFSET1__A, (u16) 25);
4415 4416
		if (status < 0)
			break;
4417
		status = write16(state, SCU_RAM_QAM_FSM_LCAVG_OFFSET2__A, (u16) 6);
4418 4419
		if (status < 0)
			break;
4420
		status = write16(state, SCU_RAM_QAM_FSM_LCAVG_OFFSET3__A, (u16) -24);
4421 4422
		if (status < 0)
			break;
4423
		status = write16(state, SCU_RAM_QAM_FSM_LCAVG_OFFSET4__A, (u16) -65);
4424 4425
		if (status < 0)
			break;
4426
		status = write16(state, SCU_RAM_QAM_FSM_LCAVG_OFFSET5__A, (u16) -127);
4427 4428
		if (status < 0)
			break;
4429 4430 4431
	} while (0);

	return status;
R
Ralph Metzler 已提交
4432 4433 4434 4435 4436 4437 4438 4439 4440
}

/*============================================================================*/

/**
* \brief QAM32 specific setup
* \param demod instance of demod.
* \return DRXStatus_t.
*/
4441
static int SetQAM32(struct drxk_state *state)
R
Ralph Metzler 已提交
4442
{
4443 4444
	int status = 0;

4445
	dprintk(1, "\n");
4446 4447 4448
	do {
		/* QAM Equalizer Setup */
		/* Equalizer */
4449
		status = write16(state, SCU_RAM_QAM_EQ_CMA_RAD0__A, 6707);
4450 4451
		if (status < 0)
			break;
4452
		status = write16(state, SCU_RAM_QAM_EQ_CMA_RAD1__A, 6707);
4453 4454
		if (status < 0)
			break;
4455
		status = write16(state, SCU_RAM_QAM_EQ_CMA_RAD2__A, 6707);
4456 4457
		if (status < 0)
			break;
4458
		status = write16(state, SCU_RAM_QAM_EQ_CMA_RAD3__A, 6707);
4459 4460
		if (status < 0)
			break;
4461
		status = write16(state, SCU_RAM_QAM_EQ_CMA_RAD4__A, 6707);
4462 4463
		if (status < 0)
			break;
4464
		status = write16(state, SCU_RAM_QAM_EQ_CMA_RAD5__A, 6707);
4465 4466
		if (status < 0)
			break;
4467 4468

		/* Decision Feedback Equalizer */
4469
		status = write16(state, QAM_DQ_QUAL_FUN0__A, 3);
4470 4471
		if (status < 0)
			break;
4472
		status = write16(state, QAM_DQ_QUAL_FUN1__A, 3);
4473 4474
		if (status < 0)
			break;
4475
		status = write16(state, QAM_DQ_QUAL_FUN2__A, 3);
4476 4477
		if (status < 0)
			break;
4478
		status = write16(state, QAM_DQ_QUAL_FUN3__A, 3);
4479 4480
		if (status < 0)
			break;
4481
		status = write16(state, QAM_DQ_QUAL_FUN4__A, 3);
4482 4483
		if (status < 0)
			break;
4484
		status = write16(state, QAM_DQ_QUAL_FUN5__A, 0);
4485 4486
		if (status < 0)
			break;
4487

4488
		status = write16(state, QAM_SY_SYNC_HWM__A, 6);
4489 4490
		if (status < 0)
			break;
4491
		status = write16(state, QAM_SY_SYNC_AWM__A, 5);
4492 4493
		if (status < 0)
			break;
4494
		status = write16(state, QAM_SY_SYNC_LWM__A, 3);
4495 4496
		if (status < 0)
			break;
4497 4498 4499

		/* QAM Slicer Settings */

4500
		status = write16(state, SCU_RAM_QAM_SL_SIG_POWER__A, DRXK_QAM_SL_SIG_POWER_QAM32);
4501 4502
		if (status < 0)
			break;
4503 4504 4505 4506


		/* QAM Loop Controller Coeficients */

4507
		status = write16(state, SCU_RAM_QAM_LC_CA_FINE__A, 15);
4508 4509
		if (status < 0)
			break;
4510
		status = write16(state, SCU_RAM_QAM_LC_CA_COARSE__A, 40);
4511 4512
		if (status < 0)
			break;
4513
		status = write16(state, SCU_RAM_QAM_LC_EP_FINE__A, 12);
4514 4515
		if (status < 0)
			break;
4516
		status = write16(state, SCU_RAM_QAM_LC_EP_MEDIUM__A, 24);
4517 4518
		if (status < 0)
			break;
4519
		status = write16(state, SCU_RAM_QAM_LC_EP_COARSE__A, 24);
4520 4521
		if (status < 0)
			break;
4522
		status = write16(state, SCU_RAM_QAM_LC_EI_FINE__A, 12);
4523 4524
		if (status < 0)
			break;
4525
		status = write16(state, SCU_RAM_QAM_LC_EI_MEDIUM__A, 16);
4526 4527
		if (status < 0)
			break;
4528
		status = write16(state, SCU_RAM_QAM_LC_EI_COARSE__A, 16);
4529 4530 4531
		if (status < 0)
			break;

4532
		status = write16(state, SCU_RAM_QAM_LC_CP_FINE__A, 5);
4533 4534
		if (status < 0)
			break;
4535
		status = write16(state, SCU_RAM_QAM_LC_CP_MEDIUM__A, 20);
4536 4537
		if (status < 0)
			break;
4538
		status = write16(state, SCU_RAM_QAM_LC_CP_COARSE__A, 80);
4539 4540
		if (status < 0)
			break;
4541
		status = write16(state, SCU_RAM_QAM_LC_CI_FINE__A, 5);
4542 4543
		if (status < 0)
			break;
4544
		status = write16(state, SCU_RAM_QAM_LC_CI_MEDIUM__A, 20);
4545 4546
		if (status < 0)
			break;
4547
		status = write16(state, SCU_RAM_QAM_LC_CI_COARSE__A, 50);
4548 4549
		if (status < 0)
			break;
4550
		status = write16(state, SCU_RAM_QAM_LC_CF_FINE__A, 16);
4551 4552
		if (status < 0)
			break;
4553
		status = write16(state, SCU_RAM_QAM_LC_CF_MEDIUM__A, 16);
4554 4555
		if (status < 0)
			break;
4556
		status = write16(state, SCU_RAM_QAM_LC_CF_COARSE__A, 16);
4557 4558
		if (status < 0)
			break;
4559
		status = write16(state, SCU_RAM_QAM_LC_CF1_FINE__A, 5);
4560 4561
		if (status < 0)
			break;
4562
		status = write16(state, SCU_RAM_QAM_LC_CF1_MEDIUM__A, 10);
4563 4564
		if (status < 0)
			break;
4565
		status = write16(state, SCU_RAM_QAM_LC_CF1_COARSE__A, 0);
4566 4567
		if (status < 0)
			break;
4568 4569 4570 4571


		/* QAM State Machine (FSM) Thresholds */

4572
		status = write16(state, SCU_RAM_QAM_FSM_RTH__A, 90);
4573 4574
		if (status < 0)
			break;
4575
		status = write16(state, SCU_RAM_QAM_FSM_FTH__A, 50);
4576 4577
		if (status < 0)
			break;
4578
		status = write16(state, SCU_RAM_QAM_FSM_CTH__A, 80);
4579 4580
		if (status < 0)
			break;
4581
		status = write16(state, SCU_RAM_QAM_FSM_PTH__A, 100);
4582 4583
		if (status < 0)
			break;
4584
		status = write16(state, SCU_RAM_QAM_FSM_QTH__A, 170);
4585 4586
		if (status < 0)
			break;
4587
		status = write16(state, SCU_RAM_QAM_FSM_MTH__A, 100);
4588 4589
		if (status < 0)
			break;
4590

4591
		status = write16(state, SCU_RAM_QAM_FSM_RATE_LIM__A, 40);
4592 4593
		if (status < 0)
			break;
4594
		status = write16(state, SCU_RAM_QAM_FSM_COUNT_LIM__A, 4);
4595 4596
		if (status < 0)
			break;
4597
		status = write16(state, SCU_RAM_QAM_FSM_FREQ_LIM__A, 10);
4598 4599
		if (status < 0)
			break;
4600 4601 4602 4603


		/* QAM FSM Tracking Parameters */

4604
		status = write16(state, SCU_RAM_QAM_FSM_MEDIAN_AV_MULT__A, (u16) 12);
4605 4606
		if (status < 0)
			break;
4607
		status = write16(state, SCU_RAM_QAM_FSM_RADIUS_AV_LIMIT__A, (u16) 140);
4608 4609
		if (status < 0)
			break;
4610
		status = write16(state, SCU_RAM_QAM_FSM_LCAVG_OFFSET1__A, (u16) -8);
4611 4612
		if (status < 0)
			break;
4613
		status = write16(state, SCU_RAM_QAM_FSM_LCAVG_OFFSET2__A, (u16) -16);
4614 4615
		if (status < 0)
			break;
4616
		status = write16(state, SCU_RAM_QAM_FSM_LCAVG_OFFSET3__A, (u16) -26);
4617 4618
		if (status < 0)
			break;
4619
		status = write16(state, SCU_RAM_QAM_FSM_LCAVG_OFFSET4__A, (u16) -56);
4620 4621
		if (status < 0)
			break;
4622
		status = write16(state, SCU_RAM_QAM_FSM_LCAVG_OFFSET5__A, (u16) -86);
4623 4624
		if (status < 0)
			break;
4625 4626 4627
	} while (0);

	return status;
R
Ralph Metzler 已提交
4628 4629 4630 4631 4632 4633 4634 4635 4636
}

/*============================================================================*/

/**
* \brief QAM64 specific setup
* \param demod instance of demod.
* \return DRXStatus_t.
*/
4637
static int SetQAM64(struct drxk_state *state)
R
Ralph Metzler 已提交
4638
{
4639 4640
	int status = 0;

4641
	dprintk(1, "\n");
4642 4643 4644
	do {
		/* QAM Equalizer Setup */
		/* Equalizer */
4645
		status = write16(state, SCU_RAM_QAM_EQ_CMA_RAD0__A, 13336);
4646 4647
		if (status < 0)
			break;
4648
		status = write16(state, SCU_RAM_QAM_EQ_CMA_RAD1__A, 12618);
4649 4650
		if (status < 0)
			break;
4651
		status = write16(state, SCU_RAM_QAM_EQ_CMA_RAD2__A, 11988);
4652 4653
		if (status < 0)
			break;
4654
		status = write16(state, SCU_RAM_QAM_EQ_CMA_RAD3__A, 13809);
4655 4656
		if (status < 0)
			break;
4657
		status = write16(state, SCU_RAM_QAM_EQ_CMA_RAD4__A, 13809);
4658 4659
		if (status < 0)
			break;
4660
		status = write16(state, SCU_RAM_QAM_EQ_CMA_RAD5__A, 15609);
4661 4662
		if (status < 0)
			break;
4663 4664

		/* Decision Feedback Equalizer */
4665
		status = write16(state, QAM_DQ_QUAL_FUN0__A, 4);
4666 4667
		if (status < 0)
			break;
4668
		status = write16(state, QAM_DQ_QUAL_FUN1__A, 4);
4669 4670
		if (status < 0)
			break;
4671
		status = write16(state, QAM_DQ_QUAL_FUN2__A, 4);
4672 4673
		if (status < 0)
			break;
4674
		status = write16(state, QAM_DQ_QUAL_FUN3__A, 4);
4675 4676
		if (status < 0)
			break;
4677
		status = write16(state, QAM_DQ_QUAL_FUN4__A, 3);
4678 4679
		if (status < 0)
			break;
4680
		status = write16(state, QAM_DQ_QUAL_FUN5__A, 0);
4681 4682
		if (status < 0)
			break;
4683

4684
		status = write16(state, QAM_SY_SYNC_HWM__A, 5);
4685 4686
		if (status < 0)
			break;
4687
		status = write16(state, QAM_SY_SYNC_AWM__A, 4);
4688 4689
		if (status < 0)
			break;
4690
		status = write16(state, QAM_SY_SYNC_LWM__A, 3);
4691 4692
		if (status < 0)
			break;
4693 4694

		/* QAM Slicer Settings */
4695
		status = write16(state, SCU_RAM_QAM_SL_SIG_POWER__A, DRXK_QAM_SL_SIG_POWER_QAM64);
4696 4697
		if (status < 0)
			break;
4698 4699 4700 4701


		/* QAM Loop Controller Coeficients */

4702
		status = write16(state, SCU_RAM_QAM_LC_CA_FINE__A, 15);
4703 4704
		if (status < 0)
			break;
4705
		status = write16(state, SCU_RAM_QAM_LC_CA_COARSE__A, 40);
4706 4707
		if (status < 0)
			break;
4708
		status = write16(state, SCU_RAM_QAM_LC_EP_FINE__A, 12);
4709 4710
		if (status < 0)
			break;
4711
		status = write16(state, SCU_RAM_QAM_LC_EP_MEDIUM__A, 24);
4712 4713
		if (status < 0)
			break;
4714
		status = write16(state, SCU_RAM_QAM_LC_EP_COARSE__A, 24);
4715 4716
		if (status < 0)
			break;
4717
		status = write16(state, SCU_RAM_QAM_LC_EI_FINE__A, 12);
4718 4719
		if (status < 0)
			break;
4720
		status = write16(state, SCU_RAM_QAM_LC_EI_MEDIUM__A, 16);
4721 4722
		if (status < 0)
			break;
4723
		status = write16(state, SCU_RAM_QAM_LC_EI_COARSE__A, 16);
4724 4725 4726
		if (status < 0)
			break;

4727
		status = write16(state, SCU_RAM_QAM_LC_CP_FINE__A, 5);
4728 4729
		if (status < 0)
			break;
4730
		status = write16(state, SCU_RAM_QAM_LC_CP_MEDIUM__A, 30);
4731 4732
		if (status < 0)
			break;
4733
		status = write16(state, SCU_RAM_QAM_LC_CP_COARSE__A, 100);
4734 4735
		if (status < 0)
			break;
4736
		status = write16(state, SCU_RAM_QAM_LC_CI_FINE__A, 5);
4737 4738
		if (status < 0)
			break;
4739
		status = write16(state, SCU_RAM_QAM_LC_CI_MEDIUM__A, 30);
4740 4741
		if (status < 0)
			break;
4742
		status = write16(state, SCU_RAM_QAM_LC_CI_COARSE__A, 50);
4743 4744
		if (status < 0)
			break;
4745
		status = write16(state, SCU_RAM_QAM_LC_CF_FINE__A, 16);
4746 4747
		if (status < 0)
			break;
4748
		status = write16(state, SCU_RAM_QAM_LC_CF_MEDIUM__A, 25);
4749 4750
		if (status < 0)
			break;
4751
		status = write16(state, SCU_RAM_QAM_LC_CF_COARSE__A, 48);
4752 4753
		if (status < 0)
			break;
4754
		status = write16(state, SCU_RAM_QAM_LC_CF1_FINE__A, 5);
4755 4756
		if (status < 0)
			break;
4757
		status = write16(state, SCU_RAM_QAM_LC_CF1_MEDIUM__A, 10);
4758 4759
		if (status < 0)
			break;
4760
		status = write16(state, SCU_RAM_QAM_LC_CF1_COARSE__A, 10);
4761 4762
		if (status < 0)
			break;
4763 4764 4765 4766


		/* QAM State Machine (FSM) Thresholds */

4767
		status = write16(state, SCU_RAM_QAM_FSM_RTH__A, 100);
4768 4769
		if (status < 0)
			break;
4770
		status = write16(state, SCU_RAM_QAM_FSM_FTH__A, 60);
4771 4772
		if (status < 0)
			break;
4773
		status = write16(state, SCU_RAM_QAM_FSM_CTH__A, 80);
4774 4775
		if (status < 0)
			break;
4776
		status = write16(state, SCU_RAM_QAM_FSM_PTH__A, 110);
4777 4778
		if (status < 0)
			break;
4779
		status = write16(state, SCU_RAM_QAM_FSM_QTH__A, 200);
4780 4781
		if (status < 0)
			break;
4782
		status = write16(state, SCU_RAM_QAM_FSM_MTH__A, 95);
4783 4784
		if (status < 0)
			break;
4785

4786
		status = write16(state, SCU_RAM_QAM_FSM_RATE_LIM__A, 40);
4787 4788
		if (status < 0)
			break;
4789
		status = write16(state, SCU_RAM_QAM_FSM_COUNT_LIM__A, 4);
4790 4791
		if (status < 0)
			break;
4792
		status = write16(state, SCU_RAM_QAM_FSM_FREQ_LIM__A, 15);
4793 4794
		if (status < 0)
			break;
4795 4796 4797 4798


		/* QAM FSM Tracking Parameters */

4799
		status = write16(state, SCU_RAM_QAM_FSM_MEDIAN_AV_MULT__A, (u16) 12);
4800 4801
		if (status < 0)
			break;
4802
		status = write16(state, SCU_RAM_QAM_FSM_RADIUS_AV_LIMIT__A, (u16) 141);
4803 4804
		if (status < 0)
			break;
4805
		status = write16(state, SCU_RAM_QAM_FSM_LCAVG_OFFSET1__A, (u16) 7);
4806 4807
		if (status < 0)
			break;
4808
		status = write16(state, SCU_RAM_QAM_FSM_LCAVG_OFFSET2__A, (u16) 0);
4809 4810
		if (status < 0)
			break;
4811
		status = write16(state, SCU_RAM_QAM_FSM_LCAVG_OFFSET3__A, (u16) -15);
4812 4813
		if (status < 0)
			break;
4814
		status = write16(state, SCU_RAM_QAM_FSM_LCAVG_OFFSET4__A, (u16) -45);
4815 4816
		if (status < 0)
			break;
4817
		status = write16(state, SCU_RAM_QAM_FSM_LCAVG_OFFSET5__A, (u16) -80);
4818 4819
		if (status < 0)
			break;
4820 4821 4822
	} while (0);

	return status;
R
Ralph Metzler 已提交
4823 4824 4825 4826 4827 4828 4829 4830 4831 4832 4833
}

/*============================================================================*/

/**
* \brief QAM128 specific setup
* \param demod: instance of demod.
* \return DRXStatus_t.
*/
static int SetQAM128(struct drxk_state *state)
{
4834 4835
	int status = 0;

4836
	dprintk(1, "\n");
4837 4838 4839
	do {
		/* QAM Equalizer Setup */
		/* Equalizer */
4840
		status = write16(state, SCU_RAM_QAM_EQ_CMA_RAD0__A, 6564);
4841 4842
		if (status < 0)
			break;
4843
		status = write16(state, SCU_RAM_QAM_EQ_CMA_RAD1__A, 6598);
4844 4845
		if (status < 0)
			break;
4846
		status = write16(state, SCU_RAM_QAM_EQ_CMA_RAD2__A, 6394);
4847 4848
		if (status < 0)
			break;
4849
		status = write16(state, SCU_RAM_QAM_EQ_CMA_RAD3__A, 6409);
4850 4851
		if (status < 0)
			break;
4852
		status = write16(state, SCU_RAM_QAM_EQ_CMA_RAD4__A, 6656);
4853 4854
		if (status < 0)
			break;
4855
		status = write16(state, SCU_RAM_QAM_EQ_CMA_RAD5__A, 7238);
4856 4857
		if (status < 0)
			break;
4858 4859

		/* Decision Feedback Equalizer */
4860
		status = write16(state, QAM_DQ_QUAL_FUN0__A, 6);
4861 4862
		if (status < 0)
			break;
4863
		status = write16(state, QAM_DQ_QUAL_FUN1__A, 6);
4864 4865
		if (status < 0)
			break;
4866
		status = write16(state, QAM_DQ_QUAL_FUN2__A, 6);
4867 4868
		if (status < 0)
			break;
4869
		status = write16(state, QAM_DQ_QUAL_FUN3__A, 6);
4870 4871
		if (status < 0)
			break;
4872
		status = write16(state, QAM_DQ_QUAL_FUN4__A, 5);
4873 4874
		if (status < 0)
			break;
4875
		status = write16(state, QAM_DQ_QUAL_FUN5__A, 0);
4876 4877
		if (status < 0)
			break;
4878

4879
		status = write16(state, QAM_SY_SYNC_HWM__A, 6);
4880 4881
		if (status < 0)
			break;
4882
		status = write16(state, QAM_SY_SYNC_AWM__A, 5);
4883 4884
		if (status < 0)
			break;
4885
		status = write16(state, QAM_SY_SYNC_LWM__A, 3);
4886 4887
		if (status < 0)
			break;
4888 4889 4890 4891


		/* QAM Slicer Settings */

4892
		status = write16(state, SCU_RAM_QAM_SL_SIG_POWER__A, DRXK_QAM_SL_SIG_POWER_QAM128);
4893 4894
		if (status < 0)
			break;
4895 4896 4897 4898


		/* QAM Loop Controller Coeficients */

4899
		status = write16(state, SCU_RAM_QAM_LC_CA_FINE__A, 15);
4900 4901
		if (status < 0)
			break;
4902
		status = write16(state, SCU_RAM_QAM_LC_CA_COARSE__A, 40);
4903 4904
		if (status < 0)
			break;
4905
		status = write16(state, SCU_RAM_QAM_LC_EP_FINE__A, 12);
4906 4907
		if (status < 0)
			break;
4908
		status = write16(state, SCU_RAM_QAM_LC_EP_MEDIUM__A, 24);
4909 4910
		if (status < 0)
			break;
4911
		status = write16(state, SCU_RAM_QAM_LC_EP_COARSE__A, 24);
4912 4913
		if (status < 0)
			break;
4914
		status = write16(state, SCU_RAM_QAM_LC_EI_FINE__A, 12);
4915 4916
		if (status < 0)
			break;
4917
		status = write16(state, SCU_RAM_QAM_LC_EI_MEDIUM__A, 16);
4918 4919
		if (status < 0)
			break;
4920
		status = write16(state, SCU_RAM_QAM_LC_EI_COARSE__A, 16);
4921 4922 4923
		if (status < 0)
			break;

4924
		status = write16(state, SCU_RAM_QAM_LC_CP_FINE__A, 5);
4925 4926
		if (status < 0)
			break;
4927
		status = write16(state, SCU_RAM_QAM_LC_CP_MEDIUM__A, 40);
4928 4929
		if (status < 0)
			break;
4930
		status = write16(state, SCU_RAM_QAM_LC_CP_COARSE__A, 120);
4931 4932
		if (status < 0)
			break;
4933
		status = write16(state, SCU_RAM_QAM_LC_CI_FINE__A, 5);
4934 4935
		if (status < 0)
			break;
4936
		status = write16(state, SCU_RAM_QAM_LC_CI_MEDIUM__A, 40);
4937 4938
		if (status < 0)
			break;
4939
		status = write16(state, SCU_RAM_QAM_LC_CI_COARSE__A, 60);
4940 4941
		if (status < 0)
			break;
4942
		status = write16(state, SCU_RAM_QAM_LC_CF_FINE__A, 16);
4943 4944
		if (status < 0)
			break;
4945
		status = write16(state, SCU_RAM_QAM_LC_CF_MEDIUM__A, 25);
4946 4947
		if (status < 0)
			break;
4948
		status = write16(state, SCU_RAM_QAM_LC_CF_COARSE__A, 64);
4949 4950
		if (status < 0)
			break;
4951
		status = write16(state, SCU_RAM_QAM_LC_CF1_FINE__A, 5);
4952 4953
		if (status < 0)
			break;
4954
		status = write16(state, SCU_RAM_QAM_LC_CF1_MEDIUM__A, 10);
4955 4956
		if (status < 0)
			break;
4957
		status = write16(state, SCU_RAM_QAM_LC_CF1_COARSE__A, 0);
4958 4959
		if (status < 0)
			break;
4960 4961 4962 4963


		/* QAM State Machine (FSM) Thresholds */

4964
		status = write16(state, SCU_RAM_QAM_FSM_RTH__A, 50);
4965 4966
		if (status < 0)
			break;
4967
		status = write16(state, SCU_RAM_QAM_FSM_FTH__A, 60);
4968 4969
		if (status < 0)
			break;
4970
		status = write16(state, SCU_RAM_QAM_FSM_CTH__A, 80);
4971 4972
		if (status < 0)
			break;
4973
		status = write16(state, SCU_RAM_QAM_FSM_PTH__A, 100);
4974 4975
		if (status < 0)
			break;
4976
		status = write16(state, SCU_RAM_QAM_FSM_QTH__A, 140);
4977 4978
		if (status < 0)
			break;
4979
		status = write16(state, SCU_RAM_QAM_FSM_MTH__A, 100);
4980 4981
		if (status < 0)
			break;
4982

4983
		status = write16(state, SCU_RAM_QAM_FSM_RATE_LIM__A, 40);
4984 4985
		if (status < 0)
			break;
4986
		status = write16(state, SCU_RAM_QAM_FSM_COUNT_LIM__A, 5);
4987 4988
		if (status < 0)
			break;
4989

4990
		status = write16(state, SCU_RAM_QAM_FSM_FREQ_LIM__A, 12);
4991 4992
		if (status < 0)
			break;
4993 4994 4995

		/* QAM FSM Tracking Parameters */

4996
		status = write16(state, SCU_RAM_QAM_FSM_MEDIAN_AV_MULT__A, (u16) 8);
4997 4998
		if (status < 0)
			break;
4999
		status = write16(state, SCU_RAM_QAM_FSM_RADIUS_AV_LIMIT__A, (u16) 65);
5000 5001
		if (status < 0)
			break;
5002
		status = write16(state, SCU_RAM_QAM_FSM_LCAVG_OFFSET1__A, (u16) 5);
5003 5004
		if (status < 0)
			break;
5005
		status = write16(state, SCU_RAM_QAM_FSM_LCAVG_OFFSET2__A, (u16) 3);
5006 5007
		if (status < 0)
			break;
5008
		status = write16(state, SCU_RAM_QAM_FSM_LCAVG_OFFSET3__A, (u16) -1);
5009 5010
		if (status < 0)
			break;
5011
		status = write16(state, SCU_RAM_QAM_FSM_LCAVG_OFFSET4__A, (u16) -12);
5012 5013
		if (status < 0)
			break;
5014
		status = write16(state, SCU_RAM_QAM_FSM_LCAVG_OFFSET5__A, (u16) -23);
5015 5016
		if (status < 0)
			break;
5017 5018 5019
	} while (0);

	return status;
R
Ralph Metzler 已提交
5020 5021 5022 5023 5024 5025 5026 5027 5028 5029 5030
}

/*============================================================================*/

/**
* \brief QAM256 specific setup
* \param demod: instance of demod.
* \return DRXStatus_t.
*/
static int SetQAM256(struct drxk_state *state)
{
5031 5032
	int status = 0;

5033
	dprintk(1, "\n");
5034 5035 5036
	do {
		/* QAM Equalizer Setup */
		/* Equalizer */
5037
		status = write16(state, SCU_RAM_QAM_EQ_CMA_RAD0__A, 11502);
5038 5039
		if (status < 0)
			break;
5040
		status = write16(state, SCU_RAM_QAM_EQ_CMA_RAD1__A, 12084);
5041 5042
		if (status < 0)
			break;
5043
		status = write16(state, SCU_RAM_QAM_EQ_CMA_RAD2__A, 12543);
5044 5045
		if (status < 0)
			break;
5046
		status = write16(state, SCU_RAM_QAM_EQ_CMA_RAD3__A, 12931);
5047 5048
		if (status < 0)
			break;
5049
		status = write16(state, SCU_RAM_QAM_EQ_CMA_RAD4__A, 13629);
5050 5051
		if (status < 0)
			break;
5052
		status = write16(state, SCU_RAM_QAM_EQ_CMA_RAD5__A, 15385);
5053 5054
		if (status < 0)
			break;
5055 5056

		/* Decision Feedback Equalizer */
5057
		status = write16(state, QAM_DQ_QUAL_FUN0__A, 8);
5058 5059
		if (status < 0)
			break;
5060
		status = write16(state, QAM_DQ_QUAL_FUN1__A, 8);
5061 5062
		if (status < 0)
			break;
5063
		status = write16(state, QAM_DQ_QUAL_FUN2__A, 8);
5064 5065
		if (status < 0)
			break;
5066
		status = write16(state, QAM_DQ_QUAL_FUN3__A, 8);
5067 5068
		if (status < 0)
			break;
5069
		status = write16(state, QAM_DQ_QUAL_FUN4__A, 6);
5070 5071
		if (status < 0)
			break;
5072
		status = write16(state, QAM_DQ_QUAL_FUN5__A, 0);
5073 5074
		if (status < 0)
			break;
5075

5076
		status = write16(state, QAM_SY_SYNC_HWM__A, 5);
5077 5078
		if (status < 0)
			break;
5079
		status = write16(state, QAM_SY_SYNC_AWM__A, 4);
5080 5081
		if (status < 0)
			break;
5082
		status = write16(state, QAM_SY_SYNC_LWM__A, 3);
5083 5084
		if (status < 0)
			break;
5085 5086 5087

		/* QAM Slicer Settings */

5088
		status = write16(state, SCU_RAM_QAM_SL_SIG_POWER__A, DRXK_QAM_SL_SIG_POWER_QAM256);
5089 5090
		if (status < 0)
			break;
5091 5092 5093 5094


		/* QAM Loop Controller Coeficients */

5095
		status = write16(state, SCU_RAM_QAM_LC_CA_FINE__A, 15);
5096 5097
		if (status < 0)
			break;
5098
		status = write16(state, SCU_RAM_QAM_LC_CA_COARSE__A, 40);
5099 5100
		if (status < 0)
			break;
5101
		status = write16(state, SCU_RAM_QAM_LC_EP_FINE__A, 12);
5102 5103
		if (status < 0)
			break;
5104
		status = write16(state, SCU_RAM_QAM_LC_EP_MEDIUM__A, 24);
5105 5106
		if (status < 0)
			break;
5107
		status = write16(state, SCU_RAM_QAM_LC_EP_COARSE__A, 24);
5108 5109
		if (status < 0)
			break;
5110
		status = write16(state, SCU_RAM_QAM_LC_EI_FINE__A, 12);
5111 5112
		if (status < 0)
			break;
5113
		status = write16(state, SCU_RAM_QAM_LC_EI_MEDIUM__A, 16);
5114 5115
		if (status < 0)
			break;
5116
		status = write16(state, SCU_RAM_QAM_LC_EI_COARSE__A, 16);
5117 5118 5119
		if (status < 0)
			break;

5120
		status = write16(state, SCU_RAM_QAM_LC_CP_FINE__A, 5);
5121 5122
		if (status < 0)
			break;
5123
		status = write16(state, SCU_RAM_QAM_LC_CP_MEDIUM__A, 50);
5124 5125
		if (status < 0)
			break;
5126
		status = write16(state, SCU_RAM_QAM_LC_CP_COARSE__A, 250);
5127 5128
		if (status < 0)
			break;
5129
		status = write16(state, SCU_RAM_QAM_LC_CI_FINE__A, 5);
5130 5131
		if (status < 0)
			break;
5132
		status = write16(state, SCU_RAM_QAM_LC_CI_MEDIUM__A, 50);
5133 5134
		if (status < 0)
			break;
5135
		status = write16(state, SCU_RAM_QAM_LC_CI_COARSE__A, 125);
5136 5137
		if (status < 0)
			break;
5138
		status = write16(state, SCU_RAM_QAM_LC_CF_FINE__A, 16);
5139 5140
		if (status < 0)
			break;
5141
		status = write16(state, SCU_RAM_QAM_LC_CF_MEDIUM__A, 25);
5142 5143
		if (status < 0)
			break;
5144
		status = write16(state, SCU_RAM_QAM_LC_CF_COARSE__A, 48);
5145 5146
		if (status < 0)
			break;
5147
		status = write16(state, SCU_RAM_QAM_LC_CF1_FINE__A, 5);
5148 5149
		if (status < 0)
			break;
5150
		status = write16(state, SCU_RAM_QAM_LC_CF1_MEDIUM__A, 10);
5151 5152
		if (status < 0)
			break;
5153
		status = write16(state, SCU_RAM_QAM_LC_CF1_COARSE__A, 10);
5154 5155
		if (status < 0)
			break;
5156 5157 5158 5159


		/* QAM State Machine (FSM) Thresholds */

5160
		status = write16(state, SCU_RAM_QAM_FSM_RTH__A, 50);
5161 5162
		if (status < 0)
			break;
5163
		status = write16(state, SCU_RAM_QAM_FSM_FTH__A, 60);
5164 5165
		if (status < 0)
			break;
5166
		status = write16(state, SCU_RAM_QAM_FSM_CTH__A, 80);
5167 5168
		if (status < 0)
			break;
5169
		status = write16(state, SCU_RAM_QAM_FSM_PTH__A, 100);
5170 5171
		if (status < 0)
			break;
5172
		status = write16(state, SCU_RAM_QAM_FSM_QTH__A, 150);
5173 5174
		if (status < 0)
			break;
5175
		status = write16(state, SCU_RAM_QAM_FSM_MTH__A, 110);
5176 5177
		if (status < 0)
			break;
5178

5179
		status = write16(state, SCU_RAM_QAM_FSM_RATE_LIM__A, 40);
5180 5181
		if (status < 0)
			break;
5182
		status = write16(state, SCU_RAM_QAM_FSM_COUNT_LIM__A, 4);
5183 5184
		if (status < 0)
			break;
5185
		status = write16(state, SCU_RAM_QAM_FSM_FREQ_LIM__A, 12);
5186 5187
		if (status < 0)
			break;
5188 5189 5190 5191


		/* QAM FSM Tracking Parameters */

5192
		status = write16(state, SCU_RAM_QAM_FSM_MEDIAN_AV_MULT__A, (u16) 8);
5193 5194
		if (status < 0)
			break;
5195
		status = write16(state, SCU_RAM_QAM_FSM_RADIUS_AV_LIMIT__A, (u16) 74);
5196 5197
		if (status < 0)
			break;
5198
		status = write16(state, SCU_RAM_QAM_FSM_LCAVG_OFFSET1__A, (u16) 18);
5199 5200
		if (status < 0)
			break;
5201
		status = write16(state, SCU_RAM_QAM_FSM_LCAVG_OFFSET2__A, (u16) 13);
5202 5203
		if (status < 0)
			break;
5204
		status = write16(state, SCU_RAM_QAM_FSM_LCAVG_OFFSET3__A, (u16) 7);
5205 5206
		if (status < 0)
			break;
5207
		status = write16(state, SCU_RAM_QAM_FSM_LCAVG_OFFSET4__A, (u16) 0);
5208 5209
		if (status < 0)
			break;
5210
		status = write16(state, SCU_RAM_QAM_FSM_LCAVG_OFFSET5__A, (u16) -8);
5211 5212
		if (status < 0)
			break;
5213 5214 5215
	} while (0);

	return status;
R
Ralph Metzler 已提交
5216 5217 5218 5219 5220 5221 5222 5223 5224 5225 5226 5227
}


/*============================================================================*/
/**
* \brief Reset QAM block.
* \param demod:   instance of demod.
* \param channel: pointer to channel data.
* \return DRXStatus_t.
*/
static int QAMResetQAM(struct drxk_state *state)
{
5228 5229
	int status;
	u16 cmdResult;
R
Ralph Metzler 已提交
5230

5231
	dprintk(1, "\n");
5232 5233
	do {
		/* Stop QAM comstate->m_exec */
5234
		status = write16(state, QAM_COMM_EXEC__A, QAM_COMM_EXEC_STOP);
5235 5236 5237 5238 5239 5240
		if (status < 0)
			break;

		status = scu_command(state, SCU_RAM_COMMAND_STANDARD_QAM | SCU_RAM_COMMAND_CMD_DEMOD_RESET, 0, NULL, 1, &cmdResult);
		if (status < 0)
			break;
5241
	} while (0);
R
Ralph Metzler 已提交
5242

5243 5244
	/* All done, all OK */
	return status;
R
Ralph Metzler 已提交
5245 5246 5247 5248 5249 5250 5251 5252 5253 5254 5255 5256
}

/*============================================================================*/

/**
* \brief Set QAM symbolrate.
* \param demod:   instance of demod.
* \param channel: pointer to channel data.
* \return DRXStatus_t.
*/
static int QAMSetSymbolrate(struct drxk_state *state)
{
5257 5258 5259 5260 5261 5262 5263
	u32 adcFrequency = 0;
	u32 symbFreq = 0;
	u32 iqmRcRate = 0;
	u16 ratesel = 0;
	u32 lcSymbRate = 0;
	int status;

5264
	dprintk(1, "\n");
5265 5266 5267 5268
	do {
		/* Select & calculate correct IQM rate */
		adcFrequency = (state->m_sysClockFreq * 1000) / 3;
		ratesel = 0;
5269
		/* printk(KERN_DEBUG "drxk: SR %d\n", state->param.u.qam.symbol_rate); */
5270 5271 5272 5273 5274 5275
		if (state->param.u.qam.symbol_rate <= 1188750)
			ratesel = 3;
		else if (state->param.u.qam.symbol_rate <= 2377500)
			ratesel = 2;
		else if (state->param.u.qam.symbol_rate <= 4755000)
			ratesel = 1;
5276
		status = write16(state, IQM_FD_RATESEL__A, ratesel);
5277 5278
		if (status < 0)
			break;
5279 5280 5281 5282 5283 5284 5285 5286 5287 5288 5289 5290

		/*
		   IqmRcRate = ((Fadc / (symbolrate * (4<<ratesel))) - 1) * (1<<23)
		 */
		symbFreq = state->param.u.qam.symbol_rate * (1 << ratesel);
		if (symbFreq == 0) {
			/* Divide by zero */
			return -1;
		}
		iqmRcRate = (adcFrequency / symbFreq) * (1 << 21) +
		    (Frac28a((adcFrequency % symbFreq), symbFreq) >> 7) -
		    (1 << 23);
5291
		status = write32(state, IQM_RC_RATE_OFS_LO__A, iqmRcRate);
5292 5293
		if (status < 0)
			break;
5294 5295 5296 5297 5298 5299 5300 5301 5302 5303 5304 5305 5306 5307
		state->m_iqmRcRate = iqmRcRate;
		/*
		   LcSymbFreq = round (.125 *  symbolrate / adcFreq * (1<<15))
		 */
		symbFreq = state->param.u.qam.symbol_rate;
		if (adcFrequency == 0) {
			/* Divide by zero */
			return -1;
		}
		lcSymbRate = (symbFreq / adcFrequency) * (1 << 12) +
		    (Frac28a((symbFreq % adcFrequency), adcFrequency) >>
		     16);
		if (lcSymbRate > 511)
			lcSymbRate = 511;
5308
		status = write16(state, QAM_LC_SYMBOL_FREQ__A, (u16) lcSymbRate);
5309 5310
		if (status < 0)
			break;
5311 5312 5313
	} while (0);

	return status;
R
Ralph Metzler 已提交
5314 5315 5316 5317 5318 5319 5320 5321 5322 5323 5324 5325 5326 5327
}

/*============================================================================*/

/**
* \brief Get QAM lock status.
* \param demod:   instance of demod.
* \param channel: pointer to channel data.
* \return DRXStatus_t.
*/

static int GetQAMLockStatus(struct drxk_state *state, u32 *pLockStatus)
{
	int status;
5328
	u16 Result[2] = { 0, 0 };
R
Ralph Metzler 已提交
5329

5330
	dprintk(1, "\n");
5331 5332 5333 5334 5335 5336
	status =
	    scu_command(state,
			SCU_RAM_COMMAND_STANDARD_QAM |
			SCU_RAM_COMMAND_CMD_DEMOD_GET_LOCK, 0, NULL, 2,
			Result);
	if (status < 0)
5337
		printk(KERN_ERR "drxk: %s status = %08x\n", __func__, status);
5338 5339

	if (Result[1] < SCU_RAM_QAM_LOCKED_LOCKED_DEMOD_LOCKED) {
R
Ralph Metzler 已提交
5340 5341
		/* 0x0000 NOT LOCKED */
		*pLockStatus = NOT_LOCKED;
5342
	} else if (Result[1] < SCU_RAM_QAM_LOCKED_LOCKED_LOCKED) {
R
Ralph Metzler 已提交
5343 5344
		/* 0x4000 DEMOD LOCKED */
		*pLockStatus = DEMOD_LOCK;
5345
	} else if (Result[1] < SCU_RAM_QAM_LOCKED_LOCKED_NEVER_LOCK) {
R
Ralph Metzler 已提交
5346 5347
		/* 0x8000 DEMOD + FEC LOCKED (system lock) */
		*pLockStatus = MPEG_LOCK;
5348
	} else {
R
Ralph Metzler 已提交
5349 5350 5351 5352 5353 5354 5355 5356 5357 5358 5359 5360 5361 5362 5363 5364
		/* 0xC000 NEVER LOCKED */
		/* (system will never be able to lock to the signal) */
		/* TODO: check this, intermediate & standard specific lock states are not
		   taken into account here */
		*pLockStatus = NEVER_LOCK;
	}
	return status;
}

#define QAM_MIRROR__M         0x03
#define QAM_MIRROR_NORMAL     0x00
#define QAM_MIRRORED          0x01
#define QAM_MIRROR_AUTO_ON    0x02
#define QAM_LOCKRANGE__M      0x10
#define QAM_LOCKRANGE_NORMAL  0x10

5365 5366
static int SetQAM(struct drxk_state *state, u16 IntermediateFreqkHz,
		  s32 tunerFreqOffset)
R
Ralph Metzler 已提交
5367 5368 5369
{
	int status = 0;
	u8 parameterLen;
5370 5371 5372
	u16 setEnvParameters[5];
	u16 setParamParameters[4] = { 0, 0, 0, 0 };
	u16 cmdResult;
R
Ralph Metzler 已提交
5373

5374
	dprintk(1, "\n");
R
Ralph Metzler 已提交
5375 5376
	do {
		/*
5377 5378 5379 5380 5381
		   STEP 1: reset demodulator
		   resets FEC DI and FEC RS
		   resets QAM block
		   resets SCU variables
		 */
5382
		status = write16(state, FEC_DI_COMM_EXEC__A, FEC_DI_COMM_EXEC_STOP);
5383 5384
		if (status < 0)
			break;
5385
		status = write16(state, FEC_RS_COMM_EXEC__A, FEC_RS_COMM_EXEC_STOP);
5386 5387 5388 5389 5390
		if (status < 0)
			break;
		status = QAMResetQAM(state);
		if (status < 0)
			break;
R
Ralph Metzler 已提交
5391 5392

		/*
5393 5394 5395 5396
		   STEP 2: configure demodulator
		   -set env
		   -set params; resets IQM,QAM,FEC HW; initializes some SCU variables
		 */
5397 5398 5399
		status = QAMSetSymbolrate(state);
		if (status < 0)
			break;
R
Ralph Metzler 已提交
5400 5401

		/* Env parameters */
5402
		setEnvParameters[2] = QAM_TOP_ANNEX_A;	/* Annex */
R
Ralph Metzler 已提交
5403
		if (state->m_OperationMode == OM_QAM_ITU_C)
5404
			setEnvParameters[2] = QAM_TOP_ANNEX_C;	/* Annex */
R
Ralph Metzler 已提交
5405
		setParamParameters[3] |= (QAM_MIRROR_AUTO_ON);
5406 5407
		/* check for LOCKRANGE Extented */
		/* setParamParameters[3] |= QAM_LOCKRANGE_NORMAL; */
R
Ralph Metzler 已提交
5408 5409 5410
		parameterLen = 4;

		/* Set params */
5411
		switch (state->param.u.qam.modulation) {
R
Ralph Metzler 已提交
5412 5413 5414 5415 5416 5417 5418 5419 5420 5421 5422 5423 5424 5425 5426 5427 5428 5429 5430 5431
		case QAM_256:
			state->m_Constellation = DRX_CONSTELLATION_QAM256;
			break;
		case QAM_AUTO:
		case QAM_64:
			state->m_Constellation = DRX_CONSTELLATION_QAM64;
			break;
		case QAM_16:
			state->m_Constellation = DRX_CONSTELLATION_QAM16;
			break;
		case QAM_32:
			state->m_Constellation = DRX_CONSTELLATION_QAM32;
			break;
		case QAM_128:
			state->m_Constellation = DRX_CONSTELLATION_QAM128;
			break;
		default:
			status = -EINVAL;
			break;
		}
5432 5433 5434
		status = status;
		if (status < 0)
			break;
5435 5436
		setParamParameters[0] = state->m_Constellation;	/* constellation     */
		setParamParameters[1] = DRXK_QAM_I12_J17;	/* interleave mode   */
R
Ralph Metzler 已提交
5437

5438 5439 5440
		status = scu_command(state, SCU_RAM_COMMAND_STANDARD_QAM | SCU_RAM_COMMAND_CMD_DEMOD_SET_PARAM, 4, setParamParameters, 1, &cmdResult);
		if (status < 0)
			break;
R
Ralph Metzler 已提交
5441 5442 5443 5444


		/* STEP 3: enable the system in a mode where the ADC provides valid signal
		   setup constellation independent registers */
5445 5446 5447 5448 5449 5450 5451 5452
#if 0
		status = SetFrequency (channel, tunerFreqOffset));
		if (status < 0)
			break;
#endif
		status = SetFrequencyShifter(state, IntermediateFreqkHz, tunerFreqOffset, true);
		if (status < 0)
			break;
R
Ralph Metzler 已提交
5453 5454

		/* Setup BER measurement */
5455 5456 5457
		status = SetQAMMeasurement(state, state->m_Constellation, state->param.u. qam.symbol_rate);
		if (status < 0)
			break;
R
Ralph Metzler 已提交
5458 5459

		/* Reset default values */
5460
		status = write16(state, IQM_CF_SCALE_SH__A, IQM_CF_SCALE_SH__PRE);
5461 5462
		if (status < 0)
			break;
5463
		status = write16(state, QAM_SY_TIMEOUT__A, QAM_SY_TIMEOUT__PRE);
5464 5465
		if (status < 0)
			break;
5466 5467

		/* Reset default LC values */
5468
		status = write16(state, QAM_LC_RATE_LIMIT__A, 3);
5469 5470
		if (status < 0)
			break;
5471
		status = write16(state, QAM_LC_LPF_FACTORP__A, 4);
5472 5473
		if (status < 0)
			break;
5474
		status = write16(state, QAM_LC_LPF_FACTORI__A, 4);
5475 5476
		if (status < 0)
			break;
5477
		status = write16(state, QAM_LC_MODE__A, 7);
5478 5479 5480
		if (status < 0)
			break;

5481
		status = write16(state, QAM_LC_QUAL_TAB0__A, 1);
5482 5483
		if (status < 0)
			break;
5484
		status = write16(state, QAM_LC_QUAL_TAB1__A, 1);
5485 5486
		if (status < 0)
			break;
5487
		status = write16(state, QAM_LC_QUAL_TAB2__A, 1);
5488 5489
		if (status < 0)
			break;
5490
		status = write16(state, QAM_LC_QUAL_TAB3__A, 1);
5491 5492
		if (status < 0)
			break;
5493
		status = write16(state, QAM_LC_QUAL_TAB4__A, 2);
5494 5495
		if (status < 0)
			break;
5496
		status = write16(state, QAM_LC_QUAL_TAB5__A, 2);
5497 5498
		if (status < 0)
			break;
5499
		status = write16(state, QAM_LC_QUAL_TAB6__A, 2);
5500 5501
		if (status < 0)
			break;
5502
		status = write16(state, QAM_LC_QUAL_TAB8__A, 2);
5503 5504
		if (status < 0)
			break;
5505
		status = write16(state, QAM_LC_QUAL_TAB9__A, 2);
5506 5507
		if (status < 0)
			break;
5508
		status = write16(state, QAM_LC_QUAL_TAB10__A, 2);
5509 5510
		if (status < 0)
			break;
5511
		status = write16(state, QAM_LC_QUAL_TAB12__A, 2);
5512 5513
		if (status < 0)
			break;
5514
		status = write16(state, QAM_LC_QUAL_TAB15__A, 3);
5515 5516
		if (status < 0)
			break;
5517
		status = write16(state, QAM_LC_QUAL_TAB16__A, 3);
5518 5519
		if (status < 0)
			break;
5520
		status = write16(state, QAM_LC_QUAL_TAB20__A, 4);
5521 5522
		if (status < 0)
			break;
5523
		status = write16(state, QAM_LC_QUAL_TAB25__A, 4);
5524 5525
		if (status < 0)
			break;
5526 5527

		/* Mirroring, QAM-block starting point not inverted */
5528
		status = write16(state, QAM_SY_SP_INV__A, QAM_SY_SP_INV_SPECTRUM_INV_DIS);
5529 5530
		if (status < 0)
			break;
5531 5532

		/* Halt SCU to enable safe non-atomic accesses */
5533
		status = write16(state, SCU_COMM_EXEC__A, SCU_COMM_EXEC_HOLD);
5534 5535
		if (status < 0)
			break;
5536 5537 5538 5539

		/* STEP 4: constellation specific setup */
		switch (state->param.u.qam.modulation) {
		case QAM_16:
5540 5541 5542
			status = SetQAM16(state);
			if (status < 0)
				break;
5543 5544
			break;
		case QAM_32:
5545 5546 5547
			status = SetQAM32(state);
			if (status < 0)
				break;
5548 5549 5550
			break;
		case QAM_AUTO:
		case QAM_64:
5551 5552 5553
			status = SetQAM64(state);
			if (status < 0)
				break;
5554 5555
			break;
		case QAM_128:
5556 5557 5558
			status = SetQAM128(state);
			if (status < 0)
				break;
5559 5560
			break;
		case QAM_256:
5561 5562 5563
			status = SetQAM256(state);
			if (status < 0)
				break;
5564 5565 5566 5567 5568 5569
			break;
		default:
			return -1;
			break;
		}		/* switch */
		/* Activate SCU to enable SCU commands */
5570
		status = write16(state, SCU_COMM_EXEC__A, SCU_COMM_EXEC_ACTIVE);
5571 5572
		if (status < 0)
			break;
5573 5574 5575 5576 5577


		/* Re-configure MPEG output, requires knowledge of channel bitrate */
		/* extAttr->currentChannel.constellation = channel->constellation; */
		/* extAttr->currentChannel.symbolrate    = channel->symbolrate; */
5578 5579 5580
		status = MPEGTSDtoSetup(state, state->m_OperationMode);
		if (status < 0)
			break;
5581 5582

		/* Start processes */
5583 5584 5585
		status = MPEGTSStart(state);
		if (status < 0)
			break;
5586
		status = write16(state, FEC_COMM_EXEC__A, FEC_COMM_EXEC_ACTIVE);
5587 5588
		if (status < 0)
			break;
5589
		status = write16(state, QAM_COMM_EXEC__A, QAM_COMM_EXEC_ACTIVE);
5590 5591
		if (status < 0)
			break;
5592
		status = write16(state, IQM_COMM_EXEC__A, IQM_COMM_EXEC_B_ACTIVE);
5593 5594
		if (status < 0)
			break;
5595 5596

		/* STEP 5: start QAM demodulator (starts FEC, QAM and IQM HW) */
5597 5598 5599
		status = scu_command(state, SCU_RAM_COMMAND_STANDARD_QAM | SCU_RAM_COMMAND_CMD_DEMOD_START, 0, NULL, 1, &cmdResult);
		if (status < 0)
			break;
5600 5601 5602 5603 5604 5605 5606 5607

		/* update global DRXK data container */
	/*?     extAttr->qamInterleaveMode = DRXK_QAM_I12_J17; */

		/* All done, all OK */
	} while (0);

	if (status < 0)
5608
		printk(KERN_ERR "drxk: %s %d\n", __func__, status);
5609 5610

	return status;
R
Ralph Metzler 已提交
5611 5612
}

5613 5614
static int SetQAMStandard(struct drxk_state *state,
			  enum OperationMode oMode)
R
Ralph Metzler 已提交
5615 5616 5617 5618 5619 5620
{
#ifdef DRXK_QAM_TAPS
#define DRXK_QAMA_TAPS_SELECT
#include "drxk_filters.h"
#undef DRXK_QAMA_TAPS_SELECT
#else
5621
	int status;
R
Ralph Metzler 已提交
5622 5623
#endif

5624
	dprintk(1, "\n");
5625 5626 5627 5628 5629
	do {
		/* added antenna switch */
		SwitchAntennaToQAM(state);

		/* Ensure correct power-up mode */
5630 5631 5632
		status = PowerUpQAM(state);
		if (status < 0)
			break;
5633
		/* Reset QAM block */
5634 5635 5636
		status = QAMResetQAM(state);
		if (status < 0)
			break;
5637 5638 5639

		/* Setup IQM */

5640
		status = write16(state, IQM_COMM_EXEC__A, IQM_COMM_EXEC_B_STOP);
5641 5642
		if (status < 0)
			break;
5643
		status = write16(state, IQM_AF_AMUX__A, IQM_AF_AMUX_SIGNAL2ADC);
5644 5645
		if (status < 0)
			break;
5646 5647 5648 5649 5650

		/* Upload IQM Channel Filter settings by
		   boot loader from ROM table */
		switch (oMode) {
		case OM_QAM_ITU_A:
5651 5652 5653
			status = BLChainCmd(state, DRXK_BL_ROM_OFFSET_TAPS_ITU_A, DRXK_BLCC_NR_ELEMENTS_TAPS, DRXK_BLC_TIMEOUT);
			if (status < 0)
				break;
5654 5655
			break;
		case OM_QAM_ITU_C:
5656 5657 5658 5659 5660 5661
			status = BLDirectCmd(state, IQM_CF_TAP_RE0__A, DRXK_BL_ROM_OFFSET_TAPS_ITU_C, DRXK_BLDC_NR_ELEMENTS_TAPS, DRXK_BLC_TIMEOUT);
			if (status < 0)
				break;
			status = BLDirectCmd(state, IQM_CF_TAP_IM0__A, DRXK_BL_ROM_OFFSET_TAPS_ITU_C, DRXK_BLDC_NR_ELEMENTS_TAPS, DRXK_BLC_TIMEOUT);
			if (status < 0)
				break;
5662 5663 5664 5665
			break;
		default:
			status = -EINVAL;
		}
5666 5667 5668 5669
		status = status;
		if (status < 0)
			break;

5670
		status = write16(state, IQM_CF_OUT_ENA__A, (1 << IQM_CF_OUT_ENA_QAM__B));
5671 5672
		if (status < 0)
			break;
5673
		status = write16(state, IQM_CF_SYMMETRIC__A, 0);
5674 5675
		if (status < 0)
			break;
5676
		status = write16(state, IQM_CF_MIDTAP__A, ((1 << IQM_CF_MIDTAP_RE__B) | (1 << IQM_CF_MIDTAP_IM__B)));
5677 5678 5679
		if (status < 0)
			break;

5680
		status = write16(state, IQM_RC_STRETCH__A, 21);
5681 5682
		if (status < 0)
			break;
5683
		status = write16(state, IQM_AF_CLP_LEN__A, 0);
5684 5685
		if (status < 0)
			break;
5686
		status = write16(state, IQM_AF_CLP_TH__A, 448);
5687 5688
		if (status < 0)
			break;
5689
		status = write16(state, IQM_AF_SNS_LEN__A, 0);
5690 5691
		if (status < 0)
			break;
5692
		status = write16(state, IQM_CF_POW_MEAS_LEN__A, 0);
5693 5694 5695
		if (status < 0)
			break;

5696
		status = write16(state, IQM_FS_ADJ_SEL__A, 1);
5697 5698
		if (status < 0)
			break;
5699
		status = write16(state, IQM_RC_ADJ_SEL__A, 1);
5700 5701
		if (status < 0)
			break;
5702
		status = write16(state, IQM_CF_ADJ_SEL__A, 1);
5703 5704
		if (status < 0)
			break;
5705
		status = write16(state, IQM_AF_UPD_SEL__A, 0);
5706 5707
		if (status < 0)
			break;
5708 5709

		/* IQM Impulse Noise Processing Unit */
5710
		status = write16(state, IQM_CF_CLP_VAL__A, 500);
5711 5712
		if (status < 0)
			break;
5713
		status = write16(state, IQM_CF_DATATH__A, 1000);
5714 5715
		if (status < 0)
			break;
5716
		status = write16(state, IQM_CF_BYPASSDET__A, 1);
5717 5718
		if (status < 0)
			break;
5719
		status = write16(state, IQM_CF_DET_LCT__A, 0);
5720 5721
		if (status < 0)
			break;
5722
		status = write16(state, IQM_CF_WND_LEN__A, 1);
5723 5724
		if (status < 0)
			break;
5725
		status = write16(state, IQM_CF_PKDTH__A, 1);
5726 5727
		if (status < 0)
			break;
5728
		status = write16(state, IQM_AF_INC_BYPASS__A, 1);
5729 5730
		if (status < 0)
			break;
5731 5732

		/* turn on IQMAF. Must be done before setAgc**() */
5733 5734 5735
		status = SetIqmAf(state, true);
		if (status < 0)
			break;
5736
		status = write16(state, IQM_AF_START_LOCK__A, 0x01);
5737 5738
		if (status < 0)
			break;
5739 5740

		/* IQM will not be reset from here, sync ADC and update/init AGC */
5741 5742 5743
		status = ADCSynchronization(state);
		if (status < 0)
			break;
5744 5745

		/* Set the FSM step period */
5746
		status = write16(state, SCU_RAM_QAM_FSM_STEP_PERIOD__A, 2000);
5747 5748
		if (status < 0)
			break;
5749 5750

		/* Halt SCU to enable safe non-atomic accesses */
5751
		status = write16(state, SCU_COMM_EXEC__A, SCU_COMM_EXEC_HOLD);
5752 5753
		if (status < 0)
			break;
5754 5755 5756 5757

		/* No more resets of the IQM, current standard correctly set =>
		   now AGCs can be configured. */

5758 5759 5760 5761 5762 5763
		status = InitAGC(state, true);
		if (status < 0)
			break;
		status = SetPreSaw(state, &(state->m_qamPreSawCfg));
		if (status < 0)
			break;
5764 5765

		/* Configure AGC's */
5766 5767 5768 5769 5770 5771
		status = SetAgcRf(state, &(state->m_qamRfAgcCfg), true);
		if (status < 0)
			break;
		status = SetAgcIf(state, &(state->m_qamIfAgcCfg), true);
		if (status < 0)
			break;
5772 5773

		/* Activate SCU to enable SCU commands */
5774
		status = write16(state, SCU_COMM_EXEC__A, SCU_COMM_EXEC_ACTIVE);
5775 5776
		if (status < 0)
			break;
5777 5778
	} while (0);
	return status;
R
Ralph Metzler 已提交
5779 5780 5781 5782
}

static int WriteGPIO(struct drxk_state *state)
{
5783 5784 5785
	int status;
	u16 value = 0;

5786
	dprintk(1, "\n");
5787 5788
	do {
		/* stop lock indicator process */
5789
		status = write16(state, SCU_RAM_GPIO__A, SCU_RAM_GPIO_HW_LOCK_IND_DISABLE);
5790 5791
		if (status < 0)
			break;
5792 5793

		/*  Write magic word to enable pdr reg write               */
5794
		status = write16(state, SIO_TOP_COMM_KEY__A, SIO_TOP_COMM_KEY_KEY);
5795 5796
		if (status < 0)
			break;
5797 5798 5799

		if (state->m_hasSAWSW) {
			/* write to io pad configuration register - output mode */
5800
			status = write16(state, SIO_PDR_SMA_TX_CFG__A, state->m_GPIOCfg);
5801 5802
			if (status < 0)
				break;
5803 5804

			/* use corresponding bit in io data output registar */
5805
			status = read16(state, SIO_PDR_UIO_OUT_LO__A, &value);
5806 5807
			if (status < 0)
				break;
5808 5809 5810 5811 5812
			if (state->m_GPIO == 0)
				value &= 0x7FFF;	/* write zero to 15th bit - 1st UIO */
			else
				value |= 0x8000;	/* write one to 15th bit - 1st UIO */
			/* write back to io data output register */
5813
			status = write16(state, SIO_PDR_UIO_OUT_LO__A, value);
5814 5815
			if (status < 0)
				break;
5816 5817 5818

		}
		/*  Write magic word to disable pdr reg write               */
5819
		status = write16(state, SIO_TOP_COMM_KEY__A, 0x0000);
5820 5821
		if (status < 0)
			break;
5822 5823
	} while (0);
	return status;
R
Ralph Metzler 已提交
5824 5825 5826 5827
}

static int SwitchAntennaToQAM(struct drxk_state *state)
{
5828 5829
	int status = -1;

5830
	dprintk(1, "\n");
5831 5832 5833 5834 5835 5836 5837
	if (state->m_AntennaSwitchDVBTDVBC != 0) {
		if (state->m_GPIO != state->m_AntennaDVBC) {
			state->m_GPIO = state->m_AntennaDVBC;
			status = WriteGPIO(state);
		}
	}
	return status;
R
Ralph Metzler 已提交
5838 5839 5840 5841 5842
}

static int SwitchAntennaToDVBT(struct drxk_state *state)
{
	int status = -1;
5843

5844
	dprintk(1, "\n");
R
Ralph Metzler 已提交
5845 5846 5847 5848 5849 5850 5851 5852 5853 5854 5855 5856 5857 5858 5859 5860 5861 5862 5863
	if (state->m_AntennaSwitchDVBTDVBC != 0) {
		if (state->m_GPIO != state->m_AntennaDVBT) {
			state->m_GPIO = state->m_AntennaDVBT;
			status = WriteGPIO(state);
		}
	}
	return status;
}


static int PowerDownDevice(struct drxk_state *state)
{
	/* Power down to requested mode */
	/* Backup some register settings */
	/* Set pins with possible pull-ups connected to them in input mode */
	/* Analog power down */
	/* ADC power down */
	/* Power down device */
	int status;
5864 5865

	dprintk(1, "\n");
R
Ralph Metzler 已提交
5866 5867
	do {
		if (state->m_bPDownOpenBridge) {
5868
			/* Open I2C bridge before power down of DRXK */
5869 5870 5871
			status = ConfigureI2CBridge(state, true);
			if (status < 0)
				break;
R
Ralph Metzler 已提交
5872
		}
5873
		/* driver 0.9.0 */
5874 5875 5876
		status = DVBTEnableOFDMTokenRing(state, false);
		if (status < 0)
			break;
R
Ralph Metzler 已提交
5877

5878
		status = write16(state, SIO_CC_PWD_MODE__A, SIO_CC_PWD_MODE_LEVEL_CLOCK);
5879 5880
		if (status < 0)
			break;
5881
		status = write16(state, SIO_CC_UPDATE__A, SIO_CC_UPDATE_KEY);
5882 5883
		if (status < 0)
			break;
R
Ralph Metzler 已提交
5884
		state->m_HICfgCtrl |= SIO_HI_RA_RAM_PAR_5_CFG_SLEEP_ZZZ;
5885 5886 5887
		status = HI_CfgCommand(state);
		if (status < 0)
			break;
5888
	} while (0);
R
Ralph Metzler 已提交
5889

5890
	if (status < 0)
R
Ralph Metzler 已提交
5891
		return -1;
5892

R
Ralph Metzler 已提交
5893 5894 5895 5896 5897 5898
	return 0;
}

static int load_microcode(struct drxk_state *state, char *mc_name)
{
	const struct firmware *fw = NULL;
5899
	int err = 0;
R
Ralph Metzler 已提交
5900

5901 5902
	dprintk(1, "\n");

R
Ralph Metzler 已提交
5903 5904 5905
	err = request_firmware(&fw, mc_name, state->i2c->dev.parent);
	if (err < 0) {
		printk(KERN_ERR
5906
		       "drxk: Could not load firmware file %s.\n", mc_name);
R
Ralph Metzler 已提交
5907
		printk(KERN_INFO
5908
		       "drxk: Copy %s to your hotplug directory!\n", mc_name);
R
Ralph Metzler 已提交
5909 5910
		return err;
	}
5911
	err = DownloadMicrocode(state, fw->data, fw->size);
R
Ralph Metzler 已提交
5912 5913 5914 5915 5916 5917 5918
	release_firmware(fw);
	return err;
}

static int init_drxk(struct drxk_state *state)
{
	int status;
5919
	enum DRXPowerMode powerMode = DRXK_POWER_DOWN_OFDM;
R
Ralph Metzler 已提交
5920 5921
	u16 driverVersion;

5922
	dprintk(1, "\n");
R
Ralph Metzler 已提交
5923 5924
	if ((state->m_DrxkState == DRXK_UNINITIALIZED)) {
		do {
5925 5926 5927 5928 5929 5930
			status = PowerUpDevice(state);
			if (status < 0)
				break;
			status = DRXX_Open(state);
			if (status < 0)
				break;
R
Ralph Metzler 已提交
5931
			/* Soft reset of OFDM-, sys- and osc-clockdomain */
5932
			status = write16(state, SIO_CC_SOFT_RST__A, SIO_CC_SOFT_RST_OFDM__M | SIO_CC_SOFT_RST_SYS__M | SIO_CC_SOFT_RST_OSC__M);
5933 5934
			if (status < 0)
				break;
5935
			status = write16(state, SIO_CC_UPDATE__A, SIO_CC_UPDATE_KEY);
5936 5937
			if (status < 0)
				break;
R
Ralph Metzler 已提交
5938 5939 5940
			/* TODO is this needed, if yes how much delay in worst case scenario */
			msleep(1);
			state->m_DRXK_A3_PATCH_CODE = true;
5941 5942 5943
			status = GetDeviceCapabilities(state);
			if (status < 0)
				break;
R
Ralph Metzler 已提交
5944 5945 5946 5947

			/* Bridge delay, uses oscilator clock */
			/* Delay = (delay (nano seconds) * oscclk (kHz))/ 1000 */
			/* SDA brdige delay */
5948 5949 5950
			state->m_HICfgBridgeDelay =
			    (u16) ((state->m_oscClockFreq / 1000) *
				   HI_I2C_BRIDGE_DELAY) / 1000;
R
Ralph Metzler 已提交
5951
			/* Clipping */
5952 5953 5954 5955
			if (state->m_HICfgBridgeDelay >
			    SIO_HI_RA_RAM_PAR_3_CFG_DBL_SDA__M) {
				state->m_HICfgBridgeDelay =
				    SIO_HI_RA_RAM_PAR_3_CFG_DBL_SDA__M;
R
Ralph Metzler 已提交
5956 5957
			}
			/* SCL bridge delay, same as SDA for now */
5958 5959 5960
			state->m_HICfgBridgeDelay +=
			    state->m_HICfgBridgeDelay <<
			    SIO_HI_RA_RAM_PAR_3_CFG_DBL_SCL__B;
R
Ralph Metzler 已提交
5961

5962 5963 5964
			status = InitHI(state);
			if (status < 0)
				break;
R
Ralph Metzler 已提交
5965 5966
			/* disable various processes */
#if NOA1ROM
5967 5968
			if (!(state->m_DRXK_A1_ROM_CODE)
			    && !(state->m_DRXK_A2_ROM_CODE))
R
Ralph Metzler 已提交
5969 5970
#endif
			{
5971
				status = write16(state, SCU_RAM_GPIO__A, SCU_RAM_GPIO_HW_LOCK_IND_DISABLE);
5972 5973
				if (status < 0)
					break;
R
Ralph Metzler 已提交
5974 5975 5976
			}

			/* disable MPEG port */
5977 5978 5979
			status = MPEGTSDisable(state);
			if (status < 0)
				break;
R
Ralph Metzler 已提交
5980 5981

			/* Stop AUD and SCU */
5982
			status = write16(state, AUD_COMM_EXEC__A, AUD_COMM_EXEC_STOP);
5983 5984
			if (status < 0)
				break;
5985
			status = write16(state, SCU_COMM_EXEC__A, SCU_COMM_EXEC_STOP);
5986 5987
			if (status < 0)
				break;
R
Ralph Metzler 已提交
5988 5989

			/* enable token-ring bus through OFDM block for possible ucode upload */
5990
			status = write16(state, SIO_OFDM_SH_OFDM_RING_ENABLE__A, SIO_OFDM_SH_OFDM_RING_ENABLE_ON);
5991 5992
			if (status < 0)
				break;
R
Ralph Metzler 已提交
5993 5994

			/* include boot loader section */
5995
			status = write16(state, SIO_BL_COMM_EXEC__A, SIO_BL_COMM_EXEC_ACTIVE);
5996 5997 5998 5999 6000
			if (status < 0)
				break;
			status = BLChainCmd(state, 0, 6, 100);
			if (status < 0)
				break;
R
Ralph Metzler 已提交
6001 6002 6003

#if 0
			if (state->m_DRXK_A3_PATCH_CODE)
6004 6005 6006
				status = DownloadMicrocode(state, DRXK_A3_microcode, DRXK_A3_microcode_length);
				if (status < 0)
					break;
R
Ralph Metzler 已提交
6007 6008 6009 6010 6011
#else
			load_microcode(state, "drxk_a3.mc");
#endif
#if NOA1ROM
			if (state->m_DRXK_A2_PATCH_CODE)
6012 6013 6014
				status = DownloadMicrocode(state, DRXK_A2_microcode, DRXK_A2_microcode_length);
				if (status < 0)
					break;
R
Ralph Metzler 已提交
6015 6016
#endif
			/* disable token-ring bus through OFDM block for possible ucode upload */
6017
			status = write16(state, SIO_OFDM_SH_OFDM_RING_ENABLE__A, SIO_OFDM_SH_OFDM_RING_ENABLE_OFF);
6018 6019
			if (status < 0)
				break;
R
Ralph Metzler 已提交
6020 6021

			/* Run SCU for a little while to initialize microcode version numbers */
6022
			status = write16(state, SCU_COMM_EXEC__A, SCU_COMM_EXEC_ACTIVE);
6023 6024 6025 6026 6027
			if (status < 0)
				break;
			status = DRXX_Open(state);
			if (status < 0)
				break;
6028
			/* added for test */
R
Ralph Metzler 已提交
6029 6030 6031
			msleep(30);

			powerMode = DRXK_POWER_DOWN_OFDM;
6032 6033 6034
			status = CtrlPowerMode(state, &powerMode);
			if (status < 0)
				break;
R
Ralph Metzler 已提交
6035 6036 6037 6038 6039 6040

			/* Stamp driver version number in SCU data RAM in BCD code
			   Done to enable field application engineers to retreive drxdriver version
			   via I2C from SCU RAM.
			   Not using SCU command interface for SCU register access since no
			   microcode may be present.
6041 6042 6043 6044 6045 6046
			 */
			driverVersion =
			    (((DRXK_VERSION_MAJOR / 100) % 10) << 12) +
			    (((DRXK_VERSION_MAJOR / 10) % 10) << 8) +
			    ((DRXK_VERSION_MAJOR % 10) << 4) +
			    (DRXK_VERSION_MINOR % 10);
6047
			status = write16(state, SCU_RAM_DRIVER_VER_HI__A, driverVersion);
6048 6049
			if (status < 0)
				break;
6050 6051 6052 6053 6054
			driverVersion =
			    (((DRXK_VERSION_PATCH / 1000) % 10) << 12) +
			    (((DRXK_VERSION_PATCH / 100) % 10) << 8) +
			    (((DRXK_VERSION_PATCH / 10) % 10) << 4) +
			    (DRXK_VERSION_PATCH % 10);
6055
			status = write16(state, SCU_RAM_DRIVER_VER_LO__A, driverVersion);
6056 6057
			if (status < 0)
				break;
6058 6059 6060 6061

			printk(KERN_INFO "DRXK driver version %d.%d.%d\n",
			       DRXK_VERSION_MAJOR, DRXK_VERSION_MINOR,
			       DRXK_VERSION_PATCH);
R
Ralph Metzler 已提交
6062 6063 6064 6065 6066 6067

			/* Dirty fix of default values for ROM/PATCH microcode
			   Dirty because this fix makes it impossible to setup suitable values
			   before calling DRX_Open. This solution requires changes to RF AGC speed
			   to be done via the CTRL function after calling DRX_Open */

6068
			/* m_dvbtRfAgcCfg.speed = 3; */
R
Ralph Metzler 已提交
6069 6070

			/* Reset driver debug flags to 0 */
6071
			status = write16(state, SCU_RAM_DRIVER_DEBUG__A, 0);
6072 6073
			if (status < 0)
				break;
R
Ralph Metzler 已提交
6074 6075 6076
			/* driver 0.9.0 */
			/* Setup FEC OC:
			   NOTE: No more full FEC resets allowed afterwards!! */
6077
			status = write16(state, FEC_COMM_EXEC__A, FEC_COMM_EXEC_STOP);
6078 6079
			if (status < 0)
				break;
6080
			/* MPEGTS functions are still the same */
6081 6082 6083 6084 6085 6086 6087 6088 6089 6090 6091 6092
			status = MPEGTSDtoInit(state);
			if (status < 0)
				break;
			status = MPEGTSStop(state);
			if (status < 0)
				break;
			status = MPEGTSConfigurePolarity(state);
			if (status < 0)
				break;
			status = MPEGTSConfigurePins(state, state->m_enableMPEGOutput);
			if (status < 0)
				break;
6093
			/* added: configure GPIO */
6094 6095 6096
			status = WriteGPIO(state);
			if (status < 0)
				break;
R
Ralph Metzler 已提交
6097

6098
			state->m_DrxkState = DRXK_STOPPED;
R
Ralph Metzler 已提交
6099 6100

			if (state->m_bPowerDown) {
6101 6102 6103
				status = PowerDownDevice(state);
				if (status < 0)
					break;
6104 6105 6106 6107
				state->m_DrxkState = DRXK_POWERED_DOWN;
			} else
				state->m_DrxkState = DRXK_STOPPED;
		} while (0);
R
Ralph Metzler 已提交
6108 6109 6110 6111 6112
	}

	return 0;
}

6113
static void drxk_c_release(struct dvb_frontend *fe)
R
Ralph Metzler 已提交
6114
{
6115 6116
	struct drxk_state *state = fe->demodulator_priv;

6117
	dprintk(1, "\n");
R
Ralph Metzler 已提交
6118 6119 6120
	kfree(state);
}

6121
static int drxk_c_init(struct dvb_frontend *fe)
R
Ralph Metzler 已提交
6122
{
6123
	struct drxk_state *state = fe->demodulator_priv;
R
Ralph Metzler 已提交
6124

6125
	dprintk(1, "\n");
6126
	if (mutex_trylock(&state->ctlock) == 0)
R
Ralph Metzler 已提交
6127 6128 6129 6130 6131
		return -EBUSY;
	SetOperationMode(state, OM_QAM_ITU_A);
	return 0;
}

6132
static int drxk_c_sleep(struct dvb_frontend *fe)
R
Ralph Metzler 已提交
6133
{
6134
	struct drxk_state *state = fe->demodulator_priv;
R
Ralph Metzler 已提交
6135

6136
	dprintk(1, "\n");
R
Ralph Metzler 已提交
6137 6138 6139 6140 6141
	ShutDown(state);
	mutex_unlock(&state->ctlock);
	return 0;
}

6142
static int drxk_gate_ctrl(struct dvb_frontend *fe, int enable)
R
Ralph Metzler 已提交
6143 6144 6145
{
	struct drxk_state *state = fe->demodulator_priv;

6146
	dprintk(1, "%s\n", enable ? "enable" : "disable");
R
Ralph Metzler 已提交
6147 6148 6149
	return ConfigureI2CBridge(state, enable ? true : false);
}

6150 6151
static int drxk_set_parameters(struct dvb_frontend *fe,
			       struct dvb_frontend_parameters *p)
R
Ralph Metzler 已提交
6152 6153 6154 6155
{
	struct drxk_state *state = fe->demodulator_priv;
	u32 IF;

6156
	dprintk(1, "\n");
R
Ralph Metzler 已提交
6157 6158 6159 6160 6161 6162
	if (fe->ops.i2c_gate_ctrl)
		fe->ops.i2c_gate_ctrl(fe, 1);
	if (fe->ops.tuner_ops.set_params)
		fe->ops.tuner_ops.set_params(fe, p);
	if (fe->ops.i2c_gate_ctrl)
		fe->ops.i2c_gate_ctrl(fe, 0);
6163
	state->param = *p;
R
Ralph Metzler 已提交
6164 6165 6166
	fe->ops.tuner_ops.get_frequency(fe, &IF);
	Start(state, 0, IF);

6167
	/* printk(KERN_DEBUG "drxk: %s IF=%d done\n", __func__, IF); */
6168

R
Ralph Metzler 已提交
6169 6170 6171
	return 0;
}

6172 6173
static int drxk_c_get_frontend(struct dvb_frontend *fe,
			       struct dvb_frontend_parameters *p)
R
Ralph Metzler 已提交
6174
{
6175
	dprintk(1, "\n");
R
Ralph Metzler 已提交
6176 6177 6178 6179 6180 6181 6182 6183
	return 0;
}

static int drxk_read_status(struct dvb_frontend *fe, fe_status_t *status)
{
	struct drxk_state *state = fe->demodulator_priv;
	u32 stat;

6184
	dprintk(1, "\n");
6185
	*status = 0;
R
Ralph Metzler 已提交
6186
	GetLockStatus(state, &stat, 0);
6187 6188 6189 6190 6191 6192
	if (stat == MPEG_LOCK)
		*status |= 0x1f;
	if (stat == FEC_LOCK)
		*status |= 0x0f;
	if (stat == DEMOD_LOCK)
		*status |= 0x07;
R
Ralph Metzler 已提交
6193 6194 6195 6196 6197
	return 0;
}

static int drxk_read_ber(struct dvb_frontend *fe, u32 *ber)
{
6198 6199
	dprintk(1, "\n");

6200
	*ber = 0;
R
Ralph Metzler 已提交
6201 6202 6203
	return 0;
}

6204 6205
static int drxk_read_signal_strength(struct dvb_frontend *fe,
				     u16 *strength)
R
Ralph Metzler 已提交
6206 6207 6208 6209
{
	struct drxk_state *state = fe->demodulator_priv;
	u32 val;

6210
	dprintk(1, "\n");
R
Ralph Metzler 已提交
6211
	ReadIFAgc(state, &val);
6212
	*strength = val & 0xffff;
R
Ralph Metzler 已提交
6213 6214 6215 6216 6217 6218 6219 6220
	return 0;
}

static int drxk_read_snr(struct dvb_frontend *fe, u16 *snr)
{
	struct drxk_state *state = fe->demodulator_priv;
	s32 snr2;

6221
	dprintk(1, "\n");
R
Ralph Metzler 已提交
6222
	GetSignalToNoise(state, &snr2);
6223
	*snr = snr2 & 0xffff;
R
Ralph Metzler 已提交
6224 6225 6226 6227 6228 6229 6230 6231
	return 0;
}

static int drxk_read_ucblocks(struct dvb_frontend *fe, u32 *ucblocks)
{
	struct drxk_state *state = fe->demodulator_priv;
	u16 err;

6232
	dprintk(1, "\n");
R
Ralph Metzler 已提交
6233 6234 6235 6236 6237
	DVBTQAMGetAccPktErr(state, &err);
	*ucblocks = (u32) err;
	return 0;
}

6238 6239
static int drxk_c_get_tune_settings(struct dvb_frontend *fe, struct dvb_frontend_tune_settings
				    *sets)
R
Ralph Metzler 已提交
6240
{
6241
	dprintk(1, "\n");
6242 6243 6244
	sets->min_delay_ms = 3000;
	sets->max_drift = 0;
	sets->step_size = 0;
R
Ralph Metzler 已提交
6245 6246 6247
	return 0;
}

6248
static void drxk_t_release(struct dvb_frontend *fe)
R
Ralph Metzler 已提交
6249
{
6250 6251 6252
#if 0
	struct drxk_state *state = fe->demodulator_priv;

6253
	dprintk(1, "\n");
6254 6255
	kfree(state);
#endif
R
Ralph Metzler 已提交
6256 6257
}

6258
static int drxk_t_init(struct dvb_frontend *fe)
R
Ralph Metzler 已提交
6259
{
6260
	struct drxk_state *state = fe->demodulator_priv;
6261 6262

	dprintk(1, "\n");
6263
	if (mutex_trylock(&state->ctlock) == 0)
R
Ralph Metzler 已提交
6264 6265 6266 6267 6268
		return -EBUSY;
	SetOperationMode(state, OM_DVBT);
	return 0;
}

6269
static int drxk_t_sleep(struct dvb_frontend *fe)
R
Ralph Metzler 已提交
6270
{
6271
	struct drxk_state *state = fe->demodulator_priv;
6272 6273

	dprintk(1, "\n");
R
Ralph Metzler 已提交
6274 6275 6276 6277
	mutex_unlock(&state->ctlock);
	return 0;
}

6278 6279
static int drxk_t_get_frontend(struct dvb_frontend *fe,
			       struct dvb_frontend_parameters *p)
R
Ralph Metzler 已提交
6280
{
6281 6282
	dprintk(1, "\n");

R
Ralph Metzler 已提交
6283 6284 6285 6286 6287
	return 0;
}

static struct dvb_frontend_ops drxk_c_ops = {
	.info = {
6288 6289 6290 6291 6292 6293 6294 6295 6296
		 .name = "DRXK DVB-C",
		 .type = FE_QAM,
		 .frequency_stepsize = 62500,
		 .frequency_min = 47000000,
		 .frequency_max = 862000000,
		 .symbol_rate_min = 870000,
		 .symbol_rate_max = 11700000,
		 .caps = FE_CAN_QAM_16 | FE_CAN_QAM_32 | FE_CAN_QAM_64 |
		 FE_CAN_QAM_128 | FE_CAN_QAM_256 | FE_CAN_FEC_AUTO},
R
Ralph Metzler 已提交
6297 6298 6299 6300 6301 6302 6303 6304 6305 6306 6307 6308 6309 6310 6311 6312 6313 6314
	.release = drxk_c_release,
	.init = drxk_c_init,
	.sleep = drxk_c_sleep,
	.i2c_gate_ctrl = drxk_gate_ctrl,

	.set_frontend = drxk_set_parameters,
	.get_frontend = drxk_c_get_frontend,
	.get_tune_settings = drxk_c_get_tune_settings,

	.read_status = drxk_read_status,
	.read_ber = drxk_read_ber,
	.read_signal_strength = drxk_read_signal_strength,
	.read_snr = drxk_read_snr,
	.read_ucblocks = drxk_read_ucblocks,
};

static struct dvb_frontend_ops drxk_t_ops = {
	.info = {
6315 6316 6317 6318 6319 6320 6321 6322 6323 6324 6325 6326 6327 6328
		 .name = "DRXK DVB-T",
		 .type = FE_OFDM,
		 .frequency_min = 47125000,
		 .frequency_max = 865000000,
		 .frequency_stepsize = 166667,
		 .frequency_tolerance = 0,
		 .caps = FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 |
		 FE_CAN_FEC_3_4 | FE_CAN_FEC_5_6 | FE_CAN_FEC_7_8 |
		 FE_CAN_FEC_AUTO |
		 FE_CAN_QAM_16 | FE_CAN_QAM_64 |
		 FE_CAN_QAM_AUTO |
		 FE_CAN_TRANSMISSION_MODE_AUTO |
		 FE_CAN_GUARD_INTERVAL_AUTO |
		 FE_CAN_HIERARCHY_AUTO | FE_CAN_RECOVER | FE_CAN_MUTE_TS},
R
Ralph Metzler 已提交
6329 6330 6331 6332 6333 6334 6335 6336 6337 6338 6339 6340 6341 6342 6343 6344 6345 6346 6347 6348
	.release = drxk_t_release,
	.init = drxk_t_init,
	.sleep = drxk_t_sleep,
	.i2c_gate_ctrl = drxk_gate_ctrl,

	.set_frontend = drxk_set_parameters,
	.get_frontend = drxk_t_get_frontend,

	.read_status = drxk_read_status,
	.read_ber = drxk_read_ber,
	.read_signal_strength = drxk_read_signal_strength,
	.read_snr = drxk_read_snr,
	.read_ucblocks = drxk_read_ucblocks,
};

struct dvb_frontend *drxk_attach(struct i2c_adapter *i2c, u8 adr,
				 struct dvb_frontend **fe_t)
{
	struct drxk_state *state = NULL;

6349
	dprintk(1, "\n");
6350
	state = kzalloc(sizeof(struct drxk_state), GFP_KERNEL);
R
Ralph Metzler 已提交
6351 6352 6353
	if (!state)
		return NULL;

6354 6355
	state->i2c = i2c;
	state->demod_address = adr;
R
Ralph Metzler 已提交
6356 6357 6358 6359

	mutex_init(&state->mutex);
	mutex_init(&state->ctlock);

6360 6361 6362 6363 6364 6365
	memcpy(&state->c_frontend.ops, &drxk_c_ops,
	       sizeof(struct dvb_frontend_ops));
	memcpy(&state->t_frontend.ops, &drxk_t_ops,
	       sizeof(struct dvb_frontend_ops));
	state->c_frontend.demodulator_priv = state;
	state->t_frontend.demodulator_priv = state;
R
Ralph Metzler 已提交
6366 6367

	init_state(state);
6368
	if (init_drxk(state) < 0)
R
Ralph Metzler 已提交
6369 6370 6371 6372 6373
		goto error;
	*fe_t = &state->t_frontend;
	return &state->c_frontend;

error:
6374
	printk(KERN_ERR "drxk: not found\n");
R
Ralph Metzler 已提交
6375 6376 6377
	kfree(state);
	return NULL;
}
6378
EXPORT_SYMBOL(drxk_attach);
R
Ralph Metzler 已提交
6379 6380 6381 6382

MODULE_DESCRIPTION("DRX-K driver");
MODULE_AUTHOR("Ralph Metzler");
MODULE_LICENSE("GPL");