2 * Realtek RTL28xxU DVB USB driver
4 * Copyright (C) 2009 Antti Palosaari <crope@iki.fi>
5 * Copyright (C) 2011 Antti Palosaari <crope@iki.fi>
6 * Copyright (C) 2012 Thomas Mair <thomas.mair86@googlemail.com>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25 static int rtl28xxu_disable_rc
;
26 module_param_named(disable_rc
, rtl28xxu_disable_rc
, int, 0644);
27 MODULE_PARM_DESC(disable_rc
, "disable RTL2832U remote controller");
28 DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr
);
30 static int rtl28xxu_ctrl_msg(struct dvb_usb_device
*d
, struct rtl28xxu_req
*req
)
32 struct rtl28xxu_dev
*dev
= d
->priv
;
37 mutex_lock(&d
->usb_mutex
);
39 if (req
->size
> sizeof(dev
->buf
)) {
40 dev_err(&d
->intf
->dev
, "too large message %u\n", req
->size
);
42 goto err_mutex_unlock
;
45 if (req
->index
& CMD_WR_FLAG
) {
47 memcpy(dev
->buf
, req
->data
, req
->size
);
48 requesttype
= (USB_TYPE_VENDOR
| USB_DIR_OUT
);
49 pipe
= usb_sndctrlpipe(d
->udev
, 0);
52 requesttype
= (USB_TYPE_VENDOR
| USB_DIR_IN
);
53 pipe
= usb_rcvctrlpipe(d
->udev
, 0);
56 ret
= usb_control_msg(d
->udev
, pipe
, 0, requesttype
, req
->value
,
57 req
->index
, dev
->buf
, req
->size
, 1000);
58 dvb_usb_dbg_usb_control_msg(d
->udev
, 0, requesttype
, req
->value
,
59 req
->index
, dev
->buf
, req
->size
);
61 goto err_mutex_unlock
;
63 /* read request, copy returned data to return buf */
64 if (requesttype
== (USB_TYPE_VENDOR
| USB_DIR_IN
))
65 memcpy(req
->data
, dev
->buf
, req
->size
);
67 mutex_unlock(&d
->usb_mutex
);
71 mutex_unlock(&d
->usb_mutex
);
72 dev_dbg(&d
->intf
->dev
, "failed=%d\n", ret
);
76 static int rtl28xxu_wr_regs(struct dvb_usb_device
*d
, u16 reg
, u8
*val
, int len
)
78 struct rtl28xxu_req req
;
81 req
.index
= CMD_USB_WR
;
82 else if (reg
< 0x4000)
83 req
.index
= CMD_SYS_WR
;
85 req
.index
= CMD_IR_WR
;
91 return rtl28xxu_ctrl_msg(d
, &req
);
94 static int rtl28xxu_rd_regs(struct dvb_usb_device
*d
, u16 reg
, u8
*val
, int len
)
96 struct rtl28xxu_req req
;
99 req
.index
= CMD_USB_RD
;
100 else if (reg
< 0x4000)
101 req
.index
= CMD_SYS_RD
;
103 req
.index
= CMD_IR_RD
;
109 return rtl28xxu_ctrl_msg(d
, &req
);
112 static int rtl28xxu_wr_reg(struct dvb_usb_device
*d
, u16 reg
, u8 val
)
114 return rtl28xxu_wr_regs(d
, reg
, &val
, 1);
117 static int rtl28xxu_rd_reg(struct dvb_usb_device
*d
, u16 reg
, u8
*val
)
119 return rtl28xxu_rd_regs(d
, reg
, val
, 1);
122 static int rtl28xxu_wr_reg_mask(struct dvb_usb_device
*d
, u16 reg
, u8 val
,
128 /* no need for read if whole reg is written */
130 ret
= rtl28xxu_rd_reg(d
, reg
, &tmp
);
139 return rtl28xxu_wr_reg(d
, reg
, val
);
143 static int rtl28xxu_i2c_xfer(struct i2c_adapter
*adap
, struct i2c_msg msg
[],
147 struct dvb_usb_device
*d
= i2c_get_adapdata(adap
);
148 struct rtl28xxu_dev
*dev
= d
->priv
;
149 struct rtl28xxu_req req
;
152 * It is not known which are real I2C bus xfer limits, but testing
153 * with RTL2831U + MT2060 gives max RD 24 and max WR 22 bytes.
154 * TODO: find out RTL2832U lens
158 * I2C adapter logic looks rather complicated due to fact it handles
159 * three different access methods. Those methods are;
160 * 1) integrated demod access
164 * Used method is selected in order 1, 2, 3. Method 3 can handle all
165 * requests but there is two reasons why not use it always;
166 * 1) It is most expensive, usually two USB messages are needed
167 * 2) At least RTL2831U does not support it
169 * Method 3 is needed in case of I2C write+read (typical register read)
170 * where write is more than one byte.
173 if (mutex_lock_interruptible(&d
->i2c_mutex
) < 0)
176 if (num
== 2 && !(msg
[0].flags
& I2C_M_RD
) &&
177 (msg
[1].flags
& I2C_M_RD
)) {
178 if (msg
[0].len
> 24 || msg
[1].len
> 24) {
179 /* TODO: check msg[0].len max */
181 goto err_mutex_unlock
;
182 } else if (msg
[0].addr
== 0x10) {
183 /* method 1 - integrated demod */
184 if (msg
[0].buf
[0] == 0x00) {
185 /* return demod page from driver cache */
186 msg
[1].buf
[0] = dev
->page
;
189 req
.value
= (msg
[0].buf
[0] << 8) | (msg
[0].addr
<< 1);
190 req
.index
= CMD_DEMOD_RD
| dev
->page
;
191 req
.size
= msg
[1].len
;
192 req
.data
= &msg
[1].buf
[0];
193 ret
= rtl28xxu_ctrl_msg(d
, &req
);
195 } else if (msg
[0].len
< 2) {
196 /* method 2 - old I2C */
197 req
.value
= (msg
[0].buf
[0] << 8) | (msg
[0].addr
<< 1);
198 req
.index
= CMD_I2C_RD
;
199 req
.size
= msg
[1].len
;
200 req
.data
= &msg
[1].buf
[0];
201 ret
= rtl28xxu_ctrl_msg(d
, &req
);
203 /* method 3 - new I2C */
204 req
.value
= (msg
[0].addr
<< 1);
205 req
.index
= CMD_I2C_DA_WR
;
206 req
.size
= msg
[0].len
;
207 req
.data
= msg
[0].buf
;
208 ret
= rtl28xxu_ctrl_msg(d
, &req
);
210 goto err_mutex_unlock
;
212 req
.value
= (msg
[0].addr
<< 1);
213 req
.index
= CMD_I2C_DA_RD
;
214 req
.size
= msg
[1].len
;
215 req
.data
= msg
[1].buf
;
216 ret
= rtl28xxu_ctrl_msg(d
, &req
);
218 } else if (num
== 1 && !(msg
[0].flags
& I2C_M_RD
)) {
219 if (msg
[0].len
> 22) {
220 /* TODO: check msg[0].len max */
222 goto err_mutex_unlock
;
223 } else if (msg
[0].addr
== 0x10) {
224 /* method 1 - integrated demod */
225 if (msg
[0].buf
[0] == 0x00) {
226 /* save demod page for later demod access */
227 dev
->page
= msg
[0].buf
[1];
230 req
.value
= (msg
[0].buf
[0] << 8) |
232 req
.index
= CMD_DEMOD_WR
| dev
->page
;
233 req
.size
= msg
[0].len
-1;
234 req
.data
= &msg
[0].buf
[1];
235 ret
= rtl28xxu_ctrl_msg(d
, &req
);
237 } else if ((msg
[0].len
< 23) && (!dev
->new_i2c_write
)) {
238 /* method 2 - old I2C */
239 req
.value
= (msg
[0].buf
[0] << 8) | (msg
[0].addr
<< 1);
240 req
.index
= CMD_I2C_WR
;
241 req
.size
= msg
[0].len
-1;
242 req
.data
= &msg
[0].buf
[1];
243 ret
= rtl28xxu_ctrl_msg(d
, &req
);
245 /* method 3 - new I2C */
246 req
.value
= (msg
[0].addr
<< 1);
247 req
.index
= CMD_I2C_DA_WR
;
248 req
.size
= msg
[0].len
;
249 req
.data
= msg
[0].buf
;
250 ret
= rtl28xxu_ctrl_msg(d
, &req
);
252 } else if (num
== 1 && (msg
[0].flags
& I2C_M_RD
)) {
253 req
.value
= (msg
[0].addr
<< 1);
254 req
.index
= CMD_I2C_DA_RD
;
255 req
.size
= msg
[0].len
;
256 req
.data
= msg
[0].buf
;
257 ret
= rtl28xxu_ctrl_msg(d
, &req
);
262 /* Retry failed I2C messages */
267 mutex_unlock(&d
->i2c_mutex
);
269 return ret
? ret
: num
;
272 static u32
rtl28xxu_i2c_func(struct i2c_adapter
*adapter
)
277 static struct i2c_algorithm rtl28xxu_i2c_algo
= {
278 .master_xfer
= rtl28xxu_i2c_xfer
,
279 .functionality
= rtl28xxu_i2c_func
,
282 static int rtl2831u_read_config(struct dvb_usb_device
*d
)
284 struct rtl28xxu_dev
*dev
= d_to_priv(d
);
287 /* open RTL2831U/RTL2830 I2C gate */
288 struct rtl28xxu_req req_gate_open
= {0x0120, 0x0011, 0x0001, "\x08"};
290 struct rtl28xxu_req req_mt2060
= {0x00c0, CMD_I2C_RD
, 1, buf
};
291 struct rtl28xxu_req req_qt1010
= {0x0fc4, CMD_I2C_RD
, 1, buf
};
293 dev_dbg(&d
->intf
->dev
, "\n");
297 * =========================================================
298 * GPIO0 | tuner#0 | 0 off | 1 on | MXL5005S (?)
299 * GPIO2 | LED | 0 off | 1 on |
300 * GPIO4 | tuner#1 | 0 on | 1 off | MT2060
304 ret
= rtl28xxu_wr_reg(d
, SYS_GPIO_DIR
, 0x0a);
308 /* enable as output GPIO0, GPIO2, GPIO4 */
309 ret
= rtl28xxu_wr_reg(d
, SYS_GPIO_OUT_EN
, 0x15);
314 * Probe used tuner. We need to know used tuner before demod attach
315 * since there is some demod params needed to set according to tuner.
318 /* demod needs some time to wake up */
321 dev
->tuner_name
= "NONE";
323 /* open demod I2C gate */
324 ret
= rtl28xxu_ctrl_msg(d
, &req_gate_open
);
328 /* check QT1010 ID(?) register; reg=0f val=2c */
329 ret
= rtl28xxu_ctrl_msg(d
, &req_qt1010
);
330 if (ret
== 0 && buf
[0] == 0x2c) {
331 dev
->tuner
= TUNER_RTL2830_QT1010
;
332 dev
->tuner_name
= "QT1010";
336 /* open demod I2C gate */
337 ret
= rtl28xxu_ctrl_msg(d
, &req_gate_open
);
341 /* check MT2060 ID register; reg=00 val=63 */
342 ret
= rtl28xxu_ctrl_msg(d
, &req_mt2060
);
343 if (ret
== 0 && buf
[0] == 0x63) {
344 dev
->tuner
= TUNER_RTL2830_MT2060
;
345 dev
->tuner_name
= "MT2060";
349 /* assume MXL5005S */
350 dev
->tuner
= TUNER_RTL2830_MXL5005S
;
351 dev
->tuner_name
= "MXL5005S";
355 dev_dbg(&d
->intf
->dev
, "tuner=%s\n", dev
->tuner_name
);
359 dev_dbg(&d
->intf
->dev
, "failed=%d\n", ret
);
363 static int rtl2832u_read_config(struct dvb_usb_device
*d
)
365 struct rtl28xxu_dev
*dev
= d_to_priv(d
);
368 /* open RTL2832U/RTL2832 I2C gate */
369 struct rtl28xxu_req req_gate_open
= {0x0120, 0x0011, 0x0001, "\x18"};
370 /* close RTL2832U/RTL2832 I2C gate */
371 struct rtl28xxu_req req_gate_close
= {0x0120, 0x0011, 0x0001, "\x10"};
373 struct rtl28xxu_req req_fc0012
= {0x00c6, CMD_I2C_RD
, 1, buf
};
374 struct rtl28xxu_req req_fc0013
= {0x00c6, CMD_I2C_RD
, 1, buf
};
375 struct rtl28xxu_req req_mt2266
= {0x00c0, CMD_I2C_RD
, 1, buf
};
376 struct rtl28xxu_req req_fc2580
= {0x01ac, CMD_I2C_RD
, 1, buf
};
377 struct rtl28xxu_req req_mt2063
= {0x00c0, CMD_I2C_RD
, 1, buf
};
378 struct rtl28xxu_req req_max3543
= {0x00c0, CMD_I2C_RD
, 1, buf
};
379 struct rtl28xxu_req req_tua9001
= {0x7ec0, CMD_I2C_RD
, 2, buf
};
380 struct rtl28xxu_req req_mxl5007t
= {0xd9c0, CMD_I2C_RD
, 1, buf
};
381 struct rtl28xxu_req req_e4000
= {0x02c8, CMD_I2C_RD
, 1, buf
};
382 struct rtl28xxu_req req_tda18272
= {0x00c0, CMD_I2C_RD
, 2, buf
};
383 struct rtl28xxu_req req_r820t
= {0x0034, CMD_I2C_RD
, 1, buf
};
384 struct rtl28xxu_req req_r828d
= {0x0074, CMD_I2C_RD
, 1, buf
};
385 struct rtl28xxu_req req_mn88472
= {0xff38, CMD_I2C_RD
, 1, buf
};
386 struct rtl28xxu_req req_mn88473
= {0xff38, CMD_I2C_RD
, 1, buf
};
387 struct rtl28xxu_req req_si2157
= {0x00c0, CMD_I2C_RD
, 1, buf
};
388 struct rtl28xxu_req req_si2168
= {0x00c8, CMD_I2C_RD
, 1, buf
};
390 dev_dbg(&d
->intf
->dev
, "\n");
392 /* enable GPIO3 and GPIO6 as output */
393 ret
= rtl28xxu_wr_reg_mask(d
, SYS_GPIO_DIR
, 0x00, 0x40);
397 ret
= rtl28xxu_wr_reg_mask(d
, SYS_GPIO_OUT_EN
, 0x48, 0x48);
402 * Probe used tuner. We need to know used tuner before demod attach
403 * since there is some demod params needed to set according to tuner.
406 /* open demod I2C gate */
407 ret
= rtl28xxu_ctrl_msg(d
, &req_gate_open
);
411 dev
->tuner_name
= "NONE";
413 /* check FC0012 ID register; reg=00 val=a1 */
414 ret
= rtl28xxu_ctrl_msg(d
, &req_fc0012
);
415 if (ret
== 0 && buf
[0] == 0xa1) {
416 dev
->tuner
= TUNER_RTL2832_FC0012
;
417 dev
->tuner_name
= "FC0012";
421 /* check FC0013 ID register; reg=00 val=a3 */
422 ret
= rtl28xxu_ctrl_msg(d
, &req_fc0013
);
423 if (ret
== 0 && buf
[0] == 0xa3) {
424 dev
->tuner
= TUNER_RTL2832_FC0013
;
425 dev
->tuner_name
= "FC0013";
429 /* check MT2266 ID register; reg=00 val=85 */
430 ret
= rtl28xxu_ctrl_msg(d
, &req_mt2266
);
431 if (ret
== 0 && buf
[0] == 0x85) {
432 dev
->tuner
= TUNER_RTL2832_MT2266
;
433 dev
->tuner_name
= "MT2266";
437 /* check FC2580 ID register; reg=01 val=56 */
438 ret
= rtl28xxu_ctrl_msg(d
, &req_fc2580
);
439 if (ret
== 0 && buf
[0] == 0x56) {
440 dev
->tuner
= TUNER_RTL2832_FC2580
;
441 dev
->tuner_name
= "FC2580";
445 /* check MT2063 ID register; reg=00 val=9e || 9c */
446 ret
= rtl28xxu_ctrl_msg(d
, &req_mt2063
);
447 if (ret
== 0 && (buf
[0] == 0x9e || buf
[0] == 0x9c)) {
448 dev
->tuner
= TUNER_RTL2832_MT2063
;
449 dev
->tuner_name
= "MT2063";
453 /* check MAX3543 ID register; reg=00 val=38 */
454 ret
= rtl28xxu_ctrl_msg(d
, &req_max3543
);
455 if (ret
== 0 && buf
[0] == 0x38) {
456 dev
->tuner
= TUNER_RTL2832_MAX3543
;
457 dev
->tuner_name
= "MAX3543";
461 /* check TUA9001 ID register; reg=7e val=2328 */
462 ret
= rtl28xxu_ctrl_msg(d
, &req_tua9001
);
463 if (ret
== 0 && buf
[0] == 0x23 && buf
[1] == 0x28) {
464 dev
->tuner
= TUNER_RTL2832_TUA9001
;
465 dev
->tuner_name
= "TUA9001";
469 /* check MXL5007R ID register; reg=d9 val=14 */
470 ret
= rtl28xxu_ctrl_msg(d
, &req_mxl5007t
);
471 if (ret
== 0 && buf
[0] == 0x14) {
472 dev
->tuner
= TUNER_RTL2832_MXL5007T
;
473 dev
->tuner_name
= "MXL5007T";
477 /* check E4000 ID register; reg=02 val=40 */
478 ret
= rtl28xxu_ctrl_msg(d
, &req_e4000
);
479 if (ret
== 0 && buf
[0] == 0x40) {
480 dev
->tuner
= TUNER_RTL2832_E4000
;
481 dev
->tuner_name
= "E4000";
485 /* check TDA18272 ID register; reg=00 val=c760 */
486 ret
= rtl28xxu_ctrl_msg(d
, &req_tda18272
);
487 if (ret
== 0 && (buf
[0] == 0xc7 || buf
[1] == 0x60)) {
488 dev
->tuner
= TUNER_RTL2832_TDA18272
;
489 dev
->tuner_name
= "TDA18272";
493 /* check R820T ID register; reg=00 val=69 */
494 ret
= rtl28xxu_ctrl_msg(d
, &req_r820t
);
495 if (ret
== 0 && buf
[0] == 0x69) {
496 dev
->tuner
= TUNER_RTL2832_R820T
;
497 dev
->tuner_name
= "R820T";
501 /* check R828D ID register; reg=00 val=69 */
502 ret
= rtl28xxu_ctrl_msg(d
, &req_r828d
);
503 if (ret
== 0 && buf
[0] == 0x69) {
504 dev
->tuner
= TUNER_RTL2832_R828D
;
505 dev
->tuner_name
= "R828D";
509 /* GPIO0 and GPIO5 to reset Si2157/Si2168 tuner and demod */
510 ret
= rtl28xxu_wr_reg_mask(d
, SYS_GPIO_OUT_VAL
, 0x00, 0x21);
514 ret
= rtl28xxu_wr_reg_mask(d
, SYS_GPIO_OUT_EN
, 0x00, 0x21);
520 ret
= rtl28xxu_wr_reg_mask(d
, SYS_GPIO_OUT_VAL
, 0x21, 0x21);
524 ret
= rtl28xxu_wr_reg_mask(d
, SYS_GPIO_OUT_EN
, 0x21, 0x21);
530 /* check Si2157 ID register; reg=c0 val=80 */
531 ret
= rtl28xxu_ctrl_msg(d
, &req_si2157
);
532 if (ret
== 0 && ((buf
[0] & 0x80) == 0x80)) {
533 dev
->tuner
= TUNER_RTL2832_SI2157
;
534 dev
->tuner_name
= "SI2157";
539 dev_dbg(&d
->intf
->dev
, "tuner=%s\n", dev
->tuner_name
);
541 /* probe slave demod */
542 if (dev
->tuner
== TUNER_RTL2832_R828D
) {
543 /* power on MN88472 demod on GPIO0 */
544 ret
= rtl28xxu_wr_reg_mask(d
, SYS_GPIO_OUT_VAL
, 0x01, 0x01);
548 ret
= rtl28xxu_wr_reg_mask(d
, SYS_GPIO_DIR
, 0x00, 0x01);
552 ret
= rtl28xxu_wr_reg_mask(d
, SYS_GPIO_OUT_EN
, 0x01, 0x01);
556 /* check MN88472 answers */
557 ret
= rtl28xxu_ctrl_msg(d
, &req_mn88472
);
558 if (ret
== 0 && buf
[0] == 0x02) {
559 dev_dbg(&d
->intf
->dev
, "MN88472 found\n");
560 dev
->slave_demod
= SLAVE_DEMOD_MN88472
;
564 ret
= rtl28xxu_ctrl_msg(d
, &req_mn88473
);
565 if (ret
== 0 && buf
[0] == 0x03) {
566 dev_dbg(&d
->intf
->dev
, "MN88473 found\n");
567 dev
->slave_demod
= SLAVE_DEMOD_MN88473
;
571 if (dev
->tuner
== TUNER_RTL2832_SI2157
) {
572 /* check Si2168 ID register; reg=c8 val=80 */
573 ret
= rtl28xxu_ctrl_msg(d
, &req_si2168
);
574 if (ret
== 0 && ((buf
[0] & 0x80) == 0x80)) {
575 dev_dbg(&d
->intf
->dev
, "Si2168 found\n");
576 dev
->slave_demod
= SLAVE_DEMOD_SI2168
;
582 /* close demod I2C gate */
583 ret
= rtl28xxu_ctrl_msg(d
, &req_gate_close
);
589 dev_dbg(&d
->intf
->dev
, "failed=%d\n", ret
);
593 static int rtl28xxu_read_config(struct dvb_usb_device
*d
)
595 struct rtl28xxu_dev
*dev
= d_to_priv(d
);
597 if (dev
->chip_id
== CHIP_ID_RTL2831U
)
598 return rtl2831u_read_config(d
);
600 return rtl2832u_read_config(d
);
603 static int rtl28xxu_identify_state(struct dvb_usb_device
*d
, const char **name
)
605 struct rtl28xxu_dev
*dev
= d_to_priv(d
);
607 struct rtl28xxu_req req_demod_i2c
= {0x0020, CMD_I2C_DA_RD
, 0, NULL
};
609 dev_dbg(&d
->intf
->dev
, "\n");
612 * Detect chip type using I2C command that is not supported
615 ret
= rtl28xxu_ctrl_msg(d
, &req_demod_i2c
);
617 dev
->chip_id
= CHIP_ID_RTL2831U
;
618 } else if (ret
== 0) {
619 dev
->chip_id
= CHIP_ID_RTL2832U
;
621 dev_err(&d
->intf
->dev
, "chip type detection failed %d\n", ret
);
624 dev_dbg(&d
->intf
->dev
, "chip_id=%u\n", dev
->chip_id
);
626 /* Retry failed I2C messages */
627 d
->i2c_adap
.retries
= 3;
628 d
->i2c_adap
.timeout
= msecs_to_jiffies(10);
632 dev_dbg(&d
->intf
->dev
, "failed=%d\n", ret
);
636 static const struct rtl2830_platform_data rtl2830_mt2060_platform_data
= {
641 .agc_targ_val
= 0x2d,
645 static const struct rtl2830_platform_data rtl2830_qt1010_platform_data
= {
650 .agc_targ_val
= 0x2d,
653 static const struct rtl2830_platform_data rtl2830_mxl5005s_platform_data
= {
658 .agc_targ_val
= 0x3e,
661 static int rtl2831u_frontend_attach(struct dvb_usb_adapter
*adap
)
663 struct dvb_usb_device
*d
= adap_to_d(adap
);
664 struct rtl28xxu_dev
*dev
= d_to_priv(d
);
665 struct rtl2830_platform_data
*pdata
= &dev
->rtl2830_platform_data
;
666 struct i2c_board_info board_info
;
667 struct i2c_client
*client
;
670 dev_dbg(&d
->intf
->dev
, "\n");
672 switch (dev
->tuner
) {
673 case TUNER_RTL2830_QT1010
:
674 *pdata
= rtl2830_qt1010_platform_data
;
676 case TUNER_RTL2830_MT2060
:
677 *pdata
= rtl2830_mt2060_platform_data
;
679 case TUNER_RTL2830_MXL5005S
:
680 *pdata
= rtl2830_mxl5005s_platform_data
;
683 dev_err(&d
->intf
->dev
, "unknown tuner %s\n", dev
->tuner_name
);
688 /* attach demodulator */
689 memset(&board_info
, 0, sizeof(board_info
));
690 strlcpy(board_info
.type
, "rtl2830", I2C_NAME_SIZE
);
691 board_info
.addr
= 0x10;
692 board_info
.platform_data
= pdata
;
693 request_module("%s", board_info
.type
);
694 client
= i2c_new_device(&d
->i2c_adap
, &board_info
);
695 if (client
== NULL
|| client
->dev
.driver
== NULL
) {
700 if (!try_module_get(client
->dev
.driver
->owner
)) {
701 i2c_unregister_device(client
);
706 adap
->fe
[0] = pdata
->get_dvb_frontend(client
);
707 dev
->demod_i2c_adapter
= pdata
->get_i2c_adapter(client
);
709 dev
->i2c_client_demod
= client
;
713 dev_dbg(&d
->intf
->dev
, "failed=%d\n", ret
);
717 static const struct rtl2832_platform_data rtl2832_fc2580_platform_data
= {
719 .tuner
= TUNER_RTL2832_FC2580
,
722 static const struct rtl2832_platform_data rtl2832_fc0012_platform_data
= {
724 .tuner
= TUNER_RTL2832_FC0012
727 static const struct rtl2832_platform_data rtl2832_fc0013_platform_data
= {
729 .tuner
= TUNER_RTL2832_FC0013
732 static const struct rtl2832_platform_data rtl2832_tua9001_platform_data
= {
734 .tuner
= TUNER_RTL2832_TUA9001
,
737 static const struct rtl2832_platform_data rtl2832_e4000_platform_data
= {
739 .tuner
= TUNER_RTL2832_E4000
,
742 static const struct rtl2832_platform_data rtl2832_r820t_platform_data
= {
744 .tuner
= TUNER_RTL2832_R820T
,
747 static const struct rtl2832_platform_data rtl2832_si2157_platform_data
= {
749 .tuner
= TUNER_RTL2832_SI2157
,
752 static int rtl2832u_fc0012_tuner_callback(struct dvb_usb_device
*d
,
758 dev_dbg(&d
->intf
->dev
, "cmd=%d arg=%d\n", cmd
, arg
);
761 case FC_FE_CALLBACK_VHF_ENABLE
:
762 /* set output values */
763 ret
= rtl28xxu_rd_reg(d
, SYS_GPIO_OUT_VAL
, &val
);
768 val
&= 0xbf; /* set GPIO6 low */
770 val
|= 0x40; /* set GPIO6 high */
773 ret
= rtl28xxu_wr_reg(d
, SYS_GPIO_OUT_VAL
, val
);
783 dev_dbg(&d
->intf
->dev
, "failed=%d\n", ret
);
787 static int rtl2832u_tua9001_tuner_callback(struct dvb_usb_device
*d
,
793 dev_dbg(&d
->intf
->dev
, "cmd=%d arg=%d\n", cmd
, arg
);
796 * CEN always enabled by hardware wiring
802 case TUA9001_CMD_RESETN
:
808 ret
= rtl28xxu_wr_reg_mask(d
, SYS_GPIO_OUT_VAL
, val
, 0x10);
812 case TUA9001_CMD_RXEN
:
818 ret
= rtl28xxu_wr_reg_mask(d
, SYS_GPIO_OUT_VAL
, val
, 0x02);
826 dev_dbg(&d
->intf
->dev
, "failed=%d\n", ret
);
830 static int rtl2832u_frontend_callback(void *adapter_priv
, int component
,
833 struct i2c_adapter
*adapter
= adapter_priv
;
834 struct device
*parent
= adapter
->dev
.parent
;
835 struct i2c_adapter
*parent_adapter
;
836 struct dvb_usb_device
*d
;
837 struct rtl28xxu_dev
*dev
;
840 * All tuners are connected to demod muxed I2C adapter. We have to
841 * resolve its parent adapter in order to get handle for this driver
842 * private data. That is a bit hackish solution, GPIO or direct driver
843 * callback would be better...
845 if (parent
!= NULL
&& parent
->type
== &i2c_adapter_type
)
846 parent_adapter
= to_i2c_adapter(parent
);
850 d
= i2c_get_adapdata(parent_adapter
);
853 dev_dbg(&d
->intf
->dev
, "component=%d cmd=%d arg=%d\n",
854 component
, cmd
, arg
);
857 case DVB_FRONTEND_COMPONENT_TUNER
:
858 switch (dev
->tuner
) {
859 case TUNER_RTL2832_FC0012
:
860 return rtl2832u_fc0012_tuner_callback(d
, cmd
, arg
);
861 case TUNER_RTL2832_TUA9001
:
862 return rtl2832u_tua9001_tuner_callback(d
, cmd
, arg
);
869 static int rtl2832u_frontend_attach(struct dvb_usb_adapter
*adap
)
871 struct dvb_usb_device
*d
= adap_to_d(adap
);
872 struct rtl28xxu_dev
*dev
= d_to_priv(d
);
873 struct rtl2832_platform_data
*pdata
= &dev
->rtl2832_platform_data
;
874 struct i2c_board_info board_info
;
875 struct i2c_client
*client
;
878 dev_dbg(&d
->intf
->dev
, "\n");
880 switch (dev
->tuner
) {
881 case TUNER_RTL2832_FC0012
:
882 *pdata
= rtl2832_fc0012_platform_data
;
884 case TUNER_RTL2832_FC0013
:
885 *pdata
= rtl2832_fc0013_platform_data
;
887 case TUNER_RTL2832_FC2580
:
888 *pdata
= rtl2832_fc2580_platform_data
;
890 case TUNER_RTL2832_TUA9001
:
891 *pdata
= rtl2832_tua9001_platform_data
;
893 case TUNER_RTL2832_E4000
:
894 *pdata
= rtl2832_e4000_platform_data
;
896 case TUNER_RTL2832_R820T
:
897 case TUNER_RTL2832_R828D
:
898 *pdata
= rtl2832_r820t_platform_data
;
900 case TUNER_RTL2832_SI2157
:
901 *pdata
= rtl2832_si2157_platform_data
;
904 dev_err(&d
->intf
->dev
, "unknown tuner %s\n", dev
->tuner_name
);
909 /* attach demodulator */
910 memset(&board_info
, 0, sizeof(board_info
));
911 strlcpy(board_info
.type
, "rtl2832", I2C_NAME_SIZE
);
912 board_info
.addr
= 0x10;
913 board_info
.platform_data
= pdata
;
914 request_module("%s", board_info
.type
);
915 client
= i2c_new_device(&d
->i2c_adap
, &board_info
);
916 if (client
== NULL
|| client
->dev
.driver
== NULL
) {
921 if (!try_module_get(client
->dev
.driver
->owner
)) {
922 i2c_unregister_device(client
);
927 adap
->fe
[0] = pdata
->get_dvb_frontend(client
);
928 dev
->demod_i2c_adapter
= pdata
->get_i2c_adapter(client
);
930 dev
->i2c_client_demod
= client
;
932 /* set fe callback */
933 adap
->fe
[0]->callback
= rtl2832u_frontend_callback
;
935 if (dev
->slave_demod
) {
936 struct i2c_board_info info
= {};
939 * We continue on reduced mode, without DVB-T2/C, using master
940 * demod, when slave demod fails.
944 /* attach slave demodulator */
945 if (dev
->slave_demod
== SLAVE_DEMOD_MN88472
) {
946 struct mn88472_config mn88472_config
= {};
948 mn88472_config
.fe
= &adap
->fe
[1];
949 mn88472_config
.i2c_wr_max
= 22,
950 strlcpy(info
.type
, "mn88472", I2C_NAME_SIZE
);
951 mn88472_config
.xtal
= 20500000;
952 mn88472_config
.ts_mode
= SERIAL_TS_MODE
;
953 mn88472_config
.ts_clock
= VARIABLE_TS_CLOCK
;
955 info
.platform_data
= &mn88472_config
;
956 request_module(info
.type
);
957 client
= i2c_new_device(&d
->i2c_adap
, &info
);
958 if (client
== NULL
|| client
->dev
.driver
== NULL
) {
959 dev
->slave_demod
= SLAVE_DEMOD_NONE
;
960 goto err_slave_demod_failed
;
963 if (!try_module_get(client
->dev
.driver
->owner
)) {
964 i2c_unregister_device(client
);
965 dev
->slave_demod
= SLAVE_DEMOD_NONE
;
966 goto err_slave_demod_failed
;
969 dev
->i2c_client_slave_demod
= client
;
970 } else if (dev
->slave_demod
== SLAVE_DEMOD_MN88473
) {
971 struct mn88473_config mn88473_config
= {};
973 mn88473_config
.fe
= &adap
->fe
[1];
974 mn88473_config
.i2c_wr_max
= 22,
975 strlcpy(info
.type
, "mn88473", I2C_NAME_SIZE
);
977 info
.platform_data
= &mn88473_config
;
978 request_module(info
.type
);
979 client
= i2c_new_device(&d
->i2c_adap
, &info
);
980 if (client
== NULL
|| client
->dev
.driver
== NULL
) {
981 dev
->slave_demod
= SLAVE_DEMOD_NONE
;
982 goto err_slave_demod_failed
;
985 if (!try_module_get(client
->dev
.driver
->owner
)) {
986 i2c_unregister_device(client
);
987 dev
->slave_demod
= SLAVE_DEMOD_NONE
;
988 goto err_slave_demod_failed
;
991 dev
->i2c_client_slave_demod
= client
;
993 struct si2168_config si2168_config
= {};
994 struct i2c_adapter
*adapter
;
996 si2168_config
.i2c_adapter
= &adapter
;
997 si2168_config
.fe
= &adap
->fe
[1];
998 si2168_config
.ts_mode
= SI2168_TS_SERIAL
;
999 si2168_config
.ts_clock_inv
= false;
1000 si2168_config
.ts_clock_gapped
= true;
1001 strlcpy(info
.type
, "si2168", I2C_NAME_SIZE
);
1003 info
.platform_data
= &si2168_config
;
1004 request_module(info
.type
);
1005 client
= i2c_new_device(&d
->i2c_adap
, &info
);
1006 if (client
== NULL
|| client
->dev
.driver
== NULL
) {
1007 dev
->slave_demod
= SLAVE_DEMOD_NONE
;
1008 goto err_slave_demod_failed
;
1011 if (!try_module_get(client
->dev
.driver
->owner
)) {
1012 i2c_unregister_device(client
);
1013 dev
->slave_demod
= SLAVE_DEMOD_NONE
;
1014 goto err_slave_demod_failed
;
1017 dev
->i2c_client_slave_demod
= client
;
1019 /* for Si2168 devices use only new I2C write method */
1020 dev
->new_i2c_write
= true;
1024 err_slave_demod_failed
:
1026 dev_dbg(&d
->intf
->dev
, "failed=%d\n", ret
);
1030 static int rtl28xxu_frontend_attach(struct dvb_usb_adapter
*adap
)
1032 struct rtl28xxu_dev
*dev
= adap_to_priv(adap
);
1034 if (dev
->chip_id
== CHIP_ID_RTL2831U
)
1035 return rtl2831u_frontend_attach(adap
);
1037 return rtl2832u_frontend_attach(adap
);
1040 static int rtl28xxu_frontend_detach(struct dvb_usb_adapter
*adap
)
1042 struct dvb_usb_device
*d
= adap_to_d(adap
);
1043 struct rtl28xxu_dev
*dev
= d_to_priv(d
);
1044 struct i2c_client
*client
;
1046 dev_dbg(&d
->intf
->dev
, "\n");
1048 /* remove I2C slave demod */
1049 client
= dev
->i2c_client_slave_demod
;
1051 module_put(client
->dev
.driver
->owner
);
1052 i2c_unregister_device(client
);
1055 /* remove I2C demod */
1056 client
= dev
->i2c_client_demod
;
1058 module_put(client
->dev
.driver
->owner
);
1059 i2c_unregister_device(client
);
1065 static struct qt1010_config rtl28xxu_qt1010_config
= {
1066 .i2c_address
= 0x62, /* 0xc4 */
1069 static struct mt2060_config rtl28xxu_mt2060_config
= {
1070 .i2c_address
= 0x60, /* 0xc0 */
1074 static struct mxl5005s_config rtl28xxu_mxl5005s_config
= {
1075 .i2c_address
= 0x63, /* 0xc6 */
1076 .if_freq
= IF_FREQ_4570000HZ
,
1077 .xtal_freq
= CRYSTAL_FREQ_16000000HZ
,
1078 .agc_mode
= MXL_SINGLE_AGC
,
1079 .tracking_filter
= MXL_TF_C_H
,
1080 .rssi_enable
= MXL_RSSI_ENABLE
,
1081 .cap_select
= MXL_CAP_SEL_ENABLE
,
1082 .div_out
= MXL_DIV_OUT_4
,
1083 .clock_out
= MXL_CLOCK_OUT_DISABLE
,
1084 .output_load
= MXL5005S_IF_OUTPUT_LOAD_200_OHM
,
1085 .top
= MXL5005S_TOP_25P2
,
1086 .mod_mode
= MXL_DIGITAL_MODE
,
1087 .if_mode
= MXL_ZERO_IF
,
1088 .AgcMasterByte
= 0x00,
1091 static int rtl2831u_tuner_attach(struct dvb_usb_adapter
*adap
)
1094 struct dvb_usb_device
*d
= adap_to_d(adap
);
1095 struct rtl28xxu_dev
*dev
= d_to_priv(d
);
1096 struct dvb_frontend
*fe
;
1098 dev_dbg(&d
->intf
->dev
, "\n");
1100 switch (dev
->tuner
) {
1101 case TUNER_RTL2830_QT1010
:
1102 fe
= dvb_attach(qt1010_attach
, adap
->fe
[0],
1103 dev
->demod_i2c_adapter
,
1104 &rtl28xxu_qt1010_config
);
1106 case TUNER_RTL2830_MT2060
:
1107 fe
= dvb_attach(mt2060_attach
, adap
->fe
[0],
1108 dev
->demod_i2c_adapter
,
1109 &rtl28xxu_mt2060_config
, 1220);
1111 case TUNER_RTL2830_MXL5005S
:
1112 fe
= dvb_attach(mxl5005s_attach
, adap
->fe
[0],
1113 dev
->demod_i2c_adapter
,
1114 &rtl28xxu_mxl5005s_config
);
1118 dev_err(&d
->intf
->dev
, "unknown tuner %d\n", dev
->tuner
);
1128 dev_dbg(&d
->intf
->dev
, "failed=%d\n", ret
);
1132 static const struct fc0012_config rtl2832u_fc0012_config
= {
1133 .i2c_address
= 0x63, /* 0xc6 >> 1 */
1134 .xtal_freq
= FC_XTAL_28_8_MHZ
,
1137 static const struct r820t_config rtl2832u_r820t_config
= {
1140 .max_i2c_msg_len
= 2,
1141 .rafael_chip
= CHIP_R820T
,
1144 static const struct r820t_config rtl2832u_r828d_config
= {
1147 .max_i2c_msg_len
= 2,
1148 .rafael_chip
= CHIP_R828D
,
1151 static int rtl2832u_tuner_attach(struct dvb_usb_adapter
*adap
)
1154 struct dvb_usb_device
*d
= adap_to_d(adap
);
1155 struct rtl28xxu_dev
*dev
= d_to_priv(d
);
1156 struct dvb_frontend
*fe
= NULL
;
1157 struct i2c_board_info info
;
1158 struct i2c_client
*client
;
1159 struct v4l2_subdev
*subdev
= NULL
;
1160 struct platform_device
*pdev
;
1161 struct rtl2832_sdr_platform_data pdata
;
1163 dev_dbg(&d
->intf
->dev
, "\n");
1165 memset(&info
, 0, sizeof(struct i2c_board_info
));
1166 memset(&pdata
, 0, sizeof(pdata
));
1168 switch (dev
->tuner
) {
1169 case TUNER_RTL2832_FC0012
:
1170 fe
= dvb_attach(fc0012_attach
, adap
->fe
[0],
1171 dev
->demod_i2c_adapter
, &rtl2832u_fc0012_config
);
1173 /* since fc0012 includs reading the signal strength delegate
1174 * that to the tuner driver */
1175 adap
->fe
[0]->ops
.read_signal_strength
=
1176 adap
->fe
[0]->ops
.tuner_ops
.get_rf_strength
;
1178 case TUNER_RTL2832_FC0013
:
1179 fe
= dvb_attach(fc0013_attach
, adap
->fe
[0],
1180 dev
->demod_i2c_adapter
, 0xc6>>1, 0, FC_XTAL_28_8_MHZ
);
1182 /* fc0013 also supports signal strength reading */
1183 adap
->fe
[0]->ops
.read_signal_strength
=
1184 adap
->fe
[0]->ops
.tuner_ops
.get_rf_strength
;
1186 case TUNER_RTL2832_E4000
: {
1187 struct e4000_config e4000_config
= {
1192 strlcpy(info
.type
, "e4000", I2C_NAME_SIZE
);
1194 info
.platform_data
= &e4000_config
;
1196 request_module(info
.type
);
1197 client
= i2c_new_device(dev
->demod_i2c_adapter
, &info
);
1198 if (client
== NULL
|| client
->dev
.driver
== NULL
)
1201 if (!try_module_get(client
->dev
.driver
->owner
)) {
1202 i2c_unregister_device(client
);
1206 dev
->i2c_client_tuner
= client
;
1207 subdev
= i2c_get_clientdata(client
);
1210 case TUNER_RTL2832_FC2580
: {
1211 struct fc2580_platform_data fc2580_pdata
= {
1212 .dvb_frontend
= adap
->fe
[0],
1214 struct i2c_board_info board_info
= {};
1216 strlcpy(board_info
.type
, "fc2580", I2C_NAME_SIZE
);
1217 board_info
.addr
= 0x56;
1218 board_info
.platform_data
= &fc2580_pdata
;
1219 request_module("fc2580");
1220 client
= i2c_new_device(dev
->demod_i2c_adapter
,
1222 if (client
== NULL
|| client
->dev
.driver
== NULL
)
1224 if (!try_module_get(client
->dev
.driver
->owner
)) {
1225 i2c_unregister_device(client
);
1228 dev
->i2c_client_tuner
= client
;
1229 subdev
= fc2580_pdata
.get_v4l2_subdev(client
);
1232 case TUNER_RTL2832_TUA9001
: {
1233 struct tua9001_platform_data tua9001_pdata
= {
1234 .dvb_frontend
= adap
->fe
[0],
1236 struct i2c_board_info board_info
= {};
1238 /* enable GPIO1 and GPIO4 as output */
1239 ret
= rtl28xxu_wr_reg_mask(d
, SYS_GPIO_DIR
, 0x00, 0x12);
1243 ret
= rtl28xxu_wr_reg_mask(d
, SYS_GPIO_OUT_EN
, 0x12, 0x12);
1247 strlcpy(board_info
.type
, "tua9001", I2C_NAME_SIZE
);
1248 board_info
.addr
= 0x60;
1249 board_info
.platform_data
= &tua9001_pdata
;
1250 request_module("tua9001");
1251 client
= i2c_new_device(dev
->demod_i2c_adapter
, &board_info
);
1252 if (client
== NULL
|| client
->dev
.driver
== NULL
)
1254 if (!try_module_get(client
->dev
.driver
->owner
)) {
1255 i2c_unregister_device(client
);
1258 dev
->i2c_client_tuner
= client
;
1261 case TUNER_RTL2832_R820T
:
1262 fe
= dvb_attach(r820t_attach
, adap
->fe
[0],
1263 dev
->demod_i2c_adapter
,
1264 &rtl2832u_r820t_config
);
1266 /* Use tuner to get the signal strength */
1267 adap
->fe
[0]->ops
.read_signal_strength
=
1268 adap
->fe
[0]->ops
.tuner_ops
.get_rf_strength
;
1270 case TUNER_RTL2832_R828D
:
1271 fe
= dvb_attach(r820t_attach
, adap
->fe
[0],
1272 dev
->demod_i2c_adapter
,
1273 &rtl2832u_r828d_config
);
1274 adap
->fe
[0]->ops
.read_signal_strength
=
1275 adap
->fe
[0]->ops
.tuner_ops
.get_rf_strength
;
1278 fe
= dvb_attach(r820t_attach
, adap
->fe
[1],
1279 dev
->demod_i2c_adapter
,
1280 &rtl2832u_r828d_config
);
1281 adap
->fe
[1]->ops
.read_signal_strength
=
1282 adap
->fe
[1]->ops
.tuner_ops
.get_rf_strength
;
1285 case TUNER_RTL2832_SI2157
: {
1286 struct si2157_config si2157_config
= {
1292 strlcpy(info
.type
, "si2157", I2C_NAME_SIZE
);
1294 info
.platform_data
= &si2157_config
;
1295 request_module(info
.type
);
1296 client
= i2c_new_device(&d
->i2c_adap
, &info
);
1297 if (client
== NULL
|| client
->dev
.driver
== NULL
)
1300 if (!try_module_get(client
->dev
.driver
->owner
)) {
1301 i2c_unregister_device(client
);
1305 dev
->i2c_client_tuner
= client
;
1306 subdev
= i2c_get_clientdata(client
);
1308 /* copy tuner ops for 2nd FE as tuner is shared */
1310 adap
->fe
[1]->tuner_priv
=
1311 adap
->fe
[0]->tuner_priv
;
1312 memcpy(&adap
->fe
[1]->ops
.tuner_ops
,
1313 &adap
->fe
[0]->ops
.tuner_ops
,
1314 sizeof(struct dvb_tuner_ops
));
1319 dev_err(&d
->intf
->dev
, "unknown tuner %d\n", dev
->tuner
);
1321 if (fe
== NULL
&& dev
->i2c_client_tuner
== NULL
) {
1327 switch (dev
->tuner
) {
1328 case TUNER_RTL2832_FC2580
:
1329 case TUNER_RTL2832_FC0012
:
1330 case TUNER_RTL2832_FC0013
:
1331 case TUNER_RTL2832_E4000
:
1332 case TUNER_RTL2832_R820T
:
1333 case TUNER_RTL2832_R828D
:
1334 pdata
.clk
= dev
->rtl2832_platform_data
.clk
;
1335 pdata
.tuner
= dev
->tuner
;
1336 pdata
.regmap
= dev
->rtl2832_platform_data
.regmap
;
1337 pdata
.dvb_frontend
= adap
->fe
[0];
1338 pdata
.dvb_usb_device
= d
;
1339 pdata
.v4l2_subdev
= subdev
;
1341 request_module("%s", "rtl2832_sdr");
1342 pdev
= platform_device_register_data(&d
->intf
->dev
,
1344 PLATFORM_DEVID_AUTO
,
1345 &pdata
, sizeof(pdata
));
1346 if (IS_ERR(pdev
) || pdev
->dev
.driver
== NULL
)
1348 dev
->platform_device_sdr
= pdev
;
1351 dev_dbg(&d
->intf
->dev
, "no SDR for tuner=%d\n", dev
->tuner
);
1356 dev_dbg(&d
->intf
->dev
, "failed=%d\n", ret
);
1360 static int rtl28xxu_tuner_attach(struct dvb_usb_adapter
*adap
)
1362 struct rtl28xxu_dev
*dev
= adap_to_priv(adap
);
1364 if (dev
->chip_id
== CHIP_ID_RTL2831U
)
1365 return rtl2831u_tuner_attach(adap
);
1367 return rtl2832u_tuner_attach(adap
);
1370 static int rtl28xxu_tuner_detach(struct dvb_usb_adapter
*adap
)
1372 struct dvb_usb_device
*d
= adap_to_d(adap
);
1373 struct rtl28xxu_dev
*dev
= d_to_priv(d
);
1374 struct i2c_client
*client
;
1375 struct platform_device
*pdev
;
1377 dev_dbg(&d
->intf
->dev
, "\n");
1379 /* remove platform SDR */
1380 pdev
= dev
->platform_device_sdr
;
1382 platform_device_unregister(pdev
);
1384 /* remove I2C tuner */
1385 client
= dev
->i2c_client_tuner
;
1387 module_put(client
->dev
.driver
->owner
);
1388 i2c_unregister_device(client
);
1394 static int rtl28xxu_init(struct dvb_usb_device
*d
)
1399 dev_dbg(&d
->intf
->dev
, "\n");
1401 /* init USB endpoints */
1402 ret
= rtl28xxu_rd_reg(d
, USB_SYSCTL_0
, &val
);
1406 /* enable DMA and Full Packet Mode*/
1408 ret
= rtl28xxu_wr_reg(d
, USB_SYSCTL_0
, val
);
1412 /* set EPA maximum packet size to 0x0200 */
1413 ret
= rtl28xxu_wr_regs(d
, USB_EPA_MAXPKT
, "\x00\x02\x00\x00", 4);
1417 /* change EPA FIFO length */
1418 ret
= rtl28xxu_wr_regs(d
, USB_EPA_FIFO_CFG
, "\x14\x00\x00\x00", 4);
1424 dev_dbg(&d
->intf
->dev
, "failed=%d\n", ret
);
1428 static int rtl2831u_power_ctrl(struct dvb_usb_device
*d
, int onoff
)
1431 u8 gpio
, sys0
, epa_ctl
[2];
1433 dev_dbg(&d
->intf
->dev
, "onoff=%d\n", onoff
);
1436 ret
= rtl28xxu_rd_reg(d
, SYS_SYS0
, &sys0
);
1440 /* tuner power, read GPIOs */
1441 ret
= rtl28xxu_rd_reg(d
, SYS_GPIO_OUT_VAL
, &gpio
);
1445 dev_dbg(&d
->intf
->dev
, "RD SYS0=%02x GPIO_OUT_VAL=%02x\n", sys0
, gpio
);
1448 gpio
|= 0x01; /* GPIO0 = 1 */
1449 gpio
&= (~0x10); /* GPIO4 = 0 */
1450 gpio
|= 0x04; /* GPIO2 = 1, LED on */
1453 epa_ctl
[0] = 0x00; /* clear stall */
1454 epa_ctl
[1] = 0x00; /* clear reset */
1456 gpio
&= (~0x01); /* GPIO0 = 0 */
1457 gpio
|= 0x10; /* GPIO4 = 1 */
1458 gpio
&= (~0x04); /* GPIO2 = 1, LED off */
1459 sys0
= sys0
& (~0xc0);
1460 epa_ctl
[0] = 0x10; /* set stall */
1461 epa_ctl
[1] = 0x02; /* set reset */
1464 dev_dbg(&d
->intf
->dev
, "WR SYS0=%02x GPIO_OUT_VAL=%02x\n", sys0
, gpio
);
1467 ret
= rtl28xxu_wr_reg(d
, SYS_SYS0
, sys0
);
1471 /* tuner power, write GPIOs */
1472 ret
= rtl28xxu_wr_reg(d
, SYS_GPIO_OUT_VAL
, gpio
);
1476 /* streaming EP: stall & reset */
1477 ret
= rtl28xxu_wr_regs(d
, USB_EPA_CTL
, epa_ctl
, 2);
1482 usb_clear_halt(d
->udev
, usb_rcvbulkpipe(d
->udev
, 0x81));
1486 dev_dbg(&d
->intf
->dev
, "failed=%d\n", ret
);
1490 static int rtl2832u_power_ctrl(struct dvb_usb_device
*d
, int onoff
)
1494 dev_dbg(&d
->intf
->dev
, "onoff=%d\n", onoff
);
1497 /* GPIO3=1, GPIO4=0 */
1498 ret
= rtl28xxu_wr_reg_mask(d
, SYS_GPIO_OUT_VAL
, 0x08, 0x18);
1503 ret
= rtl28xxu_wr_reg_mask(d
, SYS_DEMOD_CTL1
, 0x00, 0x10);
1508 ret
= rtl28xxu_wr_reg_mask(d
, SYS_DEMOD_CTL
, 0x80, 0x80);
1513 ret
= rtl28xxu_wr_reg_mask(d
, SYS_DEMOD_CTL
, 0x20, 0x20);
1517 /* streaming EP: clear stall & reset */
1518 ret
= rtl28xxu_wr_regs(d
, USB_EPA_CTL
, "\x00\x00", 2);
1522 ret
= usb_clear_halt(d
->udev
, usb_rcvbulkpipe(d
->udev
, 0x81));
1527 ret
= rtl28xxu_wr_reg_mask(d
, SYS_GPIO_OUT_VAL
, 0x10, 0x10);
1532 ret
= rtl28xxu_wr_reg_mask(d
, SYS_DEMOD_CTL
, 0x00, 0x80);
1536 /* streaming EP: set stall & reset */
1537 ret
= rtl28xxu_wr_regs(d
, USB_EPA_CTL
, "\x10\x02", 2);
1544 dev_dbg(&d
->intf
->dev
, "failed=%d\n", ret
);
1548 static int rtl28xxu_power_ctrl(struct dvb_usb_device
*d
, int onoff
)
1550 struct rtl28xxu_dev
*dev
= d_to_priv(d
);
1552 if (dev
->chip_id
== CHIP_ID_RTL2831U
)
1553 return rtl2831u_power_ctrl(d
, onoff
);
1555 return rtl2832u_power_ctrl(d
, onoff
);
1558 static int rtl28xxu_frontend_ctrl(struct dvb_frontend
*fe
, int onoff
)
1560 struct dvb_usb_device
*d
= fe_to_d(fe
);
1561 struct rtl28xxu_dev
*dev
= fe_to_priv(fe
);
1562 struct rtl2832_platform_data
*pdata
= &dev
->rtl2832_platform_data
;
1566 dev_dbg(&d
->intf
->dev
, "fe=%d onoff=%d\n", fe
->id
, onoff
);
1568 if (dev
->chip_id
== CHIP_ID_RTL2831U
)
1572 /* control internal demod ADC */
1574 val
= 0x48; /* enable ADC */
1576 val
= 0x00; /* disable ADC */
1578 ret
= rtl28xxu_wr_reg_mask(d
, SYS_DEMOD_CTL
, val
, 0x48);
1581 } else if (fe
->id
== 1) {
1582 /* bypass slave demod TS through master demod */
1583 ret
= pdata
->slave_ts_ctrl(dev
->i2c_client_demod
, onoff
);
1590 dev_dbg(&d
->intf
->dev
, "failed=%d\n", ret
);
1594 #if IS_ENABLED(CONFIG_RC_CORE)
1595 static int rtl2831u_rc_query(struct dvb_usb_device
*d
)
1598 struct rtl28xxu_dev
*dev
= d
->priv
;
1601 static const struct rtl28xxu_reg_val rc_nec_tab
[] = {
1618 /* init remote controller */
1619 if (!dev
->rc_active
) {
1620 for (i
= 0; i
< ARRAY_SIZE(rc_nec_tab
); i
++) {
1621 ret
= rtl28xxu_wr_reg(d
, rc_nec_tab
[i
].reg
,
1626 dev
->rc_active
= true;
1629 ret
= rtl28xxu_rd_regs(d
, SYS_IRRC_RP
, buf
, 5);
1633 if (buf
[4] & 0x01) {
1634 enum rc_proto proto
;
1636 if (buf
[2] == (u8
) ~buf
[3]) {
1637 if (buf
[0] == (u8
) ~buf
[1]) {
1638 /* NEC standard (16 bit) */
1639 rc_code
= RC_SCANCODE_NEC(buf
[0], buf
[2]);
1640 proto
= RC_PROTO_NEC
;
1642 /* NEC extended (24 bit) */
1643 rc_code
= RC_SCANCODE_NECX(buf
[0] << 8 | buf
[1],
1645 proto
= RC_PROTO_NECX
;
1648 /* NEC full (32 bit) */
1649 rc_code
= RC_SCANCODE_NEC32(buf
[0] << 24 | buf
[1] << 16 |
1650 buf
[2] << 8 | buf
[3]);
1651 proto
= RC_PROTO_NEC32
;
1654 rc_keydown(d
->rc_dev
, proto
, rc_code
, 0);
1656 ret
= rtl28xxu_wr_reg(d
, SYS_IRRC_SR
, 1);
1660 /* repeated intentionally to avoid extra keypress */
1661 ret
= rtl28xxu_wr_reg(d
, SYS_IRRC_SR
, 1);
1668 dev_dbg(&d
->intf
->dev
, "failed=%d\n", ret
);
1672 static int rtl2831u_get_rc_config(struct dvb_usb_device
*d
,
1673 struct dvb_usb_rc
*rc
)
1675 rc
->map_name
= RC_MAP_EMPTY
;
1676 rc
->allowed_protos
= RC_PROTO_BIT_NEC
| RC_PROTO_BIT_NECX
|
1678 rc
->query
= rtl2831u_rc_query
;
1684 static int rtl2832u_rc_query(struct dvb_usb_device
*d
)
1687 struct rtl28xxu_dev
*dev
= d
->priv
;
1688 struct ir_raw_event ev
;
1690 static const struct rtl28xxu_reg_val_mask refresh_tab
[] = {
1691 {IR_RX_IF
, 0x03, 0xff},
1692 {IR_RX_BUF_CTRL
, 0x80, 0xff},
1693 {IR_RX_CTRL
, 0x80, 0xff},
1696 /* init remote controller */
1697 if (!dev
->rc_active
) {
1698 static const struct rtl28xxu_reg_val_mask init_tab
[] = {
1699 {SYS_DEMOD_CTL1
, 0x00, 0x04},
1700 {SYS_DEMOD_CTL1
, 0x00, 0x08},
1701 {USB_CTRL
, 0x20, 0x20},
1702 {SYS_GPIO_DIR
, 0x00, 0x08},
1703 {SYS_GPIO_OUT_EN
, 0x08, 0x08},
1704 {SYS_GPIO_OUT_VAL
, 0x08, 0x08},
1705 {IR_MAX_DURATION0
, 0xd0, 0xff},
1706 {IR_MAX_DURATION1
, 0x07, 0xff},
1707 {IR_IDLE_LEN0
, 0xc0, 0xff},
1708 {IR_IDLE_LEN1
, 0x00, 0xff},
1709 {IR_GLITCH_LEN
, 0x03, 0xff},
1710 {IR_RX_CLK
, 0x09, 0xff},
1711 {IR_RX_CFG
, 0x1c, 0xff},
1712 {IR_MAX_H_TOL_LEN
, 0x1e, 0xff},
1713 {IR_MAX_L_TOL_LEN
, 0x1e, 0xff},
1714 {IR_RX_CTRL
, 0x80, 0xff},
1717 for (i
= 0; i
< ARRAY_SIZE(init_tab
); i
++) {
1718 ret
= rtl28xxu_wr_reg_mask(d
, init_tab
[i
].reg
,
1719 init_tab
[i
].val
, init_tab
[i
].mask
);
1724 dev
->rc_active
= true;
1727 ret
= rtl28xxu_rd_reg(d
, IR_RX_IF
, &buf
[0]);
1734 ret
= rtl28xxu_rd_reg(d
, IR_RX_BC
, &buf
[0]);
1740 /* read raw code from hw */
1741 ret
= rtl28xxu_rd_regs(d
, IR_RX_BUF
, buf
, len
);
1745 /* let hw receive new code */
1746 for (i
= 0; i
< ARRAY_SIZE(refresh_tab
); i
++) {
1747 ret
= rtl28xxu_wr_reg_mask(d
, refresh_tab
[i
].reg
,
1748 refresh_tab
[i
].val
, refresh_tab
[i
].mask
);
1753 /* pass data to Kernel IR decoder */
1754 init_ir_raw_event(&ev
);
1756 for (i
= 0; i
< len
; i
++) {
1757 ev
.pulse
= buf
[i
] >> 7;
1758 ev
.duration
= 50800 * (buf
[i
] & 0x7f);
1759 ir_raw_event_store_with_filter(d
->rc_dev
, &ev
);
1762 /* 'flush' ir_raw_event_store_with_filter() */
1763 ir_raw_event_set_idle(d
->rc_dev
, true);
1764 ir_raw_event_handle(d
->rc_dev
);
1768 dev_dbg(&d
->intf
->dev
, "failed=%d\n", ret
);
1772 static int rtl2832u_get_rc_config(struct dvb_usb_device
*d
,
1773 struct dvb_usb_rc
*rc
)
1775 /* disable IR interrupts in order to avoid SDR sample loss */
1776 if (rtl28xxu_disable_rc
)
1777 return rtl28xxu_wr_reg(d
, IR_RX_IE
, 0x00);
1779 /* load empty to enable rc */
1781 rc
->map_name
= RC_MAP_EMPTY
;
1782 rc
->allowed_protos
= RC_PROTO_BIT_ALL_IR_DECODER
;
1783 rc
->driver_type
= RC_DRIVER_IR_RAW
;
1784 rc
->query
= rtl2832u_rc_query
;
1790 static int rtl28xxu_get_rc_config(struct dvb_usb_device
*d
,
1791 struct dvb_usb_rc
*rc
)
1793 struct rtl28xxu_dev
*dev
= d_to_priv(d
);
1795 if (dev
->chip_id
== CHIP_ID_RTL2831U
)
1796 return rtl2831u_get_rc_config(d
, rc
);
1798 return rtl2832u_get_rc_config(d
, rc
);
1801 #define rtl28xxu_get_rc_config NULL
1804 static int rtl28xxu_pid_filter_ctrl(struct dvb_usb_adapter
*adap
, int onoff
)
1806 struct rtl28xxu_dev
*dev
= adap_to_priv(adap
);
1808 if (dev
->chip_id
== CHIP_ID_RTL2831U
) {
1809 struct rtl2830_platform_data
*pdata
= &dev
->rtl2830_platform_data
;
1811 return pdata
->pid_filter_ctrl(adap
->fe
[0], onoff
);
1813 struct rtl2832_platform_data
*pdata
= &dev
->rtl2832_platform_data
;
1815 return pdata
->pid_filter_ctrl(adap
->fe
[0], onoff
);
1819 static int rtl28xxu_pid_filter(struct dvb_usb_adapter
*adap
, int index
,
1822 struct rtl28xxu_dev
*dev
= adap_to_priv(adap
);
1824 if (dev
->chip_id
== CHIP_ID_RTL2831U
) {
1825 struct rtl2830_platform_data
*pdata
= &dev
->rtl2830_platform_data
;
1827 return pdata
->pid_filter(adap
->fe
[0], index
, pid
, onoff
);
1829 struct rtl2832_platform_data
*pdata
= &dev
->rtl2832_platform_data
;
1831 return pdata
->pid_filter(adap
->fe
[0], index
, pid
, onoff
);
1835 static const struct dvb_usb_device_properties rtl28xxu_props
= {
1836 .driver_name
= KBUILD_MODNAME
,
1837 .owner
= THIS_MODULE
,
1838 .adapter_nr
= adapter_nr
,
1839 .size_of_priv
= sizeof(struct rtl28xxu_dev
),
1841 .identify_state
= rtl28xxu_identify_state
,
1842 .power_ctrl
= rtl28xxu_power_ctrl
,
1843 .frontend_ctrl
= rtl28xxu_frontend_ctrl
,
1844 .i2c_algo
= &rtl28xxu_i2c_algo
,
1845 .read_config
= rtl28xxu_read_config
,
1846 .frontend_attach
= rtl28xxu_frontend_attach
,
1847 .frontend_detach
= rtl28xxu_frontend_detach
,
1848 .tuner_attach
= rtl28xxu_tuner_attach
,
1849 .tuner_detach
= rtl28xxu_tuner_detach
,
1850 .init
= rtl28xxu_init
,
1851 .get_rc_config
= rtl28xxu_get_rc_config
,
1856 .caps
= DVB_USB_ADAP_HAS_PID_FILTER
|
1857 DVB_USB_ADAP_PID_FILTER_CAN_BE_TURNED_OFF
,
1859 .pid_filter_count
= 32,
1860 .pid_filter_ctrl
= rtl28xxu_pid_filter_ctrl
,
1861 .pid_filter
= rtl28xxu_pid_filter
,
1863 .stream
= DVB_USB_STREAM_BULK(0x81, 6, 8 * 512),
1868 static const struct usb_device_id rtl28xxu_id_table
[] = {
1869 /* RTL2831U devices: */
1870 { DVB_USB_DEVICE(USB_VID_REALTEK
, USB_PID_REALTEK_RTL2831U
,
1871 &rtl28xxu_props
, "Realtek RTL2831U reference design", NULL
) },
1872 { DVB_USB_DEVICE(USB_VID_WIDEVIEW
, USB_PID_FREECOM_DVBT
,
1873 &rtl28xxu_props
, "Freecom USB2.0 DVB-T", NULL
) },
1874 { DVB_USB_DEVICE(USB_VID_WIDEVIEW
, USB_PID_FREECOM_DVBT_2
,
1875 &rtl28xxu_props
, "Freecom USB2.0 DVB-T", NULL
) },
1877 /* RTL2832U devices: */
1878 { DVB_USB_DEVICE(USB_VID_REALTEK
, 0x2832,
1879 &rtl28xxu_props
, "Realtek RTL2832U reference design", NULL
) },
1880 { DVB_USB_DEVICE(USB_VID_REALTEK
, 0x2838,
1881 &rtl28xxu_props
, "Realtek RTL2832U reference design", NULL
) },
1882 { DVB_USB_DEVICE(USB_VID_TERRATEC
, USB_PID_TERRATEC_CINERGY_T_STICK_BLACK_REV1
,
1883 &rtl28xxu_props
, "TerraTec Cinergy T Stick Black", RC_MAP_TERRATEC_SLIM
) },
1884 { DVB_USB_DEVICE(USB_VID_GTEK
, USB_PID_DELOCK_USB2_DVBT
,
1885 &rtl28xxu_props
, "G-Tek Electronics Group Lifeview LV5TDLX DVB-T", NULL
) },
1886 { DVB_USB_DEVICE(USB_VID_TERRATEC
, USB_PID_NOXON_DAB_STICK
,
1887 &rtl28xxu_props
, "TerraTec NOXON DAB Stick", NULL
) },
1888 { DVB_USB_DEVICE(USB_VID_TERRATEC
, USB_PID_NOXON_DAB_STICK_REV2
,
1889 &rtl28xxu_props
, "TerraTec NOXON DAB Stick (rev 2)", NULL
) },
1890 { DVB_USB_DEVICE(USB_VID_TERRATEC
, USB_PID_NOXON_DAB_STICK_REV3
,
1891 &rtl28xxu_props
, "TerraTec NOXON DAB Stick (rev 3)", NULL
) },
1892 { DVB_USB_DEVICE(USB_VID_GTEK
, USB_PID_TREKSTOR_TERRES_2_0
,
1893 &rtl28xxu_props
, "Trekstor DVB-T Stick Terres 2.0", NULL
) },
1894 { DVB_USB_DEVICE(USB_VID_DEXATEK
, 0x1101,
1895 &rtl28xxu_props
, "Dexatek DK DVB-T Dongle", NULL
) },
1896 { DVB_USB_DEVICE(USB_VID_LEADTEK
, 0x6680,
1897 &rtl28xxu_props
, "DigitalNow Quad DVB-T Receiver", NULL
) },
1898 { DVB_USB_DEVICE(USB_VID_LEADTEK
, USB_PID_WINFAST_DTV_DONGLE_MINID
,
1899 &rtl28xxu_props
, "Leadtek Winfast DTV Dongle Mini D", NULL
) },
1900 { DVB_USB_DEVICE(USB_VID_LEADTEK
, USB_PID_WINFAST_DTV2000DS_PLUS
,
1901 &rtl28xxu_props
, "Leadtek WinFast DTV2000DS Plus", RC_MAP_LEADTEK_Y04G0051
) },
1902 { DVB_USB_DEVICE(USB_VID_TERRATEC
, 0x00d3,
1903 &rtl28xxu_props
, "TerraTec Cinergy T Stick RC (Rev. 3)", NULL
) },
1904 { DVB_USB_DEVICE(USB_VID_DEXATEK
, 0x1102,
1905 &rtl28xxu_props
, "Dexatek DK mini DVB-T Dongle", NULL
) },
1906 { DVB_USB_DEVICE(USB_VID_TERRATEC
, 0x00d7,
1907 &rtl28xxu_props
, "TerraTec Cinergy T Stick+", NULL
) },
1908 { DVB_USB_DEVICE(USB_VID_KWORLD_2
, 0xd3a8,
1909 &rtl28xxu_props
, "ASUS My Cinema-U3100Mini Plus V2", NULL
) },
1910 { DVB_USB_DEVICE(USB_VID_KWORLD_2
, 0xd393,
1911 &rtl28xxu_props
, "GIGABYTE U7300", NULL
) },
1912 { DVB_USB_DEVICE(USB_VID_DEXATEK
, 0x1104,
1913 &rtl28xxu_props
, "MSI DIGIVOX Micro HD", NULL
) },
1914 { DVB_USB_DEVICE(USB_VID_COMPRO
, 0x0620,
1915 &rtl28xxu_props
, "Compro VideoMate U620F", NULL
) },
1916 { DVB_USB_DEVICE(USB_VID_COMPRO
, 0x0650,
1917 &rtl28xxu_props
, "Compro VideoMate U650F", NULL
) },
1918 { DVB_USB_DEVICE(USB_VID_KWORLD_2
, 0xd394,
1919 &rtl28xxu_props
, "MaxMedia HU394-T", NULL
) },
1920 { DVB_USB_DEVICE(USB_VID_LEADTEK
, 0x6a03,
1921 &rtl28xxu_props
, "Leadtek WinFast DTV Dongle mini", NULL
) },
1922 { DVB_USB_DEVICE(USB_VID_GTEK
, USB_PID_CPYTO_REDI_PC50A
,
1923 &rtl28xxu_props
, "Crypto ReDi PC 50 A", NULL
) },
1924 { DVB_USB_DEVICE(USB_VID_KYE
, 0x707f,
1925 &rtl28xxu_props
, "Genius TVGo DVB-T03", NULL
) },
1926 { DVB_USB_DEVICE(USB_VID_KWORLD_2
, 0xd395,
1927 &rtl28xxu_props
, "Peak DVB-T USB", NULL
) },
1928 { DVB_USB_DEVICE(USB_VID_KWORLD_2
, USB_PID_SVEON_STV20_RTL2832U
,
1929 &rtl28xxu_props
, "Sveon STV20", NULL
) },
1930 { DVB_USB_DEVICE(USB_VID_KWORLD_2
, USB_PID_SVEON_STV21
,
1931 &rtl28xxu_props
, "Sveon STV21", NULL
) },
1932 { DVB_USB_DEVICE(USB_VID_KWORLD_2
, USB_PID_SVEON_STV27
,
1933 &rtl28xxu_props
, "Sveon STV27", NULL
) },
1934 { DVB_USB_DEVICE(USB_VID_KWORLD_2
, USB_PID_TURBOX_DTT_2000
,
1935 &rtl28xxu_props
, "TURBO-X Pure TV Tuner DTT-2000", NULL
) },
1937 /* RTL2832P devices: */
1938 { DVB_USB_DEVICE(USB_VID_HANFTEK
, 0x0131,
1939 &rtl28xxu_props
, "Astrometa DVB-T2", NULL
) },
1940 { DVB_USB_DEVICE(0x5654, 0xca42,
1941 &rtl28xxu_props
, "GoTView MasterHD 3", NULL
) },
1944 MODULE_DEVICE_TABLE(usb
, rtl28xxu_id_table
);
1946 static struct usb_driver rtl28xxu_usb_driver
= {
1947 .name
= KBUILD_MODNAME
,
1948 .id_table
= rtl28xxu_id_table
,
1949 .probe
= dvb_usbv2_probe
,
1950 .disconnect
= dvb_usbv2_disconnect
,
1951 .suspend
= dvb_usbv2_suspend
,
1952 .resume
= dvb_usbv2_resume
,
1953 .reset_resume
= dvb_usbv2_reset_resume
,
1958 module_usb_driver(rtl28xxu_usb_driver
);
1960 MODULE_DESCRIPTION("Realtek RTL28xxU DVB USB driver");
1961 MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
1962 MODULE_AUTHOR("Thomas Mair <thomas.mair86@googlemail.com>");
1963 MODULE_LICENSE("GPL");