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