1 // SPDX-License-Identifier: GPL-2.0-only
2 /* DVB USB compliant linux driver for GL861 USB2.0 devices.
4 * see Documentation/driver-api/media/drivers/dvb-usb.rst for more information
6 #include <linux/string.h>
15 DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr
);
18 /* USB control message buffer */
21 struct i2c_adapter
*demod_sub_i2c
;
22 struct i2c_client
*i2c_client_demod
;
23 struct i2c_client
*i2c_client_tuner
;
26 #define CMD_WRITE_SHORT 0x01
28 #define CMD_WRITE 0x03
30 static int gl861_ctrl_msg(struct dvb_usb_device
*d
, u8 request
, u16 value
,
31 u16 index
, void *data
, u16 size
)
33 struct gl861
*ctx
= d_to_priv(d
);
34 struct usb_interface
*intf
= d
->intf
;
39 mutex_lock(&d
->usb_mutex
);
43 memcpy(ctx
->buf
, data
, size
);
46 pipe
= usb_sndctrlpipe(d
->udev
, 0);
47 requesttype
= USB_TYPE_VENDOR
| USB_DIR_OUT
;
50 pipe
= usb_rcvctrlpipe(d
->udev
, 0);
51 requesttype
= USB_TYPE_VENDOR
| USB_DIR_IN
;
55 goto err_mutex_unlock
;
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
);
64 goto err_mutex_unlock
;
66 if (request
== CMD_READ
)
67 memcpy(data
, ctx
->buf
, size
);
69 usleep_range(1000, 2000); /* Avoid I2C errors */
71 mutex_unlock(&d
->usb_mutex
);
76 mutex_unlock(&d
->usb_mutex
);
77 dev_dbg(&intf
->dev
, "failed %d\n", ret
);
81 static int gl861_short_write(struct dvb_usb_device
*d
, u8 addr
, u8 reg
, u8 val
)
83 return gl861_ctrl_msg(d
, CMD_WRITE_SHORT
,
84 (addr
<< 9) | val
, reg
, NULL
, 0);
87 static int gl861_i2c_master_xfer(struct i2c_adapter
*adap
, struct i2c_msg msg
[],
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
);
95 u16 value
, index
, size
;
97 /* XXX: I2C adapter maximum data lengths are not tested */
98 if (num
== 1 && !(msg
[0].flags
& I2C_M_RD
)) {
100 if (msg
[0].len
< 2 || msg
[0].len
> sizeof(ctx
->buf
)) {
105 value
= (msg
[0].addr
<< 1) << 8;
106 index
= msg
[0].buf
[0];
108 if (msg
[0].len
== 2) {
109 request
= CMD_WRITE_SHORT
;
110 value
|= msg
[0].buf
[1];
115 size
= msg
[0].len
- 1;
116 data
= &msg
[0].buf
[1];
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
)) {
128 value
= (msg
[0].addr
<< 1) << 8;
129 index
= msg
[0].buf
[0];
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
)) {
136 if (msg
[0].len
> sizeof(ctx
->buf
)) {
140 value
= (msg
[0].addr
<< 1) << 8;
144 ret
= gl861_ctrl_msg(d
, request
, value
, index
,
145 msg
[0].buf
, msg
[0].len
);
147 /* Unsupported I2C message */
148 dev_dbg(&intf
->dev
, "unknown i2c msg, num %u\n", num
);
156 dev_dbg(&intf
->dev
, "failed %d\n", ret
);
160 static u32
gl861_i2c_functionality(struct i2c_adapter
*adapter
)
165 static struct i2c_algorithm gl861_i2c_algo
= {
166 .master_xfer
= gl861_i2c_master_xfer
,
167 .functionality
= gl861_i2c_functionality
,
170 /* Callbacks for DVB USB */
171 static struct zl10353_config gl861_zl10353_config
= {
172 .demod_address
= 0x0f,
177 static int gl861_frontend_attach(struct dvb_usb_adapter
*adap
)
180 adap
->fe
[0] = dvb_attach(zl10353_attach
, &gl861_zl10353_config
,
181 &adap_to_d(adap
)->i2c_adap
);
182 if (adap
->fe
[0] == NULL
)
188 static struct qt1010_config gl861_qt1010_config
= {
192 static int gl861_tuner_attach(struct dvb_usb_adapter
*adap
)
194 return dvb_attach(qt1010_attach
,
195 adap
->fe
[0], &adap_to_d(adap
)->i2c_adap
,
196 &gl861_qt1010_config
) == NULL
? -ENODEV
: 0;
199 static int gl861_init(struct dvb_usb_device
*d
)
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?
207 return usb_set_interface(d
->udev
, 0, 0);
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
,
216 .size_of_priv
= sizeof(struct gl861
),
218 .i2c_algo
= &gl861_i2c_algo
,
219 .frontend_attach
= gl861_frontend_attach
,
220 .tuner_attach
= gl861_tuner_attach
,
226 .stream
= DVB_USB_STREAM_BULK(0x81, 7, 512),
235 struct friio_config
{
236 struct i2c_board_info demod_info
;
237 struct tc90522_config demod_cfg
;
239 struct i2c_board_info tuner_info
;
240 struct dvb_pll_config tuner_cfg
;
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), },
250 /* GPIO control in Friio */
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)
257 #define FRIIO_LED_RUNNING 0x6400ff64
258 #define FRIIO_LED_STOPPED 0x96ff00ff
260 /* control PIC16F676 attached to Friio */
261 static int friio_ext_ctl(struct dvb_usb_device
*d
,
262 u32 sat_color
, int power_on
)
268 u8 power
= (power_on
) ? FRIIO_CTL_LNB
: 0;
270 buf
= kmalloc(2, GFP_KERNEL
);
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);
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);
291 /* send 32bit(satur, R, G, B) data in serial */
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);
303 /* set the strobe off */
305 ret
+= i2c_transfer(&d
->i2c_adap
, &msg
, 1);
306 buf
[1] |= FRIIO_CTL_CLK
;
307 ret
+= i2c_transfer(&d
->i2c_adap
, &msg
, 1);
310 return (ret
== 70) ? 0 : -EREMOTEIO
;
313 /* init/config of gl861 for Friio */
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().
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.
327 static int friio_reset(struct dvb_usb_device
*d
)
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},
338 ret
= usb_set_interface(d
->udev
, 0, 0);
342 ret
= gl861_short_write(d
, 0x00, 0x11, 0x02);
345 usleep_range(2000, 3000);
347 ret
= gl861_short_write(d
, 0x00, 0x11, 0x00);
352 * Check if the dev is really a Friio White, since it might be
353 * another device, Friio Black, with the same VID/PID.
356 usleep_range(1000, 2000);
358 ret
= gl861_ctrl_msg(d
, CMD_WRITE
, 0x09 << 9, 0x03, wbuf
, 1);
362 usleep_range(2000, 3000);
363 ret
= gl861_ctrl_msg(d
, CMD_READ
, 0x09 << 9, 0x0100, rbuf
, 2);
366 if (rbuf
[0] != 0xff || rbuf
[1] != 0xff)
370 usleep_range(1000, 2000);
372 ret
= gl861_ctrl_msg(d
, CMD_WRITE
, 0x48 << 9, 0x03, wbuf
, 1);
376 usleep_range(2000, 3000);
377 ret
= gl861_ctrl_msg(d
, CMD_READ
, 0x48 << 9, 0x0100, rbuf
, 2);
380 if (rbuf
[0] != 0xff || rbuf
[1] != 0xff)
383 ret
= gl861_short_write(d
, 0x00, 0x30, 0x04);
387 ret
= gl861_short_write(d
, 0x00, 0x00, 0x01);
391 ret
= gl861_short_write(d
, 0x00, 0x06, 0x0f);
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]);
405 * DVB callbacks for Friio
408 static int friio_power_ctrl(struct dvb_usb_device
*d
, int onoff
)
410 return onoff
? friio_reset(d
) : 0;
413 static int friio_frontend_attach(struct dvb_usb_adapter
*adap
)
415 const struct i2c_board_info
*info
;
416 struct dvb_usb_device
*d
;
417 struct tc90522_config cfg
;
418 struct i2c_client
*cl
;
421 info
= &friio_config
.demod_info
;
422 cfg
= friio_config
.demod_cfg
;
424 cl
= dvb_module_probe("tc90522", info
->type
,
425 &d
->i2c_adap
, info
->addr
, &cfg
);
428 adap
->fe
[0] = cfg
.fe
;
430 priv
= adap_to_priv(adap
);
431 priv
->i2c_client_demod
= cl
;
432 priv
->demod_sub_i2c
= cfg
.tuner_i2c
;
436 static int friio_frontend_detach(struct dvb_usb_adapter
*adap
)
440 priv
= adap_to_priv(adap
);
441 dvb_module_release(priv
->i2c_client_demod
);
445 static int friio_tuner_attach(struct dvb_usb_adapter
*adap
)
447 const struct i2c_board_info
*info
;
448 struct dvb_pll_config cfg
;
449 struct i2c_client
*cl
;
452 priv
= adap_to_priv(adap
);
453 info
= &friio_config
.tuner_info
;
454 cfg
= friio_config
.tuner_cfg
;
455 cfg
.fe
= adap
->fe
[0];
457 cl
= dvb_module_probe("dvb_pll", info
->type
,
458 priv
->demod_sub_i2c
, info
->addr
, &cfg
);
461 priv
->i2c_client_tuner
= cl
;
465 static int friio_tuner_detach(struct dvb_usb_adapter
*adap
)
469 priv
= adap_to_priv(adap
);
470 dvb_module_release(priv
->i2c_client_tuner
);
474 static int friio_init(struct dvb_usb_device
*d
)
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},
493 ret
= friio_ext_ctl(d
, FRIIO_LED_STOPPED
, true);
498 /* init/config demod */
500 for (i
= 0; i
< ARRAY_SIZE(demod_init
); i
++) {
503 ret
= i2c_master_send(priv
->i2c_client_demod
, demod_init
[i
], 2);
511 static void friio_exit(struct dvb_usb_device
*d
)
513 friio_ext_ctl(d
, FRIIO_LED_STOPPED
, false);
516 static int friio_streaming_ctrl(struct dvb_frontend
*fe
, int onoff
)
520 led_color
= onoff
? FRIIO_LED_RUNNING
: FRIIO_LED_STOPPED
;
521 return friio_ext_ctl(fe_to_d(fe
), led_color
, true);
525 static struct dvb_usb_device_properties friio_props
= {
526 .driver_name
= KBUILD_MODNAME
,
527 .owner
= THIS_MODULE
,
528 .adapter_nr
= adapter_nr
,
530 .size_of_priv
= sizeof(struct gl861
),
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
,
540 .streaming_ctrl
= friio_streaming_ctrl
,
545 .stream
= DVB_USB_STREAM_BULK(0x01, 8, 16384),
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
) },
559 MODULE_DEVICE_TABLE(usb
, gl861_id_table
);
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
,
573 module_usb_driver(gl861_usb_driver
);
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");