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