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