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