tuner-xc2028.c 29.0 KB
Newer Older
1 2
/* tuner-xc2028
 *
3
 * Copyright (c) 2007-2008 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 <asm/unaligned.h>
19
#include "tuner-i2c.h"
20
#include "tuner-xc2028.h"
21
#include "tuner-xc2028-types.h"
22

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

26

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

31 32 33 34 35 36
static int no_poweroff;
module_param(no_poweroff, int, 0644);
MODULE_PARM_DESC(debug, "0 (default) powers device off when not used.\n"
	"1 keep device energized and with tuner ready all the times.\n"
	"  Faster, but consumes more power and keeps the device hotter\n");

37 38 39 40 41 42 43 44 45 46 47 48 49 50
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");

51 52 53 54 55
static char firmware_name[FIRMWARE_NAME_MAX];
module_param_string(firmware_name, firmware_name, sizeof(firmware_name), 0);
MODULE_PARM_DESC(firmware_name, "Firmware file name. Allows overriding the "
				"default firmware name\n");

56
static LIST_HEAD(hybrid_tuner_instance_list);
57 58
static DEFINE_MUTEX(xc2028_list_mutex);

59 60 61 62
/* struct for storing firmware table */
struct firmware_description {
	unsigned int  type;
	v4l2_std_id   id;
63
	__u16         int_freq;
64 65 66
	unsigned char *ptr;
	unsigned int  size;
};
67

68 69 70 71
struct firmware_properties {
	unsigned int	type;
	v4l2_std_id	id;
	v4l2_std_id	std_req;
72
	__u16		int_freq;
73 74 75 76
	unsigned int	scode_table;
	int 		scode_nr;
};

77
struct xc2028_data {
78
	struct list_head        hybrid_tuner_instance_list;
79
	struct tuner_i2c_props  i2c_props;
80 81 82 83
	__u32			frequency;

	struct firmware_description *firm;
	int			firm_size;
84
	__u16			firm_version;
85

86 87 88
	__u16			hwmodel;
	__u16			hwvers;

89
	struct xc2028_ctrl	ctrl;
90

91
	struct firmware_properties cur_fw;
92 93

	struct mutex lock;
94 95
};

96 97 98 99 100 101 102 103 104 105 106 107 108
#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)						\
109
		tuner_err("i2c input error: rc = %d (should be %d)\n",	\
110 111 112
			   _rc, (int)size); 				\
	_rc;								\
})
113

114 115 116 117 118 119 120 121 122 123
#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;								\
})

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

136
static int xc2028_get_reg(struct xc2028_data *priv, u16 reg, u16 *val)
137
{
138
	unsigned char buf[2];
139
	unsigned char ibuf[2];
140

141
	tuner_dbg("%s %04x called\n", __func__, reg);
142

143
	buf[0] = reg >> 8;
144
	buf[1] = (unsigned char) reg;
145

146 147
	if (i2c_send_recv(priv, buf, 2, ibuf, 2) != 2)
		return -EIO;
148

149 150
	*val = (ibuf[1]) | (ibuf[0] << 8);
	return 0;
151 152
}

153
#define dump_firm_type(t) 	dump_firm_type_and_int_freq(t, 0)
154
static void dump_firm_type_and_int_freq(unsigned int type, u16 int_freq)
155 156 157
{
	 if (type & BASE)
		printk("BASE ");
158 159
	 if (type & INIT1)
		printk("INIT1 ");
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 211 212 213 214 215
	 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 ");
216 217
	 if (type & HAS_IF)
		printk("HAS_IF_%d ", int_freq);
218 219
}

220
static  v4l2_std_id parse_audio_std_option(void)
221
{
222
	if (strcasecmp(audio_std, "A2") == 0)
223
		return V4L2_STD_A2;
224
	if (strcasecmp(audio_std, "A2/A") == 0)
225
		return V4L2_STD_A2_A;
226
	if (strcasecmp(audio_std, "A2/B") == 0)
227
		return V4L2_STD_A2_B;
228
	if (strcasecmp(audio_std, "NICAM") == 0)
229
		return V4L2_STD_NICAM;
230
	if (strcasecmp(audio_std, "NICAM/A") == 0)
231
		return V4L2_STD_NICAM_A;
232
	if (strcasecmp(audio_std, "NICAM/B") == 0)
233 234 235 236 237
		return V4L2_STD_NICAM_B;

	return 0;
}

238
static void free_firmware(struct xc2028_data *priv)
239
{
240
	int i;
241
	tuner_dbg("%s called\n", __func__);
242 243 244 245

	if (!priv->firm)
		return;

246 247 248
	for (i = 0; i < priv->firm_size; i++)
		kfree(priv->firm[i].ptr);

249 250
	kfree(priv->firm);

251
	priv->firm = NULL;
252
	priv->firm_size = 0;
253 254

	memset(&priv->cur_fw, 0, sizeof(priv->cur_fw));
255 256
}

257
static int load_all_firmwares(struct dvb_frontend *fe)
258 259
{
	struct xc2028_data    *priv = fe->tuner_priv;
260
	const struct firmware *fw   = NULL;
261
	const unsigned char   *p, *endp;
262 263
	int                   rc = 0;
	int		      n, n_array;
264
	char		      name[33];
265
	char		      *fname;
266

267
	tuner_dbg("%s called\n", __func__);
268

269 270 271 272 273 274 275
	if (!firmware_name[0])
		fname = priv->ctrl.fname;
	else
		fname = firmware_name;

	tuner_dbg("Reading firmware %s\n", fname);
	rc = request_firmware(&fw, fname, &priv->i2c_props.adap->dev);
276
	if (rc < 0) {
277
		if (rc == -ENOENT)
278
			tuner_err("Error: firmware %s not found.\n",
279
				   fname);
280
		else
281
			tuner_err("Error %d while requesting firmware %s \n",
282
				   rc, fname);
283

284 285
		return rc;
	}
286 287
	p = fw->data;
	endp = p + fw->size;
288

289 290
	if (fw->size < sizeof(name) - 1 + 2 + 2) {
		tuner_err("Error: firmware file %s has invalid size!\n",
291
			  fname);
292
		goto corrupt;
293
	}
294

295 296 297
	memcpy(name, p, sizeof(name) - 1);
	name[sizeof(name) - 1] = 0;
	p += sizeof(name) - 1;
298

299
	priv->firm_version = get_unaligned_le16(p);
300 301
	p += 2;

302
	n_array = get_unaligned_le16(p);
303 304
	p += 2;

305
	tuner_info("Loading %d firmware images from %s, type: %s, ver %d.%d\n",
306
		   n_array, fname, name,
307
		   priv->firm_version >> 8, priv->firm_version & 0xff);
308

309
	priv->firm = kzalloc(sizeof(*priv->firm) * n_array, GFP_KERNEL);
310 311
	if (priv->firm == NULL) {
		tuner_err("Not enough memory to load firmware file.\n");
312
		rc = -ENOMEM;
313
		goto err;
314
	}
315
	priv->firm_size = n_array;
316

317 318
	n = -1;
	while (p < endp) {
319 320
		__u32 type, size;
		v4l2_std_id id;
321
		__u16 int_freq = 0;
322 323 324

		n++;
		if (n >= n_array) {
325 326
			tuner_err("More firmware images in file than "
				  "were expected!\n");
327 328 329 330
			goto corrupt;
		}

		/* Checks if there's enough bytes to read */
331 332
		if (endp - p < sizeof(type) + sizeof(id) + sizeof(size))
			goto header;
333

334
		type = get_unaligned_le32(p);
335 336
		p += sizeof(type);

337
		id = get_unaligned_le64(p);
338 339
		p += sizeof(id);

340
		if (type & HAS_IF) {
341
			int_freq = get_unaligned_le16(p);
342
			p += sizeof(int_freq);
343 344
			if (endp - p < sizeof(size))
				goto header;
345 346
		}

347
		size = get_unaligned_le32(p);
348 349
		p += sizeof(size);

350
		if (!size || size > endp - p) {
351
			tuner_err("Firmware type ");
352
			dump_firm_type(type);
353 354
			printk("(%x), id %llx is corrupted "
			       "(size=%d, expected %d)\n",
355
			       type, (unsigned long long)id,
356
			       (unsigned)(endp - p), size);
357 358 359
			goto corrupt;
		}

360
		priv->firm[n].ptr = kzalloc(size, GFP_KERNEL);
361 362
		if (priv->firm[n].ptr == NULL) {
			tuner_err("Not enough memory to load firmware file.\n");
363
			rc = -ENOMEM;
364 365
			goto err;
		}
366 367
		tuner_dbg("Reading firmware type ");
		if (debug) {
368
			dump_firm_type_and_int_freq(type, int_freq);
369
			printk("(%x), id %llx, size=%d.\n",
370
			       type, (unsigned long long)id, size);
371
		}
372 373 374 375 376

		memcpy(priv->firm[n].ptr, p, size);
		priv->firm[n].type = type;
		priv->firm[n].id   = id;
		priv->firm[n].size = size;
377
		priv->firm[n].int_freq = int_freq;
378 379 380 381

		p += size;
	}

382
	if (n + 1 != priv->firm_size) {
383
		tuner_err("Firmware file is incomplete!\n");
384 385 386 387 388
		goto corrupt;
	}

	goto done;

389 390
header:
	tuner_err("Firmware header is incomplete!\n");
391
corrupt:
392
	rc = -EINVAL;
393
	tuner_err("Error: firmware file is corrupted!\n");
394 395

err:
396
	tuner_info("Releasing partially loaded firmware file.\n");
397 398 399 400
	free_firmware(priv);

done:
	release_firmware(fw);
401 402
	if (rc == 0)
		tuner_dbg("Firmware files loaded.\n");
403 404 405 406

	return rc;
}

407 408
static int seek_firmware(struct dvb_frontend *fe, unsigned int type,
			 v4l2_std_id *id)
409 410
{
	struct xc2028_data *priv = fe->tuner_priv;
411
	int                 i, best_i = -1, best_nr_matches = 0;
412
	unsigned int        type_mask = 0;
413

414
	tuner_dbg("%s called, want type=", __func__);
415 416 417 418
	if (debug) {
		dump_firm_type(type);
		printk("(%x), id %016llx.\n", type, (unsigned long long)*id);
	}
419 420

	if (!priv->firm) {
421
		tuner_err("Error! firmware not loaded\n");
422 423 424
		return -EINVAL;
	}

425
	if (((type & ~SCODE) == 0) && (*id == 0))
426
		*id = V4L2_STD_PAL;
427

428
	if (type & BASE)
429
		type_mask = BASE_TYPES;
430
	else if (type & SCODE) {
431
		type &= SCODE_TYPES;
432
		type_mask = SCODE_TYPES & ~HAS_IF;
433
	} else if (type & DTV_TYPES)
434
		type_mask = DTV_TYPES;
435
	else if (type & STD_SPECIFIC_TYPES)
436 437 438 439
		type_mask = STD_SPECIFIC_TYPES;

	type &= type_mask;

440
	if (!(type & SCODE))
441
		type_mask = ~0;
442

443
	/* Seek for exact match */
444
	for (i = 0; i < priv->firm_size; i++) {
445
		if ((type == (priv->firm[i].type & type_mask)) &&
446
		    (*id == priv->firm[i].id))
447 448 449 450
			goto found;
	}

	/* Seek for generic video standard match */
451
	for (i = 0; i < priv->firm_size; i++) {
452 453 454
		v4l2_std_id match_mask;
		int nr_matches;

455
		if (type != (priv->firm[i].type & type_mask))
456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478
			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;
479 480 481 482
	}

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

483
	i = -ENOENT;
484
	goto ret;
485 486 487 488

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

489
ret:
490
	tuner_dbg("%s firmware for type=", (i < 0) ? "Can't find" : "Found");
491 492
	if (debug) {
		dump_firm_type(type);
493
		printk("(%x), id %016llx.\n", type, (unsigned long long)*id);
494
	}
495 496 497
	return i;
}

498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514
static inline int do_tuner_callback(struct dvb_frontend *fe, int cmd, int arg)
{
	struct xc2028_data *priv = fe->tuner_priv;

	/* analog side (tuner-core) uses i2c_adap->algo_data.
	 * digital side is not guaranteed to have algo_data defined.
	 *
	 * digital side will always have fe->dvb defined.
	 * analog side (tuner-core) doesn't (yet) define fe->dvb.
	 */

	return (!fe->callback) ? -EINVAL :
		fe->callback(((fe->dvb) && (fe->dvb->priv)) ?
				fe->dvb->priv : priv->i2c_props.adap->algo_data,
			     DVB_FRONTEND_COMPONENT_TUNER, cmd, arg);
}

515 516 517 518 519
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;
520
	unsigned char      *p, *endp, buf[priv->ctrl.max_len];
521

522
	tuner_dbg("%s called\n", __func__);
523 524 525 526 527

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

528
	tuner_info("Loading firmware for type=");
529 530 531
	dump_firm_type(priv->firm[pos].type);
	printk("(%x), id %016llx.\n", priv->firm[pos].type,
	       (unsigned long long)*id);
532

533 534
	p = priv->firm[pos].ptr;
	endp = p + priv->firm[pos].size;
535

536
	while (p < endp) {
537 538 539
		__u16 size;

		/* Checks if there's enough bytes to read */
540
		if (p + sizeof(size) > endp) {
541
			tuner_err("Firmware chunk size is wrong\n");
542 543 544
			return -EINVAL;
		}

545
		size = le16_to_cpu(*(__u16 *) p);
546 547 548 549 550 551
		p += sizeof(size);

		if (size == 0xffff)
			return 0;

		if (!size) {
552
			/* Special callback command received */
553
			rc = do_tuner_callback(fe, XC2028_TUNER_RESET, 0);
554
			if (rc < 0) {
555
				tuner_err("Error at RESET code %d\n",
556
					   (*p) & 0x7f);
557
				return -EINVAL;
558 559 560
			}
			continue;
		}
561 562 563
		if (size >= 0xff00) {
			switch (size) {
			case 0xff00:
564
				rc = do_tuner_callback(fe, XC2028_RESET_CLK, 0);
565 566 567 568 569
				if (rc < 0) {
					tuner_err("Error at RESET code %d\n",
						  (*p) & 0x7f);
					return -EINVAL;
				}
570
				break;
571 572 573 574 575 576
			default:
				tuner_info("Invalid RESET code %d\n",
					   size & 0x7f);
				return -EINVAL;

			}
577
			continue;
578
		}
579 580 581

		/* Checks for a sleep command */
		if (size & 0x8000) {
582
			msleep(size & 0x7fff);
583
			continue;
584 585
		}

586
		if ((size + p > endp)) {
587
			tuner_err("missing bytes: need %d, have %d\n",
588
				   size, (int)(endp - p));
589 590
			return -EINVAL;
		}
591

592
		buf[0] = *p;
593
		p++;
594
		size--;
595

596
		/* Sends message chunks */
597
		while (size > 0) {
598 599
			int len = (size < priv->ctrl.max_len - 1) ?
				   size : priv->ctrl.max_len - 1;
600

601
			memcpy(buf + 1, p, len);
602

603
			rc = i2c_send(priv, buf, len + 1);
604
			if (rc < 0) {
605
				tuner_err("%d returned from send\n", rc);
606 607 608 609 610 611 612
				return -EINVAL;
			}

			p += len;
			size -= len;
		}
	}
613
	return 0;
614 615
}

616
static int load_scode(struct dvb_frontend *fe, unsigned int type,
617
			 v4l2_std_id *id, __u16 int_freq, int scode)
618 619 620 621 622
{
	struct xc2028_data *priv = fe->tuner_priv;
	int                pos, rc;
	unsigned char	   *p;

623
	tuner_dbg("%s called\n", __func__);
624

625 626 627 628 629 630 631
	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) &&
632
			    (priv->firm[pos].type & HAS_IF))
633 634 635 636 637
				break;
		}
		if (pos == priv->firm_size)
			return -ENOENT;
	}
638 639 640

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

641
	if (priv->firm[pos].type & HAS_IF) {
642 643 644 645 646 647 648 649 650 651 652
		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;
	}
653

654
	tuner_info("Loading SCODE for type=");
655 656
	dump_firm_type_and_int_freq(priv->firm[pos].type,
				    priv->firm[pos].int_freq);
657 658 659
	printk("(%x), id %016llx.\n", priv->firm[pos].type,
	       (unsigned long long)*id);

660
	if (priv->firm_version < 0x0202)
661 662 663 664 665
		rc = send_seq(priv, {0x20, 0x00, 0x00, 0x00});
	else
		rc = send_seq(priv, {0xa0, 0x00, 0x00, 0x00});
	if (rc < 0)
		return -EIO;
666

667
	rc = i2c_send(priv, p, 12);
668 669
	if (rc < 0)
		return -EIO;
670

671 672 673
	rc = send_seq(priv, {0x00, 0x8c});
	if (rc < 0)
		return -EIO;
674 675 676 677

	return 0;
}

678
static int check_firmware(struct dvb_frontend *fe, unsigned int type,
679
			  v4l2_std_id std, __u16 int_freq)
680
{
681
	struct xc2028_data         *priv = fe->tuner_priv;
682
	struct firmware_properties new_fw;
683 684 685
	int			   rc = 0, is_retry = 0;
	u16			   version, hwmodel;
	v4l2_std_id		   std0;
686

687
	tuner_dbg("%s called\n", __func__);
688

689
	if (!priv->firm) {
690 691
		if (!priv->ctrl.fname) {
			tuner_info("xc2028/3028 firmware name not set!\n");
692
			return -EINVAL;
693
		}
694

695 696
		rc = load_all_firmwares(fe);
		if (rc < 0)
697 698 699
			return rc;
	}

700
	if (priv->ctrl.mts && !(type & FM))
701
		type |= MTS;
702

703
retry:
704 705 706 707 708
	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;
709
	new_fw.int_freq = int_freq;
710 711 712 713

	tuner_dbg("checking firmware, user requested type=");
	if (debug) {
		dump_firm_type(new_fw.type);
714
		printk("(%x), id %016llx, ", new_fw.type,
715
		       (unsigned long long)new_fw.std_req);
716 717 718 719 720 721 722
		if (!int_freq) {
			printk("scode_tbl ");
			dump_firm_type(priv->ctrl.scode_table);
			printk("(%x), ", priv->ctrl.scode_table);
		} else
			printk("int_freq %d, ", new_fw.int_freq);
		printk("scode_nr %d\n", new_fw.scode_nr);
723 724 725 726 727 728 729 730 731 732 733 734 735
	}

	/* 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 */
736
	rc = do_tuner_callback(fe, XC2028_TUNER_RESET, 0);
737 738 739
	if (rc < 0)
		goto fail;

740 741 742
	/* BASE firmwares are all std0 */
	std0 = 0;
	rc = load_firmware(fe, BASE | new_fw.type, &std0);
743 744 745 746 747
	if (rc < 0) {
		tuner_err("Error %d while loading base firmware\n",
			  rc);
		goto fail;
	}
748

749
	/* Load INIT1, if needed */
750
	tuner_dbg("Load init1 firmware, if exists\n");
751

752
	rc = load_firmware(fe, BASE | INIT1 | new_fw.type, &std0);
753 754 755
	if (rc == -ENOENT)
		rc = load_firmware(fe, (BASE | INIT1 | new_fw.type) & ~F8MHZ,
				   &std0);
756 757 758 759 760
	if (rc < 0 && rc != -ENOENT) {
		tuner_err("Error %d while loading init1 firmware\n",
			  rc);
		goto fail;
	}
761

762 763 764 765
skip_base:
	/*
	 * No need to reload standard specific firmware if base firmware
	 * was not reloaded and requested video standards have not changed.
766
	 */
767 768
	if (priv->cur_fw.type == (BASE | new_fw.type) &&
	    priv->cur_fw.std_req == std) {
769
		tuner_dbg("Std-specific firmware already loaded.\n");
770
		goto skip_std_specific;
771
	}
772

773 774 775 776
	/* Reloading std-specific firmware forces a SCODE update */
	priv->cur_fw.scode_table = 0;

	rc = load_firmware(fe, new_fw.type, &new_fw.id);
777 778 779
	if (rc == -ENOENT)
		rc = load_firmware(fe, new_fw.type & ~F8MHZ, &new_fw.id);

780
	if (rc < 0)
781 782 783 784 785 786 787 788
		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;
	}
789

790 791 792
	if (new_fw.type & FM)
		goto check_device;

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

796 797
	rc = load_scode(fe, new_fw.type | new_fw.scode_table, &new_fw.id,
			new_fw.int_freq, new_fw.scode_nr);
798

799
check_device:
800 801 802 803 804
	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;
	}
805

806 807 808 809
	tuner_dbg("Device is Xceive %d version %d.%d, "
		  "firmware version %d.%d\n",
		  hwmodel, (version & 0xf000) >> 12, (version & 0xf00) >> 8,
		  (version & 0xf0) >> 4, version & 0xf);
810

811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827
	/* 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;
	}

828 829 830 831 832 833 834 835 836
	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;
837 838

	return 0;
839 840 841

fail:
	memset(&priv->cur_fw, 0, sizeof(priv->cur_fw));
842 843 844 845 846 847 848
	if (!is_retry) {
		msleep(50);
		is_retry = 1;
		tuner_dbg("Retrying firmware load\n");
		goto retry;
	}

849 850 851
	if (rc == -ENOENT)
		rc = -EINVAL;
	return rc;
852 853
}

854
static int xc2028_signal(struct dvb_frontend *fe, u16 *strength)
855
{
856
	struct xc2028_data *priv = fe->tuner_priv;
857 858
	u16                 frq_lock, signal = 0;
	int                 rc;
859

860
	tuner_dbg("%s called\n", __func__);
861

862
	mutex_lock(&priv->lock);
863

864
	/* Sync Lock Indicator */
865
	rc = xc2028_get_reg(priv, 0x0002, &frq_lock);
866
	if (rc < 0)
867
		goto ret;
868

869 870 871
	/* Frequency is locked */
	if (frq_lock == 1)
		signal = 32768;
872

873
	/* Get SNR of the video signal */
874 875
	rc = xc2028_get_reg(priv, 0x0040, &signal);
	if (rc < 0)
876 877 878 879
		goto ret;

	/* Use both frq_lock and signal to generate the result */
	signal = signal || ((signal & 0x07) << 12);
880 881

ret:
882 883 884
	mutex_unlock(&priv->lock);

	*strength = signal;
885

886 887
	tuner_dbg("signal strength is %d\n", signal);

888
	return rc;
889 890 891 892
}

#define DIV 15625

893
static int generic_set_freq(struct dvb_frontend *fe, u32 freq /* in HZ */,
894 895 896 897
			    enum tuner_mode new_mode,
			    unsigned int type,
			    v4l2_std_id std,
			    u16 int_freq)
898
{
899
	struct xc2028_data *priv = fe->tuner_priv;
900
	int		   rc = -EINVAL;
901
	unsigned char	   buf[4];
902
	u32		   div, offset = 0;
903

904
	tuner_dbg("%s called\n", __func__);
905

906 907
	mutex_lock(&priv->lock);

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

910
	if (check_firmware(fe, type, std, int_freq) < 0)
911
		goto ret;
912

913 914 915 916 917 918 919
	/* 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.
	 */
920
	if (new_mode == T_ANALOG_TV) {
921
		rc = send_seq(priv, {0x00, 0x00});
922 923 924
	} else if (priv->cur_fw.type & ATSC) {
		offset = 1750000;
	} else {
925
		offset = 2750000;
926 927 928 929 930 931 932 933 934 935
		/*
		 * We must adjust the offset by 500kHz in two cases in order
		 * to correctly center the IF output:
		 * 1) When the ZARLINK456 or DIBCOM52 tables were explicitly
		 *    selected and a 7MHz channel is tuned;
		 * 2) When tuning a VHF channel with DTV78 firmware.
		 */
		if (((priv->cur_fw.type & DTV7) &&
		     (priv->cur_fw.scode_table & (ZARLINK456 | DIBCOM52))) ||
		    ((priv->cur_fw.type & DTV78) && freq < 470000000))
936 937
			offset -= 500000;
	}
938

939
	div = (freq - offset + DIV / 2) / DIV;
940

941
	/* CMD= Set frequency */
942
	if (priv->firm_version < 0x0202)
943 944 945 946 947
		rc = send_seq(priv, {0x00, 0x02, 0x00, 0x00});
	else
		rc = send_seq(priv, {0x80, 0x02, 0x00, 0x00});
	if (rc < 0)
		goto ret;
948

949 950 951 952
	/* Return code shouldn't be checked.
	   The reset CLK is needed only with tm6000.
	   Driver should work fine even if this fails.
	 */
953
	do_tuner_callback(fe, XC2028_RESET_CLK, 1);
954 955

	msleep(10);
956

957 958 959 960
	buf[0] = 0xff & (div >> 24);
	buf[1] = 0xff & (div >> 16);
	buf[2] = 0xff & (div >> 8);
	buf[3] = 0xff & (div);
961

962
	rc = i2c_send(priv, buf, sizeof(buf));
963
	if (rc < 0)
964
		goto ret;
965 966
	msleep(100);

967
	priv->frequency = freq;
968

969 970 971
	tuner_dbg("divisor= %02x %02x %02x %02x (freq=%d.%03d)\n",
	       buf[0], buf[1], buf[2], buf[3],
	       freq / 1000000, (freq % 1000000) / 1000);
972

973
	rc = 0;
974

975 976
ret:
	mutex_unlock(&priv->lock);
977

978
	return rc;
979 980
}

981
static int xc2028_set_analog_freq(struct dvb_frontend *fe,
982
			      struct analog_parameters *p)
983
{
984
	struct xc2028_data *priv = fe->tuner_priv;
985 986
	unsigned int       type=0;

987
	tuner_dbg("%s called\n", __func__);
988

989 990 991 992 993
	if (p->mode == V4L2_TUNER_RADIO) {
		type |= FM;
		if (priv->ctrl.input1)
			type |= INPUT1;
		return generic_set_freq(fe, (625l * p->frequency) / 10,
994
				T_ANALOG_TV, type, 0, 0);
995 996
	}

997 998 999 1000 1001
	/* 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 */
1002 1003
	if (!(p->std & V4L2_STD_MN))
		type |= F8MHZ;
1004

1005 1006
	/* Add audio hack to std mask */
	p->std |= parse_audio_std_option();
1007

1008
	return generic_set_freq(fe, 62500l * p->frequency,
1009
				T_ANALOG_TV, type, p->std, 0);
1010
}
1011

1012 1013
static int xc2028_set_params(struct dvb_frontend *fe,
			     struct dvb_frontend_parameters *p)
1014
{
1015
	struct xc2028_data *priv = fe->tuner_priv;
1016
	unsigned int       type=0;
1017
	fe_bandwidth_t     bw = BANDWIDTH_8_MHZ;
1018
	u16                demod = 0;
1019

1020
	tuner_dbg("%s called\n", __func__);
1021

1022 1023 1024 1025 1026
	switch(fe->ops.info.type) {
	case FE_OFDM:
		bw = p->u.ofdm.bandwidth;
		break;
	case FE_QAM:
1027 1028 1029 1030
		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");
1031 1032 1033 1034 1035
		bw = BANDWIDTH_6_MHZ;
		type |= QAM;
		break;
	case FE_ATSC:
		bw = BANDWIDTH_6_MHZ;
1036 1037
		/* The only ATSC firmware (at least on v2.7) is D2633 */
		type |= ATSC | D2633;
1038
		break;
1039 1040 1041
	/* DVB-S is not supported */
	default:
		return -EINVAL;
1042 1043
	}

1044 1045
	switch (bw) {
	case BANDWIDTH_8_MHZ:
1046 1047 1048 1049 1050 1051
		if (p->frequency < 470000000)
			priv->ctrl.vhfbw7 = 0;
		else
			priv->ctrl.uhfbw8 = 1;
		type |= (priv->ctrl.vhfbw7 && priv->ctrl.uhfbw8) ? DTV78 : DTV8;
		type |= F8MHZ;
1052 1053
		break;
	case BANDWIDTH_7_MHZ:
1054 1055 1056 1057 1058 1059
		if (p->frequency < 470000000)
			priv->ctrl.vhfbw7 = 1;
		else
			priv->ctrl.uhfbw8 = 0;
		type |= (priv->ctrl.vhfbw7 && priv->ctrl.uhfbw8) ? DTV78 : DTV7;
		type |= F8MHZ;
1060 1061
		break;
	case BANDWIDTH_6_MHZ:
1062 1063 1064
		type |= DTV6;
		priv->ctrl.vhfbw7 = 0;
		priv->ctrl.uhfbw8 = 0;
1065 1066 1067 1068 1069
		break;
	default:
		tuner_err("error: bandwidth not supported.\n");
	};

1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091
	/*
	  Selects between D2633 or D2620 firmware.
	  It doesn't make sense for ATSC, since it should be D2633 on all cases
	 */
	if (fe->ops.info.type != FE_ATSC) {
		switch (priv->ctrl.type) {
		case XC2028_D2633:
			type |= D2633;
			break;
		case XC2028_D2620:
			type |= D2620;
			break;
		case XC2028_AUTO:
		default:
			/* Zarlink seems to need D2633 */
			if (priv->ctrl.demod == XC3028_FE_ZARLINK456)
				type |= D2633;
			else
				type |= D2620;
		}
	}

1092 1093
	/* All S-code tables need a 200kHz shift */
	if (priv->ctrl.demod)
1094
		demod = priv->ctrl.demod + 200;
1095

1096
	return generic_set_freq(fe, p->frequency,
1097
				T_DIGITAL_TV, type, 0, demod);
1098
}
1099

1100 1101 1102 1103 1104 1105 1106
static int xc2028_sleep(struct dvb_frontend *fe)
{
	struct xc2028_data *priv = fe->tuner_priv;
	int rc = 0;

	/* Avoid firmware reload on slow devices */
	if (no_poweroff)
1107
		return 0;
1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123

	tuner_dbg("Putting xc2028/3028 into poweroff mode.\n");

	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;
}
1124

1125
static int xc2028_dvb_release(struct dvb_frontend *fe)
1126
{
1127 1128
	struct xc2028_data *priv = fe->tuner_priv;

1129
	tuner_dbg("%s called\n", __func__);
1130

1131 1132
	mutex_lock(&xc2028_list_mutex);

1133 1134
	/* only perform final cleanup if this is the last instance */
	if (hybrid_tuner_report_instance_count(priv) == 1) {
1135
		kfree(priv->ctrl.fname);
1136 1137
		free_firmware(priv);
	}
1138

1139 1140 1141
	if (priv)
		hybrid_tuner_release_state(priv);

1142 1143
	mutex_unlock(&xc2028_list_mutex);

1144 1145
	fe->tuner_priv = NULL;

1146 1147 1148
	return 0;
}

1149
static int xc2028_get_frequency(struct dvb_frontend *fe, u32 *frequency)
1150
{
1151
	struct xc2028_data *priv = fe->tuner_priv;
1152

1153
	tuner_dbg("%s called\n", __func__);
1154

1155
	*frequency = priv->frequency;
1156 1157 1158 1159

	return 0;
}

1160
static int xc2028_set_config(struct dvb_frontend *fe, void *priv_cfg)
1161 1162 1163
{
	struct xc2028_data *priv = fe->tuner_priv;
	struct xc2028_ctrl *p    = priv_cfg;
1164
	int                 rc   = 0;
1165

1166
	tuner_dbg("%s called\n", __func__);
1167

1168 1169
	mutex_lock(&priv->lock);

1170
	memcpy(&priv->ctrl, p, sizeof(priv->ctrl));
1171 1172
	if (priv->ctrl.max_len < 9)
		priv->ctrl.max_len = 13;
1173

1174
	if (p->fname) {
1175 1176 1177 1178 1179
		if (priv->ctrl.fname && strcmp(p->fname, priv->ctrl.fname)) {
			kfree(priv->ctrl.fname);
			free_firmware(priv);
		}

1180 1181 1182
		priv->ctrl.fname = kstrdup(p->fname, GFP_KERNEL);
		if (priv->ctrl.fname == NULL)
			rc = -ENOMEM;
1183 1184
	}

1185 1186
	mutex_unlock(&priv->lock);

1187
	return rc;
1188 1189
}

1190
static const struct dvb_tuner_ops xc2028_dvb_tuner_ops = {
1191
	.info = {
1192 1193 1194 1195 1196
		 .name = "Xceive XC3028",
		 .frequency_min = 42000000,
		 .frequency_max = 864000000,
		 .frequency_step = 50000,
		 },
1197

1198
	.set_config	   = xc2028_set_config,
1199
	.set_analog_params = xc2028_set_analog_freq,
1200 1201 1202 1203
	.release           = xc2028_dvb_release,
	.get_frequency     = xc2028_get_frequency,
	.get_rf_strength   = xc2028_signal,
	.set_params        = xc2028_set_params,
1204
	.sleep             = xc2028_sleep,
1205 1206
};

1207 1208
struct dvb_frontend *xc2028_attach(struct dvb_frontend *fe,
				   struct xc2028_config *cfg)
1209
{
1210
	struct xc2028_data *priv;
1211
	int instance;
1212

1213
	if (debug)
1214
		printk(KERN_DEBUG "xc2028: Xcv2028/3028 init called!\n");
1215

1216
	if (NULL == cfg)
1217
		return NULL;
1218

1219
	if (!fe) {
1220
		printk(KERN_ERR "xc2028: No frontend!\n");
1221
		return NULL;
1222 1223
	}

1224 1225
	mutex_lock(&xc2028_list_mutex);

1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236
	instance = hybrid_tuner_request_state(struct xc2028_data, priv,
					      hybrid_tuner_instance_list,
					      cfg->i2c_adap, cfg->i2c_addr,
					      "xc2028");
	switch (instance) {
	case 0:
		/* memory allocation failure */
		goto fail;
		break;
	case 1:
		/* new tuner instance */
1237
		priv->ctrl.max_len = 13;
1238

1239 1240
		mutex_init(&priv->lock);

1241 1242 1243 1244 1245 1246 1247
		fe->tuner_priv = priv;
		break;
	case 2:
		/* existing tuner instance */
		fe->tuner_priv = priv;
		break;
	}
1248

1249
	memcpy(&fe->ops.tuner_ops, &xc2028_dvb_tuner_ops,
1250
	       sizeof(xc2028_dvb_tuner_ops));
1251 1252 1253

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

1254 1255 1256
	if (cfg->ctrl)
		xc2028_set_config(fe, cfg->ctrl);

1257 1258
	mutex_unlock(&xc2028_list_mutex);

1259
	return fe;
1260 1261 1262 1263 1264
fail:
	mutex_unlock(&xc2028_list_mutex);

	xc2028_dvb_release(fe);
	return NULL;
1265
}
1266

1267 1268
EXPORT_SYMBOL(xc2028_attach);

1269
MODULE_DESCRIPTION("Xceive xc2028/xc3028 tuner driver");
1270
MODULE_AUTHOR("Michel Ludwig <michel.ludwig@gmail.com>");
1271 1272
MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@infradead.org>");
MODULE_LICENSE("GPL");