drxk_hard.c 177.6 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
/*
 * 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>
31
#include <linux/hardirq.h>
R
Ralph Metzler 已提交
32 33 34 35 36 37 38 39
#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
}

#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

#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)

162 163 164 165 166 167 168 169 170 171
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)


172
static inline u32 MulDiv32(u32 a, u32 b, u32 c)
R
Ralph Metzler 已提交
173 174 175
{
	u64 tmp64;

176
	tmp64 = (u64) a * (u64) b;
R
Ralph Metzler 已提交
177 178 179 180 181
	do_div(tmp64, c);

	return (u32) tmp64;
}

182
static inline u32 Frac28a(u32 a, u32 c)
R
Ralph Metzler 已提交
183 184 185 186 187
{
	int i = 0;
	u32 Q1 = 0;
	u32 R0 = 0;

188 189 190
	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 已提交
191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207

	/* 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;
208
	u8 i = 0;
R
Ralph Metzler 已提交
209 210 211 212 213
	u32 y = 0;
	u32 d = 0;
	u32 k = 0;
	u32 r = 0;
	/*
214 215 216
	   log2lut[n] = (1<<scale) * 200 * log2(1.0 + ((1.0/(1<<INDEXWIDTH)) * n))
	   0 <= n < ((1<<INDEXWIDTH)+1)
	 */
R
Ralph Metzler 已提交
217 218

	static const u32 log2lut[] = {
219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251
		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 已提交
252 253 254 255
	};


	if (x == 0)
256
		return 0;
R
Ralph Metzler 已提交
257 258 259 260 261

	/* 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--) {
262
			if (x & (((u32) 1) << scale))
R
Ralph Metzler 已提交
263 264 265 266
				break;
			x <<= 1;
		}
	} else {
267 268
		for (k = scale; k < 31; k++) {
			if ((x & (((u32) (-1)) << (scale + 1))) == 0)
R
Ralph Metzler 已提交
269 270
				break;
			x >>= 1;
271
		}
R
Ralph Metzler 已提交
272 273
	}
	/*
274 275
	   Now x has binary point between bit[scale] and bit[scale-1]
	   and 1.0 <= x < 2.0 */
R
Ralph Metzler 已提交
276 277

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

	/* remove integer part */
281
	x &= ((((u32) 1) << scale) - 1);
R
Ralph Metzler 已提交
282 283 284
	/* get index */
	i = (u8) (x >> (scale - indexWidth));
	/* compute delta (x - a) */
285
	d = x & ((((u32) 1) << (scale - indexWidth)) - 1);
R
Ralph Metzler 已提交
286 287
	/* compute log, multiplication (d* (..)) must be within range ! */
	y += log2lut[i] +
288
	    ((d * (log2lut[i + 1] - log2lut[i])) >> (scale - indexWidth));
R
Ralph Metzler 已提交
289
	/* Conver to log10() */
290
	y /= 108853;		/* (log2(10) << scale) */
R
Ralph Metzler 已提交
291 292
	r = (y >> 1);
	/* rounding */
293
	if (y & ((u32) 1))
R
Ralph Metzler 已提交
294
		r++;
295
	return r;
R
Ralph Metzler 已提交
296 297 298 299 300 301
}

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

302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318
static int drxk_i2c_lock(struct drxk_state *state)
{
	i2c_lock_adapter(state->i2c);
	state->drxk_i2c_exclusive_lock = true;

	return 0;
}

static void drxk_i2c_unlock(struct drxk_state *state)
{
	if (!state->drxk_i2c_exclusive_lock)
		return;

	i2c_unlock_adapter(state->i2c);
	state->drxk_i2c_exclusive_lock = false;
}

319 320 321
static int drxk_i2c_transfer(struct drxk_state *state, struct i2c_msg *msgs,
			     unsigned len)
{
322 323 324 325
	if (state->drxk_i2c_exclusive_lock)
		return __i2c_transfer(state->i2c, msgs, len);
	else
		return i2c_transfer(state->i2c, msgs, len);
326 327 328
}

static int i2c_read1(struct drxk_state *state, u8 adr, u8 *val)
R
Ralph Metzler 已提交
329
{
330 331 332
	struct i2c_msg msgs[1] = { {.addr = adr, .flags = I2C_M_RD,
				    .buf = val, .len = 1}
	};
333

334
	return drxk_i2c_transfer(state, msgs, 1);
R
Ralph Metzler 已提交
335 336
}

337
static int i2c_write(struct drxk_state *state, u8 adr, u8 *data, int len)
R
Ralph Metzler 已提交
338
{
339
	int status;
340 341
	struct i2c_msg msg = {
	    .addr = adr, .flags = 0, .buf = data, .len = len };
R
Ralph Metzler 已提交
342

343 344 345 346 347 348 349
	dprintk(3, ":");
	if (debug > 2) {
		int i;
		for (i = 0; i < len; i++)
			printk(KERN_CONT " %02x", data[i]);
		printk(KERN_CONT "\n");
	}
350
	status = drxk_i2c_transfer(state, &msg, 1);
351 352 353 354
	if (status >= 0 && status != 1)
		status = -EIO;

	if (status < 0)
355
		printk(KERN_ERR "drxk: i2c write error at addr 0x%02x\n", adr);
356 357

	return status;
R
Ralph Metzler 已提交
358 359
}

360
static int i2c_read(struct drxk_state *state,
R
Ralph Metzler 已提交
361 362
		    u8 adr, u8 *msg, int len, u8 *answ, int alen)
{
363
	int status;
364 365
	struct i2c_msg msgs[2] = {
		{.addr = adr, .flags = 0,
366
				    .buf = msg, .len = len},
367 368
		{.addr = adr, .flags = I2C_M_RD,
		 .buf = answ, .len = alen}
369
	};
370

371
	status = drxk_i2c_transfer(state, msgs, 2);
372
	if (status != 2) {
373 374
		if (debug > 2)
			printk(KERN_CONT ": ERROR!\n");
375 376
		if (status >= 0)
			status = -EIO;
377

378
		printk(KERN_ERR "drxk: i2c read error at addr 0x%02x\n", adr);
379
		return status;
R
Ralph Metzler 已提交
380
	}
381 382
	if (debug > 2) {
		int i;
383
		dprintk(2, ": read from");
384 385
		for (i = 0; i < len; i++)
			printk(KERN_CONT " %02x", msg[i]);
386
		printk(KERN_CONT ", value = ");
387 388
		for (i = 0; i < alen; i++)
			printk(KERN_CONT " %02x", answ[i]);
389 390
		printk(KERN_CONT "\n");
	}
R
Ralph Metzler 已提交
391 392 393
	return 0;
}

394
static int read16_flags(struct drxk_state *state, u32 reg, u16 *data, u8 flags)
R
Ralph Metzler 已提交
395
{
396
	int status;
397
	u8 adr = state->demod_address, mm1[4], mm2[2], len;
398 399 400 401

	if (state->single_master)
		flags |= 0xC0;

R
Ralph Metzler 已提交
402 403 404 405 406 407 408 409 410 411 412
	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;
	}
413
	dprintk(2, "(0x%08x, 0x%02x)\n", reg, flags);
414
	status = i2c_read(state, adr, mm1, len, mm2, 2);
415 416
	if (status < 0)
		return status;
R
Ralph Metzler 已提交
417 418
	if (data)
		*data = mm2[0] | (mm2[1] << 8);
419

R
Ralph Metzler 已提交
420 421 422
	return 0;
}

423
static int read16(struct drxk_state *state, u32 reg, u16 *data)
R
Ralph Metzler 已提交
424
{
425
	return read16_flags(state, reg, data, 0);
R
Ralph Metzler 已提交
426 427
}

428
static int read32_flags(struct drxk_state *state, u32 reg, u32 *data, u8 flags)
R
Ralph Metzler 已提交
429
{
430
	int status;
R
Ralph Metzler 已提交
431
	u8 adr = state->demod_address, mm1[4], mm2[4], len;
432 433 434 435

	if (state->single_master)
		flags |= 0xC0;

R
Ralph Metzler 已提交
436 437 438 439 440 441 442 443 444 445 446
	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;
	}
447
	dprintk(2, "(0x%08x, 0x%02x)\n", reg, flags);
448
	status = i2c_read(state, adr, mm1, len, mm2, 4);
449 450
	if (status < 0)
		return status;
R
Ralph Metzler 已提交
451 452
	if (data)
		*data = mm2[0] | (mm2[1] << 8) |
453
		    (mm2[2] << 16) | (mm2[3] << 24);
454

R
Ralph Metzler 已提交
455 456 457
	return 0;
}

458 459 460 461 462 463
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 已提交
464 465
{
	u8 adr = state->demod_address, mm[6], len;
466 467 468

	if (state->single_master)
		flags |= 0xC0;
R
Ralph Metzler 已提交
469 470 471 472 473 474 475 476 477 478 479 480
	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;
481
	mm[len + 1] = (data >> 8) & 0xff;
482 483

	dprintk(2, "(0x%08x, 0x%04x, 0x%02x)\n", reg, data, flags);
484
	return i2c_write(state, adr, mm, len + 2);
R
Ralph Metzler 已提交
485 486
}

487
static int write16(struct drxk_state *state, u32 reg, u16 data)
R
Ralph Metzler 已提交
488
{
489
	return write16_flags(state, reg, data, 0);
R
Ralph Metzler 已提交
490 491
}

492
static int write32_flags(struct drxk_state *state, u32 reg, u32 data, u8 flags)
R
Ralph Metzler 已提交
493 494
{
	u8 adr = state->demod_address, mm[8], len;
495 496 497

	if (state->single_master)
		flags |= 0xC0;
R
Ralph Metzler 已提交
498 499 500 501 502 503 504 505 506 507 508 509
	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;
510 511 512
	mm[len + 1] = (data >> 8) & 0xff;
	mm[len + 2] = (data >> 16) & 0xff;
	mm[len + 3] = (data >> 24) & 0xff;
513
	dprintk(2, "(0x%08x, 0x%08x, 0x%02x)\n", reg, data, flags);
514

515
	return i2c_write(state, adr, mm, len + 4);
R
Ralph Metzler 已提交
516 517
}

518 519 520 521 522 523 524
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 已提交
525 526
{
	int status = 0, BlkSize = BlockSize;
527
	u8 Flags = 0;
528 529 530 531

	if (state->single_master)
		Flags |= 0xC0;

532
	while (BlkSize > 0) {
R
Ralph Metzler 已提交
533
		int Chunk = BlkSize > state->m_ChunkSize ?
534
		    state->m_ChunkSize : BlkSize;
R
Ralph Metzler 已提交
535 536 537
		u8 *AdrBuf = &state->Chunk[0];
		u32 AdrLength = 0;

538 539 540 541 542
		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 已提交
543 544 545 546
			AdrBuf[2] |= Flags;
			AdrLength = 4;
			if (Chunk == state->m_ChunkSize)
				Chunk -= 2;
547
		} else {
R
Ralph Metzler 已提交
548 549 550 551 552 553
			AdrBuf[0] = ((Address << 1) & 0xFF);
			AdrBuf[1] = (((Address >> 16) & 0x0F) |
				     ((Address >> 18) & 0xF0));
			AdrLength = 2;
		}
		memcpy(&state->Chunk[AdrLength], pBlock, Chunk);
554 555 556 557 558 559 560 561
		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");
		}
562
		status = i2c_write(state, state->demod_address,
563 564
				   &state->Chunk[0], Chunk + AdrLength);
		if (status < 0) {
565 566
			printk(KERN_ERR "drxk: %s: i2c write error at addr 0x%02x\n",
			       __func__, Address);
R
Ralph Metzler 已提交
567 568 569 570 571 572
			break;
		}
		pBlock += Chunk;
		Address += (Chunk >> 1);
		BlkSize -= Chunk;
	}
573
	return status;
R
Ralph Metzler 已提交
574 575 576 577 578 579
}

#ifndef DRXK_MAX_RETRIES_POWERUP
#define DRXK_MAX_RETRIES_POWERUP 20
#endif

580
static int PowerUpDevice(struct drxk_state *state)
R
Ralph Metzler 已提交
581 582 583 584 585
{
	int status;
	u8 data = 0;
	u16 retryCount = 0;

586 587
	dprintk(1, "\n");

588
	status = i2c_read1(state, state->demod_address, &data);
589
	if (status < 0) {
R
Ralph Metzler 已提交
590 591
		do {
			data = 0;
592
			status = i2c_write(state, state->demod_address,
593
					   &data, 1);
R
Ralph Metzler 已提交
594
			msleep(10);
595
			retryCount++;
596 597
			if (status < 0)
				continue;
598
			status = i2c_read1(state, state->demod_address,
599 600
					   &data);
		} while (status < 0 &&
R
Ralph Metzler 已提交
601
			 (retryCount < DRXK_MAX_RETRIES_POWERUP));
602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623
		if (status < 0 && retryCount >= DRXK_MAX_RETRIES_POWERUP)
			goto error;
	}

	/* Make sure all clk domains are active */
	status = write16(state, SIO_CC_PWD_MODE__A, SIO_CC_PWD_MODE_LEVEL_NONE);
	if (status < 0)
		goto error;
	status = write16(state, SIO_CC_UPDATE__A, SIO_CC_UPDATE_KEY);
	if (status < 0)
		goto error;
	/* Enable pll lock tests */
	status = write16(state, SIO_CC_PLL_LOCK__A, 1);
	if (status < 0)
		goto error;

	state->m_currentPowerMode = DRX_POWER_UP;

error:
	if (status < 0)
		printk(KERN_ERR "drxk: Error %d on %s\n", status, __func__);

R
Ralph Metzler 已提交
624 625 626 627 628 629
	return status;
}


static int init_state(struct drxk_state *state)
{
630 631 632 633
	/*
	 * FIXME: most (all?) of the values bellow should be moved into
	 * struct drxk_config, as they are probably board-specific
	 */
634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660
	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 已提交
661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681

	u32 ulQual83 = DEFAULT_MER_83;
	u32 ulQual93 = DEFAULT_MER_93;

	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;
	u32 ulInvertTSClock = 0;
	u32 ulTSDataStrength = DRXK_MPEG_SERIAL_OUTPUT_PIN_DRIVE_STRENGTH;
	u32 ulDVBTBitrate = 50000000;
	u32 ulDVBCBitrate = DRXK_QAM_SYMBOLRATE_MAX * 8;

	u32 ulInsertRSByte = 0;

	u32 ulRfMirror = 1;
	u32 ulPowerDown = 0;

682 683
	dprintk(1, "\n");

R
Ralph Metzler 已提交
684
	state->m_hasLNA = false;
685 686 687
	state->m_hasDVBT = false;
	state->m_hasDVBC = false;
	state->m_hasATV = false;
R
Ralph Metzler 已提交
688 689 690
	state->m_hasOOB = false;
	state->m_hasAudio = false;

691
	if (!state->m_ChunkSize)
692
		state->m_ChunkSize = 124;
R
Ralph Metzler 已提交
693 694 695 696 697 698

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

	/* real system clock frequency in kHz */
699
	state->m_sysClockFreq = 151875;
R
Ralph Metzler 已提交
700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716
	/* 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_A3_PATCH_CODE = false;

	/* Init AGC and PGA parameters */
	/* VSB IF */
717 718 719 720 721
	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 已提交
722 723 724
	state->m_vsbPgaCfg = 140;

	/* VSB RF */
725 726 727 728 729 730 731 732 733
	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 已提交
734 735 736 737 738 739 740 741 742

	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 */
743 744 745 746 747
	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 已提交
748 749

	/* ATV RF */
750 751 752 753 754 755 756 757 758
	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 已提交
759 760 761


	/* DVBT RF */
762 763 764 765 766 767 768
	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 已提交
769 770 771


	/* DVBT IF */
772 773 774 775 776 777 778
	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 已提交
779
	state->m_dvbtIfAgcCfg.FastClipCtrlDelay = 30;
780 781
	state->m_dvbtIfAgcCfg.IngainTgtMax = 30000;
	/* state->m_dvbtPgaCfg = 140; */
R
Ralph Metzler 已提交
782

783 784
	state->m_dvbtPreSawCfg.reference = 4;
	state->m_dvbtPreSawCfg.usePreSaw = false;
R
Ralph Metzler 已提交
785 786

	/* QAM RF */
787 788 789 790 791 792 793
	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 已提交
794 795

	/* QAM IF */
796 797 798 799 800 801 802 803
	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 已提交
804 805
	state->m_qamIfAgcCfg.FastClipCtrlDelay = 50;

806 807 808
	state->m_qamPgaCfg = 140;
	state->m_qamPreSawCfg.reference = 4;
	state->m_qamPreSawCfg.usePreSaw = false;
R
Ralph Metzler 已提交
809 810 811 812 813

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

	/* MPEG output configuration */
814 815 816 817 818 819 820
	state->m_enableMPEGOutput = true;	/* If TRUE; enable MPEG ouput */
	state->m_insertRSByte = false;	/* If TRUE; insert RS byte */
	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 */
821

R
Ralph Metzler 已提交
822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843
	/* 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);

	/* 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;

844 845
	/* QAM defaults */
	state->m_Constellation = DRX_CONSTELLATION_AUTO;
R
Ralph Metzler 已提交
846
	state->m_qamInterleaveMode = DRXK_QAM_I12_J17;
847 848
	state->m_fecRsPlen = 204 * 8;	/* fecRsPlen  annex A */
	state->m_fecRsPrescale = 1;
R
Ralph Metzler 已提交
849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869

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

	state->m_GPIOCfg = (ulGPIOCfg);

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

	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;

870
	dprintk(1, "\n");
871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891
	/* stop lock indicator process */
	status = write16(state, SCU_RAM_GPIO__A, SCU_RAM_GPIO_HW_LOCK_IND_DISABLE);
	if (status < 0)
		goto error;
	/* Check device id */
	status = read16(state, SIO_TOP_COMM_KEY__A, &key);
	if (status < 0)
		goto error;
	status = write16(state, SIO_TOP_COMM_KEY__A, SIO_TOP_COMM_KEY_KEY);
	if (status < 0)
		goto error;
	status = read32(state, SIO_TOP_JTAGID_LO__A, &jtag);
	if (status < 0)
		goto error;
	status = read16(state, SIO_PDR_UIO_IN_HI__A, &bid);
	if (status < 0)
		goto error;
	status = write16(state, SIO_TOP_COMM_KEY__A, key);
error:
	if (status < 0)
		printk(KERN_ERR "drxk: Error %d on %s\n", status, __func__);
R
Ralph Metzler 已提交
892 893 894 895 896
	return status;
}

static int GetDeviceCapabilities(struct drxk_state *state)
{
897
	u16 sioPdrOhwCfg = 0;
R
Ralph Metzler 已提交
898 899
	u32 sioTopJtagidLo = 0;
	int status;
900
	const char *spin = "";
R
Ralph Metzler 已提交
901

902
	dprintk(1, "\n");
R
Ralph Metzler 已提交
903

904 905 906 907 908
	/* driver 0.9.0 */
	/* stop lock indicator process */
	status = write16(state, SCU_RAM_GPIO__A, SCU_RAM_GPIO_HW_LOCK_IND_DISABLE);
	if (status < 0)
		goto error;
909
	status = write16(state, SIO_TOP_COMM_KEY__A, SIO_TOP_COMM_KEY_KEY);
910 911 912 913 914 915 916 917
	if (status < 0)
		goto error;
	status = read16(state, SIO_PDR_OHW_CFG__A, &sioPdrOhwCfg);
	if (status < 0)
		goto error;
	status = write16(state, SIO_TOP_COMM_KEY__A, 0x0000);
	if (status < 0)
		goto error;
R
Ralph Metzler 已提交
918

919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935
	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:
936
		printk(KERN_ERR "drxk: Clock Frequency is unknown\n");
937 938 939 940 941 942 943 944 945
		return -EINVAL;
	}
	/*
		Determine device capabilities
		Based on pinning v14
		*/
	status = read32(state, SIO_TOP_JTAGID_LO__A, &sioTopJtagidLo);
	if (status < 0)
		goto error;
946

947
	printk(KERN_INFO "drxk: status = 0x%08x\n", sioTopJtagidLo);
948

949 950 951 952
	/* driver 0.9.0 */
	switch ((sioTopJtagidLo >> 29) & 0xF) {
	case 0:
		state->m_deviceSpin = DRXK_SPIN_A1;
953
		spin = "A1";
954 955 956
		break;
	case 2:
		state->m_deviceSpin = DRXK_SPIN_A2;
957
		spin = "A2";
958 959 960
		break;
	case 3:
		state->m_deviceSpin = DRXK_SPIN_A3;
961
		spin = "A3";
962 963 964 965
		break;
	default:
		state->m_deviceSpin = DRXK_SPIN_UNKNOWN;
		status = -EINVAL;
966 967
		printk(KERN_ERR "drxk: Spin %d unknown\n",
		       (sioTopJtagidLo >> 29) & 0xF);
968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075
		goto error2;
	}
	switch ((sioTopJtagidLo >> 12) & 0xFF) {
	case 0x13:
		/* typeId = DRX3913K_TYPE_ID */
		state->m_hasLNA = false;
		state->m_hasOOB = false;
		state->m_hasATV = false;
		state->m_hasAudio = false;
		state->m_hasDVBT = true;
		state->m_hasDVBC = true;
		state->m_hasSAWSW = true;
		state->m_hasGPIO2 = false;
		state->m_hasGPIO1 = false;
		state->m_hasIRQN = false;
		break;
	case 0x15:
		/* typeId = DRX3915K_TYPE_ID */
		state->m_hasLNA = false;
		state->m_hasOOB = false;
		state->m_hasATV = true;
		state->m_hasAudio = false;
		state->m_hasDVBT = true;
		state->m_hasDVBC = false;
		state->m_hasSAWSW = true;
		state->m_hasGPIO2 = true;
		state->m_hasGPIO1 = true;
		state->m_hasIRQN = false;
		break;
	case 0x16:
		/* typeId = DRX3916K_TYPE_ID */
		state->m_hasLNA = false;
		state->m_hasOOB = false;
		state->m_hasATV = true;
		state->m_hasAudio = false;
		state->m_hasDVBT = true;
		state->m_hasDVBC = false;
		state->m_hasSAWSW = true;
		state->m_hasGPIO2 = true;
		state->m_hasGPIO1 = true;
		state->m_hasIRQN = false;
		break;
	case 0x18:
		/* typeId = DRX3918K_TYPE_ID */
		state->m_hasLNA = false;
		state->m_hasOOB = false;
		state->m_hasATV = true;
		state->m_hasAudio = true;
		state->m_hasDVBT = true;
		state->m_hasDVBC = false;
		state->m_hasSAWSW = true;
		state->m_hasGPIO2 = true;
		state->m_hasGPIO1 = true;
		state->m_hasIRQN = false;
		break;
	case 0x21:
		/* typeId = DRX3921K_TYPE_ID */
		state->m_hasLNA = false;
		state->m_hasOOB = false;
		state->m_hasATV = true;
		state->m_hasAudio = true;
		state->m_hasDVBT = true;
		state->m_hasDVBC = true;
		state->m_hasSAWSW = true;
		state->m_hasGPIO2 = true;
		state->m_hasGPIO1 = true;
		state->m_hasIRQN = false;
		break;
	case 0x23:
		/* typeId = DRX3923K_TYPE_ID */
		state->m_hasLNA = false;
		state->m_hasOOB = false;
		state->m_hasATV = true;
		state->m_hasAudio = true;
		state->m_hasDVBT = true;
		state->m_hasDVBC = true;
		state->m_hasSAWSW = true;
		state->m_hasGPIO2 = true;
		state->m_hasGPIO1 = true;
		state->m_hasIRQN = false;
		break;
	case 0x25:
		/* typeId = DRX3925K_TYPE_ID */
		state->m_hasLNA = false;
		state->m_hasOOB = false;
		state->m_hasATV = true;
		state->m_hasAudio = true;
		state->m_hasDVBT = true;
		state->m_hasDVBC = true;
		state->m_hasSAWSW = true;
		state->m_hasGPIO2 = true;
		state->m_hasGPIO1 = true;
		state->m_hasIRQN = false;
		break;
	case 0x26:
		/* typeId = DRX3926K_TYPE_ID */
		state->m_hasLNA = false;
		state->m_hasOOB = false;
		state->m_hasATV = true;
		state->m_hasAudio = false;
		state->m_hasDVBT = true;
		state->m_hasDVBC = true;
		state->m_hasSAWSW = true;
		state->m_hasGPIO2 = true;
		state->m_hasGPIO1 = true;
		state->m_hasIRQN = false;
		break;
	default:
1076
		printk(KERN_ERR "drxk: DeviceID 0x%02x not supported\n",
1077 1078 1079 1080 1081
			((sioTopJtagidLo >> 12) & 0xFF));
		status = -EINVAL;
		goto error2;
	}

1082 1083 1084 1085 1086 1087
	printk(KERN_INFO
	       "drxk: detected a drx-39%02xk, spin %s, xtal %d.%03d MHz\n",
	       ((sioTopJtagidLo >> 12) & 0xFF), spin,
	       state->m_oscClockFreq / 1000,
	       state->m_oscClockFreq % 1000);

1088 1089 1090 1091 1092
error:
	if (status < 0)
		printk(KERN_ERR "drxk: Error %d on %s\n", status, __func__);

error2:
R
Ralph Metzler 已提交
1093 1094 1095 1096 1097 1098 1099 1100
	return status;
}

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

1101 1102
	dprintk(1, "\n");

R
Ralph Metzler 已提交
1103
	/* Write command */
1104
	status = write16(state, SIO_HI_RA_RAM_CMD__A, cmd);
R
Ralph Metzler 已提交
1105
	if (status < 0)
1106
		goto error;
R
Ralph Metzler 已提交
1107 1108 1109 1110
	if (cmd == SIO_HI_RA_RAM_CMD_RESET)
		msleep(1);

	powerdown_cmd =
1111 1112 1113 1114
	    (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 已提交
1115 1116 1117 1118 1119 1120 1121 1122
	if (powerdown_cmd == false) {
		/* Wait until command rdy */
		u32 retryCount = 0;
		u16 waitCmd;

		do {
			msleep(1);
			retryCount += 1;
1123 1124
			status = read16(state, SIO_HI_RA_RAM_CMD__A,
					  &waitCmd);
1125 1126
		} while ((status < 0) && (retryCount < DRXK_MAX_RETRIES)
			 && (waitCmd != 0));
1127 1128 1129
		if (status < 0)
			goto error;
		status = read16(state, SIO_HI_RA_RAM_RES__A, pResult);
R
Ralph Metzler 已提交
1130
	}
1131 1132 1133 1134
error:
	if (status < 0)
		printk(KERN_ERR "drxk: Error %d on %s\n", status, __func__);

R
Ralph Metzler 已提交
1135 1136 1137 1138 1139 1140 1141
	return status;
}

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

1142 1143
	dprintk(1, "\n");

R
Ralph Metzler 已提交
1144 1145
	mutex_lock(&state->mutex);

1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169
	status = write16(state, SIO_HI_RA_RAM_PAR_6__A, state->m_HICfgTimeout);
	if (status < 0)
		goto error;
	status = write16(state, SIO_HI_RA_RAM_PAR_5__A, state->m_HICfgCtrl);
	if (status < 0)
		goto error;
	status = write16(state, SIO_HI_RA_RAM_PAR_4__A, state->m_HICfgWakeUpKey);
	if (status < 0)
		goto error;
	status = write16(state, SIO_HI_RA_RAM_PAR_3__A, state->m_HICfgBridgeDelay);
	if (status < 0)
		goto error;
	status = write16(state, SIO_HI_RA_RAM_PAR_2__A, state->m_HICfgTimingDiv);
	if (status < 0)
		goto error;
	status = write16(state, SIO_HI_RA_RAM_PAR_1__A, SIO_HI_RA_RAM_PAR_1_PAR1_SEC_KEY);
	if (status < 0)
		goto error;
	status = HI_Command(state, SIO_HI_RA_RAM_CMD_CONFIG, 0);
	if (status < 0)
		goto error;

	state->m_HICfgCtrl &= ~SIO_HI_RA_RAM_PAR_5_CFG_SLEEP_ZZZ;
error:
R
Ralph Metzler 已提交
1170
	mutex_unlock(&state->mutex);
1171 1172
	if (status < 0)
		printk(KERN_ERR "drxk: Error %d on %s\n", status, __func__);
R
Ralph Metzler 已提交
1173 1174 1175 1176 1177
	return status;
}

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

1180
	state->m_HICfgWakeUpKey = (state->demod_address << 1);
R
Ralph Metzler 已提交
1181 1182 1183
	state->m_HICfgTimeout = 0x96FF;
	/* port/bridge/power down ctrl */
	state->m_HICfgCtrl = SIO_HI_RA_RAM_PAR_5_CFG_SLV0_SLAVE;
1184

1185
	return HI_CfgCommand(state);
R
Ralph Metzler 已提交
1186 1187 1188 1189 1190
}

static int MPEGTSConfigurePins(struct drxk_state *state, bool mpegEnable)
{
	int status = -1;
1191 1192
	u16 sioPdrMclkCfg = 0;
	u16 sioPdrMdxCfg = 0;
1193
	u16 err_cfg = 0;
R
Ralph Metzler 已提交
1194

1195 1196 1197
	dprintk(1, ": mpeg %s, %s mode\n",
		mpegEnable ? "enable" : "disable",
		state->m_enableParallel ? "parallel" : "serial");
R
Ralph Metzler 已提交
1198

1199 1200 1201 1202 1203 1204
	/* stop lock indicator process */
	status = write16(state, SCU_RAM_GPIO__A, SCU_RAM_GPIO_HW_LOCK_IND_DISABLE);
	if (status < 0)
		goto error;

	/*  MPEG TS pad configuration */
1205
	status = write16(state, SIO_TOP_COMM_KEY__A, SIO_TOP_COMM_KEY_KEY);
1206 1207 1208 1209 1210 1211
	if (status < 0)
		goto error;

	if (mpegEnable == false) {
		/*  Set MPEG TS pads to inputmode */
		status = write16(state, SIO_PDR_MSTRT_CFG__A, 0x0000);
1212
		if (status < 0)
1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254
			goto error;
		status = write16(state, SIO_PDR_MERR_CFG__A, 0x0000);
		if (status < 0)
			goto error;
		status = write16(state, SIO_PDR_MCLK_CFG__A, 0x0000);
		if (status < 0)
			goto error;
		status = write16(state, SIO_PDR_MVAL_CFG__A, 0x0000);
		if (status < 0)
			goto error;
		status = write16(state, SIO_PDR_MD0_CFG__A, 0x0000);
		if (status < 0)
			goto error;
		status = write16(state, SIO_PDR_MD1_CFG__A, 0x0000);
		if (status < 0)
			goto error;
		status = write16(state, SIO_PDR_MD2_CFG__A, 0x0000);
		if (status < 0)
			goto error;
		status = write16(state, SIO_PDR_MD3_CFG__A, 0x0000);
		if (status < 0)
			goto error;
		status = write16(state, SIO_PDR_MD4_CFG__A, 0x0000);
		if (status < 0)
			goto error;
		status = write16(state, SIO_PDR_MD5_CFG__A, 0x0000);
		if (status < 0)
			goto error;
		status = write16(state, SIO_PDR_MD6_CFG__A, 0x0000);
		if (status < 0)
			goto error;
		status = write16(state, SIO_PDR_MD7_CFG__A, 0x0000);
		if (status < 0)
			goto error;
	} else {
		/* Enable MPEG output */
		sioPdrMdxCfg =
			((state->m_TSDataStrength <<
			SIO_PDR_MD0_CFG_DRIVE__B) | 0x0003);
		sioPdrMclkCfg = ((state->m_TSClockkStrength <<
					SIO_PDR_MCLK_CFG_DRIVE__B) |
					0x0003);
R
Ralph Metzler 已提交
1255

1256 1257 1258
		status = write16(state, SIO_PDR_MSTRT_CFG__A, sioPdrMdxCfg);
		if (status < 0)
			goto error;
1259 1260 1261 1262 1263

		if (state->enable_merr_cfg)
			err_cfg = sioPdrMdxCfg;

		status = write16(state, SIO_PDR_MERR_CFG__A, err_cfg);
1264 1265
		if (status < 0)
			goto error;
1266
		status = write16(state, SIO_PDR_MVAL_CFG__A, err_cfg);
1267 1268
		if (status < 0)
			goto error;
1269

1270 1271 1272
		if (state->m_enableParallel == true) {
			/* paralel -> enable MD1 to MD7 */
			status = write16(state, SIO_PDR_MD1_CFG__A, sioPdrMdxCfg);
1273
			if (status < 0)
1274 1275
				goto error;
			status = write16(state, SIO_PDR_MD2_CFG__A, sioPdrMdxCfg);
1276
			if (status < 0)
1277 1278
				goto error;
			status = write16(state, SIO_PDR_MD3_CFG__A, sioPdrMdxCfg);
1279
			if (status < 0)
1280 1281
				goto error;
			status = write16(state, SIO_PDR_MD4_CFG__A, sioPdrMdxCfg);
1282
			if (status < 0)
1283 1284
				goto error;
			status = write16(state, SIO_PDR_MD5_CFG__A, sioPdrMdxCfg);
1285
			if (status < 0)
1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297
				goto error;
			status = write16(state, SIO_PDR_MD6_CFG__A, sioPdrMdxCfg);
			if (status < 0)
				goto error;
			status = write16(state, SIO_PDR_MD7_CFG__A, sioPdrMdxCfg);
			if (status < 0)
				goto error;
		} else {
			sioPdrMdxCfg = ((state->m_TSDataStrength <<
						SIO_PDR_MD0_CFG_DRIVE__B)
					| 0x0003);
			/* serial -> disable MD1 to MD7 */
1298
			status = write16(state, SIO_PDR_MD1_CFG__A, 0x0000);
1299
			if (status < 0)
1300
				goto error;
1301
			status = write16(state, SIO_PDR_MD2_CFG__A, 0x0000);
1302
			if (status < 0)
1303
				goto error;
1304
			status = write16(state, SIO_PDR_MD3_CFG__A, 0x0000);
1305
			if (status < 0)
1306
				goto error;
1307
			status = write16(state, SIO_PDR_MD4_CFG__A, 0x0000);
1308
			if (status < 0)
1309
				goto error;
1310
			status = write16(state, SIO_PDR_MD5_CFG__A, 0x0000);
1311
			if (status < 0)
1312
				goto error;
1313
			status = write16(state, SIO_PDR_MD6_CFG__A, 0x0000);
1314
			if (status < 0)
1315
				goto error;
1316
			status = write16(state, SIO_PDR_MD7_CFG__A, 0x0000);
1317
			if (status < 0)
1318
				goto error;
R
Ralph Metzler 已提交
1319
		}
1320
		status = write16(state, SIO_PDR_MCLK_CFG__A, sioPdrMclkCfg);
1321
		if (status < 0)
1322 1323
			goto error;
		status = write16(state, SIO_PDR_MD0_CFG__A, sioPdrMdxCfg);
1324
		if (status < 0)
1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335
			goto error;
	}
	/*  Enable MB output over MPEG pads and ctl input */
	status = write16(state, SIO_PDR_MON_CFG__A, 0x0000);
	if (status < 0)
		goto error;
	/*  Write nomagic word to enable pdr reg write */
	status = write16(state, SIO_TOP_COMM_KEY__A, 0x0000);
error:
	if (status < 0)
		printk(KERN_ERR "drxk: Error %d on %s\n", status, __func__);
R
Ralph Metzler 已提交
1336 1337 1338 1339 1340
	return status;
}

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

R
Ralph Metzler 已提交
1343 1344 1345 1346 1347 1348 1349 1350 1351 1352
	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;

1353
	dprintk(1, "\n");
R
Ralph Metzler 已提交
1354
	mutex_lock(&state->mutex);
1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368
	status = write16(state, SIO_BL_MODE__A, SIO_BL_MODE_CHAIN);
	if (status < 0)
		goto error;
	status = write16(state, SIO_BL_CHAIN_ADDR__A, romOffset);
	if (status < 0)
		goto error;
	status = write16(state, SIO_BL_CHAIN_LEN__A, nrOfElements);
	if (status < 0)
		goto error;
	status = write16(state, SIO_BL_ENABLE__A, SIO_BL_ENABLE_ON);
	if (status < 0)
		goto error;

	end = jiffies + msecs_to_jiffies(timeOut);
R
Ralph Metzler 已提交
1369
	do {
1370 1371
		msleep(1);
		status = read16(state, SIO_BL_STATUS__A, &blStatus);
1372
		if (status < 0)
1373 1374 1375
			goto error;
	} while ((blStatus == 0x1) &&
			((time_is_after_jiffies(end))));
R
Ralph Metzler 已提交
1376

1377 1378 1379 1380 1381 1382 1383 1384 1385
	if (blStatus == 0x1) {
		printk(KERN_ERR "drxk: SIO not ready\n");
		status = -EINVAL;
		goto error2;
	}
error:
	if (status < 0)
		printk(KERN_ERR "drxk: Error %d on %s\n", status, __func__);
error2:
R
Ralph Metzler 已提交
1386 1387 1388 1389 1390 1391
	mutex_unlock(&state->mutex);
	return status;
}


static int DownloadMicrocode(struct drxk_state *state,
1392
			     const u8 pMCImage[], u32 Length)
R
Ralph Metzler 已提交
1393 1394 1395 1396 1397 1398 1399
{
	const u8 *pSrc = pMCImage;
	u32 Address;
	u16 nBlocks;
	u16 BlockSize;
	u32 offset = 0;
	u32 i;
1400
	int status = 0;
R
Ralph Metzler 已提交
1401

1402 1403
	dprintk(1, "\n");

1404 1405 1406
	/* down the drain (we don't care about MAGIC_WORD) */
#if 0
	/* For future reference */
R
Ralph Metzler 已提交
1407
	Drain = (pSrc[0] << 8) | pSrc[1];
1408
#endif
1409 1410
	pSrc += sizeof(u16);
	offset += sizeof(u16);
R
Ralph Metzler 已提交
1411
	nBlocks = (pSrc[0] << 8) | pSrc[1];
1412 1413
	pSrc += sizeof(u16);
	offset += sizeof(u16);
R
Ralph Metzler 已提交
1414 1415 1416

	for (i = 0; i < nBlocks; i += 1) {
		Address = (pSrc[0] << 24) | (pSrc[1] << 16) |
1417 1418 1419
		    (pSrc[2] << 8) | pSrc[3];
		pSrc += sizeof(u32);
		offset += sizeof(u32);
R
Ralph Metzler 已提交
1420 1421

		BlockSize = ((pSrc[0] << 8) | pSrc[1]) * sizeof(u16);
1422 1423
		pSrc += sizeof(u16);
		offset += sizeof(u16);
R
Ralph Metzler 已提交
1424

1425 1426
#if 0
		/* For future reference */
R
Ralph Metzler 已提交
1427
		Flags = (pSrc[0] << 8) | pSrc[1];
1428
#endif
1429 1430
		pSrc += sizeof(u16);
		offset += sizeof(u16);
R
Ralph Metzler 已提交
1431

1432 1433
#if 0
		/* For future reference */
R
Ralph Metzler 已提交
1434
		BlockCRC = (pSrc[0] << 8) | pSrc[1];
1435
#endif
1436 1437
		pSrc += sizeof(u16);
		offset += sizeof(u16);
1438 1439 1440 1441 1442 1443

		if (offset + BlockSize > Length) {
			printk(KERN_ERR "drxk: Firmware is corrupted.\n");
			return -EINVAL;
		}

1444
		status = write_block(state, Address, BlockSize, pSrc);
1445 1446
		if (status < 0) {
			printk(KERN_ERR "drxk: Error %d while loading firmware\n", status);
R
Ralph Metzler 已提交
1447
			break;
1448
		}
R
Ralph Metzler 已提交
1449 1450 1451 1452 1453 1454 1455 1456 1457
		pSrc += BlockSize;
		offset += BlockSize;
	}
	return status;
}

static int DVBTEnableOFDMTokenRing(struct drxk_state *state, bool enable)
{
	int status;
1458 1459
	u16 data = 0;
	u16 desiredCtrl = SIO_OFDM_SH_OFDM_RING_ENABLE_ON;
R
Ralph Metzler 已提交
1460 1461 1462
	u16 desiredStatus = SIO_OFDM_SH_OFDM_RING_STATUS_ENABLED;
	unsigned long end;

1463 1464
	dprintk(1, "\n");

R
Ralph Metzler 已提交
1465
	if (enable == false) {
1466
		desiredCtrl = SIO_OFDM_SH_OFDM_RING_ENABLE_OFF;
R
Ralph Metzler 已提交
1467 1468 1469
		desiredStatus = SIO_OFDM_SH_OFDM_RING_STATUS_DOWN;
	}

1470 1471
	status = read16(state, SIO_OFDM_SH_OFDM_RING_STATUS__A, &data);
	if (status >= 0 && data == desiredStatus) {
R
Ralph Metzler 已提交
1472 1473 1474 1475
		/* tokenring already has correct status */
		return status;
	}
	/* Disable/enable dvbt tokenring bridge   */
1476
	status = write16(state, SIO_OFDM_SH_OFDM_RING_ENABLE__A, desiredCtrl);
R
Ralph Metzler 已提交
1477

1478
	end = jiffies + msecs_to_jiffies(DRXK_OFDM_TR_SHUTDOWN_TIMEOUT);
1479
	do {
1480
		status = read16(state, SIO_OFDM_SH_OFDM_RING_STATUS__A, &data);
1481
		if ((status >= 0 && data == desiredStatus) || time_is_after_jiffies(end))
1482
			break;
1483 1484
		msleep(1);
	} while (1);
R
Ralph Metzler 已提交
1485
	if (data != desiredStatus) {
1486
		printk(KERN_ERR "drxk: SIO not ready\n");
1487
		return -EINVAL;
R
Ralph Metzler 已提交
1488 1489 1490 1491 1492 1493 1494 1495 1496 1497
	}
	return status;
}

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

1498 1499
	dprintk(1, "\n");

1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518
	/* Gracefull shutdown (byte boundaries) */
	status = read16(state, FEC_OC_SNC_MODE__A, &fecOcSncMode);
	if (status < 0)
		goto error;
	fecOcSncMode |= FEC_OC_SNC_MODE_SHUTDOWN__M;
	status = write16(state, FEC_OC_SNC_MODE__A, fecOcSncMode);
	if (status < 0)
		goto error;

	/* Suppress MCLK during absence of data */
	status = read16(state, FEC_OC_IPR_MODE__A, &fecOcIprMode);
	if (status < 0)
		goto error;
	fecOcIprMode |= FEC_OC_IPR_MODE_MCLK_DIS_DAT_ABS__M;
	status = write16(state, FEC_OC_IPR_MODE__A, fecOcIprMode);

error:
	if (status < 0)
		printk(KERN_ERR "drxk: Error %d on %s\n", status, __func__);
R
Ralph Metzler 已提交
1519 1520 1521 1522 1523 1524

	return status;
}

static int scu_command(struct drxk_state *state,
		       u16 cmd, u8 parameterLen,
1525
		       u16 *parameter, u8 resultLen, u16 *result)
R
Ralph Metzler 已提交
1526 1527 1528 1529 1530
{
#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;
1531
	int status = -EINVAL;
R
Ralph Metzler 已提交
1532
	unsigned long end;
1533 1534
	u8 buffer[34];
	int cnt = 0, ii;
1535 1536
	const char *p;
	char errname[30];
R
Ralph Metzler 已提交
1537

1538 1539
	dprintk(1, "\n");

R
Ralph Metzler 已提交
1540
	if ((cmd == 0) || ((parameterLen > 0) && (parameter == NULL)) ||
1541 1542 1543 1544
	    ((resultLen > 0) && (result == NULL))) {
		printk(KERN_ERR "drxk: Error %d on %s\n", status, __func__);
		return status;
	}
R
Ralph Metzler 已提交
1545 1546

	mutex_lock(&state->mutex);
1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560

	/* assume that the command register is ready
		since it is checked afterwards */
	for (ii = parameterLen - 1; ii >= 0; ii -= 1) {
		buffer[cnt++] = (parameter[ii] & 0xFF);
		buffer[cnt++] = ((parameter[ii] >> 8) & 0xFF);
	}
	buffer[cnt++] = (cmd & 0xFF);
	buffer[cnt++] = ((cmd >> 8) & 0xFF);

	write_block(state, SCU_RAM_PARAM_0__A -
			(parameterLen - 1), cnt, buffer);
	/* Wait until SCU has processed command */
	end = jiffies + msecs_to_jiffies(DRXK_MAX_WAITTIME);
R
Ralph Metzler 已提交
1561
	do {
1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575
		msleep(1);
		status = read16(state, SCU_RAM_COMMAND__A, &curCmd);
		if (status < 0)
			goto error;
	} while (!(curCmd == DRX_SCU_READY) && (time_is_after_jiffies(end)));
	if (curCmd != DRX_SCU_READY) {
		printk(KERN_ERR "drxk: SCU not ready\n");
		status = -EIO;
		goto error2;
	}
	/* read results */
	if ((resultLen > 0) && (result != NULL)) {
		s16 err;
		int ii;
R
Ralph Metzler 已提交
1576

1577 1578
		for (ii = resultLen - 1; ii >= 0; ii -= 1) {
			status = read16(state, SCU_RAM_PARAM_0__A - ii, &result[ii]);
1579
			if (status < 0)
1580
				goto error;
R
Ralph Metzler 已提交
1581
		}
1582 1583 1584

		/* Check if an error was reported by SCU */
		err = (s16)result[0];
1585 1586
		if (err >= 0)
			goto error;
1587

1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604
		/* check for the known error codes */
		switch (err) {
		case SCU_RESULT_UNKCMD:
			p = "SCU_RESULT_UNKCMD";
			break;
		case SCU_RESULT_UNKSTD:
			p = "SCU_RESULT_UNKSTD";
			break;
		case SCU_RESULT_SIZE:
			p = "SCU_RESULT_SIZE";
			break;
		case SCU_RESULT_INVPAR:
			p = "SCU_RESULT_INVPAR";
			break;
		default: /* Other negative values are errors */
			sprintf(errname, "ERROR: %d\n", err);
			p = errname;
R
Ralph Metzler 已提交
1605
		}
1606 1607 1608 1609
		printk(KERN_ERR "drxk: %s while sending cmd 0x%04x with params:", p, cmd);
		print_hex_dump_bytes("drxk: ", DUMP_PREFIX_NONE, buffer, cnt);
		status = -EINVAL;
		goto error2;
1610 1611 1612
	}

error:
1613
	if (status < 0)
1614 1615 1616
		printk(KERN_ERR "drxk: Error %d on %s\n", status, __func__);
error2:
	mutex_unlock(&state->mutex);
R
Ralph Metzler 已提交
1617 1618 1619 1620 1621 1622 1623 1624
	return status;
}

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

1625 1626
	dprintk(1, "\n");

1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650
	/* Configure IQM */
	status = read16(state, IQM_AF_STDBY__A, &data);
	if (status < 0)
		goto error;

	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
				| IQM_AF_STDBY_STDBY_TAGC_RF_STANDBY);
	} else {
		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)
			);
	}
	status = write16(state, IQM_AF_STDBY__A, data);

error:
	if (status < 0)
		printk(KERN_ERR "drxk: Error %d on %s\n", status, __func__);
R
Ralph Metzler 已提交
1651 1652 1653
	return status;
}

1654
static int CtrlPowerMode(struct drxk_state *state, enum DRXPowerMode *mode)
R
Ralph Metzler 已提交
1655 1656
{
	int status = 0;
1657
	u16 sioCcPwdMode = 0;
R
Ralph Metzler 已提交
1658

1659 1660
	dprintk(1, "\n");

R
Ralph Metzler 已提交
1661 1662
	/* Check arguments */
	if (mode == NULL)
1663
		return -EINVAL;
R
Ralph Metzler 已提交
1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682

	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 */
1683
		return -EINVAL;
R
Ralph Metzler 已提交
1684 1685 1686 1687 1688 1689 1690
	}

	/* 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 */
1691
	if (state->m_currentPowerMode != DRX_POWER_UP) {
1692 1693 1694 1695 1696 1697
		status = PowerUpDevice(state);
		if (status < 0)
			goto error;
		status = DVBTEnableOFDMTokenRing(state, true);
		if (status < 0)
			goto error;
R
Ralph Metzler 已提交
1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711
	}

	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 */
1712 1713 1714
		switch (state->m_OperationMode) {
		case OM_DVBT:
			status = MPEGTSStop(state);
1715
			if (status < 0)
1716 1717
				goto error;
			status = PowerDownDVBT(state, false);
1718
			if (status < 0)
1719 1720 1721 1722 1723
				goto error;
			break;
		case OM_QAM_ITU_A:
		case OM_QAM_ITU_C:
			status = MPEGTSStop(state);
1724
			if (status < 0)
1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741
				goto error;
			status = PowerDownQAM(state);
			if (status < 0)
				goto error;
			break;
		default:
			break;
		}
		status = DVBTEnableOFDMTokenRing(state, false);
		if (status < 0)
			goto error;
		status = write16(state, SIO_CC_PWD_MODE__A, sioCcPwdMode);
		if (status < 0)
			goto error;
		status = write16(state, SIO_CC_UPDATE__A, SIO_CC_UPDATE_KEY);
		if (status < 0)
			goto error;
R
Ralph Metzler 已提交
1742

1743 1744 1745 1746 1747 1748 1749
		if (*mode != DRXK_POWER_DOWN_OFDM) {
			state->m_HICfgCtrl |=
				SIO_HI_RA_RAM_PAR_5_CFG_SLEEP_ZZZ;
			status = HI_CfgCommand(state);
			if (status < 0)
				goto error;
		}
R
Ralph Metzler 已提交
1750 1751
	}
	state->m_currentPowerMode = *mode;
1752 1753 1754 1755 1756

error:
	if (status < 0)
		printk(KERN_ERR "drxk: Error %d on %s\n", status, __func__);

1757
	return status;
R
Ralph Metzler 已提交
1758 1759 1760 1761
}

static int PowerDownDVBT(struct drxk_state *state, bool setPowerMode)
{
1762
	enum DRXPowerMode powerMode = DRXK_POWER_DOWN_OFDM;
R
Ralph Metzler 已提交
1763 1764 1765 1766
	u16 cmdResult = 0;
	u16 data = 0;
	int status;

1767 1768
	dprintk(1, "\n");

1769 1770 1771 1772 1773 1774
	status = read16(state, SCU_COMM_EXEC__A, &data);
	if (status < 0)
		goto error;
	if (data == SCU_COMM_EXEC_ACTIVE) {
		/* Send OFDM stop command */
		status = scu_command(state, SCU_RAM_COMMAND_STANDARD_OFDM | SCU_RAM_COMMAND_CMD_DEMOD_STOP, 0, NULL, 1, &cmdResult);
1775
		if (status < 0)
1776 1777 1778
			goto error;
		/* Send OFDM reset command */
		status = scu_command(state, SCU_RAM_COMMAND_STANDARD_OFDM | SCU_RAM_COMMAND_CMD_DEMOD_RESET, 0, NULL, 1, &cmdResult);
1779
		if (status < 0)
1780 1781
			goto error;
	}
R
Ralph Metzler 已提交
1782

1783 1784 1785 1786 1787 1788 1789 1790 1791 1792
	/* Reset datapath for OFDM, processors first */
	status = write16(state, OFDM_SC_COMM_EXEC__A, OFDM_SC_COMM_EXEC_STOP);
	if (status < 0)
		goto error;
	status = write16(state, OFDM_LC_COMM_EXEC__A, OFDM_LC_COMM_EXEC_STOP);
	if (status < 0)
		goto error;
	status = write16(state, IQM_COMM_EXEC__A, IQM_COMM_EXEC_B_STOP);
	if (status < 0)
		goto error;
R
Ralph Metzler 已提交
1793

1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807
	/* powerdown AFE                   */
	status = SetIqmAf(state, false);
	if (status < 0)
		goto error;

	/* powerdown to OFDM mode          */
	if (setPowerMode) {
		status = CtrlPowerMode(state, &powerMode);
		if (status < 0)
			goto error;
	}
error:
	if (status < 0)
		printk(KERN_ERR "drxk: Error %d on %s\n", status, __func__);
R
Ralph Metzler 已提交
1808 1809 1810
	return status;
}

1811 1812
static int SetOperationMode(struct drxk_state *state,
			    enum OperationMode oMode)
R
Ralph Metzler 已提交
1813 1814 1815
{
	int status = 0;

1816
	dprintk(1, "\n");
R
Ralph Metzler 已提交
1817
	/*
1818 1819 1820 1821
	   Stop and power down previous standard
	   TODO investigate total power down instead of partial
	   power down depending on "previous" standard.
	 */
R
Ralph Metzler 已提交
1822

1823 1824 1825 1826
	/* disable HW lock indicator */
	status = write16(state, SCU_RAM_GPIO__A, SCU_RAM_GPIO_HW_LOCK_IND_DISABLE);
	if (status < 0)
		goto error;
R
Ralph Metzler 已提交
1827

1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838
	/* Device is already at the required mode */
	if (state->m_OperationMode == oMode)
		return 0;

	switch (state->m_OperationMode) {
		/* OM_NONE was added for start up */
	case OM_NONE:
		break;
	case OM_DVBT:
		status = MPEGTSStop(state);
		if (status < 0)
1839
			goto error;
1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859
		status = PowerDownDVBT(state, true);
		if (status < 0)
			goto error;
		state->m_OperationMode = OM_NONE;
		break;
	case OM_QAM_ITU_A:	/* fallthrough */
	case OM_QAM_ITU_C:
		status = MPEGTSStop(state);
		if (status < 0)
			goto error;
		status = PowerDownQAM(state);
		if (status < 0)
			goto error;
		state->m_OperationMode = OM_NONE;
		break;
	case OM_QAM_ITU_B:
	default:
		status = -EINVAL;
		goto error;
	}
1860

1861 1862 1863 1864 1865
	/*
		Power up new standard
		*/
	switch (oMode) {
	case OM_DVBT:
1866
		dprintk(1, ": DVB-T\n");
1867 1868 1869 1870 1871 1872 1873
		state->m_OperationMode = oMode;
		status = SetDVBTStandard(state, oMode);
		if (status < 0)
			goto error;
		break;
	case OM_QAM_ITU_A:	/* fallthrough */
	case OM_QAM_ITU_C:
1874 1875
		dprintk(1, ": DVB-C Annex %c\n",
			(state->m_OperationMode == OM_QAM_ITU_A) ? 'A' : 'C');
1876 1877 1878 1879 1880 1881 1882 1883
		state->m_OperationMode = oMode;
		status = SetQAMStandard(state, oMode);
		if (status < 0)
			goto error;
		break;
	case OM_QAM_ITU_B:
	default:
		status = -EINVAL;
1884 1885 1886 1887 1888
	}
error:
	if (status < 0)
		printk(KERN_ERR "drxk: Error %d on %s\n", status, __func__);
	return status;
R
Ralph Metzler 已提交
1889 1890 1891 1892 1893
}

static int Start(struct drxk_state *state, s32 offsetFreq,
		 s32 IntermediateFrequency)
{
1894 1895 1896 1897
	int status = -EINVAL;

	u16 IFreqkHz;
	s32 OffsetkHz = offsetFreq / 1000;
R
Ralph Metzler 已提交
1898

1899
	dprintk(1, "\n");
1900 1901 1902
	if (state->m_DrxkState != DRXK_STOPPED &&
		state->m_DrxkState != DRXK_DTV_STARTED)
		goto error;
R
Ralph Metzler 已提交
1903

1904
	state->m_bMirrorFreqSpect = (state->props.inversion == INVERSION_ON);
R
Ralph Metzler 已提交
1905

1906 1907 1908 1909
	if (IntermediateFrequency < 0) {
		state->m_bMirrorFreqSpect = !state->m_bMirrorFreqSpect;
		IntermediateFrequency = -IntermediateFrequency;
	}
R
Ralph Metzler 已提交
1910

1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938
	switch (state->m_OperationMode) {
	case OM_QAM_ITU_A:
	case OM_QAM_ITU_C:
		IFreqkHz = (IntermediateFrequency / 1000);
		status = SetQAM(state, IFreqkHz, OffsetkHz);
		if (status < 0)
			goto error;
		state->m_DrxkState = DRXK_DTV_STARTED;
		break;
	case OM_DVBT:
		IFreqkHz = (IntermediateFrequency / 1000);
		status = MPEGTSStop(state);
		if (status < 0)
			goto error;
		status = SetDVBT(state, IFreqkHz, OffsetkHz);
		if (status < 0)
			goto error;
		status = DVBTStart(state);
		if (status < 0)
			goto error;
		state->m_DrxkState = DRXK_DTV_STARTED;
		break;
	default:
		break;
	}
error:
	if (status < 0)
		printk(KERN_ERR "drxk: Error %d on %s\n", status, __func__);
R
Ralph Metzler 已提交
1939 1940 1941 1942 1943
	return status;
}

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

R
Ralph Metzler 已提交
1946 1947 1948 1949
	MPEGTSStop(state);
	return 0;
}

1950 1951
static int GetLockStatus(struct drxk_state *state, u32 *pLockStatus,
			 u32 Time)
R
Ralph Metzler 已提交
1952
{
1953
	int status = -EINVAL;
R
Ralph Metzler 已提交
1954

1955 1956
	dprintk(1, "\n");

R
Ralph Metzler 已提交
1957
	if (pLockStatus == NULL)
1958
		goto error;
R
Ralph Metzler 已提交
1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974

	*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;
	}
1975 1976 1977
error:
	if (status < 0)
		printk(KERN_ERR "drxk: Error %d on %s\n", status, __func__);
R
Ralph Metzler 已提交
1978 1979 1980 1981 1982
	return status;
}

static int MPEGTSStart(struct drxk_state *state)
{
1983
	int status;
R
Ralph Metzler 已提交
1984 1985 1986

	u16 fecOcSncMode = 0;

1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998
	/* Allow OC to sync again */
	status = read16(state, FEC_OC_SNC_MODE__A, &fecOcSncMode);
	if (status < 0)
		goto error;
	fecOcSncMode &= ~FEC_OC_SNC_MODE_SHUTDOWN__M;
	status = write16(state, FEC_OC_SNC_MODE__A, fecOcSncMode);
	if (status < 0)
		goto error;
	status = write16(state, FEC_OC_SNC_UNLOCK__A, 1);
error:
	if (status < 0)
		printk(KERN_ERR "drxk: Error %d on %s\n", status, __func__);
R
Ralph Metzler 已提交
1999 2000 2001 2002 2003
	return status;
}

static int MPEGTSDtoInit(struct drxk_state *state)
{
2004
	int status;
R
Ralph Metzler 已提交
2005

2006 2007
	dprintk(1, "\n");

2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044
	/* Rate integration settings */
	status = write16(state, FEC_OC_RCN_CTL_STEP_LO__A, 0x0000);
	if (status < 0)
		goto error;
	status = write16(state, FEC_OC_RCN_CTL_STEP_HI__A, 0x000C);
	if (status < 0)
		goto error;
	status = write16(state, FEC_OC_RCN_GAIN__A, 0x000A);
	if (status < 0)
		goto error;
	status = write16(state, FEC_OC_AVR_PARM_A__A, 0x0008);
	if (status < 0)
		goto error;
	status = write16(state, FEC_OC_AVR_PARM_B__A, 0x0006);
	if (status < 0)
		goto error;
	status = write16(state, FEC_OC_TMD_HI_MARGIN__A, 0x0680);
	if (status < 0)
		goto error;
	status = write16(state, FEC_OC_TMD_LO_MARGIN__A, 0x0080);
	if (status < 0)
		goto error;
	status = write16(state, FEC_OC_TMD_COUNT__A, 0x03F4);
	if (status < 0)
		goto error;

	/* Additional configuration */
	status = write16(state, FEC_OC_OCR_INVERT__A, 0);
	if (status < 0)
		goto error;
	status = write16(state, FEC_OC_SNC_LWM__A, 2);
	if (status < 0)
		goto error;
	status = write16(state, FEC_OC_SNC_HWM__A, 12);
error:
	if (status < 0)
		printk(KERN_ERR "drxk: Error %d on %s\n", status, __func__);
R
Ralph Metzler 已提交
2045 2046 2047 2048

	return status;
}

2049 2050
static int MPEGTSDtoSetup(struct drxk_state *state,
			  enum OperationMode oMode)
R
Ralph Metzler 已提交
2051
{
2052
	int status;
R
Ralph Metzler 已提交
2053

2054 2055 2056 2057 2058 2059 2060
	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 已提交
2061 2062
	u16 fecOcTmdMode = 0;
	u16 fecOcTmdIntUpdRate = 0;
2063
	u32 maxBitRate = 0;
R
Ralph Metzler 已提交
2064 2065
	bool staticCLK = false;

2066 2067
	dprintk(1, "\n");

2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084
	/* Check insertion of the Reed-Solomon parity bytes */
	status = read16(state, FEC_OC_MODE__A, &fecOcRegMode);
	if (status < 0)
		goto error;
	status = read16(state, FEC_OC_IPR_MODE__A, &fecOcRegIprMode);
	if (status < 0)
		goto error;
	fecOcRegMode &= (~FEC_OC_MODE_PARITY__M);
	fecOcRegIprMode &= (~FEC_OC_IPR_MODE_MVAL_DIS_PAR__M);
	if (state->m_insertRSByte == true) {
		/* enable parity symbol forward */
		fecOcRegMode |= FEC_OC_MODE_PARITY__M;
		/* MVAL disable during parity bytes */
		fecOcRegIprMode |= FEC_OC_IPR_MODE_MVAL_DIS_PAR__M;
		/* TS burst length to 204 */
		fecOcDtoBurstLen = 204;
	}
R
Ralph Metzler 已提交
2085

2086 2087 2088 2089 2090 2091
	/* 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;
	}
R
Ralph Metzler 已提交
2092

2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129
	switch (oMode) {
	case OM_DVBT:
		maxBitRate = state->m_DVBTBitrate;
		fecOcTmdMode = 3;
		fecOcRcnCtlRate = 0xC00000;
		staticCLK = state->m_DVBTStaticCLK;
		break;
	case OM_QAM_ITU_A:	/* fallthrough */
	case OM_QAM_ITU_C:
		fecOcTmdMode = 0x0004;
		fecOcRcnCtlRate = 0xD2B4EE;	/* good for >63 Mb/s */
		maxBitRate = state->m_DVBCBitrate;
		staticCLK = state->m_DVBCStaticCLK;
		break;
	default:
		status = -EINVAL;
	}		/* switch (standard) */
	if (status < 0)
		goto error;

	/* Configure DTO's */
	if (staticCLK) {
		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;
		if (bitRate > 75900000UL) {	/* max is 75.9 Mb/s */
			bitRate = 75900000UL;
R
Ralph Metzler 已提交
2130
		}
2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149
		/* Rational DTO period:
			dto_period = (Fsys / bitrate) - 2

			Result should be floored,
			to make sure >= requested bitrate
			*/
		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;
	}
R
Ralph Metzler 已提交
2150

2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169
	/* Write appropriate registers with requested configuration */
	status = write16(state, FEC_OC_DTO_BURST_LEN__A, fecOcDtoBurstLen);
	if (status < 0)
		goto error;
	status = write16(state, FEC_OC_DTO_PERIOD__A, fecOcDtoPeriod);
	if (status < 0)
		goto error;
	status = write16(state, FEC_OC_DTO_MODE__A, fecOcDtoMode);
	if (status < 0)
		goto error;
	status = write16(state, FEC_OC_FCT_MODE__A, fecOcFctMode);
	if (status < 0)
		goto error;
	status = write16(state, FEC_OC_MODE__A, fecOcRegMode);
	if (status < 0)
		goto error;
	status = write16(state, FEC_OC_IPR_MODE__A, fecOcRegIprMode);
	if (status < 0)
		goto error;
R
Ralph Metzler 已提交
2170

2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181
	/* Rate integration settings */
	status = write32(state, FEC_OC_RCN_CTL_RATE_LO__A, fecOcRcnCtlRate);
	if (status < 0)
		goto error;
	status = write16(state, FEC_OC_TMD_INT_UPD_RATE__A, fecOcTmdIntUpdRate);
	if (status < 0)
		goto error;
	status = write16(state, FEC_OC_TMD_MODE__A, fecOcTmdMode);
error:
	if (status < 0)
		printk(KERN_ERR "drxk: Error %d on %s\n", status, __func__);
R
Ralph Metzler 已提交
2182 2183 2184 2185 2186
	return status;
}

static int MPEGTSConfigurePolarity(struct drxk_state *state)
{
2187
	u16 fecOcRegIprInvert = 0;
R
Ralph Metzler 已提交
2188 2189 2190

	/* Data mask for the output data byte */
	u16 InvertDataMask =
2191 2192 2193 2194
	    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 已提交
2195

2196 2197
	dprintk(1, "\n");

R
Ralph Metzler 已提交
2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213
	/* 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;
2214 2215

	return write16(state, FEC_OC_IPR_INVERT__A, fecOcRegIprInvert);
R
Ralph Metzler 已提交
2216 2217 2218 2219 2220 2221 2222
}

#define   SCU_RAM_AGC_KI_INV_RF_POL__M 0x4000

static int SetAgcRf(struct drxk_state *state,
		    struct SCfgAgc *pAgcCfg, bool isDTV)
{
2223 2224
	int status = -EINVAL;
	u16 data = 0;
R
Ralph Metzler 已提交
2225 2226
	struct SCfgAgc *pIfAgcSettings;

2227 2228
	dprintk(1, "\n");

R
Ralph Metzler 已提交
2229
	if (pAgcCfg == NULL)
2230
		goto error;
R
Ralph Metzler 已提交
2231

2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244
	switch (pAgcCfg->ctrlMode) {
	case DRXK_AGC_CTRL_AUTO:
		/* Enable RF AGC DAC */
		status = read16(state, IQM_AF_STDBY__A, &data);
		if (status < 0)
			goto error;
		data &= ~IQM_AF_STDBY_STDBY_TAGC_RF_STANDBY;
		status = write16(state, IQM_AF_STDBY__A, data);
		if (status < 0)
			goto error;
		status = read16(state, SCU_RAM_AGC_CONFIG__A, &data);
		if (status < 0)
			goto error;
R
Ralph Metzler 已提交
2245

2246 2247
		/* Enable SCU RF AGC loop */
		data &= ~SCU_RAM_AGC_CONFIG_DISABLE_RF_AGC__M;
R
Ralph Metzler 已提交
2248

2249 2250 2251 2252 2253 2254 2255 2256
		/* Polarity */
		if (state->m_RfAgcPol)
			data |= SCU_RAM_AGC_CONFIG_INV_RF_POL__M;
		else
			data &= ~SCU_RAM_AGC_CONFIG_INV_RF_POL__M;
		status = write16(state, SCU_RAM_AGC_CONFIG__A, data);
		if (status < 0)
			goto error;
R
Ralph Metzler 已提交
2257

2258 2259 2260 2261
		/* Set speed (using complementary reduction value) */
		status = read16(state, SCU_RAM_AGC_KI_RED__A, &data);
		if (status < 0)
			goto error;
R
Ralph Metzler 已提交
2262

2263 2264 2265 2266
		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);
R
Ralph Metzler 已提交
2267

2268 2269 2270
		status = write16(state, SCU_RAM_AGC_KI_RED__A, data);
		if (status < 0)
			goto error;
R
Ralph Metzler 已提交
2271

2272 2273 2274 2275 2276 2277 2278 2279 2280 2281
		if (IsDVBT(state))
			pIfAgcSettings = &state->m_dvbtIfAgcCfg;
		else if (IsQAM(state))
			pIfAgcSettings = &state->m_qamIfAgcCfg;
		else
			pIfAgcSettings = &state->m_atvIfAgcCfg;
		if (pIfAgcSettings == NULL) {
			status = -EINVAL;
			goto error;
		}
R
Ralph Metzler 已提交
2282

2283 2284 2285
		/* Set TOP, only if IF-AGC is in AUTO mode */
		if (pIfAgcSettings->ctrlMode == DRXK_AGC_CTRL_AUTO)
			status = write16(state, SCU_RAM_AGC_IF_IACCU_HI_TGT_MAX__A, pAgcCfg->top);
2286
			if (status < 0)
2287
				goto error;
R
Ralph Metzler 已提交
2288

2289 2290 2291 2292
		/* Cut-Off current */
		status = write16(state, SCU_RAM_AGC_RF_IACCU_HI_CO__A, pAgcCfg->cutOffCurrent);
		if (status < 0)
			goto error;
R
Ralph Metzler 已提交
2293

2294 2295 2296 2297
		/* Max. output level */
		status = write16(state, SCU_RAM_AGC_RF_MAX__A, pAgcCfg->maxOutputLevel);
		if (status < 0)
			goto error;
R
Ralph Metzler 已提交
2298

2299
		break;
R
Ralph Metzler 已提交
2300

2301 2302 2303 2304 2305 2306 2307 2308 2309
	case DRXK_AGC_CTRL_USER:
		/* Enable RF AGC DAC */
		status = read16(state, IQM_AF_STDBY__A, &data);
		if (status < 0)
			goto error;
		data &= ~IQM_AF_STDBY_STDBY_TAGC_RF_STANDBY;
		status = write16(state, IQM_AF_STDBY__A, data);
		if (status < 0)
			goto error;
R
Ralph Metzler 已提交
2310

2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322
		/* Disable SCU RF AGC loop */
		status = read16(state, SCU_RAM_AGC_CONFIG__A, &data);
		if (status < 0)
			goto error;
		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;
		status = write16(state, SCU_RAM_AGC_CONFIG__A, data);
		if (status < 0)
			goto error;
R
Ralph Metzler 已提交
2323

2324 2325 2326 2327
		/* SCU c.o.c. to 0, enabling full control range */
		status = write16(state, SCU_RAM_AGC_RF_IACCU_HI_CO__A, 0);
		if (status < 0)
			goto error;
R
Ralph Metzler 已提交
2328

2329 2330 2331 2332 2333
		/* Write value to output pin */
		status = write16(state, SCU_RAM_AGC_RF_IACCU_HI__A, pAgcCfg->outputLevel);
		if (status < 0)
			goto error;
		break;
R
Ralph Metzler 已提交
2334

2335 2336 2337 2338 2339 2340 2341 2342 2343
	case DRXK_AGC_CTRL_OFF:
		/* Disable RF AGC DAC */
		status = read16(state, IQM_AF_STDBY__A, &data);
		if (status < 0)
			goto error;
		data |= IQM_AF_STDBY_STDBY_TAGC_RF_STANDBY;
		status = write16(state, IQM_AF_STDBY__A, data);
		if (status < 0)
			goto error;
R
Ralph Metzler 已提交
2344

2345 2346 2347 2348 2349 2350 2351 2352 2353
		/* Disable SCU RF AGC loop */
		status = read16(state, SCU_RAM_AGC_CONFIG__A, &data);
		if (status < 0)
			goto error;
		data |= SCU_RAM_AGC_CONFIG_DISABLE_RF_AGC__M;
		status = write16(state, SCU_RAM_AGC_CONFIG__A, data);
		if (status < 0)
			goto error;
		break;
R
Ralph Metzler 已提交
2354

2355 2356
	default:
		status = -EINVAL;
R
Ralph Metzler 已提交
2357

2358 2359 2360 2361
	}
error:
	if (status < 0)
		printk(KERN_ERR "drxk: Error %d on %s\n", status, __func__);
R
Ralph Metzler 已提交
2362 2363 2364 2365 2366
	return status;
}

#define SCU_RAM_AGC_KI_INV_IF_POL__M 0x2000

2367 2368
static int SetAgcIf(struct drxk_state *state,
		    struct SCfgAgc *pAgcCfg, bool isDTV)
R
Ralph Metzler 已提交
2369 2370 2371 2372 2373
{
	u16 data = 0;
	int status = 0;
	struct SCfgAgc *pRfAgcSettings;

2374 2375
	dprintk(1, "\n");

2376 2377
	switch (pAgcCfg->ctrlMode) {
	case DRXK_AGC_CTRL_AUTO:
R
Ralph Metzler 已提交
2378

2379 2380 2381 2382 2383 2384 2385 2386
		/* Enable IF AGC DAC */
		status = read16(state, IQM_AF_STDBY__A, &data);
		if (status < 0)
			goto error;
		data &= ~IQM_AF_STDBY_STDBY_TAGC_IF_STANDBY;
		status = write16(state, IQM_AF_STDBY__A, data);
		if (status < 0)
			goto error;
R
Ralph Metzler 已提交
2387

2388 2389 2390
		status = read16(state, SCU_RAM_AGC_CONFIG__A, &data);
		if (status < 0)
			goto error;
R
Ralph Metzler 已提交
2391

2392 2393
		/* Enable SCU IF AGC loop */
		data &= ~SCU_RAM_AGC_CONFIG_DISABLE_IF_AGC__M;
R
Ralph Metzler 已提交
2394

2395 2396 2397 2398 2399 2400 2401 2402
		/* Polarity */
		if (state->m_IfAgcPol)
			data |= SCU_RAM_AGC_CONFIG_INV_IF_POL__M;
		else
			data &= ~SCU_RAM_AGC_CONFIG_INV_IF_POL__M;
		status = write16(state, SCU_RAM_AGC_CONFIG__A, data);
		if (status < 0)
			goto error;
R
Ralph Metzler 已提交
2403

2404 2405 2406 2407 2408 2409 2410 2411
		/* Set speed (using complementary reduction value) */
		status = read16(state, SCU_RAM_AGC_KI_RED__A, &data);
		if (status < 0)
			goto error;
		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);
R
Ralph Metzler 已提交
2412

2413 2414 2415
		status = write16(state, SCU_RAM_AGC_KI_RED__A, data);
		if (status < 0)
			goto error;
R
Ralph Metzler 已提交
2416

2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427
		if (IsQAM(state))
			pRfAgcSettings = &state->m_qamRfAgcCfg;
		else
			pRfAgcSettings = &state->m_atvRfAgcCfg;
		if (pRfAgcSettings == NULL)
			return -1;
		/* Restore TOP */
		status = write16(state, SCU_RAM_AGC_IF_IACCU_HI_TGT_MAX__A, pRfAgcSettings->top);
		if (status < 0)
			goto error;
		break;
R
Ralph Metzler 已提交
2428

2429
	case DRXK_AGC_CTRL_USER:
R
Ralph Metzler 已提交
2430

2431 2432 2433 2434 2435 2436 2437 2438
		/* Enable IF AGC DAC */
		status = read16(state, IQM_AF_STDBY__A, &data);
		if (status < 0)
			goto error;
		data &= ~IQM_AF_STDBY_STDBY_TAGC_IF_STANDBY;
		status = write16(state, IQM_AF_STDBY__A, data);
		if (status < 0)
			goto error;
R
Ralph Metzler 已提交
2439

2440 2441 2442
		status = read16(state, SCU_RAM_AGC_CONFIG__A, &data);
		if (status < 0)
			goto error;
R
Ralph Metzler 已提交
2443

2444 2445
		/* Disable SCU IF AGC loop */
		data |= SCU_RAM_AGC_CONFIG_DISABLE_IF_AGC__M;
R
Ralph Metzler 已提交
2446

2447 2448 2449 2450 2451 2452 2453 2454
		/* Polarity */
		if (state->m_IfAgcPol)
			data |= SCU_RAM_AGC_CONFIG_INV_IF_POL__M;
		else
			data &= ~SCU_RAM_AGC_CONFIG_INV_IF_POL__M;
		status = write16(state, SCU_RAM_AGC_CONFIG__A, data);
		if (status < 0)
			goto error;
R
Ralph Metzler 已提交
2455

2456 2457 2458 2459 2460
		/* Write value to output pin */
		status = write16(state, SCU_RAM_AGC_IF_IACCU_HI_TGT_MAX__A, pAgcCfg->outputLevel);
		if (status < 0)
			goto error;
		break;
R
Ralph Metzler 已提交
2461

2462
	case DRXK_AGC_CTRL_OFF:
R
Ralph Metzler 已提交
2463

2464 2465
		/* Disable If AGC DAC */
		status = read16(state, IQM_AF_STDBY__A, &data);
2466
		if (status < 0)
2467 2468 2469 2470 2471
			goto error;
		data |= IQM_AF_STDBY_STDBY_TAGC_IF_STANDBY;
		status = write16(state, IQM_AF_STDBY__A, data);
		if (status < 0)
			goto error;
R
Ralph Metzler 已提交
2472

2473 2474 2475 2476 2477 2478 2479 2480 2481 2482
		/* Disable SCU IF AGC loop */
		status = read16(state, SCU_RAM_AGC_CONFIG__A, &data);
		if (status < 0)
			goto error;
		data |= SCU_RAM_AGC_CONFIG_DISABLE_IF_AGC__M;
		status = write16(state, SCU_RAM_AGC_CONFIG__A, data);
		if (status < 0)
			goto error;
		break;
	}		/* switch (agcSettingsIf->ctrlMode) */
R
Ralph Metzler 已提交
2483

2484 2485 2486 2487 2488 2489
	/* always set the top to support
		configurations without if-loop */
	status = write16(state, SCU_RAM_AGC_INGAIN_TGT_MIN__A, pAgcCfg->top);
error:
	if (status < 0)
		printk(KERN_ERR "drxk: Error %d on %s\n", status, __func__);
R
Ralph Metzler 已提交
2490 2491 2492 2493 2494 2495
	return status;
}

static int ReadIFAgc(struct drxk_state *state, u32 *pValue)
{
	u16 agcDacLvl;
2496 2497
	int status;
	u16 Level = 0;
R
Ralph Metzler 已提交
2498

2499 2500
	dprintk(1, "\n");

2501 2502 2503 2504 2505 2506
	status = read16(state, IQM_AF_AGC_IF__A, &agcDacLvl);
	if (status < 0) {
		printk(KERN_ERR "drxk: Error %d on %s\n", status, __func__);
		return status;
	}

R
Ralph Metzler 已提交
2507 2508
	*pValue = 0;

2509 2510 2511 2512 2513 2514 2515
	if (agcDacLvl > DRXK_AGC_DAC_OFFSET)
		Level = agcDacLvl - DRXK_AGC_DAC_OFFSET;
	if (Level < 14000)
		*pValue = (14000 - Level) / 4;
	else
		*pValue = 0;

R
Ralph Metzler 已提交
2516 2517 2518
	return status;
}

2519 2520
static int GetQAMSignalToNoise(struct drxk_state *state,
			       s32 *pSignalToNoise)
R
Ralph Metzler 已提交
2521 2522
{
	int status = 0;
2523 2524 2525
	u16 qamSlErrPower = 0;	/* accum. error between
					raw and sliced symbols */
	u32 qamSlSigPower = 0;	/* used for MER, depends of
2526
					QAM modulation */
2527
	u32 qamSlMer = 0;	/* QAM MER */
R
Ralph Metzler 已提交
2528

2529 2530
	dprintk(1, "\n");

2531
	/* MER calculation */
R
Ralph Metzler 已提交
2532

2533 2534 2535 2536 2537 2538 2539
	/* get the register value needed for MER */
	status = read16(state, QAM_SL_ERR_POWER__A, &qamSlErrPower);
	if (status < 0) {
		printk(KERN_ERR "drxk: Error %d on %s\n", status, __func__);
		return -EINVAL;
	}

2540
	switch (state->props.modulation) {
2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563
	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) {
		qamSlMer = Log10Times100(qamSlSigPower) -
			Log10Times100((u32) qamSlErrPower);
	}
	*pSignalToNoise = qamSlMer;
R
Ralph Metzler 已提交
2564 2565 2566 2567

	return status;
}

2568 2569
static int GetDVBTSignalToNoise(struct drxk_state *state,
				s32 *pSignalToNoise)
R
Ralph Metzler 已提交
2570
{
2571
	int status;
2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583
	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 已提交
2584 2585
	u16 transmissionParams = 0;

2586
	dprintk(1, "\n");
R
Ralph Metzler 已提交
2587

2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659
	status = read16(state, OFDM_EQ_TOP_TD_TPS_PWR_OFS__A, &EqRegTdTpsPwrOfs);
	if (status < 0)
		goto error;
	status = read16(state, OFDM_EQ_TOP_TD_REQ_SMB_CNT__A, &EqRegTdReqSmbCnt);
	if (status < 0)
		goto error;
	status = read16(state, OFDM_EQ_TOP_TD_SQR_ERR_EXP__A, &EqRegTdSqrErrExp);
	if (status < 0)
		goto error;
	status = read16(state, OFDM_EQ_TOP_TD_SQR_ERR_I__A, &regData);
	if (status < 0)
		goto error;
	/* Extend SQR_ERR_I operational range */
	EqRegTdSqrErrI = (u32) regData;
	if ((EqRegTdSqrErrExp > 11) &&
		(EqRegTdSqrErrI < 0x00000FFFUL)) {
		EqRegTdSqrErrI += 0x00010000UL;
	}
	status = read16(state, OFDM_EQ_TOP_TD_SQR_ERR_Q__A, &regData);
	if (status < 0)
		goto error;
	/* Extend SQR_ERR_Q operational range */
	EqRegTdSqrErrQ = (u32) regData;
	if ((EqRegTdSqrErrExp > 11) &&
		(EqRegTdSqrErrQ < 0x00000FFFUL))
		EqRegTdSqrErrQ += 0x00010000UL;

	status = read16(state, OFDM_SC_RA_RAM_OP_PARAM__A, &transmissionParams);
	if (status < 0)
		goto error;

	/* 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) <<
			EqRegTdSqrErrExp;
		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)
			*/

		/* log(x) x = 9bits * 9bits->18 bits  */
		a = Log10Times100(EqRegTdTpsPwrOfs *
					EqRegTdTpsPwrOfs);
		/* log(x) x = 16bits * 7bits->23 bits  */
		b = Log10Times100(EqRegTdReqSmbCnt * tpsCnt);
		/* log(x) x = (16bits + 16bits) << 15 ->32 bits  */
		c = Log10Times100(SqrErrIQ);

		iMER = a + b;
	}
	*pSignalToNoise = iMER;
R
Ralph Metzler 已提交
2660

2661 2662 2663
error:
	if (status < 0)
		printk(KERN_ERR "drxk: Error %d on %s\n", status, __func__);
R
Ralph Metzler 已提交
2664 2665 2666 2667 2668
	return status;
}

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

R
Ralph Metzler 已提交
2671
	*pSignalToNoise = 0;
2672
	switch (state->m_OperationMode) {
R
Ralph Metzler 已提交
2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689
	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;

2690 2691
	dprintk(1, "\n");

2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708
	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 已提交
2709 2710 2711 2712 2713 2714 2715 2716 2717 2718

	*pQuality = 0;

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

2719 2720 2721
		status = GetDVBTSignalToNoise(state, &SignalToNoise);
		if (status < 0)
			break;
2722
		status = read16(state, OFDM_EQ_TOP_TD_TPS_CONST__A, &Constellation);
2723 2724
		if (status < 0)
			break;
R
Ralph Metzler 已提交
2725 2726
		Constellation &= OFDM_EQ_TOP_TD_TPS_CONST__M;

2727
		status = read16(state, OFDM_EQ_TOP_TD_TPS_CODE_HP__A, &CodeRate);
2728 2729
		if (status < 0)
			break;
R
Ralph Metzler 已提交
2730 2731 2732 2733 2734 2735
		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 -
2736
		    QE_SN[Constellation * 5 + CodeRate];
R
Ralph Metzler 已提交
2737 2738
		BERQuality = 100;

2739 2740
		if (SignalToNoiseRel < -70)
			*pQuality = 0;
R
Ralph Metzler 已提交
2741 2742 2743 2744 2745
		else if (SignalToNoiseRel < 30)
			*pQuality = ((SignalToNoiseRel + 70) *
				     BERQuality) / 100;
		else
			*pQuality = BERQuality;
2746
	} while (0);
R
Ralph Metzler 已提交
2747 2748 2749
	return 0;
};

2750
static int GetDVBCQuality(struct drxk_state *state, s32 *pQuality)
R
Ralph Metzler 已提交
2751 2752 2753 2754
{
	int status = 0;
	*pQuality = 0;

2755 2756
	dprintk(1, "\n");

R
Ralph Metzler 已提交
2757 2758 2759 2760 2761
	do {
		u32 SignalToNoise = 0;
		u32 BERQuality = 100;
		u32 SignalToNoiseRel = 0;

2762 2763 2764
		status = GetQAMSignalToNoise(state, &SignalToNoise);
		if (status < 0)
			break;
R
Ralph Metzler 已提交
2765

2766
		switch (state->props.modulation) {
R
Ralph Metzler 已提交
2767 2768 2769 2770 2771
		case QAM_16:
			SignalToNoiseRel = SignalToNoise - 200;
			break;
		case QAM_32:
			SignalToNoiseRel = SignalToNoise - 230;
2772
			break;	/* Not in NorDig */
R
Ralph Metzler 已提交
2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791
		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;
2792
	} while (0);
R
Ralph Metzler 已提交
2793 2794 2795 2796 2797 2798

	return status;
}

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

2801 2802
	switch (state->m_OperationMode) {
	case OM_DVBT:
R
Ralph Metzler 已提交
2803
		return GetDVBTQuality(state, pQuality);
2804
	case OM_QAM_ITU_A:
R
Ralph Metzler 已提交
2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828
		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)
{
2829
	int status = -EINVAL;
R
Ralph Metzler 已提交
2830

2831 2832
	dprintk(1, "\n");

R
Ralph Metzler 已提交
2833
	if (state->m_DrxkState == DRXK_UNINITIALIZED)
2834
		return 0;
R
Ralph Metzler 已提交
2835
	if (state->m_DrxkState == DRXK_POWERED_DOWN)
2836
		goto error;
R
Ralph Metzler 已提交
2837

2838 2839
	if (state->no_i2c_bridge)
		return 0;
R
Ralph Metzler 已提交
2840

2841 2842 2843 2844 2845
	status = write16(state, SIO_HI_RA_RAM_PAR_1__A, SIO_HI_RA_RAM_PAR_1_PAR1_SEC_KEY);
	if (status < 0)
		goto error;
	if (bEnableBridge) {
		status = write16(state, SIO_HI_RA_RAM_PAR_2__A, SIO_HI_RA_RAM_PAR_2_BRD_CFG_CLOSED);
2846
		if (status < 0)
2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858
			goto error;
	} else {
		status = write16(state, SIO_HI_RA_RAM_PAR_2__A, SIO_HI_RA_RAM_PAR_2_BRD_CFG_OPEN);
		if (status < 0)
			goto error;
	}

	status = HI_Command(state, SIO_HI_RA_RAM_CMD_BRDCTRL, 0);

error:
	if (status < 0)
		printk(KERN_ERR "drxk: Error %d on %s\n", status, __func__);
R
Ralph Metzler 已提交
2859 2860 2861
	return status;
}

2862 2863
static int SetPreSaw(struct drxk_state *state,
		     struct SCfgPreSaw *pPreSawCfg)
R
Ralph Metzler 已提交
2864
{
2865
	int status = -EINVAL;
R
Ralph Metzler 已提交
2866

2867 2868
	dprintk(1, "\n");

2869 2870
	if ((pPreSawCfg == NULL)
	    || (pPreSawCfg->reference > IQM_AF_PDREF__M))
2871
		goto error;
R
Ralph Metzler 已提交
2872

2873
	status = write16(state, IQM_AF_PDREF__A, pPreSawCfg->reference);
2874 2875 2876
error:
	if (status < 0)
		printk(KERN_ERR "drxk: Error %d on %s\n", status, __func__);
R
Ralph Metzler 已提交
2877 2878 2879 2880
	return status;
}

static int BLDirectCmd(struct drxk_state *state, u32 targetAddr,
2881
		       u16 romOffset, u16 nrOfElements, u32 timeOut)
R
Ralph Metzler 已提交
2882
{
2883 2884 2885 2886
	u16 blStatus = 0;
	u16 offset = (u16) ((targetAddr >> 0) & 0x00FFFF);
	u16 blockbank = (u16) ((targetAddr >> 16) & 0x000FFF);
	int status;
R
Ralph Metzler 已提交
2887 2888
	unsigned long end;

2889 2890
	dprintk(1, "\n");

R
Ralph Metzler 已提交
2891
	mutex_lock(&state->mutex);
2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911
	status = write16(state, SIO_BL_MODE__A, SIO_BL_MODE_DIRECT);
	if (status < 0)
		goto error;
	status = write16(state, SIO_BL_TGT_HDR__A, blockbank);
	if (status < 0)
		goto error;
	status = write16(state, SIO_BL_TGT_ADDR__A, offset);
	if (status < 0)
		goto error;
	status = write16(state, SIO_BL_SRC_ADDR__A, romOffset);
	if (status < 0)
		goto error;
	status = write16(state, SIO_BL_SRC_LEN__A, nrOfElements);
	if (status < 0)
		goto error;
	status = write16(state, SIO_BL_ENABLE__A, SIO_BL_ENABLE_ON);
	if (status < 0)
		goto error;

	end = jiffies + msecs_to_jiffies(timeOut);
R
Ralph Metzler 已提交
2912
	do {
2913
		status = read16(state, SIO_BL_STATUS__A, &blStatus);
2914
		if (status < 0)
2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925
			goto error;
	} while ((blStatus == 0x1) && time_is_after_jiffies(end));
	if (blStatus == 0x1) {
		printk(KERN_ERR "drxk: SIO not ready\n");
		status = -EINVAL;
		goto error2;
	}
error:
	if (status < 0)
		printk(KERN_ERR "drxk: Error %d on %s\n", status, __func__);
error2:
R
Ralph Metzler 已提交
2926 2927 2928 2929 2930
	mutex_unlock(&state->mutex);
	return status;

}

2931
static int ADCSyncMeasurement(struct drxk_state *state, u16 *count)
R
Ralph Metzler 已提交
2932 2933 2934 2935
{
	u16 data = 0;
	int status;

2936 2937
	dprintk(1, "\n");

2938 2939 2940 2941 2942 2943 2944
	/* Start measurement */
	status = write16(state, IQM_AF_COMM_EXEC__A, IQM_AF_COMM_EXEC_ACTIVE);
	if (status < 0)
		goto error;
	status = write16(state, IQM_AF_START_LOCK__A, 1);
	if (status < 0)
		goto error;
R
Ralph Metzler 已提交
2945

2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965
	*count = 0;
	status = read16(state, IQM_AF_PHASE0__A, &data);
	if (status < 0)
		goto error;
	if (data == 127)
		*count = *count + 1;
	status = read16(state, IQM_AF_PHASE1__A, &data);
	if (status < 0)
		goto error;
	if (data == 127)
		*count = *count + 1;
	status = read16(state, IQM_AF_PHASE2__A, &data);
	if (status < 0)
		goto error;
	if (data == 127)
		*count = *count + 1;

error:
	if (status < 0)
		printk(KERN_ERR "drxk: Error %d on %s\n", status, __func__);
R
Ralph Metzler 已提交
2966 2967 2968 2969 2970 2971 2972 2973
	return status;
}

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

2974 2975
	dprintk(1, "\n");

2976 2977 2978
	status = ADCSyncMeasurement(state, &count);
	if (status < 0)
		goto error;
R
Ralph Metzler 已提交
2979

2980 2981 2982
	if (count == 1) {
		/* Try sampling on a diffrent edge */
		u16 clkNeg = 0;
R
Ralph Metzler 已提交
2983

2984 2985 2986
		status = read16(state, IQM_AF_CLKNEG__A, &clkNeg);
		if (status < 0)
			goto error;
2987
		if ((clkNeg & IQM_AF_CLKNEG_CLKNEGDATA__M) ==
2988 2989 2990 2991 2992 2993 2994 2995
			IQM_AF_CLKNEG_CLKNEGDATA_CLK_ADC_DATA_POS) {
			clkNeg &= (~(IQM_AF_CLKNEG_CLKNEGDATA__M));
			clkNeg |=
				IQM_AF_CLKNEG_CLKNEGDATA_CLK_ADC_DATA_NEG;
		} else {
			clkNeg &= (~(IQM_AF_CLKNEG_CLKNEGDATA__M));
			clkNeg |=
				IQM_AF_CLKNEG_CLKNEGDATA_CLK_ADC_DATA_POS;
R
Ralph Metzler 已提交
2996
		}
2997 2998 2999 3000 3001 3002 3003
		status = write16(state, IQM_AF_CLKNEG__A, clkNeg);
		if (status < 0)
			goto error;
		status = ADCSyncMeasurement(state, &count);
		if (status < 0)
			goto error;
	}
R
Ralph Metzler 已提交
3004

3005 3006 3007 3008 3009
	if (count < 2)
		status = -EINVAL;
error:
	if (status < 0)
		printk(KERN_ERR "drxk: Error %d on %s\n", status, __func__);
R
Ralph Metzler 已提交
3010 3011 3012 3013 3014
	return status;
}

static int SetFrequencyShifter(struct drxk_state *state,
			       u16 intermediateFreqkHz,
3015
			       s32 tunerFreqOffset, bool isDTV)
R
Ralph Metzler 已提交
3016 3017
{
	bool selectPosImage = false;
3018
	u32 rfFreqResidual = tunerFreqOffset;
R
Ralph Metzler 已提交
3019 3020 3021 3022 3023 3024
	u32 fmFrequencyShift = 0;
	bool tunerMirror = !state->m_bMirrorFreqSpect;
	u32 adcFreq;
	bool adcFlip;
	int status;
	u32 ifFreqActual;
3025
	u32 samplingFrequency = (u32) (state->m_sysClockFreq / 3);
R
Ralph Metzler 已提交
3026 3027 3028
	u32 frequencyShift;
	bool imageToSelect;

3029 3030
	dprintk(1, "\n");

R
Ralph Metzler 已提交
3031
	/*
3032 3033 3034
	   Program frequency shifter
	   No need to account for mirroring on RF
	 */
R
Ralph Metzler 已提交
3035 3036 3037 3038
	if (isDTV) {
		if ((state->m_OperationMode == OM_QAM_ITU_A) ||
		    (state->m_OperationMode == OM_QAM_ITU_C) ||
		    (state->m_OperationMode == OM_DVBT))
3039 3040 3041
			selectPosImage = true;
		else
			selectPosImage = false;
R
Ralph Metzler 已提交
3042 3043 3044 3045
	}
	if (tunerMirror)
		/* tuner doesn't mirror */
		ifFreqActual = intermediateFreqkHz +
3046
		    rfFreqResidual + fmFrequencyShift;
R
Ralph Metzler 已提交
3047 3048 3049
	else
		/* tuner mirrors */
		ifFreqActual = intermediateFreqkHz -
3050
		    rfFreqResidual - fmFrequencyShift;
R
Ralph Metzler 已提交
3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062
	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 ^
3063 3064 3065
	    adcFlip ^ selectPosImage;
	state->m_IqmFsRateOfs =
	    Frac28a((frequencyShift), samplingFrequency);
R
Ralph Metzler 已提交
3066 3067 3068 3069 3070 3071

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

	/* Program frequency shifter with tuner offset compensation */
	/* frequencyShift += tunerFreqOffset; TODO */
3072 3073
	status = write32(state, IQM_FS_RATE_OFS_LO__A,
			 state->m_IqmFsRateOfs);
3074 3075
	if (status < 0)
		printk(KERN_ERR "drxk: Error %d on %s\n", status, __func__);
R
Ralph Metzler 已提交
3076 3077 3078 3079 3080
	return status;
}

static int InitAGC(struct drxk_state *state, bool isDTV)
{
3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092
	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 已提交
3093 3094
	u16 ifIaccuHiTgtMin = 0;
	u16 ifIaccuHiTgtMax = 0;
3095 3096 3097
	u16 data = 0;
	u16 fastClpCtrlDelay = 0;
	u16 clpCtrlMode = 0;
R
Ralph Metzler 已提交
3098 3099
	int status = 0;

3100 3101
	dprintk(1, "\n");

3102 3103 3104 3105 3106 3107
	/* Common settings */
	snsSumMax = 1023;
	ifIaccuHiTgtMin = 2047;
	clpCyclen = 500;
	clpSumMax = 1023;

3108 3109 3110 3111
	/* AGCInit() not available for DVBT; init done in microcode */
	if (!IsQAM(state)) {
		printk(KERN_ERR "drxk: %s: mode %d is not DVB-C\n", __func__, state->m_OperationMode);
		return -EINVAL;
3112
	}
3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129

	/* FIXME: Analog TV AGC require different settings */

	/* Standard specific settings */
	clpSumMin = 8;
	clpDirTo = (u16) -9;
	clpCtrlMode = 0;
	snsSumMin = 8;
	snsDirTo = (u16) -9;
	kiInnergainMin = (u16) -1030;
	ifIaccuHiTgtMax = 0x2380;
	ifIaccuHiTgt = 0x2380;
	ingainTgtMin = 0x0511;
	ingainTgt = 0x0511;
	ingainTgtMax = 5119;
	fastClpCtrlDelay = state->m_qamIfAgcCfg.FastClipCtrlDelay;

3130 3131 3132
	status = write16(state, SCU_RAM_AGC_FAST_CLP_CTRL_DELAY__A, fastClpCtrlDelay);
	if (status < 0)
		goto error;
3133

3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169
	status = write16(state, SCU_RAM_AGC_CLP_CTRL_MODE__A, clpCtrlMode);
	if (status < 0)
		goto error;
	status = write16(state, SCU_RAM_AGC_INGAIN_TGT__A, ingainTgt);
	if (status < 0)
		goto error;
	status = write16(state, SCU_RAM_AGC_INGAIN_TGT_MIN__A, ingainTgtMin);
	if (status < 0)
		goto error;
	status = write16(state, SCU_RAM_AGC_INGAIN_TGT_MAX__A, ingainTgtMax);
	if (status < 0)
		goto error;
	status = write16(state, SCU_RAM_AGC_IF_IACCU_HI_TGT_MIN__A, ifIaccuHiTgtMin);
	if (status < 0)
		goto error;
	status = write16(state, SCU_RAM_AGC_IF_IACCU_HI_TGT_MAX__A, ifIaccuHiTgtMax);
	if (status < 0)
		goto error;
	status = write16(state, SCU_RAM_AGC_IF_IACCU_HI__A, 0);
	if (status < 0)
		goto error;
	status = write16(state, SCU_RAM_AGC_IF_IACCU_LO__A, 0);
	if (status < 0)
		goto error;
	status = write16(state, SCU_RAM_AGC_RF_IACCU_HI__A, 0);
	if (status < 0)
		goto error;
	status = write16(state, SCU_RAM_AGC_RF_IACCU_LO__A, 0);
	if (status < 0)
		goto error;
	status = write16(state, SCU_RAM_AGC_CLP_SUM_MAX__A, clpSumMax);
	if (status < 0)
		goto error;
	status = write16(state, SCU_RAM_AGC_SNS_SUM_MAX__A, snsSumMax);
	if (status < 0)
		goto error;
3170

3171 3172 3173 3174 3175 3176 3177 3178 3179
	status = write16(state, SCU_RAM_AGC_KI_INNERGAIN_MIN__A, kiInnergainMin);
	if (status < 0)
		goto error;
	status = write16(state, SCU_RAM_AGC_IF_IACCU_HI_TGT__A, ifIaccuHiTgt);
	if (status < 0)
		goto error;
	status = write16(state, SCU_RAM_AGC_CLP_CYCLEN__A, clpCyclen);
	if (status < 0)
		goto error;
3180

3181 3182 3183 3184 3185 3186 3187 3188 3189
	status = write16(state, SCU_RAM_AGC_RF_SNS_DEV_MAX__A, 1023);
	if (status < 0)
		goto error;
	status = write16(state, SCU_RAM_AGC_RF_SNS_DEV_MIN__A, (u16) -1023);
	if (status < 0)
		goto error;
	status = write16(state, SCU_RAM_AGC_FAST_SNS_CTRL_DELAY__A, 50);
	if (status < 0)
		goto error;
3190

3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247
	status = write16(state, SCU_RAM_AGC_KI_MAXMINGAIN_TH__A, 20);
	if (status < 0)
		goto error;
	status = write16(state, SCU_RAM_AGC_CLP_SUM_MIN__A, clpSumMin);
	if (status < 0)
		goto error;
	status = write16(state, SCU_RAM_AGC_SNS_SUM_MIN__A, snsSumMin);
	if (status < 0)
		goto error;
	status = write16(state, SCU_RAM_AGC_CLP_DIR_TO__A, clpDirTo);
	if (status < 0)
		goto error;
	status = write16(state, SCU_RAM_AGC_SNS_DIR_TO__A, snsDirTo);
	if (status < 0)
		goto error;
	status = write16(state, SCU_RAM_AGC_KI_MINGAIN__A, 0x7fff);
	if (status < 0)
		goto error;
	status = write16(state, SCU_RAM_AGC_KI_MAXGAIN__A, 0x0);
	if (status < 0)
		goto error;
	status = write16(state, SCU_RAM_AGC_KI_MIN__A, 0x0117);
	if (status < 0)
		goto error;
	status = write16(state, SCU_RAM_AGC_KI_MAX__A, 0x0657);
	if (status < 0)
		goto error;
	status = write16(state, SCU_RAM_AGC_CLP_SUM__A, 0);
	if (status < 0)
		goto error;
	status = write16(state, SCU_RAM_AGC_CLP_CYCCNT__A, 0);
	if (status < 0)
		goto error;
	status = write16(state, SCU_RAM_AGC_CLP_DIR_WD__A, 0);
	if (status < 0)
		goto error;
	status = write16(state, SCU_RAM_AGC_CLP_DIR_STP__A, 1);
	if (status < 0)
		goto error;
	status = write16(state, SCU_RAM_AGC_SNS_SUM__A, 0);
	if (status < 0)
		goto error;
	status = write16(state, SCU_RAM_AGC_SNS_CYCCNT__A, 0);
	if (status < 0)
		goto error;
	status = write16(state, SCU_RAM_AGC_SNS_DIR_WD__A, 0);
	if (status < 0)
		goto error;
	status = write16(state, SCU_RAM_AGC_SNS_DIR_STP__A, 1);
	if (status < 0)
		goto error;
	status = write16(state, SCU_RAM_AGC_SNS_CYCLEN__A, 500);
	if (status < 0)
		goto error;
	status = write16(state, SCU_RAM_AGC_KI_CYCLEN__A, 500);
	if (status < 0)
		goto error;
R
Ralph Metzler 已提交
3248

3249 3250 3251 3252
	/* Initialize inner-loop KI gain factors */
	status = read16(state, SCU_RAM_AGC_KI__A, &data);
	if (status < 0)
		goto error;
3253 3254 3255 3256 3257 3258 3259

	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);

3260 3261 3262 3263
	status = write16(state, SCU_RAM_AGC_KI__A, data);
error:
	if (status < 0)
		printk(KERN_ERR "drxk: Error %d on %s\n", status, __func__);
R
Ralph Metzler 已提交
3264 3265 3266
	return status;
}

3267
static int DVBTQAMGetAccPktErr(struct drxk_state *state, u16 *packetErr)
R
Ralph Metzler 已提交
3268 3269 3270
{
	int status;

3271
	dprintk(1, "\n");
3272 3273 3274 3275 3276 3277
	if (packetErr == NULL)
		status = write16(state, SCU_RAM_FEC_ACCUM_PKT_FAILURES__A, 0);
	else
		status = read16(state, SCU_RAM_FEC_ACCUM_PKT_FAILURES__A, packetErr);
	if (status < 0)
		printk(KERN_ERR "drxk: Error %d on %s\n", status, __func__);
R
Ralph Metzler 已提交
3278 3279 3280 3281 3282 3283 3284 3285
	return status;
}

static int DVBTScCommand(struct drxk_state *state,
			 u16 cmd, u16 subcmd,
			 u16 param0, u16 param1, u16 param2,
			 u16 param3, u16 param4)
{
3286 3287
	u16 curCmd = 0;
	u16 errCode = 0;
R
Ralph Metzler 已提交
3288
	u16 retryCnt = 0;
3289 3290
	u16 scExec = 0;
	int status;
R
Ralph Metzler 已提交
3291

3292
	dprintk(1, "\n");
3293
	status = read16(state, OFDM_SC_COMM_EXEC__A, &scExec);
R
Ralph Metzler 已提交
3294 3295
	if (scExec != 1) {
		/* SC is not running */
3296
		status = -EINVAL;
R
Ralph Metzler 已提交
3297
	}
3298 3299
	if (status < 0)
		goto error;
R
Ralph Metzler 已提交
3300 3301

	/* Wait until sc is ready to receive command */
3302
	retryCnt = 0;
R
Ralph Metzler 已提交
3303 3304
	do {
		msleep(1);
3305
		status = read16(state, OFDM_SC_RA_RAM_CMD__A, &curCmd);
R
Ralph Metzler 已提交
3306 3307
		retryCnt++;
	} while ((curCmd != 0) && (retryCnt < DRXK_MAX_RETRIES));
3308 3309 3310
	if (retryCnt >= DRXK_MAX_RETRIES && (status < 0))
		goto error;

R
Ralph Metzler 已提交
3311 3312 3313 3314 3315 3316
	/* 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:
3317 3318 3319
		status = write16(state, OFDM_SC_RA_RAM_CMD_ADDR__A, subcmd);
		if (status < 0)
			goto error;
R
Ralph Metzler 已提交
3320 3321 3322 3323
		break;
	default:
		/* Do nothing */
		break;
3324
	}
R
Ralph Metzler 已提交
3325 3326 3327 3328 3329 3330 3331 3332 3333 3334

	/* 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:
3335
		status = write16(state, OFDM_SC_RA_RAM_PARAM1__A, param1);
R
Ralph Metzler 已提交
3336 3337 3338
		/* All commands using 1 parameters */
	case OFDM_SC_RA_RAM_CMD_SET_ECHO_TIMING:
	case OFDM_SC_RA_RAM_CMD_USER_IO:
3339
		status = write16(state, OFDM_SC_RA_RAM_PARAM0__A, param0);
R
Ralph Metzler 已提交
3340 3341 3342 3343
		/* All commands using 0 parameters */
	case OFDM_SC_RA_RAM_CMD_GET_OP_PARAM:
	case OFDM_SC_RA_RAM_CMD_NULL:
		/* Write command */
3344
		status = write16(state, OFDM_SC_RA_RAM_CMD__A, cmd);
R
Ralph Metzler 已提交
3345 3346 3347
		break;
	default:
		/* Unknown command */
3348 3349 3350 3351
		status = -EINVAL;
	}
	if (status < 0)
		goto error;
R
Ralph Metzler 已提交
3352 3353 3354

	/* Wait until sc is ready processing command */
	retryCnt = 0;
3355
	do {
R
Ralph Metzler 已提交
3356
		msleep(1);
3357
		status = read16(state, OFDM_SC_RA_RAM_CMD__A, &curCmd);
R
Ralph Metzler 已提交
3358
		retryCnt++;
3359
	} while ((curCmd != 0) && (retryCnt < DRXK_MAX_RETRIES));
3360 3361
	if (retryCnt >= DRXK_MAX_RETRIES && (status < 0))
		goto error;
R
Ralph Metzler 已提交
3362 3363

	/* Check for illegal cmd */
3364
	status = read16(state, OFDM_SC_RA_RAM_CMD_ADDR__A, &errCode);
3365
	if (errCode == 0xFFFF) {
R
Ralph Metzler 已提交
3366
		/* illegal command */
3367
		status = -EINVAL;
R
Ralph Metzler 已提交
3368
	}
3369 3370
	if (status < 0)
		goto error;
R
Ralph Metzler 已提交
3371 3372 3373 3374 3375 3376 3377 3378 3379 3380

	/* 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:
3381
		status = read16(state, OFDM_SC_RA_RAM_PARAM0__A, &(param0));
R
Ralph Metzler 已提交
3382 3383 3384 3385 3386 3387 3388 3389 3390 3391
		/* 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 */
3392
		status = -EINVAL;
R
Ralph Metzler 已提交
3393
		break;
3394
	}			/* switch (cmd->cmd) */
3395 3396 3397
error:
	if (status < 0)
		printk(KERN_ERR "drxk: Error %d on %s\n", status, __func__);
R
Ralph Metzler 已提交
3398 3399 3400
	return status;
}

3401
static int PowerUpDVBT(struct drxk_state *state)
R
Ralph Metzler 已提交
3402
{
3403
	enum DRXPowerMode powerMode = DRX_POWER_UP;
R
Ralph Metzler 已提交
3404 3405
	int status;

3406
	dprintk(1, "\n");
3407 3408 3409
	status = CtrlPowerMode(state, &powerMode);
	if (status < 0)
		printk(KERN_ERR "drxk: Error %d on %s\n", status, __func__);
R
Ralph Metzler 已提交
3410 3411 3412
	return status;
}

3413
static int DVBTCtrlSetIncEnable(struct drxk_state *state, bool *enabled)
R
Ralph Metzler 已提交
3414
{
3415 3416
	int status;

3417
	dprintk(1, "\n");
3418
	if (*enabled == true)
3419
		status = write16(state, IQM_CF_BYPASSDET__A, 0);
3420
	else
3421
		status = write16(state, IQM_CF_BYPASSDET__A, 1);
3422 3423
	if (status < 0)
		printk(KERN_ERR "drxk: Error %d on %s\n", status, __func__);
3424
	return status;
R
Ralph Metzler 已提交
3425
}
3426 3427 3428

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

3431 3432
	int status;

3433
	dprintk(1, "\n");
3434 3435
	if (*enabled == true) {
		/* write mask to 1 */
3436
		status = write16(state, OFDM_SC_RA_RAM_FR_THRES_8K__A,
3437 3438 3439
				   DEFAULT_FR_THRES_8K);
	} else {
		/* write mask to 0 */
3440
		status = write16(state, OFDM_SC_RA_RAM_FR_THRES_8K__A, 0);
3441
	}
3442 3443
	if (status < 0)
		printk(KERN_ERR "drxk: Error %d on %s\n", status, __func__);
3444 3445

	return status;
R
Ralph Metzler 已提交
3446 3447
}

3448 3449
static int DVBTCtrlSetEchoThreshold(struct drxk_state *state,
				    struct DRXKCfgDvbtEchoThres_t *echoThres)
R
Ralph Metzler 已提交
3450
{
3451
	u16 data = 0;
R
Ralph Metzler 已提交
3452 3453
	int status;

3454
	dprintk(1, "\n");
3455 3456 3457 3458 3459 3460 3461 3462 3463 3464
	status = read16(state, OFDM_SC_RA_RAM_ECHO_THRES__A, &data);
	if (status < 0)
		goto error;

	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));
3465
		break;
3466 3467 3468 3469 3470
	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));
3471
		break;
3472 3473 3474
	default:
		return -EINVAL;
	}
3475

3476 3477 3478 3479
	status = write16(state, OFDM_SC_RA_RAM_ECHO_THRES__A, data);
error:
	if (status < 0)
		printk(KERN_ERR "drxk: Error %d on %s\n", status, __func__);
3480
	return status;
R
Ralph Metzler 已提交
3481 3482 3483
}

static int DVBTCtrlSetSqiSpeed(struct drxk_state *state,
3484
			       enum DRXKCfgDvbtSqiSpeed *speed)
R
Ralph Metzler 已提交
3485
{
3486
	int status = -EINVAL;
R
Ralph Metzler 已提交
3487

3488 3489
	dprintk(1, "\n");

R
Ralph Metzler 已提交
3490 3491 3492 3493 3494 3495
	switch (*speed) {
	case DRXK_DVBT_SQI_SPEED_FAST:
	case DRXK_DVBT_SQI_SPEED_MEDIUM:
	case DRXK_DVBT_SQI_SPEED_SLOW:
		break;
	default:
3496
		goto error;
R
Ralph Metzler 已提交
3497
	}
3498
	status = write16(state, SCU_RAM_FEC_PRE_RS_BER_FILTER_SH__A,
3499
			   (u16) *speed);
3500 3501 3502
error:
	if (status < 0)
		printk(KERN_ERR "drxk: Error %d on %s\n", status, __func__);
R
Ralph Metzler 已提交
3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515
	return status;
}

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

/**
* \brief Activate DVBT specific presets
* \param demod instance of demodulator.
* \return DRXStatus_t.
*
* Called in DVBTSetStandard
*
*/
3516
static int DVBTActivatePresets(struct drxk_state *state)
R
Ralph Metzler 已提交
3517
{
3518
	int status;
3519 3520
	bool setincenable = false;
	bool setfrenable = true;
3521 3522 3523 3524

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

3525
	dprintk(1, "\n");
3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541
	status = DVBTCtrlSetIncEnable(state, &setincenable);
	if (status < 0)
		goto error;
	status = DVBTCtrlSetFrEnable(state, &setfrenable);
	if (status < 0)
		goto error;
	status = DVBTCtrlSetEchoThreshold(state, &echoThres2k);
	if (status < 0)
		goto error;
	status = DVBTCtrlSetEchoThreshold(state, &echoThres8k);
	if (status < 0)
		goto error;
	status = write16(state, SCU_RAM_AGC_INGAIN_TGT_MAX__A, state->m_dvbtIfAgcCfg.IngainTgtMax);
error:
	if (status < 0)
		printk(KERN_ERR "drxk: Error %d on %s\n", status, __func__);
3542
	return status;
R
Ralph Metzler 已提交
3543
}
3544

R
Ralph Metzler 已提交
3545 3546 3547 3548 3549 3550 3551 3552 3553 3554
/*============================================================================*/

/**
* \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.
*/
3555 3556
static int SetDVBTStandard(struct drxk_state *state,
			   enum OperationMode oMode)
R
Ralph Metzler 已提交
3557
{
3558 3559 3560
	u16 cmdResult = 0;
	u16 data = 0;
	int status;
R
Ralph Metzler 已提交
3561

3562
	dprintk(1, "\n");
R
Ralph Metzler 已提交
3563

3564
	PowerUpDVBT(state);
3565 3566 3567 3568 3569 3570
	/* added antenna switch */
	SwitchAntennaToDVBT(state);
	/* send OFDM reset command */
	status = scu_command(state, SCU_RAM_COMMAND_STANDARD_OFDM | SCU_RAM_COMMAND_CMD_DEMOD_RESET, 0, NULL, 1, &cmdResult);
	if (status < 0)
		goto error;
R
Ralph Metzler 已提交
3571

3572 3573 3574 3575
	/* send OFDM setenv command */
	status = scu_command(state, SCU_RAM_COMMAND_STANDARD_OFDM | SCU_RAM_COMMAND_CMD_DEMOD_SET_ENV, 0, NULL, 1, &cmdResult);
	if (status < 0)
		goto error;
R
Ralph Metzler 已提交
3576

3577 3578 3579 3580 3581 3582 3583 3584 3585 3586
	/* reset datapath for OFDM, processors first */
	status = write16(state, OFDM_SC_COMM_EXEC__A, OFDM_SC_COMM_EXEC_STOP);
	if (status < 0)
		goto error;
	status = write16(state, OFDM_LC_COMM_EXEC__A, OFDM_LC_COMM_EXEC_STOP);
	if (status < 0)
		goto error;
	status = write16(state, IQM_COMM_EXEC__A, IQM_COMM_EXEC_B_STOP);
	if (status < 0)
		goto error;
R
Ralph Metzler 已提交
3587

3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607
	/* IQM setup */
	/* synchronize on ofdstate->m_festart */
	status = write16(state, IQM_AF_UPD_SEL__A, 1);
	if (status < 0)
		goto error;
	/* window size for clipping ADC detection */
	status = write16(state, IQM_AF_CLP_LEN__A, 0);
	if (status < 0)
		goto error;
	/* window size for for sense pre-SAW detection */
	status = write16(state, IQM_AF_SNS_LEN__A, 0);
	if (status < 0)
		goto error;
	/* sense threshold for sense pre-SAW detection */
	status = write16(state, IQM_AF_AMUX__A, IQM_AF_AMUX_SIGNAL2ADC);
	if (status < 0)
		goto error;
	status = SetIqmAf(state, true);
	if (status < 0)
		goto error;
R
Ralph Metzler 已提交
3608

3609 3610 3611
	status = write16(state, IQM_AF_AGC_RF__A, 0);
	if (status < 0)
		goto error;
R
Ralph Metzler 已提交
3612

3613 3614 3615 3616 3617 3618 3619 3620 3621 3622
	/* Impulse noise cruncher setup */
	status = write16(state, IQM_AF_INC_LCT__A, 0);	/* crunch in IQM_CF */
	if (status < 0)
		goto error;
	status = write16(state, IQM_CF_DET_LCT__A, 0);	/* detect in IQM_CF */
	if (status < 0)
		goto error;
	status = write16(state, IQM_CF_WND_LEN__A, 3);	/* peak detector window length */
	if (status < 0)
		goto error;
R
Ralph Metzler 已提交
3623

3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638
	status = write16(state, IQM_RC_STRETCH__A, 16);
	if (status < 0)
		goto error;
	status = write16(state, IQM_CF_OUT_ENA__A, 0x4);	/* enable output 2 */
	if (status < 0)
		goto error;
	status = write16(state, IQM_CF_DS_ENA__A, 0x4);	/* decimate output 2 */
	if (status < 0)
		goto error;
	status = write16(state, IQM_CF_SCALE__A, 1600);
	if (status < 0)
		goto error;
	status = write16(state, IQM_CF_SCALE_SH__A, 0);
	if (status < 0)
		goto error;
R
Ralph Metzler 已提交
3639

3640 3641 3642 3643 3644 3645 3646
	/* virtual clipping threshold for clipping ADC detection */
	status = write16(state, IQM_AF_CLP_TH__A, 448);
	if (status < 0)
		goto error;
	status = write16(state, IQM_CF_DATATH__A, 495);	/* crunching threshold */
	if (status < 0)
		goto error;
R
Ralph Metzler 已提交
3647

3648 3649 3650
	status = BLChainCmd(state, DRXK_BL_ROM_OFFSET_TAPS_DVBT, DRXK_BLCC_NR_ELEMENTS_TAPS, DRXK_BLC_TIMEOUT);
	if (status < 0)
		goto error;
R
Ralph Metzler 已提交
3651

3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664
	status = write16(state, IQM_CF_PKDTH__A, 2);	/* peak detector threshold */
	if (status < 0)
		goto error;
	status = write16(state, IQM_CF_POW_MEAS_LEN__A, 2);
	if (status < 0)
		goto error;
	/* enable power measurement interrupt */
	status = write16(state, IQM_CF_COMM_INT_MSK__A, 1);
	if (status < 0)
		goto error;
	status = write16(state, IQM_COMM_EXEC__A, IQM_COMM_EXEC_B_ACTIVE);
	if (status < 0)
		goto error;
R
Ralph Metzler 已提交
3665

3666 3667 3668 3669 3670 3671 3672
	/* IQM will not be reset from here, sync ADC and update/init AGC */
	status = ADCSynchronization(state);
	if (status < 0)
		goto error;
	status = SetPreSaw(state, &state->m_dvbtPreSawCfg);
	if (status < 0)
		goto error;
R
Ralph Metzler 已提交
3673

3674 3675 3676 3677
	/* Halt SCU to enable safe non-atomic accesses */
	status = write16(state, SCU_COMM_EXEC__A, SCU_COMM_EXEC_HOLD);
	if (status < 0)
		goto error;
R
Ralph Metzler 已提交
3678

3679 3680 3681 3682 3683 3684
	status = SetAgcRf(state, &state->m_dvbtRfAgcCfg, true);
	if (status < 0)
		goto error;
	status = SetAgcIf(state, &state->m_dvbtIfAgcCfg, true);
	if (status < 0)
		goto error;
R
Ralph Metzler 已提交
3685

3686 3687 3688 3689 3690 3691 3692 3693
	/* Set Noise Estimation notch width and enable DC fix */
	status = read16(state, OFDM_SC_RA_RAM_CONFIG__A, &data);
	if (status < 0)
		goto error;
	data |= OFDM_SC_RA_RAM_CONFIG_NE_FIX_ENABLE__M;
	status = write16(state, OFDM_SC_RA_RAM_CONFIG__A, data);
	if (status < 0)
		goto error;
R
Ralph Metzler 已提交
3694

3695 3696 3697 3698
	/* Activate SCU to enable SCU commands */
	status = write16(state, SCU_COMM_EXEC__A, SCU_COMM_EXEC_ACTIVE);
	if (status < 0)
		goto error;
R
Ralph Metzler 已提交
3699

3700 3701 3702 3703 3704 3705
	if (!state->m_DRXK_A3_ROM_CODE) {
		/* AGCInit() is not done for DVBT, so set agcFastClipCtrlDelay  */
		status = write16(state, SCU_RAM_AGC_FAST_CLP_CTRL_DELAY__A, state->m_dvbtIfAgcCfg.FastClipCtrlDelay);
		if (status < 0)
			goto error;
	}
R
Ralph Metzler 已提交
3706

3707
	/* OFDM_SC setup */
R
Ralph Metzler 已提交
3708
#ifdef COMPILE_FOR_NONRT
3709 3710 3711 3712 3713 3714
	status = write16(state, OFDM_SC_RA_RAM_BE_OPT_DELAY__A, 1);
	if (status < 0)
		goto error;
	status = write16(state, OFDM_SC_RA_RAM_BE_OPT_INIT_DELAY__A, 2);
	if (status < 0)
		goto error;
R
Ralph Metzler 已提交
3715 3716
#endif

3717 3718 3719 3720
	/* FEC setup */
	status = write16(state, FEC_DI_INPUT_CTL__A, 1);	/* OFDM input */
	if (status < 0)
		goto error;
R
Ralph Metzler 已提交
3721 3722 3723


#ifdef COMPILE_FOR_NONRT
3724 3725 3726
	status = write16(state, FEC_RS_MEASUREMENT_PERIOD__A, 0x400);
	if (status < 0)
		goto error;
R
Ralph Metzler 已提交
3727
#else
3728 3729 3730
	status = write16(state, FEC_RS_MEASUREMENT_PERIOD__A, 0x1000);
	if (status < 0)
		goto error;
R
Ralph Metzler 已提交
3731
#endif
3732 3733 3734
	status = write16(state, FEC_RS_MEASUREMENT_PRESCALE__A, 0x0001);
	if (status < 0)
		goto error;
R
Ralph Metzler 已提交
3735

3736 3737
	/* Setup MPEG bus */
	status = MPEGTSDtoSetup(state, OM_DVBT);
3738
	if (status < 0)
3739 3740 3741 3742 3743
		goto error;
	/* Set DVBT Presets */
	status = DVBTActivatePresets(state);
	if (status < 0)
		goto error;
R
Ralph Metzler 已提交
3744

3745 3746 3747
error:
	if (status < 0)
		printk(KERN_ERR "drxk: Error %d on %s\n", status, __func__);
R
Ralph Metzler 已提交
3748 3749 3750 3751 3752 3753 3754 3755 3756 3757 3758
	return status;
}

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

3763
	dprintk(1, "\n");
3764 3765
	/* Start correct processes to get in lock */
	/* DRXK: OFDM_SC_RA_RAM_PROC_LOCKTRACK is no longer in mapfile! */
3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779
	param1 = OFDM_SC_RA_RAM_LOCKTRACK_MIN;
	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)
		goto error;
	/* Start FEC OC */
	status = MPEGTSStart(state);
	if (status < 0)
		goto error;
	status = write16(state, FEC_COMM_EXEC__A, FEC_COMM_EXEC_ACTIVE);
	if (status < 0)
		goto error;
error:
	if (status < 0)
		printk(KERN_ERR "drxk: Error %d on %s\n", status, __func__);
3780
	return status;
R
Ralph Metzler 已提交
3781 3782 3783 3784 3785 3786 3787 3788 3789 3790 3791
}


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

/**
* \brief Set up dvbt demodulator for channel.
* \param demod instance of demodulator.
* \return DRXStatus_t.
* // original DVBTSetChannel()
*/
3792 3793
static int SetDVBT(struct drxk_state *state, u16 IntermediateFreqkHz,
		   s32 tunerFreqOffset)
R
Ralph Metzler 已提交
3794
{
3795 3796 3797 3798 3799 3800
	u16 cmdResult = 0;
	u16 transmissionParams = 0;
	u16 operationMode = 0;
	u32 iqmRcRateOfs = 0;
	u32 bandwidth = 0;
	u16 param1;
R
Ralph Metzler 已提交
3801 3802
	int status;

3803
	dprintk(1, "IF =%d, TFO = %d\n", IntermediateFreqkHz, tunerFreqOffset);
R
Ralph Metzler 已提交
3804

3805 3806 3807
	status = scu_command(state, SCU_RAM_COMMAND_STANDARD_OFDM | SCU_RAM_COMMAND_CMD_DEMOD_STOP, 0, NULL, 1, &cmdResult);
	if (status < 0)
		goto error;
R
Ralph Metzler 已提交
3808

3809 3810 3811 3812
	/* Halt SCU to enable safe non-atomic accesses */
	status = write16(state, SCU_COMM_EXEC__A, SCU_COMM_EXEC_HOLD);
	if (status < 0)
		goto error;
R
Ralph Metzler 已提交
3813

3814 3815 3816 3817 3818 3819 3820
	/* Stop processors */
	status = write16(state, OFDM_SC_COMM_EXEC__A, OFDM_SC_COMM_EXEC_STOP);
	if (status < 0)
		goto error;
	status = write16(state, OFDM_LC_COMM_EXEC__A, OFDM_LC_COMM_EXEC_STOP);
	if (status < 0)
		goto error;
R
Ralph Metzler 已提交
3821

3822 3823 3824 3825 3826
	/* Mandatory fix, always stop CP, required to set spl offset back to
		hardware default (is set to 0 by ucode during pilot detection */
	status = write16(state, OFDM_CP_COMM_EXEC__A, OFDM_CP_COMM_EXEC_STOP);
	if (status < 0)
		goto error;
R
Ralph Metzler 已提交
3827

3828
	/*== Write channel settings to device =====================================*/
R
Ralph Metzler 已提交
3829

3830
	/* mode */
3831
	switch (state->props.transmission_mode) {
3832 3833 3834 3835 3836 3837
	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:
		transmissionParams |= OFDM_SC_RA_RAM_OP_PARAM_MODE_8K;
3838
		break;
3839 3840
	case TRANSMISSION_MODE_2K:
		transmissionParams |= OFDM_SC_RA_RAM_OP_PARAM_MODE_2K;
3841
		break;
3842
	}
R
Ralph Metzler 已提交
3843

3844
	/* guard */
3845
	switch (state->props.guard_interval) {
3846 3847 3848 3849 3850 3851
	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:
		transmissionParams |= OFDM_SC_RA_RAM_OP_PARAM_GUARD_4;
3852
		break;
3853 3854
	case GUARD_INTERVAL_1_32:
		transmissionParams |= OFDM_SC_RA_RAM_OP_PARAM_GUARD_32;
3855
		break;
3856 3857
	case GUARD_INTERVAL_1_16:
		transmissionParams |= OFDM_SC_RA_RAM_OP_PARAM_GUARD_16;
3858
		break;
3859 3860
	case GUARD_INTERVAL_1_8:
		transmissionParams |= OFDM_SC_RA_RAM_OP_PARAM_GUARD_8;
3861
		break;
3862
	}
R
Ralph Metzler 已提交
3863

3864
	/* hierarchy */
3865
	switch (state->props.hierarchy) {
3866 3867 3868 3869 3870 3871 3872 3873 3874 3875 3876 3877 3878 3879 3880 3881 3882 3883 3884
	case HIERARCHY_AUTO:
	case HIERARCHY_NONE:
	default:
		operationMode |= OFDM_SC_RA_RAM_OP_AUTO_HIER__M;
		/* fall through , try first guess SC_RA_RAM_OP_PARAM_HIER_NO */
		/* transmissionParams |= OFDM_SC_RA_RAM_OP_PARAM_HIER_NO; */
		/* break; */
	case HIERARCHY_1:
		transmissionParams |= OFDM_SC_RA_RAM_OP_PARAM_HIER_A1;
		break;
	case HIERARCHY_2:
		transmissionParams |= OFDM_SC_RA_RAM_OP_PARAM_HIER_A2;
		break;
	case HIERARCHY_4:
		transmissionParams |= OFDM_SC_RA_RAM_OP_PARAM_HIER_A4;
		break;
	}


3885 3886
	/* modulation */
	switch (state->props.modulation) {
3887 3888 3889 3890 3891 3892 3893 3894 3895 3896 3897 3898 3899 3900
	case QAM_AUTO:
	default:
		operationMode |= OFDM_SC_RA_RAM_OP_AUTO_CONST__M;
		/* fall through , try first guess DRX_CONSTELLATION_QAM64 */
	case QAM_64:
		transmissionParams |= OFDM_SC_RA_RAM_OP_PARAM_CONST_QAM64;
		break;
	case QPSK:
		transmissionParams |= OFDM_SC_RA_RAM_OP_PARAM_CONST_QPSK;
		break;
	case QAM_16:
		transmissionParams |= OFDM_SC_RA_RAM_OP_PARAM_CONST_QAM16;
		break;
	}
R
Ralph Metzler 已提交
3901
#if 0
3902 3903 3904 3905 3906 3907 3908 3909 3910
	/* 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:
R
Ralph Metzler 已提交
3911
		transmissionParams |= OFDM_SC_RA_RAM_OP_PARAM_PRIO_HI;
3912 3913 3914 3915 3916 3917 3918 3919 3920 3921 3922 3923 3924 3925
		WR16(devAddr, OFDM_EC_SB_PRIOR__A,
			OFDM_EC_SB_PRIOR_HI));
		break;
	case DRX_PRIORITY_UNKNOWN:	/* fall through */
	default:
		status = -EINVAL;
		goto error;
	}
#else
	/* Set Priorty high */
	transmissionParams |= OFDM_SC_RA_RAM_OP_PARAM_PRIO_HI;
	status = write16(state, OFDM_EC_SB_PRIOR__A, OFDM_EC_SB_PRIOR_HI);
	if (status < 0)
		goto error;
R
Ralph Metzler 已提交
3926 3927
#endif

3928
	/* coderate */
3929
	switch (state->props.code_rate_HP) {
3930 3931 3932 3933 3934 3935 3936 3937 3938 3939 3940 3941 3942 3943 3944 3945 3946 3947 3948 3949
	case FEC_AUTO:
	default:
		operationMode |= OFDM_SC_RA_RAM_OP_AUTO_RATE__M;
		/* fall through , try first guess DRX_CODERATE_2DIV3 */
	case FEC_2_3:
		transmissionParams |= OFDM_SC_RA_RAM_OP_PARAM_RATE_2_3;
		break;
	case FEC_1_2:
		transmissionParams |= OFDM_SC_RA_RAM_OP_PARAM_RATE_1_2;
		break;
	case FEC_3_4:
		transmissionParams |= OFDM_SC_RA_RAM_OP_PARAM_RATE_3_4;
		break;
	case FEC_5_6:
		transmissionParams |= OFDM_SC_RA_RAM_OP_PARAM_RATE_5_6;
		break;
	case FEC_7_8:
		transmissionParams |= OFDM_SC_RA_RAM_OP_PARAM_RATE_7_8;
		break;
	}
R
Ralph Metzler 已提交
3950

3951 3952 3953 3954 3955 3956 3957
	/* 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 */
3958 3959 3960 3961 3962
	switch (state->props.bandwidth_hz) {
	case 0:
		state->props.bandwidth_hz = 8000000;
		/* fall though */
	case 8000000:
3963 3964 3965 3966 3967 3968 3969 3970 3971 3972 3973 3974 3975 3976 3977 3978 3979 3980
		bandwidth = DRXK_BANDWIDTH_8MHZ_IN_HZ;
		status = write16(state, OFDM_SC_RA_RAM_SRMM_FIX_FACT_8K__A, 3052);
		if (status < 0)
			goto error;
		/* cochannel protection for PAL 8 MHz */
		status = write16(state, OFDM_SC_RA_RAM_NI_INIT_8K_PER_LEFT__A, 7);
		if (status < 0)
			goto error;
		status = write16(state, OFDM_SC_RA_RAM_NI_INIT_8K_PER_RIGHT__A, 7);
		if (status < 0)
			goto error;
		status = write16(state, OFDM_SC_RA_RAM_NI_INIT_2K_PER_LEFT__A, 7);
		if (status < 0)
			goto error;
		status = write16(state, OFDM_SC_RA_RAM_NI_INIT_2K_PER_RIGHT__A, 1);
		if (status < 0)
			goto error;
		break;
3981
	case 7000000:
3982 3983 3984 3985 3986 3987 3988 3989 3990 3991 3992 3993 3994 3995 3996 3997 3998 3999
		bandwidth = DRXK_BANDWIDTH_7MHZ_IN_HZ;
		status = write16(state, OFDM_SC_RA_RAM_SRMM_FIX_FACT_8K__A, 3491);
		if (status < 0)
			goto error;
		/* cochannel protection for PAL 7 MHz */
		status = write16(state, OFDM_SC_RA_RAM_NI_INIT_8K_PER_LEFT__A, 8);
		if (status < 0)
			goto error;
		status = write16(state, OFDM_SC_RA_RAM_NI_INIT_8K_PER_RIGHT__A, 8);
		if (status < 0)
			goto error;
		status = write16(state, OFDM_SC_RA_RAM_NI_INIT_2K_PER_LEFT__A, 4);
		if (status < 0)
			goto error;
		status = write16(state, OFDM_SC_RA_RAM_NI_INIT_2K_PER_RIGHT__A, 1);
		if (status < 0)
			goto error;
		break;
4000
	case 6000000:
4001 4002 4003 4004 4005 4006 4007 4008 4009 4010 4011 4012 4013 4014 4015 4016 4017 4018 4019 4020 4021 4022
		bandwidth = DRXK_BANDWIDTH_6MHZ_IN_HZ;
		status = write16(state, OFDM_SC_RA_RAM_SRMM_FIX_FACT_8K__A, 4073);
		if (status < 0)
			goto error;
		/* cochannel protection for NTSC 6 MHz */
		status = write16(state, OFDM_SC_RA_RAM_NI_INIT_8K_PER_LEFT__A, 19);
		if (status < 0)
			goto error;
		status = write16(state, OFDM_SC_RA_RAM_NI_INIT_8K_PER_RIGHT__A, 19);
		if (status < 0)
			goto error;
		status = write16(state, OFDM_SC_RA_RAM_NI_INIT_2K_PER_LEFT__A, 14);
		if (status < 0)
			goto error;
		status = write16(state, OFDM_SC_RA_RAM_NI_INIT_2K_PER_RIGHT__A, 1);
		if (status < 0)
			goto error;
		break;
	default:
		status = -EINVAL;
		goto error;
	}
R
Ralph Metzler 已提交
4023

4024 4025 4026 4027 4028 4029 4030 4031 4032 4033 4034 4035 4036 4037 4038 4039 4040 4041 4042 4043
	if (iqmRcRateOfs == 0) {
		/* Now compute IQM_RC_RATE_OFS
			(((SysFreq/BandWidth)/2)/2) -1) * 2^23)
			=>
			((SysFreq / BandWidth) * (2^21)) - (2^23)
			*/
		/* (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  */
		iqmRcRateOfs = Frac28a((u32)
					((state->m_sysClockFreq *
						1000) / 3), bandwidth);
		/* (SysFreq / BandWidth) * (2^21), rounding before truncating  */
		if ((iqmRcRateOfs & 0x7fL) >= 0x40)
			iqmRcRateOfs += 0x80L;
		iqmRcRateOfs = iqmRcRateOfs >> 7;
		/* ((SysFreq / BandWidth) * (2^21)) - (2^23)  */
		iqmRcRateOfs = iqmRcRateOfs - (1 << 23);
	}
R
Ralph Metzler 已提交
4044

4045 4046 4047 4048 4049 4050
	iqmRcRateOfs &=
		((((u32) IQM_RC_RATE_OFS_HI__M) <<
		IQM_RC_RATE_OFS_LO__W) | IQM_RC_RATE_OFS_LO__M);
	status = write32(state, IQM_RC_RATE_OFS_LO__A, iqmRcRateOfs);
	if (status < 0)
		goto error;
R
Ralph Metzler 已提交
4051

4052
	/* Bandwidth setting done */
R
Ralph Metzler 已提交
4053

4054
#if 0
4055 4056 4057
	status = DVBTSetFrequencyShift(demod, channel, tunerOffset);
	if (status < 0)
		goto error;
4058
#endif
4059 4060 4061
	status = SetFrequencyShifter(state, IntermediateFreqkHz, tunerFreqOffset, true);
	if (status < 0)
		goto error;
R
Ralph Metzler 已提交
4062

4063
	/*== Start SC, write channel settings to SC ===============================*/
R
Ralph Metzler 已提交
4064

4065 4066 4067 4068
	/* Activate SCU to enable SCU commands */
	status = write16(state, SCU_COMM_EXEC__A, SCU_COMM_EXEC_ACTIVE);
	if (status < 0)
		goto error;
R
Ralph Metzler 已提交
4069

4070 4071 4072 4073 4074 4075 4076
	/* Enable SC after setting all other parameters */
	status = write16(state, OFDM_SC_COMM_STATE__A, 0);
	if (status < 0)
		goto error;
	status = write16(state, OFDM_SC_COMM_EXEC__A, 1);
	if (status < 0)
		goto error;
R
Ralph Metzler 已提交
4077 4078


4079 4080 4081 4082 4083 4084 4085 4086 4087 4088 4089 4090 4091 4092
	status = scu_command(state, SCU_RAM_COMMAND_STANDARD_OFDM | SCU_RAM_COMMAND_CMD_DEMOD_START, 0, NULL, 1, &cmdResult);
	if (status < 0)
		goto error;

	/* Write SC parameter registers, set all AUTO flags in operation mode */
	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);
	if (status < 0)
		goto error;
R
Ralph Metzler 已提交
4093

4094 4095 4096 4097 4098
	if (!state->m_DRXK_A3_ROM_CODE)
		status = DVBTCtrlSetSqiSpeed(state, &state->m_sqiSpeed);
error:
	if (status < 0)
		printk(KERN_ERR "drxk: Error %d on %s\n", status, __func__);
R
Ralph Metzler 已提交
4099 4100 4101 4102 4103 4104 4105 4106 4107 4108 4109 4110 4111 4112 4113 4114

	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)
{
4115 4116 4117 4118 4119 4120 4121 4122 4123
	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;

4124 4125
	dprintk(1, "\n");

4126
	*pLockStatus = NOT_LOCKED;
4127 4128
	/* driver 0.9.0 */
	/* Check if SC is running */
4129
	status = read16(state, OFDM_SC_COMM_EXEC__A, &ScCommExec);
4130 4131 4132 4133
	if (status < 0)
		goto end;
	if (ScCommExec == OFDM_SC_COMM_EXEC_STOP)
		goto end;
4134

4135
	status = read16(state, OFDM_SC_RA_RAM_LOCK__A, &ScRaRamLock);
4136 4137
	if (status < 0)
		goto end;
4138 4139 4140 4141 4142 4143 4144 4145 4146

	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;
4147 4148 4149
end:
	if (status < 0)
		printk(KERN_ERR "drxk: Error %d on %s\n", status, __func__);
4150 4151

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

4154
static int PowerUpQAM(struct drxk_state *state)
R
Ralph Metzler 已提交
4155
{
4156
	enum DRXPowerMode powerMode = DRXK_POWER_DOWN_OFDM;
4157
	int status;
R
Ralph Metzler 已提交
4158

4159
	dprintk(1, "\n");
4160 4161 4162
	status = CtrlPowerMode(state, &powerMode);
	if (status < 0)
		printk(KERN_ERR "drxk: Error %d on %s\n", status, __func__);
R
Ralph Metzler 已提交
4163

4164
	return status;
R
Ralph Metzler 已提交
4165 4166 4167
}


4168
/** Power Down QAM */
R
Ralph Metzler 已提交
4169 4170
static int PowerDownQAM(struct drxk_state *state)
{
4171 4172 4173 4174
	u16 data = 0;
	u16 cmdResult;
	int status = 0;

4175
	dprintk(1, "\n");
4176 4177 4178 4179 4180 4181 4182 4183 4184 4185
	status = read16(state, SCU_COMM_EXEC__A, &data);
	if (status < 0)
		goto error;
	if (data == SCU_COMM_EXEC_ACTIVE) {
		/*
			STOP demodulator
			QAM and HW blocks
			*/
		/* stop all comstate->m_exec */
		status = write16(state, QAM_COMM_EXEC__A, QAM_COMM_EXEC_STOP);
4186
		if (status < 0)
4187 4188
			goto error;
		status = scu_command(state, SCU_RAM_COMMAND_STANDARD_QAM | SCU_RAM_COMMAND_CMD_DEMOD_STOP, 0, NULL, 1, &cmdResult);
4189
		if (status < 0)
4190 4191 4192 4193 4194 4195 4196 4197
			goto error;
	}
	/* powerdown AFE                   */
	status = SetIqmAf(state, false);

error:
	if (status < 0)
		printk(KERN_ERR "drxk: Error %d on %s\n", status, __func__);
4198 4199

	return status;
R
Ralph Metzler 已提交
4200
}
4201

R
Ralph Metzler 已提交
4202 4203 4204 4205 4206
/*============================================================================*/

/**
* \brief Setup of the QAM Measurement intervals for signal quality
* \param demod instance of demod.
4207
* \param modulation current modulation.
R
Ralph Metzler 已提交
4208 4209 4210 4211 4212 4213 4214 4215
* \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,
4216
			     enum EDrxkConstellation modulation,
R
Ralph Metzler 已提交
4217 4218
			     u32 symbolRate)
{
4219 4220 4221 4222
	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 已提交
4223 4224
	int status = 0;

4225
	dprintk(1, "\n");
R
Ralph Metzler 已提交
4226

4227
	fecRsPrescale = 1;
4228 4229
	/* fecBitsDesired = symbolRate [kHz] *
		FrameLenght [ms] *
4230
		(modulation + 1) *
4231 4232 4233
		SyncLoss (== 1) *
		ViterbiLoss (==1)
		*/
4234
	switch (modulation) {
4235 4236 4237 4238 4239 4240 4241 4242 4243 4244 4245 4246 4247 4248 4249 4250 4251 4252 4253 4254
	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;
	}
	if (status < 0)
		goto error;
R
Ralph Metzler 已提交
4255

4256 4257
	fecBitsDesired /= 1000;	/* symbolRate [Hz] -> symbolRate [kHz]  */
	fecBitsDesired *= 500;	/* meas. period [ms] */
R
Ralph Metzler 已提交
4258

4259 4260 4261
	/* Annex A/C: bits/RsPeriod = 204 * 8 = 1632 */
	/* fecRsPeriodTotal = fecBitsDesired / 1632 */
	fecRsPeriodTotal = (fecBitsDesired / 1632UL) + 1;	/* roughly ceil */
R
Ralph Metzler 已提交
4262

4263 4264 4265 4266 4267
	/* fecRsPeriodTotal =  fecRsPrescale * fecRsPeriod  */
	fecRsPrescale = 1 + (u16) (fecRsPeriodTotal >> 16);
	if (fecRsPrescale == 0) {
		/* Divide by zero (though impossible) */
		status = -EINVAL;
4268
		if (status < 0)
4269 4270 4271 4272 4273
			goto error;
	}
	fecRsPeriod =
		((u16) fecRsPeriodTotal +
		(fecRsPrescale >> 1)) / fecRsPrescale;
R
Ralph Metzler 已提交
4274

4275 4276
	/* write corresponding registers */
	status = write16(state, FEC_RS_MEASUREMENT_PERIOD__A, fecRsPeriod);
4277
	if (status < 0)
4278 4279 4280 4281 4282 4283 4284 4285
		goto error;
	status = write16(state, FEC_RS_MEASUREMENT_PRESCALE__A, fecRsPrescale);
	if (status < 0)
		goto error;
	status = write16(state, FEC_OC_SNC_FAIL_PERIOD__A, fecRsPeriod);
error:
	if (status < 0)
		printk(KERN_ERR "drxk: Error %d on %s\n", status, __func__);
R
Ralph Metzler 已提交
4286 4287 4288
	return status;
}

4289
static int SetQAM16(struct drxk_state *state)
R
Ralph Metzler 已提交
4290
{
4291 4292
	int status = 0;

4293
	dprintk(1, "\n");
4294 4295 4296 4297 4298 4299 4300 4301 4302 4303 4304 4305 4306 4307 4308 4309 4310 4311 4312 4313 4314 4315 4316 4317 4318 4319 4320 4321 4322 4323 4324 4325 4326 4327 4328 4329 4330 4331 4332
	/* QAM Equalizer Setup */
	/* Equalizer */
	status = write16(state, SCU_RAM_QAM_EQ_CMA_RAD0__A, 13517);
	if (status < 0)
		goto error;
	status = write16(state, SCU_RAM_QAM_EQ_CMA_RAD1__A, 13517);
	if (status < 0)
		goto error;
	status = write16(state, SCU_RAM_QAM_EQ_CMA_RAD2__A, 13517);
	if (status < 0)
		goto error;
	status = write16(state, SCU_RAM_QAM_EQ_CMA_RAD3__A, 13517);
	if (status < 0)
		goto error;
	status = write16(state, SCU_RAM_QAM_EQ_CMA_RAD4__A, 13517);
	if (status < 0)
		goto error;
	status = write16(state, SCU_RAM_QAM_EQ_CMA_RAD5__A, 13517);
	if (status < 0)
		goto error;
	/* Decision Feedback Equalizer */
	status = write16(state, QAM_DQ_QUAL_FUN0__A, 2);
	if (status < 0)
		goto error;
	status = write16(state, QAM_DQ_QUAL_FUN1__A, 2);
	if (status < 0)
		goto error;
	status = write16(state, QAM_DQ_QUAL_FUN2__A, 2);
	if (status < 0)
		goto error;
	status = write16(state, QAM_DQ_QUAL_FUN3__A, 2);
	if (status < 0)
		goto error;
	status = write16(state, QAM_DQ_QUAL_FUN4__A, 2);
	if (status < 0)
		goto error;
	status = write16(state, QAM_DQ_QUAL_FUN5__A, 0);
	if (status < 0)
		goto error;
4333

4334 4335 4336 4337 4338 4339 4340 4341 4342
	status = write16(state, QAM_SY_SYNC_HWM__A, 5);
	if (status < 0)
		goto error;
	status = write16(state, QAM_SY_SYNC_AWM__A, 4);
	if (status < 0)
		goto error;
	status = write16(state, QAM_SY_SYNC_LWM__A, 3);
	if (status < 0)
		goto error;
4343

4344 4345 4346 4347
	/* QAM Slicer Settings */
	status = write16(state, SCU_RAM_QAM_SL_SIG_POWER__A, DRXK_QAM_SL_SIG_POWER_QAM16);
	if (status < 0)
		goto error;
4348

4349 4350 4351 4352 4353 4354 4355 4356 4357 4358 4359 4360 4361 4362 4363 4364 4365 4366 4367 4368 4369 4370 4371 4372 4373
	/* QAM Loop Controller Coeficients */
	status = write16(state, SCU_RAM_QAM_LC_CA_FINE__A, 15);
	if (status < 0)
		goto error;
	status = write16(state, SCU_RAM_QAM_LC_CA_COARSE__A, 40);
	if (status < 0)
		goto error;
	status = write16(state, SCU_RAM_QAM_LC_EP_FINE__A, 12);
	if (status < 0)
		goto error;
	status = write16(state, SCU_RAM_QAM_LC_EP_MEDIUM__A, 24);
	if (status < 0)
		goto error;
	status = write16(state, SCU_RAM_QAM_LC_EP_COARSE__A, 24);
	if (status < 0)
		goto error;
	status = write16(state, SCU_RAM_QAM_LC_EI_FINE__A, 12);
	if (status < 0)
		goto error;
	status = write16(state, SCU_RAM_QAM_LC_EI_MEDIUM__A, 16);
	if (status < 0)
		goto error;
	status = write16(state, SCU_RAM_QAM_LC_EI_COARSE__A, 16);
	if (status < 0)
		goto error;
4374

4375 4376 4377 4378 4379 4380 4381 4382 4383 4384 4385 4386 4387 4388 4389 4390 4391 4392 4393 4394 4395 4396 4397 4398 4399 4400 4401 4402 4403 4404 4405 4406 4407 4408 4409 4410
	status = write16(state, SCU_RAM_QAM_LC_CP_FINE__A, 5);
	if (status < 0)
		goto error;
	status = write16(state, SCU_RAM_QAM_LC_CP_MEDIUM__A, 20);
	if (status < 0)
		goto error;
	status = write16(state, SCU_RAM_QAM_LC_CP_COARSE__A, 80);
	if (status < 0)
		goto error;
	status = write16(state, SCU_RAM_QAM_LC_CI_FINE__A, 5);
	if (status < 0)
		goto error;
	status = write16(state, SCU_RAM_QAM_LC_CI_MEDIUM__A, 20);
	if (status < 0)
		goto error;
	status = write16(state, SCU_RAM_QAM_LC_CI_COARSE__A, 50);
	if (status < 0)
		goto error;
	status = write16(state, SCU_RAM_QAM_LC_CF_FINE__A, 16);
	if (status < 0)
		goto error;
	status = write16(state, SCU_RAM_QAM_LC_CF_MEDIUM__A, 16);
	if (status < 0)
		goto error;
	status = write16(state, SCU_RAM_QAM_LC_CF_COARSE__A, 32);
	if (status < 0)
		goto error;
	status = write16(state, SCU_RAM_QAM_LC_CF1_FINE__A, 5);
	if (status < 0)
		goto error;
	status = write16(state, SCU_RAM_QAM_LC_CF1_MEDIUM__A, 10);
	if (status < 0)
		goto error;
	status = write16(state, SCU_RAM_QAM_LC_CF1_COARSE__A, 10);
	if (status < 0)
		goto error;
4411 4412


4413
	/* QAM State Machine (FSM) Thresholds */
4414

4415 4416 4417 4418 4419 4420 4421 4422 4423 4424 4425 4426 4427 4428 4429 4430 4431 4432
	status = write16(state, SCU_RAM_QAM_FSM_RTH__A, 140);
	if (status < 0)
		goto error;
	status = write16(state, SCU_RAM_QAM_FSM_FTH__A, 50);
	if (status < 0)
		goto error;
	status = write16(state, SCU_RAM_QAM_FSM_CTH__A, 95);
	if (status < 0)
		goto error;
	status = write16(state, SCU_RAM_QAM_FSM_PTH__A, 120);
	if (status < 0)
		goto error;
	status = write16(state, SCU_RAM_QAM_FSM_QTH__A, 230);
	if (status < 0)
		goto error;
	status = write16(state, SCU_RAM_QAM_FSM_MTH__A, 105);
	if (status < 0)
		goto error;
4433

4434 4435 4436 4437 4438 4439 4440 4441 4442
	status = write16(state, SCU_RAM_QAM_FSM_RATE_LIM__A, 40);
	if (status < 0)
		goto error;
	status = write16(state, SCU_RAM_QAM_FSM_COUNT_LIM__A, 4);
	if (status < 0)
		goto error;
	status = write16(state, SCU_RAM_QAM_FSM_FREQ_LIM__A, 24);
	if (status < 0)
		goto error;
4443 4444


4445
	/* QAM FSM Tracking Parameters */
4446

4447 4448 4449 4450 4451 4452 4453 4454 4455 4456 4457 4458 4459 4460 4461 4462 4463 4464 4465 4466 4467
	status = write16(state, SCU_RAM_QAM_FSM_MEDIAN_AV_MULT__A, (u16) 16);
	if (status < 0)
		goto error;
	status = write16(state, SCU_RAM_QAM_FSM_RADIUS_AV_LIMIT__A, (u16) 220);
	if (status < 0)
		goto error;
	status = write16(state, SCU_RAM_QAM_FSM_LCAVG_OFFSET1__A, (u16) 25);
	if (status < 0)
		goto error;
	status = write16(state, SCU_RAM_QAM_FSM_LCAVG_OFFSET2__A, (u16) 6);
	if (status < 0)
		goto error;
	status = write16(state, SCU_RAM_QAM_FSM_LCAVG_OFFSET3__A, (u16) -24);
	if (status < 0)
		goto error;
	status = write16(state, SCU_RAM_QAM_FSM_LCAVG_OFFSET4__A, (u16) -65);
	if (status < 0)
		goto error;
	status = write16(state, SCU_RAM_QAM_FSM_LCAVG_OFFSET5__A, (u16) -127);
	if (status < 0)
		goto error;
4468

4469 4470 4471
error:
	if (status < 0)
		printk(KERN_ERR "drxk: Error %d on %s\n", status, __func__);
4472
	return status;
R
Ralph Metzler 已提交
4473 4474 4475 4476 4477 4478 4479 4480 4481
}

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

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

4486
	dprintk(1, "\n");
4487

4488 4489 4490 4491 4492 4493 4494 4495 4496 4497 4498 4499 4500 4501 4502 4503 4504 4505 4506 4507
	/* QAM Equalizer Setup */
	/* Equalizer */
	status = write16(state, SCU_RAM_QAM_EQ_CMA_RAD0__A, 6707);
	if (status < 0)
		goto error;
	status = write16(state, SCU_RAM_QAM_EQ_CMA_RAD1__A, 6707);
	if (status < 0)
		goto error;
	status = write16(state, SCU_RAM_QAM_EQ_CMA_RAD2__A, 6707);
	if (status < 0)
		goto error;
	status = write16(state, SCU_RAM_QAM_EQ_CMA_RAD3__A, 6707);
	if (status < 0)
		goto error;
	status = write16(state, SCU_RAM_QAM_EQ_CMA_RAD4__A, 6707);
	if (status < 0)
		goto error;
	status = write16(state, SCU_RAM_QAM_EQ_CMA_RAD5__A, 6707);
	if (status < 0)
		goto error;
4508

4509 4510 4511 4512 4513 4514 4515 4516 4517 4518 4519 4520 4521 4522 4523 4524 4525 4526 4527
	/* Decision Feedback Equalizer */
	status = write16(state, QAM_DQ_QUAL_FUN0__A, 3);
	if (status < 0)
		goto error;
	status = write16(state, QAM_DQ_QUAL_FUN1__A, 3);
	if (status < 0)
		goto error;
	status = write16(state, QAM_DQ_QUAL_FUN2__A, 3);
	if (status < 0)
		goto error;
	status = write16(state, QAM_DQ_QUAL_FUN3__A, 3);
	if (status < 0)
		goto error;
	status = write16(state, QAM_DQ_QUAL_FUN4__A, 3);
	if (status < 0)
		goto error;
	status = write16(state, QAM_DQ_QUAL_FUN5__A, 0);
	if (status < 0)
		goto error;
4528

4529 4530 4531 4532 4533 4534 4535 4536 4537
	status = write16(state, QAM_SY_SYNC_HWM__A, 6);
	if (status < 0)
		goto error;
	status = write16(state, QAM_SY_SYNC_AWM__A, 5);
	if (status < 0)
		goto error;
	status = write16(state, QAM_SY_SYNC_LWM__A, 3);
	if (status < 0)
		goto error;
4538

4539
	/* QAM Slicer Settings */
4540

4541 4542 4543
	status = write16(state, SCU_RAM_QAM_SL_SIG_POWER__A, DRXK_QAM_SL_SIG_POWER_QAM32);
	if (status < 0)
		goto error;
4544 4545


4546
	/* QAM Loop Controller Coeficients */
4547

4548 4549 4550 4551 4552 4553 4554 4555 4556 4557 4558 4559 4560 4561 4562 4563 4564 4565 4566 4567 4568 4569 4570 4571
	status = write16(state, SCU_RAM_QAM_LC_CA_FINE__A, 15);
	if (status < 0)
		goto error;
	status = write16(state, SCU_RAM_QAM_LC_CA_COARSE__A, 40);
	if (status < 0)
		goto error;
	status = write16(state, SCU_RAM_QAM_LC_EP_FINE__A, 12);
	if (status < 0)
		goto error;
	status = write16(state, SCU_RAM_QAM_LC_EP_MEDIUM__A, 24);
	if (status < 0)
		goto error;
	status = write16(state, SCU_RAM_QAM_LC_EP_COARSE__A, 24);
	if (status < 0)
		goto error;
	status = write16(state, SCU_RAM_QAM_LC_EI_FINE__A, 12);
	if (status < 0)
		goto error;
	status = write16(state, SCU_RAM_QAM_LC_EI_MEDIUM__A, 16);
	if (status < 0)
		goto error;
	status = write16(state, SCU_RAM_QAM_LC_EI_COARSE__A, 16);
	if (status < 0)
		goto error;
4572

4573 4574 4575 4576 4577 4578 4579 4580 4581 4582 4583 4584 4585 4586 4587 4588 4589 4590 4591 4592 4593 4594 4595 4596 4597 4598 4599 4600 4601 4602 4603 4604 4605 4606 4607 4608
	status = write16(state, SCU_RAM_QAM_LC_CP_FINE__A, 5);
	if (status < 0)
		goto error;
	status = write16(state, SCU_RAM_QAM_LC_CP_MEDIUM__A, 20);
	if (status < 0)
		goto error;
	status = write16(state, SCU_RAM_QAM_LC_CP_COARSE__A, 80);
	if (status < 0)
		goto error;
	status = write16(state, SCU_RAM_QAM_LC_CI_FINE__A, 5);
	if (status < 0)
		goto error;
	status = write16(state, SCU_RAM_QAM_LC_CI_MEDIUM__A, 20);
	if (status < 0)
		goto error;
	status = write16(state, SCU_RAM_QAM_LC_CI_COARSE__A, 50);
	if (status < 0)
		goto error;
	status = write16(state, SCU_RAM_QAM_LC_CF_FINE__A, 16);
	if (status < 0)
		goto error;
	status = write16(state, SCU_RAM_QAM_LC_CF_MEDIUM__A, 16);
	if (status < 0)
		goto error;
	status = write16(state, SCU_RAM_QAM_LC_CF_COARSE__A, 16);
	if (status < 0)
		goto error;
	status = write16(state, SCU_RAM_QAM_LC_CF1_FINE__A, 5);
	if (status < 0)
		goto error;
	status = write16(state, SCU_RAM_QAM_LC_CF1_MEDIUM__A, 10);
	if (status < 0)
		goto error;
	status = write16(state, SCU_RAM_QAM_LC_CF1_COARSE__A, 0);
	if (status < 0)
		goto error;
4609 4610


4611
	/* QAM State Machine (FSM) Thresholds */
4612

4613 4614 4615 4616 4617 4618 4619 4620 4621 4622 4623 4624 4625 4626 4627 4628 4629 4630
	status = write16(state, SCU_RAM_QAM_FSM_RTH__A, 90);
	if (status < 0)
		goto error;
	status = write16(state, SCU_RAM_QAM_FSM_FTH__A, 50);
	if (status < 0)
		goto error;
	status = write16(state, SCU_RAM_QAM_FSM_CTH__A, 80);
	if (status < 0)
		goto error;
	status = write16(state, SCU_RAM_QAM_FSM_PTH__A, 100);
	if (status < 0)
		goto error;
	status = write16(state, SCU_RAM_QAM_FSM_QTH__A, 170);
	if (status < 0)
		goto error;
	status = write16(state, SCU_RAM_QAM_FSM_MTH__A, 100);
	if (status < 0)
		goto error;
4631

4632 4633 4634 4635 4636 4637 4638 4639 4640
	status = write16(state, SCU_RAM_QAM_FSM_RATE_LIM__A, 40);
	if (status < 0)
		goto error;
	status = write16(state, SCU_RAM_QAM_FSM_COUNT_LIM__A, 4);
	if (status < 0)
		goto error;
	status = write16(state, SCU_RAM_QAM_FSM_FREQ_LIM__A, 10);
	if (status < 0)
		goto error;
4641 4642


4643
	/* QAM FSM Tracking Parameters */
4644

4645 4646 4647 4648 4649 4650 4651 4652 4653 4654 4655 4656 4657 4658 4659 4660 4661 4662 4663 4664 4665 4666
	status = write16(state, SCU_RAM_QAM_FSM_MEDIAN_AV_MULT__A, (u16) 12);
	if (status < 0)
		goto error;
	status = write16(state, SCU_RAM_QAM_FSM_RADIUS_AV_LIMIT__A, (u16) 140);
	if (status < 0)
		goto error;
	status = write16(state, SCU_RAM_QAM_FSM_LCAVG_OFFSET1__A, (u16) -8);
	if (status < 0)
		goto error;
	status = write16(state, SCU_RAM_QAM_FSM_LCAVG_OFFSET2__A, (u16) -16);
	if (status < 0)
		goto error;
	status = write16(state, SCU_RAM_QAM_FSM_LCAVG_OFFSET3__A, (u16) -26);
	if (status < 0)
		goto error;
	status = write16(state, SCU_RAM_QAM_FSM_LCAVG_OFFSET4__A, (u16) -56);
	if (status < 0)
		goto error;
	status = write16(state, SCU_RAM_QAM_FSM_LCAVG_OFFSET5__A, (u16) -86);
error:
	if (status < 0)
		printk(KERN_ERR "drxk: Error %d on %s\n", status, __func__);
4667
	return status;
R
Ralph Metzler 已提交
4668 4669 4670 4671 4672 4673 4674 4675 4676
}

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

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

4681
	dprintk(1, "\n");
4682 4683 4684 4685 4686 4687 4688 4689 4690 4691 4692 4693 4694 4695 4696 4697 4698 4699 4700 4701
	/* QAM Equalizer Setup */
	/* Equalizer */
	status = write16(state, SCU_RAM_QAM_EQ_CMA_RAD0__A, 13336);
	if (status < 0)
		goto error;
	status = write16(state, SCU_RAM_QAM_EQ_CMA_RAD1__A, 12618);
	if (status < 0)
		goto error;
	status = write16(state, SCU_RAM_QAM_EQ_CMA_RAD2__A, 11988);
	if (status < 0)
		goto error;
	status = write16(state, SCU_RAM_QAM_EQ_CMA_RAD3__A, 13809);
	if (status < 0)
		goto error;
	status = write16(state, SCU_RAM_QAM_EQ_CMA_RAD4__A, 13809);
	if (status < 0)
		goto error;
	status = write16(state, SCU_RAM_QAM_EQ_CMA_RAD5__A, 15609);
	if (status < 0)
		goto error;
4702

4703 4704 4705 4706 4707 4708 4709 4710 4711 4712 4713 4714 4715 4716 4717 4718 4719 4720 4721
	/* Decision Feedback Equalizer */
	status = write16(state, QAM_DQ_QUAL_FUN0__A, 4);
	if (status < 0)
		goto error;
	status = write16(state, QAM_DQ_QUAL_FUN1__A, 4);
	if (status < 0)
		goto error;
	status = write16(state, QAM_DQ_QUAL_FUN2__A, 4);
	if (status < 0)
		goto error;
	status = write16(state, QAM_DQ_QUAL_FUN3__A, 4);
	if (status < 0)
		goto error;
	status = write16(state, QAM_DQ_QUAL_FUN4__A, 3);
	if (status < 0)
		goto error;
	status = write16(state, QAM_DQ_QUAL_FUN5__A, 0);
	if (status < 0)
		goto error;
4722

4723 4724 4725 4726 4727 4728 4729 4730 4731
	status = write16(state, QAM_SY_SYNC_HWM__A, 5);
	if (status < 0)
		goto error;
	status = write16(state, QAM_SY_SYNC_AWM__A, 4);
	if (status < 0)
		goto error;
	status = write16(state, QAM_SY_SYNC_LWM__A, 3);
	if (status < 0)
		goto error;
4732

4733 4734 4735 4736
	/* QAM Slicer Settings */
	status = write16(state, SCU_RAM_QAM_SL_SIG_POWER__A, DRXK_QAM_SL_SIG_POWER_QAM64);
	if (status < 0)
		goto error;
4737 4738


4739
	/* QAM Loop Controller Coeficients */
4740

4741 4742 4743 4744 4745 4746 4747 4748 4749 4750 4751 4752 4753 4754 4755 4756 4757 4758 4759 4760 4761 4762 4763 4764
	status = write16(state, SCU_RAM_QAM_LC_CA_FINE__A, 15);
	if (status < 0)
		goto error;
	status = write16(state, SCU_RAM_QAM_LC_CA_COARSE__A, 40);
	if (status < 0)
		goto error;
	status = write16(state, SCU_RAM_QAM_LC_EP_FINE__A, 12);
	if (status < 0)
		goto error;
	status = write16(state, SCU_RAM_QAM_LC_EP_MEDIUM__A, 24);
	if (status < 0)
		goto error;
	status = write16(state, SCU_RAM_QAM_LC_EP_COARSE__A, 24);
	if (status < 0)
		goto error;
	status = write16(state, SCU_RAM_QAM_LC_EI_FINE__A, 12);
	if (status < 0)
		goto error;
	status = write16(state, SCU_RAM_QAM_LC_EI_MEDIUM__A, 16);
	if (status < 0)
		goto error;
	status = write16(state, SCU_RAM_QAM_LC_EI_COARSE__A, 16);
	if (status < 0)
		goto error;
4765

4766 4767 4768 4769 4770 4771 4772 4773 4774 4775 4776 4777 4778 4779 4780 4781 4782 4783 4784 4785 4786 4787 4788 4789 4790 4791 4792 4793 4794 4795 4796 4797 4798 4799 4800 4801
	status = write16(state, SCU_RAM_QAM_LC_CP_FINE__A, 5);
	if (status < 0)
		goto error;
	status = write16(state, SCU_RAM_QAM_LC_CP_MEDIUM__A, 30);
	if (status < 0)
		goto error;
	status = write16(state, SCU_RAM_QAM_LC_CP_COARSE__A, 100);
	if (status < 0)
		goto error;
	status = write16(state, SCU_RAM_QAM_LC_CI_FINE__A, 5);
	if (status < 0)
		goto error;
	status = write16(state, SCU_RAM_QAM_LC_CI_MEDIUM__A, 30);
	if (status < 0)
		goto error;
	status = write16(state, SCU_RAM_QAM_LC_CI_COARSE__A, 50);
	if (status < 0)
		goto error;
	status = write16(state, SCU_RAM_QAM_LC_CF_FINE__A, 16);
	if (status < 0)
		goto error;
	status = write16(state, SCU_RAM_QAM_LC_CF_MEDIUM__A, 25);
	if (status < 0)
		goto error;
	status = write16(state, SCU_RAM_QAM_LC_CF_COARSE__A, 48);
	if (status < 0)
		goto error;
	status = write16(state, SCU_RAM_QAM_LC_CF1_FINE__A, 5);
	if (status < 0)
		goto error;
	status = write16(state, SCU_RAM_QAM_LC_CF1_MEDIUM__A, 10);
	if (status < 0)
		goto error;
	status = write16(state, SCU_RAM_QAM_LC_CF1_COARSE__A, 10);
	if (status < 0)
		goto error;
4802 4803


4804
	/* QAM State Machine (FSM) Thresholds */
4805

4806 4807 4808 4809 4810 4811 4812 4813 4814 4815 4816 4817 4818 4819 4820 4821 4822 4823
	status = write16(state, SCU_RAM_QAM_FSM_RTH__A, 100);
	if (status < 0)
		goto error;
	status = write16(state, SCU_RAM_QAM_FSM_FTH__A, 60);
	if (status < 0)
		goto error;
	status = write16(state, SCU_RAM_QAM_FSM_CTH__A, 80);
	if (status < 0)
		goto error;
	status = write16(state, SCU_RAM_QAM_FSM_PTH__A, 110);
	if (status < 0)
		goto error;
	status = write16(state, SCU_RAM_QAM_FSM_QTH__A, 200);
	if (status < 0)
		goto error;
	status = write16(state, SCU_RAM_QAM_FSM_MTH__A, 95);
	if (status < 0)
		goto error;
4824

4825 4826 4827 4828 4829 4830 4831 4832 4833
	status = write16(state, SCU_RAM_QAM_FSM_RATE_LIM__A, 40);
	if (status < 0)
		goto error;
	status = write16(state, SCU_RAM_QAM_FSM_COUNT_LIM__A, 4);
	if (status < 0)
		goto error;
	status = write16(state, SCU_RAM_QAM_FSM_FREQ_LIM__A, 15);
	if (status < 0)
		goto error;
4834 4835


4836
	/* QAM FSM Tracking Parameters */
4837

4838 4839 4840 4841 4842 4843 4844 4845 4846 4847 4848 4849 4850 4851 4852 4853 4854 4855 4856 4857 4858 4859
	status = write16(state, SCU_RAM_QAM_FSM_MEDIAN_AV_MULT__A, (u16) 12);
	if (status < 0)
		goto error;
	status = write16(state, SCU_RAM_QAM_FSM_RADIUS_AV_LIMIT__A, (u16) 141);
	if (status < 0)
		goto error;
	status = write16(state, SCU_RAM_QAM_FSM_LCAVG_OFFSET1__A, (u16) 7);
	if (status < 0)
		goto error;
	status = write16(state, SCU_RAM_QAM_FSM_LCAVG_OFFSET2__A, (u16) 0);
	if (status < 0)
		goto error;
	status = write16(state, SCU_RAM_QAM_FSM_LCAVG_OFFSET3__A, (u16) -15);
	if (status < 0)
		goto error;
	status = write16(state, SCU_RAM_QAM_FSM_LCAVG_OFFSET4__A, (u16) -45);
	if (status < 0)
		goto error;
	status = write16(state, SCU_RAM_QAM_FSM_LCAVG_OFFSET5__A, (u16) -80);
error:
	if (status < 0)
		printk(KERN_ERR "drxk: Error %d on %s\n", status, __func__);
4860 4861

	return status;
R
Ralph Metzler 已提交
4862 4863 4864 4865 4866 4867 4868 4869 4870 4871 4872
}

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

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

4875
	dprintk(1, "\n");
4876 4877 4878 4879 4880 4881 4882 4883 4884 4885 4886 4887 4888 4889 4890 4891 4892 4893 4894 4895
	/* QAM Equalizer Setup */
	/* Equalizer */
	status = write16(state, SCU_RAM_QAM_EQ_CMA_RAD0__A, 6564);
	if (status < 0)
		goto error;
	status = write16(state, SCU_RAM_QAM_EQ_CMA_RAD1__A, 6598);
	if (status < 0)
		goto error;
	status = write16(state, SCU_RAM_QAM_EQ_CMA_RAD2__A, 6394);
	if (status < 0)
		goto error;
	status = write16(state, SCU_RAM_QAM_EQ_CMA_RAD3__A, 6409);
	if (status < 0)
		goto error;
	status = write16(state, SCU_RAM_QAM_EQ_CMA_RAD4__A, 6656);
	if (status < 0)
		goto error;
	status = write16(state, SCU_RAM_QAM_EQ_CMA_RAD5__A, 7238);
	if (status < 0)
		goto error;
4896

4897 4898 4899 4900 4901 4902 4903 4904 4905 4906 4907 4908 4909 4910 4911 4912 4913 4914 4915
	/* Decision Feedback Equalizer */
	status = write16(state, QAM_DQ_QUAL_FUN0__A, 6);
	if (status < 0)
		goto error;
	status = write16(state, QAM_DQ_QUAL_FUN1__A, 6);
	if (status < 0)
		goto error;
	status = write16(state, QAM_DQ_QUAL_FUN2__A, 6);
	if (status < 0)
		goto error;
	status = write16(state, QAM_DQ_QUAL_FUN3__A, 6);
	if (status < 0)
		goto error;
	status = write16(state, QAM_DQ_QUAL_FUN4__A, 5);
	if (status < 0)
		goto error;
	status = write16(state, QAM_DQ_QUAL_FUN5__A, 0);
	if (status < 0)
		goto error;
4916

4917 4918 4919 4920 4921 4922 4923 4924 4925
	status = write16(state, QAM_SY_SYNC_HWM__A, 6);
	if (status < 0)
		goto error;
	status = write16(state, QAM_SY_SYNC_AWM__A, 5);
	if (status < 0)
		goto error;
	status = write16(state, QAM_SY_SYNC_LWM__A, 3);
	if (status < 0)
		goto error;
4926 4927


4928
	/* QAM Slicer Settings */
4929

4930 4931 4932
	status = write16(state, SCU_RAM_QAM_SL_SIG_POWER__A, DRXK_QAM_SL_SIG_POWER_QAM128);
	if (status < 0)
		goto error;
4933 4934


4935
	/* QAM Loop Controller Coeficients */
4936

4937 4938 4939 4940 4941 4942 4943 4944 4945 4946 4947 4948 4949 4950 4951 4952 4953 4954 4955 4956 4957 4958 4959 4960 4961 4962 4963 4964 4965 4966 4967 4968 4969 4970 4971 4972 4973 4974 4975 4976 4977 4978 4979 4980 4981 4982 4983 4984 4985 4986 4987 4988 4989 4990 4991 4992 4993 4994 4995 4996 4997
	status = write16(state, SCU_RAM_QAM_LC_CA_FINE__A, 15);
	if (status < 0)
		goto error;
	status = write16(state, SCU_RAM_QAM_LC_CA_COARSE__A, 40);
	if (status < 0)
		goto error;
	status = write16(state, SCU_RAM_QAM_LC_EP_FINE__A, 12);
	if (status < 0)
		goto error;
	status = write16(state, SCU_RAM_QAM_LC_EP_MEDIUM__A, 24);
	if (status < 0)
		goto error;
	status = write16(state, SCU_RAM_QAM_LC_EP_COARSE__A, 24);
	if (status < 0)
		goto error;
	status = write16(state, SCU_RAM_QAM_LC_EI_FINE__A, 12);
	if (status < 0)
		goto error;
	status = write16(state, SCU_RAM_QAM_LC_EI_MEDIUM__A, 16);
	if (status < 0)
		goto error;
	status = write16(state, SCU_RAM_QAM_LC_EI_COARSE__A, 16);
	if (status < 0)
		goto error;

	status = write16(state, SCU_RAM_QAM_LC_CP_FINE__A, 5);
	if (status < 0)
		goto error;
	status = write16(state, SCU_RAM_QAM_LC_CP_MEDIUM__A, 40);
	if (status < 0)
		goto error;
	status = write16(state, SCU_RAM_QAM_LC_CP_COARSE__A, 120);
	if (status < 0)
		goto error;
	status = write16(state, SCU_RAM_QAM_LC_CI_FINE__A, 5);
	if (status < 0)
		goto error;
	status = write16(state, SCU_RAM_QAM_LC_CI_MEDIUM__A, 40);
	if (status < 0)
		goto error;
	status = write16(state, SCU_RAM_QAM_LC_CI_COARSE__A, 60);
	if (status < 0)
		goto error;
	status = write16(state, SCU_RAM_QAM_LC_CF_FINE__A, 16);
	if (status < 0)
		goto error;
	status = write16(state, SCU_RAM_QAM_LC_CF_MEDIUM__A, 25);
	if (status < 0)
		goto error;
	status = write16(state, SCU_RAM_QAM_LC_CF_COARSE__A, 64);
	if (status < 0)
		goto error;
	status = write16(state, SCU_RAM_QAM_LC_CF1_FINE__A, 5);
	if (status < 0)
		goto error;
	status = write16(state, SCU_RAM_QAM_LC_CF1_MEDIUM__A, 10);
	if (status < 0)
		goto error;
	status = write16(state, SCU_RAM_QAM_LC_CF1_COARSE__A, 0);
	if (status < 0)
		goto error;
4998 4999


5000
	/* QAM State Machine (FSM) Thresholds */
5001

5002 5003 5004 5005 5006 5007 5008 5009 5010 5011 5012 5013 5014 5015 5016 5017 5018 5019
	status = write16(state, SCU_RAM_QAM_FSM_RTH__A, 50);
	if (status < 0)
		goto error;
	status = write16(state, SCU_RAM_QAM_FSM_FTH__A, 60);
	if (status < 0)
		goto error;
	status = write16(state, SCU_RAM_QAM_FSM_CTH__A, 80);
	if (status < 0)
		goto error;
	status = write16(state, SCU_RAM_QAM_FSM_PTH__A, 100);
	if (status < 0)
		goto error;
	status = write16(state, SCU_RAM_QAM_FSM_QTH__A, 140);
	if (status < 0)
		goto error;
	status = write16(state, SCU_RAM_QAM_FSM_MTH__A, 100);
	if (status < 0)
		goto error;
5020

5021 5022 5023 5024 5025 5026
	status = write16(state, SCU_RAM_QAM_FSM_RATE_LIM__A, 40);
	if (status < 0)
		goto error;
	status = write16(state, SCU_RAM_QAM_FSM_COUNT_LIM__A, 5);
	if (status < 0)
		goto error;
5027

5028 5029 5030
	status = write16(state, SCU_RAM_QAM_FSM_FREQ_LIM__A, 12);
	if (status < 0)
		goto error;
5031

5032
	/* QAM FSM Tracking Parameters */
5033

5034 5035 5036 5037 5038 5039 5040 5041 5042 5043 5044 5045 5046 5047 5048 5049 5050 5051 5052 5053 5054 5055
	status = write16(state, SCU_RAM_QAM_FSM_MEDIAN_AV_MULT__A, (u16) 8);
	if (status < 0)
		goto error;
	status = write16(state, SCU_RAM_QAM_FSM_RADIUS_AV_LIMIT__A, (u16) 65);
	if (status < 0)
		goto error;
	status = write16(state, SCU_RAM_QAM_FSM_LCAVG_OFFSET1__A, (u16) 5);
	if (status < 0)
		goto error;
	status = write16(state, SCU_RAM_QAM_FSM_LCAVG_OFFSET2__A, (u16) 3);
	if (status < 0)
		goto error;
	status = write16(state, SCU_RAM_QAM_FSM_LCAVG_OFFSET3__A, (u16) -1);
	if (status < 0)
		goto error;
	status = write16(state, SCU_RAM_QAM_FSM_LCAVG_OFFSET4__A, (u16) -12);
	if (status < 0)
		goto error;
	status = write16(state, SCU_RAM_QAM_FSM_LCAVG_OFFSET5__A, (u16) -23);
error:
	if (status < 0)
		printk(KERN_ERR "drxk: Error %d on %s\n", status, __func__);
5056 5057

	return status;
R
Ralph Metzler 已提交
5058 5059 5060 5061 5062 5063 5064 5065 5066 5067 5068
}

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

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

5071
	dprintk(1, "\n");
5072 5073 5074 5075 5076 5077 5078 5079 5080 5081 5082 5083 5084 5085 5086 5087 5088 5089 5090 5091
	/* QAM Equalizer Setup */
	/* Equalizer */
	status = write16(state, SCU_RAM_QAM_EQ_CMA_RAD0__A, 11502);
	if (status < 0)
		goto error;
	status = write16(state, SCU_RAM_QAM_EQ_CMA_RAD1__A, 12084);
	if (status < 0)
		goto error;
	status = write16(state, SCU_RAM_QAM_EQ_CMA_RAD2__A, 12543);
	if (status < 0)
		goto error;
	status = write16(state, SCU_RAM_QAM_EQ_CMA_RAD3__A, 12931);
	if (status < 0)
		goto error;
	status = write16(state, SCU_RAM_QAM_EQ_CMA_RAD4__A, 13629);
	if (status < 0)
		goto error;
	status = write16(state, SCU_RAM_QAM_EQ_CMA_RAD5__A, 15385);
	if (status < 0)
		goto error;
5092

5093 5094 5095 5096 5097 5098 5099 5100 5101 5102 5103 5104 5105 5106 5107 5108 5109 5110 5111
	/* Decision Feedback Equalizer */
	status = write16(state, QAM_DQ_QUAL_FUN0__A, 8);
	if (status < 0)
		goto error;
	status = write16(state, QAM_DQ_QUAL_FUN1__A, 8);
	if (status < 0)
		goto error;
	status = write16(state, QAM_DQ_QUAL_FUN2__A, 8);
	if (status < 0)
		goto error;
	status = write16(state, QAM_DQ_QUAL_FUN3__A, 8);
	if (status < 0)
		goto error;
	status = write16(state, QAM_DQ_QUAL_FUN4__A, 6);
	if (status < 0)
		goto error;
	status = write16(state, QAM_DQ_QUAL_FUN5__A, 0);
	if (status < 0)
		goto error;
5112

5113 5114 5115 5116 5117 5118 5119 5120 5121
	status = write16(state, QAM_SY_SYNC_HWM__A, 5);
	if (status < 0)
		goto error;
	status = write16(state, QAM_SY_SYNC_AWM__A, 4);
	if (status < 0)
		goto error;
	status = write16(state, QAM_SY_SYNC_LWM__A, 3);
	if (status < 0)
		goto error;
5122

5123
	/* QAM Slicer Settings */
5124

5125 5126 5127
	status = write16(state, SCU_RAM_QAM_SL_SIG_POWER__A, DRXK_QAM_SL_SIG_POWER_QAM256);
	if (status < 0)
		goto error;
5128 5129


5130
	/* QAM Loop Controller Coeficients */
5131

5132 5133 5134 5135 5136 5137 5138 5139 5140 5141 5142 5143 5144 5145 5146 5147 5148 5149 5150 5151 5152 5153 5154 5155
	status = write16(state, SCU_RAM_QAM_LC_CA_FINE__A, 15);
	if (status < 0)
		goto error;
	status = write16(state, SCU_RAM_QAM_LC_CA_COARSE__A, 40);
	if (status < 0)
		goto error;
	status = write16(state, SCU_RAM_QAM_LC_EP_FINE__A, 12);
	if (status < 0)
		goto error;
	status = write16(state, SCU_RAM_QAM_LC_EP_MEDIUM__A, 24);
	if (status < 0)
		goto error;
	status = write16(state, SCU_RAM_QAM_LC_EP_COARSE__A, 24);
	if (status < 0)
		goto error;
	status = write16(state, SCU_RAM_QAM_LC_EI_FINE__A, 12);
	if (status < 0)
		goto error;
	status = write16(state, SCU_RAM_QAM_LC_EI_MEDIUM__A, 16);
	if (status < 0)
		goto error;
	status = write16(state, SCU_RAM_QAM_LC_EI_COARSE__A, 16);
	if (status < 0)
		goto error;
5156

5157 5158 5159 5160 5161 5162 5163 5164 5165 5166 5167 5168 5169 5170 5171 5172 5173 5174 5175 5176 5177 5178 5179 5180 5181 5182 5183 5184 5185 5186 5187 5188 5189 5190 5191 5192
	status = write16(state, SCU_RAM_QAM_LC_CP_FINE__A, 5);
	if (status < 0)
		goto error;
	status = write16(state, SCU_RAM_QAM_LC_CP_MEDIUM__A, 50);
	if (status < 0)
		goto error;
	status = write16(state, SCU_RAM_QAM_LC_CP_COARSE__A, 250);
	if (status < 0)
		goto error;
	status = write16(state, SCU_RAM_QAM_LC_CI_FINE__A, 5);
	if (status < 0)
		goto error;
	status = write16(state, SCU_RAM_QAM_LC_CI_MEDIUM__A, 50);
	if (status < 0)
		goto error;
	status = write16(state, SCU_RAM_QAM_LC_CI_COARSE__A, 125);
	if (status < 0)
		goto error;
	status = write16(state, SCU_RAM_QAM_LC_CF_FINE__A, 16);
	if (status < 0)
		goto error;
	status = write16(state, SCU_RAM_QAM_LC_CF_MEDIUM__A, 25);
	if (status < 0)
		goto error;
	status = write16(state, SCU_RAM_QAM_LC_CF_COARSE__A, 48);
	if (status < 0)
		goto error;
	status = write16(state, SCU_RAM_QAM_LC_CF1_FINE__A, 5);
	if (status < 0)
		goto error;
	status = write16(state, SCU_RAM_QAM_LC_CF1_MEDIUM__A, 10);
	if (status < 0)
		goto error;
	status = write16(state, SCU_RAM_QAM_LC_CF1_COARSE__A, 10);
	if (status < 0)
		goto error;
5193 5194


5195
	/* QAM State Machine (FSM) Thresholds */
5196

5197 5198 5199 5200 5201 5202 5203 5204 5205 5206 5207 5208 5209 5210 5211 5212 5213 5214
	status = write16(state, SCU_RAM_QAM_FSM_RTH__A, 50);
	if (status < 0)
		goto error;
	status = write16(state, SCU_RAM_QAM_FSM_FTH__A, 60);
	if (status < 0)
		goto error;
	status = write16(state, SCU_RAM_QAM_FSM_CTH__A, 80);
	if (status < 0)
		goto error;
	status = write16(state, SCU_RAM_QAM_FSM_PTH__A, 100);
	if (status < 0)
		goto error;
	status = write16(state, SCU_RAM_QAM_FSM_QTH__A, 150);
	if (status < 0)
		goto error;
	status = write16(state, SCU_RAM_QAM_FSM_MTH__A, 110);
	if (status < 0)
		goto error;
5215

5216 5217 5218 5219 5220 5221 5222 5223 5224
	status = write16(state, SCU_RAM_QAM_FSM_RATE_LIM__A, 40);
	if (status < 0)
		goto error;
	status = write16(state, SCU_RAM_QAM_FSM_COUNT_LIM__A, 4);
	if (status < 0)
		goto error;
	status = write16(state, SCU_RAM_QAM_FSM_FREQ_LIM__A, 12);
	if (status < 0)
		goto error;
5225 5226


5227
	/* QAM FSM Tracking Parameters */
5228

5229 5230 5231 5232 5233 5234 5235 5236 5237 5238 5239 5240 5241 5242 5243 5244 5245 5246 5247 5248 5249 5250
	status = write16(state, SCU_RAM_QAM_FSM_MEDIAN_AV_MULT__A, (u16) 8);
	if (status < 0)
		goto error;
	status = write16(state, SCU_RAM_QAM_FSM_RADIUS_AV_LIMIT__A, (u16) 74);
	if (status < 0)
		goto error;
	status = write16(state, SCU_RAM_QAM_FSM_LCAVG_OFFSET1__A, (u16) 18);
	if (status < 0)
		goto error;
	status = write16(state, SCU_RAM_QAM_FSM_LCAVG_OFFSET2__A, (u16) 13);
	if (status < 0)
		goto error;
	status = write16(state, SCU_RAM_QAM_FSM_LCAVG_OFFSET3__A, (u16) 7);
	if (status < 0)
		goto error;
	status = write16(state, SCU_RAM_QAM_FSM_LCAVG_OFFSET4__A, (u16) 0);
	if (status < 0)
		goto error;
	status = write16(state, SCU_RAM_QAM_FSM_LCAVG_OFFSET5__A, (u16) -8);
error:
	if (status < 0)
		printk(KERN_ERR "drxk: Error %d on %s\n", status, __func__);
5251
	return status;
R
Ralph Metzler 已提交
5252 5253 5254 5255 5256 5257 5258 5259 5260 5261 5262 5263
}


/*============================================================================*/
/**
* \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)
{
5264 5265
	int status;
	u16 cmdResult;
R
Ralph Metzler 已提交
5266

5267
	dprintk(1, "\n");
5268 5269 5270 5271
	/* Stop QAM comstate->m_exec */
	status = write16(state, QAM_COMM_EXEC__A, QAM_COMM_EXEC_STOP);
	if (status < 0)
		goto error;
R
Ralph Metzler 已提交
5272

5273 5274 5275 5276
	status = scu_command(state, SCU_RAM_COMMAND_STANDARD_QAM | SCU_RAM_COMMAND_CMD_DEMOD_RESET, 0, NULL, 1, &cmdResult);
error:
	if (status < 0)
		printk(KERN_ERR "drxk: Error %d on %s\n", status, __func__);
5277
	return status;
R
Ralph Metzler 已提交
5278 5279 5280 5281 5282 5283 5284 5285 5286 5287 5288 5289
}

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

/**
* \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)
{
5290 5291 5292 5293 5294 5295 5296
	u32 adcFrequency = 0;
	u32 symbFreq = 0;
	u32 iqmRcRate = 0;
	u16 ratesel = 0;
	u32 lcSymbRate = 0;
	int status;

5297
	dprintk(1, "\n");
5298 5299 5300
	/* Select & calculate correct IQM rate */
	adcFrequency = (state->m_sysClockFreq * 1000) / 3;
	ratesel = 0;
5301 5302
	/* printk(KERN_DEBUG "drxk: SR %d\n", state->props.symbol_rate); */
	if (state->props.symbol_rate <= 1188750)
5303
		ratesel = 3;
5304
	else if (state->props.symbol_rate <= 2377500)
5305
		ratesel = 2;
5306
	else if (state->props.symbol_rate <= 4755000)
5307 5308 5309 5310
		ratesel = 1;
	status = write16(state, IQM_FD_RATESEL__A, ratesel);
	if (status < 0)
		goto error;
5311

5312 5313 5314
	/*
		IqmRcRate = ((Fadc / (symbolrate * (4<<ratesel))) - 1) * (1<<23)
		*/
5315
	symbFreq = state->props.symbol_rate * (1 << ratesel);
5316 5317 5318 5319 5320 5321 5322 5323 5324 5325 5326 5327 5328 5329 5330
	if (symbFreq == 0) {
		/* Divide by zero */
		status = -EINVAL;
		goto error;
	}
	iqmRcRate = (adcFrequency / symbFreq) * (1 << 21) +
		(Frac28a((adcFrequency % symbFreq), symbFreq) >> 7) -
		(1 << 23);
	status = write32(state, IQM_RC_RATE_OFS_LO__A, iqmRcRate);
	if (status < 0)
		goto error;
	state->m_iqmRcRate = iqmRcRate;
	/*
		LcSymbFreq = round (.125 *  symbolrate / adcFreq * (1<<15))
		*/
5331
	symbFreq = state->props.symbol_rate;
5332 5333 5334 5335 5336 5337 5338 5339 5340 5341 5342 5343 5344 5345 5346
	if (adcFrequency == 0) {
		/* Divide by zero */
		status = -EINVAL;
		goto error;
	}
	lcSymbRate = (symbFreq / adcFrequency) * (1 << 12) +
		(Frac28a((symbFreq % adcFrequency), adcFrequency) >>
		16);
	if (lcSymbRate > 511)
		lcSymbRate = 511;
	status = write16(state, QAM_LC_SYMBOL_FREQ__A, (u16) lcSymbRate);

error:
	if (status < 0)
		printk(KERN_ERR "drxk: Error %d on %s\n", status, __func__);
5347
	return status;
R
Ralph Metzler 已提交
5348 5349 5350 5351 5352 5353 5354 5355 5356 5357 5358 5359 5360 5361
}

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

/**
* \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;
5362
	u16 Result[2] = { 0, 0 };
R
Ralph Metzler 已提交
5363

5364
	dprintk(1, "\n");
5365 5366
	*pLockStatus = NOT_LOCKED;
	status = scu_command(state,
5367 5368 5369 5370
			SCU_RAM_COMMAND_STANDARD_QAM |
			SCU_RAM_COMMAND_CMD_DEMOD_GET_LOCK, 0, NULL, 2,
			Result);
	if (status < 0)
5371
		printk(KERN_ERR "drxk: Error %d on %s\n", status, __func__);
5372 5373

	if (Result[1] < SCU_RAM_QAM_LOCKED_LOCKED_DEMOD_LOCKED) {
R
Ralph Metzler 已提交
5374
		/* 0x0000 NOT LOCKED */
5375
	} else if (Result[1] < SCU_RAM_QAM_LOCKED_LOCKED_LOCKED) {
R
Ralph Metzler 已提交
5376 5377
		/* 0x4000 DEMOD LOCKED */
		*pLockStatus = DEMOD_LOCK;
5378
	} else if (Result[1] < SCU_RAM_QAM_LOCKED_LOCKED_NEVER_LOCK) {
R
Ralph Metzler 已提交
5379 5380
		/* 0x8000 DEMOD + FEC LOCKED (system lock) */
		*pLockStatus = MPEG_LOCK;
5381
	} else {
R
Ralph Metzler 已提交
5382 5383 5384 5385 5386 5387 5388 5389 5390 5391 5392 5393 5394 5395 5396 5397
		/* 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

5398 5399 5400 5401 5402 5403 5404 5405 5406 5407 5408 5409 5410 5411 5412 5413 5414 5415 5416 5417 5418 5419 5420 5421 5422 5423 5424 5425 5426 5427 5428 5429 5430 5431 5432 5433 5434 5435 5436 5437 5438 5439 5440 5441 5442 5443
static int QAMDemodulatorCommand(struct drxk_state *state,
				 int numberOfParameters)
{
	int status;
	u16 cmdResult;
	u16 setParamParameters[4] = { 0, 0, 0, 0 };

	setParamParameters[0] = state->m_Constellation;	/* modulation     */
	setParamParameters[1] = DRXK_QAM_I12_J17;	/* interleave mode   */

	if (numberOfParameters == 2) {
		u16 setEnvParameters[1] = { 0 };

		if (state->m_OperationMode == OM_QAM_ITU_C)
			setEnvParameters[0] = QAM_TOP_ANNEX_C;
		else
			setEnvParameters[0] = QAM_TOP_ANNEX_A;

		status = scu_command(state,
				     SCU_RAM_COMMAND_STANDARD_QAM | SCU_RAM_COMMAND_CMD_DEMOD_SET_ENV,
				     1, setEnvParameters, 1, &cmdResult);
		if (status < 0)
			goto error;

		status = scu_command(state,
				     SCU_RAM_COMMAND_STANDARD_QAM | SCU_RAM_COMMAND_CMD_DEMOD_SET_PARAM,
				     numberOfParameters, setParamParameters,
				     1, &cmdResult);
	} else if (numberOfParameters == 4) {
		if (state->m_OperationMode == OM_QAM_ITU_C)
			setParamParameters[2] = QAM_TOP_ANNEX_C;
		else
			setParamParameters[2] = QAM_TOP_ANNEX_A;

		setParamParameters[3] |= (QAM_MIRROR_AUTO_ON);
		/* Env parameters */
		/* check for LOCKRANGE Extented */
		/* setParamParameters[3] |= QAM_LOCKRANGE_NORMAL; */

		status = scu_command(state,
				     SCU_RAM_COMMAND_STANDARD_QAM | SCU_RAM_COMMAND_CMD_DEMOD_SET_PARAM,
				     numberOfParameters, setParamParameters,
				     1, &cmdResult);
	} else {
		printk(KERN_WARNING "drxk: Unknown QAM demodulator parameter "
			"count %d\n", numberOfParameters);
5444
		status = -EINVAL;
5445 5446 5447 5448 5449 5450 5451 5452 5453
	}

error:
	if (status < 0)
		printk(KERN_WARNING "drxk: Warning %d on %s\n",
		       status, __func__);
	return status;
}

5454 5455
static int SetQAM(struct drxk_state *state, u16 IntermediateFreqkHz,
		  s32 tunerFreqOffset)
R
Ralph Metzler 已提交
5456
{
5457
	int status;
5458
	u16 cmdResult;
5459
	int qamDemodParamCount = state->qam_demod_parameter_count;
R
Ralph Metzler 已提交
5460

5461
	dprintk(1, "\n");
5462
	/*
5463 5464 5465 5466 5467
	 * STEP 1: reset demodulator
	 *	resets FEC DI and FEC RS
	 *	resets QAM block
	 *	resets SCU variables
	 */
5468 5469 5470 5471 5472 5473 5474 5475 5476
	status = write16(state, FEC_DI_COMM_EXEC__A, FEC_DI_COMM_EXEC_STOP);
	if (status < 0)
		goto error;
	status = write16(state, FEC_RS_COMM_EXEC__A, FEC_RS_COMM_EXEC_STOP);
	if (status < 0)
		goto error;
	status = QAMResetQAM(state);
	if (status < 0)
		goto error;
R
Ralph Metzler 已提交
5477

5478
	/*
5479 5480 5481 5482
	 * STEP 2: configure demodulator
	 *	-set params; resets IQM,QAM,FEC HW; initializes some
	 *       SCU variables
	 */
5483 5484 5485 5486 5487
	status = QAMSetSymbolrate(state);
	if (status < 0)
		goto error;

	/* Set params */
5488
	switch (state->props.modulation) {
5489 5490 5491 5492 5493 5494 5495 5496 5497 5498 5499 5500 5501 5502 5503 5504 5505 5506 5507 5508 5509 5510
	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;
	}
	if (status < 0)
		goto error;
R
Ralph Metzler 已提交
5511

5512 5513 5514 5515 5516 5517 5518
	/* Use the 4-parameter if it's requested or we're probing for
	 * the correct command. */
	if (state->qam_demod_parameter_count == 4
		|| !state->qam_demod_parameter_count) {
		qamDemodParamCount = 4;
		status = QAMDemodulatorCommand(state, qamDemodParamCount);
	}
R
Ralph Metzler 已提交
5519

5520 5521 5522 5523 5524 5525 5526
	/* Use the 2-parameter command if it was requested or if we're
	 * probing for the correct command and the 4-parameter command
	 * failed. */
	if (state->qam_demod_parameter_count == 2
		|| (!state->qam_demod_parameter_count && status < 0)) {
		qamDemodParamCount = 2;
		status = QAMDemodulatorCommand(state, qamDemodParamCount);
5527
	}
5528 5529 5530 5531 5532 5533 5534

	if (status < 0) {
		dprintk(1, "Could not set demodulator parameters. Make "
			"sure qam_demod_parameter_count (%d) is correct for "
			"your firmware (%s).\n",
			state->qam_demod_parameter_count,
			state->microcode_name);
5535
		goto error;
5536 5537 5538 5539 5540
	} else if (!state->qam_demod_parameter_count) {
		dprintk(1, "Auto-probing the correct QAM demodulator command "
			"parameters was successful - using %d parameters.\n",
			qamDemodParamCount);

5541 5542 5543 5544
		/*
		 * One of our commands was successful. We don't need to
		 * auto-probe anymore, now that we got the correct command.
		 */
5545 5546
		state->qam_demod_parameter_count = qamDemodParamCount;
	}
R
Ralph Metzler 已提交
5547

5548 5549
	/*
	 * STEP 3: enable the system in a mode where the ADC provides valid
5550
	 * signal setup modulation independent registers
5551
	 */
5552
#if 0
5553 5554 5555
	status = SetFrequency(channel, tunerFreqOffset));
	if (status < 0)
		goto error;
5556
#endif
5557 5558 5559
	status = SetFrequencyShifter(state, IntermediateFreqkHz, tunerFreqOffset, true);
	if (status < 0)
		goto error;
R
Ralph Metzler 已提交
5560

5561
	/* Setup BER measurement */
5562
	status = SetQAMMeasurement(state, state->m_Constellation, state->props.symbol_rate);
5563 5564
	if (status < 0)
		goto error;
5565

5566 5567 5568 5569 5570 5571 5572
	/* Reset default values */
	status = write16(state, IQM_CF_SCALE_SH__A, IQM_CF_SCALE_SH__PRE);
	if (status < 0)
		goto error;
	status = write16(state, QAM_SY_TIMEOUT__A, QAM_SY_TIMEOUT__PRE);
	if (status < 0)
		goto error;
5573

5574 5575 5576 5577 5578 5579 5580 5581 5582 5583 5584 5585 5586
	/* Reset default LC values */
	status = write16(state, QAM_LC_RATE_LIMIT__A, 3);
	if (status < 0)
		goto error;
	status = write16(state, QAM_LC_LPF_FACTORP__A, 4);
	if (status < 0)
		goto error;
	status = write16(state, QAM_LC_LPF_FACTORI__A, 4);
	if (status < 0)
		goto error;
	status = write16(state, QAM_LC_MODE__A, 7);
	if (status < 0)
		goto error;
5587

5588 5589 5590 5591 5592 5593 5594 5595 5596 5597 5598 5599 5600 5601 5602 5603 5604 5605 5606 5607 5608 5609 5610 5611 5612 5613 5614 5615 5616 5617 5618 5619 5620 5621 5622 5623 5624 5625 5626 5627 5628 5629 5630 5631 5632
	status = write16(state, QAM_LC_QUAL_TAB0__A, 1);
	if (status < 0)
		goto error;
	status = write16(state, QAM_LC_QUAL_TAB1__A, 1);
	if (status < 0)
		goto error;
	status = write16(state, QAM_LC_QUAL_TAB2__A, 1);
	if (status < 0)
		goto error;
	status = write16(state, QAM_LC_QUAL_TAB3__A, 1);
	if (status < 0)
		goto error;
	status = write16(state, QAM_LC_QUAL_TAB4__A, 2);
	if (status < 0)
		goto error;
	status = write16(state, QAM_LC_QUAL_TAB5__A, 2);
	if (status < 0)
		goto error;
	status = write16(state, QAM_LC_QUAL_TAB6__A, 2);
	if (status < 0)
		goto error;
	status = write16(state, QAM_LC_QUAL_TAB8__A, 2);
	if (status < 0)
		goto error;
	status = write16(state, QAM_LC_QUAL_TAB9__A, 2);
	if (status < 0)
		goto error;
	status = write16(state, QAM_LC_QUAL_TAB10__A, 2);
	if (status < 0)
		goto error;
	status = write16(state, QAM_LC_QUAL_TAB12__A, 2);
	if (status < 0)
		goto error;
	status = write16(state, QAM_LC_QUAL_TAB15__A, 3);
	if (status < 0)
		goto error;
	status = write16(state, QAM_LC_QUAL_TAB16__A, 3);
	if (status < 0)
		goto error;
	status = write16(state, QAM_LC_QUAL_TAB20__A, 4);
	if (status < 0)
		goto error;
	status = write16(state, QAM_LC_QUAL_TAB25__A, 4);
	if (status < 0)
		goto error;
5633

5634 5635 5636 5637
	/* Mirroring, QAM-block starting point not inverted */
	status = write16(state, QAM_SY_SP_INV__A, QAM_SY_SP_INV_SPECTRUM_INV_DIS);
	if (status < 0)
		goto error;
5638

5639 5640 5641 5642
	/* Halt SCU to enable safe non-atomic accesses */
	status = write16(state, SCU_COMM_EXEC__A, SCU_COMM_EXEC_HOLD);
	if (status < 0)
		goto error;
5643

5644 5645
	/* STEP 4: modulation specific setup */
	switch (state->props.modulation) {
5646 5647 5648 5649 5650 5651 5652 5653 5654 5655 5656 5657 5658 5659 5660 5661 5662 5663 5664 5665 5666 5667
	case QAM_16:
		status = SetQAM16(state);
		break;
	case QAM_32:
		status = SetQAM32(state);
		break;
	case QAM_AUTO:
	case QAM_64:
		status = SetQAM64(state);
		break;
	case QAM_128:
		status = SetQAM128(state);
		break;
	case QAM_256:
		status = SetQAM256(state);
		break;
	default:
		status = -EINVAL;
		break;
	}
	if (status < 0)
		goto error;
5668

5669 5670 5671 5672
	/* Activate SCU to enable SCU commands */
	status = write16(state, SCU_COMM_EXEC__A, SCU_COMM_EXEC_ACTIVE);
	if (status < 0)
		goto error;
5673

5674
	/* Re-configure MPEG output, requires knowledge of channel bitrate */
5675
	/* extAttr->currentChannel.modulation = channel->modulation; */
5676 5677 5678 5679
	/* extAttr->currentChannel.symbolrate    = channel->symbolrate; */
	status = MPEGTSDtoSetup(state, state->m_OperationMode);
	if (status < 0)
		goto error;
5680

5681 5682 5683 5684 5685 5686 5687 5688 5689 5690 5691 5692 5693
	/* Start processes */
	status = MPEGTSStart(state);
	if (status < 0)
		goto error;
	status = write16(state, FEC_COMM_EXEC__A, FEC_COMM_EXEC_ACTIVE);
	if (status < 0)
		goto error;
	status = write16(state, QAM_COMM_EXEC__A, QAM_COMM_EXEC_ACTIVE);
	if (status < 0)
		goto error;
	status = write16(state, IQM_COMM_EXEC__A, IQM_COMM_EXEC_B_ACTIVE);
	if (status < 0)
		goto error;
5694

5695 5696 5697 5698
	/* STEP 5: start QAM demodulator (starts FEC, QAM and IQM HW) */
	status = scu_command(state, SCU_RAM_COMMAND_STANDARD_QAM | SCU_RAM_COMMAND_CMD_DEMOD_START, 0, NULL, 1, &cmdResult);
	if (status < 0)
		goto error;
5699

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

5703
error:
5704
	if (status < 0)
5705
		printk(KERN_ERR "drxk: Error %d on %s\n", status, __func__);
5706
	return status;
R
Ralph Metzler 已提交
5707 5708
}

5709 5710
static int SetQAMStandard(struct drxk_state *state,
			  enum OperationMode oMode)
R
Ralph Metzler 已提交
5711
{
5712
	int status;
R
Ralph Metzler 已提交
5713 5714 5715 5716 5717 5718
#ifdef DRXK_QAM_TAPS
#define DRXK_QAMA_TAPS_SELECT
#include "drxk_filters.h"
#undef DRXK_QAMA_TAPS_SELECT
#endif

5719 5720
	dprintk(1, "\n");

5721 5722
	/* added antenna switch */
	SwitchAntennaToQAM(state);
5723

5724 5725 5726 5727 5728 5729 5730 5731
	/* Ensure correct power-up mode */
	status = PowerUpQAM(state);
	if (status < 0)
		goto error;
	/* Reset QAM block */
	status = QAMResetQAM(state);
	if (status < 0)
		goto error;
5732

5733
	/* Setup IQM */
5734

5735 5736 5737 5738 5739 5740
	status = write16(state, IQM_COMM_EXEC__A, IQM_COMM_EXEC_B_STOP);
	if (status < 0)
		goto error;
	status = write16(state, IQM_AF_AMUX__A, IQM_AF_AMUX_SIGNAL2ADC);
	if (status < 0)
		goto error;
5741

5742 5743 5744 5745 5746 5747 5748 5749
	/* Upload IQM Channel Filter settings by
		boot loader from ROM table */
	switch (oMode) {
	case OM_QAM_ITU_A:
		status = BLChainCmd(state, DRXK_BL_ROM_OFFSET_TAPS_ITU_A, DRXK_BLCC_NR_ELEMENTS_TAPS, DRXK_BLC_TIMEOUT);
		break;
	case OM_QAM_ITU_C:
		status = BLDirectCmd(state, IQM_CF_TAP_RE0__A, DRXK_BL_ROM_OFFSET_TAPS_ITU_C, DRXK_BLDC_NR_ELEMENTS_TAPS, DRXK_BLC_TIMEOUT);
5750
		if (status < 0)
5751 5752 5753 5754 5755 5756 5757 5758
			goto error;
		status = BLDirectCmd(state, IQM_CF_TAP_IM0__A, DRXK_BL_ROM_OFFSET_TAPS_ITU_C, DRXK_BLDC_NR_ELEMENTS_TAPS, DRXK_BLC_TIMEOUT);
		break;
	default:
		status = -EINVAL;
	}
	if (status < 0)
		goto error;
5759

5760 5761 5762 5763 5764 5765 5766 5767 5768
	status = write16(state, IQM_CF_OUT_ENA__A, (1 << IQM_CF_OUT_ENA_QAM__B));
	if (status < 0)
		goto error;
	status = write16(state, IQM_CF_SYMMETRIC__A, 0);
	if (status < 0)
		goto error;
	status = write16(state, IQM_CF_MIDTAP__A, ((1 << IQM_CF_MIDTAP_RE__B) | (1 << IQM_CF_MIDTAP_IM__B)));
	if (status < 0)
		goto error;
5769

5770 5771 5772 5773 5774 5775 5776 5777 5778 5779 5780 5781 5782 5783 5784
	status = write16(state, IQM_RC_STRETCH__A, 21);
	if (status < 0)
		goto error;
	status = write16(state, IQM_AF_CLP_LEN__A, 0);
	if (status < 0)
		goto error;
	status = write16(state, IQM_AF_CLP_TH__A, 448);
	if (status < 0)
		goto error;
	status = write16(state, IQM_AF_SNS_LEN__A, 0);
	if (status < 0)
		goto error;
	status = write16(state, IQM_CF_POW_MEAS_LEN__A, 0);
	if (status < 0)
		goto error;
5785

5786 5787 5788 5789 5790 5791 5792 5793 5794 5795 5796 5797
	status = write16(state, IQM_FS_ADJ_SEL__A, 1);
	if (status < 0)
		goto error;
	status = write16(state, IQM_RC_ADJ_SEL__A, 1);
	if (status < 0)
		goto error;
	status = write16(state, IQM_CF_ADJ_SEL__A, 1);
	if (status < 0)
		goto error;
	status = write16(state, IQM_AF_UPD_SEL__A, 0);
	if (status < 0)
		goto error;
5798

5799 5800 5801 5802 5803 5804 5805 5806 5807 5808 5809 5810 5811 5812 5813 5814 5815 5816 5817 5818 5819 5820
	/* IQM Impulse Noise Processing Unit */
	status = write16(state, IQM_CF_CLP_VAL__A, 500);
	if (status < 0)
		goto error;
	status = write16(state, IQM_CF_DATATH__A, 1000);
	if (status < 0)
		goto error;
	status = write16(state, IQM_CF_BYPASSDET__A, 1);
	if (status < 0)
		goto error;
	status = write16(state, IQM_CF_DET_LCT__A, 0);
	if (status < 0)
		goto error;
	status = write16(state, IQM_CF_WND_LEN__A, 1);
	if (status < 0)
		goto error;
	status = write16(state, IQM_CF_PKDTH__A, 1);
	if (status < 0)
		goto error;
	status = write16(state, IQM_AF_INC_BYPASS__A, 1);
	if (status < 0)
		goto error;
5821

5822 5823 5824 5825 5826 5827 5828
	/* turn on IQMAF. Must be done before setAgc**() */
	status = SetIqmAf(state, true);
	if (status < 0)
		goto error;
	status = write16(state, IQM_AF_START_LOCK__A, 0x01);
	if (status < 0)
		goto error;
5829

5830 5831 5832 5833
	/* IQM will not be reset from here, sync ADC and update/init AGC */
	status = ADCSynchronization(state);
	if (status < 0)
		goto error;
5834

5835 5836 5837 5838
	/* Set the FSM step period */
	status = write16(state, SCU_RAM_QAM_FSM_STEP_PERIOD__A, 2000);
	if (status < 0)
		goto error;
5839

5840 5841 5842 5843
	/* Halt SCU to enable safe non-atomic accesses */
	status = write16(state, SCU_COMM_EXEC__A, SCU_COMM_EXEC_HOLD);
	if (status < 0)
		goto error;
5844

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

5848 5849 5850 5851 5852 5853
	status = InitAGC(state, true);
	if (status < 0)
		goto error;
	status = SetPreSaw(state, &(state->m_qamPreSawCfg));
	if (status < 0)
		goto error;
5854

5855 5856 5857 5858 5859 5860 5861
	/* Configure AGC's */
	status = SetAgcRf(state, &(state->m_qamRfAgcCfg), true);
	if (status < 0)
		goto error;
	status = SetAgcIf(state, &(state->m_qamIfAgcCfg), true);
	if (status < 0)
		goto error;
5862

5863 5864 5865 5866 5867
	/* Activate SCU to enable SCU commands */
	status = write16(state, SCU_COMM_EXEC__A, SCU_COMM_EXEC_ACTIVE);
error:
	if (status < 0)
		printk(KERN_ERR "drxk: Error %d on %s\n", status, __func__);
5868
	return status;
R
Ralph Metzler 已提交
5869 5870 5871 5872
}

static int WriteGPIO(struct drxk_state *state)
{
5873 5874 5875
	int status;
	u16 value = 0;

5876
	dprintk(1, "\n");
5877 5878 5879 5880
	/* stop lock indicator process */
	status = write16(state, SCU_RAM_GPIO__A, SCU_RAM_GPIO_HW_LOCK_IND_DISABLE);
	if (status < 0)
		goto error;
5881

5882 5883 5884 5885
	/*  Write magic word to enable pdr reg write               */
	status = write16(state, SIO_TOP_COMM_KEY__A, SIO_TOP_COMM_KEY_KEY);
	if (status < 0)
		goto error;
5886

5887
	if (state->m_hasSAWSW) {
5888 5889 5890 5891 5892
		if (state->UIO_mask & 0x0001) { /* UIO-1 */
			/* write to io pad configuration register - output mode */
			status = write16(state, SIO_PDR_SMA_TX_CFG__A, state->m_GPIOCfg);
			if (status < 0)
				goto error;
5893

5894 5895 5896 5897 5898 5899 5900 5901 5902 5903 5904 5905 5906 5907 5908
			/* use corresponding bit in io data output registar */
			status = read16(state, SIO_PDR_UIO_OUT_LO__A, &value);
			if (status < 0)
				goto error;
			if ((state->m_GPIO & 0x0001) == 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 */
			status = write16(state, SIO_PDR_UIO_OUT_LO__A, value);
			if (status < 0)
				goto error;
		}
		if (state->UIO_mask & 0x0002) { /* UIO-2 */
			/* write to io pad configuration register - output mode */
A
Antti Palosaari 已提交
5909
			status = write16(state, SIO_PDR_SMA_RX_CFG__A, state->m_GPIOCfg);
5910 5911
			if (status < 0)
				goto error;
5912

5913 5914 5915 5916 5917 5918 5919 5920 5921 5922 5923 5924 5925 5926 5927
			/* use corresponding bit in io data output registar */
			status = read16(state, SIO_PDR_UIO_OUT_LO__A, &value);
			if (status < 0)
				goto error;
			if ((state->m_GPIO & 0x0002) == 0)
				value &= 0xBFFF;	/* write zero to 14th bit - 2st UIO */
			else
				value |= 0x4000;	/* write one to 14th bit - 2st UIO */
			/* write back to io data output register */
			status = write16(state, SIO_PDR_UIO_OUT_LO__A, value);
			if (status < 0)
				goto error;
		}
		if (state->UIO_mask & 0x0004) { /* UIO-3 */
			/* write to io pad configuration register - output mode */
A
Antti Palosaari 已提交
5928
			status = write16(state, SIO_PDR_GPIO_CFG__A, state->m_GPIOCfg);
5929 5930 5931 5932 5933 5934 5935 5936 5937 5938 5939 5940 5941 5942 5943 5944
			if (status < 0)
				goto error;

			/* use corresponding bit in io data output registar */
			status = read16(state, SIO_PDR_UIO_OUT_LO__A, &value);
			if (status < 0)
				goto error;
			if ((state->m_GPIO & 0x0004) == 0)
				value &= 0xFFFB;            /* write zero to 2nd bit - 3rd UIO */
			else
				value |= 0x0004;            /* write one to 2nd bit - 3rd UIO */
			/* write back to io data output register */
			status = write16(state, SIO_PDR_UIO_OUT_LO__A, value);
			if (status < 0)
				goto error;
		}
5945 5946 5947 5948 5949 5950
	}
	/*  Write magic word to disable pdr reg write               */
	status = write16(state, SIO_TOP_COMM_KEY__A, 0x0000);
error:
	if (status < 0)
		printk(KERN_ERR "drxk: Error %d on %s\n", status, __func__);
5951
	return status;
R
Ralph Metzler 已提交
5952 5953 5954 5955
}

static int SwitchAntennaToQAM(struct drxk_state *state)
{
5956
	int status = 0;
5957
	bool gpio_state;
5958

5959
	dprintk(1, "\n");
5960

5961 5962 5963 5964 5965 5966 5967 5968 5969 5970 5971 5972
	if (!state->antenna_gpio)
		return 0;

	gpio_state = state->m_GPIO & state->antenna_gpio;

	if (state->antenna_dvbt ^ gpio_state) {
		/* Antenna is on DVB-T mode. Switch */
		if (state->antenna_dvbt)
			state->m_GPIO &= ~state->antenna_gpio;
		else
			state->m_GPIO |= state->antenna_gpio;
		status = WriteGPIO(state);
5973
	}
5974 5975
	if (status < 0)
		printk(KERN_ERR "drxk: Error %d on %s\n", status, __func__);
5976
	return status;
R
Ralph Metzler 已提交
5977 5978 5979 5980
}

static int SwitchAntennaToDVBT(struct drxk_state *state)
{
5981
	int status = 0;
5982
	bool gpio_state;
5983

5984
	dprintk(1, "\n");
5985 5986 5987 5988 5989 5990 5991 5992 5993 5994 5995 5996 5997

	if (!state->antenna_gpio)
		return 0;

	gpio_state = state->m_GPIO & state->antenna_gpio;

	if (!(state->antenna_dvbt ^ gpio_state)) {
		/* Antenna is on DVB-C mode. Switch */
		if (state->antenna_dvbt)
			state->m_GPIO |= state->antenna_gpio;
		else
			state->m_GPIO &= ~state->antenna_gpio;
		status = WriteGPIO(state);
R
Ralph Metzler 已提交
5998
	}
5999 6000
	if (status < 0)
		printk(KERN_ERR "drxk: Error %d on %s\n", status, __func__);
R
Ralph Metzler 已提交
6001 6002 6003 6004 6005 6006 6007 6008 6009 6010 6011 6012 6013
	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;
6014 6015

	dprintk(1, "\n");
6016 6017 6018
	if (state->m_bPDownOpenBridge) {
		/* Open I2C bridge before power down of DRXK */
		status = ConfigureI2CBridge(state, true);
6019
		if (status < 0)
6020 6021 6022 6023 6024 6025
			goto error;
	}
	/* driver 0.9.0 */
	status = DVBTEnableOFDMTokenRing(state, false);
	if (status < 0)
		goto error;
R
Ralph Metzler 已提交
6026

6027
	status = write16(state, SIO_CC_PWD_MODE__A, SIO_CC_PWD_MODE_LEVEL_CLOCK);
6028
	if (status < 0)
6029 6030 6031 6032 6033 6034 6035 6036 6037
		goto error;
	status = write16(state, SIO_CC_UPDATE__A, SIO_CC_UPDATE_KEY);
	if (status < 0)
		goto error;
	state->m_HICfgCtrl |= SIO_HI_RA_RAM_PAR_5_CFG_SLEEP_ZZZ;
	status = HI_CfgCommand(state);
error:
	if (status < 0)
		printk(KERN_ERR "drxk: Error %d on %s\n", status, __func__);
6038

6039
	return status;
R
Ralph Metzler 已提交
6040 6041 6042 6043
}

static int init_drxk(struct drxk_state *state)
{
6044
	int status = 0, n = 0;
6045
	enum DRXPowerMode powerMode = DRXK_POWER_DOWN_OFDM;
R
Ralph Metzler 已提交
6046 6047
	u16 driverVersion;

6048
	dprintk(1, "\n");
R
Ralph Metzler 已提交
6049
	if ((state->m_DrxkState == DRXK_UNINITIALIZED)) {
6050
		drxk_i2c_lock(state);
6051 6052 6053 6054 6055 6056 6057 6058 6059 6060 6061 6062 6063 6064 6065 6066 6067 6068 6069 6070 6071 6072 6073 6074 6075 6076 6077 6078 6079
		status = PowerUpDevice(state);
		if (status < 0)
			goto error;
		status = DRXX_Open(state);
		if (status < 0)
			goto error;
		/* Soft reset of OFDM-, sys- and osc-clockdomain */
		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);
		if (status < 0)
			goto error;
		status = write16(state, SIO_CC_UPDATE__A, SIO_CC_UPDATE_KEY);
		if (status < 0)
			goto error;
		/* TODO is this needed, if yes how much delay in worst case scenario */
		msleep(1);
		state->m_DRXK_A3_PATCH_CODE = true;
		status = GetDeviceCapabilities(state);
		if (status < 0)
			goto error;

		/* Bridge delay, uses oscilator clock */
		/* Delay = (delay (nano seconds) * oscclk (kHz))/ 1000 */
		/* SDA brdige delay */
		state->m_HICfgBridgeDelay =
			(u16) ((state->m_oscClockFreq / 1000) *
				HI_I2C_BRIDGE_DELAY) / 1000;
		/* Clipping */
		if (state->m_HICfgBridgeDelay >
			SIO_HI_RA_RAM_PAR_3_CFG_DBL_SDA__M) {
6080
			state->m_HICfgBridgeDelay =
6081 6082 6083 6084 6085 6086 6087 6088 6089 6090 6091
				SIO_HI_RA_RAM_PAR_3_CFG_DBL_SDA__M;
		}
		/* SCL bridge delay, same as SDA for now */
		state->m_HICfgBridgeDelay +=
			state->m_HICfgBridgeDelay <<
			SIO_HI_RA_RAM_PAR_3_CFG_DBL_SCL__B;

		status = InitHI(state);
		if (status < 0)
			goto error;
		/* disable various processes */
R
Ralph Metzler 已提交
6092
#if NOA1ROM
6093 6094
		if (!(state->m_DRXK_A1_ROM_CODE)
			&& !(state->m_DRXK_A2_ROM_CODE))
R
Ralph Metzler 已提交
6095
#endif
6096 6097
		{
			status = write16(state, SCU_RAM_GPIO__A, SCU_RAM_GPIO_HW_LOCK_IND_DISABLE);
6098
			if (status < 0)
6099 6100
				goto error;
		}
R
Ralph Metzler 已提交
6101

6102 6103 6104 6105
		/* disable MPEG port */
		status = MPEGTSDisable(state);
		if (status < 0)
			goto error;
R
Ralph Metzler 已提交
6106

6107 6108 6109 6110 6111 6112 6113
		/* Stop AUD and SCU */
		status = write16(state, AUD_COMM_EXEC__A, AUD_COMM_EXEC_STOP);
		if (status < 0)
			goto error;
		status = write16(state, SCU_COMM_EXEC__A, SCU_COMM_EXEC_STOP);
		if (status < 0)
			goto error;
6114

6115 6116 6117 6118
		/* enable token-ring bus through OFDM block for possible ucode upload */
		status = write16(state, SIO_OFDM_SH_OFDM_RING_ENABLE__A, SIO_OFDM_SH_OFDM_RING_ENABLE_ON);
		if (status < 0)
			goto error;
R
Ralph Metzler 已提交
6119

6120 6121 6122 6123 6124 6125 6126
		/* include boot loader section */
		status = write16(state, SIO_BL_COMM_EXEC__A, SIO_BL_COMM_EXEC_ACTIVE);
		if (status < 0)
			goto error;
		status = BLChainCmd(state, 0, 6, 100);
		if (status < 0)
			goto error;
R
Ralph Metzler 已提交
6127

6128 6129 6130 6131 6132 6133
		if (state->fw) {
			status = DownloadMicrocode(state, state->fw->data,
						   state->fw->size);
			if (status < 0)
				goto error;
		}
R
Ralph Metzler 已提交
6134

6135 6136 6137 6138
		/* disable token-ring bus through OFDM block for possible ucode upload */
		status = write16(state, SIO_OFDM_SH_OFDM_RING_ENABLE__A, SIO_OFDM_SH_OFDM_RING_ENABLE_OFF);
		if (status < 0)
			goto error;
6139

6140 6141 6142 6143 6144 6145 6146 6147 6148
		/* Run SCU for a little while to initialize microcode version numbers */
		status = write16(state, SCU_COMM_EXEC__A, SCU_COMM_EXEC_ACTIVE);
		if (status < 0)
			goto error;
		status = DRXX_Open(state);
		if (status < 0)
			goto error;
		/* added for test */
		msleep(30);
R
Ralph Metzler 已提交
6149

6150 6151 6152 6153 6154 6155 6156 6157 6158 6159 6160 6161 6162 6163 6164 6165 6166 6167 6168 6169 6170 6171 6172 6173 6174 6175 6176 6177 6178 6179 6180 6181 6182 6183 6184 6185 6186 6187 6188 6189 6190 6191 6192 6193 6194 6195 6196 6197 6198 6199 6200 6201 6202 6203 6204 6205 6206 6207 6208 6209 6210 6211 6212 6213 6214 6215
		powerMode = DRXK_POWER_DOWN_OFDM;
		status = CtrlPowerMode(state, &powerMode);
		if (status < 0)
			goto error;

		/* 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.
			*/
		driverVersion =
			(((DRXK_VERSION_MAJOR / 100) % 10) << 12) +
			(((DRXK_VERSION_MAJOR / 10) % 10) << 8) +
			((DRXK_VERSION_MAJOR % 10) << 4) +
			(DRXK_VERSION_MINOR % 10);
		status = write16(state, SCU_RAM_DRIVER_VER_HI__A, driverVersion);
		if (status < 0)
			goto error;
		driverVersion =
			(((DRXK_VERSION_PATCH / 1000) % 10) << 12) +
			(((DRXK_VERSION_PATCH / 100) % 10) << 8) +
			(((DRXK_VERSION_PATCH / 10) % 10) << 4) +
			(DRXK_VERSION_PATCH % 10);
		status = write16(state, SCU_RAM_DRIVER_VER_LO__A, driverVersion);
		if (status < 0)
			goto error;

		printk(KERN_INFO "DRXK driver version %d.%d.%d\n",
			DRXK_VERSION_MAJOR, DRXK_VERSION_MINOR,
			DRXK_VERSION_PATCH);

		/* 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 */

		/* m_dvbtRfAgcCfg.speed = 3; */

		/* Reset driver debug flags to 0 */
		status = write16(state, SCU_RAM_DRIVER_DEBUG__A, 0);
		if (status < 0)
			goto error;
		/* driver 0.9.0 */
		/* Setup FEC OC:
			NOTE: No more full FEC resets allowed afterwards!! */
		status = write16(state, FEC_COMM_EXEC__A, FEC_COMM_EXEC_STOP);
		if (status < 0)
			goto error;
		/* MPEGTS functions are still the same */
		status = MPEGTSDtoInit(state);
		if (status < 0)
			goto error;
		status = MPEGTSStop(state);
		if (status < 0)
			goto error;
		status = MPEGTSConfigurePolarity(state);
		if (status < 0)
			goto error;
		status = MPEGTSConfigurePins(state, state->m_enableMPEGOutput);
		if (status < 0)
			goto error;
		/* added: configure GPIO */
		status = WriteGPIO(state);
		if (status < 0)
			goto error;
R
Ralph Metzler 已提交
6216

6217
		state->m_DrxkState = DRXK_STOPPED;
R
Ralph Metzler 已提交
6218

6219 6220
		if (state->m_bPowerDown) {
			status = PowerDownDevice(state);
6221
			if (status < 0)
6222 6223 6224
				goto error;
			state->m_DrxkState = DRXK_POWERED_DOWN;
		} else
6225
			state->m_DrxkState = DRXK_STOPPED;
6226 6227 6228 6229 6230 6231 6232 6233 6234 6235 6236 6237 6238 6239

		/* Initialize the supported delivery systems */
		n = 0;
		if (state->m_hasDVBC) {
			state->frontend.ops.delsys[n++] = SYS_DVBC_ANNEX_A;
			state->frontend.ops.delsys[n++] = SYS_DVBC_ANNEX_C;
			strlcat(state->frontend.ops.info.name, " DVB-C",
				sizeof(state->frontend.ops.info.name));
		}
		if (state->m_hasDVBT) {
			state->frontend.ops.delsys[n++] = SYS_DVBT;
			strlcat(state->frontend.ops.info.name, " DVB-T",
				sizeof(state->frontend.ops.info.name));
		}
6240
		drxk_i2c_unlock(state);
R
Ralph Metzler 已提交
6241
	}
6242
error:
6243
	if (status < 0) {
6244
		state->m_DrxkState = DRXK_NO_DEV;
6245
		drxk_i2c_unlock(state);
6246
		printk(KERN_ERR "drxk: Error %d on %s\n", status, __func__);
6247
	}
R
Ralph Metzler 已提交
6248

6249
	return status;
R
Ralph Metzler 已提交
6250 6251
}

6252 6253 6254 6255 6256
static void load_firmware_cb(const struct firmware *fw,
			     void *context)
{
	struct drxk_state *state = context;

6257
	dprintk(1, ": %s\n", fw ? "firmware loaded" : "firmware not loaded");
6258 6259 6260 6261 6262 6263 6264 6265 6266 6267 6268 6269 6270 6271 6272 6273 6274 6275 6276 6277 6278 6279 6280 6281 6282
	if (!fw) {
		printk(KERN_ERR
		       "drxk: Could not load firmware file %s.\n",
			state->microcode_name);
		printk(KERN_INFO
		       "drxk: Copy %s to your hotplug directory!\n",
			state->microcode_name);
		state->microcode_name = NULL;

		/*
		 * As firmware is now load asynchronous, it is not possible
		 * anymore to fail at frontend attach. We might silently
		 * return here, and hope that the driver won't crash.
		 * We might also change all DVB callbacks to return -ENODEV
		 * if the device is not initialized.
		 * As the DRX-K devices have their own internal firmware,
		 * let's just hope that it will match a firmware revision
		 * compatible with this driver and proceed.
		 */
	}
	state->fw = fw;

	init_drxk(state);
}

6283
static void drxk_release(struct dvb_frontend *fe)
R
Ralph Metzler 已提交
6284
{
6285 6286
	struct drxk_state *state = fe->demodulator_priv;

6287
	dprintk(1, "\n");
6288 6289 6290
	if (state->fw)
		release_firmware(state->fw);

R
Ralph Metzler 已提交
6291 6292 6293
	kfree(state);
}

6294
static int drxk_sleep(struct dvb_frontend *fe)
R
Ralph Metzler 已提交
6295
{
6296
	struct drxk_state *state = fe->demodulator_priv;
R
Ralph Metzler 已提交
6297

6298
	dprintk(1, "\n");
6299 6300 6301 6302 6303 6304

	if (state->m_DrxkState == DRXK_NO_DEV)
		return -ENODEV;
	if (state->m_DrxkState == DRXK_UNINITIALIZED)
		return 0;

R
Ralph Metzler 已提交
6305 6306 6307 6308
	ShutDown(state);
	return 0;
}

6309
static int drxk_gate_ctrl(struct dvb_frontend *fe, int enable)
R
Ralph Metzler 已提交
6310 6311 6312
{
	struct drxk_state *state = fe->demodulator_priv;

6313
	dprintk(1, ": %s\n", enable ? "enable" : "disable");
6314 6315 6316 6317

	if (state->m_DrxkState == DRXK_NO_DEV)
		return -ENODEV;

R
Ralph Metzler 已提交
6318 6319 6320
	return ConfigureI2CBridge(state, enable ? true : false);
}

6321
static int drxk_set_parameters(struct dvb_frontend *fe)
R
Ralph Metzler 已提交
6322
{
6323
	struct dtv_frontend_properties *p = &fe->dtv_property_cache;
6324
	u32 delsys  = p->delivery_system, old_delsys;
R
Ralph Metzler 已提交
6325 6326 6327
	struct drxk_state *state = fe->demodulator_priv;
	u32 IF;

6328
	dprintk(1, "\n");
6329

6330 6331 6332 6333 6334 6335
	if (state->m_DrxkState == DRXK_NO_DEV)
		return -ENODEV;

	if (state->m_DrxkState == DRXK_UNINITIALIZED)
		return -EAGAIN;

6336 6337 6338 6339 6340 6341
	if (!fe->ops.tuner_ops.get_if_frequency) {
		printk(KERN_ERR
		       "drxk: Error: get_if_frequency() not defined at tuner. Can't work without it!\n");
		return -EINVAL;
	}

6342 6343 6344 6345 6346 6347
	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);
	if (fe->ops.i2c_gate_ctrl)
		fe->ops.i2c_gate_ctrl(fe, 0);
6348 6349

	old_delsys = state->props.delivery_system;
6350 6351
	state->props = *p;

6352 6353 6354 6355 6356 6357 6358 6359 6360 6361 6362 6363
	if (old_delsys != delsys) {
		ShutDown(state);
		switch (delsys) {
		case SYS_DVBC_ANNEX_A:
		case SYS_DVBC_ANNEX_C:
			if (!state->m_hasDVBC)
				return -EINVAL;
			state->m_itut_annex_c = (delsys == SYS_DVBC_ANNEX_C) ? true : false;
			if (state->m_itut_annex_c)
				SetOperationMode(state, OM_QAM_ITU_C);
			else
				SetOperationMode(state, OM_QAM_ITU_A);
6364
			break;
6365 6366 6367 6368 6369 6370
		case SYS_DVBT:
			if (!state->m_hasDVBT)
				return -EINVAL;
			SetOperationMode(state, OM_DVBT);
			break;
		default:
6371
			return -EINVAL;
6372
		}
6373
	}
6374 6375

	fe->ops.tuner_ops.get_if_frequency(fe, &IF);
R
Ralph Metzler 已提交
6376 6377
	Start(state, 0, IF);

6378 6379 6380 6381 6382 6383 6384 6385 6386 6387
	/* After set_frontend, stats aren't avaliable */
	p->strength.stat[0].scale = FE_SCALE_RELATIVE;
	p->cnr.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
	p->block_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
	p->block_count.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
	p->pre_bit_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
	p->pre_bit_count.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
	p->post_bit_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
	p->post_bit_count.stat[0].scale = FE_SCALE_NOT_AVAILABLE;

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

R
Ralph Metzler 已提交
6390 6391 6392
	return 0;
}

6393 6394 6395 6396 6397 6398 6399 6400 6401 6402 6403 6404 6405 6406 6407 6408 6409 6410 6411 6412 6413 6414 6415 6416 6417 6418 6419 6420 6421 6422 6423 6424 6425 6426 6427 6428 6429 6430 6431 6432 6433 6434 6435 6436 6437 6438 6439 6440 6441 6442 6443 6444 6445 6446 6447 6448 6449 6450 6451 6452 6453 6454 6455 6456 6457 6458 6459 6460 6461 6462 6463 6464 6465 6466 6467 6468 6469 6470 6471 6472 6473 6474 6475 6476 6477 6478 6479 6480 6481 6482 6483 6484 6485 6486 6487 6488 6489 6490 6491 6492 6493
static int get_strength(struct drxk_state *state, u64 *strength)
{
	int status;
	struct SCfgAgc   rfAgc, ifAgc;
	u32          totalGain  = 0;
	u32          atten      = 0;
	u32          agcRange   = 0;
	u16            scu_lvl  = 0;
	u16            scu_coc  = 0;
	/* FIXME: those are part of the tuner presets */
	u16 tunerRfGain         = 50; /* Default value on az6007 driver */
	u16 tunerIfGain         = 40; /* Default value on az6007 driver */

	*strength = 0;

	if (IsDVBT(state)) {
		rfAgc = state->m_dvbtRfAgcCfg;
		ifAgc = state->m_dvbtIfAgcCfg;
	} else if (IsQAM(state)) {
		rfAgc = state->m_qamRfAgcCfg;
		ifAgc = state->m_qamIfAgcCfg;
	} else {
		rfAgc = state->m_atvRfAgcCfg;
		ifAgc = state->m_atvIfAgcCfg;
	}

	if (rfAgc.ctrlMode == DRXK_AGC_CTRL_AUTO) {
		/* SCU outputLevel */
		status = read16(state, SCU_RAM_AGC_RF_IACCU_HI__A, &scu_lvl);
		if (status < 0)
			return status;

		/* SCU c.o.c. */
		read16(state, SCU_RAM_AGC_RF_IACCU_HI_CO__A, &scu_coc);
		if (status < 0)
			return status;

		if (((u32) scu_lvl + (u32) scu_coc) < 0xffff)
			rfAgc.outputLevel = scu_lvl + scu_coc;
		else
			rfAgc.outputLevel = 0xffff;

		/* Take RF gain into account */
		totalGain += tunerRfGain;

		/* clip output value */
		if (rfAgc.outputLevel < rfAgc.minOutputLevel)
			rfAgc.outputLevel = rfAgc.minOutputLevel;
		if (rfAgc.outputLevel > rfAgc.maxOutputLevel)
			rfAgc.outputLevel = rfAgc.maxOutputLevel;

		agcRange = (u32) (rfAgc.maxOutputLevel - rfAgc.minOutputLevel);
		if (agcRange > 0) {
			atten += 100UL *
				((u32)(tunerRfGain)) *
				((u32)(rfAgc.outputLevel - rfAgc.minOutputLevel))
				/ agcRange;
		}
	}

	if (ifAgc.ctrlMode == DRXK_AGC_CTRL_AUTO) {
		status = read16(state, SCU_RAM_AGC_IF_IACCU_HI__A,
				&ifAgc.outputLevel);
		if (status < 0)
			return status;

		status = read16(state, SCU_RAM_AGC_INGAIN_TGT_MIN__A,
				&ifAgc.top);
		if (status < 0)
			return status;

		/* Take IF gain into account */
		totalGain += (u32) tunerIfGain;

		/* clip output value */
		if (ifAgc.outputLevel < ifAgc.minOutputLevel)
			ifAgc.outputLevel = ifAgc.minOutputLevel;
		if (ifAgc.outputLevel > ifAgc.maxOutputLevel)
			ifAgc.outputLevel = ifAgc.maxOutputLevel;

		agcRange  = (u32) (ifAgc.maxOutputLevel - ifAgc.minOutputLevel);
		if (agcRange > 0) {
			atten += 100UL *
				((u32)(tunerIfGain)) *
				((u32)(ifAgc.outputLevel - ifAgc.minOutputLevel))
				/ agcRange;
		}
	}

	/*
	 * Convert to 0..65535 scale.
	 * If it can't be measured (AGC is disabled), just show 100%.
	 */
	if (totalGain > 0)
		*strength = (65535UL * atten / totalGain);
	else
		*strength = 65535;

	return 0;
}

6494
static int drxk_get_stats(struct dvb_frontend *fe)
R
Ralph Metzler 已提交
6495
{
6496
	struct dtv_frontend_properties *c = &fe->dtv_property_cache;
R
Ralph Metzler 已提交
6497
	struct drxk_state *state = fe->demodulator_priv;
6498
	int status;
R
Ralph Metzler 已提交
6499
	u32 stat;
6500 6501 6502 6503 6504 6505 6506 6507
	u16 reg16;
	u32 post_bit_count;
	u32 post_bit_err_count;
	u32 post_bit_error_scale;
	u32 pre_bit_err_count;
	u32 pre_bit_count;
	u32 pkt_count;
	u32 pkt_error_count;
6508
	s32 cnr;
6509 6510 6511 6512 6513 6514

	if (state->m_DrxkState == DRXK_NO_DEV)
		return -ENODEV;
	if (state->m_DrxkState == DRXK_UNINITIALIZED)
		return -EAGAIN;

6515 6516
	/* get status */
	state->fe_status = 0;
R
Ralph Metzler 已提交
6517
	GetLockStatus(state, &stat, 0);
6518
	if (stat == MPEG_LOCK)
6519
		state->fe_status |= 0x1f;
6520
	if (stat == FEC_LOCK)
6521
		state->fe_status |= 0x0f;
6522
	if (stat == DEMOD_LOCK)
6523 6524
		state->fe_status |= 0x07;

6525 6526 6527 6528 6529 6530 6531
	/*
	 * Estimate signal strength from AGC
	 */
	get_strength(state, &c->strength.stat[0].uvalue);
	c->strength.stat[0].scale = FE_SCALE_RELATIVE;


6532 6533 6534 6535 6536 6537 6538 6539 6540 6541 6542 6543 6544 6545 6546 6547 6548 6549 6550 6551 6552 6553 6554 6555 6556 6557 6558 6559 6560 6561 6562 6563 6564 6565 6566 6567 6568 6569 6570 6571 6572 6573 6574 6575 6576 6577 6578 6579 6580 6581 6582 6583 6584 6585 6586 6587 6588 6589 6590 6591 6592 6593 6594 6595 6596 6597 6598 6599 6600 6601 6602 6603 6604 6605 6606 6607 6608 6609 6610 6611 6612 6613 6614 6615 6616 6617 6618 6619 6620 6621 6622 6623 6624 6625 6626 6627 6628
	if (stat >= DEMOD_LOCK) {
		GetSignalToNoise(state, &cnr);
		c->cnr.stat[0].svalue = cnr * 100;
		c->cnr.stat[0].scale = FE_SCALE_DECIBEL;
	} else {
		c->cnr.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
	}

	if (stat < FEC_LOCK) {
		c->block_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
		c->block_count.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
		c->pre_bit_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
		c->pre_bit_count.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
		c->post_bit_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
		c->post_bit_count.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
		return 0;
	}

	/* Get post BER */

	/* BER measurement is valid if at least FEC lock is achieved */

	/* OFDM_EC_VD_REQ_SMB_CNT__A and/or OFDM_EC_VD_REQ_BIT_CNT can be written
		to set nr of symbols or bits over which
		to measure EC_VD_REG_ERR_BIT_CNT__A . See CtrlSetCfg(). */

	/* Read registers for post/preViterbi BER calculation */
	status = read16(state, OFDM_EC_VD_ERR_BIT_CNT__A, &reg16);
	if (status < 0)
		goto error;
	pre_bit_err_count = reg16;

	status = read16(state, OFDM_EC_VD_IN_BIT_CNT__A , &reg16);
	if (status < 0)
		goto error;
	pre_bit_count = reg16;

	/* Number of bit-errors */
	status = read16(state, FEC_RS_NR_BIT_ERRORS__A, &reg16);
	if (status < 0)
		goto error;
	post_bit_err_count = reg16;

	status = read16(state, FEC_RS_MEASUREMENT_PRESCALE__A, &reg16);
	if (status < 0)
		goto error;
	post_bit_error_scale = reg16;

	status = read16(state, FEC_RS_MEASUREMENT_PERIOD__A, &reg16);
	if (status < 0)
		goto error;
	pkt_count = reg16;

	status = read16(state, SCU_RAM_FEC_ACCUM_PKT_FAILURES__A, &reg16);
	if (status < 0)
		goto error;
	pkt_error_count = reg16;
	write16(state, SCU_RAM_FEC_ACCUM_PKT_FAILURES__A, 0);

	post_bit_err_count *= post_bit_error_scale;

	post_bit_count = pkt_count * 204 * 8;

	/* Store the results */
	c->block_error.stat[0].scale = FE_SCALE_COUNTER;
	c->block_error.stat[0].uvalue += pkt_error_count;
	c->block_count.stat[0].scale = FE_SCALE_COUNTER;
	c->block_count.stat[0].uvalue += pkt_count;

	c->pre_bit_error.stat[0].scale = FE_SCALE_COUNTER;
	c->pre_bit_error.stat[0].uvalue += pre_bit_err_count;
	c->pre_bit_count.stat[0].scale = FE_SCALE_COUNTER;
	c->pre_bit_count.stat[0].uvalue += pre_bit_count;

	c->post_bit_error.stat[0].scale = FE_SCALE_COUNTER;
	c->post_bit_error.stat[0].uvalue += post_bit_err_count;
	c->post_bit_count.stat[0].scale = FE_SCALE_COUNTER;
	c->post_bit_count.stat[0].uvalue += post_bit_count;

error:
	return status;
}


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

	dprintk(1, "\n");

	rc = drxk_get_stats(fe);
	if (rc < 0)
		return rc;

	*status = state->fe_status;

R
Ralph Metzler 已提交
6629 6630 6631
	return 0;
}

6632 6633
static int drxk_read_signal_strength(struct dvb_frontend *fe,
				     u16 *strength)
R
Ralph Metzler 已提交
6634 6635
{
	struct drxk_state *state = fe->demodulator_priv;
6636
	u32 val = 0;
R
Ralph Metzler 已提交
6637

6638
	dprintk(1, "\n");
6639 6640 6641 6642 6643 6644

	if (state->m_DrxkState == DRXK_NO_DEV)
		return -ENODEV;
	if (state->m_DrxkState == DRXK_UNINITIALIZED)
		return -EAGAIN;

R
Ralph Metzler 已提交
6645
	ReadIFAgc(state, &val);
6646
	*strength = val & 0xffff;
R
Ralph Metzler 已提交
6647 6648 6649 6650 6651 6652 6653 6654
	return 0;
}

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

6655
	dprintk(1, "\n");
6656 6657 6658 6659 6660 6661

	if (state->m_DrxkState == DRXK_NO_DEV)
		return -ENODEV;
	if (state->m_DrxkState == DRXK_UNINITIALIZED)
		return -EAGAIN;

R
Ralph Metzler 已提交
6662
	GetSignalToNoise(state, &snr2);
6663 6664 6665 6666

	/* No negative SNR, clip to zero */
	if (snr2 < 0)
		snr2 = 0;
6667
	*snr = snr2 & 0xffff;
R
Ralph Metzler 已提交
6668 6669 6670 6671 6672 6673 6674 6675
	return 0;
}

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

6676
	dprintk(1, "\n");
6677 6678 6679 6680 6681 6682

	if (state->m_DrxkState == DRXK_NO_DEV)
		return -ENODEV;
	if (state->m_DrxkState == DRXK_UNINITIALIZED)
		return -EAGAIN;

R
Ralph Metzler 已提交
6683 6684 6685 6686 6687
	DVBTQAMGetAccPktErr(state, &err);
	*ucblocks = (u32) err;
	return 0;
}

6688
static int drxk_get_tune_settings(struct dvb_frontend *fe, struct dvb_frontend_tune_settings
6689
				    *sets)
R
Ralph Metzler 已提交
6690
{
6691
	struct drxk_state *state = fe->demodulator_priv;
6692
	struct dtv_frontend_properties *p = &fe->dtv_property_cache;
6693 6694

	dprintk(1, "\n");
6695 6696 6697 6698 6699 6700

	if (state->m_DrxkState == DRXK_NO_DEV)
		return -ENODEV;
	if (state->m_DrxkState == DRXK_UNINITIALIZED)
		return -EAGAIN;

6701 6702 6703
	switch (p->delivery_system) {
	case SYS_DVBC_ANNEX_A:
	case SYS_DVBC_ANNEX_C:
6704
	case SYS_DVBT:
6705 6706 6707 6708 6709 6710 6711
		sets->min_delay_ms = 3000;
		sets->max_drift = 0;
		sets->step_size = 0;
		return 0;
	default:
		return -EINVAL;
	}
R
Ralph Metzler 已提交
6712 6713
}

6714 6715
static struct dvb_frontend_ops drxk_ops = {
	/* .delsys will be filled dynamically */
R
Ralph Metzler 已提交
6716
	.info = {
6717 6718 6719 6720 6721 6722 6723 6724 6725 6726 6727 6728 6729 6730 6731 6732 6733 6734 6735
		.name = "DRXK",
		.frequency_min = 47000000,
		.frequency_max = 865000000,
		 /* For DVB-C */
		.symbol_rate_min = 870000,
		.symbol_rate_max = 11700000,
		/* For DVB-T */
		.frequency_stepsize = 166667,

		.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 |
			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_MUTE_TS |
			FE_CAN_TRANSMISSION_MODE_AUTO | FE_CAN_RECOVER |
			FE_CAN_GUARD_INTERVAL_AUTO | FE_CAN_HIERARCHY_AUTO
	},

	.release = drxk_release,
	.sleep = drxk_sleep,
R
Ralph Metzler 已提交
6736 6737
	.i2c_gate_ctrl = drxk_gate_ctrl,

6738
	.set_frontend = drxk_set_parameters,
6739
	.get_tune_settings = drxk_get_tune_settings,
R
Ralph Metzler 已提交
6740 6741 6742 6743 6744 6745 6746

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

6747
struct dvb_frontend *drxk_attach(const struct drxk_config *config,
6748
				 struct i2c_adapter *i2c)
R
Ralph Metzler 已提交
6749
{
6750
	struct dtv_frontend_properties *p;
R
Ralph Metzler 已提交
6751
	struct drxk_state *state = NULL;
6752
	u8 adr = config->adr;
6753
	int status;
R
Ralph Metzler 已提交
6754

6755
	dprintk(1, "\n");
6756
	state = kzalloc(sizeof(struct drxk_state), GFP_KERNEL);
R
Ralph Metzler 已提交
6757 6758 6759
	if (!state)
		return NULL;

6760 6761
	state->i2c = i2c;
	state->demod_address = adr;
6762
	state->single_master = config->single_master;
6763
	state->microcode_name = config->microcode_name;
6764
	state->qam_demod_parameter_count = config->qam_demod_parameter_count;
6765
	state->no_i2c_bridge = config->no_i2c_bridge;
6766 6767
	state->antenna_gpio = config->antenna_gpio;
	state->antenna_dvbt = config->antenna_dvbt;
6768
	state->m_ChunkSize = config->chunk_size;
6769
	state->enable_merr_cfg = config->enable_merr_cfg;
6770

6771 6772 6773 6774 6775 6776 6777 6778
	if (config->dynamic_clk) {
		state->m_DVBTStaticCLK = 0;
		state->m_DVBCStaticCLK = 0;
	} else {
		state->m_DVBTStaticCLK = 1;
		state->m_DVBCStaticCLK = 1;
	}

6779 6780 6781 6782 6783 6784

	if (config->mpeg_out_clk_strength)
		state->m_TSClockkStrength = config->mpeg_out_clk_strength & 0x07;
	else
		state->m_TSClockkStrength = 0x06;

6785 6786 6787 6788 6789
	if (config->parallel_ts)
		state->m_enableParallel = true;
	else
		state->m_enableParallel = false;

6790 6791 6792 6793 6794 6795 6796 6797
	/* NOTE: as more UIO bits will be used, add them to the mask */
	state->UIO_mask = config->antenna_gpio;

	/* Default gpio to DVB-C */
	if (!state->antenna_dvbt && state->antenna_gpio)
		state->m_GPIO |= state->antenna_gpio;
	else
		state->m_GPIO &= ~state->antenna_gpio;
R
Ralph Metzler 已提交
6798 6799 6800

	mutex_init(&state->mutex);

6801 6802
	memcpy(&state->frontend.ops, &drxk_ops, sizeof(drxk_ops));
	state->frontend.demodulator_priv = state;
R
Ralph Metzler 已提交
6803 6804

	init_state(state);
6805

6806 6807
	/* Load firmware and initialize DRX-K */
	if (state->microcode_name) {
6808 6809 6810 6811 6812 6813 6814 6815 6816 6817
		if (config->load_firmware_sync) {
			const struct firmware *fw = NULL;

			status = request_firmware(&fw, state->microcode_name,
						  state->i2c->dev.parent);
			if (status < 0)
				fw = NULL;
			load_firmware_cb(fw, state);
		} else {
			status = request_firmware_nowait(THIS_MODULE, 1,
6818 6819 6820 6821
					      state->microcode_name,
					      state->i2c->dev.parent,
					      GFP_KERNEL,
					      state, load_firmware_cb);
6822 6823 6824 6825 6826
			if (status < 0) {
				printk(KERN_ERR
				       "drxk: failed to request a firmware\n");
				return NULL;
			}
6827 6828 6829
		}
	} else if (init_drxk(state) < 0)
		goto error;
6830

6831 6832 6833 6834 6835 6836 6837 6838 6839 6840 6841 6842 6843 6844 6845 6846 6847 6848 6849 6850 6851

	/* Initialize stats */
	p = &state->frontend.dtv_property_cache;
	p->strength.len = 1;
	p->cnr.len = 1;
	p->block_error.len = 1;
	p->block_count.len = 1;
	p->pre_bit_error.len = 1;
	p->pre_bit_count.len = 1;
	p->post_bit_error.len = 1;
	p->post_bit_count.len = 1;

	p->strength.stat[0].scale = FE_SCALE_RELATIVE;
	p->cnr.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
	p->block_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
	p->block_count.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
	p->pre_bit_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
	p->pre_bit_count.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
	p->post_bit_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
	p->post_bit_count.stat[0].scale = FE_SCALE_NOT_AVAILABLE;

6852 6853
	printk(KERN_INFO "drxk: frontend initialized.\n");
	return &state->frontend;
R
Ralph Metzler 已提交
6854 6855

error:
6856
	printk(KERN_ERR "drxk: not found\n");
R
Ralph Metzler 已提交
6857 6858 6859
	kfree(state);
	return NULL;
}
6860
EXPORT_SYMBOL(drxk_attach);
R
Ralph Metzler 已提交
6861 6862 6863 6864

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