1 /*
2  * Silicon Labs Si2168 DVB-T/T2/C demodulator driver
3  *
4  * Copyright (C) 2014 Antti Palosaari <crope@iki.fi>
5  *
6  *    This program is free software; you can redistribute it and/or modify
7  *    it under the terms of the GNU General Public License as published by
8  *    the Free Software Foundation; either version 2 of the License, or
9  *    (at your option) any later version.
10  *
11  *    This program is distributed in the hope that it will be useful,
12  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *    GNU General Public License for more details.
15  */
16 
17 #include <linux/delay.h>
18 
19 #include "si2168_priv.h"
20 
21 static const struct dvb_frontend_ops si2168_ops;
22 
23 /* execute firmware command */
24 static int si2168_cmd_execute(struct i2c_client *client, struct si2168_cmd *cmd)
25 {
26 	struct si2168_dev *dev = i2c_get_clientdata(client);
27 	int ret;
28 	unsigned long timeout;
29 
30 	mutex_lock(&dev->i2c_mutex);
31 
32 	if (cmd->wlen) {
33 		/* write cmd and args for firmware */
34 		ret = i2c_master_send(client, cmd->args, cmd->wlen);
35 		if (ret < 0) {
36 			goto err_mutex_unlock;
37 		} else if (ret != cmd->wlen) {
38 			ret = -EREMOTEIO;
39 			goto err_mutex_unlock;
40 		}
41 	}
42 
43 	if (cmd->rlen) {
44 		/* wait cmd execution terminate */
45 		#define TIMEOUT 70
46 		timeout = jiffies + msecs_to_jiffies(TIMEOUT);
47 		while (!time_after(jiffies, timeout)) {
48 			ret = i2c_master_recv(client, cmd->args, cmd->rlen);
49 			if (ret < 0) {
50 				goto err_mutex_unlock;
51 			} else if (ret != cmd->rlen) {
52 				ret = -EREMOTEIO;
53 				goto err_mutex_unlock;
54 			}
55 
56 			/* firmware ready? */
57 			if ((cmd->args[0] >> 7) & 0x01)
58 				break;
59 		}
60 
61 		dev_dbg(&client->dev, "cmd execution took %d ms\n",
62 				jiffies_to_msecs(jiffies) -
63 				(jiffies_to_msecs(timeout) - TIMEOUT));
64 
65 		/* error bit set? */
66 		if ((cmd->args[0] >> 6) & 0x01) {
67 			ret = -EREMOTEIO;
68 			goto err_mutex_unlock;
69 		}
70 
71 		if (!((cmd->args[0] >> 7) & 0x01)) {
72 			ret = -ETIMEDOUT;
73 			goto err_mutex_unlock;
74 		}
75 	}
76 
77 	mutex_unlock(&dev->i2c_mutex);
78 	return 0;
79 err_mutex_unlock:
80 	mutex_unlock(&dev->i2c_mutex);
81 	dev_dbg(&client->dev, "failed=%d\n", ret);
82 	return ret;
83 }
84 
85 static int si2168_read_status(struct dvb_frontend *fe, enum fe_status *status)
86 {
87 	struct i2c_client *client = fe->demodulator_priv;
88 	struct si2168_dev *dev = i2c_get_clientdata(client);
89 	struct dtv_frontend_properties *c = &fe->dtv_property_cache;
90 	int ret, i;
91 	unsigned int utmp, utmp1, utmp2;
92 	struct si2168_cmd cmd;
93 
94 	*status = 0;
95 
96 	if (!dev->active) {
97 		ret = -EAGAIN;
98 		goto err;
99 	}
100 
101 	switch (c->delivery_system) {
102 	case SYS_DVBT:
103 		memcpy(cmd.args, "\xa0\x01", 2);
104 		cmd.wlen = 2;
105 		cmd.rlen = 13;
106 		break;
107 	case SYS_DVBC_ANNEX_A:
108 		memcpy(cmd.args, "\x90\x01", 2);
109 		cmd.wlen = 2;
110 		cmd.rlen = 9;
111 		break;
112 	case SYS_DVBT2:
113 		memcpy(cmd.args, "\x50\x01", 2);
114 		cmd.wlen = 2;
115 		cmd.rlen = 14;
116 		break;
117 	default:
118 		ret = -EINVAL;
119 		goto err;
120 	}
121 
122 	ret = si2168_cmd_execute(client, &cmd);
123 	if (ret)
124 		goto err;
125 
126 	switch ((cmd.args[2] >> 1) & 0x03) {
127 	case 0x01:
128 		*status = FE_HAS_SIGNAL | FE_HAS_CARRIER;
129 		break;
130 	case 0x03:
131 		*status = FE_HAS_SIGNAL | FE_HAS_CARRIER | FE_HAS_VITERBI |
132 				FE_HAS_SYNC | FE_HAS_LOCK;
133 		break;
134 	}
135 
136 	dev->fe_status = *status;
137 
138 	if (*status & FE_HAS_LOCK) {
139 		c->cnr.len = 1;
140 		c->cnr.stat[0].scale = FE_SCALE_DECIBEL;
141 		c->cnr.stat[0].svalue = cmd.args[3] * 1000 / 4;
142 	} else {
143 		c->cnr.len = 1;
144 		c->cnr.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
145 	}
146 
147 	dev_dbg(&client->dev, "status=%02x args=%*ph\n",
148 			*status, cmd.rlen, cmd.args);
149 
150 	/* BER */
151 	if (*status & FE_HAS_VITERBI) {
152 		memcpy(cmd.args, "\x82\x00", 2);
153 		cmd.wlen = 2;
154 		cmd.rlen = 3;
155 		ret = si2168_cmd_execute(client, &cmd);
156 		if (ret)
157 			goto err;
158 
159 		/*
160 		 * Firmware returns [0, 255] mantissa and [0, 8] exponent.
161 		 * Convert to DVB API: mantissa * 10^(8 - exponent) / 10^8
162 		 */
163 		utmp = clamp(8 - cmd.args[1], 0, 8);
164 		for (i = 0, utmp1 = 1; i < utmp; i++)
165 			utmp1 = utmp1 * 10;
166 
167 		utmp1 = cmd.args[2] * utmp1;
168 		utmp2 = 100000000; /* 10^8 */
169 
170 		dev_dbg(&client->dev,
171 			"post_bit_error=%u post_bit_count=%u ber=%u*10^-%u\n",
172 			utmp1, utmp2, cmd.args[2], cmd.args[1]);
173 
174 		c->post_bit_error.stat[0].scale = FE_SCALE_COUNTER;
175 		c->post_bit_error.stat[0].uvalue += utmp1;
176 		c->post_bit_count.stat[0].scale = FE_SCALE_COUNTER;
177 		c->post_bit_count.stat[0].uvalue += utmp2;
178 	} else {
179 		c->post_bit_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
180 		c->post_bit_count.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
181 	}
182 
183 	/* UCB */
184 	if (*status & FE_HAS_SYNC) {
185 		memcpy(cmd.args, "\x84\x01", 2);
186 		cmd.wlen = 2;
187 		cmd.rlen = 3;
188 		ret = si2168_cmd_execute(client, &cmd);
189 		if (ret)
190 			goto err;
191 
192 		utmp1 = cmd.args[2] << 8 | cmd.args[1] << 0;
193 		dev_dbg(&client->dev, "block_error=%u\n", utmp1);
194 
195 		/* Sometimes firmware returns bogus value */
196 		if (utmp1 == 0xffff)
197 			utmp1 = 0;
198 
199 		c->block_error.stat[0].scale = FE_SCALE_COUNTER;
200 		c->block_error.stat[0].uvalue += utmp1;
201 	} else {
202 		c->block_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
203 	}
204 
205 	return 0;
206 err:
207 	dev_dbg(&client->dev, "failed=%d\n", ret);
208 	return ret;
209 }
210 
211 static int si2168_set_frontend(struct dvb_frontend *fe)
212 {
213 	struct i2c_client *client = fe->demodulator_priv;
214 	struct si2168_dev *dev = i2c_get_clientdata(client);
215 	struct dtv_frontend_properties *c = &fe->dtv_property_cache;
216 	int ret;
217 	struct si2168_cmd cmd;
218 	u8 bandwidth, delivery_system;
219 
220 	dev_dbg(&client->dev,
221 			"delivery_system=%u modulation=%u frequency=%u bandwidth_hz=%u symbol_rate=%u inversion=%u stream_id=%u\n",
222 			c->delivery_system, c->modulation, c->frequency,
223 			c->bandwidth_hz, c->symbol_rate, c->inversion,
224 			c->stream_id);
225 
226 	if (!dev->active) {
227 		ret = -EAGAIN;
228 		goto err;
229 	}
230 
231 	switch (c->delivery_system) {
232 	case SYS_DVBT:
233 		delivery_system = 0x20;
234 		break;
235 	case SYS_DVBC_ANNEX_A:
236 		delivery_system = 0x30;
237 		break;
238 	case SYS_DVBT2:
239 		delivery_system = 0x70;
240 		break;
241 	default:
242 		ret = -EINVAL;
243 		goto err;
244 	}
245 
246 	if (c->bandwidth_hz == 0) {
247 		ret = -EINVAL;
248 		goto err;
249 	} else if (c->bandwidth_hz <= 2000000)
250 		bandwidth = 0x02;
251 	else if (c->bandwidth_hz <= 5000000)
252 		bandwidth = 0x05;
253 	else if (c->bandwidth_hz <= 6000000)
254 		bandwidth = 0x06;
255 	else if (c->bandwidth_hz <= 7000000)
256 		bandwidth = 0x07;
257 	else if (c->bandwidth_hz <= 8000000)
258 		bandwidth = 0x08;
259 	else if (c->bandwidth_hz <= 9000000)
260 		bandwidth = 0x09;
261 	else if (c->bandwidth_hz <= 10000000)
262 		bandwidth = 0x0a;
263 	else
264 		bandwidth = 0x0f;
265 
266 	/* program tuner */
267 	if (fe->ops.tuner_ops.set_params) {
268 		ret = fe->ops.tuner_ops.set_params(fe);
269 		if (ret)
270 			goto err;
271 	}
272 
273 	memcpy(cmd.args, "\x88\x02\x02\x02\x02", 5);
274 	cmd.wlen = 5;
275 	cmd.rlen = 5;
276 	ret = si2168_cmd_execute(client, &cmd);
277 	if (ret)
278 		goto err;
279 
280 	/* that has no big effect */
281 	if (c->delivery_system == SYS_DVBT)
282 		memcpy(cmd.args, "\x89\x21\x06\x11\xff\x98", 6);
283 	else if (c->delivery_system == SYS_DVBC_ANNEX_A)
284 		memcpy(cmd.args, "\x89\x21\x06\x11\x89\xf0", 6);
285 	else if (c->delivery_system == SYS_DVBT2)
286 		memcpy(cmd.args, "\x89\x21\x06\x11\x89\x20", 6);
287 	cmd.wlen = 6;
288 	cmd.rlen = 3;
289 	ret = si2168_cmd_execute(client, &cmd);
290 	if (ret)
291 		goto err;
292 
293 	if (c->delivery_system == SYS_DVBT2) {
294 		/* select PLP */
295 		cmd.args[0] = 0x52;
296 		cmd.args[1] = c->stream_id & 0xff;
297 		cmd.args[2] = c->stream_id == NO_STREAM_ID_FILTER ? 0 : 1;
298 		cmd.wlen = 3;
299 		cmd.rlen = 1;
300 		ret = si2168_cmd_execute(client, &cmd);
301 		if (ret)
302 			goto err;
303 	}
304 
305 	memcpy(cmd.args, "\x51\x03", 2);
306 	cmd.wlen = 2;
307 	cmd.rlen = 12;
308 	ret = si2168_cmd_execute(client, &cmd);
309 	if (ret)
310 		goto err;
311 
312 	memcpy(cmd.args, "\x12\x08\x04", 3);
313 	cmd.wlen = 3;
314 	cmd.rlen = 3;
315 	ret = si2168_cmd_execute(client, &cmd);
316 	if (ret)
317 		goto err;
318 
319 	memcpy(cmd.args, "\x14\x00\x0c\x10\x12\x00", 6);
320 	cmd.wlen = 6;
321 	cmd.rlen = 4;
322 	ret = si2168_cmd_execute(client, &cmd);
323 	if (ret)
324 		goto err;
325 
326 	memcpy(cmd.args, "\x14\x00\x06\x10\x24\x00", 6);
327 	cmd.wlen = 6;
328 	cmd.rlen = 4;
329 	ret = si2168_cmd_execute(client, &cmd);
330 	if (ret)
331 		goto err;
332 
333 	memcpy(cmd.args, "\x14\x00\x07\x10\x00\x24", 6);
334 	cmd.wlen = 6;
335 	cmd.rlen = 4;
336 	ret = si2168_cmd_execute(client, &cmd);
337 	if (ret)
338 		goto err;
339 
340 	memcpy(cmd.args, "\x14\x00\x0a\x10\x00\x00", 6);
341 	cmd.args[4] = delivery_system | bandwidth;
342 	cmd.wlen = 6;
343 	cmd.rlen = 4;
344 	ret = si2168_cmd_execute(client, &cmd);
345 	if (ret)
346 		goto err;
347 
348 	/* set DVB-C symbol rate */
349 	if (c->delivery_system == SYS_DVBC_ANNEX_A) {
350 		memcpy(cmd.args, "\x14\x00\x02\x11", 4);
351 		cmd.args[4] = ((c->symbol_rate / 1000) >> 0) & 0xff;
352 		cmd.args[5] = ((c->symbol_rate / 1000) >> 8) & 0xff;
353 		cmd.wlen = 6;
354 		cmd.rlen = 4;
355 		ret = si2168_cmd_execute(client, &cmd);
356 		if (ret)
357 			goto err;
358 	}
359 
360 	memcpy(cmd.args, "\x14\x00\x0f\x10\x10\x00", 6);
361 	cmd.wlen = 6;
362 	cmd.rlen = 4;
363 	ret = si2168_cmd_execute(client, &cmd);
364 	if (ret)
365 		goto err;
366 
367 	memcpy(cmd.args, "\x14\x00\x09\x10\xe3\x08", 6);
368 	cmd.args[5] |= dev->ts_clock_inv ? 0x00 : 0x10;
369 	cmd.wlen = 6;
370 	cmd.rlen = 4;
371 	ret = si2168_cmd_execute(client, &cmd);
372 	if (ret)
373 		goto err;
374 
375 	memcpy(cmd.args, "\x14\x00\x08\x10\xd7\x05", 6);
376 	cmd.args[5] |= dev->ts_clock_inv ? 0x00 : 0x10;
377 	cmd.wlen = 6;
378 	cmd.rlen = 4;
379 	ret = si2168_cmd_execute(client, &cmd);
380 	if (ret)
381 		goto err;
382 
383 	memcpy(cmd.args, "\x14\x00\x01\x12\x00\x00", 6);
384 	cmd.wlen = 6;
385 	cmd.rlen = 4;
386 	ret = si2168_cmd_execute(client, &cmd);
387 	if (ret)
388 		goto err;
389 
390 	memcpy(cmd.args, "\x14\x00\x01\x03\x0c\x00", 6);
391 	cmd.wlen = 6;
392 	cmd.rlen = 4;
393 	ret = si2168_cmd_execute(client, &cmd);
394 	if (ret)
395 		goto err;
396 
397 	memcpy(cmd.args, "\x85", 1);
398 	cmd.wlen = 1;
399 	cmd.rlen = 1;
400 	ret = si2168_cmd_execute(client, &cmd);
401 	if (ret)
402 		goto err;
403 
404 	dev->delivery_system = c->delivery_system;
405 
406 	return 0;
407 err:
408 	dev_dbg(&client->dev, "failed=%d\n", ret);
409 	return ret;
410 }
411 
412 static int si2168_ts_bus_ctrl(struct dvb_frontend *fe, int acquire)
413 {
414 	struct i2c_client *client = fe->demodulator_priv;
415 	struct si2168_dev *dev = i2c_get_clientdata(client);
416 	struct si2168_cmd cmd;
417 	int ret = 0;
418 
419 	dev_dbg(&client->dev, "%s acquire: %d\n", __func__, acquire);
420 
421 	/* set TS_MODE property */
422 	memcpy(cmd.args, "\x14\x00\x01\x10\x10\x00", 6);
423 	if (acquire)
424 		cmd.args[4] |= dev->ts_mode;
425 	else
426 		cmd.args[4] |= SI2168_TS_TRISTATE;
427 	if (dev->ts_clock_gapped)
428 		cmd.args[4] |= 0x40;
429 	cmd.wlen = 6;
430 	cmd.rlen = 4;
431 	ret = si2168_cmd_execute(client, &cmd);
432 
433 	return ret;
434 }
435 
436 static int si2168_init(struct dvb_frontend *fe)
437 {
438 	struct i2c_client *client = fe->demodulator_priv;
439 	struct si2168_dev *dev = i2c_get_clientdata(client);
440 	struct dtv_frontend_properties *c = &fe->dtv_property_cache;
441 	int ret, len, remaining;
442 	const struct firmware *fw;
443 	struct si2168_cmd cmd;
444 
445 	dev_dbg(&client->dev, "\n");
446 
447 	/* initialize */
448 	memcpy(cmd.args, "\xc0\x12\x00\x0c\x00\x0d\x16\x00\x00\x00\x00\x00\x00", 13);
449 	cmd.wlen = 13;
450 	cmd.rlen = 0;
451 	ret = si2168_cmd_execute(client, &cmd);
452 	if (ret)
453 		goto err;
454 
455 	if (dev->warm) {
456 		/* resume */
457 		memcpy(cmd.args, "\xc0\x06\x08\x0f\x00\x20\x21\x01", 8);
458 		cmd.wlen = 8;
459 		cmd.rlen = 1;
460 		ret = si2168_cmd_execute(client, &cmd);
461 		if (ret)
462 			goto err;
463 
464 		udelay(100);
465 		memcpy(cmd.args, "\x85", 1);
466 		cmd.wlen = 1;
467 		cmd.rlen = 1;
468 		ret = si2168_cmd_execute(client, &cmd);
469 		if (ret)
470 			goto err;
471 
472 		goto warm;
473 	}
474 
475 	/* power up */
476 	memcpy(cmd.args, "\xc0\x06\x01\x0f\x00\x20\x20\x01", 8);
477 	cmd.wlen = 8;
478 	cmd.rlen = 1;
479 	ret = si2168_cmd_execute(client, &cmd);
480 	if (ret)
481 		goto err;
482 
483 	/* request the firmware, this will block and timeout */
484 	ret = request_firmware(&fw, dev->firmware_name, &client->dev);
485 	if (ret) {
486 		/* fallback mechanism to handle old name for Si2168 B40 fw */
487 		if (dev->chip_id == SI2168_CHIP_ID_B40) {
488 			dev->firmware_name = SI2168_B40_FIRMWARE_FALLBACK;
489 			ret = request_firmware(&fw, dev->firmware_name,
490 					       &client->dev);
491 		}
492 
493 		if (ret == 0) {
494 			dev_notice(&client->dev,
495 					"please install firmware file '%s'\n",
496 					SI2168_B40_FIRMWARE);
497 		} else {
498 			dev_err(&client->dev,
499 					"firmware file '%s' not found\n",
500 					dev->firmware_name);
501 			goto err_release_firmware;
502 		}
503 	}
504 
505 	dev_info(&client->dev, "downloading firmware from file '%s'\n",
506 			dev->firmware_name);
507 
508 	if ((fw->size % 17 == 0) && (fw->data[0] > 5)) {
509 		/* firmware is in the new format */
510 		for (remaining = fw->size; remaining > 0; remaining -= 17) {
511 			len = fw->data[fw->size - remaining];
512 			if (len > SI2168_ARGLEN) {
513 				ret = -EINVAL;
514 				break;
515 			}
516 			memcpy(cmd.args, &fw->data[(fw->size - remaining) + 1], len);
517 			cmd.wlen = len;
518 			cmd.rlen = 1;
519 			ret = si2168_cmd_execute(client, &cmd);
520 			if (ret)
521 				break;
522 		}
523 	} else if (fw->size % 8 == 0) {
524 		/* firmware is in the old format */
525 		for (remaining = fw->size; remaining > 0; remaining -= 8) {
526 			len = 8;
527 			memcpy(cmd.args, &fw->data[fw->size - remaining], len);
528 			cmd.wlen = len;
529 			cmd.rlen = 1;
530 			ret = si2168_cmd_execute(client, &cmd);
531 			if (ret)
532 				break;
533 		}
534 	} else {
535 		/* bad or unknown firmware format */
536 		ret = -EINVAL;
537 	}
538 
539 	if (ret) {
540 		dev_err(&client->dev, "firmware download failed %d\n", ret);
541 		goto err_release_firmware;
542 	}
543 
544 	release_firmware(fw);
545 
546 	memcpy(cmd.args, "\x01\x01", 2);
547 	cmd.wlen = 2;
548 	cmd.rlen = 1;
549 	ret = si2168_cmd_execute(client, &cmd);
550 	if (ret)
551 		goto err;
552 
553 	/* query firmware version */
554 	memcpy(cmd.args, "\x11", 1);
555 	cmd.wlen = 1;
556 	cmd.rlen = 10;
557 	ret = si2168_cmd_execute(client, &cmd);
558 	if (ret)
559 		goto err;
560 
561 	dev->version = (cmd.args[9] + '@') << 24 | (cmd.args[6] - '0') << 16 |
562 		       (cmd.args[7] - '0') << 8 | (cmd.args[8]) << 0;
563 	dev_info(&client->dev, "firmware version: %c %d.%d.%d\n",
564 		 dev->version >> 24 & 0xff, dev->version >> 16 & 0xff,
565 		 dev->version >> 8 & 0xff, dev->version >> 0 & 0xff);
566 
567 	ret = si2168_ts_bus_ctrl(fe, 1);
568 	if (ret)
569 		goto err;
570 
571 	dev->warm = true;
572 warm:
573 	/* Init stats here to indicate which stats are supported */
574 	c->cnr.len = 1;
575 	c->cnr.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
576 	c->post_bit_error.len = 1;
577 	c->post_bit_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
578 	c->post_bit_count.len = 1;
579 	c->post_bit_count.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
580 	c->block_error.len = 1;
581 	c->block_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
582 
583 	dev->active = true;
584 
585 	return 0;
586 err_release_firmware:
587 	release_firmware(fw);
588 err:
589 	dev_dbg(&client->dev, "failed=%d\n", ret);
590 	return ret;
591 }
592 
593 static int si2168_sleep(struct dvb_frontend *fe)
594 {
595 	struct i2c_client *client = fe->demodulator_priv;
596 	struct si2168_dev *dev = i2c_get_clientdata(client);
597 	int ret;
598 	struct si2168_cmd cmd;
599 
600 	dev_dbg(&client->dev, "\n");
601 
602 	dev->active = false;
603 
604 	/* tri-state data bus */
605 	si2168_ts_bus_ctrl(fe, 0);
606 
607 	/* Firmware B 4.0-11 or later loses warm state during sleep */
608 	if (dev->version > ('B' << 24 | 4 << 16 | 0 << 8 | 11 << 0))
609 		dev->warm = false;
610 
611 	memcpy(cmd.args, "\x13", 1);
612 	cmd.wlen = 1;
613 	cmd.rlen = 0;
614 	ret = si2168_cmd_execute(client, &cmd);
615 	if (ret)
616 		goto err;
617 
618 	return 0;
619 err:
620 	dev_dbg(&client->dev, "failed=%d\n", ret);
621 	return ret;
622 }
623 
624 static int si2168_get_tune_settings(struct dvb_frontend *fe,
625 	struct dvb_frontend_tune_settings *s)
626 {
627 	s->min_delay_ms = 900;
628 
629 	return 0;
630 }
631 
632 static int si2168_select(struct i2c_mux_core *muxc, u32 chan)
633 {
634 	struct i2c_client *client = i2c_mux_priv(muxc);
635 	int ret;
636 	struct si2168_cmd cmd;
637 
638 	/* open I2C gate */
639 	memcpy(cmd.args, "\xc0\x0d\x01", 3);
640 	cmd.wlen = 3;
641 	cmd.rlen = 0;
642 	ret = si2168_cmd_execute(client, &cmd);
643 	if (ret)
644 		goto err;
645 
646 	return 0;
647 err:
648 	dev_dbg(&client->dev, "failed=%d\n", ret);
649 	return ret;
650 }
651 
652 static int si2168_deselect(struct i2c_mux_core *muxc, u32 chan)
653 {
654 	struct i2c_client *client = i2c_mux_priv(muxc);
655 	int ret;
656 	struct si2168_cmd cmd;
657 
658 	/* close I2C gate */
659 	memcpy(cmd.args, "\xc0\x0d\x00", 3);
660 	cmd.wlen = 3;
661 	cmd.rlen = 0;
662 	ret = si2168_cmd_execute(client, &cmd);
663 	if (ret)
664 		goto err;
665 
666 	return 0;
667 err:
668 	dev_dbg(&client->dev, "failed=%d\n", ret);
669 	return ret;
670 }
671 
672 static const struct dvb_frontend_ops si2168_ops = {
673 	.delsys = {SYS_DVBT, SYS_DVBT2, SYS_DVBC_ANNEX_A},
674 	.info = {
675 		.name = "Silicon Labs Si2168",
676 		.symbol_rate_min = 1000000,
677 		.symbol_rate_max = 7200000,
678 		.caps =	FE_CAN_FEC_1_2 |
679 			FE_CAN_FEC_2_3 |
680 			FE_CAN_FEC_3_4 |
681 			FE_CAN_FEC_5_6 |
682 			FE_CAN_FEC_7_8 |
683 			FE_CAN_FEC_AUTO |
684 			FE_CAN_QPSK |
685 			FE_CAN_QAM_16 |
686 			FE_CAN_QAM_32 |
687 			FE_CAN_QAM_64 |
688 			FE_CAN_QAM_128 |
689 			FE_CAN_QAM_256 |
690 			FE_CAN_QAM_AUTO |
691 			FE_CAN_TRANSMISSION_MODE_AUTO |
692 			FE_CAN_GUARD_INTERVAL_AUTO |
693 			FE_CAN_HIERARCHY_AUTO |
694 			FE_CAN_MUTE_TS |
695 			FE_CAN_2G_MODULATION |
696 			FE_CAN_MULTISTREAM
697 	},
698 
699 	.get_tune_settings = si2168_get_tune_settings,
700 
701 	.init = si2168_init,
702 	.sleep = si2168_sleep,
703 
704 	.ts_bus_ctrl          = si2168_ts_bus_ctrl,
705 
706 	.set_frontend = si2168_set_frontend,
707 
708 	.read_status = si2168_read_status,
709 };
710 
711 static int si2168_probe(struct i2c_client *client,
712 		const struct i2c_device_id *id)
713 {
714 	struct si2168_config *config = client->dev.platform_data;
715 	struct si2168_dev *dev;
716 	int ret;
717 	struct si2168_cmd cmd;
718 
719 	dev_dbg(&client->dev, "\n");
720 
721 	dev = kzalloc(sizeof(*dev), GFP_KERNEL);
722 	if (!dev) {
723 		ret = -ENOMEM;
724 		goto err;
725 	}
726 
727 	i2c_set_clientdata(client, dev);
728 	mutex_init(&dev->i2c_mutex);
729 
730 	/* Initialize */
731 	memcpy(cmd.args, "\xc0\x12\x00\x0c\x00\x0d\x16\x00\x00\x00\x00\x00\x00", 13);
732 	cmd.wlen = 13;
733 	cmd.rlen = 0;
734 	ret = si2168_cmd_execute(client, &cmd);
735 	if (ret)
736 		goto err_kfree;
737 
738 	/* Power up */
739 	memcpy(cmd.args, "\xc0\x06\x01\x0f\x00\x20\x20\x01", 8);
740 	cmd.wlen = 8;
741 	cmd.rlen = 1;
742 	ret = si2168_cmd_execute(client, &cmd);
743 	if (ret)
744 		goto err_kfree;
745 
746 	/* Query chip revision */
747 	memcpy(cmd.args, "\x02", 1);
748 	cmd.wlen = 1;
749 	cmd.rlen = 13;
750 	ret = si2168_cmd_execute(client, &cmd);
751 	if (ret)
752 		goto err_kfree;
753 
754 	dev->chip_id = cmd.args[1] << 24 | cmd.args[2] << 16 |
755 		       cmd.args[3] << 8 | cmd.args[4] << 0;
756 
757 	switch (dev->chip_id) {
758 	case SI2168_CHIP_ID_A20:
759 		dev->firmware_name = SI2168_A20_FIRMWARE;
760 		break;
761 	case SI2168_CHIP_ID_A30:
762 		dev->firmware_name = SI2168_A30_FIRMWARE;
763 		break;
764 	case SI2168_CHIP_ID_B40:
765 		dev->firmware_name = SI2168_B40_FIRMWARE;
766 		break;
767 	case SI2168_CHIP_ID_D60:
768 		dev->firmware_name = SI2168_D60_FIRMWARE;
769 		break;
770 	default:
771 		dev_dbg(&client->dev, "unknown chip version Si21%d-%c%c%c\n",
772 			cmd.args[2], cmd.args[1], cmd.args[3], cmd.args[4]);
773 		ret = -ENODEV;
774 		goto err_kfree;
775 	}
776 
777 	dev->version = (cmd.args[1]) << 24 | (cmd.args[3] - '0') << 16 |
778 		       (cmd.args[4] - '0') << 8 | (cmd.args[5]) << 0;
779 
780 	/* create mux i2c adapter for tuner */
781 	dev->muxc = i2c_mux_alloc(client->adapter, &client->dev,
782 				  1, 0, I2C_MUX_LOCKED,
783 				  si2168_select, si2168_deselect);
784 	if (!dev->muxc) {
785 		ret = -ENOMEM;
786 		goto err_kfree;
787 	}
788 	dev->muxc->priv = client;
789 	ret = i2c_mux_add_adapter(dev->muxc, 0, 0, 0);
790 	if (ret)
791 		goto err_kfree;
792 
793 	/* create dvb_frontend */
794 	memcpy(&dev->fe.ops, &si2168_ops, sizeof(struct dvb_frontend_ops));
795 	dev->fe.demodulator_priv = client;
796 	*config->i2c_adapter = dev->muxc->adapter[0];
797 	*config->fe = &dev->fe;
798 	dev->ts_mode = config->ts_mode;
799 	dev->ts_clock_inv = config->ts_clock_inv;
800 	dev->ts_clock_gapped = config->ts_clock_gapped;
801 
802 	dev_info(&client->dev, "Silicon Labs Si2168-%c%d%d successfully identified\n",
803 		 dev->version >> 24 & 0xff, dev->version >> 16 & 0xff,
804 		 dev->version >> 8 & 0xff);
805 	dev_info(&client->dev, "firmware version: %c %d.%d.%d\n",
806 		 dev->version >> 24 & 0xff, dev->version >> 16 & 0xff,
807 		 dev->version >> 8 & 0xff, dev->version >> 0 & 0xff);
808 
809 	return 0;
810 err_kfree:
811 	kfree(dev);
812 err:
813 	dev_dbg(&client->dev, "failed=%d\n", ret);
814 	return ret;
815 }
816 
817 static int si2168_remove(struct i2c_client *client)
818 {
819 	struct si2168_dev *dev = i2c_get_clientdata(client);
820 
821 	dev_dbg(&client->dev, "\n");
822 
823 	i2c_mux_del_adapters(dev->muxc);
824 
825 	dev->fe.ops.release = NULL;
826 	dev->fe.demodulator_priv = NULL;
827 
828 	kfree(dev);
829 
830 	return 0;
831 }
832 
833 static const struct i2c_device_id si2168_id_table[] = {
834 	{"si2168", 0},
835 	{}
836 };
837 MODULE_DEVICE_TABLE(i2c, si2168_id_table);
838 
839 static struct i2c_driver si2168_driver = {
840 	.driver = {
841 		.name                = "si2168",
842 		.suppress_bind_attrs = true,
843 	},
844 	.probe		= si2168_probe,
845 	.remove		= si2168_remove,
846 	.id_table	= si2168_id_table,
847 };
848 
849 module_i2c_driver(si2168_driver);
850 
851 MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
852 MODULE_DESCRIPTION("Silicon Labs Si2168 DVB-T/T2/C demodulator driver");
853 MODULE_LICENSE("GPL");
854 MODULE_FIRMWARE(SI2168_A20_FIRMWARE);
855 MODULE_FIRMWARE(SI2168_A30_FIRMWARE);
856 MODULE_FIRMWARE(SI2168_B40_FIRMWARE);
857 MODULE_FIRMWARE(SI2168_D60_FIRMWARE);
858