dib9000.c 70.6 KB
Newer Older
1 2 3 4 5 6 7 8 9
/*
 * Linux-DVB Driver for DiBcom's DiB9000 and demodulator-family.
 *
 * Copyright (C) 2005-10 DiBcom (http://www.dibcom.fr/)
 *
 * This program is free software; you can redistribute it and/or
 *	modify it under the terms of the GNU General Public License as
 *	published by the Free Software Foundation, version 2.
 */
10 11 12

#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt

13 14 15 16
#include <linux/kernel.h>
#include <linux/i2c.h>
#include <linux/mutex.h>

17 18
#include <media/dvb_math.h>
#include <media/dvb_frontend.h>
19 20 21 22 23 24 25 26

#include "dib9000.h"
#include "dibx000_common.h"

static int debug;
module_param(debug, int, 0644);
MODULE_PARM_DESC(debug, "turn on debugging (default: 0)");

27 28 29 30 31 32
#define dprintk(fmt, arg...) do {					\
	if (debug)							\
		printk(KERN_DEBUG pr_fmt("%s: " fmt),			\
		       __func__, ##arg);				\
} while (0)

33 34 35 36 37
#define MAX_NUMBER_OF_FRONTENDS 6

struct i2c_device {
	struct i2c_adapter *i2c_adap;
	u8 i2c_addr;
38 39
	u8 *i2c_read_buffer;
	u8 *i2c_write_buffer;
40 41
};

42 43 44 45 46 47 48 49 50
struct dib9000_pid_ctrl {
#define DIB9000_PID_FILTER_CTRL 0
#define DIB9000_PID_FILTER      1
	u8 cmd;
	u8 id;
	u16 pid;
	u8 onoff;
};

51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
struct dib9000_state {
	struct i2c_device i2c;

	struct dibx000_i2c_master i2c_master;
	struct i2c_adapter tuner_adap;
	struct i2c_adapter component_bus;

	u16 revision;
	u8 reg_offs;

	enum frontend_tune_state tune_state;
	u32 status;
	struct dvb_frontend_parametersContext channel_status;

	u8 fe_id;

#define DIB9000_GPIO_DEFAULT_DIRECTIONS 0xffff
	u16 gpio_dir;
#define DIB9000_GPIO_DEFAULT_VALUES     0x0000
	u16 gpio_val;
#define DIB9000_GPIO_DEFAULT_PWM_POS    0xffff
	u16 gpio_pwm_pos;

	union {			/* common for all chips */
		struct {
			u8 mobile_mode:1;
		} host;

		struct {
			struct dib9000_fe_memory_map {
				u16 addr;
				u16 size;
			} fe_mm[18];
			u8 memcmd;

86 87
			struct mutex mbx_if_lock;	/* to protect read/write operations */
			struct mutex mbx_lock;	/* to protect the whole mailbox handling */
88

89 90
			struct mutex mem_lock;	/* to protect the memory accesses */
			struct mutex mem_mbx_lock;	/* to protect the memory-based mailbox */
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106

#define MBX_MAX_WORDS (256 - 200 - 2)
#define DIB9000_MSG_CACHE_SIZE 2
			u16 message_cache[DIB9000_MSG_CACHE_SIZE][MBX_MAX_WORDS];
			u8 fw_is_running;
		} risc;
	} platform;

	union {			/* common for all platforms */
		struct {
			struct dib9000_config cfg;
		} d9;
	} chip;

	struct dvb_frontend *fe[MAX_NUMBER_OF_FRONTENDS];
	u16 component_bus_speed;
107 108 109 110 111

	/* for the I2C transfer */
	struct i2c_msg msg[2];
	u8 i2c_write_buffer[255];
	u8 i2c_read_buffer[255];
112
	struct mutex demod_lock;
113 114 115
	u8 get_frontend_internal;
	struct dib9000_pid_ctrl pid_ctrl[10];
	s8 pid_ctrl_index; /* -1: empty list; -2: do not use the list */
116 117
};

118
static const u32 fe_info[44] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
119
	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
120
	0, 0, 0, 0, 0, 0, 0, 0
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210
};

enum dib9000_power_mode {
	DIB9000_POWER_ALL = 0,

	DIB9000_POWER_NO,
	DIB9000_POWER_INTERF_ANALOG_AGC,
	DIB9000_POWER_COR4_DINTLV_ICIRM_EQUAL_CFROD,
	DIB9000_POWER_COR4_CRY_ESRAM_MOUT_NUD,
	DIB9000_POWER_INTERFACE_ONLY,
};

enum dib9000_out_messages {
	OUT_MSG_HBM_ACK,
	OUT_MSG_HOST_BUF_FAIL,
	OUT_MSG_REQ_VERSION,
	OUT_MSG_BRIDGE_I2C_W,
	OUT_MSG_BRIDGE_I2C_R,
	OUT_MSG_BRIDGE_APB_W,
	OUT_MSG_BRIDGE_APB_R,
	OUT_MSG_SCAN_CHANNEL,
	OUT_MSG_MONIT_DEMOD,
	OUT_MSG_CONF_GPIO,
	OUT_MSG_DEBUG_HELP,
	OUT_MSG_SUBBAND_SEL,
	OUT_MSG_ENABLE_TIME_SLICE,
	OUT_MSG_FE_FW_DL,
	OUT_MSG_FE_CHANNEL_SEARCH,
	OUT_MSG_FE_CHANNEL_TUNE,
	OUT_MSG_FE_SLEEP,
	OUT_MSG_FE_SYNC,
	OUT_MSG_CTL_MONIT,

	OUT_MSG_CONF_SVC,
	OUT_MSG_SET_HBM,
	OUT_MSG_INIT_DEMOD,
	OUT_MSG_ENABLE_DIVERSITY,
	OUT_MSG_SET_OUTPUT_MODE,
	OUT_MSG_SET_PRIORITARY_CHANNEL,
	OUT_MSG_ACK_FRG,
	OUT_MSG_INIT_PMU,
};

enum dib9000_in_messages {
	IN_MSG_DATA,
	IN_MSG_FRAME_INFO,
	IN_MSG_CTL_MONIT,
	IN_MSG_ACK_FREE_ITEM,
	IN_MSG_DEBUG_BUF,
	IN_MSG_MPE_MONITOR,
	IN_MSG_RAWTS_MONITOR,
	IN_MSG_END_BRIDGE_I2C_RW,
	IN_MSG_END_BRIDGE_APB_RW,
	IN_MSG_VERSION,
	IN_MSG_END_OF_SCAN,
	IN_MSG_MONIT_DEMOD,
	IN_MSG_ERROR,
	IN_MSG_FE_FW_DL_DONE,
	IN_MSG_EVENT,
	IN_MSG_ACK_CHANGE_SVC,
	IN_MSG_HBM_PROF,
};

/* memory_access requests */
#define FE_MM_W_CHANNEL                   0
#define FE_MM_W_FE_INFO                   1
#define FE_MM_RW_SYNC                     2

#define FE_SYNC_CHANNEL          1
#define FE_SYNC_W_GENERIC_MONIT	 2
#define FE_SYNC_COMPONENT_ACCESS 3

#define FE_MM_R_CHANNEL_SEARCH_STATE      3
#define FE_MM_R_CHANNEL_UNION_CONTEXT     4
#define FE_MM_R_FE_INFO                   5
#define FE_MM_R_FE_MONITOR                6

#define FE_MM_W_CHANNEL_HEAD              7
#define FE_MM_W_CHANNEL_UNION             8
#define FE_MM_W_CHANNEL_CONTEXT           9
#define FE_MM_R_CHANNEL_UNION            10
#define FE_MM_R_CHANNEL_CONTEXT          11
#define FE_MM_R_CHANNEL_TUNE_STATE       12

#define FE_MM_R_GENERIC_MONITORING_SIZE	 13
#define FE_MM_W_GENERIC_MONITORING	     14
#define FE_MM_R_GENERIC_MONITORING	     15

#define FE_MM_W_COMPONENT_ACCESS         16
#define FE_MM_RW_COMPONENT_ACCESS_BUFFER 17
211
static int dib9000_risc_apb_access_read(struct dib9000_state *state, u32 address, u16 attribute, const u8 * tx, u32 txlen, u8 * b, u32 len);
212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235
static int dib9000_risc_apb_access_write(struct dib9000_state *state, u32 address, u16 attribute, const u8 * b, u32 len);

static u16 to_fw_output_mode(u16 mode)
{
	switch (mode) {
	case OUTMODE_HIGH_Z:
		return 0;
	case OUTMODE_MPEG2_PAR_GATED_CLK:
		return 4;
	case OUTMODE_MPEG2_PAR_CONT_CLK:
		return 8;
	case OUTMODE_MPEG2_SERIAL:
		return 16;
	case OUTMODE_DIVERSITY:
		return 128;
	case OUTMODE_MPEG2_FIFO:
		return 2;
	case OUTMODE_ANALOG_ADC:
		return 1;
	default:
		return 0;
	}
}

236
static int dib9000_read16_attr(struct dib9000_state *state, u16 reg, u8 *b, u32 len, u16 attribute)
237 238 239 240 241 242 243 244
{
	u32 chunk_size = 126;
	u32 l;
	int ret;

	if (state->platform.risc.fw_is_running && (reg < 1024))
		return dib9000_risc_apb_access_read(state, reg, attribute, NULL, 0, b, len);

245 246 247 248 249 250 251 252 253 254 255 256 257
	memset(state->msg, 0, 2 * sizeof(struct i2c_msg));
	state->msg[0].addr = state->i2c.i2c_addr >> 1;
	state->msg[0].flags = 0;
	state->msg[0].buf = state->i2c_write_buffer;
	state->msg[0].len = 2;
	state->msg[1].addr = state->i2c.i2c_addr >> 1;
	state->msg[1].flags = I2C_M_RD;
	state->msg[1].buf = b;
	state->msg[1].len = len;

	state->i2c_write_buffer[0] = reg >> 8;
	state->i2c_write_buffer[1] = reg & 0xff;

258
	if (attribute & DATA_BUS_ACCESS_MODE_8BIT)
259
		state->i2c_write_buffer[0] |= (1 << 5);
260
	if (attribute & DATA_BUS_ACCESS_MODE_NO_ADDRESS_INCREMENT)
261
		state->i2c_write_buffer[0] |= (1 << 4);
262 263 264

	do {
		l = len < chunk_size ? len : chunk_size;
265 266 267
		state->msg[1].len = l;
		state->msg[1].buf = b;
		ret = i2c_transfer(state->i2c.i2c_adap, state->msg, 2) != 2 ? -EREMOTEIO : 0;
268
		if (ret != 0) {
269
			dprintk("i2c read error on %d\n", reg);
270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285
			return -EREMOTEIO;
		}

		b += l;
		len -= l;

		if (!(attribute & DATA_BUS_ACCESS_MODE_NO_ADDRESS_INCREMENT))
			reg += l / 2;
	} while ((ret == 0) && len);

	return 0;
}

static u16 dib9000_i2c_read16(struct i2c_device *i2c, u16 reg)
{
	struct i2c_msg msg[2] = {
286 287 288 289
		{.addr = i2c->i2c_addr >> 1, .flags = 0,
			.buf = i2c->i2c_write_buffer, .len = 2},
		{.addr = i2c->i2c_addr >> 1, .flags = I2C_M_RD,
			.buf = i2c->i2c_read_buffer, .len = 2},
290 291
	};

292 293 294
	i2c->i2c_write_buffer[0] = reg >> 8;
	i2c->i2c_write_buffer[1] = reg & 0xff;

295
	if (i2c_transfer(i2c->i2c_adap, msg, 2) != 2) {
296
		dprintk("read register %x error\n", reg);
297 298 299
		return 0;
	}

300
	return (i2c->i2c_read_buffer[0] << 8) | i2c->i2c_read_buffer[1];
301 302 303 304
}

static inline u16 dib9000_read_word(struct dib9000_state *state, u16 reg)
{
305
	if (dib9000_read16_attr(state, reg, state->i2c_read_buffer, 2, 0) != 0)
306
		return 0;
307
	return (state->i2c_read_buffer[0] << 8) | state->i2c_read_buffer[1];
308 309 310 311
}

static inline u16 dib9000_read_word_attr(struct dib9000_state *state, u16 reg, u16 attribute)
{
312 313
	if (dib9000_read16_attr(state, reg, state->i2c_read_buffer, 2,
				attribute) != 0)
314
		return 0;
315
	return (state->i2c_read_buffer[0] << 8) | state->i2c_read_buffer[1];
316 317 318 319
}

#define dib9000_read16_noinc_attr(state, reg, b, len, attribute) dib9000_read16_attr(state, reg, b, len, (attribute) | DATA_BUS_ACCESS_MODE_NO_ADDRESS_INCREMENT)

320
static int dib9000_write16_attr(struct dib9000_state *state, u16 reg, const u8 *buf, u32 len, u16 attribute)
321 322 323 324 325 326 327
{
	u32 chunk_size = 126;
	u32 l;
	int ret;

	if (state->platform.risc.fw_is_running && (reg < 1024)) {
		if (dib9000_risc_apb_access_write
328
		    (state, reg, DATA_BUS_ACCESS_MODE_16BIT | DATA_BUS_ACCESS_MODE_NO_ADDRESS_INCREMENT | attribute, buf, len) != 0)
329 330 331 332
			return -EINVAL;
		return 0;
	}

333 334 335 336 337 338 339 340
	memset(&state->msg[0], 0, sizeof(struct i2c_msg));
	state->msg[0].addr = state->i2c.i2c_addr >> 1;
	state->msg[0].flags = 0;
	state->msg[0].buf = state->i2c_write_buffer;
	state->msg[0].len = len + 2;

	state->i2c_write_buffer[0] = (reg >> 8) & 0xff;
	state->i2c_write_buffer[1] = (reg) & 0xff;
341 342

	if (attribute & DATA_BUS_ACCESS_MODE_8BIT)
343
		state->i2c_write_buffer[0] |= (1 << 5);
344
	if (attribute & DATA_BUS_ACCESS_MODE_NO_ADDRESS_INCREMENT)
345
		state->i2c_write_buffer[0] |= (1 << 4);
346 347 348

	do {
		l = len < chunk_size ? len : chunk_size;
349 350
		state->msg[0].len = l + 2;
		memcpy(&state->i2c_write_buffer[2], buf, l);
351

352
		ret = i2c_transfer(state->i2c.i2c_adap, state->msg, 1) != 1 ? -EREMOTEIO : 0;
353 354 355 356 357 358 359 360 361 362 363 364 365 366

		buf += l;
		len -= l;

		if (!(attribute & DATA_BUS_ACCESS_MODE_NO_ADDRESS_INCREMENT))
			reg += l / 2;
	} while ((ret == 0) && len);

	return ret;
}

static int dib9000_i2c_write16(struct i2c_device *i2c, u16 reg, u16 val)
{
	struct i2c_msg msg = {
367 368
		.addr = i2c->i2c_addr >> 1, .flags = 0,
		.buf = i2c->i2c_write_buffer, .len = 4
369 370
	};

371 372 373 374 375
	i2c->i2c_write_buffer[0] = (reg >> 8) & 0xff;
	i2c->i2c_write_buffer[1] = reg & 0xff;
	i2c->i2c_write_buffer[2] = (val >> 8) & 0xff;
	i2c->i2c_write_buffer[3] = val & 0xff;

376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407
	return i2c_transfer(i2c->i2c_adap, &msg, 1) != 1 ? -EREMOTEIO : 0;
}

static inline int dib9000_write_word(struct dib9000_state *state, u16 reg, u16 val)
{
	u8 b[2] = { val >> 8, val & 0xff };
	return dib9000_write16_attr(state, reg, b, 2, 0);
}

static inline int dib9000_write_word_attr(struct dib9000_state *state, u16 reg, u16 val, u16 attribute)
{
	u8 b[2] = { val >> 8, val & 0xff };
	return dib9000_write16_attr(state, reg, b, 2, attribute);
}

#define dib9000_write(state, reg, buf, len) dib9000_write16_attr(state, reg, buf, len, 0)
#define dib9000_write16_noinc(state, reg, buf, len) dib9000_write16_attr(state, reg, buf, len, DATA_BUS_ACCESS_MODE_NO_ADDRESS_INCREMENT)
#define dib9000_write16_noinc_attr(state, reg, buf, len, attribute) dib9000_write16_attr(state, reg, buf, len, DATA_BUS_ACCESS_MODE_NO_ADDRESS_INCREMENT | (attribute))

#define dib9000_mbx_send(state, id, data, len) dib9000_mbx_send_attr(state, id, data, len, 0)
#define dib9000_mbx_get_message(state, id, msg, len) dib9000_mbx_get_message_attr(state, id, msg, len, 0)

#define MAC_IRQ      (1 << 1)
#define IRQ_POL_MSK  (1 << 4)

#define dib9000_risc_mem_read_chunks(state, b, len) dib9000_read16_attr(state, 1063, b, len, DATA_BUS_ACCESS_MODE_8BIT | DATA_BUS_ACCESS_MODE_NO_ADDRESS_INCREMENT)
#define dib9000_risc_mem_write_chunks(state, buf, len) dib9000_write16_attr(state, 1063, buf, len, DATA_BUS_ACCESS_MODE_8BIT | DATA_BUS_ACCESS_MODE_NO_ADDRESS_INCREMENT)

static void dib9000_risc_mem_setup_cmd(struct dib9000_state *state, u32 addr, u32 len, u8 reading)
{
	u8 b[14] = { 0 };

408 409
/*      dprintk("%d memcmd: %d %d %d\n", state->fe_id, addr, addr+len, len); */
/*      b[0] = 0 << 7; */
410 411
	b[1] = 1;

412 413 414
/*      b[2] = 0; */
/*      b[3] = 0; */
	b[4] = (u8) (addr >> 8);
415 416
	b[5] = (u8) (addr & 0xff);

417 418 419
/*      b[10] = 0; */
/*      b[11] = 0; */
	b[12] = (u8) (addr >> 8);
420 421 422
	b[13] = (u8) (addr & 0xff);

	addr += len;
423 424 425
/*      b[6] = 0; */
/*      b[7] = 0; */
	b[8] = (u8) (addr >> 8);
426 427 428 429 430 431 432 433 434 435 436 437 438
	b[9] = (u8) (addr & 0xff);

	dib9000_write(state, 1056, b, 14);
	if (reading)
		dib9000_write_word(state, 1056, (1 << 15) | 1);
	state->platform.risc.memcmd = -1;	/* if it was called directly reset it - to force a future setup-call to set it */
}

static void dib9000_risc_mem_setup(struct dib9000_state *state, u8 cmd)
{
	struct dib9000_fe_memory_map *m = &state->platform.risc.fe_mm[cmd & 0x7f];
	/* decide whether we need to "refresh" the memory controller */
	if (state->platform.risc.memcmd == cmd &&	/* same command */
439
	    !(cmd & 0x80 && m->size < 67))	/* and we do not want to read something with less than 67 bytes looping - working around a bug in the memory controller */
440 441 442 443 444 445 446 447 448 449
		return;
	dib9000_risc_mem_setup_cmd(state, m->addr, m->size, cmd & 0x80);
	state->platform.risc.memcmd = cmd;
}

static int dib9000_risc_mem_read(struct dib9000_state *state, u8 cmd, u8 * b, u16 len)
{
	if (!state->platform.risc.fw_is_running)
		return -EIO;

450
	if (mutex_lock_interruptible(&state->platform.risc.mem_lock) < 0) {
451
		dprintk("could not get the lock\n");
452 453
		return -EINTR;
	}
454 455
	dib9000_risc_mem_setup(state, cmd | 0x80);
	dib9000_risc_mem_read_chunks(state, b, len);
456
	mutex_unlock(&state->platform.risc.mem_lock);
457 458 459 460 461 462 463 464 465
	return 0;
}

static int dib9000_risc_mem_write(struct dib9000_state *state, u8 cmd, const u8 * b)
{
	struct dib9000_fe_memory_map *m = &state->platform.risc.fe_mm[cmd];
	if (!state->platform.risc.fw_is_running)
		return -EIO;

466
	if (mutex_lock_interruptible(&state->platform.risc.mem_lock) < 0) {
467
		dprintk("could not get the lock\n");
468 469
		return -EINTR;
	}
470 471
	dib9000_risc_mem_setup(state, cmd);
	dib9000_risc_mem_write_chunks(state, b, m->size);
472
	mutex_unlock(&state->platform.risc.mem_lock);
473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489
	return 0;
}

static int dib9000_firmware_download(struct dib9000_state *state, u8 risc_id, u16 key, const u8 * code, u32 len)
{
	u16 offs;

	if (risc_id == 1)
		offs = 16;
	else
		offs = 0;

	/* config crtl reg */
	dib9000_write_word(state, 1024 + offs, 0x000f);
	dib9000_write_word(state, 1025 + offs, 0);
	dib9000_write_word(state, 1031 + offs, key);

490
	dprintk("going to download %dB of microcode\n", len);
491
	if (dib9000_write16_noinc(state, 1026 + offs, (u8 *) code, (u16) len) != 0) {
492
		dprintk("error while downloading microcode for RISC %c\n", 'A' + risc_id);
493 494 495
		return -EIO;
	}

496
	dprintk("Microcode for RISC %c loaded\n", 'A' + risc_id);
497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521

	return 0;
}

static int dib9000_mbx_host_init(struct dib9000_state *state, u8 risc_id)
{
	u16 mbox_offs;
	u16 reset_reg;
	u16 tries = 1000;

	if (risc_id == 1)
		mbox_offs = 16;
	else
		mbox_offs = 0;

	/* Reset mailbox  */
	dib9000_write_word(state, 1027 + mbox_offs, 0x8000);

	/* Read reset status */
	do {
		reset_reg = dib9000_read_word(state, 1027 + mbox_offs);
		msleep(100);
	} while ((reset_reg & 0x8000) && --tries);

	if (reset_reg & 0x8000) {
522
		dprintk("MBX: init ERROR, no response from RISC %c\n", 'A' + risc_id);
523 524
		return -EIO;
	}
525
	dprintk("MBX: initialized\n");
526 527 528 529 530 531
	return 0;
}

#define MAX_MAILBOX_TRY 100
static int dib9000_mbx_send_attr(struct dib9000_state *state, u8 id, u16 * data, u8 len, u16 attr)
{
532
	u8 *d, b[2];
533 534 535
	u16 tmp;
	u16 size;
	u32 i;
536
	int ret = 0;
537 538 539 540

	if (!state->platform.risc.fw_is_running)
		return -EINVAL;

541
	if (mutex_lock_interruptible(&state->platform.risc.mbx_if_lock) < 0) {
542
		dprintk("could not get the lock\n");
543 544
		return -EINTR;
	}
545 546 547 548
	tmp = MAX_MAILBOX_TRY;
	do {
		size = dib9000_read_word_attr(state, 1043, attr) & 0xff;
		if ((size + len + 1) > MBX_MAX_WORDS && --tmp) {
549
			dprintk("MBX: RISC mbx full, retrying\n");
550 551 552 553 554
			msleep(100);
		} else
			break;
	} while (1);

555
	/*dprintk( "MBX: size: %d\n", size); */
556 557 558 559 560 561

	if (tmp == 0) {
		ret = -EINVAL;
		goto out;
	}
#ifdef DUMP_MSG
562
	dprintk("--> %02x %d %*ph\n", id, len + 1, len, data);
563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583
#endif

	/* byte-order conversion - works on big (where it is not necessary) or little endian */
	d = (u8 *) data;
	for (i = 0; i < len; i++) {
		tmp = data[i];
		*d++ = tmp >> 8;
		*d++ = tmp & 0xff;
	}

	/* write msg */
	b[0] = id;
	b[1] = len + 1;
	if (dib9000_write16_noinc_attr(state, 1045, b, 2, attr) != 0 || dib9000_write16_noinc_attr(state, 1045, (u8 *) data, len * 2, attr) != 0) {
		ret = -EIO;
		goto out;
	}

	/* update register nb_mes_in_RX */
	ret = (u8) dib9000_write_word_attr(state, 1043, 1 << 14, attr);

584
out:
585
	mutex_unlock(&state->platform.risc.mbx_if_lock);
586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602

	return ret;
}

static u8 dib9000_mbx_read(struct dib9000_state *state, u16 * data, u8 risc_id, u16 attr)
{
#ifdef DUMP_MSG
	u16 *d = data;
#endif

	u16 tmp, i;
	u8 size;
	u8 mc_base;

	if (!state->platform.risc.fw_is_running)
		return 0;

603
	if (mutex_lock_interruptible(&state->platform.risc.mbx_if_lock) < 0) {
604
		dprintk("could not get the lock\n");
605 606
		return 0;
	}
607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629
	if (risc_id == 1)
		mc_base = 16;
	else
		mc_base = 0;

	/* Length and type in the first word */
	*data = dib9000_read_word_attr(state, 1029 + mc_base, attr);

	size = *data & 0xff;
	if (size <= MBX_MAX_WORDS) {
		data++;
		size--;		/* Initial word already read */

		dib9000_read16_noinc_attr(state, 1029 + mc_base, (u8 *) data, size * 2, attr);

		/* to word conversion */
		for (i = 0; i < size; i++) {
			tmp = *data;
			*data = (tmp >> 8) | (tmp << 8);
			data++;
		}

#ifdef DUMP_MSG
630
		dprintk("<--\n");
631
		for (i = 0; i < size + 1; i++)
632
			dprintk("%04x\n", d[i]);
633 634 635
		dprintk("\n");
#endif
	} else {
636
		dprintk("MBX: message is too big for message cache (%d), flushing message\n", size);
637 638 639 640 641 642 643
		size--;		/* Initial word already read */
		while (size--)
			dib9000_read16_noinc_attr(state, 1029 + mc_base, (u8 *) data, 2, attr);
	}
	/* Update register nb_mes_in_TX */
	dib9000_write_word_attr(state, 1028 + mc_base, 1 << 14, attr);

644
	mutex_unlock(&state->platform.risc.mbx_if_lock);
645 646 647 648 649 650 651 652 653 654 655 656

	return size + 1;
}

static int dib9000_risc_debug_buf(struct dib9000_state *state, u16 * data, u8 size)
{
	u32 ts = data[1] << 16 | data[0];
	char *b = (char *)&data[2];

	b[2 * (size - 2) - 1] = '\0';	/* Bullet proof the buffer */
	if (*b == '~') {
		b++;
657
		dprintk("%s\n", b);
658
	} else
659 660 661
		dprintk("RISC%d: %d.%04d %s\n",
			state->fe_id,
			ts / 10000, ts % 10000, *b ? b : "<empty>");
662 663 664 665 666 667 668 669 670 671 672 673 674 675
	return 1;
}

static int dib9000_mbx_fetch_to_cache(struct dib9000_state *state, u16 attr)
{
	int i;
	u8 size;
	u16 *block;
	/* find a free slot */
	for (i = 0; i < DIB9000_MSG_CACHE_SIZE; i++) {
		block = state->platform.risc.message_cache[i];
		if (*block == 0) {
			size = dib9000_mbx_read(state, block, 1, attr);

676
/*                      dprintk( "MBX: fetched %04x message to cache\n", *block); */
677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695

			switch (*block >> 8) {
			case IN_MSG_DEBUG_BUF:
				dib9000_risc_debug_buf(state, block + 1, size);	/* debug-messages are going to be printed right away */
				*block = 0;	/* free the block */
				break;
#if 0
			case IN_MSG_DATA:	/* FE-TRACE */
				dib9000_risc_data_process(state, block + 1, size);
				*block = 0;
				break;
#endif
			default:
				break;
			}

			return 1;
		}
	}
696
	dprintk("MBX: no free cache-slot found for new message...\n");
697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714
	return -1;
}

static u8 dib9000_mbx_count(struct dib9000_state *state, u8 risc_id, u16 attr)
{
	if (risc_id == 0)
		return (u8) (dib9000_read_word_attr(state, 1028, attr) >> 10) & 0x1f;	/* 5 bit field */
	else
		return (u8) (dib9000_read_word_attr(state, 1044, attr) >> 8) & 0x7f;	/* 7 bit field */
}

static int dib9000_mbx_process(struct dib9000_state *state, u16 attr)
{
	int ret = 0;

	if (!state->platform.risc.fw_is_running)
		return -1;

715
	if (mutex_lock_interruptible(&state->platform.risc.mbx_lock) < 0) {
716
		dprintk("could not get the lock\n");
717 718
		return -1;
	}
719 720 721 722

	if (dib9000_mbx_count(state, 1, attr))	/* 1=RiscB */
		ret = dib9000_mbx_fetch_to_cache(state, attr);

723
	dib9000_read_word_attr(state, 1229, attr);	/* Clear the IRQ */
724
/*      if (tmp) */
725
/*              dprintk( "cleared IRQ: %x\n", tmp); */
726
	mutex_unlock(&state->platform.risc.mbx_lock);
727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759

	return ret;
}

static int dib9000_mbx_get_message_attr(struct dib9000_state *state, u16 id, u16 * msg, u8 * size, u16 attr)
{
	u8 i;
	u16 *block;
	u16 timeout = 30;

	*msg = 0;
	do {
		/* dib9000_mbx_get_from_cache(); */
		for (i = 0; i < DIB9000_MSG_CACHE_SIZE; i++) {
			block = state->platform.risc.message_cache[i];
			if ((*block >> 8) == id) {
				*size = (*block & 0xff) - 1;
				memcpy(msg, block + 1, (*size) * 2);
				*block = 0;	/* free the block */
				i = 0;	/* signal that we found a message */
				break;
			}
		}

		if (i == 0)
			break;

		if (dib9000_mbx_process(state, attr) == -1)	/* try to fetch one message - if any */
			return -1;

	} while (--timeout);

	if (timeout == 0) {
760
		dprintk("waiting for message %d timed out\n", id);
761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779
		return -1;
	}

	return i == 0;
}

static int dib9000_risc_check_version(struct dib9000_state *state)
{
	u8 r[4];
	u8 size;
	u16 fw_version = 0;

	if (dib9000_mbx_send(state, OUT_MSG_REQ_VERSION, &fw_version, 1) != 0)
		return -EIO;

	if (dib9000_mbx_get_message(state, IN_MSG_VERSION, (u16 *) r, &size) < 0)
		return -EIO;

	fw_version = (r[0] << 8) | r[1];
780
	dprintk("RISC: ver: %d.%02d (IC: %d)\n", fw_version >> 10, fw_version & 0x3ff, (r[2] << 8) | r[3]);
781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857

	if ((fw_version >> 10) != 7)
		return -EINVAL;

	switch (fw_version & 0x3ff) {
	case 11:
	case 12:
	case 14:
	case 15:
	case 16:
	case 17:
		break;
	default:
		dprintk("RISC: invalid firmware version");
		return -EINVAL;
	}

	dprintk("RISC: valid firmware version");
	return 0;
}

static int dib9000_fw_boot(struct dib9000_state *state, const u8 * codeA, u32 lenA, const u8 * codeB, u32 lenB)
{
	/* Reconfig pool mac ram */
	dib9000_write_word(state, 1225, 0x02);	/* A: 8k C, 4 k D - B: 32k C 6 k D - IRAM 96k */
	dib9000_write_word(state, 1226, 0x05);

	/* Toggles IP crypto to Host APB interface. */
	dib9000_write_word(state, 1542, 1);

	/* Set jump and no jump in the dma box */
	dib9000_write_word(state, 1074, 0);
	dib9000_write_word(state, 1075, 0);

	/* Set MAC as APB Master. */
	dib9000_write_word(state, 1237, 0);

	/* Reset the RISCs */
	if (codeA != NULL)
		dib9000_write_word(state, 1024, 2);
	else
		dib9000_write_word(state, 1024, 15);
	if (codeB != NULL)
		dib9000_write_word(state, 1040, 2);

	if (codeA != NULL)
		dib9000_firmware_download(state, 0, 0x1234, codeA, lenA);
	if (codeB != NULL)
		dib9000_firmware_download(state, 1, 0x1234, codeB, lenB);

	/* Run the RISCs */
	if (codeA != NULL)
		dib9000_write_word(state, 1024, 0);
	if (codeB != NULL)
		dib9000_write_word(state, 1040, 0);

	if (codeA != NULL)
		if (dib9000_mbx_host_init(state, 0) != 0)
			return -EIO;
	if (codeB != NULL)
		if (dib9000_mbx_host_init(state, 1) != 0)
			return -EIO;

	msleep(100);
	state->platform.risc.fw_is_running = 1;

	if (dib9000_risc_check_version(state) != 0)
		return -EINVAL;

	state->platform.risc.memcmd = 0xff;
	return 0;
}

static u16 dib9000_identify(struct i2c_device *client)
{
	u16 value;

858 859
	value = dib9000_i2c_read16(client, 896);
	if (value != 0x01b3) {
860
		dprintk("wrong Vendor ID (0x%x)\n", value);
861 862 863 864 865
		return 0;
	}

	value = dib9000_i2c_read16(client, 897);
	if (value != 0x4000 && value != 0x4001 && value != 0x4002 && value != 0x4003 && value != 0x4004 && value != 0x4005) {
866
		dprintk("wrong Device ID (0x%x)\n", value);
867 868 869 870 871
		return 0;
	}

	/* protect this driver to be used with 7000PC */
	if (value == 0x4000 && dib9000_i2c_read16(client, 769) == 0x4000) {
872
		dprintk("this driver does not work with DiB7000PC\n");
873 874 875 876 877
		return 0;
	}

	switch (value) {
	case 0x4000:
878
		dprintk("found DiB7000MA/PA/MB/PB\n");
879 880
		break;
	case 0x4001:
881
		dprintk("found DiB7000HC\n");
882 883
		break;
	case 0x4002:
884
		dprintk("found DiB7000MC\n");
885 886
		break;
	case 0x4003:
887
		dprintk("found DiB9000A\n");
888 889
		break;
	case 0x4004:
890
		dprintk("found DiB9000H\n");
891 892
		break;
	case 0x4005:
893
		dprintk("found DiB9000M\n");
894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969
		break;
	}

	return value;
}

static void dib9000_set_power_mode(struct dib9000_state *state, enum dib9000_power_mode mode)
{
	/* by default everything is going to be powered off */
	u16 reg_903 = 0x3fff, reg_904 = 0xffff, reg_905 = 0xffff, reg_906;
	u8 offset;

	if (state->revision == 0x4003 || state->revision == 0x4004 || state->revision == 0x4005)
		offset = 1;
	else
		offset = 0;

	reg_906 = dib9000_read_word(state, 906 + offset) | 0x3;	/* keep settings for RISC */

	/* now, depending on the requested mode, we power on */
	switch (mode) {
		/* power up everything in the demod */
	case DIB9000_POWER_ALL:
		reg_903 = 0x0000;
		reg_904 = 0x0000;
		reg_905 = 0x0000;
		reg_906 = 0x0000;
		break;

		/* just leave power on the control-interfaces: GPIO and (I2C or SDIO or SRAM) */
	case DIB9000_POWER_INTERFACE_ONLY:	/* TODO power up either SDIO or I2C or SRAM */
		reg_905 &= ~((1 << 7) | (1 << 6) | (1 << 5) | (1 << 2));
		break;

	case DIB9000_POWER_INTERF_ANALOG_AGC:
		reg_903 &= ~((1 << 15) | (1 << 14) | (1 << 11) | (1 << 10));
		reg_905 &= ~((1 << 7) | (1 << 6) | (1 << 5) | (1 << 4) | (1 << 2));
		reg_906 &= ~((1 << 0));
		break;

	case DIB9000_POWER_COR4_DINTLV_ICIRM_EQUAL_CFROD:
		reg_903 = 0x0000;
		reg_904 = 0x801f;
		reg_905 = 0x0000;
		reg_906 &= ~((1 << 0));
		break;

	case DIB9000_POWER_COR4_CRY_ESRAM_MOUT_NUD:
		reg_903 = 0x0000;
		reg_904 = 0x8000;
		reg_905 = 0x010b;
		reg_906 &= ~((1 << 0));
		break;
	default:
	case DIB9000_POWER_NO:
		break;
	}

	/* always power down unused parts */
	if (!state->platform.host.mobile_mode)
		reg_904 |= (1 << 7) | (1 << 6) | (1 << 4) | (1 << 2) | (1 << 1);

	/* P_sdio_select_clk = 0 on MC and after */
	if (state->revision != 0x4000)
		reg_906 <<= 1;

	dib9000_write_word(state, 903 + offset, reg_903);
	dib9000_write_word(state, 904 + offset, reg_904);
	dib9000_write_word(state, 905 + offset, reg_905);
	dib9000_write_word(state, 906 + offset, reg_906);
}

static int dib9000_fw_reset(struct dvb_frontend *fe)
{
	struct dib9000_state *state = fe->demodulator_priv;

970
	dib9000_write_word(state, 1817, 0x0003);
971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014

	dib9000_write_word(state, 1227, 1);
	dib9000_write_word(state, 1227, 0);

	switch ((state->revision = dib9000_identify(&state->i2c))) {
	case 0x4003:
	case 0x4004:
	case 0x4005:
		state->reg_offs = 1;
		break;
	default:
		return -EINVAL;
	}

	/* reset the i2c-master to use the host interface */
	dibx000_reset_i2c_master(&state->i2c_master);

	dib9000_set_power_mode(state, DIB9000_POWER_ALL);

	/* unforce divstr regardless whether i2c enumeration was done or not */
	dib9000_write_word(state, 1794, dib9000_read_word(state, 1794) & ~(1 << 1));
	dib9000_write_word(state, 1796, 0);
	dib9000_write_word(state, 1805, 0x805);

	/* restart all parts */
	dib9000_write_word(state, 898, 0xffff);
	dib9000_write_word(state, 899, 0xffff);
	dib9000_write_word(state, 900, 0x0001);
	dib9000_write_word(state, 901, 0xff19);
	dib9000_write_word(state, 902, 0x003c);

	dib9000_write_word(state, 898, 0);
	dib9000_write_word(state, 899, 0);
	dib9000_write_word(state, 900, 0);
	dib9000_write_word(state, 901, 0);
	dib9000_write_word(state, 902, 0);

	dib9000_write_word(state, 911, state->chip.d9.cfg.if_drives);

	dib9000_set_power_mode(state, DIB9000_POWER_INTERFACE_ONLY);

	return 0;
}

1015
static int dib9000_risc_apb_access_read(struct dib9000_state *state, u32 address, u16 attribute, const u8 * tx, u32 txlen, u8 * b, u32 len)
1016 1017 1018 1019 1020 1021 1022
{
	u16 mb[10];
	u8 i, s;

	if (address >= 1024 || !state->platform.risc.fw_is_running)
		return -EINVAL;

1023
	/* dprintk( "APB access through rd fw %d %x\n", address, attribute); */
1024 1025 1026 1027 1028 1029

	mb[0] = (u16) address;
	mb[1] = len / 2;
	dib9000_mbx_send_attr(state, OUT_MSG_BRIDGE_APB_R, mb, 2, attribute);
	switch (dib9000_mbx_get_message_attr(state, IN_MSG_END_BRIDGE_APB_RW, mb, &s, attribute)) {
	case 1:
1030
		s--;
1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049
		for (i = 0; i < s; i++) {
			b[i * 2] = (mb[i + 1] >> 8) & 0xff;
			b[i * 2 + 1] = (mb[i + 1]) & 0xff;
		}
		return 0;
	default:
		return -EIO;
	}
	return -EIO;
}

static int dib9000_risc_apb_access_write(struct dib9000_state *state, u32 address, u16 attribute, const u8 * b, u32 len)
{
	u16 mb[10];
	u8 s, i;

	if (address >= 1024 || !state->platform.risc.fw_is_running)
		return -EINVAL;

1050 1051 1052
	if (len > 18)
		return -EINVAL;

1053
	/* dprintk( "APB access through wr fw %d %x\n", address, attribute); */
1054

1055 1056 1057 1058 1059
	mb[0] = (u16)address;
	for (i = 0; i + 1 < len; i += 2)
		mb[1 + i / 2] = b[i] << 8 | b[i + 1];
	if (len & 1)
		mb[1 + len / 2] = b[len - 1] << 8;
1060

1061
	dib9000_mbx_send_attr(state, OUT_MSG_BRIDGE_APB_W, mb, (3 + len) / 2, attribute);
1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072
	return dib9000_mbx_get_message_attr(state, IN_MSG_END_BRIDGE_APB_RW, mb, &s, attribute) == 1 ? 0 : -EINVAL;
}

static int dib9000_fw_memmbx_sync(struct dib9000_state *state, u8 i)
{
	u8 index_loop = 10;

	if (!state->platform.risc.fw_is_running)
		return 0;
	dib9000_risc_mem_write(state, FE_MM_RW_SYNC, &i);
	do {
1073 1074
		dib9000_risc_mem_read(state, FE_MM_RW_SYNC, state->i2c_read_buffer, 1);
	} while (state->i2c_read_buffer[0] && index_loop--);
1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126

	if (index_loop > 0)
		return 0;
	return -EIO;
}

static int dib9000_fw_init(struct dib9000_state *state)
{
	struct dibGPIOFunction *f;
	u16 b[40] = { 0 };
	u8 i;
	u8 size;

	if (dib9000_fw_boot(state, NULL, 0, state->chip.d9.cfg.microcode_B_fe_buffer, state->chip.d9.cfg.microcode_B_fe_size) != 0)
		return -EIO;

	/* initialize the firmware */
	for (i = 0; i < ARRAY_SIZE(state->chip.d9.cfg.gpio_function); i++) {
		f = &state->chip.d9.cfg.gpio_function[i];
		if (f->mask) {
			switch (f->function) {
			case BOARD_GPIO_FUNCTION_COMPONENT_ON:
				b[0] = (u16) f->mask;
				b[1] = (u16) f->direction;
				b[2] = (u16) f->value;
				break;
			case BOARD_GPIO_FUNCTION_COMPONENT_OFF:
				b[3] = (u16) f->mask;
				b[4] = (u16) f->direction;
				b[5] = (u16) f->value;
				break;
			}
		}
	}
	if (dib9000_mbx_send(state, OUT_MSG_CONF_GPIO, b, 15) != 0)
		return -EIO;

	/* subband */
	b[0] = state->chip.d9.cfg.subband.size;	/* type == 0 -> GPIO - PWM not yet supported */
	for (i = 0; i < state->chip.d9.cfg.subband.size; i++) {
		b[1 + i * 4] = state->chip.d9.cfg.subband.subband[i].f_mhz;
		b[2 + i * 4] = (u16) state->chip.d9.cfg.subband.subband[i].gpio.mask;
		b[3 + i * 4] = (u16) state->chip.d9.cfg.subband.subband[i].gpio.direction;
		b[4 + i * 4] = (u16) state->chip.d9.cfg.subband.subband[i].gpio.value;
	}
	b[1 + i * 4] = 0;	/* fe_id */
	if (dib9000_mbx_send(state, OUT_MSG_SUBBAND_SEL, b, 2 + 4 * i) != 0)
		return -EIO;

	/* 0 - id, 1 - no_of_frontends */
	b[0] = (0 << 8) | 1;
	/* 0 = i2c-address demod, 0 = tuner */
1127
	b[1] = (0 << 8) | (0);
1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144
	b[2] = (u16) (((state->chip.d9.cfg.xtal_clock_khz * 1000) >> 16) & 0xffff);
	b[3] = (u16) (((state->chip.d9.cfg.xtal_clock_khz * 1000)) & 0xffff);
	b[4] = (u16) ((state->chip.d9.cfg.vcxo_timer >> 16) & 0xffff);
	b[5] = (u16) ((state->chip.d9.cfg.vcxo_timer) & 0xffff);
	b[6] = (u16) ((state->chip.d9.cfg.timing_frequency >> 16) & 0xffff);
	b[7] = (u16) ((state->chip.d9.cfg.timing_frequency) & 0xffff);
	b[29] = state->chip.d9.cfg.if_drives;
	if (dib9000_mbx_send(state, OUT_MSG_INIT_DEMOD, b, ARRAY_SIZE(b)) != 0)
		return -EIO;

	if (dib9000_mbx_send(state, OUT_MSG_FE_FW_DL, NULL, 0) != 0)
		return -EIO;

	if (dib9000_mbx_get_message(state, IN_MSG_FE_FW_DL_DONE, b, &size) < 0)
		return -EIO;

	if (size > ARRAY_SIZE(b)) {
1145 1146
		dprintk("error : firmware returned %dbytes needed but the used buffer has only %dbytes\n Firmware init ABORTED", size,
			(int)ARRAY_SIZE(b));
1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157
		return -EINVAL;
	}

	for (i = 0; i < size; i += 2) {
		state->platform.risc.fe_mm[i / 2].addr = b[i + 0];
		state->platform.risc.fe_mm[i / 2].size = b[i + 1];
	}

	return 0;
}

1158
static void dib9000_fw_set_channel_head(struct dib9000_state *state)
1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178
{
	u8 b[9];
	u32 freq = state->fe[0]->dtv_property_cache.frequency / 1000;
	if (state->fe_id % 2)
		freq += 101;

	b[0] = (u8) ((freq >> 0) & 0xff);
	b[1] = (u8) ((freq >> 8) & 0xff);
	b[2] = (u8) ((freq >> 16) & 0xff);
	b[3] = (u8) ((freq >> 24) & 0xff);
	b[4] = (u8) ((state->fe[0]->dtv_property_cache.bandwidth_hz / 1000 >> 0) & 0xff);
	b[5] = (u8) ((state->fe[0]->dtv_property_cache.bandwidth_hz / 1000 >> 8) & 0xff);
	b[6] = (u8) ((state->fe[0]->dtv_property_cache.bandwidth_hz / 1000 >> 16) & 0xff);
	b[7] = (u8) ((state->fe[0]->dtv_property_cache.bandwidth_hz / 1000 >> 24) & 0xff);
	b[8] = 0x80;		/* do not wait for CELL ID when doing autosearch */
	if (state->fe[0]->dtv_property_cache.delivery_system == SYS_DVBT)
		b[8] |= 1;
	dib9000_risc_mem_write(state, FE_MM_W_CHANNEL_HEAD, b);
}

1179
static int dib9000_fw_get_channel(struct dvb_frontend *fe)
1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196
{
	struct dib9000_state *state = fe->demodulator_priv;
	struct dibDVBTChannel {
		s8 spectrum_inversion;

		s8 nfft;
		s8 guard;
		s8 constellation;

		s8 hrch;
		s8 alpha;
		s8 code_rate_hp;
		s8 code_rate_lp;
		s8 select_hp;

		s8 intlv_native;
	};
1197
	struct dibDVBTChannel *ch;
1198 1199
	int ret = 0;

1200
	if (mutex_lock_interruptible(&state->platform.risc.mem_mbx_lock) < 0) {
1201
		dprintk("could not get the lock\n");
1202 1203
		return -EINTR;
	}
1204 1205
	if (dib9000_fw_memmbx_sync(state, FE_SYNC_CHANNEL) < 0) {
		ret = -EIO;
1206
		goto error;
1207 1208
	}

1209 1210 1211 1212
	dib9000_risc_mem_read(state, FE_MM_R_CHANNEL_UNION,
			state->i2c_read_buffer, sizeof(struct dibDVBTChannel));
	ch = (struct dibDVBTChannel *)state->i2c_read_buffer;

1213

1214
	switch (ch->spectrum_inversion & 0x7) {
1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225
	case 1:
		state->fe[0]->dtv_property_cache.inversion = INVERSION_ON;
		break;
	case 0:
		state->fe[0]->dtv_property_cache.inversion = INVERSION_OFF;
		break;
	default:
	case -1:
		state->fe[0]->dtv_property_cache.inversion = INVERSION_AUTO;
		break;
	}
1226
	switch (ch->nfft) {
1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240
	case 0:
		state->fe[0]->dtv_property_cache.transmission_mode = TRANSMISSION_MODE_2K;
		break;
	case 2:
		state->fe[0]->dtv_property_cache.transmission_mode = TRANSMISSION_MODE_4K;
		break;
	case 1:
		state->fe[0]->dtv_property_cache.transmission_mode = TRANSMISSION_MODE_8K;
		break;
	default:
	case -1:
		state->fe[0]->dtv_property_cache.transmission_mode = TRANSMISSION_MODE_AUTO;
		break;
	}
1241
	switch (ch->guard) {
1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258
	case 0:
		state->fe[0]->dtv_property_cache.guard_interval = GUARD_INTERVAL_1_32;
		break;
	case 1:
		state->fe[0]->dtv_property_cache.guard_interval = GUARD_INTERVAL_1_16;
		break;
	case 2:
		state->fe[0]->dtv_property_cache.guard_interval = GUARD_INTERVAL_1_8;
		break;
	case 3:
		state->fe[0]->dtv_property_cache.guard_interval = GUARD_INTERVAL_1_4;
		break;
	default:
	case -1:
		state->fe[0]->dtv_property_cache.guard_interval = GUARD_INTERVAL_AUTO;
		break;
	}
1259
	switch (ch->constellation) {
1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273
	case 2:
		state->fe[0]->dtv_property_cache.modulation = QAM_64;
		break;
	case 1:
		state->fe[0]->dtv_property_cache.modulation = QAM_16;
		break;
	case 0:
		state->fe[0]->dtv_property_cache.modulation = QPSK;
		break;
	default:
	case -1:
		state->fe[0]->dtv_property_cache.modulation = QAM_AUTO;
		break;
	}
1274
	switch (ch->hrch) {
1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285
	case 0:
		state->fe[0]->dtv_property_cache.hierarchy = HIERARCHY_NONE;
		break;
	case 1:
		state->fe[0]->dtv_property_cache.hierarchy = HIERARCHY_1;
		break;
	default:
	case -1:
		state->fe[0]->dtv_property_cache.hierarchy = HIERARCHY_AUTO;
		break;
	}
1286
	switch (ch->code_rate_hp) {
1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306
	case 1:
		state->fe[0]->dtv_property_cache.code_rate_HP = FEC_1_2;
		break;
	case 2:
		state->fe[0]->dtv_property_cache.code_rate_HP = FEC_2_3;
		break;
	case 3:
		state->fe[0]->dtv_property_cache.code_rate_HP = FEC_3_4;
		break;
	case 5:
		state->fe[0]->dtv_property_cache.code_rate_HP = FEC_5_6;
		break;
	case 7:
		state->fe[0]->dtv_property_cache.code_rate_HP = FEC_7_8;
		break;
	default:
	case -1:
		state->fe[0]->dtv_property_cache.code_rate_HP = FEC_AUTO;
		break;
	}
1307
	switch (ch->code_rate_lp) {
1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328
	case 1:
		state->fe[0]->dtv_property_cache.code_rate_LP = FEC_1_2;
		break;
	case 2:
		state->fe[0]->dtv_property_cache.code_rate_LP = FEC_2_3;
		break;
	case 3:
		state->fe[0]->dtv_property_cache.code_rate_LP = FEC_3_4;
		break;
	case 5:
		state->fe[0]->dtv_property_cache.code_rate_LP = FEC_5_6;
		break;
	case 7:
		state->fe[0]->dtv_property_cache.code_rate_LP = FEC_7_8;
		break;
	default:
	case -1:
		state->fe[0]->dtv_property_cache.code_rate_LP = FEC_AUTO;
		break;
	}

1329
error:
1330
	mutex_unlock(&state->platform.risc.mem_mbx_lock);
1331 1332 1333
	return ret;
}

1334
static int dib9000_fw_set_channel_union(struct dvb_frontend *fe)
1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473
{
	struct dib9000_state *state = fe->demodulator_priv;
	struct dibDVBTChannel {
		s8 spectrum_inversion;

		s8 nfft;
		s8 guard;
		s8 constellation;

		s8 hrch;
		s8 alpha;
		s8 code_rate_hp;
		s8 code_rate_lp;
		s8 select_hp;

		s8 intlv_native;
	};
	struct dibDVBTChannel ch;

	switch (state->fe[0]->dtv_property_cache.inversion) {
	case INVERSION_ON:
		ch.spectrum_inversion = 1;
		break;
	case INVERSION_OFF:
		ch.spectrum_inversion = 0;
		break;
	default:
	case INVERSION_AUTO:
		ch.spectrum_inversion = -1;
		break;
	}
	switch (state->fe[0]->dtv_property_cache.transmission_mode) {
	case TRANSMISSION_MODE_2K:
		ch.nfft = 0;
		break;
	case TRANSMISSION_MODE_4K:
		ch.nfft = 2;
		break;
	case TRANSMISSION_MODE_8K:
		ch.nfft = 1;
		break;
	default:
	case TRANSMISSION_MODE_AUTO:
		ch.nfft = 1;
		break;
	}
	switch (state->fe[0]->dtv_property_cache.guard_interval) {
	case GUARD_INTERVAL_1_32:
		ch.guard = 0;
		break;
	case GUARD_INTERVAL_1_16:
		ch.guard = 1;
		break;
	case GUARD_INTERVAL_1_8:
		ch.guard = 2;
		break;
	case GUARD_INTERVAL_1_4:
		ch.guard = 3;
		break;
	default:
	case GUARD_INTERVAL_AUTO:
		ch.guard = -1;
		break;
	}
	switch (state->fe[0]->dtv_property_cache.modulation) {
	case QAM_64:
		ch.constellation = 2;
		break;
	case QAM_16:
		ch.constellation = 1;
		break;
	case QPSK:
		ch.constellation = 0;
		break;
	default:
	case QAM_AUTO:
		ch.constellation = -1;
		break;
	}
	switch (state->fe[0]->dtv_property_cache.hierarchy) {
	case HIERARCHY_NONE:
		ch.hrch = 0;
		break;
	case HIERARCHY_1:
	case HIERARCHY_2:
	case HIERARCHY_4:
		ch.hrch = 1;
		break;
	default:
	case HIERARCHY_AUTO:
		ch.hrch = -1;
		break;
	}
	ch.alpha = 1;
	switch (state->fe[0]->dtv_property_cache.code_rate_HP) {
	case FEC_1_2:
		ch.code_rate_hp = 1;
		break;
	case FEC_2_3:
		ch.code_rate_hp = 2;
		break;
	case FEC_3_4:
		ch.code_rate_hp = 3;
		break;
	case FEC_5_6:
		ch.code_rate_hp = 5;
		break;
	case FEC_7_8:
		ch.code_rate_hp = 7;
		break;
	default:
	case FEC_AUTO:
		ch.code_rate_hp = -1;
		break;
	}
	switch (state->fe[0]->dtv_property_cache.code_rate_LP) {
	case FEC_1_2:
		ch.code_rate_lp = 1;
		break;
	case FEC_2_3:
		ch.code_rate_lp = 2;
		break;
	case FEC_3_4:
		ch.code_rate_lp = 3;
		break;
	case FEC_5_6:
		ch.code_rate_lp = 5;
		break;
	case FEC_7_8:
		ch.code_rate_lp = 7;
		break;
	default:
	case FEC_AUTO:
		ch.code_rate_lp = -1;
		break;
	}
	ch.select_hp = 1;
	ch.intlv_native = 1;

1474
	dib9000_risc_mem_write(state, FE_MM_W_CHANNEL_UNION, (u8 *) &ch);
1475 1476 1477 1478

	return 0;
}

1479
static int dib9000_fw_tune(struct dvb_frontend *fe)
1480 1481 1482 1483 1484 1485 1486
{
	struct dib9000_state *state = fe->demodulator_priv;
	int ret = 10, search = state->channel_status.status == CHANNEL_STATUS_PARAMETERS_UNKNOWN;
	s8 i;

	switch (state->tune_state) {
	case CT_DEMOD_START:
1487
		dib9000_fw_set_channel_head(state);
1488 1489 1490 1491 1492 1493 1494 1495

		/* write the channel context - a channel is initialized to 0, so it is OK */
		dib9000_risc_mem_write(state, FE_MM_W_CHANNEL_CONTEXT, (u8 *) fe_info);
		dib9000_risc_mem_write(state, FE_MM_W_FE_INFO, (u8 *) fe_info);

		if (search)
			dib9000_mbx_send(state, OUT_MSG_FE_CHANNEL_SEARCH, NULL, 0);
		else {
1496
			dib9000_fw_set_channel_union(fe);
1497 1498 1499 1500 1501 1502
			dib9000_mbx_send(state, OUT_MSG_FE_CHANNEL_TUNE, NULL, 0);
		}
		state->tune_state = CT_DEMOD_STEP_1;
		break;
	case CT_DEMOD_STEP_1:
		if (search)
1503
			dib9000_risc_mem_read(state, FE_MM_R_CHANNEL_SEARCH_STATE, state->i2c_read_buffer, 1);
1504
		else
1505 1506
			dib9000_risc_mem_read(state, FE_MM_R_CHANNEL_TUNE_STATE, state->i2c_read_buffer, 1);
		i = (s8)state->i2c_read_buffer[0];
1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543
		switch (i) {	/* something happened */
		case 0:
			break;
		case -2:	/* tps locks are "slower" than MPEG locks -> even in autosearch data is OK here */
			if (search)
				state->status = FE_STATUS_DEMOD_SUCCESS;
			else {
				state->tune_state = CT_DEMOD_STOP;
				state->status = FE_STATUS_LOCKED;
			}
			break;
		default:
			state->status = FE_STATUS_TUNE_FAILED;
			state->tune_state = CT_DEMOD_STOP;
			break;
		}
		break;
	default:
		ret = FE_CALLBACK_TIME_NEVER;
		break;
	}

	return ret;
}

static int dib9000_fw_set_diversity_in(struct dvb_frontend *fe, int onoff)
{
	struct dib9000_state *state = fe->demodulator_priv;
	u16 mode = (u16) onoff;
	return dib9000_mbx_send(state, OUT_MSG_ENABLE_DIVERSITY, &mode, 1);
}

static int dib9000_fw_set_output_mode(struct dvb_frontend *fe, int mode)
{
	struct dib9000_state *state = fe->demodulator_priv;
	u16 outreg, smo_mode;

1544
	dprintk("setting output mode for demod %p to %d\n", fe, mode);
1545 1546

	switch (mode) {
1547
	case OUTMODE_MPEG2_PAR_GATED_CLK:
1548 1549
		outreg = (1 << 10);	/* 0x0400 */
		break;
1550
	case OUTMODE_MPEG2_PAR_CONT_CLK:
1551 1552
		outreg = (1 << 10) | (1 << 6);	/* 0x0440 */
		break;
1553
	case OUTMODE_MPEG2_SERIAL:
1554 1555 1556 1557 1558
		outreg = (1 << 10) | (2 << 6) | (0 << 1);	/* 0x0482 */
		break;
	case OUTMODE_DIVERSITY:
		outreg = (1 << 10) | (4 << 6);	/* 0x0500 */
		break;
1559
	case OUTMODE_MPEG2_FIFO:
1560 1561
		outreg = (1 << 10) | (5 << 6);
		break;
1562
	case OUTMODE_HIGH_Z:
1563 1564 1565
		outreg = 0;
		break;
	default:
1566
		dprintk("Unhandled output_mode passed to be set for demod %p\n", &state->fe[0]);
1567 1568 1569
		return -EINVAL;
	}

1570
	dib9000_write_word(state, 1795, outreg);
1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599

	switch (mode) {
	case OUTMODE_MPEG2_PAR_GATED_CLK:
	case OUTMODE_MPEG2_PAR_CONT_CLK:
	case OUTMODE_MPEG2_SERIAL:
	case OUTMODE_MPEG2_FIFO:
		smo_mode = (dib9000_read_word(state, 295) & 0x0010) | (1 << 1);
		if (state->chip.d9.cfg.output_mpeg2_in_188_bytes)
			smo_mode |= (1 << 5);
		dib9000_write_word(state, 295, smo_mode);
		break;
	}

	outreg = to_fw_output_mode(mode);
	return dib9000_mbx_send(state, OUT_MSG_SET_OUTPUT_MODE, &outreg, 1);
}

static int dib9000_tuner_xfer(struct i2c_adapter *i2c_adap, struct i2c_msg msg[], int num)
{
	struct dib9000_state *state = i2c_get_adapdata(i2c_adap);
	u16 i, len, t, index_msg;

	for (index_msg = 0; index_msg < num; index_msg++) {
		if (msg[index_msg].flags & I2C_M_RD) {	/* read */
			len = msg[index_msg].len;
			if (len > 16)
				len = 16;

			if (dib9000_read_word(state, 790) != 0)
1600
				dprintk("TunerITF: read busy\n");
1601 1602 1603 1604 1605 1606 1607 1608 1609 1610

			dib9000_write_word(state, 784, (u16) (msg[index_msg].addr));
			dib9000_write_word(state, 787, (len / 2) - 1);
			dib9000_write_word(state, 786, 1);	/* start read */

			i = 1000;
			while (dib9000_read_word(state, 790) != (len / 2) && i)
				i--;

			if (i == 0)
1611
				dprintk("TunerITF: read failed\n");
1612 1613 1614 1615 1616 1617 1618

			for (i = 0; i < len; i += 2) {
				t = dib9000_read_word(state, 785);
				msg[index_msg].buf[i] = (t >> 8) & 0xff;
				msg[index_msg].buf[i + 1] = (t) & 0xff;
			}
			if (dib9000_read_word(state, 790) != 0)
1619
				dprintk("TunerITF: read more data than expected\n");
1620 1621 1622 1623 1624
		} else {
			i = 1000;
			while (dib9000_read_word(state, 789) && i)
				i--;
			if (i == 0)
1625
				dprintk("TunerITF: write busy\n");
1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640

			len = msg[index_msg].len;
			if (len > 16)
				len = 16;

			for (i = 0; i < len; i += 2)
				dib9000_write_word(state, 785, (msg[index_msg].buf[i] << 8) | msg[index_msg].buf[i + 1]);
			dib9000_write_word(state, 784, (u16) msg[index_msg].addr);
			dib9000_write_word(state, 787, (len / 2) - 1);
			dib9000_write_word(state, 786, 0);	/* start write */

			i = 1000;
			while (dib9000_read_word(state, 791) > 0 && i)
				i--;
			if (i == 0)
1641
				dprintk("TunerITF: write failed\n");
1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658
		}
	}
	return num;
}

int dib9000_fw_set_component_bus_speed(struct dvb_frontend *fe, u16 speed)
{
	struct dib9000_state *state = fe->demodulator_priv;

	state->component_bus_speed = speed;
	return 0;
}
EXPORT_SYMBOL(dib9000_fw_set_component_bus_speed);

static int dib9000_fw_component_bus_xfer(struct i2c_adapter *i2c_adap, struct i2c_msg msg[], int num)
{
	struct dib9000_state *state = i2c_get_adapdata(i2c_adap);
1659
	u8 type = 0;		/* I2C */
1660
	u8 port = DIBX000_I2C_INTERFACE_GPIO_3_4;
1661
	u16 scl = state->component_bus_speed;	/* SCL frequency */
1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684
	struct dib9000_fe_memory_map *m = &state->platform.risc.fe_mm[FE_MM_RW_COMPONENT_ACCESS_BUFFER];
	u8 p[13] = { 0 };

	p[0] = type;
	p[1] = port;
	p[2] = msg[0].addr << 1;

	p[3] = (u8) scl & 0xff;	/* scl */
	p[4] = (u8) (scl >> 8);

	p[7] = 0;
	p[8] = 0;

	p[9] = (u8) (msg[0].len);
	p[10] = (u8) (msg[0].len >> 8);
	if ((num > 1) && (msg[1].flags & I2C_M_RD)) {
		p[11] = (u8) (msg[1].len);
		p[12] = (u8) (msg[1].len >> 8);
	} else {
		p[11] = 0;
		p[12] = 0;
	}

1685
	if (mutex_lock_interruptible(&state->platform.risc.mem_mbx_lock) < 0) {
1686
		dprintk("could not get the lock\n");
1687 1688
		return 0;
	}
1689 1690 1691 1692 1693 1694 1695 1696 1697 1698

	dib9000_risc_mem_write(state, FE_MM_W_COMPONENT_ACCESS, p);

	{			/* write-part */
		dib9000_risc_mem_setup_cmd(state, m->addr, msg[0].len, 0);
		dib9000_risc_mem_write_chunks(state, msg[0].buf, msg[0].len);
	}

	/* do the transaction */
	if (dib9000_fw_memmbx_sync(state, FE_SYNC_COMPONENT_ACCESS) < 0) {
1699
		mutex_unlock(&state->platform.risc.mem_mbx_lock);
1700 1701 1702 1703 1704 1705 1706
		return 0;
	}

	/* read back any possible result */
	if ((num > 1) && (msg[1].flags & I2C_M_RD))
		dib9000_risc_mem_read(state, FE_MM_RW_COMPONENT_ACCESS_BUFFER, msg[1].buf, msg[1].len);

1707
	mutex_unlock(&state->platform.risc.mem_mbx_lock);
1708 1709 1710 1711 1712 1713 1714 1715 1716

	return num;
}

static u32 dib9000_i2c_func(struct i2c_adapter *adapter)
{
	return I2C_FUNC_I2C;
}

1717
static const struct i2c_algorithm dib9000_tuner_algo = {
1718 1719 1720 1721
	.master_xfer = dib9000_tuner_xfer,
	.functionality = dib9000_i2c_func,
};

1722
static const struct i2c_algorithm dib9000_component_bus_algo = {
1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768
	.master_xfer = dib9000_fw_component_bus_xfer,
	.functionality = dib9000_i2c_func,
};

struct i2c_adapter *dib9000_get_tuner_interface(struct dvb_frontend *fe)
{
	struct dib9000_state *st = fe->demodulator_priv;
	return &st->tuner_adap;
}
EXPORT_SYMBOL(dib9000_get_tuner_interface);

struct i2c_adapter *dib9000_get_component_bus_interface(struct dvb_frontend *fe)
{
	struct dib9000_state *st = fe->demodulator_priv;
	return &st->component_bus;
}
EXPORT_SYMBOL(dib9000_get_component_bus_interface);

struct i2c_adapter *dib9000_get_i2c_master(struct dvb_frontend *fe, enum dibx000_i2c_interface intf, int gating)
{
	struct dib9000_state *st = fe->demodulator_priv;
	return dibx000_get_i2c_adapter(&st->i2c_master, intf, gating);
}
EXPORT_SYMBOL(dib9000_get_i2c_master);

int dib9000_set_i2c_adapter(struct dvb_frontend *fe, struct i2c_adapter *i2c)
{
	struct dib9000_state *st = fe->demodulator_priv;

	st->i2c.i2c_adap = i2c;
	return 0;
}
EXPORT_SYMBOL(dib9000_set_i2c_adapter);

static int dib9000_cfg_gpio(struct dib9000_state *st, u8 num, u8 dir, u8 val)
{
	st->gpio_dir = dib9000_read_word(st, 773);
	st->gpio_dir &= ~(1 << num);	/* reset the direction bit */
	st->gpio_dir |= (dir & 0x1) << num;	/* set the new direction */
	dib9000_write_word(st, 773, st->gpio_dir);

	st->gpio_val = dib9000_read_word(st, 774);
	st->gpio_val &= ~(1 << num);	/* reset the direction bit */
	st->gpio_val |= (val & 0x01) << num;	/* set the new value */
	dib9000_write_word(st, 774, st->gpio_val);

1769
	dprintk("gpio dir: %04x: gpio val: %04x\n", st->gpio_dir, st->gpio_val);
1770 1771 1772 1773 1774 1775 1776 1777 1778 1779

	return 0;
}

int dib9000_set_gpio(struct dvb_frontend *fe, u8 num, u8 dir, u8 val)
{
	struct dib9000_state *state = fe->demodulator_priv;
	return dib9000_cfg_gpio(state, num, dir, val);
}
EXPORT_SYMBOL(dib9000_set_gpio);
1780

1781 1782 1783
int dib9000_fw_pid_filter_ctrl(struct dvb_frontend *fe, u8 onoff)
{
	struct dib9000_state *state = fe->demodulator_priv;
1784 1785 1786 1787 1788
	u16 val;
	int ret;

	if ((state->pid_ctrl_index != -2) && (state->pid_ctrl_index < 9)) {
		/* postpone the pid filtering cmd */
1789
		dprintk("pid filter cmd postpone\n");
1790 1791 1792 1793 1794 1795
		state->pid_ctrl_index++;
		state->pid_ctrl[state->pid_ctrl_index].cmd = DIB9000_PID_FILTER_CTRL;
		state->pid_ctrl[state->pid_ctrl_index].onoff = onoff;
		return 0;
	}

1796
	if (mutex_lock_interruptible(&state->demod_lock) < 0) {
1797
		dprintk("could not get the lock\n");
1798 1799
		return -EINTR;
	}
1800 1801

	val = dib9000_read_word(state, 294 + 1) & 0xffef;
1802 1803
	val |= (onoff & 0x1) << 4;

1804
	dprintk("PID filter enabled %d\n", onoff);
1805
	ret = dib9000_write_word(state, 294 + 1, val);
1806
	mutex_unlock(&state->demod_lock);
1807 1808
	return ret;

1809 1810
}
EXPORT_SYMBOL(dib9000_fw_pid_filter_ctrl);
1811

1812 1813 1814
int dib9000_fw_pid_filter(struct dvb_frontend *fe, u8 id, u16 pid, u8 onoff)
{
	struct dib9000_state *state = fe->demodulator_priv;
1815 1816 1817 1818
	int ret;

	if (state->pid_ctrl_index != -2) {
		/* postpone the pid filtering cmd */
1819
		dprintk("pid filter postpone\n");
1820 1821 1822 1823 1824 1825 1826
		if (state->pid_ctrl_index < 9) {
			state->pid_ctrl_index++;
			state->pid_ctrl[state->pid_ctrl_index].cmd = DIB9000_PID_FILTER;
			state->pid_ctrl[state->pid_ctrl_index].id = id;
			state->pid_ctrl[state->pid_ctrl_index].pid = pid;
			state->pid_ctrl[state->pid_ctrl_index].onoff = onoff;
		} else
1827
			dprintk("can not add any more pid ctrl cmd\n");
1828 1829 1830
		return 0;
	}

1831
	if (mutex_lock_interruptible(&state->demod_lock) < 0) {
1832
		dprintk("could not get the lock\n");
1833 1834
		return -EINTR;
	}
1835
	dprintk("Index %x, PID %d, OnOff %d\n", id, pid, onoff);
1836 1837
	ret = dib9000_write_word(state, 300 + 1 + id,
			onoff ? (1 << 13) | pid : 0);
1838
	mutex_unlock(&state->demod_lock);
1839
	return ret;
1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854
}
EXPORT_SYMBOL(dib9000_fw_pid_filter);

int dib9000_firmware_post_pll_init(struct dvb_frontend *fe)
{
	struct dib9000_state *state = fe->demodulator_priv;
	return dib9000_fw_init(state);
}
EXPORT_SYMBOL(dib9000_firmware_post_pll_init);

static void dib9000_release(struct dvb_frontend *demod)
{
	struct dib9000_state *st = demod->demodulator_priv;
	u8 index_frontend;

1855
	for (index_frontend = 1; (index_frontend < MAX_NUMBER_OF_FRONTENDS) && (st->fe[index_frontend] != NULL); index_frontend++)
1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874
		dvb_frontend_detach(st->fe[index_frontend]);

	dibx000_exit_i2c_master(&st->i2c_master);

	i2c_del_adapter(&st->tuner_adap);
	i2c_del_adapter(&st->component_bus);
	kfree(st->fe[0]);
	kfree(st);
}

static int dib9000_wakeup(struct dvb_frontend *fe)
{
	return 0;
}

static int dib9000_sleep(struct dvb_frontend *fe)
{
	struct dib9000_state *state = fe->demodulator_priv;
	u8 index_frontend;
1875
	int ret = 0;
1876

1877
	if (mutex_lock_interruptible(&state->demod_lock) < 0) {
1878
		dprintk("could not get the lock\n");
1879 1880
		return -EINTR;
	}
1881
	for (index_frontend = 1; (index_frontend < MAX_NUMBER_OF_FRONTENDS) && (state->fe[index_frontend] != NULL); index_frontend++) {
1882 1883
		ret = state->fe[index_frontend]->ops.sleep(state->fe[index_frontend]);
		if (ret < 0)
1884
			goto error;
1885
	}
1886 1887 1888
	ret = dib9000_mbx_send(state, OUT_MSG_FE_SLEEP, NULL, 0);

error:
1889
	mutex_unlock(&state->demod_lock);
1890
	return ret;
1891 1892 1893 1894 1895 1896 1897 1898
}

static int dib9000_fe_get_tune_settings(struct dvb_frontend *fe, struct dvb_frontend_tune_settings *tune)
{
	tune->min_delay_ms = 1000;
	return 0;
}

1899 1900
static int dib9000_get_frontend(struct dvb_frontend *fe,
				struct dtv_frontend_properties *c)
1901 1902 1903
{
	struct dib9000_state *state = fe->demodulator_priv;
	u8 index_frontend, sub_index_frontend;
1904
	enum fe_status stat;
1905 1906
	int ret = 0;

1907
	if (state->get_frontend_internal == 0) {
1908
		if (mutex_lock_interruptible(&state->demod_lock) < 0) {
1909
			dprintk("could not get the lock\n");
1910 1911 1912
			return -EINTR;
		}
	}
1913

1914
	for (index_frontend = 1; (index_frontend < MAX_NUMBER_OF_FRONTENDS) && (state->fe[index_frontend] != NULL); index_frontend++) {
1915 1916
		state->fe[index_frontend]->ops.read_status(state->fe[index_frontend], &stat);
		if (stat & FE_HAS_SYNC) {
1917
			dprintk("TPS lock on the slave%i\n", index_frontend);
1918 1919

			/* synchronize the cache with the other frontends */
1920
			state->fe[index_frontend]->ops.get_frontend(state->fe[index_frontend], c);
1921 1922
			for (sub_index_frontend = 0; (sub_index_frontend < MAX_NUMBER_OF_FRONTENDS) && (state->fe[sub_index_frontend] != NULL);
			     sub_index_frontend++) {
1923
				if (sub_index_frontend != index_frontend) {
1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939
					state->fe[sub_index_frontend]->dtv_property_cache.modulation =
					    state->fe[index_frontend]->dtv_property_cache.modulation;
					state->fe[sub_index_frontend]->dtv_property_cache.inversion =
					    state->fe[index_frontend]->dtv_property_cache.inversion;
					state->fe[sub_index_frontend]->dtv_property_cache.transmission_mode =
					    state->fe[index_frontend]->dtv_property_cache.transmission_mode;
					state->fe[sub_index_frontend]->dtv_property_cache.guard_interval =
					    state->fe[index_frontend]->dtv_property_cache.guard_interval;
					state->fe[sub_index_frontend]->dtv_property_cache.hierarchy =
					    state->fe[index_frontend]->dtv_property_cache.hierarchy;
					state->fe[sub_index_frontend]->dtv_property_cache.code_rate_HP =
					    state->fe[index_frontend]->dtv_property_cache.code_rate_HP;
					state->fe[sub_index_frontend]->dtv_property_cache.code_rate_LP =
					    state->fe[index_frontend]->dtv_property_cache.code_rate_LP;
					state->fe[sub_index_frontend]->dtv_property_cache.rolloff =
					    state->fe[index_frontend]->dtv_property_cache.rolloff;
1940 1941
				}
			}
1942 1943
			ret = 0;
			goto return_value;
1944 1945 1946 1947
		}
	}

	/* get the channel from master chip */
1948
	ret = dib9000_fw_get_channel(fe);
1949
	if (ret != 0)
1950
		goto return_value;
1951 1952

	/* synchronize the cache with the other frontends */
1953
	for (index_frontend = 1; (index_frontend < MAX_NUMBER_OF_FRONTENDS) && (state->fe[index_frontend] != NULL); index_frontend++) {
1954 1955 1956 1957 1958 1959 1960 1961
		state->fe[index_frontend]->dtv_property_cache.inversion = c->inversion;
		state->fe[index_frontend]->dtv_property_cache.transmission_mode = c->transmission_mode;
		state->fe[index_frontend]->dtv_property_cache.guard_interval = c->guard_interval;
		state->fe[index_frontend]->dtv_property_cache.modulation = c->modulation;
		state->fe[index_frontend]->dtv_property_cache.hierarchy = c->hierarchy;
		state->fe[index_frontend]->dtv_property_cache.code_rate_HP = c->code_rate_HP;
		state->fe[index_frontend]->dtv_property_cache.code_rate_LP = c->code_rate_LP;
		state->fe[index_frontend]->dtv_property_cache.rolloff = c->rolloff;
1962
	}
1963
	ret = 0;
1964

1965 1966
return_value:
	if (state->get_frontend_internal == 0)
1967
		mutex_unlock(&state->demod_lock);
1968
	return ret;
1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994
}

static int dib9000_set_tune_state(struct dvb_frontend *fe, enum frontend_tune_state tune_state)
{
	struct dib9000_state *state = fe->demodulator_priv;
	state->tune_state = tune_state;
	if (tune_state == CT_DEMOD_START)
		state->status = FE_STATUS_TUNE_PENDING;

	return 0;
}

static u32 dib9000_get_status(struct dvb_frontend *fe)
{
	struct dib9000_state *state = fe->demodulator_priv;
	return state->status;
}

static int dib9000_set_channel_status(struct dvb_frontend *fe, struct dvb_frontend_parametersContext *channel_status)
{
	struct dib9000_state *state = fe->demodulator_priv;

	memcpy(&state->channel_status, channel_status, sizeof(struct dvb_frontend_parametersContext));
	return 0;
}

1995
static int dib9000_set_frontend(struct dvb_frontend *fe)
1996 1997 1998 1999 2000 2001 2002 2003 2004
{
	struct dib9000_state *state = fe->demodulator_priv;
	int sleep_time, sleep_time_slave;
	u32 frontend_status;
	u8 nbr_pending, exit_condition, index_frontend, index_frontend_success;
	struct dvb_frontend_parametersContext channel_status;

	/* check that the correct parameters are set */
	if (state->fe[0]->dtv_property_cache.frequency == 0) {
2005
		dprintk("dib9000: must specify frequency\n");
2006 2007 2008 2009
		return 0;
	}

	if (state->fe[0]->dtv_property_cache.bandwidth_hz == 0) {
2010
		dprintk("dib9000: must specify bandwidth\n");
2011 2012
		return 0;
	}
2013 2014

	state->pid_ctrl_index = -1; /* postpone the pid filtering cmd */
2015
	if (mutex_lock_interruptible(&state->demod_lock) < 0) {
2016
		dprintk("could not get the lock\n");
2017 2018
		return 0;
	}
2019

2020 2021 2022
	fe->dtv_property_cache.delivery_system = SYS_DVBT;

	/* set the master status */
2023 2024 2025 2026
	if (state->fe[0]->dtv_property_cache.transmission_mode == TRANSMISSION_MODE_AUTO ||
	    state->fe[0]->dtv_property_cache.guard_interval == GUARD_INTERVAL_AUTO ||
	    state->fe[0]->dtv_property_cache.modulation == QAM_AUTO ||
	    state->fe[0]->dtv_property_cache.code_rate_HP == FEC_AUTO) {
2027 2028 2029 2030 2031 2032
		/* no channel specified, autosearch the channel */
		state->channel_status.status = CHANNEL_STATUS_PARAMETERS_UNKNOWN;
	} else
		state->channel_status.status = CHANNEL_STATUS_PARAMETERS_SET;

	/* set mode and status for the different frontends */
2033
	for (index_frontend = 0; (index_frontend < MAX_NUMBER_OF_FRONTENDS) && (state->fe[index_frontend] != NULL); index_frontend++) {
2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046
		dib9000_fw_set_diversity_in(state->fe[index_frontend], 1);

		/* synchronization of the cache */
		memcpy(&state->fe[index_frontend]->dtv_property_cache, &fe->dtv_property_cache, sizeof(struct dtv_frontend_properties));

		state->fe[index_frontend]->dtv_property_cache.delivery_system = SYS_DVBT;
		dib9000_fw_set_output_mode(state->fe[index_frontend], OUTMODE_HIGH_Z);

		dib9000_set_channel_status(state->fe[index_frontend], &state->channel_status);
		dib9000_set_tune_state(state->fe[index_frontend], CT_DEMOD_START);
	}

	/* actual tune */
2047
	exit_condition = 0;	/* 0: tune pending; 1: tune failed; 2:tune success */
2048 2049
	index_frontend_success = 0;
	do {
2050
		sleep_time = dib9000_fw_tune(state->fe[0]);
2051
		for (index_frontend = 1; (index_frontend < MAX_NUMBER_OF_FRONTENDS) && (state->fe[index_frontend] != NULL); index_frontend++) {
2052
			sleep_time_slave = dib9000_fw_tune(state->fe[index_frontend]);
2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065
			if (sleep_time == FE_CALLBACK_TIME_NEVER)
				sleep_time = sleep_time_slave;
			else if ((sleep_time_slave != FE_CALLBACK_TIME_NEVER) && (sleep_time_slave > sleep_time))
				sleep_time = sleep_time_slave;
		}
		if (sleep_time != FE_CALLBACK_TIME_NEVER)
			msleep(sleep_time / 10);
		else
			break;

		nbr_pending = 0;
		exit_condition = 0;
		index_frontend_success = 0;
2066
		for (index_frontend = 0; (index_frontend < MAX_NUMBER_OF_FRONTENDS) && (state->fe[index_frontend] != NULL); index_frontend++) {
2067 2068
			frontend_status = -dib9000_get_status(state->fe[index_frontend]);
			if (frontend_status > -FE_STATUS_TUNE_PENDING) {
2069
				exit_condition = 2;	/* tune success */
2070 2071 2072 2073
				index_frontend_success = index_frontend;
				break;
			}
			if (frontend_status == -FE_STATUS_TUNE_PENDING)
2074
				nbr_pending++;	/* some frontends are still tuning */
2075 2076
		}
		if ((exit_condition != 2) && (nbr_pending == 0))
2077
			exit_condition = 1;	/* if all tune are done and no success, exit: tune failed */
2078 2079 2080 2081

	} while (exit_condition == 0);

	/* check the tune result */
2082
	if (exit_condition == 1) {	/* tune failed */
2083
		dprintk("tune failed\n");
2084
		mutex_unlock(&state->demod_lock);
2085 2086
		/* tune failed; put all the pid filtering cmd to junk */
		state->pid_ctrl_index = -1;
2087 2088 2089
		return 0;
	}

2090
	dprintk("tune success on frontend%i\n", index_frontend_success);
2091 2092

	/* synchronize all the channel cache */
2093
	state->get_frontend_internal = 1;
2094
	dib9000_get_frontend(state->fe[0], &state->fe[0]->dtv_property_cache);
2095
	state->get_frontend_internal = 0;
2096 2097 2098

	/* retune the other frontends with the found channel */
	channel_status.status = CHANNEL_STATUS_PARAMETERS_SET;
2099
	for (index_frontend = 0; (index_frontend < MAX_NUMBER_OF_FRONTENDS) && (state->fe[index_frontend] != NULL); index_frontend++) {
2100 2101 2102 2103 2104 2105 2106 2107
		/* only retune the frontends which was not tuned success */
		if (index_frontend != index_frontend_success) {
			dib9000_set_channel_status(state->fe[index_frontend], &channel_status);
			dib9000_set_tune_state(state->fe[index_frontend], CT_DEMOD_START);
		}
	}
	do {
		sleep_time = FE_CALLBACK_TIME_NEVER;
2108
		for (index_frontend = 0; (index_frontend < MAX_NUMBER_OF_FRONTENDS) && (state->fe[index_frontend] != NULL); index_frontend++) {
2109
			if (index_frontend != index_frontend_success) {
2110
				sleep_time_slave = dib9000_fw_tune(state->fe[index_frontend]);
2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122
				if (sleep_time == FE_CALLBACK_TIME_NEVER)
					sleep_time = sleep_time_slave;
				else if ((sleep_time_slave != FE_CALLBACK_TIME_NEVER) && (sleep_time_slave > sleep_time))
					sleep_time = sleep_time_slave;
			}
		}
		if (sleep_time != FE_CALLBACK_TIME_NEVER)
			msleep(sleep_time / 10);
		else
			break;

		nbr_pending = 0;
2123
		for (index_frontend = 0; (index_frontend < MAX_NUMBER_OF_FRONTENDS) && (state->fe[index_frontend] != NULL); index_frontend++) {
2124 2125 2126
			if (index_frontend != index_frontend_success) {
				frontend_status = -dib9000_get_status(state->fe[index_frontend]);
				if ((index_frontend != index_frontend_success) && (frontend_status == -FE_STATUS_TUNE_PENDING))
2127
					nbr_pending++;	/* some frontends are still tuning */
2128 2129 2130 2131 2132 2133
			}
		}
	} while (nbr_pending != 0);

	/* set the output mode */
	dib9000_fw_set_output_mode(state->fe[0], state->chip.d9.cfg.output_mode);
2134
	for (index_frontend = 1; (index_frontend < MAX_NUMBER_OF_FRONTENDS) && (state->fe[index_frontend] != NULL); index_frontend++)
2135 2136 2137
		dib9000_fw_set_output_mode(state->fe[index_frontend], OUTMODE_DIVERSITY);

	/* turn off the diversity for the last frontend */
2138
	dib9000_fw_set_diversity_in(state->fe[index_frontend - 1], 0);
2139

2140
	mutex_unlock(&state->demod_lock);
2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161
	if (state->pid_ctrl_index >= 0) {
		u8 index_pid_filter_cmd;
		u8 pid_ctrl_index = state->pid_ctrl_index;

		state->pid_ctrl_index = -2;
		for (index_pid_filter_cmd = 0;
				index_pid_filter_cmd <= pid_ctrl_index;
				index_pid_filter_cmd++) {
			if (state->pid_ctrl[index_pid_filter_cmd].cmd == DIB9000_PID_FILTER_CTRL)
				dib9000_fw_pid_filter_ctrl(state->fe[0],
						state->pid_ctrl[index_pid_filter_cmd].onoff);
			else if (state->pid_ctrl[index_pid_filter_cmd].cmd == DIB9000_PID_FILTER)
				dib9000_fw_pid_filter(state->fe[0],
						state->pid_ctrl[index_pid_filter_cmd].id,
						state->pid_ctrl[index_pid_filter_cmd].pid,
						state->pid_ctrl[index_pid_filter_cmd].onoff);
		}
	}
	/* do not postpone any more the pid filtering */
	state->pid_ctrl_index = -2;

2162 2163 2164 2165 2166 2167 2168 2169 2170 2171
	return 0;
}

static u16 dib9000_read_lock(struct dvb_frontend *fe)
{
	struct dib9000_state *state = fe->demodulator_priv;

	return dib9000_read_word(state, 535);
}

2172
static int dib9000_read_status(struct dvb_frontend *fe, enum fe_status *stat)
2173 2174 2175 2176 2177
{
	struct dib9000_state *state = fe->demodulator_priv;
	u8 index_frontend;
	u16 lock = 0, lock_slave = 0;

2178
	if (mutex_lock_interruptible(&state->demod_lock) < 0) {
2179
		dprintk("could not get the lock\n");
2180 2181
		return -EINTR;
	}
2182
	for (index_frontend = 1; (index_frontend < MAX_NUMBER_OF_FRONTENDS) && (state->fe[index_frontend] != NULL); index_frontend++)
2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199
		lock_slave |= dib9000_read_lock(state->fe[index_frontend]);

	lock = dib9000_read_word(state, 535);

	*stat = 0;

	if ((lock & 0x8000) || (lock_slave & 0x8000))
		*stat |= FE_HAS_SIGNAL;
	if ((lock & 0x3000) || (lock_slave & 0x3000))
		*stat |= FE_HAS_CARRIER;
	if ((lock & 0x0100) || (lock_slave & 0x0100))
		*stat |= FE_HAS_VITERBI;
	if (((lock & 0x0038) == 0x38) || ((lock_slave & 0x0038) == 0x38))
		*stat |= FE_HAS_SYNC;
	if ((lock & 0x0008) || (lock_slave & 0x0008))
		*stat |= FE_HAS_LOCK;

2200
	mutex_unlock(&state->demod_lock);
2201

2202 2203 2204 2205 2206 2207
	return 0;
}

static int dib9000_read_ber(struct dvb_frontend *fe, u32 * ber)
{
	struct dib9000_state *state = fe->demodulator_priv;
2208
	u16 *c;
2209
	int ret = 0;
2210

2211
	if (mutex_lock_interruptible(&state->demod_lock) < 0) {
2212
		dprintk("could not get the lock\n");
2213 2214
		return -EINTR;
	}
2215
	if (mutex_lock_interruptible(&state->platform.risc.mem_mbx_lock) < 0) {
2216
		dprintk("could not get the lock\n");
2217 2218 2219
		ret = -EINTR;
		goto error;
	}
2220
	if (dib9000_fw_memmbx_sync(state, FE_SYNC_CHANNEL) < 0) {
2221
		mutex_unlock(&state->platform.risc.mem_mbx_lock);
2222 2223 2224
		ret = -EIO;
		goto error;
	}
2225 2226
	dib9000_risc_mem_read(state, FE_MM_R_FE_MONITOR,
			state->i2c_read_buffer, 16 * 2);
2227
	mutex_unlock(&state->platform.risc.mem_mbx_lock);
2228

2229 2230
	c = (u16 *)state->i2c_read_buffer;

2231
	*ber = c[10] << 16 | c[11];
2232 2233

error:
2234
	mutex_unlock(&state->demod_lock);
2235
	return ret;
2236 2237 2238 2239 2240 2241
}

static int dib9000_read_signal_strength(struct dvb_frontend *fe, u16 * strength)
{
	struct dib9000_state *state = fe->demodulator_priv;
	u8 index_frontend;
2242
	u16 *c = (u16 *)state->i2c_read_buffer;
2243
	u16 val;
2244
	int ret = 0;
2245

2246
	if (mutex_lock_interruptible(&state->demod_lock) < 0) {
2247
		dprintk("could not get the lock\n");
2248 2249
		return -EINTR;
	}
2250
	*strength = 0;
2251
	for (index_frontend = 1; (index_frontend < MAX_NUMBER_OF_FRONTENDS) && (state->fe[index_frontend] != NULL); index_frontend++) {
2252 2253 2254 2255 2256 2257 2258
		state->fe[index_frontend]->ops.read_signal_strength(state->fe[index_frontend], &val);
		if (val > 65535 - *strength)
			*strength = 65535;
		else
			*strength += val;
	}

2259
	if (mutex_lock_interruptible(&state->platform.risc.mem_mbx_lock) < 0) {
2260
		dprintk("could not get the lock\n");
2261 2262 2263
		ret = -EINTR;
		goto error;
	}
2264
	if (dib9000_fw_memmbx_sync(state, FE_SYNC_CHANNEL) < 0) {
2265
		mutex_unlock(&state->platform.risc.mem_mbx_lock);
2266 2267 2268
		ret = -EIO;
		goto error;
	}
2269
	dib9000_risc_mem_read(state, FE_MM_R_FE_MONITOR, (u8 *) c, 16 * 2);
2270
	mutex_unlock(&state->platform.risc.mem_mbx_lock);
2271 2272 2273 2274 2275 2276

	val = 65535 - c[4];
	if (val > 65535 - *strength)
		*strength = 65535;
	else
		*strength += val;
2277 2278

error:
2279
	mutex_unlock(&state->demod_lock);
2280
	return ret;
2281 2282 2283 2284 2285
}

static u32 dib9000_get_snr(struct dvb_frontend *fe)
{
	struct dib9000_state *state = fe->demodulator_priv;
2286
	u16 *c = (u16 *)state->i2c_read_buffer;
2287 2288 2289
	u32 n, s, exp;
	u16 val;

2290
	if (mutex_lock_interruptible(&state->platform.risc.mem_mbx_lock) < 0) {
2291
		dprintk("could not get the lock\n");
2292 2293
		return 0;
	}
2294
	if (dib9000_fw_memmbx_sync(state, FE_SYNC_CHANNEL) < 0) {
2295
		mutex_unlock(&state->platform.risc.mem_mbx_lock);
2296
		return 0;
2297
	}
2298
	dib9000_risc_mem_read(state, FE_MM_R_FE_MONITOR, (u8 *) c, 16 * 2);
2299
	mutex_unlock(&state->platform.risc.mem_mbx_lock);
2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328

	val = c[7];
	n = (val >> 4) & 0xff;
	exp = ((val & 0xf) << 2);
	val = c[8];
	exp += ((val >> 14) & 0x3);
	if ((exp & 0x20) != 0)
		exp -= 0x40;
	n <<= exp + 16;

	s = (val >> 6) & 0xFF;
	exp = (val & 0x3F);
	if ((exp & 0x20) != 0)
		exp -= 0x40;
	s <<= exp + 16;

	if (n > 0) {
		u32 t = (s / n) << 16;
		return t + ((s << 16) - n * t) / n;
	}
	return 0xffffffff;
}

static int dib9000_read_snr(struct dvb_frontend *fe, u16 * snr)
{
	struct dib9000_state *state = fe->demodulator_priv;
	u8 index_frontend;
	u32 snr_master;

2329
	if (mutex_lock_interruptible(&state->demod_lock) < 0) {
2330
		dprintk("could not get the lock\n");
2331 2332
		return -EINTR;
	}
2333
	snr_master = dib9000_get_snr(fe);
2334
	for (index_frontend = 1; (index_frontend < MAX_NUMBER_OF_FRONTENDS) && (state->fe[index_frontend] != NULL); index_frontend++)
2335 2336 2337 2338 2339 2340 2341 2342
		snr_master += dib9000_get_snr(state->fe[index_frontend]);

	if ((snr_master >> 16) != 0) {
		snr_master = 10 * intlog10(snr_master >> 16);
		*snr = snr_master / ((1 << 24) / 10);
	} else
		*snr = 0;

2343
	mutex_unlock(&state->demod_lock);
2344

2345 2346 2347 2348 2349 2350
	return 0;
}

static int dib9000_read_unc_blocks(struct dvb_frontend *fe, u32 * unc)
{
	struct dib9000_state *state = fe->demodulator_priv;
2351
	u16 *c = (u16 *)state->i2c_read_buffer;
2352
	int ret = 0;
2353

2354
	if (mutex_lock_interruptible(&state->demod_lock) < 0) {
2355
		dprintk("could not get the lock\n");
2356 2357
		return -EINTR;
	}
2358
	if (mutex_lock_interruptible(&state->platform.risc.mem_mbx_lock) < 0) {
2359
		dprintk("could not get the lock\n");
2360 2361 2362
		ret = -EINTR;
		goto error;
	}
2363
	if (dib9000_fw_memmbx_sync(state, FE_SYNC_CHANNEL) < 0) {
2364
		mutex_unlock(&state->platform.risc.mem_mbx_lock);
2365 2366 2367
		ret = -EIO;
		goto error;
	}
2368
	dib9000_risc_mem_read(state, FE_MM_R_FE_MONITOR, (u8 *) c, 16 * 2);
2369
	mutex_unlock(&state->platform.risc.mem_mbx_lock);
2370 2371

	*unc = c[12];
2372 2373

error:
2374
	mutex_unlock(&state->demod_lock);
2375
	return ret;
2376 2377 2378 2379
}

int dib9000_i2c_enumeration(struct i2c_adapter *i2c, int no_of_demods, u8 default_addr, u8 first_addr)
{
2380
	int k = 0, ret = 0;
2381 2382 2383
	u8 new_addr = 0;
	struct i2c_device client = {.i2c_adap = i2c };

K
Kees Cook 已提交
2384
	client.i2c_write_buffer = kzalloc(4, GFP_KERNEL);
2385
	if (!client.i2c_write_buffer) {
2386
		dprintk("%s: not enough memory\n", __func__);
2387 2388
		return -ENOMEM;
	}
K
Kees Cook 已提交
2389
	client.i2c_read_buffer = kzalloc(4, GFP_KERNEL);
2390
	if (!client.i2c_read_buffer) {
2391
		dprintk("%s: not enough memory\n", __func__);
2392 2393 2394 2395
		ret = -ENOMEM;
		goto error_memory;
	}

2396
	client.i2c_addr = default_addr + 16;
2397
	dib9000_i2c_write16(&client, 1796, 0x0);
2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417

	for (k = no_of_demods - 1; k >= 0; k--) {
		/* designated i2c address */
		new_addr = first_addr + (k << 1);
		client.i2c_addr = default_addr;

		dib9000_i2c_write16(&client, 1817, 3);
		dib9000_i2c_write16(&client, 1796, 0);
		dib9000_i2c_write16(&client, 1227, 1);
		dib9000_i2c_write16(&client, 1227, 0);

		client.i2c_addr = new_addr;
		dib9000_i2c_write16(&client, 1817, 3);
		dib9000_i2c_write16(&client, 1796, 0);
		dib9000_i2c_write16(&client, 1227, 1);
		dib9000_i2c_write16(&client, 1227, 0);

		if (dib9000_identify(&client) == 0) {
			client.i2c_addr = default_addr;
			if (dib9000_identify(&client) == 0) {
2418
				dprintk("DiB9000 #%d: not identified\n", k);
2419 2420
				ret = -EIO;
				goto error;
2421 2422 2423 2424 2425 2426
			}
		}

		dib9000_i2c_write16(&client, 1795, (1 << 10) | (4 << 6));
		dib9000_i2c_write16(&client, 1794, (new_addr << 2) | 2);

2427
		dprintk("IC %d initialized (to i2c_address 0x%x)\n", k, new_addr);
2428 2429 2430 2431 2432 2433 2434 2435 2436 2437
	}

	for (k = 0; k < no_of_demods; k++) {
		new_addr = first_addr | (k << 1);
		client.i2c_addr = new_addr;

		dib9000_i2c_write16(&client, 1794, (new_addr << 2));
		dib9000_i2c_write16(&client, 1795, 0);
	}

2438 2439 2440 2441 2442 2443
error:
	kfree(client.i2c_read_buffer);
error_memory:
	kfree(client.i2c_write_buffer);

	return ret;
2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454
}
EXPORT_SYMBOL(dib9000_i2c_enumeration);

int dib9000_set_slave_frontend(struct dvb_frontend *fe, struct dvb_frontend *fe_slave)
{
	struct dib9000_state *state = fe->demodulator_priv;
	u8 index_frontend = 1;

	while ((index_frontend < MAX_NUMBER_OF_FRONTENDS) && (state->fe[index_frontend] != NULL))
		index_frontend++;
	if (index_frontend < MAX_NUMBER_OF_FRONTENDS) {
2455
		dprintk("set slave fe %p to index %i\n", fe_slave, index_frontend);
2456 2457 2458 2459
		state->fe[index_frontend] = fe_slave;
		return 0;
	}

2460
	dprintk("too many slave frontend\n");
2461 2462 2463 2464
	return -ENOMEM;
}
EXPORT_SYMBOL(dib9000_set_slave_frontend);

2465
struct dvb_frontend *dib9000_get_slave_frontend(struct dvb_frontend *fe, int slave_index)
2466 2467 2468 2469 2470 2471 2472 2473 2474
{
	struct dib9000_state *state = fe->demodulator_priv;

	if (slave_index >= MAX_NUMBER_OF_FRONTENDS)
		return NULL;
	return state->fe[slave_index];
}
EXPORT_SYMBOL(dib9000_get_slave_frontend);

2475
static const struct dvb_frontend_ops dib9000_ops;
2476 2477 2478 2479 2480 2481 2482 2483
struct dvb_frontend *dib9000_attach(struct i2c_adapter *i2c_adap, u8 i2c_addr, const struct dib9000_config *cfg)
{
	struct dvb_frontend *fe;
	struct dib9000_state *st;
	st = kzalloc(sizeof(struct dib9000_state), GFP_KERNEL);
	if (st == NULL)
		return NULL;
	fe = kzalloc(sizeof(struct dvb_frontend), GFP_KERNEL);
2484 2485
	if (fe == NULL) {
		kfree(st);
2486
		return NULL;
2487
	}
2488 2489 2490 2491

	memcpy(&st->chip.d9.cfg, cfg, sizeof(struct dib9000_config));
	st->i2c.i2c_adap = i2c_adap;
	st->i2c.i2c_addr = i2c_addr;
2492 2493
	st->i2c.i2c_write_buffer = st->i2c_write_buffer;
	st->i2c.i2c_read_buffer = st->i2c_read_buffer;
2494 2495 2496 2497 2498

	st->gpio_dir = DIB9000_GPIO_DEFAULT_DIRECTIONS;
	st->gpio_val = DIB9000_GPIO_DEFAULT_VALUES;
	st->gpio_pwm_pos = DIB9000_GPIO_DEFAULT_PWM_POS;

2499 2500 2501 2502 2503
	mutex_init(&st->platform.risc.mbx_if_lock);
	mutex_init(&st->platform.risc.mbx_lock);
	mutex_init(&st->platform.risc.mem_lock);
	mutex_init(&st->platform.risc.mem_mbx_lock);
	mutex_init(&st->demod_lock);
2504 2505 2506
	st->get_frontend_internal = 0;

	st->pid_ctrl_index = -2;
2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523

	st->fe[0] = fe;
	fe->demodulator_priv = st;
	memcpy(&st->fe[0]->ops, &dib9000_ops, sizeof(struct dvb_frontend_ops));

	/* Ensure the output mode remains at the previous default if it's
	 * not specifically set by the caller.
	 */
	if ((st->chip.d9.cfg.output_mode != OUTMODE_MPEG2_SERIAL) && (st->chip.d9.cfg.output_mode != OUTMODE_MPEG2_PAR_GATED_CLK))
		st->chip.d9.cfg.output_mode = OUTMODE_MPEG2_FIFO;

	if (dib9000_identify(&st->i2c) == 0)
		goto error;

	dibx000_init_i2c_master(&st->i2c_master, DIB7000MC, st->i2c.i2c_adap, st->i2c.i2c_addr);

	st->tuner_adap.dev.parent = i2c_adap->dev.parent;
2524 2525
	strscpy(st->tuner_adap.name, "DIB9000_FW TUNER ACCESS",
		sizeof(st->tuner_adap.name));
2526 2527 2528 2529 2530 2531 2532
	st->tuner_adap.algo = &dib9000_tuner_algo;
	st->tuner_adap.algo_data = NULL;
	i2c_set_adapdata(&st->tuner_adap, st);
	if (i2c_add_adapter(&st->tuner_adap) < 0)
		goto error;

	st->component_bus.dev.parent = i2c_adap->dev.parent;
2533 2534
	strscpy(st->component_bus.name, "DIB9000_FW COMPONENT BUS ACCESS",
		sizeof(st->component_bus.name));
2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545
	st->component_bus.algo = &dib9000_component_bus_algo;
	st->component_bus.algo_data = NULL;
	st->component_bus_speed = 340;
	i2c_set_adapdata(&st->component_bus, st);
	if (i2c_add_adapter(&st->component_bus) < 0)
		goto component_bus_add_error;

	dib9000_fw_reset(fe);

	return fe;

2546
component_bus_add_error:
2547
	i2c_del_adapter(&st->tuner_adap);
2548
error:
2549 2550 2551 2552 2553
	kfree(st);
	return NULL;
}
EXPORT_SYMBOL(dib9000_attach);

2554
static const struct dvb_frontend_ops dib9000_ops = {
2555
	.delsys = { SYS_DVBT },
2556 2557
	.info = {
		 .name = "DiBcom 9000",
2558 2559 2560
		 .frequency_min_hz =  44250 * kHz,
		 .frequency_max_hz = 867250 * kHz,
		 .frequency_stepsize_hz = 62500,
2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572
		 .caps = FE_CAN_INVERSION_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_FEC_AUTO |
		 FE_CAN_QPSK | FE_CAN_QAM_16 | FE_CAN_QAM_64 | FE_CAN_QAM_AUTO |
		 FE_CAN_TRANSMISSION_MODE_AUTO | FE_CAN_GUARD_INTERVAL_AUTO | FE_CAN_RECOVER | FE_CAN_HIERARCHY_AUTO,
		 },

	.release = dib9000_release,

	.init = dib9000_wakeup,
	.sleep = dib9000_sleep,

2573
	.set_frontend = dib9000_set_frontend,
2574
	.get_tune_settings = dib9000_fe_get_tune_settings,
2575
	.get_frontend = dib9000_get_frontend,
2576 2577 2578 2579 2580 2581 2582 2583

	.read_status = dib9000_read_status,
	.read_ber = dib9000_read_ber,
	.read_signal_strength = dib9000_read_signal_strength,
	.read_snr = dib9000_read_snr,
	.read_ucblocks = dib9000_read_unc_blocks,
};

2584 2585
MODULE_AUTHOR("Patrick Boettcher <patrick.boettcher@posteo.de>");
MODULE_AUTHOR("Olivier Grenie <olivier.grenie@parrot.com>");
2586 2587
MODULE_DESCRIPTION("Driver for the DiBcom 9000 COFDM demodulator");
MODULE_LICENSE("GPL");