tuner-xc2028.c 27.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
27
28
29
static int debug;
module_param(debug, int, 0644);
MODULE_PARM_DESC(debug, "enable verbose debug messages");

30
31
32
33
34
35
36
37
38
39
40
41
42
43
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");

44
static LIST_HEAD(xc2028_list);
45
46
static DEFINE_MUTEX(xc2028_list_mutex);

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

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

65
struct xc2028_data {
66
67
68
69
70
71
	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;
72
73
74
75
	__u32			frequency;

	struct firmware_description *firm;
	int			firm_size;
76
	__u16			firm_version;
77

78
79
80
	__u16			hwmodel;
	__u16			hwvers;

81
	struct xc2028_ctrl	ctrl;
82

83
	struct firmware_properties cur_fw;
84
85

	struct mutex lock;
86
87
};

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

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

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

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

133
	tuner_dbg("%s %04x called\n", __FUNCTION__, reg);
134

135
	buf[0] = reg >> 8;
136
	buf[1] = (unsigned char) reg;
137

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

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

145
#define dump_firm_type(t) 	dump_firm_type_and_int_freq(t, 0)
146
static void dump_firm_type_and_int_freq(unsigned int type, u16 int_freq)
147
148
149
{
	 if (type & BASE)
		printk("BASE ");
150
151
	 if (type & INIT1)
		printk("INIT1 ");
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
207
	 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 ");
208
209
	 if (type & HAS_IF)
		printk("HAS_IF_%d ", int_freq);
210
211
}

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

	return 0;
}

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

	if (!priv->firm)
		return;

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

240
241
	kfree(priv->firm);

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

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

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

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

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

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

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

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

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

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

291
292
293
	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);
294

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

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

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

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

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

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

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

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

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

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

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

		p += size;
	}

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

	goto done;

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

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

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

	return rc;
}

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

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

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

409
	if (((type & ~SCODE) == 0) && (*id == 0))
410
		*id = V4L2_STD_PAL;
411

412
413
	if (type & BASE)
		type &= BASE_TYPES;
414
	else if (type & SCODE) {
415
		type &= SCODE_TYPES;
416
417
		ign_firm_type_mask = HAS_IF;
	} else if (type & DTV_TYPES)
418
419
420
		type &= DTV_TYPES;
	else if (type & STD_SPECIFIC_TYPES)
		type &= STD_SPECIFIC_TYPES;
421

422
	/* Seek for exact match */
423
	for (i = 0; i < priv->firm_size; i++) {
424
425
		if ((type == (priv->firm[i].type & ~ign_firm_type_mask)) &&
		    (*id == priv->firm[i].id))
426
427
428
429
			goto found;
	}

	/* Seek for generic video standard match */
430
	for (i = 0; i < priv->firm_size; i++) {
431
432
433
		v4l2_std_id match_mask;
		int nr_matches;

434
		if (type != (priv->firm[i].type & ~ign_firm_type_mask))
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
			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;
458
459
460
461
	}

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

462
	i = -ENOENT;
463
	goto ret;
464
465
466
467

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

468
ret:
469
	tuner_dbg("%s firmware for type=", (i < 0) ? "Can't find" : "Found");
470
471
	if (debug) {
		dump_firm_type(type);
472
		printk("(%x), id %016llx.\n", type, (unsigned long long)*id);
473
	}
474
475
476
477
478
479
480
481
	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;
482
	unsigned char      *p, *endp, buf[priv->ctrl.max_len];
483

484
	tuner_dbg("%s called\n", __FUNCTION__);
485
486
487
488
489

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

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

495
496
	p = priv->firm[pos].ptr;
	endp = p + priv->firm[pos].size;
497

498
	while (p < endp) {
499
500
501
		__u16 size;

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

507
		size = le16_to_cpu(*(__u16 *) p);
508
509
510
511
512
513
		p += sizeof(size);

		if (size == 0xffff)
			return 0;

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

			}
541
			continue;
542
		}
543
544
545

		/* Checks for a sleep command */
		if (size & 0x8000) {
546
			msleep(size & 0x7fff);
547
			continue;
548
549
		}

550
		if ((size + p > endp)) {
551
			tuner_err("missing bytes: need %d, have %d\n",
552
				   size, (int)(endp - p));
553
554
			return -EINVAL;
		}
555

556
		buf[0] = *p;
557
		p++;
558
		size--;
559

560
		/* Sends message chunks */
561
		while (size > 0) {
562
563
			int len = (size < priv->ctrl.max_len - 1) ?
				   size : priv->ctrl.max_len - 1;
564

565
			memcpy(buf + 1, p, len);
566

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

			p += len;
			size -= len;
		}
	}
577
	return 0;
578
579
}

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

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

589
590
591
592
593
594
595
	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) &&
596
			    (priv->firm[pos].type & HAS_IF))
597
598
599
600
601
				break;
		}
		if (pos == priv->firm_size)
			return -ENOENT;
	}
602
603
604

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

605
	if (priv->firm[pos].type & HAS_IF) {
606
607
608
609
610
611
612
613
614
615
616
		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;
	}
617

618
	tuner_info("Loading SCODE for type=");
619
620
	dump_firm_type_and_int_freq(priv->firm[pos].type,
				    priv->firm[pos].int_freq);
621
622
623
	printk("(%x), id %016llx.\n", priv->firm[pos].type,
	       (unsigned long long)*id);

624
	if (priv->firm_version < 0x0202)
625
626
627
628
629
		rc = send_seq(priv, {0x20, 0x00, 0x00, 0x00});
	else
		rc = send_seq(priv, {0xa0, 0x00, 0x00, 0x00});
	if (rc < 0)
		return -EIO;
630

631
	rc = i2c_send(priv, p, 12);
632
633
	if (rc < 0)
		return -EIO;
634

635
636
637
	rc = send_seq(priv, {0x00, 0x8c});
	if (rc < 0)
		return -EIO;
638
639
640
641

	return 0;
}

642
static int check_firmware(struct dvb_frontend *fe, unsigned int type,
643
			  v4l2_std_id std, __u16 int_freq)
644
{
645
	struct xc2028_data         *priv = fe->tuner_priv;
646
	struct firmware_properties new_fw;
647
648
649
	int			   rc = 0, is_retry = 0;
	u16			   version, hwmodel;
	v4l2_std_id		   std0;
650

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

653
	if (!priv->firm) {
654
655
		if (!priv->ctrl.fname) {
			tuner_info("xc2028/3028 firmware name not set!\n");
656
			return -EINVAL;
657
		}
658

659
660
		rc = load_all_firmwares(fe);
		if (rc < 0)
661
662
663
			return rc;
	}

664
	if (priv->ctrl.mts && !(type & FM))
665
		type |= MTS;
666

667
retry:
668
669
670
671
672
	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;
673
	new_fw.int_freq = int_freq;
674
675
676
677

	tuner_dbg("checking firmware, user requested type=");
	if (debug) {
		dump_firm_type(new_fw.type);
678
		printk("(%x), id %016llx, ", new_fw.type,
679
		       (unsigned long long)new_fw.std_req);
680
681
682
683
684
685
686
		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);
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
	}

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

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

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

705
706
707
	/* BASE firmwares are all std0 */
	std0 = 0;
	rc = load_firmware(fe, BASE | new_fw.type, &std0);
708
709
710
711
712
	if (rc < 0) {
		tuner_err("Error %d while loading base firmware\n",
			  rc);
		goto fail;
	}
713

714
	/* Load INIT1, if needed */
715
	tuner_dbg("Load init1 firmware, if exists\n");
716

717
	rc = load_firmware(fe, BASE | INIT1 | new_fw.type, &std0);
718
719
720
	if (rc == -ENOENT)
		rc = load_firmware(fe, (BASE | INIT1 | new_fw.type) & ~F8MHZ,
				   &std0);
721
722
723
724
725
	if (rc < 0 && rc != -ENOENT) {
		tuner_err("Error %d while loading init1 firmware\n",
			  rc);
		goto fail;
	}
726

727
728
729
730
skip_base:
	/*
	 * No need to reload standard specific firmware if base firmware
	 * was not reloaded and requested video standards have not changed.
731
	 */
732
733
	if (priv->cur_fw.type == (BASE | new_fw.type) &&
	    priv->cur_fw.std_req == std) {
734
		tuner_dbg("Std-specific firmware already loaded.\n");
735
		goto skip_std_specific;
736
	}
737

738
739
740
741
	/* Reloading std-specific firmware forces a SCODE update */
	priv->cur_fw.scode_table = 0;

	rc = load_firmware(fe, new_fw.type, &new_fw.id);
742
743
744
	if (rc == -ENOENT)
		rc = load_firmware(fe, new_fw.type & ~F8MHZ, &new_fw.id);

745
	if (rc < 0)
746
747
748
749
750
751
752
753
		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;
	}
754

755
756
757
	if (new_fw.type & FM)
		goto check_device;

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

761
762
	rc = load_scode(fe, new_fw.type | new_fw.scode_table, &new_fw.id,
			new_fw.int_freq, new_fw.scode_nr);
763

764
check_device:
765
766
767
768
769
	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;
	}
770
771
772
773
774

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

776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
	/* 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;
	}

793
794
795
796
797
798
799
800
801
	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;
802
803

	return 0;
804
805
806

fail:
	memset(&priv->cur_fw, 0, sizeof(priv->cur_fw));
807
808
809
810
811
812
813
	if (!is_retry) {
		msleep(50);
		is_retry = 1;
		tuner_dbg("Retrying firmware load\n");
		goto retry;
	}

814
815
816
	if (rc == -ENOENT)
		rc = -EINVAL;
	return rc;
817
818
}

819
static int xc2028_signal(struct dvb_frontend *fe, u16 *strength)
820
{
821
	struct xc2028_data *priv = fe->tuner_priv;
822
823
	u16                 frq_lock, signal = 0;
	int                 rc;
824

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

827
	mutex_lock(&priv->lock);
828

829
	/* Sync Lock Indicator */
830
831
	rc = xc2028_get_reg(priv, 0x0002, &frq_lock);
	if (rc < 0 || frq_lock == 0)
832
		goto ret;
833
834
835

	/* Frequency is locked. Return signal quality */

836
	/* Get SNR of the video signal */
837
838
839
	rc = xc2028_get_reg(priv, 0x0040, &signal);
	if (rc < 0)
		signal = -frq_lock;
840
841

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

	*strength = signal;
845

846
	return rc;
847
848
849
850
}

#define DIV 15625

851
static int generic_set_freq(struct dvb_frontend *fe, u32 freq /* in HZ */,
852
853
854
855
			    enum tuner_mode new_mode,
			    unsigned int type,
			    v4l2_std_id std,
			    u16 int_freq)
856
{
857
	struct xc2028_data *priv = fe->tuner_priv;
858
	int		   rc = -EINVAL;
859
	unsigned char	   buf[4];
860
	u32		   div, offset = 0;
861

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

864
865
	mutex_lock(&priv->lock);

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

868
	if (check_firmware(fe, type, std, int_freq) < 0)
869
		goto ret;
870

871
872
873
874
875
876
877
	/* 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.
	 */
878
	if (new_mode == T_ANALOG_TV) {
879
		rc = send_seq(priv, {0x00, 0x00});
880
881
882
	} else if (priv->cur_fw.type & ATSC) {
		offset = 1750000;
	} else {
883
		offset = 2750000;
884
885
886
887
888
889
890
891
892
893
		/*
		 * 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))
894
895
			offset -= 500000;
	}
896

897
	div = (freq - offset + DIV / 2) / DIV;
898

899
	/* CMD= Set frequency */
900
	if (priv->firm_version < 0x0202)
901
902
903
904
905
		rc = send_seq(priv, {0x00, 0x02, 0x00, 0x00});
	else
		rc = send_seq(priv, {0x80, 0x02, 0x00, 0x00});
	if (rc < 0)
		goto ret;
906

907
908
909
910
911
	/* Return code shouldn't be checked.
	   The reset CLK is needed only with tm6000.
	   Driver should work fine even if this fails.
	 */
	priv->tuner_callback(priv->video_dev, XC2028_RESET_CLK, 1);
912
913

	msleep(10);
914

915
916
917
918
	buf[0] = 0xff & (div >> 24);
	buf[1] = 0xff & (div >> 16);
	buf[2] = 0xff & (div >> 8);
	buf[3] = 0xff & (div);
919

920
	rc = i2c_send(priv, buf, sizeof(buf));
921
	if (rc < 0)
922
		goto ret;
923
924
	msleep(100);

925
	priv->frequency = freq;
926

927
928
929
	tuner_dbg("divisor= %02x %02x %02x %02x (freq=%d.%03d)\n",
	       buf[0], buf[1], buf[2], buf[3],
	       freq / 1000000, (freq % 1000000) / 1000);
930

931
	rc = 0;
932

933
934
ret:
	mutex_unlock(&priv->lock);
935

936
	return rc;
937
938
}

939
static int xc2028_set_analog_freq(struct dvb_frontend *fe,
940
			      struct analog_parameters *p)
941
{
942
	struct xc2028_data *priv = fe->tuner_priv;
943
944
945
	unsigned int       type=0;

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

947
948
949
950
951
	if (p->mode == V4L2_TUNER_RADIO) {
		type |= FM;
		if (priv->ctrl.input1)
			type |= INPUT1;
		return generic_set_freq(fe, (625l * p->frequency) / 10,
952
				T_ANALOG_TV, type, 0, 0);
953
954
	}

955
956
957
958
959
	/* 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 */
960
961
	if (!(p->std & V4L2_STD_MN))
		type |= F8MHZ;
962

963
964
	/* Add audio hack to std mask */
	p->std |= parse_audio_std_option();
965

Mauro Carvalho Chehab's avatar