tuner-xc2028.c 19.9 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
#define PREFIX "xc2028"
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 37 38 39 40 41 42 43 44
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");

45
static LIST_HEAD(xc2028_list);
46 47 48 49 50 51 52
/* struct for storing firmware table */
struct firmware_description {
	unsigned int  type;
	v4l2_std_id   id;
	unsigned char *ptr;
	unsigned int  size;
};
53 54

struct xc2028_data {
55 56 57 58 59 60 61
	struct list_head        xc2028_list;
	struct tuner_i2c_props  i2c_props;
	int                     (*tuner_callback) (void *dev,
						   int command, int arg);
	struct device           *dev;
	void			*video_dev;
	int			count;
62 63 64 65 66 67 68 69
	__u32			frequency;

	struct firmware_description *firm;
	int			firm_size;

	__u16			version;

	struct xc2028_ctrl	ctrl;
70

71 72 73 74 75 76
	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? */
77 78 79

	int			max_len;	/* Max firmware chunk */

80 81
	enum tuner_mode	mode;
	struct i2c_client	*i2c_client;
82 83

	struct mutex lock;
84 85
};

86 87 88
#define i2c_send(rc, priv, buf, size) do {				\
	rc = tuner_i2c_xfer_send(&priv->i2c_props, buf, size);		\
	if (size != rc)							\
89
		tuner_err("i2c output error: rc = %d (should be %d)\n",	\
90 91 92 93 94 95
			   rc, (int)size);				\
} while (0)

#define i2c_rcv(rc, priv, buf, size) do {				\
	rc = tuner_i2c_xfer_recv(&priv->i2c_props, buf, size);		\
	if (size != rc)							\
96
		tuner_err("i2c input error: rc = %d (should be %d)\n",	\
97 98 99 100 101
			   rc, (int)size); 				\
} while (0)

#define send_seq(priv, data...)	do {					\
	int rc;								\
102
	static u8 _val[] = data;					\
103
	if (sizeof(_val) !=						\
104
			(rc = tuner_i2c_xfer_send(&priv->i2c_props,	\
105
						_val, sizeof(_val)))) {	\
106
		tuner_err("Error on line %d: %d\n", __LINE__, rc);	\
107
		return -EINVAL;						\
108
	}								\
109 110
	msleep(10);							\
} while (0)
111

112
static unsigned int xc2028_get_reg(struct xc2028_data *priv, u16 reg)
113 114
{
	int rc;
115
	unsigned char buf[2];
116

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

119 120
	buf[0] = reg>>8;
	buf[1] = (unsigned char) reg;
121

122
	i2c_send(rc, priv, buf, 2);
123
	if (rc < 0)
124 125
		return rc;

126
	i2c_rcv(rc, priv, buf, 2);
127
	if (rc < 0)
128 129
		return rc;

130
	return (buf[1]) | (buf[0] << 8);
131 132
}

133 134 135 136
void dump_firm_type(unsigned int type)
{
	 if (type & BASE)
		printk("BASE ");
137 138
	 if (type & INIT1)
		printk("INIT1 ");
139 140 141 142 143 144 145 146 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
	 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 ");
}

197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214
static v4l2_std_id parse_audio_std_option(void)
{
	if (strcasecmp(audio_std, "A2"))
		return V4L2_STD_A2;
	if (strcasecmp(audio_std, "A2/A"))
		return V4L2_STD_A2_A;
	if (strcasecmp(audio_std, "A2/B"))
		return V4L2_STD_A2_B;
	if (strcasecmp(audio_std, "NICAM"))
		return V4L2_STD_NICAM;
	if (strcasecmp(audio_std, "NICAM/A"))
		return V4L2_STD_NICAM_A;
	if (strcasecmp(audio_std, "NICAM/B"))
		return V4L2_STD_NICAM_B;

	return 0;
}

215
static void free_firmware(struct xc2028_data *priv)
216
{
217 218 219 220 221
	int i;

	if (!priv->firm)
		return;

222 223 224
	for (i = 0; i < priv->firm_size; i++)
		kfree(priv->firm[i].ptr);

225 226
	kfree(priv->firm);

227
	priv->firm = NULL;
228 229 230
	priv->need_load_generic = 1;
}

231
static int load_all_firmwares(struct dvb_frontend *fe)
232 233
{
	struct xc2028_data    *priv = fe->tuner_priv;
234
	const struct firmware *fw   = NULL;
235
	unsigned char         *p, *endp;
236 237
	int                   rc = 0;
	int		      n, n_array;
238
	char		      name[33];
239

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

242
	tuner_info("Reading firmware %s\n", priv->ctrl.fname);
243
	rc = request_firmware(&fw, priv->ctrl.fname, priv->dev);
244
	if (rc < 0) {
245
		if (rc == -ENOENT)
246
			tuner_err("Error: firmware %s not found.\n",
247
				   priv->ctrl.fname);
248
		else
249
			tuner_err("Error %d while requesting firmware %s \n",
250
				   rc, priv->ctrl.fname);
251

252 253
		return rc;
	}
254 255
	p = fw->data;
	endp = p + fw->size;
256

257
	if (fw->size < sizeof(name) - 1 + 2) {
258
		tuner_err("Error: firmware size is zero!\n");
259
		rc = -EINVAL;
260
		goto done;
261
	}
262

263 264 265
	memcpy(name, p, sizeof(name) - 1);
	name[sizeof(name) - 1] = 0;
	p += sizeof(name) - 1;
266

267
	priv->version = le16_to_cpu(*(__u16 *) p);
268 269
	p += 2;

270
	tuner_info("Firmware: %s, ver %d.%d\n", name,
271
		   priv->version >> 8, priv->version & 0xff);
272

273
	if (p + 2 > endp)
274 275
		goto corrupt;

276
	n_array = le16_to_cpu(*(__u16 *) p);
277 278
	p += 2;

279 280
	tuner_info("There are %d firmwares at %s\n",
		   n_array, priv->ctrl.fname);
281

282
	priv->firm = kzalloc(sizeof(*priv->firm) * n_array, GFP_KERNEL);
283 284

	if (!fw) {
285
		tuner_err("Not enough memory for reading firmware.\n");
286
		rc = -ENOMEM;
287
		goto done;
288 289
	}

290
	priv->firm_size = n_array;
291 292
	n = -1;
	while (p < endp) {
293 294 295 296 297
		__u32 type, size;
		v4l2_std_id id;

		n++;
		if (n >= n_array) {
298
			tuner_err("Too much firmwares at the file\n");
299 300 301 302
			goto corrupt;
		}

		/* Checks if there's enough bytes to read */
303
		if (p + sizeof(type) + sizeof(id) + sizeof(size) > endp) {
304
			tuner_err("Firmware header is incomplete!\n");
305 306 307
			goto corrupt;
		}

308
		type = le32_to_cpu(*(__u32 *) p);
309 310
		p += sizeof(type);

311
		id = le64_to_cpu(*(v4l2_std_id *) p);
312 313
		p += sizeof(id);

314
		size = le32_to_cpu(*(__u32 *) p);
315 316
		p += sizeof(size);

317
		if ((!size) || (size + p > endp)) {
318
			tuner_err("Firmware type ");
319
			dump_firm_type(type);
320 321 322
			printk("(%x), id %lx is corrupted "
			       "(size=%ld, expected %d)\n",
			       type, (unsigned long)id, endp - p, size);
323 324 325
			goto corrupt;
		}

326
		priv->firm[n].ptr = kzalloc(size, GFP_KERNEL);
327
		if (!priv->firm[n].ptr) {
328
			tuner_err("Not enough memory.\n");
329
			rc = -ENOMEM;
330 331
			goto err;
		}
332
		tuner_info("Reading firmware type ");
333 334
		dump_firm_type(type);
		printk("(%x), id %lx, size=%d.\n",
335
			   type, (unsigned long)id, size);
336 337 338 339 340 341 342 343 344

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

		p += size;
	}

345
	if (n + 1 != priv->firm_size) {
346
		tuner_err("Firmware file is incomplete!\n");
347 348 349 350 351 352
		goto corrupt;
	}

	goto done;

corrupt:
353
	rc = -EINVAL;
354
	tuner_err("Error: firmware file is corrupted!\n");
355 356 357 358 359 360 361 362

err:
	tuner_info("Releasing loaded firmware file.\n");

	free_firmware(priv);

done:
	release_firmware(fw);
363
	tuner_dbg("Firmware files loaded.\n");
364 365 366 367

	return rc;
}

368 369
static int seek_firmware(struct dvb_frontend *fe, unsigned int type,
			 v4l2_std_id *id)
370 371
{
	struct xc2028_data *priv = fe->tuner_priv;
372
	int                i;
373

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

	if (!priv->firm) {
377
		tuner_err("Error! firmware not loaded\n");
378 379 380
		return -EINVAL;
	}

381
	if (((type & ~SCODE) == 0) && (*id == 0))
382
		*id = V4L2_STD_PAL;
383 384

	/* Seek for exact match */
385 386
	for (i = 0; i < priv->firm_size; i++) {
		if ((type == priv->firm[i].type) && (*id == priv->firm[i].id))
387 388 389 390
			goto found;
	}

	/* Seek for generic video standard match */
391 392
	for (i = 0; i < priv->firm_size; i++) {
		if ((type == priv->firm[i].type) && (*id & priv->firm[i].id))
393 394 395 396 397
			goto found;
	}

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

398 399
	i = -EINVAL;
	goto ret;
400 401 402 403

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

404
ret:
405 406 407 408 409
	tuner_dbg("%s firmware for type=", (i < 0)? "Can't find": "Found");
	if (debug) {
		dump_firm_type(type);
		printk("(%x), id %08lx.\n", type, (unsigned long)*id);
	}
410 411 412 413 414 415 416 417 418 419
	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;
	unsigned char      *p, *endp, buf[priv->max_len];

420
	tuner_dbg("%s called\n", __FUNCTION__);
421 422 423 424 425

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

426 427 428 429
	tuner_info("Loading firmware for type=");
	dump_firm_type(type);
	printk("(%x), id %08lx.\n", type, (unsigned long)*id);

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

	if (!p) {
433
		tuner_err("Firmware pointer were freed!");
434
		return -EINVAL;
435
	}
436
	endp = p + priv->firm[pos].size;
437

438
	while (p < endp) {
439 440 441
		__u16 size;

		/* Checks if there's enough bytes to read */
442
		if (p + sizeof(size) > endp) {
443
			tuner_err("Firmware chunk size is wrong\n");
444 445 446
			return -EINVAL;
		}

447
		size = le16_to_cpu(*(__u16 *) p);
448 449 450 451 452 453
		p += sizeof(size);

		if (size == 0xffff)
			return 0;

		if (!size) {
454
			/* Special callback command received */
455
			rc = priv->tuner_callback(priv->video_dev,
456 457
						  XC2028_TUNER_RESET, 0);
			if (rc < 0) {
458
				tuner_err("Error at RESET code %d\n",
459
					   (*p) & 0x7f);
460
				return -EINVAL;
461 462 463
			}
			continue;
		}
464 465 466

		/* Checks for a sleep command */
		if (size & 0x8000) {
467
			msleep(size & 0x7fff);
468
			continue;
469 470
		}

471
		if ((size + p > endp)) {
472
			tuner_err("missing bytes: need %d, have %d\n",
473
				   size, (int)(endp - p));
474 475
			return -EINVAL;
		}
476

477
		buf[0] = *p;
478
		p++;
479
		size--;
480

481
		/* Sends message chunks */
482 483 484
		while (size > 0) {
			int len = (size < priv->max_len - 1) ?
				   size : priv->max_len - 1;
485

486
			memcpy(buf + 1, p, len);
487

488 489
			i2c_send(rc, priv, buf, len + 1);
			if (rc < 0) {
490
				tuner_err("%d returned from send\n", rc);
491 492 493 494 495 496 497
				return -EINVAL;
			}

			p += len;
			size -= len;
		}
	}
498
	return 0;
499 500
}

501 502 503 504 505 506 507
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;

508
	tuner_dbg("%s called\n", __FUNCTION__);
509 510 511 512 513 514 515 516

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

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

	if (!p) {
517
		tuner_err("Firmware pointer were freed!");
518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536
		return -EINVAL;
	}

	if ((priv->firm[pos].size != 12 * 16) || (scode >= 16))
		return -EINVAL;

	if (priv->version < 0x0202) {
		send_seq(priv, {0x20, 0x00, 0x00, 0x00});
	} else {
		send_seq(priv, {0xa0, 0x00, 0x00, 0x00});
	}

	i2c_send(rc, priv, p + 12 * scode, 12);

	send_seq(priv, {0x00, 0x8c});

	return 0;
}

537
static int check_firmware(struct dvb_frontend *fe, enum tuner_mode new_mode,
538
			  v4l2_std_id std, fe_bandwidth_t bandwidth)
539
{
540
	struct xc2028_data      *priv = fe->tuner_priv;
541
	int			rc, version, hwmodel;
542 543
	v4l2_std_id		std0 = 0;
	unsigned int		type0 = 0, type = 0;
544
	int			change_digital_bandwidth;
545

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

548 549 550 551
	if (!priv->firm) {
		if (!priv->ctrl.fname)
			return -EINVAL;

552 553
		rc = load_all_firmwares(fe);
		if (rc < 0)
554 555 556
			return rc;
	}

557
	tuner_dbg("I am in mode %u and I should switch to mode %i\n",
558
		   priv->mode, new_mode);
559 560

	/* first of all, determine whether we have switched the mode */
561
	if (new_mode != priv->mode) {
562 563
		priv->mode = new_mode;
		priv->need_load_generic = 1;
564 565
	}

566
	change_digital_bandwidth = (priv->mode == T_DIGITAL_TV
567
				    && bandwidth != priv->bandwidth) ? 1 : 0;
568
	tuner_dbg("old bandwidth %u, new bandwidth %u\n", priv->bandwidth,
569
		   bandwidth);
570

571
	if (priv->need_load_generic) {
572
		/* Reset is needed before loading firmware */
573 574
		rc = priv->tuner_callback(priv->video_dev,
					  XC2028_TUNER_RESET, 0);
575
		if (rc < 0)
576 577
			return rc;

578
		type0 = BASE;
579 580 581 582

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

583
		if (priv->bandwidth == 8)
584 585 586 587 588
			type0 |= F8MHZ;

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

		rc = load_firmware(fe, type0, &std0);
589
		if (rc < 0) {
590 591
			tuner_err("Error %d while loading generic firmware\n",
				  rc);
592
			return rc;
593
		}
594

595 596 597 598
		priv->need_load_generic = 0;
		priv->firm_type = 0;
		if (priv->mode == T_DIGITAL_TV)
			change_digital_bandwidth = 1;
599 600
	}

601
	tuner_dbg("I should change bandwidth %u\n", change_digital_bandwidth);
602 603

	if (change_digital_bandwidth) {
604 605 606 607 608 609

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

		/* FIXME: When should select a DTV78 firmware?
		 */
610
		switch (bandwidth) {
611 612
		case BANDWIDTH_8_MHZ:
			type |= DTV8;
613
			break;
614 615
		case BANDWIDTH_7_MHZ:
			type |= DTV7;
616
			break;
617 618
		case BANDWIDTH_6_MHZ:
			/* FIXME: Should allow select also ATSC */
619
			type |= DTV6 | QAM;
620 621
			break;

622
		default:
623
			tuner_err("error: bandwidth not supported.\n");
624
		};
625
		priv->bandwidth = bandwidth;
626 627
	}

628
	/* Load INIT1, if needed */
629
	tuner_dbg("Load init1 firmware, if exists\n");
630
	type0 = BASE | INIT1;
631 632 633 634 635 636 637 638 639 640 641 642
	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;

643
	if (priv->firm_type & std) {
644
		tuner_dbg("Std-specific firmware already loaded.\n");
645
		return 0;
646
	}
647

648 649 650
	/* Add audio hack to std mask */
	std |= parse_audio_std_option();

651
	rc = load_firmware(fe, type, &std);
652
	if (rc < 0)
653 654
		return rc;

655
	/* Load SCODE firmware, if exists */
656
	tuner_dbg("Trying to load scode 0\n");
657 658 659
	type |= SCODE;

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

661 662 663 664 665 666 667
	version = xc2028_get_reg(priv, 0x0004);
	hwmodel = xc2028_get_reg(priv, 0x0008);

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

669
	priv->firm_type = std;
670 671 672 673

	return 0;
}

674
static int xc2028_signal(struct dvb_frontend *fe, u16 *strength)
675
{
676
	struct xc2028_data *priv = fe->tuner_priv;
677
	int                frq_lock, signal = 0;
678

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

681
	mutex_lock(&priv->lock);
682

683 684
	*strength = 0;

685 686
	/* Sync Lock Indicator */
	frq_lock = xc2028_get_reg(priv, 0x0002);
687
	if (frq_lock <= 0)
688
		goto ret;
689 690 691

	/* Frequency is locked. Return signal quality */

692 693
	/* Get SNR of the video signal */
	signal = xc2028_get_reg(priv, 0x0040);
694

695 696
	if (signal <= 0)
		signal = frq_lock;
697 698

ret:
699 700 701
	mutex_unlock(&priv->lock);

	*strength = signal;
702

703
	return 0;
704 705 706 707
}

#define DIV 15625

708 709 710
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)
711
{
712
	struct xc2028_data *priv = fe->tuner_priv;
713 714 715
	int		   rc = -EINVAL;
	unsigned char	   buf[5];
	u32		   div, offset = 0;
716

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

719 720
	mutex_lock(&priv->lock);

721 722
	/* HACK: It seems that specific firmware need to be reloaded
	   when freq is changed */
723

724
	priv->firm_type = 0;
725

726
	/* Reset GPIO 1 */
727
	rc = priv->tuner_callback(priv->video_dev, XC2028_TUNER_RESET, 0);
728
	if (rc < 0)
729 730
		goto ret;

731
	msleep(10);
732
	tuner_dbg("should set frequency %d kHz)\n", freq / 1000);
733

734
	if (check_firmware(fe, new_mode, std, bandwidth) < 0)
735
		goto ret;
736

737
	if (new_mode == T_DIGITAL_TV)
738
		offset = 2750000;
739

740
	div = (freq - offset + DIV / 2) / DIV;
741

742
	/* CMD= Set frequency */
743

744
	if (priv->version < 0x0202) {
745 746 747 748 749
		send_seq(priv, {0x00, 0x02, 0x00, 0x00});
	} else {
		send_seq(priv, {0x80, 0x02, 0x00, 0x00});
	}

750
	rc = priv->tuner_callback(priv->video_dev, XC2028_RESET_CLK, 1);
751
	if (rc < 0)
752
		goto ret;
753 754

	msleep(10);
755

756 757 758 759 760
	buf[0] = 0xff & (div >> 24);
	buf[1] = 0xff & (div >> 16);
	buf[2] = 0xff & (div >> 8);
	buf[3] = 0xff & (div);
	buf[4] = 0;
761

762
	i2c_send(rc, priv, buf, sizeof(buf));
763
	if (rc < 0)
764
		goto ret;
765 766
	msleep(100);

767
	priv->frequency = freq;
768

769
	tuner_dbg("divider= %02x %02x %02x %02x (freq=%d.%02d)\n",
770 771
	       buf[1], buf[2], buf[3], buf[4],
	       freq / 1000000, (freq % 1000000) / 10000);
772

773
	rc = 0;
774

775 776
ret:
	mutex_unlock(&priv->lock);
777

778
	return rc;
779 780
}

781
static int xc2028_set_tv_freq(struct dvb_frontend *fe,
782
			      struct analog_parameters *p)
783
{
784
	struct xc2028_data *priv = fe->tuner_priv;
785

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

788 789
	return generic_set_tv_freq(fe, 62500l * p->frequency, T_ANALOG_TV,
				   p->std, BANDWIDTH_8_MHZ /* NOT USED */);
790
}
791

792 793
static int xc2028_set_params(struct dvb_frontend *fe,
			     struct dvb_frontend_parameters *p)
794
{
795
	struct xc2028_data *priv = fe->tuner_priv;
796

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

799 800
	/* FIXME: Only OFDM implemented */
	if (fe->ops.info.type != FE_OFDM) {
801
		tuner_err("DTV type not implemented.\n");
802
		return -EINVAL;
803 804
	}

805
	return generic_set_tv_freq(fe, p->frequency, T_DIGITAL_TV,
806 807
				   0 /* NOT USED */,
				   p->u.ofdm.bandwidth);
808 809

}
810

811
static int xc2028_dvb_release(struct dvb_frontend *fe)
812
{
813 814
	struct xc2028_data *priv = fe->tuner_priv;

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

817
	priv->count--;
818

819
	if (!priv->count) {
820 821
		list_del(&priv->xc2028_list);

822
		kfree(priv->ctrl.fname);
823 824

		free_firmware(priv);
825
		kfree(priv);
826
	}
827 828 829 830

	return 0;
}

831
static int xc2028_get_frequency(struct dvb_frontend *fe, u32 *frequency)
832
{
833
	struct xc2028_data *priv = fe->tuner_priv;
834

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

837
	*frequency = priv->frequency;
838 839 840 841

	return 0;
}

842
static int xc2028_set_config(struct dvb_frontend *fe, void *priv_cfg)
843 844 845 846
{
	struct xc2028_data *priv = fe->tuner_priv;
	struct xc2028_ctrl *p    = priv_cfg;

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

	priv->ctrl.type = p->type;

	if (p->fname) {
852
		kfree(priv->ctrl.fname);
853

854
		priv->ctrl.fname = kmalloc(strlen(p->fname) + 1, GFP_KERNEL);
855 856 857 858 859 860 861
		if (!priv->ctrl.fname)
			return -ENOMEM;

		free_firmware(priv);
		strcpy(priv->ctrl.fname, p->fname);
	}

862
	if (p->max_len > 0)
863 864
		priv->max_len = p->max_len;

865 866 867
	return 0;
}

868
static const struct dvb_tuner_ops xc2028_dvb_tuner_ops = {
869
	.info = {
870 871 872 873 874
		 .name = "Xceive XC3028",
		 .frequency_min = 42000000,
		 .frequency_max = 864000000,
		 .frequency_step = 50000,
		 },
875

876
	.set_config	   = xc2028_set_config,
877 878 879 880 881
	.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,
882 883 884

};

885
int xc2028_attach(struct dvb_frontend *fe, struct i2c_adapter *i2c_adap,
886
		  u8 i2c_addr, struct device *dev, void *video_dev,
887
		  int (*tuner_callback) (void *dev, int command, int arg))
888
{
889
	struct xc2028_data *priv;
890

891 892
	if (debug)
		printk(KERN_DEBUG PREFIX "Xcv2028/3028 init called!\n");
893

894 895 896 897 898 899 900
	if (NULL == dev)
		return -ENODEV;

	if (NULL == video_dev)
		return -ENODEV;

	if (!tuner_callback) {
901
		printk(KERN_ERR PREFIX "No tuner callback!\n");
902 903 904 905
		return -EINVAL;
	}

	list_for_each_entry(priv, &xc2028_list, xc2028_list) {
906
		if (priv->dev == dev)
907 908 909 910 911 912 913
			dev = NULL;
	}

	if (dev) {
		priv = kzalloc(sizeof(*priv), GFP_KERNEL);
		if (priv == NULL)
			return -ENOMEM;
914

915
		fe->tuner_priv = priv;
916

917 918
		priv->bandwidth = BANDWIDTH_6_MHZ;
		priv->need_load_generic = 1;
919 920 921 922 923 924
		priv->mode = T_UNINITIALIZED;
		priv->i2c_props.addr = i2c_addr;
		priv->i2c_props.adap = i2c_adap;
		priv->dev = dev;
		priv->video_dev = video_dev;
		priv->tuner_callback = tuner_callback;
925 926
		priv->max_len = 13;

927 928 929

		mutex_init(&priv->lock);

930
		list_add_tail(&priv->xc2028_list, &xc2028_list);
931
	}
932
	priv->count++;
933 934

	memcpy(&fe->ops.tuner_ops, &xc2028_dvb_tuner_ops,
935
	       sizeof(xc2028_dvb_tuner_ops));
936 937 938 939 940

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

	return 0;
}
941 942
EXPORT_SYMBOL(xc2028_attach);

943
MODULE_DESCRIPTION("Xceive xc2028/xc3028 tuner driver");
944
MODULE_AUTHOR("Michel Ludwig <michel.ludwig@gmail.com>");
945 946
MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@infradead.org>");
MODULE_LICENSE("GPL");