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