tuner-xc2028.c 26.2 KB
Newer Older
1 2 3
/* tuner-xc2028
 *
 * Copyright (c) 2007 Mauro Carvalho Chehab (mchehab@infradead.org)
4
 *
5 6
 * Copyright (c) 2007 Michel Ludwig (michel.ludwig@gmail.com)
 *       - frontend interface
7
 *
8 9 10 11 12 13
 * This code is placed under the terms of the GNU General Public License v2
 */

#include <linux/i2c.h>
#include <asm/div64.h>
#include <linux/firmware.h>
14
#include <linux/videodev2.h>
15
#include <linux/delay.h>
16
#include <media/tuner.h>
17
#include <linux/mutex.h>
18
#include "tuner-i2c.h"
19
#include "tuner-xc2028.h"
20
#include "tuner-xc2028-types.h"
21

22 23 24
#include <linux/dvb/frontend.h>
#include "dvb_frontend.h"

25

26
#define PREFIX "xc2028"
27

28 29 30 31
static int debug;
module_param(debug, int, 0644);
MODULE_PARM_DESC(debug, "enable verbose debug messages");

32 33 34 35 36 37 38 39 40 41 42 43 44 45
static char audio_std[8];
module_param_string(audio_std, audio_std, sizeof(audio_std), 0);
MODULE_PARM_DESC(audio_std,
	"Audio standard. XC3028 audio decoder explicitly "
	"needs to know what audio\n"
	"standard is needed for some video standards with audio A2 or NICAM.\n"
	"The valid values are:\n"
	"A2\n"
	"A2/A\n"
	"A2/B\n"
	"NICAM\n"
	"NICAM/A\n"
	"NICAM/B\n");

46
static LIST_HEAD(xc2028_list);
47 48
static DEFINE_MUTEX(xc2028_list_mutex);

49 50 51 52
/* struct for storing firmware table */
struct firmware_description {
	unsigned int  type;
	v4l2_std_id   id;
53
	__u16         int_freq;
54 55 56
	unsigned char *ptr;
	unsigned int  size;
};
57

58 59 60 61
struct firmware_properties {
	unsigned int	type;
	v4l2_std_id	id;
	v4l2_std_id	std_req;
62
	__u16		int_freq;
63 64 65 66
	unsigned int	scode_table;
	int 		scode_nr;
};

67
struct xc2028_data {
68 69 70 71 72 73
	struct list_head        xc2028_list;
	struct tuner_i2c_props  i2c_props;
	int                     (*tuner_callback) (void *dev,
						   int command, int arg);
	void			*video_dev;
	int			count;
74 75 76 77
	__u32			frequency;

	struct firmware_description *firm;
	int			firm_size;
78
	__u16			firm_version;
79

80 81 82
	__u16			hwmodel;
	__u16			hwvers;

83
	struct xc2028_ctrl	ctrl;
84

85
	struct firmware_properties cur_fw;
86 87

	struct mutex lock;
88 89
};

90 91 92 93 94 95 96 97 98 99 100 101 102
#define i2c_send(priv, buf, size) ({					\
	int _rc;							\
	_rc = tuner_i2c_xfer_send(&priv->i2c_props, buf, size);		\
	if (size != _rc)						\
		tuner_info("i2c output error: rc = %d (should be %d)\n",\
			   _rc, (int)size);				\
	_rc;								\
})

#define i2c_rcv(priv, buf, size) ({					\
	int _rc;							\
	_rc = tuner_i2c_xfer_recv(&priv->i2c_props, buf, size);		\
	if (size != _rc)						\
103
		tuner_err("i2c input error: rc = %d (should be %d)\n",	\
104 105 106
			   _rc, (int)size); 				\
	_rc;								\
})
107

108 109 110 111 112 113 114 115 116 117
#define i2c_send_recv(priv, obuf, osize, ibuf, isize) ({		\
	int _rc;							\
	_rc = tuner_i2c_xfer_send_recv(&priv->i2c_props, obuf, osize,	\
				       ibuf, isize);			\
	if (isize != _rc)						\
		tuner_err("i2c input error: rc = %d (should be %d)\n",	\
			   _rc, (int)isize); 				\
	_rc;								\
})

118
#define send_seq(priv, data...)	({					\
119
	static u8 _val[] = data;					\
120
	int _rc;							\
121
	if (sizeof(_val) !=						\
122
			(_rc = tuner_i2c_xfer_send(&priv->i2c_props,	\
123
						_val, sizeof(_val)))) {	\
124 125 126 127 128
		tuner_err("Error on line %d: %d\n", __LINE__, _rc);	\
	} else 								\
		msleep(10);						\
	_rc;								\
})
129

130
static unsigned int xc2028_get_reg(struct xc2028_data *priv, u16 reg, u16 *val)
131
{
132
	unsigned char buf[2];
133
	unsigned char ibuf[2];
134

135
	tuner_dbg("%s %04x called\n", __FUNCTION__, reg);
136

137
	buf[0] = reg >> 8;
138
	buf[1] = (unsigned char) reg;
139

140 141
	if (i2c_send_recv(priv, buf, 2, ibuf, 2) != 2)
		return -EIO;
142

143 144
	*val = (ibuf[1]) | (ibuf[0] << 8);
	return 0;
145 146
}

147 148 149 150
void dump_firm_type(unsigned int type)
{
	 if (type & BASE)
		printk("BASE ");
151 152
	 if (type & INIT1)
		printk("INIT1 ");
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210
	 if (type & F8MHZ)
		printk("F8MHZ ");
	 if (type & MTS)
		printk("MTS ");
	 if (type & D2620)
		printk("D2620 ");
	 if (type & D2633)
		printk("D2633 ");
	 if (type & DTV6)
		printk("DTV6 ");
	 if (type & QAM)
		printk("QAM ");
	 if (type & DTV7)
		printk("DTV7 ");
	 if (type & DTV78)
		printk("DTV78 ");
	 if (type & DTV8)
		printk("DTV8 ");
	 if (type & FM)
		printk("FM ");
	 if (type & INPUT1)
		printk("INPUT1 ");
	 if (type & LCD)
		printk("LCD ");
	 if (type & NOGD)
		printk("NOGD ");
	 if (type & MONO)
		printk("MONO ");
	 if (type & ATSC)
		printk("ATSC ");
	 if (type & IF)
		printk("IF ");
	 if (type & LG60)
		printk("LG60 ");
	 if (type & ATI638)
		printk("ATI638 ");
	 if (type & OREN538)
		printk("OREN538 ");
	 if (type & OREN36)
		printk("OREN36 ");
	 if (type & TOYOTA388)
		printk("TOYOTA388 ");
	 if (type & TOYOTA794)
		printk("TOYOTA794 ");
	 if (type & DIBCOM52)
		printk("DIBCOM52 ");
	 if (type & ZARLINK456)
		printk("ZARLINK456 ");
	 if (type & CHINA)
		printk("CHINA ");
	 if (type & F6MHZ)
		printk("F6MHZ ");
	 if (type & INPUT2)
		printk("INPUT2 ");
	 if (type & SCODE)
		printk("SCODE ");
}

211
static  v4l2_std_id parse_audio_std_option(void)
212
{
213
	if (strcasecmp(audio_std, "A2") == 0)
214
		return V4L2_STD_A2;
215
	if (strcasecmp(audio_std, "A2/A") == 0)
216
		return V4L2_STD_A2_A;
217
	if (strcasecmp(audio_std, "A2/B") == 0)
218
		return V4L2_STD_A2_B;
219
	if (strcasecmp(audio_std, "NICAM") == 0)
220
		return V4L2_STD_NICAM;
221
	if (strcasecmp(audio_std, "NICAM/A") == 0)
222
		return V4L2_STD_NICAM_A;
223
	if (strcasecmp(audio_std, "NICAM/B") == 0)
224 225 226 227 228
		return V4L2_STD_NICAM_B;

	return 0;
}

229
static void free_firmware(struct xc2028_data *priv)
230
{
231 232 233 234 235
	int i;

	if (!priv->firm)
		return;

236 237 238
	for (i = 0; i < priv->firm_size; i++)
		kfree(priv->firm[i].ptr);

239 240
	kfree(priv->firm);

241
	priv->firm = NULL;
242
	priv->firm_size = 0;
243 244

	memset(&priv->cur_fw, 0, sizeof(priv->cur_fw));
245 246
}

247
static int load_all_firmwares(struct dvb_frontend *fe)
248 249
{
	struct xc2028_data    *priv = fe->tuner_priv;
250
	const struct firmware *fw   = NULL;
251
	unsigned char         *p, *endp;
252 253
	int                   rc = 0;
	int		      n, n_array;
254
	char		      name[33];
255

256
	tuner_dbg("%s called\n", __FUNCTION__);
257

258
	tuner_dbg("Reading firmware %s\n", priv->ctrl.fname);
259 260
	rc = request_firmware(&fw, priv->ctrl.fname,
			      &priv->i2c_props.adap->dev);
261
	if (rc < 0) {
262
		if (rc == -ENOENT)
263
			tuner_err("Error: firmware %s not found.\n",
264
				   priv->ctrl.fname);
265
		else
266
			tuner_err("Error %d while requesting firmware %s \n",
267
				   rc, priv->ctrl.fname);
268

269 270
		return rc;
	}
271 272
	p = fw->data;
	endp = p + fw->size;
273

274 275 276 277
	if (fw->size < sizeof(name) - 1 + 2 + 2) {
		tuner_err("Error: firmware file %s has invalid size!\n",
			  priv->ctrl.fname);
		goto corrupt;
278
	}
279

280 281 282
	memcpy(name, p, sizeof(name) - 1);
	name[sizeof(name) - 1] = 0;
	p += sizeof(name) - 1;
283

284
	priv->firm_version = le16_to_cpu(*(__u16 *) p);
285 286
	p += 2;

287
	n_array = le16_to_cpu(*(__u16 *) p);
288 289
	p += 2;

290 291 292
	tuner_info("Loading %d firmware images from %s, type: %s, ver %d.%d\n",
		   n_array, priv->ctrl.fname, name,
		   priv->firm_version >> 8, priv->firm_version & 0xff);
293

294
	priv->firm = kzalloc(sizeof(*priv->firm) * n_array, GFP_KERNEL);
295 296
	if (priv->firm == NULL) {
		tuner_err("Not enough memory to load firmware file.\n");
297
		rc = -ENOMEM;
298
		goto err;
299
	}
300
	priv->firm_size = n_array;
301

302 303
	n = -1;
	while (p < endp) {
304 305
		__u32 type, size;
		v4l2_std_id id;
306
		__u16 int_freq = 0;
307 308 309

		n++;
		if (n >= n_array) {
310 311
			tuner_err("More firmware images in file than "
				  "were expected!\n");
312 313 314 315
			goto corrupt;
		}

		/* Checks if there's enough bytes to read */
316
		if (p + sizeof(type) + sizeof(id) + sizeof(size) > endp) {
317
			tuner_err("Firmware header is incomplete!\n");
318 319 320
			goto corrupt;
		}

321
		type = le32_to_cpu(*(__u32 *) p);
322 323
		p += sizeof(type);

324
		id = le64_to_cpu(*(v4l2_std_id *) p);
325 326
		p += sizeof(id);

327 328 329 330 331
		if (type & HAS_IF) {
			int_freq = le16_to_cpu(*(__u16 *) p);
			p += sizeof(int_freq);
		}

332
		size = le32_to_cpu(*(__u32 *) p);
333 334
		p += sizeof(size);

335
		if ((!size) || (size + p > endp)) {
336
			tuner_err("Firmware type ");
337
			dump_firm_type(type);
338 339
			printk("(%x), id %llx is corrupted "
			       "(size=%d, expected %d)\n",
340
			       type, (unsigned long long)id,
341
			       (unsigned)(endp - p), size);
342 343 344
			goto corrupt;
		}

345
		priv->firm[n].ptr = kzalloc(size, GFP_KERNEL);
346 347
		if (priv->firm[n].ptr == NULL) {
			tuner_err("Not enough memory to load firmware file.\n");
348
			rc = -ENOMEM;
349 350
			goto err;
		}
351 352 353 354 355 356
		tuner_dbg("Reading firmware type ");
		if (debug) {
			dump_firm_type(type);
			printk("(%x), id %llx, size=%d.\n",
				   type, (unsigned long long)id, size);
		}
357 358 359 360 361

		memcpy(priv->firm[n].ptr, p, size);
		priv->firm[n].type = type;
		priv->firm[n].id   = id;
		priv->firm[n].size = size;
362
		priv->firm[n].int_freq = int_freq;
363 364 365 366

		p += size;
	}

367
	if (n + 1 != priv->firm_size) {
368
		tuner_err("Firmware file is incomplete!\n");
369 370 371 372 373 374
		goto corrupt;
	}

	goto done;

corrupt:
375
	rc = -EINVAL;
376
	tuner_err("Error: firmware file is corrupted!\n");
377 378

err:
379
	tuner_info("Releasing partially loaded firmware file.\n");
380 381 382 383
	free_firmware(priv);

done:
	release_firmware(fw);
384 385
	if (rc == 0)
		tuner_dbg("Firmware files loaded.\n");
386 387 388 389

	return rc;
}

390 391
static int seek_firmware(struct dvb_frontend *fe, unsigned int type,
			 v4l2_std_id *id)
392 393
{
	struct xc2028_data *priv = fe->tuner_priv;
394
	int                 i, best_i = -1, best_nr_matches = 0;
395

396 397 398 399 400
	tuner_dbg("%s called, want type=", __FUNCTION__);
	if (debug) {
		dump_firm_type(type);
		printk("(%x), id %016llx.\n", type, (unsigned long long)*id);
	}
401 402

	if (!priv->firm) {
403
		tuner_err("Error! firmware not loaded\n");
404 405 406
		return -EINVAL;
	}

407
	if (((type & ~SCODE) == 0) && (*id == 0))
408
		*id = V4L2_STD_PAL;
409

410 411 412 413 414
	if (type & BASE)
		type &= BASE_TYPES;
	else if (type & SCODE)
		type &= SCODE_TYPES;
	else if (type & DTV_TYPES)
415 416 417
		type &= DTV_TYPES;
	else if (type & STD_SPECIFIC_TYPES)
		type &= STD_SPECIFIC_TYPES;
418

419
	/* Seek for exact match */
420 421
	for (i = 0; i < priv->firm_size; i++) {
		if ((type == priv->firm[i].type) && (*id == priv->firm[i].id))
422 423 424 425
			goto found;
	}

	/* Seek for generic video standard match */
426
	for (i = 0; i < priv->firm_size; i++) {
427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453
		v4l2_std_id match_mask;
		int nr_matches;

		if (type != priv->firm[i].type)
			continue;

		match_mask = *id & priv->firm[i].id;
		if (!match_mask)
			continue;

		if ((*id & match_mask) == *id)
			goto found; /* Supports all the requested standards */

		nr_matches = hweight64(match_mask);
		if (nr_matches > best_nr_matches) {
			best_nr_matches = nr_matches;
			best_i = i;
		}
	}

	if (best_nr_matches > 0) {
		tuner_dbg("Selecting best matching firmware (%d bits) for "
			  "type=", best_nr_matches);
		dump_firm_type(type);
		printk("(%x), id %016llx:\n", type, (unsigned long long)*id);
		i = best_i;
		goto found;
454 455 456 457
	}

	/*FIXME: Would make sense to seek for type "hint" match ? */

458
	i = -ENOENT;
459
	goto ret;
460 461 462 463

found:
	*id = priv->firm[i].id;

464
ret:
465
	tuner_dbg("%s firmware for type=", (i < 0) ? "Can't find" : "Found");
466 467
	if (debug) {
		dump_firm_type(type);
468
		printk("(%x), id %016llx.\n", type, (unsigned long long)*id);
469
	}
470 471 472 473 474 475 476 477
	return i;
}

static int load_firmware(struct dvb_frontend *fe, unsigned int type,
			 v4l2_std_id *id)
{
	struct xc2028_data *priv = fe->tuner_priv;
	int                pos, rc;
478
	unsigned char      *p, *endp, buf[priv->ctrl.max_len];
479

480
	tuner_dbg("%s called\n", __FUNCTION__);
481 482 483 484 485

	pos = seek_firmware(fe, type, id);
	if (pos < 0)
		return pos;

486
	tuner_info("Loading firmware for type=");
487 488 489
	dump_firm_type(priv->firm[pos].type);
	printk("(%x), id %016llx.\n", priv->firm[pos].type,
	       (unsigned long long)*id);
490

491 492
	p = priv->firm[pos].ptr;
	endp = p + priv->firm[pos].size;
493

494
	while (p < endp) {
495 496 497
		__u16 size;

		/* Checks if there's enough bytes to read */
498
		if (p + sizeof(size) > endp) {
499
			tuner_err("Firmware chunk size is wrong\n");
500 501 502
			return -EINVAL;
		}

503
		size = le16_to_cpu(*(__u16 *) p);
504 505 506 507 508 509
		p += sizeof(size);

		if (size == 0xffff)
			return 0;

		if (!size) {
510
			/* Special callback command received */
511
			rc = priv->tuner_callback(priv->video_dev,
512 513
						  XC2028_TUNER_RESET, 0);
			if (rc < 0) {
514
				tuner_err("Error at RESET code %d\n",
515
					   (*p) & 0x7f);
516
				return -EINVAL;
517 518 519
			}
			continue;
		}
520 521 522 523 524 525 526 527 528 529
		if (size >= 0xff00) {
			switch (size) {
			case 0xff00:
				rc = priv->tuner_callback(priv->video_dev,
							XC2028_RESET_CLK, 0);
				if (rc < 0) {
					tuner_err("Error at RESET code %d\n",
						  (*p) & 0x7f);
					return -EINVAL;
				}
530
				break;
531 532 533 534 535 536
			default:
				tuner_info("Invalid RESET code %d\n",
					   size & 0x7f);
				return -EINVAL;

			}
537
			continue;
538
		}
539 540 541

		/* Checks for a sleep command */
		if (size & 0x8000) {
542
			msleep(size & 0x7fff);
543
			continue;
544 545
		}

546
		if ((size + p > endp)) {
547
			tuner_err("missing bytes: need %d, have %d\n",
548
				   size, (int)(endp - p));
549 550
			return -EINVAL;
		}
551

552
		buf[0] = *p;
553
		p++;
554
		size--;
555

556
		/* Sends message chunks */
557
		while (size > 0) {
558 559
			int len = (size < priv->ctrl.max_len - 1) ?
				   size : priv->ctrl.max_len - 1;
560

561
			memcpy(buf + 1, p, len);
562

563
			rc = i2c_send(priv, buf, len + 1);
564
			if (rc < 0) {
565
				tuner_err("%d returned from send\n", rc);
566 567 568 569 570 571 572
				return -EINVAL;
			}

			p += len;
			size -= len;
		}
	}
573
	return 0;
574 575
}

576
static int load_scode(struct dvb_frontend *fe, unsigned int type,
577
			 v4l2_std_id *id, __u16 int_freq, int scode)
578 579 580 581 582
{
	struct xc2028_data *priv = fe->tuner_priv;
	int                pos, rc;
	unsigned char	   *p;

583
	tuner_dbg("%s called\n", __FUNCTION__);
584

585 586 587 588 589 590 591 592 593 594 595 596 597
	if (!int_freq) {
		pos = seek_firmware(fe, type, id);
		if (pos < 0)
			return pos;
	} else {
		for (pos = 0; pos < priv->firm_size; pos++) {
			if ((priv->firm[pos].int_freq == int_freq) &&
			    (type & HAS_IF))
				break;
		}
		if (pos == priv->firm_size)
			return -ENOENT;
	}
598 599 600

	p = priv->firm[pos].ptr;

601 602 603 604 605 606 607 608 609 610 611 612
	if (type & HAS_IF) {
		if (priv->firm[pos].size != 12 * 16 || scode >= 16)
			return -EINVAL;
		p += 12 * scode;
	} else {
		/* 16 SCODE entries per file; each SCODE entry is 12 bytes and
		 * has a 2-byte size header in the firmware format. */
		if (priv->firm[pos].size != 14 * 16 || scode >= 16 ||
		    le16_to_cpu(*(__u16 *)(p + 14 * scode)) != 12)
			return -EINVAL;
		p += 14 * scode + 2;
	}
613

614 615 616 617 618
	tuner_info("Loading SCODE for type=");
	dump_firm_type(priv->firm[pos].type);
	printk("(%x), id %016llx.\n", priv->firm[pos].type,
	       (unsigned long long)*id);

619
	if (priv->firm_version < 0x0202)
620 621 622 623 624
		rc = send_seq(priv, {0x20, 0x00, 0x00, 0x00});
	else
		rc = send_seq(priv, {0xa0, 0x00, 0x00, 0x00});
	if (rc < 0)
		return -EIO;
625

626
	rc = i2c_send(priv, p, 12);
627 628
	if (rc < 0)
		return -EIO;
629

630 631 632
	rc = send_seq(priv, {0x00, 0x8c});
	if (rc < 0)
		return -EIO;
633 634 635 636

	return 0;
}

637
static int check_firmware(struct dvb_frontend *fe, unsigned int type,
638
			  v4l2_std_id std, __u16 int_freq)
639
{
640
	struct xc2028_data         *priv = fe->tuner_priv;
641
	struct firmware_properties new_fw;
642 643 644
	int			   rc = 0, is_retry = 0;
	u16			   version, hwmodel;
	v4l2_std_id		   std0;
645

646
	tuner_dbg("%s called\n", __FUNCTION__);
647

648
	if (!priv->firm) {
649 650
		if (!priv->ctrl.fname) {
			tuner_info("xc2028/3028 firmware name not set!\n");
651
			return -EINVAL;
652
		}
653

654 655
		rc = load_all_firmwares(fe);
		if (rc < 0)
656 657 658
			return rc;
	}

659
	if (priv->ctrl.mts)
660
		type |= MTS;
661

662
retry:
663 664 665 666 667
	new_fw.type = type;
	new_fw.id = std;
	new_fw.std_req = std;
	new_fw.scode_table = SCODE | priv->ctrl.scode_table;
	new_fw.scode_nr = 0;
668
	new_fw.int_freq = int_freq;
669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695

	tuner_dbg("checking firmware, user requested type=");
	if (debug) {
		dump_firm_type(new_fw.type);
		printk("(%x), id %016llx, scode_tbl ", new_fw.type,
		       (unsigned long long)new_fw.std_req);
		dump_firm_type(priv->ctrl.scode_table);
		printk("(%x), scode_nr %d\n", priv->ctrl.scode_table,
		       new_fw.scode_nr);
	}

	/* No need to reload base firmware if it matches */
	if (((BASE | new_fw.type) & BASE_TYPES) ==
	    (priv->cur_fw.type & BASE_TYPES)) {
		tuner_dbg("BASE firmware not changed.\n");
		goto skip_base;
	}

	/* Updating BASE - forget about all currently loaded firmware */
	memset(&priv->cur_fw, 0, sizeof(priv->cur_fw));

	/* Reset is needed before loading firmware */
	rc = priv->tuner_callback(priv->video_dev,
				  XC2028_TUNER_RESET, 0);
	if (rc < 0)
		goto fail;

696 697 698
	/* BASE firmwares are all std0 */
	std0 = 0;
	rc = load_firmware(fe, BASE | new_fw.type, &std0);
699 700 701 702 703
	if (rc < 0) {
		tuner_err("Error %d while loading base firmware\n",
			  rc);
		goto fail;
	}
704

705
	/* Load INIT1, if needed */
706
	tuner_dbg("Load init1 firmware, if exists\n");
707

708
	rc = load_firmware(fe, BASE | INIT1 | new_fw.type, &std0);
709 710 711
	if (rc == -ENOENT)
		rc = load_firmware(fe, (BASE | INIT1 | new_fw.type) & ~F8MHZ,
				   &std0);
712 713 714 715 716
	if (rc < 0 && rc != -ENOENT) {
		tuner_err("Error %d while loading init1 firmware\n",
			  rc);
		goto fail;
	}
717

718 719 720 721
skip_base:
	/*
	 * No need to reload standard specific firmware if base firmware
	 * was not reloaded and requested video standards have not changed.
722
	 */
723 724
	if (priv->cur_fw.type == (BASE | new_fw.type) &&
	    priv->cur_fw.std_req == std) {
725
		tuner_dbg("Std-specific firmware already loaded.\n");
726
		goto skip_std_specific;
727
	}
728

729 730 731 732
	/* Reloading std-specific firmware forces a SCODE update */
	priv->cur_fw.scode_table = 0;

	rc = load_firmware(fe, new_fw.type, &new_fw.id);
733 734 735
	if (rc == -ENOENT)
		rc = load_firmware(fe, new_fw.type & ~F8MHZ, &new_fw.id);

736
	if (rc < 0)
737 738 739 740 741 742 743 744
		goto fail;

skip_std_specific:
	if (priv->cur_fw.scode_table == new_fw.scode_table &&
	    priv->cur_fw.scode_nr == new_fw.scode_nr) {
		tuner_dbg("SCODE firmware already loaded.\n");
		goto check_device;
	}
745

746
	/* Load SCODE firmware, if exists */
747
	tuner_dbg("Trying to load scode %d\n", new_fw.scode_nr);
748

749 750
	rc = load_scode(fe, new_fw.type | new_fw.scode_table, &new_fw.id,
			new_fw.int_freq, new_fw.scode_nr);
751

752
check_device:
753 754 755 756 757
	if (xc2028_get_reg(priv, 0x0004, &version) < 0 ||
	    xc2028_get_reg(priv, 0x0008, &hwmodel) < 0) {
		tuner_err("Unable to read tuner registers.\n");
		goto fail;
	}
758 759 760 761 762

	tuner_info("Device is Xceive %d version %d.%d, "
		   "firmware version %d.%d\n",
		   hwmodel, (version & 0xf000) >> 12, (version & 0xf00) >> 8,
		   (version & 0xf0) >> 4, version & 0xf);
763

764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780
	/* Check firmware version against what we downloaded. */
	if (priv->firm_version != ((version & 0xf0) << 4 | (version & 0x0f))) {
		tuner_err("Incorrect readback of firmware version.\n");
		goto fail;
	}

	/* Check that the tuner hardware model remains consistent over time. */
	if (priv->hwmodel == 0 && (hwmodel == 2028 || hwmodel == 3028)) {
		priv->hwmodel = hwmodel;
		priv->hwvers  = version & 0xff00;
	} else if (priv->hwmodel == 0 || priv->hwmodel != hwmodel ||
		   priv->hwvers != (version & 0xff00)) {
		tuner_err("Read invalid device hardware information - tuner "
			  "hung?\n");
		goto fail;
	}

781 782 783 784 785 786 787 788 789
	memcpy(&priv->cur_fw, &new_fw, sizeof(priv->cur_fw));

	/*
	 * By setting BASE in cur_fw.type only after successfully loading all
	 * firmwares, we can:
	 * 1. Identify that BASE firmware with type=0 has been loaded;
	 * 2. Tell whether BASE firmware was just changed the next time through.
	 */
	priv->cur_fw.type |= BASE;
790 791

	return 0;
792 793 794

fail:
	memset(&priv->cur_fw, 0, sizeof(priv->cur_fw));
795 796 797 798 799 800 801
	if (!is_retry) {
		msleep(50);
		is_retry = 1;
		tuner_dbg("Retrying firmware load\n");
		goto retry;
	}

802 803 804
	if (rc == -ENOENT)
		rc = -EINVAL;
	return rc;
805 806
}

807
static int xc2028_signal(struct dvb_frontend *fe, u16 *strength)
808
{
809
	struct xc2028_data *priv = fe->tuner_priv;
810 811
	u16                 frq_lock, signal = 0;
	int                 rc;
812

813
	tuner_dbg("%s called\n", __FUNCTION__);
814

815
	mutex_lock(&priv->lock);
816

817
	/* Sync Lock Indicator */
818 819
	rc = xc2028_get_reg(priv, 0x0002, &frq_lock);
	if (rc < 0 || frq_lock == 0)
820
		goto ret;
821 822 823

	/* Frequency is locked. Return signal quality */

824
	/* Get SNR of the video signal */
825 826 827
	rc = xc2028_get_reg(priv, 0x0040, &signal);
	if (rc < 0)
		signal = -frq_lock;
828 829

ret:
830 831 832
	mutex_unlock(&priv->lock);

	*strength = signal;
833

834
	return rc;
835 836 837 838
}

#define DIV 15625

839
static int generic_set_freq(struct dvb_frontend *fe, u32 freq /* in HZ */,
840 841 842 843
			    enum tuner_mode new_mode,
			    unsigned int type,
			    v4l2_std_id std,
			    u16 int_freq)
844
{
845
	struct xc2028_data *priv = fe->tuner_priv;
846
	int		   rc = -EINVAL;
847
	unsigned char	   buf[4];
848
	u32		   div, offset = 0;
849

850
	tuner_dbg("%s called\n", __FUNCTION__);
851

852 853
	mutex_lock(&priv->lock);

854
	tuner_dbg("should set frequency %d kHz\n", freq / 1000);
855

856
	if (check_firmware(fe, type, std, int_freq) < 0)
857
		goto ret;
858

859 860 861 862 863 864 865
	/* On some cases xc2028 can disable video output, if
	 * very weak signals are received. By sending a soft
	 * reset, this is re-enabled. So, it is better to always
	 * send a soft reset before changing channels, to be sure
	 * that xc2028 will be in a safe state.
	 * Maybe this might also be needed for DTV.
	 */
866
	if (new_mode == T_ANALOG_TV) {
867
		rc = send_seq(priv, {0x00, 0x00});
868
	} else {
869
		offset = 2750000;
870
		if (priv->cur_fw.type & DTV7)
871 872
			offset -= 500000;
	}
873

874
	div = (freq - offset + DIV / 2) / DIV;
875

876
	/* CMD= Set frequency */
877
	if (priv->firm_version < 0x0202)
878 879 880 881 882
		rc = send_seq(priv, {0x00, 0x02, 0x00, 0x00});
	else
		rc = send_seq(priv, {0x80, 0x02, 0x00, 0x00});
	if (rc < 0)
		goto ret;
883

884
	rc = priv->tuner_callback(priv->video_dev, XC2028_RESET_CLK, 1);
885
	if (rc < 0)
886
		goto ret;
887 888

	msleep(10);
889

890 891 892 893
	buf[0] = 0xff & (div >> 24);
	buf[1] = 0xff & (div >> 16);
	buf[2] = 0xff & (div >> 8);
	buf[3] = 0xff & (div);
894

895
	rc = i2c_send(priv, buf, sizeof(buf));
896
	if (rc < 0)
897
		goto ret;
898 899
	msleep(100);

900
	priv->frequency = freq;
901

902 903 904
	tuner_dbg("divisor= %02x %02x %02x %02x (freq=%d.%03d)\n",
	       buf[0], buf[1], buf[2], buf[3],
	       freq / 1000000, (freq % 1000000) / 1000);
905

906
	rc = 0;
907

908 909
ret:
	mutex_unlock(&priv->lock);
910

911
	return rc;
912 913
}

914
static int xc2028_set_analog_freq(struct dvb_frontend *fe,
915
			      struct analog_parameters *p)
916
{
917
	struct xc2028_data *priv = fe->tuner_priv;
918 919 920
	unsigned int       type=0;

	tuner_dbg("%s called\n", __FUNCTION__);
921

922 923 924 925 926
	if (p->mode == V4L2_TUNER_RADIO) {
		type |= FM;
		if (priv->ctrl.input1)
			type |= INPUT1;
		return generic_set_freq(fe, (625l * p->frequency) / 10,
927
				T_ANALOG_TV, type, 0, 0);
928 929
	}

930 931 932 933 934
	/* if std is not defined, choose one */
	if (!p->std)
		p->std = V4L2_STD_MN;

	/* PAL/M, PAL/N, PAL/Nc and NTSC variants should use 6MHz firmware */
935 936
	if (!(p->std & V4L2_STD_MN))
		type |= F8MHZ;
937

938 939
	/* Add audio hack to std mask */
	p->std |= parse_audio_std_option();
940

941
	return generic_set_freq(fe, 62500l * p->frequency,
942
				T_ANALOG_TV, type, p->std, 0);
943
}
944

945 946
static int xc2028_set_params(struct dvb_frontend *fe,
			     struct dvb_frontend_parameters *p)
947
{
948
	struct xc2028_data *priv = fe->tuner_priv;
949
	unsigned int       type=0;
950
	fe_bandwidth_t     bw = BANDWIDTH_8_MHZ;
951

952
	tuner_dbg("%s called\n", __FUNCTION__);
953

954 955 956 957 958
	if (priv->ctrl.d2633)
		type |= D2633;
	else
		type |= D2620;

959 960 961 962 963
	switch(fe->ops.info.type) {
	case FE_OFDM:
		bw = p->u.ofdm.bandwidth;
		break;
	case FE_QAM:
964 965 966 967
		tuner_info("WARN: There are some reports that "
			   "QAM 6 MHz doesn't work.\n"
			   "If this works for you, please report by "
			   "e-mail to: v4l-dvb-maintainer@linuxtv.org\n");
968 969 970 971 972
		bw = BANDWIDTH_6_MHZ;
		type |= QAM;
		break;
	case FE_ATSC:
		bw = BANDWIDTH_6_MHZ;
973 974
		/* The only ATSC firmware (at least on v2.7) is D2633,
		   so overrides ctrl->d2633 */
975
		type |= ATSC| D2633;
976
		type &= ~D2620;
977
		break;
978 979 980
	/* DVB-S is not supported */
	default:
		return -EINVAL;
981 982 983 984 985 986 987
	}

	/* FIXME:
	  There are two Scodes that will never be selected:
		DTV78 ZARLINK456, DTV78 DIBCOM52
	  When it should opt for DTV78 instead of DTV7 or DTV8?
	*/
988 989
	switch (bw) {
	case BANDWIDTH_8_MHZ:
990
		type |= DTV8 | F8MHZ;
991 992
		break;
	case BANDWIDTH_7_MHZ:
993
		type |= DTV7 | F8MHZ;
994 995
		break;
	case BANDWIDTH_6_MHZ:
996
		type |= DTV6 ;
997 998 999 1000 1001
		break;
	default:
		tuner_err("error: bandwidth not supported.\n");
	};

1002 1003 1004
	/* All S-code tables need a 200kHz shift */
	if (priv->ctrl.demod)
		priv->ctrl.demod += 200;
1005

1006
	return generic_set_freq(fe, p->frequency,
1007
				T_DIGITAL_TV, type, 0, priv->ctrl.demod);
1008
}
1009

1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031
static int xc2028_sleep(struct dvb_frontend *fe)
{
	struct xc2028_data *priv = fe->tuner_priv;
	int rc = 0;

	tuner_dbg("%s called\n", __FUNCTION__);

	mutex_lock(&priv->lock);

	if (priv->firm_version < 0x0202)
		rc = send_seq(priv, {0x00, 0x08, 0x00, 0x00});
	else
		rc = send_seq(priv, {0x80, 0x08, 0x00, 0x00});

	priv->cur_fw.type = 0;	/* need firmware reload */

	mutex_unlock(&priv->lock);

	return rc;
}


1032
static int xc2028_dvb_release(struct dvb_frontend *fe)
1033
{
1034 1035
	struct xc2028_data *priv = fe->tuner_priv;

1036
	tuner_dbg("%s called\n", __FUNCTION__);
1037

1038 1039
	mutex_lock(&xc2028_list_mutex);

1040
	priv->count--;
1041

1042
	if (!priv->count) {
1043 1044
		list_del(&priv->xc2028_list);

1045
		kfree(priv->ctrl.fname);
1046 1047

		free_firmware(priv);
1048
		kfree(priv);
1049
		fe->tuner_priv = NULL;
1050
	}
1051

1052 1053
	mutex_unlock(&xc2028_list_mutex);

1054 1055 1056
	return 0;
}

1057
static int xc2028_get_frequency(struct dvb_frontend *fe, u32 *frequency)
1058
{
1059
	struct xc2028_data *priv = fe->tuner_priv;
1060

1061
	tuner_dbg("%s called\n", __FUNCTION__);
1062

1063
	*frequency = priv->frequency;
1064 1065 1066 1067

	return 0;
}

1068
static int xc2028_set_config(struct dvb_frontend *fe, void *priv_cfg)
1069 1070 1071
{
	struct xc2028_data *priv = fe->tuner_priv;
	struct xc2028_ctrl *p    = priv_cfg;
1072
	int                 rc   = 0;
1073

1074
	tuner_dbg("%s called\n", __FUNCTION__);
1075

1076 1077
	mutex_lock(&priv->lock);

1078 1079
	kfree(priv->ctrl.fname);
	free_firmware(priv);
1080

1081 1082
	memcpy(&priv->ctrl, p, sizeof(priv->ctrl));
	priv->ctrl.fname = NULL;
1083

1084 1085 1086 1087
	if (p->fname) {
		priv->ctrl.fname = kstrdup(p->fname, GFP_KERNEL);
		if (priv->ctrl.fname == NULL)
			rc = -ENOMEM;
1088 1089
	}

1090 1091
	if (priv->ctrl.max_len < 9)
		priv->ctrl.max_len = 13;
1092

1093 1094
	mutex_unlock(&priv->lock);

1095
	return rc;
1096 1097
}

1098
static const struct dvb_tuner_ops xc2028_dvb_tuner_ops = {
1099
	.info = {
1100 1101 1102 1103 1104
		 .name = "Xceive XC3028",
		 .frequency_min = 42000000,
		 .frequency_max = 864000000,
		 .frequency_step = 50000,
		 },
1105

1106
	.set_config	   = xc2028_set_config,
1107
	.set_analog_params = xc2028_set_analog_freq,
1108 1109 1110 1111
	.release           = xc2028_dvb_release,
	.get_frequency     = xc2028_get_frequency,
	.get_rf_strength   = xc2028_signal,
	.set_params        = xc2028_set_params,
1112
	.sleep             = xc2028_sleep,
1113 1114 1115

};

1116
void *xc2028_attach(struct dvb_frontend *fe, struct xc2028_config *cfg)
1117
{
1118
	struct xc2028_data *priv;
1119
	void               *video_dev;
1120

1121
	if (debug)
1122
		printk(KERN_DEBUG PREFIX ": Xcv2028/3028 init called!\n");
1123

1124
	if (NULL == cfg || NULL == cfg->video_dev)
1125
		return NULL;
1126

1127
	if (!fe) {
1128
		printk(KERN_ERR PREFIX ": No frontend!\n");
1129
		return NULL;
1130 1131
	}

1132 1133
	video_dev = cfg->video_dev;

1134 1135
	mutex_lock(&xc2028_list_mutex);

1136
	list_for_each_entry(priv, &xc2028_list, xc2028_list) {
1137 1138 1139 1140
		if (priv->video_dev == cfg->video_dev) {
			video_dev = NULL;
			break;
		}
1141 1142
	}

1143
	if (video_dev) {
1144
		priv = kzalloc(sizeof(*priv), GFP_KERNEL);
1145 1146
		if (priv == NULL) {
			mutex_unlock(&xc2028_list_mutex);
1147
			return NULL;
1148
		}
1149

1150 1151
		priv->i2c_props.addr = cfg->i2c_addr;
		priv->i2c_props.adap = cfg->i2c_adap;
1152
		priv->video_dev = video_dev;
1153
		priv->tuner_callback = cfg->callback;
1154
		priv->ctrl.max_len = 13;
1155

1156 1157
		mutex_init(&priv->lock);

1158
		list_add_tail(&priv->xc2028_list, &xc2028_list);
1159
	}
1160 1161

	fe->tuner_priv = priv;
1162
	priv->count++;
1163 1164

	memcpy(&fe->ops.tuner_ops, &xc2028_dvb_tuner_ops,
1165
	       sizeof(xc2028_dvb_tuner_ops));
1166 1167 1168

	tuner_info("type set to %s\n", "XCeive xc2028/xc3028 tuner");

1169 1170 1171
	if (cfg->ctrl)
		xc2028_set_config(fe, cfg->ctrl);

1172 1173
	mutex_unlock(&xc2028_list_mutex);

1174
	return fe;
1175
}
1176

1177 1178
EXPORT_SYMBOL(xc2028_attach);

1179
MODULE_DESCRIPTION("Xceive xc2028/xc3028 tuner driver");
1180
MODULE_AUTHOR("Michel Ludwig <michel.ludwig@gmail.com>");
1181 1182
MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@infradead.org>");
MODULE_LICENSE("GPL");