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 DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
26 static u16 af9035_checksum(const u8 *buf, size_t len)
31 for (i = 1; i < len; i++) {
33 checksum += buf[i] << 8;
42 static int af9035_ctrl_msg(struct dvb_usb_device *d, struct usb_req *req)
44 #define REQ_HDR_LEN 4 /* send header size */
45 #define ACK_HDR_LEN 3 /* rece header size */
46 #define CHECKSUM_LEN 2
47 #define USB_TIMEOUT 2000
48 struct state *state = d_to_priv(d);
50 u16 checksum, tmp_checksum;
52 mutex_lock(&d->usb_mutex);
54 /* buffer overflow check */
55 if (req->wlen > (BUF_LEN - REQ_HDR_LEN - CHECKSUM_LEN) ||
56 req->rlen > (BUF_LEN - ACK_HDR_LEN - CHECKSUM_LEN)) {
57 dev_err(&d->udev->dev, "%s: too much data wlen=%d rlen=%d\n",
58 __func__, req->wlen, req->rlen);
63 state->buf[0] = REQ_HDR_LEN + req->wlen + CHECKSUM_LEN - 1;
64 state->buf[1] = req->mbox;
65 state->buf[2] = req->cmd;
66 state->buf[3] = state->seq++;
67 memcpy(&state->buf[REQ_HDR_LEN], req->wbuf, req->wlen);
69 wlen = REQ_HDR_LEN + req->wlen + CHECKSUM_LEN;
70 rlen = ACK_HDR_LEN + req->rlen + CHECKSUM_LEN;
72 /* calc and add checksum */
73 checksum = af9035_checksum(state->buf, state->buf[0] - 1);
74 state->buf[state->buf[0] - 1] = (checksum >> 8);
75 state->buf[state->buf[0] - 0] = (checksum & 0xff);
77 /* no ack for these packets */
78 if (req->cmd == CMD_FW_DL)
81 ret = dvb_usbv2_generic_rw_locked(d,
82 state->buf, wlen, state->buf, rlen);
86 /* no ack for those packets */
87 if (req->cmd == CMD_FW_DL)
91 checksum = af9035_checksum(state->buf, rlen - 2);
92 tmp_checksum = (state->buf[rlen - 2] << 8) | state->buf[rlen - 1];
93 if (tmp_checksum != checksum) {
94 dev_err(&d->udev->dev, "%s: command=%02x checksum mismatch " \
95 "(%04x != %04x)\n", KBUILD_MODNAME, req->cmd,
96 tmp_checksum, checksum);
103 /* fw returns status 1 when IR code was not received */
104 if (req->cmd == CMD_IR_GET || state->buf[2] == 1) {
109 dev_dbg(&d->udev->dev, "%s: command=%02x failed fw error=%d\n",
110 __func__, req->cmd, state->buf[2]);
115 /* read request, copy returned data to return buf */
117 memcpy(req->rbuf, &state->buf[ACK_HDR_LEN], req->rlen);
119 mutex_unlock(&d->usb_mutex);
121 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
125 /* write multiple registers */
126 static int af9035_wr_regs(struct dvb_usb_device *d, u32 reg, u8 *val, int len)
129 u8 mbox = (reg >> 16) & 0xff;
130 struct usb_req req = { CMD_MEM_WR, mbox, sizeof(wbuf), wbuf, 0, NULL };
136 wbuf[4] = (reg >> 8) & 0xff;
137 wbuf[5] = (reg >> 0) & 0xff;
138 memcpy(&wbuf[6], val, len);
140 return af9035_ctrl_msg(d, &req);
143 /* read multiple registers */
144 static int af9035_rd_regs(struct dvb_usb_device *d, u32 reg, u8 *val, int len)
146 u8 wbuf[] = { len, 2, 0, 0, (reg >> 8) & 0xff, reg & 0xff };
147 u8 mbox = (reg >> 16) & 0xff;
148 struct usb_req req = { CMD_MEM_RD, mbox, sizeof(wbuf), wbuf, len, val };
150 return af9035_ctrl_msg(d, &req);
153 /* write single register */
154 static int af9035_wr_reg(struct dvb_usb_device *d, u32 reg, u8 val)
156 return af9035_wr_regs(d, reg, &val, 1);
159 /* read single register */
160 static int af9035_rd_reg(struct dvb_usb_device *d, u32 reg, u8 *val)
162 return af9035_rd_regs(d, reg, val, 1);
165 /* write single register with mask */
166 static int af9035_wr_reg_mask(struct dvb_usb_device *d, u32 reg, u8 val,
172 /* no need for read if whole reg is written */
174 ret = af9035_rd_regs(d, reg, &tmp, 1);
183 return af9035_wr_regs(d, reg, &val, 1);
186 static int af9035_i2c_master_xfer(struct i2c_adapter *adap,
187 struct i2c_msg msg[], int num)
189 struct dvb_usb_device *d = i2c_get_adapdata(adap);
190 struct state *state = d_to_priv(d);
193 if (mutex_lock_interruptible(&d->i2c_mutex) < 0)
197 * I2C sub header is 5 bytes long. Meaning of those bytes are:
201 * byte 3 and 4 can be used as reg addr
203 * used when reg addr len is set to 2
205 * used when reg addr len is set to 1 or 2
207 * For the simplify we do not use register addr at all.
208 * NOTE: As a firmware knows tuner type there is very small possibility
209 * there could be some tuner I2C hacks done by firmware and this may
210 * lead problems if firmware expects those bytes are used.
212 if (num == 2 && !(msg[0].flags & I2C_M_RD) &&
213 (msg[1].flags & I2C_M_RD)) {
214 if (msg[0].len > 40 || msg[1].len > 40) {
215 /* TODO: correct limits > 40 */
217 } else if ((msg[0].addr == state->af9033_config[0].i2c_addr) ||
218 (msg[0].addr == state->af9033_config[1].i2c_addr)) {
219 /* demod access via firmware interface */
220 u32 reg = msg[0].buf[0] << 16 | msg[0].buf[1] << 8 |
223 if (msg[0].addr == state->af9033_config[1].i2c_addr)
226 ret = af9035_rd_regs(d, reg, &msg[1].buf[0],
230 u8 buf[5 + msg[0].len];
231 struct usb_req req = { CMD_I2C_RD, 0, sizeof(buf),
232 buf, msg[1].len, msg[1].buf };
233 req.mbox |= ((msg[0].addr & 0x80) >> 3);
235 buf[1] = msg[0].addr << 1;
236 buf[2] = 0x00; /* reg addr len */
237 buf[3] = 0x00; /* reg addr MSB */
238 buf[4] = 0x00; /* reg addr LSB */
239 memcpy(&buf[5], msg[0].buf, msg[0].len);
240 ret = af9035_ctrl_msg(d, &req);
242 } else if (num == 1 && !(msg[0].flags & I2C_M_RD)) {
243 if (msg[0].len > 40) {
244 /* TODO: correct limits > 40 */
246 } else if ((msg[0].addr == state->af9033_config[0].i2c_addr) ||
247 (msg[0].addr == state->af9033_config[1].i2c_addr)) {
248 /* demod access via firmware interface */
249 u32 reg = msg[0].buf[0] << 16 | msg[0].buf[1] << 8 |
252 if (msg[0].addr == state->af9033_config[1].i2c_addr)
255 ret = af9035_wr_regs(d, reg, &msg[0].buf[3],
259 u8 buf[5 + msg[0].len];
260 struct usb_req req = { CMD_I2C_WR, 0, sizeof(buf), buf,
262 req.mbox |= ((msg[0].addr & 0x80) >> 3);
264 buf[1] = msg[0].addr << 1;
265 buf[2] = 0x00; /* reg addr len */
266 buf[3] = 0x00; /* reg addr MSB */
267 buf[4] = 0x00; /* reg addr LSB */
268 memcpy(&buf[5], msg[0].buf, msg[0].len);
269 ret = af9035_ctrl_msg(d, &req);
273 * We support only two kind of I2C transactions:
274 * 1) 1 x read + 1 x write
280 mutex_unlock(&d->i2c_mutex);
288 static u32 af9035_i2c_functionality(struct i2c_adapter *adapter)
293 static struct i2c_algorithm af9035_i2c_algo = {
294 .master_xfer = af9035_i2c_master_xfer,
295 .functionality = af9035_i2c_functionality,
298 static int af9035_identify_state(struct dvb_usb_device *d, const char **name)
300 struct state *state = d_to_priv(d);
304 struct usb_req req = { CMD_FW_QUERYINFO, 0, sizeof(wbuf), wbuf,
305 sizeof(rbuf), rbuf };
307 ret = af9035_rd_regs(d, 0x1222, rbuf, 3);
311 state->chip_version = rbuf[0];
312 state->chip_type = rbuf[2] << 8 | rbuf[1] << 0;
314 ret = af9035_rd_reg(d, 0x384f, &state->prechip_version);
318 dev_info(&d->udev->dev,
319 "%s: prechip_version=%02x chip_version=%02x chip_type=%04x\n",
320 __func__, state->prechip_version, state->chip_version,
323 if (state->chip_type == 0x9135) {
324 if (state->chip_version == 0x02)
325 *name = AF9035_FIRMWARE_IT9135_V2;
327 *name = AF9035_FIRMWARE_IT9135_V1;
328 state->eeprom_addr = EEPROM_BASE_IT9135;
330 *name = AF9035_FIRMWARE_AF9035;
331 state->eeprom_addr = EEPROM_BASE_AF9035;
334 ret = af9035_ctrl_msg(d, &req);
338 dev_dbg(&d->udev->dev, "%s: reply=%*ph\n", __func__, 4, rbuf);
339 if (rbuf[0] || rbuf[1] || rbuf[2] || rbuf[3])
347 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
352 static int af9035_download_firmware_old(struct dvb_usb_device *d,
353 const struct firmware *fw)
357 struct usb_req req = { 0, 0, 0, NULL, 0, NULL };
358 struct usb_req req_fw_dl = { CMD_FW_DL, 0, 0, wbuf, 0, NULL };
360 u16 hdr_addr, hdr_data_len, hdr_checksum;
365 * Thanks to Daniel Glöckner <daniel-gl@gmx.net> about that info!
367 * byte 0: MCS 51 core
368 * There are two inside the AF9035 (1=Link and 2=OFDM) with separate
370 * byte 1-2: Big endian destination address
371 * byte 3-4: Big endian number of data bytes following the header
372 * byte 5-6: Big endian header checksum, apparently ignored by the chip
373 * Calculated as ~(h[0]*256+h[1]+h[2]*256+h[3]+h[4]*256)
376 for (i = fw->size; i > HDR_SIZE;) {
377 hdr_core = fw->data[fw->size - i + 0];
378 hdr_addr = fw->data[fw->size - i + 1] << 8;
379 hdr_addr |= fw->data[fw->size - i + 2] << 0;
380 hdr_data_len = fw->data[fw->size - i + 3] << 8;
381 hdr_data_len |= fw->data[fw->size - i + 4] << 0;
382 hdr_checksum = fw->data[fw->size - i + 5] << 8;
383 hdr_checksum |= fw->data[fw->size - i + 6] << 0;
385 dev_dbg(&d->udev->dev, "%s: core=%d addr=%04x data_len=%d " \
386 "checksum=%04x\n", __func__, hdr_core, hdr_addr,
387 hdr_data_len, hdr_checksum);
389 if (((hdr_core != 1) && (hdr_core != 2)) ||
390 (hdr_data_len > i)) {
391 dev_dbg(&d->udev->dev, "%s: bad firmware\n", __func__);
395 /* download begin packet */
396 req.cmd = CMD_FW_DL_BEGIN;
397 ret = af9035_ctrl_msg(d, &req);
401 /* download firmware packet(s) */
402 for (j = HDR_SIZE + hdr_data_len; j > 0; j -= MAX_DATA) {
406 req_fw_dl.wlen = len;
407 req_fw_dl.wbuf = (u8 *) &fw->data[fw->size - i +
408 HDR_SIZE + hdr_data_len - j];
409 ret = af9035_ctrl_msg(d, &req_fw_dl);
414 /* download end packet */
415 req.cmd = CMD_FW_DL_END;
416 ret = af9035_ctrl_msg(d, &req);
420 i -= hdr_data_len + HDR_SIZE;
422 dev_dbg(&d->udev->dev, "%s: data uploaded=%zu\n",
423 __func__, fw->size - i);
426 /* print warn if firmware is bad, continue and see what happens */
428 dev_warn(&d->udev->dev, "%s: bad firmware\n", KBUILD_MODNAME);
433 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
438 static int af9035_download_firmware_new(struct dvb_usb_device *d,
439 const struct firmware *fw)
442 struct usb_req req_fw_dl = { CMD_FW_SCATTER_WR, 0, 0, NULL, 0, NULL };
446 * There seems to be following firmware header. Meaning of bytes 0-3
455 * 6: count of data bytes ?
457 for (i = HDR_SIZE, i_prev = 0; i <= fw->size; i++) {
459 (fw->data[i + 0] == 0x03 &&
460 (fw->data[i + 1] == 0x00 ||
461 fw->data[i + 1] == 0x01) &&
462 fw->data[i + 2] == 0x00)) {
463 req_fw_dl.wlen = i - i_prev;
464 req_fw_dl.wbuf = (u8 *) &fw->data[i_prev];
466 ret = af9035_ctrl_msg(d, &req_fw_dl);
470 dev_dbg(&d->udev->dev, "%s: data uploaded=%d\n",
478 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
483 static int af9035_download_firmware(struct dvb_usb_device *d,
484 const struct firmware *fw)
486 struct state *state = d_to_priv(d);
491 struct usb_req req = { 0, 0, 0, NULL, 0, NULL };
492 struct usb_req req_fw_ver = { CMD_FW_QUERYINFO, 0, 1, wbuf, 4, rbuf } ;
493 dev_dbg(&d->udev->dev, "%s:\n", __func__);
496 * In case of dual tuner configuration we need to do some extra
497 * initialization in order to download firmware to slave demod too,
498 * which is done by master demod.
499 * Master feeds also clock and controls power via GPIO.
501 ret = af9035_rd_reg(d, state->eeprom_addr + EEPROM_DUAL_MODE, &tmp);
506 /* configure gpioh1, reset & power slave demod */
507 ret = af9035_wr_reg_mask(d, 0x00d8b0, 0x01, 0x01);
511 ret = af9035_wr_reg_mask(d, 0x00d8b1, 0x01, 0x01);
515 ret = af9035_wr_reg_mask(d, 0x00d8af, 0x00, 0x01);
519 usleep_range(10000, 50000);
521 ret = af9035_wr_reg_mask(d, 0x00d8af, 0x01, 0x01);
525 /* tell the slave I2C address */
526 ret = af9035_rd_reg(d,
527 state->eeprom_addr + EEPROM_2ND_DEMOD_ADDR,
532 if (state->chip_type == 0x9135) {
533 ret = af9035_wr_reg(d, 0x004bfb, tmp);
537 ret = af9035_wr_reg(d, 0x00417f, tmp);
541 /* enable clock out */
542 ret = af9035_wr_reg_mask(d, 0x00d81a, 0x01, 0x01);
548 if (fw->data[0] == 0x01)
549 ret = af9035_download_firmware_old(d, fw);
551 ret = af9035_download_firmware_new(d, fw);
555 /* firmware loaded, request boot */
556 req.cmd = CMD_FW_BOOT;
557 ret = af9035_ctrl_msg(d, &req);
561 /* ensure firmware starts */
563 ret = af9035_ctrl_msg(d, &req_fw_ver);
567 if (!(rbuf[0] || rbuf[1] || rbuf[2] || rbuf[3])) {
568 dev_err(&d->udev->dev, "%s: firmware did not run\n",
574 dev_info(&d->udev->dev, "%s: firmware version=%d.%d.%d.%d",
575 KBUILD_MODNAME, rbuf[0], rbuf[1], rbuf[2], rbuf[3]);
580 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
585 static int af9035_read_config(struct dvb_usb_device *d)
587 struct state *state = d_to_priv(d);
592 /* demod I2C "address" */
593 state->af9033_config[0].i2c_addr = 0x38;
594 state->af9033_config[0].adc_multiplier = AF9033_ADC_MULTIPLIER_2X;
595 state->af9033_config[1].adc_multiplier = AF9033_ADC_MULTIPLIER_2X;
596 state->af9033_config[0].ts_mode = AF9033_TS_MODE_USB;
597 state->af9033_config[1].ts_mode = AF9033_TS_MODE_SERIAL;
599 /* eeprom memory mapped location */
600 if (state->chip_type == 0x9135) {
601 if (state->chip_version == 0x02) {
602 state->af9033_config[0].tuner = AF9033_TUNER_IT9135_60;
603 state->af9033_config[1].tuner = AF9033_TUNER_IT9135_60;
606 state->af9033_config[0].tuner = AF9033_TUNER_IT9135_38;
607 state->af9033_config[1].tuner = AF9033_TUNER_IT9135_38;
611 /* check if eeprom exists */
612 ret = af9035_rd_reg(d, tmp16, &tmp);
617 dev_dbg(&d->udev->dev, "%s: no eeprom\n", __func__);
622 /* check if there is dual tuners */
623 ret = af9035_rd_reg(d, state->eeprom_addr + EEPROM_DUAL_MODE, &tmp);
627 state->dual_mode = tmp;
628 dev_dbg(&d->udev->dev, "%s: dual mode=%d\n", __func__,
631 if (state->dual_mode) {
632 /* read 2nd demodulator I2C address */
633 ret = af9035_rd_reg(d,
634 state->eeprom_addr + EEPROM_2ND_DEMOD_ADDR,
639 state->af9033_config[1].i2c_addr = tmp;
640 dev_dbg(&d->udev->dev, "%s: 2nd demod I2C addr=%02x\n",
644 addr = state->eeprom_addr;
646 for (i = 0; i < state->dual_mode + 1; i++) {
648 ret = af9035_rd_reg(d, addr + EEPROM_1_TUNER_ID, &tmp);
653 dev_dbg(&d->udev->dev,
654 "%s: [%d]tuner not set, using default\n",
657 state->af9033_config[i].tuner = tmp;
659 dev_dbg(&d->udev->dev, "%s: [%d]tuner=%02x\n",
660 __func__, i, state->af9033_config[i].tuner);
662 switch (state->af9033_config[i].tuner) {
663 case AF9033_TUNER_TUA9001:
664 case AF9033_TUNER_FC0011:
665 case AF9033_TUNER_MXL5007T:
666 case AF9033_TUNER_TDA18218:
667 case AF9033_TUNER_FC2580:
668 case AF9033_TUNER_FC0012:
669 state->af9033_config[i].spec_inv = 1;
671 case AF9033_TUNER_IT9135_38:
672 case AF9033_TUNER_IT9135_51:
673 case AF9033_TUNER_IT9135_52:
674 case AF9033_TUNER_IT9135_60:
675 case AF9033_TUNER_IT9135_61:
676 case AF9033_TUNER_IT9135_62:
679 dev_warn(&d->udev->dev,
680 "%s: tuner id=%02x not supported, please report!",
681 KBUILD_MODNAME, tmp);
684 /* disable dual mode if driver does not support it */
686 switch (state->af9033_config[i].tuner) {
687 case AF9033_TUNER_FC0012:
688 case AF9033_TUNER_IT9135_38:
689 case AF9033_TUNER_IT9135_51:
690 case AF9033_TUNER_IT9135_52:
691 case AF9033_TUNER_IT9135_60:
692 case AF9033_TUNER_IT9135_61:
693 case AF9033_TUNER_IT9135_62:
694 case AF9033_TUNER_MXL5007T:
697 state->dual_mode = false;
698 dev_info(&d->udev->dev,
699 "%s: driver does not support 2nd tuner and will disable it",
703 /* tuner IF frequency */
704 ret = af9035_rd_reg(d, addr + EEPROM_1_IF_L, &tmp);
710 ret = af9035_rd_reg(d, addr + EEPROM_1_IF_H, &tmp);
716 dev_dbg(&d->udev->dev, "%s: [%d]IF=%d\n", __func__, i, tmp16);
718 addr += 0x10; /* shift for the 2nd tuner params */
722 /* get demod clock */
723 ret = af9035_rd_reg(d, 0x00d800, &tmp);
727 tmp = (tmp >> 0) & 0x0f;
729 for (i = 0; i < ARRAY_SIZE(state->af9033_config); i++) {
730 if (state->chip_type == 0x9135)
731 state->af9033_config[i].clock = clock_lut_it9135[tmp];
733 state->af9033_config[i].clock = clock_lut_af9035[tmp];
739 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
744 static int af9035_tua9001_tuner_callback(struct dvb_usb_device *d,
750 dev_dbg(&d->udev->dev, "%s: cmd=%d arg=%d\n", __func__, cmd, arg);
753 * CEN always enabled by hardware wiring
759 case TUA9001_CMD_RESETN:
765 ret = af9035_wr_reg_mask(d, 0x00d8e7, val, 0x01);
769 case TUA9001_CMD_RXEN:
775 ret = af9035_wr_reg_mask(d, 0x00d8eb, val, 0x01);
784 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
790 static int af9035_fc0011_tuner_callback(struct dvb_usb_device *d,
796 case FC0011_FE_CALLBACK_POWER:
798 ret = af9035_wr_reg_mask(d, 0xd8eb, 1, 1);
802 ret = af9035_wr_reg_mask(d, 0xd8ec, 1, 1);
806 ret = af9035_wr_reg_mask(d, 0xd8ed, 1, 1);
811 ret = af9035_wr_reg_mask(d, 0xd8d0, 1, 1);
815 ret = af9035_wr_reg_mask(d, 0xd8d1, 1, 1);
819 usleep_range(10000, 50000);
821 case FC0011_FE_CALLBACK_RESET:
822 ret = af9035_wr_reg(d, 0xd8e9, 1);
826 ret = af9035_wr_reg(d, 0xd8e8, 1);
830 ret = af9035_wr_reg(d, 0xd8e7, 1);
834 usleep_range(10000, 20000);
836 ret = af9035_wr_reg(d, 0xd8e7, 0);
840 usleep_range(10000, 20000);
850 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
855 static int af9035_tuner_callback(struct dvb_usb_device *d, int cmd, int arg)
857 struct state *state = d_to_priv(d);
859 switch (state->af9033_config[0].tuner) {
860 case AF9033_TUNER_FC0011:
861 return af9035_fc0011_tuner_callback(d, cmd, arg);
862 case AF9033_TUNER_TUA9001:
863 return af9035_tua9001_tuner_callback(d, cmd, arg);
871 static int af9035_frontend_callback(void *adapter_priv, int component,
874 struct i2c_adapter *adap = adapter_priv;
875 struct dvb_usb_device *d = i2c_get_adapdata(adap);
877 dev_dbg(&d->udev->dev, "%s: component=%d cmd=%d arg=%d\n",
878 __func__, component, cmd, arg);
881 case DVB_FRONTEND_COMPONENT_TUNER:
882 return af9035_tuner_callback(d, cmd, arg);
890 static int af9035_get_adapter_count(struct dvb_usb_device *d)
892 struct state *state = d_to_priv(d);
894 /* disable 2nd adapter as we don't have PID filters implemented */
895 if (d->udev->speed == USB_SPEED_FULL)
898 return state->dual_mode + 1;
901 static int af9035_frontend_attach(struct dvb_usb_adapter *adap)
903 struct state *state = adap_to_priv(adap);
904 struct dvb_usb_device *d = adap_to_d(adap);
906 dev_dbg(&d->udev->dev, "%s:\n", __func__);
908 if (!state->af9033_config[adap->id].tuner) {
909 /* unsupported tuner */
914 /* attach demodulator */
915 adap->fe[0] = dvb_attach(af9033_attach, &state->af9033_config[adap->id],
917 if (adap->fe[0] == NULL) {
922 /* disable I2C-gate */
923 adap->fe[0]->ops.i2c_gate_ctrl = NULL;
924 adap->fe[0]->callback = af9035_frontend_callback;
929 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
934 static struct tua9001_config af9035_tua9001_config = {
938 static const struct fc0011_config af9035_fc0011_config = {
942 static struct mxl5007t_config af9035_mxl5007t_config[] = {
944 .xtal_freq_hz = MxL_XTAL_24_MHZ,
945 .if_freq_hz = MxL_IF_4_57_MHZ,
947 .loop_thru_enable = 0,
949 .clk_out_amp = MxL_CLKOUT_AMP_0_94V,
951 .xtal_freq_hz = MxL_XTAL_24_MHZ,
952 .if_freq_hz = MxL_IF_4_57_MHZ,
954 .loop_thru_enable = 1,
956 .clk_out_amp = MxL_CLKOUT_AMP_0_94V,
960 static struct tda18218_config af9035_tda18218_config = {
965 static const struct fc2580_config af9035_fc2580_config = {
970 static const struct fc0012_config af9035_fc0012_config[] = {
973 .xtal_freq = FC_XTAL_36_MHZ,
975 .loop_through = true,
978 .i2c_address = 0x63 | 0x80, /* I2C bus select hack */
979 .xtal_freq = FC_XTAL_36_MHZ,
984 static int af9035_tuner_attach(struct dvb_usb_adapter *adap)
986 struct state *state = adap_to_priv(adap);
987 struct dvb_usb_device *d = adap_to_d(adap);
989 struct dvb_frontend *fe;
990 struct i2c_msg msg[1];
992 dev_dbg(&d->udev->dev, "%s:\n", __func__);
995 * XXX: Hack used in that function: we abuse unused I2C address bit [7]
996 * to carry info about used I2C bus for dual tuner configuration.
999 switch (state->af9033_config[adap->id].tuner) {
1000 case AF9033_TUNER_TUA9001:
1001 /* AF9035 gpiot3 = TUA9001 RESETN
1002 AF9035 gpiot2 = TUA9001 RXEN */
1004 /* configure gpiot2 and gpiot2 as output */
1005 ret = af9035_wr_reg_mask(d, 0x00d8ec, 0x01, 0x01);
1009 ret = af9035_wr_reg_mask(d, 0x00d8ed, 0x01, 0x01);
1013 ret = af9035_wr_reg_mask(d, 0x00d8e8, 0x01, 0x01);
1017 ret = af9035_wr_reg_mask(d, 0x00d8e9, 0x01, 0x01);
1022 fe = dvb_attach(tua9001_attach, adap->fe[0],
1023 &d->i2c_adap, &af9035_tua9001_config);
1025 case AF9033_TUNER_FC0011:
1026 fe = dvb_attach(fc0011_attach, adap->fe[0],
1027 &d->i2c_adap, &af9035_fc0011_config);
1029 case AF9033_TUNER_MXL5007T:
1030 if (adap->id == 0) {
1031 ret = af9035_wr_reg(d, 0x00d8e0, 1);
1035 ret = af9035_wr_reg(d, 0x00d8e1, 1);
1039 ret = af9035_wr_reg(d, 0x00d8df, 0);
1045 ret = af9035_wr_reg(d, 0x00d8df, 1);
1051 ret = af9035_wr_reg(d, 0x00d8c0, 1);
1055 ret = af9035_wr_reg(d, 0x00d8c1, 1);
1059 ret = af9035_wr_reg(d, 0x00d8bf, 0);
1063 ret = af9035_wr_reg(d, 0x00d8b4, 1);
1067 ret = af9035_wr_reg(d, 0x00d8b5, 1);
1071 ret = af9035_wr_reg(d, 0x00d8b3, 1);
1077 tuner_addr = 0x60 | 0x80; /* I2C bus hack */
1081 fe = dvb_attach(mxl5007t_attach, adap->fe[0], &d->i2c_adap,
1082 tuner_addr, &af9035_mxl5007t_config[adap->id]);
1084 case AF9033_TUNER_TDA18218:
1086 fe = dvb_attach(tda18218_attach, adap->fe[0],
1087 &d->i2c_adap, &af9035_tda18218_config);
1089 case AF9033_TUNER_FC2580:
1090 /* Tuner enable using gpiot2_o, gpiot2_en and gpiot2_on */
1091 ret = af9035_wr_reg_mask(d, 0xd8eb, 0x01, 0x01);
1095 ret = af9035_wr_reg_mask(d, 0xd8ec, 0x01, 0x01);
1099 ret = af9035_wr_reg_mask(d, 0xd8ed, 0x01, 0x01);
1103 usleep_range(10000, 50000);
1105 fe = dvb_attach(fc2580_attach, adap->fe[0],
1106 &d->i2c_adap, &af9035_fc2580_config);
1108 case AF9033_TUNER_FC0012:
1110 * AF9035 gpiot2 = FC0012 enable
1111 * XXX: there seems to be something on gpioh8 too, but on my
1112 * my test I didn't find any difference.
1115 if (adap->id == 0) {
1116 /* configure gpiot2 as output and high */
1117 ret = af9035_wr_reg_mask(d, 0xd8eb, 0x01, 0x01);
1121 ret = af9035_wr_reg_mask(d, 0xd8ec, 0x01, 0x01);
1125 ret = af9035_wr_reg_mask(d, 0xd8ed, 0x01, 0x01);
1130 * FIXME: That belongs for the FC0012 driver.
1131 * Write 02 to FC0012 master tuner register 0d directly
1132 * in order to make slave tuner working.
1137 msg[0].buf = "\x0d\x02";
1138 ret = i2c_transfer(&d->i2c_adap, msg, 1);
1143 usleep_range(10000, 50000);
1145 fe = dvb_attach(fc0012_attach, adap->fe[0], &d->i2c_adap,
1146 &af9035_fc0012_config[adap->id]);
1148 case AF9033_TUNER_IT9135_38:
1149 case AF9033_TUNER_IT9135_51:
1150 case AF9033_TUNER_IT9135_52:
1151 case AF9033_TUNER_IT9135_60:
1152 case AF9033_TUNER_IT9135_61:
1153 case AF9033_TUNER_IT9135_62:
1155 fe = dvb_attach(it913x_attach, adap->fe[0], &d->i2c_adap,
1156 state->af9033_config[adap->id].i2c_addr,
1157 state->af9033_config[0].tuner);
1171 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
1176 static int af9035_init(struct dvb_usb_device *d)
1178 struct state *state = d_to_priv(d);
1180 u16 frame_size = (d->udev->speed == USB_SPEED_FULL ? 5 : 87) * 188 / 4;
1181 u8 packet_size = (d->udev->speed == USB_SPEED_FULL ? 64 : 512) / 4;
1182 struct reg_val_mask tab[] = {
1183 { 0x80f99d, 0x01, 0x01 },
1184 { 0x80f9a4, 0x01, 0x01 },
1185 { 0x00dd11, 0x00, 0x20 },
1186 { 0x00dd11, 0x00, 0x40 },
1187 { 0x00dd13, 0x00, 0x20 },
1188 { 0x00dd13, 0x00, 0x40 },
1189 { 0x00dd11, 0x20, 0x20 },
1190 { 0x00dd88, (frame_size >> 0) & 0xff, 0xff},
1191 { 0x00dd89, (frame_size >> 8) & 0xff, 0xff},
1192 { 0x00dd0c, packet_size, 0xff},
1193 { 0x00dd11, state->dual_mode << 6, 0x40 },
1194 { 0x00dd8a, (frame_size >> 0) & 0xff, 0xff},
1195 { 0x00dd8b, (frame_size >> 8) & 0xff, 0xff},
1196 { 0x00dd0d, packet_size, 0xff },
1197 { 0x80f9a3, state->dual_mode, 0x01 },
1198 { 0x80f9cd, state->dual_mode, 0x01 },
1199 { 0x80f99d, 0x00, 0x01 },
1200 { 0x80f9a4, 0x00, 0x01 },
1203 dev_dbg(&d->udev->dev, "%s: USB speed=%d frame_size=%04x " \
1204 "packet_size=%02x\n", __func__,
1205 d->udev->speed, frame_size, packet_size);
1207 /* init endpoints */
1208 for (i = 0; i < ARRAY_SIZE(tab); i++) {
1209 ret = af9035_wr_reg_mask(d, tab[i].reg, tab[i].val,
1218 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
1223 #if IS_ENABLED(CONFIG_RC_CORE)
1224 static int af9035_rc_query(struct dvb_usb_device *d)
1229 struct usb_req req = { CMD_IR_GET, 0, 0, NULL, 4, buf };
1231 ret = af9035_ctrl_msg(d, &req);
1237 if ((buf[2] + buf[3]) == 0xff) {
1238 if ((buf[0] + buf[1]) == 0xff) {
1239 /* NEC standard 16bit */
1240 key = buf[0] << 8 | buf[2];
1242 /* NEC extended 24bit */
1243 key = buf[0] << 16 | buf[1] << 8 | buf[2];
1246 /* NEC full code 32bit */
1247 key = buf[0] << 24 | buf[1] << 16 | buf[2] << 8 | buf[3];
1250 dev_dbg(&d->udev->dev, "%s: %*ph\n", __func__, 4, buf);
1252 rc_keydown(d->rc_dev, key, 0);
1257 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
1262 static int af9035_get_rc_config(struct dvb_usb_device *d, struct dvb_usb_rc *rc)
1264 struct state *state = d_to_priv(d);
1268 ret = af9035_rd_reg(d, state->eeprom_addr + EEPROM_IR_MODE, &tmp);
1272 dev_dbg(&d->udev->dev, "%s: ir_mode=%02x\n", __func__, tmp);
1274 /* don't activate rc if in HID mode or if not available */
1276 ret = af9035_rd_reg(d, state->eeprom_addr + EEPROM_IR_TYPE,
1281 dev_dbg(&d->udev->dev, "%s: ir_type=%02x\n", __func__, tmp);
1286 rc->allowed_protos = RC_BIT_NEC;
1289 rc->allowed_protos = RC_BIT_RC6_MCE;
1293 rc->query = af9035_rc_query;
1296 /* load empty to enable rc */
1298 rc->map_name = RC_MAP_EMPTY;
1304 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
1309 #define af9035_get_rc_config NULL
1312 static int af9035_get_stream_config(struct dvb_frontend *fe, u8 *ts_type,
1313 struct usb_data_stream_properties *stream)
1315 struct dvb_usb_device *d = fe_to_d(fe);
1316 dev_dbg(&d->udev->dev, "%s: adap=%d\n", __func__, fe_to_adap(fe)->id);
1318 if (d->udev->speed == USB_SPEED_FULL)
1319 stream->u.bulk.buffersize = 5 * 188;
1325 * FIXME: PID filter is property of demodulator and should be moved to the
1326 * correct driver. Also we support only adapter #0 PID filter and will
1327 * disable adapter #1 if USB1.1 is used.
1329 static int af9035_pid_filter_ctrl(struct dvb_usb_adapter *adap, int onoff)
1331 struct dvb_usb_device *d = adap_to_d(adap);
1334 dev_dbg(&d->udev->dev, "%s: onoff=%d\n", __func__, onoff);
1336 ret = af9035_wr_reg_mask(d, 0x80f993, onoff, 0x01);
1343 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
1348 static int af9035_pid_filter(struct dvb_usb_adapter *adap, int index, u16 pid,
1351 struct dvb_usb_device *d = adap_to_d(adap);
1353 u8 wbuf[2] = {(pid >> 0) & 0xff, (pid >> 8) & 0xff};
1355 dev_dbg(&d->udev->dev, "%s: index=%d pid=%04x onoff=%d\n",
1356 __func__, index, pid, onoff);
1358 ret = af9035_wr_regs(d, 0x80f996, wbuf, 2);
1362 ret = af9035_wr_reg(d, 0x80f994, onoff);
1366 ret = af9035_wr_reg(d, 0x80f995, index);
1373 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
1378 static int af9035_probe(struct usb_interface *intf,
1379 const struct usb_device_id *id)
1381 struct usb_device *udev = interface_to_usbdev(intf);
1382 char manufacturer[sizeof("Afatech")];
1384 memset(manufacturer, 0, sizeof(manufacturer));
1385 usb_string(udev, udev->descriptor.iManufacturer,
1386 manufacturer, sizeof(manufacturer));
1388 * There is two devices having same ID but different chipset. One uses
1389 * AF9015 and the other IT9135 chipset. Only difference seen on lsusb
1390 * is iManufacturer string.
1392 * idVendor 0x0ccd TerraTec Electronic GmbH
1395 * iManufacturer 1 Afatech
1396 * iProduct 2 DVB-T 2
1398 * idVendor 0x0ccd TerraTec Electronic GmbH
1401 * iManufacturer 1 ITE Technologies, Inc.
1402 * iProduct 2 DVB-T TV Stick
1404 if ((le16_to_cpu(udev->descriptor.idVendor) == USB_VID_TERRATEC) &&
1405 (le16_to_cpu(udev->descriptor.idProduct) == 0x0099)) {
1406 if (!strcmp("Afatech", manufacturer)) {
1407 dev_dbg(&udev->dev, "%s: rejecting device\n", __func__);
1412 return dvb_usbv2_probe(intf, id);
1415 /* interface 0 is used by DVB-T receiver and
1416 interface 1 is for remote controller (HID) */
1417 static const struct dvb_usb_device_properties af9035_props = {
1418 .driver_name = KBUILD_MODNAME,
1419 .owner = THIS_MODULE,
1420 .adapter_nr = adapter_nr,
1421 .size_of_priv = sizeof(struct state),
1423 .generic_bulk_ctrl_endpoint = 0x02,
1424 .generic_bulk_ctrl_endpoint_response = 0x81,
1426 .identify_state = af9035_identify_state,
1427 .download_firmware = af9035_download_firmware,
1429 .i2c_algo = &af9035_i2c_algo,
1430 .read_config = af9035_read_config,
1431 .frontend_attach = af9035_frontend_attach,
1432 .tuner_attach = af9035_tuner_attach,
1433 .init = af9035_init,
1434 .get_rc_config = af9035_get_rc_config,
1435 .get_stream_config = af9035_get_stream_config,
1437 .get_adapter_count = af9035_get_adapter_count,
1440 .caps = DVB_USB_ADAP_HAS_PID_FILTER |
1441 DVB_USB_ADAP_PID_FILTER_CAN_BE_TURNED_OFF,
1443 .pid_filter_count = 32,
1444 .pid_filter_ctrl = af9035_pid_filter_ctrl,
1445 .pid_filter = af9035_pid_filter,
1447 .stream = DVB_USB_STREAM_BULK(0x84, 6, 87 * 188),
1449 .stream = DVB_USB_STREAM_BULK(0x85, 6, 87 * 188),
1454 static const struct usb_device_id af9035_id_table[] = {
1455 /* AF9035 devices */
1456 { DVB_USB_DEVICE(USB_VID_AFATECH, USB_PID_AFATECH_AF9035_9035,
1457 &af9035_props, "Afatech AF9035 reference design", NULL) },
1458 { DVB_USB_DEVICE(USB_VID_AFATECH, USB_PID_AFATECH_AF9035_1000,
1459 &af9035_props, "Afatech AF9035 reference design", NULL) },
1460 { DVB_USB_DEVICE(USB_VID_AFATECH, USB_PID_AFATECH_AF9035_1001,
1461 &af9035_props, "Afatech AF9035 reference design", NULL) },
1462 { DVB_USB_DEVICE(USB_VID_AFATECH, USB_PID_AFATECH_AF9035_1002,
1463 &af9035_props, "Afatech AF9035 reference design", NULL) },
1464 { DVB_USB_DEVICE(USB_VID_AFATECH, USB_PID_AFATECH_AF9035_1003,
1465 &af9035_props, "Afatech AF9035 reference design", NULL) },
1466 { DVB_USB_DEVICE(USB_VID_TERRATEC, USB_PID_TERRATEC_CINERGY_T_STICK,
1467 &af9035_props, "TerraTec Cinergy T Stick", NULL) },
1468 { DVB_USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_A835,
1469 &af9035_props, "AVerMedia AVerTV Volar HD/PRO (A835)", NULL) },
1470 { DVB_USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_B835,
1471 &af9035_props, "AVerMedia AVerTV Volar HD/PRO (A835)", NULL) },
1472 { DVB_USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_1867,
1473 &af9035_props, "AVerMedia HD Volar (A867)", NULL) },
1474 { DVB_USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_A867,
1475 &af9035_props, "AVerMedia HD Volar (A867)", NULL) },
1476 { DVB_USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_TWINSTAR,
1477 &af9035_props, "AVerMedia Twinstar (A825)", NULL) },
1478 { DVB_USB_DEVICE(USB_VID_ASUS, USB_PID_ASUS_U3100MINI_PLUS,
1479 &af9035_props, "Asus U3100Mini Plus", NULL) },
1480 { DVB_USB_DEVICE(USB_VID_TERRATEC, 0x00aa,
1481 &af9035_props, "TerraTec Cinergy T Stick (rev. 2)", NULL) },
1482 /* IT9135 devices */
1484 { DVB_USB_DEVICE(0x048d, 0x9135,
1485 &af9035_props, "IT9135 reference design", NULL) },
1486 { DVB_USB_DEVICE(0x048d, 0x9006,
1487 &af9035_props, "IT9135 reference design", NULL) },
1489 /* XXX: that same ID [0ccd:0099] is used by af9015 driver too */
1490 { DVB_USB_DEVICE(USB_VID_TERRATEC, 0x0099,
1491 &af9035_props, "TerraTec Cinergy T Stick Dual RC (rev. 2)", NULL) },
1494 MODULE_DEVICE_TABLE(usb, af9035_id_table);
1496 static struct usb_driver af9035_usb_driver = {
1497 .name = KBUILD_MODNAME,
1498 .id_table = af9035_id_table,
1499 .probe = af9035_probe,
1500 .disconnect = dvb_usbv2_disconnect,
1501 .suspend = dvb_usbv2_suspend,
1502 .resume = dvb_usbv2_resume,
1503 .reset_resume = dvb_usbv2_reset_resume,
1508 module_usb_driver(af9035_usb_driver);
1510 MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
1511 MODULE_DESCRIPTION("Afatech AF9035 driver");
1512 MODULE_LICENSE("GPL");
1513 MODULE_FIRMWARE(AF9035_FIRMWARE_AF9035);
1514 MODULE_FIRMWARE(AF9035_FIRMWARE_IT9135_V1);
1515 MODULE_FIRMWARE(AF9035_FIRMWARE_IT9135_V2);