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