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