drxk_hard.c 176.7 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
/*
 * 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
 */

24 25
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt

R
Ralph Metzler 已提交
26 27 28 29 30 31 32
#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>
33
#include <linux/hardirq.h>
R
Ralph Metzler 已提交
34 35 36 37 38
#include <asm/div64.h>

#include "dvb_frontend.h"
#include "drxk.h"
#include "drxk_hard.h"
39
#include "dvb_math.h"
R
Ralph Metzler 已提交
40

41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
static int power_down_dvbt(struct drxk_state *state, bool set_power_mode);
static int power_down_qam(struct drxk_state *state);
static int set_dvbt_standard(struct drxk_state *state,
			   enum operation_mode o_mode);
static int set_qam_standard(struct drxk_state *state,
			  enum operation_mode o_mode);
static int set_qam(struct drxk_state *state, u16 intermediate_freqk_hz,
		  s32 tuner_freq_offset);
static int set_dvbt_standard(struct drxk_state *state,
			   enum operation_mode o_mode);
static int dvbt_start(struct drxk_state *state);
static int set_dvbt(struct drxk_state *state, u16 intermediate_freqk_hz,
		   s32 tuner_freq_offset);
static int get_qam_lock_status(struct drxk_state *state, u32 *p_lock_status);
static int get_dvbt_lock_status(struct drxk_state *state, u32 *p_lock_status);
static int switch_antenna_to_qam(struct drxk_state *state);
static int switch_antenna_to_dvbt(struct drxk_state *state);

static bool is_dvbt(struct drxk_state *state)
R
Ralph Metzler 已提交
60
{
61
	return state->m_operation_mode == OM_DVBT;
R
Ralph Metzler 已提交
62 63
}

64
static bool is_qam(struct drxk_state *state)
R
Ralph Metzler 已提交
65
{
66 67 68
	return state->m_operation_mode == OM_QAM_ITU_A ||
	    state->m_operation_mode == OM_QAM_ITU_B ||
	    state->m_operation_mode == OM_QAM_ITU_C;
R
Ralph Metzler 已提交
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
}

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

165 166 167 168 169 170 171 172 173 174
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)


175
static inline u32 MulDiv32(u32 a, u32 b, u32 c)
R
Ralph Metzler 已提交
176 177 178
{
	u64 tmp64;

179
	tmp64 = (u64) a * (u64) b;
R
Ralph Metzler 已提交
180 181 182 183 184
	do_div(tmp64, c);

	return (u32) tmp64;
}

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

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

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

207
static inline u32 log10times100(u32 value)
R
Ralph Metzler 已提交
208
{
209
	return (100L * intlog10(value)) >> 24;
R
Ralph Metzler 已提交
210 211 212 213 214 215
}

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

216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232
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;
}

233 234 235
static int drxk_i2c_transfer(struct drxk_state *state, struct i2c_msg *msgs,
			     unsigned len)
{
236 237 238 239
	if (state->drxk_i2c_exclusive_lock)
		return __i2c_transfer(state->i2c, msgs, len);
	else
		return i2c_transfer(state->i2c, msgs, len);
240 241 242
}

static int i2c_read1(struct drxk_state *state, u8 adr, u8 *val)
R
Ralph Metzler 已提交
243
{
244 245 246
	struct i2c_msg msgs[1] = { {.addr = adr, .flags = I2C_M_RD,
				    .buf = val, .len = 1}
	};
247

248
	return drxk_i2c_transfer(state, msgs, 1);
R
Ralph Metzler 已提交
249 250
}

251
static int i2c_write(struct drxk_state *state, u8 adr, u8 *data, int len)
R
Ralph Metzler 已提交
252
{
253
	int status;
254 255
	struct i2c_msg msg = {
	    .addr = adr, .flags = 0, .buf = data, .len = len };
R
Ralph Metzler 已提交
256

257 258 259 260 261 262 263
	dprintk(3, ":");
	if (debug > 2) {
		int i;
		for (i = 0; i < len; i++)
			printk(KERN_CONT " %02x", data[i]);
		printk(KERN_CONT "\n");
	}
264
	status = drxk_i2c_transfer(state, &msg, 1);
265 266 267 268
	if (status >= 0 && status != 1)
		status = -EIO;

	if (status < 0)
269
		pr_err("i2c write error at addr 0x%02x\n", adr);
270 271

	return status;
R
Ralph Metzler 已提交
272 273
}

274
static int i2c_read(struct drxk_state *state,
R
Ralph Metzler 已提交
275 276
		    u8 adr, u8 *msg, int len, u8 *answ, int alen)
{
277
	int status;
278 279
	struct i2c_msg msgs[2] = {
		{.addr = adr, .flags = 0,
280
				    .buf = msg, .len = len},
281 282
		{.addr = adr, .flags = I2C_M_RD,
		 .buf = answ, .len = alen}
283
	};
284

285
	status = drxk_i2c_transfer(state, msgs, 2);
286
	if (status != 2) {
287 288
		if (debug > 2)
			printk(KERN_CONT ": ERROR!\n");
289 290
		if (status >= 0)
			status = -EIO;
291

292
		pr_err("i2c read error at addr 0x%02x\n", adr);
293
		return status;
R
Ralph Metzler 已提交
294
	}
295 296
	if (debug > 2) {
		int i;
297
		dprintk(2, ": read from");
298 299
		for (i = 0; i < len; i++)
			printk(KERN_CONT " %02x", msg[i]);
300
		printk(KERN_CONT ", value = ");
301 302
		for (i = 0; i < alen; i++)
			printk(KERN_CONT " %02x", answ[i]);
303 304
		printk(KERN_CONT "\n");
	}
R
Ralph Metzler 已提交
305 306 307
	return 0;
}

308
static int read16_flags(struct drxk_state *state, u32 reg, u16 *data, u8 flags)
R
Ralph Metzler 已提交
309
{
310
	int status;
311
	u8 adr = state->demod_address, mm1[4], mm2[2], len;
312 313 314 315

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

R
Ralph Metzler 已提交
316 317 318 319 320 321 322 323 324 325 326
	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;
	}
327
	dprintk(2, "(0x%08x, 0x%02x)\n", reg, flags);
328
	status = i2c_read(state, adr, mm1, len, mm2, 2);
329 330
	if (status < 0)
		return status;
R
Ralph Metzler 已提交
331 332
	if (data)
		*data = mm2[0] | (mm2[1] << 8);
333

R
Ralph Metzler 已提交
334 335 336
	return 0;
}

337
static int read16(struct drxk_state *state, u32 reg, u16 *data)
R
Ralph Metzler 已提交
338
{
339
	return read16_flags(state, reg, data, 0);
R
Ralph Metzler 已提交
340 341
}

342
static int read32_flags(struct drxk_state *state, u32 reg, u32 *data, u8 flags)
R
Ralph Metzler 已提交
343
{
344
	int status;
R
Ralph Metzler 已提交
345
	u8 adr = state->demod_address, mm1[4], mm2[4], len;
346 347 348 349

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

R
Ralph Metzler 已提交
350 351 352 353 354 355 356 357 358 359 360
	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;
	}
361
	dprintk(2, "(0x%08x, 0x%02x)\n", reg, flags);
362
	status = i2c_read(state, adr, mm1, len, mm2, 4);
363 364
	if (status < 0)
		return status;
R
Ralph Metzler 已提交
365 366
	if (data)
		*data = mm2[0] | (mm2[1] << 8) |
367
		    (mm2[2] << 16) | (mm2[3] << 24);
368

R
Ralph Metzler 已提交
369 370 371
	return 0;
}

372 373 374 375 376 377
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 已提交
378 379
{
	u8 adr = state->demod_address, mm[6], len;
380 381 382

	if (state->single_master)
		flags |= 0xC0;
R
Ralph Metzler 已提交
383 384 385 386 387 388 389 390 391 392 393 394
	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;
395
	mm[len + 1] = (data >> 8) & 0xff;
396 397

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

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

406
static int write32_flags(struct drxk_state *state, u32 reg, u32 data, u8 flags)
R
Ralph Metzler 已提交
407 408
{
	u8 adr = state->demod_address, mm[8], len;
409 410 411

	if (state->single_master)
		flags |= 0xC0;
R
Ralph Metzler 已提交
412 413 414 415 416 417 418 419 420 421 422 423
	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;
424 425 426
	mm[len + 1] = (data >> 8) & 0xff;
	mm[len + 2] = (data >> 16) & 0xff;
	mm[len + 3] = (data >> 24) & 0xff;
427
	dprintk(2, "(0x%08x, 0x%08x, 0x%02x)\n", reg, data, flags);
428

429
	return i2c_write(state, adr, mm, len + 4);
R
Ralph Metzler 已提交
430 431
}

432 433 434 435 436
static int write32(struct drxk_state *state, u32 reg, u32 data)
{
	return write32_flags(state, reg, data, 0);
}

437 438
static int write_block(struct drxk_state *state, u32 address,
		      const int block_size, const u8 p_block[])
R
Ralph Metzler 已提交
439
{
440 441
	int status = 0, blk_size = block_size;
	u8 flags = 0;
442 443

	if (state->single_master)
444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460
		flags |= 0xC0;

	while (blk_size > 0) {
		int chunk = blk_size > state->m_chunk_size ?
		    state->m_chunk_size : blk_size;
		u8 *adr_buf = &state->chunk[0];
		u32 adr_length = 0;

		if (DRXDAP_FASI_LONG_FORMAT(address) || (flags != 0)) {
			adr_buf[0] = (((address << 1) & 0xFF) | 0x01);
			adr_buf[1] = ((address >> 16) & 0xFF);
			adr_buf[2] = ((address >> 24) & 0xFF);
			adr_buf[3] = ((address >> 7) & 0xFF);
			adr_buf[2] |= flags;
			adr_length = 4;
			if (chunk == state->m_chunk_size)
				chunk -= 2;
461
		} else {
462 463 464 465
			adr_buf[0] = ((address << 1) & 0xFF);
			adr_buf[1] = (((address >> 16) & 0x0F) |
				     ((address >> 18) & 0xF0));
			adr_length = 2;
R
Ralph Metzler 已提交
466
		}
467 468
		memcpy(&state->chunk[adr_length], p_block, chunk);
		dprintk(2, "(0x%08x, 0x%02x)\n", address, flags);
469 470
		if (debug > 1) {
			int i;
471 472 473
			if (p_block)
				for (i = 0; i < chunk; i++)
					printk(KERN_CONT " %02x", p_block[i]);
474 475
			printk(KERN_CONT "\n");
		}
476
		status = i2c_write(state, state->demod_address,
477
				   &state->chunk[0], chunk + adr_length);
478
		if (status < 0) {
479
			pr_err("%s: i2c write error at addr 0x%02x\n",
480
			       __func__, address);
R
Ralph Metzler 已提交
481 482
			break;
		}
483 484 485
		p_block += chunk;
		address += (chunk >> 1);
		blk_size -= chunk;
R
Ralph Metzler 已提交
486
	}
487
	return status;
R
Ralph Metzler 已提交
488 489 490 491 492 493
}

#ifndef DRXK_MAX_RETRIES_POWERUP
#define DRXK_MAX_RETRIES_POWERUP 20
#endif

494
static int power_up_device(struct drxk_state *state)
R
Ralph Metzler 已提交
495 496 497
{
	int status;
	u8 data = 0;
498
	u16 retry_count = 0;
R
Ralph Metzler 已提交
499

500 501
	dprintk(1, "\n");

502
	status = i2c_read1(state, state->demod_address, &data);
503
	if (status < 0) {
R
Ralph Metzler 已提交
504 505
		do {
			data = 0;
506
			status = i2c_write(state, state->demod_address,
507
					   &data, 1);
R
Ralph Metzler 已提交
508
			msleep(10);
509
			retry_count++;
510 511
			if (status < 0)
				continue;
512
			status = i2c_read1(state, state->demod_address,
513 514
					   &data);
		} while (status < 0 &&
515 516
			 (retry_count < DRXK_MAX_RETRIES_POWERUP));
		if (status < 0 && retry_count >= DRXK_MAX_RETRIES_POWERUP)
517 518 519 520 521 522 523 524 525 526 527 528 529 530 531
			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;

532
	state->m_current_power_mode = DRX_POWER_UP;
533 534 535

error:
	if (status < 0)
536
		pr_err("Error %d on %s\n", status, __func__);
537

R
Ralph Metzler 已提交
538 539 540 541 542 543
	return status;
}


static int init_state(struct drxk_state *state)
{
544 545 546 547
	/*
	 * FIXME: most (all?) of the values bellow should be moved into
	 * struct drxk_config, as they are probably board-specific
	 */
548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574
	u32 ul_vsb_if_agc_mode = DRXK_AGC_CTRL_AUTO;
	u32 ul_vsb_if_agc_output_level = 0;
	u32 ul_vsb_if_agc_min_level = 0;
	u32 ul_vsb_if_agc_max_level = 0x7FFF;
	u32 ul_vsb_if_agc_speed = 3;

	u32 ul_vsb_rf_agc_mode = DRXK_AGC_CTRL_AUTO;
	u32 ul_vsb_rf_agc_output_level = 0;
	u32 ul_vsb_rf_agc_min_level = 0;
	u32 ul_vsb_rf_agc_max_level = 0x7FFF;
	u32 ul_vsb_rf_agc_speed = 3;
	u32 ul_vsb_rf_agc_top = 9500;
	u32 ul_vsb_rf_agc_cut_off_current = 4000;

	u32 ul_atv_if_agc_mode = DRXK_AGC_CTRL_AUTO;
	u32 ul_atv_if_agc_output_level = 0;
	u32 ul_atv_if_agc_min_level = 0;
	u32 ul_atv_if_agc_max_level = 0;
	u32 ul_atv_if_agc_speed = 3;

	u32 ul_atv_rf_agc_mode = DRXK_AGC_CTRL_OFF;
	u32 ul_atv_rf_agc_output_level = 0;
	u32 ul_atv_rf_agc_min_level = 0;
	u32 ul_atv_rf_agc_max_level = 0;
	u32 ul_atv_rf_agc_top = 9500;
	u32 ul_atv_rf_agc_cut_off_current = 4000;
	u32 ul_atv_rf_agc_speed = 3;
R
Ralph Metzler 已提交
575 576 577 578

	u32 ulQual83 = DEFAULT_MER_83;
	u32 ulQual93 = DEFAULT_MER_93;

579 580
	u32 ul_mpeg_lock_time_out = DEFAULT_DRXK_MPEG_LOCK_TIMEOUT;
	u32 ul_demod_lock_time_out = DEFAULT_DRXK_DEMOD_LOCK_TIMEOUT;
R
Ralph Metzler 已提交
581 582 583 584

	/* 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) */
585 586 587 588 589
	u32 ul_gpio_cfg = 0x0113;
	u32 ul_invert_ts_clock = 0;
	u32 ul_ts_data_strength = DRXK_MPEG_SERIAL_OUTPUT_PIN_DRIVE_STRENGTH;
	u32 ul_dvbt_bitrate = 50000000;
	u32 ul_dvbc_bitrate = DRXK_QAM_SYMBOLRATE_MAX * 8;
R
Ralph Metzler 已提交
590

591
	u32 ul_insert_rs_byte = 0;
R
Ralph Metzler 已提交
592

593 594
	u32 ul_rf_mirror = 1;
	u32 ul_power_down = 0;
R
Ralph Metzler 已提交
595

596 597
	dprintk(1, "\n");

598 599 600 601 602 603
	state->m_has_lna = false;
	state->m_has_dvbt = false;
	state->m_has_dvbc = false;
	state->m_has_atv = false;
	state->m_has_oob = false;
	state->m_has_audio = false;
R
Ralph Metzler 已提交
604

605 606
	if (!state->m_chunk_size)
		state->m_chunk_size = 124;
R
Ralph Metzler 已提交
607

608 609 610
	state->m_osc_clock_freq = 0;
	state->m_smart_ant_inverted = false;
	state->m_b_p_down_open_bridge = false;
R
Ralph Metzler 已提交
611 612

	/* real system clock frequency in kHz */
613
	state->m_sys_clock_freq = 151875;
R
Ralph Metzler 已提交
614 615
	/* Timing div, 250ns/Psys */
	/* Timing div, = (delay (nano seconds) * sysclk (kHz))/ 1000 */
616
	state->m_hi_cfg_timing_div = ((state->m_sys_clock_freq / 1000) *
R
Ralph Metzler 已提交
617 618
				   HI_I2C_DELAY) / 1000;
	/* Clipping */
619 620 621
	if (state->m_hi_cfg_timing_div > SIO_HI_RA_RAM_PAR_2_CFG_DIV__M)
		state->m_hi_cfg_timing_div = SIO_HI_RA_RAM_PAR_2_CFG_DIV__M;
	state->m_hi_cfg_wake_up_key = (state->demod_address << 1);
R
Ralph Metzler 已提交
622
	/* port/bridge/power down ctrl */
623
	state->m_hi_cfg_ctrl = SIO_HI_RA_RAM_PAR_5_CFG_SLV0_SLAVE;
R
Ralph Metzler 已提交
624

625
	state->m_b_power_down = (ul_power_down != 0);
R
Ralph Metzler 已提交
626

627
	state->m_drxk_a3_patch_code = false;
R
Ralph Metzler 已提交
628 629 630

	/* Init AGC and PGA parameters */
	/* VSB IF */
631 632 633 634 635 636
	state->m_vsb_if_agc_cfg.ctrl_mode = (ul_vsb_if_agc_mode);
	state->m_vsb_if_agc_cfg.output_level = (ul_vsb_if_agc_output_level);
	state->m_vsb_if_agc_cfg.min_output_level = (ul_vsb_if_agc_min_level);
	state->m_vsb_if_agc_cfg.max_output_level = (ul_vsb_if_agc_max_level);
	state->m_vsb_if_agc_cfg.speed = (ul_vsb_if_agc_speed);
	state->m_vsb_pga_cfg = 140;
R
Ralph Metzler 已提交
637 638

	/* VSB RF */
639 640 641 642 643 644 645 646 647
	state->m_vsb_rf_agc_cfg.ctrl_mode = (ul_vsb_rf_agc_mode);
	state->m_vsb_rf_agc_cfg.output_level = (ul_vsb_rf_agc_output_level);
	state->m_vsb_rf_agc_cfg.min_output_level = (ul_vsb_rf_agc_min_level);
	state->m_vsb_rf_agc_cfg.max_output_level = (ul_vsb_rf_agc_max_level);
	state->m_vsb_rf_agc_cfg.speed = (ul_vsb_rf_agc_speed);
	state->m_vsb_rf_agc_cfg.top = (ul_vsb_rf_agc_top);
	state->m_vsb_rf_agc_cfg.cut_off_current = (ul_vsb_rf_agc_cut_off_current);
	state->m_vsb_pre_saw_cfg.reference = 0x07;
	state->m_vsb_pre_saw_cfg.use_pre_saw = true;
R
Ralph Metzler 已提交
648 649 650 651 652 653 654 655 656

	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 */
657 658 659 660 661
	state->m_atv_if_agc_cfg.ctrl_mode = (ul_atv_if_agc_mode);
	state->m_atv_if_agc_cfg.output_level = (ul_atv_if_agc_output_level);
	state->m_atv_if_agc_cfg.min_output_level = (ul_atv_if_agc_min_level);
	state->m_atv_if_agc_cfg.max_output_level = (ul_atv_if_agc_max_level);
	state->m_atv_if_agc_cfg.speed = (ul_atv_if_agc_speed);
R
Ralph Metzler 已提交
662 663

	/* ATV RF */
664 665 666 667 668 669 670 671 672
	state->m_atv_rf_agc_cfg.ctrl_mode = (ul_atv_rf_agc_mode);
	state->m_atv_rf_agc_cfg.output_level = (ul_atv_rf_agc_output_level);
	state->m_atv_rf_agc_cfg.min_output_level = (ul_atv_rf_agc_min_level);
	state->m_atv_rf_agc_cfg.max_output_level = (ul_atv_rf_agc_max_level);
	state->m_atv_rf_agc_cfg.speed = (ul_atv_rf_agc_speed);
	state->m_atv_rf_agc_cfg.top = (ul_atv_rf_agc_top);
	state->m_atv_rf_agc_cfg.cut_off_current = (ul_atv_rf_agc_cut_off_current);
	state->m_atv_pre_saw_cfg.reference = 0x04;
	state->m_atv_pre_saw_cfg.use_pre_saw = true;
R
Ralph Metzler 已提交
673 674 675


	/* DVBT RF */
676 677 678 679 680 681 682
	state->m_dvbt_rf_agc_cfg.ctrl_mode = DRXK_AGC_CTRL_OFF;
	state->m_dvbt_rf_agc_cfg.output_level = 0;
	state->m_dvbt_rf_agc_cfg.min_output_level = 0;
	state->m_dvbt_rf_agc_cfg.max_output_level = 0xFFFF;
	state->m_dvbt_rf_agc_cfg.top = 0x2100;
	state->m_dvbt_rf_agc_cfg.cut_off_current = 4000;
	state->m_dvbt_rf_agc_cfg.speed = 1;
R
Ralph Metzler 已提交
683 684 685


	/* DVBT IF */
686 687 688 689 690 691 692 693 694
	state->m_dvbt_if_agc_cfg.ctrl_mode = DRXK_AGC_CTRL_AUTO;
	state->m_dvbt_if_agc_cfg.output_level = 0;
	state->m_dvbt_if_agc_cfg.min_output_level = 0;
	state->m_dvbt_if_agc_cfg.max_output_level = 9000;
	state->m_dvbt_if_agc_cfg.top = 13424;
	state->m_dvbt_if_agc_cfg.cut_off_current = 0;
	state->m_dvbt_if_agc_cfg.speed = 3;
	state->m_dvbt_if_agc_cfg.fast_clip_ctrl_delay = 30;
	state->m_dvbt_if_agc_cfg.ingain_tgt_max = 30000;
695
	/* state->m_dvbtPgaCfg = 140; */
R
Ralph Metzler 已提交
696

697 698
	state->m_dvbt_pre_saw_cfg.reference = 4;
	state->m_dvbt_pre_saw_cfg.use_pre_saw = false;
R
Ralph Metzler 已提交
699 700

	/* QAM RF */
701 702 703 704 705 706 707
	state->m_qam_rf_agc_cfg.ctrl_mode = DRXK_AGC_CTRL_OFF;
	state->m_qam_rf_agc_cfg.output_level = 0;
	state->m_qam_rf_agc_cfg.min_output_level = 6023;
	state->m_qam_rf_agc_cfg.max_output_level = 27000;
	state->m_qam_rf_agc_cfg.top = 0x2380;
	state->m_qam_rf_agc_cfg.cut_off_current = 4000;
	state->m_qam_rf_agc_cfg.speed = 3;
R
Ralph Metzler 已提交
708 709

	/* QAM IF */
710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725
	state->m_qam_if_agc_cfg.ctrl_mode = DRXK_AGC_CTRL_AUTO;
	state->m_qam_if_agc_cfg.output_level = 0;
	state->m_qam_if_agc_cfg.min_output_level = 0;
	state->m_qam_if_agc_cfg.max_output_level = 9000;
	state->m_qam_if_agc_cfg.top = 0x0511;
	state->m_qam_if_agc_cfg.cut_off_current = 0;
	state->m_qam_if_agc_cfg.speed = 3;
	state->m_qam_if_agc_cfg.ingain_tgt_max = 5119;
	state->m_qam_if_agc_cfg.fast_clip_ctrl_delay = 50;

	state->m_qam_pga_cfg = 140;
	state->m_qam_pre_saw_cfg.reference = 4;
	state->m_qam_pre_saw_cfg.use_pre_saw = false;

	state->m_operation_mode = OM_NONE;
	state->m_drxk_state = DRXK_UNINITIALIZED;
R
Ralph Metzler 已提交
726 727

	/* MPEG output configuration */
728 729 730 731 732 733 734
	state->m_enable_mpeg_output = true;	/* If TRUE; enable MPEG ouput */
	state->m_insert_rs_byte = false;	/* If TRUE; insert RS byte */
	state->m_invert_data = false;	/* If TRUE; invert DATA signals */
	state->m_invert_err = false;	/* If TRUE; invert ERR signal */
	state->m_invert_str = false;	/* If TRUE; invert STR signals */
	state->m_invert_val = false;	/* If TRUE; invert VAL signals */
	state->m_invert_clk = (ul_invert_ts_clock != 0);	/* If TRUE; invert CLK signals */
735

R
Ralph Metzler 已提交
736 737 738
	/* If TRUE; static MPEG clockrate will be used;
	   otherwise clockrate will adapt to the bitrate of the TS */

739 740
	state->m_dvbt_bitrate = ul_dvbt_bitrate;
	state->m_dvbc_bitrate = ul_dvbc_bitrate;
R
Ralph Metzler 已提交
741

742
	state->m_ts_data_strength = (ul_ts_data_strength & 0x07);
R
Ralph Metzler 已提交
743 744

	/* Maximum bitrate in b/s in case static clockrate is selected */
745 746
	state->m_mpeg_ts_static_bitrate = 19392658;
	state->m_disable_te_ihandling = false;
R
Ralph Metzler 已提交
747

748 749
	if (ul_insert_rs_byte)
		state->m_insert_rs_byte = true;
R
Ralph Metzler 已提交
750

751 752 753 754 755 756
	state->m_mpeg_lock_time_out = DEFAULT_DRXK_MPEG_LOCK_TIMEOUT;
	if (ul_mpeg_lock_time_out < 10000)
		state->m_mpeg_lock_time_out = ul_mpeg_lock_time_out;
	state->m_demod_lock_time_out = DEFAULT_DRXK_DEMOD_LOCK_TIMEOUT;
	if (ul_demod_lock_time_out < 10000)
		state->m_demod_lock_time_out = ul_demod_lock_time_out;
R
Ralph Metzler 已提交
757

758
	/* QAM defaults */
759 760 761 762
	state->m_constellation = DRX_CONSTELLATION_AUTO;
	state->m_qam_interleave_mode = DRXK_QAM_I12_J17;
	state->m_fec_rs_plen = 204 * 8;	/* fecRsPlen  annex A */
	state->m_fec_rs_prescale = 1;
R
Ralph Metzler 已提交
763

764 765
	state->m_sqi_speed = DRXK_DVBT_SQI_SPEED_MEDIUM;
	state->m_agcfast_clip_ctrl_delay = 0;
R
Ralph Metzler 已提交
766

767
	state->m_gpio_cfg = (ul_gpio_cfg);
R
Ralph Metzler 已提交
768

769 770
	state->m_b_power_down = false;
	state->m_current_power_mode = DRX_POWER_DOWN;
R
Ralph Metzler 已提交
771

772 773
	state->m_rfmirror = (ul_rf_mirror == 0);
	state->m_if_agc_pol = false;
R
Ralph Metzler 已提交
774 775 776
	return 0;
}

777
static int drxx_open(struct drxk_state *state)
R
Ralph Metzler 已提交
778 779 780 781 782 783
{
	int status = 0;
	u32 jtag = 0;
	u16 bid = 0;
	u16 key = 0;

784
	dprintk(1, "\n");
785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804
	/* 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)
805
		pr_err("Error %d on %s\n", status, __func__);
R
Ralph Metzler 已提交
806 807 808
	return status;
}

809
static int get_device_capabilities(struct drxk_state *state)
R
Ralph Metzler 已提交
810
{
811 812
	u16 sio_pdr_ohw_cfg = 0;
	u32 sio_top_jtagid_lo = 0;
R
Ralph Metzler 已提交
813
	int status;
814
	const char *spin = "";
R
Ralph Metzler 已提交
815

816
	dprintk(1, "\n");
R
Ralph Metzler 已提交
817

818 819 820 821 822
	/* 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;
823
	status = write16(state, SIO_TOP_COMM_KEY__A, SIO_TOP_COMM_KEY_KEY);
824 825
	if (status < 0)
		goto error;
826
	status = read16(state, SIO_PDR_OHW_CFG__A, &sio_pdr_ohw_cfg);
827 828 829 830 831
	if (status < 0)
		goto error;
	status = write16(state, SIO_TOP_COMM_KEY__A, 0x0000);
	if (status < 0)
		goto error;
R
Ralph Metzler 已提交
832

833
	switch ((sio_pdr_ohw_cfg & SIO_PDR_OHW_CFG_FREF_SEL__M)) {
834 835 836 837 838
	case 0:
		/* ignore (bypass ?) */
		break;
	case 1:
		/* 27 MHz */
839
		state->m_osc_clock_freq = 27000;
840 841 842
		break;
	case 2:
		/* 20.25 MHz */
843
		state->m_osc_clock_freq = 20250;
844 845 846
		break;
	case 3:
		/* 4 MHz */
847
		state->m_osc_clock_freq = 20250;
848 849
		break;
	default:
850
		pr_err("Clock Frequency is unknown\n");
851 852 853 854 855 856
		return -EINVAL;
	}
	/*
		Determine device capabilities
		Based on pinning v14
		*/
857
	status = read32(state, SIO_TOP_JTAGID_LO__A, &sio_top_jtagid_lo);
858 859
	if (status < 0)
		goto error;
860

861
	pr_info("status = 0x%08x\n", sio_top_jtagid_lo);
862

863
	/* driver 0.9.0 */
864
	switch ((sio_top_jtagid_lo >> 29) & 0xF) {
865
	case 0:
866
		state->m_device_spin = DRXK_SPIN_A1;
867
		spin = "A1";
868 869
		break;
	case 2:
870
		state->m_device_spin = DRXK_SPIN_A2;
871
		spin = "A2";
872 873
		break;
	case 3:
874
		state->m_device_spin = DRXK_SPIN_A3;
875
		spin = "A3";
876 877
		break;
	default:
878
		state->m_device_spin = DRXK_SPIN_UNKNOWN;
879
		status = -EINVAL;
880
		pr_err("Spin %d unknown\n", (sio_top_jtagid_lo >> 29) & 0xF);
881 882
		goto error2;
	}
883
	switch ((sio_top_jtagid_lo >> 12) & 0xFF) {
884 885
	case 0x13:
		/* typeId = DRX3913K_TYPE_ID */
886 887 888 889 890 891 892 893 894 895
		state->m_has_lna = false;
		state->m_has_oob = false;
		state->m_has_atv = false;
		state->m_has_audio = false;
		state->m_has_dvbt = true;
		state->m_has_dvbc = true;
		state->m_has_sawsw = true;
		state->m_has_gpio2 = false;
		state->m_has_gpio1 = false;
		state->m_has_irqn = false;
896 897 898
		break;
	case 0x15:
		/* typeId = DRX3915K_TYPE_ID */
899 900 901 902 903 904 905 906 907 908
		state->m_has_lna = false;
		state->m_has_oob = false;
		state->m_has_atv = true;
		state->m_has_audio = false;
		state->m_has_dvbt = true;
		state->m_has_dvbc = false;
		state->m_has_sawsw = true;
		state->m_has_gpio2 = true;
		state->m_has_gpio1 = true;
		state->m_has_irqn = false;
909 910 911
		break;
	case 0x16:
		/* typeId = DRX3916K_TYPE_ID */
912 913 914 915 916 917 918 919 920 921
		state->m_has_lna = false;
		state->m_has_oob = false;
		state->m_has_atv = true;
		state->m_has_audio = false;
		state->m_has_dvbt = true;
		state->m_has_dvbc = false;
		state->m_has_sawsw = true;
		state->m_has_gpio2 = true;
		state->m_has_gpio1 = true;
		state->m_has_irqn = false;
922 923 924
		break;
	case 0x18:
		/* typeId = DRX3918K_TYPE_ID */
925 926 927 928 929 930 931 932 933 934
		state->m_has_lna = false;
		state->m_has_oob = false;
		state->m_has_atv = true;
		state->m_has_audio = true;
		state->m_has_dvbt = true;
		state->m_has_dvbc = false;
		state->m_has_sawsw = true;
		state->m_has_gpio2 = true;
		state->m_has_gpio1 = true;
		state->m_has_irqn = false;
935 936 937
		break;
	case 0x21:
		/* typeId = DRX3921K_TYPE_ID */
938 939 940 941 942 943 944 945 946 947
		state->m_has_lna = false;
		state->m_has_oob = false;
		state->m_has_atv = true;
		state->m_has_audio = true;
		state->m_has_dvbt = true;
		state->m_has_dvbc = true;
		state->m_has_sawsw = true;
		state->m_has_gpio2 = true;
		state->m_has_gpio1 = true;
		state->m_has_irqn = false;
948 949 950
		break;
	case 0x23:
		/* typeId = DRX3923K_TYPE_ID */
951 952 953 954 955 956 957 958 959 960
		state->m_has_lna = false;
		state->m_has_oob = false;
		state->m_has_atv = true;
		state->m_has_audio = true;
		state->m_has_dvbt = true;
		state->m_has_dvbc = true;
		state->m_has_sawsw = true;
		state->m_has_gpio2 = true;
		state->m_has_gpio1 = true;
		state->m_has_irqn = false;
961 962 963
		break;
	case 0x25:
		/* typeId = DRX3925K_TYPE_ID */
964 965 966 967 968 969 970 971 972 973
		state->m_has_lna = false;
		state->m_has_oob = false;
		state->m_has_atv = true;
		state->m_has_audio = true;
		state->m_has_dvbt = true;
		state->m_has_dvbc = true;
		state->m_has_sawsw = true;
		state->m_has_gpio2 = true;
		state->m_has_gpio1 = true;
		state->m_has_irqn = false;
974 975 976
		break;
	case 0x26:
		/* typeId = DRX3926K_TYPE_ID */
977 978 979 980 981 982 983 984 985 986
		state->m_has_lna = false;
		state->m_has_oob = false;
		state->m_has_atv = true;
		state->m_has_audio = false;
		state->m_has_dvbt = true;
		state->m_has_dvbc = true;
		state->m_has_sawsw = true;
		state->m_has_gpio2 = true;
		state->m_has_gpio1 = true;
		state->m_has_irqn = false;
987 988
		break;
	default:
989
		pr_err("DeviceID 0x%02x not supported\n",
990
			((sio_top_jtagid_lo >> 12) & 0xFF));
991 992 993 994
		status = -EINVAL;
		goto error2;
	}

995
	pr_info("detected a drx-39%02xk, spin %s, xtal %d.%03d MHz\n",
996 997 998
	       ((sio_top_jtagid_lo >> 12) & 0xFF), spin,
	       state->m_osc_clock_freq / 1000,
	       state->m_osc_clock_freq % 1000);
999

1000 1001
error:
	if (status < 0)
1002
		pr_err("Error %d on %s\n", status, __func__);
1003 1004

error2:
R
Ralph Metzler 已提交
1005 1006 1007
	return status;
}

1008
static int hi_command(struct drxk_state *state, u16 cmd, u16 *p_result)
R
Ralph Metzler 已提交
1009 1010 1011 1012
{
	int status;
	bool powerdown_cmd;

1013 1014
	dprintk(1, "\n");

R
Ralph Metzler 已提交
1015
	/* Write command */
1016
	status = write16(state, SIO_HI_RA_RAM_CMD__A, cmd);
R
Ralph Metzler 已提交
1017
	if (status < 0)
1018
		goto error;
R
Ralph Metzler 已提交
1019 1020 1021 1022
	if (cmd == SIO_HI_RA_RAM_CMD_RESET)
		msleep(1);

	powerdown_cmd =
1023
	    (bool) ((cmd == SIO_HI_RA_RAM_CMD_CONFIG) &&
1024
		    ((state->m_hi_cfg_ctrl) &
1025 1026
		     SIO_HI_RA_RAM_PAR_5_CFG_SLEEP__M) ==
		    SIO_HI_RA_RAM_PAR_5_CFG_SLEEP_ZZZ);
R
Ralph Metzler 已提交
1027 1028
	if (powerdown_cmd == false) {
		/* Wait until command rdy */
1029 1030
		u32 retry_count = 0;
		u16 wait_cmd;
R
Ralph Metzler 已提交
1031 1032 1033

		do {
			msleep(1);
1034
			retry_count += 1;
1035
			status = read16(state, SIO_HI_RA_RAM_CMD__A,
1036 1037 1038
					  &wait_cmd);
		} while ((status < 0) && (retry_count < DRXK_MAX_RETRIES)
			 && (wait_cmd != 0));
1039 1040
		if (status < 0)
			goto error;
1041
		status = read16(state, SIO_HI_RA_RAM_RES__A, p_result);
R
Ralph Metzler 已提交
1042
	}
1043 1044
error:
	if (status < 0)
1045
		pr_err("Error %d on %s\n", status, __func__);
1046

R
Ralph Metzler 已提交
1047 1048 1049
	return status;
}

1050
static int hi_cfg_command(struct drxk_state *state)
R
Ralph Metzler 已提交
1051 1052 1053
{
	int status;

1054 1055
	dprintk(1, "\n");

R
Ralph Metzler 已提交
1056 1057
	mutex_lock(&state->mutex);

1058
	status = write16(state, SIO_HI_RA_RAM_PAR_6__A, state->m_hi_cfg_timeout);
1059 1060
	if (status < 0)
		goto error;
1061
	status = write16(state, SIO_HI_RA_RAM_PAR_5__A, state->m_hi_cfg_ctrl);
1062 1063
	if (status < 0)
		goto error;
1064
	status = write16(state, SIO_HI_RA_RAM_PAR_4__A, state->m_hi_cfg_wake_up_key);
1065 1066
	if (status < 0)
		goto error;
1067
	status = write16(state, SIO_HI_RA_RAM_PAR_3__A, state->m_hi_cfg_bridge_delay);
1068 1069
	if (status < 0)
		goto error;
1070
	status = write16(state, SIO_HI_RA_RAM_PAR_2__A, state->m_hi_cfg_timing_div);
1071 1072 1073 1074 1075
	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;
1076
	status = hi_command(state, SIO_HI_RA_RAM_CMD_CONFIG, 0);
1077 1078 1079
	if (status < 0)
		goto error;

1080
	state->m_hi_cfg_ctrl &= ~SIO_HI_RA_RAM_PAR_5_CFG_SLEEP_ZZZ;
1081
error:
R
Ralph Metzler 已提交
1082
	mutex_unlock(&state->mutex);
1083
	if (status < 0)
1084
		pr_err("Error %d on %s\n", status, __func__);
R
Ralph Metzler 已提交
1085 1086 1087
	return status;
}

1088
static int init_hi(struct drxk_state *state)
R
Ralph Metzler 已提交
1089
{
1090 1091
	dprintk(1, "\n");

1092 1093
	state->m_hi_cfg_wake_up_key = (state->demod_address << 1);
	state->m_hi_cfg_timeout = 0x96FF;
R
Ralph Metzler 已提交
1094
	/* port/bridge/power down ctrl */
1095
	state->m_hi_cfg_ctrl = SIO_HI_RA_RAM_PAR_5_CFG_SLV0_SLAVE;
1096

1097
	return hi_cfg_command(state);
R
Ralph Metzler 已提交
1098 1099
}

1100
static int mpegts_configure_pins(struct drxk_state *state, bool mpeg_enable)
R
Ralph Metzler 已提交
1101 1102
{
	int status = -1;
1103 1104
	u16 sio_pdr_mclk_cfg = 0;
	u16 sio_pdr_mdx_cfg = 0;
1105
	u16 err_cfg = 0;
R
Ralph Metzler 已提交
1106

1107
	dprintk(1, ": mpeg %s, %s mode\n",
1108 1109
		mpeg_enable ? "enable" : "disable",
		state->m_enable_parallel ? "parallel" : "serial");
R
Ralph Metzler 已提交
1110

1111 1112 1113 1114 1115 1116
	/* 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 */
1117
	status = write16(state, SIO_TOP_COMM_KEY__A, SIO_TOP_COMM_KEY_KEY);
1118 1119 1120
	if (status < 0)
		goto error;

1121
	if (mpeg_enable == false) {
1122 1123
		/*  Set MPEG TS pads to inputmode */
		status = write16(state, SIO_PDR_MSTRT_CFG__A, 0x0000);
1124
		if (status < 0)
1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160
			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 */
1161 1162
		sio_pdr_mdx_cfg =
			((state->m_ts_data_strength <<
1163
			SIO_PDR_MD0_CFG_DRIVE__B) | 0x0003);
1164
		sio_pdr_mclk_cfg = ((state->m_ts_clockk_strength <<
1165 1166
					SIO_PDR_MCLK_CFG_DRIVE__B) |
					0x0003);
R
Ralph Metzler 已提交
1167

1168
		status = write16(state, SIO_PDR_MSTRT_CFG__A, sio_pdr_mdx_cfg);
1169 1170
		if (status < 0)
			goto error;
1171 1172

		if (state->enable_merr_cfg)
1173
			err_cfg = sio_pdr_mdx_cfg;
1174 1175

		status = write16(state, SIO_PDR_MERR_CFG__A, err_cfg);
1176 1177
		if (status < 0)
			goto error;
1178
		status = write16(state, SIO_PDR_MVAL_CFG__A, err_cfg);
1179 1180
		if (status < 0)
			goto error;
1181

1182
		if (state->m_enable_parallel == true) {
1183
			/* paralel -> enable MD1 to MD7 */
1184
			status = write16(state, SIO_PDR_MD1_CFG__A, sio_pdr_mdx_cfg);
1185
			if (status < 0)
1186
				goto error;
1187
			status = write16(state, SIO_PDR_MD2_CFG__A, sio_pdr_mdx_cfg);
1188
			if (status < 0)
1189
				goto error;
1190
			status = write16(state, SIO_PDR_MD3_CFG__A, sio_pdr_mdx_cfg);
1191
			if (status < 0)
1192
				goto error;
1193
			status = write16(state, SIO_PDR_MD4_CFG__A, sio_pdr_mdx_cfg);
1194
			if (status < 0)
1195
				goto error;
1196
			status = write16(state, SIO_PDR_MD5_CFG__A, sio_pdr_mdx_cfg);
1197
			if (status < 0)
1198
				goto error;
1199
			status = write16(state, SIO_PDR_MD6_CFG__A, sio_pdr_mdx_cfg);
1200 1201
			if (status < 0)
				goto error;
1202
			status = write16(state, SIO_PDR_MD7_CFG__A, sio_pdr_mdx_cfg);
1203 1204 1205
			if (status < 0)
				goto error;
		} else {
1206
			sio_pdr_mdx_cfg = ((state->m_ts_data_strength <<
1207 1208 1209
						SIO_PDR_MD0_CFG_DRIVE__B)
					| 0x0003);
			/* serial -> disable MD1 to MD7 */
1210
			status = write16(state, SIO_PDR_MD1_CFG__A, 0x0000);
1211
			if (status < 0)
1212
				goto error;
1213
			status = write16(state, SIO_PDR_MD2_CFG__A, 0x0000);
1214
			if (status < 0)
1215
				goto error;
1216
			status = write16(state, SIO_PDR_MD3_CFG__A, 0x0000);
1217
			if (status < 0)
1218
				goto error;
1219
			status = write16(state, SIO_PDR_MD4_CFG__A, 0x0000);
1220
			if (status < 0)
1221
				goto error;
1222
			status = write16(state, SIO_PDR_MD5_CFG__A, 0x0000);
1223
			if (status < 0)
1224
				goto error;
1225
			status = write16(state, SIO_PDR_MD6_CFG__A, 0x0000);
1226
			if (status < 0)
1227
				goto error;
1228
			status = write16(state, SIO_PDR_MD7_CFG__A, 0x0000);
1229
			if (status < 0)
1230
				goto error;
R
Ralph Metzler 已提交
1231
		}
1232
		status = write16(state, SIO_PDR_MCLK_CFG__A, sio_pdr_mclk_cfg);
1233
		if (status < 0)
1234
			goto error;
1235
		status = write16(state, SIO_PDR_MD0_CFG__A, sio_pdr_mdx_cfg);
1236
		if (status < 0)
1237 1238 1239 1240 1241 1242 1243 1244 1245 1246
			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)
1247
		pr_err("Error %d on %s\n", status, __func__);
R
Ralph Metzler 已提交
1248 1249 1250
	return status;
}

1251
static int mpegts_disable(struct drxk_state *state)
R
Ralph Metzler 已提交
1252
{
1253 1254
	dprintk(1, "\n");

1255
	return mpegts_configure_pins(state, false);
R
Ralph Metzler 已提交
1256 1257
}

1258 1259
static int bl_chain_cmd(struct drxk_state *state,
		      u16 rom_offset, u16 nr_of_elements, u32 time_out)
R
Ralph Metzler 已提交
1260
{
1261
	u16 bl_status = 0;
R
Ralph Metzler 已提交
1262 1263 1264
	int status;
	unsigned long end;

1265
	dprintk(1, "\n");
R
Ralph Metzler 已提交
1266
	mutex_lock(&state->mutex);
1267 1268 1269
	status = write16(state, SIO_BL_MODE__A, SIO_BL_MODE_CHAIN);
	if (status < 0)
		goto error;
1270
	status = write16(state, SIO_BL_CHAIN_ADDR__A, rom_offset);
1271 1272
	if (status < 0)
		goto error;
1273
	status = write16(state, SIO_BL_CHAIN_LEN__A, nr_of_elements);
1274 1275 1276 1277 1278 1279
	if (status < 0)
		goto error;
	status = write16(state, SIO_BL_ENABLE__A, SIO_BL_ENABLE_ON);
	if (status < 0)
		goto error;

1280
	end = jiffies + msecs_to_jiffies(time_out);
R
Ralph Metzler 已提交
1281
	do {
1282
		msleep(1);
1283
		status = read16(state, SIO_BL_STATUS__A, &bl_status);
1284
		if (status < 0)
1285
			goto error;
1286
	} while ((bl_status == 0x1) &&
1287
			((time_is_after_jiffies(end))));
R
Ralph Metzler 已提交
1288

1289
	if (bl_status == 0x1) {
1290
		pr_err("SIO not ready\n");
1291 1292 1293 1294 1295
		status = -EINVAL;
		goto error2;
	}
error:
	if (status < 0)
1296
		pr_err("Error %d on %s\n", status, __func__);
1297
error2:
R
Ralph Metzler 已提交
1298 1299 1300 1301 1302
	mutex_unlock(&state->mutex);
	return status;
}


1303 1304
static int download_microcode(struct drxk_state *state,
			     const u8 p_mc_image[], u32 length)
R
Ralph Metzler 已提交
1305
{
1306 1307 1308 1309
	const u8 *p_src = p_mc_image;
	u32 address;
	u16 n_blocks;
	u16 block_size;
R
Ralph Metzler 已提交
1310 1311
	u32 offset = 0;
	u32 i;
1312
	int status = 0;
R
Ralph Metzler 已提交
1313

1314 1315
	dprintk(1, "\n");

1316 1317 1318
	/* down the drain (we don't care about MAGIC_WORD) */
#if 0
	/* For future reference */
1319
	drain = (p_src[0] << 8) | p_src[1];
1320
#endif
1321
	p_src += sizeof(u16);
1322
	offset += sizeof(u16);
1323 1324
	n_blocks = (p_src[0] << 8) | p_src[1];
	p_src += sizeof(u16);
1325
	offset += sizeof(u16);
R
Ralph Metzler 已提交
1326

1327 1328 1329 1330
	for (i = 0; i < n_blocks; i += 1) {
		address = (p_src[0] << 24) | (p_src[1] << 16) |
		    (p_src[2] << 8) | p_src[3];
		p_src += sizeof(u32);
1331
		offset += sizeof(u32);
R
Ralph Metzler 已提交
1332

1333 1334
		block_size = ((p_src[0] << 8) | p_src[1]) * sizeof(u16);
		p_src += sizeof(u16);
1335
		offset += sizeof(u16);
R
Ralph Metzler 已提交
1336

1337 1338
#if 0
		/* For future reference */
1339
		flags = (p_src[0] << 8) | p_src[1];
1340
#endif
1341
		p_src += sizeof(u16);
1342
		offset += sizeof(u16);
R
Ralph Metzler 已提交
1343

1344 1345
#if 0
		/* For future reference */
1346
		block_crc = (p_src[0] << 8) | p_src[1];
1347
#endif
1348
		p_src += sizeof(u16);
1349
		offset += sizeof(u16);
1350

1351
		if (offset + block_size > length) {
1352
			pr_err("Firmware is corrupted.\n");
1353 1354 1355
			return -EINVAL;
		}

1356
		status = write_block(state, address, block_size, p_src);
1357
		if (status < 0) {
1358
			pr_err("Error %d while loading firmware\n", status);
R
Ralph Metzler 已提交
1359
			break;
1360
		}
1361 1362
		p_src += block_size;
		offset += block_size;
R
Ralph Metzler 已提交
1363 1364 1365 1366
	}
	return status;
}

1367
static int dvbt_enable_ofdm_token_ring(struct drxk_state *state, bool enable)
R
Ralph Metzler 已提交
1368 1369
{
	int status;
1370
	u16 data = 0;
1371 1372
	u16 desired_ctrl = SIO_OFDM_SH_OFDM_RING_ENABLE_ON;
	u16 desired_status = SIO_OFDM_SH_OFDM_RING_STATUS_ENABLED;
R
Ralph Metzler 已提交
1373 1374
	unsigned long end;

1375 1376
	dprintk(1, "\n");

R
Ralph Metzler 已提交
1377
	if (enable == false) {
1378 1379
		desired_ctrl = SIO_OFDM_SH_OFDM_RING_ENABLE_OFF;
		desired_status = SIO_OFDM_SH_OFDM_RING_STATUS_DOWN;
R
Ralph Metzler 已提交
1380 1381
	}

1382
	status = read16(state, SIO_OFDM_SH_OFDM_RING_STATUS__A, &data);
1383
	if (status >= 0 && data == desired_status) {
R
Ralph Metzler 已提交
1384 1385 1386 1387
		/* tokenring already has correct status */
		return status;
	}
	/* Disable/enable dvbt tokenring bridge   */
1388
	status = write16(state, SIO_OFDM_SH_OFDM_RING_ENABLE__A, desired_ctrl);
R
Ralph Metzler 已提交
1389

1390
	end = jiffies + msecs_to_jiffies(DRXK_OFDM_TR_SHUTDOWN_TIMEOUT);
1391
	do {
1392
		status = read16(state, SIO_OFDM_SH_OFDM_RING_STATUS__A, &data);
1393
		if ((status >= 0 && data == desired_status) || time_is_after_jiffies(end))
1394
			break;
1395 1396
		msleep(1);
	} while (1);
1397
	if (data != desired_status) {
1398
		pr_err("SIO not ready\n");
1399
		return -EINVAL;
R
Ralph Metzler 已提交
1400 1401 1402 1403
	}
	return status;
}

1404
static int mpegts_stop(struct drxk_state *state)
R
Ralph Metzler 已提交
1405 1406
{
	int status = 0;
1407 1408
	u16 fec_oc_snc_mode = 0;
	u16 fec_oc_ipr_mode = 0;
R
Ralph Metzler 已提交
1409

1410 1411
	dprintk(1, "\n");

1412
	/* Gracefull shutdown (byte boundaries) */
1413
	status = read16(state, FEC_OC_SNC_MODE__A, &fec_oc_snc_mode);
1414 1415
	if (status < 0)
		goto error;
1416 1417
	fec_oc_snc_mode |= FEC_OC_SNC_MODE_SHUTDOWN__M;
	status = write16(state, FEC_OC_SNC_MODE__A, fec_oc_snc_mode);
1418 1419 1420 1421
	if (status < 0)
		goto error;

	/* Suppress MCLK during absence of data */
1422
	status = read16(state, FEC_OC_IPR_MODE__A, &fec_oc_ipr_mode);
1423 1424
	if (status < 0)
		goto error;
1425 1426
	fec_oc_ipr_mode |= FEC_OC_IPR_MODE_MCLK_DIS_DAT_ABS__M;
	status = write16(state, FEC_OC_IPR_MODE__A, fec_oc_ipr_mode);
1427 1428 1429

error:
	if (status < 0)
1430
		pr_err("Error %d on %s\n", status, __func__);
R
Ralph Metzler 已提交
1431 1432 1433 1434 1435

	return status;
}

static int scu_command(struct drxk_state *state,
1436 1437
		       u16 cmd, u8 parameter_len,
		       u16 *parameter, u8 result_len, u16 *result)
R
Ralph Metzler 已提交
1438 1439 1440 1441
{
#if (SCU_RAM_PARAM_0__A - SCU_RAM_PARAM_15__A) != 15
#error DRXK register mapping no longer compatible with this routine!
#endif
1442
	u16 cur_cmd = 0;
1443
	int status = -EINVAL;
R
Ralph Metzler 已提交
1444
	unsigned long end;
1445 1446
	u8 buffer[34];
	int cnt = 0, ii;
1447 1448
	const char *p;
	char errname[30];
R
Ralph Metzler 已提交
1449

1450 1451
	dprintk(1, "\n");

1452 1453
	if ((cmd == 0) || ((parameter_len > 0) && (parameter == NULL)) ||
	    ((result_len > 0) && (result == NULL))) {
1454
		pr_err("Error %d on %s\n", status, __func__);
1455 1456
		return status;
	}
R
Ralph Metzler 已提交
1457 1458

	mutex_lock(&state->mutex);
1459 1460 1461

	/* assume that the command register is ready
		since it is checked afterwards */
1462
	for (ii = parameter_len - 1; ii >= 0; ii -= 1) {
1463 1464 1465 1466 1467 1468 1469
		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 -
1470
			(parameter_len - 1), cnt, buffer);
1471 1472
	/* Wait until SCU has processed command */
	end = jiffies + msecs_to_jiffies(DRXK_MAX_WAITTIME);
R
Ralph Metzler 已提交
1473
	do {
1474
		msleep(1);
1475
		status = read16(state, SCU_RAM_COMMAND__A, &cur_cmd);
1476 1477
		if (status < 0)
			goto error;
1478 1479
	} while (!(cur_cmd == DRX_SCU_READY) && (time_is_after_jiffies(end)));
	if (cur_cmd != DRX_SCU_READY) {
1480
		pr_err("SCU not ready\n");
1481 1482 1483 1484
		status = -EIO;
		goto error2;
	}
	/* read results */
1485
	if ((result_len > 0) && (result != NULL)) {
1486 1487
		s16 err;
		int ii;
R
Ralph Metzler 已提交
1488

1489
		for (ii = result_len - 1; ii >= 0; ii -= 1) {
1490
			status = read16(state, SCU_RAM_PARAM_0__A - ii, &result[ii]);
1491
			if (status < 0)
1492
				goto error;
R
Ralph Metzler 已提交
1493
		}
1494 1495 1496

		/* Check if an error was reported by SCU */
		err = (s16)result[0];
1497 1498
		if (err >= 0)
			goto error;
1499

1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516
		/* 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 已提交
1517
		}
1518
		pr_err("%s while sending cmd 0x%04x with params:", p, cmd);
1519 1520 1521
		print_hex_dump_bytes("drxk: ", DUMP_PREFIX_NONE, buffer, cnt);
		status = -EINVAL;
		goto error2;
1522 1523 1524
	}

error:
1525
	if (status < 0)
1526
		pr_err("Error %d on %s\n", status, __func__);
1527 1528
error2:
	mutex_unlock(&state->mutex);
R
Ralph Metzler 已提交
1529 1530 1531
	return status;
}

1532
static int set_iqm_af(struct drxk_state *state, bool active)
R
Ralph Metzler 已提交
1533 1534 1535 1536
{
	u16 data = 0;
	int status;

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

1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561
	/* 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)
1562
		pr_err("Error %d on %s\n", status, __func__);
R
Ralph Metzler 已提交
1563 1564 1565
	return status;
}

1566
static int ctrl_power_mode(struct drxk_state *state, enum drx_power_mode *mode)
R
Ralph Metzler 已提交
1567 1568
{
	int status = 0;
1569
	u16 sio_cc_pwd_mode = 0;
R
Ralph Metzler 已提交
1570

1571 1572
	dprintk(1, "\n");

R
Ralph Metzler 已提交
1573 1574
	/* Check arguments */
	if (mode == NULL)
1575
		return -EINVAL;
R
Ralph Metzler 已提交
1576 1577 1578

	switch (*mode) {
	case DRX_POWER_UP:
1579
		sio_cc_pwd_mode = SIO_CC_PWD_MODE_LEVEL_NONE;
R
Ralph Metzler 已提交
1580 1581
		break;
	case DRXK_POWER_DOWN_OFDM:
1582
		sio_cc_pwd_mode = SIO_CC_PWD_MODE_LEVEL_OFDM;
R
Ralph Metzler 已提交
1583 1584
		break;
	case DRXK_POWER_DOWN_CORE:
1585
		sio_cc_pwd_mode = SIO_CC_PWD_MODE_LEVEL_CLOCK;
R
Ralph Metzler 已提交
1586 1587
		break;
	case DRXK_POWER_DOWN_PLL:
1588
		sio_cc_pwd_mode = SIO_CC_PWD_MODE_LEVEL_PLL;
R
Ralph Metzler 已提交
1589 1590
		break;
	case DRX_POWER_DOWN:
1591
		sio_cc_pwd_mode = SIO_CC_PWD_MODE_LEVEL_OSC;
R
Ralph Metzler 已提交
1592 1593 1594
		break;
	default:
		/* Unknow sleep mode */
1595
		return -EINVAL;
R
Ralph Metzler 已提交
1596 1597 1598
	}

	/* If already in requested power mode, do nothing */
1599
	if (state->m_current_power_mode == *mode)
R
Ralph Metzler 已提交
1600 1601 1602
		return 0;

	/* For next steps make sure to start from DRX_POWER_UP mode */
1603 1604
	if (state->m_current_power_mode != DRX_POWER_UP) {
		status = power_up_device(state);
1605 1606
		if (status < 0)
			goto error;
1607
		status = dvbt_enable_ofdm_token_ring(state, true);
1608 1609
		if (status < 0)
			goto error;
R
Ralph Metzler 已提交
1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623
	}

	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 */
1624
		switch (state->m_operation_mode) {
1625
		case OM_DVBT:
1626
			status = mpegts_stop(state);
1627
			if (status < 0)
1628
				goto error;
1629
			status = power_down_dvbt(state, false);
1630
			if (status < 0)
1631 1632 1633 1634
				goto error;
			break;
		case OM_QAM_ITU_A:
		case OM_QAM_ITU_C:
1635
			status = mpegts_stop(state);
1636
			if (status < 0)
1637
				goto error;
1638
			status = power_down_qam(state);
1639 1640 1641 1642 1643 1644
			if (status < 0)
				goto error;
			break;
		default:
			break;
		}
1645
		status = dvbt_enable_ofdm_token_ring(state, false);
1646 1647
		if (status < 0)
			goto error;
1648
		status = write16(state, SIO_CC_PWD_MODE__A, sio_cc_pwd_mode);
1649 1650 1651 1652 1653
		if (status < 0)
			goto error;
		status = write16(state, SIO_CC_UPDATE__A, SIO_CC_UPDATE_KEY);
		if (status < 0)
			goto error;
R
Ralph Metzler 已提交
1654

1655
		if (*mode != DRXK_POWER_DOWN_OFDM) {
1656
			state->m_hi_cfg_ctrl |=
1657
				SIO_HI_RA_RAM_PAR_5_CFG_SLEEP_ZZZ;
1658
			status = hi_cfg_command(state);
1659 1660 1661
			if (status < 0)
				goto error;
		}
R
Ralph Metzler 已提交
1662
	}
1663
	state->m_current_power_mode = *mode;
1664 1665 1666

error:
	if (status < 0)
1667
		pr_err("Error %d on %s\n", status, __func__);
1668

1669
	return status;
R
Ralph Metzler 已提交
1670 1671
}

1672
static int power_down_dvbt(struct drxk_state *state, bool set_power_mode)
R
Ralph Metzler 已提交
1673
{
1674 1675
	enum drx_power_mode power_mode = DRXK_POWER_DOWN_OFDM;
	u16 cmd_result = 0;
R
Ralph Metzler 已提交
1676 1677 1678
	u16 data = 0;
	int status;

1679 1680
	dprintk(1, "\n");

1681 1682 1683 1684 1685
	status = read16(state, SCU_COMM_EXEC__A, &data);
	if (status < 0)
		goto error;
	if (data == SCU_COMM_EXEC_ACTIVE) {
		/* Send OFDM stop command */
1686
		status = scu_command(state, SCU_RAM_COMMAND_STANDARD_OFDM | SCU_RAM_COMMAND_CMD_DEMOD_STOP, 0, NULL, 1, &cmd_result);
1687
		if (status < 0)
1688 1689
			goto error;
		/* Send OFDM reset command */
1690
		status = scu_command(state, SCU_RAM_COMMAND_STANDARD_OFDM | SCU_RAM_COMMAND_CMD_DEMOD_RESET, 0, NULL, 1, &cmd_result);
1691
		if (status < 0)
1692 1693
			goto error;
	}
R
Ralph Metzler 已提交
1694

1695 1696 1697 1698 1699 1700 1701 1702 1703 1704
	/* 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 已提交
1705

1706
	/* powerdown AFE                   */
1707
	status = set_iqm_af(state, false);
1708 1709 1710 1711
	if (status < 0)
		goto error;

	/* powerdown to OFDM mode          */
1712 1713
	if (set_power_mode) {
		status = ctrl_power_mode(state, &power_mode);
1714 1715 1716 1717 1718
		if (status < 0)
			goto error;
	}
error:
	if (status < 0)
1719
		pr_err("Error %d on %s\n", status, __func__);
R
Ralph Metzler 已提交
1720 1721 1722
	return status;
}

1723 1724
static int setoperation_mode(struct drxk_state *state,
			    enum operation_mode o_mode)
R
Ralph Metzler 已提交
1725 1726 1727
{
	int status = 0;

1728
	dprintk(1, "\n");
R
Ralph Metzler 已提交
1729
	/*
1730 1731 1732 1733
	   Stop and power down previous standard
	   TODO investigate total power down instead of partial
	   power down depending on "previous" standard.
	 */
R
Ralph Metzler 已提交
1734

1735 1736 1737 1738
	/* 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 已提交
1739

1740
	/* Device is already at the required mode */
1741
	if (state->m_operation_mode == o_mode)
1742 1743
		return 0;

1744
	switch (state->m_operation_mode) {
1745 1746 1747 1748
		/* OM_NONE was added for start up */
	case OM_NONE:
		break;
	case OM_DVBT:
1749
		status = mpegts_stop(state);
1750
		if (status < 0)
1751
			goto error;
1752
		status = power_down_dvbt(state, true);
1753 1754
		if (status < 0)
			goto error;
1755
		state->m_operation_mode = OM_NONE;
1756 1757 1758
		break;
	case OM_QAM_ITU_A:	/* fallthrough */
	case OM_QAM_ITU_C:
1759
		status = mpegts_stop(state);
1760 1761
		if (status < 0)
			goto error;
1762
		status = power_down_qam(state);
1763 1764
		if (status < 0)
			goto error;
1765
		state->m_operation_mode = OM_NONE;
1766 1767 1768 1769 1770 1771
		break;
	case OM_QAM_ITU_B:
	default:
		status = -EINVAL;
		goto error;
	}
1772

1773 1774 1775
	/*
		Power up new standard
		*/
1776
	switch (o_mode) {
1777
	case OM_DVBT:
1778
		dprintk(1, ": DVB-T\n");
1779 1780
		state->m_operation_mode = o_mode;
		status = set_dvbt_standard(state, o_mode);
1781 1782 1783 1784 1785
		if (status < 0)
			goto error;
		break;
	case OM_QAM_ITU_A:	/* fallthrough */
	case OM_QAM_ITU_C:
1786
		dprintk(1, ": DVB-C Annex %c\n",
1787 1788 1789
			(state->m_operation_mode == OM_QAM_ITU_A) ? 'A' : 'C');
		state->m_operation_mode = o_mode;
		status = set_qam_standard(state, o_mode);
1790 1791 1792 1793 1794 1795
		if (status < 0)
			goto error;
		break;
	case OM_QAM_ITU_B:
	default:
		status = -EINVAL;
1796 1797 1798
	}
error:
	if (status < 0)
1799
		pr_err("Error %d on %s\n", status, __func__);
1800
	return status;
R
Ralph Metzler 已提交
1801 1802
}

1803 1804
static int start(struct drxk_state *state, s32 offset_freq,
		 s32 intermediate_frequency)
R
Ralph Metzler 已提交
1805
{
1806 1807
	int status = -EINVAL;

1808 1809
	u16 i_freqk_hz;
	s32 offsetk_hz = offset_freq / 1000;
R
Ralph Metzler 已提交
1810

1811
	dprintk(1, "\n");
1812 1813
	if (state->m_drxk_state != DRXK_STOPPED &&
		state->m_drxk_state != DRXK_DTV_STARTED)
1814
		goto error;
R
Ralph Metzler 已提交
1815

1816
	state->m_b_mirror_freq_spect = (state->props.inversion == INVERSION_ON);
R
Ralph Metzler 已提交
1817

1818 1819 1820
	if (intermediate_frequency < 0) {
		state->m_b_mirror_freq_spect = !state->m_b_mirror_freq_spect;
		intermediate_frequency = -intermediate_frequency;
1821
	}
R
Ralph Metzler 已提交
1822

1823
	switch (state->m_operation_mode) {
1824 1825
	case OM_QAM_ITU_A:
	case OM_QAM_ITU_C:
1826 1827
		i_freqk_hz = (intermediate_frequency / 1000);
		status = set_qam(state, i_freqk_hz, offsetk_hz);
1828 1829
		if (status < 0)
			goto error;
1830
		state->m_drxk_state = DRXK_DTV_STARTED;
1831 1832
		break;
	case OM_DVBT:
1833 1834
		i_freqk_hz = (intermediate_frequency / 1000);
		status = mpegts_stop(state);
1835 1836
		if (status < 0)
			goto error;
1837
		status = set_dvbt(state, i_freqk_hz, offsetk_hz);
1838 1839
		if (status < 0)
			goto error;
1840
		status = dvbt_start(state);
1841 1842
		if (status < 0)
			goto error;
1843
		state->m_drxk_state = DRXK_DTV_STARTED;
1844 1845 1846 1847 1848 1849
		break;
	default:
		break;
	}
error:
	if (status < 0)
1850
		pr_err("Error %d on %s\n", status, __func__);
R
Ralph Metzler 已提交
1851 1852 1853
	return status;
}

1854
static int shut_down(struct drxk_state *state)
R
Ralph Metzler 已提交
1855
{
1856 1857
	dprintk(1, "\n");

1858
	mpegts_stop(state);
R
Ralph Metzler 已提交
1859 1860 1861
	return 0;
}

1862
static int get_lock_status(struct drxk_state *state, u32 *p_lock_status)
R
Ralph Metzler 已提交
1863
{
1864
	int status = -EINVAL;
R
Ralph Metzler 已提交
1865

1866 1867
	dprintk(1, "\n");

1868
	if (p_lock_status == NULL)
1869
		goto error;
R
Ralph Metzler 已提交
1870

1871
	*p_lock_status = NOT_LOCKED;
R
Ralph Metzler 已提交
1872 1873

	/* define the SCU command code */
1874
	switch (state->m_operation_mode) {
R
Ralph Metzler 已提交
1875 1876 1877
	case OM_QAM_ITU_A:
	case OM_QAM_ITU_B:
	case OM_QAM_ITU_C:
1878
		status = get_qam_lock_status(state, p_lock_status);
R
Ralph Metzler 已提交
1879 1880
		break;
	case OM_DVBT:
1881
		status = get_dvbt_lock_status(state, p_lock_status);
R
Ralph Metzler 已提交
1882 1883 1884 1885
		break;
	default:
		break;
	}
1886 1887
error:
	if (status < 0)
1888
		pr_err("Error %d on %s\n", status, __func__);
R
Ralph Metzler 已提交
1889 1890 1891
	return status;
}

1892
static int mpegts_start(struct drxk_state *state)
R
Ralph Metzler 已提交
1893
{
1894
	int status;
R
Ralph Metzler 已提交
1895

1896
	u16 fec_oc_snc_mode = 0;
R
Ralph Metzler 已提交
1897

1898
	/* Allow OC to sync again */
1899
	status = read16(state, FEC_OC_SNC_MODE__A, &fec_oc_snc_mode);
1900 1901
	if (status < 0)
		goto error;
1902 1903
	fec_oc_snc_mode &= ~FEC_OC_SNC_MODE_SHUTDOWN__M;
	status = write16(state, FEC_OC_SNC_MODE__A, fec_oc_snc_mode);
1904 1905 1906 1907 1908
	if (status < 0)
		goto error;
	status = write16(state, FEC_OC_SNC_UNLOCK__A, 1);
error:
	if (status < 0)
1909
		pr_err("Error %d on %s\n", status, __func__);
R
Ralph Metzler 已提交
1910 1911 1912
	return status;
}

1913
static int mpegts_dto_init(struct drxk_state *state)
R
Ralph Metzler 已提交
1914
{
1915
	int status;
R
Ralph Metzler 已提交
1916

1917 1918
	dprintk(1, "\n");

1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954
	/* 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)
1955
		pr_err("Error %d on %s\n", status, __func__);
R
Ralph Metzler 已提交
1956 1957 1958 1959

	return status;
}

1960 1961
static int mpegts_dto_setup(struct drxk_state *state,
			  enum operation_mode o_mode)
R
Ralph Metzler 已提交
1962
{
1963
	int status;
R
Ralph Metzler 已提交
1964

1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975
	u16 fec_oc_reg_mode = 0;	/* FEC_OC_MODE       register value */
	u16 fec_oc_reg_ipr_mode = 0;	/* FEC_OC_IPR_MODE   register value */
	u16 fec_oc_dto_mode = 0;	/* FEC_OC_IPR_INVERT register value */
	u16 fec_oc_fct_mode = 0;	/* FEC_OC_IPR_INVERT register value */
	u16 fec_oc_dto_period = 2;	/* FEC_OC_IPR_INVERT register value */
	u16 fec_oc_dto_burst_len = 188;	/* FEC_OC_IPR_INVERT register value */
	u32 fec_oc_rcn_ctl_rate = 0;	/* FEC_OC_IPR_INVERT register value */
	u16 fec_oc_tmd_mode = 0;
	u16 fec_oc_tmd_int_upd_rate = 0;
	u32 max_bit_rate = 0;
	bool static_clk = false;
R
Ralph Metzler 已提交
1976

1977 1978
	dprintk(1, "\n");

1979
	/* Check insertion of the Reed-Solomon parity bytes */
1980
	status = read16(state, FEC_OC_MODE__A, &fec_oc_reg_mode);
1981 1982
	if (status < 0)
		goto error;
1983
	status = read16(state, FEC_OC_IPR_MODE__A, &fec_oc_reg_ipr_mode);
1984 1985
	if (status < 0)
		goto error;
1986 1987 1988
	fec_oc_reg_mode &= (~FEC_OC_MODE_PARITY__M);
	fec_oc_reg_ipr_mode &= (~FEC_OC_IPR_MODE_MVAL_DIS_PAR__M);
	if (state->m_insert_rs_byte == true) {
1989
		/* enable parity symbol forward */
1990
		fec_oc_reg_mode |= FEC_OC_MODE_PARITY__M;
1991
		/* MVAL disable during parity bytes */
1992
		fec_oc_reg_ipr_mode |= FEC_OC_IPR_MODE_MVAL_DIS_PAR__M;
1993
		/* TS burst length to 204 */
1994
		fec_oc_dto_burst_len = 204;
1995
	}
R
Ralph Metzler 已提交
1996

1997
	/* Check serial or parrallel output */
1998 1999
	fec_oc_reg_ipr_mode &= (~(FEC_OC_IPR_MODE_SERIAL__M));
	if (state->m_enable_parallel == false) {
2000
		/* MPEG data output is serial -> set ipr_mode[0] */
2001
		fec_oc_reg_ipr_mode |= FEC_OC_IPR_MODE_SERIAL__M;
2002
	}
R
Ralph Metzler 已提交
2003

2004
	switch (o_mode) {
2005
	case OM_DVBT:
2006 2007 2008 2009
		max_bit_rate = state->m_dvbt_bitrate;
		fec_oc_tmd_mode = 3;
		fec_oc_rcn_ctl_rate = 0xC00000;
		static_clk = state->m_dvbt_static_clk;
2010 2011 2012
		break;
	case OM_QAM_ITU_A:	/* fallthrough */
	case OM_QAM_ITU_C:
2013 2014 2015 2016
		fec_oc_tmd_mode = 0x0004;
		fec_oc_rcn_ctl_rate = 0xD2B4EE;	/* good for >63 Mb/s */
		max_bit_rate = state->m_dvbc_bitrate;
		static_clk = state->m_dvbc_static_clk;
2017 2018 2019 2020 2021 2022 2023 2024
		break;
	default:
		status = -EINVAL;
	}		/* switch (standard) */
	if (status < 0)
		goto error;

	/* Configure DTO's */
2025 2026
	if (static_clk) {
		u32 bit_rate = 0;
2027 2028 2029 2030 2031

		/* 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 */
2032
		fec_oc_dto_mode = (FEC_OC_DTO_MODE_DYNAMIC__M |
2033
				FEC_OC_DTO_MODE_OFFSET_ENABLE__M);
2034
		fec_oc_fct_mode = (FEC_OC_FCT_MODE_RAT_ENA__M |
2035 2036 2037
				FEC_OC_FCT_MODE_VIRT_ENA__M);

		/* Check user defined bitrate */
2038 2039 2040
		bit_rate = max_bit_rate;
		if (bit_rate > 75900000UL) {	/* max is 75.9 Mb/s */
			bit_rate = 75900000UL;
R
Ralph Metzler 已提交
2041
		}
2042 2043 2044
		/* Rational DTO period:
			dto_period = (Fsys / bitrate) - 2

2045
			result should be floored,
2046 2047
			to make sure >= requested bitrate
			*/
2048 2049 2050 2051
		fec_oc_dto_period = (u16) (((state->m_sys_clock_freq)
						* 1000) / bit_rate);
		if (fec_oc_dto_period <= 2)
			fec_oc_dto_period = 0;
2052
		else
2053 2054
			fec_oc_dto_period -= 2;
		fec_oc_tmd_int_upd_rate = 8;
2055
	} else {
2056 2057 2058 2059
		/* (commonAttr->static_clk == false) => dynamic mode */
		fec_oc_dto_mode = FEC_OC_DTO_MODE_DYNAMIC__M;
		fec_oc_fct_mode = FEC_OC_FCT_MODE__PRE;
		fec_oc_tmd_int_upd_rate = 5;
2060
	}
R
Ralph Metzler 已提交
2061

2062
	/* Write appropriate registers with requested configuration */
2063
	status = write16(state, FEC_OC_DTO_BURST_LEN__A, fec_oc_dto_burst_len);
2064 2065
	if (status < 0)
		goto error;
2066
	status = write16(state, FEC_OC_DTO_PERIOD__A, fec_oc_dto_period);
2067 2068
	if (status < 0)
		goto error;
2069
	status = write16(state, FEC_OC_DTO_MODE__A, fec_oc_dto_mode);
2070 2071
	if (status < 0)
		goto error;
2072
	status = write16(state, FEC_OC_FCT_MODE__A, fec_oc_fct_mode);
2073 2074
	if (status < 0)
		goto error;
2075
	status = write16(state, FEC_OC_MODE__A, fec_oc_reg_mode);
2076 2077
	if (status < 0)
		goto error;
2078
	status = write16(state, FEC_OC_IPR_MODE__A, fec_oc_reg_ipr_mode);
2079 2080
	if (status < 0)
		goto error;
R
Ralph Metzler 已提交
2081

2082
	/* Rate integration settings */
2083
	status = write32(state, FEC_OC_RCN_CTL_RATE_LO__A, fec_oc_rcn_ctl_rate);
2084 2085
	if (status < 0)
		goto error;
2086
	status = write16(state, FEC_OC_TMD_INT_UPD_RATE__A, fec_oc_tmd_int_upd_rate);
2087 2088
	if (status < 0)
		goto error;
2089
	status = write16(state, FEC_OC_TMD_MODE__A, fec_oc_tmd_mode);
2090 2091
error:
	if (status < 0)
2092
		pr_err("Error %d on %s\n", status, __func__);
R
Ralph Metzler 已提交
2093 2094 2095
	return status;
}

2096
static int mpegts_configure_polarity(struct drxk_state *state)
R
Ralph Metzler 已提交
2097
{
2098
	u16 fec_oc_reg_ipr_invert = 0;
R
Ralph Metzler 已提交
2099 2100

	/* Data mask for the output data byte */
2101
	u16 invert_data_mask =
2102 2103 2104 2105
	    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 已提交
2106

2107 2108
	dprintk(1, "\n");

R
Ralph Metzler 已提交
2109
	/* Control selective inversion of output bits */
2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126
	fec_oc_reg_ipr_invert &= (~(invert_data_mask));
	if (state->m_invert_data == true)
		fec_oc_reg_ipr_invert |= invert_data_mask;
	fec_oc_reg_ipr_invert &= (~(FEC_OC_IPR_INVERT_MERR__M));
	if (state->m_invert_err == true)
		fec_oc_reg_ipr_invert |= FEC_OC_IPR_INVERT_MERR__M;
	fec_oc_reg_ipr_invert &= (~(FEC_OC_IPR_INVERT_MSTRT__M));
	if (state->m_invert_str == true)
		fec_oc_reg_ipr_invert |= FEC_OC_IPR_INVERT_MSTRT__M;
	fec_oc_reg_ipr_invert &= (~(FEC_OC_IPR_INVERT_MVAL__M));
	if (state->m_invert_val == true)
		fec_oc_reg_ipr_invert |= FEC_OC_IPR_INVERT_MVAL__M;
	fec_oc_reg_ipr_invert &= (~(FEC_OC_IPR_INVERT_MCLK__M));
	if (state->m_invert_clk == true)
		fec_oc_reg_ipr_invert |= FEC_OC_IPR_INVERT_MCLK__M;

	return write16(state, FEC_OC_IPR_INVERT__A, fec_oc_reg_ipr_invert);
R
Ralph Metzler 已提交
2127 2128 2129 2130
}

#define   SCU_RAM_AGC_KI_INV_RF_POL__M 0x4000

2131 2132
static int set_agc_rf(struct drxk_state *state,
		    struct s_cfg_agc *p_agc_cfg, bool is_dtv)
R
Ralph Metzler 已提交
2133
{
2134 2135
	int status = -EINVAL;
	u16 data = 0;
2136
	struct s_cfg_agc *p_if_agc_settings;
R
Ralph Metzler 已提交
2137

2138 2139
	dprintk(1, "\n");

2140
	if (p_agc_cfg == NULL)
2141
		goto error;
R
Ralph Metzler 已提交
2142

2143
	switch (p_agc_cfg->ctrl_mode) {
2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155
	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 已提交
2156

2157 2158
		/* Enable SCU RF AGC loop */
		data &= ~SCU_RAM_AGC_CONFIG_DISABLE_RF_AGC__M;
R
Ralph Metzler 已提交
2159

2160
		/* Polarity */
2161
		if (state->m_rf_agc_pol)
2162 2163 2164 2165 2166 2167
			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 已提交
2168

2169 2170 2171 2172
		/* Set speed (using complementary reduction value) */
		status = read16(state, SCU_RAM_AGC_KI_RED__A, &data);
		if (status < 0)
			goto error;
R
Ralph Metzler 已提交
2173

2174
		data &= ~SCU_RAM_AGC_KI_RED_RAGC_RED__M;
2175
		data |= (~(p_agc_cfg->speed <<
2176 2177
				SCU_RAM_AGC_KI_RED_RAGC_RED__B)
				& SCU_RAM_AGC_KI_RED_RAGC_RED__M);
R
Ralph Metzler 已提交
2178

2179 2180 2181
		status = write16(state, SCU_RAM_AGC_KI_RED__A, data);
		if (status < 0)
			goto error;
R
Ralph Metzler 已提交
2182

2183 2184 2185 2186
		if (is_dvbt(state))
			p_if_agc_settings = &state->m_dvbt_if_agc_cfg;
		else if (is_qam(state))
			p_if_agc_settings = &state->m_qam_if_agc_cfg;
2187
		else
2188 2189
			p_if_agc_settings = &state->m_atv_if_agc_cfg;
		if (p_if_agc_settings == NULL) {
2190 2191 2192
			status = -EINVAL;
			goto error;
		}
R
Ralph Metzler 已提交
2193

2194
		/* Set TOP, only if IF-AGC is in AUTO mode */
2195 2196
		if (p_if_agc_settings->ctrl_mode == DRXK_AGC_CTRL_AUTO)
			status = write16(state, SCU_RAM_AGC_IF_IACCU_HI_TGT_MAX__A, p_agc_cfg->top);
2197
			if (status < 0)
2198
				goto error;
R
Ralph Metzler 已提交
2199

2200
		/* Cut-Off current */
2201
		status = write16(state, SCU_RAM_AGC_RF_IACCU_HI_CO__A, p_agc_cfg->cut_off_current);
2202 2203
		if (status < 0)
			goto error;
R
Ralph Metzler 已提交
2204

2205
		/* Max. output level */
2206
		status = write16(state, SCU_RAM_AGC_RF_MAX__A, p_agc_cfg->max_output_level);
2207 2208
		if (status < 0)
			goto error;
R
Ralph Metzler 已提交
2209

2210
		break;
R
Ralph Metzler 已提交
2211

2212 2213 2214 2215 2216 2217 2218 2219 2220
	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 已提交
2221

2222 2223 2224 2225 2226
		/* 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;
2227
		if (state->m_rf_agc_pol)
2228 2229 2230 2231 2232 2233
			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 已提交
2234

2235 2236 2237 2238
		/* 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 已提交
2239

2240
		/* Write value to output pin */
2241
		status = write16(state, SCU_RAM_AGC_RF_IACCU_HI__A, p_agc_cfg->output_level);
2242 2243 2244
		if (status < 0)
			goto error;
		break;
R
Ralph Metzler 已提交
2245

2246 2247 2248 2249 2250 2251 2252 2253 2254
	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 已提交
2255

2256 2257 2258 2259 2260 2261 2262 2263 2264
		/* 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 已提交
2265

2266 2267
	default:
		status = -EINVAL;
R
Ralph Metzler 已提交
2268

2269 2270 2271
	}
error:
	if (status < 0)
2272
		pr_err("Error %d on %s\n", status, __func__);
R
Ralph Metzler 已提交
2273 2274 2275 2276 2277
	return status;
}

#define SCU_RAM_AGC_KI_INV_IF_POL__M 0x2000

2278 2279
static int set_agc_if(struct drxk_state *state,
		    struct s_cfg_agc *p_agc_cfg, bool is_dtv)
R
Ralph Metzler 已提交
2280 2281 2282
{
	u16 data = 0;
	int status = 0;
2283
	struct s_cfg_agc *p_rf_agc_settings;
R
Ralph Metzler 已提交
2284

2285 2286
	dprintk(1, "\n");

2287
	switch (p_agc_cfg->ctrl_mode) {
2288
	case DRXK_AGC_CTRL_AUTO:
R
Ralph Metzler 已提交
2289

2290 2291 2292 2293 2294 2295 2296 2297
		/* 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 已提交
2298

2299 2300 2301
		status = read16(state, SCU_RAM_AGC_CONFIG__A, &data);
		if (status < 0)
			goto error;
R
Ralph Metzler 已提交
2302

2303 2304
		/* Enable SCU IF AGC loop */
		data &= ~SCU_RAM_AGC_CONFIG_DISABLE_IF_AGC__M;
R
Ralph Metzler 已提交
2305

2306
		/* Polarity */
2307
		if (state->m_if_agc_pol)
2308 2309 2310 2311 2312 2313
			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 已提交
2314

2315 2316 2317 2318 2319
		/* 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;
2320
		data |= (~(p_agc_cfg->speed <<
2321 2322
				SCU_RAM_AGC_KI_RED_IAGC_RED__B)
				& SCU_RAM_AGC_KI_RED_IAGC_RED__M);
R
Ralph Metzler 已提交
2323

2324 2325 2326
		status = write16(state, SCU_RAM_AGC_KI_RED__A, data);
		if (status < 0)
			goto error;
R
Ralph Metzler 已提交
2327

2328 2329
		if (is_qam(state))
			p_rf_agc_settings = &state->m_qam_rf_agc_cfg;
2330
		else
2331 2332
			p_rf_agc_settings = &state->m_atv_rf_agc_cfg;
		if (p_rf_agc_settings == NULL)
2333 2334
			return -1;
		/* Restore TOP */
2335
		status = write16(state, SCU_RAM_AGC_IF_IACCU_HI_TGT_MAX__A, p_rf_agc_settings->top);
2336 2337 2338
		if (status < 0)
			goto error;
		break;
R
Ralph Metzler 已提交
2339

2340
	case DRXK_AGC_CTRL_USER:
R
Ralph Metzler 已提交
2341

2342 2343 2344 2345 2346 2347 2348 2349
		/* 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 已提交
2350

2351 2352 2353
		status = read16(state, SCU_RAM_AGC_CONFIG__A, &data);
		if (status < 0)
			goto error;
R
Ralph Metzler 已提交
2354

2355 2356
		/* Disable SCU IF AGC loop */
		data |= SCU_RAM_AGC_CONFIG_DISABLE_IF_AGC__M;
R
Ralph Metzler 已提交
2357

2358
		/* Polarity */
2359
		if (state->m_if_agc_pol)
2360 2361 2362 2363 2364 2365
			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 已提交
2366

2367
		/* Write value to output pin */
2368
		status = write16(state, SCU_RAM_AGC_IF_IACCU_HI_TGT_MAX__A, p_agc_cfg->output_level);
2369 2370 2371
		if (status < 0)
			goto error;
		break;
R
Ralph Metzler 已提交
2372

2373
	case DRXK_AGC_CTRL_OFF:
R
Ralph Metzler 已提交
2374

2375 2376
		/* Disable If AGC DAC */
		status = read16(state, IQM_AF_STDBY__A, &data);
2377
		if (status < 0)
2378 2379 2380 2381 2382
			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 已提交
2383

2384 2385 2386 2387 2388 2389 2390 2391 2392
		/* 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;
2393
	}		/* switch (agcSettingsIf->ctrl_mode) */
R
Ralph Metzler 已提交
2394

2395 2396
	/* always set the top to support
		configurations without if-loop */
2397
	status = write16(state, SCU_RAM_AGC_INGAIN_TGT_MIN__A, p_agc_cfg->top);
2398 2399
error:
	if (status < 0)
2400
		pr_err("Error %d on %s\n", status, __func__);
R
Ralph Metzler 已提交
2401 2402 2403
	return status;
}

2404 2405
static int get_qam_signal_to_noise(struct drxk_state *state,
			       s32 *p_signal_to_noise)
R
Ralph Metzler 已提交
2406 2407
{
	int status = 0;
2408
	u16 qam_sl_err_power = 0;	/* accum. error between
2409
					raw and sliced symbols */
2410
	u32 qam_sl_sig_power = 0;	/* used for MER, depends of
2411
					QAM modulation */
2412
	u32 qam_sl_mer = 0;	/* QAM MER */
R
Ralph Metzler 已提交
2413

2414 2415
	dprintk(1, "\n");

2416
	/* MER calculation */
R
Ralph Metzler 已提交
2417

2418
	/* get the register value needed for MER */
2419
	status = read16(state, QAM_SL_ERR_POWER__A, &qam_sl_err_power);
2420
	if (status < 0) {
2421
		pr_err("Error %d on %s\n", status, __func__);
2422 2423 2424
		return -EINVAL;
	}

2425
	switch (state->props.modulation) {
2426
	case QAM_16:
2427
		qam_sl_sig_power = DRXK_QAM_SL_SIG_POWER_QAM16 << 2;
2428 2429
		break;
	case QAM_32:
2430
		qam_sl_sig_power = DRXK_QAM_SL_SIG_POWER_QAM32 << 2;
2431 2432
		break;
	case QAM_64:
2433
		qam_sl_sig_power = DRXK_QAM_SL_SIG_POWER_QAM64 << 2;
2434 2435
		break;
	case QAM_128:
2436
		qam_sl_sig_power = DRXK_QAM_SL_SIG_POWER_QAM128 << 2;
2437 2438 2439
		break;
	default:
	case QAM_256:
2440
		qam_sl_sig_power = DRXK_QAM_SL_SIG_POWER_QAM256 << 2;
2441 2442 2443
		break;
	}

2444 2445 2446
	if (qam_sl_err_power > 0) {
		qam_sl_mer = log10times100(qam_sl_sig_power) -
			log10times100((u32) qam_sl_err_power);
2447
	}
2448
	*p_signal_to_noise = qam_sl_mer;
R
Ralph Metzler 已提交
2449 2450 2451 2452

	return status;
}

2453 2454
static int get_dvbt_signal_to_noise(struct drxk_state *state,
				s32 *p_signal_to_noise)
R
Ralph Metzler 已提交
2455
{
2456
	int status;
2457 2458 2459 2460 2461 2462 2463 2464
	u16 reg_data = 0;
	u32 eq_reg_td_sqr_err_i = 0;
	u32 eq_reg_td_sqr_err_q = 0;
	u16 eq_reg_td_sqr_err_exp = 0;
	u16 eq_reg_td_tps_pwr_ofs = 0;
	u16 eq_reg_td_req_smb_cnt = 0;
	u32 tps_cnt = 0;
	u32 sqr_err_iq = 0;
2465 2466 2467
	u32 a = 0;
	u32 b = 0;
	u32 c = 0;
2468 2469
	u32 i_mer = 0;
	u16 transmission_params = 0;
R
Ralph Metzler 已提交
2470

2471
	dprintk(1, "\n");
R
Ralph Metzler 已提交
2472

2473
	status = read16(state, OFDM_EQ_TOP_TD_TPS_PWR_OFS__A, &eq_reg_td_tps_pwr_ofs);
2474 2475
	if (status < 0)
		goto error;
2476
	status = read16(state, OFDM_EQ_TOP_TD_REQ_SMB_CNT__A, &eq_reg_td_req_smb_cnt);
2477 2478
	if (status < 0)
		goto error;
2479
	status = read16(state, OFDM_EQ_TOP_TD_SQR_ERR_EXP__A, &eq_reg_td_sqr_err_exp);
2480 2481
	if (status < 0)
		goto error;
2482
	status = read16(state, OFDM_EQ_TOP_TD_SQR_ERR_I__A, &reg_data);
2483 2484 2485
	if (status < 0)
		goto error;
	/* Extend SQR_ERR_I operational range */
2486 2487 2488 2489
	eq_reg_td_sqr_err_i = (u32) reg_data;
	if ((eq_reg_td_sqr_err_exp > 11) &&
		(eq_reg_td_sqr_err_i < 0x00000FFFUL)) {
		eq_reg_td_sqr_err_i += 0x00010000UL;
2490
	}
2491
	status = read16(state, OFDM_EQ_TOP_TD_SQR_ERR_Q__A, &reg_data);
2492 2493 2494
	if (status < 0)
		goto error;
	/* Extend SQR_ERR_Q operational range */
2495 2496 2497 2498
	eq_reg_td_sqr_err_q = (u32) reg_data;
	if ((eq_reg_td_sqr_err_exp > 11) &&
		(eq_reg_td_sqr_err_q < 0x00000FFFUL))
		eq_reg_td_sqr_err_q += 0x00010000UL;
2499

2500
	status = read16(state, OFDM_SC_RA_RAM_OP_PARAM__A, &transmission_params);
2501 2502 2503 2504 2505 2506
	if (status < 0)
		goto error;

	/* Check input data for MER */

	/* MER calculation (in 0.1 dB) without math.h */
2507 2508 2509
	if ((eq_reg_td_tps_pwr_ofs == 0) || (eq_reg_td_req_smb_cnt == 0))
		i_mer = 0;
	else if ((eq_reg_td_sqr_err_i + eq_reg_td_sqr_err_q) == 0) {
2510 2511 2512
		/* No error at all, this must be the HW reset value
			* Apparently no first measurement yet
			* Set MER to 0.0 */
2513
		i_mer = 0;
2514
	} else {
2515 2516 2517
		sqr_err_iq = (eq_reg_td_sqr_err_i + eq_reg_td_sqr_err_q) <<
			eq_reg_td_sqr_err_exp;
		if ((transmission_params &
2518 2519
			OFDM_SC_RA_RAM_OP_PARAM_MODE__M)
			== OFDM_SC_RA_RAM_OP_PARAM_MODE_2K)
2520
			tps_cnt = 17;
2521
		else
2522
			tps_cnt = 68;
2523 2524

		/* IMER = 100 * log10 (x)
2525 2526
			where x = (eq_reg_td_tps_pwr_ofs^2 *
			eq_reg_td_req_smb_cnt * tps_cnt)/sqr_err_iq
2527 2528

			=> IMER = a + b -c
2529 2530 2531
			where a = 100 * log10 (eq_reg_td_tps_pwr_ofs^2)
			b = 100 * log10 (eq_reg_td_req_smb_cnt * tps_cnt)
			c = 100 * log10 (sqr_err_iq)
2532 2533 2534
			*/

		/* log(x) x = 9bits * 9bits->18 bits  */
2535 2536
		a = log10times100(eq_reg_td_tps_pwr_ofs *
					eq_reg_td_tps_pwr_ofs);
2537
		/* log(x) x = 16bits * 7bits->23 bits  */
2538
		b = log10times100(eq_reg_td_req_smb_cnt * tps_cnt);
2539
		/* log(x) x = (16bits + 16bits) << 15 ->32 bits  */
2540
		c = log10times100(sqr_err_iq);
2541

2542
		i_mer = a + b - c;
2543
	}
2544
	*p_signal_to_noise = i_mer;
R
Ralph Metzler 已提交
2545

2546 2547
error:
	if (status < 0)
2548
		pr_err("Error %d on %s\n", status, __func__);
R
Ralph Metzler 已提交
2549 2550 2551
	return status;
}

2552
static int get_signal_to_noise(struct drxk_state *state, s32 *p_signal_to_noise)
R
Ralph Metzler 已提交
2553
{
2554 2555
	dprintk(1, "\n");

2556 2557
	*p_signal_to_noise = 0;
	switch (state->m_operation_mode) {
R
Ralph Metzler 已提交
2558
	case OM_DVBT:
2559
		return get_dvbt_signal_to_noise(state, p_signal_to_noise);
R
Ralph Metzler 已提交
2560 2561
	case OM_QAM_ITU_A:
	case OM_QAM_ITU_C:
2562
		return get_qam_signal_to_noise(state, p_signal_to_noise);
R
Ralph Metzler 已提交
2563 2564 2565 2566 2567 2568 2569
	default:
		break;
	}
	return 0;
}

#if 0
2570
static int get_dvbt_quality(struct drxk_state *state, s32 *p_quality)
R
Ralph Metzler 已提交
2571 2572 2573 2574
{
	/* SNR Values for quasi errorfree reception rom Nordig 2.2 */
	int status = 0;

2575 2576
	dprintk(1, "\n");

2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593
	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 已提交
2594

2595
	*p_quality = 0;
R
Ralph Metzler 已提交
2596 2597

	do {
2598 2599 2600 2601 2602
		s32 signal_to_noise = 0;
		u16 constellation = 0;
		u16 code_rate = 0;
		u32 signal_to_noise_rel;
		u32 ber_quality;
R
Ralph Metzler 已提交
2603

2604
		status = get_dvbt_signal_to_noise(state, &signal_to_noise);
2605 2606
		if (status < 0)
			break;
2607
		status = read16(state, OFDM_EQ_TOP_TD_TPS_CONST__A, &constellation);
2608 2609
		if (status < 0)
			break;
2610
		constellation &= OFDM_EQ_TOP_TD_TPS_CONST__M;
R
Ralph Metzler 已提交
2611

2612
		status = read16(state, OFDM_EQ_TOP_TD_TPS_CODE_HP__A, &code_rate);
2613 2614
		if (status < 0)
			break;
2615
		code_rate &= OFDM_EQ_TOP_TD_TPS_CODE_HP__M;
R
Ralph Metzler 已提交
2616

2617 2618
		if (constellation > OFDM_EQ_TOP_TD_TPS_CONST_64QAM ||
		    code_rate > OFDM_EQ_TOP_TD_TPS_CODE_LP_7_8)
R
Ralph Metzler 已提交
2619
			break;
2620 2621 2622 2623 2624 2625 2626 2627 2628
		signal_to_noise_rel = signal_to_noise -
		    QE_SN[constellation * 5 + code_rate];
		ber_quality = 100;

		if (signal_to_noise_rel < -70)
			*p_quality = 0;
		else if (signal_to_noise_rel < 30)
			*p_quality = ((signal_to_noise_rel + 70) *
				     ber_quality) / 100;
R
Ralph Metzler 已提交
2629
		else
2630
			*p_quality = ber_quality;
2631
	} while (0);
R
Ralph Metzler 已提交
2632 2633 2634
	return 0;
};

2635
static int get_dvbc_quality(struct drxk_state *state, s32 *p_quality)
R
Ralph Metzler 已提交
2636 2637
{
	int status = 0;
2638
	*p_quality = 0;
R
Ralph Metzler 已提交
2639

2640 2641
	dprintk(1, "\n");

R
Ralph Metzler 已提交
2642
	do {
2643 2644 2645
		u32 signal_to_noise = 0;
		u32 ber_quality = 100;
		u32 signal_to_noise_rel = 0;
R
Ralph Metzler 已提交
2646

2647
		status = get_qam_signal_to_noise(state, &signal_to_noise);
2648 2649
		if (status < 0)
			break;
R
Ralph Metzler 已提交
2650

2651
		switch (state->props.modulation) {
R
Ralph Metzler 已提交
2652
		case QAM_16:
2653
			signal_to_noise_rel = signal_to_noise - 200;
R
Ralph Metzler 已提交
2654 2655
			break;
		case QAM_32:
2656
			signal_to_noise_rel = signal_to_noise - 230;
2657
			break;	/* Not in NorDig */
R
Ralph Metzler 已提交
2658
		case QAM_64:
2659
			signal_to_noise_rel = signal_to_noise - 260;
R
Ralph Metzler 已提交
2660 2661
			break;
		case QAM_128:
2662
			signal_to_noise_rel = signal_to_noise - 290;
R
Ralph Metzler 已提交
2663 2664 2665
			break;
		default:
		case QAM_256:
2666
			signal_to_noise_rel = signal_to_noise - 320;
R
Ralph Metzler 已提交
2667 2668 2669
			break;
		}

2670 2671 2672 2673 2674
		if (signal_to_noise_rel < -70)
			*p_quality = 0;
		else if (signal_to_noise_rel < 30)
			*p_quality = ((signal_to_noise_rel + 70) *
				     ber_quality) / 100;
R
Ralph Metzler 已提交
2675
		else
2676
			*p_quality = ber_quality;
2677
	} while (0);
R
Ralph Metzler 已提交
2678 2679 2680 2681

	return status;
}

2682
static int get_quality(struct drxk_state *state, s32 *p_quality)
R
Ralph Metzler 已提交
2683
{
2684 2685
	dprintk(1, "\n");

2686
	switch (state->m_operation_mode) {
2687
	case OM_DVBT:
2688
		return get_dvbt_quality(state, p_quality);
2689
	case OM_QAM_ITU_A:
2690
		return get_dvbc_quality(state, p_quality);
R
Ralph Metzler 已提交
2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711
	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)

2712
static int ConfigureI2CBridge(struct drxk_state *state, bool b_enable_bridge)
R
Ralph Metzler 已提交
2713
{
2714
	int status = -EINVAL;
R
Ralph Metzler 已提交
2715

2716 2717
	dprintk(1, "\n");

2718
	if (state->m_drxk_state == DRXK_UNINITIALIZED)
2719
		return 0;
2720
	if (state->m_drxk_state == DRXK_POWERED_DOWN)
2721
		goto error;
R
Ralph Metzler 已提交
2722

2723 2724
	if (state->no_i2c_bridge)
		return 0;
R
Ralph Metzler 已提交
2725

2726 2727 2728
	status = write16(state, SIO_HI_RA_RAM_PAR_1__A, SIO_HI_RA_RAM_PAR_1_PAR1_SEC_KEY);
	if (status < 0)
		goto error;
2729
	if (b_enable_bridge) {
2730
		status = write16(state, SIO_HI_RA_RAM_PAR_2__A, SIO_HI_RA_RAM_PAR_2_BRD_CFG_CLOSED);
2731
		if (status < 0)
2732 2733 2734 2735 2736 2737 2738
			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;
	}

2739
	status = hi_command(state, SIO_HI_RA_RAM_CMD_BRDCTRL, 0);
2740 2741 2742

error:
	if (status < 0)
2743
		pr_err("Error %d on %s\n", status, __func__);
R
Ralph Metzler 已提交
2744 2745 2746
	return status;
}

2747 2748
static int set_pre_saw(struct drxk_state *state,
		     struct s_cfg_pre_saw *p_pre_saw_cfg)
R
Ralph Metzler 已提交
2749
{
2750
	int status = -EINVAL;
R
Ralph Metzler 已提交
2751

2752 2753
	dprintk(1, "\n");

2754 2755
	if ((p_pre_saw_cfg == NULL)
	    || (p_pre_saw_cfg->reference > IQM_AF_PDREF__M))
2756
		goto error;
R
Ralph Metzler 已提交
2757

2758
	status = write16(state, IQM_AF_PDREF__A, p_pre_saw_cfg->reference);
2759 2760
error:
	if (status < 0)
2761
		pr_err("Error %d on %s\n", status, __func__);
R
Ralph Metzler 已提交
2762 2763 2764
	return status;
}

2765 2766
static int bl_direct_cmd(struct drxk_state *state, u32 target_addr,
		       u16 rom_offset, u16 nr_of_elements, u32 time_out)
R
Ralph Metzler 已提交
2767
{
2768 2769 2770
	u16 bl_status = 0;
	u16 offset = (u16) ((target_addr >> 0) & 0x00FFFF);
	u16 blockbank = (u16) ((target_addr >> 16) & 0x000FFF);
2771
	int status;
R
Ralph Metzler 已提交
2772 2773
	unsigned long end;

2774 2775
	dprintk(1, "\n");

R
Ralph Metzler 已提交
2776
	mutex_lock(&state->mutex);
2777 2778 2779 2780 2781 2782 2783 2784 2785
	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;
2786
	status = write16(state, SIO_BL_SRC_ADDR__A, rom_offset);
2787 2788
	if (status < 0)
		goto error;
2789
	status = write16(state, SIO_BL_SRC_LEN__A, nr_of_elements);
2790 2791 2792 2793 2794 2795
	if (status < 0)
		goto error;
	status = write16(state, SIO_BL_ENABLE__A, SIO_BL_ENABLE_ON);
	if (status < 0)
		goto error;

2796
	end = jiffies + msecs_to_jiffies(time_out);
R
Ralph Metzler 已提交
2797
	do {
2798
		status = read16(state, SIO_BL_STATUS__A, &bl_status);
2799
		if (status < 0)
2800
			goto error;
2801 2802
	} while ((bl_status == 0x1) && time_is_after_jiffies(end));
	if (bl_status == 0x1) {
2803
		pr_err("SIO not ready\n");
2804 2805 2806 2807 2808
		status = -EINVAL;
		goto error2;
	}
error:
	if (status < 0)
2809
		pr_err("Error %d on %s\n", status, __func__);
2810
error2:
R
Ralph Metzler 已提交
2811 2812 2813 2814 2815
	mutex_unlock(&state->mutex);
	return status;

}

2816
static int adc_sync_measurement(struct drxk_state *state, u16 *count)
R
Ralph Metzler 已提交
2817 2818 2819 2820
{
	u16 data = 0;
	int status;

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

2823
	/* start measurement */
2824 2825 2826 2827 2828 2829
	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 已提交
2830

2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849
	*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)
2850
		pr_err("Error %d on %s\n", status, __func__);
R
Ralph Metzler 已提交
2851 2852 2853
	return status;
}

2854
static int adc_synchronization(struct drxk_state *state)
R
Ralph Metzler 已提交
2855 2856 2857 2858
{
	u16 count = 0;
	int status;

2859 2860
	dprintk(1, "\n");

2861
	status = adc_sync_measurement(state, &count);
2862 2863
	if (status < 0)
		goto error;
R
Ralph Metzler 已提交
2864

2865 2866
	if (count == 1) {
		/* Try sampling on a diffrent edge */
2867
		u16 clk_neg = 0;
R
Ralph Metzler 已提交
2868

2869
		status = read16(state, IQM_AF_CLKNEG__A, &clk_neg);
2870 2871
		if (status < 0)
			goto error;
2872
		if ((clk_neg & IQM_AF_CLKNEG_CLKNEGDATA__M) ==
2873
			IQM_AF_CLKNEG_CLKNEGDATA_CLK_ADC_DATA_POS) {
2874 2875
			clk_neg &= (~(IQM_AF_CLKNEG_CLKNEGDATA__M));
			clk_neg |=
2876 2877
				IQM_AF_CLKNEG_CLKNEGDATA_CLK_ADC_DATA_NEG;
		} else {
2878 2879
			clk_neg &= (~(IQM_AF_CLKNEG_CLKNEGDATA__M));
			clk_neg |=
2880
				IQM_AF_CLKNEG_CLKNEGDATA_CLK_ADC_DATA_POS;
R
Ralph Metzler 已提交
2881
		}
2882
		status = write16(state, IQM_AF_CLKNEG__A, clk_neg);
2883 2884
		if (status < 0)
			goto error;
2885
		status = adc_sync_measurement(state, &count);
2886 2887 2888
		if (status < 0)
			goto error;
	}
R
Ralph Metzler 已提交
2889

2890 2891 2892 2893
	if (count < 2)
		status = -EINVAL;
error:
	if (status < 0)
2894
		pr_err("Error %d on %s\n", status, __func__);
R
Ralph Metzler 已提交
2895 2896 2897
	return status;
}

2898 2899 2900
static int set_frequency_shifter(struct drxk_state *state,
			       u16 intermediate_freqk_hz,
			       s32 tuner_freq_offset, bool is_dtv)
R
Ralph Metzler 已提交
2901
{
2902 2903 2904 2905 2906 2907
	bool select_pos_image = false;
	u32 rf_freq_residual = tuner_freq_offset;
	u32 fm_frequency_shift = 0;
	bool tuner_mirror = !state->m_b_mirror_freq_spect;
	u32 adc_freq;
	bool adc_flip;
R
Ralph Metzler 已提交
2908
	int status;
2909 2910 2911 2912
	u32 if_freq_actual;
	u32 sampling_frequency = (u32) (state->m_sys_clock_freq / 3);
	u32 frequency_shift;
	bool image_to_select;
R
Ralph Metzler 已提交
2913

2914 2915
	dprintk(1, "\n");

R
Ralph Metzler 已提交
2916
	/*
2917 2918 2919
	   Program frequency shifter
	   No need to account for mirroring on RF
	 */
2920 2921 2922 2923 2924
	if (is_dtv) {
		if ((state->m_operation_mode == OM_QAM_ITU_A) ||
		    (state->m_operation_mode == OM_QAM_ITU_C) ||
		    (state->m_operation_mode == OM_DVBT))
			select_pos_image = true;
2925
		else
2926
			select_pos_image = false;
R
Ralph Metzler 已提交
2927
	}
2928
	if (tuner_mirror)
R
Ralph Metzler 已提交
2929
		/* tuner doesn't mirror */
2930 2931
		if_freq_actual = intermediate_freqk_hz +
		    rf_freq_residual + fm_frequency_shift;
R
Ralph Metzler 已提交
2932 2933
	else
		/* tuner mirrors */
2934 2935 2936
		if_freq_actual = intermediate_freqk_hz -
		    rf_freq_residual - fm_frequency_shift;
	if (if_freq_actual > sampling_frequency / 2) {
R
Ralph Metzler 已提交
2937
		/* adc mirrors */
2938 2939
		adc_freq = sampling_frequency - if_freq_actual;
		adc_flip = true;
R
Ralph Metzler 已提交
2940 2941
	} else {
		/* adc doesn't mirror */
2942 2943
		adc_freq = if_freq_actual;
		adc_flip = false;
R
Ralph Metzler 已提交
2944 2945
	}

2946 2947 2948 2949 2950
	frequency_shift = adc_freq;
	image_to_select = state->m_rfmirror ^ tuner_mirror ^
	    adc_flip ^ select_pos_image;
	state->m_iqm_fs_rate_ofs =
	    Frac28a((frequency_shift), sampling_frequency);
R
Ralph Metzler 已提交
2951

2952 2953
	if (image_to_select)
		state->m_iqm_fs_rate_ofs = ~state->m_iqm_fs_rate_ofs + 1;
R
Ralph Metzler 已提交
2954 2955

	/* Program frequency shifter with tuner offset compensation */
2956
	/* frequency_shift += tuner_freq_offset; TODO */
2957
	status = write32(state, IQM_FS_RATE_OFS_LO__A,
2958
			 state->m_iqm_fs_rate_ofs);
2959
	if (status < 0)
2960
		pr_err("Error %d on %s\n", status, __func__);
R
Ralph Metzler 已提交
2961 2962 2963
	return status;
}

2964
static int init_agc(struct drxk_state *state, bool is_dtv)
R
Ralph Metzler 已提交
2965
{
2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979
	u16 ingain_tgt = 0;
	u16 ingain_tgt_min = 0;
	u16 ingain_tgt_max = 0;
	u16 clp_cyclen = 0;
	u16 clp_sum_min = 0;
	u16 clp_dir_to = 0;
	u16 sns_sum_min = 0;
	u16 sns_sum_max = 0;
	u16 clp_sum_max = 0;
	u16 sns_dir_to = 0;
	u16 ki_innergain_min = 0;
	u16 if_iaccu_hi_tgt = 0;
	u16 if_iaccu_hi_tgt_min = 0;
	u16 if_iaccu_hi_tgt_max = 0;
2980
	u16 data = 0;
2981 2982
	u16 fast_clp_ctrl_delay = 0;
	u16 clp_ctrl_mode = 0;
R
Ralph Metzler 已提交
2983 2984
	int status = 0;

2985 2986
	dprintk(1, "\n");

2987
	/* Common settings */
2988 2989 2990 2991
	sns_sum_max = 1023;
	if_iaccu_hi_tgt_min = 2047;
	clp_cyclen = 500;
	clp_sum_max = 1023;
2992

2993
	/* AGCInit() not available for DVBT; init done in microcode */
2994
	if (!is_qam(state)) {
2995 2996
		pr_err("%s: mode %d is not DVB-C\n",
		       __func__, state->m_operation_mode);
2997
		return -EINVAL;
2998
	}
2999 3000 3001 3002

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

	/* Standard specific settings */
3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014
	clp_sum_min = 8;
	clp_dir_to = (u16) -9;
	clp_ctrl_mode = 0;
	sns_sum_min = 8;
	sns_dir_to = (u16) -9;
	ki_innergain_min = (u16) -1030;
	if_iaccu_hi_tgt_max = 0x2380;
	if_iaccu_hi_tgt = 0x2380;
	ingain_tgt_min = 0x0511;
	ingain_tgt = 0x0511;
	ingain_tgt_max = 5119;
	fast_clp_ctrl_delay = state->m_qam_if_agc_cfg.fast_clip_ctrl_delay;
3015

3016
	status = write16(state, SCU_RAM_AGC_FAST_CLP_CTRL_DELAY__A, fast_clp_ctrl_delay);
3017 3018
	if (status < 0)
		goto error;
3019

3020
	status = write16(state, SCU_RAM_AGC_CLP_CTRL_MODE__A, clp_ctrl_mode);
3021 3022
	if (status < 0)
		goto error;
3023
	status = write16(state, SCU_RAM_AGC_INGAIN_TGT__A, ingain_tgt);
3024 3025
	if (status < 0)
		goto error;
3026
	status = write16(state, SCU_RAM_AGC_INGAIN_TGT_MIN__A, ingain_tgt_min);
3027 3028
	if (status < 0)
		goto error;
3029
	status = write16(state, SCU_RAM_AGC_INGAIN_TGT_MAX__A, ingain_tgt_max);
3030 3031
	if (status < 0)
		goto error;
3032
	status = write16(state, SCU_RAM_AGC_IF_IACCU_HI_TGT_MIN__A, if_iaccu_hi_tgt_min);
3033 3034
	if (status < 0)
		goto error;
3035
	status = write16(state, SCU_RAM_AGC_IF_IACCU_HI_TGT_MAX__A, if_iaccu_hi_tgt_max);
3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049
	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;
3050
	status = write16(state, SCU_RAM_AGC_CLP_SUM_MAX__A, clp_sum_max);
3051 3052
	if (status < 0)
		goto error;
3053
	status = write16(state, SCU_RAM_AGC_SNS_SUM_MAX__A, sns_sum_max);
3054 3055
	if (status < 0)
		goto error;
3056

3057
	status = write16(state, SCU_RAM_AGC_KI_INNERGAIN_MIN__A, ki_innergain_min);
3058 3059
	if (status < 0)
		goto error;
3060
	status = write16(state, SCU_RAM_AGC_IF_IACCU_HI_TGT__A, if_iaccu_hi_tgt);
3061 3062
	if (status < 0)
		goto error;
3063
	status = write16(state, SCU_RAM_AGC_CLP_CYCLEN__A, clp_cyclen);
3064 3065
	if (status < 0)
		goto error;
3066

3067 3068 3069 3070 3071 3072 3073 3074 3075
	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;
3076

3077 3078 3079
	status = write16(state, SCU_RAM_AGC_KI_MAXMINGAIN_TH__A, 20);
	if (status < 0)
		goto error;
3080
	status = write16(state, SCU_RAM_AGC_CLP_SUM_MIN__A, clp_sum_min);
3081 3082
	if (status < 0)
		goto error;
3083
	status = write16(state, SCU_RAM_AGC_SNS_SUM_MIN__A, sns_sum_min);
3084 3085
	if (status < 0)
		goto error;
3086
	status = write16(state, SCU_RAM_AGC_CLP_DIR_TO__A, clp_dir_to);
3087 3088
	if (status < 0)
		goto error;
3089
	status = write16(state, SCU_RAM_AGC_SNS_DIR_TO__A, sns_dir_to);
3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133
	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 已提交
3134

3135 3136 3137 3138
	/* Initialize inner-loop KI gain factors */
	status = read16(state, SCU_RAM_AGC_KI__A, &data);
	if (status < 0)
		goto error;
3139 3140 3141 3142 3143 3144 3145

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

3146 3147 3148
	status = write16(state, SCU_RAM_AGC_KI__A, data);
error:
	if (status < 0)
3149
		pr_err("Error %d on %s\n", status, __func__);
R
Ralph Metzler 已提交
3150 3151 3152
	return status;
}

3153
static int dvbtqam_get_acc_pkt_err(struct drxk_state *state, u16 *packet_err)
R
Ralph Metzler 已提交
3154 3155 3156
{
	int status;

3157
	dprintk(1, "\n");
3158
	if (packet_err == NULL)
3159 3160
		status = write16(state, SCU_RAM_FEC_ACCUM_PKT_FAILURES__A, 0);
	else
3161
		status = read16(state, SCU_RAM_FEC_ACCUM_PKT_FAILURES__A, packet_err);
3162
	if (status < 0)
3163
		pr_err("Error %d on %s\n", status, __func__);
R
Ralph Metzler 已提交
3164 3165 3166
	return status;
}

3167
static int dvbt_sc_command(struct drxk_state *state,
R
Ralph Metzler 已提交
3168 3169 3170 3171
			 u16 cmd, u16 subcmd,
			 u16 param0, u16 param1, u16 param2,
			 u16 param3, u16 param4)
{
3172 3173 3174 3175
	u16 cur_cmd = 0;
	u16 err_code = 0;
	u16 retry_cnt = 0;
	u16 sc_exec = 0;
3176
	int status;
R
Ralph Metzler 已提交
3177

3178
	dprintk(1, "\n");
3179 3180
	status = read16(state, OFDM_SC_COMM_EXEC__A, &sc_exec);
	if (sc_exec != 1) {
R
Ralph Metzler 已提交
3181
		/* SC is not running */
3182
		status = -EINVAL;
R
Ralph Metzler 已提交
3183
	}
3184 3185
	if (status < 0)
		goto error;
R
Ralph Metzler 已提交
3186 3187

	/* Wait until sc is ready to receive command */
3188
	retry_cnt = 0;
R
Ralph Metzler 已提交
3189 3190
	do {
		msleep(1);
3191 3192 3193 3194
		status = read16(state, OFDM_SC_RA_RAM_CMD__A, &cur_cmd);
		retry_cnt++;
	} while ((cur_cmd != 0) && (retry_cnt < DRXK_MAX_RETRIES));
	if (retry_cnt >= DRXK_MAX_RETRIES && (status < 0))
3195 3196
		goto error;

R
Ralph Metzler 已提交
3197 3198 3199 3200 3201 3202
	/* 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:
3203 3204 3205
		status = write16(state, OFDM_SC_RA_RAM_CMD_ADDR__A, subcmd);
		if (status < 0)
			goto error;
R
Ralph Metzler 已提交
3206 3207 3208 3209
		break;
	default:
		/* Do nothing */
		break;
3210
	}
R
Ralph Metzler 已提交
3211 3212 3213 3214 3215 3216 3217 3218 3219 3220

	/* 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:
3221
		status = write16(state, OFDM_SC_RA_RAM_PARAM1__A, param1);
R
Ralph Metzler 已提交
3222 3223 3224
		/* All commands using 1 parameters */
	case OFDM_SC_RA_RAM_CMD_SET_ECHO_TIMING:
	case OFDM_SC_RA_RAM_CMD_USER_IO:
3225
		status = write16(state, OFDM_SC_RA_RAM_PARAM0__A, param0);
R
Ralph Metzler 已提交
3226 3227 3228 3229
		/* All commands using 0 parameters */
	case OFDM_SC_RA_RAM_CMD_GET_OP_PARAM:
	case OFDM_SC_RA_RAM_CMD_NULL:
		/* Write command */
3230
		status = write16(state, OFDM_SC_RA_RAM_CMD__A, cmd);
R
Ralph Metzler 已提交
3231 3232 3233
		break;
	default:
		/* Unknown command */
3234 3235 3236 3237
		status = -EINVAL;
	}
	if (status < 0)
		goto error;
R
Ralph Metzler 已提交
3238 3239

	/* Wait until sc is ready processing command */
3240
	retry_cnt = 0;
3241
	do {
R
Ralph Metzler 已提交
3242
		msleep(1);
3243 3244 3245 3246
		status = read16(state, OFDM_SC_RA_RAM_CMD__A, &cur_cmd);
		retry_cnt++;
	} while ((cur_cmd != 0) && (retry_cnt < DRXK_MAX_RETRIES));
	if (retry_cnt >= DRXK_MAX_RETRIES && (status < 0))
3247
		goto error;
R
Ralph Metzler 已提交
3248 3249

	/* Check for illegal cmd */
3250 3251
	status = read16(state, OFDM_SC_RA_RAM_CMD_ADDR__A, &err_code);
	if (err_code == 0xFFFF) {
R
Ralph Metzler 已提交
3252
		/* illegal command */
3253
		status = -EINVAL;
R
Ralph Metzler 已提交
3254
	}
3255 3256
	if (status < 0)
		goto error;
R
Ralph Metzler 已提交
3257 3258 3259 3260 3261 3262 3263 3264 3265 3266

	/* 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:
3267
		status = read16(state, OFDM_SC_RA_RAM_PARAM0__A, &(param0));
R
Ralph Metzler 已提交
3268 3269 3270 3271 3272 3273 3274 3275 3276 3277
		/* 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 */
3278
		status = -EINVAL;
R
Ralph Metzler 已提交
3279
		break;
3280
	}			/* switch (cmd->cmd) */
3281 3282
error:
	if (status < 0)
3283
		pr_err("Error %d on %s\n", status, __func__);
R
Ralph Metzler 已提交
3284 3285 3286
	return status;
}

3287
static int power_up_dvbt(struct drxk_state *state)
R
Ralph Metzler 已提交
3288
{
3289
	enum drx_power_mode power_mode = DRX_POWER_UP;
R
Ralph Metzler 已提交
3290 3291
	int status;

3292
	dprintk(1, "\n");
3293
	status = ctrl_power_mode(state, &power_mode);
3294
	if (status < 0)
3295
		pr_err("Error %d on %s\n", status, __func__);
R
Ralph Metzler 已提交
3296 3297 3298
	return status;
}

3299
static int dvbt_ctrl_set_inc_enable(struct drxk_state *state, bool *enabled)
R
Ralph Metzler 已提交
3300
{
3301 3302
	int status;

3303
	dprintk(1, "\n");
3304
	if (*enabled == true)
3305
		status = write16(state, IQM_CF_BYPASSDET__A, 0);
3306
	else
3307
		status = write16(state, IQM_CF_BYPASSDET__A, 1);
3308
	if (status < 0)
3309
		pr_err("Error %d on %s\n", status, __func__);
3310
	return status;
R
Ralph Metzler 已提交
3311
}
3312 3313

#define DEFAULT_FR_THRES_8K     4000
3314
static int dvbt_ctrl_set_fr_enable(struct drxk_state *state, bool *enabled)
R
Ralph Metzler 已提交
3315 3316
{

3317 3318
	int status;

3319
	dprintk(1, "\n");
3320 3321
	if (*enabled == true) {
		/* write mask to 1 */
3322
		status = write16(state, OFDM_SC_RA_RAM_FR_THRES_8K__A,
3323 3324 3325
				   DEFAULT_FR_THRES_8K);
	} else {
		/* write mask to 0 */
3326
		status = write16(state, OFDM_SC_RA_RAM_FR_THRES_8K__A, 0);
3327
	}
3328
	if (status < 0)
3329
		pr_err("Error %d on %s\n", status, __func__);
3330 3331

	return status;
R
Ralph Metzler 已提交
3332 3333
}

3334 3335
static int dvbt_ctrl_set_echo_threshold(struct drxk_state *state,
				    struct drxk_cfg_dvbt_echo_thres_t *echo_thres)
R
Ralph Metzler 已提交
3336
{
3337
	u16 data = 0;
R
Ralph Metzler 已提交
3338 3339
	int status;

3340
	dprintk(1, "\n");
3341 3342 3343 3344
	status = read16(state, OFDM_SC_RA_RAM_ECHO_THRES__A, &data);
	if (status < 0)
		goto error;

3345
	switch (echo_thres->fft_mode) {
3346 3347
	case DRX_FFTMODE_2K:
		data &= ~OFDM_SC_RA_RAM_ECHO_THRES_2K__M;
3348
		data |= ((echo_thres->threshold <<
3349 3350
			OFDM_SC_RA_RAM_ECHO_THRES_2K__B)
			& (OFDM_SC_RA_RAM_ECHO_THRES_2K__M));
3351
		break;
3352 3353
	case DRX_FFTMODE_8K:
		data &= ~OFDM_SC_RA_RAM_ECHO_THRES_8K__M;
3354
		data |= ((echo_thres->threshold <<
3355 3356
			OFDM_SC_RA_RAM_ECHO_THRES_8K__B)
			& (OFDM_SC_RA_RAM_ECHO_THRES_8K__M));
3357
		break;
3358 3359 3360
	default:
		return -EINVAL;
	}
3361

3362 3363 3364
	status = write16(state, OFDM_SC_RA_RAM_ECHO_THRES__A, data);
error:
	if (status < 0)
3365
		pr_err("Error %d on %s\n", status, __func__);
3366
	return status;
R
Ralph Metzler 已提交
3367 3368
}

3369 3370
static int dvbt_ctrl_set_sqi_speed(struct drxk_state *state,
			       enum drxk_cfg_dvbt_sqi_speed *speed)
R
Ralph Metzler 已提交
3371
{
3372
	int status = -EINVAL;
R
Ralph Metzler 已提交
3373

3374 3375
	dprintk(1, "\n");

R
Ralph Metzler 已提交
3376 3377 3378 3379 3380 3381
	switch (*speed) {
	case DRXK_DVBT_SQI_SPEED_FAST:
	case DRXK_DVBT_SQI_SPEED_MEDIUM:
	case DRXK_DVBT_SQI_SPEED_SLOW:
		break;
	default:
3382
		goto error;
R
Ralph Metzler 已提交
3383
	}
3384
	status = write16(state, SCU_RAM_FEC_PRE_RS_BER_FILTER_SH__A,
3385
			   (u16) *speed);
3386 3387
error:
	if (status < 0)
3388
		pr_err("Error %d on %s\n", status, __func__);
R
Ralph Metzler 已提交
3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401
	return status;
}

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

/**
* \brief Activate DVBT specific presets
* \param demod instance of demodulator.
* \return DRXStatus_t.
*
* Called in DVBTSetStandard
*
*/
3402
static int dvbt_activate_presets(struct drxk_state *state)
R
Ralph Metzler 已提交
3403
{
3404
	int status;
3405 3406
	bool setincenable = false;
	bool setfrenable = true;
3407

3408 3409
	struct drxk_cfg_dvbt_echo_thres_t echo_thres2k = { 0, DRX_FFTMODE_2K };
	struct drxk_cfg_dvbt_echo_thres_t echo_thres8k = { 0, DRX_FFTMODE_8K };
3410

3411
	dprintk(1, "\n");
3412
	status = dvbt_ctrl_set_inc_enable(state, &setincenable);
3413 3414
	if (status < 0)
		goto error;
3415
	status = dvbt_ctrl_set_fr_enable(state, &setfrenable);
3416 3417
	if (status < 0)
		goto error;
3418
	status = dvbt_ctrl_set_echo_threshold(state, &echo_thres2k);
3419 3420
	if (status < 0)
		goto error;
3421
	status = dvbt_ctrl_set_echo_threshold(state, &echo_thres8k);
3422 3423
	if (status < 0)
		goto error;
3424
	status = write16(state, SCU_RAM_AGC_INGAIN_TGT_MAX__A, state->m_dvbt_if_agc_cfg.ingain_tgt_max);
3425 3426
error:
	if (status < 0)
3427
		pr_err("Error %d on %s\n", status, __func__);
3428
	return status;
R
Ralph Metzler 已提交
3429
}
3430

R
Ralph Metzler 已提交
3431 3432 3433 3434 3435 3436 3437 3438 3439 3440
/*============================================================================*/

/**
* \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.
*/
3441 3442
static int set_dvbt_standard(struct drxk_state *state,
			   enum operation_mode o_mode)
R
Ralph Metzler 已提交
3443
{
3444
	u16 cmd_result = 0;
3445 3446
	u16 data = 0;
	int status;
R
Ralph Metzler 已提交
3447

3448
	dprintk(1, "\n");
R
Ralph Metzler 已提交
3449

3450
	power_up_dvbt(state);
3451
	/* added antenna switch */
3452
	switch_antenna_to_dvbt(state);
3453
	/* send OFDM reset command */
3454
	status = scu_command(state, SCU_RAM_COMMAND_STANDARD_OFDM | SCU_RAM_COMMAND_CMD_DEMOD_RESET, 0, NULL, 1, &cmd_result);
3455 3456
	if (status < 0)
		goto error;
R
Ralph Metzler 已提交
3457

3458
	/* send OFDM setenv command */
3459
	status = scu_command(state, SCU_RAM_COMMAND_STANDARD_OFDM | SCU_RAM_COMMAND_CMD_DEMOD_SET_ENV, 0, NULL, 1, &cmd_result);
3460 3461
	if (status < 0)
		goto error;
R
Ralph Metzler 已提交
3462

3463 3464 3465 3466 3467 3468 3469 3470 3471 3472
	/* 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 已提交
3473

3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490
	/* 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;
3491
	status = set_iqm_af(state, true);
3492 3493
	if (status < 0)
		goto error;
R
Ralph Metzler 已提交
3494

3495 3496 3497
	status = write16(state, IQM_AF_AGC_RF__A, 0);
	if (status < 0)
		goto error;
R
Ralph Metzler 已提交
3498

3499 3500 3501 3502 3503 3504 3505 3506 3507 3508
	/* 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 已提交
3509

3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524
	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 已提交
3525

3526 3527 3528 3529 3530 3531 3532
	/* 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 已提交
3533

3534
	status = bl_chain_cmd(state, DRXK_BL_ROM_OFFSET_TAPS_DVBT, DRXK_BLCC_NR_ELEMENTS_TAPS, DRXK_BLC_TIMEOUT);
3535 3536
	if (status < 0)
		goto error;
R
Ralph Metzler 已提交
3537

3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550
	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 已提交
3551

3552
	/* IQM will not be reset from here, sync ADC and update/init AGC */
3553
	status = adc_synchronization(state);
3554 3555
	if (status < 0)
		goto error;
3556
	status = set_pre_saw(state, &state->m_dvbt_pre_saw_cfg);
3557 3558
	if (status < 0)
		goto error;
R
Ralph Metzler 已提交
3559

3560 3561 3562 3563
	/* 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 已提交
3564

3565
	status = set_agc_rf(state, &state->m_dvbt_rf_agc_cfg, true);
3566 3567
	if (status < 0)
		goto error;
3568
	status = set_agc_if(state, &state->m_dvbt_if_agc_cfg, true);
3569 3570
	if (status < 0)
		goto error;
R
Ralph Metzler 已提交
3571

3572 3573 3574 3575 3576 3577 3578 3579
	/* 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 已提交
3580

3581 3582 3583 3584
	/* 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 已提交
3585

3586 3587 3588
	if (!state->m_drxk_a3_rom_code) {
		/* AGCInit() is not done for DVBT, so set agcfast_clip_ctrl_delay  */
		status = write16(state, SCU_RAM_AGC_FAST_CLP_CTRL_DELAY__A, state->m_dvbt_if_agc_cfg.fast_clip_ctrl_delay);
3589 3590 3591
		if (status < 0)
			goto error;
	}
R
Ralph Metzler 已提交
3592

3593
	/* OFDM_SC setup */
R
Ralph Metzler 已提交
3594
#ifdef COMPILE_FOR_NONRT
3595 3596 3597 3598 3599 3600
	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 已提交
3601 3602
#endif

3603 3604 3605 3606
	/* FEC setup */
	status = write16(state, FEC_DI_INPUT_CTL__A, 1);	/* OFDM input */
	if (status < 0)
		goto error;
R
Ralph Metzler 已提交
3607 3608 3609


#ifdef COMPILE_FOR_NONRT
3610 3611 3612
	status = write16(state, FEC_RS_MEASUREMENT_PERIOD__A, 0x400);
	if (status < 0)
		goto error;
R
Ralph Metzler 已提交
3613
#else
3614 3615 3616
	status = write16(state, FEC_RS_MEASUREMENT_PERIOD__A, 0x1000);
	if (status < 0)
		goto error;
R
Ralph Metzler 已提交
3617
#endif
3618 3619 3620
	status = write16(state, FEC_RS_MEASUREMENT_PRESCALE__A, 0x0001);
	if (status < 0)
		goto error;
R
Ralph Metzler 已提交
3621

3622
	/* Setup MPEG bus */
3623
	status = mpegts_dto_setup(state, OM_DVBT);
3624
	if (status < 0)
3625 3626
		goto error;
	/* Set DVBT Presets */
3627
	status = dvbt_activate_presets(state);
3628 3629
	if (status < 0)
		goto error;
R
Ralph Metzler 已提交
3630

3631 3632
error:
	if (status < 0)
3633
		pr_err("Error %d on %s\n", status, __func__);
R
Ralph Metzler 已提交
3634 3635 3636 3637 3638
	return status;
}

/*============================================================================*/
/**
3639
* \brief start dvbt demodulating for channel.
R
Ralph Metzler 已提交
3640 3641 3642
* \param demod instance of demodulator.
* \return DRXStatus_t.
*/
3643
static int dvbt_start(struct drxk_state *state)
R
Ralph Metzler 已提交
3644
{
3645 3646
	u16 param1;
	int status;
3647
	/* drxk_ofdm_sc_cmd_t scCmd; */
3648

3649
	dprintk(1, "\n");
3650
	/* start correct processes to get in lock */
3651
	/* DRXK: OFDM_SC_RA_RAM_PROC_LOCKTRACK is no longer in mapfile! */
3652
	param1 = OFDM_SC_RA_RAM_LOCKTRACK_MIN;
3653
	status = dvbt_sc_command(state, OFDM_SC_RA_RAM_CMD_PROC_START, 0, OFDM_SC_RA_RAM_SW_EVENT_RUN_NMASK__M, param1, 0, 0, 0);
3654 3655
	if (status < 0)
		goto error;
3656 3657
	/* start FEC OC */
	status = mpegts_start(state);
3658 3659 3660 3661 3662 3663 3664
	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)
3665
		pr_err("Error %d on %s\n", status, __func__);
3666
	return status;
R
Ralph Metzler 已提交
3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677
}


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

/**
* \brief Set up dvbt demodulator for channel.
* \param demod instance of demodulator.
* \return DRXStatus_t.
* // original DVBTSetChannel()
*/
3678 3679
static int set_dvbt(struct drxk_state *state, u16 intermediate_freqk_hz,
		   s32 tuner_freq_offset)
R
Ralph Metzler 已提交
3680
{
3681 3682 3683 3684
	u16 cmd_result = 0;
	u16 transmission_params = 0;
	u16 operation_mode = 0;
	u32 iqm_rc_rate_ofs = 0;
3685 3686
	u32 bandwidth = 0;
	u16 param1;
R
Ralph Metzler 已提交
3687 3688
	int status;

3689
	dprintk(1, "IF =%d, TFO = %d\n", intermediate_freqk_hz, tuner_freq_offset);
R
Ralph Metzler 已提交
3690

3691
	status = scu_command(state, SCU_RAM_COMMAND_STANDARD_OFDM | SCU_RAM_COMMAND_CMD_DEMOD_STOP, 0, NULL, 1, &cmd_result);
3692 3693
	if (status < 0)
		goto error;
R
Ralph Metzler 已提交
3694

3695 3696 3697 3698
	/* 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 已提交
3699

3700 3701 3702 3703 3704 3705 3706
	/* 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 已提交
3707

3708 3709 3710 3711 3712
	/* 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 已提交
3713

3714
	/*== Write channel settings to device =====================================*/
R
Ralph Metzler 已提交
3715

3716
	/* mode */
3717
	switch (state->props.transmission_mode) {
3718 3719
	case TRANSMISSION_MODE_AUTO:
	default:
3720
		operation_mode |= OFDM_SC_RA_RAM_OP_AUTO_MODE__M;
3721 3722
		/* fall through , try first guess DRX_FFTMODE_8K */
	case TRANSMISSION_MODE_8K:
3723
		transmission_params |= OFDM_SC_RA_RAM_OP_PARAM_MODE_8K;
3724
		break;
3725
	case TRANSMISSION_MODE_2K:
3726
		transmission_params |= OFDM_SC_RA_RAM_OP_PARAM_MODE_2K;
3727
		break;
3728
	}
R
Ralph Metzler 已提交
3729

3730
	/* guard */
3731
	switch (state->props.guard_interval) {
3732 3733
	default:
	case GUARD_INTERVAL_AUTO:
3734
		operation_mode |= OFDM_SC_RA_RAM_OP_AUTO_GUARD__M;
3735 3736
		/* fall through , try first guess DRX_GUARD_1DIV4 */
	case GUARD_INTERVAL_1_4:
3737
		transmission_params |= OFDM_SC_RA_RAM_OP_PARAM_GUARD_4;
3738
		break;
3739
	case GUARD_INTERVAL_1_32:
3740
		transmission_params |= OFDM_SC_RA_RAM_OP_PARAM_GUARD_32;
3741
		break;
3742
	case GUARD_INTERVAL_1_16:
3743
		transmission_params |= OFDM_SC_RA_RAM_OP_PARAM_GUARD_16;
3744
		break;
3745
	case GUARD_INTERVAL_1_8:
3746
		transmission_params |= OFDM_SC_RA_RAM_OP_PARAM_GUARD_8;
3747
		break;
3748
	}
R
Ralph Metzler 已提交
3749

3750
	/* hierarchy */
3751
	switch (state->props.hierarchy) {
3752 3753 3754
	case HIERARCHY_AUTO:
	case HIERARCHY_NONE:
	default:
3755
		operation_mode |= OFDM_SC_RA_RAM_OP_AUTO_HIER__M;
3756
		/* fall through , try first guess SC_RA_RAM_OP_PARAM_HIER_NO */
3757
		/* transmission_params |= OFDM_SC_RA_RAM_OP_PARAM_HIER_NO; */
3758 3759
		/* break; */
	case HIERARCHY_1:
3760
		transmission_params |= OFDM_SC_RA_RAM_OP_PARAM_HIER_A1;
3761 3762
		break;
	case HIERARCHY_2:
3763
		transmission_params |= OFDM_SC_RA_RAM_OP_PARAM_HIER_A2;
3764 3765
		break;
	case HIERARCHY_4:
3766
		transmission_params |= OFDM_SC_RA_RAM_OP_PARAM_HIER_A4;
3767 3768 3769 3770
		break;
	}


3771 3772
	/* modulation */
	switch (state->props.modulation) {
3773 3774
	case QAM_AUTO:
	default:
3775
		operation_mode |= OFDM_SC_RA_RAM_OP_AUTO_CONST__M;
3776 3777
		/* fall through , try first guess DRX_CONSTELLATION_QAM64 */
	case QAM_64:
3778
		transmission_params |= OFDM_SC_RA_RAM_OP_PARAM_CONST_QAM64;
3779 3780
		break;
	case QPSK:
3781
		transmission_params |= OFDM_SC_RA_RAM_OP_PARAM_CONST_QPSK;
3782 3783
		break;
	case QAM_16:
3784
		transmission_params |= OFDM_SC_RA_RAM_OP_PARAM_CONST_QAM16;
3785 3786
		break;
	}
R
Ralph Metzler 已提交
3787
#if 0
3788 3789 3790 3791
	/* No hierachical channels support in BDA */
	/* Priority (only for hierarchical channels) */
	switch (channel->priority) {
	case DRX_PRIORITY_LOW:
3792 3793
		transmission_params |= OFDM_SC_RA_RAM_OP_PARAM_PRIO_LO;
		WR16(dev_addr, OFDM_EC_SB_PRIOR__A,
3794 3795 3796
			OFDM_EC_SB_PRIOR_LO);
		break;
	case DRX_PRIORITY_HIGH:
3797 3798
		transmission_params |= OFDM_SC_RA_RAM_OP_PARAM_PRIO_HI;
		WR16(dev_addr, OFDM_EC_SB_PRIOR__A,
3799 3800 3801 3802 3803 3804 3805 3806 3807
			OFDM_EC_SB_PRIOR_HI));
		break;
	case DRX_PRIORITY_UNKNOWN:	/* fall through */
	default:
		status = -EINVAL;
		goto error;
	}
#else
	/* Set Priorty high */
3808
	transmission_params |= OFDM_SC_RA_RAM_OP_PARAM_PRIO_HI;
3809 3810 3811
	status = write16(state, OFDM_EC_SB_PRIOR__A, OFDM_EC_SB_PRIOR_HI);
	if (status < 0)
		goto error;
R
Ralph Metzler 已提交
3812 3813
#endif

3814
	/* coderate */
3815
	switch (state->props.code_rate_HP) {
3816 3817
	case FEC_AUTO:
	default:
3818
		operation_mode |= OFDM_SC_RA_RAM_OP_AUTO_RATE__M;
3819 3820
		/* fall through , try first guess DRX_CODERATE_2DIV3 */
	case FEC_2_3:
3821
		transmission_params |= OFDM_SC_RA_RAM_OP_PARAM_RATE_2_3;
3822 3823
		break;
	case FEC_1_2:
3824
		transmission_params |= OFDM_SC_RA_RAM_OP_PARAM_RATE_1_2;
3825 3826
		break;
	case FEC_3_4:
3827
		transmission_params |= OFDM_SC_RA_RAM_OP_PARAM_RATE_3_4;
3828 3829
		break;
	case FEC_5_6:
3830
		transmission_params |= OFDM_SC_RA_RAM_OP_PARAM_RATE_5_6;
3831 3832
		break;
	case FEC_7_8:
3833
		transmission_params |= OFDM_SC_RA_RAM_OP_PARAM_RATE_7_8;
3834 3835
		break;
	}
R
Ralph Metzler 已提交
3836

3837 3838 3839 3840 3841 3842 3843
	/* 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 */
3844 3845 3846 3847 3848
	switch (state->props.bandwidth_hz) {
	case 0:
		state->props.bandwidth_hz = 8000000;
		/* fall though */
	case 8000000:
3849 3850 3851 3852 3853 3854 3855 3856 3857 3858 3859 3860 3861 3862 3863 3864 3865 3866
		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;
3867
	case 7000000:
3868 3869 3870 3871 3872 3873 3874 3875 3876 3877 3878 3879 3880 3881 3882 3883 3884 3885
		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;
3886
	case 6000000:
3887 3888 3889 3890 3891 3892 3893 3894 3895 3896 3897 3898 3899 3900 3901 3902 3903 3904 3905 3906 3907 3908
		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 已提交
3909

3910
	if (iqm_rc_rate_ofs == 0) {
3911 3912 3913 3914 3915 3916 3917 3918 3919
		/* 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  */
3920 3921
		iqm_rc_rate_ofs = Frac28a((u32)
					((state->m_sys_clock_freq *
3922 3923
						1000) / 3), bandwidth);
		/* (SysFreq / BandWidth) * (2^21), rounding before truncating  */
3924 3925 3926
		if ((iqm_rc_rate_ofs & 0x7fL) >= 0x40)
			iqm_rc_rate_ofs += 0x80L;
		iqm_rc_rate_ofs = iqm_rc_rate_ofs >> 7;
3927
		/* ((SysFreq / BandWidth) * (2^21)) - (2^23)  */
3928
		iqm_rc_rate_ofs = iqm_rc_rate_ofs - (1 << 23);
3929
	}
R
Ralph Metzler 已提交
3930

3931
	iqm_rc_rate_ofs &=
3932 3933
		((((u32) IQM_RC_RATE_OFS_HI__M) <<
		IQM_RC_RATE_OFS_LO__W) | IQM_RC_RATE_OFS_LO__M);
3934
	status = write32(state, IQM_RC_RATE_OFS_LO__A, iqm_rc_rate_ofs);
3935 3936
	if (status < 0)
		goto error;
R
Ralph Metzler 已提交
3937

3938
	/* Bandwidth setting done */
R
Ralph Metzler 已提交
3939

3940
#if 0
3941
	status = dvbt_set_frequency_shift(demod, channel, tuner_offset);
3942 3943
	if (status < 0)
		goto error;
3944
#endif
3945
	status = set_frequency_shifter(state, intermediate_freqk_hz, tuner_freq_offset, true);
3946 3947
	if (status < 0)
		goto error;
R
Ralph Metzler 已提交
3948

3949
	/*== start SC, write channel settings to SC ===============================*/
R
Ralph Metzler 已提交
3950

3951 3952 3953 3954
	/* 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 已提交
3955

3956 3957 3958 3959 3960 3961 3962
	/* 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 已提交
3963 3964


3965
	status = scu_command(state, SCU_RAM_COMMAND_STANDARD_OFDM | SCU_RAM_COMMAND_CMD_DEMOD_START, 0, NULL, 1, &cmd_result);
3966 3967 3968 3969 3970 3971 3972 3973 3974
	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);
3975 3976
	status = dvbt_sc_command(state, OFDM_SC_RA_RAM_CMD_SET_PREF_PARAM,
				0, transmission_params, param1, 0, 0, 0);
3977 3978
	if (status < 0)
		goto error;
R
Ralph Metzler 已提交
3979

3980 3981
	if (!state->m_drxk_a3_rom_code)
		status = dvbt_ctrl_set_sqi_speed(state, &state->m_sqi_speed);
3982 3983
error:
	if (status < 0)
3984
		pr_err("Error %d on %s\n", status, __func__);
R
Ralph Metzler 已提交
3985 3986 3987 3988 3989 3990 3991 3992 3993 3994 3995 3996 3997 3998

	return status;
}


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

/**
* \brief Retreive lock status .
* \param demod    Pointer to demodulator instance.
* \param lockStat Pointer to lock status structure.
* \return DRXStatus_t.
*
*/
3999
static int get_dvbt_lock_status(struct drxk_state *state, u32 *p_lock_status)
R
Ralph Metzler 已提交
4000
{
4001 4002 4003 4004 4005 4006
	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;

4007 4008
	u16 sc_ra_ram_lock = 0;
	u16 sc_comm_exec = 0;
4009

4010 4011
	dprintk(1, "\n");

4012
	*p_lock_status = NOT_LOCKED;
4013 4014
	/* driver 0.9.0 */
	/* Check if SC is running */
4015
	status = read16(state, OFDM_SC_COMM_EXEC__A, &sc_comm_exec);
4016 4017
	if (status < 0)
		goto end;
4018
	if (sc_comm_exec == OFDM_SC_COMM_EXEC_STOP)
4019
		goto end;
4020

4021
	status = read16(state, OFDM_SC_RA_RAM_LOCK__A, &sc_ra_ram_lock);
4022 4023
	if (status < 0)
		goto end;
4024

4025 4026 4027 4028 4029 4030 4031 4032
	if ((sc_ra_ram_lock & mpeg_lock_mask) == mpeg_lock_mask)
		*p_lock_status = MPEG_LOCK;
	else if ((sc_ra_ram_lock & fec_lock_mask) == fec_lock_mask)
		*p_lock_status = FEC_LOCK;
	else if ((sc_ra_ram_lock & demod_lock_mask) == demod_lock_mask)
		*p_lock_status = DEMOD_LOCK;
	else if (sc_ra_ram_lock & OFDM_SC_RA_RAM_LOCK_NODVBT__M)
		*p_lock_status = NEVER_LOCK;
4033 4034
end:
	if (status < 0)
4035
		pr_err("Error %d on %s\n", status, __func__);
4036 4037

	return status;
R
Ralph Metzler 已提交
4038 4039
}

4040
static int power_up_qam(struct drxk_state *state)
R
Ralph Metzler 已提交
4041
{
4042
	enum drx_power_mode power_mode = DRXK_POWER_DOWN_OFDM;
4043
	int status;
R
Ralph Metzler 已提交
4044

4045
	dprintk(1, "\n");
4046
	status = ctrl_power_mode(state, &power_mode);
4047
	if (status < 0)
4048
		pr_err("Error %d on %s\n", status, __func__);
R
Ralph Metzler 已提交
4049

4050
	return status;
R
Ralph Metzler 已提交
4051 4052 4053
}


4054
/** Power Down QAM */
4055
static int power_down_qam(struct drxk_state *state)
R
Ralph Metzler 已提交
4056
{
4057
	u16 data = 0;
4058
	u16 cmd_result;
4059 4060
	int status = 0;

4061
	dprintk(1, "\n");
4062 4063 4064 4065 4066 4067 4068 4069 4070 4071
	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);
4072
		if (status < 0)
4073
			goto error;
4074
		status = scu_command(state, SCU_RAM_COMMAND_STANDARD_QAM | SCU_RAM_COMMAND_CMD_DEMOD_STOP, 0, NULL, 1, &cmd_result);
4075
		if (status < 0)
4076 4077 4078
			goto error;
	}
	/* powerdown AFE                   */
4079
	status = set_iqm_af(state, false);
4080 4081 4082

error:
	if (status < 0)
4083
		pr_err("Error %d on %s\n", status, __func__);
4084 4085

	return status;
R
Ralph Metzler 已提交
4086
}
4087

R
Ralph Metzler 已提交
4088 4089 4090 4091 4092
/*============================================================================*/

/**
* \brief Setup of the QAM Measurement intervals for signal quality
* \param demod instance of demod.
4093
* \param modulation current modulation.
R
Ralph Metzler 已提交
4094 4095 4096 4097 4098 4099 4100
* \return DRXStatus_t.
*
*  NOTE:
*  Take into account that for certain settings the errorcounters can overflow.
*  The implementation does not check this.
*
*/
4101 4102 4103
static int set_qam_measurement(struct drxk_state *state,
			     enum e_drxk_constellation modulation,
			     u32 symbol_rate)
R
Ralph Metzler 已提交
4104
{
4105 4106 4107 4108
	u32 fec_bits_desired = 0;	/* BER accounting period */
	u32 fec_rs_period_total = 0;	/* Total period */
	u16 fec_rs_prescale = 0;	/* ReedSolomon Measurement Prescale */
	u16 fec_rs_period = 0;	/* Value for corresponding I2C register */
R
Ralph Metzler 已提交
4109 4110
	int status = 0;

4111
	dprintk(1, "\n");
R
Ralph Metzler 已提交
4112

4113 4114
	fec_rs_prescale = 1;
	/* fec_bits_desired = symbol_rate [kHz] *
4115
		FrameLenght [ms] *
4116
		(modulation + 1) *
4117 4118 4119
		SyncLoss (== 1) *
		ViterbiLoss (==1)
		*/
4120
	switch (modulation) {
4121
	case DRX_CONSTELLATION_QAM16:
4122
		fec_bits_desired = 4 * symbol_rate;
4123 4124
		break;
	case DRX_CONSTELLATION_QAM32:
4125
		fec_bits_desired = 5 * symbol_rate;
4126 4127
		break;
	case DRX_CONSTELLATION_QAM64:
4128
		fec_bits_desired = 6 * symbol_rate;
4129 4130
		break;
	case DRX_CONSTELLATION_QAM128:
4131
		fec_bits_desired = 7 * symbol_rate;
4132 4133
		break;
	case DRX_CONSTELLATION_QAM256:
4134
		fec_bits_desired = 8 * symbol_rate;
4135 4136 4137 4138 4139 4140
		break;
	default:
		status = -EINVAL;
	}
	if (status < 0)
		goto error;
R
Ralph Metzler 已提交
4141

4142 4143
	fec_bits_desired /= 1000;	/* symbol_rate [Hz] -> symbol_rate [kHz]  */
	fec_bits_desired *= 500;	/* meas. period [ms] */
R
Ralph Metzler 已提交
4144

4145
	/* Annex A/C: bits/RsPeriod = 204 * 8 = 1632 */
4146 4147
	/* fec_rs_period_total = fec_bits_desired / 1632 */
	fec_rs_period_total = (fec_bits_desired / 1632UL) + 1;	/* roughly ceil */
R
Ralph Metzler 已提交
4148

4149 4150 4151
	/* fec_rs_period_total =  fec_rs_prescale * fec_rs_period  */
	fec_rs_prescale = 1 + (u16) (fec_rs_period_total >> 16);
	if (fec_rs_prescale == 0) {
4152 4153
		/* Divide by zero (though impossible) */
		status = -EINVAL;
4154
		if (status < 0)
4155 4156
			goto error;
	}
4157 4158 4159
	fec_rs_period =
		((u16) fec_rs_period_total +
		(fec_rs_prescale >> 1)) / fec_rs_prescale;
R
Ralph Metzler 已提交
4160

4161
	/* write corresponding registers */
4162
	status = write16(state, FEC_RS_MEASUREMENT_PERIOD__A, fec_rs_period);
4163
	if (status < 0)
4164
		goto error;
4165
	status = write16(state, FEC_RS_MEASUREMENT_PRESCALE__A, fec_rs_prescale);
4166 4167
	if (status < 0)
		goto error;
4168
	status = write16(state, FEC_OC_SNC_FAIL_PERIOD__A, fec_rs_period);
4169 4170
error:
	if (status < 0)
4171
		pr_err("Error %d on %s\n", status, __func__);
R
Ralph Metzler 已提交
4172 4173 4174
	return status;
}

4175
static int set_qam16(struct drxk_state *state)
R
Ralph Metzler 已提交
4176
{
4177 4178
	int status = 0;

4179
	dprintk(1, "\n");
4180 4181 4182 4183 4184 4185 4186 4187 4188 4189 4190 4191 4192 4193 4194 4195 4196 4197 4198 4199 4200 4201 4202 4203 4204 4205 4206 4207 4208 4209 4210 4211 4212 4213 4214 4215 4216 4217 4218
	/* 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;
4219

4220 4221 4222 4223 4224 4225 4226 4227 4228
	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;
4229

4230 4231 4232 4233
	/* QAM Slicer Settings */
	status = write16(state, SCU_RAM_QAM_SL_SIG_POWER__A, DRXK_QAM_SL_SIG_POWER_QAM16);
	if (status < 0)
		goto error;
4234

4235 4236 4237 4238 4239 4240 4241 4242 4243 4244 4245 4246 4247 4248 4249 4250 4251 4252 4253 4254 4255 4256 4257 4258 4259
	/* 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;
4260

4261 4262 4263 4264 4265 4266 4267 4268 4269 4270 4271 4272 4273 4274 4275 4276 4277 4278 4279 4280 4281 4282 4283 4284 4285 4286 4287 4288 4289 4290 4291 4292 4293 4294 4295 4296
	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;
4297 4298


4299
	/* QAM State Machine (FSM) Thresholds */
4300

4301 4302 4303 4304 4305 4306 4307 4308 4309 4310 4311 4312 4313 4314 4315 4316 4317 4318
	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;
4319

4320 4321 4322 4323 4324 4325 4326 4327 4328
	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;
4329 4330


4331
	/* QAM FSM Tracking Parameters */
4332

4333 4334 4335 4336 4337 4338 4339 4340 4341 4342 4343 4344 4345 4346 4347 4348 4349 4350 4351 4352 4353
	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;
4354

4355 4356
error:
	if (status < 0)
4357
		pr_err("Error %d on %s\n", status, __func__);
4358
	return status;
R
Ralph Metzler 已提交
4359 4360 4361 4362 4363 4364 4365 4366 4367
}

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

/**
* \brief QAM32 specific setup
* \param demod instance of demod.
* \return DRXStatus_t.
*/
4368
static int set_qam32(struct drxk_state *state)
R
Ralph Metzler 已提交
4369
{
4370 4371
	int status = 0;

4372
	dprintk(1, "\n");
4373

4374 4375 4376 4377 4378 4379 4380 4381 4382 4383 4384 4385 4386 4387 4388 4389 4390 4391 4392 4393
	/* 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;
4394

4395 4396 4397 4398 4399 4400 4401 4402 4403 4404 4405 4406 4407 4408 4409 4410 4411 4412 4413
	/* 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;
4414

4415 4416 4417 4418 4419 4420 4421 4422 4423
	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;
4424

4425
	/* QAM Slicer Settings */
4426

4427 4428 4429
	status = write16(state, SCU_RAM_QAM_SL_SIG_POWER__A, DRXK_QAM_SL_SIG_POWER_QAM32);
	if (status < 0)
		goto error;
4430 4431


4432
	/* QAM Loop Controller Coeficients */
4433

4434 4435 4436 4437 4438 4439 4440 4441 4442 4443 4444 4445 4446 4447 4448 4449 4450 4451 4452 4453 4454 4455 4456 4457
	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;
4458

4459 4460 4461 4462 4463 4464 4465 4466 4467 4468 4469 4470 4471 4472 4473 4474 4475 4476 4477 4478 4479 4480 4481 4482 4483 4484 4485 4486 4487 4488 4489 4490 4491 4492 4493 4494
	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;
4495 4496


4497
	/* QAM State Machine (FSM) Thresholds */
4498

4499 4500 4501 4502 4503 4504 4505 4506 4507 4508 4509 4510 4511 4512 4513 4514 4515 4516
	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;
4517

4518 4519 4520 4521 4522 4523 4524 4525 4526
	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;
4527 4528


4529
	/* QAM FSM Tracking Parameters */
4530

4531 4532 4533 4534 4535 4536 4537 4538 4539 4540 4541 4542 4543 4544 4545 4546 4547 4548 4549 4550 4551
	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)
4552
		pr_err("Error %d on %s\n", status, __func__);
4553
	return status;
R
Ralph Metzler 已提交
4554 4555 4556 4557 4558 4559 4560 4561 4562
}

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

/**
* \brief QAM64 specific setup
* \param demod instance of demod.
* \return DRXStatus_t.
*/
4563
static int set_qam64(struct drxk_state *state)
R
Ralph Metzler 已提交
4564
{
4565 4566
	int status = 0;

4567
	dprintk(1, "\n");
4568 4569 4570 4571 4572 4573 4574 4575 4576 4577 4578 4579 4580 4581 4582 4583 4584 4585 4586 4587
	/* 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;
4588

4589 4590 4591 4592 4593 4594 4595 4596 4597 4598 4599 4600 4601 4602 4603 4604 4605 4606 4607
	/* 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;
4608

4609 4610 4611 4612 4613 4614 4615 4616 4617
	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;
4618

4619 4620 4621 4622
	/* QAM Slicer Settings */
	status = write16(state, SCU_RAM_QAM_SL_SIG_POWER__A, DRXK_QAM_SL_SIG_POWER_QAM64);
	if (status < 0)
		goto error;
4623 4624


4625
	/* QAM Loop Controller Coeficients */
4626

4627 4628 4629 4630 4631 4632 4633 4634 4635 4636 4637 4638 4639 4640 4641 4642 4643 4644 4645 4646 4647 4648 4649 4650
	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;
4651

4652 4653 4654 4655 4656 4657 4658 4659 4660 4661 4662 4663 4664 4665 4666 4667 4668 4669 4670 4671 4672 4673 4674 4675 4676 4677 4678 4679 4680 4681 4682 4683 4684 4685 4686 4687
	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;
4688 4689


4690
	/* QAM State Machine (FSM) Thresholds */
4691

4692 4693 4694 4695 4696 4697 4698 4699 4700 4701 4702 4703 4704 4705 4706 4707 4708 4709
	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;
4710

4711 4712 4713 4714 4715 4716 4717 4718 4719
	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;
4720 4721


4722
	/* QAM FSM Tracking Parameters */
4723

4724 4725 4726 4727 4728 4729 4730 4731 4732 4733 4734 4735 4736 4737 4738 4739 4740 4741 4742 4743 4744
	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)
4745
		pr_err("Error %d on %s\n", status, __func__);
4746 4747

	return status;
R
Ralph Metzler 已提交
4748 4749 4750 4751 4752 4753 4754 4755 4756
}

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

/**
* \brief QAM128 specific setup
* \param demod: instance of demod.
* \return DRXStatus_t.
*/
4757
static int set_qam128(struct drxk_state *state)
R
Ralph Metzler 已提交
4758
{
4759 4760
	int status = 0;

4761
	dprintk(1, "\n");
4762 4763 4764 4765 4766 4767 4768 4769 4770 4771 4772 4773 4774 4775 4776 4777 4778 4779 4780 4781
	/* 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;
4782

4783 4784 4785 4786 4787 4788 4789 4790 4791 4792 4793 4794 4795 4796 4797 4798 4799 4800 4801
	/* 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;
4802

4803 4804 4805 4806 4807 4808 4809 4810 4811
	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;
4812 4813


4814
	/* QAM Slicer Settings */
4815

4816 4817 4818
	status = write16(state, SCU_RAM_QAM_SL_SIG_POWER__A, DRXK_QAM_SL_SIG_POWER_QAM128);
	if (status < 0)
		goto error;
4819 4820


4821
	/* QAM Loop Controller Coeficients */
4822

4823 4824 4825 4826 4827 4828 4829 4830 4831 4832 4833 4834 4835 4836 4837 4838 4839 4840 4841 4842 4843 4844 4845 4846 4847 4848 4849 4850 4851 4852 4853 4854 4855 4856 4857 4858 4859 4860 4861 4862 4863 4864 4865 4866 4867 4868 4869 4870 4871 4872 4873 4874 4875 4876 4877 4878 4879 4880 4881 4882 4883
	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;
4884 4885


4886
	/* QAM State Machine (FSM) Thresholds */
4887

4888 4889 4890 4891 4892 4893 4894 4895 4896 4897 4898 4899 4900 4901 4902 4903 4904 4905
	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;
4906

4907 4908 4909 4910 4911 4912
	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;
4913

4914 4915 4916
	status = write16(state, SCU_RAM_QAM_FSM_FREQ_LIM__A, 12);
	if (status < 0)
		goto error;
4917

4918
	/* QAM FSM Tracking Parameters */
4919

4920 4921 4922 4923 4924 4925 4926 4927 4928 4929 4930 4931 4932 4933 4934 4935 4936 4937 4938 4939 4940
	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)
4941
		pr_err("Error %d on %s\n", status, __func__);
4942 4943

	return status;
R
Ralph Metzler 已提交
4944 4945 4946 4947 4948 4949 4950 4951 4952
}

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

/**
* \brief QAM256 specific setup
* \param demod: instance of demod.
* \return DRXStatus_t.
*/
4953
static int set_qam256(struct drxk_state *state)
R
Ralph Metzler 已提交
4954
{
4955 4956
	int status = 0;

4957
	dprintk(1, "\n");
4958 4959 4960 4961 4962 4963 4964 4965 4966 4967 4968 4969 4970 4971 4972 4973 4974 4975 4976 4977
	/* 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;
4978

4979 4980 4981 4982 4983 4984 4985 4986 4987 4988 4989 4990 4991 4992 4993 4994 4995 4996 4997
	/* 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;
4998

4999 5000 5001 5002 5003 5004 5005 5006 5007
	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;
5008

5009
	/* QAM Slicer Settings */
5010

5011 5012 5013
	status = write16(state, SCU_RAM_QAM_SL_SIG_POWER__A, DRXK_QAM_SL_SIG_POWER_QAM256);
	if (status < 0)
		goto error;
5014 5015


5016
	/* QAM Loop Controller Coeficients */
5017

5018 5019 5020 5021 5022 5023 5024 5025 5026 5027 5028 5029 5030 5031 5032 5033 5034 5035 5036 5037 5038 5039 5040 5041
	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;
5042

5043 5044 5045 5046 5047 5048 5049 5050 5051 5052 5053 5054 5055 5056 5057 5058 5059 5060 5061 5062 5063 5064 5065 5066 5067 5068 5069 5070 5071 5072 5073 5074 5075 5076 5077 5078
	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;
5079 5080


5081
	/* QAM State Machine (FSM) Thresholds */
5082

5083 5084 5085 5086 5087 5088 5089 5090 5091 5092 5093 5094 5095 5096 5097 5098 5099 5100
	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;
5101

5102 5103 5104 5105 5106 5107 5108 5109 5110
	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;
5111 5112


5113
	/* QAM FSM Tracking Parameters */
5114

5115 5116 5117 5118 5119 5120 5121 5122 5123 5124 5125 5126 5127 5128 5129 5130 5131 5132 5133 5134 5135
	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)
5136
		pr_err("Error %d on %s\n", status, __func__);
5137
	return status;
R
Ralph Metzler 已提交
5138 5139 5140 5141 5142 5143 5144 5145 5146 5147
}


/*============================================================================*/
/**
* \brief Reset QAM block.
* \param demod:   instance of demod.
* \param channel: pointer to channel data.
* \return DRXStatus_t.
*/
5148
static int qam_reset_qam(struct drxk_state *state)
R
Ralph Metzler 已提交
5149
{
5150
	int status;
5151
	u16 cmd_result;
R
Ralph Metzler 已提交
5152

5153
	dprintk(1, "\n");
5154 5155 5156 5157
	/* Stop QAM comstate->m_exec */
	status = write16(state, QAM_COMM_EXEC__A, QAM_COMM_EXEC_STOP);
	if (status < 0)
		goto error;
R
Ralph Metzler 已提交
5158

5159
	status = scu_command(state, SCU_RAM_COMMAND_STANDARD_QAM | SCU_RAM_COMMAND_CMD_DEMOD_RESET, 0, NULL, 1, &cmd_result);
5160 5161
error:
	if (status < 0)
5162
		pr_err("Error %d on %s\n", status, __func__);
5163
	return status;
R
Ralph Metzler 已提交
5164 5165 5166 5167 5168 5169 5170 5171 5172 5173
}

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

/**
* \brief Set QAM symbolrate.
* \param demod:   instance of demod.
* \param channel: pointer to channel data.
* \return DRXStatus_t.
*/
5174
static int qam_set_symbolrate(struct drxk_state *state)
R
Ralph Metzler 已提交
5175
{
5176 5177 5178
	u32 adc_frequency = 0;
	u32 symb_freq = 0;
	u32 iqm_rc_rate = 0;
5179
	u16 ratesel = 0;
5180
	u32 lc_symb_rate = 0;
5181 5182
	int status;

5183
	dprintk(1, "\n");
5184
	/* Select & calculate correct IQM rate */
5185
	adc_frequency = (state->m_sys_clock_freq * 1000) / 3;
5186
	ratesel = 0;
5187 5188
	/* printk(KERN_DEBUG "drxk: SR %d\n", state->props.symbol_rate); */
	if (state->props.symbol_rate <= 1188750)
5189
		ratesel = 3;
5190
	else if (state->props.symbol_rate <= 2377500)
5191
		ratesel = 2;
5192
	else if (state->props.symbol_rate <= 4755000)
5193 5194 5195 5196
		ratesel = 1;
	status = write16(state, IQM_FD_RATESEL__A, ratesel);
	if (status < 0)
		goto error;
5197

5198 5199 5200
	/*
		IqmRcRate = ((Fadc / (symbolrate * (4<<ratesel))) - 1) * (1<<23)
		*/
5201 5202
	symb_freq = state->props.symbol_rate * (1 << ratesel);
	if (symb_freq == 0) {
5203 5204 5205 5206
		/* Divide by zero */
		status = -EINVAL;
		goto error;
	}
5207 5208
	iqm_rc_rate = (adc_frequency / symb_freq) * (1 << 21) +
		(Frac28a((adc_frequency % symb_freq), symb_freq) >> 7) -
5209
		(1 << 23);
5210
	status = write32(state, IQM_RC_RATE_OFS_LO__A, iqm_rc_rate);
5211 5212
	if (status < 0)
		goto error;
5213
	state->m_iqm_rc_rate = iqm_rc_rate;
5214
	/*
5215
		LcSymbFreq = round (.125 *  symbolrate / adc_freq * (1<<15))
5216
		*/
5217 5218
	symb_freq = state->props.symbol_rate;
	if (adc_frequency == 0) {
5219 5220 5221 5222
		/* Divide by zero */
		status = -EINVAL;
		goto error;
	}
5223 5224
	lc_symb_rate = (symb_freq / adc_frequency) * (1 << 12) +
		(Frac28a((symb_freq % adc_frequency), adc_frequency) >>
5225
		16);
5226 5227 5228
	if (lc_symb_rate > 511)
		lc_symb_rate = 511;
	status = write16(state, QAM_LC_SYMBOL_FREQ__A, (u16) lc_symb_rate);
5229 5230 5231

error:
	if (status < 0)
5232
		pr_err("Error %d on %s\n", status, __func__);
5233
	return status;
R
Ralph Metzler 已提交
5234 5235 5236 5237 5238 5239 5240 5241 5242 5243 5244
}

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

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

5245
static int get_qam_lock_status(struct drxk_state *state, u32 *p_lock_status)
R
Ralph Metzler 已提交
5246 5247
{
	int status;
5248
	u16 result[2] = { 0, 0 };
R
Ralph Metzler 已提交
5249

5250
	dprintk(1, "\n");
5251
	*p_lock_status = NOT_LOCKED;
5252
	status = scu_command(state,
5253 5254
			SCU_RAM_COMMAND_STANDARD_QAM |
			SCU_RAM_COMMAND_CMD_DEMOD_GET_LOCK, 0, NULL, 2,
5255
			result);
5256
	if (status < 0)
5257
		pr_err("Error %d on %s\n", status, __func__);
5258

5259
	if (result[1] < SCU_RAM_QAM_LOCKED_LOCKED_DEMOD_LOCKED) {
R
Ralph Metzler 已提交
5260
		/* 0x0000 NOT LOCKED */
5261
	} else if (result[1] < SCU_RAM_QAM_LOCKED_LOCKED_LOCKED) {
R
Ralph Metzler 已提交
5262
		/* 0x4000 DEMOD LOCKED */
5263 5264
		*p_lock_status = DEMOD_LOCK;
	} else if (result[1] < SCU_RAM_QAM_LOCKED_LOCKED_NEVER_LOCK) {
R
Ralph Metzler 已提交
5265
		/* 0x8000 DEMOD + FEC LOCKED (system lock) */
5266
		*p_lock_status = MPEG_LOCK;
5267
	} else {
R
Ralph Metzler 已提交
5268 5269 5270 5271
		/* 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 */
5272
		*p_lock_status = NEVER_LOCK;
R
Ralph Metzler 已提交
5273 5274 5275 5276 5277 5278 5279 5280 5281 5282 5283
	}
	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

5284 5285
static int qam_demodulator_command(struct drxk_state *state,
				 int number_of_parameters)
5286 5287
{
	int status;
5288 5289
	u16 cmd_result;
	u16 set_param_parameters[4] = { 0, 0, 0, 0 };
5290

5291 5292
	set_param_parameters[0] = state->m_constellation;	/* modulation     */
	set_param_parameters[1] = DRXK_QAM_I12_J17;	/* interleave mode   */
5293

5294 5295
	if (number_of_parameters == 2) {
		u16 set_env_parameters[1] = { 0 };
5296

5297 5298
		if (state->m_operation_mode == OM_QAM_ITU_C)
			set_env_parameters[0] = QAM_TOP_ANNEX_C;
5299
		else
5300
			set_env_parameters[0] = QAM_TOP_ANNEX_A;
5301 5302 5303

		status = scu_command(state,
				     SCU_RAM_COMMAND_STANDARD_QAM | SCU_RAM_COMMAND_CMD_DEMOD_SET_ENV,
5304
				     1, set_env_parameters, 1, &cmd_result);
5305 5306 5307 5308 5309
		if (status < 0)
			goto error;

		status = scu_command(state,
				     SCU_RAM_COMMAND_STANDARD_QAM | SCU_RAM_COMMAND_CMD_DEMOD_SET_PARAM,
5310 5311 5312 5313 5314
				     number_of_parameters, set_param_parameters,
				     1, &cmd_result);
	} else if (number_of_parameters == 4) {
		if (state->m_operation_mode == OM_QAM_ITU_C)
			set_param_parameters[2] = QAM_TOP_ANNEX_C;
5315
		else
5316
			set_param_parameters[2] = QAM_TOP_ANNEX_A;
5317

5318
		set_param_parameters[3] |= (QAM_MIRROR_AUTO_ON);
5319 5320
		/* Env parameters */
		/* check for LOCKRANGE Extented */
5321
		/* set_param_parameters[3] |= QAM_LOCKRANGE_NORMAL; */
5322 5323 5324

		status = scu_command(state,
				     SCU_RAM_COMMAND_STANDARD_QAM | SCU_RAM_COMMAND_CMD_DEMOD_SET_PARAM,
5325 5326
				     number_of_parameters, set_param_parameters,
				     1, &cmd_result);
5327
	} else {
5328 5329
		pr_warn("Unknown QAM demodulator parameter count %d\n",
			number_of_parameters);
5330
		status = -EINVAL;
5331 5332 5333 5334
	}

error:
	if (status < 0)
5335
		pr_warn("Warning %d on %s\n", status, __func__);
5336 5337 5338
	return status;
}

5339 5340
static int set_qam(struct drxk_state *state, u16 intermediate_freqk_hz,
		  s32 tuner_freq_offset)
R
Ralph Metzler 已提交
5341
{
5342
	int status;
5343 5344
	u16 cmd_result;
	int qam_demod_param_count = state->qam_demod_parameter_count;
R
Ralph Metzler 已提交
5345

5346
	dprintk(1, "\n");
5347
	/*
5348 5349 5350 5351 5352
	 * STEP 1: reset demodulator
	 *	resets FEC DI and FEC RS
	 *	resets QAM block
	 *	resets SCU variables
	 */
5353 5354 5355 5356 5357 5358
	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;
5359
	status = qam_reset_qam(state);
5360 5361
	if (status < 0)
		goto error;
R
Ralph Metzler 已提交
5362

5363
	/*
5364 5365 5366 5367
	 * STEP 2: configure demodulator
	 *	-set params; resets IQM,QAM,FEC HW; initializes some
	 *       SCU variables
	 */
5368
	status = qam_set_symbolrate(state);
5369 5370 5371 5372
	if (status < 0)
		goto error;

	/* Set params */
5373
	switch (state->props.modulation) {
5374
	case QAM_256:
5375
		state->m_constellation = DRX_CONSTELLATION_QAM256;
5376 5377 5378
		break;
	case QAM_AUTO:
	case QAM_64:
5379
		state->m_constellation = DRX_CONSTELLATION_QAM64;
5380 5381
		break;
	case QAM_16:
5382
		state->m_constellation = DRX_CONSTELLATION_QAM16;
5383 5384
		break;
	case QAM_32:
5385
		state->m_constellation = DRX_CONSTELLATION_QAM32;
5386 5387
		break;
	case QAM_128:
5388
		state->m_constellation = DRX_CONSTELLATION_QAM128;
5389 5390 5391 5392 5393 5394 5395
		break;
	default:
		status = -EINVAL;
		break;
	}
	if (status < 0)
		goto error;
R
Ralph Metzler 已提交
5396

5397 5398 5399 5400
	/* 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) {
5401 5402
		qam_demod_param_count = 4;
		status = qam_demodulator_command(state, qam_demod_param_count);
5403
	}
R
Ralph Metzler 已提交
5404

5405 5406 5407 5408 5409
	/* 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)) {
5410 5411
		qam_demod_param_count = 2;
		status = qam_demodulator_command(state, qam_demod_param_count);
5412
	}
5413 5414 5415 5416 5417 5418 5419

	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);
5420
		goto error;
5421 5422 5423
	} else if (!state->qam_demod_parameter_count) {
		dprintk(1, "Auto-probing the correct QAM demodulator command "
			"parameters was successful - using %d parameters.\n",
5424
			qam_demod_param_count);
5425

5426 5427 5428 5429
		/*
		 * One of our commands was successful. We don't need to
		 * auto-probe anymore, now that we got the correct command.
		 */
5430
		state->qam_demod_parameter_count = qam_demod_param_count;
5431
	}
R
Ralph Metzler 已提交
5432

5433 5434
	/*
	 * STEP 3: enable the system in a mode where the ADC provides valid
5435
	 * signal setup modulation independent registers
5436
	 */
5437
#if 0
5438
	status = set_frequency(channel, tuner_freq_offset));
5439 5440
	if (status < 0)
		goto error;
5441
#endif
5442
	status = set_frequency_shifter(state, intermediate_freqk_hz, tuner_freq_offset, true);
5443 5444
	if (status < 0)
		goto error;
R
Ralph Metzler 已提交
5445

5446
	/* Setup BER measurement */
5447
	status = set_qam_measurement(state, state->m_constellation, state->props.symbol_rate);
5448 5449
	if (status < 0)
		goto error;
5450

5451 5452 5453 5454 5455 5456 5457
	/* 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;
5458

5459 5460 5461 5462 5463 5464 5465 5466 5467 5468 5469 5470 5471
	/* 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;
5472

5473 5474 5475 5476 5477 5478 5479 5480 5481 5482 5483 5484 5485 5486 5487 5488 5489 5490 5491 5492 5493 5494 5495 5496 5497 5498 5499 5500 5501 5502 5503 5504 5505 5506 5507 5508 5509 5510 5511 5512 5513 5514 5515 5516 5517
	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;
5518

5519 5520 5521 5522
	/* 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;
5523

5524 5525 5526 5527
	/* Halt SCU to enable safe non-atomic accesses */
	status = write16(state, SCU_COMM_EXEC__A, SCU_COMM_EXEC_HOLD);
	if (status < 0)
		goto error;
5528

5529 5530
	/* STEP 4: modulation specific setup */
	switch (state->props.modulation) {
5531
	case QAM_16:
5532
		status = set_qam16(state);
5533 5534
		break;
	case QAM_32:
5535
		status = set_qam32(state);
5536 5537 5538
		break;
	case QAM_AUTO:
	case QAM_64:
5539
		status = set_qam64(state);
5540 5541
		break;
	case QAM_128:
5542
		status = set_qam128(state);
5543 5544
		break;
	case QAM_256:
5545
		status = set_qam256(state);
5546 5547 5548 5549 5550 5551 5552
		break;
	default:
		status = -EINVAL;
		break;
	}
	if (status < 0)
		goto error;
5553

5554 5555 5556 5557
	/* Activate SCU to enable SCU commands */
	status = write16(state, SCU_COMM_EXEC__A, SCU_COMM_EXEC_ACTIVE);
	if (status < 0)
		goto error;
5558

5559
	/* Re-configure MPEG output, requires knowledge of channel bitrate */
5560
	/* extAttr->currentChannel.modulation = channel->modulation; */
5561
	/* extAttr->currentChannel.symbolrate    = channel->symbolrate; */
5562
	status = mpegts_dto_setup(state, state->m_operation_mode);
5563 5564
	if (status < 0)
		goto error;
5565

5566 5567
	/* start processes */
	status = mpegts_start(state);
5568 5569 5570 5571 5572 5573 5574 5575 5576 5577 5578
	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;
5579

5580
	/* STEP 5: start QAM demodulator (starts FEC, QAM and IQM HW) */
5581
	status = scu_command(state, SCU_RAM_COMMAND_STANDARD_QAM | SCU_RAM_COMMAND_CMD_DEMOD_START, 0, NULL, 1, &cmd_result);
5582 5583
	if (status < 0)
		goto error;
5584

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

5588
error:
5589
	if (status < 0)
5590
		pr_err("Error %d on %s\n", status, __func__);
5591
	return status;
R
Ralph Metzler 已提交
5592 5593
}

5594 5595
static int set_qam_standard(struct drxk_state *state,
			  enum operation_mode o_mode)
R
Ralph Metzler 已提交
5596
{
5597
	int status;
R
Ralph Metzler 已提交
5598 5599 5600 5601 5602 5603
#ifdef DRXK_QAM_TAPS
#define DRXK_QAMA_TAPS_SELECT
#include "drxk_filters.h"
#undef DRXK_QAMA_TAPS_SELECT
#endif

5604 5605
	dprintk(1, "\n");

5606
	/* added antenna switch */
5607
	switch_antenna_to_qam(state);
5608

5609
	/* Ensure correct power-up mode */
5610
	status = power_up_qam(state);
5611 5612 5613
	if (status < 0)
		goto error;
	/* Reset QAM block */
5614
	status = qam_reset_qam(state);
5615 5616
	if (status < 0)
		goto error;
5617

5618
	/* Setup IQM */
5619

5620 5621 5622 5623 5624 5625
	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;
5626

5627 5628
	/* Upload IQM Channel Filter settings by
		boot loader from ROM table */
5629
	switch (o_mode) {
5630
	case OM_QAM_ITU_A:
5631
		status = bl_chain_cmd(state, DRXK_BL_ROM_OFFSET_TAPS_ITU_A, DRXK_BLCC_NR_ELEMENTS_TAPS, DRXK_BLC_TIMEOUT);
5632 5633
		break;
	case OM_QAM_ITU_C:
5634
		status = bl_direct_cmd(state, IQM_CF_TAP_RE0__A, DRXK_BL_ROM_OFFSET_TAPS_ITU_C, DRXK_BLDC_NR_ELEMENTS_TAPS, DRXK_BLC_TIMEOUT);
5635
		if (status < 0)
5636
			goto error;
5637
		status = bl_direct_cmd(state, IQM_CF_TAP_IM0__A, DRXK_BL_ROM_OFFSET_TAPS_ITU_C, DRXK_BLDC_NR_ELEMENTS_TAPS, DRXK_BLC_TIMEOUT);
5638 5639 5640 5641 5642 5643
		break;
	default:
		status = -EINVAL;
	}
	if (status < 0)
		goto error;
5644

5645 5646 5647 5648 5649 5650 5651 5652 5653
	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;
5654

5655 5656 5657 5658 5659 5660 5661 5662 5663 5664 5665 5666 5667 5668 5669
	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;
5670

5671 5672 5673 5674 5675 5676 5677 5678 5679 5680 5681 5682
	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;
5683

5684 5685 5686 5687 5688 5689 5690 5691 5692 5693 5694 5695 5696 5697 5698 5699 5700 5701 5702 5703 5704 5705
	/* 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;
5706

5707
	/* turn on IQMAF. Must be done before setAgc**() */
5708
	status = set_iqm_af(state, true);
5709 5710 5711 5712 5713
	if (status < 0)
		goto error;
	status = write16(state, IQM_AF_START_LOCK__A, 0x01);
	if (status < 0)
		goto error;
5714

5715
	/* IQM will not be reset from here, sync ADC and update/init AGC */
5716
	status = adc_synchronization(state);
5717 5718
	if (status < 0)
		goto error;
5719

5720 5721 5722 5723
	/* Set the FSM step period */
	status = write16(state, SCU_RAM_QAM_FSM_STEP_PERIOD__A, 2000);
	if (status < 0)
		goto error;
5724

5725 5726 5727 5728
	/* Halt SCU to enable safe non-atomic accesses */
	status = write16(state, SCU_COMM_EXEC__A, SCU_COMM_EXEC_HOLD);
	if (status < 0)
		goto error;
5729

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

5733
	status = init_agc(state, true);
5734 5735
	if (status < 0)
		goto error;
5736
	status = set_pre_saw(state, &(state->m_qam_pre_saw_cfg));
5737 5738
	if (status < 0)
		goto error;
5739

5740
	/* Configure AGC's */
5741
	status = set_agc_rf(state, &(state->m_qam_rf_agc_cfg), true);
5742 5743
	if (status < 0)
		goto error;
5744
	status = set_agc_if(state, &(state->m_qam_if_agc_cfg), true);
5745 5746
	if (status < 0)
		goto error;
5747

5748 5749 5750 5751
	/* Activate SCU to enable SCU commands */
	status = write16(state, SCU_COMM_EXEC__A, SCU_COMM_EXEC_ACTIVE);
error:
	if (status < 0)
5752
		pr_err("Error %d on %s\n", status, __func__);
5753
	return status;
R
Ralph Metzler 已提交
5754 5755
}

5756
static int write_gpio(struct drxk_state *state)
R
Ralph Metzler 已提交
5757
{
5758 5759 5760
	int status;
	u16 value = 0;

5761
	dprintk(1, "\n");
5762 5763 5764 5765
	/* stop lock indicator process */
	status = write16(state, SCU_RAM_GPIO__A, SCU_RAM_GPIO_HW_LOCK_IND_DISABLE);
	if (status < 0)
		goto error;
5766

5767 5768 5769 5770
	/*  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;
5771

5772 5773
	if (state->m_has_sawsw) {
		if (state->uio_mask & 0x0001) { /* UIO-1 */
5774
			/* write to io pad configuration register - output mode */
5775
			status = write16(state, SIO_PDR_SMA_TX_CFG__A, state->m_gpio_cfg);
5776 5777
			if (status < 0)
				goto error;
5778

5779 5780 5781 5782
			/* use corresponding bit in io data output registar */
			status = read16(state, SIO_PDR_UIO_OUT_LO__A, &value);
			if (status < 0)
				goto error;
5783
			if ((state->m_gpio & 0x0001) == 0)
5784 5785 5786 5787 5788 5789 5790 5791
				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;
		}
5792
		if (state->uio_mask & 0x0002) { /* UIO-2 */
5793
			/* write to io pad configuration register - output mode */
5794
			status = write16(state, SIO_PDR_SMA_RX_CFG__A, state->m_gpio_cfg);
5795 5796
			if (status < 0)
				goto error;
5797

5798 5799 5800 5801
			/* use corresponding bit in io data output registar */
			status = read16(state, SIO_PDR_UIO_OUT_LO__A, &value);
			if (status < 0)
				goto error;
5802
			if ((state->m_gpio & 0x0002) == 0)
5803 5804 5805 5806 5807 5808 5809 5810
				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;
		}
5811
		if (state->uio_mask & 0x0004) { /* UIO-3 */
5812
			/* write to io pad configuration register - output mode */
5813
			status = write16(state, SIO_PDR_GPIO_CFG__A, state->m_gpio_cfg);
5814 5815 5816 5817 5818 5819 5820
			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;
5821
			if ((state->m_gpio & 0x0004) == 0)
5822 5823 5824 5825 5826 5827 5828 5829
				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;
		}
5830 5831 5832 5833 5834
	}
	/*  Write magic word to disable pdr reg write               */
	status = write16(state, SIO_TOP_COMM_KEY__A, 0x0000);
error:
	if (status < 0)
5835
		pr_err("Error %d on %s\n", status, __func__);
5836
	return status;
R
Ralph Metzler 已提交
5837 5838
}

5839
static int switch_antenna_to_qam(struct drxk_state *state)
R
Ralph Metzler 已提交
5840
{
5841
	int status = 0;
5842
	bool gpio_state;
5843

5844
	dprintk(1, "\n");
5845

5846 5847 5848
	if (!state->antenna_gpio)
		return 0;

5849
	gpio_state = state->m_gpio & state->antenna_gpio;
5850 5851 5852 5853

	if (state->antenna_dvbt ^ gpio_state) {
		/* Antenna is on DVB-T mode. Switch */
		if (state->antenna_dvbt)
5854
			state->m_gpio &= ~state->antenna_gpio;
5855
		else
5856 5857
			state->m_gpio |= state->antenna_gpio;
		status = write_gpio(state);
5858
	}
5859
	if (status < 0)
5860
		pr_err("Error %d on %s\n", status, __func__);
5861
	return status;
R
Ralph Metzler 已提交
5862 5863
}

5864
static int switch_antenna_to_dvbt(struct drxk_state *state)
R
Ralph Metzler 已提交
5865
{
5866
	int status = 0;
5867
	bool gpio_state;
5868

5869
	dprintk(1, "\n");
5870 5871 5872 5873

	if (!state->antenna_gpio)
		return 0;

5874
	gpio_state = state->m_gpio & state->antenna_gpio;
5875 5876 5877 5878

	if (!(state->antenna_dvbt ^ gpio_state)) {
		/* Antenna is on DVB-C mode. Switch */
		if (state->antenna_dvbt)
5879
			state->m_gpio |= state->antenna_gpio;
5880
		else
5881 5882
			state->m_gpio &= ~state->antenna_gpio;
		status = write_gpio(state);
R
Ralph Metzler 已提交
5883
	}
5884
	if (status < 0)
5885
		pr_err("Error %d on %s\n", status, __func__);
R
Ralph Metzler 已提交
5886 5887 5888 5889
	return status;
}


5890
static int power_down_device(struct drxk_state *state)
R
Ralph Metzler 已提交
5891 5892 5893 5894 5895 5896 5897 5898
{
	/* 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;
5899 5900

	dprintk(1, "\n");
5901
	if (state->m_b_p_down_open_bridge) {
5902 5903
		/* Open I2C bridge before power down of DRXK */
		status = ConfigureI2CBridge(state, true);
5904
		if (status < 0)
5905 5906 5907
			goto error;
	}
	/* driver 0.9.0 */
5908
	status = dvbt_enable_ofdm_token_ring(state, false);
5909 5910
	if (status < 0)
		goto error;
R
Ralph Metzler 已提交
5911

5912
	status = write16(state, SIO_CC_PWD_MODE__A, SIO_CC_PWD_MODE_LEVEL_CLOCK);
5913
	if (status < 0)
5914 5915 5916 5917
		goto error;
	status = write16(state, SIO_CC_UPDATE__A, SIO_CC_UPDATE_KEY);
	if (status < 0)
		goto error;
5918 5919
	state->m_hi_cfg_ctrl |= SIO_HI_RA_RAM_PAR_5_CFG_SLEEP_ZZZ;
	status = hi_cfg_command(state);
5920 5921
error:
	if (status < 0)
5922
		pr_err("Error %d on %s\n", status, __func__);
5923

5924
	return status;
R
Ralph Metzler 已提交
5925 5926 5927 5928
}

static int init_drxk(struct drxk_state *state)
{
5929
	int status = 0, n = 0;
5930 5931
	enum drx_power_mode power_mode = DRXK_POWER_DOWN_OFDM;
	u16 driver_version;
R
Ralph Metzler 已提交
5932

5933
	dprintk(1, "\n");
5934
	if ((state->m_drxk_state == DRXK_UNINITIALIZED)) {
5935
		drxk_i2c_lock(state);
5936
		status = power_up_device(state);
5937 5938
		if (status < 0)
			goto error;
5939
		status = drxx_open(state);
5940 5941 5942 5943 5944 5945 5946 5947 5948 5949 5950
		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);
5951 5952
		state->m_drxk_a3_patch_code = true;
		status = get_device_capabilities(state);
5953 5954 5955 5956 5957 5958
		if (status < 0)
			goto error;

		/* Bridge delay, uses oscilator clock */
		/* Delay = (delay (nano seconds) * oscclk (kHz))/ 1000 */
		/* SDA brdige delay */
5959 5960
		state->m_hi_cfg_bridge_delay =
			(u16) ((state->m_osc_clock_freq / 1000) *
5961 5962
				HI_I2C_BRIDGE_DELAY) / 1000;
		/* Clipping */
5963
		if (state->m_hi_cfg_bridge_delay >
5964
			SIO_HI_RA_RAM_PAR_3_CFG_DBL_SDA__M) {
5965
			state->m_hi_cfg_bridge_delay =
5966 5967 5968
				SIO_HI_RA_RAM_PAR_3_CFG_DBL_SDA__M;
		}
		/* SCL bridge delay, same as SDA for now */
5969 5970
		state->m_hi_cfg_bridge_delay +=
			state->m_hi_cfg_bridge_delay <<
5971 5972
			SIO_HI_RA_RAM_PAR_3_CFG_DBL_SCL__B;

5973
		status = init_hi(state);
5974 5975 5976
		if (status < 0)
			goto error;
		/* disable various processes */
R
Ralph Metzler 已提交
5977
#if NOA1ROM
5978 5979
		if (!(state->m_DRXK_A1_ROM_CODE)
			&& !(state->m_DRXK_A2_ROM_CODE))
R
Ralph Metzler 已提交
5980
#endif
5981 5982
		{
			status = write16(state, SCU_RAM_GPIO__A, SCU_RAM_GPIO_HW_LOCK_IND_DISABLE);
5983
			if (status < 0)
5984 5985
				goto error;
		}
R
Ralph Metzler 已提交
5986

5987
		/* disable MPEG port */
5988
		status = mpegts_disable(state);
5989 5990
		if (status < 0)
			goto error;
R
Ralph Metzler 已提交
5991

5992 5993 5994 5995 5996 5997 5998
		/* 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;
5999

6000 6001 6002 6003
		/* 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 已提交
6004

6005 6006 6007 6008
		/* include boot loader section */
		status = write16(state, SIO_BL_COMM_EXEC__A, SIO_BL_COMM_EXEC_ACTIVE);
		if (status < 0)
			goto error;
6009
		status = bl_chain_cmd(state, 0, 6, 100);
6010 6011
		if (status < 0)
			goto error;
R
Ralph Metzler 已提交
6012

6013
		if (state->fw) {
6014
			status = download_microcode(state, state->fw->data,
6015 6016 6017 6018
						   state->fw->size);
			if (status < 0)
				goto error;
		}
R
Ralph Metzler 已提交
6019

6020 6021 6022 6023
		/* 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;
6024

6025 6026 6027 6028
		/* 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;
6029
		status = drxx_open(state);
6030 6031 6032 6033
		if (status < 0)
			goto error;
		/* added for test */
		msleep(30);
R
Ralph Metzler 已提交
6034

6035 6036
		power_mode = DRXK_POWER_DOWN_OFDM;
		status = ctrl_power_mode(state, &power_mode);
6037 6038 6039 6040 6041 6042 6043 6044 6045
		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.
			*/
6046
		driver_version =
6047 6048 6049 6050
			(((DRXK_VERSION_MAJOR / 100) % 10) << 12) +
			(((DRXK_VERSION_MAJOR / 10) % 10) << 8) +
			((DRXK_VERSION_MAJOR % 10) << 4) +
			(DRXK_VERSION_MINOR % 10);
6051
		status = write16(state, SCU_RAM_DRIVER_VER_HI__A, driver_version);
6052 6053
		if (status < 0)
			goto error;
6054
		driver_version =
6055 6056 6057 6058
			(((DRXK_VERSION_PATCH / 1000) % 10) << 12) +
			(((DRXK_VERSION_PATCH / 100) % 10) << 8) +
			(((DRXK_VERSION_PATCH / 10) % 10) << 4) +
			(DRXK_VERSION_PATCH % 10);
6059
		status = write16(state, SCU_RAM_DRIVER_VER_LO__A, driver_version);
6060 6061 6062
		if (status < 0)
			goto error;

6063
		pr_info("DRXK driver version %d.%d.%d\n",
6064 6065 6066 6067 6068 6069 6070 6071
			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 */

6072
		/* m_dvbt_rf_agc_cfg.speed = 3; */
6073 6074 6075 6076 6077 6078 6079 6080 6081 6082 6083 6084

		/* 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 */
6085
		status = mpegts_dto_init(state);
6086 6087
		if (status < 0)
			goto error;
6088
		status = mpegts_stop(state);
6089 6090
		if (status < 0)
			goto error;
6091
		status = mpegts_configure_polarity(state);
6092 6093
		if (status < 0)
			goto error;
6094
		status = mpegts_configure_pins(state, state->m_enable_mpeg_output);
6095 6096 6097
		if (status < 0)
			goto error;
		/* added: configure GPIO */
6098
		status = write_gpio(state);
6099 6100
		if (status < 0)
			goto error;
R
Ralph Metzler 已提交
6101

6102
		state->m_drxk_state = DRXK_STOPPED;
R
Ralph Metzler 已提交
6103

6104 6105
		if (state->m_b_power_down) {
			status = power_down_device(state);
6106
			if (status < 0)
6107
				goto error;
6108
			state->m_drxk_state = DRXK_POWERED_DOWN;
6109
		} else
6110
			state->m_drxk_state = DRXK_STOPPED;
6111 6112 6113

		/* Initialize the supported delivery systems */
		n = 0;
6114
		if (state->m_has_dvbc) {
6115 6116 6117 6118 6119
			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));
		}
6120
		if (state->m_has_dvbt) {
6121 6122 6123 6124
			state->frontend.ops.delsys[n++] = SYS_DVBT;
			strlcat(state->frontend.ops.info.name, " DVB-T",
				sizeof(state->frontend.ops.info.name));
		}
6125
		drxk_i2c_unlock(state);
R
Ralph Metzler 已提交
6126
	}
6127
error:
6128
	if (status < 0) {
6129
		state->m_drxk_state = DRXK_NO_DEV;
6130
		drxk_i2c_unlock(state);
6131
		pr_err("Error %d on %s\n", status, __func__);
6132
	}
R
Ralph Metzler 已提交
6133

6134
	return status;
R
Ralph Metzler 已提交
6135 6136
}

6137 6138 6139 6140 6141
static void load_firmware_cb(const struct firmware *fw,
			     void *context)
{
	struct drxk_state *state = context;

6142
	dprintk(1, ": %s\n", fw ? "firmware loaded" : "firmware not loaded");
6143
	if (!fw) {
6144
		pr_err("Could not load firmware file %s.\n",
6145
			state->microcode_name);
6146
		pr_info("Copy %s to your hotplug directory!\n",
6147 6148 6149 6150 6151 6152 6153 6154 6155 6156 6157 6158 6159 6160 6161 6162 6163 6164 6165
			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);
}

6166
static void drxk_release(struct dvb_frontend *fe)
R
Ralph Metzler 已提交
6167
{
6168 6169
	struct drxk_state *state = fe->demodulator_priv;

6170
	dprintk(1, "\n");
6171 6172 6173
	if (state->fw)
		release_firmware(state->fw);

R
Ralph Metzler 已提交
6174 6175 6176
	kfree(state);
}

6177
static int drxk_sleep(struct dvb_frontend *fe)
R
Ralph Metzler 已提交
6178
{
6179
	struct drxk_state *state = fe->demodulator_priv;
R
Ralph Metzler 已提交
6180

6181
	dprintk(1, "\n");
6182

6183
	if (state->m_drxk_state == DRXK_NO_DEV)
6184
		return -ENODEV;
6185
	if (state->m_drxk_state == DRXK_UNINITIALIZED)
6186 6187
		return 0;

6188
	shut_down(state);
R
Ralph Metzler 已提交
6189 6190 6191
	return 0;
}

6192
static int drxk_gate_ctrl(struct dvb_frontend *fe, int enable)
R
Ralph Metzler 已提交
6193 6194 6195
{
	struct drxk_state *state = fe->demodulator_priv;

6196
	dprintk(1, ": %s\n", enable ? "enable" : "disable");
6197

6198
	if (state->m_drxk_state == DRXK_NO_DEV)
6199 6200
		return -ENODEV;

R
Ralph Metzler 已提交
6201 6202 6203
	return ConfigureI2CBridge(state, enable ? true : false);
}

6204
static int drxk_set_parameters(struct dvb_frontend *fe)
R
Ralph Metzler 已提交
6205
{
6206
	struct dtv_frontend_properties *p = &fe->dtv_property_cache;
6207
	u32 delsys  = p->delivery_system, old_delsys;
R
Ralph Metzler 已提交
6208 6209 6210
	struct drxk_state *state = fe->demodulator_priv;
	u32 IF;

6211
	dprintk(1, "\n");
6212

6213
	if (state->m_drxk_state == DRXK_NO_DEV)
6214 6215
		return -ENODEV;

6216
	if (state->m_drxk_state == DRXK_UNINITIALIZED)
6217 6218
		return -EAGAIN;

6219
	if (!fe->ops.tuner_ops.get_if_frequency) {
6220
		pr_err("Error: get_if_frequency() not defined at tuner. Can't work without it!\n");
6221 6222 6223
		return -EINVAL;
	}

6224 6225 6226 6227 6228 6229
	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);
6230 6231

	old_delsys = state->props.delivery_system;
6232 6233
	state->props = *p;

6234
	if (old_delsys != delsys) {
6235
		shut_down(state);
6236 6237 6238
		switch (delsys) {
		case SYS_DVBC_ANNEX_A:
		case SYS_DVBC_ANNEX_C:
6239
			if (!state->m_has_dvbc)
6240 6241 6242
				return -EINVAL;
			state->m_itut_annex_c = (delsys == SYS_DVBC_ANNEX_C) ? true : false;
			if (state->m_itut_annex_c)
6243
				setoperation_mode(state, OM_QAM_ITU_C);
6244
			else
6245
				setoperation_mode(state, OM_QAM_ITU_A);
6246
			break;
6247
		case SYS_DVBT:
6248
			if (!state->m_has_dvbt)
6249
				return -EINVAL;
6250
			setoperation_mode(state, OM_DVBT);
6251 6252
			break;
		default:
6253
			return -EINVAL;
6254
		}
6255
	}
6256 6257

	fe->ops.tuner_ops.get_if_frequency(fe, &IF);
6258
	start(state, 0, IF);
R
Ralph Metzler 已提交
6259

6260 6261 6262 6263 6264 6265 6266 6267 6268 6269
	/* 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;

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

R
Ralph Metzler 已提交
6272 6273 6274
	return 0;
}

6275 6276 6277
static int get_strength(struct drxk_state *state, u64 *strength)
{
	int status;
6278 6279
	struct s_cfg_agc   rf_agc, if_agc;
	u32          total_gain  = 0;
6280
	u32          atten      = 0;
6281
	u32          agc_range   = 0;
6282 6283 6284
	u16            scu_lvl  = 0;
	u16            scu_coc  = 0;
	/* FIXME: those are part of the tuner presets */
6285 6286
	u16 tuner_rf_gain         = 50; /* Default value on az6007 driver */
	u16 tuner_if_gain         = 40; /* Default value on az6007 driver */
6287 6288 6289

	*strength = 0;

6290 6291 6292 6293 6294 6295
	if (is_dvbt(state)) {
		rf_agc = state->m_dvbt_rf_agc_cfg;
		if_agc = state->m_dvbt_if_agc_cfg;
	} else if (is_qam(state)) {
		rf_agc = state->m_qam_rf_agc_cfg;
		if_agc = state->m_qam_if_agc_cfg;
6296
	} else {
6297 6298
		rf_agc = state->m_atv_rf_agc_cfg;
		if_agc = state->m_atv_if_agc_cfg;
6299 6300
	}

6301 6302
	if (rf_agc.ctrl_mode == DRXK_AGC_CTRL_AUTO) {
		/* SCU output_level */
6303 6304 6305 6306 6307 6308 6309 6310 6311 6312
		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)
6313
			rf_agc.output_level = scu_lvl + scu_coc;
6314
		else
6315
			rf_agc.output_level = 0xffff;
6316 6317

		/* Take RF gain into account */
6318
		total_gain += tuner_rf_gain;
6319 6320

		/* clip output value */
6321 6322 6323 6324
		if (rf_agc.output_level < rf_agc.min_output_level)
			rf_agc.output_level = rf_agc.min_output_level;
		if (rf_agc.output_level > rf_agc.max_output_level)
			rf_agc.output_level = rf_agc.max_output_level;
6325

6326 6327
		agc_range = (u32) (rf_agc.max_output_level - rf_agc.min_output_level);
		if (agc_range > 0) {
6328
			atten += 100UL *
6329 6330 6331
				((u32)(tuner_rf_gain)) *
				((u32)(rf_agc.output_level - rf_agc.min_output_level))
				/ agc_range;
6332 6333 6334
		}
	}

6335
	if (if_agc.ctrl_mode == DRXK_AGC_CTRL_AUTO) {
6336
		status = read16(state, SCU_RAM_AGC_IF_IACCU_HI__A,
6337
				&if_agc.output_level);
6338 6339 6340 6341
		if (status < 0)
			return status;

		status = read16(state, SCU_RAM_AGC_INGAIN_TGT_MIN__A,
6342
				&if_agc.top);
6343 6344 6345 6346
		if (status < 0)
			return status;

		/* Take IF gain into account */
6347
		total_gain += (u32) tuner_if_gain;
6348 6349

		/* clip output value */
6350 6351 6352 6353
		if (if_agc.output_level < if_agc.min_output_level)
			if_agc.output_level = if_agc.min_output_level;
		if (if_agc.output_level > if_agc.max_output_level)
			if_agc.output_level = if_agc.max_output_level;
6354

6355 6356
		agc_range  = (u32) (if_agc.max_output_level - if_agc.min_output_level);
		if (agc_range > 0) {
6357
			atten += 100UL *
6358 6359 6360
				((u32)(tuner_if_gain)) *
				((u32)(if_agc.output_level - if_agc.min_output_level))
				/ agc_range;
6361 6362 6363 6364 6365 6366 6367
		}
	}

	/*
	 * Convert to 0..65535 scale.
	 * If it can't be measured (AGC is disabled), just show 100%.
	 */
6368 6369
	if (total_gain > 0)
		*strength = (65535UL * atten / total_gain / 100);
6370 6371 6372 6373 6374 6375
	else
		*strength = 65535;

	return 0;
}

6376
static int drxk_get_stats(struct dvb_frontend *fe)
R
Ralph Metzler 已提交
6377
{
6378
	struct dtv_frontend_properties *c = &fe->dtv_property_cache;
R
Ralph Metzler 已提交
6379
	struct drxk_state *state = fe->demodulator_priv;
6380
	int status;
R
Ralph Metzler 已提交
6381
	u32 stat;
6382 6383 6384 6385 6386 6387 6388 6389
	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;
6390
	s32 cnr;
6391

6392
	if (state->m_drxk_state == DRXK_NO_DEV)
6393
		return -ENODEV;
6394
	if (state->m_drxk_state == DRXK_UNINITIALIZED)
6395 6396
		return -EAGAIN;

6397 6398
	/* get status */
	state->fe_status = 0;
6399
	get_lock_status(state, &stat);
6400
	if (stat == MPEG_LOCK)
6401
		state->fe_status |= 0x1f;
6402
	if (stat == FEC_LOCK)
6403
		state->fe_status |= 0x0f;
6404
	if (stat == DEMOD_LOCK)
6405 6406
		state->fe_status |= 0x07;

6407 6408 6409 6410 6411 6412 6413
	/*
	 * Estimate signal strength from AGC
	 */
	get_strength(state, &c->strength.stat[0].uvalue);
	c->strength.stat[0].scale = FE_SCALE_RELATIVE;


6414
	if (stat >= DEMOD_LOCK) {
6415
		get_signal_to_noise(state, &cnr);
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 6494 6495 6496 6497 6498 6499 6500 6501 6502 6503 6504 6505 6506 6507 6508 6509 6510
		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 已提交
6511 6512 6513
	return 0;
}

6514 6515
static int drxk_read_signal_strength(struct dvb_frontend *fe,
				     u16 *strength)
R
Ralph Metzler 已提交
6516 6517
{
	struct drxk_state *state = fe->demodulator_priv;
6518
	struct dtv_frontend_properties *c = &fe->dtv_property_cache;
R
Ralph Metzler 已提交
6519

6520
	dprintk(1, "\n");
6521

6522
	if (state->m_drxk_state == DRXK_NO_DEV)
6523
		return -ENODEV;
6524
	if (state->m_drxk_state == DRXK_UNINITIALIZED)
6525 6526
		return -EAGAIN;

6527
	*strength = c->strength.stat[0].uvalue;
R
Ralph Metzler 已提交
6528 6529 6530 6531 6532 6533 6534 6535
	return 0;
}

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

6536
	dprintk(1, "\n");
6537

6538
	if (state->m_drxk_state == DRXK_NO_DEV)
6539
		return -ENODEV;
6540
	if (state->m_drxk_state == DRXK_UNINITIALIZED)
6541 6542
		return -EAGAIN;

6543
	get_signal_to_noise(state, &snr2);
6544 6545 6546 6547

	/* No negative SNR, clip to zero */
	if (snr2 < 0)
		snr2 = 0;
6548
	*snr = snr2 & 0xffff;
R
Ralph Metzler 已提交
6549 6550 6551 6552 6553 6554 6555 6556
	return 0;
}

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

6557
	dprintk(1, "\n");
6558

6559
	if (state->m_drxk_state == DRXK_NO_DEV)
6560
		return -ENODEV;
6561
	if (state->m_drxk_state == DRXK_UNINITIALIZED)
6562 6563
		return -EAGAIN;

6564
	dvbtqam_get_acc_pkt_err(state, &err);
R
Ralph Metzler 已提交
6565 6566 6567 6568
	*ucblocks = (u32) err;
	return 0;
}

6569
static int drxk_get_tune_settings(struct dvb_frontend *fe, struct dvb_frontend_tune_settings
6570
				    *sets)
R
Ralph Metzler 已提交
6571
{
6572
	struct drxk_state *state = fe->demodulator_priv;
6573
	struct dtv_frontend_properties *p = &fe->dtv_property_cache;
6574 6575

	dprintk(1, "\n");
6576

6577
	if (state->m_drxk_state == DRXK_NO_DEV)
6578
		return -ENODEV;
6579
	if (state->m_drxk_state == DRXK_UNINITIALIZED)
6580 6581
		return -EAGAIN;

6582 6583 6584
	switch (p->delivery_system) {
	case SYS_DVBC_ANNEX_A:
	case SYS_DVBC_ANNEX_C:
6585
	case SYS_DVBT:
6586 6587 6588 6589 6590 6591 6592
		sets->min_delay_ms = 3000;
		sets->max_drift = 0;
		sets->step_size = 0;
		return 0;
	default:
		return -EINVAL;
	}
R
Ralph Metzler 已提交
6593 6594
}

6595 6596
static struct dvb_frontend_ops drxk_ops = {
	/* .delsys will be filled dynamically */
R
Ralph Metzler 已提交
6597
	.info = {
6598 6599 6600 6601 6602 6603 6604 6605 6606 6607 6608 6609 6610 6611 6612 6613 6614 6615 6616
		.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 已提交
6617 6618
	.i2c_gate_ctrl = drxk_gate_ctrl,

6619
	.set_frontend = drxk_set_parameters,
6620
	.get_tune_settings = drxk_get_tune_settings,
R
Ralph Metzler 已提交
6621 6622 6623 6624 6625 6626 6627

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

6628
struct dvb_frontend *drxk_attach(const struct drxk_config *config,
6629
				 struct i2c_adapter *i2c)
R
Ralph Metzler 已提交
6630
{
6631
	struct dtv_frontend_properties *p;
R
Ralph Metzler 已提交
6632
	struct drxk_state *state = NULL;
6633
	u8 adr = config->adr;
6634
	int status;
R
Ralph Metzler 已提交
6635

6636
	dprintk(1, "\n");
6637
	state = kzalloc(sizeof(struct drxk_state), GFP_KERNEL);
R
Ralph Metzler 已提交
6638 6639 6640
	if (!state)
		return NULL;

6641 6642
	state->i2c = i2c;
	state->demod_address = adr;
6643
	state->single_master = config->single_master;
6644
	state->microcode_name = config->microcode_name;
6645
	state->qam_demod_parameter_count = config->qam_demod_parameter_count;
6646
	state->no_i2c_bridge = config->no_i2c_bridge;
6647 6648
	state->antenna_gpio = config->antenna_gpio;
	state->antenna_dvbt = config->antenna_dvbt;
6649
	state->m_chunk_size = config->chunk_size;
6650
	state->enable_merr_cfg = config->enable_merr_cfg;
6651

6652
	if (config->dynamic_clk) {
6653 6654
		state->m_dvbt_static_clk = 0;
		state->m_dvbc_static_clk = 0;
6655
	} else {
6656 6657
		state->m_dvbt_static_clk = 1;
		state->m_dvbc_static_clk = 1;
6658 6659
	}

6660 6661

	if (config->mpeg_out_clk_strength)
6662
		state->m_ts_clockk_strength = config->mpeg_out_clk_strength & 0x07;
6663
	else
6664
		state->m_ts_clockk_strength = 0x06;
6665

6666
	if (config->parallel_ts)
6667
		state->m_enable_parallel = true;
6668
	else
6669
		state->m_enable_parallel = false;
6670

6671
	/* NOTE: as more UIO bits will be used, add them to the mask */
6672
	state->uio_mask = config->antenna_gpio;
6673 6674 6675

	/* Default gpio to DVB-C */
	if (!state->antenna_dvbt && state->antenna_gpio)
6676
		state->m_gpio |= state->antenna_gpio;
6677
	else
6678
		state->m_gpio &= ~state->antenna_gpio;
R
Ralph Metzler 已提交
6679 6680 6681

	mutex_init(&state->mutex);

6682 6683
	memcpy(&state->frontend.ops, &drxk_ops, sizeof(drxk_ops));
	state->frontend.demodulator_priv = state;
R
Ralph Metzler 已提交
6684 6685

	init_state(state);
6686

6687 6688
	/* Load firmware and initialize DRX-K */
	if (state->microcode_name) {
6689 6690 6691 6692 6693 6694 6695 6696 6697 6698
		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,
6699 6700 6701 6702
					      state->microcode_name,
					      state->i2c->dev.parent,
					      GFP_KERNEL,
					      state, load_firmware_cb);
6703
			if (status < 0) {
6704
				pr_err("failed to request a firmware\n");
6705 6706
				return NULL;
			}
6707 6708 6709
		}
	} else if (init_drxk(state) < 0)
		goto error;
6710

6711 6712 6713 6714 6715 6716 6717 6718 6719 6720 6721 6722 6723 6724 6725 6726 6727 6728 6729 6730 6731

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

6732
	pr_info("frontend initialized.\n");
6733
	return &state->frontend;
R
Ralph Metzler 已提交
6734 6735

error:
6736
	pr_err("not found\n");
R
Ralph Metzler 已提交
6737 6738 6739
	kfree(state);
	return NULL;
}
6740
EXPORT_SYMBOL(drxk_attach);
R
Ralph Metzler 已提交
6741 6742 6743 6744

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