]> asedeno.scripts.mit.edu Git - linux.git/blob - drivers/media/usb/dvb-usb-v2/gl861.c
f5f774ed05b87f5a458d5c82d2c93ac9274af911
[linux.git] / drivers / media / usb / dvb-usb-v2 / gl861.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /* DVB USB compliant linux driver for GL861 USB2.0 devices.
3  *
4  * see Documentation/media/dvb-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                 /* Fall through */
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         .tuner_info = { I2C_BOARD_INFO("tua6034_friio", 0x60), },
246 };
247
248
249 /* GPIO control in Friio */
250
251 #define FRIIO_CTL_LNB (1 << 0)
252 #define FRIIO_CTL_STROBE (1 << 1)
253 #define FRIIO_CTL_CLK (1 << 2)
254 #define FRIIO_CTL_LED (1 << 3)
255
256 #define FRIIO_LED_RUNNING 0x6400ff64
257 #define FRIIO_LED_STOPPED 0x96ff00ff
258
259 /* control PIC16F676 attached to Friio */
260 static int friio_ext_ctl(struct dvb_usb_device *d,
261                             u32 sat_color, int power_on)
262 {
263         int i, ret;
264         struct i2c_msg msg;
265         u8 *buf;
266         u32 mask;
267         u8 power = (power_on) ? FRIIO_CTL_LNB : 0;
268
269         buf = kmalloc(2, GFP_KERNEL);
270         if (!buf)
271                 return -ENOMEM;
272
273         msg.addr = 0x00;
274         msg.flags = 0;
275         msg.len = 2;
276         msg.buf = buf;
277         buf[0] = 0x00;
278
279         /* send 2bit header (&B10) */
280         buf[1] = power | FRIIO_CTL_LED | FRIIO_CTL_STROBE;
281         ret = i2c_transfer(&d->i2c_adap, &msg, 1);
282         buf[1] |= FRIIO_CTL_CLK;
283         ret += i2c_transfer(&d->i2c_adap, &msg, 1);
284
285         buf[1] = power | FRIIO_CTL_STROBE;
286         ret += i2c_transfer(&d->i2c_adap, &msg, 1);
287         buf[1] |= FRIIO_CTL_CLK;
288         ret += i2c_transfer(&d->i2c_adap, &msg, 1);
289
290         /* send 32bit(satur, R, G, B) data in serial */
291         mask = 1UL << 31;
292         for (i = 0; i < 32; i++) {
293                 buf[1] = power | FRIIO_CTL_STROBE;
294                 if (sat_color & mask)
295                         buf[1] |= FRIIO_CTL_LED;
296                 ret += i2c_transfer(&d->i2c_adap, &msg, 1);
297                 buf[1] |= FRIIO_CTL_CLK;
298                 ret += i2c_transfer(&d->i2c_adap, &msg, 1);
299                 mask >>= 1;
300         }
301
302         /* set the strobe off */
303         buf[1] = power;
304         ret += i2c_transfer(&d->i2c_adap, &msg, 1);
305         buf[1] |= FRIIO_CTL_CLK;
306         ret += i2c_transfer(&d->i2c_adap, &msg, 1);
307
308         kfree(buf);
309         return (ret == 70) ? 0 : -EREMOTEIO;
310 }
311
312 /* init/config of gl861 for Friio */
313 /* NOTE:
314  * This function cannot be moved to friio_init()/dvb_usbv2_init(),
315  * because the init defined here includes a whole device reset,
316  * it must be run early before any activities like I2C,
317  * but friio_init() is called by dvb-usbv2 after {_frontend, _tuner}_attach(),
318  * where I2C communication is used.
319  * In addition, this reset is required in reset_resume() as well.
320  * Thus this function is set to be called from _power_ctl().
321  *
322  * Since it will be called on the early init stage
323  * where the i2c adapter is not initialized yet,
324  * we cannot use i2c_transfer() here.
325  */
326 static int friio_reset(struct dvb_usb_device *d)
327 {
328         int i, ret;
329         u8 wbuf[1], rbuf[2];
330
331         static const u8 friio_init_cmds[][2] = {
332                 {0x33, 0x08}, {0x37, 0x40}, {0x3a, 0x1f}, {0x3b, 0xff},
333                 {0x3c, 0x1f}, {0x3d, 0xff}, {0x38, 0x00}, {0x35, 0x00},
334                 {0x39, 0x00}, {0x36, 0x00},
335         };
336
337         ret = usb_set_interface(d->udev, 0, 0);
338         if (ret < 0)
339                 return ret;
340
341         ret = gl861_short_write(d, 0x00, 0x11, 0x02);
342         if (ret < 0)
343                 return ret;
344         usleep_range(2000, 3000);
345
346         ret = gl861_short_write(d, 0x00, 0x11, 0x00);
347         if (ret < 0)
348                 return ret;
349
350         /*
351          * Check if the dev is really a Friio White, since it might be
352          * another device, Friio Black, with the same VID/PID.
353          */
354
355         usleep_range(1000, 2000);
356         wbuf[0] = 0x80;
357         ret = gl861_ctrl_msg(d, CMD_WRITE, 0x09 << 9, 0x03, wbuf, 1);
358         if (ret < 0)
359                 return ret;
360
361         usleep_range(2000, 3000);
362         ret = gl861_ctrl_msg(d, CMD_READ, 0x09 << 9, 0x0100, rbuf, 2);
363         if (ret < 0)
364                 return ret;
365         if (rbuf[0] != 0xff || rbuf[1] != 0xff)
366                 return -ENODEV;
367
368
369         usleep_range(1000, 2000);
370         wbuf[0] = 0x80;
371         ret = gl861_ctrl_msg(d, CMD_WRITE, 0x48 << 9, 0x03, wbuf, 1);
372         if (ret < 0)
373                 return ret;
374
375         usleep_range(2000, 3000);
376         ret = gl861_ctrl_msg(d, CMD_READ, 0x48 << 9, 0x0100, rbuf, 2);
377         if (ret < 0)
378                 return ret;
379         if (rbuf[0] != 0xff || rbuf[1] != 0xff)
380                 return -ENODEV;
381
382         ret = gl861_short_write(d, 0x00, 0x30, 0x04);
383         if (ret < 0)
384                 return ret;
385
386         ret = gl861_short_write(d, 0x00, 0x00, 0x01);
387         if (ret < 0)
388                 return ret;
389
390         ret = gl861_short_write(d, 0x00, 0x06, 0x0f);
391         if (ret < 0)
392                 return ret;
393
394         for (i = 0; i < ARRAY_SIZE(friio_init_cmds); i++) {
395                 ret = gl861_short_write(d, 0x00, friio_init_cmds[i][0],
396                                         friio_init_cmds[i][1]);
397                 if (ret < 0)
398                         return ret;
399         }
400         return 0;
401 }
402
403 /*
404  * DVB callbacks for Friio
405  */
406
407 static int friio_power_ctrl(struct dvb_usb_device *d, int onoff)
408 {
409         return onoff ? friio_reset(d) : 0;
410 }
411
412 static int friio_frontend_attach(struct dvb_usb_adapter *adap)
413 {
414         const struct i2c_board_info *info;
415         struct dvb_usb_device *d;
416         struct tc90522_config cfg;
417         struct i2c_client *cl;
418         struct gl861 *priv;
419
420         info = &friio_config.demod_info;
421         cfg = friio_config.demod_cfg;
422         d = adap_to_d(adap);
423         cl = dvb_module_probe("tc90522", info->type,
424                               &d->i2c_adap, info->addr, &cfg);
425         if (!cl)
426                 return -ENODEV;
427         adap->fe[0] = cfg.fe;
428
429         priv = adap_to_priv(adap);
430         priv->i2c_client_demod = cl;
431         priv->demod_sub_i2c = cfg.tuner_i2c;
432         return 0;
433 }
434
435 static int friio_frontend_detach(struct dvb_usb_adapter *adap)
436 {
437         struct gl861 *priv;
438
439         priv = adap_to_priv(adap);
440         dvb_module_release(priv->i2c_client_demod);
441         return 0;
442 }
443
444 static int friio_tuner_attach(struct dvb_usb_adapter *adap)
445 {
446         const struct i2c_board_info *info;
447         struct dvb_pll_config cfg;
448         struct i2c_client *cl;
449         struct gl861 *priv;
450
451         priv = adap_to_priv(adap);
452         info = &friio_config.tuner_info;
453         cfg = friio_config.tuner_cfg;
454         cfg.fe = adap->fe[0];
455
456         cl = dvb_module_probe("dvb_pll", info->type,
457                               priv->demod_sub_i2c, info->addr, &cfg);
458         if (!cl)
459                 return -ENODEV;
460         priv->i2c_client_tuner = cl;
461         return 0;
462 }
463
464 static int friio_tuner_detach(struct dvb_usb_adapter *adap)
465 {
466         struct gl861 *priv;
467
468         priv = adap_to_priv(adap);
469         dvb_module_release(priv->i2c_client_tuner);
470         return 0;
471 }
472
473 static int friio_init(struct dvb_usb_device *d)
474 {
475         int i;
476         int ret;
477         struct gl861 *priv;
478
479         static const u8 demod_init[][2] = {
480                 {0x01, 0x40}, {0x04, 0x38}, {0x05, 0x40}, {0x07, 0x40},
481                 {0x0f, 0x4f}, {0x11, 0x21}, {0x12, 0x0b}, {0x13, 0x2f},
482                 {0x14, 0x31}, {0x16, 0x02}, {0x21, 0xc4}, {0x22, 0x20},
483                 {0x2c, 0x79}, {0x2d, 0x34}, {0x2f, 0x00}, {0x30, 0x28},
484                 {0x31, 0x31}, {0x32, 0xdf}, {0x38, 0x01}, {0x39, 0x78},
485                 {0x3b, 0x33}, {0x3c, 0x33}, {0x48, 0x90}, {0x51, 0x68},
486                 {0x5e, 0x38}, {0x71, 0x00}, {0x72, 0x08}, {0x77, 0x00},
487                 {0xc0, 0x21}, {0xc1, 0x10}, {0xe4, 0x1a}, {0xea, 0x1f},
488                 {0x77, 0x00}, {0x71, 0x00}, {0x71, 0x00}, {0x76, 0x0c},
489         };
490
491         /* power on LNA? */
492         ret = friio_ext_ctl(d, FRIIO_LED_STOPPED, true);
493         if (ret < 0)
494                 return ret;
495         msleep(20);
496
497         /* init/config demod */
498         priv = d_to_priv(d);
499         for (i = 0; i < ARRAY_SIZE(demod_init); i++) {
500                 int ret;
501
502                 ret = i2c_master_send(priv->i2c_client_demod, demod_init[i], 2);
503                 if (ret < 0)
504                         return ret;
505         }
506         msleep(100);
507         return 0;
508 }
509
510 static void friio_exit(struct dvb_usb_device *d)
511 {
512         friio_ext_ctl(d, FRIIO_LED_STOPPED, false);
513 }
514
515 static int friio_streaming_ctrl(struct dvb_frontend *fe, int onoff)
516 {
517         u32 led_color;
518
519         led_color = onoff ? FRIIO_LED_RUNNING : FRIIO_LED_STOPPED;
520         return friio_ext_ctl(fe_to_d(fe), led_color, true);
521 }
522
523
524 static struct dvb_usb_device_properties friio_props = {
525         .driver_name = KBUILD_MODNAME,
526         .owner = THIS_MODULE,
527         .adapter_nr = adapter_nr,
528
529         .size_of_priv = sizeof(struct gl861),
530
531         .i2c_algo = &gl861_i2c_algo,
532         .power_ctrl = friio_power_ctrl,
533         .frontend_attach = friio_frontend_attach,
534         .frontend_detach = friio_frontend_detach,
535         .tuner_attach = friio_tuner_attach,
536         .tuner_detach = friio_tuner_detach,
537         .init = friio_init,
538         .exit = friio_exit,
539         .streaming_ctrl = friio_streaming_ctrl,
540
541         .num_adapters = 1,
542         .adapter = {
543                 {
544                         .stream = DVB_USB_STREAM_BULK(0x01, 8, 16384),
545                 }
546         }
547 };
548
549 static const struct usb_device_id gl861_id_table[] = {
550         { DVB_USB_DEVICE(USB_VID_MSI, USB_PID_MSI_MEGASKY580_55801,
551                 &gl861_props, "MSI Mega Sky 55801 DVB-T USB2.0", NULL) },
552         { DVB_USB_DEVICE(USB_VID_ALINK, USB_VID_ALINK_DTU,
553                 &gl861_props, "A-LINK DTU DVB-T USB2.0", NULL) },
554         { DVB_USB_DEVICE(USB_VID_774, USB_PID_FRIIO_WHITE,
555                 &friio_props, "774 Friio White ISDB-T USB2.0", NULL) },
556         { }
557 };
558 MODULE_DEVICE_TABLE(usb, gl861_id_table);
559
560 static struct usb_driver gl861_usb_driver = {
561         .name = KBUILD_MODNAME,
562         .id_table = gl861_id_table,
563         .probe = dvb_usbv2_probe,
564         .disconnect = dvb_usbv2_disconnect,
565         .suspend = dvb_usbv2_suspend,
566         .resume = dvb_usbv2_resume,
567         .reset_resume = dvb_usbv2_reset_resume,
568         .no_dynamic_id = 1,
569         .soft_unbind = 1,
570 };
571
572 module_usb_driver(gl861_usb_driver);
573
574 MODULE_AUTHOR("Carl Lundqvist <comabug@gmail.com>");
575 MODULE_DESCRIPTION("Driver MSI Mega Sky 580 DVB-T USB2.0 / GL861");
576 MODULE_VERSION("0.1");
577 MODULE_LICENSE("GPL");