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 default: 784 return -EINVAL; 785 } 786 787 return 0; 788 } 789 790 static int rtl2832u_frontend_attach(struct dvb_usb_adapter *adap) 791 { 792 struct dvb_usb_device *d = adap_to_d(adap); 793 struct rtl28xxu_dev *dev = d_to_priv(d); 794 struct rtl2832_platform_data *pdata = &dev->rtl2832_platform_data; 795 struct i2c_board_info board_info; 796 struct i2c_client *client; 797 int ret; 798 799 dev_dbg(&d->intf->dev, "\n"); 800 801 switch (dev->tuner) { 802 case TUNER_RTL2832_FC0012: 803 *pdata = rtl2832_fc0012_platform_data; 804 break; 805 case TUNER_RTL2832_FC0013: 806 *pdata = rtl2832_fc0013_platform_data; 807 break; 808 case TUNER_RTL2832_FC2580: 809 /* FIXME: do not abuse fc0012 settings */ 810 *pdata = rtl2832_fc0012_platform_data; 811 break; 812 case TUNER_RTL2832_TUA9001: 813 *pdata = rtl2832_tua9001_platform_data; 814 break; 815 case TUNER_RTL2832_E4000: 816 *pdata = rtl2832_e4000_platform_data; 817 break; 818 case TUNER_RTL2832_R820T: 819 case TUNER_RTL2832_R828D: 820 *pdata = rtl2832_r820t_platform_data; 821 break; 822 default: 823 dev_err(&d->intf->dev, "unknown tuner %s\n", dev->tuner_name); 824 ret = -ENODEV; 825 goto err; 826 } 827 828 /* attach demodulator */ 829 memset(&board_info, 0, sizeof(board_info)); 830 strlcpy(board_info.type, "rtl2832", I2C_NAME_SIZE); 831 board_info.addr = 0x10; 832 board_info.platform_data = pdata; 833 request_module("%s", board_info.type); 834 client = i2c_new_device(&d->i2c_adap, &board_info); 835 if (client == NULL || client->dev.driver == NULL) { 836 ret = -ENODEV; 837 goto err; 838 } 839 840 if (!try_module_get(client->dev.driver->owner)) { 841 i2c_unregister_device(client); 842 ret = -ENODEV; 843 goto err; 844 } 845 846 adap->fe[0] = pdata->get_dvb_frontend(client); 847 dev->demod_i2c_adapter = pdata->get_i2c_adapter(client); 848 849 dev->i2c_client_demod = client; 850 851 /* set fe callback */ 852 adap->fe[0]->callback = rtl2832u_frontend_callback; 853 854 if (dev->slave_demod) { 855 struct i2c_board_info info = {}; 856 857 /* 858 * We continue on reduced mode, without DVB-T2/C, using master 859 * demod, when slave demod fails. 860 */ 861 ret = 0; 862 863 /* attach slave demodulator */ 864 if (dev->slave_demod == SLAVE_DEMOD_MN88472) { 865 struct mn88472_config mn88472_config = {}; 866 867 mn88472_config.fe = &adap->fe[1]; 868 mn88472_config.i2c_wr_max = 22, 869 strlcpy(info.type, "mn88472", I2C_NAME_SIZE); 870 mn88472_config.xtal = 20500000; 871 info.addr = 0x18; 872 info.platform_data = &mn88472_config; 873 request_module(info.type); 874 client = i2c_new_device(&d->i2c_adap, &info); 875 if (client == NULL || client->dev.driver == NULL) { 876 dev->slave_demod = SLAVE_DEMOD_NONE; 877 goto err_slave_demod_failed; 878 } 879 880 if (!try_module_get(client->dev.driver->owner)) { 881 i2c_unregister_device(client); 882 dev->slave_demod = SLAVE_DEMOD_NONE; 883 goto err_slave_demod_failed; 884 } 885 886 dev->i2c_client_slave_demod = client; 887 } else { 888 struct mn88473_config mn88473_config = {}; 889 890 mn88473_config.fe = &adap->fe[1]; 891 mn88473_config.i2c_wr_max = 22, 892 strlcpy(info.type, "mn88473", I2C_NAME_SIZE); 893 info.addr = 0x18; 894 info.platform_data = &mn88473_config; 895 request_module(info.type); 896 client = i2c_new_device(&d->i2c_adap, &info); 897 if (client == NULL || client->dev.driver == NULL) { 898 dev->slave_demod = SLAVE_DEMOD_NONE; 899 goto err_slave_demod_failed; 900 } 901 902 if (!try_module_get(client->dev.driver->owner)) { 903 i2c_unregister_device(client); 904 dev->slave_demod = SLAVE_DEMOD_NONE; 905 goto err_slave_demod_failed; 906 } 907 908 dev->i2c_client_slave_demod = client; 909 } 910 } 911 912 return 0; 913 err_slave_demod_failed: 914 err: 915 dev_dbg(&d->intf->dev, "failed=%d\n", ret); 916 return ret; 917 } 918 919 static int rtl28xxu_frontend_attach(struct dvb_usb_adapter *adap) 920 { 921 struct rtl28xxu_dev *dev = adap_to_priv(adap); 922 923 if (dev->chip_id == CHIP_ID_RTL2831U) 924 return rtl2831u_frontend_attach(adap); 925 else 926 return rtl2832u_frontend_attach(adap); 927 } 928 929 static int rtl28xxu_frontend_detach(struct dvb_usb_adapter *adap) 930 { 931 struct dvb_usb_device *d = adap_to_d(adap); 932 struct rtl28xxu_dev *dev = d_to_priv(d); 933 struct i2c_client *client; 934 935 dev_dbg(&d->intf->dev, "\n"); 936 937 /* remove I2C slave demod */ 938 client = dev->i2c_client_slave_demod; 939 if (client) { 940 module_put(client->dev.driver->owner); 941 i2c_unregister_device(client); 942 } 943 944 /* remove I2C demod */ 945 client = dev->i2c_client_demod; 946 if (client) { 947 module_put(client->dev.driver->owner); 948 i2c_unregister_device(client); 949 } 950 951 return 0; 952 } 953 954 static struct qt1010_config rtl28xxu_qt1010_config = { 955 .i2c_address = 0x62, /* 0xc4 */ 956 }; 957 958 static struct mt2060_config rtl28xxu_mt2060_config = { 959 .i2c_address = 0x60, /* 0xc0 */ 960 .clock_out = 0, 961 }; 962 963 static struct mxl5005s_config rtl28xxu_mxl5005s_config = { 964 .i2c_address = 0x63, /* 0xc6 */ 965 .if_freq = IF_FREQ_4570000HZ, 966 .xtal_freq = CRYSTAL_FREQ_16000000HZ, 967 .agc_mode = MXL_SINGLE_AGC, 968 .tracking_filter = MXL_TF_C_H, 969 .rssi_enable = MXL_RSSI_ENABLE, 970 .cap_select = MXL_CAP_SEL_ENABLE, 971 .div_out = MXL_DIV_OUT_4, 972 .clock_out = MXL_CLOCK_OUT_DISABLE, 973 .output_load = MXL5005S_IF_OUTPUT_LOAD_200_OHM, 974 .top = MXL5005S_TOP_25P2, 975 .mod_mode = MXL_DIGITAL_MODE, 976 .if_mode = MXL_ZERO_IF, 977 .AgcMasterByte = 0x00, 978 }; 979 980 static int rtl2831u_tuner_attach(struct dvb_usb_adapter *adap) 981 { 982 int ret; 983 struct dvb_usb_device *d = adap_to_d(adap); 984 struct rtl28xxu_dev *dev = d_to_priv(d); 985 struct dvb_frontend *fe; 986 987 dev_dbg(&d->intf->dev, "\n"); 988 989 switch (dev->tuner) { 990 case TUNER_RTL2830_QT1010: 991 fe = dvb_attach(qt1010_attach, adap->fe[0], 992 dev->demod_i2c_adapter, 993 &rtl28xxu_qt1010_config); 994 break; 995 case TUNER_RTL2830_MT2060: 996 fe = dvb_attach(mt2060_attach, adap->fe[0], 997 dev->demod_i2c_adapter, 998 &rtl28xxu_mt2060_config, 1220); 999 break; 1000 case TUNER_RTL2830_MXL5005S: 1001 fe = dvb_attach(mxl5005s_attach, adap->fe[0], 1002 dev->demod_i2c_adapter, 1003 &rtl28xxu_mxl5005s_config); 1004 break; 1005 default: 1006 fe = NULL; 1007 dev_err(&d->intf->dev, "unknown tuner %d\n", dev->tuner); 1008 } 1009 1010 if (fe == NULL) { 1011 ret = -ENODEV; 1012 goto err; 1013 } 1014 1015 return 0; 1016 err: 1017 dev_dbg(&d->intf->dev, "failed=%d\n", ret); 1018 return ret; 1019 } 1020 1021 static const struct fc2580_config rtl2832u_fc2580_config = { 1022 .i2c_addr = 0x56, 1023 .clock = 16384000, 1024 }; 1025 1026 static struct tua9001_config rtl2832u_tua9001_config = { 1027 .i2c_addr = 0x60, 1028 }; 1029 1030 static const struct fc0012_config rtl2832u_fc0012_config = { 1031 .i2c_address = 0x63, /* 0xc6 >> 1 */ 1032 .xtal_freq = FC_XTAL_28_8_MHZ, 1033 }; 1034 1035 static const struct r820t_config rtl2832u_r820t_config = { 1036 .i2c_addr = 0x1a, 1037 .xtal = 28800000, 1038 .max_i2c_msg_len = 2, 1039 .rafael_chip = CHIP_R820T, 1040 }; 1041 1042 static const struct r820t_config rtl2832u_r828d_config = { 1043 .i2c_addr = 0x3a, 1044 .xtal = 16000000, 1045 .max_i2c_msg_len = 2, 1046 .rafael_chip = CHIP_R828D, 1047 }; 1048 1049 static int rtl2832u_tuner_attach(struct dvb_usb_adapter *adap) 1050 { 1051 int ret; 1052 struct dvb_usb_device *d = adap_to_d(adap); 1053 struct rtl28xxu_dev *dev = d_to_priv(d); 1054 struct dvb_frontend *fe = NULL; 1055 struct i2c_board_info info; 1056 struct i2c_client *client; 1057 struct v4l2_subdev *subdev = NULL; 1058 struct platform_device *pdev; 1059 struct rtl2832_sdr_platform_data pdata; 1060 1061 dev_dbg(&d->intf->dev, "\n"); 1062 1063 memset(&info, 0, sizeof(struct i2c_board_info)); 1064 memset(&pdata, 0, sizeof(pdata)); 1065 1066 switch (dev->tuner) { 1067 case TUNER_RTL2832_FC0012: 1068 fe = dvb_attach(fc0012_attach, adap->fe[0], 1069 dev->demod_i2c_adapter, &rtl2832u_fc0012_config); 1070 1071 /* since fc0012 includs reading the signal strength delegate 1072 * that to the tuner driver */ 1073 adap->fe[0]->ops.read_signal_strength = 1074 adap->fe[0]->ops.tuner_ops.get_rf_strength; 1075 break; 1076 case TUNER_RTL2832_FC0013: 1077 fe = dvb_attach(fc0013_attach, adap->fe[0], 1078 dev->demod_i2c_adapter, 0xc6>>1, 0, FC_XTAL_28_8_MHZ); 1079 1080 /* fc0013 also supports signal strength reading */ 1081 adap->fe[0]->ops.read_signal_strength = 1082 adap->fe[0]->ops.tuner_ops.get_rf_strength; 1083 break; 1084 case TUNER_RTL2832_E4000: { 1085 struct e4000_config e4000_config = { 1086 .fe = adap->fe[0], 1087 .clock = 28800000, 1088 }; 1089 1090 strlcpy(info.type, "e4000", I2C_NAME_SIZE); 1091 info.addr = 0x64; 1092 info.platform_data = &e4000_config; 1093 1094 request_module(info.type); 1095 client = i2c_new_device(dev->demod_i2c_adapter, &info); 1096 if (client == NULL || client->dev.driver == NULL) 1097 break; 1098 1099 if (!try_module_get(client->dev.driver->owner)) { 1100 i2c_unregister_device(client); 1101 break; 1102 } 1103 1104 dev->i2c_client_tuner = client; 1105 subdev = i2c_get_clientdata(client); 1106 } 1107 break; 1108 case TUNER_RTL2832_FC2580: 1109 fe = dvb_attach(fc2580_attach, adap->fe[0], 1110 dev->demod_i2c_adapter, 1111 &rtl2832u_fc2580_config); 1112 break; 1113 case TUNER_RTL2832_TUA9001: 1114 /* enable GPIO1 and GPIO4 as output */ 1115 ret = rtl28xxu_wr_reg_mask(d, SYS_GPIO_DIR, 0x00, 0x12); 1116 if (ret) 1117 goto err; 1118 1119 ret = rtl28xxu_wr_reg_mask(d, SYS_GPIO_OUT_EN, 0x12, 0x12); 1120 if (ret) 1121 goto err; 1122 1123 fe = dvb_attach(tua9001_attach, adap->fe[0], 1124 dev->demod_i2c_adapter, 1125 &rtl2832u_tua9001_config); 1126 break; 1127 case TUNER_RTL2832_R820T: 1128 fe = dvb_attach(r820t_attach, adap->fe[0], 1129 dev->demod_i2c_adapter, 1130 &rtl2832u_r820t_config); 1131 1132 /* Use tuner to get the signal strength */ 1133 adap->fe[0]->ops.read_signal_strength = 1134 adap->fe[0]->ops.tuner_ops.get_rf_strength; 1135 break; 1136 case TUNER_RTL2832_R828D: 1137 fe = dvb_attach(r820t_attach, adap->fe[0], 1138 dev->demod_i2c_adapter, 1139 &rtl2832u_r828d_config); 1140 adap->fe[0]->ops.read_signal_strength = 1141 adap->fe[0]->ops.tuner_ops.get_rf_strength; 1142 1143 if (adap->fe[1]) { 1144 fe = dvb_attach(r820t_attach, adap->fe[1], 1145 dev->demod_i2c_adapter, 1146 &rtl2832u_r828d_config); 1147 adap->fe[1]->ops.read_signal_strength = 1148 adap->fe[1]->ops.tuner_ops.get_rf_strength; 1149 } 1150 break; 1151 default: 1152 dev_err(&d->intf->dev, "unknown tuner %d\n", dev->tuner); 1153 } 1154 if (fe == NULL && dev->i2c_client_tuner == NULL) { 1155 ret = -ENODEV; 1156 goto err; 1157 } 1158 1159 /* register SDR */ 1160 switch (dev->tuner) { 1161 case TUNER_RTL2832_FC0012: 1162 case TUNER_RTL2832_FC0013: 1163 case TUNER_RTL2832_E4000: 1164 case TUNER_RTL2832_R820T: 1165 case TUNER_RTL2832_R828D: 1166 pdata.clk = dev->rtl2832_platform_data.clk; 1167 pdata.tuner = dev->tuner; 1168 pdata.i2c_client = dev->i2c_client_demod; 1169 pdata.bulk_read = dev->rtl2832_platform_data.bulk_read; 1170 pdata.bulk_write = dev->rtl2832_platform_data.bulk_write; 1171 pdata.update_bits = dev->rtl2832_platform_data.update_bits; 1172 pdata.dvb_frontend = adap->fe[0]; 1173 pdata.dvb_usb_device = d; 1174 pdata.v4l2_subdev = subdev; 1175 1176 request_module("%s", "rtl2832_sdr"); 1177 pdev = platform_device_register_data(&d->intf->dev, 1178 "rtl2832_sdr", 1179 PLATFORM_DEVID_AUTO, 1180 &pdata, sizeof(pdata)); 1181 if (pdev == NULL || pdev->dev.driver == NULL) 1182 break; 1183 dev->platform_device_sdr = pdev; 1184 break; 1185 default: 1186 dev_dbg(&d->intf->dev, "no SDR for tuner=%d\n", dev->tuner); 1187 } 1188 1189 return 0; 1190 err: 1191 dev_dbg(&d->intf->dev, "failed=%d\n", ret); 1192 return ret; 1193 } 1194 1195 static int rtl28xxu_tuner_attach(struct dvb_usb_adapter *adap) 1196 { 1197 struct rtl28xxu_dev *dev = adap_to_priv(adap); 1198 1199 if (dev->chip_id == CHIP_ID_RTL2831U) 1200 return rtl2831u_tuner_attach(adap); 1201 else 1202 return rtl2832u_tuner_attach(adap); 1203 } 1204 1205 static int rtl28xxu_tuner_detach(struct dvb_usb_adapter *adap) 1206 { 1207 struct dvb_usb_device *d = adap_to_d(adap); 1208 struct rtl28xxu_dev *dev = d_to_priv(d); 1209 struct i2c_client *client; 1210 struct platform_device *pdev; 1211 1212 dev_dbg(&d->intf->dev, "\n"); 1213 1214 /* remove platform SDR */ 1215 pdev = dev->platform_device_sdr; 1216 if (pdev) 1217 platform_device_unregister(pdev); 1218 1219 /* remove I2C tuner */ 1220 client = dev->i2c_client_tuner; 1221 if (client) { 1222 module_put(client->dev.driver->owner); 1223 i2c_unregister_device(client); 1224 } 1225 1226 return 0; 1227 } 1228 1229 static int rtl28xxu_init(struct dvb_usb_device *d) 1230 { 1231 int ret; 1232 u8 val; 1233 1234 dev_dbg(&d->intf->dev, "\n"); 1235 1236 /* init USB endpoints */ 1237 ret = rtl28xxu_rd_reg(d, USB_SYSCTL_0, &val); 1238 if (ret) 1239 goto err; 1240 1241 /* enable DMA and Full Packet Mode*/ 1242 val |= 0x09; 1243 ret = rtl28xxu_wr_reg(d, USB_SYSCTL_0, val); 1244 if (ret) 1245 goto err; 1246 1247 /* set EPA maximum packet size to 0x0200 */ 1248 ret = rtl28xxu_wr_regs(d, USB_EPA_MAXPKT, "\x00\x02\x00\x00", 4); 1249 if (ret) 1250 goto err; 1251 1252 /* change EPA FIFO length */ 1253 ret = rtl28xxu_wr_regs(d, USB_EPA_FIFO_CFG, "\x14\x00\x00\x00", 4); 1254 if (ret) 1255 goto err; 1256 1257 return ret; 1258 err: 1259 dev_dbg(&d->intf->dev, "failed=%d\n", ret); 1260 return ret; 1261 } 1262 1263 static int rtl2831u_power_ctrl(struct dvb_usb_device *d, int onoff) 1264 { 1265 int ret; 1266 u8 gpio, sys0, epa_ctl[2]; 1267 1268 dev_dbg(&d->intf->dev, "onoff=%d\n", onoff); 1269 1270 /* demod adc */ 1271 ret = rtl28xxu_rd_reg(d, SYS_SYS0, &sys0); 1272 if (ret) 1273 goto err; 1274 1275 /* tuner power, read GPIOs */ 1276 ret = rtl28xxu_rd_reg(d, SYS_GPIO_OUT_VAL, &gpio); 1277 if (ret) 1278 goto err; 1279 1280 dev_dbg(&d->intf->dev, "RD SYS0=%02x GPIO_OUT_VAL=%02x\n", sys0, gpio); 1281 1282 if (onoff) { 1283 gpio |= 0x01; /* GPIO0 = 1 */ 1284 gpio &= (~0x10); /* GPIO4 = 0 */ 1285 gpio |= 0x04; /* GPIO2 = 1, LED on */ 1286 sys0 = sys0 & 0x0f; 1287 sys0 |= 0xe0; 1288 epa_ctl[0] = 0x00; /* clear stall */ 1289 epa_ctl[1] = 0x00; /* clear reset */ 1290 } else { 1291 gpio &= (~0x01); /* GPIO0 = 0 */ 1292 gpio |= 0x10; /* GPIO4 = 1 */ 1293 gpio &= (~0x04); /* GPIO2 = 1, LED off */ 1294 sys0 = sys0 & (~0xc0); 1295 epa_ctl[0] = 0x10; /* set stall */ 1296 epa_ctl[1] = 0x02; /* set reset */ 1297 } 1298 1299 dev_dbg(&d->intf->dev, "WR SYS0=%02x GPIO_OUT_VAL=%02x\n", sys0, gpio); 1300 1301 /* demod adc */ 1302 ret = rtl28xxu_wr_reg(d, SYS_SYS0, sys0); 1303 if (ret) 1304 goto err; 1305 1306 /* tuner power, write GPIOs */ 1307 ret = rtl28xxu_wr_reg(d, SYS_GPIO_OUT_VAL, gpio); 1308 if (ret) 1309 goto err; 1310 1311 /* streaming EP: stall & reset */ 1312 ret = rtl28xxu_wr_regs(d, USB_EPA_CTL, epa_ctl, 2); 1313 if (ret) 1314 goto err; 1315 1316 if (onoff) 1317 usb_clear_halt(d->udev, usb_rcvbulkpipe(d->udev, 0x81)); 1318 1319 return ret; 1320 err: 1321 dev_dbg(&d->intf->dev, "failed=%d\n", ret); 1322 return ret; 1323 } 1324 1325 static int rtl2832u_power_ctrl(struct dvb_usb_device *d, int onoff) 1326 { 1327 int ret; 1328 1329 dev_dbg(&d->intf->dev, "onoff=%d\n", onoff); 1330 1331 if (onoff) { 1332 /* GPIO3=1, GPIO4=0 */ 1333 ret = rtl28xxu_wr_reg_mask(d, SYS_GPIO_OUT_VAL, 0x08, 0x18); 1334 if (ret) 1335 goto err; 1336 1337 /* suspend? */ 1338 ret = rtl28xxu_wr_reg_mask(d, SYS_DEMOD_CTL1, 0x00, 0x10); 1339 if (ret) 1340 goto err; 1341 1342 /* enable PLL */ 1343 ret = rtl28xxu_wr_reg_mask(d, SYS_DEMOD_CTL, 0x80, 0x80); 1344 if (ret) 1345 goto err; 1346 1347 /* disable reset */ 1348 ret = rtl28xxu_wr_reg_mask(d, SYS_DEMOD_CTL, 0x20, 0x20); 1349 if (ret) 1350 goto err; 1351 1352 /* streaming EP: clear stall & reset */ 1353 ret = rtl28xxu_wr_regs(d, USB_EPA_CTL, "\x00\x00", 2); 1354 if (ret) 1355 goto err; 1356 1357 ret = usb_clear_halt(d->udev, usb_rcvbulkpipe(d->udev, 0x81)); 1358 if (ret) 1359 goto err; 1360 } else { 1361 /* GPIO4=1 */ 1362 ret = rtl28xxu_wr_reg_mask(d, SYS_GPIO_OUT_VAL, 0x10, 0x10); 1363 if (ret) 1364 goto err; 1365 1366 /* disable PLL */ 1367 ret = rtl28xxu_wr_reg_mask(d, SYS_DEMOD_CTL, 0x00, 0x80); 1368 if (ret) 1369 goto err; 1370 1371 /* streaming EP: set stall & reset */ 1372 ret = rtl28xxu_wr_regs(d, USB_EPA_CTL, "\x10\x02", 2); 1373 if (ret) 1374 goto err; 1375 } 1376 1377 return ret; 1378 err: 1379 dev_dbg(&d->intf->dev, "failed=%d\n", ret); 1380 return ret; 1381 } 1382 1383 static int rtl28xxu_power_ctrl(struct dvb_usb_device *d, int onoff) 1384 { 1385 struct rtl28xxu_dev *dev = d_to_priv(d); 1386 1387 if (dev->chip_id == CHIP_ID_RTL2831U) 1388 return rtl2831u_power_ctrl(d, onoff); 1389 else 1390 return rtl2832u_power_ctrl(d, onoff); 1391 } 1392 1393 static int rtl28xxu_frontend_ctrl(struct dvb_frontend *fe, int onoff) 1394 { 1395 struct dvb_usb_device *d = fe_to_d(fe); 1396 struct rtl28xxu_dev *dev = fe_to_priv(fe); 1397 struct rtl2832_platform_data *pdata = &dev->rtl2832_platform_data; 1398 int ret; 1399 u8 val; 1400 1401 dev_dbg(&d->intf->dev, "fe=%d onoff=%d\n", fe->id, onoff); 1402 1403 if (dev->chip_id == CHIP_ID_RTL2831U) 1404 return 0; 1405 1406 /* control internal demod ADC */ 1407 if (fe->id == 0 && onoff) 1408 val = 0x48; /* enable ADC */ 1409 else 1410 val = 0x00; /* disable ADC */ 1411 1412 ret = rtl28xxu_wr_reg_mask(d, SYS_DEMOD_CTL, val, 0x48); 1413 if (ret) 1414 goto err; 1415 1416 /* bypass slave demod TS through master demod */ 1417 if (fe->id == 1 && onoff) { 1418 ret = pdata->enable_slave_ts(dev->i2c_client_demod); 1419 if (ret) 1420 goto err; 1421 } 1422 1423 return 0; 1424 err: 1425 dev_dbg(&d->intf->dev, "failed=%d\n", ret); 1426 return ret; 1427 } 1428 1429 #if IS_ENABLED(CONFIG_RC_CORE) 1430 static int rtl2831u_rc_query(struct dvb_usb_device *d) 1431 { 1432 int ret, i; 1433 struct rtl28xxu_dev *dev = d->priv; 1434 u8 buf[5]; 1435 u32 rc_code; 1436 struct rtl28xxu_reg_val rc_nec_tab[] = { 1437 { 0x3033, 0x80 }, 1438 { 0x3020, 0x43 }, 1439 { 0x3021, 0x16 }, 1440 { 0x3022, 0x16 }, 1441 { 0x3023, 0x5a }, 1442 { 0x3024, 0x2d }, 1443 { 0x3025, 0x16 }, 1444 { 0x3026, 0x01 }, 1445 { 0x3028, 0xb0 }, 1446 { 0x3029, 0x04 }, 1447 { 0x302c, 0x88 }, 1448 { 0x302e, 0x13 }, 1449 { 0x3030, 0xdf }, 1450 { 0x3031, 0x05 }, 1451 }; 1452 1453 /* init remote controller */ 1454 if (!dev->rc_active) { 1455 for (i = 0; i < ARRAY_SIZE(rc_nec_tab); i++) { 1456 ret = rtl28xxu_wr_reg(d, rc_nec_tab[i].reg, 1457 rc_nec_tab[i].val); 1458 if (ret) 1459 goto err; 1460 } 1461 dev->rc_active = true; 1462 } 1463 1464 ret = rtl28xxu_rd_regs(d, SYS_IRRC_RP, buf, 5); 1465 if (ret) 1466 goto err; 1467 1468 if (buf[4] & 0x01) { 1469 if (buf[2] == (u8) ~buf[3]) { 1470 if (buf[0] == (u8) ~buf[1]) { 1471 /* NEC standard (16 bit) */ 1472 rc_code = RC_SCANCODE_NEC(buf[0], buf[2]); 1473 } else { 1474 /* NEC extended (24 bit) */ 1475 rc_code = RC_SCANCODE_NECX(buf[0] << 8 | buf[1], 1476 buf[2]); 1477 } 1478 } else { 1479 /* NEC full (32 bit) */ 1480 rc_code = RC_SCANCODE_NEC32(buf[0] << 24 | buf[1] << 16 | 1481 buf[2] << 8 | buf[3]); 1482 } 1483 1484 rc_keydown(d->rc_dev, RC_TYPE_NEC, rc_code, 0); 1485 1486 ret = rtl28xxu_wr_reg(d, SYS_IRRC_SR, 1); 1487 if (ret) 1488 goto err; 1489 1490 /* repeated intentionally to avoid extra keypress */ 1491 ret = rtl28xxu_wr_reg(d, SYS_IRRC_SR, 1); 1492 if (ret) 1493 goto err; 1494 } 1495 1496 return ret; 1497 err: 1498 dev_dbg(&d->intf->dev, "failed=%d\n", ret); 1499 return ret; 1500 } 1501 1502 static int rtl2831u_get_rc_config(struct dvb_usb_device *d, 1503 struct dvb_usb_rc *rc) 1504 { 1505 rc->map_name = RC_MAP_EMPTY; 1506 rc->allowed_protos = RC_BIT_NEC; 1507 rc->query = rtl2831u_rc_query; 1508 rc->interval = 400; 1509 1510 return 0; 1511 } 1512 1513 static int rtl2832u_rc_query(struct dvb_usb_device *d) 1514 { 1515 int ret, i, len; 1516 struct rtl28xxu_dev *dev = d->priv; 1517 struct ir_raw_event ev; 1518 u8 buf[128]; 1519 static const struct rtl28xxu_reg_val_mask refresh_tab[] = { 1520 {IR_RX_IF, 0x03, 0xff}, 1521 {IR_RX_BUF_CTRL, 0x80, 0xff}, 1522 {IR_RX_CTRL, 0x80, 0xff}, 1523 }; 1524 1525 /* init remote controller */ 1526 if (!dev->rc_active) { 1527 static const struct rtl28xxu_reg_val_mask init_tab[] = { 1528 {SYS_DEMOD_CTL1, 0x00, 0x04}, 1529 {SYS_DEMOD_CTL1, 0x00, 0x08}, 1530 {USB_CTRL, 0x20, 0x20}, 1531 {SYS_GPIO_DIR, 0x00, 0x08}, 1532 {SYS_GPIO_OUT_EN, 0x08, 0x08}, 1533 {SYS_GPIO_OUT_VAL, 0x08, 0x08}, 1534 {IR_MAX_DURATION0, 0xd0, 0xff}, 1535 {IR_MAX_DURATION1, 0x07, 0xff}, 1536 {IR_IDLE_LEN0, 0xc0, 0xff}, 1537 {IR_IDLE_LEN1, 0x00, 0xff}, 1538 {IR_GLITCH_LEN, 0x03, 0xff}, 1539 {IR_RX_CLK, 0x09, 0xff}, 1540 {IR_RX_CFG, 0x1c, 0xff}, 1541 {IR_MAX_H_TOL_LEN, 0x1e, 0xff}, 1542 {IR_MAX_L_TOL_LEN, 0x1e, 0xff}, 1543 {IR_RX_CTRL, 0x80, 0xff}, 1544 }; 1545 1546 for (i = 0; i < ARRAY_SIZE(init_tab); i++) { 1547 ret = rtl28xxu_wr_reg_mask(d, init_tab[i].reg, 1548 init_tab[i].val, init_tab[i].mask); 1549 if (ret) 1550 goto err; 1551 } 1552 1553 dev->rc_active = true; 1554 } 1555 1556 ret = rtl28xxu_rd_reg(d, IR_RX_IF, &buf[0]); 1557 if (ret) 1558 goto err; 1559 1560 if (buf[0] != 0x83) 1561 goto exit; 1562 1563 ret = rtl28xxu_rd_reg(d, IR_RX_BC, &buf[0]); 1564 if (ret) 1565 goto err; 1566 1567 len = buf[0]; 1568 1569 /* read raw code from hw */ 1570 ret = rtl28xxu_rd_regs(d, IR_RX_BUF, buf, len); 1571 if (ret) 1572 goto err; 1573 1574 /* let hw receive new code */ 1575 for (i = 0; i < ARRAY_SIZE(refresh_tab); i++) { 1576 ret = rtl28xxu_wr_reg_mask(d, refresh_tab[i].reg, 1577 refresh_tab[i].val, refresh_tab[i].mask); 1578 if (ret) 1579 goto err; 1580 } 1581 1582 /* pass data to Kernel IR decoder */ 1583 init_ir_raw_event(&ev); 1584 1585 for (i = 0; i < len; i++) { 1586 ev.pulse = buf[i] >> 7; 1587 ev.duration = 50800 * (buf[i] & 0x7f); 1588 ir_raw_event_store_with_filter(d->rc_dev, &ev); 1589 } 1590 1591 /* 'flush' ir_raw_event_store_with_filter() */ 1592 ir_raw_event_set_idle(d->rc_dev, true); 1593 ir_raw_event_handle(d->rc_dev); 1594 exit: 1595 return ret; 1596 err: 1597 dev_dbg(&d->intf->dev, "failed=%d\n", ret); 1598 return ret; 1599 } 1600 1601 static int rtl2832u_get_rc_config(struct dvb_usb_device *d, 1602 struct dvb_usb_rc *rc) 1603 { 1604 /* disable IR interrupts in order to avoid SDR sample loss */ 1605 if (rtl28xxu_disable_rc) 1606 return rtl28xxu_wr_reg(d, IR_RX_IE, 0x00); 1607 1608 /* load empty to enable rc */ 1609 if (!rc->map_name) 1610 rc->map_name = RC_MAP_EMPTY; 1611 rc->allowed_protos = RC_BIT_ALL; 1612 rc->driver_type = RC_DRIVER_IR_RAW; 1613 rc->query = rtl2832u_rc_query; 1614 rc->interval = 400; 1615 1616 return 0; 1617 } 1618 1619 static int rtl28xxu_get_rc_config(struct dvb_usb_device *d, 1620 struct dvb_usb_rc *rc) 1621 { 1622 struct rtl28xxu_dev *dev = d_to_priv(d); 1623 1624 if (dev->chip_id == CHIP_ID_RTL2831U) 1625 return rtl2831u_get_rc_config(d, rc); 1626 else 1627 return rtl2832u_get_rc_config(d, rc); 1628 } 1629 #else 1630 #define rtl28xxu_get_rc_config NULL 1631 #endif 1632 1633 static int rtl28xxu_pid_filter_ctrl(struct dvb_usb_adapter *adap, int onoff) 1634 { 1635 struct rtl28xxu_dev *dev = adap_to_priv(adap); 1636 1637 if (dev->chip_id == CHIP_ID_RTL2831U) { 1638 struct rtl2830_platform_data *pdata = &dev->rtl2830_platform_data; 1639 1640 return pdata->pid_filter_ctrl(adap->fe[0], onoff); 1641 } else { 1642 struct rtl2832_platform_data *pdata = &dev->rtl2832_platform_data; 1643 1644 return pdata->pid_filter_ctrl(adap->fe[0], onoff); 1645 } 1646 } 1647 1648 static int rtl28xxu_pid_filter(struct dvb_usb_adapter *adap, int index, 1649 u16 pid, int onoff) 1650 { 1651 struct rtl28xxu_dev *dev = adap_to_priv(adap); 1652 1653 if (dev->chip_id == CHIP_ID_RTL2831U) { 1654 struct rtl2830_platform_data *pdata = &dev->rtl2830_platform_data; 1655 1656 return pdata->pid_filter(adap->fe[0], index, pid, onoff); 1657 } else { 1658 struct rtl2832_platform_data *pdata = &dev->rtl2832_platform_data; 1659 1660 return pdata->pid_filter(adap->fe[0], index, pid, onoff); 1661 } 1662 } 1663 1664 static const struct dvb_usb_device_properties rtl28xxu_props = { 1665 .driver_name = KBUILD_MODNAME, 1666 .owner = THIS_MODULE, 1667 .adapter_nr = adapter_nr, 1668 .size_of_priv = sizeof(struct rtl28xxu_dev), 1669 1670 .identify_state = rtl28xxu_identify_state, 1671 .power_ctrl = rtl28xxu_power_ctrl, 1672 .frontend_ctrl = rtl28xxu_frontend_ctrl, 1673 .i2c_algo = &rtl28xxu_i2c_algo, 1674 .read_config = rtl28xxu_read_config, 1675 .frontend_attach = rtl28xxu_frontend_attach, 1676 .frontend_detach = rtl28xxu_frontend_detach, 1677 .tuner_attach = rtl28xxu_tuner_attach, 1678 .tuner_detach = rtl28xxu_tuner_detach, 1679 .init = rtl28xxu_init, 1680 .get_rc_config = rtl28xxu_get_rc_config, 1681 1682 .num_adapters = 1, 1683 .adapter = { 1684 { 1685 .caps = DVB_USB_ADAP_HAS_PID_FILTER | 1686 DVB_USB_ADAP_PID_FILTER_CAN_BE_TURNED_OFF, 1687 1688 .pid_filter_count = 32, 1689 .pid_filter_ctrl = rtl28xxu_pid_filter_ctrl, 1690 .pid_filter = rtl28xxu_pid_filter, 1691 1692 .stream = DVB_USB_STREAM_BULK(0x81, 6, 8 * 512), 1693 }, 1694 }, 1695 }; 1696 1697 static const struct usb_device_id rtl28xxu_id_table[] = { 1698 /* RTL2831U devices: */ 1699 { DVB_USB_DEVICE(USB_VID_REALTEK, USB_PID_REALTEK_RTL2831U, 1700 &rtl28xxu_props, "Realtek RTL2831U reference design", NULL) }, 1701 { DVB_USB_DEVICE(USB_VID_WIDEVIEW, USB_PID_FREECOM_DVBT, 1702 &rtl28xxu_props, "Freecom USB2.0 DVB-T", NULL) }, 1703 { DVB_USB_DEVICE(USB_VID_WIDEVIEW, USB_PID_FREECOM_DVBT_2, 1704 &rtl28xxu_props, "Freecom USB2.0 DVB-T", NULL) }, 1705 1706 /* RTL2832U devices: */ 1707 { DVB_USB_DEVICE(USB_VID_REALTEK, 0x2832, 1708 &rtl28xxu_props, "Realtek RTL2832U reference design", NULL) }, 1709 { DVB_USB_DEVICE(USB_VID_REALTEK, 0x2838, 1710 &rtl28xxu_props, "Realtek RTL2832U reference design", NULL) }, 1711 { DVB_USB_DEVICE(USB_VID_TERRATEC, USB_PID_TERRATEC_CINERGY_T_STICK_BLACK_REV1, 1712 &rtl28xxu_props, "TerraTec Cinergy T Stick Black", RC_MAP_TERRATEC_SLIM) }, 1713 { DVB_USB_DEVICE(USB_VID_GTEK, USB_PID_DELOCK_USB2_DVBT, 1714 &rtl28xxu_props, "G-Tek Electronics Group Lifeview LV5TDLX DVB-T", NULL) }, 1715 { DVB_USB_DEVICE(USB_VID_TERRATEC, USB_PID_NOXON_DAB_STICK, 1716 &rtl28xxu_props, "TerraTec NOXON DAB Stick", NULL) }, 1717 { DVB_USB_DEVICE(USB_VID_TERRATEC, USB_PID_NOXON_DAB_STICK_REV2, 1718 &rtl28xxu_props, "TerraTec NOXON DAB Stick (rev 2)", NULL) }, 1719 { DVB_USB_DEVICE(USB_VID_TERRATEC, USB_PID_NOXON_DAB_STICK_REV3, 1720 &rtl28xxu_props, "TerraTec NOXON DAB Stick (rev 3)", NULL) }, 1721 { DVB_USB_DEVICE(USB_VID_GTEK, USB_PID_TREKSTOR_TERRES_2_0, 1722 &rtl28xxu_props, "Trekstor DVB-T Stick Terres 2.0", NULL) }, 1723 { DVB_USB_DEVICE(USB_VID_DEXATEK, 0x1101, 1724 &rtl28xxu_props, "Dexatek DK DVB-T Dongle", NULL) }, 1725 { DVB_USB_DEVICE(USB_VID_LEADTEK, 0x6680, 1726 &rtl28xxu_props, "DigitalNow Quad DVB-T Receiver", NULL) }, 1727 { DVB_USB_DEVICE(USB_VID_LEADTEK, USB_PID_WINFAST_DTV_DONGLE_MINID, 1728 &rtl28xxu_props, "Leadtek Winfast DTV Dongle Mini D", NULL) }, 1729 { DVB_USB_DEVICE(USB_VID_TERRATEC, 0x00d3, 1730 &rtl28xxu_props, "TerraTec Cinergy T Stick RC (Rev. 3)", NULL) }, 1731 { DVB_USB_DEVICE(USB_VID_DEXATEK, 0x1102, 1732 &rtl28xxu_props, "Dexatek DK mini DVB-T Dongle", NULL) }, 1733 { DVB_USB_DEVICE(USB_VID_TERRATEC, 0x00d7, 1734 &rtl28xxu_props, "TerraTec Cinergy T Stick+", NULL) }, 1735 { DVB_USB_DEVICE(USB_VID_KWORLD_2, 0xd3a8, 1736 &rtl28xxu_props, "ASUS My Cinema-U3100Mini Plus V2", NULL) }, 1737 { DVB_USB_DEVICE(USB_VID_KWORLD_2, 0xd393, 1738 &rtl28xxu_props, "GIGABYTE U7300", NULL) }, 1739 { DVB_USB_DEVICE(USB_VID_DEXATEK, 0x1104, 1740 &rtl28xxu_props, "MSI DIGIVOX Micro HD", NULL) }, 1741 { DVB_USB_DEVICE(USB_VID_COMPRO, 0x0620, 1742 &rtl28xxu_props, "Compro VideoMate U620F", NULL) }, 1743 { DVB_USB_DEVICE(USB_VID_KWORLD_2, 0xd394, 1744 &rtl28xxu_props, "MaxMedia HU394-T", NULL) }, 1745 { DVB_USB_DEVICE(USB_VID_LEADTEK, 0x6a03, 1746 &rtl28xxu_props, "Leadtek WinFast DTV Dongle mini", NULL) }, 1747 { DVB_USB_DEVICE(USB_VID_GTEK, USB_PID_CPYTO_REDI_PC50A, 1748 &rtl28xxu_props, "Crypto ReDi PC 50 A", NULL) }, 1749 { DVB_USB_DEVICE(USB_VID_KYE, 0x707f, 1750 &rtl28xxu_props, "Genius TVGo DVB-T03", NULL) }, 1751 { DVB_USB_DEVICE(USB_VID_KWORLD_2, 0xd395, 1752 &rtl28xxu_props, "Peak DVB-T USB", NULL) }, 1753 { DVB_USB_DEVICE(USB_VID_KWORLD_2, USB_PID_SVEON_STV20_RTL2832U, 1754 &rtl28xxu_props, "Sveon STV20", NULL) }, 1755 { DVB_USB_DEVICE(USB_VID_KWORLD_2, USB_PID_SVEON_STV21, 1756 &rtl28xxu_props, "Sveon STV21", NULL) }, 1757 { DVB_USB_DEVICE(USB_VID_KWORLD_2, USB_PID_SVEON_STV27, 1758 &rtl28xxu_props, "Sveon STV27", NULL) }, 1759 1760 /* RTL2832P devices: */ 1761 { DVB_USB_DEVICE(USB_VID_HANFTEK, 0x0131, 1762 &rtl28xxu_props, "Astrometa DVB-T2", NULL) }, 1763 { } 1764 }; 1765 MODULE_DEVICE_TABLE(usb, rtl28xxu_id_table); 1766 1767 static struct usb_driver rtl28xxu_usb_driver = { 1768 .name = KBUILD_MODNAME, 1769 .id_table = rtl28xxu_id_table, 1770 .probe = dvb_usbv2_probe, 1771 .disconnect = dvb_usbv2_disconnect, 1772 .suspend = dvb_usbv2_suspend, 1773 .resume = dvb_usbv2_resume, 1774 .reset_resume = dvb_usbv2_reset_resume, 1775 .no_dynamic_id = 1, 1776 .soft_unbind = 1, 1777 }; 1778 1779 module_usb_driver(rtl28xxu_usb_driver); 1780 1781 MODULE_DESCRIPTION("Realtek RTL28xxU DVB USB driver"); 1782 MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>"); 1783 MODULE_AUTHOR("Thomas Mair <thomas.mair86@googlemail.com>"); 1784 MODULE_LICENSE("GPL"); 1785