tuner-xc2028.c 22.4 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 53 54 55
/* struct for storing firmware table */
struct firmware_description {
	unsigned int  type;
	v4l2_std_id   id;
	unsigned char *ptr;
	unsigned int  size;
};
56 57

struct xc2028_data {
58 59 60 61 62 63
	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;
64 65 66 67
	__u32			frequency;

	struct firmware_description *firm;
	int			firm_size;
68
	__u16			firm_version;
69 70

	struct xc2028_ctrl	ctrl;
71

72 73 74 75 76 77 78 79
	v4l2_std_id		firm_type;	   /* video stds supported
							by current firmware */
	fe_bandwidth_t		bandwidth;	   /* Firmware bandwidth:
							      6M, 7M or 8M */
	int			need_load_generic; /* The generic firmware
							      were loaded? */
	enum tuner_mode	mode;
	struct i2c_client	*i2c_client;
80 81

	struct mutex lock;
82 83
};

84 85 86 87 88 89 90 91 92 93 94 95 96
#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)						\
97
		tuner_err("i2c input error: rc = %d (should be %d)\n",	\
98 99 100
			   _rc, (int)size); 				\
	_rc;								\
})
101

102 103 104 105 106 107 108 109 110 111
#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;								\
})

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

124
static unsigned int xc2028_get_reg(struct xc2028_data *priv, u16 reg, u16 *val)
125
{
126
	unsigned char buf[2];
127
	unsigned char ibuf[2];
128

129
	tuner_dbg("%s %04x called\n", __FUNCTION__, reg);
130

131
	buf[0] = reg >> 8;
132
	buf[1] = (unsigned char) reg;
133

134 135
	if (i2c_send_recv(priv, buf, 2, ibuf, 2) != 2)
		return -EIO;
136

137 138
	*val = (ibuf[1]) | (ibuf[0] << 8);
	return 0;
139 140
}

141 142 143 144
void dump_firm_type(unsigned int type)
{
	 if (type & BASE)
		printk("BASE ");
145 146
	 if (type & INIT1)
		printk("INIT1 ");
147 148 149 150 151 152 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
	 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 ");
}

205
static  v4l2_std_id parse_audio_std_option(void)
206
{
207
	if (strcasecmp(audio_std, "A2") == 0)
208
		return V4L2_STD_A2;
209
	if (strcasecmp(audio_std, "A2/A") == 0)
210
		return V4L2_STD_A2_A;
211
	if (strcasecmp(audio_std, "A2/B") == 0)
212
		return V4L2_STD_A2_B;
213
	if (strcasecmp(audio_std, "NICAM") == 0)
214
		return V4L2_STD_NICAM;
215
	if (strcasecmp(audio_std, "NICAM/A") == 0)
216
		return V4L2_STD_NICAM_A;
217
	if (strcasecmp(audio_std, "NICAM/B") == 0)
218 219 220 221 222
		return V4L2_STD_NICAM_B;

	return 0;
}

223
static void free_firmware(struct xc2028_data *priv)
224
{
225 226 227 228 229
	int i;

	if (!priv->firm)
		return;

230 231 232
	for (i = 0; i < priv->firm_size; i++)
		kfree(priv->firm[i].ptr);

233 234
	kfree(priv->firm);

235
	priv->firm = NULL;
236
	priv->firm_size = 0;
237 238 239
	priv->need_load_generic = 1;
}

240
static int load_all_firmwares(struct dvb_frontend *fe)
241 242
{
	struct xc2028_data    *priv = fe->tuner_priv;
243
	const struct firmware *fw   = NULL;
244
	unsigned char         *p, *endp;
245 246
	int                   rc = 0;
	int		      n, n_array;
247
	char		      name[33];
248

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

251
	tuner_dbg("Reading firmware %s\n", priv->ctrl.fname);
252 253
	rc = request_firmware(&fw, priv->ctrl.fname,
			      &priv->i2c_props.adap->dev);
254
	if (rc < 0) {
255
		if (rc == -ENOENT)
256
			tuner_err("Error: firmware %s not found.\n",
257
				   priv->ctrl.fname);
258
		else
259
			tuner_err("Error %d while requesting firmware %s \n",
260
				   rc, priv->ctrl.fname);
261

262 263
		return rc;
	}
264 265
	p = fw->data;
	endp = p + fw->size;
266

267 268 269 270
	if (fw->size < sizeof(name) - 1 + 2 + 2) {
		tuner_err("Error: firmware file %s has invalid size!\n",
			  priv->ctrl.fname);
		goto corrupt;
271
	}
272

273 274 275
	memcpy(name, p, sizeof(name) - 1);
	name[sizeof(name) - 1] = 0;
	p += sizeof(name) - 1;
276

277
	priv->firm_version = le16_to_cpu(*(__u16 *) p);
278 279
	p += 2;

280
	n_array = le16_to_cpu(*(__u16 *) p);
281 282
	p += 2;

283 284 285
	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);
286

287
	priv->firm = kzalloc(sizeof(*priv->firm) * n_array, GFP_KERNEL);
288 289
	if (priv->firm == NULL) {
		tuner_err("Not enough memory to load firmware file.\n");
290
		rc = -ENOMEM;
291
		goto err;
292
	}
293
	priv->firm_size = n_array;
294

295 296
	n = -1;
	while (p < endp) {
297 298 299 300 301
		__u32 type, size;
		v4l2_std_id id;

		n++;
		if (n >= n_array) {
302 303
			tuner_err("More firmware images in file than "
				  "were expected!\n");
304 305 306 307
			goto corrupt;
		}

		/* Checks if there's enough bytes to read */
308
		if (p + sizeof(type) + sizeof(id) + sizeof(size) > endp) {
309
			tuner_err("Firmware header is incomplete!\n");
310 311 312
			goto corrupt;
		}

313
		type = le32_to_cpu(*(__u32 *) p);
314 315
		p += sizeof(type);

316
		id = le64_to_cpu(*(v4l2_std_id *) p);
317 318
		p += sizeof(id);

319
		size = le32_to_cpu(*(__u32 *) p);
320 321
		p += sizeof(size);

322
		if ((!size) || (size + p > endp)) {
323
			tuner_err("Firmware type ");
324
			dump_firm_type(type);
325 326
			printk("(%x), id %llx is corrupted "
			       "(size=%d, expected %d)\n",
327
			       type, (unsigned long long)id,
328
			       (unsigned)(endp - p), size);
329 330 331
			goto corrupt;
		}

332
		priv->firm[n].ptr = kzalloc(size, GFP_KERNEL);
333 334
		if (priv->firm[n].ptr == NULL) {
			tuner_err("Not enough memory to load firmware file.\n");
335
			rc = -ENOMEM;
336 337
			goto err;
		}
338 339 340 341 342 343
		tuner_dbg("Reading firmware type ");
		if (debug) {
			dump_firm_type(type);
			printk("(%x), id %llx, size=%d.\n",
				   type, (unsigned long long)id, size);
		}
344 345 346 347 348 349 350 351 352

		memcpy(priv->firm[n].ptr, p, size);
		priv->firm[n].type = type;
		priv->firm[n].id   = id;
		priv->firm[n].size = size;

		p += size;
	}

353
	if (n + 1 != priv->firm_size) {
354
		tuner_err("Firmware file is incomplete!\n");
355 356 357 358 359 360
		goto corrupt;
	}

	goto done;

corrupt:
361
	rc = -EINVAL;
362
	tuner_err("Error: firmware file is corrupted!\n");
363 364

err:
365
	tuner_info("Releasing partially loaded firmware file.\n");
366 367 368 369
	free_firmware(priv);

done:
	release_firmware(fw);
370 371
	if (rc == 0)
		tuner_dbg("Firmware files loaded.\n");
372 373 374 375

	return rc;
}

376 377
static int seek_firmware(struct dvb_frontend *fe, unsigned int type,
			 v4l2_std_id *id)
378 379
{
	struct xc2028_data *priv = fe->tuner_priv;
380
	int                 i, best_i = -1, best_nr_matches = 0;
381

382 383 384 385 386
	tuner_dbg("%s called, want type=", __FUNCTION__);
	if (debug) {
		dump_firm_type(type);
		printk("(%x), id %016llx.\n", type, (unsigned long long)*id);
	}
387 388

	if (!priv->firm) {
389
		tuner_err("Error! firmware not loaded\n");
390 391 392
		return -EINVAL;
	}

393
	if (((type & ~SCODE) == 0) && (*id == 0))
394
		*id = V4L2_STD_PAL;
395 396

	/* Seek for exact match */
397 398
	for (i = 0; i < priv->firm_size; i++) {
		if ((type == priv->firm[i].type) && (*id == priv->firm[i].id))
399 400 401 402
			goto found;
	}

	/* Seek for generic video standard match */
403
	for (i = 0; i < priv->firm_size; i++) {
404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430
		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;
431 432 433 434
	}

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

435
	i = -ENOENT;
436
	goto ret;
437 438 439 440

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

441
ret:
442
	tuner_dbg("%s firmware for type=", (i < 0) ? "Can't find" : "Found");
443 444
	if (debug) {
		dump_firm_type(type);
445
		printk("(%x), id %016llx.\n", type, (unsigned long long)*id);
446
	}
447 448 449 450 451 452 453 454
	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;
455
	unsigned char      *p, *endp, buf[priv->ctrl.max_len];
456

457
	tuner_dbg("%s called\n", __FUNCTION__);
458 459 460 461 462

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

463
	tuner_info("Loading firmware for type=");
464 465 466
	dump_firm_type(priv->firm[pos].type);
	printk("(%x), id %016llx.\n", priv->firm[pos].type,
	       (unsigned long long)*id);
467

468 469
	p = priv->firm[pos].ptr;
	endp = p + priv->firm[pos].size;
470

471
	while (p < endp) {
472 473 474
		__u16 size;

		/* Checks if there's enough bytes to read */
475
		if (p + sizeof(size) > endp) {
476
			tuner_err("Firmware chunk size is wrong\n");
477 478 479
			return -EINVAL;
		}

480
		size = le16_to_cpu(*(__u16 *) p);
481 482 483 484 485 486
		p += sizeof(size);

		if (size == 0xffff)
			return 0;

		if (!size) {
487
			/* Special callback command received */
488
			rc = priv->tuner_callback(priv->video_dev,
489 490
						  XC2028_TUNER_RESET, 0);
			if (rc < 0) {
491
				tuner_err("Error at RESET code %d\n",
492
					   (*p) & 0x7f);
493
				return -EINVAL;
494 495 496
			}
			continue;
		}
497 498 499 500 501 502 503 504 505 506
		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;
				}
507
				break;
508 509 510 511 512 513
			default:
				tuner_info("Invalid RESET code %d\n",
					   size & 0x7f);
				return -EINVAL;

			}
514
			continue;
515
		}
516 517 518

		/* Checks for a sleep command */
		if (size & 0x8000) {
519
			msleep(size & 0x7fff);
520
			continue;
521 522
		}

523
		if ((size + p > endp)) {
524
			tuner_err("missing bytes: need %d, have %d\n",
525
				   size, (int)(endp - p));
526 527
			return -EINVAL;
		}
528

529
		buf[0] = *p;
530
		p++;
531
		size--;
532

533
		/* Sends message chunks */
534
		while (size > 0) {
535 536
			int len = (size < priv->ctrl.max_len - 1) ?
				   size : priv->ctrl.max_len - 1;
537

538
			memcpy(buf + 1, p, len);
539

540
			rc = i2c_send(priv, buf, len + 1);
541
			if (rc < 0) {
542
				tuner_err("%d returned from send\n", rc);
543 544 545 546 547 548 549
				return -EINVAL;
			}

			p += len;
			size -= len;
		}
	}
550
	return 0;
551 552
}

553 554 555 556 557 558 559
static int load_scode(struct dvb_frontend *fe, unsigned int type,
			 v4l2_std_id *id, int scode)
{
	struct xc2028_data *priv = fe->tuner_priv;
	int                pos, rc;
	unsigned char	   *p;

560
	tuner_dbg("%s called\n", __FUNCTION__);
561 562 563 564 565 566 567

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

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

568 569 570 571
	/* 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)
572 573
		return -EINVAL;

574 575 576 577 578
	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);

579
	if (priv->firm_version < 0x0202)
580 581 582 583 584
		rc = send_seq(priv, {0x20, 0x00, 0x00, 0x00});
	else
		rc = send_seq(priv, {0xa0, 0x00, 0x00, 0x00});
	if (rc < 0)
		return -EIO;
585

586
	rc = i2c_send(priv, p + 14 * scode + 2, 12);
587 588
	if (rc < 0)
		return -EIO;
589

590 591 592
	rc = send_seq(priv, {0x00, 0x8c});
	if (rc < 0)
		return -EIO;
593 594 595 596

	return 0;
}

597
static int check_firmware(struct dvb_frontend *fe, enum tuner_mode new_mode,
598
			  v4l2_std_id std, fe_bandwidth_t bandwidth)
599
{
600
	struct xc2028_data      *priv = fe->tuner_priv;
601 602
	int			rc;
	u16			version, hwmodel;
603 604
	v4l2_std_id		std0 = 0;
	unsigned int		type0 = 0, type = 0;
605
	int			change_digital_bandwidth;
606

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

609
	if (!priv->firm) {
610 611
		if (!priv->ctrl.fname) {
			tuner_info("xc2028/3028 firmware name not set!\n");
612
			return -EINVAL;
613
		}
614

615 616
		rc = load_all_firmwares(fe);
		if (rc < 0)
617 618 619
			return rc;
	}

620
	tuner_dbg("I am in mode %u and I should switch to mode %i\n",
621
		   priv->mode, new_mode);
622 623

	/* first of all, determine whether we have switched the mode */
624
	if (new_mode != priv->mode) {
625 626
		priv->mode = new_mode;
		priv->need_load_generic = 1;
627 628
	}

629
	change_digital_bandwidth = (priv->mode == T_DIGITAL_TV
630
				    && bandwidth != priv->bandwidth) ? 1 : 0;
631
	tuner_dbg("old bandwidth %u, new bandwidth %u\n", priv->bandwidth,
632
		   bandwidth);
633

634
	if (priv->need_load_generic) {
635
		/* Reset is needed before loading firmware */
636 637
		rc = priv->tuner_callback(priv->video_dev,
					  XC2028_TUNER_RESET, 0);
638
		if (rc < 0)
639 640
			return rc;

641
		type0 = BASE;
642 643 644 645

		if (priv->ctrl.type == XC2028_FIRM_MTS)
			type0 |= MTS;

646 647
		if (bandwidth == BANDWIDTH_7_MHZ ||
		    bandwidth == BANDWIDTH_8_MHZ)
648 649 650 651 652
			type0 |= F8MHZ;

		/* FIXME: How to load FM and FM|INPUT1 firmwares? */

		rc = load_firmware(fe, type0, &std0);
653
		if (rc < 0) {
654 655
			tuner_err("Error %d while loading generic firmware\n",
				  rc);
656
			return rc;
657
		}
658

659 660 661 662
		priv->need_load_generic = 0;
		priv->firm_type = 0;
		if (priv->mode == T_DIGITAL_TV)
			change_digital_bandwidth = 1;
663 664
	}

665
	tuner_dbg("I should change bandwidth %u\n", change_digital_bandwidth);
666 667

	if (change_digital_bandwidth) {
668 669 670 671 672 673

		/*FIXME: Should allow selecting between D2620 and D2633 */
		type |= D2620;

		/* FIXME: When should select a DTV78 firmware?
		 */
674
		switch (bandwidth) {
675 676
		case BANDWIDTH_8_MHZ:
			type |= DTV8;
677
			break;
678 679
		case BANDWIDTH_7_MHZ:
			type |= DTV7;
680
			break;
681 682
		case BANDWIDTH_6_MHZ:
			/* FIXME: Should allow select also ATSC */
683
			type |= DTV6 | QAM;
684 685
			break;

686
		default:
687
			tuner_err("error: bandwidth not supported.\n");
688
		};
689
		priv->bandwidth = bandwidth;
690 691
	}

692 693 694
	if (!change_digital_bandwidth && priv->mode == T_DIGITAL_TV)
		return 0;

695
	/* Load INIT1, if needed */
696
	tuner_dbg("Load init1 firmware, if exists\n");
697
	type0 = BASE | INIT1;
698 699 700 701 702 703 704 705 706 707 708 709
	if (priv->ctrl.type == XC2028_FIRM_MTS)
		type0 |= MTS;

	/* FIXME: Should handle errors - if INIT1 found */
	rc = load_firmware(fe, type0, &std0);

	/* FIXME: Should add support for FM radio
	 */

	if (priv->ctrl.type == XC2028_FIRM_MTS)
		type |= MTS;

710
	if (priv->firm_type & std) {
711
		tuner_dbg("Std-specific firmware already loaded.\n");
712
		return 0;
713
	}
714

715 716 717
	/* Add audio hack to std mask */
	std |= parse_audio_std_option();

718
	rc = load_firmware(fe, type, &std);
719
	if (rc < 0)
720 721
		return rc;

722
	/* Load SCODE firmware, if exists */
723
	tuner_dbg("Trying to load scode 0\n");
724 725 726
	type |= SCODE;

	rc = load_scode(fe, type, &std, 0);
727

728 729
	xc2028_get_reg(priv, 0x0004, &version);
	xc2028_get_reg(priv, 0x0008, &hwmodel);
730 731 732 733 734

	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);
735

736
	priv->firm_type = std;
737 738 739 740

	return 0;
}

741
static int xc2028_signal(struct dvb_frontend *fe, u16 *strength)
742
{
743
	struct xc2028_data *priv = fe->tuner_priv;
744 745
	u16                 frq_lock, signal = 0;
	int                 rc;
746

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

749
	mutex_lock(&priv->lock);
750

751
	/* Sync Lock Indicator */
752 753
	rc = xc2028_get_reg(priv, 0x0002, &frq_lock);
	if (rc < 0 || frq_lock == 0)
754
		goto ret;
755 756 757

	/* Frequency is locked. Return signal quality */

758
	/* Get SNR of the video signal */
759 760 761
	rc = xc2028_get_reg(priv, 0x0040, &signal);
	if (rc < 0)
		signal = -frq_lock;
762 763

ret:
764 765 766
	mutex_unlock(&priv->lock);

	*strength = signal;
767

768
	return rc;
769 770 771 772
}

#define DIV 15625

773 774 775
static int generic_set_tv_freq(struct dvb_frontend *fe, u32 freq /* in Hz */ ,
			       enum tuner_mode new_mode,
			       v4l2_std_id std, fe_bandwidth_t bandwidth)
776
{
777
	struct xc2028_data *priv = fe->tuner_priv;
778
	int		   rc = -EINVAL;
779
	unsigned char	   buf[4];
780
	u32		   div, offset = 0;
781

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

784 785
	mutex_lock(&priv->lock);

786 787
	/* HACK: It seems that specific firmware need to be reloaded
	   when freq is changed */
788

789
	priv->firm_type = 0;
790

791
	/* Reset GPIO 1 */
792
	rc = priv->tuner_callback(priv->video_dev, XC2028_TUNER_RESET, 0);
793
	if (rc < 0)
794 795
		goto ret;

796
	msleep(10);
797
	tuner_dbg("should set frequency %d kHz\n", freq / 1000);
798

799
	if (check_firmware(fe, new_mode, std, bandwidth) < 0)
800
		goto ret;
801

802
	if (new_mode == T_DIGITAL_TV) {
803
		offset = 2750000;
804 805 806
		if (priv->bandwidth == BANDWIDTH_7_MHZ)
			offset -= 500000;
	}
807

808
	div = (freq - offset + DIV / 2) / DIV;
809

810
	/* CMD= Set frequency */
811
	if (priv->firm_version < 0x0202)
812 813 814 815 816
		rc = send_seq(priv, {0x00, 0x02, 0x00, 0x00});
	else
		rc = send_seq(priv, {0x80, 0x02, 0x00, 0x00});
	if (rc < 0)
		goto ret;
817

818
	rc = priv->tuner_callback(priv->video_dev, XC2028_RESET_CLK, 1);
819
	if (rc < 0)
820
		goto ret;
821 822

	msleep(10);
823

824 825 826 827
	buf[0] = 0xff & (div >> 24);
	buf[1] = 0xff & (div >> 16);
	buf[2] = 0xff & (div >> 8);
	buf[3] = 0xff & (div);
828

829
	rc = i2c_send(priv, buf, sizeof(buf));
830
	if (rc < 0)
831
		goto ret;
832 833
	msleep(100);

834
	priv->frequency = freq;
835

836 837 838
	tuner_dbg("divisor= %02x %02x %02x %02x (freq=%d.%03d)\n",
	       buf[0], buf[1], buf[2], buf[3],
	       freq / 1000000, (freq % 1000000) / 1000);
839

840
	rc = 0;
841

842 843
ret:
	mutex_unlock(&priv->lock);
844

845
	return rc;
846 847
}

848
static int xc2028_set_tv_freq(struct dvb_frontend *fe,
849
			      struct analog_parameters *p)
850
{
851
	struct xc2028_data *priv = fe->tuner_priv;
852

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

855
	return generic_set_tv_freq(fe, 62500l * p->frequency, T_ANALOG_TV,
856 857
				   p->std, BANDWIDTH_8_MHZ);
				   /* XXX Are some analog standards 6MHz? */
858
}
859

860 861
static int xc2028_set_params(struct dvb_frontend *fe,
			     struct dvb_frontend_parameters *p)
862
{
863
	struct xc2028_data *priv = fe->tuner_priv;
864

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

867 868
	/* FIXME: Only OFDM implemented */
	if (fe->ops.info.type != FE_OFDM) {
869
		tuner_err("DTV type not implemented.\n");
870
		return -EINVAL;
871 872
	}

873
	return generic_set_tv_freq(fe, p->frequency, T_DIGITAL_TV,
874 875
				   0 /* NOT USED */,
				   p->u.ofdm.bandwidth);
876 877

}
878

879
static int xc2028_dvb_release(struct dvb_frontend *fe)
880
{
881 882
	struct xc2028_data *priv = fe->tuner_priv;

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

885 886
	mutex_lock(&xc2028_list_mutex);

887
	priv->count--;
888

889
	if (!priv->count) {
890 891
		list_del(&priv->xc2028_list);

892
		kfree(priv->ctrl.fname);
893 894

		free_firmware(priv);
895
		kfree(priv);
896
		fe->tuner_priv = NULL;
897
	}
898

899 900
	mutex_unlock(&xc2028_list_mutex);

901 902 903
	return 0;
}

904
static int xc2028_get_frequency(struct dvb_frontend *fe, u32 *frequency)
905
{
906
	struct xc2028_data *priv = fe->tuner_priv;
907

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

910
	*frequency = priv->frequency;
911 912 913 914

	return 0;
}

915
static int xc2028_set_config(struct dvb_frontend *fe, void *priv_cfg)
916 917 918
{
	struct xc2028_data *priv = fe->tuner_priv;
	struct xc2028_ctrl *p    = priv_cfg;
919
	int                 rc   = 0;
920

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

923 924
	mutex_lock(&priv->lock);

925 926
	kfree(priv->ctrl.fname);
	free_firmware(priv);
927

928 929
	memcpy(&priv->ctrl, p, sizeof(priv->ctrl));
	priv->ctrl.fname = NULL;
930

931 932 933 934
	if (p->fname) {
		priv->ctrl.fname = kstrdup(p->fname, GFP_KERNEL);
		if (priv->ctrl.fname == NULL)
			rc = -ENOMEM;
935 936
	}

937 938
	if (priv->ctrl.max_len < 9)
		priv->ctrl.max_len = 13;
939

940 941
	mutex_unlock(&priv->lock);

942
	return rc;
943 944
}

945
static const struct dvb_tuner_ops xc2028_dvb_tuner_ops = {
946
	.info = {
947 948 949 950 951
		 .name = "Xceive XC3028",
		 .frequency_min = 42000000,
		 .frequency_max = 864000000,
		 .frequency_step = 50000,
		 },
952

953
	.set_config	   = xc2028_set_config,
954 955 956 957 958
	.set_analog_params = xc2028_set_tv_freq,
	.release           = xc2028_dvb_release,
	.get_frequency     = xc2028_get_frequency,
	.get_rf_strength   = xc2028_signal,
	.set_params        = xc2028_set_params,
959 960 961

};

962
void *xc2028_attach(struct dvb_frontend *fe, struct xc2028_config *cfg)
963
{
964
	struct xc2028_data *priv;
965
	void               *video_dev;
966

967
	if (debug)
968
		printk(KERN_DEBUG PREFIX ": Xcv2028/3028 init called!\n");
969

970 971
	if (NULL == cfg->video_dev)
		return NULL;
972

973
	if (!fe) {
974
		printk(KERN_ERR PREFIX ": No frontend!\n");
975
		return NULL;
976 977
	}

978 979
	video_dev = cfg->video_dev;

980 981
	mutex_lock(&xc2028_list_mutex);

982
	list_for_each_entry(priv, &xc2028_list, xc2028_list) {
983 984 985 986
		if (priv->video_dev == cfg->video_dev) {
			video_dev = NULL;
			break;
		}
987 988
	}

989
	if (video_dev) {
990
		priv = kzalloc(sizeof(*priv), GFP_KERNEL);
991 992
		if (priv == NULL) {
			mutex_unlock(&xc2028_list_mutex);
993
			return NULL;
994
		}
995

996 997
		priv->bandwidth = BANDWIDTH_6_MHZ;
		priv->need_load_generic = 1;
998
		priv->mode = T_UNINITIALIZED;
999 1000
		priv->i2c_props.addr = cfg->i2c_addr;
		priv->i2c_props.adap = cfg->i2c_adap;
1001
		priv->video_dev = video_dev;
1002
		priv->tuner_callback = cfg->callback;
1003
		priv->ctrl.max_len = 13;
1004

1005 1006
		mutex_init(&priv->lock);

1007
		list_add_tail(&priv->xc2028_list, &xc2028_list);
1008
	}
1009 1010

	fe->tuner_priv = priv;
1011
	priv->count++;
1012 1013

	memcpy(&fe->ops.tuner_ops, &xc2028_dvb_tuner_ops,
1014
	       sizeof(xc2028_dvb_tuner_ops));
1015 1016 1017

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

1018 1019
	mutex_unlock(&xc2028_list_mutex);

1020
	return fe;
1021
}
1022

1023 1024
EXPORT_SYMBOL(xc2028_attach);

1025
MODULE_DESCRIPTION("Xceive xc2028/xc3028 tuner driver");
1026
MODULE_AUTHOR("Michel Ludwig <michel.ludwig@gmail.com>");
1027 1028
MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@infradead.org>");
MODULE_LICENSE("GPL");