af9035.c 54.1 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
/*
 * 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"

24 25 26
/* Max transfer size done by I2C transfer functions */
#define MAX_XFER_SIZE  64

27 28
DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);

29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
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;
}

45
static int af9035_ctrl_msg(struct dvb_usb_device *d, struct usb_req *req)
46 47 48 49 50
{
#define REQ_HDR_LEN 4 /* send header size */
#define ACK_HDR_LEN 3 /* rece header size */
#define CHECKSUM_LEN 2
#define USB_TIMEOUT 2000
51
	struct state *state = d_to_priv(d);
A
Antti Palosaari 已提交
52
	struct usb_interface *intf = d->intf;
53
	int ret, wlen, rlen;
54
	u16 checksum, tmp_checksum;
55

56 57
	mutex_lock(&d->usb_mutex);

58 59
	/* buffer overflow check */
	if (req->wlen > (BUF_LEN - REQ_HDR_LEN - CHECKSUM_LEN) ||
60
			req->rlen > (BUF_LEN - ACK_HDR_LEN - CHECKSUM_LEN)) {
A
Antti Palosaari 已提交
61 62
		dev_err(&intf->dev, "too much data wlen=%d rlen=%d\n",
			req->wlen, req->rlen);
63
		ret = -EINVAL;
64
		goto exit;
65 66
	}

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

	wlen = REQ_HDR_LEN + req->wlen + CHECKSUM_LEN;
	rlen = ACK_HDR_LEN + req->rlen + CHECKSUM_LEN;
75 76

	/* calc and add checksum */
77 78 79
	checksum = af9035_checksum(state->buf, state->buf[0] - 1);
	state->buf[state->buf[0] - 1] = (checksum >> 8);
	state->buf[state->buf[0] - 0] = (checksum & 0xff);
80

81 82 83
	/* no ack for these packets */
	if (req->cmd == CMD_FW_DL)
		rlen = 0;
84

85 86
	ret = dvb_usbv2_generic_rw_locked(d,
			state->buf, wlen, state->buf, rlen);
87
	if (ret)
88
		goto exit;
89 90 91

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

94
	/* verify checksum */
95 96
	checksum = af9035_checksum(state->buf, rlen - 2);
	tmp_checksum = (state->buf[rlen - 2] << 8) | state->buf[rlen - 1];
97
	if (tmp_checksum != checksum) {
A
Antti Palosaari 已提交
98 99
		dev_err(&intf->dev, "command=%02x checksum mismatch (%04x != %04x)\n",
			req->cmd, tmp_checksum, checksum);
100
		ret = -EIO;
101
		goto exit;
102
	}
103

104
	/* check status */
105
	if (state->buf[2]) {
106
		/* fw returns status 1 when IR code was not received */
107 108 109 110
		if (req->cmd == CMD_IR_GET || state->buf[2] == 1) {
			ret = 1;
			goto exit;
		}
111

A
Antti Palosaari 已提交
112 113
		dev_dbg(&intf->dev, "command=%02x failed fw error=%d\n",
			req->cmd, state->buf[2]);
114
		ret = -EIO;
115
		goto exit;
116 117 118 119
	}

	/* read request, copy returned data to return buf */
	if (req->rlen)
120
		memcpy(req->rbuf, &state->buf[ACK_HDR_LEN], req->rlen);
121
exit:
122
	mutex_unlock(&d->usb_mutex);
123
	if (ret < 0)
A
Antti Palosaari 已提交
124
		dev_dbg(&intf->dev, "failed=%d\n", ret);
125 126 127 128 129 130
	return ret;
}

/* write multiple registers */
static int af9035_wr_regs(struct dvb_usb_device *d, u32 reg, u8 *val, int len)
{
A
Antti Palosaari 已提交
131
	struct usb_interface *intf = d->intf;
132
	u8 wbuf[MAX_XFER_SIZE];
133
	u8 mbox = (reg >> 16) & 0xff;
134
	struct usb_req req = { CMD_MEM_WR, mbox, 6 + len, wbuf, 0, NULL };
135

136
	if (6 + len > sizeof(wbuf)) {
A
Antti Palosaari 已提交
137
		dev_warn(&intf->dev, "i2c wr: len=%d is too big!\n", len);
138 139 140
		return -EOPNOTSUPP;
	}

141 142 143 144 145 146 147 148
	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);

149
	return af9035_ctrl_msg(d, &req);
150 151 152 153 154 155 156 157 158
}

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

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

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

195 196
static int af9035_add_i2c_dev(struct dvb_usb_device *d, const char *type,
		u8 addr, void *platform_data, struct i2c_adapter *adapter)
197 198 199
{
	int ret, num;
	struct state *state = d_to_priv(d);
A
Antti Palosaari 已提交
200
	struct usb_interface *intf = d->intf;
201 202 203 204 205 206 207 208 209 210 211 212 213 214
	struct i2c_client *client;
	struct i2c_board_info board_info = {
		.addr = addr,
		.platform_data = platform_data,
	};

	strlcpy(board_info.type, type, I2C_NAME_SIZE);

	/* find first free client */
	for (num = 0; num < AF9035_I2C_CLIENT_MAX; num++) {
		if (state->i2c_client[num] == NULL)
			break;
	}

A
Antti Palosaari 已提交
215
	dev_dbg(&intf->dev, "num=%d\n", num);
216 217

	if (num == AF9035_I2C_CLIENT_MAX) {
A
Antti Palosaari 已提交
218
		dev_err(&intf->dev, "I2C client out of index\n");
219 220 221 222
		ret = -ENODEV;
		goto err;
	}

223
	request_module("%s", board_info.type);
224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241

	/* register I2C device */
	client = i2c_new_device(adapter, &board_info);
	if (client == NULL || client->dev.driver == NULL) {
		ret = -ENODEV;
		goto err;
	}

	/* increase I2C driver usage count */
	if (!try_module_get(client->dev.driver->owner)) {
		i2c_unregister_device(client);
		ret = -ENODEV;
		goto err;
	}

	state->i2c_client[num] = client;
	return 0;
err:
A
Antti Palosaari 已提交
242
	dev_dbg(&intf->dev, "failed=%d\n", ret);
243 244 245 246 247 248 249
	return ret;
}

static void af9035_del_i2c_dev(struct dvb_usb_device *d)
{
	int num;
	struct state *state = d_to_priv(d);
A
Antti Palosaari 已提交
250
	struct usb_interface *intf = d->intf;
251 252 253 254 255 256 257 258 259
	struct i2c_client *client;

	/* find last used client */
	num = AF9035_I2C_CLIENT_MAX;
	while (num--) {
		if (state->i2c_client[num] != NULL)
			break;
	}

A
Antti Palosaari 已提交
260
	dev_dbg(&intf->dev, "num=%d\n", num);
261 262

	if (num == -1) {
A
Antti Palosaari 已提交
263
		dev_err(&intf->dev, "I2C client out of index\n");
264 265 266 267 268 269 270 271 272 273 274 275 276 277
		goto err;
	}

	client = state->i2c_client[num];

	/* decrease I2C driver usage count */
	module_put(client->dev.driver->owner);

	/* unregister I2C device */
	i2c_unregister_device(client);

	state->i2c_client[num] = NULL;
	return;
err:
A
Antti Palosaari 已提交
278
	dev_dbg(&intf->dev, "failed\n");
279 280
}

281 282 283 284
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);
285
	struct state *state = d_to_priv(d);
286 287 288 289 290
	int ret;

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

291
	/*
292
	 * AF9035 I2C sub header is 5 bytes long. Meaning of those bytes are:
293 294 295 296 297 298 299 300 301 302 303 304 305
	 * 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.
306 307 308 309 310 311 312 313 314 315 316 317 318
	 *
	 * TODO: Here is few hacks. AF9035 chip integrates AF9033 demodulator.
	 * IT9135 chip integrates AF9033 demodulator and RF tuner. For dual
	 * tuner devices, there is also external AF9033 demodulator connected
	 * via external I2C bus. All AF9033 demod I2C traffic, both single and
	 * dual tuner configuration, is covered by firmware - actual USB IO
	 * looks just like a memory access.
	 * In case of IT913x chip, there is own tuner driver. It is implemented
	 * currently as a I2C driver, even tuner IP block is likely build
	 * directly into the demodulator memory space and there is no own I2C
	 * bus. I2C subsystem does not allow register multiple devices to same
	 * bus, having same slave address. Due to that we reuse demod address,
	 * shifted by one bit, on that case.
319 320 321 322 323 324
	 *
	 * For IT930x we use a different command and the sub header is
	 * different as well:
	 * 0: data len
	 * 1: I2C bus (0x03 seems to be only value used)
	 * 2: I2C addr << 1
325
	 */
326 327 328 329 330 331 332 333
#define AF9035_IS_I2C_XFER_WRITE_READ(_msg, _num) \
	(_num == 2 && !(_msg[0].flags & I2C_M_RD) && (_msg[1].flags & I2C_M_RD))
#define AF9035_IS_I2C_XFER_WRITE(_msg, _num) \
	(_num == 1 && !(_msg[0].flags & I2C_M_RD))
#define AF9035_IS_I2C_XFER_READ(_msg, _num) \
	(_num == 1 && (_msg[0].flags & I2C_M_RD))

	if (AF9035_IS_I2C_XFER_WRITE_READ(msg, num)) {
334 335 336
		if (msg[0].len > 40 || msg[1].len > 40) {
			/* TODO: correct limits > 40 */
			ret = -EOPNOTSUPP;
337 338
		} else if ((msg[0].addr == state->af9033_i2c_addr[0]) ||
			   (msg[0].addr == state->af9033_i2c_addr[1]) ||
339
			   (state->chip_type == 0x9135)) {
340
			/* demod access via firmware interface */
341 342
			u32 reg = msg[0].buf[0] << 16 | msg[0].buf[1] << 8 |
					msg[0].buf[2];
343

344 345
			if (msg[0].addr == state->af9033_i2c_addr[1] ||
			    msg[0].addr == (state->af9033_i2c_addr[1] >> 1))
346
				reg |= 0x100000;
347

348 349
			ret = af9035_rd_regs(d, reg, &msg[1].buf[0],
					msg[1].len);
350 351 352
		} else if (state->no_read) {
			memset(msg[1].buf, 0, msg[1].len);
			ret = 0;
353
		} else {
354
			/* I2C write + read */
355
			u8 buf[MAX_XFER_SIZE];
356
			struct usb_req req = { CMD_I2C_RD, 0, 5 + msg[0].len,
357
					buf, msg[1].len, msg[1].buf };
358

359 360 361 362
			if (state->chip_type == 0x9306) {
				req.cmd = CMD_GENERIC_I2C_RD;
				req.wlen = 3 + msg[0].len;
			}
363
			req.mbox |= ((msg[0].addr & 0x80)  >>  3);
364

H
Hans-Frieder Vogt 已提交
365
			buf[0] = msg[1].len;
366 367 368 369 370 371 372 373
			if (state->chip_type == 0x9306) {
				buf[1] = 0x03; /* I2C bus */
				buf[2] = msg[0].addr << 1;
				memcpy(&buf[3], msg[0].buf, msg[0].len);
			} else {
				buf[1] = msg[0].addr << 1;
				buf[3] = 0x00; /* reg addr MSB */
				buf[4] = 0x00; /* reg addr LSB */
374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390

				/* Keep prev behavior for write req len > 2*/
				if (msg[0].len > 2) {
					buf[2] = 0x00; /* reg addr len */
					memcpy(&buf[5], msg[0].buf, msg[0].len);

				/* Use reg addr fields if write req len <= 2 */
				} else {
					req.wlen = 5;
					buf[2] = msg[0].len;
					if (msg[0].len == 2) {
						buf[3] = msg[0].buf[0];
						buf[4] = msg[0].buf[1];
					} else if (msg[0].len == 1) {
						buf[4] = msg[0].buf[0];
					}
				}
391
			}
392
			ret = af9035_ctrl_msg(d, &req);
393
		}
394
	} else if (AF9035_IS_I2C_XFER_WRITE(msg, num)) {
395 396 397
		if (msg[0].len > 40) {
			/* TODO: correct limits > 40 */
			ret = -EOPNOTSUPP;
398 399
		} else if ((msg[0].addr == state->af9033_i2c_addr[0]) ||
			   (msg[0].addr == state->af9033_i2c_addr[1]) ||
400
			   (state->chip_type == 0x9135)) {
401
			/* demod access via firmware interface */
402 403
			u32 reg = msg[0].buf[0] << 16 | msg[0].buf[1] << 8 |
					msg[0].buf[2];
404

405 406
			if (msg[0].addr == state->af9033_i2c_addr[1] ||
			    msg[0].addr == (state->af9033_i2c_addr[1] >> 1))
407
				reg |= 0x100000;
408

409 410 411
			ret = af9035_wr_regs(d, reg, &msg[0].buf[3],
					msg[0].len - 3);
		} else {
412
			/* I2C write */
413
			u8 buf[MAX_XFER_SIZE];
414 415
			struct usb_req req = { CMD_I2C_WR, 0, 5 + msg[0].len,
					buf, 0, NULL };
416

417 418 419 420 421
			if (state->chip_type == 0x9306) {
				req.cmd = CMD_GENERIC_I2C_WR;
				req.wlen = 3 + msg[0].len;
			}

422
			req.mbox |= ((msg[0].addr & 0x80)  >>  3);
423
			buf[0] = msg[0].len;
424 425 426 427 428 429 430 431 432 433 434
			if (state->chip_type == 0x9306) {
				buf[1] = 0x03; /* I2C bus */
				buf[2] = msg[0].addr << 1;
				memcpy(&buf[3], msg[0].buf, msg[0].len);
			} else {
				buf[1] = msg[0].addr << 1;
				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);
			}
435
			ret = af9035_ctrl_msg(d, &req);
436
		}
437
	} else if (AF9035_IS_I2C_XFER_READ(msg, num)) {
438 439 440
		if (msg[0].len > 40) {
			/* TODO: correct limits > 40 */
			ret = -EOPNOTSUPP;
441 442 443
		} else if (state->no_read) {
			memset(msg[0].buf, 0, msg[0].len);
			ret = 0;
444
		} else {
445
			/* I2C read */
446 447
			u8 buf[5];
			struct usb_req req = { CMD_I2C_RD, 0, sizeof(buf),
448 449 450 451 452 453
						buf, msg[0].len, msg[0].buf };

			if (state->chip_type == 0x9306) {
				req.cmd = CMD_GENERIC_I2C_RD;
				req.wlen = 3;
			}
454 455
			req.mbox |= ((msg[0].addr & 0x80)  >>  3);
			buf[0] = msg[0].len;
456 457 458 459 460 461 462 463 464
			if (state->chip_type == 0x9306) {
				buf[1] = 0x03; /* I2C bus */
				buf[2] = msg[0].addr << 1;
			} else {
				buf[1] = msg[0].addr << 1;
				buf[2] = 0x00; /* reg addr len */
				buf[3] = 0x00; /* reg addr MSB */
				buf[4] = 0x00; /* reg addr LSB */
			}
465 466
			ret = af9035_ctrl_msg(d, &req);
		}
467 468
	} else {
		/*
469
		 * We support only three kind of I2C transactions:
470
		 * 1) 1 x write + 1 x read (repeated start)
471
		 * 2) 1 x write
472
		 * 3) 1 x read
473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494
		 */
		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,
};

495
static int af9035_identify_state(struct dvb_usb_device *d, const char **name)
496
{
497
	struct state *state = d_to_priv(d);
A
Antti Palosaari 已提交
498
	struct usb_interface *intf = d->intf;
499 500
	int ret, ts_mode_invalid;
	u8 tmp;
501 502 503 504 505
	u8 wbuf[1] = { 1 };
	u8 rbuf[4];
	struct usb_req req = { CMD_FW_QUERYINFO, 0, sizeof(wbuf), wbuf,
			sizeof(rbuf), rbuf };

506 507 508 509 510 511 512 513 514 515 516
	ret = af9035_rd_regs(d, 0x1222, rbuf, 3);
	if (ret < 0)
		goto err;

	state->chip_version = rbuf[0];
	state->chip_type = rbuf[2] << 8 | rbuf[1] << 0;

	ret = af9035_rd_reg(d, 0x384f, &state->prechip_version);
	if (ret < 0)
		goto err;

A
Antti Palosaari 已提交
517 518
	dev_info(&intf->dev, "prechip_version=%02x chip_version=%02x chip_type=%04x\n",
		 state->prechip_version, state->chip_version, state->chip_type);
519 520

	if (state->chip_type == 0x9135) {
521
		if (state->chip_version == 0x02)
522 523 524
			*name = AF9035_FIRMWARE_IT9135_V2;
		else
			*name = AF9035_FIRMWARE_IT9135_V1;
525
		state->eeprom_addr = EEPROM_BASE_IT9135;
526 527 528
	} else if (state->chip_type == 0x9306) {
		*name = AF9035_FIRMWARE_IT9303;
		state->eeprom_addr = EEPROM_BASE_IT9135;
529 530
	} else {
		*name = AF9035_FIRMWARE_AF9035;
531
		state->eeprom_addr = EEPROM_BASE_AF9035;
532 533
	}

534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563

	/* check for dual tuner mode */
	ret = af9035_rd_reg(d, state->eeprom_addr + EEPROM_TS_MODE, &tmp);
	if (ret < 0)
		goto err;

	ts_mode_invalid = 0;
	switch (tmp) {
	case 0:
		break;
	case 1:
	case 3:
		state->dual_mode = true;
		break;
	case 5:
		if (state->chip_type != 0x9135 && state->chip_type != 0x9306)
			state->dual_mode = true;	/* AF9035 */
		else
			ts_mode_invalid = 1;
		break;
	default:
		ts_mode_invalid = 1;
	}

	dev_dbg(&intf->dev, "ts mode=%d dual mode=%d\n", tmp, state->dual_mode);

	if (ts_mode_invalid)
		dev_info(&intf->dev, "ts mode=%d not supported, defaulting to single tuner mode!", tmp);


564
	ret = af9035_ctrl_msg(d, &req);
565 566 567
	if (ret < 0)
		goto err;

A
Antti Palosaari 已提交
568
	dev_dbg(&intf->dev, "reply=%*ph\n", 4, rbuf);
569
	if (rbuf[0] || rbuf[1] || rbuf[2] || rbuf[3])
570
		ret = WARM;
571
	else
572
		ret = COLD;
573

574
	return ret;
575 576

err:
A
Antti Palosaari 已提交
577
	dev_dbg(&intf->dev, "failed=%d\n", ret);
578 579 580 581

	return ret;
}

582
static int af9035_download_firmware_old(struct dvb_usb_device *d,
583 584
		const struct firmware *fw)
{
A
Antti Palosaari 已提交
585
	struct usb_interface *intf = d->intf;
586
	int ret, i, j, len;
587 588 589
	u8 wbuf[1];
	struct usb_req req = { 0, 0, 0, NULL, 0, NULL };
	struct usb_req req_fw_dl = { CMD_FW_DL, 0, 0, wbuf, 0, NULL };
590
	u8 hdr_core;
591
	u16 hdr_addr, hdr_data_len, hdr_checksum;
592
	#define MAX_DATA 58
593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615
	#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;

A
Antti Palosaari 已提交
616 617
		dev_dbg(&intf->dev, "core=%d addr=%04x data_len=%d checksum=%04x\n",
			hdr_core, hdr_addr, hdr_data_len, hdr_checksum);
618 619 620

		if (((hdr_core != 1) && (hdr_core != 2)) ||
				(hdr_data_len > i)) {
A
Antti Palosaari 已提交
621
			dev_dbg(&intf->dev, "bad firmware\n");
622 623
			break;
		}
624

625 626
		/* download begin packet */
		req.cmd = CMD_FW_DL_BEGIN;
627
		ret = af9035_ctrl_msg(d, &req);
628 629
		if (ret < 0)
			goto err;
630 631 632 633 634 635 636 637 638

		/* 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];
639
			ret = af9035_ctrl_msg(d, &req_fw_dl);
640
			if (ret < 0)
641 642
				goto err;
		}
643 644 645

		/* download end packet */
		req.cmd = CMD_FW_DL_END;
646
		ret = af9035_ctrl_msg(d, &req);
647 648 649 650 651
		if (ret < 0)
			goto err;

		i -= hdr_data_len + HDR_SIZE;

A
Antti Palosaari 已提交
652
		dev_dbg(&intf->dev, "data uploaded=%zu\n", fw->size - i);
653 654
	}

655 656
	/* print warn if firmware is bad, continue and see what happens */
	if (i)
A
Antti Palosaari 已提交
657
		dev_warn(&intf->dev, "bad firmware\n");
658

659 660 661
	return 0;

err:
A
Antti Palosaari 已提交
662
	dev_dbg(&intf->dev, "failed=%d\n", ret);
663 664 665 666

	return ret;
}

667
static int af9035_download_firmware_new(struct dvb_usb_device *d,
668 669
		const struct firmware *fw)
{
A
Antti Palosaari 已提交
670
	struct usb_interface *intf = d->intf;
671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695
	int ret, i, i_prev;
	struct usb_req req_fw_dl = { CMD_FW_SCATTER_WR, 0, 0, NULL, 0, NULL };
	#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;
696
			ret = af9035_ctrl_msg(d, &req_fw_dl);
697 698 699
			if (ret < 0)
				goto err;

A
Antti Palosaari 已提交
700
			dev_dbg(&intf->dev, "data uploaded=%d\n", i);
701 702 703
		}
	}

704 705 706
	return 0;

err:
A
Antti Palosaari 已提交
707
	dev_dbg(&intf->dev, "failed=%d\n", ret);
708 709 710 711 712 713 714

	return ret;
}

static int af9035_download_firmware(struct dvb_usb_device *d,
		const struct firmware *fw)
{
A
Antti Palosaari 已提交
715
	struct usb_interface *intf = d->intf;
716 717 718 719 720 721
	struct state *state = d_to_priv(d);
	int ret;
	u8 wbuf[1];
	u8 rbuf[4];
	u8 tmp;
	struct usb_req req = { 0, 0, 0, NULL, 0, NULL };
722
	struct usb_req req_fw_ver = { CMD_FW_QUERYINFO, 0, 1, wbuf, 4, rbuf };
723

A
Antti Palosaari 已提交
724
	dev_dbg(&intf->dev, "\n");
725 726 727 728 729 730 731

	/*
	 * In case of dual tuner configuration we need to do some extra
	 * initialization in order to download firmware to slave demod too,
	 * which is done by master demod.
	 * Master feeds also clock and controls power via GPIO.
	 */
732
	if (state->dual_mode) {
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
		/* configure gpioh1, reset & power slave demod */
		ret = af9035_wr_reg_mask(d, 0x00d8b0, 0x01, 0x01);
		if (ret < 0)
			goto err;

		ret = af9035_wr_reg_mask(d, 0x00d8b1, 0x01, 0x01);
		if (ret < 0)
			goto err;

		ret = af9035_wr_reg_mask(d, 0x00d8af, 0x00, 0x01);
		if (ret < 0)
			goto err;

		usleep_range(10000, 50000);

		ret = af9035_wr_reg_mask(d, 0x00d8af, 0x01, 0x01);
		if (ret < 0)
			goto err;

		/* tell the slave I2C address */
		ret = af9035_rd_reg(d,
				state->eeprom_addr + EEPROM_2ND_DEMOD_ADDR,
				&tmp);
		if (ret < 0)
			goto err;

759 760 761
		/* use default I2C address if eeprom has no address set */
		if (!tmp)
			tmp = 0x3a;
762

763 764
		if ((state->chip_type == 0x9135) ||
				(state->chip_type == 0x9306)) {
765 766 767 768 769 770 771 772 773 774 775 776 777 778 779
			ret = af9035_wr_reg(d, 0x004bfb, tmp);
			if (ret < 0)
				goto err;
		} else {
			ret = af9035_wr_reg(d, 0x00417f, tmp);
			if (ret < 0)
				goto err;

			/* enable clock out */
			ret = af9035_wr_reg_mask(d, 0x00d81a, 0x01, 0x01);
			if (ret < 0)
				goto err;
		}
	}

780 781
	if (fw->data[0] == 0x01)
		ret = af9035_download_firmware_old(d, fw);
782
	else
783
		ret = af9035_download_firmware_new(d, fw);
784 785 786
	if (ret < 0)
		goto err;

787 788
	/* firmware loaded, request boot */
	req.cmd = CMD_FW_BOOT;
789
	ret = af9035_ctrl_msg(d, &req);
790 791 792 793 794
	if (ret < 0)
		goto err;

	/* ensure firmware starts */
	wbuf[0] = 1;
795
	ret = af9035_ctrl_msg(d, &req_fw_ver);
796 797 798 799
	if (ret < 0)
		goto err;

	if (!(rbuf[0] || rbuf[1] || rbuf[2] || rbuf[3])) {
A
Antti Palosaari 已提交
800
		dev_err(&intf->dev, "firmware did not run\n");
801 802 803 804
		ret = -ENODEV;
		goto err;
	}

A
Antti Palosaari 已提交
805 806
	dev_info(&intf->dev, "firmware version=%d.%d.%d.%d",
		 rbuf[0], rbuf[1], rbuf[2], rbuf[3]);
807 808 809 810

	return 0;

err:
A
Antti Palosaari 已提交
811
	dev_dbg(&intf->dev, "failed=%d\n", ret);
812 813 814 815

	return ret;
}

816
static int af9035_read_config(struct dvb_usb_device *d)
817
{
A
Antti Palosaari 已提交
818
	struct usb_interface *intf = d->intf;
819
	struct state *state = d_to_priv(d);
820
	int ret, i;
821
	u8 tmp;
822
	u16 tmp16, addr;
823

824
	/* demod I2C "address" */
825 826
	state->af9033_i2c_addr[0] = 0x38;
	state->af9033_i2c_addr[1] = 0x3a;
827
	state->af9033_config[0].adc_multiplier = AF9033_ADC_MULTIPLIER_2X;
828
	state->af9033_config[1].adc_multiplier = AF9033_ADC_MULTIPLIER_2X;
829 830
	state->af9033_config[0].ts_mode = AF9033_TS_MODE_USB;
	state->af9033_config[1].ts_mode = AF9033_TS_MODE_SERIAL;
831

832
	if (state->chip_type == 0x9135) {
833 834 835 836
		/* feed clock for integrated RF tuner */
		state->af9033_config[0].dyn0_clk = true;
		state->af9033_config[1].dyn0_clk = true;

837 838
		if (state->chip_version == 0x02) {
			state->af9033_config[0].tuner = AF9033_TUNER_IT9135_60;
839
			state->af9033_config[1].tuner = AF9033_TUNER_IT9135_60;
840
			tmp16 = 0x00461d; /* eeprom memory mapped location */
841 842
		} else {
			state->af9033_config[0].tuner = AF9033_TUNER_IT9135_38;
843
			state->af9033_config[1].tuner = AF9033_TUNER_IT9135_38;
844
			tmp16 = 0x00461b; /* eeprom memory mapped location */
845 846
		}

847
		/* check if eeprom exists */
848
		ret = af9035_rd_reg(d, tmp16, &tmp);
849 850 851
		if (ret < 0)
			goto err;

852
		if (tmp == 0x00) {
A
Antti Palosaari 已提交
853
			dev_dbg(&intf->dev, "no eeprom\n");
854 855
			goto skip_eeprom;
		}
856 857 858 859 860 861
	} else if (state->chip_type == 0x9306) {
		/*
		 * IT930x is an USB bridge, only single demod-single tuner
		 * configurations seen so far.
		 */
		return 0;
862 863
	}

864

865 866
	if (state->dual_mode) {
		/* read 2nd demodulator I2C address */
867 868 869
		ret = af9035_rd_reg(d,
				state->eeprom_addr + EEPROM_2ND_DEMOD_ADDR,
				&tmp);
870 871
		if (ret < 0)
			goto err;
872

873
		if (tmp)
874
			state->af9033_i2c_addr[1] = tmp;
875

A
Antti Palosaari 已提交
876
		dev_dbg(&intf->dev, "2nd demod I2C addr=%02x\n", tmp);
877 878
	}

879 880
	addr = state->eeprom_addr;

881
	for (i = 0; i < state->dual_mode + 1; i++) {
882
		/* tuner */
883
		ret = af9035_rd_reg(d, addr + EEPROM_1_TUNER_ID, &tmp);
884 885 886
		if (ret < 0)
			goto err;

A
Antti Palosaari 已提交
887
		dev_dbg(&intf->dev, "[%d]tuner=%02x\n", i, tmp);
888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911

		/* tuner sanity check */
		if (state->chip_type == 0x9135) {
			if (state->chip_version == 0x02) {
				/* IT9135 BX (v2) */
				switch (tmp) {
				case AF9033_TUNER_IT9135_60:
				case AF9033_TUNER_IT9135_61:
				case AF9033_TUNER_IT9135_62:
					state->af9033_config[i].tuner = tmp;
					break;
				}
			} else {
				/* IT9135 AX (v1) */
				switch (tmp) {
				case AF9033_TUNER_IT9135_38:
				case AF9033_TUNER_IT9135_51:
				case AF9033_TUNER_IT9135_52:
					state->af9033_config[i].tuner = tmp;
					break;
				}
			}
		} else {
			/* AF9035 */
912
			state->af9033_config[i].tuner = tmp;
913
		}
914

915
		if (state->af9033_config[i].tuner != tmp) {
A
Antti Palosaari 已提交
916 917
			dev_info(&intf->dev, "[%d] overriding tuner from %02x to %02x\n",
				 i, tmp, state->af9033_config[i].tuner);
918
		}
919 920

		switch (state->af9033_config[i].tuner) {
921
		case AF9033_TUNER_TUA9001:
922
		case AF9033_TUNER_FC0011:
923
		case AF9033_TUNER_MXL5007T:
924
		case AF9033_TUNER_TDA18218:
925
		case AF9033_TUNER_FC2580:
926
		case AF9033_TUNER_FC0012:
927
			state->af9033_config[i].spec_inv = 1;
928
			break;
929 930 931 932 933 934 935
		case AF9033_TUNER_IT9135_38:
		case AF9033_TUNER_IT9135_51:
		case AF9033_TUNER_IT9135_52:
		case AF9033_TUNER_IT9135_60:
		case AF9033_TUNER_IT9135_61:
		case AF9033_TUNER_IT9135_62:
			break;
936
		default:
A
Antti Palosaari 已提交
937 938
			dev_warn(&intf->dev, "tuner id=%02x not supported, please report!",
				 tmp);
939
		}
940

941 942
		/* disable dual mode if driver does not support it */
		if (i == 1)
943
			switch (state->af9033_config[i].tuner) {
944
			case AF9033_TUNER_FC0012:
945 946 947 948 949 950
			case AF9033_TUNER_IT9135_38:
			case AF9033_TUNER_IT9135_51:
			case AF9033_TUNER_IT9135_52:
			case AF9033_TUNER_IT9135_60:
			case AF9033_TUNER_IT9135_61:
			case AF9033_TUNER_IT9135_62:
951
			case AF9033_TUNER_MXL5007T:
952
				break;
953 954
			default:
				state->dual_mode = false;
A
Antti Palosaari 已提交
955
				dev_info(&intf->dev, "driver does not support 2nd tuner and will disable it");
956 957
		}

958
		/* tuner IF frequency */
959
		ret = af9035_rd_reg(d, addr + EEPROM_1_IF_L, &tmp);
960 961 962 963 964
		if (ret < 0)
			goto err;

		tmp16 = tmp;

965
		ret = af9035_rd_reg(d, addr + EEPROM_1_IF_H, &tmp);
966 967 968 969 970
		if (ret < 0)
			goto err;

		tmp16 |= tmp << 8;

A
Antti Palosaari 已提交
971
		dev_dbg(&intf->dev, "[%d]IF=%d\n", i, tmp16);
972

973
		addr += 0x10; /* shift for the 2nd tuner params */
974 975
	}

976
skip_eeprom:
977 978 979 980 981 982 983
	/* get demod clock */
	ret = af9035_rd_reg(d, 0x00d800, &tmp);
	if (ret < 0)
		goto err;

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

984 985 986 987 988
	for (i = 0; i < ARRAY_SIZE(state->af9033_config); i++) {
		if (state->chip_type == 0x9135)
			state->af9033_config[i].clock = clock_lut_it9135[tmp];
		else
			state->af9033_config[i].clock = clock_lut_af9035[tmp];
989 990
	}

991 992 993 994 995 996 997 998
	state->no_read = false;
	/* Some MXL5007T devices cannot properly handle tuner I2C read ops. */
	if (state->af9033_config[0].tuner == AF9033_TUNER_MXL5007T &&
		le16_to_cpu(d->udev->descriptor.idVendor) == USB_VID_AVERMEDIA)

		switch (le16_to_cpu(d->udev->descriptor.idProduct)) {
		case USB_PID_AVERMEDIA_A867:
		case USB_PID_AVERMEDIA_TWINSTAR:
A
Antti Palosaari 已提交
999 1000
			dev_info(&intf->dev,
				 "Device may have issues with I2C read operations. Enabling fix.\n");
1001 1002 1003 1004
			state->no_read = true;
			break;
		}

1005 1006 1007
	return 0;

err:
A
Antti Palosaari 已提交
1008
	dev_dbg(&intf->dev, "failed=%d\n", ret);
1009 1010 1011 1012

	return ret;
}

1013 1014 1015
static int af9035_tua9001_tuner_callback(struct dvb_usb_device *d,
		int cmd, int arg)
{
A
Antti Palosaari 已提交
1016
	struct usb_interface *intf = d->intf;
1017 1018 1019
	int ret;
	u8 val;

A
Antti Palosaari 已提交
1020
	dev_dbg(&intf->dev, "cmd=%d arg=%d\n", cmd, arg);
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

	/*
	 * CEN     always enabled by hardware wiring
	 * RESETN  GPIOT3
	 * RXEN    GPIOT2
	 */

	switch (cmd) {
	case TUA9001_CMD_RESETN:
		if (arg)
			val = 0x00;
		else
			val = 0x01;

		ret = af9035_wr_reg_mask(d, 0x00d8e7, val, 0x01);
		if (ret < 0)
			goto err;
		break;
	case TUA9001_CMD_RXEN:
		if (arg)
			val = 0x01;
		else
			val = 0x00;

		ret = af9035_wr_reg_mask(d, 0x00d8eb, val, 0x01);
		if (ret < 0)
			goto err;
		break;
	}

	return 0;

err:
A
Antti Palosaari 已提交
1054
	dev_dbg(&intf->dev, "failed=%d\n", ret);
1055 1056 1057 1058 1059

	return ret;
}


1060
static int af9035_fc0011_tuner_callback(struct dvb_usb_device *d,
1061
		int cmd, int arg)
1062
{
A
Antti Palosaari 已提交
1063
	struct usb_interface *intf = d->intf;
1064
	int ret;
1065 1066 1067 1068

	switch (cmd) {
	case FC0011_FE_CALLBACK_POWER:
		/* Tuner enable */
1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080
		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;

1081
		/* LED */
1082 1083 1084 1085 1086 1087 1088 1089
		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;

1090
		usleep_range(10000, 50000);
1091 1092
		break;
	case FC0011_FE_CALLBACK_RESET:
1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104
		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;

1105
		usleep_range(10000, 20000);
1106 1107 1108 1109 1110

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

1111
		usleep_range(10000, 20000);
1112 1113
		break;
	default:
1114 1115
		ret = -EINVAL;
		goto err;
1116 1117 1118
	}

	return 0;
1119 1120

err:
A
Antti Palosaari 已提交
1121
	dev_dbg(&intf->dev, "failed=%d\n", ret);
1122 1123

	return ret;
1124 1125 1126 1127
}

static int af9035_tuner_callback(struct dvb_usb_device *d, int cmd, int arg)
{
1128
	struct state *state = d_to_priv(d);
1129 1130

	switch (state->af9033_config[0].tuner) {
1131 1132
	case AF9033_TUNER_FC0011:
		return af9035_fc0011_tuner_callback(d, cmd, arg);
1133 1134
	case AF9033_TUNER_TUA9001:
		return af9035_tua9001_tuner_callback(d, cmd, arg);
1135 1136 1137 1138
	default:
		break;
	}

1139
	return 0;
1140 1141 1142 1143 1144 1145 1146
}

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);
A
Antti Palosaari 已提交
1147
	struct usb_interface *intf = d->intf;
1148

A
Antti Palosaari 已提交
1149 1150
	dev_dbg(&intf->dev, "component=%d cmd=%d arg=%d\n",
		component, cmd, arg);
1151

1152 1153 1154 1155 1156 1157 1158
	switch (component) {
	case DVB_FRONTEND_COMPONENT_TUNER:
		return af9035_tuner_callback(d, cmd, arg);
	default:
		break;
	}

1159
	return 0;
1160 1161
}

1162 1163 1164
static int af9035_get_adapter_count(struct dvb_usb_device *d)
{
	struct state *state = d_to_priv(d);
1165

1166
	return state->dual_mode + 1;
1167 1168
}

1169 1170
static int af9035_frontend_attach(struct dvb_usb_adapter *adap)
{
1171 1172
	struct state *state = adap_to_priv(adap);
	struct dvb_usb_device *d = adap_to_d(adap);
A
Antti Palosaari 已提交
1173
	struct usb_interface *intf = d->intf;
1174
	int ret;
1175

A
Antti Palosaari 已提交
1176
	dev_dbg(&intf->dev, "adap->id=%d\n", adap->id);
1177

1178 1179
	if (!state->af9033_config[adap->id].tuner) {
		/* unsupported tuner */
1180 1181 1182 1183
		ret = -ENODEV;
		goto err;
	}

1184 1185
	state->af9033_config[adap->id].fe = &adap->fe[0];
	state->af9033_config[adap->id].ops = &state->ops;
1186
	ret = af9035_add_i2c_dev(d, "af9033", state->af9033_i2c_addr[adap->id],
1187
			&state->af9033_config[adap->id], &d->i2c_adap);
1188 1189 1190
	if (ret)
		goto err;

1191
	if (adap->fe[0] == NULL) {
1192 1193 1194
		ret = -ENODEV;
		goto err;
	}
1195 1196

	/* disable I2C-gate */
1197 1198
	adap->fe[0]->ops.i2c_gate_ctrl = NULL;
	adap->fe[0]->callback = af9035_frontend_callback;
1199 1200 1201 1202

	return 0;

err:
A
Antti Palosaari 已提交
1203
	dev_dbg(&intf->dev, "failed=%d\n", ret);
1204 1205 1206 1207

	return ret;
}

1208 1209 1210 1211
static int it930x_frontend_attach(struct dvb_usb_adapter *adap)
{
	struct state *state = adap_to_priv(adap);
	struct dvb_usb_device *d = adap_to_d(adap);
A
Antti Palosaari 已提交
1212
	struct usb_interface *intf = d->intf;
1213 1214 1215 1216
	int ret;
	struct si2168_config si2168_config;
	struct i2c_adapter *adapter;

A
Antti Palosaari 已提交
1217
	dev_dbg(&intf->dev, "adap->id=%d\n", adap->id);
1218

1219
	memset(&si2168_config, 0, sizeof(si2168_config));
1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239
	si2168_config.i2c_adapter = &adapter;
	si2168_config.fe = &adap->fe[0];
	si2168_config.ts_mode = SI2168_TS_SERIAL;

	state->af9033_config[adap->id].fe = &adap->fe[0];
	state->af9033_config[adap->id].ops = &state->ops;
	ret = af9035_add_i2c_dev(d, "si2168", 0x67, &si2168_config,
				&d->i2c_adap);
	if (ret)
		goto err;

	if (adap->fe[0] == NULL) {
		ret = -ENODEV;
		goto err;
	}
	state->i2c_adapter_demod = adapter;

	return 0;

err:
A
Antti Palosaari 已提交
1240
	dev_dbg(&intf->dev, "failed=%d\n", ret);
1241 1242 1243 1244

	return ret;
}

1245 1246 1247 1248
static int af9035_frontend_detach(struct dvb_usb_adapter *adap)
{
	struct state *state = adap_to_priv(adap);
	struct dvb_usb_device *d = adap_to_d(adap);
A
Antti Palosaari 已提交
1249
	struct usb_interface *intf = d->intf;
1250 1251
	int demod2;

A
Antti Palosaari 已提交
1252
	dev_dbg(&intf->dev, "adap->id=%d\n", adap->id);
1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282

	/*
	 * For dual tuner devices we have to resolve 2nd demod client, as there
	 * is two different kind of tuner drivers; one is using I2C binding
	 * and the other is using DVB attach/detach binding.
	 */
	switch (state->af9033_config[adap->id].tuner) {
	case AF9033_TUNER_IT9135_38:
	case AF9033_TUNER_IT9135_51:
	case AF9033_TUNER_IT9135_52:
	case AF9033_TUNER_IT9135_60:
	case AF9033_TUNER_IT9135_61:
	case AF9033_TUNER_IT9135_62:
		demod2 = 2;
		break;
	default:
		demod2 = 1;
	}

	if (adap->id == 1) {
		if (state->i2c_client[demod2])
			af9035_del_i2c_dev(d);
	} else if (adap->id == 0) {
		if (state->i2c_client[0])
			af9035_del_i2c_dev(d);
	}

	return 0;
}

1283 1284 1285 1286
static const struct fc0011_config af9035_fc0011_config = {
	.i2c_address = 0x60,
};

1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302
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,
	}, {
		.xtal_freq_hz = MxL_XTAL_24_MHZ,
		.if_freq_hz = MxL_IF_4_57_MHZ,
		.invert_if = 0,
		.loop_thru_enable = 1,
		.clk_out_enable = 1,
		.clk_out_amp = MxL_CLKOUT_AMP_0_94V,
	}
1303 1304
};

1305 1306 1307 1308 1309
static struct tda18218_config af9035_tda18218_config = {
	.i2c_address = 0x60,
	.i2c_wr_max = 21,
};

1310 1311 1312 1313
static const struct fc0012_config af9035_fc0012_config[] = {
	{
		.i2c_address = 0x63,
		.xtal_freq = FC_XTAL_36_MHZ,
1314
		.dual_master = true,
1315 1316 1317 1318 1319
		.loop_through = true,
		.clock_out = true,
	}, {
		.i2c_address = 0x63 | 0x80, /* I2C bus select hack */
		.xtal_freq = FC_XTAL_36_MHZ,
1320
		.dual_master = true,
1321
	}
1322 1323
};

1324 1325
static int af9035_tuner_attach(struct dvb_usb_adapter *adap)
{
1326 1327
	struct state *state = adap_to_priv(adap);
	struct dvb_usb_device *d = adap_to_d(adap);
A
Antti Palosaari 已提交
1328
	struct usb_interface *intf = d->intf;
1329 1330
	int ret;
	struct dvb_frontend *fe;
1331
	struct i2c_msg msg[1];
1332
	u8 tuner_addr;
1333

A
Antti Palosaari 已提交
1334
	dev_dbg(&intf->dev, "adap->id=%d\n", adap->id);
1335

1336 1337 1338 1339
	/*
	 * XXX: Hack used in that function: we abuse unused I2C address bit [7]
	 * to carry info about used I2C bus for dual tuner configuration.
	 */
1340

1341
	switch (state->af9033_config[adap->id].tuner) {
1342 1343 1344 1345 1346 1347 1348 1349 1350
	case AF9033_TUNER_TUA9001: {
		struct tua9001_platform_data tua9001_pdata = {
			.dvb_frontend = adap->fe[0],
		};

		/*
		 * AF9035 gpiot3 = TUA9001 RESETN
		 * AF9035 gpiot2 = TUA9001 RXEN
		 */
1351 1352

		/* configure gpiot2 and gpiot2 as output */
1353
		ret = af9035_wr_reg_mask(d, 0x00d8ec, 0x01, 0x01);
1354 1355 1356
		if (ret < 0)
			goto err;

1357
		ret = af9035_wr_reg_mask(d, 0x00d8ed, 0x01, 0x01);
1358 1359 1360
		if (ret < 0)
			goto err;

1361
		ret = af9035_wr_reg_mask(d, 0x00d8e8, 0x01, 0x01);
1362 1363 1364
		if (ret < 0)
			goto err;

1365
		ret = af9035_wr_reg_mask(d, 0x00d8e9, 0x01, 0x01);
1366 1367 1368 1369
		if (ret < 0)
			goto err;

		/* attach tuner */
1370 1371 1372 1373 1374 1375
		ret = af9035_add_i2c_dev(d, "tua9001", 0x60, &tua9001_pdata,
					 &d->i2c_adap);
		if (ret)
			goto err;

		fe = adap->fe[0];
1376
		break;
1377
	}
1378
	case AF9033_TUNER_FC0011:
1379 1380
		fe = dvb_attach(fc0011_attach, adap->fe[0],
				&d->i2c_adap, &af9035_fc0011_config);
1381
		break;
1382
	case AF9033_TUNER_MXL5007T:
1383 1384 1385 1386
		if (adap->id == 0) {
			ret = af9035_wr_reg(d, 0x00d8e0, 1);
			if (ret < 0)
				goto err;
1387

1388 1389 1390
			ret = af9035_wr_reg(d, 0x00d8e1, 1);
			if (ret < 0)
				goto err;
1391

1392 1393 1394
			ret = af9035_wr_reg(d, 0x00d8df, 0);
			if (ret < 0)
				goto err;
1395

1396
			msleep(30);
1397

1398 1399 1400
			ret = af9035_wr_reg(d, 0x00d8df, 1);
			if (ret < 0)
				goto err;
1401

1402
			msleep(300);
1403

1404 1405 1406
			ret = af9035_wr_reg(d, 0x00d8c0, 1);
			if (ret < 0)
				goto err;
1407

1408 1409 1410
			ret = af9035_wr_reg(d, 0x00d8c1, 1);
			if (ret < 0)
				goto err;
1411

1412 1413 1414
			ret = af9035_wr_reg(d, 0x00d8bf, 0);
			if (ret < 0)
				goto err;
1415

1416 1417 1418
			ret = af9035_wr_reg(d, 0x00d8b4, 1);
			if (ret < 0)
				goto err;
1419

1420 1421 1422
			ret = af9035_wr_reg(d, 0x00d8b5, 1);
			if (ret < 0)
				goto err;
1423

1424 1425 1426
			ret = af9035_wr_reg(d, 0x00d8b3, 1);
			if (ret < 0)
				goto err;
1427 1428 1429 1430

			tuner_addr = 0x60;
		} else {
			tuner_addr = 0x60 | 0x80; /* I2C bus hack */
1431
		}
1432 1433

		/* attach tuner */
1434 1435
		fe = dvb_attach(mxl5007t_attach, adap->fe[0], &d->i2c_adap,
				tuner_addr, &af9035_mxl5007t_config[adap->id]);
1436
		break;
1437 1438
	case AF9033_TUNER_TDA18218:
		/* attach tuner */
1439 1440
		fe = dvb_attach(tda18218_attach, adap->fe[0],
				&d->i2c_adap, &af9035_tda18218_config);
1441
		break;
1442 1443 1444 1445 1446
	case AF9033_TUNER_FC2580: {
		struct fc2580_platform_data fc2580_pdata = {
			.dvb_frontend = adap->fe[0],
		};

1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461
		/* Tuner enable using gpiot2_o, gpiot2_en and gpiot2_on  */
		ret = af9035_wr_reg_mask(d, 0xd8eb, 0x01, 0x01);
		if (ret < 0)
			goto err;

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

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

		usleep_range(10000, 50000);
		/* attach tuner */
1462 1463 1464 1465 1466 1467
		ret = af9035_add_i2c_dev(d, "fc2580", 0x56, &fc2580_pdata,
					 &d->i2c_adap);
		if (ret)
			goto err;

		fe = adap->fe[0];
1468
		break;
1469
	}
1470 1471 1472 1473 1474 1475 1476
	case AF9033_TUNER_FC0012:
		/*
		 * AF9035 gpiot2 = FC0012 enable
		 * XXX: there seems to be something on gpioh8 too, but on my
		 * my test I didn't find any difference.
		 */

1477 1478 1479 1480 1481
		if (adap->id == 0) {
			/* configure gpiot2 as output and high */
			ret = af9035_wr_reg_mask(d, 0xd8eb, 0x01, 0x01);
			if (ret < 0)
				goto err;
1482

1483 1484 1485
			ret = af9035_wr_reg_mask(d, 0xd8ec, 0x01, 0x01);
			if (ret < 0)
				goto err;
1486

1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503
			ret = af9035_wr_reg_mask(d, 0xd8ed, 0x01, 0x01);
			if (ret < 0)
				goto err;
		} else {
			/*
			 * FIXME: That belongs for the FC0012 driver.
			 * Write 02 to FC0012 master tuner register 0d directly
			 * in order to make slave tuner working.
			 */
			msg[0].addr = 0x63;
			msg[0].flags = 0;
			msg[0].len = 2;
			msg[0].buf = "\x0d\x02";
			ret = i2c_transfer(&d->i2c_adap, msg, 1);
			if (ret < 0)
				goto err;
		}
1504 1505 1506

		usleep_range(10000, 50000);

1507
		fe = dvb_attach(fc0012_attach, adap->fe[0], &d->i2c_adap,
1508
				&af9035_fc0012_config[adap->id]);
1509
		break;
1510
	case AF9033_TUNER_IT9135_38:
1511 1512
	case AF9033_TUNER_IT9135_51:
	case AF9033_TUNER_IT9135_52:
1513 1514 1515 1516 1517 1518
	{
		struct it913x_config it913x_config = {
			.fe = adap->fe[0],
			.chip_ver = 1,
		};

1519 1520 1521 1522 1523 1524 1525
		if (state->dual_mode) {
			if (adap->id == 0)
				it913x_config.role = IT913X_ROLE_DUAL_MASTER;
			else
				it913x_config.role = IT913X_ROLE_DUAL_SLAVE;
		}

1526
		ret = af9035_add_i2c_dev(d, "it913x",
1527
				state->af9033_i2c_addr[adap->id] >> 1,
1528
				&it913x_config, &d->i2c_adap);
1529 1530 1531 1532 1533 1534
		if (ret)
			goto err;

		fe = adap->fe[0];
		break;
	}
1535 1536 1537
	case AF9033_TUNER_IT9135_60:
	case AF9033_TUNER_IT9135_61:
	case AF9033_TUNER_IT9135_62:
1538 1539 1540 1541 1542 1543
	{
		struct it913x_config it913x_config = {
			.fe = adap->fe[0],
			.chip_ver = 2,
		};

1544 1545 1546 1547 1548 1549 1550
		if (state->dual_mode) {
			if (adap->id == 0)
				it913x_config.role = IT913X_ROLE_DUAL_MASTER;
			else
				it913x_config.role = IT913X_ROLE_DUAL_SLAVE;
		}

1551
		ret = af9035_add_i2c_dev(d, "it913x",
1552
				state->af9033_i2c_addr[adap->id] >> 1,
1553
				&it913x_config, &d->i2c_adap);
1554 1555 1556 1557
		if (ret)
			goto err;

		fe = adap->fe[0];
1558
		break;
1559
	}
1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571
	default:
		fe = NULL;
	}

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

	return 0;

err:
A
Antti Palosaari 已提交
1572
	dev_dbg(&intf->dev, "failed=%d\n", ret);
1573 1574 1575 1576

	return ret;
}

1577 1578 1579 1580
static int it930x_tuner_attach(struct dvb_usb_adapter *adap)
{
	struct state *state = adap_to_priv(adap);
	struct dvb_usb_device *d = adap_to_d(adap);
A
Antti Palosaari 已提交
1581
	struct usb_interface *intf = d->intf;
1582 1583 1584
	int ret;
	struct si2157_config si2157_config;

A
Antti Palosaari 已提交
1585
	dev_dbg(&intf->dev, "adap->id=%d\n", adap->id);
1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630

	/* I2C master bus 2 clock speed 300k */
	ret = af9035_wr_reg(d, 0x00f6a7, 0x07);
	if (ret < 0)
		goto err;

	/* I2C master bus 1,3 clock speed 300k */
	ret = af9035_wr_reg(d, 0x00f103, 0x07);
	if (ret < 0)
		goto err;

	/* set gpio11 low */
	ret = af9035_wr_reg_mask(d, 0xd8d4, 0x01, 0x01);
	if (ret < 0)
		goto err;

	ret = af9035_wr_reg_mask(d, 0xd8d5, 0x01, 0x01);
	if (ret < 0)
		goto err;

	ret = af9035_wr_reg_mask(d, 0xd8d3, 0x01, 0x01);
	if (ret < 0)
		goto err;

	/* Tuner enable using gpiot2_en, gpiot2_on and gpiot2_o (reset) */
	ret = af9035_wr_reg_mask(d, 0xd8b8, 0x01, 0x01);
	if (ret < 0)
		goto err;

	ret = af9035_wr_reg_mask(d, 0xd8b9, 0x01, 0x01);
	if (ret < 0)
		goto err;

	ret = af9035_wr_reg_mask(d, 0xd8b7, 0x00, 0x01);
	if (ret < 0)
		goto err;

	msleep(200);

	ret = af9035_wr_reg_mask(d, 0xd8b7, 0x01, 0x01);
	if (ret < 0)
		goto err;

	memset(&si2157_config, 0, sizeof(si2157_config));
	si2157_config.fe = adap->fe[0];
1631
	si2157_config.if_port = 1;
1632 1633 1634 1635 1636 1637 1638 1639 1640
	ret = af9035_add_i2c_dev(d, "si2157", 0x63,
			&si2157_config, state->i2c_adapter_demod);

	if (ret)
		goto err;

	return 0;

err:
A
Antti Palosaari 已提交
1641
	dev_dbg(&intf->dev, "failed=%d\n", ret);
1642 1643 1644 1645 1646 1647 1648 1649 1650

	return ret;
}


static int it930x_tuner_detach(struct dvb_usb_adapter *adap)
{
	struct state *state = adap_to_priv(adap);
	struct dvb_usb_device *d = adap_to_d(adap);
A
Antti Palosaari 已提交
1651
	struct usb_interface *intf = d->intf;
1652

A
Antti Palosaari 已提交
1653
	dev_dbg(&intf->dev, "adap->id=%d\n", adap->id);
1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666

	if (adap->id == 1) {
		if (state->i2c_client[3])
			af9035_del_i2c_dev(d);
	} else if (adap->id == 0) {
		if (state->i2c_client[1])
			af9035_del_i2c_dev(d);
	}

	return 0;
}


1667 1668 1669 1670
static int af9035_tuner_detach(struct dvb_usb_adapter *adap)
{
	struct state *state = adap_to_priv(adap);
	struct dvb_usb_device *d = adap_to_d(adap);
A
Antti Palosaari 已提交
1671
	struct usb_interface *intf = d->intf;
1672

A
Antti Palosaari 已提交
1673
	dev_dbg(&intf->dev, "adap->id=%d\n", adap->id);
1674 1675

	switch (state->af9033_config[adap->id].tuner) {
1676
	case AF9033_TUNER_TUA9001:
1677
	case AF9033_TUNER_FC2580:
1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695
	case AF9033_TUNER_IT9135_38:
	case AF9033_TUNER_IT9135_51:
	case AF9033_TUNER_IT9135_52:
	case AF9033_TUNER_IT9135_60:
	case AF9033_TUNER_IT9135_61:
	case AF9033_TUNER_IT9135_62:
		if (adap->id == 1) {
			if (state->i2c_client[3])
				af9035_del_i2c_dev(d);
		} else if (adap->id == 0) {
			if (state->i2c_client[1])
				af9035_del_i2c_dev(d);
		}
	}

	return 0;
}

1696 1697 1698
static int af9035_init(struct dvb_usb_device *d)
{
	struct state *state = d_to_priv(d);
A
Antti Palosaari 已提交
1699
	struct usb_interface *intf = d->intf;
1700
	int ret, i;
1701 1702
	u16 frame_size = (d->udev->speed == USB_SPEED_FULL ? 5 : 87) * 188 / 4;
	u8 packet_size = (d->udev->speed == USB_SPEED_FULL ? 64 : 512) / 4;
1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717
	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 },
1718 1719
		{ 0x80f9a3, state->dual_mode, 0x01 },
		{ 0x80f9cd, state->dual_mode, 0x01 },
1720 1721 1722
		{ 0x80f99d, 0x00, 0x01 },
		{ 0x80f9a4, 0x00, 0x01 },
	};
1723

A
Antti Palosaari 已提交
1724 1725
	dev_dbg(&intf->dev, "USB speed=%d frame_size=%04x packet_size=%02x\n",
		d->udev->speed, frame_size, packet_size);
1726

1727 1728 1729 1730 1731 1732 1733
	/* 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;
	}
1734

1735
	return 0;
1736

1737
err:
A
Antti Palosaari 已提交
1738
	dev_dbg(&intf->dev, "failed=%d\n", ret);
1739

1740 1741
	return ret;
}
1742

1743 1744 1745
static int it930x_init(struct dvb_usb_device *d)
{
	struct state *state = d_to_priv(d);
A
Antti Palosaari 已提交
1746
	struct usb_interface *intf = d->intf;
1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805
	int ret, i;
	u16 frame_size = (d->udev->speed == USB_SPEED_FULL ? 5 : 816) * 188 / 4;
	u8 packet_size = (d->udev->speed == USB_SPEED_FULL ? 64 : 512) / 4;
	struct reg_val_mask tab[] = {
		{ 0x00da1a, 0x00, 0x01 }, /* ignore_sync_byte */
		{ 0x00f41f, 0x04, 0x04 }, /* dvbt_inten */
		{ 0x00da10, 0x00, 0x01 }, /* mpeg_full_speed */
		{ 0x00f41a, 0x01, 0x01 }, /* dvbt_en */
		{ 0x00da1d, 0x01, 0x01 }, /* mp2_sw_rst, reset EP4 */
		{ 0x00dd11, 0x00, 0x20 }, /* ep4_tx_en, disable EP4 */
		{ 0x00dd13, 0x00, 0x20 }, /* ep4_tx_nak, disable EP4 NAK */
		{ 0x00dd11, 0x20, 0x20 }, /* ep4_tx_en, enable EP4 */
		{ 0x00dd11, 0x00, 0x40 }, /* ep5_tx_en, disable EP5 */
		{ 0x00dd13, 0x00, 0x40 }, /* ep5_tx_nak, disable EP5 NAK */
		{ 0x00dd11, state->dual_mode << 6, 0x40 }, /* enable EP5 */
		{ 0x00dd88, (frame_size >> 0) & 0xff, 0xff},
		{ 0x00dd89, (frame_size >> 8) & 0xff, 0xff},
		{ 0x00dd0c, packet_size, 0xff},
		{ 0x00dd8a, (frame_size >> 0) & 0xff, 0xff},
		{ 0x00dd8b, (frame_size >> 8) & 0xff, 0xff},
		{ 0x00dd0d, packet_size, 0xff },
		{ 0x00da1d, 0x00, 0x01 }, /* mp2_sw_rst, disable */
		{ 0x00d833, 0x01, 0xff }, /* slew rate ctrl: slew rate boosts */
		{ 0x00d830, 0x00, 0xff }, /* Bit 0 of output driving control */
		{ 0x00d831, 0x01, 0xff }, /* Bit 1 of output driving control */
		{ 0x00d832, 0x00, 0xff }, /* Bit 2 of output driving control */

		/* suspend gpio1 for TS-C */
		{ 0x00d8b0, 0x01, 0xff }, /* gpio1 */
		{ 0x00d8b1, 0x01, 0xff }, /* gpio1 */
		{ 0x00d8af, 0x00, 0xff }, /* gpio1 */

		/* suspend gpio7 for TS-D */
		{ 0x00d8c4, 0x01, 0xff }, /* gpio7 */
		{ 0x00d8c5, 0x01, 0xff }, /* gpio7 */
		{ 0x00d8c3, 0x00, 0xff }, /* gpio7 */

		/* suspend gpio13 for TS-B */
		{ 0x00d8dc, 0x01, 0xff }, /* gpio13 */
		{ 0x00d8dd, 0x01, 0xff }, /* gpio13 */
		{ 0x00d8db, 0x00, 0xff }, /* gpio13 */

		/* suspend gpio14 for TS-E */
		{ 0x00d8e4, 0x01, 0xff }, /* gpio14 */
		{ 0x00d8e5, 0x01, 0xff }, /* gpio14 */
		{ 0x00d8e3, 0x00, 0xff }, /* gpio14 */

		/* suspend gpio15 for TS-A */
		{ 0x00d8e8, 0x01, 0xff }, /* gpio15 */
		{ 0x00d8e9, 0x01, 0xff }, /* gpio15 */
		{ 0x00d8e7, 0x00, 0xff }, /* gpio15 */

		{ 0x00da58, 0x00, 0x01 }, /* ts_in_src, serial */
		{ 0x00da73, 0x01, 0xff }, /* ts0_aggre_mode */
		{ 0x00da78, 0x47, 0xff }, /* ts0_sync_byte */
		{ 0x00da4c, 0x01, 0xff }, /* ts0_en */
		{ 0x00da5a, 0x1f, 0xff }, /* ts_fail_ignore */
	};

A
Antti Palosaari 已提交
1806 1807
	dev_dbg(&intf->dev, "USB speed=%d frame_size=%04x packet_size=%02x\n",
		d->udev->speed, frame_size, packet_size);
1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819

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

	return 0;
err:
A
Antti Palosaari 已提交
1820
	dev_dbg(&intf->dev, "failed=%d\n", ret);
1821 1822 1823 1824 1825

	return ret;
}


1826
#if IS_ENABLED(CONFIG_RC_CORE)
1827 1828
static int af9035_rc_query(struct dvb_usb_device *d)
{
A
Antti Palosaari 已提交
1829
	struct usb_interface *intf = d->intf;
1830
	int ret;
1831
	enum rc_type proto;
1832 1833 1834
	u32 key;
	u8 buf[4];
	struct usb_req req = { CMD_IR_GET, 0, 0, NULL, 4, buf };
1835

1836
	ret = af9035_ctrl_msg(d, &req);
1837 1838 1839
	if (ret == 1)
		return 0;
	else if (ret < 0)
1840
		goto err;
1841

1842 1843 1844
	if ((buf[2] + buf[3]) == 0xff) {
		if ((buf[0] + buf[1]) == 0xff) {
			/* NEC standard 16bit */
1845
			key = RC_SCANCODE_NEC(buf[0], buf[2]);
1846
			proto = RC_TYPE_NEC;
1847
		} else {
1848
			/* NEC extended 24bit */
1849
			key = RC_SCANCODE_NECX(buf[0] << 8 | buf[1], buf[2]);
1850
			proto = RC_TYPE_NECX;
1851
		}
1852
	} else {
1853
		/* NEC full code 32bit */
1854 1855
		key = RC_SCANCODE_NEC32(buf[0] << 24 | buf[1] << 16 |
					buf[2] << 8  | buf[3]);
1856
		proto = RC_TYPE_NEC32;
1857 1858
	}

A
Antti Palosaari 已提交
1859
	dev_dbg(&intf->dev, "%*ph\n", 4, buf);
1860

1861
	rc_keydown(d->rc_dev, proto, key, 0);
1862

1863
	return 0;
1864 1865

err:
A
Antti Palosaari 已提交
1866
	dev_dbg(&intf->dev, "failed=%d\n", ret);
1867 1868

	return ret;
1869
}
1870

1871 1872
static int af9035_get_rc_config(struct dvb_usb_device *d, struct dvb_usb_rc *rc)
{
1873
	struct state *state = d_to_priv(d);
A
Antti Palosaari 已提交
1874
	struct usb_interface *intf = d->intf;
1875 1876
	int ret;
	u8 tmp;
1877

1878
	ret = af9035_rd_reg(d, state->eeprom_addr + EEPROM_IR_MODE, &tmp);
1879 1880 1881
	if (ret < 0)
		goto err;

A
Antti Palosaari 已提交
1882
	dev_dbg(&intf->dev, "ir_mode=%02x\n", tmp);
1883 1884 1885

	/* don't activate rc if in HID mode or if not available */
	if (tmp == 5) {
1886
		ret = af9035_rd_reg(d, state->eeprom_addr + EEPROM_IR_TYPE,
1887
				&tmp);
1888 1889
		if (ret < 0)
			goto err;
1890

A
Antti Palosaari 已提交
1891
		dev_dbg(&intf->dev, "ir_type=%02x\n", tmp);
1892 1893 1894 1895

		switch (tmp) {
		case 0: /* NEC */
		default:
1896 1897
			rc->allowed_protos = RC_BIT_NEC | RC_BIT_NECX |
								RC_BIT_NEC32;
1898 1899
			break;
		case 1: /* RC6 */
1900
			rc->allowed_protos = RC_BIT_RC6_MCE;
1901 1902 1903 1904 1905
			break;
		}

		rc->query = af9035_rc_query;
		rc->interval = 500;
1906 1907 1908 1909

		/* load empty to enable rc */
		if (!rc->map_name)
			rc->map_name = RC_MAP_EMPTY;
1910 1911 1912 1913 1914
	}

	return 0;

err:
A
Antti Palosaari 已提交
1915
	dev_dbg(&intf->dev, "failed=%d\n", ret);
1916 1917 1918

	return ret;
}
1919 1920 1921
#else
	#define af9035_get_rc_config NULL
#endif
1922

1923 1924 1925 1926
static int af9035_get_stream_config(struct dvb_frontend *fe, u8 *ts_type,
		struct usb_data_stream_properties *stream)
{
	struct dvb_usb_device *d = fe_to_d(fe);
A
Antti Palosaari 已提交
1927
	struct usb_interface *intf = d->intf;
1928

A
Antti Palosaari 已提交
1929
	dev_dbg(&intf->dev, "adap=%d\n", fe_to_adap(fe)->id);
1930 1931 1932 1933 1934 1935 1936 1937 1938

	if (d->udev->speed == USB_SPEED_FULL)
		stream->u.bulk.buffersize = 5 * 188;

	return 0;
}

static int af9035_pid_filter_ctrl(struct dvb_usb_adapter *adap, int onoff)
{
1939 1940 1941
	struct state *state = adap_to_priv(adap);

	return state->ops.pid_filter_ctrl(adap->fe[0], onoff);
1942 1943 1944 1945 1946
}

static int af9035_pid_filter(struct dvb_usb_adapter *adap, int index, u16 pid,
		int onoff)
{
1947 1948 1949
	struct state *state = adap_to_priv(adap);

	return state->ops.pid_filter(adap->fe[0], index, pid, onoff);
1950 1951
}

1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980
static int af9035_probe(struct usb_interface *intf,
		const struct usb_device_id *id)
{
	struct usb_device *udev = interface_to_usbdev(intf);
	char manufacturer[sizeof("Afatech")];

	memset(manufacturer, 0, sizeof(manufacturer));
	usb_string(udev, udev->descriptor.iManufacturer,
			manufacturer, sizeof(manufacturer));
	/*
	 * There is two devices having same ID but different chipset. One uses
	 * AF9015 and the other IT9135 chipset. Only difference seen on lsusb
	 * is iManufacturer string.
	 *
	 * idVendor           0x0ccd TerraTec Electronic GmbH
	 * idProduct          0x0099
	 * bcdDevice            2.00
	 * iManufacturer           1 Afatech
	 * iProduct                2 DVB-T 2
	 *
	 * idVendor           0x0ccd TerraTec Electronic GmbH
	 * idProduct          0x0099
	 * bcdDevice            2.00
	 * iManufacturer           1 ITE Technologies, Inc.
	 * iProduct                2 DVB-T TV Stick
	 */
	if ((le16_to_cpu(udev->descriptor.idVendor) == USB_VID_TERRATEC) &&
			(le16_to_cpu(udev->descriptor.idProduct) == 0x0099)) {
		if (!strcmp("Afatech", manufacturer)) {
A
Antti Palosaari 已提交
1981
			dev_dbg(&udev->dev, "rejecting device\n");
1982 1983 1984 1985 1986 1987 1988
			return -ENODEV;
		}
	}

	return dvb_usbv2_probe(intf, id);
}

1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005
/* 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,
	.download_firmware = af9035_download_firmware,

	.i2c_algo = &af9035_i2c_algo,
	.read_config = af9035_read_config,
	.frontend_attach = af9035_frontend_attach,
2006
	.frontend_detach = af9035_frontend_detach,
2007
	.tuner_attach = af9035_tuner_attach,
2008
	.tuner_detach = af9035_tuner_detach,
2009 2010
	.init = af9035_init,
	.get_rc_config = af9035_get_rc_config,
2011
	.get_stream_config = af9035_get_stream_config,
2012

2013
	.get_adapter_count = af9035_get_adapter_count,
2014 2015
	.adapter = {
		{
2016 2017 2018 2019 2020 2021 2022
			.caps = DVB_USB_ADAP_HAS_PID_FILTER |
				DVB_USB_ADAP_PID_FILTER_CAN_BE_TURNED_OFF,

			.pid_filter_count = 32,
			.pid_filter_ctrl = af9035_pid_filter_ctrl,
			.pid_filter = af9035_pid_filter,

2023 2024
			.stream = DVB_USB_STREAM_BULK(0x84, 6, 87 * 188),
		}, {
2025 2026 2027 2028 2029 2030 2031
			.caps = DVB_USB_ADAP_HAS_PID_FILTER |
				DVB_USB_ADAP_PID_FILTER_CAN_BE_TURNED_OFF,

			.pid_filter_count = 32,
			.pid_filter_ctrl = af9035_pid_filter_ctrl,
			.pid_filter = af9035_pid_filter,

2032 2033 2034 2035 2036
			.stream = DVB_USB_STREAM_BULK(0x85, 6, 87 * 188),
		},
	},
};

2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067
static const struct dvb_usb_device_properties it930x_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,
	.download_firmware = af9035_download_firmware,

	.i2c_algo = &af9035_i2c_algo,
	.read_config = af9035_read_config,
	.frontend_attach = it930x_frontend_attach,
	.frontend_detach = af9035_frontend_detach,
	.tuner_attach = it930x_tuner_attach,
	.tuner_detach = it930x_tuner_detach,
	.init = it930x_init,
	.get_stream_config = af9035_get_stream_config,

	.get_adapter_count = af9035_get_adapter_count,
	.adapter = {
		{
			.stream = DVB_USB_STREAM_BULK(0x84, 4, 816 * 188),
		}, {
			.stream = DVB_USB_STREAM_BULK(0x85, 4, 816 * 188),
		},
	},
};

2068
static const struct usb_device_id af9035_id_table[] = {
2069
	/* AF9035 devices */
2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091
	{ 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) },
2092 2093
	{ DVB_USB_DEVICE(USB_VID_ASUS, USB_PID_ASUS_U3100MINI_PLUS,
		&af9035_props, "Asus U3100Mini Plus", NULL) },
2094
	{ DVB_USB_DEVICE(USB_VID_TERRATEC, 0x00aa,
2095
		&af9035_props, "TerraTec Cinergy T Stick (rev. 2)", NULL) },
2096 2097
	{ DVB_USB_DEVICE(USB_VID_AVERMEDIA, 0x0337,
		&af9035_props, "AVerMedia HD Volar (A867)", NULL) },
2098

2099
	/* IT9135 devices */
2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113
	{ DVB_USB_DEVICE(USB_VID_ITETECH, USB_PID_ITETECH_IT9135,
		&af9035_props, "ITE 9135 Generic", RC_MAP_IT913X_V1) },
	{ DVB_USB_DEVICE(USB_VID_ITETECH, USB_PID_ITETECH_IT9135_9005,
		&af9035_props, "ITE 9135(9005) Generic", RC_MAP_IT913X_V2) },
	{ DVB_USB_DEVICE(USB_VID_ITETECH, USB_PID_ITETECH_IT9135_9006,
		&af9035_props, "ITE 9135(9006) Generic", RC_MAP_IT913X_V1) },
	{ DVB_USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_A835B_1835,
		&af9035_props, "Avermedia A835B(1835)", RC_MAP_IT913X_V2) },
	{ DVB_USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_A835B_2835,
		&af9035_props, "Avermedia A835B(2835)", RC_MAP_IT913X_V2) },
	{ DVB_USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_A835B_3835,
		&af9035_props, "Avermedia A835B(3835)", RC_MAP_IT913X_V2) },
	{ DVB_USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_A835B_4835,
		&af9035_props, "Avermedia A835B(4835)",	RC_MAP_IT913X_V2) },
2114 2115
	{ DVB_USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_TD110,
		&af9035_props, "Avermedia AverTV Volar HD 2 (TD110)", RC_MAP_AVERMEDIA_RM_KS) },
2116 2117
	{ DVB_USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_H335,
		&af9035_props, "Avermedia H335", RC_MAP_IT913X_V2) },
2118 2119 2120 2121 2122 2123 2124 2125
	{ DVB_USB_DEVICE(USB_VID_KWORLD_2, USB_PID_KWORLD_UB499_2T_T09,
		&af9035_props, "Kworld UB499-2T T09", RC_MAP_IT913X_V1) },
	{ DVB_USB_DEVICE(USB_VID_KWORLD_2, USB_PID_SVEON_STV22_IT9137,
		&af9035_props, "Sveon STV22 Dual DVB-T HDTV",
							RC_MAP_IT913X_V1) },
	{ DVB_USB_DEVICE(USB_VID_KWORLD_2, USB_PID_CTVDIGDUAL_V2,
		&af9035_props, "Digital Dual TV Receiver CTVDIGDUAL_V2",
							RC_MAP_IT913X_V1) },
2126 2127
	/* XXX: that same ID [0ccd:0099] is used by af9015 driver too */
	{ DVB_USB_DEVICE(USB_VID_TERRATEC, 0x0099,
2128 2129
		&af9035_props, "TerraTec Cinergy T Stick Dual RC (rev. 2)",
		NULL) },
2130 2131
	{ DVB_USB_DEVICE(USB_VID_LEADTEK, 0x6a05,
		&af9035_props, "Leadtek WinFast DTV Dongle Dual", NULL) },
2132 2133
	{ DVB_USB_DEVICE(USB_VID_HAUPPAUGE, 0xf900,
		&af9035_props, "Hauppauge WinTV-MiniStick 2", NULL) },
2134
	{ DVB_USB_DEVICE(USB_VID_PCTV, USB_PID_PCTV_78E,
2135
		&af9035_props, "PCTV AndroiDTV (78e)", RC_MAP_IT913X_V1) },
2136
	{ DVB_USB_DEVICE(USB_VID_PCTV, USB_PID_PCTV_79E,
2137
		&af9035_props, "PCTV microStick (79e)", RC_MAP_IT913X_V2) },
2138 2139 2140 2141

	/* IT930x devices */
	{ DVB_USB_DEVICE(USB_VID_ITETECH, USB_PID_ITETECH_IT9303,
		&it930x_props, "ITE 9303 Generic", NULL) },
2142 2143 2144 2145
	{ }
};
MODULE_DEVICE_TABLE(usb, af9035_id_table);

2146
static struct usb_driver af9035_usb_driver = {
2147 2148
	.name = KBUILD_MODNAME,
	.id_table = af9035_id_table,
2149
	.probe = af9035_probe,
2150 2151 2152
	.disconnect = dvb_usbv2_disconnect,
	.suspend = dvb_usbv2_suspend,
	.resume = dvb_usbv2_resume,
2153
	.reset_resume = dvb_usbv2_reset_resume,
2154 2155
	.no_dynamic_id = 1,
	.soft_unbind = 1,
2156 2157
};

2158
module_usb_driver(af9035_usb_driver);
2159 2160 2161 2162

MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
MODULE_DESCRIPTION("Afatech AF9035 driver");
MODULE_LICENSE("GPL");
2163
MODULE_FIRMWARE(AF9035_FIRMWARE_AF9035);
2164 2165
MODULE_FIRMWARE(AF9035_FIRMWARE_IT9135_V1);
MODULE_FIRMWARE(AF9035_FIRMWARE_IT9135_V2);
2166
MODULE_FIRMWARE(AF9035_FIRMWARE_IT9303);