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.
37 DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
39 static int rtl28xxu_ctrl_msg(struct dvb_usb_device *d, struct rtl28xxu_req *req)
46 buf = kmalloc(req->size, GFP_KERNEL);
52 if (req->index & CMD_WR_FLAG) {
54 memcpy(buf, req->data, req->size);
55 requesttype = (USB_TYPE_VENDOR | USB_DIR_OUT);
56 pipe = usb_sndctrlpipe(d->udev, 0);
59 requesttype = (USB_TYPE_VENDOR | USB_DIR_IN);
60 pipe = usb_rcvctrlpipe(d->udev, 0);
63 ret = usb_control_msg(d->udev, pipe, 0, requesttype, req->value,
64 req->index, buf, req->size, 1000);
66 dvb_usb_dbg_usb_control_msg(d->udev, 0, requesttype, req->value,
67 req->index, buf, req->size);
72 /* read request, copy returned data to return buf */
73 if (!ret && requesttype == (USB_TYPE_VENDOR | USB_DIR_IN))
74 memcpy(req->data, buf, req->size);
83 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
87 static int rtl28xx_wr_regs(struct dvb_usb_device *d, u16 reg, u8 *val, int len)
89 struct rtl28xxu_req req;
92 req.index = CMD_USB_WR;
93 else if (reg < 0x4000)
94 req.index = CMD_SYS_WR;
96 req.index = CMD_IR_WR;
102 return rtl28xxu_ctrl_msg(d, &req);
105 static int rtl2831_rd_regs(struct dvb_usb_device *d, u16 reg, u8 *val, int len)
107 struct rtl28xxu_req req;
110 req.index = CMD_USB_RD;
111 else if (reg < 0x4000)
112 req.index = CMD_SYS_RD;
114 req.index = CMD_IR_RD;
120 return rtl28xxu_ctrl_msg(d, &req);
123 static int rtl28xx_wr_reg(struct dvb_usb_device *d, u16 reg, u8 val)
125 return rtl28xx_wr_regs(d, reg, &val, 1);
128 static int rtl28xx_rd_reg(struct dvb_usb_device *d, u16 reg, u8 *val)
130 return rtl2831_rd_regs(d, reg, val, 1);
133 static int rtl28xx_wr_reg_mask(struct dvb_usb_device *d, u16 reg, u8 val,
139 /* no need for read if whole reg is written */
141 ret = rtl28xx_rd_reg(d, reg, &tmp);
150 return rtl28xx_wr_reg(d, reg, val);
154 static int rtl28xxu_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg msg[],
158 struct dvb_usb_device *d = i2c_get_adapdata(adap);
159 struct rtl28xxu_priv *priv = d->priv;
160 struct rtl28xxu_req req;
163 * It is not known which are real I2C bus xfer limits, but testing
164 * with RTL2831U + MT2060 gives max RD 24 and max WR 22 bytes.
165 * TODO: find out RTL2832U lens
169 * I2C adapter logic looks rather complicated due to fact it handles
170 * three different access methods. Those methods are;
171 * 1) integrated demod access
175 * Used method is selected in order 1, 2, 3. Method 3 can handle all
176 * requests but there is two reasons why not use it always;
177 * 1) It is most expensive, usually two USB messages are needed
178 * 2) At least RTL2831U does not support it
180 * Method 3 is needed in case of I2C write+read (typical register read)
181 * where write is more than one byte.
184 if (mutex_lock_interruptible(&d->i2c_mutex) < 0)
187 if (num == 2 && !(msg[0].flags & I2C_M_RD) &&
188 (msg[1].flags & I2C_M_RD)) {
189 if (msg[0].len > 24 || msg[1].len > 24) {
190 /* TODO: check msg[0].len max */
192 goto err_mutex_unlock;
193 } else if (msg[0].addr == 0x10) {
194 /* method 1 - integrated demod */
195 req.value = (msg[0].buf[0] << 8) | (msg[0].addr << 1);
196 req.index = CMD_DEMOD_RD | priv->page;
197 req.size = msg[1].len;
198 req.data = &msg[1].buf[0];
199 ret = rtl28xxu_ctrl_msg(d, &req);
200 } else if (msg[0].len < 2) {
201 /* method 2 - old I2C */
202 req.value = (msg[0].buf[0] << 8) | (msg[0].addr << 1);
203 req.index = CMD_I2C_RD;
204 req.size = msg[1].len;
205 req.data = &msg[1].buf[0];
206 ret = rtl28xxu_ctrl_msg(d, &req);
208 /* method 3 - new I2C */
209 req.value = (msg[0].addr << 1);
210 req.index = CMD_I2C_DA_WR;
211 req.size = msg[0].len;
212 req.data = msg[0].buf;
213 ret = rtl28xxu_ctrl_msg(d, &req);
215 goto err_mutex_unlock;
217 req.value = (msg[0].addr << 1);
218 req.index = CMD_I2C_DA_RD;
219 req.size = msg[1].len;
220 req.data = msg[1].buf;
221 ret = rtl28xxu_ctrl_msg(d, &req);
223 } else if (num == 1 && !(msg[0].flags & I2C_M_RD)) {
224 if (msg[0].len > 22) {
225 /* TODO: check msg[0].len max */
227 goto err_mutex_unlock;
228 } else if (msg[0].addr == 0x10) {
229 /* method 1 - integrated demod */
230 if (msg[0].buf[0] == 0x00) {
231 /* save demod page for later demod access */
232 priv->page = msg[0].buf[1];
235 req.value = (msg[0].buf[0] << 8) |
237 req.index = CMD_DEMOD_WR | priv->page;
238 req.size = msg[0].len-1;
239 req.data = &msg[0].buf[1];
240 ret = rtl28xxu_ctrl_msg(d, &req);
242 } else if (msg[0].len < 23) {
243 /* method 2 - old I2C */
244 req.value = (msg[0].buf[0] << 8) | (msg[0].addr << 1);
245 req.index = CMD_I2C_WR;
246 req.size = msg[0].len-1;
247 req.data = &msg[0].buf[1];
248 ret = rtl28xxu_ctrl_msg(d, &req);
250 /* method 3 - new I2C */
251 req.value = (msg[0].addr << 1);
252 req.index = CMD_I2C_DA_WR;
253 req.size = msg[0].len;
254 req.data = msg[0].buf;
255 ret = rtl28xxu_ctrl_msg(d, &req);
262 mutex_unlock(&d->i2c_mutex);
264 return ret ? ret : num;
267 static u32 rtl28xxu_i2c_func(struct i2c_adapter *adapter)
272 static struct i2c_algorithm rtl28xxu_i2c_algo = {
273 .master_xfer = rtl28xxu_i2c_xfer,
274 .functionality = rtl28xxu_i2c_func,
277 static int rtl2831u_read_config(struct dvb_usb_device *d)
279 struct rtl28xxu_priv *priv = d_to_priv(d);
282 /* open RTL2831U/RTL2830 I2C gate */
283 struct rtl28xxu_req req_gate_open = {0x0120, 0x0011, 0x0001, "\x08"};
285 struct rtl28xxu_req req_mt2060 = {0x00c0, CMD_I2C_RD, 1, buf};
286 struct rtl28xxu_req req_qt1010 = {0x0fc4, CMD_I2C_RD, 1, buf};
288 dev_dbg(&d->udev->dev, "%s:\n", __func__);
292 * =========================================================
293 * GPIO0 | tuner#0 | 0 off | 1 on | MXL5005S (?)
294 * GPIO2 | LED | 0 off | 1 on |
295 * GPIO4 | tuner#1 | 0 on | 1 off | MT2060
299 ret = rtl28xx_wr_reg(d, SYS_GPIO_DIR, 0x0a);
303 /* enable as output GPIO0, GPIO2, GPIO4 */
304 ret = rtl28xx_wr_reg(d, SYS_GPIO_OUT_EN, 0x15);
309 * Probe used tuner. We need to know used tuner before demod attach
310 * since there is some demod params needed to set according to tuner.
313 /* demod needs some time to wake up */
316 priv->tuner_name = "NONE";
318 /* open demod I2C gate */
319 ret = rtl28xxu_ctrl_msg(d, &req_gate_open);
323 /* check QT1010 ID(?) register; reg=0f val=2c */
324 ret = rtl28xxu_ctrl_msg(d, &req_qt1010);
325 if (ret == 0 && buf[0] == 0x2c) {
326 priv->tuner = TUNER_RTL2830_QT1010;
327 priv->tuner_name = "QT1010";
331 /* open demod I2C gate */
332 ret = rtl28xxu_ctrl_msg(d, &req_gate_open);
336 /* check MT2060 ID register; reg=00 val=63 */
337 ret = rtl28xxu_ctrl_msg(d, &req_mt2060);
338 if (ret == 0 && buf[0] == 0x63) {
339 priv->tuner = TUNER_RTL2830_MT2060;
340 priv->tuner_name = "MT2060";
344 /* assume MXL5005S */
345 priv->tuner = TUNER_RTL2830_MXL5005S;
346 priv->tuner_name = "MXL5005S";
350 dev_dbg(&d->udev->dev, "%s: tuner=%s\n", __func__, priv->tuner_name);
354 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
358 static int rtl2832u_read_config(struct dvb_usb_device *d)
360 struct rtl28xxu_priv *priv = d_to_priv(d);
363 /* open RTL2832U/RTL2832 I2C gate */
364 struct rtl28xxu_req req_gate_open = {0x0120, 0x0011, 0x0001, "\x18"};
365 /* close RTL2832U/RTL2832 I2C gate */
366 struct rtl28xxu_req req_gate_close = {0x0120, 0x0011, 0x0001, "\x10"};
368 struct rtl28xxu_req req_fc0012 = {0x00c6, CMD_I2C_RD, 1, buf};
369 struct rtl28xxu_req req_fc0013 = {0x00c6, CMD_I2C_RD, 1, buf};
370 struct rtl28xxu_req req_mt2266 = {0x00c0, CMD_I2C_RD, 1, buf};
371 struct rtl28xxu_req req_fc2580 = {0x01ac, CMD_I2C_RD, 1, buf};
372 struct rtl28xxu_req req_mt2063 = {0x00c0, CMD_I2C_RD, 1, buf};
373 struct rtl28xxu_req req_max3543 = {0x00c0, CMD_I2C_RD, 1, buf};
374 struct rtl28xxu_req req_tua9001 = {0x7ec0, CMD_I2C_RD, 2, buf};
375 struct rtl28xxu_req req_mxl5007t = {0xd9c0, CMD_I2C_RD, 1, buf};
376 struct rtl28xxu_req req_e4000 = {0x02c8, CMD_I2C_RD, 1, buf};
377 struct rtl28xxu_req req_tda18272 = {0x00c0, CMD_I2C_RD, 2, buf};
379 dev_dbg(&d->udev->dev, "%s:\n", __func__);
381 /* enable GPIO3 and GPIO6 as output */
382 ret = rtl28xx_wr_reg_mask(d, SYS_GPIO_DIR, 0x00, 0x40);
386 ret = rtl28xx_wr_reg_mask(d, SYS_GPIO_OUT_EN, 0x48, 0x48);
391 * Probe used tuner. We need to know used tuner before demod attach
392 * since there is some demod params needed to set according to tuner.
395 /* open demod I2C gate */
396 ret = rtl28xxu_ctrl_msg(d, &req_gate_open);
400 priv->tuner_name = "NONE";
402 /* check FC0012 ID register; reg=00 val=a1 */
403 ret = rtl28xxu_ctrl_msg(d, &req_fc0012);
404 if (ret == 0 && buf[0] == 0xa1) {
405 priv->tuner = TUNER_RTL2832_FC0012;
406 priv->tuner_name = "FC0012";
410 /* check FC0013 ID register; reg=00 val=a3 */
411 ret = rtl28xxu_ctrl_msg(d, &req_fc0013);
412 if (ret == 0 && buf[0] == 0xa3) {
413 priv->tuner = TUNER_RTL2832_FC0013;
414 priv->tuner_name = "FC0013";
418 /* check MT2266 ID register; reg=00 val=85 */
419 ret = rtl28xxu_ctrl_msg(d, &req_mt2266);
420 if (ret == 0 && buf[0] == 0x85) {
421 priv->tuner = TUNER_RTL2832_MT2266;
422 priv->tuner_name = "MT2266";
426 /* check FC2580 ID register; reg=01 val=56 */
427 ret = rtl28xxu_ctrl_msg(d, &req_fc2580);
428 if (ret == 0 && buf[0] == 0x56) {
429 priv->tuner = TUNER_RTL2832_FC2580;
430 priv->tuner_name = "FC2580";
434 /* check MT2063 ID register; reg=00 val=9e || 9c */
435 ret = rtl28xxu_ctrl_msg(d, &req_mt2063);
436 if (ret == 0 && (buf[0] == 0x9e || buf[0] == 0x9c)) {
437 priv->tuner = TUNER_RTL2832_MT2063;
438 priv->tuner_name = "MT2063";
442 /* check MAX3543 ID register; reg=00 val=38 */
443 ret = rtl28xxu_ctrl_msg(d, &req_max3543);
444 if (ret == 0 && buf[0] == 0x38) {
445 priv->tuner = TUNER_RTL2832_MAX3543;
446 priv->tuner_name = "MAX3543";
450 /* check TUA9001 ID register; reg=7e val=2328 */
451 ret = rtl28xxu_ctrl_msg(d, &req_tua9001);
452 if (ret == 0 && buf[0] == 0x23 && buf[1] == 0x28) {
453 priv->tuner = TUNER_RTL2832_TUA9001;
454 priv->tuner_name = "TUA9001";
458 /* check MXL5007R ID register; reg=d9 val=14 */
459 ret = rtl28xxu_ctrl_msg(d, &req_mxl5007t);
460 if (ret == 0 && buf[0] == 0x14) {
461 priv->tuner = TUNER_RTL2832_MXL5007T;
462 priv->tuner_name = "MXL5007T";
466 /* check E4000 ID register; reg=02 val=40 */
467 ret = rtl28xxu_ctrl_msg(d, &req_e4000);
468 if (ret == 0 && buf[0] == 0x40) {
469 priv->tuner = TUNER_RTL2832_E4000;
470 priv->tuner_name = "E4000";
474 /* check TDA18272 ID register; reg=00 val=c760 */
475 ret = rtl28xxu_ctrl_msg(d, &req_tda18272);
476 if (ret == 0 && (buf[0] == 0xc7 || buf[1] == 0x60)) {
477 priv->tuner = TUNER_RTL2832_TDA18272;
478 priv->tuner_name = "TDA18272";
483 dev_dbg(&d->udev->dev, "%s: tuner=%s\n", __func__, priv->tuner_name);
485 /* close demod I2C gate */
486 ret = rtl28xxu_ctrl_msg(d, &req_gate_close);
492 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
496 static struct rtl2830_config rtl28xxu_rtl2830_mt2060_config = {
497 .i2c_addr = 0x10, /* 0x20 */
503 .agc_targ_val = 0x2d,
507 static struct rtl2830_config rtl28xxu_rtl2830_qt1010_config = {
508 .i2c_addr = 0x10, /* 0x20 */
514 .agc_targ_val = 0x2d,
517 static struct rtl2830_config rtl28xxu_rtl2830_mxl5005s_config = {
518 .i2c_addr = 0x10, /* 0x20 */
524 .agc_targ_val = 0x3e,
527 static int rtl2831u_frontend_attach(struct dvb_usb_adapter *adap)
529 struct dvb_usb_device *d = adap_to_d(adap);
530 struct rtl28xxu_priv *priv = d_to_priv(d);
531 struct rtl2830_config *rtl2830_config;
534 dev_dbg(&d->udev->dev, "%s:\n", __func__);
536 switch (priv->tuner) {
537 case TUNER_RTL2830_QT1010:
538 rtl2830_config = &rtl28xxu_rtl2830_qt1010_config;
540 case TUNER_RTL2830_MT2060:
541 rtl2830_config = &rtl28xxu_rtl2830_mt2060_config;
543 case TUNER_RTL2830_MXL5005S:
544 rtl2830_config = &rtl28xxu_rtl2830_mxl5005s_config;
547 dev_err(&d->udev->dev, "%s: unknown tuner=%s\n",
548 KBUILD_MODNAME, priv->tuner_name);
553 /* attach demodulator */
554 adap->fe[0] = dvb_attach(rtl2830_attach, rtl2830_config, &d->i2c_adap);
562 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
566 static struct rtl2832_config rtl28xxu_rtl2832_fc0012_config = {
567 .i2c_addr = 0x10, /* 0x20 */
570 .tuner = TUNER_RTL2832_FC0012
573 static struct rtl2832_config rtl28xxu_rtl2832_fc0013_config = {
574 .i2c_addr = 0x10, /* 0x20 */
577 .tuner = TUNER_RTL2832_FC0013
580 static struct rtl2832_config rtl28xxu_rtl2832_tua9001_config = {
581 .i2c_addr = 0x10, /* 0x20 */
583 .tuner = TUNER_RTL2832_TUA9001,
586 static struct rtl2832_config rtl28xxu_rtl2832_e4000_config = {
587 .i2c_addr = 0x10, /* 0x20 */
589 .tuner = TUNER_RTL2832_E4000,
592 static int rtl2832u_fc0012_tuner_callback(struct dvb_usb_device *d,
598 dev_dbg(&d->udev->dev, "%s: cmd=%d arg=%d\n", __func__, cmd, arg);
601 case FC_FE_CALLBACK_VHF_ENABLE:
602 /* set output values */
603 ret = rtl28xx_rd_reg(d, SYS_GPIO_OUT_VAL, &val);
608 val &= 0xbf; /* set GPIO6 low */
610 val |= 0x40; /* set GPIO6 high */
613 ret = rtl28xx_wr_reg(d, SYS_GPIO_OUT_VAL, val);
623 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
627 static int rtl2832u_tua9001_tuner_callback(struct dvb_usb_device *d,
633 dev_dbg(&d->udev->dev, "%s: cmd=%d arg=%d\n", __func__, cmd, arg);
636 * CEN always enabled by hardware wiring
642 case TUA9001_CMD_RESETN:
648 ret = rtl28xx_wr_reg_mask(d, SYS_GPIO_OUT_VAL, val, 0x10);
652 case TUA9001_CMD_RXEN:
658 ret = rtl28xx_wr_reg_mask(d, SYS_GPIO_OUT_VAL, val, 0x02);
666 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
670 static int rtl2832u_tuner_callback(struct dvb_usb_device *d, int cmd, int arg)
672 struct rtl28xxu_priv *priv = d->priv;
674 switch (priv->tuner) {
675 case TUNER_RTL2832_FC0012:
676 return rtl2832u_fc0012_tuner_callback(d, cmd, arg);
677 case TUNER_RTL2832_TUA9001:
678 return rtl2832u_tua9001_tuner_callback(d, cmd, arg);
686 static int rtl2832u_frontend_callback(void *adapter_priv, int component,
689 struct i2c_adapter *adap = adapter_priv;
690 struct dvb_usb_device *d = i2c_get_adapdata(adap);
692 dev_dbg(&d->udev->dev, "%s: component=%d cmd=%d arg=%d\n",
693 __func__, component, cmd, arg);
696 case DVB_FRONTEND_COMPONENT_TUNER:
697 return rtl2832u_tuner_callback(d, cmd, arg);
705 static int rtl2832u_frontend_attach(struct dvb_usb_adapter *adap)
708 struct dvb_usb_device *d = adap_to_d(adap);
709 struct rtl28xxu_priv *priv = d_to_priv(d);
710 struct rtl2832_config *rtl2832_config;
712 dev_dbg(&d->udev->dev, "%s:\n", __func__);
714 switch (priv->tuner) {
715 case TUNER_RTL2832_FC0012:
716 rtl2832_config = &rtl28xxu_rtl2832_fc0012_config;
718 case TUNER_RTL2832_FC0013:
719 rtl2832_config = &rtl28xxu_rtl2832_fc0013_config;
721 case TUNER_RTL2832_FC2580:
722 /* FIXME: do not abuse fc0012 settings */
723 rtl2832_config = &rtl28xxu_rtl2832_fc0012_config;
725 case TUNER_RTL2832_TUA9001:
726 rtl2832_config = &rtl28xxu_rtl2832_tua9001_config;
728 case TUNER_RTL2832_E4000:
729 rtl2832_config = &rtl28xxu_rtl2832_e4000_config;
732 dev_err(&d->udev->dev, "%s: unknown tuner=%s\n",
733 KBUILD_MODNAME, priv->tuner_name);
738 /* attach demodulator */
739 adap->fe[0] = dvb_attach(rtl2832_attach, rtl2832_config, &d->i2c_adap);
745 /* set fe callback */
746 adap->fe[0]->callback = rtl2832u_frontend_callback;
750 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
754 static struct qt1010_config rtl28xxu_qt1010_config = {
755 .i2c_address = 0x62, /* 0xc4 */
758 static struct mt2060_config rtl28xxu_mt2060_config = {
759 .i2c_address = 0x60, /* 0xc0 */
763 static struct mxl5005s_config rtl28xxu_mxl5005s_config = {
764 .i2c_address = 0x63, /* 0xc6 */
765 .if_freq = IF_FREQ_4570000HZ,
766 .xtal_freq = CRYSTAL_FREQ_16000000HZ,
767 .agc_mode = MXL_SINGLE_AGC,
768 .tracking_filter = MXL_TF_C_H,
769 .rssi_enable = MXL_RSSI_ENABLE,
770 .cap_select = MXL_CAP_SEL_ENABLE,
771 .div_out = MXL_DIV_OUT_4,
772 .clock_out = MXL_CLOCK_OUT_DISABLE,
773 .output_load = MXL5005S_IF_OUTPUT_LOAD_200_OHM,
774 .top = MXL5005S_TOP_25P2,
775 .mod_mode = MXL_DIGITAL_MODE,
776 .if_mode = MXL_ZERO_IF,
777 .AgcMasterByte = 0x00,
780 static int rtl2831u_tuner_attach(struct dvb_usb_adapter *adap)
783 struct dvb_usb_device *d = adap_to_d(adap);
784 struct rtl28xxu_priv *priv = d_to_priv(d);
785 struct i2c_adapter *rtl2830_tuner_i2c;
786 struct dvb_frontend *fe;
788 dev_dbg(&d->udev->dev, "%s:\n", __func__);
790 /* use rtl2830 driver I2C adapter, for more info see rtl2830 driver */
791 rtl2830_tuner_i2c = rtl2830_get_tuner_i2c_adapter(adap->fe[0]);
793 switch (priv->tuner) {
794 case TUNER_RTL2830_QT1010:
795 fe = dvb_attach(qt1010_attach, adap->fe[0],
796 rtl2830_tuner_i2c, &rtl28xxu_qt1010_config);
798 case TUNER_RTL2830_MT2060:
799 fe = dvb_attach(mt2060_attach, adap->fe[0],
800 rtl2830_tuner_i2c, &rtl28xxu_mt2060_config,
803 case TUNER_RTL2830_MXL5005S:
804 fe = dvb_attach(mxl5005s_attach, adap->fe[0],
805 rtl2830_tuner_i2c, &rtl28xxu_mxl5005s_config);
809 dev_err(&d->udev->dev, "%s: unknown tuner=%d\n", KBUILD_MODNAME,
820 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
824 static const struct e4000_config rtl2832u_e4000_config = {
829 static const struct fc2580_config rtl2832u_fc2580_config = {
834 static struct tua9001_config rtl2832u_tua9001_config = {
838 static int rtl2832u_tuner_attach(struct dvb_usb_adapter *adap)
841 struct dvb_usb_device *d = adap_to_d(adap);
842 struct rtl28xxu_priv *priv = d_to_priv(d);
843 struct dvb_frontend *fe;
845 dev_dbg(&d->udev->dev, "%s:\n", __func__);
847 switch (priv->tuner) {
848 case TUNER_RTL2832_FC0012:
849 fe = dvb_attach(fc0012_attach, adap->fe[0],
850 &d->i2c_adap, 0xc6>>1, 0, FC_XTAL_28_8_MHZ);
852 /* since fc0012 includs reading the signal strength delegate
853 * that to the tuner driver */
854 adap->fe[0]->ops.read_signal_strength =
855 adap->fe[0]->ops.tuner_ops.get_rf_strength;
858 case TUNER_RTL2832_FC0013:
859 fe = dvb_attach(fc0013_attach, adap->fe[0],
860 &d->i2c_adap, 0xc6>>1, 0, FC_XTAL_28_8_MHZ);
862 /* fc0013 also supports signal strength reading */
863 adap->fe[0]->ops.read_signal_strength =
864 adap->fe[0]->ops.tuner_ops.get_rf_strength;
866 case TUNER_RTL2832_E4000:
867 fe = dvb_attach(e4000_attach, adap->fe[0], &d->i2c_adap,
868 &rtl2832u_e4000_config);
870 case TUNER_RTL2832_FC2580:
871 fe = dvb_attach(fc2580_attach, adap->fe[0], &d->i2c_adap,
872 &rtl2832u_fc2580_config);
874 case TUNER_RTL2832_TUA9001:
875 /* enable GPIO1 and GPIO4 as output */
876 ret = rtl28xx_wr_reg_mask(d, SYS_GPIO_DIR, 0x00, 0x12);
880 ret = rtl28xx_wr_reg_mask(d, SYS_GPIO_OUT_EN, 0x12, 0x12);
884 fe = dvb_attach(tua9001_attach, adap->fe[0], &d->i2c_adap,
885 &rtl2832u_tua9001_config);
889 dev_err(&d->udev->dev, "%s: unknown tuner=%d\n", KBUILD_MODNAME,
900 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
904 static int rtl28xxu_init(struct dvb_usb_device *d)
909 dev_dbg(&d->udev->dev, "%s:\n", __func__);
911 /* init USB endpoints */
912 ret = rtl28xx_rd_reg(d, USB_SYSCTL_0, &val);
916 /* enable DMA and Full Packet Mode*/
918 ret = rtl28xx_wr_reg(d, USB_SYSCTL_0, val);
922 /* set EPA maximum packet size to 0x0200 */
923 ret = rtl28xx_wr_regs(d, USB_EPA_MAXPKT, "\x00\x02\x00\x00", 4);
927 /* change EPA FIFO length */
928 ret = rtl28xx_wr_regs(d, USB_EPA_FIFO_CFG, "\x14\x00\x00\x00", 4);
934 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
938 static int rtl2831u_power_ctrl(struct dvb_usb_device *d, int onoff)
941 u8 gpio, sys0, epa_ctl[2];
943 dev_dbg(&d->udev->dev, "%s: onoff=%d\n", __func__, onoff);
946 ret = rtl28xx_rd_reg(d, SYS_SYS0, &sys0);
950 /* tuner power, read GPIOs */
951 ret = rtl28xx_rd_reg(d, SYS_GPIO_OUT_VAL, &gpio);
955 dev_dbg(&d->udev->dev, "%s: RD SYS0=%02x GPIO_OUT_VAL=%02x\n", __func__,
959 gpio |= 0x01; /* GPIO0 = 1 */
960 gpio &= (~0x10); /* GPIO4 = 0 */
961 gpio |= 0x04; /* GPIO2 = 1, LED on */
964 epa_ctl[0] = 0x00; /* clear stall */
965 epa_ctl[1] = 0x00; /* clear reset */
967 gpio &= (~0x01); /* GPIO0 = 0 */
968 gpio |= 0x10; /* GPIO4 = 1 */
969 gpio &= (~0x04); /* GPIO2 = 1, LED off */
970 sys0 = sys0 & (~0xc0);
971 epa_ctl[0] = 0x10; /* set stall */
972 epa_ctl[1] = 0x02; /* set reset */
975 dev_dbg(&d->udev->dev, "%s: WR SYS0=%02x GPIO_OUT_VAL=%02x\n", __func__,
979 ret = rtl28xx_wr_reg(d, SYS_SYS0, sys0);
983 /* tuner power, write GPIOs */
984 ret = rtl28xx_wr_reg(d, SYS_GPIO_OUT_VAL, gpio);
988 /* streaming EP: stall & reset */
989 ret = rtl28xx_wr_regs(d, USB_EPA_CTL, epa_ctl, 2);
994 usb_clear_halt(d->udev, usb_rcvbulkpipe(d->udev, 0x81));
998 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
1002 static int rtl2832u_power_ctrl(struct dvb_usb_device *d, int onoff)
1007 dev_dbg(&d->udev->dev, "%s: onoff=%d\n", __func__, onoff);
1010 /* set output values */
1011 ret = rtl28xx_rd_reg(d, SYS_GPIO_OUT_VAL, &val);
1018 ret = rtl28xx_wr_reg(d, SYS_GPIO_OUT_VAL, val);
1023 ret = rtl28xx_rd_reg(d, SYS_DEMOD_CTL1, &val);
1029 ret = rtl28xx_wr_reg(d, SYS_DEMOD_CTL1, val);
1035 ret = rtl28xx_rd_reg(d, SYS_DEMOD_CTL, &val);
1042 ret = rtl28xx_wr_reg(d, SYS_DEMOD_CTL, val);
1046 ret = rtl28xx_rd_reg(d, SYS_DEMOD_CTL, &val);
1052 ret = rtl28xx_wr_reg(d, SYS_DEMOD_CTL, val);
1058 /*enable ADC_Q and ADC_I */
1059 ret = rtl28xx_rd_reg(d, SYS_DEMOD_CTL, &val);
1065 ret = rtl28xx_wr_reg(d, SYS_DEMOD_CTL, val);
1069 /* streaming EP: clear stall & reset */
1070 ret = rtl28xx_wr_regs(d, USB_EPA_CTL, "\x00\x00", 2);
1074 ret = usb_clear_halt(d->udev, usb_rcvbulkpipe(d->udev, 0x81));
1079 ret = rtl28xx_rd_reg(d, SYS_DEMOD_CTL1, &val);
1085 ret = rtl28xx_wr_reg(d, SYS_DEMOD_CTL1, val);
1089 /* set output values */
1090 ret = rtl28xx_rd_reg(d, SYS_GPIO_OUT_VAL, &val);
1096 ret = rtl28xx_wr_reg(d, SYS_GPIO_OUT_VAL, val);
1101 ret = rtl28xx_rd_reg(d, SYS_DEMOD_CTL, &val);
1107 ret = rtl28xx_wr_reg(d, SYS_DEMOD_CTL, val);
1111 /* streaming EP: set stall & reset */
1112 ret = rtl28xx_wr_regs(d, USB_EPA_CTL, "\x10\x02", 2);
1119 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
1124 static int rtl2831u_rc_query(struct dvb_usb_device *d)
1127 struct rtl28xxu_priv *priv = d->priv;
1130 struct rtl28xxu_reg_val rc_nec_tab[] = {
1147 /* init remote controller */
1148 if (!priv->rc_active) {
1149 for (i = 0; i < ARRAY_SIZE(rc_nec_tab); i++) {
1150 ret = rtl28xx_wr_reg(d, rc_nec_tab[i].reg,
1155 priv->rc_active = true;
1158 ret = rtl2831_rd_regs(d, SYS_IRRC_RP, buf, 5);
1162 if (buf[4] & 0x01) {
1163 if (buf[2] == (u8) ~buf[3]) {
1164 if (buf[0] == (u8) ~buf[1]) {
1165 /* NEC standard (16 bit) */
1166 rc_code = buf[0] << 8 | buf[2];
1168 /* NEC extended (24 bit) */
1169 rc_code = buf[0] << 16 |
1170 buf[1] << 8 | buf[2];
1173 /* NEC full (32 bit) */
1174 rc_code = buf[0] << 24 | buf[1] << 16 |
1175 buf[2] << 8 | buf[3];
1178 rc_keydown(d->rc_dev, rc_code, 0);
1180 ret = rtl28xx_wr_reg(d, SYS_IRRC_SR, 1);
1184 /* repeated intentionally to avoid extra keypress */
1185 ret = rtl28xx_wr_reg(d, SYS_IRRC_SR, 1);
1192 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
1196 static int rtl2831u_get_rc_config(struct dvb_usb_device *d,
1197 struct dvb_usb_rc *rc)
1199 rc->map_name = RC_MAP_EMPTY;
1200 rc->allowed_protos = RC_TYPE_NEC;
1201 rc->query = rtl2831u_rc_query;
1207 static int rtl2832u_rc_query(struct dvb_usb_device *d)
1210 struct rtl28xxu_priv *priv = d->priv;
1213 struct rtl28xxu_reg_val rc_nec_tab[] = {
1214 { IR_RX_CTRL, 0x20 },
1215 { IR_RX_BUF_CTRL, 0x80 },
1218 { IR_MAX_DURATION0, 0xd0 },
1219 { IR_MAX_DURATION1, 0x07 },
1220 { IR_IDLE_LEN0, 0xc0 },
1221 { IR_IDLE_LEN1, 0x00 },
1222 { IR_GLITCH_LEN, 0x03 },
1223 { IR_RX_CLK, 0x09 },
1224 { IR_RX_CFG, 0x1c },
1225 { IR_MAX_H_TOL_LEN, 0x1e },
1226 { IR_MAX_L_TOL_LEN, 0x1e },
1227 { IR_RX_CTRL, 0x80 },
1230 /* init remote controller */
1231 if (!priv->rc_active) {
1232 for (i = 0; i < ARRAY_SIZE(rc_nec_tab); i++) {
1233 ret = rtl28xx_wr_reg(d, rc_nec_tab[i].reg,
1238 priv->rc_active = true;
1241 ret = rtl28xx_rd_reg(d, IR_RX_IF, &buf[0]);
1248 ret = rtl28xx_rd_reg(d, IR_RX_BC, &buf[0]);
1253 ret = rtl2831_rd_regs(d, IR_RX_BUF, buf, len);
1255 /* TODO: pass raw IR to Kernel IR decoder */
1257 ret = rtl28xx_wr_reg(d, IR_RX_IF, 0x03);
1258 ret = rtl28xx_wr_reg(d, IR_RX_BUF_CTRL, 0x80);
1259 ret = rtl28xx_wr_reg(d, IR_RX_CTRL, 0x80);
1264 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
1268 static int rtl2832u_get_rc_config(struct dvb_usb_device *d,
1269 struct dvb_usb_rc *rc)
1271 rc->map_name = RC_MAP_EMPTY;
1272 rc->allowed_protos = RC_TYPE_NEC;
1273 rc->query = rtl2832u_rc_query;
1279 static const struct dvb_usb_device_properties rtl2831u_props = {
1280 .driver_name = KBUILD_MODNAME,
1281 .owner = THIS_MODULE,
1282 .adapter_nr = adapter_nr,
1283 .size_of_priv = sizeof(struct rtl28xxu_priv),
1285 .power_ctrl = rtl2831u_power_ctrl,
1286 .i2c_algo = &rtl28xxu_i2c_algo,
1287 .read_config = rtl2831u_read_config,
1288 .frontend_attach = rtl2831u_frontend_attach,
1289 .tuner_attach = rtl2831u_tuner_attach,
1290 .init = rtl28xxu_init,
1291 .get_rc_config = rtl2831u_get_rc_config,
1296 .stream = DVB_USB_STREAM_BULK(0x81, 6, 8 * 512),
1301 static const struct dvb_usb_device_properties rtl2832u_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 = rtl2832u_power_ctrl,
1308 .i2c_algo = &rtl28xxu_i2c_algo,
1309 .read_config = rtl2832u_read_config,
1310 .frontend_attach = rtl2832u_frontend_attach,
1311 .tuner_attach = rtl2832u_tuner_attach,
1312 .init = rtl28xxu_init,
1313 .get_rc_config = rtl2832u_get_rc_config,
1318 .stream = DVB_USB_STREAM_BULK(0x81, 6, 8 * 512),
1323 static const struct usb_device_id rtl28xxu_id_table[] = {
1324 { DVB_USB_DEVICE(USB_VID_REALTEK, USB_PID_REALTEK_RTL2831U,
1325 &rtl2831u_props, "Realtek RTL2831U reference design", NULL) },
1326 { DVB_USB_DEVICE(USB_VID_WIDEVIEW, USB_PID_FREECOM_DVBT,
1327 &rtl2831u_props, "Freecom USB2.0 DVB-T", NULL) },
1328 { DVB_USB_DEVICE(USB_VID_WIDEVIEW, USB_PID_FREECOM_DVBT_2,
1329 &rtl2831u_props, "Freecom USB2.0 DVB-T", NULL) },
1331 { DVB_USB_DEVICE(USB_VID_REALTEK, 0x2832,
1332 &rtl2832u_props, "Realtek RTL2832U reference design", NULL) },
1333 { DVB_USB_DEVICE(USB_VID_REALTEK, 0x2838,
1334 &rtl2832u_props, "Realtek RTL2832U reference design", NULL) },
1335 { DVB_USB_DEVICE(USB_VID_TERRATEC, USB_PID_TERRATEC_CINERGY_T_STICK_BLACK_REV1,
1336 &rtl2832u_props, "Terratec Cinergy T Stick Black", NULL) },
1337 { DVB_USB_DEVICE(USB_VID_GTEK, USB_PID_DELOCK_USB2_DVBT,
1338 &rtl2832u_props, "G-Tek Electronics Group Lifeview LV5TDLX DVB-T", NULL) },
1339 { DVB_USB_DEVICE(USB_VID_TERRATEC, USB_PID_NOXON_DAB_STICK,
1340 &rtl2832u_props, "NOXON DAB/DAB+ USB dongle", NULL) },
1341 { DVB_USB_DEVICE(USB_VID_GTEK, USB_PID_TREKSTOR_TERRES_2_0,
1342 &rtl2832u_props, "Trekstor DVB-T Stick Terres 2.0", NULL) },
1343 { DVB_USB_DEVICE(USB_VID_DEXATEK, 0x1101,
1344 &rtl2832u_props, "Dexatek DK DVB-T Dongle", NULL) },
1345 { DVB_USB_DEVICE(USB_VID_LEADTEK, 0x6680,
1346 &rtl2832u_props, "DigitalNow Quad DVB-T Receiver", NULL) },
1347 { DVB_USB_DEVICE(USB_VID_TERRATEC, 0x00d3,
1348 &rtl2832u_props, "TerraTec Cinergy T Stick RC (Rev. 3)", NULL) },
1351 MODULE_DEVICE_TABLE(usb, rtl28xxu_id_table);
1353 static struct usb_driver rtl28xxu_usb_driver = {
1354 .name = KBUILD_MODNAME,
1355 .id_table = rtl28xxu_id_table,
1356 .probe = dvb_usbv2_probe,
1357 .disconnect = dvb_usbv2_disconnect,
1358 .suspend = dvb_usbv2_suspend,
1359 .resume = dvb_usbv2_resume,
1360 .reset_resume = dvb_usbv2_reset_resume,
1365 module_usb_driver(rtl28xxu_usb_driver);
1367 MODULE_DESCRIPTION("Realtek RTL28xxU DVB USB driver");
1368 MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
1369 MODULE_AUTHOR("Thomas Mair <thomas.mair86@googlemail.com>");
1370 MODULE_LICENSE("GPL");