]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - drivers/media/video/tuner-xc2028.c
0e580bcd0e09d27864f9f8873a5aa8a580cd30a8
[linux-2.6-omap-h63xx.git] / drivers / media / video / 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 "tuner-i2c.h"
19 #include "tuner-xc2028.h"
20 #include "tuner-xc2028-types.h"
21
22 #include <linux/dvb/frontend.h>
23 #include "dvb_frontend.h"
24
25
26 static int debug;
27 module_param(debug, int, 0644);
28 MODULE_PARM_DESC(debug, "enable verbose debug messages");
29
30 static char audio_std[8];
31 module_param_string(audio_std, audio_std, sizeof(audio_std), 0);
32 MODULE_PARM_DESC(audio_std,
33         "Audio standard. XC3028 audio decoder explicitly "
34         "needs to know what audio\n"
35         "standard is needed for some video standards with audio A2 or NICAM.\n"
36         "The valid values are:\n"
37         "A2\n"
38         "A2/A\n"
39         "A2/B\n"
40         "NICAM\n"
41         "NICAM/A\n"
42         "NICAM/B\n");
43
44 static char firmware_name[FIRMWARE_NAME_MAX];
45 module_param_string(firmware_name, firmware_name, sizeof(firmware_name), 0);
46 MODULE_PARM_DESC(firmware_name, "Firmware file name. Allows overriding the "
47                                 "default firmware name\n");
48
49 static LIST_HEAD(xc2028_list);
50 static DEFINE_MUTEX(xc2028_list_mutex);
51
52 /* struct for storing firmware table */
53 struct firmware_description {
54         unsigned int  type;
55         v4l2_std_id   id;
56         __u16         int_freq;
57         unsigned char *ptr;
58         unsigned int  size;
59 };
60
61 struct firmware_properties {
62         unsigned int    type;
63         v4l2_std_id     id;
64         v4l2_std_id     std_req;
65         __u16           int_freq;
66         unsigned int    scode_table;
67         int             scode_nr;
68 };
69
70 struct xc2028_data {
71         struct list_head        xc2028_list;
72         struct tuner_i2c_props  i2c_props;
73         int                     (*tuner_callback) (void *dev,
74                                                    int command, int arg);
75         void                    *video_dev;
76         int                     count;
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         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 = le16_to_cpu(*(__u16 *) p);
297         p += 2;
298
299         n_array = le16_to_cpu(*(__u16 *) 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 (p + sizeof(type) + sizeof(id) + sizeof(size) > endp) {
329                         tuner_err("Firmware header is incomplete!\n");
330                         goto corrupt;
331                 }
332
333                 type = le32_to_cpu(*(__u32 *) p);
334                 p += sizeof(type);
335
336                 id = le64_to_cpu(*(v4l2_std_id *) p);
337                 p += sizeof(id);
338
339                 if (type & HAS_IF) {
340                         int_freq = le16_to_cpu(*(__u16 *) p);
341                         p += sizeof(int_freq);
342                 }
343
344                 size = le32_to_cpu(*(__u32 *) p);
345                 p += sizeof(size);
346
347                 if ((!size) || (size + p > endp)) {
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 corrupt:
387         rc = -EINVAL;
388         tuner_err("Error: firmware file is corrupted!\n");
389
390 err:
391         tuner_info("Releasing partially loaded firmware file.\n");
392         free_firmware(priv);
393
394 done:
395         release_firmware(fw);
396         if (rc == 0)
397                 tuner_dbg("Firmware files loaded.\n");
398
399         return rc;
400 }
401
402 static int seek_firmware(struct dvb_frontend *fe, unsigned int type,
403                          v4l2_std_id *id)
404 {
405         struct xc2028_data *priv = fe->tuner_priv;
406         int                 i, best_i = -1, best_nr_matches = 0;
407         unsigned int        type_mask = 0;
408
409         tuner_dbg("%s called, want type=", __func__);
410         if (debug) {
411                 dump_firm_type(type);
412                 printk("(%x), id %016llx.\n", type, (unsigned long long)*id);
413         }
414
415         if (!priv->firm) {
416                 tuner_err("Error! firmware not loaded\n");
417                 return -EINVAL;
418         }
419
420         if (((type & ~SCODE) == 0) && (*id == 0))
421                 *id = V4L2_STD_PAL;
422
423         if (type & BASE)
424                 type_mask = BASE_TYPES;
425         else if (type & SCODE) {
426                 type &= SCODE_TYPES;
427                 type_mask = SCODE_TYPES & ~HAS_IF;
428         } else if (type & DTV_TYPES)
429                 type_mask = DTV_TYPES;
430         else if (type & STD_SPECIFIC_TYPES)
431                 type_mask = STD_SPECIFIC_TYPES;
432
433         type &= type_mask;
434
435         if (!type & SCODE)
436                 type_mask = ~0;
437
438         /* Seek for exact match */
439         for (i = 0; i < priv->firm_size; i++) {
440                 if ((type == (priv->firm[i].type & type_mask)) &&
441                     (*id == priv->firm[i].id))
442                         goto found;
443         }
444
445         /* Seek for generic video standard match */
446         for (i = 0; i < priv->firm_size; i++) {
447                 v4l2_std_id match_mask;
448                 int nr_matches;
449
450                 if (type != (priv->firm[i].type & type_mask))
451                         continue;
452
453                 match_mask = *id & priv->firm[i].id;
454                 if (!match_mask)
455                         continue;
456
457                 if ((*id & match_mask) == *id)
458                         goto found; /* Supports all the requested standards */
459
460                 nr_matches = hweight64(match_mask);
461                 if (nr_matches > best_nr_matches) {
462                         best_nr_matches = nr_matches;
463                         best_i = i;
464                 }
465         }
466
467         if (best_nr_matches > 0) {
468                 tuner_dbg("Selecting best matching firmware (%d bits) for "
469                           "type=", best_nr_matches);
470                 dump_firm_type(type);
471                 printk("(%x), id %016llx:\n", type, (unsigned long long)*id);
472                 i = best_i;
473                 goto found;
474         }
475
476         /*FIXME: Would make sense to seek for type "hint" match ? */
477
478         i = -ENOENT;
479         goto ret;
480
481 found:
482         *id = priv->firm[i].id;
483
484 ret:
485         tuner_dbg("%s firmware for type=", (i < 0) ? "Can't find" : "Found");
486         if (debug) {
487                 dump_firm_type(type);
488                 printk("(%x), id %016llx.\n", type, (unsigned long long)*id);
489         }
490         return i;
491 }
492
493 static int load_firmware(struct dvb_frontend *fe, unsigned int type,
494                          v4l2_std_id *id)
495 {
496         struct xc2028_data *priv = fe->tuner_priv;
497         int                pos, rc;
498         unsigned char      *p, *endp, buf[priv->ctrl.max_len];
499
500         tuner_dbg("%s called\n", __func__);
501
502         pos = seek_firmware(fe, type, id);
503         if (pos < 0)
504                 return pos;
505
506         tuner_info("Loading firmware for type=");
507         dump_firm_type(priv->firm[pos].type);
508         printk("(%x), id %016llx.\n", priv->firm[pos].type,
509                (unsigned long long)*id);
510
511         p = priv->firm[pos].ptr;
512         endp = p + priv->firm[pos].size;
513
514         while (p < endp) {
515                 __u16 size;
516
517                 /* Checks if there's enough bytes to read */
518                 if (p + sizeof(size) > endp) {
519                         tuner_err("Firmware chunk size is wrong\n");
520                         return -EINVAL;
521                 }
522
523                 size = le16_to_cpu(*(__u16 *) p);
524                 p += sizeof(size);
525
526                 if (size == 0xffff)
527                         return 0;
528
529                 if (!size) {
530                         /* Special callback command received */
531                         rc = priv->tuner_callback(priv->video_dev,
532                                                   XC2028_TUNER_RESET, 0);
533                         if (rc < 0) {
534                                 tuner_err("Error at RESET code %d\n",
535                                            (*p) & 0x7f);
536                                 return -EINVAL;
537                         }
538                         continue;
539                 }
540                 if (size >= 0xff00) {
541                         switch (size) {
542                         case 0xff00:
543                                 rc = priv->tuner_callback(priv->video_dev,
544                                                         XC2028_RESET_CLK, 0);
545                                 if (rc < 0) {
546                                         tuner_err("Error at RESET code %d\n",
547                                                   (*p) & 0x7f);
548                                         return -EINVAL;
549                                 }
550                                 break;
551                         default:
552                                 tuner_info("Invalid RESET code %d\n",
553                                            size & 0x7f);
554                                 return -EINVAL;
555
556                         }
557                         continue;
558                 }
559
560                 /* Checks for a sleep command */
561                 if (size & 0x8000) {
562                         msleep(size & 0x7fff);
563                         continue;
564                 }
565
566                 if ((size + p > endp)) {
567                         tuner_err("missing bytes: need %d, have %d\n",
568                                    size, (int)(endp - p));
569                         return -EINVAL;
570                 }
571
572                 buf[0] = *p;
573                 p++;
574                 size--;
575
576                 /* Sends message chunks */
577                 while (size > 0) {
578                         int len = (size < priv->ctrl.max_len - 1) ?
579                                    size : priv->ctrl.max_len - 1;
580
581                         memcpy(buf + 1, p, len);
582
583                         rc = i2c_send(priv, buf, len + 1);
584                         if (rc < 0) {
585                                 tuner_err("%d returned from send\n", rc);
586                                 return -EINVAL;
587                         }
588
589                         p += len;
590                         size -= len;
591                 }
592         }
593         return 0;
594 }
595
596 static int load_scode(struct dvb_frontend *fe, unsigned int type,
597                          v4l2_std_id *id, __u16 int_freq, int scode)
598 {
599         struct xc2028_data *priv = fe->tuner_priv;
600         int                pos, rc;
601         unsigned char      *p;
602
603         tuner_dbg("%s called\n", __func__);
604
605         if (!int_freq) {
606                 pos = seek_firmware(fe, type, id);
607                 if (pos < 0)
608                         return pos;
609         } else {
610                 for (pos = 0; pos < priv->firm_size; pos++) {
611                         if ((priv->firm[pos].int_freq == int_freq) &&
612                             (priv->firm[pos].type & HAS_IF))
613                                 break;
614                 }
615                 if (pos == priv->firm_size)
616                         return -ENOENT;
617         }
618
619         p = priv->firm[pos].ptr;
620
621         if (priv->firm[pos].type & HAS_IF) {
622                 if (priv->firm[pos].size != 12 * 16 || scode >= 16)
623                         return -EINVAL;
624                 p += 12 * scode;
625         } else {
626                 /* 16 SCODE entries per file; each SCODE entry is 12 bytes and
627                  * has a 2-byte size header in the firmware format. */
628                 if (priv->firm[pos].size != 14 * 16 || scode >= 16 ||
629                     le16_to_cpu(*(__u16 *)(p + 14 * scode)) != 12)
630                         return -EINVAL;
631                 p += 14 * scode + 2;
632         }
633
634         tuner_info("Loading SCODE for type=");
635         dump_firm_type_and_int_freq(priv->firm[pos].type,
636                                     priv->firm[pos].int_freq);
637         printk("(%x), id %016llx.\n", priv->firm[pos].type,
638                (unsigned long long)*id);
639
640         if (priv->firm_version < 0x0202)
641                 rc = send_seq(priv, {0x20, 0x00, 0x00, 0x00});
642         else
643                 rc = send_seq(priv, {0xa0, 0x00, 0x00, 0x00});
644         if (rc < 0)
645                 return -EIO;
646
647         rc = i2c_send(priv, p, 12);
648         if (rc < 0)
649                 return -EIO;
650
651         rc = send_seq(priv, {0x00, 0x8c});
652         if (rc < 0)
653                 return -EIO;
654
655         return 0;
656 }
657
658 static int check_firmware(struct dvb_frontend *fe, unsigned int type,
659                           v4l2_std_id std, __u16 int_freq)
660 {
661         struct xc2028_data         *priv = fe->tuner_priv;
662         struct firmware_properties new_fw;
663         int                        rc = 0, is_retry = 0;
664         u16                        version, hwmodel;
665         v4l2_std_id                std0;
666
667         tuner_dbg("%s called\n", __func__);
668
669         if (!priv->firm) {
670                 if (!priv->ctrl.fname) {
671                         tuner_info("xc2028/3028 firmware name not set!\n");
672                         return -EINVAL;
673                 }
674
675                 rc = load_all_firmwares(fe);
676                 if (rc < 0)
677                         return rc;
678         }
679
680         if (priv->ctrl.mts && !(type & FM))
681                 type |= MTS;
682
683 retry:
684         new_fw.type = type;
685         new_fw.id = std;
686         new_fw.std_req = std;
687         new_fw.scode_table = SCODE | priv->ctrl.scode_table;
688         new_fw.scode_nr = 0;
689         new_fw.int_freq = int_freq;
690
691         tuner_dbg("checking firmware, user requested type=");
692         if (debug) {
693                 dump_firm_type(new_fw.type);
694                 printk("(%x), id %016llx, ", new_fw.type,
695                        (unsigned long long)new_fw.std_req);
696                 if (!int_freq) {
697                         printk("scode_tbl ");
698                         dump_firm_type(priv->ctrl.scode_table);
699                         printk("(%x), ", priv->ctrl.scode_table);
700                 } else
701                         printk("int_freq %d, ", new_fw.int_freq);
702                 printk("scode_nr %d\n", new_fw.scode_nr);
703         }
704
705         /* No need to reload base firmware if it matches */
706         if (((BASE | new_fw.type) & BASE_TYPES) ==
707             (priv->cur_fw.type & BASE_TYPES)) {
708                 tuner_dbg("BASE firmware not changed.\n");
709                 goto skip_base;
710         }
711
712         /* Updating BASE - forget about all currently loaded firmware */
713         memset(&priv->cur_fw, 0, sizeof(priv->cur_fw));
714
715         /* Reset is needed before loading firmware */
716         rc = priv->tuner_callback(priv->video_dev,
717                                   XC2028_TUNER_RESET, 0);
718         if (rc < 0)
719                 goto fail;
720
721         /* BASE firmwares are all std0 */
722         std0 = 0;
723         rc = load_firmware(fe, BASE | new_fw.type, &std0);
724         if (rc < 0) {
725                 tuner_err("Error %d while loading base firmware\n",
726                           rc);
727                 goto fail;
728         }
729
730         /* Load INIT1, if needed */
731         tuner_dbg("Load init1 firmware, if exists\n");
732
733         rc = load_firmware(fe, BASE | INIT1 | new_fw.type, &std0);
734         if (rc == -ENOENT)
735                 rc = load_firmware(fe, (BASE | INIT1 | new_fw.type) & ~F8MHZ,
736                                    &std0);
737         if (rc < 0 && rc != -ENOENT) {
738                 tuner_err("Error %d while loading init1 firmware\n",
739                           rc);
740                 goto fail;
741         }
742
743 skip_base:
744         /*
745          * No need to reload standard specific firmware if base firmware
746          * was not reloaded and requested video standards have not changed.
747          */
748         if (priv->cur_fw.type == (BASE | new_fw.type) &&
749             priv->cur_fw.std_req == std) {
750                 tuner_dbg("Std-specific firmware already loaded.\n");
751                 goto skip_std_specific;
752         }
753
754         /* Reloading std-specific firmware forces a SCODE update */
755         priv->cur_fw.scode_table = 0;
756
757         rc = load_firmware(fe, new_fw.type, &new_fw.id);
758         if (rc == -ENOENT)
759                 rc = load_firmware(fe, new_fw.type & ~F8MHZ, &new_fw.id);
760
761         if (rc < 0)
762                 goto fail;
763
764 skip_std_specific:
765         if (priv->cur_fw.scode_table == new_fw.scode_table &&
766             priv->cur_fw.scode_nr == new_fw.scode_nr) {
767                 tuner_dbg("SCODE firmware already loaded.\n");
768                 goto check_device;
769         }
770
771         if (new_fw.type & FM)
772                 goto check_device;
773
774         /* Load SCODE firmware, if exists */
775         tuner_dbg("Trying to load scode %d\n", new_fw.scode_nr);
776
777         rc = load_scode(fe, new_fw.type | new_fw.scode_table, &new_fw.id,
778                         new_fw.int_freq, new_fw.scode_nr);
779
780 check_device:
781         if (xc2028_get_reg(priv, 0x0004, &version) < 0 ||
782             xc2028_get_reg(priv, 0x0008, &hwmodel) < 0) {
783                 tuner_err("Unable to read tuner registers.\n");
784                 goto fail;
785         }
786
787         tuner_info("Device is Xceive %d version %d.%d, "
788                    "firmware version %d.%d\n",
789                    hwmodel, (version & 0xf000) >> 12, (version & 0xf00) >> 8,
790                    (version & 0xf0) >> 4, version & 0xf);
791
792         /* Check firmware version against what we downloaded. */
793         if (priv->firm_version != ((version & 0xf0) << 4 | (version & 0x0f))) {
794                 tuner_err("Incorrect readback of firmware version.\n");
795                 goto fail;
796         }
797
798         /* Check that the tuner hardware model remains consistent over time. */
799         if (priv->hwmodel == 0 && (hwmodel == 2028 || hwmodel == 3028)) {
800                 priv->hwmodel = hwmodel;
801                 priv->hwvers  = version & 0xff00;
802         } else if (priv->hwmodel == 0 || priv->hwmodel != hwmodel ||
803                    priv->hwvers != (version & 0xff00)) {
804                 tuner_err("Read invalid device hardware information - tuner "
805                           "hung?\n");
806                 goto fail;
807         }
808
809         memcpy(&priv->cur_fw, &new_fw, sizeof(priv->cur_fw));
810
811         /*
812          * By setting BASE in cur_fw.type only after successfully loading all
813          * firmwares, we can:
814          * 1. Identify that BASE firmware with type=0 has been loaded;
815          * 2. Tell whether BASE firmware was just changed the next time through.
816          */
817         priv->cur_fw.type |= BASE;
818
819         return 0;
820
821 fail:
822         memset(&priv->cur_fw, 0, sizeof(priv->cur_fw));
823         if (!is_retry) {
824                 msleep(50);
825                 is_retry = 1;
826                 tuner_dbg("Retrying firmware load\n");
827                 goto retry;
828         }
829
830         if (rc == -ENOENT)
831                 rc = -EINVAL;
832         return rc;
833 }
834
835 static int xc2028_signal(struct dvb_frontend *fe, u16 *strength)
836 {
837         struct xc2028_data *priv = fe->tuner_priv;
838         u16                 frq_lock, signal = 0;
839         int                 rc;
840
841         tuner_dbg("%s called\n", __func__);
842
843         mutex_lock(&priv->lock);
844
845         /* Sync Lock Indicator */
846         rc = xc2028_get_reg(priv, 0x0002, &frq_lock);
847         if (rc < 0 || frq_lock == 0)
848                 goto ret;
849
850         /* Frequency is locked. Return signal quality */
851
852         /* Get SNR of the video signal */
853         rc = xc2028_get_reg(priv, 0x0040, &signal);
854         if (rc < 0)
855                 signal = -frq_lock;
856
857 ret:
858         mutex_unlock(&priv->lock);
859
860         *strength = signal;
861
862         return rc;
863 }
864
865 #define DIV 15625
866
867 static int generic_set_freq(struct dvb_frontend *fe, u32 freq /* in HZ */,
868                             enum tuner_mode new_mode,
869                             unsigned int type,
870                             v4l2_std_id std,
871                             u16 int_freq)
872 {
873         struct xc2028_data *priv = fe->tuner_priv;
874         int                rc = -EINVAL;
875         unsigned char      buf[4];
876         u32                div, offset = 0;
877
878         tuner_dbg("%s called\n", __func__);
879
880         mutex_lock(&priv->lock);
881
882         tuner_dbg("should set frequency %d kHz\n", freq / 1000);
883
884         if (check_firmware(fe, type, std, int_freq) < 0)
885                 goto ret;
886
887         /* On some cases xc2028 can disable video output, if
888          * very weak signals are received. By sending a soft
889          * reset, this is re-enabled. So, it is better to always
890          * send a soft reset before changing channels, to be sure
891          * that xc2028 will be in a safe state.
892          * Maybe this might also be needed for DTV.
893          */
894         if (new_mode == T_ANALOG_TV) {
895                 rc = send_seq(priv, {0x00, 0x00});
896         } else if (priv->cur_fw.type & ATSC) {
897                 offset = 1750000;
898         } else {
899                 offset = 2750000;
900                 /*
901                  * We must adjust the offset by 500kHz in two cases in order
902                  * to correctly center the IF output:
903                  * 1) When the ZARLINK456 or DIBCOM52 tables were explicitly
904                  *    selected and a 7MHz channel is tuned;
905                  * 2) When tuning a VHF channel with DTV78 firmware.
906                  */
907                 if (((priv->cur_fw.type & DTV7) &&
908                      (priv->cur_fw.scode_table & (ZARLINK456 | DIBCOM52))) ||
909                     ((priv->cur_fw.type & DTV78) && freq < 470000000))
910                         offset -= 500000;
911         }
912
913         div = (freq - offset + DIV / 2) / DIV;
914
915         /* CMD= Set frequency */
916         if (priv->firm_version < 0x0202)
917                 rc = send_seq(priv, {0x00, 0x02, 0x00, 0x00});
918         else
919                 rc = send_seq(priv, {0x80, 0x02, 0x00, 0x00});
920         if (rc < 0)
921                 goto ret;
922
923         /* Return code shouldn't be checked.
924            The reset CLK is needed only with tm6000.
925            Driver should work fine even if this fails.
926          */
927         priv->tuner_callback(priv->video_dev, XC2028_RESET_CLK, 1);
928
929         msleep(10);
930
931         buf[0] = 0xff & (div >> 24);
932         buf[1] = 0xff & (div >> 16);
933         buf[2] = 0xff & (div >> 8);
934         buf[3] = 0xff & (div);
935
936         rc = i2c_send(priv, buf, sizeof(buf));
937         if (rc < 0)
938                 goto ret;
939         msleep(100);
940
941         priv->frequency = freq;
942
943         tuner_dbg("divisor= %02x %02x %02x %02x (freq=%d.%03d)\n",
944                buf[0], buf[1], buf[2], buf[3],
945                freq / 1000000, (freq % 1000000) / 1000);
946
947         rc = 0;
948
949 ret:
950         mutex_unlock(&priv->lock);
951
952         return rc;
953 }
954
955 static int xc2028_set_analog_freq(struct dvb_frontend *fe,
956                               struct analog_parameters *p)
957 {
958         struct xc2028_data *priv = fe->tuner_priv;
959         unsigned int       type=0;
960
961         tuner_dbg("%s called\n", __func__);
962
963         if (p->mode == V4L2_TUNER_RADIO) {
964                 type |= FM;
965                 if (priv->ctrl.input1)
966                         type |= INPUT1;
967                 return generic_set_freq(fe, (625l * p->frequency) / 10,
968                                 T_ANALOG_TV, type, 0, 0);
969         }
970
971         /* if std is not defined, choose one */
972         if (!p->std)
973                 p->std = V4L2_STD_MN;
974
975         /* PAL/M, PAL/N, PAL/Nc and NTSC variants should use 6MHz firmware */
976         if (!(p->std & V4L2_STD_MN))
977                 type |= F8MHZ;
978
979         /* Add audio hack to std mask */
980         p->std |= parse_audio_std_option();
981
982         return generic_set_freq(fe, 62500l * p->frequency,
983                                 T_ANALOG_TV, type, p->std, 0);
984 }
985
986 static int xc2028_set_params(struct dvb_frontend *fe,
987                              struct dvb_frontend_parameters *p)
988 {
989         struct xc2028_data *priv = fe->tuner_priv;
990         unsigned int       type=0;
991         fe_bandwidth_t     bw = BANDWIDTH_8_MHZ;
992         u16                demod = 0;
993
994         tuner_dbg("%s called\n", __func__);
995
996         if (priv->ctrl.d2633)
997                 type |= D2633;
998         else
999                 type |= D2620;
1000
1001         switch(fe->ops.info.type) {
1002         case FE_OFDM:
1003                 bw = p->u.ofdm.bandwidth;
1004                 break;
1005         case FE_QAM:
1006                 tuner_info("WARN: There are some reports that "
1007                            "QAM 6 MHz doesn't work.\n"
1008                            "If this works for you, please report by "
1009                            "e-mail to: v4l-dvb-maintainer@linuxtv.org\n");
1010                 bw = BANDWIDTH_6_MHZ;
1011                 type |= QAM;
1012                 break;
1013         case FE_ATSC:
1014                 bw = BANDWIDTH_6_MHZ;
1015                 /* The only ATSC firmware (at least on v2.7) is D2633,
1016                    so overrides ctrl->d2633 */
1017                 type |= ATSC| D2633;
1018                 type &= ~D2620;
1019                 break;
1020         /* DVB-S is not supported */
1021         default:
1022                 return -EINVAL;
1023         }
1024
1025         switch (bw) {
1026         case BANDWIDTH_8_MHZ:
1027                 if (p->frequency < 470000000)
1028                         priv->ctrl.vhfbw7 = 0;
1029                 else
1030                         priv->ctrl.uhfbw8 = 1;
1031                 type |= (priv->ctrl.vhfbw7 && priv->ctrl.uhfbw8) ? DTV78 : DTV8;
1032                 type |= F8MHZ;
1033                 break;
1034         case BANDWIDTH_7_MHZ:
1035                 if (p->frequency < 470000000)
1036                         priv->ctrl.vhfbw7 = 1;
1037                 else
1038                         priv->ctrl.uhfbw8 = 0;
1039                 type |= (priv->ctrl.vhfbw7 && priv->ctrl.uhfbw8) ? DTV78 : DTV7;
1040                 type |= F8MHZ;
1041                 break;
1042         case BANDWIDTH_6_MHZ:
1043                 type |= DTV6;
1044                 priv->ctrl.vhfbw7 = 0;
1045                 priv->ctrl.uhfbw8 = 0;
1046                 break;
1047         default:
1048                 tuner_err("error: bandwidth not supported.\n");
1049         };
1050
1051         /* All S-code tables need a 200kHz shift */
1052         if (priv->ctrl.demod)
1053                 demod = priv->ctrl.demod + 200;
1054
1055         return generic_set_freq(fe, p->frequency,
1056                                 T_DIGITAL_TV, type, 0, demod);
1057 }
1058
1059
1060 static int xc2028_dvb_release(struct dvb_frontend *fe)
1061 {
1062         struct xc2028_data *priv = fe->tuner_priv;
1063
1064         tuner_dbg("%s called\n", __func__);
1065
1066         mutex_lock(&xc2028_list_mutex);
1067
1068         priv->count--;
1069
1070         if (!priv->count) {
1071                 list_del(&priv->xc2028_list);
1072
1073                 kfree(priv->ctrl.fname);
1074
1075                 free_firmware(priv);
1076                 kfree(priv);
1077                 fe->tuner_priv = NULL;
1078         }
1079
1080         mutex_unlock(&xc2028_list_mutex);
1081
1082         return 0;
1083 }
1084
1085 static int xc2028_get_frequency(struct dvb_frontend *fe, u32 *frequency)
1086 {
1087         struct xc2028_data *priv = fe->tuner_priv;
1088
1089         tuner_dbg("%s called\n", __func__);
1090
1091         *frequency = priv->frequency;
1092
1093         return 0;
1094 }
1095
1096 static int xc2028_set_config(struct dvb_frontend *fe, void *priv_cfg)
1097 {
1098         struct xc2028_data *priv = fe->tuner_priv;
1099         struct xc2028_ctrl *p    = priv_cfg;
1100         int                 rc   = 0;
1101
1102         tuner_dbg("%s called\n", __func__);
1103
1104         mutex_lock(&priv->lock);
1105
1106         memcpy(&priv->ctrl, p, sizeof(priv->ctrl));
1107         if (priv->ctrl.max_len < 9)
1108                 priv->ctrl.max_len = 13;
1109
1110         if (p->fname) {
1111                 if (priv->ctrl.fname && strcmp(p->fname, priv->ctrl.fname)) {
1112                         kfree(priv->ctrl.fname);
1113                         free_firmware(priv);
1114                 }
1115
1116                 priv->ctrl.fname = kstrdup(p->fname, GFP_KERNEL);
1117                 if (priv->ctrl.fname == NULL)
1118                         rc = -ENOMEM;
1119         }
1120
1121         mutex_unlock(&priv->lock);
1122
1123         return rc;
1124 }
1125
1126 static const struct dvb_tuner_ops xc2028_dvb_tuner_ops = {
1127         .info = {
1128                  .name = "Xceive XC3028",
1129                  .frequency_min = 42000000,
1130                  .frequency_max = 864000000,
1131                  .frequency_step = 50000,
1132                  },
1133
1134         .set_config        = xc2028_set_config,
1135         .set_analog_params = xc2028_set_analog_freq,
1136         .release           = xc2028_dvb_release,
1137         .get_frequency     = xc2028_get_frequency,
1138         .get_rf_strength   = xc2028_signal,
1139         .set_params        = xc2028_set_params,
1140 };
1141
1142 struct dvb_frontend *xc2028_attach(struct dvb_frontend *fe,
1143                                    struct xc2028_config *cfg)
1144 {
1145         struct xc2028_data *priv;
1146         void               *video_dev;
1147
1148         if (debug)
1149                 printk(KERN_DEBUG "xc2028: Xcv2028/3028 init called!\n");
1150
1151         if (NULL == cfg)
1152                 return NULL;
1153
1154         if (!fe) {
1155                 printk(KERN_ERR "xc2028: No frontend!\n");
1156                 return NULL;
1157         }
1158
1159         video_dev = cfg->i2c_adap->algo_data;
1160
1161         if (debug)
1162                 printk(KERN_DEBUG "xc2028: video_dev =%p\n", video_dev);
1163
1164         mutex_lock(&xc2028_list_mutex);
1165
1166         list_for_each_entry(priv, &xc2028_list, xc2028_list) {
1167                 if (&priv->i2c_props.adap->dev == &cfg->i2c_adap->dev) {
1168                         video_dev = NULL;
1169                         if (debug)
1170                                 printk(KERN_DEBUG "xc2028: reusing device\n");
1171
1172                         break;
1173                 }
1174         }
1175
1176         if (video_dev) {
1177                 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
1178                 if (priv == NULL) {
1179                         mutex_unlock(&xc2028_list_mutex);
1180                         return NULL;
1181                 }
1182
1183                 priv->i2c_props.addr = cfg->i2c_addr;
1184                 priv->i2c_props.adap = cfg->i2c_adap;
1185                 priv->i2c_props.name = "xc2028";
1186
1187                 priv->video_dev = video_dev;
1188                 priv->tuner_callback = cfg->callback;
1189                 priv->ctrl.max_len = 13;
1190
1191                 mutex_init(&priv->lock);
1192
1193                 list_add_tail(&priv->xc2028_list, &xc2028_list);
1194         }
1195
1196         fe->tuner_priv = priv;
1197         priv->count++;
1198
1199         if (debug)
1200                 printk(KERN_DEBUG "xc2028: usage count is %i\n", priv->count);
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 }
1214
1215 EXPORT_SYMBOL(xc2028_attach);
1216
1217 MODULE_DESCRIPTION("Xceive xc2028/xc3028 tuner driver");
1218 MODULE_AUTHOR("Michel Ludwig <michel.ludwig@gmail.com>");
1219 MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@infradead.org>");
1220 MODULE_LICENSE("GPL");