2 * Afatech AF9035 DVB USB driver
4 * Copyright (C) 2009 Antti Palosaari <crope@iki.fi>
5 * Copyright (C) 2012 Antti Palosaari <crope@iki.fi>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 /* Max transfer size done by I2C transfer functions */
25 #define MAX_XFER_SIZE 64
27 DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
29 static u16 af9035_checksum(const u8 *buf, size_t len)
34 for (i = 1; i < len; i++) {
36 checksum += buf[i] << 8;
45 static int af9035_ctrl_msg(struct dvb_usb_device *d, struct usb_req *req)
47 #define REQ_HDR_LEN 4 /* send header size */
48 #define ACK_HDR_LEN 3 /* rece header size */
49 #define CHECKSUM_LEN 2
50 #define USB_TIMEOUT 2000
51 struct state *state = d_to_priv(d);
52 struct usb_interface *intf = d->intf;
54 u16 checksum, tmp_checksum;
56 mutex_lock(&d->usb_mutex);
58 /* buffer overflow check */
59 if (req->wlen > (BUF_LEN - REQ_HDR_LEN - CHECKSUM_LEN) ||
60 req->rlen > (BUF_LEN - ACK_HDR_LEN - CHECKSUM_LEN)) {
61 dev_err(&intf->dev, "too much data wlen=%d rlen=%d\n",
62 req->wlen, req->rlen);
67 state->buf[0] = REQ_HDR_LEN + req->wlen + CHECKSUM_LEN - 1;
68 state->buf[1] = req->mbox;
69 state->buf[2] = req->cmd;
70 state->buf[3] = state->seq++;
71 memcpy(&state->buf[REQ_HDR_LEN], req->wbuf, req->wlen);
73 wlen = REQ_HDR_LEN + req->wlen + CHECKSUM_LEN;
74 rlen = ACK_HDR_LEN + req->rlen + CHECKSUM_LEN;
76 /* calc and add checksum */
77 checksum = af9035_checksum(state->buf, state->buf[0] - 1);
78 state->buf[state->buf[0] - 1] = (checksum >> 8);
79 state->buf[state->buf[0] - 0] = (checksum & 0xff);
81 /* no ack for these packets */
82 if (req->cmd == CMD_FW_DL)
85 ret = dvb_usbv2_generic_rw_locked(d,
86 state->buf, wlen, state->buf, rlen);
90 /* no ack for those packets */
91 if (req->cmd == CMD_FW_DL)
95 checksum = af9035_checksum(state->buf, rlen - 2);
96 tmp_checksum = (state->buf[rlen - 2] << 8) | state->buf[rlen - 1];
97 if (tmp_checksum != checksum) {
98 dev_err(&intf->dev, "command=%02x checksum mismatch (%04x != %04x)\n",
99 req->cmd, tmp_checksum, checksum);
106 /* fw returns status 1 when IR code was not received */
107 if (req->cmd == CMD_IR_GET || state->buf[2] == 1) {
112 dev_dbg(&intf->dev, "command=%02x failed fw error=%d\n",
113 req->cmd, state->buf[2]);
118 /* read request, copy returned data to return buf */
120 memcpy(req->rbuf, &state->buf[ACK_HDR_LEN], req->rlen);
122 mutex_unlock(&d->usb_mutex);
124 dev_dbg(&intf->dev, "failed=%d\n", ret);
128 /* write multiple registers */
129 static int af9035_wr_regs(struct dvb_usb_device *d, u32 reg, u8 *val, int len)
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 };
136 if (6 + len > sizeof(wbuf)) {
137 dev_warn(&intf->dev, "i2c wr: len=%d is too big!\n", len);
145 wbuf[4] = (reg >> 8) & 0xff;
146 wbuf[5] = (reg >> 0) & 0xff;
147 memcpy(&wbuf[6], val, len);
149 return af9035_ctrl_msg(d, &req);
152 /* read multiple registers */
153 static int af9035_rd_regs(struct dvb_usb_device *d, u32 reg, u8 *val, int len)
155 u8 wbuf[] = { len, 2, 0, 0, (reg >> 8) & 0xff, reg & 0xff };
156 u8 mbox = (reg >> 16) & 0xff;
157 struct usb_req req = { CMD_MEM_RD, mbox, sizeof(wbuf), wbuf, len, val };
159 return af9035_ctrl_msg(d, &req);
162 /* write single register */
163 static int af9035_wr_reg(struct dvb_usb_device *d, u32 reg, u8 val)
165 return af9035_wr_regs(d, reg, &val, 1);
168 /* read single register */
169 static int af9035_rd_reg(struct dvb_usb_device *d, u32 reg, u8 *val)
171 return af9035_rd_regs(d, reg, val, 1);
174 /* write single register with mask */
175 static int af9035_wr_reg_mask(struct dvb_usb_device *d, u32 reg, u8 val,
181 /* no need for read if whole reg is written */
183 ret = af9035_rd_regs(d, reg, &tmp, 1);
192 return af9035_wr_regs(d, reg, &val, 1);
195 static int af9035_add_i2c_dev(struct dvb_usb_device *d, const char *type,
196 u8 addr, void *platform_data, struct i2c_adapter *adapter)
199 struct state *state = d_to_priv(d);
200 struct usb_interface *intf = d->intf;
201 struct i2c_client *client;
202 struct i2c_board_info board_info = {
204 .platform_data = platform_data,
207 strlcpy(board_info.type, type, I2C_NAME_SIZE);
209 /* find first free client */
210 for (num = 0; num < AF9035_I2C_CLIENT_MAX; num++) {
211 if (state->i2c_client[num] == NULL)
215 dev_dbg(&intf->dev, "num=%d\n", num);
217 if (num == AF9035_I2C_CLIENT_MAX) {
218 dev_err(&intf->dev, "I2C client out of index\n");
223 request_module("%s", board_info.type);
225 /* register I2C device */
226 client = i2c_new_device(adapter, &board_info);
227 if (client == NULL || client->dev.driver == NULL) {
232 /* increase I2C driver usage count */
233 if (!try_module_get(client->dev.driver->owner)) {
234 i2c_unregister_device(client);
239 state->i2c_client[num] = client;
242 dev_dbg(&intf->dev, "failed=%d\n", ret);
246 static void af9035_del_i2c_dev(struct dvb_usb_device *d)
249 struct state *state = d_to_priv(d);
250 struct usb_interface *intf = d->intf;
251 struct i2c_client *client;
253 /* find last used client */
254 num = AF9035_I2C_CLIENT_MAX;
256 if (state->i2c_client[num] != NULL)
260 dev_dbg(&intf->dev, "num=%d\n", num);
263 dev_err(&intf->dev, "I2C client out of index\n");
267 client = state->i2c_client[num];
269 /* decrease I2C driver usage count */
270 module_put(client->dev.driver->owner);
272 /* unregister I2C device */
273 i2c_unregister_device(client);
275 state->i2c_client[num] = NULL;
278 dev_dbg(&intf->dev, "failed\n");
281 static int af9035_i2c_master_xfer(struct i2c_adapter *adap,
282 struct i2c_msg msg[], int num)
284 struct dvb_usb_device *d = i2c_get_adapdata(adap);
285 struct state *state = d_to_priv(d);
288 if (mutex_lock_interruptible(&d->i2c_mutex) < 0)
292 * AF9035 I2C sub header is 5 bytes long. Meaning of those bytes are:
296 * byte 3 and 4 can be used as reg addr
298 * used when reg addr len is set to 2
300 * used when reg addr len is set to 1 or 2
302 * For the simplify we do not use register addr at all.
303 * NOTE: As a firmware knows tuner type there is very small possibility
304 * there could be some tuner I2C hacks done by firmware and this may
305 * lead problems if firmware expects those bytes are used.
307 * TODO: Here is few hacks. AF9035 chip integrates AF9033 demodulator.
308 * IT9135 chip integrates AF9033 demodulator and RF tuner. For dual
309 * tuner devices, there is also external AF9033 demodulator connected
310 * via external I2C bus. All AF9033 demod I2C traffic, both single and
311 * dual tuner configuration, is covered by firmware - actual USB IO
312 * looks just like a memory access.
313 * In case of IT913x chip, there is own tuner driver. It is implemented
314 * currently as a I2C driver, even tuner IP block is likely build
315 * directly into the demodulator memory space and there is no own I2C
316 * bus. I2C subsystem does not allow register multiple devices to same
317 * bus, having same slave address. Due to that we reuse demod address,
318 * shifted by one bit, on that case.
320 * For IT930x we use a different command and the sub header is
323 * 1: I2C bus (0x03 seems to be only value used)
326 #define AF9035_IS_I2C_XFER_WRITE_READ(_msg, _num) \
327 (_num == 2 && !(_msg[0].flags & I2C_M_RD) && (_msg[1].flags & I2C_M_RD))
328 #define AF9035_IS_I2C_XFER_WRITE(_msg, _num) \
329 (_num == 1 && !(_msg[0].flags & I2C_M_RD))
330 #define AF9035_IS_I2C_XFER_READ(_msg, _num) \
331 (_num == 1 && (_msg[0].flags & I2C_M_RD))
333 if (AF9035_IS_I2C_XFER_WRITE_READ(msg, num)) {
334 if (msg[0].len > 40 || msg[1].len > 40) {
335 /* TODO: correct limits > 40 */
337 } else if ((msg[0].addr == state->af9033_i2c_addr[0]) ||
338 (msg[0].addr == state->af9033_i2c_addr[1])) {
339 /* demod access via firmware interface */
340 u32 reg = msg[0].buf[0] << 16 | msg[0].buf[1] << 8 |
343 if (msg[0].addr == state->af9033_i2c_addr[1])
346 ret = af9035_rd_regs(d, reg, &msg[1].buf[0],
348 } else if (state->no_read) {
349 memset(msg[1].buf, 0, msg[1].len);
352 /* I2C write + read */
353 u8 buf[MAX_XFER_SIZE];
354 struct usb_req req = { CMD_I2C_RD, 0, 5 + msg[0].len,
355 buf, msg[1].len, msg[1].buf };
357 if (state->chip_type == 0x9306) {
358 req.cmd = CMD_GENERIC_I2C_RD;
359 req.wlen = 3 + msg[0].len;
361 req.mbox |= ((msg[0].addr & 0x80) >> 3);
364 if (state->chip_type == 0x9306) {
365 buf[1] = 0x03; /* I2C bus */
366 buf[2] = msg[0].addr << 1;
367 memcpy(&buf[3], msg[0].buf, msg[0].len);
369 buf[1] = msg[0].addr << 1;
370 buf[3] = 0x00; /* reg addr MSB */
371 buf[4] = 0x00; /* reg addr LSB */
373 /* Keep prev behavior for write req len > 2*/
374 if (msg[0].len > 2) {
375 buf[2] = 0x00; /* reg addr len */
376 memcpy(&buf[5], msg[0].buf, msg[0].len);
378 /* Use reg addr fields if write req len <= 2 */
382 if (msg[0].len == 2) {
383 buf[3] = msg[0].buf[0];
384 buf[4] = msg[0].buf[1];
385 } else if (msg[0].len == 1) {
386 buf[4] = msg[0].buf[0];
390 ret = af9035_ctrl_msg(d, &req);
392 } else if (AF9035_IS_I2C_XFER_WRITE(msg, num)) {
393 if (msg[0].len > 40) {
394 /* TODO: correct limits > 40 */
396 } else if ((msg[0].addr == state->af9033_i2c_addr[0]) ||
397 (msg[0].addr == state->af9033_i2c_addr[1])) {
398 /* demod access via firmware interface */
399 u32 reg = msg[0].buf[0] << 16 | msg[0].buf[1] << 8 |
402 if (msg[0].addr == state->af9033_i2c_addr[1])
405 ret = (msg[0].len >= 3) ? af9035_wr_regs(d, reg,
411 u8 buf[MAX_XFER_SIZE];
412 struct usb_req req = { CMD_I2C_WR, 0, 5 + msg[0].len,
415 if (state->chip_type == 0x9306) {
416 req.cmd = CMD_GENERIC_I2C_WR;
417 req.wlen = 3 + msg[0].len;
420 req.mbox |= ((msg[0].addr & 0x80) >> 3);
422 if (state->chip_type == 0x9306) {
423 buf[1] = 0x03; /* I2C bus */
424 buf[2] = msg[0].addr << 1;
425 memcpy(&buf[3], msg[0].buf, msg[0].len);
427 buf[1] = msg[0].addr << 1;
428 buf[2] = 0x00; /* reg addr len */
429 buf[3] = 0x00; /* reg addr MSB */
430 buf[4] = 0x00; /* reg addr LSB */
431 memcpy(&buf[5], msg[0].buf, msg[0].len);
433 ret = af9035_ctrl_msg(d, &req);
435 } else if (AF9035_IS_I2C_XFER_READ(msg, num)) {
436 if (msg[0].len > 40) {
437 /* TODO: correct limits > 40 */
439 } else if (state->no_read) {
440 memset(msg[0].buf, 0, msg[0].len);
445 struct usb_req req = { CMD_I2C_RD, 0, sizeof(buf),
446 buf, msg[0].len, msg[0].buf };
448 if (state->chip_type == 0x9306) {
449 req.cmd = CMD_GENERIC_I2C_RD;
452 req.mbox |= ((msg[0].addr & 0x80) >> 3);
454 if (state->chip_type == 0x9306) {
455 buf[1] = 0x03; /* I2C bus */
456 buf[2] = msg[0].addr << 1;
458 buf[1] = msg[0].addr << 1;
459 buf[2] = 0x00; /* reg addr len */
460 buf[3] = 0x00; /* reg addr MSB */
461 buf[4] = 0x00; /* reg addr LSB */
463 ret = af9035_ctrl_msg(d, &req);
467 * We support only three kind of I2C transactions:
468 * 1) 1 x write + 1 x read (repeated start)
475 mutex_unlock(&d->i2c_mutex);
483 static u32 af9035_i2c_functionality(struct i2c_adapter *adapter)
488 static struct i2c_algorithm af9035_i2c_algo = {
489 .master_xfer = af9035_i2c_master_xfer,
490 .functionality = af9035_i2c_functionality,
493 static int af9035_identify_state(struct dvb_usb_device *d, const char **name)
495 struct state *state = d_to_priv(d);
496 struct usb_interface *intf = d->intf;
497 int ret, i, ts_mode_invalid;
498 unsigned int utmp, eeprom_addr;
502 struct usb_req req = { CMD_FW_QUERYINFO, 0, sizeof(wbuf), wbuf,
503 sizeof(rbuf), rbuf };
505 ret = af9035_rd_regs(d, 0x1222, rbuf, 3);
509 state->chip_version = rbuf[0];
510 state->chip_type = rbuf[2] << 8 | rbuf[1] << 0;
512 ret = af9035_rd_reg(d, 0x384f, &state->prechip_version);
516 dev_info(&intf->dev, "prechip_version=%02x chip_version=%02x chip_type=%04x\n",
517 state->prechip_version, state->chip_version, state->chip_type);
519 if (state->chip_type == 0x9135) {
520 if (state->chip_version == 0x02) {
521 *name = AF9035_FIRMWARE_IT9135_V2;
524 *name = AF9035_FIRMWARE_IT9135_V1;
528 /* Check if eeprom exists */
529 ret = af9035_rd_reg(d, utmp, &tmp);
534 dev_dbg(&intf->dev, "no eeprom\n");
535 state->no_eeprom = true;
536 goto check_firmware_status;
539 eeprom_addr = EEPROM_BASE_IT9135;
540 } else if (state->chip_type == 0x9306) {
541 *name = AF9035_FIRMWARE_IT9303;
542 state->no_eeprom = true;
543 goto check_firmware_status;
545 *name = AF9035_FIRMWARE_AF9035;
546 eeprom_addr = EEPROM_BASE_AF9035;
549 /* Read and store eeprom */
550 for (i = 0; i < 256; i += 32) {
551 ret = af9035_rd_regs(d, eeprom_addr + i, &state->eeprom[i], 32);
556 dev_dbg(&intf->dev, "eeprom dump:\n");
557 for (i = 0; i < 256; i += 16)
558 dev_dbg(&intf->dev, "%*ph\n", 16, &state->eeprom[i]);
560 /* check for dual tuner mode */
561 tmp = state->eeprom[EEPROM_TS_MODE];
568 state->dual_mode = true;
571 if (state->chip_type != 0x9135 && state->chip_type != 0x9306)
572 state->dual_mode = true; /* AF9035 */
580 dev_dbg(&intf->dev, "ts mode=%d dual mode=%d\n", tmp, state->dual_mode);
583 dev_info(&intf->dev, "ts mode=%d not supported, defaulting to single tuner mode!", tmp);
585 check_firmware_status:
586 ret = af9035_ctrl_msg(d, &req);
590 dev_dbg(&intf->dev, "reply=%*ph\n", 4, rbuf);
591 if (rbuf[0] || rbuf[1] || rbuf[2] || rbuf[3])
599 dev_dbg(&intf->dev, "failed=%d\n", ret);
604 static int af9035_download_firmware_old(struct dvb_usb_device *d,
605 const struct firmware *fw)
607 struct usb_interface *intf = d->intf;
610 struct usb_req req = { 0, 0, 0, NULL, 0, NULL };
611 struct usb_req req_fw_dl = { CMD_FW_DL, 0, 0, wbuf, 0, NULL };
613 u16 hdr_addr, hdr_data_len, hdr_checksum;
618 * Thanks to Daniel Glöckner <daniel-gl@gmx.net> about that info!
620 * byte 0: MCS 51 core
621 * There are two inside the AF9035 (1=Link and 2=OFDM) with separate
623 * byte 1-2: Big endian destination address
624 * byte 3-4: Big endian number of data bytes following the header
625 * byte 5-6: Big endian header checksum, apparently ignored by the chip
626 * Calculated as ~(h[0]*256+h[1]+h[2]*256+h[3]+h[4]*256)
629 for (i = fw->size; i > HDR_SIZE;) {
630 hdr_core = fw->data[fw->size - i + 0];
631 hdr_addr = fw->data[fw->size - i + 1] << 8;
632 hdr_addr |= fw->data[fw->size - i + 2] << 0;
633 hdr_data_len = fw->data[fw->size - i + 3] << 8;
634 hdr_data_len |= fw->data[fw->size - i + 4] << 0;
635 hdr_checksum = fw->data[fw->size - i + 5] << 8;
636 hdr_checksum |= fw->data[fw->size - i + 6] << 0;
638 dev_dbg(&intf->dev, "core=%d addr=%04x data_len=%d checksum=%04x\n",
639 hdr_core, hdr_addr, hdr_data_len, hdr_checksum);
641 if (((hdr_core != 1) && (hdr_core != 2)) ||
642 (hdr_data_len > i)) {
643 dev_dbg(&intf->dev, "bad firmware\n");
647 /* download begin packet */
648 req.cmd = CMD_FW_DL_BEGIN;
649 ret = af9035_ctrl_msg(d, &req);
653 /* download firmware packet(s) */
654 for (j = HDR_SIZE + hdr_data_len; j > 0; j -= MAX_DATA) {
658 req_fw_dl.wlen = len;
659 req_fw_dl.wbuf = (u8 *) &fw->data[fw->size - i +
660 HDR_SIZE + hdr_data_len - j];
661 ret = af9035_ctrl_msg(d, &req_fw_dl);
666 /* download end packet */
667 req.cmd = CMD_FW_DL_END;
668 ret = af9035_ctrl_msg(d, &req);
672 i -= hdr_data_len + HDR_SIZE;
674 dev_dbg(&intf->dev, "data uploaded=%zu\n", fw->size - i);
677 /* print warn if firmware is bad, continue and see what happens */
679 dev_warn(&intf->dev, "bad firmware\n");
684 dev_dbg(&intf->dev, "failed=%d\n", ret);
689 static int af9035_download_firmware_new(struct dvb_usb_device *d,
690 const struct firmware *fw)
692 struct usb_interface *intf = d->intf;
694 struct usb_req req_fw_dl = { CMD_FW_SCATTER_WR, 0, 0, NULL, 0, NULL };
698 * There seems to be following firmware header. Meaning of bytes 0-3
707 * 6: count of data bytes ?
709 for (i = HDR_SIZE, i_prev = 0; i <= fw->size; i++) {
711 (fw->data[i + 0] == 0x03 &&
712 (fw->data[i + 1] == 0x00 ||
713 fw->data[i + 1] == 0x01) &&
714 fw->data[i + 2] == 0x00)) {
715 req_fw_dl.wlen = i - i_prev;
716 req_fw_dl.wbuf = (u8 *) &fw->data[i_prev];
718 ret = af9035_ctrl_msg(d, &req_fw_dl);
722 dev_dbg(&intf->dev, "data uploaded=%d\n", i);
729 dev_dbg(&intf->dev, "failed=%d\n", ret);
734 static int af9035_download_firmware(struct dvb_usb_device *d,
735 const struct firmware *fw)
737 struct usb_interface *intf = d->intf;
738 struct state *state = d_to_priv(d);
743 struct usb_req req = { 0, 0, 0, NULL, 0, NULL };
744 struct usb_req req_fw_ver = { CMD_FW_QUERYINFO, 0, 1, wbuf, 4, rbuf };
746 dev_dbg(&intf->dev, "\n");
749 * In case of dual tuner configuration we need to do some extra
750 * initialization in order to download firmware to slave demod too,
751 * which is done by master demod.
752 * Master feeds also clock and controls power via GPIO.
754 if (state->dual_mode) {
755 /* configure gpioh1, reset & power slave demod */
756 ret = af9035_wr_reg_mask(d, 0x00d8b0, 0x01, 0x01);
760 ret = af9035_wr_reg_mask(d, 0x00d8b1, 0x01, 0x01);
764 ret = af9035_wr_reg_mask(d, 0x00d8af, 0x00, 0x01);
768 usleep_range(10000, 50000);
770 ret = af9035_wr_reg_mask(d, 0x00d8af, 0x01, 0x01);
774 /* tell the slave I2C address */
775 tmp = state->eeprom[EEPROM_2ND_DEMOD_ADDR];
777 /* Use default I2C address if eeprom has no address set */
779 tmp = 0x1d << 1; /* 8-bit format used by chip */
781 if ((state->chip_type == 0x9135) ||
782 (state->chip_type == 0x9306)) {
783 ret = af9035_wr_reg(d, 0x004bfb, tmp);
787 ret = af9035_wr_reg(d, 0x00417f, tmp);
791 /* enable clock out */
792 ret = af9035_wr_reg_mask(d, 0x00d81a, 0x01, 0x01);
798 if (fw->data[0] == 0x01)
799 ret = af9035_download_firmware_old(d, fw);
801 ret = af9035_download_firmware_new(d, fw);
805 /* firmware loaded, request boot */
806 req.cmd = CMD_FW_BOOT;
807 ret = af9035_ctrl_msg(d, &req);
811 /* ensure firmware starts */
813 ret = af9035_ctrl_msg(d, &req_fw_ver);
817 if (!(rbuf[0] || rbuf[1] || rbuf[2] || rbuf[3])) {
818 dev_err(&intf->dev, "firmware did not run\n");
823 dev_info(&intf->dev, "firmware version=%d.%d.%d.%d",
824 rbuf[0], rbuf[1], rbuf[2], rbuf[3]);
829 dev_dbg(&intf->dev, "failed=%d\n", ret);
834 static int af9035_read_config(struct dvb_usb_device *d)
836 struct usb_interface *intf = d->intf;
837 struct state *state = d_to_priv(d);
842 /* Demod I2C address */
843 state->af9033_i2c_addr[0] = 0x1c;
844 state->af9033_i2c_addr[1] = 0x1d;
845 state->af9033_config[0].adc_multiplier = AF9033_ADC_MULTIPLIER_2X;
846 state->af9033_config[1].adc_multiplier = AF9033_ADC_MULTIPLIER_2X;
847 state->af9033_config[0].ts_mode = AF9033_TS_MODE_USB;
848 state->af9033_config[1].ts_mode = AF9033_TS_MODE_SERIAL;
850 if (state->chip_type == 0x9135) {
851 /* feed clock for integrated RF tuner */
852 state->af9033_config[0].dyn0_clk = true;
853 state->af9033_config[1].dyn0_clk = true;
855 if (state->chip_version == 0x02) {
856 state->af9033_config[0].tuner = AF9033_TUNER_IT9135_60;
857 state->af9033_config[1].tuner = AF9033_TUNER_IT9135_60;
859 state->af9033_config[0].tuner = AF9033_TUNER_IT9135_38;
860 state->af9033_config[1].tuner = AF9033_TUNER_IT9135_38;
863 if (state->no_eeprom) {
864 /* Remote controller to NEC polling by default */
865 state->ir_mode = 0x05;
866 state->ir_type = 0x00;
870 } else if (state->chip_type == 0x9306) {
872 * IT930x is an USB bridge, only single demod-single tuner
873 * configurations seen so far.
878 /* Remote controller */
879 state->ir_mode = state->eeprom[EEPROM_IR_MODE];
880 state->ir_type = state->eeprom[EEPROM_IR_TYPE];
882 if (state->dual_mode) {
883 /* Read 2nd demodulator I2C address. 8-bit format on eeprom */
884 tmp = state->eeprom[EEPROM_2ND_DEMOD_ADDR];
886 state->af9033_i2c_addr[1] = tmp >> 1;
888 dev_dbg(&intf->dev, "2nd demod I2C addr=%02x\n",
889 state->af9033_i2c_addr[1]);
892 for (i = 0; i < state->dual_mode + 1; i++) {
893 unsigned int eeprom_offset = 0;
896 tmp = state->eeprom[EEPROM_1_TUNER_ID + eeprom_offset];
897 dev_dbg(&intf->dev, "[%d]tuner=%02x\n", i, tmp);
899 /* tuner sanity check */
900 if (state->chip_type == 0x9135) {
901 if (state->chip_version == 0x02) {
904 case AF9033_TUNER_IT9135_60:
905 case AF9033_TUNER_IT9135_61:
906 case AF9033_TUNER_IT9135_62:
907 state->af9033_config[i].tuner = tmp;
913 case AF9033_TUNER_IT9135_38:
914 case AF9033_TUNER_IT9135_51:
915 case AF9033_TUNER_IT9135_52:
916 state->af9033_config[i].tuner = tmp;
922 state->af9033_config[i].tuner = tmp;
925 if (state->af9033_config[i].tuner != tmp) {
926 dev_info(&intf->dev, "[%d] overriding tuner from %02x to %02x\n",
927 i, tmp, state->af9033_config[i].tuner);
930 switch (state->af9033_config[i].tuner) {
931 case AF9033_TUNER_TUA9001:
932 case AF9033_TUNER_FC0011:
933 case AF9033_TUNER_MXL5007T:
934 case AF9033_TUNER_TDA18218:
935 case AF9033_TUNER_FC2580:
936 case AF9033_TUNER_FC0012:
937 state->af9033_config[i].spec_inv = 1;
939 case AF9033_TUNER_IT9135_38:
940 case AF9033_TUNER_IT9135_51:
941 case AF9033_TUNER_IT9135_52:
942 case AF9033_TUNER_IT9135_60:
943 case AF9033_TUNER_IT9135_61:
944 case AF9033_TUNER_IT9135_62:
947 dev_warn(&intf->dev, "tuner id=%02x not supported, please report!",
951 /* disable dual mode if driver does not support it */
953 switch (state->af9033_config[i].tuner) {
954 case AF9033_TUNER_FC0012:
955 case AF9033_TUNER_IT9135_38:
956 case AF9033_TUNER_IT9135_51:
957 case AF9033_TUNER_IT9135_52:
958 case AF9033_TUNER_IT9135_60:
959 case AF9033_TUNER_IT9135_61:
960 case AF9033_TUNER_IT9135_62:
961 case AF9033_TUNER_MXL5007T:
964 state->dual_mode = false;
965 dev_info(&intf->dev, "driver does not support 2nd tuner and will disable it");
968 /* tuner IF frequency */
969 tmp = state->eeprom[EEPROM_1_IF_L + eeprom_offset];
971 tmp = state->eeprom[EEPROM_1_IF_H + eeprom_offset];
973 dev_dbg(&intf->dev, "[%d]IF=%d\n", i, tmp16);
975 eeprom_offset += 0x10; /* shift for the 2nd tuner params */
979 /* get demod clock */
980 ret = af9035_rd_reg(d, 0x00d800, &tmp);
984 tmp = (tmp >> 0) & 0x0f;
986 for (i = 0; i < ARRAY_SIZE(state->af9033_config); i++) {
987 if (state->chip_type == 0x9135)
988 state->af9033_config[i].clock = clock_lut_it9135[tmp];
990 state->af9033_config[i].clock = clock_lut_af9035[tmp];
993 state->no_read = false;
994 /* Some MXL5007T devices cannot properly handle tuner I2C read ops. */
995 if (state->af9033_config[0].tuner == AF9033_TUNER_MXL5007T &&
996 le16_to_cpu(d->udev->descriptor.idVendor) == USB_VID_AVERMEDIA)
998 switch (le16_to_cpu(d->udev->descriptor.idProduct)) {
999 case USB_PID_AVERMEDIA_A867:
1000 case USB_PID_AVERMEDIA_TWINSTAR:
1001 dev_info(&intf->dev,
1002 "Device may have issues with I2C read operations. Enabling fix.\n");
1003 state->no_read = true;
1010 dev_dbg(&intf->dev, "failed=%d\n", ret);
1015 static int af9035_tua9001_tuner_callback(struct dvb_usb_device *d,
1018 struct usb_interface *intf = d->intf;
1022 dev_dbg(&intf->dev, "cmd=%d arg=%d\n", cmd, arg);
1025 * CEN always enabled by hardware wiring
1031 case TUA9001_CMD_RESETN:
1037 ret = af9035_wr_reg_mask(d, 0x00d8e7, val, 0x01);
1041 case TUA9001_CMD_RXEN:
1047 ret = af9035_wr_reg_mask(d, 0x00d8eb, val, 0x01);
1056 dev_dbg(&intf->dev, "failed=%d\n", ret);
1062 static int af9035_fc0011_tuner_callback(struct dvb_usb_device *d,
1065 struct usb_interface *intf = d->intf;
1069 case FC0011_FE_CALLBACK_POWER:
1071 ret = af9035_wr_reg_mask(d, 0xd8eb, 1, 1);
1075 ret = af9035_wr_reg_mask(d, 0xd8ec, 1, 1);
1079 ret = af9035_wr_reg_mask(d, 0xd8ed, 1, 1);
1084 ret = af9035_wr_reg_mask(d, 0xd8d0, 1, 1);
1088 ret = af9035_wr_reg_mask(d, 0xd8d1, 1, 1);
1092 usleep_range(10000, 50000);
1094 case FC0011_FE_CALLBACK_RESET:
1095 ret = af9035_wr_reg(d, 0xd8e9, 1);
1099 ret = af9035_wr_reg(d, 0xd8e8, 1);
1103 ret = af9035_wr_reg(d, 0xd8e7, 1);
1107 usleep_range(10000, 20000);
1109 ret = af9035_wr_reg(d, 0xd8e7, 0);
1113 usleep_range(10000, 20000);
1123 dev_dbg(&intf->dev, "failed=%d\n", ret);
1128 static int af9035_tuner_callback(struct dvb_usb_device *d, int cmd, int arg)
1130 struct state *state = d_to_priv(d);
1132 switch (state->af9033_config[0].tuner) {
1133 case AF9033_TUNER_FC0011:
1134 return af9035_fc0011_tuner_callback(d, cmd, arg);
1135 case AF9033_TUNER_TUA9001:
1136 return af9035_tua9001_tuner_callback(d, cmd, arg);
1144 static int af9035_frontend_callback(void *adapter_priv, int component,
1147 struct i2c_adapter *adap = adapter_priv;
1148 struct dvb_usb_device *d = i2c_get_adapdata(adap);
1149 struct usb_interface *intf = d->intf;
1151 dev_dbg(&intf->dev, "component=%d cmd=%d arg=%d\n",
1152 component, cmd, arg);
1154 switch (component) {
1155 case DVB_FRONTEND_COMPONENT_TUNER:
1156 return af9035_tuner_callback(d, cmd, arg);
1164 static int af9035_get_adapter_count(struct dvb_usb_device *d)
1166 struct state *state = d_to_priv(d);
1168 return state->dual_mode + 1;
1171 static int af9035_frontend_attach(struct dvb_usb_adapter *adap)
1173 struct state *state = adap_to_priv(adap);
1174 struct dvb_usb_device *d = adap_to_d(adap);
1175 struct usb_interface *intf = d->intf;
1178 dev_dbg(&intf->dev, "adap->id=%d\n", adap->id);
1180 if (!state->af9033_config[adap->id].tuner) {
1181 /* unsupported tuner */
1186 state->af9033_config[adap->id].fe = &adap->fe[0];
1187 state->af9033_config[adap->id].ops = &state->ops;
1188 ret = af9035_add_i2c_dev(d, "af9033", state->af9033_i2c_addr[adap->id],
1189 &state->af9033_config[adap->id], &d->i2c_adap);
1193 if (adap->fe[0] == NULL) {
1198 /* disable I2C-gate */
1199 adap->fe[0]->ops.i2c_gate_ctrl = NULL;
1200 adap->fe[0]->callback = af9035_frontend_callback;
1205 dev_dbg(&intf->dev, "failed=%d\n", ret);
1210 static int it930x_frontend_attach(struct dvb_usb_adapter *adap)
1212 struct state *state = adap_to_priv(adap);
1213 struct dvb_usb_device *d = adap_to_d(adap);
1214 struct usb_interface *intf = d->intf;
1216 struct si2168_config si2168_config;
1217 struct i2c_adapter *adapter;
1219 dev_dbg(&intf->dev, "adap->id=%d\n", adap->id);
1221 memset(&si2168_config, 0, sizeof(si2168_config));
1222 si2168_config.i2c_adapter = &adapter;
1223 si2168_config.fe = &adap->fe[0];
1224 si2168_config.ts_mode = SI2168_TS_SERIAL;
1226 state->af9033_config[adap->id].fe = &adap->fe[0];
1227 state->af9033_config[adap->id].ops = &state->ops;
1228 ret = af9035_add_i2c_dev(d, "si2168", 0x67, &si2168_config,
1233 if (adap->fe[0] == NULL) {
1237 state->i2c_adapter_demod = adapter;
1242 dev_dbg(&intf->dev, "failed=%d\n", ret);
1247 static int af9035_frontend_detach(struct dvb_usb_adapter *adap)
1249 struct state *state = adap_to_priv(adap);
1250 struct dvb_usb_device *d = adap_to_d(adap);
1251 struct usb_interface *intf = d->intf;
1253 dev_dbg(&intf->dev, "adap->id=%d\n", adap->id);
1255 if (adap->id == 1) {
1256 if (state->i2c_client[1])
1257 af9035_del_i2c_dev(d);
1258 } else if (adap->id == 0) {
1259 if (state->i2c_client[0])
1260 af9035_del_i2c_dev(d);
1266 static const struct fc0011_config af9035_fc0011_config = {
1267 .i2c_address = 0x60,
1270 static struct mxl5007t_config af9035_mxl5007t_config[] = {
1272 .xtal_freq_hz = MxL_XTAL_24_MHZ,
1273 .if_freq_hz = MxL_IF_4_57_MHZ,
1275 .loop_thru_enable = 0,
1276 .clk_out_enable = 0,
1277 .clk_out_amp = MxL_CLKOUT_AMP_0_94V,
1279 .xtal_freq_hz = MxL_XTAL_24_MHZ,
1280 .if_freq_hz = MxL_IF_4_57_MHZ,
1282 .loop_thru_enable = 1,
1283 .clk_out_enable = 1,
1284 .clk_out_amp = MxL_CLKOUT_AMP_0_94V,
1288 static struct tda18218_config af9035_tda18218_config = {
1289 .i2c_address = 0x60,
1293 static const struct fc0012_config af9035_fc0012_config[] = {
1295 .i2c_address = 0x63,
1296 .xtal_freq = FC_XTAL_36_MHZ,
1297 .dual_master = true,
1298 .loop_through = true,
1301 .i2c_address = 0x63 | 0x80, /* I2C bus select hack */
1302 .xtal_freq = FC_XTAL_36_MHZ,
1303 .dual_master = true,
1307 static int af9035_tuner_attach(struct dvb_usb_adapter *adap)
1309 struct state *state = adap_to_priv(adap);
1310 struct dvb_usb_device *d = adap_to_d(adap);
1311 struct usb_interface *intf = d->intf;
1313 struct dvb_frontend *fe;
1314 struct i2c_msg msg[1];
1317 dev_dbg(&intf->dev, "adap->id=%d\n", adap->id);
1320 * XXX: Hack used in that function: we abuse unused I2C address bit [7]
1321 * to carry info about used I2C bus for dual tuner configuration.
1324 switch (state->af9033_config[adap->id].tuner) {
1325 case AF9033_TUNER_TUA9001: {
1326 struct tua9001_platform_data tua9001_pdata = {
1327 .dvb_frontend = adap->fe[0],
1331 * AF9035 gpiot3 = TUA9001 RESETN
1332 * AF9035 gpiot2 = TUA9001 RXEN
1335 /* configure gpiot2 and gpiot2 as output */
1336 ret = af9035_wr_reg_mask(d, 0x00d8ec, 0x01, 0x01);
1340 ret = af9035_wr_reg_mask(d, 0x00d8ed, 0x01, 0x01);
1344 ret = af9035_wr_reg_mask(d, 0x00d8e8, 0x01, 0x01);
1348 ret = af9035_wr_reg_mask(d, 0x00d8e9, 0x01, 0x01);
1353 ret = af9035_add_i2c_dev(d, "tua9001", 0x60, &tua9001_pdata,
1361 case AF9033_TUNER_FC0011:
1362 fe = dvb_attach(fc0011_attach, adap->fe[0],
1363 &d->i2c_adap, &af9035_fc0011_config);
1365 case AF9033_TUNER_MXL5007T:
1366 if (adap->id == 0) {
1367 ret = af9035_wr_reg(d, 0x00d8e0, 1);
1371 ret = af9035_wr_reg(d, 0x00d8e1, 1);
1375 ret = af9035_wr_reg(d, 0x00d8df, 0);
1381 ret = af9035_wr_reg(d, 0x00d8df, 1);
1387 ret = af9035_wr_reg(d, 0x00d8c0, 1);
1391 ret = af9035_wr_reg(d, 0x00d8c1, 1);
1395 ret = af9035_wr_reg(d, 0x00d8bf, 0);
1399 ret = af9035_wr_reg(d, 0x00d8b4, 1);
1403 ret = af9035_wr_reg(d, 0x00d8b5, 1);
1407 ret = af9035_wr_reg(d, 0x00d8b3, 1);
1413 tuner_addr = 0x60 | 0x80; /* I2C bus hack */
1417 fe = dvb_attach(mxl5007t_attach, adap->fe[0], &d->i2c_adap,
1418 tuner_addr, &af9035_mxl5007t_config[adap->id]);
1420 case AF9033_TUNER_TDA18218:
1422 fe = dvb_attach(tda18218_attach, adap->fe[0],
1423 &d->i2c_adap, &af9035_tda18218_config);
1425 case AF9033_TUNER_FC2580: {
1426 struct fc2580_platform_data fc2580_pdata = {
1427 .dvb_frontend = adap->fe[0],
1430 /* Tuner enable using gpiot2_o, gpiot2_en and gpiot2_on */
1431 ret = af9035_wr_reg_mask(d, 0xd8eb, 0x01, 0x01);
1435 ret = af9035_wr_reg_mask(d, 0xd8ec, 0x01, 0x01);
1439 ret = af9035_wr_reg_mask(d, 0xd8ed, 0x01, 0x01);
1443 usleep_range(10000, 50000);
1445 ret = af9035_add_i2c_dev(d, "fc2580", 0x56, &fc2580_pdata,
1453 case AF9033_TUNER_FC0012:
1455 * AF9035 gpiot2 = FC0012 enable
1456 * XXX: there seems to be something on gpioh8 too, but on my
1457 * my test I didn't find any difference.
1460 if (adap->id == 0) {
1461 /* configure gpiot2 as output and high */
1462 ret = af9035_wr_reg_mask(d, 0xd8eb, 0x01, 0x01);
1466 ret = af9035_wr_reg_mask(d, 0xd8ec, 0x01, 0x01);
1470 ret = af9035_wr_reg_mask(d, 0xd8ed, 0x01, 0x01);
1475 * FIXME: That belongs for the FC0012 driver.
1476 * Write 02 to FC0012 master tuner register 0d directly
1477 * in order to make slave tuner working.
1482 msg[0].buf = "\x0d\x02";
1483 ret = i2c_transfer(&d->i2c_adap, msg, 1);
1488 usleep_range(10000, 50000);
1490 fe = dvb_attach(fc0012_attach, adap->fe[0], &d->i2c_adap,
1491 &af9035_fc0012_config[adap->id]);
1493 case AF9033_TUNER_IT9135_38:
1494 case AF9033_TUNER_IT9135_51:
1495 case AF9033_TUNER_IT9135_52:
1496 case AF9033_TUNER_IT9135_60:
1497 case AF9033_TUNER_IT9135_61:
1498 case AF9033_TUNER_IT9135_62:
1500 struct platform_device *pdev;
1502 struct it913x_platform_data it913x_pdata = {
1503 .regmap = state->af9033_config[adap->id].regmap,
1507 switch (state->af9033_config[adap->id].tuner) {
1508 case AF9033_TUNER_IT9135_38:
1509 case AF9033_TUNER_IT9135_51:
1510 case AF9033_TUNER_IT9135_52:
1511 name = "it9133ax-tuner";
1513 case AF9033_TUNER_IT9135_60:
1514 case AF9033_TUNER_IT9135_61:
1515 case AF9033_TUNER_IT9135_62:
1516 name = "it9133bx-tuner";
1523 if (state->dual_mode) {
1525 it913x_pdata.role = IT913X_ROLE_DUAL_MASTER;
1527 it913x_pdata.role = IT913X_ROLE_DUAL_SLAVE;
1529 it913x_pdata.role = IT913X_ROLE_SINGLE;
1532 request_module("%s", "it913x");
1533 pdev = platform_device_register_data(&d->intf->dev, name,
1534 PLATFORM_DEVID_AUTO,
1536 sizeof(it913x_pdata));
1537 if (IS_ERR(pdev) || !pdev->dev.driver) {
1541 if (!try_module_get(pdev->dev.driver->owner)) {
1542 platform_device_unregister(pdev);
1547 state->platform_device_tuner[adap->id] = pdev;
1563 dev_dbg(&intf->dev, "failed=%d\n", ret);
1568 static int it930x_tuner_attach(struct dvb_usb_adapter *adap)
1570 struct state *state = adap_to_priv(adap);
1571 struct dvb_usb_device *d = adap_to_d(adap);
1572 struct usb_interface *intf = d->intf;
1574 struct si2157_config si2157_config;
1576 dev_dbg(&intf->dev, "adap->id=%d\n", adap->id);
1578 /* I2C master bus 2 clock speed 300k */
1579 ret = af9035_wr_reg(d, 0x00f6a7, 0x07);
1583 /* I2C master bus 1,3 clock speed 300k */
1584 ret = af9035_wr_reg(d, 0x00f103, 0x07);
1588 /* set gpio11 low */
1589 ret = af9035_wr_reg_mask(d, 0xd8d4, 0x01, 0x01);
1593 ret = af9035_wr_reg_mask(d, 0xd8d5, 0x01, 0x01);
1597 ret = af9035_wr_reg_mask(d, 0xd8d3, 0x01, 0x01);
1601 /* Tuner enable using gpiot2_en, gpiot2_on and gpiot2_o (reset) */
1602 ret = af9035_wr_reg_mask(d, 0xd8b8, 0x01, 0x01);
1606 ret = af9035_wr_reg_mask(d, 0xd8b9, 0x01, 0x01);
1610 ret = af9035_wr_reg_mask(d, 0xd8b7, 0x00, 0x01);
1616 ret = af9035_wr_reg_mask(d, 0xd8b7, 0x01, 0x01);
1620 memset(&si2157_config, 0, sizeof(si2157_config));
1621 si2157_config.fe = adap->fe[0];
1622 si2157_config.if_port = 1;
1623 ret = af9035_add_i2c_dev(d, "si2157", 0x63,
1624 &si2157_config, state->i2c_adapter_demod);
1632 dev_dbg(&intf->dev, "failed=%d\n", ret);
1638 static int it930x_tuner_detach(struct dvb_usb_adapter *adap)
1640 struct state *state = adap_to_priv(adap);
1641 struct dvb_usb_device *d = adap_to_d(adap);
1642 struct usb_interface *intf = d->intf;
1644 dev_dbg(&intf->dev, "adap->id=%d\n", adap->id);
1646 if (adap->id == 1) {
1647 if (state->i2c_client[3])
1648 af9035_del_i2c_dev(d);
1649 } else if (adap->id == 0) {
1650 if (state->i2c_client[1])
1651 af9035_del_i2c_dev(d);
1658 static int af9035_tuner_detach(struct dvb_usb_adapter *adap)
1660 struct state *state = adap_to_priv(adap);
1661 struct dvb_usb_device *d = adap_to_d(adap);
1662 struct usb_interface *intf = d->intf;
1664 dev_dbg(&intf->dev, "adap->id=%d\n", adap->id);
1666 switch (state->af9033_config[adap->id].tuner) {
1667 case AF9033_TUNER_TUA9001:
1668 case AF9033_TUNER_FC2580:
1669 if (adap->id == 1) {
1670 if (state->i2c_client[3])
1671 af9035_del_i2c_dev(d);
1672 } else if (adap->id == 0) {
1673 if (state->i2c_client[1])
1674 af9035_del_i2c_dev(d);
1677 case AF9033_TUNER_IT9135_38:
1678 case AF9033_TUNER_IT9135_51:
1679 case AF9033_TUNER_IT9135_52:
1680 case AF9033_TUNER_IT9135_60:
1681 case AF9033_TUNER_IT9135_61:
1682 case AF9033_TUNER_IT9135_62:
1684 struct platform_device *pdev;
1686 pdev = state->platform_device_tuner[adap->id];
1688 module_put(pdev->dev.driver->owner);
1689 platform_device_unregister(pdev);
1698 static int af9035_init(struct dvb_usb_device *d)
1700 struct state *state = d_to_priv(d);
1701 struct usb_interface *intf = d->intf;
1703 u16 frame_size = (d->udev->speed == USB_SPEED_FULL ? 5 : 87) * 188 / 4;
1704 u8 packet_size = (d->udev->speed == USB_SPEED_FULL ? 64 : 512) / 4;
1705 struct reg_val_mask tab[] = {
1706 { 0x80f99d, 0x01, 0x01 },
1707 { 0x80f9a4, 0x01, 0x01 },
1708 { 0x00dd11, 0x00, 0x20 },
1709 { 0x00dd11, 0x00, 0x40 },
1710 { 0x00dd13, 0x00, 0x20 },
1711 { 0x00dd13, 0x00, 0x40 },
1712 { 0x00dd11, 0x20, 0x20 },
1713 { 0x00dd88, (frame_size >> 0) & 0xff, 0xff},
1714 { 0x00dd89, (frame_size >> 8) & 0xff, 0xff},
1715 { 0x00dd0c, packet_size, 0xff},
1716 { 0x00dd11, state->dual_mode << 6, 0x40 },
1717 { 0x00dd8a, (frame_size >> 0) & 0xff, 0xff},
1718 { 0x00dd8b, (frame_size >> 8) & 0xff, 0xff},
1719 { 0x00dd0d, packet_size, 0xff },
1720 { 0x80f9a3, state->dual_mode, 0x01 },
1721 { 0x80f9cd, state->dual_mode, 0x01 },
1722 { 0x80f99d, 0x00, 0x01 },
1723 { 0x80f9a4, 0x00, 0x01 },
1726 dev_dbg(&intf->dev, "USB speed=%d frame_size=%04x packet_size=%02x\n",
1727 d->udev->speed, frame_size, packet_size);
1729 /* init endpoints */
1730 for (i = 0; i < ARRAY_SIZE(tab); i++) {
1731 ret = af9035_wr_reg_mask(d, tab[i].reg, tab[i].val,
1740 dev_dbg(&intf->dev, "failed=%d\n", ret);
1745 static int it930x_init(struct dvb_usb_device *d)
1747 struct state *state = d_to_priv(d);
1748 struct usb_interface *intf = d->intf;
1750 u16 frame_size = (d->udev->speed == USB_SPEED_FULL ? 5 : 816) * 188 / 4;
1751 u8 packet_size = (d->udev->speed == USB_SPEED_FULL ? 64 : 512) / 4;
1752 struct reg_val_mask tab[] = {
1753 { 0x00da1a, 0x00, 0x01 }, /* ignore_sync_byte */
1754 { 0x00f41f, 0x04, 0x04 }, /* dvbt_inten */
1755 { 0x00da10, 0x00, 0x01 }, /* mpeg_full_speed */
1756 { 0x00f41a, 0x01, 0x01 }, /* dvbt_en */
1757 { 0x00da1d, 0x01, 0x01 }, /* mp2_sw_rst, reset EP4 */
1758 { 0x00dd11, 0x00, 0x20 }, /* ep4_tx_en, disable EP4 */
1759 { 0x00dd13, 0x00, 0x20 }, /* ep4_tx_nak, disable EP4 NAK */
1760 { 0x00dd11, 0x20, 0x20 }, /* ep4_tx_en, enable EP4 */
1761 { 0x00dd11, 0x00, 0x40 }, /* ep5_tx_en, disable EP5 */
1762 { 0x00dd13, 0x00, 0x40 }, /* ep5_tx_nak, disable EP5 NAK */
1763 { 0x00dd11, state->dual_mode << 6, 0x40 }, /* enable EP5 */
1764 { 0x00dd88, (frame_size >> 0) & 0xff, 0xff},
1765 { 0x00dd89, (frame_size >> 8) & 0xff, 0xff},
1766 { 0x00dd0c, packet_size, 0xff},
1767 { 0x00dd8a, (frame_size >> 0) & 0xff, 0xff},
1768 { 0x00dd8b, (frame_size >> 8) & 0xff, 0xff},
1769 { 0x00dd0d, packet_size, 0xff },
1770 { 0x00da1d, 0x00, 0x01 }, /* mp2_sw_rst, disable */
1771 { 0x00d833, 0x01, 0xff }, /* slew rate ctrl: slew rate boosts */
1772 { 0x00d830, 0x00, 0xff }, /* Bit 0 of output driving control */
1773 { 0x00d831, 0x01, 0xff }, /* Bit 1 of output driving control */
1774 { 0x00d832, 0x00, 0xff }, /* Bit 2 of output driving control */
1776 /* suspend gpio1 for TS-C */
1777 { 0x00d8b0, 0x01, 0xff }, /* gpio1 */
1778 { 0x00d8b1, 0x01, 0xff }, /* gpio1 */
1779 { 0x00d8af, 0x00, 0xff }, /* gpio1 */
1781 /* suspend gpio7 for TS-D */
1782 { 0x00d8c4, 0x01, 0xff }, /* gpio7 */
1783 { 0x00d8c5, 0x01, 0xff }, /* gpio7 */
1784 { 0x00d8c3, 0x00, 0xff }, /* gpio7 */
1786 /* suspend gpio13 for TS-B */
1787 { 0x00d8dc, 0x01, 0xff }, /* gpio13 */
1788 { 0x00d8dd, 0x01, 0xff }, /* gpio13 */
1789 { 0x00d8db, 0x00, 0xff }, /* gpio13 */
1791 /* suspend gpio14 for TS-E */
1792 { 0x00d8e4, 0x01, 0xff }, /* gpio14 */
1793 { 0x00d8e5, 0x01, 0xff }, /* gpio14 */
1794 { 0x00d8e3, 0x00, 0xff }, /* gpio14 */
1796 /* suspend gpio15 for TS-A */
1797 { 0x00d8e8, 0x01, 0xff }, /* gpio15 */
1798 { 0x00d8e9, 0x01, 0xff }, /* gpio15 */
1799 { 0x00d8e7, 0x00, 0xff }, /* gpio15 */
1801 { 0x00da58, 0x00, 0x01 }, /* ts_in_src, serial */
1802 { 0x00da73, 0x01, 0xff }, /* ts0_aggre_mode */
1803 { 0x00da78, 0x47, 0xff }, /* ts0_sync_byte */
1804 { 0x00da4c, 0x01, 0xff }, /* ts0_en */
1805 { 0x00da5a, 0x1f, 0xff }, /* ts_fail_ignore */
1808 dev_dbg(&intf->dev, "USB speed=%d frame_size=%04x packet_size=%02x\n",
1809 d->udev->speed, frame_size, packet_size);
1811 /* init endpoints */
1812 for (i = 0; i < ARRAY_SIZE(tab); i++) {
1813 ret = af9035_wr_reg_mask(d, tab[i].reg,
1814 tab[i].val, tab[i].mask);
1822 dev_dbg(&intf->dev, "failed=%d\n", ret);
1828 #if IS_ENABLED(CONFIG_RC_CORE)
1829 static int af9035_rc_query(struct dvb_usb_device *d)
1831 struct usb_interface *intf = d->intf;
1833 enum rc_proto proto;
1836 struct usb_req req = { CMD_IR_GET, 0, 0, NULL, 4, buf };
1838 ret = af9035_ctrl_msg(d, &req);
1844 if ((buf[2] + buf[3]) == 0xff) {
1845 if ((buf[0] + buf[1]) == 0xff) {
1846 /* NEC standard 16bit */
1847 key = RC_SCANCODE_NEC(buf[0], buf[2]);
1848 proto = RC_PROTO_NEC;
1850 /* NEC extended 24bit */
1851 key = RC_SCANCODE_NECX(buf[0] << 8 | buf[1], buf[2]);
1852 proto = RC_PROTO_NECX;
1855 /* NEC full code 32bit */
1856 key = RC_SCANCODE_NEC32(buf[0] << 24 | buf[1] << 16 |
1857 buf[2] << 8 | buf[3]);
1858 proto = RC_PROTO_NEC32;
1861 dev_dbg(&intf->dev, "%*ph\n", 4, buf);
1863 rc_keydown(d->rc_dev, proto, key, 0);
1868 dev_dbg(&intf->dev, "failed=%d\n", ret);
1873 static int af9035_get_rc_config(struct dvb_usb_device *d, struct dvb_usb_rc *rc)
1875 struct state *state = d_to_priv(d);
1876 struct usb_interface *intf = d->intf;
1878 dev_dbg(&intf->dev, "ir_mode=%02x ir_type=%02x\n",
1879 state->ir_mode, state->ir_type);
1881 /* don't activate rc if in HID mode or if not available */
1882 if (state->ir_mode == 0x05) {
1883 switch (state->ir_type) {
1886 rc->allowed_protos = RC_PROTO_BIT_NEC |
1887 RC_PROTO_BIT_NECX | RC_PROTO_BIT_NEC32;
1890 rc->allowed_protos = RC_PROTO_BIT_RC6_MCE;
1894 rc->query = af9035_rc_query;
1897 /* load empty to enable rc */
1899 rc->map_name = RC_MAP_EMPTY;
1905 #define af9035_get_rc_config NULL
1908 static int af9035_get_stream_config(struct dvb_frontend *fe, u8 *ts_type,
1909 struct usb_data_stream_properties *stream)
1911 struct dvb_usb_device *d = fe_to_d(fe);
1912 struct usb_interface *intf = d->intf;
1914 dev_dbg(&intf->dev, "adap=%d\n", fe_to_adap(fe)->id);
1916 if (d->udev->speed == USB_SPEED_FULL)
1917 stream->u.bulk.buffersize = 5 * 188;
1922 static int af9035_pid_filter_ctrl(struct dvb_usb_adapter *adap, int onoff)
1924 struct state *state = adap_to_priv(adap);
1926 return state->ops.pid_filter_ctrl(adap->fe[0], onoff);
1929 static int af9035_pid_filter(struct dvb_usb_adapter *adap, int index, u16 pid,
1932 struct state *state = adap_to_priv(adap);
1934 return state->ops.pid_filter(adap->fe[0], index, pid, onoff);
1937 static int af9035_probe(struct usb_interface *intf,
1938 const struct usb_device_id *id)
1940 struct usb_device *udev = interface_to_usbdev(intf);
1941 char manufacturer[sizeof("Afatech")];
1943 memset(manufacturer, 0, sizeof(manufacturer));
1944 usb_string(udev, udev->descriptor.iManufacturer,
1945 manufacturer, sizeof(manufacturer));
1947 * There is two devices having same ID but different chipset. One uses
1948 * AF9015 and the other IT9135 chipset. Only difference seen on lsusb
1949 * is iManufacturer string.
1951 * idVendor 0x0ccd TerraTec Electronic GmbH
1954 * iManufacturer 1 Afatech
1955 * iProduct 2 DVB-T 2
1957 * idVendor 0x0ccd TerraTec Electronic GmbH
1960 * iManufacturer 1 ITE Technologies, Inc.
1961 * iProduct 2 DVB-T TV Stick
1963 if ((le16_to_cpu(udev->descriptor.idVendor) == USB_VID_TERRATEC) &&
1964 (le16_to_cpu(udev->descriptor.idProduct) == 0x0099)) {
1965 if (!strcmp("Afatech", manufacturer)) {
1966 dev_dbg(&udev->dev, "rejecting device\n");
1971 return dvb_usbv2_probe(intf, id);
1974 /* interface 0 is used by DVB-T receiver and
1975 interface 1 is for remote controller (HID) */
1976 static const struct dvb_usb_device_properties af9035_props = {
1977 .driver_name = KBUILD_MODNAME,
1978 .owner = THIS_MODULE,
1979 .adapter_nr = adapter_nr,
1980 .size_of_priv = sizeof(struct state),
1982 .generic_bulk_ctrl_endpoint = 0x02,
1983 .generic_bulk_ctrl_endpoint_response = 0x81,
1985 .identify_state = af9035_identify_state,
1986 .download_firmware = af9035_download_firmware,
1988 .i2c_algo = &af9035_i2c_algo,
1989 .read_config = af9035_read_config,
1990 .frontend_attach = af9035_frontend_attach,
1991 .frontend_detach = af9035_frontend_detach,
1992 .tuner_attach = af9035_tuner_attach,
1993 .tuner_detach = af9035_tuner_detach,
1994 .init = af9035_init,
1995 .get_rc_config = af9035_get_rc_config,
1996 .get_stream_config = af9035_get_stream_config,
1998 .get_adapter_count = af9035_get_adapter_count,
2001 .caps = DVB_USB_ADAP_HAS_PID_FILTER |
2002 DVB_USB_ADAP_PID_FILTER_CAN_BE_TURNED_OFF,
2004 .pid_filter_count = 32,
2005 .pid_filter_ctrl = af9035_pid_filter_ctrl,
2006 .pid_filter = af9035_pid_filter,
2008 .stream = DVB_USB_STREAM_BULK(0x84, 6, 87 * 188),
2010 .caps = DVB_USB_ADAP_HAS_PID_FILTER |
2011 DVB_USB_ADAP_PID_FILTER_CAN_BE_TURNED_OFF,
2013 .pid_filter_count = 32,
2014 .pid_filter_ctrl = af9035_pid_filter_ctrl,
2015 .pid_filter = af9035_pid_filter,
2017 .stream = DVB_USB_STREAM_BULK(0x85, 6, 87 * 188),
2022 static const struct dvb_usb_device_properties it930x_props = {
2023 .driver_name = KBUILD_MODNAME,
2024 .owner = THIS_MODULE,
2025 .adapter_nr = adapter_nr,
2026 .size_of_priv = sizeof(struct state),
2028 .generic_bulk_ctrl_endpoint = 0x02,
2029 .generic_bulk_ctrl_endpoint_response = 0x81,
2031 .identify_state = af9035_identify_state,
2032 .download_firmware = af9035_download_firmware,
2034 .i2c_algo = &af9035_i2c_algo,
2035 .read_config = af9035_read_config,
2036 .frontend_attach = it930x_frontend_attach,
2037 .frontend_detach = af9035_frontend_detach,
2038 .tuner_attach = it930x_tuner_attach,
2039 .tuner_detach = it930x_tuner_detach,
2040 .init = it930x_init,
2041 .get_stream_config = af9035_get_stream_config,
2043 .get_adapter_count = af9035_get_adapter_count,
2046 .stream = DVB_USB_STREAM_BULK(0x84, 4, 816 * 188),
2048 .stream = DVB_USB_STREAM_BULK(0x85, 4, 816 * 188),
2053 static const struct usb_device_id af9035_id_table[] = {
2054 /* AF9035 devices */
2055 { DVB_USB_DEVICE(USB_VID_AFATECH, USB_PID_AFATECH_AF9035_9035,
2056 &af9035_props, "Afatech AF9035 reference design", NULL) },
2057 { DVB_USB_DEVICE(USB_VID_AFATECH, USB_PID_AFATECH_AF9035_1000,
2058 &af9035_props, "Afatech AF9035 reference design", NULL) },
2059 { DVB_USB_DEVICE(USB_VID_AFATECH, USB_PID_AFATECH_AF9035_1001,
2060 &af9035_props, "Afatech AF9035 reference design", NULL) },
2061 { DVB_USB_DEVICE(USB_VID_AFATECH, USB_PID_AFATECH_AF9035_1002,
2062 &af9035_props, "Afatech AF9035 reference design", NULL) },
2063 { DVB_USB_DEVICE(USB_VID_AFATECH, USB_PID_AFATECH_AF9035_1003,
2064 &af9035_props, "Afatech AF9035 reference design", NULL) },
2065 { DVB_USB_DEVICE(USB_VID_TERRATEC, USB_PID_TERRATEC_CINERGY_T_STICK,
2066 &af9035_props, "TerraTec Cinergy T Stick", NULL) },
2067 { DVB_USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_A835,
2068 &af9035_props, "AVerMedia AVerTV Volar HD/PRO (A835)", NULL) },
2069 { DVB_USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_B835,
2070 &af9035_props, "AVerMedia AVerTV Volar HD/PRO (A835)", NULL) },
2071 { DVB_USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_1867,
2072 &af9035_props, "AVerMedia HD Volar (A867)", NULL) },
2073 { DVB_USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_A867,
2074 &af9035_props, "AVerMedia HD Volar (A867)", NULL) },
2075 { DVB_USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_TWINSTAR,
2076 &af9035_props, "AVerMedia Twinstar (A825)", NULL) },
2077 { DVB_USB_DEVICE(USB_VID_ASUS, USB_PID_ASUS_U3100MINI_PLUS,
2078 &af9035_props, "Asus U3100Mini Plus", NULL) },
2079 { DVB_USB_DEVICE(USB_VID_TERRATEC, 0x00aa,
2080 &af9035_props, "TerraTec Cinergy T Stick (rev. 2)", NULL) },
2081 { DVB_USB_DEVICE(USB_VID_AVERMEDIA, 0x0337,
2082 &af9035_props, "AVerMedia HD Volar (A867)", NULL) },
2083 { DVB_USB_DEVICE(USB_VID_GTEK, USB_PID_EVOLVEO_XTRATV_STICK,
2084 &af9035_props, "EVOLVEO XtraTV stick", NULL) },
2086 /* IT9135 devices */
2087 { DVB_USB_DEVICE(USB_VID_ITETECH, USB_PID_ITETECH_IT9135,
2088 &af9035_props, "ITE 9135 Generic", RC_MAP_IT913X_V1) },
2089 { DVB_USB_DEVICE(USB_VID_ITETECH, USB_PID_ITETECH_IT9135_9005,
2090 &af9035_props, "ITE 9135(9005) Generic", RC_MAP_IT913X_V2) },
2091 { DVB_USB_DEVICE(USB_VID_ITETECH, USB_PID_ITETECH_IT9135_9006,
2092 &af9035_props, "ITE 9135(9006) Generic", RC_MAP_IT913X_V1) },
2093 { DVB_USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_A835B_1835,
2094 &af9035_props, "Avermedia A835B(1835)", RC_MAP_IT913X_V2) },
2095 { DVB_USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_A835B_2835,
2096 &af9035_props, "Avermedia A835B(2835)", RC_MAP_IT913X_V2) },
2097 { DVB_USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_A835B_3835,
2098 &af9035_props, "Avermedia A835B(3835)", RC_MAP_IT913X_V2) },
2099 { DVB_USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_A835B_4835,
2100 &af9035_props, "Avermedia A835B(4835)", RC_MAP_IT913X_V2) },
2101 { DVB_USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_TD110,
2102 &af9035_props, "Avermedia AverTV Volar HD 2 (TD110)", RC_MAP_AVERMEDIA_RM_KS) },
2103 { DVB_USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_H335,
2104 &af9035_props, "Avermedia H335", RC_MAP_IT913X_V2) },
2105 { DVB_USB_DEVICE(USB_VID_KWORLD_2, USB_PID_KWORLD_UB499_2T_T09,
2106 &af9035_props, "Kworld UB499-2T T09", RC_MAP_IT913X_V1) },
2107 { DVB_USB_DEVICE(USB_VID_KWORLD_2, USB_PID_SVEON_STV22_IT9137,
2108 &af9035_props, "Sveon STV22 Dual DVB-T HDTV",
2109 RC_MAP_IT913X_V1) },
2110 { DVB_USB_DEVICE(USB_VID_KWORLD_2, USB_PID_CTVDIGDUAL_V2,
2111 &af9035_props, "Digital Dual TV Receiver CTVDIGDUAL_V2",
2112 RC_MAP_IT913X_V1) },
2113 { DVB_USB_DEVICE(USB_VID_TERRATEC, USB_PID_TERRATEC_T1,
2114 &af9035_props, "TerraTec T1", RC_MAP_IT913X_V1) },
2115 /* XXX: that same ID [0ccd:0099] is used by af9015 driver too */
2116 { DVB_USB_DEVICE(USB_VID_TERRATEC, 0x0099,
2117 &af9035_props, "TerraTec Cinergy T Stick Dual RC (rev. 2)",
2119 { DVB_USB_DEVICE(USB_VID_LEADTEK, 0x6a05,
2120 &af9035_props, "Leadtek WinFast DTV Dongle Dual", NULL) },
2121 { DVB_USB_DEVICE(USB_VID_HAUPPAUGE, 0xf900,
2122 &af9035_props, "Hauppauge WinTV-MiniStick 2", NULL) },
2123 { DVB_USB_DEVICE(USB_VID_PCTV, USB_PID_PCTV_78E,
2124 &af9035_props, "PCTV AndroiDTV (78e)", RC_MAP_IT913X_V1) },
2125 { DVB_USB_DEVICE(USB_VID_PCTV, USB_PID_PCTV_79E,
2126 &af9035_props, "PCTV microStick (79e)", RC_MAP_IT913X_V2) },
2128 /* IT930x devices */
2129 { DVB_USB_DEVICE(USB_VID_ITETECH, USB_PID_ITETECH_IT9303,
2130 &it930x_props, "ITE 9303 Generic", NULL) },
2133 MODULE_DEVICE_TABLE(usb, af9035_id_table);
2135 static struct usb_driver af9035_usb_driver = {
2136 .name = KBUILD_MODNAME,
2137 .id_table = af9035_id_table,
2138 .probe = af9035_probe,
2139 .disconnect = dvb_usbv2_disconnect,
2140 .suspend = dvb_usbv2_suspend,
2141 .resume = dvb_usbv2_resume,
2142 .reset_resume = dvb_usbv2_reset_resume,
2147 module_usb_driver(af9035_usb_driver);
2149 MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
2150 MODULE_DESCRIPTION("Afatech AF9035 driver");
2151 MODULE_LICENSE("GPL");