1 // SPDX-License-Identifier: GPL-2.0-only 2 /* DVB USB compliant linux driver for GL861 USB2.0 devices. 3 * 4 * see Documentation/driver-api/media/drivers/dvb-usb.rst for more information 5 */ 6 #include <linux/string.h> 7 8 #include "dvb_usb.h" 9 10 #include "zl10353.h" 11 #include "qt1010.h" 12 #include "tc90522.h" 13 #include "dvb-pll.h" 14 15 DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr); 16 17 struct gl861 { 18 /* USB control message buffer */ 19 u8 buf[16]; 20 21 struct i2c_adapter *demod_sub_i2c; 22 struct i2c_client *i2c_client_demod; 23 struct i2c_client *i2c_client_tuner; 24 }; 25 26 #define CMD_WRITE_SHORT 0x01 27 #define CMD_READ 0x02 28 #define CMD_WRITE 0x03 29 30 static int gl861_ctrl_msg(struct dvb_usb_device *d, u8 request, u16 value, 31 u16 index, void *data, u16 size) 32 { 33 struct gl861 *ctx = d_to_priv(d); 34 struct usb_interface *intf = d->intf; 35 int ret; 36 unsigned int pipe; 37 u8 requesttype; 38 39 mutex_lock(&d->usb_mutex); 40 41 switch (request) { 42 case CMD_WRITE: 43 memcpy(ctx->buf, data, size); 44 fallthrough; 45 case CMD_WRITE_SHORT: 46 pipe = usb_sndctrlpipe(d->udev, 0); 47 requesttype = USB_TYPE_VENDOR | USB_DIR_OUT; 48 break; 49 case CMD_READ: 50 pipe = usb_rcvctrlpipe(d->udev, 0); 51 requesttype = USB_TYPE_VENDOR | USB_DIR_IN; 52 break; 53 default: 54 ret = -EINVAL; 55 goto err_mutex_unlock; 56 } 57 58 ret = usb_control_msg(d->udev, pipe, request, requesttype, value, 59 index, ctx->buf, size, 200); 60 dev_dbg(&intf->dev, "%d | %02x %02x %*ph %*ph %*ph %s %*ph\n", 61 ret, requesttype, request, 2, &value, 2, &index, 2, &size, 62 (requesttype & USB_DIR_IN) ? "<<<" : ">>>", size, ctx->buf); 63 if (ret < 0) 64 goto err_mutex_unlock; 65 66 if (request == CMD_READ) 67 memcpy(data, ctx->buf, size); 68 69 usleep_range(1000, 2000); /* Avoid I2C errors */ 70 71 mutex_unlock(&d->usb_mutex); 72 73 return 0; 74 75 err_mutex_unlock: 76 mutex_unlock(&d->usb_mutex); 77 dev_dbg(&intf->dev, "failed %d\n", ret); 78 return ret; 79 } 80 81 static int gl861_short_write(struct dvb_usb_device *d, u8 addr, u8 reg, u8 val) 82 { 83 return gl861_ctrl_msg(d, CMD_WRITE_SHORT, 84 (addr << 9) | val, reg, NULL, 0); 85 } 86 87 static int gl861_i2c_master_xfer(struct i2c_adapter *adap, struct i2c_msg msg[], 88 int num) 89 { 90 struct dvb_usb_device *d = i2c_get_adapdata(adap); 91 struct usb_interface *intf = d->intf; 92 struct gl861 *ctx = d_to_priv(d); 93 int ret; 94 u8 request, *data; 95 u16 value, index, size; 96 97 /* XXX: I2C adapter maximum data lengths are not tested */ 98 if (num == 1 && !(msg[0].flags & I2C_M_RD)) { 99 /* I2C write */ 100 if (msg[0].len < 2 || msg[0].len > sizeof(ctx->buf)) { 101 ret = -EOPNOTSUPP; 102 goto err; 103 } 104 105 value = (msg[0].addr << 1) << 8; 106 index = msg[0].buf[0]; 107 108 if (msg[0].len == 2) { 109 request = CMD_WRITE_SHORT; 110 value |= msg[0].buf[1]; 111 size = 0; 112 data = NULL; 113 } else { 114 request = CMD_WRITE; 115 size = msg[0].len - 1; 116 data = &msg[0].buf[1]; 117 } 118 119 ret = gl861_ctrl_msg(d, request, value, index, data, size); 120 } else if (num == 2 && !(msg[0].flags & I2C_M_RD) && 121 (msg[1].flags & I2C_M_RD)) { 122 /* I2C write + read */ 123 if (msg[0].len > 1 || msg[1].len > sizeof(ctx->buf)) { 124 ret = -EOPNOTSUPP; 125 goto err; 126 } 127 128 value = (msg[0].addr << 1) << 8; 129 index = msg[0].buf[0]; 130 request = CMD_READ; 131 132 ret = gl861_ctrl_msg(d, request, value, index, 133 msg[1].buf, msg[1].len); 134 } else if (num == 1 && (msg[0].flags & I2C_M_RD)) { 135 /* I2C read */ 136 if (msg[0].len > sizeof(ctx->buf)) { 137 ret = -EOPNOTSUPP; 138 goto err; 139 } 140 value = (msg[0].addr << 1) << 8; 141 index = 0x0100; 142 request = CMD_READ; 143 144 ret = gl861_ctrl_msg(d, request, value, index, 145 msg[0].buf, msg[0].len); 146 } else { 147 /* Unsupported I2C message */ 148 dev_dbg(&intf->dev, "unknown i2c msg, num %u\n", num); 149 ret = -EOPNOTSUPP; 150 } 151 if (ret) 152 goto err; 153 154 return num; 155 err: 156 dev_dbg(&intf->dev, "failed %d\n", ret); 157 return ret; 158 } 159 160 static u32 gl861_i2c_functionality(struct i2c_adapter *adapter) 161 { 162 return I2C_FUNC_I2C; 163 } 164 165 static struct i2c_algorithm gl861_i2c_algo = { 166 .master_xfer = gl861_i2c_master_xfer, 167 .functionality = gl861_i2c_functionality, 168 }; 169 170 /* Callbacks for DVB USB */ 171 static struct zl10353_config gl861_zl10353_config = { 172 .demod_address = 0x0f, 173 .no_tuner = 1, 174 .parallel_ts = 1, 175 }; 176 177 static int gl861_frontend_attach(struct dvb_usb_adapter *adap) 178 { 179 180 adap->fe[0] = dvb_attach(zl10353_attach, &gl861_zl10353_config, 181 &adap_to_d(adap)->i2c_adap); 182 if (adap->fe[0] == NULL) 183 return -EIO; 184 185 return 0; 186 } 187 188 static struct qt1010_config gl861_qt1010_config = { 189 .i2c_address = 0x62 190 }; 191 192 static int gl861_tuner_attach(struct dvb_usb_adapter *adap) 193 { 194 return dvb_attach(qt1010_attach, 195 adap->fe[0], &adap_to_d(adap)->i2c_adap, 196 &gl861_qt1010_config) == NULL ? -ENODEV : 0; 197 } 198 199 static int gl861_init(struct dvb_usb_device *d) 200 { 201 /* 202 * There is 2 interfaces. Interface 0 is for TV and interface 1 is 203 * for HID remote controller. Interface 0 has 2 alternate settings. 204 * For some reason we need to set interface explicitly, defaulted 205 * as alternate setting 1? 206 */ 207 return usb_set_interface(d->udev, 0, 0); 208 } 209 210 /* DVB USB Driver stuff */ 211 static struct dvb_usb_device_properties gl861_props = { 212 .driver_name = KBUILD_MODNAME, 213 .owner = THIS_MODULE, 214 .adapter_nr = adapter_nr, 215 216 .size_of_priv = sizeof(struct gl861), 217 218 .i2c_algo = &gl861_i2c_algo, 219 .frontend_attach = gl861_frontend_attach, 220 .tuner_attach = gl861_tuner_attach, 221 .init = gl861_init, 222 223 .num_adapters = 1, 224 .adapter = { 225 { 226 .stream = DVB_USB_STREAM_BULK(0x81, 7, 512), 227 } 228 } 229 }; 230 231 232 /* 233 * For Friio 234 */ 235 struct friio_config { 236 struct i2c_board_info demod_info; 237 struct tc90522_config demod_cfg; 238 239 struct i2c_board_info tuner_info; 240 struct dvb_pll_config tuner_cfg; 241 }; 242 243 static const struct friio_config friio_config = { 244 .demod_info = { I2C_BOARD_INFO(TC90522_I2C_DEV_TER, 0x18), }, 245 .demod_cfg = { .split_tuner_read_i2c = true, }, 246 .tuner_info = { I2C_BOARD_INFO("tua6034_friio", 0x60), }, 247 }; 248 249 250 /* GPIO control in Friio */ 251 252 #define FRIIO_CTL_LNB (1 << 0) 253 #define FRIIO_CTL_STROBE (1 << 1) 254 #define FRIIO_CTL_CLK (1 << 2) 255 #define FRIIO_CTL_LED (1 << 3) 256 257 #define FRIIO_LED_RUNNING 0x6400ff64 258 #define FRIIO_LED_STOPPED 0x96ff00ff 259 260 /* control PIC16F676 attached to Friio */ 261 static int friio_ext_ctl(struct dvb_usb_device *d, 262 u32 sat_color, int power_on) 263 { 264 int i, ret; 265 struct i2c_msg msg; 266 u8 *buf; 267 u32 mask; 268 u8 power = (power_on) ? FRIIO_CTL_LNB : 0; 269 270 buf = kmalloc(2, GFP_KERNEL); 271 if (!buf) 272 return -ENOMEM; 273 274 msg.addr = 0x00; 275 msg.flags = 0; 276 msg.len = 2; 277 msg.buf = buf; 278 buf[0] = 0x00; 279 280 /* send 2bit header (&B10) */ 281 buf[1] = power | FRIIO_CTL_LED | FRIIO_CTL_STROBE; 282 ret = i2c_transfer(&d->i2c_adap, &msg, 1); 283 buf[1] |= FRIIO_CTL_CLK; 284 ret += i2c_transfer(&d->i2c_adap, &msg, 1); 285 286 buf[1] = power | FRIIO_CTL_STROBE; 287 ret += i2c_transfer(&d->i2c_adap, &msg, 1); 288 buf[1] |= FRIIO_CTL_CLK; 289 ret += i2c_transfer(&d->i2c_adap, &msg, 1); 290 291 /* send 32bit(satur, R, G, B) data in serial */ 292 mask = 1UL << 31; 293 for (i = 0; i < 32; i++) { 294 buf[1] = power | FRIIO_CTL_STROBE; 295 if (sat_color & mask) 296 buf[1] |= FRIIO_CTL_LED; 297 ret += i2c_transfer(&d->i2c_adap, &msg, 1); 298 buf[1] |= FRIIO_CTL_CLK; 299 ret += i2c_transfer(&d->i2c_adap, &msg, 1); 300 mask >>= 1; 301 } 302 303 /* set the strobe off */ 304 buf[1] = power; 305 ret += i2c_transfer(&d->i2c_adap, &msg, 1); 306 buf[1] |= FRIIO_CTL_CLK; 307 ret += i2c_transfer(&d->i2c_adap, &msg, 1); 308 309 kfree(buf); 310 return (ret == 70) ? 0 : -EREMOTEIO; 311 } 312 313 /* init/config of gl861 for Friio */ 314 /* NOTE: 315 * This function cannot be moved to friio_init()/dvb_usbv2_init(), 316 * because the init defined here includes a whole device reset, 317 * it must be run early before any activities like I2C, 318 * but friio_init() is called by dvb-usbv2 after {_frontend, _tuner}_attach(), 319 * where I2C communication is used. 320 * In addition, this reset is required in reset_resume() as well. 321 * Thus this function is set to be called from _power_ctl(). 322 * 323 * Since it will be called on the early init stage 324 * where the i2c adapter is not initialized yet, 325 * we cannot use i2c_transfer() here. 326 */ 327 static int friio_reset(struct dvb_usb_device *d) 328 { 329 int i, ret; 330 u8 wbuf[1], rbuf[2]; 331 332 static const u8 friio_init_cmds[][2] = { 333 {0x33, 0x08}, {0x37, 0x40}, {0x3a, 0x1f}, {0x3b, 0xff}, 334 {0x3c, 0x1f}, {0x3d, 0xff}, {0x38, 0x00}, {0x35, 0x00}, 335 {0x39, 0x00}, {0x36, 0x00}, 336 }; 337 338 ret = usb_set_interface(d->udev, 0, 0); 339 if (ret < 0) 340 return ret; 341 342 ret = gl861_short_write(d, 0x00, 0x11, 0x02); 343 if (ret < 0) 344 return ret; 345 usleep_range(2000, 3000); 346 347 ret = gl861_short_write(d, 0x00, 0x11, 0x00); 348 if (ret < 0) 349 return ret; 350 351 /* 352 * Check if the dev is really a Friio White, since it might be 353 * another device, Friio Black, with the same VID/PID. 354 */ 355 356 usleep_range(1000, 2000); 357 wbuf[0] = 0x80; 358 ret = gl861_ctrl_msg(d, CMD_WRITE, 0x09 << 9, 0x03, wbuf, 1); 359 if (ret < 0) 360 return ret; 361 362 usleep_range(2000, 3000); 363 ret = gl861_ctrl_msg(d, CMD_READ, 0x09 << 9, 0x0100, rbuf, 2); 364 if (ret < 0) 365 return ret; 366 if (rbuf[0] != 0xff || rbuf[1] != 0xff) 367 return -ENODEV; 368 369 370 usleep_range(1000, 2000); 371 wbuf[0] = 0x80; 372 ret = gl861_ctrl_msg(d, CMD_WRITE, 0x48 << 9, 0x03, wbuf, 1); 373 if (ret < 0) 374 return ret; 375 376 usleep_range(2000, 3000); 377 ret = gl861_ctrl_msg(d, CMD_READ, 0x48 << 9, 0x0100, rbuf, 2); 378 if (ret < 0) 379 return ret; 380 if (rbuf[0] != 0xff || rbuf[1] != 0xff) 381 return -ENODEV; 382 383 ret = gl861_short_write(d, 0x00, 0x30, 0x04); 384 if (ret < 0) 385 return ret; 386 387 ret = gl861_short_write(d, 0x00, 0x00, 0x01); 388 if (ret < 0) 389 return ret; 390 391 ret = gl861_short_write(d, 0x00, 0x06, 0x0f); 392 if (ret < 0) 393 return ret; 394 395 for (i = 0; i < ARRAY_SIZE(friio_init_cmds); i++) { 396 ret = gl861_short_write(d, 0x00, friio_init_cmds[i][0], 397 friio_init_cmds[i][1]); 398 if (ret < 0) 399 return ret; 400 } 401 return 0; 402 } 403 404 /* 405 * DVB callbacks for Friio 406 */ 407 408 static int friio_power_ctrl(struct dvb_usb_device *d, int onoff) 409 { 410 return onoff ? friio_reset(d) : 0; 411 } 412 413 static int friio_frontend_attach(struct dvb_usb_adapter *adap) 414 { 415 const struct i2c_board_info *info; 416 struct dvb_usb_device *d; 417 struct tc90522_config cfg; 418 struct i2c_client *cl; 419 struct gl861 *priv; 420 421 info = &friio_config.demod_info; 422 cfg = friio_config.demod_cfg; 423 d = adap_to_d(adap); 424 cl = dvb_module_probe("tc90522", info->type, 425 &d->i2c_adap, info->addr, &cfg); 426 if (!cl) 427 return -ENODEV; 428 adap->fe[0] = cfg.fe; 429 430 priv = adap_to_priv(adap); 431 priv->i2c_client_demod = cl; 432 priv->demod_sub_i2c = cfg.tuner_i2c; 433 return 0; 434 } 435 436 static int friio_frontend_detach(struct dvb_usb_adapter *adap) 437 { 438 struct gl861 *priv; 439 440 priv = adap_to_priv(adap); 441 dvb_module_release(priv->i2c_client_demod); 442 return 0; 443 } 444 445 static int friio_tuner_attach(struct dvb_usb_adapter *adap) 446 { 447 const struct i2c_board_info *info; 448 struct dvb_pll_config cfg; 449 struct i2c_client *cl; 450 struct gl861 *priv; 451 452 priv = adap_to_priv(adap); 453 info = &friio_config.tuner_info; 454 cfg = friio_config.tuner_cfg; 455 cfg.fe = adap->fe[0]; 456 457 cl = dvb_module_probe("dvb_pll", info->type, 458 priv->demod_sub_i2c, info->addr, &cfg); 459 if (!cl) 460 return -ENODEV; 461 priv->i2c_client_tuner = cl; 462 return 0; 463 } 464 465 static int friio_tuner_detach(struct dvb_usb_adapter *adap) 466 { 467 struct gl861 *priv; 468 469 priv = adap_to_priv(adap); 470 dvb_module_release(priv->i2c_client_tuner); 471 return 0; 472 } 473 474 static int friio_init(struct dvb_usb_device *d) 475 { 476 int i; 477 int ret; 478 struct gl861 *priv; 479 480 static const u8 demod_init[][2] = { 481 {0x01, 0x40}, {0x04, 0x38}, {0x05, 0x40}, {0x07, 0x40}, 482 {0x0f, 0x4f}, {0x11, 0x21}, {0x12, 0x0b}, {0x13, 0x2f}, 483 {0x14, 0x31}, {0x16, 0x02}, {0x21, 0xc4}, {0x22, 0x20}, 484 {0x2c, 0x79}, {0x2d, 0x34}, {0x2f, 0x00}, {0x30, 0x28}, 485 {0x31, 0x31}, {0x32, 0xdf}, {0x38, 0x01}, {0x39, 0x78}, 486 {0x3b, 0x33}, {0x3c, 0x33}, {0x48, 0x90}, {0x51, 0x68}, 487 {0x5e, 0x38}, {0x71, 0x00}, {0x72, 0x08}, {0x77, 0x00}, 488 {0xc0, 0x21}, {0xc1, 0x10}, {0xe4, 0x1a}, {0xea, 0x1f}, 489 {0x77, 0x00}, {0x71, 0x00}, {0x71, 0x00}, {0x76, 0x0c}, 490 }; 491 492 /* power on LNA? */ 493 ret = friio_ext_ctl(d, FRIIO_LED_STOPPED, true); 494 if (ret < 0) 495 return ret; 496 msleep(20); 497 498 /* init/config demod */ 499 priv = d_to_priv(d); 500 for (i = 0; i < ARRAY_SIZE(demod_init); i++) { 501 int ret; 502 503 ret = i2c_master_send(priv->i2c_client_demod, demod_init[i], 2); 504 if (ret < 0) 505 return ret; 506 } 507 msleep(100); 508 return 0; 509 } 510 511 static void friio_exit(struct dvb_usb_device *d) 512 { 513 friio_ext_ctl(d, FRIIO_LED_STOPPED, false); 514 } 515 516 static int friio_streaming_ctrl(struct dvb_frontend *fe, int onoff) 517 { 518 u32 led_color; 519 520 led_color = onoff ? FRIIO_LED_RUNNING : FRIIO_LED_STOPPED; 521 return friio_ext_ctl(fe_to_d(fe), led_color, true); 522 } 523 524 525 static struct dvb_usb_device_properties friio_props = { 526 .driver_name = KBUILD_MODNAME, 527 .owner = THIS_MODULE, 528 .adapter_nr = adapter_nr, 529 530 .size_of_priv = sizeof(struct gl861), 531 532 .i2c_algo = &gl861_i2c_algo, 533 .power_ctrl = friio_power_ctrl, 534 .frontend_attach = friio_frontend_attach, 535 .frontend_detach = friio_frontend_detach, 536 .tuner_attach = friio_tuner_attach, 537 .tuner_detach = friio_tuner_detach, 538 .init = friio_init, 539 .exit = friio_exit, 540 .streaming_ctrl = friio_streaming_ctrl, 541 542 .num_adapters = 1, 543 .adapter = { 544 { 545 .stream = DVB_USB_STREAM_BULK(0x01, 8, 16384), 546 } 547 } 548 }; 549 550 static const struct usb_device_id gl861_id_table[] = { 551 { DVB_USB_DEVICE(USB_VID_MSI, USB_PID_MSI_MEGASKY580_55801, 552 &gl861_props, "MSI Mega Sky 55801 DVB-T USB2.0", NULL) }, 553 { DVB_USB_DEVICE(USB_VID_ALINK, USB_PID_ALINK_DTU, 554 &gl861_props, "A-LINK DTU DVB-T USB2.0", NULL) }, 555 { DVB_USB_DEVICE(USB_VID_774, USB_PID_FRIIO_WHITE, 556 &friio_props, "774 Friio White ISDB-T USB2.0", NULL) }, 557 { } 558 }; 559 MODULE_DEVICE_TABLE(usb, gl861_id_table); 560 561 static struct usb_driver gl861_usb_driver = { 562 .name = KBUILD_MODNAME, 563 .id_table = gl861_id_table, 564 .probe = dvb_usbv2_probe, 565 .disconnect = dvb_usbv2_disconnect, 566 .suspend = dvb_usbv2_suspend, 567 .resume = dvb_usbv2_resume, 568 .reset_resume = dvb_usbv2_reset_resume, 569 .no_dynamic_id = 1, 570 .soft_unbind = 1, 571 }; 572 573 module_usb_driver(gl861_usb_driver); 574 575 MODULE_AUTHOR("Carl Lundqvist <comabug@gmail.com>"); 576 MODULE_DESCRIPTION("Driver MSI Mega Sky 580 DVB-T USB2.0 / GL861"); 577 MODULE_VERSION("0.1"); 578 MODULE_LICENSE("GPL"); 579