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