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