drxk_hard.c 177.2 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
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)						\
171
	pr_debug(fmt, ##arg);					\
172 173 174
} 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
	R0 = (a % c) << 4;	/* 32-28 == 4 shifts possible at max */
192 193 194 195
	Q1 = a / c;		/*
				 * integer part, only the 4 least significant
				 * bits will be visible in the result
				 */
R
Ralph Metzler 已提交
196 197 198 199 200 201 202 203 204 205 206 207 208

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

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

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

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

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

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

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

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

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

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

	return status;
R
Ralph Metzler 已提交
274 275
}

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

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

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

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

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

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

R
Ralph Metzler 已提交
336 337 338
	return 0;
}

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

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

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

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

R
Ralph Metzler 已提交
371 372 373
	return 0;
}

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

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

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

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

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

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

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

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

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

	if (state->single_master)
446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462
		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;
463
		} else {
464 465 466 467
			adr_buf[0] = ((address << 1) & 0xFF);
			adr_buf[1] = (((address >> 16) & 0x0F) |
				     ((address >> 18) & 0xF0));
			adr_length = 2;
R
Ralph Metzler 已提交
468
		}
469 470
		memcpy(&state->chunk[adr_length], p_block, chunk);
		dprintk(2, "(0x%08x, 0x%02x)\n", address, flags);
471 472
		if (debug > 1) {
			int i;
473 474
			if (p_block)
				for (i = 0; i < chunk; i++)
475 476
					pr_cont(" %02x", p_block[i]);
			pr_cont("\n");
477
		}
478
		status = i2c_write(state, state->demod_address,
479
				   &state->chunk[0], chunk + adr_length);
480
		if (status < 0) {
481
			pr_err("%s: i2c write error at addr 0x%02x\n",
482
			       __func__, address);
R
Ralph Metzler 已提交
483 484
			break;
		}
485 486 487
		p_block += chunk;
		address += (chunk >> 1);
		blk_size -= chunk;
R
Ralph Metzler 已提交
488
	}
489
	return status;
R
Ralph Metzler 已提交
490 491 492 493 494 495
}

#ifndef DRXK_MAX_RETRIES_POWERUP
#define DRXK_MAX_RETRIES_POWERUP 20
#endif

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

502 503
	dprintk(1, "\n");

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

534
	state->m_current_power_mode = DRX_POWER_UP;
535 536 537

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

R
Ralph Metzler 已提交
540 541 542 543 544 545
	return status;
}


static int init_state(struct drxk_state *state)
{
546 547 548 549
	/*
	 * FIXME: most (all?) of the values bellow should be moved into
	 * struct drxk_config, as they are probably board-specific
	 */
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 575 576
	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 已提交
577 578 579 580

	u32 ulQual83 = DEFAULT_MER_83;
	u32 ulQual93 = DEFAULT_MER_93;

581 582
	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 已提交
583 584 585 586

	/* 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) */
587 588 589 590 591
	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 已提交
592

593
	u32 ul_insert_rs_byte = 0;
R
Ralph Metzler 已提交
594

595 596
	u32 ul_rf_mirror = 1;
	u32 ul_power_down = 0;
R
Ralph Metzler 已提交
597

598 599
	dprintk(1, "\n");

600 601 602 603 604 605
	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 已提交
606

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

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

	/* real system clock frequency in kHz */
615
	state->m_sys_clock_freq = 151875;
R
Ralph Metzler 已提交
616 617
	/* Timing div, 250ns/Psys */
	/* Timing div, = (delay (nano seconds) * sysclk (kHz))/ 1000 */
618
	state->m_hi_cfg_timing_div = ((state->m_sys_clock_freq / 1000) *
R
Ralph Metzler 已提交
619 620
				   HI_I2C_DELAY) / 1000;
	/* Clipping */
621 622 623
	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 已提交
624
	/* port/bridge/power down ctrl */
625
	state->m_hi_cfg_ctrl = SIO_HI_RA_RAM_PAR_5_CFG_SLV0_SLAVE;
R
Ralph Metzler 已提交
626

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

629
	state->m_drxk_a3_patch_code = false;
R
Ralph Metzler 已提交
630 631 632

	/* Init AGC and PGA parameters */
	/* VSB IF */
633 634 635 636 637
	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;
638
	state->m_vsb_pga_cfg = 140;
R
Ralph Metzler 已提交
639 640

	/* VSB RF */
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;
648 649
	state->m_vsb_pre_saw_cfg.reference = 0x07;
	state->m_vsb_pre_saw_cfg.use_pre_saw = true;
R
Ralph Metzler 已提交
650 651 652 653 654 655 656 657 658

	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 */
659 660 661 662 663
	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 已提交
664 665

	/* ATV RF */
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;
673 674
	state->m_atv_pre_saw_cfg.reference = 0x04;
	state->m_atv_pre_saw_cfg.use_pre_saw = true;
R
Ralph Metzler 已提交
675 676 677


	/* DVBT RF */
678 679 680 681 682 683 684
	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 已提交
685 686 687


	/* DVBT IF */
688 689 690 691 692 693 694 695 696
	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;
697
	/* state->m_dvbtPgaCfg = 140; */
R
Ralph Metzler 已提交
698

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

	/* QAM RF */
703 704 705 706 707 708 709
	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 已提交
710 711

	/* QAM IF */
712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727
	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 已提交
728 729

	/* MPEG output configuration */
730 731 732 733 734 735 736
	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 */
737

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

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

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

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

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

753 754 755 756 757 758
	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 已提交
759

760
	/* QAM defaults */
761 762 763 764
	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 已提交
765

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

769
	state->m_gpio_cfg = ul_gpio_cfg;
R
Ralph Metzler 已提交
770

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

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

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

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

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

819
	dprintk(1, "\n");
R
Ralph Metzler 已提交
820

821 822
	/* driver 0.9.0 */
	/* stop lock indicator process */
823 824
	status = write16(state, SCU_RAM_GPIO__A,
			 SCU_RAM_GPIO_HW_LOCK_IND_DISABLE);
825 826
	if (status < 0)
		goto error;
827
	status = write16(state, SIO_TOP_COMM_KEY__A, SIO_TOP_COMM_KEY_KEY);
828 829
	if (status < 0)
		goto error;
830
	status = read16(state, SIO_PDR_OHW_CFG__A, &sio_pdr_ohw_cfg);
831 832 833 834 835
	if (status < 0)
		goto error;
	status = write16(state, SIO_TOP_COMM_KEY__A, 0x0000);
	if (status < 0)
		goto error;
R
Ralph Metzler 已提交
836

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

865
	pr_info("status = 0x%08x\n", sio_top_jtagid_lo);
866

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

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

1004 1005
error:
	if (status < 0)
1006
		pr_err("Error %d on %s\n", status, __func__);
1007 1008

error2:
R
Ralph Metzler 已提交
1009 1010 1011
	return status;
}

1012
static int hi_command(struct drxk_state *state, u16 cmd, u16 *p_result)
R
Ralph Metzler 已提交
1013 1014 1015 1016
{
	int status;
	bool powerdown_cmd;

1017 1018
	dprintk(1, "\n");

R
Ralph Metzler 已提交
1019
	/* Write command */
1020
	status = write16(state, SIO_HI_RA_RAM_CMD__A, cmd);
R
Ralph Metzler 已提交
1021
	if (status < 0)
1022
		goto error;
R
Ralph Metzler 已提交
1023
	if (cmd == SIO_HI_RA_RAM_CMD_RESET)
1024
		usleep_range(1000, 2000);
R
Ralph Metzler 已提交
1025 1026

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

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

R
Ralph Metzler 已提交
1051 1052 1053
	return status;
}

1054
static int hi_cfg_command(struct drxk_state *state)
R
Ralph Metzler 已提交
1055 1056 1057
{
	int status;

1058 1059
	dprintk(1, "\n");

R
Ralph Metzler 已提交
1060 1061
	mutex_lock(&state->mutex);

1062 1063
	status = write16(state, SIO_HI_RA_RAM_PAR_6__A,
			 state->m_hi_cfg_timeout);
1064 1065
	if (status < 0)
		goto error;
1066 1067
	status = write16(state, SIO_HI_RA_RAM_PAR_5__A,
			 state->m_hi_cfg_ctrl);
1068 1069
	if (status < 0)
		goto error;
1070 1071
	status = write16(state, SIO_HI_RA_RAM_PAR_4__A,
			 state->m_hi_cfg_wake_up_key);
1072 1073
	if (status < 0)
		goto error;
1074 1075
	status = write16(state, SIO_HI_RA_RAM_PAR_3__A,
			 state->m_hi_cfg_bridge_delay);
1076 1077
	if (status < 0)
		goto error;
1078 1079
	status = write16(state, SIO_HI_RA_RAM_PAR_2__A,
			 state->m_hi_cfg_timing_div);
1080 1081
	if (status < 0)
		goto error;
1082 1083
	status = write16(state, SIO_HI_RA_RAM_PAR_1__A,
			 SIO_HI_RA_RAM_PAR_1_PAR1_SEC_KEY);
1084 1085
	if (status < 0)
		goto error;
1086
	status = hi_command(state, SIO_HI_RA_RAM_CMD_CONFIG, NULL);
1087 1088 1089
	if (status < 0)
		goto error;

1090
	state->m_hi_cfg_ctrl &= ~SIO_HI_RA_RAM_PAR_5_CFG_SLEEP_ZZZ;
1091
error:
R
Ralph Metzler 已提交
1092
	mutex_unlock(&state->mutex);
1093
	if (status < 0)
1094
		pr_err("Error %d on %s\n", status, __func__);
R
Ralph Metzler 已提交
1095 1096 1097
	return status;
}

1098
static int init_hi(struct drxk_state *state)
R
Ralph Metzler 已提交
1099
{
1100 1101
	dprintk(1, "\n");

1102 1103
	state->m_hi_cfg_wake_up_key = (state->demod_address << 1);
	state->m_hi_cfg_timeout = 0x96FF;
R
Ralph Metzler 已提交
1104
	/* port/bridge/power down ctrl */
1105
	state->m_hi_cfg_ctrl = SIO_HI_RA_RAM_PAR_5_CFG_SLV0_SLAVE;
1106

1107
	return hi_cfg_command(state);
R
Ralph Metzler 已提交
1108 1109
}

1110
static int mpegts_configure_pins(struct drxk_state *state, bool mpeg_enable)
R
Ralph Metzler 已提交
1111 1112
{
	int status = -1;
1113 1114
	u16 sio_pdr_mclk_cfg = 0;
	u16 sio_pdr_mdx_cfg = 0;
1115
	u16 err_cfg = 0;
R
Ralph Metzler 已提交
1116

1117
	dprintk(1, ": mpeg %s, %s mode\n",
1118 1119
		mpeg_enable ? "enable" : "disable",
		state->m_enable_parallel ? "parallel" : "serial");
R
Ralph Metzler 已提交
1120

1121
	/* stop lock indicator process */
1122 1123
	status = write16(state, SCU_RAM_GPIO__A,
			 SCU_RAM_GPIO_HW_LOCK_IND_DISABLE);
1124 1125 1126 1127
	if (status < 0)
		goto error;

	/*  MPEG TS pad configuration */
1128
	status = write16(state, SIO_TOP_COMM_KEY__A, SIO_TOP_COMM_KEY_KEY);
1129 1130 1131
	if (status < 0)
		goto error;

1132
	if (mpeg_enable == false) {
1133 1134
		/*  Set MPEG TS pads to inputmode */
		status = write16(state, SIO_PDR_MSTRT_CFG__A, 0x0000);
1135
		if (status < 0)
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 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171
			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 */
1172 1173
		sio_pdr_mdx_cfg =
			((state->m_ts_data_strength <<
1174
			SIO_PDR_MD0_CFG_DRIVE__B) | 0x0003);
1175
		sio_pdr_mclk_cfg = ((state->m_ts_clockk_strength <<
1176 1177
					SIO_PDR_MCLK_CFG_DRIVE__B) |
					0x0003);
R
Ralph Metzler 已提交
1178

1179
		status = write16(state, SIO_PDR_MSTRT_CFG__A, sio_pdr_mdx_cfg);
1180 1181
		if (status < 0)
			goto error;
1182 1183

		if (state->enable_merr_cfg)
1184
			err_cfg = sio_pdr_mdx_cfg;
1185 1186

		status = write16(state, SIO_PDR_MERR_CFG__A, err_cfg);
1187 1188
		if (status < 0)
			goto error;
1189
		status = write16(state, SIO_PDR_MVAL_CFG__A, err_cfg);
1190 1191
		if (status < 0)
			goto error;
1192

1193
		if (state->m_enable_parallel == true) {
1194
			/* parallel -> enable MD1 to MD7 */
1195 1196
			status = write16(state, SIO_PDR_MD1_CFG__A,
					 sio_pdr_mdx_cfg);
1197
			if (status < 0)
1198
				goto error;
1199 1200
			status = write16(state, SIO_PDR_MD2_CFG__A,
					 sio_pdr_mdx_cfg);
1201
			if (status < 0)
1202
				goto error;
1203 1204
			status = write16(state, SIO_PDR_MD3_CFG__A,
					 sio_pdr_mdx_cfg);
1205
			if (status < 0)
1206
				goto error;
1207 1208
			status = write16(state, SIO_PDR_MD4_CFG__A,
					 sio_pdr_mdx_cfg);
1209
			if (status < 0)
1210
				goto error;
1211 1212
			status = write16(state, SIO_PDR_MD5_CFG__A,
					 sio_pdr_mdx_cfg);
1213
			if (status < 0)
1214
				goto error;
1215 1216
			status = write16(state, SIO_PDR_MD6_CFG__A,
					 sio_pdr_mdx_cfg);
1217 1218
			if (status < 0)
				goto error;
1219 1220
			status = write16(state, SIO_PDR_MD7_CFG__A,
					 sio_pdr_mdx_cfg);
1221 1222 1223
			if (status < 0)
				goto error;
		} else {
1224
			sio_pdr_mdx_cfg = ((state->m_ts_data_strength <<
1225 1226 1227
						SIO_PDR_MD0_CFG_DRIVE__B)
					| 0x0003);
			/* serial -> disable MD1 to MD7 */
1228
			status = write16(state, SIO_PDR_MD1_CFG__A, 0x0000);
1229
			if (status < 0)
1230
				goto error;
1231
			status = write16(state, SIO_PDR_MD2_CFG__A, 0x0000);
1232
			if (status < 0)
1233
				goto error;
1234
			status = write16(state, SIO_PDR_MD3_CFG__A, 0x0000);
1235
			if (status < 0)
1236
				goto error;
1237
			status = write16(state, SIO_PDR_MD4_CFG__A, 0x0000);
1238
			if (status < 0)
1239
				goto error;
1240
			status = write16(state, SIO_PDR_MD5_CFG__A, 0x0000);
1241
			if (status < 0)
1242
				goto error;
1243
			status = write16(state, SIO_PDR_MD6_CFG__A, 0x0000);
1244
			if (status < 0)
1245
				goto error;
1246
			status = write16(state, SIO_PDR_MD7_CFG__A, 0x0000);
1247
			if (status < 0)
1248
				goto error;
R
Ralph Metzler 已提交
1249
		}
1250
		status = write16(state, SIO_PDR_MCLK_CFG__A, sio_pdr_mclk_cfg);
1251
		if (status < 0)
1252
			goto error;
1253
		status = write16(state, SIO_PDR_MD0_CFG__A, sio_pdr_mdx_cfg);
1254
		if (status < 0)
1255 1256 1257 1258 1259 1260 1261 1262 1263 1264
			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)
1265
		pr_err("Error %d on %s\n", status, __func__);
R
Ralph Metzler 已提交
1266 1267 1268
	return status;
}

1269
static int mpegts_disable(struct drxk_state *state)
R
Ralph Metzler 已提交
1270
{
1271 1272
	dprintk(1, "\n");

1273
	return mpegts_configure_pins(state, false);
R
Ralph Metzler 已提交
1274 1275
}

1276 1277
static int bl_chain_cmd(struct drxk_state *state,
		      u16 rom_offset, u16 nr_of_elements, u32 time_out)
R
Ralph Metzler 已提交
1278
{
1279
	u16 bl_status = 0;
R
Ralph Metzler 已提交
1280 1281 1282
	int status;
	unsigned long end;

1283
	dprintk(1, "\n");
R
Ralph Metzler 已提交
1284
	mutex_lock(&state->mutex);
1285 1286 1287
	status = write16(state, SIO_BL_MODE__A, SIO_BL_MODE_CHAIN);
	if (status < 0)
		goto error;
1288
	status = write16(state, SIO_BL_CHAIN_ADDR__A, rom_offset);
1289 1290
	if (status < 0)
		goto error;
1291
	status = write16(state, SIO_BL_CHAIN_LEN__A, nr_of_elements);
1292 1293 1294 1295 1296 1297
	if (status < 0)
		goto error;
	status = write16(state, SIO_BL_ENABLE__A, SIO_BL_ENABLE_ON);
	if (status < 0)
		goto error;

1298
	end = jiffies + msecs_to_jiffies(time_out);
R
Ralph Metzler 已提交
1299
	do {
1300
		usleep_range(1000, 2000);
1301
		status = read16(state, SIO_BL_STATUS__A, &bl_status);
1302
		if (status < 0)
1303
			goto error;
1304
	} while ((bl_status == 0x1) &&
1305
			((time_is_after_jiffies(end))));
R
Ralph Metzler 已提交
1306

1307
	if (bl_status == 0x1) {
1308
		pr_err("SIO not ready\n");
1309 1310 1311 1312 1313
		status = -EINVAL;
		goto error2;
	}
error:
	if (status < 0)
1314
		pr_err("Error %d on %s\n", status, __func__);
1315
error2:
R
Ralph Metzler 已提交
1316 1317 1318 1319 1320
	mutex_unlock(&state->mutex);
	return status;
}


1321 1322
static int download_microcode(struct drxk_state *state,
			     const u8 p_mc_image[], u32 length)
R
Ralph Metzler 已提交
1323
{
1324 1325 1326 1327
	const u8 *p_src = p_mc_image;
	u32 address;
	u16 n_blocks;
	u16 block_size;
R
Ralph Metzler 已提交
1328 1329
	u32 offset = 0;
	u32 i;
1330
	int status = 0;
R
Ralph Metzler 已提交
1331

1332 1333
	dprintk(1, "\n");

1334 1335 1336
	/* down the drain (we don't care about MAGIC_WORD) */
#if 0
	/* For future reference */
1337
	drain = (p_src[0] << 8) | p_src[1];
1338
#endif
1339
	p_src += sizeof(u16);
1340
	offset += sizeof(u16);
1341 1342
	n_blocks = (p_src[0] << 8) | p_src[1];
	p_src += sizeof(u16);
1343
	offset += sizeof(u16);
R
Ralph Metzler 已提交
1344

1345 1346 1347 1348
	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);
1349
		offset += sizeof(u32);
R
Ralph Metzler 已提交
1350

1351 1352
		block_size = ((p_src[0] << 8) | p_src[1]) * sizeof(u16);
		p_src += sizeof(u16);
1353
		offset += sizeof(u16);
R
Ralph Metzler 已提交
1354

1355 1356
#if 0
		/* For future reference */
1357
		flags = (p_src[0] << 8) | p_src[1];
1358
#endif
1359
		p_src += sizeof(u16);
1360
		offset += sizeof(u16);
R
Ralph Metzler 已提交
1361

1362 1363
#if 0
		/* For future reference */
1364
		block_crc = (p_src[0] << 8) | p_src[1];
1365
#endif
1366
		p_src += sizeof(u16);
1367
		offset += sizeof(u16);
1368

1369
		if (offset + block_size > length) {
1370
			pr_err("Firmware is corrupted.\n");
1371 1372 1373
			return -EINVAL;
		}

1374
		status = write_block(state, address, block_size, p_src);
1375
		if (status < 0) {
1376
			pr_err("Error %d while loading firmware\n", status);
R
Ralph Metzler 已提交
1377
			break;
1378
		}
1379 1380
		p_src += block_size;
		offset += block_size;
R
Ralph Metzler 已提交
1381 1382 1383 1384
	}
	return status;
}

1385
static int dvbt_enable_ofdm_token_ring(struct drxk_state *state, bool enable)
R
Ralph Metzler 已提交
1386 1387
{
	int status;
1388
	u16 data = 0;
1389 1390
	u16 desired_ctrl = SIO_OFDM_SH_OFDM_RING_ENABLE_ON;
	u16 desired_status = SIO_OFDM_SH_OFDM_RING_STATUS_ENABLED;
R
Ralph Metzler 已提交
1391 1392
	unsigned long end;

1393 1394
	dprintk(1, "\n");

R
Ralph Metzler 已提交
1395
	if (enable == false) {
1396 1397
		desired_ctrl = SIO_OFDM_SH_OFDM_RING_ENABLE_OFF;
		desired_status = SIO_OFDM_SH_OFDM_RING_STATUS_DOWN;
R
Ralph Metzler 已提交
1398 1399
	}

1400
	status = read16(state, SIO_OFDM_SH_OFDM_RING_STATUS__A, &data);
1401
	if (status >= 0 && data == desired_status) {
R
Ralph Metzler 已提交
1402 1403 1404 1405
		/* tokenring already has correct status */
		return status;
	}
	/* Disable/enable dvbt tokenring bridge   */
1406
	status = write16(state, SIO_OFDM_SH_OFDM_RING_ENABLE__A, desired_ctrl);
R
Ralph Metzler 已提交
1407

1408
	end = jiffies + msecs_to_jiffies(DRXK_OFDM_TR_SHUTDOWN_TIMEOUT);
1409
	do {
1410
		status = read16(state, SIO_OFDM_SH_OFDM_RING_STATUS__A, &data);
1411 1412
		if ((status >= 0 && data == desired_status)
		    || time_is_after_jiffies(end))
1413
			break;
1414
		usleep_range(1000, 2000);
1415
	} while (1);
1416
	if (data != desired_status) {
1417
		pr_err("SIO not ready\n");
1418
		return -EINVAL;
R
Ralph Metzler 已提交
1419 1420 1421 1422
	}
	return status;
}

1423
static int mpegts_stop(struct drxk_state *state)
R
Ralph Metzler 已提交
1424 1425
{
	int status = 0;
1426 1427
	u16 fec_oc_snc_mode = 0;
	u16 fec_oc_ipr_mode = 0;
R
Ralph Metzler 已提交
1428

1429 1430
	dprintk(1, "\n");

1431
	/* Graceful shutdown (byte boundaries) */
1432
	status = read16(state, FEC_OC_SNC_MODE__A, &fec_oc_snc_mode);
1433 1434
	if (status < 0)
		goto error;
1435 1436
	fec_oc_snc_mode |= FEC_OC_SNC_MODE_SHUTDOWN__M;
	status = write16(state, FEC_OC_SNC_MODE__A, fec_oc_snc_mode);
1437 1438 1439 1440
	if (status < 0)
		goto error;

	/* Suppress MCLK during absence of data */
1441
	status = read16(state, FEC_OC_IPR_MODE__A, &fec_oc_ipr_mode);
1442 1443
	if (status < 0)
		goto error;
1444 1445
	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);
1446 1447 1448

error:
	if (status < 0)
1449
		pr_err("Error %d on %s\n", status, __func__);
R
Ralph Metzler 已提交
1450 1451 1452 1453 1454

	return status;
}

static int scu_command(struct drxk_state *state,
1455 1456
		       u16 cmd, u8 parameter_len,
		       u16 *parameter, u8 result_len, u16 *result)
R
Ralph Metzler 已提交
1457 1458 1459 1460
{
#if (SCU_RAM_PARAM_0__A - SCU_RAM_PARAM_15__A) != 15
#error DRXK register mapping no longer compatible with this routine!
#endif
1461
	u16 cur_cmd = 0;
1462
	int status = -EINVAL;
R
Ralph Metzler 已提交
1463
	unsigned long end;
1464 1465
	u8 buffer[34];
	int cnt = 0, ii;
1466 1467
	const char *p;
	char errname[30];
R
Ralph Metzler 已提交
1468

1469 1470
	dprintk(1, "\n");

1471 1472
	if ((cmd == 0) || ((parameter_len > 0) && (parameter == NULL)) ||
	    ((result_len > 0) && (result == NULL))) {
1473
		pr_err("Error %d on %s\n", status, __func__);
1474 1475
		return status;
	}
R
Ralph Metzler 已提交
1476 1477

	mutex_lock(&state->mutex);
1478 1479 1480

	/* assume that the command register is ready
		since it is checked afterwards */
1481
	for (ii = parameter_len - 1; ii >= 0; ii -= 1) {
1482 1483 1484 1485 1486 1487 1488
		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 -
1489
			(parameter_len - 1), cnt, buffer);
1490 1491
	/* Wait until SCU has processed command */
	end = jiffies + msecs_to_jiffies(DRXK_MAX_WAITTIME);
R
Ralph Metzler 已提交
1492
	do {
1493
		usleep_range(1000, 2000);
1494
		status = read16(state, SCU_RAM_COMMAND__A, &cur_cmd);
1495 1496
		if (status < 0)
			goto error;
1497 1498
	} while (!(cur_cmd == DRX_SCU_READY) && (time_is_after_jiffies(end)));
	if (cur_cmd != DRX_SCU_READY) {
1499
		pr_err("SCU not ready\n");
1500 1501 1502 1503
		status = -EIO;
		goto error2;
	}
	/* read results */
1504
	if ((result_len > 0) && (result != NULL)) {
1505 1506
		s16 err;
		int ii;
R
Ralph Metzler 已提交
1507

1508
		for (ii = result_len - 1; ii >= 0; ii -= 1) {
1509 1510
			status = read16(state, SCU_RAM_PARAM_0__A - ii,
					&result[ii]);
1511
			if (status < 0)
1512
				goto error;
R
Ralph Metzler 已提交
1513
		}
1514 1515 1516

		/* Check if an error was reported by SCU */
		err = (s16)result[0];
1517 1518
		if (err >= 0)
			goto error;
1519

1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536
		/* 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 已提交
1537
		}
1538
		pr_err("%s while sending cmd 0x%04x with params:", p, cmd);
1539 1540 1541
		print_hex_dump_bytes("drxk: ", DUMP_PREFIX_NONE, buffer, cnt);
		status = -EINVAL;
		goto error2;
1542 1543 1544
	}

error:
1545
	if (status < 0)
1546
		pr_err("Error %d on %s\n", status, __func__);
1547 1548
error2:
	mutex_unlock(&state->mutex);
R
Ralph Metzler 已提交
1549 1550 1551
	return status;
}

1552
static int set_iqm_af(struct drxk_state *state, bool active)
R
Ralph Metzler 已提交
1553 1554 1555 1556
{
	u16 data = 0;
	int status;

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

1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581
	/* 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)
1582
		pr_err("Error %d on %s\n", status, __func__);
R
Ralph Metzler 已提交
1583 1584 1585
	return status;
}

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

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

R
Ralph Metzler 已提交
1593 1594
	/* Check arguments */
	if (mode == NULL)
1595
		return -EINVAL;
R
Ralph Metzler 已提交
1596 1597 1598

	switch (*mode) {
	case DRX_POWER_UP:
1599
		sio_cc_pwd_mode = SIO_CC_PWD_MODE_LEVEL_NONE;
R
Ralph Metzler 已提交
1600 1601
		break;
	case DRXK_POWER_DOWN_OFDM:
1602
		sio_cc_pwd_mode = SIO_CC_PWD_MODE_LEVEL_OFDM;
R
Ralph Metzler 已提交
1603 1604
		break;
	case DRXK_POWER_DOWN_CORE:
1605
		sio_cc_pwd_mode = SIO_CC_PWD_MODE_LEVEL_CLOCK;
R
Ralph Metzler 已提交
1606 1607
		break;
	case DRXK_POWER_DOWN_PLL:
1608
		sio_cc_pwd_mode = SIO_CC_PWD_MODE_LEVEL_PLL;
R
Ralph Metzler 已提交
1609 1610
		break;
	case DRX_POWER_DOWN:
1611
		sio_cc_pwd_mode = SIO_CC_PWD_MODE_LEVEL_OSC;
R
Ralph Metzler 已提交
1612 1613 1614
		break;
	default:
		/* Unknow sleep mode */
1615
		return -EINVAL;
R
Ralph Metzler 已提交
1616 1617 1618
	}

	/* If already in requested power mode, do nothing */
1619
	if (state->m_current_power_mode == *mode)
R
Ralph Metzler 已提交
1620 1621 1622
		return 0;

	/* For next steps make sure to start from DRX_POWER_UP mode */
1623 1624
	if (state->m_current_power_mode != DRX_POWER_UP) {
		status = power_up_device(state);
1625 1626
		if (status < 0)
			goto error;
1627
		status = dvbt_enable_ofdm_token_ring(state, true);
1628 1629
		if (status < 0)
			goto error;
R
Ralph Metzler 已提交
1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643
	}

	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 */
1644
		switch (state->m_operation_mode) {
1645
		case OM_DVBT:
1646
			status = mpegts_stop(state);
1647
			if (status < 0)
1648
				goto error;
1649
			status = power_down_dvbt(state, false);
1650
			if (status < 0)
1651 1652 1653 1654
				goto error;
			break;
		case OM_QAM_ITU_A:
		case OM_QAM_ITU_C:
1655
			status = mpegts_stop(state);
1656
			if (status < 0)
1657
				goto error;
1658
			status = power_down_qam(state);
1659 1660 1661 1662 1663 1664
			if (status < 0)
				goto error;
			break;
		default:
			break;
		}
1665
		status = dvbt_enable_ofdm_token_ring(state, false);
1666 1667
		if (status < 0)
			goto error;
1668
		status = write16(state, SIO_CC_PWD_MODE__A, sio_cc_pwd_mode);
1669 1670 1671 1672 1673
		if (status < 0)
			goto error;
		status = write16(state, SIO_CC_UPDATE__A, SIO_CC_UPDATE_KEY);
		if (status < 0)
			goto error;
R
Ralph Metzler 已提交
1674

1675
		if (*mode != DRXK_POWER_DOWN_OFDM) {
1676
			state->m_hi_cfg_ctrl |=
1677
				SIO_HI_RA_RAM_PAR_5_CFG_SLEEP_ZZZ;
1678
			status = hi_cfg_command(state);
1679 1680 1681
			if (status < 0)
				goto error;
		}
R
Ralph Metzler 已提交
1682
	}
1683
	state->m_current_power_mode = *mode;
1684 1685 1686

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

1689
	return status;
R
Ralph Metzler 已提交
1690 1691
}

1692
static int power_down_dvbt(struct drxk_state *state, bool set_power_mode)
R
Ralph Metzler 已提交
1693
{
1694 1695
	enum drx_power_mode power_mode = DRXK_POWER_DOWN_OFDM;
	u16 cmd_result = 0;
R
Ralph Metzler 已提交
1696 1697 1698
	u16 data = 0;
	int status;

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

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

1721 1722 1723 1724 1725 1726 1727 1728 1729 1730
	/* 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 已提交
1731

1732
	/* powerdown AFE                   */
1733
	status = set_iqm_af(state, false);
1734 1735 1736 1737
	if (status < 0)
		goto error;

	/* powerdown to OFDM mode          */
1738 1739
	if (set_power_mode) {
		status = ctrl_power_mode(state, &power_mode);
1740 1741 1742 1743 1744
		if (status < 0)
			goto error;
	}
error:
	if (status < 0)
1745
		pr_err("Error %d on %s\n", status, __func__);
R
Ralph Metzler 已提交
1746 1747 1748
	return status;
}

1749 1750
static int setoperation_mode(struct drxk_state *state,
			    enum operation_mode o_mode)
R
Ralph Metzler 已提交
1751 1752 1753
{
	int status = 0;

1754
	dprintk(1, "\n");
R
Ralph Metzler 已提交
1755
	/*
1756 1757 1758 1759
	   Stop and power down previous standard
	   TODO investigate total power down instead of partial
	   power down depending on "previous" standard.
	 */
R
Ralph Metzler 已提交
1760

1761
	/* disable HW lock indicator */
1762 1763
	status = write16(state, SCU_RAM_GPIO__A,
			 SCU_RAM_GPIO_HW_LOCK_IND_DISABLE);
1764 1765
	if (status < 0)
		goto error;
R
Ralph Metzler 已提交
1766

1767
	/* Device is already at the required mode */
1768
	if (state->m_operation_mode == o_mode)
1769 1770
		return 0;

1771
	switch (state->m_operation_mode) {
1772 1773 1774 1775
		/* OM_NONE was added for start up */
	case OM_NONE:
		break;
	case OM_DVBT:
1776
		status = mpegts_stop(state);
1777
		if (status < 0)
1778
			goto error;
1779
		status = power_down_dvbt(state, true);
1780 1781
		if (status < 0)
			goto error;
1782
		state->m_operation_mode = OM_NONE;
1783 1784 1785
		break;
	case OM_QAM_ITU_A:	/* fallthrough */
	case OM_QAM_ITU_C:
1786
		status = mpegts_stop(state);
1787 1788
		if (status < 0)
			goto error;
1789
		status = power_down_qam(state);
1790 1791
		if (status < 0)
			goto error;
1792
		state->m_operation_mode = OM_NONE;
1793 1794 1795 1796 1797 1798
		break;
	case OM_QAM_ITU_B:
	default:
		status = -EINVAL;
		goto error;
	}
1799

1800 1801 1802
	/*
		Power up new standard
		*/
1803
	switch (o_mode) {
1804
	case OM_DVBT:
1805
		dprintk(1, ": DVB-T\n");
1806 1807
		state->m_operation_mode = o_mode;
		status = set_dvbt_standard(state, o_mode);
1808 1809 1810 1811 1812
		if (status < 0)
			goto error;
		break;
	case OM_QAM_ITU_A:	/* fallthrough */
	case OM_QAM_ITU_C:
1813
		dprintk(1, ": DVB-C Annex %c\n",
1814 1815 1816
			(state->m_operation_mode == OM_QAM_ITU_A) ? 'A' : 'C');
		state->m_operation_mode = o_mode;
		status = set_qam_standard(state, o_mode);
1817 1818 1819 1820 1821 1822
		if (status < 0)
			goto error;
		break;
	case OM_QAM_ITU_B:
	default:
		status = -EINVAL;
1823 1824 1825
	}
error:
	if (status < 0)
1826
		pr_err("Error %d on %s\n", status, __func__);
1827
	return status;
R
Ralph Metzler 已提交
1828 1829
}

1830 1831
static int start(struct drxk_state *state, s32 offset_freq,
		 s32 intermediate_frequency)
R
Ralph Metzler 已提交
1832
{
1833 1834
	int status = -EINVAL;

1835 1836
	u16 i_freqk_hz;
	s32 offsetk_hz = offset_freq / 1000;
R
Ralph Metzler 已提交
1837

1838
	dprintk(1, "\n");
1839 1840
	if (state->m_drxk_state != DRXK_STOPPED &&
		state->m_drxk_state != DRXK_DTV_STARTED)
1841
		goto error;
R
Ralph Metzler 已提交
1842

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

1845 1846 1847
	if (intermediate_frequency < 0) {
		state->m_b_mirror_freq_spect = !state->m_b_mirror_freq_spect;
		intermediate_frequency = -intermediate_frequency;
1848
	}
R
Ralph Metzler 已提交
1849

1850
	switch (state->m_operation_mode) {
1851 1852
	case OM_QAM_ITU_A:
	case OM_QAM_ITU_C:
1853 1854
		i_freqk_hz = (intermediate_frequency / 1000);
		status = set_qam(state, i_freqk_hz, offsetk_hz);
1855 1856
		if (status < 0)
			goto error;
1857
		state->m_drxk_state = DRXK_DTV_STARTED;
1858 1859
		break;
	case OM_DVBT:
1860 1861
		i_freqk_hz = (intermediate_frequency / 1000);
		status = mpegts_stop(state);
1862 1863
		if (status < 0)
			goto error;
1864
		status = set_dvbt(state, i_freqk_hz, offsetk_hz);
1865 1866
		if (status < 0)
			goto error;
1867
		status = dvbt_start(state);
1868 1869
		if (status < 0)
			goto error;
1870
		state->m_drxk_state = DRXK_DTV_STARTED;
1871 1872 1873 1874 1875 1876
		break;
	default:
		break;
	}
error:
	if (status < 0)
1877
		pr_err("Error %d on %s\n", status, __func__);
R
Ralph Metzler 已提交
1878 1879 1880
	return status;
}

1881
static int shut_down(struct drxk_state *state)
R
Ralph Metzler 已提交
1882
{
1883 1884
	dprintk(1, "\n");

1885
	mpegts_stop(state);
R
Ralph Metzler 已提交
1886 1887 1888
	return 0;
}

1889
static int get_lock_status(struct drxk_state *state, u32 *p_lock_status)
R
Ralph Metzler 已提交
1890
{
1891
	int status = -EINVAL;
R
Ralph Metzler 已提交
1892

1893 1894
	dprintk(1, "\n");

1895
	if (p_lock_status == NULL)
1896
		goto error;
R
Ralph Metzler 已提交
1897

1898
	*p_lock_status = NOT_LOCKED;
R
Ralph Metzler 已提交
1899 1900

	/* define the SCU command code */
1901
	switch (state->m_operation_mode) {
R
Ralph Metzler 已提交
1902 1903 1904
	case OM_QAM_ITU_A:
	case OM_QAM_ITU_B:
	case OM_QAM_ITU_C:
1905
		status = get_qam_lock_status(state, p_lock_status);
R
Ralph Metzler 已提交
1906 1907
		break;
	case OM_DVBT:
1908
		status = get_dvbt_lock_status(state, p_lock_status);
R
Ralph Metzler 已提交
1909 1910 1911 1912
		break;
	default:
		break;
	}
1913 1914
error:
	if (status < 0)
1915
		pr_err("Error %d on %s\n", status, __func__);
R
Ralph Metzler 已提交
1916 1917 1918
	return status;
}

1919
static int mpegts_start(struct drxk_state *state)
R
Ralph Metzler 已提交
1920
{
1921
	int status;
R
Ralph Metzler 已提交
1922

1923
	u16 fec_oc_snc_mode = 0;
R
Ralph Metzler 已提交
1924

1925
	/* Allow OC to sync again */
1926
	status = read16(state, FEC_OC_SNC_MODE__A, &fec_oc_snc_mode);
1927 1928
	if (status < 0)
		goto error;
1929 1930
	fec_oc_snc_mode &= ~FEC_OC_SNC_MODE_SHUTDOWN__M;
	status = write16(state, FEC_OC_SNC_MODE__A, fec_oc_snc_mode);
1931 1932 1933 1934 1935
	if (status < 0)
		goto error;
	status = write16(state, FEC_OC_SNC_UNLOCK__A, 1);
error:
	if (status < 0)
1936
		pr_err("Error %d on %s\n", status, __func__);
R
Ralph Metzler 已提交
1937 1938 1939
	return status;
}

1940
static int mpegts_dto_init(struct drxk_state *state)
R
Ralph Metzler 已提交
1941
{
1942
	int status;
R
Ralph Metzler 已提交
1943

1944 1945
	dprintk(1, "\n");

1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981
	/* 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)
1982
		pr_err("Error %d on %s\n", status, __func__);
R
Ralph Metzler 已提交
1983 1984 1985 1986

	return status;
}

1987 1988
static int mpegts_dto_setup(struct drxk_state *state,
			  enum operation_mode o_mode)
R
Ralph Metzler 已提交
1989
{
1990
	int status;
R
Ralph Metzler 已提交
1991

1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002
	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 已提交
2003

2004 2005
	dprintk(1, "\n");

2006
	/* Check insertion of the Reed-Solomon parity bytes */
2007
	status = read16(state, FEC_OC_MODE__A, &fec_oc_reg_mode);
2008 2009
	if (status < 0)
		goto error;
2010
	status = read16(state, FEC_OC_IPR_MODE__A, &fec_oc_reg_ipr_mode);
2011 2012
	if (status < 0)
		goto error;
2013 2014 2015
	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) {
2016
		/* enable parity symbol forward */
2017
		fec_oc_reg_mode |= FEC_OC_MODE_PARITY__M;
2018
		/* MVAL disable during parity bytes */
2019
		fec_oc_reg_ipr_mode |= FEC_OC_IPR_MODE_MVAL_DIS_PAR__M;
2020
		/* TS burst length to 204 */
2021
		fec_oc_dto_burst_len = 204;
2022
	}
R
Ralph Metzler 已提交
2023

2024
	/* Check serial or parallel output */
2025 2026
	fec_oc_reg_ipr_mode &= (~(FEC_OC_IPR_MODE_SERIAL__M));
	if (state->m_enable_parallel == false) {
2027
		/* MPEG data output is serial -> set ipr_mode[0] */
2028
		fec_oc_reg_ipr_mode |= FEC_OC_IPR_MODE_SERIAL__M;
2029
	}
R
Ralph Metzler 已提交
2030

2031
	switch (o_mode) {
2032
	case OM_DVBT:
2033 2034 2035 2036
		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;
2037 2038 2039
		break;
	case OM_QAM_ITU_A:	/* fallthrough */
	case OM_QAM_ITU_C:
2040 2041 2042 2043
		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;
2044 2045 2046 2047 2048 2049 2050 2051
		break;
	default:
		status = -EINVAL;
	}		/* switch (standard) */
	if (status < 0)
		goto error;

	/* Configure DTO's */
2052 2053
	if (static_clk) {
		u32 bit_rate = 0;
2054 2055 2056 2057 2058

		/* 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 */
2059
		fec_oc_dto_mode = (FEC_OC_DTO_MODE_DYNAMIC__M |
2060
				FEC_OC_DTO_MODE_OFFSET_ENABLE__M);
2061
		fec_oc_fct_mode = (FEC_OC_FCT_MODE_RAT_ENA__M |
2062 2063 2064
				FEC_OC_FCT_MODE_VIRT_ENA__M);

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

2072
			result should be floored,
2073 2074
			to make sure >= requested bitrate
			*/
2075 2076 2077 2078
		fec_oc_dto_period = (u16) (((state->m_sys_clock_freq)
						* 1000) / bit_rate);
		if (fec_oc_dto_period <= 2)
			fec_oc_dto_period = 0;
2079
		else
2080 2081
			fec_oc_dto_period -= 2;
		fec_oc_tmd_int_upd_rate = 8;
2082
	} else {
2083 2084 2085 2086
		/* (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;
2087
	}
R
Ralph Metzler 已提交
2088

2089
	/* Write appropriate registers with requested configuration */
2090
	status = write16(state, FEC_OC_DTO_BURST_LEN__A, fec_oc_dto_burst_len);
2091 2092
	if (status < 0)
		goto error;
2093
	status = write16(state, FEC_OC_DTO_PERIOD__A, fec_oc_dto_period);
2094 2095
	if (status < 0)
		goto error;
2096
	status = write16(state, FEC_OC_DTO_MODE__A, fec_oc_dto_mode);
2097 2098
	if (status < 0)
		goto error;
2099
	status = write16(state, FEC_OC_FCT_MODE__A, fec_oc_fct_mode);
2100 2101
	if (status < 0)
		goto error;
2102
	status = write16(state, FEC_OC_MODE__A, fec_oc_reg_mode);
2103 2104
	if (status < 0)
		goto error;
2105
	status = write16(state, FEC_OC_IPR_MODE__A, fec_oc_reg_ipr_mode);
2106 2107
	if (status < 0)
		goto error;
R
Ralph Metzler 已提交
2108

2109
	/* Rate integration settings */
2110
	status = write32(state, FEC_OC_RCN_CTL_RATE_LO__A, fec_oc_rcn_ctl_rate);
2111 2112
	if (status < 0)
		goto error;
2113 2114
	status = write16(state, FEC_OC_TMD_INT_UPD_RATE__A,
			 fec_oc_tmd_int_upd_rate);
2115 2116
	if (status < 0)
		goto error;
2117
	status = write16(state, FEC_OC_TMD_MODE__A, fec_oc_tmd_mode);
2118 2119
error:
	if (status < 0)
2120
		pr_err("Error %d on %s\n", status, __func__);
R
Ralph Metzler 已提交
2121 2122 2123
	return status;
}

2124
static int mpegts_configure_polarity(struct drxk_state *state)
R
Ralph Metzler 已提交
2125
{
2126
	u16 fec_oc_reg_ipr_invert = 0;
R
Ralph Metzler 已提交
2127 2128

	/* Data mask for the output data byte */
2129
	u16 invert_data_mask =
2130 2131 2132 2133
	    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 已提交
2134

2135 2136
	dprintk(1, "\n");

R
Ralph Metzler 已提交
2137
	/* Control selective inversion of output bits */
2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154
	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 已提交
2155 2156 2157 2158
}

#define   SCU_RAM_AGC_KI_INV_RF_POL__M 0x4000

2159 2160
static int set_agc_rf(struct drxk_state *state,
		    struct s_cfg_agc *p_agc_cfg, bool is_dtv)
R
Ralph Metzler 已提交
2161
{
2162 2163
	int status = -EINVAL;
	u16 data = 0;
2164
	struct s_cfg_agc *p_if_agc_settings;
R
Ralph Metzler 已提交
2165

2166 2167
	dprintk(1, "\n");

2168
	if (p_agc_cfg == NULL)
2169
		goto error;
R
Ralph Metzler 已提交
2170

2171
	switch (p_agc_cfg->ctrl_mode) {
2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183
	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 已提交
2184

2185 2186
		/* Enable SCU RF AGC loop */
		data &= ~SCU_RAM_AGC_CONFIG_DISABLE_RF_AGC__M;
R
Ralph Metzler 已提交
2187

2188
		/* Polarity */
2189
		if (state->m_rf_agc_pol)
2190 2191 2192 2193 2194 2195
			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 已提交
2196

2197 2198 2199 2200
		/* Set speed (using complementary reduction value) */
		status = read16(state, SCU_RAM_AGC_KI_RED__A, &data);
		if (status < 0)
			goto error;
R
Ralph Metzler 已提交
2201

2202
		data &= ~SCU_RAM_AGC_KI_RED_RAGC_RED__M;
2203
		data |= (~(p_agc_cfg->speed <<
2204 2205
				SCU_RAM_AGC_KI_RED_RAGC_RED__B)
				& SCU_RAM_AGC_KI_RED_RAGC_RED__M);
R
Ralph Metzler 已提交
2206

2207 2208 2209
		status = write16(state, SCU_RAM_AGC_KI_RED__A, data);
		if (status < 0)
			goto error;
R
Ralph Metzler 已提交
2210

2211 2212 2213 2214
		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;
2215
		else
2216 2217
			p_if_agc_settings = &state->m_atv_if_agc_cfg;
		if (p_if_agc_settings == NULL) {
2218 2219 2220
			status = -EINVAL;
			goto error;
		}
R
Ralph Metzler 已提交
2221

2222
		/* Set TOP, only if IF-AGC is in AUTO mode */
2223
		if (p_if_agc_settings->ctrl_mode == DRXK_AGC_CTRL_AUTO)
2224 2225 2226
			status = write16(state,
					 SCU_RAM_AGC_IF_IACCU_HI_TGT_MAX__A,
					 p_agc_cfg->top);
2227
			if (status < 0)
2228
				goto error;
R
Ralph Metzler 已提交
2229

2230
		/* Cut-Off current */
2231 2232
		status = write16(state, SCU_RAM_AGC_RF_IACCU_HI_CO__A,
				 p_agc_cfg->cut_off_current);
2233 2234
		if (status < 0)
			goto error;
R
Ralph Metzler 已提交
2235

2236
		/* Max. output level */
2237 2238
		status = write16(state, SCU_RAM_AGC_RF_MAX__A,
				 p_agc_cfg->max_output_level);
2239 2240
		if (status < 0)
			goto error;
R
Ralph Metzler 已提交
2241

2242
		break;
R
Ralph Metzler 已提交
2243

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

2254 2255 2256 2257 2258
		/* 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;
2259
		if (state->m_rf_agc_pol)
2260 2261 2262 2263 2264 2265
			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 已提交
2266

2267 2268 2269 2270
		/* 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 已提交
2271

2272
		/* Write value to output pin */
2273 2274
		status = write16(state, SCU_RAM_AGC_RF_IACCU_HI__A,
				 p_agc_cfg->output_level);
2275 2276 2277
		if (status < 0)
			goto error;
		break;
R
Ralph Metzler 已提交
2278

2279 2280 2281 2282 2283 2284 2285 2286 2287
	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 已提交
2288

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

2299 2300
	default:
		status = -EINVAL;
R
Ralph Metzler 已提交
2301

2302 2303 2304
	}
error:
	if (status < 0)
2305
		pr_err("Error %d on %s\n", status, __func__);
R
Ralph Metzler 已提交
2306 2307 2308 2309 2310
	return status;
}

#define SCU_RAM_AGC_KI_INV_IF_POL__M 0x2000

2311 2312
static int set_agc_if(struct drxk_state *state,
		    struct s_cfg_agc *p_agc_cfg, bool is_dtv)
R
Ralph Metzler 已提交
2313 2314 2315
{
	u16 data = 0;
	int status = 0;
2316
	struct s_cfg_agc *p_rf_agc_settings;
R
Ralph Metzler 已提交
2317

2318 2319
	dprintk(1, "\n");

2320
	switch (p_agc_cfg->ctrl_mode) {
2321
	case DRXK_AGC_CTRL_AUTO:
R
Ralph Metzler 已提交
2322

2323 2324 2325 2326 2327 2328 2329 2330
		/* 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 已提交
2331

2332 2333 2334
		status = read16(state, SCU_RAM_AGC_CONFIG__A, &data);
		if (status < 0)
			goto error;
R
Ralph Metzler 已提交
2335

2336 2337
		/* Enable SCU IF AGC loop */
		data &= ~SCU_RAM_AGC_CONFIG_DISABLE_IF_AGC__M;
R
Ralph Metzler 已提交
2338

2339
		/* Polarity */
2340
		if (state->m_if_agc_pol)
2341 2342 2343 2344 2345 2346
			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 已提交
2347

2348 2349 2350 2351 2352
		/* 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;
2353
		data |= (~(p_agc_cfg->speed <<
2354 2355
				SCU_RAM_AGC_KI_RED_IAGC_RED__B)
				& SCU_RAM_AGC_KI_RED_IAGC_RED__M);
R
Ralph Metzler 已提交
2356

2357 2358 2359
		status = write16(state, SCU_RAM_AGC_KI_RED__A, data);
		if (status < 0)
			goto error;
R
Ralph Metzler 已提交
2360

2361 2362
		if (is_qam(state))
			p_rf_agc_settings = &state->m_qam_rf_agc_cfg;
2363
		else
2364 2365
			p_rf_agc_settings = &state->m_atv_rf_agc_cfg;
		if (p_rf_agc_settings == NULL)
2366 2367
			return -1;
		/* Restore TOP */
2368 2369
		status = write16(state, SCU_RAM_AGC_IF_IACCU_HI_TGT_MAX__A,
				 p_rf_agc_settings->top);
2370 2371 2372
		if (status < 0)
			goto error;
		break;
R
Ralph Metzler 已提交
2373

2374
	case DRXK_AGC_CTRL_USER:
R
Ralph Metzler 已提交
2375

2376 2377 2378 2379 2380 2381 2382 2383
		/* 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 已提交
2384

2385 2386 2387
		status = read16(state, SCU_RAM_AGC_CONFIG__A, &data);
		if (status < 0)
			goto error;
R
Ralph Metzler 已提交
2388

2389 2390
		/* Disable SCU IF AGC loop */
		data |= SCU_RAM_AGC_CONFIG_DISABLE_IF_AGC__M;
R
Ralph Metzler 已提交
2391

2392
		/* Polarity */
2393
		if (state->m_if_agc_pol)
2394 2395 2396 2397 2398 2399
			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 已提交
2400

2401
		/* Write value to output pin */
2402 2403
		status = write16(state, SCU_RAM_AGC_IF_IACCU_HI_TGT_MAX__A,
				 p_agc_cfg->output_level);
2404 2405 2406
		if (status < 0)
			goto error;
		break;
R
Ralph Metzler 已提交
2407

2408
	case DRXK_AGC_CTRL_OFF:
R
Ralph Metzler 已提交
2409

2410 2411
		/* Disable If AGC DAC */
		status = read16(state, IQM_AF_STDBY__A, &data);
2412
		if (status < 0)
2413 2414 2415 2416 2417
			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 已提交
2418

2419 2420 2421 2422 2423 2424 2425 2426 2427
		/* 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;
2428
	}		/* switch (agcSettingsIf->ctrl_mode) */
R
Ralph Metzler 已提交
2429

2430 2431
	/* always set the top to support
		configurations without if-loop */
2432
	status = write16(state, SCU_RAM_AGC_INGAIN_TGT_MIN__A, p_agc_cfg->top);
2433 2434
error:
	if (status < 0)
2435
		pr_err("Error %d on %s\n", status, __func__);
R
Ralph Metzler 已提交
2436 2437 2438
	return status;
}

2439 2440
static int get_qam_signal_to_noise(struct drxk_state *state,
			       s32 *p_signal_to_noise)
R
Ralph Metzler 已提交
2441 2442
{
	int status = 0;
2443
	u16 qam_sl_err_power = 0;	/* accum. error between
2444
					raw and sliced symbols */
2445
	u32 qam_sl_sig_power = 0;	/* used for MER, depends of
2446
					QAM modulation */
2447
	u32 qam_sl_mer = 0;	/* QAM MER */
R
Ralph Metzler 已提交
2448

2449 2450
	dprintk(1, "\n");

2451
	/* MER calculation */
R
Ralph Metzler 已提交
2452

2453
	/* get the register value needed for MER */
2454
	status = read16(state, QAM_SL_ERR_POWER__A, &qam_sl_err_power);
2455
	if (status < 0) {
2456
		pr_err("Error %d on %s\n", status, __func__);
2457 2458 2459
		return -EINVAL;
	}

2460
	switch (state->props.modulation) {
2461
	case QAM_16:
2462
		qam_sl_sig_power = DRXK_QAM_SL_SIG_POWER_QAM16 << 2;
2463 2464
		break;
	case QAM_32:
2465
		qam_sl_sig_power = DRXK_QAM_SL_SIG_POWER_QAM32 << 2;
2466 2467
		break;
	case QAM_64:
2468
		qam_sl_sig_power = DRXK_QAM_SL_SIG_POWER_QAM64 << 2;
2469 2470
		break;
	case QAM_128:
2471
		qam_sl_sig_power = DRXK_QAM_SL_SIG_POWER_QAM128 << 2;
2472 2473 2474
		break;
	default:
	case QAM_256:
2475
		qam_sl_sig_power = DRXK_QAM_SL_SIG_POWER_QAM256 << 2;
2476 2477 2478
		break;
	}

2479 2480 2481
	if (qam_sl_err_power > 0) {
		qam_sl_mer = log10times100(qam_sl_sig_power) -
			log10times100((u32) qam_sl_err_power);
2482
	}
2483
	*p_signal_to_noise = qam_sl_mer;
R
Ralph Metzler 已提交
2484 2485 2486 2487

	return status;
}

2488 2489
static int get_dvbt_signal_to_noise(struct drxk_state *state,
				s32 *p_signal_to_noise)
R
Ralph Metzler 已提交
2490
{
2491
	int status;
2492 2493 2494 2495 2496 2497 2498 2499
	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;
2500 2501 2502
	u32 a = 0;
	u32 b = 0;
	u32 c = 0;
2503 2504
	u32 i_mer = 0;
	u16 transmission_params = 0;
R
Ralph Metzler 已提交
2505

2506
	dprintk(1, "\n");
R
Ralph Metzler 已提交
2507

2508 2509
	status = read16(state, OFDM_EQ_TOP_TD_TPS_PWR_OFS__A,
			&eq_reg_td_tps_pwr_ofs);
2510 2511
	if (status < 0)
		goto error;
2512 2513
	status = read16(state, OFDM_EQ_TOP_TD_REQ_SMB_CNT__A,
			&eq_reg_td_req_smb_cnt);
2514 2515
	if (status < 0)
		goto error;
2516 2517
	status = read16(state, OFDM_EQ_TOP_TD_SQR_ERR_EXP__A,
			&eq_reg_td_sqr_err_exp);
2518 2519
	if (status < 0)
		goto error;
2520 2521
	status = read16(state, OFDM_EQ_TOP_TD_SQR_ERR_I__A,
			&reg_data);
2522 2523 2524
	if (status < 0)
		goto error;
	/* Extend SQR_ERR_I operational range */
2525 2526 2527 2528
	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;
2529
	}
2530
	status = read16(state, OFDM_EQ_TOP_TD_SQR_ERR_Q__A, &reg_data);
2531 2532 2533
	if (status < 0)
		goto error;
	/* Extend SQR_ERR_Q operational range */
2534 2535 2536 2537
	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;
2538

2539 2540
	status = read16(state, OFDM_SC_RA_RAM_OP_PARAM__A,
			&transmission_params);
2541 2542 2543 2544 2545 2546
	if (status < 0)
		goto error;

	/* Check input data for MER */

	/* MER calculation (in 0.1 dB) without math.h */
2547 2548 2549
	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) {
2550 2551 2552
		/* No error at all, this must be the HW reset value
			* Apparently no first measurement yet
			* Set MER to 0.0 */
2553
		i_mer = 0;
2554
	} else {
2555 2556 2557
		sqr_err_iq = (eq_reg_td_sqr_err_i + eq_reg_td_sqr_err_q) <<
			eq_reg_td_sqr_err_exp;
		if ((transmission_params &
2558 2559
			OFDM_SC_RA_RAM_OP_PARAM_MODE__M)
			== OFDM_SC_RA_RAM_OP_PARAM_MODE_2K)
2560
			tps_cnt = 17;
2561
		else
2562
			tps_cnt = 68;
2563 2564

		/* IMER = 100 * log10 (x)
2565 2566
			where x = (eq_reg_td_tps_pwr_ofs^2 *
			eq_reg_td_req_smb_cnt * tps_cnt)/sqr_err_iq
2567 2568

			=> IMER = a + b -c
2569 2570 2571
			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)
2572 2573 2574
			*/

		/* log(x) x = 9bits * 9bits->18 bits  */
2575 2576
		a = log10times100(eq_reg_td_tps_pwr_ofs *
					eq_reg_td_tps_pwr_ofs);
2577
		/* log(x) x = 16bits * 7bits->23 bits  */
2578
		b = log10times100(eq_reg_td_req_smb_cnt * tps_cnt);
2579
		/* log(x) x = (16bits + 16bits) << 15 ->32 bits  */
2580
		c = log10times100(sqr_err_iq);
2581

2582
		i_mer = a + b - c;
2583
	}
2584
	*p_signal_to_noise = i_mer;
R
Ralph Metzler 已提交
2585

2586 2587
error:
	if (status < 0)
2588
		pr_err("Error %d on %s\n", status, __func__);
R
Ralph Metzler 已提交
2589 2590 2591
	return status;
}

2592
static int get_signal_to_noise(struct drxk_state *state, s32 *p_signal_to_noise)
R
Ralph Metzler 已提交
2593
{
2594 2595
	dprintk(1, "\n");

2596 2597
	*p_signal_to_noise = 0;
	switch (state->m_operation_mode) {
R
Ralph Metzler 已提交
2598
	case OM_DVBT:
2599
		return get_dvbt_signal_to_noise(state, p_signal_to_noise);
R
Ralph Metzler 已提交
2600 2601
	case OM_QAM_ITU_A:
	case OM_QAM_ITU_C:
2602
		return get_qam_signal_to_noise(state, p_signal_to_noise);
R
Ralph Metzler 已提交
2603 2604 2605 2606 2607 2608 2609
	default:
		break;
	}
	return 0;
}

#if 0
2610
static int get_dvbt_quality(struct drxk_state *state, s32 *p_quality)
R
Ralph Metzler 已提交
2611 2612 2613 2614
{
	/* SNR Values for quasi errorfree reception rom Nordig 2.2 */
	int status = 0;

2615 2616
	dprintk(1, "\n");

2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633
	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 已提交
2634

2635
	*p_quality = 0;
R
Ralph Metzler 已提交
2636 2637

	do {
2638 2639 2640 2641 2642
		s32 signal_to_noise = 0;
		u16 constellation = 0;
		u16 code_rate = 0;
		u32 signal_to_noise_rel;
		u32 ber_quality;
R
Ralph Metzler 已提交
2643

2644
		status = get_dvbt_signal_to_noise(state, &signal_to_noise);
2645 2646
		if (status < 0)
			break;
2647 2648
		status = read16(state, OFDM_EQ_TOP_TD_TPS_CONST__A,
				&constellation);
2649 2650
		if (status < 0)
			break;
2651
		constellation &= OFDM_EQ_TOP_TD_TPS_CONST__M;
R
Ralph Metzler 已提交
2652

2653 2654
		status = read16(state, OFDM_EQ_TOP_TD_TPS_CODE_HP__A,
				&code_rate);
2655 2656
		if (status < 0)
			break;
2657
		code_rate &= OFDM_EQ_TOP_TD_TPS_CODE_HP__M;
R
Ralph Metzler 已提交
2658

2659 2660
		if (constellation > OFDM_EQ_TOP_TD_TPS_CONST_64QAM ||
		    code_rate > OFDM_EQ_TOP_TD_TPS_CODE_LP_7_8)
R
Ralph Metzler 已提交
2661
			break;
2662 2663 2664 2665 2666 2667 2668 2669 2670
		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 已提交
2671
		else
2672
			*p_quality = ber_quality;
2673
	} while (0);
R
Ralph Metzler 已提交
2674 2675 2676
	return 0;
};

2677
static int get_dvbc_quality(struct drxk_state *state, s32 *p_quality)
R
Ralph Metzler 已提交
2678 2679
{
	int status = 0;
2680
	*p_quality = 0;
R
Ralph Metzler 已提交
2681

2682 2683
	dprintk(1, "\n");

R
Ralph Metzler 已提交
2684
	do {
2685 2686 2687
		u32 signal_to_noise = 0;
		u32 ber_quality = 100;
		u32 signal_to_noise_rel = 0;
R
Ralph Metzler 已提交
2688

2689
		status = get_qam_signal_to_noise(state, &signal_to_noise);
2690 2691
		if (status < 0)
			break;
R
Ralph Metzler 已提交
2692

2693
		switch (state->props.modulation) {
R
Ralph Metzler 已提交
2694
		case QAM_16:
2695
			signal_to_noise_rel = signal_to_noise - 200;
R
Ralph Metzler 已提交
2696 2697
			break;
		case QAM_32:
2698
			signal_to_noise_rel = signal_to_noise - 230;
2699
			break;	/* Not in NorDig */
R
Ralph Metzler 已提交
2700
		case QAM_64:
2701
			signal_to_noise_rel = signal_to_noise - 260;
R
Ralph Metzler 已提交
2702 2703
			break;
		case QAM_128:
2704
			signal_to_noise_rel = signal_to_noise - 290;
R
Ralph Metzler 已提交
2705 2706 2707
			break;
		default:
		case QAM_256:
2708
			signal_to_noise_rel = signal_to_noise - 320;
R
Ralph Metzler 已提交
2709 2710 2711
			break;
		}

2712 2713 2714 2715 2716
		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 已提交
2717
		else
2718
			*p_quality = ber_quality;
2719
	} while (0);
R
Ralph Metzler 已提交
2720 2721 2722 2723

	return status;
}

2724
static int get_quality(struct drxk_state *state, s32 *p_quality)
R
Ralph Metzler 已提交
2725
{
2726 2727
	dprintk(1, "\n");

2728
	switch (state->m_operation_mode) {
2729
	case OM_DVBT:
2730
		return get_dvbt_quality(state, p_quality);
2731
	case OM_QAM_ITU_A:
2732
		return get_dvbc_quality(state, p_quality);
R
Ralph Metzler 已提交
2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753
	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)

2754
static int ConfigureI2CBridge(struct drxk_state *state, bool b_enable_bridge)
R
Ralph Metzler 已提交
2755
{
2756
	int status = -EINVAL;
R
Ralph Metzler 已提交
2757

2758 2759
	dprintk(1, "\n");

2760
	if (state->m_drxk_state == DRXK_UNINITIALIZED)
2761
		return 0;
2762
	if (state->m_drxk_state == DRXK_POWERED_DOWN)
2763
		goto error;
R
Ralph Metzler 已提交
2764

2765 2766
	if (state->no_i2c_bridge)
		return 0;
R
Ralph Metzler 已提交
2767

2768 2769
	status = write16(state, SIO_HI_RA_RAM_PAR_1__A,
			 SIO_HI_RA_RAM_PAR_1_PAR1_SEC_KEY);
2770 2771
	if (status < 0)
		goto error;
2772
	if (b_enable_bridge) {
2773 2774
		status = write16(state, SIO_HI_RA_RAM_PAR_2__A,
				 SIO_HI_RA_RAM_PAR_2_BRD_CFG_CLOSED);
2775
		if (status < 0)
2776 2777
			goto error;
	} else {
2778 2779
		status = write16(state, SIO_HI_RA_RAM_PAR_2__A,
				 SIO_HI_RA_RAM_PAR_2_BRD_CFG_OPEN);
2780 2781 2782 2783
		if (status < 0)
			goto error;
	}

2784
	status = hi_command(state, SIO_HI_RA_RAM_CMD_BRDCTRL, NULL);
2785 2786 2787

error:
	if (status < 0)
2788
		pr_err("Error %d on %s\n", status, __func__);
R
Ralph Metzler 已提交
2789 2790 2791
	return status;
}

2792 2793
static int set_pre_saw(struct drxk_state *state,
		     struct s_cfg_pre_saw *p_pre_saw_cfg)
R
Ralph Metzler 已提交
2794
{
2795
	int status = -EINVAL;
R
Ralph Metzler 已提交
2796

2797 2798
	dprintk(1, "\n");

2799 2800
	if ((p_pre_saw_cfg == NULL)
	    || (p_pre_saw_cfg->reference > IQM_AF_PDREF__M))
2801
		goto error;
R
Ralph Metzler 已提交
2802

2803
	status = write16(state, IQM_AF_PDREF__A, p_pre_saw_cfg->reference);
2804 2805
error:
	if (status < 0)
2806
		pr_err("Error %d on %s\n", status, __func__);
R
Ralph Metzler 已提交
2807 2808 2809
	return status;
}

2810 2811
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 已提交
2812
{
2813 2814 2815
	u16 bl_status = 0;
	u16 offset = (u16) ((target_addr >> 0) & 0x00FFFF);
	u16 blockbank = (u16) ((target_addr >> 16) & 0x000FFF);
2816
	int status;
R
Ralph Metzler 已提交
2817 2818
	unsigned long end;

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

R
Ralph Metzler 已提交
2821
	mutex_lock(&state->mutex);
2822 2823 2824 2825 2826 2827 2828 2829 2830
	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;
2831
	status = write16(state, SIO_BL_SRC_ADDR__A, rom_offset);
2832 2833
	if (status < 0)
		goto error;
2834
	status = write16(state, SIO_BL_SRC_LEN__A, nr_of_elements);
2835 2836 2837 2838 2839 2840
	if (status < 0)
		goto error;
	status = write16(state, SIO_BL_ENABLE__A, SIO_BL_ENABLE_ON);
	if (status < 0)
		goto error;

2841
	end = jiffies + msecs_to_jiffies(time_out);
R
Ralph Metzler 已提交
2842
	do {
2843
		status = read16(state, SIO_BL_STATUS__A, &bl_status);
2844
		if (status < 0)
2845
			goto error;
2846 2847
	} while ((bl_status == 0x1) && time_is_after_jiffies(end));
	if (bl_status == 0x1) {
2848
		pr_err("SIO not ready\n");
2849 2850 2851 2852 2853
		status = -EINVAL;
		goto error2;
	}
error:
	if (status < 0)
2854
		pr_err("Error %d on %s\n", status, __func__);
2855
error2:
R
Ralph Metzler 已提交
2856 2857 2858 2859 2860
	mutex_unlock(&state->mutex);
	return status;

}

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

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

2868
	/* start measurement */
2869 2870 2871 2872 2873 2874
	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 已提交
2875

2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894
	*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)
2895
		pr_err("Error %d on %s\n", status, __func__);
R
Ralph Metzler 已提交
2896 2897 2898
	return status;
}

2899
static int adc_synchronization(struct drxk_state *state)
R
Ralph Metzler 已提交
2900 2901 2902 2903
{
	u16 count = 0;
	int status;

2904 2905
	dprintk(1, "\n");

2906
	status = adc_sync_measurement(state, &count);
2907 2908
	if (status < 0)
		goto error;
R
Ralph Metzler 已提交
2909

2910
	if (count == 1) {
2911
		/* Try sampling on a different edge */
2912
		u16 clk_neg = 0;
R
Ralph Metzler 已提交
2913

2914
		status = read16(state, IQM_AF_CLKNEG__A, &clk_neg);
2915 2916
		if (status < 0)
			goto error;
2917
		if ((clk_neg & IQM_AF_CLKNEG_CLKNEGDATA__M) ==
2918
			IQM_AF_CLKNEG_CLKNEGDATA_CLK_ADC_DATA_POS) {
2919 2920
			clk_neg &= (~(IQM_AF_CLKNEG_CLKNEGDATA__M));
			clk_neg |=
2921 2922
				IQM_AF_CLKNEG_CLKNEGDATA_CLK_ADC_DATA_NEG;
		} else {
2923 2924
			clk_neg &= (~(IQM_AF_CLKNEG_CLKNEGDATA__M));
			clk_neg |=
2925
				IQM_AF_CLKNEG_CLKNEGDATA_CLK_ADC_DATA_POS;
R
Ralph Metzler 已提交
2926
		}
2927
		status = write16(state, IQM_AF_CLKNEG__A, clk_neg);
2928 2929
		if (status < 0)
			goto error;
2930
		status = adc_sync_measurement(state, &count);
2931 2932 2933
		if (status < 0)
			goto error;
	}
R
Ralph Metzler 已提交
2934

2935 2936 2937 2938
	if (count < 2)
		status = -EINVAL;
error:
	if (status < 0)
2939
		pr_err("Error %d on %s\n", status, __func__);
R
Ralph Metzler 已提交
2940 2941 2942
	return status;
}

2943 2944 2945
static int set_frequency_shifter(struct drxk_state *state,
			       u16 intermediate_freqk_hz,
			       s32 tuner_freq_offset, bool is_dtv)
R
Ralph Metzler 已提交
2946
{
2947 2948 2949 2950 2951 2952
	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 已提交
2953
	int status;
2954 2955 2956 2957
	u32 if_freq_actual;
	u32 sampling_frequency = (u32) (state->m_sys_clock_freq / 3);
	u32 frequency_shift;
	bool image_to_select;
R
Ralph Metzler 已提交
2958

2959 2960
	dprintk(1, "\n");

R
Ralph Metzler 已提交
2961
	/*
2962 2963 2964
	   Program frequency shifter
	   No need to account for mirroring on RF
	 */
2965 2966 2967 2968 2969
	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;
2970
		else
2971
			select_pos_image = false;
R
Ralph Metzler 已提交
2972
	}
2973
	if (tuner_mirror)
R
Ralph Metzler 已提交
2974
		/* tuner doesn't mirror */
2975 2976
		if_freq_actual = intermediate_freqk_hz +
		    rf_freq_residual + fm_frequency_shift;
R
Ralph Metzler 已提交
2977 2978
	else
		/* tuner mirrors */
2979 2980 2981
		if_freq_actual = intermediate_freqk_hz -
		    rf_freq_residual - fm_frequency_shift;
	if (if_freq_actual > sampling_frequency / 2) {
R
Ralph Metzler 已提交
2982
		/* adc mirrors */
2983 2984
		adc_freq = sampling_frequency - if_freq_actual;
		adc_flip = true;
R
Ralph Metzler 已提交
2985 2986
	} else {
		/* adc doesn't mirror */
2987 2988
		adc_freq = if_freq_actual;
		adc_flip = false;
R
Ralph Metzler 已提交
2989 2990
	}

2991 2992 2993 2994 2995
	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 已提交
2996

2997 2998
	if (image_to_select)
		state->m_iqm_fs_rate_ofs = ~state->m_iqm_fs_rate_ofs + 1;
R
Ralph Metzler 已提交
2999 3000

	/* Program frequency shifter with tuner offset compensation */
3001
	/* frequency_shift += tuner_freq_offset; TODO */
3002
	status = write32(state, IQM_FS_RATE_OFS_LO__A,
3003
			 state->m_iqm_fs_rate_ofs);
3004
	if (status < 0)
3005
		pr_err("Error %d on %s\n", status, __func__);
R
Ralph Metzler 已提交
3006 3007 3008
	return status;
}

3009
static int init_agc(struct drxk_state *state, bool is_dtv)
R
Ralph Metzler 已提交
3010
{
3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024
	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;
3025
	u16 data = 0;
3026 3027
	u16 fast_clp_ctrl_delay = 0;
	u16 clp_ctrl_mode = 0;
R
Ralph Metzler 已提交
3028 3029
	int status = 0;

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

3032
	/* Common settings */
3033 3034 3035 3036
	sns_sum_max = 1023;
	if_iaccu_hi_tgt_min = 2047;
	clp_cyclen = 500;
	clp_sum_max = 1023;
3037

3038
	/* AGCInit() not available for DVBT; init done in microcode */
3039
	if (!is_qam(state)) {
3040 3041
		pr_err("%s: mode %d is not DVB-C\n",
		       __func__, state->m_operation_mode);
3042
		return -EINVAL;
3043
	}
3044 3045 3046 3047

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

	/* Standard specific settings */
3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059
	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;
3060

3061 3062
	status = write16(state, SCU_RAM_AGC_FAST_CLP_CTRL_DELAY__A,
			 fast_clp_ctrl_delay);
3063 3064
	if (status < 0)
		goto error;
3065

3066
	status = write16(state, SCU_RAM_AGC_CLP_CTRL_MODE__A, clp_ctrl_mode);
3067 3068
	if (status < 0)
		goto error;
3069
	status = write16(state, SCU_RAM_AGC_INGAIN_TGT__A, ingain_tgt);
3070 3071
	if (status < 0)
		goto error;
3072
	status = write16(state, SCU_RAM_AGC_INGAIN_TGT_MIN__A, ingain_tgt_min);
3073 3074
	if (status < 0)
		goto error;
3075
	status = write16(state, SCU_RAM_AGC_INGAIN_TGT_MAX__A, ingain_tgt_max);
3076 3077
	if (status < 0)
		goto error;
3078 3079
	status = write16(state, SCU_RAM_AGC_IF_IACCU_HI_TGT_MIN__A,
			 if_iaccu_hi_tgt_min);
3080 3081
	if (status < 0)
		goto error;
3082 3083
	status = write16(state, SCU_RAM_AGC_IF_IACCU_HI_TGT_MAX__A,
			 if_iaccu_hi_tgt_max);
3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097
	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;
3098
	status = write16(state, SCU_RAM_AGC_CLP_SUM_MAX__A, clp_sum_max);
3099 3100
	if (status < 0)
		goto error;
3101
	status = write16(state, SCU_RAM_AGC_SNS_SUM_MAX__A, sns_sum_max);
3102 3103
	if (status < 0)
		goto error;
3104

3105 3106
	status = write16(state, SCU_RAM_AGC_KI_INNERGAIN_MIN__A,
			 ki_innergain_min);
3107 3108
	if (status < 0)
		goto error;
3109 3110
	status = write16(state, SCU_RAM_AGC_IF_IACCU_HI_TGT__A,
			 if_iaccu_hi_tgt);
3111 3112
	if (status < 0)
		goto error;
3113
	status = write16(state, SCU_RAM_AGC_CLP_CYCLEN__A, clp_cyclen);
3114 3115
	if (status < 0)
		goto error;
3116

3117 3118 3119 3120 3121 3122 3123 3124 3125
	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;
3126

3127 3128 3129
	status = write16(state, SCU_RAM_AGC_KI_MAXMINGAIN_TH__A, 20);
	if (status < 0)
		goto error;
3130
	status = write16(state, SCU_RAM_AGC_CLP_SUM_MIN__A, clp_sum_min);
3131 3132
	if (status < 0)
		goto error;
3133
	status = write16(state, SCU_RAM_AGC_SNS_SUM_MIN__A, sns_sum_min);
3134 3135
	if (status < 0)
		goto error;
3136
	status = write16(state, SCU_RAM_AGC_CLP_DIR_TO__A, clp_dir_to);
3137 3138
	if (status < 0)
		goto error;
3139
	status = write16(state, SCU_RAM_AGC_SNS_DIR_TO__A, sns_dir_to);
3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183
	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 已提交
3184

3185 3186 3187 3188
	/* Initialize inner-loop KI gain factors */
	status = read16(state, SCU_RAM_AGC_KI__A, &data);
	if (status < 0)
		goto error;
3189 3190 3191 3192 3193 3194 3195

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

3196 3197 3198
	status = write16(state, SCU_RAM_AGC_KI__A, data);
error:
	if (status < 0)
3199
		pr_err("Error %d on %s\n", status, __func__);
R
Ralph Metzler 已提交
3200 3201 3202
	return status;
}

3203
static int dvbtqam_get_acc_pkt_err(struct drxk_state *state, u16 *packet_err)
R
Ralph Metzler 已提交
3204 3205 3206
{
	int status;

3207
	dprintk(1, "\n");
3208
	if (packet_err == NULL)
3209 3210
		status = write16(state, SCU_RAM_FEC_ACCUM_PKT_FAILURES__A, 0);
	else
3211 3212
		status = read16(state, SCU_RAM_FEC_ACCUM_PKT_FAILURES__A,
				packet_err);
3213
	if (status < 0)
3214
		pr_err("Error %d on %s\n", status, __func__);
R
Ralph Metzler 已提交
3215 3216 3217
	return status;
}

3218
static int dvbt_sc_command(struct drxk_state *state,
R
Ralph Metzler 已提交
3219 3220 3221 3222
			 u16 cmd, u16 subcmd,
			 u16 param0, u16 param1, u16 param2,
			 u16 param3, u16 param4)
{
3223 3224 3225 3226
	u16 cur_cmd = 0;
	u16 err_code = 0;
	u16 retry_cnt = 0;
	u16 sc_exec = 0;
3227
	int status;
R
Ralph Metzler 已提交
3228

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

	/* Wait until sc is ready to receive command */
3239
	retry_cnt = 0;
R
Ralph Metzler 已提交
3240
	do {
3241
		usleep_range(1000, 2000);
3242 3243 3244 3245
		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))
3246 3247
		goto error;

R
Ralph Metzler 已提交
3248 3249 3250 3251 3252 3253
	/* 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:
3254 3255 3256
		status = write16(state, OFDM_SC_RA_RAM_CMD_ADDR__A, subcmd);
		if (status < 0)
			goto error;
R
Ralph Metzler 已提交
3257 3258 3259 3260
		break;
	default:
		/* Do nothing */
		break;
3261
	}
R
Ralph Metzler 已提交
3262 3263 3264 3265 3266 3267 3268 3269 3270 3271

	/* 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:
3272
		status = write16(state, OFDM_SC_RA_RAM_PARAM1__A, param1);
R
Ralph Metzler 已提交
3273 3274 3275
		/* All commands using 1 parameters */
	case OFDM_SC_RA_RAM_CMD_SET_ECHO_TIMING:
	case OFDM_SC_RA_RAM_CMD_USER_IO:
3276
		status = write16(state, OFDM_SC_RA_RAM_PARAM0__A, param0);
R
Ralph Metzler 已提交
3277 3278 3279 3280
		/* All commands using 0 parameters */
	case OFDM_SC_RA_RAM_CMD_GET_OP_PARAM:
	case OFDM_SC_RA_RAM_CMD_NULL:
		/* Write command */
3281
		status = write16(state, OFDM_SC_RA_RAM_CMD__A, cmd);
R
Ralph Metzler 已提交
3282 3283 3284
		break;
	default:
		/* Unknown command */
3285 3286 3287 3288
		status = -EINVAL;
	}
	if (status < 0)
		goto error;
R
Ralph Metzler 已提交
3289 3290

	/* Wait until sc is ready processing command */
3291
	retry_cnt = 0;
3292
	do {
3293
		usleep_range(1000, 2000);
3294 3295 3296 3297
		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))
3298
		goto error;
R
Ralph Metzler 已提交
3299 3300

	/* Check for illegal cmd */
3301 3302
	status = read16(state, OFDM_SC_RA_RAM_CMD_ADDR__A, &err_code);
	if (err_code == 0xFFFF) {
R
Ralph Metzler 已提交
3303
		/* illegal command */
3304
		status = -EINVAL;
R
Ralph Metzler 已提交
3305
	}
3306 3307
	if (status < 0)
		goto error;
R
Ralph Metzler 已提交
3308

3309
	/* Retrieve results parameters from SC */
R
Ralph Metzler 已提交
3310 3311 3312 3313 3314 3315 3316 3317
	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:
3318
		status = read16(state, OFDM_SC_RA_RAM_PARAM0__A, &(param0));
R
Ralph Metzler 已提交
3319 3320 3321 3322 3323 3324 3325 3326 3327 3328
		/* 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 */
3329
		status = -EINVAL;
R
Ralph Metzler 已提交
3330
		break;
3331
	}			/* switch (cmd->cmd) */
3332 3333
error:
	if (status < 0)
3334
		pr_err("Error %d on %s\n", status, __func__);
R
Ralph Metzler 已提交
3335 3336 3337
	return status;
}

3338
static int power_up_dvbt(struct drxk_state *state)
R
Ralph Metzler 已提交
3339
{
3340
	enum drx_power_mode power_mode = DRX_POWER_UP;
R
Ralph Metzler 已提交
3341 3342
	int status;

3343
	dprintk(1, "\n");
3344
	status = ctrl_power_mode(state, &power_mode);
3345
	if (status < 0)
3346
		pr_err("Error %d on %s\n", status, __func__);
R
Ralph Metzler 已提交
3347 3348 3349
	return status;
}

3350
static int dvbt_ctrl_set_inc_enable(struct drxk_state *state, bool *enabled)
R
Ralph Metzler 已提交
3351
{
3352 3353
	int status;

3354
	dprintk(1, "\n");
3355
	if (*enabled == true)
3356
		status = write16(state, IQM_CF_BYPASSDET__A, 0);
3357
	else
3358
		status = write16(state, IQM_CF_BYPASSDET__A, 1);
3359
	if (status < 0)
3360
		pr_err("Error %d on %s\n", status, __func__);
3361
	return status;
R
Ralph Metzler 已提交
3362
}
3363 3364

#define DEFAULT_FR_THRES_8K     4000
3365
static int dvbt_ctrl_set_fr_enable(struct drxk_state *state, bool *enabled)
R
Ralph Metzler 已提交
3366 3367
{

3368 3369
	int status;

3370
	dprintk(1, "\n");
3371 3372
	if (*enabled == true) {
		/* write mask to 1 */
3373
		status = write16(state, OFDM_SC_RA_RAM_FR_THRES_8K__A,
3374 3375 3376
				   DEFAULT_FR_THRES_8K);
	} else {
		/* write mask to 0 */
3377
		status = write16(state, OFDM_SC_RA_RAM_FR_THRES_8K__A, 0);
3378
	}
3379
	if (status < 0)
3380
		pr_err("Error %d on %s\n", status, __func__);
3381 3382

	return status;
R
Ralph Metzler 已提交
3383 3384
}

3385
static int dvbt_ctrl_set_echo_threshold(struct drxk_state *state,
3386
				struct drxk_cfg_dvbt_echo_thres_t *echo_thres)
R
Ralph Metzler 已提交
3387
{
3388
	u16 data = 0;
R
Ralph Metzler 已提交
3389 3390
	int status;

3391
	dprintk(1, "\n");
3392 3393 3394 3395
	status = read16(state, OFDM_SC_RA_RAM_ECHO_THRES__A, &data);
	if (status < 0)
		goto error;

3396
	switch (echo_thres->fft_mode) {
3397 3398
	case DRX_FFTMODE_2K:
		data &= ~OFDM_SC_RA_RAM_ECHO_THRES_2K__M;
3399
		data |= ((echo_thres->threshold <<
3400 3401
			OFDM_SC_RA_RAM_ECHO_THRES_2K__B)
			& (OFDM_SC_RA_RAM_ECHO_THRES_2K__M));
3402
		break;
3403 3404
	case DRX_FFTMODE_8K:
		data &= ~OFDM_SC_RA_RAM_ECHO_THRES_8K__M;
3405
		data |= ((echo_thres->threshold <<
3406 3407
			OFDM_SC_RA_RAM_ECHO_THRES_8K__B)
			& (OFDM_SC_RA_RAM_ECHO_THRES_8K__M));
3408
		break;
3409 3410 3411
	default:
		return -EINVAL;
	}
3412

3413 3414 3415
	status = write16(state, OFDM_SC_RA_RAM_ECHO_THRES__A, data);
error:
	if (status < 0)
3416
		pr_err("Error %d on %s\n", status, __func__);
3417
	return status;
R
Ralph Metzler 已提交
3418 3419
}

3420 3421
static int dvbt_ctrl_set_sqi_speed(struct drxk_state *state,
			       enum drxk_cfg_dvbt_sqi_speed *speed)
R
Ralph Metzler 已提交
3422
{
3423
	int status = -EINVAL;
R
Ralph Metzler 已提交
3424

3425 3426
	dprintk(1, "\n");

R
Ralph Metzler 已提交
3427 3428 3429 3430 3431 3432
	switch (*speed) {
	case DRXK_DVBT_SQI_SPEED_FAST:
	case DRXK_DVBT_SQI_SPEED_MEDIUM:
	case DRXK_DVBT_SQI_SPEED_SLOW:
		break;
	default:
3433
		goto error;
R
Ralph Metzler 已提交
3434
	}
3435
	status = write16(state, SCU_RAM_FEC_PRE_RS_BER_FILTER_SH__A,
3436
			   (u16) *speed);
3437 3438
error:
	if (status < 0)
3439
		pr_err("Error %d on %s\n", status, __func__);
R
Ralph Metzler 已提交
3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452
	return status;
}

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

/**
* \brief Activate DVBT specific presets
* \param demod instance of demodulator.
* \return DRXStatus_t.
*
* Called in DVBTSetStandard
*
*/
3453
static int dvbt_activate_presets(struct drxk_state *state)
R
Ralph Metzler 已提交
3454
{
3455
	int status;
3456 3457
	bool setincenable = false;
	bool setfrenable = true;
3458

3459 3460
	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 };
3461

3462
	dprintk(1, "\n");
3463
	status = dvbt_ctrl_set_inc_enable(state, &setincenable);
3464 3465
	if (status < 0)
		goto error;
3466
	status = dvbt_ctrl_set_fr_enable(state, &setfrenable);
3467 3468
	if (status < 0)
		goto error;
3469
	status = dvbt_ctrl_set_echo_threshold(state, &echo_thres2k);
3470 3471
	if (status < 0)
		goto error;
3472
	status = dvbt_ctrl_set_echo_threshold(state, &echo_thres8k);
3473 3474
	if (status < 0)
		goto error;
3475 3476
	status = write16(state, SCU_RAM_AGC_INGAIN_TGT_MAX__A,
			 state->m_dvbt_if_agc_cfg.ingain_tgt_max);
3477 3478
error:
	if (status < 0)
3479
		pr_err("Error %d on %s\n", status, __func__);
3480
	return status;
R
Ralph Metzler 已提交
3481
}
3482

R
Ralph Metzler 已提交
3483 3484 3485 3486 3487 3488 3489 3490 3491 3492
/*============================================================================*/

/**
* \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.
*/
3493 3494
static int set_dvbt_standard(struct drxk_state *state,
			   enum operation_mode o_mode)
R
Ralph Metzler 已提交
3495
{
3496
	u16 cmd_result = 0;
3497 3498
	u16 data = 0;
	int status;
R
Ralph Metzler 已提交
3499

3500
	dprintk(1, "\n");
R
Ralph Metzler 已提交
3501

3502
	power_up_dvbt(state);
3503
	/* added antenna switch */
3504
	switch_antenna_to_dvbt(state);
3505
	/* send OFDM reset command */
3506 3507 3508 3509
	status = scu_command(state,
			     SCU_RAM_COMMAND_STANDARD_OFDM
			     | SCU_RAM_COMMAND_CMD_DEMOD_RESET,
			     0, NULL, 1, &cmd_result);
3510 3511
	if (status < 0)
		goto error;
R
Ralph Metzler 已提交
3512

3513
	/* send OFDM setenv command */
3514 3515 3516
	status = scu_command(state, SCU_RAM_COMMAND_STANDARD_OFDM
			     | SCU_RAM_COMMAND_CMD_DEMOD_SET_ENV,
			     0, NULL, 1, &cmd_result);
3517 3518
	if (status < 0)
		goto error;
R
Ralph Metzler 已提交
3519

3520 3521 3522 3523 3524 3525 3526 3527 3528 3529
	/* 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 已提交
3530

3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547
	/* 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;
3548
	status = set_iqm_af(state, true);
3549 3550
	if (status < 0)
		goto error;
R
Ralph Metzler 已提交
3551

3552 3553 3554
	status = write16(state, IQM_AF_AGC_RF__A, 0);
	if (status < 0)
		goto error;
R
Ralph Metzler 已提交
3555

3556 3557 3558 3559 3560 3561 3562 3563 3564 3565
	/* 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 已提交
3566

3567 3568 3569
	status = write16(state, IQM_RC_STRETCH__A, 16);
	if (status < 0)
		goto error;
3570
	status = write16(state, IQM_CF_OUT_ENA__A, 0x4); /* enable output 2 */
3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581
	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 已提交
3582

3583 3584 3585 3586 3587 3588 3589
	/* 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 已提交
3590

3591 3592
	status = bl_chain_cmd(state, DRXK_BL_ROM_OFFSET_TAPS_DVBT,
			      DRXK_BLCC_NR_ELEMENTS_TAPS, DRXK_BLC_TIMEOUT);
3593 3594
	if (status < 0)
		goto error;
R
Ralph Metzler 已提交
3595

3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608
	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 已提交
3609

3610
	/* IQM will not be reset from here, sync ADC and update/init AGC */
3611
	status = adc_synchronization(state);
3612 3613
	if (status < 0)
		goto error;
3614
	status = set_pre_saw(state, &state->m_dvbt_pre_saw_cfg);
3615 3616
	if (status < 0)
		goto error;
R
Ralph Metzler 已提交
3617

3618 3619 3620 3621
	/* 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 已提交
3622

3623
	status = set_agc_rf(state, &state->m_dvbt_rf_agc_cfg, true);
3624 3625
	if (status < 0)
		goto error;
3626
	status = set_agc_if(state, &state->m_dvbt_if_agc_cfg, true);
3627 3628
	if (status < 0)
		goto error;
R
Ralph Metzler 已提交
3629

3630 3631 3632 3633 3634 3635 3636 3637
	/* 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 已提交
3638

3639 3640 3641 3642
	/* 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 已提交
3643

3644 3645
	if (!state->m_drxk_a3_rom_code) {
		/* AGCInit() is not done for DVBT, so set agcfast_clip_ctrl_delay  */
3646 3647
		status = write16(state, SCU_RAM_AGC_FAST_CLP_CTRL_DELAY__A,
				 state->m_dvbt_if_agc_cfg.fast_clip_ctrl_delay);
3648 3649 3650
		if (status < 0)
			goto error;
	}
R
Ralph Metzler 已提交
3651

3652
	/* OFDM_SC setup */
R
Ralph Metzler 已提交
3653
#ifdef COMPILE_FOR_NONRT
3654 3655 3656 3657 3658 3659
	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 已提交
3660 3661
#endif

3662 3663 3664 3665
	/* FEC setup */
	status = write16(state, FEC_DI_INPUT_CTL__A, 1);	/* OFDM input */
	if (status < 0)
		goto error;
R
Ralph Metzler 已提交
3666 3667 3668


#ifdef COMPILE_FOR_NONRT
3669 3670 3671
	status = write16(state, FEC_RS_MEASUREMENT_PERIOD__A, 0x400);
	if (status < 0)
		goto error;
R
Ralph Metzler 已提交
3672
#else
3673 3674 3675
	status = write16(state, FEC_RS_MEASUREMENT_PERIOD__A, 0x1000);
	if (status < 0)
		goto error;
R
Ralph Metzler 已提交
3676
#endif
3677 3678 3679
	status = write16(state, FEC_RS_MEASUREMENT_PRESCALE__A, 0x0001);
	if (status < 0)
		goto error;
R
Ralph Metzler 已提交
3680

3681
	/* Setup MPEG bus */
3682
	status = mpegts_dto_setup(state, OM_DVBT);
3683
	if (status < 0)
3684 3685
		goto error;
	/* Set DVBT Presets */
3686
	status = dvbt_activate_presets(state);
3687 3688
	if (status < 0)
		goto error;
R
Ralph Metzler 已提交
3689

3690 3691
error:
	if (status < 0)
3692
		pr_err("Error %d on %s\n", status, __func__);
R
Ralph Metzler 已提交
3693 3694 3695 3696 3697
	return status;
}

/*============================================================================*/
/**
3698
* \brief start dvbt demodulating for channel.
R
Ralph Metzler 已提交
3699 3700 3701
* \param demod instance of demodulator.
* \return DRXStatus_t.
*/
3702
static int dvbt_start(struct drxk_state *state)
R
Ralph Metzler 已提交
3703
{
3704 3705
	u16 param1;
	int status;
3706
	/* drxk_ofdm_sc_cmd_t scCmd; */
3707

3708
	dprintk(1, "\n");
3709
	/* start correct processes to get in lock */
3710
	/* DRXK: OFDM_SC_RA_RAM_PROC_LOCKTRACK is no longer in mapfile! */
3711
	param1 = OFDM_SC_RA_RAM_LOCKTRACK_MIN;
3712 3713 3714
	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);
3715 3716
	if (status < 0)
		goto error;
3717 3718
	/* start FEC OC */
	status = mpegts_start(state);
3719 3720 3721 3722 3723 3724 3725
	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)
3726
		pr_err("Error %d on %s\n", status, __func__);
3727
	return status;
R
Ralph Metzler 已提交
3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738
}


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

/**
* \brief Set up dvbt demodulator for channel.
* \param demod instance of demodulator.
* \return DRXStatus_t.
* // original DVBTSetChannel()
*/
3739 3740
static int set_dvbt(struct drxk_state *state, u16 intermediate_freqk_hz,
		   s32 tuner_freq_offset)
R
Ralph Metzler 已提交
3741
{
3742 3743 3744 3745
	u16 cmd_result = 0;
	u16 transmission_params = 0;
	u16 operation_mode = 0;
	u32 iqm_rc_rate_ofs = 0;
3746 3747
	u32 bandwidth = 0;
	u16 param1;
R
Ralph Metzler 已提交
3748 3749
	int status;

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

3753 3754 3755
	status = scu_command(state, SCU_RAM_COMMAND_STANDARD_OFDM
			    | SCU_RAM_COMMAND_CMD_DEMOD_STOP,
			    0, NULL, 1, &cmd_result);
3756 3757
	if (status < 0)
		goto error;
R
Ralph Metzler 已提交
3758

3759 3760 3761 3762
	/* 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 已提交
3763

3764 3765 3766 3767 3768 3769 3770
	/* 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 已提交
3771

3772 3773 3774 3775 3776
	/* 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 已提交
3777

3778
	/*== Write channel settings to device ================================*/
R
Ralph Metzler 已提交
3779

3780
	/* mode */
3781
	switch (state->props.transmission_mode) {
3782 3783
	case TRANSMISSION_MODE_AUTO:
	default:
3784
		operation_mode |= OFDM_SC_RA_RAM_OP_AUTO_MODE__M;
3785 3786
		/* fall through , try first guess DRX_FFTMODE_8K */
	case TRANSMISSION_MODE_8K:
3787
		transmission_params |= OFDM_SC_RA_RAM_OP_PARAM_MODE_8K;
3788
		break;
3789
	case TRANSMISSION_MODE_2K:
3790
		transmission_params |= OFDM_SC_RA_RAM_OP_PARAM_MODE_2K;
3791
		break;
3792
	}
R
Ralph Metzler 已提交
3793

3794
	/* guard */
3795
	switch (state->props.guard_interval) {
3796 3797
	default:
	case GUARD_INTERVAL_AUTO:
3798
		operation_mode |= OFDM_SC_RA_RAM_OP_AUTO_GUARD__M;
3799 3800
		/* fall through , try first guess DRX_GUARD_1DIV4 */
	case GUARD_INTERVAL_1_4:
3801
		transmission_params |= OFDM_SC_RA_RAM_OP_PARAM_GUARD_4;
3802
		break;
3803
	case GUARD_INTERVAL_1_32:
3804
		transmission_params |= OFDM_SC_RA_RAM_OP_PARAM_GUARD_32;
3805
		break;
3806
	case GUARD_INTERVAL_1_16:
3807
		transmission_params |= OFDM_SC_RA_RAM_OP_PARAM_GUARD_16;
3808
		break;
3809
	case GUARD_INTERVAL_1_8:
3810
		transmission_params |= OFDM_SC_RA_RAM_OP_PARAM_GUARD_8;
3811
		break;
3812
	}
R
Ralph Metzler 已提交
3813

3814
	/* hierarchy */
3815
	switch (state->props.hierarchy) {
3816 3817 3818
	case HIERARCHY_AUTO:
	case HIERARCHY_NONE:
	default:
3819
		operation_mode |= OFDM_SC_RA_RAM_OP_AUTO_HIER__M;
3820
		/* fall through , try first guess SC_RA_RAM_OP_PARAM_HIER_NO */
3821
		/* transmission_params |= OFDM_SC_RA_RAM_OP_PARAM_HIER_NO; */
3822 3823
		/* break; */
	case HIERARCHY_1:
3824
		transmission_params |= OFDM_SC_RA_RAM_OP_PARAM_HIER_A1;
3825 3826
		break;
	case HIERARCHY_2:
3827
		transmission_params |= OFDM_SC_RA_RAM_OP_PARAM_HIER_A2;
3828 3829
		break;
	case HIERARCHY_4:
3830
		transmission_params |= OFDM_SC_RA_RAM_OP_PARAM_HIER_A4;
3831 3832 3833 3834
		break;
	}


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

3878
	/* coderate */
3879
	switch (state->props.code_rate_HP) {
3880 3881
	case FEC_AUTO:
	default:
3882
		operation_mode |= OFDM_SC_RA_RAM_OP_AUTO_RATE__M;
3883 3884
		/* fall through , try first guess DRX_CODERATE_2DIV3 */
	case FEC_2_3:
3885
		transmission_params |= OFDM_SC_RA_RAM_OP_PARAM_RATE_2_3;
3886 3887
		break;
	case FEC_1_2:
3888
		transmission_params |= OFDM_SC_RA_RAM_OP_PARAM_RATE_1_2;
3889 3890
		break;
	case FEC_3_4:
3891
		transmission_params |= OFDM_SC_RA_RAM_OP_PARAM_RATE_3_4;
3892 3893
		break;
	case FEC_5_6:
3894
		transmission_params |= OFDM_SC_RA_RAM_OP_PARAM_RATE_5_6;
3895 3896
		break;
	case FEC_7_8:
3897
		transmission_params |= OFDM_SC_RA_RAM_OP_PARAM_RATE_7_8;
3898 3899
		break;
	}
R
Ralph Metzler 已提交
3900

3901 3902 3903 3904 3905 3906
	/*
	 * SAW filter selection: normaly not necesarry, but if wanted
	 * the application can select a SAW filter via the driver by
	 * using UIOs
	 */

3907 3908
	/* First determine real bandwidth (Hz) */
	/* Also set delay for impulse noise cruncher */
3909 3910 3911 3912 3913
	/*
	 * 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
	 */
3914 3915 3916 3917 3918
	switch (state->props.bandwidth_hz) {
	case 0:
		state->props.bandwidth_hz = 8000000;
		/* fall though */
	case 8000000:
3919
		bandwidth = DRXK_BANDWIDTH_8MHZ_IN_HZ;
3920 3921
		status = write16(state, OFDM_SC_RA_RAM_SRMM_FIX_FACT_8K__A,
				 3052);
3922 3923 3924
		if (status < 0)
			goto error;
		/* cochannel protection for PAL 8 MHz */
3925 3926
		status = write16(state, OFDM_SC_RA_RAM_NI_INIT_8K_PER_LEFT__A,
				 7);
3927 3928
		if (status < 0)
			goto error;
3929 3930
		status = write16(state, OFDM_SC_RA_RAM_NI_INIT_8K_PER_RIGHT__A,
				 7);
3931 3932
		if (status < 0)
			goto error;
3933 3934
		status = write16(state, OFDM_SC_RA_RAM_NI_INIT_2K_PER_LEFT__A,
				 7);
3935 3936
		if (status < 0)
			goto error;
3937 3938
		status = write16(state, OFDM_SC_RA_RAM_NI_INIT_2K_PER_RIGHT__A,
				 1);
3939 3940 3941
		if (status < 0)
			goto error;
		break;
3942
	case 7000000:
3943
		bandwidth = DRXK_BANDWIDTH_7MHZ_IN_HZ;
3944 3945
		status = write16(state, OFDM_SC_RA_RAM_SRMM_FIX_FACT_8K__A,
				 3491);
3946 3947 3948
		if (status < 0)
			goto error;
		/* cochannel protection for PAL 7 MHz */
3949 3950
		status = write16(state, OFDM_SC_RA_RAM_NI_INIT_8K_PER_LEFT__A,
				 8);
3951 3952
		if (status < 0)
			goto error;
3953 3954
		status = write16(state, OFDM_SC_RA_RAM_NI_INIT_8K_PER_RIGHT__A,
				 8);
3955 3956
		if (status < 0)
			goto error;
3957 3958
		status = write16(state, OFDM_SC_RA_RAM_NI_INIT_2K_PER_LEFT__A,
				 4);
3959 3960
		if (status < 0)
			goto error;
3961 3962
		status = write16(state, OFDM_SC_RA_RAM_NI_INIT_2K_PER_RIGHT__A,
				 1);
3963 3964 3965
		if (status < 0)
			goto error;
		break;
3966
	case 6000000:
3967
		bandwidth = DRXK_BANDWIDTH_6MHZ_IN_HZ;
3968 3969
		status = write16(state, OFDM_SC_RA_RAM_SRMM_FIX_FACT_8K__A,
				 4073);
3970 3971 3972
		if (status < 0)
			goto error;
		/* cochannel protection for NTSC 6 MHz */
3973 3974
		status = write16(state, OFDM_SC_RA_RAM_NI_INIT_8K_PER_LEFT__A,
				 19);
3975 3976
		if (status < 0)
			goto error;
3977 3978
		status = write16(state, OFDM_SC_RA_RAM_NI_INIT_8K_PER_RIGHT__A,
				 19);
3979 3980
		if (status < 0)
			goto error;
3981 3982
		status = write16(state, OFDM_SC_RA_RAM_NI_INIT_2K_PER_LEFT__A,
				 14);
3983 3984
		if (status < 0)
			goto error;
3985 3986
		status = write16(state, OFDM_SC_RA_RAM_NI_INIT_2K_PER_RIGHT__A,
				 1);
3987 3988 3989 3990 3991 3992 3993
		if (status < 0)
			goto error;
		break;
	default:
		status = -EINVAL;
		goto error;
	}
R
Ralph Metzler 已提交
3994

3995
	if (iqm_rc_rate_ofs == 0) {
3996 3997 3998 3999 4000 4001
		/* Now compute IQM_RC_RATE_OFS
			(((SysFreq/BandWidth)/2)/2) -1) * 2^23)
			=>
			((SysFreq / BandWidth) * (2^21)) - (2^23)
			*/
		/* (SysFreq / BandWidth) * (2^28)  */
4002 4003 4004 4005 4006 4007
		/*
		 * assert (MAX(sysClk)/MIN(bandwidth) < 16)
		 *	=> assert(MAX(sysClk) < 16*MIN(bandwidth))
		 *	=> assert(109714272 > 48000000) = true
		 * so Frac 28 can be used
		 */
4008 4009
		iqm_rc_rate_ofs = Frac28a((u32)
					((state->m_sys_clock_freq *
4010
						1000) / 3), bandwidth);
4011
		/* (SysFreq / BandWidth) * (2^21), rounding before truncating */
4012 4013 4014
		if ((iqm_rc_rate_ofs & 0x7fL) >= 0x40)
			iqm_rc_rate_ofs += 0x80L;
		iqm_rc_rate_ofs = iqm_rc_rate_ofs >> 7;
4015
		/* ((SysFreq / BandWidth) * (2^21)) - (2^23)  */
4016
		iqm_rc_rate_ofs = iqm_rc_rate_ofs - (1 << 23);
4017
	}
R
Ralph Metzler 已提交
4018

4019
	iqm_rc_rate_ofs &=
4020 4021
		((((u32) IQM_RC_RATE_OFS_HI__M) <<
		IQM_RC_RATE_OFS_LO__W) | IQM_RC_RATE_OFS_LO__M);
4022
	status = write32(state, IQM_RC_RATE_OFS_LO__A, iqm_rc_rate_ofs);
4023 4024
	if (status < 0)
		goto error;
R
Ralph Metzler 已提交
4025

4026
	/* Bandwidth setting done */
R
Ralph Metzler 已提交
4027

4028
#if 0
4029
	status = dvbt_set_frequency_shift(demod, channel, tuner_offset);
4030 4031
	if (status < 0)
		goto error;
4032
#endif
4033 4034
	status = set_frequency_shifter(state, intermediate_freqk_hz,
				       tuner_freq_offset, true);
4035 4036
	if (status < 0)
		goto error;
R
Ralph Metzler 已提交
4037

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

4040 4041 4042 4043
	/* 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 已提交
4044

4045 4046 4047 4048 4049 4050 4051
	/* 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 已提交
4052 4053


4054 4055 4056
	status = scu_command(state, SCU_RAM_COMMAND_STANDARD_OFDM
			     | SCU_RAM_COMMAND_CMD_DEMOD_START,
			     0, NULL, 1, &cmd_result);
4057 4058 4059 4060 4061 4062 4063 4064 4065
	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);
4066 4067
	status = dvbt_sc_command(state, OFDM_SC_RA_RAM_CMD_SET_PREF_PARAM,
				0, transmission_params, param1, 0, 0, 0);
4068 4069
	if (status < 0)
		goto error;
R
Ralph Metzler 已提交
4070

4071 4072
	if (!state->m_drxk_a3_rom_code)
		status = dvbt_ctrl_set_sqi_speed(state, &state->m_sqi_speed);
4073 4074
error:
	if (status < 0)
4075
		pr_err("Error %d on %s\n", status, __func__);
R
Ralph Metzler 已提交
4076 4077 4078 4079 4080 4081 4082 4083

	return status;
}


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

/**
4084
* \brief Retrieve lock status .
R
Ralph Metzler 已提交
4085 4086 4087 4088 4089
* \param demod    Pointer to demodulator instance.
* \param lockStat Pointer to lock status structure.
* \return DRXStatus_t.
*
*/
4090
static int get_dvbt_lock_status(struct drxk_state *state, u32 *p_lock_status)
R
Ralph Metzler 已提交
4091
{
4092 4093 4094 4095 4096 4097
	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;

4098 4099
	u16 sc_ra_ram_lock = 0;
	u16 sc_comm_exec = 0;
4100

4101 4102
	dprintk(1, "\n");

4103
	*p_lock_status = NOT_LOCKED;
4104 4105
	/* driver 0.9.0 */
	/* Check if SC is running */
4106
	status = read16(state, OFDM_SC_COMM_EXEC__A, &sc_comm_exec);
4107 4108
	if (status < 0)
		goto end;
4109
	if (sc_comm_exec == OFDM_SC_COMM_EXEC_STOP)
4110
		goto end;
4111

4112
	status = read16(state, OFDM_SC_RA_RAM_LOCK__A, &sc_ra_ram_lock);
4113 4114
	if (status < 0)
		goto end;
4115

4116 4117 4118 4119 4120 4121 4122 4123
	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;
4124 4125
end:
	if (status < 0)
4126
		pr_err("Error %d on %s\n", status, __func__);
4127 4128

	return status;
R
Ralph Metzler 已提交
4129 4130
}

4131
static int power_up_qam(struct drxk_state *state)
R
Ralph Metzler 已提交
4132
{
4133
	enum drx_power_mode power_mode = DRXK_POWER_DOWN_OFDM;
4134
	int status;
R
Ralph Metzler 已提交
4135

4136
	dprintk(1, "\n");
4137
	status = ctrl_power_mode(state, &power_mode);
4138
	if (status < 0)
4139
		pr_err("Error %d on %s\n", status, __func__);
R
Ralph Metzler 已提交
4140

4141
	return status;
R
Ralph Metzler 已提交
4142 4143 4144
}


4145
/** Power Down QAM */
4146
static int power_down_qam(struct drxk_state *state)
R
Ralph Metzler 已提交
4147
{
4148
	u16 data = 0;
4149
	u16 cmd_result;
4150 4151
	int status = 0;

4152
	dprintk(1, "\n");
4153 4154 4155 4156 4157 4158 4159 4160 4161 4162
	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);
4163
		if (status < 0)
4164
			goto error;
4165 4166 4167
		status = scu_command(state, SCU_RAM_COMMAND_STANDARD_QAM
				     | SCU_RAM_COMMAND_CMD_DEMOD_STOP,
				     0, NULL, 1, &cmd_result);
4168
		if (status < 0)
4169 4170 4171
			goto error;
	}
	/* powerdown AFE                   */
4172
	status = set_iqm_af(state, false);
4173 4174 4175

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

	return status;
R
Ralph Metzler 已提交
4179
}
4180

R
Ralph Metzler 已提交
4181 4182 4183 4184 4185
/*============================================================================*/

/**
* \brief Setup of the QAM Measurement intervals for signal quality
* \param demod instance of demod.
4186
* \param modulation current modulation.
R
Ralph Metzler 已提交
4187 4188 4189 4190 4191 4192 4193
* \return DRXStatus_t.
*
*  NOTE:
*  Take into account that for certain settings the errorcounters can overflow.
*  The implementation does not check this.
*
*/
4194 4195 4196
static int set_qam_measurement(struct drxk_state *state,
			     enum e_drxk_constellation modulation,
			     u32 symbol_rate)
R
Ralph Metzler 已提交
4197
{
4198 4199 4200 4201
	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 已提交
4202 4203
	int status = 0;

4204
	dprintk(1, "\n");
R
Ralph Metzler 已提交
4205

4206 4207
	fec_rs_prescale = 1;
	/* fec_bits_desired = symbol_rate [kHz] *
4208
		FrameLenght [ms] *
4209
		(modulation + 1) *
4210 4211 4212
		SyncLoss (== 1) *
		ViterbiLoss (==1)
		*/
4213
	switch (modulation) {
4214
	case DRX_CONSTELLATION_QAM16:
4215
		fec_bits_desired = 4 * symbol_rate;
4216 4217
		break;
	case DRX_CONSTELLATION_QAM32:
4218
		fec_bits_desired = 5 * symbol_rate;
4219 4220
		break;
	case DRX_CONSTELLATION_QAM64:
4221
		fec_bits_desired = 6 * symbol_rate;
4222 4223
		break;
	case DRX_CONSTELLATION_QAM128:
4224
		fec_bits_desired = 7 * symbol_rate;
4225 4226
		break;
	case DRX_CONSTELLATION_QAM256:
4227
		fec_bits_desired = 8 * symbol_rate;
4228 4229 4230 4231 4232 4233
		break;
	default:
		status = -EINVAL;
	}
	if (status < 0)
		goto error;
R
Ralph Metzler 已提交
4234

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

4238
	/* Annex A/C: bits/RsPeriod = 204 * 8 = 1632 */
4239 4240
	/* fec_rs_period_total = fec_bits_desired / 1632 */
	fec_rs_period_total = (fec_bits_desired / 1632UL) + 1;	/* roughly ceil */
R
Ralph Metzler 已提交
4241

4242 4243 4244
	/* 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) {
4245 4246
		/* Divide by zero (though impossible) */
		status = -EINVAL;
4247
		if (status < 0)
4248 4249
			goto error;
	}
4250 4251 4252
	fec_rs_period =
		((u16) fec_rs_period_total +
		(fec_rs_prescale >> 1)) / fec_rs_prescale;
R
Ralph Metzler 已提交
4253

4254
	/* write corresponding registers */
4255
	status = write16(state, FEC_RS_MEASUREMENT_PERIOD__A, fec_rs_period);
4256
	if (status < 0)
4257
		goto error;
4258 4259
	status = write16(state, FEC_RS_MEASUREMENT_PRESCALE__A,
			 fec_rs_prescale);
4260 4261
	if (status < 0)
		goto error;
4262
	status = write16(state, FEC_OC_SNC_FAIL_PERIOD__A, fec_rs_period);
4263 4264
error:
	if (status < 0)
4265
		pr_err("Error %d on %s\n", status, __func__);
R
Ralph Metzler 已提交
4266 4267 4268
	return status;
}

4269
static int set_qam16(struct drxk_state *state)
R
Ralph Metzler 已提交
4270
{
4271 4272
	int status = 0;

4273
	dprintk(1, "\n");
4274 4275 4276 4277 4278 4279 4280 4281 4282 4283 4284 4285 4286 4287 4288 4289 4290 4291 4292 4293 4294 4295 4296 4297 4298 4299 4300 4301 4302 4303 4304 4305 4306 4307 4308 4309 4310 4311 4312
	/* 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;
4313

4314 4315 4316 4317 4318 4319 4320 4321 4322
	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;
4323

4324
	/* QAM Slicer Settings */
4325 4326
	status = write16(state, SCU_RAM_QAM_SL_SIG_POWER__A,
			 DRXK_QAM_SL_SIG_POWER_QAM16);
4327 4328
	if (status < 0)
		goto error;
4329

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

4356 4357 4358 4359 4360 4361 4362 4363 4364 4365 4366 4367 4368 4369 4370 4371 4372 4373 4374 4375 4376 4377 4378 4379 4380 4381 4382 4383 4384 4385 4386 4387 4388 4389 4390 4391
	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;
4392 4393


4394
	/* QAM State Machine (FSM) Thresholds */
4395

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

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


4426
	/* QAM FSM Tracking Parameters */
4427

4428 4429 4430 4431 4432 4433 4434 4435 4436 4437 4438 4439 4440 4441 4442 4443 4444 4445 4446 4447 4448
	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;
4449

4450 4451
error:
	if (status < 0)
4452
		pr_err("Error %d on %s\n", status, __func__);
4453
	return status;
R
Ralph Metzler 已提交
4454 4455 4456 4457 4458 4459 4460 4461 4462
}

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

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

4467
	dprintk(1, "\n");
4468

4469 4470 4471 4472 4473 4474 4475 4476 4477 4478 4479 4480 4481 4482 4483 4484 4485 4486 4487 4488
	/* 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;
4489

4490 4491 4492 4493 4494 4495 4496 4497 4498 4499 4500 4501 4502 4503 4504 4505 4506 4507 4508
	/* 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;
4509

4510 4511 4512 4513 4514 4515 4516 4517 4518
	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;
4519

4520
	/* QAM Slicer Settings */
4521

4522 4523
	status = write16(state, SCU_RAM_QAM_SL_SIG_POWER__A,
			 DRXK_QAM_SL_SIG_POWER_QAM32);
4524 4525
	if (status < 0)
		goto error;
4526 4527


4528
	/* QAM Loop Controller Coeficients */
4529

4530 4531 4532 4533 4534 4535 4536 4537 4538 4539 4540 4541 4542 4543 4544 4545 4546 4547 4548 4549 4550 4551 4552 4553
	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;
4554

4555 4556 4557 4558 4559 4560 4561 4562 4563 4564 4565 4566 4567 4568 4569 4570 4571 4572 4573 4574 4575 4576 4577 4578 4579 4580 4581 4582 4583 4584 4585 4586 4587 4588 4589 4590
	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;
4591 4592


4593
	/* QAM State Machine (FSM) Thresholds */
4594

4595 4596 4597 4598 4599 4600 4601 4602 4603 4604 4605 4606 4607 4608 4609 4610 4611 4612
	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;
4613

4614 4615 4616 4617 4618 4619 4620 4621 4622
	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;
4623 4624


4625
	/* QAM FSM Tracking Parameters */
4626

4627 4628 4629 4630 4631 4632 4633 4634 4635 4636 4637 4638 4639 4640 4641 4642 4643 4644 4645 4646 4647
	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)
4648
		pr_err("Error %d on %s\n", status, __func__);
4649
	return status;
R
Ralph Metzler 已提交
4650 4651 4652 4653 4654 4655 4656 4657 4658
}

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

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

4663
	dprintk(1, "\n");
4664 4665 4666 4667 4668 4669 4670 4671 4672 4673 4674 4675 4676 4677 4678 4679 4680 4681 4682 4683
	/* 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;
4684

4685 4686 4687 4688 4689 4690 4691 4692 4693 4694 4695 4696 4697 4698 4699 4700 4701 4702 4703
	/* 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;
4704

4705 4706 4707 4708 4709 4710 4711 4712 4713
	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;
4714

4715
	/* QAM Slicer Settings */
4716 4717
	status = write16(state, SCU_RAM_QAM_SL_SIG_POWER__A,
			 DRXK_QAM_SL_SIG_POWER_QAM64);
4718 4719
	if (status < 0)
		goto error;
4720 4721


4722
	/* QAM Loop Controller Coeficients */
4723

4724 4725 4726 4727 4728 4729 4730 4731 4732 4733 4734 4735 4736 4737 4738 4739 4740 4741 4742 4743 4744 4745 4746 4747
	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;
4748

4749 4750 4751 4752 4753 4754 4755 4756 4757 4758 4759 4760 4761 4762 4763 4764 4765 4766 4767 4768 4769 4770 4771 4772 4773 4774 4775 4776 4777 4778 4779 4780 4781 4782 4783 4784
	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;
4785 4786


4787
	/* QAM State Machine (FSM) Thresholds */
4788

4789 4790 4791 4792 4793 4794 4795 4796 4797 4798 4799 4800 4801 4802 4803 4804 4805 4806
	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;
4807

4808 4809 4810 4811 4812 4813 4814 4815 4816
	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;
4817 4818


4819
	/* QAM FSM Tracking Parameters */
4820

4821 4822 4823 4824 4825 4826 4827 4828 4829 4830 4831 4832 4833 4834 4835 4836 4837 4838 4839 4840 4841
	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)
4842
		pr_err("Error %d on %s\n", status, __func__);
4843 4844

	return status;
R
Ralph Metzler 已提交
4845 4846 4847 4848 4849 4850 4851 4852 4853
}

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

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

4858
	dprintk(1, "\n");
4859 4860 4861 4862 4863 4864 4865 4866 4867 4868 4869 4870 4871 4872 4873 4874 4875 4876 4877 4878
	/* 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;
4879

4880 4881 4882 4883 4884 4885 4886 4887 4888 4889 4890 4891 4892 4893 4894 4895 4896 4897 4898
	/* 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;
4899

4900 4901 4902 4903 4904 4905 4906 4907 4908
	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;
4909 4910


4911
	/* QAM Slicer Settings */
4912

4913 4914
	status = write16(state, SCU_RAM_QAM_SL_SIG_POWER__A,
			 DRXK_QAM_SL_SIG_POWER_QAM128);
4915 4916
	if (status < 0)
		goto error;
4917 4918


4919
	/* QAM Loop Controller Coeficients */
4920

4921 4922 4923 4924 4925 4926 4927 4928 4929 4930 4931 4932 4933 4934 4935 4936 4937 4938 4939 4940 4941 4942 4943 4944 4945 4946 4947 4948 4949 4950 4951 4952 4953 4954 4955 4956 4957 4958 4959 4960 4961 4962 4963 4964 4965 4966 4967 4968 4969 4970 4971 4972 4973 4974 4975 4976 4977 4978 4979 4980 4981
	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;
4982 4983


4984
	/* QAM State Machine (FSM) Thresholds */
4985

4986 4987 4988 4989 4990 4991 4992 4993 4994 4995 4996 4997 4998 4999 5000 5001 5002 5003
	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;
5004

5005 5006 5007 5008 5009 5010
	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;
5011

5012 5013 5014
	status = write16(state, SCU_RAM_QAM_FSM_FREQ_LIM__A, 12);
	if (status < 0)
		goto error;
5015

5016
	/* QAM FSM Tracking Parameters */
5017

5018 5019 5020 5021 5022 5023 5024 5025 5026 5027 5028 5029 5030 5031 5032 5033 5034 5035 5036 5037 5038
	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)
5039
		pr_err("Error %d on %s\n", status, __func__);
5040 5041

	return status;
R
Ralph Metzler 已提交
5042 5043 5044 5045 5046 5047 5048 5049 5050
}

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

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

5055
	dprintk(1, "\n");
5056 5057 5058 5059 5060 5061 5062 5063 5064 5065 5066 5067 5068 5069 5070 5071 5072 5073 5074 5075
	/* 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;
5076

5077 5078 5079 5080 5081 5082 5083 5084 5085 5086 5087 5088 5089 5090 5091 5092 5093 5094 5095
	/* 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;
5096

5097 5098 5099 5100 5101 5102 5103 5104 5105
	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;
5106

5107
	/* QAM Slicer Settings */
5108

5109 5110
	status = write16(state, SCU_RAM_QAM_SL_SIG_POWER__A,
			 DRXK_QAM_SL_SIG_POWER_QAM256);
5111 5112
	if (status < 0)
		goto error;
5113 5114


5115
	/* QAM Loop Controller Coeficients */
5116

5117 5118 5119 5120 5121 5122 5123 5124 5125 5126 5127 5128 5129 5130 5131 5132 5133 5134 5135 5136 5137 5138 5139 5140
	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;
5141

5142 5143 5144 5145 5146 5147 5148 5149 5150 5151 5152 5153 5154 5155 5156 5157 5158 5159 5160 5161 5162 5163 5164 5165 5166 5167 5168 5169 5170 5171 5172 5173 5174 5175 5176 5177
	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;
5178 5179


5180
	/* QAM State Machine (FSM) Thresholds */
5181

5182 5183 5184 5185 5186 5187 5188 5189 5190 5191 5192 5193 5194 5195 5196 5197 5198 5199
	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;
5200

5201 5202 5203 5204 5205 5206 5207 5208 5209
	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;
5210 5211


5212
	/* QAM FSM Tracking Parameters */
5213

5214 5215 5216 5217 5218 5219 5220 5221 5222 5223 5224 5225 5226 5227 5228 5229 5230 5231 5232 5233 5234
	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)
5235
		pr_err("Error %d on %s\n", status, __func__);
5236
	return status;
R
Ralph Metzler 已提交
5237 5238 5239 5240 5241 5242 5243 5244 5245 5246
}


/*============================================================================*/
/**
* \brief Reset QAM block.
* \param demod:   instance of demod.
* \param channel: pointer to channel data.
* \return DRXStatus_t.
*/
5247
static int qam_reset_qam(struct drxk_state *state)
R
Ralph Metzler 已提交
5248
{
5249
	int status;
5250
	u16 cmd_result;
R
Ralph Metzler 已提交
5251

5252
	dprintk(1, "\n");
5253 5254 5255 5256
	/* Stop QAM comstate->m_exec */
	status = write16(state, QAM_COMM_EXEC__A, QAM_COMM_EXEC_STOP);
	if (status < 0)
		goto error;
R
Ralph Metzler 已提交
5257

5258 5259 5260
	status = scu_command(state, SCU_RAM_COMMAND_STANDARD_QAM
			     | SCU_RAM_COMMAND_CMD_DEMOD_RESET,
			     0, NULL, 1, &cmd_result);
5261 5262
error:
	if (status < 0)
5263
		pr_err("Error %d on %s\n", status, __func__);
5264
	return status;
R
Ralph Metzler 已提交
5265 5266 5267 5268 5269 5270 5271 5272 5273 5274
}

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

/**
* \brief Set QAM symbolrate.
* \param demod:   instance of demod.
* \param channel: pointer to channel data.
* \return DRXStatus_t.
*/
5275
static int qam_set_symbolrate(struct drxk_state *state)
R
Ralph Metzler 已提交
5276
{
5277 5278 5279
	u32 adc_frequency = 0;
	u32 symb_freq = 0;
	u32 iqm_rc_rate = 0;
5280
	u16 ratesel = 0;
5281
	u32 lc_symb_rate = 0;
5282 5283
	int status;

5284
	dprintk(1, "\n");
5285
	/* Select & calculate correct IQM rate */
5286
	adc_frequency = (state->m_sys_clock_freq * 1000) / 3;
5287
	ratesel = 0;
5288 5289
	/* printk(KERN_DEBUG "drxk: SR %d\n", state->props.symbol_rate); */
	if (state->props.symbol_rate <= 1188750)
5290
		ratesel = 3;
5291
	else if (state->props.symbol_rate <= 2377500)
5292
		ratesel = 2;
5293
	else if (state->props.symbol_rate <= 4755000)
5294 5295 5296 5297
		ratesel = 1;
	status = write16(state, IQM_FD_RATESEL__A, ratesel);
	if (status < 0)
		goto error;
5298

5299 5300 5301
	/*
		IqmRcRate = ((Fadc / (symbolrate * (4<<ratesel))) - 1) * (1<<23)
		*/
5302 5303
	symb_freq = state->props.symbol_rate * (1 << ratesel);
	if (symb_freq == 0) {
5304 5305 5306 5307
		/* Divide by zero */
		status = -EINVAL;
		goto error;
	}
5308 5309
	iqm_rc_rate = (adc_frequency / symb_freq) * (1 << 21) +
		(Frac28a((adc_frequency % symb_freq), symb_freq) >> 7) -
5310
		(1 << 23);
5311
	status = write32(state, IQM_RC_RATE_OFS_LO__A, iqm_rc_rate);
5312 5313
	if (status < 0)
		goto error;
5314
	state->m_iqm_rc_rate = iqm_rc_rate;
5315
	/*
5316
		LcSymbFreq = round (.125 *  symbolrate / adc_freq * (1<<15))
5317
		*/
5318 5319
	symb_freq = state->props.symbol_rate;
	if (adc_frequency == 0) {
5320 5321 5322 5323
		/* Divide by zero */
		status = -EINVAL;
		goto error;
	}
5324 5325
	lc_symb_rate = (symb_freq / adc_frequency) * (1 << 12) +
		(Frac28a((symb_freq % adc_frequency), adc_frequency) >>
5326
		16);
5327 5328 5329
	if (lc_symb_rate > 511)
		lc_symb_rate = 511;
	status = write16(state, QAM_LC_SYMBOL_FREQ__A, (u16) lc_symb_rate);
5330 5331 5332

error:
	if (status < 0)
5333
		pr_err("Error %d on %s\n", status, __func__);
5334
	return status;
R
Ralph Metzler 已提交
5335 5336 5337 5338 5339 5340 5341 5342 5343 5344 5345
}

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

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

5346
static int get_qam_lock_status(struct drxk_state *state, u32 *p_lock_status)
R
Ralph Metzler 已提交
5347 5348
{
	int status;
5349
	u16 result[2] = { 0, 0 };
R
Ralph Metzler 已提交
5350

5351
	dprintk(1, "\n");
5352
	*p_lock_status = NOT_LOCKED;
5353
	status = scu_command(state,
5354 5355
			SCU_RAM_COMMAND_STANDARD_QAM |
			SCU_RAM_COMMAND_CMD_DEMOD_GET_LOCK, 0, NULL, 2,
5356
			result);
5357
	if (status < 0)
5358
		pr_err("Error %d on %s\n", status, __func__);
5359

5360
	if (result[1] < SCU_RAM_QAM_LOCKED_LOCKED_DEMOD_LOCKED) {
R
Ralph Metzler 已提交
5361
		/* 0x0000 NOT LOCKED */
5362
	} else if (result[1] < SCU_RAM_QAM_LOCKED_LOCKED_LOCKED) {
R
Ralph Metzler 已提交
5363
		/* 0x4000 DEMOD LOCKED */
5364 5365
		*p_lock_status = DEMOD_LOCK;
	} else if (result[1] < SCU_RAM_QAM_LOCKED_LOCKED_NEVER_LOCK) {
R
Ralph Metzler 已提交
5366
		/* 0x8000 DEMOD + FEC LOCKED (system lock) */
5367
		*p_lock_status = MPEG_LOCK;
5368
	} else {
R
Ralph Metzler 已提交
5369 5370
		/* 0xC000 NEVER LOCKED */
		/* (system will never be able to lock to the signal) */
5371 5372 5373 5374
		/*
		 * TODO: check this, intermediate & standard specific lock
		 * states are not taken into account here
		 */
5375
		*p_lock_status = NEVER_LOCK;
R
Ralph Metzler 已提交
5376 5377 5378 5379 5380 5381 5382 5383 5384 5385 5386
	}
	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

5387 5388
static int qam_demodulator_command(struct drxk_state *state,
				 int number_of_parameters)
5389 5390
{
	int status;
5391 5392
	u16 cmd_result;
	u16 set_param_parameters[4] = { 0, 0, 0, 0 };
5393

5394 5395
	set_param_parameters[0] = state->m_constellation;	/* modulation     */
	set_param_parameters[1] = DRXK_QAM_I12_J17;	/* interleave mode   */
5396

5397 5398
	if (number_of_parameters == 2) {
		u16 set_env_parameters[1] = { 0 };
5399

5400 5401
		if (state->m_operation_mode == OM_QAM_ITU_C)
			set_env_parameters[0] = QAM_TOP_ANNEX_C;
5402
		else
5403
			set_env_parameters[0] = QAM_TOP_ANNEX_A;
5404 5405

		status = scu_command(state,
5406 5407
				     SCU_RAM_COMMAND_STANDARD_QAM
				     | SCU_RAM_COMMAND_CMD_DEMOD_SET_ENV,
5408
				     1, set_env_parameters, 1, &cmd_result);
5409 5410 5411 5412
		if (status < 0)
			goto error;

		status = scu_command(state,
5413 5414
				     SCU_RAM_COMMAND_STANDARD_QAM
				     | SCU_RAM_COMMAND_CMD_DEMOD_SET_PARAM,
5415 5416 5417 5418 5419
				     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;
5420
		else
5421
			set_param_parameters[2] = QAM_TOP_ANNEX_A;
5422

5423
		set_param_parameters[3] |= (QAM_MIRROR_AUTO_ON);
5424 5425
		/* Env parameters */
		/* check for LOCKRANGE Extented */
5426
		/* set_param_parameters[3] |= QAM_LOCKRANGE_NORMAL; */
5427 5428

		status = scu_command(state,
5429 5430
				     SCU_RAM_COMMAND_STANDARD_QAM
				     | SCU_RAM_COMMAND_CMD_DEMOD_SET_PARAM,
5431 5432
				     number_of_parameters, set_param_parameters,
				     1, &cmd_result);
5433
	} else {
5434 5435
		pr_warn("Unknown QAM demodulator parameter count %d\n",
			number_of_parameters);
5436
		status = -EINVAL;
5437 5438 5439 5440
	}

error:
	if (status < 0)
5441
		pr_warn("Warning %d on %s\n", status, __func__);
5442 5443 5444
	return status;
}

5445 5446
static int set_qam(struct drxk_state *state, u16 intermediate_freqk_hz,
		  s32 tuner_freq_offset)
R
Ralph Metzler 已提交
5447
{
5448
	int status;
5449 5450
	u16 cmd_result;
	int qam_demod_param_count = state->qam_demod_parameter_count;
R
Ralph Metzler 已提交
5451

5452
	dprintk(1, "\n");
5453
	/*
5454 5455 5456 5457 5458
	 * STEP 1: reset demodulator
	 *	resets FEC DI and FEC RS
	 *	resets QAM block
	 *	resets SCU variables
	 */
5459 5460 5461 5462 5463 5464
	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;
5465
	status = qam_reset_qam(state);
5466 5467
	if (status < 0)
		goto error;
R
Ralph Metzler 已提交
5468

5469
	/*
5470 5471 5472 5473
	 * STEP 2: configure demodulator
	 *	-set params; resets IQM,QAM,FEC HW; initializes some
	 *       SCU variables
	 */
5474
	status = qam_set_symbolrate(state);
5475 5476 5477 5478
	if (status < 0)
		goto error;

	/* Set params */
5479
	switch (state->props.modulation) {
5480
	case QAM_256:
5481
		state->m_constellation = DRX_CONSTELLATION_QAM256;
5482 5483 5484
		break;
	case QAM_AUTO:
	case QAM_64:
5485
		state->m_constellation = DRX_CONSTELLATION_QAM64;
5486 5487
		break;
	case QAM_16:
5488
		state->m_constellation = DRX_CONSTELLATION_QAM16;
5489 5490
		break;
	case QAM_32:
5491
		state->m_constellation = DRX_CONSTELLATION_QAM32;
5492 5493
		break;
	case QAM_128:
5494
		state->m_constellation = DRX_CONSTELLATION_QAM128;
5495 5496 5497 5498 5499 5500 5501
		break;
	default:
		status = -EINVAL;
		break;
	}
	if (status < 0)
		goto error;
R
Ralph Metzler 已提交
5502

5503 5504 5505 5506
	/* 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) {
5507 5508
		qam_demod_param_count = 4;
		status = qam_demodulator_command(state, qam_demod_param_count);
5509
	}
R
Ralph Metzler 已提交
5510

5511 5512 5513 5514 5515
	/* 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)) {
5516 5517
		qam_demod_param_count = 2;
		status = qam_demodulator_command(state, qam_demod_param_count);
5518
	}
5519 5520

	if (status < 0) {
5521 5522 5523
		dprintk(1, "Could not set demodulator parameters.\n");
		dprintk(1,
			"Make sure qam_demod_parameter_count (%d) is correct for your firmware (%s).\n",
5524 5525
			state->qam_demod_parameter_count,
			state->microcode_name);
5526
		goto error;
5527
	} else if (!state->qam_demod_parameter_count) {
5528 5529
		dprintk(1,
			"Auto-probing the QAM command parameters was successful - using %d parameters.\n",
5530
			qam_demod_param_count);
5531

5532 5533 5534 5535
		/*
		 * One of our commands was successful. We don't need to
		 * auto-probe anymore, now that we got the correct command.
		 */
5536
		state->qam_demod_parameter_count = qam_demod_param_count;
5537
	}
R
Ralph Metzler 已提交
5538

5539 5540
	/*
	 * STEP 3: enable the system in a mode where the ADC provides valid
5541
	 * signal setup modulation independent registers
5542
	 */
5543
#if 0
5544
	status = set_frequency(channel, tuner_freq_offset));
5545 5546
	if (status < 0)
		goto error;
5547
#endif
5548 5549
	status = set_frequency_shifter(state, intermediate_freqk_hz,
				       tuner_freq_offset, true);
5550 5551
	if (status < 0)
		goto error;
R
Ralph Metzler 已提交
5552

5553
	/* Setup BER measurement */
5554 5555
	status = set_qam_measurement(state, state->m_constellation,
				     state->props.symbol_rate);
5556 5557
	if (status < 0)
		goto error;
5558

5559 5560 5561 5562 5563 5564 5565
	/* 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;
5566

5567 5568 5569 5570 5571 5572 5573 5574 5575 5576 5577 5578 5579
	/* 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;
5580

5581 5582 5583 5584 5585 5586 5587 5588 5589 5590 5591 5592 5593 5594 5595 5596 5597 5598 5599 5600 5601 5602 5603 5604 5605 5606 5607 5608 5609 5610 5611 5612 5613 5614 5615 5616 5617 5618 5619 5620 5621 5622 5623 5624 5625
	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;
5626

5627
	/* Mirroring, QAM-block starting point not inverted */
5628 5629
	status = write16(state, QAM_SY_SP_INV__A,
			 QAM_SY_SP_INV_SPECTRUM_INV_DIS);
5630 5631
	if (status < 0)
		goto error;
5632

5633 5634 5635 5636
	/* Halt SCU to enable safe non-atomic accesses */
	status = write16(state, SCU_COMM_EXEC__A, SCU_COMM_EXEC_HOLD);
	if (status < 0)
		goto error;
5637

5638 5639
	/* STEP 4: modulation specific setup */
	switch (state->props.modulation) {
5640
	case QAM_16:
5641
		status = set_qam16(state);
5642 5643
		break;
	case QAM_32:
5644
		status = set_qam32(state);
5645 5646 5647
		break;
	case QAM_AUTO:
	case QAM_64:
5648
		status = set_qam64(state);
5649 5650
		break;
	case QAM_128:
5651
		status = set_qam128(state);
5652 5653
		break;
	case QAM_256:
5654
		status = set_qam256(state);
5655 5656 5657 5658 5659 5660 5661
		break;
	default:
		status = -EINVAL;
		break;
	}
	if (status < 0)
		goto error;
5662

5663 5664 5665 5666
	/* Activate SCU to enable SCU commands */
	status = write16(state, SCU_COMM_EXEC__A, SCU_COMM_EXEC_ACTIVE);
	if (status < 0)
		goto error;
5667

5668
	/* Re-configure MPEG output, requires knowledge of channel bitrate */
5669
	/* extAttr->currentChannel.modulation = channel->modulation; */
5670
	/* extAttr->currentChannel.symbolrate    = channel->symbolrate; */
5671
	status = mpegts_dto_setup(state, state->m_operation_mode);
5672 5673
	if (status < 0)
		goto error;
5674

5675 5676
	/* start processes */
	status = mpegts_start(state);
5677 5678 5679 5680 5681 5682 5683 5684 5685 5686 5687
	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;
5688

5689
	/* STEP 5: start QAM demodulator (starts FEC, QAM and IQM HW) */
5690 5691 5692
	status = scu_command(state, SCU_RAM_COMMAND_STANDARD_QAM
			     | SCU_RAM_COMMAND_CMD_DEMOD_START,
			     0, NULL, 1, &cmd_result);
5693 5694
	if (status < 0)
		goto error;
5695

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

5699
error:
5700
	if (status < 0)
5701
		pr_err("Error %d on %s\n", status, __func__);
5702
	return status;
R
Ralph Metzler 已提交
5703 5704
}

5705 5706
static int set_qam_standard(struct drxk_state *state,
			  enum operation_mode o_mode)
R
Ralph Metzler 已提交
5707
{
5708
	int status;
R
Ralph Metzler 已提交
5709 5710 5711 5712 5713 5714
#ifdef DRXK_QAM_TAPS
#define DRXK_QAMA_TAPS_SELECT
#include "drxk_filters.h"
#undef DRXK_QAMA_TAPS_SELECT
#endif

5715 5716
	dprintk(1, "\n");

5717
	/* added antenna switch */
5718
	switch_antenna_to_qam(state);
5719

5720
	/* Ensure correct power-up mode */
5721
	status = power_up_qam(state);
5722 5723 5724
	if (status < 0)
		goto error;
	/* Reset QAM block */
5725
	status = qam_reset_qam(state);
5726 5727
	if (status < 0)
		goto error;
5728

5729
	/* Setup IQM */
5730

5731 5732 5733 5734 5735 5736
	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;
5737

5738 5739
	/* Upload IQM Channel Filter settings by
		boot loader from ROM table */
5740
	switch (o_mode) {
5741
	case OM_QAM_ITU_A:
5742 5743 5744
		status = bl_chain_cmd(state, DRXK_BL_ROM_OFFSET_TAPS_ITU_A,
				      DRXK_BLCC_NR_ELEMENTS_TAPS,
			DRXK_BLC_TIMEOUT);
5745 5746
		break;
	case OM_QAM_ITU_C:
5747 5748 5749 5750
		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);
5751
		if (status < 0)
5752
			goto error;
5753 5754 5755 5756 5757
		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);
5758 5759 5760 5761 5762 5763
		break;
	default:
		status = -EINVAL;
	}
	if (status < 0)
		goto error;
5764

5765
	status = write16(state, IQM_CF_OUT_ENA__A, 1 << IQM_CF_OUT_ENA_QAM__B);
5766 5767 5768 5769 5770
	if (status < 0)
		goto error;
	status = write16(state, IQM_CF_SYMMETRIC__A, 0);
	if (status < 0)
		goto error;
5771 5772
	status = write16(state, IQM_CF_MIDTAP__A,
		     ((1 << IQM_CF_MIDTAP_RE__B) | (1 << IQM_CF_MIDTAP_IM__B)));
5773 5774
	if (status < 0)
		goto error;
5775

5776 5777 5778 5779 5780 5781 5782 5783 5784 5785 5786 5787 5788 5789 5790
	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;
5791

5792 5793 5794 5795 5796 5797 5798 5799 5800 5801 5802 5803
	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;
5804

5805 5806 5807 5808 5809 5810 5811 5812 5813 5814 5815 5816 5817 5818 5819 5820 5821 5822 5823 5824 5825 5826
	/* 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;
5827

5828
	/* turn on IQMAF. Must be done before setAgc**() */
5829
	status = set_iqm_af(state, true);
5830 5831 5832 5833 5834
	if (status < 0)
		goto error;
	status = write16(state, IQM_AF_START_LOCK__A, 0x01);
	if (status < 0)
		goto error;
5835

5836
	/* IQM will not be reset from here, sync ADC and update/init AGC */
5837
	status = adc_synchronization(state);
5838 5839
	if (status < 0)
		goto error;
5840

5841 5842 5843 5844
	/* Set the FSM step period */
	status = write16(state, SCU_RAM_QAM_FSM_STEP_PERIOD__A, 2000);
	if (status < 0)
		goto error;
5845

5846 5847 5848 5849
	/* Halt SCU to enable safe non-atomic accesses */
	status = write16(state, SCU_COMM_EXEC__A, SCU_COMM_EXEC_HOLD);
	if (status < 0)
		goto error;
5850

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

5854
	status = init_agc(state, true);
5855 5856
	if (status < 0)
		goto error;
5857
	status = set_pre_saw(state, &(state->m_qam_pre_saw_cfg));
5858 5859
	if (status < 0)
		goto error;
5860

5861
	/* Configure AGC's */
5862
	status = set_agc_rf(state, &(state->m_qam_rf_agc_cfg), true);
5863 5864
	if (status < 0)
		goto error;
5865
	status = set_agc_if(state, &(state->m_qam_if_agc_cfg), true);
5866 5867
	if (status < 0)
		goto error;
5868

5869 5870 5871 5872
	/* Activate SCU to enable SCU commands */
	status = write16(state, SCU_COMM_EXEC__A, SCU_COMM_EXEC_ACTIVE);
error:
	if (status < 0)
5873
		pr_err("Error %d on %s\n", status, __func__);
5874
	return status;
R
Ralph Metzler 已提交
5875 5876
}

5877
static int write_gpio(struct drxk_state *state)
R
Ralph Metzler 已提交
5878
{
5879 5880 5881
	int status;
	u16 value = 0;

5882
	dprintk(1, "\n");
5883
	/* stop lock indicator process */
5884 5885
	status = write16(state, SCU_RAM_GPIO__A,
			 SCU_RAM_GPIO_HW_LOCK_IND_DISABLE);
5886 5887
	if (status < 0)
		goto error;
5888

5889 5890 5891 5892
	/*  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;
5893

5894 5895
	if (state->m_has_sawsw) {
		if (state->uio_mask & 0x0001) { /* UIO-1 */
5896
			/* write to io pad configuration register - output mode */
5897 5898
			status = write16(state, SIO_PDR_SMA_TX_CFG__A,
					 state->m_gpio_cfg);
5899 5900
			if (status < 0)
				goto error;
5901

5902 5903 5904 5905
			/* use corresponding bit in io data output registar */
			status = read16(state, SIO_PDR_UIO_OUT_LO__A, &value);
			if (status < 0)
				goto error;
5906
			if ((state->m_gpio & 0x0001) == 0)
5907 5908 5909 5910 5911 5912 5913 5914
				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;
		}
5915
		if (state->uio_mask & 0x0002) { /* UIO-2 */
5916
			/* write to io pad configuration register - output mode */
5917 5918
			status = write16(state, SIO_PDR_SMA_RX_CFG__A,
					 state->m_gpio_cfg);
5919 5920
			if (status < 0)
				goto error;
5921

5922 5923 5924 5925
			/* use corresponding bit in io data output registar */
			status = read16(state, SIO_PDR_UIO_OUT_LO__A, &value);
			if (status < 0)
				goto error;
5926
			if ((state->m_gpio & 0x0002) == 0)
5927 5928 5929 5930 5931 5932 5933 5934
				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;
		}
5935
		if (state->uio_mask & 0x0004) { /* UIO-3 */
5936
			/* write to io pad configuration register - output mode */
5937 5938
			status = write16(state, SIO_PDR_GPIO_CFG__A,
					 state->m_gpio_cfg);
5939 5940 5941 5942 5943 5944 5945
			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;
5946
			if ((state->m_gpio & 0x0004) == 0)
5947 5948 5949 5950 5951 5952 5953 5954
				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;
		}
5955 5956 5957 5958 5959
	}
	/*  Write magic word to disable pdr reg write               */
	status = write16(state, SIO_TOP_COMM_KEY__A, 0x0000);
error:
	if (status < 0)
5960
		pr_err("Error %d on %s\n", status, __func__);
5961
	return status;
R
Ralph Metzler 已提交
5962 5963
}

5964
static int switch_antenna_to_qam(struct drxk_state *state)
R
Ralph Metzler 已提交
5965
{
5966
	int status = 0;
5967
	bool gpio_state;
5968

5969
	dprintk(1, "\n");
5970

5971 5972 5973
	if (!state->antenna_gpio)
		return 0;

5974
	gpio_state = state->m_gpio & state->antenna_gpio;
5975 5976 5977 5978

	if (state->antenna_dvbt ^ gpio_state) {
		/* Antenna is on DVB-T mode. Switch */
		if (state->antenna_dvbt)
5979
			state->m_gpio &= ~state->antenna_gpio;
5980
		else
5981 5982
			state->m_gpio |= state->antenna_gpio;
		status = write_gpio(state);
5983
	}
5984
	if (status < 0)
5985
		pr_err("Error %d on %s\n", status, __func__);
5986
	return status;
R
Ralph Metzler 已提交
5987 5988
}

5989
static int switch_antenna_to_dvbt(struct drxk_state *state)
R
Ralph Metzler 已提交
5990
{
5991
	int status = 0;
5992
	bool gpio_state;
5993

5994
	dprintk(1, "\n");
5995 5996 5997 5998

	if (!state->antenna_gpio)
		return 0;

5999
	gpio_state = state->m_gpio & state->antenna_gpio;
6000 6001 6002 6003

	if (!(state->antenna_dvbt ^ gpio_state)) {
		/* Antenna is on DVB-C mode. Switch */
		if (state->antenna_dvbt)
6004
			state->m_gpio |= state->antenna_gpio;
6005
		else
6006 6007
			state->m_gpio &= ~state->antenna_gpio;
		status = write_gpio(state);
R
Ralph Metzler 已提交
6008
	}
6009
	if (status < 0)
6010
		pr_err("Error %d on %s\n", status, __func__);
R
Ralph Metzler 已提交
6011 6012 6013 6014
	return status;
}


6015
static int power_down_device(struct drxk_state *state)
R
Ralph Metzler 已提交
6016 6017 6018 6019 6020 6021 6022 6023
{
	/* 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;
6024 6025

	dprintk(1, "\n");
6026
	if (state->m_b_p_down_open_bridge) {
6027 6028
		/* Open I2C bridge before power down of DRXK */
		status = ConfigureI2CBridge(state, true);
6029
		if (status < 0)
6030 6031 6032
			goto error;
	}
	/* driver 0.9.0 */
6033
	status = dvbt_enable_ofdm_token_ring(state, false);
6034 6035
	if (status < 0)
		goto error;
R
Ralph Metzler 已提交
6036

6037 6038
	status = write16(state, SIO_CC_PWD_MODE__A,
			 SIO_CC_PWD_MODE_LEVEL_CLOCK);
6039
	if (status < 0)
6040 6041 6042 6043
		goto error;
	status = write16(state, SIO_CC_UPDATE__A, SIO_CC_UPDATE_KEY);
	if (status < 0)
		goto error;
6044 6045
	state->m_hi_cfg_ctrl |= SIO_HI_RA_RAM_PAR_5_CFG_SLEEP_ZZZ;
	status = hi_cfg_command(state);
6046 6047
error:
	if (status < 0)
6048
		pr_err("Error %d on %s\n", status, __func__);
6049

6050
	return status;
R
Ralph Metzler 已提交
6051 6052 6053 6054
}

static int init_drxk(struct drxk_state *state)
{
6055
	int status = 0, n = 0;
6056 6057
	enum drx_power_mode power_mode = DRXK_POWER_DOWN_OFDM;
	u16 driver_version;
R
Ralph Metzler 已提交
6058

6059
	dprintk(1, "\n");
6060
	if ((state->m_drxk_state == DRXK_UNINITIALIZED)) {
6061
		drxk_i2c_lock(state);
6062
		status = power_up_device(state);
6063 6064
		if (status < 0)
			goto error;
6065
		status = drxx_open(state);
6066 6067 6068
		if (status < 0)
			goto error;
		/* Soft reset of OFDM-, sys- and osc-clockdomain */
6069 6070 6071 6072
		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);
6073 6074 6075 6076 6077
		if (status < 0)
			goto error;
		status = write16(state, SIO_CC_UPDATE__A, SIO_CC_UPDATE_KEY);
		if (status < 0)
			goto error;
6078 6079 6080 6081
		/*
		 * TODO is this needed? If yes, how much delay in
		 * worst case scenario
		 */
6082
		usleep_range(1000, 2000);
6083 6084
		state->m_drxk_a3_patch_code = true;
		status = get_device_capabilities(state);
6085 6086 6087 6088 6089 6090
		if (status < 0)
			goto error;

		/* Bridge delay, uses oscilator clock */
		/* Delay = (delay (nano seconds) * oscclk (kHz))/ 1000 */
		/* SDA brdige delay */
6091 6092
		state->m_hi_cfg_bridge_delay =
			(u16) ((state->m_osc_clock_freq / 1000) *
6093 6094
				HI_I2C_BRIDGE_DELAY) / 1000;
		/* Clipping */
6095
		if (state->m_hi_cfg_bridge_delay >
6096
			SIO_HI_RA_RAM_PAR_3_CFG_DBL_SDA__M) {
6097
			state->m_hi_cfg_bridge_delay =
6098 6099 6100
				SIO_HI_RA_RAM_PAR_3_CFG_DBL_SDA__M;
		}
		/* SCL bridge delay, same as SDA for now */
6101 6102
		state->m_hi_cfg_bridge_delay +=
			state->m_hi_cfg_bridge_delay <<
6103 6104
			SIO_HI_RA_RAM_PAR_3_CFG_DBL_SCL__B;

6105
		status = init_hi(state);
6106 6107 6108
		if (status < 0)
			goto error;
		/* disable various processes */
R
Ralph Metzler 已提交
6109
#if NOA1ROM
6110 6111
		if (!(state->m_DRXK_A1_ROM_CODE)
			&& !(state->m_DRXK_A2_ROM_CODE))
R
Ralph Metzler 已提交
6112
#endif
6113
		{
6114 6115
			status = write16(state, SCU_RAM_GPIO__A,
					 SCU_RAM_GPIO_HW_LOCK_IND_DISABLE);
6116
			if (status < 0)
6117 6118
				goto error;
		}
R
Ralph Metzler 已提交
6119

6120
		/* disable MPEG port */
6121
		status = mpegts_disable(state);
6122 6123
		if (status < 0)
			goto error;
R
Ralph Metzler 已提交
6124

6125 6126 6127 6128 6129 6130 6131
		/* 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;
6132

6133
		/* enable token-ring bus through OFDM block for possible ucode upload */
6134 6135
		status = write16(state, SIO_OFDM_SH_OFDM_RING_ENABLE__A,
				 SIO_OFDM_SH_OFDM_RING_ENABLE_ON);
6136 6137
		if (status < 0)
			goto error;
R
Ralph Metzler 已提交
6138

6139
		/* include boot loader section */
6140 6141
		status = write16(state, SIO_BL_COMM_EXEC__A,
				 SIO_BL_COMM_EXEC_ACTIVE);
6142 6143
		if (status < 0)
			goto error;
6144
		status = bl_chain_cmd(state, 0, 6, 100);
6145 6146
		if (status < 0)
			goto error;
R
Ralph Metzler 已提交
6147

6148
		if (state->fw) {
6149
			status = download_microcode(state, state->fw->data,
6150 6151 6152 6153
						   state->fw->size);
			if (status < 0)
				goto error;
		}
R
Ralph Metzler 已提交
6154

6155
		/* disable token-ring bus through OFDM block for possible ucode upload */
6156 6157
		status = write16(state, SIO_OFDM_SH_OFDM_RING_ENABLE__A,
				 SIO_OFDM_SH_OFDM_RING_ENABLE_OFF);
6158 6159
		if (status < 0)
			goto error;
6160

6161 6162 6163 6164
		/* 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;
6165
		status = drxx_open(state);
6166 6167 6168 6169
		if (status < 0)
			goto error;
		/* added for test */
		msleep(30);
R
Ralph Metzler 已提交
6170

6171 6172
		power_mode = DRXK_POWER_DOWN_OFDM;
		status = ctrl_power_mode(state, &power_mode);
6173 6174 6175 6176
		if (status < 0)
			goto error;

		/* Stamp driver version number in SCU data RAM in BCD code
6177
			Done to enable field application engineers to retrieve drxdriver version
6178 6179 6180 6181
			via I2C from SCU RAM.
			Not using SCU command interface for SCU register access since no
			microcode may be present.
			*/
6182
		driver_version =
6183 6184 6185 6186
			(((DRXK_VERSION_MAJOR / 100) % 10) << 12) +
			(((DRXK_VERSION_MAJOR / 10) % 10) << 8) +
			((DRXK_VERSION_MAJOR % 10) << 4) +
			(DRXK_VERSION_MINOR % 10);
6187 6188
		status = write16(state, SCU_RAM_DRIVER_VER_HI__A,
				 driver_version);
6189 6190
		if (status < 0)
			goto error;
6191
		driver_version =
6192 6193 6194 6195
			(((DRXK_VERSION_PATCH / 1000) % 10) << 12) +
			(((DRXK_VERSION_PATCH / 100) % 10) << 8) +
			(((DRXK_VERSION_PATCH / 10) % 10) << 4) +
			(DRXK_VERSION_PATCH % 10);
6196 6197
		status = write16(state, SCU_RAM_DRIVER_VER_LO__A,
				 driver_version);
6198 6199 6200
		if (status < 0)
			goto error;

6201
		pr_info("DRXK driver version %d.%d.%d\n",
6202 6203 6204
			DRXK_VERSION_MAJOR, DRXK_VERSION_MINOR,
			DRXK_VERSION_PATCH);

6205 6206 6207 6208 6209 6210 6211
		/*
		 * 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
		 */
6212

6213
		/* m_dvbt_rf_agc_cfg.speed = 3; */
6214 6215 6216 6217 6218 6219 6220 6221 6222 6223 6224 6225

		/* 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 */
6226
		status = mpegts_dto_init(state);
6227 6228
		if (status < 0)
			goto error;
6229
		status = mpegts_stop(state);
6230 6231
		if (status < 0)
			goto error;
6232
		status = mpegts_configure_polarity(state);
6233 6234
		if (status < 0)
			goto error;
6235
		status = mpegts_configure_pins(state, state->m_enable_mpeg_output);
6236 6237 6238
		if (status < 0)
			goto error;
		/* added: configure GPIO */
6239
		status = write_gpio(state);
6240 6241
		if (status < 0)
			goto error;
R
Ralph Metzler 已提交
6242

6243
		state->m_drxk_state = DRXK_STOPPED;
R
Ralph Metzler 已提交
6244

6245 6246
		if (state->m_b_power_down) {
			status = power_down_device(state);
6247
			if (status < 0)
6248
				goto error;
6249
			state->m_drxk_state = DRXK_POWERED_DOWN;
6250
		} else
6251
			state->m_drxk_state = DRXK_STOPPED;
6252 6253 6254

		/* Initialize the supported delivery systems */
		n = 0;
6255
		if (state->m_has_dvbc) {
6256 6257 6258 6259 6260
			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));
		}
6261
		if (state->m_has_dvbt) {
6262 6263 6264 6265
			state->frontend.ops.delsys[n++] = SYS_DVBT;
			strlcat(state->frontend.ops.info.name, " DVB-T",
				sizeof(state->frontend.ops.info.name));
		}
6266
		drxk_i2c_unlock(state);
R
Ralph Metzler 已提交
6267
	}
6268
error:
6269
	if (status < 0) {
6270
		state->m_drxk_state = DRXK_NO_DEV;
6271
		drxk_i2c_unlock(state);
6272
		pr_err("Error %d on %s\n", status, __func__);
6273
	}
R
Ralph Metzler 已提交
6274

6275
	return status;
R
Ralph Metzler 已提交
6276 6277
}

6278 6279 6280 6281 6282
static void load_firmware_cb(const struct firmware *fw,
			     void *context)
{
	struct drxk_state *state = context;

6283
	dprintk(1, ": %s\n", fw ? "firmware loaded" : "firmware not loaded");
6284
	if (!fw) {
6285
		pr_err("Could not load firmware file %s.\n",
6286
			state->microcode_name);
6287
		pr_info("Copy %s to your hotplug directory!\n",
6288 6289 6290 6291 6292 6293 6294 6295 6296 6297 6298 6299 6300 6301 6302 6303 6304 6305 6306
			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);
}

6307
static void drxk_release(struct dvb_frontend *fe)
R
Ralph Metzler 已提交
6308
{
6309 6310
	struct drxk_state *state = fe->demodulator_priv;

6311
	dprintk(1, "\n");
6312 6313 6314
	if (state->fw)
		release_firmware(state->fw);

R
Ralph Metzler 已提交
6315 6316 6317
	kfree(state);
}

6318
static int drxk_sleep(struct dvb_frontend *fe)
R
Ralph Metzler 已提交
6319
{
6320
	struct drxk_state *state = fe->demodulator_priv;
R
Ralph Metzler 已提交
6321

6322
	dprintk(1, "\n");
6323

6324
	if (state->m_drxk_state == DRXK_NO_DEV)
6325
		return -ENODEV;
6326
	if (state->m_drxk_state == DRXK_UNINITIALIZED)
6327 6328
		return 0;

6329
	shut_down(state);
R
Ralph Metzler 已提交
6330 6331 6332
	return 0;
}

6333
static int drxk_gate_ctrl(struct dvb_frontend *fe, int enable)
R
Ralph Metzler 已提交
6334 6335 6336
{
	struct drxk_state *state = fe->demodulator_priv;

6337
	dprintk(1, ": %s\n", enable ? "enable" : "disable");
6338

6339
	if (state->m_drxk_state == DRXK_NO_DEV)
6340 6341
		return -ENODEV;

R
Ralph Metzler 已提交
6342 6343 6344
	return ConfigureI2CBridge(state, enable ? true : false);
}

6345
static int drxk_set_parameters(struct dvb_frontend *fe)
R
Ralph Metzler 已提交
6346
{
6347
	struct dtv_frontend_properties *p = &fe->dtv_property_cache;
6348
	u32 delsys  = p->delivery_system, old_delsys;
R
Ralph Metzler 已提交
6349 6350 6351
	struct drxk_state *state = fe->demodulator_priv;
	u32 IF;

6352
	dprintk(1, "\n");
6353

6354
	if (state->m_drxk_state == DRXK_NO_DEV)
6355 6356
		return -ENODEV;

6357
	if (state->m_drxk_state == DRXK_UNINITIALIZED)
6358 6359
		return -EAGAIN;

6360
	if (!fe->ops.tuner_ops.get_if_frequency) {
6361
		pr_err("Error: get_if_frequency() not defined at tuner. Can't work without it!\n");
6362 6363 6364
		return -EINVAL;
	}

6365 6366 6367 6368 6369 6370
	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);
6371 6372

	old_delsys = state->props.delivery_system;
6373 6374
	state->props = *p;

6375
	if (old_delsys != delsys) {
6376
		shut_down(state);
6377 6378 6379
		switch (delsys) {
		case SYS_DVBC_ANNEX_A:
		case SYS_DVBC_ANNEX_C:
6380
			if (!state->m_has_dvbc)
6381
				return -EINVAL;
6382 6383
			state->m_itut_annex_c = (delsys == SYS_DVBC_ANNEX_C) ?
						true : false;
6384
			if (state->m_itut_annex_c)
6385
				setoperation_mode(state, OM_QAM_ITU_C);
6386
			else
6387
				setoperation_mode(state, OM_QAM_ITU_A);
6388
			break;
6389
		case SYS_DVBT:
6390
			if (!state->m_has_dvbt)
6391
				return -EINVAL;
6392
			setoperation_mode(state, OM_DVBT);
6393 6394
			break;
		default:
6395
			return -EINVAL;
6396
		}
6397
	}
6398 6399

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

6402
	/* After set_frontend, stats aren't available */
6403 6404 6405 6406 6407 6408 6409 6410 6411
	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;

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

R
Ralph Metzler 已提交
6414 6415 6416
	return 0;
}

6417 6418 6419
static int get_strength(struct drxk_state *state, u64 *strength)
{
	int status;
6420 6421
	struct s_cfg_agc   rf_agc, if_agc;
	u32          total_gain  = 0;
6422
	u32          atten      = 0;
6423
	u32          agc_range   = 0;
6424 6425 6426
	u16            scu_lvl  = 0;
	u16            scu_coc  = 0;
	/* FIXME: those are part of the tuner presets */
6427 6428
	u16 tuner_rf_gain         = 50; /* Default value on az6007 driver */
	u16 tuner_if_gain         = 40; /* Default value on az6007 driver */
6429 6430 6431

	*strength = 0;

6432 6433 6434 6435 6436 6437
	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;
6438
	} else {
6439 6440
		rf_agc = state->m_atv_rf_agc_cfg;
		if_agc = state->m_atv_if_agc_cfg;
6441 6442
	}

6443 6444
	if (rf_agc.ctrl_mode == DRXK_AGC_CTRL_AUTO) {
		/* SCU output_level */
6445 6446 6447 6448 6449 6450 6451 6452 6453 6454
		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)
6455
			rf_agc.output_level = scu_lvl + scu_coc;
6456
		else
6457
			rf_agc.output_level = 0xffff;
6458 6459

		/* Take RF gain into account */
6460
		total_gain += tuner_rf_gain;
6461 6462

		/* clip output value */
6463 6464 6465 6466
		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;
6467

6468 6469
		agc_range = (u32) (rf_agc.max_output_level - rf_agc.min_output_level);
		if (agc_range > 0) {
6470
			atten += 100UL *
6471 6472 6473
				((u32)(tuner_rf_gain)) *
				((u32)(rf_agc.output_level - rf_agc.min_output_level))
				/ agc_range;
6474 6475 6476
		}
	}

6477
	if (if_agc.ctrl_mode == DRXK_AGC_CTRL_AUTO) {
6478
		status = read16(state, SCU_RAM_AGC_IF_IACCU_HI__A,
6479
				&if_agc.output_level);
6480 6481 6482 6483
		if (status < 0)
			return status;

		status = read16(state, SCU_RAM_AGC_INGAIN_TGT_MIN__A,
6484
				&if_agc.top);
6485 6486 6487 6488
		if (status < 0)
			return status;

		/* Take IF gain into account */
6489
		total_gain += (u32) tuner_if_gain;
6490 6491

		/* clip output value */
6492 6493 6494 6495
		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;
6496

6497
		agc_range  = (u32)(if_agc.max_output_level - if_agc.min_output_level);
6498
		if (agc_range > 0) {
6499
			atten += 100UL *
6500 6501 6502
				((u32)(tuner_if_gain)) *
				((u32)(if_agc.output_level - if_agc.min_output_level))
				/ agc_range;
6503 6504 6505 6506 6507 6508 6509
		}
	}

	/*
	 * Convert to 0..65535 scale.
	 * If it can't be measured (AGC is disabled), just show 100%.
	 */
6510 6511
	if (total_gain > 0)
		*strength = (65535UL * atten / total_gain / 100);
6512 6513 6514 6515 6516 6517
	else
		*strength = 65535;

	return 0;
}

6518
static int drxk_get_stats(struct dvb_frontend *fe)
R
Ralph Metzler 已提交
6519
{
6520
	struct dtv_frontend_properties *c = &fe->dtv_property_cache;
R
Ralph Metzler 已提交
6521
	struct drxk_state *state = fe->demodulator_priv;
6522
	int status;
R
Ralph Metzler 已提交
6523
	u32 stat;
6524 6525 6526 6527 6528 6529 6530 6531
	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;
6532
	s32 cnr;
6533

6534
	if (state->m_drxk_state == DRXK_NO_DEV)
6535
		return -ENODEV;
6536
	if (state->m_drxk_state == DRXK_UNINITIALIZED)
6537 6538
		return -EAGAIN;

6539 6540
	/* get status */
	state->fe_status = 0;
6541
	get_lock_status(state, &stat);
6542
	if (stat == MPEG_LOCK)
6543
		state->fe_status |= 0x1f;
6544
	if (stat == FEC_LOCK)
6545
		state->fe_status |= 0x0f;
6546
	if (stat == DEMOD_LOCK)
6547 6548
		state->fe_status |= 0x07;

6549 6550 6551 6552 6553 6554 6555
	/*
	 * Estimate signal strength from AGC
	 */
	get_strength(state, &c->strength.stat[0].uvalue);
	c->strength.stat[0].scale = FE_SCALE_RELATIVE;


6556
	if (stat >= DEMOD_LOCK) {
6557
		get_signal_to_noise(state, &cnr);
6558 6559 6560 6561 6562 6563 6564 6565 6566 6567 6568 6569 6570 6571 6572 6573 6574 6575 6576 6577
		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 */

6578 6579 6580 6581 6582
	/*
	 * 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().
	 */
6583 6584 6585 6586 6587 6588 6589 6590 6591 6592 6593 6594 6595 6596 6597 6598 6599 6600 6601 6602 6603 6604 6605 6606 6607 6608 6609 6610 6611 6612 6613 6614 6615 6616 6617 6618 6619 6620 6621 6622 6623 6624 6625 6626 6627 6628 6629 6630 6631 6632 6633 6634 6635 6636 6637 6638 6639 6640 6641 6642 6643 6644 6645 6646 6647 6648 6649 6650 6651 6652 6653 6654

	/* 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 已提交
6655 6656 6657
	return 0;
}

6658 6659
static int drxk_read_signal_strength(struct dvb_frontend *fe,
				     u16 *strength)
R
Ralph Metzler 已提交
6660 6661
{
	struct drxk_state *state = fe->demodulator_priv;
6662
	struct dtv_frontend_properties *c = &fe->dtv_property_cache;
R
Ralph Metzler 已提交
6663

6664
	dprintk(1, "\n");
6665

6666
	if (state->m_drxk_state == DRXK_NO_DEV)
6667
		return -ENODEV;
6668
	if (state->m_drxk_state == DRXK_UNINITIALIZED)
6669 6670
		return -EAGAIN;

6671
	*strength = c->strength.stat[0].uvalue;
R
Ralph Metzler 已提交
6672 6673 6674 6675 6676 6677 6678 6679
	return 0;
}

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

6680
	dprintk(1, "\n");
6681

6682
	if (state->m_drxk_state == DRXK_NO_DEV)
6683
		return -ENODEV;
6684
	if (state->m_drxk_state == DRXK_UNINITIALIZED)
6685 6686
		return -EAGAIN;

6687
	get_signal_to_noise(state, &snr2);
6688 6689 6690 6691

	/* No negative SNR, clip to zero */
	if (snr2 < 0)
		snr2 = 0;
6692
	*snr = snr2 & 0xffff;
R
Ralph Metzler 已提交
6693 6694 6695 6696 6697 6698 6699 6700
	return 0;
}

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

6701
	dprintk(1, "\n");
6702

6703
	if (state->m_drxk_state == DRXK_NO_DEV)
6704
		return -ENODEV;
6705
	if (state->m_drxk_state == DRXK_UNINITIALIZED)
6706 6707
		return -EAGAIN;

6708
	dvbtqam_get_acc_pkt_err(state, &err);
R
Ralph Metzler 已提交
6709 6710 6711 6712
	*ucblocks = (u32) err;
	return 0;
}

6713 6714
static int drxk_get_tune_settings(struct dvb_frontend *fe,
				  struct dvb_frontend_tune_settings *sets)
R
Ralph Metzler 已提交
6715
{
6716
	struct drxk_state *state = fe->demodulator_priv;
6717
	struct dtv_frontend_properties *p = &fe->dtv_property_cache;
6718 6719

	dprintk(1, "\n");
6720

6721
	if (state->m_drxk_state == DRXK_NO_DEV)
6722
		return -ENODEV;
6723
	if (state->m_drxk_state == DRXK_UNINITIALIZED)
6724 6725
		return -EAGAIN;

6726 6727 6728
	switch (p->delivery_system) {
	case SYS_DVBC_ANNEX_A:
	case SYS_DVBC_ANNEX_C:
6729
	case SYS_DVBT:
6730 6731 6732 6733 6734 6735 6736
		sets->min_delay_ms = 3000;
		sets->max_drift = 0;
		sets->step_size = 0;
		return 0;
	default:
		return -EINVAL;
	}
R
Ralph Metzler 已提交
6737 6738
}

6739 6740
static struct dvb_frontend_ops drxk_ops = {
	/* .delsys will be filled dynamically */
R
Ralph Metzler 已提交
6741
	.info = {
6742 6743 6744 6745 6746 6747 6748 6749 6750 6751 6752 6753 6754 6755 6756 6757 6758 6759 6760
		.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 已提交
6761 6762
	.i2c_gate_ctrl = drxk_gate_ctrl,

6763
	.set_frontend = drxk_set_parameters,
6764
	.get_tune_settings = drxk_get_tune_settings,
R
Ralph Metzler 已提交
6765 6766 6767 6768 6769 6770 6771

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

6772
struct dvb_frontend *drxk_attach(const struct drxk_config *config,
6773
				 struct i2c_adapter *i2c)
R
Ralph Metzler 已提交
6774
{
6775
	struct dtv_frontend_properties *p;
R
Ralph Metzler 已提交
6776
	struct drxk_state *state = NULL;
6777
	u8 adr = config->adr;
6778
	int status;
R
Ralph Metzler 已提交
6779

6780
	dprintk(1, "\n");
6781
	state = kzalloc(sizeof(struct drxk_state), GFP_KERNEL);
R
Ralph Metzler 已提交
6782 6783 6784
	if (!state)
		return NULL;

6785 6786
	state->i2c = i2c;
	state->demod_address = adr;
6787
	state->single_master = config->single_master;
6788
	state->microcode_name = config->microcode_name;
6789
	state->qam_demod_parameter_count = config->qam_demod_parameter_count;
6790
	state->no_i2c_bridge = config->no_i2c_bridge;
6791 6792
	state->antenna_gpio = config->antenna_gpio;
	state->antenna_dvbt = config->antenna_dvbt;
6793
	state->m_chunk_size = config->chunk_size;
6794
	state->enable_merr_cfg = config->enable_merr_cfg;
6795

6796
	if (config->dynamic_clk) {
6797 6798
		state->m_dvbt_static_clk = 0;
		state->m_dvbc_static_clk = 0;
6799
	} else {
6800 6801
		state->m_dvbt_static_clk = 1;
		state->m_dvbc_static_clk = 1;
6802 6803
	}

6804 6805

	if (config->mpeg_out_clk_strength)
6806
		state->m_ts_clockk_strength = config->mpeg_out_clk_strength & 0x07;
6807
	else
6808
		state->m_ts_clockk_strength = 0x06;
6809

6810
	if (config->parallel_ts)
6811
		state->m_enable_parallel = true;
6812
	else
6813
		state->m_enable_parallel = false;
6814

6815
	/* NOTE: as more UIO bits will be used, add them to the mask */
6816
	state->uio_mask = config->antenna_gpio;
6817 6818 6819

	/* Default gpio to DVB-C */
	if (!state->antenna_dvbt && state->antenna_gpio)
6820
		state->m_gpio |= state->antenna_gpio;
6821
	else
6822
		state->m_gpio &= ~state->antenna_gpio;
R
Ralph Metzler 已提交
6823 6824 6825

	mutex_init(&state->mutex);

6826 6827
	memcpy(&state->frontend.ops, &drxk_ops, sizeof(drxk_ops));
	state->frontend.demodulator_priv = state;
R
Ralph Metzler 已提交
6828 6829

	init_state(state);
6830

6831 6832
	/* Load firmware and initialize DRX-K */
	if (state->microcode_name) {
6833
		const struct firmware *fw = NULL;
6834

6835 6836 6837 6838 6839
		status = request_firmware(&fw, state->microcode_name,
					  state->i2c->dev.parent);
		if (status < 0)
			fw = NULL;
		load_firmware_cb(fw, state);
6840 6841
	} else if (init_drxk(state) < 0)
		goto error;
6842

6843 6844 6845 6846 6847 6848 6849 6850 6851 6852 6853 6854 6855 6856 6857 6858 6859 6860 6861 6862 6863

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

6864
	pr_info("frontend initialized.\n");
6865
	return &state->frontend;
R
Ralph Metzler 已提交
6866 6867

error:
6868
	pr_err("not found\n");
R
Ralph Metzler 已提交
6869 6870 6871
	kfree(state);
	return NULL;
}
6872
EXPORT_SYMBOL(drxk_attach);
R
Ralph Metzler 已提交
6873 6874 6875 6876

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