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 	if (dev->spectral_inversion)
343 		cmd.args[5] |= 1;
344 	cmd.wlen = 6;
345 	cmd.rlen = 4;
346 	ret = si2168_cmd_execute(client, &cmd);
347 	if (ret)
348 		goto err;
349 
350 	/* set DVB-C symbol rate */
351 	if (c->delivery_system == SYS_DVBC_ANNEX_A) {
352 		memcpy(cmd.args, "\x14\x00\x02\x11", 4);
353 		cmd.args[4] = ((c->symbol_rate / 1000) >> 0) & 0xff;
354 		cmd.args[5] = ((c->symbol_rate / 1000) >> 8) & 0xff;
355 		cmd.wlen = 6;
356 		cmd.rlen = 4;
357 		ret = si2168_cmd_execute(client, &cmd);
358 		if (ret)
359 			goto err;
360 	}
361 
362 	memcpy(cmd.args, "\x14\x00\x0f\x10\x10\x00", 6);
363 	cmd.wlen = 6;
364 	cmd.rlen = 4;
365 	ret = si2168_cmd_execute(client, &cmd);
366 	if (ret)
367 		goto err;
368 
369 	memcpy(cmd.args, "\x14\x00\x09\x10\xe3\x08", 6);
370 	cmd.args[5] |= dev->ts_clock_inv ? 0x00 : 0x10;
371 	cmd.wlen = 6;
372 	cmd.rlen = 4;
373 	ret = si2168_cmd_execute(client, &cmd);
374 	if (ret)
375 		goto err;
376 
377 	memcpy(cmd.args, "\x14\x00\x08\x10\xd7\x05", 6);
378 	cmd.args[5] |= dev->ts_clock_inv ? 0x00 : 0x10;
379 	cmd.wlen = 6;
380 	cmd.rlen = 4;
381 	ret = si2168_cmd_execute(client, &cmd);
382 	if (ret)
383 		goto err;
384 
385 	memcpy(cmd.args, "\x14\x00\x01\x12\x00\x00", 6);
386 	cmd.wlen = 6;
387 	cmd.rlen = 4;
388 	ret = si2168_cmd_execute(client, &cmd);
389 	if (ret)
390 		goto err;
391 
392 	memcpy(cmd.args, "\x14\x00\x01\x03\x0c\x00", 6);
393 	cmd.wlen = 6;
394 	cmd.rlen = 4;
395 	ret = si2168_cmd_execute(client, &cmd);
396 	if (ret)
397 		goto err;
398 
399 	memcpy(cmd.args, "\x85", 1);
400 	cmd.wlen = 1;
401 	cmd.rlen = 1;
402 	ret = si2168_cmd_execute(client, &cmd);
403 	if (ret)
404 		goto err;
405 
406 	dev->delivery_system = c->delivery_system;
407 
408 	return 0;
409 err:
410 	dev_dbg(&client->dev, "failed=%d\n", ret);
411 	return ret;
412 }
413 
414 static int si2168_ts_bus_ctrl(struct dvb_frontend *fe, int acquire)
415 {
416 	struct i2c_client *client = fe->demodulator_priv;
417 	struct si2168_dev *dev = i2c_get_clientdata(client);
418 	struct si2168_cmd cmd;
419 	int ret = 0;
420 
421 	dev_dbg(&client->dev, "%s acquire: %d\n", __func__, acquire);
422 
423 	/* set TS_MODE property */
424 	memcpy(cmd.args, "\x14\x00\x01\x10\x10\x00", 6);
425 	if (acquire)
426 		cmd.args[4] |= dev->ts_mode;
427 	else
428 		cmd.args[4] |= SI2168_TS_TRISTATE;
429 	if (dev->ts_clock_gapped)
430 		cmd.args[4] |= 0x40;
431 	cmd.wlen = 6;
432 	cmd.rlen = 4;
433 	ret = si2168_cmd_execute(client, &cmd);
434 
435 	return ret;
436 }
437 
438 static int si2168_init(struct dvb_frontend *fe)
439 {
440 	struct i2c_client *client = fe->demodulator_priv;
441 	struct si2168_dev *dev = i2c_get_clientdata(client);
442 	struct dtv_frontend_properties *c = &fe->dtv_property_cache;
443 	int ret, len, remaining;
444 	const struct firmware *fw;
445 	struct si2168_cmd cmd;
446 
447 	dev_dbg(&client->dev, "\n");
448 
449 	/* initialize */
450 	memcpy(cmd.args, "\xc0\x12\x00\x0c\x00\x0d\x16\x00\x00\x00\x00\x00\x00", 13);
451 	cmd.wlen = 13;
452 	cmd.rlen = 0;
453 	ret = si2168_cmd_execute(client, &cmd);
454 	if (ret)
455 		goto err;
456 
457 	if (dev->warm) {
458 		/* resume */
459 		memcpy(cmd.args, "\xc0\x06\x08\x0f\x00\x20\x21\x01", 8);
460 		cmd.wlen = 8;
461 		cmd.rlen = 1;
462 		ret = si2168_cmd_execute(client, &cmd);
463 		if (ret)
464 			goto err;
465 
466 		udelay(100);
467 		memcpy(cmd.args, "\x85", 1);
468 		cmd.wlen = 1;
469 		cmd.rlen = 1;
470 		ret = si2168_cmd_execute(client, &cmd);
471 		if (ret)
472 			goto err;
473 
474 		goto warm;
475 	}
476 
477 	/* power up */
478 	memcpy(cmd.args, "\xc0\x06\x01\x0f\x00\x20\x20\x01", 8);
479 	cmd.wlen = 8;
480 	cmd.rlen = 1;
481 	ret = si2168_cmd_execute(client, &cmd);
482 	if (ret)
483 		goto err;
484 
485 	/* request the firmware, this will block and timeout */
486 	ret = request_firmware(&fw, dev->firmware_name, &client->dev);
487 	if (ret) {
488 		/* fallback mechanism to handle old name for Si2168 B40 fw */
489 		if (dev->chip_id == SI2168_CHIP_ID_B40) {
490 			dev->firmware_name = SI2168_B40_FIRMWARE_FALLBACK;
491 			ret = request_firmware(&fw, dev->firmware_name,
492 					       &client->dev);
493 		}
494 
495 		if (ret == 0) {
496 			dev_notice(&client->dev,
497 					"please install firmware file '%s'\n",
498 					SI2168_B40_FIRMWARE);
499 		} else {
500 			dev_err(&client->dev,
501 					"firmware file '%s' not found\n",
502 					dev->firmware_name);
503 			goto err_release_firmware;
504 		}
505 	}
506 
507 	dev_info(&client->dev, "downloading firmware from file '%s'\n",
508 			dev->firmware_name);
509 
510 	if ((fw->size % 17 == 0) && (fw->data[0] > 5)) {
511 		/* firmware is in the new format */
512 		for (remaining = fw->size; remaining > 0; remaining -= 17) {
513 			len = fw->data[fw->size - remaining];
514 			if (len > SI2168_ARGLEN) {
515 				ret = -EINVAL;
516 				break;
517 			}
518 			memcpy(cmd.args, &fw->data[(fw->size - remaining) + 1], len);
519 			cmd.wlen = len;
520 			cmd.rlen = 1;
521 			ret = si2168_cmd_execute(client, &cmd);
522 			if (ret)
523 				break;
524 		}
525 	} else if (fw->size % 8 == 0) {
526 		/* firmware is in the old format */
527 		for (remaining = fw->size; remaining > 0; remaining -= 8) {
528 			len = 8;
529 			memcpy(cmd.args, &fw->data[fw->size - remaining], len);
530 			cmd.wlen = len;
531 			cmd.rlen = 1;
532 			ret = si2168_cmd_execute(client, &cmd);
533 			if (ret)
534 				break;
535 		}
536 	} else {
537 		/* bad or unknown firmware format */
538 		ret = -EINVAL;
539 	}
540 
541 	if (ret) {
542 		dev_err(&client->dev, "firmware download failed %d\n", ret);
543 		goto err_release_firmware;
544 	}
545 
546 	release_firmware(fw);
547 
548 	memcpy(cmd.args, "\x01\x01", 2);
549 	cmd.wlen = 2;
550 	cmd.rlen = 1;
551 	ret = si2168_cmd_execute(client, &cmd);
552 	if (ret)
553 		goto err;
554 
555 	/* query firmware version */
556 	memcpy(cmd.args, "\x11", 1);
557 	cmd.wlen = 1;
558 	cmd.rlen = 10;
559 	ret = si2168_cmd_execute(client, &cmd);
560 	if (ret)
561 		goto err;
562 
563 	dev->version = (cmd.args[9] + '@') << 24 | (cmd.args[6] - '0') << 16 |
564 		       (cmd.args[7] - '0') << 8 | (cmd.args[8]) << 0;
565 	dev_info(&client->dev, "firmware version: %c %d.%d.%d\n",
566 		 dev->version >> 24 & 0xff, dev->version >> 16 & 0xff,
567 		 dev->version >> 8 & 0xff, dev->version >> 0 & 0xff);
568 
569 	ret = si2168_ts_bus_ctrl(fe, 1);
570 	if (ret)
571 		goto err;
572 
573 	dev->warm = true;
574 warm:
575 	/* Init stats here to indicate which stats are supported */
576 	c->cnr.len = 1;
577 	c->cnr.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
578 	c->post_bit_error.len = 1;
579 	c->post_bit_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
580 	c->post_bit_count.len = 1;
581 	c->post_bit_count.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
582 	c->block_error.len = 1;
583 	c->block_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
584 
585 	dev->active = true;
586 
587 	return 0;
588 err_release_firmware:
589 	release_firmware(fw);
590 err:
591 	dev_dbg(&client->dev, "failed=%d\n", ret);
592 	return ret;
593 }
594 
595 static int si2168_sleep(struct dvb_frontend *fe)
596 {
597 	struct i2c_client *client = fe->demodulator_priv;
598 	struct si2168_dev *dev = i2c_get_clientdata(client);
599 	int ret;
600 	struct si2168_cmd cmd;
601 
602 	dev_dbg(&client->dev, "\n");
603 
604 	dev->active = false;
605 
606 	/* tri-state data bus */
607 	si2168_ts_bus_ctrl(fe, 0);
608 
609 	/* Firmware B 4.0-11 or later loses warm state during sleep */
610 	if (dev->version > ('B' << 24 | 4 << 16 | 0 << 8 | 11 << 0))
611 		dev->warm = false;
612 
613 	memcpy(cmd.args, "\x13", 1);
614 	cmd.wlen = 1;
615 	cmd.rlen = 0;
616 	ret = si2168_cmd_execute(client, &cmd);
617 	if (ret)
618 		goto err;
619 
620 	return 0;
621 err:
622 	dev_dbg(&client->dev, "failed=%d\n", ret);
623 	return ret;
624 }
625 
626 static int si2168_get_tune_settings(struct dvb_frontend *fe,
627 	struct dvb_frontend_tune_settings *s)
628 {
629 	s->min_delay_ms = 900;
630 
631 	return 0;
632 }
633 
634 static int si2168_select(struct i2c_mux_core *muxc, u32 chan)
635 {
636 	struct i2c_client *client = i2c_mux_priv(muxc);
637 	int ret;
638 	struct si2168_cmd cmd;
639 
640 	/* open I2C gate */
641 	memcpy(cmd.args, "\xc0\x0d\x01", 3);
642 	cmd.wlen = 3;
643 	cmd.rlen = 0;
644 	ret = si2168_cmd_execute(client, &cmd);
645 	if (ret)
646 		goto err;
647 
648 	return 0;
649 err:
650 	dev_dbg(&client->dev, "failed=%d\n", ret);
651 	return ret;
652 }
653 
654 static int si2168_deselect(struct i2c_mux_core *muxc, u32 chan)
655 {
656 	struct i2c_client *client = i2c_mux_priv(muxc);
657 	int ret;
658 	struct si2168_cmd cmd;
659 
660 	/* close I2C gate */
661 	memcpy(cmd.args, "\xc0\x0d\x00", 3);
662 	cmd.wlen = 3;
663 	cmd.rlen = 0;
664 	ret = si2168_cmd_execute(client, &cmd);
665 	if (ret)
666 		goto err;
667 
668 	return 0;
669 err:
670 	dev_dbg(&client->dev, "failed=%d\n", ret);
671 	return ret;
672 }
673 
674 static const struct dvb_frontend_ops si2168_ops = {
675 	.delsys = {SYS_DVBT, SYS_DVBT2, SYS_DVBC_ANNEX_A},
676 	.info = {
677 		.name = "Silicon Labs Si2168",
678 		.symbol_rate_min = 1000000,
679 		.symbol_rate_max = 7200000,
680 		.caps =	FE_CAN_FEC_1_2 |
681 			FE_CAN_FEC_2_3 |
682 			FE_CAN_FEC_3_4 |
683 			FE_CAN_FEC_5_6 |
684 			FE_CAN_FEC_7_8 |
685 			FE_CAN_FEC_AUTO |
686 			FE_CAN_QPSK |
687 			FE_CAN_QAM_16 |
688 			FE_CAN_QAM_32 |
689 			FE_CAN_QAM_64 |
690 			FE_CAN_QAM_128 |
691 			FE_CAN_QAM_256 |
692 			FE_CAN_QAM_AUTO |
693 			FE_CAN_TRANSMISSION_MODE_AUTO |
694 			FE_CAN_GUARD_INTERVAL_AUTO |
695 			FE_CAN_HIERARCHY_AUTO |
696 			FE_CAN_MUTE_TS |
697 			FE_CAN_2G_MODULATION |
698 			FE_CAN_MULTISTREAM
699 	},
700 
701 	.get_tune_settings = si2168_get_tune_settings,
702 
703 	.init = si2168_init,
704 	.sleep = si2168_sleep,
705 
706 	.ts_bus_ctrl          = si2168_ts_bus_ctrl,
707 
708 	.set_frontend = si2168_set_frontend,
709 
710 	.read_status = si2168_read_status,
711 };
712 
713 static int si2168_probe(struct i2c_client *client,
714 		const struct i2c_device_id *id)
715 {
716 	struct si2168_config *config = client->dev.platform_data;
717 	struct si2168_dev *dev;
718 	int ret;
719 	struct si2168_cmd cmd;
720 
721 	dev_dbg(&client->dev, "\n");
722 
723 	dev = kzalloc(sizeof(*dev), GFP_KERNEL);
724 	if (!dev) {
725 		ret = -ENOMEM;
726 		goto err;
727 	}
728 
729 	i2c_set_clientdata(client, dev);
730 	mutex_init(&dev->i2c_mutex);
731 
732 	/* Initialize */
733 	memcpy(cmd.args, "\xc0\x12\x00\x0c\x00\x0d\x16\x00\x00\x00\x00\x00\x00", 13);
734 	cmd.wlen = 13;
735 	cmd.rlen = 0;
736 	ret = si2168_cmd_execute(client, &cmd);
737 	if (ret)
738 		goto err_kfree;
739 
740 	/* Power up */
741 	memcpy(cmd.args, "\xc0\x06\x01\x0f\x00\x20\x20\x01", 8);
742 	cmd.wlen = 8;
743 	cmd.rlen = 1;
744 	ret = si2168_cmd_execute(client, &cmd);
745 	if (ret)
746 		goto err_kfree;
747 
748 	/* Query chip revision */
749 	memcpy(cmd.args, "\x02", 1);
750 	cmd.wlen = 1;
751 	cmd.rlen = 13;
752 	ret = si2168_cmd_execute(client, &cmd);
753 	if (ret)
754 		goto err_kfree;
755 
756 	dev->chip_id = cmd.args[1] << 24 | cmd.args[2] << 16 |
757 		       cmd.args[3] << 8 | cmd.args[4] << 0;
758 
759 	switch (dev->chip_id) {
760 	case SI2168_CHIP_ID_A20:
761 		dev->firmware_name = SI2168_A20_FIRMWARE;
762 		break;
763 	case SI2168_CHIP_ID_A30:
764 		dev->firmware_name = SI2168_A30_FIRMWARE;
765 		break;
766 	case SI2168_CHIP_ID_B40:
767 		dev->firmware_name = SI2168_B40_FIRMWARE;
768 		break;
769 	case SI2168_CHIP_ID_D60:
770 		dev->firmware_name = SI2168_D60_FIRMWARE;
771 		break;
772 	default:
773 		dev_dbg(&client->dev, "unknown chip version Si21%d-%c%c%c\n",
774 			cmd.args[2], cmd.args[1], cmd.args[3], cmd.args[4]);
775 		ret = -ENODEV;
776 		goto err_kfree;
777 	}
778 
779 	dev->version = (cmd.args[1]) << 24 | (cmd.args[3] - '0') << 16 |
780 		       (cmd.args[4] - '0') << 8 | (cmd.args[5]) << 0;
781 
782 	/* create mux i2c adapter for tuner */
783 	dev->muxc = i2c_mux_alloc(client->adapter, &client->dev,
784 				  1, 0, I2C_MUX_LOCKED,
785 				  si2168_select, si2168_deselect);
786 	if (!dev->muxc) {
787 		ret = -ENOMEM;
788 		goto err_kfree;
789 	}
790 	dev->muxc->priv = client;
791 	ret = i2c_mux_add_adapter(dev->muxc, 0, 0, 0);
792 	if (ret)
793 		goto err_kfree;
794 
795 	/* create dvb_frontend */
796 	memcpy(&dev->fe.ops, &si2168_ops, sizeof(struct dvb_frontend_ops));
797 	dev->fe.demodulator_priv = client;
798 	*config->i2c_adapter = dev->muxc->adapter[0];
799 	*config->fe = &dev->fe;
800 	dev->ts_mode = config->ts_mode;
801 	dev->ts_clock_inv = config->ts_clock_inv;
802 	dev->ts_clock_gapped = config->ts_clock_gapped;
803 	dev->spectral_inversion = config->spectral_inversion;
804 
805 	dev_info(&client->dev, "Silicon Labs Si2168-%c%d%d successfully identified\n",
806 		 dev->version >> 24 & 0xff, dev->version >> 16 & 0xff,
807 		 dev->version >> 8 & 0xff);
808 	dev_info(&client->dev, "firmware version: %c %d.%d.%d\n",
809 		 dev->version >> 24 & 0xff, dev->version >> 16 & 0xff,
810 		 dev->version >> 8 & 0xff, dev->version >> 0 & 0xff);
811 
812 	return 0;
813 err_kfree:
814 	kfree(dev);
815 err:
816 	dev_warn(&client->dev, "probe failed = %d\n", ret);
817 	return ret;
818 }
819 
820 static int si2168_remove(struct i2c_client *client)
821 {
822 	struct si2168_dev *dev = i2c_get_clientdata(client);
823 
824 	dev_dbg(&client->dev, "\n");
825 
826 	i2c_mux_del_adapters(dev->muxc);
827 
828 	dev->fe.ops.release = NULL;
829 	dev->fe.demodulator_priv = NULL;
830 
831 	kfree(dev);
832 
833 	return 0;
834 }
835 
836 static const struct i2c_device_id si2168_id_table[] = {
837 	{"si2168", 0},
838 	{}
839 };
840 MODULE_DEVICE_TABLE(i2c, si2168_id_table);
841 
842 static struct i2c_driver si2168_driver = {
843 	.driver = {
844 		.name                = "si2168",
845 		.suppress_bind_attrs = true,
846 	},
847 	.probe		= si2168_probe,
848 	.remove		= si2168_remove,
849 	.id_table	= si2168_id_table,
850 };
851 
852 module_i2c_driver(si2168_driver);
853 
854 MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
855 MODULE_DESCRIPTION("Silicon Labs Si2168 DVB-T/T2/C demodulator driver");
856 MODULE_LICENSE("GPL");
857 MODULE_FIRMWARE(SI2168_A20_FIRMWARE);
858 MODULE_FIRMWARE(SI2168_A30_FIRMWARE);
859 MODULE_FIRMWARE(SI2168_B40_FIRMWARE);
860 MODULE_FIRMWARE(SI2168_D60_FIRMWARE);
861