Merge branch 'timers-fixes-for-linus' of git://git.kernel.org/pub/scm/linux/kernel...
[deliverable/linux.git] / drivers / media / common / tuners / tuner-xc2028.c
1 /* tuner-xc2028
2 *
3 * Copyright (c) 2007-2008 Mauro Carvalho Chehab (mchehab@infradead.org)
4 *
5 * Copyright (c) 2007 Michel Ludwig (michel.ludwig@gmail.com)
6 * - frontend interface
7 *
8 * This code is placed under the terms of the GNU General Public License v2
9 */
10
11 #include <linux/i2c.h>
12 #include <asm/div64.h>
13 #include <linux/firmware.h>
14 #include <linux/videodev2.h>
15 #include <linux/delay.h>
16 #include <media/tuner.h>
17 #include <linux/mutex.h>
18 #include <asm/unaligned.h>
19 #include "tuner-i2c.h"
20 #include "tuner-xc2028.h"
21 #include "tuner-xc2028-types.h"
22
23 #include <linux/dvb/frontend.h>
24 #include "dvb_frontend.h"
25
26
27 static int debug;
28 module_param(debug, int, 0644);
29 MODULE_PARM_DESC(debug, "enable verbose debug messages");
30
31 static char audio_std[8];
32 module_param_string(audio_std, audio_std, sizeof(audio_std), 0);
33 MODULE_PARM_DESC(audio_std,
34 "Audio standard. XC3028 audio decoder explicitly "
35 "needs to know what audio\n"
36 "standard is needed for some video standards with audio A2 or NICAM.\n"
37 "The valid values are:\n"
38 "A2\n"
39 "A2/A\n"
40 "A2/B\n"
41 "NICAM\n"
42 "NICAM/A\n"
43 "NICAM/B\n");
44
45 static char firmware_name[FIRMWARE_NAME_MAX];
46 module_param_string(firmware_name, firmware_name, sizeof(firmware_name), 0);
47 MODULE_PARM_DESC(firmware_name, "Firmware file name. Allows overriding the "
48 "default firmware name\n");
49
50 static LIST_HEAD(hybrid_tuner_instance_list);
51 static DEFINE_MUTEX(xc2028_list_mutex);
52
53 /* struct for storing firmware table */
54 struct firmware_description {
55 unsigned int type;
56 v4l2_std_id id;
57 __u16 int_freq;
58 unsigned char *ptr;
59 unsigned int size;
60 };
61
62 struct firmware_properties {
63 unsigned int type;
64 v4l2_std_id id;
65 v4l2_std_id std_req;
66 __u16 int_freq;
67 unsigned int scode_table;
68 int scode_nr;
69 };
70
71 struct xc2028_data {
72 struct list_head hybrid_tuner_instance_list;
73 struct tuner_i2c_props i2c_props;
74 int (*tuner_callback) (void *dev,
75 int command, int arg);
76 void *video_dev;
77 __u32 frequency;
78
79 struct firmware_description *firm;
80 int firm_size;
81 __u16 firm_version;
82
83 __u16 hwmodel;
84 __u16 hwvers;
85
86 struct xc2028_ctrl ctrl;
87
88 struct firmware_properties cur_fw;
89
90 struct mutex lock;
91 };
92
93 #define i2c_send(priv, buf, size) ({ \
94 int _rc; \
95 _rc = tuner_i2c_xfer_send(&priv->i2c_props, buf, size); \
96 if (size != _rc) \
97 tuner_info("i2c output error: rc = %d (should be %d)\n",\
98 _rc, (int)size); \
99 _rc; \
100 })
101
102 #define i2c_rcv(priv, buf, size) ({ \
103 int _rc; \
104 _rc = tuner_i2c_xfer_recv(&priv->i2c_props, buf, size); \
105 if (size != _rc) \
106 tuner_err("i2c input error: rc = %d (should be %d)\n", \
107 _rc, (int)size); \
108 _rc; \
109 })
110
111 #define i2c_send_recv(priv, obuf, osize, ibuf, isize) ({ \
112 int _rc; \
113 _rc = tuner_i2c_xfer_send_recv(&priv->i2c_props, obuf, osize, \
114 ibuf, isize); \
115 if (isize != _rc) \
116 tuner_err("i2c input error: rc = %d (should be %d)\n", \
117 _rc, (int)isize); \
118 _rc; \
119 })
120
121 #define send_seq(priv, data...) ({ \
122 static u8 _val[] = data; \
123 int _rc; \
124 if (sizeof(_val) != \
125 (_rc = tuner_i2c_xfer_send(&priv->i2c_props, \
126 _val, sizeof(_val)))) { \
127 tuner_err("Error on line %d: %d\n", __LINE__, _rc); \
128 } else \
129 msleep(10); \
130 _rc; \
131 })
132
133 static int xc2028_get_reg(struct xc2028_data *priv, u16 reg, u16 *val)
134 {
135 unsigned char buf[2];
136 unsigned char ibuf[2];
137
138 tuner_dbg("%s %04x called\n", __func__, reg);
139
140 buf[0] = reg >> 8;
141 buf[1] = (unsigned char) reg;
142
143 if (i2c_send_recv(priv, buf, 2, ibuf, 2) != 2)
144 return -EIO;
145
146 *val = (ibuf[1]) | (ibuf[0] << 8);
147 return 0;
148 }
149
150 #define dump_firm_type(t) dump_firm_type_and_int_freq(t, 0)
151 static void dump_firm_type_and_int_freq(unsigned int type, u16 int_freq)
152 {
153 if (type & BASE)
154 printk("BASE ");
155 if (type & INIT1)
156 printk("INIT1 ");
157 if (type & F8MHZ)
158 printk("F8MHZ ");
159 if (type & MTS)
160 printk("MTS ");
161 if (type & D2620)
162 printk("D2620 ");
163 if (type & D2633)
164 printk("D2633 ");
165 if (type & DTV6)
166 printk("DTV6 ");
167 if (type & QAM)
168 printk("QAM ");
169 if (type & DTV7)
170 printk("DTV7 ");
171 if (type & DTV78)
172 printk("DTV78 ");
173 if (type & DTV8)
174 printk("DTV8 ");
175 if (type & FM)
176 printk("FM ");
177 if (type & INPUT1)
178 printk("INPUT1 ");
179 if (type & LCD)
180 printk("LCD ");
181 if (type & NOGD)
182 printk("NOGD ");
183 if (type & MONO)
184 printk("MONO ");
185 if (type & ATSC)
186 printk("ATSC ");
187 if (type & IF)
188 printk("IF ");
189 if (type & LG60)
190 printk("LG60 ");
191 if (type & ATI638)
192 printk("ATI638 ");
193 if (type & OREN538)
194 printk("OREN538 ");
195 if (type & OREN36)
196 printk("OREN36 ");
197 if (type & TOYOTA388)
198 printk("TOYOTA388 ");
199 if (type & TOYOTA794)
200 printk("TOYOTA794 ");
201 if (type & DIBCOM52)
202 printk("DIBCOM52 ");
203 if (type & ZARLINK456)
204 printk("ZARLINK456 ");
205 if (type & CHINA)
206 printk("CHINA ");
207 if (type & F6MHZ)
208 printk("F6MHZ ");
209 if (type & INPUT2)
210 printk("INPUT2 ");
211 if (type & SCODE)
212 printk("SCODE ");
213 if (type & HAS_IF)
214 printk("HAS_IF_%d ", int_freq);
215 }
216
217 static v4l2_std_id parse_audio_std_option(void)
218 {
219 if (strcasecmp(audio_std, "A2") == 0)
220 return V4L2_STD_A2;
221 if (strcasecmp(audio_std, "A2/A") == 0)
222 return V4L2_STD_A2_A;
223 if (strcasecmp(audio_std, "A2/B") == 0)
224 return V4L2_STD_A2_B;
225 if (strcasecmp(audio_std, "NICAM") == 0)
226 return V4L2_STD_NICAM;
227 if (strcasecmp(audio_std, "NICAM/A") == 0)
228 return V4L2_STD_NICAM_A;
229 if (strcasecmp(audio_std, "NICAM/B") == 0)
230 return V4L2_STD_NICAM_B;
231
232 return 0;
233 }
234
235 static void free_firmware(struct xc2028_data *priv)
236 {
237 int i;
238 tuner_dbg("%s called\n", __func__);
239
240 if (!priv->firm)
241 return;
242
243 for (i = 0; i < priv->firm_size; i++)
244 kfree(priv->firm[i].ptr);
245
246 kfree(priv->firm);
247
248 priv->firm = NULL;
249 priv->firm_size = 0;
250
251 memset(&priv->cur_fw, 0, sizeof(priv->cur_fw));
252 }
253
254 static int load_all_firmwares(struct dvb_frontend *fe)
255 {
256 struct xc2028_data *priv = fe->tuner_priv;
257 const struct firmware *fw = NULL;
258 const unsigned char *p, *endp;
259 int rc = 0;
260 int n, n_array;
261 char name[33];
262 char *fname;
263
264 tuner_dbg("%s called\n", __func__);
265
266 if (!firmware_name[0])
267 fname = priv->ctrl.fname;
268 else
269 fname = firmware_name;
270
271 tuner_dbg("Reading firmware %s\n", fname);
272 rc = request_firmware(&fw, fname, &priv->i2c_props.adap->dev);
273 if (rc < 0) {
274 if (rc == -ENOENT)
275 tuner_err("Error: firmware %s not found.\n",
276 fname);
277 else
278 tuner_err("Error %d while requesting firmware %s \n",
279 rc, fname);
280
281 return rc;
282 }
283 p = fw->data;
284 endp = p + fw->size;
285
286 if (fw->size < sizeof(name) - 1 + 2 + 2) {
287 tuner_err("Error: firmware file %s has invalid size!\n",
288 fname);
289 goto corrupt;
290 }
291
292 memcpy(name, p, sizeof(name) - 1);
293 name[sizeof(name) - 1] = 0;
294 p += sizeof(name) - 1;
295
296 priv->firm_version = get_unaligned_le16(p);
297 p += 2;
298
299 n_array = get_unaligned_le16(p);
300 p += 2;
301
302 tuner_info("Loading %d firmware images from %s, type: %s, ver %d.%d\n",
303 n_array, fname, name,
304 priv->firm_version >> 8, priv->firm_version & 0xff);
305
306 priv->firm = kzalloc(sizeof(*priv->firm) * n_array, GFP_KERNEL);
307 if (priv->firm == NULL) {
308 tuner_err("Not enough memory to load firmware file.\n");
309 rc = -ENOMEM;
310 goto err;
311 }
312 priv->firm_size = n_array;
313
314 n = -1;
315 while (p < endp) {
316 __u32 type, size;
317 v4l2_std_id id;
318 __u16 int_freq = 0;
319
320 n++;
321 if (n >= n_array) {
322 tuner_err("More firmware images in file than "
323 "were expected!\n");
324 goto corrupt;
325 }
326
327 /* Checks if there's enough bytes to read */
328 if (endp - p < sizeof(type) + sizeof(id) + sizeof(size))
329 goto header;
330
331 type = get_unaligned_le32(p);
332 p += sizeof(type);
333
334 id = get_unaligned_le64(p);
335 p += sizeof(id);
336
337 if (type & HAS_IF) {
338 int_freq = get_unaligned_le16(p);
339 p += sizeof(int_freq);
340 if (endp - p < sizeof(size))
341 goto header;
342 }
343
344 size = get_unaligned_le32(p);
345 p += sizeof(size);
346
347 if (!size || size > endp - p) {
348 tuner_err("Firmware type ");
349 dump_firm_type(type);
350 printk("(%x), id %llx is corrupted "
351 "(size=%d, expected %d)\n",
352 type, (unsigned long long)id,
353 (unsigned)(endp - p), size);
354 goto corrupt;
355 }
356
357 priv->firm[n].ptr = kzalloc(size, GFP_KERNEL);
358 if (priv->firm[n].ptr == NULL) {
359 tuner_err("Not enough memory to load firmware file.\n");
360 rc = -ENOMEM;
361 goto err;
362 }
363 tuner_dbg("Reading firmware type ");
364 if (debug) {
365 dump_firm_type_and_int_freq(type, int_freq);
366 printk("(%x), id %llx, size=%d.\n",
367 type, (unsigned long long)id, size);
368 }
369
370 memcpy(priv->firm[n].ptr, p, size);
371 priv->firm[n].type = type;
372 priv->firm[n].id = id;
373 priv->firm[n].size = size;
374 priv->firm[n].int_freq = int_freq;
375
376 p += size;
377 }
378
379 if (n + 1 != priv->firm_size) {
380 tuner_err("Firmware file is incomplete!\n");
381 goto corrupt;
382 }
383
384 goto done;
385
386 header:
387 tuner_err("Firmware header is incomplete!\n");
388 corrupt:
389 rc = -EINVAL;
390 tuner_err("Error: firmware file is corrupted!\n");
391
392 err:
393 tuner_info("Releasing partially loaded firmware file.\n");
394 free_firmware(priv);
395
396 done:
397 release_firmware(fw);
398 if (rc == 0)
399 tuner_dbg("Firmware files loaded.\n");
400
401 return rc;
402 }
403
404 static int seek_firmware(struct dvb_frontend *fe, unsigned int type,
405 v4l2_std_id *id)
406 {
407 struct xc2028_data *priv = fe->tuner_priv;
408 int i, best_i = -1, best_nr_matches = 0;
409 unsigned int type_mask = 0;
410
411 tuner_dbg("%s called, want type=", __func__);
412 if (debug) {
413 dump_firm_type(type);
414 printk("(%x), id %016llx.\n", type, (unsigned long long)*id);
415 }
416
417 if (!priv->firm) {
418 tuner_err("Error! firmware not loaded\n");
419 return -EINVAL;
420 }
421
422 if (((type & ~SCODE) == 0) && (*id == 0))
423 *id = V4L2_STD_PAL;
424
425 if (type & BASE)
426 type_mask = BASE_TYPES;
427 else if (type & SCODE) {
428 type &= SCODE_TYPES;
429 type_mask = SCODE_TYPES & ~HAS_IF;
430 } else if (type & DTV_TYPES)
431 type_mask = DTV_TYPES;
432 else if (type & STD_SPECIFIC_TYPES)
433 type_mask = STD_SPECIFIC_TYPES;
434
435 type &= type_mask;
436
437 if (!(type & SCODE))
438 type_mask = ~0;
439
440 /* Seek for exact match */
441 for (i = 0; i < priv->firm_size; i++) {
442 if ((type == (priv->firm[i].type & type_mask)) &&
443 (*id == priv->firm[i].id))
444 goto found;
445 }
446
447 /* Seek for generic video standard match */
448 for (i = 0; i < priv->firm_size; i++) {
449 v4l2_std_id match_mask;
450 int nr_matches;
451
452 if (type != (priv->firm[i].type & type_mask))
453 continue;
454
455 match_mask = *id & priv->firm[i].id;
456 if (!match_mask)
457 continue;
458
459 if ((*id & match_mask) == *id)
460 goto found; /* Supports all the requested standards */
461
462 nr_matches = hweight64(match_mask);
463 if (nr_matches > best_nr_matches) {
464 best_nr_matches = nr_matches;
465 best_i = i;
466 }
467 }
468
469 if (best_nr_matches > 0) {
470 tuner_dbg("Selecting best matching firmware (%d bits) for "
471 "type=", best_nr_matches);
472 dump_firm_type(type);
473 printk("(%x), id %016llx:\n", type, (unsigned long long)*id);
474 i = best_i;
475 goto found;
476 }
477
478 /*FIXME: Would make sense to seek for type "hint" match ? */
479
480 i = -ENOENT;
481 goto ret;
482
483 found:
484 *id = priv->firm[i].id;
485
486 ret:
487 tuner_dbg("%s firmware for type=", (i < 0) ? "Can't find" : "Found");
488 if (debug) {
489 dump_firm_type(type);
490 printk("(%x), id %016llx.\n", type, (unsigned long long)*id);
491 }
492 return i;
493 }
494
495 static int load_firmware(struct dvb_frontend *fe, unsigned int type,
496 v4l2_std_id *id)
497 {
498 struct xc2028_data *priv = fe->tuner_priv;
499 int pos, rc;
500 unsigned char *p, *endp, buf[priv->ctrl.max_len];
501
502 tuner_dbg("%s called\n", __func__);
503
504 pos = seek_firmware(fe, type, id);
505 if (pos < 0)
506 return pos;
507
508 tuner_info("Loading firmware for type=");
509 dump_firm_type(priv->firm[pos].type);
510 printk("(%x), id %016llx.\n", priv->firm[pos].type,
511 (unsigned long long)*id);
512
513 p = priv->firm[pos].ptr;
514 endp = p + priv->firm[pos].size;
515
516 while (p < endp) {
517 __u16 size;
518
519 /* Checks if there's enough bytes to read */
520 if (p + sizeof(size) > endp) {
521 tuner_err("Firmware chunk size is wrong\n");
522 return -EINVAL;
523 }
524
525 size = le16_to_cpu(*(__u16 *) p);
526 p += sizeof(size);
527
528 if (size == 0xffff)
529 return 0;
530
531 if (!size) {
532 /* Special callback command received */
533 rc = priv->tuner_callback(priv->video_dev,
534 XC2028_TUNER_RESET, 0);
535 if (rc < 0) {
536 tuner_err("Error at RESET code %d\n",
537 (*p) & 0x7f);
538 return -EINVAL;
539 }
540 continue;
541 }
542 if (size >= 0xff00) {
543 switch (size) {
544 case 0xff00:
545 rc = priv->tuner_callback(priv->video_dev,
546 XC2028_RESET_CLK, 0);
547 if (rc < 0) {
548 tuner_err("Error at RESET code %d\n",
549 (*p) & 0x7f);
550 return -EINVAL;
551 }
552 break;
553 default:
554 tuner_info("Invalid RESET code %d\n",
555 size & 0x7f);
556 return -EINVAL;
557
558 }
559 continue;
560 }
561
562 /* Checks for a sleep command */
563 if (size & 0x8000) {
564 msleep(size & 0x7fff);
565 continue;
566 }
567
568 if ((size + p > endp)) {
569 tuner_err("missing bytes: need %d, have %d\n",
570 size, (int)(endp - p));
571 return -EINVAL;
572 }
573
574 buf[0] = *p;
575 p++;
576 size--;
577
578 /* Sends message chunks */
579 while (size > 0) {
580 int len = (size < priv->ctrl.max_len - 1) ?
581 size : priv->ctrl.max_len - 1;
582
583 memcpy(buf + 1, p, len);
584
585 rc = i2c_send(priv, buf, len + 1);
586 if (rc < 0) {
587 tuner_err("%d returned from send\n", rc);
588 return -EINVAL;
589 }
590
591 p += len;
592 size -= len;
593 }
594 }
595 return 0;
596 }
597
598 static int load_scode(struct dvb_frontend *fe, unsigned int type,
599 v4l2_std_id *id, __u16 int_freq, int scode)
600 {
601 struct xc2028_data *priv = fe->tuner_priv;
602 int pos, rc;
603 unsigned char *p;
604
605 tuner_dbg("%s called\n", __func__);
606
607 if (!int_freq) {
608 pos = seek_firmware(fe, type, id);
609 if (pos < 0)
610 return pos;
611 } else {
612 for (pos = 0; pos < priv->firm_size; pos++) {
613 if ((priv->firm[pos].int_freq == int_freq) &&
614 (priv->firm[pos].type & HAS_IF))
615 break;
616 }
617 if (pos == priv->firm_size)
618 return -ENOENT;
619 }
620
621 p = priv->firm[pos].ptr;
622
623 if (priv->firm[pos].type & HAS_IF) {
624 if (priv->firm[pos].size != 12 * 16 || scode >= 16)
625 return -EINVAL;
626 p += 12 * scode;
627 } else {
628 /* 16 SCODE entries per file; each SCODE entry is 12 bytes and
629 * has a 2-byte size header in the firmware format. */
630 if (priv->firm[pos].size != 14 * 16 || scode >= 16 ||
631 le16_to_cpu(*(__u16 *)(p + 14 * scode)) != 12)
632 return -EINVAL;
633 p += 14 * scode + 2;
634 }
635
636 tuner_info("Loading SCODE for type=");
637 dump_firm_type_and_int_freq(priv->firm[pos].type,
638 priv->firm[pos].int_freq);
639 printk("(%x), id %016llx.\n", priv->firm[pos].type,
640 (unsigned long long)*id);
641
642 if (priv->firm_version < 0x0202)
643 rc = send_seq(priv, {0x20, 0x00, 0x00, 0x00});
644 else
645 rc = send_seq(priv, {0xa0, 0x00, 0x00, 0x00});
646 if (rc < 0)
647 return -EIO;
648
649 rc = i2c_send(priv, p, 12);
650 if (rc < 0)
651 return -EIO;
652
653 rc = send_seq(priv, {0x00, 0x8c});
654 if (rc < 0)
655 return -EIO;
656
657 return 0;
658 }
659
660 static int check_firmware(struct dvb_frontend *fe, unsigned int type,
661 v4l2_std_id std, __u16 int_freq)
662 {
663 struct xc2028_data *priv = fe->tuner_priv;
664 struct firmware_properties new_fw;
665 int rc = 0, is_retry = 0;
666 u16 version, hwmodel;
667 v4l2_std_id std0;
668
669 tuner_dbg("%s called\n", __func__);
670
671 if (!priv->firm) {
672 if (!priv->ctrl.fname) {
673 tuner_info("xc2028/3028 firmware name not set!\n");
674 return -EINVAL;
675 }
676
677 rc = load_all_firmwares(fe);
678 if (rc < 0)
679 return rc;
680 }
681
682 if (priv->ctrl.mts && !(type & FM))
683 type |= MTS;
684
685 retry:
686 new_fw.type = type;
687 new_fw.id = std;
688 new_fw.std_req = std;
689 new_fw.scode_table = SCODE | priv->ctrl.scode_table;
690 new_fw.scode_nr = 0;
691 new_fw.int_freq = int_freq;
692
693 tuner_dbg("checking firmware, user requested type=");
694 if (debug) {
695 dump_firm_type(new_fw.type);
696 printk("(%x), id %016llx, ", new_fw.type,
697 (unsigned long long)new_fw.std_req);
698 if (!int_freq) {
699 printk("scode_tbl ");
700 dump_firm_type(priv->ctrl.scode_table);
701 printk("(%x), ", priv->ctrl.scode_table);
702 } else
703 printk("int_freq %d, ", new_fw.int_freq);
704 printk("scode_nr %d\n", new_fw.scode_nr);
705 }
706
707 /* No need to reload base firmware if it matches */
708 if (((BASE | new_fw.type) & BASE_TYPES) ==
709 (priv->cur_fw.type & BASE_TYPES)) {
710 tuner_dbg("BASE firmware not changed.\n");
711 goto skip_base;
712 }
713
714 /* Updating BASE - forget about all currently loaded firmware */
715 memset(&priv->cur_fw, 0, sizeof(priv->cur_fw));
716
717 /* Reset is needed before loading firmware */
718 rc = priv->tuner_callback(priv->video_dev,
719 XC2028_TUNER_RESET, 0);
720 if (rc < 0)
721 goto fail;
722
723 /* BASE firmwares are all std0 */
724 std0 = 0;
725 rc = load_firmware(fe, BASE | new_fw.type, &std0);
726 if (rc < 0) {
727 tuner_err("Error %d while loading base firmware\n",
728 rc);
729 goto fail;
730 }
731
732 /* Load INIT1, if needed */
733 tuner_dbg("Load init1 firmware, if exists\n");
734
735 rc = load_firmware(fe, BASE | INIT1 | new_fw.type, &std0);
736 if (rc == -ENOENT)
737 rc = load_firmware(fe, (BASE | INIT1 | new_fw.type) & ~F8MHZ,
738 &std0);
739 if (rc < 0 && rc != -ENOENT) {
740 tuner_err("Error %d while loading init1 firmware\n",
741 rc);
742 goto fail;
743 }
744
745 skip_base:
746 /*
747 * No need to reload standard specific firmware if base firmware
748 * was not reloaded and requested video standards have not changed.
749 */
750 if (priv->cur_fw.type == (BASE | new_fw.type) &&
751 priv->cur_fw.std_req == std) {
752 tuner_dbg("Std-specific firmware already loaded.\n");
753 goto skip_std_specific;
754 }
755
756 /* Reloading std-specific firmware forces a SCODE update */
757 priv->cur_fw.scode_table = 0;
758
759 rc = load_firmware(fe, new_fw.type, &new_fw.id);
760 if (rc == -ENOENT)
761 rc = load_firmware(fe, new_fw.type & ~F8MHZ, &new_fw.id);
762
763 if (rc < 0)
764 goto fail;
765
766 skip_std_specific:
767 if (priv->cur_fw.scode_table == new_fw.scode_table &&
768 priv->cur_fw.scode_nr == new_fw.scode_nr) {
769 tuner_dbg("SCODE firmware already loaded.\n");
770 goto check_device;
771 }
772
773 if (new_fw.type & FM)
774 goto check_device;
775
776 /* Load SCODE firmware, if exists */
777 tuner_dbg("Trying to load scode %d\n", new_fw.scode_nr);
778
779 rc = load_scode(fe, new_fw.type | new_fw.scode_table, &new_fw.id,
780 new_fw.int_freq, new_fw.scode_nr);
781
782 check_device:
783 if (xc2028_get_reg(priv, 0x0004, &version) < 0 ||
784 xc2028_get_reg(priv, 0x0008, &hwmodel) < 0) {
785 tuner_err("Unable to read tuner registers.\n");
786 goto fail;
787 }
788
789 tuner_dbg("Device is Xceive %d version %d.%d, "
790 "firmware version %d.%d\n",
791 hwmodel, (version & 0xf000) >> 12, (version & 0xf00) >> 8,
792 (version & 0xf0) >> 4, version & 0xf);
793
794 /* Check firmware version against what we downloaded. */
795 if (priv->firm_version != ((version & 0xf0) << 4 | (version & 0x0f))) {
796 tuner_err("Incorrect readback of firmware version.\n");
797 goto fail;
798 }
799
800 /* Check that the tuner hardware model remains consistent over time. */
801 if (priv->hwmodel == 0 && (hwmodel == 2028 || hwmodel == 3028)) {
802 priv->hwmodel = hwmodel;
803 priv->hwvers = version & 0xff00;
804 } else if (priv->hwmodel == 0 || priv->hwmodel != hwmodel ||
805 priv->hwvers != (version & 0xff00)) {
806 tuner_err("Read invalid device hardware information - tuner "
807 "hung?\n");
808 goto fail;
809 }
810
811 memcpy(&priv->cur_fw, &new_fw, sizeof(priv->cur_fw));
812
813 /*
814 * By setting BASE in cur_fw.type only after successfully loading all
815 * firmwares, we can:
816 * 1. Identify that BASE firmware with type=0 has been loaded;
817 * 2. Tell whether BASE firmware was just changed the next time through.
818 */
819 priv->cur_fw.type |= BASE;
820
821 return 0;
822
823 fail:
824 memset(&priv->cur_fw, 0, sizeof(priv->cur_fw));
825 if (!is_retry) {
826 msleep(50);
827 is_retry = 1;
828 tuner_dbg("Retrying firmware load\n");
829 goto retry;
830 }
831
832 if (rc == -ENOENT)
833 rc = -EINVAL;
834 return rc;
835 }
836
837 static int xc2028_signal(struct dvb_frontend *fe, u16 *strength)
838 {
839 struct xc2028_data *priv = fe->tuner_priv;
840 u16 frq_lock, signal = 0;
841 int rc;
842
843 tuner_dbg("%s called\n", __func__);
844
845 mutex_lock(&priv->lock);
846
847 /* Sync Lock Indicator */
848 rc = xc2028_get_reg(priv, 0x0002, &frq_lock);
849 if (rc < 0)
850 goto ret;
851
852 /* Frequency is locked */
853 if (frq_lock == 1)
854 signal = 32768;
855
856 /* Get SNR of the video signal */
857 rc = xc2028_get_reg(priv, 0x0040, &signal);
858 if (rc < 0)
859 goto ret;
860
861 /* Use both frq_lock and signal to generate the result */
862 signal = signal || ((signal & 0x07) << 12);
863
864 ret:
865 mutex_unlock(&priv->lock);
866
867 *strength = signal;
868
869 tuner_dbg("signal strength is %d\n", signal);
870
871 return rc;
872 }
873
874 #define DIV 15625
875
876 static int generic_set_freq(struct dvb_frontend *fe, u32 freq /* in HZ */,
877 enum tuner_mode new_mode,
878 unsigned int type,
879 v4l2_std_id std,
880 u16 int_freq)
881 {
882 struct xc2028_data *priv = fe->tuner_priv;
883 int rc = -EINVAL;
884 unsigned char buf[4];
885 u32 div, offset = 0;
886
887 tuner_dbg("%s called\n", __func__);
888
889 mutex_lock(&priv->lock);
890
891 tuner_dbg("should set frequency %d kHz\n", freq / 1000);
892
893 if (check_firmware(fe, type, std, int_freq) < 0)
894 goto ret;
895
896 /* On some cases xc2028 can disable video output, if
897 * very weak signals are received. By sending a soft
898 * reset, this is re-enabled. So, it is better to always
899 * send a soft reset before changing channels, to be sure
900 * that xc2028 will be in a safe state.
901 * Maybe this might also be needed for DTV.
902 */
903 if (new_mode == T_ANALOG_TV) {
904 rc = send_seq(priv, {0x00, 0x00});
905 } else if (priv->cur_fw.type & ATSC) {
906 offset = 1750000;
907 } else {
908 offset = 2750000;
909 /*
910 * We must adjust the offset by 500kHz in two cases in order
911 * to correctly center the IF output:
912 * 1) When the ZARLINK456 or DIBCOM52 tables were explicitly
913 * selected and a 7MHz channel is tuned;
914 * 2) When tuning a VHF channel with DTV78 firmware.
915 */
916 if (((priv->cur_fw.type & DTV7) &&
917 (priv->cur_fw.scode_table & (ZARLINK456 | DIBCOM52))) ||
918 ((priv->cur_fw.type & DTV78) && freq < 470000000))
919 offset -= 500000;
920 }
921
922 div = (freq - offset + DIV / 2) / DIV;
923
924 /* CMD= Set frequency */
925 if (priv->firm_version < 0x0202)
926 rc = send_seq(priv, {0x00, 0x02, 0x00, 0x00});
927 else
928 rc = send_seq(priv, {0x80, 0x02, 0x00, 0x00});
929 if (rc < 0)
930 goto ret;
931
932 /* Return code shouldn't be checked.
933 The reset CLK is needed only with tm6000.
934 Driver should work fine even if this fails.
935 */
936 priv->tuner_callback(priv->video_dev, XC2028_RESET_CLK, 1);
937
938 msleep(10);
939
940 buf[0] = 0xff & (div >> 24);
941 buf[1] = 0xff & (div >> 16);
942 buf[2] = 0xff & (div >> 8);
943 buf[3] = 0xff & (div);
944
945 rc = i2c_send(priv, buf, sizeof(buf));
946 if (rc < 0)
947 goto ret;
948 msleep(100);
949
950 priv->frequency = freq;
951
952 tuner_dbg("divisor= %02x %02x %02x %02x (freq=%d.%03d)\n",
953 buf[0], buf[1], buf[2], buf[3],
954 freq / 1000000, (freq % 1000000) / 1000);
955
956 rc = 0;
957
958 ret:
959 mutex_unlock(&priv->lock);
960
961 return rc;
962 }
963
964 static int xc2028_set_analog_freq(struct dvb_frontend *fe,
965 struct analog_parameters *p)
966 {
967 struct xc2028_data *priv = fe->tuner_priv;
968 unsigned int type=0;
969
970 tuner_dbg("%s called\n", __func__);
971
972 if (p->mode == V4L2_TUNER_RADIO) {
973 type |= FM;
974 if (priv->ctrl.input1)
975 type |= INPUT1;
976 return generic_set_freq(fe, (625l * p->frequency) / 10,
977 T_ANALOG_TV, type, 0, 0);
978 }
979
980 /* if std is not defined, choose one */
981 if (!p->std)
982 p->std = V4L2_STD_MN;
983
984 /* PAL/M, PAL/N, PAL/Nc and NTSC variants should use 6MHz firmware */
985 if (!(p->std & V4L2_STD_MN))
986 type |= F8MHZ;
987
988 /* Add audio hack to std mask */
989 p->std |= parse_audio_std_option();
990
991 return generic_set_freq(fe, 62500l * p->frequency,
992 T_ANALOG_TV, type, p->std, 0);
993 }
994
995 static int xc2028_set_params(struct dvb_frontend *fe,
996 struct dvb_frontend_parameters *p)
997 {
998 struct xc2028_data *priv = fe->tuner_priv;
999 unsigned int type=0;
1000 fe_bandwidth_t bw = BANDWIDTH_8_MHZ;
1001 u16 demod = 0;
1002
1003 tuner_dbg("%s called\n", __func__);
1004
1005 if (priv->ctrl.d2633)
1006 type |= D2633;
1007 else
1008 type |= D2620;
1009
1010 switch(fe->ops.info.type) {
1011 case FE_OFDM:
1012 bw = p->u.ofdm.bandwidth;
1013 break;
1014 case FE_QAM:
1015 tuner_info("WARN: There are some reports that "
1016 "QAM 6 MHz doesn't work.\n"
1017 "If this works for you, please report by "
1018 "e-mail to: v4l-dvb-maintainer@linuxtv.org\n");
1019 bw = BANDWIDTH_6_MHZ;
1020 type |= QAM;
1021 break;
1022 case FE_ATSC:
1023 bw = BANDWIDTH_6_MHZ;
1024 /* The only ATSC firmware (at least on v2.7) is D2633,
1025 so overrides ctrl->d2633 */
1026 type |= ATSC| D2633;
1027 type &= ~D2620;
1028 break;
1029 /* DVB-S is not supported */
1030 default:
1031 return -EINVAL;
1032 }
1033
1034 switch (bw) {
1035 case BANDWIDTH_8_MHZ:
1036 if (p->frequency < 470000000)
1037 priv->ctrl.vhfbw7 = 0;
1038 else
1039 priv->ctrl.uhfbw8 = 1;
1040 type |= (priv->ctrl.vhfbw7 && priv->ctrl.uhfbw8) ? DTV78 : DTV8;
1041 type |= F8MHZ;
1042 break;
1043 case BANDWIDTH_7_MHZ:
1044 if (p->frequency < 470000000)
1045 priv->ctrl.vhfbw7 = 1;
1046 else
1047 priv->ctrl.uhfbw8 = 0;
1048 type |= (priv->ctrl.vhfbw7 && priv->ctrl.uhfbw8) ? DTV78 : DTV7;
1049 type |= F8MHZ;
1050 break;
1051 case BANDWIDTH_6_MHZ:
1052 type |= DTV6;
1053 priv->ctrl.vhfbw7 = 0;
1054 priv->ctrl.uhfbw8 = 0;
1055 break;
1056 default:
1057 tuner_err("error: bandwidth not supported.\n");
1058 };
1059
1060 /* All S-code tables need a 200kHz shift */
1061 if (priv->ctrl.demod)
1062 demod = priv->ctrl.demod + 200;
1063
1064 return generic_set_freq(fe, p->frequency,
1065 T_DIGITAL_TV, type, 0, demod);
1066 }
1067
1068
1069 static int xc2028_dvb_release(struct dvb_frontend *fe)
1070 {
1071 struct xc2028_data *priv = fe->tuner_priv;
1072
1073 tuner_dbg("%s called\n", __func__);
1074
1075 mutex_lock(&xc2028_list_mutex);
1076
1077 /* only perform final cleanup if this is the last instance */
1078 if (hybrid_tuner_report_instance_count(priv) == 1) {
1079 kfree(priv->ctrl.fname);
1080 free_firmware(priv);
1081 }
1082
1083 if (priv)
1084 hybrid_tuner_release_state(priv);
1085
1086 mutex_unlock(&xc2028_list_mutex);
1087
1088 fe->tuner_priv = NULL;
1089
1090 return 0;
1091 }
1092
1093 static int xc2028_get_frequency(struct dvb_frontend *fe, u32 *frequency)
1094 {
1095 struct xc2028_data *priv = fe->tuner_priv;
1096
1097 tuner_dbg("%s called\n", __func__);
1098
1099 *frequency = priv->frequency;
1100
1101 return 0;
1102 }
1103
1104 static int xc2028_set_config(struct dvb_frontend *fe, void *priv_cfg)
1105 {
1106 struct xc2028_data *priv = fe->tuner_priv;
1107 struct xc2028_ctrl *p = priv_cfg;
1108 int rc = 0;
1109
1110 tuner_dbg("%s called\n", __func__);
1111
1112 mutex_lock(&priv->lock);
1113
1114 memcpy(&priv->ctrl, p, sizeof(priv->ctrl));
1115 if (priv->ctrl.max_len < 9)
1116 priv->ctrl.max_len = 13;
1117
1118 if (p->fname) {
1119 if (priv->ctrl.fname && strcmp(p->fname, priv->ctrl.fname)) {
1120 kfree(priv->ctrl.fname);
1121 free_firmware(priv);
1122 }
1123
1124 priv->ctrl.fname = kstrdup(p->fname, GFP_KERNEL);
1125 if (priv->ctrl.fname == NULL)
1126 rc = -ENOMEM;
1127 }
1128
1129 mutex_unlock(&priv->lock);
1130
1131 return rc;
1132 }
1133
1134 static const struct dvb_tuner_ops xc2028_dvb_tuner_ops = {
1135 .info = {
1136 .name = "Xceive XC3028",
1137 .frequency_min = 42000000,
1138 .frequency_max = 864000000,
1139 .frequency_step = 50000,
1140 },
1141
1142 .set_config = xc2028_set_config,
1143 .set_analog_params = xc2028_set_analog_freq,
1144 .release = xc2028_dvb_release,
1145 .get_frequency = xc2028_get_frequency,
1146 .get_rf_strength = xc2028_signal,
1147 .set_params = xc2028_set_params,
1148 };
1149
1150 struct dvb_frontend *xc2028_attach(struct dvb_frontend *fe,
1151 struct xc2028_config *cfg)
1152 {
1153 struct xc2028_data *priv;
1154 int instance;
1155
1156 if (debug)
1157 printk(KERN_DEBUG "xc2028: Xcv2028/3028 init called!\n");
1158
1159 if (NULL == cfg)
1160 return NULL;
1161
1162 if (!fe) {
1163 printk(KERN_ERR "xc2028: No frontend!\n");
1164 return NULL;
1165 }
1166
1167 mutex_lock(&xc2028_list_mutex);
1168
1169 instance = hybrid_tuner_request_state(struct xc2028_data, priv,
1170 hybrid_tuner_instance_list,
1171 cfg->i2c_adap, cfg->i2c_addr,
1172 "xc2028");
1173 switch (instance) {
1174 case 0:
1175 /* memory allocation failure */
1176 goto fail;
1177 break;
1178 case 1:
1179 /* new tuner instance */
1180 priv->tuner_callback = cfg->callback;
1181 priv->ctrl.max_len = 13;
1182
1183 mutex_init(&priv->lock);
1184
1185 /* analog side (tuner-core) uses i2c_adap->algo_data.
1186 * digital side is not guaranteed to have algo_data defined.
1187 *
1188 * digital side will always have fe->dvb defined.
1189 * analog side (tuner-core) doesn't (yet) define fe->dvb.
1190 */
1191 priv->video_dev = ((fe->dvb) && (fe->dvb->priv)) ?
1192 fe->dvb->priv : cfg->i2c_adap->algo_data;
1193
1194 fe->tuner_priv = priv;
1195 break;
1196 case 2:
1197 /* existing tuner instance */
1198 fe->tuner_priv = priv;
1199 break;
1200 }
1201
1202 memcpy(&fe->ops.tuner_ops, &xc2028_dvb_tuner_ops,
1203 sizeof(xc2028_dvb_tuner_ops));
1204
1205 tuner_info("type set to %s\n", "XCeive xc2028/xc3028 tuner");
1206
1207 if (cfg->ctrl)
1208 xc2028_set_config(fe, cfg->ctrl);
1209
1210 mutex_unlock(&xc2028_list_mutex);
1211
1212 return fe;
1213 fail:
1214 mutex_unlock(&xc2028_list_mutex);
1215
1216 xc2028_dvb_release(fe);
1217 return NULL;
1218 }
1219
1220 EXPORT_SYMBOL(xc2028_attach);
1221
1222 MODULE_DESCRIPTION("Xceive xc2028/xc3028 tuner driver");
1223 MODULE_AUTHOR("Michel Ludwig <michel.ludwig@gmail.com>");
1224 MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@infradead.org>");
1225 MODULE_LICENSE("GPL");
This page took 0.05607 seconds and 6 git commands to generate.