d9ee1a9c5cbefb95e031c993fffa24dff6aeb26a
[cascardo/linux.git] / drivers / media / usb / dvb-usb-v2 / rtl28xxu.c
1 /*
2  * Realtek RTL28xxU DVB USB driver
3  *
4  * Copyright (C) 2009 Antti Palosaari <crope@iki.fi>
5  * Copyright (C) 2011 Antti Palosaari <crope@iki.fi>
6  * Copyright (C) 2012 Thomas Mair <thomas.mair86@googlemail.com>
7  *
8  *    This program is free software; you can redistribute it and/or modify
9  *    it under the terms of the GNU General Public License as published by
10  *    the Free Software Foundation; either version 2 of the License, or
11  *    (at your option) any later version.
12  *
13  *    This program is distributed in the hope that it will be useful,
14  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
15  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  *    GNU General Public License for more details.
17  *
18  *    You should have received a copy of the GNU General Public License along
19  *    with this program; if not, write to the Free Software Foundation, Inc.,
20  *    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21  */
22
23 #include "rtl28xxu.h"
24
25 #include "rtl2830.h"
26 #include "rtl2832.h"
27 #include "mn88472.h"
28
29 #include "qt1010.h"
30 #include "mt2060.h"
31 #include "mxl5005s.h"
32 #include "fc0012.h"
33 #include "fc0013.h"
34 #include "e4000.h"
35 #include "fc2580.h"
36 #include "tua9001.h"
37 #include "r820t.h"
38
39 /*
40  * RTL2832_SDR module is in staging. That logic is added in order to avoid any
41  * hard dependency to drivers/staging/ directory as we want compile mainline
42  * driver even whole staging directory is missing.
43  */
44 #include <media/v4l2-subdev.h>
45
46 #if IS_ENABLED(CONFIG_DVB_RTL2832_SDR)
47 struct dvb_frontend *rtl2832_sdr_attach(struct dvb_frontend *fe,
48         struct i2c_adapter *i2c, const struct rtl2832_config *cfg,
49         struct v4l2_subdev *sd);
50 #else
51 static inline struct dvb_frontend *rtl2832_sdr_attach(struct dvb_frontend *fe,
52         struct i2c_adapter *i2c, const struct rtl2832_config *cfg,
53         struct v4l2_subdev *sd)
54 {
55         return NULL;
56 }
57 #endif
58
59 #ifdef CONFIG_MEDIA_ATTACH
60 #define dvb_attach_sdr(FUNCTION, ARGS...) ({ \
61         void *__r = NULL; \
62         typeof(&FUNCTION) __a = symbol_request(FUNCTION); \
63         if (__a) { \
64                 __r = (void *) __a(ARGS); \
65                 if (__r == NULL) \
66                         symbol_put(FUNCTION); \
67         } \
68         __r; \
69 })
70
71 #else
72 #define dvb_attach_sdr(FUNCTION, ARGS...) ({ \
73         FUNCTION(ARGS); \
74 })
75
76 #endif
77
78 static int rtl28xxu_disable_rc;
79 module_param_named(disable_rc, rtl28xxu_disable_rc, int, 0644);
80 MODULE_PARM_DESC(disable_rc, "disable RTL2832U remote controller");
81 DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
82
83 static int rtl28xxu_ctrl_msg(struct dvb_usb_device *d, struct rtl28xxu_req *req)
84 {
85         int ret;
86         unsigned int pipe;
87         u8 requesttype;
88         u8 *buf;
89
90         buf = kmalloc(req->size, GFP_KERNEL);
91         if (!buf) {
92                 ret = -ENOMEM;
93                 goto err;
94         }
95
96         if (req->index & CMD_WR_FLAG) {
97                 /* write */
98                 memcpy(buf, req->data, req->size);
99                 requesttype = (USB_TYPE_VENDOR | USB_DIR_OUT);
100                 pipe = usb_sndctrlpipe(d->udev, 0);
101         } else {
102                 /* read */
103                 requesttype = (USB_TYPE_VENDOR | USB_DIR_IN);
104                 pipe = usb_rcvctrlpipe(d->udev, 0);
105         }
106
107         ret = usb_control_msg(d->udev, pipe, 0, requesttype, req->value,
108                         req->index, buf, req->size, 1000);
109
110         dvb_usb_dbg_usb_control_msg(d->udev, 0, requesttype, req->value,
111                         req->index, buf, req->size);
112
113         if (ret > 0)
114                 ret = 0;
115
116         /* read request, copy returned data to return buf */
117         if (!ret && requesttype == (USB_TYPE_VENDOR | USB_DIR_IN))
118                 memcpy(req->data, buf, req->size);
119
120         kfree(buf);
121
122         if (ret)
123                 goto err;
124
125         return ret;
126 err:
127         dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
128         return ret;
129 }
130
131 static int rtl28xx_wr_regs(struct dvb_usb_device *d, u16 reg, u8 *val, int len)
132 {
133         struct rtl28xxu_req req;
134
135         if (reg < 0x3000)
136                 req.index = CMD_USB_WR;
137         else if (reg < 0x4000)
138                 req.index = CMD_SYS_WR;
139         else
140                 req.index = CMD_IR_WR;
141
142         req.value = reg;
143         req.size = len;
144         req.data = val;
145
146         return rtl28xxu_ctrl_msg(d, &req);
147 }
148
149 static int rtl2831_rd_regs(struct dvb_usb_device *d, u16 reg, u8 *val, int len)
150 {
151         struct rtl28xxu_req req;
152
153         if (reg < 0x3000)
154                 req.index = CMD_USB_RD;
155         else if (reg < 0x4000)
156                 req.index = CMD_SYS_RD;
157         else
158                 req.index = CMD_IR_RD;
159
160         req.value = reg;
161         req.size = len;
162         req.data = val;
163
164         return rtl28xxu_ctrl_msg(d, &req);
165 }
166
167 static int rtl28xx_wr_reg(struct dvb_usb_device *d, u16 reg, u8 val)
168 {
169         return rtl28xx_wr_regs(d, reg, &val, 1);
170 }
171
172 static int rtl28xx_rd_reg(struct dvb_usb_device *d, u16 reg, u8 *val)
173 {
174         return rtl2831_rd_regs(d, reg, val, 1);
175 }
176
177 static int rtl28xx_wr_reg_mask(struct dvb_usb_device *d, u16 reg, u8 val,
178                 u8 mask)
179 {
180         int ret;
181         u8 tmp;
182
183         /* no need for read if whole reg is written */
184         if (mask != 0xff) {
185                 ret = rtl28xx_rd_reg(d, reg, &tmp);
186                 if (ret)
187                         return ret;
188
189                 val &= mask;
190                 tmp &= ~mask;
191                 val |= tmp;
192         }
193
194         return rtl28xx_wr_reg(d, reg, val);
195 }
196
197 /* I2C */
198 static int rtl28xxu_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg msg[],
199         int num)
200 {
201         int ret;
202         struct dvb_usb_device *d = i2c_get_adapdata(adap);
203         struct rtl28xxu_priv *priv = d->priv;
204         struct rtl28xxu_req req;
205
206         /*
207          * It is not known which are real I2C bus xfer limits, but testing
208          * with RTL2831U + MT2060 gives max RD 24 and max WR 22 bytes.
209          * TODO: find out RTL2832U lens
210          */
211
212         /*
213          * I2C adapter logic looks rather complicated due to fact it handles
214          * three different access methods. Those methods are;
215          * 1) integrated demod access
216          * 2) old I2C access
217          * 3) new I2C access
218          *
219          * Used method is selected in order 1, 2, 3. Method 3 can handle all
220          * requests but there is two reasons why not use it always;
221          * 1) It is most expensive, usually two USB messages are needed
222          * 2) At least RTL2831U does not support it
223          *
224          * Method 3 is needed in case of I2C write+read (typical register read)
225          * where write is more than one byte.
226          */
227
228         if (mutex_lock_interruptible(&d->i2c_mutex) < 0)
229                 return -EAGAIN;
230
231         if (num == 2 && !(msg[0].flags & I2C_M_RD) &&
232                 (msg[1].flags & I2C_M_RD)) {
233                 if (msg[0].len > 24 || msg[1].len > 24) {
234                         /* TODO: check msg[0].len max */
235                         ret = -EOPNOTSUPP;
236                         goto err_mutex_unlock;
237                 } else if (msg[0].addr == 0x10) {
238                         /* method 1 - integrated demod */
239                         req.value = (msg[0].buf[0] << 8) | (msg[0].addr << 1);
240                         req.index = CMD_DEMOD_RD | priv->page;
241                         req.size = msg[1].len;
242                         req.data = &msg[1].buf[0];
243                         ret = rtl28xxu_ctrl_msg(d, &req);
244                 } else if (msg[0].len < 2) {
245                         /* method 2 - old I2C */
246                         req.value = (msg[0].buf[0] << 8) | (msg[0].addr << 1);
247                         req.index = CMD_I2C_RD;
248                         req.size = msg[1].len;
249                         req.data = &msg[1].buf[0];
250                         ret = rtl28xxu_ctrl_msg(d, &req);
251                 } else {
252                         /* method 3 - new I2C */
253                         req.value = (msg[0].addr << 1);
254                         req.index = CMD_I2C_DA_WR;
255                         req.size = msg[0].len;
256                         req.data = msg[0].buf;
257                         ret = rtl28xxu_ctrl_msg(d, &req);
258                         if (ret)
259                                 goto err_mutex_unlock;
260
261                         req.value = (msg[0].addr << 1);
262                         req.index = CMD_I2C_DA_RD;
263                         req.size = msg[1].len;
264                         req.data = msg[1].buf;
265                         ret = rtl28xxu_ctrl_msg(d, &req);
266                 }
267         } else if (num == 1 && !(msg[0].flags & I2C_M_RD)) {
268                 if (msg[0].len > 22) {
269                         /* TODO: check msg[0].len max */
270                         ret = -EOPNOTSUPP;
271                         goto err_mutex_unlock;
272                 } else if (msg[0].addr == 0x10) {
273                         /* method 1 - integrated demod */
274                         if (msg[0].buf[0] == 0x00) {
275                                 /* save demod page for later demod access */
276                                 priv->page = msg[0].buf[1];
277                                 ret = 0;
278                         } else {
279                                 req.value = (msg[0].buf[0] << 8) |
280                                         (msg[0].addr << 1);
281                                 req.index = CMD_DEMOD_WR | priv->page;
282                                 req.size = msg[0].len-1;
283                                 req.data = &msg[0].buf[1];
284                                 ret = rtl28xxu_ctrl_msg(d, &req);
285                         }
286                 } else if (msg[0].len < 23) {
287                         /* method 2 - old I2C */
288                         req.value = (msg[0].buf[0] << 8) | (msg[0].addr << 1);
289                         req.index = CMD_I2C_WR;
290                         req.size = msg[0].len-1;
291                         req.data = &msg[0].buf[1];
292                         ret = rtl28xxu_ctrl_msg(d, &req);
293                 } else {
294                         /* method 3 - new I2C */
295                         req.value = (msg[0].addr << 1);
296                         req.index = CMD_I2C_DA_WR;
297                         req.size = msg[0].len;
298                         req.data = msg[0].buf;
299                         ret = rtl28xxu_ctrl_msg(d, &req);
300                 }
301         } else {
302                 ret = -EINVAL;
303         }
304
305 err_mutex_unlock:
306         mutex_unlock(&d->i2c_mutex);
307
308         return ret ? ret : num;
309 }
310
311 static u32 rtl28xxu_i2c_func(struct i2c_adapter *adapter)
312 {
313         return I2C_FUNC_I2C;
314 }
315
316 static struct i2c_algorithm rtl28xxu_i2c_algo = {
317         .master_xfer   = rtl28xxu_i2c_xfer,
318         .functionality = rtl28xxu_i2c_func,
319 };
320
321 static int rtl2831u_read_config(struct dvb_usb_device *d)
322 {
323         struct rtl28xxu_priv *priv = d_to_priv(d);
324         int ret;
325         u8 buf[1];
326         /* open RTL2831U/RTL2830 I2C gate */
327         struct rtl28xxu_req req_gate_open = {0x0120, 0x0011, 0x0001, "\x08"};
328         /* tuner probes */
329         struct rtl28xxu_req req_mt2060 = {0x00c0, CMD_I2C_RD, 1, buf};
330         struct rtl28xxu_req req_qt1010 = {0x0fc4, CMD_I2C_RD, 1, buf};
331
332         dev_dbg(&d->udev->dev, "%s:\n", __func__);
333
334         /*
335          * RTL2831U GPIOs
336          * =========================================================
337          * GPIO0 | tuner#0 | 0 off | 1 on  | MXL5005S (?)
338          * GPIO2 | LED     | 0 off | 1 on  |
339          * GPIO4 | tuner#1 | 0 on  | 1 off | MT2060
340          */
341
342         /* GPIO direction */
343         ret = rtl28xx_wr_reg(d, SYS_GPIO_DIR, 0x0a);
344         if (ret)
345                 goto err;
346
347         /* enable as output GPIO0, GPIO2, GPIO4 */
348         ret = rtl28xx_wr_reg(d, SYS_GPIO_OUT_EN, 0x15);
349         if (ret)
350                 goto err;
351
352         /*
353          * Probe used tuner. We need to know used tuner before demod attach
354          * since there is some demod params needed to set according to tuner.
355          */
356
357         /* demod needs some time to wake up */
358         msleep(20);
359
360         priv->tuner_name = "NONE";
361
362         /* open demod I2C gate */
363         ret = rtl28xxu_ctrl_msg(d, &req_gate_open);
364         if (ret)
365                 goto err;
366
367         /* check QT1010 ID(?) register; reg=0f val=2c */
368         ret = rtl28xxu_ctrl_msg(d, &req_qt1010);
369         if (ret == 0 && buf[0] == 0x2c) {
370                 priv->tuner = TUNER_RTL2830_QT1010;
371                 priv->tuner_name = "QT1010";
372                 goto found;
373         }
374
375         /* open demod I2C gate */
376         ret = rtl28xxu_ctrl_msg(d, &req_gate_open);
377         if (ret)
378                 goto err;
379
380         /* check MT2060 ID register; reg=00 val=63 */
381         ret = rtl28xxu_ctrl_msg(d, &req_mt2060);
382         if (ret == 0 && buf[0] == 0x63) {
383                 priv->tuner = TUNER_RTL2830_MT2060;
384                 priv->tuner_name = "MT2060";
385                 goto found;
386         }
387
388         /* assume MXL5005S */
389         priv->tuner = TUNER_RTL2830_MXL5005S;
390         priv->tuner_name = "MXL5005S";
391         goto found;
392
393 found:
394         dev_dbg(&d->udev->dev, "%s: tuner=%s\n", __func__, priv->tuner_name);
395
396         return 0;
397 err:
398         dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
399         return ret;
400 }
401
402 static int rtl2832u_read_config(struct dvb_usb_device *d)
403 {
404         struct rtl28xxu_priv *priv = d_to_priv(d);
405         int ret;
406         u8 buf[2];
407         /* open RTL2832U/RTL2832 I2C gate */
408         struct rtl28xxu_req req_gate_open = {0x0120, 0x0011, 0x0001, "\x18"};
409         /* close RTL2832U/RTL2832 I2C gate */
410         struct rtl28xxu_req req_gate_close = {0x0120, 0x0011, 0x0001, "\x10"};
411         /* tuner probes */
412         struct rtl28xxu_req req_fc0012 = {0x00c6, CMD_I2C_RD, 1, buf};
413         struct rtl28xxu_req req_fc0013 = {0x00c6, CMD_I2C_RD, 1, buf};
414         struct rtl28xxu_req req_mt2266 = {0x00c0, CMD_I2C_RD, 1, buf};
415         struct rtl28xxu_req req_fc2580 = {0x01ac, CMD_I2C_RD, 1, buf};
416         struct rtl28xxu_req req_mt2063 = {0x00c0, CMD_I2C_RD, 1, buf};
417         struct rtl28xxu_req req_max3543 = {0x00c0, CMD_I2C_RD, 1, buf};
418         struct rtl28xxu_req req_tua9001 = {0x7ec0, CMD_I2C_RD, 2, buf};
419         struct rtl28xxu_req req_mxl5007t = {0xd9c0, CMD_I2C_RD, 1, buf};
420         struct rtl28xxu_req req_e4000 = {0x02c8, CMD_I2C_RD, 1, buf};
421         struct rtl28xxu_req req_tda18272 = {0x00c0, CMD_I2C_RD, 2, buf};
422         struct rtl28xxu_req req_r820t = {0x0034, CMD_I2C_RD, 1, buf};
423         struct rtl28xxu_req req_r828d = {0x0074, CMD_I2C_RD, 1, buf};
424         struct rtl28xxu_req req_mn88472 = {0xff38, CMD_I2C_RD, 1, buf};
425
426         dev_dbg(&d->udev->dev, "%s:\n", __func__);
427
428         /* enable GPIO3 and GPIO6 as output */
429         ret = rtl28xx_wr_reg_mask(d, SYS_GPIO_DIR, 0x00, 0x40);
430         if (ret)
431                 goto err;
432
433         ret = rtl28xx_wr_reg_mask(d, SYS_GPIO_OUT_EN, 0x48, 0x48);
434         if (ret)
435                 goto err;
436
437         /*
438          * Probe used tuner. We need to know used tuner before demod attach
439          * since there is some demod params needed to set according to tuner.
440          */
441
442         /* open demod I2C gate */
443         ret = rtl28xxu_ctrl_msg(d, &req_gate_open);
444         if (ret)
445                 goto err;
446
447         priv->tuner_name = "NONE";
448
449         /* check FC0012 ID register; reg=00 val=a1 */
450         ret = rtl28xxu_ctrl_msg(d, &req_fc0012);
451         if (ret == 0 && buf[0] == 0xa1) {
452                 priv->tuner = TUNER_RTL2832_FC0012;
453                 priv->tuner_name = "FC0012";
454                 goto tuner_found;
455         }
456
457         /* check FC0013 ID register; reg=00 val=a3 */
458         ret = rtl28xxu_ctrl_msg(d, &req_fc0013);
459         if (ret == 0 && buf[0] == 0xa3) {
460                 priv->tuner = TUNER_RTL2832_FC0013;
461                 priv->tuner_name = "FC0013";
462                 goto tuner_found;
463         }
464
465         /* check MT2266 ID register; reg=00 val=85 */
466         ret = rtl28xxu_ctrl_msg(d, &req_mt2266);
467         if (ret == 0 && buf[0] == 0x85) {
468                 priv->tuner = TUNER_RTL2832_MT2266;
469                 priv->tuner_name = "MT2266";
470                 goto tuner_found;
471         }
472
473         /* check FC2580 ID register; reg=01 val=56 */
474         ret = rtl28xxu_ctrl_msg(d, &req_fc2580);
475         if (ret == 0 && buf[0] == 0x56) {
476                 priv->tuner = TUNER_RTL2832_FC2580;
477                 priv->tuner_name = "FC2580";
478                 goto tuner_found;
479         }
480
481         /* check MT2063 ID register; reg=00 val=9e || 9c */
482         ret = rtl28xxu_ctrl_msg(d, &req_mt2063);
483         if (ret == 0 && (buf[0] == 0x9e || buf[0] == 0x9c)) {
484                 priv->tuner = TUNER_RTL2832_MT2063;
485                 priv->tuner_name = "MT2063";
486                 goto tuner_found;
487         }
488
489         /* check MAX3543 ID register; reg=00 val=38 */
490         ret = rtl28xxu_ctrl_msg(d, &req_max3543);
491         if (ret == 0 && buf[0] == 0x38) {
492                 priv->tuner = TUNER_RTL2832_MAX3543;
493                 priv->tuner_name = "MAX3543";
494                 goto tuner_found;
495         }
496
497         /* check TUA9001 ID register; reg=7e val=2328 */
498         ret = rtl28xxu_ctrl_msg(d, &req_tua9001);
499         if (ret == 0 && buf[0] == 0x23 && buf[1] == 0x28) {
500                 priv->tuner = TUNER_RTL2832_TUA9001;
501                 priv->tuner_name = "TUA9001";
502                 goto tuner_found;
503         }
504
505         /* check MXL5007R ID register; reg=d9 val=14 */
506         ret = rtl28xxu_ctrl_msg(d, &req_mxl5007t);
507         if (ret == 0 && buf[0] == 0x14) {
508                 priv->tuner = TUNER_RTL2832_MXL5007T;
509                 priv->tuner_name = "MXL5007T";
510                 goto tuner_found;
511         }
512
513         /* check E4000 ID register; reg=02 val=40 */
514         ret = rtl28xxu_ctrl_msg(d, &req_e4000);
515         if (ret == 0 && buf[0] == 0x40) {
516                 priv->tuner = TUNER_RTL2832_E4000;
517                 priv->tuner_name = "E4000";
518                 goto tuner_found;
519         }
520
521         /* check TDA18272 ID register; reg=00 val=c760  */
522         ret = rtl28xxu_ctrl_msg(d, &req_tda18272);
523         if (ret == 0 && (buf[0] == 0xc7 || buf[1] == 0x60)) {
524                 priv->tuner = TUNER_RTL2832_TDA18272;
525                 priv->tuner_name = "TDA18272";
526                 goto tuner_found;
527         }
528
529         /* check R820T ID register; reg=00 val=69 */
530         ret = rtl28xxu_ctrl_msg(d, &req_r820t);
531         if (ret == 0 && buf[0] == 0x69) {
532                 priv->tuner = TUNER_RTL2832_R820T;
533                 priv->tuner_name = "R820T";
534                 goto tuner_found;
535         }
536
537         /* check R828D ID register; reg=00 val=69 */
538         ret = rtl28xxu_ctrl_msg(d, &req_r828d);
539         if (ret == 0 && buf[0] == 0x69) {
540                 priv->tuner = TUNER_RTL2832_R828D;
541                 priv->tuner_name = "R828D";
542                 goto tuner_found;
543         }
544
545 tuner_found:
546         dev_dbg(&d->udev->dev, "%s: tuner=%s\n", __func__, priv->tuner_name);
547
548         /* probe slave demod */
549         if (priv->tuner == TUNER_RTL2832_R828D) {
550                 /* power on MN88472 demod on GPIO0 */
551                 ret = rtl28xx_wr_reg_mask(d, SYS_GPIO_OUT_VAL, 0x01, 0x01);
552                 if (ret)
553                         goto err;
554
555                 ret = rtl28xx_wr_reg_mask(d, SYS_GPIO_DIR, 0x00, 0x01);
556                 if (ret)
557                         goto err;
558
559                 ret = rtl28xx_wr_reg_mask(d, SYS_GPIO_OUT_EN, 0x01, 0x01);
560                 if (ret)
561                         goto err;
562
563                 /* check MN88472 answers */
564                 ret = rtl28xxu_ctrl_msg(d, &req_mn88472);
565                 if (ret == 0 && buf[0] == 0x02) {
566                         dev_dbg(&d->udev->dev, "%s: MN88472 found\n", __func__);
567                         priv->slave_demod = SLAVE_DEMOD_MN88472;
568                         goto demod_found;
569                 }
570         }
571
572 demod_found:
573         /* close demod I2C gate */
574         ret = rtl28xxu_ctrl_msg(d, &req_gate_close);
575         if (ret < 0)
576                 goto err;
577
578         return 0;
579 err:
580         dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
581         return ret;
582 }
583
584 static const struct rtl2830_config rtl28xxu_rtl2830_mt2060_config = {
585         .i2c_addr = 0x10, /* 0x20 */
586         .xtal = 28800000,
587         .ts_mode = 0,
588         .spec_inv = 1,
589         .vtop = 0x20,
590         .krf = 0x04,
591         .agc_targ_val = 0x2d,
592
593 };
594
595 static const struct rtl2830_config rtl28xxu_rtl2830_qt1010_config = {
596         .i2c_addr = 0x10, /* 0x20 */
597         .xtal = 28800000,
598         .ts_mode = 0,
599         .spec_inv = 1,
600         .vtop = 0x20,
601         .krf = 0x04,
602         .agc_targ_val = 0x2d,
603 };
604
605 static const struct rtl2830_config rtl28xxu_rtl2830_mxl5005s_config = {
606         .i2c_addr = 0x10, /* 0x20 */
607         .xtal = 28800000,
608         .ts_mode = 0,
609         .spec_inv = 0,
610         .vtop = 0x3f,
611         .krf = 0x04,
612         .agc_targ_val = 0x3e,
613 };
614
615 static int rtl2831u_frontend_attach(struct dvb_usb_adapter *adap)
616 {
617         struct dvb_usb_device *d = adap_to_d(adap);
618         struct rtl28xxu_priv *priv = d_to_priv(d);
619         const struct rtl2830_config *rtl2830_config;
620         int ret;
621
622         dev_dbg(&d->udev->dev, "%s:\n", __func__);
623
624         switch (priv->tuner) {
625         case TUNER_RTL2830_QT1010:
626                 rtl2830_config = &rtl28xxu_rtl2830_qt1010_config;
627                 break;
628         case TUNER_RTL2830_MT2060:
629                 rtl2830_config = &rtl28xxu_rtl2830_mt2060_config;
630                 break;
631         case TUNER_RTL2830_MXL5005S:
632                 rtl2830_config = &rtl28xxu_rtl2830_mxl5005s_config;
633                 break;
634         default:
635                 dev_err(&d->udev->dev, "%s: unknown tuner=%s\n",
636                                 KBUILD_MODNAME, priv->tuner_name);
637                 ret = -ENODEV;
638                 goto err;
639         }
640
641         /* attach demodulator */
642         adap->fe[0] = dvb_attach(rtl2830_attach, rtl2830_config, &d->i2c_adap);
643         if (!adap->fe[0]) {
644                 ret = -ENODEV;
645                 goto err;
646         }
647
648         return 0;
649 err:
650         dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
651         return ret;
652 }
653
654 static const struct rtl2832_config rtl28xxu_rtl2832_fc0012_config = {
655         .i2c_addr = 0x10, /* 0x20 */
656         .xtal = 28800000,
657         .tuner = TUNER_RTL2832_FC0012
658 };
659
660 static const struct rtl2832_config rtl28xxu_rtl2832_fc0013_config = {
661         .i2c_addr = 0x10, /* 0x20 */
662         .xtal = 28800000,
663         .tuner = TUNER_RTL2832_FC0013
664 };
665
666 static const struct rtl2832_config rtl28xxu_rtl2832_tua9001_config = {
667         .i2c_addr = 0x10, /* 0x20 */
668         .xtal = 28800000,
669         .tuner = TUNER_RTL2832_TUA9001,
670 };
671
672 static const struct rtl2832_config rtl28xxu_rtl2832_e4000_config = {
673         .i2c_addr = 0x10, /* 0x20 */
674         .xtal = 28800000,
675         .tuner = TUNER_RTL2832_E4000,
676 };
677
678 static const struct rtl2832_config rtl28xxu_rtl2832_r820t_config = {
679         .i2c_addr = 0x10,
680         .xtal = 28800000,
681         .tuner = TUNER_RTL2832_R820T,
682 };
683
684 static int rtl2832u_fc0012_tuner_callback(struct dvb_usb_device *d,
685                 int cmd, int arg)
686 {
687         int ret;
688         u8 val;
689
690         dev_dbg(&d->udev->dev, "%s: cmd=%d arg=%d\n", __func__, cmd, arg);
691
692         switch (cmd) {
693         case FC_FE_CALLBACK_VHF_ENABLE:
694                 /* set output values */
695                 ret = rtl28xx_rd_reg(d, SYS_GPIO_OUT_VAL, &val);
696                 if (ret)
697                         goto err;
698
699                 if (arg)
700                         val &= 0xbf; /* set GPIO6 low */
701                 else
702                         val |= 0x40; /* set GPIO6 high */
703
704
705                 ret = rtl28xx_wr_reg(d, SYS_GPIO_OUT_VAL, val);
706                 if (ret)
707                         goto err;
708                 break;
709         default:
710                 ret = -EINVAL;
711                 goto err;
712         }
713         return 0;
714 err:
715         dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
716         return ret;
717 }
718
719 static int rtl2832u_tua9001_tuner_callback(struct dvb_usb_device *d,
720                 int cmd, int arg)
721 {
722         int ret;
723         u8 val;
724
725         dev_dbg(&d->udev->dev, "%s: cmd=%d arg=%d\n", __func__, cmd, arg);
726
727         /*
728          * CEN     always enabled by hardware wiring
729          * RESETN  GPIO4
730          * RXEN    GPIO1
731          */
732
733         switch (cmd) {
734         case TUA9001_CMD_RESETN:
735                 if (arg)
736                         val = (1 << 4);
737                 else
738                         val = (0 << 4);
739
740                 ret = rtl28xx_wr_reg_mask(d, SYS_GPIO_OUT_VAL, val, 0x10);
741                 if (ret)
742                         goto err;
743                 break;
744         case TUA9001_CMD_RXEN:
745                 if (arg)
746                         val = (1 << 1);
747                 else
748                         val = (0 << 1);
749
750                 ret = rtl28xx_wr_reg_mask(d, SYS_GPIO_OUT_VAL, val, 0x02);
751                 if (ret)
752                         goto err;
753                 break;
754         }
755
756         return 0;
757 err:
758         dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
759         return ret;
760 }
761
762 static int rtl2832u_tuner_callback(struct dvb_usb_device *d, int cmd, int arg)
763 {
764         struct rtl28xxu_priv *priv = d->priv;
765
766         switch (priv->tuner) {
767         case TUNER_RTL2832_FC0012:
768                 return rtl2832u_fc0012_tuner_callback(d, cmd, arg);
769         case TUNER_RTL2832_TUA9001:
770                 return rtl2832u_tua9001_tuner_callback(d, cmd, arg);
771         default:
772                 break;
773         }
774
775         return 0;
776 }
777
778 static int rtl2832u_frontend_callback(void *adapter_priv, int component,
779                 int cmd, int arg)
780 {
781         struct i2c_adapter *adap = adapter_priv;
782         struct dvb_usb_device *d = i2c_get_adapdata(adap);
783
784         dev_dbg(&d->udev->dev, "%s: component=%d cmd=%d arg=%d\n",
785                         __func__, component, cmd, arg);
786
787         switch (component) {
788         case DVB_FRONTEND_COMPONENT_TUNER:
789                 return rtl2832u_tuner_callback(d, cmd, arg);
790         default:
791                 break;
792         }
793
794         return 0;
795 }
796
797 static int rtl2832u_frontend_attach(struct dvb_usb_adapter *adap)
798 {
799         int ret;
800         struct dvb_usb_device *d = adap_to_d(adap);
801         struct rtl28xxu_priv *priv = d_to_priv(d);
802         const struct rtl2832_config *rtl2832_config;
803
804         dev_dbg(&d->udev->dev, "%s:\n", __func__);
805
806         switch (priv->tuner) {
807         case TUNER_RTL2832_FC0012:
808                 rtl2832_config = &rtl28xxu_rtl2832_fc0012_config;
809                 break;
810         case TUNER_RTL2832_FC0013:
811                 rtl2832_config = &rtl28xxu_rtl2832_fc0013_config;
812                 break;
813         case TUNER_RTL2832_FC2580:
814                 /* FIXME: do not abuse fc0012 settings */
815                 rtl2832_config = &rtl28xxu_rtl2832_fc0012_config;
816                 break;
817         case TUNER_RTL2832_TUA9001:
818                 rtl2832_config = &rtl28xxu_rtl2832_tua9001_config;
819                 break;
820         case TUNER_RTL2832_E4000:
821                 rtl2832_config = &rtl28xxu_rtl2832_e4000_config;
822                 break;
823         case TUNER_RTL2832_R820T:
824         case TUNER_RTL2832_R828D:
825                 rtl2832_config = &rtl28xxu_rtl2832_r820t_config;
826                 break;
827         default:
828                 dev_err(&d->udev->dev, "%s: unknown tuner=%s\n",
829                                 KBUILD_MODNAME, priv->tuner_name);
830                 ret = -ENODEV;
831                 goto err;
832         }
833
834         /* attach demodulator */
835         adap->fe[0] = dvb_attach(rtl2832_attach, rtl2832_config, &d->i2c_adap);
836         if (!adap->fe[0]) {
837                 ret = -ENODEV;
838                 goto err;
839         }
840
841         /* RTL2832 I2C repeater */
842         priv->demod_i2c_adapter = rtl2832_get_i2c_adapter(adap->fe[0]);
843
844         /* set fe callback */
845         adap->fe[0]->callback = rtl2832u_frontend_callback;
846
847         if (priv->slave_demod) {
848                 struct i2c_board_info info = {};
849                 struct i2c_client *client;
850
851                 /*
852                  * We continue on reduced mode, without DVB-T2/C, using master
853                  * demod, when slave demod fails.
854                  */
855                 ret = 0;
856
857                 /* attach slave demodulator */
858                 if (priv->slave_demod == SLAVE_DEMOD_MN88472) {
859                         struct mn88472_config mn88472_config = {};
860
861                         mn88472_config.fe = &adap->fe[1];
862                         mn88472_config.i2c_wr_max = 22,
863                         strlcpy(info.type, "mn88472", I2C_NAME_SIZE);
864                         info.addr = 0x18;
865                         info.platform_data = &mn88472_config;
866                         request_module(info.type);
867                         client = i2c_new_device(priv->demod_i2c_adapter, &info);
868                         if (client == NULL || client->dev.driver == NULL) {
869                                 priv->slave_demod = SLAVE_DEMOD_NONE;
870                                 goto err_slave_demod_failed;
871                         }
872
873                         if (!try_module_get(client->dev.driver->owner)) {
874                                 i2c_unregister_device(client);
875                                 priv->slave_demod = SLAVE_DEMOD_NONE;
876                                 goto err_slave_demod_failed;
877                         }
878
879                         priv->i2c_client_slave_demod = client;
880                 }
881         }
882
883         return 0;
884 err_slave_demod_failed:
885 err:
886         dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
887         return ret;
888 }
889
890 static struct qt1010_config rtl28xxu_qt1010_config = {
891         .i2c_address = 0x62, /* 0xc4 */
892 };
893
894 static struct mt2060_config rtl28xxu_mt2060_config = {
895         .i2c_address = 0x60, /* 0xc0 */
896         .clock_out = 0,
897 };
898
899 static struct mxl5005s_config rtl28xxu_mxl5005s_config = {
900         .i2c_address     = 0x63, /* 0xc6 */
901         .if_freq         = IF_FREQ_4570000HZ,
902         .xtal_freq       = CRYSTAL_FREQ_16000000HZ,
903         .agc_mode        = MXL_SINGLE_AGC,
904         .tracking_filter = MXL_TF_C_H,
905         .rssi_enable     = MXL_RSSI_ENABLE,
906         .cap_select      = MXL_CAP_SEL_ENABLE,
907         .div_out         = MXL_DIV_OUT_4,
908         .clock_out       = MXL_CLOCK_OUT_DISABLE,
909         .output_load     = MXL5005S_IF_OUTPUT_LOAD_200_OHM,
910         .top             = MXL5005S_TOP_25P2,
911         .mod_mode        = MXL_DIGITAL_MODE,
912         .if_mode         = MXL_ZERO_IF,
913         .AgcMasterByte   = 0x00,
914 };
915
916 static int rtl2831u_tuner_attach(struct dvb_usb_adapter *adap)
917 {
918         int ret;
919         struct dvb_usb_device *d = adap_to_d(adap);
920         struct rtl28xxu_priv *priv = d_to_priv(d);
921         struct i2c_adapter *rtl2830_tuner_i2c;
922         struct dvb_frontend *fe;
923
924         dev_dbg(&d->udev->dev, "%s:\n", __func__);
925
926         /* use rtl2830 driver I2C adapter, for more info see rtl2830 driver */
927         rtl2830_tuner_i2c = rtl2830_get_tuner_i2c_adapter(adap->fe[0]);
928
929         switch (priv->tuner) {
930         case TUNER_RTL2830_QT1010:
931                 fe = dvb_attach(qt1010_attach, adap->fe[0],
932                                 rtl2830_tuner_i2c, &rtl28xxu_qt1010_config);
933                 break;
934         case TUNER_RTL2830_MT2060:
935                 fe = dvb_attach(mt2060_attach, adap->fe[0],
936                                 rtl2830_tuner_i2c, &rtl28xxu_mt2060_config,
937                                 1220);
938                 break;
939         case TUNER_RTL2830_MXL5005S:
940                 fe = dvb_attach(mxl5005s_attach, adap->fe[0],
941                                 rtl2830_tuner_i2c, &rtl28xxu_mxl5005s_config);
942                 break;
943         default:
944                 fe = NULL;
945                 dev_err(&d->udev->dev, "%s: unknown tuner=%d\n", KBUILD_MODNAME,
946                                 priv->tuner);
947         }
948
949         if (fe == NULL) {
950                 ret = -ENODEV;
951                 goto err;
952         }
953
954         return 0;
955 err:
956         dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
957         return ret;
958 }
959
960 static const struct fc2580_config rtl2832u_fc2580_config = {
961         .i2c_addr = 0x56,
962         .clock = 16384000,
963 };
964
965 static struct tua9001_config rtl2832u_tua9001_config = {
966         .i2c_addr = 0x60,
967 };
968
969 static const struct fc0012_config rtl2832u_fc0012_config = {
970         .i2c_address = 0x63, /* 0xc6 >> 1 */
971         .xtal_freq = FC_XTAL_28_8_MHZ,
972 };
973
974 static const struct r820t_config rtl2832u_r820t_config = {
975         .i2c_addr = 0x1a,
976         .xtal = 28800000,
977         .max_i2c_msg_len = 2,
978         .rafael_chip = CHIP_R820T,
979 };
980
981 static const struct r820t_config rtl2832u_r828d_config = {
982         .i2c_addr = 0x3a,
983         .xtal = 16000000,
984         .max_i2c_msg_len = 2,
985         .rafael_chip = CHIP_R828D,
986 };
987
988 static int rtl2832u_tuner_attach(struct dvb_usb_adapter *adap)
989 {
990         int ret;
991         struct dvb_usb_device *d = adap_to_d(adap);
992         struct rtl28xxu_priv *priv = d_to_priv(d);
993         struct dvb_frontend *fe = NULL;
994         struct i2c_board_info info;
995         struct i2c_client *client;
996
997         dev_dbg(&d->udev->dev, "%s:\n", __func__);
998
999         memset(&info, 0, sizeof(struct i2c_board_info));
1000
1001         switch (priv->tuner) {
1002         case TUNER_RTL2832_FC0012:
1003                 fe = dvb_attach(fc0012_attach, adap->fe[0],
1004                         &d->i2c_adap, &rtl2832u_fc0012_config);
1005
1006                 /* since fc0012 includs reading the signal strength delegate
1007                  * that to the tuner driver */
1008                 adap->fe[0]->ops.read_signal_strength =
1009                                 adap->fe[0]->ops.tuner_ops.get_rf_strength;
1010
1011                 /* attach SDR */
1012                 dvb_attach_sdr(rtl2832_sdr_attach, adap->fe[0], &d->i2c_adap,
1013                                 &rtl28xxu_rtl2832_fc0012_config, NULL);
1014                 break;
1015         case TUNER_RTL2832_FC0013:
1016                 fe = dvb_attach(fc0013_attach, adap->fe[0],
1017                         &d->i2c_adap, 0xc6>>1, 0, FC_XTAL_28_8_MHZ);
1018
1019                 /* fc0013 also supports signal strength reading */
1020                 adap->fe[0]->ops.read_signal_strength =
1021                                 adap->fe[0]->ops.tuner_ops.get_rf_strength;
1022
1023                 /* attach SDR */
1024                 dvb_attach_sdr(rtl2832_sdr_attach, adap->fe[0], &d->i2c_adap,
1025                                 &rtl28xxu_rtl2832_fc0013_config, NULL);
1026                 break;
1027         case TUNER_RTL2832_E4000: {
1028                         struct v4l2_subdev *sd;
1029                         struct i2c_adapter *i2c_adap_internal =
1030                                         rtl2832_get_private_i2c_adapter(adap->fe[0]);
1031                         struct e4000_config e4000_config = {
1032                                 .fe = adap->fe[0],
1033                                 .clock = 28800000,
1034                         };
1035
1036                         strlcpy(info.type, "e4000", I2C_NAME_SIZE);
1037                         info.addr = 0x64;
1038                         info.platform_data = &e4000_config;
1039
1040                         request_module(info.type);
1041                         client = i2c_new_device(priv->demod_i2c_adapter, &info);
1042                         if (client == NULL || client->dev.driver == NULL)
1043                                 break;
1044
1045                         if (!try_module_get(client->dev.driver->owner)) {
1046                                 i2c_unregister_device(client);
1047                                 break;
1048                         }
1049
1050                         priv->client = client;
1051                         sd = i2c_get_clientdata(client);
1052                         i2c_set_adapdata(i2c_adap_internal, d);
1053
1054                         /* attach SDR */
1055                         dvb_attach_sdr(rtl2832_sdr_attach, adap->fe[0],
1056                                         i2c_adap_internal,
1057                                         &rtl28xxu_rtl2832_e4000_config, sd);
1058                 }
1059                 break;
1060         case TUNER_RTL2832_FC2580:
1061                 fe = dvb_attach(fc2580_attach, adap->fe[0], &d->i2c_adap,
1062                                 &rtl2832u_fc2580_config);
1063                 break;
1064         case TUNER_RTL2832_TUA9001:
1065                 /* enable GPIO1 and GPIO4 as output */
1066                 ret = rtl28xx_wr_reg_mask(d, SYS_GPIO_DIR, 0x00, 0x12);
1067                 if (ret)
1068                         goto err;
1069
1070                 ret = rtl28xx_wr_reg_mask(d, SYS_GPIO_OUT_EN, 0x12, 0x12);
1071                 if (ret)
1072                         goto err;
1073
1074                 fe = dvb_attach(tua9001_attach, adap->fe[0], &d->i2c_adap,
1075                                 &rtl2832u_tua9001_config);
1076                 break;
1077         case TUNER_RTL2832_R820T:
1078                 fe = dvb_attach(r820t_attach, adap->fe[0], &d->i2c_adap,
1079                                 &rtl2832u_r820t_config);
1080
1081                 /* Use tuner to get the signal strength */
1082                 adap->fe[0]->ops.read_signal_strength =
1083                                 adap->fe[0]->ops.tuner_ops.get_rf_strength;
1084
1085                 /* attach SDR */
1086                 dvb_attach_sdr(rtl2832_sdr_attach, adap->fe[0], &d->i2c_adap,
1087                                 &rtl28xxu_rtl2832_r820t_config, NULL);
1088                 break;
1089         case TUNER_RTL2832_R828D:
1090                 fe = dvb_attach(r820t_attach, adap->fe[0],
1091                                 priv->demod_i2c_adapter,
1092                                 &rtl2832u_r828d_config);
1093                 adap->fe[0]->ops.read_signal_strength =
1094                                 adap->fe[0]->ops.tuner_ops.get_rf_strength;
1095
1096                 if (adap->fe[1]) {
1097                         fe = dvb_attach(r820t_attach, adap->fe[1],
1098                                         priv->demod_i2c_adapter,
1099                                         &rtl2832u_r828d_config);
1100                         adap->fe[1]->ops.read_signal_strength =
1101                                         adap->fe[1]->ops.tuner_ops.get_rf_strength;
1102                 }
1103                 break;
1104         default:
1105                 dev_err(&d->udev->dev, "%s: unknown tuner=%d\n", KBUILD_MODNAME,
1106                                 priv->tuner);
1107         }
1108
1109         if (fe == NULL && priv->client == NULL) {
1110                 ret = -ENODEV;
1111                 goto err;
1112         }
1113
1114         return 0;
1115 err:
1116         dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
1117         return ret;
1118 }
1119
1120 static int rtl28xxu_init(struct dvb_usb_device *d)
1121 {
1122         int ret;
1123         u8 val;
1124
1125         dev_dbg(&d->udev->dev, "%s:\n", __func__);
1126
1127         /* init USB endpoints */
1128         ret = rtl28xx_rd_reg(d, USB_SYSCTL_0, &val);
1129         if (ret)
1130                 goto err;
1131
1132         /* enable DMA and Full Packet Mode*/
1133         val |= 0x09;
1134         ret = rtl28xx_wr_reg(d, USB_SYSCTL_0, val);
1135         if (ret)
1136                 goto err;
1137
1138         /* set EPA maximum packet size to 0x0200 */
1139         ret = rtl28xx_wr_regs(d, USB_EPA_MAXPKT, "\x00\x02\x00\x00", 4);
1140         if (ret)
1141                 goto err;
1142
1143         /* change EPA FIFO length */
1144         ret = rtl28xx_wr_regs(d, USB_EPA_FIFO_CFG, "\x14\x00\x00\x00", 4);
1145         if (ret)
1146                 goto err;
1147
1148         return ret;
1149 err:
1150         dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
1151         return ret;
1152 }
1153
1154 static void rtl28xxu_exit(struct dvb_usb_device *d)
1155 {
1156         struct rtl28xxu_priv *priv = d->priv;
1157         struct i2c_client *client;
1158
1159         dev_dbg(&d->udev->dev, "%s:\n", __func__);
1160
1161         /* remove I2C tuner */
1162         client = priv->client;
1163         if (client) {
1164                 module_put(client->dev.driver->owner);
1165                 i2c_unregister_device(client);
1166         }
1167
1168         /* remove I2C slave demod */
1169         client = priv->i2c_client_slave_demod;
1170         if (client) {
1171                 module_put(client->dev.driver->owner);
1172                 i2c_unregister_device(client);
1173         }
1174
1175         return;
1176 }
1177
1178 static int rtl2831u_power_ctrl(struct dvb_usb_device *d, int onoff)
1179 {
1180         int ret;
1181         u8 gpio, sys0, epa_ctl[2];
1182
1183         dev_dbg(&d->udev->dev, "%s: onoff=%d\n", __func__, onoff);
1184
1185         /* demod adc */
1186         ret = rtl28xx_rd_reg(d, SYS_SYS0, &sys0);
1187         if (ret)
1188                 goto err;
1189
1190         /* tuner power, read GPIOs */
1191         ret = rtl28xx_rd_reg(d, SYS_GPIO_OUT_VAL, &gpio);
1192         if (ret)
1193                 goto err;
1194
1195         dev_dbg(&d->udev->dev, "%s: RD SYS0=%02x GPIO_OUT_VAL=%02x\n", __func__,
1196                         sys0, gpio);
1197
1198         if (onoff) {
1199                 gpio |= 0x01; /* GPIO0 = 1 */
1200                 gpio &= (~0x10); /* GPIO4 = 0 */
1201                 gpio |= 0x04; /* GPIO2 = 1, LED on */
1202                 sys0 = sys0 & 0x0f;
1203                 sys0 |= 0xe0;
1204                 epa_ctl[0] = 0x00; /* clear stall */
1205                 epa_ctl[1] = 0x00; /* clear reset */
1206         } else {
1207                 gpio &= (~0x01); /* GPIO0 = 0 */
1208                 gpio |= 0x10; /* GPIO4 = 1 */
1209                 gpio &= (~0x04); /* GPIO2 = 1, LED off */
1210                 sys0 = sys0 & (~0xc0);
1211                 epa_ctl[0] = 0x10; /* set stall */
1212                 epa_ctl[1] = 0x02; /* set reset */
1213         }
1214
1215         dev_dbg(&d->udev->dev, "%s: WR SYS0=%02x GPIO_OUT_VAL=%02x\n", __func__,
1216                         sys0, gpio);
1217
1218         /* demod adc */
1219         ret = rtl28xx_wr_reg(d, SYS_SYS0, sys0);
1220         if (ret)
1221                 goto err;
1222
1223         /* tuner power, write GPIOs */
1224         ret = rtl28xx_wr_reg(d, SYS_GPIO_OUT_VAL, gpio);
1225         if (ret)
1226                 goto err;
1227
1228         /* streaming EP: stall & reset */
1229         ret = rtl28xx_wr_regs(d, USB_EPA_CTL, epa_ctl, 2);
1230         if (ret)
1231                 goto err;
1232
1233         if (onoff)
1234                 usb_clear_halt(d->udev, usb_rcvbulkpipe(d->udev, 0x81));
1235
1236         return ret;
1237 err:
1238         dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
1239         return ret;
1240 }
1241
1242 static int rtl2832u_power_ctrl(struct dvb_usb_device *d, int onoff)
1243 {
1244         int ret;
1245
1246         dev_dbg(&d->udev->dev, "%s: onoff=%d\n", __func__, onoff);
1247
1248         if (onoff) {
1249                 /* GPIO3=1, GPIO4=0 */
1250                 ret = rtl28xx_wr_reg_mask(d, SYS_GPIO_OUT_VAL, 0x08, 0x18);
1251                 if (ret)
1252                         goto err;
1253
1254                 /* suspend? */
1255                 ret = rtl28xx_wr_reg_mask(d, SYS_DEMOD_CTL1, 0x00, 0x10);
1256                 if (ret)
1257                         goto err;
1258
1259                 /* enable PLL */
1260                 ret = rtl28xx_wr_reg_mask(d, SYS_DEMOD_CTL, 0x80, 0x80);
1261                 if (ret)
1262                         goto err;
1263
1264                 /* disable reset */
1265                 ret = rtl28xx_wr_reg_mask(d, SYS_DEMOD_CTL, 0x20, 0x20);
1266                 if (ret)
1267                         goto err;
1268
1269                 /* streaming EP: clear stall & reset */
1270                 ret = rtl28xx_wr_regs(d, USB_EPA_CTL, "\x00\x00", 2);
1271                 if (ret)
1272                         goto err;
1273
1274                 ret = usb_clear_halt(d->udev, usb_rcvbulkpipe(d->udev, 0x81));
1275                 if (ret)
1276                         goto err;
1277         } else {
1278                 /* GPIO4=1 */
1279                 ret = rtl28xx_wr_reg_mask(d, SYS_GPIO_OUT_VAL, 0x10, 0x10);
1280                 if (ret)
1281                         goto err;
1282
1283                 /* disable PLL */
1284                 ret = rtl28xx_wr_reg_mask(d, SYS_DEMOD_CTL, 0x00, 0x80);
1285                 if (ret)
1286                         goto err;
1287
1288                 /* streaming EP: set stall & reset */
1289                 ret = rtl28xx_wr_regs(d, USB_EPA_CTL, "\x10\x02", 2);
1290                 if (ret)
1291                         goto err;
1292         }
1293
1294         return ret;
1295 err:
1296         dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
1297         return ret;
1298 }
1299
1300 static int rtl2832u_frontend_ctrl(struct dvb_frontend *fe, int onoff)
1301 {
1302         struct dvb_usb_device *d = fe_to_d(fe);
1303         struct dvb_usb_adapter *adap = fe_to_adap(fe);
1304         int ret;
1305         u8 val;
1306
1307         dev_dbg(&d->udev->dev, "%s: fe=%d onoff=%d\n", __func__, fe->id, onoff);
1308
1309         /* control internal demod ADC */
1310         if (fe->id == 0 && onoff)
1311                 val = 0x48; /* enable ADC */
1312         else
1313                 val = 0x00; /* disable ADC */
1314
1315         ret = rtl28xx_wr_reg_mask(d, SYS_DEMOD_CTL, val, 0x48);
1316         if (ret)
1317                 goto err;
1318
1319         /* bypass slave demod TS through master demod */
1320         if (fe->id == 1 && onoff) {
1321                 ret = rtl2832_enable_external_ts_if(adap->fe[0]);
1322                 if (ret)
1323                         goto err;
1324         }
1325
1326         return 0;
1327 err:
1328         dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
1329         return ret;
1330 }
1331
1332 #if IS_ENABLED(CONFIG_RC_CORE)
1333 static int rtl2831u_rc_query(struct dvb_usb_device *d)
1334 {
1335         int ret, i;
1336         struct rtl28xxu_priv *priv = d->priv;
1337         u8 buf[5];
1338         u32 rc_code;
1339         struct rtl28xxu_reg_val rc_nec_tab[] = {
1340                 { 0x3033, 0x80 },
1341                 { 0x3020, 0x43 },
1342                 { 0x3021, 0x16 },
1343                 { 0x3022, 0x16 },
1344                 { 0x3023, 0x5a },
1345                 { 0x3024, 0x2d },
1346                 { 0x3025, 0x16 },
1347                 { 0x3026, 0x01 },
1348                 { 0x3028, 0xb0 },
1349                 { 0x3029, 0x04 },
1350                 { 0x302c, 0x88 },
1351                 { 0x302e, 0x13 },
1352                 { 0x3030, 0xdf },
1353                 { 0x3031, 0x05 },
1354         };
1355
1356         /* init remote controller */
1357         if (!priv->rc_active) {
1358                 for (i = 0; i < ARRAY_SIZE(rc_nec_tab); i++) {
1359                         ret = rtl28xx_wr_reg(d, rc_nec_tab[i].reg,
1360                                         rc_nec_tab[i].val);
1361                         if (ret)
1362                                 goto err;
1363                 }
1364                 priv->rc_active = true;
1365         }
1366
1367         ret = rtl2831_rd_regs(d, SYS_IRRC_RP, buf, 5);
1368         if (ret)
1369                 goto err;
1370
1371         if (buf[4] & 0x01) {
1372                 if (buf[2] == (u8) ~buf[3]) {
1373                         if (buf[0] == (u8) ~buf[1]) {
1374                                 /* NEC standard (16 bit) */
1375                                 rc_code = RC_SCANCODE_NEC(buf[0], buf[2]);
1376                         } else {
1377                                 /* NEC extended (24 bit) */
1378                                 rc_code = RC_SCANCODE_NECX(buf[0] << 8 | buf[1],
1379                                                            buf[2]);
1380                         }
1381                 } else {
1382                         /* NEC full (32 bit) */
1383                         rc_code = RC_SCANCODE_NEC32(buf[0] << 24 | buf[1] << 16 |
1384                                                     buf[2] << 8  | buf[3]);
1385                 }
1386
1387                 rc_keydown(d->rc_dev, RC_TYPE_NEC, rc_code, 0);
1388
1389                 ret = rtl28xx_wr_reg(d, SYS_IRRC_SR, 1);
1390                 if (ret)
1391                         goto err;
1392
1393                 /* repeated intentionally to avoid extra keypress */
1394                 ret = rtl28xx_wr_reg(d, SYS_IRRC_SR, 1);
1395                 if (ret)
1396                         goto err;
1397         }
1398
1399         return ret;
1400 err:
1401         dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
1402         return ret;
1403 }
1404
1405 static int rtl2831u_get_rc_config(struct dvb_usb_device *d,
1406                 struct dvb_usb_rc *rc)
1407 {
1408         rc->map_name = RC_MAP_EMPTY;
1409         rc->allowed_protos = RC_BIT_NEC;
1410         rc->query = rtl2831u_rc_query;
1411         rc->interval = 400;
1412
1413         return 0;
1414 }
1415
1416 static int rtl2832u_rc_query(struct dvb_usb_device *d)
1417 {
1418         int ret, i, len;
1419         struct rtl28xxu_priv *priv = d->priv;
1420         struct ir_raw_event ev;
1421         u8 buf[128];
1422         static const struct rtl28xxu_reg_val_mask refresh_tab[] = {
1423                 {IR_RX_IF,               0x03, 0xff},
1424                 {IR_RX_BUF_CTRL,         0x80, 0xff},
1425                 {IR_RX_CTRL,             0x80, 0xff},
1426         };
1427
1428         /* init remote controller */
1429         if (!priv->rc_active) {
1430                 static const struct rtl28xxu_reg_val_mask init_tab[] = {
1431                         {SYS_DEMOD_CTL1,         0x00, 0x04},
1432                         {SYS_DEMOD_CTL1,         0x00, 0x08},
1433                         {USB_CTRL,               0x20, 0x20},
1434                         {SYS_GPIO_DIR,           0x00, 0x08},
1435                         {SYS_GPIO_OUT_EN,        0x08, 0x08},
1436                         {SYS_GPIO_OUT_VAL,       0x08, 0x08},
1437                         {IR_MAX_DURATION0,       0xd0, 0xff},
1438                         {IR_MAX_DURATION1,       0x07, 0xff},
1439                         {IR_IDLE_LEN0,           0xc0, 0xff},
1440                         {IR_IDLE_LEN1,           0x00, 0xff},
1441                         {IR_GLITCH_LEN,          0x03, 0xff},
1442                         {IR_RX_CLK,              0x09, 0xff},
1443                         {IR_RX_CFG,              0x1c, 0xff},
1444                         {IR_MAX_H_TOL_LEN,       0x1e, 0xff},
1445                         {IR_MAX_L_TOL_LEN,       0x1e, 0xff},
1446                         {IR_RX_CTRL,             0x80, 0xff},
1447                 };
1448
1449                 for (i = 0; i < ARRAY_SIZE(init_tab); i++) {
1450                         ret = rtl28xx_wr_reg_mask(d, init_tab[i].reg,
1451                                         init_tab[i].val, init_tab[i].mask);
1452                         if (ret)
1453                                 goto err;
1454                 }
1455
1456                 priv->rc_active = true;
1457         }
1458
1459         ret = rtl28xx_rd_reg(d, IR_RX_IF, &buf[0]);
1460         if (ret)
1461                 goto err;
1462
1463         if (buf[0] != 0x83)
1464                 goto exit;
1465
1466         ret = rtl28xx_rd_reg(d, IR_RX_BC, &buf[0]);
1467         if (ret)
1468                 goto err;
1469
1470         len = buf[0];
1471
1472         /* read raw code from hw */
1473         ret = rtl2831_rd_regs(d, IR_RX_BUF, buf, len);
1474         if (ret)
1475                 goto err;
1476
1477         /* let hw receive new code */
1478         for (i = 0; i < ARRAY_SIZE(refresh_tab); i++) {
1479                 ret = rtl28xx_wr_reg_mask(d, refresh_tab[i].reg,
1480                                 refresh_tab[i].val, refresh_tab[i].mask);
1481                 if (ret)
1482                         goto err;
1483         }
1484
1485         /* pass data to Kernel IR decoder */
1486         init_ir_raw_event(&ev);
1487
1488         for (i = 0; i < len; i++) {
1489                 ev.pulse = buf[i] >> 7;
1490                 ev.duration = 50800 * (buf[i] & 0x7f);
1491                 ir_raw_event_store_with_filter(d->rc_dev, &ev);
1492         }
1493
1494         /* 'flush' ir_raw_event_store_with_filter() */
1495         ir_raw_event_set_idle(d->rc_dev, true);
1496         ir_raw_event_handle(d->rc_dev);
1497 exit:
1498         return ret;
1499 err:
1500         dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
1501         return ret;
1502 }
1503
1504 static int rtl2832u_get_rc_config(struct dvb_usb_device *d,
1505                 struct dvb_usb_rc *rc)
1506 {
1507         /* disable IR interrupts in order to avoid SDR sample loss */
1508         if (rtl28xxu_disable_rc)
1509                 return rtl28xx_wr_reg(d, IR_RX_IE, 0x00);
1510
1511         /* load empty to enable rc */
1512         if (!rc->map_name)
1513                 rc->map_name = RC_MAP_EMPTY;
1514         rc->allowed_protos = RC_BIT_ALL;
1515         rc->driver_type = RC_DRIVER_IR_RAW;
1516         rc->query = rtl2832u_rc_query;
1517         rc->interval = 400;
1518
1519         return 0;
1520 }
1521 #else
1522 #define rtl2831u_get_rc_config NULL
1523 #define rtl2832u_get_rc_config NULL
1524 #endif
1525
1526 static const struct dvb_usb_device_properties rtl2831u_props = {
1527         .driver_name = KBUILD_MODNAME,
1528         .owner = THIS_MODULE,
1529         .adapter_nr = adapter_nr,
1530         .size_of_priv = sizeof(struct rtl28xxu_priv),
1531
1532         .power_ctrl = rtl2831u_power_ctrl,
1533         .i2c_algo = &rtl28xxu_i2c_algo,
1534         .read_config = rtl2831u_read_config,
1535         .frontend_attach = rtl2831u_frontend_attach,
1536         .tuner_attach = rtl2831u_tuner_attach,
1537         .init = rtl28xxu_init,
1538         .get_rc_config = rtl2831u_get_rc_config,
1539
1540         .num_adapters = 1,
1541         .adapter = {
1542                 {
1543                         .stream = DVB_USB_STREAM_BULK(0x81, 6, 8 * 512),
1544                 },
1545         },
1546 };
1547
1548 static const struct dvb_usb_device_properties rtl2832u_props = {
1549         .driver_name = KBUILD_MODNAME,
1550         .owner = THIS_MODULE,
1551         .adapter_nr = adapter_nr,
1552         .size_of_priv = sizeof(struct rtl28xxu_priv),
1553
1554         .power_ctrl = rtl2832u_power_ctrl,
1555         .frontend_ctrl = rtl2832u_frontend_ctrl,
1556         .i2c_algo = &rtl28xxu_i2c_algo,
1557         .read_config = rtl2832u_read_config,
1558         .frontend_attach = rtl2832u_frontend_attach,
1559         .tuner_attach = rtl2832u_tuner_attach,
1560         .init = rtl28xxu_init,
1561         .exit = rtl28xxu_exit,
1562         .get_rc_config = rtl2832u_get_rc_config,
1563
1564         .num_adapters = 1,
1565         .adapter = {
1566                 {
1567                         .stream = DVB_USB_STREAM_BULK(0x81, 6, 8 * 512),
1568                 },
1569         },
1570 };
1571
1572 static const struct usb_device_id rtl28xxu_id_table[] = {
1573         /* RTL2831U devices: */
1574         { DVB_USB_DEVICE(USB_VID_REALTEK, USB_PID_REALTEK_RTL2831U,
1575                 &rtl2831u_props, "Realtek RTL2831U reference design", NULL) },
1576         { DVB_USB_DEVICE(USB_VID_WIDEVIEW, USB_PID_FREECOM_DVBT,
1577                 &rtl2831u_props, "Freecom USB2.0 DVB-T", NULL) },
1578         { DVB_USB_DEVICE(USB_VID_WIDEVIEW, USB_PID_FREECOM_DVBT_2,
1579                 &rtl2831u_props, "Freecom USB2.0 DVB-T", NULL) },
1580
1581         /* RTL2832U devices: */
1582         { DVB_USB_DEVICE(USB_VID_REALTEK, 0x2832,
1583                 &rtl2832u_props, "Realtek RTL2832U reference design", NULL) },
1584         { DVB_USB_DEVICE(USB_VID_REALTEK, 0x2838,
1585                 &rtl2832u_props, "Realtek RTL2832U reference design", NULL) },
1586         { DVB_USB_DEVICE(USB_VID_TERRATEC, USB_PID_TERRATEC_CINERGY_T_STICK_BLACK_REV1,
1587                 &rtl2832u_props, "TerraTec Cinergy T Stick Black", RC_MAP_TERRATEC_SLIM) },
1588         { DVB_USB_DEVICE(USB_VID_GTEK, USB_PID_DELOCK_USB2_DVBT,
1589                 &rtl2832u_props, "G-Tek Electronics Group Lifeview LV5TDLX DVB-T", NULL) },
1590         { DVB_USB_DEVICE(USB_VID_TERRATEC, USB_PID_NOXON_DAB_STICK,
1591                 &rtl2832u_props, "TerraTec NOXON DAB Stick", NULL) },
1592         { DVB_USB_DEVICE(USB_VID_TERRATEC, USB_PID_NOXON_DAB_STICK_REV2,
1593                 &rtl2832u_props, "TerraTec NOXON DAB Stick (rev 2)", NULL) },
1594         { DVB_USB_DEVICE(USB_VID_TERRATEC, USB_PID_NOXON_DAB_STICK_REV3,
1595                 &rtl2832u_props, "TerraTec NOXON DAB Stick (rev 3)", NULL) },
1596         { DVB_USB_DEVICE(USB_VID_GTEK, USB_PID_TREKSTOR_TERRES_2_0,
1597                 &rtl2832u_props, "Trekstor DVB-T Stick Terres 2.0", NULL) },
1598         { DVB_USB_DEVICE(USB_VID_DEXATEK, 0x1101,
1599                 &rtl2832u_props, "Dexatek DK DVB-T Dongle", NULL) },
1600         { DVB_USB_DEVICE(USB_VID_LEADTEK, 0x6680,
1601                 &rtl2832u_props, "DigitalNow Quad DVB-T Receiver", NULL) },
1602         { DVB_USB_DEVICE(USB_VID_LEADTEK, USB_PID_WINFAST_DTV_DONGLE_MINID,
1603                 &rtl2832u_props, "Leadtek Winfast DTV Dongle Mini D", NULL) },
1604         { DVB_USB_DEVICE(USB_VID_TERRATEC, 0x00d3,
1605                 &rtl2832u_props, "TerraTec Cinergy T Stick RC (Rev. 3)", NULL) },
1606         { DVB_USB_DEVICE(USB_VID_DEXATEK, 0x1102,
1607                 &rtl2832u_props, "Dexatek DK mini DVB-T Dongle", NULL) },
1608         { DVB_USB_DEVICE(USB_VID_TERRATEC, 0x00d7,
1609                 &rtl2832u_props, "TerraTec Cinergy T Stick+", NULL) },
1610         { DVB_USB_DEVICE(USB_VID_KWORLD_2, 0xd3a8,
1611                 &rtl2832u_props, "ASUS My Cinema-U3100Mini Plus V2", NULL) },
1612         { DVB_USB_DEVICE(USB_VID_KWORLD_2, 0xd393,
1613                 &rtl2832u_props, "GIGABYTE U7300", NULL) },
1614         { DVB_USB_DEVICE(USB_VID_DEXATEK, 0x1104,
1615                 &rtl2832u_props, "MSI DIGIVOX Micro HD", NULL) },
1616         { DVB_USB_DEVICE(USB_VID_COMPRO, 0x0620,
1617                 &rtl2832u_props, "Compro VideoMate U620F", NULL) },
1618         { DVB_USB_DEVICE(USB_VID_KWORLD_2, 0xd394,
1619                 &rtl2832u_props, "MaxMedia HU394-T", NULL) },
1620         { DVB_USB_DEVICE(USB_VID_LEADTEK, 0x6a03,
1621                 &rtl2832u_props, "Leadtek WinFast DTV Dongle mini", NULL) },
1622         { DVB_USB_DEVICE(USB_VID_GTEK, USB_PID_CPYTO_REDI_PC50A,
1623                 &rtl2832u_props, "Crypto ReDi PC 50 A", NULL) },
1624         { DVB_USB_DEVICE(USB_VID_KYE, 0x707f,
1625                 &rtl2832u_props, "Genius TVGo DVB-T03", NULL) },
1626         { DVB_USB_DEVICE(USB_VID_KWORLD_2, 0xd395,
1627                 &rtl2832u_props, "Peak DVB-T USB", NULL) },
1628         { DVB_USB_DEVICE(USB_VID_KWORLD_2, USB_PID_SVEON_STV20_RTL2832U,
1629                 &rtl2832u_props, "Sveon STV20", NULL) },
1630         { DVB_USB_DEVICE(USB_VID_KWORLD_2, USB_PID_SVEON_STV21,
1631                 &rtl2832u_props, "Sveon STV21", NULL) },
1632         { DVB_USB_DEVICE(USB_VID_KWORLD_2, USB_PID_SVEON_STV27,
1633                 &rtl2832u_props, "Sveon STV27", NULL) },
1634
1635         /* RTL2832P devices: */
1636         { DVB_USB_DEVICE(USB_VID_HANFTEK, 0x0131,
1637                 &rtl2832u_props, "Astrometa DVB-T2", NULL) },
1638         { }
1639 };
1640 MODULE_DEVICE_TABLE(usb, rtl28xxu_id_table);
1641
1642 static struct usb_driver rtl28xxu_usb_driver = {
1643         .name = KBUILD_MODNAME,
1644         .id_table = rtl28xxu_id_table,
1645         .probe = dvb_usbv2_probe,
1646         .disconnect = dvb_usbv2_disconnect,
1647         .suspend = dvb_usbv2_suspend,
1648         .resume = dvb_usbv2_resume,
1649         .reset_resume = dvb_usbv2_reset_resume,
1650         .no_dynamic_id = 1,
1651         .soft_unbind = 1,
1652 };
1653
1654 module_usb_driver(rtl28xxu_usb_driver);
1655
1656 MODULE_DESCRIPTION("Realtek RTL28xxU DVB USB driver");
1657 MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
1658 MODULE_AUTHOR("Thomas Mair <thomas.mair86@googlemail.com>");
1659 MODULE_LICENSE("GPL");