blob: 101d98e5dcdb9812dd7a67d6189a0b398ec32082 [file] [log] [blame]
Antti Palosaari831e0b72011-07-08 23:36:07 -03001/*
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 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 */
21
22#include "rtl28xxu.h"
23
24#include "rtl2830.h"
25
26#include "qt1010.h"
27#include "mt2060.h"
28#include "mxl5005s.h"
29
30/* debug */
31static int dvb_usb_rtl28xxu_debug;
32module_param_named(debug, dvb_usb_rtl28xxu_debug, int, 0644);
33MODULE_PARM_DESC(debug, "set debugging level" DVB_USB_DEBUG_STATUS);
34DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
35
36static int rtl28xxu_ctrl_msg(struct dvb_usb_device *d, struct rtl28xxu_req *req)
37{
38 int ret;
39 unsigned int pipe;
40 u8 requesttype;
41 u8 *buf;
42
43 buf = kmalloc(req->size, GFP_KERNEL);
44 if (!buf) {
45 ret = -ENOMEM;
46 goto err;
47 }
48
49 if (req->index & CMD_WR_FLAG) {
50 /* write */
51 memcpy(buf, req->data, req->size);
52 requesttype = (USB_TYPE_VENDOR | USB_DIR_OUT);
53 pipe = usb_sndctrlpipe(d->udev, 0);
54 } else {
55 /* read */
56 requesttype = (USB_TYPE_VENDOR | USB_DIR_IN);
57 pipe = usb_rcvctrlpipe(d->udev, 0);
58 }
59
60 ret = usb_control_msg(d->udev, pipe, 0, requesttype, req->value,
61 req->index, buf, req->size, 1000);
62
63 deb_dump(0, requesttype, req->value, req->index, buf, req->size,
64 deb_xfer);
65
66 if (ret < 0)
67 goto err_dealloc;
68 else
69 ret = 0;
70
71 /* read request, copy returned data to return buf */
72 if (!ret && requesttype == (USB_TYPE_VENDOR | USB_DIR_IN))
73 memcpy(req->data, buf, req->size);
74
75 kfree(buf);
76 return ret;
77
78err_dealloc:
79 kfree(buf);
80err:
81 deb_info("%s: failed=%d\n", __func__, ret);
82 return ret;
83}
84
85static int rtl2831_wr_regs(struct dvb_usb_device *d, u16 reg, u8 *val, int len)
86{
87 struct rtl28xxu_req req;
88
89 if (reg < 0x3000)
90 req.index = CMD_USB_WR;
Antti Palosaarib5cbaa42011-08-04 13:26:10 -030091 else if (reg < 0x4000)
Antti Palosaari831e0b72011-07-08 23:36:07 -030092 req.index = CMD_SYS_WR;
Antti Palosaarib5cbaa42011-08-04 13:26:10 -030093 else
94 req.index = CMD_IR_WR;
Antti Palosaari831e0b72011-07-08 23:36:07 -030095
96 req.value = reg;
97 req.size = len;
98 req.data = val;
99
100 return rtl28xxu_ctrl_msg(d, &req);
101}
102
103static int rtl2831_rd_regs(struct dvb_usb_device *d, u16 reg, u8 *val, int len)
104{
105 struct rtl28xxu_req req;
106
107 if (reg < 0x3000)
108 req.index = CMD_USB_RD;
Antti Palosaarib5cbaa42011-08-04 13:26:10 -0300109 else if (reg < 0x4000)
Antti Palosaari831e0b72011-07-08 23:36:07 -0300110 req.index = CMD_SYS_RD;
Antti Palosaarib5cbaa42011-08-04 13:26:10 -0300111 else
112 req.index = CMD_IR_RD;
Antti Palosaari831e0b72011-07-08 23:36:07 -0300113
114 req.value = reg;
115 req.size = len;
116 req.data = val;
117
118 return rtl28xxu_ctrl_msg(d, &req);
119}
120
121static int rtl2831_wr_reg(struct dvb_usb_device *d, u16 reg, u8 val)
122{
123 return rtl2831_wr_regs(d, reg, &val, 1);
124}
125
126static int rtl2831_rd_reg(struct dvb_usb_device *d, u16 reg, u8 *val)
127{
128 return rtl2831_rd_regs(d, reg, val, 1);
129}
130
131/* I2C */
132static int rtl28xxu_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg msg[],
133 int num)
134{
135 int ret;
136 struct dvb_usb_device *d = i2c_get_adapdata(adap);
Antti Palosaari34ec2932011-08-04 20:21:16 -0300137 struct rtl28xxu_priv *priv = d->priv;
Antti Palosaari831e0b72011-07-08 23:36:07 -0300138 struct rtl28xxu_req req;
139
140 /*
141 * It is not known which are real I2C bus xfer limits, but testing
142 * with RTL2831U + MT2060 gives max RD 24 and max WR 22 bytes.
Antti Palosaari34ec2932011-08-04 20:21:16 -0300143 * TODO: find out RTL2832U lens
144 */
145
146 /*
147 * I2C adapter logic looks rather complicated due to fact it handles
148 * three different access methods. Those methods are;
149 * 1) integrated demod access
150 * 2) old I2C access
151 * 3) new I2C access
152 *
153 * Used method is selected in order 1, 2, 3. Method 3 can handle all
154 * requests but there is two reasons why not use it always;
155 * 1) It is most expensive, usually two USB messages are needed
156 * 2) At least RTL2831U does not support it
157 *
158 * Method 3 is needed in case of I2C write+read (typical register read)
159 * where write is more than one byte.
Antti Palosaari831e0b72011-07-08 23:36:07 -0300160 */
161
162 if (mutex_lock_interruptible(&d->i2c_mutex) < 0)
163 return -EAGAIN;
164
165 if (num == 2 && !(msg[0].flags & I2C_M_RD) &&
166 (msg[1].flags & I2C_M_RD)) {
Antti Palosaari34ec2932011-08-04 20:21:16 -0300167 if (msg[0].len > 24 || msg[1].len > 24) {
168 /* TODO: check msg[0].len max */
Antti Palosaari831e0b72011-07-08 23:36:07 -0300169 ret = -EOPNOTSUPP;
170 goto err_unlock;
Antti Palosaari34ec2932011-08-04 20:21:16 -0300171 } else if (msg[0].addr == 0x10) {
172 /* method 1 - integrated demod */
173 req.value = (msg[0].buf[0] << 8) | (msg[0].addr << 1);
174 req.index = CMD_DEMOD_RD | priv->page;
Antti Palosaari831e0b72011-07-08 23:36:07 -0300175 req.size = msg[1].len;
176 req.data = &msg[1].buf[0];
177 ret = rtl28xxu_ctrl_msg(d, &req);
Antti Palosaari34ec2932011-08-04 20:21:16 -0300178 } else if (msg[0].len < 2) {
179 /* method 2 - old I2C */
Antti Palosaari831e0b72011-07-08 23:36:07 -0300180 req.value = (msg[0].buf[0] << 8) | (msg[0].addr << 1);
181 req.index = CMD_I2C_RD;
182 req.size = msg[1].len;
183 req.data = &msg[1].buf[0];
184 ret = rtl28xxu_ctrl_msg(d, &req);
Antti Palosaari34ec2932011-08-04 20:21:16 -0300185 } else {
186 /* method 3 - new I2C */
187 req.value = (msg[0].addr << 1);
188 req.index = CMD_I2C_DA_WR;
189 req.size = msg[0].len;
190 req.data = msg[0].buf;
191 ret = rtl28xxu_ctrl_msg(d, &req);
192 if (ret)
193 goto err_unlock;
194
195 req.value = (msg[0].addr << 1);
196 req.index = CMD_I2C_DA_RD;
197 req.size = msg[1].len;
198 req.data = msg[1].buf;
199 ret = rtl28xxu_ctrl_msg(d, &req);
Antti Palosaari831e0b72011-07-08 23:36:07 -0300200 }
201 } else if (num == 1 && !(msg[0].flags & I2C_M_RD)) {
202 if (msg[0].len > 22) {
Antti Palosaari34ec2932011-08-04 20:21:16 -0300203 /* TODO: check msg[0].len max */
Antti Palosaari831e0b72011-07-08 23:36:07 -0300204 ret = -EOPNOTSUPP;
205 goto err_unlock;
Antti Palosaari34ec2932011-08-04 20:21:16 -0300206 } else if (msg[0].addr == 0x10) {
207 /* method 1 - integrated demod */
208 if (msg[0].buf[0] == 0x00) {
209 /* save demod page for later demod access */
210 priv->page = msg[0].buf[1];
211 ret = 0;
212 } else {
213 req.value = (msg[0].buf[0] << 8) |
214 (msg[0].addr << 1);
215 req.index = CMD_DEMOD_WR | priv->page;
216 req.size = msg[0].len-1;
217 req.data = &msg[0].buf[1];
218 ret = rtl28xxu_ctrl_msg(d, &req);
219 }
220 } else if (msg[0].len < 23) {
221 /* method 2 - old I2C */
Antti Palosaari831e0b72011-07-08 23:36:07 -0300222 req.value = (msg[0].buf[0] << 8) | (msg[0].addr << 1);
223 req.index = CMD_I2C_WR;
224 req.size = msg[0].len-1;
225 req.data = &msg[0].buf[1];
226 ret = rtl28xxu_ctrl_msg(d, &req);
Antti Palosaari34ec2932011-08-04 20:21:16 -0300227 } else {
228 /* method 3 - new I2C */
229 req.value = (msg[0].addr << 1);
230 req.index = CMD_I2C_DA_WR;
231 req.size = msg[0].len;
232 req.data = msg[0].buf;
233 ret = rtl28xxu_ctrl_msg(d, &req);
Antti Palosaari831e0b72011-07-08 23:36:07 -0300234 }
235 } else {
236 ret = -EINVAL;
237 }
238
239err_unlock:
240 mutex_unlock(&d->i2c_mutex);
241
242 return ret ? ret : num;
243}
244
245static u32 rtl28xxu_i2c_func(struct i2c_adapter *adapter)
246{
247 return I2C_FUNC_I2C;
248}
249
250static struct i2c_algorithm rtl28xxu_i2c_algo = {
251 .master_xfer = rtl28xxu_i2c_xfer,
252 .functionality = rtl28xxu_i2c_func,
253};
254
255static struct rtl2830_config rtl28xxu_rtl2830_mt2060_config = {
256 .i2c_addr = 0x10, /* 0x20 */
257 .xtal = 28800000,
258 .ts_mode = 0,
259 .spec_inv = 1,
260 .if_dvbt = 36150000,
261 .vtop = 0x20,
262 .krf = 0x04,
263 .agc_targ_val = 0x2d,
264
265};
266
267static struct rtl2830_config rtl28xxu_rtl2830_qt1010_config = {
268 .i2c_addr = 0x10, /* 0x20 */
269 .xtal = 28800000,
270 .ts_mode = 0,
271 .spec_inv = 1,
272 .if_dvbt = 36125000,
273 .vtop = 0x20,
274 .krf = 0x04,
275 .agc_targ_val = 0x2d,
276};
277
278static struct rtl2830_config rtl28xxu_rtl2830_mxl5005s_config = {
279 .i2c_addr = 0x10, /* 0x20 */
280 .xtal = 28800000,
281 .ts_mode = 0,
282 .spec_inv = 0,
283 .if_dvbt = 4570000,
284 .vtop = 0x3f,
285 .krf = 0x04,
286 .agc_targ_val = 0x3e,
287};
288
Antti Palosaarib5cbaa42011-08-04 13:26:10 -0300289static int rtl2831u_frontend_attach(struct dvb_usb_adapter *adap)
Antti Palosaari831e0b72011-07-08 23:36:07 -0300290{
291 int ret;
292 struct rtl28xxu_priv *priv = adap->dev->priv;
293 u8 buf[1];
294 struct rtl2830_config *rtl2830_config;
Antti Palosaarib5cbaa42011-08-04 13:26:10 -0300295 /* open RTL2831U/RTL2830 I2C gate */
296 struct rtl28xxu_req req_gate = {0x0120, 0x0011, 0x0001, "\x08"};
Antti Palosaari831e0b72011-07-08 23:36:07 -0300297 /* for MT2060 tuner probe */
298 struct rtl28xxu_req req_mt2060 = {0x00c0, CMD_I2C_RD, 1, buf};
299 /* for QT1010 tuner probe */
300 struct rtl28xxu_req req_qt1010 = {0x0fc4, CMD_I2C_RD, 1, buf};
Antti Palosaari831e0b72011-07-08 23:36:07 -0300301
302 deb_info("%s:\n", __func__);
303
304 /*
305 * RTL2831U GPIOs
306 * =========================================================
307 * GPIO0 | tuner#0 | 0 off | 1 on | MXL5005S (?)
308 * GPIO2 | LED | 0 off | 1 on |
309 * GPIO4 | tuner#1 | 0 on | 1 off | MT2060
310 */
311
312 /* GPIO direction */
313 ret = rtl2831_wr_reg(adap->dev, SYS_GPIO_DIR, 0x0a);
314 if (ret)
315 goto err;
316
317 /* enable as output GPIO0, GPIO2, GPIO4 */
318 ret = rtl2831_wr_reg(adap->dev, SYS_GPIO_OUT_EN, 0x15);
319 if (ret)
320 goto err;
321
322 /*
323 * Probe used tuner. We need to know used tuner before demod attach
324 * since there is some demod params needed to set according to tuner.
325 */
326
327 /* open demod I2C gate */
328 ret = rtl28xxu_ctrl_msg(adap->dev, &req_gate);
329 if (ret)
330 goto err;
331
332 /* check QT1010 ID(?) register; reg=0f val=2c */
333 ret = rtl28xxu_ctrl_msg(adap->dev, &req_qt1010);
334 if (ret == 0 && buf[0] == 0x2c) {
335 priv->tuner = TUNER_RTL2830_QT1010;
336 rtl2830_config = &rtl28xxu_rtl2830_qt1010_config;
337 deb_info("%s: QT1010\n", __func__);
338 goto found;
339 } else {
340 deb_info("%s: QT1010 probe failed=%d - %02x\n",
341 __func__, ret, buf[0]);
342 }
343
344 /* open demod I2C gate */
345 ret = rtl28xxu_ctrl_msg(adap->dev, &req_gate);
346 if (ret)
347 goto err;
348
349 /* check MT2060 ID register; reg=00 val=63 */
350 ret = rtl28xxu_ctrl_msg(adap->dev, &req_mt2060);
351 if (ret == 0 && buf[0] == 0x63) {
352 priv->tuner = TUNER_RTL2830_MT2060;
353 rtl2830_config = &rtl28xxu_rtl2830_mt2060_config;
354 deb_info("%s: MT2060\n", __func__);
355 goto found;
356 } else {
357 deb_info("%s: MT2060 probe failed=%d - %02x\n",
358 __func__, ret, buf[0]);
359 }
360
361 /* assume MXL5005S */
Antti Palosaarie9320ec2011-08-03 04:35:30 -0300362 ret = 0;
Antti Palosaari831e0b72011-07-08 23:36:07 -0300363 priv->tuner = TUNER_RTL2830_MXL5005S;
364 rtl2830_config = &rtl28xxu_rtl2830_mxl5005s_config;
365 deb_info("%s: MXL5005S\n", __func__);
366 goto found;
367
368found:
369 /* attach demodulator */
Antti Palosaarifba16b12012-01-21 20:28:38 -0300370 adap->fe_adap[0].fe = dvb_attach(rtl2830_attach, rtl2830_config,
Antti Palosaari831e0b72011-07-08 23:36:07 -0300371 &adap->dev->i2c_adap);
Antti Palosaarifba16b12012-01-21 20:28:38 -0300372 if (adap->fe_adap[0].fe == NULL) {
Antti Palosaari831e0b72011-07-08 23:36:07 -0300373 ret = -ENODEV;
374 goto err;
375 }
376
377 return ret;
378err:
379 deb_info("%s: failed=%d\n", __func__, ret);
380 return ret;
381}
382
Antti Palosaarib5cbaa42011-08-04 13:26:10 -0300383static int rtl2832u_frontend_attach(struct dvb_usb_adapter *adap)
384{
385 int ret;
386 struct rtl28xxu_priv *priv = adap->dev->priv;
387 u8 buf[1];
388 /* open RTL2832U/RTL2832 I2C gate */
389 struct rtl28xxu_req req_gate_open = {0x0120, 0x0011, 0x0001, "\x18"};
390 /* close RTL2832U/RTL2832 I2C gate */
391 struct rtl28xxu_req req_gate_close = {0x0120, 0x0011, 0x0001, "\x10"};
392 /* for FC2580 tuner probe */
393 struct rtl28xxu_req req_fc2580 = {0x01ac, CMD_I2C_RD, 1, buf};
394
395 deb_info("%s:\n", __func__);
396
397 /* GPIO direction */
398 ret = rtl2831_wr_reg(adap->dev, SYS_GPIO_DIR, 0x0a);
399 if (ret)
400 goto err;
401
402 /* enable as output GPIO0, GPIO2, GPIO4 */
403 ret = rtl2831_wr_reg(adap->dev, SYS_GPIO_OUT_EN, 0x15);
404 if (ret)
405 goto err;
406
407 ret = rtl2831_wr_reg(adap->dev, SYS_DEMOD_CTL, 0xe8);
408 if (ret)
409 goto err;
410
411 /*
412 * Probe used tuner. We need to know used tuner before demod attach
413 * since there is some demod params needed to set according to tuner.
414 */
415
416 /* open demod I2C gate */
417 ret = rtl28xxu_ctrl_msg(adap->dev, &req_gate_open);
418 if (ret)
419 goto err;
420
421 /* check FC2580 ID register; reg=01 val=56 */
422 ret = rtl28xxu_ctrl_msg(adap->dev, &req_fc2580);
423 if (ret == 0 && buf[0] == 0x56) {
424 priv->tuner = TUNER_RTL2832_FC2580;
425 deb_info("%s: FC2580\n", __func__);
426 goto found;
427 } else {
428 deb_info("%s: FC2580 probe failed=%d - %02x\n",
429 __func__, ret, buf[0]);
430 }
431
432 /* close demod I2C gate */
433 ret = rtl28xxu_ctrl_msg(adap->dev, &req_gate_close);
434 if (ret)
435 goto err;
436
437 /* tuner not found */
438 ret = -ENODEV;
439 goto err;
440
441found:
442 /* close demod I2C gate */
443 ret = rtl28xxu_ctrl_msg(adap->dev, &req_gate_close);
444 if (ret)
445 goto err;
446
447 /* attach demodulator */
448 /* TODO: */
449
450 return ret;
451err:
452 deb_info("%s: failed=%d\n", __func__, ret);
453 return ret;
454}
455
Antti Palosaari831e0b72011-07-08 23:36:07 -0300456static struct qt1010_config rtl28xxu_qt1010_config = {
457 .i2c_address = 0x62, /* 0xc4 */
458};
459
460static struct mt2060_config rtl28xxu_mt2060_config = {
461 .i2c_address = 0x60, /* 0xc0 */
462 .clock_out = 0,
463};
464
465static struct mxl5005s_config rtl28xxu_mxl5005s_config = {
466 .i2c_address = 0x63, /* 0xc6 */
467 .if_freq = IF_FREQ_4570000HZ,
468 .xtal_freq = CRYSTAL_FREQ_16000000HZ,
469 .agc_mode = MXL_SINGLE_AGC,
470 .tracking_filter = MXL_TF_C_H,
471 .rssi_enable = MXL_RSSI_ENABLE,
472 .cap_select = MXL_CAP_SEL_ENABLE,
473 .div_out = MXL_DIV_OUT_4,
474 .clock_out = MXL_CLOCK_OUT_DISABLE,
475 .output_load = MXL5005S_IF_OUTPUT_LOAD_200_OHM,
476 .top = MXL5005S_TOP_25P2,
477 .mod_mode = MXL_DIGITAL_MODE,
478 .if_mode = MXL_ZERO_IF,
479 .AgcMasterByte = 0x00,
480};
481
Antti Palosaarib5cbaa42011-08-04 13:26:10 -0300482static int rtl2831u_tuner_attach(struct dvb_usb_adapter *adap)
Antti Palosaari831e0b72011-07-08 23:36:07 -0300483{
484 int ret;
485 struct rtl28xxu_priv *priv = adap->dev->priv;
486 struct i2c_adapter *rtl2830_tuner_i2c;
487 struct dvb_frontend *fe = NULL;
488
489 deb_info("%s:\n", __func__);
490
491 /* use rtl2830 driver I2C adapter, for more info see rtl2830 driver */
Antti Palosaarifba16b12012-01-21 20:28:38 -0300492 rtl2830_tuner_i2c = rtl2830_get_tuner_i2c_adapter(adap->fe_adap[0].fe);
Antti Palosaari831e0b72011-07-08 23:36:07 -0300493
494 switch (priv->tuner) {
495 case TUNER_RTL2830_QT1010:
Antti Palosaarifba16b12012-01-21 20:28:38 -0300496 fe = dvb_attach(qt1010_attach, adap->fe_adap[0].fe,
497 rtl2830_tuner_i2c, &rtl28xxu_qt1010_config);
Antti Palosaari831e0b72011-07-08 23:36:07 -0300498 break;
499 case TUNER_RTL2830_MT2060:
Antti Palosaarifba16b12012-01-21 20:28:38 -0300500 fe = dvb_attach(mt2060_attach, adap->fe_adap[0].fe,
501 rtl2830_tuner_i2c, &rtl28xxu_mt2060_config,
502 1220);
Antti Palosaari831e0b72011-07-08 23:36:07 -0300503 break;
504 case TUNER_RTL2830_MXL5005S:
Antti Palosaarifba16b12012-01-21 20:28:38 -0300505 fe = dvb_attach(mxl5005s_attach, adap->fe_adap[0].fe,
506 rtl2830_tuner_i2c, &rtl28xxu_mxl5005s_config);
Antti Palosaari831e0b72011-07-08 23:36:07 -0300507 break;
508 default:
509 err("unknown tuner=%d", priv->tuner);
510 }
511
512 if (fe == NULL) {
513 ret = -ENODEV;
514 goto err;
515 }
516
517 return 0;
518err:
519 deb_info("%s: failed=%d\n", __func__, ret);
520 return ret;
521}
522
Antti Palosaarib5cbaa42011-08-04 13:26:10 -0300523static int rtl2832u_tuner_attach(struct dvb_usb_adapter *adap)
524{
525 int ret;
526 struct rtl28xxu_priv *priv = adap->dev->priv;
527 struct dvb_frontend *fe = NULL;
528
529 deb_info("%s:\n", __func__);
530
531 switch (priv->tuner) {
532 case TUNER_RTL2832_FC2580:
533 /* TODO: */
534 break;
535 default:
536 err("unknown tuner=%d", priv->tuner);
537 }
538
539 if (fe == NULL) {
540 ret = -ENODEV;
541 goto err;
542 }
543
544 return 0;
545err:
546 deb_info("%s: failed=%d\n", __func__, ret);
547 return ret;
548}
549
Antti Palosaari831e0b72011-07-08 23:36:07 -0300550static int rtl28xxu_streaming_ctrl(struct dvb_usb_adapter *adap , int onoff)
551{
552 int ret;
553 u8 buf[2], gpio;
554
555 deb_info("%s: onoff=%d\n", __func__, onoff);
556
557 ret = rtl2831_rd_reg(adap->dev, SYS_GPIO_OUT_VAL, &gpio);
558 if (ret)
559 goto err;
560
561 if (onoff) {
562 buf[0] = 0x00;
563 buf[1] = 0x00;
564 gpio |= 0x04; /* LED on */
565 } else {
566 buf[0] = 0x10; /* stall EPA */
567 buf[1] = 0x02; /* reset EPA */
568 gpio &= (~0x04); /* LED off */
569 }
570
571 ret = rtl2831_wr_reg(adap->dev, SYS_GPIO_OUT_VAL, gpio);
572 if (ret)
573 goto err;
574
575 ret = rtl2831_wr_regs(adap->dev, USB_EPA_CTL, buf, 2);
576 if (ret)
577 goto err;
578
579 return ret;
580err:
581 deb_info("%s: failed=%d\n", __func__, ret);
582 return ret;
583}
584
585static int rtl28xxu_power_ctrl(struct dvb_usb_device *d, int onoff)
586{
587 int ret;
588 u8 gpio, sys0;
589
590 deb_info("%s: onoff=%d\n", __func__, onoff);
591
592 /* demod adc */
593 ret = rtl2831_rd_reg(d, SYS_SYS0, &sys0);
594 if (ret)
595 goto err;
596
597 /* tuner power, read GPIOs */
598 ret = rtl2831_rd_reg(d, SYS_GPIO_OUT_VAL, &gpio);
599 if (ret)
600 goto err;
601
602 deb_info("%s: RD SYS0=%02x GPIO_OUT_VAL=%02x\n", __func__, sys0, gpio);
603
604 if (onoff) {
605 gpio |= 0x01; /* GPIO0 = 1 */
606 gpio &= (~0x10); /* GPIO4 = 0 */
607 sys0 = sys0 & 0x0f;
608 sys0 |= 0xe0;
609 } else {
Antti Palosaari831e0b72011-07-08 23:36:07 -0300610 /*
611 * FIXME: Use .fe_ioctl_override() to prevent demod
Antti Palosaarib5cbaa42011-08-04 13:26:10 -0300612 * IOCTLs in case of device is powered off. Or change
613 * RTL2830 demod not perform requestesd IOCTL & IO when sleep.
Antti Palosaari831e0b72011-07-08 23:36:07 -0300614 */
615 gpio &= (~0x01); /* GPIO0 = 0 */
616 gpio |= 0x10; /* GPIO4 = 1 */
617 sys0 = sys0 & (~0xc0);
Antti Palosaari831e0b72011-07-08 23:36:07 -0300618 }
619
620 deb_info("%s: WR SYS0=%02x GPIO_OUT_VAL=%02x\n", __func__, sys0, gpio);
621
622 /* demod adc */
623 ret = rtl2831_wr_reg(d, SYS_SYS0, sys0);
624 if (ret)
625 goto err;
626
627 /* tuner power, write GPIOs */
628 ret = rtl2831_wr_reg(d, SYS_GPIO_OUT_VAL, gpio);
629 if (ret)
630 goto err;
631
632 return ret;
633err:
634 deb_info("%s: failed=%d\n", __func__, ret);
635 return ret;
636}
637
Antti Palosaarib5cbaa42011-08-04 13:26:10 -0300638static int rtl2831u_rc_query(struct dvb_usb_device *d)
Antti Palosaari831e0b72011-07-08 23:36:07 -0300639{
Antti Palosaarib5cbaa42011-08-04 13:26:10 -0300640 int ret, i;
641 struct rtl28xxu_priv *priv = d->priv;
Antti Palosaari831e0b72011-07-08 23:36:07 -0300642 u8 buf[5];
643 u32 rc_code;
Antti Palosaarib5cbaa42011-08-04 13:26:10 -0300644 struct rtl28xxu_reg_val rc_nec_tab[] = {
645 { 0x3033, 0x80 },
646 { 0x3020, 0x43 },
647 { 0x3021, 0x16 },
648 { 0x3022, 0x16 },
649 { 0x3023, 0x5a },
650 { 0x3024, 0x2d },
651 { 0x3025, 0x16 },
652 { 0x3026, 0x01 },
653 { 0x3028, 0xb0 },
654 { 0x3029, 0x04 },
655 { 0x302c, 0x88 },
656 { 0x302e, 0x13 },
657 { 0x3030, 0xdf },
658 { 0x3031, 0x05 },
659 };
660
661 /* init remote controller */
662 if (!priv->rc_active) {
663 for (i = 0; i < ARRAY_SIZE(rc_nec_tab); i++) {
664 ret = rtl2831_wr_reg(d, rc_nec_tab[i].reg,
665 rc_nec_tab[i].val);
666 if (ret)
667 goto err;
668 }
669 priv->rc_active = true;
670 }
Antti Palosaari831e0b72011-07-08 23:36:07 -0300671
672 ret = rtl2831_rd_regs(d, SYS_IRRC_RP, buf, 5);
673 if (ret)
674 goto err;
675
676 if (buf[4] & 0x01) {
677 if (buf[2] == (u8) ~buf[3]) {
678 if (buf[0] == (u8) ~buf[1]) {
679 /* NEC standard (16 bit) */
680 rc_code = buf[0] << 8 | buf[2];
681 } else {
682 /* NEC extended (24 bit) */
683 rc_code = buf[0] << 16 |
684 buf[1] << 8 | buf[2];
685 }
686 } else {
687 /* NEC full (32 bit) */
688 rc_code = buf[0] << 24 | buf[1] << 16 |
689 buf[2] << 8 | buf[3];
690 }
691
692 rc_keydown(d->rc_dev, rc_code, 0);
693
694 ret = rtl2831_wr_reg(d, SYS_IRRC_SR, 1);
695 if (ret)
696 goto err;
697
698 /* repeated intentionally to avoid extra keypress */
699 ret = rtl2831_wr_reg(d, SYS_IRRC_SR, 1);
700 if (ret)
701 goto err;
702 }
703
704 return ret;
705err:
706 deb_info("%s: failed=%d\n", __func__, ret);
707 return ret;
708}
709
Antti Palosaarib5cbaa42011-08-04 13:26:10 -0300710
711static int rtl2832u_rc_query(struct dvb_usb_device *d)
712{
713 int ret, i;
714 struct rtl28xxu_priv *priv = d->priv;
715 u8 buf[128];
716 int len;
717 struct rtl28xxu_reg_val rc_nec_tab[] = {
718 {IR_RX_CTRL, 0x20},
719 {IR_RX_BUF_CTRL, 0x80},
720 {IR_RX_IF, 0xff},
721 {IR_RX_IE, 0xff},
722 {IR_MAX_DURATION0, 0xd0},
723 {IR_MAX_DURATION1, 0x07},
724 {IR_IDLE_LEN0, 0xc0},
725 {IR_IDLE_LEN1, 0x00},
726 {IR_GLITCH_LEN, 0x03},
727 {IR_RX_CLK, 0x09},
728 {IR_RX_CFG, 0x1c},
729 {IR_MAX_H_TOL_LEN, 0x1e},
730 {IR_MAX_L_TOL_LEN, 0x1e},
731 {IR_RX_CTRL, 0x80},
732 };
733
734 /* init remote controller */
735 if (!priv->rc_active) {
736 for (i = 0; i < ARRAY_SIZE(rc_nec_tab); i++) {
737 ret = rtl2831_wr_reg(d, rc_nec_tab[i].reg,
738 rc_nec_tab[i].val);
739 if (ret)
740 goto err;
741 }
742 priv->rc_active = true;
743 }
744
745 ret = rtl2831_rd_reg(d, IR_RX_IF, &buf[0]);
746 if (ret)
747 goto err;
748
749 if (buf[0] != 0x83)
750 goto exit;
751
752 ret = rtl2831_rd_reg(d, IR_RX_BC, &buf[0]);
753 if (ret)
754 goto err;
755
756 len = buf[0];
757 ret = rtl2831_rd_regs(d, IR_RX_BUF, buf, len);
758
759 /* TODO: pass raw IR to Kernel IR decoder */
760
761 ret = rtl2831_wr_reg(d, IR_RX_IF, 0x03);
762 ret = rtl2831_wr_reg(d, IR_RX_BUF_CTRL, 0x80);
763 ret = rtl2831_wr_reg(d, IR_RX_CTRL, 0x80);
764
765exit:
766 return ret;
767err:
768 deb_info("%s: failed=%d\n", __func__, ret);
769 return ret;
770}
771
772
Antti Palosaari831e0b72011-07-08 23:36:07 -0300773/* DVB USB Driver stuff */
774#define USB_VID_REALTEK 0x0bda
Antti Palosaarib5cbaa42011-08-04 13:26:10 -0300775#define USB_VID_DEXATEK 0x1D19
Antti Palosaari831e0b72011-07-08 23:36:07 -0300776#define USB_PID_RTL2831U 0x2831
Antti Palosaarib5cbaa42011-08-04 13:26:10 -0300777#define USB_PID_RTL2832U 0x2832
Antti Palosaari831e0b72011-07-08 23:36:07 -0300778#define USB_PID_FREECOM 0x0160
Antti Palosaarib5cbaa42011-08-04 13:26:10 -0300779#define USB_PID_DEXATEK_1101 0x1101
Antti Palosaari831e0b72011-07-08 23:36:07 -0300780
781#define RTL2831U_0BDA_2831 0
782#define RTL2831U_14AA_0160 1
783
Antti Palosaarib5cbaa42011-08-04 13:26:10 -0300784#define RTL2832U_1ST_ID (RTL2831U_14AA_0160 + 1)
785
786#define RTL2832U_0BDA_2832 (0 + RTL2832U_1ST_ID)
787#define RTL2832U_1D19_1101 (1 + RTL2832U_1ST_ID)
788
789
Antti Palosaari831e0b72011-07-08 23:36:07 -0300790static struct usb_device_id rtl28xxu_table[] = {
791 [RTL2831U_0BDA_2831] = {
792 USB_DEVICE(USB_VID_REALTEK, USB_PID_RTL2831U)},
793 [RTL2831U_14AA_0160] = {
794 USB_DEVICE(USB_VID_WIDEVIEW, USB_PID_FREECOM)},
Antti Palosaarib5cbaa42011-08-04 13:26:10 -0300795
796 [RTL2832U_0BDA_2832] = {
797 USB_DEVICE(USB_VID_REALTEK, USB_PID_RTL2832U)},
798 [RTL2832U_1D19_1101] = {
799 USB_DEVICE(USB_VID_DEXATEK, USB_PID_DEXATEK_1101)},
800
Antti Palosaari831e0b72011-07-08 23:36:07 -0300801 {} /* terminating entry */
802};
803
804MODULE_DEVICE_TABLE(usb, rtl28xxu_table);
805
806static struct dvb_usb_device_properties rtl28xxu_properties[] = {
807 {
808 .caps = DVB_USB_IS_AN_I2C_ADAPTER,
809
810 .usb_ctrl = DEVICE_SPECIFIC,
811 .no_reconnect = 1,
812
813 .size_of_priv = sizeof(struct rtl28xxu_priv),
814
815 .num_adapters = 1,
816 .adapter = {
817 {
Antti Palosaarifba16b12012-01-21 20:28:38 -0300818 .num_frontends = 1,
819 .fe = {
820 {
821 .frontend_attach = rtl2831u_frontend_attach,
822 .tuner_attach = rtl2831u_tuner_attach,
823 .streaming_ctrl = rtl28xxu_streaming_ctrl,
824 .stream = {
825 .type = USB_BULK,
826 .count = 6,
827 .endpoint = 0x81,
828 .u = {
829 .bulk = {
830 .buffersize = 4096,
831 }
832 }
Antti Palosaari831e0b72011-07-08 23:36:07 -0300833 }
834 }
Antti Palosaarifba16b12012-01-21 20:28:38 -0300835 }
Antti Palosaari831e0b72011-07-08 23:36:07 -0300836 }
837 },
838
839 .power_ctrl = rtl28xxu_power_ctrl,
840
841 .rc.core = {
842 .protocol = RC_TYPE_NEC,
843 .module_name = "rtl28xxu",
Antti Palosaarib5cbaa42011-08-04 13:26:10 -0300844 .rc_query = rtl2831u_rc_query,
Antti Palosaari831e0b72011-07-08 23:36:07 -0300845 .rc_interval = 400,
846 .allowed_protos = RC_TYPE_NEC,
847 .rc_codes = RC_MAP_EMPTY,
848 },
849
850 .i2c_algo = &rtl28xxu_i2c_algo,
851
852 .num_device_descs = 2,
853 .devices = {
854 {
855 .name = "Realtek RTL2831U reference design",
856 .cold_ids = {NULL},
857 .warm_ids = {
858 &rtl28xxu_table[RTL2831U_0BDA_2831], NULL},
859 },
860 {
861 .name = "Freecom USB2.0 DVB-T",
862 .cold_ids = {NULL},
863 .warm_ids = {
864 &rtl28xxu_table[RTL2831U_14AA_0160], NULL},
865 },
866 }
867 },
Antti Palosaarib5cbaa42011-08-04 13:26:10 -0300868 {
869 .caps = DVB_USB_IS_AN_I2C_ADAPTER,
870
871 .usb_ctrl = DEVICE_SPECIFIC,
872 .no_reconnect = 1,
873
874 .size_of_priv = sizeof(struct rtl28xxu_priv),
875
876 .num_adapters = 1,
877 .adapter = {
878 {
Antti Palosaarifba16b12012-01-21 20:28:38 -0300879 .num_frontends = 1,
880 .fe = {
881 {
882 .frontend_attach = rtl2832u_frontend_attach,
883 .tuner_attach = rtl2832u_tuner_attach,
884 .streaming_ctrl = rtl28xxu_streaming_ctrl,
885 .stream = {
886 .type = USB_BULK,
887 .count = 6,
888 .endpoint = 0x81,
889 .u = {
890 .bulk = {
891 .buffersize = 4096,
892 }
893 }
Antti Palosaarib5cbaa42011-08-04 13:26:10 -0300894 }
895 }
Antti Palosaarifba16b12012-01-21 20:28:38 -0300896 }
Antti Palosaarib5cbaa42011-08-04 13:26:10 -0300897 }
898 },
899
900 .power_ctrl = rtl28xxu_power_ctrl,
901
902 .rc.core = {
903 .protocol = RC_TYPE_NEC,
904 .module_name = "rtl28xxu",
905 .rc_query = rtl2832u_rc_query,
906 .rc_interval = 400,
907 .allowed_protos = RC_TYPE_NEC,
908 .rc_codes = RC_MAP_EMPTY,
909 },
910
911 .i2c_algo = &rtl28xxu_i2c_algo,
912
913 .num_device_descs = 2,
914 .devices = {
915 {
916 .name = "Realtek RTL2832U reference design",
917 .cold_ids = {NULL},
918 .warm_ids = {
919 &rtl28xxu_table[RTL2832U_0BDA_2832], NULL},
920 },
921 {
922 .name = "Dexatek dongle",
923 .cold_ids = {NULL},
924 .warm_ids = {
925 &rtl28xxu_table[RTL2832U_1D19_1101], NULL},
926 },
927 }
928 },
929
Antti Palosaari831e0b72011-07-08 23:36:07 -0300930};
931
932static int rtl28xxu_probe(struct usb_interface *intf,
933 const struct usb_device_id *id)
934{
935 int ret, i;
936 int properties_count = ARRAY_SIZE(rtl28xxu_properties);
937 struct dvb_usb_device *d = NULL;
Antti Palosaari831e0b72011-07-08 23:36:07 -0300938
939 deb_info("%s: interface=%d\n", __func__,
940 intf->cur_altsetting->desc.bInterfaceNumber);
941
942 if (intf->cur_altsetting->desc.bInterfaceNumber != 0)
943 return 0;
944
945 for (i = 0; i < properties_count; i++) {
946 ret = dvb_usb_device_init(intf, &rtl28xxu_properties[i],
947 THIS_MODULE, &d, adapter_nr);
948 if (ret == 0 || ret != -ENODEV)
949 break;
950 }
951
952 if (ret)
953 goto err;
954
955 /* init USB endpoints */
956 ret = rtl2831_wr_reg(d, USB_SYSCTL_0, 0x09);
957 if (ret)
958 goto err;
959
960 ret = rtl2831_wr_regs(d, USB_EPA_MAXPKT, "\x00\x02\x00\x00", 4);
961 if (ret)
962 goto err;
963
964 ret = rtl2831_wr_regs(d, USB_EPA_FIFO_CFG, "\x14\x00\x00\x00", 4);
965 if (ret)
966 goto err;
967
Antti Palosaari831e0b72011-07-08 23:36:07 -0300968 return ret;
969err:
970 deb_info("%s: failed=%d\n", __func__, ret);
971 return ret;
972}
973
974static struct usb_driver rtl28xxu_driver = {
975 .name = "dvb_usb_rtl28xxu",
976 .probe = rtl28xxu_probe,
977 .disconnect = dvb_usb_device_exit,
978 .id_table = rtl28xxu_table,
979};
980
981/* module stuff */
982static int __init rtl28xxu_module_init(void)
983{
984 int ret;
985 deb_info("%s:\n", __func__);
986 ret = usb_register(&rtl28xxu_driver);
987 if (ret)
988 err("usb_register failed=%d", ret);
989
990 return ret;
991}
992
993static void __exit rtl28xxu_module_exit(void)
994{
995 deb_info("%s:\n", __func__);
996 /* deregister this driver from the USB subsystem */
997 usb_deregister(&rtl28xxu_driver);
998}
999
1000module_init(rtl28xxu_module_init);
1001module_exit(rtl28xxu_module_exit);
1002
1003MODULE_DESCRIPTION("Realtek RTL28xxU DVB USB driver");
1004MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
1005MODULE_LICENSE("GPL");