[media] rtl28xxu: do not refcount rtl2832_sdr module
[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_frontend_callback(void *adapter_priv, int component,
725 int cmd, int arg)
726 {
727 struct i2c_adapter *adapter = adapter_priv;
728 struct device *parent = adapter->dev.parent;
729 struct i2c_adapter *parent_adapter;
730 struct dvb_usb_device *d;
731 struct rtl28xxu_priv *priv;
732
733 /*
734 * All tuners are connected to demod muxed I2C adapter. We have to
735 * resolve its parent adapter in order to get handle for this driver
736 * private data. That is a bit hackish solution, GPIO or direct driver
737 * callback would be better...
738 */
739 if (parent != NULL && parent->type == &i2c_adapter_type)
740 parent_adapter = to_i2c_adapter(parent);
741 else
742 return -EINVAL;
743
744 d = i2c_get_adapdata(parent_adapter);
745 priv = d->priv;
746
747 dev_dbg(&d->udev->dev, "%s: component=%d cmd=%d arg=%d\n",
748 __func__, component, cmd, arg);
749
750 switch (component) {
751 case DVB_FRONTEND_COMPONENT_TUNER:
752 switch (priv->tuner) {
753 case TUNER_RTL2832_FC0012:
754 return rtl2832u_fc0012_tuner_callback(d, cmd, arg);
755 case TUNER_RTL2832_TUA9001:
756 return rtl2832u_tua9001_tuner_callback(d, cmd, arg);
757 }
758 default:
759 return -EINVAL;
760 }
761
762 return 0;
763 }
764
765 static int rtl2832u_frontend_attach(struct dvb_usb_adapter *adap)
766 {
767 struct dvb_usb_device *d = adap_to_d(adap);
768 struct rtl28xxu_priv *priv = d_to_priv(d);
769 struct rtl2832_platform_data *pdata = &priv->rtl2832_platform_data;
770 struct i2c_board_info board_info;
771 struct i2c_client *client;
772 int ret;
773
774 dev_dbg(&d->udev->dev, "%s:\n", __func__);
775
776 switch (priv->tuner) {
777 case TUNER_RTL2832_FC0012:
778 *pdata = rtl2832_fc0012_platform_data;
779 break;
780 case TUNER_RTL2832_FC0013:
781 *pdata = rtl2832_fc0013_platform_data;
782 break;
783 case TUNER_RTL2832_FC2580:
784 /* FIXME: do not abuse fc0012 settings */
785 *pdata = rtl2832_fc0012_platform_data;
786 break;
787 case TUNER_RTL2832_TUA9001:
788 *pdata = rtl2832_tua9001_platform_data;
789 break;
790 case TUNER_RTL2832_E4000:
791 *pdata = rtl2832_e4000_platform_data;
792 break;
793 case TUNER_RTL2832_R820T:
794 case TUNER_RTL2832_R828D:
795 *pdata = rtl2832_r820t_platform_data;
796 break;
797 default:
798 dev_err(&d->udev->dev, "%s: unknown tuner=%s\n",
799 KBUILD_MODNAME, priv->tuner_name);
800 ret = -ENODEV;
801 goto err;
802 }
803
804 /* attach demodulator */
805 memset(&board_info, 0, sizeof(board_info));
806 strlcpy(board_info.type, "rtl2832", I2C_NAME_SIZE);
807 board_info.addr = 0x10;
808 board_info.platform_data = pdata;
809 request_module("%s", board_info.type);
810 client = i2c_new_device(&d->i2c_adap, &board_info);
811 if (client == NULL || client->dev.driver == NULL) {
812 ret = -ENODEV;
813 goto err;
814 }
815
816 if (!try_module_get(client->dev.driver->owner)) {
817 i2c_unregister_device(client);
818 ret = -ENODEV;
819 goto err;
820 }
821
822 adap->fe[0] = pdata->get_dvb_frontend(client);
823 priv->demod_i2c_adapter = pdata->get_i2c_adapter(client);
824
825 priv->i2c_client_demod = client;
826
827 /* set fe callback */
828 adap->fe[0]->callback = rtl2832u_frontend_callback;
829
830 if (priv->slave_demod) {
831 struct i2c_board_info info = {};
832
833 /*
834 * We continue on reduced mode, without DVB-T2/C, using master
835 * demod, when slave demod fails.
836 */
837 ret = 0;
838
839 /* attach slave demodulator */
840 if (priv->slave_demod == SLAVE_DEMOD_MN88472) {
841 struct mn88472_config mn88472_config = {};
842
843 mn88472_config.fe = &adap->fe[1];
844 mn88472_config.i2c_wr_max = 22,
845 strlcpy(info.type, "mn88472", I2C_NAME_SIZE);
846 mn88472_config.xtal = 20500000;
847 info.addr = 0x18;
848 info.platform_data = &mn88472_config;
849 request_module(info.type);
850 client = i2c_new_device(&d->i2c_adap, &info);
851 if (client == NULL || client->dev.driver == NULL) {
852 priv->slave_demod = SLAVE_DEMOD_NONE;
853 goto err_slave_demod_failed;
854 }
855
856 if (!try_module_get(client->dev.driver->owner)) {
857 i2c_unregister_device(client);
858 priv->slave_demod = SLAVE_DEMOD_NONE;
859 goto err_slave_demod_failed;
860 }
861
862 priv->i2c_client_slave_demod = client;
863 } else {
864 struct mn88473_config mn88473_config = {};
865
866 mn88473_config.fe = &adap->fe[1];
867 mn88473_config.i2c_wr_max = 22,
868 strlcpy(info.type, "mn88473", I2C_NAME_SIZE);
869 info.addr = 0x18;
870 info.platform_data = &mn88473_config;
871 request_module(info.type);
872 client = i2c_new_device(&d->i2c_adap, &info);
873 if (client == NULL || client->dev.driver == NULL) {
874 priv->slave_demod = SLAVE_DEMOD_NONE;
875 goto err_slave_demod_failed;
876 }
877
878 if (!try_module_get(client->dev.driver->owner)) {
879 i2c_unregister_device(client);
880 priv->slave_demod = SLAVE_DEMOD_NONE;
881 goto err_slave_demod_failed;
882 }
883
884 priv->i2c_client_slave_demod = client;
885 }
886 }
887
888 return 0;
889 err_slave_demod_failed:
890 err:
891 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
892 return ret;
893 }
894
895 static int rtl2832u_frontend_detach(struct dvb_usb_adapter *adap)
896 {
897 struct dvb_usb_device *d = adap_to_d(adap);
898 struct rtl28xxu_priv *priv = d_to_priv(d);
899 struct i2c_client *client;
900
901 dev_dbg(&d->udev->dev, "%s:\n", __func__);
902
903 /* remove I2C slave demod */
904 client = priv->i2c_client_slave_demod;
905 if (client) {
906 module_put(client->dev.driver->owner);
907 i2c_unregister_device(client);
908 }
909
910 /* remove I2C demod */
911 client = priv->i2c_client_demod;
912 if (client) {
913 module_put(client->dev.driver->owner);
914 i2c_unregister_device(client);
915 }
916
917 return 0;
918 }
919
920 static struct qt1010_config rtl28xxu_qt1010_config = {
921 .i2c_address = 0x62, /* 0xc4 */
922 };
923
924 static struct mt2060_config rtl28xxu_mt2060_config = {
925 .i2c_address = 0x60, /* 0xc0 */
926 .clock_out = 0,
927 };
928
929 static struct mxl5005s_config rtl28xxu_mxl5005s_config = {
930 .i2c_address = 0x63, /* 0xc6 */
931 .if_freq = IF_FREQ_4570000HZ,
932 .xtal_freq = CRYSTAL_FREQ_16000000HZ,
933 .agc_mode = MXL_SINGLE_AGC,
934 .tracking_filter = MXL_TF_C_H,
935 .rssi_enable = MXL_RSSI_ENABLE,
936 .cap_select = MXL_CAP_SEL_ENABLE,
937 .div_out = MXL_DIV_OUT_4,
938 .clock_out = MXL_CLOCK_OUT_DISABLE,
939 .output_load = MXL5005S_IF_OUTPUT_LOAD_200_OHM,
940 .top = MXL5005S_TOP_25P2,
941 .mod_mode = MXL_DIGITAL_MODE,
942 .if_mode = MXL_ZERO_IF,
943 .AgcMasterByte = 0x00,
944 };
945
946 static int rtl2831u_tuner_attach(struct dvb_usb_adapter *adap)
947 {
948 int ret;
949 struct dvb_usb_device *d = adap_to_d(adap);
950 struct rtl28xxu_priv *priv = d_to_priv(d);
951 struct dvb_frontend *fe;
952
953 dev_dbg(&d->udev->dev, "%s:\n", __func__);
954
955 switch (priv->tuner) {
956 case TUNER_RTL2830_QT1010:
957 fe = dvb_attach(qt1010_attach, adap->fe[0],
958 priv->demod_i2c_adapter,
959 &rtl28xxu_qt1010_config);
960 break;
961 case TUNER_RTL2830_MT2060:
962 fe = dvb_attach(mt2060_attach, adap->fe[0],
963 priv->demod_i2c_adapter,
964 &rtl28xxu_mt2060_config, 1220);
965 break;
966 case TUNER_RTL2830_MXL5005S:
967 fe = dvb_attach(mxl5005s_attach, adap->fe[0],
968 priv->demod_i2c_adapter,
969 &rtl28xxu_mxl5005s_config);
970 break;
971 default:
972 fe = NULL;
973 dev_err(&d->udev->dev, "%s: unknown tuner=%d\n", KBUILD_MODNAME,
974 priv->tuner);
975 }
976
977 if (fe == NULL) {
978 ret = -ENODEV;
979 goto err;
980 }
981
982 return 0;
983 err:
984 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
985 return ret;
986 }
987
988 static const struct fc2580_config rtl2832u_fc2580_config = {
989 .i2c_addr = 0x56,
990 .clock = 16384000,
991 };
992
993 static struct tua9001_config rtl2832u_tua9001_config = {
994 .i2c_addr = 0x60,
995 };
996
997 static const struct fc0012_config rtl2832u_fc0012_config = {
998 .i2c_address = 0x63, /* 0xc6 >> 1 */
999 .xtal_freq = FC_XTAL_28_8_MHZ,
1000 };
1001
1002 static const struct r820t_config rtl2832u_r820t_config = {
1003 .i2c_addr = 0x1a,
1004 .xtal = 28800000,
1005 .max_i2c_msg_len = 2,
1006 .rafael_chip = CHIP_R820T,
1007 };
1008
1009 static const struct r820t_config rtl2832u_r828d_config = {
1010 .i2c_addr = 0x3a,
1011 .xtal = 16000000,
1012 .max_i2c_msg_len = 2,
1013 .rafael_chip = CHIP_R828D,
1014 };
1015
1016 static int rtl2832u_tuner_attach(struct dvb_usb_adapter *adap)
1017 {
1018 int ret;
1019 struct dvb_usb_device *d = adap_to_d(adap);
1020 struct rtl28xxu_priv *priv = d_to_priv(d);
1021 struct dvb_frontend *fe = NULL;
1022 struct i2c_board_info info;
1023 struct i2c_client *client;
1024 struct v4l2_subdev *subdev = NULL;
1025
1026 dev_dbg(&d->udev->dev, "%s:\n", __func__);
1027
1028 memset(&info, 0, sizeof(struct i2c_board_info));
1029
1030 switch (priv->tuner) {
1031 case TUNER_RTL2832_FC0012:
1032 fe = dvb_attach(fc0012_attach, adap->fe[0],
1033 priv->demod_i2c_adapter, &rtl2832u_fc0012_config);
1034
1035 /* since fc0012 includs reading the signal strength delegate
1036 * that to the tuner driver */
1037 adap->fe[0]->ops.read_signal_strength =
1038 adap->fe[0]->ops.tuner_ops.get_rf_strength;
1039 break;
1040 case TUNER_RTL2832_FC0013:
1041 fe = dvb_attach(fc0013_attach, adap->fe[0],
1042 priv->demod_i2c_adapter, 0xc6>>1, 0, FC_XTAL_28_8_MHZ);
1043
1044 /* fc0013 also supports signal strength reading */
1045 adap->fe[0]->ops.read_signal_strength =
1046 adap->fe[0]->ops.tuner_ops.get_rf_strength;
1047 break;
1048 case TUNER_RTL2832_E4000: {
1049 struct e4000_config e4000_config = {
1050 .fe = adap->fe[0],
1051 .clock = 28800000,
1052 };
1053
1054 strlcpy(info.type, "e4000", I2C_NAME_SIZE);
1055 info.addr = 0x64;
1056 info.platform_data = &e4000_config;
1057
1058 request_module(info.type);
1059 client = i2c_new_device(priv->demod_i2c_adapter, &info);
1060 if (client == NULL || client->dev.driver == NULL)
1061 break;
1062
1063 if (!try_module_get(client->dev.driver->owner)) {
1064 i2c_unregister_device(client);
1065 break;
1066 }
1067
1068 priv->i2c_client_tuner = client;
1069 subdev = i2c_get_clientdata(client);
1070 }
1071 break;
1072 case TUNER_RTL2832_FC2580:
1073 fe = dvb_attach(fc2580_attach, adap->fe[0],
1074 priv->demod_i2c_adapter,
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],
1088 priv->demod_i2c_adapter,
1089 &rtl2832u_tua9001_config);
1090 break;
1091 case TUNER_RTL2832_R820T:
1092 fe = dvb_attach(r820t_attach, adap->fe[0],
1093 priv->demod_i2c_adapter,
1094 &rtl2832u_r820t_config);
1095
1096 /* Use tuner to get the signal strength */
1097 adap->fe[0]->ops.read_signal_strength =
1098 adap->fe[0]->ops.tuner_ops.get_rf_strength;
1099 break;
1100 case TUNER_RTL2832_R828D:
1101 fe = dvb_attach(r820t_attach, adap->fe[0],
1102 priv->demod_i2c_adapter,
1103 &rtl2832u_r828d_config);
1104 adap->fe[0]->ops.read_signal_strength =
1105 adap->fe[0]->ops.tuner_ops.get_rf_strength;
1106
1107 if (adap->fe[1]) {
1108 fe = dvb_attach(r820t_attach, adap->fe[1],
1109 priv->demod_i2c_adapter,
1110 &rtl2832u_r828d_config);
1111 adap->fe[1]->ops.read_signal_strength =
1112 adap->fe[1]->ops.tuner_ops.get_rf_strength;
1113 }
1114 break;
1115 default:
1116 dev_err(&d->udev->dev, "%s: unknown tuner=%d\n", KBUILD_MODNAME,
1117 priv->tuner);
1118 }
1119 if (fe == NULL && priv->i2c_client_tuner == NULL) {
1120 ret = -ENODEV;
1121 goto err;
1122 }
1123
1124 /* register SDR */
1125 switch (priv->tuner) {
1126 struct platform_device *pdev;
1127 struct rtl2832_sdr_platform_data pdata = {};
1128
1129 case TUNER_RTL2832_FC0012:
1130 case TUNER_RTL2832_FC0013:
1131 case TUNER_RTL2832_E4000:
1132 case TUNER_RTL2832_R820T:
1133 case TUNER_RTL2832_R828D:
1134 pdata.clk = priv->rtl2832_platform_data.clk;
1135 pdata.tuner = priv->tuner;
1136 pdata.i2c_client = priv->i2c_client_demod;
1137 pdata.bulk_read = priv->rtl2832_platform_data.bulk_read;
1138 pdata.bulk_write = priv->rtl2832_platform_data.bulk_write;
1139 pdata.update_bits = priv->rtl2832_platform_data.update_bits;
1140 pdata.dvb_frontend = adap->fe[0];
1141 pdata.dvb_usb_device = d;
1142 pdata.v4l2_subdev = subdev;
1143
1144 request_module("%s", "rtl2832_sdr");
1145 pdev = platform_device_register_data(&d->intf->dev,
1146 "rtl2832_sdr",
1147 PLATFORM_DEVID_AUTO,
1148 &pdata, sizeof(pdata));
1149 if (pdev == NULL || pdev->dev.driver == NULL)
1150 break;
1151 priv->platform_device_sdr = pdev;
1152 break;
1153 default:
1154 dev_dbg(&d->udev->dev, "no SDR for tuner=%d\n", priv->tuner);
1155 }
1156
1157 return 0;
1158 err:
1159 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
1160 return ret;
1161 }
1162
1163 static int rtl2832u_tuner_detach(struct dvb_usb_adapter *adap)
1164 {
1165 struct dvb_usb_device *d = adap_to_d(adap);
1166 struct rtl28xxu_priv *priv = d_to_priv(d);
1167 struct i2c_client *client;
1168 struct platform_device *pdev;
1169
1170 dev_dbg(&d->udev->dev, "%s:\n", __func__);
1171
1172 /* remove platform SDR */
1173 pdev = priv->platform_device_sdr;
1174 if (pdev)
1175 platform_device_unregister(pdev);
1176
1177 /* remove I2C tuner */
1178 client = priv->i2c_client_tuner;
1179 if (client) {
1180 module_put(client->dev.driver->owner);
1181 i2c_unregister_device(client);
1182 }
1183
1184 return 0;
1185 }
1186
1187 static int rtl28xxu_init(struct dvb_usb_device *d)
1188 {
1189 int ret;
1190 u8 val;
1191
1192 dev_dbg(&d->udev->dev, "%s:\n", __func__);
1193
1194 /* init USB endpoints */
1195 ret = rtl28xx_rd_reg(d, USB_SYSCTL_0, &val);
1196 if (ret)
1197 goto err;
1198
1199 /* enable DMA and Full Packet Mode*/
1200 val |= 0x09;
1201 ret = rtl28xx_wr_reg(d, USB_SYSCTL_0, val);
1202 if (ret)
1203 goto err;
1204
1205 /* set EPA maximum packet size to 0x0200 */
1206 ret = rtl28xx_wr_regs(d, USB_EPA_MAXPKT, "\x00\x02\x00\x00", 4);
1207 if (ret)
1208 goto err;
1209
1210 /* change EPA FIFO length */
1211 ret = rtl28xx_wr_regs(d, USB_EPA_FIFO_CFG, "\x14\x00\x00\x00", 4);
1212 if (ret)
1213 goto err;
1214
1215 return ret;
1216 err:
1217 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
1218 return ret;
1219 }
1220
1221 static int rtl2831u_power_ctrl(struct dvb_usb_device *d, int onoff)
1222 {
1223 int ret;
1224 u8 gpio, sys0, epa_ctl[2];
1225
1226 dev_dbg(&d->udev->dev, "%s: onoff=%d\n", __func__, onoff);
1227
1228 /* demod adc */
1229 ret = rtl28xx_rd_reg(d, SYS_SYS0, &sys0);
1230 if (ret)
1231 goto err;
1232
1233 /* tuner power, read GPIOs */
1234 ret = rtl28xx_rd_reg(d, SYS_GPIO_OUT_VAL, &gpio);
1235 if (ret)
1236 goto err;
1237
1238 dev_dbg(&d->udev->dev, "%s: RD SYS0=%02x GPIO_OUT_VAL=%02x\n", __func__,
1239 sys0, gpio);
1240
1241 if (onoff) {
1242 gpio |= 0x01; /* GPIO0 = 1 */
1243 gpio &= (~0x10); /* GPIO4 = 0 */
1244 gpio |= 0x04; /* GPIO2 = 1, LED on */
1245 sys0 = sys0 & 0x0f;
1246 sys0 |= 0xe0;
1247 epa_ctl[0] = 0x00; /* clear stall */
1248 epa_ctl[1] = 0x00; /* clear reset */
1249 } else {
1250 gpio &= (~0x01); /* GPIO0 = 0 */
1251 gpio |= 0x10; /* GPIO4 = 1 */
1252 gpio &= (~0x04); /* GPIO2 = 1, LED off */
1253 sys0 = sys0 & (~0xc0);
1254 epa_ctl[0] = 0x10; /* set stall */
1255 epa_ctl[1] = 0x02; /* set reset */
1256 }
1257
1258 dev_dbg(&d->udev->dev, "%s: WR SYS0=%02x GPIO_OUT_VAL=%02x\n", __func__,
1259 sys0, gpio);
1260
1261 /* demod adc */
1262 ret = rtl28xx_wr_reg(d, SYS_SYS0, sys0);
1263 if (ret)
1264 goto err;
1265
1266 /* tuner power, write GPIOs */
1267 ret = rtl28xx_wr_reg(d, SYS_GPIO_OUT_VAL, gpio);
1268 if (ret)
1269 goto err;
1270
1271 /* streaming EP: stall & reset */
1272 ret = rtl28xx_wr_regs(d, USB_EPA_CTL, epa_ctl, 2);
1273 if (ret)
1274 goto err;
1275
1276 if (onoff)
1277 usb_clear_halt(d->udev, usb_rcvbulkpipe(d->udev, 0x81));
1278
1279 return ret;
1280 err:
1281 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
1282 return ret;
1283 }
1284
1285 static int rtl2832u_power_ctrl(struct dvb_usb_device *d, int onoff)
1286 {
1287 int ret;
1288
1289 dev_dbg(&d->udev->dev, "%s: onoff=%d\n", __func__, onoff);
1290
1291 if (onoff) {
1292 /* GPIO3=1, GPIO4=0 */
1293 ret = rtl28xx_wr_reg_mask(d, SYS_GPIO_OUT_VAL, 0x08, 0x18);
1294 if (ret)
1295 goto err;
1296
1297 /* suspend? */
1298 ret = rtl28xx_wr_reg_mask(d, SYS_DEMOD_CTL1, 0x00, 0x10);
1299 if (ret)
1300 goto err;
1301
1302 /* enable PLL */
1303 ret = rtl28xx_wr_reg_mask(d, SYS_DEMOD_CTL, 0x80, 0x80);
1304 if (ret)
1305 goto err;
1306
1307 /* disable reset */
1308 ret = rtl28xx_wr_reg_mask(d, SYS_DEMOD_CTL, 0x20, 0x20);
1309 if (ret)
1310 goto err;
1311
1312 /* streaming EP: clear stall & reset */
1313 ret = rtl28xx_wr_regs(d, USB_EPA_CTL, "\x00\x00", 2);
1314 if (ret)
1315 goto err;
1316
1317 ret = usb_clear_halt(d->udev, usb_rcvbulkpipe(d->udev, 0x81));
1318 if (ret)
1319 goto err;
1320 } else {
1321 /* GPIO4=1 */
1322 ret = rtl28xx_wr_reg_mask(d, SYS_GPIO_OUT_VAL, 0x10, 0x10);
1323 if (ret)
1324 goto err;
1325
1326 /* disable PLL */
1327 ret = rtl28xx_wr_reg_mask(d, SYS_DEMOD_CTL, 0x00, 0x80);
1328 if (ret)
1329 goto err;
1330
1331 /* streaming EP: set stall & reset */
1332 ret = rtl28xx_wr_regs(d, USB_EPA_CTL, "\x10\x02", 2);
1333 if (ret)
1334 goto err;
1335 }
1336
1337 return ret;
1338 err:
1339 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
1340 return ret;
1341 }
1342
1343 static int rtl2832u_frontend_ctrl(struct dvb_frontend *fe, int onoff)
1344 {
1345 struct dvb_usb_device *d = fe_to_d(fe);
1346 struct rtl28xxu_priv *priv = fe_to_priv(fe);
1347 struct rtl2832_platform_data *pdata = &priv->rtl2832_platform_data;
1348 int ret;
1349 u8 val;
1350
1351 dev_dbg(&d->udev->dev, "%s: fe=%d onoff=%d\n", __func__, fe->id, onoff);
1352
1353 /* control internal demod ADC */
1354 if (fe->id == 0 && onoff)
1355 val = 0x48; /* enable ADC */
1356 else
1357 val = 0x00; /* disable ADC */
1358
1359 ret = rtl28xx_wr_reg_mask(d, SYS_DEMOD_CTL, val, 0x48);
1360 if (ret)
1361 goto err;
1362
1363 /* bypass slave demod TS through master demod */
1364 if (fe->id == 1 && onoff) {
1365 ret = pdata->enable_slave_ts(priv->i2c_client_demod);
1366 if (ret)
1367 goto err;
1368 }
1369
1370 return 0;
1371 err:
1372 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
1373 return ret;
1374 }
1375
1376 #if IS_ENABLED(CONFIG_RC_CORE)
1377 static int rtl2831u_rc_query(struct dvb_usb_device *d)
1378 {
1379 int ret, i;
1380 struct rtl28xxu_priv *priv = d->priv;
1381 u8 buf[5];
1382 u32 rc_code;
1383 struct rtl28xxu_reg_val rc_nec_tab[] = {
1384 { 0x3033, 0x80 },
1385 { 0x3020, 0x43 },
1386 { 0x3021, 0x16 },
1387 { 0x3022, 0x16 },
1388 { 0x3023, 0x5a },
1389 { 0x3024, 0x2d },
1390 { 0x3025, 0x16 },
1391 { 0x3026, 0x01 },
1392 { 0x3028, 0xb0 },
1393 { 0x3029, 0x04 },
1394 { 0x302c, 0x88 },
1395 { 0x302e, 0x13 },
1396 { 0x3030, 0xdf },
1397 { 0x3031, 0x05 },
1398 };
1399
1400 /* init remote controller */
1401 if (!priv->rc_active) {
1402 for (i = 0; i < ARRAY_SIZE(rc_nec_tab); i++) {
1403 ret = rtl28xx_wr_reg(d, rc_nec_tab[i].reg,
1404 rc_nec_tab[i].val);
1405 if (ret)
1406 goto err;
1407 }
1408 priv->rc_active = true;
1409 }
1410
1411 ret = rtl2831_rd_regs(d, SYS_IRRC_RP, buf, 5);
1412 if (ret)
1413 goto err;
1414
1415 if (buf[4] & 0x01) {
1416 if (buf[2] == (u8) ~buf[3]) {
1417 if (buf[0] == (u8) ~buf[1]) {
1418 /* NEC standard (16 bit) */
1419 rc_code = RC_SCANCODE_NEC(buf[0], buf[2]);
1420 } else {
1421 /* NEC extended (24 bit) */
1422 rc_code = RC_SCANCODE_NECX(buf[0] << 8 | buf[1],
1423 buf[2]);
1424 }
1425 } else {
1426 /* NEC full (32 bit) */
1427 rc_code = RC_SCANCODE_NEC32(buf[0] << 24 | buf[1] << 16 |
1428 buf[2] << 8 | buf[3]);
1429 }
1430
1431 rc_keydown(d->rc_dev, RC_TYPE_NEC, rc_code, 0);
1432
1433 ret = rtl28xx_wr_reg(d, SYS_IRRC_SR, 1);
1434 if (ret)
1435 goto err;
1436
1437 /* repeated intentionally to avoid extra keypress */
1438 ret = rtl28xx_wr_reg(d, SYS_IRRC_SR, 1);
1439 if (ret)
1440 goto err;
1441 }
1442
1443 return ret;
1444 err:
1445 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
1446 return ret;
1447 }
1448
1449 static int rtl2831u_get_rc_config(struct dvb_usb_device *d,
1450 struct dvb_usb_rc *rc)
1451 {
1452 rc->map_name = RC_MAP_EMPTY;
1453 rc->allowed_protos = RC_BIT_NEC;
1454 rc->query = rtl2831u_rc_query;
1455 rc->interval = 400;
1456
1457 return 0;
1458 }
1459
1460 static int rtl2832u_rc_query(struct dvb_usb_device *d)
1461 {
1462 int ret, i, len;
1463 struct rtl28xxu_priv *priv = d->priv;
1464 struct ir_raw_event ev;
1465 u8 buf[128];
1466 static const struct rtl28xxu_reg_val_mask refresh_tab[] = {
1467 {IR_RX_IF, 0x03, 0xff},
1468 {IR_RX_BUF_CTRL, 0x80, 0xff},
1469 {IR_RX_CTRL, 0x80, 0xff},
1470 };
1471
1472 /* init remote controller */
1473 if (!priv->rc_active) {
1474 static const struct rtl28xxu_reg_val_mask init_tab[] = {
1475 {SYS_DEMOD_CTL1, 0x00, 0x04},
1476 {SYS_DEMOD_CTL1, 0x00, 0x08},
1477 {USB_CTRL, 0x20, 0x20},
1478 {SYS_GPIO_DIR, 0x00, 0x08},
1479 {SYS_GPIO_OUT_EN, 0x08, 0x08},
1480 {SYS_GPIO_OUT_VAL, 0x08, 0x08},
1481 {IR_MAX_DURATION0, 0xd0, 0xff},
1482 {IR_MAX_DURATION1, 0x07, 0xff},
1483 {IR_IDLE_LEN0, 0xc0, 0xff},
1484 {IR_IDLE_LEN1, 0x00, 0xff},
1485 {IR_GLITCH_LEN, 0x03, 0xff},
1486 {IR_RX_CLK, 0x09, 0xff},
1487 {IR_RX_CFG, 0x1c, 0xff},
1488 {IR_MAX_H_TOL_LEN, 0x1e, 0xff},
1489 {IR_MAX_L_TOL_LEN, 0x1e, 0xff},
1490 {IR_RX_CTRL, 0x80, 0xff},
1491 };
1492
1493 for (i = 0; i < ARRAY_SIZE(init_tab); i++) {
1494 ret = rtl28xx_wr_reg_mask(d, init_tab[i].reg,
1495 init_tab[i].val, init_tab[i].mask);
1496 if (ret)
1497 goto err;
1498 }
1499
1500 priv->rc_active = true;
1501 }
1502
1503 ret = rtl28xx_rd_reg(d, IR_RX_IF, &buf[0]);
1504 if (ret)
1505 goto err;
1506
1507 if (buf[0] != 0x83)
1508 goto exit;
1509
1510 ret = rtl28xx_rd_reg(d, IR_RX_BC, &buf[0]);
1511 if (ret)
1512 goto err;
1513
1514 len = buf[0];
1515
1516 /* read raw code from hw */
1517 ret = rtl2831_rd_regs(d, IR_RX_BUF, buf, len);
1518 if (ret)
1519 goto err;
1520
1521 /* let hw receive new code */
1522 for (i = 0; i < ARRAY_SIZE(refresh_tab); i++) {
1523 ret = rtl28xx_wr_reg_mask(d, refresh_tab[i].reg,
1524 refresh_tab[i].val, refresh_tab[i].mask);
1525 if (ret)
1526 goto err;
1527 }
1528
1529 /* pass data to Kernel IR decoder */
1530 init_ir_raw_event(&ev);
1531
1532 for (i = 0; i < len; i++) {
1533 ev.pulse = buf[i] >> 7;
1534 ev.duration = 50800 * (buf[i] & 0x7f);
1535 ir_raw_event_store_with_filter(d->rc_dev, &ev);
1536 }
1537
1538 /* 'flush' ir_raw_event_store_with_filter() */
1539 ir_raw_event_set_idle(d->rc_dev, true);
1540 ir_raw_event_handle(d->rc_dev);
1541 exit:
1542 return ret;
1543 err:
1544 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
1545 return ret;
1546 }
1547
1548 static int rtl2832u_get_rc_config(struct dvb_usb_device *d,
1549 struct dvb_usb_rc *rc)
1550 {
1551 /* disable IR interrupts in order to avoid SDR sample loss */
1552 if (rtl28xxu_disable_rc)
1553 return rtl28xx_wr_reg(d, IR_RX_IE, 0x00);
1554
1555 /* load empty to enable rc */
1556 if (!rc->map_name)
1557 rc->map_name = RC_MAP_EMPTY;
1558 rc->allowed_protos = RC_BIT_ALL;
1559 rc->driver_type = RC_DRIVER_IR_RAW;
1560 rc->query = rtl2832u_rc_query;
1561 rc->interval = 400;
1562
1563 return 0;
1564 }
1565 #else
1566 #define rtl2831u_get_rc_config NULL
1567 #define rtl2832u_get_rc_config NULL
1568 #endif
1569
1570 static int rtl2831u_pid_filter_ctrl(struct dvb_usb_adapter *adap, int onoff)
1571 {
1572 struct dvb_usb_device *d = adap_to_d(adap);
1573 struct rtl28xxu_priv *priv = d_to_priv(d);
1574 struct rtl2830_platform_data *pdata = &priv->rtl2830_platform_data;
1575
1576 return pdata->pid_filter_ctrl(adap->fe[0], onoff);
1577 }
1578
1579 static int rtl2832u_pid_filter_ctrl(struct dvb_usb_adapter *adap, int onoff)
1580 {
1581 struct dvb_usb_device *d = adap_to_d(adap);
1582 struct rtl28xxu_priv *priv = d_to_priv(d);
1583 struct rtl2832_platform_data *pdata = &priv->rtl2832_platform_data;
1584
1585 return pdata->pid_filter_ctrl(adap->fe[0], onoff);
1586 }
1587
1588 static int rtl2831u_pid_filter(struct dvb_usb_adapter *adap, int index, u16 pid, int onoff)
1589 {
1590 struct dvb_usb_device *d = adap_to_d(adap);
1591 struct rtl28xxu_priv *priv = d_to_priv(d);
1592 struct rtl2830_platform_data *pdata = &priv->rtl2830_platform_data;
1593
1594 return pdata->pid_filter(adap->fe[0], index, pid, onoff);
1595 }
1596
1597 static int rtl2832u_pid_filter(struct dvb_usb_adapter *adap, int index, u16 pid, int onoff)
1598 {
1599 struct dvb_usb_device *d = adap_to_d(adap);
1600 struct rtl28xxu_priv *priv = d_to_priv(d);
1601 struct rtl2832_platform_data *pdata = &priv->rtl2832_platform_data;
1602
1603 return pdata->pid_filter(adap->fe[0], index, pid, onoff);
1604 }
1605
1606 static const struct dvb_usb_device_properties rtl2831u_props = {
1607 .driver_name = KBUILD_MODNAME,
1608 .owner = THIS_MODULE,
1609 .adapter_nr = adapter_nr,
1610 .size_of_priv = sizeof(struct rtl28xxu_priv),
1611
1612 .power_ctrl = rtl2831u_power_ctrl,
1613 .i2c_algo = &rtl28xxu_i2c_algo,
1614 .read_config = rtl2831u_read_config,
1615 .frontend_attach = rtl2831u_frontend_attach,
1616 .frontend_detach = rtl2832u_frontend_detach,
1617 .tuner_attach = rtl2831u_tuner_attach,
1618 .init = rtl28xxu_init,
1619 .get_rc_config = rtl2831u_get_rc_config,
1620
1621 .num_adapters = 1,
1622 .adapter = {
1623 {
1624 .caps = DVB_USB_ADAP_HAS_PID_FILTER |
1625 DVB_USB_ADAP_PID_FILTER_CAN_BE_TURNED_OFF,
1626
1627 .pid_filter_count = 32,
1628 .pid_filter_ctrl = rtl2831u_pid_filter_ctrl,
1629 .pid_filter = rtl2831u_pid_filter,
1630
1631 .stream = DVB_USB_STREAM_BULK(0x81, 6, 8 * 512),
1632 },
1633 },
1634 };
1635
1636 static const struct dvb_usb_device_properties rtl2832u_props = {
1637 .driver_name = KBUILD_MODNAME,
1638 .owner = THIS_MODULE,
1639 .adapter_nr = adapter_nr,
1640 .size_of_priv = sizeof(struct rtl28xxu_priv),
1641
1642 .power_ctrl = rtl2832u_power_ctrl,
1643 .frontend_ctrl = rtl2832u_frontend_ctrl,
1644 .i2c_algo = &rtl28xxu_i2c_algo,
1645 .read_config = rtl2832u_read_config,
1646 .frontend_attach = rtl2832u_frontend_attach,
1647 .frontend_detach = rtl2832u_frontend_detach,
1648 .tuner_attach = rtl2832u_tuner_attach,
1649 .tuner_detach = rtl2832u_tuner_detach,
1650 .init = rtl28xxu_init,
1651 .get_rc_config = rtl2832u_get_rc_config,
1652
1653 .num_adapters = 1,
1654 .adapter = {
1655 {
1656 .caps = DVB_USB_ADAP_HAS_PID_FILTER |
1657 DVB_USB_ADAP_PID_FILTER_CAN_BE_TURNED_OFF,
1658
1659 .pid_filter_count = 32,
1660 .pid_filter_ctrl = rtl2832u_pid_filter_ctrl,
1661 .pid_filter = rtl2832u_pid_filter,
1662
1663 .stream = DVB_USB_STREAM_BULK(0x81, 6, 8 * 512),
1664 },
1665 },
1666 };
1667
1668 static const struct usb_device_id rtl28xxu_id_table[] = {
1669 /* RTL2831U devices: */
1670 { DVB_USB_DEVICE(USB_VID_REALTEK, USB_PID_REALTEK_RTL2831U,
1671 &rtl2831u_props, "Realtek RTL2831U reference design", NULL) },
1672 { DVB_USB_DEVICE(USB_VID_WIDEVIEW, USB_PID_FREECOM_DVBT,
1673 &rtl2831u_props, "Freecom USB2.0 DVB-T", NULL) },
1674 { DVB_USB_DEVICE(USB_VID_WIDEVIEW, USB_PID_FREECOM_DVBT_2,
1675 &rtl2831u_props, "Freecom USB2.0 DVB-T", NULL) },
1676
1677 /* RTL2832U devices: */
1678 { DVB_USB_DEVICE(USB_VID_REALTEK, 0x2832,
1679 &rtl2832u_props, "Realtek RTL2832U reference design", NULL) },
1680 { DVB_USB_DEVICE(USB_VID_REALTEK, 0x2838,
1681 &rtl2832u_props, "Realtek RTL2832U reference design", NULL) },
1682 { DVB_USB_DEVICE(USB_VID_TERRATEC, USB_PID_TERRATEC_CINERGY_T_STICK_BLACK_REV1,
1683 &rtl2832u_props, "TerraTec Cinergy T Stick Black", RC_MAP_TERRATEC_SLIM) },
1684 { DVB_USB_DEVICE(USB_VID_GTEK, USB_PID_DELOCK_USB2_DVBT,
1685 &rtl2832u_props, "G-Tek Electronics Group Lifeview LV5TDLX DVB-T", NULL) },
1686 { DVB_USB_DEVICE(USB_VID_TERRATEC, USB_PID_NOXON_DAB_STICK,
1687 &rtl2832u_props, "TerraTec NOXON DAB Stick", NULL) },
1688 { DVB_USB_DEVICE(USB_VID_TERRATEC, USB_PID_NOXON_DAB_STICK_REV2,
1689 &rtl2832u_props, "TerraTec NOXON DAB Stick (rev 2)", NULL) },
1690 { DVB_USB_DEVICE(USB_VID_TERRATEC, USB_PID_NOXON_DAB_STICK_REV3,
1691 &rtl2832u_props, "TerraTec NOXON DAB Stick (rev 3)", NULL) },
1692 { DVB_USB_DEVICE(USB_VID_GTEK, USB_PID_TREKSTOR_TERRES_2_0,
1693 &rtl2832u_props, "Trekstor DVB-T Stick Terres 2.0", NULL) },
1694 { DVB_USB_DEVICE(USB_VID_DEXATEK, 0x1101,
1695 &rtl2832u_props, "Dexatek DK DVB-T Dongle", NULL) },
1696 { DVB_USB_DEVICE(USB_VID_LEADTEK, 0x6680,
1697 &rtl2832u_props, "DigitalNow Quad DVB-T Receiver", NULL) },
1698 { DVB_USB_DEVICE(USB_VID_LEADTEK, USB_PID_WINFAST_DTV_DONGLE_MINID,
1699 &rtl2832u_props, "Leadtek Winfast DTV Dongle Mini D", NULL) },
1700 { DVB_USB_DEVICE(USB_VID_TERRATEC, 0x00d3,
1701 &rtl2832u_props, "TerraTec Cinergy T Stick RC (Rev. 3)", NULL) },
1702 { DVB_USB_DEVICE(USB_VID_DEXATEK, 0x1102,
1703 &rtl2832u_props, "Dexatek DK mini DVB-T Dongle", NULL) },
1704 { DVB_USB_DEVICE(USB_VID_TERRATEC, 0x00d7,
1705 &rtl2832u_props, "TerraTec Cinergy T Stick+", NULL) },
1706 { DVB_USB_DEVICE(USB_VID_KWORLD_2, 0xd3a8,
1707 &rtl2832u_props, "ASUS My Cinema-U3100Mini Plus V2", NULL) },
1708 { DVB_USB_DEVICE(USB_VID_KWORLD_2, 0xd393,
1709 &rtl2832u_props, "GIGABYTE U7300", NULL) },
1710 { DVB_USB_DEVICE(USB_VID_DEXATEK, 0x1104,
1711 &rtl2832u_props, "MSI DIGIVOX Micro HD", NULL) },
1712 { DVB_USB_DEVICE(USB_VID_COMPRO, 0x0620,
1713 &rtl2832u_props, "Compro VideoMate U620F", NULL) },
1714 { DVB_USB_DEVICE(USB_VID_KWORLD_2, 0xd394,
1715 &rtl2832u_props, "MaxMedia HU394-T", NULL) },
1716 { DVB_USB_DEVICE(USB_VID_LEADTEK, 0x6a03,
1717 &rtl2832u_props, "Leadtek WinFast DTV Dongle mini", NULL) },
1718 { DVB_USB_DEVICE(USB_VID_GTEK, USB_PID_CPYTO_REDI_PC50A,
1719 &rtl2832u_props, "Crypto ReDi PC 50 A", NULL) },
1720 { DVB_USB_DEVICE(USB_VID_KYE, 0x707f,
1721 &rtl2832u_props, "Genius TVGo DVB-T03", NULL) },
1722 { DVB_USB_DEVICE(USB_VID_KWORLD_2, 0xd395,
1723 &rtl2832u_props, "Peak DVB-T USB", NULL) },
1724 { DVB_USB_DEVICE(USB_VID_KWORLD_2, USB_PID_SVEON_STV20_RTL2832U,
1725 &rtl2832u_props, "Sveon STV20", NULL) },
1726 { DVB_USB_DEVICE(USB_VID_KWORLD_2, USB_PID_SVEON_STV21,
1727 &rtl2832u_props, "Sveon STV21", NULL) },
1728 { DVB_USB_DEVICE(USB_VID_KWORLD_2, USB_PID_SVEON_STV27,
1729 &rtl2832u_props, "Sveon STV27", NULL) },
1730
1731 /* RTL2832P devices: */
1732 { DVB_USB_DEVICE(USB_VID_HANFTEK, 0x0131,
1733 &rtl2832u_props, "Astrometa DVB-T2", NULL) },
1734 { }
1735 };
1736 MODULE_DEVICE_TABLE(usb, rtl28xxu_id_table);
1737
1738 static struct usb_driver rtl28xxu_usb_driver = {
1739 .name = KBUILD_MODNAME,
1740 .id_table = rtl28xxu_id_table,
1741 .probe = dvb_usbv2_probe,
1742 .disconnect = dvb_usbv2_disconnect,
1743 .suspend = dvb_usbv2_suspend,
1744 .resume = dvb_usbv2_resume,
1745 .reset_resume = dvb_usbv2_reset_resume,
1746 .no_dynamic_id = 1,
1747 .soft_unbind = 1,
1748 };
1749
1750 module_usb_driver(rtl28xxu_usb_driver);
1751
1752 MODULE_DESCRIPTION("Realtek RTL28xxU DVB USB driver");
1753 MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
1754 MODULE_AUTHOR("Thomas Mair <thomas.mair86@googlemail.com>");
1755 MODULE_LICENSE("GPL");
This page took 0.106953 seconds and 5 git commands to generate.