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