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 <linux/delay.h>
18 
19 #include "si2168_priv.h"
20 
21 static const struct dvb_frontend_ops si2168_ops;
22 
23 /* execute firmware command */
24 static int si2168_cmd_execute(struct i2c_client *client, struct si2168_cmd *cmd)
25 {
26 	struct si2168_dev *dev = i2c_get_clientdata(client);
27 	int ret;
28 	unsigned long timeout;
29 
30 	mutex_lock(&dev->i2c_mutex);
31 
32 	if (cmd->wlen) {
33 		/* write cmd and args for firmware */
34 		ret = i2c_master_send(client, cmd->args, cmd->wlen);
35 		if (ret < 0) {
36 			goto err_mutex_unlock;
37 		} else if (ret != cmd->wlen) {
38 			ret = -EREMOTEIO;
39 			goto err_mutex_unlock;
40 		}
41 	}
42 
43 	if (cmd->rlen) {
44 		/* wait cmd execution terminate */
45 		#define TIMEOUT 70
46 		timeout = jiffies + msecs_to_jiffies(TIMEOUT);
47 		while (!time_after(jiffies, timeout)) {
48 			ret = i2c_master_recv(client, cmd->args, cmd->rlen);
49 			if (ret < 0) {
50 				goto err_mutex_unlock;
51 			} else if (ret != cmd->rlen) {
52 				ret = -EREMOTEIO;
53 				goto err_mutex_unlock;
54 			}
55 
56 			/* firmware ready? */
57 			if ((cmd->args[0] >> 7) & 0x01)
58 				break;
59 		}
60 
61 		dev_dbg(&client->dev, "cmd execution took %d ms\n",
62 				jiffies_to_msecs(jiffies) -
63 				(jiffies_to_msecs(timeout) - TIMEOUT));
64 
65 		/* error bit set? */
66 		if ((cmd->args[0] >> 6) & 0x01) {
67 			ret = -EREMOTEIO;
68 			goto err_mutex_unlock;
69 		}
70 
71 		if (!((cmd->args[0] >> 7) & 0x01)) {
72 			ret = -ETIMEDOUT;
73 			goto err_mutex_unlock;
74 		}
75 	}
76 
77 	mutex_unlock(&dev->i2c_mutex);
78 	return 0;
79 err_mutex_unlock:
80 	mutex_unlock(&dev->i2c_mutex);
81 	dev_dbg(&client->dev, "failed=%d\n", ret);
82 	return ret;
83 }
84 
85 static int si2168_ts_bus_ctrl(struct dvb_frontend *fe, int acquire)
86 {
87 	struct i2c_client *client = fe->demodulator_priv;
88 	struct si2168_dev *dev = i2c_get_clientdata(client);
89 	struct si2168_cmd cmd;
90 	int ret = 0;
91 
92 	dev_dbg(&client->dev, "%s acquire: %d\n", __func__, acquire);
93 
94 	/* set TS_MODE property */
95 	memcpy(cmd.args, "\x14\x00\x01\x10\x10\x00", 6);
96 	if (acquire)
97 		cmd.args[4] |= dev->ts_mode;
98 	else
99 		cmd.args[4] |= SI2168_TS_TRISTATE;
100 	if (dev->ts_clock_gapped)
101 		cmd.args[4] |= 0x40;
102 	cmd.wlen = 6;
103 	cmd.rlen = 4;
104 	ret = si2168_cmd_execute(client, &cmd);
105 
106 	return ret;
107 }
108 
109 static int si2168_read_status(struct dvb_frontend *fe, enum fe_status *status)
110 {
111 	struct i2c_client *client = fe->demodulator_priv;
112 	struct si2168_dev *dev = i2c_get_clientdata(client);
113 	struct dtv_frontend_properties *c = &fe->dtv_property_cache;
114 	int ret, i;
115 	unsigned int utmp, utmp1, utmp2;
116 	struct si2168_cmd cmd;
117 
118 	*status = 0;
119 
120 	if (!dev->active) {
121 		ret = -EAGAIN;
122 		goto err;
123 	}
124 
125 	switch (c->delivery_system) {
126 	case SYS_DVBT:
127 		memcpy(cmd.args, "\xa0\x01", 2);
128 		cmd.wlen = 2;
129 		cmd.rlen = 13;
130 		break;
131 	case SYS_DVBC_ANNEX_A:
132 		memcpy(cmd.args, "\x90\x01", 2);
133 		cmd.wlen = 2;
134 		cmd.rlen = 9;
135 		break;
136 	case SYS_DVBT2:
137 		memcpy(cmd.args, "\x50\x01", 2);
138 		cmd.wlen = 2;
139 		cmd.rlen = 14;
140 		break;
141 	default:
142 		ret = -EINVAL;
143 		goto err;
144 	}
145 
146 	ret = si2168_cmd_execute(client, &cmd);
147 	if (ret)
148 		goto err;
149 
150 	switch ((cmd.args[2] >> 1) & 0x03) {
151 	case 0x01:
152 		*status = FE_HAS_SIGNAL | FE_HAS_CARRIER;
153 		break;
154 	case 0x03:
155 		*status = FE_HAS_SIGNAL | FE_HAS_CARRIER | FE_HAS_VITERBI |
156 				FE_HAS_SYNC | FE_HAS_LOCK;
157 		break;
158 	}
159 
160 	dev->fe_status = *status;
161 
162 	if (*status & FE_HAS_LOCK) {
163 		c->cnr.len = 1;
164 		c->cnr.stat[0].scale = FE_SCALE_DECIBEL;
165 		c->cnr.stat[0].svalue = cmd.args[3] * 1000 / 4;
166 	} else {
167 		c->cnr.len = 1;
168 		c->cnr.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
169 	}
170 
171 	dev_dbg(&client->dev, "status=%02x args=%*ph\n",
172 			*status, cmd.rlen, cmd.args);
173 
174 	/* BER */
175 	if (*status & FE_HAS_VITERBI) {
176 		memcpy(cmd.args, "\x82\x00", 2);
177 		cmd.wlen = 2;
178 		cmd.rlen = 3;
179 		ret = si2168_cmd_execute(client, &cmd);
180 		if (ret)
181 			goto err;
182 
183 		/*
184 		 * Firmware returns [0, 255] mantissa and [0, 8] exponent.
185 		 * Convert to DVB API: mantissa * 10^(8 - exponent) / 10^8
186 		 */
187 		utmp = clamp(8 - cmd.args[1], 0, 8);
188 		for (i = 0, utmp1 = 1; i < utmp; i++)
189 			utmp1 = utmp1 * 10;
190 
191 		utmp1 = cmd.args[2] * utmp1;
192 		utmp2 = 100000000; /* 10^8 */
193 
194 		dev_dbg(&client->dev,
195 			"post_bit_error=%u post_bit_count=%u ber=%u*10^-%u\n",
196 			utmp1, utmp2, cmd.args[2], cmd.args[1]);
197 
198 		c->post_bit_error.stat[0].scale = FE_SCALE_COUNTER;
199 		c->post_bit_error.stat[0].uvalue += utmp1;
200 		c->post_bit_count.stat[0].scale = FE_SCALE_COUNTER;
201 		c->post_bit_count.stat[0].uvalue += utmp2;
202 	} else {
203 		c->post_bit_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
204 		c->post_bit_count.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
205 	}
206 
207 	/* UCB */
208 	if (*status & FE_HAS_SYNC) {
209 		memcpy(cmd.args, "\x84\x01", 2);
210 		cmd.wlen = 2;
211 		cmd.rlen = 3;
212 		ret = si2168_cmd_execute(client, &cmd);
213 		if (ret)
214 			goto err;
215 
216 		utmp1 = cmd.args[2] << 8 | cmd.args[1] << 0;
217 		dev_dbg(&client->dev, "block_error=%u\n", utmp1);
218 
219 		/* Sometimes firmware returns bogus value */
220 		if (utmp1 == 0xffff)
221 			utmp1 = 0;
222 
223 		c->block_error.stat[0].scale = FE_SCALE_COUNTER;
224 		c->block_error.stat[0].uvalue += utmp1;
225 	} else {
226 		c->block_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
227 	}
228 
229 	return 0;
230 err:
231 	dev_dbg(&client->dev, "failed=%d\n", ret);
232 	return ret;
233 }
234 
235 static int si2168_set_frontend(struct dvb_frontend *fe)
236 {
237 	struct i2c_client *client = fe->demodulator_priv;
238 	struct si2168_dev *dev = i2c_get_clientdata(client);
239 	struct dtv_frontend_properties *c = &fe->dtv_property_cache;
240 	int ret;
241 	struct si2168_cmd cmd;
242 	u8 bandwidth, delivery_system;
243 
244 	dev_dbg(&client->dev,
245 			"delivery_system=%u modulation=%u frequency=%u bandwidth_hz=%u symbol_rate=%u inversion=%u stream_id=%u\n",
246 			c->delivery_system, c->modulation, c->frequency,
247 			c->bandwidth_hz, c->symbol_rate, c->inversion,
248 			c->stream_id);
249 
250 	if (!dev->active) {
251 		ret = -EAGAIN;
252 		goto err;
253 	}
254 
255 	switch (c->delivery_system) {
256 	case SYS_DVBT:
257 		delivery_system = 0x20;
258 		break;
259 	case SYS_DVBC_ANNEX_A:
260 		delivery_system = 0x30;
261 		break;
262 	case SYS_DVBT2:
263 		delivery_system = 0x70;
264 		break;
265 	default:
266 		ret = -EINVAL;
267 		goto err;
268 	}
269 
270 	if (c->bandwidth_hz == 0) {
271 		ret = -EINVAL;
272 		goto err;
273 	} else if (c->bandwidth_hz <= 2000000)
274 		bandwidth = 0x02;
275 	else if (c->bandwidth_hz <= 5000000)
276 		bandwidth = 0x05;
277 	else if (c->bandwidth_hz <= 6000000)
278 		bandwidth = 0x06;
279 	else if (c->bandwidth_hz <= 7000000)
280 		bandwidth = 0x07;
281 	else if (c->bandwidth_hz <= 8000000)
282 		bandwidth = 0x08;
283 	else if (c->bandwidth_hz <= 9000000)
284 		bandwidth = 0x09;
285 	else if (c->bandwidth_hz <= 10000000)
286 		bandwidth = 0x0a;
287 	else
288 		bandwidth = 0x0f;
289 
290 	/* program tuner */
291 	if (fe->ops.tuner_ops.set_params) {
292 		ret = fe->ops.tuner_ops.set_params(fe);
293 		if (ret)
294 			goto err;
295 	}
296 
297 	memcpy(cmd.args, "\x88\x02\x02\x02\x02", 5);
298 	cmd.wlen = 5;
299 	cmd.rlen = 5;
300 	ret = si2168_cmd_execute(client, &cmd);
301 	if (ret)
302 		goto err;
303 
304 	/* that has no big effect */
305 	if (c->delivery_system == SYS_DVBT)
306 		memcpy(cmd.args, "\x89\x21\x06\x11\xff\x98", 6);
307 	else if (c->delivery_system == SYS_DVBC_ANNEX_A)
308 		memcpy(cmd.args, "\x89\x21\x06\x11\x89\xf0", 6);
309 	else if (c->delivery_system == SYS_DVBT2)
310 		memcpy(cmd.args, "\x89\x21\x06\x11\x89\x20", 6);
311 	cmd.wlen = 6;
312 	cmd.rlen = 3;
313 	ret = si2168_cmd_execute(client, &cmd);
314 	if (ret)
315 		goto err;
316 
317 	if (c->delivery_system == SYS_DVBT2) {
318 		/* select PLP */
319 		cmd.args[0] = 0x52;
320 		cmd.args[1] = c->stream_id & 0xff;
321 		cmd.args[2] = c->stream_id == NO_STREAM_ID_FILTER ? 0 : 1;
322 		cmd.wlen = 3;
323 		cmd.rlen = 1;
324 		ret = si2168_cmd_execute(client, &cmd);
325 		if (ret)
326 			goto err;
327 	}
328 
329 	memcpy(cmd.args, "\x51\x03", 2);
330 	cmd.wlen = 2;
331 	cmd.rlen = 12;
332 	ret = si2168_cmd_execute(client, &cmd);
333 	if (ret)
334 		goto err;
335 
336 	memcpy(cmd.args, "\x12\x08\x04", 3);
337 	cmd.wlen = 3;
338 	cmd.rlen = 3;
339 	ret = si2168_cmd_execute(client, &cmd);
340 	if (ret)
341 		goto err;
342 
343 	memcpy(cmd.args, "\x14\x00\x0c\x10\x12\x00", 6);
344 	cmd.wlen = 6;
345 	cmd.rlen = 4;
346 	ret = si2168_cmd_execute(client, &cmd);
347 	if (ret)
348 		goto err;
349 
350 	memcpy(cmd.args, "\x14\x00\x06\x10\x24\x00", 6);
351 	cmd.wlen = 6;
352 	cmd.rlen = 4;
353 	ret = si2168_cmd_execute(client, &cmd);
354 	if (ret)
355 		goto err;
356 
357 	memcpy(cmd.args, "\x14\x00\x07\x10\x00\x24", 6);
358 	cmd.wlen = 6;
359 	cmd.rlen = 4;
360 	ret = si2168_cmd_execute(client, &cmd);
361 	if (ret)
362 		goto err;
363 
364 	memcpy(cmd.args, "\x14\x00\x0a\x10\x00\x00", 6);
365 	cmd.args[4] = delivery_system | bandwidth;
366 	if (dev->spectral_inversion)
367 		cmd.args[5] |= 1;
368 	cmd.wlen = 6;
369 	cmd.rlen = 4;
370 	ret = si2168_cmd_execute(client, &cmd);
371 	if (ret)
372 		goto err;
373 
374 	/* set DVB-C symbol rate */
375 	if (c->delivery_system == SYS_DVBC_ANNEX_A) {
376 		memcpy(cmd.args, "\x14\x00\x02\x11", 4);
377 		cmd.args[4] = ((c->symbol_rate / 1000) >> 0) & 0xff;
378 		cmd.args[5] = ((c->symbol_rate / 1000) >> 8) & 0xff;
379 		cmd.wlen = 6;
380 		cmd.rlen = 4;
381 		ret = si2168_cmd_execute(client, &cmd);
382 		if (ret)
383 			goto err;
384 	}
385 
386 	memcpy(cmd.args, "\x14\x00\x0f\x10\x10\x00", 6);
387 	cmd.wlen = 6;
388 	cmd.rlen = 4;
389 	ret = si2168_cmd_execute(client, &cmd);
390 	if (ret)
391 		goto err;
392 
393 	memcpy(cmd.args, "\x14\x00\x09\x10\xe3\x08", 6);
394 	cmd.args[5] |= dev->ts_clock_inv ? 0x00 : 0x10;
395 	cmd.wlen = 6;
396 	cmd.rlen = 4;
397 	ret = si2168_cmd_execute(client, &cmd);
398 	if (ret)
399 		goto err;
400 
401 	memcpy(cmd.args, "\x14\x00\x08\x10\xd7\x05", 6);
402 	cmd.args[5] |= dev->ts_clock_inv ? 0x00 : 0x10;
403 	cmd.wlen = 6;
404 	cmd.rlen = 4;
405 	ret = si2168_cmd_execute(client, &cmd);
406 	if (ret)
407 		goto err;
408 
409 	memcpy(cmd.args, "\x14\x00\x01\x12\x00\x00", 6);
410 	cmd.wlen = 6;
411 	cmd.rlen = 4;
412 	ret = si2168_cmd_execute(client, &cmd);
413 	if (ret)
414 		goto err;
415 
416 	memcpy(cmd.args, "\x14\x00\x01\x03\x0c\x00", 6);
417 	cmd.wlen = 6;
418 	cmd.rlen = 4;
419 	ret = si2168_cmd_execute(client, &cmd);
420 	if (ret)
421 		goto err;
422 
423 	memcpy(cmd.args, "\x85", 1);
424 	cmd.wlen = 1;
425 	cmd.rlen = 1;
426 	ret = si2168_cmd_execute(client, &cmd);
427 	if (ret)
428 		goto err;
429 
430 	dev->delivery_system = c->delivery_system;
431 
432 	/* enable ts bus */
433 	ret = si2168_ts_bus_ctrl(fe, 1);
434 	if (ret)
435 		goto err;
436 
437 	return 0;
438 err:
439 	dev_dbg(&client->dev, "failed=%d\n", ret);
440 	return ret;
441 }
442 
443 static int si2168_init(struct dvb_frontend *fe)
444 {
445 	struct i2c_client *client = fe->demodulator_priv;
446 	struct si2168_dev *dev = i2c_get_clientdata(client);
447 	struct dtv_frontend_properties *c = &fe->dtv_property_cache;
448 	int ret, len, remaining;
449 	const struct firmware *fw;
450 	struct si2168_cmd cmd;
451 
452 	dev_dbg(&client->dev, "\n");
453 
454 	/* initialize */
455 	memcpy(cmd.args, "\xc0\x12\x00\x0c\x00\x0d\x16\x00\x00\x00\x00\x00\x00", 13);
456 	cmd.wlen = 13;
457 	cmd.rlen = 0;
458 	ret = si2168_cmd_execute(client, &cmd);
459 	if (ret)
460 		goto err;
461 
462 	if (dev->warm) {
463 		/* resume */
464 		memcpy(cmd.args, "\xc0\x06\x08\x0f\x00\x20\x21\x01", 8);
465 		cmd.wlen = 8;
466 		cmd.rlen = 1;
467 		ret = si2168_cmd_execute(client, &cmd);
468 		if (ret)
469 			goto err;
470 
471 		udelay(100);
472 		memcpy(cmd.args, "\x85", 1);
473 		cmd.wlen = 1;
474 		cmd.rlen = 1;
475 		ret = si2168_cmd_execute(client, &cmd);
476 		if (ret)
477 			goto err;
478 
479 		goto warm;
480 	}
481 
482 	/* power up */
483 	memcpy(cmd.args, "\xc0\x06\x01\x0f\x00\x20\x20\x01", 8);
484 	cmd.wlen = 8;
485 	cmd.rlen = 1;
486 	ret = si2168_cmd_execute(client, &cmd);
487 	if (ret)
488 		goto err;
489 
490 	/* request the firmware, this will block and timeout */
491 	ret = request_firmware(&fw, dev->firmware_name, &client->dev);
492 	if (ret) {
493 		/* fallback mechanism to handle old name for Si2168 B40 fw */
494 		if (dev->chip_id == SI2168_CHIP_ID_B40) {
495 			dev->firmware_name = SI2168_B40_FIRMWARE_FALLBACK;
496 			ret = request_firmware(&fw, dev->firmware_name,
497 					       &client->dev);
498 		}
499 
500 		if (ret == 0) {
501 			dev_notice(&client->dev,
502 					"please install firmware file '%s'\n",
503 					SI2168_B40_FIRMWARE);
504 		} else {
505 			dev_err(&client->dev,
506 					"firmware file '%s' not found\n",
507 					dev->firmware_name);
508 			goto err_release_firmware;
509 		}
510 	}
511 
512 	dev_info(&client->dev, "downloading firmware from file '%s'\n",
513 			dev->firmware_name);
514 
515 	if ((fw->size % 17 == 0) && (fw->data[0] > 5)) {
516 		/* firmware is in the new format */
517 		for (remaining = fw->size; remaining > 0; remaining -= 17) {
518 			len = fw->data[fw->size - remaining];
519 			if (len > SI2168_ARGLEN) {
520 				ret = -EINVAL;
521 				break;
522 			}
523 			memcpy(cmd.args, &fw->data[(fw->size - remaining) + 1], len);
524 			cmd.wlen = len;
525 			cmd.rlen = 1;
526 			ret = si2168_cmd_execute(client, &cmd);
527 			if (ret)
528 				break;
529 		}
530 	} else if (fw->size % 8 == 0) {
531 		/* firmware is in the old format */
532 		for (remaining = fw->size; remaining > 0; remaining -= 8) {
533 			len = 8;
534 			memcpy(cmd.args, &fw->data[fw->size - remaining], len);
535 			cmd.wlen = len;
536 			cmd.rlen = 1;
537 			ret = si2168_cmd_execute(client, &cmd);
538 			if (ret)
539 				break;
540 		}
541 	} else {
542 		/* bad or unknown firmware format */
543 		ret = -EINVAL;
544 	}
545 
546 	if (ret) {
547 		dev_err(&client->dev, "firmware download failed %d\n", ret);
548 		goto err_release_firmware;
549 	}
550 
551 	release_firmware(fw);
552 
553 	memcpy(cmd.args, "\x01\x01", 2);
554 	cmd.wlen = 2;
555 	cmd.rlen = 1;
556 	ret = si2168_cmd_execute(client, &cmd);
557 	if (ret)
558 		goto err;
559 
560 	/* query firmware version */
561 	memcpy(cmd.args, "\x11", 1);
562 	cmd.wlen = 1;
563 	cmd.rlen = 10;
564 	ret = si2168_cmd_execute(client, &cmd);
565 	if (ret)
566 		goto err;
567 
568 	dev->version = (cmd.args[9] + '@') << 24 | (cmd.args[6] - '0') << 16 |
569 		       (cmd.args[7] - '0') << 8 | (cmd.args[8]) << 0;
570 	dev_info(&client->dev, "firmware version: %c %d.%d.%d\n",
571 		 dev->version >> 24 & 0xff, dev->version >> 16 & 0xff,
572 		 dev->version >> 8 & 0xff, dev->version >> 0 & 0xff);
573 
574 	/* set ts mode */
575 	ret = si2168_ts_bus_ctrl(fe, 1);
576 	if (ret)
577 		goto err;
578 
579 	dev->warm = true;
580 warm:
581 	/* Init stats here to indicate which stats are supported */
582 	c->cnr.len = 1;
583 	c->cnr.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
584 	c->post_bit_error.len = 1;
585 	c->post_bit_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
586 	c->post_bit_count.len = 1;
587 	c->post_bit_count.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
588 	c->block_error.len = 1;
589 	c->block_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
590 
591 	dev->active = true;
592 
593 	return 0;
594 err_release_firmware:
595 	release_firmware(fw);
596 err:
597 	dev_dbg(&client->dev, "failed=%d\n", ret);
598 	return ret;
599 }
600 
601 static int si2168_sleep(struct dvb_frontend *fe)
602 {
603 	struct i2c_client *client = fe->demodulator_priv;
604 	struct si2168_dev *dev = i2c_get_clientdata(client);
605 	int ret;
606 	struct si2168_cmd cmd;
607 
608 	dev_dbg(&client->dev, "\n");
609 
610 	dev->active = false;
611 
612 	/* tri-state data bus */
613 	ret = si2168_ts_bus_ctrl(fe, 0);
614 	if (ret)
615 		goto err;
616 
617 	/* Firmware later than B 4.0-11 loses warm state during sleep */
618 	if (dev->version > ('B' << 24 | 4 << 16 | 0 << 8 | 11 << 0))
619 		dev->warm = false;
620 
621 	memcpy(cmd.args, "\x13", 1);
622 	cmd.wlen = 1;
623 	cmd.rlen = 0;
624 	ret = si2168_cmd_execute(client, &cmd);
625 	if (ret)
626 		goto err;
627 
628 	return 0;
629 err:
630 	dev_dbg(&client->dev, "failed=%d\n", ret);
631 	return ret;
632 }
633 
634 static int si2168_get_tune_settings(struct dvb_frontend *fe,
635 	struct dvb_frontend_tune_settings *s)
636 {
637 	s->min_delay_ms = 900;
638 
639 	return 0;
640 }
641 
642 static int si2168_select(struct i2c_mux_core *muxc, u32 chan)
643 {
644 	struct i2c_client *client = i2c_mux_priv(muxc);
645 	int ret;
646 	struct si2168_cmd cmd;
647 
648 	/* open I2C gate */
649 	memcpy(cmd.args, "\xc0\x0d\x01", 3);
650 	cmd.wlen = 3;
651 	cmd.rlen = 0;
652 	ret = si2168_cmd_execute(client, &cmd);
653 	if (ret)
654 		goto err;
655 
656 	return 0;
657 err:
658 	dev_dbg(&client->dev, "failed=%d\n", ret);
659 	return ret;
660 }
661 
662 static int si2168_deselect(struct i2c_mux_core *muxc, u32 chan)
663 {
664 	struct i2c_client *client = i2c_mux_priv(muxc);
665 	int ret;
666 	struct si2168_cmd cmd;
667 
668 	/* close I2C gate */
669 	memcpy(cmd.args, "\xc0\x0d\x00", 3);
670 	cmd.wlen = 3;
671 	cmd.rlen = 0;
672 	ret = si2168_cmd_execute(client, &cmd);
673 	if (ret)
674 		goto err;
675 
676 	return 0;
677 err:
678 	dev_dbg(&client->dev, "failed=%d\n", ret);
679 	return ret;
680 }
681 
682 static const struct dvb_frontend_ops si2168_ops = {
683 	.delsys = {SYS_DVBT, SYS_DVBT2, SYS_DVBC_ANNEX_A},
684 	.info = {
685 		.name = "Silicon Labs Si2168",
686 		.symbol_rate_min = 1000000,
687 		.symbol_rate_max = 7200000,
688 		.caps =	FE_CAN_FEC_1_2 |
689 			FE_CAN_FEC_2_3 |
690 			FE_CAN_FEC_3_4 |
691 			FE_CAN_FEC_5_6 |
692 			FE_CAN_FEC_7_8 |
693 			FE_CAN_FEC_AUTO |
694 			FE_CAN_QPSK |
695 			FE_CAN_QAM_16 |
696 			FE_CAN_QAM_32 |
697 			FE_CAN_QAM_64 |
698 			FE_CAN_QAM_128 |
699 			FE_CAN_QAM_256 |
700 			FE_CAN_QAM_AUTO |
701 			FE_CAN_TRANSMISSION_MODE_AUTO |
702 			FE_CAN_GUARD_INTERVAL_AUTO |
703 			FE_CAN_HIERARCHY_AUTO |
704 			FE_CAN_MUTE_TS |
705 			FE_CAN_2G_MODULATION |
706 			FE_CAN_MULTISTREAM
707 	},
708 
709 	.get_tune_settings = si2168_get_tune_settings,
710 
711 	.init = si2168_init,
712 	.sleep = si2168_sleep,
713 
714 	.set_frontend = si2168_set_frontend,
715 
716 	.read_status = si2168_read_status,
717 };
718 
719 static int si2168_probe(struct i2c_client *client,
720 		const struct i2c_device_id *id)
721 {
722 	struct si2168_config *config = client->dev.platform_data;
723 	struct si2168_dev *dev;
724 	int ret;
725 	struct si2168_cmd cmd;
726 
727 	dev_dbg(&client->dev, "\n");
728 
729 	dev = kzalloc(sizeof(*dev), GFP_KERNEL);
730 	if (!dev) {
731 		ret = -ENOMEM;
732 		goto err;
733 	}
734 
735 	i2c_set_clientdata(client, dev);
736 	mutex_init(&dev->i2c_mutex);
737 
738 	/* Initialize */
739 	memcpy(cmd.args, "\xc0\x12\x00\x0c\x00\x0d\x16\x00\x00\x00\x00\x00\x00", 13);
740 	cmd.wlen = 13;
741 	cmd.rlen = 0;
742 	ret = si2168_cmd_execute(client, &cmd);
743 	if (ret)
744 		goto err_kfree;
745 
746 	/* Power up */
747 	memcpy(cmd.args, "\xc0\x06\x01\x0f\x00\x20\x20\x01", 8);
748 	cmd.wlen = 8;
749 	cmd.rlen = 1;
750 	ret = si2168_cmd_execute(client, &cmd);
751 	if (ret)
752 		goto err_kfree;
753 
754 	/* Query chip revision */
755 	memcpy(cmd.args, "\x02", 1);
756 	cmd.wlen = 1;
757 	cmd.rlen = 13;
758 	ret = si2168_cmd_execute(client, &cmd);
759 	if (ret)
760 		goto err_kfree;
761 
762 	dev->chip_id = cmd.args[1] << 24 | cmd.args[2] << 16 |
763 		       cmd.args[3] << 8 | cmd.args[4] << 0;
764 
765 	switch (dev->chip_id) {
766 	case SI2168_CHIP_ID_A20:
767 		dev->firmware_name = SI2168_A20_FIRMWARE;
768 		break;
769 	case SI2168_CHIP_ID_A30:
770 		dev->firmware_name = SI2168_A30_FIRMWARE;
771 		break;
772 	case SI2168_CHIP_ID_B40:
773 		dev->firmware_name = SI2168_B40_FIRMWARE;
774 		break;
775 	case SI2168_CHIP_ID_D60:
776 		dev->firmware_name = SI2168_D60_FIRMWARE;
777 		break;
778 	default:
779 		dev_dbg(&client->dev, "unknown chip version Si21%d-%c%c%c\n",
780 			cmd.args[2], cmd.args[1], cmd.args[3], cmd.args[4]);
781 		ret = -ENODEV;
782 		goto err_kfree;
783 	}
784 
785 	dev->version = (cmd.args[1]) << 24 | (cmd.args[3] - '0') << 16 |
786 		       (cmd.args[4] - '0') << 8 | (cmd.args[5]) << 0;
787 
788 	/* create mux i2c adapter for tuner */
789 	dev->muxc = i2c_mux_alloc(client->adapter, &client->dev,
790 				  1, 0, I2C_MUX_LOCKED,
791 				  si2168_select, si2168_deselect);
792 	if (!dev->muxc) {
793 		ret = -ENOMEM;
794 		goto err_kfree;
795 	}
796 	dev->muxc->priv = client;
797 	ret = i2c_mux_add_adapter(dev->muxc, 0, 0, 0);
798 	if (ret)
799 		goto err_kfree;
800 
801 	/* create dvb_frontend */
802 	memcpy(&dev->fe.ops, &si2168_ops, sizeof(struct dvb_frontend_ops));
803 	dev->fe.demodulator_priv = client;
804 	*config->i2c_adapter = dev->muxc->adapter[0];
805 	*config->fe = &dev->fe;
806 	dev->ts_mode = config->ts_mode;
807 	dev->ts_clock_inv = config->ts_clock_inv;
808 	dev->ts_clock_gapped = config->ts_clock_gapped;
809 	dev->spectral_inversion = config->spectral_inversion;
810 
811 	dev_info(&client->dev, "Silicon Labs Si2168-%c%d%d successfully identified\n",
812 		 dev->version >> 24 & 0xff, dev->version >> 16 & 0xff,
813 		 dev->version >> 8 & 0xff);
814 	dev_info(&client->dev, "firmware version: %c %d.%d.%d\n",
815 		 dev->version >> 24 & 0xff, dev->version >> 16 & 0xff,
816 		 dev->version >> 8 & 0xff, dev->version >> 0 & 0xff);
817 
818 	return 0;
819 err_kfree:
820 	kfree(dev);
821 err:
822 	dev_warn(&client->dev, "probe failed = %d\n", ret);
823 	return ret;
824 }
825 
826 static int si2168_remove(struct i2c_client *client)
827 {
828 	struct si2168_dev *dev = i2c_get_clientdata(client);
829 
830 	dev_dbg(&client->dev, "\n");
831 
832 	i2c_mux_del_adapters(dev->muxc);
833 
834 	dev->fe.ops.release = NULL;
835 	dev->fe.demodulator_priv = NULL;
836 
837 	kfree(dev);
838 
839 	return 0;
840 }
841 
842 static const struct i2c_device_id si2168_id_table[] = {
843 	{"si2168", 0},
844 	{}
845 };
846 MODULE_DEVICE_TABLE(i2c, si2168_id_table);
847 
848 static struct i2c_driver si2168_driver = {
849 	.driver = {
850 		.name                = "si2168",
851 		.suppress_bind_attrs = true,
852 	},
853 	.probe		= si2168_probe,
854 	.remove		= si2168_remove,
855 	.id_table	= si2168_id_table,
856 };
857 
858 module_i2c_driver(si2168_driver);
859 
860 MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
861 MODULE_DESCRIPTION("Silicon Labs Si2168 DVB-T/T2/C demodulator driver");
862 MODULE_LICENSE("GPL");
863 MODULE_FIRMWARE(SI2168_A20_FIRMWARE);
864 MODULE_FIRMWARE(SI2168_A30_FIRMWARE);
865 MODULE_FIRMWARE(SI2168_B40_FIRMWARE);
866 MODULE_FIRMWARE(SI2168_D60_FIRMWARE);
867