af9035.c 25.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
/*
 * Afatech AF9035 DVB USB driver
 *
 * Copyright (C) 2009 Antti Palosaari <crope@iki.fi>
 * Copyright (C) 2012 Antti Palosaari <crope@iki.fi>
 *
 *    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; either version 2 of the License, or
 *    (at your option) any later version.
 *
 *    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.
 */

#include "af9035.h"

DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);

26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
static u16 af9035_checksum(const u8 *buf, size_t len)
{
	size_t i;
	u16 checksum = 0;

	for (i = 1; i < len; i++) {
		if (i % 2)
			checksum += buf[i] << 8;
		else
			checksum += buf[i];
	}
	checksum = ~checksum;

	return checksum;
}

42
static int af9035_ctrl_msg(struct dvb_usb_device *d, struct usb_req *req)
43
{
44
#define BUF_LEN 64
45 46 47 48
#define REQ_HDR_LEN 4 /* send header size */
#define ACK_HDR_LEN 3 /* rece header size */
#define CHECKSUM_LEN 2
#define USB_TIMEOUT 2000
49 50
	struct state *state = d_to_priv(d);
	int ret, wlen, rlen;
51
	u8 buf[BUF_LEN];
52
	u16 checksum, tmp_checksum;
53 54 55 56 57 58 59 60 61 62 63 64

	/* buffer overflow check */
	if (req->wlen > (BUF_LEN - REQ_HDR_LEN - CHECKSUM_LEN) ||
		req->rlen > (BUF_LEN - ACK_HDR_LEN - CHECKSUM_LEN)) {
		pr_debug("%s: too much data wlen=%d rlen=%d\n", __func__,
				req->wlen, req->rlen);
		return -EINVAL;
	}

	buf[0] = REQ_HDR_LEN + req->wlen + CHECKSUM_LEN - 1;
	buf[1] = req->mbox;
	buf[2] = req->cmd;
65 66 67 68 69
	buf[3] = state->seq++;
	memcpy(&buf[REQ_HDR_LEN], req->wbuf, req->wlen);

	wlen = REQ_HDR_LEN + req->wlen + CHECKSUM_LEN;
	rlen = ACK_HDR_LEN + req->rlen + CHECKSUM_LEN;
70 71

	/* calc and add checksum */
72
	checksum = af9035_checksum(buf, buf[0] - 1);
73 74
	buf[buf[0] - 1] = (checksum >> 8);
	buf[buf[0] - 0] = (checksum & 0xff);
75

76 77 78
	/* no ack for these packets */
	if (req->cmd == CMD_FW_DL)
		rlen = 0;
79

80
	ret = dvb_usbv2_generic_rw(d, buf, wlen, buf, rlen);
81 82
	if (ret)
		goto err;
83 84 85

	/* no ack for those packets */
	if (req->cmd == CMD_FW_DL)
86
		goto exit;
87

88
	/* verify checksum */
89 90
	checksum = af9035_checksum(buf, rlen - 2);
	tmp_checksum = (buf[rlen - 2] << 8) | buf[rlen - 1];
91
	if (tmp_checksum != checksum) {
92 93 94
		pr_err("%s: command=%02x checksum mismatch (%04x != %04x)\n",
				KBUILD_MODNAME, req->cmd, tmp_checksum,
				checksum);
95
		ret = -EIO;
96
		goto err;
97
	}
98

99 100 101 102 103
	/* check status */
	if (buf[2]) {
		pr_debug("%s: command=%02x failed fw error=%d\n", __func__,
				req->cmd, buf[2]);
		ret = -EIO;
104
		goto err;
105 106 107 108 109 110
	}

	/* read request, copy returned data to return buf */
	if (req->rlen)
		memcpy(req->rbuf, &buf[ACK_HDR_LEN], req->rlen);

111 112 113 114 115
exit:
	return 0;

err:
	pr_debug("%s: failed=%d\n", __func__, ret);
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134

	return ret;
}

/* write multiple registers */
static int af9035_wr_regs(struct dvb_usb_device *d, u32 reg, u8 *val, int len)
{
	u8 wbuf[6 + len];
	u8 mbox = (reg >> 16) & 0xff;
	struct usb_req req = { CMD_MEM_WR, mbox, sizeof(wbuf), wbuf, 0, NULL };

	wbuf[0] = len;
	wbuf[1] = 2;
	wbuf[2] = 0;
	wbuf[3] = 0;
	wbuf[4] = (reg >> 8) & 0xff;
	wbuf[5] = (reg >> 0) & 0xff;
	memcpy(&wbuf[6], val, len);

135
	return af9035_ctrl_msg(d, &req);
136 137 138 139 140 141 142 143 144
}

/* read multiple registers */
static int af9035_rd_regs(struct dvb_usb_device *d, u32 reg, u8 *val, int len)
{
	u8 wbuf[] = { len, 2, 0, 0, (reg >> 8) & 0xff, reg & 0xff };
	u8 mbox = (reg >> 16) & 0xff;
	struct usb_req req = { CMD_MEM_RD, mbox, sizeof(wbuf), wbuf, len, val };

145
	return af9035_ctrl_msg(d, &req);
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
}

/* write single register */
static int af9035_wr_reg(struct dvb_usb_device *d, u32 reg, u8 val)
{
	return af9035_wr_regs(d, reg, &val, 1);
}

/* read single register */
static int af9035_rd_reg(struct dvb_usb_device *d, u32 reg, u8 *val)
{
	return af9035_rd_regs(d, reg, val, 1);
}

/* write single register with mask */
static int af9035_wr_reg_mask(struct dvb_usb_device *d, u32 reg, u8 val,
		u8 mask)
{
	int ret;
	u8 tmp;

	/* no need for read if whole reg is written */
	if (mask != 0xff) {
		ret = af9035_rd_regs(d, reg, &tmp, 1);
		if (ret)
			return ret;

		val &= mask;
		tmp &= ~mask;
		val |= tmp;
	}

	return af9035_wr_regs(d, reg, &val, 1);
}

static int af9035_i2c_master_xfer(struct i2c_adapter *adap,
		struct i2c_msg msg[], int num)
{
	struct dvb_usb_device *d = i2c_get_adapdata(adap);
185
	struct state *state = d_to_priv(d);
186 187 188 189 190
	int ret;

	if (mutex_lock_interruptible(&d->i2c_mutex) < 0)
		return -EAGAIN;

191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206
	/*
	 * I2C sub header is 5 bytes long. Meaning of those bytes are:
	 * 0: data len
	 * 1: I2C addr << 1
	 * 2: reg addr len
	 *    byte 3 and 4 can be used as reg addr
	 * 3: reg addr MSB
	 *    used when reg addr len is set to 2
	 * 4: reg addr LSB
	 *    used when reg addr len is set to 1 or 2
	 *
	 * For the simplify we do not use register addr at all.
	 * NOTE: As a firmware knows tuner type there is very small possibility
	 * there could be some tuner I2C hacks done by firmware and this may
	 * lead problems if firmware expects those bytes are used.
	 */
207 208 209 210 211
	if (num == 2 && !(msg[0].flags & I2C_M_RD) &&
			(msg[1].flags & I2C_M_RD)) {
		if (msg[0].len > 40 || msg[1].len > 40) {
			/* TODO: correct limits > 40 */
			ret = -EOPNOTSUPP;
212
		} else if (msg[0].addr == state->af9033_config[0].i2c_addr) {
213 214 215 216 217 218 219
			/* integrated demod */
			u32 reg = msg[0].buf[0] << 16 | msg[0].buf[1] << 8 |
					msg[0].buf[2];
			ret = af9035_rd_regs(d, reg, &msg[1].buf[0],
					msg[1].len);
		} else {
			/* I2C */
220
			u8 buf[5 + msg[0].len];
221 222
			struct usb_req req = { CMD_I2C_RD, 0, sizeof(buf),
					buf, msg[1].len, msg[1].buf };
H
Hans-Frieder Vogt 已提交
223
			buf[0] = msg[1].len;
224
			buf[1] = msg[0].addr << 1;
225 226 227 228
			buf[2] = 0x00; /* reg addr len */
			buf[3] = 0x00; /* reg addr MSB */
			buf[4] = 0x00; /* reg addr LSB */
			memcpy(&buf[5], msg[0].buf, msg[0].len);
229
			ret = af9035_ctrl_msg(d, &req);
230 231 232 233 234
		}
	} else if (num == 1 && !(msg[0].flags & I2C_M_RD)) {
		if (msg[0].len > 40) {
			/* TODO: correct limits > 40 */
			ret = -EOPNOTSUPP;
235
		} else if (msg[0].addr == state->af9033_config[0].i2c_addr) {
236 237 238 239 240 241 242
			/* integrated demod */
			u32 reg = msg[0].buf[0] << 16 | msg[0].buf[1] << 8 |
					msg[0].buf[2];
			ret = af9035_wr_regs(d, reg, &msg[0].buf[3],
					msg[0].len - 3);
		} else {
			/* I2C */
243
			u8 buf[5 + msg[0].len];
244 245 246 247
			struct usb_req req = { CMD_I2C_WR, 0, sizeof(buf), buf,
					0, NULL };
			buf[0] = msg[0].len;
			buf[1] = msg[0].addr << 1;
248 249 250 251
			buf[2] = 0x00; /* reg addr len */
			buf[3] = 0x00; /* reg addr MSB */
			buf[4] = 0x00; /* reg addr LSB */
			memcpy(&buf[5], msg[0].buf, msg[0].len);
252
			ret = af9035_ctrl_msg(d, &req);
253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280
		}
	} else {
		/*
		 * We support only two kind of I2C transactions:
		 * 1) 1 x read + 1 x write
		 * 2) 1 x write
		 */
		ret = -EOPNOTSUPP;
	}

	mutex_unlock(&d->i2c_mutex);

	if (ret < 0)
		return ret;
	else
		return num;
}

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

static struct i2c_algorithm af9035_i2c_algo = {
	.master_xfer = af9035_i2c_master_xfer,
	.functionality = af9035_i2c_functionality,
};

281
static int af9035_identify_state(struct dvb_usb_device *d, const char **name)
282 283 284 285 286 287 288
{
	int ret;
	u8 wbuf[1] = { 1 };
	u8 rbuf[4];
	struct usb_req req = { CMD_FW_QUERYINFO, 0, sizeof(wbuf), wbuf,
			sizeof(rbuf), rbuf };

289
	ret = af9035_ctrl_msg(d, &req);
290 291 292
	if (ret < 0)
		goto err;

293
	pr_debug("%s: reply=%*ph\n", __func__, 4, rbuf);
294
	if (rbuf[0] || rbuf[1] || rbuf[2] || rbuf[3])
295
		ret = WARM;
296
	else
297
		ret = COLD;
298

299
	return ret;
300 301 302 303 304 305 306

err:
	pr_debug("%s: failed=%d\n", __func__, ret);

	return ret;
}

307
static int af9035_download_firmware(struct dvb_usb_device *d,
308 309
		const struct firmware *fw)
{
310
	int ret, i, j, len;
311 312 313 314 315
	u8 wbuf[1];
	u8 rbuf[4];
	struct usb_req req = { 0, 0, 0, NULL, 0, NULL };
	struct usb_req req_fw_dl = { CMD_FW_DL, 0, 0, wbuf, 0, NULL };
	struct usb_req req_fw_ver = { CMD_FW_QUERYINFO, 0, 1, wbuf, 4, rbuf } ;
316 317
	u8 hdr_core;
	u16 hdr_addr, hdr_data_len, hdr_checksum;
318
	#define MAX_DATA 58
319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350
	#define HDR_SIZE 7

	/*
	 * Thanks to Daniel Glöckner <daniel-gl@gmx.net> about that info!
	 *
	 * byte 0: MCS 51 core
	 *  There are two inside the AF9035 (1=Link and 2=OFDM) with separate
	 *  address spaces
	 * byte 1-2: Big endian destination address
	 * byte 3-4: Big endian number of data bytes following the header
	 * byte 5-6: Big endian header checksum, apparently ignored by the chip
	 *  Calculated as ~(h[0]*256+h[1]+h[2]*256+h[3]+h[4]*256)
	 */

	for (i = fw->size; i > HDR_SIZE;) {
		hdr_core = fw->data[fw->size - i + 0];
		hdr_addr = fw->data[fw->size - i + 1] << 8;
		hdr_addr |= fw->data[fw->size - i + 2] << 0;
		hdr_data_len = fw->data[fw->size - i + 3] << 8;
		hdr_data_len |= fw->data[fw->size - i + 4] << 0;
		hdr_checksum = fw->data[fw->size - i + 5] << 8;
		hdr_checksum |= fw->data[fw->size - i + 6] << 0;

		pr_debug("%s: core=%d addr=%04x data_len=%d checksum=%04x\n",
				__func__, hdr_core, hdr_addr, hdr_data_len,
				hdr_checksum);

		if (((hdr_core != 1) && (hdr_core != 2)) ||
				(hdr_data_len > i)) {
			pr_debug("%s: bad firmware\n", __func__);
			break;
		}
351

352 353
		/* download begin packet */
		req.cmd = CMD_FW_DL_BEGIN;
354
		ret = af9035_ctrl_msg(d, &req);
355 356
		if (ret < 0)
			goto err;
357 358 359 360 361 362 363 364 365

		/* download firmware packet(s) */
		for (j = HDR_SIZE + hdr_data_len; j > 0; j -= MAX_DATA) {
			len = j;
			if (len > MAX_DATA)
				len = MAX_DATA;
			req_fw_dl.wlen = len;
			req_fw_dl.wbuf = (u8 *) &fw->data[fw->size - i +
					HDR_SIZE + hdr_data_len - j];
366
			ret = af9035_ctrl_msg(d, &req_fw_dl);
367
			if (ret < 0)
368 369
				goto err;
		}
370 371 372

		/* download end packet */
		req.cmd = CMD_FW_DL_END;
373
		ret = af9035_ctrl_msg(d, &req);
374 375 376 377 378
		if (ret < 0)
			goto err;

		i -= hdr_data_len + HDR_SIZE;

G
Gianluca Gennari 已提交
379
		pr_debug("%s: data uploaded=%zu\n", __func__, fw->size - i);
380 381 382 383
	}

	/* firmware loaded, request boot */
	req.cmd = CMD_FW_BOOT;
384
	ret = af9035_ctrl_msg(d, &req);
385 386 387 388 389
	if (ret < 0)
		goto err;

	/* ensure firmware starts */
	wbuf[0] = 1;
390
	ret = af9035_ctrl_msg(d, &req_fw_ver);
391 392 393 394
	if (ret < 0)
		goto err;

	if (!(rbuf[0] || rbuf[1] || rbuf[2] || rbuf[3])) {
395
		pr_err("%s: firmware did not run\n", KBUILD_MODNAME);
396 397 398 399
		ret = -ENODEV;
		goto err;
	}

400 401
	pr_info("%s: firmware version=%d.%d.%d.%d", KBUILD_MODNAME,
			rbuf[0], rbuf[1], rbuf[2], rbuf[3]);
402

403 404 405 406 407 408 409 410
	return 0;

err:
	pr_debug("%s: failed=%d\n", __func__, ret);

	return ret;
}

411
static int af9035_download_firmware_it9135(struct dvb_usb_device *d,
412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443
		const struct firmware *fw)
{
	int ret, i, i_prev;
	u8 wbuf[1];
	u8 rbuf[4];
	struct usb_req req = { 0, 0, 0, NULL, 0, NULL };
	struct usb_req req_fw_dl = { CMD_FW_SCATTER_WR, 0, 0, NULL, 0, NULL };
	struct usb_req req_fw_ver = { CMD_FW_QUERYINFO, 0, 1, wbuf, 4, rbuf } ;
	#define HDR_SIZE 7

	/*
	 * There seems to be following firmware header. Meaning of bytes 0-3
	 * is unknown.
	 *
	 * 0: 3
	 * 1: 0, 1
	 * 2: 0
	 * 3: 1, 2, 3
	 * 4: addr MSB
	 * 5: addr LSB
	 * 6: count of data bytes ?
	 */

	for (i = HDR_SIZE, i_prev = 0; i <= fw->size; i++) {
		if (i == fw->size ||
				(fw->data[i + 0] == 0x03 &&
				(fw->data[i + 1] == 0x00 ||
				fw->data[i + 1] == 0x01) &&
				fw->data[i + 2] == 0x00)) {
			req_fw_dl.wlen = i - i_prev;
			req_fw_dl.wbuf = (u8 *) &fw->data[i_prev];
			i_prev = i;
444
			ret = af9035_ctrl_msg(d, &req_fw_dl);
445 446 447 448 449 450 451 452 453
			if (ret < 0)
				goto err;

			pr_debug("%s: data uploaded=%d\n", __func__, i);
		}
	}

	/* firmware loaded, request boot */
	req.cmd = CMD_FW_BOOT;
454
	ret = af9035_ctrl_msg(d, &req);
455 456 457 458 459
	if (ret < 0)
		goto err;

	/* ensure firmware starts */
	wbuf[0] = 1;
460
	ret = af9035_ctrl_msg(d, &req_fw_ver);
461 462 463 464
	if (ret < 0)
		goto err;

	if (!(rbuf[0] || rbuf[1] || rbuf[2] || rbuf[3])) {
465
		pr_err("%s: firmware did not run\n", KBUILD_MODNAME);
466 467 468 469
		ret = -ENODEV;
		goto err;
	}

470 471
	pr_info("%s: firmware version=%d.%d.%d.%d", KBUILD_MODNAME,
			rbuf[0], rbuf[1], rbuf[2], rbuf[3]);
472 473 474 475 476 477 478 479 480

	return 0;

err:
	pr_debug("%s: failed=%d\n", __func__, ret);

	return ret;
}

481
static int af9035_read_config(struct dvb_usb_device *d)
482
{
483
	struct state *state = d_to_priv(d);
484 485 486 487 488 489 490 491 492
	int ret, i, eeprom_shift = 0;
	u8 tmp;
	u16 tmp16;

	/* check if there is dual tuners */
	ret = af9035_rd_reg(d, EEPROM_DUAL_MODE, &tmp);
	if (ret < 0)
		goto err;

493 494
	state->dual_mode = tmp;
	pr_debug("%s: dual mode=%d\n", __func__, state->dual_mode);
495

496
	for (i = 0; i < state->dual_mode + 1; i++) {
497 498 499 500 501
		/* tuner */
		ret = af9035_rd_reg(d, EEPROM_1_TUNER_ID + eeprom_shift, &tmp);
		if (ret < 0)
			goto err;

502
		state->af9033_config[i].tuner = tmp;
503 504 505 506
		pr_debug("%s: [%d]tuner=%02x\n", __func__, i, tmp);

		switch (tmp) {
		case AF9033_TUNER_TUA9001:
507
		case AF9033_TUNER_FC0011:
508
		case AF9033_TUNER_MXL5007T:
509
		case AF9033_TUNER_TDA18218:
510
			state->af9033_config[i].spec_inv = 1;
511 512
			break;
		default:
513 514
			pr_info("%s: tuner ID=%02x not supported, please " \
					"report!", KBUILD_MODNAME, tmp);
515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541
		};

		/* tuner IF frequency */
		ret = af9035_rd_reg(d, EEPROM_1_IFFREQ_L + eeprom_shift, &tmp);
		if (ret < 0)
			goto err;

		tmp16 = tmp;

		ret = af9035_rd_reg(d, EEPROM_1_IFFREQ_H + eeprom_shift, &tmp);
		if (ret < 0)
			goto err;

		tmp16 |= tmp << 8;

		pr_debug("%s: [%d]IF=%d\n", __func__, i, tmp16);

		eeprom_shift = 0x10; /* shift for the 2nd tuner params */
	}

	/* get demod clock */
	ret = af9035_rd_reg(d, 0x00d800, &tmp);
	if (ret < 0)
		goto err;

	tmp = (tmp >> 0) & 0x0f;

542
	for (i = 0; i < ARRAY_SIZE(state->af9033_config); i++)
543
		state->af9033_config[i].clock = clock_lut[tmp];
544 545 546 547 548 549 550 551 552

	return 0;

err:
	pr_debug("%s: failed=%d\n", __func__, ret);

	return ret;
}

553
static int af9035_read_config_it9135(struct dvb_usb_device *d)
554
{
555
	struct state *state = d_to_priv(d);
556 557 558
	int ret, i;
	u8 tmp;

559
	state->dual_mode = false;
560 561 562 563 564 565 566 567

	/* get demod clock */
	ret = af9035_rd_reg(d, 0x00d800, &tmp);
	if (ret < 0)
		goto err;

	tmp = (tmp >> 0) & 0x0f;

568
	for (i = 0; i < ARRAY_SIZE(state->af9033_config); i++)
569
		state->af9033_config[i].clock = clock_lut_it9135[tmp];
570 571 572 573 574 575 576 577 578

	return 0;

err:
	pr_debug("%s: failed=%d\n", __func__, ret);

	return ret;
}

579
static int af9035_fc0011_tuner_callback(struct dvb_usb_device *d,
580
		int cmd, int arg)
581
{
582
	int ret;
583 584 585 586

	switch (cmd) {
	case FC0011_FE_CALLBACK_POWER:
		/* Tuner enable */
587 588 589 590 591 592 593 594 595 596 597 598
		ret = af9035_wr_reg_mask(d, 0xd8eb, 1, 1);
		if (ret < 0)
			goto err;

		ret = af9035_wr_reg_mask(d, 0xd8ec, 1, 1);
		if (ret < 0)
			goto err;

		ret = af9035_wr_reg_mask(d, 0xd8ed, 1, 1);
		if (ret < 0)
			goto err;

599
		/* LED */
600 601 602 603 604 605 606 607
		ret = af9035_wr_reg_mask(d, 0xd8d0, 1, 1);
		if (ret < 0)
			goto err;

		ret = af9035_wr_reg_mask(d, 0xd8d1, 1, 1);
		if (ret < 0)
			goto err;

608
		usleep_range(10000, 50000);
609 610
		break;
	case FC0011_FE_CALLBACK_RESET:
611 612 613 614 615 616 617 618 619 620 621 622
		ret = af9035_wr_reg(d, 0xd8e9, 1);
		if (ret < 0)
			goto err;

		ret = af9035_wr_reg(d, 0xd8e8, 1);
		if (ret < 0)
			goto err;

		ret = af9035_wr_reg(d, 0xd8e7, 1);
		if (ret < 0)
			goto err;

623
		usleep_range(10000, 20000);
624 625 626 627 628

		ret = af9035_wr_reg(d, 0xd8e7, 0);
		if (ret < 0)
			goto err;

629
		usleep_range(10000, 20000);
630 631
		break;
	default:
632 633
		ret = -EINVAL;
		goto err;
634 635 636
	}

	return 0;
637 638 639 640 641

err:
	pr_debug("%s: failed=%d\n", __func__, ret);

	return ret;
642 643 644 645
}

static int af9035_tuner_callback(struct dvb_usb_device *d, int cmd, int arg)
{
646
	struct state *state = d_to_priv(d);
647 648

	switch (state->af9033_config[0].tuner) {
649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673
	case AF9033_TUNER_FC0011:
		return af9035_fc0011_tuner_callback(d, cmd, arg);
	default:
		break;
	}

	return -ENODEV;
}

static int af9035_frontend_callback(void *adapter_priv, int component,
				    int cmd, int arg)
{
	struct i2c_adapter *adap = adapter_priv;
	struct dvb_usb_device *d = i2c_get_adapdata(adap);

	switch (component) {
	case DVB_FRONTEND_COMPONENT_TUNER:
		return af9035_tuner_callback(d, cmd, arg);
	default:
		break;
	}

	return -EINVAL;
}

674 675
static int af9035_frontend_attach(struct dvb_usb_adapter *adap)
{
676 677
	struct state *state = adap_to_priv(adap);
	struct dvb_usb_device *d = adap_to_d(adap);
678 679
	int ret;

680 681
	if (!state->af9033_config[adap->id].tuner) {
		/* unsupported tuner */
682 683 684 685
		ret = -ENODEV;
		goto err;
	}

686
	if (adap->id == 0) {
687 688 689
		state->af9033_config[0].ts_mode = AF9033_TS_MODE_USB;
		state->af9033_config[1].ts_mode = AF9033_TS_MODE_SERIAL;

690
		ret = af9035_wr_reg(d, 0x00417f,
691
				state->af9033_config[1].i2c_addr);
692 693 694
		if (ret < 0)
			goto err;

695
		ret = af9035_wr_reg(d, 0x00d81a,
696
				state->dual_mode);
697 698 699 700 701
		if (ret < 0)
			goto err;
	}

	/* attach demodulator */
702 703 704
	adap->fe[0] = dvb_attach(af9033_attach,
			&state->af9033_config[adap->id], &d->i2c_adap);
	if (adap->fe[0] == NULL) {
705 706 707
		ret = -ENODEV;
		goto err;
	}
708 709

	/* disable I2C-gate */
710 711
	adap->fe[0]->ops.i2c_gate_ctrl = NULL;
	adap->fe[0]->callback = af9035_frontend_callback;
712 713 714 715 716 717 718 719 720 721 722 723 724

	return 0;

err:
	pr_debug("%s: failed=%d\n", __func__, ret);

	return ret;
}

static struct tua9001_config af9035_tua9001_config = {
	.i2c_addr = 0x60,
};

725 726 727 728
static const struct fc0011_config af9035_fc0011_config = {
	.i2c_address = 0x60,
};

729 730 731 732 733 734 735 736 737
static struct mxl5007t_config af9035_mxl5007t_config = {
	.xtal_freq_hz = MxL_XTAL_24_MHZ,
	.if_freq_hz = MxL_IF_4_57_MHZ,
	.invert_if = 0,
	.loop_thru_enable = 0,
	.clk_out_enable = 0,
	.clk_out_amp = MxL_CLKOUT_AMP_0_94V,
};

738 739 740 741 742
static struct tda18218_config af9035_tda18218_config = {
	.i2c_address = 0x60,
	.i2c_wr_max = 21,
};

743 744
static int af9035_tuner_attach(struct dvb_usb_adapter *adap)
{
745 746
	struct state *state = adap_to_priv(adap);
	struct dvb_usb_device *d = adap_to_d(adap);
747 748 749
	int ret;
	struct dvb_frontend *fe;

750
	switch (state->af9033_config[adap->id].tuner) {
751 752 753 754 755
	case AF9033_TUNER_TUA9001:
		/* AF9035 gpiot3 = TUA9001 RESETN
		   AF9035 gpiot2 = TUA9001 RXEN */

		/* configure gpiot2 and gpiot2 as output */
756
		ret = af9035_wr_reg_mask(d, 0x00d8ec, 0x01, 0x01);
757 758 759
		if (ret < 0)
			goto err;

760
		ret = af9035_wr_reg_mask(d, 0x00d8ed, 0x01, 0x01);
761 762 763
		if (ret < 0)
			goto err;

764
		ret = af9035_wr_reg_mask(d, 0x00d8e8, 0x01, 0x01);
765 766 767
		if (ret < 0)
			goto err;

768
		ret = af9035_wr_reg_mask(d, 0x00d8e9, 0x01, 0x01);
769 770 771 772
		if (ret < 0)
			goto err;

		/* reset tuner */
773
		ret = af9035_wr_reg_mask(d, 0x00d8e7, 0x00, 0x01);
774 775 776 777 778
		if (ret < 0)
			goto err;

		usleep_range(2000, 20000);

779
		ret = af9035_wr_reg_mask(d, 0x00d8e7, 0x01, 0x01);
780 781 782 783 784
		if (ret < 0)
			goto err;

		/* activate tuner RX */
		/* TODO: use callback for TUA9001 RXEN */
785
		ret = af9035_wr_reg_mask(d, 0x00d8eb, 0x01, 0x01);
786 787 788 789
		if (ret < 0)
			goto err;

		/* attach tuner */
790 791
		fe = dvb_attach(tua9001_attach, adap->fe[0],
				&d->i2c_adap, &af9035_tua9001_config);
792
		break;
793
	case AF9033_TUNER_FC0011:
794 795
		fe = dvb_attach(fc0011_attach, adap->fe[0],
				&d->i2c_adap, &af9035_fc0011_config);
796
		break;
797
	case AF9033_TUNER_MXL5007T:
798
		ret = af9035_wr_reg(d, 0x00d8e0, 1);
799 800
		if (ret < 0)
			goto err;
801
		ret = af9035_wr_reg(d, 0x00d8e1, 1);
802 803
		if (ret < 0)
			goto err;
804
		ret = af9035_wr_reg(d, 0x00d8df, 0);
805 806 807 808 809
		if (ret < 0)
			goto err;

		msleep(30);

810
		ret = af9035_wr_reg(d, 0x00d8df, 1);
811 812 813 814 815
		if (ret < 0)
			goto err;

		msleep(300);

816
		ret = af9035_wr_reg(d, 0x00d8c0, 1);
817 818
		if (ret < 0)
			goto err;
819
		ret = af9035_wr_reg(d, 0x00d8c1, 1);
820 821
		if (ret < 0)
			goto err;
822
		ret = af9035_wr_reg(d, 0x00d8bf, 0);
823 824
		if (ret < 0)
			goto err;
825
		ret = af9035_wr_reg(d, 0x00d8b4, 1);
826 827
		if (ret < 0)
			goto err;
828
		ret = af9035_wr_reg(d, 0x00d8b5, 1);
829 830
		if (ret < 0)
			goto err;
831
		ret = af9035_wr_reg(d, 0x00d8b3, 1);
832 833 834 835
		if (ret < 0)
			goto err;

		/* attach tuner */
836 837
		fe = dvb_attach(mxl5007t_attach, adap->fe[0],
				&d->i2c_adap, 0x60, &af9035_mxl5007t_config);
838
		break;
839 840
	case AF9033_TUNER_TDA18218:
		/* attach tuner */
841 842
		fe = dvb_attach(tda18218_attach, adap->fe[0],
				&d->i2c_adap, &af9035_tda18218_config);
843
		break;
844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860
	default:
		fe = NULL;
	}

	if (fe == NULL) {
		ret = -ENODEV;
		goto err;
	}

	return 0;

err:
	pr_debug("%s: failed=%d\n", __func__, ret);

	return ret;
}

861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886
static int af9035_init(struct dvb_usb_device *d)
{
	struct state *state = d_to_priv(d);
	int ret, i;
	u16 frame_size = 87 * 188 / 4;
	u8  packet_size = 512 / 4;
	struct reg_val_mask tab[] = {
		{ 0x80f99d, 0x01, 0x01 },
		{ 0x80f9a4, 0x01, 0x01 },
		{ 0x00dd11, 0x00, 0x20 },
		{ 0x00dd11, 0x00, 0x40 },
		{ 0x00dd13, 0x00, 0x20 },
		{ 0x00dd13, 0x00, 0x40 },
		{ 0x00dd11, 0x20, 0x20 },
		{ 0x00dd88, (frame_size >> 0) & 0xff, 0xff},
		{ 0x00dd89, (frame_size >> 8) & 0xff, 0xff},
		{ 0x00dd0c, packet_size, 0xff},
		{ 0x00dd11, state->dual_mode << 6, 0x40 },
		{ 0x00dd8a, (frame_size >> 0) & 0xff, 0xff},
		{ 0x00dd8b, (frame_size >> 8) & 0xff, 0xff},
		{ 0x00dd0d, packet_size, 0xff },
		{ 0x80f9a3, 0x00, 0x01 },
		{ 0x80f9cd, 0x00, 0x01 },
		{ 0x80f99d, 0x00, 0x01 },
		{ 0x80f9a4, 0x00, 0x01 },
	};
887

888 889
	pr_debug("%s: USB speed=%d frame_size=%04x packet_size=%02x\n",
		__func__, d->udev->speed, frame_size, packet_size);
890

891 892 893 894 895 896 897
	/* init endpoints */
	for (i = 0; i < ARRAY_SIZE(tab); i++) {
		ret = af9035_wr_reg_mask(d, tab[i].reg, tab[i].val,
				tab[i].mask);
		if (ret < 0)
			goto err;
	}
898

899
	return 0;
900

901 902
err:
	pr_debug("%s: failed=%d\n", __func__, ret);
903

904 905
	return ret;
}
906

907 908 909 910 911 912
static int af9035_rc_query(struct dvb_usb_device *d)
{
	unsigned int key;
	unsigned char b[4];
	int ret;
	struct usb_req req = { CMD_IR_GET, 0, 0, NULL, 4, b };
913

914 915 916
	ret = af9035_ctrl_msg(d, &req);
	if (ret < 0)
		goto err;
917

918 919 920 921 922 923 924
	if ((b[2] + b[3]) == 0xff) {
		if ((b[0] + b[1]) == 0xff) {
			/* NEC */
			key = b[0] << 8 | b[2];
		} else {
			/* ext. NEC */
			key = b[0] << 16 | b[1] << 8 | b[2];
925
		}
926 927
	} else {
		key = b[0] << 24 | b[1] << 16 | b[2] << 8 | b[3];
928 929
	}

930
	rc_keydown(d->rc_dev, key, 0);
931

932 933 934 935
err:
	/* ignore errors */
	return 0;
}
936

937 938 939 940
static int af9035_get_rc_config(struct dvb_usb_device *d, struct dvb_usb_rc *rc)
{
	int ret;
	u8 tmp;
941

942
	ret = af9035_rd_reg(d, EEPROM_IR_MODE, &tmp);
943 944 945
	if (ret < 0)
		goto err;

946 947 948 949 950
	pr_debug("%s: ir_mode=%02x\n", __func__, tmp);

	/* don't activate rc if in HID mode or if not available */
	if (tmp == 5) {
		ret = af9035_rd_reg(d, EEPROM_IR_TYPE, &tmp);
951 952
		if (ret < 0)
			goto err;
953 954 955 956 957 958 959 960 961 962 963 964 965 966 967

		pr_debug("%s: ir_type=%02x\n", __func__, tmp);

		switch (tmp) {
		case 0: /* NEC */
		default:
			rc->allowed_protos = RC_TYPE_NEC;
			break;
		case 1: /* RC6 */
			rc->allowed_protos = RC_TYPE_RC6;
			break;
		}

		rc->query = af9035_rc_query;
		rc->interval = 500;
968 969 970 971

		/* load empty to enable rc */
		if (!rc->map_name)
			rc->map_name = RC_MAP_EMPTY;
972 973 974 975 976 977 978 979 980 981
	}

	return 0;

err:
	pr_debug("%s: failed=%d\n", __func__, ret);

	return ret;
}

982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070
/* interface 0 is used by DVB-T receiver and
   interface 1 is for remote controller (HID) */
static const struct dvb_usb_device_properties af9035_props = {
	.driver_name = KBUILD_MODNAME,
	.owner = THIS_MODULE,
	.adapter_nr = adapter_nr,
	.size_of_priv = sizeof(struct state),

	.generic_bulk_ctrl_endpoint = 0x02,
	.generic_bulk_ctrl_endpoint_response = 0x81,

	.identify_state = af9035_identify_state,
	.firmware = "dvb-usb-af9035-02.fw",
	.download_firmware = af9035_download_firmware,

	.i2c_algo = &af9035_i2c_algo,
	.read_config = af9035_read_config,
	.frontend_attach = af9035_frontend_attach,
	.tuner_attach = af9035_tuner_attach,
	.init = af9035_init,
	.get_rc_config = af9035_get_rc_config,

	.num_adapters = 1,
	.adapter = {
		{
			.stream = DVB_USB_STREAM_BULK(0x84, 6, 87 * 188),
		}, {
			.stream = DVB_USB_STREAM_BULK(0x85, 6, 87 * 188),
		},
	},
};

static const struct dvb_usb_device_properties it9135_props = {
	.driver_name = KBUILD_MODNAME,
	.owner = THIS_MODULE,
	.adapter_nr = adapter_nr,
	.size_of_priv = sizeof(struct state),

	.generic_bulk_ctrl_endpoint = 0x02,
	.generic_bulk_ctrl_endpoint_response = 0x81,

	.identify_state = af9035_identify_state,
	.firmware = "dvb-usb-it9135-01.fw",
	.download_firmware = af9035_download_firmware_it9135,

	.i2c_algo = &af9035_i2c_algo,
	.read_config = af9035_read_config_it9135,
	.frontend_attach = af9035_frontend_attach,
	.tuner_attach = af9035_tuner_attach,
	.init = af9035_init,
	.get_rc_config = af9035_get_rc_config,

	.num_adapters = 1,
	.adapter = {
		{
			.stream = DVB_USB_STREAM_BULK(0x84, 6, 87 * 188),
		}, {
			.stream = DVB_USB_STREAM_BULK(0x85, 6, 87 * 188),
		},
	},
};

static const struct usb_device_id af9035_id_table[] = {
	{ DVB_USB_DEVICE(USB_VID_AFATECH, USB_PID_AFATECH_AF9035_9035,
		&af9035_props, "Afatech AF9035 reference design", NULL) },
	{ DVB_USB_DEVICE(USB_VID_AFATECH, USB_PID_AFATECH_AF9035_1000,
		&af9035_props, "Afatech AF9035 reference design", NULL) },
	{ DVB_USB_DEVICE(USB_VID_AFATECH, USB_PID_AFATECH_AF9035_1001,
		&af9035_props, "Afatech AF9035 reference design", NULL) },
	{ DVB_USB_DEVICE(USB_VID_AFATECH, USB_PID_AFATECH_AF9035_1002,
		&af9035_props, "Afatech AF9035 reference design", NULL) },
	{ DVB_USB_DEVICE(USB_VID_AFATECH, USB_PID_AFATECH_AF9035_1003,
		&af9035_props, "Afatech AF9035 reference design", NULL) },
	{ DVB_USB_DEVICE(USB_VID_TERRATEC, USB_PID_TERRATEC_CINERGY_T_STICK,
		&af9035_props, "TerraTec Cinergy T Stick", NULL) },
	{ DVB_USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_A835,
		&af9035_props, "AVerMedia AVerTV Volar HD/PRO (A835)", NULL) },
	{ DVB_USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_B835,
		&af9035_props, "AVerMedia AVerTV Volar HD/PRO (A835)", NULL) },
	{ DVB_USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_1867,
		&af9035_props, "AVerMedia HD Volar (A867)", NULL) },
	{ DVB_USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_A867,
		&af9035_props, "AVerMedia HD Volar (A867)", NULL) },
	{ DVB_USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_TWINSTAR,
		&af9035_props, "AVerMedia Twinstar (A825)", NULL) },
	{ }
};
MODULE_DEVICE_TABLE(usb, af9035_id_table);

1071
static struct usb_driver af9035_usb_driver = {
1072 1073 1074 1075 1076 1077 1078 1079
	.name = KBUILD_MODNAME,
	.id_table = af9035_id_table,
	.probe = dvb_usbv2_probe,
	.disconnect = dvb_usbv2_disconnect,
	.suspend = dvb_usbv2_suspend,
	.resume = dvb_usbv2_resume,
	.no_dynamic_id = 1,
	.soft_unbind = 1,
1080 1081
};

1082
module_usb_driver(af9035_usb_driver);
1083 1084 1085 1086

MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
MODULE_DESCRIPTION("Afatech AF9035 driver");
MODULE_LICENSE("GPL");