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 KBUILD_MODNAME
, 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
,
95 "%s: command=%02x checksum mismatch (%04x != %04x)\n",
96 KBUILD_MODNAME
, req
->cmd
, tmp_checksum
,
104 /* fw returns status 1 when IR code was not received */
105 if (req
->cmd
== CMD_IR_GET
|| state
->buf
[2] == 1) {
110 dev_dbg(&d
->udev
->dev
, "%s: command=%02x failed fw error=%d\n",
111 __func__
, req
->cmd
, state
->buf
[2]);
116 /* read request, copy returned data to return buf */
118 memcpy(req
->rbuf
, &state
->buf
[ACK_HDR_LEN
], req
->rlen
);
120 mutex_unlock(&d
->usb_mutex
);
122 dev_dbg(&d
->udev
->dev
, "%s: failed=%d\n", __func__
, ret
);
126 /* write multiple registers */
127 static int af9035_wr_regs(struct dvb_usb_device
*d
, u32 reg
, u8
*val
, int len
)
130 u8 mbox
= (reg
>> 16) & 0xff;
131 struct usb_req req
= { CMD_MEM_WR
, mbox
, sizeof(wbuf
), wbuf
, 0, NULL
};
137 wbuf
[4] = (reg
>> 8) & 0xff;
138 wbuf
[5] = (reg
>> 0) & 0xff;
139 memcpy(&wbuf
[6], val
, len
);
141 return af9035_ctrl_msg(d
, &req
);
144 /* read multiple registers */
145 static int af9035_rd_regs(struct dvb_usb_device
*d
, u32 reg
, u8
*val
, int len
)
147 u8 wbuf
[] = { len
, 2, 0, 0, (reg
>> 8) & 0xff, reg
& 0xff };
148 u8 mbox
= (reg
>> 16) & 0xff;
149 struct usb_req req
= { CMD_MEM_RD
, mbox
, sizeof(wbuf
), wbuf
, len
, val
};
151 return af9035_ctrl_msg(d
, &req
);
154 /* write single register */
155 static int af9035_wr_reg(struct dvb_usb_device
*d
, u32 reg
, u8 val
)
157 return af9035_wr_regs(d
, reg
, &val
, 1);
160 /* read single register */
161 static int af9035_rd_reg(struct dvb_usb_device
*d
, u32 reg
, u8
*val
)
163 return af9035_rd_regs(d
, reg
, val
, 1);
166 /* write single register with mask */
167 static int af9035_wr_reg_mask(struct dvb_usb_device
*d
, u32 reg
, u8 val
,
173 /* no need for read if whole reg is written */
175 ret
= af9035_rd_regs(d
, reg
, &tmp
, 1);
184 return af9035_wr_regs(d
, reg
, &val
, 1);
187 static int af9035_i2c_master_xfer(struct i2c_adapter
*adap
,
188 struct i2c_msg msg
[], int num
)
190 struct dvb_usb_device
*d
= i2c_get_adapdata(adap
);
191 struct state
*state
= d_to_priv(d
);
194 if (mutex_lock_interruptible(&d
->i2c_mutex
) < 0)
198 * I2C sub header is 5 bytes long. Meaning of those bytes are:
202 * byte 3 and 4 can be used as reg addr
204 * used when reg addr len is set to 2
206 * used when reg addr len is set to 1 or 2
208 * For the simplify we do not use register addr at all.
209 * NOTE: As a firmware knows tuner type there is very small possibility
210 * there could be some tuner I2C hacks done by firmware and this may
211 * lead problems if firmware expects those bytes are used.
213 if (num
== 2 && !(msg
[0].flags
& I2C_M_RD
) &&
214 (msg
[1].flags
& I2C_M_RD
)) {
215 if (msg
[0].len
> 40 || msg
[1].len
> 40) {
216 /* TODO: correct limits > 40 */
218 } else if ((msg
[0].addr
== state
->af9033_config
[0].i2c_addr
) ||
219 (msg
[0].addr
== state
->af9033_config
[1].i2c_addr
)) {
220 /* demod access via firmware interface */
221 u32 reg
= msg
[0].buf
[0] << 16 | msg
[0].buf
[1] << 8 |
224 if (msg
[0].addr
== state
->af9033_config
[1].i2c_addr
)
227 ret
= af9035_rd_regs(d
, reg
, &msg
[1].buf
[0],
231 u8 buf
[5 + msg
[0].len
];
232 struct usb_req req
= { CMD_I2C_RD
, 0, sizeof(buf
),
233 buf
, msg
[1].len
, msg
[1].buf
};
234 req
.mbox
|= ((msg
[0].addr
& 0x80) >> 3);
236 buf
[1] = msg
[0].addr
<< 1;
237 buf
[2] = 0x00; /* reg addr len */
238 buf
[3] = 0x00; /* reg addr MSB */
239 buf
[4] = 0x00; /* reg addr LSB */
240 memcpy(&buf
[5], msg
[0].buf
, msg
[0].len
);
241 ret
= af9035_ctrl_msg(d
, &req
);
243 } else if (num
== 1 && !(msg
[0].flags
& I2C_M_RD
)) {
244 if (msg
[0].len
> 40) {
245 /* TODO: correct limits > 40 */
247 } else if ((msg
[0].addr
== state
->af9033_config
[0].i2c_addr
) ||
248 (msg
[0].addr
== state
->af9033_config
[1].i2c_addr
)) {
249 /* demod access via firmware interface */
250 u32 reg
= msg
[0].buf
[0] << 16 | msg
[0].buf
[1] << 8 |
253 if (msg
[0].addr
== state
->af9033_config
[1].i2c_addr
)
256 ret
= af9035_wr_regs(d
, reg
, &msg
[0].buf
[3],
260 u8 buf
[5 + msg
[0].len
];
261 struct usb_req req
= { CMD_I2C_WR
, 0, sizeof(buf
), buf
,
263 req
.mbox
|= ((msg
[0].addr
& 0x80) >> 3);
265 buf
[1] = msg
[0].addr
<< 1;
266 buf
[2] = 0x00; /* reg addr len */
267 buf
[3] = 0x00; /* reg addr MSB */
268 buf
[4] = 0x00; /* reg addr LSB */
269 memcpy(&buf
[5], msg
[0].buf
, msg
[0].len
);
270 ret
= af9035_ctrl_msg(d
, &req
);
272 } else if (num
== 1 && (msg
[0].flags
& I2C_M_RD
)) {
273 if (msg
[0].len
> 40) {
274 /* TODO: correct limits > 40 */
279 struct usb_req req
= { CMD_I2C_RD
, 0, sizeof(buf
),
280 buf
, msg
[0].len
, msg
[0].buf
};
281 req
.mbox
|= ((msg
[0].addr
& 0x80) >> 3);
283 buf
[1] = msg
[0].addr
<< 1;
284 buf
[2] = 0x00; /* reg addr len */
285 buf
[3] = 0x00; /* reg addr MSB */
286 buf
[4] = 0x00; /* reg addr LSB */
287 ret
= af9035_ctrl_msg(d
, &req
);
291 * We support only three kind of I2C transactions:
292 * 1) 1 x read + 1 x write (repeated start)
299 mutex_unlock(&d
->i2c_mutex
);
307 static u32
af9035_i2c_functionality(struct i2c_adapter
*adapter
)
312 static struct i2c_algorithm af9035_i2c_algo
= {
313 .master_xfer
= af9035_i2c_master_xfer
,
314 .functionality
= af9035_i2c_functionality
,
317 static int af9035_identify_state(struct dvb_usb_device
*d
, const char **name
)
319 struct state
*state
= d_to_priv(d
);
323 struct usb_req req
= { CMD_FW_QUERYINFO
, 0, sizeof(wbuf
), wbuf
,
324 sizeof(rbuf
), rbuf
};
326 ret
= af9035_rd_regs(d
, 0x1222, rbuf
, 3);
330 state
->chip_version
= rbuf
[0];
331 state
->chip_type
= rbuf
[2] << 8 | rbuf
[1] << 0;
333 ret
= af9035_rd_reg(d
, 0x384f, &state
->prechip_version
);
337 dev_info(&d
->udev
->dev
,
338 "%s: prechip_version=%02x chip_version=%02x chip_type=%04x\n",
339 KBUILD_MODNAME
, state
->prechip_version
,
340 state
->chip_version
, state
->chip_type
);
342 if (state
->chip_type
== 0x9135) {
343 if (state
->chip_version
== 0x02)
344 *name
= AF9035_FIRMWARE_IT9135_V2
;
346 *name
= AF9035_FIRMWARE_IT9135_V1
;
347 state
->eeprom_addr
= EEPROM_BASE_IT9135
;
349 *name
= AF9035_FIRMWARE_AF9035
;
350 state
->eeprom_addr
= EEPROM_BASE_AF9035
;
353 ret
= af9035_ctrl_msg(d
, &req
);
357 dev_dbg(&d
->udev
->dev
, "%s: reply=%*ph\n", __func__
, 4, rbuf
);
358 if (rbuf
[0] || rbuf
[1] || rbuf
[2] || rbuf
[3])
366 dev_dbg(&d
->udev
->dev
, "%s: failed=%d\n", __func__
, ret
);
371 static int af9035_download_firmware_old(struct dvb_usb_device
*d
,
372 const struct firmware
*fw
)
376 struct usb_req req
= { 0, 0, 0, NULL
, 0, NULL
};
377 struct usb_req req_fw_dl
= { CMD_FW_DL
, 0, 0, wbuf
, 0, NULL
};
379 u16 hdr_addr
, hdr_data_len
, hdr_checksum
;
384 * Thanks to Daniel Glöckner <daniel-gl@gmx.net> about that info!
386 * byte 0: MCS 51 core
387 * There are two inside the AF9035 (1=Link and 2=OFDM) with separate
389 * byte 1-2: Big endian destination address
390 * byte 3-4: Big endian number of data bytes following the header
391 * byte 5-6: Big endian header checksum, apparently ignored by the chip
392 * Calculated as ~(h[0]*256+h[1]+h[2]*256+h[3]+h[4]*256)
395 for (i
= fw
->size
; i
> HDR_SIZE
;) {
396 hdr_core
= fw
->data
[fw
->size
- i
+ 0];
397 hdr_addr
= fw
->data
[fw
->size
- i
+ 1] << 8;
398 hdr_addr
|= fw
->data
[fw
->size
- i
+ 2] << 0;
399 hdr_data_len
= fw
->data
[fw
->size
- i
+ 3] << 8;
400 hdr_data_len
|= fw
->data
[fw
->size
- i
+ 4] << 0;
401 hdr_checksum
= fw
->data
[fw
->size
- i
+ 5] << 8;
402 hdr_checksum
|= fw
->data
[fw
->size
- i
+ 6] << 0;
404 dev_dbg(&d
->udev
->dev
,
405 "%s: core=%d addr=%04x data_len=%d checksum=%04x\n",
406 __func__
, hdr_core
, hdr_addr
, hdr_data_len
,
409 if (((hdr_core
!= 1) && (hdr_core
!= 2)) ||
410 (hdr_data_len
> i
)) {
411 dev_dbg(&d
->udev
->dev
, "%s: bad firmware\n", __func__
);
415 /* download begin packet */
416 req
.cmd
= CMD_FW_DL_BEGIN
;
417 ret
= af9035_ctrl_msg(d
, &req
);
421 /* download firmware packet(s) */
422 for (j
= HDR_SIZE
+ hdr_data_len
; j
> 0; j
-= MAX_DATA
) {
426 req_fw_dl
.wlen
= len
;
427 req_fw_dl
.wbuf
= (u8
*) &fw
->data
[fw
->size
- i
+
428 HDR_SIZE
+ hdr_data_len
- j
];
429 ret
= af9035_ctrl_msg(d
, &req_fw_dl
);
434 /* download end packet */
435 req
.cmd
= CMD_FW_DL_END
;
436 ret
= af9035_ctrl_msg(d
, &req
);
440 i
-= hdr_data_len
+ HDR_SIZE
;
442 dev_dbg(&d
->udev
->dev
, "%s: data uploaded=%zu\n",
443 __func__
, fw
->size
- i
);
446 /* print warn if firmware is bad, continue and see what happens */
448 dev_warn(&d
->udev
->dev
, "%s: bad firmware\n", KBUILD_MODNAME
);
453 dev_dbg(&d
->udev
->dev
, "%s: failed=%d\n", __func__
, ret
);
458 static int af9035_download_firmware_new(struct dvb_usb_device
*d
,
459 const struct firmware
*fw
)
462 struct usb_req req_fw_dl
= { CMD_FW_SCATTER_WR
, 0, 0, NULL
, 0, NULL
};
466 * There seems to be following firmware header. Meaning of bytes 0-3
475 * 6: count of data bytes ?
477 for (i
= HDR_SIZE
, i_prev
= 0; i
<= fw
->size
; i
++) {
479 (fw
->data
[i
+ 0] == 0x03 &&
480 (fw
->data
[i
+ 1] == 0x00 ||
481 fw
->data
[i
+ 1] == 0x01) &&
482 fw
->data
[i
+ 2] == 0x00)) {
483 req_fw_dl
.wlen
= i
- i_prev
;
484 req_fw_dl
.wbuf
= (u8
*) &fw
->data
[i_prev
];
486 ret
= af9035_ctrl_msg(d
, &req_fw_dl
);
490 dev_dbg(&d
->udev
->dev
, "%s: data uploaded=%d\n",
498 dev_dbg(&d
->udev
->dev
, "%s: failed=%d\n", __func__
, ret
);
503 static int af9035_download_firmware(struct dvb_usb_device
*d
,
504 const struct firmware
*fw
)
506 struct state
*state
= d_to_priv(d
);
511 struct usb_req req
= { 0, 0, 0, NULL
, 0, NULL
};
512 struct usb_req req_fw_ver
= { CMD_FW_QUERYINFO
, 0, 1, wbuf
, 4, rbuf
};
513 dev_dbg(&d
->udev
->dev
, "%s:\n", __func__
);
516 * In case of dual tuner configuration we need to do some extra
517 * initialization in order to download firmware to slave demod too,
518 * which is done by master demod.
519 * Master feeds also clock and controls power via GPIO.
521 ret
= af9035_rd_reg(d
, state
->eeprom_addr
+ EEPROM_TS_MODE
, &tmp
);
525 if (tmp
== 1 || tmp
== 3) {
526 /* configure gpioh1, reset & power slave demod */
527 ret
= af9035_wr_reg_mask(d
, 0x00d8b0, 0x01, 0x01);
531 ret
= af9035_wr_reg_mask(d
, 0x00d8b1, 0x01, 0x01);
535 ret
= af9035_wr_reg_mask(d
, 0x00d8af, 0x00, 0x01);
539 usleep_range(10000, 50000);
541 ret
= af9035_wr_reg_mask(d
, 0x00d8af, 0x01, 0x01);
545 /* tell the slave I2C address */
546 ret
= af9035_rd_reg(d
,
547 state
->eeprom_addr
+ EEPROM_2ND_DEMOD_ADDR
,
552 if (state
->chip_type
== 0x9135) {
553 ret
= af9035_wr_reg(d
, 0x004bfb, tmp
);
557 ret
= af9035_wr_reg(d
, 0x00417f, tmp
);
561 /* enable clock out */
562 ret
= af9035_wr_reg_mask(d
, 0x00d81a, 0x01, 0x01);
568 if (fw
->data
[0] == 0x01)
569 ret
= af9035_download_firmware_old(d
, fw
);
571 ret
= af9035_download_firmware_new(d
, fw
);
575 /* firmware loaded, request boot */
576 req
.cmd
= CMD_FW_BOOT
;
577 ret
= af9035_ctrl_msg(d
, &req
);
581 /* ensure firmware starts */
583 ret
= af9035_ctrl_msg(d
, &req_fw_ver
);
587 if (!(rbuf
[0] || rbuf
[1] || rbuf
[2] || rbuf
[3])) {
588 dev_err(&d
->udev
->dev
, "%s: firmware did not run\n",
594 dev_info(&d
->udev
->dev
, "%s: firmware version=%d.%d.%d.%d",
595 KBUILD_MODNAME
, rbuf
[0], rbuf
[1], rbuf
[2], rbuf
[3]);
600 dev_dbg(&d
->udev
->dev
, "%s: failed=%d\n", __func__
, ret
);
605 static int af9035_read_config(struct dvb_usb_device
*d
)
607 struct state
*state
= d_to_priv(d
);
612 /* demod I2C "address" */
613 state
->af9033_config
[0].i2c_addr
= 0x38;
614 state
->af9033_config
[0].adc_multiplier
= AF9033_ADC_MULTIPLIER_2X
;
615 state
->af9033_config
[1].adc_multiplier
= AF9033_ADC_MULTIPLIER_2X
;
616 state
->af9033_config
[0].ts_mode
= AF9033_TS_MODE_USB
;
617 state
->af9033_config
[1].ts_mode
= AF9033_TS_MODE_SERIAL
;
619 /* eeprom memory mapped location */
620 if (state
->chip_type
== 0x9135) {
621 if (state
->chip_version
== 0x02) {
622 state
->af9033_config
[0].tuner
= AF9033_TUNER_IT9135_60
;
623 state
->af9033_config
[1].tuner
= AF9033_TUNER_IT9135_60
;
626 state
->af9033_config
[0].tuner
= AF9033_TUNER_IT9135_38
;
627 state
->af9033_config
[1].tuner
= AF9033_TUNER_IT9135_38
;
631 /* check if eeprom exists */
632 ret
= af9035_rd_reg(d
, tmp16
, &tmp
);
637 dev_dbg(&d
->udev
->dev
, "%s: no eeprom\n", __func__
);
642 /* check if there is dual tuners */
643 ret
= af9035_rd_reg(d
, state
->eeprom_addr
+ EEPROM_TS_MODE
, &tmp
);
647 if (tmp
== 1 || tmp
== 3)
648 state
->dual_mode
= true;
650 dev_dbg(&d
->udev
->dev
, "%s: ts mode=%d dual mode=%d\n", __func__
,
651 tmp
, state
->dual_mode
);
653 if (state
->dual_mode
) {
654 /* read 2nd demodulator I2C address */
655 ret
= af9035_rd_reg(d
,
656 state
->eeprom_addr
+ EEPROM_2ND_DEMOD_ADDR
,
661 state
->af9033_config
[1].i2c_addr
= tmp
;
662 dev_dbg(&d
->udev
->dev
, "%s: 2nd demod I2C addr=%02x\n",
666 addr
= state
->eeprom_addr
;
668 for (i
= 0; i
< state
->dual_mode
+ 1; i
++) {
670 ret
= af9035_rd_reg(d
, addr
+ EEPROM_1_TUNER_ID
, &tmp
);
675 dev_dbg(&d
->udev
->dev
,
676 "%s: [%d]tuner not set, using default\n",
679 state
->af9033_config
[i
].tuner
= tmp
;
681 dev_dbg(&d
->udev
->dev
, "%s: [%d]tuner=%02x\n",
682 __func__
, i
, state
->af9033_config
[i
].tuner
);
684 switch (state
->af9033_config
[i
].tuner
) {
685 case AF9033_TUNER_TUA9001
:
686 case AF9033_TUNER_FC0011
:
687 case AF9033_TUNER_MXL5007T
:
688 case AF9033_TUNER_TDA18218
:
689 case AF9033_TUNER_FC2580
:
690 case AF9033_TUNER_FC0012
:
691 state
->af9033_config
[i
].spec_inv
= 1;
693 case AF9033_TUNER_IT9135_38
:
694 case AF9033_TUNER_IT9135_51
:
695 case AF9033_TUNER_IT9135_52
:
696 case AF9033_TUNER_IT9135_60
:
697 case AF9033_TUNER_IT9135_61
:
698 case AF9033_TUNER_IT9135_62
:
701 dev_warn(&d
->udev
->dev
,
702 "%s: tuner id=%02x not supported, please report!",
703 KBUILD_MODNAME
, tmp
);
706 /* disable dual mode if driver does not support it */
708 switch (state
->af9033_config
[i
].tuner
) {
709 case AF9033_TUNER_FC0012
:
710 case AF9033_TUNER_IT9135_38
:
711 case AF9033_TUNER_IT9135_51
:
712 case AF9033_TUNER_IT9135_52
:
713 case AF9033_TUNER_IT9135_60
:
714 case AF9033_TUNER_IT9135_61
:
715 case AF9033_TUNER_IT9135_62
:
716 case AF9033_TUNER_MXL5007T
:
719 state
->dual_mode
= false;
720 dev_info(&d
->udev
->dev
,
721 "%s: driver does not support 2nd tuner and will disable it",
725 /* tuner IF frequency */
726 ret
= af9035_rd_reg(d
, addr
+ EEPROM_1_IF_L
, &tmp
);
732 ret
= af9035_rd_reg(d
, addr
+ EEPROM_1_IF_H
, &tmp
);
738 dev_dbg(&d
->udev
->dev
, "%s: [%d]IF=%d\n", __func__
, i
, tmp16
);
740 addr
+= 0x10; /* shift for the 2nd tuner params */
744 /* get demod clock */
745 ret
= af9035_rd_reg(d
, 0x00d800, &tmp
);
749 tmp
= (tmp
>> 0) & 0x0f;
751 for (i
= 0; i
< ARRAY_SIZE(state
->af9033_config
); i
++) {
752 if (state
->chip_type
== 0x9135)
753 state
->af9033_config
[i
].clock
= clock_lut_it9135
[tmp
];
755 state
->af9033_config
[i
].clock
= clock_lut_af9035
[tmp
];
761 dev_dbg(&d
->udev
->dev
, "%s: failed=%d\n", __func__
, ret
);
766 static int af9035_tua9001_tuner_callback(struct dvb_usb_device
*d
,
772 dev_dbg(&d
->udev
->dev
, "%s: cmd=%d arg=%d\n", __func__
, cmd
, arg
);
775 * CEN always enabled by hardware wiring
781 case TUA9001_CMD_RESETN
:
787 ret
= af9035_wr_reg_mask(d
, 0x00d8e7, val
, 0x01);
791 case TUA9001_CMD_RXEN
:
797 ret
= af9035_wr_reg_mask(d
, 0x00d8eb, val
, 0x01);
806 dev_dbg(&d
->udev
->dev
, "%s: failed=%d\n", __func__
, ret
);
812 static int af9035_fc0011_tuner_callback(struct dvb_usb_device
*d
,
818 case FC0011_FE_CALLBACK_POWER
:
820 ret
= af9035_wr_reg_mask(d
, 0xd8eb, 1, 1);
824 ret
= af9035_wr_reg_mask(d
, 0xd8ec, 1, 1);
828 ret
= af9035_wr_reg_mask(d
, 0xd8ed, 1, 1);
833 ret
= af9035_wr_reg_mask(d
, 0xd8d0, 1, 1);
837 ret
= af9035_wr_reg_mask(d
, 0xd8d1, 1, 1);
841 usleep_range(10000, 50000);
843 case FC0011_FE_CALLBACK_RESET
:
844 ret
= af9035_wr_reg(d
, 0xd8e9, 1);
848 ret
= af9035_wr_reg(d
, 0xd8e8, 1);
852 ret
= af9035_wr_reg(d
, 0xd8e7, 1);
856 usleep_range(10000, 20000);
858 ret
= af9035_wr_reg(d
, 0xd8e7, 0);
862 usleep_range(10000, 20000);
872 dev_dbg(&d
->udev
->dev
, "%s: failed=%d\n", __func__
, ret
);
877 static int af9035_tuner_callback(struct dvb_usb_device
*d
, int cmd
, int arg
)
879 struct state
*state
= d_to_priv(d
);
881 switch (state
->af9033_config
[0].tuner
) {
882 case AF9033_TUNER_FC0011
:
883 return af9035_fc0011_tuner_callback(d
, cmd
, arg
);
884 case AF9033_TUNER_TUA9001
:
885 return af9035_tua9001_tuner_callback(d
, cmd
, arg
);
893 static int af9035_frontend_callback(void *adapter_priv
, int component
,
896 struct i2c_adapter
*adap
= adapter_priv
;
897 struct dvb_usb_device
*d
= i2c_get_adapdata(adap
);
899 dev_dbg(&d
->udev
->dev
, "%s: component=%d cmd=%d arg=%d\n",
900 __func__
, component
, cmd
, arg
);
903 case DVB_FRONTEND_COMPONENT_TUNER
:
904 return af9035_tuner_callback(d
, cmd
, arg
);
912 static int af9035_get_adapter_count(struct dvb_usb_device
*d
)
914 struct state
*state
= d_to_priv(d
);
916 /* disable 2nd adapter as we don't have PID filters implemented */
917 if (d
->udev
->speed
== USB_SPEED_FULL
)
920 return state
->dual_mode
+ 1;
923 static int af9035_frontend_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 dev_dbg(&d
->udev
->dev
, "%s:\n", __func__
);
930 if (!state
->af9033_config
[adap
->id
].tuner
) {
931 /* unsupported tuner */
936 /* attach demodulator */
937 adap
->fe
[0] = dvb_attach(af9033_attach
, &state
->af9033_config
[adap
->id
],
939 if (adap
->fe
[0] == NULL
) {
944 /* disable I2C-gate */
945 adap
->fe
[0]->ops
.i2c_gate_ctrl
= NULL
;
946 adap
->fe
[0]->callback
= af9035_frontend_callback
;
951 dev_dbg(&d
->udev
->dev
, "%s: failed=%d\n", __func__
, ret
);
956 static struct tua9001_config af9035_tua9001_config
= {
960 static const struct fc0011_config af9035_fc0011_config
= {
964 static struct mxl5007t_config af9035_mxl5007t_config
[] = {
966 .xtal_freq_hz
= MxL_XTAL_24_MHZ
,
967 .if_freq_hz
= MxL_IF_4_57_MHZ
,
969 .loop_thru_enable
= 0,
971 .clk_out_amp
= MxL_CLKOUT_AMP_0_94V
,
973 .xtal_freq_hz
= MxL_XTAL_24_MHZ
,
974 .if_freq_hz
= MxL_IF_4_57_MHZ
,
976 .loop_thru_enable
= 1,
978 .clk_out_amp
= MxL_CLKOUT_AMP_0_94V
,
982 static struct tda18218_config af9035_tda18218_config
= {
987 static const struct fc2580_config af9035_fc2580_config
= {
992 static const struct fc0012_config af9035_fc0012_config
[] = {
995 .xtal_freq
= FC_XTAL_36_MHZ
,
997 .loop_through
= true,
1000 .i2c_address
= 0x63 | 0x80, /* I2C bus select hack */
1001 .xtal_freq
= FC_XTAL_36_MHZ
,
1002 .dual_master
= true,
1006 static int af9035_tuner_attach(struct dvb_usb_adapter
*adap
)
1008 struct state
*state
= adap_to_priv(adap
);
1009 struct dvb_usb_device
*d
= adap_to_d(adap
);
1011 struct dvb_frontend
*fe
;
1012 struct i2c_msg msg
[1];
1014 dev_dbg(&d
->udev
->dev
, "%s:\n", __func__
);
1017 * XXX: Hack used in that function: we abuse unused I2C address bit [7]
1018 * to carry info about used I2C bus for dual tuner configuration.
1021 switch (state
->af9033_config
[adap
->id
].tuner
) {
1022 case AF9033_TUNER_TUA9001
:
1023 /* AF9035 gpiot3 = TUA9001 RESETN
1024 AF9035 gpiot2 = TUA9001 RXEN */
1026 /* configure gpiot2 and gpiot2 as output */
1027 ret
= af9035_wr_reg_mask(d
, 0x00d8ec, 0x01, 0x01);
1031 ret
= af9035_wr_reg_mask(d
, 0x00d8ed, 0x01, 0x01);
1035 ret
= af9035_wr_reg_mask(d
, 0x00d8e8, 0x01, 0x01);
1039 ret
= af9035_wr_reg_mask(d
, 0x00d8e9, 0x01, 0x01);
1044 fe
= dvb_attach(tua9001_attach
, adap
->fe
[0],
1045 &d
->i2c_adap
, &af9035_tua9001_config
);
1047 case AF9033_TUNER_FC0011
:
1048 fe
= dvb_attach(fc0011_attach
, adap
->fe
[0],
1049 &d
->i2c_adap
, &af9035_fc0011_config
);
1051 case AF9033_TUNER_MXL5007T
:
1052 if (adap
->id
== 0) {
1053 ret
= af9035_wr_reg(d
, 0x00d8e0, 1);
1057 ret
= af9035_wr_reg(d
, 0x00d8e1, 1);
1061 ret
= af9035_wr_reg(d
, 0x00d8df, 0);
1067 ret
= af9035_wr_reg(d
, 0x00d8df, 1);
1073 ret
= af9035_wr_reg(d
, 0x00d8c0, 1);
1077 ret
= af9035_wr_reg(d
, 0x00d8c1, 1);
1081 ret
= af9035_wr_reg(d
, 0x00d8bf, 0);
1085 ret
= af9035_wr_reg(d
, 0x00d8b4, 1);
1089 ret
= af9035_wr_reg(d
, 0x00d8b5, 1);
1093 ret
= af9035_wr_reg(d
, 0x00d8b3, 1);
1099 tuner_addr
= 0x60 | 0x80; /* I2C bus hack */
1103 fe
= dvb_attach(mxl5007t_attach
, adap
->fe
[0], &d
->i2c_adap
,
1104 tuner_addr
, &af9035_mxl5007t_config
[adap
->id
]);
1106 case AF9033_TUNER_TDA18218
:
1108 fe
= dvb_attach(tda18218_attach
, adap
->fe
[0],
1109 &d
->i2c_adap
, &af9035_tda18218_config
);
1111 case AF9033_TUNER_FC2580
:
1112 /* Tuner enable using gpiot2_o, gpiot2_en and gpiot2_on */
1113 ret
= af9035_wr_reg_mask(d
, 0xd8eb, 0x01, 0x01);
1117 ret
= af9035_wr_reg_mask(d
, 0xd8ec, 0x01, 0x01);
1121 ret
= af9035_wr_reg_mask(d
, 0xd8ed, 0x01, 0x01);
1125 usleep_range(10000, 50000);
1127 fe
= dvb_attach(fc2580_attach
, adap
->fe
[0],
1128 &d
->i2c_adap
, &af9035_fc2580_config
);
1130 case AF9033_TUNER_FC0012
:
1132 * AF9035 gpiot2 = FC0012 enable
1133 * XXX: there seems to be something on gpioh8 too, but on my
1134 * my test I didn't find any difference.
1137 if (adap
->id
== 0) {
1138 /* configure gpiot2 as output and high */
1139 ret
= af9035_wr_reg_mask(d
, 0xd8eb, 0x01, 0x01);
1143 ret
= af9035_wr_reg_mask(d
, 0xd8ec, 0x01, 0x01);
1147 ret
= af9035_wr_reg_mask(d
, 0xd8ed, 0x01, 0x01);
1152 * FIXME: That belongs for the FC0012 driver.
1153 * Write 02 to FC0012 master tuner register 0d directly
1154 * in order to make slave tuner working.
1159 msg
[0].buf
= "\x0d\x02";
1160 ret
= i2c_transfer(&d
->i2c_adap
, msg
, 1);
1165 usleep_range(10000, 50000);
1167 fe
= dvb_attach(fc0012_attach
, adap
->fe
[0], &d
->i2c_adap
,
1168 &af9035_fc0012_config
[adap
->id
]);
1170 case AF9033_TUNER_IT9135_38
:
1171 case AF9033_TUNER_IT9135_51
:
1172 case AF9033_TUNER_IT9135_52
:
1173 case AF9033_TUNER_IT9135_60
:
1174 case AF9033_TUNER_IT9135_61
:
1175 case AF9033_TUNER_IT9135_62
:
1177 fe
= dvb_attach(it913x_attach
, adap
->fe
[0], &d
->i2c_adap
,
1178 state
->af9033_config
[adap
->id
].i2c_addr
,
1179 state
->af9033_config
[0].tuner
);
1193 dev_dbg(&d
->udev
->dev
, "%s: failed=%d\n", __func__
, ret
);
1198 static int af9035_init(struct dvb_usb_device
*d
)
1200 struct state
*state
= d_to_priv(d
);
1202 u16 frame_size
= (d
->udev
->speed
== USB_SPEED_FULL
? 5 : 87) * 188 / 4;
1203 u8 packet_size
= (d
->udev
->speed
== USB_SPEED_FULL
? 64 : 512) / 4;
1204 struct reg_val_mask tab
[] = {
1205 { 0x80f99d, 0x01, 0x01 },
1206 { 0x80f9a4, 0x01, 0x01 },
1207 { 0x00dd11, 0x00, 0x20 },
1208 { 0x00dd11, 0x00, 0x40 },
1209 { 0x00dd13, 0x00, 0x20 },
1210 { 0x00dd13, 0x00, 0x40 },
1211 { 0x00dd11, 0x20, 0x20 },
1212 { 0x00dd88, (frame_size
>> 0) & 0xff, 0xff},
1213 { 0x00dd89, (frame_size
>> 8) & 0xff, 0xff},
1214 { 0x00dd0c, packet_size
, 0xff},
1215 { 0x00dd11, state
->dual_mode
<< 6, 0x40 },
1216 { 0x00dd8a, (frame_size
>> 0) & 0xff, 0xff},
1217 { 0x00dd8b, (frame_size
>> 8) & 0xff, 0xff},
1218 { 0x00dd0d, packet_size
, 0xff },
1219 { 0x80f9a3, state
->dual_mode
, 0x01 },
1220 { 0x80f9cd, state
->dual_mode
, 0x01 },
1221 { 0x80f99d, 0x00, 0x01 },
1222 { 0x80f9a4, 0x00, 0x01 },
1225 dev_dbg(&d
->udev
->dev
,
1226 "%s: USB speed=%d frame_size=%04x packet_size=%02x\n",
1227 __func__
, d
->udev
->speed
, frame_size
, packet_size
);
1229 /* init endpoints */
1230 for (i
= 0; i
< ARRAY_SIZE(tab
); i
++) {
1231 ret
= af9035_wr_reg_mask(d
, tab
[i
].reg
, tab
[i
].val
,
1240 dev_dbg(&d
->udev
->dev
, "%s: failed=%d\n", __func__
, ret
);
1245 #if IS_ENABLED(CONFIG_RC_CORE)
1246 static int af9035_rc_query(struct dvb_usb_device
*d
)
1251 struct usb_req req
= { CMD_IR_GET
, 0, 0, NULL
, 4, buf
};
1253 ret
= af9035_ctrl_msg(d
, &req
);
1259 if ((buf
[2] + buf
[3]) == 0xff) {
1260 if ((buf
[0] + buf
[1]) == 0xff) {
1261 /* NEC standard 16bit */
1262 key
= buf
[0] << 8 | buf
[2];
1264 /* NEC extended 24bit */
1265 key
= buf
[0] << 16 | buf
[1] << 8 | buf
[2];
1268 /* NEC full code 32bit */
1269 key
= buf
[0] << 24 | buf
[1] << 16 | buf
[2] << 8 | buf
[3];
1272 dev_dbg(&d
->udev
->dev
, "%s: %*ph\n", __func__
, 4, buf
);
1274 rc_keydown(d
->rc_dev
, key
, 0);
1279 dev_dbg(&d
->udev
->dev
, "%s: failed=%d\n", __func__
, ret
);
1284 static int af9035_get_rc_config(struct dvb_usb_device
*d
, struct dvb_usb_rc
*rc
)
1286 struct state
*state
= d_to_priv(d
);
1290 ret
= af9035_rd_reg(d
, state
->eeprom_addr
+ EEPROM_IR_MODE
, &tmp
);
1294 dev_dbg(&d
->udev
->dev
, "%s: ir_mode=%02x\n", __func__
, tmp
);
1296 /* don't activate rc if in HID mode or if not available */
1298 ret
= af9035_rd_reg(d
, state
->eeprom_addr
+ EEPROM_IR_TYPE
,
1303 dev_dbg(&d
->udev
->dev
, "%s: ir_type=%02x\n", __func__
, tmp
);
1308 rc
->allowed_protos
= RC_BIT_NEC
;
1311 rc
->allowed_protos
= RC_BIT_RC6_MCE
;
1315 rc
->query
= af9035_rc_query
;
1318 /* load empty to enable rc */
1320 rc
->map_name
= RC_MAP_EMPTY
;
1326 dev_dbg(&d
->udev
->dev
, "%s: failed=%d\n", __func__
, ret
);
1331 #define af9035_get_rc_config NULL
1334 static int af9035_get_stream_config(struct dvb_frontend
*fe
, u8
*ts_type
,
1335 struct usb_data_stream_properties
*stream
)
1337 struct dvb_usb_device
*d
= fe_to_d(fe
);
1338 dev_dbg(&d
->udev
->dev
, "%s: adap=%d\n", __func__
, fe_to_adap(fe
)->id
);
1340 if (d
->udev
->speed
== USB_SPEED_FULL
)
1341 stream
->u
.bulk
.buffersize
= 5 * 188;
1347 * FIXME: PID filter is property of demodulator and should be moved to the
1348 * correct driver. Also we support only adapter #0 PID filter and will
1349 * disable adapter #1 if USB1.1 is used.
1351 static int af9035_pid_filter_ctrl(struct dvb_usb_adapter
*adap
, int onoff
)
1353 struct dvb_usb_device
*d
= adap_to_d(adap
);
1356 dev_dbg(&d
->udev
->dev
, "%s: onoff=%d\n", __func__
, onoff
);
1358 ret
= af9035_wr_reg_mask(d
, 0x80f993, onoff
, 0x01);
1365 dev_dbg(&d
->udev
->dev
, "%s: failed=%d\n", __func__
, ret
);
1370 static int af9035_pid_filter(struct dvb_usb_adapter
*adap
, int index
, u16 pid
,
1373 struct dvb_usb_device
*d
= adap_to_d(adap
);
1375 u8 wbuf
[2] = {(pid
>> 0) & 0xff, (pid
>> 8) & 0xff};
1377 dev_dbg(&d
->udev
->dev
, "%s: index=%d pid=%04x onoff=%d\n",
1378 __func__
, index
, pid
, onoff
);
1380 ret
= af9035_wr_regs(d
, 0x80f996, wbuf
, 2);
1384 ret
= af9035_wr_reg(d
, 0x80f994, onoff
);
1388 ret
= af9035_wr_reg(d
, 0x80f995, index
);
1395 dev_dbg(&d
->udev
->dev
, "%s: failed=%d\n", __func__
, ret
);
1400 static int af9035_probe(struct usb_interface
*intf
,
1401 const struct usb_device_id
*id
)
1403 struct usb_device
*udev
= interface_to_usbdev(intf
);
1404 char manufacturer
[sizeof("Afatech")];
1406 memset(manufacturer
, 0, sizeof(manufacturer
));
1407 usb_string(udev
, udev
->descriptor
.iManufacturer
,
1408 manufacturer
, sizeof(manufacturer
));
1410 * There is two devices having same ID but different chipset. One uses
1411 * AF9015 and the other IT9135 chipset. Only difference seen on lsusb
1412 * is iManufacturer string.
1414 * idVendor 0x0ccd TerraTec Electronic GmbH
1417 * iManufacturer 1 Afatech
1418 * iProduct 2 DVB-T 2
1420 * idVendor 0x0ccd TerraTec Electronic GmbH
1423 * iManufacturer 1 ITE Technologies, Inc.
1424 * iProduct 2 DVB-T TV Stick
1426 if ((le16_to_cpu(udev
->descriptor
.idVendor
) == USB_VID_TERRATEC
) &&
1427 (le16_to_cpu(udev
->descriptor
.idProduct
) == 0x0099)) {
1428 if (!strcmp("Afatech", manufacturer
)) {
1429 dev_dbg(&udev
->dev
, "%s: rejecting device\n", __func__
);
1434 return dvb_usbv2_probe(intf
, id
);
1437 /* interface 0 is used by DVB-T receiver and
1438 interface 1 is for remote controller (HID) */
1439 static const struct dvb_usb_device_properties af9035_props
= {
1440 .driver_name
= KBUILD_MODNAME
,
1441 .owner
= THIS_MODULE
,
1442 .adapter_nr
= adapter_nr
,
1443 .size_of_priv
= sizeof(struct state
),
1445 .generic_bulk_ctrl_endpoint
= 0x02,
1446 .generic_bulk_ctrl_endpoint_response
= 0x81,
1448 .identify_state
= af9035_identify_state
,
1449 .download_firmware
= af9035_download_firmware
,
1451 .i2c_algo
= &af9035_i2c_algo
,
1452 .read_config
= af9035_read_config
,
1453 .frontend_attach
= af9035_frontend_attach
,
1454 .tuner_attach
= af9035_tuner_attach
,
1455 .init
= af9035_init
,
1456 .get_rc_config
= af9035_get_rc_config
,
1457 .get_stream_config
= af9035_get_stream_config
,
1459 .get_adapter_count
= af9035_get_adapter_count
,
1462 .caps
= DVB_USB_ADAP_HAS_PID_FILTER
|
1463 DVB_USB_ADAP_PID_FILTER_CAN_BE_TURNED_OFF
,
1465 .pid_filter_count
= 32,
1466 .pid_filter_ctrl
= af9035_pid_filter_ctrl
,
1467 .pid_filter
= af9035_pid_filter
,
1469 .stream
= DVB_USB_STREAM_BULK(0x84, 6, 87 * 188),
1471 .stream
= DVB_USB_STREAM_BULK(0x85, 6, 87 * 188),
1476 static const struct usb_device_id af9035_id_table
[] = {
1477 /* AF9035 devices */
1478 { DVB_USB_DEVICE(USB_VID_AFATECH
, USB_PID_AFATECH_AF9035_9035
,
1479 &af9035_props
, "Afatech AF9035 reference design", NULL
) },
1480 { DVB_USB_DEVICE(USB_VID_AFATECH
, USB_PID_AFATECH_AF9035_1000
,
1481 &af9035_props
, "Afatech AF9035 reference design", NULL
) },
1482 { DVB_USB_DEVICE(USB_VID_AFATECH
, USB_PID_AFATECH_AF9035_1001
,
1483 &af9035_props
, "Afatech AF9035 reference design", NULL
) },
1484 { DVB_USB_DEVICE(USB_VID_AFATECH
, USB_PID_AFATECH_AF9035_1002
,
1485 &af9035_props
, "Afatech AF9035 reference design", NULL
) },
1486 { DVB_USB_DEVICE(USB_VID_AFATECH
, USB_PID_AFATECH_AF9035_1003
,
1487 &af9035_props
, "Afatech AF9035 reference design", NULL
) },
1488 { DVB_USB_DEVICE(USB_VID_TERRATEC
, USB_PID_TERRATEC_CINERGY_T_STICK
,
1489 &af9035_props
, "TerraTec Cinergy T Stick", NULL
) },
1490 { DVB_USB_DEVICE(USB_VID_AVERMEDIA
, USB_PID_AVERMEDIA_A835
,
1491 &af9035_props
, "AVerMedia AVerTV Volar HD/PRO (A835)", NULL
) },
1492 { DVB_USB_DEVICE(USB_VID_AVERMEDIA
, USB_PID_AVERMEDIA_B835
,
1493 &af9035_props
, "AVerMedia AVerTV Volar HD/PRO (A835)", NULL
) },
1494 { DVB_USB_DEVICE(USB_VID_AVERMEDIA
, USB_PID_AVERMEDIA_1867
,
1495 &af9035_props
, "AVerMedia HD Volar (A867)", NULL
) },
1496 { DVB_USB_DEVICE(USB_VID_AVERMEDIA
, USB_PID_AVERMEDIA_A867
,
1497 &af9035_props
, "AVerMedia HD Volar (A867)", NULL
) },
1498 { DVB_USB_DEVICE(USB_VID_AVERMEDIA
, USB_PID_AVERMEDIA_TWINSTAR
,
1499 &af9035_props
, "AVerMedia Twinstar (A825)", NULL
) },
1500 { DVB_USB_DEVICE(USB_VID_ASUS
, USB_PID_ASUS_U3100MINI_PLUS
,
1501 &af9035_props
, "Asus U3100Mini Plus", NULL
) },
1502 { DVB_USB_DEVICE(USB_VID_TERRATEC
, 0x00aa,
1503 &af9035_props
, "TerraTec Cinergy T Stick (rev. 2)", NULL
) },
1504 /* IT9135 devices */
1506 { DVB_USB_DEVICE(0x048d, 0x9135,
1507 &af9035_props
, "IT9135 reference design", NULL
) },
1508 { DVB_USB_DEVICE(0x048d, 0x9006,
1509 &af9035_props
, "IT9135 reference design", NULL
) },
1511 /* XXX: that same ID [0ccd:0099] is used by af9015 driver too */
1512 { DVB_USB_DEVICE(USB_VID_TERRATEC
, 0x0099,
1513 &af9035_props
, "TerraTec Cinergy T Stick Dual RC (rev. 2)", NULL
) },
1516 MODULE_DEVICE_TABLE(usb
, af9035_id_table
);
1518 static struct usb_driver af9035_usb_driver
= {
1519 .name
= KBUILD_MODNAME
,
1520 .id_table
= af9035_id_table
,
1521 .probe
= af9035_probe
,
1522 .disconnect
= dvb_usbv2_disconnect
,
1523 .suspend
= dvb_usbv2_suspend
,
1524 .resume
= dvb_usbv2_resume
,
1525 .reset_resume
= dvb_usbv2_reset_resume
,
1530 module_usb_driver(af9035_usb_driver
);
1532 MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
1533 MODULE_DESCRIPTION("Afatech AF9035 driver");
1534 MODULE_LICENSE("GPL");
1535 MODULE_FIRMWARE(AF9035_FIRMWARE_AF9035
);
1536 MODULE_FIRMWARE(AF9035_FIRMWARE_IT9135_V1
);
1537 MODULE_FIRMWARE(AF9035_FIRMWARE_IT9135_V2
);