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