gl861.c 14 KB

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