blob: a676e445284799b30b20980abfee8de02675617e [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>
Thomas Mair5cf66312012-05-18 14:47:41 -03006 * Copyright (C) 2012 Thomas Mair <thomas.mair86@googlemail.com>
Antti Palosaari831e0b72011-07-08 23:36:07 -03007 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 */
22
23#include "rtl28xxu.h"
24
25#include "rtl2830.h"
Thomas Mair5cf66312012-05-18 14:47:41 -030026#include "rtl2832.h"
Antti Palosaari831e0b72011-07-08 23:36:07 -030027
28#include "qt1010.h"
29#include "mt2060.h"
30#include "mxl5005s.h"
Thomas Mair5cf66312012-05-18 14:47:41 -030031#include "fc0012.h"
32#include "fc0013.h"
Antti Palosaari542f6a52012-09-01 21:09:22 -030033#include "e4000.h"
Antti Palosaari384df492012-09-08 22:07:25 -030034#include "fc2580.h"
Antti Palosaari5be65722012-09-11 22:27:09 -030035#include "tua9001.h"
Mauro Carvalho Chehab6889ab22013-04-06 09:40:11 -030036#include "r820t.h"
Antti Palosaari831e0b72011-07-08 23:36:07 -030037
Antti Palosaari1fd10f92014-04-09 12:42:35 -030038/*
39 * RTL2832_SDR module is in staging. That logic is added in order to avoid any
40 * hard dependency to drivers/staging/ directory as we want compile mainline
41 * driver even whole staging directory is missing.
42 */
43#include <media/v4l2-subdev.h>
44
45#if IS_ENABLED(CONFIG_DVB_RTL2832_SDR)
46struct dvb_frontend *rtl2832_sdr_attach(struct dvb_frontend *fe,
47 struct i2c_adapter *i2c, const struct rtl2832_config *cfg,
48 struct v4l2_subdev *sd);
49#else
50static inline struct dvb_frontend *rtl2832_sdr_attach(struct dvb_frontend *fe,
51 struct i2c_adapter *i2c, const struct rtl2832_config *cfg,
52 struct v4l2_subdev *sd)
53{
54 return NULL;
55}
56#endif
57
Antti Palosaaribeab1b52014-04-09 18:08:37 -030058#ifdef CONFIG_MEDIA_ATTACH
59#define dvb_attach_sdr(FUNCTION, ARGS...) ({ \
60 void *__r = NULL; \
61 typeof(&FUNCTION) __a = symbol_request(FUNCTION); \
62 if (__a) { \
63 __r = (void *) __a(ARGS); \
64 if (__r == NULL) \
65 symbol_put(FUNCTION); \
66 } \
67 __r; \
68})
69
70#else
71#define dvb_attach_sdr(FUNCTION, ARGS...) ({ \
72 FUNCTION(ARGS); \
73})
74
75#endif
76
Antti Palosaari94b5fa62013-11-22 14:20:14 -030077static int rtl28xxu_disable_rc;
78module_param_named(disable_rc, rtl28xxu_disable_rc, int, 0644);
79MODULE_PARM_DESC(disable_rc, "disable RTL2832U remote controller");
Antti Palosaari831e0b72011-07-08 23:36:07 -030080DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
81
82static int rtl28xxu_ctrl_msg(struct dvb_usb_device *d, struct rtl28xxu_req *req)
83{
84 int ret;
85 unsigned int pipe;
86 u8 requesttype;
87 u8 *buf;
88
89 buf = kmalloc(req->size, GFP_KERNEL);
90 if (!buf) {
91 ret = -ENOMEM;
92 goto err;
93 }
94
95 if (req->index & CMD_WR_FLAG) {
96 /* write */
97 memcpy(buf, req->data, req->size);
98 requesttype = (USB_TYPE_VENDOR | USB_DIR_OUT);
99 pipe = usb_sndctrlpipe(d->udev, 0);
100 } else {
101 /* read */
102 requesttype = (USB_TYPE_VENDOR | USB_DIR_IN);
103 pipe = usb_rcvctrlpipe(d->udev, 0);
104 }
105
106 ret = usb_control_msg(d->udev, pipe, 0, requesttype, req->value,
Antti Palosaari9935eea2012-01-21 22:26:52 -0300107 req->index, buf, req->size, 1000);
Antti Palosaarid89b9362012-08-22 19:42:00 -0300108
109 dvb_usb_dbg_usb_control_msg(d->udev, 0, requesttype, req->value,
110 req->index, buf, req->size);
111
Antti Palosaari9935eea2012-01-21 22:26:52 -0300112 if (ret > 0)
113 ret = 0;
Antti Palosaari831e0b72011-07-08 23:36:07 -0300114
Antti Palosaari831e0b72011-07-08 23:36:07 -0300115 /* read request, copy returned data to return buf */
116 if (!ret && requesttype == (USB_TYPE_VENDOR | USB_DIR_IN))
117 memcpy(req->data, buf, req->size);
118
119 kfree(buf);
Antti Palosaari831e0b72011-07-08 23:36:07 -0300120
Antti Palosaari9935eea2012-01-21 22:26:52 -0300121 if (ret)
122 goto err;
123
124 return ret;
Antti Palosaari831e0b72011-07-08 23:36:07 -0300125err:
Antti Palosaaric01a3592012-08-12 22:27:07 -0300126 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
Antti Palosaari831e0b72011-07-08 23:36:07 -0300127 return ret;
128}
129
Thomas Mair40a2d3c2012-05-18 14:47:42 -0300130static int rtl28xx_wr_regs(struct dvb_usb_device *d, u16 reg, u8 *val, int len)
Antti Palosaari831e0b72011-07-08 23:36:07 -0300131{
132 struct rtl28xxu_req req;
133
134 if (reg < 0x3000)
135 req.index = CMD_USB_WR;
Antti Palosaarib5cbaa42011-08-04 13:26:10 -0300136 else if (reg < 0x4000)
Antti Palosaari831e0b72011-07-08 23:36:07 -0300137 req.index = CMD_SYS_WR;
Antti Palosaarib5cbaa42011-08-04 13:26:10 -0300138 else
139 req.index = CMD_IR_WR;
Antti Palosaari831e0b72011-07-08 23:36:07 -0300140
141 req.value = reg;
142 req.size = len;
143 req.data = val;
144
145 return rtl28xxu_ctrl_msg(d, &req);
146}
147
148static int rtl2831_rd_regs(struct dvb_usb_device *d, u16 reg, u8 *val, int len)
149{
150 struct rtl28xxu_req req;
151
152 if (reg < 0x3000)
153 req.index = CMD_USB_RD;
Antti Palosaarib5cbaa42011-08-04 13:26:10 -0300154 else if (reg < 0x4000)
Antti Palosaari831e0b72011-07-08 23:36:07 -0300155 req.index = CMD_SYS_RD;
Antti Palosaarib5cbaa42011-08-04 13:26:10 -0300156 else
157 req.index = CMD_IR_RD;
Antti Palosaari831e0b72011-07-08 23:36:07 -0300158
159 req.value = reg;
160 req.size = len;
161 req.data = val;
162
163 return rtl28xxu_ctrl_msg(d, &req);
164}
165
Thomas Mair40a2d3c2012-05-18 14:47:42 -0300166static int rtl28xx_wr_reg(struct dvb_usb_device *d, u16 reg, u8 val)
Antti Palosaari831e0b72011-07-08 23:36:07 -0300167{
Thomas Mair40a2d3c2012-05-18 14:47:42 -0300168 return rtl28xx_wr_regs(d, reg, &val, 1);
Antti Palosaari831e0b72011-07-08 23:36:07 -0300169}
170
Thomas Mair40a2d3c2012-05-18 14:47:42 -0300171static int rtl28xx_rd_reg(struct dvb_usb_device *d, u16 reg, u8 *val)
Antti Palosaari831e0b72011-07-08 23:36:07 -0300172{
173 return rtl2831_rd_regs(d, reg, val, 1);
174}
175
Antti Palosaari4005c1a2012-09-17 15:43:57 -0300176static int rtl28xx_wr_reg_mask(struct dvb_usb_device *d, u16 reg, u8 val,
177 u8 mask)
178{
179 int ret;
180 u8 tmp;
181
182 /* no need for read if whole reg is written */
183 if (mask != 0xff) {
184 ret = rtl28xx_rd_reg(d, reg, &tmp);
185 if (ret)
186 return ret;
187
188 val &= mask;
189 tmp &= ~mask;
190 val |= tmp;
191 }
192
193 return rtl28xx_wr_reg(d, reg, val);
194}
195
Antti Palosaari831e0b72011-07-08 23:36:07 -0300196/* I2C */
197static int rtl28xxu_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg msg[],
198 int num)
199{
200 int ret;
201 struct dvb_usb_device *d = i2c_get_adapdata(adap);
Antti Palosaari34ec2932011-08-04 20:21:16 -0300202 struct rtl28xxu_priv *priv = d->priv;
Antti Palosaari831e0b72011-07-08 23:36:07 -0300203 struct rtl28xxu_req req;
204
205 /*
206 * It is not known which are real I2C bus xfer limits, but testing
207 * with RTL2831U + MT2060 gives max RD 24 and max WR 22 bytes.
Antti Palosaari34ec2932011-08-04 20:21:16 -0300208 * TODO: find out RTL2832U lens
209 */
210
211 /*
212 * I2C adapter logic looks rather complicated due to fact it handles
213 * three different access methods. Those methods are;
214 * 1) integrated demod access
215 * 2) old I2C access
216 * 3) new I2C access
217 *
218 * Used method is selected in order 1, 2, 3. Method 3 can handle all
219 * requests but there is two reasons why not use it always;
220 * 1) It is most expensive, usually two USB messages are needed
221 * 2) At least RTL2831U does not support it
222 *
223 * Method 3 is needed in case of I2C write+read (typical register read)
224 * where write is more than one byte.
Antti Palosaari831e0b72011-07-08 23:36:07 -0300225 */
226
227 if (mutex_lock_interruptible(&d->i2c_mutex) < 0)
228 return -EAGAIN;
229
230 if (num == 2 && !(msg[0].flags & I2C_M_RD) &&
231 (msg[1].flags & I2C_M_RD)) {
Antti Palosaari34ec2932011-08-04 20:21:16 -0300232 if (msg[0].len > 24 || msg[1].len > 24) {
233 /* TODO: check msg[0].len max */
Antti Palosaari831e0b72011-07-08 23:36:07 -0300234 ret = -EOPNOTSUPP;
Antti Palosaari9935eea2012-01-21 22:26:52 -0300235 goto err_mutex_unlock;
Antti Palosaari34ec2932011-08-04 20:21:16 -0300236 } else if (msg[0].addr == 0x10) {
237 /* method 1 - integrated demod */
238 req.value = (msg[0].buf[0] << 8) | (msg[0].addr << 1);
239 req.index = CMD_DEMOD_RD | priv->page;
Antti Palosaari831e0b72011-07-08 23:36:07 -0300240 req.size = msg[1].len;
241 req.data = &msg[1].buf[0];
242 ret = rtl28xxu_ctrl_msg(d, &req);
Antti Palosaari34ec2932011-08-04 20:21:16 -0300243 } else if (msg[0].len < 2) {
244 /* method 2 - old I2C */
Antti Palosaari831e0b72011-07-08 23:36:07 -0300245 req.value = (msg[0].buf[0] << 8) | (msg[0].addr << 1);
246 req.index = CMD_I2C_RD;
247 req.size = msg[1].len;
248 req.data = &msg[1].buf[0];
249 ret = rtl28xxu_ctrl_msg(d, &req);
Antti Palosaari34ec2932011-08-04 20:21:16 -0300250 } else {
251 /* method 3 - new I2C */
252 req.value = (msg[0].addr << 1);
253 req.index = CMD_I2C_DA_WR;
254 req.size = msg[0].len;
255 req.data = msg[0].buf;
256 ret = rtl28xxu_ctrl_msg(d, &req);
257 if (ret)
Antti Palosaari9935eea2012-01-21 22:26:52 -0300258 goto err_mutex_unlock;
Antti Palosaari34ec2932011-08-04 20:21:16 -0300259
260 req.value = (msg[0].addr << 1);
261 req.index = CMD_I2C_DA_RD;
262 req.size = msg[1].len;
263 req.data = msg[1].buf;
264 ret = rtl28xxu_ctrl_msg(d, &req);
Antti Palosaari831e0b72011-07-08 23:36:07 -0300265 }
266 } else if (num == 1 && !(msg[0].flags & I2C_M_RD)) {
267 if (msg[0].len > 22) {
Antti Palosaari34ec2932011-08-04 20:21:16 -0300268 /* TODO: check msg[0].len max */
Antti Palosaari831e0b72011-07-08 23:36:07 -0300269 ret = -EOPNOTSUPP;
Antti Palosaari9935eea2012-01-21 22:26:52 -0300270 goto err_mutex_unlock;
Antti Palosaari34ec2932011-08-04 20:21:16 -0300271 } else if (msg[0].addr == 0x10) {
272 /* method 1 - integrated demod */
273 if (msg[0].buf[0] == 0x00) {
274 /* save demod page for later demod access */
275 priv->page = msg[0].buf[1];
276 ret = 0;
277 } else {
278 req.value = (msg[0].buf[0] << 8) |
279 (msg[0].addr << 1);
280 req.index = CMD_DEMOD_WR | priv->page;
281 req.size = msg[0].len-1;
282 req.data = &msg[0].buf[1];
283 ret = rtl28xxu_ctrl_msg(d, &req);
284 }
285 } else if (msg[0].len < 23) {
286 /* method 2 - old I2C */
Antti Palosaari831e0b72011-07-08 23:36:07 -0300287 req.value = (msg[0].buf[0] << 8) | (msg[0].addr << 1);
288 req.index = CMD_I2C_WR;
289 req.size = msg[0].len-1;
290 req.data = &msg[0].buf[1];
291 ret = rtl28xxu_ctrl_msg(d, &req);
Antti Palosaari34ec2932011-08-04 20:21:16 -0300292 } else {
293 /* method 3 - new I2C */
294 req.value = (msg[0].addr << 1);
295 req.index = CMD_I2C_DA_WR;
296 req.size = msg[0].len;
297 req.data = msg[0].buf;
298 ret = rtl28xxu_ctrl_msg(d, &req);
Antti Palosaari831e0b72011-07-08 23:36:07 -0300299 }
300 } else {
301 ret = -EINVAL;
302 }
303
Antti Palosaari9935eea2012-01-21 22:26:52 -0300304err_mutex_unlock:
Antti Palosaari831e0b72011-07-08 23:36:07 -0300305 mutex_unlock(&d->i2c_mutex);
306
307 return ret ? ret : num;
308}
309
310static u32 rtl28xxu_i2c_func(struct i2c_adapter *adapter)
311{
312 return I2C_FUNC_I2C;
313}
314
315static struct i2c_algorithm rtl28xxu_i2c_algo = {
316 .master_xfer = rtl28xxu_i2c_xfer,
317 .functionality = rtl28xxu_i2c_func,
318};
319
Antti Palosaari81347f52012-09-17 16:50:36 -0300320static int rtl2831u_read_config(struct dvb_usb_device *d)
321{
322 struct rtl28xxu_priv *priv = d_to_priv(d);
323 int ret;
324 u8 buf[1];
325 /* open RTL2831U/RTL2830 I2C gate */
326 struct rtl28xxu_req req_gate_open = {0x0120, 0x0011, 0x0001, "\x08"};
327 /* tuner probes */
328 struct rtl28xxu_req req_mt2060 = {0x00c0, CMD_I2C_RD, 1, buf};
329 struct rtl28xxu_req req_qt1010 = {0x0fc4, CMD_I2C_RD, 1, buf};
330
331 dev_dbg(&d->udev->dev, "%s:\n", __func__);
332
333 /*
334 * RTL2831U GPIOs
335 * =========================================================
336 * GPIO0 | tuner#0 | 0 off | 1 on | MXL5005S (?)
337 * GPIO2 | LED | 0 off | 1 on |
338 * GPIO4 | tuner#1 | 0 on | 1 off | MT2060
339 */
340
341 /* GPIO direction */
342 ret = rtl28xx_wr_reg(d, SYS_GPIO_DIR, 0x0a);
343 if (ret)
344 goto err;
345
346 /* enable as output GPIO0, GPIO2, GPIO4 */
347 ret = rtl28xx_wr_reg(d, SYS_GPIO_OUT_EN, 0x15);
348 if (ret)
349 goto err;
350
351 /*
352 * Probe used tuner. We need to know used tuner before demod attach
353 * since there is some demod params needed to set according to tuner.
354 */
355
356 /* demod needs some time to wake up */
357 msleep(20);
358
359 priv->tuner_name = "NONE";
360
361 /* open demod I2C gate */
362 ret = rtl28xxu_ctrl_msg(d, &req_gate_open);
363 if (ret)
364 goto err;
365
366 /* check QT1010 ID(?) register; reg=0f val=2c */
367 ret = rtl28xxu_ctrl_msg(d, &req_qt1010);
368 if (ret == 0 && buf[0] == 0x2c) {
369 priv->tuner = TUNER_RTL2830_QT1010;
370 priv->tuner_name = "QT1010";
371 goto found;
372 }
373
374 /* open demod I2C gate */
375 ret = rtl28xxu_ctrl_msg(d, &req_gate_open);
376 if (ret)
377 goto err;
378
379 /* check MT2060 ID register; reg=00 val=63 */
380 ret = rtl28xxu_ctrl_msg(d, &req_mt2060);
381 if (ret == 0 && buf[0] == 0x63) {
382 priv->tuner = TUNER_RTL2830_MT2060;
383 priv->tuner_name = "MT2060";
384 goto found;
385 }
386
387 /* assume MXL5005S */
388 priv->tuner = TUNER_RTL2830_MXL5005S;
389 priv->tuner_name = "MXL5005S";
390 goto found;
391
392found:
393 dev_dbg(&d->udev->dev, "%s: tuner=%s\n", __func__, priv->tuner_name);
394
395 return 0;
396err:
397 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
398 return ret;
399}
400
Antti Palosaarief37be12012-09-17 14:01:35 -0300401static int rtl2832u_read_config(struct dvb_usb_device *d)
402{
403 struct rtl28xxu_priv *priv = d_to_priv(d);
404 int ret;
Antti Palosaari4005c1a2012-09-17 15:43:57 -0300405 u8 buf[2];
Antti Palosaarief37be12012-09-17 14:01:35 -0300406 /* open RTL2832U/RTL2832 I2C gate */
407 struct rtl28xxu_req req_gate_open = {0x0120, 0x0011, 0x0001, "\x18"};
408 /* close RTL2832U/RTL2832 I2C gate */
409 struct rtl28xxu_req req_gate_close = {0x0120, 0x0011, 0x0001, "\x10"};
410 /* tuner probes */
411 struct rtl28xxu_req req_fc0012 = {0x00c6, CMD_I2C_RD, 1, buf};
412 struct rtl28xxu_req req_fc0013 = {0x00c6, CMD_I2C_RD, 1, buf};
413 struct rtl28xxu_req req_mt2266 = {0x00c0, CMD_I2C_RD, 1, buf};
414 struct rtl28xxu_req req_fc2580 = {0x01ac, CMD_I2C_RD, 1, buf};
415 struct rtl28xxu_req req_mt2063 = {0x00c0, CMD_I2C_RD, 1, buf};
416 struct rtl28xxu_req req_max3543 = {0x00c0, CMD_I2C_RD, 1, buf};
417 struct rtl28xxu_req req_tua9001 = {0x7ec0, CMD_I2C_RD, 2, buf};
418 struct rtl28xxu_req req_mxl5007t = {0xd9c0, CMD_I2C_RD, 1, buf};
419 struct rtl28xxu_req req_e4000 = {0x02c8, CMD_I2C_RD, 1, buf};
420 struct rtl28xxu_req req_tda18272 = {0x00c0, CMD_I2C_RD, 2, buf};
Gianluca Gennaria908eb92013-06-02 17:24:24 -0300421 struct rtl28xxu_req req_r820t = {0x0034, CMD_I2C_RD, 1, buf};
Antti Palosaari8b4cac12013-10-30 00:45:38 -0300422 struct rtl28xxu_req req_r828d = {0x0074, CMD_I2C_RD, 1, buf};
Antti Palosaarief37be12012-09-17 14:01:35 -0300423
424 dev_dbg(&d->udev->dev, "%s:\n", __func__);
425
Antti Palosaari4005c1a2012-09-17 15:43:57 -0300426 /* enable GPIO3 and GPIO6 as output */
427 ret = rtl28xx_wr_reg_mask(d, SYS_GPIO_DIR, 0x00, 0x40);
Antti Palosaarief37be12012-09-17 14:01:35 -0300428 if (ret)
429 goto err;
430
Antti Palosaari4005c1a2012-09-17 15:43:57 -0300431 ret = rtl28xx_wr_reg_mask(d, SYS_GPIO_OUT_EN, 0x48, 0x48);
Antti Palosaarief37be12012-09-17 14:01:35 -0300432 if (ret)
433 goto err;
434
435 /*
436 * Probe used tuner. We need to know used tuner before demod attach
437 * since there is some demod params needed to set according to tuner.
438 */
439
440 /* open demod I2C gate */
441 ret = rtl28xxu_ctrl_msg(d, &req_gate_open);
442 if (ret)
443 goto err;
444
445 priv->tuner_name = "NONE";
446
447 /* check FC0012 ID register; reg=00 val=a1 */
448 ret = rtl28xxu_ctrl_msg(d, &req_fc0012);
449 if (ret == 0 && buf[0] == 0xa1) {
450 priv->tuner = TUNER_RTL2832_FC0012;
451 priv->tuner_name = "FC0012";
452 goto found;
453 }
454
455 /* check FC0013 ID register; reg=00 val=a3 */
456 ret = rtl28xxu_ctrl_msg(d, &req_fc0013);
457 if (ret == 0 && buf[0] == 0xa3) {
458 priv->tuner = TUNER_RTL2832_FC0013;
459 priv->tuner_name = "FC0013";
460 goto found;
461 }
462
463 /* check MT2266 ID register; reg=00 val=85 */
464 ret = rtl28xxu_ctrl_msg(d, &req_mt2266);
465 if (ret == 0 && buf[0] == 0x85) {
466 priv->tuner = TUNER_RTL2832_MT2266;
467 priv->tuner_name = "MT2266";
468 goto found;
469 }
470
471 /* check FC2580 ID register; reg=01 val=56 */
472 ret = rtl28xxu_ctrl_msg(d, &req_fc2580);
473 if (ret == 0 && buf[0] == 0x56) {
474 priv->tuner = TUNER_RTL2832_FC2580;
475 priv->tuner_name = "FC2580";
476 goto found;
477 }
478
479 /* check MT2063 ID register; reg=00 val=9e || 9c */
480 ret = rtl28xxu_ctrl_msg(d, &req_mt2063);
481 if (ret == 0 && (buf[0] == 0x9e || buf[0] == 0x9c)) {
482 priv->tuner = TUNER_RTL2832_MT2063;
483 priv->tuner_name = "MT2063";
484 goto found;
485 }
486
487 /* check MAX3543 ID register; reg=00 val=38 */
488 ret = rtl28xxu_ctrl_msg(d, &req_max3543);
489 if (ret == 0 && buf[0] == 0x38) {
490 priv->tuner = TUNER_RTL2832_MAX3543;
491 priv->tuner_name = "MAX3543";
492 goto found;
493 }
494
495 /* check TUA9001 ID register; reg=7e val=2328 */
496 ret = rtl28xxu_ctrl_msg(d, &req_tua9001);
497 if (ret == 0 && buf[0] == 0x23 && buf[1] == 0x28) {
498 priv->tuner = TUNER_RTL2832_TUA9001;
499 priv->tuner_name = "TUA9001";
500 goto found;
501 }
502
503 /* check MXL5007R ID register; reg=d9 val=14 */
504 ret = rtl28xxu_ctrl_msg(d, &req_mxl5007t);
505 if (ret == 0 && buf[0] == 0x14) {
506 priv->tuner = TUNER_RTL2832_MXL5007T;
507 priv->tuner_name = "MXL5007T";
508 goto found;
509 }
510
511 /* check E4000 ID register; reg=02 val=40 */
512 ret = rtl28xxu_ctrl_msg(d, &req_e4000);
513 if (ret == 0 && buf[0] == 0x40) {
514 priv->tuner = TUNER_RTL2832_E4000;
515 priv->tuner_name = "E4000";
516 goto found;
517 }
518
519 /* check TDA18272 ID register; reg=00 val=c760 */
520 ret = rtl28xxu_ctrl_msg(d, &req_tda18272);
521 if (ret == 0 && (buf[0] == 0xc7 || buf[1] == 0x60)) {
522 priv->tuner = TUNER_RTL2832_TDA18272;
523 priv->tuner_name = "TDA18272";
524 goto found;
525 }
526
Gianluca Gennaria908eb92013-06-02 17:24:24 -0300527 /* check R820T ID register; reg=00 val=69 */
Mauro Carvalho Chehab6889ab22013-04-06 09:40:11 -0300528 ret = rtl28xxu_ctrl_msg(d, &req_r820t);
Gianluca Gennaria908eb92013-06-02 17:24:24 -0300529 if (ret == 0 && buf[0] == 0x69) {
Mauro Carvalho Chehab6889ab22013-04-06 09:40:11 -0300530 priv->tuner = TUNER_RTL2832_R820T;
531 priv->tuner_name = "R820T";
532 goto found;
533 }
534
Antti Palosaari8b4cac12013-10-30 00:45:38 -0300535 /* check R828D ID register; reg=00 val=69 */
536 ret = rtl28xxu_ctrl_msg(d, &req_r828d);
537 if (ret == 0 && buf[0] == 0x69) {
538 priv->tuner = TUNER_RTL2832_R828D;
539 priv->tuner_name = "R828D";
540 goto found;
541 }
542
543
Antti Palosaarief37be12012-09-17 14:01:35 -0300544found:
545 dev_dbg(&d->udev->dev, "%s: tuner=%s\n", __func__, priv->tuner_name);
546
547 /* close demod I2C gate */
548 ret = rtl28xxu_ctrl_msg(d, &req_gate_close);
549 if (ret < 0)
550 goto err;
551
552 return 0;
553err:
554 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
555 return ret;
556}
557
Antti Palosaarie8b46682013-10-12 23:35:35 -0300558static const struct rtl2830_config rtl28xxu_rtl2830_mt2060_config = {
Antti Palosaari831e0b72011-07-08 23:36:07 -0300559 .i2c_addr = 0x10, /* 0x20 */
560 .xtal = 28800000,
561 .ts_mode = 0,
562 .spec_inv = 1,
Antti Palosaari831e0b72011-07-08 23:36:07 -0300563 .vtop = 0x20,
564 .krf = 0x04,
565 .agc_targ_val = 0x2d,
566
567};
568
Antti Palosaarie8b46682013-10-12 23:35:35 -0300569static const struct rtl2830_config rtl28xxu_rtl2830_qt1010_config = {
Antti Palosaari831e0b72011-07-08 23:36:07 -0300570 .i2c_addr = 0x10, /* 0x20 */
571 .xtal = 28800000,
572 .ts_mode = 0,
573 .spec_inv = 1,
Antti Palosaari831e0b72011-07-08 23:36:07 -0300574 .vtop = 0x20,
575 .krf = 0x04,
576 .agc_targ_val = 0x2d,
577};
578
Antti Palosaarie8b46682013-10-12 23:35:35 -0300579static const struct rtl2830_config rtl28xxu_rtl2830_mxl5005s_config = {
Antti Palosaari831e0b72011-07-08 23:36:07 -0300580 .i2c_addr = 0x10, /* 0x20 */
581 .xtal = 28800000,
582 .ts_mode = 0,
583 .spec_inv = 0,
Antti Palosaari831e0b72011-07-08 23:36:07 -0300584 .vtop = 0x3f,
585 .krf = 0x04,
586 .agc_targ_val = 0x3e,
587};
588
Antti Palosaarib5cbaa42011-08-04 13:26:10 -0300589static int rtl2831u_frontend_attach(struct dvb_usb_adapter *adap)
Antti Palosaari831e0b72011-07-08 23:36:07 -0300590{
Antti Palosaaric01a3592012-08-12 22:27:07 -0300591 struct dvb_usb_device *d = adap_to_d(adap);
592 struct rtl28xxu_priv *priv = d_to_priv(d);
Antti Palosaarie8b46682013-10-12 23:35:35 -0300593 const struct rtl2830_config *rtl2830_config;
Antti Palosaari81347f52012-09-17 16:50:36 -0300594 int ret;
Antti Palosaari831e0b72011-07-08 23:36:07 -0300595
Antti Palosaaric01a3592012-08-12 22:27:07 -0300596 dev_dbg(&d->udev->dev, "%s:\n", __func__);
Antti Palosaari831e0b72011-07-08 23:36:07 -0300597
Antti Palosaari81347f52012-09-17 16:50:36 -0300598 switch (priv->tuner) {
599 case TUNER_RTL2830_QT1010:
Antti Palosaari831e0b72011-07-08 23:36:07 -0300600 rtl2830_config = &rtl28xxu_rtl2830_qt1010_config;
Antti Palosaari81347f52012-09-17 16:50:36 -0300601 break;
602 case TUNER_RTL2830_MT2060:
Antti Palosaari831e0b72011-07-08 23:36:07 -0300603 rtl2830_config = &rtl28xxu_rtl2830_mt2060_config;
Antti Palosaari81347f52012-09-17 16:50:36 -0300604 break;
605 case TUNER_RTL2830_MXL5005S:
606 rtl2830_config = &rtl28xxu_rtl2830_mxl5005s_config;
607 break;
608 default:
609 dev_err(&d->udev->dev, "%s: unknown tuner=%s\n",
610 KBUILD_MODNAME, priv->tuner_name);
Antti Palosaari831e0b72011-07-08 23:36:07 -0300611 ret = -ENODEV;
612 goto err;
613 }
614
Antti Palosaari81347f52012-09-17 16:50:36 -0300615 /* attach demodulator */
616 adap->fe[0] = dvb_attach(rtl2830_attach, rtl2830_config, &d->i2c_adap);
617 if (!adap->fe[0]) {
618 ret = -ENODEV;
619 goto err;
620 }
621
622 return 0;
Antti Palosaari831e0b72011-07-08 23:36:07 -0300623err:
Antti Palosaaric01a3592012-08-12 22:27:07 -0300624 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
Antti Palosaari831e0b72011-07-08 23:36:07 -0300625 return ret;
626}
627
Antti Palosaarie8b46682013-10-12 23:35:35 -0300628static const struct rtl2832_config rtl28xxu_rtl2832_fc0012_config = {
Thomas Mair5cf66312012-05-18 14:47:41 -0300629 .i2c_addr = 0x10, /* 0x20 */
630 .xtal = 28800000,
Thomas Mair5cf66312012-05-18 14:47:41 -0300631 .tuner = TUNER_RTL2832_FC0012
632};
633
Antti Palosaarie8b46682013-10-12 23:35:35 -0300634static const struct rtl2832_config rtl28xxu_rtl2832_fc0013_config = {
Thomas Mair5cf66312012-05-18 14:47:41 -0300635 .i2c_addr = 0x10, /* 0x20 */
636 .xtal = 28800000,
Thomas Mair5cf66312012-05-18 14:47:41 -0300637 .tuner = TUNER_RTL2832_FC0013
638};
639
Antti Palosaarie8b46682013-10-12 23:35:35 -0300640static const struct rtl2832_config rtl28xxu_rtl2832_tua9001_config = {
Antti Palosaari5be65722012-09-11 22:27:09 -0300641 .i2c_addr = 0x10, /* 0x20 */
642 .xtal = 28800000,
643 .tuner = TUNER_RTL2832_TUA9001,
644};
645
Antti Palosaarie8b46682013-10-12 23:35:35 -0300646static const struct rtl2832_config rtl28xxu_rtl2832_e4000_config = {
Antti Palosaari3d8a60d2012-09-17 17:54:27 -0300647 .i2c_addr = 0x10, /* 0x20 */
648 .xtal = 28800000,
649 .tuner = TUNER_RTL2832_E4000,
650};
651
Antti Palosaarie8b46682013-10-12 23:35:35 -0300652static const struct rtl2832_config rtl28xxu_rtl2832_r820t_config = {
Mauro Carvalho Chehab6889ab22013-04-06 09:40:11 -0300653 .i2c_addr = 0x10,
654 .xtal = 28800000,
655 .tuner = TUNER_RTL2832_R820T,
656};
657
Thomas Mair5cf66312012-05-18 14:47:41 -0300658static int rtl2832u_fc0012_tuner_callback(struct dvb_usb_device *d,
659 int cmd, int arg)
660{
661 int ret;
662 u8 val;
663
Antti Palosaaric01a3592012-08-12 22:27:07 -0300664 dev_dbg(&d->udev->dev, "%s: cmd=%d arg=%d\n", __func__, cmd, arg);
665
Thomas Mair5cf66312012-05-18 14:47:41 -0300666 switch (cmd) {
667 case FC_FE_CALLBACK_VHF_ENABLE:
668 /* set output values */
Thomas Mair40a2d3c2012-05-18 14:47:42 -0300669 ret = rtl28xx_rd_reg(d, SYS_GPIO_OUT_VAL, &val);
Thomas Mair5cf66312012-05-18 14:47:41 -0300670 if (ret)
671 goto err;
672
673 if (arg)
674 val &= 0xbf; /* set GPIO6 low */
675 else
676 val |= 0x40; /* set GPIO6 high */
677
678
Thomas Mair40a2d3c2012-05-18 14:47:42 -0300679 ret = rtl28xx_wr_reg(d, SYS_GPIO_OUT_VAL, val);
Thomas Mair5cf66312012-05-18 14:47:41 -0300680 if (ret)
681 goto err;
682 break;
683 default:
684 ret = -EINVAL;
685 goto err;
686 }
687 return 0;
Thomas Mair5cf66312012-05-18 14:47:41 -0300688err:
Antti Palosaaric01a3592012-08-12 22:27:07 -0300689 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
Thomas Mair5cf66312012-05-18 14:47:41 -0300690 return ret;
691}
692
Antti Palosaari5be65722012-09-11 22:27:09 -0300693static int rtl2832u_tua9001_tuner_callback(struct dvb_usb_device *d,
694 int cmd, int arg)
695{
696 int ret;
697 u8 val;
698
699 dev_dbg(&d->udev->dev, "%s: cmd=%d arg=%d\n", __func__, cmd, arg);
700
701 /*
702 * CEN always enabled by hardware wiring
703 * RESETN GPIO4
704 * RXEN GPIO1
705 */
706
Antti Palosaari5be65722012-09-11 22:27:09 -0300707 switch (cmd) {
708 case TUA9001_CMD_RESETN:
709 if (arg)
Antti Palosaari4005c1a2012-09-17 15:43:57 -0300710 val = (1 << 4);
Antti Palosaari5be65722012-09-11 22:27:09 -0300711 else
Antti Palosaari4005c1a2012-09-17 15:43:57 -0300712 val = (0 << 4);
Antti Palosaari5be65722012-09-11 22:27:09 -0300713
Antti Palosaari4005c1a2012-09-17 15:43:57 -0300714 ret = rtl28xx_wr_reg_mask(d, SYS_GPIO_OUT_VAL, val, 0x10);
715 if (ret)
Antti Palosaari5be65722012-09-11 22:27:09 -0300716 goto err;
717 break;
718 case TUA9001_CMD_RXEN:
719 if (arg)
Antti Palosaari4005c1a2012-09-17 15:43:57 -0300720 val = (1 << 1);
Antti Palosaari5be65722012-09-11 22:27:09 -0300721 else
Antti Palosaari4005c1a2012-09-17 15:43:57 -0300722 val = (0 << 1);
Antti Palosaari5be65722012-09-11 22:27:09 -0300723
Antti Palosaari4005c1a2012-09-17 15:43:57 -0300724 ret = rtl28xx_wr_reg_mask(d, SYS_GPIO_OUT_VAL, val, 0x02);
725 if (ret)
Antti Palosaari5be65722012-09-11 22:27:09 -0300726 goto err;
727 break;
728 }
729
730 return 0;
Antti Palosaari5be65722012-09-11 22:27:09 -0300731err:
732 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
733 return ret;
734}
735
Thomas Mair5cf66312012-05-18 14:47:41 -0300736static int rtl2832u_tuner_callback(struct dvb_usb_device *d, int cmd, int arg)
737{
738 struct rtl28xxu_priv *priv = d->priv;
739
740 switch (priv->tuner) {
741 case TUNER_RTL2832_FC0012:
742 return rtl2832u_fc0012_tuner_callback(d, cmd, arg);
Antti Palosaari5be65722012-09-11 22:27:09 -0300743 case TUNER_RTL2832_TUA9001:
744 return rtl2832u_tua9001_tuner_callback(d, cmd, arg);
Thomas Mair5cf66312012-05-18 14:47:41 -0300745 default:
746 break;
747 }
748
Antti Palosaari97efe782012-09-17 15:49:54 -0300749 return 0;
Thomas Mair5cf66312012-05-18 14:47:41 -0300750}
751
752static int rtl2832u_frontend_callback(void *adapter_priv, int component,
Antti Palosaari8acc91c2012-09-17 16:55:39 -0300753 int cmd, int arg)
Thomas Mair5cf66312012-05-18 14:47:41 -0300754{
755 struct i2c_adapter *adap = adapter_priv;
756 struct dvb_usb_device *d = i2c_get_adapdata(adap);
757
Antti Palosaari8acc91c2012-09-17 16:55:39 -0300758 dev_dbg(&d->udev->dev, "%s: component=%d cmd=%d arg=%d\n",
759 __func__, component, cmd, arg);
760
Thomas Mair5cf66312012-05-18 14:47:41 -0300761 switch (component) {
762 case DVB_FRONTEND_COMPONENT_TUNER:
763 return rtl2832u_tuner_callback(d, cmd, arg);
764 default:
765 break;
766 }
767
Antti Palosaari97efe782012-09-17 15:49:54 -0300768 return 0;
Thomas Mair5cf66312012-05-18 14:47:41 -0300769}
770
Antti Palosaarib5cbaa42011-08-04 13:26:10 -0300771static int rtl2832u_frontend_attach(struct dvb_usb_adapter *adap)
772{
773 int ret;
Antti Palosaaric01a3592012-08-12 22:27:07 -0300774 struct dvb_usb_device *d = adap_to_d(adap);
775 struct rtl28xxu_priv *priv = d_to_priv(d);
Antti Palosaarie8b46682013-10-12 23:35:35 -0300776 const struct rtl2832_config *rtl2832_config;
Antti Palosaarib5cbaa42011-08-04 13:26:10 -0300777
Antti Palosaaric01a3592012-08-12 22:27:07 -0300778 dev_dbg(&d->udev->dev, "%s:\n", __func__);
Antti Palosaarib5cbaa42011-08-04 13:26:10 -0300779
Antti Palosaarief37be12012-09-17 14:01:35 -0300780 switch (priv->tuner) {
781 case TUNER_RTL2832_FC0012:
Thomas Mair5cf66312012-05-18 14:47:41 -0300782 rtl2832_config = &rtl28xxu_rtl2832_fc0012_config;
Antti Palosaarief37be12012-09-17 14:01:35 -0300783 break;
784 case TUNER_RTL2832_FC0013:
Thomas Mair5cf66312012-05-18 14:47:41 -0300785 rtl2832_config = &rtl28xxu_rtl2832_fc0013_config;
Antti Palosaarief37be12012-09-17 14:01:35 -0300786 break;
787 case TUNER_RTL2832_FC2580:
Antti Palosaari384df492012-09-08 22:07:25 -0300788 /* FIXME: do not abuse fc0012 settings */
789 rtl2832_config = &rtl28xxu_rtl2832_fc0012_config;
Antti Palosaarief37be12012-09-17 14:01:35 -0300790 break;
791 case TUNER_RTL2832_TUA9001:
Antti Palosaari5be65722012-09-11 22:27:09 -0300792 rtl2832_config = &rtl28xxu_rtl2832_tua9001_config;
Antti Palosaarief37be12012-09-17 14:01:35 -0300793 break;
794 case TUNER_RTL2832_E4000:
Antti Palosaari3d8a60d2012-09-17 17:54:27 -0300795 rtl2832_config = &rtl28xxu_rtl2832_e4000_config;
Antti Palosaarief37be12012-09-17 14:01:35 -0300796 break;
Mauro Carvalho Chehab6889ab22013-04-06 09:40:11 -0300797 case TUNER_RTL2832_R820T:
Antti Palosaari8b4cac12013-10-30 00:45:38 -0300798 case TUNER_RTL2832_R828D:
Mauro Carvalho Chehab6889ab22013-04-06 09:40:11 -0300799 rtl2832_config = &rtl28xxu_rtl2832_r820t_config;
800 break;
Antti Palosaarief37be12012-09-17 14:01:35 -0300801 default:
802 dev_err(&d->udev->dev, "%s: unknown tuner=%s\n",
803 KBUILD_MODNAME, priv->tuner_name);
804 ret = -ENODEV;
Antti Palosaarib5cbaa42011-08-04 13:26:10 -0300805 goto err;
Antti Palosaarief37be12012-09-17 14:01:35 -0300806 }
Antti Palosaarib5cbaa42011-08-04 13:26:10 -0300807
808 /* attach demodulator */
Antti Palosaarief37be12012-09-17 14:01:35 -0300809 adap->fe[0] = dvb_attach(rtl2832_attach, rtl2832_config, &d->i2c_adap);
810 if (!adap->fe[0]) {
811 ret = -ENODEV;
812 goto err;
813 }
Thomas Mair5cf66312012-05-18 14:47:41 -0300814
Antti Palosaariae1f8452014-02-08 04:03:57 -0300815 /* RTL2832 I2C repeater */
816 priv->demod_i2c_adapter = rtl2832_get_i2c_adapter(adap->fe[0]);
817
Antti Palosaarief37be12012-09-17 14:01:35 -0300818 /* set fe callback */
Antti Palosaaric01a3592012-08-12 22:27:07 -0300819 adap->fe[0]->callback = rtl2832u_frontend_callback;
Antti Palosaarib5cbaa42011-08-04 13:26:10 -0300820
Antti Palosaarief37be12012-09-17 14:01:35 -0300821 return 0;
Antti Palosaarib5cbaa42011-08-04 13:26:10 -0300822err:
Antti Palosaaric01a3592012-08-12 22:27:07 -0300823 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
Antti Palosaarib5cbaa42011-08-04 13:26:10 -0300824 return ret;
825}
826
Antti Palosaari831e0b72011-07-08 23:36:07 -0300827static struct qt1010_config rtl28xxu_qt1010_config = {
828 .i2c_address = 0x62, /* 0xc4 */
829};
830
831static struct mt2060_config rtl28xxu_mt2060_config = {
832 .i2c_address = 0x60, /* 0xc0 */
833 .clock_out = 0,
834};
835
836static struct mxl5005s_config rtl28xxu_mxl5005s_config = {
837 .i2c_address = 0x63, /* 0xc6 */
838 .if_freq = IF_FREQ_4570000HZ,
839 .xtal_freq = CRYSTAL_FREQ_16000000HZ,
840 .agc_mode = MXL_SINGLE_AGC,
841 .tracking_filter = MXL_TF_C_H,
842 .rssi_enable = MXL_RSSI_ENABLE,
843 .cap_select = MXL_CAP_SEL_ENABLE,
844 .div_out = MXL_DIV_OUT_4,
845 .clock_out = MXL_CLOCK_OUT_DISABLE,
846 .output_load = MXL5005S_IF_OUTPUT_LOAD_200_OHM,
847 .top = MXL5005S_TOP_25P2,
848 .mod_mode = MXL_DIGITAL_MODE,
849 .if_mode = MXL_ZERO_IF,
850 .AgcMasterByte = 0x00,
851};
852
Antti Palosaarib5cbaa42011-08-04 13:26:10 -0300853static int rtl2831u_tuner_attach(struct dvb_usb_adapter *adap)
Antti Palosaari831e0b72011-07-08 23:36:07 -0300854{
855 int ret;
Antti Palosaaric01a3592012-08-12 22:27:07 -0300856 struct dvb_usb_device *d = adap_to_d(adap);
857 struct rtl28xxu_priv *priv = d_to_priv(d);
Antti Palosaari831e0b72011-07-08 23:36:07 -0300858 struct i2c_adapter *rtl2830_tuner_i2c;
Antti Palosaari9935eea2012-01-21 22:26:52 -0300859 struct dvb_frontend *fe;
Antti Palosaari831e0b72011-07-08 23:36:07 -0300860
Antti Palosaaric01a3592012-08-12 22:27:07 -0300861 dev_dbg(&d->udev->dev, "%s:\n", __func__);
Antti Palosaari831e0b72011-07-08 23:36:07 -0300862
863 /* use rtl2830 driver I2C adapter, for more info see rtl2830 driver */
Antti Palosaaric01a3592012-08-12 22:27:07 -0300864 rtl2830_tuner_i2c = rtl2830_get_tuner_i2c_adapter(adap->fe[0]);
Antti Palosaari831e0b72011-07-08 23:36:07 -0300865
866 switch (priv->tuner) {
867 case TUNER_RTL2830_QT1010:
Antti Palosaaric01a3592012-08-12 22:27:07 -0300868 fe = dvb_attach(qt1010_attach, adap->fe[0],
Antti Palosaarifba16b12012-01-21 20:28:38 -0300869 rtl2830_tuner_i2c, &rtl28xxu_qt1010_config);
Antti Palosaari831e0b72011-07-08 23:36:07 -0300870 break;
871 case TUNER_RTL2830_MT2060:
Antti Palosaaric01a3592012-08-12 22:27:07 -0300872 fe = dvb_attach(mt2060_attach, adap->fe[0],
Antti Palosaarifba16b12012-01-21 20:28:38 -0300873 rtl2830_tuner_i2c, &rtl28xxu_mt2060_config,
874 1220);
Antti Palosaari831e0b72011-07-08 23:36:07 -0300875 break;
876 case TUNER_RTL2830_MXL5005S:
Antti Palosaaric01a3592012-08-12 22:27:07 -0300877 fe = dvb_attach(mxl5005s_attach, adap->fe[0],
Antti Palosaari9935eea2012-01-21 22:26:52 -0300878 rtl2830_tuner_i2c, &rtl28xxu_mxl5005s_config);
Antti Palosaari831e0b72011-07-08 23:36:07 -0300879 break;
880 default:
Antti Palosaari9935eea2012-01-21 22:26:52 -0300881 fe = NULL;
Antti Palosaaric01a3592012-08-12 22:27:07 -0300882 dev_err(&d->udev->dev, "%s: unknown tuner=%d\n", KBUILD_MODNAME,
883 priv->tuner);
Antti Palosaari831e0b72011-07-08 23:36:07 -0300884 }
885
886 if (fe == NULL) {
887 ret = -ENODEV;
888 goto err;
889 }
890
891 return 0;
892err:
Antti Palosaaric01a3592012-08-12 22:27:07 -0300893 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
Antti Palosaari831e0b72011-07-08 23:36:07 -0300894 return ret;
895}
896
Antti Palosaari384df492012-09-08 22:07:25 -0300897static const struct fc2580_config rtl2832u_fc2580_config = {
898 .i2c_addr = 0x56,
899 .clock = 16384000,
900};
901
Antti Palosaari5be65722012-09-11 22:27:09 -0300902static struct tua9001_config rtl2832u_tua9001_config = {
903 .i2c_addr = 0x60,
904};
905
Antti Palosaariad3a7582012-12-08 23:27:49 -0300906static const struct fc0012_config rtl2832u_fc0012_config = {
907 .i2c_address = 0x63, /* 0xc6 >> 1 */
908 .xtal_freq = FC_XTAL_28_8_MHZ,
909};
910
Mauro Carvalho Chehab6889ab22013-04-06 09:40:11 -0300911static const struct r820t_config rtl2832u_r820t_config = {
912 .i2c_addr = 0x1a,
913 .xtal = 28800000,
914 .max_i2c_msg_len = 2,
915 .rafael_chip = CHIP_R820T,
916};
917
Antti Palosaari8b4cac12013-10-30 00:45:38 -0300918static const struct r820t_config rtl2832u_r828d_config = {
919 .i2c_addr = 0x3a,
920 .xtal = 16000000,
921 .max_i2c_msg_len = 2,
922 .rafael_chip = CHIP_R828D,
923};
924
Antti Palosaarib5cbaa42011-08-04 13:26:10 -0300925static int rtl2832u_tuner_attach(struct dvb_usb_adapter *adap)
926{
927 int ret;
Antti Palosaaric01a3592012-08-12 22:27:07 -0300928 struct dvb_usb_device *d = adap_to_d(adap);
929 struct rtl28xxu_priv *priv = d_to_priv(d);
Antti Palosaari28fd31f2013-10-15 19:22:45 -0300930 struct dvb_frontend *fe = NULL;
931 struct i2c_board_info info;
932 struct i2c_client *client;
Antti Palosaarib5cbaa42011-08-04 13:26:10 -0300933
Antti Palosaaric01a3592012-08-12 22:27:07 -0300934 dev_dbg(&d->udev->dev, "%s:\n", __func__);
Antti Palosaarib5cbaa42011-08-04 13:26:10 -0300935
Antti Palosaari28fd31f2013-10-15 19:22:45 -0300936 memset(&info, 0, sizeof(struct i2c_board_info));
937
Antti Palosaarib5cbaa42011-08-04 13:26:10 -0300938 switch (priv->tuner) {
Thomas Mair5cf66312012-05-18 14:47:41 -0300939 case TUNER_RTL2832_FC0012:
Antti Palosaaric01a3592012-08-12 22:27:07 -0300940 fe = dvb_attach(fc0012_attach, adap->fe[0],
Antti Palosaariad3a7582012-12-08 23:27:49 -0300941 &d->i2c_adap, &rtl2832u_fc0012_config);
Thomas Mair5cf66312012-05-18 14:47:41 -0300942
943 /* since fc0012 includs reading the signal strength delegate
944 * that to the tuner driver */
Antti Palosaaric01a3592012-08-12 22:27:07 -0300945 adap->fe[0]->ops.read_signal_strength =
946 adap->fe[0]->ops.tuner_ops.get_rf_strength;
Antti Palosaaribcf43392013-09-10 00:13:57 -0300947
948 /* attach SDR */
Antti Palosaaribeab1b52014-04-09 18:08:37 -0300949 dvb_attach_sdr(rtl2832_sdr_attach, adap->fe[0], &d->i2c_adap,
Antti Palosaaribcf43392013-09-10 00:13:57 -0300950 &rtl28xxu_rtl2832_fc0012_config, NULL);
Antti Palosaarib5cbaa42011-08-04 13:26:10 -0300951 break;
Thomas Mair5cf66312012-05-18 14:47:41 -0300952 case TUNER_RTL2832_FC0013:
Antti Palosaaric01a3592012-08-12 22:27:07 -0300953 fe = dvb_attach(fc0013_attach, adap->fe[0],
954 &d->i2c_adap, 0xc6>>1, 0, FC_XTAL_28_8_MHZ);
Thomas Mair5cf66312012-05-18 14:47:41 -0300955
956 /* fc0013 also supports signal strength reading */
Antti Palosaaric01a3592012-08-12 22:27:07 -0300957 adap->fe[0]->ops.read_signal_strength =
958 adap->fe[0]->ops.tuner_ops.get_rf_strength;
Antti Palosaaribcf43392013-09-10 00:13:57 -0300959
960 /* attach SDR */
Antti Palosaaribeab1b52014-04-09 18:08:37 -0300961 dvb_attach_sdr(rtl2832_sdr_attach, adap->fe[0], &d->i2c_adap,
Antti Palosaaribcf43392013-09-10 00:13:57 -0300962 &rtl28xxu_rtl2832_fc0013_config, NULL);
Antti Palosaari55cdb7d2014-02-03 23:07:21 -0300963 break;
Antti Palosaari28fd31f2013-10-15 19:22:45 -0300964 case TUNER_RTL2832_E4000: {
Antti Palosaaribcf43392013-09-10 00:13:57 -0300965 struct v4l2_subdev *sd;
Antti Palosaariae1f8452014-02-08 04:03:57 -0300966 struct i2c_adapter *i2c_adap_internal =
967 rtl2832_get_private_i2c_adapter(adap->fe[0]);
Antti Palosaari28fd31f2013-10-15 19:22:45 -0300968 struct e4000_config e4000_config = {
969 .fe = adap->fe[0],
970 .clock = 28800000,
971 };
972
973 strlcpy(info.type, "e4000", I2C_NAME_SIZE);
974 info.addr = 0x64;
975 info.platform_data = &e4000_config;
976
977 request_module(info.type);
Antti Palosaariae1f8452014-02-08 04:03:57 -0300978 client = i2c_new_device(priv->demod_i2c_adapter, &info);
Antti Palosaari28fd31f2013-10-15 19:22:45 -0300979 if (client == NULL || client->dev.driver == NULL)
980 break;
981
982 if (!try_module_get(client->dev.driver->owner)) {
983 i2c_unregister_device(client);
984 break;
985 }
986
987 priv->client = client;
Antti Palosaaribcf43392013-09-10 00:13:57 -0300988 sd = i2c_get_clientdata(client);
Antti Palosaariae1f8452014-02-08 04:03:57 -0300989 i2c_set_adapdata(i2c_adap_internal, d);
Antti Palosaaribcf43392013-09-10 00:13:57 -0300990
991 /* attach SDR */
Antti Palosaaribeab1b52014-04-09 18:08:37 -0300992 dvb_attach_sdr(rtl2832_sdr_attach, adap->fe[0],
Antti Palosaariae1f8452014-02-08 04:03:57 -0300993 i2c_adap_internal,
Antti Palosaaribcf43392013-09-10 00:13:57 -0300994 &rtl28xxu_rtl2832_e4000_config, sd);
Antti Palosaari28fd31f2013-10-15 19:22:45 -0300995 }
Antti Palosaari542f6a52012-09-01 21:09:22 -0300996 break;
Antti Palosaari384df492012-09-08 22:07:25 -0300997 case TUNER_RTL2832_FC2580:
998 fe = dvb_attach(fc2580_attach, adap->fe[0], &d->i2c_adap,
999 &rtl2832u_fc2580_config);
1000 break;
Antti Palosaari5be65722012-09-11 22:27:09 -03001001 case TUNER_RTL2832_TUA9001:
1002 /* enable GPIO1 and GPIO4 as output */
Antti Palosaari4005c1a2012-09-17 15:43:57 -03001003 ret = rtl28xx_wr_reg_mask(d, SYS_GPIO_DIR, 0x00, 0x12);
1004 if (ret)
Antti Palosaari5be65722012-09-11 22:27:09 -03001005 goto err;
1006
Antti Palosaari4005c1a2012-09-17 15:43:57 -03001007 ret = rtl28xx_wr_reg_mask(d, SYS_GPIO_OUT_EN, 0x12, 0x12);
1008 if (ret)
Antti Palosaari5be65722012-09-11 22:27:09 -03001009 goto err;
1010
1011 fe = dvb_attach(tua9001_attach, adap->fe[0], &d->i2c_adap,
1012 &rtl2832u_tua9001_config);
1013 break;
Mauro Carvalho Chehab6889ab22013-04-06 09:40:11 -03001014 case TUNER_RTL2832_R820T:
1015 fe = dvb_attach(r820t_attach, adap->fe[0], &d->i2c_adap,
1016 &rtl2832u_r820t_config);
Mauro Carvalho Chehab9e30edd2013-04-07 18:57:15 -03001017
1018 /* Use tuner to get the signal strength */
1019 adap->fe[0]->ops.read_signal_strength =
1020 adap->fe[0]->ops.tuner_ops.get_rf_strength;
Antti Palosaaribcf43392013-09-10 00:13:57 -03001021
1022 /* attach SDR */
Antti Palosaaribeab1b52014-04-09 18:08:37 -03001023 dvb_attach_sdr(rtl2832_sdr_attach, adap->fe[0], &d->i2c_adap,
Antti Palosaaribcf43392013-09-10 00:13:57 -03001024 &rtl28xxu_rtl2832_r820t_config, NULL);
Mauro Carvalho Chehab6889ab22013-04-06 09:40:11 -03001025 break;
Antti Palosaari8b4cac12013-10-30 00:45:38 -03001026 case TUNER_RTL2832_R828D:
1027 /* power off mn88472 demod on GPIO0 */
1028 ret = rtl28xx_wr_reg_mask(d, SYS_GPIO_OUT_VAL, 0x00, 0x01);
1029 if (ret)
1030 goto err;
1031
1032 ret = rtl28xx_wr_reg_mask(d, SYS_GPIO_DIR, 0x00, 0x01);
1033 if (ret)
1034 goto err;
1035
1036 ret = rtl28xx_wr_reg_mask(d, SYS_GPIO_OUT_EN, 0x01, 0x01);
1037 if (ret)
1038 goto err;
1039
1040 fe = dvb_attach(r820t_attach, adap->fe[0], &d->i2c_adap,
1041 &rtl2832u_r828d_config);
1042
1043 /* Use tuner to get the signal strength */
1044 adap->fe[0]->ops.read_signal_strength =
1045 adap->fe[0]->ops.tuner_ops.get_rf_strength;
1046 break;
Antti Palosaarib5cbaa42011-08-04 13:26:10 -03001047 default:
Antti Palosaaric01a3592012-08-12 22:27:07 -03001048 dev_err(&d->udev->dev, "%s: unknown tuner=%d\n", KBUILD_MODNAME,
1049 priv->tuner);
Antti Palosaarib5cbaa42011-08-04 13:26:10 -03001050 }
1051
Antti Palosaari28fd31f2013-10-15 19:22:45 -03001052 if (fe == NULL && priv->client == NULL) {
Antti Palosaarib5cbaa42011-08-04 13:26:10 -03001053 ret = -ENODEV;
1054 goto err;
1055 }
1056
1057 return 0;
1058err:
Antti Palosaaric01a3592012-08-12 22:27:07 -03001059 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
Antti Palosaarib5cbaa42011-08-04 13:26:10 -03001060 return ret;
1061}
1062
Antti Palosaaric01a3592012-08-12 22:27:07 -03001063static int rtl28xxu_init(struct dvb_usb_device *d)
1064{
1065 int ret;
1066 u8 val;
1067
1068 dev_dbg(&d->udev->dev, "%s:\n", __func__);
1069
1070 /* init USB endpoints */
1071 ret = rtl28xx_rd_reg(d, USB_SYSCTL_0, &val);
1072 if (ret)
1073 goto err;
1074
1075 /* enable DMA and Full Packet Mode*/
1076 val |= 0x09;
1077 ret = rtl28xx_wr_reg(d, USB_SYSCTL_0, val);
1078 if (ret)
1079 goto err;
1080
1081 /* set EPA maximum packet size to 0x0200 */
1082 ret = rtl28xx_wr_regs(d, USB_EPA_MAXPKT, "\x00\x02\x00\x00", 4);
1083 if (ret)
1084 goto err;
1085
1086 /* change EPA FIFO length */
1087 ret = rtl28xx_wr_regs(d, USB_EPA_FIFO_CFG, "\x14\x00\x00\x00", 4);
1088 if (ret)
1089 goto err;
1090
1091 return ret;
1092err:
1093 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
1094 return ret;
1095}
1096
Antti Palosaari28fd31f2013-10-15 19:22:45 -03001097static void rtl28xxu_exit(struct dvb_usb_device *d)
1098{
1099 struct rtl28xxu_priv *priv = d->priv;
1100 struct i2c_client *client = priv->client;
1101
1102 dev_dbg(&d->udev->dev, "%s:\n", __func__);
1103
1104 /* remove I2C tuner */
1105 if (client) {
1106 module_put(client->dev.driver->owner);
1107 i2c_unregister_device(client);
1108 }
1109
1110 return;
1111}
1112
Thomas Mair5cf66312012-05-18 14:47:41 -03001113static int rtl2831u_power_ctrl(struct dvb_usb_device *d, int onoff)
Antti Palosaari831e0b72011-07-08 23:36:07 -03001114{
1115 int ret;
Antti Palosaari8b036632012-09-01 09:54:43 -03001116 u8 gpio, sys0, epa_ctl[2];
Antti Palosaari831e0b72011-07-08 23:36:07 -03001117
Antti Palosaaric01a3592012-08-12 22:27:07 -03001118 dev_dbg(&d->udev->dev, "%s: onoff=%d\n", __func__, onoff);
Antti Palosaari831e0b72011-07-08 23:36:07 -03001119
1120 /* demod adc */
Thomas Mair40a2d3c2012-05-18 14:47:42 -03001121 ret = rtl28xx_rd_reg(d, SYS_SYS0, &sys0);
Antti Palosaari831e0b72011-07-08 23:36:07 -03001122 if (ret)
1123 goto err;
1124
1125 /* tuner power, read GPIOs */
Thomas Mair40a2d3c2012-05-18 14:47:42 -03001126 ret = rtl28xx_rd_reg(d, SYS_GPIO_OUT_VAL, &gpio);
Antti Palosaari831e0b72011-07-08 23:36:07 -03001127 if (ret)
1128 goto err;
1129
Antti Palosaaric01a3592012-08-12 22:27:07 -03001130 dev_dbg(&d->udev->dev, "%s: RD SYS0=%02x GPIO_OUT_VAL=%02x\n", __func__,
1131 sys0, gpio);
Antti Palosaari831e0b72011-07-08 23:36:07 -03001132
1133 if (onoff) {
1134 gpio |= 0x01; /* GPIO0 = 1 */
1135 gpio &= (~0x10); /* GPIO4 = 0 */
Antti Palosaari991452a2012-08-12 22:27:08 -03001136 gpio |= 0x04; /* GPIO2 = 1, LED on */
Antti Palosaari831e0b72011-07-08 23:36:07 -03001137 sys0 = sys0 & 0x0f;
1138 sys0 |= 0xe0;
Antti Palosaari8b036632012-09-01 09:54:43 -03001139 epa_ctl[0] = 0x00; /* clear stall */
1140 epa_ctl[1] = 0x00; /* clear reset */
Antti Palosaari831e0b72011-07-08 23:36:07 -03001141 } else {
Antti Palosaari831e0b72011-07-08 23:36:07 -03001142 gpio &= (~0x01); /* GPIO0 = 0 */
1143 gpio |= 0x10; /* GPIO4 = 1 */
Antti Palosaari991452a2012-08-12 22:27:08 -03001144 gpio &= (~0x04); /* GPIO2 = 1, LED off */
Antti Palosaari831e0b72011-07-08 23:36:07 -03001145 sys0 = sys0 & (~0xc0);
Antti Palosaari8b036632012-09-01 09:54:43 -03001146 epa_ctl[0] = 0x10; /* set stall */
1147 epa_ctl[1] = 0x02; /* set reset */
Antti Palosaari831e0b72011-07-08 23:36:07 -03001148 }
1149
Antti Palosaaric01a3592012-08-12 22:27:07 -03001150 dev_dbg(&d->udev->dev, "%s: WR SYS0=%02x GPIO_OUT_VAL=%02x\n", __func__,
1151 sys0, gpio);
Antti Palosaari831e0b72011-07-08 23:36:07 -03001152
1153 /* demod adc */
Thomas Mair40a2d3c2012-05-18 14:47:42 -03001154 ret = rtl28xx_wr_reg(d, SYS_SYS0, sys0);
Antti Palosaari831e0b72011-07-08 23:36:07 -03001155 if (ret)
1156 goto err;
1157
1158 /* tuner power, write GPIOs */
Thomas Mair40a2d3c2012-05-18 14:47:42 -03001159 ret = rtl28xx_wr_reg(d, SYS_GPIO_OUT_VAL, gpio);
Antti Palosaari831e0b72011-07-08 23:36:07 -03001160 if (ret)
1161 goto err;
1162
Antti Palosaari8b036632012-09-01 09:54:43 -03001163 /* streaming EP: stall & reset */
1164 ret = rtl28xx_wr_regs(d, USB_EPA_CTL, epa_ctl, 2);
1165 if (ret)
1166 goto err;
1167
1168 if (onoff)
1169 usb_clear_halt(d->udev, usb_rcvbulkpipe(d->udev, 0x81));
1170
Antti Palosaari831e0b72011-07-08 23:36:07 -03001171 return ret;
1172err:
Antti Palosaaric01a3592012-08-12 22:27:07 -03001173 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
Antti Palosaari831e0b72011-07-08 23:36:07 -03001174 return ret;
1175}
1176
Thomas Mair5cf66312012-05-18 14:47:41 -03001177static int rtl2832u_power_ctrl(struct dvb_usb_device *d, int onoff)
1178{
1179 int ret;
Thomas Mair5cf66312012-05-18 14:47:41 -03001180
Antti Palosaaric01a3592012-08-12 22:27:07 -03001181 dev_dbg(&d->udev->dev, "%s: onoff=%d\n", __func__, onoff);
Thomas Mair5cf66312012-05-18 14:47:41 -03001182
1183 if (onoff) {
Antti Palosaari526ca8d2013-06-04 18:26:54 -03001184 /* GPIO3=1, GPIO4=0 */
1185 ret = rtl28xx_wr_reg_mask(d, SYS_GPIO_OUT_VAL, 0x08, 0x18);
Thomas Mair5cf66312012-05-18 14:47:41 -03001186 if (ret)
1187 goto err;
1188
Antti Palosaari526ca8d2013-06-04 18:26:54 -03001189 /* suspend? */
1190 ret = rtl28xx_wr_reg_mask(d, SYS_DEMOD_CTL1, 0x00, 0x10);
Thomas Mair5cf66312012-05-18 14:47:41 -03001191 if (ret)
1192 goto err;
1193
Antti Palosaari526ca8d2013-06-04 18:26:54 -03001194 /* enable PLL */
1195 ret = rtl28xx_wr_reg_mask(d, SYS_DEMOD_CTL, 0x80, 0x80);
Thomas Mair5cf66312012-05-18 14:47:41 -03001196 if (ret)
1197 goto err;
1198
Antti Palosaari526ca8d2013-06-04 18:26:54 -03001199 /* disable reset */
1200 ret = rtl28xx_wr_reg_mask(d, SYS_DEMOD_CTL, 0x20, 0x20);
Thomas Mair5cf66312012-05-18 14:47:41 -03001201 if (ret)
1202 goto err;
1203
1204 mdelay(5);
1205
Antti Palosaari526ca8d2013-06-04 18:26:54 -03001206 /* enable ADC */
1207 ret = rtl28xx_wr_reg_mask(d, SYS_DEMOD_CTL, 0x48, 0x48);
Thomas Mair5cf66312012-05-18 14:47:41 -03001208 if (ret)
1209 goto err;
1210
Antti Palosaari8b036632012-09-01 09:54:43 -03001211 /* streaming EP: clear stall & reset */
1212 ret = rtl28xx_wr_regs(d, USB_EPA_CTL, "\x00\x00", 2);
1213 if (ret)
1214 goto err;
1215
1216 ret = usb_clear_halt(d->udev, usb_rcvbulkpipe(d->udev, 0x81));
1217 if (ret)
1218 goto err;
Thomas Mair5cf66312012-05-18 14:47:41 -03001219 } else {
Antti Palosaari526ca8d2013-06-04 18:26:54 -03001220 /* GPIO4=1 */
1221 ret = rtl28xx_wr_reg_mask(d, SYS_GPIO_OUT_VAL, 0x10, 0x10);
Thomas Mair5cf66312012-05-18 14:47:41 -03001222 if (ret)
1223 goto err;
1224
Antti Palosaari526ca8d2013-06-04 18:26:54 -03001225 /* disable ADC */
1226 ret = rtl28xx_wr_reg_mask(d, SYS_DEMOD_CTL, 0x00, 0x48);
Thomas Mair5cf66312012-05-18 14:47:41 -03001227 if (ret)
1228 goto err;
1229
Antti Palosaari526ca8d2013-06-04 18:26:54 -03001230 /* disable PLL */
1231 ret = rtl28xx_wr_reg_mask(d, SYS_DEMOD_CTL, 0x00, 0x80);
Thomas Mair5cf66312012-05-18 14:47:41 -03001232 if (ret)
1233 goto err;
1234
Antti Palosaari8b036632012-09-01 09:54:43 -03001235 /* streaming EP: set stall & reset */
1236 ret = rtl28xx_wr_regs(d, USB_EPA_CTL, "\x10\x02", 2);
1237 if (ret)
1238 goto err;
Thomas Mair5cf66312012-05-18 14:47:41 -03001239 }
1240
1241 return ret;
1242err:
Antti Palosaaric01a3592012-08-12 22:27:07 -03001243 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
Thomas Mair5cf66312012-05-18 14:47:41 -03001244 return ret;
1245}
1246
Antti Palosaari37b44a02013-01-04 15:21:26 -03001247#if IS_ENABLED(CONFIG_RC_CORE)
Antti Palosaarib5cbaa42011-08-04 13:26:10 -03001248static int rtl2831u_rc_query(struct dvb_usb_device *d)
Antti Palosaari831e0b72011-07-08 23:36:07 -03001249{
Antti Palosaarib5cbaa42011-08-04 13:26:10 -03001250 int ret, i;
1251 struct rtl28xxu_priv *priv = d->priv;
Antti Palosaari831e0b72011-07-08 23:36:07 -03001252 u8 buf[5];
1253 u32 rc_code;
Antti Palosaarib5cbaa42011-08-04 13:26:10 -03001254 struct rtl28xxu_reg_val rc_nec_tab[] = {
1255 { 0x3033, 0x80 },
1256 { 0x3020, 0x43 },
1257 { 0x3021, 0x16 },
1258 { 0x3022, 0x16 },
1259 { 0x3023, 0x5a },
1260 { 0x3024, 0x2d },
1261 { 0x3025, 0x16 },
1262 { 0x3026, 0x01 },
1263 { 0x3028, 0xb0 },
1264 { 0x3029, 0x04 },
1265 { 0x302c, 0x88 },
1266 { 0x302e, 0x13 },
1267 { 0x3030, 0xdf },
1268 { 0x3031, 0x05 },
1269 };
1270
1271 /* init remote controller */
1272 if (!priv->rc_active) {
1273 for (i = 0; i < ARRAY_SIZE(rc_nec_tab); i++) {
Thomas Mair40a2d3c2012-05-18 14:47:42 -03001274 ret = rtl28xx_wr_reg(d, rc_nec_tab[i].reg,
Antti Palosaari9935eea2012-01-21 22:26:52 -03001275 rc_nec_tab[i].val);
Antti Palosaarib5cbaa42011-08-04 13:26:10 -03001276 if (ret)
1277 goto err;
1278 }
1279 priv->rc_active = true;
1280 }
Antti Palosaari831e0b72011-07-08 23:36:07 -03001281
1282 ret = rtl2831_rd_regs(d, SYS_IRRC_RP, buf, 5);
1283 if (ret)
1284 goto err;
1285
1286 if (buf[4] & 0x01) {
1287 if (buf[2] == (u8) ~buf[3]) {
1288 if (buf[0] == (u8) ~buf[1]) {
1289 /* NEC standard (16 bit) */
1290 rc_code = buf[0] << 8 | buf[2];
1291 } else {
1292 /* NEC extended (24 bit) */
1293 rc_code = buf[0] << 16 |
Antti Palosaari9935eea2012-01-21 22:26:52 -03001294 buf[1] << 8 | buf[2];
Antti Palosaari831e0b72011-07-08 23:36:07 -03001295 }
1296 } else {
1297 /* NEC full (32 bit) */
1298 rc_code = buf[0] << 24 | buf[1] << 16 |
1299 buf[2] << 8 | buf[3];
1300 }
1301
1302 rc_keydown(d->rc_dev, rc_code, 0);
1303
Thomas Mair40a2d3c2012-05-18 14:47:42 -03001304 ret = rtl28xx_wr_reg(d, SYS_IRRC_SR, 1);
Antti Palosaari831e0b72011-07-08 23:36:07 -03001305 if (ret)
1306 goto err;
1307
1308 /* repeated intentionally to avoid extra keypress */
Thomas Mair40a2d3c2012-05-18 14:47:42 -03001309 ret = rtl28xx_wr_reg(d, SYS_IRRC_SR, 1);
Antti Palosaari831e0b72011-07-08 23:36:07 -03001310 if (ret)
1311 goto err;
1312 }
1313
1314 return ret;
1315err:
Antti Palosaaric01a3592012-08-12 22:27:07 -03001316 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
Antti Palosaari831e0b72011-07-08 23:36:07 -03001317 return ret;
1318}
1319
Antti Palosaaric01a3592012-08-12 22:27:07 -03001320static int rtl2831u_get_rc_config(struct dvb_usb_device *d,
1321 struct dvb_usb_rc *rc)
1322{
1323 rc->map_name = RC_MAP_EMPTY;
David Härdemanc003ab12012-10-11 19:11:54 -03001324 rc->allowed_protos = RC_BIT_NEC;
Antti Palosaaric01a3592012-08-12 22:27:07 -03001325 rc->query = rtl2831u_rc_query;
1326 rc->interval = 400;
1327
1328 return 0;
1329}
1330
Antti Palosaarib5cbaa42011-08-04 13:26:10 -03001331static int rtl2832u_rc_query(struct dvb_usb_device *d)
1332{
Antti Palosaarif39fac32013-06-04 09:17:03 -03001333 int ret, i, len;
Antti Palosaarib5cbaa42011-08-04 13:26:10 -03001334 struct rtl28xxu_priv *priv = d->priv;
Antti Palosaarif39fac32013-06-04 09:17:03 -03001335 struct ir_raw_event ev;
Antti Palosaarib5cbaa42011-08-04 13:26:10 -03001336 u8 buf[128];
Antti Palosaarif39fac32013-06-04 09:17:03 -03001337 static const struct rtl28xxu_reg_val_mask refresh_tab[] = {
1338 {IR_RX_IF, 0x03, 0xff},
1339 {IR_RX_BUF_CTRL, 0x80, 0xff},
1340 {IR_RX_CTRL, 0x80, 0xff},
1341 };
Antti Palosaarib5cbaa42011-08-04 13:26:10 -03001342
1343 /* init remote controller */
1344 if (!priv->rc_active) {
Antti Palosaarif39fac32013-06-04 09:17:03 -03001345 static const struct rtl28xxu_reg_val_mask init_tab[] = {
1346 {SYS_DEMOD_CTL1, 0x00, 0x04},
1347 {SYS_DEMOD_CTL1, 0x00, 0x08},
1348 {USB_CTRL, 0x20, 0x20},
1349 {SYS_GPIO_DIR, 0x00, 0x08},
1350 {SYS_GPIO_OUT_EN, 0x08, 0x08},
1351 {SYS_GPIO_OUT_VAL, 0x08, 0x08},
1352 {IR_MAX_DURATION0, 0xd0, 0xff},
1353 {IR_MAX_DURATION1, 0x07, 0xff},
1354 {IR_IDLE_LEN0, 0xc0, 0xff},
1355 {IR_IDLE_LEN1, 0x00, 0xff},
1356 {IR_GLITCH_LEN, 0x03, 0xff},
1357 {IR_RX_CLK, 0x09, 0xff},
1358 {IR_RX_CFG, 0x1c, 0xff},
1359 {IR_MAX_H_TOL_LEN, 0x1e, 0xff},
1360 {IR_MAX_L_TOL_LEN, 0x1e, 0xff},
1361 {IR_RX_CTRL, 0x80, 0xff},
1362 };
1363
1364 for (i = 0; i < ARRAY_SIZE(init_tab); i++) {
1365 ret = rtl28xx_wr_reg_mask(d, init_tab[i].reg,
1366 init_tab[i].val, init_tab[i].mask);
Rodrigo Tartajo1e414132013-04-20 20:02:12 -03001367 if (ret)
1368 goto err;
1369 }
Antti Palosaarif39fac32013-06-04 09:17:03 -03001370
Antti Palosaarib5cbaa42011-08-04 13:26:10 -03001371 priv->rc_active = true;
1372 }
1373
Thomas Mair40a2d3c2012-05-18 14:47:42 -03001374 ret = rtl28xx_rd_reg(d, IR_RX_IF, &buf[0]);
Antti Palosaarib5cbaa42011-08-04 13:26:10 -03001375 if (ret)
1376 goto err;
1377
1378 if (buf[0] != 0x83)
Antti Palosaarif39fac32013-06-04 09:17:03 -03001379 goto exit;
Antti Palosaarib5cbaa42011-08-04 13:26:10 -03001380
Thomas Mair40a2d3c2012-05-18 14:47:42 -03001381 ret = rtl28xx_rd_reg(d, IR_RX_BC, &buf[0]);
Antti Palosaarib5cbaa42011-08-04 13:26:10 -03001382 if (ret)
1383 goto err;
1384
1385 len = buf[0];
Antti Palosaarif39fac32013-06-04 09:17:03 -03001386
1387 /* read raw code from hw */
Antti Palosaarib5cbaa42011-08-04 13:26:10 -03001388 ret = rtl2831_rd_regs(d, IR_RX_BUF, buf, len);
Antti Palosaarif39fac32013-06-04 09:17:03 -03001389 if (ret)
1390 goto err;
Antti Palosaarib5cbaa42011-08-04 13:26:10 -03001391
Antti Palosaarif39fac32013-06-04 09:17:03 -03001392 /* let hw receive new code */
1393 for (i = 0; i < ARRAY_SIZE(refresh_tab); i++) {
1394 ret = rtl28xx_wr_reg_mask(d, refresh_tab[i].reg,
1395 refresh_tab[i].val, refresh_tab[i].mask);
Rodrigo Tartajo1e414132013-04-20 20:02:12 -03001396 if (ret)
1397 goto err;
1398 }
Antti Palosaarib5cbaa42011-08-04 13:26:10 -03001399
Antti Palosaarif39fac32013-06-04 09:17:03 -03001400 /* pass data to Kernel IR decoder */
1401 init_ir_raw_event(&ev);
1402
1403 for (i = 0; i < len; i++) {
1404 ev.pulse = buf[i] >> 7;
1405 ev.duration = 50800 * (buf[i] & 0x7f);
1406 ir_raw_event_store_with_filter(d->rc_dev, &ev);
1407 }
1408
1409 /* 'flush' ir_raw_event_store_with_filter() */
1410 ir_raw_event_set_idle(d->rc_dev, true);
1411 ir_raw_event_handle(d->rc_dev);
1412exit:
Antti Palosaarib5cbaa42011-08-04 13:26:10 -03001413 return ret;
1414err:
Antti Palosaaric01a3592012-08-12 22:27:07 -03001415 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
Antti Palosaarib5cbaa42011-08-04 13:26:10 -03001416 return ret;
1417}
1418
Antti Palosaaric01a3592012-08-12 22:27:07 -03001419static int rtl2832u_get_rc_config(struct dvb_usb_device *d,
1420 struct dvb_usb_rc *rc)
1421{
Antti Palosaari94b5fa62013-11-22 14:20:14 -03001422 /* disable IR interrupts in order to avoid SDR sample loss */
1423 if (rtl28xxu_disable_rc)
1424 return rtl28xx_wr_reg(d, IR_RX_IE, 0x00);
1425
Antti Palosaarif39fac32013-06-04 09:17:03 -03001426 /* load empty to enable rc */
1427 if (!rc->map_name)
1428 rc->map_name = RC_MAP_EMPTY;
Rodrigo Tartajo1e414132013-04-20 20:02:12 -03001429 rc->allowed_protos = RC_BIT_ALL;
Antti Palosaarif39fac32013-06-04 09:17:03 -03001430 rc->driver_type = RC_DRIVER_IR_RAW;
Antti Palosaaric01a3592012-08-12 22:27:07 -03001431 rc->query = rtl2832u_rc_query;
1432 rc->interval = 400;
Antti Palosaarib5cbaa42011-08-04 13:26:10 -03001433
Antti Palosaaric01a3592012-08-12 22:27:07 -03001434 return 0;
1435}
Antti Palosaaricedda372012-12-09 20:06:00 -03001436#else
Antti Palosaari5a186642013-06-04 16:50:54 -03001437#define rtl2831u_get_rc_config NULL
1438#define rtl2832u_get_rc_config NULL
Antti Palosaaricedda372012-12-09 20:06:00 -03001439#endif
Antti Palosaarib5cbaa42011-08-04 13:26:10 -03001440
Antti Palosaaric01a3592012-08-12 22:27:07 -03001441static const struct dvb_usb_device_properties rtl2831u_props = {
1442 .driver_name = KBUILD_MODNAME,
1443 .owner = THIS_MODULE,
1444 .adapter_nr = adapter_nr,
1445 .size_of_priv = sizeof(struct rtl28xxu_priv),
Antti Palosaari831e0b72011-07-08 23:36:07 -03001446
Antti Palosaaric01a3592012-08-12 22:27:07 -03001447 .power_ctrl = rtl2831u_power_ctrl,
1448 .i2c_algo = &rtl28xxu_i2c_algo,
Antti Palosaari81347f52012-09-17 16:50:36 -03001449 .read_config = rtl2831u_read_config,
Antti Palosaaric01a3592012-08-12 22:27:07 -03001450 .frontend_attach = rtl2831u_frontend_attach,
1451 .tuner_attach = rtl2831u_tuner_attach,
1452 .init = rtl28xxu_init,
1453 .get_rc_config = rtl2831u_get_rc_config,
Antti Palosaari831e0b72011-07-08 23:36:07 -03001454
Antti Palosaaric01a3592012-08-12 22:27:07 -03001455 .num_adapters = 1,
1456 .adapter = {
1457 {
1458 .stream = DVB_USB_STREAM_BULK(0x81, 6, 8 * 512),
Antti Palosaari831e0b72011-07-08 23:36:07 -03001459 },
Antti Palosaari831e0b72011-07-08 23:36:07 -03001460 },
Antti Palosaaric01a3592012-08-12 22:27:07 -03001461};
Antti Palosaarib5cbaa42011-08-04 13:26:10 -03001462
Antti Palosaaric01a3592012-08-12 22:27:07 -03001463static const struct dvb_usb_device_properties rtl2832u_props = {
1464 .driver_name = KBUILD_MODNAME,
1465 .owner = THIS_MODULE,
1466 .adapter_nr = adapter_nr,
1467 .size_of_priv = sizeof(struct rtl28xxu_priv),
Antti Palosaarib5cbaa42011-08-04 13:26:10 -03001468
Antti Palosaaric01a3592012-08-12 22:27:07 -03001469 .power_ctrl = rtl2832u_power_ctrl,
1470 .i2c_algo = &rtl28xxu_i2c_algo,
Antti Palosaarief37be12012-09-17 14:01:35 -03001471 .read_config = rtl2832u_read_config,
Antti Palosaaric01a3592012-08-12 22:27:07 -03001472 .frontend_attach = rtl2832u_frontend_attach,
1473 .tuner_attach = rtl2832u_tuner_attach,
1474 .init = rtl28xxu_init,
Antti Palosaari28fd31f2013-10-15 19:22:45 -03001475 .exit = rtl28xxu_exit,
Antti Palosaaric01a3592012-08-12 22:27:07 -03001476 .get_rc_config = rtl2832u_get_rc_config,
Antti Palosaarib5cbaa42011-08-04 13:26:10 -03001477
Antti Palosaaric01a3592012-08-12 22:27:07 -03001478 .num_adapters = 1,
1479 .adapter = {
1480 {
1481 .stream = DVB_USB_STREAM_BULK(0x81, 6, 8 * 512),
Antti Palosaarib5cbaa42011-08-04 13:26:10 -03001482 },
Antti Palosaarib5cbaa42011-08-04 13:26:10 -03001483 },
Antti Palosaari831e0b72011-07-08 23:36:07 -03001484};
1485
Antti Palosaaric01a3592012-08-12 22:27:07 -03001486static const struct usb_device_id rtl28xxu_id_table[] = {
Jan Vcelak60aa4c62014-02-26 15:33:40 -03001487 /* RTL2831U devices: */
Antti Palosaaric01a3592012-08-12 22:27:07 -03001488 { DVB_USB_DEVICE(USB_VID_REALTEK, USB_PID_REALTEK_RTL2831U,
1489 &rtl2831u_props, "Realtek RTL2831U reference design", NULL) },
1490 { DVB_USB_DEVICE(USB_VID_WIDEVIEW, USB_PID_FREECOM_DVBT,
1491 &rtl2831u_props, "Freecom USB2.0 DVB-T", NULL) },
1492 { DVB_USB_DEVICE(USB_VID_WIDEVIEW, USB_PID_FREECOM_DVBT_2,
1493 &rtl2831u_props, "Freecom USB2.0 DVB-T", NULL) },
Antti Palosaari831e0b72011-07-08 23:36:07 -03001494
Jan Vcelak60aa4c62014-02-26 15:33:40 -03001495 /* RTL2832U devices: */
Antti Palosaari254ee2e2012-09-18 10:27:05 -03001496 { DVB_USB_DEVICE(USB_VID_REALTEK, 0x2832,
1497 &rtl2832u_props, "Realtek RTL2832U reference design", NULL) },
1498 { DVB_USB_DEVICE(USB_VID_REALTEK, 0x2838,
1499 &rtl2832u_props, "Realtek RTL2832U reference design", NULL) },
Antti Palosaaric01a3592012-08-12 22:27:07 -03001500 { DVB_USB_DEVICE(USB_VID_TERRATEC, USB_PID_TERRATEC_CINERGY_T_STICK_BLACK_REV1,
Antti Palosaaribc6fc532013-06-04 17:24:06 -03001501 &rtl2832u_props, "TerraTec Cinergy T Stick Black", RC_MAP_TERRATEC_SLIM) },
Antti Palosaaric01a3592012-08-12 22:27:07 -03001502 { DVB_USB_DEVICE(USB_VID_GTEK, USB_PID_DELOCK_USB2_DVBT,
1503 &rtl2832u_props, "G-Tek Electronics Group Lifeview LV5TDLX DVB-T", NULL) },
1504 { DVB_USB_DEVICE(USB_VID_TERRATEC, USB_PID_NOXON_DAB_STICK,
Antti Palosaari9db15632013-01-04 18:35:00 -03001505 &rtl2832u_props, "TerraTec NOXON DAB Stick", NULL) },
Juergen Locke9de0512012-11-13 14:09:28 -03001506 { DVB_USB_DEVICE(USB_VID_TERRATEC, USB_PID_NOXON_DAB_STICK_REV2,
Antti Palosaari9db15632013-01-04 18:35:00 -03001507 &rtl2832u_props, "TerraTec NOXON DAB Stick (rev 2)", NULL) },
Till Dörgesa24bc322014-02-11 17:27:57 -03001508 { DVB_USB_DEVICE(USB_VID_TERRATEC, USB_PID_NOXON_DAB_STICK_REV3,
1509 &rtl2832u_props, "TerraTec NOXON DAB Stick (rev 3)", NULL) },
Philipp Dreimann6d608052012-09-02 19:30:54 -03001510 { DVB_USB_DEVICE(USB_VID_GTEK, USB_PID_TREKSTOR_TERRES_2_0,
1511 &rtl2832u_props, "Trekstor DVB-T Stick Terres 2.0", NULL) },
Antti Palosaaric2d246d12012-09-08 22:07:26 -03001512 { DVB_USB_DEVICE(USB_VID_DEXATEK, 0x1101,
1513 &rtl2832u_props, "Dexatek DK DVB-T Dongle", NULL) },
Antti Palosaari3d8866d2012-09-20 18:34:50 -03001514 { DVB_USB_DEVICE(USB_VID_LEADTEK, 0x6680,
1515 &rtl2832u_props, "DigitalNow Quad DVB-T Receiver", NULL) },
Robert Backhausd22d32e2013-12-13 09:59:10 -03001516 { DVB_USB_DEVICE(USB_VID_LEADTEK, USB_PID_WINFAST_DTV_DONGLE_MINID,
1517 &rtl2832u_props, "Leadtek Winfast DTV Dongle Mini D", NULL) },
Antti Palosaaria4f64402012-09-29 15:28:56 -03001518 { DVB_USB_DEVICE(USB_VID_TERRATEC, 0x00d3,
1519 &rtl2832u_props, "TerraTec Cinergy T Stick RC (Rev. 3)", NULL) },
Antti Palosaari08e57272012-11-06 16:03:35 -03001520 { DVB_USB_DEVICE(USB_VID_DEXATEK, 0x1102,
1521 &rtl2832u_props, "Dexatek DK mini DVB-T Dongle", NULL) },
Andrew Karpow86163ad2012-11-06 20:01:16 -03001522 { DVB_USB_DEVICE(USB_VID_TERRATEC, 0x00d7,
1523 &rtl2832u_props, "TerraTec Cinergy T Stick+", NULL) },
Antti Palosaari3971e792012-12-16 11:47:07 -03001524 { DVB_USB_DEVICE(USB_VID_KWORLD_2, 0xd3a8,
1525 &rtl2832u_props, "ASUS My Cinema-U3100Mini Plus V2", NULL) },
Alexander Inyukhin78a5e702013-01-04 18:19:02 -03001526 { DVB_USB_DEVICE(USB_VID_KWORLD_2, 0xd393,
Antti Palosaari9db15632013-01-04 18:35:00 -03001527 &rtl2832u_props, "GIGABYTE U7300", NULL) },
Eddi De Pieri09f94082013-01-14 18:21:32 -03001528 { DVB_USB_DEVICE(USB_VID_DEXATEK, 0x1104,
Antti Palosaari7dc0a012013-06-04 17:01:25 -03001529 &rtl2832u_props, "MSI DIGIVOX Micro HD", NULL) },
Alistair Buxtonb9e2aff2013-02-12 21:58:47 -03001530 { DVB_USB_DEVICE(USB_VID_COMPRO, 0x0620,
1531 &rtl2832u_props, "Compro VideoMate U620F", NULL) },
Fabrizio Gazzatoe6a60d72013-02-15 18:54:54 -03001532 { DVB_USB_DEVICE(USB_VID_KWORLD_2, 0xd394,
1533 &rtl2832u_props, "MaxMedia HU394-T", NULL) },
Miroslav Šustek6c5a4062013-05-14 19:42:11 -03001534 { DVB_USB_DEVICE(USB_VID_LEADTEK, 0x6a03,
Antti Palosaari7dc0a012013-06-04 17:01:25 -03001535 &rtl2832u_props, "Leadtek WinFast DTV Dongle mini", NULL) },
Alessandro Micelic6be7522013-06-04 16:10:34 -03001536 { DVB_USB_DEVICE(USB_VID_GTEK, USB_PID_CPYTO_REDI_PC50A,
Antti Palosaari7df27252013-06-04 19:43:30 -03001537 &rtl2832u_props, "Crypto ReDi PC 50 A", NULL) },
Jan Vcelakac298cc2014-02-26 15:33:39 -03001538 { DVB_USB_DEVICE(USB_VID_KYE, 0x707f,
1539 &rtl2832u_props, "Genius TVGo DVB-T03", NULL) },
Brian Healy9ca24ae2014-05-04 18:43:39 -03001540 { DVB_USB_DEVICE(USB_VID_KWORLD_2, 0xd395,
1541 &rtl2832u_props, "Peak DVB-T USB", NULL) },
Alessandro Micelif27f5b02014-05-04 07:37:15 -03001542 { DVB_USB_DEVICE(USB_VID_KWORLD_2, USB_PID_SVEON_STV20_RTL2832U,
1543 &rtl2832u_props, "Sveon STV20", NULL) },
Alessandro Miceli74a86272014-05-04 07:50:31 -03001544 { DVB_USB_DEVICE(USB_VID_KWORLD_2, USB_PID_SVEON_STV27,
1545 &rtl2832u_props, "Sveon STV27", NULL) },
Antti Palosaari8fdd33b2013-10-30 02:33:42 -03001546
Jan Vcelak60aa4c62014-02-26 15:33:40 -03001547 /* RTL2832P devices: */
Antti Palosaari8fdd33b2013-10-30 02:33:42 -03001548 { DVB_USB_DEVICE(USB_VID_HANFTEK, 0x0131,
1549 &rtl2832u_props, "Astrometa DVB-T2", NULL) },
Antti Palosaaric01a3592012-08-12 22:27:07 -03001550 { }
1551};
1552MODULE_DEVICE_TABLE(usb, rtl28xxu_id_table);
Antti Palosaari831e0b72011-07-08 23:36:07 -03001553
Antti Palosaaric01a3592012-08-12 22:27:07 -03001554static struct usb_driver rtl28xxu_usb_driver = {
1555 .name = KBUILD_MODNAME,
1556 .id_table = rtl28xxu_id_table,
1557 .probe = dvb_usbv2_probe,
1558 .disconnect = dvb_usbv2_disconnect,
1559 .suspend = dvb_usbv2_suspend,
1560 .resume = dvb_usbv2_resume,
Antti Palosaari78fa5902012-08-14 22:21:09 -03001561 .reset_resume = dvb_usbv2_reset_resume,
Antti Palosaaric01a3592012-08-12 22:27:07 -03001562 .no_dynamic_id = 1,
1563 .soft_unbind = 1,
Antti Palosaari831e0b72011-07-08 23:36:07 -03001564};
1565
Antti Palosaaric01a3592012-08-12 22:27:07 -03001566module_usb_driver(rtl28xxu_usb_driver);
Antti Palosaari831e0b72011-07-08 23:36:07 -03001567
1568MODULE_DESCRIPTION("Realtek RTL28xxU DVB USB driver");
1569MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
Thomas Mair5cf66312012-05-18 14:47:41 -03001570MODULE_AUTHOR("Thomas Mair <thomas.mair86@googlemail.com>");
Antti Palosaari831e0b72011-07-08 23:36:07 -03001571MODULE_LICENSE("GPL");