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