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