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=%d\n",
159 			c->delivery_system, c->modulation,
160 			c->frequency, c->bandwidth_hz, c->symbol_rate,
161 			c->inversion, 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 <= 5000000)
184 		bandwidth = 0x05;
185 	else if (c->bandwidth_hz <= 6000000)
186 		bandwidth = 0x06;
187 	else if (c->bandwidth_hz <= 7000000)
188 		bandwidth = 0x07;
189 	else if (c->bandwidth_hz <= 8000000)
190 		bandwidth = 0x08;
191 	else if (c->bandwidth_hz <= 9000000)
192 		bandwidth = 0x09;
193 	else if (c->bandwidth_hz <= 10000000)
194 		bandwidth = 0x0a;
195 	else
196 		bandwidth = 0x0f;
197 
198 	/* program tuner */
199 	if (fe->ops.tuner_ops.set_params) {
200 		ret = fe->ops.tuner_ops.set_params(fe);
201 		if (ret)
202 			goto err;
203 	}
204 
205 	memcpy(cmd.args, "\x88\x02\x02\x02\x02", 5);
206 	cmd.wlen = 5;
207 	cmd.rlen = 5;
208 	ret = si2168_cmd_execute(client, &cmd);
209 	if (ret)
210 		goto err;
211 
212 	/* that has no big effect */
213 	if (c->delivery_system == SYS_DVBT)
214 		memcpy(cmd.args, "\x89\x21\x06\x11\xff\x98", 6);
215 	else if (c->delivery_system == SYS_DVBC_ANNEX_A)
216 		memcpy(cmd.args, "\x89\x21\x06\x11\x89\xf0", 6);
217 	else if (c->delivery_system == SYS_DVBT2)
218 		memcpy(cmd.args, "\x89\x21\x06\x11\x89\x20", 6);
219 	cmd.wlen = 6;
220 	cmd.rlen = 3;
221 	ret = si2168_cmd_execute(client, &cmd);
222 	if (ret)
223 		goto err;
224 
225 	if (c->delivery_system == SYS_DVBT2) {
226 		/* select PLP */
227 		cmd.args[0] = 0x52;
228 		cmd.args[1] = c->stream_id & 0xff;
229 		cmd.args[2] = c->stream_id == NO_STREAM_ID_FILTER ? 0 : 1;
230 		cmd.wlen = 3;
231 		cmd.rlen = 1;
232 		ret = si2168_cmd_execute(client, &cmd);
233 		if (ret)
234 			goto err;
235 	}
236 
237 	memcpy(cmd.args, "\x51\x03", 2);
238 	cmd.wlen = 2;
239 	cmd.rlen = 12;
240 	ret = si2168_cmd_execute(client, &cmd);
241 	if (ret)
242 		goto err;
243 
244 	memcpy(cmd.args, "\x12\x08\x04", 3);
245 	cmd.wlen = 3;
246 	cmd.rlen = 3;
247 	ret = si2168_cmd_execute(client, &cmd);
248 	if (ret)
249 		goto err;
250 
251 	memcpy(cmd.args, "\x14\x00\x0c\x10\x12\x00", 6);
252 	cmd.wlen = 6;
253 	cmd.rlen = 4;
254 	ret = si2168_cmd_execute(client, &cmd);
255 	if (ret)
256 		goto err;
257 
258 	memcpy(cmd.args, "\x14\x00\x06\x10\x24\x00", 6);
259 	cmd.wlen = 6;
260 	cmd.rlen = 4;
261 	ret = si2168_cmd_execute(client, &cmd);
262 	if (ret)
263 		goto err;
264 
265 	memcpy(cmd.args, "\x14\x00\x07\x10\x00\x24", 6);
266 	cmd.wlen = 6;
267 	cmd.rlen = 4;
268 	ret = si2168_cmd_execute(client, &cmd);
269 	if (ret)
270 		goto err;
271 
272 	memcpy(cmd.args, "\x14\x00\x0a\x10\x00\x00", 6);
273 	cmd.args[4] = delivery_system | bandwidth;
274 	cmd.wlen = 6;
275 	cmd.rlen = 4;
276 	ret = si2168_cmd_execute(client, &cmd);
277 	if (ret)
278 		goto err;
279 
280 	/* set DVB-C symbol rate */
281 	if (c->delivery_system == SYS_DVBC_ANNEX_A) {
282 		memcpy(cmd.args, "\x14\x00\x02\x11", 4);
283 		cmd.args[4] = ((c->symbol_rate / 1000) >> 0) & 0xff;
284 		cmd.args[5] = ((c->symbol_rate / 1000) >> 8) & 0xff;
285 		cmd.wlen = 6;
286 		cmd.rlen = 4;
287 		ret = si2168_cmd_execute(client, &cmd);
288 		if (ret)
289 			goto err;
290 	}
291 
292 	memcpy(cmd.args, "\x14\x00\x0f\x10\x10\x00", 6);
293 	cmd.wlen = 6;
294 	cmd.rlen = 4;
295 	ret = si2168_cmd_execute(client, &cmd);
296 	if (ret)
297 		goto err;
298 
299 	memcpy(cmd.args, "\x14\x00\x09\x10\xe3\x08", 6);
300 	cmd.args[5] |= dev->ts_clock_inv ? 0x00 : 0x10;
301 	cmd.wlen = 6;
302 	cmd.rlen = 4;
303 	ret = si2168_cmd_execute(client, &cmd);
304 	if (ret)
305 		goto err;
306 
307 	memcpy(cmd.args, "\x14\x00\x08\x10\xd7\x05", 6);
308 	cmd.args[5] |= dev->ts_clock_inv ? 0x00 : 0x10;
309 	cmd.wlen = 6;
310 	cmd.rlen = 4;
311 	ret = si2168_cmd_execute(client, &cmd);
312 	if (ret)
313 		goto err;
314 
315 	memcpy(cmd.args, "\x14\x00\x01\x12\x00\x00", 6);
316 	cmd.wlen = 6;
317 	cmd.rlen = 4;
318 	ret = si2168_cmd_execute(client, &cmd);
319 	if (ret)
320 		goto err;
321 
322 	memcpy(cmd.args, "\x14\x00\x01\x03\x0c\x00", 6);
323 	cmd.wlen = 6;
324 	cmd.rlen = 4;
325 	ret = si2168_cmd_execute(client, &cmd);
326 	if (ret)
327 		goto err;
328 
329 	memcpy(cmd.args, "\x85", 1);
330 	cmd.wlen = 1;
331 	cmd.rlen = 1;
332 	ret = si2168_cmd_execute(client, &cmd);
333 	if (ret)
334 		goto err;
335 
336 	dev->delivery_system = c->delivery_system;
337 
338 	return 0;
339 err:
340 	dev_dbg(&client->dev, "failed=%d\n", ret);
341 	return ret;
342 }
343 
344 static int si2168_init(struct dvb_frontend *fe)
345 {
346 	struct i2c_client *client = fe->demodulator_priv;
347 	struct si2168_dev *dev = i2c_get_clientdata(client);
348 	int ret, len, remaining;
349 	const struct firmware *fw = NULL;
350 	u8 *fw_file;
351 	const unsigned int i2c_wr_max = 8;
352 	struct si2168_cmd cmd;
353 	unsigned int chip_id;
354 
355 	dev_dbg(&client->dev, "\n");
356 
357 	/* initialize */
358 	memcpy(cmd.args, "\xc0\x12\x00\x0c\x00\x0d\x16\x00\x00\x00\x00\x00\x00", 13);
359 	cmd.wlen = 13;
360 	cmd.rlen = 0;
361 	ret = si2168_cmd_execute(client, &cmd);
362 	if (ret)
363 		goto err;
364 
365 	if (dev->fw_loaded) {
366 		/* resume */
367 		memcpy(cmd.args, "\xc0\x06\x08\x0f\x00\x20\x21\x01", 8);
368 		cmd.wlen = 8;
369 		cmd.rlen = 1;
370 		ret = si2168_cmd_execute(client, &cmd);
371 		if (ret)
372 			goto err;
373 
374 		memcpy(cmd.args, "\x85", 1);
375 		cmd.wlen = 1;
376 		cmd.rlen = 1;
377 		ret = si2168_cmd_execute(client, &cmd);
378 		if (ret)
379 			goto err;
380 
381 		goto warm;
382 	}
383 
384 	/* power up */
385 	memcpy(cmd.args, "\xc0\x06\x01\x0f\x00\x20\x20\x01", 8);
386 	cmd.wlen = 8;
387 	cmd.rlen = 1;
388 	ret = si2168_cmd_execute(client, &cmd);
389 	if (ret)
390 		goto err;
391 
392 	/* query chip revision */
393 	memcpy(cmd.args, "\x02", 1);
394 	cmd.wlen = 1;
395 	cmd.rlen = 13;
396 	ret = si2168_cmd_execute(client, &cmd);
397 	if (ret)
398 		goto err;
399 
400 	chip_id = cmd.args[1] << 24 | cmd.args[2] << 16 | cmd.args[3] << 8 |
401 			cmd.args[4] << 0;
402 
403 	#define SI2168_A20 ('A' << 24 | 68 << 16 | '2' << 8 | '0' << 0)
404 	#define SI2168_A30 ('A' << 24 | 68 << 16 | '3' << 8 | '0' << 0)
405 	#define SI2168_B40 ('B' << 24 | 68 << 16 | '4' << 8 | '0' << 0)
406 
407 	switch (chip_id) {
408 	case SI2168_A20:
409 		fw_file = SI2168_A20_FIRMWARE;
410 		break;
411 	case SI2168_A30:
412 		fw_file = SI2168_A30_FIRMWARE;
413 		break;
414 	case SI2168_B40:
415 		fw_file = SI2168_B40_FIRMWARE;
416 		break;
417 	default:
418 		dev_err(&client->dev,
419 				"unknown chip version Si21%d-%c%c%c\n",
420 				cmd.args[2], cmd.args[1],
421 				cmd.args[3], cmd.args[4]);
422 		ret = -EINVAL;
423 		goto err;
424 	}
425 
426 	/* cold state - try to download firmware */
427 	dev_info(&client->dev, "found a '%s' in cold state\n",
428 			si2168_ops.info.name);
429 
430 	/* request the firmware, this will block and timeout */
431 	ret = request_firmware(&fw, fw_file, &client->dev);
432 	if (ret) {
433 		/* fallback mechanism to handle old name for Si2168 B40 fw */
434 		if (chip_id == SI2168_B40) {
435 			fw_file = SI2168_B40_FIRMWARE_FALLBACK;
436 			ret = request_firmware(&fw, fw_file, &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_file);
447 			goto err_release_firmware;
448 		}
449 	}
450 
451 	dev_info(&client->dev, "downloading firmware from file '%s'\n",
452 			fw_file);
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 				dev_err(&client->dev,
464 						"firmware download failed=%d\n",
465 						ret);
466 				goto err_release_firmware;
467 			}
468 		}
469 	} else {
470 		/* firmware is in the old format */
471 		for (remaining = fw->size; remaining > 0; remaining -= i2c_wr_max) {
472 			len = remaining;
473 			if (len > i2c_wr_max)
474 				len = i2c_wr_max;
475 
476 			memcpy(cmd.args, &fw->data[fw->size - remaining], len);
477 			cmd.wlen = len;
478 			cmd.rlen = 1;
479 			ret = si2168_cmd_execute(client, &cmd);
480 			if (ret) {
481 				dev_err(&client->dev,
482 						"firmware download failed=%d\n",
483 						ret);
484 				goto err_release_firmware;
485 			}
486 		}
487 	}
488 
489 	release_firmware(fw);
490 	fw = NULL;
491 
492 	memcpy(cmd.args, "\x01\x01", 2);
493 	cmd.wlen = 2;
494 	cmd.rlen = 1;
495 	ret = si2168_cmd_execute(client, &cmd);
496 	if (ret)
497 		goto err;
498 
499 	/* query firmware version */
500 	memcpy(cmd.args, "\x11", 1);
501 	cmd.wlen = 1;
502 	cmd.rlen = 10;
503 	ret = si2168_cmd_execute(client, &cmd);
504 	if (ret)
505 		goto err;
506 
507 	dev_info(&client->dev, "firmware version: %c.%c.%d\n",
508 			cmd.args[6], cmd.args[7], cmd.args[8]);
509 
510 	/* set ts mode */
511 	memcpy(cmd.args, "\x14\x00\x01\x10\x10\x00", 6);
512 	cmd.args[4] |= dev->ts_mode;
513 	cmd.wlen = 6;
514 	cmd.rlen = 4;
515 	ret = si2168_cmd_execute(client, &cmd);
516 	if (ret)
517 		goto err;
518 
519 	dev->fw_loaded = true;
520 
521 	dev_info(&client->dev, "found a '%s' in warm state\n",
522 			si2168_ops.info.name);
523 warm:
524 	dev->active = true;
525 
526 	return 0;
527 err_release_firmware:
528 	release_firmware(fw);
529 err:
530 	dev_dbg(&client->dev, "failed=%d\n", ret);
531 	return ret;
532 }
533 
534 static int si2168_sleep(struct dvb_frontend *fe)
535 {
536 	struct i2c_client *client = fe->demodulator_priv;
537 	struct si2168_dev *dev = i2c_get_clientdata(client);
538 	int ret;
539 	struct si2168_cmd cmd;
540 
541 	dev_dbg(&client->dev, "\n");
542 
543 	dev->active = false;
544 
545 	memcpy(cmd.args, "\x13", 1);
546 	cmd.wlen = 1;
547 	cmd.rlen = 0;
548 	ret = si2168_cmd_execute(client, &cmd);
549 	if (ret)
550 		goto err;
551 
552 	return 0;
553 err:
554 	dev_dbg(&client->dev, "failed=%d\n", ret);
555 	return ret;
556 }
557 
558 static int si2168_get_tune_settings(struct dvb_frontend *fe,
559 	struct dvb_frontend_tune_settings *s)
560 {
561 	s->min_delay_ms = 900;
562 
563 	return 0;
564 }
565 
566 /*
567  * I2C gate logic
568  * We must use unlocked i2c_transfer() here because I2C lock is already taken
569  * by tuner driver.
570  */
571 static int si2168_select(struct i2c_adapter *adap, void *mux_priv, u32 chan)
572 {
573 	struct i2c_client *client = mux_priv;
574 	struct si2168_dev *dev = i2c_get_clientdata(client);
575 	int ret;
576 	struct i2c_msg gate_open_msg = {
577 		.addr = client->addr,
578 		.flags = 0,
579 		.len = 3,
580 		.buf = "\xc0\x0d\x01",
581 	};
582 
583 	mutex_lock(&dev->i2c_mutex);
584 
585 	/* open tuner I2C gate */
586 	ret = __i2c_transfer(client->adapter, &gate_open_msg, 1);
587 	if (ret != 1) {
588 		dev_warn(&client->dev, "i2c write failed=%d\n", ret);
589 		if (ret >= 0)
590 			ret = -EREMOTEIO;
591 	} else {
592 		ret = 0;
593 	}
594 
595 	return ret;
596 }
597 
598 static int si2168_deselect(struct i2c_adapter *adap, void *mux_priv, u32 chan)
599 {
600 	struct i2c_client *client = mux_priv;
601 	struct si2168_dev *dev = i2c_get_clientdata(client);
602 	int ret;
603 	struct i2c_msg gate_close_msg = {
604 		.addr = client->addr,
605 		.flags = 0,
606 		.len = 3,
607 		.buf = "\xc0\x0d\x00",
608 	};
609 
610 	/* close tuner I2C gate */
611 	ret = __i2c_transfer(client->adapter, &gate_close_msg, 1);
612 	if (ret != 1) {
613 		dev_warn(&client->dev, "i2c write failed=%d\n", ret);
614 		if (ret >= 0)
615 			ret = -EREMOTEIO;
616 	} else {
617 		ret = 0;
618 	}
619 
620 	mutex_unlock(&dev->i2c_mutex);
621 
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 
669 	dev_dbg(&client->dev, "\n");
670 
671 	dev = kzalloc(sizeof(*dev), GFP_KERNEL);
672 	if (!dev) {
673 		ret = -ENOMEM;
674 		dev_err(&client->dev, "kzalloc() failed\n");
675 		goto err_kfree;
676 	}
677 
678 	mutex_init(&dev->i2c_mutex);
679 
680 	/* create mux i2c adapter for tuner */
681 	dev->adapter = i2c_add_mux_adapter(client->adapter, &client->dev,
682 			client, 0, 0, 0, si2168_select, si2168_deselect);
683 	if (dev->adapter == NULL) {
684 		ret = -ENODEV;
685 		goto err_kfree;
686 	}
687 
688 	/* create dvb_frontend */
689 	memcpy(&dev->fe.ops, &si2168_ops, sizeof(struct dvb_frontend_ops));
690 	dev->fe.demodulator_priv = client;
691 	*config->i2c_adapter = dev->adapter;
692 	*config->fe = &dev->fe;
693 	dev->ts_mode = config->ts_mode;
694 	dev->ts_clock_inv = config->ts_clock_inv;
695 	dev->fw_loaded = false;
696 
697 	i2c_set_clientdata(client, dev);
698 
699 	dev_info(&client->dev, "Silicon Labs Si2168 successfully attached\n");
700 	return 0;
701 err_kfree:
702 	kfree(dev);
703 	dev_dbg(&client->dev, "failed=%d\n", ret);
704 	return ret;
705 }
706 
707 static int si2168_remove(struct i2c_client *client)
708 {
709 	struct si2168_dev *dev = i2c_get_clientdata(client);
710 
711 	dev_dbg(&client->dev, "\n");
712 
713 	i2c_del_mux_adapter(dev->adapter);
714 
715 	dev->fe.ops.release = NULL;
716 	dev->fe.demodulator_priv = NULL;
717 
718 	kfree(dev);
719 
720 	return 0;
721 }
722 
723 static const struct i2c_device_id si2168_id_table[] = {
724 	{"si2168", 0},
725 	{}
726 };
727 MODULE_DEVICE_TABLE(i2c, si2168_id_table);
728 
729 static struct i2c_driver si2168_driver = {
730 	.driver = {
731 		.owner	= THIS_MODULE,
732 		.name	= "si2168",
733 	},
734 	.probe		= si2168_probe,
735 	.remove		= si2168_remove,
736 	.id_table	= si2168_id_table,
737 };
738 
739 module_i2c_driver(si2168_driver);
740 
741 MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
742 MODULE_DESCRIPTION("Silicon Labs Si2168 DVB-T/T2/C demodulator driver");
743 MODULE_LICENSE("GPL");
744 MODULE_FIRMWARE(SI2168_A20_FIRMWARE);
745 MODULE_FIRMWARE(SI2168_A30_FIRMWARE);
746 MODULE_FIRMWARE(SI2168_B40_FIRMWARE);
747