2 * Realtek RTL28xxU DVB USB driver
4 * Copyright (C) 2009 Antti Palosaari <crope@iki.fi>
5 * Copyright (C) 2011 Antti Palosaari <crope@iki.fi>
6 * Copyright (C) 2012 Thomas Mair <thomas.mair86@googlemail.com>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
38 DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
40 static int rtl28xxu_ctrl_msg(struct dvb_usb_device *d, struct rtl28xxu_req *req)
47 buf = kmalloc(req->size, GFP_KERNEL);
53 if (req->index & CMD_WR_FLAG) {
55 memcpy(buf, req->data, req->size);
56 requesttype = (USB_TYPE_VENDOR | USB_DIR_OUT);
57 pipe = usb_sndctrlpipe(d->udev, 0);
60 requesttype = (USB_TYPE_VENDOR | USB_DIR_IN);
61 pipe = usb_rcvctrlpipe(d->udev, 0);
64 ret = usb_control_msg(d->udev, pipe, 0, requesttype, req->value,
65 req->index, buf, req->size, 1000);
67 dvb_usb_dbg_usb_control_msg(d->udev, 0, requesttype, req->value,
68 req->index, buf, req->size);
73 /* read request, copy returned data to return buf */
74 if (!ret && requesttype == (USB_TYPE_VENDOR | USB_DIR_IN))
75 memcpy(req->data, buf, req->size);
84 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
88 static int rtl28xx_wr_regs(struct dvb_usb_device *d, u16 reg, u8 *val, int len)
90 struct rtl28xxu_req req;
93 req.index = CMD_USB_WR;
94 else if (reg < 0x4000)
95 req.index = CMD_SYS_WR;
97 req.index = CMD_IR_WR;
103 return rtl28xxu_ctrl_msg(d, &req);
106 static int rtl2831_rd_regs(struct dvb_usb_device *d, u16 reg, u8 *val, int len)
108 struct rtl28xxu_req req;
111 req.index = CMD_USB_RD;
112 else if (reg < 0x4000)
113 req.index = CMD_SYS_RD;
115 req.index = CMD_IR_RD;
121 return rtl28xxu_ctrl_msg(d, &req);
124 static int rtl28xx_wr_reg(struct dvb_usb_device *d, u16 reg, u8 val)
126 return rtl28xx_wr_regs(d, reg, &val, 1);
129 static int rtl28xx_rd_reg(struct dvb_usb_device *d, u16 reg, u8 *val)
131 return rtl2831_rd_regs(d, reg, val, 1);
134 static int rtl28xx_wr_reg_mask(struct dvb_usb_device *d, u16 reg, u8 val,
140 /* no need for read if whole reg is written */
142 ret = rtl28xx_rd_reg(d, reg, &tmp);
151 return rtl28xx_wr_reg(d, reg, val);
155 static int rtl28xxu_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg msg[],
159 struct dvb_usb_device *d = i2c_get_adapdata(adap);
160 struct rtl28xxu_priv *priv = d->priv;
161 struct rtl28xxu_req req;
164 * It is not known which are real I2C bus xfer limits, but testing
165 * with RTL2831U + MT2060 gives max RD 24 and max WR 22 bytes.
166 * TODO: find out RTL2832U lens
170 * I2C adapter logic looks rather complicated due to fact it handles
171 * three different access methods. Those methods are;
172 * 1) integrated demod access
176 * Used method is selected in order 1, 2, 3. Method 3 can handle all
177 * requests but there is two reasons why not use it always;
178 * 1) It is most expensive, usually two USB messages are needed
179 * 2) At least RTL2831U does not support it
181 * Method 3 is needed in case of I2C write+read (typical register read)
182 * where write is more than one byte.
185 if (mutex_lock_interruptible(&d->i2c_mutex) < 0)
188 if (num == 2 && !(msg[0].flags & I2C_M_RD) &&
189 (msg[1].flags & I2C_M_RD)) {
190 if (msg[0].len > 24 || msg[1].len > 24) {
191 /* TODO: check msg[0].len max */
193 goto err_mutex_unlock;
194 } else if (msg[0].addr == 0x10) {
195 /* method 1 - integrated demod */
196 req.value = (msg[0].buf[0] << 8) | (msg[0].addr << 1);
197 req.index = CMD_DEMOD_RD | priv->page;
198 req.size = msg[1].len;
199 req.data = &msg[1].buf[0];
200 ret = rtl28xxu_ctrl_msg(d, &req);
201 } else if (msg[0].len < 2) {
202 /* method 2 - old I2C */
203 req.value = (msg[0].buf[0] << 8) | (msg[0].addr << 1);
204 req.index = CMD_I2C_RD;
205 req.size = msg[1].len;
206 req.data = &msg[1].buf[0];
207 ret = rtl28xxu_ctrl_msg(d, &req);
209 /* method 3 - new I2C */
210 req.value = (msg[0].addr << 1);
211 req.index = CMD_I2C_DA_WR;
212 req.size = msg[0].len;
213 req.data = msg[0].buf;
214 ret = rtl28xxu_ctrl_msg(d, &req);
216 goto err_mutex_unlock;
218 req.value = (msg[0].addr << 1);
219 req.index = CMD_I2C_DA_RD;
220 req.size = msg[1].len;
221 req.data = msg[1].buf;
222 ret = rtl28xxu_ctrl_msg(d, &req);
224 } else if (num == 1 && !(msg[0].flags & I2C_M_RD)) {
225 if (msg[0].len > 22) {
226 /* TODO: check msg[0].len max */
228 goto err_mutex_unlock;
229 } else if (msg[0].addr == 0x10) {
230 /* method 1 - integrated demod */
231 if (msg[0].buf[0] == 0x00) {
232 /* save demod page for later demod access */
233 priv->page = msg[0].buf[1];
236 req.value = (msg[0].buf[0] << 8) |
238 req.index = CMD_DEMOD_WR | priv->page;
239 req.size = msg[0].len-1;
240 req.data = &msg[0].buf[1];
241 ret = rtl28xxu_ctrl_msg(d, &req);
243 } else if (msg[0].len < 23) {
244 /* method 2 - old I2C */
245 req.value = (msg[0].buf[0] << 8) | (msg[0].addr << 1);
246 req.index = CMD_I2C_WR;
247 req.size = msg[0].len-1;
248 req.data = &msg[0].buf[1];
249 ret = rtl28xxu_ctrl_msg(d, &req);
251 /* method 3 - new I2C */
252 req.value = (msg[0].addr << 1);
253 req.index = CMD_I2C_DA_WR;
254 req.size = msg[0].len;
255 req.data = msg[0].buf;
256 ret = rtl28xxu_ctrl_msg(d, &req);
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_priv *priv = 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->udev->dev, "%s:\n", __func__);
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 = rtl28xx_wr_reg(d, SYS_GPIO_DIR, 0x0a);
304 /* enable as output GPIO0, GPIO2, GPIO4 */
305 ret = rtl28xx_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 priv->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 priv->tuner = TUNER_RTL2830_QT1010;
328 priv->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 priv->tuner = TUNER_RTL2830_MT2060;
341 priv->tuner_name = "MT2060";
345 /* assume MXL5005S */
346 priv->tuner = TUNER_RTL2830_MXL5005S;
347 priv->tuner_name = "MXL5005S";
351 dev_dbg(&d->udev->dev, "%s: tuner=%s\n", __func__, priv->tuner_name);
355 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
359 static int rtl2832u_read_config(struct dvb_usb_device *d)
361 struct rtl28xxu_priv *priv = 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};
381 dev_dbg(&d->udev->dev, "%s:\n", __func__);
383 /* enable GPIO3 and GPIO6 as output */
384 ret = rtl28xx_wr_reg_mask(d, SYS_GPIO_DIR, 0x00, 0x40);
388 ret = rtl28xx_wr_reg_mask(d, SYS_GPIO_OUT_EN, 0x48, 0x48);
393 * Probe used tuner. We need to know used tuner before demod attach
394 * since there is some demod params needed to set according to tuner.
397 /* open demod I2C gate */
398 ret = rtl28xxu_ctrl_msg(d, &req_gate_open);
402 priv->tuner_name = "NONE";
404 /* check FC0012 ID register; reg=00 val=a1 */
405 ret = rtl28xxu_ctrl_msg(d, &req_fc0012);
406 if (ret == 0 && buf[0] == 0xa1) {
407 priv->tuner = TUNER_RTL2832_FC0012;
408 priv->tuner_name = "FC0012";
412 /* check FC0013 ID register; reg=00 val=a3 */
413 ret = rtl28xxu_ctrl_msg(d, &req_fc0013);
414 if (ret == 0 && buf[0] == 0xa3) {
415 priv->tuner = TUNER_RTL2832_FC0013;
416 priv->tuner_name = "FC0013";
420 /* check MT2266 ID register; reg=00 val=85 */
421 ret = rtl28xxu_ctrl_msg(d, &req_mt2266);
422 if (ret == 0 && buf[0] == 0x85) {
423 priv->tuner = TUNER_RTL2832_MT2266;
424 priv->tuner_name = "MT2266";
428 /* check FC2580 ID register; reg=01 val=56 */
429 ret = rtl28xxu_ctrl_msg(d, &req_fc2580);
430 if (ret == 0 && buf[0] == 0x56) {
431 priv->tuner = TUNER_RTL2832_FC2580;
432 priv->tuner_name = "FC2580";
436 /* check MT2063 ID register; reg=00 val=9e || 9c */
437 ret = rtl28xxu_ctrl_msg(d, &req_mt2063);
438 if (ret == 0 && (buf[0] == 0x9e || buf[0] == 0x9c)) {
439 priv->tuner = TUNER_RTL2832_MT2063;
440 priv->tuner_name = "MT2063";
444 /* check MAX3543 ID register; reg=00 val=38 */
445 ret = rtl28xxu_ctrl_msg(d, &req_max3543);
446 if (ret == 0 && buf[0] == 0x38) {
447 priv->tuner = TUNER_RTL2832_MAX3543;
448 priv->tuner_name = "MAX3543";
452 /* check TUA9001 ID register; reg=7e val=2328 */
453 ret = rtl28xxu_ctrl_msg(d, &req_tua9001);
454 if (ret == 0 && buf[0] == 0x23 && buf[1] == 0x28) {
455 priv->tuner = TUNER_RTL2832_TUA9001;
456 priv->tuner_name = "TUA9001";
460 /* check MXL5007R ID register; reg=d9 val=14 */
461 ret = rtl28xxu_ctrl_msg(d, &req_mxl5007t);
462 if (ret == 0 && buf[0] == 0x14) {
463 priv->tuner = TUNER_RTL2832_MXL5007T;
464 priv->tuner_name = "MXL5007T";
468 /* check E4000 ID register; reg=02 val=40 */
469 ret = rtl28xxu_ctrl_msg(d, &req_e4000);
470 if (ret == 0 && buf[0] == 0x40) {
471 priv->tuner = TUNER_RTL2832_E4000;
472 priv->tuner_name = "E4000";
476 /* check TDA18272 ID register; reg=00 val=c760 */
477 ret = rtl28xxu_ctrl_msg(d, &req_tda18272);
478 if (ret == 0 && (buf[0] == 0xc7 || buf[1] == 0x60)) {
479 priv->tuner = TUNER_RTL2832_TDA18272;
480 priv->tuner_name = "TDA18272";
484 /* check R820T ID register; reg=00 val=69 */
485 ret = rtl28xxu_ctrl_msg(d, &req_r820t);
486 if (ret == 0 && buf[0] == 0x69) {
487 priv->tuner = TUNER_RTL2832_R820T;
488 priv->tuner_name = "R820T";
493 dev_dbg(&d->udev->dev, "%s: tuner=%s\n", __func__, priv->tuner_name);
495 /* close demod I2C gate */
496 ret = rtl28xxu_ctrl_msg(d, &req_gate_close);
502 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
506 static struct rtl2830_config rtl28xxu_rtl2830_mt2060_config = {
507 .i2c_addr = 0x10, /* 0x20 */
513 .agc_targ_val = 0x2d,
517 static struct rtl2830_config rtl28xxu_rtl2830_qt1010_config = {
518 .i2c_addr = 0x10, /* 0x20 */
524 .agc_targ_val = 0x2d,
527 static struct rtl2830_config rtl28xxu_rtl2830_mxl5005s_config = {
528 .i2c_addr = 0x10, /* 0x20 */
534 .agc_targ_val = 0x3e,
537 static int rtl2831u_frontend_attach(struct dvb_usb_adapter *adap)
539 struct dvb_usb_device *d = adap_to_d(adap);
540 struct rtl28xxu_priv *priv = d_to_priv(d);
541 struct rtl2830_config *rtl2830_config;
544 dev_dbg(&d->udev->dev, "%s:\n", __func__);
546 switch (priv->tuner) {
547 case TUNER_RTL2830_QT1010:
548 rtl2830_config = &rtl28xxu_rtl2830_qt1010_config;
550 case TUNER_RTL2830_MT2060:
551 rtl2830_config = &rtl28xxu_rtl2830_mt2060_config;
553 case TUNER_RTL2830_MXL5005S:
554 rtl2830_config = &rtl28xxu_rtl2830_mxl5005s_config;
557 dev_err(&d->udev->dev, "%s: unknown tuner=%s\n",
558 KBUILD_MODNAME, priv->tuner_name);
563 /* attach demodulator */
564 adap->fe[0] = dvb_attach(rtl2830_attach, rtl2830_config, &d->i2c_adap);
572 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
576 static struct rtl2832_config rtl28xxu_rtl2832_fc0012_config = {
577 .i2c_addr = 0x10, /* 0x20 */
580 .tuner = TUNER_RTL2832_FC0012
583 static struct rtl2832_config rtl28xxu_rtl2832_fc0013_config = {
584 .i2c_addr = 0x10, /* 0x20 */
587 .tuner = TUNER_RTL2832_FC0013
590 static struct rtl2832_config rtl28xxu_rtl2832_tua9001_config = {
591 .i2c_addr = 0x10, /* 0x20 */
593 .tuner = TUNER_RTL2832_TUA9001,
596 static struct rtl2832_config rtl28xxu_rtl2832_e4000_config = {
597 .i2c_addr = 0x10, /* 0x20 */
599 .tuner = TUNER_RTL2832_E4000,
602 static struct rtl2832_config rtl28xxu_rtl2832_r820t_config = {
605 .tuner = TUNER_RTL2832_R820T,
608 static int rtl2832u_fc0012_tuner_callback(struct dvb_usb_device *d,
614 dev_dbg(&d->udev->dev, "%s: cmd=%d arg=%d\n", __func__, cmd, arg);
617 case FC_FE_CALLBACK_VHF_ENABLE:
618 /* set output values */
619 ret = rtl28xx_rd_reg(d, SYS_GPIO_OUT_VAL, &val);
624 val &= 0xbf; /* set GPIO6 low */
626 val |= 0x40; /* set GPIO6 high */
629 ret = rtl28xx_wr_reg(d, SYS_GPIO_OUT_VAL, val);
639 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
643 static int rtl2832u_tua9001_tuner_callback(struct dvb_usb_device *d,
649 dev_dbg(&d->udev->dev, "%s: cmd=%d arg=%d\n", __func__, cmd, arg);
652 * CEN always enabled by hardware wiring
658 case TUA9001_CMD_RESETN:
664 ret = rtl28xx_wr_reg_mask(d, SYS_GPIO_OUT_VAL, val, 0x10);
668 case TUA9001_CMD_RXEN:
674 ret = rtl28xx_wr_reg_mask(d, SYS_GPIO_OUT_VAL, val, 0x02);
682 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
686 static int rtl2832u_tuner_callback(struct dvb_usb_device *d, int cmd, int arg)
688 struct rtl28xxu_priv *priv = d->priv;
690 switch (priv->tuner) {
691 case TUNER_RTL2832_FC0012:
692 return rtl2832u_fc0012_tuner_callback(d, cmd, arg);
693 case TUNER_RTL2832_TUA9001:
694 return rtl2832u_tua9001_tuner_callback(d, cmd, arg);
702 static int rtl2832u_frontend_callback(void *adapter_priv, int component,
705 struct i2c_adapter *adap = adapter_priv;
706 struct dvb_usb_device *d = i2c_get_adapdata(adap);
708 dev_dbg(&d->udev->dev, "%s: component=%d cmd=%d arg=%d\n",
709 __func__, component, cmd, arg);
712 case DVB_FRONTEND_COMPONENT_TUNER:
713 return rtl2832u_tuner_callback(d, cmd, arg);
721 static int rtl2832u_frontend_attach(struct dvb_usb_adapter *adap)
724 struct dvb_usb_device *d = adap_to_d(adap);
725 struct rtl28xxu_priv *priv = d_to_priv(d);
726 struct rtl2832_config *rtl2832_config;
728 dev_dbg(&d->udev->dev, "%s:\n", __func__);
730 switch (priv->tuner) {
731 case TUNER_RTL2832_FC0012:
732 rtl2832_config = &rtl28xxu_rtl2832_fc0012_config;
734 case TUNER_RTL2832_FC0013:
735 rtl2832_config = &rtl28xxu_rtl2832_fc0013_config;
737 case TUNER_RTL2832_FC2580:
738 /* FIXME: do not abuse fc0012 settings */
739 rtl2832_config = &rtl28xxu_rtl2832_fc0012_config;
741 case TUNER_RTL2832_TUA9001:
742 rtl2832_config = &rtl28xxu_rtl2832_tua9001_config;
744 case TUNER_RTL2832_E4000:
745 rtl2832_config = &rtl28xxu_rtl2832_e4000_config;
747 case TUNER_RTL2832_R820T:
748 rtl2832_config = &rtl28xxu_rtl2832_r820t_config;
751 dev_err(&d->udev->dev, "%s: unknown tuner=%s\n",
752 KBUILD_MODNAME, priv->tuner_name);
757 /* attach demodulator */
758 adap->fe[0] = dvb_attach(rtl2832_attach, rtl2832_config, &d->i2c_adap);
764 /* set fe callback */
765 adap->fe[0]->callback = rtl2832u_frontend_callback;
769 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
773 static struct qt1010_config rtl28xxu_qt1010_config = {
774 .i2c_address = 0x62, /* 0xc4 */
777 static struct mt2060_config rtl28xxu_mt2060_config = {
778 .i2c_address = 0x60, /* 0xc0 */
782 static struct mxl5005s_config rtl28xxu_mxl5005s_config = {
783 .i2c_address = 0x63, /* 0xc6 */
784 .if_freq = IF_FREQ_4570000HZ,
785 .xtal_freq = CRYSTAL_FREQ_16000000HZ,
786 .agc_mode = MXL_SINGLE_AGC,
787 .tracking_filter = MXL_TF_C_H,
788 .rssi_enable = MXL_RSSI_ENABLE,
789 .cap_select = MXL_CAP_SEL_ENABLE,
790 .div_out = MXL_DIV_OUT_4,
791 .clock_out = MXL_CLOCK_OUT_DISABLE,
792 .output_load = MXL5005S_IF_OUTPUT_LOAD_200_OHM,
793 .top = MXL5005S_TOP_25P2,
794 .mod_mode = MXL_DIGITAL_MODE,
795 .if_mode = MXL_ZERO_IF,
796 .AgcMasterByte = 0x00,
799 static int rtl2831u_tuner_attach(struct dvb_usb_adapter *adap)
802 struct dvb_usb_device *d = adap_to_d(adap);
803 struct rtl28xxu_priv *priv = d_to_priv(d);
804 struct i2c_adapter *rtl2830_tuner_i2c;
805 struct dvb_frontend *fe;
807 dev_dbg(&d->udev->dev, "%s:\n", __func__);
809 /* use rtl2830 driver I2C adapter, for more info see rtl2830 driver */
810 rtl2830_tuner_i2c = rtl2830_get_tuner_i2c_adapter(adap->fe[0]);
812 switch (priv->tuner) {
813 case TUNER_RTL2830_QT1010:
814 fe = dvb_attach(qt1010_attach, adap->fe[0],
815 rtl2830_tuner_i2c, &rtl28xxu_qt1010_config);
817 case TUNER_RTL2830_MT2060:
818 fe = dvb_attach(mt2060_attach, adap->fe[0],
819 rtl2830_tuner_i2c, &rtl28xxu_mt2060_config,
822 case TUNER_RTL2830_MXL5005S:
823 fe = dvb_attach(mxl5005s_attach, adap->fe[0],
824 rtl2830_tuner_i2c, &rtl28xxu_mxl5005s_config);
828 dev_err(&d->udev->dev, "%s: unknown tuner=%d\n", KBUILD_MODNAME,
839 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
843 static const struct e4000_config rtl2832u_e4000_config = {
848 static const struct fc2580_config rtl2832u_fc2580_config = {
853 static struct tua9001_config rtl2832u_tua9001_config = {
857 static const struct fc0012_config rtl2832u_fc0012_config = {
858 .i2c_address = 0x63, /* 0xc6 >> 1 */
859 .xtal_freq = FC_XTAL_28_8_MHZ,
862 static const struct r820t_config rtl2832u_r820t_config = {
865 .max_i2c_msg_len = 2,
866 .rafael_chip = CHIP_R820T,
869 static int rtl2832u_tuner_attach(struct dvb_usb_adapter *adap)
872 struct dvb_usb_device *d = adap_to_d(adap);
873 struct rtl28xxu_priv *priv = d_to_priv(d);
874 struct dvb_frontend *fe;
876 dev_dbg(&d->udev->dev, "%s:\n", __func__);
878 switch (priv->tuner) {
879 case TUNER_RTL2832_FC0012:
880 fe = dvb_attach(fc0012_attach, adap->fe[0],
881 &d->i2c_adap, &rtl2832u_fc0012_config);
883 /* since fc0012 includs reading the signal strength delegate
884 * that to the tuner driver */
885 adap->fe[0]->ops.read_signal_strength =
886 adap->fe[0]->ops.tuner_ops.get_rf_strength;
889 case TUNER_RTL2832_FC0013:
890 fe = dvb_attach(fc0013_attach, adap->fe[0],
891 &d->i2c_adap, 0xc6>>1, 0, FC_XTAL_28_8_MHZ);
893 /* fc0013 also supports signal strength reading */
894 adap->fe[0]->ops.read_signal_strength =
895 adap->fe[0]->ops.tuner_ops.get_rf_strength;
897 case TUNER_RTL2832_E4000:
898 fe = dvb_attach(e4000_attach, adap->fe[0], &d->i2c_adap,
899 &rtl2832u_e4000_config);
901 case TUNER_RTL2832_FC2580:
902 fe = dvb_attach(fc2580_attach, adap->fe[0], &d->i2c_adap,
903 &rtl2832u_fc2580_config);
905 case TUNER_RTL2832_TUA9001:
906 /* enable GPIO1 and GPIO4 as output */
907 ret = rtl28xx_wr_reg_mask(d, SYS_GPIO_DIR, 0x00, 0x12);
911 ret = rtl28xx_wr_reg_mask(d, SYS_GPIO_OUT_EN, 0x12, 0x12);
915 fe = dvb_attach(tua9001_attach, adap->fe[0], &d->i2c_adap,
916 &rtl2832u_tua9001_config);
918 case TUNER_RTL2832_R820T:
919 fe = dvb_attach(r820t_attach, adap->fe[0], &d->i2c_adap,
920 &rtl2832u_r820t_config);
922 /* Use tuner to get the signal strength */
923 adap->fe[0]->ops.read_signal_strength =
924 adap->fe[0]->ops.tuner_ops.get_rf_strength;
928 dev_err(&d->udev->dev, "%s: unknown tuner=%d\n", KBUILD_MODNAME,
939 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
943 static int rtl28xxu_init(struct dvb_usb_device *d)
948 dev_dbg(&d->udev->dev, "%s:\n", __func__);
950 /* init USB endpoints */
951 ret = rtl28xx_rd_reg(d, USB_SYSCTL_0, &val);
955 /* enable DMA and Full Packet Mode*/
957 ret = rtl28xx_wr_reg(d, USB_SYSCTL_0, val);
961 /* set EPA maximum packet size to 0x0200 */
962 ret = rtl28xx_wr_regs(d, USB_EPA_MAXPKT, "\x00\x02\x00\x00", 4);
966 /* change EPA FIFO length */
967 ret = rtl28xx_wr_regs(d, USB_EPA_FIFO_CFG, "\x14\x00\x00\x00", 4);
973 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
977 static int rtl2831u_power_ctrl(struct dvb_usb_device *d, int onoff)
980 u8 gpio, sys0, epa_ctl[2];
982 dev_dbg(&d->udev->dev, "%s: onoff=%d\n", __func__, onoff);
985 ret = rtl28xx_rd_reg(d, SYS_SYS0, &sys0);
989 /* tuner power, read GPIOs */
990 ret = rtl28xx_rd_reg(d, SYS_GPIO_OUT_VAL, &gpio);
994 dev_dbg(&d->udev->dev, "%s: RD SYS0=%02x GPIO_OUT_VAL=%02x\n", __func__,
998 gpio |= 0x01; /* GPIO0 = 1 */
999 gpio &= (~0x10); /* GPIO4 = 0 */
1000 gpio |= 0x04; /* GPIO2 = 1, LED on */
1003 epa_ctl[0] = 0x00; /* clear stall */
1004 epa_ctl[1] = 0x00; /* clear reset */
1006 gpio &= (~0x01); /* GPIO0 = 0 */
1007 gpio |= 0x10; /* GPIO4 = 1 */
1008 gpio &= (~0x04); /* GPIO2 = 1, LED off */
1009 sys0 = sys0 & (~0xc0);
1010 epa_ctl[0] = 0x10; /* set stall */
1011 epa_ctl[1] = 0x02; /* set reset */
1014 dev_dbg(&d->udev->dev, "%s: WR SYS0=%02x GPIO_OUT_VAL=%02x\n", __func__,
1018 ret = rtl28xx_wr_reg(d, SYS_SYS0, sys0);
1022 /* tuner power, write GPIOs */
1023 ret = rtl28xx_wr_reg(d, SYS_GPIO_OUT_VAL, gpio);
1027 /* streaming EP: stall & reset */
1028 ret = rtl28xx_wr_regs(d, USB_EPA_CTL, epa_ctl, 2);
1033 usb_clear_halt(d->udev, usb_rcvbulkpipe(d->udev, 0x81));
1037 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
1041 static int rtl2832u_power_ctrl(struct dvb_usb_device *d, int onoff)
1045 dev_dbg(&d->udev->dev, "%s: onoff=%d\n", __func__, onoff);
1048 /* GPIO3=1, GPIO4=0 */
1049 ret = rtl28xx_wr_reg_mask(d, SYS_GPIO_OUT_VAL, 0x08, 0x18);
1054 ret = rtl28xx_wr_reg_mask(d, SYS_DEMOD_CTL1, 0x00, 0x10);
1059 ret = rtl28xx_wr_reg_mask(d, SYS_DEMOD_CTL, 0x80, 0x80);
1064 ret = rtl28xx_wr_reg_mask(d, SYS_DEMOD_CTL, 0x20, 0x20);
1071 ret = rtl28xx_wr_reg_mask(d, SYS_DEMOD_CTL, 0x48, 0x48);
1075 /* streaming EP: clear stall & reset */
1076 ret = rtl28xx_wr_regs(d, USB_EPA_CTL, "\x00\x00", 2);
1080 ret = usb_clear_halt(d->udev, usb_rcvbulkpipe(d->udev, 0x81));
1085 ret = rtl28xx_wr_reg_mask(d, SYS_GPIO_OUT_VAL, 0x10, 0x10);
1090 ret = rtl28xx_wr_reg_mask(d, SYS_DEMOD_CTL, 0x00, 0x48);
1095 ret = rtl28xx_wr_reg_mask(d, SYS_DEMOD_CTL, 0x00, 0x80);
1099 /* streaming EP: set stall & reset */
1100 ret = rtl28xx_wr_regs(d, USB_EPA_CTL, "\x10\x02", 2);
1107 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
1111 #if IS_ENABLED(CONFIG_RC_CORE)
1112 static int rtl2831u_rc_query(struct dvb_usb_device *d)
1115 struct rtl28xxu_priv *priv = d->priv;
1118 struct rtl28xxu_reg_val rc_nec_tab[] = {
1135 /* init remote controller */
1136 if (!priv->rc_active) {
1137 for (i = 0; i < ARRAY_SIZE(rc_nec_tab); i++) {
1138 ret = rtl28xx_wr_reg(d, rc_nec_tab[i].reg,
1143 priv->rc_active = true;
1146 ret = rtl2831_rd_regs(d, SYS_IRRC_RP, buf, 5);
1150 if (buf[4] & 0x01) {
1151 if (buf[2] == (u8) ~buf[3]) {
1152 if (buf[0] == (u8) ~buf[1]) {
1153 /* NEC standard (16 bit) */
1154 rc_code = buf[0] << 8 | buf[2];
1156 /* NEC extended (24 bit) */
1157 rc_code = buf[0] << 16 |
1158 buf[1] << 8 | buf[2];
1161 /* NEC full (32 bit) */
1162 rc_code = buf[0] << 24 | buf[1] << 16 |
1163 buf[2] << 8 | buf[3];
1166 rc_keydown(d->rc_dev, rc_code, 0);
1168 ret = rtl28xx_wr_reg(d, SYS_IRRC_SR, 1);
1172 /* repeated intentionally to avoid extra keypress */
1173 ret = rtl28xx_wr_reg(d, SYS_IRRC_SR, 1);
1180 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
1184 static int rtl2831u_get_rc_config(struct dvb_usb_device *d,
1185 struct dvb_usb_rc *rc)
1187 rc->map_name = RC_MAP_EMPTY;
1188 rc->allowed_protos = RC_BIT_NEC;
1189 rc->query = rtl2831u_rc_query;
1195 static int rtl2832u_rc_query(struct dvb_usb_device *d)
1198 struct rtl28xxu_priv *priv = d->priv;
1199 struct ir_raw_event ev;
1201 static const struct rtl28xxu_reg_val_mask refresh_tab[] = {
1202 {IR_RX_IF, 0x03, 0xff},
1203 {IR_RX_BUF_CTRL, 0x80, 0xff},
1204 {IR_RX_CTRL, 0x80, 0xff},
1207 /* init remote controller */
1208 if (!priv->rc_active) {
1209 static const struct rtl28xxu_reg_val_mask init_tab[] = {
1210 {SYS_DEMOD_CTL1, 0x00, 0x04},
1211 {SYS_DEMOD_CTL1, 0x00, 0x08},
1212 {USB_CTRL, 0x20, 0x20},
1213 {SYS_GPIO_DIR, 0x00, 0x08},
1214 {SYS_GPIO_OUT_EN, 0x08, 0x08},
1215 {SYS_GPIO_OUT_VAL, 0x08, 0x08},
1216 {IR_MAX_DURATION0, 0xd0, 0xff},
1217 {IR_MAX_DURATION1, 0x07, 0xff},
1218 {IR_IDLE_LEN0, 0xc0, 0xff},
1219 {IR_IDLE_LEN1, 0x00, 0xff},
1220 {IR_GLITCH_LEN, 0x03, 0xff},
1221 {IR_RX_CLK, 0x09, 0xff},
1222 {IR_RX_CFG, 0x1c, 0xff},
1223 {IR_MAX_H_TOL_LEN, 0x1e, 0xff},
1224 {IR_MAX_L_TOL_LEN, 0x1e, 0xff},
1225 {IR_RX_CTRL, 0x80, 0xff},
1228 for (i = 0; i < ARRAY_SIZE(init_tab); i++) {
1229 ret = rtl28xx_wr_reg_mask(d, init_tab[i].reg,
1230 init_tab[i].val, init_tab[i].mask);
1235 priv->rc_active = true;
1238 ret = rtl28xx_rd_reg(d, IR_RX_IF, &buf[0]);
1245 ret = rtl28xx_rd_reg(d, IR_RX_BC, &buf[0]);
1251 /* read raw code from hw */
1252 ret = rtl2831_rd_regs(d, IR_RX_BUF, buf, len);
1256 /* let hw receive new code */
1257 for (i = 0; i < ARRAY_SIZE(refresh_tab); i++) {
1258 ret = rtl28xx_wr_reg_mask(d, refresh_tab[i].reg,
1259 refresh_tab[i].val, refresh_tab[i].mask);
1264 /* pass data to Kernel IR decoder */
1265 init_ir_raw_event(&ev);
1267 for (i = 0; i < len; i++) {
1268 ev.pulse = buf[i] >> 7;
1269 ev.duration = 50800 * (buf[i] & 0x7f);
1270 ir_raw_event_store_with_filter(d->rc_dev, &ev);
1273 /* 'flush' ir_raw_event_store_with_filter() */
1274 ir_raw_event_set_idle(d->rc_dev, true);
1275 ir_raw_event_handle(d->rc_dev);
1279 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
1283 static int rtl2832u_get_rc_config(struct dvb_usb_device *d,
1284 struct dvb_usb_rc *rc)
1286 /* load empty to enable rc */
1288 rc->map_name = RC_MAP_EMPTY;
1289 rc->allowed_protos = RC_BIT_ALL;
1290 rc->driver_type = RC_DRIVER_IR_RAW;
1291 rc->query = rtl2832u_rc_query;
1297 #define rtl2831u_get_rc_config NULL
1298 #define rtl2832u_get_rc_config NULL
1301 static const struct dvb_usb_device_properties rtl2831u_props = {
1302 .driver_name = KBUILD_MODNAME,
1303 .owner = THIS_MODULE,
1304 .adapter_nr = adapter_nr,
1305 .size_of_priv = sizeof(struct rtl28xxu_priv),
1307 .power_ctrl = rtl2831u_power_ctrl,
1308 .i2c_algo = &rtl28xxu_i2c_algo,
1309 .read_config = rtl2831u_read_config,
1310 .frontend_attach = rtl2831u_frontend_attach,
1311 .tuner_attach = rtl2831u_tuner_attach,
1312 .init = rtl28xxu_init,
1313 .get_rc_config = rtl2831u_get_rc_config,
1318 .stream = DVB_USB_STREAM_BULK(0x81, 6, 8 * 512),
1323 static const struct dvb_usb_device_properties rtl2832u_props = {
1324 .driver_name = KBUILD_MODNAME,
1325 .owner = THIS_MODULE,
1326 .adapter_nr = adapter_nr,
1327 .size_of_priv = sizeof(struct rtl28xxu_priv),
1329 .power_ctrl = rtl2832u_power_ctrl,
1330 .i2c_algo = &rtl28xxu_i2c_algo,
1331 .read_config = rtl2832u_read_config,
1332 .frontend_attach = rtl2832u_frontend_attach,
1333 .tuner_attach = rtl2832u_tuner_attach,
1334 .init = rtl28xxu_init,
1335 .get_rc_config = rtl2832u_get_rc_config,
1340 .stream = DVB_USB_STREAM_BULK(0x81, 6, 8 * 512),
1345 static const struct usb_device_id rtl28xxu_id_table[] = {
1346 { DVB_USB_DEVICE(USB_VID_REALTEK, USB_PID_REALTEK_RTL2831U,
1347 &rtl2831u_props, "Realtek RTL2831U reference design", NULL) },
1348 { DVB_USB_DEVICE(USB_VID_WIDEVIEW, USB_PID_FREECOM_DVBT,
1349 &rtl2831u_props, "Freecom USB2.0 DVB-T", NULL) },
1350 { DVB_USB_DEVICE(USB_VID_WIDEVIEW, USB_PID_FREECOM_DVBT_2,
1351 &rtl2831u_props, "Freecom USB2.0 DVB-T", NULL) },
1353 { DVB_USB_DEVICE(USB_VID_REALTEK, 0x2832,
1354 &rtl2832u_props, "Realtek RTL2832U reference design", NULL) },
1355 { DVB_USB_DEVICE(USB_VID_REALTEK, 0x2838,
1356 &rtl2832u_props, "Realtek RTL2832U reference design", NULL) },
1357 { DVB_USB_DEVICE(USB_VID_TERRATEC, USB_PID_TERRATEC_CINERGY_T_STICK_BLACK_REV1,
1358 &rtl2832u_props, "TerraTec Cinergy T Stick Black", RC_MAP_TERRATEC_SLIM) },
1359 { DVB_USB_DEVICE(USB_VID_GTEK, USB_PID_DELOCK_USB2_DVBT,
1360 &rtl2832u_props, "G-Tek Electronics Group Lifeview LV5TDLX DVB-T", NULL) },
1361 { DVB_USB_DEVICE(USB_VID_TERRATEC, USB_PID_NOXON_DAB_STICK,
1362 &rtl2832u_props, "TerraTec NOXON DAB Stick", NULL) },
1363 { DVB_USB_DEVICE(USB_VID_TERRATEC, USB_PID_NOXON_DAB_STICK_REV2,
1364 &rtl2832u_props, "TerraTec NOXON DAB Stick (rev 2)", NULL) },
1365 { DVB_USB_DEVICE(USB_VID_GTEK, USB_PID_TREKSTOR_TERRES_2_0,
1366 &rtl2832u_props, "Trekstor DVB-T Stick Terres 2.0", NULL) },
1367 { DVB_USB_DEVICE(USB_VID_DEXATEK, 0x1101,
1368 &rtl2832u_props, "Dexatek DK DVB-T Dongle", NULL) },
1369 { DVB_USB_DEVICE(USB_VID_LEADTEK, 0x6680,
1370 &rtl2832u_props, "DigitalNow Quad DVB-T Receiver", NULL) },
1371 { DVB_USB_DEVICE(USB_VID_TERRATEC, 0x00d3,
1372 &rtl2832u_props, "TerraTec Cinergy T Stick RC (Rev. 3)", NULL) },
1373 { DVB_USB_DEVICE(USB_VID_DEXATEK, 0x1102,
1374 &rtl2832u_props, "Dexatek DK mini DVB-T Dongle", NULL) },
1375 { DVB_USB_DEVICE(USB_VID_TERRATEC, 0x00d7,
1376 &rtl2832u_props, "TerraTec Cinergy T Stick+", NULL) },
1377 { DVB_USB_DEVICE(USB_VID_KWORLD_2, 0xd3a8,
1378 &rtl2832u_props, "ASUS My Cinema-U3100Mini Plus V2", NULL) },
1379 { DVB_USB_DEVICE(USB_VID_KWORLD_2, 0xd393,
1380 &rtl2832u_props, "GIGABYTE U7300", NULL) },
1381 { DVB_USB_DEVICE(USB_VID_DEXATEK, 0x1104,
1382 &rtl2832u_props, "MSI DIGIVOX Micro HD", NULL) },
1383 { DVB_USB_DEVICE(USB_VID_COMPRO, 0x0620,
1384 &rtl2832u_props, "Compro VideoMate U620F", NULL) },
1385 { DVB_USB_DEVICE(USB_VID_KWORLD_2, 0xd394,
1386 &rtl2832u_props, "MaxMedia HU394-T", NULL) },
1387 { DVB_USB_DEVICE(USB_VID_LEADTEK, 0x6a03,
1388 &rtl2832u_props, "Leadtek WinFast DTV Dongle mini", NULL) },
1389 { DVB_USB_DEVICE(USB_VID_GTEK, USB_PID_CPYTO_REDI_PC50A,
1390 &rtl2832u_props, "Crypto ReDi PC 50 A", NULL) },
1393 MODULE_DEVICE_TABLE(usb, rtl28xxu_id_table);
1395 static struct usb_driver rtl28xxu_usb_driver = {
1396 .name = KBUILD_MODNAME,
1397 .id_table = rtl28xxu_id_table,
1398 .probe = dvb_usbv2_probe,
1399 .disconnect = dvb_usbv2_disconnect,
1400 .suspend = dvb_usbv2_suspend,
1401 .resume = dvb_usbv2_resume,
1402 .reset_resume = dvb_usbv2_reset_resume,
1407 module_usb_driver(rtl28xxu_usb_driver);
1409 MODULE_DESCRIPTION("Realtek RTL28xxU DVB USB driver");
1410 MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
1411 MODULE_AUTHOR("Thomas Mair <thomas.mair86@googlemail.com>");
1412 MODULE_LICENSE("GPL");