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