1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Realtek RTL28xxU DVB USB driver
5 * Copyright (C) 2009 Antti Palosaari <crope@iki.fi>
6 * Copyright (C) 2011 Antti Palosaari <crope@iki.fi>
7 * Copyright (C) 2012 Thomas Mair <thomas.mair86@googlemail.com>
12 static int rtl28xxu_disable_rc;
13 module_param_named(disable_rc, rtl28xxu_disable_rc, int, 0644);
14 MODULE_PARM_DESC(disable_rc, "disable RTL2832U remote controller");
15 DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
17 static int rtl28xxu_ctrl_msg(struct dvb_usb_device *d, struct rtl28xxu_req *req)
19 struct rtl28xxu_dev *dev = d->priv;
24 mutex_lock(&d->usb_mutex);
26 if (req->size > sizeof(dev->buf)) {
27 dev_err(&d->intf->dev, "too large message %u\n", req->size);
29 goto err_mutex_unlock;
32 if (req->index & CMD_WR_FLAG) {
34 memcpy(dev->buf, req->data, req->size);
35 requesttype = (USB_TYPE_VENDOR | USB_DIR_OUT);
36 pipe = usb_sndctrlpipe(d->udev, 0);
39 requesttype = (USB_TYPE_VENDOR | USB_DIR_IN);
42 * Zero-length transfers must use usb_sndctrlpipe() and
43 * rtl28xxu_identify_state() uses a zero-length i2c read
44 * command to determine the chip type.
47 pipe = usb_rcvctrlpipe(d->udev, 0);
49 pipe = usb_sndctrlpipe(d->udev, 0);
52 ret = usb_control_msg(d->udev, pipe, 0, requesttype, req->value,
53 req->index, dev->buf, req->size, 1000);
54 dvb_usb_dbg_usb_control_msg(d->udev, 0, requesttype, req->value,
55 req->index, dev->buf, req->size);
57 goto err_mutex_unlock;
59 /* read request, copy returned data to return buf */
60 if (requesttype == (USB_TYPE_VENDOR | USB_DIR_IN))
61 memcpy(req->data, dev->buf, req->size);
63 mutex_unlock(&d->usb_mutex);
67 mutex_unlock(&d->usb_mutex);
68 dev_dbg(&d->intf->dev, "failed=%d\n", ret);
72 static int rtl28xxu_wr_regs(struct dvb_usb_device *d, u16 reg, u8 *val, int len)
74 struct rtl28xxu_req req;
77 req.index = CMD_USB_WR;
78 else if (reg < 0x4000)
79 req.index = CMD_SYS_WR;
81 req.index = CMD_IR_WR;
87 return rtl28xxu_ctrl_msg(d, &req);
90 static int rtl28xxu_rd_regs(struct dvb_usb_device *d, u16 reg, u8 *val, int len)
92 struct rtl28xxu_req req;
95 req.index = CMD_USB_RD;
96 else if (reg < 0x4000)
97 req.index = CMD_SYS_RD;
99 req.index = CMD_IR_RD;
105 return rtl28xxu_ctrl_msg(d, &req);
108 static int rtl28xxu_wr_reg(struct dvb_usb_device *d, u16 reg, u8 val)
110 return rtl28xxu_wr_regs(d, reg, &val, 1);
113 static int rtl28xxu_rd_reg(struct dvb_usb_device *d, u16 reg, u8 *val)
115 return rtl28xxu_rd_regs(d, reg, val, 1);
118 static int rtl28xxu_wr_reg_mask(struct dvb_usb_device *d, u16 reg, u8 val,
124 /* no need for read if whole reg is written */
126 ret = rtl28xxu_rd_reg(d, reg, &tmp);
135 return rtl28xxu_wr_reg(d, reg, val);
139 static int rtl28xxu_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg msg[],
143 struct dvb_usb_device *d = i2c_get_adapdata(adap);
144 struct rtl28xxu_dev *dev = d->priv;
145 struct rtl28xxu_req req;
148 * It is not known which are real I2C bus xfer limits, but testing
149 * with RTL2831U + MT2060 gives max RD 24 and max WR 22 bytes.
150 * TODO: find out RTL2832U lens
154 * I2C adapter logic looks rather complicated due to fact it handles
155 * three different access methods. Those methods are;
156 * 1) integrated demod access
160 * Used method is selected in order 1, 2, 3. Method 3 can handle all
161 * requests but there is two reasons why not use it always;
162 * 1) It is most expensive, usually two USB messages are needed
163 * 2) At least RTL2831U does not support it
165 * Method 3 is needed in case of I2C write+read (typical register read)
166 * where write is more than one byte.
169 if (mutex_lock_interruptible(&d->i2c_mutex) < 0)
172 if (num == 2 && !(msg[0].flags & I2C_M_RD) &&
173 (msg[1].flags & I2C_M_RD)) {
174 if (msg[0].len > 24 || msg[1].len > 24) {
175 /* TODO: check msg[0].len max */
177 goto err_mutex_unlock;
178 } else if (msg[0].addr == 0x10) {
179 if (msg[0].len < 1 || msg[1].len < 1) {
181 goto err_mutex_unlock;
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 if (msg[0].len < 1) {
198 goto err_mutex_unlock;
200 /* method 2 - old I2C */
201 req.value = (msg[0].buf[0] << 8) | (msg[0].addr << 1);
202 req.index = CMD_I2C_RD;
203 req.size = msg[1].len;
204 req.data = &msg[1].buf[0];
205 ret = rtl28xxu_ctrl_msg(d, &req);
207 /* method 3 - new I2C */
208 req.value = (msg[0].addr << 1);
209 req.index = CMD_I2C_DA_WR;
210 req.size = msg[0].len;
211 req.data = msg[0].buf;
212 ret = rtl28xxu_ctrl_msg(d, &req);
214 goto err_mutex_unlock;
216 req.value = (msg[0].addr << 1);
217 req.index = CMD_I2C_DA_RD;
218 req.size = msg[1].len;
219 req.data = msg[1].buf;
220 ret = rtl28xxu_ctrl_msg(d, &req);
222 } else if (num == 1 && !(msg[0].flags & I2C_M_RD)) {
223 if (msg[0].len > 22) {
224 /* TODO: check msg[0].len max */
226 goto err_mutex_unlock;
227 } else if (msg[0].addr == 0x10) {
228 if (msg[0].len < 1) {
230 goto err_mutex_unlock;
232 /* method 1 - integrated demod */
233 if (msg[0].buf[0] == 0x00) {
234 if (msg[0].len < 2) {
236 goto err_mutex_unlock;
238 /* save demod page for later demod access */
239 dev->page = msg[0].buf[1];
242 req.value = (msg[0].buf[0] << 8) |
244 req.index = CMD_DEMOD_WR | dev->page;
245 req.size = msg[0].len-1;
246 req.data = &msg[0].buf[1];
247 ret = rtl28xxu_ctrl_msg(d, &req);
249 } else if ((msg[0].len < 23) && (!dev->new_i2c_write)) {
250 if (msg[0].len < 1) {
252 goto err_mutex_unlock;
254 /* method 2 - old I2C */
255 req.value = (msg[0].buf[0] << 8) | (msg[0].addr << 1);
256 req.index = CMD_I2C_WR;
257 req.size = msg[0].len-1;
258 req.data = &msg[0].buf[1];
259 ret = rtl28xxu_ctrl_msg(d, &req);
261 /* method 3 - new I2C */
262 req.value = (msg[0].addr << 1);
263 req.index = CMD_I2C_DA_WR;
264 req.size = msg[0].len;
265 req.data = msg[0].buf;
266 ret = rtl28xxu_ctrl_msg(d, &req);
268 } else if (num == 1 && (msg[0].flags & I2C_M_RD)) {
269 req.value = (msg[0].addr << 1);
270 req.index = CMD_I2C_DA_RD;
271 req.size = msg[0].len;
272 req.data = msg[0].buf;
273 ret = rtl28xxu_ctrl_msg(d, &req);
278 /* Retry failed I2C messages */
283 mutex_unlock(&d->i2c_mutex);
285 return ret ? ret : num;
288 static u32 rtl28xxu_i2c_func(struct i2c_adapter *adapter)
293 static struct i2c_algorithm rtl28xxu_i2c_algo = {
294 .master_xfer = rtl28xxu_i2c_xfer,
295 .functionality = rtl28xxu_i2c_func,
298 static int rtl2831u_read_config(struct dvb_usb_device *d)
300 struct rtl28xxu_dev *dev = d_to_priv(d);
303 /* open RTL2831U/RTL2830 I2C gate */
304 struct rtl28xxu_req req_gate_open = {0x0120, 0x0011, 0x0001, "\x08"};
306 struct rtl28xxu_req req_mt2060 = {0x00c0, CMD_I2C_RD, 1, buf};
307 struct rtl28xxu_req req_qt1010 = {0x0fc4, CMD_I2C_RD, 1, buf};
309 dev_dbg(&d->intf->dev, "\n");
313 * =========================================================
314 * GPIO0 | tuner#0 | 0 off | 1 on | MXL5005S (?)
315 * GPIO2 | LED | 0 off | 1 on |
316 * GPIO4 | tuner#1 | 0 on | 1 off | MT2060
320 ret = rtl28xxu_wr_reg(d, SYS_GPIO_DIR, 0x0a);
324 /* enable as output GPIO0, GPIO2, GPIO4 */
325 ret = rtl28xxu_wr_reg(d, SYS_GPIO_OUT_EN, 0x15);
330 * Probe used tuner. We need to know used tuner before demod attach
331 * since there is some demod params needed to set according to tuner.
334 /* demod needs some time to wake up */
337 dev->tuner_name = "NONE";
339 /* open demod I2C gate */
340 ret = rtl28xxu_ctrl_msg(d, &req_gate_open);
344 /* check QT1010 ID(?) register; reg=0f val=2c */
345 ret = rtl28xxu_ctrl_msg(d, &req_qt1010);
346 if (ret == 0 && buf[0] == 0x2c) {
347 dev->tuner = TUNER_RTL2830_QT1010;
348 dev->tuner_name = "QT1010";
352 /* open demod I2C gate */
353 ret = rtl28xxu_ctrl_msg(d, &req_gate_open);
357 /* check MT2060 ID register; reg=00 val=63 */
358 ret = rtl28xxu_ctrl_msg(d, &req_mt2060);
359 if (ret == 0 && buf[0] == 0x63) {
360 dev->tuner = TUNER_RTL2830_MT2060;
361 dev->tuner_name = "MT2060";
365 /* assume MXL5005S */
366 dev->tuner = TUNER_RTL2830_MXL5005S;
367 dev->tuner_name = "MXL5005S";
371 dev_dbg(&d->intf->dev, "tuner=%s\n", dev->tuner_name);
375 dev_dbg(&d->intf->dev, "failed=%d\n", ret);
379 static int rtl2832u_read_config(struct dvb_usb_device *d)
381 struct rtl28xxu_dev *dev = d_to_priv(d);
384 /* open RTL2832U/RTL2832 I2C gate */
385 struct rtl28xxu_req req_gate_open = {0x0120, 0x0011, 0x0001, "\x18"};
386 /* close RTL2832U/RTL2832 I2C gate */
387 struct rtl28xxu_req req_gate_close = {0x0120, 0x0011, 0x0001, "\x10"};
389 struct rtl28xxu_req req_fc0012 = {0x00c6, CMD_I2C_RD, 1, buf};
390 struct rtl28xxu_req req_fc0013 = {0x00c6, CMD_I2C_RD, 1, buf};
391 struct rtl28xxu_req req_mt2266 = {0x00c0, CMD_I2C_RD, 1, buf};
392 struct rtl28xxu_req req_fc2580 = {0x01ac, CMD_I2C_RD, 1, buf};
393 struct rtl28xxu_req req_mt2063 = {0x00c0, CMD_I2C_RD, 1, buf};
394 struct rtl28xxu_req req_max3543 = {0x00c0, CMD_I2C_RD, 1, buf};
395 struct rtl28xxu_req req_tua9001 = {0x7ec0, CMD_I2C_RD, 2, buf};
396 struct rtl28xxu_req req_mxl5007t = {0xd9c0, CMD_I2C_RD, 1, buf};
397 struct rtl28xxu_req req_e4000 = {0x02c8, CMD_I2C_RD, 1, buf};
398 struct rtl28xxu_req req_tda18272 = {0x00c0, CMD_I2C_RD, 2, buf};
399 struct rtl28xxu_req req_r820t = {0x0034, CMD_I2C_RD, 1, buf};
400 struct rtl28xxu_req req_r828d = {0x0074, CMD_I2C_RD, 1, buf};
401 struct rtl28xxu_req req_mn88472 = {0xff38, CMD_I2C_RD, 1, buf};
402 struct rtl28xxu_req req_mn88473 = {0xff38, CMD_I2C_RD, 1, buf};
403 struct rtl28xxu_req req_cxd2837er = {0xfdd8, CMD_I2C_RD, 1, buf};
404 struct rtl28xxu_req req_si2157 = {0x00c0, CMD_I2C_RD, 1, buf};
405 struct rtl28xxu_req req_si2168 = {0x00c8, CMD_I2C_RD, 1, buf};
407 dev_dbg(&d->intf->dev, "\n");
409 /* enable GPIO3 and GPIO6 as output */
410 ret = rtl28xxu_wr_reg_mask(d, SYS_GPIO_DIR, 0x00, 0x40);
414 ret = rtl28xxu_wr_reg_mask(d, SYS_GPIO_OUT_EN, 0x48, 0x48);
419 * Probe used tuner. We need to know used tuner before demod attach
420 * since there is some demod params needed to set according to tuner.
423 /* open demod I2C gate */
424 ret = rtl28xxu_ctrl_msg(d, &req_gate_open);
428 dev->tuner_name = "NONE";
430 /* check FC0012 ID register; reg=00 val=a1 */
431 ret = rtl28xxu_ctrl_msg(d, &req_fc0012);
432 if (ret == 0 && buf[0] == 0xa1) {
433 dev->tuner = TUNER_RTL2832_FC0012;
434 dev->tuner_name = "FC0012";
438 /* check FC0013 ID register; reg=00 val=a3 */
439 ret = rtl28xxu_ctrl_msg(d, &req_fc0013);
440 if (ret == 0 && buf[0] == 0xa3) {
441 dev->tuner = TUNER_RTL2832_FC0013;
442 dev->tuner_name = "FC0013";
446 /* check MT2266 ID register; reg=00 val=85 */
447 ret = rtl28xxu_ctrl_msg(d, &req_mt2266);
448 if (ret == 0 && buf[0] == 0x85) {
449 dev->tuner = TUNER_RTL2832_MT2266;
450 dev->tuner_name = "MT2266";
454 /* check FC2580 ID register; reg=01 val=56 */
455 ret = rtl28xxu_ctrl_msg(d, &req_fc2580);
456 if (ret == 0 && buf[0] == 0x56) {
457 dev->tuner = TUNER_RTL2832_FC2580;
458 dev->tuner_name = "FC2580";
462 /* check MT2063 ID register; reg=00 val=9e || 9c */
463 ret = rtl28xxu_ctrl_msg(d, &req_mt2063);
464 if (ret == 0 && (buf[0] == 0x9e || buf[0] == 0x9c)) {
465 dev->tuner = TUNER_RTL2832_MT2063;
466 dev->tuner_name = "MT2063";
470 /* check MAX3543 ID register; reg=00 val=38 */
471 ret = rtl28xxu_ctrl_msg(d, &req_max3543);
472 if (ret == 0 && buf[0] == 0x38) {
473 dev->tuner = TUNER_RTL2832_MAX3543;
474 dev->tuner_name = "MAX3543";
478 /* check TUA9001 ID register; reg=7e val=2328 */
479 ret = rtl28xxu_ctrl_msg(d, &req_tua9001);
480 if (ret == 0 && buf[0] == 0x23 && buf[1] == 0x28) {
481 dev->tuner = TUNER_RTL2832_TUA9001;
482 dev->tuner_name = "TUA9001";
486 /* check MXL5007R ID register; reg=d9 val=14 */
487 ret = rtl28xxu_ctrl_msg(d, &req_mxl5007t);
488 if (ret == 0 && buf[0] == 0x14) {
489 dev->tuner = TUNER_RTL2832_MXL5007T;
490 dev->tuner_name = "MXL5007T";
494 /* check E4000 ID register; reg=02 val=40 */
495 ret = rtl28xxu_ctrl_msg(d, &req_e4000);
496 if (ret == 0 && buf[0] == 0x40) {
497 dev->tuner = TUNER_RTL2832_E4000;
498 dev->tuner_name = "E4000";
502 /* check TDA18272 ID register; reg=00 val=c760 */
503 ret = rtl28xxu_ctrl_msg(d, &req_tda18272);
504 if (ret == 0 && (buf[0] == 0xc7 || buf[1] == 0x60)) {
505 dev->tuner = TUNER_RTL2832_TDA18272;
506 dev->tuner_name = "TDA18272";
510 /* check R820T ID register; reg=00 val=69 */
511 ret = rtl28xxu_ctrl_msg(d, &req_r820t);
512 if (ret == 0 && buf[0] == 0x69) {
513 dev->tuner = TUNER_RTL2832_R820T;
514 dev->tuner_name = "R820T";
518 /* check R828D ID register; reg=00 val=69 */
519 ret = rtl28xxu_ctrl_msg(d, &req_r828d);
520 if (ret == 0 && buf[0] == 0x69) {
521 dev->tuner = TUNER_RTL2832_R828D;
522 dev->tuner_name = "R828D";
526 /* GPIO0 and GPIO5 to reset Si2157/Si2168 tuner and demod */
527 ret = rtl28xxu_wr_reg_mask(d, SYS_GPIO_OUT_VAL, 0x00, 0x21);
531 ret = rtl28xxu_wr_reg_mask(d, SYS_GPIO_OUT_EN, 0x00, 0x21);
537 ret = rtl28xxu_wr_reg_mask(d, SYS_GPIO_OUT_VAL, 0x21, 0x21);
541 ret = rtl28xxu_wr_reg_mask(d, SYS_GPIO_OUT_EN, 0x21, 0x21);
547 /* check Si2157 ID register; reg=c0 val=80 */
548 ret = rtl28xxu_ctrl_msg(d, &req_si2157);
549 if (ret == 0 && ((buf[0] & 0x80) == 0x80)) {
550 dev->tuner = TUNER_RTL2832_SI2157;
551 dev->tuner_name = "SI2157";
556 dev_dbg(&d->intf->dev, "tuner=%s\n", dev->tuner_name);
558 /* probe slave demod */
559 if (dev->tuner == TUNER_RTL2832_R828D) {
560 /* power off slave demod on GPIO0 to reset CXD2837ER */
561 ret = rtl28xxu_wr_reg_mask(d, SYS_GPIO_OUT_VAL, 0x00, 0x01);
565 ret = rtl28xxu_wr_reg_mask(d, SYS_GPIO_OUT_EN, 0x00, 0x01);
571 /* power on slave demod on GPIO0 */
572 ret = rtl28xxu_wr_reg_mask(d, SYS_GPIO_OUT_VAL, 0x01, 0x01);
576 ret = rtl28xxu_wr_reg_mask(d, SYS_GPIO_DIR, 0x00, 0x01);
580 ret = rtl28xxu_wr_reg_mask(d, SYS_GPIO_OUT_EN, 0x01, 0x01);
584 /* slave demod needs some time to wake up */
587 /* check slave answers */
588 ret = rtl28xxu_ctrl_msg(d, &req_mn88472);
589 if (ret == 0 && buf[0] == 0x02) {
590 dev_dbg(&d->intf->dev, "MN88472 found\n");
591 dev->slave_demod = SLAVE_DEMOD_MN88472;
595 ret = rtl28xxu_ctrl_msg(d, &req_mn88473);
596 if (ret == 0 && buf[0] == 0x03) {
597 dev_dbg(&d->intf->dev, "MN88473 found\n");
598 dev->slave_demod = SLAVE_DEMOD_MN88473;
602 ret = rtl28xxu_ctrl_msg(d, &req_cxd2837er);
603 if (ret == 0 && buf[0] == 0xb1) {
604 dev_dbg(&d->intf->dev, "CXD2837ER found\n");
605 dev->slave_demod = SLAVE_DEMOD_CXD2837ER;
609 if (dev->tuner == TUNER_RTL2832_SI2157) {
610 /* check Si2168 ID register; reg=c8 val=80 */
611 ret = rtl28xxu_ctrl_msg(d, &req_si2168);
612 if (ret == 0 && ((buf[0] & 0x80) == 0x80)) {
613 dev_dbg(&d->intf->dev, "Si2168 found\n");
614 dev->slave_demod = SLAVE_DEMOD_SI2168;
620 /* close demod I2C gate */
621 ret = rtl28xxu_ctrl_msg(d, &req_gate_close);
627 dev_dbg(&d->intf->dev, "failed=%d\n", ret);
631 static int rtl28xxu_read_config(struct dvb_usb_device *d)
633 struct rtl28xxu_dev *dev = d_to_priv(d);
635 if (dev->chip_id == CHIP_ID_RTL2831U)
636 return rtl2831u_read_config(d);
638 return rtl2832u_read_config(d);
641 static int rtl28xxu_identify_state(struct dvb_usb_device *d, const char **name)
643 struct rtl28xxu_dev *dev = d_to_priv(d);
645 struct rtl28xxu_req req_demod_i2c = {0x0020, CMD_I2C_DA_RD, 0, NULL};
647 dev_dbg(&d->intf->dev, "\n");
650 * Detect chip type using I2C command that is not supported
653 ret = rtl28xxu_ctrl_msg(d, &req_demod_i2c);
655 dev->chip_id = CHIP_ID_RTL2831U;
656 } else if (ret == 0) {
657 dev->chip_id = CHIP_ID_RTL2832U;
659 dev_err(&d->intf->dev, "chip type detection failed %d\n", ret);
662 dev_dbg(&d->intf->dev, "chip_id=%u\n", dev->chip_id);
664 /* Retry failed I2C messages */
665 d->i2c_adap.retries = 3;
666 d->i2c_adap.timeout = msecs_to_jiffies(10);
670 dev_dbg(&d->intf->dev, "failed=%d\n", ret);
674 static const struct rtl2830_platform_data rtl2830_mt2060_platform_data = {
679 .agc_targ_val = 0x2d,
683 static const struct rtl2830_platform_data rtl2830_qt1010_platform_data = {
688 .agc_targ_val = 0x2d,
691 static const struct rtl2830_platform_data rtl2830_mxl5005s_platform_data = {
696 .agc_targ_val = 0x3e,
699 static int rtl2831u_frontend_attach(struct dvb_usb_adapter *adap)
701 struct dvb_usb_device *d = adap_to_d(adap);
702 struct rtl28xxu_dev *dev = d_to_priv(d);
703 struct rtl2830_platform_data *pdata = &dev->rtl2830_platform_data;
704 struct i2c_board_info board_info;
705 struct i2c_client *client;
708 dev_dbg(&d->intf->dev, "\n");
710 switch (dev->tuner) {
711 case TUNER_RTL2830_QT1010:
712 *pdata = rtl2830_qt1010_platform_data;
714 case TUNER_RTL2830_MT2060:
715 *pdata = rtl2830_mt2060_platform_data;
717 case TUNER_RTL2830_MXL5005S:
718 *pdata = rtl2830_mxl5005s_platform_data;
721 dev_err(&d->intf->dev, "unknown tuner %s\n", dev->tuner_name);
726 /* attach demodulator */
727 memset(&board_info, 0, sizeof(board_info));
728 strscpy(board_info.type, "rtl2830", I2C_NAME_SIZE);
729 board_info.addr = 0x10;
730 board_info.platform_data = pdata;
731 request_module("%s", board_info.type);
732 client = i2c_new_client_device(&d->i2c_adap, &board_info);
733 if (!i2c_client_has_driver(client)) {
738 if (!try_module_get(client->dev.driver->owner)) {
739 i2c_unregister_device(client);
744 adap->fe[0] = pdata->get_dvb_frontend(client);
745 dev->demod_i2c_adapter = pdata->get_i2c_adapter(client);
747 dev->i2c_client_demod = client;
751 dev_dbg(&d->intf->dev, "failed=%d\n", ret);
755 static const struct rtl2832_platform_data rtl2832_fc2580_platform_data = {
757 .tuner = TUNER_RTL2832_FC2580,
760 static const struct rtl2832_platform_data rtl2832_fc0012_platform_data = {
762 .tuner = TUNER_RTL2832_FC0012
765 static const struct rtl2832_platform_data rtl2832_fc0013_platform_data = {
767 .tuner = TUNER_RTL2832_FC0013
770 static const struct rtl2832_platform_data rtl2832_tua9001_platform_data = {
772 .tuner = TUNER_RTL2832_TUA9001,
775 static const struct rtl2832_platform_data rtl2832_e4000_platform_data = {
777 .tuner = TUNER_RTL2832_E4000,
780 static const struct rtl2832_platform_data rtl2832_r820t_platform_data = {
782 .tuner = TUNER_RTL2832_R820T,
785 static const struct rtl2832_platform_data rtl2832_si2157_platform_data = {
787 .tuner = TUNER_RTL2832_SI2157,
790 static int rtl2832u_fc0012_tuner_callback(struct dvb_usb_device *d,
796 dev_dbg(&d->intf->dev, "cmd=%d arg=%d\n", cmd, arg);
799 case FC_FE_CALLBACK_VHF_ENABLE:
800 /* set output values */
801 ret = rtl28xxu_rd_reg(d, SYS_GPIO_OUT_VAL, &val);
806 val &= 0xbf; /* set GPIO6 low */
808 val |= 0x40; /* set GPIO6 high */
811 ret = rtl28xxu_wr_reg(d, SYS_GPIO_OUT_VAL, val);
821 dev_dbg(&d->intf->dev, "failed=%d\n", ret);
825 static int rtl2832u_tua9001_tuner_callback(struct dvb_usb_device *d,
831 dev_dbg(&d->intf->dev, "cmd=%d arg=%d\n", cmd, arg);
834 * CEN always enabled by hardware wiring
840 case TUA9001_CMD_RESETN:
846 ret = rtl28xxu_wr_reg_mask(d, SYS_GPIO_OUT_VAL, val, 0x10);
850 case TUA9001_CMD_RXEN:
856 ret = rtl28xxu_wr_reg_mask(d, SYS_GPIO_OUT_VAL, val, 0x02);
864 dev_dbg(&d->intf->dev, "failed=%d\n", ret);
868 static int rtl2832u_frontend_callback(void *adapter_priv, int component,
871 struct i2c_adapter *adapter = adapter_priv;
872 struct device *parent = adapter->dev.parent;
873 struct i2c_adapter *parent_adapter;
874 struct dvb_usb_device *d;
875 struct rtl28xxu_dev *dev;
878 * All tuners are connected to demod muxed I2C adapter. We have to
879 * resolve its parent adapter in order to get handle for this driver
880 * private data. That is a bit hackish solution, GPIO or direct driver
881 * callback would be better...
883 if (parent != NULL && parent->type == &i2c_adapter_type)
884 parent_adapter = to_i2c_adapter(parent);
888 d = i2c_get_adapdata(parent_adapter);
891 dev_dbg(&d->intf->dev, "component=%d cmd=%d arg=%d\n",
892 component, cmd, arg);
895 case DVB_FRONTEND_COMPONENT_TUNER:
896 switch (dev->tuner) {
897 case TUNER_RTL2832_FC0012:
898 return rtl2832u_fc0012_tuner_callback(d, cmd, arg);
899 case TUNER_RTL2832_TUA9001:
900 return rtl2832u_tua9001_tuner_callback(d, cmd, arg);
907 static int rtl2832u_frontend_attach(struct dvb_usb_adapter *adap)
909 struct dvb_usb_device *d = adap_to_d(adap);
910 struct rtl28xxu_dev *dev = d_to_priv(d);
911 struct rtl2832_platform_data *pdata = &dev->rtl2832_platform_data;
912 struct i2c_board_info board_info;
913 struct i2c_client *client;
916 dev_dbg(&d->intf->dev, "\n");
918 switch (dev->tuner) {
919 case TUNER_RTL2832_FC0012:
920 *pdata = rtl2832_fc0012_platform_data;
922 case TUNER_RTL2832_FC0013:
923 *pdata = rtl2832_fc0013_platform_data;
925 case TUNER_RTL2832_FC2580:
926 *pdata = rtl2832_fc2580_platform_data;
928 case TUNER_RTL2832_TUA9001:
929 *pdata = rtl2832_tua9001_platform_data;
931 case TUNER_RTL2832_E4000:
932 *pdata = rtl2832_e4000_platform_data;
934 case TUNER_RTL2832_R820T:
935 case TUNER_RTL2832_R828D:
936 *pdata = rtl2832_r820t_platform_data;
938 case TUNER_RTL2832_SI2157:
939 *pdata = rtl2832_si2157_platform_data;
942 dev_err(&d->intf->dev, "unknown tuner %s\n", dev->tuner_name);
947 /* attach demodulator */
948 memset(&board_info, 0, sizeof(board_info));
949 strscpy(board_info.type, "rtl2832", I2C_NAME_SIZE);
950 board_info.addr = 0x10;
951 board_info.platform_data = pdata;
952 request_module("%s", board_info.type);
953 client = i2c_new_client_device(&d->i2c_adap, &board_info);
954 if (!i2c_client_has_driver(client)) {
959 if (!try_module_get(client->dev.driver->owner)) {
960 i2c_unregister_device(client);
965 adap->fe[0] = pdata->get_dvb_frontend(client);
966 dev->demod_i2c_adapter = pdata->get_i2c_adapter(client);
968 dev->i2c_client_demod = client;
970 /* set fe callback */
971 adap->fe[0]->callback = rtl2832u_frontend_callback;
973 if (dev->slave_demod) {
974 struct i2c_board_info info = {};
976 /* attach slave demodulator */
977 if (dev->slave_demod == SLAVE_DEMOD_MN88472) {
978 struct mn88472_config mn88472_config = {};
980 mn88472_config.fe = &adap->fe[1];
981 mn88472_config.i2c_wr_max = 22;
982 strscpy(info.type, "mn88472", I2C_NAME_SIZE);
983 mn88472_config.xtal = 20500000;
984 mn88472_config.ts_mode = SERIAL_TS_MODE;
985 mn88472_config.ts_clock = VARIABLE_TS_CLOCK;
987 info.platform_data = &mn88472_config;
988 request_module(info.type);
989 client = i2c_new_client_device(&d->i2c_adap, &info);
990 if (!i2c_client_has_driver(client))
991 goto err_slave_demod_failed;
993 if (!try_module_get(client->dev.driver->owner)) {
994 i2c_unregister_device(client);
995 goto err_slave_demod_failed;
998 dev->i2c_client_slave_demod = client;
999 } else if (dev->slave_demod == SLAVE_DEMOD_MN88473) {
1000 struct mn88473_config mn88473_config = {};
1002 mn88473_config.fe = &adap->fe[1];
1003 mn88473_config.i2c_wr_max = 22;
1004 strscpy(info.type, "mn88473", I2C_NAME_SIZE);
1006 info.platform_data = &mn88473_config;
1007 request_module(info.type);
1008 client = i2c_new_client_device(&d->i2c_adap, &info);
1009 if (!i2c_client_has_driver(client))
1010 goto err_slave_demod_failed;
1012 if (!try_module_get(client->dev.driver->owner)) {
1013 i2c_unregister_device(client);
1014 goto err_slave_demod_failed;
1017 dev->i2c_client_slave_demod = client;
1018 } else if (dev->slave_demod == SLAVE_DEMOD_CXD2837ER) {
1019 struct cxd2841er_config cxd2837er_config = {};
1021 cxd2837er_config.i2c_addr = 0xd8;
1022 cxd2837er_config.xtal = SONY_XTAL_20500;
1023 cxd2837er_config.flags = (CXD2841ER_AUTO_IFHZ |
1024 CXD2841ER_NO_AGCNEG | CXD2841ER_TSBITS |
1025 CXD2841ER_EARLY_TUNE | CXD2841ER_TS_SERIAL);
1026 adap->fe[1] = dvb_attach(cxd2841er_attach_t_c,
1030 goto err_slave_demod_failed;
1031 adap->fe[1]->id = 1;
1032 dev->i2c_client_slave_demod = NULL;
1034 struct si2168_config si2168_config = {};
1035 struct i2c_adapter *adapter;
1037 si2168_config.i2c_adapter = &adapter;
1038 si2168_config.fe = &adap->fe[1];
1039 si2168_config.ts_mode = SI2168_TS_SERIAL;
1040 si2168_config.ts_clock_inv = false;
1041 si2168_config.ts_clock_gapped = true;
1042 strscpy(info.type, "si2168", I2C_NAME_SIZE);
1044 info.platform_data = &si2168_config;
1045 request_module(info.type);
1046 client = i2c_new_client_device(&d->i2c_adap, &info);
1047 if (!i2c_client_has_driver(client))
1048 goto err_slave_demod_failed;
1050 if (!try_module_get(client->dev.driver->owner)) {
1051 i2c_unregister_device(client);
1052 goto err_slave_demod_failed;
1055 dev->i2c_client_slave_demod = client;
1057 /* for Si2168 devices use only new I2C write method */
1058 dev->new_i2c_write = true;
1064 dev_dbg(&d->intf->dev, "failed=%d\n", ret);
1067 err_slave_demod_failed:
1069 * We continue on reduced mode, without DVB-T2/C, using master
1070 * demod, when slave demod fails.
1072 dev->slave_demod = SLAVE_DEMOD_NONE;
1076 static int rtl28xxu_frontend_attach(struct dvb_usb_adapter *adap)
1078 struct rtl28xxu_dev *dev = adap_to_priv(adap);
1080 if (dev->chip_id == CHIP_ID_RTL2831U)
1081 return rtl2831u_frontend_attach(adap);
1083 return rtl2832u_frontend_attach(adap);
1086 static int rtl28xxu_frontend_detach(struct dvb_usb_adapter *adap)
1088 struct dvb_usb_device *d = adap_to_d(adap);
1089 struct rtl28xxu_dev *dev = d_to_priv(d);
1090 struct i2c_client *client;
1092 dev_dbg(&d->intf->dev, "\n");
1094 /* remove I2C slave demod */
1095 client = dev->i2c_client_slave_demod;
1097 module_put(client->dev.driver->owner);
1098 i2c_unregister_device(client);
1101 /* remove I2C demod */
1102 client = dev->i2c_client_demod;
1104 module_put(client->dev.driver->owner);
1105 i2c_unregister_device(client);
1111 static struct qt1010_config rtl28xxu_qt1010_config = {
1112 .i2c_address = 0x62, /* 0xc4 */
1115 static struct mt2060_config rtl28xxu_mt2060_config = {
1116 .i2c_address = 0x60, /* 0xc0 */
1120 static struct mxl5005s_config rtl28xxu_mxl5005s_config = {
1121 .i2c_address = 0x63, /* 0xc6 */
1122 .if_freq = IF_FREQ_4570000HZ,
1123 .xtal_freq = CRYSTAL_FREQ_16000000HZ,
1124 .agc_mode = MXL_SINGLE_AGC,
1125 .tracking_filter = MXL_TF_C_H,
1126 .rssi_enable = MXL_RSSI_ENABLE,
1127 .cap_select = MXL_CAP_SEL_ENABLE,
1128 .div_out = MXL_DIV_OUT_4,
1129 .clock_out = MXL_CLOCK_OUT_DISABLE,
1130 .output_load = MXL5005S_IF_OUTPUT_LOAD_200_OHM,
1131 .top = MXL5005S_TOP_25P2,
1132 .mod_mode = MXL_DIGITAL_MODE,
1133 .if_mode = MXL_ZERO_IF,
1134 .AgcMasterByte = 0x00,
1137 static int rtl2831u_tuner_attach(struct dvb_usb_adapter *adap)
1140 struct dvb_usb_device *d = adap_to_d(adap);
1141 struct rtl28xxu_dev *dev = d_to_priv(d);
1142 struct dvb_frontend *fe;
1144 dev_dbg(&d->intf->dev, "\n");
1146 switch (dev->tuner) {
1147 case TUNER_RTL2830_QT1010:
1148 fe = dvb_attach(qt1010_attach, adap->fe[0],
1149 dev->demod_i2c_adapter,
1150 &rtl28xxu_qt1010_config);
1152 case TUNER_RTL2830_MT2060:
1153 fe = dvb_attach(mt2060_attach, adap->fe[0],
1154 dev->demod_i2c_adapter,
1155 &rtl28xxu_mt2060_config, 1220);
1157 case TUNER_RTL2830_MXL5005S:
1158 fe = dvb_attach(mxl5005s_attach, adap->fe[0],
1159 dev->demod_i2c_adapter,
1160 &rtl28xxu_mxl5005s_config);
1164 dev_err(&d->intf->dev, "unknown tuner %d\n", dev->tuner);
1174 dev_dbg(&d->intf->dev, "failed=%d\n", ret);
1178 static const struct fc0012_config rtl2832u_fc0012_config = {
1179 .i2c_address = 0x63, /* 0xc6 >> 1 */
1180 .xtal_freq = FC_XTAL_28_8_MHZ,
1183 static const struct r820t_config rtl2832u_r820t_config = {
1186 .max_i2c_msg_len = 2,
1187 .rafael_chip = CHIP_R820T,
1190 static const struct r820t_config rtl2832u_r828d_config = {
1193 .max_i2c_msg_len = 2,
1194 .rafael_chip = CHIP_R828D,
1197 static int rtl2832u_tuner_attach(struct dvb_usb_adapter *adap)
1200 struct dvb_usb_device *d = adap_to_d(adap);
1201 struct rtl28xxu_dev *dev = d_to_priv(d);
1202 struct dvb_frontend *fe = NULL;
1203 struct i2c_board_info info;
1204 struct i2c_client *client;
1205 struct v4l2_subdev *subdev = NULL;
1206 struct platform_device *pdev;
1207 struct rtl2832_sdr_platform_data pdata;
1209 dev_dbg(&d->intf->dev, "\n");
1211 memset(&info, 0, sizeof(struct i2c_board_info));
1212 memset(&pdata, 0, sizeof(pdata));
1214 switch (dev->tuner) {
1215 case TUNER_RTL2832_FC0012:
1216 fe = dvb_attach(fc0012_attach, adap->fe[0],
1217 dev->demod_i2c_adapter, &rtl2832u_fc0012_config);
1219 /* since fc0012 includs reading the signal strength delegate
1220 * that to the tuner driver */
1221 adap->fe[0]->ops.read_signal_strength =
1222 adap->fe[0]->ops.tuner_ops.get_rf_strength;
1224 case TUNER_RTL2832_FC0013:
1225 fe = dvb_attach(fc0013_attach, adap->fe[0],
1226 dev->demod_i2c_adapter, 0xc6>>1, 0, FC_XTAL_28_8_MHZ);
1228 /* fc0013 also supports signal strength reading */
1229 adap->fe[0]->ops.read_signal_strength =
1230 adap->fe[0]->ops.tuner_ops.get_rf_strength;
1232 case TUNER_RTL2832_E4000: {
1233 struct e4000_config e4000_config = {
1238 strscpy(info.type, "e4000", I2C_NAME_SIZE);
1240 info.platform_data = &e4000_config;
1242 request_module(info.type);
1243 client = i2c_new_client_device(dev->demod_i2c_adapter,
1245 if (!i2c_client_has_driver(client))
1248 if (!try_module_get(client->dev.driver->owner)) {
1249 i2c_unregister_device(client);
1253 dev->i2c_client_tuner = client;
1254 subdev = i2c_get_clientdata(client);
1257 case TUNER_RTL2832_FC2580: {
1258 struct fc2580_platform_data fc2580_pdata = {
1259 .dvb_frontend = adap->fe[0],
1261 struct i2c_board_info board_info = {};
1263 strscpy(board_info.type, "fc2580", I2C_NAME_SIZE);
1264 board_info.addr = 0x56;
1265 board_info.platform_data = &fc2580_pdata;
1266 request_module("fc2580");
1267 client = i2c_new_client_device(dev->demod_i2c_adapter,
1269 if (!i2c_client_has_driver(client))
1271 if (!try_module_get(client->dev.driver->owner)) {
1272 i2c_unregister_device(client);
1275 dev->i2c_client_tuner = client;
1276 subdev = fc2580_pdata.get_v4l2_subdev(client);
1279 case TUNER_RTL2832_TUA9001: {
1280 struct tua9001_platform_data tua9001_pdata = {
1281 .dvb_frontend = adap->fe[0],
1283 struct i2c_board_info board_info = {};
1285 /* enable GPIO1 and GPIO4 as output */
1286 ret = rtl28xxu_wr_reg_mask(d, SYS_GPIO_DIR, 0x00, 0x12);
1290 ret = rtl28xxu_wr_reg_mask(d, SYS_GPIO_OUT_EN, 0x12, 0x12);
1294 strscpy(board_info.type, "tua9001", I2C_NAME_SIZE);
1295 board_info.addr = 0x60;
1296 board_info.platform_data = &tua9001_pdata;
1297 request_module("tua9001");
1298 client = i2c_new_client_device(dev->demod_i2c_adapter,
1300 if (!i2c_client_has_driver(client))
1302 if (!try_module_get(client->dev.driver->owner)) {
1303 i2c_unregister_device(client);
1306 dev->i2c_client_tuner = client;
1309 case TUNER_RTL2832_R820T:
1310 fe = dvb_attach(r820t_attach, adap->fe[0],
1311 dev->demod_i2c_adapter,
1312 &rtl2832u_r820t_config);
1314 /* Use tuner to get the signal strength */
1315 adap->fe[0]->ops.read_signal_strength =
1316 adap->fe[0]->ops.tuner_ops.get_rf_strength;
1318 case TUNER_RTL2832_R828D:
1319 fe = dvb_attach(r820t_attach, adap->fe[0],
1320 dev->demod_i2c_adapter,
1321 &rtl2832u_r828d_config);
1322 adap->fe[0]->ops.read_signal_strength =
1323 adap->fe[0]->ops.tuner_ops.get_rf_strength;
1326 fe = dvb_attach(r820t_attach, adap->fe[1],
1327 dev->demod_i2c_adapter,
1328 &rtl2832u_r828d_config);
1329 adap->fe[1]->ops.read_signal_strength =
1330 adap->fe[1]->ops.tuner_ops.get_rf_strength;
1333 case TUNER_RTL2832_SI2157: {
1334 struct si2157_config si2157_config = {
1340 strscpy(info.type, "si2157", I2C_NAME_SIZE);
1342 info.platform_data = &si2157_config;
1343 request_module(info.type);
1344 client = i2c_new_client_device(&d->i2c_adap, &info);
1345 if (!i2c_client_has_driver(client))
1348 if (!try_module_get(client->dev.driver->owner)) {
1349 i2c_unregister_device(client);
1353 dev->i2c_client_tuner = client;
1354 subdev = i2c_get_clientdata(client);
1356 /* copy tuner ops for 2nd FE as tuner is shared */
1358 adap->fe[1]->tuner_priv =
1359 adap->fe[0]->tuner_priv;
1360 memcpy(&adap->fe[1]->ops.tuner_ops,
1361 &adap->fe[0]->ops.tuner_ops,
1362 sizeof(struct dvb_tuner_ops));
1367 dev_err(&d->intf->dev, "unknown tuner %d\n", dev->tuner);
1369 if (fe == NULL && dev->i2c_client_tuner == NULL) {
1375 switch (dev->tuner) {
1376 case TUNER_RTL2832_FC2580:
1377 case TUNER_RTL2832_FC0012:
1378 case TUNER_RTL2832_FC0013:
1379 case TUNER_RTL2832_E4000:
1380 case TUNER_RTL2832_R820T:
1381 case TUNER_RTL2832_R828D:
1382 pdata.clk = dev->rtl2832_platform_data.clk;
1383 pdata.tuner = dev->tuner;
1384 pdata.regmap = dev->rtl2832_platform_data.regmap;
1385 pdata.dvb_frontend = adap->fe[0];
1386 pdata.dvb_usb_device = d;
1387 pdata.v4l2_subdev = subdev;
1389 request_module("%s", "rtl2832_sdr");
1390 pdev = platform_device_register_data(&d->intf->dev,
1392 PLATFORM_DEVID_AUTO,
1393 &pdata, sizeof(pdata));
1394 if (IS_ERR(pdev) || pdev->dev.driver == NULL)
1396 dev->platform_device_sdr = pdev;
1399 dev_dbg(&d->intf->dev, "no SDR for tuner=%d\n", dev->tuner);
1404 dev_dbg(&d->intf->dev, "failed=%d\n", ret);
1408 static int rtl28xxu_tuner_attach(struct dvb_usb_adapter *adap)
1410 struct rtl28xxu_dev *dev = adap_to_priv(adap);
1412 if (dev->chip_id == CHIP_ID_RTL2831U)
1413 return rtl2831u_tuner_attach(adap);
1415 return rtl2832u_tuner_attach(adap);
1418 static int rtl28xxu_tuner_detach(struct dvb_usb_adapter *adap)
1420 struct dvb_usb_device *d = adap_to_d(adap);
1421 struct rtl28xxu_dev *dev = d_to_priv(d);
1422 struct i2c_client *client;
1423 struct platform_device *pdev;
1425 dev_dbg(&d->intf->dev, "\n");
1427 /* remove platform SDR */
1428 pdev = dev->platform_device_sdr;
1430 platform_device_unregister(pdev);
1432 /* remove I2C tuner */
1433 client = dev->i2c_client_tuner;
1435 module_put(client->dev.driver->owner);
1436 i2c_unregister_device(client);
1442 static int rtl28xxu_init(struct dvb_usb_device *d)
1447 dev_dbg(&d->intf->dev, "\n");
1449 /* init USB endpoints */
1450 ret = rtl28xxu_rd_reg(d, USB_SYSCTL_0, &val);
1454 /* enable DMA and Full Packet Mode*/
1456 ret = rtl28xxu_wr_reg(d, USB_SYSCTL_0, val);
1460 /* set EPA maximum packet size to 0x0200 */
1461 ret = rtl28xxu_wr_regs(d, USB_EPA_MAXPKT, "\x00\x02\x00\x00", 4);
1465 /* change EPA FIFO length */
1466 ret = rtl28xxu_wr_regs(d, USB_EPA_FIFO_CFG, "\x14\x00\x00\x00", 4);
1472 dev_dbg(&d->intf->dev, "failed=%d\n", ret);
1476 static int rtl2831u_power_ctrl(struct dvb_usb_device *d, int onoff)
1479 u8 gpio, sys0, epa_ctl[2];
1481 dev_dbg(&d->intf->dev, "onoff=%d\n", onoff);
1484 ret = rtl28xxu_rd_reg(d, SYS_SYS0, &sys0);
1488 /* tuner power, read GPIOs */
1489 ret = rtl28xxu_rd_reg(d, SYS_GPIO_OUT_VAL, &gpio);
1493 dev_dbg(&d->intf->dev, "RD SYS0=%02x GPIO_OUT_VAL=%02x\n", sys0, gpio);
1496 gpio |= 0x01; /* GPIO0 = 1 */
1497 gpio &= (~0x10); /* GPIO4 = 0 */
1498 gpio |= 0x04; /* GPIO2 = 1, LED on */
1501 epa_ctl[0] = 0x00; /* clear stall */
1502 epa_ctl[1] = 0x00; /* clear reset */
1504 gpio &= (~0x01); /* GPIO0 = 0 */
1505 gpio |= 0x10; /* GPIO4 = 1 */
1506 gpio &= (~0x04); /* GPIO2 = 1, LED off */
1507 sys0 = sys0 & (~0xc0);
1508 epa_ctl[0] = 0x10; /* set stall */
1509 epa_ctl[1] = 0x02; /* set reset */
1512 dev_dbg(&d->intf->dev, "WR SYS0=%02x GPIO_OUT_VAL=%02x\n", sys0, gpio);
1515 ret = rtl28xxu_wr_reg(d, SYS_SYS0, sys0);
1519 /* tuner power, write GPIOs */
1520 ret = rtl28xxu_wr_reg(d, SYS_GPIO_OUT_VAL, gpio);
1524 /* streaming EP: stall & reset */
1525 ret = rtl28xxu_wr_regs(d, USB_EPA_CTL, epa_ctl, 2);
1530 usb_clear_halt(d->udev, usb_rcvbulkpipe(d->udev, 0x81));
1534 dev_dbg(&d->intf->dev, "failed=%d\n", ret);
1538 static int rtl2832u_power_ctrl(struct dvb_usb_device *d, int onoff)
1542 dev_dbg(&d->intf->dev, "onoff=%d\n", onoff);
1545 /* GPIO3=1, GPIO4=0 */
1546 ret = rtl28xxu_wr_reg_mask(d, SYS_GPIO_OUT_VAL, 0x08, 0x18);
1551 ret = rtl28xxu_wr_reg_mask(d, SYS_DEMOD_CTL1, 0x00, 0x10);
1556 ret = rtl28xxu_wr_reg_mask(d, SYS_DEMOD_CTL, 0x80, 0x80);
1561 ret = rtl28xxu_wr_reg_mask(d, SYS_DEMOD_CTL, 0x20, 0x20);
1565 /* streaming EP: clear stall & reset */
1566 ret = rtl28xxu_wr_regs(d, USB_EPA_CTL, "\x00\x00", 2);
1570 ret = usb_clear_halt(d->udev, usb_rcvbulkpipe(d->udev, 0x81));
1575 ret = rtl28xxu_wr_reg_mask(d, SYS_GPIO_OUT_VAL, 0x10, 0x10);
1580 ret = rtl28xxu_wr_reg_mask(d, SYS_DEMOD_CTL, 0x00, 0x80);
1584 /* streaming EP: set stall & reset */
1585 ret = rtl28xxu_wr_regs(d, USB_EPA_CTL, "\x10\x02", 2);
1592 dev_dbg(&d->intf->dev, "failed=%d\n", ret);
1596 static int rtl28xxu_power_ctrl(struct dvb_usb_device *d, int onoff)
1598 struct rtl28xxu_dev *dev = d_to_priv(d);
1600 if (dev->chip_id == CHIP_ID_RTL2831U)
1601 return rtl2831u_power_ctrl(d, onoff);
1603 return rtl2832u_power_ctrl(d, onoff);
1606 static int rtl28xxu_frontend_ctrl(struct dvb_frontend *fe, int onoff)
1608 struct dvb_usb_device *d = fe_to_d(fe);
1609 struct rtl28xxu_dev *dev = fe_to_priv(fe);
1610 struct rtl2832_platform_data *pdata = &dev->rtl2832_platform_data;
1614 dev_dbg(&d->intf->dev, "fe=%d onoff=%d\n", fe->id, onoff);
1616 if (dev->chip_id == CHIP_ID_RTL2831U)
1620 /* control internal demod ADC */
1622 val = 0x48; /* enable ADC */
1624 val = 0x00; /* disable ADC */
1626 ret = rtl28xxu_wr_reg_mask(d, SYS_DEMOD_CTL, val, 0x48);
1629 } else if (fe->id == 1) {
1630 /* bypass slave demod TS through master demod */
1631 ret = pdata->slave_ts_ctrl(dev->i2c_client_demod, onoff);
1638 dev_dbg(&d->intf->dev, "failed=%d\n", ret);
1642 #if IS_ENABLED(CONFIG_RC_CORE)
1643 static int rtl2831u_rc_query(struct dvb_usb_device *d)
1646 struct rtl28xxu_dev *dev = d->priv;
1649 static const struct rtl28xxu_reg_val rc_nec_tab[] = {
1666 /* init remote controller */
1667 if (!dev->rc_active) {
1668 for (i = 0; i < ARRAY_SIZE(rc_nec_tab); i++) {
1669 ret = rtl28xxu_wr_reg(d, rc_nec_tab[i].reg,
1674 dev->rc_active = true;
1677 ret = rtl28xxu_rd_regs(d, SYS_IRRC_RP, buf, 5);
1681 if (buf[4] & 0x01) {
1682 enum rc_proto proto;
1684 if (buf[2] == (u8) ~buf[3]) {
1685 if (buf[0] == (u8) ~buf[1]) {
1686 /* NEC standard (16 bit) */
1687 rc_code = RC_SCANCODE_NEC(buf[0], buf[2]);
1688 proto = RC_PROTO_NEC;
1690 /* NEC extended (24 bit) */
1691 rc_code = RC_SCANCODE_NECX(buf[0] << 8 | buf[1],
1693 proto = RC_PROTO_NECX;
1696 /* NEC full (32 bit) */
1697 rc_code = RC_SCANCODE_NEC32(buf[0] << 24 | buf[1] << 16 |
1698 buf[2] << 8 | buf[3]);
1699 proto = RC_PROTO_NEC32;
1702 rc_keydown(d->rc_dev, proto, rc_code, 0);
1704 ret = rtl28xxu_wr_reg(d, SYS_IRRC_SR, 1);
1708 /* repeated intentionally to avoid extra keypress */
1709 ret = rtl28xxu_wr_reg(d, SYS_IRRC_SR, 1);
1716 dev_dbg(&d->intf->dev, "failed=%d\n", ret);
1720 static int rtl2831u_get_rc_config(struct dvb_usb_device *d,
1721 struct dvb_usb_rc *rc)
1723 rc->map_name = RC_MAP_EMPTY;
1724 rc->allowed_protos = RC_PROTO_BIT_NEC | RC_PROTO_BIT_NECX |
1726 rc->query = rtl2831u_rc_query;
1732 static int rtl2832u_rc_query(struct dvb_usb_device *d)
1735 struct rtl28xxu_dev *dev = d->priv;
1736 struct ir_raw_event ev = {};
1738 static const struct rtl28xxu_reg_val_mask refresh_tab[] = {
1739 {IR_RX_IF, 0x03, 0xff},
1740 {IR_RX_BUF_CTRL, 0x80, 0xff},
1741 {IR_RX_CTRL, 0x80, 0xff},
1744 /* init remote controller */
1745 if (!dev->rc_active) {
1746 static const struct rtl28xxu_reg_val_mask init_tab[] = {
1747 {SYS_DEMOD_CTL1, 0x00, 0x04},
1748 {SYS_DEMOD_CTL1, 0x00, 0x08},
1749 {USB_CTRL, 0x20, 0x20},
1750 {SYS_GPIO_DIR, 0x00, 0x08},
1751 {SYS_GPIO_OUT_EN, 0x08, 0x08},
1752 {SYS_GPIO_OUT_VAL, 0x08, 0x08},
1753 {IR_MAX_DURATION0, 0xd0, 0xff},
1754 {IR_MAX_DURATION1, 0x07, 0xff},
1755 {IR_IDLE_LEN0, 0xc0, 0xff},
1756 {IR_IDLE_LEN1, 0x00, 0xff},
1757 {IR_GLITCH_LEN, 0x03, 0xff},
1758 {IR_RX_CLK, 0x09, 0xff},
1759 {IR_RX_CFG, 0x1c, 0xff},
1760 {IR_MAX_H_TOL_LEN, 0x1e, 0xff},
1761 {IR_MAX_L_TOL_LEN, 0x1e, 0xff},
1762 {IR_RX_CTRL, 0x80, 0xff},
1765 for (i = 0; i < ARRAY_SIZE(init_tab); i++) {
1766 ret = rtl28xxu_wr_reg_mask(d, init_tab[i].reg,
1767 init_tab[i].val, init_tab[i].mask);
1772 dev->rc_active = true;
1775 ret = rtl28xxu_rd_reg(d, IR_RX_IF, &buf[0]);
1782 ret = rtl28xxu_rd_reg(d, IR_RX_BC, &buf[0]);
1783 if (ret || buf[0] > sizeof(buf))
1788 /* read raw code from hw */
1789 ret = rtl28xxu_rd_regs(d, IR_RX_BUF, buf, len);
1793 /* let hw receive new code */
1794 for (i = 0; i < ARRAY_SIZE(refresh_tab); i++) {
1795 ret = rtl28xxu_wr_reg_mask(d, refresh_tab[i].reg,
1796 refresh_tab[i].val, refresh_tab[i].mask);
1801 /* pass data to Kernel IR decoder */
1802 for (i = 0; i < len; i++) {
1803 ev.pulse = buf[i] >> 7;
1804 ev.duration = 51 * (buf[i] & 0x7f);
1805 ir_raw_event_store_with_filter(d->rc_dev, &ev);
1808 /* 'flush' ir_raw_event_store_with_filter() */
1809 ir_raw_event_handle(d->rc_dev);
1813 dev_dbg(&d->intf->dev, "failed=%d\n", ret);
1817 static int rtl2832u_get_rc_config(struct dvb_usb_device *d,
1818 struct dvb_usb_rc *rc)
1820 /* disable IR interrupts in order to avoid SDR sample loss */
1821 if (rtl28xxu_disable_rc)
1822 return rtl28xxu_wr_reg(d, IR_RX_IE, 0x00);
1824 /* load empty to enable rc */
1826 rc->map_name = RC_MAP_EMPTY;
1827 rc->allowed_protos = RC_PROTO_BIT_ALL_IR_DECODER;
1828 rc->driver_type = RC_DRIVER_IR_RAW;
1829 rc->query = rtl2832u_rc_query;
1831 /* we program idle len to 0xc0, set timeout to one less */
1832 rc->timeout = 0xbf * 51;
1837 static int rtl28xxu_get_rc_config(struct dvb_usb_device *d,
1838 struct dvb_usb_rc *rc)
1840 struct rtl28xxu_dev *dev = d_to_priv(d);
1842 if (dev->chip_id == CHIP_ID_RTL2831U)
1843 return rtl2831u_get_rc_config(d, rc);
1845 return rtl2832u_get_rc_config(d, rc);
1848 #define rtl28xxu_get_rc_config NULL
1851 static int rtl28xxu_pid_filter_ctrl(struct dvb_usb_adapter *adap, int onoff)
1853 struct rtl28xxu_dev *dev = adap_to_priv(adap);
1855 if (dev->chip_id == CHIP_ID_RTL2831U) {
1856 struct rtl2830_platform_data *pdata = &dev->rtl2830_platform_data;
1858 return pdata->pid_filter_ctrl(adap->fe[0], onoff);
1860 struct rtl2832_platform_data *pdata = &dev->rtl2832_platform_data;
1862 return pdata->pid_filter_ctrl(adap->fe[0], onoff);
1866 static int rtl28xxu_pid_filter(struct dvb_usb_adapter *adap, int index,
1869 struct rtl28xxu_dev *dev = adap_to_priv(adap);
1871 if (dev->chip_id == CHIP_ID_RTL2831U) {
1872 struct rtl2830_platform_data *pdata = &dev->rtl2830_platform_data;
1874 return pdata->pid_filter(adap->fe[0], index, pid, onoff);
1876 struct rtl2832_platform_data *pdata = &dev->rtl2832_platform_data;
1878 return pdata->pid_filter(adap->fe[0], index, pid, onoff);
1882 static const struct dvb_usb_device_properties rtl28xxu_props = {
1883 .driver_name = KBUILD_MODNAME,
1884 .owner = THIS_MODULE,
1885 .adapter_nr = adapter_nr,
1886 .size_of_priv = sizeof(struct rtl28xxu_dev),
1888 .identify_state = rtl28xxu_identify_state,
1889 .power_ctrl = rtl28xxu_power_ctrl,
1890 .frontend_ctrl = rtl28xxu_frontend_ctrl,
1891 .i2c_algo = &rtl28xxu_i2c_algo,
1892 .read_config = rtl28xxu_read_config,
1893 .frontend_attach = rtl28xxu_frontend_attach,
1894 .frontend_detach = rtl28xxu_frontend_detach,
1895 .tuner_attach = rtl28xxu_tuner_attach,
1896 .tuner_detach = rtl28xxu_tuner_detach,
1897 .init = rtl28xxu_init,
1898 .get_rc_config = rtl28xxu_get_rc_config,
1903 .caps = DVB_USB_ADAP_HAS_PID_FILTER |
1904 DVB_USB_ADAP_PID_FILTER_CAN_BE_TURNED_OFF,
1906 .pid_filter_count = 32,
1907 .pid_filter_ctrl = rtl28xxu_pid_filter_ctrl,
1908 .pid_filter = rtl28xxu_pid_filter,
1910 .stream = DVB_USB_STREAM_BULK(0x81, 6, 8 * 512),
1915 static const struct usb_device_id rtl28xxu_id_table[] = {
1916 /* RTL2831U devices: */
1917 { DVB_USB_DEVICE(USB_VID_REALTEK, USB_PID_REALTEK_RTL2831U,
1918 &rtl28xxu_props, "Realtek RTL2831U reference design", NULL) },
1919 { DVB_USB_DEVICE(USB_VID_WIDEVIEW, USB_PID_FREECOM_DVBT,
1920 &rtl28xxu_props, "Freecom USB2.0 DVB-T", NULL) },
1921 { DVB_USB_DEVICE(USB_VID_WIDEVIEW, USB_PID_FREECOM_DVBT_2,
1922 &rtl28xxu_props, "Freecom USB2.0 DVB-T", NULL) },
1924 /* RTL2832U devices: */
1925 { DVB_USB_DEVICE(USB_VID_REALTEK, 0x2832,
1926 &rtl28xxu_props, "Realtek RTL2832U reference design", NULL) },
1927 { DVB_USB_DEVICE(USB_VID_REALTEK, 0x2838,
1928 &rtl28xxu_props, "Realtek RTL2832U reference design", NULL) },
1929 { DVB_USB_DEVICE(USB_VID_TERRATEC, USB_PID_TERRATEC_CINERGY_T_STICK_BLACK_REV1,
1930 &rtl28xxu_props, "TerraTec Cinergy T Stick Black", RC_MAP_TERRATEC_SLIM) },
1931 { DVB_USB_DEVICE(USB_VID_GTEK, USB_PID_DELOCK_USB2_DVBT,
1932 &rtl28xxu_props, "G-Tek Electronics Group Lifeview LV5TDLX DVB-T", NULL) },
1933 { DVB_USB_DEVICE(USB_VID_TERRATEC, USB_PID_NOXON_DAB_STICK,
1934 &rtl28xxu_props, "TerraTec NOXON DAB Stick", NULL) },
1935 { DVB_USB_DEVICE(USB_VID_TERRATEC, USB_PID_NOXON_DAB_STICK_REV2,
1936 &rtl28xxu_props, "TerraTec NOXON DAB Stick (rev 2)", NULL) },
1937 { DVB_USB_DEVICE(USB_VID_TERRATEC, USB_PID_NOXON_DAB_STICK_REV3,
1938 &rtl28xxu_props, "TerraTec NOXON DAB Stick (rev 3)", NULL) },
1939 { DVB_USB_DEVICE(USB_VID_GTEK, USB_PID_TREKSTOR_TERRES_2_0,
1940 &rtl28xxu_props, "Trekstor DVB-T Stick Terres 2.0", NULL) },
1941 { DVB_USB_DEVICE(USB_VID_DEXATEK, 0x1101,
1942 &rtl28xxu_props, "Dexatek DK DVB-T Dongle", NULL) },
1943 { DVB_USB_DEVICE(USB_VID_LEADTEK, 0x6680,
1944 &rtl28xxu_props, "DigitalNow Quad DVB-T Receiver", NULL) },
1945 { DVB_USB_DEVICE(USB_VID_LEADTEK, USB_PID_WINFAST_DTV_DONGLE_MINID,
1946 &rtl28xxu_props, "Leadtek Winfast DTV Dongle Mini D", NULL) },
1947 { DVB_USB_DEVICE(USB_VID_LEADTEK, USB_PID_WINFAST_DTV2000DS_PLUS,
1948 &rtl28xxu_props, "Leadtek WinFast DTV2000DS Plus", RC_MAP_LEADTEK_Y04G0051) },
1949 { DVB_USB_DEVICE(USB_VID_TERRATEC, 0x00d3,
1950 &rtl28xxu_props, "TerraTec Cinergy T Stick RC (Rev. 3)", NULL) },
1951 { DVB_USB_DEVICE(USB_VID_DEXATEK, 0x1102,
1952 &rtl28xxu_props, "Dexatek DK mini DVB-T Dongle", NULL) },
1953 { DVB_USB_DEVICE(USB_VID_TERRATEC, 0x00d7,
1954 &rtl28xxu_props, "TerraTec Cinergy T Stick+", NULL) },
1955 { DVB_USB_DEVICE(USB_VID_KWORLD_2, 0xd3a8,
1956 &rtl28xxu_props, "ASUS My Cinema-U3100Mini Plus V2", NULL) },
1957 { DVB_USB_DEVICE(USB_VID_KWORLD_2, 0xd393,
1958 &rtl28xxu_props, "GIGABYTE U7300", NULL) },
1959 { DVB_USB_DEVICE(USB_VID_DEXATEK, 0x1104,
1960 &rtl28xxu_props, "MSI DIGIVOX Micro HD", NULL) },
1961 { DVB_USB_DEVICE(USB_VID_COMPRO, 0x0620,
1962 &rtl28xxu_props, "Compro VideoMate U620F", NULL) },
1963 { DVB_USB_DEVICE(USB_VID_COMPRO, 0x0650,
1964 &rtl28xxu_props, "Compro VideoMate U650F", NULL) },
1965 { DVB_USB_DEVICE(USB_VID_KWORLD_2, 0xd394,
1966 &rtl28xxu_props, "MaxMedia HU394-T", NULL) },
1967 { DVB_USB_DEVICE(USB_VID_LEADTEK, 0x6a03,
1968 &rtl28xxu_props, "Leadtek WinFast DTV Dongle mini", NULL) },
1969 { DVB_USB_DEVICE(USB_VID_GTEK, USB_PID_CPYTO_REDI_PC50A,
1970 &rtl28xxu_props, "Crypto ReDi PC 50 A", NULL) },
1971 { DVB_USB_DEVICE(USB_VID_KYE, 0x707f,
1972 &rtl28xxu_props, "Genius TVGo DVB-T03", NULL) },
1973 { DVB_USB_DEVICE(USB_VID_KWORLD_2, 0xd395,
1974 &rtl28xxu_props, "Peak DVB-T USB", NULL) },
1975 { DVB_USB_DEVICE(USB_VID_KWORLD_2, USB_PID_SVEON_STV20_RTL2832U,
1976 &rtl28xxu_props, "Sveon STV20", NULL) },
1977 { DVB_USB_DEVICE(USB_VID_KWORLD_2, USB_PID_SVEON_STV21,
1978 &rtl28xxu_props, "Sveon STV21", NULL) },
1979 { DVB_USB_DEVICE(USB_VID_KWORLD_2, USB_PID_SVEON_STV27,
1980 &rtl28xxu_props, "Sveon STV27", NULL) },
1981 { DVB_USB_DEVICE(USB_VID_KWORLD_2, USB_PID_TURBOX_DTT_2000,
1982 &rtl28xxu_props, "TURBO-X Pure TV Tuner DTT-2000", NULL) },
1983 { DVB_USB_DEVICE(USB_VID_GTEK, USB_PID_PROLECTRIX_DV107669,
1984 &rtl28xxu_props, "PROlectrix DV107669", NULL) },
1986 /* RTL2832P devices: */
1987 { DVB_USB_DEVICE(USB_VID_HANFTEK, 0x0131,
1988 &rtl28xxu_props, "Astrometa DVB-T2",
1989 RC_MAP_ASTROMETA_T2HYBRID) },
1990 { DVB_USB_DEVICE(0x5654, 0xca42,
1991 &rtl28xxu_props, "GoTView MasterHD 3", NULL) },
1994 MODULE_DEVICE_TABLE(usb, rtl28xxu_id_table);
1996 static struct usb_driver rtl28xxu_usb_driver = {
1997 .name = KBUILD_MODNAME,
1998 .id_table = rtl28xxu_id_table,
1999 .probe = dvb_usbv2_probe,
2000 .disconnect = dvb_usbv2_disconnect,
2001 .suspend = dvb_usbv2_suspend,
2002 .resume = dvb_usbv2_resume,
2003 .reset_resume = dvb_usbv2_reset_resume,
2008 module_usb_driver(rtl28xxu_usb_driver);
2010 MODULE_DESCRIPTION("Realtek RTL28xxU DVB USB driver");
2011 MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
2012 MODULE_AUTHOR("Thomas Mair <thomas.mair86@googlemail.com>");
2013 MODULE_LICENSE("GPL");