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