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