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 /* method 1 - integrated demod */
180 if (msg[0].buf[0] == 0x00) {
181 /* return demod page from driver cache */
182 msg[1].buf[0] = dev->page;
185 req.value = (msg[0].buf[0] << 8) | (msg[0].addr << 1);
186 req.index = CMD_DEMOD_RD | dev->page;
187 req.size = msg[1].len;
188 req.data = &msg[1].buf[0];
189 ret = rtl28xxu_ctrl_msg(d, &req);
191 } else if (msg[0].len < 2) {
192 /* method 2 - old I2C */
193 req.value = (msg[0].buf[0] << 8) | (msg[0].addr << 1);
194 req.index = CMD_I2C_RD;
195 req.size = msg[1].len;
196 req.data = &msg[1].buf[0];
197 ret = rtl28xxu_ctrl_msg(d, &req);
199 /* method 3 - new I2C */
200 req.value = (msg[0].addr << 1);
201 req.index = CMD_I2C_DA_WR;
202 req.size = msg[0].len;
203 req.data = msg[0].buf;
204 ret = rtl28xxu_ctrl_msg(d, &req);
206 goto err_mutex_unlock;
208 req.value = (msg[0].addr << 1);
209 req.index = CMD_I2C_DA_RD;
210 req.size = msg[1].len;
211 req.data = msg[1].buf;
212 ret = rtl28xxu_ctrl_msg(d, &req);
214 } else if (num == 1 && !(msg[0].flags & I2C_M_RD)) {
215 if (msg[0].len > 22) {
216 /* TODO: check msg[0].len max */
218 goto err_mutex_unlock;
219 } else if (msg[0].addr == 0x10) {
220 /* method 1 - integrated demod */
221 if (msg[0].buf[0] == 0x00) {
222 /* save demod page for later demod access */
223 dev->page = msg[0].buf[1];
226 req.value = (msg[0].buf[0] << 8) |
228 req.index = CMD_DEMOD_WR | dev->page;
229 req.size = msg[0].len-1;
230 req.data = &msg[0].buf[1];
231 ret = rtl28xxu_ctrl_msg(d, &req);
233 } else if ((msg[0].len < 23) && (!dev->new_i2c_write)) {
234 /* method 2 - old I2C */
235 req.value = (msg[0].buf[0] << 8) | (msg[0].addr << 1);
236 req.index = CMD_I2C_WR;
237 req.size = msg[0].len-1;
238 req.data = &msg[0].buf[1];
239 ret = rtl28xxu_ctrl_msg(d, &req);
241 /* method 3 - new I2C */
242 req.value = (msg[0].addr << 1);
243 req.index = CMD_I2C_DA_WR;
244 req.size = msg[0].len;
245 req.data = msg[0].buf;
246 ret = rtl28xxu_ctrl_msg(d, &req);
248 } else if (num == 1 && (msg[0].flags & I2C_M_RD)) {
249 req.value = (msg[0].addr << 1);
250 req.index = CMD_I2C_DA_RD;
251 req.size = msg[0].len;
252 req.data = msg[0].buf;
253 ret = rtl28xxu_ctrl_msg(d, &req);
258 /* Retry failed I2C messages */
263 mutex_unlock(&d->i2c_mutex);
265 return ret ? ret : num;
268 static u32 rtl28xxu_i2c_func(struct i2c_adapter *adapter)
273 static struct i2c_algorithm rtl28xxu_i2c_algo = {
274 .master_xfer = rtl28xxu_i2c_xfer,
275 .functionality = rtl28xxu_i2c_func,
278 static int rtl2831u_read_config(struct dvb_usb_device *d)
280 struct rtl28xxu_dev *dev = d_to_priv(d);
283 /* open RTL2831U/RTL2830 I2C gate */
284 struct rtl28xxu_req req_gate_open = {0x0120, 0x0011, 0x0001, "\x08"};
286 struct rtl28xxu_req req_mt2060 = {0x00c0, CMD_I2C_RD, 1, buf};
287 struct rtl28xxu_req req_qt1010 = {0x0fc4, CMD_I2C_RD, 1, buf};
289 dev_dbg(&d->intf->dev, "\n");
293 * =========================================================
294 * GPIO0 | tuner#0 | 0 off | 1 on | MXL5005S (?)
295 * GPIO2 | LED | 0 off | 1 on |
296 * GPIO4 | tuner#1 | 0 on | 1 off | MT2060
300 ret = rtl28xxu_wr_reg(d, SYS_GPIO_DIR, 0x0a);
304 /* enable as output GPIO0, GPIO2, GPIO4 */
305 ret = rtl28xxu_wr_reg(d, SYS_GPIO_OUT_EN, 0x15);
310 * Probe used tuner. We need to know used tuner before demod attach
311 * since there is some demod params needed to set according to tuner.
314 /* demod needs some time to wake up */
317 dev->tuner_name = "NONE";
319 /* open demod I2C gate */
320 ret = rtl28xxu_ctrl_msg(d, &req_gate_open);
324 /* check QT1010 ID(?) register; reg=0f val=2c */
325 ret = rtl28xxu_ctrl_msg(d, &req_qt1010);
326 if (ret == 0 && buf[0] == 0x2c) {
327 dev->tuner = TUNER_RTL2830_QT1010;
328 dev->tuner_name = "QT1010";
332 /* open demod I2C gate */
333 ret = rtl28xxu_ctrl_msg(d, &req_gate_open);
337 /* check MT2060 ID register; reg=00 val=63 */
338 ret = rtl28xxu_ctrl_msg(d, &req_mt2060);
339 if (ret == 0 && buf[0] == 0x63) {
340 dev->tuner = TUNER_RTL2830_MT2060;
341 dev->tuner_name = "MT2060";
345 /* assume MXL5005S */
346 dev->tuner = TUNER_RTL2830_MXL5005S;
347 dev->tuner_name = "MXL5005S";
351 dev_dbg(&d->intf->dev, "tuner=%s\n", dev->tuner_name);
355 dev_dbg(&d->intf->dev, "failed=%d\n", ret);
359 static int rtl2832u_read_config(struct dvb_usb_device *d)
361 struct rtl28xxu_dev *dev = d_to_priv(d);
364 /* open RTL2832U/RTL2832 I2C gate */
365 struct rtl28xxu_req req_gate_open = {0x0120, 0x0011, 0x0001, "\x18"};
366 /* close RTL2832U/RTL2832 I2C gate */
367 struct rtl28xxu_req req_gate_close = {0x0120, 0x0011, 0x0001, "\x10"};
369 struct rtl28xxu_req req_fc0012 = {0x00c6, CMD_I2C_RD, 1, buf};
370 struct rtl28xxu_req req_fc0013 = {0x00c6, CMD_I2C_RD, 1, buf};
371 struct rtl28xxu_req req_mt2266 = {0x00c0, CMD_I2C_RD, 1, buf};
372 struct rtl28xxu_req req_fc2580 = {0x01ac, CMD_I2C_RD, 1, buf};
373 struct rtl28xxu_req req_mt2063 = {0x00c0, CMD_I2C_RD, 1, buf};
374 struct rtl28xxu_req req_max3543 = {0x00c0, CMD_I2C_RD, 1, buf};
375 struct rtl28xxu_req req_tua9001 = {0x7ec0, CMD_I2C_RD, 2, buf};
376 struct rtl28xxu_req req_mxl5007t = {0xd9c0, CMD_I2C_RD, 1, buf};
377 struct rtl28xxu_req req_e4000 = {0x02c8, CMD_I2C_RD, 1, buf};
378 struct rtl28xxu_req req_tda18272 = {0x00c0, CMD_I2C_RD, 2, buf};
379 struct rtl28xxu_req req_r820t = {0x0034, CMD_I2C_RD, 1, buf};
380 struct rtl28xxu_req req_r828d = {0x0074, CMD_I2C_RD, 1, buf};
381 struct rtl28xxu_req req_mn88472 = {0xff38, CMD_I2C_RD, 1, buf};
382 struct rtl28xxu_req req_mn88473 = {0xff38, CMD_I2C_RD, 1, buf};
383 struct rtl28xxu_req req_cxd2837er = {0xfdd8, CMD_I2C_RD, 1, buf};
384 struct rtl28xxu_req req_si2157 = {0x00c0, CMD_I2C_RD, 1, buf};
385 struct rtl28xxu_req req_si2168 = {0x00c8, CMD_I2C_RD, 1, buf};
387 dev_dbg(&d->intf->dev, "\n");
389 /* enable GPIO3 and GPIO6 as output */
390 ret = rtl28xxu_wr_reg_mask(d, SYS_GPIO_DIR, 0x00, 0x40);
394 ret = rtl28xxu_wr_reg_mask(d, SYS_GPIO_OUT_EN, 0x48, 0x48);
399 * Probe used tuner. We need to know used tuner before demod attach
400 * since there is some demod params needed to set according to tuner.
403 /* open demod I2C gate */
404 ret = rtl28xxu_ctrl_msg(d, &req_gate_open);
408 dev->tuner_name = "NONE";
410 /* check FC0012 ID register; reg=00 val=a1 */
411 ret = rtl28xxu_ctrl_msg(d, &req_fc0012);
412 if (ret == 0 && buf[0] == 0xa1) {
413 dev->tuner = TUNER_RTL2832_FC0012;
414 dev->tuner_name = "FC0012";
418 /* check FC0013 ID register; reg=00 val=a3 */
419 ret = rtl28xxu_ctrl_msg(d, &req_fc0013);
420 if (ret == 0 && buf[0] == 0xa3) {
421 dev->tuner = TUNER_RTL2832_FC0013;
422 dev->tuner_name = "FC0013";
426 /* check MT2266 ID register; reg=00 val=85 */
427 ret = rtl28xxu_ctrl_msg(d, &req_mt2266);
428 if (ret == 0 && buf[0] == 0x85) {
429 dev->tuner = TUNER_RTL2832_MT2266;
430 dev->tuner_name = "MT2266";
434 /* check FC2580 ID register; reg=01 val=56 */
435 ret = rtl28xxu_ctrl_msg(d, &req_fc2580);
436 if (ret == 0 && buf[0] == 0x56) {
437 dev->tuner = TUNER_RTL2832_FC2580;
438 dev->tuner_name = "FC2580";
442 /* check MT2063 ID register; reg=00 val=9e || 9c */
443 ret = rtl28xxu_ctrl_msg(d, &req_mt2063);
444 if (ret == 0 && (buf[0] == 0x9e || buf[0] == 0x9c)) {
445 dev->tuner = TUNER_RTL2832_MT2063;
446 dev->tuner_name = "MT2063";
450 /* check MAX3543 ID register; reg=00 val=38 */
451 ret = rtl28xxu_ctrl_msg(d, &req_max3543);
452 if (ret == 0 && buf[0] == 0x38) {
453 dev->tuner = TUNER_RTL2832_MAX3543;
454 dev->tuner_name = "MAX3543";
458 /* check TUA9001 ID register; reg=7e val=2328 */
459 ret = rtl28xxu_ctrl_msg(d, &req_tua9001);
460 if (ret == 0 && buf[0] == 0x23 && buf[1] == 0x28) {
461 dev->tuner = TUNER_RTL2832_TUA9001;
462 dev->tuner_name = "TUA9001";
466 /* check MXL5007R ID register; reg=d9 val=14 */
467 ret = rtl28xxu_ctrl_msg(d, &req_mxl5007t);
468 if (ret == 0 && buf[0] == 0x14) {
469 dev->tuner = TUNER_RTL2832_MXL5007T;
470 dev->tuner_name = "MXL5007T";
474 /* check E4000 ID register; reg=02 val=40 */
475 ret = rtl28xxu_ctrl_msg(d, &req_e4000);
476 if (ret == 0 && buf[0] == 0x40) {
477 dev->tuner = TUNER_RTL2832_E4000;
478 dev->tuner_name = "E4000";
482 /* check TDA18272 ID register; reg=00 val=c760 */
483 ret = rtl28xxu_ctrl_msg(d, &req_tda18272);
484 if (ret == 0 && (buf[0] == 0xc7 || buf[1] == 0x60)) {
485 dev->tuner = TUNER_RTL2832_TDA18272;
486 dev->tuner_name = "TDA18272";
490 /* check R820T ID register; reg=00 val=69 */
491 ret = rtl28xxu_ctrl_msg(d, &req_r820t);
492 if (ret == 0 && buf[0] == 0x69) {
493 dev->tuner = TUNER_RTL2832_R820T;
494 dev->tuner_name = "R820T";
498 /* check R828D ID register; reg=00 val=69 */
499 ret = rtl28xxu_ctrl_msg(d, &req_r828d);
500 if (ret == 0 && buf[0] == 0x69) {
501 dev->tuner = TUNER_RTL2832_R828D;
502 dev->tuner_name = "R828D";
506 /* GPIO0 and GPIO5 to reset Si2157/Si2168 tuner and demod */
507 ret = rtl28xxu_wr_reg_mask(d, SYS_GPIO_OUT_VAL, 0x00, 0x21);
511 ret = rtl28xxu_wr_reg_mask(d, SYS_GPIO_OUT_EN, 0x00, 0x21);
517 ret = rtl28xxu_wr_reg_mask(d, SYS_GPIO_OUT_VAL, 0x21, 0x21);
521 ret = rtl28xxu_wr_reg_mask(d, SYS_GPIO_OUT_EN, 0x21, 0x21);
527 /* check Si2157 ID register; reg=c0 val=80 */
528 ret = rtl28xxu_ctrl_msg(d, &req_si2157);
529 if (ret == 0 && ((buf[0] & 0x80) == 0x80)) {
530 dev->tuner = TUNER_RTL2832_SI2157;
531 dev->tuner_name = "SI2157";
536 dev_dbg(&d->intf->dev, "tuner=%s\n", dev->tuner_name);
538 /* probe slave demod */
539 if (dev->tuner == TUNER_RTL2832_R828D) {
540 /* power off slave demod on GPIO0 to reset CXD2837ER */
541 ret = rtl28xxu_wr_reg_mask(d, SYS_GPIO_OUT_VAL, 0x00, 0x01);
545 ret = rtl28xxu_wr_reg_mask(d, SYS_GPIO_OUT_EN, 0x00, 0x01);
551 /* power on slave demod on GPIO0 */
552 ret = rtl28xxu_wr_reg_mask(d, SYS_GPIO_OUT_VAL, 0x01, 0x01);
556 ret = rtl28xxu_wr_reg_mask(d, SYS_GPIO_DIR, 0x00, 0x01);
560 ret = rtl28xxu_wr_reg_mask(d, SYS_GPIO_OUT_EN, 0x01, 0x01);
564 /* slave demod needs some time to wake up */
567 /* check slave answers */
568 ret = rtl28xxu_ctrl_msg(d, &req_mn88472);
569 if (ret == 0 && buf[0] == 0x02) {
570 dev_dbg(&d->intf->dev, "MN88472 found\n");
571 dev->slave_demod = SLAVE_DEMOD_MN88472;
575 ret = rtl28xxu_ctrl_msg(d, &req_mn88473);
576 if (ret == 0 && buf[0] == 0x03) {
577 dev_dbg(&d->intf->dev, "MN88473 found\n");
578 dev->slave_demod = SLAVE_DEMOD_MN88473;
582 ret = rtl28xxu_ctrl_msg(d, &req_cxd2837er);
583 if (ret == 0 && buf[0] == 0xb1) {
584 dev_dbg(&d->intf->dev, "CXD2837ER found\n");
585 dev->slave_demod = SLAVE_DEMOD_CXD2837ER;
589 if (dev->tuner == TUNER_RTL2832_SI2157) {
590 /* check Si2168 ID register; reg=c8 val=80 */
591 ret = rtl28xxu_ctrl_msg(d, &req_si2168);
592 if (ret == 0 && ((buf[0] & 0x80) == 0x80)) {
593 dev_dbg(&d->intf->dev, "Si2168 found\n");
594 dev->slave_demod = SLAVE_DEMOD_SI2168;
600 /* close demod I2C gate */
601 ret = rtl28xxu_ctrl_msg(d, &req_gate_close);
607 dev_dbg(&d->intf->dev, "failed=%d\n", ret);
611 static int rtl28xxu_read_config(struct dvb_usb_device *d)
613 struct rtl28xxu_dev *dev = d_to_priv(d);
615 if (dev->chip_id == CHIP_ID_RTL2831U)
616 return rtl2831u_read_config(d);
618 return rtl2832u_read_config(d);
621 static int rtl28xxu_identify_state(struct dvb_usb_device *d, const char **name)
623 struct rtl28xxu_dev *dev = d_to_priv(d);
625 struct rtl28xxu_req req_demod_i2c = {0x0020, CMD_I2C_DA_RD, 0, NULL};
627 dev_dbg(&d->intf->dev, "\n");
630 * Detect chip type using I2C command that is not supported
633 ret = rtl28xxu_ctrl_msg(d, &req_demod_i2c);
635 dev->chip_id = CHIP_ID_RTL2831U;
636 } else if (ret == 0) {
637 dev->chip_id = CHIP_ID_RTL2832U;
639 dev_err(&d->intf->dev, "chip type detection failed %d\n", ret);
642 dev_dbg(&d->intf->dev, "chip_id=%u\n", dev->chip_id);
644 /* Retry failed I2C messages */
645 d->i2c_adap.retries = 3;
646 d->i2c_adap.timeout = msecs_to_jiffies(10);
650 dev_dbg(&d->intf->dev, "failed=%d\n", ret);
654 static const struct rtl2830_platform_data rtl2830_mt2060_platform_data = {
659 .agc_targ_val = 0x2d,
663 static const struct rtl2830_platform_data rtl2830_qt1010_platform_data = {
668 .agc_targ_val = 0x2d,
671 static const struct rtl2830_platform_data rtl2830_mxl5005s_platform_data = {
676 .agc_targ_val = 0x3e,
679 static int rtl2831u_frontend_attach(struct dvb_usb_adapter *adap)
681 struct dvb_usb_device *d = adap_to_d(adap);
682 struct rtl28xxu_dev *dev = d_to_priv(d);
683 struct rtl2830_platform_data *pdata = &dev->rtl2830_platform_data;
684 struct i2c_board_info board_info;
685 struct i2c_client *client;
688 dev_dbg(&d->intf->dev, "\n");
690 switch (dev->tuner) {
691 case TUNER_RTL2830_QT1010:
692 *pdata = rtl2830_qt1010_platform_data;
694 case TUNER_RTL2830_MT2060:
695 *pdata = rtl2830_mt2060_platform_data;
697 case TUNER_RTL2830_MXL5005S:
698 *pdata = rtl2830_mxl5005s_platform_data;
701 dev_err(&d->intf->dev, "unknown tuner %s\n", dev->tuner_name);
706 /* attach demodulator */
707 memset(&board_info, 0, sizeof(board_info));
708 strscpy(board_info.type, "rtl2830", I2C_NAME_SIZE);
709 board_info.addr = 0x10;
710 board_info.platform_data = pdata;
711 request_module("%s", board_info.type);
712 client = i2c_new_client_device(&d->i2c_adap, &board_info);
713 if (!i2c_client_has_driver(client)) {
718 if (!try_module_get(client->dev.driver->owner)) {
719 i2c_unregister_device(client);
724 adap->fe[0] = pdata->get_dvb_frontend(client);
725 dev->demod_i2c_adapter = pdata->get_i2c_adapter(client);
727 dev->i2c_client_demod = client;
731 dev_dbg(&d->intf->dev, "failed=%d\n", ret);
735 static const struct rtl2832_platform_data rtl2832_fc2580_platform_data = {
737 .tuner = TUNER_RTL2832_FC2580,
740 static const struct rtl2832_platform_data rtl2832_fc0012_platform_data = {
742 .tuner = TUNER_RTL2832_FC0012
745 static const struct rtl2832_platform_data rtl2832_fc0013_platform_data = {
747 .tuner = TUNER_RTL2832_FC0013
750 static const struct rtl2832_platform_data rtl2832_tua9001_platform_data = {
752 .tuner = TUNER_RTL2832_TUA9001,
755 static const struct rtl2832_platform_data rtl2832_e4000_platform_data = {
757 .tuner = TUNER_RTL2832_E4000,
760 static const struct rtl2832_platform_data rtl2832_r820t_platform_data = {
762 .tuner = TUNER_RTL2832_R820T,
765 static const struct rtl2832_platform_data rtl2832_si2157_platform_data = {
767 .tuner = TUNER_RTL2832_SI2157,
770 static int rtl2832u_fc0012_tuner_callback(struct dvb_usb_device *d,
776 dev_dbg(&d->intf->dev, "cmd=%d arg=%d\n", cmd, arg);
779 case FC_FE_CALLBACK_VHF_ENABLE:
780 /* set output values */
781 ret = rtl28xxu_rd_reg(d, SYS_GPIO_OUT_VAL, &val);
786 val &= 0xbf; /* set GPIO6 low */
788 val |= 0x40; /* set GPIO6 high */
791 ret = rtl28xxu_wr_reg(d, SYS_GPIO_OUT_VAL, val);
801 dev_dbg(&d->intf->dev, "failed=%d\n", ret);
805 static int rtl2832u_tua9001_tuner_callback(struct dvb_usb_device *d,
811 dev_dbg(&d->intf->dev, "cmd=%d arg=%d\n", cmd, arg);
814 * CEN always enabled by hardware wiring
820 case TUA9001_CMD_RESETN:
826 ret = rtl28xxu_wr_reg_mask(d, SYS_GPIO_OUT_VAL, val, 0x10);
830 case TUA9001_CMD_RXEN:
836 ret = rtl28xxu_wr_reg_mask(d, SYS_GPIO_OUT_VAL, val, 0x02);
844 dev_dbg(&d->intf->dev, "failed=%d\n", ret);
848 static int rtl2832u_frontend_callback(void *adapter_priv, int component,
851 struct i2c_adapter *adapter = adapter_priv;
852 struct device *parent = adapter->dev.parent;
853 struct i2c_adapter *parent_adapter;
854 struct dvb_usb_device *d;
855 struct rtl28xxu_dev *dev;
858 * All tuners are connected to demod muxed I2C adapter. We have to
859 * resolve its parent adapter in order to get handle for this driver
860 * private data. That is a bit hackish solution, GPIO or direct driver
861 * callback would be better...
863 if (parent != NULL && parent->type == &i2c_adapter_type)
864 parent_adapter = to_i2c_adapter(parent);
868 d = i2c_get_adapdata(parent_adapter);
871 dev_dbg(&d->intf->dev, "component=%d cmd=%d arg=%d\n",
872 component, cmd, arg);
875 case DVB_FRONTEND_COMPONENT_TUNER:
876 switch (dev->tuner) {
877 case TUNER_RTL2832_FC0012:
878 return rtl2832u_fc0012_tuner_callback(d, cmd, arg);
879 case TUNER_RTL2832_TUA9001:
880 return rtl2832u_tua9001_tuner_callback(d, cmd, arg);
887 static int rtl2832u_frontend_attach(struct dvb_usb_adapter *adap)
889 struct dvb_usb_device *d = adap_to_d(adap);
890 struct rtl28xxu_dev *dev = d_to_priv(d);
891 struct rtl2832_platform_data *pdata = &dev->rtl2832_platform_data;
892 struct i2c_board_info board_info;
893 struct i2c_client *client;
896 dev_dbg(&d->intf->dev, "\n");
898 switch (dev->tuner) {
899 case TUNER_RTL2832_FC0012:
900 *pdata = rtl2832_fc0012_platform_data;
902 case TUNER_RTL2832_FC0013:
903 *pdata = rtl2832_fc0013_platform_data;
905 case TUNER_RTL2832_FC2580:
906 *pdata = rtl2832_fc2580_platform_data;
908 case TUNER_RTL2832_TUA9001:
909 *pdata = rtl2832_tua9001_platform_data;
911 case TUNER_RTL2832_E4000:
912 *pdata = rtl2832_e4000_platform_data;
914 case TUNER_RTL2832_R820T:
915 case TUNER_RTL2832_R828D:
916 *pdata = rtl2832_r820t_platform_data;
918 case TUNER_RTL2832_SI2157:
919 *pdata = rtl2832_si2157_platform_data;
922 dev_err(&d->intf->dev, "unknown tuner %s\n", dev->tuner_name);
927 /* attach demodulator */
928 memset(&board_info, 0, sizeof(board_info));
929 strscpy(board_info.type, "rtl2832", I2C_NAME_SIZE);
930 board_info.addr = 0x10;
931 board_info.platform_data = pdata;
932 request_module("%s", board_info.type);
933 client = i2c_new_client_device(&d->i2c_adap, &board_info);
934 if (!i2c_client_has_driver(client)) {
939 if (!try_module_get(client->dev.driver->owner)) {
940 i2c_unregister_device(client);
945 adap->fe[0] = pdata->get_dvb_frontend(client);
946 dev->demod_i2c_adapter = pdata->get_i2c_adapter(client);
948 dev->i2c_client_demod = client;
950 /* set fe callback */
951 adap->fe[0]->callback = rtl2832u_frontend_callback;
953 if (dev->slave_demod) {
954 struct i2c_board_info info = {};
956 /* attach slave demodulator */
957 if (dev->slave_demod == SLAVE_DEMOD_MN88472) {
958 struct mn88472_config mn88472_config = {};
960 mn88472_config.fe = &adap->fe[1];
961 mn88472_config.i2c_wr_max = 22;
962 strscpy(info.type, "mn88472", I2C_NAME_SIZE);
963 mn88472_config.xtal = 20500000;
964 mn88472_config.ts_mode = SERIAL_TS_MODE;
965 mn88472_config.ts_clock = VARIABLE_TS_CLOCK;
967 info.platform_data = &mn88472_config;
968 request_module(info.type);
969 client = i2c_new_client_device(&d->i2c_adap, &info);
970 if (!i2c_client_has_driver(client))
971 goto err_slave_demod_failed;
973 if (!try_module_get(client->dev.driver->owner)) {
974 i2c_unregister_device(client);
975 goto err_slave_demod_failed;
978 dev->i2c_client_slave_demod = client;
979 } else if (dev->slave_demod == SLAVE_DEMOD_MN88473) {
980 struct mn88473_config mn88473_config = {};
982 mn88473_config.fe = &adap->fe[1];
983 mn88473_config.i2c_wr_max = 22;
984 strscpy(info.type, "mn88473", I2C_NAME_SIZE);
986 info.platform_data = &mn88473_config;
987 request_module(info.type);
988 client = i2c_new_client_device(&d->i2c_adap, &info);
989 if (!i2c_client_has_driver(client))
990 goto err_slave_demod_failed;
992 if (!try_module_get(client->dev.driver->owner)) {
993 i2c_unregister_device(client);
994 goto err_slave_demod_failed;
997 dev->i2c_client_slave_demod = client;
998 } else if (dev->slave_demod == SLAVE_DEMOD_CXD2837ER) {
999 struct cxd2841er_config cxd2837er_config = {};
1001 cxd2837er_config.i2c_addr = 0xd8;
1002 cxd2837er_config.xtal = SONY_XTAL_20500;
1003 cxd2837er_config.flags = (CXD2841ER_AUTO_IFHZ |
1004 CXD2841ER_NO_AGCNEG | CXD2841ER_TSBITS |
1005 CXD2841ER_EARLY_TUNE | CXD2841ER_TS_SERIAL);
1006 adap->fe[1] = dvb_attach(cxd2841er_attach_t_c,
1010 goto err_slave_demod_failed;
1011 adap->fe[1]->id = 1;
1012 dev->i2c_client_slave_demod = NULL;
1014 struct si2168_config si2168_config = {};
1015 struct i2c_adapter *adapter;
1017 si2168_config.i2c_adapter = &adapter;
1018 si2168_config.fe = &adap->fe[1];
1019 si2168_config.ts_mode = SI2168_TS_SERIAL;
1020 si2168_config.ts_clock_inv = false;
1021 si2168_config.ts_clock_gapped = true;
1022 strscpy(info.type, "si2168", I2C_NAME_SIZE);
1024 info.platform_data = &si2168_config;
1025 request_module(info.type);
1026 client = i2c_new_client_device(&d->i2c_adap, &info);
1027 if (!i2c_client_has_driver(client))
1028 goto err_slave_demod_failed;
1030 if (!try_module_get(client->dev.driver->owner)) {
1031 i2c_unregister_device(client);
1032 goto err_slave_demod_failed;
1035 dev->i2c_client_slave_demod = client;
1037 /* for Si2168 devices use only new I2C write method */
1038 dev->new_i2c_write = true;
1044 dev_dbg(&d->intf->dev, "failed=%d\n", ret);
1047 err_slave_demod_failed:
1049 * We continue on reduced mode, without DVB-T2/C, using master
1050 * demod, when slave demod fails.
1052 dev->slave_demod = SLAVE_DEMOD_NONE;
1056 static int rtl28xxu_frontend_attach(struct dvb_usb_adapter *adap)
1058 struct rtl28xxu_dev *dev = adap_to_priv(adap);
1060 if (dev->chip_id == CHIP_ID_RTL2831U)
1061 return rtl2831u_frontend_attach(adap);
1063 return rtl2832u_frontend_attach(adap);
1066 static int rtl28xxu_frontend_detach(struct dvb_usb_adapter *adap)
1068 struct dvb_usb_device *d = adap_to_d(adap);
1069 struct rtl28xxu_dev *dev = d_to_priv(d);
1070 struct i2c_client *client;
1072 dev_dbg(&d->intf->dev, "\n");
1074 /* remove I2C slave demod */
1075 client = dev->i2c_client_slave_demod;
1077 module_put(client->dev.driver->owner);
1078 i2c_unregister_device(client);
1081 /* remove I2C demod */
1082 client = dev->i2c_client_demod;
1084 module_put(client->dev.driver->owner);
1085 i2c_unregister_device(client);
1091 static struct qt1010_config rtl28xxu_qt1010_config = {
1092 .i2c_address = 0x62, /* 0xc4 */
1095 static struct mt2060_config rtl28xxu_mt2060_config = {
1096 .i2c_address = 0x60, /* 0xc0 */
1100 static struct mxl5005s_config rtl28xxu_mxl5005s_config = {
1101 .i2c_address = 0x63, /* 0xc6 */
1102 .if_freq = IF_FREQ_4570000HZ,
1103 .xtal_freq = CRYSTAL_FREQ_16000000HZ,
1104 .agc_mode = MXL_SINGLE_AGC,
1105 .tracking_filter = MXL_TF_C_H,
1106 .rssi_enable = MXL_RSSI_ENABLE,
1107 .cap_select = MXL_CAP_SEL_ENABLE,
1108 .div_out = MXL_DIV_OUT_4,
1109 .clock_out = MXL_CLOCK_OUT_DISABLE,
1110 .output_load = MXL5005S_IF_OUTPUT_LOAD_200_OHM,
1111 .top = MXL5005S_TOP_25P2,
1112 .mod_mode = MXL_DIGITAL_MODE,
1113 .if_mode = MXL_ZERO_IF,
1114 .AgcMasterByte = 0x00,
1117 static int rtl2831u_tuner_attach(struct dvb_usb_adapter *adap)
1120 struct dvb_usb_device *d = adap_to_d(adap);
1121 struct rtl28xxu_dev *dev = d_to_priv(d);
1122 struct dvb_frontend *fe;
1124 dev_dbg(&d->intf->dev, "\n");
1126 switch (dev->tuner) {
1127 case TUNER_RTL2830_QT1010:
1128 fe = dvb_attach(qt1010_attach, adap->fe[0],
1129 dev->demod_i2c_adapter,
1130 &rtl28xxu_qt1010_config);
1132 case TUNER_RTL2830_MT2060:
1133 fe = dvb_attach(mt2060_attach, adap->fe[0],
1134 dev->demod_i2c_adapter,
1135 &rtl28xxu_mt2060_config, 1220);
1137 case TUNER_RTL2830_MXL5005S:
1138 fe = dvb_attach(mxl5005s_attach, adap->fe[0],
1139 dev->demod_i2c_adapter,
1140 &rtl28xxu_mxl5005s_config);
1144 dev_err(&d->intf->dev, "unknown tuner %d\n", dev->tuner);
1154 dev_dbg(&d->intf->dev, "failed=%d\n", ret);
1158 static const struct fc0012_config rtl2832u_fc0012_config = {
1159 .i2c_address = 0x63, /* 0xc6 >> 1 */
1160 .xtal_freq = FC_XTAL_28_8_MHZ,
1163 static const struct r820t_config rtl2832u_r820t_config = {
1166 .max_i2c_msg_len = 2,
1167 .rafael_chip = CHIP_R820T,
1170 static const struct r820t_config rtl2832u_r828d_config = {
1173 .max_i2c_msg_len = 2,
1174 .rafael_chip = CHIP_R828D,
1177 static int rtl2832u_tuner_attach(struct dvb_usb_adapter *adap)
1180 struct dvb_usb_device *d = adap_to_d(adap);
1181 struct rtl28xxu_dev *dev = d_to_priv(d);
1182 struct dvb_frontend *fe = NULL;
1183 struct i2c_board_info info;
1184 struct i2c_client *client;
1185 struct v4l2_subdev *subdev = NULL;
1186 struct platform_device *pdev;
1187 struct rtl2832_sdr_platform_data pdata;
1189 dev_dbg(&d->intf->dev, "\n");
1191 memset(&info, 0, sizeof(struct i2c_board_info));
1192 memset(&pdata, 0, sizeof(pdata));
1194 switch (dev->tuner) {
1195 case TUNER_RTL2832_FC0012:
1196 fe = dvb_attach(fc0012_attach, adap->fe[0],
1197 dev->demod_i2c_adapter, &rtl2832u_fc0012_config);
1199 /* since fc0012 includs reading the signal strength delegate
1200 * that to the tuner driver */
1201 adap->fe[0]->ops.read_signal_strength =
1202 adap->fe[0]->ops.tuner_ops.get_rf_strength;
1204 case TUNER_RTL2832_FC0013:
1205 fe = dvb_attach(fc0013_attach, adap->fe[0],
1206 dev->demod_i2c_adapter, 0xc6>>1, 0, FC_XTAL_28_8_MHZ);
1208 /* fc0013 also supports signal strength reading */
1209 adap->fe[0]->ops.read_signal_strength =
1210 adap->fe[0]->ops.tuner_ops.get_rf_strength;
1212 case TUNER_RTL2832_E4000: {
1213 struct e4000_config e4000_config = {
1218 strscpy(info.type, "e4000", I2C_NAME_SIZE);
1220 info.platform_data = &e4000_config;
1222 request_module(info.type);
1223 client = i2c_new_client_device(dev->demod_i2c_adapter,
1225 if (!i2c_client_has_driver(client))
1228 if (!try_module_get(client->dev.driver->owner)) {
1229 i2c_unregister_device(client);
1233 dev->i2c_client_tuner = client;
1234 subdev = i2c_get_clientdata(client);
1237 case TUNER_RTL2832_FC2580: {
1238 struct fc2580_platform_data fc2580_pdata = {
1239 .dvb_frontend = adap->fe[0],
1241 struct i2c_board_info board_info = {};
1243 strscpy(board_info.type, "fc2580", I2C_NAME_SIZE);
1244 board_info.addr = 0x56;
1245 board_info.platform_data = &fc2580_pdata;
1246 request_module("fc2580");
1247 client = i2c_new_client_device(dev->demod_i2c_adapter,
1249 if (!i2c_client_has_driver(client))
1251 if (!try_module_get(client->dev.driver->owner)) {
1252 i2c_unregister_device(client);
1255 dev->i2c_client_tuner = client;
1256 subdev = fc2580_pdata.get_v4l2_subdev(client);
1259 case TUNER_RTL2832_TUA9001: {
1260 struct tua9001_platform_data tua9001_pdata = {
1261 .dvb_frontend = adap->fe[0],
1263 struct i2c_board_info board_info = {};
1265 /* enable GPIO1 and GPIO4 as output */
1266 ret = rtl28xxu_wr_reg_mask(d, SYS_GPIO_DIR, 0x00, 0x12);
1270 ret = rtl28xxu_wr_reg_mask(d, SYS_GPIO_OUT_EN, 0x12, 0x12);
1274 strscpy(board_info.type, "tua9001", I2C_NAME_SIZE);
1275 board_info.addr = 0x60;
1276 board_info.platform_data = &tua9001_pdata;
1277 request_module("tua9001");
1278 client = i2c_new_client_device(dev->demod_i2c_adapter,
1280 if (!i2c_client_has_driver(client))
1282 if (!try_module_get(client->dev.driver->owner)) {
1283 i2c_unregister_device(client);
1286 dev->i2c_client_tuner = client;
1289 case TUNER_RTL2832_R820T:
1290 fe = dvb_attach(r820t_attach, adap->fe[0],
1291 dev->demod_i2c_adapter,
1292 &rtl2832u_r820t_config);
1294 /* Use tuner to get the signal strength */
1295 adap->fe[0]->ops.read_signal_strength =
1296 adap->fe[0]->ops.tuner_ops.get_rf_strength;
1298 case TUNER_RTL2832_R828D:
1299 fe = dvb_attach(r820t_attach, adap->fe[0],
1300 dev->demod_i2c_adapter,
1301 &rtl2832u_r828d_config);
1302 adap->fe[0]->ops.read_signal_strength =
1303 adap->fe[0]->ops.tuner_ops.get_rf_strength;
1306 fe = dvb_attach(r820t_attach, adap->fe[1],
1307 dev->demod_i2c_adapter,
1308 &rtl2832u_r828d_config);
1309 adap->fe[1]->ops.read_signal_strength =
1310 adap->fe[1]->ops.tuner_ops.get_rf_strength;
1313 case TUNER_RTL2832_SI2157: {
1314 struct si2157_config si2157_config = {
1320 strscpy(info.type, "si2157", I2C_NAME_SIZE);
1322 info.platform_data = &si2157_config;
1323 request_module(info.type);
1324 client = i2c_new_client_device(&d->i2c_adap, &info);
1325 if (!i2c_client_has_driver(client))
1328 if (!try_module_get(client->dev.driver->owner)) {
1329 i2c_unregister_device(client);
1333 dev->i2c_client_tuner = client;
1334 subdev = i2c_get_clientdata(client);
1336 /* copy tuner ops for 2nd FE as tuner is shared */
1338 adap->fe[1]->tuner_priv =
1339 adap->fe[0]->tuner_priv;
1340 memcpy(&adap->fe[1]->ops.tuner_ops,
1341 &adap->fe[0]->ops.tuner_ops,
1342 sizeof(struct dvb_tuner_ops));
1347 dev_err(&d->intf->dev, "unknown tuner %d\n", dev->tuner);
1349 if (fe == NULL && dev->i2c_client_tuner == NULL) {
1355 switch (dev->tuner) {
1356 case TUNER_RTL2832_FC2580:
1357 case TUNER_RTL2832_FC0012:
1358 case TUNER_RTL2832_FC0013:
1359 case TUNER_RTL2832_E4000:
1360 case TUNER_RTL2832_R820T:
1361 case TUNER_RTL2832_R828D:
1362 pdata.clk = dev->rtl2832_platform_data.clk;
1363 pdata.tuner = dev->tuner;
1364 pdata.regmap = dev->rtl2832_platform_data.regmap;
1365 pdata.dvb_frontend = adap->fe[0];
1366 pdata.dvb_usb_device = d;
1367 pdata.v4l2_subdev = subdev;
1369 request_module("%s", "rtl2832_sdr");
1370 pdev = platform_device_register_data(&d->intf->dev,
1372 PLATFORM_DEVID_AUTO,
1373 &pdata, sizeof(pdata));
1374 if (IS_ERR(pdev) || pdev->dev.driver == NULL)
1376 dev->platform_device_sdr = pdev;
1379 dev_dbg(&d->intf->dev, "no SDR for tuner=%d\n", dev->tuner);
1384 dev_dbg(&d->intf->dev, "failed=%d\n", ret);
1388 static int rtl28xxu_tuner_attach(struct dvb_usb_adapter *adap)
1390 struct rtl28xxu_dev *dev = adap_to_priv(adap);
1392 if (dev->chip_id == CHIP_ID_RTL2831U)
1393 return rtl2831u_tuner_attach(adap);
1395 return rtl2832u_tuner_attach(adap);
1398 static int rtl28xxu_tuner_detach(struct dvb_usb_adapter *adap)
1400 struct dvb_usb_device *d = adap_to_d(adap);
1401 struct rtl28xxu_dev *dev = d_to_priv(d);
1402 struct i2c_client *client;
1403 struct platform_device *pdev;
1405 dev_dbg(&d->intf->dev, "\n");
1407 /* remove platform SDR */
1408 pdev = dev->platform_device_sdr;
1410 platform_device_unregister(pdev);
1412 /* remove I2C tuner */
1413 client = dev->i2c_client_tuner;
1415 module_put(client->dev.driver->owner);
1416 i2c_unregister_device(client);
1422 static int rtl28xxu_init(struct dvb_usb_device *d)
1427 dev_dbg(&d->intf->dev, "\n");
1429 /* init USB endpoints */
1430 ret = rtl28xxu_rd_reg(d, USB_SYSCTL_0, &val);
1434 /* enable DMA and Full Packet Mode*/
1436 ret = rtl28xxu_wr_reg(d, USB_SYSCTL_0, val);
1440 /* set EPA maximum packet size to 0x0200 */
1441 ret = rtl28xxu_wr_regs(d, USB_EPA_MAXPKT, "\x00\x02\x00\x00", 4);
1445 /* change EPA FIFO length */
1446 ret = rtl28xxu_wr_regs(d, USB_EPA_FIFO_CFG, "\x14\x00\x00\x00", 4);
1452 dev_dbg(&d->intf->dev, "failed=%d\n", ret);
1456 static int rtl2831u_power_ctrl(struct dvb_usb_device *d, int onoff)
1459 u8 gpio, sys0, epa_ctl[2];
1461 dev_dbg(&d->intf->dev, "onoff=%d\n", onoff);
1464 ret = rtl28xxu_rd_reg(d, SYS_SYS0, &sys0);
1468 /* tuner power, read GPIOs */
1469 ret = rtl28xxu_rd_reg(d, SYS_GPIO_OUT_VAL, &gpio);
1473 dev_dbg(&d->intf->dev, "RD SYS0=%02x GPIO_OUT_VAL=%02x\n", sys0, gpio);
1476 gpio |= 0x01; /* GPIO0 = 1 */
1477 gpio &= (~0x10); /* GPIO4 = 0 */
1478 gpio |= 0x04; /* GPIO2 = 1, LED on */
1481 epa_ctl[0] = 0x00; /* clear stall */
1482 epa_ctl[1] = 0x00; /* clear reset */
1484 gpio &= (~0x01); /* GPIO0 = 0 */
1485 gpio |= 0x10; /* GPIO4 = 1 */
1486 gpio &= (~0x04); /* GPIO2 = 1, LED off */
1487 sys0 = sys0 & (~0xc0);
1488 epa_ctl[0] = 0x10; /* set stall */
1489 epa_ctl[1] = 0x02; /* set reset */
1492 dev_dbg(&d->intf->dev, "WR SYS0=%02x GPIO_OUT_VAL=%02x\n", sys0, gpio);
1495 ret = rtl28xxu_wr_reg(d, SYS_SYS0, sys0);
1499 /* tuner power, write GPIOs */
1500 ret = rtl28xxu_wr_reg(d, SYS_GPIO_OUT_VAL, gpio);
1504 /* streaming EP: stall & reset */
1505 ret = rtl28xxu_wr_regs(d, USB_EPA_CTL, epa_ctl, 2);
1510 usb_clear_halt(d->udev, usb_rcvbulkpipe(d->udev, 0x81));
1514 dev_dbg(&d->intf->dev, "failed=%d\n", ret);
1518 static int rtl2832u_power_ctrl(struct dvb_usb_device *d, int onoff)
1522 dev_dbg(&d->intf->dev, "onoff=%d\n", onoff);
1525 /* GPIO3=1, GPIO4=0 */
1526 ret = rtl28xxu_wr_reg_mask(d, SYS_GPIO_OUT_VAL, 0x08, 0x18);
1531 ret = rtl28xxu_wr_reg_mask(d, SYS_DEMOD_CTL1, 0x00, 0x10);
1536 ret = rtl28xxu_wr_reg_mask(d, SYS_DEMOD_CTL, 0x80, 0x80);
1541 ret = rtl28xxu_wr_reg_mask(d, SYS_DEMOD_CTL, 0x20, 0x20);
1545 /* streaming EP: clear stall & reset */
1546 ret = rtl28xxu_wr_regs(d, USB_EPA_CTL, "\x00\x00", 2);
1550 ret = usb_clear_halt(d->udev, usb_rcvbulkpipe(d->udev, 0x81));
1555 ret = rtl28xxu_wr_reg_mask(d, SYS_GPIO_OUT_VAL, 0x10, 0x10);
1560 ret = rtl28xxu_wr_reg_mask(d, SYS_DEMOD_CTL, 0x00, 0x80);
1564 /* streaming EP: set stall & reset */
1565 ret = rtl28xxu_wr_regs(d, USB_EPA_CTL, "\x10\x02", 2);
1572 dev_dbg(&d->intf->dev, "failed=%d\n", ret);
1576 static int rtl28xxu_power_ctrl(struct dvb_usb_device *d, int onoff)
1578 struct rtl28xxu_dev *dev = d_to_priv(d);
1580 if (dev->chip_id == CHIP_ID_RTL2831U)
1581 return rtl2831u_power_ctrl(d, onoff);
1583 return rtl2832u_power_ctrl(d, onoff);
1586 static int rtl28xxu_frontend_ctrl(struct dvb_frontend *fe, int onoff)
1588 struct dvb_usb_device *d = fe_to_d(fe);
1589 struct rtl28xxu_dev *dev = fe_to_priv(fe);
1590 struct rtl2832_platform_data *pdata = &dev->rtl2832_platform_data;
1594 dev_dbg(&d->intf->dev, "fe=%d onoff=%d\n", fe->id, onoff);
1596 if (dev->chip_id == CHIP_ID_RTL2831U)
1600 /* control internal demod ADC */
1602 val = 0x48; /* enable ADC */
1604 val = 0x00; /* disable ADC */
1606 ret = rtl28xxu_wr_reg_mask(d, SYS_DEMOD_CTL, val, 0x48);
1609 } else if (fe->id == 1) {
1610 /* bypass slave demod TS through master demod */
1611 ret = pdata->slave_ts_ctrl(dev->i2c_client_demod, onoff);
1618 dev_dbg(&d->intf->dev, "failed=%d\n", ret);
1622 #if IS_ENABLED(CONFIG_RC_CORE)
1623 static int rtl2831u_rc_query(struct dvb_usb_device *d)
1626 struct rtl28xxu_dev *dev = d->priv;
1629 static const struct rtl28xxu_reg_val rc_nec_tab[] = {
1646 /* init remote controller */
1647 if (!dev->rc_active) {
1648 for (i = 0; i < ARRAY_SIZE(rc_nec_tab); i++) {
1649 ret = rtl28xxu_wr_reg(d, rc_nec_tab[i].reg,
1654 dev->rc_active = true;
1657 ret = rtl28xxu_rd_regs(d, SYS_IRRC_RP, buf, 5);
1661 if (buf[4] & 0x01) {
1662 enum rc_proto proto;
1664 if (buf[2] == (u8) ~buf[3]) {
1665 if (buf[0] == (u8) ~buf[1]) {
1666 /* NEC standard (16 bit) */
1667 rc_code = RC_SCANCODE_NEC(buf[0], buf[2]);
1668 proto = RC_PROTO_NEC;
1670 /* NEC extended (24 bit) */
1671 rc_code = RC_SCANCODE_NECX(buf[0] << 8 | buf[1],
1673 proto = RC_PROTO_NECX;
1676 /* NEC full (32 bit) */
1677 rc_code = RC_SCANCODE_NEC32(buf[0] << 24 | buf[1] << 16 |
1678 buf[2] << 8 | buf[3]);
1679 proto = RC_PROTO_NEC32;
1682 rc_keydown(d->rc_dev, proto, rc_code, 0);
1684 ret = rtl28xxu_wr_reg(d, SYS_IRRC_SR, 1);
1688 /* repeated intentionally to avoid extra keypress */
1689 ret = rtl28xxu_wr_reg(d, SYS_IRRC_SR, 1);
1696 dev_dbg(&d->intf->dev, "failed=%d\n", ret);
1700 static int rtl2831u_get_rc_config(struct dvb_usb_device *d,
1701 struct dvb_usb_rc *rc)
1703 rc->map_name = RC_MAP_EMPTY;
1704 rc->allowed_protos = RC_PROTO_BIT_NEC | RC_PROTO_BIT_NECX |
1706 rc->query = rtl2831u_rc_query;
1712 static int rtl2832u_rc_query(struct dvb_usb_device *d)
1715 struct rtl28xxu_dev *dev = d->priv;
1716 struct ir_raw_event ev = {};
1718 static const struct rtl28xxu_reg_val_mask refresh_tab[] = {
1719 {IR_RX_IF, 0x03, 0xff},
1720 {IR_RX_BUF_CTRL, 0x80, 0xff},
1721 {IR_RX_CTRL, 0x80, 0xff},
1724 /* init remote controller */
1725 if (!dev->rc_active) {
1726 static const struct rtl28xxu_reg_val_mask init_tab[] = {
1727 {SYS_DEMOD_CTL1, 0x00, 0x04},
1728 {SYS_DEMOD_CTL1, 0x00, 0x08},
1729 {USB_CTRL, 0x20, 0x20},
1730 {SYS_GPIO_DIR, 0x00, 0x08},
1731 {SYS_GPIO_OUT_EN, 0x08, 0x08},
1732 {SYS_GPIO_OUT_VAL, 0x08, 0x08},
1733 {IR_MAX_DURATION0, 0xd0, 0xff},
1734 {IR_MAX_DURATION1, 0x07, 0xff},
1735 {IR_IDLE_LEN0, 0xc0, 0xff},
1736 {IR_IDLE_LEN1, 0x00, 0xff},
1737 {IR_GLITCH_LEN, 0x03, 0xff},
1738 {IR_RX_CLK, 0x09, 0xff},
1739 {IR_RX_CFG, 0x1c, 0xff},
1740 {IR_MAX_H_TOL_LEN, 0x1e, 0xff},
1741 {IR_MAX_L_TOL_LEN, 0x1e, 0xff},
1742 {IR_RX_CTRL, 0x80, 0xff},
1745 for (i = 0; i < ARRAY_SIZE(init_tab); i++) {
1746 ret = rtl28xxu_wr_reg_mask(d, init_tab[i].reg,
1747 init_tab[i].val, init_tab[i].mask);
1752 dev->rc_active = true;
1755 ret = rtl28xxu_rd_reg(d, IR_RX_IF, &buf[0]);
1762 ret = rtl28xxu_rd_reg(d, IR_RX_BC, &buf[0]);
1763 if (ret || buf[0] > sizeof(buf))
1768 /* read raw code from hw */
1769 ret = rtl28xxu_rd_regs(d, IR_RX_BUF, buf, len);
1773 /* let hw receive new code */
1774 for (i = 0; i < ARRAY_SIZE(refresh_tab); i++) {
1775 ret = rtl28xxu_wr_reg_mask(d, refresh_tab[i].reg,
1776 refresh_tab[i].val, refresh_tab[i].mask);
1781 /* pass data to Kernel IR decoder */
1782 for (i = 0; i < len; i++) {
1783 ev.pulse = buf[i] >> 7;
1784 ev.duration = 51 * (buf[i] & 0x7f);
1785 ir_raw_event_store_with_filter(d->rc_dev, &ev);
1788 /* 'flush' ir_raw_event_store_with_filter() */
1789 ir_raw_event_handle(d->rc_dev);
1793 dev_dbg(&d->intf->dev, "failed=%d\n", ret);
1797 static int rtl2832u_get_rc_config(struct dvb_usb_device *d,
1798 struct dvb_usb_rc *rc)
1800 /* disable IR interrupts in order to avoid SDR sample loss */
1801 if (rtl28xxu_disable_rc)
1802 return rtl28xxu_wr_reg(d, IR_RX_IE, 0x00);
1804 /* load empty to enable rc */
1806 rc->map_name = RC_MAP_EMPTY;
1807 rc->allowed_protos = RC_PROTO_BIT_ALL_IR_DECODER;
1808 rc->driver_type = RC_DRIVER_IR_RAW;
1809 rc->query = rtl2832u_rc_query;
1811 /* we program idle len to 0xc0, set timeout to one less */
1812 rc->timeout = 0xbf * 51;
1817 static int rtl28xxu_get_rc_config(struct dvb_usb_device *d,
1818 struct dvb_usb_rc *rc)
1820 struct rtl28xxu_dev *dev = d_to_priv(d);
1822 if (dev->chip_id == CHIP_ID_RTL2831U)
1823 return rtl2831u_get_rc_config(d, rc);
1825 return rtl2832u_get_rc_config(d, rc);
1828 #define rtl28xxu_get_rc_config NULL
1831 static int rtl28xxu_pid_filter_ctrl(struct dvb_usb_adapter *adap, int onoff)
1833 struct rtl28xxu_dev *dev = adap_to_priv(adap);
1835 if (dev->chip_id == CHIP_ID_RTL2831U) {
1836 struct rtl2830_platform_data *pdata = &dev->rtl2830_platform_data;
1838 return pdata->pid_filter_ctrl(adap->fe[0], onoff);
1840 struct rtl2832_platform_data *pdata = &dev->rtl2832_platform_data;
1842 return pdata->pid_filter_ctrl(adap->fe[0], onoff);
1846 static int rtl28xxu_pid_filter(struct dvb_usb_adapter *adap, int index,
1849 struct rtl28xxu_dev *dev = adap_to_priv(adap);
1851 if (dev->chip_id == CHIP_ID_RTL2831U) {
1852 struct rtl2830_platform_data *pdata = &dev->rtl2830_platform_data;
1854 return pdata->pid_filter(adap->fe[0], index, pid, onoff);
1856 struct rtl2832_platform_data *pdata = &dev->rtl2832_platform_data;
1858 return pdata->pid_filter(adap->fe[0], index, pid, onoff);
1862 static const struct dvb_usb_device_properties rtl28xxu_props = {
1863 .driver_name = KBUILD_MODNAME,
1864 .owner = THIS_MODULE,
1865 .adapter_nr = adapter_nr,
1866 .size_of_priv = sizeof(struct rtl28xxu_dev),
1868 .identify_state = rtl28xxu_identify_state,
1869 .power_ctrl = rtl28xxu_power_ctrl,
1870 .frontend_ctrl = rtl28xxu_frontend_ctrl,
1871 .i2c_algo = &rtl28xxu_i2c_algo,
1872 .read_config = rtl28xxu_read_config,
1873 .frontend_attach = rtl28xxu_frontend_attach,
1874 .frontend_detach = rtl28xxu_frontend_detach,
1875 .tuner_attach = rtl28xxu_tuner_attach,
1876 .tuner_detach = rtl28xxu_tuner_detach,
1877 .init = rtl28xxu_init,
1878 .get_rc_config = rtl28xxu_get_rc_config,
1883 .caps = DVB_USB_ADAP_HAS_PID_FILTER |
1884 DVB_USB_ADAP_PID_FILTER_CAN_BE_TURNED_OFF,
1886 .pid_filter_count = 32,
1887 .pid_filter_ctrl = rtl28xxu_pid_filter_ctrl,
1888 .pid_filter = rtl28xxu_pid_filter,
1890 .stream = DVB_USB_STREAM_BULK(0x81, 6, 8 * 512),
1895 static const struct usb_device_id rtl28xxu_id_table[] = {
1896 /* RTL2831U devices: */
1897 { DVB_USB_DEVICE(USB_VID_REALTEK, USB_PID_REALTEK_RTL2831U,
1898 &rtl28xxu_props, "Realtek RTL2831U reference design", NULL) },
1899 { DVB_USB_DEVICE(USB_VID_WIDEVIEW, USB_PID_FREECOM_DVBT,
1900 &rtl28xxu_props, "Freecom USB2.0 DVB-T", NULL) },
1901 { DVB_USB_DEVICE(USB_VID_WIDEVIEW, USB_PID_FREECOM_DVBT_2,
1902 &rtl28xxu_props, "Freecom USB2.0 DVB-T", NULL) },
1904 /* RTL2832U devices: */
1905 { DVB_USB_DEVICE(USB_VID_REALTEK, 0x2832,
1906 &rtl28xxu_props, "Realtek RTL2832U reference design", NULL) },
1907 { DVB_USB_DEVICE(USB_VID_REALTEK, 0x2838,
1908 &rtl28xxu_props, "Realtek RTL2832U reference design", NULL) },
1909 { DVB_USB_DEVICE(USB_VID_TERRATEC, USB_PID_TERRATEC_CINERGY_T_STICK_BLACK_REV1,
1910 &rtl28xxu_props, "TerraTec Cinergy T Stick Black", RC_MAP_TERRATEC_SLIM) },
1911 { DVB_USB_DEVICE(USB_VID_GTEK, USB_PID_DELOCK_USB2_DVBT,
1912 &rtl28xxu_props, "G-Tek Electronics Group Lifeview LV5TDLX DVB-T", NULL) },
1913 { DVB_USB_DEVICE(USB_VID_TERRATEC, USB_PID_NOXON_DAB_STICK,
1914 &rtl28xxu_props, "TerraTec NOXON DAB Stick", NULL) },
1915 { DVB_USB_DEVICE(USB_VID_TERRATEC, USB_PID_NOXON_DAB_STICK_REV2,
1916 &rtl28xxu_props, "TerraTec NOXON DAB Stick (rev 2)", NULL) },
1917 { DVB_USB_DEVICE(USB_VID_TERRATEC, USB_PID_NOXON_DAB_STICK_REV3,
1918 &rtl28xxu_props, "TerraTec NOXON DAB Stick (rev 3)", NULL) },
1919 { DVB_USB_DEVICE(USB_VID_GTEK, USB_PID_TREKSTOR_TERRES_2_0,
1920 &rtl28xxu_props, "Trekstor DVB-T Stick Terres 2.0", NULL) },
1921 { DVB_USB_DEVICE(USB_VID_DEXATEK, 0x1101,
1922 &rtl28xxu_props, "Dexatek DK DVB-T Dongle", NULL) },
1923 { DVB_USB_DEVICE(USB_VID_LEADTEK, 0x6680,
1924 &rtl28xxu_props, "DigitalNow Quad DVB-T Receiver", NULL) },
1925 { DVB_USB_DEVICE(USB_VID_LEADTEK, USB_PID_WINFAST_DTV_DONGLE_MINID,
1926 &rtl28xxu_props, "Leadtek Winfast DTV Dongle Mini D", NULL) },
1927 { DVB_USB_DEVICE(USB_VID_LEADTEK, USB_PID_WINFAST_DTV2000DS_PLUS,
1928 &rtl28xxu_props, "Leadtek WinFast DTV2000DS Plus", RC_MAP_LEADTEK_Y04G0051) },
1929 { DVB_USB_DEVICE(USB_VID_TERRATEC, 0x00d3,
1930 &rtl28xxu_props, "TerraTec Cinergy T Stick RC (Rev. 3)", NULL) },
1931 { DVB_USB_DEVICE(USB_VID_DEXATEK, 0x1102,
1932 &rtl28xxu_props, "Dexatek DK mini DVB-T Dongle", NULL) },
1933 { DVB_USB_DEVICE(USB_VID_TERRATEC, 0x00d7,
1934 &rtl28xxu_props, "TerraTec Cinergy T Stick+", NULL) },
1935 { DVB_USB_DEVICE(USB_VID_KWORLD_2, 0xd3a8,
1936 &rtl28xxu_props, "ASUS My Cinema-U3100Mini Plus V2", NULL) },
1937 { DVB_USB_DEVICE(USB_VID_KWORLD_2, 0xd393,
1938 &rtl28xxu_props, "GIGABYTE U7300", NULL) },
1939 { DVB_USB_DEVICE(USB_VID_DEXATEK, 0x1104,
1940 &rtl28xxu_props, "MSI DIGIVOX Micro HD", NULL) },
1941 { DVB_USB_DEVICE(USB_VID_COMPRO, 0x0620,
1942 &rtl28xxu_props, "Compro VideoMate U620F", NULL) },
1943 { DVB_USB_DEVICE(USB_VID_COMPRO, 0x0650,
1944 &rtl28xxu_props, "Compro VideoMate U650F", NULL) },
1945 { DVB_USB_DEVICE(USB_VID_KWORLD_2, 0xd394,
1946 &rtl28xxu_props, "MaxMedia HU394-T", NULL) },
1947 { DVB_USB_DEVICE(USB_VID_LEADTEK, 0x6a03,
1948 &rtl28xxu_props, "Leadtek WinFast DTV Dongle mini", NULL) },
1949 { DVB_USB_DEVICE(USB_VID_GTEK, USB_PID_CPYTO_REDI_PC50A,
1950 &rtl28xxu_props, "Crypto ReDi PC 50 A", NULL) },
1951 { DVB_USB_DEVICE(USB_VID_KYE, 0x707f,
1952 &rtl28xxu_props, "Genius TVGo DVB-T03", NULL) },
1953 { DVB_USB_DEVICE(USB_VID_KWORLD_2, 0xd395,
1954 &rtl28xxu_props, "Peak DVB-T USB", NULL) },
1955 { DVB_USB_DEVICE(USB_VID_KWORLD_2, USB_PID_SVEON_STV20_RTL2832U,
1956 &rtl28xxu_props, "Sveon STV20", NULL) },
1957 { DVB_USB_DEVICE(USB_VID_KWORLD_2, USB_PID_SVEON_STV21,
1958 &rtl28xxu_props, "Sveon STV21", NULL) },
1959 { DVB_USB_DEVICE(USB_VID_KWORLD_2, USB_PID_SVEON_STV27,
1960 &rtl28xxu_props, "Sveon STV27", NULL) },
1961 { DVB_USB_DEVICE(USB_VID_KWORLD_2, USB_PID_TURBOX_DTT_2000,
1962 &rtl28xxu_props, "TURBO-X Pure TV Tuner DTT-2000", NULL) },
1963 { DVB_USB_DEVICE(USB_VID_GTEK, USB_PID_PROLECTRIX_DV107669,
1964 &rtl28xxu_props, "PROlectrix DV107669", NULL) },
1966 /* RTL2832P devices: */
1967 { DVB_USB_DEVICE(USB_VID_HANFTEK, 0x0131,
1968 &rtl28xxu_props, "Astrometa DVB-T2",
1969 RC_MAP_ASTROMETA_T2HYBRID) },
1970 { DVB_USB_DEVICE(0x5654, 0xca42,
1971 &rtl28xxu_props, "GoTView MasterHD 3", NULL) },
1974 MODULE_DEVICE_TABLE(usb, rtl28xxu_id_table);
1976 static struct usb_driver rtl28xxu_usb_driver = {
1977 .name = KBUILD_MODNAME,
1978 .id_table = rtl28xxu_id_table,
1979 .probe = dvb_usbv2_probe,
1980 .disconnect = dvb_usbv2_disconnect,
1981 .suspend = dvb_usbv2_suspend,
1982 .resume = dvb_usbv2_resume,
1983 .reset_resume = dvb_usbv2_reset_resume,
1988 module_usb_driver(rtl28xxu_usb_driver);
1990 MODULE_DESCRIPTION("Realtek RTL28xxU DVB USB driver");
1991 MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
1992 MODULE_AUTHOR("Thomas Mair <thomas.mair86@googlemail.com>");
1993 MODULE_LICENSE("GPL");