si2168.c 17.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/*
 * Silicon Labs Si2168 DVB-T/T2/C demodulator driver
 *
 * Copyright (C) 2014 Antti Palosaari <crope@iki.fi>
 *
 *    This program is free software; you can redistribute it and/or modify
 *    it under the terms of the GNU General Public License as published by
 *    the Free Software Foundation; either version 2 of the License, or
 *    (at your option) any later version.
 *
 *    This program is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU General Public License for more details.
 */

17 18 19 20
#include "si2168_priv.h"

static const struct dvb_frontend_ops si2168_ops;

21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
/* Own I2C adapter locking is needed because of I2C gate logic. */
static int si2168_i2c_master_send_unlocked(const struct i2c_client *client,
					   const char *buf, int count)
{
	int ret;
	struct i2c_msg msg = {
		.addr = client->addr,
		.flags = 0,
		.len = count,
		.buf = (char *)buf,
	};

	ret = __i2c_transfer(client->adapter, &msg, 1);
	return (ret == 1) ? count : ret;
}

static int si2168_i2c_master_recv_unlocked(const struct i2c_client *client,
					   char *buf, int count)
{
	int ret;
	struct i2c_msg msg = {
		.addr = client->addr,
		.flags = I2C_M_RD,
		.len = count,
		.buf = buf,
	};

	ret = __i2c_transfer(client->adapter, &msg, 1);
	return (ret == 1) ? count : ret;
}

52
/* execute firmware command */
53 54
static int si2168_cmd_execute_unlocked(struct i2c_client *client,
				       struct si2168_cmd *cmd)
55 56 57 58 59 60
{
	int ret;
	unsigned long timeout;

	if (cmd->wlen) {
		/* write cmd and args for firmware */
61 62
		ret = si2168_i2c_master_send_unlocked(client, cmd->args,
						      cmd->wlen);
63
		if (ret < 0) {
64
			goto err;
65 66
		} else if (ret != cmd->wlen) {
			ret = -EREMOTEIO;
67
			goto err;
68 69 70 71 72
		}
	}

	if (cmd->rlen) {
		/* wait cmd execution terminate */
73
		#define TIMEOUT 70
74 75
		timeout = jiffies + msecs_to_jiffies(TIMEOUT);
		while (!time_after(jiffies, timeout)) {
76 77
			ret = si2168_i2c_master_recv_unlocked(client, cmd->args,
							      cmd->rlen);
78
			if (ret < 0) {
79
				goto err;
80 81
			} else if (ret != cmd->rlen) {
				ret = -EREMOTEIO;
82
				goto err;
83 84 85 86 87 88 89
			}

			/* firmware ready? */
			if ((cmd->args[0] >> 7) & 0x01)
				break;
		}

90
		dev_dbg(&client->dev, "cmd execution took %d ms\n",
91 92 93
				jiffies_to_msecs(jiffies) -
				(jiffies_to_msecs(timeout) - TIMEOUT));

94 95 96
		/* error bit set? */
		if ((cmd->args[0] >> 6) & 0x01) {
			ret = -EREMOTEIO;
97
			goto err;
98 99
		}

100
		if (!((cmd->args[0] >> 7) & 0x01)) {
101
			ret = -ETIMEDOUT;
102
			goto err;
103 104 105
		}
	}

106
	return 0;
107
err:
108
	dev_dbg(&client->dev, "failed=%d\n", ret);
109 110 111
	return ret;
}

112 113 114 115 116 117 118 119 120 121 122
static int si2168_cmd_execute(struct i2c_client *client, struct si2168_cmd *cmd)
{
	int ret;

	i2c_lock_adapter(client->adapter);
	ret = si2168_cmd_execute_unlocked(client, cmd);
	i2c_unlock_adapter(client->adapter);

	return ret;
}

123
static int si2168_read_status(struct dvb_frontend *fe, enum fe_status *status)
124
{
125 126
	struct i2c_client *client = fe->demodulator_priv;
	struct si2168_dev *dev = i2c_get_clientdata(client);
127
	struct dtv_frontend_properties *c = &fe->dtv_property_cache;
128 129 130 131 132
	int ret;
	struct si2168_cmd cmd;

	*status = 0;

133
	if (!dev->active) {
134 135 136 137
		ret = -EAGAIN;
		goto err;
	}

138 139
	switch (c->delivery_system) {
	case SYS_DVBT:
140
		memcpy(cmd.args, "\xa0\x01", 2);
141 142 143
		cmd.wlen = 2;
		cmd.rlen = 13;
		break;
144
	case SYS_DVBC_ANNEX_A:
145
		memcpy(cmd.args, "\x90\x01", 2);
146 147 148
		cmd.wlen = 2;
		cmd.rlen = 9;
		break;
149
	case SYS_DVBT2:
150
		memcpy(cmd.args, "\x50\x01", 2);
151 152 153 154 155 156 157 158
		cmd.wlen = 2;
		cmd.rlen = 14;
		break;
	default:
		ret = -EINVAL;
		goto err;
	}

159
	ret = si2168_cmd_execute(client, &cmd);
160 161 162
	if (ret)
		goto err;

163 164
	switch ((cmd.args[2] >> 1) & 0x03) {
	case 0x01:
165 166
		*status = FE_HAS_SIGNAL | FE_HAS_CARRIER;
		break;
167
	case 0x03:
168 169 170 171 172
		*status = FE_HAS_SIGNAL | FE_HAS_CARRIER | FE_HAS_VITERBI |
				FE_HAS_SYNC | FE_HAS_LOCK;
		break;
	}

173
	dev->fe_status = *status;
174

175 176 177 178 179 180 181 182 183
	if (*status & FE_HAS_LOCK) {
		c->cnr.len = 1;
		c->cnr.stat[0].scale = FE_SCALE_DECIBEL;
		c->cnr.stat[0].svalue = cmd.args[3] * 1000 / 4;
	} else {
		c->cnr.len = 1;
		c->cnr.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
	}

184
	dev_dbg(&client->dev, "status=%02x args=%*ph\n",
O
Olli Salonen 已提交
185
			*status, cmd.rlen, cmd.args);
186 187 188

	return 0;
err:
189
	dev_dbg(&client->dev, "failed=%d\n", ret);
190 191 192 193 194
	return ret;
}

static int si2168_set_frontend(struct dvb_frontend *fe)
{
195 196
	struct i2c_client *client = fe->demodulator_priv;
	struct si2168_dev *dev = i2c_get_clientdata(client);
197 198 199
	struct dtv_frontend_properties *c = &fe->dtv_property_cache;
	int ret;
	struct si2168_cmd cmd;
200
	u8 bandwidth, delivery_system;
201

202
	dev_dbg(&client->dev,
203 204 205 206
			"delivery_system=%u modulation=%u frequency=%u bandwidth_hz=%u symbol_rate=%u inversion=%u stream_id=%u\n",
			c->delivery_system, c->modulation, c->frequency,
			c->bandwidth_hz, c->symbol_rate, c->inversion,
			c->stream_id);
207

208
	if (!dev->active) {
209 210 211 212
		ret = -EAGAIN;
		goto err;
	}

213 214 215 216
	switch (c->delivery_system) {
	case SYS_DVBT:
		delivery_system = 0x20;
		break;
217 218 219
	case SYS_DVBC_ANNEX_A:
		delivery_system = 0x30;
		break;
220 221 222 223 224 225 226 227
	case SYS_DVBT2:
		delivery_system = 0x70;
		break;
	default:
		ret = -EINVAL;
		goto err;
	}

228 229 230
	if (c->bandwidth_hz == 0) {
		ret = -EINVAL;
		goto err;
231 232 233
	} else if (c->bandwidth_hz <= 2000000)
		bandwidth = 0x02;
	else if (c->bandwidth_hz <= 5000000)
234
		bandwidth = 0x05;
235
	else if (c->bandwidth_hz <= 6000000)
236
		bandwidth = 0x06;
237
	else if (c->bandwidth_hz <= 7000000)
238
		bandwidth = 0x07;
239
	else if (c->bandwidth_hz <= 8000000)
240
		bandwidth = 0x08;
241 242 243 244 245 246
	else if (c->bandwidth_hz <= 9000000)
		bandwidth = 0x09;
	else if (c->bandwidth_hz <= 10000000)
		bandwidth = 0x0a;
	else
		bandwidth = 0x0f;
247 248 249 250 251 252 253 254 255 256 257

	/* program tuner */
	if (fe->ops.tuner_ops.set_params) {
		ret = fe->ops.tuner_ops.set_params(fe);
		if (ret)
			goto err;
	}

	memcpy(cmd.args, "\x88\x02\x02\x02\x02", 5);
	cmd.wlen = 5;
	cmd.rlen = 5;
258
	ret = si2168_cmd_execute(client, &cmd);
259 260 261
	if (ret)
		goto err;

262 263 264
	/* that has no big effect */
	if (c->delivery_system == SYS_DVBT)
		memcpy(cmd.args, "\x89\x21\x06\x11\xff\x98", 6);
265 266
	else if (c->delivery_system == SYS_DVBC_ANNEX_A)
		memcpy(cmd.args, "\x89\x21\x06\x11\x89\xf0", 6);
267 268
	else if (c->delivery_system == SYS_DVBT2)
		memcpy(cmd.args, "\x89\x21\x06\x11\x89\x20", 6);
269 270
	cmd.wlen = 6;
	cmd.rlen = 3;
271
	ret = si2168_cmd_execute(client, &cmd);
272 273 274
	if (ret)
		goto err;

275 276 277 278 279 280 281
	if (c->delivery_system == SYS_DVBT2) {
		/* select PLP */
		cmd.args[0] = 0x52;
		cmd.args[1] = c->stream_id & 0xff;
		cmd.args[2] = c->stream_id == NO_STREAM_ID_FILTER ? 0 : 1;
		cmd.wlen = 3;
		cmd.rlen = 1;
282
		ret = si2168_cmd_execute(client, &cmd);
283 284 285 286
		if (ret)
			goto err;
	}

287 288 289
	memcpy(cmd.args, "\x51\x03", 2);
	cmd.wlen = 2;
	cmd.rlen = 12;
290
	ret = si2168_cmd_execute(client, &cmd);
291 292 293 294 295 296
	if (ret)
		goto err;

	memcpy(cmd.args, "\x12\x08\x04", 3);
	cmd.wlen = 3;
	cmd.rlen = 3;
297
	ret = si2168_cmd_execute(client, &cmd);
298 299 300 301 302
	if (ret)
		goto err;

	memcpy(cmd.args, "\x14\x00\x0c\x10\x12\x00", 6);
	cmd.wlen = 6;
303
	cmd.rlen = 4;
304
	ret = si2168_cmd_execute(client, &cmd);
305 306 307 308 309
	if (ret)
		goto err;

	memcpy(cmd.args, "\x14\x00\x06\x10\x24\x00", 6);
	cmd.wlen = 6;
310
	cmd.rlen = 4;
311
	ret = si2168_cmd_execute(client, &cmd);
312 313 314 315 316
	if (ret)
		goto err;

	memcpy(cmd.args, "\x14\x00\x07\x10\x00\x24", 6);
	cmd.wlen = 6;
317
	cmd.rlen = 4;
318
	ret = si2168_cmd_execute(client, &cmd);
319 320 321 322
	if (ret)
		goto err;

	memcpy(cmd.args, "\x14\x00\x0a\x10\x00\x00", 6);
323
	cmd.args[4] = delivery_system | bandwidth;
324
	cmd.wlen = 6;
325
	cmd.rlen = 4;
326
	ret = si2168_cmd_execute(client, &cmd);
327 328 329
	if (ret)
		goto err;

330 331 332
	/* set DVB-C symbol rate */
	if (c->delivery_system == SYS_DVBC_ANNEX_A) {
		memcpy(cmd.args, "\x14\x00\x02\x11", 4);
333
		cmd.args[4] = ((c->symbol_rate / 1000) >> 0) & 0xff;
334 335 336
		cmd.args[5] = ((c->symbol_rate / 1000) >> 8) & 0xff;
		cmd.wlen = 6;
		cmd.rlen = 4;
337
		ret = si2168_cmd_execute(client, &cmd);
338 339 340 341
		if (ret)
			goto err;
	}

342 343
	memcpy(cmd.args, "\x14\x00\x0f\x10\x10\x00", 6);
	cmd.wlen = 6;
344
	cmd.rlen = 4;
345
	ret = si2168_cmd_execute(client, &cmd);
346 347 348
	if (ret)
		goto err;

349
	memcpy(cmd.args, "\x14\x00\x09\x10\xe3\x08", 6);
350
	cmd.args[5] |= dev->ts_clock_inv ? 0x00 : 0x10;
351
	cmd.wlen = 6;
352
	cmd.rlen = 4;
353
	ret = si2168_cmd_execute(client, &cmd);
354 355 356
	if (ret)
		goto err;

357
	memcpy(cmd.args, "\x14\x00\x08\x10\xd7\x05", 6);
358
	cmd.args[5] |= dev->ts_clock_inv ? 0x00 : 0x10;
359
	cmd.wlen = 6;
360
	cmd.rlen = 4;
361
	ret = si2168_cmd_execute(client, &cmd);
362 363 364 365 366
	if (ret)
		goto err;

	memcpy(cmd.args, "\x14\x00\x01\x12\x00\x00", 6);
	cmd.wlen = 6;
367
	cmd.rlen = 4;
368
	ret = si2168_cmd_execute(client, &cmd);
369 370 371
	if (ret)
		goto err;

372 373 374
	memcpy(cmd.args, "\x14\x00\x01\x03\x0c\x00", 6);
	cmd.wlen = 6;
	cmd.rlen = 4;
375
	ret = si2168_cmd_execute(client, &cmd);
376 377 378
	if (ret)
		goto err;

379
	memcpy(cmd.args, "\x85", 1);
380 381
	cmd.wlen = 1;
	cmd.rlen = 1;
382
	ret = si2168_cmd_execute(client, &cmd);
383 384 385
	if (ret)
		goto err;

386
	dev->delivery_system = c->delivery_system;
387 388 389

	return 0;
err:
390
	dev_dbg(&client->dev, "failed=%d\n", ret);
391 392 393 394 395
	return ret;
}

static int si2168_init(struct dvb_frontend *fe)
{
396 397
	struct i2c_client *client = fe->demodulator_priv;
	struct si2168_dev *dev = i2c_get_clientdata(client);
398
	int ret, len, remaining;
399
	const struct firmware *fw;
400
	const char *fw_name;
401
	struct si2168_cmd cmd;
402
	unsigned int chip_id;
403

404
	dev_dbg(&client->dev, "\n");
405

406
	/* initialize */
407
	memcpy(cmd.args, "\xc0\x12\x00\x0c\x00\x0d\x16\x00\x00\x00\x00\x00\x00", 13);
408 409
	cmd.wlen = 13;
	cmd.rlen = 0;
410
	ret = si2168_cmd_execute(client, &cmd);
411 412 413
	if (ret)
		goto err;

414
	if (dev->fw_loaded) {
415 416 417 418
		/* resume */
		memcpy(cmd.args, "\xc0\x06\x08\x0f\x00\x20\x21\x01", 8);
		cmd.wlen = 8;
		cmd.rlen = 1;
419
		ret = si2168_cmd_execute(client, &cmd);
420 421 422 423 424 425
		if (ret)
			goto err;

		memcpy(cmd.args, "\x85", 1);
		cmd.wlen = 1;
		cmd.rlen = 1;
426
		ret = si2168_cmd_execute(client, &cmd);
427 428 429 430 431 432 433
		if (ret)
			goto err;

		goto warm;
	}

	/* power up */
434
	memcpy(cmd.args, "\xc0\x06\x01\x0f\x00\x20\x20\x01", 8);
435 436
	cmd.wlen = 8;
	cmd.rlen = 1;
437
	ret = si2168_cmd_execute(client, &cmd);
438 439 440
	if (ret)
		goto err;

441
	/* query chip revision */
442
	memcpy(cmd.args, "\x02", 1);
443 444
	cmd.wlen = 1;
	cmd.rlen = 13;
445
	ret = si2168_cmd_execute(client, &cmd);
446 447 448
	if (ret)
		goto err;

449 450 451
	chip_id = cmd.args[1] << 24 | cmd.args[2] << 16 | cmd.args[3] << 8 |
			cmd.args[4] << 0;

452
	#define SI2168_A20 ('A' << 24 | 68 << 16 | '2' << 8 | '0' << 0)
453 454 455 456
	#define SI2168_A30 ('A' << 24 | 68 << 16 | '3' << 8 | '0' << 0)
	#define SI2168_B40 ('B' << 24 | 68 << 16 | '4' << 8 | '0' << 0)

	switch (chip_id) {
457
	case SI2168_A20:
458
		fw_name = SI2168_A20_FIRMWARE;
459
		break;
460
	case SI2168_A30:
461
		fw_name = SI2168_A30_FIRMWARE;
462 463
		break;
	case SI2168_B40:
464
		fw_name = SI2168_B40_FIRMWARE;
465 466
		break;
	default:
467
		dev_err(&client->dev, "unknown chip version Si21%d-%c%c%c\n",
O
Olli Salonen 已提交
468
				cmd.args[2], cmd.args[1],
469
				cmd.args[3], cmd.args[4]);
470 471 472 473
		ret = -EINVAL;
		goto err;
	}

474 475
	dev_info(&client->dev, "found a 'Silicon Labs Si21%d-%c%c%c'\n",
			cmd.args[2], cmd.args[1], cmd.args[3], cmd.args[4]);
476 477

	/* request the firmware, this will block and timeout */
478
	ret = request_firmware(&fw, fw_name, &client->dev);
479
	if (ret) {
480 481
		/* fallback mechanism to handle old name for Si2168 B40 fw */
		if (chip_id == SI2168_B40) {
482 483
			fw_name = SI2168_B40_FIRMWARE_FALLBACK;
			ret = request_firmware(&fw, fw_name, &client->dev);
484
		}
485 486

		if (ret == 0) {
487
			dev_notice(&client->dev,
O
Olli Salonen 已提交
488 489
					"please install firmware file '%s'\n",
					SI2168_B40_FIRMWARE);
490
		} else {
491
			dev_err(&client->dev,
O
Olli Salonen 已提交
492
					"firmware file '%s' not found\n",
493
					fw_name);
494
			goto err_release_firmware;
495
		}
496 497
	}

498
	dev_info(&client->dev, "downloading firmware from file '%s'\n",
499
			fw_name);
500

501 502 503 504
	if ((fw->size % 17 == 0) && (fw->data[0] > 5)) {
		/* firmware is in the new format */
		for (remaining = fw->size; remaining > 0; remaining -= 17) {
			len = fw->data[fw->size - remaining];
505 506 507 508
			if (len > SI2168_ARGLEN) {
				ret = -EINVAL;
				break;
			}
509 510 511
			memcpy(cmd.args, &fw->data[(fw->size - remaining) + 1], len);
			cmd.wlen = len;
			cmd.rlen = 1;
512
			ret = si2168_cmd_execute(client, &cmd);
513 514
			if (ret)
				break;
515
		}
516
	} else if (fw->size % 8 == 0) {
517
		/* firmware is in the old format */
518 519
		for (remaining = fw->size; remaining > 0; remaining -= 8) {
			len = 8;
520 521 522
			memcpy(cmd.args, &fw->data[fw->size - remaining], len);
			cmd.wlen = len;
			cmd.rlen = 1;
523
			ret = si2168_cmd_execute(client, &cmd);
524 525
			if (ret)
				break;
526
		}
527 528 529 530 531 532 533 534
	} else {
		/* bad or unknown firmware format */
		ret = -EINVAL;
	}

	if (ret) {
		dev_err(&client->dev, "firmware download failed %d\n", ret);
		goto err_release_firmware;
535 536 537 538
	}

	release_firmware(fw);

539
	memcpy(cmd.args, "\x01\x01", 2);
540 541
	cmd.wlen = 2;
	cmd.rlen = 1;
542
	ret = si2168_cmd_execute(client, &cmd);
543 544 545
	if (ret)
		goto err;

546 547 548 549
	/* query firmware version */
	memcpy(cmd.args, "\x11", 1);
	cmd.wlen = 1;
	cmd.rlen = 10;
550
	ret = si2168_cmd_execute(client, &cmd);
551 552 553
	if (ret)
		goto err;

554
	dev_info(&client->dev, "firmware version: %c.%c.%d\n",
555 556
			cmd.args[6], cmd.args[7], cmd.args[8]);

557 558
	/* set ts mode */
	memcpy(cmd.args, "\x14\x00\x01\x10\x10\x00", 6);
559
	cmd.args[4] |= dev->ts_mode;
560 561
	if (dev->ts_clock_gapped)
		cmd.args[4] |= 0x40;
562 563
	cmd.wlen = 6;
	cmd.rlen = 4;
564
	ret = si2168_cmd_execute(client, &cmd);
565 566 567
	if (ret)
		goto err;

568
	dev->fw_loaded = true;
569
warm:
570
	dev->active = true;
571 572

	return 0;
573

574
err_release_firmware:
575 576
	release_firmware(fw);
err:
577
	dev_dbg(&client->dev, "failed=%d\n", ret);
578 579 580 581 582
	return ret;
}

static int si2168_sleep(struct dvb_frontend *fe)
{
583 584
	struct i2c_client *client = fe->demodulator_priv;
	struct si2168_dev *dev = i2c_get_clientdata(client);
585 586
	int ret;
	struct si2168_cmd cmd;
587

588
	dev_dbg(&client->dev, "\n");
589

590
	dev->active = false;
591

592 593 594
	memcpy(cmd.args, "\x13", 1);
	cmd.wlen = 1;
	cmd.rlen = 0;
595
	ret = si2168_cmd_execute(client, &cmd);
596 597 598
	if (ret)
		goto err;

599
	return 0;
600
err:
601
	dev_dbg(&client->dev, "failed=%d\n", ret);
602
	return ret;
603 604 605 606 607 608 609 610 611 612 613 614
}

static int si2168_get_tune_settings(struct dvb_frontend *fe,
	struct dvb_frontend_tune_settings *s)
{
	s->min_delay_ms = 900;

	return 0;
}

/*
 * I2C gate logic
615 616
 * We must use unlocked I2C I/O because I2C adapter lock is already taken
 * by the caller (usually tuner driver).
617
 */
618
static int si2168_select(struct i2c_mux_core *muxc, u32 chan)
619
{
620
	struct i2c_client *client = i2c_mux_priv(muxc);
621
	int ret;
622
	struct si2168_cmd cmd;
623

624 625 626 627 628 629 630
	/* open I2C gate */
	memcpy(cmd.args, "\xc0\x0d\x01", 3);
	cmd.wlen = 3;
	cmd.rlen = 0;
	ret = si2168_cmd_execute_unlocked(client, &cmd);
	if (ret)
		goto err;
631

632 633 634
	return 0;
err:
	dev_dbg(&client->dev, "failed=%d\n", ret);
635 636 637
	return ret;
}

638
static int si2168_deselect(struct i2c_mux_core *muxc, u32 chan)
639
{
640
	struct i2c_client *client = i2c_mux_priv(muxc);
641
	int ret;
642
	struct si2168_cmd cmd;
643

644 645 646 647 648 649 650
	/* close I2C gate */
	memcpy(cmd.args, "\xc0\x0d\x00", 3);
	cmd.wlen = 3;
	cmd.rlen = 0;
	ret = si2168_cmd_execute_unlocked(client, &cmd);
	if (ret)
		goto err;
651

652 653 654
	return 0;
err:
	dev_dbg(&client->dev, "failed=%d\n", ret);
655 656 657 658
	return ret;
}

static const struct dvb_frontend_ops si2168_ops = {
659
	.delsys = {SYS_DVBT, SYS_DVBT2, SYS_DVBC_ANNEX_A},
660 661
	.info = {
		.name = "Silicon Labs Si2168",
662 663
		.symbol_rate_min = 1000000,
		.symbol_rate_max = 7200000,
664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680
		.caps =	FE_CAN_FEC_1_2 |
			FE_CAN_FEC_2_3 |
			FE_CAN_FEC_3_4 |
			FE_CAN_FEC_5_6 |
			FE_CAN_FEC_7_8 |
			FE_CAN_FEC_AUTO |
			FE_CAN_QPSK |
			FE_CAN_QAM_16 |
			FE_CAN_QAM_32 |
			FE_CAN_QAM_64 |
			FE_CAN_QAM_128 |
			FE_CAN_QAM_256 |
			FE_CAN_QAM_AUTO |
			FE_CAN_TRANSMISSION_MODE_AUTO |
			FE_CAN_GUARD_INTERVAL_AUTO |
			FE_CAN_HIERARCHY_AUTO |
			FE_CAN_MUTE_TS |
681 682
			FE_CAN_2G_MODULATION |
			FE_CAN_MULTISTREAM
683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698
	},

	.get_tune_settings = si2168_get_tune_settings,

	.init = si2168_init,
	.sleep = si2168_sleep,

	.set_frontend = si2168_set_frontend,

	.read_status = si2168_read_status,
};

static int si2168_probe(struct i2c_client *client,
		const struct i2c_device_id *id)
{
	struct si2168_config *config = client->dev.platform_data;
699
	struct si2168_dev *dev;
700 701
	int ret;

O
Olli Salonen 已提交
702
	dev_dbg(&client->dev, "\n");
703

704 705
	dev = kzalloc(sizeof(*dev), GFP_KERNEL);
	if (!dev) {
706
		ret = -ENOMEM;
O
Olli Salonen 已提交
707
		dev_err(&client->dev, "kzalloc() failed\n");
708
		goto err;
709 710 711
	}

	/* create mux i2c adapter for tuner */
712 713 714 715 716
	dev->muxc = i2c_mux_alloc(client->adapter, &client->dev,
				  1, 0, 0,
				  si2168_select, si2168_deselect);
	if (!dev->muxc) {
		ret = -ENOMEM;
717
		goto err_kfree;
718
	}
719 720 721 722
	dev->muxc->priv = client;
	ret = i2c_mux_add_adapter(dev->muxc, 0, 0, 0);
	if (ret)
		goto err_kfree;
723 724

	/* create dvb_frontend */
725
	memcpy(&dev->fe.ops, &si2168_ops, sizeof(struct dvb_frontend_ops));
726
	dev->fe.demodulator_priv = client;
727
	*config->i2c_adapter = dev->muxc->adapter[0];
728 729 730
	*config->fe = &dev->fe;
	dev->ts_mode = config->ts_mode;
	dev->ts_clock_inv = config->ts_clock_inv;
731
	dev->ts_clock_gapped = config->ts_clock_gapped;
732
	dev->fw_loaded = false;
733

734
	i2c_set_clientdata(client, dev);
735

736
	dev_info(&client->dev, "Silicon Labs Si2168 successfully attached\n");
737
	return 0;
738
err_kfree:
739
	kfree(dev);
740
err:
O
Olli Salonen 已提交
741
	dev_dbg(&client->dev, "failed=%d\n", ret);
742 743 744 745 746
	return ret;
}

static int si2168_remove(struct i2c_client *client)
{
747
	struct si2168_dev *dev = i2c_get_clientdata(client);
748

O
Olli Salonen 已提交
749
	dev_dbg(&client->dev, "\n");
750

751
	i2c_mux_del_adapters(dev->muxc);
752

753 754
	dev->fe.ops.release = NULL;
	dev->fe.demodulator_priv = NULL;
755

756
	kfree(dev);
757 758 759 760

	return 0;
}

761
static const struct i2c_device_id si2168_id_table[] = {
762 763 764
	{"si2168", 0},
	{}
};
765
MODULE_DEVICE_TABLE(i2c, si2168_id_table);
766 767 768 769 770 771 772

static struct i2c_driver si2168_driver = {
	.driver = {
		.name	= "si2168",
	},
	.probe		= si2168_probe,
	.remove		= si2168_remove,
773
	.id_table	= si2168_id_table,
774 775 776 777 778 779 780
};

module_i2c_driver(si2168_driver);

MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
MODULE_DESCRIPTION("Silicon Labs Si2168 DVB-T/T2/C demodulator driver");
MODULE_LICENSE("GPL");
781
MODULE_FIRMWARE(SI2168_A20_FIRMWARE);
782
MODULE_FIRMWARE(SI2168_A30_FIRMWARE);
783
MODULE_FIRMWARE(SI2168_B40_FIRMWARE);