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)
45 #define REQ_HDR_LEN 4 /* send header size */
46 #define ACK_HDR_LEN 3 /* rece header size */
47 #define CHECKSUM_LEN 2
48 #define USB_TIMEOUT 2000
49 struct state *state = d_to_priv(d);
52 u16 checksum, tmp_checksum;
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);
62 buf[0] = REQ_HDR_LEN + req->wlen + CHECKSUM_LEN - 1;
65 buf[3] = state->seq++;
66 memcpy(&buf[REQ_HDR_LEN], req->wbuf, req->wlen);
68 wlen = REQ_HDR_LEN + req->wlen + CHECKSUM_LEN;
69 rlen = ACK_HDR_LEN + req->rlen + CHECKSUM_LEN;
71 /* calc and add checksum */
72 checksum = af9035_checksum(buf, buf[0] - 1);
73 buf[buf[0] - 1] = (checksum >> 8);
74 buf[buf[0] - 0] = (checksum & 0xff);
76 /* no ack for these packets */
77 if (req->cmd == CMD_FW_DL)
80 ret = dvb_usbv2_generic_rw(d, buf, wlen, buf, rlen);
84 /* no ack for those packets */
85 if (req->cmd == CMD_FW_DL)
89 checksum = af9035_checksum(buf, rlen - 2);
90 tmp_checksum = (buf[rlen - 2] << 8) | buf[rlen - 1];
91 if (tmp_checksum != checksum) {
92 dev_err(&d->udev->dev, "%s: command=%02x checksum mismatch " \
93 "(%04x != %04x)\n", KBUILD_MODNAME, req->cmd,
94 tmp_checksum, checksum);
101 dev_dbg(&d->udev->dev, "%s: command=%02x failed fw error=%d\n",
102 __func__, req->cmd, buf[2]);
107 /* read request, copy returned data to return buf */
109 memcpy(req->rbuf, &buf[ACK_HDR_LEN], req->rlen);
115 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
120 /* write multiple registers */
121 static int af9035_wr_regs(struct dvb_usb_device *d, u32 reg, u8 *val, int len)
124 u8 mbox = (reg >> 16) & 0xff;
125 struct usb_req req = { CMD_MEM_WR, mbox, sizeof(wbuf), wbuf, 0, NULL };
131 wbuf[4] = (reg >> 8) & 0xff;
132 wbuf[5] = (reg >> 0) & 0xff;
133 memcpy(&wbuf[6], val, len);
135 return af9035_ctrl_msg(d, &req);
138 /* read multiple registers */
139 static int af9035_rd_regs(struct dvb_usb_device *d, u32 reg, u8 *val, int len)
141 u8 wbuf[] = { len, 2, 0, 0, (reg >> 8) & 0xff, reg & 0xff };
142 u8 mbox = (reg >> 16) & 0xff;
143 struct usb_req req = { CMD_MEM_RD, mbox, sizeof(wbuf), wbuf, len, val };
145 return af9035_ctrl_msg(d, &req);
148 /* write single register */
149 static int af9035_wr_reg(struct dvb_usb_device *d, u32 reg, u8 val)
151 return af9035_wr_regs(d, reg, &val, 1);
154 /* read single register */
155 static int af9035_rd_reg(struct dvb_usb_device *d, u32 reg, u8 *val)
157 return af9035_rd_regs(d, reg, val, 1);
160 /* write single register with mask */
161 static int af9035_wr_reg_mask(struct dvb_usb_device *d, u32 reg, u8 val,
167 /* no need for read if whole reg is written */
169 ret = af9035_rd_regs(d, reg, &tmp, 1);
178 return af9035_wr_regs(d, reg, &val, 1);
181 static int af9035_i2c_master_xfer(struct i2c_adapter *adap,
182 struct i2c_msg msg[], int num)
184 struct dvb_usb_device *d = i2c_get_adapdata(adap);
185 struct state *state = d_to_priv(d);
188 if (mutex_lock_interruptible(&d->i2c_mutex) < 0)
192 * I2C sub header is 5 bytes long. Meaning of those bytes are:
196 * byte 3 and 4 can be used as reg addr
198 * used when reg addr len is set to 2
200 * used when reg addr len is set to 1 or 2
202 * For the simplify we do not use register addr at all.
203 * NOTE: As a firmware knows tuner type there is very small possibility
204 * there could be some tuner I2C hacks done by firmware and this may
205 * lead problems if firmware expects those bytes are used.
207 if (num == 2 && !(msg[0].flags & I2C_M_RD) &&
208 (msg[1].flags & I2C_M_RD)) {
209 if (msg[0].len > 40 || msg[1].len > 40) {
210 /* TODO: correct limits > 40 */
212 } else if ((msg[0].addr == state->af9033_config[0].i2c_addr) ||
213 (msg[0].addr == state->af9033_config[1].i2c_addr)) {
214 /* demod access via firmware interface */
215 u32 reg = msg[0].buf[0] << 16 | msg[0].buf[1] << 8 |
218 if (msg[0].addr == state->af9033_config[1].i2c_addr)
221 ret = af9035_rd_regs(d, reg, &msg[1].buf[0],
225 u8 buf[5 + msg[0].len];
226 struct usb_req req = { CMD_I2C_RD, 0, sizeof(buf),
227 buf, msg[1].len, msg[1].buf };
228 req.mbox |= ((msg[0].addr & 0x80) >> 3);
230 buf[1] = msg[0].addr << 1;
231 buf[2] = 0x00; /* reg addr len */
232 buf[3] = 0x00; /* reg addr MSB */
233 buf[4] = 0x00; /* reg addr LSB */
234 memcpy(&buf[5], msg[0].buf, msg[0].len);
235 ret = af9035_ctrl_msg(d, &req);
237 } else if (num == 1 && !(msg[0].flags & I2C_M_RD)) {
238 if (msg[0].len > 40) {
239 /* TODO: correct limits > 40 */
241 } else if ((msg[0].addr == state->af9033_config[0].i2c_addr) ||
242 (msg[0].addr == state->af9033_config[1].i2c_addr)) {
243 /* demod access via firmware interface */
244 u32 reg = msg[0].buf[0] << 16 | msg[0].buf[1] << 8 |
247 if (msg[0].addr == state->af9033_config[1].i2c_addr)
250 ret = af9035_wr_regs(d, reg, &msg[0].buf[3],
254 u8 buf[5 + msg[0].len];
255 struct usb_req req = { CMD_I2C_WR, 0, sizeof(buf), buf,
257 req.mbox |= ((msg[0].addr & 0x80) >> 3);
259 buf[1] = msg[0].addr << 1;
260 buf[2] = 0x00; /* reg addr len */
261 buf[3] = 0x00; /* reg addr MSB */
262 buf[4] = 0x00; /* reg addr LSB */
263 memcpy(&buf[5], msg[0].buf, msg[0].len);
264 ret = af9035_ctrl_msg(d, &req);
268 * We support only two kind of I2C transactions:
269 * 1) 1 x read + 1 x write
275 mutex_unlock(&d->i2c_mutex);
283 static u32 af9035_i2c_functionality(struct i2c_adapter *adapter)
288 static struct i2c_algorithm af9035_i2c_algo = {
289 .master_xfer = af9035_i2c_master_xfer,
290 .functionality = af9035_i2c_functionality,
293 static int af9035_identify_state(struct dvb_usb_device *d, const char **name)
298 struct usb_req req = { CMD_FW_QUERYINFO, 0, sizeof(wbuf), wbuf,
299 sizeof(rbuf), rbuf };
301 ret = af9035_ctrl_msg(d, &req);
305 dev_dbg(&d->udev->dev, "%s: reply=%*ph\n", __func__, 4, rbuf);
306 if (rbuf[0] || rbuf[1] || rbuf[2] || rbuf[3])
314 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
319 static int af9035_download_firmware(struct dvb_usb_device *d,
320 const struct firmware *fw)
325 struct usb_req req = { 0, 0, 0, NULL, 0, NULL };
326 struct usb_req req_fw_dl = { CMD_FW_DL, 0, 0, wbuf, 0, NULL };
327 struct usb_req req_fw_ver = { CMD_FW_QUERYINFO, 0, 1, wbuf, 4, rbuf } ;
329 u16 hdr_addr, hdr_data_len, hdr_checksum;
334 * In case of dual tuner configuration we need to do some extra
335 * initialization in order to download firmware to slave demod too,
336 * which is done by master demod.
337 * Master feeds also clock and controls power via GPIO.
339 ret = af9035_rd_reg(d, EEPROM_DUAL_MODE, &tmp);
344 /* configure gpioh1, reset & power slave demod */
345 ret = af9035_wr_reg_mask(d, 0x00d8b0, 0x01, 0x01);
349 ret = af9035_wr_reg_mask(d, 0x00d8b1, 0x01, 0x01);
353 ret = af9035_wr_reg_mask(d, 0x00d8af, 0x00, 0x01);
357 usleep_range(10000, 50000);
359 ret = af9035_wr_reg_mask(d, 0x00d8af, 0x01, 0x01);
363 /* tell the slave I2C address */
364 ret = af9035_rd_reg(d, EEPROM_2ND_DEMOD_ADDR, &tmp);
368 ret = af9035_wr_reg(d, 0x00417f, tmp);
372 /* enable clock out */
373 ret = af9035_wr_reg_mask(d, 0x00d81a, 0x01, 0x01);
379 * Thanks to Daniel Glöckner <daniel-gl@gmx.net> about that info!
381 * byte 0: MCS 51 core
382 * There are two inside the AF9035 (1=Link and 2=OFDM) with separate
384 * byte 1-2: Big endian destination address
385 * byte 3-4: Big endian number of data bytes following the header
386 * byte 5-6: Big endian header checksum, apparently ignored by the chip
387 * Calculated as ~(h[0]*256+h[1]+h[2]*256+h[3]+h[4]*256)
390 for (i = fw->size; i > HDR_SIZE;) {
391 hdr_core = fw->data[fw->size - i + 0];
392 hdr_addr = fw->data[fw->size - i + 1] << 8;
393 hdr_addr |= fw->data[fw->size - i + 2] << 0;
394 hdr_data_len = fw->data[fw->size - i + 3] << 8;
395 hdr_data_len |= fw->data[fw->size - i + 4] << 0;
396 hdr_checksum = fw->data[fw->size - i + 5] << 8;
397 hdr_checksum |= fw->data[fw->size - i + 6] << 0;
399 dev_dbg(&d->udev->dev, "%s: core=%d addr=%04x data_len=%d " \
400 "checksum=%04x\n", __func__, hdr_core, hdr_addr,
401 hdr_data_len, hdr_checksum);
403 if (((hdr_core != 1) && (hdr_core != 2)) ||
404 (hdr_data_len > i)) {
405 dev_dbg(&d->udev->dev, "%s: bad firmware\n", __func__);
409 /* download begin packet */
410 req.cmd = CMD_FW_DL_BEGIN;
411 ret = af9035_ctrl_msg(d, &req);
415 /* download firmware packet(s) */
416 for (j = HDR_SIZE + hdr_data_len; j > 0; j -= MAX_DATA) {
420 req_fw_dl.wlen = len;
421 req_fw_dl.wbuf = (u8 *) &fw->data[fw->size - i +
422 HDR_SIZE + hdr_data_len - j];
423 ret = af9035_ctrl_msg(d, &req_fw_dl);
428 /* download end packet */
429 req.cmd = CMD_FW_DL_END;
430 ret = af9035_ctrl_msg(d, &req);
434 i -= hdr_data_len + HDR_SIZE;
436 dev_dbg(&d->udev->dev, "%s: data uploaded=%zu\n",
437 __func__, fw->size - i);
440 /* firmware loaded, request boot */
441 req.cmd = CMD_FW_BOOT;
442 ret = af9035_ctrl_msg(d, &req);
446 /* ensure firmware starts */
448 ret = af9035_ctrl_msg(d, &req_fw_ver);
452 if (!(rbuf[0] || rbuf[1] || rbuf[2] || rbuf[3])) {
453 dev_err(&d->udev->dev, "%s: firmware did not run\n",
459 dev_info(&d->udev->dev, "%s: firmware version=%d.%d.%d.%d",
460 KBUILD_MODNAME, rbuf[0], rbuf[1], rbuf[2], rbuf[3]);
465 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
470 static int af9035_download_firmware_it9135(struct dvb_usb_device *d,
471 const struct firmware *fw)
476 struct usb_req req = { 0, 0, 0, NULL, 0, NULL };
477 struct usb_req req_fw_dl = { CMD_FW_SCATTER_WR, 0, 0, NULL, 0, NULL };
478 struct usb_req req_fw_ver = { CMD_FW_QUERYINFO, 0, 1, wbuf, 4, rbuf } ;
482 * There seems to be following firmware header. Meaning of bytes 0-3
491 * 6: count of data bytes ?
494 for (i = HDR_SIZE, i_prev = 0; i <= fw->size; i++) {
496 (fw->data[i + 0] == 0x03 &&
497 (fw->data[i + 1] == 0x00 ||
498 fw->data[i + 1] == 0x01) &&
499 fw->data[i + 2] == 0x00)) {
500 req_fw_dl.wlen = i - i_prev;
501 req_fw_dl.wbuf = (u8 *) &fw->data[i_prev];
503 ret = af9035_ctrl_msg(d, &req_fw_dl);
507 dev_dbg(&d->udev->dev, "%s: data uploaded=%d\n",
512 /* firmware loaded, request boot */
513 req.cmd = CMD_FW_BOOT;
514 ret = af9035_ctrl_msg(d, &req);
518 /* ensure firmware starts */
520 ret = af9035_ctrl_msg(d, &req_fw_ver);
524 if (!(rbuf[0] || rbuf[1] || rbuf[2] || rbuf[3])) {
525 dev_err(&d->udev->dev, "%s: firmware did not run\n",
531 dev_info(&d->udev->dev, "%s: firmware version=%d.%d.%d.%d",
532 KBUILD_MODNAME, rbuf[0], rbuf[1], rbuf[2], rbuf[3]);
537 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
542 static int af9035_read_config(struct dvb_usb_device *d)
544 struct state *state = d_to_priv(d);
545 int ret, i, eeprom_shift = 0;
549 /* demod I2C "address" */
550 state->af9033_config[0].i2c_addr = 0x38;
552 /* check if there is dual tuners */
553 ret = af9035_rd_reg(d, EEPROM_DUAL_MODE, &tmp);
557 state->dual_mode = tmp;
558 dev_dbg(&d->udev->dev, "%s: dual mode=%d\n", __func__,
561 if (state->dual_mode) {
562 /* read 2nd demodulator I2C address */
563 ret = af9035_rd_reg(d, EEPROM_2ND_DEMOD_ADDR, &tmp);
567 state->af9033_config[1].i2c_addr = tmp;
568 dev_dbg(&d->udev->dev, "%s: 2nd demod I2C addr=%02x\n",
572 for (i = 0; i < state->dual_mode + 1; i++) {
574 ret = af9035_rd_reg(d, EEPROM_1_TUNER_ID + eeprom_shift, &tmp);
578 state->af9033_config[i].tuner = tmp;
579 dev_dbg(&d->udev->dev, "%s: [%d]tuner=%02x\n",
583 case AF9033_TUNER_TUA9001:
584 case AF9033_TUNER_FC0011:
585 case AF9033_TUNER_MXL5007T:
586 case AF9033_TUNER_TDA18218:
587 case AF9033_TUNER_FC2580:
588 case AF9033_TUNER_FC0012:
589 state->af9033_config[i].spec_inv = 1;
592 dev_warn(&d->udev->dev, "%s: tuner id=%02x not " \
593 "supported, please report!",
594 KBUILD_MODNAME, tmp);
597 /* disable dual mode if driver does not support it */
601 state->dual_mode = false;
602 dev_info(&d->udev->dev, "%s: driver does not " \
603 "support 2nd tuner and will " \
604 "disable it", KBUILD_MODNAME);
607 /* tuner IF frequency */
608 ret = af9035_rd_reg(d, EEPROM_1_IFFREQ_L + eeprom_shift, &tmp);
614 ret = af9035_rd_reg(d, EEPROM_1_IFFREQ_H + eeprom_shift, &tmp);
620 dev_dbg(&d->udev->dev, "%s: [%d]IF=%d\n", __func__, i, tmp16);
622 eeprom_shift = 0x10; /* shift for the 2nd tuner params */
625 /* get demod clock */
626 ret = af9035_rd_reg(d, 0x00d800, &tmp);
630 tmp = (tmp >> 0) & 0x0f;
632 for (i = 0; i < ARRAY_SIZE(state->af9033_config); i++)
633 state->af9033_config[i].clock = clock_lut[tmp];
638 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
643 static int af9035_read_config_it9135(struct dvb_usb_device *d)
645 struct state *state = d_to_priv(d);
649 state->dual_mode = false;
651 /* get demod clock */
652 ret = af9035_rd_reg(d, 0x00d800, &tmp);
656 tmp = (tmp >> 0) & 0x0f;
658 for (i = 0; i < ARRAY_SIZE(state->af9033_config); i++)
659 state->af9033_config[i].clock = clock_lut_it9135[tmp];
664 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
669 static int af9035_tua9001_tuner_callback(struct dvb_usb_device *d,
675 dev_dbg(&d->udev->dev, "%s: cmd=%d arg=%d\n", __func__, cmd, arg);
678 * CEN always enabled by hardware wiring
684 case TUA9001_CMD_RESETN:
690 ret = af9035_wr_reg_mask(d, 0x00d8e7, val, 0x01);
694 case TUA9001_CMD_RXEN:
700 ret = af9035_wr_reg_mask(d, 0x00d8eb, val, 0x01);
709 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
715 static int af9035_fc0011_tuner_callback(struct dvb_usb_device *d,
721 case FC0011_FE_CALLBACK_POWER:
723 ret = af9035_wr_reg_mask(d, 0xd8eb, 1, 1);
727 ret = af9035_wr_reg_mask(d, 0xd8ec, 1, 1);
731 ret = af9035_wr_reg_mask(d, 0xd8ed, 1, 1);
736 ret = af9035_wr_reg_mask(d, 0xd8d0, 1, 1);
740 ret = af9035_wr_reg_mask(d, 0xd8d1, 1, 1);
744 usleep_range(10000, 50000);
746 case FC0011_FE_CALLBACK_RESET:
747 ret = af9035_wr_reg(d, 0xd8e9, 1);
751 ret = af9035_wr_reg(d, 0xd8e8, 1);
755 ret = af9035_wr_reg(d, 0xd8e7, 1);
759 usleep_range(10000, 20000);
761 ret = af9035_wr_reg(d, 0xd8e7, 0);
765 usleep_range(10000, 20000);
775 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
780 static int af9035_tuner_callback(struct dvb_usb_device *d, int cmd, int arg)
782 struct state *state = d_to_priv(d);
784 switch (state->af9033_config[0].tuner) {
785 case AF9033_TUNER_FC0011:
786 return af9035_fc0011_tuner_callback(d, cmd, arg);
787 case AF9033_TUNER_TUA9001:
788 return af9035_tua9001_tuner_callback(d, cmd, arg);
796 static int af9035_frontend_callback(void *adapter_priv, int component,
799 struct i2c_adapter *adap = adapter_priv;
800 struct dvb_usb_device *d = i2c_get_adapdata(adap);
802 dev_dbg(&d->udev->dev, "%s: component=%d cmd=%d arg=%d\n",
803 __func__, component, cmd, arg);
806 case DVB_FRONTEND_COMPONENT_TUNER:
807 return af9035_tuner_callback(d, cmd, arg);
815 static int af9035_get_adapter_count(struct dvb_usb_device *d)
817 struct state *state = d_to_priv(d);
818 return state->dual_mode + 1;
821 static int af9035_frontend_attach(struct dvb_usb_adapter *adap)
823 struct state *state = adap_to_priv(adap);
824 struct dvb_usb_device *d = adap_to_d(adap);
827 if (!state->af9033_config[adap->id].tuner) {
828 /* unsupported tuner */
834 state->af9033_config[0].ts_mode = AF9033_TS_MODE_USB;
835 state->af9033_config[1].ts_mode = AF9033_TS_MODE_SERIAL;
837 ret = af9035_wr_reg(d, 0x00417f,
838 state->af9033_config[1].i2c_addr);
842 ret = af9035_wr_reg(d, 0x00d81a, state->dual_mode);
847 /* attach demodulator */
848 adap->fe[0] = dvb_attach(af9033_attach, &state->af9033_config[adap->id],
850 if (adap->fe[0] == NULL) {
855 /* disable I2C-gate */
856 adap->fe[0]->ops.i2c_gate_ctrl = NULL;
857 adap->fe[0]->callback = af9035_frontend_callback;
862 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
867 static struct tua9001_config af9035_tua9001_config = {
871 static const struct fc0011_config af9035_fc0011_config = {
875 static struct mxl5007t_config af9035_mxl5007t_config[] = {
877 .xtal_freq_hz = MxL_XTAL_24_MHZ,
878 .if_freq_hz = MxL_IF_4_57_MHZ,
880 .loop_thru_enable = 0,
882 .clk_out_amp = MxL_CLKOUT_AMP_0_94V,
884 .xtal_freq_hz = MxL_XTAL_24_MHZ,
885 .if_freq_hz = MxL_IF_4_57_MHZ,
887 .loop_thru_enable = 1,
889 .clk_out_amp = MxL_CLKOUT_AMP_0_94V,
893 static struct tda18218_config af9035_tda18218_config = {
898 static const struct fc2580_config af9035_fc2580_config = {
903 static const struct fc0012_config af9035_fc0012_config = {
905 .xtal_freq = FC_XTAL_36_MHZ,
909 static int af9035_tuner_attach(struct dvb_usb_adapter *adap)
911 struct state *state = adap_to_priv(adap);
912 struct dvb_usb_device *d = adap_to_d(adap);
914 struct dvb_frontend *fe;
917 * XXX: Hack used in that function: we abuse unused I2C address bit [7]
918 * to carry info about used I2C bus for dual tuner configuration.
921 switch (state->af9033_config[adap->id].tuner) {
922 case AF9033_TUNER_TUA9001:
923 /* AF9035 gpiot3 = TUA9001 RESETN
924 AF9035 gpiot2 = TUA9001 RXEN */
926 /* configure gpiot2 and gpiot2 as output */
927 ret = af9035_wr_reg_mask(d, 0x00d8ec, 0x01, 0x01);
931 ret = af9035_wr_reg_mask(d, 0x00d8ed, 0x01, 0x01);
935 ret = af9035_wr_reg_mask(d, 0x00d8e8, 0x01, 0x01);
939 ret = af9035_wr_reg_mask(d, 0x00d8e9, 0x01, 0x01);
944 fe = dvb_attach(tua9001_attach, adap->fe[0],
945 &d->i2c_adap, &af9035_tua9001_config);
947 case AF9033_TUNER_FC0011:
948 fe = dvb_attach(fc0011_attach, adap->fe[0],
949 &d->i2c_adap, &af9035_fc0011_config);
951 case AF9033_TUNER_MXL5007T:
953 ret = af9035_wr_reg(d, 0x00d8e0, 1);
957 ret = af9035_wr_reg(d, 0x00d8e1, 1);
961 ret = af9035_wr_reg(d, 0x00d8df, 0);
967 ret = af9035_wr_reg(d, 0x00d8df, 1);
973 ret = af9035_wr_reg(d, 0x00d8c0, 1);
977 ret = af9035_wr_reg(d, 0x00d8c1, 1);
981 ret = af9035_wr_reg(d, 0x00d8bf, 0);
985 ret = af9035_wr_reg(d, 0x00d8b4, 1);
989 ret = af9035_wr_reg(d, 0x00d8b5, 1);
993 ret = af9035_wr_reg(d, 0x00d8b3, 1);
999 tuner_addr = 0x60 | 0x80; /* I2C bus hack */
1003 fe = dvb_attach(mxl5007t_attach, adap->fe[0], &d->i2c_adap,
1004 tuner_addr, &af9035_mxl5007t_config[adap->id]);
1006 case AF9033_TUNER_TDA18218:
1008 fe = dvb_attach(tda18218_attach, adap->fe[0],
1009 &d->i2c_adap, &af9035_tda18218_config);
1011 case AF9033_TUNER_FC2580:
1012 /* Tuner enable using gpiot2_o, gpiot2_en and gpiot2_on */
1013 ret = af9035_wr_reg_mask(d, 0xd8eb, 0x01, 0x01);
1017 ret = af9035_wr_reg_mask(d, 0xd8ec, 0x01, 0x01);
1021 ret = af9035_wr_reg_mask(d, 0xd8ed, 0x01, 0x01);
1025 usleep_range(10000, 50000);
1027 fe = dvb_attach(fc2580_attach, adap->fe[0],
1028 &d->i2c_adap, &af9035_fc2580_config);
1030 case AF9033_TUNER_FC0012:
1032 * AF9035 gpiot2 = FC0012 enable
1033 * XXX: there seems to be something on gpioh8 too, but on my
1034 * my test I didn't find any difference.
1037 /* configure gpiot2 as output and high */
1038 ret = af9035_wr_reg_mask(d, 0xd8eb, 0x01, 0x01);
1042 ret = af9035_wr_reg_mask(d, 0xd8ec, 0x01, 0x01);
1046 ret = af9035_wr_reg_mask(d, 0xd8ed, 0x01, 0x01);
1050 usleep_range(10000, 50000);
1052 fe = dvb_attach(fc0012_attach, adap->fe[0], &d->i2c_adap,
1053 &af9035_fc0012_config);
1067 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
1072 static int af9035_init(struct dvb_usb_device *d)
1074 struct state *state = d_to_priv(d);
1076 u16 frame_size = 87 * 188 / 4;
1077 u8 packet_size = 512 / 4;
1078 struct reg_val_mask tab[] = {
1079 { 0x80f99d, 0x01, 0x01 },
1080 { 0x80f9a4, 0x01, 0x01 },
1081 { 0x00dd11, 0x00, 0x20 },
1082 { 0x00dd11, 0x00, 0x40 },
1083 { 0x00dd13, 0x00, 0x20 },
1084 { 0x00dd13, 0x00, 0x40 },
1085 { 0x00dd11, 0x20, 0x20 },
1086 { 0x00dd88, (frame_size >> 0) & 0xff, 0xff},
1087 { 0x00dd89, (frame_size >> 8) & 0xff, 0xff},
1088 { 0x00dd0c, packet_size, 0xff},
1089 { 0x00dd11, state->dual_mode << 6, 0x40 },
1090 { 0x00dd8a, (frame_size >> 0) & 0xff, 0xff},
1091 { 0x00dd8b, (frame_size >> 8) & 0xff, 0xff},
1092 { 0x00dd0d, packet_size, 0xff },
1093 { 0x80f9a3, state->dual_mode, 0x01 },
1094 { 0x80f9cd, state->dual_mode, 0x01 },
1095 { 0x80f99d, 0x00, 0x01 },
1096 { 0x80f9a4, 0x00, 0x01 },
1099 dev_dbg(&d->udev->dev, "%s: USB speed=%d frame_size=%04x " \
1100 "packet_size=%02x\n", __func__,
1101 d->udev->speed, frame_size, packet_size);
1103 /* init endpoints */
1104 for (i = 0; i < ARRAY_SIZE(tab); i++) {
1105 ret = af9035_wr_reg_mask(d, tab[i].reg, tab[i].val,
1114 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
1119 static int af9035_rc_query(struct dvb_usb_device *d)
1124 struct usb_req req = { CMD_IR_GET, 0, 0, NULL, 4, b };
1126 ret = af9035_ctrl_msg(d, &req);
1130 if ((b[2] + b[3]) == 0xff) {
1131 if ((b[0] + b[1]) == 0xff) {
1133 key = b[0] << 8 | b[2];
1136 key = b[0] << 16 | b[1] << 8 | b[2];
1139 key = b[0] << 24 | b[1] << 16 | b[2] << 8 | b[3];
1142 rc_keydown(d->rc_dev, key, 0);
1149 static int af9035_get_rc_config(struct dvb_usb_device *d, struct dvb_usb_rc *rc)
1154 ret = af9035_rd_reg(d, EEPROM_IR_MODE, &tmp);
1158 dev_dbg(&d->udev->dev, "%s: ir_mode=%02x\n", __func__, tmp);
1160 /* don't activate rc if in HID mode or if not available */
1162 ret = af9035_rd_reg(d, EEPROM_IR_TYPE, &tmp);
1166 dev_dbg(&d->udev->dev, "%s: ir_type=%02x\n", __func__, tmp);
1171 rc->allowed_protos = RC_BIT_NEC;
1174 rc->allowed_protos = RC_BIT_RC6_MCE;
1178 rc->query = af9035_rc_query;
1181 /* load empty to enable rc */
1183 rc->map_name = RC_MAP_EMPTY;
1189 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
1194 /* interface 0 is used by DVB-T receiver and
1195 interface 1 is for remote controller (HID) */
1196 static const struct dvb_usb_device_properties af9035_props = {
1197 .driver_name = KBUILD_MODNAME,
1198 .owner = THIS_MODULE,
1199 .adapter_nr = adapter_nr,
1200 .size_of_priv = sizeof(struct state),
1202 .generic_bulk_ctrl_endpoint = 0x02,
1203 .generic_bulk_ctrl_endpoint_response = 0x81,
1205 .identify_state = af9035_identify_state,
1206 .firmware = AF9035_FIRMWARE_AF9035,
1207 .download_firmware = af9035_download_firmware,
1209 .i2c_algo = &af9035_i2c_algo,
1210 .read_config = af9035_read_config,
1211 .frontend_attach = af9035_frontend_attach,
1212 .tuner_attach = af9035_tuner_attach,
1213 .init = af9035_init,
1214 .get_rc_config = af9035_get_rc_config,
1216 .get_adapter_count = af9035_get_adapter_count,
1219 .stream = DVB_USB_STREAM_BULK(0x84, 6, 87 * 188),
1221 .stream = DVB_USB_STREAM_BULK(0x85, 6, 87 * 188),
1226 static const struct dvb_usb_device_properties it9135_props = {
1227 .driver_name = KBUILD_MODNAME,
1228 .owner = THIS_MODULE,
1229 .adapter_nr = adapter_nr,
1230 .size_of_priv = sizeof(struct state),
1232 .generic_bulk_ctrl_endpoint = 0x02,
1233 .generic_bulk_ctrl_endpoint_response = 0x81,
1235 .identify_state = af9035_identify_state,
1236 .firmware = AF9035_FIRMWARE_IT9135,
1237 .download_firmware = af9035_download_firmware_it9135,
1239 .i2c_algo = &af9035_i2c_algo,
1240 .read_config = af9035_read_config_it9135,
1241 .frontend_attach = af9035_frontend_attach,
1242 .tuner_attach = af9035_tuner_attach,
1243 .init = af9035_init,
1244 .get_rc_config = af9035_get_rc_config,
1249 .stream = DVB_USB_STREAM_BULK(0x84, 6, 87 * 188),
1251 .stream = DVB_USB_STREAM_BULK(0x85, 6, 87 * 188),
1256 static const struct usb_device_id af9035_id_table[] = {
1257 { DVB_USB_DEVICE(USB_VID_AFATECH, USB_PID_AFATECH_AF9035_9035,
1258 &af9035_props, "Afatech AF9035 reference design", NULL) },
1259 { DVB_USB_DEVICE(USB_VID_AFATECH, USB_PID_AFATECH_AF9035_1000,
1260 &af9035_props, "Afatech AF9035 reference design", NULL) },
1261 { DVB_USB_DEVICE(USB_VID_AFATECH, USB_PID_AFATECH_AF9035_1001,
1262 &af9035_props, "Afatech AF9035 reference design", NULL) },
1263 { DVB_USB_DEVICE(USB_VID_AFATECH, USB_PID_AFATECH_AF9035_1002,
1264 &af9035_props, "Afatech AF9035 reference design", NULL) },
1265 { DVB_USB_DEVICE(USB_VID_AFATECH, USB_PID_AFATECH_AF9035_1003,
1266 &af9035_props, "Afatech AF9035 reference design", NULL) },
1267 { DVB_USB_DEVICE(USB_VID_TERRATEC, USB_PID_TERRATEC_CINERGY_T_STICK,
1268 &af9035_props, "TerraTec Cinergy T Stick", NULL) },
1269 { DVB_USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_A835,
1270 &af9035_props, "AVerMedia AVerTV Volar HD/PRO (A835)", NULL) },
1271 { DVB_USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_B835,
1272 &af9035_props, "AVerMedia AVerTV Volar HD/PRO (A835)", NULL) },
1273 { DVB_USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_1867,
1274 &af9035_props, "AVerMedia HD Volar (A867)", NULL) },
1275 { DVB_USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_A867,
1276 &af9035_props, "AVerMedia HD Volar (A867)", NULL) },
1277 { DVB_USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_TWINSTAR,
1278 &af9035_props, "AVerMedia Twinstar (A825)", NULL) },
1279 { DVB_USB_DEVICE(USB_VID_ASUS, USB_PID_ASUS_U3100MINI_PLUS,
1280 &af9035_props, "Asus U3100Mini Plus", NULL) },
1283 MODULE_DEVICE_TABLE(usb, af9035_id_table);
1285 static struct usb_driver af9035_usb_driver = {
1286 .name = KBUILD_MODNAME,
1287 .id_table = af9035_id_table,
1288 .probe = dvb_usbv2_probe,
1289 .disconnect = dvb_usbv2_disconnect,
1290 .suspend = dvb_usbv2_suspend,
1291 .resume = dvb_usbv2_resume,
1292 .reset_resume = dvb_usbv2_reset_resume,
1297 module_usb_driver(af9035_usb_driver);
1299 MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
1300 MODULE_DESCRIPTION("Afatech AF9035 driver");
1301 MODULE_LICENSE("GPL");
1302 MODULE_FIRMWARE(AF9035_FIRMWARE_AF9035);
1303 MODULE_FIRMWARE(AF9035_FIRMWARE_IT9135);