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

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

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

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

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

131
	tuner_dbg("%s %04x called\n", __FUNCTION__, reg);
132

133
	buf[0] = reg >> 8;
134
	buf[1] = (unsigned char) reg;
135

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

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

143 144 145 146
void dump_firm_type(unsigned int type)
{
	 if (type & BASE)
		printk("BASE ");
147 148
	 if (type & INIT1)
		printk("INIT1 ");
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 205 206
	 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 ");
}

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

	return 0;
}

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

	if (!priv->firm)
		return;

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

235 236
	kfree(priv->firm);

237
	priv->firm = NULL;
238 239 240
	priv->need_load_generic = 1;
}

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

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

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

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

268
	if (fw->size < sizeof(name) - 1 + 2) {
269
		tuner_err("Error: firmware size is zero!\n");
270
		rc = -EINVAL;
271
		goto done;
272
	}
273

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

278
	priv->version = le16_to_cpu(*(__u16 *) p);
279 280
	p += 2;

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

284
	if (p + 2 > endp)
285 286
		goto corrupt;

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

290 291
	tuner_info("There are %d firmwares at %s\n",
		   n_array, priv->ctrl.fname);
292

293
	priv->firm = kzalloc(sizeof(*priv->firm) * n_array, GFP_KERNEL);
294 295

	if (!fw) {
296
		tuner_err("Not enough memory for reading firmware.\n");
297
		rc = -ENOMEM;
298
		goto done;
299 300
	}

301
	priv->firm_size = n_array;
302 303
	n = -1;
	while (p < endp) {
304 305 306 307 308
		__u32 type, size;
		v4l2_std_id id;

		n++;
		if (n >= n_array) {
309
			tuner_err("Too much firmwares at the file\n");
310 311 312 313
			goto corrupt;
		}

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

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

322
		id = le64_to_cpu(*(v4l2_std_id *) p);
323 324
		p += sizeof(id);

325
		size = le32_to_cpu(*(__u32 *) p);
326 327
		p += sizeof(size);

328
		if ((!size) || (size + p > endp)) {
329
			tuner_err("Firmware type ");
330
			dump_firm_type(type);
331 332 333 334
			printk("(%x), id %llx is corrupted "
			       "(size=%d, expected %d)\n",
			       type, id,
			       (unsigned)(endp - p), size);
335 336 337
			goto corrupt;
		}

338
		priv->firm[n].ptr = kzalloc(size, GFP_KERNEL);
339
		if (!priv->firm[n].ptr) {
340
			tuner_err("Not enough memory.\n");
341
			rc = -ENOMEM;
342 343
			goto err;
		}
344
		tuner_info("Reading firmware type ");
345 346
		dump_firm_type(type);
		printk("(%x), id %lx, size=%d.\n",
347
			   type, (unsigned long)id, size);
348 349 350 351 352 353 354 355 356

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

		p += size;
	}

357
	if (n + 1 != priv->firm_size) {
358
		tuner_err("Firmware file is incomplete!\n");
359 360 361 362 363 364
		goto corrupt;
	}

	goto done;

corrupt:
365
	rc = -EINVAL;
366
	tuner_err("Error: firmware file is corrupted!\n");
367 368 369 370 371 372 373 374

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

	free_firmware(priv);

done:
	release_firmware(fw);
375
	tuner_dbg("Firmware files loaded.\n");
376 377 378 379

	return rc;
}

380 381
static int seek_firmware(struct dvb_frontend *fe, unsigned int type,
			 v4l2_std_id *id)
382 383
{
	struct xc2028_data *priv = fe->tuner_priv;
384
	int                i;
385

386
	tuner_dbg("%s called\n", __FUNCTION__);
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 404
	for (i = 0; i < priv->firm_size; i++) {
		if ((type == priv->firm[i].type) && (*id & priv->firm[i].id))
405 406 407 408 409
			goto found;
	}

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

410 411
	i = -EINVAL;
	goto ret;
412 413 414 415

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

416
ret:
417 418 419 420 421
	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);
	}
422 423 424 425 426 427 428 429 430 431
	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];

432
	tuner_dbg("%s called\n", __FUNCTION__);
433 434 435 436 437

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

438 439 440 441
	tuner_info("Loading firmware for type=");
	dump_firm_type(type);
	printk("(%x), id %08lx.\n", type, (unsigned long)*id);

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

	if (!p) {
445
		tuner_err("Firmware pointer were freed!");
446
		return -EINVAL;
447
	}
448
	endp = p + priv->firm[pos].size;
449

450
	while (p < endp) {
451 452 453
		__u16 size;

		/* Checks if there's enough bytes to read */
454
		if (p + sizeof(size) > endp) {
455
			tuner_err("Firmware chunk size is wrong\n");
456 457 458
			return -EINVAL;
		}

459
		size = le16_to_cpu(*(__u16 *) p);
460 461 462 463 464 465
		p += sizeof(size);

		if (size == 0xffff)
			return 0;

		if (!size) {
466
			/* Special callback command received */
467
			rc = priv->tuner_callback(priv->video_dev,
468 469
						  XC2028_TUNER_RESET, 0);
			if (rc < 0) {
470
				tuner_err("Error at RESET code %d\n",
471
					   (*p) & 0x7f);
472
				return -EINVAL;
473 474 475
			}
			continue;
		}
476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491
		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;
				}
			default:
				tuner_info("Invalid RESET code %d\n",
					   size & 0x7f);
				return -EINVAL;

			}
492
			continue;
493
		}
494 495 496

		/* Checks for a sleep command */
		if (size & 0x8000) {
497
			msleep(size & 0x7fff);
498
			continue;
499 500
		}

501
		if ((size + p > endp)) {
502
			tuner_err("missing bytes: need %d, have %d\n",
503
				   size, (int)(endp - p));
504 505
			return -EINVAL;
		}
506

507
		buf[0] = *p;
508
		p++;
509
		size--;
510

511
		/* Sends message chunks */
512 513 514
		while (size > 0) {
			int len = (size < priv->max_len - 1) ?
				   size : priv->max_len - 1;
515

516
			memcpy(buf + 1, p, len);
517

518
			rc = i2c_send(priv, buf, len + 1);
519
			if (rc < 0) {
520
				tuner_err("%d returned from send\n", rc);
521 522 523 524 525 526 527
				return -EINVAL;
			}

			p += len;
			size -= len;
		}
	}
528
	return 0;
529 530
}

531 532 533 534 535 536 537
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;

538
	tuner_dbg("%s called\n", __FUNCTION__);
539 540 541 542 543 544 545 546

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

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

	if (!p) {
547
		tuner_err("Firmware pointer were freed!");
548 549 550 551 552 553
		return -EINVAL;
	}

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

554 555 556 557 558 559
	if (priv->version < 0x0202)
		rc = send_seq(priv, {0x20, 0x00, 0x00, 0x00});
	else
		rc = send_seq(priv, {0xa0, 0x00, 0x00, 0x00});
	if (rc < 0)
		return -EIO;
560

561 562 563
	rc = i2c_send(priv, p + 12 * scode, 12);
	if (rc < 0)
		return -EIO;
564

565 566 567
	rc = send_seq(priv, {0x00, 0x8c});
	if (rc < 0)
		return -EIO;
568 569 570 571

	return 0;
}

572
static int check_firmware(struct dvb_frontend *fe, enum tuner_mode new_mode,
573
			  v4l2_std_id std, fe_bandwidth_t bandwidth)
574
{
575
	struct xc2028_data      *priv = fe->tuner_priv;
576 577
	int			rc;
	u16			version, hwmodel;
578 579
	v4l2_std_id		std0 = 0;
	unsigned int		type0 = 0, type = 0;
580
	int			change_digital_bandwidth;
581

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

584
	if (!priv->firm) {
585 586
		if (!priv->ctrl.fname) {
			tuner_info("xc2028/3028 firmware name not set!\n");
587
			return -EINVAL;
588
		}
589

590 591
		rc = load_all_firmwares(fe);
		if (rc < 0)
592 593 594
			return rc;
	}

595
	tuner_dbg("I am in mode %u and I should switch to mode %i\n",
596
		   priv->mode, new_mode);
597 598

	/* first of all, determine whether we have switched the mode */
599
	if (new_mode != priv->mode) {
600 601
		priv->mode = new_mode;
		priv->need_load_generic = 1;
602 603
	}

604
	change_digital_bandwidth = (priv->mode == T_DIGITAL_TV
605
				    && bandwidth != priv->bandwidth) ? 1 : 0;
606
	tuner_dbg("old bandwidth %u, new bandwidth %u\n", priv->bandwidth,
607
		   bandwidth);
608

609
	if (priv->need_load_generic) {
610
		/* Reset is needed before loading firmware */
611 612
		rc = priv->tuner_callback(priv->video_dev,
					  XC2028_TUNER_RESET, 0);
613
		if (rc < 0)
614 615
			return rc;

616
		type0 = BASE;
617 618 619 620

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

621
		if (priv->bandwidth == 8)
622 623 624 625 626
			type0 |= F8MHZ;

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

		rc = load_firmware(fe, type0, &std0);
627
		if (rc < 0) {
628 629
			tuner_err("Error %d while loading generic firmware\n",
				  rc);
630
			return rc;
631
		}
632

633 634 635 636
		priv->need_load_generic = 0;
		priv->firm_type = 0;
		if (priv->mode == T_DIGITAL_TV)
			change_digital_bandwidth = 1;
637 638
	}

639
	tuner_dbg("I should change bandwidth %u\n", change_digital_bandwidth);
640 641

	if (change_digital_bandwidth) {
642 643 644 645 646 647

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

		/* FIXME: When should select a DTV78 firmware?
		 */
648
		switch (bandwidth) {
649 650
		case BANDWIDTH_8_MHZ:
			type |= DTV8;
651
			break;
652 653
		case BANDWIDTH_7_MHZ:
			type |= DTV7;
654
			break;
655 656
		case BANDWIDTH_6_MHZ:
			/* FIXME: Should allow select also ATSC */
657
			type |= DTV6 | QAM;
658 659
			break;

660
		default:
661
			tuner_err("error: bandwidth not supported.\n");
662
		};
663
		priv->bandwidth = bandwidth;
664 665
	}

666 667 668
	if (!change_digital_bandwidth && priv->mode == T_DIGITAL_TV)
		return 0;

669
	/* Load INIT1, if needed */
670
	tuner_dbg("Load init1 firmware, if exists\n");
671
	type0 = BASE | INIT1;
672 673 674 675 676 677 678 679 680 681 682 683
	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;

684
	if (priv->firm_type & std) {
685
		tuner_dbg("Std-specific firmware already loaded.\n");
686
		return 0;
687
	}
688

689 690 691
	/* Add audio hack to std mask */
	std |= parse_audio_std_option();

692
	rc = load_firmware(fe, type, &std);
693
	if (rc < 0)
694 695
		return rc;

696
	/* Load SCODE firmware, if exists */
697
	tuner_dbg("Trying to load scode 0\n");
698 699 700
	type |= SCODE;

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

702 703
	xc2028_get_reg(priv, 0x0004, &version);
	xc2028_get_reg(priv, 0x0008, &hwmodel);
704 705 706 707 708

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

710
	priv->firm_type = std;
711 712 713 714

	return 0;
}

715
static int xc2028_signal(struct dvb_frontend *fe, u16 *strength)
716
{
717
	struct xc2028_data *priv = fe->tuner_priv;
718 719
	u16                 frq_lock, signal = 0;
	int                 rc;
720

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

723
	mutex_lock(&priv->lock);
724

725
	/* Sync Lock Indicator */
726 727
	rc = xc2028_get_reg(priv, 0x0002, &frq_lock);
	if (rc < 0 || frq_lock == 0)
728
		goto ret;
729 730 731

	/* Frequency is locked. Return signal quality */

732
	/* Get SNR of the video signal */
733 734 735
	rc = xc2028_get_reg(priv, 0x0040, &signal);
	if (rc < 0)
		signal = -frq_lock;
736 737

ret:
738 739 740
	mutex_unlock(&priv->lock);

	*strength = signal;
741

742
	return rc;
743 744 745 746
}

#define DIV 15625

747 748 749
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)
750
{
751
	struct xc2028_data *priv = fe->tuner_priv;
752 753 754
	int		   rc = -EINVAL;
	unsigned char	   buf[5];
	u32		   div, offset = 0;
755

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

758 759
	mutex_lock(&priv->lock);

760 761
	/* HACK: It seems that specific firmware need to be reloaded
	   when freq is changed */
762

763
	priv->firm_type = 0;
764

765
	/* Reset GPIO 1 */
766
	rc = priv->tuner_callback(priv->video_dev, XC2028_TUNER_RESET, 0);
767
	if (rc < 0)
768 769
		goto ret;

770
	msleep(10);
771
	tuner_dbg("should set frequency %d kHz)\n", freq / 1000);
772

773
	if (check_firmware(fe, new_mode, std, bandwidth) < 0)
774
		goto ret;
775

776
	if (new_mode == T_DIGITAL_TV)
777
		offset = 2750000;
778

779
	div = (freq - offset + DIV / 2) / DIV;
780

781
	/* CMD= Set frequency */
782

783 784 785 786 787 788
	if (priv->version < 0x0202)
		rc = send_seq(priv, {0x00, 0x02, 0x00, 0x00});
	else
		rc = send_seq(priv, {0x80, 0x02, 0x00, 0x00});
	if (rc < 0)
		goto ret;
789

790
	rc = priv->tuner_callback(priv->video_dev, XC2028_RESET_CLK, 1);
791
	if (rc < 0)
792
		goto ret;
793 794

	msleep(10);
795

796 797 798 799 800
	buf[0] = 0xff & (div >> 24);
	buf[1] = 0xff & (div >> 16);
	buf[2] = 0xff & (div >> 8);
	buf[3] = 0xff & (div);
	buf[4] = 0;
801

802
	rc = i2c_send(priv, buf, sizeof(buf));
803
	if (rc < 0)
804
		goto ret;
805 806
	msleep(100);

807
	priv->frequency = freq;
808

809
	tuner_dbg("divider= %02x %02x %02x %02x (freq=%d.%02d)\n",
810 811
	       buf[1], buf[2], buf[3], buf[4],
	       freq / 1000000, (freq % 1000000) / 10000);
812

813
	rc = 0;
814

815 816
ret:
	mutex_unlock(&priv->lock);
817

818
	return rc;
819 820
}

821
static int xc2028_set_tv_freq(struct dvb_frontend *fe,
822
			      struct analog_parameters *p)
823
{
824
	struct xc2028_data *priv = fe->tuner_priv;
825

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

828 829
	return generic_set_tv_freq(fe, 62500l * p->frequency, T_ANALOG_TV,
				   p->std, BANDWIDTH_8_MHZ /* NOT USED */);
830
}
831

832 833
static int xc2028_set_params(struct dvb_frontend *fe,
			     struct dvb_frontend_parameters *p)
834
{
835
	struct xc2028_data *priv = fe->tuner_priv;
836

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

839 840
	/* FIXME: Only OFDM implemented */
	if (fe->ops.info.type != FE_OFDM) {
841
		tuner_err("DTV type not implemented.\n");
842
		return -EINVAL;
843 844
	}

845
	return generic_set_tv_freq(fe, p->frequency, T_DIGITAL_TV,
846 847
				   0 /* NOT USED */,
				   p->u.ofdm.bandwidth);
848 849

}
850

851
static int xc2028_dvb_release(struct dvb_frontend *fe)
852
{
853 854
	struct xc2028_data *priv = fe->tuner_priv;

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

857
	priv->count--;
858

859
	if (!priv->count) {
860 861
		list_del(&priv->xc2028_list);

862
		kfree(priv->ctrl.fname);
863 864

		free_firmware(priv);
865
		kfree(priv);
866
	}
867 868 869 870

	return 0;
}

871
static int xc2028_get_frequency(struct dvb_frontend *fe, u32 *frequency)
872
{
873
	struct xc2028_data *priv = fe->tuner_priv;
874

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

877
	*frequency = priv->frequency;
878 879 880 881

	return 0;
}

882
static int xc2028_set_config(struct dvb_frontend *fe, void *priv_cfg)
883 884 885 886
{
	struct xc2028_data *priv = fe->tuner_priv;
	struct xc2028_ctrl *p    = priv_cfg;

887
	tuner_dbg("%s called\n", __FUNCTION__);
888 889 890 891

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

	if (p->fname) {
892
		kfree(priv->ctrl.fname);
893

894
		priv->ctrl.fname = kmalloc(strlen(p->fname) + 1, GFP_KERNEL);
895 896 897 898 899 900 901
		if (!priv->ctrl.fname)
			return -ENOMEM;

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

902
	if (p->max_len > 0)
903 904
		priv->max_len = p->max_len;

905 906 907
	return 0;
}

908
static const struct dvb_tuner_ops xc2028_dvb_tuner_ops = {
909
	.info = {
910 911 912 913 914
		 .name = "Xceive XC3028",
		 .frequency_min = 42000000,
		 .frequency_max = 864000000,
		 .frequency_step = 50000,
		 },
915

916
	.set_config	   = xc2028_set_config,
917 918 919 920 921
	.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,
922 923 924

};

925
void *xc2028_attach(struct dvb_frontend *fe, struct xc2028_config *cfg)
926
{
927
	struct xc2028_data *priv;
928
	void               *video_dev;
929

930 931
	if (debug)
		printk(KERN_DEBUG PREFIX "Xcv2028/3028 init called!\n");
932

933 934
	if (NULL == cfg->video_dev)
		return NULL;
935

936 937 938
	if (!fe) {
		printk(KERN_ERR PREFIX "No frontend!\n");
		return NULL;
939 940
	}

941 942
	video_dev = cfg->video_dev;

943
	list_for_each_entry(priv, &xc2028_list, xc2028_list) {
944 945 946 947
		if (priv->video_dev == cfg->video_dev) {
			video_dev = NULL;
			break;
		}
948 949
	}

950
	if (video_dev) {
951 952
		priv = kzalloc(sizeof(*priv), GFP_KERNEL);
		if (priv == NULL)
953
			return NULL;
954

955 956
		priv->bandwidth = BANDWIDTH_6_MHZ;
		priv->need_load_generic = 1;
957
		priv->mode = T_UNINITIALIZED;
958 959
		priv->i2c_props.addr = cfg->i2c_addr;
		priv->i2c_props.adap = cfg->i2c_adap;
960
		priv->video_dev = video_dev;
961
		priv->tuner_callback = cfg->callback;
962 963
		priv->max_len = 13;

964 965
		mutex_init(&priv->lock);

966
		list_add_tail(&priv->xc2028_list, &xc2028_list);
967
	}
968 969

	fe->tuner_priv = priv;
970
	priv->count++;
971 972

	memcpy(&fe->ops.tuner_ops, &xc2028_dvb_tuner_ops,
973
	       sizeof(xc2028_dvb_tuner_ops));
974 975 976

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

977
	return fe;
978
}
979

980 981
EXPORT_SYMBOL(xc2028_attach);

982
MODULE_DESCRIPTION("Xceive xc2028/xc3028 tuner driver");
983
MODULE_AUTHOR("Michel Ludwig <michel.ludwig@gmail.com>");
984 985
MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@infradead.org>");
MODULE_LICENSE("GPL");