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