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);
53 u16 checksum, tmp_checksum;
55 mutex_lock(&d->usb_mutex);
57 /* buffer overflow check */
58 if (req->wlen > (BUF_LEN - REQ_HDR_LEN - CHECKSUM_LEN) ||
59 req->rlen > (BUF_LEN - ACK_HDR_LEN - CHECKSUM_LEN)) {
60 dev_err(&d->udev->dev, "%s: too much data wlen=%d rlen=%d\n",
61 KBUILD_MODNAME, req->wlen, req->rlen);
66 state->buf[0] = REQ_HDR_LEN + req->wlen + CHECKSUM_LEN - 1;
67 state->buf[1] = req->mbox;
68 state->buf[2] = req->cmd;
69 state->buf[3] = state->seq++;
70 memcpy(&state->buf[REQ_HDR_LEN], req->wbuf, req->wlen);
72 wlen = REQ_HDR_LEN + req->wlen + CHECKSUM_LEN;
73 rlen = ACK_HDR_LEN + req->rlen + CHECKSUM_LEN;
75 /* calc and add checksum */
76 checksum = af9035_checksum(state->buf, state->buf[0] - 1);
77 state->buf[state->buf[0] - 1] = (checksum >> 8);
78 state->buf[state->buf[0] - 0] = (checksum & 0xff);
80 /* no ack for these packets */
81 if (req->cmd == CMD_FW_DL)
84 ret = dvb_usbv2_generic_rw_locked(d,
85 state->buf, wlen, state->buf, rlen);
89 /* no ack for those packets */
90 if (req->cmd == CMD_FW_DL)
94 checksum = af9035_checksum(state->buf, rlen - 2);
95 tmp_checksum = (state->buf[rlen - 2] << 8) | state->buf[rlen - 1];
96 if (tmp_checksum != checksum) {
97 dev_err(&d->udev->dev,
98 "%s: command=%02x checksum mismatch (%04x != %04x)\n",
99 KBUILD_MODNAME, req->cmd, tmp_checksum,
107 /* fw returns status 1 when IR code was not received */
108 if (req->cmd == CMD_IR_GET || state->buf[2] == 1) {
113 dev_dbg(&d->udev->dev, "%s: command=%02x failed fw error=%d\n",
114 __func__, req->cmd, state->buf[2]);
119 /* read request, copy returned data to return buf */
121 memcpy(req->rbuf, &state->buf[ACK_HDR_LEN], req->rlen);
123 mutex_unlock(&d->usb_mutex);
125 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
129 /* write multiple registers */
130 static int af9035_wr_regs(struct dvb_usb_device *d, u32 reg, u8 *val, int len)
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(&d->udev->dev, "%s: i2c wr: len=%d is too big!\n",
138 KBUILD_MODNAME, len);
146 wbuf[4] = (reg >> 8) & 0xff;
147 wbuf[5] = (reg >> 0) & 0xff;
148 memcpy(&wbuf[6], val, len);
150 return af9035_ctrl_msg(d, &req);
153 /* read multiple registers */
154 static int af9035_rd_regs(struct dvb_usb_device *d, u32 reg, u8 *val, int len)
156 u8 wbuf[] = { len, 2, 0, 0, (reg >> 8) & 0xff, reg & 0xff };
157 u8 mbox = (reg >> 16) & 0xff;
158 struct usb_req req = { CMD_MEM_RD, mbox, sizeof(wbuf), wbuf, len, val };
160 return af9035_ctrl_msg(d, &req);
163 /* write single register */
164 static int af9035_wr_reg(struct dvb_usb_device *d, u32 reg, u8 val)
166 return af9035_wr_regs(d, reg, &val, 1);
169 /* read single register */
170 static int af9035_rd_reg(struct dvb_usb_device *d, u32 reg, u8 *val)
172 return af9035_rd_regs(d, reg, val, 1);
175 /* write single register with mask */
176 static int af9035_wr_reg_mask(struct dvb_usb_device *d, u32 reg, u8 val,
182 /* no need for read if whole reg is written */
184 ret = af9035_rd_regs(d, reg, &tmp, 1);
193 return af9035_wr_regs(d, reg, &val, 1);
196 static int af9035_add_i2c_dev(struct dvb_usb_device *d, const char *type,
197 u8 addr, void *platform_data, struct i2c_adapter *adapter)
200 struct state *state = d_to_priv(d);
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(&d->udev->dev, "%s: num=%d\n", __func__, num);
217 if (num == AF9035_I2C_CLIENT_MAX) {
218 dev_err(&d->udev->dev, "%s: I2C client out of index\n",
224 request_module("%s", board_info.type);
226 /* register I2C device */
227 client = i2c_new_device(adapter, &board_info);
228 if (client == NULL || client->dev.driver == NULL) {
233 /* increase I2C driver usage count */
234 if (!try_module_get(client->dev.driver->owner)) {
235 i2c_unregister_device(client);
240 state->i2c_client[num] = client;
243 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
247 static void af9035_del_i2c_dev(struct dvb_usb_device *d)
250 struct state *state = d_to_priv(d);
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(&d->udev->dev, "%s: num=%d\n", __func__, num);
263 dev_err(&d->udev->dev, "%s: I2C client out of index\n",
268 client = state->i2c_client[num];
270 /* decrease I2C driver usage count */
271 module_put(client->dev.driver->owner);
273 /* unregister I2C device */
274 i2c_unregister_device(client);
276 state->i2c_client[num] = NULL;
279 dev_dbg(&d->udev->dev, "%s: failed\n", __func__);
282 static int af9035_i2c_master_xfer(struct i2c_adapter *adap,
283 struct i2c_msg msg[], int num)
285 struct dvb_usb_device *d = i2c_get_adapdata(adap);
286 struct state *state = d_to_priv(d);
289 if (mutex_lock_interruptible(&d->i2c_mutex) < 0)
293 * AF9035 I2C sub header is 5 bytes long. Meaning of those bytes are:
297 * byte 3 and 4 can be used as reg addr
299 * used when reg addr len is set to 2
301 * used when reg addr len is set to 1 or 2
303 * For the simplify we do not use register addr at all.
304 * NOTE: As a firmware knows tuner type there is very small possibility
305 * there could be some tuner I2C hacks done by firmware and this may
306 * lead problems if firmware expects those bytes are used.
308 * TODO: Here is few hacks. AF9035 chip integrates AF9033 demodulator.
309 * IT9135 chip integrates AF9033 demodulator and RF tuner. For dual
310 * tuner devices, there is also external AF9033 demodulator connected
311 * via external I2C bus. All AF9033 demod I2C traffic, both single and
312 * dual tuner configuration, is covered by firmware - actual USB IO
313 * looks just like a memory access.
314 * In case of IT913x chip, there is own tuner driver. It is implemented
315 * currently as a I2C driver, even tuner IP block is likely build
316 * directly into the demodulator memory space and there is no own I2C
317 * bus. I2C subsystem does not allow register multiple devices to same
318 * bus, having same slave address. Due to that we reuse demod address,
319 * shifted by one bit, on that case.
321 * For IT930x we use a different command and the sub header is
324 * 1: I2C bus (0x03 seems to be only value used)
327 #define AF9035_IS_I2C_XFER_WRITE_READ(_msg, _num) \
328 (_num == 2 && !(_msg[0].flags & I2C_M_RD) && (_msg[1].flags & I2C_M_RD))
329 #define AF9035_IS_I2C_XFER_WRITE(_msg, _num) \
330 (_num == 1 && !(_msg[0].flags & I2C_M_RD))
331 #define AF9035_IS_I2C_XFER_READ(_msg, _num) \
332 (_num == 1 && (_msg[0].flags & I2C_M_RD))
334 if (AF9035_IS_I2C_XFER_WRITE_READ(msg, num)) {
335 if (msg[0].len > 40 || msg[1].len > 40) {
336 /* TODO: correct limits > 40 */
338 } else if ((msg[0].addr == state->af9033_i2c_addr[0]) ||
339 (msg[0].addr == state->af9033_i2c_addr[1]) ||
340 (state->chip_type == 0x9135)) {
341 /* demod access via firmware interface */
342 u32 reg = msg[0].buf[0] << 16 | msg[0].buf[1] << 8 |
345 if (msg[0].addr == state->af9033_i2c_addr[1] ||
346 msg[0].addr == (state->af9033_i2c_addr[1] >> 1))
349 ret = af9035_rd_regs(d, reg, &msg[1].buf[0],
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[2] = 0x00; /* reg addr len */
371 buf[3] = 0x00; /* reg addr MSB */
372 buf[4] = 0x00; /* reg addr LSB */
373 memcpy(&buf[5], msg[0].buf, msg[0].len);
375 ret = af9035_ctrl_msg(d, &req);
377 } else if (AF9035_IS_I2C_XFER_WRITE(msg, num)) {
378 if (msg[0].len > 40) {
379 /* TODO: correct limits > 40 */
381 } else if ((msg[0].addr == state->af9033_i2c_addr[0]) ||
382 (msg[0].addr == state->af9033_i2c_addr[1]) ||
383 (state->chip_type == 0x9135)) {
384 /* demod access via firmware interface */
385 u32 reg = msg[0].buf[0] << 16 | msg[0].buf[1] << 8 |
388 if (msg[0].addr == state->af9033_i2c_addr[1] ||
389 msg[0].addr == (state->af9033_i2c_addr[1] >> 1))
392 ret = af9035_wr_regs(d, reg, &msg[0].buf[3],
396 u8 buf[MAX_XFER_SIZE];
397 struct usb_req req = { CMD_I2C_WR, 0, 5 + msg[0].len,
400 if (state->chip_type == 0x9306) {
401 req.cmd = CMD_GENERIC_I2C_WR;
402 req.wlen = 3 + msg[0].len;
405 req.mbox |= ((msg[0].addr & 0x80) >> 3);
407 if (state->chip_type == 0x9306) {
408 buf[1] = 0x03; /* I2C bus */
409 buf[2] = msg[0].addr << 1;
410 memcpy(&buf[3], msg[0].buf, msg[0].len);
412 buf[1] = msg[0].addr << 1;
413 buf[2] = 0x00; /* reg addr len */
414 buf[3] = 0x00; /* reg addr MSB */
415 buf[4] = 0x00; /* reg addr LSB */
416 memcpy(&buf[5], msg[0].buf, msg[0].len);
418 ret = af9035_ctrl_msg(d, &req);
420 } else if (AF9035_IS_I2C_XFER_READ(msg, num)) {
421 if (msg[0].len > 40) {
422 /* TODO: correct limits > 40 */
427 struct usb_req req = { CMD_I2C_RD, 0, sizeof(buf),
428 buf, msg[0].len, msg[0].buf };
430 if (state->chip_type == 0x9306) {
431 req.cmd = CMD_GENERIC_I2C_RD;
434 req.mbox |= ((msg[0].addr & 0x80) >> 3);
436 if (state->chip_type == 0x9306) {
437 buf[1] = 0x03; /* I2C bus */
438 buf[2] = msg[0].addr << 1;
440 buf[1] = msg[0].addr << 1;
441 buf[2] = 0x00; /* reg addr len */
442 buf[3] = 0x00; /* reg addr MSB */
443 buf[4] = 0x00; /* reg addr LSB */
445 ret = af9035_ctrl_msg(d, &req);
449 * We support only three kind of I2C transactions:
450 * 1) 1 x write + 1 x read (repeated start)
457 mutex_unlock(&d->i2c_mutex);
465 static u32 af9035_i2c_functionality(struct i2c_adapter *adapter)
470 static struct i2c_algorithm af9035_i2c_algo = {
471 .master_xfer = af9035_i2c_master_xfer,
472 .functionality = af9035_i2c_functionality,
475 static int af9035_identify_state(struct dvb_usb_device *d, const char **name)
477 struct state *state = d_to_priv(d);
481 struct usb_req req = { CMD_FW_QUERYINFO, 0, sizeof(wbuf), wbuf,
482 sizeof(rbuf), rbuf };
484 ret = af9035_rd_regs(d, 0x1222, rbuf, 3);
488 state->chip_version = rbuf[0];
489 state->chip_type = rbuf[2] << 8 | rbuf[1] << 0;
491 ret = af9035_rd_reg(d, 0x384f, &state->prechip_version);
495 dev_info(&d->udev->dev,
496 "%s: prechip_version=%02x chip_version=%02x chip_type=%04x\n",
497 KBUILD_MODNAME, state->prechip_version,
498 state->chip_version, state->chip_type);
500 if (state->chip_type == 0x9135) {
501 if (state->chip_version == 0x02)
502 *name = AF9035_FIRMWARE_IT9135_V2;
504 *name = AF9035_FIRMWARE_IT9135_V1;
505 state->eeprom_addr = EEPROM_BASE_IT9135;
506 } else if (state->chip_type == 0x9306) {
507 *name = AF9035_FIRMWARE_IT9303;
508 state->eeprom_addr = EEPROM_BASE_IT9135;
510 *name = AF9035_FIRMWARE_AF9035;
511 state->eeprom_addr = EEPROM_BASE_AF9035;
514 ret = af9035_ctrl_msg(d, &req);
518 dev_dbg(&d->udev->dev, "%s: reply=%*ph\n", __func__, 4, rbuf);
519 if (rbuf[0] || rbuf[1] || rbuf[2] || rbuf[3])
527 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
532 static int af9035_download_firmware_old(struct dvb_usb_device *d,
533 const struct firmware *fw)
537 struct usb_req req = { 0, 0, 0, NULL, 0, NULL };
538 struct usb_req req_fw_dl = { CMD_FW_DL, 0, 0, wbuf, 0, NULL };
540 u16 hdr_addr, hdr_data_len, hdr_checksum;
545 * Thanks to Daniel Glöckner <daniel-gl@gmx.net> about that info!
547 * byte 0: MCS 51 core
548 * There are two inside the AF9035 (1=Link and 2=OFDM) with separate
550 * byte 1-2: Big endian destination address
551 * byte 3-4: Big endian number of data bytes following the header
552 * byte 5-6: Big endian header checksum, apparently ignored by the chip
553 * Calculated as ~(h[0]*256+h[1]+h[2]*256+h[3]+h[4]*256)
556 for (i = fw->size; i > HDR_SIZE;) {
557 hdr_core = fw->data[fw->size - i + 0];
558 hdr_addr = fw->data[fw->size - i + 1] << 8;
559 hdr_addr |= fw->data[fw->size - i + 2] << 0;
560 hdr_data_len = fw->data[fw->size - i + 3] << 8;
561 hdr_data_len |= fw->data[fw->size - i + 4] << 0;
562 hdr_checksum = fw->data[fw->size - i + 5] << 8;
563 hdr_checksum |= fw->data[fw->size - i + 6] << 0;
565 dev_dbg(&d->udev->dev,
566 "%s: core=%d addr=%04x data_len=%d checksum=%04x\n",
567 __func__, hdr_core, hdr_addr, hdr_data_len,
570 if (((hdr_core != 1) && (hdr_core != 2)) ||
571 (hdr_data_len > i)) {
572 dev_dbg(&d->udev->dev, "%s: bad firmware\n", __func__);
576 /* download begin packet */
577 req.cmd = CMD_FW_DL_BEGIN;
578 ret = af9035_ctrl_msg(d, &req);
582 /* download firmware packet(s) */
583 for (j = HDR_SIZE + hdr_data_len; j > 0; j -= MAX_DATA) {
587 req_fw_dl.wlen = len;
588 req_fw_dl.wbuf = (u8 *) &fw->data[fw->size - i +
589 HDR_SIZE + hdr_data_len - j];
590 ret = af9035_ctrl_msg(d, &req_fw_dl);
595 /* download end packet */
596 req.cmd = CMD_FW_DL_END;
597 ret = af9035_ctrl_msg(d, &req);
601 i -= hdr_data_len + HDR_SIZE;
603 dev_dbg(&d->udev->dev, "%s: data uploaded=%zu\n",
604 __func__, fw->size - i);
607 /* print warn if firmware is bad, continue and see what happens */
609 dev_warn(&d->udev->dev, "%s: bad firmware\n", KBUILD_MODNAME);
614 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
619 static int af9035_download_firmware_new(struct dvb_usb_device *d,
620 const struct firmware *fw)
623 struct usb_req req_fw_dl = { CMD_FW_SCATTER_WR, 0, 0, NULL, 0, NULL };
627 * There seems to be following firmware header. Meaning of bytes 0-3
636 * 6: count of data bytes ?
638 for (i = HDR_SIZE, i_prev = 0; i <= fw->size; i++) {
640 (fw->data[i + 0] == 0x03 &&
641 (fw->data[i + 1] == 0x00 ||
642 fw->data[i + 1] == 0x01) &&
643 fw->data[i + 2] == 0x00)) {
644 req_fw_dl.wlen = i - i_prev;
645 req_fw_dl.wbuf = (u8 *) &fw->data[i_prev];
647 ret = af9035_ctrl_msg(d, &req_fw_dl);
651 dev_dbg(&d->udev->dev, "%s: data uploaded=%d\n",
659 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
664 static int af9035_download_firmware(struct dvb_usb_device *d,
665 const struct firmware *fw)
667 struct state *state = d_to_priv(d);
672 struct usb_req req = { 0, 0, 0, NULL, 0, NULL };
673 struct usb_req req_fw_ver = { CMD_FW_QUERYINFO, 0, 1, wbuf, 4, rbuf };
675 dev_dbg(&d->udev->dev, "%s:\n", __func__);
678 * In case of dual tuner configuration we need to do some extra
679 * initialization in order to download firmware to slave demod too,
680 * which is done by master demod.
681 * Master feeds also clock and controls power via GPIO.
683 ret = af9035_rd_reg(d, state->eeprom_addr + EEPROM_TS_MODE, &tmp);
687 if (tmp == 1 || tmp == 3) {
688 /* configure gpioh1, reset & power slave demod */
689 ret = af9035_wr_reg_mask(d, 0x00d8b0, 0x01, 0x01);
693 ret = af9035_wr_reg_mask(d, 0x00d8b1, 0x01, 0x01);
697 ret = af9035_wr_reg_mask(d, 0x00d8af, 0x00, 0x01);
701 usleep_range(10000, 50000);
703 ret = af9035_wr_reg_mask(d, 0x00d8af, 0x01, 0x01);
707 /* tell the slave I2C address */
708 ret = af9035_rd_reg(d,
709 state->eeprom_addr + EEPROM_2ND_DEMOD_ADDR,
714 /* use default I2C address if eeprom has no address set */
718 if ((state->chip_type == 0x9135) ||
719 (state->chip_type == 0x9306)) {
720 ret = af9035_wr_reg(d, 0x004bfb, tmp);
724 ret = af9035_wr_reg(d, 0x00417f, tmp);
728 /* enable clock out */
729 ret = af9035_wr_reg_mask(d, 0x00d81a, 0x01, 0x01);
735 if (fw->data[0] == 0x01)
736 ret = af9035_download_firmware_old(d, fw);
738 ret = af9035_download_firmware_new(d, fw);
742 /* firmware loaded, request boot */
743 req.cmd = CMD_FW_BOOT;
744 ret = af9035_ctrl_msg(d, &req);
748 /* ensure firmware starts */
750 ret = af9035_ctrl_msg(d, &req_fw_ver);
754 if (!(rbuf[0] || rbuf[1] || rbuf[2] || rbuf[3])) {
755 dev_err(&d->udev->dev, "%s: firmware did not run\n",
761 dev_info(&d->udev->dev, "%s: firmware version=%d.%d.%d.%d",
762 KBUILD_MODNAME, rbuf[0], rbuf[1], rbuf[2], rbuf[3]);
767 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
772 static int af9035_read_config(struct dvb_usb_device *d)
774 struct state *state = d_to_priv(d);
779 /* demod I2C "address" */
780 state->af9033_i2c_addr[0] = 0x38;
781 state->af9033_i2c_addr[1] = 0x3a;
782 state->af9033_config[0].adc_multiplier = AF9033_ADC_MULTIPLIER_2X;
783 state->af9033_config[1].adc_multiplier = AF9033_ADC_MULTIPLIER_2X;
784 state->af9033_config[0].ts_mode = AF9033_TS_MODE_USB;
785 state->af9033_config[1].ts_mode = AF9033_TS_MODE_SERIAL;
787 if (state->chip_type == 0x9135) {
788 /* feed clock for integrated RF tuner */
789 state->af9033_config[0].dyn0_clk = true;
790 state->af9033_config[1].dyn0_clk = true;
792 if (state->chip_version == 0x02) {
793 state->af9033_config[0].tuner = AF9033_TUNER_IT9135_60;
794 state->af9033_config[1].tuner = AF9033_TUNER_IT9135_60;
795 tmp16 = 0x00461d; /* eeprom memory mapped location */
797 state->af9033_config[0].tuner = AF9033_TUNER_IT9135_38;
798 state->af9033_config[1].tuner = AF9033_TUNER_IT9135_38;
799 tmp16 = 0x00461b; /* eeprom memory mapped location */
802 /* check if eeprom exists */
803 ret = af9035_rd_reg(d, tmp16, &tmp);
808 dev_dbg(&d->udev->dev, "%s: no eeprom\n", __func__);
811 } else if (state->chip_type == 0x9306) {
813 * IT930x is an USB bridge, only single demod-single tuner
814 * configurations seen so far.
821 /* check if there is dual tuners */
822 ret = af9035_rd_reg(d, state->eeprom_addr + EEPROM_TS_MODE, &tmp);
826 if (tmp == 1 || tmp == 3)
827 state->dual_mode = true;
829 dev_dbg(&d->udev->dev, "%s: ts mode=%d dual mode=%d\n", __func__,
830 tmp, state->dual_mode);
832 if (state->dual_mode) {
833 /* read 2nd demodulator I2C address */
834 ret = af9035_rd_reg(d,
835 state->eeprom_addr + EEPROM_2ND_DEMOD_ADDR,
841 state->af9033_i2c_addr[1] = tmp;
843 dev_dbg(&d->udev->dev, "%s: 2nd demod I2C addr=%02x\n",
847 addr = state->eeprom_addr;
849 for (i = 0; i < state->dual_mode + 1; i++) {
851 ret = af9035_rd_reg(d, addr + EEPROM_1_TUNER_ID, &tmp);
855 dev_dbg(&d->udev->dev, "%s: [%d]tuner=%02x\n",
858 /* tuner sanity check */
859 if (state->chip_type == 0x9135) {
860 if (state->chip_version == 0x02) {
863 case AF9033_TUNER_IT9135_60:
864 case AF9033_TUNER_IT9135_61:
865 case AF9033_TUNER_IT9135_62:
866 state->af9033_config[i].tuner = tmp;
872 case AF9033_TUNER_IT9135_38:
873 case AF9033_TUNER_IT9135_51:
874 case AF9033_TUNER_IT9135_52:
875 state->af9033_config[i].tuner = tmp;
881 state->af9033_config[i].tuner = tmp;
884 if (state->af9033_config[i].tuner != tmp) {
885 dev_info(&d->udev->dev,
886 "%s: [%d] overriding tuner from %02x to %02x\n",
887 KBUILD_MODNAME, i, tmp,
888 state->af9033_config[i].tuner);
891 switch (state->af9033_config[i].tuner) {
892 case AF9033_TUNER_TUA9001:
893 case AF9033_TUNER_FC0011:
894 case AF9033_TUNER_MXL5007T:
895 case AF9033_TUNER_TDA18218:
896 case AF9033_TUNER_FC2580:
897 case AF9033_TUNER_FC0012:
898 state->af9033_config[i].spec_inv = 1;
900 case AF9033_TUNER_IT9135_38:
901 case AF9033_TUNER_IT9135_51:
902 case AF9033_TUNER_IT9135_52:
903 case AF9033_TUNER_IT9135_60:
904 case AF9033_TUNER_IT9135_61:
905 case AF9033_TUNER_IT9135_62:
908 dev_warn(&d->udev->dev,
909 "%s: tuner id=%02x not supported, please report!",
910 KBUILD_MODNAME, tmp);
913 /* disable dual mode if driver does not support it */
915 switch (state->af9033_config[i].tuner) {
916 case AF9033_TUNER_FC0012:
917 case AF9033_TUNER_IT9135_38:
918 case AF9033_TUNER_IT9135_51:
919 case AF9033_TUNER_IT9135_52:
920 case AF9033_TUNER_IT9135_60:
921 case AF9033_TUNER_IT9135_61:
922 case AF9033_TUNER_IT9135_62:
923 case AF9033_TUNER_MXL5007T:
926 state->dual_mode = false;
927 dev_info(&d->udev->dev,
928 "%s: driver does not support 2nd tuner and will disable it",
932 /* tuner IF frequency */
933 ret = af9035_rd_reg(d, addr + EEPROM_1_IF_L, &tmp);
939 ret = af9035_rd_reg(d, addr + EEPROM_1_IF_H, &tmp);
945 dev_dbg(&d->udev->dev, "%s: [%d]IF=%d\n", __func__, i, tmp16);
947 addr += 0x10; /* shift for the 2nd tuner params */
951 /* get demod clock */
952 ret = af9035_rd_reg(d, 0x00d800, &tmp);
956 tmp = (tmp >> 0) & 0x0f;
958 for (i = 0; i < ARRAY_SIZE(state->af9033_config); i++) {
959 if (state->chip_type == 0x9135)
960 state->af9033_config[i].clock = clock_lut_it9135[tmp];
962 state->af9033_config[i].clock = clock_lut_af9035[tmp];
968 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
973 static int af9035_tua9001_tuner_callback(struct dvb_usb_device *d,
979 dev_dbg(&d->udev->dev, "%s: cmd=%d arg=%d\n", __func__, cmd, arg);
982 * CEN always enabled by hardware wiring
988 case TUA9001_CMD_RESETN:
994 ret = af9035_wr_reg_mask(d, 0x00d8e7, val, 0x01);
998 case TUA9001_CMD_RXEN:
1004 ret = af9035_wr_reg_mask(d, 0x00d8eb, val, 0x01);
1013 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
1019 static int af9035_fc0011_tuner_callback(struct dvb_usb_device *d,
1025 case FC0011_FE_CALLBACK_POWER:
1027 ret = af9035_wr_reg_mask(d, 0xd8eb, 1, 1);
1031 ret = af9035_wr_reg_mask(d, 0xd8ec, 1, 1);
1035 ret = af9035_wr_reg_mask(d, 0xd8ed, 1, 1);
1040 ret = af9035_wr_reg_mask(d, 0xd8d0, 1, 1);
1044 ret = af9035_wr_reg_mask(d, 0xd8d1, 1, 1);
1048 usleep_range(10000, 50000);
1050 case FC0011_FE_CALLBACK_RESET:
1051 ret = af9035_wr_reg(d, 0xd8e9, 1);
1055 ret = af9035_wr_reg(d, 0xd8e8, 1);
1059 ret = af9035_wr_reg(d, 0xd8e7, 1);
1063 usleep_range(10000, 20000);
1065 ret = af9035_wr_reg(d, 0xd8e7, 0);
1069 usleep_range(10000, 20000);
1079 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
1084 static int af9035_tuner_callback(struct dvb_usb_device *d, int cmd, int arg)
1086 struct state *state = d_to_priv(d);
1088 switch (state->af9033_config[0].tuner) {
1089 case AF9033_TUNER_FC0011:
1090 return af9035_fc0011_tuner_callback(d, cmd, arg);
1091 case AF9033_TUNER_TUA9001:
1092 return af9035_tua9001_tuner_callback(d, cmd, arg);
1100 static int af9035_frontend_callback(void *adapter_priv, int component,
1103 struct i2c_adapter *adap = adapter_priv;
1104 struct dvb_usb_device *d = i2c_get_adapdata(adap);
1106 dev_dbg(&d->udev->dev, "%s: component=%d cmd=%d arg=%d\n",
1107 __func__, component, cmd, arg);
1109 switch (component) {
1110 case DVB_FRONTEND_COMPONENT_TUNER:
1111 return af9035_tuner_callback(d, cmd, arg);
1119 static int af9035_get_adapter_count(struct dvb_usb_device *d)
1121 struct state *state = d_to_priv(d);
1123 return state->dual_mode + 1;
1126 static int af9035_frontend_attach(struct dvb_usb_adapter *adap)
1128 struct state *state = adap_to_priv(adap);
1129 struct dvb_usb_device *d = adap_to_d(adap);
1132 dev_dbg(&d->udev->dev, "%s: adap->id=%d\n", __func__, adap->id);
1134 if (!state->af9033_config[adap->id].tuner) {
1135 /* unsupported tuner */
1140 state->af9033_config[adap->id].fe = &adap->fe[0];
1141 state->af9033_config[adap->id].ops = &state->ops;
1142 ret = af9035_add_i2c_dev(d, "af9033", state->af9033_i2c_addr[adap->id],
1143 &state->af9033_config[adap->id], &d->i2c_adap);
1147 if (adap->fe[0] == NULL) {
1152 /* disable I2C-gate */
1153 adap->fe[0]->ops.i2c_gate_ctrl = NULL;
1154 adap->fe[0]->callback = af9035_frontend_callback;
1159 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
1164 static int it930x_frontend_attach(struct dvb_usb_adapter *adap)
1166 struct state *state = adap_to_priv(adap);
1167 struct dvb_usb_device *d = adap_to_d(adap);
1169 struct si2168_config si2168_config;
1170 struct i2c_adapter *adapter;
1172 dev_dbg(&d->udev->dev, "adap->id=%d\n", adap->id);
1174 memset(&si2168_config, 0, sizeof(si2168_config));
1175 si2168_config.i2c_adapter = &adapter;
1176 si2168_config.fe = &adap->fe[0];
1177 si2168_config.ts_mode = SI2168_TS_SERIAL;
1179 state->af9033_config[adap->id].fe = &adap->fe[0];
1180 state->af9033_config[adap->id].ops = &state->ops;
1181 ret = af9035_add_i2c_dev(d, "si2168", 0x67, &si2168_config,
1186 if (adap->fe[0] == NULL) {
1190 state->i2c_adapter_demod = adapter;
1195 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
1200 static int af9035_frontend_detach(struct dvb_usb_adapter *adap)
1202 struct state *state = adap_to_priv(adap);
1203 struct dvb_usb_device *d = adap_to_d(adap);
1206 dev_dbg(&d->udev->dev, "%s: adap->id=%d\n", __func__, adap->id);
1209 * For dual tuner devices we have to resolve 2nd demod client, as there
1210 * is two different kind of tuner drivers; one is using I2C binding
1211 * and the other is using DVB attach/detach binding.
1213 switch (state->af9033_config[adap->id].tuner) {
1214 case AF9033_TUNER_IT9135_38:
1215 case AF9033_TUNER_IT9135_51:
1216 case AF9033_TUNER_IT9135_52:
1217 case AF9033_TUNER_IT9135_60:
1218 case AF9033_TUNER_IT9135_61:
1219 case AF9033_TUNER_IT9135_62:
1226 if (adap->id == 1) {
1227 if (state->i2c_client[demod2])
1228 af9035_del_i2c_dev(d);
1229 } else if (adap->id == 0) {
1230 if (state->i2c_client[0])
1231 af9035_del_i2c_dev(d);
1237 static struct tua9001_config af9035_tua9001_config = {
1241 static const struct fc0011_config af9035_fc0011_config = {
1242 .i2c_address = 0x60,
1245 static struct mxl5007t_config af9035_mxl5007t_config[] = {
1247 .xtal_freq_hz = MxL_XTAL_24_MHZ,
1248 .if_freq_hz = MxL_IF_4_57_MHZ,
1250 .loop_thru_enable = 0,
1251 .clk_out_enable = 0,
1252 .clk_out_amp = MxL_CLKOUT_AMP_0_94V,
1254 .xtal_freq_hz = MxL_XTAL_24_MHZ,
1255 .if_freq_hz = MxL_IF_4_57_MHZ,
1257 .loop_thru_enable = 1,
1258 .clk_out_enable = 1,
1259 .clk_out_amp = MxL_CLKOUT_AMP_0_94V,
1263 static struct tda18218_config af9035_tda18218_config = {
1264 .i2c_address = 0x60,
1268 static const struct fc2580_config af9035_fc2580_config = {
1273 static const struct fc0012_config af9035_fc0012_config[] = {
1275 .i2c_address = 0x63,
1276 .xtal_freq = FC_XTAL_36_MHZ,
1277 .dual_master = true,
1278 .loop_through = true,
1281 .i2c_address = 0x63 | 0x80, /* I2C bus select hack */
1282 .xtal_freq = FC_XTAL_36_MHZ,
1283 .dual_master = true,
1287 static int af9035_tuner_attach(struct dvb_usb_adapter *adap)
1289 struct state *state = adap_to_priv(adap);
1290 struct dvb_usb_device *d = adap_to_d(adap);
1292 struct dvb_frontend *fe;
1293 struct i2c_msg msg[1];
1296 dev_dbg(&d->udev->dev, "%s: adap->id=%d\n", __func__, adap->id);
1299 * XXX: Hack used in that function: we abuse unused I2C address bit [7]
1300 * to carry info about used I2C bus for dual tuner configuration.
1303 switch (state->af9033_config[adap->id].tuner) {
1304 case AF9033_TUNER_TUA9001:
1305 /* AF9035 gpiot3 = TUA9001 RESETN
1306 AF9035 gpiot2 = TUA9001 RXEN */
1308 /* configure gpiot2 and gpiot2 as output */
1309 ret = af9035_wr_reg_mask(d, 0x00d8ec, 0x01, 0x01);
1313 ret = af9035_wr_reg_mask(d, 0x00d8ed, 0x01, 0x01);
1317 ret = af9035_wr_reg_mask(d, 0x00d8e8, 0x01, 0x01);
1321 ret = af9035_wr_reg_mask(d, 0x00d8e9, 0x01, 0x01);
1326 fe = dvb_attach(tua9001_attach, adap->fe[0],
1327 &d->i2c_adap, &af9035_tua9001_config);
1329 case AF9033_TUNER_FC0011:
1330 fe = dvb_attach(fc0011_attach, adap->fe[0],
1331 &d->i2c_adap, &af9035_fc0011_config);
1333 case AF9033_TUNER_MXL5007T:
1334 if (adap->id == 0) {
1335 ret = af9035_wr_reg(d, 0x00d8e0, 1);
1339 ret = af9035_wr_reg(d, 0x00d8e1, 1);
1343 ret = af9035_wr_reg(d, 0x00d8df, 0);
1349 ret = af9035_wr_reg(d, 0x00d8df, 1);
1355 ret = af9035_wr_reg(d, 0x00d8c0, 1);
1359 ret = af9035_wr_reg(d, 0x00d8c1, 1);
1363 ret = af9035_wr_reg(d, 0x00d8bf, 0);
1367 ret = af9035_wr_reg(d, 0x00d8b4, 1);
1371 ret = af9035_wr_reg(d, 0x00d8b5, 1);
1375 ret = af9035_wr_reg(d, 0x00d8b3, 1);
1381 tuner_addr = 0x60 | 0x80; /* I2C bus hack */
1385 fe = dvb_attach(mxl5007t_attach, adap->fe[0], &d->i2c_adap,
1386 tuner_addr, &af9035_mxl5007t_config[adap->id]);
1388 case AF9033_TUNER_TDA18218:
1390 fe = dvb_attach(tda18218_attach, adap->fe[0],
1391 &d->i2c_adap, &af9035_tda18218_config);
1393 case AF9033_TUNER_FC2580:
1394 /* Tuner enable using gpiot2_o, gpiot2_en and gpiot2_on */
1395 ret = af9035_wr_reg_mask(d, 0xd8eb, 0x01, 0x01);
1399 ret = af9035_wr_reg_mask(d, 0xd8ec, 0x01, 0x01);
1403 ret = af9035_wr_reg_mask(d, 0xd8ed, 0x01, 0x01);
1407 usleep_range(10000, 50000);
1409 fe = dvb_attach(fc2580_attach, adap->fe[0],
1410 &d->i2c_adap, &af9035_fc2580_config);
1412 case AF9033_TUNER_FC0012:
1414 * AF9035 gpiot2 = FC0012 enable
1415 * XXX: there seems to be something on gpioh8 too, but on my
1416 * my test I didn't find any difference.
1419 if (adap->id == 0) {
1420 /* configure gpiot2 as output and high */
1421 ret = af9035_wr_reg_mask(d, 0xd8eb, 0x01, 0x01);
1425 ret = af9035_wr_reg_mask(d, 0xd8ec, 0x01, 0x01);
1429 ret = af9035_wr_reg_mask(d, 0xd8ed, 0x01, 0x01);
1434 * FIXME: That belongs for the FC0012 driver.
1435 * Write 02 to FC0012 master tuner register 0d directly
1436 * in order to make slave tuner working.
1441 msg[0].buf = "\x0d\x02";
1442 ret = i2c_transfer(&d->i2c_adap, msg, 1);
1447 usleep_range(10000, 50000);
1449 fe = dvb_attach(fc0012_attach, adap->fe[0], &d->i2c_adap,
1450 &af9035_fc0012_config[adap->id]);
1452 case AF9033_TUNER_IT9135_38:
1453 case AF9033_TUNER_IT9135_51:
1454 case AF9033_TUNER_IT9135_52:
1456 struct it913x_config it913x_config = {
1461 if (state->dual_mode) {
1463 it913x_config.role = IT913X_ROLE_DUAL_MASTER;
1465 it913x_config.role = IT913X_ROLE_DUAL_SLAVE;
1468 ret = af9035_add_i2c_dev(d, "it913x",
1469 state->af9033_i2c_addr[adap->id] >> 1,
1470 &it913x_config, &d->i2c_adap);
1477 case AF9033_TUNER_IT9135_60:
1478 case AF9033_TUNER_IT9135_61:
1479 case AF9033_TUNER_IT9135_62:
1481 struct it913x_config it913x_config = {
1486 if (state->dual_mode) {
1488 it913x_config.role = IT913X_ROLE_DUAL_MASTER;
1490 it913x_config.role = IT913X_ROLE_DUAL_SLAVE;
1493 ret = af9035_add_i2c_dev(d, "it913x",
1494 state->af9033_i2c_addr[adap->id] >> 1,
1495 &it913x_config, &d->i2c_adap);
1514 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
1519 static int it930x_tuner_attach(struct dvb_usb_adapter *adap)
1521 struct state *state = adap_to_priv(adap);
1522 struct dvb_usb_device *d = adap_to_d(adap);
1524 struct si2157_config si2157_config;
1526 dev_dbg(&d->udev->dev, "%s: adap->id=%d\n", __func__, adap->id);
1528 /* I2C master bus 2 clock speed 300k */
1529 ret = af9035_wr_reg(d, 0x00f6a7, 0x07);
1533 /* I2C master bus 1,3 clock speed 300k */
1534 ret = af9035_wr_reg(d, 0x00f103, 0x07);
1538 /* set gpio11 low */
1539 ret = af9035_wr_reg_mask(d, 0xd8d4, 0x01, 0x01);
1543 ret = af9035_wr_reg_mask(d, 0xd8d5, 0x01, 0x01);
1547 ret = af9035_wr_reg_mask(d, 0xd8d3, 0x01, 0x01);
1551 /* Tuner enable using gpiot2_en, gpiot2_on and gpiot2_o (reset) */
1552 ret = af9035_wr_reg_mask(d, 0xd8b8, 0x01, 0x01);
1556 ret = af9035_wr_reg_mask(d, 0xd8b9, 0x01, 0x01);
1560 ret = af9035_wr_reg_mask(d, 0xd8b7, 0x00, 0x01);
1566 ret = af9035_wr_reg_mask(d, 0xd8b7, 0x01, 0x01);
1570 memset(&si2157_config, 0, sizeof(si2157_config));
1571 si2157_config.fe = adap->fe[0];
1572 ret = af9035_add_i2c_dev(d, "si2157", 0x63,
1573 &si2157_config, state->i2c_adapter_demod);
1581 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
1587 static int it930x_tuner_detach(struct dvb_usb_adapter *adap)
1589 struct state *state = adap_to_priv(adap);
1590 struct dvb_usb_device *d = adap_to_d(adap);
1592 dev_dbg(&d->udev->dev, "adap->id=%d\n", adap->id);
1594 if (adap->id == 1) {
1595 if (state->i2c_client[3])
1596 af9035_del_i2c_dev(d);
1597 } else if (adap->id == 0) {
1598 if (state->i2c_client[1])
1599 af9035_del_i2c_dev(d);
1606 static int af9035_tuner_detach(struct dvb_usb_adapter *adap)
1608 struct state *state = adap_to_priv(adap);
1609 struct dvb_usb_device *d = adap_to_d(adap);
1611 dev_dbg(&d->udev->dev, "%s: adap->id=%d\n", __func__, adap->id);
1613 switch (state->af9033_config[adap->id].tuner) {
1614 case AF9033_TUNER_IT9135_38:
1615 case AF9033_TUNER_IT9135_51:
1616 case AF9033_TUNER_IT9135_52:
1617 case AF9033_TUNER_IT9135_60:
1618 case AF9033_TUNER_IT9135_61:
1619 case AF9033_TUNER_IT9135_62:
1620 if (adap->id == 1) {
1621 if (state->i2c_client[3])
1622 af9035_del_i2c_dev(d);
1623 } else if (adap->id == 0) {
1624 if (state->i2c_client[1])
1625 af9035_del_i2c_dev(d);
1632 static int af9035_init(struct dvb_usb_device *d)
1634 struct state *state = d_to_priv(d);
1636 u16 frame_size = (d->udev->speed == USB_SPEED_FULL ? 5 : 87) * 188 / 4;
1637 u8 packet_size = (d->udev->speed == USB_SPEED_FULL ? 64 : 512) / 4;
1638 struct reg_val_mask tab[] = {
1639 { 0x80f99d, 0x01, 0x01 },
1640 { 0x80f9a4, 0x01, 0x01 },
1641 { 0x00dd11, 0x00, 0x20 },
1642 { 0x00dd11, 0x00, 0x40 },
1643 { 0x00dd13, 0x00, 0x20 },
1644 { 0x00dd13, 0x00, 0x40 },
1645 { 0x00dd11, 0x20, 0x20 },
1646 { 0x00dd88, (frame_size >> 0) & 0xff, 0xff},
1647 { 0x00dd89, (frame_size >> 8) & 0xff, 0xff},
1648 { 0x00dd0c, packet_size, 0xff},
1649 { 0x00dd11, state->dual_mode << 6, 0x40 },
1650 { 0x00dd8a, (frame_size >> 0) & 0xff, 0xff},
1651 { 0x00dd8b, (frame_size >> 8) & 0xff, 0xff},
1652 { 0x00dd0d, packet_size, 0xff },
1653 { 0x80f9a3, state->dual_mode, 0x01 },
1654 { 0x80f9cd, state->dual_mode, 0x01 },
1655 { 0x80f99d, 0x00, 0x01 },
1656 { 0x80f9a4, 0x00, 0x01 },
1659 dev_dbg(&d->udev->dev,
1660 "%s: USB speed=%d frame_size=%04x packet_size=%02x\n",
1661 __func__, d->udev->speed, frame_size, packet_size);
1663 /* init endpoints */
1664 for (i = 0; i < ARRAY_SIZE(tab); i++) {
1665 ret = af9035_wr_reg_mask(d, tab[i].reg, tab[i].val,
1674 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
1679 static int it930x_init(struct dvb_usb_device *d)
1681 struct state *state = d_to_priv(d);
1683 u16 frame_size = (d->udev->speed == USB_SPEED_FULL ? 5 : 816) * 188 / 4;
1684 u8 packet_size = (d->udev->speed == USB_SPEED_FULL ? 64 : 512) / 4;
1685 struct reg_val_mask tab[] = {
1686 { 0x00da1a, 0x00, 0x01 }, /* ignore_sync_byte */
1687 { 0x00f41f, 0x04, 0x04 }, /* dvbt_inten */
1688 { 0x00da10, 0x00, 0x01 }, /* mpeg_full_speed */
1689 { 0x00f41a, 0x01, 0x01 }, /* dvbt_en */
1690 { 0x00da1d, 0x01, 0x01 }, /* mp2_sw_rst, reset EP4 */
1691 { 0x00dd11, 0x00, 0x20 }, /* ep4_tx_en, disable EP4 */
1692 { 0x00dd13, 0x00, 0x20 }, /* ep4_tx_nak, disable EP4 NAK */
1693 { 0x00dd11, 0x20, 0x20 }, /* ep4_tx_en, enable EP4 */
1694 { 0x00dd11, 0x00, 0x40 }, /* ep5_tx_en, disable EP5 */
1695 { 0x00dd13, 0x00, 0x40 }, /* ep5_tx_nak, disable EP5 NAK */
1696 { 0x00dd11, state->dual_mode << 6, 0x40 }, /* enable EP5 */
1697 { 0x00dd88, (frame_size >> 0) & 0xff, 0xff},
1698 { 0x00dd89, (frame_size >> 8) & 0xff, 0xff},
1699 { 0x00dd0c, packet_size, 0xff},
1700 { 0x00dd8a, (frame_size >> 0) & 0xff, 0xff},
1701 { 0x00dd8b, (frame_size >> 8) & 0xff, 0xff},
1702 { 0x00dd0d, packet_size, 0xff },
1703 { 0x00da1d, 0x00, 0x01 }, /* mp2_sw_rst, disable */
1704 { 0x00d833, 0x01, 0xff }, /* slew rate ctrl: slew rate boosts */
1705 { 0x00d830, 0x00, 0xff }, /* Bit 0 of output driving control */
1706 { 0x00d831, 0x01, 0xff }, /* Bit 1 of output driving control */
1707 { 0x00d832, 0x00, 0xff }, /* Bit 2 of output driving control */
1709 /* suspend gpio1 for TS-C */
1710 { 0x00d8b0, 0x01, 0xff }, /* gpio1 */
1711 { 0x00d8b1, 0x01, 0xff }, /* gpio1 */
1712 { 0x00d8af, 0x00, 0xff }, /* gpio1 */
1714 /* suspend gpio7 for TS-D */
1715 { 0x00d8c4, 0x01, 0xff }, /* gpio7 */
1716 { 0x00d8c5, 0x01, 0xff }, /* gpio7 */
1717 { 0x00d8c3, 0x00, 0xff }, /* gpio7 */
1719 /* suspend gpio13 for TS-B */
1720 { 0x00d8dc, 0x01, 0xff }, /* gpio13 */
1721 { 0x00d8dd, 0x01, 0xff }, /* gpio13 */
1722 { 0x00d8db, 0x00, 0xff }, /* gpio13 */
1724 /* suspend gpio14 for TS-E */
1725 { 0x00d8e4, 0x01, 0xff }, /* gpio14 */
1726 { 0x00d8e5, 0x01, 0xff }, /* gpio14 */
1727 { 0x00d8e3, 0x00, 0xff }, /* gpio14 */
1729 /* suspend gpio15 for TS-A */
1730 { 0x00d8e8, 0x01, 0xff }, /* gpio15 */
1731 { 0x00d8e9, 0x01, 0xff }, /* gpio15 */
1732 { 0x00d8e7, 0x00, 0xff }, /* gpio15 */
1734 { 0x00da58, 0x00, 0x01 }, /* ts_in_src, serial */
1735 { 0x00da73, 0x01, 0xff }, /* ts0_aggre_mode */
1736 { 0x00da78, 0x47, 0xff }, /* ts0_sync_byte */
1737 { 0x00da4c, 0x01, 0xff }, /* ts0_en */
1738 { 0x00da5a, 0x1f, 0xff }, /* ts_fail_ignore */
1741 dev_dbg(&d->udev->dev,
1742 "%s: USB speed=%d frame_size=%04x packet_size=%02x\n",
1743 __func__, d->udev->speed, frame_size, packet_size);
1745 /* init endpoints */
1746 for (i = 0; i < ARRAY_SIZE(tab); i++) {
1747 ret = af9035_wr_reg_mask(d, tab[i].reg,
1748 tab[i].val, tab[i].mask);
1756 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
1762 #if IS_ENABLED(CONFIG_RC_CORE)
1763 static int af9035_rc_query(struct dvb_usb_device *d)
1768 struct usb_req req = { CMD_IR_GET, 0, 0, NULL, 4, buf };
1770 ret = af9035_ctrl_msg(d, &req);
1776 if ((buf[2] + buf[3]) == 0xff) {
1777 if ((buf[0] + buf[1]) == 0xff) {
1778 /* NEC standard 16bit */
1779 key = RC_SCANCODE_NEC(buf[0], buf[2]);
1781 /* NEC extended 24bit */
1782 key = RC_SCANCODE_NECX(buf[0] << 8 | buf[1], buf[2]);
1785 /* NEC full code 32bit */
1786 key = RC_SCANCODE_NEC32(buf[0] << 24 | buf[1] << 16 |
1787 buf[2] << 8 | buf[3]);
1790 dev_dbg(&d->udev->dev, "%s: %*ph\n", __func__, 4, buf);
1792 rc_keydown(d->rc_dev, RC_TYPE_NEC, key, 0);
1797 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
1802 static int af9035_get_rc_config(struct dvb_usb_device *d, struct dvb_usb_rc *rc)
1804 struct state *state = d_to_priv(d);
1808 ret = af9035_rd_reg(d, state->eeprom_addr + EEPROM_IR_MODE, &tmp);
1812 dev_dbg(&d->udev->dev, "%s: ir_mode=%02x\n", __func__, tmp);
1814 /* don't activate rc if in HID mode or if not available */
1816 ret = af9035_rd_reg(d, state->eeprom_addr + EEPROM_IR_TYPE,
1821 dev_dbg(&d->udev->dev, "%s: ir_type=%02x\n", __func__, tmp);
1826 rc->allowed_protos = RC_BIT_NEC;
1829 rc->allowed_protos = RC_BIT_RC6_MCE;
1833 rc->query = af9035_rc_query;
1836 /* load empty to enable rc */
1838 rc->map_name = RC_MAP_EMPTY;
1844 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
1849 #define af9035_get_rc_config NULL
1852 static int af9035_get_stream_config(struct dvb_frontend *fe, u8 *ts_type,
1853 struct usb_data_stream_properties *stream)
1855 struct dvb_usb_device *d = fe_to_d(fe);
1857 dev_dbg(&d->udev->dev, "%s: adap=%d\n", __func__, fe_to_adap(fe)->id);
1859 if (d->udev->speed == USB_SPEED_FULL)
1860 stream->u.bulk.buffersize = 5 * 188;
1865 static int af9035_pid_filter_ctrl(struct dvb_usb_adapter *adap, int onoff)
1867 struct state *state = adap_to_priv(adap);
1869 return state->ops.pid_filter_ctrl(adap->fe[0], onoff);
1872 static int af9035_pid_filter(struct dvb_usb_adapter *adap, int index, u16 pid,
1875 struct state *state = adap_to_priv(adap);
1877 return state->ops.pid_filter(adap->fe[0], index, pid, onoff);
1880 static int af9035_probe(struct usb_interface *intf,
1881 const struct usb_device_id *id)
1883 struct usb_device *udev = interface_to_usbdev(intf);
1884 char manufacturer[sizeof("Afatech")];
1886 memset(manufacturer, 0, sizeof(manufacturer));
1887 usb_string(udev, udev->descriptor.iManufacturer,
1888 manufacturer, sizeof(manufacturer));
1890 * There is two devices having same ID but different chipset. One uses
1891 * AF9015 and the other IT9135 chipset. Only difference seen on lsusb
1892 * is iManufacturer string.
1894 * idVendor 0x0ccd TerraTec Electronic GmbH
1897 * iManufacturer 1 Afatech
1898 * iProduct 2 DVB-T 2
1900 * idVendor 0x0ccd TerraTec Electronic GmbH
1903 * iManufacturer 1 ITE Technologies, Inc.
1904 * iProduct 2 DVB-T TV Stick
1906 if ((le16_to_cpu(udev->descriptor.idVendor) == USB_VID_TERRATEC) &&
1907 (le16_to_cpu(udev->descriptor.idProduct) == 0x0099)) {
1908 if (!strcmp("Afatech", manufacturer)) {
1909 dev_dbg(&udev->dev, "%s: rejecting device\n", __func__);
1914 return dvb_usbv2_probe(intf, id);
1917 /* interface 0 is used by DVB-T receiver and
1918 interface 1 is for remote controller (HID) */
1919 static const struct dvb_usb_device_properties af9035_props = {
1920 .driver_name = KBUILD_MODNAME,
1921 .owner = THIS_MODULE,
1922 .adapter_nr = adapter_nr,
1923 .size_of_priv = sizeof(struct state),
1925 .generic_bulk_ctrl_endpoint = 0x02,
1926 .generic_bulk_ctrl_endpoint_response = 0x81,
1928 .identify_state = af9035_identify_state,
1929 .download_firmware = af9035_download_firmware,
1931 .i2c_algo = &af9035_i2c_algo,
1932 .read_config = af9035_read_config,
1933 .frontend_attach = af9035_frontend_attach,
1934 .frontend_detach = af9035_frontend_detach,
1935 .tuner_attach = af9035_tuner_attach,
1936 .tuner_detach = af9035_tuner_detach,
1937 .init = af9035_init,
1938 .get_rc_config = af9035_get_rc_config,
1939 .get_stream_config = af9035_get_stream_config,
1941 .get_adapter_count = af9035_get_adapter_count,
1944 .caps = DVB_USB_ADAP_HAS_PID_FILTER |
1945 DVB_USB_ADAP_PID_FILTER_CAN_BE_TURNED_OFF,
1947 .pid_filter_count = 32,
1948 .pid_filter_ctrl = af9035_pid_filter_ctrl,
1949 .pid_filter = af9035_pid_filter,
1951 .stream = DVB_USB_STREAM_BULK(0x84, 6, 87 * 188),
1953 .caps = DVB_USB_ADAP_HAS_PID_FILTER |
1954 DVB_USB_ADAP_PID_FILTER_CAN_BE_TURNED_OFF,
1956 .pid_filter_count = 32,
1957 .pid_filter_ctrl = af9035_pid_filter_ctrl,
1958 .pid_filter = af9035_pid_filter,
1960 .stream = DVB_USB_STREAM_BULK(0x85, 6, 87 * 188),
1965 static const struct dvb_usb_device_properties it930x_props = {
1966 .driver_name = KBUILD_MODNAME,
1967 .owner = THIS_MODULE,
1968 .adapter_nr = adapter_nr,
1969 .size_of_priv = sizeof(struct state),
1971 .generic_bulk_ctrl_endpoint = 0x02,
1972 .generic_bulk_ctrl_endpoint_response = 0x81,
1974 .identify_state = af9035_identify_state,
1975 .download_firmware = af9035_download_firmware,
1977 .i2c_algo = &af9035_i2c_algo,
1978 .read_config = af9035_read_config,
1979 .frontend_attach = it930x_frontend_attach,
1980 .frontend_detach = af9035_frontend_detach,
1981 .tuner_attach = it930x_tuner_attach,
1982 .tuner_detach = it930x_tuner_detach,
1983 .init = it930x_init,
1984 .get_stream_config = af9035_get_stream_config,
1986 .get_adapter_count = af9035_get_adapter_count,
1989 .stream = DVB_USB_STREAM_BULK(0x84, 4, 816 * 188),
1991 .stream = DVB_USB_STREAM_BULK(0x85, 4, 816 * 188),
1996 static const struct usb_device_id af9035_id_table[] = {
1997 /* AF9035 devices */
1998 { DVB_USB_DEVICE(USB_VID_AFATECH, USB_PID_AFATECH_AF9035_9035,
1999 &af9035_props, "Afatech AF9035 reference design", NULL) },
2000 { DVB_USB_DEVICE(USB_VID_AFATECH, USB_PID_AFATECH_AF9035_1000,
2001 &af9035_props, "Afatech AF9035 reference design", NULL) },
2002 { DVB_USB_DEVICE(USB_VID_AFATECH, USB_PID_AFATECH_AF9035_1001,
2003 &af9035_props, "Afatech AF9035 reference design", NULL) },
2004 { DVB_USB_DEVICE(USB_VID_AFATECH, USB_PID_AFATECH_AF9035_1002,
2005 &af9035_props, "Afatech AF9035 reference design", NULL) },
2006 { DVB_USB_DEVICE(USB_VID_AFATECH, USB_PID_AFATECH_AF9035_1003,
2007 &af9035_props, "Afatech AF9035 reference design", NULL) },
2008 { DVB_USB_DEVICE(USB_VID_TERRATEC, USB_PID_TERRATEC_CINERGY_T_STICK,
2009 &af9035_props, "TerraTec Cinergy T Stick", NULL) },
2010 { DVB_USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_A835,
2011 &af9035_props, "AVerMedia AVerTV Volar HD/PRO (A835)", NULL) },
2012 { DVB_USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_B835,
2013 &af9035_props, "AVerMedia AVerTV Volar HD/PRO (A835)", NULL) },
2014 { DVB_USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_1867,
2015 &af9035_props, "AVerMedia HD Volar (A867)", NULL) },
2016 { DVB_USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_A867,
2017 &af9035_props, "AVerMedia HD Volar (A867)", NULL) },
2018 { DVB_USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_TWINSTAR,
2019 &af9035_props, "AVerMedia Twinstar (A825)", NULL) },
2020 { DVB_USB_DEVICE(USB_VID_ASUS, USB_PID_ASUS_U3100MINI_PLUS,
2021 &af9035_props, "Asus U3100Mini Plus", NULL) },
2022 { DVB_USB_DEVICE(USB_VID_TERRATEC, 0x00aa,
2023 &af9035_props, "TerraTec Cinergy T Stick (rev. 2)", NULL) },
2024 /* IT9135 devices */
2025 { DVB_USB_DEVICE(USB_VID_ITETECH, USB_PID_ITETECH_IT9135,
2026 &af9035_props, "ITE 9135 Generic", RC_MAP_IT913X_V1) },
2027 { DVB_USB_DEVICE(USB_VID_ITETECH, USB_PID_ITETECH_IT9135_9005,
2028 &af9035_props, "ITE 9135(9005) Generic", RC_MAP_IT913X_V2) },
2029 { DVB_USB_DEVICE(USB_VID_ITETECH, USB_PID_ITETECH_IT9135_9006,
2030 &af9035_props, "ITE 9135(9006) Generic", RC_MAP_IT913X_V1) },
2031 { DVB_USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_A835B_1835,
2032 &af9035_props, "Avermedia A835B(1835)", RC_MAP_IT913X_V2) },
2033 { DVB_USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_A835B_2835,
2034 &af9035_props, "Avermedia A835B(2835)", RC_MAP_IT913X_V2) },
2035 { DVB_USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_A835B_3835,
2036 &af9035_props, "Avermedia A835B(3835)", RC_MAP_IT913X_V2) },
2037 { DVB_USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_A835B_4835,
2038 &af9035_props, "Avermedia A835B(4835)", RC_MAP_IT913X_V2) },
2039 { DVB_USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_H335,
2040 &af9035_props, "Avermedia H335", RC_MAP_IT913X_V2) },
2041 { DVB_USB_DEVICE(USB_VID_KWORLD_2, USB_PID_KWORLD_UB499_2T_T09,
2042 &af9035_props, "Kworld UB499-2T T09", RC_MAP_IT913X_V1) },
2043 { DVB_USB_DEVICE(USB_VID_KWORLD_2, USB_PID_SVEON_STV22_IT9137,
2044 &af9035_props, "Sveon STV22 Dual DVB-T HDTV",
2045 RC_MAP_IT913X_V1) },
2046 { DVB_USB_DEVICE(USB_VID_KWORLD_2, USB_PID_CTVDIGDUAL_V2,
2047 &af9035_props, "Digital Dual TV Receiver CTVDIGDUAL_V2",
2048 RC_MAP_IT913X_V1) },
2049 /* IT930x devices */
2050 { DVB_USB_DEVICE(USB_VID_ITETECH, USB_PID_ITETECH_IT9303,
2051 &it930x_props, "ITE 9303 Generic", NULL) },
2052 /* XXX: that same ID [0ccd:0099] is used by af9015 driver too */
2053 { DVB_USB_DEVICE(USB_VID_TERRATEC, 0x0099,
2054 &af9035_props, "TerraTec Cinergy T Stick Dual RC (rev. 2)",
2056 { DVB_USB_DEVICE(USB_VID_LEADTEK, 0x6a05,
2057 &af9035_props, "Leadtek WinFast DTV Dongle Dual", NULL) },
2058 { DVB_USB_DEVICE(USB_VID_HAUPPAUGE, 0xf900,
2059 &af9035_props, "Hauppauge WinTV-MiniStick 2", NULL) },
2060 { DVB_USB_DEVICE(USB_VID_PCTV, USB_PID_PCTV_78E,
2061 &af9035_props, "PCTV AndroiDTV (78e)", RC_MAP_IT913X_V1) },
2062 { DVB_USB_DEVICE(USB_VID_PCTV, USB_PID_PCTV_79E,
2063 &af9035_props, "PCTV microStick (79e)", RC_MAP_IT913X_V2) },
2066 MODULE_DEVICE_TABLE(usb, af9035_id_table);
2068 static struct usb_driver af9035_usb_driver = {
2069 .name = KBUILD_MODNAME,
2070 .id_table = af9035_id_table,
2071 .probe = af9035_probe,
2072 .disconnect = dvb_usbv2_disconnect,
2073 .suspend = dvb_usbv2_suspend,
2074 .resume = dvb_usbv2_resume,
2075 .reset_resume = dvb_usbv2_reset_resume,
2080 module_usb_driver(af9035_usb_driver);
2082 MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
2083 MODULE_DESCRIPTION("Afatech AF9035 driver");
2084 MODULE_LICENSE("GPL");
2085 MODULE_FIRMWARE(AF9035_FIRMWARE_AF9035);
2086 MODULE_FIRMWARE(AF9035_FIRMWARE_IT9135_V1);
2087 MODULE_FIRMWARE(AF9035_FIRMWARE_IT9135_V2);
2088 MODULE_FIRMWARE(AF9035_FIRMWARE_IT9303);