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