Merge tag 'regmap-fix-v5.11-rc2' of git://git.kernel.org/pub/scm/linux/kernel/git...
[linux/fpc-iii.git] / drivers / media / usb / dvb-usb-v2 / gl861.c
blob0c434259c36f19034247f90d16151692db4eb356
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
5 */
6 #include <linux/string.h>
8 #include "dvb_usb.h"
10 #include "zl10353.h"
11 #include "qt1010.h"
12 #include "tc90522.h"
13 #include "dvb-pll.h"
15 DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
17 struct gl861 {
18 /* USB control message buffer */
19 u8 buf[16];
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
27 #define CMD_READ 0x02
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;
35 int ret;
36 unsigned int pipe;
37 u8 requesttype;
39 mutex_lock(&d->usb_mutex);
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;
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;
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);
73 return 0;
75 err_mutex_unlock:
76 mutex_unlock(&d->usb_mutex);
77 dev_dbg(&intf->dev, "failed %d\n", ret);
78 return 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[],
88 int num)
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;
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;
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];
111 size = 0;
112 data = NULL;
113 } else {
114 request = CMD_WRITE;
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)) {
124 ret = -EOPNOTSUPP;
125 goto err;
128 value = (msg[0].addr << 1) << 8;
129 index = msg[0].buf[0];
130 request = CMD_READ;
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;
140 value = (msg[0].addr << 1) << 8;
141 index = 0x0100;
142 request = CMD_READ;
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;
151 if (ret)
152 goto err;
154 return num;
155 err:
156 dev_dbg(&intf->dev, "failed %d\n", ret);
157 return ret;
160 static u32 gl861_i2c_functionality(struct i2c_adapter *adapter)
162 return I2C_FUNC_I2C;
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,
173 .no_tuner = 1,
174 .parallel_ts = 1,
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)
183 return -EIO;
185 return 0;
188 static struct qt1010_config gl861_qt1010_config = {
189 .i2c_address = 0x62
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,
221 .init = gl861_init,
223 .num_adapters = 1,
224 .adapter = {
226 .stream = DVB_USB_STREAM_BULK(0x81, 7, 512),
233 * For Friio
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)
264 int i, ret;
265 struct i2c_msg msg;
266 u8 *buf;
267 u32 mask;
268 u8 power = (power_on) ? FRIIO_CTL_LNB : 0;
270 buf = kmalloc(2, GFP_KERNEL);
271 if (!buf)
272 return -ENOMEM;
274 msg.addr = 0x00;
275 msg.flags = 0;
276 msg.len = 2;
277 msg.buf = buf;
278 buf[0] = 0x00;
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 */
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;
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);
309 kfree(buf);
310 return (ret == 70) ? 0 : -EREMOTEIO;
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().
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)
329 int i, ret;
330 u8 wbuf[1], rbuf[2];
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);
339 if (ret < 0)
340 return ret;
342 ret = gl861_short_write(d, 0x00, 0x11, 0x02);
343 if (ret < 0)
344 return ret;
345 usleep_range(2000, 3000);
347 ret = gl861_short_write(d, 0x00, 0x11, 0x00);
348 if (ret < 0)
349 return ret;
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);
357 wbuf[0] = 0x80;
358 ret = gl861_ctrl_msg(d, CMD_WRITE, 0x09 << 9, 0x03, wbuf, 1);
359 if (ret < 0)
360 return ret;
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;
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;
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;
383 ret = gl861_short_write(d, 0x00, 0x30, 0x04);
384 if (ret < 0)
385 return ret;
387 ret = gl861_short_write(d, 0x00, 0x00, 0x01);
388 if (ret < 0)
389 return ret;
391 ret = gl861_short_write(d, 0x00, 0x06, 0x0f);
392 if (ret < 0)
393 return ret;
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;
401 return 0;
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;
419 struct gl861 *priv;
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;
430 priv = adap_to_priv(adap);
431 priv->i2c_client_demod = cl;
432 priv->demod_sub_i2c = cfg.tuner_i2c;
433 return 0;
436 static int friio_frontend_detach(struct dvb_usb_adapter *adap)
438 struct gl861 *priv;
440 priv = adap_to_priv(adap);
441 dvb_module_release(priv->i2c_client_demod);
442 return 0;
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;
450 struct gl861 *priv;
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);
459 if (!cl)
460 return -ENODEV;
461 priv->i2c_client_tuner = cl;
462 return 0;
465 static int friio_tuner_detach(struct dvb_usb_adapter *adap)
467 struct gl861 *priv;
469 priv = adap_to_priv(adap);
470 dvb_module_release(priv->i2c_client_tuner);
471 return 0;
474 static int friio_init(struct dvb_usb_device *d)
476 int i;
477 int ret;
478 struct gl861 *priv;
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},
492 /* power on LNA? */
493 ret = friio_ext_ctl(d, FRIIO_LED_STOPPED, true);
494 if (ret < 0)
495 return ret;
496 msleep(20);
498 /* init/config demod */
499 priv = d_to_priv(d);
500 for (i = 0; i < ARRAY_SIZE(demod_init); i++) {
501 int ret;
503 ret = i2c_master_send(priv->i2c_client_demod, demod_init[i], 2);
504 if (ret < 0)
505 return ret;
507 msleep(100);
508 return 0;
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)
518 u32 led_color;
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,
538 .init = friio_init,
539 .exit = friio_exit,
540 .streaming_ctrl = friio_streaming_ctrl,
542 .num_adapters = 1,
543 .adapter = {
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,
569 .no_dynamic_id = 1,
570 .soft_unbind = 1,
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");