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