drxk_hard.c 177.1 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
static unsigned int debug;
module_param(debug, int, 0644);
MODULE_PARM_DESC(debug, "enable debug messages");

169 170 171
#define dprintk(level, fmt, arg...) do {				\
if (debug >= level)							\
	printk(KERN_DEBUG KBUILD_MODNAME ": %s " fmt, __func__, ##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);
1031
	if (!powerdown_cmd) {
R
Ralph Metzler 已提交
1032
		/* 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) {
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) {
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");

1395
	if (!enable) {
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
	fec_oc_reg_mode &= (~FEC_OC_MODE_PARITY__M);
	fec_oc_reg_ipr_mode &= (~FEC_OC_IPR_MODE_MVAL_DIS_PAR__M);
2015
	if (state->m_insert_rs_byte) {
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
	fec_oc_reg_ipr_mode &= (~(FEC_OC_IPR_MODE_SERIAL__M));
2026
	if (!state->m_enable_parallel) {
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
	fec_oc_reg_ipr_invert &= (~(invert_data_mask));
2139
	if (state->m_invert_data)
2140 2141
		fec_oc_reg_ipr_invert |= invert_data_mask;
	fec_oc_reg_ipr_invert &= (~(FEC_OC_IPR_INVERT_MERR__M));
2142
	if (state->m_invert_err)
2143 2144
		fec_oc_reg_ipr_invert |= FEC_OC_IPR_INVERT_MERR__M;
	fec_oc_reg_ipr_invert &= (~(FEC_OC_IPR_INVERT_MSTRT__M));
2145
	if (state->m_invert_str)
2146 2147
		fec_oc_reg_ipr_invert |= FEC_OC_IPR_INVERT_MSTRT__M;
	fec_oc_reg_ipr_invert &= (~(FEC_OC_IPR_INVERT_MVAL__M));
2148
	if (state->m_invert_val)
2149 2150
		fec_oc_reg_ipr_invert |= FEC_OC_IPR_INVERT_MVAL__M;
	fec_oc_reg_ipr_invert &= (~(FEC_OC_IPR_INVERT_MCLK__M));
2151
	if (state->m_invert_clk)
2152 2153 2154
		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;
2229
		}
R
Ralph Metzler 已提交
2230

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

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

2243
		break;
R
Ralph Metzler 已提交
2244

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

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

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

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

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

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

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

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

#define SCU_RAM_AGC_KI_INV_IF_POL__M 0x2000

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

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

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

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

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

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

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

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

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

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

2375
	case DRXK_AGC_CTRL_USER:
R
Ralph Metzler 已提交
2376

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

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

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

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

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

2409
	case DRXK_AGC_CTRL_OFF:
R
Ralph Metzler 已提交
2410

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

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

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

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

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

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

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

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

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

	return status;
}

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

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

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

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

	/* Check input data for MER */

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

	return status;
}

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

}

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

3128 3129 3130
	status = write16(state, SCU_RAM_AGC_KI_MAXMINGAIN_TH__A, 20);
	if (status < 0)
		goto error;
3131
	status = write16(state, SCU_RAM_AGC_CLP_SUM_MIN__A, clp_sum_min);
3132 3133
	if (status < 0)
		goto error;
3134
	status = write16(state, SCU_RAM_AGC_SNS_SUM_MIN__A, sns_sum_min);
3135 3136
	if (status < 0)
		goto error;
3137
	status = write16(state, SCU_RAM_AGC_CLP_DIR_TO__A, clp_dir_to);
3138 3139
	if (status < 0)
		goto error;
3140
	status = write16(state, SCU_RAM_AGC_SNS_DIR_TO__A, sns_dir_to);
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 3184
	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 已提交
3185

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

3369 3370
	int status;

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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


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

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

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

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

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


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

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

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

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

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

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

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

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

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

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

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


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

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

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

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

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

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

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

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

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

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

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


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

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

	return status;
}


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

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

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

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

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

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

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

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

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

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

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


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

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

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

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

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

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

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

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

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

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

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

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

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

4274
	dprintk(1, "\n");
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 4313
	/* 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;
4314

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

4325
	/* QAM Slicer Settings */
4326 4327
	status = write16(state, SCU_RAM_QAM_SL_SIG_POWER__A,
			 DRXK_QAM_SL_SIG_POWER_QAM16);
4328 4329
	if (status < 0)
		goto error;
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 4355
	/* 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;
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 4392
	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;
4393 4394


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

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

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


4427
	/* QAM FSM Tracking Parameters */
4428

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

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

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

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

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

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

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

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

4521
	/* QAM Slicer Settings */
4522

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


4529
	/* QAM Loop Controller Coeficients */
4530

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


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

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

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


4626
	/* QAM FSM Tracking Parameters */
4627

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

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

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

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

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

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

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


4723
	/* QAM Loop Controller Coeficients */
4724

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


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

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

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


4820
	/* QAM FSM Tracking Parameters */
4821

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

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

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

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

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

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

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


4912
	/* QAM Slicer Settings */
4913

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


4920
	/* QAM Loop Controller Coeficients */
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 4982
	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;
4983 4984


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

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

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

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

5017
	/* QAM FSM Tracking Parameters */
5018

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

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

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

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

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

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

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

5108
	/* QAM Slicer Settings */
5109

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


5116
	/* QAM Loop Controller Coeficients */
5117

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


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

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

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


5213
	/* QAM FSM Tracking Parameters */
5214

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


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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

5730
	/* Setup IQM */
5731

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

	if (!state->antenna_gpio)
		return 0;

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

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


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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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 = false;
		state->m_dvbc_static_clk = false;
6799
	} else {
6800 6801
		state->m_dvbt_static_clk = true;
		state->m_dvbc_static_clk = true;
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");