1 /* 2 * Realtek RTL28xxU DVB USB driver 3 * 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> 7 * 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. 12 * 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. 17 * 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. 21 */ 22 23 #include "rtl28xxu.h" 24 25 static int rtl28xxu_disable_rc; 26 module_param_named(disable_rc, rtl28xxu_disable_rc, int, 0644); 27 MODULE_PARM_DESC(disable_rc, "disable RTL2832U remote controller"); 28 DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr); 29 30 static int rtl28xxu_ctrl_msg(struct dvb_usb_device *d, struct rtl28xxu_req *req) 31 { 32 struct rtl28xxu_dev *dev = d->priv; 33 int ret; 34 unsigned int pipe; 35 u8 requesttype; 36 37 if (req->index & CMD_WR_FLAG) { 38 /* write */ 39 memcpy(dev->buf, req->data, req->size); 40 requesttype = (USB_TYPE_VENDOR | USB_DIR_OUT); 41 pipe = usb_sndctrlpipe(d->udev, 0); 42 } else { 43 /* read */ 44 requesttype = (USB_TYPE_VENDOR | USB_DIR_IN); 45 pipe = usb_rcvctrlpipe(d->udev, 0); 46 } 47 48 ret = usb_control_msg(d->udev, pipe, 0, requesttype, req->value, 49 req->index, dev->buf, req->size, 1000); 50 dvb_usb_dbg_usb_control_msg(d->udev, 0, requesttype, req->value, 51 req->index, dev->buf, req->size); 52 if (ret < 0) 53 goto err; 54 55 /* read request, copy returned data to return buf */ 56 if (requesttype == (USB_TYPE_VENDOR | USB_DIR_IN)) 57 memcpy(req->data, dev->buf, req->size); 58 59 return 0; 60 err: 61 dev_dbg(&d->intf->dev, "failed=%d\n", ret); 62 return ret; 63 } 64 65 static int rtl28xxu_wr_regs(struct dvb_usb_device *d, u16 reg, u8 *val, int len) 66 { 67 struct rtl28xxu_req req; 68 69 if (reg < 0x3000) 70 req.index = CMD_USB_WR; 71 else if (reg < 0x4000) 72 req.index = CMD_SYS_WR; 73 else 74 req.index = CMD_IR_WR; 75 76 req.value = reg; 77 req.size = len; 78 req.data = val; 79 80 return rtl28xxu_ctrl_msg(d, &req); 81 } 82 83 static int rtl28xxu_rd_regs(struct dvb_usb_device *d, u16 reg, u8 *val, int len) 84 { 85 struct rtl28xxu_req req; 86 87 if (reg < 0x3000) 88 req.index = CMD_USB_RD; 89 else if (reg < 0x4000) 90 req.index = CMD_SYS_RD; 91 else 92 req.index = CMD_IR_RD; 93 94 req.value = reg; 95 req.size = len; 96 req.data = val; 97 98 return rtl28xxu_ctrl_msg(d, &req); 99 } 100 101 static int rtl28xxu_wr_reg(struct dvb_usb_device *d, u16 reg, u8 val) 102 { 103 return rtl28xxu_wr_regs(d, reg, &val, 1); 104 } 105 106 static int rtl28xxu_rd_reg(struct dvb_usb_device *d, u16 reg, u8 *val) 107 { 108 return rtl28xxu_rd_regs(d, reg, val, 1); 109 } 110 111 static int rtl28xxu_wr_reg_mask(struct dvb_usb_device *d, u16 reg, u8 val, 112 u8 mask) 113 { 114 int ret; 115 u8 tmp; 116 117 /* no need for read if whole reg is written */ 118 if (mask != 0xff) { 119 ret = rtl28xxu_rd_reg(d, reg, &tmp); 120 if (ret) 121 return ret; 122 123 val &= mask; 124 tmp &= ~mask; 125 val |= tmp; 126 } 127 128 return rtl28xxu_wr_reg(d, reg, val); 129 } 130 131 /* I2C */ 132 static int rtl28xxu_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg msg[], 133 int num) 134 { 135 int ret; 136 struct dvb_usb_device *d = i2c_get_adapdata(adap); 137 struct rtl28xxu_dev *dev = d->priv; 138 struct rtl28xxu_req req; 139 140 /* 141 * It is not known which are real I2C bus xfer limits, but testing 142 * with RTL2831U + MT2060 gives max RD 24 and max WR 22 bytes. 143 * TODO: find out RTL2832U lens 144 */ 145 146 /* 147 * I2C adapter logic looks rather complicated due to fact it handles 148 * three different access methods. Those methods are; 149 * 1) integrated demod access 150 * 2) old I2C access 151 * 3) new I2C access 152 * 153 * Used method is selected in order 1, 2, 3. Method 3 can handle all 154 * requests but there is two reasons why not use it always; 155 * 1) It is most expensive, usually two USB messages are needed 156 * 2) At least RTL2831U does not support it 157 * 158 * Method 3 is needed in case of I2C write+read (typical register read) 159 * where write is more than one byte. 160 */ 161 162 if (mutex_lock_interruptible(&d->i2c_mutex) < 0) 163 return -EAGAIN; 164 165 if (num == 2 && !(msg[0].flags & I2C_M_RD) && 166 (msg[1].flags & I2C_M_RD)) { 167 if (msg[0].len > 24 || msg[1].len > 24) { 168 /* TODO: check msg[0].len max */ 169 ret = -EOPNOTSUPP; 170 goto err_mutex_unlock; 171 } else if (msg[0].addr == 0x10) { 172 /* method 1 - integrated demod */ 173 req.value = (msg[0].buf[0] << 8) | (msg[0].addr << 1); 174 req.index = CMD_DEMOD_RD | dev->page; 175 req.size = msg[1].len; 176 req.data = &msg[1].buf[0]; 177 ret = rtl28xxu_ctrl_msg(d, &req); 178 } else if (msg[0].len < 2) { 179 /* method 2 - old I2C */ 180 req.value = (msg[0].buf[0] << 8) | (msg[0].addr << 1); 181 req.index = CMD_I2C_RD; 182 req.size = msg[1].len; 183 req.data = &msg[1].buf[0]; 184 ret = rtl28xxu_ctrl_msg(d, &req); 185 } else { 186 /* method 3 - new I2C */ 187 req.value = (msg[0].addr << 1); 188 req.index = CMD_I2C_DA_WR; 189 req.size = msg[0].len; 190 req.data = msg[0].buf; 191 ret = rtl28xxu_ctrl_msg(d, &req); 192 if (ret) 193 goto err_mutex_unlock; 194 195 req.value = (msg[0].addr << 1); 196 req.index = CMD_I2C_DA_RD; 197 req.size = msg[1].len; 198 req.data = msg[1].buf; 199 ret = rtl28xxu_ctrl_msg(d, &req); 200 } 201 } else if (num == 1 && !(msg[0].flags & I2C_M_RD)) { 202 if (msg[0].len > 22) { 203 /* TODO: check msg[0].len max */ 204 ret = -EOPNOTSUPP; 205 goto err_mutex_unlock; 206 } else if (msg[0].addr == 0x10) { 207 /* method 1 - integrated demod */ 208 if (msg[0].buf[0] == 0x00) { 209 /* save demod page for later demod access */ 210 dev->page = msg[0].buf[1]; 211 ret = 0; 212 } else { 213 req.value = (msg[0].buf[0] << 8) | 214 (msg[0].addr << 1); 215 req.index = CMD_DEMOD_WR | dev->page; 216 req.size = msg[0].len-1; 217 req.data = &msg[0].buf[1]; 218 ret = rtl28xxu_ctrl_msg(d, &req); 219 } 220 } else if (msg[0].len < 23) { 221 /* method 2 - old I2C */ 222 req.value = (msg[0].buf[0] << 8) | (msg[0].addr << 1); 223 req.index = CMD_I2C_WR; 224 req.size = msg[0].len-1; 225 req.data = &msg[0].buf[1]; 226 ret = rtl28xxu_ctrl_msg(d, &req); 227 } else { 228 /* method 3 - new I2C */ 229 req.value = (msg[0].addr << 1); 230 req.index = CMD_I2C_DA_WR; 231 req.size = msg[0].len; 232 req.data = msg[0].buf; 233 ret = rtl28xxu_ctrl_msg(d, &req); 234 } 235 } else { 236 ret = -EINVAL; 237 } 238 239 err_mutex_unlock: 240 mutex_unlock(&d->i2c_mutex); 241 242 return ret ? ret : num; 243 } 244 245 static u32 rtl28xxu_i2c_func(struct i2c_adapter *adapter) 246 { 247 return I2C_FUNC_I2C; 248 } 249 250 static struct i2c_algorithm rtl28xxu_i2c_algo = { 251 .master_xfer = rtl28xxu_i2c_xfer, 252 .functionality = rtl28xxu_i2c_func, 253 }; 254 255 static int rtl2831u_read_config(struct dvb_usb_device *d) 256 { 257 struct rtl28xxu_dev *dev = d_to_priv(d); 258 int ret; 259 u8 buf[1]; 260 /* open RTL2831U/RTL2830 I2C gate */ 261 struct rtl28xxu_req req_gate_open = {0x0120, 0x0011, 0x0001, "\x08"}; 262 /* tuner probes */ 263 struct rtl28xxu_req req_mt2060 = {0x00c0, CMD_I2C_RD, 1, buf}; 264 struct rtl28xxu_req req_qt1010 = {0x0fc4, CMD_I2C_RD, 1, buf}; 265 266 dev_dbg(&d->intf->dev, "\n"); 267 268 /* 269 * RTL2831U GPIOs 270 * ========================================================= 271 * GPIO0 | tuner#0 | 0 off | 1 on | MXL5005S (?) 272 * GPIO2 | LED | 0 off | 1 on | 273 * GPIO4 | tuner#1 | 0 on | 1 off | MT2060 274 */ 275 276 /* GPIO direction */ 277 ret = rtl28xxu_wr_reg(d, SYS_GPIO_DIR, 0x0a); 278 if (ret) 279 goto err; 280 281 /* enable as output GPIO0, GPIO2, GPIO4 */ 282 ret = rtl28xxu_wr_reg(d, SYS_GPIO_OUT_EN, 0x15); 283 if (ret) 284 goto err; 285 286 /* 287 * Probe used tuner. We need to know used tuner before demod attach 288 * since there is some demod params needed to set according to tuner. 289 */ 290 291 /* demod needs some time to wake up */ 292 msleep(20); 293 294 dev->tuner_name = "NONE"; 295 296 /* open demod I2C gate */ 297 ret = rtl28xxu_ctrl_msg(d, &req_gate_open); 298 if (ret) 299 goto err; 300 301 /* check QT1010 ID(?) register; reg=0f val=2c */ 302 ret = rtl28xxu_ctrl_msg(d, &req_qt1010); 303 if (ret == 0 && buf[0] == 0x2c) { 304 dev->tuner = TUNER_RTL2830_QT1010; 305 dev->tuner_name = "QT1010"; 306 goto found; 307 } 308 309 /* open demod I2C gate */ 310 ret = rtl28xxu_ctrl_msg(d, &req_gate_open); 311 if (ret) 312 goto err; 313 314 /* check MT2060 ID register; reg=00 val=63 */ 315 ret = rtl28xxu_ctrl_msg(d, &req_mt2060); 316 if (ret == 0 && buf[0] == 0x63) { 317 dev->tuner = TUNER_RTL2830_MT2060; 318 dev->tuner_name = "MT2060"; 319 goto found; 320 } 321 322 /* assume MXL5005S */ 323 dev->tuner = TUNER_RTL2830_MXL5005S; 324 dev->tuner_name = "MXL5005S"; 325 goto found; 326 327 found: 328 dev_dbg(&d->intf->dev, "tuner=%s\n", dev->tuner_name); 329 330 return 0; 331 err: 332 dev_dbg(&d->intf->dev, "failed=%d\n", ret); 333 return ret; 334 } 335 336 static int rtl2832u_read_config(struct dvb_usb_device *d) 337 { 338 struct rtl28xxu_dev *dev = d_to_priv(d); 339 int ret; 340 u8 buf[2]; 341 /* open RTL2832U/RTL2832 I2C gate */ 342 struct rtl28xxu_req req_gate_open = {0x0120, 0x0011, 0x0001, "\x18"}; 343 /* close RTL2832U/RTL2832 I2C gate */ 344 struct rtl28xxu_req req_gate_close = {0x0120, 0x0011, 0x0001, "\x10"}; 345 /* tuner probes */ 346 struct rtl28xxu_req req_fc0012 = {0x00c6, CMD_I2C_RD, 1, buf}; 347 struct rtl28xxu_req req_fc0013 = {0x00c6, CMD_I2C_RD, 1, buf}; 348 struct rtl28xxu_req req_mt2266 = {0x00c0, CMD_I2C_RD, 1, buf}; 349 struct rtl28xxu_req req_fc2580 = {0x01ac, CMD_I2C_RD, 1, buf}; 350 struct rtl28xxu_req req_mt2063 = {0x00c0, CMD_I2C_RD, 1, buf}; 351 struct rtl28xxu_req req_max3543 = {0x00c0, CMD_I2C_RD, 1, buf}; 352 struct rtl28xxu_req req_tua9001 = {0x7ec0, CMD_I2C_RD, 2, buf}; 353 struct rtl28xxu_req req_mxl5007t = {0xd9c0, CMD_I2C_RD, 1, buf}; 354 struct rtl28xxu_req req_e4000 = {0x02c8, CMD_I2C_RD, 1, buf}; 355 struct rtl28xxu_req req_tda18272 = {0x00c0, CMD_I2C_RD, 2, buf}; 356 struct rtl28xxu_req req_r820t = {0x0034, CMD_I2C_RD, 1, buf}; 357 struct rtl28xxu_req req_r828d = {0x0074, CMD_I2C_RD, 1, buf}; 358 struct rtl28xxu_req req_mn88472 = {0xff38, CMD_I2C_RD, 1, buf}; 359 struct rtl28xxu_req req_mn88473 = {0xff38, CMD_I2C_RD, 1, buf}; 360 361 dev_dbg(&d->intf->dev, "\n"); 362 363 /* enable GPIO3 and GPIO6 as output */ 364 ret = rtl28xxu_wr_reg_mask(d, SYS_GPIO_DIR, 0x00, 0x40); 365 if (ret) 366 goto err; 367 368 ret = rtl28xxu_wr_reg_mask(d, SYS_GPIO_OUT_EN, 0x48, 0x48); 369 if (ret) 370 goto err; 371 372 /* 373 * Probe used tuner. We need to know used tuner before demod attach 374 * since there is some demod params needed to set according to tuner. 375 */ 376 377 /* open demod I2C gate */ 378 ret = rtl28xxu_ctrl_msg(d, &req_gate_open); 379 if (ret) 380 goto err; 381 382 dev->tuner_name = "NONE"; 383 384 /* check FC0012 ID register; reg=00 val=a1 */ 385 ret = rtl28xxu_ctrl_msg(d, &req_fc0012); 386 if (ret == 0 && buf[0] == 0xa1) { 387 dev->tuner = TUNER_RTL2832_FC0012; 388 dev->tuner_name = "FC0012"; 389 goto tuner_found; 390 } 391 392 /* check FC0013 ID register; reg=00 val=a3 */ 393 ret = rtl28xxu_ctrl_msg(d, &req_fc0013); 394 if (ret == 0 && buf[0] == 0xa3) { 395 dev->tuner = TUNER_RTL2832_FC0013; 396 dev->tuner_name = "FC0013"; 397 goto tuner_found; 398 } 399 400 /* check MT2266 ID register; reg=00 val=85 */ 401 ret = rtl28xxu_ctrl_msg(d, &req_mt2266); 402 if (ret == 0 && buf[0] == 0x85) { 403 dev->tuner = TUNER_RTL2832_MT2266; 404 dev->tuner_name = "MT2266"; 405 goto tuner_found; 406 } 407 408 /* check FC2580 ID register; reg=01 val=56 */ 409 ret = rtl28xxu_ctrl_msg(d, &req_fc2580); 410 if (ret == 0 && buf[0] == 0x56) { 411 dev->tuner = TUNER_RTL2832_FC2580; 412 dev->tuner_name = "FC2580"; 413 goto tuner_found; 414 } 415 416 /* check MT2063 ID register; reg=00 val=9e || 9c */ 417 ret = rtl28xxu_ctrl_msg(d, &req_mt2063); 418 if (ret == 0 && (buf[0] == 0x9e || buf[0] == 0x9c)) { 419 dev->tuner = TUNER_RTL2832_MT2063; 420 dev->tuner_name = "MT2063"; 421 goto tuner_found; 422 } 423 424 /* check MAX3543 ID register; reg=00 val=38 */ 425 ret = rtl28xxu_ctrl_msg(d, &req_max3543); 426 if (ret == 0 && buf[0] == 0x38) { 427 dev->tuner = TUNER_RTL2832_MAX3543; 428 dev->tuner_name = "MAX3543"; 429 goto tuner_found; 430 } 431 432 /* check TUA9001 ID register; reg=7e val=2328 */ 433 ret = rtl28xxu_ctrl_msg(d, &req_tua9001); 434 if (ret == 0 && buf[0] == 0x23 && buf[1] == 0x28) { 435 dev->tuner = TUNER_RTL2832_TUA9001; 436 dev->tuner_name = "TUA9001"; 437 goto tuner_found; 438 } 439 440 /* check MXL5007R ID register; reg=d9 val=14 */ 441 ret = rtl28xxu_ctrl_msg(d, &req_mxl5007t); 442 if (ret == 0 && buf[0] == 0x14) { 443 dev->tuner = TUNER_RTL2832_MXL5007T; 444 dev->tuner_name = "MXL5007T"; 445 goto tuner_found; 446 } 447 448 /* check E4000 ID register; reg=02 val=40 */ 449 ret = rtl28xxu_ctrl_msg(d, &req_e4000); 450 if (ret == 0 && buf[0] == 0x40) { 451 dev->tuner = TUNER_RTL2832_E4000; 452 dev->tuner_name = "E4000"; 453 goto tuner_found; 454 } 455 456 /* check TDA18272 ID register; reg=00 val=c760 */ 457 ret = rtl28xxu_ctrl_msg(d, &req_tda18272); 458 if (ret == 0 && (buf[0] == 0xc7 || buf[1] == 0x60)) { 459 dev->tuner = TUNER_RTL2832_TDA18272; 460 dev->tuner_name = "TDA18272"; 461 goto tuner_found; 462 } 463 464 /* check R820T ID register; reg=00 val=69 */ 465 ret = rtl28xxu_ctrl_msg(d, &req_r820t); 466 if (ret == 0 && buf[0] == 0x69) { 467 dev->tuner = TUNER_RTL2832_R820T; 468 dev->tuner_name = "R820T"; 469 goto tuner_found; 470 } 471 472 /* check R828D ID register; reg=00 val=69 */ 473 ret = rtl28xxu_ctrl_msg(d, &req_r828d); 474 if (ret == 0 && buf[0] == 0x69) { 475 dev->tuner = TUNER_RTL2832_R828D; 476 dev->tuner_name = "R828D"; 477 goto tuner_found; 478 } 479 480 tuner_found: 481 dev_dbg(&d->intf->dev, "tuner=%s\n", dev->tuner_name); 482 483 /* probe slave demod */ 484 if (dev->tuner == TUNER_RTL2832_R828D) { 485 /* power on MN88472 demod on GPIO0 */ 486 ret = rtl28xxu_wr_reg_mask(d, SYS_GPIO_OUT_VAL, 0x01, 0x01); 487 if (ret) 488 goto err; 489 490 ret = rtl28xxu_wr_reg_mask(d, SYS_GPIO_DIR, 0x00, 0x01); 491 if (ret) 492 goto err; 493 494 ret = rtl28xxu_wr_reg_mask(d, SYS_GPIO_OUT_EN, 0x01, 0x01); 495 if (ret) 496 goto err; 497 498 /* check MN88472 answers */ 499 ret = rtl28xxu_ctrl_msg(d, &req_mn88472); 500 if (ret == 0 && buf[0] == 0x02) { 501 dev_dbg(&d->intf->dev, "MN88472 found\n"); 502 dev->slave_demod = SLAVE_DEMOD_MN88472; 503 goto demod_found; 504 } 505 506 ret = rtl28xxu_ctrl_msg(d, &req_mn88473); 507 if (ret == 0 && buf[0] == 0x03) { 508 dev_dbg(&d->intf->dev, "MN88473 found\n"); 509 dev->slave_demod = SLAVE_DEMOD_MN88473; 510 goto demod_found; 511 } 512 } 513 514 demod_found: 515 /* close demod I2C gate */ 516 ret = rtl28xxu_ctrl_msg(d, &req_gate_close); 517 if (ret < 0) 518 goto err; 519 520 return 0; 521 err: 522 dev_dbg(&d->intf->dev, "failed=%d\n", ret); 523 return ret; 524 } 525 526 static int rtl28xxu_read_config(struct dvb_usb_device *d) 527 { 528 struct rtl28xxu_dev *dev = d_to_priv(d); 529 530 if (dev->chip_id == CHIP_ID_RTL2831U) 531 return rtl2831u_read_config(d); 532 else 533 return rtl2832u_read_config(d); 534 } 535 536 static int rtl28xxu_identify_state(struct dvb_usb_device *d, const char **name) 537 { 538 struct rtl28xxu_dev *dev = d_to_priv(d); 539 int ret; 540 struct rtl28xxu_req req_demod_i2c = {0x0020, CMD_I2C_DA_RD, 0, NULL}; 541 542 dev_dbg(&d->intf->dev, "\n"); 543 544 /* 545 * Detect chip type using I2C command that is not supported 546 * by old RTL2831U. 547 */ 548 ret = rtl28xxu_ctrl_msg(d, &req_demod_i2c); 549 if (ret == -EPIPE) { 550 dev->chip_id = CHIP_ID_RTL2831U; 551 } else if (ret == 0) { 552 dev->chip_id = CHIP_ID_RTL2832U; 553 } else { 554 dev_err(&d->intf->dev, "chip type detection failed %d\n", ret); 555 goto err; 556 } 557 dev_dbg(&d->intf->dev, "chip_id=%u\n", dev->chip_id); 558 559 return WARM; 560 err: 561 dev_dbg(&d->intf->dev, "failed=%d\n", ret); 562 return ret; 563 } 564 565 static const struct rtl2830_platform_data rtl2830_mt2060_platform_data = { 566 .clk = 28800000, 567 .spec_inv = 1, 568 .vtop = 0x20, 569 .krf = 0x04, 570 .agc_targ_val = 0x2d, 571 572 }; 573 574 static const struct rtl2830_platform_data rtl2830_qt1010_platform_data = { 575 .clk = 28800000, 576 .spec_inv = 1, 577 .vtop = 0x20, 578 .krf = 0x04, 579 .agc_targ_val = 0x2d, 580 }; 581 582 static const struct rtl2830_platform_data rtl2830_mxl5005s_platform_data = { 583 .clk = 28800000, 584 .spec_inv = 0, 585 .vtop = 0x3f, 586 .krf = 0x04, 587 .agc_targ_val = 0x3e, 588 }; 589 590 static int rtl2831u_frontend_attach(struct dvb_usb_adapter *adap) 591 { 592 struct dvb_usb_device *d = adap_to_d(adap); 593 struct rtl28xxu_dev *dev = d_to_priv(d); 594 struct rtl2830_platform_data *pdata = &dev->rtl2830_platform_data; 595 struct i2c_board_info board_info; 596 struct i2c_client *client; 597 int ret; 598 599 dev_dbg(&d->intf->dev, "\n"); 600 601 switch (dev->tuner) { 602 case TUNER_RTL2830_QT1010: 603 *pdata = rtl2830_qt1010_platform_data; 604 break; 605 case TUNER_RTL2830_MT2060: 606 *pdata = rtl2830_mt2060_platform_data; 607 break; 608 case TUNER_RTL2830_MXL5005S: 609 *pdata = rtl2830_mxl5005s_platform_data; 610 break; 611 default: 612 dev_err(&d->intf->dev, "unknown tuner %s\n", dev->tuner_name); 613 ret = -ENODEV; 614 goto err; 615 } 616 617 /* attach demodulator */ 618 memset(&board_info, 0, sizeof(board_info)); 619 strlcpy(board_info.type, "rtl2830", I2C_NAME_SIZE); 620 board_info.addr = 0x10; 621 board_info.platform_data = pdata; 622 request_module("%s", board_info.type); 623 client = i2c_new_device(&d->i2c_adap, &board_info); 624 if (client == NULL || client->dev.driver == NULL) { 625 ret = -ENODEV; 626 goto err; 627 } 628 629 if (!try_module_get(client->dev.driver->owner)) { 630 i2c_unregister_device(client); 631 ret = -ENODEV; 632 goto err; 633 } 634 635 adap->fe[0] = pdata->get_dvb_frontend(client); 636 dev->demod_i2c_adapter = pdata->get_i2c_adapter(client); 637 638 dev->i2c_client_demod = client; 639 640 return 0; 641 err: 642 dev_dbg(&d->intf->dev, "failed=%d\n", ret); 643 return ret; 644 } 645 646 static const struct rtl2832_platform_data rtl2832_fc0012_platform_data = { 647 .clk = 28800000, 648 .tuner = TUNER_RTL2832_FC0012 649 }; 650 651 static const struct rtl2832_platform_data rtl2832_fc0013_platform_data = { 652 .clk = 28800000, 653 .tuner = TUNER_RTL2832_FC0013 654 }; 655 656 static const struct rtl2832_platform_data rtl2832_tua9001_platform_data = { 657 .clk = 28800000, 658 .tuner = TUNER_RTL2832_TUA9001, 659 }; 660 661 static const struct rtl2832_platform_data rtl2832_e4000_platform_data = { 662 .clk = 28800000, 663 .tuner = TUNER_RTL2832_E4000, 664 }; 665 666 static const struct rtl2832_platform_data rtl2832_r820t_platform_data = { 667 .clk = 28800000, 668 .tuner = TUNER_RTL2832_R820T, 669 }; 670 671 static int rtl2832u_fc0012_tuner_callback(struct dvb_usb_device *d, 672 int cmd, int arg) 673 { 674 int ret; 675 u8 val; 676 677 dev_dbg(&d->intf->dev, "cmd=%d arg=%d\n", cmd, arg); 678 679 switch (cmd) { 680 case FC_FE_CALLBACK_VHF_ENABLE: 681 /* set output values */ 682 ret = rtl28xxu_rd_reg(d, SYS_GPIO_OUT_VAL, &val); 683 if (ret) 684 goto err; 685 686 if (arg) 687 val &= 0xbf; /* set GPIO6 low */ 688 else 689 val |= 0x40; /* set GPIO6 high */ 690 691 692 ret = rtl28xxu_wr_reg(d, SYS_GPIO_OUT_VAL, val); 693 if (ret) 694 goto err; 695 break; 696 default: 697 ret = -EINVAL; 698 goto err; 699 } 700 return 0; 701 err: 702 dev_dbg(&d->intf->dev, "failed=%d\n", ret); 703 return ret; 704 } 705 706 static int rtl2832u_tua9001_tuner_callback(struct dvb_usb_device *d, 707 int cmd, int arg) 708 { 709 int ret; 710 u8 val; 711 712 dev_dbg(&d->intf->dev, "cmd=%d arg=%d\n", cmd, arg); 713 714 /* 715 * CEN always enabled by hardware wiring 716 * RESETN GPIO4 717 * RXEN GPIO1 718 */ 719 720 switch (cmd) { 721 case TUA9001_CMD_RESETN: 722 if (arg) 723 val = (1 << 4); 724 else 725 val = (0 << 4); 726 727 ret = rtl28xxu_wr_reg_mask(d, SYS_GPIO_OUT_VAL, val, 0x10); 728 if (ret) 729 goto err; 730 break; 731 case TUA9001_CMD_RXEN: 732 if (arg) 733 val = (1 << 1); 734 else 735 val = (0 << 1); 736 737 ret = rtl28xxu_wr_reg_mask(d, SYS_GPIO_OUT_VAL, val, 0x02); 738 if (ret) 739 goto err; 740 break; 741 } 742 743 return 0; 744 err: 745 dev_dbg(&d->intf->dev, "failed=%d\n", ret); 746 return ret; 747 } 748 749 static int rtl2832u_frontend_callback(void *adapter_priv, int component, 750 int cmd, int arg) 751 { 752 struct i2c_adapter *adapter = adapter_priv; 753 struct device *parent = adapter->dev.parent; 754 struct i2c_adapter *parent_adapter; 755 struct dvb_usb_device *d; 756 struct rtl28xxu_dev *dev; 757 758 /* 759 * All tuners are connected to demod muxed I2C adapter. We have to 760 * resolve its parent adapter in order to get handle for this driver 761 * private data. That is a bit hackish solution, GPIO or direct driver 762 * callback would be better... 763 */ 764 if (parent != NULL && parent->type == &i2c_adapter_type) 765 parent_adapter = to_i2c_adapter(parent); 766 else 767 return -EINVAL; 768 769 d = i2c_get_adapdata(parent_adapter); 770 dev = d->priv; 771 772 dev_dbg(&d->intf->dev, "component=%d cmd=%d arg=%d\n", 773 component, cmd, arg); 774 775 switch (component) { 776 case DVB_FRONTEND_COMPONENT_TUNER: 777 switch (dev->tuner) { 778 case TUNER_RTL2832_FC0012: 779 return rtl2832u_fc0012_tuner_callback(d, cmd, arg); 780 case TUNER_RTL2832_TUA9001: 781 return rtl2832u_tua9001_tuner_callback(d, cmd, arg); 782 } 783 } 784 785 return 0; 786 } 787 788 static int rtl2832u_frontend_attach(struct dvb_usb_adapter *adap) 789 { 790 struct dvb_usb_device *d = adap_to_d(adap); 791 struct rtl28xxu_dev *dev = d_to_priv(d); 792 struct rtl2832_platform_data *pdata = &dev->rtl2832_platform_data; 793 struct i2c_board_info board_info; 794 struct i2c_client *client; 795 int ret; 796 797 dev_dbg(&d->intf->dev, "\n"); 798 799 switch (dev->tuner) { 800 case TUNER_RTL2832_FC0012: 801 *pdata = rtl2832_fc0012_platform_data; 802 break; 803 case TUNER_RTL2832_FC0013: 804 *pdata = rtl2832_fc0013_platform_data; 805 break; 806 case TUNER_RTL2832_FC2580: 807 /* FIXME: do not abuse fc0012 settings */ 808 *pdata = rtl2832_fc0012_platform_data; 809 break; 810 case TUNER_RTL2832_TUA9001: 811 *pdata = rtl2832_tua9001_platform_data; 812 break; 813 case TUNER_RTL2832_E4000: 814 *pdata = rtl2832_e4000_platform_data; 815 break; 816 case TUNER_RTL2832_R820T: 817 case TUNER_RTL2832_R828D: 818 *pdata = rtl2832_r820t_platform_data; 819 break; 820 default: 821 dev_err(&d->intf->dev, "unknown tuner %s\n", dev->tuner_name); 822 ret = -ENODEV; 823 goto err; 824 } 825 826 /* attach demodulator */ 827 memset(&board_info, 0, sizeof(board_info)); 828 strlcpy(board_info.type, "rtl2832", I2C_NAME_SIZE); 829 board_info.addr = 0x10; 830 board_info.platform_data = pdata; 831 request_module("%s", board_info.type); 832 client = i2c_new_device(&d->i2c_adap, &board_info); 833 if (client == NULL || client->dev.driver == NULL) { 834 ret = -ENODEV; 835 goto err; 836 } 837 838 if (!try_module_get(client->dev.driver->owner)) { 839 i2c_unregister_device(client); 840 ret = -ENODEV; 841 goto err; 842 } 843 844 adap->fe[0] = pdata->get_dvb_frontend(client); 845 dev->demod_i2c_adapter = pdata->get_i2c_adapter(client); 846 847 dev->i2c_client_demod = client; 848 849 /* set fe callback */ 850 adap->fe[0]->callback = rtl2832u_frontend_callback; 851 852 if (dev->slave_demod) { 853 struct i2c_board_info info = {}; 854 855 /* 856 * We continue on reduced mode, without DVB-T2/C, using master 857 * demod, when slave demod fails. 858 */ 859 ret = 0; 860 861 /* attach slave demodulator */ 862 if (dev->slave_demod == SLAVE_DEMOD_MN88472) { 863 struct mn88472_config mn88472_config = {}; 864 865 mn88472_config.fe = &adap->fe[1]; 866 mn88472_config.i2c_wr_max = 22, 867 strlcpy(info.type, "mn88472", I2C_NAME_SIZE); 868 mn88472_config.xtal = 20500000; 869 info.addr = 0x18; 870 info.platform_data = &mn88472_config; 871 request_module(info.type); 872 client = i2c_new_device(&d->i2c_adap, &info); 873 if (client == NULL || client->dev.driver == NULL) { 874 dev->slave_demod = SLAVE_DEMOD_NONE; 875 goto err_slave_demod_failed; 876 } 877 878 if (!try_module_get(client->dev.driver->owner)) { 879 i2c_unregister_device(client); 880 dev->slave_demod = SLAVE_DEMOD_NONE; 881 goto err_slave_demod_failed; 882 } 883 884 dev->i2c_client_slave_demod = client; 885 } else { 886 struct mn88473_config mn88473_config = {}; 887 888 mn88473_config.fe = &adap->fe[1]; 889 mn88473_config.i2c_wr_max = 22, 890 strlcpy(info.type, "mn88473", I2C_NAME_SIZE); 891 info.addr = 0x18; 892 info.platform_data = &mn88473_config; 893 request_module(info.type); 894 client = i2c_new_device(&d->i2c_adap, &info); 895 if (client == NULL || client->dev.driver == NULL) { 896 dev->slave_demod = SLAVE_DEMOD_NONE; 897 goto err_slave_demod_failed; 898 } 899 900 if (!try_module_get(client->dev.driver->owner)) { 901 i2c_unregister_device(client); 902 dev->slave_demod = SLAVE_DEMOD_NONE; 903 goto err_slave_demod_failed; 904 } 905 906 dev->i2c_client_slave_demod = client; 907 } 908 } 909 910 return 0; 911 err_slave_demod_failed: 912 err: 913 dev_dbg(&d->intf->dev, "failed=%d\n", ret); 914 return ret; 915 } 916 917 static int rtl28xxu_frontend_attach(struct dvb_usb_adapter *adap) 918 { 919 struct rtl28xxu_dev *dev = adap_to_priv(adap); 920 921 if (dev->chip_id == CHIP_ID_RTL2831U) 922 return rtl2831u_frontend_attach(adap); 923 else 924 return rtl2832u_frontend_attach(adap); 925 } 926 927 static int rtl28xxu_frontend_detach(struct dvb_usb_adapter *adap) 928 { 929 struct dvb_usb_device *d = adap_to_d(adap); 930 struct rtl28xxu_dev *dev = d_to_priv(d); 931 struct i2c_client *client; 932 933 dev_dbg(&d->intf->dev, "\n"); 934 935 /* remove I2C slave demod */ 936 client = dev->i2c_client_slave_demod; 937 if (client) { 938 module_put(client->dev.driver->owner); 939 i2c_unregister_device(client); 940 } 941 942 /* remove I2C demod */ 943 client = dev->i2c_client_demod; 944 if (client) { 945 module_put(client->dev.driver->owner); 946 i2c_unregister_device(client); 947 } 948 949 return 0; 950 } 951 952 static struct qt1010_config rtl28xxu_qt1010_config = { 953 .i2c_address = 0x62, /* 0xc4 */ 954 }; 955 956 static struct mt2060_config rtl28xxu_mt2060_config = { 957 .i2c_address = 0x60, /* 0xc0 */ 958 .clock_out = 0, 959 }; 960 961 static struct mxl5005s_config rtl28xxu_mxl5005s_config = { 962 .i2c_address = 0x63, /* 0xc6 */ 963 .if_freq = IF_FREQ_4570000HZ, 964 .xtal_freq = CRYSTAL_FREQ_16000000HZ, 965 .agc_mode = MXL_SINGLE_AGC, 966 .tracking_filter = MXL_TF_C_H, 967 .rssi_enable = MXL_RSSI_ENABLE, 968 .cap_select = MXL_CAP_SEL_ENABLE, 969 .div_out = MXL_DIV_OUT_4, 970 .clock_out = MXL_CLOCK_OUT_DISABLE, 971 .output_load = MXL5005S_IF_OUTPUT_LOAD_200_OHM, 972 .top = MXL5005S_TOP_25P2, 973 .mod_mode = MXL_DIGITAL_MODE, 974 .if_mode = MXL_ZERO_IF, 975 .AgcMasterByte = 0x00, 976 }; 977 978 static int rtl2831u_tuner_attach(struct dvb_usb_adapter *adap) 979 { 980 int ret; 981 struct dvb_usb_device *d = adap_to_d(adap); 982 struct rtl28xxu_dev *dev = d_to_priv(d); 983 struct dvb_frontend *fe; 984 985 dev_dbg(&d->intf->dev, "\n"); 986 987 switch (dev->tuner) { 988 case TUNER_RTL2830_QT1010: 989 fe = dvb_attach(qt1010_attach, adap->fe[0], 990 dev->demod_i2c_adapter, 991 &rtl28xxu_qt1010_config); 992 break; 993 case TUNER_RTL2830_MT2060: 994 fe = dvb_attach(mt2060_attach, adap->fe[0], 995 dev->demod_i2c_adapter, 996 &rtl28xxu_mt2060_config, 1220); 997 break; 998 case TUNER_RTL2830_MXL5005S: 999 fe = dvb_attach(mxl5005s_attach, adap->fe[0], 1000 dev->demod_i2c_adapter, 1001 &rtl28xxu_mxl5005s_config); 1002 break; 1003 default: 1004 fe = NULL; 1005 dev_err(&d->intf->dev, "unknown tuner %d\n", dev->tuner); 1006 } 1007 1008 if (fe == NULL) { 1009 ret = -ENODEV; 1010 goto err; 1011 } 1012 1013 return 0; 1014 err: 1015 dev_dbg(&d->intf->dev, "failed=%d\n", ret); 1016 return ret; 1017 } 1018 1019 static const struct fc2580_config rtl2832u_fc2580_config = { 1020 .i2c_addr = 0x56, 1021 .clock = 16384000, 1022 }; 1023 1024 static struct tua9001_config rtl2832u_tua9001_config = { 1025 .i2c_addr = 0x60, 1026 }; 1027 1028 static const struct fc0012_config rtl2832u_fc0012_config = { 1029 .i2c_address = 0x63, /* 0xc6 >> 1 */ 1030 .xtal_freq = FC_XTAL_28_8_MHZ, 1031 }; 1032 1033 static const struct r820t_config rtl2832u_r820t_config = { 1034 .i2c_addr = 0x1a, 1035 .xtal = 28800000, 1036 .max_i2c_msg_len = 2, 1037 .rafael_chip = CHIP_R820T, 1038 }; 1039 1040 static const struct r820t_config rtl2832u_r828d_config = { 1041 .i2c_addr = 0x3a, 1042 .xtal = 16000000, 1043 .max_i2c_msg_len = 2, 1044 .rafael_chip = CHIP_R828D, 1045 }; 1046 1047 static int rtl2832u_tuner_attach(struct dvb_usb_adapter *adap) 1048 { 1049 int ret; 1050 struct dvb_usb_device *d = adap_to_d(adap); 1051 struct rtl28xxu_dev *dev = d_to_priv(d); 1052 struct dvb_frontend *fe = NULL; 1053 struct i2c_board_info info; 1054 struct i2c_client *client; 1055 struct v4l2_subdev *subdev = NULL; 1056 struct platform_device *pdev; 1057 struct rtl2832_sdr_platform_data pdata; 1058 1059 dev_dbg(&d->intf->dev, "\n"); 1060 1061 memset(&info, 0, sizeof(struct i2c_board_info)); 1062 memset(&pdata, 0, sizeof(pdata)); 1063 1064 switch (dev->tuner) { 1065 case TUNER_RTL2832_FC0012: 1066 fe = dvb_attach(fc0012_attach, adap->fe[0], 1067 dev->demod_i2c_adapter, &rtl2832u_fc0012_config); 1068 1069 /* since fc0012 includs reading the signal strength delegate 1070 * that to the tuner driver */ 1071 adap->fe[0]->ops.read_signal_strength = 1072 adap->fe[0]->ops.tuner_ops.get_rf_strength; 1073 break; 1074 case TUNER_RTL2832_FC0013: 1075 fe = dvb_attach(fc0013_attach, adap->fe[0], 1076 dev->demod_i2c_adapter, 0xc6>>1, 0, FC_XTAL_28_8_MHZ); 1077 1078 /* fc0013 also supports signal strength reading */ 1079 adap->fe[0]->ops.read_signal_strength = 1080 adap->fe[0]->ops.tuner_ops.get_rf_strength; 1081 break; 1082 case TUNER_RTL2832_E4000: { 1083 struct e4000_config e4000_config = { 1084 .fe = adap->fe[0], 1085 .clock = 28800000, 1086 }; 1087 1088 strlcpy(info.type, "e4000", I2C_NAME_SIZE); 1089 info.addr = 0x64; 1090 info.platform_data = &e4000_config; 1091 1092 request_module(info.type); 1093 client = i2c_new_device(dev->demod_i2c_adapter, &info); 1094 if (client == NULL || client->dev.driver == NULL) 1095 break; 1096 1097 if (!try_module_get(client->dev.driver->owner)) { 1098 i2c_unregister_device(client); 1099 break; 1100 } 1101 1102 dev->i2c_client_tuner = client; 1103 subdev = i2c_get_clientdata(client); 1104 } 1105 break; 1106 case TUNER_RTL2832_FC2580: 1107 fe = dvb_attach(fc2580_attach, adap->fe[0], 1108 dev->demod_i2c_adapter, 1109 &rtl2832u_fc2580_config); 1110 break; 1111 case TUNER_RTL2832_TUA9001: 1112 /* enable GPIO1 and GPIO4 as output */ 1113 ret = rtl28xxu_wr_reg_mask(d, SYS_GPIO_DIR, 0x00, 0x12); 1114 if (ret) 1115 goto err; 1116 1117 ret = rtl28xxu_wr_reg_mask(d, SYS_GPIO_OUT_EN, 0x12, 0x12); 1118 if (ret) 1119 goto err; 1120 1121 fe = dvb_attach(tua9001_attach, adap->fe[0], 1122 dev->demod_i2c_adapter, 1123 &rtl2832u_tua9001_config); 1124 break; 1125 case TUNER_RTL2832_R820T: 1126 fe = dvb_attach(r820t_attach, adap->fe[0], 1127 dev->demod_i2c_adapter, 1128 &rtl2832u_r820t_config); 1129 1130 /* Use tuner to get the signal strength */ 1131 adap->fe[0]->ops.read_signal_strength = 1132 adap->fe[0]->ops.tuner_ops.get_rf_strength; 1133 break; 1134 case TUNER_RTL2832_R828D: 1135 fe = dvb_attach(r820t_attach, adap->fe[0], 1136 dev->demod_i2c_adapter, 1137 &rtl2832u_r828d_config); 1138 adap->fe[0]->ops.read_signal_strength = 1139 adap->fe[0]->ops.tuner_ops.get_rf_strength; 1140 1141 if (adap->fe[1]) { 1142 fe = dvb_attach(r820t_attach, adap->fe[1], 1143 dev->demod_i2c_adapter, 1144 &rtl2832u_r828d_config); 1145 adap->fe[1]->ops.read_signal_strength = 1146 adap->fe[1]->ops.tuner_ops.get_rf_strength; 1147 } 1148 break; 1149 default: 1150 dev_err(&d->intf->dev, "unknown tuner %d\n", dev->tuner); 1151 } 1152 if (fe == NULL && dev->i2c_client_tuner == NULL) { 1153 ret = -ENODEV; 1154 goto err; 1155 } 1156 1157 /* register SDR */ 1158 switch (dev->tuner) { 1159 case TUNER_RTL2832_FC0012: 1160 case TUNER_RTL2832_FC0013: 1161 case TUNER_RTL2832_E4000: 1162 case TUNER_RTL2832_R820T: 1163 case TUNER_RTL2832_R828D: 1164 pdata.clk = dev->rtl2832_platform_data.clk; 1165 pdata.tuner = dev->tuner; 1166 pdata.i2c_client = dev->i2c_client_demod; 1167 pdata.bulk_read = dev->rtl2832_platform_data.bulk_read; 1168 pdata.bulk_write = dev->rtl2832_platform_data.bulk_write; 1169 pdata.update_bits = dev->rtl2832_platform_data.update_bits; 1170 pdata.dvb_frontend = adap->fe[0]; 1171 pdata.dvb_usb_device = d; 1172 pdata.v4l2_subdev = subdev; 1173 1174 request_module("%s", "rtl2832_sdr"); 1175 pdev = platform_device_register_data(&d->intf->dev, 1176 "rtl2832_sdr", 1177 PLATFORM_DEVID_AUTO, 1178 &pdata, sizeof(pdata)); 1179 if (pdev == NULL || pdev->dev.driver == NULL) 1180 break; 1181 dev->platform_device_sdr = pdev; 1182 break; 1183 default: 1184 dev_dbg(&d->intf->dev, "no SDR for tuner=%d\n", dev->tuner); 1185 } 1186 1187 return 0; 1188 err: 1189 dev_dbg(&d->intf->dev, "failed=%d\n", ret); 1190 return ret; 1191 } 1192 1193 static int rtl28xxu_tuner_attach(struct dvb_usb_adapter *adap) 1194 { 1195 struct rtl28xxu_dev *dev = adap_to_priv(adap); 1196 1197 if (dev->chip_id == CHIP_ID_RTL2831U) 1198 return rtl2831u_tuner_attach(adap); 1199 else 1200 return rtl2832u_tuner_attach(adap); 1201 } 1202 1203 static int rtl28xxu_tuner_detach(struct dvb_usb_adapter *adap) 1204 { 1205 struct dvb_usb_device *d = adap_to_d(adap); 1206 struct rtl28xxu_dev *dev = d_to_priv(d); 1207 struct i2c_client *client; 1208 struct platform_device *pdev; 1209 1210 dev_dbg(&d->intf->dev, "\n"); 1211 1212 /* remove platform SDR */ 1213 pdev = dev->platform_device_sdr; 1214 if (pdev) 1215 platform_device_unregister(pdev); 1216 1217 /* remove I2C tuner */ 1218 client = dev->i2c_client_tuner; 1219 if (client) { 1220 module_put(client->dev.driver->owner); 1221 i2c_unregister_device(client); 1222 } 1223 1224 return 0; 1225 } 1226 1227 static int rtl28xxu_init(struct dvb_usb_device *d) 1228 { 1229 int ret; 1230 u8 val; 1231 1232 dev_dbg(&d->intf->dev, "\n"); 1233 1234 /* init USB endpoints */ 1235 ret = rtl28xxu_rd_reg(d, USB_SYSCTL_0, &val); 1236 if (ret) 1237 goto err; 1238 1239 /* enable DMA and Full Packet Mode*/ 1240 val |= 0x09; 1241 ret = rtl28xxu_wr_reg(d, USB_SYSCTL_0, val); 1242 if (ret) 1243 goto err; 1244 1245 /* set EPA maximum packet size to 0x0200 */ 1246 ret = rtl28xxu_wr_regs(d, USB_EPA_MAXPKT, "\x00\x02\x00\x00", 4); 1247 if (ret) 1248 goto err; 1249 1250 /* change EPA FIFO length */ 1251 ret = rtl28xxu_wr_regs(d, USB_EPA_FIFO_CFG, "\x14\x00\x00\x00", 4); 1252 if (ret) 1253 goto err; 1254 1255 return ret; 1256 err: 1257 dev_dbg(&d->intf->dev, "failed=%d\n", ret); 1258 return ret; 1259 } 1260 1261 static int rtl2831u_power_ctrl(struct dvb_usb_device *d, int onoff) 1262 { 1263 int ret; 1264 u8 gpio, sys0, epa_ctl[2]; 1265 1266 dev_dbg(&d->intf->dev, "onoff=%d\n", onoff); 1267 1268 /* demod adc */ 1269 ret = rtl28xxu_rd_reg(d, SYS_SYS0, &sys0); 1270 if (ret) 1271 goto err; 1272 1273 /* tuner power, read GPIOs */ 1274 ret = rtl28xxu_rd_reg(d, SYS_GPIO_OUT_VAL, &gpio); 1275 if (ret) 1276 goto err; 1277 1278 dev_dbg(&d->intf->dev, "RD SYS0=%02x GPIO_OUT_VAL=%02x\n", sys0, gpio); 1279 1280 if (onoff) { 1281 gpio |= 0x01; /* GPIO0 = 1 */ 1282 gpio &= (~0x10); /* GPIO4 = 0 */ 1283 gpio |= 0x04; /* GPIO2 = 1, LED on */ 1284 sys0 = sys0 & 0x0f; 1285 sys0 |= 0xe0; 1286 epa_ctl[0] = 0x00; /* clear stall */ 1287 epa_ctl[1] = 0x00; /* clear reset */ 1288 } else { 1289 gpio &= (~0x01); /* GPIO0 = 0 */ 1290 gpio |= 0x10; /* GPIO4 = 1 */ 1291 gpio &= (~0x04); /* GPIO2 = 1, LED off */ 1292 sys0 = sys0 & (~0xc0); 1293 epa_ctl[0] = 0x10; /* set stall */ 1294 epa_ctl[1] = 0x02; /* set reset */ 1295 } 1296 1297 dev_dbg(&d->intf->dev, "WR SYS0=%02x GPIO_OUT_VAL=%02x\n", sys0, gpio); 1298 1299 /* demod adc */ 1300 ret = rtl28xxu_wr_reg(d, SYS_SYS0, sys0); 1301 if (ret) 1302 goto err; 1303 1304 /* tuner power, write GPIOs */ 1305 ret = rtl28xxu_wr_reg(d, SYS_GPIO_OUT_VAL, gpio); 1306 if (ret) 1307 goto err; 1308 1309 /* streaming EP: stall & reset */ 1310 ret = rtl28xxu_wr_regs(d, USB_EPA_CTL, epa_ctl, 2); 1311 if (ret) 1312 goto err; 1313 1314 if (onoff) 1315 usb_clear_halt(d->udev, usb_rcvbulkpipe(d->udev, 0x81)); 1316 1317 return ret; 1318 err: 1319 dev_dbg(&d->intf->dev, "failed=%d\n", ret); 1320 return ret; 1321 } 1322 1323 static int rtl2832u_power_ctrl(struct dvb_usb_device *d, int onoff) 1324 { 1325 int ret; 1326 1327 dev_dbg(&d->intf->dev, "onoff=%d\n", onoff); 1328 1329 if (onoff) { 1330 /* GPIO3=1, GPIO4=0 */ 1331 ret = rtl28xxu_wr_reg_mask(d, SYS_GPIO_OUT_VAL, 0x08, 0x18); 1332 if (ret) 1333 goto err; 1334 1335 /* suspend? */ 1336 ret = rtl28xxu_wr_reg_mask(d, SYS_DEMOD_CTL1, 0x00, 0x10); 1337 if (ret) 1338 goto err; 1339 1340 /* enable PLL */ 1341 ret = rtl28xxu_wr_reg_mask(d, SYS_DEMOD_CTL, 0x80, 0x80); 1342 if (ret) 1343 goto err; 1344 1345 /* disable reset */ 1346 ret = rtl28xxu_wr_reg_mask(d, SYS_DEMOD_CTL, 0x20, 0x20); 1347 if (ret) 1348 goto err; 1349 1350 /* streaming EP: clear stall & reset */ 1351 ret = rtl28xxu_wr_regs(d, USB_EPA_CTL, "\x00\x00", 2); 1352 if (ret) 1353 goto err; 1354 1355 ret = usb_clear_halt(d->udev, usb_rcvbulkpipe(d->udev, 0x81)); 1356 if (ret) 1357 goto err; 1358 } else { 1359 /* GPIO4=1 */ 1360 ret = rtl28xxu_wr_reg_mask(d, SYS_GPIO_OUT_VAL, 0x10, 0x10); 1361 if (ret) 1362 goto err; 1363 1364 /* disable PLL */ 1365 ret = rtl28xxu_wr_reg_mask(d, SYS_DEMOD_CTL, 0x00, 0x80); 1366 if (ret) 1367 goto err; 1368 1369 /* streaming EP: set stall & reset */ 1370 ret = rtl28xxu_wr_regs(d, USB_EPA_CTL, "\x10\x02", 2); 1371 if (ret) 1372 goto err; 1373 } 1374 1375 return ret; 1376 err: 1377 dev_dbg(&d->intf->dev, "failed=%d\n", ret); 1378 return ret; 1379 } 1380 1381 static int rtl28xxu_power_ctrl(struct dvb_usb_device *d, int onoff) 1382 { 1383 struct rtl28xxu_dev *dev = d_to_priv(d); 1384 1385 if (dev->chip_id == CHIP_ID_RTL2831U) 1386 return rtl2831u_power_ctrl(d, onoff); 1387 else 1388 return rtl2832u_power_ctrl(d, onoff); 1389 } 1390 1391 static int rtl28xxu_frontend_ctrl(struct dvb_frontend *fe, int onoff) 1392 { 1393 struct dvb_usb_device *d = fe_to_d(fe); 1394 struct rtl28xxu_dev *dev = fe_to_priv(fe); 1395 struct rtl2832_platform_data *pdata = &dev->rtl2832_platform_data; 1396 int ret; 1397 u8 val; 1398 1399 dev_dbg(&d->intf->dev, "fe=%d onoff=%d\n", fe->id, onoff); 1400 1401 if (dev->chip_id == CHIP_ID_RTL2831U) 1402 return 0; 1403 1404 /* control internal demod ADC */ 1405 if (fe->id == 0 && onoff) 1406 val = 0x48; /* enable ADC */ 1407 else 1408 val = 0x00; /* disable ADC */ 1409 1410 ret = rtl28xxu_wr_reg_mask(d, SYS_DEMOD_CTL, val, 0x48); 1411 if (ret) 1412 goto err; 1413 1414 /* bypass slave demod TS through master demod */ 1415 if (fe->id == 1 && onoff) { 1416 ret = pdata->enable_slave_ts(dev->i2c_client_demod); 1417 if (ret) 1418 goto err; 1419 } 1420 1421 return 0; 1422 err: 1423 dev_dbg(&d->intf->dev, "failed=%d\n", ret); 1424 return ret; 1425 } 1426 1427 #if IS_ENABLED(CONFIG_RC_CORE) 1428 static int rtl2831u_rc_query(struct dvb_usb_device *d) 1429 { 1430 int ret, i; 1431 struct rtl28xxu_dev *dev = d->priv; 1432 u8 buf[5]; 1433 u32 rc_code; 1434 struct rtl28xxu_reg_val rc_nec_tab[] = { 1435 { 0x3033, 0x80 }, 1436 { 0x3020, 0x43 }, 1437 { 0x3021, 0x16 }, 1438 { 0x3022, 0x16 }, 1439 { 0x3023, 0x5a }, 1440 { 0x3024, 0x2d }, 1441 { 0x3025, 0x16 }, 1442 { 0x3026, 0x01 }, 1443 { 0x3028, 0xb0 }, 1444 { 0x3029, 0x04 }, 1445 { 0x302c, 0x88 }, 1446 { 0x302e, 0x13 }, 1447 { 0x3030, 0xdf }, 1448 { 0x3031, 0x05 }, 1449 }; 1450 1451 /* init remote controller */ 1452 if (!dev->rc_active) { 1453 for (i = 0; i < ARRAY_SIZE(rc_nec_tab); i++) { 1454 ret = rtl28xxu_wr_reg(d, rc_nec_tab[i].reg, 1455 rc_nec_tab[i].val); 1456 if (ret) 1457 goto err; 1458 } 1459 dev->rc_active = true; 1460 } 1461 1462 ret = rtl28xxu_rd_regs(d, SYS_IRRC_RP, buf, 5); 1463 if (ret) 1464 goto err; 1465 1466 if (buf[4] & 0x01) { 1467 if (buf[2] == (u8) ~buf[3]) { 1468 if (buf[0] == (u8) ~buf[1]) { 1469 /* NEC standard (16 bit) */ 1470 rc_code = RC_SCANCODE_NEC(buf[0], buf[2]); 1471 } else { 1472 /* NEC extended (24 bit) */ 1473 rc_code = RC_SCANCODE_NECX(buf[0] << 8 | buf[1], 1474 buf[2]); 1475 } 1476 } else { 1477 /* NEC full (32 bit) */ 1478 rc_code = RC_SCANCODE_NEC32(buf[0] << 24 | buf[1] << 16 | 1479 buf[2] << 8 | buf[3]); 1480 } 1481 1482 rc_keydown(d->rc_dev, RC_TYPE_NEC, rc_code, 0); 1483 1484 ret = rtl28xxu_wr_reg(d, SYS_IRRC_SR, 1); 1485 if (ret) 1486 goto err; 1487 1488 /* repeated intentionally to avoid extra keypress */ 1489 ret = rtl28xxu_wr_reg(d, SYS_IRRC_SR, 1); 1490 if (ret) 1491 goto err; 1492 } 1493 1494 return ret; 1495 err: 1496 dev_dbg(&d->intf->dev, "failed=%d\n", ret); 1497 return ret; 1498 } 1499 1500 static int rtl2831u_get_rc_config(struct dvb_usb_device *d, 1501 struct dvb_usb_rc *rc) 1502 { 1503 rc->map_name = RC_MAP_EMPTY; 1504 rc->allowed_protos = RC_BIT_NEC; 1505 rc->query = rtl2831u_rc_query; 1506 rc->interval = 400; 1507 1508 return 0; 1509 } 1510 1511 static int rtl2832u_rc_query(struct dvb_usb_device *d) 1512 { 1513 int ret, i, len; 1514 struct rtl28xxu_dev *dev = d->priv; 1515 struct ir_raw_event ev; 1516 u8 buf[128]; 1517 static const struct rtl28xxu_reg_val_mask refresh_tab[] = { 1518 {IR_RX_IF, 0x03, 0xff}, 1519 {IR_RX_BUF_CTRL, 0x80, 0xff}, 1520 {IR_RX_CTRL, 0x80, 0xff}, 1521 }; 1522 1523 /* init remote controller */ 1524 if (!dev->rc_active) { 1525 static const struct rtl28xxu_reg_val_mask init_tab[] = { 1526 {SYS_DEMOD_CTL1, 0x00, 0x04}, 1527 {SYS_DEMOD_CTL1, 0x00, 0x08}, 1528 {USB_CTRL, 0x20, 0x20}, 1529 {SYS_GPIO_DIR, 0x00, 0x08}, 1530 {SYS_GPIO_OUT_EN, 0x08, 0x08}, 1531 {SYS_GPIO_OUT_VAL, 0x08, 0x08}, 1532 {IR_MAX_DURATION0, 0xd0, 0xff}, 1533 {IR_MAX_DURATION1, 0x07, 0xff}, 1534 {IR_IDLE_LEN0, 0xc0, 0xff}, 1535 {IR_IDLE_LEN1, 0x00, 0xff}, 1536 {IR_GLITCH_LEN, 0x03, 0xff}, 1537 {IR_RX_CLK, 0x09, 0xff}, 1538 {IR_RX_CFG, 0x1c, 0xff}, 1539 {IR_MAX_H_TOL_LEN, 0x1e, 0xff}, 1540 {IR_MAX_L_TOL_LEN, 0x1e, 0xff}, 1541 {IR_RX_CTRL, 0x80, 0xff}, 1542 }; 1543 1544 for (i = 0; i < ARRAY_SIZE(init_tab); i++) { 1545 ret = rtl28xxu_wr_reg_mask(d, init_tab[i].reg, 1546 init_tab[i].val, init_tab[i].mask); 1547 if (ret) 1548 goto err; 1549 } 1550 1551 dev->rc_active = true; 1552 } 1553 1554 ret = rtl28xxu_rd_reg(d, IR_RX_IF, &buf[0]); 1555 if (ret) 1556 goto err; 1557 1558 if (buf[0] != 0x83) 1559 goto exit; 1560 1561 ret = rtl28xxu_rd_reg(d, IR_RX_BC, &buf[0]); 1562 if (ret) 1563 goto err; 1564 1565 len = buf[0]; 1566 1567 /* read raw code from hw */ 1568 ret = rtl28xxu_rd_regs(d, IR_RX_BUF, buf, len); 1569 if (ret) 1570 goto err; 1571 1572 /* let hw receive new code */ 1573 for (i = 0; i < ARRAY_SIZE(refresh_tab); i++) { 1574 ret = rtl28xxu_wr_reg_mask(d, refresh_tab[i].reg, 1575 refresh_tab[i].val, refresh_tab[i].mask); 1576 if (ret) 1577 goto err; 1578 } 1579 1580 /* pass data to Kernel IR decoder */ 1581 init_ir_raw_event(&ev); 1582 1583 for (i = 0; i < len; i++) { 1584 ev.pulse = buf[i] >> 7; 1585 ev.duration = 50800 * (buf[i] & 0x7f); 1586 ir_raw_event_store_with_filter(d->rc_dev, &ev); 1587 } 1588 1589 /* 'flush' ir_raw_event_store_with_filter() */ 1590 ir_raw_event_set_idle(d->rc_dev, true); 1591 ir_raw_event_handle(d->rc_dev); 1592 exit: 1593 return ret; 1594 err: 1595 dev_dbg(&d->intf->dev, "failed=%d\n", ret); 1596 return ret; 1597 } 1598 1599 static int rtl2832u_get_rc_config(struct dvb_usb_device *d, 1600 struct dvb_usb_rc *rc) 1601 { 1602 /* disable IR interrupts in order to avoid SDR sample loss */ 1603 if (rtl28xxu_disable_rc) 1604 return rtl28xxu_wr_reg(d, IR_RX_IE, 0x00); 1605 1606 /* load empty to enable rc */ 1607 if (!rc->map_name) 1608 rc->map_name = RC_MAP_EMPTY; 1609 rc->allowed_protos = RC_BIT_ALL; 1610 rc->driver_type = RC_DRIVER_IR_RAW; 1611 rc->query = rtl2832u_rc_query; 1612 rc->interval = 400; 1613 1614 return 0; 1615 } 1616 1617 static int rtl28xxu_get_rc_config(struct dvb_usb_device *d, 1618 struct dvb_usb_rc *rc) 1619 { 1620 struct rtl28xxu_dev *dev = d_to_priv(d); 1621 1622 if (dev->chip_id == CHIP_ID_RTL2831U) 1623 return rtl2831u_get_rc_config(d, rc); 1624 else 1625 return rtl2832u_get_rc_config(d, rc); 1626 } 1627 #else 1628 #define rtl28xxu_get_rc_config NULL 1629 #endif 1630 1631 static int rtl28xxu_pid_filter_ctrl(struct dvb_usb_adapter *adap, int onoff) 1632 { 1633 struct rtl28xxu_dev *dev = adap_to_priv(adap); 1634 1635 if (dev->chip_id == CHIP_ID_RTL2831U) { 1636 struct rtl2830_platform_data *pdata = &dev->rtl2830_platform_data; 1637 1638 return pdata->pid_filter_ctrl(adap->fe[0], onoff); 1639 } else { 1640 struct rtl2832_platform_data *pdata = &dev->rtl2832_platform_data; 1641 1642 return pdata->pid_filter_ctrl(adap->fe[0], onoff); 1643 } 1644 } 1645 1646 static int rtl28xxu_pid_filter(struct dvb_usb_adapter *adap, int index, 1647 u16 pid, int onoff) 1648 { 1649 struct rtl28xxu_dev *dev = adap_to_priv(adap); 1650 1651 if (dev->chip_id == CHIP_ID_RTL2831U) { 1652 struct rtl2830_platform_data *pdata = &dev->rtl2830_platform_data; 1653 1654 return pdata->pid_filter(adap->fe[0], index, pid, onoff); 1655 } else { 1656 struct rtl2832_platform_data *pdata = &dev->rtl2832_platform_data; 1657 1658 return pdata->pid_filter(adap->fe[0], index, pid, onoff); 1659 } 1660 } 1661 1662 static const struct dvb_usb_device_properties rtl28xxu_props = { 1663 .driver_name = KBUILD_MODNAME, 1664 .owner = THIS_MODULE, 1665 .adapter_nr = adapter_nr, 1666 .size_of_priv = sizeof(struct rtl28xxu_dev), 1667 1668 .identify_state = rtl28xxu_identify_state, 1669 .power_ctrl = rtl28xxu_power_ctrl, 1670 .frontend_ctrl = rtl28xxu_frontend_ctrl, 1671 .i2c_algo = &rtl28xxu_i2c_algo, 1672 .read_config = rtl28xxu_read_config, 1673 .frontend_attach = rtl28xxu_frontend_attach, 1674 .frontend_detach = rtl28xxu_frontend_detach, 1675 .tuner_attach = rtl28xxu_tuner_attach, 1676 .tuner_detach = rtl28xxu_tuner_detach, 1677 .init = rtl28xxu_init, 1678 .get_rc_config = rtl28xxu_get_rc_config, 1679 1680 .num_adapters = 1, 1681 .adapter = { 1682 { 1683 .caps = DVB_USB_ADAP_HAS_PID_FILTER | 1684 DVB_USB_ADAP_PID_FILTER_CAN_BE_TURNED_OFF, 1685 1686 .pid_filter_count = 32, 1687 .pid_filter_ctrl = rtl28xxu_pid_filter_ctrl, 1688 .pid_filter = rtl28xxu_pid_filter, 1689 1690 .stream = DVB_USB_STREAM_BULK(0x81, 6, 8 * 512), 1691 }, 1692 }, 1693 }; 1694 1695 static const struct usb_device_id rtl28xxu_id_table[] = { 1696 /* RTL2831U devices: */ 1697 { DVB_USB_DEVICE(USB_VID_REALTEK, USB_PID_REALTEK_RTL2831U, 1698 &rtl28xxu_props, "Realtek RTL2831U reference design", NULL) }, 1699 { DVB_USB_DEVICE(USB_VID_WIDEVIEW, USB_PID_FREECOM_DVBT, 1700 &rtl28xxu_props, "Freecom USB2.0 DVB-T", NULL) }, 1701 { DVB_USB_DEVICE(USB_VID_WIDEVIEW, USB_PID_FREECOM_DVBT_2, 1702 &rtl28xxu_props, "Freecom USB2.0 DVB-T", NULL) }, 1703 1704 /* RTL2832U devices: */ 1705 { DVB_USB_DEVICE(USB_VID_REALTEK, 0x2832, 1706 &rtl28xxu_props, "Realtek RTL2832U reference design", NULL) }, 1707 { DVB_USB_DEVICE(USB_VID_REALTEK, 0x2838, 1708 &rtl28xxu_props, "Realtek RTL2832U reference design", NULL) }, 1709 { DVB_USB_DEVICE(USB_VID_TERRATEC, USB_PID_TERRATEC_CINERGY_T_STICK_BLACK_REV1, 1710 &rtl28xxu_props, "TerraTec Cinergy T Stick Black", RC_MAP_TERRATEC_SLIM) }, 1711 { DVB_USB_DEVICE(USB_VID_GTEK, USB_PID_DELOCK_USB2_DVBT, 1712 &rtl28xxu_props, "G-Tek Electronics Group Lifeview LV5TDLX DVB-T", NULL) }, 1713 { DVB_USB_DEVICE(USB_VID_TERRATEC, USB_PID_NOXON_DAB_STICK, 1714 &rtl28xxu_props, "TerraTec NOXON DAB Stick", NULL) }, 1715 { DVB_USB_DEVICE(USB_VID_TERRATEC, USB_PID_NOXON_DAB_STICK_REV2, 1716 &rtl28xxu_props, "TerraTec NOXON DAB Stick (rev 2)", NULL) }, 1717 { DVB_USB_DEVICE(USB_VID_TERRATEC, USB_PID_NOXON_DAB_STICK_REV3, 1718 &rtl28xxu_props, "TerraTec NOXON DAB Stick (rev 3)", NULL) }, 1719 { DVB_USB_DEVICE(USB_VID_GTEK, USB_PID_TREKSTOR_TERRES_2_0, 1720 &rtl28xxu_props, "Trekstor DVB-T Stick Terres 2.0", NULL) }, 1721 { DVB_USB_DEVICE(USB_VID_DEXATEK, 0x1101, 1722 &rtl28xxu_props, "Dexatek DK DVB-T Dongle", NULL) }, 1723 { DVB_USB_DEVICE(USB_VID_LEADTEK, 0x6680, 1724 &rtl28xxu_props, "DigitalNow Quad DVB-T Receiver", NULL) }, 1725 { DVB_USB_DEVICE(USB_VID_LEADTEK, USB_PID_WINFAST_DTV_DONGLE_MINID, 1726 &rtl28xxu_props, "Leadtek Winfast DTV Dongle Mini D", NULL) }, 1727 { DVB_USB_DEVICE(USB_VID_TERRATEC, 0x00d3, 1728 &rtl28xxu_props, "TerraTec Cinergy T Stick RC (Rev. 3)", NULL) }, 1729 { DVB_USB_DEVICE(USB_VID_DEXATEK, 0x1102, 1730 &rtl28xxu_props, "Dexatek DK mini DVB-T Dongle", NULL) }, 1731 { DVB_USB_DEVICE(USB_VID_TERRATEC, 0x00d7, 1732 &rtl28xxu_props, "TerraTec Cinergy T Stick+", NULL) }, 1733 { DVB_USB_DEVICE(USB_VID_KWORLD_2, 0xd3a8, 1734 &rtl28xxu_props, "ASUS My Cinema-U3100Mini Plus V2", NULL) }, 1735 { DVB_USB_DEVICE(USB_VID_KWORLD_2, 0xd393, 1736 &rtl28xxu_props, "GIGABYTE U7300", NULL) }, 1737 { DVB_USB_DEVICE(USB_VID_DEXATEK, 0x1104, 1738 &rtl28xxu_props, "MSI DIGIVOX Micro HD", NULL) }, 1739 { DVB_USB_DEVICE(USB_VID_COMPRO, 0x0620, 1740 &rtl28xxu_props, "Compro VideoMate U620F", NULL) }, 1741 { DVB_USB_DEVICE(USB_VID_KWORLD_2, 0xd394, 1742 &rtl28xxu_props, "MaxMedia HU394-T", NULL) }, 1743 { DVB_USB_DEVICE(USB_VID_LEADTEK, 0x6a03, 1744 &rtl28xxu_props, "Leadtek WinFast DTV Dongle mini", NULL) }, 1745 { DVB_USB_DEVICE(USB_VID_GTEK, USB_PID_CPYTO_REDI_PC50A, 1746 &rtl28xxu_props, "Crypto ReDi PC 50 A", NULL) }, 1747 { DVB_USB_DEVICE(USB_VID_KYE, 0x707f, 1748 &rtl28xxu_props, "Genius TVGo DVB-T03", NULL) }, 1749 { DVB_USB_DEVICE(USB_VID_KWORLD_2, 0xd395, 1750 &rtl28xxu_props, "Peak DVB-T USB", NULL) }, 1751 { DVB_USB_DEVICE(USB_VID_KWORLD_2, USB_PID_SVEON_STV20_RTL2832U, 1752 &rtl28xxu_props, "Sveon STV20", NULL) }, 1753 { DVB_USB_DEVICE(USB_VID_KWORLD_2, USB_PID_SVEON_STV21, 1754 &rtl28xxu_props, "Sveon STV21", NULL) }, 1755 { DVB_USB_DEVICE(USB_VID_KWORLD_2, USB_PID_SVEON_STV27, 1756 &rtl28xxu_props, "Sveon STV27", NULL) }, 1757 1758 /* RTL2832P devices: */ 1759 { DVB_USB_DEVICE(USB_VID_HANFTEK, 0x0131, 1760 &rtl28xxu_props, "Astrometa DVB-T2", NULL) }, 1761 { } 1762 }; 1763 MODULE_DEVICE_TABLE(usb, rtl28xxu_id_table); 1764 1765 static struct usb_driver rtl28xxu_usb_driver = { 1766 .name = KBUILD_MODNAME, 1767 .id_table = rtl28xxu_id_table, 1768 .probe = dvb_usbv2_probe, 1769 .disconnect = dvb_usbv2_disconnect, 1770 .suspend = dvb_usbv2_suspend, 1771 .resume = dvb_usbv2_resume, 1772 .reset_resume = dvb_usbv2_reset_resume, 1773 .no_dynamic_id = 1, 1774 .soft_unbind = 1, 1775 }; 1776 1777 module_usb_driver(rtl28xxu_usb_driver); 1778 1779 MODULE_DESCRIPTION("Realtek RTL28xxU DVB USB driver"); 1780 MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>"); 1781 MODULE_AUTHOR("Thomas Mair <thomas.mair86@googlemail.com>"); 1782 MODULE_LICENSE("GPL"); 1783