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 /* print warn if firmware is bad, continue and see what happens */
442 dev_warn(&d->udev->dev, "%s: bad firmware\n", KBUILD_MODNAME);
444 /* firmware loaded, request boot */
445 req.cmd = CMD_FW_BOOT;
446 ret = af9035_ctrl_msg(d, &req);
450 /* ensure firmware starts */
452 ret = af9035_ctrl_msg(d, &req_fw_ver);
456 if (!(rbuf[0] || rbuf[1] || rbuf[2] || rbuf[3])) {
457 dev_err(&d->udev->dev, "%s: firmware did not run\n",
463 dev_info(&d->udev->dev, "%s: firmware version=%d.%d.%d.%d",
464 KBUILD_MODNAME, rbuf[0], rbuf[1], rbuf[2], rbuf[3]);
469 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
474 static int af9035_download_firmware_it9135(struct dvb_usb_device *d,
475 const struct firmware *fw)
480 struct usb_req req = { 0, 0, 0, NULL, 0, NULL };
481 struct usb_req req_fw_dl = { CMD_FW_SCATTER_WR, 0, 0, NULL, 0, NULL };
482 struct usb_req req_fw_ver = { CMD_FW_QUERYINFO, 0, 1, wbuf, 4, rbuf } ;
486 * There seems to be following firmware header. Meaning of bytes 0-3
495 * 6: count of data bytes ?
498 for (i = HDR_SIZE, i_prev = 0; i <= fw->size; i++) {
500 (fw->data[i + 0] == 0x03 &&
501 (fw->data[i + 1] == 0x00 ||
502 fw->data[i + 1] == 0x01) &&
503 fw->data[i + 2] == 0x00)) {
504 req_fw_dl.wlen = i - i_prev;
505 req_fw_dl.wbuf = (u8 *) &fw->data[i_prev];
507 ret = af9035_ctrl_msg(d, &req_fw_dl);
511 dev_dbg(&d->udev->dev, "%s: data uploaded=%d\n",
516 /* firmware loaded, request boot */
517 req.cmd = CMD_FW_BOOT;
518 ret = af9035_ctrl_msg(d, &req);
522 /* ensure firmware starts */
524 ret = af9035_ctrl_msg(d, &req_fw_ver);
528 if (!(rbuf[0] || rbuf[1] || rbuf[2] || rbuf[3])) {
529 dev_err(&d->udev->dev, "%s: firmware did not run\n",
535 dev_info(&d->udev->dev, "%s: firmware version=%d.%d.%d.%d",
536 KBUILD_MODNAME, rbuf[0], rbuf[1], rbuf[2], rbuf[3]);
541 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
546 static int af9035_read_config(struct dvb_usb_device *d)
548 struct state *state = d_to_priv(d);
549 int ret, i, eeprom_shift = 0;
553 /* demod I2C "address" */
554 state->af9033_config[0].i2c_addr = 0x38;
556 /* check if there is dual tuners */
557 ret = af9035_rd_reg(d, EEPROM_DUAL_MODE, &tmp);
561 state->dual_mode = tmp;
562 dev_dbg(&d->udev->dev, "%s: dual mode=%d\n", __func__,
565 if (state->dual_mode) {
566 /* read 2nd demodulator I2C address */
567 ret = af9035_rd_reg(d, EEPROM_2ND_DEMOD_ADDR, &tmp);
571 state->af9033_config[1].i2c_addr = tmp;
572 dev_dbg(&d->udev->dev, "%s: 2nd demod I2C addr=%02x\n",
576 for (i = 0; i < state->dual_mode + 1; i++) {
578 ret = af9035_rd_reg(d, EEPROM_1_TUNER_ID + eeprom_shift, &tmp);
582 state->af9033_config[i].tuner = tmp;
583 dev_dbg(&d->udev->dev, "%s: [%d]tuner=%02x\n",
587 case AF9033_TUNER_TUA9001:
588 case AF9033_TUNER_FC0011:
589 case AF9033_TUNER_MXL5007T:
590 case AF9033_TUNER_TDA18218:
591 case AF9033_TUNER_FC2580:
592 case AF9033_TUNER_FC0012:
593 state->af9033_config[i].spec_inv = 1;
596 dev_warn(&d->udev->dev, "%s: tuner id=%02x not " \
597 "supported, please report!",
598 KBUILD_MODNAME, tmp);
601 /* disable dual mode if driver does not support it */
604 case AF9033_TUNER_FC0012:
607 state->dual_mode = false;
608 dev_info(&d->udev->dev, "%s: driver does not " \
609 "support 2nd tuner and will " \
610 "disable it", KBUILD_MODNAME);
613 /* tuner IF frequency */
614 ret = af9035_rd_reg(d, EEPROM_1_IFFREQ_L + eeprom_shift, &tmp);
620 ret = af9035_rd_reg(d, EEPROM_1_IFFREQ_H + eeprom_shift, &tmp);
626 dev_dbg(&d->udev->dev, "%s: [%d]IF=%d\n", __func__, i, tmp16);
628 eeprom_shift = 0x10; /* shift for the 2nd tuner params */
631 /* get demod clock */
632 ret = af9035_rd_reg(d, 0x00d800, &tmp);
636 tmp = (tmp >> 0) & 0x0f;
638 for (i = 0; i < ARRAY_SIZE(state->af9033_config); i++)
639 state->af9033_config[i].clock = clock_lut[tmp];
644 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
649 static int af9035_read_config_it9135(struct dvb_usb_device *d)
651 struct state *state = d_to_priv(d);
655 state->dual_mode = false;
657 /* get demod clock */
658 ret = af9035_rd_reg(d, 0x00d800, &tmp);
662 tmp = (tmp >> 0) & 0x0f;
664 for (i = 0; i < ARRAY_SIZE(state->af9033_config); i++)
665 state->af9033_config[i].clock = clock_lut_it9135[tmp];
670 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
675 static int af9035_tua9001_tuner_callback(struct dvb_usb_device *d,
681 dev_dbg(&d->udev->dev, "%s: cmd=%d arg=%d\n", __func__, cmd, arg);
684 * CEN always enabled by hardware wiring
690 case TUA9001_CMD_RESETN:
696 ret = af9035_wr_reg_mask(d, 0x00d8e7, val, 0x01);
700 case TUA9001_CMD_RXEN:
706 ret = af9035_wr_reg_mask(d, 0x00d8eb, val, 0x01);
715 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
721 static int af9035_fc0011_tuner_callback(struct dvb_usb_device *d,
727 case FC0011_FE_CALLBACK_POWER:
729 ret = af9035_wr_reg_mask(d, 0xd8eb, 1, 1);
733 ret = af9035_wr_reg_mask(d, 0xd8ec, 1, 1);
737 ret = af9035_wr_reg_mask(d, 0xd8ed, 1, 1);
742 ret = af9035_wr_reg_mask(d, 0xd8d0, 1, 1);
746 ret = af9035_wr_reg_mask(d, 0xd8d1, 1, 1);
750 usleep_range(10000, 50000);
752 case FC0011_FE_CALLBACK_RESET:
753 ret = af9035_wr_reg(d, 0xd8e9, 1);
757 ret = af9035_wr_reg(d, 0xd8e8, 1);
761 ret = af9035_wr_reg(d, 0xd8e7, 1);
765 usleep_range(10000, 20000);
767 ret = af9035_wr_reg(d, 0xd8e7, 0);
771 usleep_range(10000, 20000);
781 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
786 static int af9035_tuner_callback(struct dvb_usb_device *d, int cmd, int arg)
788 struct state *state = d_to_priv(d);
790 switch (state->af9033_config[0].tuner) {
791 case AF9033_TUNER_FC0011:
792 return af9035_fc0011_tuner_callback(d, cmd, arg);
793 case AF9033_TUNER_TUA9001:
794 return af9035_tua9001_tuner_callback(d, cmd, arg);
802 static int af9035_frontend_callback(void *adapter_priv, int component,
805 struct i2c_adapter *adap = adapter_priv;
806 struct dvb_usb_device *d = i2c_get_adapdata(adap);
808 dev_dbg(&d->udev->dev, "%s: component=%d cmd=%d arg=%d\n",
809 __func__, component, cmd, arg);
812 case DVB_FRONTEND_COMPONENT_TUNER:
813 return af9035_tuner_callback(d, cmd, arg);
821 static int af9035_get_adapter_count(struct dvb_usb_device *d)
823 struct state *state = d_to_priv(d);
824 return state->dual_mode + 1;
827 static int af9035_frontend_attach(struct dvb_usb_adapter *adap)
829 struct state *state = adap_to_priv(adap);
830 struct dvb_usb_device *d = adap_to_d(adap);
833 if (!state->af9033_config[adap->id].tuner) {
834 /* unsupported tuner */
840 state->af9033_config[0].ts_mode = AF9033_TS_MODE_USB;
841 state->af9033_config[1].ts_mode = AF9033_TS_MODE_SERIAL;
843 ret = af9035_wr_reg(d, 0x00417f,
844 state->af9033_config[1].i2c_addr);
848 ret = af9035_wr_reg(d, 0x00d81a, state->dual_mode);
853 /* attach demodulator */
854 adap->fe[0] = dvb_attach(af9033_attach, &state->af9033_config[adap->id],
856 if (adap->fe[0] == NULL) {
861 /* disable I2C-gate */
862 adap->fe[0]->ops.i2c_gate_ctrl = NULL;
863 adap->fe[0]->callback = af9035_frontend_callback;
868 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
873 static struct tua9001_config af9035_tua9001_config = {
877 static const struct fc0011_config af9035_fc0011_config = {
881 static struct mxl5007t_config af9035_mxl5007t_config[] = {
883 .xtal_freq_hz = MxL_XTAL_24_MHZ,
884 .if_freq_hz = MxL_IF_4_57_MHZ,
886 .loop_thru_enable = 0,
888 .clk_out_amp = MxL_CLKOUT_AMP_0_94V,
890 .xtal_freq_hz = MxL_XTAL_24_MHZ,
891 .if_freq_hz = MxL_IF_4_57_MHZ,
893 .loop_thru_enable = 1,
895 .clk_out_amp = MxL_CLKOUT_AMP_0_94V,
899 static struct tda18218_config af9035_tda18218_config = {
904 static const struct fc2580_config af9035_fc2580_config = {
909 static const struct fc0012_config af9035_fc0012_config[] = {
912 .xtal_freq = FC_XTAL_36_MHZ,
914 .loop_through = true,
917 .i2c_address = 0x63 | 0x80, /* I2C bus select hack */
918 .xtal_freq = FC_XTAL_36_MHZ,
923 static int af9035_tuner_attach(struct dvb_usb_adapter *adap)
925 struct state *state = adap_to_priv(adap);
926 struct dvb_usb_device *d = adap_to_d(adap);
928 struct dvb_frontend *fe;
929 struct i2c_msg msg[1];
932 * XXX: Hack used in that function: we abuse unused I2C address bit [7]
933 * to carry info about used I2C bus for dual tuner configuration.
936 switch (state->af9033_config[adap->id].tuner) {
937 case AF9033_TUNER_TUA9001:
938 /* AF9035 gpiot3 = TUA9001 RESETN
939 AF9035 gpiot2 = TUA9001 RXEN */
941 /* configure gpiot2 and gpiot2 as output */
942 ret = af9035_wr_reg_mask(d, 0x00d8ec, 0x01, 0x01);
946 ret = af9035_wr_reg_mask(d, 0x00d8ed, 0x01, 0x01);
950 ret = af9035_wr_reg_mask(d, 0x00d8e8, 0x01, 0x01);
954 ret = af9035_wr_reg_mask(d, 0x00d8e9, 0x01, 0x01);
959 fe = dvb_attach(tua9001_attach, adap->fe[0],
960 &d->i2c_adap, &af9035_tua9001_config);
962 case AF9033_TUNER_FC0011:
963 fe = dvb_attach(fc0011_attach, adap->fe[0],
964 &d->i2c_adap, &af9035_fc0011_config);
966 case AF9033_TUNER_MXL5007T:
968 ret = af9035_wr_reg(d, 0x00d8e0, 1);
972 ret = af9035_wr_reg(d, 0x00d8e1, 1);
976 ret = af9035_wr_reg(d, 0x00d8df, 0);
982 ret = af9035_wr_reg(d, 0x00d8df, 1);
988 ret = af9035_wr_reg(d, 0x00d8c0, 1);
992 ret = af9035_wr_reg(d, 0x00d8c1, 1);
996 ret = af9035_wr_reg(d, 0x00d8bf, 0);
1000 ret = af9035_wr_reg(d, 0x00d8b4, 1);
1004 ret = af9035_wr_reg(d, 0x00d8b5, 1);
1008 ret = af9035_wr_reg(d, 0x00d8b3, 1);
1014 tuner_addr = 0x60 | 0x80; /* I2C bus hack */
1018 fe = dvb_attach(mxl5007t_attach, adap->fe[0], &d->i2c_adap,
1019 tuner_addr, &af9035_mxl5007t_config[adap->id]);
1021 case AF9033_TUNER_TDA18218:
1023 fe = dvb_attach(tda18218_attach, adap->fe[0],
1024 &d->i2c_adap, &af9035_tda18218_config);
1026 case AF9033_TUNER_FC2580:
1027 /* Tuner enable using gpiot2_o, gpiot2_en and gpiot2_on */
1028 ret = af9035_wr_reg_mask(d, 0xd8eb, 0x01, 0x01);
1032 ret = af9035_wr_reg_mask(d, 0xd8ec, 0x01, 0x01);
1036 ret = af9035_wr_reg_mask(d, 0xd8ed, 0x01, 0x01);
1040 usleep_range(10000, 50000);
1042 fe = dvb_attach(fc2580_attach, adap->fe[0],
1043 &d->i2c_adap, &af9035_fc2580_config);
1045 case AF9033_TUNER_FC0012:
1047 * AF9035 gpiot2 = FC0012 enable
1048 * XXX: there seems to be something on gpioh8 too, but on my
1049 * my test I didn't find any difference.
1052 if (adap->id == 0) {
1053 /* configure gpiot2 as output and high */
1054 ret = af9035_wr_reg_mask(d, 0xd8eb, 0x01, 0x01);
1058 ret = af9035_wr_reg_mask(d, 0xd8ec, 0x01, 0x01);
1062 ret = af9035_wr_reg_mask(d, 0xd8ed, 0x01, 0x01);
1067 * FIXME: That belongs for the FC0012 driver.
1068 * Write 02 to FC0012 master tuner register 0d directly
1069 * in order to make slave tuner working.
1074 msg[0].buf = "\x0d\x02";
1075 ret = i2c_transfer(&d->i2c_adap, msg, 1);
1080 usleep_range(10000, 50000);
1082 fe = dvb_attach(fc0012_attach, adap->fe[0], &d->i2c_adap,
1083 &af9035_fc0012_config[adap->id]);
1097 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
1102 static int af9035_init(struct dvb_usb_device *d)
1104 struct state *state = d_to_priv(d);
1106 u16 frame_size = 87 * 188 / 4;
1107 u8 packet_size = 512 / 4;
1108 struct reg_val_mask tab[] = {
1109 { 0x80f99d, 0x01, 0x01 },
1110 { 0x80f9a4, 0x01, 0x01 },
1111 { 0x00dd11, 0x00, 0x20 },
1112 { 0x00dd11, 0x00, 0x40 },
1113 { 0x00dd13, 0x00, 0x20 },
1114 { 0x00dd13, 0x00, 0x40 },
1115 { 0x00dd11, 0x20, 0x20 },
1116 { 0x00dd88, (frame_size >> 0) & 0xff, 0xff},
1117 { 0x00dd89, (frame_size >> 8) & 0xff, 0xff},
1118 { 0x00dd0c, packet_size, 0xff},
1119 { 0x00dd11, state->dual_mode << 6, 0x40 },
1120 { 0x00dd8a, (frame_size >> 0) & 0xff, 0xff},
1121 { 0x00dd8b, (frame_size >> 8) & 0xff, 0xff},
1122 { 0x00dd0d, packet_size, 0xff },
1123 { 0x80f9a3, state->dual_mode, 0x01 },
1124 { 0x80f9cd, state->dual_mode, 0x01 },
1125 { 0x80f99d, 0x00, 0x01 },
1126 { 0x80f9a4, 0x00, 0x01 },
1129 dev_dbg(&d->udev->dev, "%s: USB speed=%d frame_size=%04x " \
1130 "packet_size=%02x\n", __func__,
1131 d->udev->speed, frame_size, packet_size);
1133 /* init endpoints */
1134 for (i = 0; i < ARRAY_SIZE(tab); i++) {
1135 ret = af9035_wr_reg_mask(d, tab[i].reg, tab[i].val,
1144 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
1149 #if IS_ENABLED(CONFIG_RC_CORE)
1150 static int af9035_rc_query(struct dvb_usb_device *d)
1155 struct usb_req req = { CMD_IR_GET, 0, 0, NULL, 4, b };
1157 ret = af9035_ctrl_msg(d, &req);
1161 if ((b[2] + b[3]) == 0xff) {
1162 if ((b[0] + b[1]) == 0xff) {
1164 key = b[0] << 8 | b[2];
1167 key = b[0] << 16 | b[1] << 8 | b[2];
1170 key = b[0] << 24 | b[1] << 16 | b[2] << 8 | b[3];
1173 rc_keydown(d->rc_dev, key, 0);
1180 static int af9035_get_rc_config(struct dvb_usb_device *d, struct dvb_usb_rc *rc)
1185 ret = af9035_rd_reg(d, EEPROM_IR_MODE, &tmp);
1189 dev_dbg(&d->udev->dev, "%s: ir_mode=%02x\n", __func__, tmp);
1191 /* don't activate rc if in HID mode or if not available */
1193 ret = af9035_rd_reg(d, EEPROM_IR_TYPE, &tmp);
1197 dev_dbg(&d->udev->dev, "%s: ir_type=%02x\n", __func__, tmp);
1202 rc->allowed_protos = RC_BIT_NEC;
1205 rc->allowed_protos = RC_BIT_RC6_MCE;
1209 rc->query = af9035_rc_query;
1212 /* load empty to enable rc */
1214 rc->map_name = RC_MAP_EMPTY;
1220 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
1225 #define af9035_get_rc_config NULL
1228 /* interface 0 is used by DVB-T receiver and
1229 interface 1 is for remote controller (HID) */
1230 static const struct dvb_usb_device_properties af9035_props = {
1231 .driver_name = KBUILD_MODNAME,
1232 .owner = THIS_MODULE,
1233 .adapter_nr = adapter_nr,
1234 .size_of_priv = sizeof(struct state),
1236 .generic_bulk_ctrl_endpoint = 0x02,
1237 .generic_bulk_ctrl_endpoint_response = 0x81,
1239 .identify_state = af9035_identify_state,
1240 .firmware = AF9035_FIRMWARE_AF9035,
1241 .download_firmware = af9035_download_firmware,
1243 .i2c_algo = &af9035_i2c_algo,
1244 .read_config = af9035_read_config,
1245 .frontend_attach = af9035_frontend_attach,
1246 .tuner_attach = af9035_tuner_attach,
1247 .init = af9035_init,
1248 .get_rc_config = af9035_get_rc_config,
1250 .get_adapter_count = af9035_get_adapter_count,
1253 .stream = DVB_USB_STREAM_BULK(0x84, 6, 87 * 188),
1255 .stream = DVB_USB_STREAM_BULK(0x85, 6, 87 * 188),
1260 static const struct dvb_usb_device_properties it9135_props = {
1261 .driver_name = KBUILD_MODNAME,
1262 .owner = THIS_MODULE,
1263 .adapter_nr = adapter_nr,
1264 .size_of_priv = sizeof(struct state),
1266 .generic_bulk_ctrl_endpoint = 0x02,
1267 .generic_bulk_ctrl_endpoint_response = 0x81,
1269 .identify_state = af9035_identify_state,
1270 .firmware = AF9035_FIRMWARE_IT9135,
1271 .download_firmware = af9035_download_firmware_it9135,
1273 .i2c_algo = &af9035_i2c_algo,
1274 .read_config = af9035_read_config_it9135,
1275 .frontend_attach = af9035_frontend_attach,
1276 .tuner_attach = af9035_tuner_attach,
1277 .init = af9035_init,
1278 .get_rc_config = af9035_get_rc_config,
1283 .stream = DVB_USB_STREAM_BULK(0x84, 6, 87 * 188),
1285 .stream = DVB_USB_STREAM_BULK(0x85, 6, 87 * 188),
1290 static const struct usb_device_id af9035_id_table[] = {
1291 { DVB_USB_DEVICE(USB_VID_AFATECH, USB_PID_AFATECH_AF9035_9035,
1292 &af9035_props, "Afatech AF9035 reference design", NULL) },
1293 { DVB_USB_DEVICE(USB_VID_AFATECH, USB_PID_AFATECH_AF9035_1000,
1294 &af9035_props, "Afatech AF9035 reference design", NULL) },
1295 { DVB_USB_DEVICE(USB_VID_AFATECH, USB_PID_AFATECH_AF9035_1001,
1296 &af9035_props, "Afatech AF9035 reference design", NULL) },
1297 { DVB_USB_DEVICE(USB_VID_AFATECH, USB_PID_AFATECH_AF9035_1002,
1298 &af9035_props, "Afatech AF9035 reference design", NULL) },
1299 { DVB_USB_DEVICE(USB_VID_AFATECH, USB_PID_AFATECH_AF9035_1003,
1300 &af9035_props, "Afatech AF9035 reference design", NULL) },
1301 { DVB_USB_DEVICE(USB_VID_TERRATEC, USB_PID_TERRATEC_CINERGY_T_STICK,
1302 &af9035_props, "TerraTec Cinergy T Stick", NULL) },
1303 { DVB_USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_A835,
1304 &af9035_props, "AVerMedia AVerTV Volar HD/PRO (A835)", NULL) },
1305 { DVB_USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_B835,
1306 &af9035_props, "AVerMedia AVerTV Volar HD/PRO (A835)", NULL) },
1307 { DVB_USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_1867,
1308 &af9035_props, "AVerMedia HD Volar (A867)", NULL) },
1309 { DVB_USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_A867,
1310 &af9035_props, "AVerMedia HD Volar (A867)", NULL) },
1311 { DVB_USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_TWINSTAR,
1312 &af9035_props, "AVerMedia Twinstar (A825)", NULL) },
1313 { DVB_USB_DEVICE(USB_VID_ASUS, USB_PID_ASUS_U3100MINI_PLUS,
1314 &af9035_props, "Asus U3100Mini Plus", NULL) },
1317 MODULE_DEVICE_TABLE(usb, af9035_id_table);
1319 static struct usb_driver af9035_usb_driver = {
1320 .name = KBUILD_MODNAME,
1321 .id_table = af9035_id_table,
1322 .probe = dvb_usbv2_probe,
1323 .disconnect = dvb_usbv2_disconnect,
1324 .suspend = dvb_usbv2_suspend,
1325 .resume = dvb_usbv2_resume,
1326 .reset_resume = dvb_usbv2_reset_resume,
1331 module_usb_driver(af9035_usb_driver);
1333 MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
1334 MODULE_DESCRIPTION("Afatech AF9035 driver");
1335 MODULE_LICENSE("GPL");
1336 MODULE_FIRMWARE(AF9035_FIRMWARE_AF9035);
1337 MODULE_FIRMWARE(AF9035_FIRMWARE_IT9135);